@@ -1001,11 +1001,6 @@ PKCS7 is a format described in :rfc:`2315`, among other specifications. It can
10011001contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b ``,
10021002``p7m ``, or ``p7s `` file suffix but other suffixes are also seen in the wild.
10031003
1004- .. note ::
1005-
1006- ``cryptography `` only supports parsing certificates from PKCS7 files at
1007- this time.
1008-
10091004.. data :: PKCS7HashTypes
10101005
10111006 .. versionadded :: 40.0.0
@@ -1126,6 +1121,60 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
11261121 -----END CERTIFICATE-----
11271122 """.strip()
11281123
1124+ ca_key_rsa = b"""
1125+ -----BEGIN PRIVATE KEY-----
1126+ MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDQSIXkXNR0+DM1
1127+ eRr1Gw5PQhVOg06JkQKTakZos64kapujmOB7d3e9QV6IOvyAZKgJ2eP1yUONBuLF
1128+ Q2+dpNdaD73yfxeaXPulKjwS/kBs2BpCaLmwKlxaSOqMNKmshTUC79E/aOModEED
1129+ qBr4Apr/daporS62TV7uFPUu+hvg4hkk/kMjJDMY/lbBkbEUQbn1dbq3J7xVo1Ok
1130+ NvnK9nKdJjABvejU8iLJGIifLy9N1s+A1+JJTuF+O3z5g51PzjJ+Em7zGfPeo9S9
1131+ CdOEvrlU4U5MUFnBXKl4V+ajPJM3IyVJsmxZW39edI91ornFuPCv4+3ydMfat4lK
1132+ OBr2tHKEnIJSVnIKPwQQsBQ8PDVW2u56cUkTImkt6k79HRBXEZ7wcnPu4chscZVn
1133+ UxPbR4rFCNXmVZPT/c4qjTmSrHGPGV9fvwuDPV+vWOwPCO+BeXTtuyEcnBIDq0qN
1134+ s9TYX0sG6ia/WtkwbUbBYp5/K4ygSMzZ9BOafYztVo8bZHIx3116SzfBRTL6GCPZ
1135+ fyvmVg5vbG6GhfI64KM0nNNOABXpgB+/ZpghlUSl59bwwKOAywuqdzYgRWEHGG1v
1136+ Vfm3hg+rK7BesSbb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SBQC5LaICztWJaXXGpfJLXdo0ZWIbvQOBVuv4d5jYBMAiNgEAsW7Q4I6xd
1168+ vmHdmsyngo/ZxCvuLZwG2jAAai1slPnXXY1UYeBeBO72PS8bu2o5LpBXsNmVMhGg
1169+ A8U1rm3MOMBGbvmY8/sV4YDR4H0pch4yPja7HMHBtUQOCxXoz/2LvYv0RacMe5mb
1170+ F3ECggEAWxQZnT8pObxKrISZpHSKi54VxuLYbemS63Tdr4HE/KuiFAvbM6AeZOki
1171+ jbiMnqrCTOhJRS/i9HV78zSxRZZyVm961tnsjqMyaamX/S4yD7v3Vzu1mfsdVCa2
1172+ Sl+JUUxsEgs/G3Fu6I/0TsCSn/HgNLM8b3f8TDkbpnOqKX165ddojXqSCfxjuYau
1173+ Szih/+jF1dz2/zBye1ARkLRdY/SzlzGl0cVn8bfkE0YEde7wvQ624Biy7r9i1o40
1174+ 7cy/8EQBR2FcXpOAZ7UgOqgGLNhXnd4FPsX4ldKOf5De8FErQOFirJ8pCUxFGr0U
1175+ fDWXtBuybAb5u+ZaVwHgqaaPCkKkVQ==
1176+ -----END PRIVATE KEY-----
1177+ """.strip()
11291178
11301179.. class :: PKCS7SignatureBuilder
11311180
@@ -1261,28 +1310,204 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
12611310 this operation only
12621311 :attr: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options.Text ` and
12631312 :attr: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options.Binary `
1264- are supported.
1313+ are supported, and cannot be used at the same time .
12651314
12661315 :returns bytes: The enveloped PKCS7 message.
12671316
1317+ .. function :: pkcs7_decrypt_der(data, certificate, private_key, options)
1318+
1319+ .. versionadded :: 44.0.0
1320+
1321+ .. doctest ::
1322+
1323+ >>> from cryptography import x509
1324+ >>> from cryptography.hazmat.primitives import serialization
1325+ >>> from cryptography.hazmat.primitives.serialization import pkcs7
1326+ >>> cert = x509.load_pem_x509_certificate(ca_cert_rsa)
1327+ >>> key = serialization.load_pem_private_key(ca_key_rsa, None )
1328+ >>> options = [pkcs7.PKCS7Options.Text]
1329+ >>> enveloped = pkcs7.PKCS7EnvelopeBuilder().set_data(
1330+ ... b " data to encrypt"
1331+ ... ).add_recipient(
1332+ ... cert
1333+ ... ).encrypt(
1334+ ... serialization.Encoding.DER , options
1335+ ... )
1336+ >>> pkcs7.pkcs7_decrypt_der(enveloped, cert, key, options)
1337+ b'data to encrypt'
1338+
1339+ Deserialize and decrypt a DER-encoded PKCS7 message. PKCS7 (or S/MIME) has multiple versions,
1340+ but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2.
1341+
1342+ :param data: The data, encoded in DER format.
1343+ :type data: bytes
1344+
1345+ :param certificate: A :class: `~cryptography.x509.Certificate ` for an intended
1346+ recipient of the encrypted message. Only certificates with public RSA keys
1347+ are currently supported.
1348+
1349+ :param private_key: The :class: `~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey `
1350+ associated with the certificate provided. Only private RSA keys are supported.
1351+
1352+ :param options: A list of
1353+ :class: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options `. For
1354+ this operation only
1355+ :attr: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options.Text ` is supported.
1356+
1357+ :returns bytes: The decrypted message.
1358+
1359+ :raises ValueError: If the recipient certificate does not match any of the encrypted keys in the
1360+ PKCS7 data.
1361+
1362+ :raises cryptography.exceptions.UnsupportedAlgorithm: If any of the PKCS7 keys are encrypted
1363+ with another algorithm than RSA with PKCS1 v1.5 padding.
1364+
1365+ :raises cryptography.exceptions.UnsupportedAlgorithm: If the content is encrypted with
1366+ another algorithm than AES-128-CBC.
1367+
1368+ :raises ValueError: If the PKCS7 data does not contain encrypted content.
1369+
1370+ :raises ValueError: If the PKCS7 data is not of the enveloped data type.
1371+
1372+ .. function :: pkcs7_decrypt_pem(data, certificate, private_key, options)
1373+
1374+ .. versionadded :: 44.0.0
1375+
1376+ .. doctest ::
1377+
1378+ >>> from cryptography import x509
1379+ >>> from cryptography.hazmat.primitives import serialization
1380+ >>> from cryptography.hazmat.primitives.serialization import pkcs7
1381+ >>> cert = x509.load_pem_x509_certificate(ca_cert_rsa)
1382+ >>> key = serialization.load_pem_private_key(ca_key_rsa, None )
1383+ >>> options = [pkcs7.PKCS7Options.Text]
1384+ >>> enveloped = pkcs7.PKCS7EnvelopeBuilder().set_data(
1385+ ... b " data to encrypt"
1386+ ... ).add_recipient(
1387+ ... cert
1388+ ... ).encrypt(
1389+ ... serialization.Encoding.PEM , options
1390+ ... )
1391+ >>> pkcs7.pkcs7_decrypt_pem(enveloped, cert, key, options)
1392+ b'data to encrypt'
1393+
1394+ Deserialize and decrypt a PEM-encoded PKCS7E message. PKCS7 (or S/MIME) has multiple versions,
1395+ but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2.
1396+
1397+ :param data: The data, encoded in PEM format.
1398+ :type data: bytes
1399+
1400+ :param certificate: A :class: `~cryptography.x509.Certificate ` for an intended
1401+ recipient of the encrypted message. Only certificates with public RSA keys
1402+ are currently supported.
1403+
1404+ :param private_key: The :class: `~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey `
1405+ associated with the certificate provided. Only private RSA keys are supported.
1406+
1407+ :param options: A list of
1408+ :class: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options `. For
1409+ this operation only
1410+ :attr: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options.Text ` is supported.
1411+
1412+ :returns bytes: The decrypted message.
1413+
1414+ :raises ValueError: If the PEM data does not have the PKCS7 tag.
1415+
1416+ :raises ValueError: If the recipient certificate does not match any of the encrypted keys in the
1417+ PKCS7 data.
1418+
1419+ :raises cryptography.exceptions.UnsupportedAlgorithm: If any of the PKCS7 keys are encrypted
1420+ with another algorithm than RSA with PKCS1 v1.5 padding.
1421+
1422+ :raises cryptography.exceptions.UnsupportedAlgorithm: If the content is encrypted with
1423+ another algorithm than AES-128-CBC.
1424+
1425+ :raises ValueError: If the PKCS7 data does not contain encrypted content.
1426+
1427+ :raises ValueError: If the PKCS7 data is not of the enveloped data type.
1428+
1429+ .. function :: pkcs7_decrypt_smime(data, certificate, private_key, options)
1430+
1431+ .. versionadded :: 44.0.0
1432+
1433+ .. doctest ::
1434+
1435+ >>> from cryptography import x509
1436+ >>> from cryptography.hazmat.primitives import serialization
1437+ >>> from cryptography.hazmat.primitives.serialization import pkcs7
1438+ >>> cert = x509.load_pem_x509_certificate(ca_cert_rsa)
1439+ >>> key = serialization.load_pem_private_key(ca_key_rsa, None )
1440+ >>> options = [pkcs7.PKCS7Options.Text]
1441+ >>> enveloped = pkcs7.PKCS7EnvelopeBuilder().set_data(
1442+ ... b " data to encrypt"
1443+ ... ).add_recipient(
1444+ ... cert
1445+ ... ).encrypt(
1446+ ... serialization.Encoding.SMIME , options
1447+ ... )
1448+ >>> pkcs7.pkcs7_decrypt_smime(enveloped, cert, key, options)
1449+ b'data to encrypt'
1450+
1451+ Deserialize and decrypt a S/MIME-encoded PKCS7 message. PKCS7 (or S/MIME) has multiple versions,
1452+ but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2.
1453+
1454+ :param data: The data. It should be in S/MIME format, meaning MIME with content type
1455+ ``application/pkcs7-mime `` or ``application/x-pkcs7-mime ``.
1456+ :type data: bytes
1457+
1458+ :param certificate: A :class: `~cryptography.x509.Certificate ` for an intended
1459+ recipient of the encrypted message. Only certificates with public RSA keys
1460+ are currently supported.
1461+
1462+ :param private_key: The :class: `~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey `
1463+ associated with the certificate provided. Only private RSA keys are supported.
1464+
1465+ :param options: A list of
1466+ :class: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options `. For
1467+ this operation only
1468+ :attr: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options.Text ` is supported.
1469+
1470+ :returns bytes: The decrypted message.
1471+
1472+ :raises ValueError: If the S/MIME data is not one of the correct content types.
1473+
1474+ :raises ValueError: If the recipient certificate does not match any of the encrypted keys in the
1475+ PKCS7 data.
1476+
1477+ :raises cryptography.exceptions.UnsupportedAlgorithm: If any of the PKCS7 keys are encrypted
1478+ with another algorithm than RSA with PKCS1 v1.5 padding.
1479+
1480+ :raises cryptography.exceptions.UnsupportedAlgorithm: If the content is encrypted with
1481+ another algorithm than AES-128-CBC.
1482+
1483+ :raises ValueError: If the PKCS7 data does not contain encrypted content.
1484+
1485+ :raises ValueError: If the PKCS7 data is not of the enveloped data type.
1486+
12681487
12691488.. class :: PKCS7Options
12701489
12711490 .. versionadded :: 3.2
12721491
1273- An enumeration of options for PKCS7 signature and envelope creation.
1492+ An enumeration of options for PKCS7 signature, envelope creation, and decryption .
12741493
12751494 .. attribute :: Text
12761495
1277- The text option adds ``text/plain `` headers to an S/MIME message when
1278- serializing to
1496+ For signing, the text option adds ``text/plain `` headers to an S/MIME message when
1497+ serializing to
12791498 :attr: `~cryptography.hazmat.primitives.serialization.Encoding.SMIME `.
12801499 This option is disallowed with ``DER `` serialization.
1500+ For envelope creation, it adds ``text/plain `` headers to the encrypted content, regardless
1501+ of the specified encoding.
1502+ For envelope decryption, it parses the decrypted content headers (if any), checks if the
1503+ content type is 'text/plain', then removes all headers (keeping only the payload) of this
1504+ decrypted content. If there is no header, or the content type is not "text/plain", it
1505+ raises an error.
12811506
12821507 .. attribute :: Binary
12831508
1284- Signing normally converts line endings (LF to CRLF). When
1285- passing this option the data will not be converted.
1509+ Signature and envelope creation normally converts line endings (LF to CRLF). When
1510+ passing this option, the data will not be converted.
12861511
12871512 .. attribute :: DetachedSignature
12881513
0 commit comments