Skip to content

Migrate PKCS7 backend to Rust#10228

Merged
alex merged 3 commits intopyca:mainfrom
trail-of-forks:pkcs7-rust
Jan 22, 2024
Merged

Migrate PKCS7 backend to Rust#10228
alex merged 3 commits intopyca:mainfrom
trail-of-forks:pkcs7-rust

Conversation

@facutuesca
Copy link
Copy Markdown
Contributor

Part of #8770. Now that rust-openssl has the necessary bindings, this PR replaces the Python implementation with calls to the Rust OpenSSL bindings.

@facutuesca facutuesca marked this pull request as draft January 22, 2024 14:26
Comment thread src/rust/src/pkcs7.rs Outdated
Comment on lines +299 to +301
let nid = pkcs7.type_().map_or(openssl::nid::Nid::UNDEF, |x| x.nid());
assert_ne!(nid, openssl::nid::Nid::UNDEF);
if nid != openssl::nid::Nid::PKCS7_SIGNED {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this assertion? Can we not just do nid = pkcs7.type_().map(|t| t.nid()); if nid != Some(PKCS7_SIGNED) {}?

Copy link
Copy Markdown
Contributor Author

@facutuesca facutuesca Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replicated the behavior from the Python implementation: src. Should I change it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think my formulation here is better --

it's important to note that the openssl_assert() would produce a python execption while this code would produce a rust panic, so they're not quite teh same

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread src/rust/src/pkcs7.rs Outdated
)),
Some(c) => c
.iter()
.map(|c| load_pem_x509_certificate(py, c.to_pem()?.as_slice(), None))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round-trip via DER, it'll be faster.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@alex alex mentioned this pull request Jan 22, 2024
22 tasks
@facutuesca
Copy link
Copy Markdown
Contributor Author

Leaving as a draft for now since rust-openssl doesn't have PKCS#7 bindings enabled for BoringSSL

@alex
Copy link
Copy Markdown
Member

alex commented Jan 22, 2024

I believe that's because BoringSSL is missing PKCS#7 functionality -- we should just disable this there.

@facutuesca
Copy link
Copy Markdown
Contributor Author

facutuesca commented Jan 22, 2024

I believe that's because BoringSSL is missing PKCS#7 functionality -- we should just disable this there.

Wouldn't that mean we would be effectively removing PCKS#7 support from cryptography with BoringSSL? nvm it was never supported

    def pkcs7_supported(self) -> bool:
        return not self._lib.CRYPTOGRAPHY_IS_BORINGSSL

@facutuesca facutuesca force-pushed the pkcs7-rust branch 3 times, most recently from 05c5b5b to 9bee3fc Compare January 22, 2024 18:26
@facutuesca facutuesca requested a review from alex January 22, 2024 19:12
@facutuesca facutuesca marked this pull request as ready for review January 22, 2024 19:12
Comment thread src/rust/src/pkcs7.rs
exceptions::UnsupportedAlgorithm::new_err((
"PKCS#7 is not supported by this backend.",
exceptions::Reasons::BACKEND_MISSING_INTERFACE,
)),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use UNSUPPORTED_SERIALIZATION here I think

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread src/rust/src/pkcs7.rs Outdated
fn load_pkcs7_certificates(
py: pyo3::Python<'_>,
pkcs7: Pkcs7,
) -> CryptographyResult<Vec<x509::certificate::Certificate>> {
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@facutuesca facutuesca force-pushed the pkcs7-rust branch 2 times, most recently from b94a208 to 71ec332 Compare January 22, 2024 20:57
alex
alex previously approved these changes Jan 22, 2024
@alex alex enabled auto-merge (squash) January 22, 2024 21:02
Comment thread src/rust/src/pkcs7.rs Outdated
Comment on lines +325 to +332
result.append(
load_der_x509_certificate(
py,
pyo3::types::PyBytes::new(py, c.to_der()?.as_slice()).into_py(py),
None,
)?
.into_py(py),
)?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're getting coverage issues here. Best resolution is to rewrite as:

let cert_der = pyo3::types::PyBytes::new(py, c.to_der()?.as_slice()).into_py(py);
let cert = load_der_x509_certificate(py, cert_der, None)?;
result.append(cert.into_py(py))?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx! fixed

auto-merge was automatically disabled January 22, 2024 21:11

Head branch was pushed to by a user without write access

@alex alex enabled auto-merge (squash) January 22, 2024 21:15
@alex alex merged commit 41daf2d into pyca:main Jan 22, 2024
@facutuesca facutuesca deleted the pkcs7-rust branch January 22, 2024 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants