Skip to content

Commit 44c08bc

Browse files
author
Andris Reinman
committed
version 0.1.0
1 parent bacb2ec commit 44c08bc

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed

LICENSE

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Copyright (c) 2012 Andris Reinman
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16+
SOFTWARE.

README.md

+93-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,96 @@ pem
33

44
Create private keys and certificates with node.js
55

6-
[![Build Status](https://secure.travis-ci.org/andris9/pem.png)](http://travis-ci.org/andris9/pem)
6+
[![Build Status](https://secure.travis-ci.org/andris9/pem.png)](http://travis-ci.org/andris9/pem)
7+
8+
**NB!** This module does not yet support node v0.7+/0.8 or Windows. Sorry.
9+
10+
## Installation
11+
12+
Install with npm
13+
14+
npm install pem
15+
16+
## API
17+
18+
### Create a private key
19+
20+
Use `createPrivateKey` for creating private keys
21+
22+
pem.createPrivateKey(keyBitsize, callback)
23+
24+
Where
25+
26+
* **keyBitsize** is an optional size of the key, defaults to 1024 (bit)
27+
* **callback** is a callback function with an error object and `{key}`
28+
29+
### Create a Certificate Signing Request
30+
31+
Use `createCSR` for creating private keys
32+
33+
pem.createCSR(options, callback)
34+
35+
Where
36+
37+
* **options** is an optional options object
38+
* **callback** is a callback function with an error object and `{csr, clientKey}`
39+
40+
Possible options are the following
41+
42+
* **clientKey** is an optional client key to use
43+
* **keyBitsize** - if `clientKey` is undefined, bit size to use for generating a new key (defaults to 1024)
44+
* **hash** is a hash function to use (either `md5` or `sha1`, defaults to `sha1`)
45+
* **country** is a CSR country field
46+
* **state** is a CSR state field
47+
* **locality** is a CSR locality field
48+
* **organization** is a CSR organization field
49+
* **organizationUnit** is a CSR organizational unit field
50+
* **commonName** is a CSR common name field (defaults to `localhost`)
51+
* **emailAddress** is a CSR email address field
52+
53+
### Create a certificate
54+
55+
Use `createCertificate` for creating private keys
56+
57+
pem.createCertificate(options, callback)
58+
59+
Where
60+
61+
* **options** is an optional options object
62+
* **callback** is a callback function with an error object and `{certificate, csr, clientKey, serviceKey}`
63+
64+
Possible options include all the options for `createCSR` - in case `csr` parameter is not defined and a new
65+
CSR needs to be generated.
66+
67+
In addition, possible options are the following
68+
69+
* **serviceKey** is a private key for signing the certificate, if not defined a new one is generated
70+
* **selfSigned** - if set to true and `serviceKey` is not defined, use `clientKey` for signing
71+
* **csr** is a CSR for the certificate, if not defined a new one is generated
72+
* **days** is the certificate expire time in days
73+
74+
### Export a public key
75+
76+
Use `getPublicKey` for exporting a public key from a private key, CSR or certificate
77+
78+
pem.getPublicKey(certificate, callback)
79+
80+
Where
81+
82+
* **certificate** is a PEM encoded private key, CSR or certificate
83+
* **callback** is a callback function with an error object and `{publicKey}`
84+
85+
### Read certificate info
86+
87+
Use `readCertificateInfo` for reading subject data from a certificate or a CSR
88+
89+
pem.readCertificateInfo(certificate, callback)
90+
91+
Where
92+
93+
* **certificate** is a PEM encoded CSR or a certificate
94+
* **callback** is a callback function with an error object and `{country, state, locality, organization, organizationUnit, commonName, emailAddress}`
95+
96+
## License
97+
98+
**MIT**

lib/pem.js

+56
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ module.exports.createCertificate = createCertificate;
66
module.exports.readCertificateInfo = readCertificateInfo;
77
module.exports.getPublicKey = getPublicKey;
88

9+
// PUBLIC API
10+
11+
/**
12+
* Creates a private key
13+
*
14+
* @param {Number} [keyBitsize=1024] Size of the key, defaults to 1024bit
15+
* @param {Function} callback Callback function with an error object and {key}
16+
*/
917
function createPrivateKey(keyBitsize, callback){
1018
if(!callback && typeof keyBitsize == "function"){
1119
callback = keyBitsize;
@@ -28,6 +36,25 @@ function createPrivateKey(keyBitsize, callback){
2836
});
2937
}
3038

39+
/**
40+
* Creates a Certificate Signing Request
41+
*
42+
* If client key is undefined, a new key is created automatically. The used key is included
43+
* in the callback return as clientKey
44+
*
45+
* @param {Object} [options] Optional options object
46+
* @param {String} [options.clientKey] Optional client key to use
47+
* @param {Number} [options.keyBitsize] If clientKey is undefined, bit size to use for generating a new key (defaults to 1024)
48+
* @param {String} [options.hash] Hash function to use (either md5 or sha1, defaults to sha1)
49+
* @param {String} [options.country] CSR country field
50+
* @param {String} [options.state] CSR state field
51+
* @param {String} [options.locality] CSR locality field
52+
* @param {String} [options.organization] CSR organization field
53+
* @param {String} [options.organizationUnit] CSR organizational unit field
54+
* @param {String} [options.commonName="localhost"] CSR common name field
55+
* @param {String} [options.emailAddress] CSR email address field
56+
* @param {Function} callback Callback function with an error object and {csr, clientKey}
57+
*/
3158
function createCSR(options, callback){
3259
if(!callback && typeof options == "function"){
3360
callback = options;
@@ -69,6 +96,18 @@ function createCSR(options, callback){
6996
});
7097
}
7198

99+
/**
100+
* Creates a certificate based on a CSR. If CSR is not defined, a new one
101+
* will be generated automatically. For CSR generation all the options values
102+
* can be used as with createCSR.
103+
*
104+
* @param {Object} [options] Optional options object
105+
* @param {String} [options.serviceKey] Private key for signing the certificate, if not defined a new one is generated
106+
* @param {Boolean} [options.selfSigned] If set to true and serviceKey is not defined, use clientKey for signing
107+
* @param {String} [options.csr] CSR for the certificate, if not defined a new one is generated
108+
* @param {Number} [options.days] Certificate expire time in days
109+
* @param {Function} callback Callback function with an error object and {certificate, csr, clientKey, serviceKey}
110+
*/
72111
function createCertificate(options, callback){
73112
if(!callback && typeof options == "function"){
74113
callback = options;
@@ -129,6 +168,12 @@ function createCertificate(options, callback){
129168
});
130169
}
131170

171+
/**
172+
* Exports a public key from a private key, CSR or certificate
173+
*
174+
* @param {String} certificate PEM encoded private key, CSR or certificate
175+
* @param {Function} callback Callback function with an error object and {publicKey}
176+
*/
132177
function getPublicKey(certificate, callback){
133178
if(!callback && typeof certificate == "function"){
134179
callback = certificate;
@@ -166,6 +211,12 @@ function getPublicKey(certificate, callback){
166211
});
167212
}
168213

214+
/**
215+
* Reads subject data from a certificate or a CSR
216+
*
217+
* @param {String} certificate PEM encoded CSR or certificate
218+
* @param {Function} callback Callback function with an error object and {country, state, locality, organization, organizationUnit, commonName, emailAddress}
219+
*/
169220
function readCertificateInfo(certificate, callback){
170221
if(!callback && typeof certificate == "function"){
171222
callback = certificate;
@@ -208,6 +259,8 @@ function readCertificateInfo(certificate, callback){
208259
});
209260
}
210261

262+
// HELPER FUNCTIONS
263+
211264
function fetchCertificateData(certData, callback){
212265
certData = (certData || "").toString();
213266

@@ -268,6 +321,9 @@ function generateCSRSubject(options){
268321
return csrBuilder.join("");
269322
}
270323

324+
/**
325+
* Spawn an openssl command
326+
*/
271327
function execOpenSSL(params, searchStr, stdin, callback){
272328
var openssl = spawn("openssl", params),
273329
stdout = "",

0 commit comments

Comments
 (0)