Skip to content

Commit f7d1310

Browse files
authored
Merge pull request #32 from ribizli/issue_31_private_pkcs8_trimming_fails
fix: 31
2 parents 58bdc86 + 3658d85 commit f7d1310

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/rsa/import_key.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function rsa_import_jwk(key: JSONWebKey): RSAKeyParams {
5757
* @param key
5858
*/
5959
function rsa_import_pem_cert(key: string): RSAKeyParams {
60-
const trimmedKey = key.substr(27, key.length - 53);
60+
const trimmedKey = key.substr(27, key.length - 52);
6161
const parseKey = ber_simple(
6262
ber_decode(base64_to_binary(trimmedKey)),
6363
) as RSACertKeyFormat;
@@ -76,7 +76,7 @@ function rsa_import_pem_cert(key: string): RSAKeyParams {
7676
* @param key PEM encoded key format
7777
*/
7878
function rsa_import_pem_private(key: string): RSAKeyParams {
79-
const trimmedKey = key.substr(31, key.length - 61);
79+
const trimmedKey = key.substr(31, key.length - 60);
8080
const parseKey = ber_simple(
8181
ber_decode(base64_to_binary(trimmedKey)),
8282
) as bigint[];
@@ -101,7 +101,7 @@ function rsa_import_pem_private(key: string): RSAKeyParams {
101101
* @param key PEM encoded key format
102102
*/
103103
function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams {
104-
const trimmedKey = key.substr(27, key.length - 57);
104+
const trimmedKey = key.substr(27, key.length - 52);
105105
const parseWrappedKey = ber_simple(
106106
ber_decode(base64_to_binary(trimmedKey)),
107107
) as [number, unknown, Uint8Array];
@@ -128,7 +128,7 @@ function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams {
128128
* @param key PEM encoded key format
129129
*/
130130
function rsa_import_pem_public(key: string): RSAKeyParams {
131-
const trimmedKey = key.substr(26, key.length - 51);
131+
const trimmedKey = key.substr(26, key.length - 50);
132132
const parseKey = ber_simple(
133133
ber_decode(base64_to_binary(trimmedKey)),
134134
) as RSAPublicKeyFormat;

tests/rsa/rsa.import_key.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ Deno.test("RSA - Import Private Key (PKCS8)", () => {
8686
const e = 65537n;
8787
const d =
8888
675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n;
89+
const qi =
90+
74289828655732991195558689791424564707132679819891037249694826356870666658448134417270855397604232230569020096065966539928979441113025455296436655960522783579897346034371763410999278044050901737497683356751868229820180356905837023964589114523481066796348442538273820591008174072287844578937802915493303718997n;
8991
assertEquals(key.e, e);
9092
assertEquals(key.n, n);
9193
assertEquals(key.d, d);
94+
assertEquals(key.qi, qi);
9295
assertEquals(key.length, 256);
9396

9497
// Testing key with trailing newline
@@ -97,3 +100,47 @@ Deno.test("RSA - Import Private Key (PKCS8)", () => {
97100
assertEquals(key2.n, n);
98101
assertEquals(key2.d, d);
99102
});
103+
104+
Deno.test("RSA - Import RSA Private Key (PEM)", () => {
105+
const raw = `-----BEGIN RSA PRIVATE KEY-----
106+
MIIEowIBAAKCAQEAlWeqIOFxsSwBx2cB5tuwLVDD+vvMBapZb7qdE69FdtWL8cz8
107+
Vo2r9vpiziDPLqWu6c73KXORIgh+fHXp5iybMoKqNK9gfFocx2PqwiFiX1dmEmcY
108+
86b0gB38yyaw6YV4KoJwjcRI0bLsgmAIhiyDCtIbmPdAOTDDAK4FM5MfJDHf36vx
109+
nfV7eYiPhezZcpSfmq5yXN9OqYBhytzTdhgV9b50rggdvNElF5GX0wXspMg2emKw
110+
KtF6ZwjVfSch6i0OBJoxIXNslKN2jUclzTAXXWcdxwixXMcGQlQKdLUJAHexK7FA
111+
+T+Q0fESCd365JUlFGQehJA8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HF
127+
U6KjwiGmTgo0qwd5jjB3SXEWl51aXa+P74UEA/btEEtm6ZqxHeupoP5bo88Iv8ph
128+
F4OZQQKBgGnKz25Q/hvwwJk1Ld7WcBi3SL8OAXg8gr88ZCcdWdOKzGR+9RBMgQzz
129+
AQcmTT7Y6U7s4AGRO9TIg9wVS8Y/x3ahI1FUEo7jlMoiSnuRohhduV4Xt/9VWsLq
130+
wnR367+l5s3vpUQx2bhDnFdRazPs5ykF5ZKT2y95VNgqYBhRSpRV
131+
-----END RSA PRIVATE KEY-----`;
132+
const key = RSA.importKey(raw);
133+
const n =
134+
18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n;
135+
136+
const e = 65537n;
137+
const d =
138+
675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n;
139+
const qi =
140+
74289828655732991195558689791424564707132679819891037249694826356870666658448134417270855397604232230569020096065966539928979441113025455296436655960522783579897346034371763410999278044050901737497683356751868229820180356905837023964589114523481066796348442538273820591008174072287844578937802915493303718997n;
141+
assertEquals(key.e, e);
142+
assertEquals(key.n, n);
143+
assertEquals(key.d, d);
144+
assertEquals(key.qi, qi);
145+
assertEquals(key.length, 256);
146+
});

0 commit comments

Comments
 (0)