-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
test-tls-psk-client.js
97 lines (87 loc) Β· 2.14 KB
/
test-tls-psk-client.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
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
if (!common.opensslCli)
common.skip('missing openssl cli');
const assert = require('assert');
const tls = require('tls');
const net = require('net');
const { spawn } = require('child_process');
const CIPHERS = 'PSK+HIGH';
const KEY = 'd731ef57be09e5204f0b205b60627028';
const IDENTITY = 'Client_identity'; // Hardcoded by `openssl s_server`
const server = spawn(common.opensslCli, [
's_server',
'-accept', common.PORT,
'-cipher', CIPHERS,
'-psk', KEY,
'-psk_hint', IDENTITY,
'-nocert',
'-rev',
]);
const cleanUp = (err) => {
clearTimeout(timeout);
if (err)
console.log('Failed:', err);
server.kill();
process.exitCode = err ? 1 : 0;
};
const timeout = setTimeout(() => cleanUp('Timed out'), 5000);
function waitForPort(port, cb) {
const socket = net.connect(common.PORT, () => {
socket.on('data', () => {});
socket.end();
socket.on('end', cb);
});
socket.on('error', (e) => {
if (e.code === 'ENOENT' || e.code === 'ECONNREFUSED') {
setTimeout(() => waitForPort(port, cb), 1000);
} else {
cb(e);
}
});
}
waitForPort(common.PORT, common.mustCall((err) => {
if (err) {
cleanUp(err);
return;
}
const message = 'hello';
const reverse = message.split('').reverse().join('');
runClient(message, common.mustCall((err, data) => {
try {
if (!err) assert.strictEqual(data.trim(), reverse);
} finally {
cleanUp(err);
}
}));
}));
function runClient(message, cb) {
const s = tls.connect(common.PORT, {
ciphers: CIPHERS,
checkServerIdentity: () => {},
pskCallback(hint) {
// 'hint' will be null in TLS1.3.
if (hint === null || hint === IDENTITY) {
return {
identity: IDENTITY,
psk: Buffer.from(KEY, 'hex')
};
}
}
});
s.on('secureConnect', common.mustCall(() => {
let data = '';
s.on('data', common.mustCallAtLeast((d) => {
data += d;
}));
s.on('end', common.mustCall(() => {
cb(null, data);
}));
s.end(message);
}));
s.on('error', (e) => {
cb(e);
});
}