|
| 1 | +// Flags: --experimental-quic --no-warnings |
| 2 | + |
| 3 | +import { hasQuic, skip, mustCall } from '../common/index.mjs'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import * as fixtures from '../common/fixtures.mjs'; |
| 6 | + |
| 7 | +if (!hasQuic) { |
| 8 | + skip('QUIC is not enabled'); |
| 9 | +} |
| 10 | + |
| 11 | +const { listen, connect } = await import('node:quic'); |
| 12 | +const { createPrivateKey } = await import('node:crypto'); |
| 13 | +const tls = await import('node:tls'); |
| 14 | + |
| 15 | +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); |
| 16 | +const cert = fixtures.readKey('agent1-cert.pem'); |
| 17 | + |
| 18 | +// Certificate compression (RFC 8879) depends on the OpenSSL build linking in |
| 19 | +// the compression libraries. Reuse the node:tls detection helper to decide |
| 20 | +// whether the feature is available. |
| 21 | +const supported = tls.getCertificateCompressionAlgorithms(); |
| 22 | +if (supported.length === 0) { |
| 23 | + skip('certificate compression not supported by this OpenSSL build'); |
| 24 | +} |
| 25 | + |
| 26 | +// --------------------------------------------------------------------------- |
| 27 | +// Option validation. The certificateCompression option is parsed by |
| 28 | +// processTlsOptions during connect()/listen(), so invalid values reject the |
| 29 | +// returned promise before any network activity happens. |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | +{ |
| 32 | + // Non-array values are rejected. |
| 33 | + await assert.rejects( |
| 34 | + connect('127.0.0.1:4433', { certificateCompression: true }), |
| 35 | + { code: 'ERR_INVALID_ARG_TYPE' }); |
| 36 | + |
| 37 | + await assert.rejects( |
| 38 | + connect('127.0.0.1:4433', { certificateCompression: 'zlib' }), |
| 39 | + { code: 'ERR_INVALID_ARG_TYPE' }); |
| 40 | + |
| 41 | + // Unknown algorithm names are rejected. |
| 42 | + await assert.rejects( |
| 43 | + connect('127.0.0.1:4433', { certificateCompression: ['invalid'] }), |
| 44 | + { code: 'ERR_INVALID_ARG_VALUE', |
| 45 | + message: /must be 'zlib', 'brotli', or 'zstd'/ }); |
| 46 | + |
| 47 | + // Non-string entries are rejected. |
| 48 | + await assert.rejects( |
| 49 | + connect('127.0.0.1:4433', { certificateCompression: [1] }), |
| 50 | + { code: 'ERR_INVALID_ARG_VALUE', |
| 51 | + message: /must be 'zlib', 'brotli', or 'zstd'/ }); |
| 52 | + |
| 53 | + // At most three algorithms may be specified. |
| 54 | + await assert.rejects( |
| 55 | + connect('127.0.0.1:4433', { |
| 56 | + certificateCompression: ['zlib', 'brotli', 'zstd', 'zlib'], |
| 57 | + }), |
| 58 | + { code: 'ERR_INVALID_ARG_VALUE', message: /at most 3 algorithms/ }); |
| 59 | +} |
| 60 | + |
| 61 | +// --------------------------------------------------------------------------- |
| 62 | +// Functional handshakes. Enabling certificate compression must not break the |
| 63 | +// TLS 1.3 handshake. The on-the-wire size reduction is exercised by the |
| 64 | +// node:tls certificate compression test; over QUIC the payload is encrypted |
| 65 | +// and carried in UDP datagrams, so here we assert the handshake completes |
| 66 | +// successfully with the option enabled on the server, the client, or both. |
| 67 | +// --------------------------------------------------------------------------- |
| 68 | +async function handshake({ serverAlgs, clientAlgs }) { |
| 69 | + const serverOpened = Promise.withResolvers(); |
| 70 | + |
| 71 | + const endpoint = await listen(mustCall(async (serverSession) => { |
| 72 | + const info = await serverSession.opened; |
| 73 | + assert.strictEqual(info.protocol, 'h3'); |
| 74 | + serverOpened.resolve(); |
| 75 | + serverSession.close(); |
| 76 | + }), { |
| 77 | + sni: { '*': { keys: [key], certs: [cert] } }, |
| 78 | + ...(serverAlgs !== undefined ? |
| 79 | + { certificateCompression: serverAlgs } : {}), |
| 80 | + }); |
| 81 | + |
| 82 | + const clientSession = await connect(endpoint.address, { |
| 83 | + servername: 'localhost', |
| 84 | + verifyPeer: 'manual', |
| 85 | + ...(clientAlgs !== undefined ? |
| 86 | + { certificateCompression: clientAlgs } : {}), |
| 87 | + }); |
| 88 | + |
| 89 | + const info = await clientSession.opened; |
| 90 | + assert.strictEqual(info.protocol, 'h3'); |
| 91 | + |
| 92 | + await serverOpened.promise; |
| 93 | + await clientSession.close(); |
| 94 | + await endpoint.close(); |
| 95 | +} |
| 96 | + |
| 97 | +// Each supported algorithm negotiates a successful handshake when enabled on |
| 98 | +// both peers. |
| 99 | +for (const algo of supported) { |
| 100 | + await handshake({ serverAlgs: [algo], clientAlgs: [algo] }); |
| 101 | +} |
| 102 | + |
| 103 | +// All supported algorithms advertised together. |
| 104 | +await handshake({ serverAlgs: supported, clientAlgs: supported }); |
| 105 | + |
| 106 | +// Asymmetric configurations must also complete: a peer that does not advertise |
| 107 | +// compression simply receives an uncompressed certificate. |
| 108 | +await handshake({ serverAlgs: [supported[0]], clientAlgs: undefined }); |
| 109 | +await handshake({ serverAlgs: undefined, clientAlgs: [supported[0]] }); |
| 110 | + |
| 111 | +// An empty array is equivalent to disabling compression. |
| 112 | +await handshake({ serverAlgs: [], clientAlgs: [] }); |
0 commit comments