|
21 | 21 | algorithms,
|
22 | 22 | )
|
23 | 23 | from cryptography.utils import _check_byteslike
|
| 24 | +from cryptography.x509 import Certificate |
| 25 | +from cryptography.x509.verification import ( |
| 26 | + Criticality, |
| 27 | + ExtensionPolicy, |
| 28 | + Policy, |
| 29 | +) |
24 | 30 |
|
25 | 31 | load_pem_pkcs7_certificates = rust_pkcs7.load_pem_pkcs7_certificates
|
26 | 32 |
|
@@ -53,6 +59,45 @@ class PKCS7Options(utils.Enum):
|
53 | 59 | NoCerts = "Don't embed signer certificate"
|
54 | 60 |
|
55 | 61 |
|
| 62 | +def pkcs7_x509_extension_policies() -> tuple[ExtensionPolicy, ExtensionPolicy]: |
| 63 | + """ |
| 64 | + Gets the default X.509 extension policy for S/MIME. Some specifications |
| 65 | + that differ from the standard ones: |
| 66 | + - Certificates used as end entities (i.e., the cert used to sign |
| 67 | + a PKCS#7/SMIME message) should not have ca=true in their basic |
| 68 | + constraints extension. |
| 69 | + - EKU_CLIENT_AUTH_OID is not required |
| 70 | + - EKU_EMAIL_PROTECTION_OID is required |
| 71 | + """ |
| 72 | + |
| 73 | + # CA policy |
| 74 | + def _validate_ca( |
| 75 | + policy: Policy, cert: Certificate, bc: x509.BasicConstraints |
| 76 | + ): |
| 77 | + assert not bc.ca |
| 78 | + |
| 79 | + ca_policy = ExtensionPolicy.permit_all().require_present( |
| 80 | + x509.BasicConstraints, |
| 81 | + Criticality.AGNOSTIC, |
| 82 | + _validate_ca, |
| 83 | + ) |
| 84 | + |
| 85 | + # EE policy |
| 86 | + def _validate_eku( |
| 87 | + policy: Policy, cert: Certificate, eku: x509.ExtendedKeyUsage |
| 88 | + ): |
| 89 | + # Checking for EKU_EMAIL_PROTECTION_OID |
| 90 | + assert x509.ExtendedKeyUsageOID.EMAIL_PROTECTION in eku # type: ignore[attr-defined] |
| 91 | + |
| 92 | + ee_policy = ExtensionPolicy.permit_all().require_present( |
| 93 | + x509.ExtendedKeyUsage, |
| 94 | + Criticality.AGNOSTIC, |
| 95 | + _validate_eku, |
| 96 | + ) |
| 97 | + |
| 98 | + return ca_policy, ee_policy |
| 99 | + |
| 100 | + |
56 | 101 | class PKCS7SignatureBuilder:
|
57 | 102 | def __init__(
|
58 | 103 | self,
|
|
0 commit comments