grpc 1.37.1 → 1.40.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (738) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +112 -59
  3. data/include/grpc/event_engine/README.md +38 -0
  4. data/include/grpc/event_engine/endpoint_config.h +48 -0
  5. data/include/grpc/event_engine/event_engine.h +330 -0
  6. data/include/grpc/event_engine/port.h +41 -0
  7. data/include/grpc/event_engine/slice_allocator.h +66 -0
  8. data/include/grpc/grpc.h +11 -4
  9. data/include/grpc/grpc_security.h +32 -0
  10. data/include/grpc/grpc_security_constants.h +15 -0
  11. data/include/grpc/impl/codegen/grpc_types.h +44 -19
  12. data/include/grpc/impl/codegen/port_platform.h +46 -0
  13. data/include/grpc/module.modulemap +14 -14
  14. data/src/core/ext/filters/client_channel/backup_poller.cc +3 -3
  15. data/src/core/ext/filters/client_channel/channel_connectivity.cc +177 -202
  16. data/src/core/ext/filters/client_channel/client_channel.cc +975 -3282
  17. data/src/core/ext/filters/client_channel/client_channel.h +513 -55
  18. data/src/core/ext/filters/client_channel/client_channel_channelz.h +1 -1
  19. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +4 -1
  20. data/src/core/ext/filters/client_channel/config_selector.h +20 -7
  21. data/src/core/ext/filters/client_channel/connector.h +1 -1
  22. data/src/core/ext/filters/client_channel/dynamic_filters.cc +9 -10
  23. data/src/core/ext/filters/client_channel/dynamic_filters.h +3 -3
  24. data/src/core/ext/filters/client_channel/health/health_check_client.cc +28 -27
  25. data/src/core/ext/filters/client_channel/health/health_check_client.h +30 -29
  26. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +24 -21
  27. data/src/core/ext/filters/client_channel/http_proxy.cc +16 -1
  28. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +6 -6
  29. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +53 -51
  30. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +1 -1
  31. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +2 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +14 -23
  33. data/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +16 -16
  34. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc +734 -0
  35. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h +10 -0
  36. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +10 -17
  37. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +1 -1
  38. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +17 -20
  39. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +53 -65
  40. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_impl.cc +36 -44
  41. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc +33 -55
  42. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_resolver.cc +151 -163
  43. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -16
  44. data/src/core/ext/filters/client_channel/lb_policy.h +70 -46
  45. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  46. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +4 -4
  47. data/src/core/ext/filters/client_channel/lb_policy_registry.h +1 -1
  48. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +24 -18
  49. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +1 -1
  50. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_event_engine.cc +31 -0
  51. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +3 -3
  52. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +2 -2
  53. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +14 -14
  54. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +33 -24
  55. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +1 -1
  56. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_event_engine.cc +28 -0
  57. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv.cc +1 -1
  58. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +1 -1
  59. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +18 -12
  60. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +20 -28
  61. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +7 -5
  62. data/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc +20 -13
  63. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +1 -1
  64. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +158 -102
  65. data/src/core/ext/filters/client_channel/resolver.h +2 -2
  66. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +32 -239
  67. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +20 -49
  68. data/src/core/ext/filters/client_channel/retry_filter.cc +2598 -0
  69. data/src/core/ext/filters/client_channel/retry_filter.h +30 -0
  70. data/src/core/ext/filters/client_channel/retry_service_config.cc +316 -0
  71. data/src/core/ext/filters/client_channel/retry_service_config.h +96 -0
  72. data/src/core/ext/filters/client_channel/server_address.cc +1 -1
  73. data/src/core/ext/filters/client_channel/service_config.cc +15 -14
  74. data/src/core/ext/filters/client_channel/service_config.h +7 -6
  75. data/src/core/ext/filters/client_channel/service_config_call_data.h +45 -5
  76. data/src/core/ext/filters/client_channel/service_config_channel_arg_filter.cc +5 -4
  77. data/src/core/ext/filters/client_channel/service_config_parser.cc +6 -6
  78. data/src/core/ext/filters/client_channel/service_config_parser.h +7 -4
  79. data/src/core/ext/filters/client_channel/subchannel.cc +17 -16
  80. data/src/core/ext/filters/client_channel/subchannel.h +7 -6
  81. data/src/core/ext/filters/client_idle/client_idle_filter.cc +17 -16
  82. data/src/core/ext/filters/deadline/deadline_filter.cc +10 -10
  83. data/src/core/ext/filters/fault_injection/fault_injection_filter.cc +19 -18
  84. data/src/core/ext/filters/fault_injection/service_config_parser.cc +5 -5
  85. data/src/core/ext/filters/fault_injection/service_config_parser.h +1 -1
  86. data/src/core/ext/filters/http/client/http_client_filter.cc +33 -23
  87. data/src/core/ext/filters/http/client_authority_filter.cc +3 -3
  88. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +23 -22
  89. data/src/core/ext/filters/http/message_compress/message_decompress_filter.cc +21 -21
  90. data/src/core/ext/filters/http/server/http_server_filter.cc +27 -23
  91. data/src/core/ext/filters/max_age/max_age_filter.cc +12 -10
  92. data/src/core/ext/filters/message_size/message_size_filter.cc +14 -11
  93. data/src/core/ext/filters/message_size/message_size_filter.h +1 -1
  94. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.cc +4 -3
  95. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +7 -7
  96. data/src/core/ext/transport/chttp2/client/chttp2_connector.h +7 -7
  97. data/src/core/ext/transport/chttp2/client/insecure/channel_create.cc +2 -2
  98. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +3 -2
  99. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +3 -3
  100. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +49 -46
  101. data/src/core/ext/transport/chttp2/server/chttp2_server.h +2 -2
  102. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.cc +3 -4
  103. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +5 -4
  104. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.cc +3 -4
  105. data/src/core/ext/transport/chttp2/transport/bin_decoder.cc +1 -1
  106. data/src/core/ext/transport/chttp2/transport/chttp2_slice_allocator.cc +66 -0
  107. data/src/core/ext/transport/chttp2/transport/chttp2_slice_allocator.h +74 -0
  108. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +141 -126
  109. data/src/core/ext/transport/chttp2/transport/context_list.cc +4 -5
  110. data/src/core/ext/transport/chttp2/transport/context_list.h +4 -4
  111. data/src/core/ext/transport/chttp2/transport/flow_control.cc +3 -3
  112. data/src/core/ext/transport/chttp2/transport/flow_control.h +9 -9
  113. data/src/core/ext/transport/chttp2/transport/frame_data.cc +12 -12
  114. data/src/core/ext/transport/chttp2/transport/frame_data.h +10 -10
  115. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +15 -16
  116. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +6 -6
  117. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +7 -8
  118. data/src/core/ext/transport/chttp2/transport/frame_ping.h +7 -6
  119. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +7 -7
  120. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +6 -6
  121. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +11 -10
  122. data/src/core/ext/transport/chttp2/transport/frame_settings.h +6 -6
  123. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +4 -6
  124. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +4 -6
  125. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +652 -736
  126. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +195 -74
  127. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +4 -3
  128. data/src/core/ext/transport/chttp2/transport/hpack_table.h +4 -4
  129. data/src/core/ext/transport/chttp2/transport/incoming_metadata.cc +2 -2
  130. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +2 -2
  131. data/src/core/ext/transport/chttp2/transport/internal.h +33 -28
  132. data/src/core/ext/transport/chttp2/transport/parsing.cc +129 -106
  133. data/src/core/ext/transport/chttp2/transport/varint.cc +6 -4
  134. data/src/core/ext/transport/chttp2/transport/writing.cc +7 -3
  135. data/src/core/ext/transport/inproc/inproc_transport.cc +72 -60
  136. data/src/core/ext/upb-generated/envoy/config/bootstrap/v3/bootstrap.upb.c +56 -35
  137. data/src/core/ext/upb-generated/envoy/config/bootstrap/v3/bootstrap.upb.h +180 -76
  138. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.c +35 -27
  139. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.h +97 -48
  140. data/src/core/ext/upb-generated/envoy/config/core/v3/base.upb.c +45 -9
  141. data/src/core/ext/upb-generated/envoy/config/core/v3/base.upb.h +67 -7
  142. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.c +66 -9
  143. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.h +227 -0
  144. data/src/core/ext/upb-generated/envoy/config/core/v3/resolver.upb.c +46 -0
  145. data/src/core/ext/upb-generated/envoy/config/core/v3/resolver.upb.h +121 -0
  146. data/src/core/ext/upb-generated/envoy/config/core/v3/substitution_format_string.upb.c +1 -0
  147. data/src/core/ext/upb-generated/envoy/config/core/v3/udp_socket_config.upb.c +35 -0
  148. data/src/core/ext/upb-generated/envoy/config/core/v3/udp_socket_config.upb.h +90 -0
  149. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.c +32 -24
  150. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.h +120 -73
  151. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.c +4 -2
  152. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.h +15 -0
  153. data/src/core/ext/upb-generated/envoy/config/listener/v3/quic_config.upb.c +48 -0
  154. data/src/core/ext/upb-generated/envoy/config/listener/v3/quic_config.upb.h +171 -0
  155. data/src/core/ext/upb-generated/envoy/config/listener/v3/udp_listener_config.upb.c +8 -6
  156. data/src/core/ext/upb-generated/envoy/config/listener/v3/udp_listener_config.upb.h +27 -19
  157. data/src/core/ext/upb-generated/envoy/config/rbac/v3/rbac.upb.c +1 -0
  158. data/src/core/ext/upb-generated/envoy/config/route/v3/route.upb.c +24 -7
  159. data/src/core/ext/upb-generated/envoy/config/route/v3/route.upb.h +57 -0
  160. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.c +29 -17
  161. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.h +72 -0
  162. data/src/core/ext/upb-generated/envoy/extensions/filters/http/fault/v3/fault.upb.c +3 -2
  163. data/src/core/ext/upb-generated/envoy/extensions/filters/http/fault/v3/fault.upb.h +4 -0
  164. data/src/core/ext/upb-generated/envoy/extensions/filters/http/router/v3/router.upb.c +6 -5
  165. data/src/core/ext/upb-generated/envoy/extensions/filters/http/router/v3/router.upb.h +15 -11
  166. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.c +85 -43
  167. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.h +274 -91
  168. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.c +11 -8
  169. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.h +30 -13
  170. data/src/core/ext/upb-generated/envoy/service/status/v3/csds.upb.c +33 -5
  171. data/src/core/ext/upb-generated/envoy/service/status/v3/csds.upb.h +115 -0
  172. data/src/core/ext/upb-generated/envoy/type/http/v3/path_transformation.upb.c +60 -0
  173. data/src/core/ext/upb-generated/envoy/type/http/v3/path_transformation.upb.h +181 -0
  174. data/src/core/ext/upb-generated/envoy/type/matcher/v3/regex.upb.c +1 -0
  175. data/src/core/ext/upb-generated/validate/validate.upb.c +82 -66
  176. data/src/core/ext/upb-generated/validate/validate.upb.h +220 -124
  177. data/src/core/ext/upbdefs-generated/envoy/annotations/deprecation.upbdefs.c +15 -7
  178. data/src/core/ext/upbdefs-generated/envoy/config/accesslog/v3/accesslog.upbdefs.c +53 -52
  179. data/src/core/ext/upbdefs-generated/envoy/config/bootstrap/v3/bootstrap.upbdefs.c +318 -277
  180. data/src/core/ext/upbdefs-generated/envoy/config/bootstrap/v3/bootstrap.upbdefs.h +5 -0
  181. data/src/core/ext/upbdefs-generated/envoy/config/cluster/v3/cluster.upbdefs.c +437 -410
  182. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/base.upbdefs.c +198 -170
  183. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/base.upbdefs.h +10 -0
  184. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/config_source.upbdefs.c +9 -8
  185. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/protocol.upbdefs.c +219 -163
  186. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/protocol.upbdefs.h +15 -0
  187. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/resolver.upbdefs.c +59 -0
  188. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/resolver.upbdefs.h +40 -0
  189. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/substitution_format_string.upbdefs.c +29 -25
  190. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/udp_socket_config.upbdefs.c +52 -0
  191. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/udp_socket_config.upbdefs.h +35 -0
  192. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/listener.upbdefs.c +135 -125
  193. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/listener.upbdefs.h +5 -0
  194. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/listener_components.upbdefs.c +131 -123
  195. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/quic_config.upbdefs.c +90 -0
  196. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/quic_config.upbdefs.h +35 -0
  197. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/udp_listener_config.upbdefs.c +32 -24
  198. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route.upbdefs.c +69 -55
  199. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route.upbdefs.h +5 -0
  200. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route_components.upbdefs.c +684 -664
  201. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route_components.upbdefs.h +5 -0
  202. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/http/fault/v3/fault.upbdefs.c +13 -10
  203. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/http/router/v3/router.upbdefs.c +13 -10
  204. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upbdefs.c +441 -375
  205. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upbdefs.h +10 -0
  206. data/src/core/ext/upbdefs-generated/envoy/extensions/transport_sockets/tls/v3/common.upbdefs.c +122 -114
  207. data/src/core/ext/upbdefs-generated/envoy/extensions/transport_sockets/tls/v3/tls.upbdefs.c +1 -1
  208. data/src/core/ext/upbdefs-generated/envoy/service/status/v3/csds.upbdefs.c +112 -79
  209. data/src/core/ext/upbdefs-generated/envoy/service/status/v3/csds.upbdefs.h +5 -0
  210. data/src/core/ext/upbdefs-generated/envoy/type/http/v3/path_transformation.upbdefs.c +64 -0
  211. data/src/core/ext/upbdefs-generated/envoy/type/http/v3/path_transformation.upbdefs.h +50 -0
  212. data/src/core/ext/upbdefs-generated/envoy/type/matcher/v3/regex.upbdefs.c +35 -32
  213. data/src/core/ext/upbdefs-generated/google/rpc/status.upbdefs.c +4 -4
  214. data/src/core/ext/upbdefs-generated/validate/validate.upbdefs.c +182 -160
  215. data/src/core/ext/xds/certificate_provider_factory.h +1 -1
  216. data/src/core/ext/xds/certificate_provider_store.h +3 -3
  217. data/src/core/ext/xds/file_watcher_certificate_provider_factory.cc +3 -3
  218. data/src/core/ext/xds/file_watcher_certificate_provider_factory.h +2 -2
  219. data/src/core/ext/xds/xds_api.cc +665 -317
  220. data/src/core/ext/xds/xds_api.h +52 -14
  221. data/src/core/ext/xds/xds_bootstrap.cc +101 -160
  222. data/src/core/ext/xds/xds_bootstrap.h +19 -24
  223. data/src/core/ext/xds/xds_certificate_provider.cc +4 -4
  224. data/src/core/ext/xds/xds_certificate_provider.h +4 -4
  225. data/src/core/ext/xds/xds_channel_args.h +5 -2
  226. data/src/core/ext/xds/xds_client.cc +370 -215
  227. data/src/core/ext/xds/xds_client.h +38 -28
  228. data/src/core/ext/xds/xds_client_stats.h +3 -2
  229. data/src/core/ext/xds/xds_http_filters.cc +3 -2
  230. data/src/core/ext/xds/xds_http_filters.h +3 -0
  231. data/src/core/ext/xds/xds_server_config_fetcher.cc +34 -20
  232. data/src/core/lib/{iomgr → address_utils}/parse_address.cc +17 -17
  233. data/src/core/lib/{iomgr → address_utils}/parse_address.h +7 -7
  234. data/src/core/lib/{iomgr → address_utils}/sockaddr_utils.cc +16 -20
  235. data/src/core/lib/{iomgr → address_utils}/sockaddr_utils.h +16 -11
  236. data/src/core/lib/channel/call_tracer.h +85 -0
  237. data/src/core/lib/channel/channel_stack.cc +10 -9
  238. data/src/core/lib/channel/channel_stack.h +11 -10
  239. data/src/core/lib/channel/channel_stack_builder.cc +2 -2
  240. data/src/core/lib/channel/channel_stack_builder.h +1 -1
  241. data/src/core/lib/channel/channelz.cc +21 -13
  242. data/src/core/lib/channel/channelz.h +3 -0
  243. data/src/core/lib/channel/connected_channel.cc +4 -4
  244. data/src/core/lib/channel/context.h +3 -0
  245. data/src/core/lib/channel/handshaker.cc +7 -6
  246. data/src/core/lib/channel/handshaker.h +5 -5
  247. data/src/core/lib/channel/status_util.h +4 -0
  248. data/src/core/lib/compression/stream_compression.h +1 -1
  249. data/src/core/lib/compression/stream_compression_gzip.h +1 -1
  250. data/src/core/lib/compression/stream_compression_identity.h +1 -1
  251. data/src/core/lib/debug/stats.h +1 -1
  252. data/src/core/lib/event_engine/endpoint_config.cc +46 -0
  253. data/src/core/lib/event_engine/endpoint_config_internal.h +42 -0
  254. data/src/core/lib/event_engine/event_engine.cc +50 -0
  255. data/src/core/lib/event_engine/sockaddr.cc +40 -0
  256. data/src/core/lib/event_engine/sockaddr.h +44 -0
  257. data/src/core/lib/gpr/murmur_hash.cc +4 -2
  258. data/src/core/lib/gpr/wrap_memcpy.cc +2 -1
  259. data/src/core/lib/gprpp/manual_constructor.h +1 -1
  260. data/src/core/lib/gprpp/orphanable.h +3 -3
  261. data/src/core/lib/gprpp/ref_counted.h +28 -14
  262. data/src/core/lib/gprpp/status_helper.cc +407 -0
  263. data/src/core/lib/gprpp/status_helper.h +183 -0
  264. data/src/core/lib/gprpp/sync.h +2 -30
  265. data/src/core/lib/http/httpcli.cc +11 -11
  266. data/src/core/lib/http/httpcli_security_connector.cc +11 -7
  267. data/src/core/lib/http/parser.cc +16 -16
  268. data/src/core/lib/http/parser.h +4 -4
  269. data/src/core/lib/iomgr/buffer_list.cc +8 -10
  270. data/src/core/lib/iomgr/buffer_list.h +4 -5
  271. data/src/core/lib/iomgr/call_combiner.cc +15 -12
  272. data/src/core/lib/iomgr/call_combiner.h +12 -14
  273. data/src/core/lib/iomgr/cfstream_handle.cc +3 -3
  274. data/src/core/lib/iomgr/cfstream_handle.h +1 -1
  275. data/src/core/lib/iomgr/closure.h +7 -6
  276. data/src/core/lib/iomgr/combiner.cc +14 -12
  277. data/src/core/lib/iomgr/combiner.h +2 -2
  278. data/src/core/lib/iomgr/endpoint.cc +1 -1
  279. data/src/core/lib/iomgr/endpoint.h +2 -2
  280. data/src/core/lib/iomgr/endpoint_cfstream.cc +11 -13
  281. data/src/core/lib/iomgr/endpoint_pair_event_engine.cc +33 -0
  282. data/src/core/lib/iomgr/endpoint_pair_windows.cc +1 -1
  283. data/src/core/lib/iomgr/error.cc +168 -61
  284. data/src/core/lib/iomgr/error.h +217 -106
  285. data/src/core/lib/iomgr/error_cfstream.cc +3 -2
  286. data/src/core/lib/iomgr/error_cfstream.h +2 -2
  287. data/src/core/lib/iomgr/error_internal.h +5 -1
  288. data/src/core/lib/iomgr/ev_apple.cc +5 -5
  289. data/src/core/lib/iomgr/ev_apple.h +1 -1
  290. data/src/core/lib/iomgr/ev_epoll1_linux.cc +19 -19
  291. data/src/core/lib/iomgr/ev_epollex_linux.cc +48 -45
  292. data/src/core/lib/iomgr/ev_poll_posix.cc +26 -23
  293. data/src/core/lib/iomgr/ev_posix.cc +9 -8
  294. data/src/core/lib/iomgr/ev_posix.h +9 -9
  295. data/src/core/lib/iomgr/event_engine/closure.cc +54 -0
  296. data/src/core/lib/iomgr/event_engine/closure.h +33 -0
  297. data/src/core/lib/iomgr/event_engine/endpoint.cc +192 -0
  298. data/src/core/lib/iomgr/event_engine/endpoint.h +53 -0
  299. data/src/core/lib/iomgr/event_engine/iomgr.cc +105 -0
  300. data/src/core/lib/iomgr/event_engine/iomgr.h +24 -0
  301. data/src/core/lib/iomgr/event_engine/pollset.cc +87 -0
  302. data/src/core/lib/iomgr/event_engine/pollset.h +25 -0
  303. data/src/core/lib/iomgr/event_engine/promise.h +51 -0
  304. data/src/core/lib/iomgr/event_engine/resolved_address_internal.cc +41 -0
  305. data/src/core/lib/iomgr/event_engine/resolved_address_internal.h +35 -0
  306. data/src/core/lib/iomgr/event_engine/resolver.cc +110 -0
  307. data/src/core/lib/iomgr/event_engine/tcp.cc +263 -0
  308. data/src/core/lib/iomgr/event_engine/timer.cc +57 -0
  309. data/src/core/lib/iomgr/exec_ctx.cc +12 -4
  310. data/src/core/lib/iomgr/exec_ctx.h +4 -5
  311. data/src/core/lib/iomgr/executor/threadpool.cc +2 -3
  312. data/src/core/lib/iomgr/executor/threadpool.h +2 -2
  313. data/src/core/lib/iomgr/executor.cc +8 -8
  314. data/src/core/lib/iomgr/executor.h +2 -2
  315. data/src/core/lib/iomgr/iomgr.cc +2 -2
  316. data/src/core/lib/iomgr/iomgr.h +1 -1
  317. data/src/core/lib/iomgr/iomgr_custom.cc +1 -1
  318. data/src/core/lib/iomgr/iomgr_internal.cc +2 -2
  319. data/src/core/lib/iomgr/iomgr_internal.h +3 -3
  320. data/src/core/lib/iomgr/iomgr_posix.cc +3 -1
  321. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +42 -12
  322. data/src/core/lib/iomgr/iomgr_windows.cc +1 -1
  323. data/src/core/lib/iomgr/load_file.cc +4 -4
  324. data/src/core/lib/iomgr/load_file.h +2 -2
  325. data/src/core/lib/iomgr/lockfree_event.cc +5 -5
  326. data/src/core/lib/iomgr/lockfree_event.h +1 -1
  327. data/src/core/lib/iomgr/pollset.cc +5 -5
  328. data/src/core/lib/iomgr/pollset.h +9 -9
  329. data/src/core/lib/iomgr/pollset_custom.cc +7 -7
  330. data/src/core/lib/iomgr/pollset_custom.h +3 -1
  331. data/src/core/lib/iomgr/pollset_uv.cc +3 -1
  332. data/src/core/lib/iomgr/pollset_uv.h +5 -1
  333. data/src/core/lib/iomgr/pollset_windows.cc +5 -5
  334. data/src/core/lib/iomgr/port.h +7 -5
  335. data/src/core/lib/iomgr/python_util.h +2 -2
  336. data/src/core/lib/iomgr/resolve_address.cc +8 -4
  337. data/src/core/lib/iomgr/resolve_address.h +12 -6
  338. data/src/core/lib/iomgr/resolve_address_custom.cc +10 -9
  339. data/src/core/lib/iomgr/resolve_address_custom.h +3 -3
  340. data/src/core/lib/iomgr/resolve_address_posix.cc +3 -3
  341. data/src/core/lib/iomgr/resolve_address_windows.cc +4 -4
  342. data/src/core/lib/iomgr/resource_quota.cc +13 -10
  343. data/src/core/lib/iomgr/sockaddr.h +1 -0
  344. data/src/core/lib/iomgr/socket_mutator.cc +15 -2
  345. data/src/core/lib/iomgr/socket_mutator.h +26 -2
  346. data/src/core/lib/iomgr/socket_utils_common_posix.cc +24 -22
  347. data/src/core/lib/iomgr/socket_utils_posix.h +20 -20
  348. data/src/core/lib/iomgr/tcp_client_cfstream.cc +4 -4
  349. data/src/core/lib/iomgr/tcp_client_custom.cc +5 -6
  350. data/src/core/lib/iomgr/tcp_client_posix.cc +22 -19
  351. data/src/core/lib/iomgr/tcp_client_posix.h +3 -4
  352. data/src/core/lib/iomgr/tcp_client_windows.cc +7 -5
  353. data/src/core/lib/iomgr/tcp_custom.cc +14 -16
  354. data/src/core/lib/iomgr/tcp_custom.h +13 -12
  355. data/src/core/lib/iomgr/tcp_posix.cc +78 -73
  356. data/src/core/lib/iomgr/tcp_posix.h +8 -0
  357. data/src/core/lib/iomgr/tcp_server.cc +6 -6
  358. data/src/core/lib/iomgr/tcp_server.h +12 -11
  359. data/src/core/lib/iomgr/tcp_server_custom.cc +26 -25
  360. data/src/core/lib/iomgr/tcp_server_posix.cc +29 -21
  361. data/src/core/lib/iomgr/tcp_server_utils_posix.h +13 -12
  362. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +21 -18
  363. data/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc +9 -9
  364. data/src/core/lib/iomgr/tcp_server_utils_posix_noifaddrs.cc +4 -4
  365. data/src/core/lib/iomgr/tcp_server_windows.cc +26 -25
  366. data/src/core/lib/iomgr/tcp_uv.cc +25 -23
  367. data/src/core/lib/iomgr/tcp_windows.cc +13 -13
  368. data/src/core/lib/iomgr/tcp_windows.h +2 -2
  369. data/src/core/lib/iomgr/timer.h +6 -1
  370. data/src/core/lib/iomgr/timer_custom.cc +2 -1
  371. data/src/core/lib/iomgr/timer_custom.h +1 -1
  372. data/src/core/lib/iomgr/timer_generic.cc +6 -6
  373. data/src/core/lib/iomgr/timer_manager.cc +1 -1
  374. data/src/core/lib/iomgr/udp_server.cc +21 -20
  375. data/src/core/lib/iomgr/unix_sockets_posix.cc +3 -3
  376. data/src/core/lib/iomgr/unix_sockets_posix.h +2 -2
  377. data/src/core/lib/iomgr/unix_sockets_posix_noop.cc +10 -7
  378. data/src/core/lib/iomgr/wakeup_fd_eventfd.cc +3 -3
  379. data/src/core/lib/iomgr/wakeup_fd_pipe.cc +4 -4
  380. data/src/core/lib/iomgr/wakeup_fd_posix.cc +3 -3
  381. data/src/core/lib/iomgr/wakeup_fd_posix.h +8 -6
  382. data/src/core/lib/iomgr/work_serializer.h +17 -1
  383. data/src/core/lib/json/json.h +1 -1
  384. data/src/core/lib/json/json_reader.cc +5 -6
  385. data/src/core/lib/matchers/matchers.cc +46 -58
  386. data/src/core/lib/matchers/matchers.h +30 -29
  387. data/src/core/lib/security/authorization/authorization_engine.h +44 -0
  388. data/src/core/lib/security/authorization/authorization_policy_provider.h +32 -0
  389. data/src/core/lib/security/authorization/authorization_policy_provider_vtable.cc +46 -0
  390. data/src/core/lib/security/authorization/evaluate_args.cc +209 -0
  391. data/src/core/lib/security/authorization/evaluate_args.h +91 -0
  392. data/src/core/lib/security/credentials/composite/composite_credentials.cc +4 -4
  393. data/src/core/lib/security/credentials/composite/composite_credentials.h +2 -2
  394. data/src/core/lib/security/credentials/credentials.h +2 -2
  395. data/src/core/lib/security/credentials/external/aws_external_account_credentials.cc +17 -13
  396. data/src/core/lib/security/credentials/external/aws_external_account_credentials.h +13 -11
  397. data/src/core/lib/security/credentials/external/aws_request_signer.cc +2 -1
  398. data/src/core/lib/security/credentials/external/aws_request_signer.h +1 -1
  399. data/src/core/lib/security/credentials/external/external_account_credentials.cc +15 -12
  400. data/src/core/lib/security/credentials/external/external_account_credentials.h +9 -8
  401. data/src/core/lib/security/credentials/external/file_external_account_credentials.cc +5 -4
  402. data/src/core/lib/security/credentials/external/file_external_account_credentials.h +4 -3
  403. data/src/core/lib/security/credentials/external/url_external_account_credentials.cc +8 -8
  404. data/src/core/lib/security/credentials/external/url_external_account_credentials.h +9 -7
  405. data/src/core/lib/security/credentials/fake/fake_credentials.cc +2 -2
  406. data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -2
  407. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +12 -10
  408. data/src/core/lib/security/credentials/iam/iam_credentials.cc +2 -2
  409. data/src/core/lib/security/credentials/iam/iam_credentials.h +2 -2
  410. data/src/core/lib/security/credentials/jwt/json_token.cc +2 -2
  411. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +3 -3
  412. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +2 -2
  413. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +7 -5
  414. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +21 -19
  415. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +5 -5
  416. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +5 -5
  417. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -2
  418. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.cc +8 -7
  419. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.h +9 -9
  420. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc +68 -13
  421. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h +7 -0
  422. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +4 -0
  423. data/src/core/lib/security/credentials/tls/tls_utils.cc +32 -0
  424. data/src/core/lib/security/credentials/tls/tls_utils.h +13 -0
  425. data/src/core/lib/security/credentials/xds/xds_credentials.cc +3 -3
  426. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +13 -3
  427. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +13 -3
  428. data/src/core/lib/security/security_connector/insecure/insecure_security_connector.cc +2 -2
  429. data/src/core/lib/security/security_connector/insecure/insecure_security_connector.h +12 -2
  430. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +1 -1
  431. data/src/core/lib/security/security_connector/local/local_security_connector.cc +22 -9
  432. data/src/core/lib/security/security_connector/security_connector.h +9 -4
  433. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +16 -6
  434. data/src/core/lib/security/security_connector/ssl_utils.cc +27 -4
  435. data/src/core/lib/security/security_connector/ssl_utils.h +4 -4
  436. data/src/core/lib/security/security_connector/tls/tls_security_connector.cc +60 -76
  437. data/src/core/lib/security/security_connector/tls/tls_security_connector.h +66 -48
  438. data/src/core/lib/security/transport/client_auth_filter.cc +18 -10
  439. data/src/core/lib/security/transport/secure_endpoint.cc +4 -4
  440. data/src/core/lib/security/transport/security_handshaker.cc +45 -36
  441. data/src/core/lib/security/transport/server_auth_filter.cc +17 -18
  442. data/src/core/lib/security/transport/tsi_error.cc +2 -1
  443. data/src/core/lib/security/transport/tsi_error.h +2 -1
  444. data/src/core/lib/security/util/json_util.cc +2 -2
  445. data/src/core/lib/security/util/json_util.h +1 -1
  446. data/src/core/lib/slice/slice_internal.h +1 -0
  447. data/src/core/lib/surface/call.cc +72 -52
  448. data/src/core/lib/surface/call.h +13 -2
  449. data/src/core/lib/surface/channel.cc +6 -6
  450. data/src/core/lib/surface/channel.h +3 -2
  451. data/src/core/lib/surface/channel_ping.cc +1 -1
  452. data/src/core/lib/surface/completion_queue.cc +68 -69
  453. data/src/core/lib/surface/completion_queue.h +3 -2
  454. data/src/core/lib/surface/completion_queue_factory.cc +1 -2
  455. data/src/core/lib/surface/init.cc +1 -3
  456. data/src/core/lib/surface/init.h +10 -1
  457. data/src/core/lib/surface/lame_client.cc +11 -11
  458. data/src/core/lib/surface/lame_client.h +1 -1
  459. data/src/core/lib/surface/server.cc +31 -23
  460. data/src/core/lib/surface/server.h +19 -18
  461. data/src/core/lib/surface/validate_metadata.cc +7 -7
  462. data/src/core/lib/surface/validate_metadata.h +3 -2
  463. data/src/core/lib/surface/version.cc +2 -2
  464. data/src/core/lib/transport/byte_stream.cc +5 -5
  465. data/src/core/lib/transport/byte_stream.h +8 -8
  466. data/src/core/lib/transport/connectivity_state.cc +1 -1
  467. data/src/core/lib/transport/error_utils.cc +21 -10
  468. data/src/core/lib/transport/error_utils.h +11 -5
  469. data/src/core/lib/transport/metadata_batch.cc +37 -37
  470. data/src/core/lib/transport/metadata_batch.h +19 -18
  471. data/src/core/lib/transport/transport.cc +4 -3
  472. data/src/core/lib/transport/transport.h +6 -4
  473. data/src/core/lib/transport/transport_op_string.cc +6 -6
  474. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  475. data/src/core/tsi/alts/crypt/gsec.h +6 -0
  476. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +5 -4
  477. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +7 -6
  478. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h +2 -1
  479. data/src/core/tsi/ssl_transport_security.cc +32 -14
  480. data/src/core/tsi/ssl_transport_security.h +3 -4
  481. data/src/ruby/bin/math_services_pb.rb +1 -1
  482. data/src/ruby/ext/grpc/extconf.rb +2 -0
  483. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +6 -0
  484. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +11 -2
  485. data/src/ruby/lib/grpc/version.rb +1 -1
  486. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +1 -1
  487. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +6 -6
  488. data/third_party/abseil-cpp/absl/algorithm/container.h +3 -3
  489. data/third_party/abseil-cpp/absl/base/attributes.h +24 -4
  490. data/third_party/abseil-cpp/absl/base/call_once.h +2 -9
  491. data/third_party/abseil-cpp/absl/base/config.h +37 -9
  492. data/third_party/abseil-cpp/absl/base/dynamic_annotations.h +24 -10
  493. data/third_party/abseil-cpp/absl/base/internal/direct_mmap.h +4 -1
  494. data/third_party/abseil-cpp/absl/base/internal/endian.h +61 -0
  495. data/third_party/abseil-cpp/absl/base/internal/low_level_scheduling.h +2 -3
  496. data/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +34 -32
  497. data/third_party/abseil-cpp/absl/base/internal/raw_logging.h +16 -6
  498. data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +11 -2
  499. data/third_party/abseil-cpp/absl/base/internal/spinlock.h +14 -5
  500. data/third_party/abseil-cpp/absl/base/internal/spinlock_akaros.inc +2 -2
  501. data/third_party/abseil-cpp/absl/base/internal/spinlock_linux.inc +3 -3
  502. data/third_party/abseil-cpp/absl/base/internal/spinlock_posix.inc +2 -2
  503. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.h +11 -11
  504. data/third_party/abseil-cpp/absl/base/internal/spinlock_win32.inc +5 -5
  505. data/third_party/abseil-cpp/absl/base/internal/sysinfo.cc +1 -1
  506. data/third_party/abseil-cpp/absl/base/internal/thread_identity.cc +5 -2
  507. data/third_party/abseil-cpp/absl/base/internal/thread_identity.h +43 -42
  508. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.cc +111 -7
  509. data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +0 -76
  510. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc +1 -3
  511. data/third_party/abseil-cpp/absl/base/log_severity.h +4 -4
  512. data/third_party/abseil-cpp/absl/base/macros.h +11 -0
  513. data/third_party/abseil-cpp/absl/base/optimization.h +10 -7
  514. data/third_party/abseil-cpp/absl/base/options.h +1 -1
  515. data/third_party/abseil-cpp/absl/base/port.h +0 -1
  516. data/third_party/abseil-cpp/absl/base/thread_annotations.h +1 -1
  517. data/third_party/abseil-cpp/absl/container/fixed_array.h +2 -2
  518. data/third_party/abseil-cpp/absl/container/inlined_vector.h +5 -3
  519. data/third_party/abseil-cpp/absl/container/internal/compressed_tuple.h +1 -1
  520. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler.cc +5 -1
  521. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler.h +2 -1
  522. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler_force_weak_definition.cc +2 -1
  523. data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +141 -66
  524. data/third_party/abseil-cpp/absl/container/internal/layout.h +4 -4
  525. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set.cc +14 -1
  526. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set.h +136 -136
  527. data/third_party/abseil-cpp/absl/debugging/internal/demangle.cc +16 -12
  528. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_aarch64-inl.inc +5 -2
  529. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_config.h +3 -12
  530. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_powerpc-inl.inc +6 -1
  531. data/third_party/abseil-cpp/absl/debugging/internal/symbolize.h +3 -5
  532. data/third_party/abseil-cpp/absl/debugging/symbolize_darwin.inc +2 -2
  533. data/third_party/abseil-cpp/absl/debugging/symbolize_elf.inc +2 -2
  534. data/third_party/abseil-cpp/absl/hash/internal/city.cc +15 -12
  535. data/third_party/abseil-cpp/absl/hash/internal/city.h +1 -19
  536. data/third_party/abseil-cpp/absl/hash/internal/hash.cc +25 -10
  537. data/third_party/abseil-cpp/absl/hash/internal/hash.h +86 -37
  538. data/third_party/abseil-cpp/absl/hash/internal/wyhash.cc +111 -0
  539. data/third_party/abseil-cpp/absl/hash/internal/wyhash.h +48 -0
  540. data/third_party/abseil-cpp/absl/meta/type_traits.h +16 -2
  541. data/third_party/abseil-cpp/absl/numeric/bits.h +177 -0
  542. data/third_party/abseil-cpp/absl/numeric/int128.cc +3 -3
  543. data/third_party/abseil-cpp/absl/numeric/internal/bits.h +358 -0
  544. data/third_party/abseil-cpp/absl/numeric/internal/representation.h +55 -0
  545. data/third_party/abseil-cpp/absl/status/internal/status_internal.h +18 -0
  546. data/third_party/abseil-cpp/absl/status/internal/statusor_internal.h +4 -7
  547. data/third_party/abseil-cpp/absl/status/status.cc +29 -22
  548. data/third_party/abseil-cpp/absl/status/status.h +81 -20
  549. data/third_party/abseil-cpp/absl/status/statusor.h +3 -3
  550. data/third_party/abseil-cpp/absl/strings/charconv.cc +5 -5
  551. data/third_party/abseil-cpp/absl/strings/cord.cc +326 -371
  552. data/third_party/abseil-cpp/absl/strings/cord.h +182 -64
  553. data/third_party/abseil-cpp/absl/strings/escaping.cc +4 -4
  554. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.cc +6 -6
  555. data/third_party/abseil-cpp/absl/strings/internal/cord_internal.cc +83 -0
  556. data/third_party/abseil-cpp/absl/strings/internal/cord_internal.h +387 -17
  557. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_flat.h +146 -0
  558. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_ring.cc +897 -0
  559. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_ring.h +589 -0
  560. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_ring_reader.h +114 -0
  561. data/third_party/abseil-cpp/absl/strings/internal/str_format/arg.cc +14 -0
  562. data/third_party/abseil-cpp/absl/strings/internal/str_format/arg.h +14 -0
  563. data/third_party/abseil-cpp/absl/strings/internal/str_format/bind.cc +15 -1
  564. data/third_party/abseil-cpp/absl/strings/internal/str_format/bind.h +19 -4
  565. data/third_party/abseil-cpp/absl/strings/internal/str_format/checker.h +14 -0
  566. data/third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.cc +36 -18
  567. data/third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.h +14 -0
  568. data/third_party/abseil-cpp/absl/strings/internal/str_format/parser.cc +14 -0
  569. data/third_party/abseil-cpp/absl/strings/internal/str_format/parser.h +14 -0
  570. data/third_party/abseil-cpp/absl/strings/internal/str_split_internal.h +15 -40
  571. data/third_party/abseil-cpp/absl/strings/internal/string_constant.h +64 -0
  572. data/third_party/abseil-cpp/absl/strings/match.cc +6 -3
  573. data/third_party/abseil-cpp/absl/strings/match.h +16 -6
  574. data/third_party/abseil-cpp/absl/strings/numbers.cc +132 -4
  575. data/third_party/abseil-cpp/absl/strings/numbers.h +10 -10
  576. data/third_party/abseil-cpp/absl/strings/str_join.h +1 -1
  577. data/third_party/abseil-cpp/absl/strings/str_split.h +38 -4
  578. data/third_party/abseil-cpp/absl/synchronization/internal/futex.h +154 -0
  579. data/third_party/abseil-cpp/absl/synchronization/internal/kernel_timeout.h +2 -1
  580. data/third_party/abseil-cpp/absl/synchronization/internal/per_thread_sem.cc +2 -2
  581. data/third_party/abseil-cpp/absl/synchronization/internal/per_thread_sem.h +4 -4
  582. data/third_party/abseil-cpp/absl/synchronization/internal/waiter.cc +1 -65
  583. data/third_party/abseil-cpp/absl/synchronization/internal/waiter.h +2 -6
  584. data/third_party/abseil-cpp/absl/synchronization/mutex.cc +71 -59
  585. data/third_party/abseil-cpp/absl/synchronization/mutex.h +79 -62
  586. data/third_party/abseil-cpp/absl/time/clock.cc +146 -130
  587. data/third_party/abseil-cpp/absl/time/clock.h +2 -2
  588. data/third_party/abseil-cpp/absl/time/duration.cc +3 -2
  589. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time_detail.h +7 -11
  590. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_libc.cc +7 -1
  591. data/third_party/abseil-cpp/absl/time/internal/cctz/src/tzfile.h +4 -4
  592. data/third_party/abseil-cpp/absl/time/time.cc +4 -3
  593. data/third_party/abseil-cpp/absl/time/time.h +26 -24
  594. data/third_party/abseil-cpp/absl/types/internal/variant.h +1 -1
  595. data/third_party/abseil-cpp/absl/types/variant.h +9 -4
  596. data/third_party/boringssl-with-bazel/err_data.c +483 -461
  597. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_bool.c +1 -1
  598. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_object.c +9 -7
  599. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_type.c +18 -8
  600. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_lib.c +1 -2
  601. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_locl.h +5 -0
  602. data/third_party/boringssl-with-bazel/src/crypto/asn1/tasn_enc.c +1 -1
  603. data/third_party/boringssl-with-bazel/src/crypto/asn1/tasn_fre.c +1 -1
  604. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/cipher_extra.c +4 -0
  605. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/e_tls.c +1 -88
  606. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/internal.h +14 -3
  607. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/tls_cbc.c +119 -273
  608. data/third_party/boringssl-with-bazel/src/crypto/curve25519/curve25519.c +1 -1
  609. data/third_party/boringssl-with-bazel/src/crypto/curve25519/internal.h +1 -1
  610. data/third_party/boringssl-with-bazel/src/crypto/err/err.c +87 -80
  611. data/third_party/boringssl-with-bazel/src/crypto/evp/evp.c +9 -0
  612. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bcm.c +1 -0
  613. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/internal.h +1 -1
  614. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/prime.c +0 -4
  615. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/cipher.c +11 -3
  616. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/e_aes.c +25 -2
  617. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digest.c +7 -0
  618. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digests.c +10 -2
  619. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/md32_common.h +87 -160
  620. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec.c +4 -0
  621. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec_key.c +0 -1
  622. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h +0 -4
  623. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/ecdsa.c +104 -93
  624. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/internal.h +39 -0
  625. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md4/md4.c +52 -65
  626. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md5/md5.c +52 -66
  627. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/cbc.c +33 -22
  628. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/cfb.c +9 -8
  629. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/ctr.c +9 -8
  630. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/gcm.c +17 -13
  631. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/internal.h +1 -22
  632. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/ofb.c +2 -1
  633. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h +1 -4
  634. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/rand.c +0 -13
  635. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/urandom.c +26 -7
  636. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +26 -24
  637. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +10 -7
  638. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/fips.c +79 -0
  639. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/self_check.c +14 -9
  640. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha1.c +61 -75
  641. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha256.c +80 -103
  642. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha512.c +40 -49
  643. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +367 -315
  644. data/third_party/boringssl-with-bazel/src/crypto/internal.h +65 -0
  645. data/third_party/boringssl-with-bazel/src/crypto/mem.c +14 -0
  646. data/third_party/boringssl-with-bazel/src/crypto/obj/obj.c +3 -3
  647. data/third_party/boringssl-with-bazel/src/crypto/pkcs7/pkcs7_x509.c +5 -3
  648. data/third_party/boringssl-with-bazel/src/crypto/pkcs8/pkcs8_x509.c +95 -48
  649. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/passive.c +2 -2
  650. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/rand_extra.c +1 -1
  651. data/third_party/boringssl-with-bazel/src/crypto/rsa_extra/rsa_asn1.c +1 -2
  652. data/third_party/boringssl-with-bazel/src/crypto/thread_pthread.c +0 -28
  653. data/third_party/boringssl-with-bazel/src/crypto/x509/internal.h +120 -11
  654. data/third_party/boringssl-with-bazel/src/crypto/x509/t_req.c +2 -0
  655. data/third_party/boringssl-with-bazel/src/crypto/x509/t_x509a.c +3 -0
  656. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_att.c +19 -25
  657. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +3 -2
  658. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_req.c +42 -89
  659. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +9 -16
  660. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +2 -0
  661. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +14 -15
  662. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vpm.c +53 -73
  663. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +31 -0
  664. data/third_party/boringssl-with-bazel/src/crypto/x509/x509rset.c +3 -0
  665. data/third_party/boringssl-with-bazel/src/crypto/x509/x_algor.c +21 -17
  666. data/third_party/boringssl-with-bazel/src/crypto/x509/x_all.c +3 -0
  667. data/third_party/boringssl-with-bazel/src/crypto/x509/x_attrib.c +7 -25
  668. data/third_party/boringssl-with-bazel/src/crypto/x509/x_crl.c +5 -0
  669. data/third_party/boringssl-with-bazel/src/crypto/x509/x_pubkey.c +25 -22
  670. data/third_party/boringssl-with-bazel/src/crypto/x509/x_req.c +5 -8
  671. data/third_party/boringssl-with-bazel/src/crypto/x509/x_sig.c +5 -0
  672. data/third_party/boringssl-with-bazel/src/crypto/x509/x_val.c +2 -0
  673. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509a.c +3 -0
  674. data/third_party/boringssl-with-bazel/src/crypto/x509v3/internal.h +7 -0
  675. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_cpols.c +2 -4
  676. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +1 -1
  677. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_skey.c +1 -0
  678. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_utl.c +5 -8
  679. data/third_party/boringssl-with-bazel/src/include/openssl/aead.h +1 -4
  680. data/third_party/boringssl-with-bazel/src/include/openssl/arm_arch.h +66 -1
  681. data/third_party/boringssl-with-bazel/src/include/openssl/asn1.h +120 -41
  682. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +47 -7
  683. data/third_party/boringssl-with-bazel/src/include/openssl/bytestring.h +1 -0
  684. data/third_party/boringssl-with-bazel/src/include/openssl/chacha.h +1 -1
  685. data/third_party/boringssl-with-bazel/src/include/openssl/cipher.h +0 -8
  686. data/third_party/boringssl-with-bazel/src/include/openssl/crypto.h +24 -4
  687. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +6 -2
  688. data/third_party/boringssl-with-bazel/src/include/openssl/ec.h +5 -2
  689. data/third_party/boringssl-with-bazel/src/include/openssl/ecdsa.h +33 -0
  690. data/third_party/boringssl-with-bazel/src/include/openssl/err.h +3 -2
  691. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +20 -49
  692. data/third_party/boringssl-with-bazel/src/{crypto/x509/x509_r2x.c → include/openssl/evp_errors.h} +41 -58
  693. data/third_party/boringssl-with-bazel/src/include/openssl/hpke.h +325 -0
  694. data/third_party/boringssl-with-bazel/src/include/openssl/obj.h +24 -5
  695. data/third_party/boringssl-with-bazel/src/include/openssl/pkcs7.h +25 -7
  696. data/third_party/boringssl-with-bazel/src/include/openssl/pkcs8.h +9 -1
  697. data/third_party/boringssl-with-bazel/src/include/openssl/rand.h +2 -2
  698. data/third_party/boringssl-with-bazel/src/include/openssl/rsa.h +99 -63
  699. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +283 -85
  700. data/third_party/boringssl-with-bazel/src/include/openssl/tls1.h +13 -19
  701. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +445 -152
  702. data/third_party/boringssl-with-bazel/src/include/openssl/x509_vfy.h +451 -435
  703. data/third_party/boringssl-with-bazel/src/include/openssl/x509v3.h +2 -1
  704. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +7 -2
  705. data/third_party/boringssl-with-bazel/src/ssl/d1_srtp.cc +1 -1
  706. data/third_party/boringssl-with-bazel/src/ssl/encrypted_client_hello.cc +1133 -0
  707. data/third_party/boringssl-with-bazel/src/ssl/handoff.cc +298 -22
  708. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +66 -30
  709. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +189 -86
  710. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +154 -24
  711. data/third_party/boringssl-with-bazel/src/ssl/internal.h +414 -135
  712. data/third_party/boringssl-with-bazel/src/ssl/s3_both.cc +9 -3
  713. data/third_party/boringssl-with-bazel/src/ssl/s3_lib.cc +2 -2
  714. data/third_party/boringssl-with-bazel/src/ssl/s3_pkt.cc +14 -19
  715. data/third_party/boringssl-with-bazel/src/ssl/ssl_cert.cc +4 -6
  716. data/third_party/boringssl-with-bazel/src/ssl/ssl_key_share.cc +23 -26
  717. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +51 -60
  718. data/third_party/boringssl-with-bazel/src/ssl/ssl_privkey.cc +2 -0
  719. data/third_party/boringssl-with-bazel/src/ssl/ssl_session.cc +8 -31
  720. data/third_party/boringssl-with-bazel/src/ssl/ssl_stat.cc +3 -0
  721. data/third_party/boringssl-with-bazel/src/ssl/ssl_transcript.cc +4 -3
  722. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +7 -3
  723. data/third_party/boringssl-with-bazel/src/ssl/t1_lib.cc +664 -702
  724. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +65 -7
  725. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +98 -39
  726. data/third_party/boringssl-with-bazel/src/ssl/tls13_enc.cc +141 -94
  727. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +213 -118
  728. data/third_party/boringssl-with-bazel/src/ssl/tls_method.cc +4 -2
  729. data/third_party/xxhash/xxhash.h +77 -195
  730. metadata +116 -51
  731. data/src/core/lib/gpr/arena.h +0 -47
  732. data/src/core/lib/iomgr/poller/eventmanager_libuv.cc +0 -88
  733. data/src/core/lib/iomgr/poller/eventmanager_libuv.h +0 -88
  734. data/third_party/abseil-cpp/absl/base/internal/bits.h +0 -219
  735. data/third_party/abseil-cpp/absl/synchronization/internal/mutex_nonprod.inc +0 -249
  736. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/is_fips.c +0 -29
  737. data/third_party/boringssl-with-bazel/src/crypto/hpke/internal.h +0 -246
  738. data/third_party/boringssl-with-bazel/src/crypto/x509/vpm_int.h +0 -71
@@ -203,6 +203,7 @@ enum ssl_private_key_result_t ssl_private_key_sign(
203
203
  SSL *const ssl = hs->ssl;
204
204
  const SSL_PRIVATE_KEY_METHOD *key_method = hs->config->cert->key_method;
205
205
  EVP_PKEY *privatekey = hs->config->cert->privatekey.get();
206
+ assert(!hs->can_release_private_key);
206
207
  if (ssl_signing_with_dc(hs)) {
207
208
  key_method = hs->config->cert->dc_key_method;
208
209
  privatekey = hs->config->cert->dc_privatekey.get();
@@ -254,6 +255,7 @@ enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
254
255
  size_t max_out,
255
256
  Span<const uint8_t> in) {
256
257
  SSL *const ssl = hs->ssl;
258
+ assert(!hs->can_release_private_key);
257
259
  if (hs->config->cert->key_method != NULL) {
258
260
  enum ssl_private_key_result_t ret;
259
261
  if (hs->pending_private_key_op) {
@@ -350,19 +350,19 @@ const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session) {
350
350
  session->cipher);
351
351
  }
352
352
 
353
- int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
353
+ bool ssl_get_new_session(SSL_HANDSHAKE *hs) {
354
354
  SSL *const ssl = hs->ssl;
355
355
  if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
356
356
  OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
357
- return 0;
357
+ return false;
358
358
  }
359
359
 
360
360
  UniquePtr<SSL_SESSION> session = ssl_session_new(ssl->ctx->x509_method);
361
361
  if (session == NULL) {
362
- return 0;
362
+ return false;
363
363
  }
364
364
 
365
- session->is_server = is_server;
365
+ session->is_server = ssl->server;
366
366
  session->ssl_version = ssl->version;
367
367
  session->is_quic = ssl->quic_method != nullptr;
368
368
 
@@ -384,24 +384,9 @@ int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
384
384
  session->auth_timeout = ssl->session_ctx->session_timeout;
385
385
  }
386
386
 
387
- if (is_server) {
388
- if (hs->ticket_expected || version >= TLS1_3_VERSION) {
389
- // Don't set session IDs for sessions resumed with tickets. This will keep
390
- // them out of the session cache.
391
- session->session_id_length = 0;
392
- } else {
393
- session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
394
- if (!RAND_bytes(session->session_id, session->session_id_length)) {
395
- return 0;
396
- }
397
- }
398
- } else {
399
- session->session_id_length = 0;
400
- }
401
-
402
387
  if (hs->config->cert->sid_ctx_length > sizeof(session->sid_ctx)) {
403
388
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
404
- return 0;
389
+ return false;
405
390
  }
406
391
  OPENSSL_memcpy(session->sid_ctx, hs->config->cert->sid_ctx,
407
392
  hs->config->cert->sid_ctx_length);
@@ -413,7 +398,7 @@ int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
413
398
 
414
399
  hs->new_session = std::move(session);
415
400
  ssl_set_session(ssl, NULL);
416
- return 1;
401
+ return true;
417
402
  }
418
403
 
419
404
  int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) {
@@ -1019,7 +1004,8 @@ int SSL_SESSION_should_be_single_use(const SSL_SESSION *session) {
1019
1004
  }
1020
1005
 
1021
1006
  int SSL_SESSION_is_resumable(const SSL_SESSION *session) {
1022
- return !session->not_resumable;
1007
+ return !session->not_resumable &&
1008
+ (session->session_id_length != 0 || !session->ticket.empty());
1023
1009
  }
1024
1010
 
1025
1011
  int SSL_SESSION_has_ticket(const SSL_SESSION *session) {
@@ -1298,12 +1284,3 @@ void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
1298
1284
  int value) {
1299
1285
  return ctx->info_callback;
1300
1286
  }
1301
-
1302
- void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
1303
- void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
1304
- ctx->channel_id_cb = cb;
1305
- }
1306
-
1307
- void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
1308
- return ctx->channel_id_cb;
1309
- }
@@ -224,6 +224,9 @@ const char *SSL_alert_desc_string_long(int value) {
224
224
  case TLS1_AD_NO_APPLICATION_PROTOCOL:
225
225
  return "no application protocol";
226
226
 
227
+ case TLS1_AD_ECH_REQUIRED:
228
+ return "ECH required";
229
+
227
230
  default:
228
231
  return "unknown";
229
232
  }
@@ -206,7 +206,8 @@ bool SSLTranscript::UpdateForHelloRetryRequest() {
206
206
  return true;
207
207
  }
208
208
 
209
- bool SSLTranscript::CopyToHashContext(EVP_MD_CTX *ctx, const EVP_MD *digest) {
209
+ bool SSLTranscript::CopyToHashContext(EVP_MD_CTX *ctx,
210
+ const EVP_MD *digest) const {
210
211
  const EVP_MD *transcript_digest = Digest();
211
212
  if (transcript_digest != nullptr &&
212
213
  EVP_MD_type(transcript_digest) == EVP_MD_type(digest)) {
@@ -237,7 +238,7 @@ bool SSLTranscript::Update(Span<const uint8_t> in) {
237
238
  return true;
238
239
  }
239
240
 
240
- bool SSLTranscript::GetHash(uint8_t *out, size_t *out_len) {
241
+ bool SSLTranscript::GetHash(uint8_t *out, size_t *out_len) const {
241
242
  ScopedEVP_MD_CTX ctx;
242
243
  unsigned len;
243
244
  if (!EVP_MD_CTX_copy_ex(ctx.get(), hash_.get()) ||
@@ -250,7 +251,7 @@ bool SSLTranscript::GetHash(uint8_t *out, size_t *out_len) {
250
251
 
251
252
  bool SSLTranscript::GetFinishedMAC(uint8_t *out, size_t *out_len,
252
253
  const SSL_SESSION *session,
253
- bool from_server) {
254
+ bool from_server) const {
254
255
  static const char kClientLabel[] = "client finished";
255
256
  static const char kServerLabel[] = "server finished";
256
257
  auto label = from_server
@@ -260,8 +260,8 @@ uint16_t ssl_protocol_version(const SSL *ssl) {
260
260
  return version;
261
261
  }
262
262
 
263
- bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
264
- SSL *const ssl = hs->ssl;
263
+ bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version) {
264
+ const SSL *const ssl = hs->ssl;
265
265
  uint16_t protocol_version;
266
266
  if (!ssl_method_supports_version(ssl->method, version) ||
267
267
  !ssl_protocol_version_from_wire(&protocol_version, version) ||
@@ -273,9 +273,13 @@ bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
273
273
  return true;
274
274
  }
275
275
 
276
- bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
276
+ bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb,
277
+ uint16_t extra_min_version) {
277
278
  for (uint16_t version : get_method_versions(hs->ssl->method)) {
279
+ uint16_t protocol_version;
278
280
  if (ssl_supports_version(hs, version) &&
281
+ ssl_protocol_version_from_wire(&protocol_version, version) &&
282
+ protocol_version >= extra_min_version && //
279
283
  !CBB_add_u16(cbb, version)) {
280
284
  return false;
281
285
  }
@@ -124,11 +124,11 @@
124
124
  #include <openssl/err.h>
125
125
  #include <openssl/evp.h>
126
126
  #include <openssl/hmac.h>
127
+ #include <openssl/hpke.h>
127
128
  #include <openssl/mem.h>
128
129
  #include <openssl/nid.h>
129
130
  #include <openssl/rand.h>
130
131
 
131
- #include "../crypto/hpke/internal.h"
132
132
  #include "../crypto/internal.h"
133
133
  #include "internal.h"
134
134
 
@@ -209,11 +209,11 @@ static bool is_post_quantum_group(uint16_t id) {
209
209
  }
210
210
 
211
211
  bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out,
212
- const SSLMessage &msg) {
212
+ Span<const uint8_t> body) {
213
213
  OPENSSL_memset(out, 0, sizeof(*out));
214
214
  out->ssl = const_cast<SSL *>(ssl);
215
- out->client_hello = CBS_data(&msg.body);
216
- out->client_hello_len = CBS_len(&msg.body);
215
+ out->client_hello = body.data();
216
+ out->client_hello_len = body.size();
217
217
 
218
218
  CBS client_hello, random, session_id;
219
219
  CBS_init(&client_hello, out->client_hello, out->client_hello_len);
@@ -405,6 +405,11 @@ bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) {
405
405
  return false;
406
406
  }
407
407
 
408
+ // We internally assume zero is never allocated as a group ID.
409
+ if (group_id == 0) {
410
+ return false;
411
+ }
412
+
408
413
  for (uint16_t supported : tls1_get_grouplist(hs)) {
409
414
  if (supported == group_id) {
410
415
  return true;
@@ -488,9 +493,7 @@ bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert,
488
493
  return false;
489
494
  }
490
495
 
491
- // tls_extension represents a TLS extension that is handled internally. The
492
- // |init| function is called for each handshake, before any other functions of
493
- // the extension. Then the add and parse callbacks are called as needed.
496
+ // tls_extension represents a TLS extension that is handled internally.
494
497
  //
495
498
  // The parse callbacks receive a |CBS| that contains the contents of the
496
499
  // extension (i.e. not including the type and length bytes). If an extension is
@@ -500,14 +503,27 @@ bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert,
500
503
  // The add callbacks receive a |CBB| to which the extension can be appended but
501
504
  // the function is responsible for appending the type and length bytes too.
502
505
  //
506
+ // |add_clienthello| may be called multiple times and must not mutate |hs|. It
507
+ // is additionally passed two output |CBB|s. If the extension is the same
508
+ // independent of the value of |type|, the callback may write to
509
+ // |out_compressible| instead of |out|. When serializing the ClientHelloInner,
510
+ // all compressible extensions will be made continguous and replaced with
511
+ // ech_outer_extensions when encrypted. When serializing the ClientHelloOuter
512
+ // or not offering ECH, |out| will be equal to |out_compressible|, so writing to
513
+ // |out_compressible| still works.
514
+ //
515
+ // Note the |parse_serverhello| and |add_serverhello| callbacks refer to the
516
+ // TLS 1.2 ServerHello. In TLS 1.3, these callbacks act on EncryptedExtensions,
517
+ // with ServerHello extensions handled elsewhere in the handshake.
518
+ //
503
519
  // All callbacks return true for success and false for error. If a parse
504
520
  // function returns zero then a fatal alert with value |*out_alert| will be
505
521
  // sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent.
506
522
  struct tls_extension {
507
523
  uint16_t value;
508
- void (*init)(SSL_HANDSHAKE *hs);
509
524
 
510
- bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
525
+ bool (*add_clienthello)(const SSL_HANDSHAKE *hs, CBB *out,
526
+ CBB *out_compressible, ssl_client_hello_type_t type);
511
527
  bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
512
528
  CBS *contents);
513
529
 
@@ -542,10 +558,21 @@ static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
542
558
  //
543
559
  // https://tools.ietf.org/html/rfc6066#section-3.
544
560
 
545
- static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
546
- SSL *const ssl = hs->ssl;
547
- if (ssl->hostname == nullptr) {
548
- return true;
561
+ static bool ext_sni_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
562
+ CBB *out_compressible,
563
+ ssl_client_hello_type_t type) {
564
+ const SSL *const ssl = hs->ssl;
565
+ // If offering ECH, send the public name instead of the configured name.
566
+ Span<const uint8_t> hostname;
567
+ if (type == ssl_client_hello_outer) {
568
+ hostname = hs->selected_ech_config->public_name;
569
+ } else {
570
+ if (ssl->hostname == nullptr) {
571
+ return true;
572
+ }
573
+ hostname =
574
+ MakeConstSpan(reinterpret_cast<const uint8_t *>(ssl->hostname.get()),
575
+ strlen(ssl->hostname.get()));
549
576
  }
550
577
 
551
578
  CBB contents, server_name_list, name;
@@ -554,8 +581,7 @@ static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
554
581
  !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
555
582
  !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
556
583
  !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
557
- !CBB_add_bytes(&name, (const uint8_t *)ssl->hostname.get(),
558
- strlen(ssl->hostname.get())) ||
584
+ !CBB_add_bytes(&name, hostname.data(), hostname.size()) ||
559
585
  !CBB_flush(out)) {
560
586
  return false;
561
587
  }
@@ -591,150 +617,59 @@ static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
591
617
  }
592
618
 
593
619
 
594
- // Encrypted Client Hello (ECH)
620
+ // Encrypted ClientHello (ECH)
595
621
  //
596
- // https://tools.ietf.org/html/draft-ietf-tls-esni-09
597
-
598
- // random_size returns a random value between |min| and |max|, inclusive.
599
- static size_t random_size(size_t min, size_t max) {
600
- assert(min < max);
601
- size_t value;
602
- RAND_bytes(reinterpret_cast<uint8_t *>(&value), sizeof(value));
603
- return value % (max - min + 1) + min;
604
- }
605
-
606
- static bool ext_ech_add_clienthello_grease(SSL_HANDSHAKE *hs, CBB *out) {
607
- // If we are responding to the server's HelloRetryRequest, we repeat the bytes
608
- // of the first ECH GREASE extension.
609
- if (hs->ssl->s3->used_hello_retry_request) {
610
- CBB ech_body;
611
- if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
612
- !CBB_add_u16_length_prefixed(out, &ech_body) ||
613
- !CBB_add_bytes(&ech_body, hs->ech_grease.data(),
614
- hs->ech_grease.size()) ||
615
- !CBB_flush(out)) {
616
- return false;
617
- }
622
+ // https://tools.ietf.org/html/draft-ietf-tls-esni-10
623
+
624
+ static bool ext_ech_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
625
+ CBB *out_compressible,
626
+ ssl_client_hello_type_t type) {
627
+ if (type == ssl_client_hello_inner || hs->ech_client_bytes.empty()) {
618
628
  return true;
619
629
  }
620
630
 
621
- constexpr uint16_t kdf_id = EVP_HPKE_HKDF_SHA256;
622
- const uint16_t aead_id = EVP_has_aes_hardware()
623
- ? EVP_HPKE_AEAD_AES_GCM_128
624
- : EVP_HPKE_AEAD_CHACHA20POLY1305;
625
- const EVP_AEAD *aead = EVP_HPKE_get_aead(aead_id);
626
- assert(aead != nullptr);
627
-
628
- uint8_t ech_config_id[8];
629
- RAND_bytes(ech_config_id, sizeof(ech_config_id));
630
-
631
- uint8_t ech_enc[X25519_PUBLIC_VALUE_LEN];
632
- uint8_t private_key_unused[X25519_PRIVATE_KEY_LEN];
633
- X25519_keypair(ech_enc, private_key_unused);
634
-
635
- // To determine a plausible length for the payload, we first estimate the size
636
- // of a typical EncodedClientHelloInner, with an expected use of
637
- // outer_extensions. To limit the size, we only consider initial ClientHellos
638
- // that do not offer resumption.
639
- //
640
- // Field/Extension Size
641
- // ---------------------------------------------------------------------
642
- // version 2
643
- // random 32
644
- // legacy_session_id 1
645
- // - Has a U8 length prefix, but body is
646
- // always empty string in inner CH.
647
- // cipher_suites 2 (length prefix)
648
- // - Only includes TLS 1.3 ciphers (3). 6
649
- // - Maybe also include a GREASE suite. 2
650
- // legacy_compression_methods 2 (length prefix)
651
- // - Always has "null" compression method. 1
652
- // extensions: 2 (length prefix)
653
- // - encrypted_client_hello (empty). 4 (id + length prefix)
654
- // - supported_versions. 4 (id + length prefix)
655
- // - U8 length prefix 1
656
- // - U16 protocol version (TLS 1.3) 2
657
- // - outer_extensions. 4 (id + length prefix)
658
- // - U8 length prefix 1
659
- // - N extension IDs (2 bytes each):
660
- // - key_share 2
661
- // - sigalgs 2
662
- // - sct 2
663
- // - alpn 2
664
- // - supported_groups. 2
665
- // - status_request. 2
666
- // - psk_key_exchange_modes. 2
667
- // - compress_certificate. 2
668
- //
669
- // The server_name extension has an overhead of 9 bytes, plus up to an
670
- // estimated 100 bytes of hostname. Rounding up to a multiple of 32 yields a
671
- // range of 96 to 192. Note that this estimate does not fully capture
672
- // optional extensions like GREASE, but the rounding gives some leeway.
673
-
674
- uint8_t payload[EVP_AEAD_MAX_OVERHEAD + 192];
675
- const size_t payload_len =
676
- EVP_AEAD_max_overhead(aead) + 32 * random_size(96 / 32, 192 / 32);
677
- assert(payload_len <= sizeof(payload));
678
- RAND_bytes(payload, payload_len);
679
-
680
- // Inside the TLS extension contents, write a serialized ClientEncryptedCH.
681
- CBB ech_body, config_id_cbb, enc_cbb, payload_cbb;
631
+ CBB ech_body;
682
632
  if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
683
633
  !CBB_add_u16_length_prefixed(out, &ech_body) ||
684
- !CBB_add_u16(&ech_body, kdf_id) || //
685
- !CBB_add_u16(&ech_body, aead_id) ||
686
- !CBB_add_u8_length_prefixed(&ech_body, &config_id_cbb) ||
687
- !CBB_add_bytes(&config_id_cbb, ech_config_id, sizeof(ech_config_id)) ||
688
- !CBB_add_u16_length_prefixed(&ech_body, &enc_cbb) ||
689
- !CBB_add_bytes(&enc_cbb, ech_enc, OPENSSL_ARRAY_SIZE(ech_enc)) ||
690
- !CBB_add_u16_length_prefixed(&ech_body, &payload_cbb) ||
691
- !CBB_add_bytes(&payload_cbb, payload, payload_len) || //
692
- !CBB_flush(&ech_body)) {
693
- return false;
694
- }
695
- // Save the bytes of the newly-generated extension in case the server sends
696
- // a HelloRetryRequest.
697
- if (!hs->ech_grease.CopyFrom(
698
- MakeConstSpan(CBB_data(&ech_body), CBB_len(&ech_body)))) {
634
+ !CBB_add_bytes(&ech_body, hs->ech_client_bytes.data(),
635
+ hs->ech_client_bytes.size()) ||
636
+ !CBB_flush(out)) {
699
637
  return false;
700
638
  }
701
- return CBB_flush(out);
702
- }
703
-
704
- static bool ext_ech_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
705
- if (hs->max_version < TLS1_3_VERSION) {
706
- return true;
707
- }
708
- if (hs->config->ech_grease_enabled) {
709
- return ext_ech_add_clienthello_grease(hs, out);
710
- }
711
- // Nothing to do, since we don't yet implement the non-GREASE parts of ECH.
712
639
  return true;
713
640
  }
714
641
 
715
642
  static bool ext_ech_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
716
643
  CBS *contents) {
644
+ SSL *const ssl = hs->ssl;
717
645
  if (contents == NULL) {
718
646
  return true;
719
647
  }
720
648
 
721
- // If the client only sent GREASE, we must check the extension syntactically.
722
- CBS ech_configs;
723
- if (!CBS_get_u16_length_prefixed(contents, &ech_configs) ||
724
- CBS_len(&ech_configs) == 0 || //
725
- CBS_len(contents) > 0) {
726
- *out_alert = SSL_AD_DECODE_ERROR;
649
+ // The ECH extension may not be sent in TLS 1.2 ServerHello, only TLS 1.3
650
+ // EncryptedExtension.
651
+ if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
652
+ *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
653
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
727
654
  return false;
728
655
  }
729
- while (CBS_len(&ech_configs) > 0) {
730
- // Do a top-level parse of the ECHConfig, stopping before ECHConfigContents.
731
- uint16_t version;
732
- CBS ech_config_contents;
733
- if (!CBS_get_u16(&ech_configs, &version) ||
734
- !CBS_get_u16_length_prefixed(&ech_configs, &ech_config_contents)) {
735
- *out_alert = SSL_AD_DECODE_ERROR;
736
- return false;
737
- }
656
+
657
+ // The server may only send retry configs in response to ClientHelloOuter (or
658
+ // ECH GREASE), not ClientHelloInner. The unsolicited extension rule checks
659
+ // this implicitly because the ClientHelloInner has no encrypted_client_hello
660
+ // extension.
661
+ //
662
+ // TODO(https://crbug.com/boringssl/275): If
663
+ // https://github.com/tlswg/draft-ietf-tls-esni/pull/422 is merged, a later
664
+ // draft will fold encrypted_client_hello and ech_is_inner together. Then this
665
+ // assert should become a runtime check.
666
+ assert(!ssl->s3->ech_accept);
667
+
668
+ // TODO(https://crbug.com/boringssl/275): When the implementing the
669
+ // ClientHelloOuter flow, save the retry configs.
670
+ if (!ssl_is_valid_ech_config_list(*contents)) {
671
+ *out_alert = SSL_AD_DECODE_ERROR;
672
+ return false;
738
673
  }
739
674
  return true;
740
675
  }
@@ -748,7 +683,43 @@ static bool ext_ech_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
748
683
  return true;
749
684
  }
750
685
 
751
- static bool ext_ech_is_inner_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
686
+ static bool ext_ech_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
687
+ SSL *const ssl = hs->ssl;
688
+ if (ssl_protocol_version(ssl) < TLS1_3_VERSION || //
689
+ ssl->s3->ech_accept || //
690
+ hs->ech_keys == nullptr) {
691
+ return true;
692
+ }
693
+
694
+ // Write the list of retry configs to |out|. Note |SSL_CTX_set1_ech_keys|
695
+ // ensures |ech_keys| contains at least one retry config.
696
+ CBB body, retry_configs;
697
+ if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
698
+ !CBB_add_u16_length_prefixed(out, &body) ||
699
+ !CBB_add_u16_length_prefixed(&body, &retry_configs)) {
700
+ return false;
701
+ }
702
+ for (const auto &config : hs->ech_keys->configs) {
703
+ if (!config->is_retry_config()) {
704
+ continue;
705
+ }
706
+ if (!CBB_add_bytes(&retry_configs, config->ech_config().raw.data(),
707
+ config->ech_config().raw.size())) {
708
+ return false;
709
+ }
710
+ }
711
+ return CBB_flush(out);
712
+ }
713
+
714
+ static bool ext_ech_is_inner_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
715
+ CBB *out_compressible,
716
+ ssl_client_hello_type_t type) {
717
+ if (type == ssl_client_hello_inner) {
718
+ if (!CBB_add_u16(out, TLSEXT_TYPE_ech_is_inner) ||
719
+ !CBB_add_u16(out, 0 /* empty extension */)) {
720
+ return false;
721
+ }
722
+ }
752
723
  return true;
753
724
  }
754
725
 
@@ -771,10 +742,13 @@ static bool ext_ech_is_inner_parse_clienthello(SSL_HANDSHAKE *hs,
771
742
  //
772
743
  // https://tools.ietf.org/html/rfc5746
773
744
 
774
- static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
775
- SSL *const ssl = hs->ssl;
745
+ static bool ext_ri_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
746
+ CBB *out_compressible,
747
+ ssl_client_hello_type_t type) {
748
+ const SSL *const ssl = hs->ssl;
776
749
  // Renegotiation indication is not necessary in TLS 1.3.
777
- if (hs->min_version >= TLS1_3_VERSION) {
750
+ if (hs->min_version >= TLS1_3_VERSION ||
751
+ type == ssl_client_hello_inner) {
778
752
  return true;
779
753
  }
780
754
 
@@ -936,9 +910,11 @@ static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
936
910
  //
937
911
  // https://tools.ietf.org/html/rfc7627
938
912
 
939
- static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
913
+ static bool ext_ems_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
914
+ CBB *out_compressible,
915
+ ssl_client_hello_type_t type) {
940
916
  // Extended master secret is not necessary in TLS 1.3.
941
- if (hs->min_version >= TLS1_3_VERSION) {
917
+ if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) {
942
918
  return true;
943
919
  }
944
920
 
@@ -1011,10 +987,12 @@ static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1011
987
  //
1012
988
  // https://tools.ietf.org/html/rfc5077
1013
989
 
1014
- static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1015
- SSL *const ssl = hs->ssl;
990
+ static bool ext_ticket_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
991
+ CBB *out_compressible,
992
+ ssl_client_hello_type_t type) {
993
+ const SSL *const ssl = hs->ssl;
1016
994
  // TLS 1.3 uses a different ticket extension.
1017
- if (hs->min_version >= TLS1_3_VERSION ||
995
+ if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner ||
1018
996
  SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1019
997
  return true;
1020
998
  }
@@ -1089,17 +1067,19 @@ static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1089
1067
  //
1090
1068
  // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
1091
1069
 
1092
- static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1070
+ static bool ext_sigalgs_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1071
+ CBB *out_compressible,
1072
+ ssl_client_hello_type_t type) {
1093
1073
  if (hs->max_version < TLS1_2_VERSION) {
1094
1074
  return true;
1095
1075
  }
1096
1076
 
1097
1077
  CBB contents, sigalgs_cbb;
1098
- if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1099
- !CBB_add_u16_length_prefixed(out, &contents) ||
1078
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_signature_algorithms) ||
1079
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
1100
1080
  !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1101
1081
  !tls12_add_verify_sigalgs(hs, &sigalgs_cbb) ||
1102
- !CBB_flush(out)) {
1082
+ !CBB_flush(out_compressible)) {
1103
1083
  return false;
1104
1084
  }
1105
1085
 
@@ -1128,18 +1108,20 @@ static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1128
1108
  //
1129
1109
  // https://tools.ietf.org/html/rfc6066#section-8
1130
1110
 
1131
- static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1111
+ static bool ext_ocsp_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1112
+ CBB *out_compressible,
1113
+ ssl_client_hello_type_t type) {
1132
1114
  if (!hs->config->ocsp_stapling_enabled) {
1133
1115
  return true;
1134
1116
  }
1135
1117
 
1136
1118
  CBB contents;
1137
- if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1138
- !CBB_add_u16_length_prefixed(out, &contents) ||
1119
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_status_request) ||
1120
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
1139
1121
  !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1140
1122
  !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1141
1123
  !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1142
- !CBB_flush(out)) {
1124
+ !CBB_flush(out_compressible)) {
1143
1125
  return false;
1144
1126
  }
1145
1127
 
@@ -1210,11 +1192,16 @@ static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1210
1192
  //
1211
1193
  // https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
1212
1194
 
1213
- static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1214
- SSL *const ssl = hs->ssl;
1215
- if (ssl->s3->initial_handshake_complete ||
1216
- ssl->ctx->next_proto_select_cb == NULL ||
1217
- SSL_is_dtls(ssl)) {
1195
+ static bool ext_npn_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1196
+ CBB *out_compressible,
1197
+ ssl_client_hello_type_t type) {
1198
+ const SSL *const ssl = hs->ssl;
1199
+ if (ssl->ctx->next_proto_select_cb == NULL ||
1200
+ // Do not allow NPN to change on renegotiation.
1201
+ ssl->s3->initial_handshake_complete ||
1202
+ // NPN is not defined in DTLS or TLS 1.3.
1203
+ SSL_is_dtls(ssl) || hs->min_version >= TLS1_3_VERSION ||
1204
+ type == ssl_client_hello_inner) {
1218
1205
  return true;
1219
1206
  }
1220
1207
 
@@ -1333,13 +1320,15 @@ static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1333
1320
  //
1334
1321
  // https://tools.ietf.org/html/rfc6962#section-3.3.1
1335
1322
 
1336
- static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1323
+ static bool ext_sct_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1324
+ CBB *out_compressible,
1325
+ ssl_client_hello_type_t type) {
1337
1326
  if (!hs->config->signed_cert_timestamps_enabled) {
1338
1327
  return true;
1339
1328
  }
1340
1329
 
1341
- if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1342
- !CBB_add_u16(out, 0 /* length */)) {
1330
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_certificate_timestamp) ||
1331
+ !CBB_add_u16(out_compressible, 0 /* length */)) {
1343
1332
  return false;
1344
1333
  }
1345
1334
 
@@ -1424,8 +1413,10 @@ static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1424
1413
  //
1425
1414
  // https://tools.ietf.org/html/rfc7301
1426
1415
 
1427
- static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1428
- SSL *const ssl = hs->ssl;
1416
+ static bool ext_alpn_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1417
+ CBB *out_compressible,
1418
+ ssl_client_hello_type_t type) {
1419
+ const SSL *const ssl = hs->ssl;
1429
1420
  if (hs->config->alpn_client_proto_list.empty() && ssl->quic_method) {
1430
1421
  // ALPN MUST be used with QUIC.
1431
1422
  OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL);
@@ -1438,12 +1429,13 @@ static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1438
1429
  }
1439
1430
 
1440
1431
  CBB contents, proto_list;
1441
- if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1442
- !CBB_add_u16_length_prefixed(out, &contents) ||
1432
+ if (!CBB_add_u16(out_compressible,
1433
+ TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1434
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
1443
1435
  !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1444
1436
  !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(),
1445
1437
  hs->config->alpn_client_proto_list.size()) ||
1446
- !CBB_flush(out)) {
1438
+ !CBB_flush(out_compressible)) {
1447
1439
  return false;
1448
1440
  }
1449
1441
 
@@ -1499,6 +1491,22 @@ static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1499
1491
  return true;
1500
1492
  }
1501
1493
 
1494
+ bool ssl_is_valid_alpn_list(Span<const uint8_t> in) {
1495
+ CBS protocol_name_list = in;
1496
+ if (CBS_len(&protocol_name_list) == 0) {
1497
+ return false;
1498
+ }
1499
+ while (CBS_len(&protocol_name_list) > 0) {
1500
+ CBS protocol_name;
1501
+ if (!CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1502
+ // Empty protocol names are forbidden.
1503
+ CBS_len(&protocol_name) == 0) {
1504
+ return false;
1505
+ }
1506
+ }
1507
+ return true;
1508
+ }
1509
+
1502
1510
  bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs,
1503
1511
  Span<const uint8_t> protocol) {
1504
1512
  if (hs->config->alpn_client_proto_list.empty()) {
@@ -1551,25 +1559,12 @@ bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1551
1559
  CBS protocol_name_list;
1552
1560
  if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
1553
1561
  CBS_len(&contents) != 0 ||
1554
- CBS_len(&protocol_name_list) < 2) {
1562
+ !ssl_is_valid_alpn_list(protocol_name_list)) {
1555
1563
  OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1556
1564
  *out_alert = SSL_AD_DECODE_ERROR;
1557
1565
  return false;
1558
1566
  }
1559
1567
 
1560
- // Validate the protocol list.
1561
- CBS protocol_name_list_copy = protocol_name_list;
1562
- while (CBS_len(&protocol_name_list_copy) > 0) {
1563
- CBS protocol_name;
1564
- if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1565
- // Empty protocol names are forbidden.
1566
- CBS_len(&protocol_name) == 0) {
1567
- OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1568
- *out_alert = SSL_AD_DECODE_ERROR;
1569
- return false;
1570
- }
1571
- }
1572
-
1573
1568
  const uint8_t *selected;
1574
1569
  uint8_t selected_len;
1575
1570
  int ret = ssl->ctx->alpn_select_cb(
@@ -1635,18 +1630,16 @@ static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1635
1630
  //
1636
1631
  // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
1637
1632
 
1638
- static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1639
- hs->ssl->s3->channel_id_valid = false;
1640
- }
1641
-
1642
- static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1643
- SSL *const ssl = hs->ssl;
1644
- if (!hs->config->channel_id_enabled || SSL_is_dtls(ssl)) {
1633
+ static bool ext_channel_id_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1634
+ CBB *out_compressible,
1635
+ ssl_client_hello_type_t type) {
1636
+ const SSL *const ssl = hs->ssl;
1637
+ if (!hs->config->channel_id_private || SSL_is_dtls(ssl)) {
1645
1638
  return true;
1646
1639
  }
1647
1640
 
1648
- if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1649
- !CBB_add_u16(out, 0 /* length */)) {
1641
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_channel_id) ||
1642
+ !CBB_add_u16(out_compressible, 0 /* length */)) {
1650
1643
  return false;
1651
1644
  }
1652
1645
 
@@ -1656,19 +1649,18 @@ static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1656
1649
  static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1657
1650
  uint8_t *out_alert,
1658
1651
  CBS *contents) {
1659
- SSL *const ssl = hs->ssl;
1660
1652
  if (contents == NULL) {
1661
1653
  return true;
1662
1654
  }
1663
1655
 
1664
- assert(!SSL_is_dtls(ssl));
1665
- assert(hs->config->channel_id_enabled);
1656
+ assert(!SSL_is_dtls(hs->ssl));
1657
+ assert(hs->config->channel_id_private);
1666
1658
 
1667
1659
  if (CBS_len(contents) != 0) {
1668
1660
  return false;
1669
1661
  }
1670
1662
 
1671
- ssl->s3->channel_id_valid = true;
1663
+ hs->channel_id_negotiated = true;
1672
1664
  return true;
1673
1665
  }
1674
1666
 
@@ -1684,13 +1676,12 @@ static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1684
1676
  return false;
1685
1677
  }
1686
1678
 
1687
- ssl->s3->channel_id_valid = true;
1679
+ hs->channel_id_negotiated = true;
1688
1680
  return true;
1689
1681
  }
1690
1682
 
1691
1683
  static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1692
- SSL *const ssl = hs->ssl;
1693
- if (!ssl->s3->channel_id_valid) {
1684
+ if (!hs->channel_id_negotiated) {
1694
1685
  return true;
1695
1686
  }
1696
1687
 
@@ -1707,22 +1698,21 @@ static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1707
1698
  //
1708
1699
  // https://tools.ietf.org/html/rfc5764
1709
1700
 
1710
-
1711
- static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1712
- hs->ssl->s3->srtp_profile = NULL;
1713
- }
1714
-
1715
- static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1716
- SSL *const ssl = hs->ssl;
1717
- STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1701
+ static bool ext_srtp_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1702
+ CBB *out_compressible,
1703
+ ssl_client_hello_type_t type) {
1704
+ const SSL *const ssl = hs->ssl;
1705
+ const STACK_OF(SRTP_PROTECTION_PROFILE) *profiles =
1706
+ SSL_get_srtp_profiles(ssl);
1718
1707
  if (profiles == NULL ||
1719
- sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
1708
+ sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0 ||
1709
+ !SSL_is_dtls(ssl)) {
1720
1710
  return true;
1721
1711
  }
1722
1712
 
1723
1713
  CBB contents, profile_ids;
1724
- if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1725
- !CBB_add_u16_length_prefixed(out, &contents) ||
1714
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_srtp) ||
1715
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
1726
1716
  !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1727
1717
  return false;
1728
1718
  }
@@ -1734,7 +1724,7 @@ static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1734
1724
  }
1735
1725
 
1736
1726
  if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1737
- !CBB_flush(out)) {
1727
+ !CBB_flush(out_compressible)) {
1738
1728
  return false;
1739
1729
  }
1740
1730
 
@@ -1752,6 +1742,7 @@ static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1752
1742
  // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1753
1743
  //
1754
1744
  // See https://tools.ietf.org/html/rfc5764#section-4.1.1
1745
+ assert(SSL_is_dtls(ssl));
1755
1746
  CBS profile_ids, srtp_mki;
1756
1747
  uint16_t profile_id;
1757
1748
  if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
@@ -1770,11 +1761,8 @@ static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1770
1761
  return false;
1771
1762
  }
1772
1763
 
1773
- STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1774
-
1775
- // Check to see if the server gave us something we support (and presumably
1776
- // offered).
1777
- for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
1764
+ // Check to see if the server gave us something we support and offered.
1765
+ for (const SRTP_PROTECTION_PROFILE *profile : SSL_get_srtp_profiles(ssl)) {
1778
1766
  if (profile->id == profile_id) {
1779
1767
  ssl->s3->srtp_profile = profile;
1780
1768
  return true;
@@ -1789,7 +1777,8 @@ static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1789
1777
  static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1790
1778
  CBS *contents) {
1791
1779
  SSL *const ssl = hs->ssl;
1792
- if (contents == NULL) {
1780
+ // DTLS-SRTP is only defined for DTLS.
1781
+ if (contents == NULL || !SSL_is_dtls(ssl)) {
1793
1782
  return true;
1794
1783
  }
1795
1784
 
@@ -1833,6 +1822,7 @@ static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1833
1822
  return true;
1834
1823
  }
1835
1824
 
1825
+ assert(SSL_is_dtls(ssl));
1836
1826
  CBB contents, profile_ids;
1837
1827
  if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1838
1828
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -1851,7 +1841,7 @@ static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1851
1841
  //
1852
1842
  // https://tools.ietf.org/html/rfc4492#section-5.1.2
1853
1843
 
1854
- static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
1844
+ static bool ext_ec_point_add_extension(const SSL_HANDSHAKE *hs, CBB *out) {
1855
1845
  CBB contents, formats;
1856
1846
  if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1857
1847
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -1864,9 +1854,11 @@ static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
1864
1854
  return true;
1865
1855
  }
1866
1856
 
1867
- static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1857
+ static bool ext_ec_point_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
1858
+ CBB *out_compressible,
1859
+ ssl_client_hello_type_t type) {
1868
1860
  // The point format extension is unnecessary in TLS 1.3.
1869
- if (hs->min_version >= TLS1_3_VERSION) {
1861
+ if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) {
1870
1862
  return true;
1871
1863
  }
1872
1864
 
@@ -1932,10 +1924,37 @@ static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1932
1924
  //
1933
1925
  // https://tools.ietf.org/html/rfc8446#section-4.2.11
1934
1926
 
1935
- static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1936
- SSL *const ssl = hs->ssl;
1927
+ static bool should_offer_psk(const SSL_HANDSHAKE *hs,
1928
+ ssl_client_hello_type_t type) {
1929
+ const SSL *const ssl = hs->ssl;
1937
1930
  if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1938
- ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
1931
+ ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
1932
+ // The ClientHelloOuter cannot include the PSK extension.
1933
+ //
1934
+ // TODO(https://crbug.com/boringssl/275): draft-ietf-tls-esni-10 mandates
1935
+ // this, but it risks breaking the ClientHelloOuter flow on 0-RTT reject.
1936
+ // Later drafts will recommend including a placeholder one, at which point
1937
+ // we will need to synthesize a ticket. See
1938
+ // https://github.com/tlswg/draft-ietf-tls-esni/issues/408
1939
+ type == ssl_client_hello_outer) {
1940
+ return false;
1941
+ }
1942
+
1943
+ // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1944
+ // cipher in HelloRetryRequest does not match. This avoids performing the
1945
+ // transcript hash transformation for multiple hashes.
1946
+ if (ssl->s3->used_hello_retry_request &&
1947
+ ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1948
+ return false;
1949
+ }
1950
+
1951
+ return true;
1952
+ }
1953
+
1954
+ static size_t ext_pre_shared_key_clienthello_length(
1955
+ const SSL_HANDSHAKE *hs, ssl_client_hello_type_t type) {
1956
+ const SSL *const ssl = hs->ssl;
1957
+ if (!should_offer_psk(hs, type)) {
1939
1958
  return 0;
1940
1959
  }
1941
1960
 
@@ -1943,19 +1962,12 @@ static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1943
1962
  return 15 + ssl->session->ticket.size() + binder_len;
1944
1963
  }
1945
1964
 
1946
- static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1947
- SSL *const ssl = hs->ssl;
1948
- hs->needs_psk_binder = false;
1949
- if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1950
- ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
1951
- return true;
1952
- }
1953
-
1954
- // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1955
- // cipher in HelloRetryRequest does not match. This avoids performing the
1956
- // transcript hash transformation for multiple hashes.
1957
- if (ssl->s3 && ssl->s3->used_hello_retry_request &&
1958
- ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) {
1965
+ static bool ext_pre_shared_key_add_clienthello(const SSL_HANDSHAKE *hs,
1966
+ CBB *out, bool *out_needs_binder,
1967
+ ssl_client_hello_type_t type) {
1968
+ const SSL *const ssl = hs->ssl;
1969
+ *out_needs_binder = false;
1970
+ if (!should_offer_psk(hs, type)) {
1959
1971
  return true;
1960
1972
  }
1961
1973
 
@@ -1983,7 +1995,7 @@ static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1983
1995
  return false;
1984
1996
  }
1985
1997
 
1986
- hs->needs_psk_binder = true;
1998
+ *out_needs_binder = true;
1987
1999
  return CBB_flush(out);
1988
2000
  }
1989
2001
 
@@ -2096,21 +2108,22 @@ bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2096
2108
  //
2097
2109
  // https://tools.ietf.org/html/rfc8446#section-4.2.9
2098
2110
 
2099
- static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2100
- CBB *out) {
2111
+ static bool ext_psk_key_exchange_modes_add_clienthello(
2112
+ const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible,
2113
+ ssl_client_hello_type_t type) {
2101
2114
  if (hs->max_version < TLS1_3_VERSION) {
2102
2115
  return true;
2103
2116
  }
2104
2117
 
2105
2118
  CBB contents, ke_modes;
2106
- if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2107
- !CBB_add_u16_length_prefixed(out, &contents) ||
2119
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_psk_key_exchange_modes) ||
2120
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
2108
2121
  !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2109
2122
  !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2110
2123
  return false;
2111
2124
  }
2112
2125
 
2113
- return CBB_flush(out);
2126
+ return CBB_flush(out_compressible);
2114
2127
  }
2115
2128
 
2116
2129
  static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
@@ -2140,23 +2153,10 @@ static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
2140
2153
  //
2141
2154
  // https://tools.ietf.org/html/rfc8446#section-4.2.10
2142
2155
 
2143
- // ssl_get_local_application_settings looks up the configured ALPS value for
2144
- // |protocol|. If found, it sets |*out_settings| to the value and returns true.
2145
- // Otherwise, it returns false.
2146
- static bool ssl_get_local_application_settings(
2147
- const SSL_HANDSHAKE *hs, Span<const uint8_t> *out_settings,
2148
- Span<const uint8_t> protocol) {
2149
- for (const ALPSConfig &config : hs->config->alps_configs) {
2150
- if (protocol == config.protocol) {
2151
- *out_settings = config.settings;
2152
- return true;
2153
- }
2154
- }
2155
- return false;
2156
- }
2157
-
2158
- static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2159
- SSL *const ssl = hs->ssl;
2156
+ static bool ext_early_data_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
2157
+ CBB *out_compressible,
2158
+ ssl_client_hello_type_t type) {
2159
+ const SSL *const ssl = hs->ssl;
2160
2160
  // The second ClientHello never offers early data, and we must have already
2161
2161
  // filled in |early_data_reason| by this point.
2162
2162
  if (ssl->s3->used_hello_retry_request) {
@@ -2164,56 +2164,20 @@ static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2164
2164
  return true;
2165
2165
  }
2166
2166
 
2167
- if (!ssl->enable_early_data) {
2168
- ssl->s3->early_data_reason = ssl_early_data_disabled;
2169
- return true;
2170
- }
2171
-
2172
- if (hs->max_version < TLS1_3_VERSION) {
2173
- // We discard inapplicable sessions, so this is redundant with the session
2174
- // checks below, but we check give a more useful reason.
2175
- ssl->s3->early_data_reason = ssl_early_data_protocol_version;
2176
- return true;
2177
- }
2178
-
2179
- if (ssl->session == nullptr) {
2180
- ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
2181
- return true;
2182
- }
2183
-
2184
- if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
2185
- ssl->session->ticket_max_early_data == 0) {
2186
- ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
2167
+ if (!hs->early_data_offered) {
2187
2168
  return true;
2188
2169
  }
2189
2170
 
2190
- if (!ssl->session->early_alpn.empty()) {
2191
- if (!ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
2192
- // Avoid reporting a confusing value in |SSL_get0_alpn_selected|.
2193
- ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
2194
- return true;
2195
- }
2196
-
2197
- // If the previous connection negotiated ALPS, only offer 0-RTT when the
2198
- // local are settings are consistent with what we'd offer for this
2199
- // connection.
2200
- if (ssl->session->has_application_settings) {
2201
- Span<const uint8_t> settings;
2202
- if (!ssl_get_local_application_settings(hs, &settings,
2203
- ssl->session->early_alpn) ||
2204
- settings != ssl->session->local_application_settings) {
2205
- ssl->s3->early_data_reason = ssl_early_data_alps_mismatch;
2206
- return true;
2207
- }
2208
- }
2209
- }
2210
-
2211
- // |early_data_reason| will be filled in later when the server responds.
2212
- hs->early_data_offered = true;
2213
-
2214
- if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2215
- !CBB_add_u16(out, 0) ||
2216
- !CBB_flush(out)) {
2171
+ // If offering ECH, the extension only applies to ClientHelloInner, but we
2172
+ // send the extension in both ClientHellos. This ensures that, if the server
2173
+ // handshakes with ClientHelloOuter, it can skip past early data. See
2174
+ // https://github.com/tlswg/draft-ietf-tls-esni/pull/415
2175
+ //
2176
+ // TODO(https://crbug.com/boringssl/275): Replace this with a reference to the
2177
+ // right section in the next draft.
2178
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_early_data) ||
2179
+ !CBB_add_u16(out_compressible, 0) ||
2180
+ !CBB_flush(out_compressible)) {
2217
2181
  return false;
2218
2182
  }
2219
2183
 
@@ -2294,43 +2258,33 @@ static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2294
2258
  //
2295
2259
  // https://tools.ietf.org/html/rfc8446#section-4.2.8
2296
2260
 
2297
- static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2261
+ bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id) {
2298
2262
  SSL *const ssl = hs->ssl;
2263
+ hs->key_shares[0].reset();
2264
+ hs->key_shares[1].reset();
2265
+ hs->key_share_bytes.Reset();
2266
+
2299
2267
  if (hs->max_version < TLS1_3_VERSION) {
2300
2268
  return true;
2301
2269
  }
2302
2270
 
2303
- CBB contents, kse_bytes;
2304
- if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2305
- !CBB_add_u16_length_prefixed(out, &contents) ||
2306
- !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2271
+ bssl::ScopedCBB cbb;
2272
+ if (!CBB_init(cbb.get(), 64)) {
2307
2273
  return false;
2308
2274
  }
2309
2275
 
2310
- uint16_t group_id = hs->retry_group;
2311
- uint16_t second_group_id = 0;
2312
- if (ssl->s3 && ssl->s3->used_hello_retry_request) {
2313
- // We received a HelloRetryRequest without a new curve, so there is no new
2314
- // share to append. Leave |hs->key_share| as-is.
2315
- if (group_id == 0 &&
2316
- !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2317
- hs->key_share_bytes.size())) {
2318
- return false;
2319
- }
2320
- hs->key_share_bytes.Reset();
2321
- if (group_id == 0) {
2322
- return CBB_flush(out);
2323
- }
2324
- } else {
2325
- // Add a fake group. See draft-davidben-tls-grease-01.
2326
- if (ssl->ctx->grease_enabled &&
2327
- (!CBB_add_u16(&kse_bytes,
2328
- ssl_get_grease_value(hs, ssl_grease_group)) ||
2329
- !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2330
- !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2276
+ if (override_group_id == 0 && ssl->ctx->grease_enabled) {
2277
+ // Add a fake group. See RFC 8701.
2278
+ if (!CBB_add_u16(cbb.get(), ssl_get_grease_value(hs, ssl_grease_group)) ||
2279
+ !CBB_add_u16(cbb.get(), 1 /* length */) ||
2280
+ !CBB_add_u8(cbb.get(), 0 /* one byte key share */)) {
2331
2281
  return false;
2332
2282
  }
2283
+ }
2333
2284
 
2285
+ uint16_t group_id = override_group_id;
2286
+ uint16_t second_group_id = 0;
2287
+ if (override_group_id == 0) {
2334
2288
  // Predict the most preferred group.
2335
2289
  Span<const uint16_t> groups = tls1_get_grouplist(hs);
2336
2290
  if (groups.empty()) {
@@ -2350,34 +2304,45 @@ static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2350
2304
 
2351
2305
  CBB key_exchange;
2352
2306
  hs->key_shares[0] = SSLKeyShare::Create(group_id);
2353
- if (!hs->key_shares[0] ||
2354
- !CBB_add_u16(&kse_bytes, group_id) ||
2355
- !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2356
- !hs->key_shares[0]->Offer(&key_exchange) ||
2357
- !CBB_flush(&kse_bytes)) {
2307
+ if (!hs->key_shares[0] || //
2308
+ !CBB_add_u16(cbb.get(), group_id) ||
2309
+ !CBB_add_u16_length_prefixed(cbb.get(), &key_exchange) ||
2310
+ !hs->key_shares[0]->Offer(&key_exchange)) {
2358
2311
  return false;
2359
2312
  }
2360
2313
 
2361
2314
  if (second_group_id != 0) {
2362
2315
  hs->key_shares[1] = SSLKeyShare::Create(second_group_id);
2363
- if (!hs->key_shares[1] ||
2364
- !CBB_add_u16(&kse_bytes, second_group_id) ||
2365
- !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2366
- !hs->key_shares[1]->Offer(&key_exchange) ||
2367
- !CBB_flush(&kse_bytes)) {
2316
+ if (!hs->key_shares[1] || //
2317
+ !CBB_add_u16(cbb.get(), second_group_id) ||
2318
+ !CBB_add_u16_length_prefixed(cbb.get(), &key_exchange) ||
2319
+ !hs->key_shares[1]->Offer(&key_exchange)) {
2368
2320
  return false;
2369
2321
  }
2370
2322
  }
2371
2323
 
2372
- // Save the contents of the extension to repeat it in the second
2373
- // ClientHello.
2374
- if (ssl->s3 && !ssl->s3->used_hello_retry_request &&
2375
- !hs->key_share_bytes.CopyFrom(
2376
- MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) {
2324
+ return CBBFinishArray(cbb.get(), &hs->key_share_bytes);
2325
+ }
2326
+
2327
+ static bool ext_key_share_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
2328
+ CBB *out_compressible,
2329
+ ssl_client_hello_type_t type) {
2330
+ if (hs->max_version < TLS1_3_VERSION) {
2331
+ return true;
2332
+ }
2333
+
2334
+ assert(!hs->key_share_bytes.empty());
2335
+ CBB contents, kse_bytes;
2336
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_key_share) ||
2337
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
2338
+ !CBB_add_u16_length_prefixed(&contents, &kse_bytes) ||
2339
+ !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2340
+ hs->key_share_bytes.size()) ||
2341
+ !CBB_flush(out_compressible)) {
2377
2342
  return false;
2378
2343
  }
2379
2344
 
2380
- return CBB_flush(out);
2345
+ return true;
2381
2346
  }
2382
2347
 
2383
2348
  bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
@@ -2415,25 +2380,29 @@ bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
2415
2380
  }
2416
2381
 
2417
2382
  bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2418
- Array<uint8_t> *out_secret,
2419
- uint8_t *out_alert, CBS *contents) {
2420
- uint16_t group_id;
2421
- CBS key_shares;
2422
- if (!tls1_get_shared_group(hs, &group_id)) {
2423
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
2424
- *out_alert = SSL_AD_HANDSHAKE_FAILURE;
2383
+ Span<const uint8_t> *out_peer_key,
2384
+ uint8_t *out_alert,
2385
+ const SSL_CLIENT_HELLO *client_hello) {
2386
+ // We only support connections that include an ECDHE key exchange.
2387
+ CBS contents;
2388
+ if (!ssl_client_hello_get_extension(client_hello, &contents,
2389
+ TLSEXT_TYPE_key_share)) {
2390
+ OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE);
2391
+ *out_alert = SSL_AD_MISSING_EXTENSION;
2425
2392
  return false;
2426
2393
  }
2427
2394
 
2428
- if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
2429
- CBS_len(contents) != 0) {
2395
+ CBS key_shares;
2396
+ if (!CBS_get_u16_length_prefixed(&contents, &key_shares) ||
2397
+ CBS_len(&contents) != 0) {
2430
2398
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2431
2399
  return false;
2432
2400
  }
2433
2401
 
2434
2402
  // Find the corresponding key share.
2403
+ const uint16_t group_id = hs->new_session->group_id;
2435
2404
  CBS peer_key;
2436
- CBS_init(&peer_key, NULL, 0);
2405
+ CBS_init(&peer_key, nullptr, 0);
2437
2406
  while (CBS_len(&key_shares) > 0) {
2438
2407
  uint16_t id;
2439
2408
  CBS peer_key_tmp;
@@ -2456,47 +2425,24 @@ bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found,
2456
2425
  }
2457
2426
  }
2458
2427
 
2459
- if (CBS_len(&peer_key) == 0) {
2460
- *out_found = false;
2461
- out_secret->Reset();
2462
- return true;
2428
+ if (out_peer_key != nullptr) {
2429
+ *out_peer_key = peer_key;
2463
2430
  }
2464
-
2465
- // Compute the DH secret.
2466
- Array<uint8_t> secret;
2467
- ScopedCBB public_key;
2468
- UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
2469
- if (!key_share ||
2470
- !CBB_init(public_key.get(), 32) ||
2471
- !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) ||
2472
- !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) {
2473
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2474
- return false;
2475
- }
2476
-
2477
- *out_secret = std::move(secret);
2478
- *out_found = true;
2431
+ *out_found = CBS_len(&peer_key) != 0;
2479
2432
  return true;
2480
2433
  }
2481
2434
 
2482
- bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out,
2483
- bool dry_run) {
2484
- uint16_t group_id;
2435
+ bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2485
2436
  CBB kse_bytes, public_key;
2486
- if (!tls1_get_shared_group(hs, &group_id) ||
2487
- !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2437
+ if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2488
2438
  !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2489
- !CBB_add_u16(&kse_bytes, group_id) ||
2439
+ !CBB_add_u16(&kse_bytes, hs->new_session->group_id) ||
2490
2440
  !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2491
2441
  !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(),
2492
2442
  hs->ecdh_public_key.size()) ||
2493
2443
  !CBB_flush(out)) {
2494
2444
  return false;
2495
2445
  }
2496
- if (!dry_run) {
2497
- hs->ecdh_public_key.Reset();
2498
- hs->new_session->group_id = group_id;
2499
- }
2500
2446
  return true;
2501
2447
  }
2502
2448
 
@@ -2505,12 +2451,20 @@ bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out,
2505
2451
  //
2506
2452
  // https://tools.ietf.org/html/rfc8446#section-4.2.1
2507
2453
 
2508
- static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2509
- SSL *const ssl = hs->ssl;
2454
+ static bool ext_supported_versions_add_clienthello(
2455
+ const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible,
2456
+ ssl_client_hello_type_t type) {
2457
+ const SSL *const ssl = hs->ssl;
2510
2458
  if (hs->max_version <= TLS1_2_VERSION) {
2511
2459
  return true;
2512
2460
  }
2513
2461
 
2462
+ // supported_versions is compressible in ECH if ClientHelloOuter already
2463
+ // requires TLS 1.3. Otherwise the extensions differ in the older versions.
2464
+ if (hs->min_version >= TLS1_3_VERSION) {
2465
+ out = out_compressible;
2466
+ }
2467
+
2514
2468
  CBB contents, versions;
2515
2469
  if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2516
2470
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -2518,13 +2472,16 @@ static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out)
2518
2472
  return false;
2519
2473
  }
2520
2474
 
2521
- // Add a fake version. See draft-davidben-tls-grease-01.
2475
+ // Add a fake version. See RFC 8701.
2522
2476
  if (ssl->ctx->grease_enabled &&
2523
2477
  !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
2524
2478
  return false;
2525
2479
  }
2526
2480
 
2527
- if (!ssl_add_supported_versions(hs, &versions) ||
2481
+ // Encrypted ClientHellos requires TLS 1.3 or later.
2482
+ uint16_t extra_min_version =
2483
+ type == ssl_client_hello_inner ? TLS1_3_VERSION : 0;
2484
+ if (!ssl_add_supported_versions(hs, &versions, extra_min_version) ||
2528
2485
  !CBB_flush(out)) {
2529
2486
  return false;
2530
2487
  }
@@ -2537,22 +2494,22 @@ static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out)
2537
2494
  //
2538
2495
  // https://tools.ietf.org/html/rfc8446#section-4.2.2
2539
2496
 
2540
- static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2497
+ static bool ext_cookie_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
2498
+ CBB *out_compressible,
2499
+ ssl_client_hello_type_t type) {
2541
2500
  if (hs->cookie.empty()) {
2542
2501
  return true;
2543
2502
  }
2544
2503
 
2545
2504
  CBB contents, cookie;
2546
- if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2547
- !CBB_add_u16_length_prefixed(out, &contents) ||
2505
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_cookie) ||
2506
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
2548
2507
  !CBB_add_u16_length_prefixed(&contents, &cookie) ||
2549
2508
  !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
2550
- !CBB_flush(out)) {
2509
+ !CBB_flush(out_compressible)) {
2551
2510
  return false;
2552
2511
  }
2553
2512
 
2554
- // The cookie is no longer needed in memory.
2555
- hs->cookie.Reset();
2556
2513
  return true;
2557
2514
  }
2558
2515
 
@@ -2562,16 +2519,19 @@ static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2562
2519
  // https://tools.ietf.org/html/rfc4492#section-5.1.1
2563
2520
  // https://tools.ietf.org/html/rfc8446#section-4.2.7
2564
2521
 
2565
- static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2566
- SSL *const ssl = hs->ssl;
2522
+ static bool ext_supported_groups_add_clienthello(const SSL_HANDSHAKE *hs,
2523
+ CBB *out,
2524
+ CBB *out_compressible,
2525
+ ssl_client_hello_type_t type) {
2526
+ const SSL *const ssl = hs->ssl;
2567
2527
  CBB contents, groups_bytes;
2568
- if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
2569
- !CBB_add_u16_length_prefixed(out, &contents) ||
2528
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_supported_groups) ||
2529
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
2570
2530
  !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
2571
2531
  return false;
2572
2532
  }
2573
2533
 
2574
- // Add a fake group. See draft-davidben-tls-grease-01.
2534
+ // Add a fake group. See RFC 8701.
2575
2535
  if (ssl->ctx->grease_enabled &&
2576
2536
  !CBB_add_u16(&groups_bytes,
2577
2537
  ssl_get_grease_value(hs, ssl_grease_group))) {
@@ -2588,7 +2548,7 @@ static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2588
2548
  }
2589
2549
  }
2590
2550
 
2591
- return CBB_flush(out);
2551
+ return CBB_flush(out_compressible);
2592
2552
  }
2593
2553
 
2594
2554
  static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
@@ -2640,158 +2600,11 @@ static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2640
2600
  return true;
2641
2601
  }
2642
2602
 
2643
- // Token Binding
2644
- //
2645
- // https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
2646
-
2647
- // The Token Binding version number currently matches the draft number of
2648
- // draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
2649
- // Since there are no wire changes to the protocol from draft 13 through the
2650
- // current draft (16), this implementation supports all versions in that range.
2651
- static uint16_t kTokenBindingMaxVersion = 16;
2652
- static uint16_t kTokenBindingMinVersion = 13;
2653
-
2654
- static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2655
- SSL *const ssl = hs->ssl;
2656
- if (hs->config->token_binding_params.empty() || SSL_is_dtls(ssl)) {
2657
- return true;
2658
- }
2659
-
2660
- CBB contents, params;
2661
- if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2662
- !CBB_add_u16_length_prefixed(out, &contents) ||
2663
- !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
2664
- !CBB_add_u8_length_prefixed(&contents, &params) ||
2665
- !CBB_add_bytes(&params, hs->config->token_binding_params.data(),
2666
- hs->config->token_binding_params.size()) ||
2667
- !CBB_flush(out)) {
2668
- return false;
2669
- }
2670
-
2671
- return true;
2672
- }
2673
-
2674
- static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
2675
- uint8_t *out_alert,
2676
- CBS *contents) {
2677
- SSL *const ssl = hs->ssl;
2678
- if (contents == nullptr) {
2679
- return true;
2680
- }
2681
-
2682
- CBS params_list;
2683
- uint16_t version;
2684
- uint8_t param;
2685
- if (!CBS_get_u16(contents, &version) ||
2686
- !CBS_get_u8_length_prefixed(contents, &params_list) ||
2687
- !CBS_get_u8(&params_list, &param) ||
2688
- CBS_len(&params_list) > 0 ||
2689
- CBS_len(contents) > 0) {
2690
- *out_alert = SSL_AD_DECODE_ERROR;
2691
- return false;
2692
- }
2693
-
2694
- // The server-negotiated version must be less than or equal to our version.
2695
- if (version > kTokenBindingMaxVersion) {
2696
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2697
- return false;
2698
- }
2699
-
2700
- // If the server-selected version is less than what we support, then Token
2701
- // Binding wasn't negotiated (but the extension was parsed successfully).
2702
- if (version < kTokenBindingMinVersion) {
2703
- return true;
2704
- }
2705
-
2706
- for (uint8_t config_param : hs->config->token_binding_params) {
2707
- if (param == config_param) {
2708
- ssl->s3->negotiated_token_binding_param = param;
2709
- ssl->s3->token_binding_negotiated = true;
2710
- return true;
2711
- }
2712
- }
2713
-
2714
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2715
- return false;
2716
- }
2717
-
2718
- // select_tb_param looks for the first token binding param in
2719
- // |hs->ssl->token_binding_params| that is also in |params| and puts it in
2720
- // |hs->ssl->negotiated_token_binding_param|. It returns true if a token binding
2721
- // param is found, and false otherwise.
2722
- static bool select_tb_param(SSL_HANDSHAKE *hs,
2723
- Span<const uint8_t> peer_params) {
2724
- for (uint8_t tb_param : hs->config->token_binding_params) {
2725
- for (uint8_t peer_param : peer_params) {
2726
- if (tb_param == peer_param) {
2727
- hs->ssl->s3->negotiated_token_binding_param = tb_param;
2728
- return true;
2729
- }
2730
- }
2731
- }
2732
- return false;
2733
- }
2734
-
2735
- static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
2736
- uint8_t *out_alert,
2737
- CBS *contents) {
2738
- SSL *const ssl = hs->ssl;
2739
- if (contents == nullptr || hs->config->token_binding_params.empty()) {
2740
- return true;
2741
- }
2742
-
2743
- CBS params;
2744
- uint16_t version;
2745
- if (!CBS_get_u16(contents, &version) ||
2746
- !CBS_get_u8_length_prefixed(contents, &params) ||
2747
- CBS_len(&params) == 0 ||
2748
- CBS_len(contents) > 0) {
2749
- *out_alert = SSL_AD_DECODE_ERROR;
2750
- return false;
2751
- }
2752
-
2753
- // If the client-selected version is less than what we support, then Token
2754
- // Binding wasn't negotiated (but the extension was parsed successfully).
2755
- if (version < kTokenBindingMinVersion) {
2756
- return true;
2757
- }
2758
-
2759
- // If the client-selected version is higher than we support, use our max
2760
- // version. Otherwise, use the client's version.
2761
- hs->negotiated_token_binding_version =
2762
- std::min(version, kTokenBindingMaxVersion);
2763
- if (!select_tb_param(hs, params)) {
2764
- return true;
2765
- }
2766
-
2767
- ssl->s3->token_binding_negotiated = true;
2768
- return true;
2769
- }
2770
-
2771
- static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2772
- SSL *const ssl = hs->ssl;
2773
-
2774
- if (!ssl->s3->token_binding_negotiated) {
2775
- return true;
2776
- }
2777
-
2778
- CBB contents, params;
2779
- if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2780
- !CBB_add_u16_length_prefixed(out, &contents) ||
2781
- !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
2782
- !CBB_add_u8_length_prefixed(&contents, &params) ||
2783
- !CBB_add_u8(&params, ssl->s3->negotiated_token_binding_param) ||
2784
- !CBB_flush(out)) {
2785
- return false;
2786
- }
2787
-
2788
- return true;
2789
- }
2790
2603
 
2791
2604
  // QUIC Transport Parameters
2792
2605
 
2793
2606
  static bool ext_quic_transport_params_add_clienthello_impl(
2794
- SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) {
2607
+ const SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) {
2795
2608
  if (hs->config->quic_transport_params.empty() && !hs->ssl->quic_method) {
2796
2609
  return true;
2797
2610
  }
@@ -2808,7 +2621,7 @@ static bool ext_quic_transport_params_add_clienthello_impl(
2808
2621
  return true;
2809
2622
  }
2810
2623
 
2811
- uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters_standard;
2624
+ uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters;
2812
2625
  if (hs->config->quic_use_legacy_codepoint) {
2813
2626
  extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy;
2814
2627
  }
@@ -2824,16 +2637,18 @@ static bool ext_quic_transport_params_add_clienthello_impl(
2824
2637
  return true;
2825
2638
  }
2826
2639
 
2827
- static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2828
- CBB *out) {
2640
+ static bool ext_quic_transport_params_add_clienthello(
2641
+ const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible,
2642
+ ssl_client_hello_type_t type) {
2829
2643
  return ext_quic_transport_params_add_clienthello_impl(
2830
- hs, out, /*use_legacy_codepoint=*/false);
2644
+ hs, out_compressible, /*use_legacy_codepoint=*/false);
2831
2645
  }
2832
2646
 
2833
- static bool ext_quic_transport_params_add_clienthello_legacy(SSL_HANDSHAKE *hs,
2834
- CBB *out) {
2647
+ static bool ext_quic_transport_params_add_clienthello_legacy(
2648
+ const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible,
2649
+ ssl_client_hello_type_t type) {
2835
2650
  return ext_quic_transport_params_add_clienthello_impl(
2836
- hs, out, /*use_legacy_codepoint=*/true);
2651
+ hs, out_compressible, /*use_legacy_codepoint=*/true);
2837
2652
  }
2838
2653
 
2839
2654
  static bool ext_quic_transport_params_parse_serverhello_impl(
@@ -2944,7 +2759,7 @@ static bool ext_quic_transport_params_add_serverhello_impl(
2944
2759
  return true;
2945
2760
  }
2946
2761
 
2947
- uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters_standard;
2762
+ uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters;
2948
2763
  if (hs->config->quic_use_legacy_codepoint) {
2949
2764
  extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy;
2950
2765
  }
@@ -2977,8 +2792,9 @@ static bool ext_quic_transport_params_add_serverhello_legacy(SSL_HANDSHAKE *hs,
2977
2792
  //
2978
2793
  // https://tools.ietf.org/html/draft-ietf-tls-subcerts
2979
2794
 
2980
- static bool ext_delegated_credential_add_clienthello(SSL_HANDSHAKE *hs,
2981
- CBB *out) {
2795
+ static bool ext_delegated_credential_add_clienthello(
2796
+ const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible,
2797
+ ssl_client_hello_type_t type) {
2982
2798
  return true;
2983
2799
  }
2984
2800
 
@@ -3007,7 +2823,9 @@ static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs,
3007
2823
 
3008
2824
  // Certificate compression
3009
2825
 
3010
- static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2826
+ static bool cert_compression_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
2827
+ CBB *out_compressible,
2828
+ ssl_client_hello_type_t type) {
3011
2829
  bool first = true;
3012
2830
  CBB contents, algs;
3013
2831
 
@@ -3016,9 +2834,10 @@ static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3016
2834
  continue;
3017
2835
  }
3018
2836
 
3019
- if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
3020
- !CBB_add_u16_length_prefixed(out, &contents) ||
3021
- !CBB_add_u8_length_prefixed(&contents, &algs))) {
2837
+ if (first &&
2838
+ (!CBB_add_u16(out_compressible, TLSEXT_TYPE_cert_compression) ||
2839
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
2840
+ !CBB_add_u8_length_prefixed(&contents, &algs))) {
3022
2841
  return false;
3023
2842
  }
3024
2843
  first = false;
@@ -3027,7 +2846,7 @@ static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3027
2846
  }
3028
2847
  }
3029
2848
 
3030
- return first || CBB_flush(out);
2849
+ return first || CBB_flush(out_compressible);
3031
2850
  }
3032
2851
 
3033
2852
  static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
@@ -3113,8 +2932,22 @@ static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
3113
2932
  //
3114
2933
  // https://tools.ietf.org/html/draft-vvv-tls-alps-01
3115
2934
 
3116
- static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3117
- SSL *const ssl = hs->ssl;
2935
+ bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs,
2936
+ Span<const uint8_t> *out_settings,
2937
+ Span<const uint8_t> protocol) {
2938
+ for (const ALPSConfig &config : hs->config->alps_configs) {
2939
+ if (protocol == config.protocol) {
2940
+ *out_settings = config.settings;
2941
+ return true;
2942
+ }
2943
+ }
2944
+ return false;
2945
+ }
2946
+
2947
+ static bool ext_alps_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out,
2948
+ CBB *out_compressible,
2949
+ ssl_client_hello_type_t type) {
2950
+ const SSL *const ssl = hs->ssl;
3118
2951
  if (// ALPS requires TLS 1.3.
3119
2952
  hs->max_version < TLS1_3_VERSION ||
3120
2953
  // Do not offer ALPS without ALPN.
@@ -3127,8 +2960,8 @@ static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3127
2960
  }
3128
2961
 
3129
2962
  CBB contents, proto_list, proto;
3130
- if (!CBB_add_u16(out, TLSEXT_TYPE_application_settings) ||
3131
- !CBB_add_u16_length_prefixed(out, &contents) ||
2963
+ if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_application_settings) ||
2964
+ !CBB_add_u16_length_prefixed(out_compressible, &contents) ||
3132
2965
  !CBB_add_u16_length_prefixed(&contents, &proto_list)) {
3133
2966
  return false;
3134
2967
  }
@@ -3141,7 +2974,7 @@ static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3141
2974
  }
3142
2975
  }
3143
2976
 
3144
- return CBB_flush(out);
2977
+ return CBB_flush(out_compressible);
3145
2978
  }
3146
2979
 
3147
2980
  static bool ext_alps_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
@@ -3252,7 +3085,6 @@ bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert,
3252
3085
  static const struct tls_extension kExtensions[] = {
3253
3086
  {
3254
3087
  TLSEXT_TYPE_server_name,
3255
- NULL,
3256
3088
  ext_sni_add_clienthello,
3257
3089
  ext_sni_parse_serverhello,
3258
3090
  ext_sni_parse_clienthello,
@@ -3260,15 +3092,13 @@ static const struct tls_extension kExtensions[] = {
3260
3092
  },
3261
3093
  {
3262
3094
  TLSEXT_TYPE_encrypted_client_hello,
3263
- NULL,
3264
3095
  ext_ech_add_clienthello,
3265
3096
  ext_ech_parse_serverhello,
3266
3097
  ext_ech_parse_clienthello,
3267
- dont_add_serverhello,
3098
+ ext_ech_add_serverhello,
3268
3099
  },
3269
3100
  {
3270
3101
  TLSEXT_TYPE_ech_is_inner,
3271
- NULL,
3272
3102
  ext_ech_is_inner_add_clienthello,
3273
3103
  forbid_parse_serverhello,
3274
3104
  ext_ech_is_inner_parse_clienthello,
@@ -3276,7 +3106,6 @@ static const struct tls_extension kExtensions[] = {
3276
3106
  },
3277
3107
  {
3278
3108
  TLSEXT_TYPE_extended_master_secret,
3279
- NULL,
3280
3109
  ext_ems_add_clienthello,
3281
3110
  ext_ems_parse_serverhello,
3282
3111
  ext_ems_parse_clienthello,
@@ -3284,7 +3113,6 @@ static const struct tls_extension kExtensions[] = {
3284
3113
  },
3285
3114
  {
3286
3115
  TLSEXT_TYPE_renegotiate,
3287
- NULL,
3288
3116
  ext_ri_add_clienthello,
3289
3117
  ext_ri_parse_serverhello,
3290
3118
  ext_ri_parse_clienthello,
@@ -3292,7 +3120,6 @@ static const struct tls_extension kExtensions[] = {
3292
3120
  },
3293
3121
  {
3294
3122
  TLSEXT_TYPE_supported_groups,
3295
- NULL,
3296
3123
  ext_supported_groups_add_clienthello,
3297
3124
  ext_supported_groups_parse_serverhello,
3298
3125
  ext_supported_groups_parse_clienthello,
@@ -3300,7 +3127,6 @@ static const struct tls_extension kExtensions[] = {
3300
3127
  },
3301
3128
  {
3302
3129
  TLSEXT_TYPE_ec_point_formats,
3303
- NULL,
3304
3130
  ext_ec_point_add_clienthello,
3305
3131
  ext_ec_point_parse_serverhello,
3306
3132
  ext_ec_point_parse_clienthello,
@@ -3308,7 +3134,6 @@ static const struct tls_extension kExtensions[] = {
3308
3134
  },
3309
3135
  {
3310
3136
  TLSEXT_TYPE_session_ticket,
3311
- NULL,
3312
3137
  ext_ticket_add_clienthello,
3313
3138
  ext_ticket_parse_serverhello,
3314
3139
  // Ticket extension client parsing is handled in ssl_session.c
@@ -3317,7 +3142,6 @@ static const struct tls_extension kExtensions[] = {
3317
3142
  },
3318
3143
  {
3319
3144
  TLSEXT_TYPE_application_layer_protocol_negotiation,
3320
- NULL,
3321
3145
  ext_alpn_add_clienthello,
3322
3146
  ext_alpn_parse_serverhello,
3323
3147
  // ALPN is negotiated late in |ssl_negotiate_alpn|.
@@ -3326,7 +3150,6 @@ static const struct tls_extension kExtensions[] = {
3326
3150
  },
3327
3151
  {
3328
3152
  TLSEXT_TYPE_status_request,
3329
- NULL,
3330
3153
  ext_ocsp_add_clienthello,
3331
3154
  ext_ocsp_parse_serverhello,
3332
3155
  ext_ocsp_parse_clienthello,
@@ -3334,7 +3157,6 @@ static const struct tls_extension kExtensions[] = {
3334
3157
  },
3335
3158
  {
3336
3159
  TLSEXT_TYPE_signature_algorithms,
3337
- NULL,
3338
3160
  ext_sigalgs_add_clienthello,
3339
3161
  forbid_parse_serverhello,
3340
3162
  ext_sigalgs_parse_clienthello,
@@ -3342,7 +3164,6 @@ static const struct tls_extension kExtensions[] = {
3342
3164
  },
3343
3165
  {
3344
3166
  TLSEXT_TYPE_next_proto_neg,
3345
- NULL,
3346
3167
  ext_npn_add_clienthello,
3347
3168
  ext_npn_parse_serverhello,
3348
3169
  ext_npn_parse_clienthello,
@@ -3350,7 +3171,6 @@ static const struct tls_extension kExtensions[] = {
3350
3171
  },
3351
3172
  {
3352
3173
  TLSEXT_TYPE_certificate_timestamp,
3353
- NULL,
3354
3174
  ext_sct_add_clienthello,
3355
3175
  ext_sct_parse_serverhello,
3356
3176
  ext_sct_parse_clienthello,
@@ -3358,7 +3178,6 @@ static const struct tls_extension kExtensions[] = {
3358
3178
  },
3359
3179
  {
3360
3180
  TLSEXT_TYPE_channel_id,
3361
- ext_channel_id_init,
3362
3181
  ext_channel_id_add_clienthello,
3363
3182
  ext_channel_id_parse_serverhello,
3364
3183
  ext_channel_id_parse_clienthello,
@@ -3366,7 +3185,6 @@ static const struct tls_extension kExtensions[] = {
3366
3185
  },
3367
3186
  {
3368
3187
  TLSEXT_TYPE_srtp,
3369
- ext_srtp_init,
3370
3188
  ext_srtp_add_clienthello,
3371
3189
  ext_srtp_parse_serverhello,
3372
3190
  ext_srtp_parse_clienthello,
@@ -3374,7 +3192,6 @@ static const struct tls_extension kExtensions[] = {
3374
3192
  },
3375
3193
  {
3376
3194
  TLSEXT_TYPE_key_share,
3377
- NULL,
3378
3195
  ext_key_share_add_clienthello,
3379
3196
  forbid_parse_serverhello,
3380
3197
  ignore_parse_clienthello,
@@ -3382,7 +3199,6 @@ static const struct tls_extension kExtensions[] = {
3382
3199
  },
3383
3200
  {
3384
3201
  TLSEXT_TYPE_psk_key_exchange_modes,
3385
- NULL,
3386
3202
  ext_psk_key_exchange_modes_add_clienthello,
3387
3203
  forbid_parse_serverhello,
3388
3204
  ext_psk_key_exchange_modes_parse_clienthello,
@@ -3390,7 +3206,6 @@ static const struct tls_extension kExtensions[] = {
3390
3206
  },
3391
3207
  {
3392
3208
  TLSEXT_TYPE_early_data,
3393
- NULL,
3394
3209
  ext_early_data_add_clienthello,
3395
3210
  ext_early_data_parse_serverhello,
3396
3211
  ext_early_data_parse_clienthello,
@@ -3398,7 +3213,6 @@ static const struct tls_extension kExtensions[] = {
3398
3213
  },
3399
3214
  {
3400
3215
  TLSEXT_TYPE_supported_versions,
3401
- NULL,
3402
3216
  ext_supported_versions_add_clienthello,
3403
3217
  forbid_parse_serverhello,
3404
3218
  ignore_parse_clienthello,
@@ -3406,15 +3220,13 @@ static const struct tls_extension kExtensions[] = {
3406
3220
  },
3407
3221
  {
3408
3222
  TLSEXT_TYPE_cookie,
3409
- NULL,
3410
3223
  ext_cookie_add_clienthello,
3411
3224
  forbid_parse_serverhello,
3412
3225
  ignore_parse_clienthello,
3413
3226
  dont_add_serverhello,
3414
3227
  },
3415
3228
  {
3416
- TLSEXT_TYPE_quic_transport_parameters_standard,
3417
- NULL,
3229
+ TLSEXT_TYPE_quic_transport_parameters,
3418
3230
  ext_quic_transport_params_add_clienthello,
3419
3231
  ext_quic_transport_params_parse_serverhello,
3420
3232
  ext_quic_transport_params_parse_clienthello,
@@ -3422,23 +3234,13 @@ static const struct tls_extension kExtensions[] = {
3422
3234
  },
3423
3235
  {
3424
3236
  TLSEXT_TYPE_quic_transport_parameters_legacy,
3425
- NULL,
3426
3237
  ext_quic_transport_params_add_clienthello_legacy,
3427
3238
  ext_quic_transport_params_parse_serverhello_legacy,
3428
3239
  ext_quic_transport_params_parse_clienthello_legacy,
3429
3240
  ext_quic_transport_params_add_serverhello_legacy,
3430
3241
  },
3431
- {
3432
- TLSEXT_TYPE_token_binding,
3433
- NULL,
3434
- ext_token_binding_add_clienthello,
3435
- ext_token_binding_parse_serverhello,
3436
- ext_token_binding_parse_clienthello,
3437
- ext_token_binding_add_serverhello,
3438
- },
3439
3242
  {
3440
3243
  TLSEXT_TYPE_cert_compression,
3441
- NULL,
3442
3244
  cert_compression_add_clienthello,
3443
3245
  cert_compression_parse_serverhello,
3444
3246
  cert_compression_parse_clienthello,
@@ -3446,7 +3248,6 @@ static const struct tls_extension kExtensions[] = {
3446
3248
  },
3447
3249
  {
3448
3250
  TLSEXT_TYPE_delegated_credential,
3449
- NULL,
3450
3251
  ext_delegated_credential_add_clienthello,
3451
3252
  forbid_parse_serverhello,
3452
3253
  ext_delegated_credential_parse_clienthello,
@@ -3454,7 +3255,6 @@ static const struct tls_extension kExtensions[] = {
3454
3255
  },
3455
3256
  {
3456
3257
  TLSEXT_TYPE_application_settings,
3457
- NULL,
3458
3258
  ext_alps_add_clienthello,
3459
3259
  ext_alps_parse_serverhello,
3460
3260
  // ALPS is negotiated late in |ssl_negotiate_alpn|.
@@ -3472,6 +3272,30 @@ static_assert(kNumExtensions <=
3472
3272
  sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
3473
3273
  "too many extensions for received bitset");
3474
3274
 
3275
+ bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs) {
3276
+ if (!hs->config->permute_extensions) {
3277
+ return true;
3278
+ }
3279
+
3280
+ static_assert(kNumExtensions <= UINT8_MAX,
3281
+ "extensions_permutation type is too small");
3282
+ uint32_t seeds[kNumExtensions - 1];
3283
+ Array<uint8_t> permutation;
3284
+ if (!RAND_bytes(reinterpret_cast<uint8_t *>(seeds), sizeof(seeds)) ||
3285
+ !permutation.Init(kNumExtensions)) {
3286
+ return false;
3287
+ }
3288
+ for (size_t i = 0; i < kNumExtensions; i++) {
3289
+ permutation[i] = i;
3290
+ }
3291
+ for (size_t i = kNumExtensions - 1; i > 0; i--) {
3292
+ // Set element |i| to a randomly-selected element 0 <= j <= i.
3293
+ std::swap(permutation[i], permutation[seeds[i - 1] % (i + 1)]);
3294
+ }
3295
+ hs->extension_permutation = std::move(permutation);
3296
+ return true;
3297
+ }
3298
+
3475
3299
  static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3476
3300
  uint16_t value) {
3477
3301
  unsigned i;
@@ -3485,47 +3309,205 @@ static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3485
3309
  return NULL;
3486
3310
  }
3487
3311
 
3488
- bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3489
- size_t header_len) {
3490
- SSL *const ssl = hs->ssl;
3491
- CBB extensions;
3492
- if (!CBB_add_u16_length_prefixed(out, &extensions)) {
3312
+ static bool add_padding_extension(CBB *cbb, uint16_t ext, size_t len) {
3313
+ CBB child;
3314
+ uint8_t *ptr;
3315
+ if (!CBB_add_u16(cbb, ext) || //
3316
+ !CBB_add_u16_length_prefixed(cbb, &child) ||
3317
+ !CBB_add_space(&child, &ptr, len)) {
3493
3318
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3494
3319
  return false;
3495
3320
  }
3321
+ OPENSSL_memset(ptr, 0, len);
3322
+ return CBB_flush(cbb);
3323
+ }
3496
3324
 
3497
- // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS
3498
- // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is
3499
- // important to reset this value.
3500
- hs->extensions.sent = 0;
3501
-
3502
- for (size_t i = 0; i < kNumExtensions; i++) {
3503
- if (kExtensions[i].init != NULL) {
3504
- kExtensions[i].init(hs);
3505
- }
3325
+ static bool ssl_add_clienthello_tlsext_inner(SSL_HANDSHAKE *hs, CBB *out,
3326
+ CBB *out_encoded,
3327
+ bool *out_needs_psk_binder) {
3328
+ // When writing ClientHelloInner, we construct the real and encoded
3329
+ // ClientHellos concurrently, to handle compression. Uncompressed extensions
3330
+ // are written to |extensions| and copied to |extensions_encoded|. Compressed
3331
+ // extensions are buffered in |compressed| and written to the end. (ECH can
3332
+ // only compress continguous extensions.)
3333
+ SSL *const ssl = hs->ssl;
3334
+ bssl::ScopedCBB compressed, outer_extensions;
3335
+ CBB extensions, extensions_encoded;
3336
+ if (!CBB_add_u16_length_prefixed(out, &extensions) ||
3337
+ !CBB_add_u16_length_prefixed(out_encoded, &extensions_encoded) ||
3338
+ !CBB_init(compressed.get(), 64) ||
3339
+ !CBB_init(outer_extensions.get(), 64)) {
3340
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3341
+ return false;
3506
3342
  }
3507
3343
 
3508
- uint16_t grease_ext1 = 0;
3344
+ hs->inner_extensions_sent = 0;
3345
+
3509
3346
  if (ssl->ctx->grease_enabled) {
3510
- // Add a fake empty extension. See draft-davidben-tls-grease-01.
3511
- grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
3512
- if (!CBB_add_u16(&extensions, grease_ext1) ||
3513
- !CBB_add_u16(&extensions, 0 /* zero length */)) {
3514
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3347
+ // Add a fake empty extension. See RFC 8701. This always matches
3348
+ // |ssl_add_clienthello_tlsext|, so compress it.
3349
+ uint16_t grease_ext = ssl_get_grease_value(hs, ssl_grease_extension1);
3350
+ if (!add_padding_extension(compressed.get(), grease_ext, 0) ||
3351
+ !CBB_add_u16(outer_extensions.get(), grease_ext)) {
3515
3352
  return false;
3516
3353
  }
3517
3354
  }
3518
3355
 
3519
- bool last_was_empty = false;
3520
- for (size_t i = 0; i < kNumExtensions; i++) {
3356
+ for (size_t unpermuted = 0; unpermuted < kNumExtensions; unpermuted++) {
3357
+ size_t i = hs->extension_permutation.empty()
3358
+ ? unpermuted
3359
+ : hs->extension_permutation[unpermuted];
3521
3360
  const size_t len_before = CBB_len(&extensions);
3522
- if (!kExtensions[i].add_clienthello(hs, &extensions)) {
3361
+ const size_t len_compressed_before = CBB_len(compressed.get());
3362
+ if (!kExtensions[i].add_clienthello(hs, &extensions, compressed.get(),
3363
+ ssl_client_hello_inner)) {
3523
3364
  OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
3524
3365
  ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
3525
3366
  return false;
3526
3367
  }
3527
3368
 
3528
3369
  const size_t bytes_written = CBB_len(&extensions) - len_before;
3370
+ const size_t bytes_written_compressed =
3371
+ CBB_len(compressed.get()) - len_compressed_before;
3372
+ // The callback may write to at most one output.
3373
+ assert(bytes_written == 0 || bytes_written_compressed == 0);
3374
+ if (bytes_written != 0 || bytes_written_compressed != 0) {
3375
+ hs->inner_extensions_sent |= (1u << i);
3376
+ }
3377
+ // If compressed, update the running ech_outer_extensions extension.
3378
+ if (bytes_written_compressed != 0 &&
3379
+ !CBB_add_u16(outer_extensions.get(), kExtensions[i].value)) {
3380
+ return false;
3381
+ }
3382
+ }
3383
+
3384
+ if (ssl->ctx->grease_enabled) {
3385
+ // Add a fake non-empty extension. See RFC 8701. This always matches
3386
+ // |ssl_add_clienthello_tlsext|, so compress it.
3387
+ uint16_t grease_ext = ssl_get_grease_value(hs, ssl_grease_extension2);
3388
+ if (!add_padding_extension(compressed.get(), grease_ext, 1) ||
3389
+ !CBB_add_u16(outer_extensions.get(), grease_ext)) {
3390
+ return false;
3391
+ }
3392
+ }
3393
+
3394
+ // Pad the server name. See draft-ietf-tls-esni-10, section 6.1.2.
3395
+ // TODO(https://crbug.com/boringssl/275): Ideally we'd pad the whole thing to
3396
+ // reduce the output range. See
3397
+ // https://github.com/tlswg/draft-ietf-tls-esni/issues/433
3398
+ size_t padding_len = 0;
3399
+ size_t maximum_name_length = hs->selected_ech_config->maximum_name_length;
3400
+ if (ssl->hostname) {
3401
+ size_t hostname_len = strlen(ssl->hostname.get());
3402
+ if (hostname_len <= maximum_name_length) {
3403
+ padding_len = maximum_name_length - hostname_len;
3404
+ } else {
3405
+ // If the server underestimated the maximum size, pad to a multiple of 32.
3406
+ padding_len = 31 - (hostname_len - 1) % 32;
3407
+ // If the input is close to |maximum_name_length|, pad to the next
3408
+ // multiple for at least 32 bytes of length ambiguity.
3409
+ if (hostname_len + padding_len < maximum_name_length + 32) {
3410
+ padding_len += 32;
3411
+ }
3412
+ }
3413
+ } else {
3414
+ // No SNI. Pad up to |maximum_name_length|, including server_name extension
3415
+ // overhead.
3416
+ padding_len = 9 + maximum_name_length;
3417
+ }
3418
+ if (!add_padding_extension(&extensions, TLSEXT_TYPE_padding, padding_len)) {
3419
+ return false;
3420
+ }
3421
+
3422
+ // Uncompressed extensions are encoded as-is.
3423
+ if (!CBB_add_bytes(&extensions_encoded, CBB_data(&extensions),
3424
+ CBB_len(&extensions))) {
3425
+ return false;
3426
+ }
3427
+
3428
+ // Flush all the compressed extensions.
3429
+ if (CBB_len(compressed.get()) != 0) {
3430
+ CBB extension, child;
3431
+ // Copy them as-is in the real ClientHelloInner.
3432
+ if (!CBB_add_bytes(&extensions, CBB_data(compressed.get()),
3433
+ CBB_len(compressed.get())) ||
3434
+ // Replace with ech_outer_extensions in the encoded form.
3435
+ !CBB_add_u16(&extensions_encoded, TLSEXT_TYPE_ech_outer_extensions) ||
3436
+ !CBB_add_u16_length_prefixed(&extensions_encoded, &extension) ||
3437
+ !CBB_add_u8_length_prefixed(&extension, &child) ||
3438
+ !CBB_add_bytes(&child, CBB_data(outer_extensions.get()),
3439
+ CBB_len(outer_extensions.get())) ||
3440
+ !CBB_flush(&extensions_encoded)) {
3441
+ return false;
3442
+ }
3443
+ }
3444
+
3445
+ // The PSK extension must be last. It is never compressed. Note, if there is a
3446
+ // binder, the caller will need to update both ClientHelloInner and
3447
+ // EncodedClientHelloInner after computing it.
3448
+ const size_t len_before = CBB_len(&extensions);
3449
+ if (!ext_pre_shared_key_add_clienthello(hs, &extensions, out_needs_psk_binder,
3450
+ ssl_client_hello_inner) ||
3451
+ !CBB_add_bytes(&extensions_encoded, CBB_data(&extensions) + len_before,
3452
+ CBB_len(&extensions) - len_before) ||
3453
+ !CBB_flush(out) || //
3454
+ !CBB_flush(out_encoded)) {
3455
+ return false;
3456
+ }
3457
+
3458
+ return true;
3459
+ }
3460
+
3461
+ bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded,
3462
+ bool *out_needs_psk_binder,
3463
+ ssl_client_hello_type_t type, size_t header_len,
3464
+ size_t omit_ech_len) {
3465
+ *out_needs_psk_binder = false;
3466
+
3467
+ if (type == ssl_client_hello_inner) {
3468
+ return ssl_add_clienthello_tlsext_inner(hs, out, out_encoded,
3469
+ out_needs_psk_binder);
3470
+ }
3471
+
3472
+ assert(out_encoded == nullptr); // Only ClientHelloInner needs two outputs.
3473
+ SSL *const ssl = hs->ssl;
3474
+ CBB extensions;
3475
+ if (!CBB_add_u16_length_prefixed(out, &extensions)) {
3476
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3477
+ return false;
3478
+ }
3479
+
3480
+ // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS
3481
+ // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is
3482
+ // important to reset this value.
3483
+ hs->extensions.sent = 0;
3484
+
3485
+ // Add a fake empty extension. See RFC 8701.
3486
+ if (ssl->ctx->grease_enabled &&
3487
+ !add_padding_extension(
3488
+ &extensions, ssl_get_grease_value(hs, ssl_grease_extension1), 0)) {
3489
+ return false;
3490
+ }
3491
+
3492
+ bool last_was_empty = false;
3493
+ for (size_t unpermuted = 0; unpermuted < kNumExtensions; unpermuted++) {
3494
+ size_t i = hs->extension_permutation.empty()
3495
+ ? unpermuted
3496
+ : hs->extension_permutation[unpermuted];
3497
+ size_t bytes_written;
3498
+ if (omit_ech_len != 0 &&
3499
+ kExtensions[i].value == TLSEXT_TYPE_encrypted_client_hello) {
3500
+ bytes_written = omit_ech_len;
3501
+ } else {
3502
+ const size_t len_before = CBB_len(&extensions);
3503
+ if (!kExtensions[i].add_clienthello(hs, &extensions, &extensions, type)) {
3504
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
3505
+ ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
3506
+ return false;
3507
+ }
3508
+
3509
+ bytes_written = CBB_len(&extensions) - len_before;
3510
+ }
3529
3511
  if (bytes_written != 0) {
3530
3512
  hs->extensions.sent |= (1u << i);
3531
3513
  }
@@ -3535,29 +3517,22 @@ bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3535
3517
  }
3536
3518
 
3537
3519
  if (ssl->ctx->grease_enabled) {
3538
- // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
3539
- uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
3540
-
3541
- // The two fake extensions must not have the same value. GREASE values are
3542
- // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
3543
- // one.
3544
- if (grease_ext1 == grease_ext2) {
3545
- grease_ext2 ^= 0x1010;
3546
- }
3547
-
3548
- if (!CBB_add_u16(&extensions, grease_ext2) ||
3549
- !CBB_add_u16(&extensions, 1 /* one byte length */) ||
3550
- !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
3551
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3520
+ // Add a fake non-empty extension. See RFC 8701.
3521
+ if (!add_padding_extension(
3522
+ &extensions, ssl_get_grease_value(hs, ssl_grease_extension2), 1)) {
3552
3523
  return false;
3553
3524
  }
3554
-
3555
3525
  last_was_empty = false;
3556
3526
  }
3557
3527
 
3558
- if (!SSL_is_dtls(ssl) && !ssl->quic_method) {
3559
- size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
3560
- header_len += 2 + CBB_len(&extensions) + psk_extension_len;
3528
+ // In cleartext ClientHellos, we add the padding extension to work around
3529
+ // bugs. We also apply this padding to ClientHelloOuter, to keep the wire
3530
+ // images aligned.
3531
+ size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs, type);
3532
+ if (!SSL_is_dtls(ssl) && !ssl->quic_method &&
3533
+ !ssl->s3->used_hello_retry_request) {
3534
+ header_len += SSL3_HM_HEADER_LENGTH + 2 + CBB_len(&extensions) +
3535
+ omit_ech_len + psk_extension_len;
3561
3536
  size_t padding_len = 0;
3562
3537
 
3563
3538
  // The final extension must be non-empty. WebSphere Application
@@ -3591,24 +3566,21 @@ bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3591
3566
  }
3592
3567
  }
3593
3568
 
3594
- if (padding_len != 0) {
3595
- uint8_t *padding_bytes;
3596
- if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
3597
- !CBB_add_u16(&extensions, padding_len) ||
3598
- !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
3599
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3600
- return false;
3601
- }
3602
-
3603
- OPENSSL_memset(padding_bytes, 0, padding_len);
3569
+ if (padding_len != 0 &&
3570
+ !add_padding_extension(&extensions, TLSEXT_TYPE_padding, padding_len)) {
3571
+ return false;
3604
3572
  }
3605
3573
  }
3606
3574
 
3607
3575
  // The PSK extension must be last, including after the padding.
3608
- if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
3576
+ const size_t len_before = CBB_len(&extensions);
3577
+ if (!ext_pre_shared_key_add_clienthello(hs, &extensions, out_needs_psk_binder,
3578
+ type)) {
3609
3579
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3610
3580
  return false;
3611
3581
  }
3582
+ assert(psk_extension_len == CBB_len(&extensions) - len_before);
3583
+ (void)len_before; // |assert| is omitted in release builds.
3612
3584
 
3613
3585
  // Discard empty extensions blocks.
3614
3586
  if (CBB_len(&extensions) == 0) {
@@ -3654,12 +3626,6 @@ err:
3654
3626
  static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3655
3627
  const SSL_CLIENT_HELLO *client_hello,
3656
3628
  int *out_alert) {
3657
- for (size_t i = 0; i < kNumExtensions; i++) {
3658
- if (kExtensions[i].init != NULL) {
3659
- kExtensions[i].init(hs);
3660
- }
3661
- }
3662
-
3663
3629
  hs->extensions.received = 0;
3664
3630
  CBS extensions;
3665
3631
  CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
@@ -3820,18 +3786,8 @@ static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3820
3786
 
3821
3787
  static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3822
3788
  SSL *const ssl = hs->ssl;
3823
-
3824
- if (ssl->s3->token_binding_negotiated &&
3825
- !(SSL_get_secure_renegotiation_support(ssl) &&
3826
- SSL_get_extms_support(ssl))) {
3827
- OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
3828
- ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
3829
- return false;
3830
- }
3831
-
3832
3789
  int ret = SSL_TLSEXT_ERR_NOACK;
3833
3790
  int al = SSL_AD_UNRECOGNIZED_NAME;
3834
-
3835
3791
  if (ssl->ctx->servername_callback != 0) {
3836
3792
  ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
3837
3793
  } else if (ssl->session_ctx->servername_callback != 0) {
@@ -4046,6 +4002,7 @@ enum ssl_ticket_aead_result_t ssl_process_ticket(
4046
4002
  SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
4047
4003
  bool *out_renew_ticket, Span<const uint8_t> ticket,
4048
4004
  Span<const uint8_t> session_id) {
4005
+ SSL *const ssl = hs->ssl;
4049
4006
  *out_renew_ticket = false;
4050
4007
  out_session->reset();
4051
4008
 
@@ -4054,9 +4011,21 @@ enum ssl_ticket_aead_result_t ssl_process_ticket(
4054
4011
  return ssl_ticket_aead_ignore_ticket;
4055
4012
  }
4056
4013
 
4014
+ // Tickets in TLS 1.3 are tied into pre-shared keys (PSKs), unlike in TLS 1.2
4015
+ // where that concept doesn't exist. The |decrypted_psk| and |ignore_psk|
4016
+ // hints only apply to PSKs. We check the version to determine which this is.
4017
+ const bool is_psk = ssl_protocol_version(ssl) >= TLS1_3_VERSION;
4018
+
4057
4019
  Array<uint8_t> plaintext;
4058
4020
  enum ssl_ticket_aead_result_t result;
4059
- if (hs->ssl->session_ctx->ticket_aead_method != NULL) {
4021
+ SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
4022
+ if (is_psk && hints && !hs->hints_requested &&
4023
+ !hints->decrypted_psk.empty()) {
4024
+ result = plaintext.CopyFrom(hints->decrypted_psk) ? ssl_ticket_aead_success
4025
+ : ssl_ticket_aead_error;
4026
+ } else if (is_psk && hints && !hs->hints_requested && hints->ignore_psk) {
4027
+ result = ssl_ticket_aead_ignore_ticket;
4028
+ } else if (ssl->session_ctx->ticket_aead_method != NULL) {
4060
4029
  result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket,
4061
4030
  ticket);
4062
4031
  } else {
@@ -4065,9 +4034,8 @@ enum ssl_ticket_aead_result_t ssl_process_ticket(
4065
4034
  // length should be well under the minimum size for the session material and
4066
4035
  // HMAC.
4067
4036
  if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
4068
- return ssl_ticket_aead_ignore_ticket;
4069
- }
4070
- if (hs->ssl->session_ctx->ticket_key_cb != NULL) {
4037
+ result = ssl_ticket_aead_ignore_ticket;
4038
+ } else if (ssl->session_ctx->ticket_key_cb != NULL) {
4071
4039
  result =
4072
4040
  ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket);
4073
4041
  } else {
@@ -4075,22 +4043,33 @@ enum ssl_ticket_aead_result_t ssl_process_ticket(
4075
4043
  }
4076
4044
  }
4077
4045
 
4046
+ if (is_psk && hints && hs->hints_requested) {
4047
+ if (result == ssl_ticket_aead_ignore_ticket) {
4048
+ hints->ignore_psk = true;
4049
+ } else if (result == ssl_ticket_aead_success &&
4050
+ !hints->decrypted_psk.CopyFrom(plaintext)) {
4051
+ return ssl_ticket_aead_error;
4052
+ }
4053
+ }
4054
+
4078
4055
  if (result != ssl_ticket_aead_success) {
4079
4056
  return result;
4080
4057
  }
4081
4058
 
4082
4059
  // Decode the session.
4083
4060
  UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(
4084
- plaintext.data(), plaintext.size(), hs->ssl->ctx.get()));
4061
+ plaintext.data(), plaintext.size(), ssl->ctx.get()));
4085
4062
  if (!session) {
4086
4063
  ERR_clear_error(); // Don't leave an error on the queue.
4087
4064
  return ssl_ticket_aead_ignore_ticket;
4088
4065
  }
4089
4066
 
4090
- // Copy the client's session ID into the new session, to denote the ticket has
4091
- // been accepted.
4092
- OPENSSL_memcpy(session->session_id, session_id.data(), session_id.size());
4093
- session->session_id_length = session_id.size();
4067
+ // Envoy's tests expect the session to have a session ID that matches the
4068
+ // placeholder used by the client. It's unclear whether this is a good idea,
4069
+ // but we maintain it for now.
4070
+ SHA256(ticket.data(), ticket.size(), session->session_id);
4071
+ // Other consumers may expect a non-empty session ID to indicate resumption.
4072
+ session->session_id_length = SHA256_DIGEST_LENGTH;
4094
4073
 
4095
4074
  *out_session = std::move(session);
4096
4075
  return ssl_ticket_aead_success;
@@ -4238,11 +4217,11 @@ bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
4238
4217
  if (!sig_ok) {
4239
4218
  OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
4240
4219
  ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
4241
- ssl->s3->channel_id_valid = false;
4242
4220
  return false;
4243
4221
  }
4244
4222
 
4245
4223
  OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
4224
+ ssl->s3->channel_id_valid = true;
4246
4225
  return true;
4247
4226
  }
4248
4227
 
@@ -4353,23 +4332,6 @@ bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
4353
4332
  return true;
4354
4333
  }
4355
4334
 
4356
- bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
4357
- if (hs->config->channel_id_private != NULL ||
4358
- hs->ssl->ctx->channel_id_cb == NULL) {
4359
- return true;
4360
- }
4361
-
4362
- EVP_PKEY *key = NULL;
4363
- hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
4364
- if (key == NULL) {
4365
- // The caller should try again later.
4366
- return true;
4367
- }
4368
-
4369
- UniquePtr<EVP_PKEY> free_key(key);
4370
- return SSL_set1_tls_channel_id(hs->ssl, key);
4371
- }
4372
-
4373
4335
  bool ssl_is_sct_list_valid(const CBS *contents) {
4374
4336
  // Shallow parse the SCT list for sanity. By the RFC
4375
4337
  // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any