Skip to content

Commit 46f4bfb

Browse files
aglbradfitz
authored andcommitted
crypto/tls: pass argument to serverInit rather than using a field in Config.
Updates #20164. Change-Id: Ib900095e7885f25cd779750674a712c770603ca8 Reviewed-on: https://go-review.googlesource.com/42137 Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b53acd8 commit 46f4bfb

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

src/crypto/tls/common.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,13 @@ type Config struct {
509509

510510
serverInitOnce sync.Once // guards calling (*Config).serverInit
511511

512-
// mutex protects sessionTicketKeys and originalConfig.
512+
// mutex protects sessionTicketKeys.
513513
mutex sync.RWMutex
514514
// sessionTicketKeys contains zero or more ticket keys. If the length
515515
// is zero, SessionTicketsDisabled must be true. The first key is used
516516
// for new tickets and any subsequent keys can be used to decrypt old
517517
// tickets.
518518
sessionTicketKeys []ticketKey
519-
// originalConfig is set to the Config that was passed to Server if
520-
// this Config is returned by a GetConfigForClient callback. It's used
521-
// by serverInit in order to copy session ticket keys if needed.
522-
originalConfig *Config
523519
}
524520

525521
// ticketKeyNameLen is the number of bytes of identifier that is prepended to
@@ -551,7 +547,7 @@ func ticketKeyFromBytes(b [32]byte) (key ticketKey) {
551547
func (c *Config) Clone() *Config {
552548
// Running serverInit ensures that it's safe to read
553549
// SessionTicketsDisabled.
554-
c.serverInitOnce.Do(c.serverInit)
550+
c.serverInitOnce.Do(func() { c.serverInit(nil) })
555551

556552
var sessionTicketKeys []ticketKey
557553
c.mutex.RLock()
@@ -585,20 +581,17 @@ func (c *Config) Clone() *Config {
585581
Renegotiation: c.Renegotiation,
586582
KeyLogWriter: c.KeyLogWriter,
587583
sessionTicketKeys: sessionTicketKeys,
588-
// originalConfig is deliberately not duplicated.
589584
}
590585
}
591586

592-
func (c *Config) serverInit() {
587+
// serverInit is run under c.serverInitOnce to do initialization of c. If c was
588+
// returned by a GetConfigForClient callback then the argument should be the
589+
// Config that was passed to Server, otherwise it should be nil.
590+
func (c *Config) serverInit(originalConfig *Config) {
593591
if c.SessionTicketsDisabled || len(c.ticketKeys()) != 0 {
594592
return
595593
}
596594

597-
var originalConfig *Config
598-
c.mutex.Lock()
599-
originalConfig, c.originalConfig = c.originalConfig, nil
600-
c.mutex.Unlock()
601-
602595
alreadySet := false
603596
for _, b := range c.SessionTicketKey {
604597
if b != 0 {

src/crypto/tls/handshake_server.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type serverHandshakeState struct {
4040
func (c *Conn) serverHandshake() error {
4141
// If this is the first server handshake, we generate a random key to
4242
// encrypt the tickets with.
43-
c.config.serverInitOnce.Do(c.config.serverInit)
43+
c.config.serverInitOnce.Do(func() { c.config.serverInit(nil) })
4444

4545
hs := serverHandshakeState{
4646
c: c,
@@ -129,11 +129,7 @@ func (hs *serverHandshakeState) readClientHello() (isResume bool, err error) {
129129
c.sendAlert(alertInternalError)
130130
return false, err
131131
} else if newConfig != nil {
132-
newConfig.mutex.Lock()
133-
newConfig.originalConfig = c.config
134-
newConfig.mutex.Unlock()
135-
136-
newConfig.serverInitOnce.Do(newConfig.serverInit)
132+
newConfig.serverInitOnce.Do(func() { newConfig.serverInit(c.config) })
137133
c.config = newConfig
138134
}
139135
}

0 commit comments

Comments
 (0)