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,310 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raiha::QUIC
4
+ # The keys that protect a connection's packets, one pair for each
5
+ # encryption level (:initial, :zero_rtt, :handshake, :one_rtt): the
6
+ # Initial keys derived from the client's first Destination Connection ID,
7
+ # the others from the secrets TLS gives out, until a level is discarded
8
+ # (RFC 9001 "Packet Protection").
9
+ #
10
+ # The 1-RTT keys change with key updates (RFC 9001 "Key Update"). The
11
+ # read keys of the next key phase are made as soon as those of the
12
+ # current are, so that how long a packet takes to open does not tell
13
+ # whether its Key Phase bit changed; those of the previous phase stay
14
+ # for the packets the network delayed, OLD_KEY_PTOS PTOs from the first
15
+ # packet that opens with the new ones.
16
+ #
17
+ # This end updates the keys as quic-go does, so that the peer's handling
18
+ # of an update is exercised early in a connection: after
19
+ # FIRST_KEY_UPDATE_INTERVAL packets sent or opened with the first keys,
20
+ # and KEY_UPDATE_INTERVAL with each later ones (quic-go's
21
+ # `FirstKeyUpdateInterval` and `KeyUpdateInterval`).
22
+ #
23
+ # Packets that fail authentication are counted across all keys, and past
24
+ # the integrity limit of the AEAD the connection closes; so does it when
25
+ # the 1-RTT keys are about to reach the confidentiality limit and cannot
26
+ # be updated (RFC 9001 "Limits on AEAD Usage").
27
+ #
28
+ # The Initial keys are made for each version a connection may use, as
29
+ # compatible version negotiation can change the version after the first
30
+ # packets (RFC 9368 "Compatible Version Negotiation"). A packet of the
31
+ # Initial level opens with the keys of its header's version. The version
32
+ # in use, which `negotiate` changes, writes the Initial packets and names
33
+ # the labels of the Handshake and 1-RTT keys; 0-RTT stays in the version
34
+ # the connection started in (RFC 9369 "Compatible Negotiation
35
+ # Requirements").
36
+ class PacketProtection
37
+ LEVELS = %i[initial zero_rtt handshake one_rtt].freeze
38
+ OLD_KEY_PTOS = 3
39
+ FIRST_KEY_UPDATE_INTERVAL = 100
40
+ KEY_UPDATE_INTERVAL = 100_000
41
+
42
+ # How many times the 1-RTT keys have changed.
43
+ attr_reader :key_phase
44
+
45
+ # `role` is this end's, :client or :server. The block is called with
46
+ # the key phase each time the 1-RTT keys change.
47
+ def initialize(role:, &updated)
48
+ @role = role
49
+ # The version in use, and the one 0-RTT is in, once the Initial keys
50
+ # are.
51
+ @version = nil #: Version?
52
+ @zero_rtt_version = nil #: Version?
53
+ # The Initial keys of each version, those of the version in use also
54
+ # as the keys of the Initial level.
55
+ @initial = {} #: Hash[Version, Hash[direction, Crypto::Keys]]
56
+ @keys = LEVELS.to_h { |level| [level, {}] } #: Hash[level, Hash[direction, Crypto::Keys]]
57
+ @discarded = [] #: Array[level]
58
+ @updated = updated
59
+ # The cipher suite TLS selected, the Initial keys' until it has.
60
+ @suite = Crypto::Initial::SUITE
61
+ # How many packets have failed authentication.
62
+ @failures = 0
63
+ # The secrets of the next key phase, the read keys of the next and the
64
+ # previous phase, and when the latter go.
65
+ @next_secrets = {} #: Hash[direction, String]
66
+ @next_read = nil #: Crypto::Keys?
67
+ @previous_read = nil #: Crypto::Keys?
68
+ @previous_until = nil #: Float?
69
+ @key_phase = 0
70
+ @confirmed = false
71
+ # The largest 1-RTT packet number the peer has acknowledged, opened,
72
+ # and opened with keys older than the current.
73
+ @largest_acked = nil #: Integer?
74
+ @largest_opened = nil #: Integer?
75
+ @largest_older = nil #: Integer?
76
+ # Of the packets of the current key phase: the first number sent, the
77
+ # lowest opened, how many were sent and opened, and the largest an
78
+ # ACK frame sent with them acknowledged.
79
+ @first_sent = nil #: Integer?
80
+ @lowest_opened = nil #: Integer?
81
+ @sent_count = 0
82
+ @opened_count = 0
83
+ @largest_ack_sent = nil #: Integer?
84
+ end
85
+
86
+ # The Initial keys of each of `versions` from the client's first
87
+ # Destination Connection ID, again after a Retry with the new one. The
88
+ # first time, the first of `versions` is the one in use and the one
89
+ # 0-RTT is in.
90
+ def install_initial(dcid, versions:)
91
+ versions.each do |version|
92
+ initial = Crypto::Initial.derive(dcid:, version:)
93
+ own, peer = @role == :client ? [initial.client, initial.server] : [initial.server, initial.client]
94
+ @initial[version] = { write: own, read: peer }
95
+ end
96
+ version = @version ||= versions.fetch(0)
97
+ @zero_rtt_version ||= version
98
+ @keys[:initial] = @initial.fetch(version)
99
+ end
100
+
101
+ # The version in use is `version` from now on, whose Initial keys are
102
+ # installed: the Initial packets go in it, and the Handshake and 1-RTT
103
+ # keys take its labels.
104
+ def negotiate(version)
105
+ initial = @initial[version] or raise Error::StateError, "no Initial keys of #{format("0x%08x", version.number)}"
106
+ @version = version
107
+ @keys[:initial] = initial unless @discarded.include?(:initial)
108
+ end
109
+
110
+ # The version of the packets of `level`: the one 0-RTT started in for
111
+ # 0-RTT, the one in use for the others.
112
+ def version_of(level)
113
+ version = level == :zero_rtt ? @zero_rtt_version : @version
114
+ version or raise Error::StateError, "no Initial keys yet"
115
+ end
116
+
117
+ # The keys of `level` in `direction` (:read or :write) from a secret of
118
+ # TLS.
119
+ def install(level, direction, secret:, suite:)
120
+ raise ArgumentError, "the Initial keys come from the connection ID" if level == :initial
121
+ raise ArgumentError, "#{level} is discarded" if @discarded.include?(level)
122
+ version = version_of(level)
123
+ keys = @keys.fetch(level)[direction] = Crypto::Keys.derive(suite:, secret:, version:)
124
+ @suite = suite
125
+ return unless level == :one_rtt
126
+ following = @next_secrets[direction] = Crypto::Keys.next_secret(suite:, secret:, version:)
127
+ @next_read = keys.update(suite:, secret: following, version:) if direction == :read
128
+ end
129
+
130
+ # The keys to read packets of `level` with; of the Initial level, those
131
+ # of `version` when given, the version of the packet's header.
132
+ def read_keys(level, version: nil)
133
+ return @keys.fetch(level)[:read] unless level == :initial && version
134
+ @initial[version]&.[](:read)
135
+ end
136
+
137
+ def write_keys(level) = @keys.fetch(level)[:write]
138
+
139
+ def can_read?(level) = @keys.fetch(level).key?(:read)
140
+
141
+ def can_write?(level) = @keys.fetch(level).key?(:write)
142
+
143
+ def discarded?(level) = @discarded.include?(level)
144
+
145
+ # The Key Phase bit of the 1-RTT packets the current keys protect.
146
+ def key_phase_bit = @key_phase & 1
147
+
148
+ # The payload of a packet of `level` with header protection off, or nil
149
+ # when it does not open. A 1-RTT packet opens with the keys its Key
150
+ # Phase bit and packet number tell (RFC 9001 "Receiving with Different
151
+ # Keys"): the current ones when the bit is theirs; else the previous
152
+ # ones below the lowest packet number opened with the current, and the
153
+ # next ones above it. A packet that opens with the next keys is the
154
+ # peer's key update, which this end follows (RFC 9001 "Responding to a
155
+ # Key Update"). `pto` is the PTO period at `now`.
156
+ #
157
+ # A Violation of KEY_UPDATE_ERROR when the peer updates again before
158
+ # this end has acknowledged, with the current keys, a packet of the
159
+ # peer's current keys (MAY), or when a packet opens with keys older than
160
+ # those of a packet with a lower number (MUST, RFC 9001 "Sending with
161
+ # Updated Keys"); of AEAD_LIMIT_REACHED when more packets have failed
162
+ # authentication than the integrity limit.
163
+ def open(level, unmasked, now:, pto:)
164
+ header = unmasked.header
165
+ keys = read_keys(level, version: header.is_a?(Wire::LongHeader) ? header.version : nil) or return nil
166
+ return open_with(keys, unmasked) unless level == :one_rtt
167
+ discard_previous_keys(now)
168
+ number = unmasked.packet_number
169
+ lowest = @lowest_opened
170
+ if unmasked.header.key_phase == key_phase_bit # steep:ignore
171
+ payload = open_with(keys, unmasked) or return nil
172
+ elsif lowest ? number < lowest : @key_phase.positive?
173
+ previous = @previous_read or return nil
174
+ payload = open_with(previous, unmasked) or return nil
175
+ @largest_older = number if @largest_older.nil? || number > @largest_older # steep:ignore
176
+ @largest_opened = number if @largest_opened.nil? || number > @largest_opened # steep:ignore
177
+ return payload
178
+ else
179
+ following = @next_read or return nil
180
+ payload = open_with(following, unmasked) or return nil
181
+ if @key_phase.positive? && !(lowest && (ack = @largest_ack_sent) && ack >= lowest)
182
+ key_update_error("the peer updated its keys again before an ACK of its packets with the current ones")
183
+ end
184
+ update_keys
185
+ lowest = nil
186
+ end
187
+ older = @largest_older
188
+ key_update_error("packet #{number} opened with keys newer than packet #{older}") if older && number < older
189
+ @previous_until = now + OLD_KEY_PTOS * pto if lowest.nil? && @previous_read
190
+ @lowest_opened = number if lowest.nil? || number < lowest
191
+ @largest_opened = number if @largest_opened.nil? || number > @largest_opened # steep:ignore
192
+ @opened_count += 1
193
+ payload
194
+ end
195
+
196
+ # A 1-RTT packet numbered `packet_number` went out with the current
197
+ # keys, with an ACK frame of the packets up to `acknowledging` when not
198
+ # nil.
199
+ #
200
+ # Keys that have protected as many packets as the confidentiality limit
201
+ # are not used again.
202
+ def sent(packet_number, acknowledging: nil)
203
+ @first_sent ||= packet_number
204
+ @sent_count += 1
205
+ if acknowledging && (@largest_ack_sent.nil? || acknowledging > @largest_ack_sent) # steep:ignore
206
+ @largest_ack_sent = acknowledging
207
+ end
208
+ limit = @suite.confidentiality_limit(:quic)
209
+ @keys.fetch(:one_rtt).delete(:write) if limit && @sent_count >= limit
210
+ end
211
+
212
+ # The peer acknowledged the 1-RTT packets up to `largest`. An ACK of a
213
+ # packet sent with the current keys before any packet of the peer opened
214
+ # with them came with older keys: the peer did not follow the update, a
215
+ # Violation of KEY_UPDATE_ERROR (MAY, RFC 9001 "Responding to a Key
216
+ # Update", as quic-go checks it).
217
+ def acknowledged(largest)
218
+ if (first = @first_sent) && largest >= first && @opened_count.zero?
219
+ key_update_error("an ACK of packet #{largest}, sent with the current keys, came with older keys")
220
+ end
221
+ @largest_acked = largest if @largest_acked.nil? || largest > @largest_acked # steep:ignore
222
+ end
223
+
224
+ def handshake_confirmed! = @confirmed = true
225
+
226
+ # Moves to the keys of the next key phase when an update is due: as
227
+ # many packets gone with the current keys as the interval, or all but
228
+ # one the confidentiality limit allows. An update needs the handshake
229
+ # confirmed and a packet sent with the current keys acknowledged (RFC
230
+ # 9001 "Initiating a Key Update"); without them at the limit, a
231
+ # Violation of AEAD_LIMIT_REACHED, whose CONNECTION_CLOSE is the last
232
+ # packet the keys protect.
233
+ def update_keys_if_due
234
+ limit = @suite.confidentiality_limit(:quic)
235
+ exhausted = limit && @sent_count >= limit - 1
236
+ return unless exhausted || [@sent_count, @opened_count].max >= (@key_phase.zero? ? FIRST_KEY_UPDATE_INTERVAL : KEY_UPDATE_INTERVAL)
237
+ return update_keys if update_allowed?
238
+ return unless exhausted
239
+ raise Violation.new("#{@sent_count} packets sent with the 1-RTT keys, which cannot be updated, near the limit of #{limit}",
240
+ code: ErrorCode::AEAD_LIMIT_REACHED)
241
+ end
242
+
243
+ # When the previous 1-RTT read keys go, nil when none are to go.
244
+ def next_timeout = @previous_until
245
+
246
+ def handle_timeout(now:) = discard_previous_keys(now)
247
+
248
+ # Forgets the keys of `level` for good (RFC 9001 "Discarding Unused
249
+ # Keys").
250
+ def discard(level)
251
+ @keys[level] = {}
252
+ @initial.clear if level == :initial
253
+ @discarded << level unless @discarded.include?(level)
254
+ end
255
+
256
+ # The 1-RTT keys of the next key phase in place of the current, which
257
+ # stay to read the previous phase's packets.
258
+ private def update_keys
259
+ suite = @suite
260
+ following = @next_read or raise Error::StateError, "no 1-RTT read keys of the next phase"
261
+ keys = @keys.fetch(:one_rtt)
262
+ @previous_read = keys.fetch(:read)
263
+ @previous_until = nil
264
+ keys[:read] = following
265
+ version = version_of(:one_rtt)
266
+ keys[:write] = keys.fetch(:write).update(suite:, secret: @next_secrets.fetch(:write), version:)
267
+ @next_secrets.transform_values! { |secret| Crypto::Keys.next_secret(suite:, secret:, version:) }
268
+ @next_read = keys.fetch(:read).update(suite:, secret: @next_secrets.fetch(:read), version:)
269
+ @key_phase += 1
270
+ @largest_older = @largest_opened
271
+ @first_sent = nil
272
+ @lowest_opened = nil
273
+ @sent_count = 0
274
+ @opened_count = 0
275
+ @largest_ack_sent = nil
276
+ @updated&.call(@key_phase)
277
+ end
278
+
279
+ private def update_allowed?
280
+ return false unless @confirmed && @next_read
281
+ first = @first_sent or return false
282
+ acked = @largest_acked or return false
283
+ acked >= first
284
+ end
285
+
286
+ private def key_update_error(message) = raise Violation.new(message, code: ErrorCode::KEY_UPDATE_ERROR)
287
+
288
+ private def open_with(keys, unmasked) = unmasked.open(keys.aead) || authentication_failed
289
+
290
+ # RFC 9001 "Limits on AEAD Usage": once more packets have failed
291
+ # authentication, across all keys, than the integrity limit of the AEAD
292
+ # TLS selected, the connection closes with AEAD_LIMIT_REACHED and
293
+ # processes no more.
294
+ private def authentication_failed
295
+ @failures += 1
296
+ limit = @suite.integrity_limit(:quic)
297
+ return nil unless limit && @failures > limit
298
+ raise Violation.new("#{@failures} packets failed authentication, over the limit of #{limit}", code: ErrorCode::AEAD_LIMIT_REACHED)
299
+ end
300
+
301
+ # RFC 9001 "Receiving with Different Keys": an endpoint keeps the old
302
+ # read keys no more than three PTOs after a packet opens with the new.
303
+ private def discard_previous_keys(now)
304
+ deadline = @previous_until or return
305
+ return if now < deadline
306
+ @previous_read = nil
307
+ @previous_until = nil
308
+ end
309
+ end
310
+ end