@@ -1267,6 +1267,154 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
1267
1267
:returns bytes: The signed PKCS7 message.
1268
1268
1269
1269
1270
+ .. function :: pkcs7_verify_der(data, content, certificate, options)
1271
+
1272
+ .. versionadded :: 45.0.0
1273
+
1274
+ .. doctest ::
1275
+
1276
+ >>> from cryptography import x509
1277
+ >>> from cryptography.hazmat.primitives import hashes, serialization
1278
+ >>> 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 )
1281
+ >>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
1282
+ ... b " data to sign"
1283
+ ... ).add_signer(
1284
+ ... cert, key, hashes.SHA256()
1285
+ ... ).sign(
1286
+ ... serialization.Encoding.DER , []
1287
+ ... )
1288
+ >>> pkcs7.pkcs7_verify_der(signed, None , cert, [])
1289
+
1290
+ Deserialize and verify a DER-encoded PKCS7 signed message. PKCS7 (or S/MIME) has multiple
1291
+ versions, but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2. If the
1292
+ verification succeeds, does not return anything. If the verification fails, raises an exception.
1293
+
1294
+ :param data: The data, encoded in DER format.
1295
+ :type data: bytes
1296
+
1297
+ :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
1300
+
1301
+ :param certificate: A :class: `~cryptography.x509.Certificate ` to verify against the signed
1302
+ message.
1303
+
1304
+ :param options: A list of
1305
+ :class: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options `. For
1306
+ this operation, no options are supported as of now.
1307
+
1308
+ :raises ValueError: If the recipient certificate does not match any of the signers in the
1309
+ PKCS7 data.
1310
+
1311
+ :raises ValueError: If no content is specified and no content is found in the PKCS7 data.
1312
+
1313
+ :raises ValueError: If the PKCS7 data is not of the signed data type.
1314
+
1315
+
1316
+ .. function :: pkcs7_verify_pem(data, content, certificate, options)
1317
+
1318
+ .. versionadded :: 45.0.0
1319
+
1320
+ .. doctest ::
1321
+
1322
+ >>> from cryptography import x509
1323
+ >>> from cryptography.hazmat.primitives import hashes, serialization
1324
+ >>> 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 )
1327
+ >>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
1328
+ ... b " data to sign"
1329
+ ... ).add_signer(
1330
+ ... cert, key, hashes.SHA256()
1331
+ ... ).sign(
1332
+ ... serialization.Encoding.PEM , []
1333
+ ... )
1334
+ >>> pkcs7.pkcs7_verify_pem(signed, None , cert, [])
1335
+
1336
+ Deserialize and verify a PEM-encoded PKCS7 signed message. PKCS7 (or S/MIME) has multiple
1337
+ versions, but this supports a subset of :rfc: `5751 `, also known as S/MIME Version 3.2. If the
1338
+ verification succeeds, does not return anything. If the verification fails, raises an exception.
1339
+
1340
+ :param data: The data, encoded in PEM format.
1341
+ :type data: bytes
1342
+
1343
+ :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
1346
+
1347
+ :param certificate: A :class: `~cryptography.x509.Certificate ` to verify against the signed
1348
+ message.
1349
+
1350
+ :param options: A list of
1351
+ :class: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options `. For
1352
+ this operation, no options are supported as of now.
1353
+
1354
+ :raises ValueError: If the PEM data does not have the PKCS7 tag.
1355
+
1356
+ :raises ValueError: If the recipient certificate does not match any of the signers in the
1357
+ PKCS7 data.
1358
+
1359
+ :raises ValueError: If no content is specified and no content is found in the PKCS7 data.
1360
+
1361
+ :raises ValueError: If the PKCS7 data is not of the signed data type.
1362
+
1363
+
1364
+ .. function :: pkcs7_verify_smime(data, content, certificate, options)
1365
+
1366
+ .. versionadded :: 45.0.0
1367
+
1368
+ .. doctest ::
1369
+
1370
+ >>> from cryptography import x509
1371
+ >>> from cryptography.hazmat.primitives import hashes, serialization
1372
+ >>> 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 )
1375
+ >>> signed = pkcs7.PKCS7SignatureBuilder().set_data(
1376
+ ... b " data to sign"
1377
+ ... ).add_signer(
1378
+ ... cert, key, hashes.SHA256()
1379
+ ... ).sign(
1380
+ ... serialization.Encoding.SMIME , []
1381
+ ... )
1382
+ >>> pkcs7.pkcs7_verify_smime(signed, None , cert, [])
1383
+
1384
+ Verify a PKCS7 signed message stored in a MIME message, by reading it, extracting the content
1385
+ (if any) and signature, deserializing the signature and verifying it against the content. PKCS7
1386
+ (or S/MIME) has multiple versions, but this supports a subset of :rfc: `5751 `, also known as
1387
+ S/MIME Version 3.2. If the verification succeeds, does not return anything. If the verification
1388
+ fails, raises an exception.
1389
+
1390
+ :param data: The data, encoded in MIME format.
1391
+ :type data: bytes
1392
+
1393
+ :param content: if specified, the content to verify against the signed message. If the content
1394
+ 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
1397
+
1398
+ :param certificate: A :class: `~cryptography.x509.Certificate ` to verify against the signed
1399
+ message.
1400
+
1401
+ :param options: A list of
1402
+ :class: `~cryptography.hazmat.primitives.serialization.pkcs7.PKCS7Options `. For
1403
+ this operation, no options are supported as of now.
1404
+
1405
+ :raises ValueError: If the MIME message is not a S/MIME signed message: content type is
1406
+ different than ``multipart/signed `` or ``application/pkcs7-mime ``.
1407
+
1408
+ :raises ValueError: If the MIME message is a malformed ``multipart/signed `` S/MIME message: not
1409
+ multipart, or multipart with more than 2 parts (content & signature).
1410
+
1411
+ :raises ValueError: If the recipient certificate does not match any of the signers in the
1412
+ PKCS7 data.
1413
+
1414
+ :raises ValueError: If no content is specified and no content is found in the PKCS7 data.
1415
+
1416
+ :raises ValueError: If the PKCS7 data is not of the signed data type.
1417
+
1270
1418
.. class :: PKCS7EnvelopeBuilder
1271
1419
1272
1420
The PKCS7 envelope builder can create encrypted S/MIME messages,
0 commit comments