Skip to content

Commit

Permalink
Merge pull request #70 from thekuwayama/ech__12
Browse files Browse the repository at this point in the history
[ech] 12. feat: ECHHelloRetryRequest
  • Loading branch information
thekuwayama authored Dec 18, 2023
2 parents beda780 + bf1903e commit 853155a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
9 changes: 4 additions & 5 deletions lib/tttls1.3/message/extension/alpn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ class Alpn
# https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
def initialize(protocol_name_list)
@extension_type \
= ExtensionType::APPLICATION_LAYER_PROTOCOL_NEGOTIATION
= ExtensionType::APPLICATION_LAYER_PROTOCOL_NEGOTIATION
@protocol_name_list = protocol_name_list || []
raise Error::ErrorAlerts, :internal_error \
if @protocol_name_list.empty?
end

# @return [String]
def serialize
binary = @protocol_name_list
.map(&:prefix_uint8_length)
.join
.prefix_uint16_length
binary = @protocol_name_list.map(&:prefix_uint8_length)
.join
.prefix_uint16_length

@extension_type + binary.prefix_uint16_length
end
Expand Down
33 changes: 33 additions & 0 deletions lib/tttls1.3/message/extension/ech.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def initialize(retry_configs)
@retry_configs = retry_configs
end

# @return [String]
def serialize
@extension_type + @retry_configs.map(&:encode)
.join
Expand All @@ -203,6 +204,38 @@ def self.deserialize(binary)
)
end
end

# NOTE:
# struct {
# opaque confirmation[8];
# } ECHHelloRetryRequest;
class ECHHelloRetryRequest
attr_accessor :extension_type
attr_accessor :confirmation

# @param confirmation [String]
def initialize(confirmation)
@extension_type = ExtensionType::ENCRYPTED_CLIENT_HELLO
@confirmation = confirmation
end

# @return [String]
def serialize
@extension_type + @confirmation.prefix_uint16_length
end

# @param binary [String]
#
# @raise [TTTLS13::Error::ErrorAlerts]
#
# @return [TTTLS13::Message::Extensions::ECHHelloRetryRequest]
def self.deserialize(binary)
raise Error::ErrorAlerts, :internal_error \
if binary.nil? || binary.length != 8

ECHHelloRetryRequest.new(binary)
end
end
end
end
end
9 changes: 5 additions & 4 deletions lib/tttls1.3/message/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@ def deserialize_extension(binary, extension_type, msg_type)
when ExtensionType::KEY_SHARE
Extension::KeyShare.deserialize(binary, msg_type)
when ExtensionType::ENCRYPTED_CLIENT_HELLO
if msg_type == HandshakeType::CLIENT_HELLO
case msg_type
when HandshakeType::CLIENT_HELLO
Extension::ECHClientHello.deserialize(binary)
elsif msg_type == HandshakeType::ENCRYPTED_EXTENSIONS
when HandshakeType::ENCRYPTED_EXTENSIONS
Extension::ECHEncryptedExtensions.deserialize(binary)
# elsif msg_type == HandshakeType::SERVER_HELLO
# Extension::ECHHelloRetryRequest.deserialize(binary)
when HandshakeType::SERVER_HELLO
Extension::ECHHelloRetryRequest.deserialize(binary)
else
Extension::UnknownExtension.deserialize(binary, extension_type)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/ech_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@
expect(extension.retry_configs.first.is_a?(ECHConfig)).to be_truthy
end
end

context 'valid ECHHelloRetryRequest binary' do
let(:extension) do
ECHHelloRetryRequest.deserialize(TESTBINARY_ECH_HRR)
end

it 'should generate valid object' do
expect(extension.extension_type)
.to eq ExtensionType::ENCRYPTED_CLIENT_HELLO
expect(extension.confirmation).to eq "\x00" * 8
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ def read(len = @buffer.length)
09 6c 6f 63 61 6c 68 6f 73 74 00 00
BIN

TESTBINARY_ECH_HRR = <<BIN.split.map(&:hex).map(&:chr).join
00 00 00 00 00 00 00 00
BIN

# https://tools.ietf.org/html/rfc8448#section-3
# 3. Simple 1-RTT Handshake
TESTBINARY_CLIENT_HELLO = <<BIN.split.map(&:hex).map(&:chr).join
Expand Down

0 comments on commit 853155a

Please sign in to comment.