|
|
@ -3,43 +3,24 @@ |
|
|
|
var PEM = module.exports; |
|
|
|
var Enc = require('./encoding.js'); |
|
|
|
|
|
|
|
PEM.RSA_OBJID = '06 09 2A864886F70D010101' |
|
|
|
.replace(/\s+/g, '').toLowerCase(); |
|
|
|
|
|
|
|
PEM.parseBlock = function pemToDer(pem) { |
|
|
|
var typ; |
|
|
|
var pub; |
|
|
|
var hex; |
|
|
|
var der = Enc.base64ToBuf(pem.split(/\n/).filter(function (line, i) { |
|
|
|
if (0 === i) { |
|
|
|
if (/ PUBLIC /.test(line)) { |
|
|
|
pub = true; |
|
|
|
} else if (/ PRIVATE /.test(line)) { |
|
|
|
pub = false; |
|
|
|
} |
|
|
|
if (/ RSA /.test(line)) { |
|
|
|
typ = 'RSA'; |
|
|
|
} |
|
|
|
} |
|
|
|
return !/---/.test(line); |
|
|
|
}).join('')); |
|
|
|
var lines = pem.trim().split(/\n/); |
|
|
|
var end = lines.length - 1; |
|
|
|
var head = lines[0].match(/-----BEGIN (.*)-----/); |
|
|
|
var foot = lines[end].match(/-----END (.*)-----/); |
|
|
|
|
|
|
|
if (!typ) { |
|
|
|
hex = Enc.bufToHex(der); |
|
|
|
if (-1 !== hex.indexOf(PEM.RSA_OBJID)) { |
|
|
|
typ = 'RSA'; |
|
|
|
if (head) { |
|
|
|
lines = lines.slice(1, end); |
|
|
|
head = head[1]; |
|
|
|
if (head !== foot[1]) { |
|
|
|
throw new Error("headers and footers do not match"); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!typ) { |
|
|
|
console.warn("Definitely not an RSA PKCS#8 because there's no RSA Object ID in the DER body."); |
|
|
|
console.warn("Probably not an RSA PKCS#1 because 'RSA' wasn't in the PEM type string."); |
|
|
|
} |
|
|
|
|
|
|
|
return { kty: typ, pub: pub, der: der }; |
|
|
|
return { type: head, bytes: Enc.base64ToBuf(lines.join('')) }; |
|
|
|
}; |
|
|
|
|
|
|
|
PEM.packBlock = function (opts) { |
|
|
|
// TODO allow for headers?
|
|
|
|
return '-----BEGIN ' + opts.type + '-----\n' |
|
|
|
+ Enc.bufToBase64(opts.bytes).match(/.{1,64}/g).join('\n') + '\n' |
|
|
|
+ '-----END ' + opts.type + '-----' |
|
|
|