raiha 0.1.0 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE +202 -0
- data/README.md +94 -22
- data/docs/configuration.md +227 -0
- data/docs/development.md +99 -0
- data/docs/fiber-scheduler.md +591 -0
- data/docs/getting-started.md +190 -0
- data/docs/http3-client.md +303 -0
- data/docs/http3-server.md +301 -0
- data/docs/platforms.md +98 -0
- data/docs/qlog.md +86 -0
- data/docs/quic.md +268 -0
- data/docs/threads.md +127 -0
- data/examples/_certificate.rb +55 -0
- data/examples/high_level_loopback.rb +59 -0
- data/examples/http3_get_public.rb +68 -0
- data/examples/in_memory_handshake.rb +38 -0
- data/examples/qlog_output.rb +58 -0
- data/examples/qlog_roundtrips.rb +66 -0
- data/examples/quic_gem_loopback.rb +62 -0
- data/examples/stream_echo.rb +68 -0
- data/examples/udp_handshake.rb +76 -0
- data/lib/raiha/http3/action.rb +21 -0
- data/lib/raiha/http3/capsule.rb +86 -0
- data/lib/raiha/http3/connection.rb +983 -0
- data/lib/raiha/http3/error.rb +24 -0
- data/lib/raiha/http3/error_code.rb +27 -0
- data/lib/raiha/http3/event.rb +63 -0
- data/lib/raiha/http3/frame.rb +148 -0
- data/lib/raiha/http3/frame_reader.rb +114 -0
- data/lib/raiha/http3/message.rb +137 -0
- data/lib/raiha/http3/output.rb +61 -0
- data/lib/raiha/http3/priority.rb +52 -0
- data/lib/raiha/http3/qlog.rb +145 -0
- data/lib/raiha/http3/qpack/decoder.rb +302 -0
- data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
- data/lib/raiha/http3/qpack/encoder.rb +94 -0
- data/lib/raiha/http3/qpack/error.rb +39 -0
- data/lib/raiha/http3/qpack/error_code.rb +13 -0
- data/lib/raiha/http3/qpack/field.rb +15 -0
- data/lib/raiha/http3/qpack/huffman.rb +397 -0
- data/lib/raiha/http3/qpack/reader.rb +82 -0
- data/lib/raiha/http3/qpack/result.rb +26 -0
- data/lib/raiha/http3/qpack/static_table.rb +139 -0
- data/lib/raiha/http3/qpack/writer.rb +44 -0
- data/lib/raiha/http3/qpack.rb +26 -0
- data/lib/raiha/http3/receiving.rb +46 -0
- data/lib/raiha/http3/sending.rb +23 -0
- data/lib/raiha/http3/session.rb +31 -0
- data/lib/raiha/http3/settings.rb +81 -0
- data/lib/raiha/http3/structured_field.rb +120 -0
- data/lib/raiha/http3/tracer.rb +26 -0
- data/lib/raiha/http3/varint.rb +45 -0
- data/lib/raiha/http3.rb +19 -0
- data/lib/raiha/http3_binding.rb +139 -0
- data/lib/raiha/namespace.rb +11 -0
- data/lib/raiha/quic/config.rb +229 -0
- data/lib/raiha/quic/congestion/algorithms.rb +17 -0
- data/lib/raiha/quic/congestion/cubic.rb +258 -0
- data/lib/raiha/quic/congestion/new_reno.rb +161 -0
- data/lib/raiha/quic/congestion/pacing.rb +21 -0
- data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
- data/lib/raiha/quic/congestion/startup.rb +13 -0
- data/lib/raiha/quic/congestion.rb +15 -0
- data/lib/raiha/quic/connection.rb +2022 -0
- data/lib/raiha/quic/connection_ids.rb +62 -0
- data/lib/raiha/quic/crypto/aead.rb +68 -0
- data/lib/raiha/quic/crypto/header_protection.rb +42 -0
- data/lib/raiha/quic/crypto/initial.rb +23 -0
- data/lib/raiha/quic/crypto/keys.rb +32 -0
- data/lib/raiha/quic/crypto/mac.rb +15 -0
- data/lib/raiha/quic/crypto/random.rb +13 -0
- data/lib/raiha/quic/crypto.rb +18 -0
- data/lib/raiha/quic/crypto_stream.rb +161 -0
- data/lib/raiha/quic/datagrams.rb +91 -0
- data/lib/raiha/quic/endpoint.rb +785 -0
- data/lib/raiha/quic/error.rb +61 -0
- data/lib/raiha/quic/error_code.rb +34 -0
- data/lib/raiha/quic/event.rb +59 -0
- data/lib/raiha/quic/issued_connection_ids.rb +137 -0
- data/lib/raiha/quic/kept_packets.rb +41 -0
- data/lib/raiha/quic/mtu_search.rb +75 -0
- data/lib/raiha/quic/negotiation.rb +143 -0
- data/lib/raiha/quic/new_token.rb +55 -0
- data/lib/raiha/quic/pacer.rb +52 -0
- data/lib/raiha/quic/packet_protection.rb +310 -0
- data/lib/raiha/quic/path.rb +473 -0
- data/lib/raiha/quic/peer_connection_ids.rb +160 -0
- data/lib/raiha/quic/qlog.rb +260 -0
- data/lib/raiha/quic/ranges.rb +32 -0
- data/lib/raiha/quic/recovery/duty.rb +102 -0
- data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
- data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
- data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
- data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
- data/lib/raiha/quic/recovery.rb +641 -0
- data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
- data/lib/raiha/quic/retry_token.rb +54 -0
- data/lib/raiha/quic/route.rb +37 -0
- data/lib/raiha/quic/scheduler.rb +112 -0
- data/lib/raiha/quic/session.rb +47 -0
- data/lib/raiha/quic/stateless_reset.rb +38 -0
- data/lib/raiha/quic/stats.rb +24 -0
- data/lib/raiha/quic/streams/receive_window.rb +119 -0
- data/lib/raiha/quic/streams/receiver.rb +217 -0
- data/lib/raiha/quic/streams/sender.rb +200 -0
- data/lib/raiha/quic/streams.rb +687 -0
- data/lib/raiha/quic/test/bottleneck.rb +55 -0
- data/lib/raiha/quic/test/congestion_contract.rb +106 -0
- data/lib/raiha/quic/test/network.rb +244 -0
- data/lib/raiha/quic/test/random_loss.rb +34 -0
- data/lib/raiha/quic/test/reorder.rb +51 -0
- data/lib/raiha/quic/test/server.rb +54 -0
- data/lib/raiha/quic/test/vectors.rb +337 -0
- data/lib/raiha/quic/test/virtual_clock.rb +21 -0
- data/lib/raiha/quic/test.rb +17 -0
- data/lib/raiha/quic/tls_binding.rb +179 -0
- data/lib/raiha/quic/token.rb +28 -0
- data/lib/raiha/quic/tracer.rb +127 -0
- data/lib/raiha/quic/transmit.rb +14 -0
- data/lib/raiha/quic/transport_parameters.rb +244 -0
- data/lib/raiha/quic/version.rb +63 -0
- data/lib/raiha/quic/wire/codec.rb +136 -0
- data/lib/raiha/quic/wire/frame.rb +521 -0
- data/lib/raiha/quic/wire/header.rb +90 -0
- data/lib/raiha/quic/wire/packet.rb +130 -0
- data/lib/raiha/quic/wire/packet_number.rb +47 -0
- data/lib/raiha/quic/wire/retry.rb +62 -0
- data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
- data/lib/raiha/quic/wire.rb +9 -0
- data/lib/raiha/quic.rb +46 -0
- data/lib/raiha/runtime/addresses.rb +35 -0
- data/lib/raiha/runtime/admission.rb +37 -0
- data/lib/raiha/runtime/client.rb +190 -0
- data/lib/raiha/runtime/driver.rb +165 -0
- data/lib/raiha/runtime/error.rb +16 -0
- data/lib/raiha/runtime/http3.rb +363 -0
- data/lib/raiha/runtime/http3_fields.rb +38 -0
- data/lib/raiha/runtime/http3_server.rb +416 -0
- data/lib/raiha/runtime/kept.rb +36 -0
- data/lib/raiha/runtime/qlog.rb +204 -0
- data/lib/raiha/runtime/rack.rb +180 -0
- data/lib/raiha/runtime/server.rb +334 -0
- data/lib/raiha/runtime/session.rb +447 -0
- data/lib/raiha/runtime/threaded/client.rb +161 -0
- data/lib/raiha/runtime/threaded/driver.rb +230 -0
- data/lib/raiha/runtime/threaded/http3.rb +287 -0
- data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
- data/lib/raiha/runtime/threaded/server.rb +290 -0
- data/lib/raiha/runtime/threaded/session.rb +604 -0
- data/lib/raiha/runtime/threaded.rb +45 -0
- data/lib/raiha/runtime/udp.rb +410 -0
- data/lib/raiha/runtime/worker.rb +138 -0
- data/lib/raiha/runtime/workers.rb +248 -0
- data/lib/raiha/runtime.rb +37 -0
- data/lib/raiha/tls/anti_replay.rb +139 -0
- data/lib/raiha/tls/cipher_suite.rb +62 -0
- data/lib/raiha/tls/codec/error.rb +43 -0
- data/lib/raiha/tls/codec/reader.rb +95 -0
- data/lib/raiha/tls/codec/writer.rb +81 -0
- data/lib/raiha/tls/codec.rb +5 -0
- data/lib/raiha/tls/config.rb +318 -0
- data/lib/raiha/tls/crypto/aead.rb +64 -0
- data/lib/raiha/tls/crypto/certificate.rb +28 -0
- data/lib/raiha/tls/crypto/digest.rb +30 -0
- data/lib/raiha/tls/crypto/ecdh.rb +74 -0
- data/lib/raiha/tls/crypto/hash.rb +22 -0
- data/lib/raiha/tls/crypto/hkdf.rb +28 -0
- data/lib/raiha/tls/crypto/signature.rb +87 -0
- data/lib/raiha/tls/crypto.rb +19 -0
- data/lib/raiha/tls/error.rb +36 -0
- data/lib/raiha/tls/event.rb +45 -0
- data/lib/raiha/tls/extension/alpn.rb +35 -0
- data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
- data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
- data/lib/raiha/tls/extension/cookie.rb +27 -0
- data/lib/raiha/tls/extension/early_data.rb +54 -0
- data/lib/raiha/tls/extension/key_share.rb +90 -0
- data/lib/raiha/tls/extension/list.rb +49 -0
- data/lib/raiha/tls/extension/opaque.rb +21 -0
- data/lib/raiha/tls/extension/placement.rb +55 -0
- data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
- data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
- data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
- data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
- data/lib/raiha/tls/extension/registry.rb +50 -0
- data/lib/raiha/tls/extension/server_name.rb +63 -0
- data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
- data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
- data/lib/raiha/tls/extension/supported_groups.rb +32 -0
- data/lib/raiha/tls/extension/supported_versions.rb +53 -0
- data/lib/raiha/tls/extension.rb +32 -0
- data/lib/raiha/tls/handshake/acceptor.rb +164 -0
- data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
- data/lib/raiha/tls/handshake/certificate.rb +56 -0
- data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
- data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
- data/lib/raiha/tls/handshake/client_hello.rb +208 -0
- data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
- data/lib/raiha/tls/handshake/client_side.rb +578 -0
- data/lib/raiha/tls/handshake/conductor.rb +390 -0
- data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
- data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
- data/lib/raiha/tls/handshake/finished.rb +36 -0
- data/lib/raiha/tls/handshake/key_update.rb +38 -0
- data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
- data/lib/raiha/tls/handshake/machine.rb +238 -0
- data/lib/raiha/tls/handshake/message.rb +25 -0
- data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
- data/lib/raiha/tls/handshake/reassembler.rb +88 -0
- data/lib/raiha/tls/handshake/server_hello.rb +98 -0
- data/lib/raiha/tls/handshake/server_side.rb +672 -0
- data/lib/raiha/tls/handshake/violation.rb +12 -0
- data/lib/raiha/tls/handshake.rb +62 -0
- data/lib/raiha/tls/iana.rb +113 -0
- data/lib/raiha/tls/key_exchange.rb +58 -0
- data/lib/raiha/tls/key_schedule.rb +269 -0
- data/lib/raiha/tls/keylog.rb +40 -0
- data/lib/raiha/tls/keyring.rb +180 -0
- data/lib/raiha/tls/masked_inspect.rb +33 -0
- data/lib/raiha/tls/output.rb +32 -0
- data/lib/raiha/tls/profile.rb +82 -0
- data/lib/raiha/tls/record/layer.rb +228 -0
- data/lib/raiha/tls/record/protection.rb +67 -0
- data/lib/raiha/tls/record.rb +11 -0
- data/lib/raiha/tls/server_name.rb +42 -0
- data/lib/raiha/tls/session.rb +106 -0
- data/lib/raiha/tls/signature_scheme.rb +35 -0
- data/lib/raiha/tls/tcp/acceptor.rb +121 -0
- data/lib/raiha/tls/tcp/connection.rb +371 -0
- data/lib/raiha/tls/tcp/event.rb +15 -0
- data/lib/raiha/tls/tcp/socket.rb +161 -0
- data/lib/raiha/tls/tcp.rb +14 -0
- data/lib/raiha/tls/test/fixed_kem.rb +30 -0
- data/lib/raiha/tls/test/fixed_random.rb +18 -0
- data/lib/raiha/tls/test/rfc8448.rb +923 -0
- data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
- data/lib/raiha/tls/test.rb +13 -0
- data/lib/raiha/tls/ticket.rb +184 -0
- data/lib/raiha/tls/ticket_keys.rb +206 -0
- data/lib/raiha/tls/trace.rb +58 -0
- data/lib/raiha/tls/transcript.rb +94 -0
- data/lib/raiha/tls/verifier/default.rb +72 -0
- data/lib/raiha/tls/verifier/host_name.rb +75 -0
- data/lib/raiha/tls/verifier/none.rb +14 -0
- data/lib/raiha/tls/verifier.rb +24 -0
- data/lib/raiha/tls.rb +34 -0
- data/lib/raiha/version.rb +3 -1
- data/lib/raiha.rb +8 -5
- data/sig/patches/array.rbs +9 -0
- data/sig/patches/io_buffer.rbs +13 -0
- data/sig/patches/openssl.rbs +25 -0
- data/sig/patches/socket.rbs +29 -0
- data/sig/patches/string.rbs +26 -0
- data/sig/patches/strscan.rbs +12 -0
- data/sig/raiha/http3/capsule.rbs +32 -0
- data/sig/raiha/http3/connection.rbs +324 -0
- data/sig/raiha/http3/frame.rbs +138 -0
- data/sig/raiha/http3/frame_reader.rbs +39 -0
- data/sig/raiha/http3/message.rbs +23 -0
- data/sig/raiha/http3/priority.rbs +40 -0
- data/sig/raiha/http3/qlog.rbs +54 -0
- data/sig/raiha/http3/qpack.rbs +246 -0
- data/sig/raiha/http3/session.rbs +11 -0
- data/sig/raiha/http3/settings.rbs +26 -0
- data/sig/raiha/http3.rbs +2 -0
- data/sig/raiha/http3_binding.rbs +26 -0
- data/sig/raiha/namespace.rbs +2 -0
- data/sig/raiha/quic/config.rbs +89 -0
- data/sig/raiha/quic/congestion.rbs +213 -0
- data/sig/raiha/quic/connection.rbs +249 -0
- data/sig/raiha/quic/connection_ids.rbs +34 -0
- data/sig/raiha/quic/crypto.rbs +78 -0
- data/sig/raiha/quic/crypto_stream.rbs +34 -0
- data/sig/raiha/quic/datagrams.rbs +32 -0
- data/sig/raiha/quic/endpoint.rbs +179 -0
- data/sig/raiha/quic/frame.rbs +387 -0
- data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
- data/sig/raiha/quic/kept_packets.rbs +25 -0
- data/sig/raiha/quic/mtu_search.rbs +21 -0
- data/sig/raiha/quic/negotiation.rbs +33 -0
- data/sig/raiha/quic/new_token.rbs +14 -0
- data/sig/raiha/quic/pacer.rbs +16 -0
- data/sig/raiha/quic/packet_protection.rbs +63 -0
- data/sig/raiha/quic/path.rbs +106 -0
- data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
- data/sig/raiha/quic/qlog.rbs +74 -0
- data/sig/raiha/quic/ranges.rbs +7 -0
- data/sig/raiha/quic/recovery.rbs +376 -0
- data/sig/raiha/quic/retry_token.rbs +21 -0
- data/sig/raiha/quic/route.rbs +51 -0
- data/sig/raiha/quic/scheduler.rbs +36 -0
- data/sig/raiha/quic/session.rbs +17 -0
- data/sig/raiha/quic/stateless_reset.rbs +10 -0
- data/sig/raiha/quic/streams.rbs +244 -0
- data/sig/raiha/quic/test.rbs +354 -0
- data/sig/raiha/quic/tls_binding.rbs +58 -0
- data/sig/raiha/quic/token.rbs +11 -0
- data/sig/raiha/quic/tracer.rbs +71 -0
- data/sig/raiha/quic/transport_parameters.rbs +113 -0
- data/sig/raiha/quic/values.rbs +200 -0
- data/sig/raiha/quic/version.rbs +30 -0
- data/sig/raiha/quic/wire.rbs +199 -0
- data/sig/raiha/quic.rbs +2 -0
- data/sig/raiha/runtime/threaded.rbs +616 -0
- data/sig/raiha/runtime.rbs +982 -0
- data/sig/raiha/tls/anti_replay.rbs +41 -0
- data/sig/raiha/tls/cipher_suite.rbs +41 -0
- data/sig/raiha/tls/codec.rbs +66 -0
- data/sig/raiha/tls/config.rbs +122 -0
- data/sig/raiha/tls/crypto.rbs +86 -0
- data/sig/raiha/tls/error.rbs +21 -0
- data/sig/raiha/tls/event.rbs +61 -0
- data/sig/raiha/tls/extension.rbs +331 -0
- data/sig/raiha/tls/handshake.rbs +311 -0
- data/sig/raiha/tls/iana.rbs +13 -0
- data/sig/raiha/tls/key_exchange.rbs +33 -0
- data/sig/raiha/tls/key_schedule.rbs +58 -0
- data/sig/raiha/tls/keylog.rbs +20 -0
- data/sig/raiha/tls/keyring.rbs +64 -0
- data/sig/raiha/tls/machine.rbs +429 -0
- data/sig/raiha/tls/masked_inspect.rbs +12 -0
- data/sig/raiha/tls/output.rbs +37 -0
- data/sig/raiha/tls/profile.rbs +26 -0
- data/sig/raiha/tls/record.rbs +79 -0
- data/sig/raiha/tls/server_name.rbs +10 -0
- data/sig/raiha/tls/session.rbs +48 -0
- data/sig/raiha/tls/signature_scheme.rbs +20 -0
- data/sig/raiha/tls/tcp.rbs +134 -0
- data/sig/raiha/tls/test.rbs +143 -0
- data/sig/raiha/tls/ticket.rbs +68 -0
- data/sig/raiha/tls/ticket_keys.rbs +67 -0
- data/sig/raiha/tls/trace.rbs +99 -0
- data/sig/raiha/tls/transcript.rbs +22 -0
- data/sig/raiha/tls/verifier.rbs +54 -0
- data/sig/raiha/tls.rbs +5 -0
- data/sig/raiha.rbs +3 -0
- metadata +355 -57
- data/.gitignore +0 -10
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -22
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/raiha.gemspec +0 -40
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../error"
|
|
4
|
+
require_relative "../event"
|
|
5
|
+
require_relative "../key_schedule"
|
|
6
|
+
require_relative "../keylog"
|
|
7
|
+
require_relative "../output"
|
|
8
|
+
require_relative "../trace"
|
|
9
|
+
require_relative "../transcript"
|
|
10
|
+
require_relative "key_update"
|
|
11
|
+
require_relative "reassembler"
|
|
12
|
+
|
|
13
|
+
module Raiha::TLS
|
|
14
|
+
module Handshake
|
|
15
|
+
# The part of a handshake that does not depend on the role. It feeds the
|
|
16
|
+
# peer's bytes through a Reassembler per epoch, the transcript and the
|
|
17
|
+
# parser to the role's side (ClientSide or ServerSide), and collects what
|
|
18
|
+
# comes out: the Output queue and the Event queue.
|
|
19
|
+
#
|
|
20
|
+
# Each kind of change has exactly one place, which a hygiene test checks:
|
|
21
|
+
#
|
|
22
|
+
# - `transition!` assigns the state;
|
|
23
|
+
# - `change_epochs!` moves the read and write epochs and queues Secrets;
|
|
24
|
+
# - `fail!` builds the Output::Alert;
|
|
25
|
+
# - the transcript is added to only by the methods for receiving a
|
|
26
|
+
# message, sending one, and a ServerHello or HelloRetryRequest.
|
|
27
|
+
#
|
|
28
|
+
# Within one trigger, the Secrets that became usable go out before the
|
|
29
|
+
# Messages; a failure drops the half-built flight and sends only the
|
|
30
|
+
# alert. On failure, `discard!` or an exception from an object the user
|
|
31
|
+
# gave (a Verifier or tracer), every secret is forgotten.
|
|
32
|
+
class Conductor
|
|
33
|
+
CERTIFICATE_MESSAGES = Set[Certificate::MSG_TYPE, CertificateRequest::MSG_TYPE, 25].freeze
|
|
34
|
+
|
|
35
|
+
# The transcript hash before these messages is what their check covers.
|
|
36
|
+
CHECKED_AGAINST_PREVIOUS_TRANSCRIPT = Set[CertificateVerify::MSG_TYPE, Finished::MSG_TYPE].freeze
|
|
37
|
+
# Messages that enter the transcript once they are parsed, not before.
|
|
38
|
+
AFTER_PARSING = Set[ServerHello::MSG_TYPE, ClientHello::MSG_TYPE].freeze
|
|
39
|
+
|
|
40
|
+
attr_reader :config, :keyring, :side, :state, :read_epoch, :write_epoch, :alert, :key_schedule
|
|
41
|
+
|
|
42
|
+
# The side is built by the block, given this Conductor. `substitutions`
|
|
43
|
+
# replace messages we send with given bytes, by message type and in
|
|
44
|
+
# order; only Test::RFC8448 passes them.
|
|
45
|
+
def initialize(config:, keyring:, tracer:, keylog: nil, substitutions: nil)
|
|
46
|
+
@config = config
|
|
47
|
+
@keyring = keyring
|
|
48
|
+
@tracer = tracer
|
|
49
|
+
@keylog = keylog
|
|
50
|
+
@substitutions = substitutions&.transform_values(&:dup)
|
|
51
|
+
@state = nil #: Symbol?
|
|
52
|
+
@read_epoch = :initial #: epoch
|
|
53
|
+
@write_epoch = :initial #: epoch
|
|
54
|
+
@alert = nil #: Symbol?
|
|
55
|
+
# @type var reassemblers: Hash[epoch, Reassembler]
|
|
56
|
+
reassemblers = {}
|
|
57
|
+
@reassemblers = reassemblers
|
|
58
|
+
hashes = config.cipher_suites.map { |name| Config::CIPHER_SUITES_BY_NAME.fetch(name).hash_algorithm }.uniq
|
|
59
|
+
@hashes = hashes
|
|
60
|
+
@transcript = Transcript.new(hashes:)
|
|
61
|
+
# What a trace shows before a hash is selected.
|
|
62
|
+
@trace_hash = hashes.fetch(0)
|
|
63
|
+
# The transcript hashes of a received ClientHello up to its binders,
|
|
64
|
+
# by hash.
|
|
65
|
+
@binder_transcript_hashes = {} #: Hash[Symbol, String]
|
|
66
|
+
@key_schedule = nil #: KeySchedule?
|
|
67
|
+
# @type var outputs: Array[Output::t]
|
|
68
|
+
outputs = []
|
|
69
|
+
@outputs = outputs
|
|
70
|
+
# @type var events: Array[Event::t]
|
|
71
|
+
events = []
|
|
72
|
+
@events = events
|
|
73
|
+
# @type var flight_secrets: Array[Output::Secret]
|
|
74
|
+
flight_secrets = []
|
|
75
|
+
@flight_secrets = flight_secrets
|
|
76
|
+
# @type var flight_messages: Array[Output::Message]
|
|
77
|
+
flight_messages = []
|
|
78
|
+
@flight_messages = flight_messages
|
|
79
|
+
@side = yield(self)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def failed? = @state == :failed
|
|
83
|
+
|
|
84
|
+
# Runs the side's first steps (a client's ClientHello, a server's first
|
|
85
|
+
# flight) as one trigger.
|
|
86
|
+
def start(now:)
|
|
87
|
+
guard { @side.start(now:) }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Our own KeyUpdate, as one trigger: only once the handshake is
|
|
91
|
+
# complete, and never with a Profile that has no KeyUpdate message.
|
|
92
|
+
def request_key_update(request_peer_update:)
|
|
93
|
+
raise UsageError, "this Profile has no KeyUpdate message" unless @config.profile.allow_key_update_message
|
|
94
|
+
raise UsageError, "a KeyUpdate needs a complete handshake" unless @state == :connected
|
|
95
|
+
guard { @side.request_key_update(request_peer_update:) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Application data is about to go out: a KeyUpdate owed goes first.
|
|
99
|
+
def before_application_data
|
|
100
|
+
guard { @side.before_application_data } if @state == :connected
|
|
101
|
+
nil
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def application_data_received
|
|
105
|
+
@side.application_data_received if @state == :connected
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def receive(bytes, epoch:, now:)
|
|
110
|
+
guard do
|
|
111
|
+
next if @state.nil? || failed?
|
|
112
|
+
next fail!(:unexpected_message, :wrong_epoch) unless epoch == @read_epoch
|
|
113
|
+
reassembler = reassembler_for(epoch)
|
|
114
|
+
reassembler.expect(expected_types)
|
|
115
|
+
status = reassembler << bytes
|
|
116
|
+
status = drain(reassembler, epoch, now) if status == :ok
|
|
117
|
+
reassembly_failed(status, reassembler) unless status == :ok || failed?
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# A message that did not come through `receive`, as a server's
|
|
122
|
+
# ClientHello from an Acceptor does.
|
|
123
|
+
def receive_message(epoch, raw, now:)
|
|
124
|
+
guard { process(epoch, raw.getbyte(0).to_i, raw.byteslice(4, raw.bytesize).to_s, now) }
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def each_output
|
|
128
|
+
while (output = @outputs.shift)
|
|
129
|
+
yield output
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def next_event = @events.shift
|
|
134
|
+
|
|
135
|
+
# The message types acceptable now, from the side's table.
|
|
136
|
+
def expected_types = @side.expected_types(@state, @read_epoch)
|
|
137
|
+
|
|
138
|
+
# Whether bytes of the current epoch are waiting after the message
|
|
139
|
+
# being handled.
|
|
140
|
+
def pending_input? = reassembler_for(@read_epoch).pending?
|
|
141
|
+
|
|
142
|
+
# The transcript hash under `hash`, the selected one unless named.
|
|
143
|
+
def transcript_hash(hash = @transcript.selected) = @transcript.digest(hash)
|
|
144
|
+
|
|
145
|
+
# The transcript hash under `hash` the binders of the ClientHello just
|
|
146
|
+
# received are MACs over (RFC 9846 "PSK Binder").
|
|
147
|
+
def binder_transcript_hash(hash) = @binder_transcript_hashes.fetch(hash)
|
|
148
|
+
|
|
149
|
+
# The key schedule of this handshake. Each traffic secret goes to the
|
|
150
|
+
# keylog, if any, as it is derived.
|
|
151
|
+
def start_key_schedule(hash:, client_random:, psk: nil)
|
|
152
|
+
@key_schedule&.discard!
|
|
153
|
+
keylog = @keylog
|
|
154
|
+
log = keylog && ->(name, secret) { keylog.write(Keylog.line(name, client_random, secret)) }
|
|
155
|
+
@key_schedule = KeySchedule.new(hash:, psk:, on_traffic_secret: log)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def event!(event)
|
|
159
|
+
@events << event
|
|
160
|
+
nil
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def tracing? = @tracer ? true : false
|
|
164
|
+
|
|
165
|
+
def trace(value)
|
|
166
|
+
@tracer&.tls_trace(value)
|
|
167
|
+
nil
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# The only assignment to the state. A state outside the side's STATES is
|
|
171
|
+
# a bug here, not something the peer did, so it raises.
|
|
172
|
+
def transition!(to)
|
|
173
|
+
raise "#{to.inspect} is not a state of #{@side.class}" unless @side.class::STATES.include?(to) # steep:ignore
|
|
174
|
+
@state = to
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# The only place the epochs move and Secrets are queued. Moving the read
|
|
178
|
+
# epoch with bytes of the old one still waiting means a handshake
|
|
179
|
+
# message spans a key change: unexpected_message for RFC 9846, which
|
|
180
|
+
# QUIC turns into PROTOCOL_VIOLATION (RFC 9001 "Sending and Receiving
|
|
181
|
+
# Handshake Messages"). Returns false then. A KeyUpdate waiting there,
|
|
182
|
+
# where the Profile takes none, fails as one: RFC 9001 "Key Update"
|
|
183
|
+
# asks for unexpected_message for every KeyUpdate received.
|
|
184
|
+
def change_epochs!(cipher_suite:, read: nil, write: nil, secrets: [])
|
|
185
|
+
secrets.each do |epoch, direction, secret|
|
|
186
|
+
@flight_secrets << Output::Secret.new(epoch:, direction:, secret:, cipher_suite:)
|
|
187
|
+
end
|
|
188
|
+
if read && read != @read_epoch
|
|
189
|
+
if (left = @reassemblers[@read_epoch])&.pending?
|
|
190
|
+
key_update = !@config.profile.allow_key_update_message && left.next_type == KeyUpdate::MSG_TYPE
|
|
191
|
+
fail!(:unexpected_message, key_update ? @side.unexpected_reason(@state, KeyUpdate::MSG_TYPE) : :handshake_data_spans_key_change)
|
|
192
|
+
return false
|
|
193
|
+
end
|
|
194
|
+
@read_epoch = read
|
|
195
|
+
end
|
|
196
|
+
@write_epoch = write if write
|
|
197
|
+
true
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# The only place an Output::Alert is built. Drops the flight being
|
|
201
|
+
# built, queues the alert under the current write epoch and
|
|
202
|
+
# Event::Failed, and forgets every secret.
|
|
203
|
+
def fail!(alert, reason)
|
|
204
|
+
return nil if failed?
|
|
205
|
+
@flight_secrets.clear
|
|
206
|
+
@flight_messages.clear
|
|
207
|
+
@outputs << Output::Alert.new(level: :fatal, description: alert, epoch: @write_epoch)
|
|
208
|
+
@events << Event::Failed.new(alert:, reason:)
|
|
209
|
+
@alert = alert
|
|
210
|
+
forget_secrets!
|
|
211
|
+
transition!(:failed)
|
|
212
|
+
nil
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# The connection ended outside the handshake (a peer's alert, a QUIC
|
|
216
|
+
# CONNECTION_CLOSE): forget everything and stop, without output.
|
|
217
|
+
def discard!
|
|
218
|
+
@flight_secrets.clear
|
|
219
|
+
@flight_messages.clear
|
|
220
|
+
forget_secrets!
|
|
221
|
+
transition!(:failed) unless failed?
|
|
222
|
+
nil
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Sends `message`: into the transcript and the flight. Returns the
|
|
226
|
+
# message sent, which is a substitute's when one is queued for its type.
|
|
227
|
+
def emit_message!(epoch, message)
|
|
228
|
+
bytes, message = substitute(message)
|
|
229
|
+
@transcript << bytes if Transcript::TRANSCRIPT_MESSAGES.include?(message.msg_type)
|
|
230
|
+
trace_transcript(message.msg_type, bytes.bytesize)
|
|
231
|
+
@flight_messages << Output::Message.new(epoch:, bytes: bytes.freeze)
|
|
232
|
+
message
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# A client's ClientHello offering a PSK, whose binders are MACs over the
|
|
236
|
+
# transcript up to them (RFC 9846 "PSK Binder"): the ClientHello up to
|
|
237
|
+
# the binders goes into the transcript, the block makes the binders from
|
|
238
|
+
# its hash under `hash`, then the binders follow. A substitute's bytes
|
|
239
|
+
# up to its binders take the place of ours, so its binders are made
|
|
240
|
+
# here as ours would be.
|
|
241
|
+
def emit_client_hello_with_binders!(message, hash)
|
|
242
|
+
bytes, message = substitute(message)
|
|
243
|
+
truncated = bytes.byteslice(0, bytes.bytesize - message.binders_wire_length).to_s
|
|
244
|
+
@transcript << truncated
|
|
245
|
+
binders = yield(@transcript.digest(hash))
|
|
246
|
+
w = Codec::Writer.new
|
|
247
|
+
w.vector(2, min: 33) { binders.each { |binder| w.vector(1, min: 32) { w.bytes(binder) } } }
|
|
248
|
+
@transcript << w.to_s
|
|
249
|
+
sent = message.with_binders(binders)
|
|
250
|
+
trace_transcript(sent.msg_type, truncated.bytesize + w.to_s.bytesize)
|
|
251
|
+
@flight_messages << Output::Message.new(epoch: :initial, bytes: (truncated + w.to_s).freeze)
|
|
252
|
+
sent
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# A server's ServerHello: the suite is chosen, so the transcript keeps
|
|
256
|
+
# only its hash from here.
|
|
257
|
+
def emit_server_hello!(message, hash)
|
|
258
|
+
@transcript.select!(hash)
|
|
259
|
+
emit_message!(:initial, message)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# A client's ServerHello, added once the side has checked its suite.
|
|
263
|
+
def record_server_hello!(raw, hash)
|
|
264
|
+
@transcript << raw
|
|
265
|
+
@transcript.select!(hash)
|
|
266
|
+
trace_transcript(ServerHello::MSG_TYPE, raw.bytesize)
|
|
267
|
+
nil
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# A HelloRetryRequest the client received: ClientHello1 gives way to
|
|
271
|
+
# message_hash under the HelloRetryRequest's hash (RFC 9846 "Transcript
|
|
272
|
+
# Hash"), then the HelloRetryRequest follows.
|
|
273
|
+
def receive_hello_retry_request!(raw, hash)
|
|
274
|
+
@transcript.select!(hash)
|
|
275
|
+
@transcript.restart_with_message_hash!
|
|
276
|
+
@transcript << raw
|
|
277
|
+
trace_transcript(ServerHello::MSG_TYPE, raw.bytesize)
|
|
278
|
+
nil
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# A HelloRetryRequest the server sends, in the same order.
|
|
282
|
+
def emit_hello_retry_request!(message, hash)
|
|
283
|
+
@transcript.select!(hash)
|
|
284
|
+
@transcript.restart_with_message_hash!
|
|
285
|
+
emit_message!(:initial, message)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Ends the flight being built here, so that what follows goes out after
|
|
289
|
+
# it. Only a KeyUpdate needs this: its Message goes under the old key,
|
|
290
|
+
# before the Secret that replaces it.
|
|
291
|
+
def close_flight!
|
|
292
|
+
@outputs.concat(@flight_secrets)
|
|
293
|
+
@outputs.concat(@flight_messages)
|
|
294
|
+
@flight_secrets.clear
|
|
295
|
+
@flight_messages.clear
|
|
296
|
+
nil
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# Runs a trigger: queues the flight it built, Secrets first. An
|
|
300
|
+
# exception escaping it, from a user's object or a bug, leaves the
|
|
301
|
+
# handshake failed with its secrets forgotten, and goes on up.
|
|
302
|
+
private def guard
|
|
303
|
+
yield
|
|
304
|
+
close_flight!
|
|
305
|
+
rescue StandardError
|
|
306
|
+
discard!
|
|
307
|
+
raise
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
private def drain(reassembler, epoch, now)
|
|
311
|
+
reassembler.each_message do |msg_type, body|
|
|
312
|
+
process(epoch, msg_type, body, now)
|
|
313
|
+
break :ok if failed? || @read_epoch != epoch
|
|
314
|
+
reassembler.expect(expected_types)
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# Bytes, then the transcript, then the parser, then the side. The
|
|
319
|
+
# ServerHello waits for the side, which knows whether it is a
|
|
320
|
+
# HelloRetryRequest only after checking it; the ClientHello waits for
|
|
321
|
+
# the parser, which finds its binders.
|
|
322
|
+
private def process(epoch, msg_type, body, now)
|
|
323
|
+
raw = [msg_type, body.bytesize >> 16, body.bytesize >> 8 & 0xff, body.bytesize & 0xff].pack("C4") + body
|
|
324
|
+
before = @transcript.digest if CHECKED_AGAINST_PREVIOUS_TRANSCRIPT.include?(msg_type)
|
|
325
|
+
if Transcript::TRANSCRIPT_MESSAGES.include?(msg_type) && !AFTER_PARSING.include?(msg_type)
|
|
326
|
+
@transcript << raw
|
|
327
|
+
trace_transcript(msg_type, raw.bytesize)
|
|
328
|
+
end
|
|
329
|
+
begin
|
|
330
|
+
message = Handshake.parse_body(msg_type, body, registry: @config.registry)
|
|
331
|
+
rescue Codec::Malformed => e
|
|
332
|
+
return fail!(e.alert, :malformed_message)
|
|
333
|
+
end
|
|
334
|
+
if (violation = message.validate)
|
|
335
|
+
return fail!(violation.alert, violation.reason)
|
|
336
|
+
end
|
|
337
|
+
case message
|
|
338
|
+
when ClientHello then record_client_hello(raw, message)
|
|
339
|
+
end
|
|
340
|
+
@side.handle(message, raw:, transcript_hash_before: before, now:)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
# A received ClientHello goes into the transcript in two parts around
|
|
344
|
+
# its binders, the hashes between them kept for a binder to be
|
|
345
|
+
# verified by, as the client made them; the same order as
|
|
346
|
+
# emit_client_hello_with_binders!.
|
|
347
|
+
private def record_client_hello(raw, client_hello)
|
|
348
|
+
cut = raw.bytesize - client_hello.binders_wire_length
|
|
349
|
+
@transcript << raw.byteslice(0, cut).to_s
|
|
350
|
+
hashes = (selected = @transcript.selected) ? [selected] : @hashes
|
|
351
|
+
@binder_transcript_hashes = hashes.to_h { |hash| [hash, @transcript.digest(hash)] }
|
|
352
|
+
@transcript << raw.byteslice(cut, raw.bytesize - cut).to_s
|
|
353
|
+
trace_transcript(ClientHello::MSG_TYPE, raw.bytesize)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
private def reassembly_failed(status, reassembler)
|
|
357
|
+
if status == :overflow
|
|
358
|
+
fail!(:decode_error, :message_too_large)
|
|
359
|
+
else
|
|
360
|
+
fail!(:unexpected_message, @side.unexpected_reason(@state, reassembler.next_type))
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
private def reassembler_for(epoch)
|
|
365
|
+
@reassemblers[epoch] ||= Reassembler.new(limits: ->(msg_type) { message_limit(msg_type) })
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
private def message_limit(msg_type)
|
|
369
|
+
CERTIFICATE_MESSAGES.include?(msg_type) ? @config.max_certificate_message_size : @config.max_handshake_message_size
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
private def substitute(message) # steep:ignore
|
|
373
|
+
bytes = @substitutions&.[](message.msg_type)&.shift
|
|
374
|
+
return [message.encode, message] unless bytes
|
|
375
|
+
[bytes, Handshake.parse(bytes, registry: @config.registry)]
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
private def trace_transcript(msg_type, length)
|
|
379
|
+
tracer = @tracer or return
|
|
380
|
+
digest = @transcript.digest(@transcript.selected || @trace_hash)
|
|
381
|
+
tracer.tls_trace(Trace::TranscriptUpdated.new(msg_type:, length:, digest:))
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
private def forget_secrets!
|
|
385
|
+
@key_schedule&.discard!
|
|
386
|
+
@side.forget_secrets!
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "../extension"
|
|
5
|
+
require_relative "message"
|
|
6
|
+
require_relative "violation"
|
|
7
|
+
|
|
8
|
+
module Raiha::TLS
|
|
9
|
+
module Handshake
|
|
10
|
+
class EncryptedExtensions < Data.define(:extensions)
|
|
11
|
+
include Message
|
|
12
|
+
include ExtensionLookup
|
|
13
|
+
|
|
14
|
+
MSG_TYPE = 8
|
|
15
|
+
|
|
16
|
+
def self.parse_body(body, registry:)
|
|
17
|
+
r = Codec::Reader.new(body)
|
|
18
|
+
extensions = Extension.read_list(r, context: :encrypted_extensions, registry:)
|
|
19
|
+
r.finish!
|
|
20
|
+
new(extensions:)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def msg_type = MSG_TYPE
|
|
24
|
+
|
|
25
|
+
# Nothing to check without context.
|
|
26
|
+
def validate = nil
|
|
27
|
+
|
|
28
|
+
def write_body(w)
|
|
29
|
+
Extension.write_list(w, extensions)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "message"
|
|
5
|
+
require_relative "violation"
|
|
6
|
+
|
|
7
|
+
module Raiha::TLS
|
|
8
|
+
module Handshake
|
|
9
|
+
# Empty. Sent over TCP only; QUIC forbids it.
|
|
10
|
+
class EndOfEarlyData < Data.define
|
|
11
|
+
include Message
|
|
12
|
+
|
|
13
|
+
MSG_TYPE = 5
|
|
14
|
+
|
|
15
|
+
def self.parse_body(body, registry: nil)
|
|
16
|
+
Codec::Reader.new(body).finish!
|
|
17
|
+
new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def msg_type = MSG_TYPE
|
|
21
|
+
|
|
22
|
+
# Nothing to check without context.
|
|
23
|
+
def validate = nil
|
|
24
|
+
|
|
25
|
+
def write_body(w) = nil
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "../masked_inspect"
|
|
5
|
+
require_relative "message"
|
|
6
|
+
require_relative "violation"
|
|
7
|
+
|
|
8
|
+
module Raiha::TLS
|
|
9
|
+
module Handshake
|
|
10
|
+
# verify_data has no length prefix: it is the whole body, and its length
|
|
11
|
+
# must equal the negotiated hash's. Only the state machine knows the hash,
|
|
12
|
+
# so the length is checked there.
|
|
13
|
+
class Finished < Data.define(:verify_data)
|
|
14
|
+
include Message
|
|
15
|
+
include MaskedInspect
|
|
16
|
+
|
|
17
|
+
MSG_TYPE = 20
|
|
18
|
+
|
|
19
|
+
def self.parse_body(body, registry: nil)
|
|
20
|
+
r = Codec::Reader.new(body)
|
|
21
|
+
new(verify_data: r.bytes(r.remaining))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def msg_type = MSG_TYPE
|
|
25
|
+
|
|
26
|
+
# Nothing to check without context.
|
|
27
|
+
def validate = nil
|
|
28
|
+
|
|
29
|
+
def write_body(w)
|
|
30
|
+
w.bytes(verify_data)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def masked_members = %i[verify_data]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "message"
|
|
5
|
+
require_relative "violation"
|
|
6
|
+
|
|
7
|
+
module Raiha::TLS
|
|
8
|
+
module Handshake
|
|
9
|
+
# `request_update` is kept as received; a value other than 0 or 1 is a
|
|
10
|
+
# violation that `#validate` reports, not a parse error.
|
|
11
|
+
class KeyUpdate < Data.define(:request_update)
|
|
12
|
+
include Message
|
|
13
|
+
|
|
14
|
+
MSG_TYPE = 24
|
|
15
|
+
|
|
16
|
+
UPDATE_NOT_REQUESTED = 0
|
|
17
|
+
UPDATE_REQUESTED = 1
|
|
18
|
+
|
|
19
|
+
def self.parse_body(body, registry: nil)
|
|
20
|
+
r = Codec::Reader.new(body)
|
|
21
|
+
request_update = r.u8
|
|
22
|
+
r.finish!
|
|
23
|
+
new(request_update:)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def msg_type = MSG_TYPE
|
|
27
|
+
|
|
28
|
+
def validate
|
|
29
|
+
return nil if request_update == UPDATE_NOT_REQUESTED || request_update == UPDATE_REQUESTED
|
|
30
|
+
Violation.new(alert: :illegal_parameter, reason: :request_update)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def write_body(w)
|
|
34
|
+
w.u8(request_update)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "key_update"
|
|
4
|
+
|
|
5
|
+
module Raiha::TLS
|
|
6
|
+
module Handshake
|
|
7
|
+
# KeyUpdate over TCP, the same for both roles (RFC 9846 "Key and
|
|
8
|
+
# Initialization Vector Update"). QUIC updates keys on its own and never
|
|
9
|
+
# gets here.
|
|
10
|
+
#
|
|
11
|
+
# A peer's KeyUpdate moves our reading to the next generation. When it
|
|
12
|
+
# asks for one back, we owe a single KeyUpdate however many requests
|
|
13
|
+
# come, and send it only right before our next application data: RFC
|
|
14
|
+
# 9846 lets a silent side answer many requests once, so what the peer
|
|
15
|
+
# sends never makes us write. A run of more than
|
|
16
|
+
# MAX_CONSECUTIVE_KEY_UPDATES without application data from the peer
|
|
17
|
+
# ends the connection.
|
|
18
|
+
#
|
|
19
|
+
# RFC 9846 limits how many times a sender updates, never the receiver:
|
|
20
|
+
# past MAX_KEY_UPDATES we stop sending, while the peer's updates are
|
|
21
|
+
# always followed.
|
|
22
|
+
module KeyUpdateHandling
|
|
23
|
+
MAX_KEY_UPDATES = (1 << 48) - 1
|
|
24
|
+
# BoringSSL's kMaxKeyUpdates, which rustls uses too.
|
|
25
|
+
MAX_CONSECUTIVE_KEY_UPDATES = 32
|
|
26
|
+
|
|
27
|
+
# How many KeyUpdates we have sent: the epoch RFC 9846 limits.
|
|
28
|
+
attr_reader :key_updates
|
|
29
|
+
|
|
30
|
+
# Owes the peer an answer before our next application data.
|
|
31
|
+
def before_application_data
|
|
32
|
+
send_key_update(KeyUpdate::UPDATE_NOT_REQUESTED) if @key_update_owed
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# The peer's run of KeyUpdates ends.
|
|
37
|
+
def application_data_received
|
|
38
|
+
@consecutive_key_updates = 0
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# A KeyUpdate of our own. A KeyUpdate we owe goes first and also
|
|
43
|
+
# serves for one that does not ask for the peer's. While our request
|
|
44
|
+
# waits for the peer's KeyUpdate, another one cannot ask again.
|
|
45
|
+
def request_key_update(request_peer_update:)
|
|
46
|
+
owed = @key_update_owed
|
|
47
|
+
before_application_data
|
|
48
|
+
return nil if owed && !request_peer_update
|
|
49
|
+
send_key_update(request_peer_update ? KeyUpdate::UPDATE_REQUESTED : KeyUpdate::UPDATE_NOT_REQUESTED)
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private def start_key_updates
|
|
54
|
+
@key_updates = 0
|
|
55
|
+
@consecutive_key_updates = 0
|
|
56
|
+
@key_update_owed = false
|
|
57
|
+
@awaiting_peer_update = false
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# A KeyUpdate changes keys, so it has to end its record (RFC 9846
|
|
61
|
+
# "Record Protocol"); that, and the run of KeyUpdates, are checked
|
|
62
|
+
# before anything changes or is owed.
|
|
63
|
+
private def receive_key_update(message)
|
|
64
|
+
return fail!(:unexpected_message, :handshake_data_spans_key_change) if @conductor.pending_input?
|
|
65
|
+
@consecutive_key_updates += 1
|
|
66
|
+
return fail!(:unexpected_message, :too_many_key_updates) if @consecutive_key_updates > MAX_CONSECUTIVE_KEY_UPDATES
|
|
67
|
+
@conductor.change_epochs!(cipher_suite: suite,
|
|
68
|
+
secrets: [[:application, :read, key_schedule.update_application_traffic_secret!(peer_side)]])
|
|
69
|
+
@awaiting_peer_update = false
|
|
70
|
+
@key_update_owed = true if message.request_update == KeyUpdate::UPDATE_REQUESTED
|
|
71
|
+
nil
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Sent under the old key, then the next write Secret.
|
|
75
|
+
private def send_key_update(request)
|
|
76
|
+
@key_update_owed = false
|
|
77
|
+
return if @key_updates >= MAX_KEY_UPDATES
|
|
78
|
+
request = KeyUpdate::UPDATE_NOT_REQUESTED if @awaiting_peer_update
|
|
79
|
+
@conductor.emit_message!(:application, KeyUpdate.new(request_update: request))
|
|
80
|
+
@conductor.close_flight!
|
|
81
|
+
@key_updates += 1
|
|
82
|
+
@awaiting_peer_update = true if request == KeyUpdate::UPDATE_REQUESTED
|
|
83
|
+
@conductor.change_epochs!(cipher_suite: suite,
|
|
84
|
+
secrets: [[:application, :write, key_schedule.update_application_traffic_secret!(own_side)]])
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|