|
|
@ -5,8 +5,37 @@ var ASN1 = require('./asn1.js'); |
|
|
|
//var Enc = require('./encoding.js');
|
|
|
|
var PEM = require('./pem.js'); |
|
|
|
var SSH = require('./ssh.js'); |
|
|
|
var Enc = require('./encoding.js'); |
|
|
|
|
|
|
|
|
|
|
|
/*global Promise*/ |
|
|
|
RSA.parse = function parseRsa(opts) { |
|
|
|
return Promise.resolve().then(function () { |
|
|
|
if (!opts || !opts.pem || 'string' !== typeof opts.pem) { |
|
|
|
throw new Error("must pass { pem: pem } as a string"); |
|
|
|
} |
|
|
|
if (0 === opts.pem.indexOf('ssh-rsa ')) { |
|
|
|
return SSH.parse(opts.pem); |
|
|
|
} |
|
|
|
var pem = opts.pem; |
|
|
|
var block = PEM.parseBlock(pem); |
|
|
|
var asn1 = ASN1.parse(block.der); |
|
|
|
//var hex = toHex(u8);
|
|
|
|
var jwk = { kty: 'RSA' }; |
|
|
|
|
|
|
|
console.log(asn1); |
|
|
|
var len = asn1.children.length; |
|
|
|
if (2 === len || 9 === len) { |
|
|
|
jwk = RSA.parsePkcs1(block.der, asn1, jwk); |
|
|
|
} else { |
|
|
|
jwk = RSA.parsePkcs8(block.der, asn1, jwk); |
|
|
|
} |
|
|
|
|
|
|
|
return jwk; |
|
|
|
}); |
|
|
|
}; |
|
|
|
RSA.toJwk = RSA.import = RSA.parse; |
|
|
|
|
|
|
|
/* |
|
|
|
RSAPrivateKey ::= SEQUENCE { |
|
|
|
version Version, |
|
|
@ -22,38 +51,35 @@ RSAPrivateKey ::= SEQUENCE { |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
/*global Promise*/ |
|
|
|
RSA.parse = function parseEc(opts) { |
|
|
|
return Promise.resolve().then(function () { |
|
|
|
if (!opts || !opts.pem || 'string' !== typeof opts.pem) { |
|
|
|
throw new Error("must pass { pem: pem } as a string"); |
|
|
|
} |
|
|
|
if (0 === opts.pem.indexOf('ssh-rsa ')) { |
|
|
|
return SSH.parse(opts.pem); |
|
|
|
} |
|
|
|
var pem = opts.pem; |
|
|
|
var block = PEM.parseBlock(pem); |
|
|
|
//var hex = toHex(u8);
|
|
|
|
//var jwk = { kty: 'RSA' };
|
|
|
|
RSA.parsePkcs1 = function parseRsaPkcs1(buf, asn1, jwk) { |
|
|
|
if (!asn1.children.every(function(el) { |
|
|
|
return 0x02 === el.type; |
|
|
|
})) { |
|
|
|
throw new Error("not an RSA PKCS#1 public or private key (not all ints)"); |
|
|
|
} |
|
|
|
|
|
|
|
var asn1 = ASN1.parse(block.der); |
|
|
|
var ws = ''; |
|
|
|
function write(asn1) { |
|
|
|
console.log(ws, 'ch', Buffer.from([asn1.type]).toString('hex'), asn1.length); |
|
|
|
if (!asn1.children) { |
|
|
|
return; |
|
|
|
} |
|
|
|
asn1.children.forEach(function (a) { |
|
|
|
ws += '\t'; |
|
|
|
write(a); |
|
|
|
ws = ws.slice(1); |
|
|
|
}); |
|
|
|
} |
|
|
|
//console.log(JSON.stringify(asn1, null, 2));
|
|
|
|
console.log(asn1); |
|
|
|
write(asn1); |
|
|
|
if (2 === asn1.children.length) { |
|
|
|
|
|
|
|
return { kty: 'RSA' }; |
|
|
|
}); |
|
|
|
jwk.n = Enc.bufToUrlBase64(asn1.children[0].value); |
|
|
|
jwk.e = Enc.bufToUrlBase64(asn1.children[1].value); |
|
|
|
return jwk; |
|
|
|
|
|
|
|
} else if (asn1.children.length >= 9) { |
|
|
|
// the standard allows for "otherPrimeInfos", hence at least 9
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
return jwk; |
|
|
|
} else { |
|
|
|
throw new Error("not an RSA PKCS#1 public or private key (wrong number of ints)"); |
|
|
|
} |
|
|
|
}; |
|
|
|
RSA.parsePkcs8 = function parseRsaPkcs8(buf, asn1, jwk) { |
|
|
|
}; |
|
|
|
RSA.toJwk = RSA.import = RSA.parse; |
|
|
|