summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrennkie <mail@rhab.de>2020-10-25 15:50:18 +0100
committerGitHub <noreply@github.com>2020-10-25 07:50:18 -0700
commit611c4a340f6c53a7e28a9695a3248bd4e9f8558d (patch)
tree2ad99e789ce065d87368e48634ca0f65debb3621
parent836a92a28fbe9df8c37121e340b91ed9cd519ddd (diff)
downloadcryptography-611c4a340f6c53a7e28a9695a3248bd4e9f8558d.tar.gz
PKCS7SignatureBuilder now supports new option NoCerts when signing (#5500)
-rw-r--r--docs/hazmat/primitives/asymmetric/serialization.rst7
-rw-r--r--src/cryptography/hazmat/backends/openssl/backend.py4
-rw-r--r--src/cryptography/hazmat/primitives/serialization/pkcs7.py1
-rw-r--r--tests/hazmat/primitives/test_pkcs7.py17
4 files changed, 29 insertions, 0 deletions
diff --git a/docs/hazmat/primitives/asymmetric/serialization.rst b/docs/hazmat/primitives/asymmetric/serialization.rst
index bb278d8e1..6b2e858db 100644
--- a/docs/hazmat/primitives/asymmetric/serialization.rst
+++ b/docs/hazmat/primitives/asymmetric/serialization.rst
@@ -707,6 +707,13 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
pass ``NoAttributes`` you can't pass ``NoCapabilities`` since
``NoAttributes`` removes ``MIMECapabilities`` and more.
+ .. attribute:: NoCerts
+
+ Don't include the signer's certificate in the PKCS7 structure. This can
+ reduce the size of the signature but requires that the recipient can
+ obtain the signer's certificate by other means (for example from a
+ previously signed message).
+
Serialization Formats
~~~~~~~~~~~~~~~~~~~~~
diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py
index 76600dc08..d948de228 100644
--- a/src/cryptography/hazmat/backends/openssl/backend.py
+++ b/src/cryptography/hazmat/backends/openssl/backend.py
@@ -2728,6 +2728,10 @@ class Backend(object):
signer_flags |= self._lib.PKCS7_NOSMIMECAP
elif pkcs7.PKCS7Options.NoAttributes in options:
signer_flags |= self._lib.PKCS7_NOATTR
+
+ if pkcs7.PKCS7Options.NoCerts in options:
+ signer_flags |= self._lib.PKCS7_NOCERTS
+
for certificate, private_key, hash_algorithm in builder._signers:
md = self._evp_md_non_null_from_algorithm(hash_algorithm)
p7signerinfo = self._lib.PKCS7_sign_add_signer(
diff --git a/src/cryptography/hazmat/primitives/serialization/pkcs7.py b/src/cryptography/hazmat/primitives/serialization/pkcs7.py
index 275d77083..1e11e28ef 100644
--- a/src/cryptography/hazmat/primitives/serialization/pkcs7.py
+++ b/src/cryptography/hazmat/primitives/serialization/pkcs7.py
@@ -129,3 +129,4 @@ class PKCS7Options(Enum):
DetachedSignature = "Don't embed data in the PKCS7 structure"
NoCapabilities = "Don't embed SMIME capabilities"
NoAttributes = "Don't embed authenticatedAttributes"
+ NoCerts = "Don't embed signer certificate"
diff --git a/tests/hazmat/primitives/test_pkcs7.py b/tests/hazmat/primitives/test_pkcs7.py
index 03a928352..8b93cb633 100644
--- a/tests/hazmat/primitives/test_pkcs7.py
+++ b/tests/hazmat/primitives/test_pkcs7.py
@@ -535,6 +535,23 @@ class TestPKCS7Builder(object):
backend,
)
+ def test_sign_no_certs(self, backend):
+ data = b"hello world"
+ cert, key = _load_cert_key()
+ builder = (
+ pkcs7.PKCS7SignatureBuilder()
+ .set_data(data)
+ .add_signer(cert, key, hashes.SHA256())
+ )
+
+ options = []
+ sig = builder.sign(serialization.Encoding.DER, options)
+ assert sig.count(cert.public_bytes(serialization.Encoding.DER)) == 1
+
+ options = [pkcs7.PKCS7Options.NoCerts]
+ sig_no = builder.sign(serialization.Encoding.DER, options)
+ assert sig_no.count(cert.public_bytes(serialization.Encoding.DER)) == 0
+
def test_multiple_signers(self, backend):
data = b"hello world"
cert, key = _load_cert_key()