rsa-compat.js/lib/rsa-ursa.js

183 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-07-30 20:00:08 +00:00
'use strict';
var ursa;
try {
ursa = require('ursa');
} catch(e) {
try {
ursa = require('ursa-optional');
} catch(e2) {
throw e;
}
}
2016-07-30 20:00:08 +00:00
function notToJson() {
return undefined;
}
2016-07-30 23:09:37 +00:00
var ursac = module.exports = {
2016-07-30 20:00:08 +00:00
//
// to components
//
_privateJwkToComponents: function (jwk) {
var components = [];
[ 'n', 'e', 'p', 'q', 'dp', 'dq', 'qi', 'd' ].forEach(function (key) {
components.push(Buffer.from(jwk[key], 'base64'));
2016-07-30 20:00:08 +00:00
});
return components;
}
, _publicJwkToComponents: function (jwk) {
var components = [];
[ 'n', 'e' ].forEach(function (key) {
components.push(Buffer.from(jwk[key], 'base64'));
2016-07-30 20:00:08 +00:00
});
return components;
}
//
// Generate New Keypair
//
, generateKeypair: function (bitlen, exp, options, cb) {
2016-07-30 23:09:37 +00:00
var keypair = ursa.generatePrivateKey(bitlen || 2048, exp || 65537);
2016-07-30 20:00:08 +00:00
keypair.toJSON = notToJson;
cb(null, {
_ursa: keypair
});
}
//
2016-08-01 08:03:50 +00:00
// Import
2016-07-30 20:00:08 +00:00
//
2016-08-01 08:03:50 +00:00
, _ursaImportPem: function (keypair) {
if (keypair._ursa) {
return;
}
2016-07-30 20:00:08 +00:00
if (keypair.privateKeyPem) {
2016-08-01 08:03:50 +00:00
keypair._ursa = ursa.createPrivateKey(keypair.privateKeyPem);
keypair._ursa.toJSON = notToJson;
}
else if (keypair.publicKeyPem) {
ursac._ursaImportPublicPem(keypair);
}
}
, _ursaImportPublicPem: function (keypair) {
if (keypair._ursa || keypair._ursaPublic) {
return;
2016-07-30 20:00:08 +00:00
}
2016-08-01 08:03:50 +00:00
if (keypair.publicKeyPem) {
keypair._ursaPublic = ursa.createPublicKey(keypair.publicKeyPem);
keypair._ursaPublic.toJSON = notToJson;
}
}
, _ursaImportJwk: function (keypair) {
2016-07-30 20:00:08 +00:00
if (keypair._ursa) {
2016-08-01 08:03:50 +00:00
return;
2016-07-30 20:00:08 +00:00
}
if (keypair.privateKeyJwk) {
keypair._ursa = ursa.createPrivateKeyFromComponents.apply(
ursa
, ursac._privateJwkToComponents(keypair.privateKeyJwk)
);
keypair._ursa.toJSON = notToJson;
2016-08-01 08:03:50 +00:00
}
else if (keypair.publicKeyJwk) {
ursac._ursaImportPublicJwk(keypair);
}
}
, _ursaImportPublicJwk: function (keypair) {
if (keypair._ursa || keypair._ursaPublic) {
return;
}
2016-08-02 16:35:23 +00:00
if (keypair.publicKeyJwk) {
keypair._ursaPublic = ursa.createPublicKeyFromComponents.apply(
ursa
, ursac._publicJwkToComponents(keypair.publicKeyJwk)
);
keypair._ursaPublic.toJSON = notToJson;
}
2016-08-01 08:03:50 +00:00
}
, import: function (keypair) {
ursac._ursaImportJwk(keypair);
ursac._ursaImportPem(keypair);
return keypair;
}
//
// Export Public / Private PEMs
//
2016-08-02 20:42:44 +00:00
, _pemBinToPem: function (pem) {
return pem.toString('ascii').replace(/[\n\r]+/g, '\r\n');
}
2016-08-01 08:03:50 +00:00
, exportPrivatePem: function (keypair) {
if (keypair.privateKeyPem) {
return keypair.privateKeyPem;
}
if (keypair._ursa) {
2016-08-02 20:42:44 +00:00
return ursac._pemBinToPem(keypair._ursa.toPrivatePem());
2016-08-01 08:03:50 +00:00
}
if (keypair.privateKeyJwk) {
ursac._ursaImportJwk(keypair);
2016-07-30 20:00:08 +00:00
2016-08-02 20:42:44 +00:00
return ursac._pemBinToPem(keypair._ursa.toPrivatePem());
2016-07-30 20:00:08 +00:00
}
throw new Error("None of privateKeyPem, _ursa, or privateKeyJwk found. No way to export private key PEM");
}
2016-07-31 03:47:52 +00:00
, exportPublicPem: function (keypair) {
2016-07-30 20:00:08 +00:00
if (keypair.publicKeyPem) {
return keypair.publicKeyPem;
}
if (keypair._ursa || keypair._ursaPublic) {
2016-08-02 20:42:44 +00:00
return ursac._pemBinToPem((keypair._ursa || keypair._ursaPublic).toPublicPem());
2016-07-30 20:00:08 +00:00
}
if (keypair.publicKeyJwk) {
2016-08-01 08:03:50 +00:00
ursac._ursaImportPublicJwk(keypair);
2016-07-30 20:00:08 +00:00
2016-08-02 20:42:44 +00:00
return ursac._pemBinToPem(keypair._ursaPublic.toPublicPem());
2016-07-30 20:00:08 +00:00
}
if (keypair.privateKeyJwk) {
2016-08-01 08:03:50 +00:00
ursac._ursaImportJwk(keypair);
2016-07-30 20:00:08 +00:00
2016-08-02 20:42:44 +00:00
return ursac._pemBinToPem(keypair._ursa.toPublicPem());
2016-07-30 20:00:08 +00:00
}
if (keypair.privateKeyPem) {
2016-08-01 08:03:50 +00:00
ursac._ursaImportPem(keypair);
2016-07-30 20:00:08 +00:00
2016-08-02 20:42:44 +00:00
return ursac._pemBinToPem(keypair._ursa.toPublicPem());
2016-07-30 20:00:08 +00:00
}
throw new Error("None of publicKeyPem, _ursa, publicKeyJwk, privateKeyPem, or privateKeyJwk found. No way to export public key PEM");
}
//, exportPrivateKeyJwk: NOT IMPLEMENTED HERE
//, exportPublicKeyJwk: NOT IMPLEMENTED HERE
};