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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
var forge = require('../js/forge');
var fs = require('fs');
var http = require('http');
//var rdf = require('./rdflib');
var sys = require('sys');
var urllib = require('url');
var ws = require('./ws');
// remove xmlns from input
var normalizeNs = function(input, ns) {
var rval = null;
// primitive
if(typeof input === 'string' ||
typeof input === 'number' ||
typeof input === 'boolean') {
rval = input;
}
// array
else if(forge.util.isArray(input)) {
rval = [];
for(var i = 0; i < input.length; ++i) {
rval.push(normalizeNs(input[i], ns));
}
}
// object
else {
if('@' in input) {
// copy namespace map
var newNs = {};
for(var key in ns) {
newNs[key] = ns[key];
}
ns = newNs;
// update namespace map
for(var key in input['@']) {
if(key.indexOf('xmlns:') === 0) {
ns[key.substr(6)] = input['@'][key];
}
}
}
rval = {};
for(var key in input) {
if(key.indexOf('xmlns:') !== 0) {
var value = input[key];
var colon = key.indexOf(':');
if(colon !== -1) {
var prefix = key.substr(0, colon);
if(prefix in ns) {
key = ns[prefix] + key.substr(colon + 1);
}
}
rval[key] = normalizeNs(value, ns);
}
}
}
return rval;
};
// gets public key from WebID rdf
var getPublicKey = function(data, uri, callback) {
// FIXME: use RDF library to simplify code below
//var kb = new rdf.RDFParser(rdf.IndexedFormula(), uri).loadBuf(data);
//var CERT = rdf.Namespace('http://www.w3.org/ns/auth/cert#');
//var RSA = rdf.Namespace('http://www.w3.org/ns/auth/rsa#');
var RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
var CERT = 'http://www.w3.org/ns/auth/cert#';
var RSA = 'http://www.w3.org/ns/auth/rsa#';
var desc = RDF + 'Description';
var about = RDF + 'about';
var type = RDF + 'type';
var resource = RDF + 'resource';
var publicKey = RSA + 'RSAPublicKey';
var modulus = RSA + 'modulus';
var exponent = RSA + 'public_exponent';
var identity = CERT + 'identity';
var hex = CERT + 'hex';
var decimal = CERT + 'decimal';
// gets a resource identifer from a node
var getResource = function(node, key) {
var rval = null;
// special case 'about'
if(key === about) {
if('@' in node && about in node['@']) {
rval = node['@'][about];
}
}
// any other resource
else if(
key in node &&
typeof node[key] === 'object' && !forge.util.isArray(node[key]) &&
'@' in node[key] && resource in node[key]['@']) {
rval = node[key]['@'][resource];
}
return rval;
};
// parse XML
uri = urllib.parse(uri);
var xml2js = require('./xml2js');
var parser = new xml2js.Parser();
parser.addListener('end', function(result) {
// normalize namespaces
result = normalizeNs(result, {});
// find grab all public keys whose identity matches hash from uri
var keys = [];
if(desc in result) {
// normalize RDF descriptions to array
if(!forge.util.isArray(result[desc])) {
desc = [result[desc]];
}
else {
desc = result[desc];
}
// collect properties for all resources
var graph = {};
for(var i = 0; i < desc.length; ++i) {
var node = desc[i];
var res = {};
for(var key in node) {
var obj = getResource(node, key);
res[key] = (obj === null) ? node[key] : obj;
}
graph[getResource(node, about) || ''] = res;
}
// for every public key w/identity that matches the uri hash
// save the public key modulus and exponent
for(var r in graph) {
var props = graph[r];
if(identity in props &&
type in props &&
props[type] === publicKey &&
props[identity] === uri.hash &&
modulus in props &&
exponent in props &&
props[modulus] in graph &&
props[exponent] in graph &&
hex in graph[props[modulus]] &&
decimal in graph[props[exponent]]) {
keys.push({
modulus: graph[props[modulus]][hex],
exponent: graph[props[exponent]][decimal]
});
}
}
}
sys.log('Public keys from RDF: ' + JSON.stringify(keys));
callback(keys);
});
parser.parseString(data);
};
// compares two public keys for equality
var comparePublicKeys = function(key1, key2) {
return key1.modulus === key2.modulus && key1.exponent === key2.exponent;
};
// gets the RDF data from a URL
var fetchUrl = function(url, callback, redirects) {
// allow 3 redirects by default
if(typeof(redirects) === 'undefined') {
redirects = 3;
}
sys.log('Fetching URL: \"' + url + '\"');
// parse URL
url = forge.util.parseUrl(url);
var client = http.createClient(
url.port, url.fullHost, url.scheme === 'https');
var request = client.request('GET', url.path, {
'Host': url.host,
'Accept': 'application/rdf+xml'
});
request.addListener('response', function(response) {
var body = '';
// error, return empty body
if(response.statusCode >= 400) {
callback(body);
}
// follow redirect
else if(response.statusCode === 302) {
if(redirects > 0) {
// follow redirect
fetchUrl(response.headers.location, callback, --redirects);
}
else {
// return empty body
callback(body);
}
}
// handle data
else {
response.setEncoding('utf8');
response.addListener('data', function(chunk) {
body += chunk;
});
response.addListener('end', function() {
callback(body);
});
}
});
request.end();
};
// does WebID authentication
var authenticateWebId = function(c, state) {
var auth = false;
// get client-certificate
var cert = c.peerCertificate;
// get public key from certificate
var publicKey = {
modulus: cert.publicKey.n.toString(16).toLowerCase(),
exponent: cert.publicKey.e.toString(10)
};
sys.log(
'Server verifying certificate w/CN: \"' +
cert.subject.getField('CN').value + '\"\n' +
'Public Key: ' + JSON.stringify(publicKey));
// build queue of subject alternative names to authenticate with
var altNames = [];
var ext = cert.getExtension({name: 'subjectAltName'});
if(ext !== null && ext.altNames) {
for(var i = 0; i < ext.altNames.length; ++i) {
var altName = ext.altNames[i];
if(altName.type === 6) {
altNames.push(altName.value);
}
}
}
// create authentication processor
var authNext = function() {
if(!auth) {
// no more alt names, auth failed
if(altNames.length === 0) {
sys.log('WebID authentication FAILED.');
c.prepare(JSON.stringify({
success: false,
error: 'Not Authenticated'
}));
c.close();
}
// try next alt name
else {
// fetch URL
var url = altNames.shift();
fetchUrl(url, function(body) {
// get public key
getPublicKey(body, url, function(keys) {
// compare public keys from RDF until one matches
for(var i = 0; !auth && i < keys.length; ++i) {
auth = comparePublicKeys(keys[i], publicKey);
}
if(auth) {
// send authenticated notice to client
sys.log('WebID authentication PASSED.');
state.authenticated = true;
c.prepare(JSON.stringify({
success: true,
cert: forge.pki.certificateToPem(cert),
webID: url,
rdf: forge.util.encode64(body)
}));
}
else {
// try next alt name
authNext();
}
});
});
}
}
};
// do auth
authNext();
};
// creates credentials (private key + certificate)
var createCredentials = function(cn, credentials) {
sys.log('Generating 512-bit key-pair and certificate for \"' + cn + '\".');
var keys = forge.pki.rsa.generateKeyPair(512);
sys.log('key-pair created.');
var cert = forge.pki.createCertificate();
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
var attrs = [{
name: 'commonName',
value: cn
}, {
name: 'countryName',
value: 'US'
}, {
shortName: 'ST',
value: 'Virginia'
}, {
name: 'localityName',
value: 'Blacksburg'
}, {
name: 'organizationName',
value: 'Test'
}, {
shortName: 'OU',
value: 'Test'
}];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([{
name: 'basicConstraints',
cA: true
}, {
name: 'keyUsage',
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true
}, {
name: 'subjectAltName',
altNames: [{
type: 6, // URI
value: 'http://myuri.com/webid#me'
}]
}]);
// FIXME: add subjectKeyIdentifier extension
// FIXME: add authorityKeyIdentifier extension
cert.publicKey = keys.publicKey;
// self-sign certificate
cert.sign(keys.privateKey);
// save credentials
credentials.key = forge.pki.privateKeyToPem(keys.privateKey);
credentials.cert = forge.pki.certificateToPem(cert);
sys.log('Certificate created for \"' + cn + '\": \n' + credentials.cert);
};
// initialize credentials
var credentials = {
key: null,
cert: null
};
// read private key from file
var readPrivateKey = function(filename) {
credentials.key = fs.readFileSync(filename);
// try to parse from PEM as test
forge.pki.privateKeyFromPem(credentials.key);
};
// read certificate from file
var readCertificate = function(filename) {
credentials.cert = fs.readFileSync(filename);
// try to parse from PEM as test
forge.pki.certificateFromPem(credentials.cert);
};
// parse command line options
var opts = require('opts');
var options = [
{ short : 'v'
, long : 'version'
, description : 'Show version and exit'
, callback : function() { console.log('v1.0'); process.exit(1); }
},
{ short : 'p'
, long : 'port'
, description : 'The port to listen for WebSocket connections on'
, value : true
},
{ long : 'key'
, description : 'The server private key file to use in PEM format'
, value : true
, callback : readPrivateKey
},
{ long : 'cert'
, description : 'The server certificate file to use in PEM format'
, value : true
, callback : readCertificate
}
];
opts.parse(options, true);
// create credentials for server
if(credentials.key === null || credentials.cert === null) {
createCredentials('server', credentials);
}
// function to create TLS server connection
var createTls = function(websocket) {
var state = {
authenticated: false
};
return forge.tls.createConnection({
server: true,
caStore: [],
sessionCache: {},
// supported cipher suites in order of preference
cipherSuites: [
forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
connected: function(c) {
sys.log('Server connected');
// do WebID authentication
try {
authenticateWebId(c, state);
}
catch(ex) {
c.close();
}
},
verifyClient: true,
verify: function(c, verified, depth, certs) {
// accept certs w/unknown-CA (48)
if(verified === 48) {
verified = true;
}
return verified;
},
getCertificate: function(c, hint) {
sys.log('Server using certificate for \"' + hint[0] + '\"');
return credentials.cert;
},
getPrivateKey: function(c, cert) {
return credentials.key;
},
tlsDataReady: function(c) {
// send base64-encoded TLS data over websocket
websocket.write(forge.util.encode64(c.tlsData.getBytes()));
},
dataReady: function(c) {
// ignore any data until connection is authenticated
if(state.authenticated) {
sys.log('Server received \"' + c.data.getBytes() + '\"');
}
},
closed: function(c) {
sys.log('Server disconnected');
websocket.end();
},
error: function(c, error) {
sys.log('Server error: ' + error.message);
}
});
};
// create websocket server
var port = opts.get('port') || 8080;
ws.createServer(function(websocket) {
// create TLS server connection
var tls = createTls(websocket);
websocket.addListener('connect', function(resource) {
sys.log('WebSocket connected: ' + resource);
// close connection after 30 second timeout
setTimeout(websocket.end, 30 * 1000);
});
websocket.addListener('data', function(data) {
// base64-decode data and process it
tls.process(forge.util.decode64(data));
});
websocket.addListener('close', function() {
sys.log('WebSocket closed');
});
}).listen(port);
sys.log('WebSocket WebID server running on port ' + port);
|