|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +if (!common.hasCrypto) |
| 4 | + common.skip('missing crypto'); |
| 5 | + |
| 6 | +const assert = require('assert'); |
| 7 | +const fixtures = require('../common/fixtures'); |
| 8 | +const h2 = require('http2'); |
| 9 | +const tls = require('tls'); |
| 10 | +const { once } = require('events'); |
| 11 | + |
| 12 | +function load(name) { |
| 13 | + return fixtures.readKey(name); |
| 14 | +} |
| 15 | + |
| 16 | +const contexts = { |
| 17 | + agent1: tls.createSecureContext({ |
| 18 | + key: load('agent1-key.pem'), |
| 19 | + cert: load('agent1-cert.pem'), |
| 20 | + }), |
| 21 | + agent3: tls.createSecureContext({ |
| 22 | + key: load('agent3-key.pem'), |
| 23 | + cert: load('agent3-cert.pem'), |
| 24 | + }), |
| 25 | +}; |
| 26 | + |
| 27 | +function makeRequest(server, authority, options) { |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + let session; |
| 30 | + let settled = false; |
| 31 | + |
| 32 | + function done(err, result) { |
| 33 | + if (settled) |
| 34 | + return; |
| 35 | + settled = true; |
| 36 | + |
| 37 | + if (err) { |
| 38 | + client.destroy(); |
| 39 | + reject(err); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + client.close(); |
| 44 | + resolve({ session, result }); |
| 45 | + } |
| 46 | + |
| 47 | + const client = h2.connect(authority, { |
| 48 | + ...options, |
| 49 | + ALPNProtocols: ['h2'], |
| 50 | + createConnection(url, connectOptions) { |
| 51 | + const socket = tls.connect({ |
| 52 | + ...connectOptions, |
| 53 | + ALPNProtocols: ['h2'], |
| 54 | + host: '127.0.0.1', |
| 55 | + port: server.address().port, |
| 56 | + servername: connectOptions.servername || url.hostname, |
| 57 | + }); |
| 58 | + |
| 59 | + socket.once('session', (value) => { |
| 60 | + if (session === undefined) |
| 61 | + session = value; |
| 62 | + }); |
| 63 | + |
| 64 | + return socket; |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + client.once('error', (err) => done(err)); |
| 69 | + client.once('connect', () => { |
| 70 | + const req = client.request({ ':path': '/' }); |
| 71 | + req.setEncoding('utf8'); |
| 72 | + req.on('response', () => {}); |
| 73 | + req.on('error', (err) => done(err)); |
| 74 | + req.on('data', () => {}); |
| 75 | + req.on('end', () => { |
| 76 | + done(null, { |
| 77 | + authority: client.socket.servername, |
| 78 | + authorized: client.socket.authorized, |
| 79 | + reused: client.socket.isSessionReused(), |
| 80 | + authorizationError: client.socket.authorizationError, |
| 81 | + }); |
| 82 | + }); |
| 83 | + req.end(); |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +const server = h2.createSecureServer({ |
| 89 | + key: load('agent1-key.pem'), |
| 90 | + cert: load('agent1-cert.pem'), |
| 91 | + allowHTTP1: false, |
| 92 | + minVersion: 'TLSv1.2', |
| 93 | + maxVersion: 'TLSv1.2', |
| 94 | + SNICallback(servername, cb) { |
| 95 | + cb(null, contexts[servername] || contexts.agent1); |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +server.on('stream', (stream) => { |
| 100 | + stream.respond({ ':status': 200 }); |
| 101 | + stream.end('ok'); |
| 102 | +}); |
| 103 | + |
| 104 | +(async function() { |
| 105 | + server.listen(0); |
| 106 | + await once(server, 'listening'); |
| 107 | + |
| 108 | + try { |
| 109 | + const port = server.address().port; |
| 110 | + const first = await makeRequest(server, `https://agent1:${port}`, { |
| 111 | + ca: [load('ca1-cert.pem')], |
| 112 | + minVersion: 'TLSv1.2', |
| 113 | + maxVersion: 'TLSv1.2', |
| 114 | + }); |
| 115 | + |
| 116 | + assert.strictEqual(first.result.authorized, true); |
| 117 | + assert.strictEqual(first.result.reused, false); |
| 118 | + assert(first.session); |
| 119 | + |
| 120 | + await assert.rejects( |
| 121 | + makeRequest(server, `https://agent3:${port}`, { |
| 122 | + ca: [load('ca1-cert.pem')], |
| 123 | + minVersion: 'TLSv1.2', |
| 124 | + maxVersion: 'TLSv1.2', |
| 125 | + session: first.session, |
| 126 | + }), |
| 127 | + { |
| 128 | + code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', |
| 129 | + }, |
| 130 | + ); |
| 131 | + |
| 132 | + await assert.rejects( |
| 133 | + makeRequest(server, `https://agent3:${port}`, { |
| 134 | + ca: [load('ca1-cert.pem')], |
| 135 | + minVersion: 'TLSv1.2', |
| 136 | + maxVersion: 'TLSv1.2', |
| 137 | + }), |
| 138 | + { |
| 139 | + code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', |
| 140 | + }, |
| 141 | + ); |
| 142 | + } finally { |
| 143 | + server.close(); |
| 144 | + await once(server, 'close'); |
| 145 | + } |
| 146 | +})().then(common.mustCall()); |
0 commit comments