grpc 1.0.1 → 1.1.2

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 (705) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3696 -867
  3. data/etc/roots.pem +39 -111
  4. data/include/grpc/byte_buffer.h +64 -1
  5. data/include/grpc/census.h +40 -96
  6. data/include/grpc/compression.h +2 -1
  7. data/include/grpc/grpc.h +42 -7
  8. data/include/grpc/grpc_posix.h +8 -5
  9. data/include/grpc/impl/codegen/atm.h +3 -0
  10. data/include/grpc/impl/codegen/atm_gcc_atomic.h +2 -0
  11. data/include/grpc/impl/codegen/atm_gcc_sync.h +8 -0
  12. data/include/grpc/impl/codegen/atm_windows.h +4 -0
  13. data/include/grpc/impl/codegen/byte_buffer_reader.h +4 -4
  14. data/include/grpc/impl/codegen/compression_types.h +1 -1
  15. data/include/grpc/impl/codegen/connectivity_state.h +2 -0
  16. data/include/grpc/impl/codegen/exec_ctx_fwd.h +41 -0
  17. data/include/grpc/impl/codegen/gpr_slice.h +84 -0
  18. data/include/grpc/impl/codegen/{alloc.h → gpr_types.h} +30 -29
  19. data/include/grpc/impl/codegen/grpc_types.h +91 -9
  20. data/include/grpc/impl/codegen/port_platform.h +25 -92
  21. data/include/grpc/impl/codegen/slice.h +54 -97
  22. data/include/grpc/impl/codegen/sync.h +0 -253
  23. data/include/grpc/module.modulemap +0 -2
  24. data/include/grpc/slice.h +132 -0
  25. data/include/grpc/{impl/codegen/slice_buffer.h → slice_buffer.h} +22 -39
  26. data/include/grpc/support/alloc.h +40 -1
  27. data/include/grpc/support/log.h +80 -1
  28. data/include/grpc/support/log_windows.h +2 -0
  29. data/include/grpc/support/string_util.h +1 -1
  30. data/include/grpc/support/sync.h +252 -0
  31. data/include/grpc/support/time.h +67 -1
  32. data/src/boringssl/err_data.c +639 -627
  33. data/src/core/ext/census/base_resources.c +71 -0
  34. data/src/core/ext/census/base_resources.h +39 -0
  35. data/src/core/ext/census/gen/census.pb.c +26 -29
  36. data/src/core/ext/census/gen/census.pb.h +68 -67
  37. data/src/core/ext/census/gen/trace_context.pb.c +81 -0
  38. data/src/core/ext/census/gen/trace_context.pb.h +99 -0
  39. data/src/core/ext/census/grpc_filter.c +22 -16
  40. data/src/core/ext/census/grpc_plugin.c +2 -1
  41. data/src/core/ext/census/initialize.c +16 -4
  42. data/src/core/ext/census/mlog.h +1 -1
  43. data/src/core/ext/census/placeholders.c +0 -45
  44. data/src/core/ext/census/resource.c +312 -0
  45. data/src/core/ext/census/resource.h +63 -0
  46. data/src/core/ext/census/trace_context.c +86 -0
  47. data/src/core/ext/census/trace_context.h +68 -0
  48. data/src/core/ext/census/tracing.c +8 -2
  49. data/src/core/ext/{client_config → client_channel}/channel_connectivity.c +8 -4
  50. data/src/core/ext/client_channel/client_channel.c +1218 -0
  51. data/src/core/ext/{client_config → client_channel}/client_channel.h +8 -11
  52. data/src/core/ext/{client_config → client_channel}/client_channel_factory.c +33 -3
  53. data/src/core/ext/{client_config → client_channel}/client_channel_factory.h +15 -8
  54. data/src/core/ext/{client_config/client_config_plugin.c → client_channel/client_channel_plugin.c} +16 -15
  55. data/src/core/ext/{client_config → client_channel}/connector.c +1 -1
  56. data/src/core/ext/{client_config → client_channel}/connector.h +5 -8
  57. data/{include/grpc/support/slice_buffer.h → src/core/ext/client_channel/default_initial_connect_string.c} +4 -5
  58. data/src/core/ext/client_channel/http_connect_handshaker.c +399 -0
  59. data/src/core/ext/client_channel/http_connect_handshaker.h +52 -0
  60. data/src/core/ext/{client_config → client_channel}/initial_connect_string.c +6 -7
  61. data/src/core/ext/{client_config → client_channel}/initial_connect_string.h +10 -10
  62. data/src/core/ext/{client_config → client_channel}/lb_policy.c +11 -11
  63. data/src/core/ext/{client_config → client_channel}/lb_policy.h +68 -27
  64. data/src/core/ext/client_channel/lb_policy_factory.c +163 -0
  65. data/src/core/ext/{client_config → client_channel}/lb_policy_factory.h +64 -9
  66. data/src/core/ext/{client_config → client_channel}/lb_policy_registry.c +6 -4
  67. data/src/core/ext/{client_config → client_channel}/lb_policy_registry.h +4 -4
  68. data/src/core/ext/{client_config → client_channel}/parse_address.c +21 -14
  69. data/src/core/ext/{client_config → client_channel}/parse_address.h +8 -10
  70. data/src/core/ext/{client_config → client_channel}/resolver.c +3 -4
  71. data/src/core/ext/{client_config → client_channel}/resolver.h +11 -15
  72. data/src/core/ext/{client_config → client_channel}/resolver_factory.c +4 -3
  73. data/src/core/ext/{client_config → client_channel}/resolver_factory.h +13 -11
  74. data/src/core/ext/{client_config → client_channel}/resolver_registry.c +54 -34
  75. data/src/core/ext/{client_config → client_channel}/resolver_registry.h +21 -8
  76. data/src/core/ext/{client_config → client_channel}/subchannel.c +208 -119
  77. data/src/core/ext/{client_config → client_channel}/subchannel.h +21 -11
  78. data/src/core/ext/{client_config → client_channel}/subchannel_index.c +6 -17
  79. data/src/core/ext/{client_config → client_channel}/subchannel_index.h +7 -7
  80. data/src/core/ext/{client_config → client_channel}/uri_parser.c +21 -28
  81. data/src/core/ext/{client_config → client_channel}/uri_parser.h +3 -3
  82. data/src/core/ext/lb_policy/grpclb/grpclb.c +1406 -0
  83. data/src/core/ext/lb_policy/grpclb/grpclb.h +44 -0
  84. data/src/core/ext/lb_policy/grpclb/load_balancer_api.c +117 -37
  85. data/src/core/ext/lb_policy/grpclb/load_balancer_api.h +31 -12
  86. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +6 -36
  87. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +22 -42
  88. data/src/core/ext/lb_policy/pick_first/pick_first.c +64 -46
  89. data/src/core/ext/lb_policy/round_robin/round_robin.c +324 -160
  90. data/src/core/ext/load_reporting/load_reporting.c +7 -56
  91. data/src/core/ext/load_reporting/load_reporting.h +41 -28
  92. data/src/core/ext/load_reporting/load_reporting_filter.c +132 -42
  93. data/src/core/ext/load_reporting/load_reporting_filter.h +1 -0
  94. data/src/core/ext/resolver/dns/native/dns_resolver.c +88 -80
  95. data/src/core/ext/resolver/sockaddr/sockaddr_resolver.c +57 -102
  96. data/src/core/ext/transport/chttp2/alpn/alpn.c +1 -1
  97. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +253 -0
  98. data/src/core/{lib/iomgr/ev_poll_and_epoll_posix.h → ext/transport/chttp2/client/chttp2_connector.h} +5 -5
  99. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +31 -160
  100. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +5 -5
  101. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.c +44 -243
  102. data/src/core/ext/transport/chttp2/server/chttp2_server.c +342 -0
  103. data/src/core/ext/transport/chttp2/server/chttp2_server.h +47 -0
  104. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.c +11 -124
  105. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.c +20 -9
  106. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.c +28 -236
  107. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +31 -27
  108. data/src/core/ext/transport/chttp2/transport/bin_decoder.h +5 -4
  109. data/src/core/ext/transport/chttp2/transport/bin_encoder.c +25 -22
  110. data/src/core/ext/transport/chttp2/transport/bin_encoder.h +8 -7
  111. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +0 -3
  112. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +1345 -1521
  113. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -1
  114. data/src/core/ext/transport/chttp2/transport/frame.h +3 -5
  115. data/src/core/ext/transport/chttp2/transport/frame_data.c +50 -47
  116. data/src/core/ext/transport/chttp2/transport/frame_data.h +8 -9
  117. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +19 -21
  118. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +9 -8
  119. data/src/core/ext/transport/chttp2/transport/frame_ping.c +13 -12
  120. data/src/core/ext/transport/chttp2/transport/frame_ping.h +6 -6
  121. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +31 -19
  122. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +8 -7
  123. data/src/core/ext/transport/chttp2/transport/frame_settings.c +22 -25
  124. data/src/core/ext/transport/chttp2/transport/frame_settings.h +9 -8
  125. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +26 -18
  126. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +5 -6
  127. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +68 -58
  128. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +8 -5
  129. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +327 -214
  130. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +14 -9
  131. data/src/core/ext/transport/chttp2/transport/hpack_table.c +24 -19
  132. data/src/core/ext/transport/chttp2/transport/hpack_table.h +9 -6
  133. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +2 -2
  134. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +1 -1
  135. data/src/core/ext/transport/chttp2/transport/internal.h +284 -436
  136. data/src/core/ext/transport/chttp2/transport/parsing.c +355 -590
  137. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -309
  138. data/src/core/ext/transport/chttp2/transport/stream_map.c +13 -34
  139. data/src/core/ext/transport/chttp2/transport/stream_map.h +3 -4
  140. data/src/core/ext/transport/chttp2/transport/writing.c +174 -286
  141. data/src/core/lib/channel/channel_args.c +70 -13
  142. data/src/core/lib/channel/channel_args.h +28 -2
  143. data/src/core/lib/channel/channel_stack.c +77 -28
  144. data/src/core/lib/channel/channel_stack.h +61 -23
  145. data/src/core/lib/channel/channel_stack_builder.c +33 -25
  146. data/src/core/lib/channel/channel_stack_builder.h +17 -8
  147. data/src/core/lib/channel/compress_filter.c +52 -36
  148. data/src/core/lib/channel/connected_channel.c +20 -12
  149. data/src/core/lib/channel/connected_channel.h +2 -1
  150. data/src/core/lib/channel/context.h +13 -1
  151. data/src/core/lib/channel/deadline_filter.c +344 -0
  152. data/src/core/lib/channel/deadline_filter.h +99 -0
  153. data/src/core/lib/channel/handshaker.c +240 -0
  154. data/src/core/lib/channel/handshaker.h +164 -0
  155. data/src/core/lib/{security/credentials/google_default/credentials_windows.c → channel/handshaker_factory.c} +16 -23
  156. data/src/core/lib/channel/handshaker_factory.h +66 -0
  157. data/src/core/lib/channel/handshaker_registry.c +113 -0
  158. data/src/core/{ext/client_config/client_config.h → lib/channel/handshaker_registry.h} +26 -16
  159. data/src/core/lib/channel/http_client_filter.c +248 -46
  160. data/src/core/lib/channel/http_client_filter.h +3 -0
  161. data/src/core/lib/channel/http_server_filter.c +136 -24
  162. data/src/core/lib/channel/message_size_filter.c +261 -0
  163. data/src/core/lib/channel/message_size_filter.h +39 -0
  164. data/src/core/lib/compression/message_compress.c +43 -37
  165. data/src/core/lib/compression/message_compress.h +7 -5
  166. data/src/core/lib/http/format_request.c +26 -11
  167. data/src/core/lib/http/format_request.h +7 -5
  168. data/src/core/lib/http/httpcli.c +45 -27
  169. data/src/core/lib/http/httpcli.h +4 -4
  170. data/src/core/lib/http/httpcli_security_connector.c +56 -46
  171. data/src/core/lib/http/parser.c +17 -14
  172. data/src/core/lib/http/parser.h +4 -2
  173. data/src/core/lib/iomgr/closure.c +49 -7
  174. data/src/core/lib/iomgr/closure.h +56 -14
  175. data/src/core/lib/iomgr/combiner.c +422 -0
  176. data/src/core/lib/iomgr/combiner.h +64 -0
  177. data/src/core/lib/iomgr/endpoint.c +8 -2
  178. data/src/core/lib/iomgr/endpoint.h +17 -7
  179. data/src/core/lib/iomgr/endpoint_pair.h +3 -2
  180. data/src/core/lib/iomgr/endpoint_pair_posix.c +9 -8
  181. data/src/core/{ext/client_config/lb_policy_factory.c → lib/iomgr/endpoint_pair_uv.c} +18 -13
  182. data/src/core/lib/iomgr/endpoint_pair_windows.c +7 -6
  183. data/src/core/lib/iomgr/error.c +72 -6
  184. data/src/core/lib/iomgr/error.h +30 -3
  185. data/src/core/lib/iomgr/ev_epoll_linux.c +500 -382
  186. data/src/core/lib/iomgr/ev_epoll_linux.h +3 -2
  187. data/src/core/lib/iomgr/ev_poll_posix.c +317 -30
  188. data/src/core/lib/iomgr/ev_poll_posix.h +1 -0
  189. data/src/core/lib/iomgr/ev_posix.c +26 -5
  190. data/src/core/lib/iomgr/ev_posix.h +12 -1
  191. data/src/core/lib/iomgr/exec_ctx.c +27 -94
  192. data/src/core/lib/iomgr/exec_ctx.h +19 -22
  193. data/src/core/lib/iomgr/executor.c +29 -8
  194. data/src/core/lib/iomgr/executor.h +2 -4
  195. data/src/core/lib/iomgr/iocp_windows.c +3 -4
  196. data/src/core/lib/iomgr/iomgr.c +14 -10
  197. data/src/core/lib/iomgr/iomgr.h +6 -2
  198. data/src/core/lib/iomgr/iomgr_posix.c +2 -2
  199. data/src/core/lib/iomgr/iomgr_uv.c +49 -0
  200. data/src/core/lib/iomgr/iomgr_windows.c +2 -2
  201. data/src/core/lib/iomgr/load_file.c +3 -3
  202. data/src/core/lib/iomgr/load_file.h +2 -2
  203. data/src/core/lib/iomgr/network_status_tracker.c +1 -1
  204. data/src/core/lib/iomgr/pollset_set_uv.c +62 -0
  205. data/src/core/lib/iomgr/pollset_set_windows.c +3 -3
  206. data/src/core/lib/iomgr/pollset_uv.c +142 -0
  207. data/src/core/lib/iomgr/pollset_uv.h +42 -0
  208. data/src/core/lib/iomgr/pollset_windows.c +5 -6
  209. data/src/core/lib/iomgr/port.h +129 -0
  210. data/src/core/lib/iomgr/resolve_address.h +2 -1
  211. data/src/core/lib/iomgr/resolve_address_posix.c +14 -13
  212. data/src/core/lib/iomgr/resolve_address_uv.c +233 -0
  213. data/src/core/lib/iomgr/resolve_address_windows.c +14 -12
  214. data/src/core/lib/iomgr/resource_quota.c +832 -0
  215. data/src/core/lib/iomgr/resource_quota.h +159 -0
  216. data/src/core/lib/iomgr/sockaddr.h +10 -2
  217. data/src/core/lib/iomgr/sockaddr_utils.c +63 -36
  218. data/src/core/lib/iomgr/sockaddr_utils.h +14 -14
  219. data/src/core/lib/iomgr/socket_mutator.c +98 -0
  220. data/src/core/lib/iomgr/socket_mutator.h +80 -0
  221. data/src/core/lib/iomgr/socket_utils.h +42 -0
  222. data/src/core/lib/iomgr/socket_utils_common_posix.c +28 -13
  223. data/src/core/lib/iomgr/socket_utils_linux.c +11 -5
  224. data/src/core/lib/iomgr/socket_utils_posix.c +10 -7
  225. data/src/core/lib/iomgr/socket_utils_posix.h +11 -4
  226. data/src/core/lib/iomgr/socket_utils_uv.c +49 -0
  227. data/src/core/lib/iomgr/socket_utils_windows.c +52 -0
  228. data/src/core/lib/iomgr/socket_windows.c +14 -6
  229. data/src/core/lib/iomgr/socket_windows.h +1 -0
  230. data/src/core/lib/iomgr/tcp_client.h +8 -2
  231. data/src/core/lib/iomgr/tcp_client_posix.c +131 -82
  232. data/src/core/lib/iomgr/tcp_client_posix.h +45 -0
  233. data/src/core/lib/iomgr/tcp_client_uv.c +190 -0
  234. data/src/core/lib/iomgr/tcp_client_windows.c +54 -30
  235. data/src/core/lib/iomgr/tcp_posix.c +135 -56
  236. data/src/core/lib/iomgr/tcp_posix.h +2 -2
  237. data/src/core/lib/iomgr/tcp_server.h +14 -6
  238. data/src/core/lib/iomgr/tcp_server_posix.c +154 -118
  239. data/src/core/lib/iomgr/tcp_server_uv.c +388 -0
  240. data/src/core/lib/iomgr/tcp_server_windows.c +127 -100
  241. data/src/core/lib/iomgr/tcp_uv.c +367 -0
  242. data/src/core/lib/iomgr/tcp_uv.h +59 -0
  243. data/src/core/lib/iomgr/tcp_windows.c +65 -48
  244. data/src/core/lib/iomgr/tcp_windows.h +3 -1
  245. data/src/core/lib/iomgr/timer.h +21 -21
  246. data/src/core/lib/iomgr/{timer.c → timer_generic.c} +15 -10
  247. data/src/core/lib/iomgr/timer_generic.h +49 -0
  248. data/src/core/lib/iomgr/timer_heap.c +6 -0
  249. data/src/core/lib/iomgr/timer_uv.c +99 -0
  250. data/src/core/lib/iomgr/timer_uv.h +47 -0
  251. data/src/core/lib/iomgr/udp_server.c +116 -98
  252. data/src/core/lib/iomgr/udp_server.h +5 -3
  253. data/src/core/lib/iomgr/unix_sockets_posix.c +14 -6
  254. data/src/core/lib/iomgr/unix_sockets_posix.h +6 -5
  255. data/src/core/lib/iomgr/unix_sockets_posix_noop.c +4 -4
  256. data/src/core/lib/iomgr/wakeup_fd_cv.c +118 -0
  257. data/src/core/lib/iomgr/wakeup_fd_cv.h +80 -0
  258. data/src/core/lib/iomgr/wakeup_fd_eventfd.c +3 -3
  259. data/src/core/lib/iomgr/wakeup_fd_nospecial.c +3 -3
  260. data/src/core/lib/iomgr/wakeup_fd_pipe.c +12 -6
  261. data/src/core/lib/iomgr/wakeup_fd_posix.c +34 -5
  262. data/src/core/lib/iomgr/wakeup_fd_posix.h +5 -0
  263. data/src/core/lib/iomgr/workqueue.h +12 -20
  264. data/src/core/{ext/client_config/client_config.c → lib/iomgr/workqueue_uv.c} +24 -33
  265. data/{include/grpc/support/slice.h → src/core/lib/iomgr/workqueue_uv.h} +4 -6
  266. data/src/core/lib/iomgr/workqueue_windows.c +9 -8
  267. data/src/core/lib/json/json.c +3 -3
  268. data/src/core/lib/json/json.h +11 -11
  269. data/src/core/lib/json/json_reader.c +9 -5
  270. data/src/core/lib/profiling/basic_timers.c +10 -1
  271. data/src/core/lib/profiling/timers.h +2 -0
  272. data/src/core/lib/security/context/security_context.c +13 -3
  273. data/src/core/lib/security/context/security_context.h +20 -0
  274. data/src/core/lib/security/credentials/composite/composite_credentials.c +28 -14
  275. data/src/core/lib/security/credentials/composite/composite_credentials.h +2 -2
  276. data/src/core/lib/security/credentials/credentials.c +48 -19
  277. data/src/core/lib/security/credentials/credentials.h +36 -19
  278. data/src/core/lib/security/credentials/credentials_metadata.c +11 -8
  279. data/src/core/lib/security/credentials/fake/fake_credentials.c +15 -11
  280. data/src/core/lib/security/credentials/google_default/{credentials_posix.c → credentials_generic.c} +7 -14
  281. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +33 -21
  282. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +14 -0
  283. data/src/core/lib/security/credentials/iam/iam_credentials.c +3 -2
  284. data/src/core/lib/security/credentials/jwt/json_token.c +1 -0
  285. data/src/core/lib/security/credentials/jwt/json_token.h +1 -1
  286. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +54 -19
  287. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +2 -1
  288. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +129 -79
  289. data/src/core/lib/security/credentials/jwt/jwt_verifier.h +9 -6
  290. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +63 -28
  291. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -1
  292. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +32 -11
  293. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +13 -9
  294. data/src/core/lib/security/transport/client_auth_filter.c +33 -27
  295. data/src/core/lib/security/transport/secure_endpoint.c +93 -68
  296. data/src/core/lib/security/transport/secure_endpoint.h +2 -2
  297. data/src/core/lib/security/transport/security_connector.c +133 -168
  298. data/src/core/lib/security/transport/security_connector.h +31 -46
  299. data/src/core/lib/security/transport/security_handshaker.c +501 -0
  300. data/src/core/lib/security/transport/{handshake.h → security_handshaker.h} +10 -10
  301. data/src/core/lib/security/transport/server_auth_filter.c +50 -38
  302. data/src/core/lib/security/util/b64.c +11 -8
  303. data/src/core/lib/security/util/b64.h +5 -4
  304. data/src/core/lib/slice/percent_encoding.c +182 -0
  305. data/src/core/lib/slice/percent_encoding.h +78 -0
  306. data/src/core/lib/{support → slice}/slice.c +81 -50
  307. data/src/core/lib/{support → slice}/slice_buffer.c +78 -60
  308. data/src/core/lib/slice/slice_internal.h +49 -0
  309. data/src/core/lib/slice/slice_string_helpers.c +90 -0
  310. data/src/core/lib/{iomgr/workqueue_posix.h → slice/slice_string_helpers.h} +18 -18
  311. data/src/core/lib/support/backoff.c +24 -13
  312. data/src/core/lib/support/backoff.h +5 -2
  313. data/src/core/lib/support/env.h +0 -2
  314. data/src/core/lib/support/log.c +5 -4
  315. data/src/core/lib/support/log_linux.c +0 -1
  316. data/src/core/lib/support/log_posix.c +1 -1
  317. data/src/core/lib/support/mpscq.c +83 -0
  318. data/src/core/lib/support/mpscq.h +65 -0
  319. data/src/core/lib/support/string.c +58 -49
  320. data/src/core/lib/support/string.h +11 -8
  321. data/src/core/lib/support/subprocess_posix.c +5 -2
  322. data/src/core/lib/support/thd.c +1 -1
  323. data/src/core/lib/support/time.c +43 -79
  324. data/src/core/lib/support/time_posix.c +1 -1
  325. data/src/core/lib/support/tmpfile.h +0 -2
  326. data/src/core/lib/surface/alarm.c +4 -1
  327. data/src/core/lib/surface/byte_buffer.c +17 -11
  328. data/src/core/lib/surface/byte_buffer_reader.c +23 -15
  329. data/src/core/lib/surface/call.c +294 -276
  330. data/src/core/lib/surface/call.h +24 -9
  331. data/src/core/lib/surface/call_log_batch.c +5 -3
  332. data/src/core/lib/surface/channel.c +127 -111
  333. data/src/core/lib/surface/channel.h +14 -5
  334. data/src/core/lib/surface/channel_init.c +1 -1
  335. data/src/core/lib/surface/channel_init.h +10 -1
  336. data/src/core/lib/surface/channel_ping.c +7 -6
  337. data/src/core/lib/surface/completion_queue.c +154 -18
  338. data/src/core/lib/surface/completion_queue.h +5 -0
  339. data/src/core/lib/surface/init.c +40 -6
  340. data/src/core/lib/surface/init.h +1 -0
  341. data/src/core/lib/surface/init_secure.c +5 -2
  342. data/src/core/lib/surface/lame_client.c +28 -18
  343. data/src/core/lib/surface/server.c +134 -87
  344. data/src/core/lib/surface/server.h +8 -0
  345. data/src/core/lib/surface/validate_metadata.c +1 -1
  346. data/src/core/lib/surface/version.c +3 -1
  347. data/src/core/lib/transport/byte_stream.c +7 -4
  348. data/src/core/lib/transport/byte_stream.h +6 -10
  349. data/src/core/lib/transport/connectivity_state.c +21 -12
  350. data/src/core/lib/transport/connectivity_state.h +4 -1
  351. data/src/core/lib/transport/mdstr_hash_table.c +118 -0
  352. data/src/core/lib/transport/mdstr_hash_table.h +77 -0
  353. data/src/core/lib/transport/metadata.c +83 -60
  354. data/src/core/lib/transport/metadata.h +41 -23
  355. data/src/core/lib/transport/metadata_batch.c +17 -11
  356. data/src/core/lib/transport/metadata_batch.h +20 -6
  357. data/src/core/lib/transport/pid_controller.c +57 -0
  358. data/src/core/lib/transport/pid_controller.h +64 -0
  359. data/src/core/lib/transport/service_config.c +251 -0
  360. data/src/core/lib/transport/service_config.h +71 -0
  361. data/src/core/lib/transport/static_metadata.c +18 -16
  362. data/src/core/lib/transport/static_metadata.h +113 -107
  363. data/src/core/{ext/transport/chttp2 → lib}/transport/timeout_encoding.c +3 -3
  364. data/src/core/{ext/transport/chttp2 → lib}/transport/timeout_encoding.h +7 -7
  365. data/src/core/lib/transport/transport.c +84 -23
  366. data/src/core/lib/transport/transport.h +53 -8
  367. data/src/core/lib/transport/transport_impl.h +3 -0
  368. data/src/core/lib/transport/transport_op_string.c +92 -20
  369. data/src/core/lib/tsi/ssl_transport_security.c +3 -1
  370. data/src/core/plugin_registry/grpc_plugin_registry.c +8 -4
  371. data/src/ruby/ext/grpc/extconf.rb +0 -1
  372. data/src/ruby/ext/grpc/rb_byte_buffer.c +8 -7
  373. data/src/ruby/ext/grpc/rb_call.c +15 -5
  374. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  375. data/src/ruby/ext/grpc/rb_compression_options.c +466 -0
  376. data/src/{core/ext/client_config/default_initial_connect_string.c → ruby/ext/grpc/rb_compression_options.h} +10 -5
  377. data/src/ruby/ext/grpc/rb_grpc.c +3 -1
  378. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +198 -190
  379. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +306 -294
  380. data/src/ruby/ext/grpc/rb_server.c +18 -12
  381. data/src/ruby/lib/grpc/errors.rb +154 -2
  382. data/src/ruby/lib/grpc/generic/active_call.rb +144 -63
  383. data/src/ruby/lib/grpc/generic/bidi_call.rb +18 -2
  384. data/src/ruby/lib/grpc/generic/client_stub.rb +7 -5
  385. data/src/ruby/lib/grpc/generic/rpc_desc.rb +39 -13
  386. data/src/ruby/lib/grpc/generic/rpc_server.rb +51 -24
  387. data/src/ruby/lib/grpc/generic/service.rb +3 -2
  388. data/src/ruby/lib/grpc/version.rb +1 -1
  389. data/src/ruby/pb/grpc/health/checker.rb +3 -1
  390. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +7 -0
  391. data/src/ruby/pb/test/client.rb +307 -7
  392. data/src/ruby/pb/test/server.rb +26 -1
  393. data/src/ruby/spec/compression_options_spec.rb +164 -0
  394. data/src/ruby/spec/error_sanity_spec.rb +64 -0
  395. data/src/ruby/spec/generic/active_call_spec.rb +290 -12
  396. data/src/ruby/spec/generic/client_stub_spec.rb +91 -41
  397. data/src/ruby/spec/generic/rpc_desc_spec.rb +36 -16
  398. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +22 -28
  399. data/src/ruby/spec/generic/rpc_server_spec.rb +6 -6
  400. data/src/ruby/spec/pb/health/checker_spec.rb +27 -19
  401. data/src/ruby/spec/spec_helper.rb +2 -0
  402. data/third_party/boringssl/crypto/aes/aes.c +12 -12
  403. data/third_party/boringssl/crypto/aes/mode_wrappers.c +6 -2
  404. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +28 -13
  405. data/third_party/boringssl/crypto/asn1/a_gentm.c +2 -0
  406. data/third_party/boringssl/crypto/asn1/a_object.c +7 -3
  407. data/third_party/boringssl/crypto/asn1/a_strnid.c +1 -0
  408. data/third_party/boringssl/crypto/asn1/a_time.c +0 -11
  409. data/third_party/boringssl/crypto/asn1/a_type.c +0 -2
  410. data/third_party/boringssl/crypto/asn1/a_utctm.c +1 -30
  411. data/third_party/boringssl/crypto/asn1/asn1_lib.c +56 -76
  412. data/third_party/boringssl/crypto/asn1/asn1_locl.h +0 -10
  413. data/third_party/boringssl/crypto/asn1/asn1_par.c +0 -322
  414. data/third_party/boringssl/crypto/asn1/f_enum.c +1 -108
  415. data/third_party/boringssl/crypto/asn1/f_int.c +1 -106
  416. data/third_party/boringssl/crypto/asn1/f_string.c +1 -106
  417. data/third_party/boringssl/crypto/asn1/tasn_dec.c +10 -14
  418. data/third_party/boringssl/crypto/asn1/tasn_enc.c +17 -11
  419. data/third_party/boringssl/crypto/asn1/tasn_typ.c +29 -42
  420. data/third_party/boringssl/crypto/asn1/tasn_utl.c +1 -1
  421. data/third_party/boringssl/crypto/base64/base64.c +249 -285
  422. data/third_party/boringssl/crypto/bio/bio.c +13 -23
  423. data/third_party/boringssl/crypto/bio/bio_mem.c +3 -2
  424. data/third_party/boringssl/crypto/bio/connect.c +12 -3
  425. data/third_party/boringssl/crypto/bio/fd.c +22 -15
  426. data/third_party/boringssl/crypto/bio/file.c +2 -38
  427. data/third_party/boringssl/crypto/bio/hexdump.c +1 -2
  428. data/third_party/boringssl/crypto/bio/internal.h +3 -0
  429. data/third_party/boringssl/crypto/bio/pair.c +1 -1
  430. data/third_party/boringssl/crypto/bio/socket.c +10 -2
  431. data/third_party/boringssl/crypto/bio/socket_helper.c +2 -2
  432. data/third_party/boringssl/crypto/bn/asm/x86_64-gcc.c +0 -8
  433. data/third_party/boringssl/crypto/bn/bn.c +38 -0
  434. data/third_party/boringssl/crypto/bn/cmp.c +25 -0
  435. data/third_party/boringssl/crypto/bn/convert.c +73 -76
  436. data/third_party/boringssl/crypto/bn/div.c +136 -70
  437. data/third_party/boringssl/crypto/bn/exponentiation.c +86 -381
  438. data/third_party/boringssl/crypto/bn/gcd.c +213 -296
  439. data/third_party/boringssl/crypto/bn/generic.c +0 -80
  440. data/third_party/boringssl/crypto/bn/internal.h +15 -3
  441. data/third_party/boringssl/crypto/bn/montgomery.c +57 -207
  442. data/third_party/boringssl/crypto/bn/montgomery_inv.c +160 -0
  443. data/third_party/boringssl/crypto/bn/mul.c +2 -1
  444. data/third_party/boringssl/crypto/bn/prime.c +24 -8
  445. data/third_party/boringssl/crypto/bn/random.c +47 -33
  446. data/third_party/boringssl/crypto/bn/sqrt.c +4 -5
  447. data/third_party/boringssl/crypto/buf/buf.c +25 -21
  448. data/third_party/boringssl/crypto/bytestring/ber.c +1 -0
  449. data/third_party/boringssl/crypto/bytestring/cbb.c +50 -22
  450. data/third_party/boringssl/crypto/bytestring/cbs.c +28 -4
  451. data/third_party/boringssl/crypto/chacha/{chacha_generic.c → chacha.c} +56 -29
  452. data/third_party/boringssl/crypto/cipher/aead.c +11 -22
  453. data/third_party/boringssl/crypto/cipher/cipher.c +2 -2
  454. data/third_party/boringssl/crypto/cipher/e_aes.c +53 -103
  455. data/third_party/boringssl/crypto/cipher/e_chacha20poly1305.c +2 -8
  456. data/third_party/boringssl/crypto/cipher/e_des.c +3 -5
  457. data/third_party/boringssl/crypto/cipher/e_null.c +1 -1
  458. data/third_party/boringssl/crypto/cipher/e_rc2.c +1 -1
  459. data/third_party/boringssl/crypto/cipher/e_rc4.c +1 -1
  460. data/third_party/boringssl/crypto/cipher/e_ssl3.c +3 -63
  461. data/third_party/boringssl/crypto/cipher/e_tls.c +12 -83
  462. data/third_party/boringssl/crypto/cipher/internal.h +8 -10
  463. data/third_party/boringssl/crypto/cipher/tls_cbc.c +69 -40
  464. data/third_party/boringssl/crypto/conf/conf.c +2 -1
  465. data/third_party/boringssl/crypto/cpu-aarch64-linux.c +61 -0
  466. data/third_party/boringssl/crypto/cpu-arm-linux.c +360 -0
  467. data/third_party/boringssl/crypto/cpu-arm.c +0 -161
  468. data/third_party/boringssl/crypto/cpu-intel.c +5 -3
  469. data/third_party/boringssl/{ssl/test/scoped_types.h → crypto/cpu-ppc64le.c} +21 -9
  470. data/third_party/boringssl/crypto/crypto.c +29 -7
  471. data/third_party/boringssl/crypto/curve25519/curve25519.c +284 -242
  472. data/third_party/boringssl/crypto/curve25519/internal.h +64 -0
  473. data/third_party/boringssl/crypto/curve25519/spake25519.c +464 -0
  474. data/third_party/boringssl/crypto/curve25519/x25519-x86_64.c +21 -0
  475. data/third_party/boringssl/crypto/dh/check.c +22 -6
  476. data/third_party/boringssl/crypto/dh/dh.c +45 -21
  477. data/third_party/boringssl/crypto/dh/dh_asn1.c +96 -20
  478. data/third_party/boringssl/crypto/dh/params.c +30 -78
  479. data/third_party/boringssl/crypto/digest/digest.c +3 -3
  480. data/third_party/boringssl/crypto/dsa/dsa.c +59 -29
  481. data/third_party/boringssl/crypto/dsa/dsa_asn1.c +4 -0
  482. data/third_party/boringssl/crypto/ec/ec.c +84 -140
  483. data/third_party/boringssl/crypto/ec/ec_asn1.c +82 -52
  484. data/third_party/boringssl/crypto/ec/ec_key.c +15 -15
  485. data/third_party/boringssl/crypto/ec/ec_montgomery.c +87 -50
  486. data/third_party/boringssl/crypto/ec/internal.h +12 -36
  487. data/third_party/boringssl/crypto/ec/oct.c +11 -11
  488. data/third_party/boringssl/crypto/ec/p224-64.c +59 -116
  489. data/third_party/boringssl/crypto/ec/p256-64.c +88 -163
  490. data/third_party/boringssl/crypto/ec/p256-x86_64.c +46 -58
  491. data/third_party/boringssl/crypto/ec/simple.c +81 -201
  492. data/third_party/boringssl/crypto/ec/util-64.c +0 -74
  493. data/third_party/boringssl/crypto/ecdh/ecdh.c +7 -1
  494. data/third_party/boringssl/crypto/ecdsa/ecdsa.c +28 -46
  495. data/third_party/boringssl/crypto/ecdsa/ecdsa_asn1.c +1 -0
  496. data/third_party/boringssl/crypto/engine/engine.c +1 -1
  497. data/third_party/boringssl/crypto/err/err.c +3 -3
  498. data/third_party/boringssl/crypto/evp/evp.c +14 -59
  499. data/third_party/boringssl/crypto/evp/evp_asn1.c +144 -87
  500. data/third_party/boringssl/crypto/evp/evp_ctx.c +7 -7
  501. data/third_party/boringssl/crypto/evp/internal.h +4 -46
  502. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +8 -157
  503. data/third_party/boringssl/crypto/evp/p_ec.c +1 -1
  504. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +22 -170
  505. data/third_party/boringssl/crypto/evp/p_rsa.c +1 -1
  506. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +10 -548
  507. data/third_party/boringssl/crypto/evp/print.c +520 -0
  508. data/third_party/boringssl/crypto/ex_data.c +4 -6
  509. data/third_party/boringssl/crypto/hkdf/hkdf.c +38 -17
  510. data/third_party/boringssl/crypto/hmac/hmac.c +6 -6
  511. data/third_party/boringssl/crypto/internal.h +57 -77
  512. data/third_party/boringssl/crypto/lhash/lhash.c +6 -10
  513. data/third_party/boringssl/crypto/md4/md4.c +9 -0
  514. data/third_party/boringssl/crypto/mem.c +19 -19
  515. data/third_party/boringssl/crypto/modes/cfb.c +5 -6
  516. data/third_party/boringssl/crypto/modes/ctr.c +10 -18
  517. data/third_party/boringssl/crypto/modes/gcm.c +100 -66
  518. data/third_party/boringssl/crypto/modes/internal.h +15 -27
  519. data/third_party/boringssl/crypto/modes/ofb.c +9 -22
  520. data/third_party/boringssl/crypto/newhope/error_correction.c +131 -0
  521. data/third_party/boringssl/crypto/newhope/internal.h +71 -0
  522. data/third_party/boringssl/crypto/newhope/newhope.c +174 -0
  523. data/third_party/boringssl/crypto/newhope/ntt.c +148 -0
  524. data/third_party/boringssl/crypto/newhope/poly.c +183 -0
  525. data/third_party/boringssl/crypto/newhope/precomp.c +306 -0
  526. data/third_party/boringssl/crypto/newhope/reduce.c +42 -0
  527. data/third_party/boringssl/crypto/obj/obj.c +111 -135
  528. data/third_party/boringssl/crypto/obj/obj_dat.h +4 -10
  529. data/third_party/boringssl/crypto/pem/pem_lib.c +6 -43
  530. data/third_party/boringssl/crypto/pem/pem_pkey.c +10 -19
  531. data/third_party/boringssl/crypto/pkcs8/p5_pbe.c +1 -0
  532. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +2 -1
  533. data/third_party/boringssl/crypto/pkcs8/p8_pkey.c +2 -2
  534. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +95 -87
  535. data/third_party/boringssl/crypto/{test/test_util.h → poly1305/internal.h} +15 -10
  536. data/third_party/boringssl/crypto/poly1305/poly1305.c +8 -15
  537. data/third_party/boringssl/crypto/poly1305/poly1305_arm.c +1 -0
  538. data/third_party/boringssl/crypto/poly1305/poly1305_vec.c +3 -3
  539. data/third_party/boringssl/crypto/rand/deterministic.c +47 -0
  540. data/third_party/boringssl/crypto/rand/rand.c +4 -1
  541. data/third_party/boringssl/crypto/rand/urandom.c +5 -7
  542. data/third_party/boringssl/crypto/rand/windows.c +5 -8
  543. data/third_party/boringssl/crypto/rc4/rc4.c +24 -209
  544. data/third_party/boringssl/crypto/refcount_lock.c +2 -2
  545. data/third_party/boringssl/crypto/rsa/blinding.c +74 -232
  546. data/third_party/boringssl/crypto/rsa/internal.h +5 -13
  547. data/third_party/boringssl/crypto/rsa/padding.c +64 -63
  548. data/third_party/boringssl/crypto/rsa/rsa.c +50 -28
  549. data/third_party/boringssl/crypto/rsa/rsa_asn1.c +8 -16
  550. data/third_party/boringssl/crypto/rsa/rsa_impl.c +134 -122
  551. data/third_party/boringssl/crypto/sha/sha256.c +2 -2
  552. data/third_party/boringssl/crypto/sha/sha512.c +7 -7
  553. data/third_party/boringssl/crypto/stack/stack.c +13 -22
  554. data/third_party/boringssl/crypto/thread.c +21 -12
  555. data/third_party/boringssl/crypto/thread_none.c +6 -2
  556. data/third_party/boringssl/crypto/thread_pthread.c +16 -7
  557. data/third_party/boringssl/crypto/thread_win.c +38 -85
  558. data/third_party/boringssl/crypto/x509/a_sign.c +3 -3
  559. data/third_party/boringssl/crypto/x509/a_strex.c +1 -1
  560. data/third_party/boringssl/crypto/x509/a_verify.c +2 -2
  561. data/third_party/boringssl/crypto/{evp → x509}/algorithm.c +37 -53
  562. data/third_party/boringssl/crypto/x509/asn1_gen.c +1 -2
  563. data/third_party/boringssl/crypto/x509/by_dir.c +6 -6
  564. data/third_party/boringssl/crypto/x509/internal.h +66 -0
  565. data/third_party/boringssl/crypto/x509/rsa_pss.c +385 -0
  566. data/third_party/boringssl/crypto/x509/t_x509.c +10 -12
  567. data/third_party/boringssl/crypto/x509/x509.c +5 -0
  568. data/third_party/boringssl/crypto/x509/x509_att.c +9 -3
  569. data/third_party/boringssl/crypto/x509/x509_lu.c +34 -44
  570. data/third_party/boringssl/crypto/x509/x509_obj.c +19 -2
  571. data/third_party/boringssl/crypto/x509/x509_r2x.c +9 -5
  572. data/third_party/boringssl/crypto/x509/x509_set.c +5 -0
  573. data/third_party/boringssl/crypto/x509/x509_txt.c +5 -0
  574. data/third_party/boringssl/crypto/x509/x509_vfy.c +63 -32
  575. data/third_party/boringssl/crypto/x509/x509_vpm.c +29 -18
  576. data/third_party/boringssl/crypto/x509/x509cset.c +2 -1
  577. data/third_party/boringssl/crypto/x509/x_crl.c +2 -2
  578. data/third_party/boringssl/crypto/x509/x_name.c +14 -17
  579. data/third_party/boringssl/crypto/x509/x_pubkey.c +10 -7
  580. data/third_party/boringssl/crypto/x509/x_x509.c +67 -6
  581. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +2 -2
  582. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +2 -1
  583. data/third_party/boringssl/crypto/x509v3/v3_conf.c +4 -3
  584. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +5 -0
  585. data/third_party/boringssl/crypto/x509v3/v3_prn.c +0 -3
  586. data/third_party/boringssl/crypto/x509v3/v3_purp.c +2 -2
  587. data/third_party/boringssl/crypto/x509v3/v3_utl.c +2 -1
  588. data/third_party/boringssl/include/openssl/aead.h +72 -73
  589. data/third_party/boringssl/include/openssl/arm_arch.h +0 -6
  590. data/third_party/boringssl/include/openssl/asn1.h +103 -235
  591. data/third_party/boringssl/include/openssl/asn1_mac.h +17 -74
  592. data/third_party/boringssl/include/openssl/asn1t.h +1 -11
  593. data/third_party/boringssl/include/openssl/base.h +145 -3
  594. data/third_party/boringssl/include/openssl/base64.h +20 -17
  595. data/third_party/boringssl/include/openssl/bio.h +59 -34
  596. data/third_party/boringssl/include/openssl/bn.h +118 -51
  597. data/third_party/boringssl/include/openssl/buf.h +15 -0
  598. data/third_party/boringssl/include/openssl/bytestring.h +52 -4
  599. data/third_party/boringssl/include/openssl/chacha.h +2 -2
  600. data/third_party/boringssl/include/openssl/cipher.h +18 -1
  601. data/third_party/boringssl/include/openssl/cmac.h +11 -0
  602. data/third_party/boringssl/include/openssl/conf.h +13 -2
  603. data/third_party/boringssl/include/openssl/cpu.h +20 -23
  604. data/third_party/boringssl/include/openssl/crypto.h +22 -1
  605. data/third_party/boringssl/include/openssl/curve25519.h +96 -4
  606. data/third_party/boringssl/include/openssl/dh.h +71 -16
  607. data/third_party/boringssl/include/openssl/digest.h +38 -11
  608. data/third_party/boringssl/include/openssl/dsa.h +40 -4
  609. data/third_party/boringssl/include/openssl/ec.h +44 -18
  610. data/third_party/boringssl/include/openssl/ec_key.h +27 -6
  611. data/third_party/boringssl/include/openssl/ecdsa.h +11 -0
  612. data/third_party/boringssl/include/openssl/engine.h +11 -0
  613. data/third_party/boringssl/include/openssl/evp.h +52 -88
  614. data/third_party/boringssl/include/openssl/hkdf.h +24 -4
  615. data/third_party/boringssl/include/openssl/hmac.h +20 -6
  616. data/third_party/boringssl/include/openssl/md4.h +4 -0
  617. data/third_party/boringssl/include/openssl/mem.h +19 -0
  618. data/third_party/boringssl/include/openssl/newhope.h +158 -0
  619. data/third_party/boringssl/include/openssl/nid.h +4166 -0
  620. data/third_party/boringssl/include/openssl/obj.h +31 -3
  621. data/third_party/boringssl/include/openssl/obj_mac.h +17 -4143
  622. data/third_party/boringssl/include/openssl/{opensslfeatures.h → opensslconf.h} +3 -3
  623. data/third_party/boringssl/include/openssl/pem.h +5 -0
  624. data/third_party/boringssl/include/openssl/pkcs8.h +12 -0
  625. data/third_party/boringssl/include/openssl/rand.h +6 -0
  626. data/third_party/boringssl/include/openssl/rc4.h +6 -0
  627. data/third_party/boringssl/{crypto/dh/internal.h → include/openssl/ripemd.h} +38 -11
  628. data/third_party/boringssl/include/openssl/rsa.h +127 -65
  629. data/third_party/boringssl/include/openssl/sha.h +14 -10
  630. data/third_party/boringssl/include/openssl/ssl.h +561 -275
  631. data/third_party/boringssl/include/openssl/ssl3.h +18 -25
  632. data/third_party/boringssl/include/openssl/stack.h +2 -4
  633. data/third_party/boringssl/include/openssl/stack_macros.h +321 -353
  634. data/third_party/boringssl/include/openssl/thread.h +31 -13
  635. data/third_party/boringssl/include/openssl/time_support.h +1 -0
  636. data/third_party/boringssl/include/openssl/tls1.h +37 -33
  637. data/third_party/boringssl/include/openssl/x509.h +69 -26
  638. data/third_party/boringssl/include/openssl/x509_vfy.h +12 -10
  639. data/third_party/boringssl/include/openssl/x509v3.h +23 -2
  640. data/third_party/boringssl/ssl/custom_extensions.c +3 -5
  641. data/third_party/boringssl/ssl/d1_both.c +463 -499
  642. data/third_party/boringssl/ssl/d1_lib.c +38 -109
  643. data/third_party/boringssl/ssl/d1_pkt.c +173 -334
  644. data/third_party/boringssl/ssl/d1_srtp.c +20 -18
  645. data/third_party/boringssl/ssl/{d1_meth.c → dtls_method.c} +88 -15
  646. data/third_party/boringssl/ssl/dtls_record.c +27 -26
  647. data/third_party/boringssl/ssl/{s3_clnt.c → handshake_client.c} +816 -904
  648. data/third_party/boringssl/ssl/handshake_server.c +1932 -0
  649. data/third_party/boringssl/ssl/internal.h +712 -439
  650. data/third_party/boringssl/ssl/s3_both.c +445 -257
  651. data/third_party/boringssl/ssl/s3_enc.c +53 -36
  652. data/third_party/boringssl/ssl/s3_lib.c +23 -268
  653. data/third_party/boringssl/ssl/s3_pkt.c +168 -364
  654. data/third_party/boringssl/ssl/ssl_aead_ctx.c +46 -17
  655. data/third_party/boringssl/ssl/ssl_asn1.c +56 -26
  656. data/third_party/boringssl/ssl/ssl_buffer.c +16 -24
  657. data/third_party/boringssl/ssl/ssl_cert.c +324 -49
  658. data/third_party/boringssl/ssl/ssl_cipher.c +205 -150
  659. data/third_party/boringssl/ssl/ssl_ecdh.c +287 -51
  660. data/third_party/boringssl/ssl/ssl_file.c +21 -68
  661. data/third_party/boringssl/ssl/ssl_lib.c +881 -510
  662. data/third_party/boringssl/ssl/ssl_rsa.c +404 -34
  663. data/third_party/boringssl/ssl/ssl_session.c +324 -103
  664. data/third_party/boringssl/ssl/ssl_stat.c +6 -88
  665. data/third_party/boringssl/ssl/t1_enc.c +23 -39
  666. data/third_party/boringssl/ssl/t1_lib.c +1120 -622
  667. data/third_party/boringssl/ssl/tls13_both.c +440 -0
  668. data/third_party/boringssl/ssl/tls13_client.c +682 -0
  669. data/third_party/boringssl/ssl/tls13_enc.c +391 -0
  670. data/third_party/boringssl/ssl/tls13_server.c +672 -0
  671. data/third_party/boringssl/ssl/{s3_meth.c → tls_method.c} +100 -21
  672. data/third_party/boringssl/ssl/tls_record.c +159 -77
  673. data/third_party/nanopb/pb.h +60 -28
  674. data/third_party/nanopb/pb_decode.c +120 -92
  675. data/third_party/nanopb/pb_decode.h +3 -3
  676. data/third_party/nanopb/pb_encode.c +73 -67
  677. data/third_party/nanopb/pb_encode.h +4 -4
  678. metadata +155 -89
  679. data/include/grpc/impl/codegen/byte_buffer.h +0 -122
  680. data/include/grpc/impl/codegen/log.h +0 -118
  681. data/include/grpc/impl/codegen/time.h +0 -130
  682. data/src/core/ext/client_config/client_channel.c +0 -593
  683. data/src/core/ext/client_config/subchannel_call_holder.c +0 -272
  684. data/src/core/ext/client_config/subchannel_call_holder.h +0 -99
  685. data/src/core/lib/iomgr/ev_poll_and_epoll_posix.c +0 -2046
  686. data/src/core/lib/iomgr/workqueue_posix.c +0 -151
  687. data/src/core/lib/security/transport/handshake.c +0 -368
  688. data/third_party/boringssl/crypto/asn1/a_bytes.c +0 -308
  689. data/third_party/boringssl/crypto/asn1/bio_asn1.c +0 -477
  690. data/third_party/boringssl/crypto/asn1/bio_ndef.c +0 -251
  691. data/third_party/boringssl/crypto/asn1/t_pkey.c +0 -110
  692. data/third_party/boringssl/crypto/asn1/tasn_prn.c +0 -596
  693. data/third_party/boringssl/crypto/chacha/chacha_vec.c +0 -328
  694. data/third_party/boringssl/crypto/directory.h +0 -66
  695. data/third_party/boringssl/crypto/directory_posix.c +0 -108
  696. data/third_party/boringssl/crypto/directory_win.c +0 -144
  697. data/third_party/boringssl/crypto/test/scoped_types.h +0 -140
  698. data/third_party/boringssl/include/openssl/pqueue.h +0 -146
  699. data/third_party/boringssl/ssl/d1_clnt.c +0 -561
  700. data/third_party/boringssl/ssl/d1_srvr.c +0 -476
  701. data/third_party/boringssl/ssl/pqueue/pqueue.c +0 -197
  702. data/third_party/boringssl/ssl/s3_srvr.c +0 -2272
  703. data/third_party/boringssl/ssl/test/async_bio.h +0 -45
  704. data/third_party/boringssl/ssl/test/packeted_bio.h +0 -44
  705. data/third_party/boringssl/ssl/test/test_config.h +0 -110
@@ -110,39 +110,21 @@ const char *SSL_state_string_long(const SSL *ssl) {
110
110
  case SSL3_ST_CR_SRVR_HELLO_A:
111
111
  return "SSLv3 read server hello A";
112
112
 
113
- case SSL3_ST_CR_SRVR_HELLO_B:
114
- return "SSLv3 read server hello B";
115
-
116
113
  case SSL3_ST_CR_CERT_A:
117
114
  return "SSLv3 read server certificate A";
118
115
 
119
- case SSL3_ST_CR_CERT_B:
120
- return "SSLv3 read server certificate B";
121
-
122
116
  case SSL3_ST_CR_KEY_EXCH_A:
123
117
  return "SSLv3 read server key exchange A";
124
118
 
125
- case SSL3_ST_CR_KEY_EXCH_B:
126
- return "SSLv3 read server key exchange B";
127
-
128
119
  case SSL3_ST_CR_CERT_REQ_A:
129
120
  return "SSLv3 read server certificate request A";
130
121
 
131
- case SSL3_ST_CR_CERT_REQ_B:
132
- return "SSLv3 read server certificate request B";
133
-
134
122
  case SSL3_ST_CR_SESSION_TICKET_A:
135
123
  return "SSLv3 read server session ticket A";
136
124
 
137
- case SSL3_ST_CR_SESSION_TICKET_B:
138
- return "SSLv3 read server session ticket B";
139
-
140
125
  case SSL3_ST_CR_SRVR_DONE_A:
141
126
  return "SSLv3 read server done A";
142
127
 
143
- case SSL3_ST_CR_SRVR_DONE_B:
144
- return "SSLv3 read server done B";
145
-
146
128
  case SSL3_ST_CW_CERT_A:
147
129
  return "SSLv3 write client certificate A";
148
130
 
@@ -152,9 +134,6 @@ const char *SSL_state_string_long(const SSL *ssl) {
152
134
  case SSL3_ST_CW_CERT_C:
153
135
  return "SSLv3 write client certificate C";
154
136
 
155
- case SSL3_ST_CW_CERT_D:
156
- return "SSLv3 write client certificate D";
157
-
158
137
  case SSL3_ST_CW_KEY_EXCH_A:
159
138
  return "SSLv3 write client key exchange A";
160
139
 
@@ -167,13 +146,9 @@ const char *SSL_state_string_long(const SSL *ssl) {
167
146
  case SSL3_ST_CW_CERT_VRFY_B:
168
147
  return "SSLv3 write certificate verify B";
169
148
 
170
- case SSL3_ST_CW_CHANGE_A:
171
- case SSL3_ST_SW_CHANGE_A:
172
- return "SSLv3 write change cipher spec A";
173
-
174
- case SSL3_ST_CW_CHANGE_B:
175
- case SSL3_ST_SW_CHANGE_B:
176
- return "SSLv3 write change cipher spec B";
149
+ case SSL3_ST_CW_CHANGE:
150
+ case SSL3_ST_SW_CHANGE:
151
+ return "SSLv3 write change cipher spec";
177
152
 
178
153
  case SSL3_ST_CW_FINISHED_A:
179
154
  case SSL3_ST_SW_FINISHED_A:
@@ -191,10 +166,6 @@ const char *SSL_state_string_long(const SSL *ssl) {
191
166
  case SSL3_ST_SR_FINISHED_A:
192
167
  return "SSLv3 read finished A";
193
168
 
194
- case SSL3_ST_CR_FINISHED_B:
195
- case SSL3_ST_SR_FINISHED_B:
196
- return "SSLv3 read finished B";
197
-
198
169
  case SSL3_ST_CW_FLUSH:
199
170
  case SSL3_ST_SW_FLUSH:
200
171
  return "SSLv3 flush data";
@@ -208,9 +179,6 @@ const char *SSL_state_string_long(const SSL *ssl) {
208
179
  case SSL3_ST_SR_CLNT_HELLO_C:
209
180
  return "SSLv3 read client hello C";
210
181
 
211
- case SSL3_ST_SR_CLNT_HELLO_D:
212
- return "SSLv3 read client hello D";
213
-
214
182
  case SSL3_ST_SW_HELLO_REQ_A:
215
183
  return "SSLv3 write hello request A";
216
184
 
@@ -259,9 +227,6 @@ const char *SSL_state_string_long(const SSL *ssl) {
259
227
  case SSL3_ST_SR_CERT_A:
260
228
  return "SSLv3 read client certificate A";
261
229
 
262
- case SSL3_ST_SR_CERT_B:
263
- return "SSLv3 read client certificate B";
264
-
265
230
  case SSL3_ST_SR_KEY_EXCH_A:
266
231
  return "SSLv3 read client key exchange A";
267
232
 
@@ -271,16 +236,10 @@ const char *SSL_state_string_long(const SSL *ssl) {
271
236
  case SSL3_ST_SR_CERT_VRFY_A:
272
237
  return "SSLv3 read certificate verify A";
273
238
 
274
- case SSL3_ST_SR_CERT_VRFY_B:
275
- return "SSLv3 read certificate verify B";
276
-
277
239
  /* DTLS */
278
240
  case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
279
241
  return "DTLS1 read hello verify request A";
280
242
 
281
- case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
282
- return "DTLS1 read hello verify request B";
283
-
284
243
  default:
285
244
  return "unknown state";
286
245
  }
@@ -311,33 +270,18 @@ const char *SSL_state_string(const SSL *ssl) {
311
270
  case SSL3_ST_CR_SRVR_HELLO_A:
312
271
  return "3RSH_A";
313
272
 
314
- case SSL3_ST_CR_SRVR_HELLO_B:
315
- return "3RSH_B";
316
-
317
273
  case SSL3_ST_CR_CERT_A:
318
274
  return "3RSC_A";
319
275
 
320
- case SSL3_ST_CR_CERT_B:
321
- return "3RSC_B";
322
-
323
276
  case SSL3_ST_CR_KEY_EXCH_A:
324
277
  return "3RSKEA";
325
278
 
326
- case SSL3_ST_CR_KEY_EXCH_B:
327
- return "3RSKEB";
328
-
329
279
  case SSL3_ST_CR_CERT_REQ_A:
330
280
  return "3RCR_A";
331
281
 
332
- case SSL3_ST_CR_CERT_REQ_B:
333
- return "3RCR_B";
334
-
335
282
  case SSL3_ST_CR_SRVR_DONE_A:
336
283
  return "3RSD_A";
337
284
 
338
- case SSL3_ST_CR_SRVR_DONE_B:
339
- return "3RSD_B";
340
-
341
285
  case SSL3_ST_CW_CERT_A:
342
286
  return "3WCC_A";
343
287
 
@@ -347,9 +291,6 @@ const char *SSL_state_string(const SSL *ssl) {
347
291
  case SSL3_ST_CW_CERT_C:
348
292
  return "3WCC_C";
349
293
 
350
- case SSL3_ST_CW_CERT_D:
351
- return "3WCC_D";
352
-
353
294
  case SSL3_ST_CW_KEY_EXCH_A:
354
295
  return "3WCKEA";
355
296
 
@@ -362,13 +303,9 @@ const char *SSL_state_string(const SSL *ssl) {
362
303
  case SSL3_ST_CW_CERT_VRFY_B:
363
304
  return "3WCV_B";
364
305
 
365
- case SSL3_ST_SW_CHANGE_A:
366
- case SSL3_ST_CW_CHANGE_A:
367
- return "3WCCSA";
368
-
369
- case SSL3_ST_SW_CHANGE_B:
370
- case SSL3_ST_CW_CHANGE_B:
371
- return "3WCCSB";
306
+ case SSL3_ST_SW_CHANGE:
307
+ case SSL3_ST_CW_CHANGE:
308
+ return "3WCCS_";
372
309
 
373
310
  case SSL3_ST_SW_FINISHED_A:
374
311
  case SSL3_ST_CW_FINISHED_A:
@@ -386,10 +323,6 @@ const char *SSL_state_string(const SSL *ssl) {
386
323
  case SSL3_ST_CR_FINISHED_A:
387
324
  return "3RFINA";
388
325
 
389
- case SSL3_ST_SR_FINISHED_B:
390
- case SSL3_ST_CR_FINISHED_B:
391
- return "3RFINB";
392
-
393
326
  case SSL3_ST_SW_HELLO_REQ_A:
394
327
  return "3WHR_A";
395
328
 
@@ -408,9 +341,6 @@ const char *SSL_state_string(const SSL *ssl) {
408
341
  case SSL3_ST_SR_CLNT_HELLO_C:
409
342
  return "3RCH_C";
410
343
 
411
- case SSL3_ST_SR_CLNT_HELLO_D:
412
- return "3RCH_D";
413
-
414
344
  case SSL3_ST_SW_SRVR_HELLO_A:
415
345
  return "3WSH_A";
416
346
 
@@ -444,28 +374,16 @@ const char *SSL_state_string(const SSL *ssl) {
444
374
  case SSL3_ST_SR_CERT_A:
445
375
  return "3RCC_A";
446
376
 
447
- case SSL3_ST_SR_CERT_B:
448
- return "3RCC_B";
449
-
450
377
  case SSL3_ST_SR_KEY_EXCH_A:
451
378
  return "3RCKEA";
452
379
 
453
- case SSL3_ST_SR_KEY_EXCH_B:
454
- return "3RCKEB";
455
-
456
380
  case SSL3_ST_SR_CERT_VRFY_A:
457
381
  return "3RCV_A";
458
382
 
459
- case SSL3_ST_SR_CERT_VRFY_B:
460
- return "3RCV_B";
461
-
462
383
  /* DTLS */
463
384
  case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
464
385
  return "DRCHVA";
465
386
 
466
- case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
467
- return "DRCHVB";
468
-
469
387
  default:
470
388
  return "UNKWN ";
471
389
  }
@@ -136,7 +136,6 @@
136
136
  #include <openssl/ssl.h>
137
137
 
138
138
  #include <assert.h>
139
- #include <stdio.h>
140
139
  #include <string.h>
141
140
 
142
141
  #include <openssl/err.h>
@@ -144,7 +143,7 @@
144
143
  #include <openssl/hmac.h>
145
144
  #include <openssl/md5.h>
146
145
  #include <openssl/mem.h>
147
- #include <openssl/obj.h>
146
+ #include <openssl/nid.h>
148
147
  #include <openssl/rand.h>
149
148
 
150
149
  #include "internal.h"
@@ -314,11 +313,10 @@ int tls1_change_cipher_state(SSL *ssl, int which) {
314
313
  }
315
314
 
316
315
  if (is_read) {
317
- ssl_set_read_state(ssl, aead_ctx);
318
- } else {
319
- ssl_set_write_state(ssl, aead_ctx);
316
+ return ssl->method->set_read_state(ssl, aead_ctx);
320
317
  }
321
- return 1;
318
+
319
+ return ssl->method->set_write_state(ssl, aead_ctx);
322
320
  }
323
321
 
324
322
  size_t SSL_get_key_block_len(const SSL *ssl) {
@@ -329,8 +327,8 @@ size_t SSL_get_key_block_len(const SSL *ssl) {
329
327
 
330
328
  int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) {
331
329
  return ssl->s3->enc_method->prf(
332
- ssl, out, out_len, ssl->session->master_key,
333
- ssl->session->master_key_length, TLS_MD_KEY_EXPANSION_CONST,
330
+ ssl, out, out_len, SSL_get_session(ssl)->master_key,
331
+ SSL_get_session(ssl)->master_key_length, TLS_MD_KEY_EXPANSION_CONST,
334
332
  TLS_MD_KEY_EXPANSION_CONST_SIZE, ssl->s3->server_random, SSL3_RANDOM_SIZE,
335
333
  ssl->s3->client_random, SSL3_RANDOM_SIZE);
336
334
  }
@@ -340,12 +338,16 @@ int tls1_setup_key_block(SSL *ssl) {
340
338
  return 1;
341
339
  }
342
340
 
341
+ SSL_SESSION *session = ssl->session;
342
+ if (ssl->s3->new_session != NULL) {
343
+ session = ssl->s3->new_session;
344
+ }
345
+
343
346
  const EVP_AEAD *aead = NULL;
344
347
  size_t mac_secret_len, fixed_iv_len;
345
- if (ssl->session->cipher == NULL ||
348
+ if (session->cipher == NULL ||
346
349
  !ssl_cipher_get_evp_aead(&aead, &mac_secret_len, &fixed_iv_len,
347
- ssl->session->cipher,
348
- ssl3_protocol_version(ssl))) {
350
+ session->cipher, ssl3_protocol_version(ssl))) {
349
351
  OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
350
352
  return 0;
351
353
  }
@@ -390,29 +392,6 @@ int tls1_setup_key_block(SSL *ssl) {
390
392
  return 1;
391
393
  }
392
394
 
393
- static int tls1_cert_verify_mac(SSL *ssl, int md_nid, uint8_t *out) {
394
- const EVP_MD_CTX *ctx_template;
395
- if (md_nid == NID_md5) {
396
- ctx_template = &ssl->s3->handshake_md5;
397
- } else if (md_nid == EVP_MD_CTX_type(&ssl->s3->handshake_hash)) {
398
- ctx_template = &ssl->s3->handshake_hash;
399
- } else {
400
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_REQUIRED_DIGEST);
401
- return 0;
402
- }
403
-
404
- EVP_MD_CTX ctx;
405
- EVP_MD_CTX_init(&ctx);
406
- if (!EVP_MD_CTX_copy_ex(&ctx, ctx_template)) {
407
- EVP_MD_CTX_cleanup(&ctx);
408
- return 0;
409
- }
410
- unsigned ret;
411
- EVP_DigestFinal_ex(&ctx, out, &ret);
412
- EVP_MD_CTX_cleanup(&ctx);
413
- return ret;
414
- }
415
-
416
395
  static int append_digest(const EVP_MD_CTX *ctx, uint8_t *out, size_t *out_len,
417
396
  size_t max_out) {
418
397
  int ret = 0;
@@ -479,8 +458,8 @@ static int tls1_final_finish_mac(SSL *ssl, int from_server, uint8_t *out) {
479
458
 
480
459
  static const size_t kFinishedLen = 12;
481
460
  if (!ssl->s3->enc_method->prf(ssl, out, kFinishedLen,
482
- ssl->session->master_key,
483
- ssl->session->master_key_length, label,
461
+ SSL_get_session(ssl)->master_key,
462
+ SSL_get_session(ssl)->master_key_length, label,
484
463
  label_len, buf, digests_len, NULL, 0)) {
485
464
  return 0;
486
465
  }
@@ -526,6 +505,11 @@ int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
526
505
  return 0;
527
506
  }
528
507
 
508
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
509
+ return tls13_export_keying_material(ssl, out, out_len, label, label_len,
510
+ context, context_len, use_context);
511
+ }
512
+
529
513
  size_t seed_len = 2 * SSL3_RANDOM_SIZE;
530
514
  if (use_context) {
531
515
  if (context_len >= 1u << 16) {
@@ -549,8 +533,9 @@ int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
549
533
  }
550
534
 
551
535
  int ret =
552
- ssl->s3->enc_method->prf(ssl, out, out_len, ssl->session->master_key,
553
- ssl->session->master_key_length, label,
536
+ ssl->s3->enc_method->prf(ssl, out, out_len,
537
+ SSL_get_session(ssl)->master_key,
538
+ SSL_get_session(ssl)->master_key_length, label,
554
539
  label_len, seed, seed_len, NULL, 0);
555
540
  OPENSSL_free(seed);
556
541
  return ret;
@@ -559,5 +544,4 @@ int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
559
544
  const SSL3_ENC_METHOD TLSv1_enc_data = {
560
545
  tls1_prf,
561
546
  tls1_final_finish_mac,
562
- tls1_cert_verify_mac,
563
547
  };
@@ -110,7 +110,6 @@
110
110
 
111
111
  #include <assert.h>
112
112
  #include <limits.h>
113
- #include <stdio.h>
114
113
  #include <stdlib.h>
115
114
  #include <string.h>
116
115
 
@@ -120,11 +119,12 @@
120
119
  #include <openssl/evp.h>
121
120
  #include <openssl/hmac.h>
122
121
  #include <openssl/mem.h>
123
- #include <openssl/obj.h>
122
+ #include <openssl/nid.h>
124
123
  #include <openssl/rand.h>
125
124
  #include <openssl/type_check.h>
126
125
 
127
126
  #include "internal.h"
127
+ #include "../crypto/internal.h"
128
128
 
129
129
 
130
130
  static int ssl_check_clienthello_tlsext(SSL *ssl);
@@ -203,45 +203,46 @@ done:
203
203
  return ret;
204
204
  }
205
205
 
206
- char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
207
- CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
206
+ int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
207
+ const uint8_t *in, size_t in_len) {
208
+ memset(ctx, 0, sizeof(*ctx));
209
+ ctx->ssl = ssl;
210
+ ctx->client_hello = in;
211
+ ctx->client_hello_len = in_len;
208
212
 
213
+ CBS client_hello, random, session_id;
209
214
  CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
210
-
211
- if (/* Skip client version. */
212
- !CBS_skip(&client_hello, 2) ||
213
- /* Skip client nonce. */
214
- !CBS_skip(&client_hello, 32) ||
215
- /* Extract session_id. */
216
- !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
215
+ if (!CBS_get_u16(&client_hello, &ctx->version) ||
216
+ !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
217
+ !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
218
+ CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
217
219
  return 0;
218
220
  }
219
221
 
222
+ ctx->random = CBS_data(&random);
223
+ ctx->random_len = CBS_len(&random);
220
224
  ctx->session_id = CBS_data(&session_id);
221
225
  ctx->session_id_len = CBS_len(&session_id);
222
226
 
223
227
  /* Skip past DTLS cookie */
224
- if (SSL_IS_DTLS(ctx->ssl)) {
228
+ if (SSL_is_dtls(ctx->ssl)) {
225
229
  CBS cookie;
226
-
227
- if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
230
+ if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
231
+ CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
228
232
  return 0;
229
233
  }
230
234
  }
231
235
 
232
- /* Extract cipher_suites. */
236
+ CBS cipher_suites, compression_methods;
233
237
  if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
234
- CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
238
+ CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
239
+ !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
240
+ CBS_len(&compression_methods) < 1) {
235
241
  return 0;
236
242
  }
243
+
237
244
  ctx->cipher_suites = CBS_data(&cipher_suites);
238
245
  ctx->cipher_suites_len = CBS_len(&cipher_suites);
239
-
240
- /* Extract compression_methods. */
241
- if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
242
- CBS_len(&compression_methods) < 1) {
243
- return 0;
244
- }
245
246
  ctx->compression_methods = CBS_data(&compression_methods);
246
247
  ctx->compression_methods_len = CBS_len(&compression_methods);
247
248
 
@@ -254,37 +255,34 @@ char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
254
255
  }
255
256
 
256
257
  /* Extract extensions and check it is valid. */
258
+ CBS extensions;
257
259
  if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
258
260
  !tls1_check_duplicate_extensions(&extensions) ||
259
261
  CBS_len(&client_hello) != 0) {
260
262
  return 0;
261
263
  }
264
+
262
265
  ctx->extensions = CBS_data(&extensions);
263
266
  ctx->extensions_len = CBS_len(&extensions);
264
267
 
265
268
  return 1;
266
269
  }
267
270
 
268
- int SSL_early_callback_ctx_extension_get(
269
- const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
270
- const uint8_t **out_data, size_t *out_len) {
271
+ int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
272
+ CBS *out, uint16_t extension_type) {
271
273
  CBS extensions;
272
-
273
274
  CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
274
-
275
275
  while (CBS_len(&extensions) != 0) {
276
+ /* Decode the next extension. */
276
277
  uint16_t type;
277
278
  CBS extension;
278
-
279
- /* Decode the next extension. */
280
279
  if (!CBS_get_u16(&extensions, &type) ||
281
280
  !CBS_get_u16_length_prefixed(&extensions, &extension)) {
282
281
  return 0;
283
282
  }
284
283
 
285
284
  if (type == extension_type) {
286
- *out_data = CBS_data(&extension);
287
- *out_len = CBS_len(&extension);
285
+ *out = extension;
288
286
  return 1;
289
287
  }
290
288
  }
@@ -292,7 +290,20 @@ int SSL_early_callback_ctx_extension_get(
292
290
  return 0;
293
291
  }
294
292
 
295
- static const uint16_t eccurves_default[] = {
293
+ int SSL_early_callback_ctx_extension_get(
294
+ const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
295
+ const uint8_t **out_data, size_t *out_len) {
296
+ CBS cbs;
297
+ if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
298
+ return 0;
299
+ }
300
+
301
+ *out_data = CBS_data(&cbs);
302
+ *out_len = CBS_len(&cbs);
303
+ return 1;
304
+ }
305
+
306
+ static const uint16_t kDefaultGroups[] = {
296
307
  SSL_CURVE_X25519,
297
308
  SSL_CURVE_SECP256R1,
298
309
  SSL_CURVE_SECP384R1,
@@ -301,68 +312,65 @@ static const uint16_t eccurves_default[] = {
301
312
  #endif
302
313
  };
303
314
 
304
- /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
305
- * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
306
- * peer's curve list. Otherwise, return the preferred list. */
307
- static void tls1_get_curvelist(SSL *ssl, int get_peer_curves,
308
- const uint16_t **out_curve_ids,
309
- size_t *out_curve_ids_len) {
310
- if (get_peer_curves) {
311
- /* Only clients send a curve list, so this function is only called
312
- * on the server. */
315
+ void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
316
+ const uint16_t **out_group_ids,
317
+ size_t *out_group_ids_len) {
318
+ if (get_peer_groups) {
319
+ /* Only clients send a supported group list, so this function is only
320
+ * called on the server. */
313
321
  assert(ssl->server);
314
- *out_curve_ids = ssl->s3->tmp.peer_ellipticcurvelist;
315
- *out_curve_ids_len = ssl->s3->tmp.peer_ellipticcurvelist_length;
322
+ *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
323
+ *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
316
324
  return;
317
325
  }
318
326
 
319
- *out_curve_ids = ssl->tlsext_ellipticcurvelist;
320
- *out_curve_ids_len = ssl->tlsext_ellipticcurvelist_length;
321
- if (!*out_curve_ids) {
322
- *out_curve_ids = eccurves_default;
323
- *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
327
+ *out_group_ids = ssl->supported_group_list;
328
+ *out_group_ids_len = ssl->supported_group_list_len;
329
+ if (!*out_group_ids) {
330
+ *out_group_ids = kDefaultGroups;
331
+ *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
324
332
  }
325
333
  }
326
334
 
327
- int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
328
- const uint16_t *curves, *peer_curves, *pref, *supp;
329
- size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
335
+ int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
336
+ const uint16_t *groups, *peer_groups, *pref, *supp;
337
+ size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
330
338
 
331
339
  /* Can't do anything on client side */
332
340
  if (ssl->server == 0) {
333
341
  return 0;
334
342
  }
335
343
 
336
- tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len);
337
- tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len);
344
+ tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
345
+ tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
338
346
 
339
- if (peer_curves_len == 0) {
340
- /* Clients are not required to send a supported_curves extension. In this
341
- * case, the server is free to pick any curve it likes. See RFC 4492,
347
+ if (peer_groups_len == 0) {
348
+ /* Clients are not required to send a supported_groups extension. In this
349
+ * case, the server is free to pick any group it likes. See RFC 4492,
342
350
  * section 4, paragraph 3.
343
351
  *
344
352
  * However, in the interests of compatibility, we will skip ECDH if the
345
353
  * client didn't send an extension because we can't be sure that they'll
346
- * support our favoured curve. */
354
+ * support our favoured group. */
347
355
  return 0;
348
356
  }
349
357
 
350
358
  if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
351
- pref = curves;
352
- pref_len = curves_len;
353
- supp = peer_curves;
354
- supp_len = peer_curves_len;
359
+ pref = groups;
360
+ pref_len = groups_len;
361
+ supp = peer_groups;
362
+ supp_len = peer_groups_len;
355
363
  } else {
356
- pref = peer_curves;
357
- pref_len = peer_curves_len;
358
- supp = curves;
359
- supp_len = curves_len;
364
+ pref = peer_groups;
365
+ pref_len = peer_groups_len;
366
+ supp = groups;
367
+ supp_len = groups_len;
360
368
  }
361
369
 
362
370
  for (i = 0; i < pref_len; i++) {
363
371
  for (j = 0; j < supp_len; j++) {
364
372
  if (pref[i] == supp[j]) {
365
- *out_curve_id = pref[i];
373
+ *out_group_id = pref[i];
366
374
  return 1;
367
375
  }
368
376
  }
@@ -371,34 +379,33 @@ int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) {
371
379
  return 0;
372
380
  }
373
381
 
374
- int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
382
+ int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
375
383
  const int *curves, size_t ncurves) {
376
- uint16_t *curve_ids;
377
- size_t i;
384
+ uint16_t *group_ids;
378
385
 
379
- curve_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
380
- if (curve_ids == NULL) {
386
+ group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
387
+ if (group_ids == NULL) {
381
388
  return 0;
382
389
  }
383
390
 
384
- for (i = 0; i < ncurves; i++) {
385
- if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) {
386
- OPENSSL_free(curve_ids);
391
+ for (size_t i = 0; i < ncurves; i++) {
392
+ if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
393
+ OPENSSL_free(group_ids);
387
394
  return 0;
388
395
  }
389
396
  }
390
397
 
391
- OPENSSL_free(*out_curve_ids);
392
- *out_curve_ids = curve_ids;
393
- *out_curve_ids_len = ncurves;
398
+ OPENSSL_free(*out_group_ids);
399
+ *out_group_ids = group_ids;
400
+ *out_group_ids_len = ncurves;
394
401
 
395
402
  return 1;
396
403
  }
397
404
 
398
- /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
399
- * TLS curve ID and point format, respectively, for |ec|. It returns one on
405
+ /* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
406
+ * TLS group ID and point format, respectively, for |ec|. It returns one on
400
407
  * success and zero on failure. */
401
- static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
408
+ static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
402
409
  uint8_t *out_comp_id, EC_KEY *ec) {
403
410
  int nid;
404
411
  uint16_t id;
@@ -413,14 +420,14 @@ static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
413
420
  return 0;
414
421
  }
415
422
 
416
- /* Determine curve ID */
423
+ /* Determine group ID */
417
424
  nid = EC_GROUP_get_curve_name(grp);
418
- if (!ssl_nid_to_curve_id(&id, nid)) {
425
+ if (!ssl_nid_to_group_id(&id, nid)) {
419
426
  return 0;
420
427
  }
421
428
 
422
- /* Set the named curve ID. Arbitrary explicit curves are not supported. */
423
- *out_curve_id = id;
429
+ /* Set the named group ID. Arbitrary explicit groups are not supported. */
430
+ *out_group_id = id;
424
431
 
425
432
  if (out_comp_id) {
426
433
  if (EC_KEY_get0_public_key(ec) == NULL) {
@@ -436,35 +443,35 @@ static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
436
443
  return 1;
437
444
  }
438
445
 
439
- /* tls1_check_curve_id returns one if |curve_id| is consistent with both our
440
- * and the peer's curve preferences. Note: if called as the client, only our
446
+ /* tls1_check_group_id returns one if |group_id| is consistent with both our
447
+ * and the peer's group preferences. Note: if called as the client, only our
441
448
  * preferences are checked; the peer (the server) does not send preferences. */
442
- int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
443
- const uint16_t *curves;
444
- size_t curves_len, i, get_peer_curves;
449
+ int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
450
+ const uint16_t *groups;
451
+ size_t groups_len, i, get_peer_groups;
445
452
 
446
453
  /* Check against our list, then the peer's list. */
447
- for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
448
- if (get_peer_curves && !ssl->server) {
454
+ for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
455
+ if (get_peer_groups && !ssl->server) {
449
456
  /* Servers do not present a preference list so, if we are a client, only
450
457
  * check our list. */
451
458
  continue;
452
459
  }
453
460
 
454
- tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len);
455
- if (get_peer_curves && curves_len == 0) {
456
- /* Clients are not required to send a supported_curves extension. In this
457
- * case, the server is free to pick any curve it likes. See RFC 4492,
461
+ tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
462
+ if (get_peer_groups && groups_len == 0) {
463
+ /* Clients are not required to send a supported_groups extension. In this
464
+ * case, the server is free to pick any group it likes. See RFC 4492,
458
465
  * section 4, paragraph 3. */
459
466
  continue;
460
467
  }
461
- for (i = 0; i < curves_len; i++) {
462
- if (curves[i] == curve_id) {
468
+ for (i = 0; i < groups_len; i++) {
469
+ if (groups[i] == group_id) {
463
470
  break;
464
471
  }
465
472
  }
466
473
 
467
- if (i == curves_len) {
474
+ if (i == groups_len) {
468
475
  return 0;
469
476
  }
470
477
  }
@@ -473,18 +480,23 @@ int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) {
473
480
  }
474
481
 
475
482
  int tls1_check_ec_cert(SSL *ssl, X509 *x) {
476
- int ret = 0;
477
- EVP_PKEY *pkey = X509_get_pubkey(x);
478
- uint16_t curve_id;
479
- uint8_t comp_id;
483
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
484
+ /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
485
+ return 1;
486
+ }
480
487
 
481
- if (!pkey) {
482
- goto done;
488
+ EVP_PKEY *pkey = X509_get_pubkey(x);
489
+ if (pkey == NULL) {
490
+ return 0;
483
491
  }
492
+
493
+ int ret = 0;
494
+ uint16_t group_id;
495
+ uint8_t comp_id;
484
496
  EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
485
497
  if (ec_key == NULL ||
486
- !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
487
- !tls1_check_curve_id(ssl, curve_id) ||
498
+ !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
499
+ !tls1_check_group_id(ssl, group_id) ||
488
500
  comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
489
501
  goto done;
490
502
  }
@@ -499,48 +511,65 @@ done:
499
511
  /* List of supported signature algorithms and hashes. Should make this
500
512
  * customisable at some point, for now include everything we support. */
501
513
 
502
- #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
514
+ static const uint16_t kDefaultSignatureAlgorithms[] = {
515
+ SSL_SIGN_RSA_PKCS1_SHA512,
516
+ SSL_SIGN_ECDSA_SECP521R1_SHA512,
503
517
 
504
- #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
518
+ SSL_SIGN_RSA_PKCS1_SHA384,
519
+ SSL_SIGN_ECDSA_SECP384R1_SHA384,
505
520
 
506
- #define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
521
+ SSL_SIGN_RSA_PKCS1_SHA256,
522
+ SSL_SIGN_ECDSA_SECP256R1_SHA256,
507
523
 
508
- static const uint8_t tls12_sigalgs[] = {
509
- tlsext_sigalg(TLSEXT_hash_sha512)
510
- tlsext_sigalg(TLSEXT_hash_sha384)
511
- tlsext_sigalg(TLSEXT_hash_sha256)
512
- tlsext_sigalg(TLSEXT_hash_sha1)
524
+ SSL_SIGN_RSA_PKCS1_SHA1,
525
+ SSL_SIGN_ECDSA_SHA1,
513
526
  };
514
527
 
515
- size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) {
516
- *psigs = tls12_sigalgs;
517
- return sizeof(tls12_sigalgs);
518
- }
528
+ static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
529
+ SSL_SIGN_RSA_PSS_SHA512,
530
+ SSL_SIGN_RSA_PKCS1_SHA512,
531
+ SSL_SIGN_ECDSA_SECP521R1_SHA512,
519
532
 
520
- int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
521
- uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
522
- const uint8_t *sent_sigs;
523
- size_t sent_sigslen, i;
524
- int sigalg = tls12_get_sigid(pkey->type);
533
+ SSL_SIGN_RSA_PSS_SHA384,
534
+ SSL_SIGN_RSA_PKCS1_SHA384,
535
+ SSL_SIGN_ECDSA_SECP384R1_SHA384,
525
536
 
526
- /* Should never happen */
527
- if (sigalg == -1) {
528
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
529
- *out_alert = SSL_AD_INTERNAL_ERROR;
537
+ SSL_SIGN_RSA_PSS_SHA256,
538
+ SSL_SIGN_RSA_PKCS1_SHA256,
539
+ SSL_SIGN_ECDSA_SECP256R1_SHA256,
540
+
541
+ SSL_SIGN_RSA_PKCS1_SHA1,
542
+ SSL_SIGN_ECDSA_SHA1,
543
+ };
544
+
545
+ size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
546
+ uint16_t min_version, max_version;
547
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
548
+ assert(0); /* This should never happen. */
549
+
550
+ /* Return an empty list. */
551
+ ERR_clear_error();
552
+ *psigs = NULL;
530
553
  return 0;
531
554
  }
532
555
 
533
- /* Check key type is consistent with signature */
534
- if (sigalg != signature) {
535
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
536
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
537
- return 0;
556
+ if (max_version >= TLS1_3_VERSION) {
557
+ *psigs = kDefaultTLS13SignatureAlgorithms;
558
+ return OPENSSL_ARRAY_SIZE(kDefaultTLS13SignatureAlgorithms);
538
559
  }
539
560
 
561
+ *psigs = kDefaultSignatureAlgorithms;
562
+ return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
563
+ }
564
+
565
+ int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
566
+ const uint16_t *sent_sigs;
567
+ size_t sent_sigslen, i;
568
+
540
569
  /* Check signature matches a type we sent */
541
570
  sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
542
- for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
543
- if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
571
+ for (i = 0; i < sent_sigslen; i++) {
572
+ if (sigalg == sent_sigs[i]) {
544
573
  break;
545
574
  }
546
575
  }
@@ -551,13 +580,6 @@ int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
551
580
  return 0;
552
581
  }
553
582
 
554
- *out_md = tls12_get_hash(hash);
555
- if (*out_md == NULL) {
556
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
557
- *out_alert = SSL_AD_ILLEGAL_PARAMETER;
558
- return 0;
559
- }
560
-
561
583
  return 1;
562
584
  }
563
585
 
@@ -567,22 +589,30 @@ int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
567
589
  * settings. */
568
590
  void ssl_set_client_disabled(SSL *ssl) {
569
591
  CERT *c = ssl->cert;
570
- const uint8_t *sigalgs;
571
- size_t i, sigalgslen;
572
592
  int have_rsa = 0, have_ecdsa = 0;
573
593
  c->mask_a = 0;
574
594
  c->mask_k = 0;
575
595
 
576
596
  /* Now go through all signature algorithms seeing if we support any for RSA,
577
597
  * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
578
- sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
579
- for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
580
- switch (sigalgs[1]) {
581
- case TLSEXT_signature_rsa:
598
+ const uint16_t *sigalgs;
599
+ size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
600
+ for (size_t i = 0; i < num_sigalgs; i++) {
601
+ switch (sigalgs[i]) {
602
+ case SSL_SIGN_RSA_PSS_SHA512:
603
+ case SSL_SIGN_RSA_PSS_SHA384:
604
+ case SSL_SIGN_RSA_PSS_SHA256:
605
+ case SSL_SIGN_RSA_PKCS1_SHA512:
606
+ case SSL_SIGN_RSA_PKCS1_SHA384:
607
+ case SSL_SIGN_RSA_PKCS1_SHA256:
608
+ case SSL_SIGN_RSA_PKCS1_SHA1:
582
609
  have_rsa = 1;
583
610
  break;
584
611
 
585
- case TLSEXT_signature_ecdsa:
612
+ case SSL_SIGN_ECDSA_SECP521R1_SHA512:
613
+ case SSL_SIGN_ECDSA_SECP384R1_SHA384:
614
+ case SSL_SIGN_ECDSA_SECP256R1_SHA256:
615
+ case SSL_SIGN_ECDSA_SHA1:
586
616
  have_ecdsa = 1;
587
617
  break;
588
618
  }
@@ -629,6 +659,25 @@ struct tls_extension {
629
659
  int (*add_serverhello)(SSL *ssl, CBB *out);
630
660
  };
631
661
 
662
+ static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
663
+ if (contents != NULL) {
664
+ /* Servers MUST NOT send this extension. */
665
+ *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
666
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
667
+ return 0;
668
+ }
669
+
670
+ return 1;
671
+ }
672
+
673
+ static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
674
+ /* This extension from the client is handled elsewhere. */
675
+ return 1;
676
+ }
677
+
678
+ static int dont_add_serverhello(SSL *ssl, CBB *out) {
679
+ return 1;
680
+ }
632
681
 
633
682
  /* Server name indication (SNI).
634
683
  *
@@ -670,10 +719,10 @@ static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
670
719
 
671
720
  assert(ssl->tlsext_hostname != NULL);
672
721
 
673
- if (!ssl->hit) {
674
- assert(ssl->session->tlsext_hostname == NULL);
675
- ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
676
- if (!ssl->session->tlsext_hostname) {
722
+ if (ssl->session == NULL) {
723
+ assert(ssl->s3->new_session->tlsext_hostname == NULL);
724
+ ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
725
+ if (!ssl->s3->new_session->tlsext_hostname) {
677
726
  *out_alert = SSL_AD_INTERNAL_ERROR;
678
727
  return 0;
679
728
  }
@@ -688,91 +737,53 @@ static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
688
737
  return 1;
689
738
  }
690
739
 
691
- /* The servername extension is treated as follows:
692
- *
693
- * - Only the hostname type is supported with a maximum length of 255.
694
- * - The servername is rejected if too long or if it contains zeros, in
695
- * which case an fatal alert is generated.
696
- * - The servername field is maintained together with the session cache.
697
- * - When a session is resumed, the servername callback is invoked in order
698
- * to allow the application to position itself to the right context.
699
- * - The servername is acknowledged if it is new for a session or when
700
- * it is identical to a previously used for the same session.
701
- * Applications can control the behaviour. They can at any time
702
- * set a 'desirable' servername for a new SSL object. This can be the
703
- * case for example with HTTPS when a Host: header field is received and
704
- * a renegotiation is requested. In this case, a possible servername
705
- * presented in the new client hello is only acknowledged if it matches
706
- * the value of the Host: field.
707
- * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
708
- * if they provide for changing an explicit servername context for the
709
- * session,
710
- * i.e. when the session has been established with a servername extension.
711
- */
712
-
713
- CBS server_name_list;
714
- char have_seen_host_name = 0;
715
-
740
+ CBS server_name_list, host_name;
741
+ uint8_t name_type;
716
742
  if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
717
- CBS_len(&server_name_list) == 0 ||
743
+ !CBS_get_u8(&server_name_list, &name_type) ||
744
+ /* Although the server_name extension was intended to be extensible to
745
+ * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
746
+ * different name types will cause an error. Further, RFC 4366 originally
747
+ * defined syntax inextensibly. RFC 6066 corrected this mistake, but
748
+ * adding new name types is no longer feasible.
749
+ *
750
+ * Act as if the extensibility does not exist to simplify parsing. */
751
+ !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
752
+ CBS_len(&server_name_list) != 0 ||
718
753
  CBS_len(contents) != 0) {
719
754
  return 0;
720
755
  }
721
756
 
722
- /* Decode each ServerName in the extension. */
723
- while (CBS_len(&server_name_list) > 0) {
724
- uint8_t name_type;
725
- CBS host_name;
726
-
727
- if (!CBS_get_u8(&server_name_list, &name_type) ||
728
- !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
729
- return 0;
730
- }
731
-
732
- /* Only host_name is supported. */
733
- if (name_type != TLSEXT_NAMETYPE_host_name) {
734
- continue;
735
- }
736
-
737
- if (have_seen_host_name) {
738
- /* The ServerNameList MUST NOT contain more than one name of the same
739
- * name_type. */
740
- return 0;
741
- }
757
+ if (name_type != TLSEXT_NAMETYPE_host_name ||
758
+ CBS_len(&host_name) == 0 ||
759
+ CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
760
+ CBS_contains_zero_byte(&host_name)) {
761
+ *out_alert = SSL_AD_UNRECOGNIZED_NAME;
762
+ return 0;
763
+ }
742
764
 
743
- have_seen_host_name = 1;
765
+ /* TODO(davidben): SNI should be resolved before resumption. We have the
766
+ * early callback as a replacement, but we should fix the current callback
767
+ * and avoid the need for |SSL_CTX_set_session_id_context|. */
768
+ if (ssl->session == NULL) {
769
+ assert(ssl->s3->new_session->tlsext_hostname == NULL);
744
770
 
745
- if (CBS_len(&host_name) == 0 ||
746
- CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
747
- CBS_contains_zero_byte(&host_name)) {
748
- *out_alert = SSL_AD_UNRECOGNIZED_NAME;
771
+ /* Copy the hostname as a string. */
772
+ if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
773
+ *out_alert = SSL_AD_INTERNAL_ERROR;
749
774
  return 0;
750
775
  }
751
776
 
752
- if (!ssl->hit) {
753
- assert(ssl->session->tlsext_hostname == NULL);
754
- if (ssl->session->tlsext_hostname) {
755
- /* This should be impossible. */
756
- return 0;
757
- }
758
-
759
- /* Copy the hostname as a string. */
760
- if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
761
- *out_alert = SSL_AD_INTERNAL_ERROR;
762
- return 0;
763
- }
764
-
765
- ssl->s3->tmp.should_ack_sni = 1;
766
- }
777
+ ssl->s3->tmp.should_ack_sni = 1;
767
778
  }
768
779
 
769
780
  return 1;
770
781
  }
771
782
 
772
783
  static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
773
- if (ssl->hit ||
784
+ if (ssl->session != NULL ||
774
785
  !ssl->s3->tmp.should_ack_sni ||
775
- ssl->session->tlsext_hostname == NULL) {
786
+ ssl->s3->new_session->tlsext_hostname == NULL) {
776
787
  return 1;
777
788
  }
778
789
 
@@ -790,6 +801,16 @@ static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
790
801
  * https://tools.ietf.org/html/rfc5746 */
791
802
 
792
803
  static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
804
+ uint16_t min_version, max_version;
805
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
806
+ return 0;
807
+ }
808
+
809
+ /* Renegotiation indication is not necessary in TLS 1.3. */
810
+ if (min_version >= TLS1_3_VERSION) {
811
+ return 1;
812
+ }
813
+
793
814
  CBB contents, prev_finished;
794
815
  if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
795
816
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -805,6 +826,10 @@ static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
805
826
 
806
827
  static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
807
828
  CBS *contents) {
829
+ if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
830
+ return 0;
831
+ }
832
+
808
833
  /* Servers may not switch between omitting the extension and supporting it.
809
834
  * See RFC 5746, sections 3.5 and 4.2. */
810
835
  if (ssl->s3->initial_handshake_complete &&
@@ -874,25 +899,15 @@ static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
874
899
  * called after the initial handshake. */
875
900
  assert(!ssl->s3->initial_handshake_complete);
876
901
 
877
- CBS fake_contents;
878
- static const uint8_t kFakeExtension[] = {0};
902
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
903
+ return 1;
904
+ }
879
905
 
880
906
  if (contents == NULL) {
881
- if (ssl->s3->send_connection_binding) {
882
- /* The renegotiation SCSV was received so pretend that we received a
883
- * renegotiation extension. */
884
- CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
885
- contents = &fake_contents;
886
- /* We require that the renegotiation extension is at index zero of
887
- * kExtensions. */
888
- ssl->s3->tmp.extensions.received |= (1u << 0);
889
- } else {
890
- return 1;
891
- }
907
+ return 1;
892
908
  }
893
909
 
894
910
  CBS renegotiated_connection;
895
-
896
911
  if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
897
912
  CBS_len(contents) != 0) {
898
913
  OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
@@ -914,6 +929,10 @@ static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
914
929
  }
915
930
 
916
931
  static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
932
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
933
+ return 1;
934
+ }
935
+
917
936
  CBB contents, prev_finished;
918
937
  if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
919
938
  !CBB_add_u16_length_prefixed(out, &contents) ||
@@ -934,12 +953,14 @@ static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
934
953
  *
935
954
  * https://tools.ietf.org/html/rfc7627 */
936
955
 
937
- static void ext_ems_init(SSL *ssl) {
938
- ssl->s3->tmp.extended_master_secret = 0;
939
- }
940
-
941
956
  static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
942
- if (ssl->version == SSL3_VERSION) {
957
+ uint16_t min_version, max_version;
958
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
959
+ return 0;
960
+ }
961
+
962
+ /* Extended master secret is not necessary in TLS 1.3. */
963
+ if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
943
964
  return 1;
944
965
  }
945
966
 
@@ -953,11 +974,27 @@ static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
953
974
 
954
975
  static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
955
976
  CBS *contents) {
977
+ /* Whether EMS is negotiated may not change on renegotation. */
978
+ if (ssl->s3->initial_handshake_complete) {
979
+ if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
980
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
981
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
982
+ return 0;
983
+ }
984
+
985
+ return 1;
986
+ }
987
+
956
988
  if (contents == NULL) {
957
989
  return 1;
958
990
  }
959
991
 
960
- if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
992
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
993
+ ssl->version == SSL3_VERSION) {
994
+ return 0;
995
+ }
996
+
997
+ if (CBS_len(contents) != 0) {
961
998
  return 0;
962
999
  }
963
1000
 
@@ -967,7 +1004,12 @@ static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
967
1004
 
968
1005
  static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
969
1006
  CBS *contents) {
970
- if (ssl->version == SSL3_VERSION || contents == NULL) {
1007
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1008
+ ssl->version == SSL3_VERSION) {
1009
+ return 1;
1010
+ }
1011
+
1012
+ if (contents == NULL) {
971
1013
  return 1;
972
1014
  }
973
1015
 
@@ -998,7 +1040,14 @@ static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
998
1040
  * https://tools.ietf.org/html/rfc5077 */
999
1041
 
1000
1042
  static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1001
- if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1043
+ uint16_t min_version, max_version;
1044
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1045
+ return 0;
1046
+ }
1047
+
1048
+ /* TLS 1.3 uses a different ticket extension. */
1049
+ if (min_version >= TLS1_3_VERSION ||
1050
+ SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1002
1051
  return 1;
1003
1052
  }
1004
1053
 
@@ -1009,9 +1058,14 @@ static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1009
1058
  * advertise the extension to avoid potentially breaking servers which carry
1010
1059
  * over the state from the previous handshake, such as OpenSSL servers
1011
1060
  * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1061
+ uint16_t session_version;
1012
1062
  if (!ssl->s3->initial_handshake_complete &&
1013
1063
  ssl->session != NULL &&
1014
- ssl->session->tlsext_tick != NULL) {
1064
+ ssl->session->tlsext_tick != NULL &&
1065
+ /* Don't send TLS 1.3 session tickets in the ticket extension. */
1066
+ ssl->method->version_from_wire(&session_version,
1067
+ ssl->session->ssl_version) &&
1068
+ session_version < TLS1_3_VERSION) {
1015
1069
  ticket_data = ssl->session->tlsext_tick;
1016
1070
  ticket_len = ssl->session->tlsext_ticklen;
1017
1071
  }
@@ -1035,6 +1089,10 @@ static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1035
1089
  return 1;
1036
1090
  }
1037
1091
 
1092
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1093
+ return 0;
1094
+ }
1095
+
1038
1096
  /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1039
1097
  * this function should never be called, even if the server tries to send the
1040
1098
  * extension. */
@@ -1048,13 +1106,6 @@ static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1048
1106
  return 1;
1049
1107
  }
1050
1108
 
1051
- static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1052
- CBS *contents) {
1053
- /* This function isn't used because the ticket extension from the client is
1054
- * handled in ssl_session.c. */
1055
- return 1;
1056
- }
1057
-
1058
1109
  static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1059
1110
  if (!ssl->tlsext_ticket_expected) {
1060
1111
  return 1;
@@ -1078,31 +1129,32 @@ static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1078
1129
  * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1079
1130
 
1080
1131
  static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1081
- if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1132
+ uint16_t min_version, max_version;
1133
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1134
+ return 0;
1135
+ }
1136
+
1137
+ if (max_version < TLS1_2_VERSION) {
1082
1138
  return 1;
1083
1139
  }
1084
1140
 
1085
- const uint8_t *sigalgs_data;
1086
- const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1141
+ const uint16_t *sigalgs;
1142
+ const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
1087
1143
 
1088
- CBB contents, sigalgs;
1144
+ CBB contents, sigalgs_cbb;
1089
1145
  if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1090
1146
  !CBB_add_u16_length_prefixed(out, &contents) ||
1091
- !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1092
- !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1093
- !CBB_flush(out)) {
1147
+ !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
1094
1148
  return 0;
1095
1149
  }
1096
1150
 
1097
- return 1;
1098
- }
1151
+ for (size_t i = 0; i < num_sigalgs; i++) {
1152
+ if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1153
+ return 0;
1154
+ }
1155
+ }
1099
1156
 
1100
- static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1101
- CBS *contents) {
1102
- if (contents != NULL) {
1103
- /* Servers MUST NOT send this extension. */
1104
- *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1105
- OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
1157
+ if (!CBB_flush(out)) {
1106
1158
  return 0;
1107
1159
  }
1108
1160
 
@@ -1111,9 +1163,9 @@ static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1111
1163
 
1112
1164
  static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1113
1165
  CBS *contents) {
1114
- OPENSSL_free(ssl->cert->peer_sigalgs);
1115
- ssl->cert->peer_sigalgs = NULL;
1116
- ssl->cert->peer_sigalgslen = 0;
1166
+ OPENSSL_free(ssl->s3->hs->peer_sigalgs);
1167
+ ssl->s3->hs->peer_sigalgs = NULL;
1168
+ ssl->s3->hs->num_peer_sigalgs = 0;
1117
1169
 
1118
1170
  if (contents == NULL) {
1119
1171
  return 1;
@@ -1130,11 +1182,6 @@ static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1130
1182
  return 1;
1131
1183
  }
1132
1184
 
1133
- static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1134
- /* Servers MUST NOT send this extension. */
1135
- return 1;
1136
- }
1137
-
1138
1185
 
1139
1186
  /* OCSP Stapling.
1140
1187
  *
@@ -1170,11 +1217,36 @@ static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1170
1217
  return 1;
1171
1218
  }
1172
1219
 
1173
- if (CBS_len(contents) != 0) {
1220
+ /* OCSP stapling is forbidden on a non-certificate cipher. */
1221
+ if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
1222
+ return 0;
1223
+ }
1224
+
1225
+ if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1226
+ if (CBS_len(contents) != 0) {
1227
+ return 0;
1228
+ }
1229
+
1230
+ ssl->s3->tmp.certificate_status_expected = 1;
1231
+ return 1;
1232
+ }
1233
+
1234
+ uint8_t status_type;
1235
+ CBS ocsp_response;
1236
+ if (!CBS_get_u8(contents, &status_type) ||
1237
+ status_type != TLSEXT_STATUSTYPE_ocsp ||
1238
+ !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1239
+ CBS_len(&ocsp_response) == 0 ||
1240
+ CBS_len(contents) != 0) {
1241
+ return 0;
1242
+ }
1243
+
1244
+ if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1245
+ &ssl->s3->new_session->ocsp_response_length)) {
1246
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1174
1247
  return 0;
1175
1248
  }
1176
1249
 
1177
- ssl->s3->tmp.certificate_status_expected = 1;
1178
1250
  return 1;
1179
1251
  }
1180
1252
 
@@ -1197,17 +1269,32 @@ static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1197
1269
  }
1198
1270
 
1199
1271
  static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
1200
- /* The extension shouldn't be sent when resuming sessions. */
1201
- if (ssl->hit ||
1202
- !ssl->s3->tmp.ocsp_stapling_requested ||
1203
- ssl->ctx->ocsp_response_length == 0) {
1272
+ if (!ssl->s3->tmp.ocsp_stapling_requested ||
1273
+ ssl->ctx->ocsp_response_length == 0 ||
1274
+ !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
1204
1275
  return 1;
1205
1276
  }
1206
1277
 
1207
- ssl->s3->tmp.certificate_status_expected = 1;
1278
+ if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1279
+ /* The extension shouldn't be sent when resuming sessions. */
1280
+ if (ssl->session != NULL) {
1281
+ return 1;
1282
+ }
1283
+
1284
+ ssl->s3->tmp.certificate_status_expected = 1;
1208
1285
 
1286
+ return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1287
+ CBB_add_u16(out, 0 /* length */);
1288
+ }
1289
+
1290
+ CBB body, ocsp_response;
1209
1291
  return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1210
- CBB_add_u16(out, 0 /* length */);
1292
+ CBB_add_u16_length_prefixed(out, &body) &&
1293
+ CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1294
+ CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1295
+ CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1296
+ ssl->ctx->ocsp_response_length) &&
1297
+ CBB_flush(out);
1211
1298
  }
1212
1299
 
1213
1300
 
@@ -1223,7 +1310,7 @@ static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1223
1310
  if (ssl->s3->initial_handshake_complete ||
1224
1311
  ssl->ctx->next_proto_select_cb == NULL ||
1225
1312
  (ssl->options & SSL_OP_DISABLE_NPN) ||
1226
- SSL_IS_DTLS(ssl)) {
1313
+ SSL_is_dtls(ssl)) {
1227
1314
  return 1;
1228
1315
  }
1229
1316
 
@@ -1241,11 +1328,15 @@ static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1241
1328
  return 1;
1242
1329
  }
1243
1330
 
1331
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1332
+ return 0;
1333
+ }
1334
+
1244
1335
  /* If any of these are false then we should never have sent the NPN
1245
1336
  * extension in the ClientHello and thus this function should never have been
1246
1337
  * called. */
1247
1338
  assert(!ssl->s3->initial_handshake_complete);
1248
- assert(!SSL_IS_DTLS(ssl));
1339
+ assert(!SSL_is_dtls(ssl));
1249
1340
  assert(ssl->ctx->next_proto_select_cb != NULL);
1250
1341
  assert(!(ssl->options & SSL_OP_DISABLE_NPN));
1251
1342
 
@@ -1291,6 +1382,10 @@ static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1291
1382
 
1292
1383
  static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1293
1384
  CBS *contents) {
1385
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1386
+ return 1;
1387
+ }
1388
+
1294
1389
  if (contents != NULL && CBS_len(contents) != 0) {
1295
1390
  return 0;
1296
1391
  }
@@ -1302,7 +1397,7 @@ static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1302
1397
  * |next_proto_neg_seen|. */
1303
1398
  ssl->s3->alpn_selected != NULL ||
1304
1399
  ssl->ctx->next_protos_advertised_cb == NULL ||
1305
- SSL_IS_DTLS(ssl)) {
1400
+ SSL_is_dtls(ssl)) {
1306
1401
  return 1;
1307
1402
  }
1308
1403
 
@@ -1372,9 +1467,11 @@ static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1372
1467
  }
1373
1468
 
1374
1469
  /* Session resumption uses the original session information. */
1375
- if (!ssl->hit &&
1376
- !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1377
- &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1470
+ if (!ssl->s3->session_reused &&
1471
+ !CBS_stow(
1472
+ contents,
1473
+ &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1474
+ &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
1378
1475
  *out_alert = SSL_AD_INTERNAL_ERROR;
1379
1476
  return 0;
1380
1477
  }
@@ -1389,7 +1486,7 @@ static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1389
1486
 
1390
1487
  static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1391
1488
  /* The extension shouldn't be sent when resuming sessions. */
1392
- if (ssl->hit ||
1489
+ if (ssl->s3->session_reused ||
1393
1490
  ssl->ctx->signed_cert_timestamp_list_length == 0) {
1394
1491
  return 1;
1395
1492
  }
@@ -1459,6 +1556,32 @@ static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1459
1556
  return 0;
1460
1557
  }
1461
1558
 
1559
+ /* Check that the protcol name is one of the ones we advertised. */
1560
+ int protocol_ok = 0;
1561
+ CBS client_protocol_name_list, client_protocol_name;
1562
+ CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1563
+ ssl->alpn_client_proto_list_len);
1564
+ while (CBS_len(&client_protocol_name_list) > 0) {
1565
+ if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1566
+ &client_protocol_name)) {
1567
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1568
+ return 0;
1569
+ }
1570
+
1571
+ if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1572
+ memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1573
+ CBS_len(&protocol_name)) == 0) {
1574
+ protocol_ok = 1;
1575
+ break;
1576
+ }
1577
+ }
1578
+
1579
+ if (!protocol_ok) {
1580
+ OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1581
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1582
+ return 0;
1583
+ }
1584
+
1462
1585
  if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1463
1586
  &ssl->s3->alpn_selected_len)) {
1464
1587
  *out_alert = SSL_AD_INTERNAL_ERROR;
@@ -1549,7 +1672,7 @@ static void ext_channel_id_init(SSL *ssl) {
1549
1672
 
1550
1673
  static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1551
1674
  if (!ssl->tlsext_channel_id_enabled ||
1552
- SSL_IS_DTLS(ssl)) {
1675
+ SSL_is_dtls(ssl)) {
1553
1676
  return 1;
1554
1677
  }
1555
1678
 
@@ -1567,7 +1690,11 @@ static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1567
1690
  return 1;
1568
1691
  }
1569
1692
 
1570
- assert(!SSL_IS_DTLS(ssl));
1693
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1694
+ return 0;
1695
+ }
1696
+
1697
+ assert(!SSL_is_dtls(ssl));
1571
1698
  assert(ssl->tlsext_channel_id_enabled);
1572
1699
 
1573
1700
  if (CBS_len(contents) != 0) {
@@ -1582,7 +1709,7 @@ static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1582
1709
  CBS *contents) {
1583
1710
  if (contents == NULL ||
1584
1711
  !ssl->tlsext_channel_id_enabled ||
1585
- SSL_IS_DTLS(ssl)) {
1712
+ SSL_is_dtls(ssl)) {
1586
1713
  return 1;
1587
1714
  }
1588
1715
 
@@ -1595,6 +1722,10 @@ static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1595
1722
  }
1596
1723
 
1597
1724
  static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1725
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1726
+ return 1;
1727
+ }
1728
+
1598
1729
  if (!ssl->s3->tlsext_channel_id_valid) {
1599
1730
  return 1;
1600
1731
  }
@@ -1634,8 +1765,7 @@ static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1634
1765
  return 0;
1635
1766
  }
1636
1767
 
1637
- size_t i;
1638
- for (i = 0; i < num_profiles; i++) {
1768
+ for (size_t i = 0; i < num_profiles; i++) {
1639
1769
  if (!CBB_add_u16(&profile_ids,
1640
1770
  sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1641
1771
  return 0;
@@ -1682,8 +1812,7 @@ static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1682
1812
 
1683
1813
  /* Check to see if the server gave us something we support (and presumably
1684
1814
  * offered). */
1685
- size_t i;
1686
- for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1815
+ for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1687
1816
  const SRTP_PROTECTION_PROFILE *profile =
1688
1817
  sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1689
1818
 
@@ -1718,8 +1847,7 @@ static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1718
1847
  SSL_get_srtp_profiles(ssl);
1719
1848
 
1720
1849
  /* Pick the server's most preferred profile. */
1721
- size_t i;
1722
- for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1850
+ for (size_t i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1723
1851
  const SRTP_PROTECTION_PROFILE *server_profile =
1724
1852
  sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1725
1853
 
@@ -1766,14 +1894,13 @@ static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1766
1894
  * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1767
1895
 
1768
1896
  static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1769
- if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1897
+ if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
1770
1898
  return 0;
1771
1899
  }
1772
1900
 
1773
1901
  const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1774
1902
 
1775
- size_t i;
1776
- for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1903
+ for (size_t i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1777
1904
  const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1778
1905
 
1779
1906
  const uint32_t alg_k = cipher->algorithm_mkey;
@@ -1813,6 +1940,10 @@ static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1813
1940
  return 1;
1814
1941
  }
1815
1942
 
1943
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1944
+ return 0;
1945
+ }
1946
+
1816
1947
  CBS ec_point_format_list;
1817
1948
  if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1818
1949
  CBS_len(contents) != 0) {
@@ -1832,10 +1963,18 @@ static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1832
1963
 
1833
1964
  static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1834
1965
  CBS *contents) {
1966
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1967
+ return 1;
1968
+ }
1969
+
1835
1970
  return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1836
1971
  }
1837
1972
 
1838
1973
  static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1974
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1975
+ return 1;
1976
+ }
1977
+
1839
1978
  const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1840
1979
  const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1841
1980
  const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
@@ -1847,102 +1986,439 @@ static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1847
1986
  return ext_ec_point_add_extension(ssl, out);
1848
1987
  }
1849
1988
 
1850
-
1851
- /* EC supported curves.
1989
+ /* Pre Shared Key
1852
1990
  *
1853
- * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1991
+ * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
1854
1992
 
1855
- static void ext_ec_curves_init(SSL *ssl) {
1856
- OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1857
- ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1858
- ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1859
- }
1993
+ static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1994
+ uint16_t min_version, max_version;
1995
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1996
+ return 0;
1997
+ }
1860
1998
 
1861
- static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1862
- if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1999
+ uint16_t session_version;
2000
+ if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
2001
+ !ssl->method->version_from_wire(&session_version,
2002
+ ssl->session->ssl_version) ||
2003
+ session_version < TLS1_3_VERSION) {
1863
2004
  return 1;
1864
2005
  }
1865
2006
 
1866
- CBB contents, curves_bytes;
1867
- if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
2007
+ CBB contents, identities, identity;
2008
+ if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1868
2009
  !CBB_add_u16_length_prefixed(out, &contents) ||
1869
- !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
2010
+ !CBB_add_u16_length_prefixed(&contents, &identities) ||
2011
+ !CBB_add_u16_length_prefixed(&identities, &identity) ||
2012
+ !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2013
+ ssl->session->tlsext_ticklen)) {
1870
2014
  return 0;
1871
2015
  }
1872
2016
 
1873
- const uint16_t *curves;
1874
- size_t curves_len;
1875
- tls1_get_curvelist(ssl, 0, &curves, &curves_len);
2017
+ return CBB_flush(out);
2018
+ }
1876
2019
 
1877
- size_t i;
1878
- for (i = 0; i < curves_len; i++) {
1879
- if (!CBB_add_u16(&curves_bytes, curves[i])) {
1880
- return 0;
1881
- }
2020
+ int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2021
+ CBS *contents) {
2022
+ uint16_t psk_id;
2023
+ if (!CBS_get_u16(contents, &psk_id) ||
2024
+ CBS_len(contents) != 0) {
2025
+ *out_alert = SSL_AD_DECODE_ERROR;
2026
+ return 0;
1882
2027
  }
1883
2028
 
1884
- return CBB_flush(out);
1885
- }
2029
+ if (psk_id != 0) {
2030
+ *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2031
+ return 0;
2032
+ }
1886
2033
 
1887
- static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1888
- CBS *contents) {
1889
- /* This extension is not expected to be echoed by servers and is ignored. */
1890
2034
  return 1;
1891
2035
  }
1892
2036
 
1893
- static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1894
- CBS *contents) {
1895
- if (contents == NULL) {
1896
- return 1;
1897
- }
1898
-
1899
- CBS elliptic_curve_list;
1900
- if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
1901
- CBS_len(&elliptic_curve_list) == 0 ||
1902
- (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2037
+ int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2038
+ SSL_SESSION **out_session,
2039
+ uint8_t *out_alert,
2040
+ CBS *contents) {
2041
+ CBS identities, identity;
2042
+ if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2043
+ !CBS_get_u16_length_prefixed(&identities, &identity) ||
1903
2044
  CBS_len(contents) != 0) {
2045
+ *out_alert = SSL_AD_DECODE_ERROR;
1904
2046
  return 0;
1905
2047
  }
1906
2048
 
1907
- ssl->s3->tmp.peer_ellipticcurvelist = OPENSSL_malloc(CBS_len(&elliptic_curve_list));
1908
- if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
1909
- *out_alert = SSL_AD_INTERNAL_ERROR;
1910
- return 0;
1911
- }
2049
+ /* TLS 1.3 session tickets are renewed separately as part of the
2050
+ * NewSessionTicket. */
2051
+ int renew;
2052
+ return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2053
+ CBS_len(&identity), NULL, 0);
2054
+ }
1912
2055
 
1913
- const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
1914
- size_t i;
1915
- for (i = 0; i < num_curves; i++) {
1916
- if (!CBS_get_u16(&elliptic_curve_list,
1917
- &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
1918
- goto err;
1919
- }
2056
+ int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2057
+ if (!ssl->s3->session_reused) {
2058
+ return 1;
1920
2059
  }
1921
2060
 
1922
- assert(CBS_len(&elliptic_curve_list) == 0);
1923
- ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2061
+ CBB contents;
2062
+ if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2063
+ !CBB_add_u16_length_prefixed(out, &contents) ||
2064
+ /* We only consider the first identity for resumption */
2065
+ !CBB_add_u16(&contents, 0) ||
2066
+ !CBB_flush(out)) {
2067
+ return 0;
2068
+ }
1924
2069
 
1925
2070
  return 1;
1926
-
1927
- err:
1928
- OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1929
- ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1930
- *out_alert = SSL_AD_INTERNAL_ERROR;
1931
- return 0;
1932
2071
  }
1933
2072
 
1934
- static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
1935
- /* Servers don't echo this extension. */
1936
- return 1;
2073
+
2074
+ /* Key Share
2075
+ *
2076
+ * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2077
+
2078
+ static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2079
+ uint16_t min_version, max_version;
2080
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2081
+ return 0;
2082
+ }
2083
+
2084
+ if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2085
+ return 1;
2086
+ }
2087
+
2088
+ CBB contents, kse_bytes;
2089
+ if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2090
+ !CBB_add_u16_length_prefixed(out, &contents) ||
2091
+ !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2092
+ return 0;
2093
+ }
2094
+
2095
+ uint16_t group_id;
2096
+ if (ssl->s3->hs->retry_group) {
2097
+ /* Append the new key share to the old list. */
2098
+ if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2099
+ ssl->s3->hs->key_share_bytes_len)) {
2100
+ return 0;
2101
+ }
2102
+ OPENSSL_free(ssl->s3->hs->key_share_bytes);
2103
+ ssl->s3->hs->key_share_bytes = NULL;
2104
+ ssl->s3->hs->key_share_bytes_len = 0;
2105
+
2106
+ group_id = ssl->s3->hs->retry_group;
2107
+ } else {
2108
+ /* Add a fake group. See draft-davidben-tls-grease-01. */
2109
+ if (ssl->ctx->grease_enabled &&
2110
+ (!CBB_add_u16(&kse_bytes,
2111
+ ssl_get_grease_value(ssl, ssl_grease_group)) ||
2112
+ !CBB_add_u16(&kse_bytes, 1 /* length */) ||
2113
+ !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
2114
+ return 0;
2115
+ }
2116
+
2117
+ /* Predict the most preferred group. */
2118
+ const uint16_t *groups;
2119
+ size_t groups_len;
2120
+ tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2121
+ if (groups_len == 0) {
2122
+ OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
2123
+ return 0;
2124
+ }
2125
+
2126
+ group_id = groups[0];
2127
+ }
2128
+
2129
+ CBB key_exchange;
2130
+ if (!CBB_add_u16(&kse_bytes, group_id) ||
2131
+ !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2132
+ !SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
2133
+ !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &key_exchange) ||
2134
+ !CBB_flush(&kse_bytes)) {
2135
+ return 0;
2136
+ }
2137
+
2138
+ if (!ssl->s3->hs->retry_group) {
2139
+ /* Save the contents of the extension to repeat it in the second
2140
+ * ClientHello. */
2141
+ ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2142
+ ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2143
+ CBB_len(&kse_bytes));
2144
+ if (ssl->s3->hs->key_share_bytes == NULL) {
2145
+ return 0;
2146
+ }
2147
+ }
2148
+
2149
+ return CBB_flush(out);
2150
+ }
2151
+
2152
+ int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2153
+ size_t *out_secret_len,
2154
+ uint8_t *out_alert, CBS *contents) {
2155
+ CBS peer_key;
2156
+ uint16_t group_id;
2157
+ if (!CBS_get_u16(contents, &group_id) ||
2158
+ !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2159
+ CBS_len(contents) != 0) {
2160
+ *out_alert = SSL_AD_DECODE_ERROR;
2161
+ return 0;
2162
+ }
2163
+
2164
+ if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->ecdh_ctx) != group_id) {
2165
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2166
+ OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2167
+ return 0;
2168
+ }
2169
+
2170
+ if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len,
2171
+ out_alert, CBS_data(&peer_key),
2172
+ CBS_len(&peer_key))) {
2173
+ *out_alert = SSL_AD_INTERNAL_ERROR;
2174
+ return 0;
2175
+ }
2176
+
2177
+ ssl->s3->new_session->key_exchange_info = group_id;
2178
+ SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
2179
+ return 1;
2180
+ }
2181
+
2182
+ int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2183
+ uint8_t **out_secret,
2184
+ size_t *out_secret_len,
2185
+ uint8_t *out_alert, CBS *contents) {
2186
+ uint16_t group_id;
2187
+ CBS key_shares;
2188
+ if (!tls1_get_shared_group(ssl, &group_id) ||
2189
+ !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2190
+ CBS_len(contents) != 0) {
2191
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2192
+ return 0;
2193
+ }
2194
+
2195
+ /* Find the corresponding key share. */
2196
+ int found = 0;
2197
+ CBS peer_key;
2198
+ while (CBS_len(&key_shares) > 0) {
2199
+ uint16_t id;
2200
+ CBS peer_key_tmp;
2201
+ if (!CBS_get_u16(&key_shares, &id) ||
2202
+ !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
2203
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
2204
+ return 0;
2205
+ }
2206
+
2207
+ if (id == group_id) {
2208
+ if (found) {
2209
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
2210
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2211
+ return 0;
2212
+ }
2213
+
2214
+ found = 1;
2215
+ peer_key = peer_key_tmp;
2216
+ /* Continue parsing the structure to keep peers honest. */
2217
+ }
2218
+ }
2219
+
2220
+ if (!found) {
2221
+ *out_found = 0;
2222
+ *out_secret = NULL;
2223
+ *out_secret_len = 0;
2224
+ return 1;
2225
+ }
2226
+
2227
+ /* Compute the DH secret. */
2228
+ uint8_t *secret = NULL;
2229
+ size_t secret_len;
2230
+ SSL_ECDH_CTX group;
2231
+ memset(&group, 0, sizeof(SSL_ECDH_CTX));
2232
+ CBB public_key;
2233
+ if (!CBB_init(&public_key, 32) ||
2234
+ !SSL_ECDH_CTX_init(&group, group_id) ||
2235
+ !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len,
2236
+ out_alert, CBS_data(&peer_key),
2237
+ CBS_len(&peer_key)) ||
2238
+ !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2239
+ &ssl->s3->hs->public_key_len)) {
2240
+ OPENSSL_free(secret);
2241
+ SSL_ECDH_CTX_cleanup(&group);
2242
+ CBB_cleanup(&public_key);
2243
+ return 0;
2244
+ }
2245
+
2246
+ SSL_ECDH_CTX_cleanup(&group);
2247
+
2248
+ *out_secret = secret;
2249
+ *out_secret_len = secret_len;
2250
+ *out_found = 1;
2251
+ return 1;
2252
+ }
2253
+
2254
+ int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
2255
+ if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2256
+ return 1;
2257
+ }
2258
+
2259
+ uint16_t group_id;
2260
+ CBB kse_bytes, public_key;
2261
+ if (!tls1_get_shared_group(ssl, &group_id) ||
2262
+ !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2263
+ !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2264
+ !CBB_add_u16(&kse_bytes, group_id) ||
2265
+ !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2266
+ !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2267
+ ssl->s3->hs->public_key_len) ||
2268
+ !CBB_flush(out)) {
2269
+ return 0;
2270
+ }
2271
+
2272
+ OPENSSL_free(ssl->s3->hs->public_key);
2273
+ ssl->s3->hs->public_key = NULL;
2274
+ ssl->s3->hs->public_key_len = 0;
2275
+
2276
+ ssl->s3->new_session->key_exchange_info = group_id;
2277
+ return 1;
2278
+ }
2279
+
2280
+
2281
+ /* Supported Versions
2282
+ *
2283
+ * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */
2284
+
2285
+ static int ext_supported_versions_add_clienthello(SSL *ssl, CBB *out) {
2286
+ uint16_t min_version, max_version;
2287
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2288
+ return 0;
2289
+ }
2290
+
2291
+ if (max_version <= TLS1_2_VERSION) {
2292
+ return 1;
2293
+ }
2294
+
2295
+ CBB contents, versions;
2296
+ if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
2297
+ !CBB_add_u16_length_prefixed(out, &contents) ||
2298
+ !CBB_add_u8_length_prefixed(&contents, &versions)) {
2299
+ return 0;
2300
+ }
2301
+
2302
+ /* Add a fake version. See draft-davidben-tls-grease-01. */
2303
+ if (ssl->ctx->grease_enabled &&
2304
+ !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
2305
+ return 0;
2306
+ }
2307
+
2308
+ for (uint16_t version = max_version; version >= min_version; version--) {
2309
+ if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) {
2310
+ return 0;
2311
+ }
2312
+ }
2313
+
2314
+ if (!CBB_flush(out)) {
2315
+ return 0;
2316
+ }
2317
+
2318
+ return 1;
2319
+ }
2320
+
2321
+
2322
+ /* Negotiated Groups
2323
+ *
2324
+ * https://tools.ietf.org/html/rfc4492#section-5.1.2
2325
+ * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
2326
+
2327
+ static void ext_supported_groups_init(SSL *ssl) {
2328
+ OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2329
+ ssl->s3->tmp.peer_supported_group_list = NULL;
2330
+ ssl->s3->tmp.peer_supported_group_list_len = 0;
2331
+ }
2332
+
2333
+ static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
2334
+ if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2335
+ return 1;
2336
+ }
2337
+
2338
+ CBB contents, groups_bytes;
2339
+ if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
2340
+ !CBB_add_u16_length_prefixed(out, &contents) ||
2341
+ !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
2342
+ return 0;
2343
+ }
2344
+
2345
+ /* Add a fake group. See draft-davidben-tls-grease-01. */
2346
+ if (ssl->ctx->grease_enabled &&
2347
+ !CBB_add_u16(&groups_bytes,
2348
+ ssl_get_grease_value(ssl, ssl_grease_group))) {
2349
+ return 0;
2350
+ }
2351
+
2352
+ const uint16_t *groups;
2353
+ size_t groups_len;
2354
+ tls1_get_grouplist(ssl, 0, &groups, &groups_len);
2355
+
2356
+ for (size_t i = 0; i < groups_len; i++) {
2357
+ if (!CBB_add_u16(&groups_bytes, groups[i])) {
2358
+ return 0;
2359
+ }
2360
+ }
2361
+
2362
+ return CBB_flush(out);
2363
+ }
2364
+
2365
+ static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2366
+ CBS *contents) {
2367
+ /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2368
+ * BigIP servers send it nonetheless, so do not enforce this. */
2369
+ return 1;
2370
+ }
2371
+
2372
+ static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2373
+ CBS *contents) {
2374
+ if (contents == NULL) {
2375
+ return 1;
2376
+ }
2377
+
2378
+ CBS supported_group_list;
2379
+ if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2380
+ CBS_len(&supported_group_list) == 0 ||
2381
+ (CBS_len(&supported_group_list) & 1) != 0 ||
2382
+ CBS_len(contents) != 0) {
2383
+ return 0;
2384
+ }
2385
+
2386
+ ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2387
+ CBS_len(&supported_group_list));
2388
+ if (ssl->s3->tmp.peer_supported_group_list == NULL) {
2389
+ *out_alert = SSL_AD_INTERNAL_ERROR;
2390
+ return 0;
2391
+ }
2392
+
2393
+ const size_t num_groups = CBS_len(&supported_group_list) / 2;
2394
+ for (size_t i = 0; i < num_groups; i++) {
2395
+ if (!CBS_get_u16(&supported_group_list,
2396
+ &ssl->s3->tmp.peer_supported_group_list[i])) {
2397
+ goto err;
2398
+ }
2399
+ }
2400
+
2401
+ assert(CBS_len(&supported_group_list) == 0);
2402
+ ssl->s3->tmp.peer_supported_group_list_len = num_groups;
2403
+
2404
+ return 1;
2405
+
2406
+ err:
2407
+ OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2408
+ ssl->s3->tmp.peer_supported_group_list = NULL;
2409
+ *out_alert = SSL_AD_INTERNAL_ERROR;
2410
+ return 0;
2411
+ }
2412
+
2413
+ static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
2414
+ /* Servers don't echo this extension. */
2415
+ return 1;
1937
2416
  }
1938
2417
 
1939
2418
 
1940
2419
  /* kExtensions contains all the supported extensions. */
1941
2420
  static const struct tls_extension kExtensions[] = {
1942
2421
  {
1943
- /* The renegotiation extension must always be at index zero because the
1944
- * |received| and |sent| bitsets need to be tweaked when the "extension" is
1945
- * sent as an SCSV. */
1946
2422
  TLSEXT_TYPE_renegotiate,
1947
2423
  NULL,
1948
2424
  ext_ri_add_clienthello,
@@ -1960,7 +2436,7 @@ static const struct tls_extension kExtensions[] = {
1960
2436
  },
1961
2437
  {
1962
2438
  TLSEXT_TYPE_extended_master_secret,
1963
- ext_ems_init,
2439
+ NULL,
1964
2440
  ext_ems_add_clienthello,
1965
2441
  ext_ems_parse_serverhello,
1966
2442
  ext_ems_parse_clienthello,
@@ -1971,16 +2447,17 @@ static const struct tls_extension kExtensions[] = {
1971
2447
  NULL,
1972
2448
  ext_ticket_add_clienthello,
1973
2449
  ext_ticket_parse_serverhello,
1974
- ext_ticket_parse_clienthello,
2450
+ /* Ticket extension client parsing is handled in ssl_session.c */
2451
+ ignore_parse_clienthello,
1975
2452
  ext_ticket_add_serverhello,
1976
2453
  },
1977
2454
  {
1978
2455
  TLSEXT_TYPE_signature_algorithms,
1979
2456
  NULL,
1980
2457
  ext_sigalgs_add_clienthello,
1981
- ext_sigalgs_parse_serverhello,
2458
+ forbid_parse_serverhello,
1982
2459
  ext_sigalgs_parse_clienthello,
1983
- ext_sigalgs_add_serverhello,
2460
+ dont_add_serverhello,
1984
2461
  },
1985
2462
  {
1986
2463
  TLSEXT_TYPE_status_request,
@@ -2039,12 +2516,39 @@ static const struct tls_extension kExtensions[] = {
2039
2516
  ext_ec_point_add_serverhello,
2040
2517
  },
2041
2518
  {
2042
- TLSEXT_TYPE_elliptic_curves,
2043
- ext_ec_curves_init,
2044
- ext_ec_curves_add_clienthello,
2045
- ext_ec_curves_parse_serverhello,
2046
- ext_ec_curves_parse_clienthello,
2047
- ext_ec_curves_add_serverhello,
2519
+ TLSEXT_TYPE_key_share,
2520
+ NULL,
2521
+ ext_key_share_add_clienthello,
2522
+ forbid_parse_serverhello,
2523
+ ignore_parse_clienthello,
2524
+ dont_add_serverhello,
2525
+ },
2526
+ {
2527
+ TLSEXT_TYPE_pre_shared_key,
2528
+ NULL,
2529
+ ext_pre_shared_key_add_clienthello,
2530
+ forbid_parse_serverhello,
2531
+ ignore_parse_clienthello,
2532
+ dont_add_serverhello,
2533
+ },
2534
+ {
2535
+ TLSEXT_TYPE_supported_versions,
2536
+ NULL,
2537
+ ext_supported_versions_add_clienthello,
2538
+ forbid_parse_serverhello,
2539
+ ignore_parse_clienthello,
2540
+ dont_add_serverhello,
2541
+ },
2542
+ /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2543
+ * intolerant to the last extension being zero-length. See
2544
+ * https://crbug.com/363583. */
2545
+ {
2546
+ TLSEXT_TYPE_supported_groups,
2547
+ ext_supported_groups_init,
2548
+ ext_supported_groups_add_clienthello,
2549
+ ext_supported_groups_parse_serverhello,
2550
+ ext_supported_groups_parse_clienthello,
2551
+ ext_supported_groups_add_serverhello,
2048
2552
  },
2049
2553
  };
2050
2554
 
@@ -2092,14 +2596,23 @@ int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
2092
2596
  ssl->s3->tmp.extensions.sent = 0;
2093
2597
  ssl->s3->tmp.custom_extensions.sent = 0;
2094
2598
 
2095
- size_t i;
2096
- for (i = 0; i < kNumExtensions; i++) {
2599
+ for (size_t i = 0; i < kNumExtensions; i++) {
2097
2600
  if (kExtensions[i].init != NULL) {
2098
2601
  kExtensions[i].init(ssl);
2099
2602
  }
2100
2603
  }
2101
2604
 
2102
- for (i = 0; i < kNumExtensions; i++) {
2605
+ uint16_t grease_ext1 = 0;
2606
+ if (ssl->ctx->grease_enabled) {
2607
+ /* Add a fake empty extension. See draft-davidben-tls-grease-01. */
2608
+ grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
2609
+ if (!CBB_add_u16(&extensions, grease_ext1) ||
2610
+ !CBB_add_u16(&extensions, 0 /* zero length */)) {
2611
+ goto err;
2612
+ }
2613
+ }
2614
+
2615
+ for (size_t i = 0; i < kNumExtensions; i++) {
2103
2616
  const size_t len_before = CBB_len(&extensions);
2104
2617
  if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
2105
2618
  OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
@@ -2116,7 +2629,25 @@ int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
2116
2629
  goto err;
2117
2630
  }
2118
2631
 
2119
- if (!SSL_IS_DTLS(ssl)) {
2632
+ if (ssl->ctx->grease_enabled) {
2633
+ /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */
2634
+ uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
2635
+
2636
+ /* The two fake extensions must not have the same value. GREASE values are
2637
+ * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
2638
+ * one. */
2639
+ if (grease_ext1 == grease_ext2) {
2640
+ grease_ext2 ^= 0x1010;
2641
+ }
2642
+
2643
+ if (!CBB_add_u16(&extensions, grease_ext2) ||
2644
+ !CBB_add_u16(&extensions, 1 /* one byte length */) ||
2645
+ !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
2646
+ goto err;
2647
+ }
2648
+ }
2649
+
2650
+ if (!SSL_is_dtls(ssl)) {
2120
2651
  header_len += 2 + CBB_len(&extensions);
2121
2652
  if (header_len > 0xff && header_len < 0x200) {
2122
2653
  /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
@@ -2124,9 +2655,10 @@ int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
2124
2655
  * NB: because this code works out the length of all existing extensions
2125
2656
  * it MUST always appear last. */
2126
2657
  size_t padding_len = 0x200 - header_len;
2127
- /* Extensions take at least four bytes to encode. Always include least
2658
+ /* Extensions take at least four bytes to encode. Always include at least
2128
2659
  * one byte of data if including the extension. WebSphere Application
2129
- * Server 7.0 is intolerant to the last extension being zero-length. */
2660
+ * Server 7.0 is intolerant to the last extension being zero-length. See
2661
+ * https://crbug.com/363583. */
2130
2662
  if (padding_len >= 4 + 1) {
2131
2663
  padding_len -= 4;
2132
2664
  } else {
@@ -2180,8 +2712,9 @@ int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
2180
2712
  goto err;
2181
2713
  }
2182
2714
 
2183
- /* Discard empty extensions blocks. */
2184
- if (CBB_len(&extensions) == 0) {
2715
+ /* Discard empty extensions blocks before TLS 1.3. */
2716
+ if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2717
+ CBB_len(&extensions) == 0) {
2185
2718
  CBB_discard_child(out);
2186
2719
  }
2187
2720
 
@@ -2192,9 +2725,10 @@ err:
2192
2725
  return 0;
2193
2726
  }
2194
2727
 
2195
- static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
2196
- size_t i;
2197
- for (i = 0; i < kNumExtensions; i++) {
2728
+ static int ssl_scan_clienthello_tlsext(
2729
+ SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2730
+ int *out_alert) {
2731
+ for (size_t i = 0; i < kNumExtensions; i++) {
2198
2732
  if (kExtensions[i].init != NULL) {
2199
2733
  kExtensions[i].init(ssl);
2200
2734
  }
@@ -2202,81 +2736,84 @@ static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
2202
2736
 
2203
2737
  ssl->s3->tmp.extensions.received = 0;
2204
2738
  ssl->s3->tmp.custom_extensions.received = 0;
2205
- /* The renegotiation extension must always be at index zero because the
2206
- * |received| and |sent| bitsets need to be tweaked when the "extension" is
2207
- * sent as an SCSV. */
2208
- assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
2209
-
2210
- /* There may be no extensions. */
2211
- if (CBS_len(cbs) != 0) {
2212
- /* Decode the extensions block and check it is valid. */
2213
- CBS extensions;
2214
- if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2215
- !tls1_check_duplicate_extensions(&extensions)) {
2739
+
2740
+ CBS extensions;
2741
+ CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2742
+ while (CBS_len(&extensions) != 0) {
2743
+ uint16_t type;
2744
+ CBS extension;
2745
+
2746
+ /* Decode the next extension. */
2747
+ if (!CBS_get_u16(&extensions, &type) ||
2748
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2216
2749
  *out_alert = SSL_AD_DECODE_ERROR;
2217
2750
  return 0;
2218
2751
  }
2219
2752
 
2220
- while (CBS_len(&extensions) != 0) {
2221
- uint16_t type;
2222
- CBS extension;
2223
-
2224
- /* Decode the next extension. */
2225
- if (!CBS_get_u16(&extensions, &type) ||
2226
- !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2227
- *out_alert = SSL_AD_DECODE_ERROR;
2228
- return 0;
2229
- }
2230
-
2231
- /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2232
- * ambiguous. Ignore all but the renegotiation_info extension. */
2233
- if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2234
- continue;
2235
- }
2753
+ /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2754
+ * ambiguous. Ignore all but the renegotiation_info extension. */
2755
+ if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2756
+ continue;
2757
+ }
2236
2758
 
2237
- unsigned ext_index;
2238
- const struct tls_extension *const ext =
2239
- tls_extension_find(&ext_index, type);
2759
+ unsigned ext_index;
2760
+ const struct tls_extension *const ext =
2761
+ tls_extension_find(&ext_index, type);
2240
2762
 
2241
- if (ext == NULL) {
2242
- if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
2243
- OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2244
- return 0;
2245
- }
2246
- continue;
2247
- }
2248
-
2249
- ssl->s3->tmp.extensions.received |= (1u << ext_index);
2250
- uint8_t alert = SSL_AD_DECODE_ERROR;
2251
- if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2252
- *out_alert = alert;
2763
+ if (ext == NULL) {
2764
+ if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
2253
2765
  OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2254
- ERR_add_error_dataf("extension: %u", (unsigned)type);
2255
2766
  return 0;
2256
2767
  }
2768
+ continue;
2769
+ }
2770
+
2771
+ ssl->s3->tmp.extensions.received |= (1u << ext_index);
2772
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2773
+ if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2774
+ *out_alert = alert;
2775
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2776
+ ERR_add_error_dataf("extension: %u", (unsigned)type);
2777
+ return 0;
2257
2778
  }
2258
2779
  }
2259
2780
 
2260
- for (i = 0; i < kNumExtensions; i++) {
2261
- if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
2262
- /* Extension wasn't observed so call the callback with a NULL
2263
- * parameter. */
2264
- uint8_t alert = SSL_AD_DECODE_ERROR;
2265
- if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
2266
- OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2267
- ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2268
- *out_alert = alert;
2269
- return 0;
2270
- }
2781
+ for (size_t i = 0; i < kNumExtensions; i++) {
2782
+ if (ssl->s3->tmp.extensions.received & (1u << i)) {
2783
+ continue;
2784
+ }
2785
+
2786
+ CBS *contents = NULL, fake_contents;
2787
+ static const uint8_t kFakeRenegotiateExtension[] = {0};
2788
+ if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2789
+ ssl_client_cipher_list_contains_cipher(client_hello,
2790
+ SSL3_CK_SCSV & 0xffff)) {
2791
+ /* The renegotiation SCSV was received so pretend that we received a
2792
+ * renegotiation extension. */
2793
+ CBS_init(&fake_contents, kFakeRenegotiateExtension,
2794
+ sizeof(kFakeRenegotiateExtension));
2795
+ contents = &fake_contents;
2796
+ ssl->s3->tmp.extensions.received |= (1u << i);
2797
+ }
2798
+
2799
+ /* Extension wasn't observed so call the callback with a NULL
2800
+ * parameter. */
2801
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2802
+ if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2803
+ OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2804
+ ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2805
+ *out_alert = alert;
2806
+ return 0;
2271
2807
  }
2272
2808
  }
2273
2809
 
2274
2810
  return 1;
2275
2811
  }
2276
2812
 
2277
- int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
2813
+ int ssl_parse_clienthello_tlsext(
2814
+ SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
2278
2815
  int alert = -1;
2279
- if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2816
+ if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
2280
2817
  ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
2281
2818
  return 0;
2282
2819
  }
@@ -2292,62 +2829,64 @@ int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
2292
2829
  OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2293
2830
 
2294
2831
  static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
2832
+ /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2833
+ if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2834
+ return 1;
2835
+ }
2836
+
2837
+ /* Decode the extensions block and check it is valid. */
2838
+ CBS extensions;
2839
+ if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2840
+ !tls1_check_duplicate_extensions(&extensions)) {
2841
+ *out_alert = SSL_AD_DECODE_ERROR;
2842
+ return 0;
2843
+ }
2844
+
2295
2845
  uint32_t received = 0;
2846
+ while (CBS_len(&extensions) != 0) {
2847
+ uint16_t type;
2848
+ CBS extension;
2296
2849
 
2297
- if (CBS_len(cbs) != 0) {
2298
- /* Decode the extensions block and check it is valid. */
2299
- CBS extensions;
2300
- if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2301
- !tls1_check_duplicate_extensions(&extensions)) {
2850
+ /* Decode the next extension. */
2851
+ if (!CBS_get_u16(&extensions, &type) ||
2852
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2302
2853
  *out_alert = SSL_AD_DECODE_ERROR;
2303
2854
  return 0;
2304
2855
  }
2305
2856
 
2857
+ unsigned ext_index;
2858
+ const struct tls_extension *const ext =
2859
+ tls_extension_find(&ext_index, type);
2306
2860
 
2307
- while (CBS_len(&extensions) != 0) {
2308
- uint16_t type;
2309
- CBS extension;
2310
-
2311
- /* Decode the next extension. */
2312
- if (!CBS_get_u16(&extensions, &type) ||
2313
- !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2314
- *out_alert = SSL_AD_DECODE_ERROR;
2861
+ if (ext == NULL) {
2862
+ if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
2315
2863
  return 0;
2316
2864
  }
2865
+ continue;
2866
+ }
2317
2867
 
2318
- unsigned ext_index;
2319
- const struct tls_extension *const ext =
2320
- tls_extension_find(&ext_index, type);
2321
-
2322
- if (ext == NULL) {
2323
- if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
2324
- return 0;
2325
- }
2326
- continue;
2327
- }
2328
-
2329
- if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
2330
- /* If the extension was never sent then it is illegal. */
2331
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2332
- ERR_add_error_dataf("extension :%u", (unsigned)type);
2333
- *out_alert = SSL_AD_DECODE_ERROR;
2334
- return 0;
2335
- }
2868
+ if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2869
+ type != TLSEXT_TYPE_renegotiate) {
2870
+ /* If the extension was never sent then it is illegal, except for the
2871
+ * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
2872
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2873
+ ERR_add_error_dataf("extension :%u", (unsigned)type);
2874
+ *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2875
+ return 0;
2876
+ }
2336
2877
 
2337
- received |= (1u << ext_index);
2878
+ received |= (1u << ext_index);
2338
2879
 
2339
- uint8_t alert = SSL_AD_DECODE_ERROR;
2340
- if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2341
- OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2342
- ERR_add_error_dataf("extension: %u", (unsigned)type);
2343
- *out_alert = alert;
2344
- return 0;
2345
- }
2880
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2881
+ if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2882
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2883
+ ERR_add_error_dataf("extension: %u", (unsigned)type);
2884
+ *out_alert = alert;
2885
+ return 0;
2346
2886
  }
2347
2887
  }
2348
2888
 
2349
- size_t i;
2350
- for (i = 0; i < kNumExtensions; i++) {
2889
+ for (size_t i = 0; i < kNumExtensions; i++) {
2351
2890
  if (!(received & (1u << i))) {
2352
2891
  /* Extension wasn't observed so call the callback with a NULL
2353
2892
  * parameter. */
@@ -2368,14 +2907,10 @@ static int ssl_check_clienthello_tlsext(SSL *ssl) {
2368
2907
  int ret = SSL_TLSEXT_ERR_NOACK;
2369
2908
  int al = SSL_AD_UNRECOGNIZED_NAME;
2370
2909
 
2371
- /* The handling of the ECPointFormats extension is done elsewhere, namely in
2372
- * ssl3_choose_cipher in s3_lib.c. */
2373
-
2374
- if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2910
+ if (ssl->ctx->tlsext_servername_callback != 0) {
2375
2911
  ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2376
- ssl->ctx->tlsext_servername_arg);
2377
- } else if (ssl->initial_ctx != NULL &&
2378
- ssl->initial_ctx->tlsext_servername_callback != 0) {
2912
+ ssl->ctx->tlsext_servername_arg);
2913
+ } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
2379
2914
  ret = ssl->initial_ctx->tlsext_servername_callback(
2380
2915
  ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
2381
2916
  }
@@ -2402,11 +2937,10 @@ static int ssl_check_serverhello_tlsext(SSL *ssl) {
2402
2937
  int ret = SSL_TLSEXT_ERR_OK;
2403
2938
  int al = SSL_AD_UNRECOGNIZED_NAME;
2404
2939
 
2405
- if (ssl->ctx != NULL && ssl->ctx->tlsext_servername_callback != 0) {
2940
+ if (ssl->ctx->tlsext_servername_callback != 0) {
2406
2941
  ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
2407
- ssl->ctx->tlsext_servername_arg);
2408
- } else if (ssl->initial_ctx != NULL &&
2409
- ssl->initial_ctx->tlsext_servername_callback != 0) {
2942
+ ssl->ctx->tlsext_servername_arg);
2943
+ } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
2410
2944
  ret = ssl->initial_ctx->tlsext_servername_callback(
2411
2945
  ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
2412
2946
  }
@@ -2456,6 +2990,10 @@ int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2456
2990
  *out_renew_ticket = 0;
2457
2991
  *out_session = NULL;
2458
2992
 
2993
+ if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2994
+ goto done;
2995
+ }
2996
+
2459
2997
  if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2460
2998
  goto done;
2461
2999
  }
@@ -2509,7 +3047,12 @@ int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2509
3047
  }
2510
3048
  HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2511
3049
  HMAC_Final(&hmac_ctx, mac, NULL);
2512
- if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
3050
+ int mac_ok =
3051
+ CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
3052
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3053
+ mac_ok = 1;
3054
+ #endif
3055
+ if (!mac_ok) {
2513
3056
  goto done;
2514
3057
  }
2515
3058
 
@@ -2522,6 +3065,11 @@ int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2522
3065
  ret = 0;
2523
3066
  goto done;
2524
3067
  }
3068
+ size_t plaintext_len;
3069
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3070
+ memcpy(plaintext, ciphertext, ciphertext_len);
3071
+ plaintext_len = ciphertext_len;
3072
+ #else
2525
3073
  if (ciphertext_len >= INT_MAX) {
2526
3074
  goto done;
2527
3075
  }
@@ -2532,9 +3080,11 @@ int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2532
3080
  ERR_clear_error(); /* Don't leave an error on the queue. */
2533
3081
  goto done;
2534
3082
  }
3083
+ plaintext_len = (size_t)(len1 + len2);
3084
+ #endif
2535
3085
 
2536
3086
  /* Decode the session. */
2537
- SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
3087
+ SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len);
2538
3088
  if (session == NULL) {
2539
3089
  ERR_clear_error(); /* Don't leave an error on the queue. */
2540
3090
  goto done;
@@ -2545,6 +3095,12 @@ int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2545
3095
  memcpy(session->session_id, session_id, session_id_len);
2546
3096
  session->session_id_length = session_id_len;
2547
3097
 
3098
+ if (!ssl_session_is_context_valid(ssl, session) ||
3099
+ !ssl_session_is_time_valid(ssl, session)) {
3100
+ SSL_SESSION_free(session);
3101
+ session = NULL;
3102
+ }
3103
+
2548
3104
  *out_session = session;
2549
3105
 
2550
3106
  done:
@@ -2554,99 +3110,18 @@ done:
2554
3110
  return ret;
2555
3111
  }
2556
3112
 
2557
- /* Tables to translate from NIDs to TLS v1.2 ids */
2558
- typedef struct {
2559
- int nid;
2560
- int id;
2561
- } tls12_lookup;
2562
-
2563
- static const tls12_lookup tls12_md[] = {
2564
- {NID_sha1, TLSEXT_hash_sha1},
2565
- {NID_sha256, TLSEXT_hash_sha256},
2566
- {NID_sha384, TLSEXT_hash_sha384},
2567
- {NID_sha512, TLSEXT_hash_sha512},
2568
- };
2569
-
2570
- static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2571
- {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
2572
-
2573
- static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2574
- size_t i;
2575
- for (i = 0; i < tlen; i++) {
2576
- if (table[i].nid == nid) {
2577
- return table[i].id;
2578
- }
2579
- }
2580
-
2581
- return -1;
2582
- }
2583
-
2584
- int tls12_get_sigid(int pkey_type) {
2585
- return tls12_find_id(pkey_type, tls12_sig,
2586
- sizeof(tls12_sig) / sizeof(tls12_lookup));
2587
- }
2588
-
2589
- int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) {
2590
- int md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2591
- sizeof(tls12_md) / sizeof(tls12_lookup));
2592
- int sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
2593
-
2594
- return md_id != -1 &&
2595
- sig_id != -1 &&
2596
- CBB_add_u8(out, (uint8_t)md_id) &&
2597
- CBB_add_u8(out, (uint8_t)sig_id);
2598
- }
2599
-
2600
- const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2601
- switch (hash_alg) {
2602
- case TLSEXT_hash_sha1:
2603
- return EVP_sha1();
2604
-
2605
- case TLSEXT_hash_sha256:
2606
- return EVP_sha256();
2607
-
2608
- case TLSEXT_hash_sha384:
2609
- return EVP_sha384();
2610
-
2611
- case TLSEXT_hash_sha512:
2612
- return EVP_sha512();
2613
-
2614
- default:
2615
- return NULL;
2616
- }
2617
- }
2618
-
2619
- /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2620
- * algorithm |sig_alg|. It returns -1 if the type is unknown. */
2621
- static int tls12_get_pkey_type(uint8_t sig_alg) {
2622
- switch (sig_alg) {
2623
- case TLSEXT_signature_rsa:
2624
- return EVP_PKEY_RSA;
2625
-
2626
- case TLSEXT_signature_ecdsa:
2627
- return EVP_PKEY_EC;
2628
-
2629
- default:
2630
- return -1;
2631
- }
2632
- }
2633
-
2634
- OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2635
- sizeof_tls_sigalgs_is_not_two);
2636
-
2637
3113
  int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
2638
3114
  /* Extension ignored for inappropriate versions */
2639
3115
  if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
2640
3116
  return 1;
2641
3117
  }
2642
3118
 
2643
- CERT *const cert = ssl->cert;
2644
- OPENSSL_free(cert->peer_sigalgs);
2645
- cert->peer_sigalgs = NULL;
2646
- cert->peer_sigalgslen = 0;
3119
+ SSL_HANDSHAKE *hs = ssl->s3->hs;
3120
+ OPENSSL_free(hs->peer_sigalgs);
3121
+ hs->peer_sigalgs = NULL;
3122
+ hs->num_peer_sigalgs = 0;
2647
3123
 
2648
3124
  size_t num_sigalgs = CBS_len(in_sigalgs);
2649
-
2650
3125
  if (num_sigalgs % 2 != 0) {
2651
3126
  return 0;
2652
3127
  }
@@ -2658,22 +3133,18 @@ int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
2658
3133
  return 1;
2659
3134
  }
2660
3135
 
2661
- /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2662
- * (statically asserted above) and we just divided |num_sigalgs| by two. */
2663
- cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2664
- if (cert->peer_sigalgs == NULL) {
3136
+ /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3137
+ * and we just divided |num_sigalgs| by two. */
3138
+ hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
3139
+ if (hs->peer_sigalgs == NULL) {
2665
3140
  return 0;
2666
3141
  }
2667
- cert->peer_sigalgslen = num_sigalgs;
3142
+ hs->num_peer_sigalgs = num_sigalgs;
2668
3143
 
2669
3144
  CBS sigalgs;
2670
3145
  CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2671
-
2672
- size_t i;
2673
- for (i = 0; i < num_sigalgs; i++) {
2674
- TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2675
- if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2676
- !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
3146
+ for (size_t i = 0; i < num_sigalgs; i++) {
3147
+ if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
2677
3148
  return 0;
2678
3149
  }
2679
3150
  }
@@ -2681,38 +3152,64 @@ int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
2681
3152
  return 1;
2682
3153
  }
2683
3154
 
2684
- const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
3155
+ int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
2685
3156
  CERT *cert = ssl->cert;
2686
- int type = ssl_private_key_type(ssl);
2687
- size_t i, j;
2688
-
2689
- static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2690
- NID_sha1};
2691
-
2692
- const int *digest_nids = kDefaultDigestList;
2693
- size_t num_digest_nids =
2694
- sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2695
- if (cert->digest_nids != NULL) {
2696
- digest_nids = cert->digest_nids;
2697
- num_digest_nids = cert->num_digest_nids;
2698
- }
2699
-
2700
- for (i = 0; i < num_digest_nids; i++) {
2701
- const int digest_nid = digest_nids[i];
2702
- for (j = 0; j < cert->peer_sigalgslen; j++) {
2703
- const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2704
- if (md == NULL ||
2705
- digest_nid != EVP_MD_type(md) ||
2706
- tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2707
- continue;
2708
- }
3157
+ SSL_HANDSHAKE *hs = ssl->s3->hs;
3158
+
3159
+ /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3160
+ * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3161
+ if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
3162
+ int type = ssl_private_key_type(ssl);
3163
+ if (type == NID_rsaEncryption) {
3164
+ *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
3165
+ return 1;
3166
+ }
3167
+ if (ssl_is_ecdsa_key_type(type)) {
3168
+ *out = SSL_SIGN_ECDSA_SHA1;
3169
+ return 1;
3170
+ }
3171
+ OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3172
+ return 0;
3173
+ }
2709
3174
 
2710
- return md;
3175
+ const uint16_t *sigalgs;
3176
+ size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
3177
+ if (cert->sigalgs != NULL) {
3178
+ sigalgs = cert->sigalgs;
3179
+ num_sigalgs = cert->num_sigalgs;
3180
+ }
3181
+
3182
+ const uint16_t *peer_sigalgs = hs->peer_sigalgs;
3183
+ size_t num_peer_sigalgs = hs->num_peer_sigalgs;
3184
+ if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
3185
+ /* If the client didn't specify any signature_algorithms extension then
3186
+ * we can assume that it supports SHA1. See
3187
+ * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3188
+ static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3189
+ SSL_SIGN_ECDSA_SHA1};
3190
+ peer_sigalgs = kDefaultPeerAlgorithms;
3191
+ num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
3192
+ }
3193
+
3194
+ for (size_t i = 0; i < num_sigalgs; i++) {
3195
+ uint16_t sigalg = sigalgs[i];
3196
+ /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3197
+ * negotiated. */
3198
+ if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3199
+ !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3200
+ continue;
3201
+ }
3202
+
3203
+ for (size_t j = 0; j < num_peer_sigalgs; j++) {
3204
+ if (sigalg == peer_sigalgs[j]) {
3205
+ *out = sigalg;
3206
+ return 1;
3207
+ }
2711
3208
  }
2712
3209
  }
2713
3210
 
2714
- /* If no suitable digest may be found, default to SHA-1. */
2715
- return EVP_sha1();
3211
+ OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3212
+ return 0;
2716
3213
  }
2717
3214
 
2718
3215
  int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
@@ -2727,7 +3224,7 @@ int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2727
3224
  static const char kClientIDMagic[] = "TLS Channel ID signature";
2728
3225
  EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2729
3226
 
2730
- if (ssl->hit) {
3227
+ if (ssl->session != NULL) {
2731
3228
  static const char kResumptionMagic[] = "Resumption";
2732
3229
  EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2733
3230
  if (ssl->session->original_handshake_hash_len == 0) {
@@ -2757,25 +3254,26 @@ err:
2757
3254
  }
2758
3255
 
2759
3256
  /* tls1_record_handshake_hashes_for_channel_id records the current handshake
2760
- * hashes in |ssl->session| so that Channel ID resumptions can sign that
3257
+ * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
2761
3258
  * data. */
2762
3259
  int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
2763
3260
  int digest_len;
2764
3261
  /* This function should never be called for a resumed session because the
2765
3262
  * handshake hashes that we wish to record are for the original, full
2766
3263
  * handshake. */
2767
- if (ssl->hit) {
3264
+ if (ssl->session != NULL) {
2768
3265
  return -1;
2769
3266
  }
2770
3267
 
2771
3268
  digest_len =
2772
- tls1_handshake_digest(ssl, ssl->session->original_handshake_hash,
2773
- sizeof(ssl->session->original_handshake_hash));
3269
+ tls1_handshake_digest(
3270
+ ssl, ssl->s3->new_session->original_handshake_hash,
3271
+ sizeof(ssl->s3->new_session->original_handshake_hash));
2774
3272
  if (digest_len < 0) {
2775
3273
  return -1;
2776
3274
  }
2777
3275
 
2778
- ssl->session->original_handshake_hash_len = digest_len;
3276
+ ssl->s3->new_session->original_handshake_hash_len = digest_len;
2779
3277
 
2780
3278
  return 1;
2781
3279
  }