forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertificateAuthority.cs
More file actions
936 lines (782 loc) · 33.3 KB
/
Copy pathCertificateAuthority.cs
File metadata and controls
936 lines (782 loc) · 33.3 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using Xunit;
namespace System.Security.Cryptography.X509Certificates.Tests.Common
{
// This class represents only a portion of what is required to be a proper Certificate Authority.
//
// Please do not use it as the basis for any real Public/Private Key Infrastructure (PKI) system
// without understanding all of the portions of proper CA management that you're skipping.
//
// At minimum, read the current baseline requirements of the CA/Browser Forum.
[Flags]
public enum PkiOptions
{
None = 0,
IssuerRevocationViaCrl = 1 << 0,
IssuerRevocationViaOcsp = 1 << 1,
EndEntityRevocationViaCrl = 1 << 2,
EndEntityRevocationViaOcsp = 1 << 3,
CrlEverywhere = IssuerRevocationViaCrl | EndEntityRevocationViaCrl,
OcspEverywhere = IssuerRevocationViaOcsp | EndEntityRevocationViaOcsp,
AllIssuerRevocation = IssuerRevocationViaCrl | IssuerRevocationViaOcsp,
AllEndEntityRevocation = EndEntityRevocationViaCrl | EndEntityRevocationViaOcsp,
AllRevocation = CrlEverywhere | OcspEverywhere,
IssuerAuthorityHasDesignatedOcspResponder = 1 << 16,
RootAuthorityHasDesignatedOcspResponder = 1 << 17,
NoIssuerCertDistributionUri = 1 << 18,
NoRootCertDistributionUri = 1 << 18,
}
internal sealed class CertificateAuthority : IDisposable
{
private static readonly Asn1Tag s_context0 = new Asn1Tag(TagClass.ContextSpecific, 0);
private static readonly Asn1Tag s_context1 = new Asn1Tag(TagClass.ContextSpecific, 1);
private static readonly Asn1Tag s_context2 = new Asn1Tag(TagClass.ContextSpecific, 2);
private static readonly Asn1Tag s_context4 = new Asn1Tag(TagClass.ContextSpecific, 4);
private static readonly X500DistinguishedName s_nonParticipatingName =
new X500DistinguishedName("CN=The Ghost in the Machine");
private static readonly X509BasicConstraintsExtension s_eeConstraints =
new X509BasicConstraintsExtension(false, false, 0, false);
private static readonly X509KeyUsageExtension s_caKeyUsage =
new X509KeyUsageExtension(
X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign,
critical: false);
private static readonly X509KeyUsageExtension s_eeKeyUsage =
new X509KeyUsageExtension(
X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DataEncipherment,
critical: false);
private static readonly X509EnhancedKeyUsageExtension s_ocspResponderEku =
new X509EnhancedKeyUsageExtension(
new OidCollection
{
new Oid("1.3.6.1.5.5.7.3.9", null),
},
critical: false);
private static readonly X509EnhancedKeyUsageExtension s_tlsClientEku =
new X509EnhancedKeyUsageExtension(
new OidCollection
{
new Oid("1.3.6.1.5.5.7.3.2", null)
},
false);
private X509Certificate2 _cert;
private byte[] _certData;
private X509Extension _cdpExtension;
private X509Extension _aiaExtension;
private X509Extension _akidExtension;
private List<(byte[], DateTimeOffset)> _revocationList;
private byte[] _crl;
private int _crlNumber;
private DateTimeOffset _crlExpiry;
private X509Certificate2 _ocspResponder;
private byte[] _dnHash;
internal string AiaHttpUri { get; }
internal string CdpUri { get; }
internal string OcspUri { get; }
internal bool CorruptRevocationSignature { get; set; }
internal DateTimeOffset? RevocationExpiration { get; set; }
internal bool CorruptRevocationIssuerName { get; set; }
// All keys created in this method are smaller than recommended,
// but they only live for a few seconds (at most),
// and never communicate out of process.
const int DefaultKeySize = 1024;
internal CertificateAuthority(
X509Certificate2 cert,
string aiaHttpUrl,
string cdpUrl,
string ocspUrl)
{
_cert = cert;
AiaHttpUri = aiaHttpUrl;
CdpUri = cdpUrl;
OcspUri = ocspUrl;
}
public void Dispose()
{
_cert.Dispose();
}
internal string SubjectName => _cert.Subject;
internal bool HasOcspDelegation => _ocspResponder != null;
internal string OcspResponderSubjectName => (_ocspResponder ?? _cert).Subject;
internal X509Certificate2 CloneIssuerCert()
{
return new X509Certificate2(_cert.RawData);
}
internal void Revoke(X509Certificate2 certificate, DateTimeOffset revocationTime)
{
if (!certificate.IssuerName.RawData.SequenceEqual(_cert.SubjectName.RawData))
{
throw new ArgumentException("Certificate was not from this issuer", nameof(certificate));
}
if (_revocationList == null)
{
_revocationList = new List<(byte[], DateTimeOffset)>();
}
byte[] serial = certificate.GetSerialNumber();
Array.Reverse(serial);
_revocationList.Add((serial, revocationTime));
_crl = null;
}
internal X509Certificate2 CreateSubordinateCA(
string subject,
RSA publicKey,
int? depthLimit = null)
{
return CreateCertificate(
subject,
publicKey,
TimeSpan.FromMinutes(1),
new X509ExtensionCollection() {
new X509BasicConstraintsExtension(
certificateAuthority: true,
depthLimit.HasValue,
depthLimit.GetValueOrDefault(),
critical: true),
s_caKeyUsage });
}
internal X509Certificate2 CreateEndEntity(string subject, RSA publicKey, X509ExtensionCollection extensions)
{
return CreateCertificate(
subject,
publicKey,
TimeSpan.FromSeconds(2),
extensions);
}
internal X509Certificate2 CreateOcspSigner(string subject, RSA publicKey)
{
return CreateCertificate(
subject,
publicKey,
TimeSpan.FromSeconds(1),
new X509ExtensionCollection() { s_eeConstraints, s_eeKeyUsage, s_ocspResponderEku},
ocspResponder: true);
}
internal void RebuildRootWithRevocation()
{
if (_cdpExtension == null && CdpUri != null)
{
_cdpExtension = CreateCdpExtension(CdpUri);
}
if (_aiaExtension == null && (OcspUri != null || AiaHttpUri != null))
{
_aiaExtension = CreateAiaExtension(AiaHttpUri, OcspUri);
}
RebuildRootWithRevocation(_cdpExtension, _aiaExtension);
}
private void RebuildRootWithRevocation(X509Extension cdpExtension, X509Extension aiaExtension)
{
X500DistinguishedName subjectName = _cert.SubjectName;
if (!subjectName.RawData.SequenceEqual(_cert.IssuerName.RawData))
{
throw new InvalidOperationException();
}
var req = new CertificateRequest(subjectName, _cert.PublicKey, HashAlgorithmName.SHA256);
foreach (X509Extension ext in _cert.Extensions)
{
req.CertificateExtensions.Add(ext);
}
req.CertificateExtensions.Add(cdpExtension);
req.CertificateExtensions.Add(aiaExtension);
byte[] serial = _cert.GetSerialNumber();
Array.Reverse(serial);
X509Certificate2 dispose = _cert;
using (dispose)
using (RSA rsa = _cert.GetRSAPrivateKey())
using (X509Certificate2 tmp = req.Create(
subjectName,
X509SignatureGenerator.CreateForRSA(rsa, RSASignaturePadding.Pkcs1),
new DateTimeOffset(_cert.NotBefore),
new DateTimeOffset(_cert.NotAfter),
serial))
{
_cert = tmp.CopyWithPrivateKey(rsa);
}
}
private X509Certificate2 CreateCertificate(
string subject,
RSA publicKey,
TimeSpan nestingBuffer,
X509ExtensionCollection extensions,
bool ocspResponder = false)
{
if (_cdpExtension == null && CdpUri != null)
{
_cdpExtension = CreateCdpExtension(CdpUri);
}
if (_aiaExtension == null && (OcspUri != null || AiaHttpUri != null))
{
_aiaExtension = CreateAiaExtension(AiaHttpUri, OcspUri);
}
if (_akidExtension == null)
{
_akidExtension = CreateAkidExtension();
}
CertificateRequest request = new CertificateRequest(
subject,
publicKey,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
foreach (X509Extension extension in extensions)
{
request.CertificateExtensions.Add(extension);
}
// Windows does not accept OCSP Responder certificates which have
// a CDP extension, or an AIA extension with an OCSP endpoint.
if (!ocspResponder)
{
request.CertificateExtensions.Add(_cdpExtension);
request.CertificateExtensions.Add(_aiaExtension);
}
request.CertificateExtensions.Add(_akidExtension);
request.CertificateExtensions.Add(
new X509SubjectKeyIdentifierExtension(request.PublicKey, false));
byte[] serial = new byte[sizeof(long)];
RandomNumberGenerator.Fill(serial);
return request.Create(
_cert,
_cert.NotBefore.Add(nestingBuffer),
_cert.NotAfter.Subtract(nestingBuffer),
serial);
}
internal byte[] GetCertData()
{
return (_certData ??= _cert.RawData);
}
internal byte[] GetCrl()
{
byte[] crl = _crl;
DateTimeOffset now = DateTimeOffset.UtcNow;
if (crl != null && now < _crlExpiry)
{
return crl;
}
DateTimeOffset newExpiry = now.AddSeconds(2);
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
using (writer.PushSequence())
{
writer.WriteObjectIdentifier("1.2.840.113549.1.1.11");
writer.WriteNull();
}
byte[] signatureAlgId = writer.Encode();
writer.Reset();
// TBSCertList
using (writer.PushSequence())
{
// version v2(1)
writer.WriteInteger(1);
// signature (AlgorithmIdentifier)
writer.WriteEncodedValue(signatureAlgId);
// issuer
if (CorruptRevocationIssuerName)
{
writer.WriteEncodedValue(s_nonParticipatingName.RawData);
}
else
{
writer.WriteEncodedValue(_cert.SubjectName.RawData);
}
if (RevocationExpiration.HasValue)
{
// thisUpdate
writer.WriteUtcTime(_cert.NotBefore);
// nextUpdate
writer.WriteUtcTime(RevocationExpiration.Value);
}
else
{
// thisUpdate
writer.WriteUtcTime(now);
// nextUpdate
writer.WriteUtcTime(newExpiry);
}
// revokedCertificates (don't write down if empty)
if (_revocationList?.Count > 0)
{
// SEQUENCE OF
using (writer.PushSequence())
{
foreach ((byte[] serial, DateTimeOffset when) in _revocationList)
{
// Anonymous CRL Entry type
using (writer.PushSequence())
{
writer.WriteInteger(serial);
writer.WriteUtcTime(when);
}
}
}
}
// extensions [0] EXPLICIT Extensions
using (writer.PushSequence(s_context0))
{
// Extensions (SEQUENCE OF)
using (writer.PushSequence())
{
if (_akidExtension == null)
{
_akidExtension = CreateAkidExtension();
}
// Authority Key Identifier Extension
using (writer.PushSequence())
{
writer.WriteObjectIdentifier(_akidExtension.Oid.Value);
if (_akidExtension.Critical)
{
writer.WriteBoolean(true);
}
writer.WriteOctetString(_akidExtension.RawData);
}
// CRL Number Extension
using (writer.PushSequence())
{
writer.WriteObjectIdentifier("2.5.29.20");
using (writer.PushOctetString())
{
writer.WriteInteger(_crlNumber);
}
}
}
}
}
byte[] tbsCertList = writer.Encode();
writer.Reset();
byte[] signature;
using (RSA key = _cert.GetRSAPrivateKey())
{
signature =
key.SignData(tbsCertList, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
if (CorruptRevocationSignature)
{
signature[5] ^= 0xFF;
}
}
// CertificateList
using (writer.PushSequence())
{
writer.WriteEncodedValue(tbsCertList);
writer.WriteEncodedValue(signatureAlgId);
writer.WriteBitString(signature);
}
_crl = writer.Encode();
_crlExpiry = newExpiry;
_crlNumber++;
return _crl;
}
internal void DesignateOcspResponder(X509Certificate2 responder)
{
_ocspResponder = responder;
}
internal byte[] BuildOcspResponse(
ReadOnlyMemory<byte> certId,
ReadOnlyMemory<byte> nonceExtension)
{
DateTimeOffset now = DateTimeOffset.UtcNow;
DateTimeOffset revokedTime = default;
CertStatus status = CheckRevocation(certId, ref revokedTime);
X509Certificate2 responder = (_ocspResponder ?? _cert);
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
/*
ResponseData ::= SEQUENCE {
version [0] EXPLICIT Version DEFAULT v1,
responderID ResponderID,
producedAt GeneralizedTime,
responses SEQUENCE OF SingleResponse,
responseExtensions [1] EXPLICIT Extensions OPTIONAL }
*/
using (writer.PushSequence())
{
// Skip version (v1)
/*
ResponderID ::= CHOICE {
byName [1] Name,
byKey [2] KeyHash }
*/
using (writer.PushSequence(s_context1))
{
if (CorruptRevocationIssuerName)
{
writer.WriteEncodedValue(s_nonParticipatingName.RawData);
}
else
{
writer.WriteEncodedValue(responder.SubjectName.RawData);
}
}
writer.WriteGeneralizedTime(now);
using (writer.PushSequence())
{
/*
SingleResponse ::= SEQUENCE {
certID CertID,
certStatus CertStatus,
thisUpdate GeneralizedTime,
nextUpdate [0] EXPLICIT GeneralizedTime OPTIONAL,
singleExtensions [1] EXPLICIT Extensions OPTIONAL }
*/
using (writer.PushSequence())
{
writer.WriteEncodedValue(certId.Span);
if (status == CertStatus.OK)
{
writer.WriteNull(s_context0);
}
else if (status == CertStatus.Revoked)
{
writer.PushSequence(s_context1);
writer.WriteGeneralizedTime(revokedTime);
writer.PopSequence(s_context1);
}
else
{
Assert.Equal(CertStatus.Unknown, status);
writer.WriteNull(s_context2);
}
if (RevocationExpiration.HasValue)
{
writer.WriteGeneralizedTime(
_cert.NotBefore,
omitFractionalSeconds: true);
using (writer.PushSequence(s_context0))
{
writer.WriteGeneralizedTime(
RevocationExpiration.Value,
omitFractionalSeconds: true);
}
}
else
{
writer.WriteGeneralizedTime(now, omitFractionalSeconds: true);
}
}
}
if (!nonceExtension.IsEmpty)
{
using (writer.PushSequence(s_context1))
using (writer.PushSequence())
{
writer.WriteEncodedValue(nonceExtension.Span);
}
}
}
byte[] tbsResponseData = writer.Encode();
writer.Reset();
/*
BasicOCSPResponse ::= SEQUENCE {
tbsResponseData ResponseData,
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING,
certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
*/
using (writer.PushSequence())
{
writer.WriteEncodedValue(tbsResponseData);
using (writer.PushSequence())
{
writer.WriteObjectIdentifier("1.2.840.113549.1.1.11");
writer.WriteNull();
}
using (RSA rsa = responder.GetRSAPrivateKey())
{
byte[] signature = rsa.SignData(
tbsResponseData,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
if (CorruptRevocationSignature)
{
signature[5] ^= 0xFF;
}
writer.WriteBitString(signature);
}
if (_ocspResponder != null)
{
using (writer.PushSequence(s_context0))
using (writer.PushSequence())
{
writer.WriteEncodedValue(_ocspResponder.RawData);
writer.PopSequence();
}
}
}
byte[] responseBytes = writer.Encode();
writer.Reset();
using (writer.PushSequence())
{
writer.WriteEnumeratedValue(OcspResponseStatus.Successful);
using (writer.PushSequence(s_context0))
using (writer.PushSequence())
{
writer.WriteObjectIdentifier("1.3.6.1.5.5.7.48.1.1");
writer.WriteOctetString(responseBytes);
}
}
return writer.Encode();
}
private CertStatus CheckRevocation(ReadOnlyMemory<byte> certId, ref DateTimeOffset revokedTime)
{
AsnReader reader = new AsnReader(certId, AsnEncodingRules.DER);
AsnReader idReader = reader.ReadSequence();
reader.ThrowIfNotEmpty();
AsnReader algIdReader = idReader.ReadSequence();
if (algIdReader.ReadObjectIdentifier() != "1.3.14.3.2.26")
{
return CertStatus.Unknown;
}
if (algIdReader.HasData)
{
algIdReader.ReadNull();
algIdReader.ThrowIfNotEmpty();
}
if (_dnHash == null)
{
using (HashAlgorithm hash = SHA1.Create())
{
_dnHash = hash.ComputeHash(_cert.SubjectName.RawData);
}
}
if (!idReader.TryReadPrimitiveOctetString(out ReadOnlyMemory<byte> reqDn))
{
idReader.ThrowIfNotEmpty();
}
if (!reqDn.Span.SequenceEqual(_dnHash))
{
return CertStatus.Unknown;
}
if (!idReader.TryReadPrimitiveOctetString(out ReadOnlyMemory<byte> reqKeyHash))
{
idReader.ThrowIfNotEmpty();
}
// We could check the key hash...
ReadOnlyMemory<byte> reqSerial = idReader.ReadIntegerBytes();
idReader.ThrowIfNotEmpty();
if (_revocationList == null)
{
return CertStatus.OK;
}
ReadOnlySpan<byte> reqSerialSpan = reqSerial.Span;
foreach ((byte[] serial, DateTimeOffset time) in _revocationList)
{
if (reqSerialSpan.SequenceEqual(serial))
{
revokedTime = time;
return CertStatus.Revoked;
}
}
return CertStatus.OK;
}
private static X509Extension CreateAiaExtension(string certLocation, string ocspStem)
{
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
// AuthorityInfoAccessSyntax (SEQUENCE OF)
using (writer.PushSequence())
{
if (!string.IsNullOrEmpty(ocspStem))
{
// AccessDescription for id-ad-ocsp
using (writer.PushSequence())
{
writer.WriteObjectIdentifier("1.3.6.1.5.5.7.48.1");
writer.WriteCharacterString(
UniversalTagNumber.IA5String,
ocspStem,
new Asn1Tag(TagClass.ContextSpecific, 6));
}
}
if (!string.IsNullOrEmpty(certLocation))
{
// AccessDescription for id-ad-caIssuers
using (writer.PushSequence())
{
writer.WriteObjectIdentifier("1.3.6.1.5.5.7.48.2");
writer.WriteCharacterString(
UniversalTagNumber.IA5String,
certLocation,
new Asn1Tag(TagClass.ContextSpecific, 6));
}
}
}
return new X509Extension("1.3.6.1.5.5.7.1.1", writer.Encode(), false);
}
private static X509Extension CreateCdpExtension(string cdp)
{
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
// SEQUENCE OF
using (writer.PushSequence())
{
// DistributionPoint
using (writer.PushSequence())
{
// Because DistributionPointName is a CHOICE type this tag is explicit.
// (ITU-T REC X.680-201508 C.3.2.2(g)(3rd bullet))
// distributionPoint [0] DistributionPointName
using (writer.PushSequence(s_context0))
{
// [0] DistributionPointName (GeneralNames (SEQUENCE OF))
using (writer.PushSequence(s_context0))
{
// GeneralName ([6] IA5String)
writer.WriteCharacterString(
UniversalTagNumber.IA5String,
cdp,
new Asn1Tag(TagClass.ContextSpecific, 6));
}
}
}
}
return new X509Extension("2.5.29.31", writer.Encode(), false);
}
private X509Extension CreateAkidExtension()
{
X509SubjectKeyIdentifierExtension skid =
_cert.Extensions.OfType<X509SubjectKeyIdentifierExtension>().SingleOrDefault();
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
// AuthorityKeyIdentifier
using (writer.PushSequence())
{
if (skid == null)
{
// authorityCertIssuer [1] GeneralNames (SEQUENCE OF)
using (writer.PushSequence(s_context1))
{
// directoryName [4] Name
byte[] dn = _cert.SubjectName.RawData;
if (s_context4.Encode(dn) != 1)
{
throw new InvalidOperationException();
}
writer.WriteEncodedValue(dn);
}
// authorityCertSerialNumber [2] CertificateSerialNumber (INTEGER)
byte[] serial = _cert.GetSerialNumber();
Array.Reverse(serial);
writer.WriteInteger(serial, s_context2);
}
else
{
// keyIdentifier [0] KeyIdentifier (OCTET STRING)
AsnReader reader = new AsnReader(skid.RawData, AsnEncodingRules.BER);
ReadOnlyMemory<byte> contents;
if (!reader.TryReadPrimitiveOctetString(out contents))
{
throw new InvalidOperationException();
}
reader.ThrowIfNotEmpty();
writer.WriteOctetString(contents.Span, s_context0);
}
}
return new X509Extension("2.5.29.35", writer.Encode(), false);
}
private enum OcspResponseStatus
{
Successful,
}
private enum CertStatus
{
Unknown,
OK,
Revoked,
}
internal static void BuildPrivatePki(
PkiOptions pkiOptions,
out RevocationResponder responder,
out CertificateAuthority rootAuthority,
out CertificateAuthority intermediateAuthority,
out X509Certificate2 endEntityCert,
string testName = null,
bool registerAuthorities = true,
bool pkiOptionsInSubject = false,
string subjectName = null,
int keySize = DefaultKeySize,
X509ExtensionCollection extensions = null)
{
bool rootDistributionViaHttp = !pkiOptions.HasFlag(PkiOptions.NoRootCertDistributionUri);
bool issuerRevocationViaCrl = pkiOptions.HasFlag(PkiOptions.IssuerRevocationViaCrl);
bool issuerRevocationViaOcsp = pkiOptions.HasFlag(PkiOptions.IssuerRevocationViaOcsp);
bool issuerDistributionViaHttp = !pkiOptions.HasFlag(PkiOptions.NoIssuerCertDistributionUri);
bool endEntityRevocationViaCrl = pkiOptions.HasFlag(PkiOptions.EndEntityRevocationViaCrl);
bool endEntityRevocationViaOcsp = pkiOptions.HasFlag(PkiOptions.EndEntityRevocationViaOcsp);
Assert.True(
issuerRevocationViaCrl || issuerRevocationViaOcsp ||
endEntityRevocationViaCrl || endEntityRevocationViaOcsp,
"At least one revocation mode is enabled");
if (extensions == null)
{
// default to client
extensions = new X509ExtensionCollection() { s_eeConstraints, s_eeKeyUsage, s_tlsClientEku };
}
using (RSA rootKey = RSA.Create(keySize))
using (RSA intermedKey = RSA.Create(keySize))
using (RSA eeKey = RSA.Create(keySize))
{
var rootReq = new CertificateRequest(
BuildSubject("A Revocation Test Root", testName, pkiOptions, pkiOptionsInSubject),
rootKey,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
X509BasicConstraintsExtension caConstraints =
new X509BasicConstraintsExtension(true, false, 0, true);
rootReq.CertificateExtensions.Add(caConstraints);
var rootSkid = new X509SubjectKeyIdentifierExtension(rootReq.PublicKey, false);
rootReq.CertificateExtensions.Add(
rootSkid);
DateTimeOffset start = DateTimeOffset.UtcNow;
DateTimeOffset end = start.AddMonths(3);
// Don't dispose this, it's being transferred to the CertificateAuthority
X509Certificate2 rootCert = rootReq.CreateSelfSigned(start.AddDays(-2), end.AddDays(2));
responder = RevocationResponder.CreateAndListen();
string certUrl = $"{responder.UriPrefix}cert/{rootSkid.SubjectKeyIdentifier}.cer";
string cdpUrl = $"{responder.UriPrefix}crl/{rootSkid.SubjectKeyIdentifier}.crl";
string ocspUrl = $"{responder.UriPrefix}ocsp/{rootSkid.SubjectKeyIdentifier}";
rootAuthority = new CertificateAuthority(
rootCert,
rootDistributionViaHttp ? certUrl : null,
issuerRevocationViaCrl ? cdpUrl : null,
issuerRevocationViaOcsp ? ocspUrl : null);
// Don't dispose this, it's being transferred to the CertificateAuthority
X509Certificate2 intermedCert;
{
X509Certificate2 intermedPub = rootAuthority.CreateSubordinateCA(
BuildSubject("A Revocation Test CA", testName, pkiOptions, pkiOptionsInSubject),
intermedKey);
intermedCert = intermedPub.CopyWithPrivateKey(intermedKey);
intermedPub.Dispose();
}
X509SubjectKeyIdentifierExtension intermedSkid =
intermedCert.Extensions.OfType<X509SubjectKeyIdentifierExtension>().Single();
certUrl = $"{responder.UriPrefix}cert/{intermedSkid.SubjectKeyIdentifier}.cer";
cdpUrl = $"{responder.UriPrefix}crl/{intermedSkid.SubjectKeyIdentifier}.crl";
ocspUrl = $"{responder.UriPrefix}ocsp/{intermedSkid.SubjectKeyIdentifier}";
intermediateAuthority = new CertificateAuthority(
intermedCert,
issuerDistributionViaHttp ? certUrl : null,
endEntityRevocationViaCrl ? cdpUrl : null,
endEntityRevocationViaOcsp ? ocspUrl : null);
endEntityCert = intermediateAuthority.CreateEndEntity(
BuildSubject(subjectName ?? "A Revocation Test Cert", testName, pkiOptions, pkiOptionsInSubject),
eeKey,
extensions);
endEntityCert = endEntityCert.CopyWithPrivateKey(eeKey);
}
if (registerAuthorities)
{
responder.AddCertificateAuthority(rootAuthority);
responder.AddCertificateAuthority(intermediateAuthority);
}
}
private static string BuildSubject(
string cn,
string testName,
PkiOptions pkiOptions,
bool includePkiOptions)
{
if (includePkiOptions)
{
return $"CN=\"{cn}\", O=\"{testName}\", OU=\"{pkiOptions}\"";
}
return $"CN=\"{cn}\", O=\"{testName}\"";
}
}
}