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,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::TLS
|
|
4
|
+
module Verifier
|
|
5
|
+
# The verifier a Keyring uses unless told otherwise: the chain by RFC 5280
|
|
6
|
+
# through OpenSSL's X509::Store, at the time given, then the server's name
|
|
7
|
+
# by RFC 9525.
|
|
8
|
+
#
|
|
9
|
+
# Revocation is not checked unless `verify_flags` asks for it (for
|
|
10
|
+
# example OpenSSL::X509::V_FLAG_CRL_CHECK, with the CRLs in the store).
|
|
11
|
+
class Default
|
|
12
|
+
# OpenSSL's verification errors, mapped to the alert RFC 9846 has for
|
|
13
|
+
# them; anything else is bad_certificate.
|
|
14
|
+
ALERTS = {
|
|
15
|
+
OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED => :certificate_expired,
|
|
16
|
+
OpenSSL::X509::V_ERR_CERT_REVOKED => :certificate_revoked,
|
|
17
|
+
OpenSSL::X509::V_ERR_UNABLE_TO_GET_ISSUER_CERT => :unknown_ca,
|
|
18
|
+
OpenSSL::X509::V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY => :unknown_ca,
|
|
19
|
+
OpenSSL::X509::V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT => :unknown_ca,
|
|
20
|
+
OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN => :unknown_ca,
|
|
21
|
+
OpenSSL::X509::V_ERR_INVALID_PURPOSE => :unsupported_certificate
|
|
22
|
+
}.freeze #: Hash[Integer, Symbol]
|
|
23
|
+
|
|
24
|
+
# The verify_flags that check revocation.
|
|
25
|
+
REVOCATION_FLAGS = OpenSSL::X509::V_FLAG_CRL_CHECK | OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
|
|
26
|
+
|
|
27
|
+
PURPOSES = {
|
|
28
|
+
server: OpenSSL::X509::PURPOSE_SSL_SERVER,
|
|
29
|
+
client: OpenSSL::X509::PURPOSE_SSL_CLIENT
|
|
30
|
+
}.freeze #: Hash[Symbol, Integer]
|
|
31
|
+
|
|
32
|
+
# An X509::Store trusting exactly `trust_anchors`, given as DER.
|
|
33
|
+
def self.store(trust_anchors)
|
|
34
|
+
store = OpenSSL::X509::Store.new
|
|
35
|
+
trust_anchors.each do |der|
|
|
36
|
+
store.add_cert(Crypto::Certificate.parse(der) || raise(ArgumentError, "a trust anchor is not a DER certificate"))
|
|
37
|
+
end
|
|
38
|
+
store
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
attr_reader :verify_flags
|
|
42
|
+
|
|
43
|
+
def initialize(store:, verify_flags: 0)
|
|
44
|
+
@store = store
|
|
45
|
+
@verify_flags = verify_flags
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def verify(chain:, server_name:, now:, purpose:)
|
|
49
|
+
leaf = chain.first or return failure(:bad_certificate)
|
|
50
|
+
context = OpenSSL::X509::StoreContext.new(@store, leaf, chain.drop(1))
|
|
51
|
+
context.time = now
|
|
52
|
+
context.flags = @verify_flags unless @verify_flags.zero?
|
|
53
|
+
context.purpose = PURPOSES.fetch(purpose)
|
|
54
|
+
return failure(ALERTS.fetch(context.error, :bad_certificate)) unless context.verify
|
|
55
|
+
if purpose == :server && (server_name.nil? || !HostName.match?(leaf, server_name))
|
|
56
|
+
return failure(:bad_certificate)
|
|
57
|
+
end
|
|
58
|
+
Result.new(ok: true, reason: nil, verified_chain: context.chain || chain)
|
|
59
|
+
rescue OpenSSL::X509::StoreError, OpenSSL::X509::CertificateError
|
|
60
|
+
failure(:bad_certificate)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Nil when revocation is checked, which a resumption, holding no more
|
|
64
|
+
# than the leaf, could not check again.
|
|
65
|
+
def resumption_policy
|
|
66
|
+
"default;verify_flags=#{@verify_flags}" if (@verify_flags & REVOCATION_FLAGS).zero?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private def failure(alert) = Result.new(ok: false, reason: alert, verified_chain: [])
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ipaddr"
|
|
4
|
+
|
|
5
|
+
module Raiha::TLS
|
|
6
|
+
module Verifier
|
|
7
|
+
# Whether a certificate is valid for a name, by RFC 9525.
|
|
8
|
+
#
|
|
9
|
+
# Only subjectAltName counts: a DNS name against the dNSName entries, an
|
|
10
|
+
# IP address octet for octet against the iPAddress entries. There is no
|
|
11
|
+
# fallback to the subject's commonName, which RFC 9525 ended. A wildcard
|
|
12
|
+
# is valid only as the whole leftmost label, and then matches exactly
|
|
13
|
+
# one label; a presented name with a "*" anywhere else is ignored.
|
|
14
|
+
module HostName
|
|
15
|
+
SUBJECT_ALT_NAME = "subjectAltName"
|
|
16
|
+
DNS_NAME = 2
|
|
17
|
+
IP_ADDRESS = 7
|
|
18
|
+
|
|
19
|
+
# `name` is in the normal form a handshake uses: ASCII lowercase with
|
|
20
|
+
# no trailing dot, or an IP address literal.
|
|
21
|
+
def self.match?(certificate, name)
|
|
22
|
+
dns_names, ip_addresses = subject_alt_names(certificate)
|
|
23
|
+
if (address = ip_address(name))
|
|
24
|
+
ip_addresses.include?(address.hton)
|
|
25
|
+
else
|
|
26
|
+
dns_names.any? { |presented| dns_name_match?(presented, name) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# IPv6 may come in brackets, as in a URI.
|
|
31
|
+
def self.ip_address(name)
|
|
32
|
+
literal = name.delete_prefix("[").delete_suffix("]")
|
|
33
|
+
return nil unless literal.match?(/\A[0-9.]+\z/) || literal.include?(":")
|
|
34
|
+
IPAddr.new(literal)
|
|
35
|
+
rescue IPAddr::Error
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.dns_name_match?(presented, name)
|
|
40
|
+
presented = presented.downcase.delete_suffix(".")
|
|
41
|
+
return presented == name unless presented.include?("*")
|
|
42
|
+
return false unless presented.start_with?("*.") && presented.count("*") == 1
|
|
43
|
+
suffix = presented.delete_prefix("*")
|
|
44
|
+
first_label, rest = name.split(".", 2)
|
|
45
|
+
!first_label.nil? && !first_label.empty? && !rest.nil? && ".#{rest}" == suffix
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The dNSName and iPAddress entries of the subjectAltName extension;
|
|
49
|
+
# none when the extension is absent or malformed.
|
|
50
|
+
def self.subject_alt_names(certificate)
|
|
51
|
+
# @type var dns_names: Array[String]
|
|
52
|
+
dns_names = []
|
|
53
|
+
# @type var ip_addresses: Array[String]
|
|
54
|
+
ip_addresses = []
|
|
55
|
+
extension = certificate.extensions.find { |ext| ext.oid == SUBJECT_ALT_NAME }
|
|
56
|
+
return [dns_names, ip_addresses] unless extension
|
|
57
|
+
general_names = OpenSSL::ASN1.decode(extension.value_der).value
|
|
58
|
+
return [dns_names, ip_addresses] unless general_names.is_a?(Array)
|
|
59
|
+
general_names.each do |general_name|
|
|
60
|
+
next unless general_name.tag_class == :CONTEXT_SPECIFIC
|
|
61
|
+
value = general_name.value
|
|
62
|
+
value = value.first.value if value.is_a?(Array)
|
|
63
|
+
next unless value.is_a?(String)
|
|
64
|
+
case general_name.tag
|
|
65
|
+
when DNS_NAME then dns_names << value
|
|
66
|
+
when IP_ADDRESS then ip_addresses << value.b
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
[dns_names, ip_addresses]
|
|
70
|
+
rescue OpenSSL::ASN1::ASN1Error
|
|
71
|
+
[[], []]
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::TLS
|
|
4
|
+
module Verifier
|
|
5
|
+
# Accepts any chain. For tests only: a handshake that used it reports
|
|
6
|
+
# `peer_verified: false` in Event::HandshakeComplete, so it shows if it
|
|
7
|
+
# reaches production.
|
|
8
|
+
class None
|
|
9
|
+
def verify(chain:, server_name:, now:, purpose:) = Result.new(ok: true, reason: nil, verified_chain: chain)
|
|
10
|
+
|
|
11
|
+
def resumption_policy = "none"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
module Raiha::TLS
|
|
6
|
+
# Decides whether the peer's certificate chain is acceptable.
|
|
7
|
+
#
|
|
8
|
+
# A verifier answers `verify(chain:, server_name:, now:, purpose:)` with a
|
|
9
|
+
# Result, and `resumption_policy` with a String that changes whenever what
|
|
10
|
+
# it accepts changes (nil if it cannot say). A failure is a Result, never
|
|
11
|
+
# an exception: the chain comes from the peer, so an exception would be one
|
|
12
|
+
# the peer can raise. An exception that escapes anyway is not caught; the
|
|
13
|
+
# handshake forgets its secrets, fails and lets it through.
|
|
14
|
+
module Verifier
|
|
15
|
+
# `reason` is the alert to send when `ok` is false. `verified_chain` is
|
|
16
|
+
# the chain as verified, leaf first.
|
|
17
|
+
class Result < Data.define(:ok, :reason, :verified_chain)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
require_relative "verifier/host_name"
|
|
23
|
+
require_relative "verifier/default"
|
|
24
|
+
require_relative "verifier/none"
|
data/lib/raiha/tls.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "namespace"
|
|
4
|
+
|
|
5
|
+
# TLS 1.3. Depends on nothing but openssl.
|
|
6
|
+
module Raiha::TLS
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
require_relative "tls/codec"
|
|
10
|
+
require_relative "tls/iana"
|
|
11
|
+
require_relative "tls/crypto"
|
|
12
|
+
require_relative "tls/error"
|
|
13
|
+
require_relative "tls/key_schedule"
|
|
14
|
+
require_relative "tls/keylog"
|
|
15
|
+
require_relative "tls/transcript"
|
|
16
|
+
require_relative "tls/cipher_suite"
|
|
17
|
+
require_relative "tls/signature_scheme"
|
|
18
|
+
require_relative "tls/key_exchange"
|
|
19
|
+
require_relative "tls/extension"
|
|
20
|
+
require_relative "tls/handshake"
|
|
21
|
+
require_relative "tls/profile"
|
|
22
|
+
require_relative "tls/output"
|
|
23
|
+
require_relative "tls/event"
|
|
24
|
+
require_relative "tls/trace"
|
|
25
|
+
require_relative "tls/ticket_keys"
|
|
26
|
+
require_relative "tls/ticket"
|
|
27
|
+
require_relative "tls/session"
|
|
28
|
+
require_relative "tls/anti_replay"
|
|
29
|
+
require_relative "tls/config"
|
|
30
|
+
require_relative "tls/verifier"
|
|
31
|
+
require_relative "tls/keyring"
|
|
32
|
+
require_relative "tls/server_name"
|
|
33
|
+
require_relative "tls/record"
|
|
34
|
+
require_relative "tls/tcp"
|
data/lib/raiha/version.rb
CHANGED
data/lib/raiha.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
require_relative "raiha/version"
|
|
4
|
+
require_relative "raiha/namespace"
|
|
5
|
+
require_relative "raiha/tls"
|
|
6
|
+
require_relative "raiha/quic"
|
|
7
|
+
require_relative "raiha/http3"
|
|
8
|
+
require_relative "raiha/http3_binding"
|
|
9
|
+
require_relative "raiha/runtime"
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# rbs 4.2 declares the forms of Array#[]= that take an array as
|
|
2
|
+
# `[T < _ToAry[E]] (..., T array) -> T`, and steep 2.1 does not resolve an
|
|
3
|
+
# upper bound that names the class's own type parameter, so no array is
|
|
4
|
+
# accepted there. The form raiha uses is added with `| ...`, as in
|
|
5
|
+
# string.rbs.
|
|
6
|
+
class Array[unchecked out E]
|
|
7
|
+
def []=: (int start, int length, Array[E] array) -> Array[E]
|
|
8
|
+
| ...
|
|
9
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Ruby 3.3 added bitwise operators and a single-argument slice overload to
|
|
2
|
+
# IO::Buffer that aren't yet declared in the bundled rbs core stubs.
|
|
3
|
+
class IO
|
|
4
|
+
class Buffer
|
|
5
|
+
def xor!: (Buffer mask) -> Buffer
|
|
6
|
+
def and!: (Buffer mask) -> Buffer
|
|
7
|
+
def or!: (Buffer mask) -> Buffer
|
|
8
|
+
def not!: -> Buffer
|
|
9
|
+
|
|
10
|
+
def slice: (Integer offset) -> Buffer
|
|
11
|
+
| (Integer offset, Integer length) -> Buffer
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# OpenSSL additions not covered by the bundled rbs core stubs.
|
|
2
|
+
module OpenSSL
|
|
3
|
+
module PKey
|
|
4
|
+
def self.new_raw_public_key: (String group, String key_exchange) -> PKey
|
|
5
|
+
def self.new_raw_private_key: (String algorithm, String key) -> PKey
|
|
6
|
+
def self.generate_key: (String group) -> PKey
|
|
7
|
+
|
|
8
|
+
class PKey
|
|
9
|
+
def raw_public_key: -> String
|
|
10
|
+
def verify_pss: (String digest, String signature, String data, salt_length: Integer | Symbol, mgf1_hash: String) -> bool
|
|
11
|
+
def sign_pss: (String digest, String data, salt_length: Integer | Symbol, mgf1_hash: String) -> String
|
|
12
|
+
# A nil digest for Ed25519 and Ed448, which hash on their own.
|
|
13
|
+
def verify: (String | OpenSSL::Digest | nil digest, String signature, String data) -> bool
|
|
14
|
+
def sign: (String | OpenSSL::Digest | nil digest, String data) -> String
|
|
15
|
+
def oid: () -> String
|
|
16
|
+
def private?: () -> bool
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Cipher
|
|
21
|
+
# openssl 4.0: an AEAD tag that does not authenticate.
|
|
22
|
+
class AuthTagError < CipherError
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Socket::AncillaryData.int takes the int it carries as its last argument,
|
|
2
|
+
# which the bundled declaration types as a Symbol like the other three.
|
|
3
|
+
# Added to it with `| ...`. Linux's IPV6_MTU_DISCOVER, which Socket defines
|
|
4
|
+
# there, the bundled declaration does not name.
|
|
5
|
+
#
|
|
6
|
+
# BasicSocket#getsockopt returns a Socket::Option, which the bundled
|
|
7
|
+
# declaration has as the value inside it (Integer | boolish | String) and
|
|
8
|
+
# does not declare.
|
|
9
|
+
class Socket
|
|
10
|
+
class Option
|
|
11
|
+
def int: () -> Integer
|
|
12
|
+
def bool: () -> bool
|
|
13
|
+
def data: () -> String
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class BasicSocket
|
|
18
|
+
def getsockopt: (Symbol | Integer level, Symbol | Integer optname) -> Socket::Option
|
|
19
|
+
| ...
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Socket
|
|
23
|
+
IPV6_MTU_DISCOVER: Integer
|
|
24
|
+
|
|
25
|
+
class AncillaryData
|
|
26
|
+
def self.int: (Symbol family, Symbol level, Symbol type, Integer value) -> instance
|
|
27
|
+
| ...
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# String#unpack1 / #unpack format-string specific overloads, added to the
|
|
2
|
+
# core declarations with `| ...`, not written as definitions of their own,
|
|
3
|
+
# which RBS rejects as duplicates of the core ones; String then has no
|
|
4
|
+
# definition at all and every method on a String goes unchecked.
|
|
5
|
+
# The bundled
|
|
6
|
+
# rbs core declaration returns `(Integer | Float | String)?` regardless
|
|
7
|
+
# of the format string, but raiha's binary parsers always rely on
|
|
8
|
+
# unsigned-integer formats (`C`, `n`, `N`, `Q>`) which deterministically
|
|
9
|
+
# yield an Integer. Without these overloads every parse site would need
|
|
10
|
+
# its own `#: Integer` annotation, which is impractical to write inside
|
|
11
|
+
# chained expressions.
|
|
12
|
+
class String
|
|
13
|
+
def unpack1: ("C" template, ?offset: int) -> Integer
|
|
14
|
+
| ("n" template, ?offset: int) -> Integer
|
|
15
|
+
| ("N" template, ?offset: int) -> Integer
|
|
16
|
+
| ("Q>" template, ?offset: int) -> Integer
|
|
17
|
+
| ("L>" template, ?offset: int) -> Integer
|
|
18
|
+
| ("C*" template, ?offset: int) -> Integer
|
|
19
|
+
| ("H*" template, ?offset: int) -> String
|
|
20
|
+
| ...
|
|
21
|
+
|
|
22
|
+
def unpack: ("CCC" template, ?offset: int) -> Array[Integer]
|
|
23
|
+
| ("CCCCCCC" template, ?offset: int) -> Array[Integer]
|
|
24
|
+
| ("C*" template, ?offset: int) -> Array[Integer]
|
|
25
|
+
| ...
|
|
26
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# strscan takes a String as a pattern as well as a Regexp, matched as it
|
|
2
|
+
# is (strscan 3.1, as Ruby 4.0 bundles it), but the bundled rbs
|
|
3
|
+
# declaration takes only a Regexp. The String overloads are added with
|
|
4
|
+
# `| ...`, as in string.rbs.
|
|
5
|
+
class StringScanner
|
|
6
|
+
def skip: (String pattern) -> Integer?
|
|
7
|
+
| ...
|
|
8
|
+
def scan: (String pattern) -> String?
|
|
9
|
+
| ...
|
|
10
|
+
def match?: (String pattern) -> Integer?
|
|
11
|
+
| ...
|
|
12
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module Raiha::HTTP3
|
|
2
|
+
class Capsule < Data
|
|
3
|
+
DATAGRAM: Integer
|
|
4
|
+
|
|
5
|
+
attr_reader type: Integer
|
|
6
|
+
attr_reader value: String
|
|
7
|
+
|
|
8
|
+
def self.new: (type: Integer, value: String) -> instance
|
|
9
|
+
| (Integer type, String value) -> instance
|
|
10
|
+
|
|
11
|
+
def initialize: (type: Integer, value: String) -> void
|
|
12
|
+
|
|
13
|
+
def self.encode: (Capsule capsule) -> String
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class CapsuleReader
|
|
17
|
+
MAX_HEADER: Integer
|
|
18
|
+
|
|
19
|
+
@max_value: Integer
|
|
20
|
+
@header: String
|
|
21
|
+
@type: Integer?
|
|
22
|
+
@left: Integer
|
|
23
|
+
@value: String?
|
|
24
|
+
|
|
25
|
+
def initialize: (max_value: Integer) -> void
|
|
26
|
+
def mid_capsule?: () -> bool
|
|
27
|
+
def feed: (String bytes) { (Capsule capsule) -> void } -> void
|
|
28
|
+
private def read_header: (String bytes, Integer offset) { (Capsule capsule) -> void } -> Integer
|
|
29
|
+
private def read_value: (String bytes, Integer offset) { (Capsule capsule) -> void } -> Integer
|
|
30
|
+
private def finish: () { (Capsule capsule) -> void } -> void
|
|
31
|
+
end
|
|
32
|
+
end
|