Newer
Older
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
jwcrypto = require('jwcrypto'),
Ben Adida
committed
Buffer = require('buffer').Buffer,
crypto = require('crypto');
Ben Adida
committed
Ben Adida
committed
// make this async capable
function bytesToChars(buf) {
var str = "";
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Ben Adida
committed
Ben Adida
committed
// yes, we are biasing the output here a bit.
// I'm ok with that. We can improve this over time.
for (var i=0; i < buf.length; i++) {
str += alphabet.charAt(buf[i] % alphabet.length);
}
Lloyd Hilaiel
committed
Ben Adida
committed
return str;
Ben Adida
committed
}
Ben Adida
committed
exports.generate = function(chars, cb) {
if (cb) {
crypto.randomBytes(chars, function(ex, buf) {
cb(bytesToChars(buf));
});
} else {
return bytesToChars(crypto.randomBytes(chars));
}
};
Lloyd Hilaiel
committed
Ben Adida
committed
// we don't bother to make this async, cause it's not needed
exports.weakGenerate = function(chars) {
Lloyd Hilaiel
committed
var str = "";
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Ben Adida
committed
Lloyd Hilaiel
committed
for (var i=0; i < chars; i++) {
Ben Adida
committed
str += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
return str;
Ben Adida
committed
};
Lloyd Hilaiel
committed
// functions to set defaults
// default key name is 'root'
function checkName(name) {
return name ? name : 'root';
}
// default directory is the var dir.
function checkDir(dir) {
return dir ? dir : require('./configuration').get('var_path');
}
Lloyd Hilaiel
committed
exports.hydrateSecret = function(name, dir) {
dir = checkDir(dir);
Lloyd Hilaiel
committed
var p = path.join(dir, name + ".sekret");
var secret = undefined;
Lloyd Hilaiel
committed
try{ secret = fs.readFileSync(p).toString(); } catch(e) {};
if (secret === undefined) {
fs.writeFileSync(p, '');
fs.chmodSync(p, 0600);
Lloyd Hilaiel
committed
fs.writeFileSync(p, secret);
}
return secret;
};
exports.loadSecretKey = function(name, dir) {
name = checkName(name);
dir = checkDir(dir);
var p = path.join(dir, name + ".secretkey");
var secret = undefined;
Lloyd Hilaiel
committed
// may throw
secret = fs.readFileSync(p).toString();
if (secret === undefined) {
return null;
}
// parse it
return jwcrypto.loadSecretKey(secret);
Lloyd Hilaiel
committed
function readAndParseCert(name, dir) {
name = checkName(name);
dir = checkDir(dir);
Lloyd Hilaiel
committed
var p = path.join(dir, name + ".cert");
var cert = undefined;
Lloyd Hilaiel
committed
// may throw
Lloyd Hilaiel
committed
cert = fs.readFileSync(p).toString();
Lloyd Hilaiel
committed
if (cert === undefined) {
return null;
}
// parse it
// it should be a JSON structure with alg and serialized key
// {alg: <ALG>, value: <SERIALIZED_KEY>}
var payloadSegment = jwcrypto.extractComponents(cert).payloadSegment;
return JSON.parse(new Buffer(payloadSegment, 'base64').toString());
Lloyd Hilaiel
committed
exports.publicKeyCreationDate = function(name, dir) {
return new Date(readAndParseCert(name, dir).iat);
};
exports.loadPublicKey = function(name, dir) {
var parsedCert = readAndParseCert(name, dir);
var pkString = parsedCert['public-key'] || parsedCert.publicKey;
return jwcrypto.loadPublicKey(JSON.stringify(pkString));