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
|
<html>
<head>
<link type="text/css" rel="stylesheet" media="all" href="screen.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="forge/debug.js"></script>
<script type="text/javascript" src="forge/util.js"></script>
<script type="text/javascript" src="forge/log.js"></script>
<script type="text/javascript" src="forge/socket.js"></script>
<script type="text/javascript" src="forge/md5.js"></script>
<script type="text/javascript" src="forge/sha1.js"></script>
<script type="text/javascript" src="forge/hmac.js"></script>
<script type="text/javascript" src="forge/aes.js"></script>
<script type="text/javascript" src="forge/pem.js"></script>
<script type="text/javascript" src="forge/asn1.js"></script>
<script type="text/javascript" src="forge/jsbn.js"></script>
<script type="text/javascript" src="forge/prng.js"></script>
<script type="text/javascript" src="forge/random.js"></script>
<script type="text/javascript" src="forge/oids.js"></script>
<script type="text/javascript" src="forge/rsa.js"></script>
<script type="text/javascript" src="forge/pbe.js"></script>
<script type="text/javascript" src="forge/x509.js"></script>
<script type="text/javascript" src="forge/pki.js"></script>
<script type="text/javascript" src="forge/tls.js"></script>
<script type="text/javascript" src="forge/aesCipherSuites.js"></script>
<script type="text/javascript" src="forge/tlssocket.js"></script>
<script type="text/javascript" src="forge/http.js"></script>
<script type="text/javascript" src="ws-webid.js"></script>
<script type="text/javascript">
//<![CDATA[
// logging category
var cat = 'forge.tests.tls';
swfobject.embedSWF(
'forge/SocketPool.swf', 'socketPool', '0', '0', '9.0.0',
false, {}, {allowscriptaccess: 'always'}, {});
// CA certificate for test server
var certificatePem =
'-----BEGIN CERTIFICATE-----\r\n' +
'MIIEaDCCA1CgAwIBAgIJAJuj0AjEWncuMA0GCSqGSIb3DQEBBQUAMH8xCzAJBgNV\r\n' +
'BAYTAlVTMREwDwYDVQQIEwhWaXJnaW5pYTETMBEGA1UEBxMKQmxhY2tzYnVyZzEd\r\n' +
'MBsGA1UEChMURGlnaXRhbCBCYXphYXIsIEluYy4xGjAYBgNVBAsTEUZvcmdlIFRl\r\n' +
'c3QgU2VydmVyMQ0wCwYDVQQDEwR0ZXN0MB4XDTEwMDcxMzE3MjAzN1oXDTMwMDcw\r\n' +
'ODE3MjAzN1owfzELMAkGA1UEBhMCVVMxETAPBgNVBAgTCFZpcmdp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D+0M35noyaOwWIFu2\r\n' +
'7gPZ0uGJ1n9ZMe/S9yZmmusaIrc66rX4o+fslUlH0g3SrH7yf83M8aOC2pEyCsG0\r\n' +
'mNNfwSFWfmu+1GMRHXJQ/qT8qBX8ZPhzRY2BAS6vr+eh3gwXR6yXLA8Xm1+e+iDU\r\n' +
'gGTQoYkixDIL2nhvd4AFFlE977BiE+0sMS1eJKUUbQ36MLAWb5oOZKHrphEvqMKA\r\n' +
'eGDO3qoDqB5TkZC3x38DXBDvAZ01d9s0fvveag==\r\n' +
'-----END CERTIFICATE-----';
// local aliases
var net = window.forge.net;
var tls = window.forge.tls;
var http = window.forge.http;
var util = window.forge.util;
var client;
function client_init(primed)
{
try
{
var sp = net.createSocketPool({
flashId: 'socketPool',
policyPort: 19945,
msie: false
});
client = http.createClient({
//url: 'https://localhost:4433',
url: 'https://' + window.location.host,
socketPool: sp,
connections: 10,
caCerts: [certificatePem],
// optional cipher suites in order of preference
cipherSuites: [
tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
verify: function(c, verified, depth, certs)
{
forge.log.debug(cat,
'TLS certificate ' + depth + ' verified', verified);
// Note: change to always true to test verifying without cert
//return verified;
// FIXME: temporarily accept any cert to allow hitting any bpe
if(verified !== true)
{
forge.log.warning(cat,
'Certificate NOT verified. Ignored for test.');
}
return true;
},
primeTlsSockets: primed
});
document.getElementById('feedback').innerHTML =
'http client created';
}
catch(ex)
{
forge.log.error(cat, ex);
}
return false;
}
function client_cleanup()
{
var sp = client.socketPool;
client.destroy();
sp.destroy();
document.getElementById('feedback').innerHTML =
'http client cleaned up';
return false;
}
function client_send()
{
/*
var request = http.createRequest({
method: 'POST',
path: '/',
body: 'echo=foo',
headers: [{'Content-Type': 'application/x-www-form-urlencoded'}]
});
*/
var request = http.createRequest({
method: 'GET',
path: '/'
});
client.send({
request: request,
connected: function(e)
{
forge.log.debug(cat, 'connected', e);
},
headerReady: function(e)
{
forge.log.debug(cat, 'header ready', e);
},
bodyReady: function(e)
{
forge.log.debug(cat, 'body ready', e);
// FIXME: current test server doesn't seem to handle keep-alive
// correctly, so close connection
e.socket.close();
},
error: function(e)
{
forge.log.error(cat, 'error', e);
}
});
document.getElementById('feedback').innerHTML =
'http request sent';
return false;
}
function client_send_10()
{
for(var i = 0; i < 10; ++i)
{
client_send();
}
return false;
}
function client_stress()
{
for(var i = 0; i < 10; ++i)
{
setTimeout(function()
{
for(var i = 0; i < 10; ++i)
{
client_send();
}
}, 0);
}
return false;
}
function client_cookies()
{
var cookie =
{
name: 'test-cookie',
value: 'test-value',
maxAge: -1,
secure: true,
path: '/'
};
client.setCookie(cookie);
forge.log.debug(cat, 'cookie', client.getCookie('test-cookie'));
}
function client_clear_cookies()
{
client.clearCookies();
}
function websocket_test()
{
// create certificate
var cn = 'client';
console.log(
'Generating 512-bit key-pair and certificate for \"' + cn + '\".');
var keys = forge.pki.rsa.generateKeyPair(512);
console.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 cert and private key in PEM format
cert = forge.pki.certificateToPem(cert);
privateKey = forge.pki.privateKeyToPem(keys.privateKey);
console.log('certificate created for \"' + cn + '\": \n' + cert);
// create websocket
var ws = new WebSocket('ws://localhost:8080');
console.log('created websocket', ws);
// create TLS client
var success = false;
var tls = forge.tls.createConnection(
{
server: false,
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],
virtualHost: 'server',
verify: function(c, verified, depth, certs)
{
console.log(
'TLS Client verifying certificate w/CN: \"' +
certs[0].subject.getField('CN').value +
'\", verified: ' + verified + '...');
// accept any certificate from the server for this test
return true;
},
connected: function(c)
{
console.log('Client connected...');
// send message to server
setTimeout(function()
{
c.prepare('Hello Server');
}, 1);
},
getCertificate: function(c, hint)
{
console.log('Client getting certificate ...');
return cert;
},
getPrivateKey: function(c, cert)
{
return privateKey;
},
tlsDataReady: function(c)
{
// send base64-encoded TLS data to server
ws.send(forge.util.encode64(c.tlsData.getBytes()));
},
dataReady: function(c)
{
var response = c.data.getBytes();
console.log('Client received \"' + response + '\"');
success = (response === 'Hello Client');
c.close();
},
closed: function(c)
{
console.log('Client disconnected.');
if(success)
{
console.log('PASS');
}
else
{
console.log('FAIL');
}
},
error: function(c, error)
{
console.log('Client error: ' + error.message);
}
});
ws.onopen = function(evt)
{
console.log('websocket connected');
// do TLS handshake
tls.handshake();
};
ws.onmessage = function(evt)
{
// base64-decode data and process it
tls.process(forge.util.decode64(evt.data));
};
ws.onclose = function(evt)
{
console.log('websocket closed');
};
}
//]]>
</script>
</head>
<body>
<div class="nav"><a href="index.html">Forge Tests</a> / TLS</div>
<div class="header">
<h1>TLS Test</h1>
</div>
<div class="content">
<!-- div used to hold the flash socket pool implemenation -->
<div id="socketPool">
<p>Could not load the flash SocketPool.</p>
</div>
<fieldset class="section">
<ul>
<li>Use the controls below to test the HTTP client over TLS.</li>
<li>You currently need a JavaScript console to view the output.</li>
<li>This test connects to a TLS server so you must have one running. The easiest way to run this test is to start the test server with --tls and load this page over HTTPS.</li>
</ul>
</fieldset>
<fieldset class="section">
<legend>Controls</legend>
<div id="controls">
<button id="init" onclick="javascript:return client_init(false);">init</button>
<button id="init_primed" onclick="javascript:return client_init(true);">init primed</button>
<button id="cleanup" onclick="javascript:return client_cleanup();">cleanup</button>
<button id="send" onclick="javascript:return client_send();">send</button>
<button id="send10" onclick="javascript:return client_send_10();">send 10</button>
<button id="stress" onclick="javascript:return client_stress();">stress</button>
<button id="client_cookies" onclick="javascript:return client_cookies();">cookies</button>
<button id="clear_cookies" onclick="javascript:return client_clear_cookies();">clear cookies</button>
<button id="websocket" onclick="javascript:return websocket_test();">websocket test</button>
<button id="websocket-webid" onclick="javascript:return websocket_webid('localhost', 8080);">websocket webid test</button>
</div>
</fieldset>
<fieldset class="section">
<legend>Feedback</legend>
<p>Feedback from the flash SocketPool:</p>
<div id="feedback">
None
</div>
</fieldset>
</div>
</body>
</html>
|