@@ -1262,6 +1262,28 @@ 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+ MIIBhjCCASygAwIBAgICAwkwCgYIKoZIzj0EAwIwJzELMAkGA1UEBhMCVVMxGDAW
1268+ BgNVBAMMD2NyeXB0b2dyYXBoeSBDQTAgFw0xNzAxMDEwMTAwMDBaGA8yMTAwMDEw
1269+ MTAwMDAwMFowJzELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD2NyeXB0b2dyYXBoeSBD
1270+ QTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABBj/z7v5Obj13cPuwECLBnUGq0/N
1271+ 2CxSJE4f4BBGZ7VfFblivTvPDG++Gve0oQ+0uctuhrNQ+WxRv8GC177F+QWjRjBE
1272+ MCEGA1UdEQEB/wQXMBWBE2V4YW1wbGVAZXhhbXBsZS5jb20wHwYDVR0jBBgwFoAU
1273+ /Ou02BLyyT2Zwzxn9H03feYT7fowCgYIKoZIzj0EAwIDSAAwRQIgUwIdC0Emkd6f
1274+ 17DeOXTlmTAhwSDJ2FTuyHESwei7wJcCIQCnr9NpBxbtJfEzxHGGyd7PxgpOLi5u
1275+ rk+8QfzGMmg/fw==
1276+ -----END CERTIFICATE-----
1277+ """.strip()
1278+
1279+ verify_key = b"""
1280+ -----BEGIN PRIVATE KEY-----
1281+ MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgA8Zqz5vLeR0ePZUe
1282+ jBfdyMmnnI4U5uAJApWTsMn/RuWhRANCAAQY/8+7+Tm49d3D7sBAiwZ1BqtPzdgs
1283+ UiROH+AQRme1XxW5Yr07zwxvvhr3tKEPtLnLboazUPlsUb/Bgte+xfkF
1284+ -----END PRIVATE KEY-----
1285+ """.strip()
1286+
12651287.. class :: PKCS7SignatureBuilder
12661288
12671289 The PKCS7 signature builder can create both basic PKCS7 signed messages as
@@ -1340,6 +1362,150 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13401362 :returns bytes: The signed PKCS7 message.
13411363
13421364
1365+ .. function :: pkcs7_verify_der(data, content=None, certificate=None, options=None)
1366+
1367+ .. versionadded :: 45.0.0
1368+
1369+ .. doctest ::
1370+
1371+ >>> from cryptography import x509
1372+ >>> from cryptography.hazmat.primitives import hashes, serialization
1373+ >>> from cryptography.hazmat.primitives.serialization import pkcs7
1374+ >>> cert = x509.load_pem_x509_certificate(verify_cert)
1375+ >>> key = serialization.load_pem_private_key(verify_key, None )
1376+ >>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
1377+ ... b " data to sign"
1378+ ... ).add_signer(
1379+ ... cert, key, hashes.SHA256()
1380+ ... ).sign(
1381+ ... serialization.Encoding.DER , []
1382+ ... )
1383+ >>> pkcs7.pkcs7_verify_der(signed)
1384+
1385+ Deserialize and verify a DER-encoded PKCS7 signed message. PKCS7 (or S/MIME) has multiple
1386+ versions, but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2. If the
1387+ verification succeeds, does not return anything. If the verification fails, raises an exception.
1388+
1389+ :param data: The data, encoded in DER format.
1390+ :type data: bytes
1391+
1392+ :param content: if specified, the content to verify against the signed message. If the content
1393+ is not specified, the function will look for the content in the signed message. Defaults to
1394+ None.
1395+ :type content: bytes or None
1396+
1397+ :param certificate: if specified, a :class: `~cryptography.x509.Certificate ` to verify against
1398+ the signed message. If None, the function will look for the signer certificate in the signed
1399+ message. Defaults to None.
1400+ :type certificate: :class: `~cryptography.x509.Certificate ` or None
1401+
1402+ :raises ValueError: If the recipient certificate does not match any of the signers in the
1403+ PKCS7 data.
1404+
1405+ :raises ValueError: If no content is specified and no content is found in the PKCS7 data.
1406+
1407+ :raises ValueError: If the PKCS7 data is not of the signed data type.
1408+
1409+
1410+ .. function :: pkcs7_verify_pem(data, content=None, certificate=None, options=None)
1411+
1412+ .. versionadded :: 45.0.0
1413+
1414+ .. doctest ::
1415+
1416+ >>> from cryptography import x509
1417+ >>> from cryptography.hazmat.primitives import hashes, serialization
1418+ >>> from cryptography.hazmat.primitives.serialization import pkcs7
1419+ >>> cert = x509.load_pem_x509_certificate(verify_cert)
1420+ >>> key = serialization.load_pem_private_key(verify_key, None )
1421+ >>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
1422+ ... b " data to sign"
1423+ ... ).add_signer(
1424+ ... cert, key, hashes.SHA256()
1425+ ... ).sign(
1426+ ... serialization.Encoding.PEM , []
1427+ ... )
1428+ >>> pkcs7.pkcs7_verify_pem(signed)
1429+
1430+ Deserialize and verify a PEM-encoded PKCS7 signed message. PKCS7 (or S/MIME) has multiple
1431+ versions, but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2. If the
1432+ verification succeeds, does not return anything. If the verification fails, raises an exception.
1433+
1434+ :param data: The data, encoded in PEM format.
1435+ :type data: bytes
1436+
1437+ :param content: if specified, the content to verify against the signed message. If the content
1438+ is not specified, the function will look for the content in the signed message. Defaults to
1439+ None.
1440+ :type content: bytes or None
1441+
1442+ :param certificate: if specified, a :class: `~cryptography.x509.Certificate ` to verify against
1443+ the signed message. If None, the function will look for the signer certificate in the signed
1444+ message. Defaults to None.
1445+ :type certificate: :class: `~cryptography.x509.Certificate ` or None
1446+
1447+ :raises ValueError: If the PEM data does not have the PKCS7 tag.
1448+
1449+ :raises ValueError: If the recipient certificate does not match any of the signers in the
1450+ PKCS7 data.
1451+
1452+ :raises ValueError: If no content is specified and no content is found in the PKCS7 data.
1453+
1454+ :raises ValueError: If the PKCS7 data is not of the signed data type.
1455+
1456+
1457+ .. function :: pkcs7_verify_smime(data, content=None, certificate=None, options=None)
1458+
1459+ .. versionadded :: 45.0.0
1460+
1461+ .. doctest ::
1462+
1463+ >>> from cryptography import x509
1464+ >>> from cryptography.hazmat.primitives import hashes, serialization
1465+ >>> from cryptography.hazmat.primitives.serialization import pkcs7
1466+ >>> cert = x509.load_pem_x509_certificate(verify_cert)
1467+ >>> key = serialization.load_pem_private_key(verify_key, None )
1468+ >>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
1469+ ... b " data to sign"
1470+ ... ).add_signer(
1471+ ... cert, key, hashes.SHA256()
1472+ ... ).sign(
1473+ ... serialization.Encoding.SMIME , []
1474+ ... )
1475+ >>> pkcs7.pkcs7_verify_smime(signed)
1476+
1477+ Verify a PKCS7 signed message stored in a MIME message, by reading it, extracting the content
1478+ (if any) and signature, deserializing the signature and verifying it against the content. PKCS7
1479+ (or S/MIME) has multiple versions, but this supports a subset of :rfc: `5751 `, also known as
1480+ S/MIME Version 3.2. If the verification succeeds, does not return anything. If the verification
1481+ fails, raises an exception.
1482+
1483+ :param data: The data, encoded in MIME format.
1484+ :type data: bytes
1485+
1486+ :param content: if specified, the content to verify against the signed message. If the content
1487+ is not specified, the function will look for the content in the MIME message and in the
1488+ signature. Defaults to None.
1489+ :type content: bytes or None
1490+
1491+ :param certificate: if specified, a :class: `~cryptography.x509.Certificate ` to verify against
1492+ the signed message. If None, the function will look for the signer certificate in the signed
1493+ message. Defaults to None.
1494+ :type certificate: :class: `~cryptography.x509.Certificate ` or None
1495+
1496+ :raises ValueError: If the MIME message is not a S/MIME signed message: content type is
1497+ different than ``multipart/signed `` or ``application/pkcs7-mime ``.
1498+
1499+ :raises ValueError: If the MIME message is a malformed ``multipart/signed `` S/MIME message: not
1500+ multipart, or multipart with more than 2 parts (content & signature).
1501+
1502+ :raises ValueError: If the recipient certificate does not match any of the signers in the
1503+ PKCS7 data.
1504+
1505+ :raises ValueError: If no content is specified and no content is found in the PKCS7 data.
1506+
1507+ :raises ValueError: If the PKCS7 data is not of the signed data type.
1508+
13431509.. class :: PKCS7EnvelopeBuilder
13441510
13451511 The PKCS7 envelope builder can create encrypted S/MIME messages,
@@ -1633,6 +1799,7 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
16331799 obtain the signer's certificate by other means (for example from a
16341800 previously signed message).
16351801
1802+
16361803Serialization Formats
16371804~~~~~~~~~~~~~~~~~~~~~
16381805
0 commit comments