-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DynamicRevocationTests.cs
1622 lines (1361 loc) · 69.7 KB
/
DynamicRevocationTests.cs
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates.Tests.Common;
using Test.Cryptography;
using Xunit;
namespace System.Security.Cryptography.X509Certificates.Tests.RevocationTests
{
[OuterLoop("These tests run serially at about 1 second each, and the code shouldn't change that often.")]
[ConditionalClass(typeof(DynamicRevocationTests), nameof(SupportsDynamicRevocation))]
[SkipOnPlatform(TestPlatforms.Browser, "Browser doesn't support X.509 certificates")]
public static partial class DynamicRevocationTests
{
// The CI machines are doing an awful lot of things at once, be generous with the timeout;
internal static readonly TimeSpan s_urlRetrievalLimit = TimeSpan.FromSeconds(30);
private static readonly Oid s_tlsServerOid = new Oid("1.3.6.1.5.5.7.3.1", null);
private static bool SupportsEntireChainCheck => !PlatformDetection.IsAndroid;
private static readonly X509ChainStatusFlags ThisOsRevocationStatusUnknown =
PlatformDetection.IsOSX || PlatformDetection.IsiOS || PlatformDetection.IstvOS || PlatformDetection.IsMacCatalyst || PlatformDetection.IsAndroid ?
X509ChainStatusFlags.RevocationStatusUnknown :
X509ChainStatusFlags.RevocationStatusUnknown | X509ChainStatusFlags.OfflineRevocation;
// Android will stop checking after the first revocation error, so any revoked certificates
// after will have RevocationStatusUnknown instead of Revoked
private static readonly X509ChainStatusFlags ThisOsRevokedWithPreviousRevocationError =
PlatformDetection.IsAndroid ?
X509ChainStatusFlags.RevocationStatusUnknown :
X509ChainStatusFlags.Revoked;
// Android will stop checking after the first revocation error, so any non-revoked certificates
// after will have RevocationStatusUnknown instead of NoError
private static readonly X509ChainStatusFlags ThisOsNoErrorWithPreviousRevocationError =
PlatformDetection.IsAndroid ?
X509ChainStatusFlags.RevocationStatusUnknown :
X509ChainStatusFlags.NoError;
private delegate void RunSimpleTest(
CertificateAuthority root,
CertificateAuthority intermediate,
X509Certificate2 endEntity,
ChainHolder chainHolder,
RevocationResponder responder);
public static IEnumerable<object[]> AllViableRevocation
{
get
{
for (int designation = 0; designation < 4; designation++)
{
PkiOptions designationOptions = (PkiOptions)(designation << 16);
for (int iss = 1; iss < 4; iss++)
{
PkiOptions issuerRevocation = (PkiOptions)iss;
if (designationOptions.HasFlag(PkiOptions.RootAuthorityHasDesignatedOcspResponder) &&
!issuerRevocation.HasFlag(PkiOptions.IssuerRevocationViaOcsp))
{
continue;
}
for (int ee = 1; ee < 4; ee++)
{
PkiOptions endEntityRevocation = (PkiOptions)(ee << 2);
if (designationOptions.HasFlag(PkiOptions.IssuerAuthorityHasDesignatedOcspResponder) &&
!endEntityRevocation.HasFlag(PkiOptions.EndEntityRevocationViaOcsp))
{
continue;
}
// https://github.com/dotnet/runtime/issues/31249
// not all scenarios are working on macOS.
if (PlatformDetection.IsOSX || PlatformDetection.IsiOS || PlatformDetection.IstvOS || PlatformDetection.IsMacCatalyst)
{
if (!endEntityRevocation.HasFlag(PkiOptions.EndEntityRevocationViaOcsp))
{
continue;
}
}
yield return new object[] { designationOptions | issuerRevocation | endEntityRevocation };
}
}
}
}
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
public static void NothingRevoked(PkiOptions pkiOptions)
{
bool usingCrl = pkiOptions.HasFlag(PkiOptions.IssuerRevocationViaCrl) || pkiOptions.HasFlag(PkiOptions.EndEntityRevocationViaCrl);
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
if (PlatformDetection.IsAndroid && usingCrl)
{
// Android uses the verification time when determining if a CRL is relevant. If there
// are no relevant CRLs based on that time, the revocation status will be unknown.
// SimpleTest sets the verification time to the end entity's NotBefore + 1 minute,
// while the revocation responder uses the current time to set thisUpdate/nextUpdate.
// If using CRLs, set the verification time to the current time so that fetched CRLs
// will be considered relevant.
holder.Chain.ChainPolicy.VerificationTime = DateTime.UtcNow;
}
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: false,
issrRevoked: false,
leafRevoked: false);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeIntermediate(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
using (X509Certificate2 intermediateCert = intermediate.CloneIssuerCert())
{
X509Chain chain = holder.Chain;
DateTimeOffset now = DateTimeOffset.UtcNow;
root.Revoke(intermediateCert, now);
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
}
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: false,
issrRevoked: true,
leafRevoked: false);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
public static void RevokeEndEntity(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
intermediate.Revoke(endEntity, now);
holder.Chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: false,
issrRevoked: false,
leafRevoked: true);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
public static void RevokeLeafWithAiaFetchingDisabled(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
intermediate.Revoke(endEntity, now);
holder.Chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
holder.Chain.ChainPolicy.DisableCertificateDownloads = true;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: false,
issrRevoked: false,
leafRevoked: true);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeIntermediateAndEndEntity(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
using (X509Certificate2 intermediateCert = intermediate.CloneIssuerCert())
{
X509Chain chain = holder.Chain;
DateTimeOffset now = DateTimeOffset.UtcNow;
root.Revoke(intermediateCert, now);
intermediate.Revoke(endEntity, now);
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: false,
issrRevoked: true,
leafRevoked: true);
}
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeRoot(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
root.RebuildRootWithRevocation();
using (X509Certificate2 revocableRoot = root.CloneIssuerCert())
{
chain.ChainPolicy.CustomTrustStore.Clear();
chain.ChainPolicy.CustomTrustStore.Add(revocableRoot);
root.Revoke(revocableRoot, now);
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: true,
issrRevoked: false,
leafRevoked: false,
testWithRootRevocation: true);
// Make sure nothing weird happens during the root-only test.
CheckRevokedRootDirectly(holder, revocableRoot);
}
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeRootAndEndEntity(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
root.RebuildRootWithRevocation();
using (X509Certificate2 revocableRoot = root.CloneIssuerCert())
{
chain.ChainPolicy.CustomTrustStore.Clear();
chain.ChainPolicy.CustomTrustStore.Add(revocableRoot);
root.Revoke(revocableRoot, now);
intermediate.Revoke(endEntity, now);
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: true,
issrRevoked: false,
leafRevoked: true,
testWithRootRevocation: true);
}
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeRootAndIntermediate(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
root.RebuildRootWithRevocation();
using (X509Certificate2 revocableRoot = root.CloneIssuerCert())
using (X509Certificate2 intermediatePub = intermediate.CloneIssuerCert())
{
chain.ChainPolicy.CustomTrustStore.Clear();
chain.ChainPolicy.CustomTrustStore.Add(revocableRoot);
root.Revoke(revocableRoot, now);
root.Revoke(intermediatePub, now);
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: true,
issrRevoked: true,
leafRevoked: false,
testWithRootRevocation: true);
}
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeEverything(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
root.RebuildRootWithRevocation();
using (X509Certificate2 revocableRoot = root.CloneIssuerCert())
using (X509Certificate2 intermediatePub = intermediate.CloneIssuerCert())
{
chain.ChainPolicy.CustomTrustStore.Clear();
chain.ChainPolicy.CustomTrustStore.Add(revocableRoot);
root.Revoke(revocableRoot, now);
root.Revoke(intermediatePub, now);
intermediate.Revoke(endEntity, now);
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: true,
issrRevoked: true,
leafRevoked: true,
testWithRootRevocation: true);
}
});
}
[ConditionalTheory(typeof(SignatureSupport), nameof(SignatureSupport.SupportsX509Sha1Signatures))]
[InlineData(PkiOptions.OcspEverywhere)]
[InlineData(PkiOptions.AllIssuerRevocation | PkiOptions.EndEntityRevocationViaOcsp)]
[InlineData(PkiOptions.IssuerRevocationViaCrl | PkiOptions.EndEntityRevocationViaOcsp)]
public static void RevokeEndEntity_IssuerUnrelatedOcsp(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
using (RSA tmpRoot = RSA.Create())
using (RSA rsa = RSA.Create())
{
CertificateRequest rootReq = new CertificateRequest(
BuildSubject(
"Unauthorized Root",
nameof(RevokeEndEntity_IssuerUnrelatedOcsp),
pkiOptions,
true),
tmpRoot,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
rootReq.CertificateExtensions.Add(
new X509BasicConstraintsExtension(true, false, 0, true));
rootReq.CertificateExtensions.Add(
new X509SubjectKeyIdentifierExtension(rootReq.PublicKey, false));
rootReq.CertificateExtensions.Add(
new X509KeyUsageExtension(
X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign,
false));
using (CertificateAuthority unrelated = new CertificateAuthority(
rootReq.CreateSelfSigned(now.AddMinutes(-5), now.AddMonths(1)),
aiaHttpUrl: null,
cdpUrl: null,
ocspUrl: null))
{
X509Certificate2 designatedSigner = unrelated.CreateOcspSigner(
BuildSubject(
"Unrelated Designated OCSP Responder",
nameof(RevokeEndEntity_IssuerUnrelatedOcsp),
pkiOptions,
true),
rsa);
using (designatedSigner)
{
intermediate.DesignateOcspResponder(designatedSigner.CopyWithPrivateKey(rsa));
}
}
}
intermediate.Revoke(endEntity, now);
X509Chain chain = holder.Chain;
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
bool chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: X509ChainStatusFlags.NoError,
leafStatus: ThisOsRevocationStatusUnknown);
Assert.False(chainBuilt, "Chain built with ExcludeRoot.");
holder.DisposeChainElements();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EndCertificateOnly;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: X509ChainStatusFlags.NoError,
leafStatus: ThisOsRevocationStatusUnknown);
Assert.False(chainBuilt, "Chain built with EndCertificateOnly");
});
}
[ConditionalTheory(typeof(SignatureSupport), nameof(SignatureSupport.SupportsX509Sha1Signatures))]
[InlineData(PkiOptions.OcspEverywhere)]
[InlineData(PkiOptions.IssuerRevocationViaOcsp | PkiOptions.AllEndEntityRevocation)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeEndEntity_RootUnrelatedOcsp(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
using (RSA tmpRoot = RSA.Create())
using (RSA rsa = RSA.Create())
{
CertificateRequest rootReq = new CertificateRequest(
BuildSubject(
"Unauthorized Root",
nameof(RevokeEndEntity_IssuerUnrelatedOcsp),
pkiOptions,
true),
tmpRoot,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
rootReq.CertificateExtensions.Add(
new X509BasicConstraintsExtension(true, false, 0, true));
rootReq.CertificateExtensions.Add(
new X509SubjectKeyIdentifierExtension(rootReq.PublicKey, false));
rootReq.CertificateExtensions.Add(
new X509KeyUsageExtension(
X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign,
false));
using (CertificateAuthority unrelated = new CertificateAuthority(
rootReq.CreateSelfSigned(now.AddMinutes(-5), now.AddMonths(1)),
aiaHttpUrl: null,
cdpUrl: null,
ocspUrl: null))
{
X509Certificate2 designatedSigner = unrelated.CreateOcspSigner(
BuildSubject(
"Unrelated Designated OCSP Responder",
nameof(RevokeEndEntity_IssuerUnrelatedOcsp),
pkiOptions,
true),
rsa);
using (designatedSigner)
{
root.DesignateOcspResponder(designatedSigner.CopyWithPrivateKey(rsa));
}
}
}
using (X509Certificate2 issuerPub = intermediate.CloneIssuerCert())
{
root.Revoke(issuerPub, now);
}
X509Chain chain = holder.Chain;
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
bool chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: ThisOsRevocationStatusUnknown,
leafStatus: ThisOsNoErrorWithPreviousRevocationError);
Assert.False(chainBuilt, "Chain built with ExcludeRoot.");
holder.DisposeChainElements();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EndCertificateOnly;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: X509ChainStatusFlags.NoError,
leafStatus: X509ChainStatusFlags.NoError);
Assert.True(chainBuilt, "Chain built with EndCertificateOnly");
});
}
public static IEnumerable<object[]> PolicyErrorsNotTimeValidData
{
get
{
// Values are { policyErrors, notTimeValid }
yield return new object[] { true, false};
// Android always validates timestamp as part of building a path,
// so we don't include test cases with invalid time here
if (!PlatformDetection.IsAndroid)
{
yield return new object[] { false, true};
yield return new object[] { true, true};
}
}
}
[Theory]
[MemberData(nameof(PolicyErrorsNotTimeValidData))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeIntermediate_PolicyErrors_NotTimeValid(bool policyErrors, bool notTimeValid)
{
SimpleTest(
PkiOptions.OcspEverywhere,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
chain.ChainPolicy.UrlRetrievalTimeout = s_urlRetrievalLimit;
using (X509Certificate2 intermediateCert = intermediate.CloneIssuerCert())
{
root.Revoke(intermediateCert, now);
}
X509ChainStatusFlags leafProblems = X509ChainStatusFlags.NoError;
X509ChainStatusFlags issuerExtraProblems = X509ChainStatusFlags.NoError;
if (notTimeValid)
{
chain.ChainPolicy.VerificationTime = endEntity.NotAfter.AddSeconds(1);
leafProblems |= X509ChainStatusFlags.NotTimeValid;
}
else
{
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
}
if (policyErrors)
{
chain.ChainPolicy.ApplicationPolicy.Add(s_tlsServerOid);
leafProblems |= X509ChainStatusFlags.NotValidForUsage;
// [ActiveIssue("https://github.com/dotnet/runtime/issues/31246")]
// Linux reports this code at more levels than Windows does.
if (OperatingSystem.IsLinux())
{
issuerExtraProblems |= X509ChainStatusFlags.NotValidForUsage;
}
}
bool chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: issuerExtraProblems,
issrStatus: issuerExtraProblems | X509ChainStatusFlags.Revoked,
leafStatus: leafProblems | ThisOsRevocationStatusUnknown);
Assert.False(chainBuilt, "Chain built with ExcludeRoot.");
holder.DisposeChainElements();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EndCertificateOnly;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: issuerExtraProblems,
issrStatus: issuerExtraProblems,
leafStatus: leafProblems);
Assert.False(chainBuilt, "Chain built with EndCertificateOnly (no ignore flags)");
holder.DisposeChainElements();
chain.ChainPolicy.VerificationFlags |=
X509VerificationFlags.IgnoreNotTimeValid |
X509VerificationFlags.IgnoreWrongUsage;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: issuerExtraProblems,
issrStatus: issuerExtraProblems,
leafStatus: leafProblems);
Assert.True(chainBuilt, "Chain built with EndCertificateOnly (with ignore flags)");
},
pkiOptionsInTestName: false);
}
[Theory]
[MemberData(nameof(PolicyErrorsNotTimeValidData))]
public static void RevokeEndEntity_PolicyErrors_NotTimeValid(bool policyErrors, bool notTimeValid)
{
SimpleTest(
PkiOptions.OcspEverywhere,
(root, intermediate, endEntity, holder, responder) =>
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
chain.ChainPolicy.UrlRetrievalTimeout = s_urlRetrievalLimit;
intermediate.Revoke(endEntity, now);
X509ChainStatusFlags leafProblems = X509ChainStatusFlags.NoError;
X509ChainStatusFlags issuerExtraProblems = X509ChainStatusFlags.NoError;
if (notTimeValid)
{
chain.ChainPolicy.VerificationTime = endEntity.NotAfter.AddSeconds(1);
leafProblems |= X509ChainStatusFlags.NotTimeValid;
}
else
{
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
}
if (policyErrors)
{
chain.ChainPolicy.ApplicationPolicy.Add(s_tlsServerOid);
leafProblems |= X509ChainStatusFlags.NotValidForUsage;
// [ActiveIssue("https://github.com/dotnet/runtime/issues/31246")]
// Linux reports this code at more levels than Windows does.
if (!OperatingSystem.IsWindows())
{
issuerExtraProblems |= X509ChainStatusFlags.NotValidForUsage;
}
}
bool chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: issuerExtraProblems,
issrStatus: issuerExtraProblems,
leafStatus: leafProblems | X509ChainStatusFlags.Revoked);
Assert.False(chainBuilt, "Chain built with ExcludeRoot.");
holder.DisposeChainElements();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EndCertificateOnly;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: issuerExtraProblems,
issrStatus: issuerExtraProblems,
leafStatus: leafProblems | X509ChainStatusFlags.Revoked);
Assert.False(chainBuilt, "Chain built with EndCertificateOnly (no ignore flags)");
holder.DisposeChainElements();
chain.ChainPolicy.VerificationFlags |=
X509VerificationFlags.IgnoreNotTimeValid |
X509VerificationFlags.IgnoreWrongUsage;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: issuerExtraProblems,
issrStatus: issuerExtraProblems,
leafStatus: leafProblems | X509ChainStatusFlags.Revoked);
Assert.False(chainBuilt, "Chain built with EndCertificateOnly (with ignore flags)");
},
pkiOptionsInTestName: false);
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeEndEntity_RootRevocationOffline(PkiOptions pkiOptions)
{
BuildPrivatePki(
pkiOptions,
out RevocationResponder responder,
out CertificateAuthority root,
out CertificateAuthority intermediate,
out X509Certificate2 endEntity,
registerAuthorities: false,
pkiOptionsInSubject: true);
using (responder)
using (root)
using (intermediate)
using (endEntity)
using (ChainHolder holder = new ChainHolder())
using (X509Certificate2 rootCert = root.CloneIssuerCert())
using (X509Certificate2 intermediateCert = intermediate.CloneIssuerCert())
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
responder.AddCertificateAuthority(intermediate);
intermediate.Revoke(endEntity, now);
chain.ChainPolicy.ExtraStore.Add(intermediateCert);
chain.ChainPolicy.CustomTrustStore.Add(rootCert);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
chain.ChainPolicy.UrlRetrievalTimeout = s_urlRetrievalLimit;
bool chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: ThisOsRevocationStatusUnknown,
leafStatus: ThisOsRevokedWithPreviousRevocationError);
Assert.False(chainBuilt, "Chain built with ExcludeRoot.");
holder.DisposeChainElements();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EndCertificateOnly;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: X509ChainStatusFlags.NoError,
leafStatus: X509ChainStatusFlags.Revoked);
Assert.False(chainBuilt, "Chain built with EndCertificateOnly");
holder.DisposeChainElements();
if (SupportsEntireChainCheck)
{
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chainBuilt = chain.Build(endEntity);
// Potentially surprising result: Even in EntireChain mode,
// root revocation is NoError, not RevocationStatusUnknown.
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: ThisOsRevocationStatusUnknown,
leafStatus: X509ChainStatusFlags.Revoked);
Assert.False(chainBuilt, "Chain built with EntireChain");
}
}
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void NothingRevoked_RootRevocationOffline(PkiOptions pkiOptions)
{
BuildPrivatePki(
pkiOptions,
out RevocationResponder responder,
out CertificateAuthority root,
out CertificateAuthority intermediate,
out X509Certificate2 endEntity,
registerAuthorities: false,
pkiOptionsInSubject: true);
using (responder)
using (root)
using (intermediate)
using (endEntity)
using (ChainHolder holder = new ChainHolder())
using (X509Certificate2 rootCert = root.CloneIssuerCert())
using (X509Certificate2 intermediateCert = intermediate.CloneIssuerCert())
{
DateTimeOffset now = DateTimeOffset.UtcNow;
X509Chain chain = holder.Chain;
responder.AddCertificateAuthority(intermediate);
chain.ChainPolicy.ExtraStore.Add(intermediateCert);
chain.ChainPolicy.CustomTrustStore.Add(rootCert);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.VerificationTime = now.AddSeconds(1).UtcDateTime;
chain.ChainPolicy.UrlRetrievalTimeout = s_urlRetrievalLimit;
bool chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: ThisOsRevocationStatusUnknown,
leafStatus: ThisOsNoErrorWithPreviousRevocationError);
Assert.False(chainBuilt, "Chain built with ExcludeRoot.");
holder.DisposeChainElements();
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EndCertificateOnly;
chainBuilt = chain.Build(endEntity);
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: X509ChainStatusFlags.NoError,
leafStatus: X509ChainStatusFlags.NoError);
Assert.True(chainBuilt, "Chain built with EndCertificateOnly");
holder.DisposeChainElements();
if (SupportsEntireChainCheck)
{
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chainBuilt = chain.Build(endEntity);
// Potentially surprising result: Even in EntireChain mode,
// root revocation is NoError, not RevocationStatusUnknown.
AssertChainStatus(
chain,
rootStatus: X509ChainStatusFlags.NoError,
issrStatus: ThisOsRevocationStatusUnknown,
leafStatus: X509ChainStatusFlags.NoError);
Assert.False(chainBuilt, "Chain built with EntireChain");
}
}
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
public static void RevokeEndEntityWithInvalidRevocationSignature(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
intermediate.CorruptRevocationSignature = true;
RevokeEndEntityWithInvalidRevocation(holder, intermediate, endEntity);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeIntermediateWithInvalidRevocationSignature(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
root.CorruptRevocationSignature = true;
RevokeIntermediateWithInvalidRevocation(holder, root, intermediate, endEntity);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
public static void RevokeEndEntityWithInvalidRevocationName(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
intermediate.CorruptRevocationIssuerName = true;
RevokeEndEntityWithInvalidRevocation(holder, intermediate, endEntity);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeIntermediateWithInvalidRevocationName(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
root.CorruptRevocationIssuerName = true;
RevokeIntermediateWithInvalidRevocation(holder, root, intermediate, endEntity);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
public static void RevokeEndEntityWithExpiredRevocation(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTime revocationTime = endEntity.NotBefore;
if (PlatformDetection.IsAndroid)
{
// Android seems to use different times (+/- some buffer) to determine whether or not
// to use the revocation data it fetches.
// CRL : verification time
// OCSP : current time
// This test dynamically build the certs such that the current time falls within their
// period of validity (with more than a one second range), so we should be able to use
// the current time as revocation time and one second past that as verification time.
revocationTime = DateTime.UtcNow;
Assert.True(revocationTime >= endEntity.NotBefore && revocationTime < endEntity.NotAfter);
}
holder.Chain.ChainPolicy.VerificationTime = revocationTime.AddSeconds(1);
intermediate.RevocationExpiration = revocationTime;
intermediate.Revoke(endEntity, revocationTime);
SimpleRevocationBody(
holder,
endEntity,
rootRevoked: false,
issrRevoked: false,
leafRevoked: true);
});
}
[Theory]
[MemberData(nameof(AllViableRevocation))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31249", PlatformSupport.AppleCrypto)]
public static void RevokeIntermediateWithExpiredRevocation(PkiOptions pkiOptions)
{
SimpleTest(
pkiOptions,
(root, intermediate, endEntity, holder, responder) =>
{
DateTime revocationTime = endEntity.NotBefore;
if (PlatformDetection.IsAndroid)
{
// Android seems to use different times (+/- some buffer) to determine whether or not
// to use the revocation data it fetches.
// CRL : verification time
// OCSP : current time
// This test dynamically build the certs such that the current time falls within their
// period of validity (with more than a one second range), so we should be able to use
// the current time as revocation time and one second past that as verification time.
// This should allow the fetched data from both CRL and OCSP to be considered relevant.
revocationTime = DateTime.UtcNow;
Assert.True(revocationTime >= endEntity.NotBefore && revocationTime < endEntity.NotAfter);
}
holder.Chain.ChainPolicy.VerificationTime = revocationTime.AddSeconds(1);
using (X509Certificate2 intermediatePub = intermediate.CloneIssuerCert())
{
root.RevocationExpiration = revocationTime;
root.Revoke(intermediatePub, revocationTime);
}