This commit is contained in:
AJ ONeal 2018-12-01 16:30:05 -07:00
parent e11120ae29
commit 86956eb9d1
2 changed files with 11 additions and 39 deletions

View File

@ -243,7 +243,7 @@ EC.importSync = function importEcSync(opts) {
return EC.parseSsh(opts.pem); return EC.parseSsh(opts.pem);
} }
var pem = opts.pem; var pem = opts.pem;
var u8 = PEM.parseBlock(pem).der; var u8 = PEM.parseBlock(pem).bytes;
var hex = Enc.bufToHex(u8); var hex = Enc.bufToHex(u8);
var jwk = { kty: 'EC', crv: null, x: null, y: null }; var jwk = { kty: 'EC', crv: null, x: null, y: null };

View File

@ -3,52 +3,24 @@
var PEM = module.exports; var PEM = module.exports;
var Enc = require('./encoding.js'); var Enc = require('./encoding.js');
// TODO move object id hinting to x509.js
// 1.2.840.10045.3.1.7
// prime256v1 (ANSI X9.62 named elliptic curve)
var OBJ_ID_EC = '06 08 2A8648CE3D030107'.replace(/\s+/g, '').toLowerCase();
// 1.3.132.0.34
// secp384r1 (SECG (Certicom) named elliptic curve)
var OBJ_ID_EC_384 = '06 05 2B81040022'.replace(/\s+/g, '').toLowerCase();
PEM.parseBlock = function pemToDer(pem) { PEM.parseBlock = function pemToDer(pem) {
var typ; var lines = pem.trim().split(/\n/);
var pub; var end = lines.length - 1;
var crv; var head = lines[0].match(/-----BEGIN (.*)-----/);
var der = Buffer.from(pem.split(/\n/).filter(function (line, i) { var foot = lines[end].match(/-----END (.*)-----/);
if (0 === i) {
if (/ PUBLIC /.test(line)) {
pub = true;
} else if (/ PRIVATE /.test(line)) {
pub = false;
}
if (/ EC/.test(line)) {
typ = 'EC';
}
}
return !/---/.test(line);
}).join(''), 'base64');
if (!typ || 'EC' === typ) { if (head) {
var hex = Enc.bufToHex(der); lines = lines.slice(1, end);
if (-1 !== hex.indexOf(OBJ_ID_EC)) { head = head[1];
typ = 'EC'; if (head !== foot[1]) {
crv = 'P-256'; throw new Error("headers and footers do not match");
} else if (-1 !== hex.indexOf(OBJ_ID_EC_384)) {
typ = 'EC';
crv = 'P-384';
} else {
// TODO support P-384 as well (but probably nothing else)
console.warn("unsupported ec curve");
} }
} }
return { kty: typ, pub: pub, der: der, crv: crv }; return { type: head, bytes: Enc.base64ToBuf(lines.join('')) };
}; };
PEM.packBlock = function (opts) { PEM.packBlock = function (opts) {
// TODO allow for headers?
return '-----BEGIN ' + opts.type + '-----\n' return '-----BEGIN ' + opts.type + '-----\n'
+ Enc.bufToBase64(opts.bytes).match(/.{1,64}/g).join('\n') + '\n' + Enc.bufToBase64(opts.bytes).match(/.{1,64}/g).join('\n') + '\n'
+ '-----END ' + opts.type + '-----' + '-----END ' + opts.type + '-----'