-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate PKCS7 backend to Rust #10228
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,17 @@ use std::ops::Deref; | |
use cryptography_x509::csr::Attribute; | ||
use cryptography_x509::{common, oid, pkcs7}; | ||
use once_cell::sync::Lazy; | ||
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] | ||
use openssl::pkcs7::Pkcs7; | ||
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] | ||
use pyo3::IntoPy; | ||
|
||
use crate::asn1::encode_der_data; | ||
use crate::buf::CffiBuf; | ||
use crate::error::CryptographyResult; | ||
use crate::{types, x509}; | ||
use crate::error::{CryptographyError, CryptographyResult}; | ||
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] | ||
use crate::x509::certificate::load_der_x509_certificate; | ||
use crate::{exceptions, types, x509}; | ||
|
||
const PKCS7_CONTENT_TYPE_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 9, 3); | ||
const PKCS7_MESSAGE_DIGEST_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 9, 4); | ||
|
@@ -290,11 +296,97 @@ fn smime_canonicalize(data: &[u8], text_mode: bool) -> (Cow<'_, [u8]>, Cow<'_, [ | |
} | ||
} | ||
|
||
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] | ||
fn load_pkcs7_certificates( | ||
py: pyo3::Python<'_>, | ||
pkcs7: Pkcs7, | ||
) -> CryptographyResult<Vec<x509::certificate::Certificate>> { | ||
let nid = pkcs7.type_().map(|t| t.nid()); | ||
if nid != Some(openssl::nid::Nid::PKCS7_SIGNED) { | ||
let nid_string = nid.map_or("empty".to_string(), |n| n.as_raw().to_string()); | ||
return Err(CryptographyError::from( | ||
exceptions::UnsupportedAlgorithm::new_err(( | ||
format!("Only basic signed structures are currently supported. NID for this data was {}", nid_string), | ||
exceptions::Reasons::UNSUPPORTED_SERIALIZATION, | ||
)), | ||
)); | ||
} | ||
|
||
let signed_certificates = pkcs7.signed().and_then(|x| x.certificates()); | ||
match signed_certificates { | ||
None => Err(CryptographyError::from( | ||
pyo3::exceptions::PyValueError::new_err( | ||
"The provided PKCS7 has no certificate data, but a cert loading method was called.", | ||
), | ||
)), | ||
Some(c) => c | ||
.iter() | ||
.map(|c| { | ||
load_der_x509_certificate( | ||
py, | ||
pyo3::types::PyBytes::new(py, c.to_der()?.as_slice()).into_py(py), | ||
None, | ||
) | ||
}) | ||
.collect(), | ||
} | ||
} | ||
|
||
#[pyo3::prelude::pyfunction] | ||
fn load_pem_pkcs7_certificates( | ||
py: pyo3::Python<'_>, | ||
data: &[u8], | ||
) -> CryptographyResult<Vec<x509::certificate::Certificate>> { | ||
cfg_if::cfg_if! { | ||
if #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] { | ||
let pkcs7_decoded = openssl::pkcs7::Pkcs7::from_pem(data).map_err(|_| { | ||
CryptographyError::from(pyo3::exceptions::PyValueError::new_err( | ||
"Unable to parse PKCS7 data", | ||
)) | ||
})?; | ||
load_pkcs7_certificates(py, pkcs7_decoded) | ||
} else { | ||
return Err(CryptographyError::from( | ||
exceptions::UnsupportedAlgorithm::new_err(( | ||
"PKCS#7 is not supported by this backend.", | ||
exceptions::Reasons::BACKEND_MISSING_INTERFACE, | ||
)), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
#[pyo3::prelude::pyfunction] | ||
fn load_der_pkcs7_certificates( | ||
py: pyo3::Python<'_>, | ||
data: &[u8], | ||
) -> CryptographyResult<Vec<x509::certificate::Certificate>> { | ||
cfg_if::cfg_if! { | ||
if #[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))] { | ||
let pkcs7_decoded = openssl::pkcs7::Pkcs7::from_der(data).map_err(|_| { | ||
CryptographyError::from(pyo3::exceptions::PyValueError::new_err( | ||
"Unable to parse PKCS7 data", | ||
)) | ||
})?; | ||
load_pkcs7_certificates(py, pkcs7_decoded) | ||
} else { | ||
return Err(CryptographyError::from( | ||
exceptions::UnsupportedAlgorithm::new_err(( | ||
"PKCS#7 is not supported by this backend.", | ||
exceptions::Reasons::BACKEND_MISSING_INTERFACE, | ||
)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
)); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn create_submodule(py: pyo3::Python<'_>) -> pyo3::PyResult<&pyo3::prelude::PyModule> { | ||
let submod = pyo3::prelude::PyModule::new(py, "pkcs7")?; | ||
|
||
submod.add_function(pyo3::wrap_pyfunction!(serialize_certificates, submod)?)?; | ||
submod.add_function(pyo3::wrap_pyfunction!(sign_and_serialize, submod)?)?; | ||
submod.add_function(pyo3::wrap_pyfunction!(load_pem_pkcs7_certificates, submod)?)?; | ||
submod.add_function(pyo3::wrap_pyfunction!(load_der_pkcs7_certificates, submod)?)?; | ||
|
||
Ok(submod) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit: Instead of returning a Vec, I'd suggest the
&pyo3::types::PyList
. This avoids a copy+malloc round trip.See verify.rs l239 for an example of this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed