Skip to content

Conversation

frncs-rss
Copy link
Contributor

Upstreaming https://android-review.googlesource.com/c/platform/external/rust/crates/openssl/+/2191953

In the ECPrivateKey schema (RFC 5915 s3) the parameters field which holds the NamedCurve is optional.

If an EC key was originally enclosed in a PKCS#8 (RFC 5208 s5) wrapper, the curve may be identified in the wrapper (in the AlgorithmIdentifier.parameters field) rather than in the ECPrivateKey.

In this case, the existing ec::EcKey::private_key_from_der() method (which corresponds to d2i_ECPrivateKey()) cannot determine the curve and so cannot import such a key.

Add a new method that includes explicit specification of the curve to cope with this situation, passing through to the (BoringSSL-specific) EC_KEY_parse_private_key function.

&mut cbs as *mut ffi::CBS,
group.as_ptr(),
))
.map(|p| EcKey::from_ptr(p))
Copy link
Contributor

@davidben davidben Feb 19, 2025

Choose a reason for hiding this comment

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

Without commenting on whether this makes sense in this project or not, FYI this patch is wrong. It doesn't account for trailing data by checking the CBS is empty after parsing.

Copy link
Contributor

Choose a reason for hiding this comment

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

(There also is a way to do this in OpenSSL but it's suuuuuuuper confusing and hard to use. d2i_ECPrivateKey peeks at the state of the input EC_KEY in the object reuse mode. You also have to be very careful when using the object reuse mode because the function might destroy the object and put a different one in its place.)

Copy link

Choose a reason for hiding this comment

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

Without commenting on whether this makes sense in this project or not, FYI this patch is wrong. It doesn't account for trailing data by checking the CBS is empty after parsing.

Chiming in here since I'm helping Francois with this effort - what would the correct handling be? Generate an error if the CBS is non-empty after parsing?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup. Both CBS and d2i functions parse a prefix of the input and tell you how far they got, so that you can parse a series of things. I'm... not sure this was the least errorprone API design in hindsight but here we are. This means that if you are implementing an API that just parses it, rather than parses and tells you what was left, you need to check the input was fully consumed.

Copy link

Choose a reason for hiding this comment

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

Gotcha; does the new version I uploaded look good? (Assuming we figure out a reasonable error to report)

Upstreaming https://android-review.googlesource.com/c/platform/external/rust/crates/openssl/+/2191953

In the `ECPrivateKey` schema (RFC 5915 s3) the parameters field which
holds the NamedCurve is optional.

If an EC key was originally enclosed in a PKCS#8 (RFC 5208 s5)
wrapper, the curve may be identified in the wrapper (in the
`AlgorithmIdentifier.parameters` field) rather than in the
`ECPrivateKey`.

In this case, the existing `ec::EcKey::private_key_from_der()` method
(which corresponds to `d2i_ECPrivateKey()`) cannot determine the curve
and so cannot import such a key.

Add a new method that includes explicit specification of the curve to
cope with this situation, passing through to the (BoringSSL-specific)
`EC_KEY_parse_private_key` function.
@joshlf joshlf force-pushed the private_key_from_der_for_group branch from b996561 to 5198075 Compare February 22, 2025 00:38
group.as_ptr(),
))?;
if ffi::CBS_len(p) != 0 {
todo!()
Copy link

Choose a reason for hiding this comment

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

Can one of the maintainers let me know what you'd suggest I do here to generate a new error? Thanks!

@alex
Copy link
Collaborator

alex commented Feb 22, 2025

I'm not wild about APIs that are unique to BoringSSL. Given that BoringSSL also provides a native set of Rust bindings, it seems to me the role of BoringSSL support in rust-openssl is primarily to provide compatibility to projects already using rust-openssl which want to also support BoringSSL.

(That's how I've personally used the BoringSSL support thus far.)

Since, AFAIK, Android is the primary other consumer of this support, I'd appreciate understanding your thoughts/vision?

@frncs-rss
Copy link
Contributor Author

I'm not wild about APIs that are unique to BoringSSL. Given that BoringSSL also provides a native set of Rust bindings, it seems to me the role of BoringSSL support in rust-openssl is primarily to provide compatibility to projects already using rust-openssl which want to also support BoringSSL.

(That's how I've personally used the BoringSSL support thus far.)

Since, AFAIK, Android is the primary other consumer of this support, I'd appreciate understanding your thoughts/vision?

My intention was to avoid having to carry the same downstream patch in both Android and Fuchsia.

I can understand not wanting something BoringSSL-specific upstream and it seems that providing an OpenSSL equivalent might be a fair bit of work.

Maybe an alternative we can explore is to try to lift this from a downstream rust-openssl patch to something in KeyMint itself and then Fuchsia will get it when rolling KeyMint – from the look of it, this feels like a constructor more so than accessing private fields so it might be possible?

@alex
Copy link
Collaborator

alex commented Feb 22, 2025 via email

@frncs-rss frncs-rss closed this Feb 28, 2025
bherrera pushed a commit to misttech/fuchsia that referenced this pull request Mar 12, 2025
we temporarily need 2 patches:
1. encrypt.rs: rust-openssl/rust-openssl#2372
   already upstream waiting for the next quarterly release
2. ec.rs: rust-openssl/rust-openssl#2371
   rejected upstream waiting to be inlined into KeyMint tracked by
   https://fxbug.dev/398863191

Fixed: 395970649
Change-Id: I12a795f645b74297948eddb6baaa8ca44df7404d
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1219109
Reviewed-by: Erick Tryzelaar <[email protected]>
Commit-Queue: Francois Rousseau <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants