Skip to content

Commit

Permalink
crypto/tls: implement draft-ietf-tls-esni-13
Browse files Browse the repository at this point in the history
Adds support for draft 13 of the Encrypted ClientHello (ECH) extension
for TLS. This requires CIRCL to implement draft 08 or later of the HPKE
specification (draft-irtf-cfrg-hpke-08).

Adds a CFEvent for reporting when ECH is offered or greased by the
client, when ECH is accepted or rejected by the server, and when the
outer SNI doesn't match the public name of the ECH config.

Missing ECH features:
* Record-level padding.
* Proper validation of the public name by the client.
* Retry after rejection.
* PSKs are disabled when ECH is accepted.
  • Loading branch information
cjpatton authored and Lekensteyn committed Jan 20, 2022
1 parent f392a07 commit f6b38b2
Show file tree
Hide file tree
Showing 17 changed files with 3,197 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/crypto/tls/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
alertUnknownPSKIdentity alert = 115
alertCertificateRequired alert = 116
alertNoApplicationProtocol alert = 120
alertECHRequired alert = 121
)

var alertText = map[alert]string{
Expand Down Expand Up @@ -84,6 +85,7 @@ var alertText = map[alert]string{
alertUnknownPSKIdentity: "unknown PSK identity",
alertCertificateRequired: "certificate required",
alertNoApplicationProtocol: "no application protocol",
alertECHRequired: "ECH required",
}

func (e alert) String() string {
Expand Down
89 changes: 88 additions & 1 deletion src/crypto/tls/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const (
extensionSignatureAlgorithmsCert uint16 = 50
extensionKeyShare uint16 = 51
extensionRenegotiationInfo uint16 = 0xff01
extensionECH uint16 = 0xfe0d // draft-ietf-tls-esni-13
extensionECHOuterExtensions uint16 = 0xfd00 // draft-ietf-tls-esni-13
)

// TLS signaling cipher suite values
Expand Down Expand Up @@ -223,6 +225,45 @@ const (
// include downgrade canaries even if it's using its highers supported version.
var testingOnlyForceDowngradeCanary bool

// testingTriggerHRR causes the server to intentionally trigger a
// HelloRetryRequest (HRR). This is useful for testing new TLS features that
// change the HRR codepath.
var testingTriggerHRR bool

// testingECHTriggerBypassAfterHRR causes the client to bypass ECH after HRR.
// If available, the client will offer ECH in the first CH only.
var testingECHTriggerBypassAfterHRR bool

// testingECHTriggerBypassBeforeHRR causes the client to bypass ECH before HRR.
// The client will offer ECH in the second CH only.
var testingECHTriggerBypassBeforeHRR bool

// testingECHIllegalHandleAfterHRR causes the client to illegally change the ECH
// extension after HRR.
var testingECHIllegalHandleAfterHRR bool

// testingECHTriggerPayloadDecryptError causes the client to to send an
// inauthentic payload.
var testingECHTriggerPayloadDecryptError bool

// testingECHOuterExtMany causes a client to incorporate a sequence of
// outer extensions into the ClientHelloInner when it offers the ECH extension.
// The "key_share" extension is the only incorporated extension by default.
var testingECHOuterExtMany bool

// testingECHOuterExtNone causes a client to not use the "outer_extension"
// mechanism for ECH. The "key_shares" extension is incorporated by default.
var testingECHOuterExtNone bool

// testingECHOuterExtIncorrectOrder causes the client to send the
// "outer_extension" extension in the wrong order when offering the ECH
// extension.
var testingECHOuterExtIncorrectOrder bool

// testingECHOuterExtIllegal causes the client to send in its
// "outer_extension" extension the codepoint for the ECH extension.
var testingECHOuterExtIllegal bool

// ConnectionState records basic TLS details about the connection.
type ConnectionState struct {
// Version is the TLS version used by the connection (e.g. VersionTLS12).
Expand Down Expand Up @@ -291,6 +332,14 @@ type ConnectionState struct {
// RFC 7627, and https://mitls.org/pages/attacks/3SHAKE#channelbindings.
TLSUnique []byte

// ECHAccepted is set if the ECH extension was offered by the client and
// accepted by the server.
ECHAccepted bool

// ECHOffered is set if the ECH extension is present in the ClientHello.
// This means the client has offered ECH or sent GREASE ECH.
ECHOffered bool

// CFControl is used to pass additional TLS configuration information to
// HTTP requests.
//
Expand Down Expand Up @@ -710,7 +759,8 @@ type Config struct {

// SessionTicketsDisabled may be set to true to disable session ticket and
// PSK (resumption) support. Note that on clients, session ticket support is
// also disabled if ClientSessionCache is nil.
// also disabled if ClientSessionCache is nil. On clients or servers,
// support is disabled if the ECH extension is enabled.
SessionTicketsDisabled bool

// SessionTicketKey is used by TLS servers to provide session resumption.
Expand Down Expand Up @@ -765,6 +815,23 @@ type Config struct {
// used for debugging.
KeyLogWriter io.Writer

// ECHEnabled determines whether the ECH extension is enabled for this
// connection.
ECHEnabled bool

// ClientECHConfigs are the parameters used by the client when it offers the
// ECH extension. If ECH is enabled, a suitable configuration is found, and
// the client supports TLS 1.3, then it will offer ECH in this handshake.
// Otherwise, if ECH is enabled, it will send a dummy ECH extension.
ClientECHConfigs []ECHConfig

// ServerECHProvider is the ECH provider used by the client-facing server
// for the ECH extension. If the client offers ECH and TLS 1.3 is
// negotiated, then the provider is used to compute the HPKE context
// (draft-irtf-cfrg-hpke-07), which in turn is used to decrypt the extension
// payload.
ServerECHProvider ECHProvider

// CFEventHandler, if set, is called by the client and server at various
// points during the handshake to handle specific events. This is used
// primarily for collecting metrics.
Expand Down Expand Up @@ -878,6 +945,9 @@ func (c *Config) Clone() *Config {
Renegotiation: c.Renegotiation,
KeyLogWriter: c.KeyLogWriter,
SupportDelegatedCredential: c.SupportDelegatedCredential,
ECHEnabled: c.ECHEnabled,
ClientECHConfigs: c.ClientECHConfigs,
ServerECHProvider: c.ServerECHProvider,
CFEventHandler: c.CFEventHandler,
CFControl: c.CFControl,
sessionTicketKeys: c.sessionTicketKeys,
Expand Down Expand Up @@ -1055,6 +1125,23 @@ func (c *Config) supportedVersions() []uint16 {
return versions
}

func (c *Config) supportedVersionsFromMin(minVersion uint16) []uint16 {
versions := make([]uint16, 0, len(supportedVersions))
for _, v := range supportedVersions {
if c != nil && c.MinVersion != 0 && v < c.MinVersion {
continue
}
if c != nil && c.MaxVersion != 0 && v > c.MaxVersion {
continue
}
if v < minVersion {
continue
}
versions = append(versions, v)
}
return versions
}

func (c *Config) maxSupportedVersion() uint16 {
supportedVersions := c.supportedVersions()
if len(supportedVersions) == 0 {
Expand Down
46 changes: 46 additions & 0 deletions src/crypto/tls/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tls

import (
"bytes"
"circl/hpke"
"context"
"crypto/cipher"
"crypto/subtle"
Expand Down Expand Up @@ -118,6 +119,20 @@ type Conn struct {
activeCall int32

tmp [16]byte

// State used for the ECH extension.
ech struct {
sealer hpke.Sealer // The client's HPKE context
opener hpke.Opener // The server's HPKE context

// The state shared by the client and server.
offered bool // Client offered ECH
greased bool // Client greased ECH
accepted bool // Server accepted ECH
retryConfigs []byte // The retry configurations
configId uint8 // The ECH config id
maxNameLen int // maximum_name_len indicated by the ECH config
}
}

// Access to net.Conn methods.
Expand Down Expand Up @@ -695,6 +710,12 @@ func (c *Conn) readRecordOrCCS(expectChangeCipherSpec bool) error {
return c.in.setErrorLocked(io.EOF)
}
if c.vers == VersionTLS13 {
if !c.isClient && c.ech.greased && alert(data[1]) == alertECHRequired {
// This condition indicates that the client intended to offer
// ECH, but did not use a known ECH config.
c.ech.offered = true
c.ech.greased = false
}
return c.in.setErrorLocked(&net.OpError{Op: "remote error", Err: alert(data[1])})
}
switch data[0] {
Expand Down Expand Up @@ -1339,6 +1360,29 @@ func (c *Conn) Close() error {
if err := c.conn.Close(); err != nil {
return err
}

// Resolve ECH status.
if !c.isClient && c.config.MaxVersion < VersionTLS13 {
c.handleCFEvent(CFEventECHServerStatus(echStatusBypassed))
} else if !c.ech.offered {
if !c.ech.greased {
c.handleCFEvent(CFEventECHClientStatus(echStatusBypassed))
} else {
c.handleCFEvent(CFEventECHClientStatus(echStatusOuter))
}
} else {
c.handleCFEvent(CFEventECHClientStatus(echStatusInner))
if !c.ech.accepted {
if len(c.ech.retryConfigs) > 0 {
c.handleCFEvent(CFEventECHServerStatus(echStatusOuter))
} else {
c.handleCFEvent(CFEventECHServerStatus(echStatusBypassed))
}
} else {
c.handleCFEvent(CFEventECHServerStatus(echStatusInner))
}
}

return alertErr
}

Expand Down Expand Up @@ -1484,6 +1528,8 @@ func (c *Conn) connectionStateLocked() ConnectionState {
}
state.SignedCertificateTimestamps = c.scts
state.OCSPResponse = c.ocspResponse
state.ECHAccepted = c.ech.accepted
state.ECHOffered = c.ech.offered || c.ech.greased
state.CFControl = c.config.CFControl
if !c.didResume && c.vers != VersionTLS13 {
if c.clientFinishedIsFirst {
Expand Down
Loading

0 comments on commit f6b38b2

Please sign in to comment.