Skip to content

Commit ae11b13

Browse files
cjpattonLekensteyn
authored andcommitted
crypto/tls: implement draft-ietf-tls-esni-13
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.
1 parent ccb8370 commit ae11b13

17 files changed

+3191
-34
lines changed

src/crypto/tls/alert.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
alertUnknownPSKIdentity alert = 115
4949
alertCertificateRequired alert = 116
5050
alertNoApplicationProtocol alert = 120
51+
alertECHRequired alert = 121
5152
)
5253

5354
var alertText = map[alert]string{
@@ -84,6 +85,7 @@ var alertText = map[alert]string{
8485
alertUnknownPSKIdentity: "unknown PSK identity",
8586
alertCertificateRequired: "certificate required",
8687
alertNoApplicationProtocol: "no application protocol",
88+
alertECHRequired: "ECH required",
8789
}
8890

8991
func (e alert) String() string {

src/crypto/tls/common.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const (
101101
extensionSignatureAlgorithmsCert uint16 = 50
102102
extensionKeyShare uint16 = 51
103103
extensionRenegotiationInfo uint16 = 0xff01
104+
extensionECH uint16 = 0xfe0d // draft-ietf-tls-esni-13
105+
extensionECHOuterExtensions uint16 = 0xfd00 // draft-ietf-tls-esni-13
104106
)
105107

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

228+
// testingTriggerHRR causes the server to intentionally trigger a
229+
// HelloRetryRequest (HRR). This is useful for testing new TLS features that
230+
// change the HRR codepath.
231+
var testingTriggerHRR bool
232+
233+
// testingECHTriggerBypassAfterHRR causes the client to bypass ECH after HRR.
234+
// If available, the client will offer ECH in the first CH only.
235+
var testingECHTriggerBypassAfterHRR bool
236+
237+
// testingECHTriggerBypassBeforeHRR causes the client to bypass ECH before HRR.
238+
// The client will offer ECH in the second CH only.
239+
var testingECHTriggerBypassBeforeHRR bool
240+
241+
// testingECHIllegalHandleAfterHRR causes the client to illegally change the ECH
242+
// extension after HRR.
243+
var testingECHIllegalHandleAfterHRR bool
244+
245+
// testingECHTriggerPayloadDecryptError causes the client to to send an
246+
// inauthentic payload.
247+
var testingECHTriggerPayloadDecryptError bool
248+
249+
// testingECHOuterExtMany causes a client to incorporate a sequence of
250+
// outer extensions into the ClientHelloInner when it offers the ECH extension.
251+
// The "key_share" extension is the only incorporated extension by default.
252+
var testingECHOuterExtMany bool
253+
254+
// testingECHOuterExtNone causes a client to not use the "outer_extension"
255+
// mechanism for ECH. The "key_shares" extension is incorporated by default.
256+
var testingECHOuterExtNone bool
257+
258+
// testingECHOuterExtIncorrectOrder causes the client to send the
259+
// "outer_extension" extension in the wrong order when offering the ECH
260+
// extension.
261+
var testingECHOuterExtIncorrectOrder bool
262+
263+
// testingECHOuterExtIllegal causes the client to send in its
264+
// "outer_extension" extension the codepoint for the ECH extension.
265+
var testingECHOuterExtIllegal bool
266+
226267
// ConnectionState records basic TLS details about the connection.
227268
type ConnectionState struct {
228269
// Version is the TLS version used by the connection (e.g. VersionTLS12).
@@ -291,6 +332,14 @@ type ConnectionState struct {
291332
// RFC 7627, and https://mitls.org/pages/attacks/3SHAKE#channelbindings.
292333
TLSUnique []byte
293334

335+
// ECHAccepted is set if the ECH extension was offered by the client and
336+
// accepted by the server.
337+
ECHAccepted bool
338+
339+
// ECHOffered is set if the ECH extension is present in the ClientHello.
340+
// This means the client has offered ECH or sent GREASE ECH.
341+
ECHOffered bool
342+
294343
// CFControl is used to pass additional TLS configuration information to
295344
// HTTP requests.
296345
//
@@ -710,7 +759,8 @@ type Config struct {
710759

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

716766
// SessionTicketKey is used by TLS servers to provide session resumption.
@@ -774,6 +824,23 @@ type Config struct {
774824
// used for debugging.
775825
KeyLogWriter io.Writer
776826

827+
// ECHEnabled determines whether the ECH extension is enabled for this
828+
// connection.
829+
ECHEnabled bool
830+
831+
// ClientECHConfigs are the parameters used by the client when it offers the
832+
// ECH extension. If ECH is enabled, a suitable configuration is found, and
833+
// the client supports TLS 1.3, then it will offer ECH in this handshake.
834+
// Otherwise, if ECH is enabled, it will send a dummy ECH extension.
835+
ClientECHConfigs []ECHConfig
836+
837+
// ServerECHProvider is the ECH provider used by the client-facing server
838+
// for the ECH extension. If the client offers ECH and TLS 1.3 is
839+
// negotiated, then the provider is used to compute the HPKE context
840+
// (draft-irtf-cfrg-hpke-07), which in turn is used to decrypt the extension
841+
// payload.
842+
ServerECHProvider ECHProvider
843+
777844
// CFEventHandler, if set, is called by the client and server at various
778845
// points during the handshake to handle specific events. This is used
779846
// primarily for collecting metrics.
@@ -887,6 +954,9 @@ func (c *Config) Clone() *Config {
887954
Renegotiation: c.Renegotiation,
888955
KeyLogWriter: c.KeyLogWriter,
889956
SupportDelegatedCredential: c.SupportDelegatedCredential,
957+
ECHEnabled: c.ECHEnabled,
958+
ClientECHConfigs: c.ClientECHConfigs,
959+
ServerECHProvider: c.ServerECHProvider,
890960
CFEventHandler: c.CFEventHandler,
891961
CFControl: c.CFControl,
892962
sessionTicketKeys: c.sessionTicketKeys,
@@ -1079,6 +1149,17 @@ func (c *Config) supportedVersions(isClient bool) []uint16 {
10791149
return versions
10801150
}
10811151

1152+
func (c *Config) supportedVersionsFromMin(isClient bool, minVersion uint16) []uint16 {
1153+
versions := c.supportedVersions(isClient)
1154+
filteredVersions := versions[:0]
1155+
for _, v := range versions {
1156+
if v >= minVersion {
1157+
filteredVersions = append(filteredVersions, v)
1158+
}
1159+
}
1160+
return filteredVersions
1161+
}
1162+
10821163
func (c *Config) maxSupportedVersion(isClient bool) uint16 {
10831164
supportedVersions := c.supportedVersions(isClient)
10841165
if len(supportedVersions) == 0 {

src/crypto/tls/conn.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package tls
88

99
import (
1010
"bytes"
11+
"circl/hpke"
1112
"context"
1213
"crypto/cipher"
1314
"crypto/subtle"
@@ -119,6 +120,20 @@ type Conn struct {
119120
activeCall int32
120121

121122
tmp [16]byte
123+
124+
// State used for the ECH extension.
125+
ech struct {
126+
sealer hpke.Sealer // The client's HPKE context
127+
opener hpke.Opener // The server's HPKE context
128+
129+
// The state shared by the client and server.
130+
offered bool // Client offered ECH
131+
greased bool // Client greased ECH
132+
accepted bool // Server accepted ECH
133+
retryConfigs []byte // The retry configurations
134+
configId uint8 // The ECH config id
135+
maxNameLen int // maximum_name_len indicated by the ECH config
136+
}
122137
}
123138

124139
// Access to net.Conn methods.
@@ -705,6 +720,12 @@ func (c *Conn) readRecordOrCCS(expectChangeCipherSpec bool) error {
705720
return c.in.setErrorLocked(io.EOF)
706721
}
707722
if c.vers == VersionTLS13 {
723+
if !c.isClient && c.ech.greased && alert(data[1]) == alertECHRequired {
724+
// This condition indicates that the client intended to offer
725+
// ECH, but did not use a known ECH config.
726+
c.ech.offered = true
727+
c.ech.greased = false
728+
}
708729
return c.in.setErrorLocked(&net.OpError{Op: "remote error", Err: alert(data[1])})
709730
}
710731
switch data[0] {
@@ -1349,6 +1370,29 @@ func (c *Conn) Close() error {
13491370
if err := c.conn.Close(); err != nil {
13501371
return err
13511372
}
1373+
1374+
// Resolve ECH status.
1375+
if !c.isClient && c.config.MaxVersion < VersionTLS13 {
1376+
c.handleCFEvent(CFEventECHServerStatus(echStatusBypassed))
1377+
} else if !c.ech.offered {
1378+
if !c.ech.greased {
1379+
c.handleCFEvent(CFEventECHClientStatus(echStatusBypassed))
1380+
} else {
1381+
c.handleCFEvent(CFEventECHClientStatus(echStatusOuter))
1382+
}
1383+
} else {
1384+
c.handleCFEvent(CFEventECHClientStatus(echStatusInner))
1385+
if !c.ech.accepted {
1386+
if len(c.ech.retryConfigs) > 0 {
1387+
c.handleCFEvent(CFEventECHServerStatus(echStatusOuter))
1388+
} else {
1389+
c.handleCFEvent(CFEventECHServerStatus(echStatusBypassed))
1390+
}
1391+
} else {
1392+
c.handleCFEvent(CFEventECHServerStatus(echStatusInner))
1393+
}
1394+
}
1395+
13521396
return alertErr
13531397
}
13541398

@@ -1504,6 +1548,8 @@ func (c *Conn) connectionStateLocked() ConnectionState {
15041548
}
15051549
state.SignedCertificateTimestamps = c.scts
15061550
state.OCSPResponse = c.ocspResponse
1551+
state.ECHAccepted = c.ech.accepted
1552+
state.ECHOffered = c.ech.offered || c.ech.greased
15071553
state.CFControl = c.config.CFControl
15081554
if !c.didResume && c.vers != VersionTLS13 {
15091555
if c.clientFinishedIsFirst {

0 commit comments

Comments
 (0)