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,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::TLS
|
|
4
|
+
module Handshake
|
|
5
|
+
# The first check a message fails on its own, without knowing what was
|
|
6
|
+
# sent, the configuration or the role: the alert to send and a Symbol
|
|
7
|
+
# naming the check. Checks that need that context belong to the state
|
|
8
|
+
# machine.
|
|
9
|
+
class Violation < Data.define(:alert, :reason)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "codec"
|
|
4
|
+
require_relative "extension"
|
|
5
|
+
require_relative "handshake/message"
|
|
6
|
+
require_relative "handshake/violation"
|
|
7
|
+
require_relative "handshake/client_hello"
|
|
8
|
+
require_relative "handshake/server_hello"
|
|
9
|
+
require_relative "handshake/encrypted_extensions"
|
|
10
|
+
require_relative "handshake/certificate_request"
|
|
11
|
+
require_relative "handshake/certificate"
|
|
12
|
+
require_relative "handshake/certificate_verify"
|
|
13
|
+
require_relative "handshake/finished"
|
|
14
|
+
require_relative "handshake/new_session_ticket"
|
|
15
|
+
require_relative "handshake/end_of_early_data"
|
|
16
|
+
require_relative "handshake/key_update"
|
|
17
|
+
require_relative "handshake/reassembler"
|
|
18
|
+
require_relative "handshake/client_hello_builder"
|
|
19
|
+
require_relative "handshake/machine"
|
|
20
|
+
|
|
21
|
+
module Raiha::TLS
|
|
22
|
+
# Handshake messages as values (RFC 9846 "Handshake Protocol"). Fields keep
|
|
23
|
+
# the RFC's names, `legacy_` prefixes included, and enumerated values stay
|
|
24
|
+
# Integers so unknown ones survive a round trip.
|
|
25
|
+
module Handshake
|
|
26
|
+
TYPES = {
|
|
27
|
+
ClientHello::MSG_TYPE => ClientHello,
|
|
28
|
+
ServerHello::MSG_TYPE => ServerHello,
|
|
29
|
+
NewSessionTicket::MSG_TYPE => NewSessionTicket,
|
|
30
|
+
EndOfEarlyData::MSG_TYPE => EndOfEarlyData,
|
|
31
|
+
EncryptedExtensions::MSG_TYPE => EncryptedExtensions,
|
|
32
|
+
Certificate::MSG_TYPE => Certificate,
|
|
33
|
+
CertificateRequest::MSG_TYPE => CertificateRequest,
|
|
34
|
+
CertificateVerify::MSG_TYPE => CertificateVerify,
|
|
35
|
+
Finished::MSG_TYPE => Finished,
|
|
36
|
+
KeyUpdate::MSG_TYPE => KeyUpdate,
|
|
37
|
+
# Only ever synthesized into the transcript after a HelloRetryRequest;
|
|
38
|
+
# it never appears on the wire.
|
|
39
|
+
254 => :message_hash
|
|
40
|
+
}.freeze
|
|
41
|
+
|
|
42
|
+
# Parses one handshake message, header included. Raises a
|
|
43
|
+
# Codec::Malformed for anything that is not a well-formed message of a
|
|
44
|
+
# known type; the state machine rejects unknown types before parsing.
|
|
45
|
+
def self.parse(bytes, registry:)
|
|
46
|
+
r = Codec::Reader.new(bytes)
|
|
47
|
+
msg_type = r.u8
|
|
48
|
+
body = r.opaque(3, max: 0xffffff)
|
|
49
|
+
r.finish!
|
|
50
|
+
parse_body(msg_type, body, registry:)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.parse_body(msg_type, body, registry:)
|
|
54
|
+
case (klass = TYPES[msg_type])
|
|
55
|
+
when nil, Symbol
|
|
56
|
+
raise Codec::UnknownMessageType, "handshake message type #{msg_type} is not supported"
|
|
57
|
+
else
|
|
58
|
+
klass.parse_body(body, registry:)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Names for the integers on the wire. Values stay Integers everywhere else,
|
|
4
|
+
# so an out-of-date table cannot break parsing. These tables are used only at
|
|
5
|
+
# the public API boundary and for logs, qlog and `inspect`.
|
|
6
|
+
module Raiha::TLS::IANA
|
|
7
|
+
CIPHER_SUITES = {
|
|
8
|
+
0x1301 => :TLS_AES_128_GCM_SHA256,
|
|
9
|
+
0x1302 => :TLS_AES_256_GCM_SHA384,
|
|
10
|
+
0x1303 => :TLS_CHACHA20_POLY1305_SHA256,
|
|
11
|
+
0x1304 => :TLS_AES_128_CCM_SHA256,
|
|
12
|
+
0x1305 => :TLS_AES_128_CCM_8_SHA256
|
|
13
|
+
}.freeze #: Hash[Integer, Symbol]
|
|
14
|
+
|
|
15
|
+
NAMED_GROUPS = {
|
|
16
|
+
0x0017 => :secp256r1,
|
|
17
|
+
0x0018 => :secp384r1,
|
|
18
|
+
0x0019 => :secp521r1,
|
|
19
|
+
0x001d => :x25519,
|
|
20
|
+
0x001e => :x448,
|
|
21
|
+
0x0100 => :ffdhe2048,
|
|
22
|
+
0x0101 => :ffdhe3072,
|
|
23
|
+
0x0102 => :ffdhe4096,
|
|
24
|
+
0x0103 => :ffdhe6144,
|
|
25
|
+
0x0104 => :ffdhe8192,
|
|
26
|
+
0x0200 => :MLKEM512,
|
|
27
|
+
0x0201 => :MLKEM768,
|
|
28
|
+
0x0202 => :MLKEM1024,
|
|
29
|
+
0x11eb => :SecP256r1MLKEM768,
|
|
30
|
+
0x11ec => :X25519MLKEM768,
|
|
31
|
+
0x11ed => :SecP384r1MLKEM1024
|
|
32
|
+
}.freeze #: Hash[Integer, Symbol]
|
|
33
|
+
|
|
34
|
+
EXTENSIONS = {
|
|
35
|
+
0 => :server_name,
|
|
36
|
+
1 => :max_fragment_length,
|
|
37
|
+
5 => :status_request,
|
|
38
|
+
10 => :supported_groups,
|
|
39
|
+
13 => :signature_algorithms,
|
|
40
|
+
14 => :use_srtp,
|
|
41
|
+
15 => :heartbeat,
|
|
42
|
+
16 => :application_layer_protocol_negotiation,
|
|
43
|
+
17 => :status_request_v2,
|
|
44
|
+
18 => :signed_certificate_timestamp,
|
|
45
|
+
19 => :client_certificate_type,
|
|
46
|
+
20 => :server_certificate_type,
|
|
47
|
+
21 => :padding,
|
|
48
|
+
27 => :compress_certificate,
|
|
49
|
+
28 => :record_size_limit,
|
|
50
|
+
35 => :session_ticket,
|
|
51
|
+
41 => :pre_shared_key,
|
|
52
|
+
42 => :early_data,
|
|
53
|
+
43 => :supported_versions,
|
|
54
|
+
44 => :cookie,
|
|
55
|
+
45 => :psk_key_exchange_modes,
|
|
56
|
+
47 => :certificate_authorities,
|
|
57
|
+
48 => :oid_filters,
|
|
58
|
+
49 => :post_handshake_auth,
|
|
59
|
+
50 => :signature_algorithms_cert,
|
|
60
|
+
51 => :key_share,
|
|
61
|
+
57 => :quic_transport_parameters,
|
|
62
|
+
0xfe0d => :encrypted_client_hello,
|
|
63
|
+
0xff01 => :renegotiation_info
|
|
64
|
+
}.freeze #: Hash[Integer, Symbol]
|
|
65
|
+
|
|
66
|
+
ALERTS = {
|
|
67
|
+
0 => :close_notify,
|
|
68
|
+
10 => :unexpected_message,
|
|
69
|
+
20 => :bad_record_mac,
|
|
70
|
+
22 => :record_overflow,
|
|
71
|
+
40 => :handshake_failure,
|
|
72
|
+
42 => :bad_certificate,
|
|
73
|
+
43 => :unsupported_certificate,
|
|
74
|
+
44 => :certificate_revoked,
|
|
75
|
+
45 => :certificate_expired,
|
|
76
|
+
46 => :certificate_unknown,
|
|
77
|
+
47 => :illegal_parameter,
|
|
78
|
+
48 => :unknown_ca,
|
|
79
|
+
49 => :access_denied,
|
|
80
|
+
50 => :decode_error,
|
|
81
|
+
51 => :decrypt_error,
|
|
82
|
+
70 => :protocol_version,
|
|
83
|
+
71 => :insufficient_security,
|
|
84
|
+
80 => :internal_error,
|
|
85
|
+
86 => :inappropriate_fallback,
|
|
86
|
+
90 => :user_canceled,
|
|
87
|
+
109 => :missing_extension,
|
|
88
|
+
110 => :unsupported_extension,
|
|
89
|
+
112 => :unrecognized_name,
|
|
90
|
+
113 => :bad_certificate_status_response,
|
|
91
|
+
115 => :unknown_psk_identity,
|
|
92
|
+
116 => :certificate_required,
|
|
93
|
+
117 => :general_error,
|
|
94
|
+
120 => :no_application_protocol
|
|
95
|
+
}.freeze #: Hash[Integer, Symbol]
|
|
96
|
+
|
|
97
|
+
ALERT_CODES = ALERTS.invert.freeze #: Hash[Symbol, Integer]
|
|
98
|
+
|
|
99
|
+
# nil for a value this table does not know.
|
|
100
|
+
def self.cipher_suite_name(v) = CIPHER_SUITES[v]
|
|
101
|
+
|
|
102
|
+
def self.alert_name(v) = ALERTS[v]
|
|
103
|
+
|
|
104
|
+
# Only for alerts this implementation sends, so a missing name is a bug
|
|
105
|
+
# here and raises KeyError.
|
|
106
|
+
def self.alert_code(name) = ALERT_CODES.fetch(name)
|
|
107
|
+
|
|
108
|
+
# A name for logs, qlog and `inspect`, including values the table lacks.
|
|
109
|
+
def self.label(table, v) = table[v]&.to_s || format("unknown(0x%04x)", v)
|
|
110
|
+
|
|
111
|
+
# RFC 8701 GREASE values: 0x0A0A, 0x1A1A, ..., 0xFAFA.
|
|
112
|
+
def self.grease?(v) = (v & 0x0f0f) == 0x0a0a && (v >> 8) == (v & 0xff)
|
|
113
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "crypto"
|
|
4
|
+
require_relative "masked_inspect"
|
|
5
|
+
|
|
6
|
+
module Raiha::TLS
|
|
7
|
+
# Key exchange in the shape of a KEM, since that is what RFC 9846 now
|
|
8
|
+
# describes and what the post-quantum hybrids are: the client sends a
|
|
9
|
+
# public key, the server encapsulates to it and returns a ciphertext. For
|
|
10
|
+
# ECDH the server's "ciphertext" is its own fresh public key.
|
|
11
|
+
#
|
|
12
|
+
# A provider answers `supported_groups`, `generate(group)`,
|
|
13
|
+
# `encapsulate(group, peer_public_bytes)` and `decapsulate(key_pair,
|
|
14
|
+
# ciphertext)`. It must generate a fresh key pair for every connection:
|
|
15
|
+
# RFC 9846 forbids reusing a key share, although a peer that does so must
|
|
16
|
+
# still be accepted, as RFC 8446 allowed it. `encapsulate` and
|
|
17
|
+
# `decapsulate` return nil for bytes from the peer that are not a valid key
|
|
18
|
+
# of the group; the state machine answers with illegal_parameter.
|
|
19
|
+
module KeyExchange
|
|
20
|
+
class KeyPair < Data.define(:group, :public_bytes, :private_key)
|
|
21
|
+
include MaskedInspect
|
|
22
|
+
|
|
23
|
+
def masked_members = %i[private_key]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The default provider, on OpenSSL through Crypto::ECDH: X25519 first,
|
|
27
|
+
# then P-256 and P-384.
|
|
28
|
+
class OpenSSL
|
|
29
|
+
GROUPS = Crypto::ECDH.groups.freeze #: Array[Symbol]
|
|
30
|
+
|
|
31
|
+
def supported_groups = GROUPS
|
|
32
|
+
|
|
33
|
+
def generate(group)
|
|
34
|
+
check_group(group)
|
|
35
|
+
key = Crypto::ECDH.generate(group)
|
|
36
|
+
KeyPair.new(group:, public_bytes: Crypto::ECDH.public_bytes(group, key).freeze, private_key: key)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A fresh key pair of our own, whose public key is the ciphertext, and
|
|
40
|
+
# the shared secret; nil when the peer's key is invalid.
|
|
41
|
+
def encapsulate(group, peer_public_bytes)
|
|
42
|
+
key_pair = generate(group)
|
|
43
|
+
shared_secret = decapsulate(key_pair, peer_public_bytes) or return nil
|
|
44
|
+
[key_pair.public_bytes, shared_secret]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def decapsulate(key_pair, ciphertext)
|
|
48
|
+
shared_secret = Crypto::ECDH.derive(key_pair.group, key_pair.private_key, ciphertext)
|
|
49
|
+
return nil if shared_secret.nil? || shared_secret.each_byte.all?(&:zero?)
|
|
50
|
+
shared_secret
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private def check_group(group)
|
|
54
|
+
raise ArgumentError, "unsupported group: #{group.inspect}" unless GROUPS.include?(group)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "codec"
|
|
4
|
+
require_relative "crypto"
|
|
5
|
+
require_relative "error"
|
|
6
|
+
|
|
7
|
+
module Raiha::TLS
|
|
8
|
+
# The TLS 1.3 key schedule (RFC 9846 "Key Schedule").
|
|
9
|
+
#
|
|
10
|
+
# The class methods are the derivation functions. They are public because
|
|
11
|
+
# QUIC derives its packet protection keys from TLS traffic secrets with
|
|
12
|
+
# labels of its own ("quic key", ...), so this layer hands out secrets and
|
|
13
|
+
# the functions, never derived keys.
|
|
14
|
+
#
|
|
15
|
+
# An instance walks one handshake through the stages early, handshake,
|
|
16
|
+
# main and resumption, holding the current stage's secret and forgetting
|
|
17
|
+
# the previous one on each step, as RFC 9846 asks of secrets whose
|
|
18
|
+
# derivations are done. It also keeps the secrets still needed after they
|
|
19
|
+
# are handed out: the finished keys until used, and the current
|
|
20
|
+
# application traffic secrets for KeyUpdate. A call out of order, or one
|
|
21
|
+
# that needs a forgotten secret, raises UsageError.
|
|
22
|
+
#
|
|
23
|
+
# Forgetting is String#clear, a best effort: Ruby's GC may have copied a
|
|
24
|
+
# secret before it was cleared.
|
|
25
|
+
class KeySchedule
|
|
26
|
+
PREFIX = "tls13 "
|
|
27
|
+
|
|
28
|
+
# Every label the TLS key schedule uses, written here and nowhere else.
|
|
29
|
+
# RFC 9846 renamed "master" to "main" but kept the labels, so a name here
|
|
30
|
+
# does not have to match its label.
|
|
31
|
+
LABELS = {
|
|
32
|
+
derived: "derived",
|
|
33
|
+
external_binder: "ext binder",
|
|
34
|
+
resumption_binder: "res binder",
|
|
35
|
+
client_early_traffic: "c e traffic",
|
|
36
|
+
early_exporter: "e exp master",
|
|
37
|
+
client_handshake_traffic: "c hs traffic",
|
|
38
|
+
server_handshake_traffic: "s hs traffic",
|
|
39
|
+
client_application_traffic: "c ap traffic",
|
|
40
|
+
server_application_traffic: "s ap traffic",
|
|
41
|
+
exporter: "exp master",
|
|
42
|
+
resumption: "res master",
|
|
43
|
+
ticket_psk: "resumption",
|
|
44
|
+
exporter_value: "exporter",
|
|
45
|
+
finished: "finished",
|
|
46
|
+
key: "key",
|
|
47
|
+
iv: "iv",
|
|
48
|
+
traffic_update: "traffic upd"
|
|
49
|
+
}.freeze #: Hash[Symbol, String]
|
|
50
|
+
|
|
51
|
+
BINDER_LABELS = { external: :external_binder, resumption: :resumption_binder }.freeze #: Hash[Symbol, Symbol]
|
|
52
|
+
|
|
53
|
+
def self.extract(salt:, ikm:, hash:) = Crypto::HKDF.extract(hash:, salt:, ikm:)
|
|
54
|
+
|
|
55
|
+
# HKDF-Expand-Label. `label` is given without the "tls13 " prefix; a
|
|
56
|
+
# derivation of this implementation's own gives a prefix of its own.
|
|
57
|
+
def self.expand_label(secret:, label:, length:, hash:, context: "".b, prefix: PREFIX)
|
|
58
|
+
w = Codec::Writer.new
|
|
59
|
+
w.u16(length)
|
|
60
|
+
w.vector(1, min: 7, max: 255) { w.bytes(prefix + label) }
|
|
61
|
+
w.vector(1, max: 255) { w.bytes(context) }
|
|
62
|
+
Crypto::HKDF.expand(hash:, prk: secret, info: w.to_s, length:)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.derive_secret(secret:, label:, transcript_hash:, hash:)
|
|
66
|
+
expand_label(secret:, label:, context: transcript_hash, length: Crypto.hash_length(hash), hash:)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The outer step of TLS-Exporter, from the inner value `#exporter_values`
|
|
70
|
+
# computed for the label (RFC 9846 "Exporters"). A missing context and an
|
|
71
|
+
# empty one give the same value.
|
|
72
|
+
def self.export(inner_value:, context:, length:, hash:)
|
|
73
|
+
expand_label(secret: inner_value, label: LABELS.fetch(:exporter_value), context: Crypto.hash(hash, context), length:, hash:)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
attr_reader :hash_algorithm, :hash_length, :stage
|
|
77
|
+
|
|
78
|
+
# The early secret from `psk`, or from zeros without one. The hash is
|
|
79
|
+
# fixed here: a server knows it once the suite and PSK are chosen, a
|
|
80
|
+
# client from the PSK it offers or the ServerHello. `on_traffic_secret`
|
|
81
|
+
# is called with the name of each traffic secret and the secret as it
|
|
82
|
+
# is derived, for the keylog; a KeyUpdate's next generation is not.
|
|
83
|
+
def initialize(hash:, psk: nil, on_traffic_secret: nil)
|
|
84
|
+
@on_traffic_secret = on_traffic_secret
|
|
85
|
+
@hash_algorithm = hash
|
|
86
|
+
@hash_length = Crypto.hash_length(hash)
|
|
87
|
+
@empty_hash = Crypto.hash(hash, "".b)
|
|
88
|
+
zeros = "\x00".b * @hash_length
|
|
89
|
+
@secret = KeySchedule.extract(salt: zeros, ikm: psk || zeros, hash:)
|
|
90
|
+
@stage = :early
|
|
91
|
+
# @type var finished_keys: Hash[side, String]
|
|
92
|
+
finished_keys = {}
|
|
93
|
+
@finished_keys = finished_keys
|
|
94
|
+
# @type var application_traffic_secrets: Hash[side, String]
|
|
95
|
+
application_traffic_secrets = {}
|
|
96
|
+
@application_traffic_secrets = application_traffic_secrets
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The PSK binder over `transcript_hash`, the hash of the ClientHello up
|
|
100
|
+
# to the binders. `kind` is :external or :resumption.
|
|
101
|
+
def binder(kind:, transcript_hash:)
|
|
102
|
+
binder_key = derive(stage_secret(:early), BINDER_LABELS.fetch(kind), @empty_hash)
|
|
103
|
+
finished_key = expand(binder_key, :finished, @hash_length)
|
|
104
|
+
mac(finished_key, transcript_hash)
|
|
105
|
+
ensure
|
|
106
|
+
binder_key&.clear
|
|
107
|
+
finished_key&.clear
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Compares in constant time. A binder of the wrong length does not
|
|
111
|
+
# match.
|
|
112
|
+
def verify_binder(kind:, transcript_hash:, binder:)
|
|
113
|
+
expected = binder(kind:, transcript_hash:)
|
|
114
|
+
expected.bytesize == binder.bytesize && Crypto.fixed_length_secure_compare(expected, binder)
|
|
115
|
+
ensure
|
|
116
|
+
expected&.clear
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def client_early_traffic_secret(transcript_hash) = derive_traffic_secret(:client_early_traffic, stage_secret(:early), transcript_hash)
|
|
120
|
+
|
|
121
|
+
# To the handshake secret; forgets the early secret.
|
|
122
|
+
def handshake!(shared_secret)
|
|
123
|
+
advance(:early, :handshake) { |early| next_stage(early, shared_secret) }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Also makes the client's finished key, kept until used.
|
|
127
|
+
def client_handshake_traffic_secret(transcript_hash) = handshake_traffic_secret(:client, transcript_hash)
|
|
128
|
+
|
|
129
|
+
def server_handshake_traffic_secret(transcript_hash) = handshake_traffic_secret(:server, transcript_hash)
|
|
130
|
+
|
|
131
|
+
# To the main secret; forgets the handshake secret.
|
|
132
|
+
def main!
|
|
133
|
+
advance(:handshake, :main) { |handshake| next_stage(handshake, "\x00".b * @hash_length) }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def client_application_traffic_secret(transcript_hash) = derive_application_traffic_secret(:client, transcript_hash)
|
|
137
|
+
|
|
138
|
+
def server_application_traffic_secret(transcript_hash) = derive_application_traffic_secret(:server, transcript_hash)
|
|
139
|
+
|
|
140
|
+
# A copy of `side`'s current application traffic secret: a server
|
|
141
|
+
# derives the client's at its own Finished and hands it out only once the
|
|
142
|
+
# client's Finished verifies.
|
|
143
|
+
def application_traffic_secret(side)
|
|
144
|
+
current = @application_traffic_secrets[side] or raise UsageError, "no #{side} application traffic secret yet"
|
|
145
|
+
current.dup
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The inner exporter value of each label, Derive-Secret(exporter_secret,
|
|
149
|
+
# label, ""), from which `KeySchedule.export` makes any length. The
|
|
150
|
+
# exporter secret itself is never returned and is forgotten at once; with
|
|
151
|
+
# no labels it is not derived at all.
|
|
152
|
+
def exporter_values(labels, transcript_hash)
|
|
153
|
+
main = stage_secret(:main)
|
|
154
|
+
return {} if labels.empty?
|
|
155
|
+
exporter_secret = derive(main, :exporter, transcript_hash)
|
|
156
|
+
labels.to_h do |label|
|
|
157
|
+
[label, KeySchedule.derive_secret(secret: exporter_secret, label:, transcript_hash: @empty_hash, hash: @hash_algorithm)]
|
|
158
|
+
end
|
|
159
|
+
ensure
|
|
160
|
+
exporter_secret&.clear
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# The verify_data of our own Finished. Uses up that side's finished key.
|
|
164
|
+
def finished_verify_data(side, transcript_hash)
|
|
165
|
+
finished_key = @finished_keys.delete(side) or raise UsageError, "no #{side} finished key: derive it first, and use it once"
|
|
166
|
+
mac(finished_key, transcript_hash)
|
|
167
|
+
ensure
|
|
168
|
+
finished_key&.clear
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Whether the peer's verify_data matches, compared in constant time; one
|
|
172
|
+
# of the wrong length does not. Uses up that side's finished key.
|
|
173
|
+
def verify_finished(side, transcript_hash, verify_data)
|
|
174
|
+
expected = finished_verify_data(side, transcript_hash)
|
|
175
|
+
expected.bytesize == verify_data.bytesize && Crypto.fixed_length_secure_compare(expected, verify_data)
|
|
176
|
+
ensure
|
|
177
|
+
expected&.clear
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# To the resumption secret; forgets the main secret. The transcript runs
|
|
181
|
+
# through the client Finished.
|
|
182
|
+
def resumption!(transcript_hash)
|
|
183
|
+
advance(:main, :resumption) { |main| derive(main, :resumption, transcript_hash) }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# A copy of the resumption secret, for a client to keep in a Session.
|
|
187
|
+
def resumption_secret = stage_secret(:resumption).dup
|
|
188
|
+
|
|
189
|
+
# The PSK of the ticket that carries `ticket_nonce`.
|
|
190
|
+
def ticket_psk(ticket_nonce)
|
|
191
|
+
KeySchedule.expand_label(secret: stage_secret(:resumption), label: LABELS.fetch(:ticket_psk), context: ticket_nonce,
|
|
192
|
+
length: @hash_length, hash: @hash_algorithm)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# The next generation of `side`'s application traffic secret, for a TLS
|
|
196
|
+
# KeyUpdate; forgets the current one.
|
|
197
|
+
def update_application_traffic_secret!(side)
|
|
198
|
+
current = @application_traffic_secrets[side] or raise UsageError, "no #{side} application traffic secret yet"
|
|
199
|
+
updated = expand(current, :traffic_update, @hash_length)
|
|
200
|
+
current.clear
|
|
201
|
+
@application_traffic_secrets[side] = updated
|
|
202
|
+
updated.dup
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Forgets every secret. Nothing can be derived afterwards.
|
|
206
|
+
def discard!
|
|
207
|
+
@secret.clear
|
|
208
|
+
@finished_keys.each_value(&:clear).clear
|
|
209
|
+
@application_traffic_secrets.each_value(&:clear).clear
|
|
210
|
+
@stage = :discarded
|
|
211
|
+
self
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
private def instance_variables_to_inspect = %i[@hash_algorithm @stage]
|
|
215
|
+
|
|
216
|
+
private def handshake_traffic_secret(side, transcript_hash)
|
|
217
|
+
label = side == :client ? :client_handshake_traffic : :server_handshake_traffic
|
|
218
|
+
traffic_secret = derive_traffic_secret(label, stage_secret(:handshake), transcript_hash)
|
|
219
|
+
@finished_keys[side]&.clear
|
|
220
|
+
@finished_keys[side] = expand(traffic_secret, :finished, @hash_length)
|
|
221
|
+
traffic_secret
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Keeps the secret for KeyUpdate and returns a copy, since the copy
|
|
225
|
+
# handed out cannot be cleared from here.
|
|
226
|
+
private def derive_application_traffic_secret(side, transcript_hash)
|
|
227
|
+
label = side == :client ? :client_application_traffic : :server_application_traffic
|
|
228
|
+
traffic_secret = derive_traffic_secret(label, stage_secret(:main), transcript_hash)
|
|
229
|
+
@application_traffic_secrets[side]&.clear
|
|
230
|
+
@application_traffic_secrets[side] = traffic_secret
|
|
231
|
+
traffic_secret.dup
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
private def advance(from, to)
|
|
235
|
+
previous = stage_secret(from)
|
|
236
|
+
@secret = yield(previous)
|
|
237
|
+
previous.clear
|
|
238
|
+
@stage = to
|
|
239
|
+
self
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The Extract of the next stage, salted with Derive-Secret(., "derived", "").
|
|
243
|
+
private def next_stage(secret, ikm)
|
|
244
|
+
salt = derive(secret, :derived, @empty_hash)
|
|
245
|
+
KeySchedule.extract(salt:, ikm:, hash: @hash_algorithm)
|
|
246
|
+
ensure
|
|
247
|
+
salt&.clear
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
private def stage_secret(stage)
|
|
251
|
+
raise UsageError, "the key schedule is at #{@stage}, not #{stage}" unless @stage == stage
|
|
252
|
+
@secret
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
private def derive_traffic_secret(label, secret, transcript_hash)
|
|
256
|
+
derive(secret, label, transcript_hash).tap { |derived| @on_traffic_secret&.call(label, derived) }
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
private def derive(secret, label, transcript_hash)
|
|
260
|
+
KeySchedule.derive_secret(secret:, label: LABELS.fetch(label), transcript_hash:, hash: @hash_algorithm)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
private def expand(secret, label, length)
|
|
264
|
+
KeySchedule.expand_label(secret:, label: LABELS.fetch(label), length:, hash: @hash_algorithm)
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
private def mac(key, data) = Crypto.hmac(hash: @hash_algorithm, key:, data:)
|
|
268
|
+
end
|
|
269
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::TLS
|
|
4
|
+
# Lines of the SSLKEYLOGFILE format (RFC 9850 "The SSLKEYLOGFILE Format
|
|
5
|
+
# for TLS") for the `keylog:` of a Machine: a label, the ClientHello
|
|
6
|
+
# random and a traffic secret, in hex. Only the traffic secrets are
|
|
7
|
+
# written; the exporter secret is not, since it is not needed to decrypt
|
|
8
|
+
# and would give away every exporter value.
|
|
9
|
+
#
|
|
10
|
+
# RFC 9850 forbids using this in production. Nothing turns it on but
|
|
11
|
+
# code that passes `keylog:`; SSLKEYLOGFILE is not read.
|
|
12
|
+
module Keylog
|
|
13
|
+
LABELS = {
|
|
14
|
+
client_early_traffic: "CLIENT_EARLY_TRAFFIC_SECRET",
|
|
15
|
+
client_handshake_traffic: "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
|
|
16
|
+
server_handshake_traffic: "SERVER_HANDSHAKE_TRAFFIC_SECRET",
|
|
17
|
+
client_application_traffic: "CLIENT_TRAFFIC_SECRET_0",
|
|
18
|
+
server_application_traffic: "SERVER_TRAFFIC_SECRET_0"
|
|
19
|
+
}.freeze #: Hash[Symbol, String]
|
|
20
|
+
|
|
21
|
+
# The line for the secret KeySchedule names `name`.
|
|
22
|
+
def self.line(name, client_random, secret)
|
|
23
|
+
"#{LABELS.fetch(name)} #{client_random.unpack1("H*")} #{secret.unpack1("H*")}\n"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A keylog to give a Machine: `path` opened for appending and readable by
|
|
27
|
+
# its owner only, even when it was there already.
|
|
28
|
+
class File
|
|
29
|
+
def initialize(path)
|
|
30
|
+
@io = ::File.open(path, ::File::WRONLY | ::File::APPEND | ::File::CREAT, 0o600)
|
|
31
|
+
@io.chmod(0o600)
|
|
32
|
+
@io.sync = true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def write(line) = @io.write(line)
|
|
36
|
+
|
|
37
|
+
def close = @io.close
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|