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

LibWeb: Complete WebCryptoAPI ECDH support #2598

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

devgianlu
Copy link
Contributor

This PR aims to implement ECDH support in WebCryptoAPI fully. There are still some edge cases that are not handled in WPT, but overall it increases the number of tests that pass by ~700.

P-521 support is still missing across the board.

While working with the code around ASN.1, I noticed that parsing utilities can be improved, especially by moving some stuff out of LibCrypto::Certificate and into LibCrypto::ASN1 (e.g. macros), but this was outside the scope of this PR.

Some love is also due in the EC implementations that, right now, work with bytes only. An attempt has been made to sketch something that works with numbers in this PR, but it's just a placeholder for an actual refactor.

Add support for encoding parameters in `wrap_in_private_key_info` and
`wrap_in_subject_public_key_info` as well as turn `Span<int>` into
`Span<int const>`.
It looks like the `SECPxxxr1` was made mainly to work with the TLS
implementation which requires everything to be bytes. This is not always
 the case and a loss of generality.

 I have added some methods that take and return `UnsignedBigInteger`s
 for better interoperability with ASN.1 stuff. I would like to remove
 the old methods relying on bytes, but I haven't made my mind around how
  to generalize it for all curves.
Added the following OIDs:
    - secp256r1_oid
    - secp384r1_oid
    - secp521r1_oid
This will be required to parse the ASN.1 `ECPrivateKey` sequence in the
next commits.
Added basic EC private and public key definitions as well as ASN.1
encoding and decoding.

A lot of refactoring can be made around the ASN.1 processing (here and
in other parts of the codebase) by utilizing what is available
in `LibCrypto::Certificate` as macros, but I think it's outside the
scope of implementing ECDH support for WebCryptoAPI.
Parse and store the `ECPrivateKey` extracted from the
`privateKeyAlgorithm` field of the ASN.1 `PrivateKeyInfo` sequence when
the algorithm identifier is `ec_public_key_encryption`.

The parsing function returns `ErrorOr` instead of an "empty" key, like
`parse_rsa_key` does. To me, this seemed better in terms of reliability.
 As mentioned in the previous commit, there is room for improvement.
Previously, `ECDH::generate_key` was implemented by storing a
`ByteBuffer` in the `InternalKeyData`. This improves the implementation
by using internal structures of already-parsed data.
@@ -611,7 +611,7 @@ JS::ThrowCompletionOr<GC::Ref<WebIDL::Promise>> SubtleCrypto::derive_bits(Algori
// 6. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm.

// 7. If the name member of normalizedAlgorithm is not equal to the name attribute of the [[algorithm]] internal slot of baseKey then throw an InvalidAccessError.
if (normalized_algorithm.parameter->name != base_key->algorithm_name()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

While this passes more subtests, it goes against the spec. A bug report in WPT or the spec would be great :)

https://www.w3.org/TR/WebCryptoAPI/#conformance

Unless otherwise stated, string comparisons are done in a case-sensitive manner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've dropped the commit for now since it's not that important. I've also noticed there are some places where existing code is not fully compliant, will do a separate PR.

I've never submitted a spec bug, so I'll take my time. If anyone wants to do it, feel free.

Import and rebaseline tests related to WebCryptoAPI ECDH. The commits
before this one allow for ~700 tests to pass.
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.

2 participants