2016-07-31 03:47:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//var crypto = require('crypto');
|
|
|
|
var forge = require('node-forge');
|
|
|
|
|
|
|
|
function binstrToB64(binstr) {
|
|
|
|
return new Buffer(binstr, 'binary').toString('base64');
|
|
|
|
}
|
2016-08-01 08:03:50 +00:00
|
|
|
function b64ToBinstr(b64) {
|
|
|
|
return new Buffer(b64, 'b64').toString('binary');
|
|
|
|
}
|
2016-07-31 03:47:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
importPemPrivateKey: function(pem) {
|
|
|
|
var key = forge.pki.privateKeyFromPem(pem);
|
|
|
|
return {
|
|
|
|
privateKey: exportPrivateKey(key),
|
|
|
|
publicKey: exportPublicKey(key)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
importPemCertificate: function(pem) {
|
|
|
|
return forge.pki.certificateFromPem(pem);
|
|
|
|
},
|
|
|
|
|
|
|
|
privateKeyToPem: function(privateKey) {
|
|
|
|
var priv = importPrivateKey(privateKey);
|
|
|
|
return forge.pki.privateKeyToPem(priv);
|
|
|
|
},
|
|
|
|
|
|
|
|
certificateToPem: function(certificate) {
|
|
|
|
var derCert = base64ToBytes(certificate);
|
|
|
|
var cert = forge.pki.certificateFromAsn1(forge.asn1.fromDer(derCert));
|
|
|
|
return forge.pki.certificateToPem(cert);
|
|
|
|
},
|
|
|
|
*/
|
|
|
|
|
|
|
|
var extrac = module.exports = {
|
|
|
|
//
|
|
|
|
// internals
|
|
|
|
//
|
|
|
|
_forgeToPrivateJwk: function (keypair) {
|
2016-08-01 08:03:50 +00:00
|
|
|
var k = keypair._forge;
|
2016-07-31 03:47:52 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
kty: "RSA"
|
|
|
|
, n: binstrToB64(k.n.toByteArray())
|
|
|
|
, e: binstrToB64(k.e.toByteArray())
|
|
|
|
, d: binstrToB64(k.d.toByteArray())
|
|
|
|
, p: binstrToB64(k.p.toByteArray())
|
|
|
|
, q: binstrToB64(k.q.toByteArray())
|
|
|
|
, dp: binstrToB64(k.dP.toByteArray())
|
|
|
|
, dq: binstrToB64(k.dQ.toByteArray())
|
|
|
|
, qi: binstrToB64(k.qInv.toByteArray())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
, _forgeToPublicJwk: function (keypair) {
|
2016-08-01 08:03:50 +00:00
|
|
|
var k = keypair._forge || keypair._forgePublic;
|
2016-07-31 03:47:52 +00:00
|
|
|
return {
|
|
|
|
kty: "RSA"
|
|
|
|
, n: binstrToB64(k.n.toByteArray())
|
|
|
|
, e: binstrToB64(k.e.toByteArray())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-01 08:03:50 +00:00
|
|
|
//
|
|
|
|
// Import Forge
|
|
|
|
//
|
|
|
|
, _forgeImportJwk: require('./rsa-forge')._forgeImportJwk
|
|
|
|
, _forgeImportPublicJwk: require('./rsa-forge')._forgeImportPublicJwk
|
|
|
|
, _forgeImportPem: function (keypair) {
|
|
|
|
keypair._forge = keypair._forge || forge.pki.privateKeyFromPem(keypair.privateKeyPem);
|
|
|
|
}
|
|
|
|
, _forgeImportPublicPem: function (keypair) {
|
|
|
|
keypair._forgePublic = keypair._forgePublic || forge.pki.publicKeyFromPem(keypair.publicKeyPem);
|
|
|
|
}
|
|
|
|
, importForge: function (keypair) {
|
|
|
|
extrac._forgeImportJwk(keypair);
|
|
|
|
if (keypair.privateKeyPem) {
|
|
|
|
extrac._forgeImportPem(keypair);
|
|
|
|
}
|
|
|
|
if (keypair.publicKeyPem) {
|
|
|
|
extrac._forgeImportPublicPem(keypair);
|
|
|
|
}
|
|
|
|
return keypair;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-31 03:47:52 +00:00
|
|
|
//
|
|
|
|
// Export JWK
|
|
|
|
//
|
|
|
|
, exportPrivateJwk: function (keypair) {
|
|
|
|
var hasUrsaPrivate = keypair._ursa && true;
|
|
|
|
var hasPrivatePem = keypair.privateKeyPem && true;
|
2016-08-01 08:03:50 +00:00
|
|
|
var hasForgePrivate = keypair._forge && keypair._forge && true;
|
2016-07-31 03:47:52 +00:00
|
|
|
|
|
|
|
if (keypair.privateKeyJwk) {
|
|
|
|
return keypair.privateKeyJwk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasForgePrivate) {
|
|
|
|
if (hasUrsaPrivate && !hasPrivatePem) {
|
|
|
|
keypair.privateKeyPem = keypair._ursa.toPrivatePem().toString('ascii');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keypair.privateKeyPem) {
|
2016-08-01 08:03:50 +00:00
|
|
|
extrac._forgeImportPem(keypair);
|
2016-07-31 03:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 08:03:50 +00:00
|
|
|
if (keypair._forge && keypair._forge) {
|
2016-07-31 03:47:52 +00:00
|
|
|
return extrac._forgeToPrivateJwk(keypair);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("None of privateKeyPem, _ursa, _forge, or privateKeyJwk found. No way to export private key Jwk");
|
|
|
|
}
|
|
|
|
, exportPublicJwk: function (keypair) {
|
|
|
|
var hasUrsaPublic = (keypair._ursa || keypair._ursaPublic) && true;
|
|
|
|
var hasPublicPem = (keypair.privateKeyPem || keypair.publicKeyPem) && true;
|
|
|
|
var hasForgePublic = keypair._forge && true;
|
|
|
|
|
|
|
|
if (keypair.publicKeyJwk) {
|
|
|
|
return keypair.publicKeyJwk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keypair.privateKeyJwk) {
|
|
|
|
return {
|
|
|
|
kty: 'RSA'
|
|
|
|
, n: keypair.privateKeyJwk.n
|
|
|
|
, e: keypair.privateKeyJwk.e
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasForgePublic) {
|
|
|
|
if (hasUrsaPublic && !hasPublicPem) {
|
|
|
|
keypair.publicKeyPem = (keypair._ursa || keypair._ursaPublic).toPublicPem().toString('ascii');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keypair.publicKeyPem) {
|
2016-08-01 08:03:50 +00:00
|
|
|
extrac._forgeImportPublicPem(keypair);
|
2016-07-31 03:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 08:03:50 +00:00
|
|
|
if (keypair._forge && keypair._forge) {
|
2016-07-31 03:47:52 +00:00
|
|
|
return extrac._forgeToPublicJwk(keypair);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("None of publicKeyPem privateKeyPem, _ursa, _forge, publicKeyJwk, or privateKeyJwk found. No way to export private key Jwk");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|