partial implementation
This commit is contained in:
parent
6c084e6b48
commit
26595e7539
|
@ -0,0 +1,130 @@
|
|||
'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));
|
||||
}
|
||||
*/
|
||||
|
||||
var forgec = {
|
||||
|
||||
|
||||
|
||||
//
|
||||
// to components
|
||||
//
|
||||
_base64ToBn: function (base64) {
|
||||
return new forge.jsbn.BigInteger(utils.b64dec(base64).toString("hex"), 16);
|
||||
}
|
||||
, _privateJwkToComponents: function (jwk) {
|
||||
var components = [];
|
||||
|
||||
[ '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
|
||||
//
|
||||
, exportPrivateKeyPem: function (keypair) {
|
||||
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");
|
||||
}
|
||||
, exportPublicKeyPem: function (keypair) {
|
||||
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
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
return forgec;
|
Loading…
Reference in New Issue