Add TLS session id generator

This commit is contained in:
世界 2023-02-20 12:52:24 +08:00
parent 3ba2f038e1
commit 320d58c57a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 42 additions and 14 deletions

View file

@ -733,6 +733,8 @@ type Config struct {
// used for debugging.
KeyLogWriter io.Writer
SessionIDGenerator func(clientHello []byte, sessionID []byte) error
// mutex protects sessionTicketKeys and autoSessionTicketKeys.
mutex sync.RWMutex
// sessionTicketKeys contains zero or more ticket keys. If set, it means

View file

@ -111,13 +111,6 @@ func (c *Conn) makeClientHello() (*clientHelloMsg, *ecdh.PrivateKey, error) {
return nil, nil, errors.New("tls: short read from Rand: " + err.Error())
}
// A random session ID is used to detect when the server accepted a ticket
// and is resuming a session (see RFC 5077). In TLS 1.3, it's always set as
// a compatibility measure (see RFC 8446, Section 4.1.2).
if _, err := io.ReadFull(config.rand(), hello.sessionId); err != nil {
return nil, nil, errors.New("tls: short read from Rand: " + err.Error())
}
if hello.vers >= VersionTLS12 {
hello.supportedSignatureAlgorithms = supportedSignatureAlgorithms()
}
@ -144,6 +137,25 @@ func (c *Conn) makeClientHello() (*clientHelloMsg, *ecdh.PrivateKey, error) {
hello.keyShares = []keyShare{{group: curveID, data: key.PublicKey().Bytes()}}
}
// A random session ID is used to detect when the server accepted a ticket
// and is resuming a session (see RFC 5077). In TLS 1.3, it's always set as
// a compatibility measure (see RFC 8446, Section 4.1.2).
if config.SessionIDGenerator != nil {
buffer, err := hello.marshal()
if err != nil {
return nil, nil, err
}
if err := config.SessionIDGenerator(buffer, hello.sessionId); err != nil {
return nil, nil, errors.New("tls: generate session id failed: " + err.Error())
}
hello.raw = nil
} else {
if _, err := io.ReadFull(config.rand(), hello.sessionId); err != nil {
return nil, nil, errors.New("tls: short read from Rand: " + err.Error())
}
}
return hello, key, nil
}

View file

@ -723,6 +723,8 @@ type Config struct {
// used for debugging.
KeyLogWriter io.Writer
SessionIDGenerator func(clientHello []byte, sessionID []byte) error
// mutex protects sessionTicketKeys and autoSessionTicketKeys.
mutex sync.RWMutex
// sessionTicketKeys contains zero or more ticket keys. If set, it means the

View file

@ -111,13 +111,6 @@ func (c *Conn) makeClientHello() (*clientHelloMsg, ecdheParameters, error) {
return nil, nil, errors.New("tls: short read from Rand: " + err.Error())
}
// A random session ID is used to detect when the server accepted a ticket
// and is resuming a session (see RFC 5077). In TLS 1.3, it's always set as
// a compatibility measure (see RFC 8446, Section 4.1.2).
if _, err := io.ReadFull(config.rand(), hello.sessionId); err != nil {
return nil, nil, errors.New("tls: short read from Rand: " + err.Error())
}
if hello.vers >= VersionTLS12 {
hello.supportedSignatureAlgorithms = supportedSignatureAlgorithms()
}
@ -144,6 +137,25 @@ func (c *Conn) makeClientHello() (*clientHelloMsg, ecdheParameters, error) {
hello.keyShares = []keyShare{{group: curveID, data: params.PublicKey()}}
}
// A random session ID is used to detect when the server accepted a ticket
// and is resuming a session (see RFC 5077). In TLS 1.3, it's always set as
// a compatibility measure (see RFC 8446, Section 4.1.2).
if config.SessionIDGenerator != nil {
buffer, err := hello.marshal()
if err != nil {
return nil, nil, err
}
if err := config.SessionIDGenerator(buffer, hello.sessionId); err != nil {
return nil, nil, errors.New("tls: generate session id failed: " + err.Error())
}
hello.raw = nil
} else {
if _, err := io.ReadFull(config.rand(), hello.sessionId); err != nil {
return nil, nil, errors.New("tls: short read from Rand: " + err.Error())
}
}
return hello, params, nil
}