You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(ext/crypto): move getPublicKey to SubtleCrypto and validate usages (#34913)
## Summary
[`getPublicKey`](https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-getPublicKey)
was incorrectly exposed as a method on `CryptoKey.prototype`. Per the
WICG WebCrypto Modern Algorithms spec it belongs on
`SubtleCrypto.prototype` as `getPublicKey(key, keyUsages)`, and it must
validate the requested public-key usages for the key's algorithm. It
also previously did no usage validation.
Before:
```js
const kp = await crypto.subtle.generateKey({ name: "ML-KEM-512" }, true, ["decapsulateBits"]);
kp.privateKey.getPublicKey(["sign"]); // returned a CryptoKey (wrong)
```
After:
```js
const kp = await crypto.subtle.generateKey({ name: "ML-KEM-512" }, true, ["decapsulateBits"]);
kp.privateKey.getPublicKey; // undefined -> TypeError when called
await crypto.subtle.getPublicKey(kp.privateKey, ["encapsulateBits"]); // CryptoKey
await crypto.subtle.getPublicKey(kp.privateKey, ["sign"]); // DOMException (SyntaxError)
```
## Changes
- Removed `getPublicKey()` from `CryptoKey` (so `key.getPublicKey` is
now `undefined`).
- Added async `SubtleCrypto.prototype.getPublicKey(key, keyUsages)`
following the spec step order:
- `NotSupportedError` for algorithms that cannot derive a public key
(symmetric/KDF),
- `InvalidAccessError` when the input is not a private key,
- `SyntaxError` when a requested usage is not valid for a public key of
the algorithm,
- otherwise derives an extractable public key with the requested usages.
- Implemented support for **all** asymmetric algorithms: RSA, EC
(ECDSA/ECDH), Ed25519, X25519, X448, ML-KEM and ML-DSA. RSA/EC reuse the
existing SPKI export path (which already derives the public key from the
private key) and re-import; Ed25519/X25519/X448 derive the raw public
key and re-import as a JWK. A new `op_crypto_x448_public_key` op derives
the X448 public key from its private key.
- Updated the `WebCryptoAPI` WPT expectations: `getPublicKey.tentative`
now passes fully, and the `getPublicKey` `idlharness` subtests are no
longer expected to fail.
## Tests
- Updated the existing ML-KEM/ML-DSA `getPublicKey` unit tests to the
new API and added invalid-usage / wrong-key-type rejection assertions.
- Added unit tests covering `getPublicKey` for the classical algorithms
(ECDSA, Ed25519, X25519, RSA-PSS) and the `NotSupportedError` case.
- The `getPublicKey.tentative` WPT test passes for every covered
algorithm (verified by replicating each subtest against the build).
Closes#34907Closesdenoland/divybot#495
Co-authored-by: divybot <divybot@users.noreply.github.com>
Co-authored-by: Divy Srivastava <me@littledivy.com>
0 commit comments