diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2021-09-13 15:03:28 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-13 11:33:28 +0200 |
commit | 2199bdaf64c59c69f53079362e902355325cfa37 (patch) | |
tree | baeca465179956e1a34d9501ab4e872d58db2488 /ext/crypto/00_crypto.js | |
parent | 84f874715763df71bb3bbf77f0714f8afdc17bb3 (diff) |
feat(ext/crypto): export RSA keys as pkcs#8 (#11880)
Diffstat (limited to 'ext/crypto/00_crypto.js')
-rw-r--r-- | ext/crypto/00_crypto.js | 95 |
1 files changed, 90 insertions, 5 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js index 2cb7a3bb2..68a8e4f9f 100644 --- a/ext/crypto/00_crypto.js +++ b/ext/crypto/00_crypto.js @@ -1001,7 +1001,6 @@ * @param {CryptoKey} key * @returns {Promise<any>} */ - // deno-lint-ignore require-await async exportKey(format, key) { webidl.assertBranded(this, SubtleCrypto); const prefix = "Failed to execute 'exportKey' on 'SubtleCrypto'"; @@ -1077,8 +1076,92 @@ // TODO(@littledivy): Redundant break but deno_lint complains without it break; } - // TODO(@littledivy): RSASSA-PKCS1-v1_5 - // TODO(@littledivy): RSA-PSS + case "RSASSA-PKCS1-v1_5": { + switch (format) { + case "pkcs8": { + // 1. + if (key[_type] !== "private") { + throw new DOMException( + "Key is not a private key", + "InvalidAccessError", + ); + } + + // 2. + const data = await core.opAsync( + "op_crypto_export_key", + { + key: innerKey, + format: "pkcs8", + algorithm: "RSASSA-PKCS1-v1_5", + }, + ); + + // 3. + return data.buffer; + } + default: + throw new DOMException("Not implemented", "NotSupportedError"); + } + } + case "RSA-PSS": { + switch (format) { + case "pkcs8": { + // 1. + if (key[_type] !== "private") { + throw new DOMException( + "Key is not a private key", + "InvalidAccessError", + ); + } + + // 2. + const data = await core.opAsync( + "op_crypto_export_key", + { + key: innerKey, + format: "pkcs8", + algorithm: "RSA-PSS", + hash: key[_algorithm].hash.name, + }, + ); + + // 3. + return data.buffer; + } + default: + throw new DOMException("Not implemented", "NotSupportedError"); + } + } + case "RSA-OAEP": { + switch (format) { + case "pkcs8": { + // 1. + if (key[_type] !== "private") { + throw new DOMException( + "Key is not a private key", + "InvalidAccessError", + ); + } + + // 2. + const data = await core.opAsync( + "op_crypto_export_key", + { + key: innerKey, + format: "pkcs8", + algorithm: "RSA-PSS", + hash: key[_algorithm].hash.name, + }, + ); + + // 3. + return data.buffer; + } + default: + throw new DOMException("Not implemented", "NotSupportedError"); + } + } // TODO(@littledivy): ECDSA default: throw new DOMException("Not implemented", "NotSupportedError"); @@ -1339,7 +1422,8 @@ ); const handle = {}; WeakMapPrototypeSet(KEY_STORE, handle, { - type: "pkcs8", + // PKCS#1 for RSA + type: "raw", data: keyData, }); @@ -1399,7 +1483,8 @@ ); const handle = {}; WeakMapPrototypeSet(KEY_STORE, handle, { - type: "pkcs8", + // PKCS#1 for RSA + type: "raw", data: keyData, }); |