2016-07-31 03:47:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-02 20:42:44 +00:00
|
|
|
// for forge
|
|
|
|
function _bigIntToBase64Url(fbin) {
|
|
|
|
var hex = fbin.toRadix(16);
|
|
|
|
if (hex.length % 2) {
|
|
|
|
// Invalid hex string
|
|
|
|
hex = '0' + hex;
|
|
|
|
}
|
|
|
|
var buf = Buffer.from(hex, 'hex');
|
|
|
|
var b64 = buf.toString('base64');
|
|
|
|
var b64Url = b64.replace(/[+]/g, "-").replace(/\//g, "_").replace(/=/g,"");
|
|
|
|
|
|
|
|
return b64Url;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
// I think this doesn't work because toByteArray() returns signed bytes
|
|
|
|
function _xxx_bigIntToBase64Url(fbin) {
|
|
|
|
if (!fbin.toByteArray) {
|
|
|
|
console.log('fbin');
|
|
|
|
console.log(fbin);
|
|
|
|
}
|
|
|
|
var byteArray = fbin.toByteArray();
|
|
|
|
var buf = Buffer.from(byteArray);
|
|
|
|
var b64 = buf.toString('base64');
|
|
|
|
var b64Url = b64.replace(/[+]/g, "-").replace(/\//g, "_").replace(/=/g,"");
|
|
|
|
|
|
|
|
return b64Url;
|
2016-08-01 08:03:50 +00:00
|
|
|
}
|
2016-08-02 20:42:44 +00:00
|
|
|
*/
|
2016-07-31 03:47:52 +00:00
|
|
|
|
|
|
|
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"
|
2016-08-02 20:42:44 +00:00
|
|
|
, n: _bigIntToBase64Url(k.n)
|
|
|
|
, e: _bigIntToBase64Url(k.e)
|
|
|
|
, d: _bigIntToBase64Url(k.d)
|
|
|
|
, p: _bigIntToBase64Url(k.p)
|
|
|
|
, q: _bigIntToBase64Url(k.q)
|
|
|
|
, dp: _bigIntToBase64Url(k.dP)
|
|
|
|
, dq: _bigIntToBase64Url(k.dQ)
|
|
|
|
, qi: _bigIntToBase64Url(k.qInv)
|
2016-07-31 03:47:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
, _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"
|
2016-08-02 20:42:44 +00:00
|
|
|
, n: _bigIntToBase64Url(k.n)
|
|
|
|
, e: _bigIntToBase64Url(k.e)
|
2016-07-31 03:47:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-01 08:03:50 +00:00
|
|
|
//
|
|
|
|
// Import Forge
|
|
|
|
//
|
|
|
|
, _forgeImportJwk: require('./rsa-forge')._forgeImportJwk
|
|
|
|
, _forgeImportPublicJwk: require('./rsa-forge')._forgeImportPublicJwk
|
2016-08-01 14:01:52 +00:00
|
|
|
, _forgeImportPem: require('./rsa-forge')._forgeImportPem
|
|
|
|
, _forgeImportPublicPem: require('./rsa-forge')._forgeImportPublicPem
|
2016-08-01 08:03:50 +00:00
|
|
|
, 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 13:50:46 +00:00
|
|
|
var hasForgePrivate = 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 13:50:46 +00:00
|
|
|
if (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-02 16:35:23 +00:00
|
|
|
if (keypair._forge || keypair._forgePublic) {
|
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");
|
|
|
|
}
|
|
|
|
};
|