Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ To override the default client CN of `john doe jdoe123`, add another option for
var pems = selfsigned.generate(null, { clientCertificate: true, clientCertificateCN: 'FooBar' });
```

### Generate certificates signed by your own CA

Provide your private certificate authority root certificate and private key in PEM format in `ca` option object (under `cert` and `private` keys respectively):

```js
const cert = selfsigned.generate(
[{ name: 'commonName', value: 'example.com' }],
{
keySize: 2048,
ca: {
cert: "-----BEGIN CERTIFICATE-----\r\n…\r\n-----END CERTIFICATE-----\r\n",
private: "-----BEGIN RSA PRIVATE KEY-----\r\n…\r\n-----END RSA PRIVATE KEY-----\r\n",
},
algorithm: 'sha256',
extensions: [
{
name: 'basicConstraints',
cA: false,
},
{
name: "keyUsage",
keyCertSign: false, // Must be set to false or Chrome won't accept this certificate otherwise
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true,
},
{
name: "extKeyUsage",
serverAuth: true,
clientAuth: true,
codeSigning: true,
timeStamping: true,
},
],
}
)
```

And yes, you can generate CA certificate with selfsigned itself and just provide output of `selfsigned.generate` into `ca` option.

## License

MIT
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ exports.generate = function generate(attrs, options, done) {
cert.validity.notAfter = new Date();
cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + (options.days || 365));

var caPrivateKey, caCert, issuerAttrs;
if (options && options.ca) {
caPrivateKey = forge.pki.privateKeyFromPem(options.ca.private),
caCert = forge.pki.certificateFromPem(options.ca.cert);
issuerAttrs = caCert.subject.attributes;
} else {
// Self-signed certificate: use our own key for signing
caPrivateKey = keyPair.privateKey;
caCert = cert;
issuerAttrs = attrs;
}

attrs = attrs || [{
name: 'commonName',
value: 'example.org'
Expand All @@ -64,7 +76,7 @@ exports.generate = function generate(attrs, options, done) {
}];

cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setIssuer(issuerAttrs);

cert.publicKey = keyPair.publicKey;

Expand All @@ -86,7 +98,7 @@ exports.generate = function generate(attrs, options, done) {
}]
}]);

cert.sign(keyPair.privateKey, getAlgorithm(options && options.algorithm));
cert.sign(caPrivateKey, getAlgorithm(options && options.algorithm));

const fingerprint = forge.md.sha1
.create()
Expand Down Expand Up @@ -150,7 +162,7 @@ exports.generate = function generate(attrs, options, done) {
}

var caStore = forge.pki.createCaStore();
caStore.addCertificate(cert);
caStore.addCertificate(caCert);

try {
forge.pki.verifyCertificateChain(caStore, [cert],
Expand Down