Skip to content
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 3 commits into from
Jan 22, 2024
Merged

Migrate PKCS7 backend to Rust #10228

merged 3 commits into from
Jan 22, 2024

Conversation

facutuesca
Copy link
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 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
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

fixed

)),
Some(c) => c
.iter()
.map(|c| load_pem_x509_certificate(py, c.to_pem()?.as_slice(), None))
Copy link
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
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
Contributor Author

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

@alex
Copy link
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
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
exceptions::UnsupportedAlgorithm::new_err((
"PKCS#7 is not supported by this backend.",
exceptions::Reasons::BACKEND_MISSING_INTERFACE,
)),
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

fixed

fn load_pkcs7_certificates(
py: pyo3::Python<'_>,
pkcs7: Pkcs7,
) -> CryptographyResult<Vec<x509::certificate::Certificate>> {
Copy link
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
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 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
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
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
58 checks passed
@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