@@ -3,7 +3,7 @@ import { once } from 'events';
33import { assert } from 'chai' ;
44import { promisify } from 'util' ;
55import DuplexPair from 'native-duplexpair' ;
6- import { TLSSocket } from 'tls' ;
6+ import { checkServerIdentity , type PeerCertificate , TLSSocket } from 'tls' ;
77import { readFileSync } from 'fs' ;
88import { Duplex } from 'stream' ;
99
@@ -339,7 +339,9 @@ describe('MessageIO', function() {
339339 } ) ;
340340
341341 describe ( '#startTls' , function ( ) {
342- let securePair : { encrypted : Duplex , cleartext : TLSSocket } ;
342+ // `@types/node` does not declare the server-side `servername` getter
343+ // on `TLSSocket`, so widen the type here.
344+ let securePair : { encrypted : Duplex , cleartext : TLSSocket & { servername ?: string } } ;
343345
344346 beforeEach ( function ( ) {
345347 const duplexpair = new DuplexPair ( ) ;
@@ -362,6 +364,30 @@ describe('MessageIO', function() {
362364 securePair . encrypted . destroy ( ) ;
363365 } ) ;
364366
367+ /**
368+ * Forwards TLS handshake data between the given `MessageIO` and the
369+ * server side of the secure pair, unwrapping it from / wrapping it into
370+ * `PRELOGIN` messages.
371+ */
372+ async function forwardTlsHandshake ( io : MessageIO , rounds : number ) {
373+ for ( let i = 0 ; i < rounds ; i ++ ) {
374+ const message = await io . readMessage ( ) ;
375+ for await ( const chunk of message ) {
376+ securePair . encrypted . write ( chunk ) ;
377+ }
378+
379+ await once ( securePair . encrypted , 'readable' ) ;
380+
381+ const chunks = [ ] ;
382+ let chunk ;
383+ while ( chunk = securePair . encrypted . read ( ) ) {
384+ chunks . push ( chunk ) ;
385+ }
386+
387+ io . sendMessage ( TYPE . PRELOGIN , Buffer . concat ( chunks ) ) ;
388+ }
389+ }
390+
365391 it ( 'performs TLS negotiation' , async function ( ) {
366392 await Promise . all ( [
367393 // Client side
@@ -517,6 +543,119 @@ describe('MessageIO', function() {
517543 ] ) ;
518544 } ) ;
519545
546+ it ( 'sends the hostname via the SNI extension' , async function ( ) {
547+ await Promise . all ( [
548+ // Client side
549+ ( async ( ) => {
550+ const io = new MessageIO ( clientConnection , packetSize , debug ) ;
551+
552+ await io . startTls ( { } , 'localhost' , true ) ;
553+
554+ assert ( io . tlsNegotiationComplete ) ;
555+ } ) ( ) ,
556+
557+ // Server side
558+ ( async ( ) => {
559+ const io = new MessageIO ( serverConnection , packetSize , debug ) ;
560+
561+ const onSecure = once ( securePair . cleartext , 'secure' ) ;
562+
563+ await forwardTlsHandshake ( io , 2 ) ;
564+
565+ await onSecure ;
566+
567+ assert . strictEqual ( securePair . cleartext . servername , 'localhost' ) ;
568+ } ) ( )
569+ ] ) ;
570+ } ) ;
571+
572+ describe ( 'when connecting to an IP address' , function ( ) {
573+ beforeEach ( function ( ) {
574+ // Replace the server side of the secure pair with one that uses a
575+ // certificate that carries IP address SANs (and no DNS SANs).
576+ securePair . cleartext . destroy ( ) ;
577+ securePair . encrypted . destroy ( ) ;
578+
579+ const duplexpair = new DuplexPair ( ) ;
580+
581+ securePair = {
582+ cleartext : new TLSSocket ( duplexpair . socket1 as Socket , {
583+ key : readFileSync ( './test/fixtures/loopback-ip.key' ) ,
584+ cert : readFileSync ( './test/fixtures/loopback-ip.crt' ) ,
585+ isServer : true ,
586+ ciphers : 'ECDHE-RSA-AES128-GCM-SHA256' ,
587+ // TDS 7.x only supports TLS versions up to TLS v1.2
588+ maxVersion : 'TLSv1.2'
589+ } ) ,
590+ encrypted : duplexpair . socket2
591+ } ;
592+ } ) ;
593+
594+ it ( 'omits the SNI extension and validates the certificate against the IPv4 address' , async function ( ) {
595+ await Promise . all ( [
596+ // Client side
597+ ( async ( ) => {
598+ const io = new MessageIO ( clientConnection , packetSize , debug ) ;
599+
600+ await io . startTls ( {
601+ ca : [ readFileSync ( './test/fixtures/loopback-ip.crt' ) ]
602+ } , '127.0.0.1' , false ) ;
603+
604+ assert ( io . tlsNegotiationComplete ) ;
605+ } ) ( ) ,
606+
607+ // Server side
608+ ( async ( ) => {
609+ const io = new MessageIO ( serverConnection , packetSize , debug ) ;
610+
611+ const onSecure = once ( securePair . cleartext , 'secure' ) ;
612+
613+ await forwardTlsHandshake ( io , 2 ) ;
614+
615+ await onSecure ;
616+
617+ assert . notOk ( securePair . cleartext . servername ) ;
618+ } ) ( )
619+ ] ) ;
620+ } ) ;
621+
622+ it ( 'omits the SNI extension and validates the certificate against the IPv6 address' , async function ( ) {
623+ // Some Node.js versions are affected by an upstream regression that
624+ // prevents IPv6 addresses from being matched against `IP Address`
625+ // SANs. See https://github.com/nodejs/node/issues/64144
626+ const cert = { subject : { CN : 'dummy' } , subjectaltname : 'IP Address:0:0:0:0:0:0:0:1' } ;
627+ if ( checkServerIdentity ( '::1' , cert as PeerCertificate ) !== undefined ) {
628+ this . skip ( ) ;
629+ }
630+
631+ await Promise . all ( [
632+ // Client side
633+ ( async ( ) => {
634+ const io = new MessageIO ( clientConnection , packetSize , debug ) ;
635+
636+ await io . startTls ( {
637+ ca : [ readFileSync ( './test/fixtures/loopback-ip.crt' ) ]
638+ } , '::1' , false ) ;
639+
640+ assert ( io . tlsNegotiationComplete ) ;
641+ } ) ( ) ,
642+
643+ // Server side
644+ ( async ( ) => {
645+ const io = new MessageIO ( serverConnection , packetSize , debug ) ;
646+
647+ const onSecure = once ( securePair . cleartext , 'secure' ) ;
648+
649+ await forwardTlsHandshake ( io , 2 ) ;
650+
651+ await onSecure ;
652+
653+ assert . notOk ( securePair . cleartext . servername ) ;
654+ } ) ( )
655+ ] ) ;
656+ } ) ;
657+ } ) ;
658+
520659 it ( 'handles errors happening before TLS negotiation has sent any data' , async function ( ) {
521660 await Promise . all ( [
522661 // Client side
0 commit comments