grpc 1.38.0 → 1.40.0

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 (340) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +68 -21
  3. data/include/grpc/event_engine/endpoint_config.h +48 -0
  4. data/include/grpc/event_engine/event_engine.h +23 -29
  5. data/include/grpc/event_engine/port.h +2 -0
  6. data/include/grpc/event_engine/slice_allocator.h +21 -36
  7. data/include/grpc/grpc.h +9 -2
  8. data/include/grpc/grpc_security.h +32 -0
  9. data/include/grpc/grpc_security_constants.h +1 -0
  10. data/include/grpc/impl/codegen/grpc_types.h +33 -19
  11. data/include/grpc/impl/codegen/port_platform.h +41 -0
  12. data/src/core/ext/filters/client_channel/client_channel.cc +415 -249
  13. data/src/core/ext/filters/client_channel/client_channel.h +42 -18
  14. data/src/core/ext/filters/client_channel/config_selector.h +19 -6
  15. data/src/core/ext/filters/client_channel/health/health_check_client.cc +2 -0
  16. data/src/core/ext/filters/client_channel/health/health_check_client.h +3 -3
  17. data/src/core/ext/filters/client_channel/http_proxy.cc +16 -1
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +7 -8
  19. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +12 -21
  20. data/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +3 -5
  21. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc +734 -0
  22. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h +10 -0
  23. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +8 -15
  24. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +3 -6
  25. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +18 -36
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_impl.cc +14 -22
  27. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc +2 -9
  28. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_resolver.cc +68 -103
  29. data/src/core/ext/filters/client_channel/lb_policy.cc +1 -15
  30. data/src/core/ext/filters/client_channel/lb_policy.h +70 -46
  31. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +1 -3
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_event_engine.cc +31 -0
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_event_engine.cc +28 -0
  34. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +1 -3
  35. data/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc +7 -2
  36. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +116 -76
  37. data/src/core/ext/filters/client_channel/retry_filter.cc +967 -544
  38. data/src/core/ext/filters/client_channel/retry_service_config.cc +57 -28
  39. data/src/core/ext/filters/client_channel/retry_service_config.h +9 -3
  40. data/src/core/ext/filters/client_channel/service_config_call_data.h +45 -5
  41. data/src/core/ext/filters/client_idle/client_idle_filter.cc +1 -1
  42. data/src/core/ext/filters/http/client/http_client_filter.cc +5 -2
  43. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +2 -1
  44. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +5 -1
  45. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +3 -2
  46. data/src/core/ext/transport/chttp2/transport/bin_decoder.cc +1 -1
  47. data/src/core/{lib/event_engine/slice_allocator.cc → ext/transport/chttp2/transport/chttp2_slice_allocator.cc} +23 -16
  48. data/src/core/ext/transport/chttp2/transport/chttp2_slice_allocator.h +74 -0
  49. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +12 -10
  50. data/src/core/ext/transport/chttp2/transport/flow_control.h +1 -1
  51. data/src/core/ext/transport/chttp2/transport/frame_data.cc +4 -4
  52. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +8 -8
  53. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +5 -5
  54. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +639 -752
  55. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +190 -69
  56. data/src/core/ext/transport/chttp2/transport/internal.h +2 -1
  57. data/src/core/ext/transport/chttp2/transport/parsing.cc +72 -56
  58. data/src/core/ext/transport/chttp2/transport/varint.cc +6 -4
  59. data/src/core/ext/transport/inproc/inproc_transport.cc +42 -31
  60. data/src/core/ext/upb-generated/envoy/config/bootstrap/v3/bootstrap.upb.c +56 -35
  61. data/src/core/ext/upb-generated/envoy/config/bootstrap/v3/bootstrap.upb.h +180 -76
  62. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.c +35 -27
  63. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.h +97 -48
  64. data/src/core/ext/upb-generated/envoy/config/core/v3/base.upb.c +45 -9
  65. data/src/core/ext/upb-generated/envoy/config/core/v3/base.upb.h +67 -7
  66. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.c +66 -9
  67. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.h +227 -0
  68. data/src/core/ext/upb-generated/envoy/config/core/v3/resolver.upb.c +46 -0
  69. data/src/core/ext/upb-generated/envoy/config/core/v3/resolver.upb.h +121 -0
  70. data/src/core/ext/upb-generated/envoy/config/core/v3/substitution_format_string.upb.c +1 -0
  71. data/src/core/ext/upb-generated/envoy/config/core/v3/udp_socket_config.upb.c +35 -0
  72. data/src/core/ext/upb-generated/envoy/config/core/v3/udp_socket_config.upb.h +90 -0
  73. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.c +32 -24
  74. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.h +120 -73
  75. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.c +4 -2
  76. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.h +15 -0
  77. data/src/core/ext/upb-generated/envoy/config/listener/v3/quic_config.upb.c +48 -0
  78. data/src/core/ext/upb-generated/envoy/config/listener/v3/quic_config.upb.h +171 -0
  79. data/src/core/ext/upb-generated/envoy/config/listener/v3/udp_listener_config.upb.c +8 -6
  80. data/src/core/ext/upb-generated/envoy/config/listener/v3/udp_listener_config.upb.h +27 -19
  81. data/src/core/ext/upb-generated/envoy/config/rbac/v3/rbac.upb.c +1 -0
  82. data/src/core/ext/upb-generated/envoy/config/route/v3/route.upb.c +24 -7
  83. data/src/core/ext/upb-generated/envoy/config/route/v3/route.upb.h +57 -0
  84. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.c +29 -17
  85. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.h +72 -0
  86. data/src/core/ext/upb-generated/envoy/extensions/filters/http/fault/v3/fault.upb.c +3 -2
  87. data/src/core/ext/upb-generated/envoy/extensions/filters/http/fault/v3/fault.upb.h +4 -0
  88. data/src/core/ext/upb-generated/envoy/extensions/filters/http/router/v3/router.upb.c +6 -5
  89. data/src/core/ext/upb-generated/envoy/extensions/filters/http/router/v3/router.upb.h +15 -11
  90. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.c +85 -43
  91. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.h +274 -91
  92. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.c +11 -8
  93. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.h +30 -13
  94. data/src/core/ext/upb-generated/envoy/service/status/v3/csds.upb.c +33 -5
  95. data/src/core/ext/upb-generated/envoy/service/status/v3/csds.upb.h +115 -0
  96. data/src/core/ext/upb-generated/envoy/type/http/v3/path_transformation.upb.c +60 -0
  97. data/src/core/ext/upb-generated/envoy/type/http/v3/path_transformation.upb.h +181 -0
  98. data/src/core/ext/upb-generated/envoy/type/matcher/v3/regex.upb.c +1 -0
  99. data/src/core/ext/upb-generated/validate/validate.upb.c +82 -66
  100. data/src/core/ext/upb-generated/validate/validate.upb.h +220 -124
  101. data/src/core/ext/upbdefs-generated/envoy/annotations/deprecation.upbdefs.c +15 -7
  102. data/src/core/ext/upbdefs-generated/envoy/config/accesslog/v3/accesslog.upbdefs.c +53 -52
  103. data/src/core/ext/upbdefs-generated/envoy/config/bootstrap/v3/bootstrap.upbdefs.c +318 -277
  104. data/src/core/ext/upbdefs-generated/envoy/config/bootstrap/v3/bootstrap.upbdefs.h +5 -0
  105. data/src/core/ext/upbdefs-generated/envoy/config/cluster/v3/cluster.upbdefs.c +437 -410
  106. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/base.upbdefs.c +198 -170
  107. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/base.upbdefs.h +10 -0
  108. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/config_source.upbdefs.c +9 -8
  109. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/protocol.upbdefs.c +219 -163
  110. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/protocol.upbdefs.h +15 -0
  111. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/resolver.upbdefs.c +59 -0
  112. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/resolver.upbdefs.h +40 -0
  113. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/substitution_format_string.upbdefs.c +29 -25
  114. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/udp_socket_config.upbdefs.c +52 -0
  115. data/src/core/ext/upbdefs-generated/envoy/config/core/v3/udp_socket_config.upbdefs.h +35 -0
  116. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/listener.upbdefs.c +135 -125
  117. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/listener.upbdefs.h +5 -0
  118. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/listener_components.upbdefs.c +131 -123
  119. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/quic_config.upbdefs.c +90 -0
  120. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/quic_config.upbdefs.h +35 -0
  121. data/src/core/ext/upbdefs-generated/envoy/config/listener/v3/udp_listener_config.upbdefs.c +32 -24
  122. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route.upbdefs.c +69 -55
  123. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route.upbdefs.h +5 -0
  124. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route_components.upbdefs.c +684 -664
  125. data/src/core/ext/upbdefs-generated/envoy/config/route/v3/route_components.upbdefs.h +5 -0
  126. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/http/fault/v3/fault.upbdefs.c +13 -10
  127. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/http/router/v3/router.upbdefs.c +13 -10
  128. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upbdefs.c +441 -375
  129. data/src/core/ext/upbdefs-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upbdefs.h +10 -0
  130. data/src/core/ext/upbdefs-generated/envoy/extensions/transport_sockets/tls/v3/common.upbdefs.c +122 -114
  131. data/src/core/ext/upbdefs-generated/envoy/extensions/transport_sockets/tls/v3/tls.upbdefs.c +1 -1
  132. data/src/core/ext/upbdefs-generated/envoy/service/status/v3/csds.upbdefs.c +112 -79
  133. data/src/core/ext/upbdefs-generated/envoy/service/status/v3/csds.upbdefs.h +5 -0
  134. data/src/core/ext/upbdefs-generated/envoy/type/http/v3/path_transformation.upbdefs.c +64 -0
  135. data/src/core/ext/upbdefs-generated/envoy/type/http/v3/path_transformation.upbdefs.h +50 -0
  136. data/src/core/ext/upbdefs-generated/envoy/type/matcher/v3/regex.upbdefs.c +35 -32
  137. data/src/core/ext/upbdefs-generated/google/rpc/status.upbdefs.c +4 -4
  138. data/src/core/ext/upbdefs-generated/validate/validate.upbdefs.c +182 -160
  139. data/src/core/ext/xds/certificate_provider_store.h +1 -1
  140. data/src/core/ext/xds/xds_api.cc +582 -257
  141. data/src/core/ext/xds/xds_api.h +46 -8
  142. data/src/core/ext/xds/xds_bootstrap.cc +4 -1
  143. data/src/core/ext/xds/xds_client.cc +66 -43
  144. data/src/core/ext/xds/xds_client.h +0 -4
  145. data/src/core/ext/xds/xds_http_filters.cc +3 -2
  146. data/src/core/ext/xds/xds_http_filters.h +3 -0
  147. data/src/core/lib/address_utils/sockaddr_utils.cc +13 -0
  148. data/src/core/lib/address_utils/sockaddr_utils.h +10 -0
  149. data/src/core/lib/channel/call_tracer.h +85 -0
  150. data/src/core/lib/channel/channel_stack.h +1 -1
  151. data/src/core/lib/channel/channelz.h +3 -0
  152. data/src/core/lib/channel/context.h +3 -0
  153. data/src/core/lib/channel/status_util.h +4 -0
  154. data/src/core/lib/compression/stream_compression.h +1 -1
  155. data/src/core/lib/compression/stream_compression_gzip.h +1 -1
  156. data/src/core/lib/compression/stream_compression_identity.h +1 -1
  157. data/src/core/lib/debug/stats.h +1 -1
  158. data/src/core/lib/event_engine/endpoint_config.cc +46 -0
  159. data/src/core/lib/event_engine/endpoint_config_internal.h +42 -0
  160. data/src/core/lib/event_engine/event_engine.cc +50 -0
  161. data/src/core/lib/event_engine/sockaddr.cc +14 -12
  162. data/src/core/lib/event_engine/sockaddr.h +44 -0
  163. data/src/core/lib/gpr/murmur_hash.cc +4 -2
  164. data/src/core/lib/gpr/wrap_memcpy.cc +2 -1
  165. data/src/core/lib/gprpp/manual_constructor.h +1 -1
  166. data/src/core/lib/gprpp/orphanable.h +3 -3
  167. data/src/core/lib/gprpp/status_helper.h +3 -0
  168. data/src/core/lib/gprpp/sync.h +2 -30
  169. data/src/core/lib/iomgr/buffer_list.cc +1 -1
  170. data/src/core/lib/iomgr/endpoint_pair_event_engine.cc +33 -0
  171. data/src/core/lib/iomgr/error.cc +5 -4
  172. data/src/core/lib/iomgr/error.h +1 -1
  173. data/src/core/lib/iomgr/ev_apple.h +1 -1
  174. data/src/core/lib/iomgr/event_engine/closure.cc +54 -0
  175. data/src/core/lib/iomgr/event_engine/closure.h +33 -0
  176. data/src/core/lib/iomgr/event_engine/endpoint.cc +192 -0
  177. data/src/core/lib/iomgr/event_engine/endpoint.h +53 -0
  178. data/src/core/lib/iomgr/event_engine/iomgr.cc +105 -0
  179. data/src/core/lib/iomgr/event_engine/iomgr.h +24 -0
  180. data/src/core/lib/iomgr/event_engine/pollset.cc +87 -0
  181. data/{include/grpc/event_engine/channel_args.h → src/core/lib/iomgr/event_engine/pollset.h} +7 -10
  182. data/src/core/lib/iomgr/event_engine/promise.h +51 -0
  183. data/src/core/lib/iomgr/event_engine/resolved_address_internal.cc +41 -0
  184. data/src/core/lib/iomgr/event_engine/resolved_address_internal.h +35 -0
  185. data/src/core/lib/iomgr/event_engine/resolver.cc +110 -0
  186. data/src/core/lib/iomgr/event_engine/tcp.cc +263 -0
  187. data/src/core/lib/iomgr/event_engine/timer.cc +57 -0
  188. data/src/core/lib/iomgr/exec_ctx.cc +8 -0
  189. data/src/core/lib/iomgr/exec_ctx.h +3 -4
  190. data/src/core/lib/iomgr/executor/threadpool.cc +2 -3
  191. data/src/core/lib/iomgr/executor/threadpool.h +2 -2
  192. data/src/core/lib/iomgr/iomgr.cc +1 -1
  193. data/src/core/lib/iomgr/iomgr_posix.cc +2 -0
  194. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +40 -10
  195. data/src/core/lib/iomgr/pollset_custom.cc +2 -2
  196. data/src/core/lib/iomgr/pollset_custom.h +3 -1
  197. data/src/core/lib/iomgr/pollset_uv.cc +3 -1
  198. data/src/core/lib/iomgr/pollset_uv.h +5 -1
  199. data/src/core/lib/iomgr/port.h +7 -5
  200. data/src/core/lib/iomgr/python_util.h +1 -1
  201. data/src/core/lib/iomgr/resolve_address.cc +5 -1
  202. data/src/core/lib/iomgr/resolve_address.h +6 -0
  203. data/src/core/lib/iomgr/resource_quota.cc +2 -0
  204. data/src/core/lib/iomgr/sockaddr.h +1 -0
  205. data/src/core/lib/iomgr/socket_mutator.cc +15 -2
  206. data/src/core/lib/iomgr/socket_mutator.h +26 -2
  207. data/src/core/lib/iomgr/socket_utils_common_posix.cc +4 -4
  208. data/src/core/lib/iomgr/socket_utils_posix.h +2 -2
  209. data/src/core/lib/iomgr/tcp_client_posix.cc +7 -2
  210. data/src/core/lib/iomgr/tcp_client_windows.cc +2 -0
  211. data/src/core/lib/iomgr/tcp_posix.cc +42 -39
  212. data/src/core/lib/iomgr/tcp_posix.h +8 -0
  213. data/src/core/lib/iomgr/tcp_server_custom.cc +3 -4
  214. data/src/core/lib/iomgr/tcp_server_posix.cc +7 -0
  215. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +2 -1
  216. data/src/core/lib/iomgr/timer.h +6 -1
  217. data/src/core/lib/iomgr/timer_manager.cc +1 -1
  218. data/src/core/lib/json/json_reader.cc +1 -2
  219. data/src/core/lib/matchers/matchers.cc +8 -20
  220. data/src/core/lib/matchers/matchers.h +2 -1
  221. data/src/core/lib/security/authorization/authorization_engine.h +44 -0
  222. data/src/core/lib/security/authorization/authorization_policy_provider.h +32 -0
  223. data/src/core/lib/security/authorization/authorization_policy_provider_vtable.cc +46 -0
  224. data/src/core/lib/security/authorization/evaluate_args.cc +209 -0
  225. data/src/core/lib/security/authorization/evaluate_args.h +91 -0
  226. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +3 -1
  227. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc +49 -0
  228. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h +7 -0
  229. data/src/core/lib/security/credentials/tls/tls_utils.cc +32 -0
  230. data/src/core/lib/security/credentials/tls/tls_utils.h +13 -0
  231. data/src/core/lib/security/security_connector/local/local_security_connector.cc +9 -6
  232. data/src/core/lib/security/security_connector/ssl_utils.cc +5 -0
  233. data/src/core/lib/security/security_connector/tls/tls_security_connector.cc +6 -18
  234. data/src/core/lib/security/transport/security_handshaker.cc +12 -4
  235. data/src/core/lib/security/transport/server_auth_filter.cc +0 -7
  236. data/src/core/lib/slice/slice.cc +12 -2
  237. data/src/core/lib/slice/slice_internal.h +1 -0
  238. data/src/core/lib/surface/call.cc +26 -7
  239. data/src/core/lib/surface/call.h +11 -0
  240. data/src/core/lib/surface/completion_queue.cc +22 -22
  241. data/src/core/lib/surface/completion_queue.h +1 -1
  242. data/src/core/lib/surface/completion_queue_factory.cc +1 -2
  243. data/src/core/lib/surface/init.cc +1 -3
  244. data/src/core/lib/surface/init.h +10 -1
  245. data/src/core/lib/surface/server.cc +3 -1
  246. data/src/core/lib/surface/server.h +3 -3
  247. data/src/core/lib/surface/version.cc +2 -4
  248. data/src/core/lib/transport/error_utils.cc +2 -2
  249. data/src/core/lib/transport/metadata_batch.cc +13 -2
  250. data/src/core/lib/transport/metadata_batch.h +7 -0
  251. data/src/core/lib/transport/transport.h +2 -0
  252. data/src/core/lib/transport/transport_op_string.cc +1 -1
  253. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  254. data/src/core/tsi/alts/crypt/gsec.h +2 -0
  255. data/src/ruby/ext/grpc/extconf.rb +2 -0
  256. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +6 -0
  257. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +10 -1
  258. data/src/ruby/lib/grpc/version.rb +1 -1
  259. data/third_party/boringssl-with-bazel/err_data.c +269 -263
  260. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_object.c +8 -6
  261. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/cipher_extra.c +4 -0
  262. data/third_party/boringssl-with-bazel/src/crypto/curve25519/curve25519.c +1 -1
  263. data/third_party/boringssl-with-bazel/src/crypto/curve25519/internal.h +1 -1
  264. data/third_party/boringssl-with-bazel/src/crypto/evp/evp.c +9 -0
  265. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/prime.c +0 -4
  266. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digest.c +7 -0
  267. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/md32_common.h +87 -121
  268. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md4/md4.c +20 -30
  269. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md5/md5.c +19 -30
  270. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h +1 -4
  271. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/rand.c +0 -13
  272. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +26 -24
  273. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +10 -7
  274. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha1.c +28 -39
  275. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha256.c +48 -66
  276. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha512.c +4 -5
  277. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +362 -371
  278. data/third_party/boringssl-with-bazel/src/crypto/pkcs7/pkcs7_x509.c +4 -2
  279. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/passive.c +2 -2
  280. data/third_party/boringssl-with-bazel/src/crypto/rsa_extra/rsa_asn1.c +1 -2
  281. data/third_party/boringssl-with-bazel/src/crypto/x509/internal.h +101 -11
  282. data/third_party/boringssl-with-bazel/src/crypto/x509/t_x509a.c +3 -0
  283. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +2 -2
  284. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_req.c +3 -0
  285. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +1 -1
  286. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +2 -0
  287. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +14 -15
  288. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vpm.c +53 -73
  289. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +31 -0
  290. data/third_party/boringssl-with-bazel/src/crypto/x509/x509rset.c +3 -0
  291. data/third_party/boringssl-with-bazel/src/crypto/x509/x_all.c +3 -0
  292. data/third_party/boringssl-with-bazel/src/crypto/x509/x_req.c +5 -8
  293. data/third_party/boringssl-with-bazel/src/crypto/x509/x_sig.c +5 -0
  294. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509a.c +3 -0
  295. data/third_party/boringssl-with-bazel/src/crypto/x509v3/internal.h +7 -0
  296. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +1 -1
  297. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_utl.c +5 -8
  298. data/third_party/boringssl-with-bazel/src/include/openssl/aead.h +1 -1
  299. data/third_party/boringssl-with-bazel/src/include/openssl/arm_arch.h +66 -1
  300. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +40 -9
  301. data/third_party/boringssl-with-bazel/src/include/openssl/bytestring.h +1 -0
  302. data/third_party/boringssl-with-bazel/src/include/openssl/chacha.h +1 -1
  303. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +6 -2
  304. data/third_party/boringssl-with-bazel/src/include/openssl/ecdsa.h +14 -0
  305. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +19 -11
  306. data/third_party/boringssl-with-bazel/src/include/openssl/hpke.h +325 -0
  307. data/third_party/boringssl-with-bazel/src/include/openssl/pkcs7.h +23 -7
  308. data/third_party/boringssl-with-bazel/src/include/openssl/rsa.h +99 -63
  309. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +139 -109
  310. data/third_party/boringssl-with-bazel/src/include/openssl/tls1.h +12 -19
  311. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +48 -50
  312. data/third_party/boringssl-with-bazel/src/include/openssl/x509_vfy.h +451 -435
  313. data/third_party/boringssl-with-bazel/src/include/openssl/x509v3.h +0 -1
  314. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +2 -2
  315. data/third_party/boringssl-with-bazel/src/ssl/d1_srtp.cc +1 -1
  316. data/third_party/boringssl-with-bazel/src/ssl/encrypted_client_hello.cc +773 -84
  317. data/third_party/boringssl-with-bazel/src/ssl/handoff.cc +80 -47
  318. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +24 -19
  319. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +189 -86
  320. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +45 -56
  321. data/third_party/boringssl-with-bazel/src/ssl/internal.h +272 -167
  322. data/third_party/boringssl-with-bazel/src/ssl/s3_both.cc +2 -2
  323. data/third_party/boringssl-with-bazel/src/ssl/s3_lib.cc +2 -2
  324. data/third_party/boringssl-with-bazel/src/ssl/s3_pkt.cc +14 -19
  325. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +34 -102
  326. data/third_party/boringssl-with-bazel/src/ssl/ssl_privkey.cc +2 -0
  327. data/third_party/boringssl-with-bazel/src/ssl/ssl_session.cc +8 -31
  328. data/third_party/boringssl-with-bazel/src/ssl/ssl_stat.cc +3 -0
  329. data/third_party/boringssl-with-bazel/src/ssl/ssl_transcript.cc +4 -3
  330. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +7 -3
  331. data/third_party/boringssl-with-bazel/src/ssl/t1_lib.cc +576 -648
  332. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +31 -3
  333. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +98 -39
  334. data/third_party/boringssl-with-bazel/src/ssl/tls13_enc.cc +141 -94
  335. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +58 -68
  336. data/third_party/xxhash/xxhash.h +77 -195
  337. metadata +81 -39
  338. data/src/core/lib/gpr/arena.h +0 -47
  339. data/third_party/boringssl-with-bazel/src/crypto/hpke/internal.h +0 -267
  340. data/third_party/boringssl-with-bazel/src/crypto/x509/vpm_int.h +0 -71
@@ -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
 
@@ -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
  }
@@ -593,148 +619,57 @@ static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
593
619
 
594
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_128_GCM
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
  }
@@ -751,33 +686,40 @@ static bool ext_ech_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
751
686
  static bool ext_ech_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
752
687
  SSL *const ssl = hs->ssl;
753
688
  if (ssl_protocol_version(ssl) < TLS1_3_VERSION || //
754
- hs->ech_accept || //
755
- hs->ech_server_config_list == nullptr) {
689
+ ssl->s3->ech_accept || //
690
+ hs->ech_keys == nullptr) {
756
691
  return true;
757
692
  }
758
693
 
759
- // Write the list of retry configs to |out|. Note
760
- // |SSL_CTX_set1_ech_server_config_list| ensures |ech_server_config_list|
761
- // contains at least one retry config.
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.
762
696
  CBB body, retry_configs;
763
697
  if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) ||
764
698
  !CBB_add_u16_length_prefixed(out, &body) ||
765
699
  !CBB_add_u16_length_prefixed(&body, &retry_configs)) {
766
700
  return false;
767
701
  }
768
- for (const ECHServerConfig &config : hs->ech_server_config_list->configs) {
769
- if (!config.is_retry_config()) {
702
+ for (const auto &config : hs->ech_keys->configs) {
703
+ if (!config->is_retry_config()) {
770
704
  continue;
771
705
  }
772
- if (!CBB_add_bytes(&retry_configs, config.raw().data(),
773
- config.raw().size())) {
706
+ if (!CBB_add_bytes(&retry_configs, config->ech_config().raw.data(),
707
+ config->ech_config().raw.size())) {
774
708
  return false;
775
709
  }
776
710
  }
777
711
  return CBB_flush(out);
778
712
  }
779
713
 
780
- static bool ext_ech_is_inner_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
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
+ }
781
723
  return true;
782
724
  }
783
725
 
@@ -800,10 +742,13 @@ static bool ext_ech_is_inner_parse_clienthello(SSL_HANDSHAKE *hs,
800
742
  //
801
743
  // https://tools.ietf.org/html/rfc5746
802
744
 
803
- static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
804
- 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;
805
749
  // Renegotiation indication is not necessary in TLS 1.3.
806
- if (hs->min_version >= TLS1_3_VERSION) {
750
+ if (hs->min_version >= TLS1_3_VERSION ||
751
+ type == ssl_client_hello_inner) {
807
752
  return true;
808
753
  }
809
754
 
@@ -965,9 +910,11 @@ static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
965
910
  //
966
911
  // https://tools.ietf.org/html/rfc7627
967
912
 
968
- 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) {
969
916
  // Extended master secret is not necessary in TLS 1.3.
970
- if (hs->min_version >= TLS1_3_VERSION) {
917
+ if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) {
971
918
  return true;
972
919
  }
973
920
 
@@ -1040,10 +987,12 @@ static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1040
987
  //
1041
988
  // https://tools.ietf.org/html/rfc5077
1042
989
 
1043
- static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1044
- 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;
1045
994
  // TLS 1.3 uses a different ticket extension.
1046
- if (hs->min_version >= TLS1_3_VERSION ||
995
+ if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner ||
1047
996
  SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1048
997
  return true;
1049
998
  }
@@ -1118,17 +1067,19 @@ static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1118
1067
  //
1119
1068
  // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
1120
1069
 
1121
- 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) {
1122
1073
  if (hs->max_version < TLS1_2_VERSION) {
1123
1074
  return true;
1124
1075
  }
1125
1076
 
1126
1077
  CBB contents, sigalgs_cbb;
1127
- if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1128
- !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) ||
1129
1080
  !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
1130
1081
  !tls12_add_verify_sigalgs(hs, &sigalgs_cbb) ||
1131
- !CBB_flush(out)) {
1082
+ !CBB_flush(out_compressible)) {
1132
1083
  return false;
1133
1084
  }
1134
1085
 
@@ -1157,18 +1108,20 @@ static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1157
1108
  //
1158
1109
  // https://tools.ietf.org/html/rfc6066#section-8
1159
1110
 
1160
- 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) {
1161
1114
  if (!hs->config->ocsp_stapling_enabled) {
1162
1115
  return true;
1163
1116
  }
1164
1117
 
1165
1118
  CBB contents;
1166
- if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1167
- !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) ||
1168
1121
  !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1169
1122
  !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1170
1123
  !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1171
- !CBB_flush(out)) {
1124
+ !CBB_flush(out_compressible)) {
1172
1125
  return false;
1173
1126
  }
1174
1127
 
@@ -1239,11 +1192,16 @@ static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1239
1192
  //
1240
1193
  // https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
1241
1194
 
1242
- static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1243
- SSL *const ssl = hs->ssl;
1244
- if (ssl->s3->initial_handshake_complete ||
1245
- ssl->ctx->next_proto_select_cb == NULL ||
1246
- 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) {
1247
1205
  return true;
1248
1206
  }
1249
1207
 
@@ -1362,13 +1320,15 @@ static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1362
1320
  //
1363
1321
  // https://tools.ietf.org/html/rfc6962#section-3.3.1
1364
1322
 
1365
- 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) {
1366
1326
  if (!hs->config->signed_cert_timestamps_enabled) {
1367
1327
  return true;
1368
1328
  }
1369
1329
 
1370
- if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1371
- !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 */)) {
1372
1332
  return false;
1373
1333
  }
1374
1334
 
@@ -1453,8 +1413,10 @@ static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1453
1413
  //
1454
1414
  // https://tools.ietf.org/html/rfc7301
1455
1415
 
1456
- static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1457
- 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;
1458
1420
  if (hs->config->alpn_client_proto_list.empty() && ssl->quic_method) {
1459
1421
  // ALPN MUST be used with QUIC.
1460
1422
  OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL);
@@ -1467,12 +1429,13 @@ static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1467
1429
  }
1468
1430
 
1469
1431
  CBB contents, proto_list;
1470
- if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1471
- !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) ||
1472
1435
  !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1473
1436
  !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(),
1474
1437
  hs->config->alpn_client_proto_list.size()) ||
1475
- !CBB_flush(out)) {
1438
+ !CBB_flush(out_compressible)) {
1476
1439
  return false;
1477
1440
  }
1478
1441
 
@@ -1667,18 +1630,16 @@ static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1667
1630
  //
1668
1631
  // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
1669
1632
 
1670
- static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
1671
- hs->ssl->s3->channel_id_valid = false;
1672
- }
1673
-
1674
- static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1675
- SSL *const ssl = hs->ssl;
1676
- 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)) {
1677
1638
  return true;
1678
1639
  }
1679
1640
 
1680
- if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1681
- !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 */)) {
1682
1643
  return false;
1683
1644
  }
1684
1645
 
@@ -1688,19 +1649,18 @@ static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1688
1649
  static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
1689
1650
  uint8_t *out_alert,
1690
1651
  CBS *contents) {
1691
- SSL *const ssl = hs->ssl;
1692
1652
  if (contents == NULL) {
1693
1653
  return true;
1694
1654
  }
1695
1655
 
1696
- assert(!SSL_is_dtls(ssl));
1697
- assert(hs->config->channel_id_enabled);
1656
+ assert(!SSL_is_dtls(hs->ssl));
1657
+ assert(hs->config->channel_id_private);
1698
1658
 
1699
1659
  if (CBS_len(contents) != 0) {
1700
1660
  return false;
1701
1661
  }
1702
1662
 
1703
- ssl->s3->channel_id_valid = true;
1663
+ hs->channel_id_negotiated = true;
1704
1664
  return true;
1705
1665
  }
1706
1666
 
@@ -1716,13 +1676,12 @@ static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
1716
1676
  return false;
1717
1677
  }
1718
1678
 
1719
- ssl->s3->channel_id_valid = true;
1679
+ hs->channel_id_negotiated = true;
1720
1680
  return true;
1721
1681
  }
1722
1682
 
1723
1683
  static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1724
- SSL *const ssl = hs->ssl;
1725
- if (!ssl->s3->channel_id_valid) {
1684
+ if (!hs->channel_id_negotiated) {
1726
1685
  return true;
1727
1686
  }
1728
1687
 
@@ -1739,22 +1698,21 @@ static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1739
1698
  //
1740
1699
  // https://tools.ietf.org/html/rfc5764
1741
1700
 
1742
-
1743
- static void ext_srtp_init(SSL_HANDSHAKE *hs) {
1744
- hs->ssl->s3->srtp_profile = NULL;
1745
- }
1746
-
1747
- static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1748
- SSL *const ssl = hs->ssl;
1749
- 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);
1750
1707
  if (profiles == NULL ||
1751
- sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
1708
+ sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0 ||
1709
+ !SSL_is_dtls(ssl)) {
1752
1710
  return true;
1753
1711
  }
1754
1712
 
1755
1713
  CBB contents, profile_ids;
1756
- if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1757
- !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) ||
1758
1716
  !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1759
1717
  return false;
1760
1718
  }
@@ -1766,7 +1724,7 @@ static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1766
1724
  }
1767
1725
 
1768
1726
  if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1769
- !CBB_flush(out)) {
1727
+ !CBB_flush(out_compressible)) {
1770
1728
  return false;
1771
1729
  }
1772
1730
 
@@ -1784,6 +1742,7 @@ static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1784
1742
  // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1785
1743
  //
1786
1744
  // See https://tools.ietf.org/html/rfc5764#section-4.1.1
1745
+ assert(SSL_is_dtls(ssl));
1787
1746
  CBS profile_ids, srtp_mki;
1788
1747
  uint16_t profile_id;
1789
1748
  if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
@@ -1802,11 +1761,8 @@ static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1802
1761
  return false;
1803
1762
  }
1804
1763
 
1805
- STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1806
-
1807
- // Check to see if the server gave us something we support (and presumably
1808
- // offered).
1809
- 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)) {
1810
1766
  if (profile->id == profile_id) {
1811
1767
  ssl->s3->srtp_profile = profile;
1812
1768
  return true;
@@ -1821,7 +1777,8 @@ static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1821
1777
  static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
1822
1778
  CBS *contents) {
1823
1779
  SSL *const ssl = hs->ssl;
1824
- if (contents == NULL) {
1780
+ // DTLS-SRTP is only defined for DTLS.
1781
+ if (contents == NULL || !SSL_is_dtls(ssl)) {
1825
1782
  return true;
1826
1783
  }
1827
1784
 
@@ -1865,6 +1822,7 @@ static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1865
1822
  return true;
1866
1823
  }
1867
1824
 
1825
+ assert(SSL_is_dtls(ssl));
1868
1826
  CBB contents, profile_ids;
1869
1827
  if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1870
1828
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -1883,7 +1841,7 @@ static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1883
1841
  //
1884
1842
  // https://tools.ietf.org/html/rfc4492#section-5.1.2
1885
1843
 
1886
- 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) {
1887
1845
  CBB contents, formats;
1888
1846
  if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1889
1847
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -1896,9 +1854,11 @@ static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
1896
1854
  return true;
1897
1855
  }
1898
1856
 
1899
- 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) {
1900
1860
  // The point format extension is unnecessary in TLS 1.3.
1901
- if (hs->min_version >= TLS1_3_VERSION) {
1861
+ if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) {
1902
1862
  return true;
1903
1863
  }
1904
1864
 
@@ -1964,10 +1924,37 @@ static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
1964
1924
  //
1965
1925
  // https://tools.ietf.org/html/rfc8446#section-4.2.11
1966
1926
 
1967
- static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1968
- 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;
1969
1930
  if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1970
- 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)) {
1971
1958
  return 0;
1972
1959
  }
1973
1960
 
@@ -1975,19 +1962,12 @@ static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
1975
1962
  return 15 + ssl->session->ticket.size() + binder_len;
1976
1963
  }
1977
1964
 
1978
- static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
1979
- SSL *const ssl = hs->ssl;
1980
- hs->needs_psk_binder = false;
1981
- if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr ||
1982
- ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) {
1983
- return true;
1984
- }
1985
-
1986
- // Per RFC 8446 section 4.1.4, skip offering the session if the selected
1987
- // cipher in HelloRetryRequest does not match. This avoids performing the
1988
- // transcript hash transformation for multiple hashes.
1989
- if (ssl->s3 && ssl->s3->used_hello_retry_request &&
1990
- 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)) {
1991
1971
  return true;
1992
1972
  }
1993
1973
 
@@ -2015,7 +1995,7 @@ static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2015
1995
  return false;
2016
1996
  }
2017
1997
 
2018
- hs->needs_psk_binder = true;
1998
+ *out_needs_binder = true;
2019
1999
  return CBB_flush(out);
2020
2000
  }
2021
2001
 
@@ -2128,21 +2108,22 @@ bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2128
2108
  //
2129
2109
  // https://tools.ietf.org/html/rfc8446#section-4.2.9
2130
2110
 
2131
- static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
2132
- 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) {
2133
2114
  if (hs->max_version < TLS1_3_VERSION) {
2134
2115
  return true;
2135
2116
  }
2136
2117
 
2137
2118
  CBB contents, ke_modes;
2138
- if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
2139
- !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) ||
2140
2121
  !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
2141
2122
  !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
2142
2123
  return false;
2143
2124
  }
2144
2125
 
2145
- return CBB_flush(out);
2126
+ return CBB_flush(out_compressible);
2146
2127
  }
2147
2128
 
2148
2129
  static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
@@ -2172,23 +2153,10 @@ static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
2172
2153
  //
2173
2154
  // https://tools.ietf.org/html/rfc8446#section-4.2.10
2174
2155
 
2175
- // ssl_get_local_application_settings looks up the configured ALPS value for
2176
- // |protocol|. If found, it sets |*out_settings| to the value and returns true.
2177
- // Otherwise, it returns false.
2178
- static bool ssl_get_local_application_settings(
2179
- const SSL_HANDSHAKE *hs, Span<const uint8_t> *out_settings,
2180
- Span<const uint8_t> protocol) {
2181
- for (const ALPSConfig &config : hs->config->alps_configs) {
2182
- if (protocol == config.protocol) {
2183
- *out_settings = config.settings;
2184
- return true;
2185
- }
2186
- }
2187
- return false;
2188
- }
2189
-
2190
- static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2191
- 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;
2192
2160
  // The second ClientHello never offers early data, and we must have already
2193
2161
  // filled in |early_data_reason| by this point.
2194
2162
  if (ssl->s3->used_hello_retry_request) {
@@ -2196,56 +2164,20 @@ static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2196
2164
  return true;
2197
2165
  }
2198
2166
 
2199
- if (!ssl->enable_early_data) {
2200
- ssl->s3->early_data_reason = ssl_early_data_disabled;
2167
+ if (!hs->early_data_offered) {
2201
2168
  return true;
2202
2169
  }
2203
2170
 
2204
- if (hs->max_version < TLS1_3_VERSION) {
2205
- // We discard inapplicable sessions, so this is redundant with the session
2206
- // checks below, but we check give a more useful reason.
2207
- ssl->s3->early_data_reason = ssl_early_data_protocol_version;
2208
- return true;
2209
- }
2210
-
2211
- if (ssl->session == nullptr) {
2212
- ssl->s3->early_data_reason = ssl_early_data_no_session_offered;
2213
- return true;
2214
- }
2215
-
2216
- if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
2217
- ssl->session->ticket_max_early_data == 0) {
2218
- ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session;
2219
- return true;
2220
- }
2221
-
2222
- if (!ssl->session->early_alpn.empty()) {
2223
- if (!ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
2224
- // Avoid reporting a confusing value in |SSL_get0_alpn_selected|.
2225
- ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch;
2226
- return true;
2227
- }
2228
-
2229
- // If the previous connection negotiated ALPS, only offer 0-RTT when the
2230
- // local are settings are consistent with what we'd offer for this
2231
- // connection.
2232
- if (ssl->session->has_application_settings) {
2233
- Span<const uint8_t> settings;
2234
- if (!ssl_get_local_application_settings(hs, &settings,
2235
- ssl->session->early_alpn) ||
2236
- settings != ssl->session->local_application_settings) {
2237
- ssl->s3->early_data_reason = ssl_early_data_alps_mismatch;
2238
- return true;
2239
- }
2240
- }
2241
- }
2242
-
2243
- // |early_data_reason| will be filled in later when the server responds.
2244
- hs->early_data_offered = true;
2245
-
2246
- if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
2247
- !CBB_add_u16(out, 0) ||
2248
- !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)) {
2249
2181
  return false;
2250
2182
  }
2251
2183
 
@@ -2326,43 +2258,33 @@ static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2326
2258
  //
2327
2259
  // https://tools.ietf.org/html/rfc8446#section-4.2.8
2328
2260
 
2329
- 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) {
2330
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
+
2331
2267
  if (hs->max_version < TLS1_3_VERSION) {
2332
2268
  return true;
2333
2269
  }
2334
2270
 
2335
- CBB contents, kse_bytes;
2336
- if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2337
- !CBB_add_u16_length_prefixed(out, &contents) ||
2338
- !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2271
+ bssl::ScopedCBB cbb;
2272
+ if (!CBB_init(cbb.get(), 64)) {
2339
2273
  return false;
2340
2274
  }
2341
2275
 
2342
- uint16_t group_id = hs->retry_group;
2343
- uint16_t second_group_id = 0;
2344
- if (ssl->s3 && ssl->s3->used_hello_retry_request) {
2345
- // We received a HelloRetryRequest without a new curve, so there is no new
2346
- // share to append. Leave |hs->key_share| as-is.
2347
- if (group_id == 0 &&
2348
- !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(),
2349
- hs->key_share_bytes.size())) {
2350
- return false;
2351
- }
2352
- hs->key_share_bytes.Reset();
2353
- if (group_id == 0) {
2354
- return CBB_flush(out);
2355
- }
2356
- } else {
2357
- // Add a fake group. See draft-davidben-tls-grease-01.
2358
- if (ssl->ctx->grease_enabled &&
2359
- (!CBB_add_u16(&kse_bytes,
2360
- ssl_get_grease_value(hs, ssl_grease_group)) ||
2361
- !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2362
- !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 */)) {
2363
2281
  return false;
2364
2282
  }
2283
+ }
2365
2284
 
2285
+ uint16_t group_id = override_group_id;
2286
+ uint16_t second_group_id = 0;
2287
+ if (override_group_id == 0) {
2366
2288
  // Predict the most preferred group.
2367
2289
  Span<const uint16_t> groups = tls1_get_grouplist(hs);
2368
2290
  if (groups.empty()) {
@@ -2382,34 +2304,45 @@ static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2382
2304
 
2383
2305
  CBB key_exchange;
2384
2306
  hs->key_shares[0] = SSLKeyShare::Create(group_id);
2385
- if (!hs->key_shares[0] ||
2386
- !CBB_add_u16(&kse_bytes, group_id) ||
2387
- !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2388
- !hs->key_shares[0]->Offer(&key_exchange) ||
2389
- !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)) {
2390
2311
  return false;
2391
2312
  }
2392
2313
 
2393
2314
  if (second_group_id != 0) {
2394
2315
  hs->key_shares[1] = SSLKeyShare::Create(second_group_id);
2395
- if (!hs->key_shares[1] ||
2396
- !CBB_add_u16(&kse_bytes, second_group_id) ||
2397
- !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2398
- !hs->key_shares[1]->Offer(&key_exchange) ||
2399
- !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)) {
2400
2320
  return false;
2401
2321
  }
2402
2322
  }
2403
2323
 
2404
- // Save the contents of the extension to repeat it in the second
2405
- // ClientHello.
2406
- if (ssl->s3 && !ssl->s3->used_hello_retry_request &&
2407
- !hs->key_share_bytes.CopyFrom(
2408
- 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)) {
2409
2342
  return false;
2410
2343
  }
2411
2344
 
2412
- return CBB_flush(out);
2345
+ return true;
2413
2346
  }
2414
2347
 
2415
2348
  bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs,
@@ -2518,12 +2451,20 @@ bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2518
2451
  //
2519
2452
  // https://tools.ietf.org/html/rfc8446#section-4.2.1
2520
2453
 
2521
- static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2522
- 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;
2523
2458
  if (hs->max_version <= TLS1_2_VERSION) {
2524
2459
  return true;
2525
2460
  }
2526
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
+
2527
2468
  CBB contents, versions;
2528
2469
  if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2529
2470
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -2531,13 +2472,16 @@ static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out)
2531
2472
  return false;
2532
2473
  }
2533
2474
 
2534
- // Add a fake version. See draft-davidben-tls-grease-01.
2475
+ // Add a fake version. See RFC 8701.
2535
2476
  if (ssl->ctx->grease_enabled &&
2536
2477
  !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) {
2537
2478
  return false;
2538
2479
  }
2539
2480
 
2540
- 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) ||
2541
2485
  !CBB_flush(out)) {
2542
2486
  return false;
2543
2487
  }
@@ -2550,22 +2494,22 @@ static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out)
2550
2494
  //
2551
2495
  // https://tools.ietf.org/html/rfc8446#section-4.2.2
2552
2496
 
2553
- 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) {
2554
2500
  if (hs->cookie.empty()) {
2555
2501
  return true;
2556
2502
  }
2557
2503
 
2558
2504
  CBB contents, cookie;
2559
- if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
2560
- !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) ||
2561
2507
  !CBB_add_u16_length_prefixed(&contents, &cookie) ||
2562
2508
  !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) ||
2563
- !CBB_flush(out)) {
2509
+ !CBB_flush(out_compressible)) {
2564
2510
  return false;
2565
2511
  }
2566
2512
 
2567
- // The cookie is no longer needed in memory.
2568
- hs->cookie.Reset();
2569
2513
  return true;
2570
2514
  }
2571
2515
 
@@ -2575,16 +2519,19 @@ static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2575
2519
  // https://tools.ietf.org/html/rfc4492#section-5.1.1
2576
2520
  // https://tools.ietf.org/html/rfc8446#section-4.2.7
2577
2521
 
2578
- static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2579
- 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;
2580
2527
  CBB contents, groups_bytes;
2581
- if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
2582
- !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) ||
2583
2530
  !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
2584
2531
  return false;
2585
2532
  }
2586
2533
 
2587
- // Add a fake group. See draft-davidben-tls-grease-01.
2534
+ // Add a fake group. See RFC 8701.
2588
2535
  if (ssl->ctx->grease_enabled &&
2589
2536
  !CBB_add_u16(&groups_bytes,
2590
2537
  ssl_get_grease_value(hs, ssl_grease_group))) {
@@ -2601,7 +2548,7 @@ static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2601
2548
  }
2602
2549
  }
2603
2550
 
2604
- return CBB_flush(out);
2551
+ return CBB_flush(out_compressible);
2605
2552
  }
2606
2553
 
2607
2554
  static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
@@ -2653,158 +2600,11 @@ static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
2653
2600
  return true;
2654
2601
  }
2655
2602
 
2656
- // Token Binding
2657
- //
2658
- // https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
2659
-
2660
- // The Token Binding version number currently matches the draft number of
2661
- // draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
2662
- // Since there are no wire changes to the protocol from draft 13 through the
2663
- // current draft (16), this implementation supports all versions in that range.
2664
- static uint16_t kTokenBindingMaxVersion = 16;
2665
- static uint16_t kTokenBindingMinVersion = 13;
2666
-
2667
- static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
2668
- SSL *const ssl = hs->ssl;
2669
- if (hs->config->token_binding_params.empty() || SSL_is_dtls(ssl)) {
2670
- return true;
2671
- }
2672
-
2673
- CBB contents, params;
2674
- if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2675
- !CBB_add_u16_length_prefixed(out, &contents) ||
2676
- !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
2677
- !CBB_add_u8_length_prefixed(&contents, &params) ||
2678
- !CBB_add_bytes(&params, hs->config->token_binding_params.data(),
2679
- hs->config->token_binding_params.size()) ||
2680
- !CBB_flush(out)) {
2681
- return false;
2682
- }
2683
-
2684
- return true;
2685
- }
2686
-
2687
- static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
2688
- uint8_t *out_alert,
2689
- CBS *contents) {
2690
- SSL *const ssl = hs->ssl;
2691
- if (contents == nullptr) {
2692
- return true;
2693
- }
2694
-
2695
- CBS params_list;
2696
- uint16_t version;
2697
- uint8_t param;
2698
- if (!CBS_get_u16(contents, &version) ||
2699
- !CBS_get_u8_length_prefixed(contents, &params_list) ||
2700
- !CBS_get_u8(&params_list, &param) ||
2701
- CBS_len(&params_list) > 0 ||
2702
- CBS_len(contents) > 0) {
2703
- *out_alert = SSL_AD_DECODE_ERROR;
2704
- return false;
2705
- }
2706
-
2707
- // The server-negotiated version must be less than or equal to our version.
2708
- if (version > kTokenBindingMaxVersion) {
2709
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2710
- return false;
2711
- }
2712
-
2713
- // If the server-selected version is less than what we support, then Token
2714
- // Binding wasn't negotiated (but the extension was parsed successfully).
2715
- if (version < kTokenBindingMinVersion) {
2716
- return true;
2717
- }
2718
-
2719
- for (uint8_t config_param : hs->config->token_binding_params) {
2720
- if (param == config_param) {
2721
- ssl->s3->negotiated_token_binding_param = param;
2722
- ssl->s3->token_binding_negotiated = true;
2723
- return true;
2724
- }
2725
- }
2726
-
2727
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2728
- return false;
2729
- }
2730
-
2731
- // select_tb_param looks for the first token binding param in
2732
- // |hs->ssl->token_binding_params| that is also in |params| and puts it in
2733
- // |hs->ssl->negotiated_token_binding_param|. It returns true if a token binding
2734
- // param is found, and false otherwise.
2735
- static bool select_tb_param(SSL_HANDSHAKE *hs,
2736
- Span<const uint8_t> peer_params) {
2737
- for (uint8_t tb_param : hs->config->token_binding_params) {
2738
- for (uint8_t peer_param : peer_params) {
2739
- if (tb_param == peer_param) {
2740
- hs->ssl->s3->negotiated_token_binding_param = tb_param;
2741
- return true;
2742
- }
2743
- }
2744
- }
2745
- return false;
2746
- }
2747
-
2748
- static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
2749
- uint8_t *out_alert,
2750
- CBS *contents) {
2751
- SSL *const ssl = hs->ssl;
2752
- if (contents == nullptr || hs->config->token_binding_params.empty()) {
2753
- return true;
2754
- }
2755
-
2756
- CBS params;
2757
- uint16_t version;
2758
- if (!CBS_get_u16(contents, &version) ||
2759
- !CBS_get_u8_length_prefixed(contents, &params) ||
2760
- CBS_len(&params) == 0 ||
2761
- CBS_len(contents) > 0) {
2762
- *out_alert = SSL_AD_DECODE_ERROR;
2763
- return false;
2764
- }
2765
-
2766
- // If the client-selected version is less than what we support, then Token
2767
- // Binding wasn't negotiated (but the extension was parsed successfully).
2768
- if (version < kTokenBindingMinVersion) {
2769
- return true;
2770
- }
2771
-
2772
- // If the client-selected version is higher than we support, use our max
2773
- // version. Otherwise, use the client's version.
2774
- hs->negotiated_token_binding_version =
2775
- std::min(version, kTokenBindingMaxVersion);
2776
- if (!select_tb_param(hs, params)) {
2777
- return true;
2778
- }
2779
-
2780
- ssl->s3->token_binding_negotiated = true;
2781
- return true;
2782
- }
2783
-
2784
- static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
2785
- SSL *const ssl = hs->ssl;
2786
-
2787
- if (!ssl->s3->token_binding_negotiated) {
2788
- return true;
2789
- }
2790
-
2791
- CBB contents, params;
2792
- if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
2793
- !CBB_add_u16_length_prefixed(out, &contents) ||
2794
- !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
2795
- !CBB_add_u8_length_prefixed(&contents, &params) ||
2796
- !CBB_add_u8(&params, ssl->s3->negotiated_token_binding_param) ||
2797
- !CBB_flush(out)) {
2798
- return false;
2799
- }
2800
-
2801
- return true;
2802
- }
2803
2603
 
2804
2604
  // QUIC Transport Parameters
2805
2605
 
2806
2606
  static bool ext_quic_transport_params_add_clienthello_impl(
2807
- SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) {
2607
+ const SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) {
2808
2608
  if (hs->config->quic_transport_params.empty() && !hs->ssl->quic_method) {
2809
2609
  return true;
2810
2610
  }
@@ -2821,7 +2621,7 @@ static bool ext_quic_transport_params_add_clienthello_impl(
2821
2621
  return true;
2822
2622
  }
2823
2623
 
2824
- uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters_standard;
2624
+ uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters;
2825
2625
  if (hs->config->quic_use_legacy_codepoint) {
2826
2626
  extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy;
2827
2627
  }
@@ -2837,16 +2637,18 @@ static bool ext_quic_transport_params_add_clienthello_impl(
2837
2637
  return true;
2838
2638
  }
2839
2639
 
2840
- static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
2841
- 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) {
2842
2643
  return ext_quic_transport_params_add_clienthello_impl(
2843
- hs, out, /*use_legacy_codepoint=*/false);
2644
+ hs, out_compressible, /*use_legacy_codepoint=*/false);
2844
2645
  }
2845
2646
 
2846
- static bool ext_quic_transport_params_add_clienthello_legacy(SSL_HANDSHAKE *hs,
2847
- 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) {
2848
2650
  return ext_quic_transport_params_add_clienthello_impl(
2849
- hs, out, /*use_legacy_codepoint=*/true);
2651
+ hs, out_compressible, /*use_legacy_codepoint=*/true);
2850
2652
  }
2851
2653
 
2852
2654
  static bool ext_quic_transport_params_parse_serverhello_impl(
@@ -2957,7 +2759,7 @@ static bool ext_quic_transport_params_add_serverhello_impl(
2957
2759
  return true;
2958
2760
  }
2959
2761
 
2960
- uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters_standard;
2762
+ uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters;
2961
2763
  if (hs->config->quic_use_legacy_codepoint) {
2962
2764
  extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy;
2963
2765
  }
@@ -2990,8 +2792,9 @@ static bool ext_quic_transport_params_add_serverhello_legacy(SSL_HANDSHAKE *hs,
2990
2792
  //
2991
2793
  // https://tools.ietf.org/html/draft-ietf-tls-subcerts
2992
2794
 
2993
- static bool ext_delegated_credential_add_clienthello(SSL_HANDSHAKE *hs,
2994
- 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) {
2995
2798
  return true;
2996
2799
  }
2997
2800
 
@@ -3020,7 +2823,9 @@ static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs,
3020
2823
 
3021
2824
  // Certificate compression
3022
2825
 
3023
- 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) {
3024
2829
  bool first = true;
3025
2830
  CBB contents, algs;
3026
2831
 
@@ -3029,9 +2834,10 @@ static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3029
2834
  continue;
3030
2835
  }
3031
2836
 
3032
- if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
3033
- !CBB_add_u16_length_prefixed(out, &contents) ||
3034
- !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))) {
3035
2841
  return false;
3036
2842
  }
3037
2843
  first = false;
@@ -3040,7 +2846,7 @@ static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3040
2846
  }
3041
2847
  }
3042
2848
 
3043
- return first || CBB_flush(out);
2849
+ return first || CBB_flush(out_compressible);
3044
2850
  }
3045
2851
 
3046
2852
  static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
@@ -3126,8 +2932,22 @@ static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
3126
2932
  //
3127
2933
  // https://tools.ietf.org/html/draft-vvv-tls-alps-01
3128
2934
 
3129
- static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3130
- 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;
3131
2951
  if (// ALPS requires TLS 1.3.
3132
2952
  hs->max_version < TLS1_3_VERSION ||
3133
2953
  // Do not offer ALPS without ALPN.
@@ -3140,8 +2960,8 @@ static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3140
2960
  }
3141
2961
 
3142
2962
  CBB contents, proto_list, proto;
3143
- if (!CBB_add_u16(out, TLSEXT_TYPE_application_settings) ||
3144
- !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) ||
3145
2965
  !CBB_add_u16_length_prefixed(&contents, &proto_list)) {
3146
2966
  return false;
3147
2967
  }
@@ -3154,7 +2974,7 @@ static bool ext_alps_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
3154
2974
  }
3155
2975
  }
3156
2976
 
3157
- return CBB_flush(out);
2977
+ return CBB_flush(out_compressible);
3158
2978
  }
3159
2979
 
3160
2980
  static bool ext_alps_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
@@ -3265,7 +3085,6 @@ bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert,
3265
3085
  static const struct tls_extension kExtensions[] = {
3266
3086
  {
3267
3087
  TLSEXT_TYPE_server_name,
3268
- NULL,
3269
3088
  ext_sni_add_clienthello,
3270
3089
  ext_sni_parse_serverhello,
3271
3090
  ext_sni_parse_clienthello,
@@ -3273,7 +3092,6 @@ static const struct tls_extension kExtensions[] = {
3273
3092
  },
3274
3093
  {
3275
3094
  TLSEXT_TYPE_encrypted_client_hello,
3276
- NULL,
3277
3095
  ext_ech_add_clienthello,
3278
3096
  ext_ech_parse_serverhello,
3279
3097
  ext_ech_parse_clienthello,
@@ -3281,7 +3099,6 @@ static const struct tls_extension kExtensions[] = {
3281
3099
  },
3282
3100
  {
3283
3101
  TLSEXT_TYPE_ech_is_inner,
3284
- NULL,
3285
3102
  ext_ech_is_inner_add_clienthello,
3286
3103
  forbid_parse_serverhello,
3287
3104
  ext_ech_is_inner_parse_clienthello,
@@ -3289,7 +3106,6 @@ static const struct tls_extension kExtensions[] = {
3289
3106
  },
3290
3107
  {
3291
3108
  TLSEXT_TYPE_extended_master_secret,
3292
- NULL,
3293
3109
  ext_ems_add_clienthello,
3294
3110
  ext_ems_parse_serverhello,
3295
3111
  ext_ems_parse_clienthello,
@@ -3297,7 +3113,6 @@ static const struct tls_extension kExtensions[] = {
3297
3113
  },
3298
3114
  {
3299
3115
  TLSEXT_TYPE_renegotiate,
3300
- NULL,
3301
3116
  ext_ri_add_clienthello,
3302
3117
  ext_ri_parse_serverhello,
3303
3118
  ext_ri_parse_clienthello,
@@ -3305,7 +3120,6 @@ static const struct tls_extension kExtensions[] = {
3305
3120
  },
3306
3121
  {
3307
3122
  TLSEXT_TYPE_supported_groups,
3308
- NULL,
3309
3123
  ext_supported_groups_add_clienthello,
3310
3124
  ext_supported_groups_parse_serverhello,
3311
3125
  ext_supported_groups_parse_clienthello,
@@ -3313,7 +3127,6 @@ static const struct tls_extension kExtensions[] = {
3313
3127
  },
3314
3128
  {
3315
3129
  TLSEXT_TYPE_ec_point_formats,
3316
- NULL,
3317
3130
  ext_ec_point_add_clienthello,
3318
3131
  ext_ec_point_parse_serverhello,
3319
3132
  ext_ec_point_parse_clienthello,
@@ -3321,7 +3134,6 @@ static const struct tls_extension kExtensions[] = {
3321
3134
  },
3322
3135
  {
3323
3136
  TLSEXT_TYPE_session_ticket,
3324
- NULL,
3325
3137
  ext_ticket_add_clienthello,
3326
3138
  ext_ticket_parse_serverhello,
3327
3139
  // Ticket extension client parsing is handled in ssl_session.c
@@ -3330,7 +3142,6 @@ static const struct tls_extension kExtensions[] = {
3330
3142
  },
3331
3143
  {
3332
3144
  TLSEXT_TYPE_application_layer_protocol_negotiation,
3333
- NULL,
3334
3145
  ext_alpn_add_clienthello,
3335
3146
  ext_alpn_parse_serverhello,
3336
3147
  // ALPN is negotiated late in |ssl_negotiate_alpn|.
@@ -3339,7 +3150,6 @@ static const struct tls_extension kExtensions[] = {
3339
3150
  },
3340
3151
  {
3341
3152
  TLSEXT_TYPE_status_request,
3342
- NULL,
3343
3153
  ext_ocsp_add_clienthello,
3344
3154
  ext_ocsp_parse_serverhello,
3345
3155
  ext_ocsp_parse_clienthello,
@@ -3347,7 +3157,6 @@ static const struct tls_extension kExtensions[] = {
3347
3157
  },
3348
3158
  {
3349
3159
  TLSEXT_TYPE_signature_algorithms,
3350
- NULL,
3351
3160
  ext_sigalgs_add_clienthello,
3352
3161
  forbid_parse_serverhello,
3353
3162
  ext_sigalgs_parse_clienthello,
@@ -3355,7 +3164,6 @@ static const struct tls_extension kExtensions[] = {
3355
3164
  },
3356
3165
  {
3357
3166
  TLSEXT_TYPE_next_proto_neg,
3358
- NULL,
3359
3167
  ext_npn_add_clienthello,
3360
3168
  ext_npn_parse_serverhello,
3361
3169
  ext_npn_parse_clienthello,
@@ -3363,7 +3171,6 @@ static const struct tls_extension kExtensions[] = {
3363
3171
  },
3364
3172
  {
3365
3173
  TLSEXT_TYPE_certificate_timestamp,
3366
- NULL,
3367
3174
  ext_sct_add_clienthello,
3368
3175
  ext_sct_parse_serverhello,
3369
3176
  ext_sct_parse_clienthello,
@@ -3371,7 +3178,6 @@ static const struct tls_extension kExtensions[] = {
3371
3178
  },
3372
3179
  {
3373
3180
  TLSEXT_TYPE_channel_id,
3374
- ext_channel_id_init,
3375
3181
  ext_channel_id_add_clienthello,
3376
3182
  ext_channel_id_parse_serverhello,
3377
3183
  ext_channel_id_parse_clienthello,
@@ -3379,7 +3185,6 @@ static const struct tls_extension kExtensions[] = {
3379
3185
  },
3380
3186
  {
3381
3187
  TLSEXT_TYPE_srtp,
3382
- ext_srtp_init,
3383
3188
  ext_srtp_add_clienthello,
3384
3189
  ext_srtp_parse_serverhello,
3385
3190
  ext_srtp_parse_clienthello,
@@ -3387,7 +3192,6 @@ static const struct tls_extension kExtensions[] = {
3387
3192
  },
3388
3193
  {
3389
3194
  TLSEXT_TYPE_key_share,
3390
- NULL,
3391
3195
  ext_key_share_add_clienthello,
3392
3196
  forbid_parse_serverhello,
3393
3197
  ignore_parse_clienthello,
@@ -3395,7 +3199,6 @@ static const struct tls_extension kExtensions[] = {
3395
3199
  },
3396
3200
  {
3397
3201
  TLSEXT_TYPE_psk_key_exchange_modes,
3398
- NULL,
3399
3202
  ext_psk_key_exchange_modes_add_clienthello,
3400
3203
  forbid_parse_serverhello,
3401
3204
  ext_psk_key_exchange_modes_parse_clienthello,
@@ -3403,7 +3206,6 @@ static const struct tls_extension kExtensions[] = {
3403
3206
  },
3404
3207
  {
3405
3208
  TLSEXT_TYPE_early_data,
3406
- NULL,
3407
3209
  ext_early_data_add_clienthello,
3408
3210
  ext_early_data_parse_serverhello,
3409
3211
  ext_early_data_parse_clienthello,
@@ -3411,7 +3213,6 @@ static const struct tls_extension kExtensions[] = {
3411
3213
  },
3412
3214
  {
3413
3215
  TLSEXT_TYPE_supported_versions,
3414
- NULL,
3415
3216
  ext_supported_versions_add_clienthello,
3416
3217
  forbid_parse_serverhello,
3417
3218
  ignore_parse_clienthello,
@@ -3419,15 +3220,13 @@ static const struct tls_extension kExtensions[] = {
3419
3220
  },
3420
3221
  {
3421
3222
  TLSEXT_TYPE_cookie,
3422
- NULL,
3423
3223
  ext_cookie_add_clienthello,
3424
3224
  forbid_parse_serverhello,
3425
3225
  ignore_parse_clienthello,
3426
3226
  dont_add_serverhello,
3427
3227
  },
3428
3228
  {
3429
- TLSEXT_TYPE_quic_transport_parameters_standard,
3430
- NULL,
3229
+ TLSEXT_TYPE_quic_transport_parameters,
3431
3230
  ext_quic_transport_params_add_clienthello,
3432
3231
  ext_quic_transport_params_parse_serverhello,
3433
3232
  ext_quic_transport_params_parse_clienthello,
@@ -3435,23 +3234,13 @@ static const struct tls_extension kExtensions[] = {
3435
3234
  },
3436
3235
  {
3437
3236
  TLSEXT_TYPE_quic_transport_parameters_legacy,
3438
- NULL,
3439
3237
  ext_quic_transport_params_add_clienthello_legacy,
3440
3238
  ext_quic_transport_params_parse_serverhello_legacy,
3441
3239
  ext_quic_transport_params_parse_clienthello_legacy,
3442
3240
  ext_quic_transport_params_add_serverhello_legacy,
3443
3241
  },
3444
- {
3445
- TLSEXT_TYPE_token_binding,
3446
- NULL,
3447
- ext_token_binding_add_clienthello,
3448
- ext_token_binding_parse_serverhello,
3449
- ext_token_binding_parse_clienthello,
3450
- ext_token_binding_add_serverhello,
3451
- },
3452
3242
  {
3453
3243
  TLSEXT_TYPE_cert_compression,
3454
- NULL,
3455
3244
  cert_compression_add_clienthello,
3456
3245
  cert_compression_parse_serverhello,
3457
3246
  cert_compression_parse_clienthello,
@@ -3459,7 +3248,6 @@ static const struct tls_extension kExtensions[] = {
3459
3248
  },
3460
3249
  {
3461
3250
  TLSEXT_TYPE_delegated_credential,
3462
- NULL,
3463
3251
  ext_delegated_credential_add_clienthello,
3464
3252
  forbid_parse_serverhello,
3465
3253
  ext_delegated_credential_parse_clienthello,
@@ -3467,7 +3255,6 @@ static const struct tls_extension kExtensions[] = {
3467
3255
  },
3468
3256
  {
3469
3257
  TLSEXT_TYPE_application_settings,
3470
- NULL,
3471
3258
  ext_alps_add_clienthello,
3472
3259
  ext_alps_parse_serverhello,
3473
3260
  // ALPS is negotiated late in |ssl_negotiate_alpn|.
@@ -3485,6 +3272,30 @@ static_assert(kNumExtensions <=
3485
3272
  sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
3486
3273
  "too many extensions for received bitset");
3487
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
+
3488
3299
  static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3489
3300
  uint16_t value) {
3490
3301
  unsigned i;
@@ -3498,47 +3309,205 @@ static const struct tls_extension *tls_extension_find(uint32_t *out_index,
3498
3309
  return NULL;
3499
3310
  }
3500
3311
 
3501
- bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3502
- size_t header_len) {
3503
- SSL *const ssl = hs->ssl;
3504
- CBB extensions;
3505
- 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)) {
3506
3318
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3507
3319
  return false;
3508
3320
  }
3321
+ OPENSSL_memset(ptr, 0, len);
3322
+ return CBB_flush(cbb);
3323
+ }
3509
3324
 
3510
- // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS
3511
- // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is
3512
- // important to reset this value.
3513
- hs->extensions.sent = 0;
3514
-
3515
- for (size_t i = 0; i < kNumExtensions; i++) {
3516
- if (kExtensions[i].init != NULL) {
3517
- kExtensions[i].init(hs);
3518
- }
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;
3519
3342
  }
3520
3343
 
3521
- uint16_t grease_ext1 = 0;
3344
+ hs->inner_extensions_sent = 0;
3345
+
3522
3346
  if (ssl->ctx->grease_enabled) {
3523
- // Add a fake empty extension. See draft-davidben-tls-grease-01.
3524
- grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
3525
- if (!CBB_add_u16(&extensions, grease_ext1) ||
3526
- !CBB_add_u16(&extensions, 0 /* zero length */)) {
3527
- 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)) {
3528
3352
  return false;
3529
3353
  }
3530
3354
  }
3531
3355
 
3532
- bool last_was_empty = false;
3533
- 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];
3534
3360
  const size_t len_before = CBB_len(&extensions);
3535
- 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)) {
3536
3364
  OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
3537
3365
  ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
3538
3366
  return false;
3539
3367
  }
3540
3368
 
3541
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
+ }
3542
3511
  if (bytes_written != 0) {
3543
3512
  hs->extensions.sent |= (1u << i);
3544
3513
  }
@@ -3548,29 +3517,22 @@ bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3548
3517
  }
3549
3518
 
3550
3519
  if (ssl->ctx->grease_enabled) {
3551
- // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
3552
- uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
3553
-
3554
- // The two fake extensions must not have the same value. GREASE values are
3555
- // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
3556
- // one.
3557
- if (grease_ext1 == grease_ext2) {
3558
- grease_ext2 ^= 0x1010;
3559
- }
3560
-
3561
- if (!CBB_add_u16(&extensions, grease_ext2) ||
3562
- !CBB_add_u16(&extensions, 1 /* one byte length */) ||
3563
- !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
3564
- 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)) {
3565
3523
  return false;
3566
3524
  }
3567
-
3568
3525
  last_was_empty = false;
3569
3526
  }
3570
3527
 
3571
- if (!SSL_is_dtls(ssl) && !ssl->quic_method) {
3572
- size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
3573
- 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;
3574
3536
  size_t padding_len = 0;
3575
3537
 
3576
3538
  // The final extension must be non-empty. WebSphere Application
@@ -3604,24 +3566,21 @@ bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
3604
3566
  }
3605
3567
  }
3606
3568
 
3607
- if (padding_len != 0) {
3608
- uint8_t *padding_bytes;
3609
- if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
3610
- !CBB_add_u16(&extensions, padding_len) ||
3611
- !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
3612
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3613
- return false;
3614
- }
3615
-
3616
- 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;
3617
3572
  }
3618
3573
  }
3619
3574
 
3620
3575
  // The PSK extension must be last, including after the padding.
3621
- 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)) {
3622
3579
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3623
3580
  return false;
3624
3581
  }
3582
+ assert(psk_extension_len == CBB_len(&extensions) - len_before);
3583
+ (void)len_before; // |assert| is omitted in release builds.
3625
3584
 
3626
3585
  // Discard empty extensions blocks.
3627
3586
  if (CBB_len(&extensions) == 0) {
@@ -3667,12 +3626,6 @@ err:
3667
3626
  static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
3668
3627
  const SSL_CLIENT_HELLO *client_hello,
3669
3628
  int *out_alert) {
3670
- for (size_t i = 0; i < kNumExtensions; i++) {
3671
- if (kExtensions[i].init != NULL) {
3672
- kExtensions[i].init(hs);
3673
- }
3674
- }
3675
-
3676
3629
  hs->extensions.received = 0;
3677
3630
  CBS extensions;
3678
3631
  CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
@@ -3833,18 +3786,8 @@ static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
3833
3786
 
3834
3787
  static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
3835
3788
  SSL *const ssl = hs->ssl;
3836
-
3837
- if (ssl->s3->token_binding_negotiated &&
3838
- !(SSL_get_secure_renegotiation_support(ssl) &&
3839
- SSL_get_extms_support(ssl))) {
3840
- OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
3841
- ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
3842
- return false;
3843
- }
3844
-
3845
3789
  int ret = SSL_TLSEXT_ERR_NOACK;
3846
3790
  int al = SSL_AD_UNRECOGNIZED_NAME;
3847
-
3848
3791
  if (ssl->ctx->servername_callback != 0) {
3849
3792
  ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
3850
3793
  } else if (ssl->session_ctx->servername_callback != 0) {
@@ -4121,10 +4064,12 @@ enum ssl_ticket_aead_result_t ssl_process_ticket(
4121
4064
  return ssl_ticket_aead_ignore_ticket;
4122
4065
  }
4123
4066
 
4124
- // Copy the client's session ID into the new session, to denote the ticket has
4125
- // been accepted.
4126
- OPENSSL_memcpy(session->session_id, session_id.data(), session_id.size());
4127
- 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;
4128
4073
 
4129
4074
  *out_session = std::move(session);
4130
4075
  return ssl_ticket_aead_success;
@@ -4272,11 +4217,11 @@ bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
4272
4217
  if (!sig_ok) {
4273
4218
  OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
4274
4219
  ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
4275
- ssl->s3->channel_id_valid = false;
4276
4220
  return false;
4277
4221
  }
4278
4222
 
4279
4223
  OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
4224
+ ssl->s3->channel_id_valid = true;
4280
4225
  return true;
4281
4226
  }
4282
4227
 
@@ -4387,23 +4332,6 @@ bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
4387
4332
  return true;
4388
4333
  }
4389
4334
 
4390
- bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
4391
- if (hs->config->channel_id_private != NULL ||
4392
- hs->ssl->ctx->channel_id_cb == NULL) {
4393
- return true;
4394
- }
4395
-
4396
- EVP_PKEY *key = NULL;
4397
- hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
4398
- if (key == NULL) {
4399
- // The caller should try again later.
4400
- return true;
4401
- }
4402
-
4403
- UniquePtr<EVP_PKEY> free_key(key);
4404
- return SSL_set1_tls_channel_id(hs->ssl, key);
4405
- }
4406
-
4407
4335
  bool ssl_is_sct_list_valid(const CBS *contents) {
4408
4336
  // Shallow parse the SCT list for sanity. By the RFC
4409
4337
  // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any