Skip to content

Commit db95cf5

Browse files
committed
fix: adapted docmentation
fix: passed into Cow again coverage: added one test case
1 parent 1e92f20 commit db95cf5

File tree

3 files changed

+109
-33
lines changed

3 files changed

+109
-33
lines changed

docs/hazmat/primitives/asymmetric/serialization.rst

Lines changed: 105 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,63 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
11761176
-----END PRIVATE KEY-----
11771177
""".strip()
11781178

1179+
verify_cert = b"""
1180+
-----BEGIN CERTIFICATE-----
1181+
MIID9zCCAt+gAwIBAgIQIxMA+XhyS9Ou0qAc0zPyVTANBgkqhkiG9w0BAQsFADAN
1182+
MQswCQYDVQQDDAJDQTAeFw0yNTAxMDUxMDQ4MjhaFw0yNjAxMDUxMDQ4MjhaMCUx
1183+
IzAhBgkqhkiG9w0BCQEWFGRlbW8xQHRyaXNvZnQuY29tLnBsMIIBIjANBgkqhkiG
1184+
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt0WRzh5y+QmEUjCm+iHXZLrstOSSEhiEcUre
1185+
3L8zkuGYVLCKBEvmaHQI7uCu/xdqEht6/wEBCiK+KLdGDVrD4v3A7TnmHzzhvqCs
1186+
BTL/EmnD3ZMAJVYv4uEBaFpFPSYnPswd353E6KRkFYR4RmFjG9xLTayHXOKqCF6d
1187+
Hd3uVR7NSs98uhcSYRV7g4NdjmaDj8kz5HeRMfr/uqbcriJ9tu/ljFBWYSwPeiNY
1188+
nYhaOBLpUhZckyjFDfC+UpwOBPlkK7J047urvzG21xCtVU9DMHtXMkXYe/C+WSm1
1189+
MRYtgcsOTxpGf+ujceltI2/+IUhWxr5ys7m+xM1jYaM4O1Pw0QIDAQABo4IBOTCC
1190+
ATUwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBQduUy7zqv6z3uk4fJeifohSntD
1191+
2TAtBgNVHR8EJjAkMCKgIKAehhxodHRwOi8vY2EudHJpc29mdC5jb20ucGwvY3Js
1192+
MGYGCCsGAQUFBwEBBFowWDArBggrBgEFBQcwAoYfaHR0cDovL2NhLnRyaXNvZnQu
1193+
Y29tLnBsL2NhY2VydDApBggrBgEFBQcwAYYdaHR0cDovL2NhLnRyaXNvZnQuY29t
1194+
LnBsL29jc3AwHwYDVR0RBBgwFoEUZGVtbzFAdHJpc29mdC5jb20ucGwwHQYDVR0l
1195+
BBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMEMB0GA1UdDgQWBBT0/QFDFX/CCMsX356G
1196+
ImiWwPYxjDAOBgNVHQ8BAf8EBAMCA+gwDQYJKoZIhvcNAQELBQADggEBAL3Iisca
1197+
IqoFBLMox3cIhCANWO/U1eOvjDjfM/tOHn+6jci/pL/ZHgdRtqCCiaCKtJED/f/9
1198+
NFUKqcSZ9+vzW0RWLJxHgIvCSjLpoM06XClSlxjVnv62Hb1NC4FfDfnzyG+DZHus
1199+
nz/MQuXNwHntA6+JyB/HWHUie2ierQYH2mEN1XIJm5luSGwtuGaWfNz/w324ukcV
1200+
pMd3CbEOZqqfSYGWUHOVG90/OMSfKA/I0hia8Yij0X4Ny+b+bLnHaoozZwJ/UqBl
1201+
9ptbfiOOuFXJP7gt547Rp6+2C0XGJM+le0EYlUzbWE6UWgxaIRp5uc8HnUd5e4lX
1202+
br+Ixxcl3WHckkk=
1203+
-----END CERTIFICATE-----
1204+
""".strip()
1205+
1206+
verify_key = b"""
1207+
-----BEGIN RSA PRIVATE KEY-----
1208+
MIIEowIBAAKCAQEAt0WRzh5y+QmEUjCm+iHXZLrstOSSEhiEcUre3L8zkuGYVLCK
1209+
BEvmaHQI7uCu/xdqEht6/wEBCiK+KLdGDVrD4v3A7TnmHzzhvqCsBTL/EmnD3ZMA
1210+
JVYv4uEBaFpFPSYnPswd353E6KRkFYR4RmFjG9xLTayHXOKqCF6dHd3uVR7NSs98
1211+
uhcSYRV7g4NdjmaDj8kz5HeRMfr/uqbcriJ9tu/ljFBWYSwPeiNYnYhaOBLpUhZc
1212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cJNNw2DDrfUPD9
1228+
9eI0rL2cNJUKWRX+Wbz183uRseRGWHJ4u+vpqNcPe8hF1th21EP4HBpAvwcLIXT8
1229+
IuszEkjMavdDHR+OlifsZKfEa07C9Vg2MAG3NnzLITopiMcw8rgN0n2uBVcsT4fV
1230+
i2DhAoGBAIJtHUe9e8oPrasRlZ3bTFmDT+jNg+7RB8ebG8ZDqAUI3/gnklUd0+rF
1231+
nPGI8GEpjwgBxB/zg4/rYz/TEP0E2pd0beWH2vKD31kQVngbz/zhzLHCNLyKDlB4
1232+
vFHpXRHb7ddgTLjHbg6GvY/pRRCqSxWnLgNRW4m+pyLzAx/Hpk1D
1233+
-----END RSA PRIVATE KEY-----
1234+
""".strip()
1235+
11791236
.. class:: PKCS7SignatureBuilder
11801237

11811238
The PKCS7 signature builder can create both basic PKCS7 signed messages as
@@ -1254,7 +1311,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
12541311
:returns bytes: The signed PKCS7 message.
12551312

12561313

1257-
.. function:: pkcs7_verify_der(data, content, certificate, options)
1314+
.. function:: pkcs7_verify_der(data, content=None, certificate=None, options=None)
12581315

12591316
.. versionadded:: 45.0.0
12601317

@@ -1263,16 +1320,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
12631320
>>> from cryptography import x509
12641321
>>> from cryptography.hazmat.primitives import hashes, serialization
12651322
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1266-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1267-
>>> key = serialization.load_pem_private_key(ca_key, None)
1323+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1324+
>>> key = serialization.load_pem_private_key(verify_key, None)
12681325
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
12691326
... b"data to sign"
12701327
... ).add_signer(
12711328
... cert, key, hashes.SHA256()
12721329
... ).sign(
12731330
... serialization.Encoding.DER, []
12741331
... )
1275-
>>> pkcs7.pkcs7_verify_der(signed, None, cert, [])
1332+
>>> pkcs7.pkcs7_verify_der(signed)
12761333

12771334
Deserialize and verify a DER-encoded PKCS7 signed message. PKCS7 (or S/MIME) has multiple
12781335
versions, but this supports a subset of :rfc:`5751`, also known as S/MIME Version 3.2. If the
@@ -1282,15 +1339,21 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
12821339
:type data: bytes
12831340

12841341
:param content: if specified, the content to verify against the signed message. If the content
1285-
is not specified, the function will look for the content in the signed message.
1286-
:type data: bytes or None
1342+
is not specified, the function will look for the content in the signed message. Defaults to
1343+
None.
1344+
:type content: bytes or None
12871345

1288-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1289-
message.
1346+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1347+
the signed message. If None, the function will look for the signer certificate in the signed
1348+
message. Defaults to None.
1349+
:type certificate: :class:`~cryptography.x509.Certificate` or None
12901350

12911351
:param options: A list of
1292-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1293-
this operation, no options are supported as of now.
1352+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1353+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1354+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1355+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1356+
:type options: list[`~cryptography.x509.Certificate`] or None
12941357

12951358
:raises ValueError: If the recipient certificate does not match any of the signers in the
12961359
PKCS7 data.
@@ -1300,7 +1363,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13001363
:raises ValueError: If the PKCS7 data is not of the signed data type.
13011364

13021365

1303-
.. function:: pkcs7_verify_pem(data, content, certificate, options)
1366+
.. function:: pkcs7_verify_pem(data, content=None, certificate=None, options=None)
13041367

13051368
.. versionadded:: 45.0.0
13061369

@@ -1309,16 +1372,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13091372
>>> from cryptography import x509
13101373
>>> from cryptography.hazmat.primitives import hashes, serialization
13111374
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1312-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1313-
>>> key = serialization.load_pem_private_key(ca_key, None)
1375+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1376+
>>> key = serialization.load_pem_private_key(verify_key, None)
13141377
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
13151378
... b"data to sign"
13161379
... ).add_signer(
13171380
... cert, key, hashes.SHA256()
13181381
... ).sign(
13191382
... serialization.Encoding.PEM, []
13201383
... )
1321-
>>> pkcs7.pkcs7_verify_pem(signed, None, cert, [])
1384+
>>> pkcs7.pkcs7_verify_pem(signed)
13221385

13231386
Deserialize and verify a PEM-encoded PKCS7 signed message. PKCS7 (or S/MIME) has multiple
13241387
versions, but this supports a subset of :rfc:`5751`, also known as S/MIME Version 3.2. If the
@@ -1328,15 +1391,21 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13281391
:type data: bytes
13291392

13301393
:param content: if specified, the content to verify against the signed message. If the content
1331-
is not specified, the function will look for the content in the signed message.
1332-
:type data: bytes or None
1394+
is not specified, the function will look for the content in the signed message. Defaults to
1395+
None.
1396+
:type content: bytes or None
13331397

1334-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1335-
message.
1398+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1399+
the signed message. If None, the function will look for the signer certificate in the signed
1400+
message. Defaults to None.
1401+
:type certificate: :class:`~cryptography.x509.Certificate` or None
13361402

13371403
:param options: A list of
1338-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1339-
this operation, no options are supported as of now.
1404+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1405+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1406+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1407+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1408+
:type options: list[`~cryptography.x509.Certificate`] or None
13401409

13411410
:raises ValueError: If the PEM data does not have the PKCS7 tag.
13421411

@@ -1348,7 +1417,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13481417
:raises ValueError: If the PKCS7 data is not of the signed data type.
13491418

13501419

1351-
.. function:: pkcs7_verify_smime(data, content, certificate, options)
1420+
.. function:: pkcs7_verify_smime(data, content=None, certificate=None, options=None)
13521421

13531422
.. versionadded:: 45.0.0
13541423

@@ -1357,16 +1426,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13571426
>>> from cryptography import x509
13581427
>>> from cryptography.hazmat.primitives import hashes, serialization
13591428
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1360-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1361-
>>> key = serialization.load_pem_private_key(ca_key, None)
1429+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1430+
>>> key = serialization.load_pem_private_key(verify_key, None)
13621431
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
13631432
... b"data to sign"
13641433
... ).add_signer(
13651434
... cert, key, hashes.SHA256()
13661435
... ).sign(
13671436
... serialization.Encoding.SMIME, []
13681437
... )
1369-
>>> pkcs7.pkcs7_verify_smime(signed, None, cert, [])
1438+
>>> pkcs7.pkcs7_verify_smime(signed)
13701439

13711440
Verify a PKCS7 signed message stored in a MIME message, by reading it, extracting the content
13721441
(if any) and signature, deserializing the signature and verifying it against the content. PKCS7
@@ -1379,15 +1448,21 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13791448

13801449
:param content: if specified, the content to verify against the signed message. If the content
13811450
is not specified, the function will look for the content in the MIME message and in the
1382-
signature.
1383-
:type data: bytes or None
1451+
signature. Defaults to None.
1452+
:type content: bytes or None
13841453

1385-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1386-
message.
1454+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1455+
the signed message. If None, the function will look for the signer certificate in the signed
1456+
message. Defaults to None.
1457+
:type certificate: :class:`~cryptography.x509.Certificate` or None
13871458

13881459
:param options: A list of
1389-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1390-
this operation, no options are supported as of now.
1460+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1461+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1462+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1463+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1464+
:type options: list[`~cryptography.x509.Certificate`] or None
1465+
13911466

13921467
:raises ValueError: If the MIME message is not a S/MIME signed message: content type is
13931468
different than ``multipart/signed`` or ``application/pkcs7-mime``.

src/rust/src/pkcs7.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,10 @@ fn verify_der<'p>(
740740
) -> CryptographyResult<()> {
741741
// Check the verify options
742742
let options = match options {
743-
Some(options) => options,
744-
None => &pyo3::types::PyList::empty(py),
743+
Some(options) => Cow::Borrowed(options),
744+
None => Cow::Owned(pyo3::types::PyList::empty(py)),
745745
};
746-
check_verify_options(py, options)?;
746+
check_verify_options(py, &options)?;
747747

748748
// Verify the data
749749
let content_info = asn1::parse_single::<pkcs7::ContentInfo<'_>>(signature)?;

tests/hazmat/primitives/test_pkcs7.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ def test_not_a_cert(self, backend):
887887
"invalid_options",
888888
[
889889
[b"invalid"],
890+
[pkcs7.PKCS7Options.Binary],
890891
],
891892
)
892893
def test_pkcs7_verify_invalid_options(self, backend, invalid_options):

0 commit comments

Comments
 (0)