168 lines
4.0 KiB
JavaScript
168 lines
4.0 KiB
JavaScript
'use strict';
|
|
|
|
var forge = require('node-forge');
|
|
|
|
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));
|
|
}
|
|
*/
|
|
|
|
var forgec = module.exports = {
|
|
|
|
|
|
|
|
//
|
|
// to components
|
|
//
|
|
_toStandardBase64: function (str) {
|
|
var b64 = str.replace(/-/g, "+").replace(/_/g, "/").replace(/=/g, "");
|
|
|
|
switch (b64.length % 4) {
|
|
case 2: b64 += "=="; break;
|
|
case 3: b64 += "="; break;
|
|
}
|
|
|
|
return b64;
|
|
}
|
|
, _base64ToBin: function (base64) {
|
|
var std64 = forgec._toStandardBase64(base64);
|
|
var hex = new Buffer(std64, 'base64').toString("hex");
|
|
|
|
return new forge.jsbn.BigInteger(hex, 16);
|
|
}
|
|
, _privateJwkToComponents: function (jwk) {
|
|
var components = [];
|
|
|
|
// [ 'n', 'e', 'd', 'p', 'q', 'dP', 'dQ', 'qInv' ]
|
|
[ 'n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi' ].forEach(function (key) {
|
|
components.push(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 fkeypair = forge.pki.rsa.generateKeyPair({ bits: bitlen || 1024, e: exp || 0x10001 });
|
|
|
|
fkeypair.toJSON = notToJson;
|
|
|
|
cb(null, {
|
|
_forge: fkeypair.privateKey
|
|
, _forgePublic: fkeypair.publicKey
|
|
});
|
|
}
|
|
|
|
|
|
|
|
//
|
|
// Import (no-op)
|
|
//
|
|
, _forgeImportJwk: function (keypair) {
|
|
if (!keypair._forge && keypair.privateKeyJwk) {
|
|
keypair._forge = forge.pki.rsa.setPrivateKey.apply(
|
|
forge.pki.rsa
|
|
, forgec._privateJwkToComponents(keypair.privateKeyJwk)
|
|
);
|
|
}
|
|
keypair._forge.toJSON = notToJson;
|
|
|
|
forgec._forgeImportPublicJwk(keypair);
|
|
}
|
|
, _forgeImportPublicJwk: function (keypair) {
|
|
if (keypair._forgePublic) {
|
|
return;
|
|
}
|
|
|
|
if (keypair._forge) {
|
|
keypair._forgePublic = forge.pki.rsa.setPublicKey(keypair._forge.n, keypair._forge.e);
|
|
}
|
|
else if (keypair.publicKeyJwk) {
|
|
keypair._forgePublic = forge.pki.rsa.setPublicKey.apply(
|
|
forge.pki.rsa
|
|
, forgec._publicJwkToComponents(keypair.publicKeyJwk || keypair.privateKeyJwk)
|
|
);
|
|
}
|
|
keypair._forgePublic.toJSON = notToJson;
|
|
}
|
|
, import: function (keypair) {
|
|
// no-op since this must be done anyway in extra
|
|
return keypair;
|
|
}
|
|
|
|
|
|
|
|
//
|
|
// Export Public / Private PEMs
|
|
//
|
|
, exportPrivatePem: function (keypair) {
|
|
if (keypair.privateKeyPem) {
|
|
return keypair.privateKeyPem;
|
|
}
|
|
|
|
if (keypair.privateKeyJwk && !(keypair._forge && keypair._forge)) {
|
|
forgec._forgeImportJwk(keypair);
|
|
}
|
|
|
|
if (keypair._forge && keypair._forge) {
|
|
return forge.pki.privateKeyToPem(keypair._forge);
|
|
}
|
|
|
|
throw new Error("None of privateKeyPem, _forge, or privateKeyJwk found. No way to export private key PEM");
|
|
}
|
|
, exportPublicPem: function (keypair) {
|
|
if (keypair.publicKeyPem) {
|
|
return keypair.publicKeyPem;
|
|
}
|
|
|
|
if ((keypair.privateKeyJwk || keypair.publicKeyJwk)
|
|
&& !(keypair._forge && (keypair._forge || keypair._forgePublic))
|
|
) {
|
|
forgec._forgeImportPublicJwk(keypair);
|
|
}
|
|
|
|
if (keypair._forge) {
|
|
return forge.pki.publicKeyToPem(keypair._forgePublic || keypair._forge);
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
};
|