Skip to content

Commit 130d6f6

Browse files
committed
fix: adapted docmentation
fix: passed into Cow again coverage: added one test case
1 parent 62c3c13 commit 130d6f6

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
@@ -1262,6 +1262,63 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
12621262
-----END PRIVATE KEY-----
12631263
""".strip()
12641264

1265+
verify_cert = b"""
1266+
-----BEGIN CERTIFICATE-----
1267+
MIID9zCCAt+gAwIBAgIQIxMA+XhyS9Ou0qAc0zPyVTANBgkqhkiG9w0BAQsFADAN
1268+
MQswCQYDVQQDDAJDQTAeFw0yNTAxMDUxMDQ4MjhaFw0yNjAxMDUxMDQ4MjhaMCUx
1269+
IzAhBgkqhkiG9w0BCQEWFGRlbW8xQHRyaXNvZnQuY29tLnBsMIIBIjANBgkqhkiG
1270+
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt0WRzh5y+QmEUjCm+iHXZLrstOSSEhiEcUre
1271+
3L8zkuGYVLCKBEvmaHQI7uCu/xdqEht6/wEBCiK+KLdGDVrD4v3A7TnmHzzhvqCs
1272+
BTL/EmnD3ZMAJVYv4uEBaFpFPSYnPswd353E6KRkFYR4RmFjG9xLTayHXOKqCF6d
1273+
Hd3uVR7NSs98uhcSYRV7g4NdjmaDj8kz5HeRMfr/uqbcriJ9tu/ljFBWYSwPeiNY
1274+
nYhaOBLpUhZckyjFDfC+UpwOBPlkK7J047urvzG21xCtVU9DMHtXMkXYe/C+WSm1
1275+
MRYtgcsOTxpGf+ujceltI2/+IUhWxr5ys7m+xM1jYaM4O1Pw0QIDAQABo4IBOTCC
1276+
ATUwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBQduUy7zqv6z3uk4fJeifohSntD
1277+
2TAtBgNVHR8EJjAkMCKgIKAehhxodHRwOi8vY2EudHJpc29mdC5jb20ucGwvY3Js
1278+
MGYGCCsGAQUFBwEBBFowWDArBggrBgEFBQcwAoYfaHR0cDovL2NhLnRyaXNvZnQu
1279+
Y29tLnBsL2NhY2VydDApBggrBgEFBQcwAYYdaHR0cDovL2NhLnRyaXNvZnQuY29t
1280+
LnBsL29jc3AwHwYDVR0RBBgwFoEUZGVtbzFAdHJpc29mdC5jb20ucGwwHQYDVR0l
1281+
BBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMEMB0GA1UdDgQWBBT0/QFDFX/CCMsX356G
1282+
ImiWwPYxjDAOBgNVHQ8BAf8EBAMCA+gwDQYJKoZIhvcNAQELBQADggEBAL3Iisca
1283+
IqoFBLMox3cIhCANWO/U1eOvjDjfM/tOHn+6jci/pL/ZHgdRtqCCiaCKtJED/f/9
1284+
NFUKqcSZ9+vzW0RWLJxHgIvCSjLpoM06XClSlxjVnv62Hb1NC4FfDfnzyG+DZHus
1285+
nz/MQuXNwHntA6+JyB/HWHUie2ierQYH2mEN1XIJm5luSGwtuGaWfNz/w324ukcV
1286+
pMd3CbEOZqqfSYGWUHOVG90/OMSfKA/I0hia8Yij0X4Ny+b+bLnHaoozZwJ/UqBl
1287+
9ptbfiOOuFXJP7gt547Rp6+2C0XGJM+le0EYlUzbWE6UWgxaIRp5uc8HnUd5e4lX
1288+
br+Ixxcl3WHckkk=
1289+
-----END CERTIFICATE-----
1290+
""".strip()
1291+
1292+
verify_key = b"""
1293+
-----BEGIN RSA PRIVATE KEY-----
1294+
MIIEowIBAAKCAQEAt0WRzh5y+QmEUjCm+iHXZLrstOSSEhiEcUre3L8zkuGYVLCK
1295+
BEvmaHQI7uCu/xdqEht6/wEBCiK+KLdGDVrD4v3A7TnmHzzhvqCsBTL/EmnD3ZMA
1296+
JVYv4uEBaFpFPSYnPswd353E6KRkFYR4RmFjG9xLTayHXOKqCF6dHd3uVR7NSs98
1297+
uhcSYRV7g4NdjmaDj8kz5HeRMfr/uqbcriJ9tu/ljFBWYSwPeiNYnYhaOBLpUhZc
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ptFcJNNw2DDrfUPD9
1314+
9eI0rL2cNJUKWRX+Wbz183uRseRGWHJ4u+vpqNcPe8hF1th21EP4HBpAvwcLIXT8
1315+
IuszEkjMavdDHR+OlifsZKfEa07C9Vg2MAG3NnzLITopiMcw8rgN0n2uBVcsT4fV
1316+
i2DhAoGBAIJtHUe9e8oPrasRlZ3bTFmDT+jNg+7RB8ebG8ZDqAUI3/gnklUd0+rF
1317+
nPGI8GEpjwgBxB/zg4/rYz/TEP0E2pd0beWH2vKD31kQVngbz/zhzLHCNLyKDlB4
1318+
vFHpXRHb7ddgTLjHbg6GvY/pRRCqSxWnLgNRW4m+pyLzAx/Hpk1D
1319+
-----END RSA PRIVATE KEY-----
1320+
""".strip()
1321+
12651322
.. class:: PKCS7SignatureBuilder
12661323

12671324
The PKCS7 signature builder can create both basic PKCS7 signed messages as
@@ -1340,7 +1397,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13401397
:returns bytes: The signed PKCS7 message.
13411398

13421399

1343-
.. function:: pkcs7_verify_der(data, content, certificate, options)
1400+
.. function:: pkcs7_verify_der(data, content=None, certificate=None, options=None)
13441401

13451402
.. versionadded:: 45.0.0
13461403

@@ -1349,16 +1406,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13491406
>>> from cryptography import x509
13501407
>>> from cryptography.hazmat.primitives import hashes, serialization
13511408
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1352-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1353-
>>> key = serialization.load_pem_private_key(ca_key, None)
1409+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1410+
>>> key = serialization.load_pem_private_key(verify_key, None)
13541411
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
13551412
... b"data to sign"
13561413
... ).add_signer(
13571414
... cert, key, hashes.SHA256()
13581415
... ).sign(
13591416
... serialization.Encoding.DER, []
13601417
... )
1361-
>>> pkcs7.pkcs7_verify_der(signed, None, cert, [])
1418+
>>> pkcs7.pkcs7_verify_der(signed)
13621419

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

13701427
:param content: if specified, the content to verify against the signed message. If the content
1371-
is not specified, the function will look for the content in the signed message.
1372-
:type data: bytes or None
1428+
is not specified, the function will look for the content in the signed message. Defaults to
1429+
None.
1430+
:type content: bytes or None
13731431

1374-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1375-
message.
1432+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1433+
the signed message. If None, the function will look for the signer certificate in the signed
1434+
message. Defaults to None.
1435+
:type certificate: :class:`~cryptography.x509.Certificate` or None
13761436

13771437
:param options: A list of
1378-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1379-
this operation, no options are supported as of now.
1438+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1439+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1440+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1441+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1442+
:type options: list[`~cryptography.x509.Certificate`] or None
13801443

13811444
:raises ValueError: If the recipient certificate does not match any of the signers in the
13821445
PKCS7 data.
@@ -1386,7 +1449,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13861449
:raises ValueError: If the PKCS7 data is not of the signed data type.
13871450

13881451

1389-
.. function:: pkcs7_verify_pem(data, content, certificate, options)
1452+
.. function:: pkcs7_verify_pem(data, content=None, certificate=None, options=None)
13901453

13911454
.. versionadded:: 45.0.0
13921455

@@ -1395,16 +1458,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13951458
>>> from cryptography import x509
13961459
>>> from cryptography.hazmat.primitives import hashes, serialization
13971460
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1398-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1399-
>>> key = serialization.load_pem_private_key(ca_key, None)
1461+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1462+
>>> key = serialization.load_pem_private_key(verify_key, None)
14001463
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
14011464
... b"data to sign"
14021465
... ).add_signer(
14031466
... cert, key, hashes.SHA256()
14041467
... ).sign(
14051468
... serialization.Encoding.PEM, []
14061469
... )
1407-
>>> pkcs7.pkcs7_verify_pem(signed, None, cert, [])
1470+
>>> pkcs7.pkcs7_verify_pem(signed)
14081471

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

14161479
:param content: if specified, the content to verify against the signed message. If the content
1417-
is not specified, the function will look for the content in the signed message.
1418-
:type data: bytes or None
1480+
is not specified, the function will look for the content in the signed message. Defaults to
1481+
None.
1482+
:type content: bytes or None
14191483

1420-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1421-
message.
1484+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1485+
the signed message. If None, the function will look for the signer certificate in the signed
1486+
message. Defaults to None.
1487+
:type certificate: :class:`~cryptography.x509.Certificate` or None
14221488

14231489
:param options: A list of
1424-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1425-
this operation, no options are supported as of now.
1490+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1491+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1492+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1493+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1494+
:type options: list[`~cryptography.x509.Certificate`] or None
14261495

14271496
:raises ValueError: If the PEM data does not have the PKCS7 tag.
14281497

@@ -1434,7 +1503,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
14341503
:raises ValueError: If the PKCS7 data is not of the signed data type.
14351504

14361505

1437-
.. function:: pkcs7_verify_smime(data, content, certificate, options)
1506+
.. function:: pkcs7_verify_smime(data, content=None, certificate=None, options=None)
14381507

14391508
.. versionadded:: 45.0.0
14401509

@@ -1443,16 +1512,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
14431512
>>> from cryptography import x509
14441513
>>> from cryptography.hazmat.primitives import hashes, serialization
14451514
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1446-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1447-
>>> key = serialization.load_pem_private_key(ca_key, None)
1515+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1516+
>>> key = serialization.load_pem_private_key(verify_key, None)
14481517
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
14491518
... b"data to sign"
14501519
... ).add_signer(
14511520
... cert, key, hashes.SHA256()
14521521
... ).sign(
14531522
... serialization.Encoding.SMIME, []
14541523
... )
1455-
>>> pkcs7.pkcs7_verify_smime(signed, None, cert, [])
1524+
>>> pkcs7.pkcs7_verify_smime(signed)
14561525

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

14661535
:param content: if specified, the content to verify against the signed message. If the content
14671536
is not specified, the function will look for the content in the MIME message and in the
1468-
signature.
1469-
:type data: bytes or None
1537+
signature. Defaults to None.
1538+
:type content: bytes or None
14701539

1471-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1472-
message.
1540+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1541+
the signed message. If None, the function will look for the signer certificate in the signed
1542+
message. Defaults to None.
1543+
:type certificate: :class:`~cryptography.x509.Certificate` or None
14731544

14741545
:param options: A list of
1475-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1476-
this operation, no options are supported as of now.
1546+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1547+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1548+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1549+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1550+
:type options: list[`~cryptography.x509.Certificate`] or None
1551+
14771552

14781553
:raises ValueError: If the MIME message is not a S/MIME signed message: content type is
14791554
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
@@ -744,10 +744,10 @@ fn verify_der<'p>(
744744
) -> CryptographyResult<()> {
745745
// Check the verify options
746746
let options = match options {
747-
Some(options) => options,
748-
None => &pyo3::types::PyList::empty(py),
747+
Some(options) => Cow::Borrowed(options),
748+
None => Cow::Owned(pyo3::types::PyList::empty(py)),
749749
};
750-
check_verify_options(py, options)?;
750+
check_verify_options(py, &options)?;
751751

752752
// Verify the data
753753
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
@@ -1045,6 +1045,7 @@ def test_not_a_cert(self, backend):
10451045
"invalid_options",
10461046
[
10471047
[b"invalid"],
1048+
[pkcs7.PKCS7Options.Binary],
10481049
],
10491050
)
10501051
def test_pkcs7_verify_invalid_options(self, backend, invalid_options):

0 commit comments

Comments
 (0)