Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sandrinodimattia
Created January 29, 2018 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandrinodimattia/8ef6094325e7254473a16925898ab0a1 to your computer and use it in GitHub Desktop.
Save sandrinodimattia/8ef6094325e7254473a16925898ab0a1 to your computer and use it in GitHub Desktop.
Generate self signed certificates with the selfsigned library and validate those with OpenSSL 1.1
const fs = require('fs');
const ursa = require('ursa');
const async = require('async');
const faker = require('faker');
const util = require('util');
const spawn = require('child_process').spawn;
const selfsigned = require('selfsigned');
const total = Array.from(Array(10000).keys());
function validateOpenSSL(path, cb) {
const ssl = spawn(
'/usr/local/Cellar/openssl@1.1/1.1.0g_1/bin/openssl', ['x509', '-noout', '-text', '-in', path]
);
ssl.stderr.on('data', function (data) {
process.stdout.write(data);
});
ssl.on('exit', function (code) {
if (code !== 0) {
console.log(code);
process.exit(0);
}
cb();
});
}
async.everyLimit(total, 1, function (index, callback) {
const path = './cert.pem';
const keyPair = ursa.generatePrivateKey(2048, 65537);
const attributes = [{
name: 'commonName',
value: faker.internet.userName() + '.something.com'
}];
const options = {
pkcs7: true,
days: 5000,
algorithm: 'sha256',
keyPair: {
privateKey: keyPair.toPrivatePem().toString(),
publicKey: keyPair.toPublicPem().toString()
},
extensions: [{
name: 'basicConstraints',
cA: true,
critical: true
}, {
name: 'subjectKeyIdentifier'
}, {
name: 'keyUsage',
digitalSignature: true,
keyCertSign: true,
critical: true
}]
};
const { cert } = selfsigned.generate(attributes, options);
console.log('Validating ' + index);
fs.writeFile(path, cert, 'UTF8', err => {
if (err) {
return callback(err);
}
validateOpenSSL(path, () => {
callback(null, true);
});
});
},
function (err, result) {
console.log('done', err, result);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment