Source file src/crypto/tls/ech_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tls
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestDecodeECHConfigLists(t *testing.T) {
    15  	for _, tc := range []struct {
    16  		list       string
    17  		numConfigs int
    18  	}{
    19  		{"0045fe0d0041590020002092a01233db2218518ccbbbbc24df20686af417b37388de6460e94011974777090004000100010012636c6f7564666c6172652d6563682e636f6d0000", 1},
    20  		{"0105badd00050504030201fe0d0066000010004104e62b69e2bf659f97be2f1e0d948a4cd5976bb7a91e0d46fbdda9a91e9ddcba5a01e7d697a80a18f9c3c4a31e56e27c8348db161a1cf51d7ef1942d4bcf7222c1000c000100010001000200010003400e7075626c69632e6578616d706c650000fe0d003d00002000207d661615730214aeee70533366f36a609ead65c0c208e62322346ab5bcd8de1c000411112222400e7075626c69632e6578616d706c650000fe0d004d000020002085bd6a03277c25427b52e269e0c77a8eb524ba1eb3d2f132662d4b0ac6cb7357000c000100010001000200010003400e7075626c69632e6578616d706c650008aaaa000474657374", 3},
    21  	} {
    22  		b, err := hex.DecodeString(tc.list)
    23  		if err != nil {
    24  			t.Fatal(err)
    25  		}
    26  		configs, err := parseECHConfigList(b)
    27  		if err != nil {
    28  			t.Fatal(err)
    29  		}
    30  		if len(configs) != tc.numConfigs {
    31  			t.Fatalf("unexpected number of configs parsed: got %d want %d", len(configs), tc.numConfigs)
    32  		}
    33  	}
    34  
    35  }
    36  
    37  func TestSkipBadConfigs(t *testing.T) {
    38  	b, err := hex.DecodeString("00c8badd00050504030201fe0d0029006666000401020304000c000100010001000200010003400e7075626c69632e6578616d706c650000fe0d003d000020002072e8a23b7aef67832bcc89d652e3870a60f88ca684ec65d6eace6b61f136064c000411112222400e7075626c69632e6578616d706c650000fe0d004d00002000200ce95810a81d8023f41e83679bc92701b2acd46c75869f95c72bc61c6b12297c000c000100010001000200010003400e7075626c69632e6578616d706c650008aaaa000474657374")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	configs, err := parseECHConfigList(b)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	config, _, _, _ := pickECHConfig(configs)
    47  	if config != nil {
    48  		t.Fatal("pickECHConfig picked an invalid config")
    49  	}
    50  }
    51  
    52  func TestPickECHConfigWithInvalidAEADID(t *testing.T) {
    53  	b, err := hex.DecodeString("0045fe0d0041590020002092a01233db2218518ccbbbbc24df20686af417b37388de6460e94011974777090004000100010012636c6f7564666c6172652d6563682e636f6d0000")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	buf := bytes.Replace(b, []byte{0x00, 0x01, 0x00, 0x01}, []byte{0x00, 0x01, 0xFF, 0xFF}, 1)
    58  	configs, err := parseECHConfigList(buf)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if config, _, _, _ := pickECHConfig(configs); config != nil {
    63  		t.Fatalf("got %v, want nil", config)
    64  	}
    65  }
    66  
    67  func TestECHPadding(t *testing.T) {
    68  	const maxNameLength = 64
    69  	for _, tc := range []struct {
    70  		name       string
    71  		serverName string
    72  	}{
    73  		{"Short", "a.test"},
    74  		{"Medium", strings.Repeat("a", 30) + ".test"},
    75  		{"MaxLength", strings.Repeat("a", maxNameLength) + ".test"},
    76  		{"NoSNI", ""},
    77  	} {
    78  		t.Run(tc.name, func(t *testing.T) {
    79  			inner := &clientHelloMsg{
    80  				vers:               VersionTLS13,
    81  				random:             make([]byte, 32),
    82  				serverName:         tc.serverName,
    83  				cipherSuites:       []uint16{TLS_AES_128_GCM_SHA256},
    84  				compressionMethods: []uint8{0},
    85  				supportedVersions:  []uint16{VersionTLS13},
    86  			}
    87  			encoded, err := encodeInnerClientHello(inner, maxNameLength)
    88  			if err != nil {
    89  				t.Fatal(err)
    90  			}
    91  			if len(encoded)%32 != 0 {
    92  				t.Errorf("got %d, want multiple of 32", len(encoded))
    93  			}
    94  		})
    95  	}
    96  
    97  	t.Run("SetSizeReduction", func(t *testing.T) {
    98  		sizes := make(map[int]struct{})
    99  		for sniLen := 1; sniLen <= maxNameLength; sniLen++ {
   100  			inner := &clientHelloMsg{
   101  				vers:               VersionTLS13,
   102  				random:             make([]byte, 32),
   103  				serverName:         strings.Repeat("a", sniLen) + ".test",
   104  				cipherSuites:       []uint16{TLS_AES_128_GCM_SHA256},
   105  				compressionMethods: []uint8{0},
   106  				supportedVersions:  []uint16{VersionTLS13},
   107  			}
   108  			encoded, err := encodeInnerClientHello(inner, maxNameLength)
   109  			if err != nil {
   110  				t.Fatal(err)
   111  			}
   112  			sizes[len(encoded)] = struct{}{}
   113  		}
   114  		if len(sizes) > 4 {
   115  			t.Errorf("got %d distinct encoded sizes for SNI lengths 1..%d, want <= 4", len(sizes), maxNameLength)
   116  		}
   117  	})
   118  }
   119  

View as plain text