|
19 | 19 | from cryptography import x509 |
20 | 20 | from cryptography.hazmat.primitives import serialization |
21 | 21 | from cryptography.hazmat.primitives.asymmetric import ec |
| 22 | +from cryptography.x509 import ExtensionNotFound |
22 | 23 | from cryptography.x509 import oid as crypto_oid |
23 | 24 | from in_toto_attestation.v1 import statement |
24 | 25 | from OpenSSL import crypto as ssl_crypto |
@@ -162,22 +163,32 @@ def verify(self, bundle: bundle_pb.Bundle) -> None: |
162 | 163 | raise VerificationError( |
163 | 164 | f"signing certificate verification failed: {err}" |
164 | 165 | ) from err |
| 166 | + |
165 | 167 | usage = signing_cert_crypto.extensions.get_extension_for_class( |
166 | 168 | x509.KeyUsage |
167 | 169 | ) |
168 | 170 | if not usage.value.digital_signature: |
169 | | - raise VerificationError( |
170 | | - "the certificate is not valid for digital signature usage" |
171 | | - ) |
172 | | - ext_usage = signing_cert_crypto.extensions.get_extension_for_class( |
173 | | - x509.ExtendedKeyUsage |
174 | | - ) |
175 | | - if crypto_oid.ExtendedKeyUsageOID.CODE_SIGNING not in ext_usage.value: |
176 | | - raise VerificationError( |
177 | | - "the certificate is not valid for code signing usage" |
178 | | - ) |
| 171 | + code_signing = False |
| 172 | + try: |
| 173 | + ext_usage = ( |
| 174 | + signing_cert_crypto.extensions.get_extension_for_class( |
| 175 | + x509.ExtendedKeyUsage |
| 176 | + ) |
| 177 | + ) |
| 178 | + if ( |
| 179 | + crypto_oid.ExtendedKeyUsageOID.CODE_SIGNING |
| 180 | + in ext_usage.value |
| 181 | + ): |
| 182 | + code_signing = True |
| 183 | + except ExtensionNotFound: |
| 184 | + pass |
| 185 | + if not code_signing: |
| 186 | + raise VerificationError( |
| 187 | + "signing certificate neither allows digital signature" |
| 188 | + "nor code signing" |
| 189 | + ) |
179 | 190 |
|
180 | 191 | # Verify the contents with a key verifier |
181 | | - pub_key: ec.EllipticCurvePublicKey = signing_cert_crypto.public_key |
| 192 | + pub_key: ec.EllipticCurvePublicKey = signing_cert_crypto.public_key() |
182 | 193 | verifier = ECKeyVerifier(pub_key) |
183 | 194 | return verifier.verify(bundle) |
0 commit comments