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,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../crypto"
|
|
4
|
+
require_relative "../error"
|
|
5
|
+
require_relative "../signature_scheme"
|
|
6
|
+
require_relative "../trace"
|
|
7
|
+
require_relative "../verifier"
|
|
8
|
+
require_relative "certificate"
|
|
9
|
+
require_relative "certificate_verify"
|
|
10
|
+
require_relative "finished"
|
|
11
|
+
|
|
12
|
+
module Raiha::TLS
|
|
13
|
+
module Handshake
|
|
14
|
+
# Certificate, CertificateVerify and Finished, which RFC 9846 groups as
|
|
15
|
+
# the "Authentication Messages": verifying the peer's and building our
|
|
16
|
+
# own. Both roles use the same steps; what differs between them is the
|
|
17
|
+
# value of Parameters, never a branch on the role.
|
|
18
|
+
#
|
|
19
|
+
# Results come back as return values, never through callbacks: a verified
|
|
20
|
+
# value or a Failure, and messages to send. The caller adds messages to
|
|
21
|
+
# the transcript and the output queue. Transcript hashes come in as
|
|
22
|
+
# arguments, since RFC 9846 signs and MACs a transcript that ends before
|
|
23
|
+
# the message itself.
|
|
24
|
+
#
|
|
25
|
+
# The Trace values that tell a test oracle a check passed are built here,
|
|
26
|
+
# right where it passes, and nowhere else.
|
|
27
|
+
class AuthenticationMessages
|
|
28
|
+
SERVER_CONTEXT = "TLS 1.3, server CertificateVerify"
|
|
29
|
+
CLIENT_CONTEXT = "TLS 1.3, client CertificateVerify"
|
|
30
|
+
|
|
31
|
+
# The differences between the roles. The context strings and finished
|
|
32
|
+
# keys swap between verifying the peer and authenticating ourselves, so
|
|
33
|
+
# one value holds both sides to keep them from being mixed up.
|
|
34
|
+
class Parameters < Data.define(:own_side, :peer_side, :own_context, :peer_context)
|
|
35
|
+
CLIENT = new(own_side: :client, peer_side: :server, own_context: CLIENT_CONTEXT, peer_context: SERVER_CONTEXT)
|
|
36
|
+
SERVER = new(own_side: :server, peer_side: :client, own_context: SERVER_CONTEXT, peer_context: CLIENT_CONTEXT)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A check that failed: the alert to send and a Symbol naming it.
|
|
40
|
+
class Failure < Data.define(:alert, :reason)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
attr_reader :parameters
|
|
44
|
+
|
|
45
|
+
def initialize(parameters, tracer:)
|
|
46
|
+
@parameters = parameters
|
|
47
|
+
@tracer = tracer
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The peer's chain once the Verifier accepts it, leaf first, or a
|
|
51
|
+
# Failure. An empty certificate_list is the caller's to handle first,
|
|
52
|
+
# as the roles treat it differently.
|
|
53
|
+
def verify_certificate(certificate, keyring:, server_name:, now:, max_chain_length:, context:)
|
|
54
|
+
unless certificate.certificate_request_context == context
|
|
55
|
+
return Failure.new(alert: :decode_error, reason: :certificate_request_context)
|
|
56
|
+
end
|
|
57
|
+
entries = certificate.certificate_list
|
|
58
|
+
return Failure.new(alert: :decode_error, reason: :certificate_chain_too_long) if entries.size > max_chain_length
|
|
59
|
+
# @type var chain: Array[OpenSSL::X509::Certificate]
|
|
60
|
+
chain = []
|
|
61
|
+
entries.each do |entry|
|
|
62
|
+
parsed = Crypto::Certificate.parse(entry.cert_data) or return Failure.new(alert: :bad_certificate, reason: :malformed_certificate)
|
|
63
|
+
chain << parsed
|
|
64
|
+
end
|
|
65
|
+
purpose = @parameters.peer_side
|
|
66
|
+
result = keyring.verifier.verify(chain:, server_name:, now:, purpose:)
|
|
67
|
+
raise UsageError, "a Verifier must return a Verifier::Result, not #{result.class}" unless result.is_a?(Verifier::Result)
|
|
68
|
+
return Failure.new(alert: result.reason || :bad_certificate, reason: :certificate_rejected) unless result.ok
|
|
69
|
+
@tracer&.tls_trace(Trace::PeerCertificateVerified.new(purpose:))
|
|
70
|
+
result.verified_chain
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# True once the signature verifies, or a Failure. `offered` are the
|
|
74
|
+
# schemes we sent, in signature_algorithms or the CertificateRequest.
|
|
75
|
+
def verify_certificate_verify(certificate_verify, leaf, offered:, transcript_hash:)
|
|
76
|
+
algorithm = certificate_verify.algorithm
|
|
77
|
+
scheme = SignatureScheme.find(algorithm)
|
|
78
|
+
public_key = leaf.public_key
|
|
79
|
+
unless scheme && offered.include?(algorithm) && scheme.usable_in_certificate_verify? &&
|
|
80
|
+
Crypto::Signature.key_matches?(scheme, public_key)
|
|
81
|
+
return Failure.new(alert: :illegal_parameter, reason: :signature_algorithm)
|
|
82
|
+
end
|
|
83
|
+
content = signed_content(@parameters.peer_context, transcript_hash)
|
|
84
|
+
unless Crypto::Signature.verify(scheme, public_key, certificate_verify.signature, content)
|
|
85
|
+
return Failure.new(alert: :decrypt_error, reason: :bad_signature)
|
|
86
|
+
end
|
|
87
|
+
@tracer&.tls_trace(Trace::PeerSignatureVerified.new(algorithm: scheme.name))
|
|
88
|
+
true
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# True once the peer's verify_data matches, or a Failure. A
|
|
92
|
+
# verify_data of another length than the hash's is a Finished that
|
|
93
|
+
# does not decode (RFC 8446 "Finished": verify_data[Hash.length]), and
|
|
94
|
+
# one of the length that does not match fails to verify.
|
|
95
|
+
def verify_finished(finished, key_schedule, transcript_hash)
|
|
96
|
+
return Failure.new(alert: :decode_error, reason: :malformed_message) unless finished.verify_data.bytesize == key_schedule.hash_length
|
|
97
|
+
unless key_schedule.verify_finished(@parameters.peer_side, transcript_hash, finished.verify_data)
|
|
98
|
+
return Failure.new(alert: :decrypt_error, reason: :bad_finished)
|
|
99
|
+
end
|
|
100
|
+
@tracer&.tls_trace(Trace::PeerFinishedVerified.new(transcript_hash:))
|
|
101
|
+
true
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Our Certificate; an empty one without a credential, which a client
|
|
105
|
+
# sends when it has nothing to answer a CertificateRequest with.
|
|
106
|
+
def certificate(credential, context: "".b)
|
|
107
|
+
# @type var entries: Array[CertificateEntry]
|
|
108
|
+
entries = []
|
|
109
|
+
credential&.chain_der&.each { |der| entries << CertificateEntry.new(cert_data: der, extensions: []) }
|
|
110
|
+
Certificate.new(certificate_request_context: context, certificate_list: entries)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def certificate_verify(credential, scheme, transcript_hash)
|
|
114
|
+
content = signed_content(@parameters.own_context, transcript_hash)
|
|
115
|
+
CertificateVerify.new(algorithm: scheme.id, signature: Crypto::Signature.sign(scheme, credential.key, content))
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def finished(key_schedule, transcript_hash)
|
|
119
|
+
Finished.new(verify_data: key_schedule.finished_verify_data(@parameters.own_side, transcript_hash))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# RFC 9846 "Certificate Verify": 64 spaces, the context string, a zero
|
|
123
|
+
# byte and the transcript hash.
|
|
124
|
+
private def signed_content(context, transcript_hash) = (" " * 64 + context + "\x00").b + transcript_hash
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "../extension"
|
|
5
|
+
require_relative "message"
|
|
6
|
+
require_relative "violation"
|
|
7
|
+
|
|
8
|
+
module Raiha::TLS
|
|
9
|
+
module Handshake
|
|
10
|
+
# One certificate in a Certificate message. Only X.509 is supported, so
|
|
11
|
+
# cert_data is a DER certificate. Its extensions have a context of their
|
|
12
|
+
# own: status_request and signed_certificate_timestamp may appear here.
|
|
13
|
+
class CertificateEntry < Data.define(:cert_data, :extensions)
|
|
14
|
+
include ExtensionLookup
|
|
15
|
+
|
|
16
|
+
def self.read(r, registry:)
|
|
17
|
+
cert_data = r.opaque(3, min: 1, max: 0xffffff)
|
|
18
|
+
new(cert_data:, extensions: Extension.read_list(r, context: :certificate_entry, registry:))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def write(w)
|
|
22
|
+
w.vector(3, min: 1, max: 0xffffff) { w.bytes(cert_data) }
|
|
23
|
+
Extension.write_list(w, extensions)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Certificate < Data.define(:certificate_request_context, :certificate_list)
|
|
28
|
+
include Message
|
|
29
|
+
|
|
30
|
+
MSG_TYPE = 11
|
|
31
|
+
|
|
32
|
+
def self.parse_body(body, registry:)
|
|
33
|
+
r = Codec::Reader.new(body)
|
|
34
|
+
certificate_request_context = r.opaque(1, max: 0xff)
|
|
35
|
+
certificate_list = r.vector(3, max: 0xffffff) do |v|
|
|
36
|
+
# @type var list: Array[CertificateEntry]
|
|
37
|
+
list = []
|
|
38
|
+
list << CertificateEntry.read(v, registry:) until v.eof?
|
|
39
|
+
list.freeze
|
|
40
|
+
end
|
|
41
|
+
r.finish!
|
|
42
|
+
new(certificate_request_context:, certificate_list:)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def msg_type = MSG_TYPE
|
|
46
|
+
|
|
47
|
+
# Nothing to check without context.
|
|
48
|
+
def validate = nil
|
|
49
|
+
|
|
50
|
+
def write_body(w)
|
|
51
|
+
w.vector(1, max: 0xff) { w.bytes(certificate_request_context) }
|
|
52
|
+
w.vector(3, max: 0xffffff) { certificate_list.each { |entry| entry.write(w) } }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "../extension"
|
|
5
|
+
require_relative "message"
|
|
6
|
+
require_relative "violation"
|
|
7
|
+
|
|
8
|
+
module Raiha::TLS
|
|
9
|
+
module Handshake
|
|
10
|
+
class CertificateRequest < Data.define(:certificate_request_context, :extensions)
|
|
11
|
+
include Message
|
|
12
|
+
include ExtensionLookup
|
|
13
|
+
|
|
14
|
+
MSG_TYPE = 13
|
|
15
|
+
|
|
16
|
+
def self.parse_body(body, registry:)
|
|
17
|
+
r = Codec::Reader.new(body)
|
|
18
|
+
certificate_request_context = r.opaque(1, max: 0xff)
|
|
19
|
+
extensions = Extension.read_list(r, context: :certificate_request, registry:)
|
|
20
|
+
r.finish!
|
|
21
|
+
new(certificate_request_context:, extensions:)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def msg_type = MSG_TYPE
|
|
25
|
+
|
|
26
|
+
def validate
|
|
27
|
+
case extension(Extension::SignatureAlgorithms::TYPE)
|
|
28
|
+
when Extension::SignatureAlgorithms then nil
|
|
29
|
+
else Violation.new(alert: :missing_extension, reason: :signature_algorithms)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def write_body(w)
|
|
34
|
+
w.vector(1, max: 0xff) { w.bytes(certificate_request_context) }
|
|
35
|
+
Extension.write_list(w, extensions)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "../signature_scheme"
|
|
5
|
+
require_relative "message"
|
|
6
|
+
require_relative "violation"
|
|
7
|
+
|
|
8
|
+
module Raiha::TLS
|
|
9
|
+
module Handshake
|
|
10
|
+
class CertificateVerify < Data.define(:algorithm, :signature)
|
|
11
|
+
include Message
|
|
12
|
+
|
|
13
|
+
MSG_TYPE = 15
|
|
14
|
+
|
|
15
|
+
def self.parse_body(body, registry: nil)
|
|
16
|
+
r = Codec::Reader.new(body)
|
|
17
|
+
algorithm = r.u16
|
|
18
|
+
signature = r.opaque(2, max: 0xffff)
|
|
19
|
+
r.finish!
|
|
20
|
+
new(algorithm:, signature:)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def msg_type = MSG_TYPE
|
|
24
|
+
|
|
25
|
+
# RFC 9846 forbids RSASSA-PKCS1-v1_5 and SHA-1 in CertificateVerify. A
|
|
26
|
+
# scheme this implementation does not define is left to the state
|
|
27
|
+
# machine, which rejects any scheme it did not offer.
|
|
28
|
+
def validate
|
|
29
|
+
scheme = SignatureScheme.find(algorithm)
|
|
30
|
+
return nil if scheme.nil? || scheme.usable_in_certificate_verify?
|
|
31
|
+
Violation.new(alert: :illegal_parameter, reason: :signature_algorithm)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def write_body(w)
|
|
35
|
+
w.u16(algorithm)
|
|
36
|
+
w.vector(2, max: 0xffff) { w.bytes(signature) }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../codec"
|
|
4
|
+
require_relative "../extension"
|
|
5
|
+
require_relative "message"
|
|
6
|
+
require_relative "violation"
|
|
7
|
+
|
|
8
|
+
module Raiha::TLS
|
|
9
|
+
module Handshake
|
|
10
|
+
class ClientHello < Data.define(
|
|
11
|
+
:legacy_version, :random, :legacy_session_id, :cipher_suites, :legacy_compression_methods, :extensions
|
|
12
|
+
)
|
|
13
|
+
include Message
|
|
14
|
+
include ExtensionLookup
|
|
15
|
+
|
|
16
|
+
MSG_TYPE = 1
|
|
17
|
+
|
|
18
|
+
def self.parse_body(body, registry:)
|
|
19
|
+
r = Codec::Reader.new(body)
|
|
20
|
+
legacy_version = r.u16
|
|
21
|
+
random = r.bytes(32)
|
|
22
|
+
legacy_session_id = r.opaque(1, max: 32)
|
|
23
|
+
cipher_suites = r.vector(2, min: 2, max: 0xfffe) do |v|
|
|
24
|
+
# @type var list: Array[Integer]
|
|
25
|
+
list = []
|
|
26
|
+
list << v.u16 until v.eof?
|
|
27
|
+
list.freeze
|
|
28
|
+
end
|
|
29
|
+
legacy_compression_methods = r.opaque(1, min: 1, max: 0xff).bytes.freeze
|
|
30
|
+
extensions = Extension.read_list(r, context: :client_hello, registry:, min: 8)
|
|
31
|
+
r.finish!
|
|
32
|
+
new(legacy_version:, random:, legacy_session_id:, cipher_suites:, legacy_compression_methods:, extensions:)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def msg_type = MSG_TYPE
|
|
36
|
+
|
|
37
|
+
def write_body(w)
|
|
38
|
+
w.u16(legacy_version)
|
|
39
|
+
w.bytes(random)
|
|
40
|
+
w.vector(1, max: 32) { w.bytes(legacy_session_id) }
|
|
41
|
+
w.vector(2, min: 2, max: 0xfffe) { cipher_suites.each { |suite| w.u16(suite) } }
|
|
42
|
+
w.vector(1, min: 1, max: 0xff) { legacy_compression_methods.each { |method| w.u8(method) } }
|
|
43
|
+
Extension.write_list(w, extensions, min: 8)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Raises EncodeError when pre_shared_key is present but not last, which
|
|
47
|
+
# RFC 9846 requires of a ClientHello.
|
|
48
|
+
def encode
|
|
49
|
+
psk_index = extensions.index { |ext| ext.extension_type == Extension::PreSharedKey::TYPE }
|
|
50
|
+
if psk_index && psk_index != extensions.size - 1
|
|
51
|
+
raise Codec::EncodeError, "pre_shared_key must be the last extension"
|
|
52
|
+
end
|
|
53
|
+
super
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The checks a server makes on a ClientHello by itself, in order; the
|
|
57
|
+
# first failure is returned. The order is part of the rules:
|
|
58
|
+
#
|
|
59
|
+
# 1. Versions first. Only after 2 is it a ClientHello offering TLS 1.3,
|
|
60
|
+
# to which the extension requirements in 4 to 6 apply.
|
|
61
|
+
# 2. missing_extension before illegal_parameter: check that an extension
|
|
62
|
+
# is there before looking inside it. 9 reads both supported_groups and
|
|
63
|
+
# key_share, so it relies on 5 having passed.
|
|
64
|
+
# 3. 9 runs only when key_share is present. A PSK-only ClientHello has
|
|
65
|
+
# neither extension, passes 5, and 9 finds nothing to check.
|
|
66
|
+
#
|
|
67
|
+
# Requiring key_share to list groups in supported_groups order is left
|
|
68
|
+
# out on purpose, to avoid ossification.
|
|
69
|
+
def validate
|
|
70
|
+
# 1
|
|
71
|
+
return Violation.new(alert: :protocol_version, reason: :legacy_version) unless legacy_version == 0x0303
|
|
72
|
+
# 2. Without supported_versions a server would have to negotiate TLS 1.2
|
|
73
|
+
# or lower, which this implementation cannot.
|
|
74
|
+
versions = supported_versions_extension
|
|
75
|
+
unless versions && versions.versions.include?(0x0304)
|
|
76
|
+
return Violation.new(alert: :protocol_version, reason: :supported_versions)
|
|
77
|
+
end
|
|
78
|
+
# 3
|
|
79
|
+
unless legacy_compression_methods == [0]
|
|
80
|
+
return Violation.new(alert: :illegal_parameter, reason: :legacy_compression_methods)
|
|
81
|
+
end
|
|
82
|
+
groups = supported_groups_extension
|
|
83
|
+
key_share = key_share_extension
|
|
84
|
+
psk = pre_shared_key_extension
|
|
85
|
+
# 4
|
|
86
|
+
if psk.nil? && signature_algorithms_extension.nil?
|
|
87
|
+
return Violation.new(alert: :missing_extension, reason: :signature_algorithms)
|
|
88
|
+
end
|
|
89
|
+
return Violation.new(alert: :missing_extension, reason: :supported_groups) if psk.nil? && groups.nil?
|
|
90
|
+
# 5
|
|
91
|
+
if key_share && groups.nil?
|
|
92
|
+
return Violation.new(alert: :missing_extension, reason: :key_share_without_supported_groups)
|
|
93
|
+
end
|
|
94
|
+
if groups && key_share.nil?
|
|
95
|
+
return Violation.new(alert: :missing_extension, reason: :supported_groups_without_key_share)
|
|
96
|
+
end
|
|
97
|
+
# 6
|
|
98
|
+
if psk && psk_key_exchange_modes_extension.nil?
|
|
99
|
+
return Violation.new(alert: :missing_extension, reason: :psk_key_exchange_modes)
|
|
100
|
+
end
|
|
101
|
+
# 7
|
|
102
|
+
if groups && groups.named_group_list.uniq.size != groups.named_group_list.size
|
|
103
|
+
return Violation.new(alert: :illegal_parameter, reason: :duplicate_supported_group)
|
|
104
|
+
end
|
|
105
|
+
if key_share
|
|
106
|
+
share_groups = key_share.client_shares.map(&:group)
|
|
107
|
+
# 8
|
|
108
|
+
if share_groups.uniq.size != share_groups.size
|
|
109
|
+
return Violation.new(alert: :illegal_parameter, reason: :duplicate_key_share_group)
|
|
110
|
+
end
|
|
111
|
+
# 9. GREASE groups pass: RFC 8701 puts them in supported_groups too.
|
|
112
|
+
unless groups && (share_groups - groups.named_group_list).empty?
|
|
113
|
+
return Violation.new(alert: :illegal_parameter, reason: :key_share_group_not_supported)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
if psk
|
|
117
|
+
# 10
|
|
118
|
+
unless psk.equal?(extensions.last)
|
|
119
|
+
return Violation.new(alert: :illegal_parameter, reason: :pre_shared_key_not_last)
|
|
120
|
+
end
|
|
121
|
+
# 11
|
|
122
|
+
unless psk.binders.size == psk.identities.size
|
|
123
|
+
return Violation.new(alert: :illegal_parameter, reason: :binders_count)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
nil
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The number of bytes the binders list takes at the end of the encoded
|
|
130
|
+
# ClientHello: its 2-byte length and each binder with its 1-byte length.
|
|
131
|
+
# Zero without pre_shared_key.
|
|
132
|
+
def binders_wire_length
|
|
133
|
+
case (psk = extension(Extension::PreSharedKey::TYPE))
|
|
134
|
+
when Extension::PreSharedKey::CH
|
|
135
|
+
2 + psk.binders.sum { |binder| 1 + binder.bytesize }
|
|
136
|
+
else
|
|
137
|
+
0
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The encoded ClientHello up to, but not including, the binders list: the
|
|
142
|
+
# input to the binder MAC. Encode the ClientHello with placeholder
|
|
143
|
+
# binders of the right lengths first, so every length field in front is
|
|
144
|
+
# final, then fill the real binders in with `#with_binders`.
|
|
145
|
+
def encode_truncated
|
|
146
|
+
full = encode
|
|
147
|
+
full.byteslice(0, full.bytesize - binders_wire_length) #: String
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# This ClientHello with the pre_shared_key binders replaced. Each binder
|
|
151
|
+
# must be as long as the one it replaces, or the truncated ClientHello
|
|
152
|
+
# the binders were computed over would no longer match.
|
|
153
|
+
def with_binders(binders)
|
|
154
|
+
found = false
|
|
155
|
+
replaced = extensions.map do |ext|
|
|
156
|
+
case ext
|
|
157
|
+
when Extension::PreSharedKey::CH
|
|
158
|
+
found = true
|
|
159
|
+
unless binders.map(&:bytesize) == ext.binders.map(&:bytesize)
|
|
160
|
+
raise ArgumentError, "binder lengths #{binders.map(&:bytesize)} differ from #{ext.binders.map(&:bytesize)}"
|
|
161
|
+
end
|
|
162
|
+
ext.with(binders: binders.freeze)
|
|
163
|
+
else
|
|
164
|
+
ext
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
raise ArgumentError, "this ClientHello has no pre_shared_key" unless found
|
|
168
|
+
with(extensions: replaced.freeze)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
private def supported_versions_extension
|
|
172
|
+
case (ext = extension(Extension::SupportedVersions::TYPE))
|
|
173
|
+
when Extension::SupportedVersions::CH then ext
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
private def supported_groups_extension
|
|
178
|
+
case (ext = extension(Extension::SupportedGroups::TYPE))
|
|
179
|
+
when Extension::SupportedGroups then ext
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
private def key_share_extension
|
|
184
|
+
case (ext = extension(Extension::KeyShare::TYPE))
|
|
185
|
+
when Extension::KeyShare::CH then ext
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
private def pre_shared_key_extension
|
|
190
|
+
case (ext = extension(Extension::PreSharedKey::TYPE))
|
|
191
|
+
when Extension::PreSharedKey::CH then ext
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
private def signature_algorithms_extension
|
|
196
|
+
case (ext = extension(Extension::SignatureAlgorithms::TYPE))
|
|
197
|
+
when Extension::SignatureAlgorithms then ext
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
private def psk_key_exchange_modes_extension
|
|
202
|
+
case (ext = extension(Extension::PskKeyExchangeModes::TYPE))
|
|
203
|
+
when Extension::PskKeyExchangeModes then ext
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../config"
|
|
4
|
+
require_relative "../crypto"
|
|
5
|
+
require_relative "../extension"
|
|
6
|
+
require_relative "../profile"
|
|
7
|
+
require_relative "client_hello"
|
|
8
|
+
|
|
9
|
+
module Raiha::TLS
|
|
10
|
+
module Handshake
|
|
11
|
+
# Builds the ClientHello a client opens with, and the second one after a
|
|
12
|
+
# HelloRetryRequest.
|
|
13
|
+
#
|
|
14
|
+
# The random comes first. The GREASE values (RFC 8701) and the order of
|
|
15
|
+
# the extensions are derived from it, so the second ClientHello, which
|
|
16
|
+
# keeps the random, keeps them too, and a connection needs no seed of its
|
|
17
|
+
# own. Extensions are shuffled once per connection, as Chrome does, unless
|
|
18
|
+
# `permute_extensions` is off; a GREASE extension stays first and another
|
|
19
|
+
# last, since some servers cannot handle an empty extension at the end.
|
|
20
|
+
module ClientHelloBuilder
|
|
21
|
+
# The ClientHello and the key pairs behind its key shares.
|
|
22
|
+
class Built < Data.define(:client_hello, :key_pairs)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
TLS13 = 0x0304
|
|
26
|
+
LEGACY_VERSION = 0x0303
|
|
27
|
+
|
|
28
|
+
# The order of the extensions without permutation. User extensions come
|
|
29
|
+
# after these, quic_transport_parameters first among them.
|
|
30
|
+
FIXED_ORDER = [
|
|
31
|
+
Extension::ServerName::TYPE, Extension::SupportedVersions::TYPE, Extension::SupportedGroups::TYPE,
|
|
32
|
+
Extension::KeyShare::TYPE, Extension::SignatureAlgorithms::TYPE, Extension::SignatureAlgorithmsCert::TYPE,
|
|
33
|
+
Extension::PskKeyExchangeModes::TYPE, Extension::Alpn::TYPE, Extension::RecordSizeLimit::TYPE,
|
|
34
|
+
Extension::CompressCertificate::TYPE, Extension::PostHandshakeAuth::TYPE, Profile::QUIC_TRANSPORT_PARAMETERS,
|
|
35
|
+
Extension::Cookie::TYPE
|
|
36
|
+
].freeze
|
|
37
|
+
|
|
38
|
+
PADDING = 21
|
|
39
|
+
|
|
40
|
+
# The only PSK key exchange mode offered: a resumption keeps forward
|
|
41
|
+
# secrecy only with a key exchange of its own.
|
|
42
|
+
PSK_DHE_KE = 1
|
|
43
|
+
|
|
44
|
+
# Where the GREASE values are taken from the seed.
|
|
45
|
+
GREASE_SLOTS = { cipher_suite: 0, group: 1, version: 2, first_extension: 3, last_extension: 4 }.freeze #: Hash[Symbol, Integer]
|
|
46
|
+
|
|
47
|
+
# `server_name` is the name in normal form, or nil to send no SNI (an IP
|
|
48
|
+
# address). `extensions` are this connection's own, beside the
|
|
49
|
+
# Config's. A key share is offered for the first group of the Config.
|
|
50
|
+
def self.build(config:, keyring:, server_name:, extensions: [])
|
|
51
|
+
random = keyring.random_bytes(32)
|
|
52
|
+
legacy_session_id = config.profile.compatibility_mode ? keyring.random_bytes(32) : "".b
|
|
53
|
+
seed = Crypto.hash(:sha256, "raiha ClientHello seed".b + random)
|
|
54
|
+
# @type var grease: Hash[Symbol, Integer]
|
|
55
|
+
grease = config.grease ? GREASE_SLOTS.transform_values { |slot| grease_value(seed, slot) } : {}
|
|
56
|
+
if grease[:first_extension] && grease[:first_extension] == grease[:last_extension]
|
|
57
|
+
grease[:last_extension] ^= 0x1010
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
key_pair = keyring.key_exchange.generate(config.groups.fetch(0))
|
|
61
|
+
shares = [Extension::KeyShare::Entry.new(group: Config::GROUPS_BY_NAME.fetch(key_pair.group), key_exchange: key_pair.public_bytes)]
|
|
62
|
+
shares.unshift(Extension::KeyShare::Entry.new(group: grease.fetch(:group), key_exchange: "\x00".b)) if config.grease
|
|
63
|
+
|
|
64
|
+
built = {} #: Hash[Integer, Extension::_Extension]
|
|
65
|
+
built[Extension::ServerName::TYPE] = Extension::ServerName::CH.new(host_name: server_name) if server_name
|
|
66
|
+
built[Extension::SupportedVersions::TYPE] =
|
|
67
|
+
Extension::SupportedVersions::CH.new(versions: [*grease.values_at(:version).compact, TLS13])
|
|
68
|
+
built[Extension::SupportedGroups::TYPE] =
|
|
69
|
+
Extension::SupportedGroups.new(named_group_list: [*grease.values_at(:group).compact, *config.group_ids])
|
|
70
|
+
built[Extension::KeyShare::TYPE] = Extension::KeyShare::CH.new(client_shares: shares)
|
|
71
|
+
built[Extension::SignatureAlgorithms::TYPE] =
|
|
72
|
+
Extension::SignatureAlgorithms.new(supported_signature_algorithms: config.signature_algorithm_ids)
|
|
73
|
+
unless config.signature_algorithm_cert_ids == config.signature_algorithm_ids
|
|
74
|
+
built[Extension::SignatureAlgorithmsCert::TYPE] =
|
|
75
|
+
Extension::SignatureAlgorithmsCert.new(supported_signature_algorithms: config.signature_algorithm_cert_ids)
|
|
76
|
+
end
|
|
77
|
+
# A client that resumes says how it takes a PSK, so that a server
|
|
78
|
+
# issues it tickets: psk_dhe_ke alone.
|
|
79
|
+
built[Extension::PskKeyExchangeModes::TYPE] = Extension::PskKeyExchangeModes.new(ke_modes: [PSK_DHE_KE]) if config.resumption
|
|
80
|
+
built[Extension::Alpn::TYPE] = Extension::Alpn.new(protocol_name_list: config.alpn) unless config.alpn.empty?
|
|
81
|
+
if (limit = config.record_size_limit)
|
|
82
|
+
built[Extension::RecordSizeLimit::TYPE] = Extension::RecordSizeLimit.new(record_size_limit: limit)
|
|
83
|
+
end
|
|
84
|
+
given = config.extensions + extensions
|
|
85
|
+
given.each { |ext| built[ext.extension_type] = ext }
|
|
86
|
+
|
|
87
|
+
ordered = FIXED_ORDER.filter_map { |type| built[type] } + given.reject { |ext| FIXED_ORDER.include?(ext.extension_type) }
|
|
88
|
+
if config.permute_extensions
|
|
89
|
+
ordered = ordered.shuffle(random: Random.new(seed.unpack1("Q>")))
|
|
90
|
+
end
|
|
91
|
+
if config.grease
|
|
92
|
+
ordered.unshift(Extension::Opaque.new(type: grease.fetch(:first_extension), data: "".b))
|
|
93
|
+
ordered.push(Extension::Opaque.new(type: grease.fetch(:last_extension), data: "\x00".b))
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
cipher_suites = [*grease.values_at(:cipher_suite).compact, *config.cipher_suite_ids]
|
|
97
|
+
client_hello = ClientHello.new(
|
|
98
|
+
legacy_version: LEGACY_VERSION, random:, legacy_session_id:, cipher_suites:,
|
|
99
|
+
legacy_compression_methods: [0], extensions: ordered
|
|
100
|
+
)
|
|
101
|
+
Built.new(client_hello:, key_pairs: [key_pair])
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# The second ClientHello, from the first and the HelloRetryRequest
|
|
105
|
+
# alone, never the Config: RFC 9846 "Client Hello" allows only these
|
|
106
|
+
# changes, and copying everything else in place keeps the two the same
|
|
107
|
+
# by construction, order included. The block makes the key share for
|
|
108
|
+
# the group the server asked for.
|
|
109
|
+
#
|
|
110
|
+
# - key_share: a single entry for the requested group, in place; kept
|
|
111
|
+
# when the HelloRetryRequest asks only for a cookie.
|
|
112
|
+
# - early_data: removed.
|
|
113
|
+
# - cookie: the server's, replacing ours or inserted before
|
|
114
|
+
# pre_shared_key or padding, whichever comes first, or at the end.
|
|
115
|
+
def self.retry(ch1, hrr)
|
|
116
|
+
share = case (requested = hrr.extension(Extension::KeyShare::TYPE))
|
|
117
|
+
when Extension::KeyShare::HRR then yield(requested.selected_group)
|
|
118
|
+
end
|
|
119
|
+
cookie = hrr.extension(Extension::Cookie::TYPE)
|
|
120
|
+
extensions = ch1.extensions.filter_map do |ext|
|
|
121
|
+
case ext.extension_type
|
|
122
|
+
when Extension::EarlyData::TYPE then nil
|
|
123
|
+
when Extension::KeyShare::TYPE then share ? Extension::KeyShare::CH.new(client_shares: [share]) : ext
|
|
124
|
+
when Extension::Cookie::TYPE then cookie
|
|
125
|
+
else ext
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
had_cookie = ch1.extensions.any? { |ext| ext.extension_type == Extension::Cookie::TYPE }
|
|
129
|
+
if cookie && !had_cookie
|
|
130
|
+
at = extensions.index { |ext| [Extension::PreSharedKey::TYPE, PADDING].include?(ext.extension_type) } || extensions.size
|
|
131
|
+
extensions.insert(at, cookie)
|
|
132
|
+
end
|
|
133
|
+
ch1.with(extensions:)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# One of the 16 GREASE values 0x0A0A, 0x1A1A, ..., 0xFAFA, from a byte
|
|
137
|
+
# of the seed.
|
|
138
|
+
private_class_method def self.grease_value(seed, slot)
|
|
139
|
+
nibble = seed.getbyte(slot).to_i & 0xf0
|
|
140
|
+
((nibble | 0x0a) << 8) | nibble | 0x0a
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|