1# SPDX-License-Identifier: GPL-2.0-only
2# This file is part of Scapy
3# See https://scapy.net/ for more information
4# Copyright (C) 2008 Arnaud Ebalard <arnaud.ebalard@eads.net>
5# <arno@natisbad.org>
6# 2015, 2016, 2017 Maxence Tury <maxence.tury@ssi.gouv.fr>
7# 2022-2025 Gabriel Potter
8
9r"""
10High-level methods for PKI objects (X.509 certificates, CRLs, CSR, Keys, CMS).
11Supported keys include RSA, ECDSA and EdDSA.
12
13The classes below are wrappers for the ASN.1 objects defined in x509.py.
14
15Example 1: Certificate & Private key
16____________________________________
17
18For instance, here is what you could do in order to modify the subject public
19key info of a 'cert' and then resign it with whatever 'key'::
20
21 >>> from scapy.layers.tls.cert import *
22 >>> cert = Cert("cert.der")
23 >>> k = PrivKeyRSA() # generate a private key
24 >>> cert.setSubjectPublicKeyFromPrivateKey(k)
25 >>> cert.resignWith(k)
26 >>> cert.export("newcert.pem")
27 >>> k.export("mykey.pem")
28
29One could also edit arguments like the serial number, as such::
30
31 >>> from scapy.layers.tls.cert import *
32 >>> c = Cert("mycert.pem")
33 >>> c.tbsCertificate.serialNumber = 0x4B1D
34 >>> k = PrivKey("mykey.pem") # import an existing private key
35 >>> c.resignWith(k)
36 >>> c.export("newcert.pem")
37
38To export the public key of a private key::
39
40 >>> k = PrivKey("mykey.pem")
41 >>> k.pubkey.export("mypubkey.pem")
42
43Example 2: CertList and CertTree
44________________________________
45
46Load a .pem file that contains multiple certificates::
47
48 >>> l = CertList("ca_chain.pem")
49 >>> l.show()
50 0000 [X.509 Cert Subject:/C=FR/OU=Scapy Test PKI/CN=Scapy Test CA...]
51 0001 [X.509 Cert Subject:/C=FR/OU=Scapy Test PKI/CN=Scapy Test Client...]
52
53Use 'CertTree' to organize the certificates in a tree::
54
55 >>> tree = CertTree("ca_chain.pem") # or tree = CertTree(l)
56 >>> tree.show()
57 /C=Ulaanbaatar/OU=Scapy Test PKI/CN=Scapy Test CA [Self Signed]
58 /C=FR/OU=Scapy Test PKI/CN=Scapy Test Client [Not Self Signed]
59
60Example 3: Certificate Signing Request (CSR)
61____________________________________________
62
63Scapy's :py:class:`~scapy.layers.tls.cert.CSR` class supports both PKCS#10 and CMC
64formats.
65
66Load and display a CSR::
67
68 >>> csr = CSR("cert.req")
69 >>> csr
70 [CSR Format: CMC, Subject:/O=TestOrg/CN=TestCN, Verified: True]
71 >>> csr.certReq.show()
72 ###[ PKCS10_CertificationRequest ]###
73 \certificationRequestInfo\
74 |###[ PKCS10_CertificationRequestInfo ]###
75 | version = 0x0 <ASN1_INTEGER[0]
76 | \subject \
77 | |###[ X509_RDN ]###
78 | | \rdn \
79 | | |###[ X509_AttributeTypeAndValue ]###
80 | | | type = <ASN1_OID['organizationName']>
81 | | | value = <ASN1_UTF8_STRING[b'TestOrg']>
82 [...]
83
84Get its public key and verify its signature::
85
86 >>> csr.pubkey
87 <scapy.layers.tls.cert.PubKeyRSA at 0x7f3481149310>
88 >>> csr.verifySelf()
89 True
90
91No need for obnoxious openssl tweaking anymore. :)
92"""
93
94import base64
95import calendar
96import enum
97import os
98import socket
99import ssl
100import time
101import warnings
102
103from scapy.config import conf, crypto_validator
104from scapy.compat import Self, plain_str
105from scapy.error import log_runtime, warning
106from scapy.utils import binrepr
107from scapy.pton_ntop import inet_ntop
108from scapy.asn1.asn1 import (
109 ASN1_BIT_STRING,
110 ASN1_NULL,
111 ASN1_OID,
112 ASN1_STRING,
113)
114from scapy.asn1.mib import hash_by_oid
115from scapy.layers.tls.crypto.hash import _tls_hash_algs
116from scapy.packet import Packet
117from scapy.layers.x509 import (
118 CMS_CertificateChoices,
119 CMS_ContentInfo,
120 CMS_EncapsulatedContentInfo,
121 CMS_IssuerAndSerialNumber,
122 CMS_RevocationInfoChoice,
123 CMS_SignedAttrsForSignature,
124 CMS_SignedData,
125 CMS_SignerInfo,
126 CMS_SubjectKeyIdentifier,
127 ECDSAPrivateKey,
128 ECDSAPrivateKey_OpenSSL,
129 ECDSAPublicKey,
130 EdDSAPrivateKey,
131 EdDSAPublicKey,
132 MLDSAPrivateKey,
133 MLDSAPublicKey,
134 PKCS10_CertificationRequest,
135 RSAPrivateKey,
136 RSAPrivateKey_OpenSSL,
137 RSAPublicKey,
138 X509_AlgorithmIdentifier,
139 X509_Attribute,
140 X509_AttributeValue,
141 X509_CRL,
142 X509_Cert,
143 X509_DNSName,
144 X509_IPAddress,
145 X509_OneAsymmetricKey,
146 X509_SubjectPublicKeyInfo,
147)
148from scapy.layers.tls.crypto.hash import _get_hash
149from scapy.layers.tls.crypto.pkcs1 import (
150 _DecryptAndSignRSA,
151 _EncryptAndVerifyRSA,
152 pkcs_os2ip,
153)
154from scapy.compat import bytes_encode
155
156# Typing imports
157from typing import (
158 List,
159 Optional,
160 Union,
161)
162
163if conf.crypto_valid:
164 from cryptography.exceptions import InvalidSignature
165 from cryptography.hazmat.backends import default_backend
166 from cryptography.hazmat.primitives import hashes
167 from cryptography.hazmat.primitives import serialization
168 from cryptography.hazmat.primitives.asymmetric import rsa, ec, x25519, x448
169
170 try:
171 # cryptography >= 47.0
172 from cryptography.hazmat.primitives.asymmetric import mldsa
173 except ImportError:
174 pass
175
176 # cryptography raised the minimum RSA key length to 1024 in 43.0+
177 # https://github.com/pyca/cryptography/pull/10278
178 # but we need still 512 for EXPORT40 ciphers (yes EXPORT is terrible)
179 # https://datatracker.ietf.org/doc/html/rfc2246#autoid-66
180 # The following detects the change and hacks around it using the backend
181
182 try:
183 rsa.generate_private_key(public_exponent=65537, key_size=512)
184 _RSA_512_SUPPORTED = True
185 except ValueError:
186 # cryptography > 43.0
187 _RSA_512_SUPPORTED = False
188 from cryptography.hazmat.primitives.asymmetric.rsa import rust_openssl
189
190
191# Maximum allowed size in bytes for a certificate file, to avoid
192# loading huge file when importing a cert
193_MAX_KEY_SIZE = 50 * 1024
194_MAX_CERT_SIZE = 50 * 1024
195_MAX_CRL_SIZE = 10 * 1024 * 1024 # some are that big
196_MAX_CSR_SIZE = 50 * 1024
197
198
199#####################################################################
200# Some helpers
201#####################################################################
202
203
204@conf.commands.register
205def der2pem(der_string, obj="UNKNOWN"):
206 """Convert DER octet string to PEM format (with optional header)"""
207 # Encode a byte string in PEM format. Header advertises <obj> type.
208 pem_string = "-----BEGIN %s-----\n" % obj
209 base64_string = base64.b64encode(der_string).decode()
210 chunks = [base64_string[i : i + 64] for i in range(0, len(base64_string), 64)]
211 pem_string += "\n".join(chunks)
212 pem_string += "\n-----END %s-----\n" % obj
213 return pem_string
214
215
216@conf.commands.register
217def pem2der(pem_string):
218 """Convert PEM string to DER format"""
219 # Encode all lines between the first '-----\n' and the 2nd-to-last '-----'.
220 pem_string = pem_string.replace(b"\r", b"")
221 first_idx = pem_string.find(b"-----\n") + 6
222 if pem_string.find(b"-----BEGIN", first_idx) != -1:
223 raise Exception("pem2der() expects only one PEM-encoded object")
224 last_idx = pem_string.rfind(b"-----", 0, pem_string.rfind(b"-----"))
225 base64_string = pem_string[first_idx:last_idx]
226 base64_string.replace(b"\n", b"")
227 der_string = base64.b64decode(base64_string)
228 return der_string
229
230
231def split_pem(s):
232 """
233 Split PEM objects. Useful to process concatenated certificates.
234 """
235 pem_strings = []
236 while s != b"":
237 start_idx = s.find(b"-----BEGIN")
238 if start_idx == -1:
239 break
240 end_idx = s.find(b"-----END")
241 if end_idx == -1:
242 raise Exception("Invalid PEM object (missing END tag)")
243 end_idx = s.find(b"\n", end_idx) + 1
244 if end_idx == 0:
245 # There is no final \n
246 end_idx = len(s)
247 pem_strings.append(s[start_idx:end_idx])
248 s = s[end_idx:]
249 return pem_strings
250
251
252class _PKIObj(object):
253 def __init__(self, frmt, der):
254 self.frmt = frmt
255 self._der = der
256
257
258class _PKIObjMaker(type):
259 def __call__(cls, obj_path, obj_max_size, pem_marker=None):
260 # This enables transparent DER and PEM-encoded data imports.
261 # Note that when importing a PEM file with multiple objects (like ECDSA
262 # private keys output by openssl), it will concatenate every object in
263 # order to create a 'der' attribute. When converting a 'multi' DER file
264 # into a PEM file, though, the PEM attribute will not be valid,
265 # because we do not try to identify the class of each object.
266 error_msg = "Unable to import data"
267
268 if obj_path is None:
269 raise Exception(error_msg)
270 # Make sure that only a str object can store a path to read a certificate
271 is_file = isinstance(obj_path, str) and os.path.isfile(obj_path)
272 obj_path = bytes_encode(obj_path)
273
274 if is_file:
275 _size = os.path.getsize(obj_path)
276 if _size > obj_max_size:
277 raise Exception(error_msg)
278 try:
279 with open(obj_path, "rb") as f:
280 _raw = f.read()
281 except Exception:
282 raise Exception(error_msg)
283 else:
284 _raw = obj_path
285
286 try:
287 if b"-----BEGIN" in _raw:
288 frmt = "PEM"
289 pem = _raw
290 der_list = split_pem(pem)
291 der = b"".join(map(pem2der, der_list))
292 else:
293 frmt = "DER"
294 der = _raw
295 except Exception:
296 raise Exception(error_msg)
297
298 p = _PKIObj(frmt, der)
299 return p
300
301
302#####################################################################
303# PKI objects wrappers
304#####################################################################
305
306###############
307# Public Keys #
308###############
309
310
311class _PubKeyFactory(_PKIObjMaker):
312 """
313 Metaclass for PubKey creation.
314 It casts the appropriate class on the fly, then fills in
315 the appropriate attributes with import_from_asn1pkt() submethod.
316 """
317
318 def __call__(cls, key_path=None, cryptography_obj=None):
319 # This allows to import cryptography objects directly
320 if cryptography_obj is not None:
321 obj = type.__call__(cls)
322 obj.__class__ = cls
323 obj.frmt = "original"
324 obj.marker = "PUBLIC KEY"
325 obj.pubkey = cryptography_obj
326 return obj
327
328 if key_path is None:
329 obj = type.__call__(cls)
330 if cls is PubKey:
331 cls = PubKeyRSA
332 obj.__class__ = cls
333 obj.frmt = "original"
334 obj.fill_and_store()
335 return obj
336
337 # This deals with the rare RSA 'kx export' call.
338 if isinstance(key_path, tuple):
339 obj = type.__call__(cls)
340 obj.__class__ = PubKeyRSA
341 obj.frmt = "tuple"
342 obj.import_from_tuple(key_path)
343 return obj
344
345 # Now for the usual calls, key_path may be the path to either:
346 # _an X509_SubjectPublicKeyInfo, as processed by openssl;
347 # _an RSAPublicKey;
348 # _an ECDSAPublicKey;
349 # _an EdDSAPublicKey;
350 # _an MLDSAPublicKey.
351 obj = _PKIObjMaker.__call__(cls, key_path, _MAX_KEY_SIZE)
352 try:
353 spki = X509_SubjectPublicKeyInfo(obj._der)
354 pubkey = spki.subjectPublicKey
355 if isinstance(pubkey, RSAPublicKey):
356 obj.__class__ = PubKeyRSA
357 obj.import_from_asn1pkt(pubkey)
358 elif isinstance(pubkey, ECDSAPublicKey):
359 obj.__class__ = PubKeyECDSA
360 obj.import_from_der(obj._der)
361 elif isinstance(pubkey, EdDSAPublicKey):
362 obj.__class__ = PubKeyEdDSA
363 obj.import_from_der(obj._der)
364 elif isinstance(pubkey, MLDSAPublicKey):
365 obj.__class__ = PubKeyMLDSA
366 obj.import_from_der(obj._der)
367 else:
368 raise
369 obj.marker = "PUBLIC KEY"
370 except Exception:
371 try:
372 pubkey = RSAPublicKey(obj._der)
373 obj.__class__ = PubKeyRSA
374 obj.import_from_asn1pkt(pubkey)
375 obj.marker = "RSA PUBLIC KEY"
376 except Exception:
377 # We cannot import an ECDSA public key without curve knowledge
378 if conf.debug_dissector:
379 raise
380 raise Exception("Unable to import public key")
381 return obj
382
383
384class PubKey(metaclass=_PubKeyFactory):
385 """
386 Parent class for PubKeyRSA, PubKeyECDSA, PubKeyEdDSA and PubKeyMLDSA.
387 Provides common verifyCert() and export() methods.
388 """
389
390 def verifyCert(self, cert):
391 """Verifies either a Cert or an X509_Cert."""
392 h = _get_cert_sig_hashname(cert)
393 tbsCert = cert.tbsCertificate
394 sigVal = bytes(cert.signatureValue)
395 return self.verify(bytes(tbsCert), sigVal, h=h, t="pkcs")
396
397 def verifyCsr(self, csr):
398 """Verifies a CSR."""
399 h = _get_csr_sig_hashname(csr)
400 certReqInfo = csr.certReq.certificationRequestInfo
401 sigVal = bytes(csr.certReq.signature)
402 return self.verify(bytes(certReqInfo), sigVal, h=h, t="pkcs")
403
404 @property
405 def pem(self):
406 return der2pem(self.der, self.marker)
407
408 @property
409 def der(self):
410 return self.pubkey.public_bytes(
411 encoding=serialization.Encoding.DER,
412 format=serialization.PublicFormat.SubjectPublicKeyInfo,
413 )
414
415 def public_numbers(self, *args, **kwargs):
416 return self.pubkey.public_numbers(*args, **kwargs)
417
418 @property
419 def key_size(self):
420 return self.pubkey.key_size
421
422 def export(self, filename, fmt=None):
423 """
424 Export public key in 'fmt' format (DER or PEM) to file 'filename'
425 """
426 if fmt is None:
427 if filename.endswith(".pem"):
428 fmt = "PEM"
429 else:
430 fmt = "DER"
431 with open(filename, "wb") as f:
432 if fmt == "DER":
433 return f.write(self.der)
434 elif fmt == "PEM":
435 return f.write(self.pem.encode())
436
437 @crypto_validator
438 def verify(self, msg, sig, h="sha256", **kwargs):
439 """
440 Verify signed data.
441 """
442 raise NotImplementedError
443
444
445class PubKeyRSA(PubKey, _EncryptAndVerifyRSA):
446 """
447 Wrapper for RSA keys based on _EncryptAndVerifyRSA from crypto/pkcs1.py
448 Use the 'key' attribute to access original object.
449 """
450
451 @crypto_validator
452 def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None):
453 pubExp = pubExp or 65537
454 if not modulus:
455 real_modulusLen = modulusLen or 2048
456 if real_modulusLen < 1024 and not _RSA_512_SUPPORTED:
457 # cryptography > 43.0 compatibility
458 private_key = rust_openssl.rsa.generate_private_key(
459 public_exponent=pubExp,
460 key_size=real_modulusLen,
461 )
462 else:
463 private_key = rsa.generate_private_key(
464 public_exponent=pubExp,
465 key_size=real_modulusLen,
466 backend=default_backend(),
467 )
468 self.pubkey = private_key.public_key()
469 else:
470 real_modulusLen = len(binrepr(modulus))
471 if modulusLen and real_modulusLen != modulusLen:
472 warning("modulus and modulusLen do not match!")
473 pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
474 self.pubkey = pubNum.public_key(default_backend())
475
476 self.marker = "PUBLIC KEY"
477
478 # Lines below are only useful for the legacy part of pkcs1.py
479 pubNum = self.pubkey.public_numbers()
480 self._modulusLen = real_modulusLen
481 self._modulus = pubNum.n
482 self._pubExp = pubNum.e
483
484 @crypto_validator
485 def import_from_tuple(self, tup):
486 # this is rarely used
487 e, m, mLen = tup
488 if isinstance(m, bytes):
489 m = pkcs_os2ip(m)
490 if isinstance(e, bytes):
491 e = pkcs_os2ip(e)
492 self.fill_and_store(modulus=m, pubExp=e)
493
494 def import_from_asn1pkt(self, pubkey):
495 modulus = pubkey.modulus.val
496 pubExp = pubkey.publicExponent.val
497 self.fill_and_store(modulus=modulus, pubExp=pubExp)
498
499 def encrypt(self, msg, t="pkcs", h="sha256", mgf=None, L=None):
500 # no ECDSA encryption support, hence no ECDSA specific keywords here
501 return _EncryptAndVerifyRSA.encrypt(self, msg, t=t, h=h, mgf=mgf, L=L)
502
503 def verify(self, msg, sig, t="pkcs", h="sha256", mgf=None, L=None):
504 return _EncryptAndVerifyRSA.verify(self, msg, sig, t=t, h=h, mgf=mgf, L=L)
505
506
507class PubKeyECDSA(PubKey):
508 """
509 Wrapper for ECDSA keys based on the cryptography library.
510 Use the 'key' attribute to access original object.
511 """
512
513 @crypto_validator
514 def fill_and_store(self, curve=None):
515 curve = curve or ec.SECP256R1
516 private_key = ec.generate_private_key(curve(), default_backend())
517 self.pubkey = private_key.public_key()
518
519 @crypto_validator
520 def import_from_der(self, pubkey):
521 # No lib support for explicit curves nor compressed points.
522 self.pubkey = serialization.load_der_public_key(
523 pubkey,
524 backend=default_backend(),
525 )
526
527 def encrypt(self, msg, h="sha256", **kwargs):
528 raise Exception("No ECDSA encryption support")
529
530 @crypto_validator
531 def verify(self, msg, sig, h="sha256", **kwargs):
532 # 'sig' should be a DER-encoded signature, as per RFC 3279
533 try:
534 self.pubkey.verify(sig, msg, ec.ECDSA(_get_hash(h)))
535 return True
536 except InvalidSignature:
537 return False
538
539
540class PubKeyEdDSA(PubKey):
541 """
542 Wrapper for EdDSA keys based on the cryptography library.
543 Use the 'key' attribute to access original object.
544 """
545
546 @crypto_validator
547 def fill_and_store(self, curve=None):
548 curve = curve or x25519.X25519PrivateKey
549 private_key = curve.generate()
550 self.pubkey = private_key.public_key()
551
552 @crypto_validator
553 def import_from_der(self, pubkey):
554 self.pubkey = serialization.load_der_public_key(
555 pubkey,
556 backend=default_backend(),
557 )
558
559 def encrypt(self, msg, **kwargs):
560 raise Exception("No EdDSA encryption support")
561
562 @crypto_validator
563 def verify(self, msg, sig, **kwargs):
564 # 'sig' should be a DER-encoded signature, as per RFC 3279
565 try:
566 self.pubkey.verify(sig, msg)
567 return True
568 except InvalidSignature:
569 return False
570
571
572class PubKeyMLDSA(PubKey):
573 """
574 Wrapper for MLDSA keys based on the cryptography library.
575 Use the 'key' attribute to access original object.
576 """
577
578 @crypto_validator
579 def fill_and_store(self, curve=None):
580 curve = curve or mldsa.MLDSA87PrivateKey
581 private_key = curve.generate()
582 self.pubkey = private_key.public_key()
583
584 @crypto_validator
585 def import_from_der(self, pubkey):
586 self.pubkey = serialization.load_der_public_key(
587 pubkey,
588 backend=default_backend(),
589 )
590
591 def encrypt(self, msg, **kwargs):
592 raise Exception("No MLDSA encryption support")
593
594 @crypto_validator
595 def verify(self, msg, sig, **kwargs):
596 # 'sig' should be a DER-encoded signature, as per RFC 3279
597 try:
598 self.pubkey.verify(sig, msg)
599 return True
600 except InvalidSignature:
601 return False
602
603
604################
605# Private Keys #
606################
607
608
609class _PrivKeyFactory(_PKIObjMaker):
610 """
611 Metaclass for PrivKey creation.
612 It casts the appropriate class on the fly, then fills in
613 the appropriate attributes with import_from_asn1pkt() submethod.
614 """
615
616 def __call__(cls, key_path=None, cryptography_obj=None):
617 """
618 key_path may be the path to either:
619 _an RSAPrivateKey_OpenSSL (as generated by openssl);
620 _an ECDSAPrivateKey_OpenSSL (as generated by openssl);
621 _an RSAPrivateKey;
622 _an ECDSAPrivateKey.
623 """
624 # This allows to import cryptography objects directly
625 if cryptography_obj is not None:
626 # We (stupidly) need to go through the whole import process because RSA
627 # does more than just importing the cryptography objects...
628 obj = _PKIObj(
629 "DER",
630 cryptography_obj.private_bytes(
631 encoding=serialization.Encoding.DER,
632 format=serialization.PrivateFormat.PKCS8,
633 encryption_algorithm=serialization.NoEncryption(),
634 ),
635 )
636 elif key_path is None:
637 obj = type.__call__(cls)
638 if cls is PrivKey:
639 cls = PrivKeyECDSA
640 obj.__class__ = cls
641 obj.frmt = "original"
642 obj.fill_and_store()
643 return obj
644 else:
645 # Load from file
646 obj = _PKIObjMaker.__call__(cls, key_path, _MAX_KEY_SIZE)
647
648 try:
649 # The modern format "OneAsymmetricKey" supports specifying the algorithm
650 privkey = X509_OneAsymmetricKey(obj._der)
651 if isinstance(privkey.privateKey, EdDSAPrivateKey):
652 obj.__class__ = PrivKeyEdDSA
653 elif isinstance(privkey.privateKey, MLDSAPrivateKey):
654 obj.__class__ = PrivKeyMLDSA
655 else:
656 raise
657 obj.marker = "PRIVATE KEY"
658 except Exception:
659 # If it fails, we have a chain of legacy fallbacks.
660 try:
661 privkey = RSAPrivateKey_OpenSSL(obj._der)
662 privkey = privkey.privateKey
663 obj.__class__ = PrivKeyRSA
664 obj.marker = "PRIVATE KEY"
665 except Exception:
666 try:
667 privkey = ECDSAPrivateKey_OpenSSL(obj._der)
668 privkey = privkey.privateKey
669 obj.__class__ = PrivKeyECDSA
670 obj.marker = "EC PRIVATE KEY"
671 except Exception:
672 try:
673 privkey = RSAPrivateKey(obj._der)
674 obj.__class__ = PrivKeyRSA
675 obj.marker = "RSA PRIVATE KEY"
676 except Exception:
677 try:
678 privkey = ECDSAPrivateKey(obj._der)
679 obj.__class__ = PrivKeyECDSA
680 obj.marker = "EC PRIVATE KEY"
681 except Exception:
682 raise Exception("Unable to import private key")
683 try:
684 obj.import_from_asn1pkt(privkey)
685 except ImportError:
686 pass
687 return obj
688
689
690class _Raw_ASN1_BIT_STRING(ASN1_BIT_STRING):
691 """A ASN1_BIT_STRING that ignores BER encoding"""
692
693 def __bytes__(self):
694 return self.val_readable
695
696 __str__ = __bytes__
697
698
699class PrivKey(metaclass=_PrivKeyFactory):
700 """
701 Parent class for PrivKeyRSA, PrivKeyECDSA and PrivKeyEdDSA.
702 Provides common signTBSCert(), resignCert(), verifyCert()
703 and export() methods.
704 """
705
706 def signTBSCert(self, tbsCert, h="sha256"):
707 """
708 Note that this will always copy the signature field from the
709 tbsCertificate into the signatureAlgorithm field of the result,
710 regardless of the coherence between its contents (which might
711 indicate ecdsa-with-SHA512) and the result (e.g. RSA signing MD2).
712
713 There is a small inheritance trick for the computation of sigVal
714 below: in order to use a sign() method which would apply
715 to both PrivKeyRSA and PrivKeyECDSA, the sign() methods of the
716 subclasses accept any argument, be it from the RSA or ECDSA world,
717 and then they keep the ones they're interested in.
718 Here, t will be passed eventually to pkcs1._DecryptAndSignRSA.sign().
719 """
720 sigAlg = tbsCert.signature
721 h = h or hash_by_oid[sigAlg.algorithm.val]
722 sigVal = self.sign(bytes(tbsCert), h=h, t="pkcs")
723 c = X509_Cert()
724 c.tbsCertificate = tbsCert
725 c.signatureAlgorithm = sigAlg
726 c.signatureValue = _Raw_ASN1_BIT_STRING(sigVal, readable=True)
727 return c
728
729 def resignCert(self, cert):
730 """Rewrite the signature of either a Cert or an X509_Cert."""
731 return self.signTBSCert(cert.tbsCertificate, h=None)
732
733 def verifyCert(self, cert):
734 """Verifies either a Cert or an X509_Cert."""
735 return self.pubkey.verifyCert(cert)
736
737 def verifyCsr(self, cert):
738 """Verifies either a CSR."""
739 return self.pubkey.verifyCsr(cert)
740
741 @property
742 def pem(self):
743 return der2pem(self.der, self.marker)
744
745 @property
746 def der(self):
747 return self.key.private_bytes(
748 encoding=serialization.Encoding.DER,
749 format=serialization.PrivateFormat.PKCS8,
750 encryption_algorithm=serialization.NoEncryption(),
751 )
752
753 def export(self, filename, fmt=None):
754 """
755 Export private key in 'fmt' format (DER or PEM) to file 'filename'
756 """
757 if fmt is None:
758 if filename.endswith(".pem"):
759 fmt = "PEM"
760 else:
761 fmt = "DER"
762 with open(filename, "wb") as f:
763 if fmt == "DER":
764 return f.write(self.der)
765 elif fmt == "PEM":
766 return f.write(self.pem.encode())
767
768 @crypto_validator
769 def sign(self, data, h="sha256", **kwargs):
770 """
771 Sign data.
772 """
773 raise NotImplementedError
774
775 @crypto_validator
776 def verify(self, msg, sig, h="sha256", **kwargs):
777 """
778 Verify signed data.
779 """
780 raise NotImplementedError
781
782
783class PrivKeyRSA(PrivKey, _DecryptAndSignRSA):
784 """
785 Wrapper for RSA keys based on _DecryptAndSignRSA from crypto/pkcs1.py
786 Use the 'key' attribute to access original object.
787 """
788
789 @crypto_validator
790 def fill_and_store(
791 self,
792 modulus=None,
793 modulusLen=None,
794 pubExp=None,
795 prime1=None,
796 prime2=None,
797 coefficient=None,
798 exponent1=None,
799 exponent2=None,
800 privExp=None,
801 ):
802 pubExp = pubExp or 65537
803 if None in [
804 modulus,
805 prime1,
806 prime2,
807 coefficient,
808 privExp,
809 exponent1,
810 exponent2,
811 ]:
812 # note that the library requires every parameter
813 # in order to call RSAPrivateNumbers(...)
814 # if one of these is missing, we generate a whole new key
815 real_modulusLen = modulusLen or 2048
816 if real_modulusLen < 1024 and not _RSA_512_SUPPORTED:
817 # cryptography > 43.0 compatibility
818 self.key = rust_openssl.rsa.generate_private_key(
819 public_exponent=pubExp,
820 key_size=real_modulusLen,
821 )
822 else:
823 self.key = rsa.generate_private_key(
824 public_exponent=pubExp,
825 key_size=real_modulusLen,
826 backend=default_backend(),
827 )
828 pubkey = self.key.public_key()
829 else:
830 real_modulusLen = len(binrepr(modulus))
831 if modulusLen and real_modulusLen != modulusLen:
832 warning("modulus and modulusLen do not match!")
833 pubNum = rsa.RSAPublicNumbers(n=modulus, e=pubExp)
834 privNum = rsa.RSAPrivateNumbers(
835 p=prime1,
836 q=prime2,
837 dmp1=exponent1,
838 dmq1=exponent2,
839 iqmp=coefficient,
840 d=privExp,
841 public_numbers=pubNum,
842 )
843 self.key = privNum.private_key(default_backend())
844 pubkey = self.key.public_key()
845
846 self.marker = "PRIVATE KEY"
847
848 # Lines below are only useful for the legacy part of pkcs1.py
849 pubNum = pubkey.public_numbers()
850 self._modulusLen = real_modulusLen
851 self._modulus = pubNum.n
852 self._pubExp = pubNum.e
853
854 self.pubkey = PubKeyRSA((pubNum.e, pubNum.n, real_modulusLen))
855
856 def import_from_asn1pkt(self, privkey):
857 modulus = privkey.modulus.val
858 pubExp = privkey.publicExponent.val
859 privExp = privkey.privateExponent.val
860 prime1 = privkey.prime1.val
861 prime2 = privkey.prime2.val
862 exponent1 = privkey.exponent1.val
863 exponent2 = privkey.exponent2.val
864 coefficient = privkey.coefficient.val
865 self.fill_and_store(
866 modulus=modulus,
867 pubExp=pubExp,
868 privExp=privExp,
869 prime1=prime1,
870 prime2=prime2,
871 exponent1=exponent1,
872 exponent2=exponent2,
873 coefficient=coefficient,
874 )
875
876 def verify(self, msg, sig, t="pkcs", h="sha256", mgf=None, L=None):
877 return self.pubkey.verify(
878 msg=msg,
879 sig=sig,
880 t=t,
881 h=h,
882 mgf=mgf,
883 L=L,
884 )
885
886 def sign(self, data, t="pkcs", h="sha256", mgf=None, L=None):
887 return _DecryptAndSignRSA.sign(self, data, t=t, h=h, mgf=mgf, L=L)
888
889
890class PrivKeyECDSA(PrivKey):
891 """
892 Wrapper for ECDSA keys based on SigningKey from ecdsa library.
893 Use the 'key' attribute to access original object.
894 """
895
896 @crypto_validator
897 def fill_and_store(self, curve=None):
898 curve = curve or ec.SECP256R1
899 self.key = ec.generate_private_key(curve(), default_backend())
900 self.pubkey = PubKeyECDSA(cryptography_obj=self.key.public_key())
901 self.marker = "EC PRIVATE KEY"
902
903 @crypto_validator
904 def import_from_asn1pkt(self, privkey):
905 self.key = serialization.load_der_private_key(
906 bytes(privkey), None, backend=default_backend()
907 )
908 self.pubkey = PubKeyECDSA(cryptography_obj=self.key.public_key())
909 self.marker = "EC PRIVATE KEY"
910
911 @crypto_validator
912 def verify(self, msg, sig, h="sha256", **kwargs):
913 return self.pubkey.verify(msg=msg, sig=sig, h=h, **kwargs)
914
915 @crypto_validator
916 def sign(self, data, h="sha256", **kwargs):
917 return self.key.sign(data, ec.ECDSA(_get_hash(h)))
918
919
920class PrivKeyEdDSA(PrivKey):
921 """
922 Wrapper for EdDSA keys
923 Use the 'key' attribute to access original object.
924 """
925
926 @crypto_validator
927 def fill_and_store(self, curve=None):
928 curve = curve or x25519.X25519PrivateKey
929 self.key = curve.generate()
930 self.pubkey = PubKeyEdDSA(cryptography_obj=self.key.public_key())
931 self.marker = "PRIVATE KEY"
932
933 @crypto_validator
934 def import_from_asn1pkt(self, privkey):
935 self.key = serialization.load_der_private_key(
936 bytes(privkey), None, backend=default_backend()
937 )
938 self.pubkey = PubKeyEdDSA(cryptography_obj=self.key.public_key())
939 self.marker = "PRIVATE KEY"
940
941 @crypto_validator
942 def verify(self, msg, sig, **kwargs):
943 return self.pubkey.verify(msg=msg, sig=sig, **kwargs)
944
945 @crypto_validator
946 def sign(self, data, **kwargs):
947 return self.key.sign(data)
948
949
950class PrivKeyMLDSA(PrivKey):
951 """
952 Wrapper for MLDSA keys
953 Use the 'key' attribute to access original object.
954 """
955
956 @crypto_validator
957 def fill_and_store(self, curve=None):
958 curve = curve or mldsa.MLDSA87PrivateKey
959 self.key = curve.generate()
960 self.pubkey = PubKeyMLDSA(cryptography_obj=self.key.public_key())
961 self.marker = "PRIVATE KEY"
962
963 @crypto_validator
964 def import_from_asn1pkt(self, privkey):
965 self.key = serialization.load_der_private_key(
966 bytes(privkey), None, backend=default_backend()
967 )
968 self.pubkey = PubKeyMLDSA(cryptography_obj=self.key.public_key())
969 self.marker = "PRIVATE KEY"
970
971 @crypto_validator
972 def verify(self, msg, sig, **kwargs):
973 return self.pubkey.verify(msg=msg, sig=sig, **kwargs)
974
975 @crypto_validator
976 def sign(self, data, **kwargs):
977 return self.key.sign(data)
978
979
980################
981# Certificates #
982################
983
984
985class _CertMaker(_PKIObjMaker):
986 """
987 Metaclass for Cert creation. It is not necessary as it was for the keys,
988 but we reuse the model instead of creating redundant constructors.
989 """
990
991 def __call__(cls, cert_path=None, cryptography_obj=None):
992 # This allows to import cryptography objects directly
993 if cryptography_obj is not None:
994 obj = _PKIObj(
995 "DER",
996 cryptography_obj.public_bytes(
997 encoding=serialization.Encoding.DER,
998 ),
999 )
1000 else:
1001 # Load from file
1002 obj = _PKIObjMaker.__call__(cls, cert_path, _MAX_CERT_SIZE, "CERTIFICATE")
1003 obj.__class__ = Cert
1004 obj.marker = "CERTIFICATE"
1005 try:
1006 cert = X509_Cert(obj._der)
1007 except Exception:
1008 if conf.debug_dissector:
1009 raise
1010 raise Exception("Unable to import certificate")
1011 cert.remove_payload()
1012 obj.import_from_asn1pkt(cert)
1013 return obj
1014
1015
1016def _get_cert_sig_hashname(cert):
1017 """
1018 Return the hash associated with the signature algorithm of a certificate.
1019 """
1020 tbsCert = cert.tbsCertificate
1021 sigAlg = tbsCert.signature
1022 return hash_by_oid[sigAlg.algorithm.val]
1023
1024
1025def _get_csr_sig_hashname(csr):
1026 """
1027 Return the hash associated with the signature algorithm of a CSR.
1028 """
1029 certReq = csr.certReq
1030 sigAlg = certReq.signatureAlgorithm
1031 return hash_by_oid[sigAlg.algorithm.val]
1032
1033
1034def _match_dns_name(pattern, hostname):
1035 """
1036 Whether a certificate DNS name matches a hostname, RFC 6125 sect 6.4.3.
1037
1038 :param pattern: a dNSName from the certificate
1039 :param hostname: the name the client asked for
1040 :return: True if they match
1041 """
1042 pattern = pattern.lower().rstrip(".")
1043 hostname = hostname.lower().rstrip(".")
1044 if pattern[:2] == "*.":
1045 return pattern[2:] == hostname.split(".", 1)[-1]
1046 else:
1047 return pattern == hostname
1048
1049
1050class Cert(metaclass=_CertMaker):
1051 """
1052 Wrapper for the X509_Cert from layers/x509.py.
1053 Use the 'x509Cert' attribute to access original object.
1054 """
1055
1056 def import_from_asn1pkt(self, cert):
1057 error_msg = "Unable to import certificate"
1058
1059 self.x509Cert = cert
1060
1061 tbsCert = cert.tbsCertificate
1062
1063 if tbsCert.version:
1064 self.version = tbsCert.version.val + 1
1065 else:
1066 self.version = 1
1067 self.serial = tbsCert.serialNumber.val
1068 self.sigAlg = tbsCert.signature.algorithm.oidname
1069 self.issuer = tbsCert.get_issuer()
1070 self.issuer_str = tbsCert.get_issuer_str()
1071 self.issuer_hash = hash(self.issuer_str)
1072 self.subject = tbsCert.get_subject()
1073 self.subject_str = tbsCert.get_subject_str()
1074 self.subject_hash = hash(self.subject_str)
1075 self.authorityKeyID = None
1076 self.extKeyUsage = None
1077
1078 self.notBefore_str = tbsCert.validity.not_before.pretty_time
1079 try:
1080 self.notBefore = tbsCert.validity.not_before.datetime.timetuple()
1081 except ValueError:
1082 raise Exception(error_msg)
1083 self.notBefore_str_simple = time.strftime("%x", self.notBefore)
1084
1085 self.notAfter_str = tbsCert.validity.not_after.pretty_time
1086 try:
1087 self.notAfter = tbsCert.validity.not_after.datetime.timetuple()
1088 except ValueError:
1089 raise Exception(error_msg)
1090 self.notAfter_str_simple = time.strftime("%x", self.notAfter)
1091
1092 self.pubkey = PubKey(bytes(tbsCert.subjectPublicKeyInfo))
1093 self.subjectAltName = None
1094 self.cA = False
1095 self.pathLenConstraint = None
1096
1097 if tbsCert.extensions:
1098 for extn in tbsCert.extensions:
1099 if extn.extnID.oidname == "basicConstraints":
1100 if extn.extnValue.cA:
1101 self.cA = not (extn.extnValue.cA.val == 0)
1102 if extn.extnValue.pathLenConstraint is not None:
1103 self.pathLenConstraint = extn.extnValue.pathLenConstraint.val
1104 elif extn.extnID.oidname == "keyUsage":
1105 self.keyUsage = extn.extnValue.get_keyUsage()
1106 elif extn.extnID.oidname == "extKeyUsage":
1107 self.extKeyUsage = extn.extnValue.get_extendedKeyUsage()
1108 elif extn.extnID.oidname == "authorityKeyIdentifier":
1109 self.authorityKeyID = extn.extnValue.keyIdentifier.val
1110 elif extn.extnID.oidname == "subjectAltName":
1111 self.subjectAltName = extn.extnValue.subjectAltName
1112
1113 self.signatureValue = bytes(cert.signatureValue)
1114 self.signatureLen = len(self.signatureValue)
1115
1116 def isIssuer(self, other):
1117 """
1118 True if 'other' issued 'self', i.e.:
1119 - self.issuer == other.subject
1120 - self is signed by other
1121 """
1122 if self.issuer_hash != other.subject_hash:
1123 return False
1124 return other.pubkey.verifyCert(self)
1125
1126 def isIssuerCert(self, other):
1127 return self.isIssuer(other)
1128
1129 def isSelfSigned(self):
1130 """
1131 Return True if the certificate is self-signed:
1132 - issuer and subject are the same
1133 - the signature of the certificate is valid.
1134 """
1135 if self.issuer_hash == self.subject_hash:
1136 return self.isIssuer(self)
1137 return False
1138
1139 def encrypt(self, msg, t="pkcs", h="sha256", mgf=None, L=None):
1140 # no ECDSA *encryption* support, hence only RSA specific keywords here
1141 return self.pubkey.encrypt(msg, t=t, h=h, mgf=mgf, L=L)
1142
1143 def verify(self, msg, sig, t="pkcs", h="sha256", mgf=None, L=None):
1144 return self.pubkey.verify(msg, sig, t=t, h=h, mgf=mgf, L=L)
1145
1146 def getCertSignatureHash(self):
1147 """
1148 Return the hash cryptography object used by the 'signatureAlgorithm'
1149 """
1150 return _get_hash(_get_cert_sig_hashname(self))
1151
1152 def setSubjectPublicKeyFromPrivateKey(self, key):
1153 """
1154 Replace the subjectPublicKeyInfo of this certificate with the one from
1155 the provided key.
1156 """
1157 if isinstance(key, (PubKey, PrivKey)):
1158 if isinstance(key, PrivKey):
1159 pubkey = key.pubkey
1160 else:
1161 pubkey = key
1162 self.tbsCertificate.subjectPublicKeyInfo = X509_SubjectPublicKeyInfo(
1163 pubkey.der
1164 )
1165 else:
1166 raise ValueError("Unknown type 'key', should be PubKey or PrivKey")
1167
1168 def resignWith(self, key):
1169 """
1170 Resign a certificate with a specific key
1171 """
1172 self.import_from_asn1pkt(key.resignCert(self))
1173
1174 def remainingDays(self, now=None):
1175 """
1176 Based on the value of notAfter field, returns the number of
1177 days the certificate will still be valid. The date used for the
1178 comparison is the current and local date, as returned by
1179 time.localtime(), except if 'now' argument is provided another
1180 one. 'now' argument can be given as either a time tuple or a string
1181 representing the date. Accepted format for the string version
1182 are:
1183
1184 - '%b %d %H:%M:%S %Y %Z' e.g. 'Jan 30 07:38:59 2008 GMT'
1185 - '%m/%d/%y' e.g. '01/30/08' (less precise)
1186
1187 If the certificate is no more valid at the date considered, then
1188 a negative value is returned representing the number of days
1189 since it has expired.
1190
1191 The number of days is returned as a float to deal with the unlikely
1192 case of certificates that are still just valid.
1193 """
1194 if now is None:
1195 now = time.localtime()
1196 elif isinstance(now, str):
1197 try:
1198 if "/" in now:
1199 now = time.strptime(now, "%m/%d/%y")
1200 else:
1201 now = time.strptime(now, "%b %d %H:%M:%S %Y %Z")
1202 except Exception:
1203 warning("Bad time string provided, will use localtime() instead.")
1204 now = time.localtime()
1205
1206 now = time.mktime(now)
1207 nft = time.mktime(self.notAfter)
1208 diff = (nft - now) / (24.0 * 3600)
1209 return diff
1210
1211 def isValidAt(self, now=None):
1212 """
1213 Whether the current time falls inside the certificate's validity period.
1214
1215 The comparison is made in UTC, which is how notBefore and notAfter are
1216 stored. (:func:`remainingDays` compares in local time and so is off by
1217 the local UTC offset.)
1218
1219 :param now: (optional) a UTC time tuple to compare against, defaulting
1220 to the current time
1221 :return: True if the certificate is neither expired nor not yet valid
1222 """
1223 if now is None:
1224 now = time.gmtime()
1225 now = calendar.timegm(now)
1226 return calendar.timegm(self.notBefore) <= now <= calendar.timegm(self.notAfter)
1227
1228 def matchesHostname(self, hostname):
1229 """
1230 Whether this certificate was issued to the given host.
1231
1232 Names come from the subjectAltName extension. RFC 6125 sect 6.4.4 says
1233 the Common Name is only consulted when there is no subjectAltName at
1234 all, and that is what happens here.
1235
1236 :param hostname: the DNS name or IP address the client asked for
1237 :return: True if the certificate names that host
1238 """
1239 if not hostname:
1240 return False
1241 hostname = plain_str(hostname)
1242
1243 if self.subjectAltName is not None:
1244 # Check subjectAltName
1245 for generalName in self.subjectAltName:
1246 name = generalName.generalName
1247 if isinstance(name, X509_DNSName):
1248 if _match_dns_name(plain_str(name.dNSName.val), hostname):
1249 return True
1250 elif isinstance(name, X509_IPAddress):
1251 raw = name.iPAddress.val
1252 if len(raw) == 4:
1253 if inet_ntop(socket.AF_INET, raw) == hostname:
1254 return True
1255 elif len(raw) == 16:
1256 if inet_ntop(socket.AF_INET6, raw) == hostname:
1257 return True
1258 return False
1259 else:
1260 # Check Common Name
1261 if "commonName" in self.subject:
1262 return _match_dns_name(self.subject["commonName"], hostname)
1263 return False
1264
1265 def isRevoked(self, crl_list):
1266 """
1267 Given a list of trusted CRL (their signature has already been
1268 verified with trusted anchors), this function returns True if
1269 the certificate is marked as revoked by one of those CRL.
1270
1271 Note that if the Certificate was on hold in a previous CRL and
1272 is now valid again in a new CRL and bot are in the list, it
1273 will be considered revoked: this is because _all_ CRLs are
1274 checked (not only the freshest) and revocation status is not
1275 handled.
1276
1277 Also note that the check on the issuer is performed on the
1278 Authority Key Identifier if available in _both_ the CRL and the
1279 Cert. Otherwise, the issuers are simply compared.
1280 """
1281 for c in crl_list:
1282 if (
1283 self.authorityKeyID is not None
1284 and c.authorityKeyID is not None
1285 and self.authorityKeyID == c.authorityKeyID
1286 ):
1287 return self.serial in (x[0] for x in c.revoked_cert_serials)
1288 elif self.issuer == c.issuer:
1289 return self.serial in (x[0] for x in c.revoked_cert_serials)
1290 return False
1291
1292 @property
1293 def tbsCertificate(self):
1294 return self.x509Cert.tbsCertificate
1295
1296 @property
1297 def pem(self):
1298 return der2pem(self.der, self.marker)
1299
1300 @property
1301 def der(self):
1302 return bytes(self.x509Cert)
1303
1304 @property
1305 def pubKey(self):
1306 warnings.warn(
1307 "Cert.pubKey is deprecated and will be removed in a future version. "
1308 "Use Cert.pubkey",
1309 DeprecationWarning,
1310 )
1311 return self.pubkey
1312
1313 @property
1314 def extensions(self):
1315 return self.tbsCertificate.extensions
1316
1317 def __eq__(self, other):
1318 return self.der == other.der
1319
1320 def __hash__(self):
1321 return hash(self.der)
1322
1323 def export(self, filename, fmt=None):
1324 """
1325 Export certificate in 'fmt' format (DER or PEM) to file 'filename'
1326 """
1327 if fmt is None:
1328 if filename.endswith(".pem"):
1329 fmt = "PEM"
1330 else:
1331 fmt = "DER"
1332 with open(filename, "wb") as f:
1333 if fmt == "DER":
1334 return f.write(self.der)
1335 elif fmt == "PEM":
1336 return f.write(self.pem.encode())
1337
1338 def show(self):
1339 print("Serial: %s" % self.serial)
1340 print("Issuer: " + self.issuer_str)
1341 print("Subject: " + self.subject_str)
1342 print("Validity: %s to %s" % (self.notBefore_str, self.notAfter_str))
1343
1344 def __repr__(self):
1345 return "[X.509 Cert. Subject:%s, Issuer:%s]" % (
1346 self.subject_str,
1347 self.issuer_str,
1348 )
1349
1350
1351################################
1352# Certificate Revocation Lists #
1353################################
1354
1355
1356class _CRLMaker(_PKIObjMaker):
1357 """
1358 Metaclass for CRL creation. It is not necessary as it was for the keys,
1359 but we reuse the model instead of creating redundant constructors.
1360 """
1361
1362 def __call__(cls, cert_path):
1363 obj = _PKIObjMaker.__call__(cls, cert_path, _MAX_CRL_SIZE, "X509 CRL")
1364 obj.__class__ = CRL
1365 try:
1366 crl = X509_CRL(obj._der)
1367 except Exception:
1368 raise Exception("Unable to import CRL")
1369 obj.import_from_asn1pkt(crl)
1370 return obj
1371
1372
1373class CRL(metaclass=_CRLMaker):
1374 """
1375 Wrapper for the X509_CRL from layers/x509.py.
1376 Use the 'x509CRL' attribute to access original object.
1377 """
1378
1379 def import_from_asn1pkt(self, crl):
1380 error_msg = "Unable to import CRL"
1381
1382 self.x509CRL = crl
1383
1384 tbsCertList = crl.tbsCertList
1385 self.tbsCertList = bytes(tbsCertList)
1386
1387 if tbsCertList.version:
1388 self.version = tbsCertList.version.val + 1
1389 else:
1390 self.version = 1
1391 self.sigAlg = tbsCertList.signature.algorithm.oidname
1392 self.issuer = tbsCertList.get_issuer()
1393 self.issuer_str = tbsCertList.get_issuer_str()
1394 self.issuer_hash = hash(self.issuer_str)
1395
1396 self.lastUpdate_str = tbsCertList.this_update.pretty_time
1397 lastUpdate = tbsCertList.this_update.val
1398 if lastUpdate[-1] == "Z":
1399 lastUpdate = lastUpdate[:-1]
1400 try:
1401 self.lastUpdate = time.strptime(lastUpdate, "%y%m%d%H%M%S")
1402 except Exception:
1403 raise Exception(error_msg)
1404 self.lastUpdate_str_simple = time.strftime("%x", self.lastUpdate)
1405
1406 self.nextUpdate = None
1407 self.nextUpdate_str_simple = None
1408 if tbsCertList.next_update:
1409 self.nextUpdate_str = tbsCertList.next_update.pretty_time
1410 nextUpdate = tbsCertList.next_update.val
1411 if nextUpdate[-1] == "Z":
1412 nextUpdate = nextUpdate[:-1]
1413 try:
1414 self.nextUpdate = time.strptime(nextUpdate, "%y%m%d%H%M%S")
1415 except Exception:
1416 raise Exception(error_msg)
1417 self.nextUpdate_str_simple = time.strftime("%x", self.nextUpdate)
1418
1419 if tbsCertList.crlExtensions:
1420 for extension in tbsCertList.crlExtensions:
1421 if extension.extnID.oidname == "cRLNumber":
1422 self.number = extension.extnValue.cRLNumber.val
1423
1424 revoked = []
1425 if tbsCertList.revokedCertificates:
1426 for cert in tbsCertList.revokedCertificates:
1427 serial = cert.serialNumber.val
1428 date = cert.revocationDate.val
1429 if date[-1] == "Z":
1430 date = date[:-1]
1431 try:
1432 time.strptime(date, "%y%m%d%H%M%S")
1433 except Exception:
1434 raise Exception(error_msg)
1435 revoked.append((serial, date))
1436 self.revoked_cert_serials = revoked
1437
1438 self.signatureValue = bytes(crl.signatureValue)
1439 self.signatureLen = len(self.signatureValue)
1440
1441 def isIssuer(self, other):
1442 # This is exactly the same thing as in Cert method.
1443 if self.issuer_hash != other.subject_hash:
1444 return False
1445 return other.pubkey.verifyCert(self)
1446
1447 def verify(self, anchors):
1448 # Return True iff the CRL is signed by one of the provided anchors.
1449 return any(self.isIssuer(a) for a in anchors)
1450
1451 def show(self):
1452 print("Version: %d" % self.version)
1453 print("sigAlg: " + self.sigAlg)
1454 print("Issuer: " + self.issuer_str)
1455 print("lastUpdate: %s" % self.lastUpdate_str)
1456 print("nextUpdate: %s" % self.nextUpdate_str)
1457
1458
1459###############################
1460# Certificate Signing Request #
1461###############################
1462
1463
1464class _CSRMaker(_PKIObjMaker):
1465 """
1466 Metaclass for CSR creation. It is not necessary as it was for the keys,
1467 but we reuse the model instead of creating redundant constructors.
1468 """
1469
1470 def __call__(cls, cert_path):
1471 obj = _PKIObjMaker.__call__(cls, cert_path, _MAX_CSR_SIZE)
1472 obj.__class__ = CSR
1473 try:
1474 # PKCS#10 format
1475 csr = PKCS10_CertificationRequest(obj._der)
1476 obj.marker = "NEW CERTIFICATE REQUEST"
1477 obj.fmt = CSR.FORMAT.PKCS10
1478 except Exception:
1479 try:
1480 # CMC format
1481 csr = CMS_ContentInfo(obj._der)
1482 obj.marker = "NEW CERTIFICATE REQUEST"
1483 obj.fmt = CSR.FORMAT.CMC
1484 except Exception:
1485 raise Exception("Unable to import CSR")
1486
1487 obj.import_from_asn1pkt(csr)
1488 return obj
1489
1490
1491class CSR(metaclass=_CSRMaker):
1492 """
1493 Wrapper for the CSR formats.
1494 This can handle both PKCS#10 and CMC formats.
1495 """
1496
1497 class FORMAT(enum.Enum):
1498 """
1499 The format used by the CSR.
1500 """
1501
1502 PKCS10 = "PKCS#10"
1503 CMC = "CMC"
1504
1505 def import_from_asn1pkt(self, csr):
1506 self.csr = csr
1507 certReqInfo = self.certReq.certificationRequestInfo
1508
1509 # Subject
1510 self.subject = certReqInfo.get_subject()
1511 self.subject_str = certReqInfo.get_subject_str()
1512 self.subject_hash = hash(self.subject_str)
1513
1514 # pubkey
1515 self.pubkey = PubKey(bytes(certReqInfo.subjectPublicKeyInfo))
1516
1517 # Get the "subjectKeyIdentifier" from the "extensionRequest" attribute
1518 try:
1519 extReq = next(
1520 x.values[0].value
1521 for x in certReqInfo.attributes
1522 if x.type.val == "1.2.840.113549.1.9.14" # extKeyUsage
1523 )
1524 self.sid = next(
1525 x.extnValue.keyIdentifier
1526 for x in extReq.extensions
1527 if x.extnID.val == "2.5.29.14" # subjectKeyIdentifier
1528 )
1529 except StopIteration:
1530 self.sid = None
1531
1532 @property
1533 def certReq(self):
1534 csr = self.csr
1535
1536 if self.fmt == CSR.FORMAT.PKCS10:
1537 return csr
1538 elif self.fmt == CSR.FORMAT.CMC:
1539 if (
1540 csr.contentType.oidname != "id-signedData"
1541 or csr.content.encapContentInfo.eContentType.oidname != "id-cct-PKIData"
1542 ):
1543 raise ValueError("Invalid CMC wrapping !")
1544 req = csr.content.encapContentInfo.eContent.reqSequence[0]
1545 return req.request.certificationRequest
1546 else:
1547 raise ValueError("Invalid CSR format !")
1548
1549 @property
1550 def pem(self):
1551 return der2pem(self.der, self.marker)
1552
1553 @property
1554 def der(self):
1555 return bytes(self.csr)
1556
1557 def __eq__(self, other):
1558 return self.der == other.der
1559
1560 def __hash__(self):
1561 return hash(self.der)
1562
1563 def isIssuer(self, other):
1564 return other.sid == self.sid
1565
1566 def isSelfSigned(self):
1567 return True
1568
1569 def verify(self, msg, sig, t="pkcs", h="sha256", mgf=None, L=None):
1570 return self.pubkey.verify(msg, sig, t=t, h=h, mgf=mgf, L=L)
1571
1572 def export(self, filename, fmt=None):
1573 """
1574 Export certificate in 'fmt' format (DER or PEM) to file 'filename'
1575 """
1576 if fmt is None:
1577 if filename.endswith(".pem"):
1578 fmt = "PEM"
1579 else:
1580 fmt = "DER"
1581 with open(filename, "wb") as f:
1582 if fmt == "DER":
1583 return f.write(self.der)
1584 elif fmt == "PEM":
1585 return f.write(self.pem.encode())
1586
1587 def show(self):
1588 certReqInfo = self.certReq.certificationRequestInfo
1589
1590 print("Subject: " + self.subject_str)
1591 print("Attributes:")
1592 for attr in certReqInfo.attributes:
1593 print(" - %s" % attr.type.oidname)
1594
1595 def verifySelf(self) -> bool:
1596 """
1597 Verify the signatures of the CSR
1598 """
1599 if self.fmt == self.FORMAT.CMC:
1600 try:
1601 cms_engine = CMS_Engine([self])
1602 cms_engine.verify(self.csr)
1603 return self.pubkey.verifyCsr(self)
1604 except ValueError:
1605 return False
1606 elif self.fmt == self.FORMAT.PKCS10:
1607 return self.pubkey.verifyCsr(self)
1608 else:
1609 return False
1610
1611 def __repr__(self):
1612 return "[CSR Format: %s, Subject:%s, Verified: %s]" % (
1613 self.fmt.value,
1614 self.subject_str,
1615 self.verifySelf(),
1616 )
1617
1618
1619####################
1620# Certificate list #
1621####################
1622
1623
1624class CertList(list):
1625 """
1626 An object that can store a list of Cert objects, load them and export them
1627 into DER/PEM format.
1628 """
1629
1630 def __init__(
1631 self,
1632 certList: Union[Self, List[Cert], List[CSR], Cert, str],
1633 ):
1634 """
1635 Construct a list of certificates/CRLs to be used as list of ROOT certificates.
1636 """
1637 # Parse the certificate list / CA
1638 if isinstance(certList, str):
1639 # It's a path. First get the _PKIObj
1640 obj = _PKIObjMaker.__call__(
1641 CertList, certList, _MAX_CERT_SIZE, "CERTIFICATE"
1642 )
1643
1644 # Then parse the der until there's nothing left
1645 certList = []
1646 payload = obj._der
1647 while payload:
1648 cert = X509_Cert(payload)
1649 if conf.raw_layer in cert.payload:
1650 payload = cert.payload.load
1651 else:
1652 payload = None
1653 cert.remove_payload()
1654 certList.append(Cert(cert))
1655
1656 self.frmt = obj.frmt
1657 elif isinstance(certList, Cert):
1658 certList = [certList]
1659 self.frmt = "PEM"
1660 else:
1661 self.frmt = "PEM"
1662
1663 super(CertList, self).__init__(certList)
1664
1665 @classmethod
1666 def load_system_store(cls):
1667 """
1668 Return a CertList containing the default trusted system store.
1669 """
1670 context = ssl.create_default_context()
1671 certs = []
1672 for der in context.get_ca_certs(binary_form=True):
1673 try:
1674 certs.append(Cert(der))
1675 except Exception as ex:
1676 log_runtime.error("Failed loading cert.")
1677 log_runtime.error(der2pem(der))
1678 raise ex
1679 return cls(certs)
1680
1681 def findCertBySid(self, sid):
1682 """
1683 Find a certificate in the list by SubjectIDentifier.
1684 """
1685 for cert in self:
1686 if isinstance(cert, Cert) and isinstance(sid, CMS_IssuerAndSerialNumber):
1687 if cert.issuer == sid.get_issuer():
1688 return cert
1689 elif isinstance(cert, CSR) and isinstance(sid, CMS_SubjectKeyIdentifier):
1690 if cert.sid == sid.sid:
1691 return cert
1692 raise KeyError("Certificate not found !")
1693
1694 def export(self, filename, fmt=None):
1695 """
1696 Export a list of certificates 'fmt' format (DER or PEM) to file 'filename'
1697 """
1698 if fmt is None:
1699 if filename.endswith(".pem"):
1700 fmt = "PEM"
1701 else:
1702 fmt = "DER"
1703 with open(filename, "wb") as f:
1704 if fmt == "DER":
1705 return f.write(self.der)
1706 elif fmt == "PEM":
1707 return f.write(self.pem.encode())
1708
1709 @property
1710 def der(self):
1711 return b"".join(x.der for x in self)
1712
1713 @property
1714 def pem(self):
1715 return "".join(x.pem for x in self)
1716
1717 def __repr__(self):
1718 return "<CertList %s certificates>" % (len(self),)
1719
1720 def show(self):
1721 for i, c in enumerate(self):
1722 print(conf.color_theme.id(i, fmt="%04i"), end=" ")
1723 print(repr(c))
1724
1725
1726######################
1727# Certificate chains #
1728######################
1729
1730
1731class CertTree(CertList):
1732 """
1733 An extension to CertList that additionally has a list of ROOT CAs
1734 that are trusted.
1735
1736 Example::
1737
1738 >>> tree = CertTree("ca_chain.pem")
1739 >>> tree.show()
1740 /CN=DOMAIN-DC1-CA/dc=DOMAIN [Self Signed]
1741 /CN=Administrator/dc=DOMAIN [Not Self Signed]
1742 """
1743
1744 __slots__ = ["frmt", "rootCAs"]
1745
1746 def __init__(
1747 self,
1748 certList: Union[List[Cert], CertList, str],
1749 rootCAs: Union[List[Cert], CertList, Cert, str, None] = None,
1750 ):
1751 """
1752 Construct a chain of certificates that follows issuer/subject matching and
1753 respects signature validity.
1754
1755 Note that we do not check AKID/{SKID/issuer/serial} matching,
1756 nor the presence of keyCertSign in keyUsage extension (if present).
1757
1758 :param certList: a list of Cert/CRL objects (or path to PEM/DER file containing
1759 multiple certs/CRL) to try to chain.
1760 :param rootCAs: (optional) a list of certificates to trust. If not provided,
1761 trusts any self-signed certificates from the certList.
1762 """
1763 # Parse the certificate list
1764 certList = CertList(certList)
1765
1766 # Find the ROOT CAs if store isn't specified
1767 if rootCAs is None:
1768 # Build cert store.
1769 self.rootCAs = CertList([x for x in certList if x.isSelfSigned()])
1770 # And remove those certs from the list
1771 for cert in self.rootCAs:
1772 certList.remove(cert)
1773 else:
1774 # Store cert store.
1775 self.rootCAs = CertList(rootCAs)
1776 # And remove those certs from the list if present (remove dups)
1777 for cert in self.rootCAs:
1778 if cert in certList:
1779 certList.remove(cert)
1780
1781 # Append our root CAs to the certList
1782 certList.extend(self.rootCAs)
1783
1784 # Super instantiate
1785 super(CertTree, self).__init__(certList)
1786
1787 @property
1788 def tree(self):
1789 """
1790 Get a tree-like object of the certificate list
1791 """
1792 # We store the tree object as a dictionary that contains children.
1793 tree = [(x, []) for x in self.rootCAs]
1794
1795 # We'll empty this list eventually
1796 certList = list(self)
1797
1798 # We make a list of certificates we have to search children for, and iterate
1799 # through it until it's empty.
1800 todo = list(tree)
1801
1802 # Iterate
1803 while todo:
1804 cert, children = todo.pop()
1805 for c in certList:
1806 # Check if this certificate matches the one we're looking at
1807 if c.isIssuer(cert) and c != cert:
1808 item = (c, [])
1809 children.append(item)
1810 certList.remove(c)
1811 todo.append(item)
1812
1813 return tree
1814
1815 def getchain(self, cert):
1816 """
1817 Return a chain of certificate that points from a ROOT CA to a certificate.
1818 """
1819
1820 def _rec_getchain(chain, curtree):
1821 # See if an element of the current tree signs the cert, if so add it to
1822 # the chain, else recurse.
1823 for c, subtree in curtree:
1824 curchain = chain + [c]
1825 # If 'cert' is issued by c
1826 if cert.isIssuer(c) or c == cert:
1827 # Final node of the chain !
1828 # (add the final cert if not self signed)
1829 if c != cert:
1830 curchain += [cert]
1831 return curchain
1832 else:
1833 # Not the final node of the chain ! Recurse.
1834 curchain = _rec_getchain(curchain, subtree)
1835 if curchain:
1836 return curchain
1837 return None
1838
1839 chain = _rec_getchain([], self.tree)
1840 if chain is not None:
1841 # We add the first certificate to the ROOT in all cases
1842 return CertTree(chain, [chain[0]])
1843 else:
1844 return None
1845
1846 def verify(
1847 self,
1848 cert: Cert,
1849 hostname: str = None,
1850 now: time.struct_time = None,
1851 allow_expired: bool = False,
1852 ekus: Optional[List[ASN1_OID]] = None,
1853 ):
1854 """
1855 Verify that a certificate is properly signed, current, and the right one.
1856
1857 Raises ValueError when the certificate fails any of the checks.
1858
1859 :param cert: the certificate to verify
1860 :param hostname: (optional) the DNS name or IP address the peer was
1861 expected to be. Without it the identity of the peer is not checked,
1862 so any certificate the store can chain is accepted.
1863 :param now: (optional) a UTC time tuple to check validity against,
1864 defaulting to the current time
1865 :param allow_expired: (optional) accept a chain that is outside its
1866 validity period. Useful when checking a signature made while the
1867 certificate was still valid, or against a stored capture, where
1868 the dates say nothing about whether the signature was genuine.
1869 :param ekus: if provided, verifies that the signer certificate has the
1870 following EKUs.
1871 """
1872 # Check that we can find a chain to this certificate
1873 chain = self.getchain(cert)
1874 if not chain:
1875 raise ValueError("Certificate verification failed !")
1876
1877 # Nothing in the chain may have expired or be in the future: an issuer
1878 # that is out of date does not vouch for anything below it. A chain can
1879 # also hold a CSR, which has no validity period to check.
1880 # Every certificate that issued another one in the chain must also be
1881 # a CA, and may bound the number of CAs below it (RFC 5280 sect 6.1.4
1882 # (k) to (m)). The chain goes from the certificate right below the
1883 # root down to cert, and ends with the root, which is trusted as is
1884 # so that a non-CA certificate can still be pinned.
1885 intermediates = len(chain) - 2
1886 for i, c in enumerate(chain):
1887 if isinstance(c, Cert) and not allow_expired and not c.isValidAt(now):
1888 raise ValueError(
1889 "Certificate %s is outside its validity period "
1890 "(%s to %s) !" % (c.subject_str, c.notBefore_str, c.notAfter_str)
1891 )
1892 if i < intermediates:
1893 if not c.cA:
1894 raise ValueError(
1895 "Certificate %s is not a CA but issued %s !"
1896 % (c.subject_str, chain[i + 1].subject_str)
1897 )
1898 if (
1899 c.pathLenConstraint is not None
1900 and intermediates - i - 1 > c.pathLenConstraint
1901 ):
1902 raise ValueError(
1903 "Certificate %s allows at most %d CA below it !"
1904 % (c.subject_str, c.pathLenConstraint)
1905 )
1906
1907 # Check hostname
1908 if hostname is not None and not cert.matchesHostname(hostname):
1909 raise ValueError(
1910 "Certificate %s was not issued to %s !"
1911 % (cert.subject_str, plain_str(hostname))
1912 )
1913
1914 # Check ekus
1915 if ekus is not None:
1916 certificate_ekus = [eku.oidname for eku in (cert.extKeyUsage or [])]
1917 for eku in ekus:
1918 if eku.oidname not in certificate_ekus:
1919 raise ValueError(
1920 "Certificate %s does not contain the required EKU %s !"
1921 % (cert.subject_str, eku.oidname)
1922 )
1923
1924 def show(self, ret: bool = False):
1925 """
1926 Return the CertTree as a string certificate tree
1927 """
1928
1929 def _rec_show(c, children, lvl=0):
1930 s = ""
1931 # Process the current CA
1932 if c:
1933 if not c.isSelfSigned():
1934 s += "%s [Not Self Signed]\n" % c.subject_str
1935 else:
1936 s += "%s [Self Signed]\n" % c.subject_str
1937 s = lvl * " " + s
1938 lvl += 1
1939 # Process all sub-CAs at a lower level
1940 for child, subchildren in children:
1941 s += _rec_show(child, subchildren, lvl=lvl)
1942 return s
1943
1944 showed = _rec_show(None, self.tree)
1945 if ret:
1946 return showed
1947 else:
1948 print(showed)
1949
1950 def __repr__(self):
1951 return "<CertTree %s certificates (%s ROOT CA)>" % (
1952 len(self),
1953 len(self.rootCAs),
1954 )
1955
1956
1957#######
1958# CMS #
1959#######
1960
1961# RFC3852
1962
1963
1964class CMS_Engine:
1965 """
1966 A utility class to perform CMS/PKCS7 operations, as specified by RFC3852.
1967
1968 :param store: a ROOT CA certificate list to trust.
1969 :param crls: a list of CRLs to include. This is currently not checked.
1970 """
1971
1972 def __init__(
1973 self,
1974 store: CertList,
1975 crls: List[X509_CRL] = [],
1976 ):
1977 self.store = store
1978 self.crls = crls
1979
1980 def _get_algorithms(self, key: PrivKey, h="sha256") -> ASN1_OID:
1981 """
1982 Get the algorithms matching a private key
1983 """
1984 if isinstance(key, PrivKeyRSA):
1985 # RFC3370 sect 3.2
1986 return (
1987 ASN1_OID("rsaEncryption"),
1988 _get_hash(h),
1989 h,
1990 )
1991 elif isinstance(key, PrivKeyECDSA):
1992 # RFC5753 sect 2.1.1
1993 if h == "sha1":
1994 return (
1995 ASN1_OID("ecdsa-with-SHA1"),
1996 hashes.SHA1(),
1997 "sha1",
1998 )
1999 elif h == "sha224":
2000 return (
2001 ASN1_OID("ecdsa-with-SHA224"),
2002 hashes.SHA224(),
2003 "sha224",
2004 )
2005 elif h == "sha256":
2006 return (
2007 ASN1_OID("ecdsa-with-SHA256"),
2008 hashes.SHA256(),
2009 "sha256",
2010 )
2011 elif h == "sha384":
2012 return (
2013 ASN1_OID("ecdsa-with-SHA384"),
2014 hashes.SHA384(),
2015 "sha384",
2016 )
2017 elif h == "sha512":
2018 return (
2019 ASN1_OID("ecdsa-with-SHA512"),
2020 hashes.SHA512(),
2021 "sha512",
2022 )
2023 else:
2024 raise ValueError("Unknown hash for private key !")
2025 elif isinstance(key, PrivKeyEdDSA):
2026 # RFC8419 sect 2.3
2027 if isinstance(key.key, x25519.X25519PrivateKey):
2028 return (
2029 ASN1_OID("Ed25519"),
2030 hashes.SHA512(),
2031 "sha512",
2032 )
2033 elif isinstance(key.key, x448.X448PrivateKey):
2034 return (
2035 ASN1_OID("Ed448"),
2036 hashes.SHAKE256(64),
2037 "shake256",
2038 )
2039 else:
2040 raise ValueError("Unknown private key type !")
2041
2042 def sign(
2043 self,
2044 message: Union[bytes, Packet],
2045 eContentType: ASN1_OID,
2046 cert: Cert,
2047 key: PrivKey,
2048 dhash: Optional[str] = "sha256",
2049 ):
2050 """
2051 Sign a message using CMS.
2052
2053 :param message: the inner content to sign.
2054 :param eContentType: the OID of the inner content.
2055 :param cert: the certificate whose key to use use for signing.
2056 :param key: the private key to use for signing.
2057 :param dhash: the hash to use for message digest (ECDSA only).
2058
2059 We currently only support X.509 certificates !
2060 """
2061 sigalg, cdhash, dhash = self._get_algorithms(key, h=dhash)
2062
2063 # RFC3852 5.4. Message Digest Calculation Process
2064 hash = hashes.Hash(cdhash)
2065 hash.update(bytes(message))
2066 hashed_message = hash.finalize()
2067
2068 # RFC3852 5.5. Signature Generation Process
2069 signerInfo = CMS_SignerInfo(
2070 version=1,
2071 sid=CMS_IssuerAndSerialNumber(
2072 issuer=cert.tbsCertificate.issuer,
2073 serialNumber=cert.tbsCertificate.serialNumber,
2074 ),
2075 digestAlgorithm=X509_AlgorithmIdentifier(
2076 algorithm=ASN1_OID(dhash),
2077 parameters=ASN1_NULL(0),
2078 ),
2079 signedAttrs=[
2080 X509_Attribute(
2081 type=ASN1_OID("contentType"),
2082 values=[
2083 X509_AttributeValue(value=eContentType),
2084 ],
2085 ),
2086 X509_Attribute(
2087 type=ASN1_OID("messageDigest"),
2088 # "A message-digest attribute MUST have a single attribute value"
2089 values=[
2090 X509_AttributeValue(value=ASN1_STRING(hashed_message)),
2091 ],
2092 ),
2093 ],
2094 signatureAlgorithm=X509_AlgorithmIdentifier(
2095 algorithm=sigalg,
2096 parameters=ASN1_NULL(0),
2097 ),
2098 )
2099 signerInfo.signature = ASN1_STRING(
2100 key.sign(
2101 bytes(
2102 CMS_SignedAttrsForSignature(
2103 signedAttrs=signerInfo.signedAttrs,
2104 )
2105 ),
2106 h=dhash,
2107 )
2108 )
2109
2110 # Build a chain of X509_Cert to ship (but skip the ROOT certificate)
2111 certTree = CertTree(cert, self.store)
2112 certificates = [x.x509Cert for x in certTree if not x.isSelfSigned()]
2113
2114 # Build final structure
2115 return CMS_ContentInfo(
2116 contentType=ASN1_OID("id-signedData"),
2117 content=CMS_SignedData(
2118 version=3 if certificates else 1,
2119 digestAlgorithms=X509_AlgorithmIdentifier(
2120 algorithm=ASN1_OID(dhash),
2121 parameters=ASN1_NULL(0),
2122 ),
2123 encapContentInfo=CMS_EncapsulatedContentInfo(
2124 eContentType=eContentType,
2125 eContent=message,
2126 ),
2127 certificates=(
2128 [CMS_CertificateChoices(certificate=cert) for cert in certificates]
2129 if certificates
2130 else None
2131 ),
2132 crls=(
2133 [CMS_RevocationInfoChoice(crl=crl) for crl in self.crls]
2134 if self.crls
2135 else None
2136 ),
2137 signerInfos=[
2138 signerInfo,
2139 ],
2140 ),
2141 )
2142
2143 def verify(
2144 self,
2145 contentInfo: CMS_ContentInfo,
2146 eContentType: Optional[ASN1_OID] = None,
2147 eContent: Optional[bytes] = None,
2148 no_verify_cert: bool = False,
2149 allow_expired: bool = False,
2150 ekus: Optional[List[ASN1_OID]] = None,
2151 ):
2152 """
2153 Verify a CMS message against the list of trusted certificates,
2154 and return the unpacked message if the verification succeeds.
2155
2156 :param contentInfo: the ContentInfo whose signature to verify
2157 :param eContentType: if provided, verifies that the content type is valid
2158 :param eContent: in PKCS 7.1, provide the content to verify
2159 :param no_verify_cert: do not check the remote certificate (unsafe)
2160 :param allow_expired: accept a signer certificate that is outside its
2161 validity period. A CMS signature is often checked long after it was
2162 made, so an expired signer does not by itself mean the signature is
2163 not genuine.
2164 :param ekus: if provided, verifies that the signer certificate has the
2165 following EKUs.
2166 """
2167 if contentInfo.contentType.oidname != "id-signedData":
2168 raise ValueError("ContentInfo isn't signed !")
2169
2170 signeddata = contentInfo.content
2171
2172 # Build the certificate chain
2173 certificates = []
2174 if signeddata.certificates:
2175 certificates = [Cert(x.certificate) for x in signeddata.certificates]
2176 certTree = CertTree(certificates, self.store)
2177
2178 # Check there's at least one signature
2179 if not signeddata.signerInfos:
2180 raise ValueError("ContentInfo contained no signature !")
2181
2182 # Check all signatures
2183 for signerInfo in signeddata.signerInfos:
2184 # RFC 5652 sect 5.4: the digest is the one named by
2185 # digestAlgorithm, not by signatureAlgorithm.
2186 sigh = signerInfo.digestAlgorithm.algorithm.oidname
2187 if sigh not in _tls_hash_algs:
2188 sigh = hash_by_oid[signerInfo.signatureAlgorithm.algorithm.val]
2189
2190 # Find certificate in the chain that did this
2191 cert: Cert = certTree.findCertBySid(signerInfo.sid)
2192
2193 # Verify certificate signature
2194 if not no_verify_cert:
2195 certTree.verify(cert, allow_expired=allow_expired, ekus=ekus)
2196
2197 # Verify the message hash
2198 if signerInfo.signedAttrs:
2199 # Verify the contentType
2200 try:
2201 contentType = next(
2202 x.values[0].value
2203 for x in signerInfo.signedAttrs
2204 if x.type.oidname == "contentType"
2205 )
2206
2207 if contentType != signeddata.encapContentInfo.eContentType:
2208 raise ValueError(
2209 "Inconsistent 'contentType' was detected in packet !"
2210 )
2211
2212 if eContentType is not None and eContentType != contentType:
2213 raise ValueError(
2214 "Expected '%s' but got '%s' contentType !"
2215 % (
2216 eContentType,
2217 contentType,
2218 )
2219 )
2220 except StopIteration:
2221 raise ValueError("Missing contentType in signedAttrs !")
2222
2223 # Verify the messageDigest value
2224 try:
2225 # "A message-digest attribute MUST have a single attribute value"
2226 messageDigest = next(
2227 x.values[0].value
2228 for x in signerInfo.signedAttrs
2229 if x.type.oidname == "messageDigest"
2230 )
2231
2232 if signeddata.encapContentInfo.eContent is not None:
2233 eContent = bytes(signeddata.encapContentInfo.eContent)
2234 elif eContent is None:
2235 raise ValueError("No eContent was provided !")
2236
2237 # Re-calculate hash
2238 h = signerInfo.digestAlgorithm.algorithm.oidname
2239 hash = hashes.Hash(_get_hash(h))
2240 hash.update(eContent)
2241 hashed_message = hash.finalize()
2242
2243 if hashed_message != messageDigest:
2244 raise ValueError("Invalid messageDigest value !")
2245 except StopIteration:
2246 raise ValueError("Missing messageDigest in signedAttrs !")
2247
2248 # Verify the signature
2249 if not cert.verify(
2250 msg=bytes(
2251 CMS_SignedAttrsForSignature(
2252 signedAttrs=signerInfo.signedAttrs,
2253 )
2254 ),
2255 sig=signerInfo.signature.val,
2256 h=sigh,
2257 ):
2258 raise ValueError("Invalid signature !")
2259 else:
2260 if not cert.verify(
2261 msg=bytes(signeddata.encapContentInfo),
2262 sig=signerInfo.signature.val,
2263 h=sigh,
2264 ):
2265 raise ValueError("Invalid signature !")
2266
2267 # Return the content
2268 return signeddata.encapContentInfo.eContent