• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6if (!common.opensslCli)
7  common.skip('missing openssl cli');
8
9const assert = require('assert');
10
11const tls = require('tls');
12const spawn = require('child_process').spawn;
13
14const CIPHERS = 'PSK+HIGH';
15const KEY = 'd731ef57be09e5204f0b205b60627028';
16const IDENTITY = 'TestUser';
17
18const server = tls.createServer({
19  ciphers: CIPHERS,
20  pskIdentityHint: IDENTITY,
21  pskCallback(socket, identity) {
22    assert.ok(socket instanceof tls.TLSSocket);
23    assert.ok(typeof identity === 'string');
24    if (identity === IDENTITY)
25      return Buffer.from(KEY, 'hex');
26  }
27});
28
29server.on('connection', common.mustCall());
30
31server.on('secureConnection', (socket) => {
32  socket.write('hello\r\n');
33
34  socket.on('data', (data) => {
35    socket.write(data);
36  });
37});
38
39let gotHello = false;
40let sentWorld = false;
41let gotWorld = false;
42
43server.listen(0, () => {
44  const client = spawn(common.opensslCli, [
45    's_client',
46    '-connect', `127.0.0.1:${server.address().port}`,
47    '-cipher', CIPHERS,
48    '-psk', KEY,
49    '-psk_identity', IDENTITY,
50  ]);
51
52  let out = '';
53
54  client.stdout.setEncoding('utf8');
55  client.stdout.on('data', (d) => {
56    out += d;
57
58    if (!gotHello && /hello/.test(out)) {
59      gotHello = true;
60      client.stdin.write('world\r\n');
61      sentWorld = true;
62    }
63
64    if (!gotWorld && /world/.test(out)) {
65      gotWorld = true;
66      client.stdin.end();
67    }
68  });
69
70  client.on('exit', common.mustCall((code) => {
71    assert.ok(gotHello);
72    assert.ok(sentWorld);
73    assert.ok(gotWorld);
74    assert.strictEqual(code, 0);
75    server.close();
76  }));
77});
78