Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional features #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
72 changes: 64 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,62 @@ Simple node.js module to split a single certificate authority chain file (bundle

## Usage

Usage will depend on your server module of choice, but most https modules require an options hash with `ca`, `key`, and `cert`. Simply give split-ca the filepath of your bundle file.
Usage will depend on your server module of choice, but most https modules require an options hash with `ca`, `key`, and `cert`. If you have a file containing a certificate bundle, you can use the `splitFileSync` function to read the bundle and split it into an Array:

```js
var https = require('https');
var fs = require('fs');
const https = require('https');
const fs = require('fs');

var splitca = require('split-ca');
const { splitFileSync } = require('split-ca');

var options = {
ca: splitca("path/to/ca_bundle_file"),
const options = {
ca: splitFileSync("path/to/ca_bundle_file"),
key:fs.readFileSync("path/to/server_key_file"),
cert:fs.readFileSync("path/to/server_cert_file"),
requestCert: true,
rejectUnauthorized: true
};

https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
```

Non-synchronous version:

```js
const https = require('https');
const fs = require('fs');
const { splitFile } = require('split-ca');

async function startServer() {
const ca = await splitFile("path/to/ca_bundle_file");

const options = {
ca,
key:fs.readFileSync("path/to/server_key_file"),
cert:fs.readFileSync("path/to/server_cert_file"),
requestCert: true,
rejectUnauthorized: true
};

https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}
```

Version if your bundle is in a string rather than a file:

```js
const https = require('https');
const fs = require('fs');
const { splitContent } = require('split-ca');

const options = {
ca: splitContent(process.env.CA),
key:fs.readFileSync("path/to/server_key_file"),
cert:fs.readFileSync("path/to/server_cert_file"),
requestCert: true,
Expand All @@ -32,20 +78,30 @@ https.createServer(options, function (req, res) {

## Args

`split-ca('filepath','split-string','encoding')`
This module exports three functions:

```
splitFile(filepath, splitString, encoding)
splitFileSync(filepath, splitString, encoding)
splitContent(content, splitString)
```

#### `filepath`

A standard node path to your object. An error is thrown if the file cannot be parsed, is not formatted properly.

#### `split-string`
#### `splitString`

Optional. Defaults to `"\n"`, can be replaced with anything.

#### `encoding`

Optional. Defaults to `"utf-8"`, can be replaced with anything accepted by node's `fs` module.

#### `content`

PEM-encoded certificate bundle string.

## Credits

Thanks to [Benjie Gillam](https://twitter.com/Benjie) for the [blog post and sample code](http://www.benjiegillam.com/2012/06/node-dot-js-ssl-certificate-chain/) that was unashamedly ripped for this module.
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
var fs = require('fs');
const fs = require('fs');

module.exports = function (filepath, split, encoding) {
split = typeof split !== 'undefined' ? split : "\n";
encoding = typeof encoding !== 'undefined' ? encoding : "utf8";
function splitFileSync(filepath, split = "\n", encoding = "utf8") {
const chain = fs.readFileSync(filepath, encoding);
return splitContent(chain, split);
}

async function splitFile(filepath, split = "\n", encoding = "utf8") {
const chain = await fs.promises.readFile(filepath, encoding);
return splitContent(chain, split);
}

var ca = [];
var chain = fs.readFileSync(filepath, encoding);
if(chain.indexOf("-END CERTIFICATE-") < 0 || chain.indexOf("-BEGIN CERTIFICATE-") < 0){
function splitContent(chain, split = "\n") {
const ca = [];
if (chain.indexOf("-END CERTIFICATE-") < 0 || chain.indexOf("-BEGIN CERTIFICATE-") < 0) {
throw Error("File does not contain 'BEGIN CERTIFICATE' or 'END CERTIFICATE'");
}
chain = chain.split(split);
var cert = [];
var _i, _len;
for (_i = 0, _len = chain.length; _i < _len; _i++) {
var line = chain[_i];
let cert = [];
for (const line of chain) {
if (!(line.length !== 0)) {
continue;
}
Expand All @@ -25,3 +29,9 @@ module.exports = function (filepath, split, encoding) {
}
return ca;
}

module.exports = splitFileSync;

module.exports.splitFile = splitFile;
module.exports.splitFileSync = splitFileSync;
module.exports.splitContent = splitContent;
Loading