@@ -146,6 +146,7 @@ func (c *Conn) clientHandshake() (err error) {
146
146
if err != nil {
147
147
return err
148
148
}
149
+ c .serverName = hello .serverName
149
150
150
151
cacheKey , session , earlySecret , binderKey := c .loadSession (hello )
151
152
if cacheKey != "" && session != nil {
@@ -388,6 +389,7 @@ func (hs *clientHandshakeState) handshake() error {
388
389
hs .finishedHash .Write (hs .serverHello .marshal ())
389
390
390
391
c .buffering = true
392
+ c .didResume = isResume
391
393
if isResume {
392
394
if err := hs .establishKeys (); err != nil {
393
395
return err
@@ -399,6 +401,15 @@ func (hs *clientHandshakeState) handshake() error {
399
401
return err
400
402
}
401
403
c .clientFinishedIsFirst = false
404
+ // Make sure the connection is still being verified whether or not this
405
+ // is a resumption. Resumptions currently don't reverify certificates so
406
+ // they don't call verifyServerCertificate. See Issue 31641.
407
+ if c .config .VerifyConnection != nil {
408
+ if err := c .config .VerifyConnection (c .connectionStateLocked ()); err != nil {
409
+ c .sendAlert (alertBadCertificate )
410
+ return err
411
+ }
412
+ }
402
413
if err := hs .sendFinished (c .clientFinished [:]); err != nil {
403
414
return err
404
415
}
@@ -428,7 +439,6 @@ func (hs *clientHandshakeState) handshake() error {
428
439
}
429
440
430
441
c .ekm = ekmFromMasterSecret (c .vers , hs .suite , hs .masterSecret , hs .hello .random , hs .serverHello .random )
431
- c .didResume = isResume
432
442
atomic .StoreUint32 (& c .handshakeStatus , 1 )
433
443
434
444
return nil
@@ -458,25 +468,6 @@ func (hs *clientHandshakeState) doFullHandshake() error {
458
468
}
459
469
hs .finishedHash .Write (certMsg .marshal ())
460
470
461
- if c .handshakes == 0 {
462
- // If this is the first handshake on a connection, process and
463
- // (optionally) verify the server's certificates.
464
- if err := c .verifyServerCertificate (certMsg .certificates ); err != nil {
465
- return err
466
- }
467
- } else {
468
- // This is a renegotiation handshake. We require that the
469
- // server's identity (i.e. leaf certificate) is unchanged and
470
- // thus any previous trust decision is still valid.
471
- //
472
- // See https://mitls.org/pages/attacks/3SHAKE for the
473
- // motivation behind this requirement.
474
- if ! bytes .Equal (c .peerCertificates [0 ].Raw , certMsg .certificates [0 ]) {
475
- c .sendAlert (alertBadCertificate )
476
- return errors .New ("tls: server's identity changed during renegotiation" )
477
- }
478
- }
479
-
480
471
msg , err = c .readHandshake ()
481
472
if err != nil {
482
473
return err
@@ -505,6 +496,25 @@ func (hs *clientHandshakeState) doFullHandshake() error {
505
496
}
506
497
}
507
498
499
+ if c .handshakes == 0 {
500
+ // If this is the first handshake on a connection, process and
501
+ // (optionally) verify the server's certificates.
502
+ if err := c .verifyServerCertificate (certMsg .certificates ); err != nil {
503
+ return err
504
+ }
505
+ } else {
506
+ // This is a renegotiation handshake. We require that the
507
+ // server's identity (i.e. leaf certificate) is unchanged and
508
+ // thus any previous trust decision is still valid.
509
+ //
510
+ // See https://mitls.org/pages/attacks/3SHAKE for the
511
+ // motivation behind this requirement.
512
+ if ! bytes .Equal (c .peerCertificates [0 ].Raw , certMsg .certificates [0 ]) {
513
+ c .sendAlert (alertBadCertificate )
514
+ return errors .New ("tls: server's identity changed during renegotiation" )
515
+ }
516
+ }
517
+
508
518
keyAgreement := hs .suite .ka (c .vers )
509
519
510
520
skx , ok := msg .(* serverKeyExchangeMsg )
@@ -831,13 +841,6 @@ func (c *Conn) verifyServerCertificate(certificates [][]byte) error {
831
841
}
832
842
}
833
843
834
- if c .config .VerifyPeerCertificate != nil {
835
- if err := c .config .VerifyPeerCertificate (certificates , c .verifiedChains ); err != nil {
836
- c .sendAlert (alertBadCertificate )
837
- return err
838
- }
839
- }
840
-
841
844
switch certs [0 ].PublicKey .(type ) {
842
845
case * rsa.PublicKey , * ecdsa.PublicKey , ed25519.PublicKey :
843
846
break
@@ -848,6 +851,20 @@ func (c *Conn) verifyServerCertificate(certificates [][]byte) error {
848
851
849
852
c .peerCertificates = certs
850
853
854
+ if c .config .VerifyPeerCertificate != nil {
855
+ if err := c .config .VerifyPeerCertificate (certificates , c .verifiedChains ); err != nil {
856
+ c .sendAlert (alertBadCertificate )
857
+ return err
858
+ }
859
+ }
860
+
861
+ if c .config .VerifyConnection != nil {
862
+ if err := c .config .VerifyConnection (c .connectionStateLocked ()); err != nil {
863
+ c .sendAlert (alertBadCertificate )
864
+ return err
865
+ }
866
+ }
867
+
851
868
return nil
852
869
}
853
870
0 commit comments