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,578 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../cipher_suite"
|
|
4
|
+
require_relative "../config"
|
|
5
|
+
require_relative "../event"
|
|
6
|
+
require_relative "../extension"
|
|
7
|
+
require_relative "../iana"
|
|
8
|
+
require_relative "../key_schedule"
|
|
9
|
+
require_relative "../session"
|
|
10
|
+
require_relative "../trace"
|
|
11
|
+
require_relative "../verifier"
|
|
12
|
+
require_relative "authentication_messages"
|
|
13
|
+
require_relative "client_hello_builder"
|
|
14
|
+
require_relative "key_update_handling"
|
|
15
|
+
|
|
16
|
+
module Raiha::TLS
|
|
17
|
+
module Handshake
|
|
18
|
+
# The client's states, table of expected messages and handlers, and what
|
|
19
|
+
# it keeps for one connection. The checks in each handler follow the
|
|
20
|
+
# state machine's table for that state, in its order; the first failure
|
|
21
|
+
# is the one reported.
|
|
22
|
+
class ClientSide
|
|
23
|
+
include KeyUpdateHandling
|
|
24
|
+
|
|
25
|
+
STATES = %i[wait_sh wait_sh2 wait_ee wait_cert_cr wait_cert wait_cv wait_finished connected failed].freeze
|
|
26
|
+
|
|
27
|
+
# State => epoch => the message types acceptable there. KeyUpdate in
|
|
28
|
+
# :connected only where the Profile allows it.
|
|
29
|
+
EXPECTED = Ractor.make_shareable({
|
|
30
|
+
wait_sh: { initial: Set[ServerHello::MSG_TYPE] },
|
|
31
|
+
wait_sh2: { initial: Set[ServerHello::MSG_TYPE] },
|
|
32
|
+
wait_ee: { handshake: Set[EncryptedExtensions::MSG_TYPE] },
|
|
33
|
+
wait_cert_cr: { handshake: Set[Certificate::MSG_TYPE, CertificateRequest::MSG_TYPE] },
|
|
34
|
+
wait_cert: { handshake: Set[Certificate::MSG_TYPE] },
|
|
35
|
+
wait_cv: { handshake: Set[CertificateVerify::MSG_TYPE] },
|
|
36
|
+
wait_finished: { handshake: Set[Finished::MSG_TYPE] },
|
|
37
|
+
connected: { application: Set[NewSessionTicket::MSG_TYPE, KeyUpdate::MSG_TYPE] },
|
|
38
|
+
failed: {} # steep:ignore
|
|
39
|
+
}) #: Hash[Symbol, Hash[epoch, Set[Integer]]]
|
|
40
|
+
|
|
41
|
+
NONE = Set.new.freeze #: Set[Integer]
|
|
42
|
+
|
|
43
|
+
# RFC 9001 "Enabling 0-RTT": a QUIC server sends this, and only this, as
|
|
44
|
+
# max_early_data_size.
|
|
45
|
+
EARLY_DATA_SENTINEL = 0xffffffff
|
|
46
|
+
|
|
47
|
+
attr_reader :cipher_suite, :group, :alpn, :peer_certificates, :exporter_values, :peer_record_size_limit
|
|
48
|
+
|
|
49
|
+
def initialize(conductor, server_name:, sni:, extensions:, tracer:, session:, early_data:)
|
|
50
|
+
@conductor = conductor
|
|
51
|
+
@config = conductor.config
|
|
52
|
+
@keyring = conductor.keyring
|
|
53
|
+
@server_name = server_name
|
|
54
|
+
@sni = sni
|
|
55
|
+
@extensions = extensions
|
|
56
|
+
@authentication = AuthenticationMessages.new(AuthenticationMessages::Parameters::CLIENT, tracer:)
|
|
57
|
+
# The session given to resume, until it is not offered, and the PSK
|
|
58
|
+
# offered with it.
|
|
59
|
+
@session = session
|
|
60
|
+
@psk = nil #: String?
|
|
61
|
+
# Whether the server took the offered PSK.
|
|
62
|
+
@resumed = false
|
|
63
|
+
# Whether 0-RTT is to be sent when it can be, and how it went: :none,
|
|
64
|
+
# :attempted, then :accepted or :rejected.
|
|
65
|
+
@early_data_requested = early_data
|
|
66
|
+
@early_data = :none #: :none | :attempted | :accepted | :rejected
|
|
67
|
+
@client_hello = nil #: ClientHello?
|
|
68
|
+
@hello_retry_request = nil #: ServerHello?
|
|
69
|
+
# @type var key_pairs: Array[KeyExchange::KeyPair]
|
|
70
|
+
key_pairs = []
|
|
71
|
+
@key_pairs = key_pairs
|
|
72
|
+
@cipher_suite = nil #: CipherSuite?
|
|
73
|
+
@group = nil #: Symbol?
|
|
74
|
+
@alpn = nil #: String?
|
|
75
|
+
@peer_record_size_limit = nil #: Integer?
|
|
76
|
+
@certificate_request = nil #: CertificateRequest?
|
|
77
|
+
# @type var peer_certificates: Array[OpenSSL::X509::Certificate]
|
|
78
|
+
peer_certificates = []
|
|
79
|
+
@peer_certificates = peer_certificates
|
|
80
|
+
@peer_verified = false
|
|
81
|
+
# When the full handshake whose key material the connection holds
|
|
82
|
+
# took place (milliseconds), nil until the handshake is over.
|
|
83
|
+
@authenticated_at = nil #: Integer?
|
|
84
|
+
# @type var exporter_values: Hash[String, String]
|
|
85
|
+
exporter_values = {}
|
|
86
|
+
@exporter_values = exporter_values
|
|
87
|
+
start_key_updates
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Sends the ClientHello, with the session's PSK when it passes the
|
|
91
|
+
# checks.
|
|
92
|
+
def start(now:)
|
|
93
|
+
session = session_to_offer(now)
|
|
94
|
+
built = ClientHelloBuilder.build(config: @config, keyring: @keyring, server_name: @sni, extensions: @extensions)
|
|
95
|
+
@key_pairs = built.key_pairs
|
|
96
|
+
@client_hello = if session
|
|
97
|
+
offer(built.client_hello, session, now, early_data: early_data?(session))
|
|
98
|
+
else
|
|
99
|
+
@conductor.emit_message!(:initial, built.client_hello)
|
|
100
|
+
end
|
|
101
|
+
@conductor.transition!(:wait_sh)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def expected_types(state, epoch)
|
|
105
|
+
return NONE if state.nil?
|
|
106
|
+
types = EXPECTED.fetch(state).fetch(epoch, NONE)
|
|
107
|
+
@config.profile.allow_key_update_message ? types : types - [KeyUpdate::MSG_TYPE]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# A CertificateRequest after the handshake: v1 has no post-handshake
|
|
111
|
+
# authentication and never offers it. QUIC maps this reason to
|
|
112
|
+
# PROTOCOL_VIOLATION (RFC 9001 "Peer Authentication").
|
|
113
|
+
def unexpected_reason(state, msg_type)
|
|
114
|
+
if state == :connected && msg_type == CertificateRequest::MSG_TYPE
|
|
115
|
+
:post_handshake_certificate_request
|
|
116
|
+
else
|
|
117
|
+
:unexpected_message
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def handle(message, raw:, transcript_hash_before:, now:)
|
|
122
|
+
case message
|
|
123
|
+
when ServerHello then server_hello(message, raw, now)
|
|
124
|
+
when EncryptedExtensions then encrypted_extensions(message)
|
|
125
|
+
when CertificateRequest then certificate_request(message)
|
|
126
|
+
when Certificate then certificate(message, now)
|
|
127
|
+
when CertificateVerify then certificate_verify(message, transcript_hash_before) # steep:ignore
|
|
128
|
+
when Finished then finished(message, transcript_hash_before, now) # steep:ignore
|
|
129
|
+
when NewSessionTicket then new_session_ticket(message, now)
|
|
130
|
+
when KeyUpdate then receive_key_update(message)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# A client skips no 0-RTT of its peer.
|
|
135
|
+
def skip_early_data_limit = nil
|
|
136
|
+
|
|
137
|
+
def forget_secrets!
|
|
138
|
+
@psk&.clear
|
|
139
|
+
@key_pairs = []
|
|
140
|
+
@exporter_values.each_value(&:clear).clear
|
|
141
|
+
nil
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
private def client_hello = @client_hello || raise("no ClientHello was sent")
|
|
145
|
+
|
|
146
|
+
private def key_schedule = @conductor.key_schedule || raise("the key schedule has not started")
|
|
147
|
+
|
|
148
|
+
private def suite = @cipher_suite || raise("no cipher suite was negotiated")
|
|
149
|
+
|
|
150
|
+
private def own_side = :client
|
|
151
|
+
|
|
152
|
+
private def peer_side = :server
|
|
153
|
+
|
|
154
|
+
private def fail!(alert, reason) = @conductor.fail!(alert, reason)
|
|
155
|
+
|
|
156
|
+
# The types of the extensions offered that a server may answer: not the
|
|
157
|
+
# GREASE ones, which RFC 8701 "Client Behavior" has the client fail a
|
|
158
|
+
# server for answering ("Any ServerHello extension", "Any
|
|
159
|
+
# HelloRetryRequest, EncryptedExtensions, or Certificate extension").
|
|
160
|
+
private def offered_types = client_hello.extensions.map(&:extension_type).reject { |type| IANA.grease?(type) }
|
|
161
|
+
|
|
162
|
+
private def server_hello(message, raw, now)
|
|
163
|
+
hrr = @hello_retry_request
|
|
164
|
+
# 0a to 0c, after a HelloRetryRequest, before everything else.
|
|
165
|
+
if hrr
|
|
166
|
+
failure = check_after_hello_retry_request(message, hrr)
|
|
167
|
+
return fail!(failure.alert, failure.reason) if failure
|
|
168
|
+
end
|
|
169
|
+
# 1
|
|
170
|
+
suite = CipherSuite.find(message.cipher_suite)
|
|
171
|
+
unless suite && client_hello.cipher_suites.include?(suite.id) && @config.cipher_suite_ids.include?(suite.id)
|
|
172
|
+
return fail!(:illegal_parameter, :cipher_suite_not_offered)
|
|
173
|
+
end
|
|
174
|
+
# 2
|
|
175
|
+
unless message.legacy_session_id_echo == client_hello.legacy_session_id
|
|
176
|
+
return fail!(:illegal_parameter, :legacy_session_id_echo)
|
|
177
|
+
end
|
|
178
|
+
# 3: the HelloRetryRequest may bring a cookie we did not send.
|
|
179
|
+
allowed = offered_types
|
|
180
|
+
allowed += [Extension::Cookie::TYPE] if message.hello_retry_request?
|
|
181
|
+
unless message.extensions.all? { |ext| allowed.include?(ext.extension_type) }
|
|
182
|
+
return fail!(:unsupported_extension, :unsolicited_extension)
|
|
183
|
+
end
|
|
184
|
+
message.hello_retry_request? ? hello_retry_request(message, raw, suite, now) : accept_server_hello(message, raw, suite)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# RFC 9846 "Hello Retry Request" and "Key Share": one
|
|
188
|
+
# HelloRetryRequest, then the same suite and the group it asked for.
|
|
189
|
+
private def check_after_hello_retry_request(message, hrr)
|
|
190
|
+
# 0a
|
|
191
|
+
return Violation.new(alert: :unexpected_message, reason: :second_hello_retry_request) if message.hello_retry_request?
|
|
192
|
+
# 0b
|
|
193
|
+
return Violation.new(alert: :illegal_parameter, reason: :cipher_suite_changed) unless message.cipher_suite == hrr.cipher_suite
|
|
194
|
+
# 0c
|
|
195
|
+
share = message.extension(Extension::KeyShare::TYPE)
|
|
196
|
+
requested = hrr.extension(Extension::KeyShare::TYPE)
|
|
197
|
+
case requested
|
|
198
|
+
when Extension::KeyShare::HRR
|
|
199
|
+
case share
|
|
200
|
+
when Extension::KeyShare::SH
|
|
201
|
+
return Violation.new(alert: :illegal_parameter, reason: :key_share_group_changed) unless share.server_share.group == requested.selected_group
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
private def hello_retry_request(hrr, raw, suite, now)
|
|
208
|
+
# 4: the server cannot send anything before our second ClientHello.
|
|
209
|
+
return fail!(:unexpected_message, :data_after_hello_retry_request) if @conductor.pending_input?
|
|
210
|
+
requested = case (ext = hrr.extension(Extension::KeyShare::TYPE))
|
|
211
|
+
when Extension::KeyShare::HRR then ext.selected_group
|
|
212
|
+
end
|
|
213
|
+
if requested
|
|
214
|
+
# 5
|
|
215
|
+
offered_groups = @config.group_ids
|
|
216
|
+
shared_groups = key_share_groups(client_hello)
|
|
217
|
+
if !offered_groups.include?(requested) || shared_groups.include?(requested)
|
|
218
|
+
return fail!(:illegal_parameter, :hello_retry_request_group)
|
|
219
|
+
end
|
|
220
|
+
elsif hrr.extensions.none? { |ext| ext.extension_type == Extension::Cookie::TYPE }
|
|
221
|
+
# 6: nothing would change in the second ClientHello.
|
|
222
|
+
return fail!(:illegal_parameter, :hello_retry_request_changes_nothing)
|
|
223
|
+
end
|
|
224
|
+
@conductor.receive_hello_retry_request!(raw, suite.hash_algorithm)
|
|
225
|
+
@hello_retry_request = hrr
|
|
226
|
+
# No 0-RTT after a HelloRetryRequest (RFC 9846 "Early Data
|
|
227
|
+
# Indication"): back to sending in the clear.
|
|
228
|
+
reject_early_data(:hello_retry_request)
|
|
229
|
+
@conductor.change_epochs!(cipher_suite: suite, write: :initial)
|
|
230
|
+
second = ClientHelloBuilder.retry(client_hello, hrr) do |group|
|
|
231
|
+
key_pair = @keyring.key_exchange.generate(IANA::NAMED_GROUPS.fetch(group))
|
|
232
|
+
@key_pairs = [key_pair]
|
|
233
|
+
Extension::KeyShare::Entry.new(group:, key_exchange: key_pair.public_bytes)
|
|
234
|
+
end
|
|
235
|
+
@client_hello = offer_again(second, suite, now)
|
|
236
|
+
@conductor.transition!(:wait_sh2)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# The second ClientHello offers the session again, its age and binder
|
|
240
|
+
# made anew, when the session's hash is the suite's; otherwise it
|
|
241
|
+
# offers none, as RFC 9846 "Pre-Shared Key Extension" asks, and the
|
|
242
|
+
# handshake is a full one.
|
|
243
|
+
private def offer_again(second, suite, now)
|
|
244
|
+
session = @session
|
|
245
|
+
if session && CipherSuite.find(session.cipher_suite)&.hash_algorithm == suite.hash_algorithm
|
|
246
|
+
return offer(second, session, now)
|
|
247
|
+
end
|
|
248
|
+
@psk&.clear
|
|
249
|
+
@psk = nil
|
|
250
|
+
@session = nil
|
|
251
|
+
@conductor.emit_message!(:initial, second.with(extensions: second.extensions.reject { |ext| ext.extension_type == Extension::PreSharedKey::TYPE }))
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
private def accept_server_hello(message, raw, suite)
|
|
255
|
+
share = case (ext = message.extension(Extension::KeyShare::TYPE))
|
|
256
|
+
when Extension::KeyShare::SH then ext.server_share
|
|
257
|
+
end
|
|
258
|
+
# 7, and 9 with a PSK: psk_dhe_ke is the only mode offered, so there
|
|
259
|
+
# is always a key exchange.
|
|
260
|
+
return fail!(:illegal_parameter, :missing_key_share) unless share
|
|
261
|
+
# 8
|
|
262
|
+
key_pair = @key_pairs.find { |pair| Config::GROUPS_BY_NAME[pair.group] == share.group }
|
|
263
|
+
return fail!(:illegal_parameter, :key_share_group_not_offered) unless key_pair
|
|
264
|
+
# 9: one identity offered, of the session's hash (RFC 9846
|
|
265
|
+
# "Pre-Shared Key Extension"); an unoffered pre_shared_key fails at 3.
|
|
266
|
+
selected = case (ext = message.extension(Extension::PreSharedKey::TYPE))
|
|
267
|
+
when Extension::PreSharedKey::SH then ext.selected_identity
|
|
268
|
+
end
|
|
269
|
+
session = @session
|
|
270
|
+
if selected
|
|
271
|
+
return fail!(:illegal_parameter, :selected_identity_out_of_range) unless selected.zero? && session
|
|
272
|
+
unless CipherSuite.find(session.cipher_suite)&.hash_algorithm == suite.hash_algorithm
|
|
273
|
+
return fail!(:illegal_parameter, :psk_cipher_suite_mismatch)
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
# 10
|
|
277
|
+
shared_secret = @keyring.key_exchange.decapsulate(key_pair, share.key_exchange)
|
|
278
|
+
return fail!(:illegal_parameter, :invalid_key_share) unless shared_secret
|
|
279
|
+
|
|
280
|
+
@conductor.record_server_hello!(raw, suite.hash_algorithm)
|
|
281
|
+
@cipher_suite = suite
|
|
282
|
+
@group = key_pair.group
|
|
283
|
+
@key_pairs = []
|
|
284
|
+
@resumed = !selected.nil?
|
|
285
|
+
reject_early_data(:psk_not_selected) unless @resumed
|
|
286
|
+
schedule = @conductor.start_key_schedule(hash: suite.hash_algorithm, client_random: client_hello.random, psk: (@psk if @resumed))
|
|
287
|
+
@psk&.clear
|
|
288
|
+
@psk = nil
|
|
289
|
+
schedule.handshake!(shared_secret)
|
|
290
|
+
shared_secret.clear
|
|
291
|
+
transcript_hash = @conductor.transcript_hash
|
|
292
|
+
client_secret = schedule.client_handshake_traffic_secret(transcript_hash)
|
|
293
|
+
server_secret = schedule.server_handshake_traffic_secret(transcript_hash)
|
|
294
|
+
# 0-RTT goes on until EncryptedExtensions says whether it was taken.
|
|
295
|
+
moved = @conductor.change_epochs!(cipher_suite: suite, read: :handshake, write: (:handshake unless @early_data == :attempted),
|
|
296
|
+
secrets: [[:handshake, :read, server_secret], [:handshake, :write, client_secret]])
|
|
297
|
+
@conductor.transition!(:wait_ee) if moved
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
private def encrypted_extensions(message)
|
|
301
|
+
types = message.extensions.map(&:extension_type)
|
|
302
|
+
profile = @config.profile
|
|
303
|
+
# The other direction of RFC 9001's rule on quic_transport_parameters.
|
|
304
|
+
unless (types & profile.forbidden_extensions).empty?
|
|
305
|
+
return fail!(:unsupported_extension, :forbidden_extension)
|
|
306
|
+
end
|
|
307
|
+
# 1
|
|
308
|
+
return fail!(:unsupported_extension, :unsolicited_extension) unless (types - offered_types).empty?
|
|
309
|
+
alpn = case (ext = message.extension(Extension::Alpn::TYPE))
|
|
310
|
+
when Extension::Alpn then ext.protocol_name_list
|
|
311
|
+
end
|
|
312
|
+
if types.include?(Extension::EarlyData::TYPE)
|
|
313
|
+
failure = check_early_data_accepted(alpn&.first)
|
|
314
|
+
return fail!(failure.alert, failure.reason) if failure
|
|
315
|
+
end
|
|
316
|
+
# 4
|
|
317
|
+
if alpn && !(alpn.size == 1 && @config.alpn.include?(alpn.fetch(0)))
|
|
318
|
+
return fail!(:no_application_protocol, :alpn_not_offered)
|
|
319
|
+
end
|
|
320
|
+
# 5
|
|
321
|
+
if profile.require_alpn && !@config.alpn.empty? && alpn.nil?
|
|
322
|
+
return fail!(:no_application_protocol, :alpn_not_selected)
|
|
323
|
+
end
|
|
324
|
+
# 6
|
|
325
|
+
missing = profile.required_extensions - types
|
|
326
|
+
return fail!(:missing_extension, :required_extension) unless missing.empty?
|
|
327
|
+
|
|
328
|
+
@alpn = alpn&.first
|
|
329
|
+
if types.include?(Extension::EarlyData::TYPE)
|
|
330
|
+
@early_data = :accepted
|
|
331
|
+
else
|
|
332
|
+
reject_early_data(:rejected_by_server)
|
|
333
|
+
@conductor.change_epochs!(cipher_suite: suite, write: :handshake)
|
|
334
|
+
end
|
|
335
|
+
case (ext = message.extension(Extension::RecordSizeLimit::TYPE))
|
|
336
|
+
when Extension::RecordSizeLimit then @peer_record_size_limit = ext.record_size_limit
|
|
337
|
+
end
|
|
338
|
+
message.extensions.each do |ext|
|
|
339
|
+
next if Config::RESERVED_EXTENSIONS.include?(ext.extension_type)
|
|
340
|
+
@conductor.event!(Event::PeerExtension.new(type: ext.extension_type, data: ext.encode, message: EncryptedExtensions::MSG_TYPE))
|
|
341
|
+
end
|
|
342
|
+
# A server that resumes authenticates by the PSK alone (RFC 9846
|
|
343
|
+
# "Certificate Request").
|
|
344
|
+
@conductor.transition!(@resumed ? :wait_finished : :wait_cert_cr)
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
# 2 and 3 of the checks of EncryptedExtensions, for one that takes
|
|
348
|
+
# 0-RTT: the PSK it was sent under selected, and the session's cipher
|
|
349
|
+
# suite and ALPN protocol, which 0-RTT was sent under too (RFC 9846
|
|
350
|
+
# "Early Data Indication").
|
|
351
|
+
private def check_early_data_accepted(alpn)
|
|
352
|
+
session = @session
|
|
353
|
+
return Violation.new(alert: :illegal_parameter, reason: :early_data_without_psk) unless @resumed && session
|
|
354
|
+
return Violation.new(alert: :illegal_parameter, reason: :early_data_cipher_suite_changed) unless suite.id == session.cipher_suite
|
|
355
|
+
return Violation.new(alert: :illegal_parameter, reason: :early_data_alpn_changed) unless alpn == session.alpn
|
|
356
|
+
nil
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
private def certificate_request(message)
|
|
360
|
+
# Empty during the handshake (RFC 9846 "Certificate Request").
|
|
361
|
+
return fail!(:decode_error, :certificate_request_context) unless message.certificate_request_context.empty?
|
|
362
|
+
@certificate_request = message
|
|
363
|
+
@conductor.transition!(:wait_cert)
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
private def certificate(message, now)
|
|
367
|
+
return fail!(:decode_error, :certificate_request_context) unless message.certificate_request_context.empty?
|
|
368
|
+
# RFC 9846 "Certificate": a server must send a certificate.
|
|
369
|
+
return fail!(:decode_error, :empty_certificate) if message.certificate_list.empty?
|
|
370
|
+
# We request neither OCSP stapling nor SCTs.
|
|
371
|
+
return fail!(:unsupported_extension, :unsolicited_extension) if message.certificate_list.any? { |entry| !entry.extensions.empty? }
|
|
372
|
+
result = @authentication.verify_certificate(message, keyring: @keyring, server_name: @server_name, now:,
|
|
373
|
+
max_chain_length: @config.max_certificate_chain_length, context: "".b)
|
|
374
|
+
case result
|
|
375
|
+
when AuthenticationMessages::Failure then fail!(result.alert, result.reason)
|
|
376
|
+
else
|
|
377
|
+
@peer_certificates = result
|
|
378
|
+
@conductor.transition!(:wait_cv)
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
private def certificate_verify(message, transcript_hash)
|
|
383
|
+
leaf = @peer_certificates.first or raise "no peer certificate to verify against"
|
|
384
|
+
result = @authentication.verify_certificate_verify(message, leaf, offered: @config.signature_algorithm_ids,
|
|
385
|
+
transcript_hash:)
|
|
386
|
+
case result
|
|
387
|
+
when AuthenticationMessages::Failure then fail!(result.alert, result.reason)
|
|
388
|
+
else @conductor.transition!(:wait_finished)
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
private def finished(message, transcript_hash, now)
|
|
393
|
+
schedule = key_schedule
|
|
394
|
+
case (result = @authentication.verify_finished(message, schedule, transcript_hash))
|
|
395
|
+
when AuthenticationMessages::Failure then return fail!(result.alert, result.reason)
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
schedule.main!
|
|
399
|
+
through_server_finished = @conductor.transcript_hash
|
|
400
|
+
server_secret = schedule.server_application_traffic_secret(through_server_finished)
|
|
401
|
+
client_secret = schedule.client_application_traffic_secret(through_server_finished)
|
|
402
|
+
@exporter_values = schedule.exporter_values(@config.exporter_labels, through_server_finished)
|
|
403
|
+
return unless @conductor.change_epochs!(cipher_suite: suite, read: :application,
|
|
404
|
+
secrets: [[:application, :read, server_secret], [:application, :write, client_secret]])
|
|
405
|
+
# The end of 0-RTT, under its key, where there is a record layer.
|
|
406
|
+
if @early_data == :accepted && @config.profile.send_end_of_early_data
|
|
407
|
+
@conductor.emit_message!(:early_data, EndOfEarlyData.new)
|
|
408
|
+
end
|
|
409
|
+
send_client_flight(schedule)
|
|
410
|
+
schedule.resumption!(@conductor.transcript_hash)
|
|
411
|
+
authenticated(now)
|
|
412
|
+
@conductor.change_epochs!(cipher_suite: suite, write: :application)
|
|
413
|
+
@conductor.transition!(:connected)
|
|
414
|
+
@conductor.event!(Event::HandshakeComplete.new(
|
|
415
|
+
server_name: @server_name, alpn: @alpn, cipher_suite: suite.name, group: @group, resumed: @resumed,
|
|
416
|
+
early_data_accepted: @early_data == :accepted, peer_certificates: @peer_certificates, peer_verified: @peer_verified
|
|
417
|
+
))
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# What the server was authenticated by: this handshake's Certificate,
|
|
421
|
+
# or, when it resumed, the full handshake the session comes from, whose
|
|
422
|
+
# chain and time a resumption carries on to the tickets it receives.
|
|
423
|
+
private def authenticated(now)
|
|
424
|
+
session = @session
|
|
425
|
+
if @resumed && session
|
|
426
|
+
@peer_certificates = session.peer_certificates.map { |der| Crypto::Certificate.parse(der) or raise "the session's chain was checked" }
|
|
427
|
+
@peer_verified = session.peer_verified
|
|
428
|
+
@authenticated_at = session.authenticated_at
|
|
429
|
+
else
|
|
430
|
+
@peer_verified = @keyring.peer_verified?
|
|
431
|
+
@authenticated_at = (now.to_r * 1000).floor
|
|
432
|
+
end
|
|
433
|
+
@session = nil
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
# Our Certificate and CertificateVerify when the server asked, then our
|
|
437
|
+
# Finished. With no identity that fits the request, an empty
|
|
438
|
+
# Certificate and no CertificateVerify (RFC 9846 "Certificate").
|
|
439
|
+
private def send_client_flight(schedule)
|
|
440
|
+
if (request = @certificate_request)
|
|
441
|
+
# @type var algorithms: Array[Integer]
|
|
442
|
+
algorithms = case (ext = request.extension(Extension::SignatureAlgorithms::TYPE))
|
|
443
|
+
when Extension::SignatureAlgorithms then ext.supported_signature_algorithms
|
|
444
|
+
else []
|
|
445
|
+
end
|
|
446
|
+
authorities = case (ext = request.extension(Extension::CertificateAuthorities::TYPE))
|
|
447
|
+
when Extension::CertificateAuthorities then ext.authorities
|
|
448
|
+
end
|
|
449
|
+
credential, scheme = @keyring.client_credential(signature_algorithms: algorithms, certificate_authorities: authorities)
|
|
450
|
+
@conductor.emit_message!(:handshake, @authentication.certificate(credential, context: request.certificate_request_context))
|
|
451
|
+
if credential && scheme
|
|
452
|
+
@conductor.emit_message!(:handshake, @authentication.certificate_verify(credential, scheme, @conductor.transcript_hash))
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
@conductor.emit_message!(:handshake, @authentication.finished(schedule, @conductor.transcript_hash))
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
# A ticket becomes a Session the application may keep, its age counted
|
|
459
|
+
# from now. With QUIC, max_early_data_size is checked first, a ticket
|
|
460
|
+
# ignored or not (RFC 9001 "Enabling 0-RTT"); a client that does not
|
|
461
|
+
# resume ignores it then (RFC 9846 "New Session Ticket Message").
|
|
462
|
+
private def new_session_ticket(message, now)
|
|
463
|
+
early_data = case (ext = message.extension(Extension::EarlyData::TYPE))
|
|
464
|
+
when Extension::EarlyData::NST then ext.max_early_data_size
|
|
465
|
+
end
|
|
466
|
+
if @config.profile.max_early_data_size_policy == :sentinel && early_data && early_data != EARLY_DATA_SENTINEL
|
|
467
|
+
return fail!(:illegal_parameter, :max_early_data_size_not_sentinel)
|
|
468
|
+
end
|
|
469
|
+
return unless @config.resumption
|
|
470
|
+
@conductor.event!(Event::NewSessionTicket.new(session: Session.new(
|
|
471
|
+
ticket: message.ticket, ticket_nonce: message.ticket_nonce, resumption_secret: key_schedule.resumption_secret,
|
|
472
|
+
cipher_suite: suite.id, issued_at: (now.to_r * 1000).floor, lifetime: message.ticket_lifetime,
|
|
473
|
+
age_add: message.ticket_age_add, max_early_data_size: early_data || 0, server_name: @server_name, alpn: @alpn,
|
|
474
|
+
peer_certificates: @peer_certificates.map(&:to_der), authenticated_at: @authenticated_at || raise("no handshake to resume"),
|
|
475
|
+
peer_verified: @peer_verified
|
|
476
|
+
)))
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
# The session to offer, or nil, with Event::SessionNotOffered, for one
|
|
481
|
+
# that fails a check.
|
|
482
|
+
private def session_to_offer(now)
|
|
483
|
+
session = @session or return nil
|
|
484
|
+
reason = session_refusal(session, now) or return session
|
|
485
|
+
@session = nil
|
|
486
|
+
@conductor.event!(Event::SessionNotOffered.new(reason:))
|
|
487
|
+
nil
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
# The first check, in order, that `session` fails at `now`: those after
|
|
491
|
+
# which no connection can use it come first. The chain is verified as
|
|
492
|
+
# for the name it was verified for, so that another name now is
|
|
493
|
+
# :server_name_mismatch; a name that is the same was valid for the
|
|
494
|
+
# leaf then and still is (RFC 9846 "New Session Ticket Message").
|
|
495
|
+
private def session_refusal(session, now)
|
|
496
|
+
return :ticket_expired unless session.usable?(now:)
|
|
497
|
+
chain = session.peer_certificates.map { |der| Crypto::Certificate.parse(der) }
|
|
498
|
+
return :untrusted_chain if chain.empty? || chain.include?(nil)
|
|
499
|
+
# @type var parsed: Array[OpenSSL::X509::Certificate]
|
|
500
|
+
parsed = chain.compact
|
|
501
|
+
# A certificate always has its dates; OpenSSL's signature allows nil.
|
|
502
|
+
return :certificate_expired if parsed.any? { |cert| cert.not_before > now || cert.not_after < now } # steep:ignore
|
|
503
|
+
return :unverified_origin unless session.peer_verified
|
|
504
|
+
result = @keyring.verifier.verify(chain: parsed, server_name: session.server_name, now:, purpose: :server)
|
|
505
|
+
raise UsageError, "a Verifier must return a Verifier::Result, not #{result.class}" unless result.is_a?(Verifier::Result)
|
|
506
|
+
return :untrusted_chain unless result.ok
|
|
507
|
+
return :server_name_mismatch unless session.server_name == @server_name
|
|
508
|
+
hash = CipherSuite.find(session.cipher_suite)&.hash_algorithm
|
|
509
|
+
offered = @config.cipher_suite_ids.any? { |id| CipherSuite.find(id)&.hash_algorithm == hash }
|
|
510
|
+
return :cipher_suite_unavailable unless hash && offered
|
|
511
|
+
nil
|
|
512
|
+
end
|
|
513
|
+
|
|
514
|
+
# `client_hello` with the session's PSK (RFC 9846 "Pre-Shared Key
|
|
515
|
+
# Extension"): one identity, its age as the client counts it, and
|
|
516
|
+
# pre_shared_key last, in place of the one a first ClientHello had;
|
|
517
|
+
# psk_dhe_ke is offered already. With `early_data`, early_data before
|
|
518
|
+
# it, and 0-RTT starts.
|
|
519
|
+
private def offer(client_hello, session, now, early_data: false)
|
|
520
|
+
suite = CipherSuite.find(session.cipher_suite) or raise "the session's cipher suite was checked"
|
|
521
|
+
identity = Extension::PreSharedKey::Identity.new(identity: session.ticket, obfuscated_ticket_age: session.obfuscated_ticket_age(now))
|
|
522
|
+
placeholder = Extension::PreSharedKey::CH.new(identities: [identity], binders: ["\x00".b * suite.hash_length])
|
|
523
|
+
others = client_hello.extensions.reject { |ext| ext.extension_type == Extension::PreSharedKey::TYPE }
|
|
524
|
+
others += [Extension::EarlyData::CH.new] if early_data
|
|
525
|
+
psk = @psk ||= session.psk
|
|
526
|
+
sent = @conductor.emit_client_hello_with_binders!(client_hello.with(extensions: others + [placeholder]), suite.hash_algorithm) do |transcript_hash|
|
|
527
|
+
schedule = KeySchedule.new(hash: suite.hash_algorithm, psk:)
|
|
528
|
+
[schedule.binder(kind: :resumption, transcript_hash:)].tap { schedule.discard! }
|
|
529
|
+
end
|
|
530
|
+
start_early_data(suite, sent, psk) if early_data
|
|
531
|
+
sent
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
# Whether to send 0-RTT with `session`: only when asked, and when the
|
|
535
|
+
# server could take it, the ticket allowing it and the session's cipher
|
|
536
|
+
# suite and ALPN protocol offered again (RFC 9846 "Early Data
|
|
537
|
+
# Indication"); otherwise traced why not.
|
|
538
|
+
private def early_data?(session)
|
|
539
|
+
return false unless @early_data_requested
|
|
540
|
+
alpn = session.alpn
|
|
541
|
+
reason = if session.max_early_data_size.zero? then :ticket_disallows
|
|
542
|
+
elsif !@config.cipher_suite_ids.include?(session.cipher_suite) then :cipher_suite_not_offered
|
|
543
|
+
elsif alpn && !@config.alpn.include?(alpn) then :alpn_not_offered
|
|
544
|
+
end
|
|
545
|
+
return true unless reason
|
|
546
|
+
@conductor.trace(Trace::EarlyDataNotAttempted.new(reason:)) if @conductor.tracing?
|
|
547
|
+
false
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# The client's 0-RTT key, from the PSK over the whole ClientHello (RFC
|
|
551
|
+
# 9846 "Key Schedule"), under the session's cipher suite.
|
|
552
|
+
private def start_early_data(suite, client_hello, psk)
|
|
553
|
+
schedule = @conductor.start_key_schedule(hash: suite.hash_algorithm, client_random: client_hello.random, psk:)
|
|
554
|
+
secret = schedule.client_early_traffic_secret(@conductor.transcript_hash(suite.hash_algorithm))
|
|
555
|
+
@conductor.change_epochs!(cipher_suite: suite, write: :early_data, secrets: [[:early_data, :write, secret]])
|
|
556
|
+
@early_data = :attempted
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
# 0-RTT that was sent is not taken; what was sent in it is not sent
|
|
560
|
+
# again here.
|
|
561
|
+
private def reject_early_data(reason)
|
|
562
|
+
return unless @early_data == :attempted
|
|
563
|
+
@early_data = :rejected
|
|
564
|
+
@conductor.event!(Event::EarlyDataRejected.new(reason:))
|
|
565
|
+
@conductor.trace(Trace::EarlyDataRejected.new(reason:)) if @conductor.tracing?
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
private def key_share_groups(client_hello)
|
|
569
|
+
# @type var none: Array[Integer]
|
|
570
|
+
none = []
|
|
571
|
+
case (ext = client_hello.extension(Extension::KeyShare::TYPE))
|
|
572
|
+
when Extension::KeyShare::CH then ext.client_shares.map(&:group)
|
|
573
|
+
else none
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
end
|
|
577
|
+
end
|
|
578
|
+
end
|