|
2 | 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository
|
3 | 3 | # for complete details.
|
4 | 4 |
|
5 |
| - |
| 5 | +import typing |
6 | 6 | from enum import Enum
|
7 | 7 |
|
8 | 8 | from cryptography import x509
|
|
12 | 12 | from cryptography.utils import _check_byteslike
|
13 | 13 |
|
14 | 14 |
|
15 |
| -def load_pem_pkcs7_certificates(data): |
| 15 | +def load_pem_pkcs7_certificates(data: bytes) -> typing.List[x509.Certificate]: |
16 | 16 | backend = _get_backend(None)
|
17 | 17 | return backend.load_pem_pkcs7_certificates(data)
|
18 | 18 |
|
19 | 19 |
|
20 |
| -def load_der_pkcs7_certificates(data): |
| 20 | +def load_der_pkcs7_certificates(data: bytes) -> typing.List[x509.Certificate]: |
21 | 21 | backend = _get_backend(None)
|
22 | 22 | return backend.load_der_pkcs7_certificates(data)
|
23 | 23 |
|
24 | 24 |
|
| 25 | +_ALLOWED_PKCS7_HASH_TYPES = typing.Union[ |
| 26 | + hashes.SHA1, |
| 27 | + hashes.SHA224, |
| 28 | + hashes.SHA256, |
| 29 | + hashes.SHA384, |
| 30 | + hashes.SHA512, |
| 31 | +] |
| 32 | + |
| 33 | +_ALLOWED_PRIVATE_KEY_TYPES = typing.Union[ |
| 34 | + rsa.RSAPrivateKey, ec.EllipticCurvePrivateKey |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +class PKCS7Options(Enum): |
| 39 | + Text = "Add text/plain MIME type" |
| 40 | + Binary = "Don't translate input data into canonical MIME format" |
| 41 | + DetachedSignature = "Don't embed data in the PKCS7 structure" |
| 42 | + NoCapabilities = "Don't embed SMIME capabilities" |
| 43 | + NoAttributes = "Don't embed authenticatedAttributes" |
| 44 | + NoCerts = "Don't embed signer certificate" |
| 45 | + |
| 46 | + |
25 | 47 | class PKCS7SignatureBuilder(object):
|
26 | 48 | def __init__(self, data=None, signers=[], additional_certs=[]):
|
27 | 49 | self._data = data
|
28 | 50 | self._signers = signers
|
29 | 51 | self._additional_certs = additional_certs
|
30 | 52 |
|
31 |
| - def set_data(self, data): |
| 53 | + def set_data(self, data: bytes) -> "PKCS7SignatureBuilder": |
32 | 54 | _check_byteslike("data", data)
|
33 | 55 | if self._data is not None:
|
34 | 56 | raise ValueError("data may only be set once")
|
35 | 57 |
|
36 | 58 | return PKCS7SignatureBuilder(data, self._signers)
|
37 | 59 |
|
38 |
| - def add_signer(self, certificate, private_key, hash_algorithm): |
| 60 | + def add_signer( |
| 61 | + self, |
| 62 | + certificate: x509.Certificate, |
| 63 | + private_key: _ALLOWED_PRIVATE_KEY_TYPES, |
| 64 | + hash_algorithm: _ALLOWED_PKCS7_HASH_TYPES, |
| 65 | + ) -> "PKCS7SignatureBuilder": |
39 | 66 | if not isinstance(
|
40 | 67 | hash_algorithm,
|
41 | 68 | (
|
@@ -63,15 +90,22 @@ def add_signer(self, certificate, private_key, hash_algorithm):
|
63 | 90 | self._signers + [(certificate, private_key, hash_algorithm)],
|
64 | 91 | )
|
65 | 92 |
|
66 |
| - def add_certificate(self, certificate): |
| 93 | + def add_certificate( |
| 94 | + self, certificate: x509.Certificate |
| 95 | + ) -> "PKCS7SignatureBuilder": |
67 | 96 | if not isinstance(certificate, x509.Certificate):
|
68 | 97 | raise TypeError("certificate must be a x509.Certificate")
|
69 | 98 |
|
70 | 99 | return PKCS7SignatureBuilder(
|
71 | 100 | self._data, self._signers, self._additional_certs + [certificate]
|
72 | 101 | )
|
73 | 102 |
|
74 |
| - def sign(self, encoding, options, backend=None): |
| 103 | + def sign( |
| 104 | + self, |
| 105 | + encoding: serialization.Encoding, |
| 106 | + options: typing.Iterable[PKCS7Options], |
| 107 | + backend=None, |
| 108 | + ) -> bytes: |
75 | 109 | if len(self._signers) == 0:
|
76 | 110 | raise ValueError("Must have at least one signer")
|
77 | 111 | if self._data is None:
|
@@ -120,12 +154,3 @@ def sign(self, encoding, options, backend=None):
|
120 | 154 |
|
121 | 155 | backend = _get_backend(backend)
|
122 | 156 | return backend.pkcs7_sign(self, encoding, options)
|
123 |
| - |
124 |
| - |
125 |
| -class PKCS7Options(Enum): |
126 |
| - Text = "Add text/plain MIME type" |
127 |
| - Binary = "Don't translate input data into canonical MIME format" |
128 |
| - DetachedSignature = "Don't embed data in the PKCS7 structure" |
129 |
| - NoCapabilities = "Don't embed SMIME capabilities" |
130 |
| - NoAttributes = "Don't embed authenticatedAttributes" |
131 |
| - NoCerts = "Don't embed signer certificate" |
|
0 commit comments