12
12
13
13
from cryptography import utils , x509
14
14
from cryptography .hazmat ._oid import ExtendedKeyUsageOID
15
- from cryptography .x509 .extensions import (
16
- ExtendedKeyUsage ,
17
- ExtensionType ,
18
- )
15
+ from cryptography .x509 import ExtensionType
19
16
from cryptography .x509 .general_name import DNSName , IPAddress
20
17
from cryptography .x509 .verification import (
21
18
Criticality ,
@@ -303,7 +300,7 @@ def test_error_message(self):
303
300
verifier .verify (leaf , [])
304
301
305
302
306
- TESTED_EXTENSION_TYPES = (
303
+ SUPPORTED_EXTENSION_TYPES = (
307
304
x509 .AuthorityInformationAccess ,
308
305
x509 .AuthorityKeyIdentifier ,
309
306
x509 .SubjectKeyIdentifier ,
@@ -390,38 +387,99 @@ def validator_cb(policy, cert, ext: Optional[ExtensionType]):
390
387
391
388
return validator_cb
392
389
393
- # def test_all_extension_types(self):
394
- # ca_ext_policy = ExtensionPolicy.webpki_defaults_ca()
395
- # ee_ext_policy = ExtensionPolicy.webpki_defaults_ee()
390
+ def test_require_not_present (self ):
391
+ default_ee = ExtensionPolicy .webpki_defaults_ee ()
392
+ no_basic_constraints_ee = default_ee .require_not_present (
393
+ x509 .BasicConstraints
394
+ )
396
395
397
- # ca_validator_called = False
396
+ default_builder = (
397
+ PolicyBuilder ().store (self .store ).time (self .validation_time )
398
+ )
399
+ builder_no_basic_constraints = default_builder .extension_policies (
400
+ ExtensionPolicy .webpki_defaults_ca (), no_basic_constraints_ee
401
+ )
398
402
399
- # extension_types = [
400
- # AuthorityInformationAccess,
401
- # AuthorityKeyIdentifier,
402
- # SubjectKeyIdentifier,
403
- # KeyUsage,
404
- # SubjectAlternativeName,
405
- # BasicConstraints,
406
- # ]
403
+ default_builder .build_client_verifier ().verify (self .leaf , [])
407
404
408
- # for
405
+ with pytest .raises (
406
+ VerificationError ,
407
+ match = "Certificate contains prohibited extension" ,
408
+ ):
409
+ builder_no_basic_constraints .build_client_verifier ().verify (
410
+ self .leaf , []
411
+ )
409
412
410
- # ca_ext_policy = ca_ext_policy.may_be_present(
411
- # x509.BasicConstraints,
412
- # Criticality.AGNOSTIC,
413
- # ca_basic_constraints_validator,
414
- # )
413
+ def test_require_present (self ):
414
+ default_builder = (
415
+ PolicyBuilder ().store (self .store ).time (self .validation_time )
416
+ )
417
+ builder_require_subject_keyid = default_builder .extension_policies (
418
+ ExtensionPolicy .webpki_defaults_ca (),
419
+ ExtensionPolicy .webpki_defaults_ee ().require_present (
420
+ x509 .SubjectKeyIdentifier ,
421
+ Criticality .AGNOSTIC ,
422
+ self ._make_validator_cb (x509 .SubjectKeyIdentifier ),
423
+ ),
424
+ )
425
+ builder_require_san = default_builder .extension_policies (
426
+ ExtensionPolicy .webpki_defaults_ca (),
427
+ ExtensionPolicy .webpki_defaults_ee ().require_present (
428
+ x509 .SubjectAlternativeName ,
429
+ Criticality .AGNOSTIC ,
430
+ self ._make_validator_cb (x509 .SubjectAlternativeName ),
431
+ ),
432
+ )
415
433
416
- # builder = PolicyBuilder().store(self.store)
417
- # builder = builder.time(self.validation_time)
418
- # builder = builder.extension_policies(ca_ext_policy, ee_ext_policy)
434
+ default_builder .build_client_verifier ().verify (self .leaf , [])
435
+ builder_require_san .build_client_verifier ().verify (self .leaf , [])
419
436
420
- # builder.build_client_verifier().verify(self.leaf, [])
437
+ with pytest .raises (
438
+ VerificationError ,
439
+ match = "missing required extension" ,
440
+ ):
441
+ builder_require_subject_keyid .build_client_verifier ().verify (
442
+ self .leaf , []
443
+ )
444
+
445
+ def test_criticality_constraints (self ):
446
+ builder = PolicyBuilder ().store (self .store ).time (self .validation_time )
447
+ noncrit_key_usage_builder = builder .extension_policies (
448
+ ExtensionPolicy .webpki_defaults_ca (),
449
+ ExtensionPolicy .webpki_defaults_ee ().require_present (
450
+ x509 .KeyUsage , Criticality .NON_CRITICAL , None
451
+ ),
452
+ )
453
+ critical_eku_builder = builder .extension_policies (
454
+ ExtensionPolicy .webpki_defaults_ca (),
455
+ ExtensionPolicy .webpki_defaults_ee ().require_present (
456
+ x509 .ExtendedKeyUsage , Criticality .CRITICAL , None
457
+ ),
458
+ )
459
+
460
+ def make_pattern (extension_type : Type [ExtensionType ]):
461
+ return (
462
+ f"invalid extension: { extension_type .oid .dotted_string } :"
463
+ " Certificate extension has incorrect criticality"
464
+ )
465
+
466
+ builder .build_client_verifier ().verify (self .leaf , [])
467
+ with pytest .raises (
468
+ VerificationError ,
469
+ match = make_pattern (x509 .KeyUsage ),
470
+ ):
471
+ noncrit_key_usage_builder .build_client_verifier ().verify (
472
+ self .leaf , []
473
+ )
474
+ with pytest .raises (
475
+ VerificationError ,
476
+ match = make_pattern (x509 .ExtendedKeyUsage ),
477
+ ):
478
+ critical_eku_builder .build_client_verifier ().verify (self .leaf , [])
421
479
422
480
@pytest .mark .parametrize (
423
481
"extension_type" ,
424
- TESTED_EXTENSION_TYPES ,
482
+ SUPPORTED_EXTENSION_TYPES ,
425
483
)
426
484
def test_custom_cb_pass (self , extension_type : Type [x509 .ExtensionType ]):
427
485
ca_ext_policy = ExtensionPolicy .webpki_defaults_ca ()
@@ -446,7 +504,7 @@ def test_custom_cb_pass(self, extension_type: Type[x509.ExtensionType]):
446
504
447
505
@pytest .mark .parametrize (
448
506
"extension_type" ,
449
- TESTED_EXTENSION_TYPES ,
507
+ SUPPORTED_EXTENSION_TYPES ,
450
508
)
451
509
def test_custom_cb_exception_fails_verification (self , extension_type ):
452
510
ca_ext_policy = ExtensionPolicy .webpki_defaults_ca ()
@@ -482,7 +540,7 @@ def validator(*_):
482
540
return False
483
541
484
542
ee_ext_policy = ee_ext_policy .may_be_present (
485
- ExtendedKeyUsage ,
543
+ x509 . ExtendedKeyUsage ,
486
544
Criticality .AGNOSTIC ,
487
545
validator ,
488
546
)
0 commit comments