summaryrefslogtreecommitdiff
path: root/cli/tests/unit/webcrypto_test.ts
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2021-09-13 15:03:28 +0530
committerGitHub <noreply@github.com>2021-09-13 11:33:28 +0200
commit2199bdaf64c59c69f53079362e902355325cfa37 (patch)
treebaeca465179956e1a34d9501ab4e872d58db2488 /cli/tests/unit/webcrypto_test.ts
parent84f874715763df71bb3bbf77f0714f8afdc17bb3 (diff)
feat(ext/crypto): export RSA keys as pkcs#8 (#11880)
Diffstat (limited to 'cli/tests/unit/webcrypto_test.ts')
-rw-r--r--cli/tests/unit/webcrypto_test.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts
index 57ab051d1..7e0f132e0 100644
--- a/cli/tests/unit/webcrypto_test.ts
+++ b/cli/tests/unit/webcrypto_test.ts
@@ -357,6 +357,50 @@ unitTest(async function subtleCryptoHmacImportExport() {
assertEquals(exportedKey2, jwk);
});
+// deno-fmt-ignore
+const asn1AlgorithmIdentifier = new Uint8Array([
+ 0x02, 0x01, 0x00, // INTEGER
+ 0x30, 0x0d, // SEQUENCE (2 elements)
+ 0x06, 0x09, // OBJECT IDENTIFIER
+ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, // 1.2.840.113549.1.1.1 (rsaEncryption)
+ 0x05, 0x00, // NULL
+]);
+
+unitTest(async function rsaExportPkcs8() {
+ for (const algorithm of ["RSASSA-PKCS1-v1_5", "RSA-PSS", "RSA-OAEP"]) {
+ const keyPair = await crypto.subtle.generateKey(
+ {
+ name: algorithm,
+ modulusLength: 2048,
+ publicExponent: new Uint8Array([1, 0, 1]),
+ hash: "SHA-256",
+ },
+ true,
+ algorithm !== "RSA-OAEP" ? ["sign", "verify"] : ["encrypt", "decrypt"],
+ );
+
+ assert(keyPair.privateKey);
+ assert(keyPair.publicKey);
+ assertEquals(keyPair.privateKey.extractable, true);
+
+ const exportedKey = await crypto.subtle.exportKey(
+ "pkcs8",
+ keyPair.privateKey,
+ );
+
+ assert(exportedKey);
+ assert(exportedKey instanceof ArrayBuffer);
+
+ const pkcs8 = new Uint8Array(exportedKey);
+ assert(pkcs8.length > 0);
+
+ assertEquals(
+ pkcs8.slice(4, asn1AlgorithmIdentifier.byteLength + 4),
+ asn1AlgorithmIdentifier,
+ );
+ }
+});
+
unitTest(async function testHkdfDeriveBits() {
const rawKey = await crypto.getRandomValues(new Uint8Array(16));
const key = await crypto.subtle.importKey(