forked from dimastopel/certdepot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
executable file
·261 lines (217 loc) · 7.34 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var exec = require('child_process').exec;
var uuid = require('node-uuid');
var fs = require('fs');
module.exports = function(app, models, mongoose){
var title = "Certificate Depot"
var description = "Create your self-signed SSL certificate instantly and for free."
//var certPath = "/root/certs/"; // was /home/dima/certs
var certPath = "/home/dima/certs/";
//var certPath = "~/certs/";
/**
* Index
*/
app.get('/', function(req, res){
//get all the examples
//models.examples.find({}, function(err, docs){
var connAddr = req.connection.remoteAddress
var sockAddr = req.socket.remoteAddress
var msg = 'Connection from: ' + connAddr + ' ' + sockAddr + ' for main \n';
console.log(msg);
var ws = fs.createWriteStream('feedback/connections.txt', {flags: 'a', encoding: 'utf-8', mode: 0666 });
ws.end(msg);
//render the index page
res.render('index.jade', {
locals: {
title: title,
description: description,
page: 'index'
}
});
});
//});
/**
* Help
*/
app.get('/help', function(req, res){
//render the index page
res.render('help.jade', {
locals: {
title: title,
description: description,
page: 'help'
}
});
});
function getCertNames(id) {
var prefix = certPath + id + ".";
var names = {};
names.private = prefix + "private.pem";
names.public = prefix + "public.pem";
names.private_nodir = id + "." + "private.pem";
names.public_nodir = id + "." + "public.pem";
names.pfx = prefix + "pfx";
names.zip = prefix + "private_public.zip";
names.pfx_nodir = id + "." + "pfx";
names.zip_nodir = id + "." + "private_public.zip";
return names;
}
function escapeShell(cmd) {
return '"'+cmd.replace(/(["\s'$`\\])/g,'\\$1')+'"';
};
/**
* Feedback
*/
app.post('/feedback', function(req, res, next) {
var feedback = req.body.feedback;
var connAddr = req.connection.remoteAddress
var sockAddr = req.socket.remoteAddress
var msg = connAddr + ' ' + sockAddr + ' ' + feedback + '\n';
console.log('feedback: ' + msg);
var ws = fs.createWriteStream('feedback/feedback.txt', {flags: 'a', encoding: 'utf-8', mode: 0666 });
ws.end(msg);
res.send('Thanks!');
});
/**
* Create cert
*/
app.post('/create', function(req, res, next) {
//create the cert for a given id
var cn = req.body.cn; //
var days = req.body.days; //
var country = req.body.country;
var state = req.body.state;
var city = req.body.city;
var org = req.body.org;
var orgUnit = req.body.orgUnit;
var email = req.body.email;
var pfxPass = req.body.pfxPass; //
// get other cert info
if (!cn || cn === "")
{
console.log("Error: CN is empty. Doing nothing.");
res.send(400, {error: "Common Name can't be empty"});
return;
}
var id = uuid.v4();
var names = getCertNames(id);
/*
var opts = { encoding: 'utf8',
timeout: 0,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: "d:\\Utils\\OpenSSL-Win32\\bin\\",
env: null };
*/
var command = "openssl genrsa -out " + names.private + " 1024";
exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
res.send(500, {error: 'Failed to generate private key: ' + error});
return;
}
var subj = "/commonName=" + cn;
if (country && country != "") {
subj += "/C=" + country;
}
if (state && state != "") {
subj += "/ST=" + state;
}
if (city && city != "") {
subj += "/L=" + city;
}
if (org && org != "") {
subj += "/O=" + org;
}
if (orgUnit && orgUnit != "") {
subj += "/OU=" + orgUnit;
}
if (email && email != "") {
subj += "/emailAddress=" + email;
}
console.log("subj: " + subj);
command = "openssl req -x509 -new -batch -subj " + escapeShell(subj) + " -key " + names.private + " -out " + names.public + " -days " + escapeShell(days);
exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
res.send(500, {error: 'Failed to generate public certificate: ' + error});
return;
}
if (!pfxPass || pfxPass === "") {
pfxPass = "password";
}
command = "openssl pkcs12 -export -inkey " + names.private + " -out " + names.pfx + " -in " + names.public + " -password pass:" + escapeShell(pfxPass);
exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
res.send(500, {error: 'Failed to create pfx: ' + error});
return;
}
command = "bash ~/github/certdepot/createZip.sh " + names.zip_nodir + " " + names.private_nodir + " " + names.public_nodir;
exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
res.send(500, {error: 'Failed to create ZIP archive: ' + error});
return;
}
console.log('Successfully generated certs for CN: ' + cn + ' ID:' + id);
}
);
}
);
}
);
}
);
res.header('Content-Type', 'application/json');
res.send({id:id});
});
app.get('/getcert/id/:certId/type/:certType', function(req, res, next) {
var id = req.params.certId;
var type = req.params.certType;
var names = getCertNames(id);
console.log('Retreiving certs for id: ' + id);
//console.log(JSON.stringify(names));
if (type === "zip") {
if (!fs.existsSync(names.zip)) {
console.warn('can not find: ' + names.zip);
res.send(404, {error: 'Can not find cert for id/type: ' + id + "/" + type});
return;
}
res.download(names.zip, names.zip, function(err) {
if (err) {
console.error('Can not return zip: ' + err);
}
});
} else if (type === "pfx") {
res.download(names.pfx, names.pfx, function(err) {
if (err) {
console.error('Can not return pfx: ' + err);
}
});
} else {
console.warn('Invalid type was sent: ' + type);
res.send(400, {error: 'Can not return cert for this type: ' + type});
}
});
app.get('/certcount', function(req, res, next) {
// TODO: create a separate counter and delete the certs
console.log('Retreiving certs count');
var command = "ls " + certPath + "*.zip | wc -l";
exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
res.send(500, {error: 'Failed to get certificate count: ' + error});
return;
}
var count = stdout.toString().trim();
console.log('Successfully retreived certs count: ' + count);
res.send({count:count});
}
);
});
};