diff options
author | Luca Casonato <hello@lcas.dev> | 2021-09-13 11:35:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-13 11:35:49 +0200 |
commit | d0b5ff6db9ec42f645210de7c4806bad385bdfaf (patch) | |
tree | 18db6aa2147288c4c8db3b9c8b8f23d177d8013d /ext/crypto/00_crypto.js | |
parent | 2199bdaf64c59c69f53079362e902355325cfa37 (diff) |
feat(ext/crypto): generate ECDH keys (#11870)
Add support for ECDH algorithm in SubtleCrypto#generateKey.
Diffstat (limited to 'ext/crypto/00_crypto.js')
-rw-r--r-- | ext/crypto/00_crypto.js | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js index 68a8e4f9f..b3131a4f8 100644 --- a/ext/crypto/00_crypto.js +++ b/ext/crypto/00_crypto.js @@ -82,6 +82,7 @@ "RSA-PSS": "RsaHashedKeyGenParams", "RSA-OAEP": "RsaHashedKeyGenParams", "ECDSA": "EcKeyGenParams", + "ECDH": "EcKeyGenParams", "AES-CTR": "AesKeyGenParams", "AES-CBC": "AesKeyGenParams", "AES-GCM": "AesKeyGenParams", @@ -1575,7 +1576,64 @@ // 17-20. return { publicKey, privateKey }; } - // TODO(lucacasonato): ECDH + case "ECDH": { + // 1. + if ( + ArrayPrototypeFind( + usages, + (u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u), + ) !== undefined + ) { + throw new DOMException("Invalid key usages", "SyntaxError"); + } + + // 2-3. + const handle = {}; + if ( + ArrayPrototypeIncludes( + supportedNamedCurves, + normalizedAlgorithm.namedCurve, + ) + ) { + const keyData = await core.opAsync("op_crypto_generate_key", { + name: "ECDH", + namedCurve: normalizedAlgorithm.namedCurve, + }); + WeakMapPrototypeSet(KEY_STORE, handle, { + type: "pkcs8", + data: keyData, + }); + } else { + throw new DOMException("Curve not supported", "NotSupportedError"); + } + + // 4-6. + const algorithm = { + name: "ECDH", + namedCurve: normalizedAlgorithm.namedCurve, + }; + + // 7-11. + const publicKey = constructKey( + "public", + true, + usageIntersection(usages, []), + algorithm, + handle, + ); + + // 12-16. + const privateKey = constructKey( + "private", + extractable, + usageIntersection(usages, ["deriveKey", "deriveBits"]), + algorithm, + handle, + ); + + // 17-20. + return { publicKey, privateKey }; + } case "AES-CTR": case "AES-CBC": case "AES-GCM": { |