rsa-compat.js/lib/rsa-forge.js

130 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-07-30 20:35:25 +00:00
'use strict';
var forge = require('node-forge');
var utils = require('./key-utils.js');
function notToJson() {
return undefined;
}
/*
function importPrivateKey(privateKey) {
return forge.pki.rsa.setPrivateKey(
base64ToBn(privateKey.n)
, base64ToBn(privateKey.e)
, base64ToBn(privateKey.d)
, base64ToBn(privateKey.p)
, base64ToBn(privateKey.q)
, base64ToBn(privateKey.dp)
, base64ToBn(privateKey.dq)
, base64ToBn(privateKey.qi)
);
}
function importPublicKey(publicKey) {
return forge.pki.rsa.setPublicKey(
base64ToBn(publicKey.n),
base64ToBn(publicKey.e));
}
*/
2016-07-30 23:09:37 +00:00
var forgec = module.exports = {
2016-07-30 20:35:25 +00:00
//
// to components
//
_base64ToBn: function (base64) {
return new forge.jsbn.BigInteger(utils.b64dec(base64).toString("hex"), 16);
}
, _privateJwkToComponents: function (jwk) {
var components = [];
2016-07-31 03:47:52 +00:00
// [ 'n', 'e', 'd', 'p', 'q', 'dP', 'dQ', 'qInv' ]
2016-07-30 20:35:25 +00:00
[ 'n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi' ].forEach(function (key) {
components.push(new forgec._base64tobin(jwk[key]));
});
return components;
}
, _publicJwkToComponents: function (jwk) {
var components = [];
[ 'n', 'e' ].forEach(function (key) {
components.push(new Buffer(jwk[key], 'base64'));
});
return components;
}
//
// Generate New Keypair
//
, generateKeypair: function (bitlen, exp, options, cb) {
var keypair = forge.pki.rsa.generateKeyPair({ bits: bitlen || 1024, e: exp || 0x10001 });
keypair.toJSON = notToJson;
cb(null, {
_forge: keypair
});
}
//
// Export Public / Private PEMs
//
2016-07-31 03:47:52 +00:00
, exportPrivatePem: function (keypair) {
2016-07-30 20:35:25 +00:00
if (keypair.privateKeyPem) {
return keypair.privateKeyPem;
}
if (keypair.privateKeyJwk && !(keypair._forge && keypair._forge.privateKey)) {
keypair._forge = {
privateKey: forge.pki.rsa.setPrivateKey.apply(
forge.pki.rsa
, forgec._privateJwkToComponents(keypair.privateKeyJwk)
)
};
keypair._forge.toJSON = notToJson;
}
if (keypair._forge && keypair._forge.privateKey) {
return forge.pki.privateKeyToPem(keypair._forge.privateKey);
}
throw new Error("None of privateKeyPem, _forge, 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:35:25 +00:00
if (keypair.publicKeyPem) {
return keypair.publicKeyPem;
}
if ((keypair.privateKeyJwk || keypair.publicKeyJwk)
&& !(keypair._forge && (keypair._forge.privateKey || keypair._forge.publicKey))
) {
keypair._forge = {
publicKey: forge.pki.rsa.setPublicKey.apply(
forge.pki.rsa
, forgec._publicJwkToComponents(keypair.publicKeyJwk)
)
};
keypair._forge.toJSON = notToJson;
}
if (keypair._forge) {
return forge.pki.publicKeyToPem(keypair._forge.publicKey || keypair._forge.privateKey);
}
throw new Error("None of publicKeyPem, _forge, publicKeyJwk, privateKeyPem, or privateKeyJwk found. No way to export public key PEM");
}
//, exportPrivateKeyJwk: NOT IMPLEMENTED HERE
//, exportPublicKeyJwk: NOT IMPLEMENTED HERE
};