/selfsigned-promise

Generate self-signed x509 certificates from NodeJS

Primary LanguageJavaScript

Generates a self-signed x509 certificate and its RSA keys from NodeJS. It is pomises based, see Usage

Build Status

Install

  npm install selfsigned

Basic usage

const selfsigned = require('selfsigned');
selfsigned.generate()
  .catch(err => console.log(err))
  .then(cert => console.log(cert));

Will return an object with the following keys and values :

{
  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/CkECQB4OdY+2FbCciI473eQkTu310RMf8jElU63iwnx4R/XN\r\n/mgqN589OfF4SS0U/MoRzYk9jF9IAJN1Mi/571T+nw4=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DQEBBQUAA4GB\r\nAC9hGQlDh8anNo1YDJdG2mYqOQ5uybJV++kixblGaOkoDROPsWepUpL6kMDUtbAM\r\n4uXTyFkvlUQSaQkhNgOY5w/BRIAkCIu6u4D4XcjlCdwFq6vcKMEuWTHMAlBWFla3\r\nXJZAPO10PHuDen7JeMOUf1Re7lRFtwfRGAvVYmrvYFKv\r\n-----END CERTIFICATE-----\r\n'
}

Advanced usage

selfsigned.generate(certAttributes, options)
  .catch(err => console.log(err))
  .then(cert => console.log(cert));

Certificate attributes and options

const certAttributes = [
  {
    name: 'commonName',
    value: 'helloworld.paris',
  },
  {
    name: 'countryName',
    value: 'France',
  },
  {
    shortName: 'ST',
    value: 'FR',
  },
  {
    name: 'localityName',
    value: 'Paris',
  },
  {
    name: 'organizationName',
    value: 'Hello World Corporation',
  },
  {
    shortName: 'OU',
    value: 'Hello World Corporation',
  }
];

const options = {
  keySize: 2048,
  validForDays: 30,
  certExtensions: [
    {
      name: 'basicConstraints',
      cA: true,
    },
    {
      name: 'subjectAltName',
      altNames: [{
        type: 6,
        value: 'http://example.org/webid#me',
      }],
    }
  ],
  exportAsPkcs7: true,
}

You can avoid key pair generation specifying your own PEM keys in the options object.

const options = {
  keyPair: {
    publicKey: '-----BEGIN PUBLIC KEY-----...',
    privateKey: '-----BEGIN RSA PRIVATE KEY-----...'
  }
  /* ... */
};

License

MIT