Skip to content

Commit 98f9128

Browse files
committed
fix: adapted docmentation
fix: passed into Cow again coverage: added one test case
1 parent 12bc3e8 commit 98f9128

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

1192+
verify_cert = b"""
1193+
-----BEGIN CERTIFICATE-----
1194+
MIID9zCCAt+gAwIBAgIQIxMA+XhyS9Ou0qAc0zPyVTANBgkqhkiG9w0BAQsFADAN
1195+
MQswCQYDVQQDDAJDQTAeFw0yNTAxMDUxMDQ4MjhaFw0yNjAxMDUxMDQ4MjhaMCUx
1196+
IzAhBgkqhkiG9w0BCQEWFGRlbW8xQHRyaXNvZnQuY29tLnBsMIIBIjANBgkqhkiG
1197+
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt0WRzh5y+QmEUjCm+iHXZLrstOSSEhiEcUre
1198+
3L8zkuGYVLCKBEvmaHQI7uCu/xdqEht6/wEBCiK+KLdGDVrD4v3A7TnmHzzhvqCs
1199+
BTL/EmnD3ZMAJVYv4uEBaFpFPSYnPswd353E6KRkFYR4RmFjG9xLTayHXOKqCF6d
1200+
Hd3uVR7NSs98uhcSYRV7g4NdjmaDj8kz5HeRMfr/uqbcriJ9tu/ljFBWYSwPeiNY
1201+
nYhaOBLpUhZckyjFDfC+UpwOBPlkK7J047urvzG21xCtVU9DMHtXMkXYe/C+WSm1
1202+
MRYtgcsOTxpGf+ujceltI2/+IUhWxr5ys7m+xM1jYaM4O1Pw0QIDAQABo4IBOTCC
1203+
ATUwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBQduUy7zqv6z3uk4fJeifohSntD
1204+
2TAtBgNVHR8EJjAkMCKgIKAehhxodHRwOi8vY2EudHJpc29mdC5jb20ucGwvY3Js
1205+
MGYGCCsGAQUFBwEBBFowWDArBggrBgEFBQcwAoYfaHR0cDovL2NhLnRyaXNvZnQu
1206+
Y29tLnBsL2NhY2VydDApBggrBgEFBQcwAYYdaHR0cDovL2NhLnRyaXNvZnQuY29t
1207+
LnBsL29jc3AwHwYDVR0RBBgwFoEUZGVtbzFAdHJpc29mdC5jb20ucGwwHQYDVR0l
1208+
BBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMEMB0GA1UdDgQWBBT0/QFDFX/CCMsX356G
1209+
ImiWwPYxjDAOBgNVHQ8BAf8EBAMCA+gwDQYJKoZIhvcNAQELBQADggEBAL3Iisca
1210+
IqoFBLMox3cIhCANWO/U1eOvjDjfM/tOHn+6jci/pL/ZHgdRtqCCiaCKtJED/f/9
1211+
NFUKqcSZ9+vzW0RWLJxHgIvCSjLpoM06XClSlxjVnv62Hb1NC4FfDfnzyG+DZHus
1212+
nz/MQuXNwHntA6+JyB/HWHUie2ierQYH2mEN1XIJm5luSGwtuGaWfNz/w324ukcV
1213+
pMd3CbEOZqqfSYGWUHOVG90/OMSfKA/I0hia8Yij0X4Ny+b+bLnHaoozZwJ/UqBl
1214+
9ptbfiOOuFXJP7gt547Rp6+2C0XGJM+le0EYlUzbWE6UWgxaIRp5uc8HnUd5e4lX
1215+
br+Ixxcl3WHckkk=
1216+
-----END CERTIFICATE-----
1217+
""".strip()
1218+
1219+
verify_key = b"""
1220+
-----BEGIN RSA PRIVATE KEY-----
1221+
MIIEowIBAAKCAQEAt0WRzh5y+QmEUjCm+iHXZLrstOSSEhiEcUre3L8zkuGYVLCK
1222+
BEvmaHQI7uCu/xdqEht6/wEBCiK+KLdGDVrD4v3A7TnmHzzhvqCsBTL/EmnD3ZMA
1223+
JVYv4uEBaFpFPSYnPswd353E6KRkFYR4RmFjG9xLTayHXOKqCF6dHd3uVR7NSs98
1224+
uhcSYRV7g4NdjmaDj8kz5HeRMfr/uqbcriJ9tu/ljFBWYSwPeiNYnYhaOBLpUhZc
1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FcJNNw2DDrfUPD9
1241+
9eI0rL2cNJUKWRX+Wbz183uRseRGWHJ4u+vpqNcPe8hF1th21EP4HBpAvwcLIXT8
1242+
IuszEkjMavdDHR+OlifsZKfEa07C9Vg2MAG3NnzLITopiMcw8rgN0n2uBVcsT4fV
1243+
i2DhAoGBAIJtHUe9e8oPrasRlZ3bTFmDT+jNg+7RB8ebG8ZDqAUI3/gnklUd0+rF
1244+
nPGI8GEpjwgBxB/zg4/rYz/TEP0E2pd0beWH2vKD31kQVngbz/zhzLHCNLyKDlB4
1245+
vFHpXRHb7ddgTLjHbg6GvY/pRRCqSxWnLgNRW4m+pyLzAx/Hpk1D
1246+
-----END RSA PRIVATE KEY-----
1247+
""".strip()
1248+
11921249
.. class:: PKCS7SignatureBuilder
11931250

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

12691326

1270-
.. function:: pkcs7_verify_der(data, content, certificate, options)
1327+
.. function:: pkcs7_verify_der(data, content=None, certificate=None, options=None)
12711328

12721329
.. versionadded:: 45.0.0
12731330

@@ -1276,16 +1333,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
12761333
>>> from cryptography import x509
12771334
>>> from cryptography.hazmat.primitives import hashes, serialization
12781335
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1279-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1280-
>>> key = serialization.load_pem_private_key(ca_key, None)
1336+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1337+
>>> key = serialization.load_pem_private_key(verify_key, None)
12811338
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
12821339
... b"data to sign"
12831340
... ).add_signer(
12841341
... cert, key, hashes.SHA256()
12851342
... ).sign(
12861343
... serialization.Encoding.DER, []
12871344
... )
1288-
>>> pkcs7.pkcs7_verify_der(signed, None, cert, [])
1345+
>>> pkcs7.pkcs7_verify_der(signed)
12891346

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

12971354
:param content: if specified, the content to verify against the signed message. If the content
1298-
is not specified, the function will look for the content in the signed message.
1299-
:type data: bytes or None
1355+
is not specified, the function will look for the content in the signed message. Defaults to
1356+
None.
1357+
:type content: bytes or None
13001358

1301-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1302-
message.
1359+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1360+
the signed message. If None, the function will look for the signer certificate in the signed
1361+
message. Defaults to None.
1362+
:type certificate: :class:`~cryptography.x509.Certificate` or None
13031363

13041364
:param options: A list of
1305-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1306-
this operation, no options are supported as of now.
1365+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1366+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1367+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1368+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1369+
:type options: list[`~cryptography.x509.Certificate`] or None
13071370

13081371
:raises ValueError: If the recipient certificate does not match any of the signers in the
13091372
PKCS7 data.
@@ -1313,7 +1376,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13131376
:raises ValueError: If the PKCS7 data is not of the signed data type.
13141377

13151378

1316-
.. function:: pkcs7_verify_pem(data, content, certificate, options)
1379+
.. function:: pkcs7_verify_pem(data, content=None, certificate=None, options=None)
13171380

13181381
.. versionadded:: 45.0.0
13191382

@@ -1322,16 +1385,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13221385
>>> from cryptography import x509
13231386
>>> from cryptography.hazmat.primitives import hashes, serialization
13241387
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1325-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1326-
>>> key = serialization.load_pem_private_key(ca_key, None)
1388+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1389+
>>> key = serialization.load_pem_private_key(verify_key, None)
13271390
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
13281391
... b"data to sign"
13291392
... ).add_signer(
13301393
... cert, key, hashes.SHA256()
13311394
... ).sign(
13321395
... serialization.Encoding.PEM, []
13331396
... )
1334-
>>> pkcs7.pkcs7_verify_pem(signed, None, cert, [])
1397+
>>> pkcs7.pkcs7_verify_pem(signed)
13351398

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

13431406
:param content: if specified, the content to verify against the signed message. If the content
1344-
is not specified, the function will look for the content in the signed message.
1345-
:type data: bytes or None
1407+
is not specified, the function will look for the content in the signed message. Defaults to
1408+
None.
1409+
:type content: bytes or None
13461410

1347-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1348-
message.
1411+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1412+
the signed message. If None, the function will look for the signer certificate in the signed
1413+
message. Defaults to None.
1414+
:type certificate: :class:`~cryptography.x509.Certificate` or None
13491415

13501416
:param options: A list of
1351-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1352-
this operation, no options are supported as of now.
1417+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1418+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1419+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1420+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1421+
:type options: list[`~cryptography.x509.Certificate`] or None
13531422

13541423
:raises ValueError: If the PEM data does not have the PKCS7 tag.
13551424

@@ -1361,7 +1430,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13611430
:raises ValueError: If the PKCS7 data is not of the signed data type.
13621431

13631432

1364-
.. function:: pkcs7_verify_smime(data, content, certificate, options)
1433+
.. function:: pkcs7_verify_smime(data, content=None, certificate=None, options=None)
13651434

13661435
.. versionadded:: 45.0.0
13671436

@@ -1370,16 +1439,16 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13701439
>>> from cryptography import x509
13711440
>>> from cryptography.hazmat.primitives import hashes, serialization
13721441
>>> from cryptography.hazmat.primitives.serialization import pkcs7
1373-
>>> cert = x509.load_pem_x509_certificate(ca_cert)
1374-
>>> key = serialization.load_pem_private_key(ca_key, None)
1442+
>>> cert = x509.load_pem_x509_certificate(verify_cert)
1443+
>>> key = serialization.load_pem_private_key(verify_key, None)
13751444
>>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
13761445
... b"data to sign"
13771446
... ).add_signer(
13781447
... cert, key, hashes.SHA256()
13791448
... ).sign(
13801449
... serialization.Encoding.SMIME, []
13811450
... )
1382-
>>> pkcs7.pkcs7_verify_smime(signed, None, cert, [])
1451+
>>> pkcs7.pkcs7_verify_smime(signed)
13831452

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

13931462
:param content: if specified, the content to verify against the signed message. If the content
13941463
is not specified, the function will look for the content in the MIME message and in the
1395-
signature.
1396-
:type data: bytes or None
1464+
signature. Defaults to None.
1465+
:type content: bytes or None
13971466

1398-
:param certificate: A :class:`~cryptography.x509.Certificate` to verify against the signed
1399-
message.
1467+
:param certificate: if specified, a :class:`~cryptography.x509.Certificate` to verify against
1468+
the signed message. If None, the function will look for the signer certificate in the signed
1469+
message. Defaults to None.
1470+
:type certificate: :class:`~cryptography.x509.Certificate` or None
14001471

14011472
:param options: A list of
1402-
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For
1403-
this operation, no options are supported as of now.
1473+
:class:`~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options`. For this
1474+
operation, the `NoSigs` and `NoVerify` options are supported. If `NoSigs` is specified, the
1475+
function will not verify the signature in the PKCS#7 message. If `NoVerify` is specified,
1476+
the function will not verify the certificates in the PKCS#7 message. Defaults to None.
1477+
:type options: list[`~cryptography.x509.Certificate`] or None
1478+
14041479

14051480
:raises ValueError: If the MIME message is not a S/MIME signed message: content type is
14061481
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
@@ -902,6 +902,7 @@ def test_not_a_cert(self, backend):
902902
"invalid_options",
903903
[
904904
[b"invalid"],
905+
[pkcs7.PKCS7Options.Binary],
905906
],
906907
)
907908
def test_pkcs7_verify_invalid_options(self, backend, invalid_options):

0 commit comments

Comments
 (0)