expose importSync and exportSync

This commit is contained in:
AJ ONeal 2018-11-23 12:18:13 -07:00
parent 03f4700554
commit 57edc8641b
2 changed files with 85 additions and 73 deletions

View file

@ -7,33 +7,39 @@ var x509 = require('./x509.js');
var ASN1 = require('./asn1.js'); var ASN1 = require('./asn1.js');
/*global Promise*/ /*global Promise*/
RSA.importSync = function (opts) {
if (!opts || !opts.pem || 'string' !== typeof opts.pem) {
throw new Error("must pass { pem: pem } as a string");
}
var jwk = { kty: 'RSA', n: null, e: null };
if (0 === opts.pem.indexOf('ssh-rsa ')) {
return SSH.parse(opts.pem, jwk);
}
var pem = opts.pem;
var block = PEM.parseBlock(pem);
//var hex = toHex(u8);
var asn1 = ASN1.parse(block.der);
var meta = x509.guess(block.der, asn1);
if ('pkcs1' === meta.format) {
jwk = x509.parsePkcs1(block.der, asn1, jwk);
} else {
jwk = x509.parsePkcs8(block.der, asn1, jwk);
}
if (opts.public) {
jwk = RSA.nueter(jwk);
}
return jwk;
};
RSA.parse = function parseRsa(opts) { RSA.parse = function parseRsa(opts) {
// wrapped in a promise for API compatibility
// with the forthcoming browser version
// (and potential future native node capability)
return Promise.resolve().then(function () { return Promise.resolve().then(function () {
if (!opts || !opts.pem || 'string' !== typeof opts.pem) { return RSA.importSync(opts);
throw new Error("must pass { pem: pem } as a string");
}
var jwk = { kty: 'RSA', n: null, e: null };
if (0 === opts.pem.indexOf('ssh-rsa ')) {
return SSH.parse(opts.pem, jwk);
}
var pem = opts.pem;
var block = PEM.parseBlock(pem);
//var hex = toHex(u8);
var asn1 = ASN1.parse(block.der);
var meta = x509.guess(block.der, asn1);
if ('pkcs1' === meta.format) {
jwk = x509.parsePkcs1(block.der, asn1, jwk);
} else {
jwk = x509.parsePkcs8(block.der, asn1, jwk);
}
if (opts.public) {
jwk = RSA.nueter(jwk);
}
return jwk;
}); });
}; };
RSA.toJwk = RSA.import = RSA.parse; RSA.toJwk = RSA.import = RSA.parse;
@ -53,57 +59,63 @@ RSAPrivateKey ::= SEQUENCE {
} }
*/ */
RSA.pack = function (opts) { RSA.exportSync = function (opts) {
return Promise.resolve().then(function () { if (!opts || !opts.jwk || 'object' !== typeof opts.jwk) {
if (!opts || !opts.jwk || 'object' !== typeof opts.jwk) { throw new Error("must pass { jwk: jwk }");
throw new Error("must pass { jwk: jwk }"); }
} var jwk = JSON.parse(JSON.stringify(opts.jwk));
var jwk = JSON.parse(JSON.stringify(opts.jwk)); var format = opts.format;
var format = opts.format; var pub = opts.public;
var pub = opts.public; if (pub || -1 !== [ 'spki', 'pkix', 'ssh', 'rfc4716' ].indexOf(format)) {
if (pub || -1 !== [ 'spki', 'pkix', 'ssh', 'rfc4716' ].indexOf(format)) { jwk = RSA.nueter(jwk);
jwk = RSA.nueter(jwk); }
} if ('RSA' !== jwk.kty) {
if ('RSA' !== jwk.kty) { throw new Error("options.jwk.kty must be 'RSA' for RSA keys");
throw new Error("options.jwk.kty must be 'RSA' for RSA keys"); }
} if (!jwk.p) {
if (!jwk.p) { // TODO test for n and e
// TODO test for n and e pub = true;
pub = true; if (!format || 'pkcs1' === format) {
if (!format || 'pkcs1' === format) { format = 'pkcs1';
format = 'pkcs1';
} else if (-1 !== [ 'spki', 'pkix' ].indexOf(format)) {
format = 'spki';
} else if (-1 !== [ 'ssh', 'rfc4716' ].indexOf(format)) {
format = 'ssh';
} else {
throw new Error("options.format must be 'spki', 'pkcs1', or 'ssh' for public RSA keys, not ("
+ typeof format + ") " + format);
}
} else {
// TODO test for all necessary keys (d, p, q ...)
if (!format || 'pkcs1' === format) {
format = 'pkcs1';
} else if ('pkcs8' !== format) {
throw new Error("options.format must be 'pkcs1' or 'pkcs8' for private RSA keys");
}
}
if ('pkcs1' === format) {
if (jwk.d) {
return PEM.packBlock({ type: "RSA PRIVATE KEY", bytes: x509.packPkcs1(jwk) });
} else {
return PEM.packBlock({ type: "RSA PUBLIC KEY", bytes: x509.packPkcs1(jwk) });
}
} else if ('pkcs8' === format) {
return PEM.packBlock({ type: "PRIVATE KEY", bytes: x509.packPkcs8(jwk) });
} else if (-1 !== [ 'spki', 'pkix' ].indexOf(format)) { } else if (-1 !== [ 'spki', 'pkix' ].indexOf(format)) {
return PEM.packBlock({ type: "PUBLIC KEY", bytes: x509.packSpki(jwk) }); format = 'spki';
} else if (-1 !== [ 'ssh', 'rfc4716' ].indexOf(format)) { } else if (-1 !== [ 'ssh', 'rfc4716' ].indexOf(format)) {
return SSH.pack({ jwk: jwk, comment: opts.comment }); format = 'ssh';
} else { } else {
throw new Error("Sanity Error: reached unreachable code block with format: " + format); throw new Error("options.format must be 'spki', 'pkcs1', or 'ssh' for public RSA keys, not ("
+ typeof format + ") " + format);
} }
} else {
// TODO test for all necessary keys (d, p, q ...)
if (!format || 'pkcs1' === format) {
format = 'pkcs1';
} else if ('pkcs8' !== format) {
throw new Error("options.format must be 'pkcs1' or 'pkcs8' for private RSA keys");
}
}
if ('pkcs1' === format) {
if (jwk.d) {
return PEM.packBlock({ type: "RSA PRIVATE KEY", bytes: x509.packPkcs1(jwk) });
} else {
return PEM.packBlock({ type: "RSA PUBLIC KEY", bytes: x509.packPkcs1(jwk) });
}
} else if ('pkcs8' === format) {
return PEM.packBlock({ type: "PRIVATE KEY", bytes: x509.packPkcs8(jwk) });
} else if (-1 !== [ 'spki', 'pkix' ].indexOf(format)) {
return PEM.packBlock({ type: "PUBLIC KEY", bytes: x509.packSpki(jwk) });
} else if (-1 !== [ 'ssh', 'rfc4716' ].indexOf(format)) {
return SSH.pack({ jwk: jwk, comment: opts.comment });
} else {
throw new Error("Sanity Error: reached unreachable code block with format: " + format);
}
};
RSA.pack = function (opts) {
// wrapped in a promise for API compatibility
// with the forthcoming browser version
// (and potential future native node capability)
return Promise.resolve().then(function () {
return RSA.exportSync(opts);
}); });
}; };
RSA.toPem = RSA.export = RSA.pack; RSA.toPem = RSA.export = RSA.pack;

View file

@ -1,6 +1,6 @@
{ {
"name": "rasha", "name": "rasha",
"version": "1.0.1", "version": "1.0.2",
"description": "💯 PEM-to-JWK and JWK-to-PEM for RSA keys in a lightweight, zero-dependency library focused on perfect universal compatibility.", "description": "💯 PEM-to-JWK and JWK-to-PEM for RSA keys in a lightweight, zero-dependency library focused on perfect universal compatibility.",
"homepage": "https://git.coolaj86.com/coolaj86/rasha.js", "homepage": "https://git.coolaj86.com/coolaj86/rasha.js",
"main": "index.js", "main": "index.js",