handles all known pem formats

This commit is contained in:
AJ ONeal 2018-11-22 02:36:21 -07:00
parent 66f245681e
commit 5882ce82e7
3 changed files with 31 additions and 4 deletions

View file

@ -18,14 +18,14 @@ try {
if ('string' === typeof key) { if ('string' === typeof key) {
var pub = (-1 !== [ 'public', 'spki', 'pkix' ].indexOf(format)); var pub = (-1 !== [ 'public', 'spki', 'pkix' ].indexOf(format));
Rasha.import({ pem: key, public: (pub || format) }).then(function (jwk) { Rasha.import({ pem: key, public: (pub || format) }).then(function (jwk) {
console.log(JSON.stringify(jwk, null, 2)); console.info(JSON.stringify(jwk, null, 2));
}).catch(function (err) { }).catch(function (err) {
console.error(err); console.error(err);
process.exit(1); process.exit(1);
}); });
} else { } else {
Rasha.export({ jwk: key, format: format }).then(function (pem) { Rasha.export({ jwk: key, format: format }).then(function (pem) {
console.log(pem); console.info(pem);
}).catch(function (err) { }).catch(function (err) {
console.error(err); console.error(err);
process.exit(2); process.exit(2);

View file

@ -19,7 +19,7 @@ RSA.parse = function parseRsa(opts) {
var pem = opts.pem; var pem = opts.pem;
var block = PEM.parseBlock(pem); var block = PEM.parseBlock(pem);
//var hex = toHex(u8); //var hex = toHex(u8);
var jwk = { kty: 'RSA' }; var jwk = { kty: 'RSA', n: null, e: null };
var asn1 = ASN1.parse(block.der); var asn1 = ASN1.parse(block.der);
var meta = x509.guess(block.der, asn1); var meta = x509.guess(block.der, asn1);
@ -82,6 +82,31 @@ RSA.parsePkcs1 = function parseRsaPkcs1(buf, asn1, jwk) {
}; };
RSA.parsePkcs8 = function parseRsaPkcs8(buf, asn1, jwk) { RSA.parsePkcs8 = function parseRsaPkcs8(buf, asn1, jwk) {
console.log(asn1); if (2 === asn1.children.length
&& 0x03 === asn1.children[1].type
&& 0x30 === asn1.children[1].value[0]) {
asn1 = ASN1.parse(asn1.children[1].value);
jwk.n = Enc.bufToUrlBase64(asn1.children[0].value);
jwk.e = Enc.bufToUrlBase64(asn1.children[1].value);
} else if (3 === asn1.children.length
&& 0x04 === asn1.children[2].type
&& 0x30 === asn1.children[2].children[0].type
&& 0x02 === asn1.children[2].children[0].children[0].type) {
asn1 = asn1.children[2].children[0];
jwk.n = Enc.bufToUrlBase64(asn1.children[1].value);
jwk.e = Enc.bufToUrlBase64(asn1.children[2].value);
jwk.d = Enc.bufToUrlBase64(asn1.children[3].value);
jwk.p = Enc.bufToUrlBase64(asn1.children[4].value);
jwk.q = Enc.bufToUrlBase64(asn1.children[5].value);
jwk.dp = Enc.bufToUrlBase64(asn1.children[6].value);
jwk.dq = Enc.bufToUrlBase64(asn1.children[7].value);
jwk.qi = Enc.bufToUrlBase64(asn1.children[8].value);
} else {
throw new Error("not an RSA PKCS#8 public or private key (wrong format)");
}
return jwk; return jwk;
}; };

View file

@ -23,6 +23,8 @@ x509.guess = function (der, asn1) {
} else { } else {
throw new Error("not an RSA PKCS#1 public or private key (wrong number of ints)"); throw new Error("not an RSA PKCS#1 public or private key (wrong number of ints)");
} }
} else {
meta.format = 'pkcs8';
} }
return meta; return meta;