mirror of
https://github.com/SagerNet/sing-shadowtls.git
synced 2025-04-01 19:27:36 +03:00
Add TLS session id generator
This commit is contained in:
parent
3ba2f038e1
commit
320d58c57a
4 changed files with 42 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue