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,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # What makes the connection IDs an Endpoint issues: the first of each
5
+ # connection, a Retry's, and those NEW_CONNECTION_ID frames carry. An
6
+ # Endpoint takes one as `connection_ids:`, and its own draws random
7
+ # bytes; one that puts a worker's ID in each, or a QUIC-LB server ID,
8
+ # takes its place without the Endpoint changing.
9
+ #
10
+ # `generate(length)` gives an ID of `length` bytes, the Endpoint's
11
+ # Config#connection_id_length; the Endpoint draws again while the ID is
12
+ # in use. `worker_of(connection_id)` is the ID of the other worker that
13
+ # issued `connection_id`, nil when it is this one's or no worker's, so
14
+ # that the Endpoint can hand on what came for another worker.
15
+ class RandomConnectionIDs
16
+ def initialize(random)
17
+ @random = random
18
+ end
19
+
20
+ def generate(length) = @random.bytes(length).b
21
+
22
+ def worker_of(_connection_id) = nil
23
+ end
24
+
25
+ # The connection IDs of worker `worker_id` of `worker_count`, each
26
+ # carrying the worker's ID in its first byte. Plain, that byte would let
27
+ # an observer tell connection IDs of one connection from those of
28
+ # another (RFC 9000 "Connection ID" forbids that), so it is the ID XOR
29
+ # the first byte of an HMAC of the rest, under a key that `key`, the
30
+ # Config's stateless_reset_key every worker shares, derives: the first
31
+ # byte looks as random as the rest, and every worker reads it back.
32
+ class WorkerConnectionIDs
33
+ # The shortest ID a worker issues: the worker's byte and 3 random ones.
34
+ MIN_LENGTH = 4
35
+ MAX_WORKERS = 256
36
+ LABEL = "raiha worker connection ID"
37
+
38
+ def initialize(random, worker_id:, worker_count:, key:)
39
+ raise ArgumentError, "worker_count #{worker_count} is not 1 to #{MAX_WORKERS}" unless (1..MAX_WORKERS).cover?(worker_count)
40
+ raise ArgumentError, "worker_id #{worker_id} is not 0 to #{worker_count - 1}" unless (0...worker_count).cover?(worker_id)
41
+ @random = random
42
+ @worker_id = worker_id
43
+ @worker_count = worker_count
44
+ @key = Crypto::Mac.digest(key, LABEL)
45
+ end
46
+
47
+ def generate(length)
48
+ raise ArgumentError, "a worker's connection ID is #{MIN_LENGTH} bytes at least, not #{length}" if length < MIN_LENGTH
49
+ rest = @random.bytes(length - 1).b
50
+ [@worker_id ^ mask(rest)].pack("C") + rest
51
+ end
52
+
53
+ def worker_of(connection_id)
54
+ return nil if connection_id.bytesize < MIN_LENGTH
55
+ rest = connection_id.byteslice(1..) #: String
56
+ worker = connection_id.getbyte(0).to_i ^ mask(rest)
57
+ worker == @worker_id || worker >= @worker_count ? nil : worker
58
+ end
59
+
60
+ private def mask(rest) = Crypto::Mac.digest(@key, rest).getbyte(0).to_i
61
+ end
62
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Crypto
5
+ # The packet protection of one key (RFC 9001 "AEAD Usage"): the nonce is
6
+ # the IV with the packet number XORed into its low bytes, and the header
7
+ # up to the end of the packet number is the associated data. Each
8
+ # direction keeps its own OpenSSL context, keyed once and reused for
9
+ # every packet.
10
+ class AEAD
11
+ CIPHERS = { aes_128_gcm: "aes-128-gcm", aes_256_gcm: "aes-256-gcm", chacha20_poly1305: "chacha20-poly1305" }.freeze #: Hash[Symbol, String]
12
+
13
+ # How many bytes `seal` adds to the plaintext.
14
+ attr_reader :tag_length
15
+
16
+ def initialize(suite:, key:, iv:)
17
+ raise ArgumentError, "#{suite.name} takes a #{suite.key_length}-byte key" unless key.bytesize == suite.key_length
18
+ raise ArgumentError, "#{suite.name} takes a #{suite.iv_length}-byte IV" unless iv.bytesize == suite.iv_length
19
+ @name = CIPHERS.fetch(suite.aead)
20
+ @tag_length = suite.tag_length
21
+ @key = key.b
22
+ # A packet number has 62 bits at most, so it reaches only the low 8
23
+ # of the 12 bytes.
24
+ @iv_high = iv.unpack1("N")
25
+ @iv_low = iv.unpack1("Q>", offset: 4)
26
+ @sealer = nil #: OpenSSL::Cipher?
27
+ @opener = nil #: OpenSSL::Cipher?
28
+ end
29
+
30
+ # The ciphertext followed by the tag.
31
+ def seal(packet_number:, header:, plaintext:)
32
+ cipher = @sealer ||= context(:encrypt)
33
+ cipher.iv = nonce(packet_number)
34
+ cipher.auth_data = header
35
+ sealed = plaintext.empty? ? "".b : cipher.update(plaintext)
36
+ sealed << cipher.final << cipher.auth_tag(@tag_length)
37
+ end
38
+
39
+ # The plaintext, or nil when `ciphertext` (with its tag) does not
40
+ # authenticate. A forged packet, or one under another key, is an
41
+ # everyday event and not an exception; any other failure of OpenSSL is
42
+ # a bug of ours and raises.
43
+ def open(packet_number:, header:, ciphertext:)
44
+ body_length = ciphertext.bytesize - @tag_length
45
+ return nil if body_length.negative?
46
+ cipher = @opener ||= context(:decrypt)
47
+ cipher.iv = nonce(packet_number)
48
+ cipher.auth_tag = ciphertext.byteslice(body_length, @tag_length) # steep:ignore
49
+ cipher.auth_data = header
50
+ opened = body_length.zero? ? "".b : cipher.update(ciphertext.byteslice(0, body_length)) # steep:ignore
51
+ opened << cipher.final
52
+ rescue OpenSSL::Cipher::AuthTagError
53
+ nil
54
+ end
55
+
56
+ private def instance_variables_to_inspect = %i[@name]
57
+
58
+ private def nonce(packet_number) = [@iv_high, @iv_low ^ packet_number].pack("NQ>")
59
+
60
+ private def context(direction)
61
+ cipher = OpenSSL::Cipher.new(@name)
62
+ direction == :encrypt ? cipher.encrypt : cipher.decrypt
63
+ cipher.key = @key
64
+ cipher
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Crypto
5
+ # The header protection of one key (RFC 9001 "Header Protection"): a
6
+ # 5-byte mask from a 16-byte sample of the ciphertext. The AES-GCM suites
7
+ # encrypt the sample with AES in ECB mode; ChaCha20-Poly1305 takes the
8
+ # sample as the block counter and nonce of ChaCha20 and encrypts five
9
+ # zero bytes. One OpenSSL context, keyed once.
10
+ class HeaderProtection
11
+ SAMPLE_LENGTH = 16
12
+ MASK_LENGTH = 5
13
+ CIPHERS = { aes_128_gcm: "aes-128-ecb", aes_256_gcm: "aes-256-ecb", chacha20_poly1305: "chacha20" }.freeze #: Hash[Symbol, String]
14
+ ZEROS = ("\0".b * MASK_LENGTH).freeze
15
+
16
+ def initialize(suite:, key:)
17
+ raise ArgumentError, "#{suite.name} takes a #{suite.key_length}-byte header protection key" unless key.bytesize == suite.key_length
18
+ @name = CIPHERS.fetch(suite.aead)
19
+ @chacha20 = suite.aead == :chacha20_poly1305
20
+ @cipher = OpenSSL::Cipher.new(@name)
21
+ @cipher.encrypt
22
+ @cipher.key = key
23
+ @cipher.padding = 0 unless @chacha20
24
+ end
25
+
26
+ # The mask for `sample`, 5 bytes.
27
+ def mask(sample) # steep:ignore
28
+ raise ArgumentError, "a sample is #{SAMPLE_LENGTH} bytes, not #{sample.bytesize}" unless sample.bytesize == SAMPLE_LENGTH
29
+ if @chacha20
30
+ # OpenSSL's ChaCha20 takes a 16-byte IV, the 4-byte little-endian
31
+ # block counter and then the 12-byte nonce: the sample as it is.
32
+ @cipher.iv = sample
33
+ @cipher.update(ZEROS)
34
+ else
35
+ @cipher.update(sample).byteslice(0, MASK_LENGTH)
36
+ end
37
+ end
38
+
39
+ private def instance_variables_to_inspect = %i[@name]
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Crypto
5
+ # The Initial keys of both directions (RFC 9001 "Initial Secrets"), from
6
+ # the Destination Connection ID of the client's first Initial packet and
7
+ # the salt of the version. Initial packets always use
8
+ # TLS_AES_128_GCM_SHA256. Whoever sees that packet can derive the same
9
+ # keys, so they keep off-path attackers out and hide nothing.
10
+ class Initial < Data.define(:client, :server)
11
+ SUITE = Raiha::TLS::CipherSuite::TLS_AES_128_GCM_SHA256
12
+
13
+ def self.derive(dcid:, version:)
14
+ hash = SUITE.hash_algorithm
15
+ length = SUITE.hash_length
16
+ initial = Raiha::TLS::KeySchedule.extract(salt: version.initial_salt, ikm: dcid, hash:)
17
+ client = Raiha::TLS::KeySchedule.expand_label(secret: initial, label: "client in", length:, hash:)
18
+ server = Raiha::TLS::KeySchedule.expand_label(secret: initial, label: "server in", length:, hash:)
19
+ new(client: Keys.derive(suite: SUITE, secret: client, version:), server: Keys.derive(suite: SUITE, secret: server, version:))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Crypto
5
+ # The packet protection of one direction at one encryption level, from
6
+ # its secret (RFC 9001 "Packet Protection Keys"): the AEAD key and IV
7
+ # and the header protection key, under the labels of `version`.
8
+ class Keys < Data.define(:aead, :header_protection)
9
+ def self.derive(suite:, secret:, version:)
10
+ hp = Raiha::TLS::KeySchedule.expand_label(secret:, label: version.label("hp"), length: suite.key_length, hash: suite.hash_algorithm)
11
+ new(aead: derive_aead(suite:, secret:, version:), header_protection: HeaderProtection.new(suite:, key: hp))
12
+ end
13
+
14
+ def self.derive_aead(suite:, secret:, version:)
15
+ hash = suite.hash_algorithm
16
+ key = Raiha::TLS::KeySchedule.expand_label(secret:, label: version.label("key"), length: suite.key_length, hash:)
17
+ iv = Raiha::TLS::KeySchedule.expand_label(secret:, label: version.label("iv"), length: suite.iv_length, hash:)
18
+ AEAD.new(suite:, key:, iv:)
19
+ end
20
+
21
+ # The 1-RTT secret after `secret` (RFC 9001 "Initiating a Key
22
+ # Update").
23
+ def self.next_secret(suite:, secret:, version:)
24
+ Raiha::TLS::KeySchedule.expand_label(secret:, label: version.label("ku"), length: suite.hash_length, hash: suite.hash_algorithm)
25
+ end
26
+
27
+ # These keys after a key update to `secret`: the AEAD key and IV from
28
+ # it, the header protection key as it was.
29
+ def update(suite:, secret:, version:) = with(aead: Keys.derive_aead(suite:, secret:, version:))
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ module Crypto
5
+ # HMAC-SHA256, for the tokens an Endpoint makes and checks itself, and a
6
+ # comparison of them that takes as long wherever they differ.
7
+ module Mac
8
+ LENGTH = 32
9
+
10
+ def self.digest(key, data) = OpenSSL::HMAC.digest("SHA256", key, data)
11
+
12
+ def self.same?(a, b) = a.bytesize == b.bytesize && OpenSSL.fixed_length_secure_compare(a, b)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Raiha::QUIC
6
+ module Crypto
7
+ # Where random bytes come from when no other source is given: connection
8
+ # IDs, PATH_CHALLENGE data, the padding of a stateless reset. Anything
9
+ # with `#bytes(n)` can take its place, and a seeded ::Random makes the
10
+ # packets reproducible in tests; do not replace it anywhere else.
11
+ Random = SecureRandom
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module Raiha::QUIC
6
+ # Everything in the QUIC layer that calls OpenSSL goes through here. Keys
7
+ # are derived with the TLS layer's KeySchedule, so that both layers share
8
+ # one HKDF-Expand-Label.
9
+ module Crypto
10
+ end
11
+ end
12
+
13
+ require_relative "crypto/aead"
14
+ require_relative "crypto/header_protection"
15
+ require_relative "crypto/keys"
16
+ require_relative "crypto/initial"
17
+ require_relative "crypto/random"
18
+ require_relative "crypto/mac"
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The CRYPTO frames of one encryption level (RFC 9000 "CRYPTO Frames").
5
+ #
6
+ # Sending, the bytes TLS writes stay until acknowledged; frames are cut
7
+ # from what was lost first, then from what has not gone yet, and each
8
+ # range acknowledged or lost comes back as a Recovery::Duty::Crypto.
9
+ #
10
+ # Receiving, frames are put back in order and handed on as far as the
11
+ # bytes run without a gap. What waits, from what has been read up to the
12
+ # end of the furthest frame, must stay within the limit given, or the
13
+ # connection fails with CRYPTO_BUFFER_EXCEEDED (RFC 9000 "Cryptographic
14
+ # Message Buffering"). The bytes wait in one buffer of that span, however
15
+ # the frames overlap, and the ranges received in at most MAX_RANGES
16
+ # pieces, as quic-go's frame sorter keeps to 1000 gaps
17
+ # (`MaxStreamFrameSorterGaps`), so that neither memory nor time grows
18
+ # faster than the bytes.
19
+ class CryptoStream
20
+ FRAME_TYPE = 0x06
21
+ MAX_RANGES = 1000
22
+
23
+ # The number space the level's packets are in.
24
+ attr_reader :space
25
+ # The end of the furthest frame received.
26
+ attr_reader :received_end
27
+ # How far the bytes received have been read.
28
+ attr_reader :read_offset
29
+
30
+ def initialize(space)
31
+ @space = space
32
+ @sent = "".b
33
+ @sent_base = 0
34
+ @next_offset = 0
35
+ @lost = [] #: Array[Range[Integer]]
36
+ @acked = [] #: Array[Range[Integer]]
37
+ # The bytes from `read_offset` to `received_end`, from `@window_start`
38
+ # on in `@window`, and which of them have been received, as ranges in
39
+ # order.
40
+ @window = "".b
41
+ @window_start = 0
42
+ @filled = [] #: Array[Range[Integer]]
43
+ @received_end = 0
44
+ @read_offset = 0
45
+ end
46
+
47
+ # Bytes of TLS to send.
48
+ def write(bytes) = @sent << bytes
49
+
50
+ def pending? = !@lost.empty? || @next_offset < send_end
51
+
52
+ # The next frame, and its duty, in no more than `room` bytes: from what
53
+ # was lost first, then from what has not gone yet. nil when nothing waits
54
+ # or nothing fits.
55
+ def next_frame(room)
56
+ range = @lost.first || (@next_offset...send_end)
57
+ return nil if Ranges.empty?(range)
58
+ offset = range.begin
59
+ length = [range.end - offset, room - 1 - Wire::Varint.size(offset) - Wire::Varint.size(room)].min
60
+ return nil unless length.positive?
61
+ if @lost.empty?
62
+ @next_offset += length
63
+ else
64
+ rest = (offset + length)...range.end
65
+ Ranges.empty?(rest) ? @lost.shift : @lost[0] = rest
66
+ end
67
+ data = @sent.byteslice(offset - @sent_base, length) #: String
68
+ [Wire::Frame::Crypto.new(offset:, data:), Recovery::Duty::Crypto.new(space: @space, offset:, length:)]
69
+ end
70
+
71
+ # A range sent was acknowledged: it is not sent again, and the bytes up
72
+ # to the first gap are let go.
73
+ def acked(offset, length)
74
+ @acked = Ranges.add(@acked, offset...(offset + length))
75
+ @lost = Ranges.subtract(@lost, offset...(offset + length))
76
+ first = @acked.first or return
77
+ return unless first.begin <= @sent_base && first.end > @sent_base
78
+ @sent = @sent.byteslice((first.end - @sent_base)..) #: String
79
+ @sent_base = first.end
80
+ end
81
+
82
+ # A range sent was lost: what of it is not acknowledged goes again.
83
+ def lost(offset, length)
84
+ range = [offset, @sent_base].max...(offset + length)
85
+ return if Ranges.empty?(range)
86
+ @lost = @acked.reduce([range]) { |left, acked| Ranges.subtract(left, acked) }.reduce(@lost) { |lost, part| Ranges.add(lost, part) }
87
+ end
88
+
89
+ # Bytes received at `offset`, kept until read; a Violation of
90
+ # CRYPTO_BUFFER_EXCEEDED when more than `limit` bytes would wait or the
91
+ # bytes received would lie in more than MAX_RANGES pieces. Whether all
92
+ # of them had been read before, as when the peer sends them again; a
93
+ # frame without bytes changes nothing.
94
+ def receive(offset, data, limit:)
95
+ finish = offset + data.bytesize
96
+ return false if data.empty?
97
+ return true if finish <= @read_offset
98
+ exceeded("#{finish - @read_offset} CRYPTO bytes waiting, over #{limit}") if finish - @read_offset > limit
99
+ if offset < @read_offset
100
+ data = data.byteslice((@read_offset - offset)..) #: String
101
+ offset = @read_offset
102
+ end
103
+ wanted = @window_start + finish - @read_offset
104
+ @window << "\0".b * (wanted - @window.bytesize) if wanted > @window.bytesize
105
+ @window.bytesplice(@window_start + offset - @read_offset, data.bytesize, data)
106
+ fill(offset...finish)
107
+ exceeded("CRYPTO bytes received in over #{MAX_RANGES} pieces") if @filled.size > MAX_RANGES
108
+ @received_end = finish if finish > @received_end
109
+ false
110
+ end
111
+
112
+ # The first `length` bytes were received and read elsewhere: frames
113
+ # carrying them again are old.
114
+ def consumed(length)
115
+ raise ArgumentError, "bytes have been received already" unless @received_end.zero?
116
+ @received_end = @read_offset = length
117
+ end
118
+
119
+ # The bytes received from where reading stopped up to the first gap.
120
+ # What is read stays at the front of the window until it is over half
121
+ # of it, so that a frame received after a read does not copy the window
122
+ # it would share with the bytes read.
123
+ def read
124
+ first = @filled.first
125
+ return "".b unless first && first.begin == @read_offset
126
+ length = first.end - @read_offset
127
+ out = @window.byteslice(@window_start, length) #: String
128
+ @window_start += length
129
+ @read_offset = first.end
130
+ @filled.shift
131
+ if @window_start * 2 > @window.bytesize
132
+ @window = @window.byteslice(@window_start..) #: String
133
+ @window_start = 0
134
+ end
135
+ out
136
+ end
137
+
138
+ # How many bytes wait to be read, counted to the end of the furthest
139
+ # frame.
140
+ def buffered = @received_end - @read_offset
141
+
142
+ private def send_end = @sent_base + @sent.bytesize
143
+
144
+ private def exceeded(message) = raise Violation.new(message, code: ErrorCode::CRYPTO_BUFFER_EXCEEDED, frame_type: FRAME_TYPE)
145
+
146
+ # `range` marked received, merged with the ranges it overlaps or
147
+ # touches, found by binary search.
148
+ private def fill(range)
149
+ first = @filled.bsearch_index { |filled| filled.end >= range.begin } || @filled.size
150
+ last = first
151
+ start = range.begin
152
+ finish = range.end
153
+ while (filled = @filled[last]) && filled.begin <= finish
154
+ start = filled.begin if filled.begin < start
155
+ finish = filled.end if filled.end > finish
156
+ last += 1
157
+ end
158
+ @filled[first...last] = [start...finish]
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The DATAGRAM frames of a connection (RFC 9221): those the application
5
+ # gave to send, and those received that it has not taken yet.
6
+ #
7
+ # A DATAGRAM goes only to a peer that took them in its transport
8
+ # parameters, no larger than it said, and is never sent again. Those
9
+ # waiting go in order of urgency, the lowest first, as RFC 9218 counts
10
+ # it, and one past its `expires_at` is dropped unsent (RFC 9221
11
+ # "Congestion Control"); when MAX_SEND_QUEUE wait, the application is
12
+ # told no, and nothing waiting is dropped to make room.
13
+ #
14
+ # One received when this end did not take them, or larger than it said,
15
+ # is PROTOCOL_VIOLATION. Its data is copied out of the packet and comes to
16
+ # the application as Event::DatagramReceived; while MAX_RECEIVED of them,
17
+ # or MAX_RECEIVED_BYTES, have not been taken, those that come are dropped,
18
+ # as a receiver may (RFC 9221 "Flow Control"), since the application not
19
+ # taking them is no fault of the peer's.
20
+ class Datagrams
21
+ MAX_SEND_QUEUE = 32
22
+ MAX_RECEIVED = 128
23
+ MAX_RECEIVED_BYTES = 1024 * 1024
24
+ FRAME_TYPE = Wire::Frame::Datagram::TYPES.fetch(0)
25
+
26
+ # One waiting to go.
27
+ Waiting = Data.define(:data, :expires_at, :urgency)
28
+
29
+ def initialize(config:, events:)
30
+ @max_frame_size = config.max_datagram_frame_size || 0
31
+ @events = events
32
+ @queue = [] #: Array[Waiting]
33
+ @received = 0
34
+ @received_bytes = 0
35
+ end
36
+
37
+ # Queues `data` to send, of `max_size` bytes at most: whether it was.
38
+ def send(data, expires_at:, urgency:, max_size:)
39
+ return false if max_size.nil? || data.bytesize > max_size || @queue.size >= MAX_SEND_QUEUE
40
+ waiting = Waiting.new(data: "".b << data, expires_at:, urgency:)
41
+ index = @queue.index { |queued| queued.urgency > urgency } || @queue.size
42
+ @queue.insert(index, waiting)
43
+ true
44
+ end
45
+
46
+ # The urgency of the DATAGRAM to go next, nil when none waits; those
47
+ # expired at `now` are dropped.
48
+ def urgency(now:)
49
+ drop_expired(now)
50
+ @queue.first&.urgency
51
+ end
52
+
53
+ # The next DATAGRAM frame to send in no more than `room` bytes, nil when
54
+ # none waits or the next does not fit; those expired at `now` are
55
+ # dropped.
56
+ def next_frame(room, now:)
57
+ drop_expired(now)
58
+ head = @queue.first or return nil
59
+ frame = Wire::Frame::Datagram.new(data: head.data)
60
+ return nil if Wire::Frame.encode([frame], ack_delay_exponent: 0).bytesize > room
61
+ @queue.shift
62
+ frame
63
+ end
64
+
65
+ # Takes a DATAGRAM frame from the peer.
66
+ # A client's 0-RTT was rejected: what waits to go was written for it,
67
+ # and goes no more (RFC 9001 "Accepting and Rejecting 0-RTT").
68
+ def drop_queued = @queue.clear
69
+
70
+ def receive(frame)
71
+ size = frame.data.bytesize
72
+ raise Violation.new("a DATAGRAM frame, which this end did not take", frame_type: FRAME_TYPE) if @max_frame_size.zero?
73
+ raise Violation.new("a DATAGRAM frame over #{@max_frame_size} bytes", frame_type: FRAME_TYPE) if 1 + size > @max_frame_size
74
+ return if @received >= MAX_RECEIVED || @received_bytes + size > MAX_RECEIVED_BYTES
75
+ @received += 1
76
+ @received_bytes += size
77
+ @events << Event::DatagramReceived.new(data: "".b << frame.data)
78
+ end
79
+
80
+ # The application took `event` from the connection.
81
+ def taken(event)
82
+ return unless event.is_a?(Event::DatagramReceived)
83
+ @received -= 1
84
+ @received_bytes -= event.data.bytesize
85
+ end
86
+
87
+ private def drop_expired(now)
88
+ @queue.shift while (head = @queue.first) && (expires_at = head.expires_at) && expires_at <= now
89
+ end
90
+ end
91
+ end