From bc838df0d1a0254396e4f04e655cbc61ca6bb590 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 9 Dec 2018 21:03:38 -0700 Subject: [PATCH] v1.2.3: bugfix RSA swapped n and e values (private key) --- lib/ssh-parser.js | 9 +++++++-- package.json | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ssh-parser.js b/lib/ssh-parser.js index f15bcb1..009ba67 100644 --- a/lib/ssh-parser.js +++ b/lib/ssh-parser.js @@ -9,6 +9,11 @@ SSH.parse = function (opts) { var ssh = SSH.parseBlock(pub); if ('OPENSSH PRIVATE KEY' === ssh.type) { ssh = SSH.parsePrivateElements(ssh); + if (7 === ssh.elements.length) { + // RSA Private Keys have the `e` and `n` swapped (which is actually more normal) + // but we have to reswap them to make them consistent with the public key format + ssh.elements.splice(1, 0, ssh.elements.splice(2 ,1)[0]); + } if (opts.public) { ssh.elements = ssh.elements.slice(0, 3); } @@ -155,15 +160,15 @@ SSH.parsePublicKey = function (ssh) { if (3 === els.length) { ssh.jwk = { kty: 'RSA' - , n: Enc.bufToUrlBase64(els[2]) , e: Enc.bufToUrlBase64(els[1]) + , n: Enc.bufToUrlBase64(els[2]) }; } else { console.log('len:', els.length); ssh.jwk = { kty: 'RSA' - , n: Enc.bufToUrlBase64(els[2]) , e: Enc.bufToUrlBase64(els[1]) + , n: Enc.bufToUrlBase64(els[2]) , d: Enc.bufToUrlBase64(els[3]) , p: Enc.bufToUrlBase64(els[5]) , q: Enc.bufToUrlBase64(els[6]) diff --git a/package.json b/package.json index 3790693..22cc29e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ssh-to-jwk", - "version": "1.2.2", + "version": "1.2.3", "description": "💯 SSH to JWK in a lightweight, zero-dependency library.", "homepage": "https://git.coolaj86.com/coolaj86/ssh-to-jwk.js", "main": "index.js",