echspec 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3902d0c840b18babccf1c9e7c764c570fd6b29e6f9688e0f9f0c5696bae4e871
4
- data.tar.gz: c01a13d625bc07918e8f80db55266019ff3031c597e30e9be0dbd4515755bcb5
3
+ metadata.gz: 8e07ddc58f010362c3d1cbf70aeaa0b89fb70c0ae6cad75f3c53dd4e64555986
4
+ data.tar.gz: 91f26f14e3520cc7761eb20b2f640a42ae22c2302c1372e5a59fec2ff3230a09
5
5
  SHA512:
6
- metadata.gz: 22d2c6085da85d18cbbf3d5e52f7f4f8511f2d878da9642ce1ad53817e4a33d8429c132456305592dd37e547c53998aa2db0bc091df276b4a216f4b2283b9492
7
- data.tar.gz: 9fa6ac8c64727d9e64418b7bcd3e38a98dc693b59ff52f6c7a42b4c7dc7635420d5dc60ef7f11ecf9837cd8d85a0fe7b5fd59987d81f57539ded979f330c4938
6
+ metadata.gz: 72bf8ce3e68f61c9a9ef325b004024453807a876cf2a4c654eb468041a3e44f07f555a9b9c04086451a9dc5ffa5b3021cf27dc43bc7279eb8b0fa6056623b8aa
7
+ data.tar.gz: b09ca3150563ca2745009ffc1baf63783ae6faa9da2a906ef103d8c7025c98dec78932ed39c74d5d5d986cab7967d339ae60cd3de25d7e7bbc565c87493ce46d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 4.0.2
1
+ 4.1-dev
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ gem 'tttls1.3'
7
7
  group :development do
8
8
  gem 'rake', '13.2.1'
9
9
  gem 'rspec'
10
- gem 'rubocop', '1.82.1'
10
+ gem 'rubocop', '1.93.0'
11
11
  end
12
12
 
13
13
  gemspec
data/README.md CHANGED
@@ -56,23 +56,29 @@ Example output:
56
56
  $ echspec run research.cloudflare.com
57
57
  TLS Encrypted Client Hello Server
58
58
  ✔ MUST implement the following HPKE cipher suite: KEM: DHKEM(X25519, HKDF-SHA256), KDF: HKDF-SHA256 and AEAD: AES-128-GCM. [9]
59
+ ✔ MUST include an "encrypted_client_hello" extension with an 8-byte confirmation payload in HelloRetryRequest, if ClientHello offers the inner variant. [5-11]
59
60
  ✔ MUST abort with an "illegal_parameter" alert, if EncodedClientHelloInner is padded with non-zero values. [5.1-9]
60
61
  ✔ MUST abort with an "illegal_parameter" alert, if any referenced extension is missing in ClientHelloOuter. [5.1-10]
61
62
  ✔ MUST abort with an "illegal_parameter" alert, if any extension is referenced in OuterExtensions more than once. [5.1-10]
62
63
  ✔ MUST abort with an "illegal_parameter" alert, if "encrypted_client_hello" is referenced in OuterExtensions. [5.1-10]
63
64
  ✔ MUST abort with an "illegal_parameter" alert, if the extensions in ClientHelloOuter corresponding to those in OuterExtensions do not occur in the same order. [5.1-10]
64
- ✔ MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloInner. [7-5]
65
- x MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloOuter. [7-5]
65
+ ✔ MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloInner. [7-3]
66
+ x MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloOuter. [7-3]
66
67
  ✔ MUST abort with an "illegal_parameter" alert, if ClientHelloInner offers TLS 1.2 or below. [7.1-11]
68
+ ✔ MUST abort with an "illegal_parameter" alert, if ClientHelloInner does not contain the "encrypted_client_hello" extension. [7.1-11]
67
69
  ✔ MUST include the "encrypted_client_hello" extension in its EncryptedExtensions with the "retry_configs" field set to one or more ECHConfig. [7.1-14.2.1]
68
70
  ✔ MUST abort with a "missing_extension" alert, if 2nd ClientHelloOuter does not contains the "encrypted_client_hello" extension. [7.1.1-2]
69
- ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" enc is empty. [7.1.1-2]
71
+ ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" enc is not empty. [7.1.1-2]
72
+ ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" config_id is changed. [7.1.1-2]
73
+ ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" cipher_suite is changed. [7.1.1-2]
70
74
  ✔ MUST abort with a "decrypt_error" alert, if fails to decrypt 2nd ClientHelloOuter. [7.1.1-5]
75
+ ✔ MUST include the "encrypted_client_hello" extension in its EncryptedExtensions with the "retry_configs" field set to one or more ECHConfig, if it rejected ECH with HelloRetryRequest. [7.1.1-7]
76
+ ✔ MUST confirm ECH acceptance in ServerHello, if it confirmed ECH acceptance in HelloRetryRequest. [7.2-1]
71
77
 
72
78
  Failures:
73
79
 
74
- 1) MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloOuter. [7-5]
75
- https://datatracker.ietf.org/doc/html/rfc9849#section-7-5
80
+ 1) MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloOuter. [7-3]
81
+ https://datatracker.ietf.org/doc/html/rfc9849#section-7-3
76
82
  did not send expected alert: illegal_parameter
77
83
 
78
84
  1 failure
@@ -105,14 +111,16 @@ To run only specific test SECTIONS, use the `-s` option:
105
111
  $ echspec run -f fixtures/echconfigs.pem -p 4433 -n -s 7.1.1-2,7.1.1-5 localhost
106
112
  TLS Encrypted Client Hello Server
107
113
  ✔ MUST abort with a "missing_extension" alert, if 2nd ClientHelloOuter does not contains the "encrypted_client_hello" extension. [7.1.1-2]
108
- ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" enc is empty. [7.1.1-2]
114
+ ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" enc is not empty. [7.1.1-2]
115
+ ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" config_id is changed. [7.1.1-2]
116
+ ✔ MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" cipher_suite is changed. [7.1.1-2]
109
117
  ✔ MUST abort with a "decrypt_error" alert, if fails to decrypt 2nd ClientHelloOuter. [7.1.1-5]
110
118
  ```
111
119
 
112
120
  Using the `-v` or `--verbose` option provides a message stack if an error occurs. The message stack is formatted as JSON.
113
121
 
114
122
  ```sh-session
115
- $ echspec run -s 7-5 -v research.cloudflare.com 2>&1 > /dev/null | jq .
123
+ $ echspec run -s 7-3 -v research.cloudflare.com 2>&1 > /dev/null | jq .
116
124
  ````
117
125
 
118
126
  <details>
@@ -287,21 +295,43 @@ $ echspec run -s 7-5 -v research.cloudflare.com 2>&1 > /dev/null | jq .
287
295
 
288
296
  ### gen_configs
289
297
 
290
- You can generate an ECHConfig PEM file the following:
298
+ You can generate an ECHConfig PEM file, in the format defined in [RFC 9934](https://datatracker.ietf.org/doc/html/rfc9934), the following:
291
299
 
292
300
  ```sh-session
293
301
  $ echspec gen_configs echconfigs.pem
294
302
  ```
295
303
  ```
296
304
  -----BEGIN PRIVATE KEY-----
297
- MC4CAQAwBQYDK2VuBCIEICjd4yGRdsoP9gU7YT7My8DHx1Tjme8GYDXrOMCi8v1V
305
+ MC4CAQAwBQYDK2VuBCIEIJhqOprwiCzv3taJ6OfqCt9IDkso/9z8rShp6D+NbMpg
298
306
  -----END PRIVATE KEY-----
299
307
  -----BEGIN ECHCONFIG-----
300
- AD7+DQA65wAgACA8wVN2BtscOl3vQheUzHeIkVmKIiydUhDCliA4iyQRCwAEAAEA
301
- AQALZXhhbXBsZS5jb20AAA==
308
+ ADz+DQA4ewAgACB/trzH8nCxAxVnGy4ZiFuTYKOtLw0X7FO8CQRbU8eZSAAEAAEA
309
+ ASAJbG9jYWxob3N0AAA=
302
310
  -----END ECHCONFIG-----
303
311
  ```
304
312
 
313
+ The generated file contains the following two PEM blocks:
314
+
315
+ - `PRIVATE KEY`
316
+ - X25519
317
+ - `ECHCONFIG`
318
+ - version
319
+ - 0xfe0d
320
+ - config_id
321
+ - 123
322
+ - KEM
323
+ - DHKEM(X25519, HKDF-SHA256)
324
+ - KDF
325
+ - HKDF-SHA256
326
+ - AEAD
327
+ - AES-128-GCM
328
+ - maximum\_name\_length
329
+ - 32
330
+ - public_name
331
+ - "localhost"
332
+ - extensions
333
+ - []
334
+
305
335
  ### resolve
306
336
 
307
337
  You can resolve ECHConfigs for a hostname and print fields using the `resolve` subcommand.
@@ -326,20 +356,20 @@ Options:
326
356
  ```
327
357
  ```sh-session
328
358
  $ echspec resolve research.cloudflare.com
329
- ECHConfig: │
330
- version(uint16): │ fe 0d
331
- length(uint16): │ 65
332
- contents(ECHConfigContents): │
333
- key_config(HpkeKeyConfig): │
334
- config_id(uint8): │ 104
335
- kem_id(uint16): │ 00 20
336
- public_key(opaque): │ e8 49 25 b3 6c e3 fb 9c 72 c7 71 e2 7a b2 8c 65 84 ba c2 2a 90 c2 cb 59 23 b2 27 d8 c1 85 84 17
337
- cipher_suites(HpkeSymmetricCipherSuite): │
338
- kdf_id(uint16): │ 00 01
339
- aead_id(uint16): │ 00 01
340
- maximum_name_length(uint8): │ 0
341
- public_name(opaque): │ cloudflare-ech.com
342
- extensions(opaque): │
359
+ ECHConfig: │
360
+ version(hex): │ fe 0d
361
+ length(integer): │ 65
362
+ contents: │
363
+ key_config: │
364
+ config_id(integer): │ 104
365
+ kem_id(hex): │ 00 20
366
+ public_key(hex): │ e8 49 25 b3 6c e3 fb 9c 72 c7 71 e2 7a b2 8c 65 84 ba c2 2a 90 c2 cb 59 23 b2 27 d8 c1 85 84 17
367
+ cipher_suites: │
368
+ kdf_id(hex): │ 00 01
369
+ aead_id(hex): │ 00 01
370
+ maximum_name_length(integer): │ 0
371
+ public_name(string): │ cloudflare-ech.com
372
+ extensions(hex): │
343
373
  ```
344
374
 
345
375
  ### grease
@@ -362,20 +392,20 @@ Options:
362
392
  ```
363
393
  ```sh-session
364
394
  $ echspec grease research.cloudflare.com
365
- ECHConfig: │
366
- version(uint16): │ fe 0d
367
- length(uint16): │ 65
368
- contents(ECHConfigContents): │
369
- key_config(HpkeKeyConfig): │
370
- config_id(uint8): │ 72
371
- kem_id(uint16): │ 00 20
372
- public_key(opaque): │ 0a 89 2a 9f 05 0a d5 21 da cc 42 65 df cf 70 38 22 7e 85 44 93 1b 68 fa a2 b5 37 ad d8 2c 89 39
373
- cipher_suites(HpkeSymmetricCipherSuite): │
374
- kdf_id(uint16): │ 00 01
375
- aead_id(uint16): │ 00 01
376
- maximum_name_length(uint8): │ 0
377
- public_name(opaque): │ cloudflare-ech.com
378
- extensions(opaque): │
395
+ ECHConfig: │
396
+ version(hex): │ fe 0d
397
+ length(integer): │ 65
398
+ contents: │
399
+ key_config: │
400
+ config_id(integer): │ 72
401
+ kem_id(hex): │ 00 20
402
+ public_key(hex): │ 0a 89 2a 9f 05 0a d5 21 da cc 42 65 df cf 70 38 22 7e 85 44 93 1b 68 fa a2 b5 37 ad d8 2c 89 39
403
+ cipher_suites: │
404
+ kdf_id(hex): │ 00 01
405
+ aead_id(hex): │ 00 01
406
+ maximum_name_length(integer): │ 0
407
+ public_name(string): │ cloudflare-ech.com
408
+ extensions(hex): │
379
409
  ```
380
410
 
381
411
 
@@ -28,25 +28,25 @@ module EchSpec
28
28
  ext = ec.extensions.octet.unpack1('H*').scan(/.{2}/).join(' ')
29
29
  cipher_suite_pairs = kc.cipher_suites.flat_map do |cs|
30
30
  [
31
- [' kdf_id(uint16):', cs.kdf_id.encode.unpack1('H4').scan(/.{2}/).join(' ')],
32
- [' aead_id(uint16):', cs.aead_id.encode.unpack1('H4').scan(/.{2}/).join(' ')]
31
+ [' kdf_id(hex):', cs.kdf_id.encode.unpack1('H4').scan(/.{2}/).join(' ')],
32
+ [' aead_id(hex):', cs.aead_id.encode.unpack1('H4').scan(/.{2}/).join(' ')]
33
33
  ]
34
34
  end
35
35
 
36
36
  [
37
37
  ['ECHConfig:', ''],
38
- [' version(uint16):', c.version.unpack1('H4').scan(/.{2}/).join(' ')],
39
- [' length(uint16):', ec.encode.length],
40
- [' contents(ECHConfigContents):', ''],
41
- [' key_config(HpkeKeyConfig):', ''],
42
- [' config_id(uint8):', kc.config_id],
43
- [' kem_id(uint16):', kc.kem_id.encode.unpack1('H4').scan(/.{2}/).join(' ')],
44
- [' public_key(opaque):', kc.public_key.opaque.unpack1('H*').scan(/.{2}/).join(' ')],
45
- [' cipher_suites(HpkeSymmetricCipherSuite):', ''],
38
+ [' version(hex):', c.version.unpack1('H4').scan(/.{2}/).join(' ')],
39
+ [' length(integer):', ec.encode.length],
40
+ [' contents:', ''],
41
+ [' key_config:', ''],
42
+ [' config_id(integer):', kc.config_id],
43
+ [' kem_id(hex):', kc.kem_id.encode.unpack1('H4').scan(/.{2}/).join(' ')],
44
+ [' public_key(hex):', kc.public_key.opaque.unpack1('H*').scan(/.{2}/).join(' ')],
45
+ [' cipher_suites:', ''],
46
46
  *cipher_suite_pairs,
47
- [' maximum_name_length(uint8):', ec.maximum_name_length],
48
- [' public_name(opaque):', ec.public_name],
49
- [' extensions(opaque):', ext]
47
+ [' maximum_name_length(integer):', ec.maximum_name_length],
48
+ [' public_name(string):', ec.public_name],
49
+ [' extensions(hex):', ext]
50
50
  ]
51
51
  end
52
52
  # rubocop: enable Metrics/AbcSize
@@ -36,32 +36,8 @@ module EchSpec
36
36
  end
37
37
 
38
38
  def write(fpath)
39
- hostname = 'localhost'
40
-
41
39
  key = OpenSSL::PKey.generate_key('X25519')
42
- echconfigs = ECHConfigList.new(
43
- [
44
- ECHConfig.new(
45
- "\xfe\x0d".b,
46
- ECHConfig::ECHConfigContents.new(
47
- ECHConfig::ECHConfigContents::HpkeKeyConfig.new(
48
- 123,
49
- ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeKemId.new(HPKE::DHKEM_X25519_HKDF_SHA256),
50
- ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkePublicKey.new(key.raw_public_key),
51
- [
52
- ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite.new(
53
- ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite::HpkeKdfId.new(HPKE::HKDF_SHA256),
54
- ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite::HpkeAeadId.new(HPKE::AES_128_GCM)
55
- )
56
- ]
57
- ),
58
- 32,
59
- hostname.b,
60
- ECHConfig::ECHConfigContents::Extensions.new('')
61
- )
62
- )
63
- ]
64
- )
40
+ echconfigs = ECHConfigList.new([EchConfig.gen_ech_config(key.raw_public_key)])
65
41
  File.write(fpath, key.private_to_pem + echconfigs.to_pem)
66
42
  end
67
43
  end
@@ -8,7 +8,7 @@ module EchSpec
8
8
 
9
9
  TTTLS13::Logging.logger.level = Logger::WARN
10
10
  socket = TCPSocket.new(hostname, port)
11
- recv = TLS13Client.send_ch_with_greased_ech(socket, hostname)
11
+ recv = TLS13Client.send_ch_with_greased_ech(socket, hostname, Log::MessageStack.new)
12
12
  ex = recv.extensions[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO]
13
13
 
14
14
  if ex.nil? || ex.retry_configs.nil? || ex.retry_configs.empty?
@@ -0,0 +1,36 @@
1
+ module EchSpec
2
+ module EchConfig
3
+ # @param public_key [String]
4
+ # @param kem_id [Integer]
5
+ # @param kdf_id [Integer]
6
+ # @param aead_id [Integer]
7
+ #
8
+ # @return [ECHConfig]
9
+ def self.gen_ech_config(public_key, kem_id: HPKE::DHKEM_X25519_HKDF_SHA256, kdf_id: HPKE::HKDF_SHA256, aead_id: HPKE::AES_128_GCM)
10
+ version = "\xfe\x0d".b
11
+ config_id = 123
12
+ maximum_name_length = 32
13
+ hostname = 'localhost'
14
+
15
+ ECHConfig.new(
16
+ version,
17
+ ECHConfig::ECHConfigContents.new(
18
+ ECHConfig::ECHConfigContents::HpkeKeyConfig.new(
19
+ config_id,
20
+ ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeKemId.new(kem_id),
21
+ ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkePublicKey.new(public_key),
22
+ [
23
+ ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite.new(
24
+ ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite::HpkeKdfId.new(kdf_id),
25
+ ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite::HpkeAeadId.new(aead_id)
26
+ )
27
+ ]
28
+ ),
29
+ maximum_name_length,
30
+ hostname.b,
31
+ ECHConfig::ECHConfigContents::Extensions.new('')
32
+ )
33
+ )
34
+ end
35
+ end
36
+ end
data/lib/echspec/log.rb CHANGED
@@ -17,7 +17,6 @@ module EchSpec
17
17
  end
18
18
 
19
19
  # rubocop: disable Metrics/CyclomaticComplexity
20
- # rubocop: disable Metrics/PerceivedComplexity
21
20
  def self.msg2name(msg)
22
21
  case msg
23
22
  in TTTLS13::Message::ClientHello if msg.ch_inner?
@@ -49,10 +48,8 @@ module EchSpec
49
48
  end
50
49
  end
51
50
  # rubocop: enable Metrics/CyclomaticComplexity
52
- # rubocop: enable Metrics/PerceivedComplexity
53
51
 
54
52
  # rubocop: disable Metrics/CyclomaticComplexity
55
- # rubocop: disable Metrics/PerceivedComplexity
56
53
  def self.obj2json(obj)
57
54
  case obj
58
55
  in OpenSSL::X509::Certificate
@@ -83,7 +80,6 @@ module EchSpec
83
80
  end
84
81
  end
85
82
  # rubocop: enable Metrics/CyclomaticComplexity
86
- # rubocop: enable Metrics/PerceivedComplexity
87
83
  end
88
84
  end
89
85
  end
@@ -0,0 +1,70 @@
1
+ module EchSpec
2
+ module Spec
3
+ class Spec5_11 < WithSocket
4
+ # Finally, when the client offers the "encrypted_client_hello", if the
5
+ # payload is the inner variant and the server responds with
6
+ # HelloRetryRequest, it MUST include an "encrypted_client_hello"
7
+ # extension with the following payload:
8
+ #
9
+ # struct {
10
+ # opaque confirmation[8];
11
+ # } ECHHelloRetryRequest;
12
+ #
13
+ # The value of ECHHelloRetryRequest.confirmation is set to
14
+ # hrr_accept_confirmation as described in Section 7.2.1.
15
+ #
16
+ # https://datatracker.ietf.org/doc/html/rfc9849#section-5-11
17
+
18
+ # @return [EchSpec::SpecGroup]
19
+ def self.spec_group
20
+ SpecGroup.new(
21
+ '5-11',
22
+ [
23
+ SpecCase.new(
24
+ 'MUST include an "encrypted_client_hello" extension with an 8-byte confirmation payload in HelloRetryRequest, if ClientHello offers the inner variant.',
25
+ method(:validate_hrr_ech_length)
26
+ )
27
+ ]
28
+ )
29
+ end
30
+
31
+ # @param hostname [String]
32
+ # @param port [Integer]
33
+ # @param ech_config [ECHConfig]
34
+ #
35
+ # @return [EchSpec::Ok | Err]
36
+ def self.validate_hrr_ech_length(hostname, port, ech_config)
37
+ Spec5_11.new.do_validate_hrr_ech_length(hostname, port, ech_config)
38
+ end
39
+
40
+ # @param hostname [String]
41
+ # @param port [Integer]
42
+ # @param ech_config [ECHConfig]
43
+ #
44
+ # @return [EchSpec::Ok | Err]
45
+ def do_validate_hrr_ech_length(hostname, port, ech_config)
46
+ with_socket(hostname, port) do |socket|
47
+ _conn, _inner, _ch, hrr, = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
48
+ validate_hrr_ech(hrr)
49
+ end
50
+ end
51
+
52
+ # @param hrr [TTTLS13::Message::ServerHello]
53
+ #
54
+ # @return [EchSpec::Ok | Err]
55
+ def validate_hrr_ech(hrr)
56
+ # Extensions#[] returns nil for UnknownExtension, so use super_fetch.
57
+ ex = hrr.extensions.super_fetch(TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO, nil)
58
+ case ex
59
+ in TTTLS13::Message::Extension::ECHHelloRetryRequest
60
+ Ok.new(nil)
61
+ in nil
62
+ Err.new('HelloRetryRequest did not include "encrypted_client_hello" extension', message_stack)
63
+ in TTTLS13::Message::Extension::UnknownExtension
64
+ # tttls1.3 deserializes the extension whose length is not 8 as UnknownExtension
65
+ Err.new("HelloRetryRequest \"encrypted_client_hello\" extension has #{ex.extension_data.length}-byte payload, expected 8 bytes", message_stack)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -92,14 +92,9 @@ module EchSpec
92
92
  def self.padding_encoded_ch_inner(s,
93
93
  server_name_length,
94
94
  maximum_name_length)
95
- padding_len =
96
- if server_name_length.positive?
97
- [maximum_name_length - server_name_length, 0].max
98
- else
99
- 9 + maximum_name_length
100
- end
101
-
102
- padding_len = 31 - ((s.length + padding_len - 1) % 32)
95
+ padding_len = super.length - s.length
96
+ # at least one non-zero byte is needed for this test case
97
+ padding_len = 32 if padding_len.zero?
103
98
  s + NON_ZERO * padding_len # padding with non-zero value
104
99
  end
105
100
  end
@@ -1,19 +1,15 @@
1
1
  module EchSpec
2
2
  module Spec
3
- class Spec7_5 < WithSocket
4
- # In shared mode, a server plays both roles, first decrypting the
5
- # ClientHelloOuter and then using the contents of the ClientHelloInner.
6
- # A shared mode server which receives a ClientHello with
7
- # ECHClientHello.type of inner MUST abort with an "illegal_parameter"
8
- # alert, because such a ClientHello should never be received directly
9
- # from the network.
3
+ class Spec7_3 < WithSocket
4
+ # If ECHClientHello.type is not a valid ECHClientHelloType, then the
5
+ # server MUST abort with an "illegal_parameter" alert.
10
6
  #
11
- # https://datatracker.ietf.org/doc/html/rfc9849#section-7-5
7
+ # https://datatracker.ietf.org/doc/html/rfc9849#section-7-3
12
8
 
13
9
  # @return [EchSpec::SpecGroup]
14
10
  def self.spec_group
15
11
  SpecGroup.new(
16
- '7-5',
12
+ '7-3',
17
13
  [
18
14
  SpecCase.new(
19
15
  'MUST abort with an "illegal_parameter" alert, if ECHClientHello.type is not a valid ECHClientHelloType in ClientHelloInner.',
@@ -33,7 +29,7 @@ module EchSpec
33
29
  #
34
30
  # @return [EchSpec::Ok | Err]
35
31
  def self.validate_illegal_inner_ech_type(hostname, port, ech_config)
36
- Spec7_5.new.do_validate_illegal_ech_type(
32
+ Spec7_3.new.do_validate_illegal_ech_type(
37
33
  hostname,
38
34
  port,
39
35
  ech_config,
@@ -47,7 +43,7 @@ module EchSpec
47
43
  #
48
44
  # @return [EchSpec::Ok | Err]
49
45
  def self.validate_illegal_outer_ech_type(hostname, port, ech_config)
50
- Spec7_5.new.do_validate_illegal_ech_type(
46
+ Spec7_3.new.do_validate_illegal_ech_type(
51
47
  hostname,
52
48
  port,
53
49
  ech_config,
@@ -17,6 +17,10 @@ module EchSpec
17
17
  SpecCase.new(
18
18
  'MUST abort with an "illegal_parameter" alert, if ClientHelloInner offers TLS 1.2 or below.',
19
19
  method(:validate_ech_with_tls12)
20
+ ),
21
+ SpecCase.new(
22
+ 'MUST abort with an "illegal_parameter" alert, if ClientHelloInner does not contain the "encrypted_client_hello" extension.',
23
+ method(:validate_ch_inner_without_ech)
20
24
  )
21
25
  ]
22
26
  )
@@ -46,6 +50,30 @@ module EchSpec
46
50
  end
47
51
  end
48
52
 
53
+ # @param hostname [String]
54
+ # @param port [Integer]
55
+ # @param ech_config [ECHConfig]
56
+ #
57
+ # @return [EchSpec::Ok | Err]
58
+ def self.validate_ch_inner_without_ech(hostname, port, ech_config)
59
+ Spec7_1_11.new.do_validate_ch_inner_without_ech(hostname, port, ech_config)
60
+ end
61
+
62
+ # @param hostname [String]
63
+ # @param port [Integer]
64
+ # @param ech_config [ECHConfig]
65
+ #
66
+ # @return [EchSpec::Ok | Err]
67
+ def do_validate_ch_inner_without_ech(hostname, port, ech_config)
68
+ with_socket(hostname, port) do |socket|
69
+ recv = send_ch_inner_without_ech(socket, hostname, ech_config)
70
+ return Err.new('did not send expected alert: illegal_parameter', message_stack) \
71
+ unless Spec.expect_alert(recv, :illegal_parameter)
72
+
73
+ Ok.new(nil)
74
+ end
75
+ end
76
+
49
77
  # rubocop: disable Metrics/MethodLength
50
78
  def send_ch_ech_with_tls12(socket, hostname, ech_config)
51
79
  conn = TLS13Client::Connection.new(socket, :client)
@@ -88,6 +116,39 @@ module EchSpec
88
116
  recv
89
117
  end
90
118
  # rubocop: enable Metrics/MethodLength
119
+
120
+ def send_ch_inner_without_ech(socket, hostname, ech_config)
121
+ conn = TLS13Client::Connection.new(socket, :client)
122
+ exs, = TLS13Client.gen_ch_extensions(hostname)
123
+ # ClientHelloInner without "encrypted_client_hello"
124
+ inner = TTTLS13::Message::ClientHello.new(
125
+ cipher_suites: TTTLS13::CipherSuites.new(
126
+ [
127
+ TTTLS13::CipherSuite::TLS_AES_256_GCM_SHA384,
128
+ TTTLS13::CipherSuite::TLS_CHACHA20_POLY1305_SHA256,
129
+ TTTLS13::CipherSuite::TLS_AES_128_GCM_SHA256
130
+ ]
131
+ ),
132
+ extensions: exs
133
+ )
134
+
135
+ selector = proc { |x| TLS13Client.select_ech_hpke_cipher_suite(x) }
136
+ ch, inner, = TTTLS13::Ech.offer_ech(inner, ech_config, selector)
137
+ conn.send_record(
138
+ TTTLS13::Message::Record.new(
139
+ type: TTTLS13::Message::ContentType::HANDSHAKE,
140
+ messages: [ch],
141
+ cipher: TTTLS13::Cryptograph::Passer.new
142
+ )
143
+ )
144
+ @stack << inner
145
+ @stack << ch
146
+
147
+ recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new)
148
+ @stack << recv
149
+
150
+ recv
151
+ end
91
152
  end
92
153
  end
93
154
  end
@@ -44,13 +44,9 @@ module EchSpec
44
44
  def do_validate_ee_retry_configs(hostname, port)
45
45
  with_socket(hostname, port) do |socket|
46
46
  recv = TLS13Client.send_ch_with_greased_ech(socket, hostname, @stack)
47
- ex = recv.extensions[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO]
48
- return Err.new('did not send expected alert: encrypted_client_hello', message_stack) \
49
- unless ex.is_a?(TTTLS13::Message::Extension::ECHEncryptedExtensions)
50
- return Err.new('ECHConfigs did not have "retry_configs"', message_stack) \
51
- if ex.retry_configs.nil? || ex.retry_configs.empty?
52
-
53
- Ok.new(nil)
47
+ validate_ee_ech(recv)
48
+ rescue ECHConfig::DecodeError
49
+ Err.new('EncryptedExtensions "encrypted_client_hello" extension could not be decoded', message_stack)
54
50
  end
55
51
  end
56
52
  end
@@ -21,8 +21,16 @@ module EchSpec
21
21
  method(:validate_2nd_ch_missing_ech)
22
22
  ),
23
23
  SpecCase.new(
24
- 'MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" enc is empty.',
24
+ 'MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" enc is not empty.',
25
25
  method(:validate_2nd_ch_unchanged_ech)
26
+ ),
27
+ SpecCase.new(
28
+ 'MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" config_id is changed.',
29
+ method(:validate_2nd_ch_changed_config_id)
30
+ ),
31
+ SpecCase.new(
32
+ 'MUST abort with an "illegal_parameter" alert, if 2nd ClientHelloOuter "encrypted_client_hello" cipher_suite is changed.',
33
+ method(:validate_2nd_ch_changed_cipher_suite)
26
34
  )
27
35
  ]
28
36
  )
@@ -76,6 +84,54 @@ module EchSpec
76
84
  end
77
85
  end
78
86
 
87
+ # @param hostname [String]
88
+ # @param port [Integer]
89
+ # @param ech_config [ECHConfig]
90
+ #
91
+ # @return [EchSpec::Ok | Err]
92
+ def self.validate_2nd_ch_changed_config_id(hostname, port, ech_config)
93
+ Spec7_1_1_2.new.do_validate_2nd_ch_changed_config_id(hostname, port, ech_config)
94
+ end
95
+
96
+ # @param hostname [String]
97
+ # @param port [Integer]
98
+ # @param ech_config [ECHConfig]
99
+ #
100
+ # @return [EchSpec::Ok | Err]
101
+ def do_validate_2nd_ch_changed_config_id(hostname, port, ech_config)
102
+ with_socket(hostname, port) do |socket|
103
+ recv = send_2nd_ch_changed_config_id(socket, hostname, ech_config)
104
+ return Err.new('did not send expected alert: illegal_parameter', message_stack) \
105
+ unless Spec.expect_alert(recv, :illegal_parameter)
106
+
107
+ Ok.new(nil)
108
+ end
109
+ end
110
+
111
+ # @param hostname [String]
112
+ # @param port [Integer]
113
+ # @param ech_config [ECHConfig]
114
+ #
115
+ # @return [EchSpec::Ok | Err]
116
+ def self.validate_2nd_ch_changed_cipher_suite(hostname, port, ech_config)
117
+ Spec7_1_1_2.new.do_validate_2nd_ch_changed_cipher_suite(hostname, port, ech_config)
118
+ end
119
+
120
+ # @param hostname [String]
121
+ # @param port [Integer]
122
+ # @param ech_config [ECHConfig]
123
+ #
124
+ # @return [EchSpec::Ok | Err]
125
+ def do_validate_2nd_ch_changed_cipher_suite(hostname, port, ech_config)
126
+ with_socket(hostname, port) do |socket|
127
+ recv = send_2nd_ch_changed_cipher_suite(socket, hostname, ech_config)
128
+ return Err.new('did not send expected alert: illegal_parameter', message_stack) \
129
+ unless Spec.expect_alert(recv, :illegal_parameter)
130
+
131
+ Ok.new(nil)
132
+ end
133
+ end
134
+
79
135
  def send_2nd_ch_missing_ech(socket, hostname, ech_config)
80
136
  conn, _inner1, ch1, hrr, = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
81
137
  # send 2nd ClientHello without ech
@@ -98,12 +154,7 @@ module EchSpec
98
154
  )
99
155
  @stack << ch
100
156
 
101
- recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new)
102
- @stack << recv
103
-
104
- recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new) \
105
- if recv.is_a?(TTTLS13::Message::ChangeCipherSpec)
106
- recv
157
+ conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
107
158
  end
108
159
 
109
160
  def send_2nd_ch_unchanged_ech(socket, hostname, ech_config)
@@ -130,13 +181,80 @@ module EchSpec
130
181
  @stack << inner1
131
182
  @stack << ch
132
183
 
133
- recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new)
134
- @stack << recv
184
+ conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
185
+ end
186
+
187
+ def send_2nd_ch_changed_config_id(socket, hostname, ech_config)
188
+ conn, inner1, ch1, hrr, = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
189
+ # send 2nd ClientHello with changed config_id
190
+ ech1 = ch1.extensions[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO]
191
+ new_exs = TLS13Client.gen_newch_extensions(ch1, hrr)
192
+ new_exs[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO] =
193
+ TTTLS13::Message::Extension::ECHClientHello.new_outer(
194
+ cipher_suite: ech1.cipher_suite,
195
+ config_id: (ech1.config_id + 1) % 256,
196
+ enc: '',
197
+ payload: ech1.payload
198
+ )
199
+ ch = TTTLS13::Message::ClientHello.new(
200
+ legacy_version: ch1.legacy_version,
201
+ random: ch1.random,
202
+ legacy_session_id: ch1.legacy_session_id,
203
+ cipher_suites: ch1.cipher_suites,
204
+ legacy_compression_methods: ch1.legacy_compression_methods,
205
+ extensions: new_exs
206
+ )
207
+ conn.send_record(
208
+ TTTLS13::Message::Record.new(
209
+ type: TTTLS13::Message::ContentType::HANDSHAKE,
210
+ messages: [ch],
211
+ cipher: TTTLS13::Cryptograph::Passer.new
212
+ )
213
+ )
214
+ @stack << inner1
215
+ @stack << ch
216
+
217
+ conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
218
+ end
219
+
220
+ # rubocop: disable Metrics/MethodLength
221
+ def send_2nd_ch_changed_cipher_suite(socket, hostname, ech_config)
222
+ conn, inner1, ch1, hrr, = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
223
+ # send 2nd ClientHello with changed cipher_suite
224
+ ech1 = ch1.extensions[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO]
225
+ aead_id = ech1.cipher_suite.aead_id.uint16 == HPKE::AES_128_GCM ? HPKE::AES_256_GCM : HPKE::AES_128_GCM
226
+ new_exs = TLS13Client.gen_newch_extensions(ch1, hrr)
227
+ new_exs[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO] =
228
+ TTTLS13::Message::Extension::ECHClientHello.new_outer(
229
+ cipher_suite: ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite.new(
230
+ ech1.cipher_suite.kdf_id,
231
+ ECHConfig::ECHConfigContents::HpkeKeyConfig::HpkeSymmetricCipherSuite::HpkeAeadId.new(aead_id)
232
+ ),
233
+ config_id: ech1.config_id,
234
+ enc: '',
235
+ payload: ech1.payload
236
+ )
237
+ ch = TTTLS13::Message::ClientHello.new(
238
+ legacy_version: ch1.legacy_version,
239
+ random: ch1.random,
240
+ legacy_session_id: ch1.legacy_session_id,
241
+ cipher_suites: ch1.cipher_suites,
242
+ legacy_compression_methods: ch1.legacy_compression_methods,
243
+ extensions: new_exs
244
+ )
245
+ conn.send_record(
246
+ TTTLS13::Message::Record.new(
247
+ type: TTTLS13::Message::ContentType::HANDSHAKE,
248
+ messages: [ch],
249
+ cipher: TTTLS13::Cryptograph::Passer.new
250
+ )
251
+ )
252
+ @stack << inner1
253
+ @stack << ch
135
254
 
136
- recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new) \
137
- if recv.is_a?(TTTLS13::Message::ChangeCipherSpec)
138
- recv
255
+ conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
139
256
  end
257
+ # rubocop: enable Metrics/MethodLength
140
258
  end
141
259
  end
142
260
  end
@@ -48,16 +48,15 @@ module EchSpec
48
48
  end
49
49
 
50
50
  def send_2nd_ch_with_undecryptable_ech(socket, hostname, ech_config)
51
- conn, _inner1, ch1, hrr, ech_state = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
51
+ conn, inner1, _ch1, hrr, ech_state = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
52
52
  # send 2nd ClientHello with undecryptable ech
53
- new_exs = TLS13Client.gen_newch_extensions(ch1, hrr)
54
53
  inner = TTTLS13::Message::ClientHello.new(
55
- legacy_version: ch1.legacy_version,
56
- random: ch1.random,
57
- legacy_session_id: ch1.legacy_session_id,
58
- cipher_suites: ch1.cipher_suites,
59
- legacy_compression_methods: ch1.legacy_compression_methods,
60
- extensions: new_exs
54
+ legacy_version: inner1.legacy_version,
55
+ random: inner1.random,
56
+ legacy_session_id: inner1.legacy_session_id,
57
+ cipher_suites: inner1.cipher_suites,
58
+ legacy_compression_methods: inner1.legacy_compression_methods,
59
+ extensions: TLS13Client.gen_newch_extensions(inner1, hrr)
61
60
  )
62
61
  ech_state.ctx.increment_seq # invalidly increment of the sequence number
63
62
  ch, inner = TTTLS13::Ech.offer_new_ech(inner, ech_state)
@@ -71,12 +70,7 @@ module EchSpec
71
70
  @stack << inner
72
71
  @stack << ch
73
72
 
74
- recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new)
75
- @stack << recv
76
-
77
- recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new) \
78
- if recv.is_a?(TTTLS13::Message::ChangeCipherSpec)
79
- recv
73
+ conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
80
74
  end
81
75
  end
82
76
  end
@@ -0,0 +1,152 @@
1
+ module EchSpec
2
+ module Spec
3
+ class Spec7_1_1_7 < WithSocket
4
+ # If the client-facing server rejected ECH, or if the first ClientHello
5
+ # did not include an "encrypted_client_hello" extension, the client-
6
+ # facing server proceeds with the connection as usual. The server does
7
+ # not decrypt the second ClientHello's ECHClientHello.payload value, if
8
+ # there is one. Moreover, if the server is configured with any
9
+ # ECHConfigs, it MUST include the "encrypted_client_hello" extension in
10
+ # its EncryptedExtensions with the "retry_configs" field set to one or
11
+ # more ECHConfig structures with up-to-date keys, as described in
12
+ # Section 7.1.
13
+ #
14
+ # https://datatracker.ietf.org/doc/html/rfc9849#section-7.1.1-7
15
+
16
+ # @return [EchSpec::SpecGroup]
17
+ def self.spec_group
18
+ SpecGroup.new(
19
+ '7.1.1-7',
20
+ [
21
+ SpecCase.new(
22
+ 'MUST include the "encrypted_client_hello" extension in its EncryptedExtensions with the "retry_configs" field set to one or more ECHConfig, if it rejected ECH with HelloRetryRequest.',
23
+ method(:validate_ee_retry_configs_after_hrr)
24
+ )
25
+ ]
26
+ )
27
+ end
28
+
29
+ # @param hostname [String]
30
+ # @param port [Integer]
31
+ # @param _ [ECHConfig]
32
+ #
33
+ # @return [EchSpec::Ok | Err]
34
+ def self.validate_ee_retry_configs_after_hrr(hostname, port, _)
35
+ Spec7_1_1_7.new.do_validate_ee_retry_configs_after_hrr(hostname, port)
36
+ end
37
+
38
+ # @param hostname [String]
39
+ # @param port [Integer]
40
+ #
41
+ # @return [EchSpec::Ok | Err]
42
+ def do_validate_ee_retry_configs_after_hrr(hostname, port)
43
+ with_socket(hostname, port) do |socket|
44
+ recv = send_2nd_ch_with_greased_ech(socket, hostname)
45
+ validate_ee_ech(recv)
46
+ rescue ECHConfig::DecodeError
47
+ Err.new('EncryptedExtensions "encrypted_client_hello" extension could not be decoded', message_stack)
48
+ end
49
+ end
50
+
51
+ # rubocop: disable Metrics/AbcSize
52
+ # rubocop: disable Metrics/MethodLength
53
+ def send_2nd_ch_with_greased_ech(socket, hostname)
54
+ # send 1st ClientHello with greased ech
55
+ conn = TLS13Client::Connection.new(socket, :client)
56
+ inner_ech = TTTLS13::Message::Extension::ECHClientHello.new_inner
57
+ exs, = TLS13Client.gen_ch_extensions(hostname)
58
+ # for HRR
59
+ key_share = TTTLS13::Message::Extension::KeyShare.new(
60
+ msg_type: TTTLS13::Message::HandshakeType::CLIENT_HELLO,
61
+ key_share_entry: [] # empty client_shares vector
62
+ )
63
+ exs[TTTLS13::Message::ExtensionType::KEY_SHARE] = key_share
64
+ inner = TTTLS13::Message::ClientHello.new(
65
+ cipher_suites: TTTLS13::CipherSuites.new(
66
+ [
67
+ TTTLS13::CipherSuite::TLS_AES_256_GCM_SHA384,
68
+ TTTLS13::CipherSuite::TLS_CHACHA20_POLY1305_SHA256,
69
+ TTTLS13::CipherSuite::TLS_AES_128_GCM_SHA256
70
+ ]
71
+ ),
72
+ extensions: exs.merge(
73
+ TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO => inner_ech
74
+ )
75
+ )
76
+ @stack << inner
77
+
78
+ ch1 = TTTLS13::Ech.new_greased_ch(inner, TTTLS13::Ech.new_grease_ech)
79
+ conn.send_record(
80
+ TTTLS13::Message::Record.new(
81
+ type: TTTLS13::Message::ContentType::HANDSHAKE,
82
+ messages: [ch1],
83
+ cipher: TTTLS13::Cryptograph::Passer.new
84
+ )
85
+ )
86
+ @stack << ch1
87
+
88
+ # receive HelloRetryRequest
89
+ hrr, = conn.recv_message(TTTLS13::Cryptograph::Passer.new)
90
+ @stack << hrr
91
+ raise Error::BeforeTargetSituationError, 'did not send expected handshake message: HelloRetryRequest' \
92
+ unless hrr.is_a?(TTTLS13::Message::ServerHello) && hrr.hrr?
93
+ raise Error::BeforeTargetSituationError, 'HelloRetryRequest did not include "key_share" extension' \
94
+ unless hrr.extensions.include?(TTTLS13::Message::ExtensionType::KEY_SHARE)
95
+
96
+ # send 2nd ClientHello; the greased ech is copied from 1st ClientHello
97
+ new_exs = TLS13Client.gen_newch_extensions(ch1, hrr)
98
+ group = hrr.extensions[TTTLS13::Message::ExtensionType::KEY_SHARE].key_share_entry.first.group
99
+ key_share, shared_secret = TTTLS13::Message::Extension::KeyShare.gen_ch_key_share([group])
100
+ new_exs[TTTLS13::Message::ExtensionType::KEY_SHARE] = key_share
101
+ ch = TTTLS13::Message::ClientHello.new(
102
+ legacy_version: ch1.legacy_version,
103
+ random: ch1.random,
104
+ legacy_session_id: ch1.legacy_session_id,
105
+ cipher_suites: ch1.cipher_suites,
106
+ legacy_compression_methods: ch1.legacy_compression_methods,
107
+ extensions: new_exs
108
+ )
109
+ conn.send_record(
110
+ TTTLS13::Message::Record.new(
111
+ type: TTTLS13::Message::ContentType::HANDSHAKE,
112
+ messages: [ch],
113
+ cipher: TTTLS13::Cryptograph::Passer.new
114
+ )
115
+ )
116
+ @stack << ch
117
+
118
+ # receive ServerHello
119
+ recv = conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
120
+ raise Error::BeforeTargetSituationError, 'not received ServerHello' \
121
+ unless recv.is_a?(TTTLS13::Message::ServerHello) && !recv.hrr?
122
+
123
+ # receive EncryptedExtensions
124
+ sh = recv
125
+ transcript = TTTLS13::Transcript.new
126
+ transcript[TTTLS13::CH1] = [ch1, ch1.serialize]
127
+ transcript[TTTLS13::HRR] = [hrr, hrr.serialize]
128
+ transcript[TTTLS13::CH] = [ch, ch.serialize]
129
+ transcript[TTTLS13::SH] = [sh, sh.serialize]
130
+ kse = sh.extensions[TTTLS13::Message::ExtensionType::KEY_SHARE].key_share_entry.first
131
+ key_schedule = TTTLS13::KeySchedule.new(
132
+ psk: nil,
133
+ shared_secret: shared_secret.build(kse.group, kse.key_exchange),
134
+ cipher_suite: sh.cipher_suite,
135
+ transcript:
136
+ )
137
+ hs_rcipher = TTTLS13::Endpoint.gen_cipher(
138
+ sh.cipher_suite,
139
+ key_schedule.server_handshake_write_key,
140
+ key_schedule.server_handshake_write_iv
141
+ )
142
+ recv = conn.recv_message_skipping_ccs(hs_rcipher, @stack)
143
+ raise Error::BeforeTargetSituationError, 'not received EncryptedExtensions' \
144
+ unless recv.is_a?(TTTLS13::Message::EncryptedExtensions)
145
+
146
+ recv
147
+ end
148
+ # rubocop: enable Metrics/AbcSize
149
+ # rubocop: enable Metrics/MethodLength
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,109 @@
1
+ module EchSpec
2
+ module Spec
3
+ class Spec7_2_1 < WithSocket
4
+ # Upon receipt of an "encrypted_client_hello" extension of type inner
5
+ # in a ClientHello, if the backend server negotiates TLS 1.3 or higher,
6
+ # then it MUST confirm ECH acceptance to the client by computing its
7
+ # ServerHello as described here.
8
+ #
9
+ # https://datatracker.ietf.org/doc/html/rfc9849#section-7.2-1
10
+
11
+ # @return [EchSpec::SpecGroup]
12
+ def self.spec_group
13
+ SpecGroup.new(
14
+ '7.2-1',
15
+ [
16
+ SpecCase.new(
17
+ 'MUST confirm ECH acceptance in ServerHello, if it confirmed ECH acceptance in HelloRetryRequest.',
18
+ method(:validate_sh_confirmation_after_hrr)
19
+ )
20
+ ]
21
+ )
22
+ end
23
+
24
+ # @param hostname [String]
25
+ # @param port [Integer]
26
+ # @param ech_config [ECHConfig]
27
+ #
28
+ # @return [EchSpec::Ok | Err]
29
+ def self.validate_sh_confirmation_after_hrr(hostname, port, ech_config)
30
+ Spec7_2_1.new.do_validate_sh_confirmation_after_hrr(hostname, port, ech_config)
31
+ end
32
+
33
+ # @param hostname [String]
34
+ # @param port [Integer]
35
+ # @param ech_config [ECHConfig]
36
+ #
37
+ # @return [EchSpec::Ok | Err]
38
+ def do_validate_sh_confirmation_after_hrr(hostname, port, ech_config)
39
+ with_socket(hostname, port) do |socket|
40
+ recv, confirmation = send_2nd_ch_after_hrr(socket, hostname, ech_config)
41
+ return Err.new('did not send expected handshake message: ServerHello', message_stack) \
42
+ unless recv.is_a?(TTTLS13::Message::ServerHello) && !recv.hrr?
43
+
44
+ validate_sh_confirmation(confirmation, recv)
45
+ end
46
+ end
47
+
48
+ # @param expected [String] accept_confirmation
49
+ # @param sh [TTTLS13::Message::ServerHello]
50
+ #
51
+ # @return [EchSpec::Ok | Err]
52
+ def validate_sh_confirmation(expected, sh)
53
+ return Ok.new(nil) if sh.random[-8..] == expected
54
+
55
+ Err.new('the last 8 bytes of ServerHello.random did not match accept_confirmation, although HelloRetryRequest confirmed ECH acceptance', message_stack)
56
+ end
57
+
58
+ # rubocop: disable Metrics/AbcSize
59
+ # rubocop: disable Metrics/MethodLength
60
+ def send_2nd_ch_after_hrr(socket, hostname, ech_config)
61
+ conn, inner1, _ch1, hrr, ech_state = TLS13Client.recv_hrr(socket, hostname, ech_config, @stack)
62
+ transcript = TTTLS13::Transcript.new
63
+ transcript[TTTLS13::CH1] = [inner1, inner1.serialize]
64
+ transcript[TTTLS13::HRR] = [hrr, hrr.serialize]
65
+ # shared_secret is not used to compute (hrr_)accept_confirmation
66
+ key_schedule = TTTLS13::KeySchedule.new(
67
+ shared_secret: nil,
68
+ cipher_suite: hrr.cipher_suite,
69
+ transcript:
70
+ )
71
+ ex = hrr.extensions[TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO]
72
+ raise Error::BeforeTargetSituationError, 'HelloRetryRequest did not confirm ECH acceptance' \
73
+ if ex.nil? || ex.confirmation != key_schedule.hrr_accept_confirmation
74
+
75
+ # send 2nd ClientHello; ClientHelloInner2 is based on ClientHelloInner1,
76
+ # and its "encrypted_client_hello" extension of type inner is left
77
+ # unmodified.
78
+ inner2 = TTTLS13::Message::ClientHello.new(
79
+ legacy_version: inner1.legacy_version,
80
+ random: inner1.random,
81
+ legacy_session_id: inner1.legacy_session_id,
82
+ cipher_suites: inner1.cipher_suites,
83
+ legacy_compression_methods: inner1.legacy_compression_methods,
84
+ extensions: TLS13Client.gen_newch_extensions(inner1, hrr)
85
+ )
86
+ ch, inner2 = TTTLS13::Ech.offer_new_ech(inner2, ech_state)
87
+ conn.send_record(
88
+ TTTLS13::Message::Record.new(
89
+ type: TTTLS13::Message::ContentType::HANDSHAKE,
90
+ messages: [ch],
91
+ cipher: TTTLS13::Cryptograph::Passer.new
92
+ )
93
+ )
94
+ @stack << inner2
95
+ @stack << ch
96
+
97
+ recv = conn.recv_message_skipping_ccs(TTTLS13::Cryptograph::Passer.new, @stack)
98
+ return [recv, nil] unless recv.is_a?(TTTLS13::Message::ServerHello) && !recv.hrr?
99
+
100
+ transcript[TTTLS13::CH] = [inner2, inner2.serialize]
101
+ sh = recv
102
+ transcript[TTTLS13::SH] = [sh, sh.serialize]
103
+ [recv, key_schedule.accept_confirmation]
104
+ end
105
+ # rubocop: enable Metrics/AbcSize
106
+ # rubocop: enable Metrics/MethodLength
107
+ end
108
+ end
109
+ end
@@ -77,8 +77,8 @@ module EchSpec
77
77
  return Err.new("HTTPS resource record for #{hostname} does NOT have ech SvcParams.", nil) if rr.params[ech].nil?
78
78
 
79
79
  octet = rr.params[ech].value
80
- Err.new('Failed to parse ECHConfig on HTTPS resource record.', nil) \
81
- unless octet.length == octet.slice(0, 2).unpack1('n') + 2
80
+ return Err.new('Failed to parse ECHConfig on HTTPS resource record.', nil) \
81
+ unless octet.length >= 2 && octet.length == octet.slice(0, 2).unpack1('n') + 2
82
82
 
83
83
  Ok.new(ECHConfig.decode_vectors(octet.slice(2..)))
84
84
  end
@@ -135,6 +135,8 @@ module EchSpec
135
135
  # @param body [String]
136
136
  #
137
137
  # @return [EchSpec::Ok | Err]
138
+ # rubocop: disable Metrics/CyclomaticComplexity
139
+ # rubocop: disable Metrics/PerceivedComplexity
138
140
  def parse_origin_svcb(body)
139
141
  h = JSON.parse(body)
140
142
  echs = h['endpoints']&.filter_map { |e| e.dig('params', 'ech') }
@@ -143,6 +145,9 @@ module EchSpec
143
145
  # if parsing any one of the ech values fails, return Err
144
146
  ech_configs = echs.flat_map do |ech|
145
147
  octet = Base64.decode64(ech)
148
+ return Err.new('Failed to parse ECHConfig on the origin-svcb well-known resource.', nil) \
149
+ unless octet.length >= 2 && octet.length == octet.slice(0, 2).unpack1('n') + 2
150
+
146
151
  ECHConfig.decode_vectors(octet.slice(2..))
147
152
  end
148
153
 
@@ -150,6 +155,8 @@ module EchSpec
150
155
  rescue StandardError => e
151
156
  Err.new(e.message, nil)
152
157
  end
158
+ # rubocop: enable Metrics/CyclomaticComplexity
159
+ # rubocop: enable Metrics/PerceivedComplexity
153
160
 
154
161
  # @param pem [String]
155
162
  #
@@ -160,6 +167,9 @@ module EchSpec
160
167
  .first
161
168
  .gsub("\n", '')
162
169
  b = Base64.decode64(s)
170
+ return Err.new('Failed to parse ECHConfig on the PEM file.', nil) \
171
+ unless b.length >= 2 && b.length == b.slice(0, 2).unpack1('n') + 2
172
+
163
173
  ech_configs = ECHConfig.decode_vectors(b.slice(2..))
164
174
  Ok.new(ech_configs)
165
175
  rescue StandardError
data/lib/echspec/spec.rb CHANGED
@@ -78,6 +78,26 @@ module EchSpec
78
78
  def message_stack
79
79
  @stack.marshal
80
80
  end
81
+
82
+ # @param ee [TTTLS13::Message::EncryptedExtensions]
83
+ #
84
+ # @return [EchSpec::Ok | Err]
85
+ def validate_ee_ech(ee)
86
+ # Extensions#[] returns nil for UnknownExtension, so use super_fetch.
87
+ ex = ee.extensions.super_fetch(TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO, nil)
88
+ case ex
89
+ in TTTLS13::Message::Extension::ECHEncryptedExtensions
90
+ return Err.new('EncryptedExtensions "encrypted_client_hello" extension did not have "retry_configs"', message_stack) \
91
+ if ex.retry_configs.nil? || ex.retry_configs.empty?
92
+
93
+ Ok.new(nil)
94
+ in nil
95
+ Err.new('EncryptedExtensions did not include "encrypted_client_hello" extension', message_stack)
96
+ in TTTLS13::Message::Extension::UnknownExtension
97
+ # tttls1.3 deserializes the extension whose length field mismatches as UnknownExtension
98
+ Err.new('EncryptedExtensions "encrypted_client_hello" extension could not be decoded', message_stack)
99
+ end
100
+ end
81
101
  end
82
102
  end
83
103
  end
@@ -176,10 +196,10 @@ module EchSpec
176
196
 
177
197
  def spec_groups
178
198
  # https://datatracker.ietf.org/doc/html/rfc9849#section-5
179
- groups = [Spec5_1_9, Spec5_1_10]
199
+ groups = [Spec5_11, Spec5_1_9, Spec5_1_10]
180
200
 
181
201
  # https://datatracker.ietf.org/doc/html/rfc9849#section-7
182
- groups += [Spec7_5, Spec7_1_11, Spec7_1_14_2_1, Spec7_1_1_2, Spec7_1_1_5]
202
+ groups += [Spec7_3, Spec7_1_11, Spec7_1_14_2_1, Spec7_1_1_2, Spec7_1_1_5, Spec7_1_1_7, Spec7_2_1]
183
203
 
184
204
  groups.map(&:spec_group)
185
205
  end
@@ -26,6 +26,20 @@ module EchSpec
26
26
 
27
27
  [message, orig_msg]
28
28
  end
29
+
30
+ # @param cipher [TTTLS13::Cryptograph::$Object]
31
+ # @param stack [EchSpec::Log::MessageStack]
32
+ #
33
+ # @return [TTTLS13::Message::$Object]
34
+ def recv_message_skipping_ccs(cipher, stack)
35
+ recv, = recv_message(cipher)
36
+ stack << recv
37
+ if recv.is_a?(TTTLS13::Message::ChangeCipherSpec)
38
+ recv, = recv_message(cipher)
39
+ stack << recv
40
+ end
41
+ recv
42
+ end
29
43
  end
30
44
 
31
45
  class << self
@@ -98,16 +112,14 @@ module EchSpec
98
112
 
99
113
  # @param socket [TCPSocket]
100
114
  # @param hostname [String]
101
- # @param stack [EchSpec::Log::MessageStack | nil]
115
+ # @param stack [EchSpec::Log::MessageStack]
102
116
  #
103
117
  # @raise [EchSpec::Error::BeforeTargetSituationError]
104
118
  #
105
119
  # @return [TTTLS13::Message::EncryptedExtensions]
106
120
  # rubocop: disable Metrics/AbcSize
107
- # rubocop: disable Metrics/CyclomaticComplexity
108
121
  # rubocop: disable Metrics/MethodLength
109
- # rubocop: disable Metrics/PerceivedComplexity
110
- def send_ch_with_greased_ech(socket, hostname, stack = nil)
122
+ def send_ch_with_greased_ech(socket, hostname, stack)
111
123
  conn = TLS13Client::Connection.new(socket, :client)
112
124
  inner_ech = TTTLS13::Message::Extension::ECHClientHello.new_inner
113
125
  exs, shared_secret = TLS13Client.gen_ch_extensions(hostname)
@@ -123,7 +135,7 @@ module EchSpec
123
135
  TTTLS13::Message::ExtensionType::ENCRYPTED_CLIENT_HELLO => inner_ech
124
136
  )
125
137
  )
126
- stack << inner if stack
138
+ stack << inner
127
139
 
128
140
  ch = TTTLS13::Ech.new_greased_ch(inner, TTTLS13::Ech.new_grease_ech)
129
141
  conn.send_record(
@@ -133,10 +145,10 @@ module EchSpec
133
145
  cipher: TTTLS13::Cryptograph::Passer.new
134
146
  )
135
147
  )
136
- stack << ch if stack
148
+ stack << ch
137
149
 
138
150
  recv, = conn.recv_message(TTTLS13::Cryptograph::Passer.new)
139
- stack << recv if stack
151
+ stack << recv
140
152
  raise Error::BeforeTargetSituationError, 'not received ServerHello' \
141
153
  unless recv.is_a?(TTTLS13::Message::ServerHello) && !recv.hrr?
142
154
 
@@ -157,23 +169,15 @@ module EchSpec
157
169
  key_schedule.server_handshake_write_key,
158
170
  key_schedule.server_handshake_write_iv
159
171
  )
160
- recv, = conn.recv_message(hs_rcipher)
161
- stack << recv if stack
162
- if recv.is_a?(TTTLS13::Message::ChangeCipherSpec)
163
- recv, = conn.recv_message(hs_rcipher)
164
- stack << recv if stack
165
- end
166
-
172
+ recv = conn.recv_message_skipping_ccs(hs_rcipher, stack)
167
173
  raise Error::BeforeTargetSituationError, 'not received EncryptedExtensions' \
168
174
  unless recv.is_a?(TTTLS13::Message::EncryptedExtensions)
169
175
 
170
176
  recv
171
177
  end
178
+
172
179
  # rubocop: enable Metrics/AbcSize
173
- # rubocop: enable Metrics/CyclomaticComplexity
174
180
  # rubocop: enable Metrics/MethodLength
175
- # rubocop: enable Metrics/PerceivedComplexity
176
-
177
181
  # @param conf [ECHConfig::ECHConfigContents::HpkeKeyConfig]
178
182
  #
179
183
  # @return [Boolean]
@@ -1,3 +1,3 @@
1
1
  module EchSpec
2
- VERSION = '0.0.7'.freeze
2
+ VERSION = '0.0.9'.freeze
3
3
  end
data/lib/echspec.rb CHANGED
@@ -12,6 +12,7 @@ require_relative 'echspec/utils'
12
12
  require_relative 'echspec/log'
13
13
  require_relative 'echspec/error'
14
14
  require_relative 'echspec/result'
15
+ require_relative 'echspec/ech_config'
15
16
  require_relative 'echspec/tls13_client'
16
17
  require_relative 'echspec/spec_case'
17
18
  require_relative 'echspec/spec_group'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: echspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - thekuwayama
@@ -106,17 +106,21 @@ files:
106
106
  - lib/echspec/cli/grease.rb
107
107
  - lib/echspec/cli/resolve.rb
108
108
  - lib/echspec/cli/run.rb
109
+ - lib/echspec/ech_config.rb
109
110
  - lib/echspec/error.rb
110
111
  - lib/echspec/log.rb
111
112
  - lib/echspec/result.rb
112
113
  - lib/echspec/spec.rb
114
+ - lib/echspec/spec/5-11.rb
113
115
  - lib/echspec/spec/5.1-10.rb
114
116
  - lib/echspec/spec/5.1-9.rb
115
- - lib/echspec/spec/7-5.rb
117
+ - lib/echspec/spec/7-3.rb
116
118
  - lib/echspec/spec/7.1-11.rb
117
119
  - lib/echspec/spec/7.1-14.2.1.rb
118
120
  - lib/echspec/spec/7.1.1-2.rb
119
121
  - lib/echspec/spec/7.1.1-5.rb
122
+ - lib/echspec/spec/7.1.1-7.rb
123
+ - lib/echspec/spec/7.2-1.rb
120
124
  - lib/echspec/spec/9.rb
121
125
  - lib/echspec/spec_case.rb
122
126
  - lib/echspec/spec_group.rb
@@ -141,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
145
  - !ruby/object:Gem::Version
142
146
  version: '0'
143
147
  requirements: []
144
- rubygems_version: 4.0.6
148
+ rubygems_version: 4.0.20
145
149
  specification_version: 4
146
150
  summary: A conformance testing tool for ECH implementation
147
151
  test_files: []