-
Notifications
You must be signed in to change notification settings - Fork 59
/
sdpCredentialMaker.js
176 lines (149 loc) · 4.9 KB
/
sdpCredentialMaker.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
/*
* Copyright 2016 Waverley Labs, LLC
*
* This file is part of SDPcontroller
*
* SDPcontroller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SDPcontroller is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
var pem = require('pem');
var fs = require('fs');
var await = require('await');
var prompt = require('prompt');
var config;
var caKeyPassword;
function credentialMaker(configuration) {
config = configuration;
}
credentialMaker.prototype.init = function(callback) {
caKeyPassword = config.caKeyPassword;
if(caKeyPassword || !config.caKeyPasswordRequired)
callback();
else
{
var schema = {
properties: {
password: {
description: 'Enter certificate authority key password',
hidden: true,
replace: '*',
required: true
}
}
};
prompt.start();
prompt.get(schema, function(err,result) {
if(err)
{
console.log("prompt.get failed for cert authority key password");
throw err;
}
else
{
caKeyPassword = result.password;
callback();
}
});
}
};
// Get new credentials for member
credentialMaker.prototype.getNewCredentials = function(memberDetails, callback) {
var promiseNewCreds = await('encryptionKey', 'hmacKey', 'cert');
var newCreds;
getNewKey(config.encryptionKeyLen, function(err, key) {
if (err) promiseNewCreds.fail(err);
else promiseNewCreds.keep('encryptionKey', key);
});
getNewKey(config.hmacKeyLen, function(err, key) {
if (err) promiseNewCreds.fail(err);
else promiseNewCreds.keep('hmacKey', key);
});
getNewCert(memberDetails, function(err, cert) {
if (err) promiseNewCreds.fail(err);
else promiseNewCreds.keep('cert', cert);
});
promiseNewCreds.then( function(creds) {
console.log("New credentials successfully created for sdp member " + memberDetails.sdpid);
var credentials = {
spa_encryption_key_base64: creds.encryptionKey,
spa_hmac_key_base64: creds.hmacKey,
tls_key: creds.cert.clientKey,
tls_cert: creds.cert.certificate
};
callback(null, credentials);
},function(err){
// oops, there was an error
console.log(err);
callback(err, null);
});
};
// Generate a new key for hmac or encryption
function getNewKey(keyLen, callback) {
// this function wants key len in bits, our config
// specified it in bytes, so multiply by 8
pem.createPrivateKey(keyLen*8, function(err, key) {
if (err) {
console.log("key callback got an error");
callback(err, null);
} else {
var tempKeyStr = "";
var finalKey = "";
// get rid of wrapping text and new lines
var result = key.key.split("\n");
for (var i = 1; i < (result.length-1); i++) {
tempKeyStr += result[i];
}
//console.log("tempKeyStr len: %d\ntempKeyStr: %s\n", tempKeyStr.length, tempKeyStr);
// decode the string
var tempKeyBuf = new Buffer(tempKeyStr, 'base64');
//console.log("tempKeyBuf len: %d", tempKeyBuf.length);
// if the result is shorter than keyLen
if(tempKeyBuf.length < keyLen)
{
var error = "pem.createPrivateKey() returned key of decoded length " +
tempKeyBuf.length + ", shorter than required length " + keyLen;
callback(error, null);
}
else
{
//take from end of buffer because beginning is always rather similar
finalKey = tempKeyBuf.toString('base64', tempKeyBuf.length - keyLen);
}
callback(null, finalKey);
}
});
}
// Generate new client certificate and key
function getNewCert(memberDetails, callback) {
var certOptions = {
serviceKey: fs.readFileSync(config.caKey),
serviceKeyPassword: caKeyPassword,
serviceCertificate: fs.readFileSync(config.caCert),
serial: memberDetails.serial,
selfSigned: true,
days: config.daysToExpiration,
country: memberDetails.country,
state: memberDetails.state,
locality: memberDetails.locality,
organization: memberDetails.org,
organizationUnit: memberDetails.org_unit,
commonName: memberDetails.sdpid.toString(),
emailAddress: memberDetails.email
};
pem.createCertificate(certOptions, function(err, keys){
if (err) callback(err, null);
callback(null, keys);
});
}
module.exports = credentialMaker;