diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2021-09-14 18:51:20 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 15:21:20 +0200 |
commit | c41460ecc421ac7730cc5455542e5e05f7366c4a (patch) | |
tree | 18b7ac8683d6543b0610c25d42df089e2bf7d183 /cli/tests/unit/webcrypto_test.ts | |
parent | d36b01ff6956930b51a80cd773f618b708a5f595 (diff) |
feat(ext/crypto): import RSA pkcs#8 keys (#11891)
Diffstat (limited to 'cli/tests/unit/webcrypto_test.ts')
-rw-r--r-- | cli/tests/unit/webcrypto_test.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts index 7e0f132e0..2dda71e6f 100644 --- a/cli/tests/unit/webcrypto_test.ts +++ b/cli/tests/unit/webcrypto_test.ts @@ -357,6 +357,49 @@ unitTest(async function subtleCryptoHmacImportExport() { assertEquals(exportedKey2, jwk); }); +// 2048-bits publicExponent=65537 +const pkcs8TestVectors = [ + // rsaEncryption + "cli/tests/testdata/webcrypto/id_rsaEncryption.pem", + // id-RSASSA-PSS + "cli/tests/testdata/webcrypto/id_rsassaPss.pem", +]; + +unitTest({ perms: { read: true } }, async function importRsaPkcs8() { + const pemHeader = "-----BEGIN PRIVATE KEY-----"; + const pemFooter = "-----END PRIVATE KEY-----"; + for (const keyFile of pkcs8TestVectors) { + const pem = await Deno.readTextFile(keyFile); + const pemContents = pem.substring( + pemHeader.length, + pem.length - pemFooter.length, + ); + const binaryDerString = atob(pemContents); + const binaryDer = new Uint8Array(binaryDerString.length); + for (let i = 0; i < binaryDerString.length; i++) { + binaryDer[i] = binaryDerString.charCodeAt(i); + } + + const key = await crypto.subtle.importKey( + "pkcs8", + binaryDer, + { name: "RSA-PSS", hash: "SHA-256" }, + true, + ["sign"], + ); + + assert(key); + assertEquals(key.type, "private"); + assertEquals(key.extractable, true); + assertEquals(key.usages, ["sign"]); + const algorithm = key.algorithm as RsaHashedKeyAlgorithm; + assertEquals(algorithm.name, "RSA-PSS"); + assertEquals(algorithm.hash.name, "SHA-256"); + assertEquals(algorithm.modulusLength, 2048); + assertEquals(algorithm.publicExponent, new Uint8Array([1, 0, 1])); + } +}); + // deno-fmt-ignore const asn1AlgorithmIdentifier = new Uint8Array([ 0x02, 0x01, 0x00, // INTEGER |