@@ -58,7 +58,7 @@ type clientStateStart struct {
5858 cookie []byte
5959 firstClientHello * HandshakeMessage
6060 helloRetryRequest * HandshakeMessage
61- hsCtx HandshakeContext
61+ hsCtx * HandshakeContext
6262}
6363
6464var _ HandshakeState = & clientStateStart {}
@@ -172,8 +172,10 @@ func (state clientStateStart) Next(hr handshakeMessageReader) (HandshakeState, [
172172 }
173173 ch .CipherSuites = compatibleSuites
174174
175+ // TODO([email protected] ): Check that the ticket can be used for early 176+ // data.
175177 // Signal early data if we're going to do it
176- if len ( state .Opts . EarlyData ) > 0 {
178+ if state .Config . AllowEarlyData && state . helloRetryRequest == nil {
177179 state .Params .ClientSendingEarlyData = true
178180 ed = & EarlyDataExtension {}
179181 err = ch .Extensions .Add (ed )
@@ -255,9 +257,6 @@ func (state clientStateStart) Next(hr handshakeMessageReader) (HandshakeState, [
255257 earlyTrafficSecret := deriveSecret (params , earlySecret , labelEarlyTrafficSecret , chHash )
256258 logf (logTypeCrypto , "early traffic secret: [%d] %x" , len (earlyTrafficSecret ), earlyTrafficSecret )
257259 clientEarlyTrafficKeys = makeTrafficKeys (params , earlyTrafficSecret )
258- } else if len (state .Opts .EarlyData ) > 0 {
259- logf (logTypeHandshake , "[ClientStateWaitSH] Early data without PSK" )
260- return nil , nil , AlertInternalError
261260 } else {
262261 clientHello , err = state .hsCtx .hOut .HandshakeMessageFromBody (ch )
263262 if err != nil {
@@ -291,7 +290,6 @@ func (state clientStateStart) Next(hr handshakeMessageReader) (HandshakeState, [
291290 if state .Params .ClientSendingEarlyData {
292291 toSend = append (toSend , []HandshakeAction {
293292 RekeyOut {epoch : EpochEarlyData , KeySet : clientEarlyTrafficKeys },
294- SendEarlyData {},
295293 }... )
296294 }
297295
@@ -302,7 +300,7 @@ type clientStateWaitSH struct {
302300 Config * Config
303301 Opts ConnectionOptions
304302 Params ConnectionParameters
305- hsCtx HandshakeContext
303+ hsCtx * HandshakeContext
306304 OfferedDH map [NamedGroup ][]byte
307305 OfferedPSK PreSharedKey
308306 PSK []byte
@@ -412,6 +410,11 @@ func (state clientStateWaitSH) Next(hr handshakeMessageReader) (HandshakeState,
412410 body : h .Sum (nil ),
413411 }
414412
413+ state .hsCtx .receivedEndOfFlight ()
414+
415+ // TODO([email protected] ): Need to rekey with cleartext if we are on 0-RTT 416+ // mode. In DTLS, we also need to bump the sequence number.
417+ // This is a pre-existing defect in Mint. Issue #175.
415418 logf (logTypeHandshake , "[ClientStateWaitSH] -> [ClientStateStart]" )
416419 return clientStateStart {
417420 Config : state .Config ,
@@ -420,7 +423,7 @@ func (state clientStateWaitSH) Next(hr handshakeMessageReader) (HandshakeState,
420423 cookie : serverCookie .Cookie ,
421424 firstClientHello : firstClientHello ,
422425 helloRetryRequest : hm ,
423- }, nil , AlertNoAlert
426+ }, [] HandshakeAction { ResetOut { 1 }} , AlertNoAlert
424427 }
425428
426429 // This is SH.
@@ -515,7 +518,6 @@ func (state clientStateWaitSH) Next(hr handshakeMessageReader) (HandshakeState,
515518 logf (logTypeCrypto , "master secret: [%d] %x" , len (masterSecret ), masterSecret )
516519
517520 serverHandshakeKeys := makeTrafficKeys (params , serverHandshakeTrafficSecret )
518-
519521 logf (logTypeHandshake , "[ClientStateWaitSH] -> [ClientStateWaitEE]" )
520522 nextState := clientStateWaitEE {
521523 Config : state .Config ,
@@ -530,13 +532,20 @@ func (state clientStateWaitSH) Next(hr handshakeMessageReader) (HandshakeState,
530532 toSend := []HandshakeAction {
531533 RekeyIn {epoch : EpochHandshakeData , KeySet : serverHandshakeKeys },
532534 }
535+ // We're definitely not going to have to send anything with
536+ // early data.
537+ if ! state .Params .ClientSendingEarlyData {
538+ toSend = append (toSend , RekeyOut {epoch : EpochHandshakeData ,
539+ KeySet : makeTrafficKeys (params , clientHandshakeTrafficSecret )})
540+ }
541+
533542 return nextState , toSend , AlertNoAlert
534543}
535544
536545type clientStateWaitEE struct {
537546 Config * Config
538547 Params ConnectionParameters
539- hsCtx HandshakeContext
548+ hsCtx * HandshakeContext
540549 cryptoParams CipherSuiteParams
541550 handshakeHash hash.Hash
542551 masterSecret []byte
@@ -596,6 +605,14 @@ func (state clientStateWaitEE) Next(hr handshakeMessageReader) (HandshakeState,
596605
597606 state .handshakeHash .Write (hm .Marshal ())
598607
608+ toSend := []HandshakeAction {}
609+
610+ if state .Params .ClientSendingEarlyData && ! state .Params .UsingEarlyData {
611+ // We didn't get 0-RTT, so rekey to handshake.
612+ toSend = append (toSend , RekeyOut {epoch : EpochHandshakeData ,
613+ KeySet : makeTrafficKeys (state .cryptoParams , state .clientHandshakeTrafficSecret )})
614+ }
615+
599616 if state .Params .UsingPSK {
600617 logf (logTypeHandshake , "[ClientStateWaitEE] -> [ClientStateWaitFinished]" )
601618 nextState := clientStateWaitFinished {
@@ -608,7 +625,7 @@ func (state clientStateWaitEE) Next(hr handshakeMessageReader) (HandshakeState,
608625 clientHandshakeTrafficSecret : state .clientHandshakeTrafficSecret ,
609626 serverHandshakeTrafficSecret : state .serverHandshakeTrafficSecret ,
610627 }
611- return nextState , nil , AlertNoAlert
628+ return nextState , toSend , AlertNoAlert
612629 }
613630
614631 logf (logTypeHandshake , "[ClientStateWaitEE] -> [ClientStateWaitCertCR]" )
@@ -622,13 +639,13 @@ func (state clientStateWaitEE) Next(hr handshakeMessageReader) (HandshakeState,
622639 clientHandshakeTrafficSecret : state .clientHandshakeTrafficSecret ,
623640 serverHandshakeTrafficSecret : state .serverHandshakeTrafficSecret ,
624641 }
625- return nextState , nil , AlertNoAlert
642+ return nextState , toSend , AlertNoAlert
626643}
627644
628645type clientStateWaitCertCR struct {
629646 Config * Config
630647 Params ConnectionParameters
631- hsCtx HandshakeContext
648+ hsCtx * HandshakeContext
632649 cryptoParams CipherSuiteParams
633650 handshakeHash hash.Hash
634651 masterSecret []byte
@@ -706,7 +723,7 @@ func (state clientStateWaitCertCR) Next(hr handshakeMessageReader) (HandshakeSta
706723type clientStateWaitCert struct {
707724 Config * Config
708725 Params ConnectionParameters
709- hsCtx HandshakeContext
726+ hsCtx * HandshakeContext
710727 cryptoParams CipherSuiteParams
711728 handshakeHash hash.Hash
712729
@@ -760,7 +777,7 @@ func (state clientStateWaitCert) Next(hr handshakeMessageReader) (HandshakeState
760777type clientStateWaitCV struct {
761778 Config * Config
762779 Params ConnectionParameters
763- hsCtx HandshakeContext
780+ hsCtx * HandshakeContext
764781 cryptoParams CipherSuiteParams
765782 handshakeHash hash.Hash
766783
@@ -861,7 +878,7 @@ func (state clientStateWaitCV) Next(hr handshakeMessageReader) (HandshakeState,
861878
862879type clientStateWaitFinished struct {
863880 Params ConnectionParameters
864- hsCtx HandshakeContext
881+ hsCtx * HandshakeContext
865882 cryptoParams CipherSuiteParams
866883 handshakeHash hash.Hash
867884
@@ -933,6 +950,7 @@ func (state clientStateWaitFinished) Next(hr handshakeMessageReader) (HandshakeS
933950 toSend := []HandshakeAction {}
934951
935952 if state .Params .UsingEarlyData {
953+ logf (logTypeHandshake , "Sending end of early data" )
936954 // Note: We only send EOED if the server is actually going to use the early
937955 // data. Otherwise, it will never see it, and the transcripts will
938956 // mismatch.
@@ -942,10 +960,11 @@ func (state clientStateWaitFinished) Next(hr handshakeMessageReader) (HandshakeS
942960
943961 state .handshakeHash .Write (eoedm .Marshal ())
944962 logf (logTypeCrypto , "input to handshake hash [%d]: %x" , len (eoedm .Marshal ()), eoedm .Marshal ())
945- }
946963
947- clientHandshakeKeys := makeTrafficKeys (state .cryptoParams , state .clientHandshakeTrafficSecret )
948- toSend = append (toSend , RekeyOut {epoch : EpochHandshakeData , KeySet : clientHandshakeKeys })
964+ // And then rekey to handshake
965+ toSend = append (toSend , RekeyOut {epoch : EpochHandshakeData ,
966+ KeySet : makeTrafficKeys (state .cryptoParams , state .clientHandshakeTrafficSecret )})
967+ }
949968
950969 if state .Params .UsingClientAuth {
951970 // Extract constraints from certicateRequest
@@ -1045,6 +1064,8 @@ func (state clientStateWaitFinished) Next(hr handshakeMessageReader) (HandshakeS
10451064 RekeyOut {epoch : EpochApplicationData , KeySet : clientTrafficKeys },
10461065 }... )
10471066
1067+ state .hsCtx .receivedEndOfFlight ()
1068+
10481069 logf (logTypeHandshake , "[ClientStateWaitFinished] -> [StateConnected]" )
10491070 nextState := stateConnected {
10501071 Params : state .Params ,
0 commit comments