From 0bda12f2f9eb8dede618314918ad8cdd91ad1ce9 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Sat, 5 Dec 2020 19:37:29 +0700 Subject: [PATCH 1/3] Add support import pkcs8 private key --- src/rsa/basic_encoding_rule.ts | 17 +++++---- src/rsa/import_key.ts | 38 +++++++++++++++++--- tests/rsa/rsa.import_key.test.ts | 59 +++++++++++++++++++++++++++----- 3 files changed, 92 insertions(+), 22 deletions(-) diff --git a/src/rsa/basic_encoding_rule.ts b/src/rsa/basic_encoding_rule.ts index a2d9833..6708bcf 100644 --- a/src/rsa/basic_encoding_rule.ts +++ b/src/rsa/basic_encoding_rule.ts @@ -23,7 +23,7 @@ type BasicEncodingSimpleValue = export function ber_decode( bytes: Uint8Array, from?: number, - to?: number, + to?: number ): BasicEncodingRule { return ber_next(bytes); } @@ -31,7 +31,7 @@ export function ber_decode( function ber_sequence( bytes: Uint8Array, from: number, - length: number, + length: number ): BasicEncodingRule[] { const end = from + length; let res: BasicEncodingRule[] = []; @@ -55,10 +55,7 @@ function ber_integer(bytes: Uint8Array, from: number, length: number): bigint { } function ber_oid(bytes: Uint8Array, from: number, length: number): string { - const id = [ - (bytes[from] / 40) | 0, - (bytes[from] % 40), - ]; + const id = [(bytes[from] / 40) | 0, bytes[from] % 40]; let value = 0; for (const b of bytes.slice(from + 1, from + length)) { @@ -76,7 +73,7 @@ function ber_oid(bytes: Uint8Array, from: number, length: number): string { function ber_unknown( bytes: Uint8Array, from: number, - length: number, + length: number ): Uint8Array { return bytes.slice(from, from + length); } @@ -89,7 +86,7 @@ export function ber_simple(n: BasicEncodingRule): BasicEncodingSimpleValue { function ber_next( bytes: Uint8Array, from?: number, - to?: number, + to?: number ): BasicEncodingRule { if (!from) from = 0; if (!to) to = bytes.length; @@ -120,12 +117,14 @@ function ber_next( value = null; } else if (type === 0x6) { value = ber_oid(bytes, ptr, size); + } else if (type === 0x4) { + value = ber_sequence(bytes, ptr, size); } else { value = ber_unknown(bytes, ptr, size); } return { - totalLength: (ptr - from) + size, + totalLength: ptr - from + size, type, length: size, value, diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index a8078c2..185ce84 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -7,7 +7,7 @@ import { os2ip } from "./primitives.ts"; type RSAImportKeyFormat = "auto" | "jwk" | "pem"; type RSAPublicKeyFormat = [[string, null], [[bigint, bigint]]]; type RSACertKeyFormat = [ - [number, string, null, null, null, RSAPublicKeyFormat], + [number, string, null, null, null, RSAPublicKeyFormat] ]; /** @@ -59,7 +59,7 @@ function rsa_import_jwk(key: JSONWebKey): RSAKeyParams { function rsa_import_pem_cert(key: string): RSAKeyParams { const trimmedKey = key.substr(27, key.length - 53); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)), + ber_decode(base64_to_binary(trimmedKey)) ) as RSACertKeyFormat; return { @@ -78,7 +78,7 @@ function rsa_import_pem_cert(key: string): RSAKeyParams { function rsa_import_pem_private(key: string): RSAKeyParams { const trimmedKey = key.substr(31, key.length - 61); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)), + ber_decode(base64_to_binary(trimmedKey)) ) as bigint[]; return { @@ -94,6 +94,33 @@ function rsa_import_pem_private(key: string): RSAKeyParams { }; } +/** + * Import private key from Privacy-Enhanced Mail (PEM) format + * https://tools.ietf.org/html/rfc5208 + * + * @param key PEM encoded key format + */ +function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams { + const trimmedKey = key.substr(27, key.length - 57); + const parseKey = ber_simple(ber_decode(base64_to_binary(trimmedKey))) as [ + number, + unknown, + [bigint[]] + ]; + + return { + n: parseKey[2][0][1], + d: parseKey[2][0][3], + e: parseKey[2][0][2], + p: parseKey[2][0][4], + q: parseKey[2][0][5], + dp: parseKey[2][0][6], + dq: parseKey[2][0][7], + qi: parseKey[2][0][8], + length: get_key_size(parseKey[2][0][1]), + }; +} + /** * Import public key from Privacy-Enhanced Mail (PEM) format * https://tools.ietf.org/html/rfc5208 @@ -103,7 +130,7 @@ function rsa_import_pem_private(key: string): RSAKeyParams { function rsa_import_pem_public(key: string): RSAKeyParams { const trimmedKey = key.substr(26, key.length - 51); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)), + ber_decode(base64_to_binary(trimmedKey)) ) as RSAPublicKeyFormat; return { @@ -124,6 +151,7 @@ function rsa_import_pem(key: string): RSAKeyParams { const maps: [string, (key: string) => RSAKeyParams][] = [ ["-----BEGIN RSA PRIVATE KEY-----", rsa_import_pem_private], + ["-----BEGIN PRIVATE KEY-----", rsa_import_pem_private_pkcs8], ["-----BEGIN PUBLIC KEY-----", rsa_import_pem_public], ["-----BEGIN CERTIFICATE-----", rsa_import_pem_cert], ]; @@ -143,7 +171,7 @@ function rsa_import_pem(key: string): RSAKeyParams { */ export function rsa_import_key( key: string | JSONWebKey, - format: RSAImportKeyFormat, + format: RSAImportKeyFormat ): RSAKeyParams { const finalFormat = format === "auto" ? detect_format(key) : format; diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index 7b11fbd..4fda13d 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -3,19 +3,19 @@ import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; Deno.test("RSA - Import JWK Public Key", () => { const jwk = { - "e": "AQAB", - "alg": "RS256", - "use": "sig", - "n": + e: "AQAB", + alg: "RS256", + use: "sig", + n: "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", - "kty": "RSA", - "kid": "0a7dc12664590c957ffaebf7b6718297b864ba91", + kty: "RSA", + kid: "0a7dc12664590c957ffaebf7b6718297b864ba91", }; const key = RSA.importKey(jwk); assertEquals( key.n, - 29898696334083768896266718702903171091859023056380305309417814312144807651236219776572988301924887627930068177446780412276940629516142064593112933354190594654697661829130606057587381851965706738909234655495626384372595795359236395774819608658749454999590004164863045288407450035910884781406353708688482649485901718330442366762092993760752462641504190676097552830866364434725980527790004239511223633036676476183317655075621501198026388523929916443949606011158134681907852424177605535928134115519418874993982126227290991413378693772648195647359413482755806577610702326142298069698977819919395238440777867763969032866121n, + 29898696334083768896266718702903171091859023056380305309417814312144807651236219776572988301924887627930068177446780412276940629516142064593112933354190594654697661829130606057587381851965706738909234655495626384372595795359236395774819608658749454999590004164863045288407450035910884781406353708688482649485901718330442366762092993760752462641504190676097552830866364434725980527790004239511223633036676476183317655075621501198026388523929916443949606011158134681907852424177605535928134115519418874993982126227290991413378693772648195647359413482755806577610702326142298069698977819919395238440777867763969032866121n ); assertEquals(key.e, 65537n); assertEquals(key.length, 256); @@ -45,7 +45,50 @@ Deno.test("RSA - Import Certificate PEM", () => { assertEquals(key.e, 65537n); assertEquals( key.n, - 23090961311737573793101890708283924695433283000385054150823173852612673758230706931831331316346585281948637691806795052569468114184708524749728610979168598273283877538163829031755269185449374762314082288398270538054993384545276491531066649969558630423935152857250299602221281913410774031127740158653973996811337367884788028635132184093457045502358896056164012639638637637694971273993250785116955454220202034598919946428438516582026303702956399809398972276905876470428715345135826409789537975085486698172979761097583691702126455811212002371429452274581681294432992139097164326433615550289131259416635196384643694156981n, + 23090961311737573793101890708283924695433283000385054150823173852612673758230706931831331316346585281948637691806795052569468114184708524749728610979168598273283877538163829031755269185449374762314082288398270538054993384545276491531066649969558630423935152857250299602221281913410774031127740158653973996811337367884788028635132184093457045502358896056164012639638637637694971273993250785116955454220202034598919946428438516582026303702956399809398972276905876470428715345135826409789537975085486698172979761097583691702126455811212002371429452274581681294432992139097164326433615550289131259416635196384643694156981n + ); + assertEquals(key.length, 256); +}); + +Deno.test("RSA - Import Private Key (PKCS8)", () => { + const raw = `-----BEGIN PRIVATE KEY----- + MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVZ6og4XGxLAHH + ZwHm27AtUMP6+8wFqllvup0Tr0V21YvxzPxWjav2+mLOIM8upa7pzvcpc5EiCH58 + denmLJsygqo0r2B8WhzHY+rCIWJfV2YSZxjzpvSAHfzLJrDphXgqgnCNxEjRsuyC + YAiGLIMK0huY90A5MMMArgUzkx8kMd/fq/Gd9Xt5iI+F7NlylJ+arnJc306pgGHK + 3NN2GBX1vnSuCB280SUXkZfTBeykyDZ6YrAq0XpnCNV9JyHqLQ4EmjEhc2yUo3aN + RyXNMBddZx3HCLFcxwZCVAp0tQkAd7ErsUD5P5DR8RIJ3frklSUUZB6EkDw8G7KL + WKVcNQ2FAgMBAAECggEABVqkmOQ4s2PzlgkDDdofxJFL+KhXMyPGlo+uNd32hA/v + 4YTaTSLAv8t7c3jEW/RW4ezyDrj5bD9i2FnPEu8pyk6u7qTon67eBaeMapO7h1Wl + 5DEazo5+3WwrBT+4ICI8QG+8vK6E+ojU23DkFU+D4a/OX+C1GzzmsWqgZ+z/inKL + aMwS6rsWROUlgA1x5zNSoqC3SJFllVXXew5UyANTsBjvQ5J2Mmb9g8CEaAOn+xMk + xNlwTlDot+09nFJ9vvAWirUkZdsYNnlY+zrDQoPT7Am5jtKTHmMI1KfWNfmznyji + RltdnWfd8Bg0H7VtwrhWMWGs8QkATUAE+fC3qS144QKBgQDNFLk8eMwK6koMoNmY + vsRAf1Y6Xdd+LUN+TJqkZiUEySaKy8NLi71IBbzK7zKqbYXDPUFCbNJFhOlluKOw + bsKPkwBjR0JfoXVzbUdE+2tM2YccswaJe7iinZxr0wHGXIvJL0HNVIzmNggS7+Zm + rEsdCyFXnTT/83o1wVW3E/lspQKBgQC6gBVyYCP7AxYHTSRDCgcpsTO7HyzuY92t + aYOhRJFC7HNbzJbVdVsfnbtQH/Epe8ySk91GyFZ1dSJrxetcLhHsS2TMrkYUmY4x + bJnqzJTh7cgWBP9DuWYvSJxPooSpFwbh2L12vQ2ADpwwoAZMLkAagaGlY9MZz821 + XY+KkOLnYQKBgQCQ49lTTgzqkUirz2Cst+qznsNvDSnYbWZH7xs6lygET5E5cmiS + ETIzlkoiHgjvu91LaRWYNoYAs7yqL18Godo30aXufkP4iHwQht5ZcEAI1Y7NyfYO + YCi8SxpeW3/fgzcHdqnIxbmeVAI0TuW7GHMhG+H8oob1ZjGrlOJYLHaGOQKBgDLO + vgkAxAyYFKI8k8pnqvfivJMXtSfksPmTKzb99QzkWbEClXzlkcOVNvhnG04P2fV8 + ruWfol4xYQU3UB02t89F4toYCCOIicJRMcVToqPCIaZOCjSrB3mOMHdJcRaXnVpd + r4/vhQQD9u0QS2bpmrEd66mg/lujzwi/ymEXg5lBAoGAacrPblD+G/DAmTUt3tZw + GLdIvw4BeDyCvzxkJx1Z04rMZH71EEyBDPMBByZNPtjpTuzgAZE71MiD3BVLxj/H + dqEjUVQSjuOUyiJKe5GiGF25Xhe3/1VawurCdHfrv6Xmze+lRDHZuEOcV1FrM+zn + KQXlkpPbL3lU2CpgGFFKlFU= + -----END PRIVATE KEY-----`; + + const key = RSA.importKey(raw); + assertEquals(key.e, 65537n); + assertEquals( + key.n, + 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n + ); + assertEquals( + key.d, + 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n ); assertEquals(key.length, 256); }); From 2dd64c522ed7e6193192146f9ba93f109a95bdcf Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Sat, 5 Dec 2020 19:38:51 +0700 Subject: [PATCH 2/3] Fixing deno fmt --- src/rsa/basic_encoding_rule.ts | 8 ++++---- src/rsa/import_key.ts | 12 ++++++------ tests/rsa/rsa.import_key.test.ts | 11 +++++------ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/rsa/basic_encoding_rule.ts b/src/rsa/basic_encoding_rule.ts index 6708bcf..b1a109d 100644 --- a/src/rsa/basic_encoding_rule.ts +++ b/src/rsa/basic_encoding_rule.ts @@ -23,7 +23,7 @@ type BasicEncodingSimpleValue = export function ber_decode( bytes: Uint8Array, from?: number, - to?: number + to?: number, ): BasicEncodingRule { return ber_next(bytes); } @@ -31,7 +31,7 @@ export function ber_decode( function ber_sequence( bytes: Uint8Array, from: number, - length: number + length: number, ): BasicEncodingRule[] { const end = from + length; let res: BasicEncodingRule[] = []; @@ -73,7 +73,7 @@ function ber_oid(bytes: Uint8Array, from: number, length: number): string { function ber_unknown( bytes: Uint8Array, from: number, - length: number + length: number, ): Uint8Array { return bytes.slice(from, from + length); } @@ -86,7 +86,7 @@ export function ber_simple(n: BasicEncodingRule): BasicEncodingSimpleValue { function ber_next( bytes: Uint8Array, from?: number, - to?: number + to?: number, ): BasicEncodingRule { if (!from) from = 0; if (!to) to = bytes.length; diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index 185ce84..c213d32 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -7,7 +7,7 @@ import { os2ip } from "./primitives.ts"; type RSAImportKeyFormat = "auto" | "jwk" | "pem"; type RSAPublicKeyFormat = [[string, null], [[bigint, bigint]]]; type RSACertKeyFormat = [ - [number, string, null, null, null, RSAPublicKeyFormat] + [number, string, null, null, null, RSAPublicKeyFormat], ]; /** @@ -59,7 +59,7 @@ function rsa_import_jwk(key: JSONWebKey): RSAKeyParams { function rsa_import_pem_cert(key: string): RSAKeyParams { const trimmedKey = key.substr(27, key.length - 53); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)) + ber_decode(base64_to_binary(trimmedKey)), ) as RSACertKeyFormat; return { @@ -78,7 +78,7 @@ function rsa_import_pem_cert(key: string): RSAKeyParams { function rsa_import_pem_private(key: string): RSAKeyParams { const trimmedKey = key.substr(31, key.length - 61); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)) + ber_decode(base64_to_binary(trimmedKey)), ) as bigint[]; return { @@ -105,7 +105,7 @@ function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams { const parseKey = ber_simple(ber_decode(base64_to_binary(trimmedKey))) as [ number, unknown, - [bigint[]] + [bigint[]], ]; return { @@ -130,7 +130,7 @@ function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams { function rsa_import_pem_public(key: string): RSAKeyParams { const trimmedKey = key.substr(26, key.length - 51); const parseKey = ber_simple( - ber_decode(base64_to_binary(trimmedKey)) + ber_decode(base64_to_binary(trimmedKey)), ) as RSAPublicKeyFormat; return { @@ -171,7 +171,7 @@ function rsa_import_pem(key: string): RSAKeyParams { */ export function rsa_import_key( key: string | JSONWebKey, - format: RSAImportKeyFormat + format: RSAImportKeyFormat, ): RSAKeyParams { const finalFormat = format === "auto" ? detect_format(key) : format; diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index 4fda13d..6c6a6e8 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -6,8 +6,7 @@ Deno.test("RSA - Import JWK Public Key", () => { e: "AQAB", alg: "RS256", use: "sig", - n: - "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", + n: "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", kty: "RSA", kid: "0a7dc12664590c957ffaebf7b6718297b864ba91", }; @@ -15,7 +14,7 @@ Deno.test("RSA - Import JWK Public Key", () => { const key = RSA.importKey(jwk); assertEquals( key.n, - 29898696334083768896266718702903171091859023056380305309417814312144807651236219776572988301924887627930068177446780412276940629516142064593112933354190594654697661829130606057587381851965706738909234655495626384372595795359236395774819608658749454999590004164863045288407450035910884781406353708688482649485901718330442366762092993760752462641504190676097552830866364434725980527790004239511223633036676476183317655075621501198026388523929916443949606011158134681907852424177605535928134115519418874993982126227290991413378693772648195647359413482755806577610702326142298069698977819919395238440777867763969032866121n + 29898696334083768896266718702903171091859023056380305309417814312144807651236219776572988301924887627930068177446780412276940629516142064593112933354190594654697661829130606057587381851965706738909234655495626384372595795359236395774819608658749454999590004164863045288407450035910884781406353708688482649485901718330442366762092993760752462641504190676097552830866364434725980527790004239511223633036676476183317655075621501198026388523929916443949606011158134681907852424177605535928134115519418874993982126227290991413378693772648195647359413482755806577610702326142298069698977819919395238440777867763969032866121n, ); assertEquals(key.e, 65537n); assertEquals(key.length, 256); @@ -45,7 +44,7 @@ Deno.test("RSA - Import Certificate PEM", () => { assertEquals(key.e, 65537n); assertEquals( key.n, - 23090961311737573793101890708283924695433283000385054150823173852612673758230706931831331316346585281948637691806795052569468114184708524749728610979168598273283877538163829031755269185449374762314082288398270538054993384545276491531066649969558630423935152857250299602221281913410774031127740158653973996811337367884788028635132184093457045502358896056164012639638637637694971273993250785116955454220202034598919946428438516582026303702956399809398972276905876470428715345135826409789537975085486698172979761097583691702126455811212002371429452274581681294432992139097164326433615550289131259416635196384643694156981n + 23090961311737573793101890708283924695433283000385054150823173852612673758230706931831331316346585281948637691806795052569468114184708524749728610979168598273283877538163829031755269185449374762314082288398270538054993384545276491531066649969558630423935152857250299602221281913410774031127740158653973996811337367884788028635132184093457045502358896056164012639638637637694971273993250785116955454220202034598919946428438516582026303702956399809398972276905876470428715345135826409789537975085486698172979761097583691702126455811212002371429452274581681294432992139097164326433615550289131259416635196384643694156981n, ); assertEquals(key.length, 256); }); @@ -84,11 +83,11 @@ Deno.test("RSA - Import Private Key (PKCS8)", () => { assertEquals(key.e, 65537n); assertEquals( key.n, - 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n + 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n, ); assertEquals( key.d, - 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n + 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n, ); assertEquals(key.length, 256); }); From e7ee1f37d54a384d07dccab635958fa6401d753b Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Sat, 5 Dec 2020 20:42:31 +0700 Subject: [PATCH 3/3] Add correct prettier setting --- .prettierrc.json | 3 +++ .vscode/settings.json | 10 ++++------ src/rsa/basic_encoding_rule.ts | 2 -- src/rsa/import_key.ts | 28 ++++++++++++++-------------- 4 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 .prettierrc.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..bf357fb --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "trailingComma": "all" +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 521eda4..a08982f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,5 @@ { - "deno.enableFormatter": true, - "editor.formatOnSave": true, - "editor.tabSize": 2, - "deno.enable": true, - "files.eol": "\n" -} \ No newline at end of file + "editor.tabSize": 2, + "deno.enable": true, + "files.eol": "\n", +} diff --git a/src/rsa/basic_encoding_rule.ts b/src/rsa/basic_encoding_rule.ts index b1a109d..c2fda24 100644 --- a/src/rsa/basic_encoding_rule.ts +++ b/src/rsa/basic_encoding_rule.ts @@ -117,8 +117,6 @@ function ber_next( value = null; } else if (type === 0x6) { value = ber_oid(bytes, ptr, size); - } else if (type === 0x4) { - value = ber_sequence(bytes, ptr, size); } else { value = ber_unknown(bytes, ptr, size); } diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index c213d32..10e60ab 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -102,22 +102,22 @@ function rsa_import_pem_private(key: string): RSAKeyParams { */ function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams { const trimmedKey = key.substr(27, key.length - 57); - const parseKey = ber_simple(ber_decode(base64_to_binary(trimmedKey))) as [ - number, - unknown, - [bigint[]], - ]; + const parseWrappedKey = ber_simple( + ber_decode(base64_to_binary(trimmedKey)), + ) as [number, unknown, Uint8Array]; + + const parseKey = ber_simple(ber_decode(parseWrappedKey[2])) as bigint[]; return { - n: parseKey[2][0][1], - d: parseKey[2][0][3], - e: parseKey[2][0][2], - p: parseKey[2][0][4], - q: parseKey[2][0][5], - dp: parseKey[2][0][6], - dq: parseKey[2][0][7], - qi: parseKey[2][0][8], - length: get_key_size(parseKey[2][0][1]), + n: parseKey[1], + d: parseKey[3], + e: parseKey[2], + p: parseKey[4], + q: parseKey[5], + dp: parseKey[6], + dq: parseKey[7], + qi: parseKey[8], + length: get_key_size(parseKey[1]), }; }