-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCertificateRequest.cs
More file actions
396 lines (347 loc) · 10.7 KB
/
CertificateRequest.cs
File metadata and controls
396 lines (347 loc) · 10.7 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.Collections.Generic;
using System.Text;
namespace Waher.Security.PKCS
{
/// <summary>
/// Contains information about a Certificate Signing Request (CSR).
/// </summary>
public class CertificateRequest
{
private readonly SignatureAlgorithm signatureAlgorithm;
private string commonName = null; // 2.5.4.3 CN
private string surname = null; // 2.5.4.4
private string serialNumber = null; // 2.5.4.5
private string country = null; // 2.5.4.6 C
private string locality = null; // 2.5.4.7 L
private string stateOrProvince = null; // 2.5.4.8 ST
private string streetAddress = null; // 2.5.4.9
private string organization = null; // 2.5.4.10 O
private string organizationalUnit = null; // 2.5.4.11 OU
private string title = null; // 2.5.4.12
private string description = null; // 2.5.4.13
private string postalAddress = null; // 2.5.4.16
private string postalCode = null; // 2.5.4.17
private string postOfficeBox = null; // 2.5.4.18
private string physicalDeliveryOfficeName = null; // 2.5.4.19
private string telephoneNumber = null; // 2.5.4.20
private string registeredAddress = null; // 2.5.4.26
private string presentationAddress = null; // 2.5.4.29
private string name = null; // 2.5.4.41
private string givenName = null; // 2.5.4.42
private string initials = null; // 2.5.4.43
private string distinguishedName = null; // 2.5.4.49
private string houseIdentifier = null; // 2.5.4.51
private string[] subjectAlternativeNames = null; // 2.5.29.17
private string emailAddress = null; // 1.2.840.113549.1.9.1
/// <summary>
/// Contains information about a Certificate Signing Request (CSR).
/// </summary>
/// <param name="SignatureAlgorithm">Signature algorithm.</param>
public CertificateRequest(SignatureAlgorithm SignatureAlgorithm)
{
this.signatureAlgorithm = SignatureAlgorithm;
}
/// <summary>
/// Signature algorithm.
/// </summary>
public SignatureAlgorithm SignatureAlgorithm => this.signatureAlgorithm;
/// <summary>
/// Common Name (OID 2.5.4.3)
/// </summary>
public string CommonName
{
get => this.commonName;
set => this.commonName = value;
}
/// <summary>
/// Surname (OID 2.5.4.4)
/// </summary>
public string Surname
{
get => this.surname;
set => this.surname = value;
}
/// <summary>
/// Serial Number (OID 2.5.4.5)
/// </summary>
public string SerialNumber
{
get => this.serialNumber;
set => this.serialNumber = value;
}
/// <summary>
/// Country Name (OID 2.5.4.6)
/// </summary>
public string Country
{
get => this.country;
set => this.country = value;
}
/// <summary>
/// Locality Name (OID 2.5.4.7)
/// </summary>
public string Locality
{
get => this.locality;
set => this.locality = value;
}
/// <summary>
/// Country Name (OID 2.5.4.8)
/// </summary>
public string StateOrProvince
{
get => this.stateOrProvince;
set => this.stateOrProvince = value;
}
/// <summary>
/// Street Address (OID 2.5.4.9)
/// </summary>
public string StreetAddress
{
get => this.streetAddress;
set => this.streetAddress = value;
}
/// <summary>
/// Organization Name (OID 2.5.4.10)
/// </summary>
public string Organization
{
get => this.organization;
set => this.organization = value;
}
/// <summary>
/// Organizational Unit Name (OID 2.5.4.11)
/// </summary>
public string OrganizationalUnit
{
get => this.organizationalUnit;
set => this.organizationalUnit = value;
}
/// <summary>
/// Title (OID 2.5.4.12)
/// </summary>
public string Title
{
get => this.title;
set => this.title = value;
}
/// <summary>
/// Description (OID 2.5.4.13)
/// </summary>
public string Description
{
get => this.description;
set => this.description = value;
}
/// <summary>
/// Postal Address (OID 2.5.4.16)
/// </summary>
public string PostalAddress
{
get => this.postalAddress;
set => this.postalAddress = value;
}
/// <summary>
/// Postal Code (OID 2.5.4.17)
/// </summary>
public string PostalCode
{
get => this.postalCode;
set => this.postalCode = value;
}
/// <summary>
/// Post Office Box (OID 2.5.4.18)
/// </summary>
public string PostOfficeBox
{
get => this.postOfficeBox;
set => this.postOfficeBox = value;
}
/// <summary>
/// Physical Delivery Office Name (OID 2.5.4.19)
/// </summary>
public string PhysicalDeliveryOfficeName
{
get => this.physicalDeliveryOfficeName;
set => this.physicalDeliveryOfficeName = value;
}
/// <summary>
/// Telephone number (OID 2.5.4.20)
/// </summary>
public string TelephoneNumber
{
get => this.telephoneNumber;
set => this.telephoneNumber = value;
}
/// <summary>
/// Registered Address (OID 2.5.4.26)
/// </summary>
public string RegisteredAddress
{
get => this.registeredAddress;
set => this.registeredAddress = value;
}
/// <summary>
/// Presentation Address (OID 2.5.4.29)
/// </summary>
public string PresentationAddress
{
get => this.presentationAddress;
set => this.presentationAddress = value;
}
/// <summary>
/// Name (OID 2.5.4.41)
/// </summary>
public string Name
{
get => this.name;
set => this.name = value;
}
/// <summary>
/// Given Name (OID 2.5.4.42)
/// </summary>
public string GivenName
{
get => this.givenName;
set => this.givenName = value;
}
/// <summary>
/// Initials (OID 2.5.4.43)
/// </summary>
public string Initials
{
get => this.initials;
set => this.initials = value;
}
/// <summary>
/// Distinguished name (OID 2.5.4.49)
/// </summary>
public string DistinguishedName
{
get => this.distinguishedName;
set => this.distinguishedName = value;
}
/// <summary>
/// House identifier (OID 2.5.4.51)
/// </summary>
public string HouseIdentifier
{
get => this.houseIdentifier;
set => this.houseIdentifier = value;
}
/// <summary>
/// Subject Alternative Names (OID 2.5.29.17)
/// </summary>
public string[] SubjectAlternativeNames
{
get => this.subjectAlternativeNames;
set => this.subjectAlternativeNames = value;
}
/// <summary>
/// e-Mail Address (OID 1.2.840.113549.1.9.1)
/// </summary>
public string EMailAddress
{
get => this.emailAddress;
set => this.emailAddress = value;
}
/// <summary>
/// Building a Certificate Signing Request (CSR) in accordance with RFC 2986
/// </summary>
/// <returns>CSR</returns>
public byte[] BuildCSR()
{
DerEncoder DER = new DerEncoder();
DER.StartSEQUENCE(); // CertificationRequestInfo
DER.INTEGER(0); // Version
DER.StartSEQUENCE(); // subject
this.EncodeIfDefined(DER, "2.5.4.3", this.commonName);
this.EncodeIfDefined(DER, "2.5.4.4", this.surname);
this.EncodeIfDefined(DER, "2.5.4.5", this.serialNumber);
this.EncodeIfDefined(DER, "2.5.4.6", this.country);
this.EncodeIfDefined(DER, "2.5.4.7", this.locality);
this.EncodeIfDefined(DER, "2.5.4.8", this.stateOrProvince);
this.EncodeIfDefined(DER, "2.5.4.9", this.streetAddress);
this.EncodeIfDefined(DER, "2.5.4.10", this.organization);
this.EncodeIfDefined(DER, "2.5.4.11", this.organizationalUnit);
this.EncodeIfDefined(DER, "2.5.4.12", this.title);
this.EncodeIfDefined(DER, "2.5.4.13", this.description);
this.EncodeIfDefined(DER, "2.5.4.16", this.postalAddress);
this.EncodeIfDefined(DER, "2.5.4.17", this.postalCode);
this.EncodeIfDefined(DER, "2.5.4.18", this.postOfficeBox);
this.EncodeIfDefined(DER, "2.5.4.19", this.physicalDeliveryOfficeName);
this.EncodeIfDefined(DER, "2.5.4.20", this.telephoneNumber);
this.EncodeIfDefined(DER, "2.5.4.26", this.registeredAddress);
this.EncodeIfDefined(DER, "2.5.4.29", this.presentationAddress);
this.EncodeIfDefined(DER, "2.5.4.41", this.name);
this.EncodeIfDefined(DER, "2.5.4.42", this.givenName);
this.EncodeIfDefined(DER, "2.5.4.43", this.initials);
this.EncodeIfDefined(DER, "2.5.4.49", this.distinguishedName);
this.EncodeIfDefined(DER, "2.5.4.51", this.houseIdentifier);
this.EncodeIfDefined(DER, "1.2.840.113549.1.9.1", this.emailAddress);
DER.EndSEQUENCE(); // end of subject
DER.StartSEQUENCE(); // subjectPKInfo
DER.StartSEQUENCE(); // algorithm
DER.OBJECT_IDENTIFIER(this.signatureAlgorithm.PkiAlgorithmOID);
DER.NULL(); // No parameters
DER.EndSEQUENCE(); // end of algorithm
DER.StartBITSTRING(); // subjectPublicKey
this.signatureAlgorithm.ExportPublicKey(DER);
DER.EndBITSTRING(); // end of subjectPublicKey
DER.EndSEQUENCE(); // end of subjectPKInfo
DER.StartContent(Asn1TypeClass.ContextSpecific); // attributes
if (!(this.subjectAlternativeNames is null) && this.subjectAlternativeNames.Length > 0)
{
DER.StartSEQUENCE();
DER.OBJECT_IDENTIFIER("1.2.840.113549.1.9.14"); // extensionRequest
DER.StartSET();
DER.StartSEQUENCE();
DER.StartSEQUENCE();
DER.OBJECT_IDENTIFIER("2.5.29.17");
DER.StartOCTET_STRING();
DER.StartSEQUENCE();
foreach (string s in this.subjectAlternativeNames)
{
int Pos = DER.Position;
DER.IA5_STRING(s);
DER[Pos] = 0x82; // Encoded as Context-specific INTEGER...
}
DER.EndSEQUENCE();
DER.EndOCTET_STRING();
DER.EndSEQUENCE();
DER.EndSEQUENCE();
DER.EndSET();
DER.EndSEQUENCE();
}
DER.EndContent(Asn1TypeClass.ContextSpecific); // end of attributes
DER.EndSEQUENCE(); // end of CertificationRequestInfo
byte[] CertificationRequestInfo = DER.ToArray();
DER.Clear();
DER.StartSEQUENCE(); // CertificationRequest
DER.Raw(CertificationRequestInfo);
DER.StartSEQUENCE(); // signatureAlgorithm
DER.OBJECT_IDENTIFIER(this.signatureAlgorithm.HashAlgorithmOID);
DER.NULL(); // parameters
DER.EndSEQUENCE(); // End of signatureAlgorithm
DER.BITSTRING(this.signatureAlgorithm.Sign(CertificationRequestInfo)); // signature
DER.EndSEQUENCE(); // end of CertificationRequest
return DER.ToArray();
}
private void EncodeIfDefined(DerEncoder DER, string OID, string Value)
{
if (!(Value is null))
{
DER.StartSET();
DER.StartSEQUENCE();
DER.OBJECT_IDENTIFIER(OID);
if (DerEncoder.IsPrintable(Value))
DER.PRINTABLE_STRING(Value);
else
DER.IA5_STRING(Value);
DER.EndSEQUENCE();
DER.EndSET();
}
}
}
}