Skip to content

Commit b849f16

Browse files
committed
removed options for now
1 parent c45ee63 commit b849f16

File tree

6 files changed

+78
-197
lines changed

6 files changed

+78
-197
lines changed

docs/hazmat/primitives/asymmetric/serialization.rst

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,13 +1360,6 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
13601360
the signed message. If None, the function will look for the signer certificate in the signed
13611361
message. Defaults to None.
13621362
:type certificate: :class:`~cryptography.x509.Certificate` or None
1363-
1364-
:param options: A list of
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
13701363

13711364
:raises ValueError: If the recipient certificate does not match any of the signers in the
13721365
PKCS7 data.
@@ -1413,13 +1406,6 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
14131406
message. Defaults to None.
14141407
:type certificate: :class:`~cryptography.x509.Certificate` or None
14151408

1416-
:param options: A list of
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
1422-
14231409
:raises ValueError: If the PEM data does not have the PKCS7 tag.
14241410

14251411
:raises ValueError: If the recipient certificate does not match any of the signers in the
@@ -1469,14 +1455,6 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
14691455
message. Defaults to None.
14701456
:type certificate: :class:`~cryptography.x509.Certificate` or None
14711457

1472-
:param options: A list of
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-
1479-
14801458
:raises ValueError: If the MIME message is not a S/MIME signed message: content type is
14811459
different than ``multipart/signed`` or ``application/pkcs7-mime``.
14821460

@@ -1783,11 +1761,6 @@ contain certificates, CRLs, and much more. PKCS7 files commonly have a ``p7b``,
17831761
obtain the signer's certificate by other means (for example from a
17841762
previously signed message).
17851763

1786-
.. attribute:: NoVerify
1787-
1788-
For S/MIME verification only. Don't verify signers certificate. This is
1789-
useful when the signer's certificate is not available or when the signer's
1790-
certificate is not trusted.
17911764

17921765
Serialization Formats
17931766
~~~~~~~~~~~~~~~~~~~~~

src/cryptography/hazmat/bindings/_rust/pkcs7.pyi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,16 @@ def verify_der(
4646
signature: bytes,
4747
content: bytes | None = None,
4848
certificate: x509.Certificate | None = None,
49-
options: Iterable[pkcs7.PKCS7Options] | None = None,
5049
) -> None: ...
5150
def verify_pem(
5251
signature: bytes,
5352
content: bytes | None = None,
5453
certificate: x509.Certificate | None = None,
55-
options: Iterable[pkcs7.PKCS7Options] | None = None,
5654
) -> None: ...
5755
def verify_smime(
5856
signature: bytes,
5957
content: bytes | None = None,
6058
certificate: x509.Certificate | None = None,
61-
options: Iterable[pkcs7.PKCS7Options] | None = None,
6259
) -> None: ...
6360
def load_pem_pkcs7_certificates(
6461
data: bytes,

src/cryptography/hazmat/primitives/serialization/pkcs7.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class PKCS7Options(utils.Enum):
5252
NoCapabilities = "Don't embed SMIME capabilities"
5353
NoAttributes = "Don't embed authenticatedAttributes"
5454
NoCerts = "Don't embed signer certificate"
55-
NoVerify = "Don't verify signers certificate"
56-
NoSigs = "Don't verify signature"
5755

5856

5957
class PKCS7SignatureBuilder:

src/rust/src/pkcs7.rs

Lines changed: 76 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,12 @@ fn compute_pkcs7_signature_algorithm<'p>(
673673
}
674674

675675
#[pyo3::pyfunction]
676-
#[pyo3(signature = (signature, content = None, certificate = None, options = None))]
676+
#[pyo3(signature = (signature, content = None, certificate = None))]
677677
fn verify_smime<'p>(
678678
py: pyo3::Python<'p>,
679679
signature: &[u8],
680680
content: Option<&[u8]>,
681681
certificate: Option<pyo3::Bound<'p, x509::certificate::Certificate>>,
682-
options: Option<&pyo3::Bound<'p, pyo3::types::PyList>>,
683682
) -> CryptographyResult<()> {
684683
// Parse the email
685684
let py_content_and_signature = types::SMIME_SIGNED_DECODE.get(py)?.call1((signature,))?;
@@ -703,17 +702,16 @@ fn verify_smime<'p>(
703702
}
704703
};
705704

706-
verify_der(py, signature, content, certificate, options)
705+
verify_der(py, signature, content, certificate)
707706
}
708707

709708
#[pyo3::pyfunction]
710-
#[pyo3(signature = (signature, content = None, certificate = None, options = None))]
709+
#[pyo3(signature = (signature, content = None, certificate = None))]
711710
fn verify_pem<'p>(
712711
py: pyo3::Python<'p>,
713712
signature: &[u8],
714713
content: Option<&[u8]>,
715714
certificate: Option<pyo3::Bound<'p, x509::certificate::Certificate>>,
716-
options: Option<&pyo3::Bound<'p, pyo3::types::PyList>>,
717715
) -> CryptographyResult<()> {
718716
let pem_str = std::str::from_utf8(signature)
719717
.map_err(|_| pyo3::exceptions::PyValueError::new_err("Invalid PEM data"))?;
@@ -729,25 +727,17 @@ fn verify_pem<'p>(
729727
));
730728
}
731729

732-
verify_der(py, &pem.into_contents(), content, certificate, options)
730+
verify_der(py, &pem.into_contents(), content, certificate)
733731
}
734732

735733
#[pyo3::pyfunction]
736-
#[pyo3(signature = (signature, content = None, certificate = None, options = None))]
734+
#[pyo3(signature = (signature, content = None, certificate = None))]
737735
fn verify_der<'p>(
738736
py: pyo3::Python<'p>,
739737
signature: &[u8],
740738
content: Option<&[u8]>,
741739
certificate: Option<pyo3::Bound<'p, x509::certificate::Certificate>>,
742-
options: Option<&pyo3::Bound<'p, pyo3::types::PyList>>,
743740
) -> CryptographyResult<()> {
744-
// Check the verify options
745-
let options = match options {
746-
Some(options) => Cow::Borrowed(options),
747-
None => Cow::Owned(pyo3::types::PyList::empty(py)),
748-
};
749-
check_verify_options(py, &options)?;
750-
751741
// Verify the data
752742
let content_info = asn1::parse_single::<pkcs7::ContentInfo<'_>>(signature)?;
753743
match content_info.content {
@@ -793,88 +783,84 @@ fn verify_der<'p>(
793783

794784
// Get recipients, and find the one matching with the signer infos (if any)
795785
// TODO: what to do for multiple certificates?
796-
if !options.contains(types::PKCS7_NO_SIGS.get(py)?)? {
797-
let mut signer_infos = signed_data.signer_infos.unwrap_read().clone();
798-
let signer_certificate = certificate.get().raw.borrow_dependent();
799-
let signer_serial_number = signer_certificate.tbs_cert.serial;
800-
let signer_issuer = signer_certificate.tbs_cert.issuer.clone();
801-
let found_signer_info = signer_infos.find(|info| {
802-
info.issuer_and_serial_number.serial_number == signer_serial_number
803-
&& info.issuer_and_serial_number.issuer == signer_issuer
804-
});
786+
let mut signer_infos = signed_data.signer_infos.unwrap_read().clone();
787+
let signer_certificate = certificate.get().raw.borrow_dependent();
788+
let signer_serial_number = signer_certificate.tbs_cert.serial;
789+
let signer_issuer = signer_certificate.tbs_cert.issuer.clone();
790+
let found_signer_info = signer_infos.find(|info| {
791+
info.issuer_and_serial_number.serial_number == signer_serial_number
792+
&& info.issuer_and_serial_number.issuer == signer_issuer
793+
});
805794

806-
// Raise error when no signer is found
807-
let signer_info = match found_signer_info {
808-
Some(info) => info,
809-
None => {
810-
return Err(CryptographyError::from(
811-
pyo3::exceptions::PyValueError::new_err(
812-
"No signer found that matches the given certificate.",
813-
),
814-
));
815-
}
816-
};
817-
818-
// Prepare the content: try to use the authenticated attributes, then the content stored
819-
// in the signed data, then the provided content. If None of these are available, raise
820-
// an error. TODO: what should the order be?
821-
let data = match signer_info.authenticated_attributes {
822-
Some(attrs) => Cow::Owned(asn1::write_single(&attrs)?),
823-
None => match content {
824-
Some(data) => Cow::Borrowed(data),
825-
None => match signed_data.content_info.content {
826-
pkcs7::Content::Data(Some(data)) => Cow::Borrowed(data.into_inner()),
827-
_ => {
828-
return Err(CryptographyError::from(
829-
pyo3::exceptions::PyValueError::new_err(
830-
"No content stored in the signature or provided.",
831-
),
832-
));
833-
}
834-
},
835-
},
836-
};
837-
838-
// For RSA signatures (with no PSS padding), the OID is always the same no matter the
839-
// digest algorithm. We need to modify the algorithm identifier to add the hash
840-
// algorithm information. We are checking for RSA-256, which the S/MIME v3.2 RFC
841-
// specifies as MUST support (https://datatracker.ietf.org/doc/html/rfc5751#section-2.2)
842-
let signature_algorithm = match signer_info.digest_encryption_algorithm.oid() {
843-
&oid::RSA_OID => match signer_info.digest_algorithm.oid() {
844-
&oid::SHA256_OID => common::AlgorithmIdentifier {
845-
oid: asn1::DefinedByMarker::marker(),
846-
params: common::AlgorithmParameters::RsaWithSha256(Some(())),
847-
},
795+
// Raise error when no signer is found
796+
let signer_info = match found_signer_info {
797+
Some(info) => info,
798+
None => {
799+
return Err(CryptographyError::from(
800+
pyo3::exceptions::PyValueError::new_err(
801+
"No signer found that matches the given certificate.",
802+
),
803+
));
804+
}
805+
};
806+
807+
// Prepare the content: try to use the authenticated attributes, then the content stored
808+
// in the signed data, then the provided content. If None of these are available, raise
809+
// an error. TODO: what should the order be?
810+
let data = match signer_info.authenticated_attributes {
811+
Some(attrs) => Cow::Owned(asn1::write_single(&attrs)?),
812+
None => match content {
813+
Some(data) => Cow::Borrowed(data),
814+
None => match signed_data.content_info.content {
815+
pkcs7::Content::Data(Some(data)) => Cow::Borrowed(data.into_inner()),
848816
_ => {
849817
return Err(CryptographyError::from(
850-
exceptions::UnsupportedAlgorithm::new_err((
851-
"Only SHA-256 is currently supported for content verification with RSA.",
852-
exceptions::Reasons::UNSUPPORTED_SERIALIZATION,
853-
)),
854-
))
818+
pyo3::exceptions::PyValueError::new_err(
819+
"No content stored in the signature or provided.",
820+
),
821+
));
855822
}
856823
},
857-
_ => signer_info.digest_encryption_algorithm,
858-
};
859-
860-
// Verify the signature
861-
x509::sign::verify_signature_with_signature_algorithm(
862-
py,
863-
certificate.call_method0(pyo3::intern!(py, "public_key"))?,
864-
&signature_algorithm,
865-
signer_info.encrypted_digest,
866-
&data,
867-
)?;
868-
}
824+
},
825+
};
826+
827+
// For RSA signatures (with no PSS padding), the OID is always the same no matter the
828+
// digest algorithm. We need to modify the algorithm identifier to add the hash
829+
// algorithm information. We are checking for RSA-256, which the S/MIME v3.2 RFC
830+
// specifies as MUST support (https://datatracker.ietf.org/doc/html/rfc5751#section-2.2)
831+
let signature_algorithm = match signer_info.digest_encryption_algorithm.oid() {
832+
&oid::RSA_OID => match signer_info.digest_algorithm.oid() {
833+
&oid::SHA256_OID => common::AlgorithmIdentifier {
834+
oid: asn1::DefinedByMarker::marker(),
835+
params: common::AlgorithmParameters::RsaWithSha256(Some(())),
836+
},
837+
_ => {
838+
return Err(CryptographyError::from(
839+
exceptions::UnsupportedAlgorithm::new_err((
840+
"Only SHA-256 is currently supported for content verification with RSA.",
841+
exceptions::Reasons::UNSUPPORTED_SERIALIZATION,
842+
)),
843+
))
844+
}
845+
},
846+
_ => signer_info.digest_encryption_algorithm,
847+
};
848+
849+
// Verify the signature
850+
x509::sign::verify_signature_with_signature_algorithm(
851+
py,
852+
certificate.call_method0(pyo3::intern!(py, "public_key"))?,
853+
&signature_algorithm,
854+
signer_info.encrypted_digest,
855+
&data,
856+
)?;
869857

870858
// Verify the certificate
871-
if !options.contains(types::PKCS7_NO_VERIFY.get(py)?)? {
872-
let certificates = pyo3::types::PyList::empty(py);
873-
certificates.append(certificate)?;
874-
types::VERIFY_PKCS7_CERTIFICATES
875-
.get(py)?
876-
.call1((certificates,))?;
877-
}
859+
let certificates = pyo3::types::PyList::empty(py);
860+
certificates.append(certificate)?;
861+
types::VERIFY_PKCS7_CERTIFICATES
862+
.get(py)?
863+
.call1((certificates,))?;
878864
}
879865
_ => {
880866
return Err(CryptographyError::from(
@@ -888,38 +874,6 @@ fn verify_der<'p>(
888874
Ok(())
889875
}
890876

891-
fn check_verify_options<'p>(
892-
py: pyo3::Python<'p>,
893-
options: &pyo3::Bound<'p, pyo3::types::PyList>,
894-
) -> Result<(), CryptographyError> {
895-
// Check if all options are from the PKCS7Options enum
896-
let pkcs7_options = types::PKCS7_OPTIONS.get(py)?;
897-
for opt in options.iter() {
898-
if !opt.is_instance(&pkcs7_options)? {
899-
return Err(CryptographyError::from(
900-
pyo3::exceptions::PyValueError::new_err(
901-
"options must be from the PKCS7Options enum",
902-
),
903-
));
904-
}
905-
}
906-
907-
// Check if any option is not PKCS7Options::NoVerify
908-
let no_verify_option = types::PKCS7_NO_VERIFY.get(py)?;
909-
let no_sigs_option = types::PKCS7_NO_SIGS.get(py)?;
910-
for opt in options.iter() {
911-
if opt.ne(no_verify_option.clone())? & opt.ne(no_sigs_option.clone())? {
912-
return Err(CryptographyError::from(
913-
pyo3::exceptions::PyValueError::new_err(
914-
"Only the following options are supported for verification: NoVerify, NoSigs",
915-
),
916-
));
917-
}
918-
}
919-
920-
Ok(())
921-
}
922-
923877
fn smime_canonicalize(data: &[u8], text_mode: bool) -> (Cow<'_, [u8]>, Cow<'_, [u8]>) {
924878
let mut new_data_with_header = vec![];
925879
let mut new_data_without_header = vec![];

src/rust/src/types.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,6 @@ pub static PKCS7_DETACHED_SIGNATURE: LazyPyImport = LazyPyImport::new(
354354
"cryptography.hazmat.primitives.serialization.pkcs7",
355355
&["PKCS7Options", "DetachedSignature"],
356356
);
357-
pub static PKCS7_NO_VERIFY: LazyPyImport = LazyPyImport::new(
358-
"cryptography.hazmat.primitives.serialization.pkcs7",
359-
&["PKCS7Options", "NoVerify"],
360-
);
361-
pub static PKCS7_NO_SIGS: LazyPyImport = LazyPyImport::new(
362-
"cryptography.hazmat.primitives.serialization.pkcs7",
363-
&["PKCS7Options", "NoSigs"],
364-
);
365357

366358
pub static SMIME_ENVELOPED_ENCODE: LazyPyImport = LazyPyImport::new(
367359
"cryptography.hazmat.primitives.serialization.pkcs7",

0 commit comments

Comments
 (0)