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
@@ -116,17 +116,15 @@
116
116
 
117
117
  #include <openssl/ssl.h>
118
118
 
119
- #include <stdio.h>
120
119
  #include <string.h>
121
120
 
122
121
  #include <openssl/bytestring.h>
123
122
  #include <openssl/err.h>
124
- #include <openssl/obj.h>
125
123
 
126
124
  #include "internal.h"
127
125
 
128
126
 
129
- const SRTP_PROTECTION_PROFILE kSRTPProfiles[] = {
127
+ static const SRTP_PROTECTION_PROFILE kSRTPProfiles[] = {
130
128
  {
131
129
  "SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80,
132
130
  },
@@ -162,27 +160,27 @@ static int find_profile_by_name(const char *profile_name,
162
160
 
163
161
  static int ssl_ctx_make_profiles(const char *profiles_string,
164
162
  STACK_OF(SRTP_PROTECTION_PROFILE) **out) {
165
- STACK_OF(SRTP_PROTECTION_PROFILE) *profiles;
166
-
167
- const char *col;
168
- const char *ptr = profiles_string;
169
-
170
- profiles = sk_SRTP_PROTECTION_PROFILE_new_null();
163
+ STACK_OF(SRTP_PROTECTION_PROFILE) *profiles =
164
+ sk_SRTP_PROTECTION_PROFILE_new_null();
171
165
  if (profiles == NULL) {
172
166
  OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES);
173
167
  return 0;
174
168
  }
175
169
 
170
+ const char *col;
171
+ const char *ptr = profiles_string;
176
172
  do {
177
- const SRTP_PROTECTION_PROFILE *p;
178
-
179
173
  col = strchr(ptr, ':');
180
- if (find_profile_by_name(ptr, &p,
181
- col ? (size_t)(col - ptr) : strlen(ptr))) {
182
- sk_SRTP_PROTECTION_PROFILE_push(profiles, p);
183
- } else {
174
+
175
+ const SRTP_PROTECTION_PROFILE *profile;
176
+ if (!find_profile_by_name(ptr, &profile,
177
+ col ? (size_t)(col - ptr) : strlen(ptr))) {
184
178
  OPENSSL_PUT_ERROR(SSL, SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE);
185
- return 0;
179
+ goto err;
180
+ }
181
+
182
+ if (!sk_SRTP_PROTECTION_PROFILE_push(profiles, profile)) {
183
+ goto err;
186
184
  }
187
185
 
188
186
  if (col) {
@@ -190,9 +188,13 @@ static int ssl_ctx_make_profiles(const char *profiles_string,
190
188
  }
191
189
  } while (col);
192
190
 
191
+ sk_SRTP_PROTECTION_PROFILE_free(*out);
193
192
  *out = profiles;
194
-
195
193
  return 1;
194
+
195
+ err:
196
+ sk_SRTP_PROTECTION_PROFILE_free(profiles);
197
+ return 0;
196
198
  }
197
199
 
198
200
  int SSL_CTX_set_srtp_profiles(SSL_CTX *ctx, const char *profiles) {
@@ -212,7 +214,7 @@ STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *ssl) {
212
214
  return ssl->srtp_profiles;
213
215
  }
214
216
 
215
- if (ssl->ctx != NULL && ssl->ctx->srtp_profiles != NULL) {
217
+ if (ssl->ctx->srtp_profiles != NULL) {
216
218
  return ssl->ctx->srtp_profiles;
217
219
  }
218
220
 
@@ -56,51 +56,124 @@
56
56
 
57
57
  #include <openssl/ssl.h>
58
58
 
59
+ #include <assert.h>
60
+ #include <string.h>
61
+
62
+ #include <openssl/buf.h>
63
+ #include <openssl/err.h>
64
+
59
65
  #include "internal.h"
60
66
 
61
67
 
62
- static const SSL_PROTOCOL_METHOD DTLS_protocol_method = {
68
+ static int dtls1_version_from_wire(uint16_t *out_version,
69
+ uint16_t wire_version) {
70
+ switch (wire_version) {
71
+ case DTLS1_VERSION:
72
+ /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
73
+ *out_version = TLS1_1_VERSION;
74
+ return 1;
75
+ case DTLS1_2_VERSION:
76
+ *out_version = TLS1_2_VERSION;
77
+ return 1;
78
+ }
79
+
80
+ return 0;
81
+ }
82
+
83
+ static uint16_t dtls1_version_to_wire(uint16_t version) {
84
+ switch (version) {
85
+ case TLS1_1_VERSION:
86
+ /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
87
+ return DTLS1_VERSION;
88
+ case TLS1_2_VERSION:
89
+ return DTLS1_2_VERSION;
90
+ }
91
+
92
+ /* It is an error to use this function with an invalid version. */
93
+ assert(0);
94
+ return 0;
95
+ }
96
+
97
+ static int dtls1_set_read_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
98
+ /* Cipher changes are illegal when there are buffered incoming messages. */
99
+ if (dtls_has_incoming_messages(ssl)) {
100
+ OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFERED_MESSAGES_ON_CIPHER_CHANGE);
101
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
102
+ SSL_AEAD_CTX_free(aead_ctx);
103
+ return 0;
104
+ }
105
+
106
+ ssl->d1->r_epoch++;
107
+ memset(&ssl->d1->bitmap, 0, sizeof(ssl->d1->bitmap));
108
+ memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
109
+
110
+ SSL_AEAD_CTX_free(ssl->s3->aead_read_ctx);
111
+ ssl->s3->aead_read_ctx = aead_ctx;
112
+ return 1;
113
+ }
114
+
115
+ static int dtls1_set_write_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
116
+ ssl->d1->w_epoch++;
117
+ memcpy(ssl->d1->last_write_sequence, ssl->s3->write_sequence,
118
+ sizeof(ssl->s3->write_sequence));
119
+ memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
120
+
121
+ SSL_AEAD_CTX_free(ssl->s3->aead_write_ctx);
122
+ ssl->s3->aead_write_ctx = aead_ctx;
123
+ return 1;
124
+ }
125
+
126
+ static const SSL_PROTOCOL_METHOD kDTLSProtocolMethod = {
63
127
  1 /* is_dtls */,
128
+ TLS1_1_VERSION,
129
+ TLS1_2_VERSION,
130
+ dtls1_version_from_wire,
131
+ dtls1_version_to_wire,
64
132
  dtls1_new,
65
133
  dtls1_free,
66
- dtls1_accept,
67
- dtls1_connect,
68
134
  dtls1_get_message,
135
+ dtls1_hash_current_message,
136
+ dtls1_release_current_message,
69
137
  dtls1_read_app_data,
70
138
  dtls1_read_change_cipher_spec,
71
139
  dtls1_read_close_notify,
72
140
  dtls1_write_app_data,
73
141
  dtls1_dispatch_alert,
74
142
  dtls1_supports_cipher,
75
- DTLS1_HM_HEADER_LENGTH,
76
- dtls1_set_handshake_header,
77
- dtls1_handshake_write,
143
+ dtls1_init_message,
144
+ dtls1_finish_message,
145
+ dtls1_write_message,
146
+ dtls1_send_change_cipher_spec,
147
+ dtls1_expect_flight,
148
+ dtls1_received_flight,
149
+ dtls1_set_read_state,
150
+ dtls1_set_write_state,
78
151
  };
79
152
 
80
153
  const SSL_METHOD *DTLS_method(void) {
81
- static const SSL_METHOD method = {
154
+ static const SSL_METHOD kMethod = {
82
155
  0,
83
- &DTLS_protocol_method,
156
+ &kDTLSProtocolMethod,
84
157
  };
85
- return &method;
158
+ return &kMethod;
86
159
  }
87
160
 
88
161
  /* Legacy version-locked methods. */
89
162
 
90
163
  const SSL_METHOD *DTLSv1_2_method(void) {
91
- static const SSL_METHOD method = {
164
+ static const SSL_METHOD kMethod = {
92
165
  DTLS1_2_VERSION,
93
- &DTLS_protocol_method,
166
+ &kDTLSProtocolMethod,
94
167
  };
95
- return &method;
168
+ return &kMethod;
96
169
  }
97
170
 
98
171
  const SSL_METHOD *DTLSv1_method(void) {
99
- static const SSL_METHOD method = {
172
+ static const SSL_METHOD kMethod = {
100
173
  DTLS1_VERSION,
101
- &DTLS_protocol_method,
174
+ &kDTLSProtocolMethod,
102
175
  };
103
- return &method;
176
+ return &kMethod;
104
177
  }
105
178
 
106
179
  /* Legacy side-specific methods. */
@@ -118,6 +118,7 @@
118
118
  #include <openssl/err.h>
119
119
 
120
120
  #include "internal.h"
121
+ #include "../crypto/internal.h"
121
122
 
122
123
 
123
124
  /* to_u64_be treats |in| as a 8-byte big-endian integer and returns the value as
@@ -171,10 +172,12 @@ static void dtls1_bitmap_record(DTLS1_BITMAP *bitmap,
171
172
  }
172
173
  }
173
174
 
174
- enum ssl_open_record_t dtls_open_record(
175
- SSL *ssl, uint8_t *out_type, uint8_t *out, size_t *out_len,
176
- size_t *out_consumed, uint8_t *out_alert, size_t max_out, const uint8_t *in,
177
- size_t in_len) {
175
+ enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
176
+ size_t *out_consumed,
177
+ uint8_t *out_alert, uint8_t *in,
178
+ size_t in_len) {
179
+ *out_consumed = 0;
180
+
178
181
  CBS cbs;
179
182
  CBS_init(&cbs, in, in_len);
180
183
 
@@ -195,10 +198,8 @@ enum ssl_open_record_t dtls_open_record(
195
198
  return ssl_open_record_discard;
196
199
  }
197
200
 
198
- if (ssl->msg_callback != NULL) {
199
- ssl->msg_callback(0 /* read */, 0, SSL3_RT_HEADER, in,
200
- DTLS1_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg);
201
- }
201
+ ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, in,
202
+ DTLS1_RT_HEADER_LENGTH);
202
203
 
203
204
  uint16_t epoch = (((uint16_t)sequence[0]) << 8) | sequence[1];
204
205
  if (epoch != ssl->d1->r_epoch ||
@@ -211,11 +212,9 @@ enum ssl_open_record_t dtls_open_record(
211
212
  return ssl_open_record_discard;
212
213
  }
213
214
 
214
- /* Decrypt the body. */
215
- size_t plaintext_len;
216
- if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, &plaintext_len, max_out,
217
- type, version, sequence, CBS_data(&body),
218
- CBS_len(&body))) {
215
+ /* Decrypt the body in-place. */
216
+ if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, type, version, sequence,
217
+ (uint8_t *)CBS_data(&body), CBS_len(&body))) {
219
218
  /* Bad packets are silently dropped in DTLS. See section 4.2.1 of RFC 6347.
220
219
  * Clear the error queue of any errors decryption may have added. Drop the
221
220
  * entire packet as it must not have come from the peer.
@@ -226,9 +225,10 @@ enum ssl_open_record_t dtls_open_record(
226
225
  *out_consumed = in_len - CBS_len(&cbs);
227
226
  return ssl_open_record_discard;
228
227
  }
228
+ *out_consumed = in_len - CBS_len(&cbs);
229
229
 
230
230
  /* Check the plaintext length. */
231
- if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH) {
231
+ if (CBS_len(out) > SSL3_RT_MAX_PLAIN_LENGTH) {
232
232
  OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
233
233
  *out_alert = SSL_AD_RECORD_OVERFLOW;
234
234
  return ssl_open_record_error;
@@ -239,15 +239,24 @@ enum ssl_open_record_t dtls_open_record(
239
239
  /* TODO(davidben): Limit the number of empty records as in TLS? This is only
240
240
  * useful if we also limit discarded packets. */
241
241
 
242
+ if (type == SSL3_RT_ALERT) {
243
+ return ssl_process_alert(ssl, out_alert, CBS_data(out), CBS_len(out));
244
+ }
245
+
246
+ ssl->s3->warning_alert_count = 0;
247
+
242
248
  *out_type = type;
243
- *out_len = plaintext_len;
244
- *out_consumed = in_len - CBS_len(&cbs);
245
249
  return ssl_open_record_success;
246
250
  }
247
251
 
248
252
  int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
249
253
  uint8_t type, const uint8_t *in, size_t in_len,
250
254
  enum dtls1_use_epoch_t use_epoch) {
255
+ if (buffers_alias(in, in_len, out, max_out)) {
256
+ OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
257
+ return 0;
258
+ }
259
+
251
260
  /* Determine the parameters for the current epoch. */
252
261
  uint16_t epoch = ssl->d1->w_epoch;
253
262
  SSL_AEAD_CTX *aead = ssl->s3->aead_write_ctx;
@@ -265,12 +274,6 @@ int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
265
274
  OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
266
275
  return 0;
267
276
  }
268
- /* Check the record header does not alias any part of the input.
269
- * |SSL_AEAD_CTX_seal| will internally enforce other aliasing requirements. */
270
- if (in < out + DTLS1_RT_HEADER_LENGTH && out < in + in_len) {
271
- OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
272
- return 0;
273
- }
274
277
 
275
278
  out[0] = type;
276
279
 
@@ -299,10 +302,8 @@ int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
299
302
 
300
303
  *out_len = DTLS1_RT_HEADER_LENGTH + ciphertext_len;
301
304
 
302
- if (ssl->msg_callback) {
303
- ssl->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out,
304
- DTLS1_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg);
305
- }
305
+ ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, out,
306
+ DTLS1_RT_HEADER_LENGTH);
306
307
 
307
308
  return 1;
308
309
  }
@@ -150,7 +150,6 @@
150
150
  #include <openssl/ssl.h>
151
151
 
152
152
  #include <assert.h>
153
- #include <stdio.h>
154
153
  #include <string.h>
155
154
 
156
155
  #include <openssl/bn.h>
@@ -163,184 +162,184 @@
163
162
  #include <openssl/evp.h>
164
163
  #include <openssl/md5.h>
165
164
  #include <openssl/mem.h>
166
- #include <openssl/obj.h>
167
165
  #include <openssl/rand.h>
168
166
  #include <openssl/x509.h>
169
167
  #include <openssl/x509v3.h>
170
168
 
171
169
  #include "internal.h"
172
- #include "../crypto/dh/internal.h"
173
170
 
174
171
 
172
+ static int ssl3_send_client_hello(SSL *ssl);
173
+ static int dtls1_get_hello_verify(SSL *ssl);
174
+ static int ssl3_get_server_hello(SSL *ssl);
175
+ static int ssl3_get_server_certificate(SSL *ssl);
176
+ static int ssl3_get_cert_status(SSL *ssl);
177
+ static int ssl3_verify_server_cert(SSL *ssl);
178
+ static int ssl3_get_server_key_exchange(SSL *ssl);
179
+ static int ssl3_get_certificate_request(SSL *ssl);
180
+ static int ssl3_get_server_hello_done(SSL *ssl);
181
+ static int ssl3_send_client_certificate(SSL *ssl);
182
+ static int ssl3_send_client_key_exchange(SSL *ssl);
183
+ static int ssl3_send_cert_verify(SSL *ssl);
184
+ static int ssl3_send_next_proto(SSL *ssl);
185
+ static int ssl3_send_channel_id(SSL *ssl);
186
+ static int ssl3_get_new_session_ticket(SSL *ssl);
187
+
175
188
  int ssl3_connect(SSL *ssl) {
176
- BUF_MEM *buf = NULL;
177
- void (*cb)(const SSL *ssl, int type, int value) = NULL;
178
189
  int ret = -1;
179
- int new_state, state, skip = 0;
190
+ int state, skip = 0;
180
191
 
181
192
  assert(ssl->handshake_func == ssl3_connect);
182
193
  assert(!ssl->server);
183
- assert(!SSL_IS_DTLS(ssl));
184
-
185
- ERR_clear_error();
186
- ERR_clear_system_error();
187
-
188
- if (ssl->info_callback != NULL) {
189
- cb = ssl->info_callback;
190
- } else if (ssl->ctx->info_callback != NULL) {
191
- cb = ssl->ctx->info_callback;
192
- }
193
-
194
- ssl->in_handshake++;
195
194
 
196
195
  for (;;) {
197
196
  state = ssl->state;
198
197
 
199
198
  switch (ssl->state) {
200
- case SSL_ST_CONNECT:
201
- if (cb != NULL) {
202
- cb(ssl, SSL_CB_HANDSHAKE_START, 1);
203
- }
204
-
205
- if (ssl->init_buf == NULL) {
206
- buf = BUF_MEM_new();
207
- if (buf == NULL ||
208
- !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
209
- ret = -1;
210
- goto end;
211
- }
199
+ case SSL_ST_INIT:
200
+ ssl->state = SSL_ST_CONNECT;
201
+ skip = 1;
202
+ break;
212
203
 
213
- ssl->init_buf = buf;
214
- buf = NULL;
215
- }
204
+ case SSL_ST_CONNECT:
205
+ ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
216
206
 
217
- if (!ssl_init_wbio_buffer(ssl, 0)) {
207
+ ssl->s3->hs = ssl_handshake_new(tls13_client_handshake);
208
+ if (ssl->s3->hs == NULL) {
218
209
  ret = -1;
219
210
  goto end;
220
211
  }
221
212
 
222
- /* don't push the buffering BIO quite yet */
223
-
224
- if (!ssl3_init_handshake_buffer(ssl)) {
225
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
213
+ if (!ssl_init_wbio_buffer(ssl)) {
226
214
  ret = -1;
227
215
  goto end;
228
216
  }
229
217
 
230
218
  ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
231
- ssl->init_num = 0;
232
219
  break;
233
220
 
234
221
  case SSL3_ST_CW_CLNT_HELLO_A:
235
222
  case SSL3_ST_CW_CLNT_HELLO_B:
236
- ssl->shutdown = 0;
237
223
  ret = ssl3_send_client_hello(ssl);
238
224
  if (ret <= 0) {
239
225
  goto end;
240
226
  }
241
- ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
242
- ssl->init_num = 0;
243
227
 
244
- /* turn on buffering for the next lot of output */
245
- if (ssl->bbio != ssl->wbio) {
246
- ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
228
+ if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
229
+ ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
230
+ } else {
231
+ ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
247
232
  }
233
+ ssl->state = SSL3_ST_CW_FLUSH;
234
+ break;
248
235
 
236
+ case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
237
+ assert(SSL_is_dtls(ssl));
238
+ ret = dtls1_get_hello_verify(ssl);
239
+ if (ret <= 0) {
240
+ goto end;
241
+ }
242
+ if (ssl->d1->send_cookie) {
243
+ ssl->method->received_flight(ssl);
244
+ ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
245
+ } else {
246
+ ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
247
+ }
249
248
  break;
250
249
 
251
250
  case SSL3_ST_CR_SRVR_HELLO_A:
252
- case SSL3_ST_CR_SRVR_HELLO_B:
253
251
  ret = ssl3_get_server_hello(ssl);
252
+ if (ssl->state == SSL_ST_TLS13) {
253
+ break;
254
+ }
254
255
  if (ret <= 0) {
255
256
  goto end;
256
257
  }
257
258
 
258
- if (ssl->hit) {
259
- ssl->state = SSL3_ST_CR_CHANGE;
260
- if (ssl->tlsext_ticket_expected) {
261
- /* receive renewed session ticket */
262
- ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
263
- }
259
+ if (ssl->session != NULL) {
260
+ ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
264
261
  } else {
265
262
  ssl->state = SSL3_ST_CR_CERT_A;
266
263
  }
267
- ssl->init_num = 0;
268
264
  break;
269
265
 
270
266
  case SSL3_ST_CR_CERT_A:
271
- case SSL3_ST_CR_CERT_B:
272
- if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
267
+ if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
273
268
  ret = ssl3_get_server_certificate(ssl);
274
269
  if (ret <= 0) {
275
270
  goto end;
276
271
  }
277
- if (ssl->s3->tmp.certificate_status_expected) {
278
- ssl->state = SSL3_ST_CR_CERT_STATUS_A;
279
- } else {
280
- ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
272
+ } else {
273
+ skip = 1;
274
+ }
275
+ ssl->state = SSL3_ST_CR_CERT_STATUS_A;
276
+ break;
277
+
278
+ case SSL3_ST_CR_CERT_STATUS_A:
279
+ if (ssl->s3->tmp.certificate_status_expected) {
280
+ ret = ssl3_get_cert_status(ssl);
281
+ if (ret <= 0) {
282
+ goto end;
281
283
  }
282
284
  } else {
283
285
  skip = 1;
284
- ssl->state = SSL3_ST_CR_KEY_EXCH_A;
285
286
  }
286
- ssl->init_num = 0;
287
+ ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
287
288
  break;
288
289
 
289
290
  case SSL3_ST_VERIFY_SERVER_CERT:
290
- ret = ssl3_verify_server_cert(ssl);
291
- if (ret <= 0) {
292
- goto end;
291
+ if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
292
+ ret = ssl3_verify_server_cert(ssl);
293
+ if (ret <= 0) {
294
+ goto end;
295
+ }
296
+ } else {
297
+ skip = 1;
293
298
  }
294
-
295
299
  ssl->state = SSL3_ST_CR_KEY_EXCH_A;
296
- ssl->init_num = 0;
297
300
  break;
298
301
 
299
302
  case SSL3_ST_CR_KEY_EXCH_A:
300
- case SSL3_ST_CR_KEY_EXCH_B:
301
303
  ret = ssl3_get_server_key_exchange(ssl);
302
304
  if (ret <= 0) {
303
305
  goto end;
304
306
  }
305
307
  ssl->state = SSL3_ST_CR_CERT_REQ_A;
306
- ssl->init_num = 0;
307
308
  break;
308
309
 
309
310
  case SSL3_ST_CR_CERT_REQ_A:
310
- case SSL3_ST_CR_CERT_REQ_B:
311
- ret = ssl3_get_certificate_request(ssl);
312
- if (ret <= 0) {
313
- goto end;
311
+ if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
312
+ ret = ssl3_get_certificate_request(ssl);
313
+ if (ret <= 0) {
314
+ goto end;
315
+ }
316
+ } else {
317
+ skip = 1;
314
318
  }
315
319
  ssl->state = SSL3_ST_CR_SRVR_DONE_A;
316
- ssl->init_num = 0;
317
320
  break;
318
321
 
319
322
  case SSL3_ST_CR_SRVR_DONE_A:
320
- case SSL3_ST_CR_SRVR_DONE_B:
321
- ret = ssl3_get_server_done(ssl);
323
+ ret = ssl3_get_server_hello_done(ssl);
322
324
  if (ret <= 0) {
323
325
  goto end;
324
326
  }
325
- if (ssl->s3->tmp.cert_req) {
326
- ssl->state = SSL3_ST_CW_CERT_A;
327
- } else {
328
- ssl->state = SSL3_ST_CW_KEY_EXCH_A;
329
- }
330
- ssl->init_num = 0;
331
-
327
+ ssl->method->received_flight(ssl);
328
+ ssl->state = SSL3_ST_CW_CERT_A;
332
329
  break;
333
330
 
334
331
  case SSL3_ST_CW_CERT_A:
335
332
  case SSL3_ST_CW_CERT_B:
336
333
  case SSL3_ST_CW_CERT_C:
337
- case SSL3_ST_CW_CERT_D:
338
- ret = ssl3_send_client_certificate(ssl);
339
- if (ret <= 0) {
340
- goto end;
334
+ if (ssl->s3->tmp.cert_request) {
335
+ ret = ssl3_send_client_certificate(ssl);
336
+ if (ret <= 0) {
337
+ goto end;
338
+ }
339
+ } else {
340
+ skip = 1;
341
341
  }
342
342
  ssl->state = SSL3_ST_CW_KEY_EXCH_A;
343
- ssl->init_num = 0;
344
343
  break;
345
344
 
346
345
  case SSL3_ST_CW_KEY_EXCH_A:
@@ -349,44 +348,30 @@ int ssl3_connect(SSL *ssl) {
349
348
  if (ret <= 0) {
350
349
  goto end;
351
350
  }
352
- /* For TLS, cert_req is set to 2, so a cert chain
353
- * of nothing is sent, but no verify packet is sent */
354
- if (ssl->s3->tmp.cert_req == 1) {
355
- ssl->state = SSL3_ST_CW_CERT_VRFY_A;
356
- } else {
357
- ssl->state = SSL3_ST_CW_CHANGE_A;
358
- }
359
-
360
- ssl->init_num = 0;
351
+ ssl->state = SSL3_ST_CW_CERT_VRFY_A;
361
352
  break;
362
353
 
363
354
  case SSL3_ST_CW_CERT_VRFY_A:
364
355
  case SSL3_ST_CW_CERT_VRFY_B:
365
356
  case SSL3_ST_CW_CERT_VRFY_C:
366
- ret = ssl3_send_cert_verify(ssl);
367
- if (ret <= 0) {
368
- goto end;
357
+ if (ssl->s3->tmp.cert_request) {
358
+ ret = ssl3_send_cert_verify(ssl);
359
+ if (ret <= 0) {
360
+ goto end;
361
+ }
362
+ } else {
363
+ skip = 1;
369
364
  }
370
- ssl->state = SSL3_ST_CW_CHANGE_A;
371
- ssl->init_num = 0;
365
+ ssl->state = SSL3_ST_CW_CHANGE;
372
366
  break;
373
367
 
374
- case SSL3_ST_CW_CHANGE_A:
375
- case SSL3_ST_CW_CHANGE_B:
376
- ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
377
- SSL3_ST_CW_CHANGE_B);
368
+ case SSL3_ST_CW_CHANGE:
369
+ ret = ssl->method->send_change_cipher_spec(ssl);
378
370
  if (ret <= 0) {
379
371
  goto end;
380
372
  }
381
373
 
382
- ssl->state = SSL3_ST_CW_FINISHED_A;
383
- if (ssl->s3->tlsext_channel_id_valid) {
384
- ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
385
- }
386
- if (ssl->s3->next_proto_neg_seen) {
387
- ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
388
- }
389
- ssl->init_num = 0;
374
+ ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
390
375
 
391
376
  if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
392
377
  ret = -1;
@@ -397,23 +382,26 @@ int ssl3_connect(SSL *ssl) {
397
382
 
398
383
  case SSL3_ST_CW_NEXT_PROTO_A:
399
384
  case SSL3_ST_CW_NEXT_PROTO_B:
400
- ret = ssl3_send_next_proto(ssl);
401
- if (ret <= 0) {
402
- goto end;
403
- }
404
-
405
- if (ssl->s3->tlsext_channel_id_valid) {
406
- ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
385
+ if (ssl->s3->next_proto_neg_seen) {
386
+ ret = ssl3_send_next_proto(ssl);
387
+ if (ret <= 0) {
388
+ goto end;
389
+ }
407
390
  } else {
408
- ssl->state = SSL3_ST_CW_FINISHED_A;
391
+ skip = 1;
409
392
  }
393
+ ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
410
394
  break;
411
395
 
412
396
  case SSL3_ST_CW_CHANNEL_ID_A:
413
397
  case SSL3_ST_CW_CHANNEL_ID_B:
414
- ret = ssl3_send_channel_id(ssl);
415
- if (ret <= 0) {
416
- goto end;
398
+ if (ssl->s3->tlsext_channel_id_valid) {
399
+ ret = ssl3_send_channel_id(ssl);
400
+ if (ret <= 0) {
401
+ goto end;
402
+ }
403
+ } else {
404
+ skip = 1;
417
405
  }
418
406
  ssl->state = SSL3_ST_CW_FINISHED_A;
419
407
  break;
@@ -427,7 +415,7 @@ int ssl3_connect(SSL *ssl) {
427
415
  }
428
416
  ssl->state = SSL3_ST_CW_FLUSH;
429
417
 
430
- if (ssl->hit) {
418
+ if (ssl->session != NULL) {
431
419
  ssl->s3->tmp.next_state = SSL_ST_OK;
432
420
  } else {
433
421
  /* This is a non-resumption handshake. If it involves ChannelID, then
@@ -445,39 +433,33 @@ int ssl3_connect(SSL *ssl) {
445
433
  !ssl->s3->initial_handshake_complete) {
446
434
  ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
447
435
  } else {
448
- /* Allow NewSessionTicket if ticket expected */
449
- if (ssl->tlsext_ticket_expected) {
450
- ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
451
- } else {
452
- ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
453
- }
436
+ ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
454
437
  }
455
438
  }
456
- ssl->init_num = 0;
457
439
  break;
458
440
 
441
+ case SSL3_ST_FALSE_START:
442
+ ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
443
+ ssl->s3->tmp.in_false_start = 1;
444
+
445
+ ssl_free_wbio_buffer(ssl);
446
+ ret = 1;
447
+ goto end;
448
+
459
449
  case SSL3_ST_CR_SESSION_TICKET_A:
460
- case SSL3_ST_CR_SESSION_TICKET_B:
461
- ret = ssl3_get_new_session_ticket(ssl);
462
- if (ret <= 0) {
463
- goto end;
450
+ if (ssl->tlsext_ticket_expected) {
451
+ ret = ssl3_get_new_session_ticket(ssl);
452
+ if (ret <= 0) {
453
+ goto end;
454
+ }
455
+ } else {
456
+ skip = 1;
464
457
  }
465
458
  ssl->state = SSL3_ST_CR_CHANGE;
466
- ssl->init_num = 0;
467
- break;
468
-
469
- case SSL3_ST_CR_CERT_STATUS_A:
470
- case SSL3_ST_CR_CERT_STATUS_B:
471
- ret = ssl3_get_cert_status(ssl);
472
- if (ret <= 0) {
473
- goto end;
474
- }
475
- ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
476
- ssl->init_num = 0;
477
459
  break;
478
460
 
479
461
  case SSL3_ST_CR_CHANGE:
480
- ret = ssl->method->ssl_read_change_cipher_spec(ssl);
462
+ ret = ssl->method->read_change_cipher_spec(ssl);
481
463
  if (ret <= 0) {
482
464
  goto end;
483
465
  }
@@ -490,57 +472,76 @@ int ssl3_connect(SSL *ssl) {
490
472
  break;
491
473
 
492
474
  case SSL3_ST_CR_FINISHED_A:
493
- case SSL3_ST_CR_FINISHED_B:
494
- ret = ssl3_get_finished(ssl, SSL3_ST_CR_FINISHED_A,
495
- SSL3_ST_CR_FINISHED_B);
475
+ ret = ssl3_get_finished(ssl);
496
476
  if (ret <= 0) {
497
477
  goto end;
498
478
  }
479
+ ssl->method->received_flight(ssl);
499
480
 
500
- if (ssl->hit) {
501
- ssl->state = SSL3_ST_CW_CHANGE_A;
481
+ if (ssl->session != NULL) {
482
+ ssl->state = SSL3_ST_CW_CHANGE;
502
483
  } else {
503
484
  ssl->state = SSL_ST_OK;
504
485
  }
505
- ssl->init_num = 0;
506
486
  break;
507
487
 
508
488
  case SSL3_ST_CW_FLUSH:
509
- ssl->rwstate = SSL_WRITING;
510
489
  if (BIO_flush(ssl->wbio) <= 0) {
490
+ ssl->rwstate = SSL_WRITING;
511
491
  ret = -1;
512
492
  goto end;
513
493
  }
514
- ssl->rwstate = SSL_NOTHING;
515
494
  ssl->state = ssl->s3->tmp.next_state;
495
+ if (ssl->state != SSL_ST_OK) {
496
+ ssl->method->expect_flight(ssl);
497
+ }
516
498
  break;
517
499
 
518
- case SSL3_ST_FALSE_START:
519
- /* Allow NewSessionTicket if ticket expected */
520
- if (ssl->tlsext_ticket_expected) {
521
- ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
522
- } else {
523
- ssl->state = SSL3_ST_CR_CHANGE;
500
+ case SSL_ST_TLS13:
501
+ ret = tls13_handshake(ssl);
502
+ if (ret <= 0) {
503
+ goto end;
524
504
  }
525
- ssl->s3->tmp.in_false_start = 1;
526
-
527
- ssl_free_wbio_buffer(ssl);
528
- ret = 1;
529
- goto end;
505
+ ssl->state = SSL_ST_OK;
506
+ break;
530
507
 
531
508
  case SSL_ST_OK:
532
- /* clean a few things up */
509
+ /* Clean a few things up. */
533
510
  ssl3_cleanup_key_block(ssl);
511
+ ssl->method->release_current_message(ssl, 1 /* free_buffer */);
512
+
513
+ SSL_SESSION_free(ssl->s3->established_session);
514
+ if (ssl->session != NULL) {
515
+ SSL_SESSION_up_ref(ssl->session);
516
+ ssl->s3->established_session = ssl->session;
517
+ } else {
518
+ /* We make a copy of the session in order to maintain the immutability
519
+ * of the new established_session due to False Start. The caller may
520
+ * have taken a reference to the temporary session. */
521
+ ssl->s3->established_session =
522
+ SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
523
+ if (ssl->s3->established_session == NULL) {
524
+ /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
525
+ * callers. */
526
+ ssl->state = SSL_ST_ERROR;
527
+ skip = 1;
528
+ ret = -1;
529
+ goto end;
530
+ }
531
+ ssl->s3->established_session->not_resumable = 0;
534
532
 
535
- BUF_MEM_free(ssl->init_buf);
536
- ssl->init_buf = NULL;
533
+ SSL_SESSION_free(ssl->s3->new_session);
534
+ ssl->s3->new_session = NULL;
535
+ }
537
536
 
538
537
  /* Remove write buffering now. */
539
538
  ssl_free_wbio_buffer(ssl);
540
539
 
540
+ ssl_handshake_free(ssl->s3->hs);
541
+ ssl->s3->hs = NULL;
542
+
541
543
  const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
542
544
 
543
- ssl->init_num = 0;
544
545
  ssl->s3->tmp.in_false_start = 0;
545
546
  ssl->s3->initial_handshake_complete = 1;
546
547
 
@@ -550,12 +551,12 @@ int ssl3_connect(SSL *ssl) {
550
551
  }
551
552
 
552
553
  ret = 1;
553
- /* ssl->server=0; */
554
-
555
- if (cb != NULL) {
556
- cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
557
- }
554
+ ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
555
+ goto end;
558
556
 
557
+ case SSL_ST_ERROR:
558
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
559
+ ret = -1;
559
560
  goto end;
560
561
 
561
562
  default:
@@ -564,27 +565,35 @@ int ssl3_connect(SSL *ssl) {
564
565
  goto end;
565
566
  }
566
567
 
567
- if (!ssl->s3->tmp.reuse_message && !skip) {
568
- if (cb != NULL && ssl->state != state) {
569
- new_state = ssl->state;
570
- ssl->state = state;
571
- cb(ssl, SSL_CB_CONNECT_LOOP, 1);
572
- ssl->state = new_state;
573
- }
568
+ if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
569
+ int new_state = ssl->state;
570
+ ssl->state = state;
571
+ ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
572
+ ssl->state = new_state;
574
573
  }
575
574
  skip = 0;
576
575
  }
577
576
 
578
577
  end:
579
- ssl->in_handshake--;
580
- BUF_MEM_free(buf);
581
- if (cb != NULL) {
582
- cb(ssl, SSL_CB_CONNECT_EXIT, ret);
583
- }
578
+ ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
579
+ return ret;
580
+ }
581
+
582
+ uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
583
+ /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
584
+ * a single byte repeatedly and ensures the values are deterministic. This
585
+ * allows the same ClientHello be sent twice for a HelloRetryRequest or the
586
+ * same group be advertised in both supported_groups and key_shares. */
587
+ uint16_t ret = ssl->s3->client_random[index];
588
+ /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
589
+ ret = (ret & 0xf0) | 0x0a;
590
+ ret |= ret << 8;
584
591
  return ret;
585
592
  }
586
593
 
587
- static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
594
+ static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
595
+ uint16_t min_version,
596
+ uint16_t max_version) {
588
597
  /* Prepare disabled cipher masks. */
589
598
  ssl_set_client_disabled(ssl);
590
599
 
@@ -593,25 +602,42 @@ static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
593
602
  return 0;
594
603
  }
595
604
 
605
+ /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
606
+ if (ssl->ctx->grease_enabled &&
607
+ !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
608
+ return 0;
609
+ }
610
+
596
611
  STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
597
612
 
598
613
  int any_enabled = 0;
599
- size_t i;
600
- for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
614
+ for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
601
615
  const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
602
616
  /* Skip disabled ciphers */
603
617
  if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
604
618
  (cipher->algorithm_auth & ssl->cert->mask_a)) {
605
619
  continue;
606
620
  }
607
- if (SSL_CIPHER_get_min_version(cipher) >
608
- ssl3_version_from_wire(ssl, ssl->client_version)) {
621
+ if (SSL_CIPHER_get_min_version(cipher) > max_version ||
622
+ SSL_CIPHER_get_max_version(cipher) < min_version) {
609
623
  continue;
610
624
  }
611
625
  any_enabled = 1;
612
626
  if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
613
627
  return 0;
614
628
  }
629
+ /* Add PSK ciphers for TLS 1.3 resumption. */
630
+ uint16_t session_version;
631
+ if (ssl->session != NULL &&
632
+ ssl->method->version_from_wire(&session_version,
633
+ ssl->session->ssl_version) &&
634
+ session_version >= TLS1_3_VERSION) {
635
+ uint16_t resumption_cipher;
636
+ if (ssl_cipher_get_ecdhe_psk_cipher(cipher, &resumption_cipher) &&
637
+ !CBB_add_u16(&child, resumption_cipher)) {
638
+ return 0;
639
+ }
640
+ }
615
641
  }
616
642
 
617
643
  /* If all ciphers were disabled, return the error to the caller. */
@@ -627,26 +653,64 @@ static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
627
653
  if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
628
654
  return 0;
629
655
  }
630
- /* The renegotiation extension is required to be at index zero. */
631
- ssl->s3->tmp.extensions.sent |= (1u << 0);
632
656
  }
633
657
 
634
- if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) &&
635
- !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
636
- return 0;
658
+ if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
659
+ if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
660
+ return 0;
661
+ }
637
662
  }
638
663
 
639
664
  return CBB_flush(out);
640
665
  }
641
666
 
642
- int ssl3_send_client_hello(SSL *ssl) {
667
+ int ssl_add_client_hello_body(SSL *ssl, CBB *body) {
668
+ uint16_t min_version, max_version;
669
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
670
+ return 0;
671
+ }
672
+
673
+ /* Renegotiations do not participate in session resumption. */
674
+ int has_session = ssl->session != NULL &&
675
+ !ssl->s3->initial_handshake_complete;
676
+
677
+ CBB child;
678
+ if (!CBB_add_u16(body, ssl->client_version) ||
679
+ !CBB_add_bytes(body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
680
+ !CBB_add_u8_length_prefixed(body, &child) ||
681
+ (has_session &&
682
+ !CBB_add_bytes(&child, ssl->session->session_id,
683
+ ssl->session->session_id_length))) {
684
+ return 0;
685
+ }
686
+
687
+ if (SSL_is_dtls(ssl)) {
688
+ if (!CBB_add_u8_length_prefixed(body, &child) ||
689
+ !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
690
+ return 0;
691
+ }
692
+ }
693
+
694
+ size_t header_len =
695
+ SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
696
+ if (!ssl_write_client_cipher_list(ssl, body, min_version, max_version) ||
697
+ !CBB_add_u8(body, 1 /* one compression method */) ||
698
+ !CBB_add_u8(body, 0 /* null compression */) ||
699
+ !ssl_add_clienthello_tlsext(ssl, body, header_len + CBB_len(body))) {
700
+ return 0;
701
+ }
702
+
703
+ return 1;
704
+ }
705
+
706
+ static int ssl3_send_client_hello(SSL *ssl) {
643
707
  if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
644
- return ssl_do_write(ssl);
708
+ return ssl->method->write_message(ssl);
645
709
  }
646
710
 
647
- /* In DTLS, reset the handshake buffer each time a new ClientHello is
648
- * assembled. We may send multiple if we receive HelloVerifyRequest. */
649
- if (SSL_IS_DTLS(ssl) && !ssl3_init_handshake_buffer(ssl)) {
711
+ /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
712
+ * may send multiple ClientHellos if we receive HelloVerifyRequest. */
713
+ if (!ssl3_init_handshake_buffer(ssl)) {
650
714
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
651
715
  return -1;
652
716
  }
@@ -654,99 +718,115 @@ int ssl3_send_client_hello(SSL *ssl) {
654
718
  CBB cbb;
655
719
  CBB_zero(&cbb);
656
720
 
721
+ uint16_t min_version, max_version;
722
+ if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
723
+ goto err;
724
+ }
725
+
657
726
  assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
658
727
  if (!ssl->s3->have_version) {
659
- uint16_t max_version = ssl3_get_max_client_version(ssl);
660
- /* Disabling all versions is silly: return an error. */
661
- if (max_version == 0) {
662
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
663
- goto err;
664
- }
665
-
666
- ssl->version = max_version;
728
+ ssl->version = ssl->method->version_to_wire(max_version);
667
729
  /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
668
730
  * although locked to a version, reuse the value. When using the plain RSA
669
731
  * key exchange, the ClientHello version is checked in the premaster secret.
670
732
  * Some servers fail when this value changes. */
671
- ssl->client_version = max_version;
733
+ ssl->client_version = ssl->version;
734
+
735
+ if (max_version >= TLS1_3_VERSION) {
736
+ ssl->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
737
+ }
672
738
  }
673
739
 
674
- /* If the configured session has expired or was created at a version higher
675
- * than our maximum version, drop it. */
676
- if (ssl->session != NULL &&
677
- (ssl->session->session_id_length == 0 || ssl->session->not_resumable ||
678
- ssl->session->timeout < (long)(time(NULL) - ssl->session->time) ||
679
- (!SSL_IS_DTLS(ssl) && ssl->session->ssl_version > ssl->version) ||
680
- (SSL_IS_DTLS(ssl) && ssl->session->ssl_version < ssl->version))) {
681
- SSL_set_session(ssl, NULL);
740
+ /* If the configured session has expired or was created at a disabled
741
+ * version, drop it. */
742
+ if (ssl->session != NULL) {
743
+ uint16_t session_version;
744
+ if (!ssl->method->version_from_wire(&session_version,
745
+ ssl->session->ssl_version) ||
746
+ (session_version < TLS1_3_VERSION &&
747
+ ssl->session->session_id_length == 0) ||
748
+ ssl->session->not_resumable ||
749
+ !ssl_session_is_time_valid(ssl, ssl->session) ||
750
+ session_version < min_version || session_version > max_version) {
751
+ ssl_set_session(ssl, NULL);
752
+ }
682
753
  }
683
754
 
684
755
  /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
685
756
  * renegerate the client_random. The random must be reused. */
686
- if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) &&
687
- !ssl_fill_hello_random(ssl->s3->client_random,
688
- sizeof(ssl->s3->client_random), 0 /* client */)) {
757
+ if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
758
+ !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
689
759
  goto err;
690
760
  }
691
761
 
692
- /* Renegotiations do not participate in session resumption. */
693
- int has_session = ssl->session != NULL &&
694
- !ssl->s3->initial_handshake_complete;
695
-
696
- CBB child;
697
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
698
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
699
- !CBB_add_u16(&cbb, ssl->client_version) ||
700
- !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
701
- !CBB_add_u8_length_prefixed(&cbb, &child) ||
702
- (has_session &&
703
- !CBB_add_bytes(&child, ssl->session->session_id,
704
- ssl->session->session_id_length))) {
762
+ CBB body;
763
+ if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) ||
764
+ !ssl_add_client_hello_body(ssl, &body) ||
765
+ !ssl->method->finish_message(ssl, &cbb)) {
705
766
  goto err;
706
767
  }
707
768
 
708
- if (SSL_IS_DTLS(ssl)) {
709
- if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
710
- !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
711
- goto err;
712
- }
769
+ ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
770
+ return ssl->method->write_message(ssl);
771
+
772
+ err:
773
+ CBB_cleanup(&cbb);
774
+ return -1;
775
+ }
776
+
777
+ static int dtls1_get_hello_verify(SSL *ssl) {
778
+ int al;
779
+ CBS hello_verify_request, cookie;
780
+ uint16_t server_version;
781
+
782
+ int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
783
+ if (ret <= 0) {
784
+ return ret;
713
785
  }
714
786
 
715
- size_t length;
716
- if (!ssl3_write_client_cipher_list(ssl, &cbb) ||
717
- !CBB_add_u8(&cbb, 1 /* one compression method */) ||
718
- !CBB_add_u8(&cbb, 0 /* null compression */) ||
719
- !ssl_add_clienthello_tlsext(ssl, &cbb,
720
- CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) ||
721
- !CBB_finish(&cbb, NULL, &length) ||
722
- !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) {
723
- goto err;
787
+ if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
788
+ ssl->d1->send_cookie = 0;
789
+ ssl->s3->tmp.reuse_message = 1;
790
+ return 1;
724
791
  }
725
792
 
726
- ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
727
- return ssl_do_write(ssl);
793
+ CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
728
794
 
729
- err:
730
- CBB_cleanup(&cbb);
795
+ if (!CBS_get_u16(&hello_verify_request, &server_version) ||
796
+ !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
797
+ CBS_len(&hello_verify_request) != 0) {
798
+ al = SSL_AD_DECODE_ERROR;
799
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
800
+ goto f_err;
801
+ }
802
+
803
+ if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
804
+ al = SSL_AD_ILLEGAL_PARAMETER;
805
+ goto f_err;
806
+ }
807
+
808
+ memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
809
+ ssl->d1->cookie_len = CBS_len(&cookie);
810
+
811
+ ssl->d1->send_cookie = 1;
812
+ return 1;
813
+
814
+ f_err:
815
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
731
816
  return -1;
732
817
  }
733
818
 
734
- int ssl3_get_server_hello(SSL *ssl) {
819
+ static int ssl3_get_server_hello(SSL *ssl) {
735
820
  STACK_OF(SSL_CIPHER) *sk;
736
821
  const SSL_CIPHER *c;
737
822
  CERT *ct = ssl->cert;
738
- int al = SSL_AD_INTERNAL_ERROR, ok;
739
- long n;
823
+ int al = SSL_AD_INTERNAL_ERROR;
740
824
  CBS server_hello, server_random, session_id;
741
- uint16_t server_version, cipher_suite;
825
+ uint16_t server_wire_version, cipher_suite;
742
826
  uint8_t compression_method;
743
827
 
744
- n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_HELLO_A,
745
- SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
746
- 20000, /* ?? */
747
- ssl_hash_message, &ok);
748
-
749
- if (!ok) {
828
+ int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
829
+ if (ret <= 0) {
750
830
  uint32_t err = ERR_peek_error();
751
831
  if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
752
832
  ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
@@ -758,71 +838,91 @@ int ssl3_get_server_hello(SSL *ssl) {
758
838
  * See https://crbug.com/446505. */
759
839
  OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
760
840
  }
761
- return n;
841
+ return ret;
842
+ }
843
+
844
+ if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
845
+ ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
846
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
847
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
848
+ return -1;
762
849
  }
763
850
 
764
- CBS_init(&server_hello, ssl->init_msg, n);
851
+ CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
765
852
 
766
- if (!CBS_get_u16(&server_hello, &server_version) ||
767
- !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
768
- !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
769
- CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
770
- !CBS_get_u16(&server_hello, &cipher_suite) ||
771
- !CBS_get_u8(&server_hello, &compression_method)) {
853
+ if (!CBS_get_u16(&server_hello, &server_wire_version)) {
772
854
  al = SSL_AD_DECODE_ERROR;
773
855
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
774
856
  goto f_err;
775
857
  }
776
858
 
859
+ uint16_t min_version, max_version, server_version;
860
+ if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
861
+ !ssl->method->version_from_wire(&server_version, server_wire_version) ||
862
+ server_version < min_version || server_version > max_version) {
863
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
864
+ al = SSL_AD_PROTOCOL_VERSION;
865
+ goto f_err;
866
+ }
867
+
777
868
  assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
778
869
  if (!ssl->s3->have_version) {
779
- if (!ssl3_is_version_enabled(ssl, server_version)) {
780
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
781
- ssl->version = server_version;
782
- /* Mark the version as fixed so the record-layer version is not clamped
783
- * to TLS 1.0. */
784
- ssl->s3->have_version = 1;
785
- al = SSL_AD_PROTOCOL_VERSION;
786
- goto f_err;
787
- }
788
- ssl->version = server_version;
870
+ ssl->version = server_wire_version;
789
871
  ssl->s3->enc_method = ssl3_get_enc_method(server_version);
790
872
  assert(ssl->s3->enc_method != NULL);
791
873
  /* At this point, the connection's version is known and ssl->version is
792
874
  * fixed. Begin enforcing the record-layer version. */
793
875
  ssl->s3->have_version = 1;
794
- } else if (server_version != ssl->version) {
876
+ } else if (server_wire_version != ssl->version) {
795
877
  OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
796
878
  al = SSL_AD_PROTOCOL_VERSION;
797
879
  goto f_err;
798
880
  }
799
881
 
800
- /* Copy over the server random. */
801
- memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
882
+ if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
883
+ ssl->state = SSL_ST_TLS13;
884
+ return 1;
885
+ }
802
886
 
803
- assert(ssl->session == NULL || ssl->session->session_id_length > 0);
804
- if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
805
- CBS_mem_equal(&session_id, ssl->session->session_id,
806
- ssl->session->session_id_length)) {
807
- if (ssl->sid_ctx_length != ssl->session->sid_ctx_length ||
808
- memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) {
809
- /* actually a client application bug */
810
- al = SSL_AD_ILLEGAL_PARAMETER;
811
- OPENSSL_PUT_ERROR(SSL,
812
- SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
813
- goto f_err;
814
- }
815
- ssl->hit = 1;
887
+ ssl_clear_tls13_state(ssl);
888
+
889
+ if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
890
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
891
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
892
+ return -1;
893
+ }
894
+
895
+ if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
896
+ !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
897
+ CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
898
+ !CBS_get_u16(&server_hello, &cipher_suite) ||
899
+ !CBS_get_u8(&server_hello, &compression_method)) {
900
+ al = SSL_AD_DECODE_ERROR;
901
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
902
+ goto f_err;
903
+ }
904
+
905
+ /* Copy over the server random. */
906
+ memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
907
+
908
+ /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
909
+ * 1.3 is finalized and we are not implementing a draft version. */
910
+
911
+ if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
912
+ ssl->session->session_id_length != 0 &&
913
+ CBS_mem_equal(&session_id, ssl->session->session_id,
914
+ ssl->session->session_id_length)) {
915
+ ssl->s3->session_reused = 1;
816
916
  } else {
817
917
  /* The session wasn't resumed. Create a fresh SSL_SESSION to
818
918
  * fill out. */
819
- ssl->hit = 0;
919
+ ssl_set_session(ssl, NULL);
820
920
  if (!ssl_get_new_session(ssl, 0 /* client */)) {
821
921
  goto f_err;
822
922
  }
823
923
  /* Note: session_id could be empty. */
824
- ssl->session->session_id_length = CBS_len(&session_id);
825
- memcpy(ssl->session->session_id, CBS_data(&session_id),
924
+ ssl->s3->new_session->session_id_length = CBS_len(&session_id);
925
+ memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
826
926
  CBS_len(&session_id));
827
927
  }
828
928
 
@@ -836,7 +936,8 @@ int ssl3_get_server_hello(SSL *ssl) {
836
936
  /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
837
937
  * the server selected it, it's an error. */
838
938
  if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
839
- SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl)) {
939
+ SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
940
+ SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
840
941
  al = SSL_AD_ILLEGAL_PARAMETER;
841
942
  OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
842
943
  goto f_err;
@@ -850,7 +951,7 @@ int ssl3_get_server_hello(SSL *ssl) {
850
951
  goto f_err;
851
952
  }
852
953
 
853
- if (ssl->hit) {
954
+ if (ssl->session != NULL) {
854
955
  if (ssl->session->cipher != c) {
855
956
  al = SSL_AD_ILLEGAL_PARAMETER;
856
957
  OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
@@ -861,8 +962,15 @@ int ssl3_get_server_hello(SSL *ssl) {
861
962
  OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
862
963
  goto f_err;
863
964
  }
965
+ if (!ssl_session_is_context_valid(ssl, ssl->session)) {
966
+ /* This is actually a client application bug. */
967
+ al = SSL_AD_ILLEGAL_PARAMETER;
968
+ OPENSSL_PUT_ERROR(SSL,
969
+ SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
970
+ goto f_err;
971
+ }
864
972
  } else {
865
- ssl->session->cipher = c;
973
+ ssl->s3->new_session->cipher = c;
866
974
  }
867
975
  ssl->s3->tmp.new_cipher = c;
868
976
 
@@ -871,10 +979,11 @@ int ssl3_get_server_hello(SSL *ssl) {
871
979
  goto f_err;
872
980
  }
873
981
 
874
- /* If doing a full handshake with TLS 1.2, the server may request a client
875
- * certificate which requires hashing the handshake transcript under a
876
- * different hash. Otherwise, the handshake buffer may be released. */
877
- if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
982
+ /* If doing a full handshake, the server may request a client certificate
983
+ * which requires hashing the handshake transcript. Otherwise, the handshake
984
+ * buffer may be released. */
985
+ if (ssl->session != NULL ||
986
+ !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
878
987
  ssl3_free_handshake_buffer(ssl);
879
988
  }
880
989
 
@@ -895,11 +1004,11 @@ int ssl3_get_server_hello(SSL *ssl) {
895
1004
  if (CBS_len(&server_hello) != 0) {
896
1005
  /* wrong packet length */
897
1006
  al = SSL_AD_DECODE_ERROR;
898
- OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
1007
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
899
1008
  goto f_err;
900
1009
  }
901
1010
 
902
- if (ssl->hit &&
1011
+ if (ssl->session != NULL &&
903
1012
  ssl->s3->tmp.extended_master_secret !=
904
1013
  ssl->session->extended_master_secret) {
905
1014
  al = SSL_AD_HANDSHAKE_FAILURE;
@@ -919,184 +1028,131 @@ err:
919
1028
  return -1;
920
1029
  }
921
1030
 
922
- /* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server
923
- * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the
924
- * error queue. */
925
- static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) {
926
- int ret = 0;
927
- EVP_PKEY *pkey = X509_get_pubkey(leaf);
928
- if (pkey == NULL) {
929
- goto err;
1031
+ static int ssl3_get_server_certificate(SSL *ssl) {
1032
+ int ret =
1033
+ ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
1034
+ if (ret <= 0) {
1035
+ return ret;
930
1036
  }
931
1037
 
932
- /* Check the certificate's type matches the cipher. */
933
- const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
934
- int expected_type = ssl_cipher_get_key_type(cipher);
935
- assert(expected_type != EVP_PKEY_NONE);
936
- if (pkey->type != expected_type) {
937
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
1038
+ CBS cbs;
1039
+ CBS_init(&cbs, ssl->init_msg, ssl->init_num);
1040
+ uint8_t alert;
1041
+ STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs);
1042
+ if (chain == NULL) {
1043
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
938
1044
  goto err;
939
1045
  }
940
1046
 
941
- if (cipher->algorithm_auth & SSL_aECDSA) {
942
- /* TODO(davidben): This behavior is preserved from upstream. Should key
943
- * usages be checked in other cases as well? */
944
- /* This call populates the ex_flags field correctly */
945
- X509_check_purpose(leaf, -1, 0);
946
- if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
947
- !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
948
- OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
949
- goto err;
950
- }
1047
+ if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) {
1048
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1049
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1050
+ goto err;
1051
+ }
951
1052
 
952
- if (!tls1_check_ec_cert(ssl, leaf)) {
953
- OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
954
- goto err;
955
- }
1053
+ X509 *leaf = sk_X509_value(chain, 0);
1054
+ if (!ssl_check_leaf_certificate(ssl, leaf)) {
1055
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1056
+ goto err;
956
1057
  }
957
1058
 
958
- ret = 1;
1059
+ /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1060
+ * the leaf. */
1061
+ sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
1062
+ ssl->s3->new_session->cert_chain = chain;
1063
+
1064
+ X509_free(ssl->s3->new_session->peer);
1065
+ X509_up_ref(leaf);
1066
+ ssl->s3->new_session->peer = leaf;
1067
+
1068
+ return 1;
959
1069
 
960
1070
  err:
961
- EVP_PKEY_free(pkey);
962
- return ret;
1071
+ sk_X509_pop_free(chain, X509_free);
1072
+ return -1;
963
1073
  }
964
1074
 
965
- int ssl3_get_server_certificate(SSL *ssl) {
966
- int al, ok, ret = -1;
967
- unsigned long n;
968
- X509 *x = NULL;
969
- STACK_OF(X509) *sk = NULL;
970
- EVP_PKEY *pkey = NULL;
971
- CBS cbs, certificate_list;
972
- const uint8_t *data;
973
-
974
- n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
975
- SSL3_MT_CERTIFICATE, (long)ssl->max_cert_list,
976
- ssl_hash_message, &ok);
1075
+ static int ssl3_get_cert_status(SSL *ssl) {
1076
+ int al;
1077
+ CBS certificate_status, ocsp_response;
1078
+ uint8_t status_type;
977
1079
 
978
- if (!ok) {
979
- return n;
1080
+ int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1081
+ if (ret <= 0) {
1082
+ return ret;
980
1083
  }
981
1084
 
982
- CBS_init(&cbs, ssl->init_msg, n);
983
-
984
- sk = sk_X509_new_null();
985
- if (sk == NULL) {
986
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
987
- goto err;
1085
+ if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1086
+ /* A server may send status_request in ServerHello and then change
1087
+ * its mind about sending CertificateStatus. */
1088
+ ssl->s3->tmp.reuse_message = 1;
1089
+ return 1;
988
1090
  }
989
1091
 
990
- if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
991
- CBS_len(&certificate_list) == 0 ||
992
- CBS_len(&cbs) != 0) {
1092
+ CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
1093
+ if (!CBS_get_u8(&certificate_status, &status_type) ||
1094
+ status_type != TLSEXT_STATUSTYPE_ocsp ||
1095
+ !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1096
+ CBS_len(&ocsp_response) == 0 ||
1097
+ CBS_len(&certificate_status) != 0) {
993
1098
  al = SSL_AD_DECODE_ERROR;
994
1099
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
995
1100
  goto f_err;
996
1101
  }
997
1102
 
998
- while (CBS_len(&certificate_list) > 0) {
999
- CBS certificate;
1000
- if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1001
- al = SSL_AD_DECODE_ERROR;
1002
- OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1003
- goto f_err;
1004
- }
1005
- /* A u24 length cannot overflow a long. */
1006
- data = CBS_data(&certificate);
1007
- x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
1008
- if (x == NULL) {
1009
- al = SSL_AD_BAD_CERTIFICATE;
1010
- OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1011
- goto f_err;
1012
- }
1013
- if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1014
- al = SSL_AD_DECODE_ERROR;
1015
- OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1016
- goto f_err;
1017
- }
1018
- if (!sk_X509_push(sk, x)) {
1019
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1020
- goto err;
1021
- }
1022
- x = NULL;
1023
- }
1024
-
1025
- X509 *leaf = sk_X509_value(sk, 0);
1026
- if (!ssl3_check_leaf_certificate(ssl, leaf)) {
1027
- al = SSL_AD_ILLEGAL_PARAMETER;
1103
+ if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1104
+ &ssl->s3->new_session->ocsp_response_length)) {
1105
+ al = SSL_AD_INTERNAL_ERROR;
1106
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1028
1107
  goto f_err;
1029
1108
  }
1109
+ return 1;
1030
1110
 
1031
- /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1032
- * the leaf. */
1033
- sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1034
- ssl->session->cert_chain = sk;
1035
- sk = NULL;
1036
-
1037
- X509_free(ssl->session->peer);
1038
- ssl->session->peer = X509_up_ref(leaf);
1039
-
1040
- ssl->session->verify_result = ssl->verify_result;
1041
-
1042
- ret = 1;
1111
+ f_err:
1112
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1113
+ return -1;
1114
+ }
1043
1115
 
1044
- if (0) {
1045
- f_err:
1046
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1116
+ static int ssl3_verify_server_cert(SSL *ssl) {
1117
+ if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1118
+ ssl->s3->new_session->cert_chain)) {
1119
+ return -1;
1047
1120
  }
1048
1121
 
1049
- err:
1050
- EVP_PKEY_free(pkey);
1051
- X509_free(x);
1052
- sk_X509_pop_free(sk, X509_free);
1053
- return ret;
1122
+ return 1;
1054
1123
  }
1055
1124
 
1056
- int ssl3_get_server_key_exchange(SSL *ssl) {
1057
- EVP_MD_CTX md_ctx;
1058
- int al, ok;
1125
+ static int ssl3_get_server_key_exchange(SSL *ssl) {
1126
+ int al;
1059
1127
  EVP_PKEY *pkey = NULL;
1060
1128
  DH *dh = NULL;
1061
1129
  EC_KEY *ecdh = NULL;
1062
1130
  EC_POINT *srvr_ecpoint = NULL;
1063
1131
 
1064
- /* use same message size as in ssl3_get_certificate_request() as
1065
- * ServerKeyExchange message may be skipped */
1066
- long n = ssl->method->ssl_get_message(
1067
- ssl, SSL3_ST_CR_KEY_EXCH_A, SSL3_ST_CR_KEY_EXCH_B, -1, ssl->max_cert_list,
1068
- ssl_hash_message, &ok);
1069
- if (!ok) {
1070
- return n;
1132
+ int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1133
+ if (ret <= 0) {
1134
+ return ret;
1071
1135
  }
1072
1136
 
1073
1137
  if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1138
+ /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
1074
1139
  if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
1075
1140
  OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1076
1141
  ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1077
1142
  return -1;
1078
1143
  }
1079
1144
 
1080
- /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no
1081
- * identity hint. */
1082
- if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
1083
- /* TODO(davidben): This should be reset in one place with the rest of the
1084
- * handshake state. */
1085
- OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
1086
- ssl->s3->tmp.peer_psk_identity_hint = NULL;
1087
- }
1088
1145
  ssl->s3->tmp.reuse_message = 1;
1089
1146
  return 1;
1090
1147
  }
1091
1148
 
1092
1149
  /* Retain a copy of the original CBS to compute the signature over. */
1093
1150
  CBS server_key_exchange;
1094
- CBS_init(&server_key_exchange, ssl->init_msg, n);
1151
+ CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
1095
1152
  CBS server_key_exchange_orig = server_key_exchange;
1096
1153
 
1097
1154
  uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1098
1155
  uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1099
- EVP_MD_CTX_init(&md_ctx);
1100
1156
 
1101
1157
  if (alg_a & SSL_aPSK) {
1102
1158
  CBS psk_identity_hint;
@@ -1123,8 +1179,13 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1123
1179
  goto f_err;
1124
1180
  }
1125
1181
 
1126
- /* Save the identity hint as a C string. */
1127
- if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
1182
+ /* Save non-empty identity hints as a C string. Empty identity hints we
1183
+ * treat as missing. Plain PSK makes it possible to send either no hint
1184
+ * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1185
+ * empty hint. Having different capabilities is odd, so we interpret empty
1186
+ * and missing as identical. */
1187
+ if (CBS_len(&psk_identity_hint) != 0 &&
1188
+ !CBS_strdup(&psk_identity_hint, &ssl->s3->hs->peer_psk_identity_hint)) {
1128
1189
  al = SSL_AD_INTERNAL_ERROR;
1129
1190
  OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1130
1191
  goto f_err;
@@ -1155,11 +1216,11 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1155
1216
  goto err;
1156
1217
  }
1157
1218
 
1158
- ssl->session->key_exchange_info = DH_num_bits(dh);
1159
- if (ssl->session->key_exchange_info < 1024) {
1219
+ ssl->s3->new_session->key_exchange_info = DH_num_bits(dh);
1220
+ if (ssl->s3->new_session->key_exchange_info < 1024) {
1160
1221
  OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
1161
1222
  goto err;
1162
- } else if (ssl->session->key_exchange_info > 4096) {
1223
+ } else if (ssl->s3->new_session->key_exchange_info > 4096) {
1163
1224
  /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1164
1225
  * to prevent a server from causing us to perform too expensive of a
1165
1226
  * computation. */
@@ -1175,26 +1236,27 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1175
1236
  if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
1176
1237
  goto err;
1177
1238
  }
1178
- /* |dh_Ys| has a u16 length prefix, so this fits in a |uint16_t|. */
1239
+ /* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
1240
+ * fits in a uint16_t. */
1179
1241
  assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1180
1242
  ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1181
1243
  } else if (alg_k & SSL_kECDHE) {
1182
1244
  /* Parse the server parameters. */
1183
- uint8_t curve_type;
1184
- uint16_t curve_id;
1245
+ uint8_t group_type;
1246
+ uint16_t group_id;
1185
1247
  CBS point;
1186
- if (!CBS_get_u8(&server_key_exchange, &curve_type) ||
1187
- curve_type != NAMED_CURVE_TYPE ||
1188
- !CBS_get_u16(&server_key_exchange, &curve_id) ||
1248
+ if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1249
+ group_type != NAMED_CURVE_TYPE ||
1250
+ !CBS_get_u16(&server_key_exchange, &group_id) ||
1189
1251
  !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1190
1252
  al = SSL_AD_DECODE_ERROR;
1191
1253
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1192
1254
  goto f_err;
1193
1255
  }
1194
- ssl->session->key_exchange_info = curve_id;
1256
+ ssl->s3->new_session->key_exchange_info = group_id;
1195
1257
 
1196
- /* Ensure the curve is consistent with preferences. */
1197
- if (!tls1_check_curve_id(ssl, curve_id)) {
1258
+ /* Ensure the group is consistent with preferences. */
1259
+ if (!tls1_check_group_id(ssl, group_id)) {
1198
1260
  al = SSL_AD_ILLEGAL_PARAMETER;
1199
1261
  OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
1200
1262
  goto f_err;
@@ -1202,11 +1264,29 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1202
1264
 
1203
1265
  /* Initialize ECDH and save the peer public key for later. */
1204
1266
  size_t peer_key_len;
1205
- if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) ||
1267
+ if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
1206
1268
  !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
1207
1269
  goto err;
1208
1270
  }
1209
- /* |point| has a u8 length prefix, so this fits in a |uint16_t|. */
1271
+ /* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len
1272
+ * fits in a uint16_t. */
1273
+ assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1274
+ ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1275
+ } else if (alg_k & SSL_kCECPQ1) {
1276
+ SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
1277
+ CBS key;
1278
+ if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
1279
+ al = SSL_AD_DECODE_ERROR;
1280
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1281
+ goto f_err;
1282
+ }
1283
+
1284
+ size_t peer_key_len;
1285
+ if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) {
1286
+ goto err;
1287
+ }
1288
+ /* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
1289
+ * fits in a uint16_t. */
1210
1290
  assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1211
1291
  ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1212
1292
  } else if (!(alg_k & SSL_kPSK)) {
@@ -1223,29 +1303,31 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1223
1303
  CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1224
1304
 
1225
1305
  /* ServerKeyExchange should be signed by the server's public key. */
1226
- if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
1227
- pkey = X509_get_pubkey(ssl->session->peer);
1306
+ if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
1307
+ pkey = X509_get_pubkey(ssl->s3->new_session->peer);
1228
1308
  if (pkey == NULL) {
1229
1309
  goto err;
1230
1310
  }
1231
1311
 
1232
- const EVP_MD *md = NULL;
1312
+ uint16_t signature_algorithm = 0;
1233
1313
  if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1234
- uint8_t hash, signature;
1235
- if (!CBS_get_u8(&server_key_exchange, &hash) ||
1236
- !CBS_get_u8(&server_key_exchange, &signature)) {
1314
+ if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
1237
1315
  al = SSL_AD_DECODE_ERROR;
1238
1316
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1239
1317
  goto f_err;
1240
1318
  }
1241
- if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) {
1319
+ if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
1242
1320
  goto f_err;
1243
1321
  }
1244
- ssl->s3->tmp.server_key_exchange_hash = hash;
1322
+ ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
1245
1323
  } else if (pkey->type == EVP_PKEY_RSA) {
1246
- md = EVP_md5_sha1();
1324
+ signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1325
+ } else if (pkey->type == EVP_PKEY_EC) {
1326
+ signature_algorithm = SSL_SIGN_ECDSA_SHA1;
1247
1327
  } else {
1248
- md = EVP_sha1();
1328
+ al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1329
+ OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1330
+ goto f_err;
1249
1331
  }
1250
1332
 
1251
1333
  /* The last field in |server_key_exchange| is the signature. */
@@ -1257,15 +1339,30 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1257
1339
  goto f_err;
1258
1340
  }
1259
1341
 
1260
- if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
1261
- !EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random,
1262
- SSL3_RANDOM_SIZE) ||
1263
- !EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random,
1264
- SSL3_RANDOM_SIZE) ||
1265
- !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
1266
- CBS_len(&parameter)) ||
1267
- !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
1268
- CBS_len(&signature))) {
1342
+ CBB transcript;
1343
+ uint8_t *transcript_data;
1344
+ size_t transcript_len;
1345
+ if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1346
+ !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1347
+ !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1348
+ !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1349
+ !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1350
+ CBB_cleanup(&transcript);
1351
+ al = SSL_AD_INTERNAL_ERROR;
1352
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1353
+ goto f_err;
1354
+ }
1355
+
1356
+ int sig_ok = ssl_public_key_verify(
1357
+ ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1358
+ pkey, transcript_data, transcript_len);
1359
+ OPENSSL_free(transcript_data);
1360
+
1361
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1362
+ sig_ok = 1;
1363
+ ERR_clear_error();
1364
+ #endif
1365
+ if (!sig_ok) {
1269
1366
  /* bad signature */
1270
1367
  al = SSL_AD_DECRYPT_ERROR;
1271
1368
  OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
@@ -1282,7 +1379,6 @@ int ssl3_get_server_key_exchange(SSL *ssl) {
1282
1379
  }
1283
1380
  }
1284
1381
  EVP_PKEY_free(pkey);
1285
- EVP_MD_CTX_cleanup(&md_ctx);
1286
1382
  return 1;
1287
1383
 
1288
1384
  f_err:
@@ -1292,35 +1388,18 @@ err:
1292
1388
  DH_free(dh);
1293
1389
  EC_POINT_free(srvr_ecpoint);
1294
1390
  EC_KEY_free(ecdh);
1295
- EVP_MD_CTX_cleanup(&md_ctx);
1296
1391
  return -1;
1297
1392
  }
1298
1393
 
1299
- static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
1300
- return X509_NAME_cmp(*a, *b);
1301
- }
1302
-
1303
- int ssl3_get_certificate_request(SSL *ssl) {
1304
- int ok, ret = 0;
1305
- unsigned long n;
1306
- X509_NAME *xn = NULL;
1307
- STACK_OF(X509_NAME) *ca_sk = NULL;
1308
- CBS cbs;
1309
- CBS certificate_types;
1310
- CBS certificate_authorities;
1311
- const uint8_t *data;
1312
-
1313
- n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_REQ_A,
1314
- SSL3_ST_CR_CERT_REQ_B, -1, ssl->max_cert_list,
1315
- ssl_hash_message, &ok);
1316
-
1317
- if (!ok) {
1318
- return n;
1394
+ static int ssl3_get_certificate_request(SSL *ssl) {
1395
+ int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1396
+ if (msg_ret <= 0) {
1397
+ return msg_ret;
1319
1398
  }
1320
1399
 
1321
- ssl->s3->tmp.cert_req = 0;
1400
+ ssl->s3->tmp.cert_request = 0;
1322
1401
 
1323
- if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1402
+ if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
1324
1403
  ssl->s3->tmp.reuse_message = 1;
1325
1404
  /* If we get here we don't need the handshake buffer as we won't be doing
1326
1405
  * client auth. */
@@ -1330,29 +1409,25 @@ int ssl3_get_certificate_request(SSL *ssl) {
1330
1409
 
1331
1410
  if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1332
1411
  ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1333
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
1334
- goto err;
1412
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1413
+ return -1;
1335
1414
  }
1336
1415
 
1337
- CBS_init(&cbs, ssl->init_msg, n);
1338
-
1339
- ca_sk = sk_X509_NAME_new(ca_dn_cmp);
1340
- if (ca_sk == NULL) {
1341
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1342
- goto err;
1343
- }
1416
+ CBS cbs;
1417
+ CBS_init(&cbs, ssl->init_msg, ssl->init_num);
1344
1418
 
1345
- /* get the certificate types */
1419
+ /* Get the certificate types. */
1420
+ CBS certificate_types;
1346
1421
  if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
1347
1422
  ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1348
1423
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1349
- goto err;
1424
+ return -1;
1350
1425
  }
1351
1426
 
1352
1427
  if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
1353
1428
  &ssl->s3->tmp.num_certificate_types)) {
1354
1429
  ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1355
- goto err;
1430
+ return -1;
1356
1431
  }
1357
1432
 
1358
1433
  if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
@@ -1361,223 +1436,111 @@ int ssl3_get_certificate_request(SSL *ssl) {
1361
1436
  !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1362
1437
  ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1363
1438
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1364
- goto err;
1439
+ return -1;
1365
1440
  }
1366
1441
  }
1367
1442
 
1368
- /* get the CA RDNs */
1369
- if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
1370
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1371
- OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
1372
- goto err;
1443
+ uint8_t alert;
1444
+ STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
1445
+ if (ca_sk == NULL) {
1446
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1447
+ return -1;
1373
1448
  }
1374
1449
 
1375
- while (CBS_len(&certificate_authorities) > 0) {
1376
- CBS distinguished_name;
1377
- if (!CBS_get_u16_length_prefixed(&certificate_authorities,
1378
- &distinguished_name)) {
1379
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1380
- OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
1381
- goto err;
1382
- }
1383
-
1384
- data = CBS_data(&distinguished_name);
1385
-
1386
- /* A u16 length cannot overflow a long. */
1387
- xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name));
1388
- if (xn == NULL) {
1389
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1390
- OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1391
- goto err;
1392
- }
1393
-
1394
- if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
1395
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1396
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1397
- goto err;
1398
- }
1399
-
1400
- if (CBS_len(&distinguished_name) != 0) {
1401
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1402
- OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_LENGTH_MISMATCH);
1403
- goto err;
1404
- }
1405
-
1406
- if (!sk_X509_NAME_push(ca_sk, xn)) {
1407
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1408
- goto err;
1409
- }
1450
+ if (CBS_len(&cbs) != 0) {
1451
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1452
+ sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1453
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1454
+ return -1;
1410
1455
  }
1411
1456
 
1412
- /* we should setup a certificate to return.... */
1413
- ssl->s3->tmp.cert_req = 1;
1457
+ ssl->s3->tmp.cert_request = 1;
1414
1458
  sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
1415
1459
  ssl->s3->tmp.ca_names = ca_sk;
1416
- ca_sk = NULL;
1417
-
1418
- ret = 1;
1419
-
1420
- err:
1421
- sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1422
- return ret;
1460
+ return 1;
1423
1461
  }
1424
1462
 
1425
- int ssl3_get_new_session_ticket(SSL *ssl) {
1426
- int ok, al;
1427
- long n = ssl->method->ssl_get_message(
1428
- ssl, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
1429
- SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
1430
-
1431
- if (!ok) {
1432
- return n;
1463
+ static int ssl3_get_server_hello_done(SSL *ssl) {
1464
+ int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
1465
+ ssl_hash_message);
1466
+ if (ret <= 0) {
1467
+ return ret;
1433
1468
  }
1434
1469
 
1435
- CBS new_session_ticket, ticket;
1436
- uint32_t ticket_lifetime_hint;
1437
- CBS_init(&new_session_ticket, ssl->init_msg, n);
1438
- if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
1439
- !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1440
- CBS_len(&new_session_ticket) != 0) {
1441
- al = SSL_AD_DECODE_ERROR;
1470
+ /* ServerHelloDone is empty. */
1471
+ if (ssl->init_num > 0) {
1472
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1442
1473
  OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1443
- goto f_err;
1444
- }
1445
-
1446
- if (CBS_len(&ticket) == 0) {
1447
- /* RFC 5077 allows a server to change its mind and send no ticket after
1448
- * negotiating the extension. The value of |tlsext_ticket_expected| is
1449
- * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
1450
- * update. */
1451
- ssl->tlsext_ticket_expected = 0;
1452
- return 1;
1453
- }
1454
-
1455
- if (ssl->hit) {
1456
- /* The server is sending a new ticket for an existing session. Sessions are
1457
- * immutable once established, so duplicate all but the ticket of the
1458
- * existing session. */
1459
- uint8_t *bytes;
1460
- size_t bytes_len;
1461
- if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) {
1462
- goto err;
1463
- }
1464
- SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
1465
- OPENSSL_free(bytes);
1466
- if (new_session == NULL) {
1467
- /* This should never happen. */
1468
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1469
- goto err;
1470
- }
1471
-
1472
- SSL_SESSION_free(ssl->session);
1473
- ssl->session = new_session;
1474
- }
1475
-
1476
- if (!CBS_stow(&ticket, &ssl->session->tlsext_tick,
1477
- &ssl->session->tlsext_ticklen)) {
1478
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1479
- goto err;
1480
- }
1481
- ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
1482
-
1483
- /* Generate a session ID for this session based on the session ticket. We use
1484
- * the session ID mechanism for detecting ticket resumption. This also fits in
1485
- * with assumptions elsewhere in OpenSSL.*/
1486
- if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id,
1487
- &ssl->session->session_id_length, EVP_sha256(), NULL)) {
1488
- goto err;
1474
+ return -1;
1489
1475
  }
1490
1476
 
1491
1477
  return 1;
1492
-
1493
- f_err:
1494
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1495
- err:
1496
- return -1;
1497
1478
  }
1498
1479
 
1499
- int ssl3_get_cert_status(SSL *ssl) {
1500
- int ok, al;
1501
- long n;
1502
- CBS certificate_status, ocsp_response;
1503
- uint8_t status_type;
1504
-
1505
- n = ssl->method->ssl_get_message(
1506
- ssl, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
1507
- -1, 16384, ssl_hash_message, &ok);
1508
-
1509
- if (!ok) {
1510
- return n;
1511
- }
1512
-
1513
- if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1514
- /* A server may send status_request in ServerHello and then change
1515
- * its mind about sending CertificateStatus. */
1516
- ssl->s3->tmp.reuse_message = 1;
1517
- return 1;
1518
- }
1519
-
1520
- CBS_init(&certificate_status, ssl->init_msg, n);
1521
- if (!CBS_get_u8(&certificate_status, &status_type) ||
1522
- status_type != TLSEXT_STATUSTYPE_ocsp ||
1523
- !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1524
- CBS_len(&ocsp_response) == 0 ||
1525
- CBS_len(&certificate_status) != 0) {
1526
- al = SSL_AD_DECODE_ERROR;
1527
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1528
- goto f_err;
1529
- }
1480
+ static int ssl3_send_client_certificate(SSL *ssl) {
1481
+ if (ssl->state == SSL3_ST_CW_CERT_A) {
1482
+ /* Call cert_cb to update the certificate. */
1483
+ if (ssl->cert->cert_cb) {
1484
+ int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1485
+ if (ret < 0) {
1486
+ ssl->rwstate = SSL_X509_LOOKUP;
1487
+ return -1;
1488
+ }
1489
+ if (ret == 0) {
1490
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1491
+ return -1;
1492
+ }
1493
+ }
1530
1494
 
1531
- if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
1532
- &ssl->session->ocsp_response_length)) {
1533
- al = SSL_AD_INTERNAL_ERROR;
1534
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1535
- goto f_err;
1495
+ ssl->state = SSL3_ST_CW_CERT_B;
1536
1496
  }
1537
- return 1;
1538
-
1539
- f_err:
1540
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1541
- return -1;
1542
- }
1543
1497
 
1544
- int ssl3_get_server_done(SSL *ssl) {
1545
- int ok;
1546
- long n;
1498
+ if (ssl->state == SSL3_ST_CW_CERT_B) {
1499
+ /* Call client_cert_cb to update the certificate. */
1500
+ int should_retry;
1501
+ if (!ssl_do_client_cert_cb(ssl, &should_retry)) {
1502
+ if (should_retry) {
1503
+ ssl->rwstate = SSL_X509_LOOKUP;
1504
+ }
1505
+ return -1;
1506
+ }
1547
1507
 
1548
- n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_DONE_A,
1549
- SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1550
- 30, /* should be very small, like 0 :-) */
1551
- ssl_hash_message, &ok);
1508
+ if (!ssl_has_certificate(ssl)) {
1509
+ ssl->s3->tmp.cert_request = 0;
1510
+ /* Without a client certificate, the handshake buffer may be released. */
1511
+ ssl3_free_handshake_buffer(ssl);
1552
1512
 
1553
- if (!ok) {
1554
- return n;
1555
- }
1513
+ if (ssl->version == SSL3_VERSION) {
1514
+ /* In SSL 3.0, send no certificate by skipping both messages. */
1515
+ ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1516
+ return 1;
1517
+ }
1518
+ }
1556
1519
 
1557
- if (n > 0) {
1558
- /* should contain no data */
1559
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1560
- OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
1561
- return -1;
1520
+ if (!ssl3_output_cert_chain(ssl)) {
1521
+ return -1;
1522
+ }
1523
+ ssl->state = SSL3_ST_CW_CERT_C;
1562
1524
  }
1563
1525
 
1564
- return 1;
1526
+ assert(ssl->state == SSL3_ST_CW_CERT_C);
1527
+ return ssl->method->write_message(ssl);
1565
1528
  }
1566
1529
 
1567
1530
  OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1568
1531
  SIZE_T_IS_SMALLER_THAN_UNSIGNED);
1569
1532
 
1570
- int ssl3_send_client_key_exchange(SSL *ssl) {
1533
+ static int ssl3_send_client_key_exchange(SSL *ssl) {
1571
1534
  if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
1572
- return ssl_do_write(ssl);
1535
+ return ssl->method->write_message(ssl);
1573
1536
  }
1574
1537
  assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
1575
1538
 
1576
1539
  uint8_t *pms = NULL;
1577
1540
  size_t pms_len = 0;
1578
- CBB cbb;
1579
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1580
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1541
+ CBB cbb, body;
1542
+ if (!ssl->method->init_message(ssl, &cbb, &body,
1543
+ SSL3_MT_CLIENT_KEY_EXCHANGE)) {
1581
1544
  goto err;
1582
1545
  }
1583
1546
 
@@ -1596,7 +1559,7 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1596
1559
  char identity[PSK_MAX_IDENTITY_LEN + 1];
1597
1560
  memset(identity, 0, sizeof(identity));
1598
1561
  psk_len = ssl->psk_client_callback(
1599
- ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
1562
+ ssl, ssl->s3->hs->peer_psk_identity_hint, identity, sizeof(identity),
1600
1563
  psk, sizeof(psk));
1601
1564
  if (psk_len == 0) {
1602
1565
  OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
@@ -1605,19 +1568,19 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1605
1568
  }
1606
1569
  assert(psk_len <= PSK_MAX_PSK_LEN);
1607
1570
 
1608
- OPENSSL_free(ssl->session->psk_identity);
1609
- ssl->session->psk_identity = BUF_strdup(identity);
1610
- if (ssl->session->psk_identity == NULL) {
1571
+ OPENSSL_free(ssl->s3->new_session->psk_identity);
1572
+ ssl->s3->new_session->psk_identity = BUF_strdup(identity);
1573
+ if (ssl->s3->new_session->psk_identity == NULL) {
1611
1574
  OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1612
1575
  goto err;
1613
1576
  }
1614
1577
 
1615
1578
  /* Write out psk_identity. */
1616
1579
  CBB child;
1617
- if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1580
+ if (!CBB_add_u16_length_prefixed(&body, &child) ||
1618
1581
  !CBB_add_bytes(&child, (const uint8_t *)identity,
1619
1582
  OPENSSL_strnlen(identity, sizeof(identity))) ||
1620
- !CBB_flush(&cbb)) {
1583
+ !CBB_flush(&body)) {
1621
1584
  goto err;
1622
1585
  }
1623
1586
  }
@@ -1631,7 +1594,7 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1631
1594
  goto err;
1632
1595
  }
1633
1596
 
1634
- EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer);
1597
+ EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->peer);
1635
1598
  if (pkey == NULL) {
1636
1599
  goto err;
1637
1600
  }
@@ -1643,7 +1606,6 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1643
1606
  goto err;
1644
1607
  }
1645
1608
 
1646
- ssl->session->key_exchange_info = EVP_PKEY_bits(pkey);
1647
1609
  EVP_PKEY_free(pkey);
1648
1610
 
1649
1611
  pms[0] = ssl->client_version >> 8;
@@ -1652,11 +1614,11 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1652
1614
  goto err;
1653
1615
  }
1654
1616
 
1655
- CBB child, *enc_pms = &cbb;
1617
+ CBB child, *enc_pms = &body;
1656
1618
  size_t enc_pms_len;
1657
1619
  /* In TLS, there is a length prefix. */
1658
1620
  if (ssl->version > SSL3_VERSION) {
1659
- if (!CBB_add_u16_length_prefixed(&cbb, &child)) {
1621
+ if (!CBB_add_u16_length_prefixed(&body, &child)) {
1660
1622
  goto err;
1661
1623
  }
1662
1624
  enc_pms = &child;
@@ -1669,34 +1631,27 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1669
1631
  /* Log the premaster secret, if logging is enabled. */
1670
1632
  !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1671
1633
  !CBB_did_write(enc_pms, enc_pms_len) ||
1672
- !CBB_flush(&cbb)) {
1634
+ !CBB_flush(&body)) {
1673
1635
  goto err;
1674
1636
  }
1675
- } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
1676
- /* Generate a keypair and serialize the public half. ECDHE uses a u8 length
1677
- * prefix while DHE uses u16. */
1637
+ } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
1638
+ /* Generate a keypair and serialize the public half. */
1678
1639
  CBB child;
1679
- int child_ok;
1680
- if (alg_k & SSL_kECDHE) {
1681
- child_ok = CBB_add_u8_length_prefixed(&cbb, &child);
1682
- } else {
1683
- child_ok = CBB_add_u16_length_prefixed(&cbb, &child);
1684
- }
1685
-
1686
- if (!child_ok ||
1687
- !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child) ||
1688
- !CBB_flush(&cbb)) {
1640
+ if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &body, &child)) {
1689
1641
  goto err;
1690
1642
  }
1691
1643
 
1692
1644
  /* Compute the premaster. */
1693
1645
  uint8_t alert;
1694
- if (!SSL_ECDH_CTX_compute_secret(&ssl->s3->tmp.ecdh_ctx, &pms, &pms_len,
1695
- &alert, ssl->s3->tmp.peer_key,
1696
- ssl->s3->tmp.peer_key_len)) {
1646
+ if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len,
1647
+ &alert, ssl->s3->tmp.peer_key,
1648
+ ssl->s3->tmp.peer_key_len)) {
1697
1649
  ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1698
1650
  goto err;
1699
1651
  }
1652
+ if (!CBB_flush(&body)) {
1653
+ goto err;
1654
+ }
1700
1655
 
1701
1656
  /* The key exchange state may now be discarded. */
1702
1657
  SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
@@ -1744,26 +1699,26 @@ int ssl3_send_client_key_exchange(SSL *ssl) {
1744
1699
 
1745
1700
  /* The message must be added to the finished hash before calculating the
1746
1701
  * master secret. */
1747
- size_t length;
1748
- if (!CBB_finish(&cbb, NULL, &length) ||
1749
- !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) {
1702
+ if (!ssl->method->finish_message(ssl, &cbb)) {
1750
1703
  goto err;
1751
1704
  }
1752
1705
  ssl->state = SSL3_ST_CW_KEY_EXCH_B;
1753
1706
 
1754
- ssl->session->master_key_length =
1755
- tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len);
1756
- if (ssl->session->master_key_length == 0) {
1707
+ ssl->s3->new_session->master_key_length =
1708
+ tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
1709
+ pms_len);
1710
+ if (ssl->s3->new_session->master_key_length == 0) {
1757
1711
  goto err;
1758
1712
  }
1759
- ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
1713
+ ssl->s3->new_session->extended_master_secret =
1714
+ ssl->s3->tmp.extended_master_secret;
1760
1715
  OPENSSL_cleanse(pms, pms_len);
1761
1716
  OPENSSL_free(pms);
1762
1717
 
1763
- /* SSL3_ST_CW_KEY_EXCH_B */
1764
- return ssl_do_write(ssl);
1718
+ return ssl->method->write_message(ssl);
1765
1719
 
1766
1720
  err:
1721
+ CBB_cleanup(&cbb);
1767
1722
  if (pms != NULL) {
1768
1723
  OPENSSL_cleanse(pms, pms_len);
1769
1724
  OPENSSL_free(pms);
@@ -1771,75 +1726,88 @@ err:
1771
1726
  return -1;
1772
1727
  }
1773
1728
 
1774
- int ssl3_send_cert_verify(SSL *ssl) {
1729
+ static int ssl3_send_cert_verify(SSL *ssl) {
1775
1730
  if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
1776
- return ssl_do_write(ssl);
1731
+ return ssl->method->write_message(ssl);
1777
1732
  }
1778
1733
 
1779
- CBB cbb, child;
1780
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1781
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1734
+ assert(ssl_has_private_key(ssl));
1735
+
1736
+ CBB cbb, body, child;
1737
+ if (!ssl->method->init_message(ssl, &cbb, &body,
1738
+ SSL3_MT_CERTIFICATE_VERIFY)) {
1782
1739
  goto err;
1783
1740
  }
1784
1741
 
1785
- assert(ssl_has_private_key(ssl));
1742
+ uint16_t signature_algorithm;
1743
+ if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1744
+ goto err;
1745
+ }
1746
+ if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1747
+ /* Write out the digest type in TLS 1.2. */
1748
+ if (!CBB_add_u16(&body, signature_algorithm)) {
1749
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1750
+ goto err;
1751
+ }
1752
+ }
1786
1753
 
1754
+ /* Set aside space for the signature. */
1787
1755
  const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1788
- size_t sig_len;
1756
+ uint8_t *ptr;
1757
+ if (!CBB_add_u16_length_prefixed(&body, &child) ||
1758
+ !CBB_reserve(&child, &ptr, max_sig_len)) {
1759
+ goto err;
1760
+ }
1761
+
1762
+ size_t sig_len = max_sig_len;
1789
1763
  enum ssl_private_key_result_t sign_result;
1790
1764
  if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
1791
- /* Select and write out the digest type in TLS 1.2. */
1792
- const EVP_MD *md = NULL;
1793
- if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1794
- md = tls1_choose_signing_digest(ssl);
1795
- if (!tls12_add_sigandhash(ssl, &cbb, md)) {
1796
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1765
+ /* The SSL3 construction for CertificateVerify does not decompose into a
1766
+ * single final digest and signature, and must be special-cased. */
1767
+ if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1768
+ if (ssl->cert->key_method != NULL) {
1769
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1797
1770
  goto err;
1798
1771
  }
1799
- }
1800
1772
 
1801
- /* Compute the digest. In TLS 1.1 and below, the digest type is also
1802
- * selected here. */
1803
- uint8_t digest[EVP_MAX_MD_SIZE];
1804
- size_t digest_len;
1805
- if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md,
1806
- ssl_private_key_type(ssl))) {
1807
- goto err;
1773
+ const EVP_MD *md;
1774
+ uint8_t digest[EVP_MAX_MD_SIZE];
1775
+ size_t digest_len;
1776
+ if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
1777
+ signature_algorithm)) {
1778
+ goto err;
1779
+ }
1780
+
1781
+ sign_result = ssl_private_key_success;
1782
+
1783
+ EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1784
+ if (pctx == NULL ||
1785
+ !EVP_PKEY_sign_init(pctx) ||
1786
+ !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
1787
+ !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1788
+ EVP_PKEY_CTX_free(pctx);
1789
+ sign_result = ssl_private_key_failure;
1790
+ goto err;
1791
+ }
1792
+ EVP_PKEY_CTX_free(pctx);
1793
+ } else {
1794
+ sign_result = ssl_private_key_sign(
1795
+ ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
1796
+ (const uint8_t *)ssl->s3->handshake_buffer->data,
1797
+ ssl->s3->handshake_buffer->length);
1808
1798
  }
1809
1799
 
1810
1800
  /* The handshake buffer is no longer necessary. */
1811
1801
  ssl3_free_handshake_buffer(ssl);
1812
-
1813
- /* Sign the digest. */
1814
- uint8_t *ptr;
1815
- if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1816
- !CBB_reserve(&child, &ptr, max_sig_len)) {
1817
- goto err;
1818
- }
1819
- sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1820
- digest, digest_len);
1821
1802
  } else {
1822
1803
  assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
1823
-
1824
- /* Skip over the already written signature algorithm and retry the
1825
- * signature. */
1826
- uint8_t *ptr;
1827
- if ((ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
1828
- !CBB_did_write(&cbb, 2)) ||
1829
- !CBB_add_u16_length_prefixed(&cbb, &child) ||
1830
- !CBB_reserve(&child, &ptr, max_sig_len)) {
1831
- goto err;
1832
- }
1833
- sign_result =
1834
- ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
1804
+ sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
1835
1805
  }
1836
1806
 
1837
1807
  switch (sign_result) {
1838
1808
  case ssl_private_key_success:
1839
- ssl->rwstate = SSL_NOTHING;
1840
1809
  break;
1841
1810
  case ssl_private_key_failure:
1842
- ssl->rwstate = SSL_NOTHING;
1843
1811
  goto err;
1844
1812
  case ssl_private_key_retry:
1845
1813
  ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
@@ -1847,118 +1815,22 @@ int ssl3_send_cert_verify(SSL *ssl) {
1847
1815
  goto err;
1848
1816
  }
1849
1817
 
1850
- size_t length;
1851
1818
  if (!CBB_did_write(&child, sig_len) ||
1852
- !CBB_finish(&cbb, NULL, &length) ||
1853
- !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) {
1819
+ !ssl->method->finish_message(ssl, &cbb)) {
1854
1820
  goto err;
1855
1821
  }
1856
1822
 
1857
1823
  ssl->state = SSL3_ST_CW_CERT_VRFY_C;
1858
- return ssl_do_write(ssl);
1824
+ return ssl->method->write_message(ssl);
1859
1825
 
1860
1826
  err:
1861
1827
  CBB_cleanup(&cbb);
1862
1828
  return -1;
1863
1829
  }
1864
1830
 
1865
- /* ssl3_has_client_certificate returns true if a client certificate is
1866
- * configured. */
1867
- static int ssl3_has_client_certificate(SSL *ssl) {
1868
- return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl);
1869
- }
1870
-
1871
- int ssl3_send_client_certificate(SSL *ssl) {
1872
- X509 *x509 = NULL;
1873
- EVP_PKEY *pkey = NULL;
1874
- int i;
1875
-
1876
- if (ssl->state == SSL3_ST_CW_CERT_A) {
1877
- /* Let cert callback update client certificates if required */
1878
- if (ssl->cert->cert_cb) {
1879
- i = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1880
- if (i < 0) {
1881
- ssl->rwstate = SSL_X509_LOOKUP;
1882
- return -1;
1883
- }
1884
- if (i == 0) {
1885
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1886
- return 0;
1887
- }
1888
- ssl->rwstate = SSL_NOTHING;
1889
- }
1890
-
1891
- if (ssl3_has_client_certificate(ssl)) {
1892
- ssl->state = SSL3_ST_CW_CERT_C;
1893
- } else {
1894
- ssl->state = SSL3_ST_CW_CERT_B;
1895
- }
1896
- }
1897
-
1898
- /* We need to get a client cert */
1899
- if (ssl->state == SSL3_ST_CW_CERT_B) {
1900
- /* If we get an error, we need to:
1901
- * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1902
- * We then get retried later */
1903
- i = ssl_do_client_cert_cb(ssl, &x509, &pkey);
1904
- if (i < 0) {
1905
- ssl->rwstate = SSL_X509_LOOKUP;
1906
- return -1;
1907
- }
1908
- ssl->rwstate = SSL_NOTHING;
1909
- if (i == 1 && pkey != NULL && x509 != NULL) {
1910
- ssl->state = SSL3_ST_CW_CERT_B;
1911
- if (!SSL_use_certificate(ssl, x509) || !SSL_use_PrivateKey(ssl, pkey)) {
1912
- i = 0;
1913
- }
1914
- } else if (i == 1) {
1915
- i = 0;
1916
- OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1917
- }
1918
-
1919
- X509_free(x509);
1920
- EVP_PKEY_free(pkey);
1921
- if (i && !ssl3_has_client_certificate(ssl)) {
1922
- i = 0;
1923
- }
1924
- if (i == 0) {
1925
- if (ssl->version == SSL3_VERSION) {
1926
- ssl->s3->tmp.cert_req = 0;
1927
- ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1928
- return 1;
1929
- } else {
1930
- ssl->s3->tmp.cert_req = 2;
1931
- /* There is no client certificate, so the handshake buffer may be
1932
- * released. */
1933
- ssl3_free_handshake_buffer(ssl);
1934
- }
1935
- }
1936
-
1937
- /* Ok, we have a cert */
1938
- ssl->state = SSL3_ST_CW_CERT_C;
1939
- }
1940
-
1941
- if (ssl->state == SSL3_ST_CW_CERT_C) {
1942
- if (ssl->s3->tmp.cert_req == 2) {
1943
- /* Send an empty Certificate message. */
1944
- uint8_t *p = ssl_handshake_start(ssl);
1945
- l2n3(0, p);
1946
- if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) {
1947
- return -1;
1948
- }
1949
- } else if (!ssl3_output_cert_chain(ssl)) {
1950
- return -1;
1951
- }
1952
- ssl->state = SSL3_ST_CW_CERT_D;
1953
- }
1954
-
1955
- /* SSL3_ST_CW_CERT_D */
1956
- return ssl_do_write(ssl);
1957
- }
1958
-
1959
- int ssl3_send_next_proto(SSL *ssl) {
1831
+ static int ssl3_send_next_proto(SSL *ssl) {
1960
1832
  if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
1961
- return ssl_do_write(ssl);
1833
+ return ssl->method->write_message(ssl);
1962
1834
  }
1963
1835
 
1964
1836
  assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
@@ -1966,30 +1838,26 @@ int ssl3_send_next_proto(SSL *ssl) {
1966
1838
  static const uint8_t kZero[32] = {0};
1967
1839
  size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
1968
1840
 
1969
- CBB cbb, child;
1970
- size_t length;
1971
- CBB_zero(&cbb);
1972
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1973
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1974
- !CBB_add_u8_length_prefixed(&cbb, &child) ||
1841
+ CBB cbb, body, child;
1842
+ if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1843
+ !CBB_add_u8_length_prefixed(&body, &child) ||
1975
1844
  !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1976
1845
  ssl->s3->next_proto_negotiated_len) ||
1977
- !CBB_add_u8_length_prefixed(&cbb, &child) ||
1846
+ !CBB_add_u8_length_prefixed(&body, &child) ||
1978
1847
  !CBB_add_bytes(&child, kZero, padding_len) ||
1979
- !CBB_finish(&cbb, NULL, &length) ||
1980
- !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) {
1848
+ !ssl->method->finish_message(ssl, &cbb)) {
1981
1849
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1982
1850
  CBB_cleanup(&cbb);
1983
1851
  return -1;
1984
1852
  }
1985
1853
 
1986
1854
  ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
1987
- return ssl_do_write(ssl);
1855
+ return ssl->method->write_message(ssl);
1988
1856
  }
1989
1857
 
1990
- int ssl3_send_channel_id(SSL *ssl) {
1858
+ static int ssl3_send_channel_id(SSL *ssl) {
1991
1859
  if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
1992
- return ssl_do_write(ssl);
1860
+ return ssl->method->write_message(ssl);
1993
1861
  }
1994
1862
 
1995
1863
  assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
@@ -2010,7 +1878,6 @@ int ssl3_send_channel_id(SSL *ssl) {
2010
1878
  ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
2011
1879
  return -1;
2012
1880
  }
2013
- ssl->rwstate = SSL_NOTHING;
2014
1881
 
2015
1882
  EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
2016
1883
  if (ec_key == NULL) {
@@ -2040,26 +1907,21 @@ int ssl3_send_channel_id(SSL *ssl) {
2040
1907
  goto err;
2041
1908
  }
2042
1909
 
2043
- CBB cbb, child;
2044
- size_t length;
2045
- CBB_zero(&cbb);
2046
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
2047
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
2048
- !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) ||
2049
- !CBB_add_u16_length_prefixed(&cbb, &child) ||
2050
- !BN_bn2cbb_padded(&child, 32, x) ||
2051
- !BN_bn2cbb_padded(&child, 32, y) ||
1910
+ CBB cbb, body, child;
1911
+ if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
1912
+ !CBB_add_u16(&body, TLSEXT_TYPE_channel_id) ||
1913
+ !CBB_add_u16_length_prefixed(&body, &child) ||
1914
+ !BN_bn2cbb_padded(&child, 32, x) || !BN_bn2cbb_padded(&child, 32, y) ||
2052
1915
  !BN_bn2cbb_padded(&child, 32, sig->r) ||
2053
1916
  !BN_bn2cbb_padded(&child, 32, sig->s) ||
2054
- !CBB_finish(&cbb, NULL, &length) ||
2055
- !ssl_set_handshake_header(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS, length)) {
1917
+ !ssl->method->finish_message(ssl, &cbb)) {
2056
1918
  OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2057
1919
  CBB_cleanup(&cbb);
2058
1920
  goto err;
2059
1921
  }
2060
1922
 
2061
1923
  ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
2062
- ret = ssl_do_write(ssl);
1924
+ ret = ssl->method->write_message(ssl);
2063
1925
 
2064
1926
  err:
2065
1927
  BN_free(x);
@@ -2068,23 +1930,73 @@ err:
2068
1930
  return ret;
2069
1931
  }
2070
1932
 
2071
- int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
2072
- if (ssl->ctx->client_cert_cb == NULL) {
2073
- return 0;
1933
+ static int ssl3_get_new_session_ticket(SSL *ssl) {
1934
+ int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
1935
+ ssl_hash_message);
1936
+ if (ret <= 0) {
1937
+ return ret;
2074
1938
  }
2075
- return ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey);
2076
- }
2077
1939
 
2078
- int ssl3_verify_server_cert(SSL *ssl) {
2079
- int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain);
2080
- if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) {
2081
- int al = ssl_verify_alarm_type(ssl->verify_result);
2082
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
2083
- OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
2084
- } else {
2085
- ret = 1;
2086
- ERR_clear_error(); /* but we keep ssl->verify_result */
1940
+ CBS new_session_ticket, ticket;
1941
+ uint32_t tlsext_tick_lifetime_hint;
1942
+ CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
1943
+ if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
1944
+ !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1945
+ CBS_len(&new_session_ticket) != 0) {
1946
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1947
+ OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1948
+ return -1;
2087
1949
  }
2088
1950
 
2089
- return ret;
1951
+ if (CBS_len(&ticket) == 0) {
1952
+ /* RFC 5077 allows a server to change its mind and send no ticket after
1953
+ * negotiating the extension. The value of |tlsext_ticket_expected| is
1954
+ * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
1955
+ * update. */
1956
+ ssl->tlsext_ticket_expected = 0;
1957
+ return 1;
1958
+ }
1959
+
1960
+ int session_renewed = ssl->session != NULL;
1961
+ SSL_SESSION *session = ssl->s3->new_session;
1962
+ if (session_renewed) {
1963
+ /* The server is sending a new ticket for an existing session. Sessions are
1964
+ * immutable once established, so duplicate all but the ticket of the
1965
+ * existing session. */
1966
+ session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1967
+ if (session == NULL) {
1968
+ /* This should never happen. */
1969
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1970
+ goto err;
1971
+ }
1972
+ }
1973
+
1974
+ if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
1975
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1976
+ goto err;
1977
+ }
1978
+ session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
1979
+
1980
+ /* Generate a session ID for this session based on the session ticket. We use
1981
+ * the session ID mechanism for detecting ticket resumption. This also fits in
1982
+ * with assumptions elsewhere in OpenSSL.*/
1983
+ if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1984
+ session->session_id, &session->session_id_length,
1985
+ EVP_sha256(), NULL)) {
1986
+ goto err;
1987
+ }
1988
+
1989
+ if (session_renewed) {
1990
+ session->not_resumable = 0;
1991
+ SSL_SESSION_free(ssl->session);
1992
+ ssl->session = session;
1993
+ }
1994
+
1995
+ return 1;
1996
+
1997
+ err:
1998
+ if (session_renewed) {
1999
+ SSL_SESSION_free(session);
2000
+ }
2001
+ return -1;
2090
2002
  }