Skip to content

Commit d47add4

Browse files
committed
PKCS7SignatureBuilder now supports new option NoCerts when signing
1 parent 95c4f68 commit d47add4

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

docs/hazmat/primitives/asymmetric/serialization.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,13 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
699699
pass ``NoAttributes`` you can't pass ``NoCapabilities`` since
700700
``NoAttributes`` removes ``MIMECapabilities`` and more.
701701

702+
.. attribute:: NoCerts
703+
704+
Don't include the signer's certificate in the PKCS7 structure. This can
705+
reduce the size of the signature but requires that the recipient can
706+
obtain the signer's certificate by other means (for example from a
707+
previously signed message).
708+
702709
Serialization Formats
703710
~~~~~~~~~~~~~~~~~~~~~
704711

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,10 @@ def pkcs7_sign(self, builder, encoding, options):
27192719
signer_flags |= self._lib.PKCS7_NOSMIMECAP
27202720
elif pkcs7.PKCS7Options.NoAttributes in options:
27212721
signer_flags |= self._lib.PKCS7_NOATTR
2722+
2723+
if pkcs7.PKCS7Options.NoCerts in options:
2724+
signer_flags |= self._lib.PKCS7_NOCERTS
2725+
27222726
for certificate, private_key, hash_algorithm in builder._signers:
27232727
md = self._evp_md_non_null_from_algorithm(hash_algorithm)
27242728
p7signerinfo = self._lib.PKCS7_sign_add_signer(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@ class PKCS7Options(Enum):
120120
DetachedSignature = "Don't embed data in the PKCS7 structure"
121121
NoCapabilities = "Don't embed SMIME capabilities"
122122
NoAttributes = "Don't embed authenticatedAttributes"
123+
NoCerts = "Don't embed signer certificate"

tests/hazmat/primitives/test_pkcs7.py

Lines changed: 17 additions & 0 deletions
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)