Skip to content

Commit 85d2d09

Browse files
author
Andris Reinman
committed
added tests
1 parent 90cb544 commit 85d2d09

File tree

2 files changed

+274
-19
lines changed

2 files changed

+274
-19
lines changed

lib/pem.js

+43-14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ var spawn = require("child_process").spawn;
33
module.exports.createPrivateKey = createPrivateKey;
44
module.exports.createCSR =createCSR;
55
module.exports.createCertificate = createCertificate;
6+
module.exports.readCertificateInfo = readCertificateInfo;
7+
module.exports.getPublicKey = getPublicKey;
68

79
function createPrivateKey(keyBitsize, callback){
8-
keyBitsize = Number(keyBitsize) || 2048;
10+
if(!callback && typeof keyBitsize == "function"){
11+
callback = keyBitsize;
12+
keyBitsize = undefined;
13+
}
14+
15+
keyBitsize = Number(keyBitsize) || 1024;
916

1017
var params = ["genrsa",
1118
"-rand",
@@ -22,6 +29,11 @@ function createPrivateKey(keyBitsize, callback){
2229
}
2330

2431
function createCSR(options, callback){
32+
if(!callback && typeof options == "function"){
33+
callback = options;
34+
options = undefined;
35+
}
36+
2537
options = options || {};
2638

2739
if(!options.clientKey){
@@ -58,6 +70,11 @@ function createCSR(options, callback){
5870
}
5971

6072
function createCertificate(options, callback){
73+
if(!callback && typeof options == "function"){
74+
callback = options;
75+
options = undefined;
76+
}
77+
6178
options = options || {};
6279

6380
if(!options.csr){
@@ -73,14 +90,19 @@ function createCertificate(options, callback){
7390
}
7491

7592
if(!options.serviceKey){
76-
createPrivateKey(options.keyBitsize || 1024, function(error, keyData){
77-
if(error){
78-
return callback(error);
79-
}
80-
options.serviceKey = keyData.key;
81-
createCertificate(options, callback);
82-
});
83-
return;
93+
94+
if(options.selfSigned){
95+
options.serviceKey = options.clientKey;
96+
}else{
97+
createPrivateKey(options.keyBitsize || 1024, function(error, keyData){
98+
if(error){
99+
return callback(error);
100+
}
101+
options.serviceKey = keyData.key;
102+
createCertificate(options, callback);
103+
});
104+
return;
105+
}
84106
}
85107

86108
var params = ["x509",
@@ -108,6 +130,11 @@ function createCertificate(options, callback){
108130
}
109131

110132
function getPublicKey(certificate, callback){
133+
if(!callback && typeof options == "function"){
134+
callback = options;
135+
options = undefined;
136+
}
137+
111138
certificate = (certificate || "").toString();
112139

113140
var params;
@@ -130,8 +157,7 @@ function getPublicKey(certificate, callback){
130157
"-pubkey",
131158
"-noout"];
132159
}
133-
console.log(params)
134-
console.log(certificate)
160+
135161
execOpenSSL(params, "PUBLIC KEY", certificate, function(error, key){
136162
if(error){
137163
return callback(error);
@@ -141,6 +167,11 @@ function getPublicKey(certificate, callback){
141167
}
142168

143169
function readCertificateInfo(certificate, callback){
170+
if(!callback && typeof options == "function"){
171+
callback = options;
172+
options = undefined;
173+
}
174+
144175
certificate = (certificate || "").toString();
145176

146177
var type = certificate.match(/BEGIN CERTIFICATE REQUEST/)?"req":"x509",
@@ -202,7 +233,6 @@ function fetchCertificateData(certData, callback){
202233
certValues.organization = tmp && tmp[1] || "";
203234
// unit
204235
tmp = subject.match(/\sOU=([^,\n].*?)[,\n]/);
205-
console.log(tmp)
206236
certValues.organizationUnit = tmp && tmp[1] || "";
207237
// common name
208238
tmp = subject.match(/\sCN=([^,\n].*?)[,\n]/);
@@ -211,8 +241,7 @@ function fetchCertificateData(certData, callback){
211241
tmp = extra.match(/emailAddress=([^,\n\/].*?)[,\n\/]/);
212242
certValues.emailAddress = tmp && tmp[1] || "";
213243
}
214-
console.log(certData)
215-
console.log(certValues);
244+
callback(null, certValues);
216245
}
217246

218247
function generateCSRSubject(options){

test/pem.js

+231-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,239 @@ var pem = require(".."),
22
testCase = require('nodeunit').testCase;
33

44
exports["General Tests"] = {
5-
"Sample test": function(test){
6-
7-
pem.createCertificate(null, function(error, data){
5+
6+
"Create default sized Private key": function(test){
7+
pem.createPrivateKey(function(error, data){
8+
var key = (data && data.key || "").toString();
9+
test.ifError(error);
10+
test.ok(key);
11+
test.ok(key.match(/^\n*\-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-\n/));
12+
test.ok(key.match(/\n\-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-\n*$/));
13+
test.ok(key.trim().length > 850 && key.trim().length < 900);
14+
test.done();
15+
});
16+
},
17+
18+
"Create 2048bit Private key": function(test){
19+
pem.createPrivateKey(2048, function(error, data){
20+
var key = (data && data.key || "").toString();
21+
test.ifError(error);
22+
test.ok(key);
23+
test.ok(key.match(/^\n*\-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-\n/));
24+
test.ok(key.match(/\n\-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-\n*$/));
25+
test.ok(key.trim().length > 1650 && key.trim().length < 1700);
26+
test.done();
27+
});
28+
},
29+
30+
"Create default CSR": function(test){
31+
pem.createCSR(function(error, data){
32+
var csr = (data && data.csr || "").toString();
33+
test.ifError(error);
34+
test.ok(csr);
35+
test.ok(csr.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE REQUEST\-\-\-\-\-\n/));
36+
test.ok(csr.match(/\n\-\-\-\-\-END CERTIFICATE REQUEST\-\-\-\-\-\n*$/));
37+
38+
test.ok(data && data.clientKey);
39+
40+
test.done();
41+
});
42+
},
43+
44+
"Create CSR with own key": function(test){
45+
pem.createPrivateKey(function(error, data){
46+
var key = (data && data.key || "").toString();
47+
48+
pem.createCSR({clientKey: key}, function(error, data){
49+
var csr = (data && data.csr || "").toString();
50+
test.ifError(error);
51+
test.ok(csr);
52+
test.ok(csr.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE REQUEST\-\-\-\-\-\n/));
53+
test.ok(csr.match(/\n\-\-\-\-\-END CERTIFICATE REQUEST\-\-\-\-\-\n*$/));
54+
55+
test.equal(data && data.clientKey, key);
56+
57+
test.ok(data && data.clientKey);
58+
59+
test.done();
60+
});
61+
62+
});
63+
},
64+
65+
"Create default certificate": function(test){
66+
pem.createCertificate(function(error, data){
67+
var certificate = (data && data.certificate || "").toString();
68+
test.ifError(error);
69+
test.ok(certificate);
70+
test.ok(certificate.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE\-\-\-\-\-\n/));
71+
test.ok(certificate.match(/\n\-\-\-\-\-END CERTIFICATE\-\-\-\-\-\n*$/));
72+
73+
test.ok((data && data.clientKey) != (data && data.serviceKey));
74+
75+
test.ok(data && data.clientKey);
76+
test.ok(data && data.serviceKey);
77+
test.ok(data && data.csr);
78+
79+
test.done();
80+
});
81+
},
82+
83+
"Create self signed certificate": function(test){
84+
pem.createCertificate({selfSigned: true}, function(error, data){
85+
var certificate = (data && data.certificate || "").toString();
886
test.ifError(error);
9-
test.ok(data);
10-
console.log(data);
87+
test.ok(certificate);
88+
test.ok(certificate.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE\-\-\-\-\-\n/));
89+
test.ok(certificate.match(/\n\-\-\-\-\-END CERTIFICATE\-\-\-\-\-\n*$/));
90+
91+
test.ok((data && data.clientKey) == (data && data.serviceKey));
92+
93+
test.ok(data && data.clientKey);
94+
test.ok(data && data.serviceKey);
95+
test.ok(data && data.csr);
96+
1197
test.done();
1298
});
99+
},
100+
101+
"Read default cert data from CSR": function(test){
102+
pem.createCSR(function(error, data){
103+
var csr = (data && data.csr || "").toString();
104+
test.ifError(error);
105+
106+
pem.readCertificateInfo(csr, function(error, data){
107+
test.ifError(error);
108+
test.deepEqual(data,{
109+
country: '',
110+
state: '',
111+
locality: '',
112+
organization: '',
113+
organizationUnit: '',
114+
commonName: 'localhost',
115+
emailAddress: '' })
116+
test.done();
117+
});
118+
});
119+
},
120+
121+
"Read edited cert data from CSR": function(test){
122+
var certInfo = {country:"EE",
123+
state:"Harjumaa",
124+
locality:"Tallinn",
125+
organization:"Node.ee",
126+
organizationUnit:"test",
127+
commonName:"www.node.ee",
128+
emailAddress:"[email protected]"};
129+
pem.createCSR(Object.create(certInfo), function(error, data){
130+
var csr = (data && data.csr || "").toString();
131+
test.ifError(error);
132+
133+
pem.readCertificateInfo(csr, function(error, data){
134+
test.ifError(error);
135+
test.deepEqual(data, certInfo)
136+
test.done();
137+
});
138+
});
139+
},
140+
141+
"Read default cert data from certificate": function(test){
142+
pem.createCertificate(function(error, data){
143+
var certificate = (data && data.certificate || "").toString();
144+
test.ifError(error);
145+
146+
pem.readCertificateInfo(certificate, function(error, data){
147+
test.ifError(error);
148+
test.deepEqual(data,{
149+
country: '',
150+
state: '',
151+
locality: '',
152+
organization: '',
153+
organizationUnit: '',
154+
commonName: 'localhost',
155+
emailAddress: '' })
156+
test.done();
157+
});
158+
});
159+
},
160+
161+
"Read edited cert data from certificate": function(test){
162+
var certInfo = {country:"EE",
163+
state:"Harjumaa",
164+
locality:"Tallinn",
165+
organization:"Node.ee",
166+
organizationUnit:"test",
167+
commonName:"www.node.ee",
168+
emailAddress:"[email protected]"};
169+
pem.createCertificate(Object.create(certInfo), function(error, data){
170+
var certificate = (data && data.certificate || "").toString();
171+
test.ifError(error);
172+
173+
pem.readCertificateInfo(certificate, function(error, data){
174+
test.ifError(error);
175+
test.deepEqual(data, certInfo)
176+
test.done();
177+
});
178+
});
179+
},
180+
181+
"Get public key from private key": function(test){
182+
pem.createPrivateKey(function(error, data){
183+
var key = (data && data.key || "").toString();
184+
test.ifError(error);
185+
test.ok(key);
186+
187+
pem.getPublicKey(key, function(error, data){
188+
var pubkey = (data && data.publicKey || "").toString();
189+
test.ifError(error);
190+
test.ok(pubkey);
191+
192+
test.ok(pubkey.match(/^\n*\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\n/));
193+
test.ok(pubkey.match(/\n\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\n*$/));
194+
195+
test.done();
196+
});
197+
198+
});
199+
},
200+
201+
"Get public key from CSR": function(test){
202+
pem.createCSR(function(error, data){
203+
var key = (data && data.clientKey || "").toString();
204+
test.ifError(error);
205+
test.ok(key);
206+
207+
pem.getPublicKey(key, function(error, data){
208+
var pubkey = (data && data.publicKey || "").toString();
209+
test.ifError(error);
210+
test.ok(pubkey);
211+
212+
test.ok(pubkey.match(/^\n*\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\n/));
213+
test.ok(pubkey.match(/\n\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\n*$/));
214+
215+
test.done();
216+
});
217+
218+
});
219+
},
220+
221+
"Get public key from certificate": function(test){
222+
pem.createCertificate(function(error, data){
223+
var key = (data && data.clientKey || "").toString();
224+
test.ifError(error);
225+
test.ok(key);
226+
227+
pem.getPublicKey(key, function(error, data){
228+
var pubkey = (data && data.publicKey || "").toString();
229+
test.ifError(error);
230+
test.ok(pubkey);
231+
232+
test.ok(pubkey.match(/^\n*\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\n/));
233+
test.ok(pubkey.match(/\n\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\n*$/));
234+
235+
test.done();
236+
});
237+
238+
});
13239
}
14240
}

0 commit comments

Comments
 (0)