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,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+ require_relative "../masked_inspect"
5
+
6
+ module Raiha::TLS
7
+ module Extension
8
+ # pre_shared_key.
9
+ module PreSharedKey
10
+ TYPE = 41
11
+
12
+ # PskIdentity: `opaque identity<1..2^16-1>; uint32 obfuscated_ticket_age`.
13
+ class Identity < Data.define(:identity, :obfuscated_ticket_age)
14
+ def self.read(r)
15
+ identity = r.opaque(2, min: 1, max: 0xffff)
16
+ new(identity:, obfuscated_ticket_age: r.u32)
17
+ end
18
+
19
+ def write(w)
20
+ w.vector(2, min: 1, max: 0xffff) { w.bytes(identity) }
21
+ w.u32(obfuscated_ticket_age)
22
+ end
23
+ end
24
+
25
+ # In a ClientHello: `PskIdentity identities<7..2^16-1>` and
26
+ # `PskBinderEntry binders<33..2^16-1>` of `opaque PskBinderEntry<32..255>`.
27
+ class CH < Data.define(:identities, :binders)
28
+ include MaskedInspect
29
+
30
+ def self.parse(data)
31
+ r = Codec::Reader.new(data)
32
+ identities = r.vector(2, min: 7, max: 0xffff) do |v|
33
+ # @type var list: Array[Identity]
34
+ list = []
35
+ list << Identity.read(v) until v.eof?
36
+ list.freeze
37
+ end
38
+ binders = r.vector(2, min: 33, max: 0xffff) do |v|
39
+ # @type var list: Array[String]
40
+ list = []
41
+ list << v.opaque(1, min: 32, max: 255) until v.eof?
42
+ list.freeze
43
+ end
44
+ r.finish!
45
+ new(identities:, binders:)
46
+ end
47
+
48
+ def extension_type = TYPE
49
+
50
+ def encode
51
+ w = Codec::Writer.new
52
+ w.vector(2, min: 7, max: 0xffff) { identities.each { |identity| identity.write(w) } }
53
+ w.vector(2, min: 33, max: 0xffff) do
54
+ binders.each { |binder| w.vector(1, min: 32, max: 255) { w.bytes(binder) } }
55
+ end
56
+ w.to_s
57
+ end
58
+
59
+ def masked_members = %i[binders]
60
+ end
61
+
62
+ # In a ServerHello: `uint16 selected_identity`.
63
+ class SH < Data.define(:selected_identity)
64
+ def self.parse(data)
65
+ r = Codec::Reader.new(data)
66
+ selected = r.u16
67
+ r.finish!
68
+ new(selected_identity: selected)
69
+ end
70
+
71
+ def extension_type = TYPE
72
+
73
+ def encode
74
+ w = Codec::Writer.new
75
+ w.u16(selected_identity)
76
+ w.to_s
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # psk_key_exchange_modes: `PskKeyExchangeMode ke_modes<1..255>`.
8
+ class PskKeyExchangeModes < Data.define(:ke_modes)
9
+ TYPE = 45
10
+
11
+ def self.parse(data)
12
+ r = Codec::Reader.new(data)
13
+ modes = r.vector(1, min: 1, max: 0xff) do |v|
14
+ # @type var list: Array[Integer]
15
+ list = []
16
+ list << v.u8 until v.eof?
17
+ list.freeze
18
+ end
19
+ r.finish!
20
+ new(ke_modes: modes)
21
+ end
22
+
23
+ def extension_type = TYPE
24
+
25
+ def encode
26
+ w = Codec::Writer.new
27
+ w.vector(1, min: 1, max: 0xff) { ke_modes.each { |mode| w.u8(mode) } }
28
+ w.to_s
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # record_size_limit (RFC 8449 §4): `uint16 RecordSizeLimit`. A value
8
+ # under MINIMUM is illegal_parameter.
9
+ class RecordSizeLimit < Data.define(:record_size_limit)
10
+ TYPE = 28
11
+ MINIMUM = 64
12
+
13
+ def self.parse(data)
14
+ r = Codec::Reader.new(data)
15
+ limit = r.u16
16
+ r.finish!
17
+ raise Codec::IllegalValue, "record_size_limit #{limit} is under #{MINIMUM}" if limit < MINIMUM
18
+ new(record_size_limit: limit)
19
+ end
20
+
21
+ def extension_type = TYPE
22
+
23
+ def encode
24
+ w = Codec::Writer.new
25
+ w.u16(record_size_limit)
26
+ w.to_s
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "placement"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # The extensions a connection can parse, by type and context. `Config`
8
+ # holds one; nothing reads a global table. The built-in extensions are
9
+ # registered the same way an outside one is, so `BUILTIN.with` is all it
10
+ # takes to add, say, quic_transport_parameters.
11
+ #
12
+ # A module passed to `#with` defines `TYPE` and `CODECS`, a Hash from
13
+ # context to an object whose `parse(extension_data)` returns a value with
14
+ # `#extension_type` and `#encode`.
15
+ class Registry < Data.define(:codecs)
16
+ CONTEXTS = %i[
17
+ client_hello server_hello hello_retry_request encrypted_extensions certificate
18
+ certificate_request new_session_ticket certificate_entry
19
+ ].freeze
20
+
21
+ def lookup(type, context) = codecs[type]&.[](context)
22
+
23
+ def registered?(type) = codecs.key?(type)
24
+
25
+ # A new Registry with the extensions defined by `modules` added. A type
26
+ # that is already registered raises ArgumentError instead of replacing
27
+ # the existing one.
28
+ def with(*modules)
29
+ # @type var added: Hash[Integer, Hash[Handshake::context, _Parser]]
30
+ added = {}
31
+ modules.each do |mod|
32
+ type = mod.const_get(:TYPE)
33
+ by_context = mod.const_get(:CODECS)
34
+ raise ArgumentError, "#{mod}::TYPE must be an Integer in 0..65535" unless type.is_a?(Integer) && type.between?(0, 0xffff)
35
+ raise ArgumentError, "extension #{type} is already registered" if registered?(type) || added.key?(type)
36
+ raise ArgumentError, "#{mod}::CODECS must be a Hash" unless by_context.is_a?(Hash)
37
+ unknown = by_context.keys - CONTEXTS
38
+ raise ArgumentError, "#{mod}::CODECS has unknown contexts: #{unknown.inspect}" unless unknown.empty?
39
+ # A copy, so that making it shareable does not freeze the caller's Hash.
40
+ added[type] = by_context.dup
41
+ end
42
+ Registry.new(codecs: Ractor.make_shareable(codecs.merge(added)))
43
+ rescue Ractor::Error => e
44
+ raise ArgumentError, "an extension codec cannot be shared between Ractors: #{e.message}"
45
+ end
46
+
47
+ BUILTIN = new(codecs: PLACEMENT)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # server_name (RFC 6066 §3).
8
+ module ServerName
9
+ TYPE = 0
10
+ HOST_NAME = 0
11
+
12
+ # In a ClientHello: a ServerNameList. RFC 6066 allows at most one name
13
+ # of each type and leaves room for types other than host_name, the only
14
+ # one defined. A name of another type is skipped, read like a HostName
15
+ # (opaque<1..2^16-1>, the only layout defined); a second host_name is
16
+ # malformed. `host_name` is nil when the list holds none, which counts
17
+ # as no SNI.
18
+ class CH < Data.define(:host_name)
19
+ def self.parse(data)
20
+ r = Codec::Reader.new(data)
21
+ host_name = r.vector(2, min: 1, max: 0xffff) do |list|
22
+ # @type var found: String?
23
+ found = nil
24
+ until list.eof?
25
+ name_type = list.u8
26
+ name = list.opaque(2, min: 1, max: 0xffff)
27
+ next unless name_type == HOST_NAME
28
+ raise Codec::Malformed, "server_name: more than one host_name" if found
29
+ found = name
30
+ end
31
+ found
32
+ end
33
+ r.finish!
34
+ new(host_name:)
35
+ end
36
+
37
+ def extension_type = TYPE
38
+
39
+ def encode
40
+ name = host_name or raise Codec::EncodeError, "server_name: no host_name to send"
41
+ w = Codec::Writer.new
42
+ w.vector(2, min: 1, max: 0xffff) do
43
+ w.u8(HOST_NAME)
44
+ w.vector(2, min: 1, max: 0xffff) { w.bytes(name) }
45
+ end
46
+ w.to_s
47
+ end
48
+ end
49
+
50
+ # In EncryptedExtensions: empty, acknowledging the client's name.
51
+ class EE < Data.define
52
+ def self.parse(data)
53
+ Codec::Reader.new(data).finish!
54
+ new
55
+ end
56
+
57
+ def extension_type = TYPE
58
+
59
+ def encode = "".b
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # signature_algorithms: `SignatureScheme supported_signature_algorithms<2..2^16-2>`.
8
+ class SignatureAlgorithms < Data.define(:supported_signature_algorithms)
9
+ TYPE = 13
10
+
11
+ def self.parse(data)
12
+ new(supported_signature_algorithms: parse_schemes(data))
13
+ end
14
+
15
+ # The SignatureSchemeList shared with signature_algorithms_cert.
16
+ def self.parse_schemes(data)
17
+ r = Codec::Reader.new(data)
18
+ schemes = r.vector(2, min: 2, max: 0xfffe) do |v|
19
+ # @type var list: Array[Integer]
20
+ list = []
21
+ list << v.u16 until v.eof?
22
+ list.freeze
23
+ end
24
+ r.finish!
25
+ schemes
26
+ end
27
+
28
+ def self.encode_schemes(schemes)
29
+ w = Codec::Writer.new
30
+ w.vector(2, min: 2, max: 0xfffe) { schemes.each { |scheme| w.u16(scheme) } }
31
+ w.to_s
32
+ end
33
+
34
+ def extension_type = TYPE
35
+
36
+ def encode = SignatureAlgorithms.encode_schemes(supported_signature_algorithms)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "signature_algorithms"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # signature_algorithms_cert: the same SignatureSchemeList as
8
+ # signature_algorithms, for signatures in certificates.
9
+ class SignatureAlgorithmsCert < Data.define(:supported_signature_algorithms)
10
+ TYPE = 50
11
+
12
+ def self.parse(data)
13
+ new(supported_signature_algorithms: SignatureAlgorithms.parse_schemes(data))
14
+ end
15
+
16
+ def extension_type = TYPE
17
+
18
+ def encode = SignatureAlgorithms.encode_schemes(supported_signature_algorithms)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # supported_groups: `NamedGroup named_group_list<2..2^16-1>`.
8
+ class SupportedGroups < Data.define(:named_group_list)
9
+ TYPE = 10
10
+
11
+ def self.parse(data)
12
+ r = Codec::Reader.new(data)
13
+ groups = r.vector(2, min: 2, max: 0xffff) do |v|
14
+ # @type var list: Array[Integer]
15
+ list = []
16
+ list << v.u16 until v.eof?
17
+ list.freeze
18
+ end
19
+ r.finish!
20
+ new(named_group_list: groups)
21
+ end
22
+
23
+ def extension_type = TYPE
24
+
25
+ def encode
26
+ w = Codec::Writer.new
27
+ w.vector(2, min: 2, max: 0xffff) { named_group_list.each { |group| w.u16(group) } }
28
+ w.to_s
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../codec"
4
+
5
+ module Raiha::TLS
6
+ module Extension
7
+ # supported_versions.
8
+ module SupportedVersions
9
+ TYPE = 43
10
+
11
+ # In a ClientHello: `ProtocolVersion versions<2..254>`.
12
+ class CH < Data.define(:versions)
13
+ def self.parse(data)
14
+ r = Codec::Reader.new(data)
15
+ versions = r.vector(1, min: 2, max: 254) do |v|
16
+ # @type var list: Array[Integer]
17
+ list = []
18
+ list << v.u16 until v.eof?
19
+ list.freeze
20
+ end
21
+ r.finish!
22
+ new(versions:)
23
+ end
24
+
25
+ def extension_type = TYPE
26
+
27
+ def encode
28
+ w = Codec::Writer.new
29
+ w.vector(1, min: 2, max: 254) { versions.each { |version| w.u16(version) } }
30
+ w.to_s
31
+ end
32
+ end
33
+
34
+ # In a ServerHello or HelloRetryRequest: the one version selected.
35
+ class SH < Data.define(:selected_version)
36
+ def self.parse(data)
37
+ r = Codec::Reader.new(data)
38
+ version = r.u16
39
+ r.finish!
40
+ new(selected_version: version)
41
+ end
42
+
43
+ def extension_type = TYPE
44
+
45
+ def encode
46
+ w = Codec::Writer.new
47
+ w.u16(selected_version)
48
+ w.to_s
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::TLS
4
+ # Extension values. Each class keeps the RFC's field names and follows the
5
+ # contract an `Extension::Registry` relies on: `.parse(String)`,
6
+ # `#extension_type` and `#encode`, where the String is the extension_data.
7
+ #
8
+ # An extension whose structure depends on the message it rides in has one
9
+ # class per message (`KeyShare::CH`, `KeyShare::SH`, ...), so the type says
10
+ # which combination is valid.
11
+ module Extension
12
+ end
13
+ end
14
+
15
+ require_relative "extension/opaque"
16
+ require_relative "extension/server_name"
17
+ require_relative "extension/supported_groups"
18
+ require_relative "extension/signature_algorithms"
19
+ require_relative "extension/signature_algorithms_cert"
20
+ require_relative "extension/alpn"
21
+ require_relative "extension/psk_key_exchange_modes"
22
+ require_relative "extension/cookie"
23
+ require_relative "extension/record_size_limit"
24
+ require_relative "extension/certificate_authorities"
25
+ require_relative "extension/post_handshake_auth"
26
+ require_relative "extension/compress_certificate"
27
+ require_relative "extension/supported_versions"
28
+ require_relative "extension/key_share"
29
+ require_relative "extension/pre_shared_key"
30
+ require_relative "extension/early_data"
31
+ require_relative "extension/registry"
32
+ require_relative "extension/list"
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../error"
4
+ require_relative "../extension"
5
+ require_relative "../output"
6
+ require_relative "../server_name"
7
+ require_relative "client_hello"
8
+ require_relative "reassembler"
9
+
10
+ module Raiha::TLS
11
+ module Handshake
12
+ # A ClientHello and what the application may choose a Config by. All of
13
+ # it is unauthenticated, and with 0-RTT it may be replayed. PSK
14
+ # identities, key shares and binders are left out on purpose.
15
+ #
16
+ # Values from the peer stay Integers. `server_name` is in normal form,
17
+ # or nil when there was none or it was not a usable host name.
18
+ # `after_hello_retry_request` says the ClientHello carries a cookie.
19
+ class ClientHelloInfo < Data.define(
20
+ :server_name, :alpn_protocols, :cipher_suites, :supported_groups,
21
+ :signature_algorithms, :extension_types, :psk_offered, :early_data_offered, :after_hello_retry_request
22
+ )
23
+ end
24
+
25
+ # The ClientHello, as the bytes that came over the wire, with its info.
26
+ # A value, not a token: making two Machines of one is the caller's bug.
27
+ class Accepted < Data.define(:bytes, :info)
28
+ # The extension_data of the extension of `type` in the ClientHello,
29
+ # nil when it has none. A layer above reads one it defines, the QUIC
30
+ # transport parameters say, before it starts a Machine, without
31
+ # parsing the ClientHello itself.
32
+ def extension_data(type)
33
+ body = bytes.byteslice(4..) #: String
34
+ ClientHello.parse_body(body, registry: Extension::Registry::BUILTIN).extension(type)&.encode
35
+ end
36
+ end
37
+
38
+ # The start of a server's handshake, before a Config is chosen: it takes
39
+ # :initial bytes until a ClientHello is whole, checks it on its own
40
+ # (parsing, placement and the checks that need no context) and offers
41
+ # it as an Accepted. The application picks a Config and a Keyring by it,
42
+ # by SNI say, and starts a Machine with it; or turns it down with
43
+ # `reject`. It needs neither Config nor Profile.
44
+ #
45
+ # Bytes from the peer never raise. The only output is an Output::Alert.
46
+ class Acceptor
47
+ MAX_CLIENT_HELLO_SIZE = 16 * 1024
48
+
49
+ # Reasons to turn a ClientHello down before anything in it is
50
+ # authenticated. The alerts that report broken messages are the
51
+ # Acceptor's own, and access_denied needs an authenticated peer.
52
+ REJECT_ALERTS = %i[
53
+ unrecognized_name missing_extension no_application_protocol handshake_failure
54
+ insufficient_security internal_error general_error
55
+ ].freeze
56
+
57
+ EXPECTED = Set[ClientHello::MSG_TYPE].freeze
58
+ NOTHING = Set.new.freeze #: Set[Integer]
59
+
60
+ attr_reader :alert, :reason
61
+
62
+ def initialize
63
+ @reassembler = Reassembler.new(limits: ->(_) { MAX_CLIENT_HELLO_SIZE })
64
+ @reassembler.expect(EXPECTED)
65
+ @accepted = nil #: Accepted?
66
+ @alert = nil #: Symbol?
67
+ @reason = nil #: Symbol?
68
+ # @type var outputs: Array[Output::Alert]
69
+ outputs = []
70
+ @outputs = outputs
71
+ end
72
+
73
+ def failed? = !@alert.nil?
74
+
75
+ # Part of a ClientHello has come, but not all of it.
76
+ def partial_message? = @reassembler.pending?
77
+
78
+ # The ClientHello once whole and valid; nil before, and after a failure.
79
+ def accepted = failed? ? nil : @accepted
80
+
81
+ # A server reads only :initial bytes before it has keys, so any other
82
+ # epoch is the caller's mistake.
83
+ def receive(bytes, epoch:, now:)
84
+ raise UsageError, "an Acceptor takes only :initial bytes, not #{epoch.inspect}" unless epoch == :initial
85
+ return nil if failed?
86
+ return fail!(:unexpected_message, :data_after_client_hello) if @accepted
87
+ status = @reassembler << bytes
88
+ status = @reassembler.each_message { |_, body| break accept(body) } if status == :ok
89
+ case status
90
+ when :overflow then fail!(:decode_error, :message_too_large)
91
+ when :unexpected then fail!(:unexpected_message, :unexpected_message)
92
+ end
93
+ fail!(:unexpected_message, :data_after_client_hello) if @accepted && @reassembler.pending?
94
+ nil
95
+ end
96
+
97
+ def each_output
98
+ while (output = @outputs.shift)
99
+ yield output
100
+ end
101
+ end
102
+
103
+ # Turns the ClientHello down with one of REJECT_ALERTS.
104
+ def reject(description)
105
+ raise ArgumentError, "reject takes one of #{REJECT_ALERTS.inspect}, not #{description.inspect}" unless REJECT_ALERTS.include?(description)
106
+ fail!(description, :rejected)
107
+ end
108
+
109
+ private def accept(body)
110
+ @reassembler.expect(NOTHING)
111
+ raw = [ClientHello::MSG_TYPE, body.bytesize >> 16, body.bytesize >> 8 & 0xff, body.bytesize & 0xff].pack("C4") + body
112
+ begin
113
+ client_hello = ClientHello.parse_body(body, registry: Extension::Registry::BUILTIN)
114
+ rescue Codec::Malformed => e
115
+ return fail!(e.alert, :malformed_message)
116
+ end
117
+ if (violation = client_hello.validate)
118
+ return fail!(violation.alert, violation.reason)
119
+ end
120
+ @accepted = Accepted.new(bytes: raw.freeze, info: info(client_hello))
121
+ :ok
122
+ end
123
+
124
+ private def info(ch)
125
+ name = case (ext = ch.extension(Extension::ServerName::TYPE))
126
+ when Extension::ServerName::CH then ext.host_name&.then { |host_name| ServerName.for_server(host_name).first }
127
+ end
128
+ # @type var alpn: Array[String]
129
+ alpn = case (ext = ch.extension(Extension::Alpn::TYPE))
130
+ when Extension::Alpn then ext.protocol_name_list
131
+ else []
132
+ end
133
+ # @type var groups: Array[Integer]
134
+ groups = case (ext = ch.extension(Extension::SupportedGroups::TYPE))
135
+ when Extension::SupportedGroups then ext.named_group_list
136
+ else []
137
+ end
138
+ # @type var algorithms: Array[Integer]
139
+ algorithms = case (ext = ch.extension(Extension::SignatureAlgorithms::TYPE))
140
+ when Extension::SignatureAlgorithms then ext.supported_signature_algorithms
141
+ else []
142
+ end
143
+ types = ch.extensions.map(&:extension_type)
144
+ ClientHelloInfo.new(
145
+ server_name: name, alpn_protocols: alpn, cipher_suites: ch.cipher_suites, supported_groups: groups,
146
+ signature_algorithms: algorithms, extension_types: types,
147
+ psk_offered: types.include?(Extension::PreSharedKey::TYPE),
148
+ early_data_offered: types.include?(Extension::EarlyData::TYPE),
149
+ after_hello_retry_request: types.include?(Extension::Cookie::TYPE)
150
+ )
151
+ end
152
+
153
+ # The Acceptor's one place to build an alert, before any Machine
154
+ # exists to own it.
155
+ private def fail!(alert, reason)
156
+ return nil if failed?
157
+ @alert = alert
158
+ @reason = reason
159
+ @outputs << Output::Alert.new(level: :fatal, description: alert, epoch: :initial)
160
+ nil
161
+ end
162
+ end
163
+ end
164
+ end