Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'Create 2048bit Private key': (test: any) => {
pem.createPrivateKey(2048, (error: any, data: any) => {
const key = (data && data.key || '').toString();
test.ifError(error);
test.ok(key);
test.ok(key.match(/^\n*\-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-\n/));
test.ok(key.match(/\n\-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-\n*$/));
test.ok(key.trim().length > 1650 && key.trim().length < 1700);
// test.ok(fs.readdirSync('./tmp').length === 0);
test.done();
});
},
'Create CSR with own encrypted key': (test: any) => {
var password = 'my:secure! "password\'s\nawesome';
pem.createPrivateKey(2048, { cipher: 'des3', password: password }, (error: any, data: any) => {
var key = (data && data.key || '').toString();
pem.createCSR({
clientKey: key,
clientKeyPassword: password
}, (error: any, data: any) => {
var csr = (data && data.csr || '').toString();
test.ifError(error);
test.ok(csr);
test.ok(csr.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE REQUEST\-\-\-\-\-\n/));
test.ok(csr.match(/\n\-\-\-\-\-END CERTIFICATE REQUEST\-\-\-\-\-\n*$/));
test.equal(data && data.clientKey, key);
test.ok(data && data.clientKey);
// test.ok(fs.readdirSync('./tmp').length === 0);
function getNewKey(keyLen, callback) {
// this function wants key len in bits, our config
// specified it in bytes, so multiply by 8
pem.createPrivateKey(keyLen*8, function(err, key) {
if (err) {
console.log("key callback got an error");
callback(err, null);
} else {
var tempKeyStr = "";
var finalKey = "";
// get rid of wrapping text and new lines
var result = key.key.split("\n");
for (var i = 1; i < (result.length-1); i++) {
tempKeyStr += result[i];
}
//console.log("tempKeyStr len: %d\ntempKeyStr: %s\n", tempKeyStr.length, tempKeyStr);
// decode the string
function checkServiceKey(cb) {
try {
fs.statSync(keyPath);
var serviceKey = fs.readFileSync(keyPath);
cb(null, serviceKey);
} catch (e) {
if (e.code === 'ENOENT') {
// if key.pem not exist, generate the private key
console.log("Create the service key 'config/key.pem'");
pem.createPrivateKey(2048, function(e, keyData) {
if (e) {
cb(e);
} else {
fs.writeFile(keyPath, keyData.key, function(e) {
cb(e, e || { newKey: true, keyData: keyData.key });
});
}
});
} else {
cb(e);
}
}
}
function createPrivateKey(req, res, next){
if (openSSLEnabled) {
let options = getKeyOptions(req.body.options);
pem.createPrivateKey(
options.keyBitsize,
options,
function(err, privateKey){
if(err){
return res.parcel.setStatus(400)
.setData(err)
.deliver();
}
return res.parcel.setStatus(200)
.setData(privateKey.key)
.deliver();
}
);
} else {
res.parcel.setStatus(400)
.setMessage('Security options have not been properly configured')
tasks.push(function(cb){
pem.createPrivateKey(keyOptions.keyBitsize, keyOptions, function(err, key){
csrOptions.clientKey = key.key;
cb(err);
});
});
}