From 6554a8278eeeb0c65c093da662f7bad97d38cee4 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 20 Nov 2018 09:53:52 -0700 Subject: [PATCH] output public jwk from private pem --- bin/eckles.js | 3 ++- lib/eckles.js | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/bin/eckles.js b/bin/eckles.js index 4e1829c..079dfdb 100755 --- a/bin/eckles.js +++ b/bin/eckles.js @@ -16,7 +16,8 @@ try { } if ('string' === typeof key) { - eckles.import({ pem: key }).then(function (jwk) { + var pub = (-1 !== [ 'public', 'spki', 'pkix' ].indexOf(format)); + eckles.import({ pem: key, public: (pub || format) }).then(function (jwk) { console.log(JSON.stringify(jwk, null, 2)); }).catch(function (err) { console.error(err); diff --git a/lib/eckles.js b/lib/eckles.js index d04d92d..fd60fc1 100644 --- a/lib/eckles.js +++ b/lib/eckles.js @@ -244,15 +244,15 @@ EC.parse = function parseEc(opts) { // PKCS8 if (0x02 === u8[3] && 0x30 === u8[6] && 0x06 === u8[8]) { //console.log("PKCS8", u8[3].toString(16), u8[6].toString(16), u8[8].toString(16)); - return EC.parsePkcs8(u8, jwk); + jwk = EC.parsePkcs8(u8, jwk); // EC-only } else if (0x02 === u8[2] && 0x04 === u8[5] && 0xA0 === u8[39]) { //console.log("EC---", u8[2].toString(16), u8[5].toString(16), u8[39].toString(16)); - return EC.parseSec1(u8, jwk); + jwk = EC.parseSec1(u8, jwk); // SPKI/PKIK (Public) } else if (0x30 === u8[2] && 0x06 === u8[4] && 0x06 === u8[13]) { //console.log("SPKI-", u8[2].toString(16), u8[4].toString(16), u8[13].toString(16)); - return EC.parseSpki(u8, jwk); + jwk = EC.parseSpki(u8, jwk); // Error } else { //console.log("PKCS8", u8[3].toString(16), u8[6].toString(16), u8[8].toString(16)); @@ -266,15 +266,15 @@ EC.parse = function parseEc(opts) { // PKCS8 if (0x02 === u8[3] && 0x30 === u8[6] && 0x06 === u8[8]) { //console.log("PKCS8", u8[3].toString(16), u8[6].toString(16), u8[8].toString(16)); - return EC.parsePkcs8(u8, jwk); + jwk = EC.parsePkcs8(u8, jwk); // EC-only } else if (0x02 === u8[3] && 0x04 === u8[6] && 0xA0 === u8[56]) { //console.log("EC---", u8[3].toString(16), u8[6].toString(16), u8[56].toString(16)); - return EC.parseSec1(u8, jwk); + jwk = EC.parseSec1(u8, jwk); // SPKI/PKIK (Public) } else if (0x30 === u8[2] && 0x06 === u8[4] && 0x06 === u8[13]) { //console.log("SPKI-", u8[2].toString(16), u8[4].toString(16), u8[13].toString(16)); - return EC.parseSpki(u8, jwk); + jwk = EC.parseSpki(u8, jwk); // Error } else { //console.log("PKCS8", u8[3].toString(16), u8[6].toString(16), u8[8].toString(16)); @@ -285,6 +285,14 @@ EC.parse = function parseEc(opts) { } else { throw new Error("Supported key types are P-256 and P-384"); } + if (opts.public) { + if (true !== opts.public) { + throw new Error("options.public must be either `true` or `false` not (" + + typeof opts.public + ") '" + opts.public + "'"); + } + delete jwk.d; + } + return jwk; }); }; EC.toJwk = EC.import = EC.parse;