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

Allow to serialize extension values as DER bytes strings #6346

Merged
merged 2 commits into from
Nov 12, 2021

Conversation

felixfontein
Copy link
Contributor

This is another try to implement #5002 (Support for getting ASN.1 value for parsed extensions).

It uses the existing backend function _create_x509_extension() to convert a extension created for this extension value to an OpenSSL extension, and then uses the same code as in

data = self._backend._lib.X509_EXTENSION_get_data(ext)
self._backend.openssl_assert(data != self._backend._ffi.NULL)
der = self._backend._ffi.buffer(data.data, data.length)[:]
unrecognized = x509.UnrecognizedExtension(oid, der)
to extract the extension's value. As a handler map it uses the union of all handler maps in encode_asn1.py (I checked that the same OID in different mappings are mapped to the same function, in case someone wonders).

If the general approach looks good, I can add documentation and tests.

@reaperhulk
Copy link
Member

We're going to convert X509 encoding to Rust next so I'd prefer to tackle this after we do that conversion. With the rust code we can just call asn1::write_single to serialize to DER so implementing this would be very simple.

@felixfontein felixfontein force-pushed the der-string-for-extension branch 2 times, most recently from 62aeeb1 to 5c26542 Compare October 6, 2021 10:56
@felixfontein felixfontein changed the title Allow to serialize extension as DER bytes string [WIP] Allow to serialize extension as DER bytes string Oct 6, 2021
@felixfontein felixfontein force-pushed the der-string-for-extension branch 2 times, most recently from e20b057 to b4fa36a Compare October 10, 2021 08:51
@felixfontein
Copy link
Contributor Author

@reaperhulk @alex I've reworked this PR to use the Rust code to serialize the extension values (and falls back to Python+OpenSSL for the ones not yet supported by the Rust code). Does the approach look sensible / acceptable? (I've added in the comments what needs to be updated once all extensions are serialized by Rust code.)

@felixfontein felixfontein force-pushed the der-string-for-extension branch 2 times, most recently from 0c56e98 to 91d7e66 Compare October 19, 2021 06:45
@felixfontein felixfontein changed the title [WIP] Allow to serialize extension as DER bytes string Allow to serialize extension as DER bytes string Oct 19, 2021
@felixfontein
Copy link
Contributor Author

I've rebased and added a last test to reach 100% coverage after #6439 was merged. This is now ready for review.

@reaperhulk
Copy link
Member

This PR took advantage of a window where we had exposed public methods for extension encoding. Those are now gone as we completed our x509 conversion. However, we can add methods back, we should just scope them to doing exactly what this method requires. It seems like what you really want is a single rust method encode_extension that takes an ExtensionType and returns the encoded bytes without regard to whether they're a CSR, cert, CRL, CRL entry, or OCSP extension?

I also think the name here should probably be public_bytes (which matches our DER serialization on Name as well).

@felixfontein felixfontein changed the title Allow to serialize extension as DER bytes string Allow to serialize extension values as DER bytes strings Oct 31, 2021
@felixfontein
Copy link
Contributor Author

@reaperhulk thanks for the feedback! I've renamed it to public_bytes as you suggested, and added a rust function x509::common::encode_extension_value (its code inspired by encode_extensions above it), and it seems to work fine. This is the first Rust code I've written in some years now, so hopefully it is not too horrible :)

The failing CI check seems to be unrelated to this PR (or there is something very strange going on I don't understand...).

Copy link
Member

@reaperhulk reaperhulk left a comment

Choose a reason for hiding this comment

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

also have a changelog conflict

docs/x509/reference.rst Outdated Show resolved Hide resolved
src/cryptography/x509/extensions.py Show resolved Hide resolved
src/rust/src/x509/common.rs Outdated Show resolved Hide resolved
@reaperhulk reaperhulk added this to the Thirty Sixth Release milestone Nov 5, 2021
@felixfontein felixfontein changed the title Allow to serialize extension values as DER bytes strings [WIP] Allow to serialize extension values as DER bytes strings Nov 9, 2021
@felixfontein
Copy link
Contributor Author

Rebased on top of #6571.

@felixfontein felixfontein changed the title [WIP] Allow to serialize extension values as DER bytes strings Allow to serialize extension values as DER bytes strings Nov 11, 2021
@felixfontein
Copy link
Contributor Author

Rebased after #6571 was merged, and CI is green on the first try 🎉

Copy link
Member

@reaperhulk reaperhulk left a comment

Choose a reason for hiding this comment

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

approving even though our serialization for KU needs to properly account for unused bits because this code will improve testing for the fix.

@reaperhulk reaperhulk merged commit 19da50e into pyca:main Nov 12, 2021
@felixfontein felixfontein deleted the der-string-for-extension branch November 12, 2021 06:13
@felixfontein
Copy link
Contributor Author

@reaperhulk @alex thanks a lot for reviewing and merging!

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.

3 participants