Skip to content

Commit 611c4a3

Browse files
authored
PKCS7SignatureBuilder now supports new option NoCerts when signing (#5500)
1 parent 836a92a commit 611c4a3

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

docs/hazmat/primitives/asymmetric/serialization.rst

+7
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,13 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
707707
pass ``NoAttributes`` you can't pass ``NoCapabilities`` since
708708
``NoAttributes`` removes ``MIMECapabilities`` and more.
709709

710+
.. attribute:: NoCerts
711+
712+
Don't include the signer's certificate in the PKCS7 structure. This can
713+
reduce the size of the signature but requires that the recipient can
714+
obtain the signer's certificate by other means (for example from a
715+
previously signed message).
716+
710717
Serialization Formats
711718
~~~~~~~~~~~~~~~~~~~~~
712719

src/cryptography/hazmat/backends/openssl/backend.py

+4
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,10 @@ def pkcs7_sign(self, builder, encoding, options):
27282728
signer_flags |= self._lib.PKCS7_NOSMIMECAP
27292729
elif pkcs7.PKCS7Options.NoAttributes in options:
27302730
signer_flags |= self._lib.PKCS7_NOATTR
2731+
2732+
if pkcs7.PKCS7Options.NoCerts in options:
2733+
signer_flags |= self._lib.PKCS7_NOCERTS
2734+
27312735
for certificate, private_key, hash_algorithm in builder._signers:
27322736
md = self._evp_md_non_null_from_algorithm(hash_algorithm)
27332737
p7signerinfo = self._lib.PKCS7_sign_add_signer(

src/cryptography/hazmat/primitives/serialization/pkcs7.py

+1
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ class PKCS7Options(Enum):
129129
DetachedSignature = "Don't embed data in the PKCS7 structure"
130130
NoCapabilities = "Don't embed SMIME capabilities"
131131
NoAttributes = "Don't embed authenticatedAttributes"
132+
NoCerts = "Don't embed signer certificate"

tests/hazmat/primitives/test_pkcs7.py

+17
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,23 @@ def test_sign_no_attributes(self, backend):
535535
backend,
536536
)
537537

538+
def test_sign_no_certs(self, backend):
539+
data = b"hello world"
540+
cert, key = _load_cert_key()
541+
builder = (
542+
pkcs7.PKCS7SignatureBuilder()
543+
.set_data(data)
544+
.add_signer(cert, key, hashes.SHA256())
545+
)
546+
547+
options = []
548+
sig = builder.sign(serialization.Encoding.DER, options)
549+
assert sig.count(cert.public_bytes(serialization.Encoding.DER)) == 1
550+
551+
options = [pkcs7.PKCS7Options.NoCerts]
552+
sig_no = builder.sign(serialization.Encoding.DER, options)
553+
assert sig_no.count(cert.public_bytes(serialization.Encoding.DER)) == 0
554+
538555
def test_multiple_signers(self, backend):
539556
data = b"hello world"
540557
cert, key = _load_cert_key()

0 commit comments

Comments
 (0)