self-signed

1.3.1 • Public • Published

Generate a self signed x509 certificate from node.js.

Build Status

Install

$ npm install self-signed

Test

Run any of the following:

$ npm test
$ make test
$ mocha

Note: Don't forget to npm install!

Usage

var generate = require('self-signed');
 
var pems = generate({
  name: 'example.com',
  city: 'Blacksburg',
  state: 'Virginia',
  organization: 'Test',
  unit: 'Test'
}, {
  keySize: 1024, // default
  expire: 2 * 365 * 24 * 60 * 60 * 1000 // defaults to exactly 1 year
});
 
console.log(pems);

Will log an object with private, public and cert properties.

{
  private: '-----BEGIN RSA PRIVATE KEY-----\r\nMIICXAIBAAKBgQCBFMXMYS/+RZz6+qzv+xeqXPdjw4YKZC4y3dPhSwgEwkecrCTX\r\nsR6boue+1MjIqPqWggXZnotIGldfEN0kn0Jbh2vMTrTx6YwqQ8tceBPoyuuqcYBO\r\nOONAcKOB3MLnZbyOgVtbyT3j68JE5V/lx6LhpIKAgY0m5WIuaKrW6mvLXQIDAQAB\r\nAoGAU6ODGxAqSecPdayyG/ml9vSwNAuAMgGB0eHcpZG5i2PbhRAh+0TAIXaoFQXJ\r\naAPeA2ISqlTJyRmQXYAO2uj61FzeyDzYCf0z3+yZEVz3cO7jB5Pl6iBvzbxWuuuA\r\ncbJtWLhWtW5/jioc8F0EAzZ+lkC/XuVJdwKHDmwt2qvJO+ECQQD+dvo1g3Sz9xGw\r\n21n+fDG5i4128+Qh+JPgh5AeLuXSofc1HMHaOXcC6Wu/Cloh7QAD934b7W0A7VoD\r\ndLd/JLyFAkEAgdwjryyvdhy69e516IrPB3b+m4rggtntBlZREMrk9tOzeIucVO3W\r\ntKI3FHm6JebN2gVcG+rZ+FaDPo+ifJkW+QJBAPojrMwEACmUevB2f9246gxx0UsY\r\nbq6yM3No71OsWEEY8/Bi53CEQqg7Gq5+F6H33qcHmBEN8LQTngN9rY+vZh0CQBg0\r\nqJImii5B/LeK03+dICoMDDmCEYdSh9P+ku3GZBd+Lp3xqBpMmxDgi9PNPN2DwCs7\r\nhIfPpwGbXqtyqp7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wAwEB/zALBgNVHQ8EBAMCAvQwJgYDVR0RBB8w\r\nHYYbaHR0cDovL2V4YW1wbGUub3JnL3dlYmlkI21lMA0GCSqGSIb3DQEBBQUAA4GB\r\nAC9hGQlDh8anNo1YDJdG2mYqOQ5uybJV++kixblGaOkoDROPsWepUpL6kMDUtbAM\r\n4uXTyFkvlUQSaQkhNgOY5w/BRIAkCIu6u4D4XcjlCdwFq6vcKMEuWTHMAlBWFla3\r\nXJZAPO10PHuDen7JeMOUf1Re7lRFtwfRGAvVYmrvYFKv\r\n-----END CERTIFICATE-----\r\n'
}

Attributes

Supports attributes using a simplified syntax.

var generate = require('self-signed');
 
var pems = generate({
  name: 'example.com', // the common name
  country: 'US', // default
  city: 'Blacksburg',
  state: 'Virginia',
  organization: 'Test',
  unit: 'Test' // the organizational unit (e.g. department)
});
console.log(pems);

Full Options

Supports keySize, serial, expire, alt, pkcs7

var pems = generate(null, {
  keySize: 2048, // defaults to 1024
  serial: '329485', // defaults to '01'
  expire: new Date('10 December 2100'), // defaults to one year from today
  pkcs7: true, // defaults to false, indicates whether to protect with PKCS#7
  alt: [] // default undefined, alternate names if array of objects/strings
});

Expire

Expire can be specified as:

  • a Date object
  • a string timestamp to be passed to the Date constructor
  • a number representing milliseconds from now

If the expire option matches none of these, it defaults to new Date plus one year.

Alternate names

The alt specifies alternate names that apply to the certificate. Currently autodetects IP addresses,

Allowed formats include:

  • ['127.0.0.1', 'http://www.example.org']
  • [{type: Number, value: *}, {type: 7, ip: '127.0.0.1'}, {type: 6, value: 'http://example.org'}]

Example

Creating an HTTPS Server and Making Verified Requests

var keypair = require('..');
 
var https = require('https');
var portfinder = require('portfinder'); // you'll need to install this to test
 
createServer(function(err, server) {
  if (err) {
    throw err;
  }
  var raw = 'hello, world!', body = new Buffer(raw);
  server.on('request', function(req, res) {
    res.writeHead(200, {
      'content-length': body.length,
      'content-type': 'text/plain'
    });
    res.end(body);
  });
  request({
    port: server.port,
    ca: [server.cert]
  }, function(err, data) {
    server.close();
    if (err) {
      throw err;
    }
    console.log('success:', data.toString() === raw);
  });
});
 
// easy-to-use wrapper
function createServer(callback) {
  var options = keypair({
    name: 'localhost',
    city: 'Blacksburg',
    state: 'Virginia',
    organization: 'Test',
    unit: 'Test'
  }, {
    alt: ['127.0.0.1']
  });
 
  // server automatically sets up a .cert property
  var server = https.createServer({
    key: options.private,
    cert: options.cert
  });
 
  portfinder.getPort(function(err, port) {
    if (err) {
      return callback(err);
    }
    server.port = port;
    server.listen(port, 'localhost', function() {
      callback(null, server);
    });
  });
 
  return server;
}
 
function request(options, callback) {
  options.hostname = 'localhost';
  options.path = '/';
  var req = https.get(options, function(res) {
    res.on('error', callback);
 
    var data = [], length = 0;
    res.on('data', function(chunk) {
      data.push(chunk);
      length += chunk.length;
    });
    res.on('end', function() {
      callback(null, Buffer.concat(data, length));
    });
  });
  req.on('error', callback);
}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i self-signed

Weekly Downloads

82

Version

1.3.1

License

MIT

Last publish

Collaborators

  • skeggse