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.
Files changed (347) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/LICENSE +202 -0
  4. data/README.md +94 -22
  5. data/docs/configuration.md +227 -0
  6. data/docs/development.md +99 -0
  7. data/docs/fiber-scheduler.md +591 -0
  8. data/docs/getting-started.md +190 -0
  9. data/docs/http3-client.md +303 -0
  10. data/docs/http3-server.md +301 -0
  11. data/docs/platforms.md +98 -0
  12. data/docs/qlog.md +86 -0
  13. data/docs/quic.md +268 -0
  14. data/docs/threads.md +127 -0
  15. data/examples/_certificate.rb +55 -0
  16. data/examples/high_level_loopback.rb +59 -0
  17. data/examples/http3_get_public.rb +68 -0
  18. data/examples/in_memory_handshake.rb +38 -0
  19. data/examples/qlog_output.rb +58 -0
  20. data/examples/qlog_roundtrips.rb +66 -0
  21. data/examples/quic_gem_loopback.rb +62 -0
  22. data/examples/stream_echo.rb +68 -0
  23. data/examples/udp_handshake.rb +76 -0
  24. data/lib/raiha/http3/action.rb +21 -0
  25. data/lib/raiha/http3/capsule.rb +86 -0
  26. data/lib/raiha/http3/connection.rb +983 -0
  27. data/lib/raiha/http3/error.rb +24 -0
  28. data/lib/raiha/http3/error_code.rb +27 -0
  29. data/lib/raiha/http3/event.rb +63 -0
  30. data/lib/raiha/http3/frame.rb +148 -0
  31. data/lib/raiha/http3/frame_reader.rb +114 -0
  32. data/lib/raiha/http3/message.rb +137 -0
  33. data/lib/raiha/http3/output.rb +61 -0
  34. data/lib/raiha/http3/priority.rb +52 -0
  35. data/lib/raiha/http3/qlog.rb +145 -0
  36. data/lib/raiha/http3/qpack/decoder.rb +302 -0
  37. data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
  38. data/lib/raiha/http3/qpack/encoder.rb +94 -0
  39. data/lib/raiha/http3/qpack/error.rb +39 -0
  40. data/lib/raiha/http3/qpack/error_code.rb +13 -0
  41. data/lib/raiha/http3/qpack/field.rb +15 -0
  42. data/lib/raiha/http3/qpack/huffman.rb +397 -0
  43. data/lib/raiha/http3/qpack/reader.rb +82 -0
  44. data/lib/raiha/http3/qpack/result.rb +26 -0
  45. data/lib/raiha/http3/qpack/static_table.rb +139 -0
  46. data/lib/raiha/http3/qpack/writer.rb +44 -0
  47. data/lib/raiha/http3/qpack.rb +26 -0
  48. data/lib/raiha/http3/receiving.rb +46 -0
  49. data/lib/raiha/http3/sending.rb +23 -0
  50. data/lib/raiha/http3/session.rb +31 -0
  51. data/lib/raiha/http3/settings.rb +81 -0
  52. data/lib/raiha/http3/structured_field.rb +120 -0
  53. data/lib/raiha/http3/tracer.rb +26 -0
  54. data/lib/raiha/http3/varint.rb +45 -0
  55. data/lib/raiha/http3.rb +19 -0
  56. data/lib/raiha/http3_binding.rb +139 -0
  57. data/lib/raiha/namespace.rb +11 -0
  58. data/lib/raiha/quic/config.rb +229 -0
  59. data/lib/raiha/quic/congestion/algorithms.rb +17 -0
  60. data/lib/raiha/quic/congestion/cubic.rb +258 -0
  61. data/lib/raiha/quic/congestion/new_reno.rb +161 -0
  62. data/lib/raiha/quic/congestion/pacing.rb +21 -0
  63. data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
  64. data/lib/raiha/quic/congestion/startup.rb +13 -0
  65. data/lib/raiha/quic/congestion.rb +15 -0
  66. data/lib/raiha/quic/connection.rb +2022 -0
  67. data/lib/raiha/quic/connection_ids.rb +62 -0
  68. data/lib/raiha/quic/crypto/aead.rb +68 -0
  69. data/lib/raiha/quic/crypto/header_protection.rb +42 -0
  70. data/lib/raiha/quic/crypto/initial.rb +23 -0
  71. data/lib/raiha/quic/crypto/keys.rb +32 -0
  72. data/lib/raiha/quic/crypto/mac.rb +15 -0
  73. data/lib/raiha/quic/crypto/random.rb +13 -0
  74. data/lib/raiha/quic/crypto.rb +18 -0
  75. data/lib/raiha/quic/crypto_stream.rb +161 -0
  76. data/lib/raiha/quic/datagrams.rb +91 -0
  77. data/lib/raiha/quic/endpoint.rb +785 -0
  78. data/lib/raiha/quic/error.rb +61 -0
  79. data/lib/raiha/quic/error_code.rb +34 -0
  80. data/lib/raiha/quic/event.rb +59 -0
  81. data/lib/raiha/quic/issued_connection_ids.rb +137 -0
  82. data/lib/raiha/quic/kept_packets.rb +41 -0
  83. data/lib/raiha/quic/mtu_search.rb +75 -0
  84. data/lib/raiha/quic/negotiation.rb +143 -0
  85. data/lib/raiha/quic/new_token.rb +55 -0
  86. data/lib/raiha/quic/pacer.rb +52 -0
  87. data/lib/raiha/quic/packet_protection.rb +310 -0
  88. data/lib/raiha/quic/path.rb +473 -0
  89. data/lib/raiha/quic/peer_connection_ids.rb +160 -0
  90. data/lib/raiha/quic/qlog.rb +260 -0
  91. data/lib/raiha/quic/ranges.rb +32 -0
  92. data/lib/raiha/quic/recovery/duty.rb +102 -0
  93. data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
  94. data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
  95. data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
  96. data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
  97. data/lib/raiha/quic/recovery.rb +641 -0
  98. data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
  99. data/lib/raiha/quic/retry_token.rb +54 -0
  100. data/lib/raiha/quic/route.rb +37 -0
  101. data/lib/raiha/quic/scheduler.rb +112 -0
  102. data/lib/raiha/quic/session.rb +47 -0
  103. data/lib/raiha/quic/stateless_reset.rb +38 -0
  104. data/lib/raiha/quic/stats.rb +24 -0
  105. data/lib/raiha/quic/streams/receive_window.rb +119 -0
  106. data/lib/raiha/quic/streams/receiver.rb +217 -0
  107. data/lib/raiha/quic/streams/sender.rb +200 -0
  108. data/lib/raiha/quic/streams.rb +687 -0
  109. data/lib/raiha/quic/test/bottleneck.rb +55 -0
  110. data/lib/raiha/quic/test/congestion_contract.rb +106 -0
  111. data/lib/raiha/quic/test/network.rb +244 -0
  112. data/lib/raiha/quic/test/random_loss.rb +34 -0
  113. data/lib/raiha/quic/test/reorder.rb +51 -0
  114. data/lib/raiha/quic/test/server.rb +54 -0
  115. data/lib/raiha/quic/test/vectors.rb +337 -0
  116. data/lib/raiha/quic/test/virtual_clock.rb +21 -0
  117. data/lib/raiha/quic/test.rb +17 -0
  118. data/lib/raiha/quic/tls_binding.rb +179 -0
  119. data/lib/raiha/quic/token.rb +28 -0
  120. data/lib/raiha/quic/tracer.rb +127 -0
  121. data/lib/raiha/quic/transmit.rb +14 -0
  122. data/lib/raiha/quic/transport_parameters.rb +244 -0
  123. data/lib/raiha/quic/version.rb +63 -0
  124. data/lib/raiha/quic/wire/codec.rb +136 -0
  125. data/lib/raiha/quic/wire/frame.rb +521 -0
  126. data/lib/raiha/quic/wire/header.rb +90 -0
  127. data/lib/raiha/quic/wire/packet.rb +130 -0
  128. data/lib/raiha/quic/wire/packet_number.rb +47 -0
  129. data/lib/raiha/quic/wire/retry.rb +62 -0
  130. data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
  131. data/lib/raiha/quic/wire.rb +9 -0
  132. data/lib/raiha/quic.rb +46 -0
  133. data/lib/raiha/runtime/addresses.rb +35 -0
  134. data/lib/raiha/runtime/admission.rb +37 -0
  135. data/lib/raiha/runtime/client.rb +190 -0
  136. data/lib/raiha/runtime/driver.rb +165 -0
  137. data/lib/raiha/runtime/error.rb +16 -0
  138. data/lib/raiha/runtime/http3.rb +363 -0
  139. data/lib/raiha/runtime/http3_fields.rb +38 -0
  140. data/lib/raiha/runtime/http3_server.rb +416 -0
  141. data/lib/raiha/runtime/kept.rb +36 -0
  142. data/lib/raiha/runtime/qlog.rb +204 -0
  143. data/lib/raiha/runtime/rack.rb +180 -0
  144. data/lib/raiha/runtime/server.rb +334 -0
  145. data/lib/raiha/runtime/session.rb +447 -0
  146. data/lib/raiha/runtime/threaded/client.rb +161 -0
  147. data/lib/raiha/runtime/threaded/driver.rb +230 -0
  148. data/lib/raiha/runtime/threaded/http3.rb +287 -0
  149. data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
  150. data/lib/raiha/runtime/threaded/server.rb +290 -0
  151. data/lib/raiha/runtime/threaded/session.rb +604 -0
  152. data/lib/raiha/runtime/threaded.rb +45 -0
  153. data/lib/raiha/runtime/udp.rb +410 -0
  154. data/lib/raiha/runtime/worker.rb +138 -0
  155. data/lib/raiha/runtime/workers.rb +248 -0
  156. data/lib/raiha/runtime.rb +37 -0
  157. data/lib/raiha/tls/anti_replay.rb +139 -0
  158. data/lib/raiha/tls/cipher_suite.rb +62 -0
  159. data/lib/raiha/tls/codec/error.rb +43 -0
  160. data/lib/raiha/tls/codec/reader.rb +95 -0
  161. data/lib/raiha/tls/codec/writer.rb +81 -0
  162. data/lib/raiha/tls/codec.rb +5 -0
  163. data/lib/raiha/tls/config.rb +318 -0
  164. data/lib/raiha/tls/crypto/aead.rb +64 -0
  165. data/lib/raiha/tls/crypto/certificate.rb +28 -0
  166. data/lib/raiha/tls/crypto/digest.rb +30 -0
  167. data/lib/raiha/tls/crypto/ecdh.rb +74 -0
  168. data/lib/raiha/tls/crypto/hash.rb +22 -0
  169. data/lib/raiha/tls/crypto/hkdf.rb +28 -0
  170. data/lib/raiha/tls/crypto/signature.rb +87 -0
  171. data/lib/raiha/tls/crypto.rb +19 -0
  172. data/lib/raiha/tls/error.rb +36 -0
  173. data/lib/raiha/tls/event.rb +45 -0
  174. data/lib/raiha/tls/extension/alpn.rb +35 -0
  175. data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
  176. data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
  177. data/lib/raiha/tls/extension/cookie.rb +27 -0
  178. data/lib/raiha/tls/extension/early_data.rb +54 -0
  179. data/lib/raiha/tls/extension/key_share.rb +90 -0
  180. data/lib/raiha/tls/extension/list.rb +49 -0
  181. data/lib/raiha/tls/extension/opaque.rb +21 -0
  182. data/lib/raiha/tls/extension/placement.rb +55 -0
  183. data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
  184. data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
  185. data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
  186. data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
  187. data/lib/raiha/tls/extension/registry.rb +50 -0
  188. data/lib/raiha/tls/extension/server_name.rb +63 -0
  189. data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
  190. data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
  191. data/lib/raiha/tls/extension/supported_groups.rb +32 -0
  192. data/lib/raiha/tls/extension/supported_versions.rb +53 -0
  193. data/lib/raiha/tls/extension.rb +32 -0
  194. data/lib/raiha/tls/handshake/acceptor.rb +164 -0
  195. data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
  196. data/lib/raiha/tls/handshake/certificate.rb +56 -0
  197. data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
  198. data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
  199. data/lib/raiha/tls/handshake/client_hello.rb +208 -0
  200. data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
  201. data/lib/raiha/tls/handshake/client_side.rb +578 -0
  202. data/lib/raiha/tls/handshake/conductor.rb +390 -0
  203. data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
  204. data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
  205. data/lib/raiha/tls/handshake/finished.rb +36 -0
  206. data/lib/raiha/tls/handshake/key_update.rb +38 -0
  207. data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
  208. data/lib/raiha/tls/handshake/machine.rb +238 -0
  209. data/lib/raiha/tls/handshake/message.rb +25 -0
  210. data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
  211. data/lib/raiha/tls/handshake/reassembler.rb +88 -0
  212. data/lib/raiha/tls/handshake/server_hello.rb +98 -0
  213. data/lib/raiha/tls/handshake/server_side.rb +672 -0
  214. data/lib/raiha/tls/handshake/violation.rb +12 -0
  215. data/lib/raiha/tls/handshake.rb +62 -0
  216. data/lib/raiha/tls/iana.rb +113 -0
  217. data/lib/raiha/tls/key_exchange.rb +58 -0
  218. data/lib/raiha/tls/key_schedule.rb +269 -0
  219. data/lib/raiha/tls/keylog.rb +40 -0
  220. data/lib/raiha/tls/keyring.rb +180 -0
  221. data/lib/raiha/tls/masked_inspect.rb +33 -0
  222. data/lib/raiha/tls/output.rb +32 -0
  223. data/lib/raiha/tls/profile.rb +82 -0
  224. data/lib/raiha/tls/record/layer.rb +228 -0
  225. data/lib/raiha/tls/record/protection.rb +67 -0
  226. data/lib/raiha/tls/record.rb +11 -0
  227. data/lib/raiha/tls/server_name.rb +42 -0
  228. data/lib/raiha/tls/session.rb +106 -0
  229. data/lib/raiha/tls/signature_scheme.rb +35 -0
  230. data/lib/raiha/tls/tcp/acceptor.rb +121 -0
  231. data/lib/raiha/tls/tcp/connection.rb +371 -0
  232. data/lib/raiha/tls/tcp/event.rb +15 -0
  233. data/lib/raiha/tls/tcp/socket.rb +161 -0
  234. data/lib/raiha/tls/tcp.rb +14 -0
  235. data/lib/raiha/tls/test/fixed_kem.rb +30 -0
  236. data/lib/raiha/tls/test/fixed_random.rb +18 -0
  237. data/lib/raiha/tls/test/rfc8448.rb +923 -0
  238. data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
  239. data/lib/raiha/tls/test.rb +13 -0
  240. data/lib/raiha/tls/ticket.rb +184 -0
  241. data/lib/raiha/tls/ticket_keys.rb +206 -0
  242. data/lib/raiha/tls/trace.rb +58 -0
  243. data/lib/raiha/tls/transcript.rb +94 -0
  244. data/lib/raiha/tls/verifier/default.rb +72 -0
  245. data/lib/raiha/tls/verifier/host_name.rb +75 -0
  246. data/lib/raiha/tls/verifier/none.rb +14 -0
  247. data/lib/raiha/tls/verifier.rb +24 -0
  248. data/lib/raiha/tls.rb +34 -0
  249. data/lib/raiha/version.rb +3 -1
  250. data/lib/raiha.rb +8 -5
  251. data/sig/patches/array.rbs +9 -0
  252. data/sig/patches/io_buffer.rbs +13 -0
  253. data/sig/patches/openssl.rbs +25 -0
  254. data/sig/patches/socket.rbs +29 -0
  255. data/sig/patches/string.rbs +26 -0
  256. data/sig/patches/strscan.rbs +12 -0
  257. data/sig/raiha/http3/capsule.rbs +32 -0
  258. data/sig/raiha/http3/connection.rbs +324 -0
  259. data/sig/raiha/http3/frame.rbs +138 -0
  260. data/sig/raiha/http3/frame_reader.rbs +39 -0
  261. data/sig/raiha/http3/message.rbs +23 -0
  262. data/sig/raiha/http3/priority.rbs +40 -0
  263. data/sig/raiha/http3/qlog.rbs +54 -0
  264. data/sig/raiha/http3/qpack.rbs +246 -0
  265. data/sig/raiha/http3/session.rbs +11 -0
  266. data/sig/raiha/http3/settings.rbs +26 -0
  267. data/sig/raiha/http3.rbs +2 -0
  268. data/sig/raiha/http3_binding.rbs +26 -0
  269. data/sig/raiha/namespace.rbs +2 -0
  270. data/sig/raiha/quic/config.rbs +89 -0
  271. data/sig/raiha/quic/congestion.rbs +213 -0
  272. data/sig/raiha/quic/connection.rbs +249 -0
  273. data/sig/raiha/quic/connection_ids.rbs +34 -0
  274. data/sig/raiha/quic/crypto.rbs +78 -0
  275. data/sig/raiha/quic/crypto_stream.rbs +34 -0
  276. data/sig/raiha/quic/datagrams.rbs +32 -0
  277. data/sig/raiha/quic/endpoint.rbs +179 -0
  278. data/sig/raiha/quic/frame.rbs +387 -0
  279. data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
  280. data/sig/raiha/quic/kept_packets.rbs +25 -0
  281. data/sig/raiha/quic/mtu_search.rbs +21 -0
  282. data/sig/raiha/quic/negotiation.rbs +33 -0
  283. data/sig/raiha/quic/new_token.rbs +14 -0
  284. data/sig/raiha/quic/pacer.rbs +16 -0
  285. data/sig/raiha/quic/packet_protection.rbs +63 -0
  286. data/sig/raiha/quic/path.rbs +106 -0
  287. data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
  288. data/sig/raiha/quic/qlog.rbs +74 -0
  289. data/sig/raiha/quic/ranges.rbs +7 -0
  290. data/sig/raiha/quic/recovery.rbs +376 -0
  291. data/sig/raiha/quic/retry_token.rbs +21 -0
  292. data/sig/raiha/quic/route.rbs +51 -0
  293. data/sig/raiha/quic/scheduler.rbs +36 -0
  294. data/sig/raiha/quic/session.rbs +17 -0
  295. data/sig/raiha/quic/stateless_reset.rbs +10 -0
  296. data/sig/raiha/quic/streams.rbs +244 -0
  297. data/sig/raiha/quic/test.rbs +354 -0
  298. data/sig/raiha/quic/tls_binding.rbs +58 -0
  299. data/sig/raiha/quic/token.rbs +11 -0
  300. data/sig/raiha/quic/tracer.rbs +71 -0
  301. data/sig/raiha/quic/transport_parameters.rbs +113 -0
  302. data/sig/raiha/quic/values.rbs +200 -0
  303. data/sig/raiha/quic/version.rbs +30 -0
  304. data/sig/raiha/quic/wire.rbs +199 -0
  305. data/sig/raiha/quic.rbs +2 -0
  306. data/sig/raiha/runtime/threaded.rbs +616 -0
  307. data/sig/raiha/runtime.rbs +982 -0
  308. data/sig/raiha/tls/anti_replay.rbs +41 -0
  309. data/sig/raiha/tls/cipher_suite.rbs +41 -0
  310. data/sig/raiha/tls/codec.rbs +66 -0
  311. data/sig/raiha/tls/config.rbs +122 -0
  312. data/sig/raiha/tls/crypto.rbs +86 -0
  313. data/sig/raiha/tls/error.rbs +21 -0
  314. data/sig/raiha/tls/event.rbs +61 -0
  315. data/sig/raiha/tls/extension.rbs +331 -0
  316. data/sig/raiha/tls/handshake.rbs +311 -0
  317. data/sig/raiha/tls/iana.rbs +13 -0
  318. data/sig/raiha/tls/key_exchange.rbs +33 -0
  319. data/sig/raiha/tls/key_schedule.rbs +58 -0
  320. data/sig/raiha/tls/keylog.rbs +20 -0
  321. data/sig/raiha/tls/keyring.rbs +64 -0
  322. data/sig/raiha/tls/machine.rbs +429 -0
  323. data/sig/raiha/tls/masked_inspect.rbs +12 -0
  324. data/sig/raiha/tls/output.rbs +37 -0
  325. data/sig/raiha/tls/profile.rbs +26 -0
  326. data/sig/raiha/tls/record.rbs +79 -0
  327. data/sig/raiha/tls/server_name.rbs +10 -0
  328. data/sig/raiha/tls/session.rbs +48 -0
  329. data/sig/raiha/tls/signature_scheme.rbs +20 -0
  330. data/sig/raiha/tls/tcp.rbs +134 -0
  331. data/sig/raiha/tls/test.rbs +143 -0
  332. data/sig/raiha/tls/ticket.rbs +68 -0
  333. data/sig/raiha/tls/ticket_keys.rbs +67 -0
  334. data/sig/raiha/tls/trace.rbs +99 -0
  335. data/sig/raiha/tls/transcript.rbs +22 -0
  336. data/sig/raiha/tls/verifier.rbs +54 -0
  337. data/sig/raiha/tls.rbs +5 -0
  338. data/sig/raiha.rbs +3 -0
  339. metadata +355 -57
  340. data/.gitignore +0 -10
  341. data/CODE_OF_CONDUCT.md +0 -74
  342. data/Gemfile +0 -4
  343. data/Gemfile.lock +0 -22
  344. data/Rakefile +0 -10
  345. data/bin/console +0 -14
  346. data/bin/setup +0 -8
  347. data/raiha.gemspec +0 -40
@@ -0,0 +1,238 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../config"
4
+ require_relative "../error"
5
+ require_relative "../key_schedule"
6
+ require_relative "../keyring"
7
+ require_relative "../server_name"
8
+ require_relative "acceptor"
9
+ require_relative "client_side"
10
+ require_relative "conductor"
11
+ require_relative "server_side"
12
+
13
+ module Raiha::TLS
14
+ module Handshake
15
+ # A TLS 1.3 handshake without I/O: handshake bytes in, an ordered queue of
16
+ # Outputs (messages, secrets, alerts) and Events out. The transport, TCP
17
+ # records or QUIC CRYPTO frames, carries the bytes and installs the keys.
18
+ #
19
+ # Bytes from the peer never raise: a failure is an Output::Alert and an
20
+ # Event::Failed. Misuse by the caller raises UsageError. An exception from
21
+ # an object the caller gave (the Verifier, the tracer) is not caught: the
22
+ # handshake forgets its secrets, fails and lets it through.
23
+ class Machine
24
+ # Starts a client, which queues its ClientHello at once. `server_name`
25
+ # is a DNS name (A-labels) or an IP address literal; an address is
26
+ # checked against the certificate but not sent as SNI. `extensions` are
27
+ # this connection's own, beside the Config's. `keylog`, anything with
28
+ # `#write(String)`, gets a Keylog line for each traffic secret: never in
29
+ # production (RFC 9850).
30
+ # `session` is one to resume, offered if it still passes the client's
31
+ # checks at `now`, else reported by Event::SessionNotOffered.
32
+ def self.client(config:, keyring:, server_name:, now:, extensions: [], tracer: nil, keylog: nil, session: nil, early_data: false)
33
+ start_client(config:, keyring:, server_name:, now:, extensions:, tracer:, keylog:, substitutions: nil, session:, early_data:)
34
+ end
35
+
36
+ # Starts a server on the ClientHello an Acceptor offered; everything up
37
+ # to the first flight happens here. Without one, the Machine takes the
38
+ # ClientHello through `receive` with an Acceptor of its own, for a
39
+ # server that does not choose its Config by the ClientHello.
40
+ # `resumption_binding` is what the layer above binds the tickets of the
41
+ # connection to, a ticket bound to another failing to open (the QUIC
42
+ # version); `early_data_context` is what a ticket keeps for 0-RTT to be
43
+ # taken on it later only if the same. `anti_replay` records the
44
+ # ClientHellos whose 0-RTT was taken (AntiReplay), and a Config that
45
+ # takes 0-RTT needs one.
46
+ def self.server(config:, keyring:, now:, client_hello: nil, extensions: [], tracer: nil, keylog: nil,
47
+ resumption_binding: "".b, early_data_context: "".b, anti_replay: nil)
48
+ start_server(config:, keyring:, client_hello:, now:, extensions:, tracer:, keylog:, substitutions: nil,
49
+ resumption_binding:, early_data_context:, anti_replay:)
50
+ end
51
+
52
+ # The constructors, with the `substitutions` only Test::RFC8448 passes.
53
+ private_class_method def self.start_client(config:, keyring:, server_name:, now:, extensions:, tracer:, keylog:, substitutions:,
54
+ session: nil, early_data: false)
55
+ raise ArgumentError, "Machine.client takes a client Config" unless config.client?
56
+ check(config, keyring, extensions)
57
+ raise ArgumentError, "session must be a Session" unless session.nil? || session.is_a?(Session)
58
+ raise UsageError, "a Config with resumption: false resumes no session" if session && !config.resumption
59
+ raise ArgumentError, "early_data must be true or false" unless early_data == true || early_data == false
60
+ sni, name = ServerName.for_client(server_name)
61
+ new(config:, keyring:, tracer:, keylog:, substitutions:, now:, client_hello: nil) do |conductor|
62
+ ClientSide.new(conductor, server_name: name, sni:, extensions:, tracer:, session:, early_data:)
63
+ end
64
+ end
65
+
66
+ private_class_method def self.start_server(config:, keyring:, client_hello:, now:, extensions:, tracer:, keylog:, substitutions:,
67
+ resumption_binding: "".b, early_data_context: "".b, anti_replay: nil)
68
+ raise ArgumentError, "Machine.server takes a server Config" unless config.server?
69
+ check(config, keyring, extensions)
70
+ unless resumption_binding.is_a?(String) && early_data_context.is_a?(String)
71
+ raise ArgumentError, "resumption_binding and early_data_context must be Strings"
72
+ end
73
+ # Without a record, 0-RTT the tickets allow could never be taken.
74
+ raise UsageError, "a Config that takes 0-RTT needs anti_replay:" unless anti_replay || config.max_early_data_size.zero?
75
+ new(config:, keyring:, tracer:, keylog:, substitutions:, now:, client_hello: client_hello || Acceptor.new) do |conductor|
76
+ ServerSide.new(conductor, extensions:, tracer:, resumption_binding: resumption_binding.b, early_data_context: early_data_context.b,
77
+ anti_replay:)
78
+ end
79
+ end
80
+
81
+ private_class_method def self.check(config, keyring, extensions)
82
+ raise ArgumentError, "the Keyring was built for another Config" unless keyring.config.equal?(config)
83
+ Config.check_extensions(extensions, config.registry)
84
+ overlap = extensions.map(&:extension_type) & config.extensions.map(&:extension_type)
85
+ raise ArgumentError, "extensions #{overlap.inspect} are set in the Config already" unless overlap.empty?
86
+ end
87
+
88
+ private_class_method :new
89
+
90
+ # A client starts by sending; a server by receiving its ClientHello,
91
+ # given or still to come through its Acceptor.
92
+ def initialize(config:, keyring:, tracer:, keylog:, substitutions:, now:, client_hello:, &side)
93
+ @config = config
94
+ @conductor = Conductor.new(config:, keyring:, tracer:, keylog:, substitutions:, &side)
95
+ @acceptor = nil #: Acceptor?
96
+ case client_hello
97
+ when Accepted then @conductor.receive_message(:initial, client_hello.bytes, now:)
98
+ when Acceptor then @acceptor = client_hello
99
+ else @conductor.start(now:)
100
+ end
101
+ end
102
+
103
+ # The peer's handshake bytes of `epoch`, in order; a message may be
104
+ # split anywhere. Ignored once failed.
105
+ def receive(bytes, epoch:, now:)
106
+ if (acceptor = @acceptor)
107
+ acceptor.receive(bytes, epoch:, now:)
108
+ if (failure = acceptor.alert)
109
+ @acceptor = nil
110
+ @conductor.fail!(failure, acceptor.reason || :unexpected_message)
111
+ elsif (accepted = acceptor.accepted)
112
+ @acceptor = nil
113
+ @conductor.receive_message(:initial, accepted.bytes, now:)
114
+ end
115
+ else
116
+ @conductor.receive(bytes, epoch:, now:)
117
+ end
118
+ nil
119
+ end
120
+
121
+ def each_output(&)
122
+ return enum_for(:each_output) unless block_given?
123
+ @conductor.each_output(&)
124
+ end
125
+
126
+ # Takes every queued output.
127
+ def outputs
128
+ # @type var taken: Array[Output::t]
129
+ taken = []
130
+ @conductor.each_output { |output| taken << output }
131
+ taken
132
+ end
133
+
134
+ def next_event = @conductor.next_event
135
+
136
+ def each_event
137
+ return enum_for(:each_event) unless block_given?
138
+ while (event = next_event)
139
+ yield event
140
+ end
141
+ end
142
+
143
+ # The epoch of the next handshake bytes to pass in.
144
+ def read_epoch = @conductor.read_epoch
145
+
146
+ # Part of a handshake message has come, but not all of it. Over TCP no
147
+ # other kind of record may come in between (RFC 9846 "Record Layer").
148
+ def partial_message? = @acceptor&.partial_message? || @conductor.pending_input?
149
+
150
+ # The epoch to send an alert under now: the one the peer reads.
151
+ def write_epoch = @conductor.write_epoch
152
+
153
+ # Our Finished is sent and the peer's verified.
154
+ def complete? = @conductor.state == :connected
155
+
156
+ # A KeyUpdate of our own over TCP: Message(:application, KeyUpdate)
157
+ # under the old key, then the next Secret(:application, :write). A
158
+ # KeyUpdate the peer asked for goes first. Raises UsageError before the
159
+ # handshake is complete and with Profile::QUIC.
160
+ def request_key_update(request_peer_update: false) = @conductor.request_key_update(request_peer_update:)
161
+
162
+ # The TCP binding calls this right before it sends application data,
163
+ # which is when a KeyUpdate the peer asked for goes out (RFC 9846 "Key
164
+ # and Initialization Vector Update").
165
+ def before_application_data = @conductor.before_application_data
166
+
167
+ # The TCP binding calls this when a record of application data, not
168
+ # empty, arrives: the peer's run of KeyUpdates ends there.
169
+ def application_data_received = @conductor.application_data_received
170
+
171
+ # How many KeyUpdates we have sent.
172
+ def key_update_epoch = @conductor.side.key_updates
173
+
174
+ # The record_size_limit the peer sent (RFC 8449), the most our
175
+ # protected records to it may carry, content type included; nil
176
+ # without one. A client learns it from EncryptedExtensions, a server
177
+ # from the ClientHello.
178
+ def peer_record_size_limit = @conductor.side.peer_record_size_limit
179
+
180
+ # How many bytes of 0-RTT records a server's record layer skips (RFC
181
+ # 9846 "Early Data Indication"): the Config's max_early_data_size, or
182
+ # 16 KiB if more, when the ClientHello offered 0-RTT that was not
183
+ # taken, else nil. Records
184
+ # of application_data while `read_epoch` is :initial (after a
185
+ # HelloRetryRequest), records that do not open under the handshake key
186
+ # while it is :handshake; the record layer counts them and ends the
187
+ # skipping. Nil for a client, and where there is no record layer.
188
+ def skip_early_data_limit = @conductor.side.skip_early_data_limit
189
+
190
+ def failed? = @conductor.failed?
191
+
192
+ def alert = @conductor.alert
193
+
194
+ def cipher_suite = @conductor.side.cipher_suite&.name
195
+
196
+ def group = @conductor.side.group
197
+
198
+ def alpn = @conductor.side.alpn
199
+
200
+ def peer_certificates = @conductor.side.peer_certificates
201
+
202
+ # RFC 9846 "Exporters", for a label declared in the Config's
203
+ # `exporter_labels`. A missing context and an empty one are the same.
204
+ def export_keying_material(label:, length:, context: "".b)
205
+ raise UsageError, "the handshake is not complete" unless complete?
206
+ side = @conductor.side
207
+ inner = side.exporter_values[label] or raise UsageError, "#{label.inspect} is not in the Config's exporter_labels"
208
+ suite = side.cipher_suite or raise UsageError, "the handshake is not complete"
209
+ KeySchedule.export(inner_value: inner, context:, length:, hash: suite.hash_algorithm)
210
+ end
211
+
212
+ # The most handshake data the peer may send in `epoch`, for a QUIC
213
+ # binding to bound its CRYPTO buffer: the sum of the limits on the
214
+ # messages the peer can send there.
215
+ def max_flight_size(epoch)
216
+ h = @config.max_handshake_message_size
217
+ c = @config.max_certificate_message_size
218
+ case [@config.role, epoch]
219
+ in [:client, :initial] then h
220
+ in [:client, :handshake] then 2 * c + 3 * h
221
+ in [:client, :application] then h
222
+ in [:server, :initial] then h
223
+ in [:server, :handshake] then @config.client_auth == :none ? h : c + 2 * h
224
+ else 0
225
+ end
226
+ end
227
+
228
+ # The connection ended outside the handshake: forget every secret and
229
+ # stop, without output or event. Idempotent.
230
+ def discard
231
+ @conductor.discard!
232
+ nil
233
+ end
234
+
235
+ def inspect = "#<#{self.class.name} #{@config.role} #{@conductor.state.inspect}>"
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Handshake
7
+ # What every handshake message value shares: `#encode` writes the 4-byte
8
+ # header (msg_type and a 3-byte length) around `#write_body`.
9
+ module Message
10
+ def encode
11
+ w = Codec::Writer.new
12
+ w.u8(msg_type)
13
+ w.vector(3) { write_body(w) }
14
+ w.to_s
15
+ end
16
+ end
17
+
18
+ # `#extension(type)` for a message that carries an extension block. The
19
+ # block is an Array rather than a Hash so that its order and any
20
+ # duplicate stay representable.
21
+ module ExtensionLookup
22
+ def extension(type) = extensions.find { |ext| ext.extension_type == type }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+ require_relative "../extension"
5
+ require_relative "../masked_inspect"
6
+ require_relative "message"
7
+ require_relative "violation"
8
+
9
+ module Raiha::TLS
10
+ module Handshake
11
+ # `ticket_lifetime` is not bounded here. The 7-day limit in RFC 9846 is a
12
+ # rule for the sender and for how long a client may use the ticket, not a
13
+ # reason to abort on receipt.
14
+ class NewSessionTicket < Data.define(:ticket_lifetime, :ticket_age_add, :ticket_nonce, :ticket, :extensions)
15
+ include Message
16
+ include ExtensionLookup
17
+ include MaskedInspect
18
+
19
+ MSG_TYPE = 4
20
+
21
+ def self.parse_body(body, registry:)
22
+ r = Codec::Reader.new(body)
23
+ ticket_lifetime = r.u32
24
+ ticket_age_add = r.u32
25
+ ticket_nonce = r.opaque(1, max: 0xff)
26
+ ticket = r.opaque(2, min: 1, max: 0xffff)
27
+ extensions = Extension.read_list(r, context: :new_session_ticket, registry:)
28
+ r.finish!
29
+ new(ticket_lifetime:, ticket_age_add:, ticket_nonce:, ticket:, extensions:)
30
+ end
31
+
32
+ def msg_type = MSG_TYPE
33
+
34
+ # Nothing to check without context.
35
+ def validate = nil
36
+
37
+ def write_body(w)
38
+ w.u32(ticket_lifetime)
39
+ w.u32(ticket_age_add)
40
+ w.vector(1, max: 0xff) { w.bytes(ticket_nonce) }
41
+ w.vector(2, min: 1, max: 0xffff) { w.bytes(ticket) }
42
+ Extension.write_list(w, extensions)
43
+ end
44
+
45
+ def masked_members = %i[ticket]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::TLS
4
+ module Handshake
5
+ # Rebuilds handshake messages from the bytes of one epoch, which may split
6
+ # a message anywhere or carry several in one piece. One per epoch.
7
+ #
8
+ # The 4-byte header of each message is checked as soon as it is complete,
9
+ # before its body is buffered: a type outside the set given to `#expect`
10
+ # is `:unexpected`, and a length above the limit for its type is
11
+ # `:overflow`. So an unexpected Certificate never costs 256 KiB, and an
12
+ # unknown type never reaches the parser.
13
+ #
14
+ # The state machine changes what it expects after every message, so only
15
+ # the next header is checked at a time: `#<<` checks it when the bytes
16
+ # arrive, and `#each_message` checks each following one after yielding
17
+ # the message before it. Both return `:ok`, `:overflow` or `:unexpected`.
18
+ # A failure is final; later calls return it again and yield nothing.
19
+ class Reassembler
20
+ def initialize(limits:)
21
+ @limits = limits
22
+ @expected = Set.new #: Set[Integer]
23
+ @buf = String.new(encoding: Encoding::BINARY)
24
+ @header_checked = false
25
+ @status = :ok #: :ok | :overflow | :unexpected
26
+ end
27
+
28
+ # The message types acceptable next, from the state machine's table of
29
+ # expected messages.
30
+ def expect(types)
31
+ @expected = types
32
+ nil
33
+ end
34
+
35
+ def <<(bytes)
36
+ return @status unless @status == :ok
37
+ @buf << bytes
38
+ check_header
39
+ end
40
+
41
+ # Yields `(msg_type, body)` for every complete message, in order.
42
+ def each_message
43
+ loop do
44
+ status = check_header
45
+ return status unless status == :ok
46
+ length = message_length
47
+ break if length.nil? || @buf.bytesize < 4 + length
48
+ msg_type = @buf.getbyte(0) #: Integer
49
+ body = @buf.byteslice(4, length).freeze
50
+ @buf = @buf.byteslice(4 + length, @buf.bytesize) # steep:ignore
51
+ @header_checked = false
52
+ yield msg_type, body # steep:ignore
53
+ end
54
+ :ok
55
+ end
56
+
57
+ # Whether any bytes wait: part of a message, or whole messages not yet
58
+ # taken by `#each_message`. The state machine must not move to the next
59
+ # epoch while this is true.
60
+ def pending? = !@buf.empty?
61
+
62
+ # The type of the next message, once its first byte is here: after an
63
+ # `:unexpected`, the type that was not expected.
64
+ def next_type = @buf.getbyte(0)
65
+
66
+ private def check_header
67
+ return @status unless @status == :ok
68
+ return :ok if @header_checked
69
+ length = message_length
70
+ return :ok if length.nil?
71
+ msg_type = @buf.getbyte(0) #: Integer
72
+ if !@expected.include?(msg_type)
73
+ @status = :unexpected
74
+ elsif length > @limits.call(msg_type)
75
+ @status = :overflow
76
+ else
77
+ @header_checked = true
78
+ :ok
79
+ end
80
+ end
81
+
82
+ private def message_length
83
+ return nil if @buf.bytesize < 4
84
+ (@buf.getbyte(1) << 16) | (@buf.getbyte(2) << 8) | @buf.getbyte(3) # steep:ignore
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,98 @@
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
+ # A ServerHello, or a HelloRetryRequest: RFC 9846 defines the latter as a
11
+ # ServerHello whose random is a fixed value, not as a message type of its
12
+ # own. The two differ only in which extensions they may carry, so parsing
13
+ # picks the extension context from the random.
14
+ class ServerHello < Data.define(
15
+ :legacy_version, :random, :legacy_session_id_echo, :cipher_suite, :legacy_compression_method, :extensions
16
+ )
17
+ include Message
18
+ include ExtensionLookup
19
+
20
+ MSG_TYPE = 2
21
+
22
+ HELLO_RETRY_REQUEST_RANDOM = ["CF21AD74E59A6111BE1D8C021E65B891C2A211167ABB8C5E079E09E2C8A8339C"].pack("H*").freeze
23
+
24
+ def self.parse_body(body, registry:)
25
+ r = Codec::Reader.new(body)
26
+ legacy_version = r.u16
27
+ random = r.bytes(32)
28
+ legacy_session_id_echo = r.opaque(1, max: 32)
29
+ cipher_suite = r.u16
30
+ legacy_compression_method = r.u8
31
+ context = random == HELLO_RETRY_REQUEST_RANDOM ? :hello_retry_request : :server_hello #: Handshake::context
32
+ extensions = Extension.read_list(r, context:, registry:, min: 6)
33
+ r.finish!
34
+ new(legacy_version:, random:, legacy_session_id_echo:, cipher_suite:, legacy_compression_method:, extensions:)
35
+ end
36
+
37
+ def msg_type = MSG_TYPE
38
+
39
+ # The message type cannot tell the two apart, so a client must check
40
+ # this on every ServerHello it receives.
41
+ def hello_retry_request? = random == HELLO_RETRY_REQUEST_RANDOM
42
+
43
+ # The last 8 bytes of a TLS 1.3 server's random when it negotiates TLS 1.2
44
+ # or TLS 1.1 and below.
45
+ DOWNGRADE_TLS12 = "DOWNGRD\x01".b.freeze
46
+ DOWNGRADE_TLS11 = "DOWNGRD\x00".b.freeze
47
+
48
+ # The checks a client makes on a ServerHello or HelloRetryRequest by
49
+ # itself, in order; the first failure is returned. 3 must come before 4:
50
+ # the downgrade sentinel matters only once supported_versions is known
51
+ # to be absent, that is, when the server claims TLS 1.2 or lower.
52
+ #
53
+ # 5 needs no context: this implementation offers only TLS 1.3, so any
54
+ # other selected_version was not offered. 1 is protocol_version only
55
+ # without supported_versions. With it, the version is the extension's
56
+ # alone (RFC 9846 "Supported Versions": clients "MUST ignore the
57
+ # ServerHello.legacy_version value and MUST use only the
58
+ # 'supported_versions' extension to determine the selected version"),
59
+ # and a legacy_version other than 0x0303 is illegal_parameter all the
60
+ # same (RFC 9846 "Server Hello": a client "MUST abort the handshake
61
+ # with an 'illegal_parameter' alert"): it is checked, not taken for the
62
+ # version.
63
+ def validate
64
+ # 2
65
+ unless legacy_compression_method == 0
66
+ return Violation.new(alert: :illegal_parameter, reason: :legacy_compression_method)
67
+ end
68
+ case (versions = extension(Extension::SupportedVersions::TYPE))
69
+ when Extension::SupportedVersions::SH
70
+ # 5
71
+ return Violation.new(alert: :illegal_parameter, reason: :selected_version) unless versions.selected_version == 0x0304
72
+ # 1, of TLS 1.3
73
+ return Violation.new(alert: :illegal_parameter, reason: :legacy_version) unless legacy_version == 0x0303
74
+ else
75
+ # 1
76
+ return Violation.new(alert: :protocol_version, reason: :legacy_version) unless legacy_version == 0x0303
77
+ # 3
78
+ tail = random.byteslice(24, 8)
79
+ if tail == DOWNGRADE_TLS12 || tail == DOWNGRADE_TLS11
80
+ return Violation.new(alert: :illegal_parameter, reason: :downgrade_sentinel)
81
+ end
82
+ # 4
83
+ return Violation.new(alert: :protocol_version, reason: :supported_versions)
84
+ end
85
+ nil
86
+ end
87
+
88
+ def write_body(w)
89
+ w.u16(legacy_version)
90
+ w.bytes(random)
91
+ w.vector(1, max: 32) { w.bytes(legacy_session_id_echo) }
92
+ w.u16(cipher_suite)
93
+ w.u8(legacy_compression_method)
94
+ Extension.write_list(w, extensions, min: 6)
95
+ end
96
+ end
97
+ end
98
+ end