Skip to content

Commit 08059b3

Browse files
committed
Fix nits
1 parent f46052f commit 08059b3

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

src/crypto/tls/common.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,18 @@ const (
479479
ECDSAWithSHA1 SignatureScheme = 0x0203
480480
)
481481

482+
// A TLSFlag is the index of the bit in the TLSFlags bit array that should be
483+
// set to send the flag. Because TLSFlags can have so many different uses, many
484+
// yet to be defined, this extension should not be copied into the ECH
485+
// ClientHelloOuter
482486
type TLSFlag uint16
483487

484488
const (
485-
FlagSupportMTLS TLSFlag = 0x50
489+
// ExperimentalFlagSupportMTLS is a flag that signals that a client
490+
// supports mTLS, and will be able to respond to CertificateRequest
491+
// messages appropriately.
492+
// https://datatracker.ietf.org/doc/draft-jhoyla-req-mtls-flag/
493+
ExperimentalFlagSupportMTLS TLSFlag = 0x50
486494
)
487495

488496
// ClientHelloInfo contains information from a ClientHello message in order to
@@ -921,6 +929,10 @@ type Config struct {
921929
// See https://tools.ietf.org/html/draft-ietf-tls-subcerts.
922930
SupportDelegatedCredential bool
923931

932+
// TLSFlagsSupported is the list of flags that the client or server is
933+
// willing to support. This is currently limited to the set of flags set in
934+
// the ClientHello, although the draft specifies various other messages
935+
// where they can appear.
924936
TLSFlagsSupported []TLSFlag
925937

926938
// mutex protects sessionTicketKeys and autoSessionTicketKeys.

src/crypto/tls/handshake_messages.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,7 @@ func (m *clientHelloMsg) unmarshal(data []byte) bool {
591591
if !extData.ReadUint8LengthPrefixed(&flagsList) || flagsList.Empty() {
592592
return false
593593
}
594-
for !flagsList.Empty() {
595-
var flagByte uint8
596-
if !flagsList.ReadUint8(&flagByte) {
597-
return false
598-
}
599-
m.tlsFlags = append(m.tlsFlags, flagByte)
600-
}
594+
m.tlsFlags = flagsList
601595
case extensionCookie:
602596
// RFC 8446, Section 4.2.2
603597
if !readUint16LengthPrefixed(&extData, &m.cookie) ||

src/crypto/tls/handshake_server_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,41 @@ func testCrossVersionResume(t *testing.T, version uint16) {
539539
}
540540
}
541541

542+
func TestTLSFlags(t *testing.T) {
543+
serverConfig := &Config{
544+
Certificates: []Certificate{{
545+
Certificate: [][]byte{testRSACertificate},
546+
PrivateKey: testRSAPrivateKey,
547+
}},
548+
TLSFlagsSupported: []TLSFlag{0x50},
549+
}
550+
clientCert, err := X509KeyPair([]byte(clientECDSACertificatePEM), []byte(clientECDSAKeyPEM))
551+
if err != nil {
552+
t.Fatalf("couldn't load client certs")
553+
}
554+
clientConfig := &Config{
555+
TLSFlagsSupported: []TLSFlag{0x50},
556+
InsecureSkipVerify: true,
557+
Certificates: []Certificate{clientCert},
558+
}
559+
state, _, err := testHandshake(t, clientConfig, serverConfig)
560+
if err != nil {
561+
t.Fatalf("handshake failed: %s", err)
562+
}
563+
if state.PeerTLSFlags[0] != TLSFlag(0x50) {
564+
t.Fatalf("Received wrong flags")
565+
}
566+
if state.AgreedTLSFlags[0] != TLSFlag(0x50) {
567+
t.Fatalf("Failed to agree correct flags")
568+
}
569+
if !state.RequestClientCert {
570+
t.Fatalf("Failed to request client cert")
571+
}
572+
if len(state.PeerCertificates) == 0 {
573+
t.Fatalf("Didn't receive correct client certs")
574+
}
575+
}
576+
542577
// Note: see comment in handshake_test.go for details of how the reference
543578
// tests work.
544579

src/crypto/tls/handshake_server_tls13.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,13 @@ GroupSelection:
322322
return errors.New("tls: invalid client key share")
323323
}
324324
if len(hs.clientHello.tlsFlags) != 0 {
325+
if hs.clientHello.tlsFlags[len(hs.clientHello.tlsFlags)-1] == 0 {
326+
c.sendAlert(alertIllegalParameter)
327+
return errors.New("tls: invalid client TLS flags")
328+
}
325329
supportedFlags, err := encodeFlags(hs.c.config.TLSFlagsSupported)
326330
if err != nil {
331+
c.sendAlert(alertInternalError)
327332
return errors.New("tls: invalid server flags")
328333
}
329334
var mutuallySupportedFlags []byte
@@ -385,13 +390,14 @@ GroupSelection:
385390

386391
func decodeFlags(flagBytes []byte) ([]TLSFlag, error) {
387392
var flags []TLSFlag
393+
if len(flagBytes) > int(maxTLSFlag>>3) {
394+
return nil, fmt.Errorf("TLS flags extension malformed (too long)")
395+
}
396+
388397
for byteIndex, b := range flagBytes {
389398
for i := 0; !(b == 0); i++ {
390399
if (b & 1) == 1 {
391400
flagNo := byteIndex*8 + i
392-
if flagNo >= int(maxTLSFlag) {
393-
return nil, fmt.Errorf("TLS flag is out of range: %d", flagNo)
394-
}
395401
flags = append(flags, TLSFlag(flagNo))
396402
}
397403
b >>= 1
@@ -937,7 +943,7 @@ func (hs *serverHandshakeStateTLS13) sendServerParameters() error {
937943

938944
func (hs *serverHandshakeStateTLS13) requestClientCert() bool {
939945
for _, flag := range hs.tlsFlags {
940-
if flag == FlagSupportMTLS {
946+
if flag == ExperimentalFlagSupportMTLS {
941947
return true
942948
}
943949
}

0 commit comments

Comments
 (0)