Commit graph

51 commits

Author SHA1 Message Date
devgianlu
94374f0d19 LibWeb: Implement AES-KW in WebCryptoAPI
Add support for AES-KW for key wrapping/unwrapping. Very similar
implementation to other AES modes.

Added generic tests for symmetric import and specific AES-KW ones.

Adds ~400 test passes on WPT. Now we do better than Firefox in
`WebCryptoAPI/wrapKey_unwrapKey`!
2024-12-17 11:00:14 +01:00
devgianlu
6ef8b54d21 LibWeb: Add support for AES-GCM encrypt and decrypt
Adds ~400 WPT test passes.
2024-12-16 13:27:53 +01:00
devgianlu
06733bea48 LibWeb: Fix X448 PCKS#8 key export format
The ASN1 structure for PCKS#8 was wrong and missing one wrapping of the
key in a OctetString.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
89f1f3f31c LibWeb: Fix X25519 PCKS#8 key export format
The ASN1 structure for PCKS#8 was wrong and missing one wrapping of the
key in a OctetString.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
880401030d LibWeb: Fix X448 JWK key export format
The presence of padding in the base64 fields made plenty of WPT tests
fail. Additionally, export was performed with the wrong public key.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
ac99e2791f LibWeb: Fix X25519 JWK key export format
The presence of padding in the base64 fields made plenty of WPT tests
fail.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
08af878466 LibCrypto+LibWeb: Allow serializing key info without params
Previously, if `nullptr` was passed as params for
`wrap_in_private_key_info` or `wrap_in_subject_public_key_info` an ASN1
null was serialized. This was not the intended behaviour for many.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
9613b87b12 LibWeb: Fix ED25519 PCKS#8 key export format
The ASN1 structure for PCKS#8 was wrong and missing one wrapping of the
key in a OctetString.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
6e33dbb533 LibWeb: Fix ED25519 JWK key export format
The presence of padding in the base64 fields and the typo made plenty of
WPT tests fail.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
1e98fa96d7 LibWeb: Fix bogus AesGcm and AesCtr key import length validation
The validation of the key size and specified algorithm was out of spec.
It is now implemented correctly like in `AesCbc`.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
ec990d620f LibCrypto: Cleanup Crypto::PK::RSA constructors to avoid pitfalls
- Removed the constructor taking a (n, d, e) tuple and moved
  it to `RSAPrivateKey`
- Removed default constructor with key generation because it was always
  misused and the default key size is quite small
- Added utility constructors to accept a key pair, public key, private
  key or both
- Made constructor parameters const
- Updated test to use generated random keys where possible
2024-12-15 23:31:49 +01:00
devgianlu
9240d38273 LibCrypto+LibTLS+LibWeb: Store EC key size + refactor serialization
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
In order for public/private key serialization to work correctly we must
store the size of the key because P-521 cannot be stored as full words
inside `UnsignedBigInteger` and therefore is exported as the wrong
length (68 instead of 66).

This makes it also possible to refactor some methods and cleanup
constants scattered around.

Gets almost all import/export tests, expect the JWK ones that calculate
the public key on export. The `SECPxxxr1` implementation currently fails
to do calculations for P-521.
2024-12-14 01:52:16 +01:00
devgianlu
70db7772b8 LibWeb: Expose support for P-521 in ECDH and ECDSA
Replace all TODOs and FIXMEs requiring P-521 support with actual code
paths that make use of it. Gets a few tests by simply not failing early.
2024-12-14 01:52:16 +01:00
Ben Wiederhake
872a6a11a4 WebCrypto: Unspoof correctness of AES-GSM encryption/decryption 2024-12-12 21:48:57 +01:00
devgianlu
dace140b0d LibCrypto+LibWeb: Implement ECDSA.sign
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
Gained ~20 tests, failing only on P-521.
2024-12-03 13:20:51 +01:00
devgianlu
1d11448f00 LibCrypto+LibWeb: Refactor integer conversions in SECPxxxr1
Little effort to refactor the chaos of integers / bytes / ASN.1 that
is inside `SECPxxxr1`. More love is needed.
2024-12-03 13:20:51 +01:00
devgianlu
0c60f7c995 LibWeb: Migrate ECDSA.generateKey and ECDSA.verify away from ByteBuffer
Use instances of `ECPublicKey` and `ECPrivateKey` instead of
`ByteBuffer` for ECDSA. Fixes another ~200 tests.
2024-12-03 13:20:51 +01:00
devgianlu
f0fbd50c66 LibWeb: Implement ECDSA.importKey and ECDSA.exportKey
Very similar implementation to ECDH. Fixes ~700 tests!
2024-12-03 13:20:51 +01:00
devgianlu
c6e684791f LibWeb: Remove useless bytes conversion in ECDH.exportKey
Using `generate_public_key_point` instead of `generate_public_key`
removes a bit of redundant code.
2024-11-30 11:17:44 +01:00
devgianlu
399b3d2430 LibCrypto+LibWeb: Parse EC public key in parse_subject_public_key_info
Replicate what we are doing with RSA and parse both the private and
public key when parsing the ASN1.

The only thing that changed in the tests is the error message.
2024-11-30 11:17:44 +01:00
devgianlu
51f69be51f LibCrypto: Move ASN1 constants to Crypto::ASN1
Makes more sense to have them in `Crypto::ASN1` rather than in
`Crypto::Certificate`.
2024-11-30 11:17:44 +01:00
devgianlu
ab2960e49f LibCrypto+LibWeb: Reorganize OID ASN1 constants
I have divided ANS1 constants by length so that they don't have
trailing zeros that need to be removed.

Also moved OIDs lists to the only place they are used for clarity.

Fixed a couple of WPT tests by adding SECP521r1 to the list of known
curves.
2024-11-30 11:17:44 +01:00
devgianlu
46e724729c LibWeb: Match algorithm names case-insensitive
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
I dug through the code and the WebCryptoAPI spec to figure out the
reason for `... mixed case parameters` WPT tests and figured out that
our implementation was slightly wrong.

By being closer to the spec we can now pass those tests and also remove
a bunch of duplicated code.

Context: https://github.com/LadybirdBrowser/ladybird/pull/2598#discussion_r1859263798
2024-11-28 00:21:49 +01:00
devgianlu
6ebc812035 LibWeb: Rename EcdhKeyDerivePrams to EcdhKeyDeriveParams 2024-11-28 00:21:49 +01:00
devgianlu
13c9874830 LibWeb: Be spec compliant by comparing WebCryptoAPI strings with case
Following the discovery that the WebCryptoAPI spec requires all strings
to be compared case-sensitive, I have updated existing code that did
not respect this criterion.

No test failures are introduced.

Spec: https://www.w3.org/TR/WebCryptoAPI/#conformance
Context: https://github.com/LadybirdBrowser/ladybird/pull/2598#discussion_r1859263798
2024-11-28 00:21:49 +01:00
devgianlu
6fa53a4382 LibWeb: Implement ECDH.exportKey 2024-11-27 10:59:48 +01:00
devgianlu
228276a383 LibWeb: Implement ECDH.importKey 2024-11-27 10:59:48 +01:00
devgianlu
e6056ab959 LibWeb: Implement ECDH.deriveBits 2024-11-27 10:59:48 +01:00
devgianlu
4fb19913a1 LibWeb: Switch ECDH.generateKey to use internal keys instead of bytes
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.
2024-11-27 10:59:48 +01:00
devgianlu
fd336ed619 LibWeb: Define EcKeyImportParams 2024-11-27 10:59:48 +01:00
devgianlu
9eea94aa14 LibCrypto+LibWeb: Support passing parameters to ASN.1 wrappers
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>`.
2024-11-27 10:59:48 +01:00
Andreas Kling
d625e12082 LibWeb: Implement the deriveBits algorithm for X448
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
2024-11-25 17:16:51 +01:00
Andreas Kling
96ddccd961 LibWeb: Implement the importKey algorithm for X448 2024-11-25 17:16:51 +01:00
Andreas Kling
4a734226aa LibWeb: Implement the exportKey algorithm for X448 2024-11-25 17:16:51 +01:00
Andreas Kling
5a8b0a2610 LibWeb: Implement the generateKey algorithm for X448 2024-11-25 17:16:51 +01:00
devgianlu
506e490793 LibWeb+LibCrypto: Remove OID constants scattered around
Now that `Certificate` has been moved, the OID constants are easily
reachable in `LibCrypto`.
2024-11-25 13:38:38 +01:00
devgianlu
49c388b891 LibTLS+LibWeb+LibCrypto: Move Certificate to LibCrypto
By moving `Certificate` to `LibCrypto` it is possible to reuse a bunch
of code from in `LibCrypto` itself. It also moves some constants
and pieces of code to a more appropriate place than `LibTLS`.

This also makes future work on WebCryptoAPI easier.
2024-11-25 13:38:38 +01:00
devgianlu
fcdcba51f5 LibTLS+LibWeb: Decouple EC parameters from TLS::SupportedGroup
This is in preparation of the next commits to split the changes.
2024-11-25 13:38:38 +01:00
Andreas Kling
ed7b1caba2 LibWeb: Implement the exportKey algorithm for Ed25519 2024-11-24 23:28:23 +01:00
Andreas Kling
4d25369f29 LibWeb: Implement the importKey algorithm for Ed25519 2024-11-24 23:28:23 +01:00
Andreas Kling
8cb371b2ce LibWeb: Decode X25519 keys as base64url + throw on bogus key data
This makes the X25519 importKey tests from WPT actually run.
2024-11-24 23:28:23 +01:00
devgianlu
009f328308 LibWeb: Implement ECDH.generateKey 2024-11-21 11:45:22 +01:00
Shannon Booth
f87041bf3a LibGC+Everywhere: Factor out a LibGC from LibJS
Resulting in a massive rename across almost everywhere! Alongside the
namespace change, we now have the following names:

 * JS::NonnullGCPtr -> GC::Ref
 * JS::GCPtr -> GC::Ptr
 * JS::HeapFunction -> GC::Function
 * JS::CellImpl -> GC::Cell
 * JS::Handle -> GC::Root
2024-11-15 14:49:20 +01:00
Jelle Raaijmakers
f8c853712e LibWeb: Add some missing spec links to Crypto 2024-11-15 12:32:04 +01:00
Jelle Raaijmakers
b290c180e0 LibWeb: Move PBKDF2::import_key() up in the file
Let's try to keep algorithm implementations together. No functional
changes.
2024-11-15 12:32:04 +01:00
stelar7
5b67f17551 LibWeb: Sset the key_usages on X25519 export in a better way 2024-11-14 19:48:06 +01:00
stelar7
19ee8ddec2 LibWeb: Correctly set the key_usages on HMAC export 2024-11-14 19:48:06 +01:00
Jelle Raaijmakers
329cd946ac LibWeb: Implement Web Crypto HMAC algorithm
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
2024-11-14 11:52:18 +01:00
Jelle Raaijmakers
884a4163a0 LibWeb: Centralize validating a JWK's key_ops field
This gets rid of a couple FIXMEs and allows reusing the logic of
validating this field between different algorithms. While we're here,
expand its logic to match the constraints as outlined in RFC 7517.
2024-11-14 11:52:18 +01:00
Jelle Raaijmakers
f73a434177 LibWeb: Centralize getting the hash algorithm identifier for crypto 2024-11-14 11:52:18 +01:00