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,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "cipher_suite"
|
|
4
|
+
require_relative "key_schedule"
|
|
5
|
+
require_relative "masked_inspect"
|
|
6
|
+
|
|
7
|
+
module Raiha::TLS
|
|
8
|
+
# A session a client may resume, as a value: what a NewSessionTicket gave
|
|
9
|
+
# and what of the connection it came on a resumption needs. The
|
|
10
|
+
# application keeps it where it likes, `to_h` and `from_h` turning it
|
|
11
|
+
# into plain values and back.
|
|
12
|
+
#
|
|
13
|
+
# `issued_at` is when the client received the ticket and
|
|
14
|
+
# `authenticated_at` when the full handshake the session comes from took
|
|
15
|
+
# place, both in milliseconds of the wall clock; `cipher_suite` is the ID
|
|
16
|
+
# of the cipher suite; `peer_certificates` is the server's chain, DER,
|
|
17
|
+
# leaf first, and `peer_verified` whether it was verified (false when
|
|
18
|
+
# Verifier::None let it in); `server_name` and `alpn` are nil for none.
|
|
19
|
+
class Session < Data.define(
|
|
20
|
+
:ticket, :ticket_nonce, :resumption_secret, :cipher_suite,
|
|
21
|
+
:issued_at, :lifetime, :age_add, :max_early_data_size,
|
|
22
|
+
:server_name, :alpn, :peer_certificates, :authenticated_at, :peer_verified
|
|
23
|
+
)
|
|
24
|
+
include MaskedInspect
|
|
25
|
+
|
|
26
|
+
# RFC 9846 "New Session Ticket Message": no ticket is used more than
|
|
27
|
+
# 7 days after it was issued, and here no key material more than 7
|
|
28
|
+
# days after the full handshake it comes from.
|
|
29
|
+
MAX_AGE = 604_800
|
|
30
|
+
|
|
31
|
+
def masked_members = %i[resumption_secret]
|
|
32
|
+
|
|
33
|
+
def self.from_h(hash)
|
|
34
|
+
raise ArgumentError, "a session is a Hash" unless hash.is_a?(Hash)
|
|
35
|
+
bytes = ->(name) { hash.fetch(name).then { |v| v.is_a?(String) ? v.b.freeze : raise(ArgumentError, "#{name} must be a String") } }
|
|
36
|
+
integer = ->(name) { hash.fetch(name).then { |v| v.is_a?(Integer) && !v.negative? ? v : raise(ArgumentError, "#{name} must be an Integer") } }
|
|
37
|
+
optional = ->(name) { hash.fetch(name).nil? ? nil : bytes.(name) }
|
|
38
|
+
verified = hash.fetch(:peer_verified)
|
|
39
|
+
raise ArgumentError, "peer_verified must be true or false" unless verified == true || verified == false
|
|
40
|
+
certificates = hash.fetch(:peer_certificates)
|
|
41
|
+
raise ArgumentError, "peer_certificates must be an Array of Strings" unless certificates.is_a?(Array) && certificates.all?(String)
|
|
42
|
+
raise ArgumentError, "unknown cipher suite" unless CipherSuite.find(integer.(:cipher_suite))
|
|
43
|
+
new(ticket: bytes.(:ticket), ticket_nonce: bytes.(:ticket_nonce), resumption_secret: bytes.(:resumption_secret),
|
|
44
|
+
cipher_suite: integer.(:cipher_suite), issued_at: integer.(:issued_at), lifetime: integer.(:lifetime),
|
|
45
|
+
age_add: integer.(:age_add), max_early_data_size: integer.(:max_early_data_size), server_name: optional.(:server_name),
|
|
46
|
+
alpn: optional.(:alpn), peer_certificates: certificates.map { |der| der.b.freeze }.freeze,
|
|
47
|
+
authenticated_at: integer.(:authenticated_at), peer_verified: verified)
|
|
48
|
+
rescue KeyError => e
|
|
49
|
+
raise ArgumentError, "a session lacks #{e.key}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Whether the session may still be offered at `now`: neither the ticket
|
|
53
|
+
# nor the key material it carries is past its age.
|
|
54
|
+
def usable?(now:)
|
|
55
|
+
now_ms = (now.to_r * 1000).floor
|
|
56
|
+
now_ms - issued_at <= [lifetime, MAX_AGE].min * 1000 && now_ms - authenticated_at <= MAX_AGE * 1000
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# The PSK of the ticket (RFC 9846 "New Session Ticket Message").
|
|
60
|
+
def psk
|
|
61
|
+
suite = CipherSuite.find(cipher_suite) or raise ArgumentError, "unknown cipher suite"
|
|
62
|
+
KeySchedule.expand_label(secret: resumption_secret, label: KeySchedule::LABELS.fetch(:ticket_psk), context: ticket_nonce,
|
|
63
|
+
length: suite.hash_length, hash: suite.hash_algorithm)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The obfuscated_ticket_age to offer at `now` (RFC 9846 "Pre-Shared Key
|
|
67
|
+
# Extension"): the milliseconds since the ticket came, plus age_add,
|
|
68
|
+
# modulo 2^32.
|
|
69
|
+
def obfuscated_ticket_age(now) = ((now.to_r * 1000).floor - issued_at + age_add) % (1 << 32)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
module SessionStore
|
|
73
|
+
# Sessions kept in memory by server name, the newest 8 of each. A
|
|
74
|
+
# session is taken out to be offered, as a ticket offered once is not to
|
|
75
|
+
# be offered again (RFC 9846 "Client and Server Tracking Prevention").
|
|
76
|
+
class Memory
|
|
77
|
+
PER_SERVER = 8
|
|
78
|
+
|
|
79
|
+
def initialize
|
|
80
|
+
@sessions = {} #: Hash[String?, Array[Session]]
|
|
81
|
+
@lock = Thread::Mutex.new
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def put(session)
|
|
85
|
+
@lock.synchronize do
|
|
86
|
+
kept = @sessions[session.server_name] ||= []
|
|
87
|
+
kept << session
|
|
88
|
+
kept.shift while kept.size > PER_SERVER
|
|
89
|
+
end
|
|
90
|
+
self
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The newest session for `server_name` still usable at `now`, taken
|
|
94
|
+
# out; sessions past their age are dropped on the way.
|
|
95
|
+
def take(server_name, now:)
|
|
96
|
+
@lock.synchronize do
|
|
97
|
+
kept = @sessions[server_name] or return nil
|
|
98
|
+
while (session = kept.pop)
|
|
99
|
+
return session if session.usable?(now:)
|
|
100
|
+
end
|
|
101
|
+
nil
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::TLS
|
|
4
|
+
# A SignatureScheme of RFC 9846: the signature algorithm, the hash it uses
|
|
5
|
+
# and, for ECDSA and EdDSA, the curve the key must be on.
|
|
6
|
+
#
|
|
7
|
+
# RSASSA-PKCS1-v1_5 and SHA-1 may sign certificates but never a
|
|
8
|
+
# CertificateVerify, which `#usable_in_certificate_verify?` says.
|
|
9
|
+
class SignatureScheme < Data.define(:id, :name, :family, :hash_algorithm, :curve)
|
|
10
|
+
def usable_in_certificate_verify? = family != :rsa_pkcs1 && hash_algorithm != :sha1
|
|
11
|
+
|
|
12
|
+
SCHEMES = [
|
|
13
|
+
new(id: 0x0401, name: :rsa_pkcs1_sha256, family: :rsa_pkcs1, hash_algorithm: :sha256, curve: nil),
|
|
14
|
+
new(id: 0x0501, name: :rsa_pkcs1_sha384, family: :rsa_pkcs1, hash_algorithm: :sha384, curve: nil),
|
|
15
|
+
new(id: 0x0601, name: :rsa_pkcs1_sha512, family: :rsa_pkcs1, hash_algorithm: :sha512, curve: nil),
|
|
16
|
+
new(id: 0x0403, name: :ecdsa_secp256r1_sha256, family: :ecdsa, hash_algorithm: :sha256, curve: :secp256r1),
|
|
17
|
+
new(id: 0x0503, name: :ecdsa_secp384r1_sha384, family: :ecdsa, hash_algorithm: :sha384, curve: :secp384r1),
|
|
18
|
+
new(id: 0x0603, name: :ecdsa_secp521r1_sha512, family: :ecdsa, hash_algorithm: :sha512, curve: :secp521r1),
|
|
19
|
+
new(id: 0x0804, name: :rsa_pss_rsae_sha256, family: :rsa_pss_rsae, hash_algorithm: :sha256, curve: nil),
|
|
20
|
+
new(id: 0x0805, name: :rsa_pss_rsae_sha384, family: :rsa_pss_rsae, hash_algorithm: :sha384, curve: nil),
|
|
21
|
+
new(id: 0x0806, name: :rsa_pss_rsae_sha512, family: :rsa_pss_rsae, hash_algorithm: :sha512, curve: nil),
|
|
22
|
+
new(id: 0x0807, name: :ed25519, family: :eddsa, hash_algorithm: nil, curve: :ed25519),
|
|
23
|
+
new(id: 0x0808, name: :ed448, family: :eddsa, hash_algorithm: nil, curve: :ed448),
|
|
24
|
+
new(id: 0x0809, name: :rsa_pss_pss_sha256, family: :rsa_pss_pss, hash_algorithm: :sha256, curve: nil),
|
|
25
|
+
new(id: 0x080a, name: :rsa_pss_pss_sha384, family: :rsa_pss_pss, hash_algorithm: :sha384, curve: nil),
|
|
26
|
+
new(id: 0x080b, name: :rsa_pss_pss_sha512, family: :rsa_pss_pss, hash_algorithm: :sha512, curve: nil),
|
|
27
|
+
# Legacy: for certificate signatures only.
|
|
28
|
+
new(id: 0x0201, name: :rsa_pkcs1_sha1, family: :rsa_pkcs1, hash_algorithm: :sha1, curve: nil),
|
|
29
|
+
new(id: 0x0203, name: :ecdsa_sha1, family: :ecdsa, hash_algorithm: :sha1, curve: nil)
|
|
30
|
+
].to_h { |scheme| [scheme.id, scheme] }.freeze
|
|
31
|
+
|
|
32
|
+
# nil for a code point this table does not define, GREASE included.
|
|
33
|
+
def self.find(id) = SCHEMES[id]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../error"
|
|
4
|
+
require_relative "../iana"
|
|
5
|
+
require_relative "../profile"
|
|
6
|
+
require_relative "../record"
|
|
7
|
+
require_relative "../handshake/acceptor"
|
|
8
|
+
|
|
9
|
+
module Raiha::TLS
|
|
10
|
+
module TCP
|
|
11
|
+
# The start of a TCP server's handshake, before a Config is chosen: the
|
|
12
|
+
# Handshake::Acceptor behind the record layer. It reads plaintext records
|
|
13
|
+
# until a ClientHello is whole and offers it as `accepted`; the
|
|
14
|
+
# application picks a Config and a Keyring by it and passes this to
|
|
15
|
+
# TCP::Connection.server, or turns the client down with `reject`.
|
|
16
|
+
#
|
|
17
|
+
# Bytes that come after the ClientHello are kept as they came, up to
|
|
18
|
+
# MAX_HELD_BYTES, for the Connection to read first. Bytes from the peer
|
|
19
|
+
# never raise.
|
|
20
|
+
class Acceptor
|
|
21
|
+
MAX_HELD_BYTES = 64 * 1024
|
|
22
|
+
|
|
23
|
+
def initialize
|
|
24
|
+
@acceptor = Handshake::Acceptor.new
|
|
25
|
+
@record = Record::Layer.new(profile: Profile::TCP)
|
|
26
|
+
@outgoing = "".b
|
|
27
|
+
@alert = nil #: Symbol?
|
|
28
|
+
@reason = nil #: Symbol?
|
|
29
|
+
@peer_alerted = false
|
|
30
|
+
@handed_over = false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Bytes read from the socket, in any pieces.
|
|
34
|
+
def receive(bytes, now:)
|
|
35
|
+
return nil if failed?
|
|
36
|
+
@record << bytes
|
|
37
|
+
return hold if @acceptor.accepted
|
|
38
|
+
until failed? || @acceptor.accepted || (record = @record.read(epoch: :initial)).nil?
|
|
39
|
+
case record
|
|
40
|
+
when Record::Failure then fail!(record.alert, record.reason)
|
|
41
|
+
else take(record, now)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
hold if @acceptor.accepted
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The bytes to write to the socket, if any: only ever an alert.
|
|
49
|
+
def each_output
|
|
50
|
+
return if @outgoing.empty?
|
|
51
|
+
bytes = @outgoing
|
|
52
|
+
@outgoing = "".b
|
|
53
|
+
yield bytes
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The ClientHello once whole and valid; nil before, and after a failure.
|
|
57
|
+
def accepted = failed? ? nil : @acceptor.accepted
|
|
58
|
+
|
|
59
|
+
# Turns the ClientHello down with one of Handshake::Acceptor::REJECT_ALERTS.
|
|
60
|
+
def reject(description)
|
|
61
|
+
@acceptor.reject(description)
|
|
62
|
+
send_alerts
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def failed? = !@alert.nil? || @peer_alerted || @acceptor.failed?
|
|
66
|
+
|
|
67
|
+
# The alert sent, if any: none answers the peer's own alert.
|
|
68
|
+
def alert = @alert || @acceptor.alert
|
|
69
|
+
|
|
70
|
+
def reason = @reason || @acceptor.reason
|
|
71
|
+
|
|
72
|
+
# What a Connection takes over, once: the ClientHello and the record
|
|
73
|
+
# layer holding the bytes that came after it.
|
|
74
|
+
def hand_over
|
|
75
|
+
accepted = self.accepted or raise UsageError, "the Acceptor has accepted no ClientHello"
|
|
76
|
+
raise UsageError, "the Acceptor has been handed over already" if @handed_over
|
|
77
|
+
@handed_over = true
|
|
78
|
+
[accepted, @record]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Only handshake records come before the ClientHello is whole, and none
|
|
82
|
+
# of another type in between its pieces (RFC 9846 "Record Layer").
|
|
83
|
+
private def take(record, now)
|
|
84
|
+
case record.content_type
|
|
85
|
+
when :handshake
|
|
86
|
+
@acceptor.receive(record.payload, epoch: :initial, now:)
|
|
87
|
+
send_alerts
|
|
88
|
+
when :alert
|
|
89
|
+
# The client gave up; there is nobody to answer.
|
|
90
|
+
@peer_alerted = true
|
|
91
|
+
@reason = :peer_alert
|
|
92
|
+
@record.discard!
|
|
93
|
+
else
|
|
94
|
+
fail!(:unexpected_message, :unexpected_record)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The bytes after the ClientHello wait for the Connection, but not
|
|
99
|
+
# without bound (the Acceptor may wait long for the application).
|
|
100
|
+
private def hold
|
|
101
|
+
fail!(:unexpected_message, :too_much_data_before_accept) if @record.pending_bytes > MAX_HELD_BYTES
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private def send_alerts
|
|
106
|
+
@acceptor.each_output do |output|
|
|
107
|
+
@outgoing << @record.write(content_type: :alert, payload: [2, IANA.alert_code(output.description)].pack("C2"), epoch: :initial)
|
|
108
|
+
end
|
|
109
|
+
nil
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private def fail!(alert, reason)
|
|
113
|
+
@alert = alert
|
|
114
|
+
@reason = reason
|
|
115
|
+
@outgoing << @record.write(content_type: :alert, payload: [2, IANA.alert_code(alert)].pack("C2"), epoch: :initial)
|
|
116
|
+
@record.discard!
|
|
117
|
+
nil
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../error"
|
|
4
|
+
require_relative "../event"
|
|
5
|
+
require_relative "../iana"
|
|
6
|
+
require_relative "../output"
|
|
7
|
+
require_relative "../record"
|
|
8
|
+
require_relative "../trace"
|
|
9
|
+
require_relative "../handshake/machine"
|
|
10
|
+
require_relative "event"
|
|
11
|
+
|
|
12
|
+
module Raiha::TLS
|
|
13
|
+
module TCP
|
|
14
|
+
# TLS over TCP without I/O: a handshake Machine and a Record::Layer put
|
|
15
|
+
# together. Bytes read from the socket go into `receive`; the bytes to
|
|
16
|
+
# write come out of `each_output`. The application's data goes in by
|
|
17
|
+
# `write` and comes out of `read`.
|
|
18
|
+
#
|
|
19
|
+
# Bytes from the peer never raise: a failure sends an alert, if there is
|
|
20
|
+
# one to send, and shows as an event. Writing to or reading from a
|
|
21
|
+
# connection that failed raises UsageError.
|
|
22
|
+
class Connection
|
|
23
|
+
# How much of the application's data `write` holds before it goes out:
|
|
24
|
+
# held until the handshake completes, then until `each_output` takes
|
|
25
|
+
# it. Past this `write` takes less, or nothing.
|
|
26
|
+
WRITE_BUFFER_LIMIT = 64 * 1024
|
|
27
|
+
|
|
28
|
+
ALERT_LEVELS = { warning: 1, fatal: 2 }.freeze #: Hash[Symbol, Integer]
|
|
29
|
+
|
|
30
|
+
# user_canceled alerts read on in a row, without application data
|
|
31
|
+
# between them; one more ends the connection. BoringSSL's
|
|
32
|
+
# kMaxWarningAlerts, which rustls uses too.
|
|
33
|
+
MAX_CONSECUTIVE_WARNING_ALERTS = 4
|
|
34
|
+
|
|
35
|
+
# Event::NewSessionTickets held until taken: one past this drops the
|
|
36
|
+
# oldest, so that a server sending tickets without end cannot make a
|
|
37
|
+
# client keep them all.
|
|
38
|
+
MAX_HELD_TICKETS = 8
|
|
39
|
+
|
|
40
|
+
# A client resumes `session` if it still may (Machine.client), and
|
|
41
|
+
# with `early_data: true` sends 0-RTT through `write_early_data` when it
|
|
42
|
+
# can.
|
|
43
|
+
def self.client(config:, keyring:, server_name:, now:, extensions: [], tracer: nil, keylog: nil, session: nil, early_data: false)
|
|
44
|
+
machine = Handshake::Machine.client(config:, keyring:, server_name:, now:, extensions:, tracer:, keylog:, session:, early_data:)
|
|
45
|
+
new(machine, config, early_data_room: session&.max_early_data_size || 0, tracer:)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# A server takes its ClientHello through `receive`, or from an
|
|
49
|
+
# Acceptor that has offered one: then with the bytes that came after it,
|
|
50
|
+
# read before anything given to `receive`. `anti_replay` is the record
|
|
51
|
+
# of the 0-RTT taken (Machine.server), which a Config that takes 0-RTT
|
|
52
|
+
# needs.
|
|
53
|
+
def self.server(config:, keyring:, now:, acceptor: nil, extensions: [], tracer: nil, keylog: nil, anti_replay: nil)
|
|
54
|
+
raise ArgumentError, "a TCP::Connection needs a profile with a record layer, as Profile::TCP" unless config.profile.record_layer
|
|
55
|
+
unless acceptor
|
|
56
|
+
return new(Handshake::Machine.server(config:, keyring:, now:, extensions:, tracer:, keylog:, anti_replay:), config, tracer:)
|
|
57
|
+
end
|
|
58
|
+
accepted, record = acceptor.hand_over
|
|
59
|
+
machine = Handshake::Machine.server(config:, keyring:, client_hello: accepted, now:, extensions:, tracer:, keylog:, anti_replay:)
|
|
60
|
+
new(machine, config, record:, client_hello_received: true, tracer:).tap { |connection| connection.receive("".b, now:) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private_class_method :new
|
|
64
|
+
|
|
65
|
+
def initialize(machine, config, record: Record::Layer.new(profile: config.profile), client_hello_received: false, early_data_room: 0,
|
|
66
|
+
tracer: nil)
|
|
67
|
+
@machine = machine
|
|
68
|
+
@config = config
|
|
69
|
+
@tracer = tracer
|
|
70
|
+
@record = record
|
|
71
|
+
# How much more 0-RTT the session lets a client send.
|
|
72
|
+
@early_data_room = early_data_room
|
|
73
|
+
@outgoing = "".b
|
|
74
|
+
@unsent = "".b
|
|
75
|
+
@received = "".b
|
|
76
|
+
# The client's 0-RTT a server took, until read, and how much came.
|
|
77
|
+
@early_received = "".b
|
|
78
|
+
@early_data_size = 0
|
|
79
|
+
# @type var events: Array[event]
|
|
80
|
+
events = []
|
|
81
|
+
@events = events
|
|
82
|
+
@failed = false
|
|
83
|
+
@read_closed = false
|
|
84
|
+
@write_closed = false
|
|
85
|
+
@consecutive_warning_alerts = 0
|
|
86
|
+
@handshake_received = client_hello_received
|
|
87
|
+
@record_size_limits_set = false
|
|
88
|
+
@change_cipher_spec_sent = !config.profile.compatibility_mode
|
|
89
|
+
limit_record_sizes
|
|
90
|
+
drain_machine
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Bytes read from the socket, in any pieces. After the peer's
|
|
94
|
+
# close_notify they are ignored.
|
|
95
|
+
def receive(bytes, now:)
|
|
96
|
+
return nil if @failed || @read_closed
|
|
97
|
+
@record << bytes
|
|
98
|
+
until @failed || @read_closed || (record = @record.read(epoch: @machine.read_epoch, skip_early_data: @machine.skip_early_data_limit)).nil?
|
|
99
|
+
case record
|
|
100
|
+
when Record::Failure then fail!(record.alert, record.reason)
|
|
101
|
+
else take(record, now)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
nil
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The bytes to write to the socket, if any.
|
|
108
|
+
def each_output
|
|
109
|
+
return if @outgoing.empty?
|
|
110
|
+
bytes = @outgoing
|
|
111
|
+
@outgoing = "".b
|
|
112
|
+
yield bytes
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Takes as much of `data` as fits WRITE_BUFFER_LIMIT and returns how
|
|
116
|
+
# much that was. Before the handshake completes it is held, and sent
|
|
117
|
+
# as 1-RTT data once it does.
|
|
118
|
+
def write(data)
|
|
119
|
+
raise UsageError, "the connection failed" if @failed
|
|
120
|
+
raise UsageError, "our side is closed" if @write_closed
|
|
121
|
+
room = WRITE_BUFFER_LIMIT - @unsent.bytesize - @outgoing.bytesize
|
|
122
|
+
taken = data.byteslice(0, room.clamp(0..)).to_s # steep:ignore
|
|
123
|
+
@unsent << taken
|
|
124
|
+
send_application_data
|
|
125
|
+
taken.bytesize
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Sends as much of `data` as 0-RTT as the session allows and returns how
|
|
129
|
+
# much that was, or nil when 0-RTT is over or never began: a client
|
|
130
|
+
# given `early_data: true` sends it from the start until the server
|
|
131
|
+
# takes it or not (RFC 9846 "Early Data Indication"). 0-RTT the server
|
|
132
|
+
# did not take is not sent again (Event::EarlyDataRejected), and may
|
|
133
|
+
# be replayed: send in it only what may be.
|
|
134
|
+
def write_early_data(data)
|
|
135
|
+
raise UsageError, "the connection failed" if @failed
|
|
136
|
+
raise UsageError, "our side is closed" if @write_closed
|
|
137
|
+
return nil unless @machine.write_epoch == :early_data
|
|
138
|
+
taken = data.byteslice(0, @early_data_room).to_s
|
|
139
|
+
@early_data_room -= taken.bytesize
|
|
140
|
+
send_record(:application_data, taken, :early_data) unless taken.empty?
|
|
141
|
+
taken.bytesize
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Up to `max_bytes` of the peer's application data, "" when none has
|
|
145
|
+
# come, and nil once the peer has closed its side and all is read. The
|
|
146
|
+
# client's 0-RTT is read first, by `read_early_data`.
|
|
147
|
+
def read(max_bytes)
|
|
148
|
+
raise UsageError, "0-RTT is left to read_early_data" unless @early_received.empty?
|
|
149
|
+
return nil if @read_closed && @received.empty?
|
|
150
|
+
raise UsageError, "the connection failed" if @failed && @received.empty?
|
|
151
|
+
@received.slice!(0, max_bytes).to_s
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Up to `max_bytes` of the client's 0-RTT that a server took, "" when
|
|
155
|
+
# none has come yet, and nil once it is over (EndOfEarlyData came and
|
|
156
|
+
# all is read) or there is none, not offered or not taken. It may be a
|
|
157
|
+
# replay of another connection's (RFC 9846 "Replay Attacks on 0-RTT"):
|
|
158
|
+
# `handshake_complete?` says when the client has proved it is live.
|
|
159
|
+
def read_early_data(max_bytes)
|
|
160
|
+
raise UsageError, "the connection failed" if @failed && @early_received.empty?
|
|
161
|
+
return @early_received.slice!(0, max_bytes).to_s unless @early_received.empty?
|
|
162
|
+
@machine.read_epoch == :early_data ? "".b : nil
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# A KeyUpdate of our own, and of the peer's too with
|
|
166
|
+
# `request_peer_update: true` (Machine#request_key_update). Raises
|
|
167
|
+
# UsageError before the handshake completes and once the connection
|
|
168
|
+
# failed.
|
|
169
|
+
def request_key_update(request_peer_update: false)
|
|
170
|
+
raise UsageError, "the connection failed" if @failed
|
|
171
|
+
raise UsageError, "our side is closed" if @write_closed
|
|
172
|
+
@machine.request_key_update(request_peer_update:)
|
|
173
|
+
drain_machine
|
|
174
|
+
nil
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Closes our side (RFC 9846 "Closure Alerts"): a close_notify under the
|
|
178
|
+
# epoch the peer reads, and nothing more after it, not even an alert.
|
|
179
|
+
# The peer's side stays open. Data held for a handshake that has not
|
|
180
|
+
# completed is dropped.
|
|
181
|
+
def close_notify
|
|
182
|
+
raise UsageError, "the connection failed" if @failed
|
|
183
|
+
return nil if @write_closed
|
|
184
|
+
send_alert(@machine.write_epoch, :warning, :close_notify)
|
|
185
|
+
@write_closed = true
|
|
186
|
+
@unsent.clear
|
|
187
|
+
nil
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def handshake_complete? = @machine.complete?
|
|
191
|
+
|
|
192
|
+
def failed? = @failed
|
|
193
|
+
|
|
194
|
+
# The Machine's events, and Event::PeerAlert, in the order they came.
|
|
195
|
+
def next_event = @events.shift
|
|
196
|
+
|
|
197
|
+
private def take(record, now)
|
|
198
|
+
case record.content_type
|
|
199
|
+
when :handshake
|
|
200
|
+
@handshake_received = true
|
|
201
|
+
@machine.receive(record.payload, epoch: @machine.read_epoch, now:)
|
|
202
|
+
limit_record_sizes
|
|
203
|
+
drain_machine
|
|
204
|
+
send_application_data
|
|
205
|
+
when :change_cipher_spec
|
|
206
|
+
# RFC 9846 "Record Protocol": dropped once the first ClientHello
|
|
207
|
+
# is out or in; the record layer has refused it after the peer's
|
|
208
|
+
# Finished.
|
|
209
|
+
return fail!(:unexpected_message, :unexpected_change_cipher_spec) if !@handshake_received && @config.server?
|
|
210
|
+
fail!(:unexpected_message, :interleaved_record) if @machine.partial_message?
|
|
211
|
+
when :alert
|
|
212
|
+
return fail!(:unexpected_message, :interleaved_record) if @machine.partial_message?
|
|
213
|
+
receive_alert(record.payload)
|
|
214
|
+
when :application_data
|
|
215
|
+
return fail!(:unexpected_message, :interleaved_record) if @machine.partial_message?
|
|
216
|
+
return take_early_data(record.payload) if @machine.read_epoch == :early_data
|
|
217
|
+
return fail!(:unexpected_message, :application_data_before_handshake) unless @machine.read_epoch == :application
|
|
218
|
+
@received << record.payload
|
|
219
|
+
# The peer's runs of KeyUpdates and of user_canceled end with data
|
|
220
|
+
# that is not empty.
|
|
221
|
+
unless record.payload.empty?
|
|
222
|
+
@machine.application_data_received
|
|
223
|
+
@consecutive_warning_alerts = 0
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# The client's 0-RTT, as far as the Config takes (RFC 9846 "Early Data
|
|
229
|
+
# Indication").
|
|
230
|
+
private def take_early_data(payload)
|
|
231
|
+
@early_data_size += payload.bytesize
|
|
232
|
+
return fail!(:unexpected_message, :too_much_early_data) if @early_data_size > @config.max_early_data_size
|
|
233
|
+
@early_received << payload
|
|
234
|
+
nil
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# RFC 8449, once the Machine has seen the peer's record_size_limit and
|
|
238
|
+
# before the records it lets out: ours keep to it, and if we sent one
|
|
239
|
+
# too, the peer's keep to ours. A server learns the client's from the
|
|
240
|
+
# ClientHello, before its first flight.
|
|
241
|
+
private def limit_record_sizes
|
|
242
|
+
return if @record_size_limits_set
|
|
243
|
+
peer = @machine.peer_record_size_limit or return
|
|
244
|
+
@record.limit_sizes(send: peer, receive: @config.record_size_limit || Record::DEFAULT_SIZE_LIMIT)
|
|
245
|
+
@record_size_limits_set = true
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# RFC 9846 "Closure Alerts": close_notify after the handshake closes
|
|
249
|
+
# the peer's side only; ours stays open and sends nothing back, as
|
|
250
|
+
# answering at once is what TLS 1.2 did, inviting truncation. During
|
|
251
|
+
# the handshake it is a failure, unauthenticated as the peer still is.
|
|
252
|
+
# RFC 9846 "Error Alerts": every other alert ends the connection,
|
|
253
|
+
# whatever its level, and none is sent back, but for user_canceled,
|
|
254
|
+
# after which we read on until the close_notify that should follow.
|
|
255
|
+
private def receive_alert(payload)
|
|
256
|
+
return fail!(:decode_error, :malformed_alert) unless payload.bytesize == 2
|
|
257
|
+
level, description = payload.unpack("C2").map(&:to_i)
|
|
258
|
+
if IANA.alert_name(description) == :close_notify # steep:ignore
|
|
259
|
+
return close_read if @machine.complete?
|
|
260
|
+
@machine.discard
|
|
261
|
+
@events << ::Raiha::TLS::Event::Failed.new(alert: nil, reason: :peer_closed_during_handshake)
|
|
262
|
+
return stop
|
|
263
|
+
end
|
|
264
|
+
if IANA.alert_name(description) == :user_canceled # steep:ignore
|
|
265
|
+
@consecutive_warning_alerts += 1
|
|
266
|
+
return fail!(:unexpected_message, :too_many_warning_alerts) if @consecutive_warning_alerts > MAX_CONSECUTIVE_WARNING_ALERTS
|
|
267
|
+
@events << Event::PeerAlert.new(level:, description:) # steep:ignore
|
|
268
|
+
return nil
|
|
269
|
+
end
|
|
270
|
+
@events << Event::PeerAlert.new(level:, description:) # steep:ignore
|
|
271
|
+
@machine.discard
|
|
272
|
+
stop
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
private def close_read
|
|
276
|
+
@read_closed = true
|
|
277
|
+
nil
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# The Machine's outputs in queue order: each Secret into the key
|
|
281
|
+
# table, each run of Messages of one epoch into as few records as fit,
|
|
282
|
+
# and an alert, after which nothing more is sent.
|
|
283
|
+
private def drain_machine
|
|
284
|
+
runs = @machine.outputs.chunk_while do |a, b|
|
|
285
|
+
a.is_a?(Output::Message) && b.is_a?(Output::Message) && a.epoch == b.epoch
|
|
286
|
+
end
|
|
287
|
+
runs.each do |run|
|
|
288
|
+
case (output = run.first)
|
|
289
|
+
when Output::Message then send_record(:handshake, run.filter_map { |message| message.bytes if message.is_a?(Output::Message) }.join, output.epoch)
|
|
290
|
+
when Output::Secret
|
|
291
|
+
@record.install(direction: output.direction, epoch: output.epoch, secret: output.secret, cipher_suite: output.cipher_suite)
|
|
292
|
+
when Output::Alert then send_alert(output.epoch, output.level, output.description)
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
while (event = @machine.next_event)
|
|
296
|
+
make_room_for_a_ticket if event.is_a?(::Raiha::TLS::Event::NewSessionTicket)
|
|
297
|
+
@events << event
|
|
298
|
+
end
|
|
299
|
+
stop if @machine.failed?
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
private def make_room_for_a_ticket
|
|
303
|
+
held = @events.count { |event| event.is_a?(::Raiha::TLS::Event::NewSessionTicket) }
|
|
304
|
+
return if held < MAX_HELD_TICKETS
|
|
305
|
+
oldest = @events.index { |event| event.is_a?(::Raiha::TLS::Event::NewSessionTicket) } or return
|
|
306
|
+
@events.delete_at(oldest)
|
|
307
|
+
@tracer&.tls_trace(Trace::TicketIgnored.new(reason: :too_many_tickets))
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
private def send_alert(epoch, level, description)
|
|
311
|
+
send_record(:alert, [ALERT_LEVELS.fetch(level), IANA.alert_code(description)].pack("C2"), epoch)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Middlebox compatibility (RFC 9846 "Middlebox Compatibility Mode"): a
|
|
315
|
+
# client sends one ChangeCipherSpec before its first protected record,
|
|
316
|
+
# a server right after its first handshake record.
|
|
317
|
+
private def send_record(content_type, payload, epoch)
|
|
318
|
+
return if @write_closed
|
|
319
|
+
change_cipher_spec if @config.client? && epoch != :initial
|
|
320
|
+
@outgoing << @record.write(content_type:, payload:, epoch:)
|
|
321
|
+
change_cipher_spec if @config.server? && content_type == :handshake
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
private def change_cipher_spec
|
|
325
|
+
return if @change_cipher_spec_sent
|
|
326
|
+
@change_cipher_spec_sent = true
|
|
327
|
+
@outgoing << @record.write(content_type: :change_cipher_spec, payload: "\x01".b, epoch: :initial)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# A KeyUpdate the peer asked for first (RFC 9846 "Key and
|
|
331
|
+
# Initialization Vector Update"), then one record at a time, so the key
|
|
332
|
+
# can change between two.
|
|
333
|
+
private def send_application_data
|
|
334
|
+
return if @unsent.empty? || !@machine.complete? || @failed
|
|
335
|
+
@machine.before_application_data
|
|
336
|
+
drain_machine
|
|
337
|
+
until @unsent.empty?
|
|
338
|
+
update_keys_at_the_limit
|
|
339
|
+
chunk = @unsent.byteslice(0, @record.fragment_size(:application)).to_s
|
|
340
|
+
@unsent = @unsent.byteslice(chunk.bytesize..).to_s
|
|
341
|
+
@outgoing << @record.write(content_type: :application_data, payload: chunk, epoch: :application)
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# RFC 9846 "Limits on Key Usage": the key changes before it reaches its
|
|
346
|
+
# suite's limit, by a KeyUpdate under the old key.
|
|
347
|
+
private def update_keys_at_the_limit
|
|
348
|
+
return unless @record.key_update_due?(:application)
|
|
349
|
+
@machine.request_key_update(request_peer_update: false)
|
|
350
|
+
drain_machine
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# A failure found here rather than in the Machine: the alert, under the
|
|
354
|
+
# epoch the peer reads, and Event::Failed.
|
|
355
|
+
private def fail!(alert, reason)
|
|
356
|
+
send_alert(@machine.write_epoch, :fatal, alert)
|
|
357
|
+
@machine.discard
|
|
358
|
+
@events << ::Raiha::TLS::Event::Failed.new(alert:, reason:)
|
|
359
|
+
stop
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# Nothing more is sent or taken, and the keys are forgotten.
|
|
363
|
+
private def stop
|
|
364
|
+
@failed = true
|
|
365
|
+
@record.discard!
|
|
366
|
+
@unsent.clear
|
|
367
|
+
nil
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
end
|
|
371
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::TLS
|
|
4
|
+
module TCP
|
|
5
|
+
# What TCP::Connection tells the application on top of the Machine's
|
|
6
|
+
# events. Alerts never reach the Machine, so these are the binding's.
|
|
7
|
+
module Event
|
|
8
|
+
# An alert the peer sent. Both values are the peer's, so they stay
|
|
9
|
+
# Integers: the description may be one this implementation does not
|
|
10
|
+
# know (IANA.alert_name names the known ones).
|
|
11
|
+
class PeerAlert < Data.define(:level, :description)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|