|
| 1 | +import * as net from 'net'; |
| 2 | +import * as tls from 'tls'; |
| 3 | + |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { DestroyableServer, makeDestroyable } from 'destroyable-server'; |
| 6 | + |
| 7 | +import { createTestServer } from './test-helpers.js'; |
| 8 | + |
| 9 | +describe("TLS cipher endpoints", () => { |
| 10 | + |
| 11 | + let server: DestroyableServer; |
| 12 | + let port: number; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + server = makeDestroyable(await createTestServer()); |
| 16 | + await new Promise<void>((resolve) => server.listen(resolve)); |
| 17 | + port = (server.address() as net.AddressInfo).port; |
| 18 | + }); |
| 19 | + afterEach(async () => { await server.destroy(); }); |
| 20 | + |
| 21 | + // Offers the given legacy cipher (and, by not capping the version, TLS 1.3 too) so we can |
| 22 | + // check both that the endpoint negotiates the weak cipher AND that it caps at TLS 1.2. |
| 23 | + const negotiate = (servername: string, ciphers: string) => |
| 24 | + new Promise<{ name: string, version: string | null }>((resolve, reject) => { |
| 25 | + const conn = tls.connect({ |
| 26 | + port, host: '127.0.0.1', servername, rejectUnauthorized: false, |
| 27 | + ciphers: `${ciphers}@SECLEVEL=0` |
| 28 | + }); |
| 29 | + conn.on('secureConnect', () => { |
| 30 | + const info = { name: conn.getCipher().name, version: conn.getProtocol() }; |
| 31 | + conn.destroy(); |
| 32 | + resolve(info); |
| 33 | + }); |
| 34 | + conn.on('error', reject); |
| 35 | + }); |
| 36 | + |
| 37 | + it("static-rsa negotiates an RSA-key-exchange suite over TLS 1.2", async () => { |
| 38 | + const { name, version } = await negotiate('static-rsa.localhost', 'AES128-GCM-SHA256:AES128-SHA'); |
| 39 | + expect(version).to.equal('TLSv1.2'); |
| 40 | + expect(name).to.match(/^AES(128|256)-/); // no ECDHE-/DHE- prefix => static RSA |
| 41 | + }); |
| 42 | + |
| 43 | + it("cbc negotiates a CBC-mode suite over TLS 1.2", async () => { |
| 44 | + const { name, version } = await negotiate('cbc.localhost', 'ECDHE-RSA-AES128-SHA'); |
| 45 | + expect(version).to.equal('TLSv1.2'); |
| 46 | + expect(name).to.equal('ECDHE-RSA-AES128-SHA'); // -SHA (not -SHA256/-GCM) => CBC |
| 47 | + }); |
| 48 | + |
| 49 | + it("null-cipher negotiates a NULL cipher over TLS 1.2", async () => { |
| 50 | + const { name, version } = await negotiate('null-cipher.localhost', 'NULL-SHA'); |
| 51 | + expect(version).to.equal('TLSv1.2'); |
| 52 | + expect(name).to.match(/NULL/); |
| 53 | + }); |
| 54 | + |
| 55 | + it("weak-dh negotiates ephemeral DH over TLS 1.2", async () => { |
| 56 | + const { name, version } = await negotiate('weak-dh.localhost', 'DHE-RSA-AES128-SHA'); |
| 57 | + expect(version).to.equal('TLSv1.2'); |
| 58 | + expect(name).to.match(/^DHE-/); |
| 59 | + }); |
| 60 | + |
| 61 | + it("refuses a client that won't offer the weak cipher", async () => { |
| 62 | + // A default modern client offers no NULL cipher, so it can't connect to null-cipher. |
| 63 | + const outcome = await new Promise<'connected' | 'failed'>((resolve) => { |
| 64 | + const conn = tls.connect({ port, host: '127.0.0.1', servername: 'null-cipher.localhost', rejectUnauthorized: false }); |
| 65 | + conn.on('secureConnect', () => { conn.destroy(); resolve('connected'); }); |
| 66 | + conn.on('error', () => resolve('failed')); |
| 67 | + }); |
| 68 | + expect(outcome).to.equal('failed'); |
| 69 | + }); |
| 70 | + |
| 71 | + it("treats two cipher endpoints as a conflict", async () => { |
| 72 | + let rejected = false; |
| 73 | + await negotiate('static-rsa--null-cipher.localhost', 'AES128-SHA').catch(() => { rejected = true; }); |
| 74 | + expect(rejected).to.equal(true); |
| 75 | + }); |
| 76 | +}); |
0 commit comments