@@ -391,6 +391,60 @@ async def test_verify_name_constraints_excludes(
391391 assert excinfo .value .next == leaf
392392
393393
394+ async def test_verify_basic_constraints_must_have_ca (
395+ aiohttp_session , mock_with_x5u , cache , now_fixed
396+ ):
397+ certs = [
398+ cryptography .x509 .load_pem_x509_certificate (pem , backend = default_backend ())
399+ for pem in STAGE_CERT_LIST
400+ ]
401+ real_intermediate = certs [1 ]
402+ intermediate = mock_cert (real_intermediate )
403+ basic_mock = mock .Mock ()
404+ basic_mock .ca = False
405+ mock_cert_extension (intermediate , cryptography .x509 .BasicConstraints , basic_mock )
406+ certs [1 ] = intermediate
407+
408+ with mock .patch ("cryptography.x509.load_pem_x509_certificate" ) as load_cert_mock :
409+ load_cert_mock .side_effect = lambda * args , ** kwargs : certs .pop (0 )
410+ s = SignatureVerifier (aiohttp_session , cache , STAGE_ROOT_HASH )
411+ with pytest .raises (autograph_utils .CertificateCannotSign ) as excinfo :
412+ await s .verify_x5u (FAKE_CERT_URL )
413+
414+ assert excinfo .value .detail .startswith (
415+ "Certificate cannot be used for signing because "
416+ )
417+ assert excinfo .value .cert == intermediate
418+ assert excinfo .value .extra == "ca is false"
419+
420+
421+ async def test_verify_basic_constraints_must_have_cert_signing (
422+ aiohttp_session , mock_with_x5u , cache , now_fixed
423+ ):
424+ certs = [
425+ cryptography .x509 .load_pem_x509_certificate (pem , backend = default_backend ())
426+ for pem in STAGE_CERT_LIST
427+ ]
428+ real_intermediate = certs [1 ]
429+ intermediate = mock_cert (real_intermediate )
430+ uses_mock = mock .Mock ()
431+ uses_mock .key_cert_sign = False
432+ mock_cert_extension (intermediate , cryptography .x509 .KeyUsage , uses_mock )
433+ certs [1 ] = intermediate
434+
435+ with mock .patch ("cryptography.x509.load_pem_x509_certificate" ) as load_cert_mock :
436+ load_cert_mock .side_effect = lambda * args , ** kwargs : certs .pop (0 )
437+ s = SignatureVerifier (aiohttp_session , cache , STAGE_ROOT_HASH )
438+ with pytest .raises (autograph_utils .CertificateCannotSign ) as excinfo :
439+ await s .verify_x5u (FAKE_CERT_URL )
440+
441+ assert excinfo .value .detail .startswith (
442+ "Certificate cannot be used for signing because "
443+ )
444+ assert excinfo .value .cert == intermediate
445+ assert excinfo .value .extra == "key usage is incomplete"
446+
447+
394448async def test_verify_leaf_code_signing (
395449 aiohttp_session , mock_with_x5u , cache , now_fixed
396450):
0 commit comments