@@ -28,7 +28,6 @@ if (!common.opensslCli)
28
28
common . skip ( 'node compiled without OpenSSL CLI.' ) ;
29
29
30
30
const assert = require ( 'assert' ) ;
31
- const spawn = require ( 'child_process' ) . spawn ;
32
31
const tls = require ( 'tls' ) ;
33
32
const fixtures = require ( '../common/fixtures' ) ;
34
33
@@ -51,63 +50,49 @@ function test(next) {
51
50
key : fixtures . readSync ( 'test_key.pem' )
52
51
} ;
53
52
54
- let seenError = false ;
55
-
56
53
const server = tls . createServer ( options , function ( conn ) {
57
54
conn . on ( 'error' , function ( err ) {
58
55
console . error ( `Caught exception: ${ err } ` ) ;
59
56
assert ( / T L S s e s s i o n r e n e g o t i a t i o n a t t a c k / . test ( err ) ) ;
60
57
conn . destroy ( ) ;
61
- seenError = true ;
62
58
} ) ;
63
59
conn . pipe ( conn ) ;
64
60
} ) ;
65
61
66
- server . listen ( common . PORT , function ( ) {
67
- const args = ( `s_client -connect 127.0.0.1:${ common . PORT } ` ) . split ( ' ' ) ;
68
- const child = spawn ( common . opensslCli , args ) ;
69
-
70
- child . stdout . resume ( ) ;
71
- child . stderr . resume ( ) ;
62
+ server . listen ( 0 , function ( ) {
63
+ const options = {
64
+ host : server . address ( ) . host ,
65
+ port : server . address ( ) . port ,
66
+ rejectUnauthorized : false
67
+ } ;
68
+ const client = tls . connect ( options , spam ) ;
72
69
73
- // Count handshakes, start the attack after the initial handshake is done
74
- let handshakes = 0 ;
75
70
let renegs = 0 ;
76
71
77
- child . stderr . on ( 'data' , function ( data ) {
78
- if ( seenError ) return ;
79
- handshakes += ( ( String ( data ) ) . match ( / v e r i f y r e t u r n : 1 / g) || [ ] ) . length ;
80
- if ( handshakes === 2 ) spam ( ) ;
81
- renegs += ( ( String ( data ) ) . match ( / R E N E G O T I A T I N G / g) || [ ] ) . length ;
82
- } ) ;
83
-
84
- child . on ( 'exit' , function ( ) {
72
+ client . on ( 'close' , function ( ) {
85
73
assert . strictEqual ( renegs , tls . CLIENT_RENEG_LIMIT + 1 ) ;
86
74
server . close ( ) ;
87
75
process . nextTick ( next ) ;
88
76
} ) ;
89
77
90
- let closed = false ;
91
- child . stdin . on ( 'error' , function ( err ) {
92
- switch ( err . code ) {
93
- case 'ECONNRESET' :
94
- case 'EPIPE' :
95
- break ;
96
- default :
97
- assert . strictEqual ( err . code , 'ECONNRESET' ) ;
98
- break ;
99
- }
100
- closed = true ;
78
+ client . on ( 'error' , function ( err ) {
79
+ console . log ( 'CLIENT ERR' , err ) ;
80
+ throw err ;
101
81
} ) ;
102
- child . stdin . on ( 'close' , function ( ) {
103
- closed = true ;
82
+
83
+ client . on ( 'close' , function ( hadErr ) {
84
+ assert . strictEqual ( hadErr , false ) ;
104
85
} ) ;
105
86
106
87
// simulate renegotiation attack
107
88
function spam ( ) {
108
- if ( closed ) return ;
109
- child . stdin . write ( 'R\n' ) ;
110
- setTimeout ( spam , 50 ) ;
89
+ client . write ( '' ) ;
90
+ client . renegotiate ( { } , ( err ) => {
91
+ assert . ifError ( err ) ;
92
+ assert . ok ( renegs <= tls . CLIENT_RENEG_LIMIT ) ;
93
+ spam ( ) ;
94
+ } ) ;
95
+ renegs ++ ;
111
96
}
112
97
} ) ;
113
98
}
0 commit comments