le-acme-core.js/lib/letsencrypt-ursa.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-12-15 22:07:02 +00:00
/*!
2015-12-16 02:58:53 +00:00
* letiny-core
2015-12-15 22:07:02 +00:00
* Copyright(c) 2015 AJ ONeal <aj@daplie.com> https://daplie.com
* Apache-2.0 OR MIT (and hence also MPL 2.0)
*/
'use strict';
var crypto = require('crypto');
var ursa = require('ursa');
var forge = require('node-forge');
function binstrToB64(binstr) {
2015-12-15 22:07:02 +00:00
return new Buffer(binstr, 'binary').toString('base64');
}
/*
function b64ToBinstr(b64) {
return new Buffer(b64, 'base64').toString('binary');
}
*/
2015-12-20 01:36:50 +00:00
function privatePemToJwk(privkeyPem) {
2015-12-15 22:07:02 +00:00
var forgePrivkey = forge.pki.privateKeyFromPem(privkeyPem);
// required in node.js 4.2.2 (but not io.js 1.6.3)
Object.keys(forgePrivkey).forEach(function (k) {
var val = forgePrivkey[k];
if (val && val.toByteArray) {
forgePrivkey[k] = val.toByteArray();
}
});
2015-12-15 22:07:02 +00:00
return {
kty: "RSA"
, n: binstrToB64(forgePrivkey.n)
, e: binstrToB64(forgePrivkey.e)
, d: binstrToB64(forgePrivkey.d)
, p: binstrToB64(forgePrivkey.p)
, q: binstrToB64(forgePrivkey.q)
, dp: binstrToB64(forgePrivkey.dP)
, dq: binstrToB64(forgePrivkey.dQ)
, qi: binstrToB64(forgePrivkey.qInv)
2015-12-15 22:07:02 +00:00
};
}
function generateRsaKeypair(bitlen, exp, cb) {
var keypair = ursa.generatePrivateKey(bitlen || 2048, exp || 6553);
2015-12-15 22:07:02 +00:00
var pems = {
publicKeyPem: keypair.toPublicPem().toString('ascii') // ascii PEM: ----BEGIN...
, privateKeyPem: keypair.toPrivatePem().toString('ascii') // ascii PEM: ----BEGIN...
};
2015-12-19 19:26:52 +00:00
// for account id
pems.publicKeySha256 = crypto.createHash('sha256').update(pems.publicKeyPem).digest('hex');
// for compat with python client account id
2015-12-15 22:07:02 +00:00
pems.publicKeyMd5 = crypto.createHash('md5').update(pems.publicKeyPem).digest('hex');
// json { n: ..., e: ..., iq: ..., etc }
2015-12-20 01:36:50 +00:00
pems.privateKeyJwk = privatePemToJwk(pems.privateKeyPem);
2015-12-15 22:07:02 +00:00
pems.privateKeyJson = pems.privateKeyJwk;
// TODO thumbprint
cb(null, pems);
}
2015-12-20 01:36:50 +00:00
function privateJwkToPems(pkj, cb) {
2015-12-15 22:07:02 +00:00
Object.keys(pkj).forEach(function (key) {
pkj[key] = new Buffer(pkj[key], 'base64');
});
var priv;
2015-12-20 01:36:50 +00:00
var pems;
2015-12-15 22:07:02 +00:00
try {
priv = ursa.createPrivateKeyFromComponents(
pkj.n // modulus
, pkj.e // exponent
, pkj.p
, pkj.q
, pkj.dp
, pkj.dq
, pkj.qi
, pkj.d
);
} catch(e) {
cb(e);
return;
}
2015-12-20 01:36:50 +00:00
pems = {
privateKeyPem: priv.toPrivatePem().toString('ascii')
, publicKeyPem: priv.toPublicPem().toString('ascii')
};
// for account id
pems.publicKeySha256 = crypto.createHash('sha256').update(pems.publicKeyPem).digest('hex');
// for compat with python client account id
pems.publicKeyMd5 = crypto.createHash('md5').update(pems.publicKeyPem).digest('hex');
// json { n: ..., e: ..., iq: ..., etc }
pems.privateKeyJwk = privatePemToJwk(pems.privateKeyPem);
pems.privateKeyJson = pems.privateKeyJwk;
cb(null, pems);
2015-12-15 22:07:02 +00:00
}
module.exports.generateRsaKeypair = generateRsaKeypair;
2015-12-20 01:36:50 +00:00
module.exports.privateJwkToPems = privateJwkToPems;
module.exports.privatePemToJwk = privatePemToJwk;