1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Tls
{
/// <summary>Parsing and encoding of a <i>CertificateRequest</i> struct from RFC 4346.</summary>
/// <remarks>
/// <pre>
/// struct {
/// ClientCertificateType certificate_types<1..2^8-1>;
/// DistinguishedName certificate_authorities<3..2^16-1>;
/// } CertificateRequest;
/// </pre>
/// Updated for RFC 5246:
/// <pre>
/// struct {
/// ClientCertificateType certificate_types <1..2 ^ 8 - 1>;
/// SignatureAndHashAlgorithm supported_signature_algorithms <2 ^ 16 - 1>;
/// DistinguishedName certificate_authorities <0..2 ^ 16 - 1>;
/// } CertificateRequest;
/// </pre>
/// Revised for RFC 8446:
/// <pre>
/// struct {
/// opaque certificate_request_context <0..2 ^ 8 - 1>;
/// Extension extensions <2..2 ^ 16 - 1>;
/// } CertificateRequest;
/// </pre>
/// </remarks>
/// <seealso cref="ClientCertificateType"/>
/// <seealso cref="X509Name"/>
public sealed class CertificateRequest
{
/// <exception cref="IOException"/>
private static IList CheckSupportedSignatureAlgorithms(IList supportedSignatureAlgorithms,
short alertDescription)
{
if (null == supportedSignatureAlgorithms)
throw new TlsFatalAlert(alertDescription, "'signature_algorithms' is required");
return supportedSignatureAlgorithms;
}
private readonly byte[] m_certificateRequestContext;
private readonly short[] m_certificateTypes;
private readonly IList m_supportedSignatureAlgorithms;
private readonly IList m_supportedSignatureAlgorithmsCert;
private readonly IList m_certificateAuthorities;
/// <param name="certificateTypes">see <see cref="ClientCertificateType"/> for valid constants.</param>
/// <param name="supportedSignatureAlgorithms"></param>
/// <param name="certificateAuthorities">an <see cref="IList"/> of <see cref="X509Name"/>.</param>
public CertificateRequest(short[] certificateTypes, IList supportedSignatureAlgorithms,
IList certificateAuthorities)
: this(null, certificateTypes, supportedSignatureAlgorithms, null, certificateAuthorities)
{
}
// TODO[tls13] Prefer to manage the certificateRequestContext internally only?
/// <exception cref="IOException"/>
public CertificateRequest(byte[] certificateRequestContext, IList supportedSignatureAlgorithms,
IList supportedSignatureAlgorithmsCert, IList certificateAuthorities)
: this(certificateRequestContext, null,
CheckSupportedSignatureAlgorithms(supportedSignatureAlgorithms, AlertDescription.internal_error),
supportedSignatureAlgorithmsCert, certificateAuthorities)
{
/*
* TODO[tls13] Removed certificateTypes, added certificate_request_context, added extensions
* (required: signature_algorithms, optional: status_request, signed_certificate_timestamp,
* certificate_authorities, oid_filters, signature_algorithms_cert)
*/
}
private CertificateRequest(byte[] certificateRequestContext, short[] certificateTypes,
IList supportedSignatureAlgorithms, IList supportedSignatureAlgorithmsCert, IList certificateAuthorities)
{
if (null != certificateRequestContext && !TlsUtilities.IsValidUint8(certificateRequestContext.Length))
throw new ArgumentException("cannot be longer than 255", "certificateRequestContext");
if (null != certificateTypes
&& (certificateTypes.Length < 1 || !TlsUtilities.IsValidUint8(certificateTypes.Length)))
{
throw new ArgumentException("should have length from 1 to 255", "certificateTypes");
}
this.m_certificateRequestContext = TlsUtilities.Clone(certificateRequestContext);
this.m_certificateTypes = certificateTypes;
this.m_supportedSignatureAlgorithms = supportedSignatureAlgorithms;
this.m_supportedSignatureAlgorithmsCert = supportedSignatureAlgorithmsCert;
this.m_certificateAuthorities = certificateAuthorities;
}
public byte[] GetCertificateRequestContext()
{
return TlsUtilities.Clone(m_certificateRequestContext);
}
/// <returns>an array of certificate types</returns>
/// <seealso cref="ClientCertificateType"/>
public short[] CertificateTypes
{
get { return m_certificateTypes; }
}
/// <returns>an <see cref="IList"/> of <see cref="SignatureAndHashAlgorithm"/> (or null before TLS 1.2).
/// </returns>
public IList SupportedSignatureAlgorithms
{
get { return m_supportedSignatureAlgorithms; }
}
/// <returns>an optional <see cref="IList"/> of <see cref="SignatureAndHashAlgorithm"/>. May be non-null from
/// TLS 1.3 onwards.</returns>
public IList SupportedSignatureAlgorithmsCert
{
get { return m_supportedSignatureAlgorithmsCert; }
}
/// <returns>an <see cref="IList"/> of <see cref="X509Name"/>.</returns>
public IList CertificateAuthorities
{
get { return m_certificateAuthorities; }
}
public bool HasCertificateRequestContext(byte[] certificateRequestContext)
{
return Arrays.AreEqual(m_certificateRequestContext, certificateRequestContext);
}
/// <summary>Encode this <see cref="CertificateRequest"/> to a <see cref="Stream"/>.</summary>
/// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
/// <param name="output">the <see cref="Stream"/> to encode to.</param>
/// <exception cref="IOException"/>
public void Encode(TlsContext context, Stream output)
{
ProtocolVersion negotiatedVersion = context.ServerVersion;
bool isTlsV12 = TlsUtilities.IsTlsV12(negotiatedVersion);
bool isTlsV13 = TlsUtilities.IsTlsV13(negotiatedVersion);
if (isTlsV13 != (null != m_certificateRequestContext) ||
isTlsV13 != (null == m_certificateTypes) ||
isTlsV12 != (null != m_supportedSignatureAlgorithms) ||
(!isTlsV13 && (null != m_supportedSignatureAlgorithmsCert)))
{
throw new InvalidOperationException();
}
if (isTlsV13)
{
TlsUtilities.WriteOpaque8(m_certificateRequestContext, output);
IDictionary extensions = Platform.CreateHashtable();
TlsExtensionsUtilities.AddSignatureAlgorithmsExtension(extensions, m_supportedSignatureAlgorithms);
if (null != m_supportedSignatureAlgorithmsCert)
{
TlsExtensionsUtilities.AddSignatureAlgorithmsCertExtension(extensions,
m_supportedSignatureAlgorithmsCert);
}
if (null != m_certificateAuthorities)
{
TlsExtensionsUtilities.AddCertificateAuthoritiesExtension(extensions, m_certificateAuthorities);
}
byte[] extEncoding = TlsProtocol.WriteExtensionsData(extensions);
TlsUtilities.WriteOpaque16(extEncoding, output);
return;
}
TlsUtilities.WriteUint8ArrayWithUint8Length(m_certificateTypes, output);
if (isTlsV12)
{
// TODO Check whether SignatureAlgorithm.anonymous is allowed here
TlsUtilities.EncodeSupportedSignatureAlgorithms(m_supportedSignatureAlgorithms, output);
}
if (m_certificateAuthorities == null || m_certificateAuthorities.Count < 1)
{
TlsUtilities.WriteUint16(0, output);
}
else
{
IList derEncodings = Platform.CreateArrayList(m_certificateAuthorities.Count);
int totalLength = 0;
foreach (X509Name certificateAuthority in m_certificateAuthorities)
{
byte[] derEncoding = certificateAuthority.GetEncoded(Asn1Encodable.Der);
derEncodings.Add(derEncoding);
totalLength += derEncoding.Length + 2;
}
TlsUtilities.CheckUint16(totalLength);
TlsUtilities.WriteUint16(totalLength, output);
foreach (byte[] derEncoding in derEncodings)
{
TlsUtilities.WriteOpaque16(derEncoding, output);
}
}
}
/// <summary>Parse a <see cref="CertificateRequest"/> from a <see cref="Stream"/></summary>
/// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
/// <param name="input">the <see cref="Stream"/> to parse from.</param>
/// <returns>a <see cref="CertificateRequest"/> object.</returns>
/// <exception cref="IOException"/>
public static CertificateRequest Parse(TlsContext context, Stream input)
{
ProtocolVersion negotiatedVersion = context.ServerVersion;
bool isTlsV13 = TlsUtilities.IsTlsV13(negotiatedVersion);
if (isTlsV13)
{
byte[] certificateRequestContext = TlsUtilities.ReadOpaque8(input);
/*
* TODO[tls13] required: signature_algorithms; optional: status_request,
* signed_certificate_timestamp, certificate_authorities, oid_filters,
* signature_algorithms_cert
*/
byte[] extEncoding = TlsUtilities.ReadOpaque16(input);
IDictionary extensions = TlsProtocol.ReadExtensionsData13(HandshakeType.certificate_request,
extEncoding);
IList supportedSignatureAlgorithms13 = CheckSupportedSignatureAlgorithms(
TlsExtensionsUtilities.GetSignatureAlgorithmsExtension(extensions),
AlertDescription.missing_extension);
IList supportedSignatureAlgorithmsCert13 = TlsExtensionsUtilities
.GetSignatureAlgorithmsCertExtension(extensions);
IList certificateAuthorities13 = TlsExtensionsUtilities.GetCertificateAuthoritiesExtension(extensions);
return new CertificateRequest(certificateRequestContext, supportedSignatureAlgorithms13,
supportedSignatureAlgorithmsCert13, certificateAuthorities13);
}
bool isTLSv12 = TlsUtilities.IsTlsV12(negotiatedVersion);
short[] certificateTypes = TlsUtilities.ReadUint8ArrayWithUint8Length(input, 1);
IList supportedSignatureAlgorithms = null;
if (isTLSv12)
{
supportedSignatureAlgorithms = TlsUtilities.ParseSupportedSignatureAlgorithms(input);
}
IList certificateAuthorities = null;
{
byte[] certAuthData = TlsUtilities.ReadOpaque16(input);
if (certAuthData.Length > 0)
{
certificateAuthorities = Platform.CreateArrayList();
MemoryStream bis = new MemoryStream(certAuthData, false);
do
{
byte[] derEncoding = TlsUtilities.ReadOpaque16(bis, 1);
Asn1Object asn1 = TlsUtilities.ReadAsn1Object(derEncoding);
X509Name ca = X509Name.GetInstance(asn1);
TlsUtilities.RequireDerEncoding(ca, derEncoding);
certificateAuthorities.Add(ca);
}
while (bis.Position < bis.Length);
}
}
return new CertificateRequest(certificateTypes, supportedSignatureAlgorithms, certificateAuthorities);
}
}
}
|