Skip to content

Commit

Permalink
crypto/tls: set Conn.ConnectionState.ServerName unconditionally
Browse files Browse the repository at this point in the history
Moves the state.ServerName assignment to outside the if
statement that checks for handshakeComplete.

Fixes golang#15571

Change-Id: I6c4131ddb16389aed1c410a975f9aa3b52816965
Reviewed-on: https://go-review.googlesource.com/22862
Run-TryBot: Adam Langley <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Adam Langley <[email protected]>
  • Loading branch information
amalaviy authored and agl committed Aug 17, 2016
1 parent 8e34bdc commit 4b78482
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,8 @@ func (c *Conn) ConnectionState() ConnectionState {

var state ConnectionState
state.HandshakeComplete = c.handshakeComplete
state.ServerName = c.serverName

if c.handshakeComplete {
state.Version = c.vers
state.NegotiatedProtocol = c.clientProtocol
Expand All @@ -1254,7 +1256,6 @@ func (c *Conn) ConnectionState() ConnectionState {
state.CipherSuite = c.cipherSuite
state.PeerCertificates = c.peerCertificates
state.VerifiedChains = c.verifiedChains
state.ServerName = c.serverName
state.SignedCertificateTimestamps = c.scts
state.OCSPResponse = c.ocspResponse
if !c.didResume {
Expand Down
41 changes: 41 additions & 0 deletions handshake_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,47 @@ func TestClientAuth(t *testing.T) {
runServerTestTLS12(t, test)
}

func TestSNIGivenOnFailure(t *testing.T) {
const expectedServerName = "test.testing"

clientHello := &clientHelloMsg{
vers: VersionTLS10,
cipherSuites: []uint16{TLS_RSA_WITH_RC4_128_SHA},
compressionMethods: []uint8{compressionNone},
serverName: expectedServerName,
}

serverConfig := testConfig.clone()
// Erase the server's cipher suites to ensure the handshake fails.
serverConfig.CipherSuites = nil

c, s := net.Pipe()
go func() {
cli := Client(c, testConfig)
cli.vers = clientHello.vers
cli.writeRecord(recordTypeHandshake, clientHello.marshal())
c.Close()
}()
hs := serverHandshakeState{
c: Server(s, serverConfig),
}
_, err := hs.readClientHello()
defer s.Close()

if err == nil {
t.Error("No error reported from server")
}

cs := hs.c.ConnectionState()
if cs.HandshakeComplete {
t.Error("Handshake registered as complete")
}

if cs.ServerName != expectedServerName {
t.Errorf("Expected ServerName of %q, but got %q", expectedServerName, cs.ServerName)
}
}

func bigFromString(s string) *big.Int {
ret := new(big.Int)
ret.SetString(s, 10)
Expand Down

0 comments on commit 4b78482

Please sign in to comment.