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
@@ -121,7 +121,6 @@
121
121
  #include <openssl/stack.h>
122
122
  #include <openssl/x509.h>
123
123
 
124
- #include "../crypto/directory.h"
125
124
  #include "internal.h"
126
125
 
127
126
 
@@ -165,16 +164,17 @@ STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
165
164
  goto err;
166
165
  }
167
166
 
168
- /* check for duplicates */
169
- xn = X509_NAME_dup(xn);
170
- if (xn == NULL) {
171
- goto err;
172
- }
167
+ /* Check for duplicates. */
173
168
  if (sk_X509_NAME_find(sk, NULL, xn)) {
169
+ continue;
170
+ }
171
+
172
+ xn = X509_NAME_dup(xn);
173
+ if (xn == NULL ||
174
+ !sk_X509_NAME_push(sk /* non-owning */, xn) ||
175
+ !sk_X509_NAME_push(ret /* owning */, xn)) {
174
176
  X509_NAME_free(xn);
175
- } else {
176
- sk_X509_NAME_push(sk, xn);
177
- sk_X509_NAME_push(ret, xn);
177
+ goto err;
178
178
  }
179
179
  }
180
180
 
@@ -198,7 +198,7 @@ int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
198
198
  BIO *in;
199
199
  X509 *x = NULL;
200
200
  X509_NAME *xn = NULL;
201
- int ret = 1;
201
+ int ret = 0;
202
202
  int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
203
203
 
204
204
  oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
@@ -221,24 +221,24 @@ int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
221
221
  if (xn == NULL) {
222
222
  goto err;
223
223
  }
224
- xn = X509_NAME_dup(xn);
225
- if (xn == NULL) {
226
- goto err;
227
- }
224
+
225
+ /* Check for duplicates. */
228
226
  if (sk_X509_NAME_find(stack, NULL, xn)) {
227
+ continue;
228
+ }
229
+
230
+ xn = X509_NAME_dup(xn);
231
+ if (xn == NULL ||
232
+ !sk_X509_NAME_push(stack, xn)) {
229
233
  X509_NAME_free(xn);
230
- } else {
231
- sk_X509_NAME_push(stack, xn);
234
+ goto err;
232
235
  }
233
236
  }
234
237
 
235
238
  ERR_clear_error();
239
+ ret = 1;
236
240
 
237
- if (0) {
238
- err:
239
- ret = 0;
240
- }
241
-
241
+ err:
242
242
  BIO_free(in);
243
243
  X509_free(x);
244
244
 
@@ -247,53 +247,6 @@ int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
247
247
  return ret;
248
248
  }
249
249
 
250
- /* Add a directory of certs to a stack.
251
- *
252
- * \param stack the stack to append to.
253
- * \param dir the directory to append from. All files in this directory will be
254
- * examined as potential certs. Any that are acceptable to
255
- * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
256
- * be included.
257
- * \return 1 for success, 0 for failure. Note that in the case of failure some
258
- * certs may have been added to \c stack. */
259
- int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
260
- const char *dir) {
261
- OPENSSL_DIR_CTX *d = NULL;
262
- const char *filename;
263
- int ret = 0;
264
-
265
- /* Note that a side effect is that the CAs will be sorted by name */
266
- while ((filename = OPENSSL_DIR_read(&d, dir))) {
267
- char buf[1024];
268
- int r;
269
-
270
- if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
271
- OPENSSL_PUT_ERROR(SSL, SSL_R_PATH_TOO_LONG);
272
- goto err;
273
- }
274
-
275
- r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
276
- if (r <= 0 || r >= (int)sizeof(buf) ||
277
- !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
278
- goto err;
279
- }
280
- }
281
-
282
- if (errno) {
283
- OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
284
- ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
285
- goto err;
286
- }
287
-
288
- ret = 1;
289
-
290
- err:
291
- if (d) {
292
- OPENSSL_DIR_end(&d);
293
- }
294
- return ret;
295
- }
296
-
297
250
  int SSL_use_certificate_file(SSL *ssl, const char *file, int type) {
298
251
  int reason_code;
299
252
  BIO *in;
@@ -141,7 +141,7 @@
141
141
  #include <openssl/ssl.h>
142
142
 
143
143
  #include <assert.h>
144
- #include <stdio.h>
144
+ #include <stdlib.h>
145
145
  #include <string.h>
146
146
 
147
147
  #include <openssl/bytestring.h>
@@ -150,18 +150,29 @@
150
150
  #include <openssl/err.h>
151
151
  #include <openssl/lhash.h>
152
152
  #include <openssl/mem.h>
153
- #include <openssl/obj.h>
154
153
  #include <openssl/rand.h>
155
154
  #include <openssl/x509v3.h>
156
155
 
157
156
  #include "internal.h"
158
157
  #include "../crypto/internal.h"
159
158
 
159
+ #if defined(OPENSSL_WINDOWS)
160
+ #include <sys/timeb.h>
161
+ #else
162
+ #include <sys/socket.h>
163
+ #include <sys/time.h>
164
+ #endif
165
+
160
166
 
161
167
  /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
162
168
  * to avoid downstream churn. */
163
169
  OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
164
170
 
171
+ /* The following errors are no longer emitted, but are used in nginx without
172
+ * #ifdefs. */
173
+ OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
174
+ OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
175
+
165
176
  /* Some error codes are special. Ensure the make_errors.go script never
166
177
  * regresses this. */
167
178
  OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
@@ -296,9 +307,10 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
296
307
 
297
308
  /* Lock the SSL_CTX to the specified version, for compatibility with legacy
298
309
  * uses of SSL_METHOD. */
299
- if (method->version != 0) {
300
- SSL_CTX_set_max_version(ret, method->version);
301
- SSL_CTX_set_min_version(ret, method->version);
310
+ if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
311
+ !SSL_CTX_set_min_proto_version(ret, method->version)) {
312
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
313
+ goto err2;
302
314
  }
303
315
 
304
316
  return ret;
@@ -310,6 +322,11 @@ err2:
310
322
  return NULL;
311
323
  }
312
324
 
325
+ int SSL_CTX_up_ref(SSL_CTX *ctx) {
326
+ CRYPTO_refcount_inc(&ctx->references);
327
+ return 1;
328
+ }
329
+
313
330
  void SSL_CTX_free(SSL_CTX *ctx) {
314
331
  if (ctx == NULL ||
315
332
  !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
@@ -343,7 +360,7 @@ void SSL_CTX_free(SSL_CTX *ctx) {
343
360
  sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
344
361
  sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
345
362
  OPENSSL_free(ctx->psk_identity_hint);
346
- OPENSSL_free(ctx->tlsext_ellipticcurvelist);
363
+ OPENSSL_free(ctx->supported_group_list);
347
364
  OPENSSL_free(ctx->alpn_client_proto_list);
348
365
  OPENSSL_free(ctx->ocsp_response);
349
366
  OPENSSL_free(ctx->signed_cert_timestamp_list);
@@ -371,6 +388,12 @@ SSL *SSL_new(SSL_CTX *ctx) {
371
388
  ssl->min_version = ctx->min_version;
372
389
  ssl->max_version = ctx->max_version;
373
390
 
391
+ ssl->state = SSL_ST_INIT;
392
+
393
+ /* RFC 6347 states that implementations SHOULD use an initial timer value of
394
+ * 1 second. */
395
+ ssl->initial_timeout_duration_ms = 1000;
396
+
374
397
  ssl->options = ctx->options;
375
398
  ssl->mode = ctx->mode;
376
399
  ssl->max_cert_list = ctx->max_cert_list;
@@ -401,14 +424,14 @@ SSL *SSL_new(SSL_CTX *ctx) {
401
424
  CRYPTO_refcount_inc(&ctx->references);
402
425
  ssl->initial_ctx = ctx;
403
426
 
404
- if (ctx->tlsext_ellipticcurvelist) {
405
- ssl->tlsext_ellipticcurvelist =
406
- BUF_memdup(ctx->tlsext_ellipticcurvelist,
407
- ctx->tlsext_ellipticcurvelist_length * 2);
408
- if (!ssl->tlsext_ellipticcurvelist) {
427
+ if (ctx->supported_group_list) {
428
+ ssl->supported_group_list =
429
+ BUF_memdup(ctx->supported_group_list,
430
+ ctx->supported_group_list_len * 2);
431
+ if (!ssl->supported_group_list) {
409
432
  goto err;
410
433
  }
411
- ssl->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
434
+ ssl->supported_group_list_len = ctx->supported_group_list_len;
412
435
  }
413
436
 
414
437
  if (ssl->ctx->alpn_client_proto_list) {
@@ -420,7 +443,6 @@ SSL *SSL_new(SSL_CTX *ctx) {
420
443
  ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
421
444
  }
422
445
 
423
- ssl->verify_result = X509_V_OK;
424
446
  ssl->method = ctx->method;
425
447
 
426
448
  if (!ssl->method->ssl_new(ssl)) {
@@ -443,8 +465,8 @@ SSL *SSL_new(SSL_CTX *ctx) {
443
465
 
444
466
  ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
445
467
  if (ctx->tlsext_channel_id_private) {
446
- ssl->tlsext_channel_id_private =
447
- EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
468
+ EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
469
+ ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
448
470
  }
449
471
 
450
472
  ssl->signed_cert_timestamps_enabled =
@@ -469,20 +491,11 @@ void SSL_free(SSL *ssl) {
469
491
 
470
492
  CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
471
493
 
472
- if (ssl->bbio != NULL) {
473
- /* If the buffering BIO is in place, pop it off */
474
- if (ssl->bbio == ssl->wbio) {
475
- ssl->wbio = BIO_pop(ssl->wbio);
476
- }
477
- BIO_free(ssl->bbio);
478
- ssl->bbio = NULL;
479
- }
494
+ ssl_free_wbio_buffer(ssl);
495
+ assert(ssl->bbio == NULL);
480
496
 
481
- int free_wbio = ssl->wbio != ssl->rbio;
482
497
  BIO_free_all(ssl->rbio);
483
- if (free_wbio) {
484
- BIO_free_all(ssl->wbio);
485
- }
498
+ BIO_free_all(ssl->wbio);
486
499
 
487
500
  BUF_MEM_free(ssl->init_buf);
488
501
 
@@ -490,14 +503,13 @@ void SSL_free(SSL *ssl) {
490
503
  ssl_cipher_preference_list_free(ssl->cipher_list);
491
504
  sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
492
505
 
493
- ssl_clear_bad_session(ssl);
494
506
  SSL_SESSION_free(ssl->session);
495
507
 
496
508
  ssl_cert_free(ssl->cert);
497
509
 
498
510
  OPENSSL_free(ssl->tlsext_hostname);
499
511
  SSL_CTX_free(ssl->initial_ctx);
500
- OPENSSL_free(ssl->tlsext_ellipticcurvelist);
512
+ OPENSSL_free(ssl->supported_group_list);
501
513
  OPENSSL_free(ssl->alpn_client_proto_list);
502
514
  EVP_PKEY_free(ssl->tlsext_channel_id_private);
503
515
  OPENSSL_free(ssl->psk_identity_hint);
@@ -514,42 +526,85 @@ void SSL_free(SSL *ssl) {
514
526
 
515
527
  void SSL_set_connect_state(SSL *ssl) {
516
528
  ssl->server = 0;
517
- ssl->shutdown = 0;
518
- ssl->state = SSL_ST_CONNECT;
519
- ssl->handshake_func = ssl->method->ssl_connect;
529
+ ssl->handshake_func = ssl3_connect;
520
530
  }
521
531
 
522
532
  void SSL_set_accept_state(SSL *ssl) {
523
533
  ssl->server = 1;
524
- ssl->shutdown = 0;
525
- ssl->state = SSL_ST_ACCEPT;
526
- ssl->handshake_func = ssl->method->ssl_accept;
534
+ ssl->handshake_func = ssl3_accept;
527
535
  }
528
536
 
529
- void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
537
+ void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
538
+ BIO_free_all(ssl->rbio);
539
+ ssl->rbio = rbio;
540
+ }
541
+
542
+ void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
530
543
  /* If the output buffering BIO is still in place, remove it. */
531
544
  if (ssl->bbio != NULL) {
532
- if (ssl->wbio == ssl->bbio) {
533
- ssl->wbio = ssl->wbio->next_bio;
534
- ssl->bbio->next_bio = NULL;
535
- }
545
+ ssl->wbio = BIO_pop(ssl->wbio);
536
546
  }
537
547
 
538
- if (ssl->rbio != rbio) {
539
- BIO_free_all(ssl->rbio);
548
+ BIO_free_all(ssl->wbio);
549
+ ssl->wbio = wbio;
550
+
551
+ /* Re-attach |bbio| to the new |wbio|. */
552
+ if (ssl->bbio != NULL) {
553
+ ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
540
554
  }
541
- if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
542
- BIO_free_all(ssl->wbio);
555
+ }
556
+
557
+ void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
558
+ /* For historical reasons, this function has many different cases in ownership
559
+ * handling. */
560
+
561
+ /* If nothing has changed, do nothing */
562
+ if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
563
+ return;
543
564
  }
544
- ssl->rbio = rbio;
545
- ssl->wbio = wbio;
565
+
566
+ /* If the two arguments are equal, one fewer reference is granted than
567
+ * taken. */
568
+ if (rbio != NULL && rbio == wbio) {
569
+ BIO_up_ref(rbio);
570
+ }
571
+
572
+ /* If only the wbio is changed, adopt only one reference. */
573
+ if (rbio == SSL_get_rbio(ssl)) {
574
+ SSL_set0_wbio(ssl, wbio);
575
+ return;
576
+ }
577
+
578
+ /* There is an asymmetry here for historical reasons. If only the rbio is
579
+ * changed AND the rbio and wbio were originally different, then we only adopt
580
+ * one reference. */
581
+ if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
582
+ SSL_set0_rbio(ssl, rbio);
583
+ return;
584
+ }
585
+
586
+ /* Otherwise, adopt both references. */
587
+ SSL_set0_rbio(ssl, rbio);
588
+ SSL_set0_wbio(ssl, wbio);
546
589
  }
547
590
 
548
591
  BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
549
592
 
550
- BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
593
+ BIO *SSL_get_wbio(const SSL *ssl) {
594
+ if (ssl->bbio != NULL) {
595
+ /* If |bbio| is active, the true caller-configured BIO is its |next_bio|. */
596
+ assert(ssl->bbio == ssl->wbio);
597
+ return ssl->bbio->next_bio;
598
+ }
599
+ return ssl->wbio;
600
+ }
551
601
 
552
602
  int SSL_do_handshake(SSL *ssl) {
603
+ ssl->rwstate = SSL_NOTHING;
604
+ /* Functions which use SSL_get_error must clear the error queue on entry. */
605
+ ERR_clear_error();
606
+ ERR_clear_system_error();
607
+
553
608
  if (ssl->handshake_func == NULL) {
554
609
  OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
555
610
  return -1;
@@ -563,131 +618,223 @@ int SSL_do_handshake(SSL *ssl) {
563
618
  }
564
619
 
565
620
  int SSL_connect(SSL *ssl) {
566
- if (ssl->handshake_func == 0) {
621
+ if (ssl->handshake_func == NULL) {
567
622
  /* Not properly initialized yet */
568
623
  SSL_set_connect_state(ssl);
569
624
  }
570
625
 
571
- if (ssl->handshake_func != ssl->method->ssl_connect) {
572
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
573
- return -1;
574
- }
575
-
576
- return ssl->handshake_func(ssl);
626
+ return SSL_do_handshake(ssl);
577
627
  }
578
628
 
579
629
  int SSL_accept(SSL *ssl) {
580
- if (ssl->handshake_func == 0) {
630
+ if (ssl->handshake_func == NULL) {
581
631
  /* Not properly initialized yet */
582
632
  SSL_set_accept_state(ssl);
583
633
  }
584
634
 
585
- if (ssl->handshake_func != ssl->method->ssl_accept) {
586
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
587
- return -1;
588
- }
589
-
590
- return ssl->handshake_func(ssl);
635
+ return SSL_do_handshake(ssl);
591
636
  }
592
637
 
593
- int SSL_read(SSL *ssl, void *buf, int num) {
594
- if (ssl->handshake_func == 0) {
595
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
596
- return -1;
638
+ static int ssl_do_renegotiate(SSL *ssl) {
639
+ /* We do not accept renegotiations as a server. */
640
+ if (ssl->server) {
641
+ goto no_renegotiation;
597
642
  }
598
643
 
599
- if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
600
- ssl->rwstate = SSL_NOTHING;
644
+ if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
645
+ ssl->init_num != 0) {
646
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
647
+ OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
601
648
  return 0;
602
649
  }
603
650
 
604
- ERR_clear_system_error();
605
- return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
651
+ switch (ssl->renegotiate_mode) {
652
+ case ssl_renegotiate_ignore:
653
+ /* Ignore the HelloRequest. */
654
+ return 1;
655
+
656
+ case ssl_renegotiate_once:
657
+ if (ssl->s3->total_renegotiations != 0) {
658
+ goto no_renegotiation;
659
+ }
660
+ break;
661
+
662
+ case ssl_renegotiate_never:
663
+ goto no_renegotiation;
664
+
665
+ case ssl_renegotiate_freely:
666
+ break;
667
+ }
668
+
669
+ /* Renegotiation is only supported at quiescent points in the application
670
+ * protocol, namely in HTTPS, just before reading the HTTP response. Require
671
+ * the record-layer be idle and avoid complexities of sending a handshake
672
+ * record while an application_data record is being written. */
673
+ if (ssl_write_buffer_is_pending(ssl)) {
674
+ goto no_renegotiation;
675
+ }
676
+
677
+ /* Begin a new handshake. */
678
+ ssl->s3->total_renegotiations++;
679
+ ssl->state = SSL_ST_INIT;
680
+ return 1;
681
+
682
+ no_renegotiation:
683
+ ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
684
+ OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
685
+ return 0;
606
686
  }
607
687
 
608
- int SSL_peek(SSL *ssl, void *buf, int num) {
609
- if (ssl->handshake_func == 0) {
688
+ static int ssl_do_post_handshake(SSL *ssl) {
689
+ if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
690
+ return ssl_do_renegotiate(ssl);
691
+ }
692
+
693
+ return tls13_post_handshake(ssl);
694
+ }
695
+
696
+ static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
697
+ ssl->rwstate = SSL_NOTHING;
698
+ /* Functions which use SSL_get_error must clear the error queue on entry. */
699
+ ERR_clear_error();
700
+ ERR_clear_system_error();
701
+
702
+ if (ssl->handshake_func == NULL) {
610
703
  OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
611
704
  return -1;
612
705
  }
613
706
 
614
- if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
615
- return 0;
707
+ for (;;) {
708
+ /* Complete the current handshake, if any. False Start will cause
709
+ * |SSL_do_handshake| to return mid-handshake, so this may require multiple
710
+ * iterations. */
711
+ while (SSL_in_init(ssl)) {
712
+ int ret = SSL_do_handshake(ssl);
713
+ if (ret < 0) {
714
+ return ret;
715
+ }
716
+ if (ret == 0) {
717
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
718
+ return -1;
719
+ }
720
+ }
721
+
722
+ int got_handshake;
723
+ int ret = ssl->method->read_app_data(ssl, &got_handshake, buf, num, peek);
724
+ if (ret > 0 || !got_handshake) {
725
+ ssl->s3->key_update_count = 0;
726
+ return ret;
727
+ }
728
+
729
+ /* Handle the post-handshake message and try again. */
730
+ if (!ssl_do_post_handshake(ssl)) {
731
+ return -1;
732
+ }
733
+ ssl->method->release_current_message(ssl, 1 /* free buffer */);
616
734
  }
735
+ }
617
736
 
618
- ERR_clear_system_error();
619
- return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
737
+ int SSL_read(SSL *ssl, void *buf, int num) {
738
+ return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
739
+ }
740
+
741
+ int SSL_peek(SSL *ssl, void *buf, int num) {
742
+ return ssl_read_impl(ssl, buf, num, 1 /* peek */);
620
743
  }
621
744
 
622
745
  int SSL_write(SSL *ssl, const void *buf, int num) {
623
- if (ssl->handshake_func == 0) {
746
+ ssl->rwstate = SSL_NOTHING;
747
+ /* Functions which use SSL_get_error must clear the error queue on entry. */
748
+ ERR_clear_error();
749
+ ERR_clear_system_error();
750
+
751
+ if (ssl->handshake_func == NULL) {
624
752
  OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
625
753
  return -1;
626
754
  }
627
755
 
628
- if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
629
- ssl->rwstate = SSL_NOTHING;
756
+ if (ssl->s3->send_shutdown != ssl_shutdown_none) {
630
757
  OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
631
758
  return -1;
632
759
  }
633
760
 
634
- ERR_clear_system_error();
635
- return ssl->method->ssl_write_app_data(ssl, buf, num);
761
+ /* If necessary, complete the handshake implicitly. */
762
+ if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
763
+ int ret = SSL_do_handshake(ssl);
764
+ if (ret < 0) {
765
+ return ret;
766
+ }
767
+ if (ret == 0) {
768
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
769
+ return -1;
770
+ }
771
+ }
772
+
773
+ return ssl->method->write_app_data(ssl, buf, num);
636
774
  }
637
775
 
638
776
  int SSL_shutdown(SSL *ssl) {
639
- /* Note that this function behaves differently from what one might expect.
640
- * Return values are 0 for no success (yet), 1 for success; but calling it
641
- * once is usually not enough, even if blocking I/O is used (see
642
- * ssl3_shutdown). */
777
+ ssl->rwstate = SSL_NOTHING;
778
+ /* Functions which use SSL_get_error must clear the error queue on entry. */
779
+ ERR_clear_error();
780
+ ERR_clear_system_error();
643
781
 
644
- if (ssl->handshake_func == 0) {
782
+ if (ssl->handshake_func == NULL) {
645
783
  OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
646
784
  return -1;
647
785
  }
648
786
 
787
+ /* We can't shutdown properly if we are in the middle of a handshake. */
649
788
  if (SSL_in_init(ssl)) {
650
- return 1;
789
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
790
+ return -1;
651
791
  }
652
792
 
653
- /* Do nothing if configured not to send a close_notify. */
654
793
  if (ssl->quiet_shutdown) {
655
- ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
794
+ /* Do nothing if configured not to send a close_notify. */
795
+ ssl->s3->send_shutdown = ssl_shutdown_close_notify;
796
+ ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
656
797
  return 1;
657
798
  }
658
799
 
659
- if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
660
- ssl->shutdown |= SSL_SENT_SHUTDOWN;
661
- ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
800
+ /* This function completes in two stages. It sends a close_notify and then it
801
+ * waits for a close_notify to come in. Perform exactly one action and return
802
+ * whether or not it succeeds. */
662
803
 
663
- /* our shutdown alert has been sent now, and if it still needs to be
664
- * written, ssl->s3->alert_dispatch will be true */
665
- if (ssl->s3->alert_dispatch) {
666
- return -1; /* return WANT_WRITE */
804
+ if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
805
+ /* Send a close_notify. */
806
+ if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
807
+ return -1;
667
808
  }
668
809
  } else if (ssl->s3->alert_dispatch) {
669
- /* resend it if not sent */
670
- int ret = ssl->method->ssl_dispatch_alert(ssl);
671
- if (ret == -1) {
672
- /* we only get to return -1 here the 2nd/Nth invocation, we must have
673
- * already signalled return 0 upon a previous invoation, return
674
- * WANT_WRITE */
675
- return ret;
810
+ /* Finish sending the close_notify. */
811
+ if (ssl->method->dispatch_alert(ssl) <= 0) {
812
+ return -1;
676
813
  }
677
- } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
678
- /* If we are waiting for a close from our peer, we are closed */
679
- ssl->method->ssl_read_close_notify(ssl);
680
- if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
681
- return -1; /* return WANT_READ */
814
+ } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
815
+ /* Wait for the peer's close_notify. */
816
+ ssl->method->read_close_notify(ssl);
817
+ if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
818
+ return -1;
682
819
  }
683
820
  }
684
821
 
685
- if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
686
- !ssl->s3->alert_dispatch) {
687
- return 1;
688
- } else {
689
- return 0;
822
+ /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
823
+ return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
824
+ }
825
+
826
+ int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
827
+ if (ssl->s3->alert_dispatch) {
828
+ if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
829
+ ssl->s3->send_alert[1] != alert) {
830
+ /* We are already attempting to write a different alert. */
831
+ OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
832
+ return -1;
833
+ }
834
+ return ssl->method->dispatch_alert(ssl);
690
835
  }
836
+
837
+ return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
691
838
  }
692
839
 
693
840
  int SSL_get_error(const SSL *ssl, int ret_code) {
@@ -710,9 +857,7 @@ int SSL_get_error(const SSL *ssl, int ret_code) {
710
857
  }
711
858
 
712
859
  if (ret_code == 0) {
713
- if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
714
- (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
715
- /* The socket was cleanly shut down with a close_notify. */
860
+ if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
716
861
  return SSL_ERROR_ZERO_RETURN;
717
862
  }
718
863
  /* An EOF was observed which violates the protocol, and the underlying
@@ -799,20 +944,52 @@ int SSL_get_error(const SSL *ssl, int ret_code) {
799
944
  return SSL_ERROR_SYSCALL;
800
945
  }
801
946
 
802
- void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
803
- ctx->min_version = version;
947
+ static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
948
+ uint16_t version) {
949
+ if (version == 0) {
950
+ *out = method->min_version;
951
+ return 1;
952
+ }
953
+
954
+ if (version == TLS1_3_VERSION) {
955
+ version = TLS1_3_DRAFT_VERSION;
956
+ }
957
+
958
+ return method->version_from_wire(out, version);
959
+ }
960
+
961
+ static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
962
+ uint16_t version) {
963
+ if (version == 0) {
964
+ *out = method->max_version;
965
+ /* TODO(svaldez): Enable TLS 1.3 by default once fully implemented. */
966
+ if (*out > TLS1_2_VERSION) {
967
+ *out = TLS1_2_VERSION;
968
+ }
969
+ return 1;
970
+ }
971
+
972
+ if (version == TLS1_3_VERSION) {
973
+ version = TLS1_3_DRAFT_VERSION;
974
+ }
975
+
976
+ return method->version_from_wire(out, version);
977
+ }
978
+
979
+ int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
980
+ return set_min_version(ctx->method, &ctx->min_version, version);
804
981
  }
805
982
 
806
- void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
807
- ctx->max_version = version;
983
+ int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
984
+ return set_max_version(ctx->method, &ctx->max_version, version);
808
985
  }
809
986
 
810
- void SSL_set_min_version(SSL *ssl, uint16_t version) {
811
- ssl->min_version = version;
987
+ int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
988
+ return set_min_version(ssl->method, &ssl->min_version, version);
812
989
  }
813
990
 
814
- void SSL_set_max_version(SSL *ssl, uint16_t version) {
815
- ssl->max_version = version;
991
+ int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
992
+ return set_max_version(ssl->method, &ssl->max_version, version);
816
993
  }
817
994
 
818
995
  uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
@@ -864,17 +1041,26 @@ uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
864
1041
  uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
865
1042
 
866
1043
  X509 *SSL_get_peer_certificate(const SSL *ssl) {
867
- if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
1044
+ if (ssl == NULL) {
1045
+ return NULL;
1046
+ }
1047
+ SSL_SESSION *session = SSL_get_session(ssl);
1048
+ if (session == NULL || session->peer == NULL) {
868
1049
  return NULL;
869
1050
  }
870
- return X509_up_ref(ssl->session->peer);
1051
+ X509_up_ref(session->peer);
1052
+ return session->peer;
871
1053
  }
872
1054
 
873
1055
  STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
874
- if (ssl == NULL || ssl->session == NULL) {
1056
+ if (ssl == NULL) {
1057
+ return NULL;
1058
+ }
1059
+ SSL_SESSION *session = SSL_get_session(ssl);
1060
+ if (session == NULL) {
875
1061
  return NULL;
876
1062
  }
877
- return ssl->session->cert_chain;
1063
+ return session->cert_chain;
878
1064
  }
879
1065
 
880
1066
  int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
@@ -884,7 +1070,7 @@ int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
884
1070
  * https://tools.ietf.org/html/rfc5929#section-3.1. */
885
1071
  const uint8_t *finished = ssl->s3->previous_client_finished;
886
1072
  size_t finished_len = ssl->s3->previous_client_finished_len;
887
- if (ssl->hit) {
1073
+ if (ssl->session != NULL) {
888
1074
  /* tls-unique is broken for resumed sessions unless EMS is used. */
889
1075
  if (!ssl->session->extended_master_secret) {
890
1076
  goto err;
@@ -997,7 +1183,7 @@ int SSL_get_wfd(const SSL *ssl) {
997
1183
  }
998
1184
 
999
1185
  int SSL_set_fd(SSL *ssl, int fd) {
1000
- BIO *bio = BIO_new(BIO_s_fd());
1186
+ BIO *bio = BIO_new(BIO_s_socket());
1001
1187
  if (bio == NULL) {
1002
1188
  OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1003
1189
  return 0;
@@ -1008,35 +1194,40 @@ int SSL_set_fd(SSL *ssl, int fd) {
1008
1194
  }
1009
1195
 
1010
1196
  int SSL_set_wfd(SSL *ssl, int fd) {
1011
- if (ssl->rbio == NULL ||
1012
- BIO_method_type(ssl->rbio) != BIO_TYPE_FD ||
1013
- BIO_get_fd(ssl->rbio, NULL) != fd) {
1014
- BIO *bio = BIO_new(BIO_s_fd());
1197
+ BIO *rbio = SSL_get_rbio(ssl);
1198
+ if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
1199
+ BIO_get_fd(rbio, NULL) != fd) {
1200
+ BIO *bio = BIO_new(BIO_s_socket());
1015
1201
  if (bio == NULL) {
1016
1202
  OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1017
1203
  return 0;
1018
1204
  }
1019
1205
  BIO_set_fd(bio, fd, BIO_NOCLOSE);
1020
- SSL_set_bio(ssl, SSL_get_rbio(ssl), bio);
1206
+ SSL_set0_wbio(ssl, bio);
1021
1207
  } else {
1022
- SSL_set_bio(ssl, SSL_get_rbio(ssl), SSL_get_rbio(ssl));
1208
+ /* Copy the rbio over to the wbio. */
1209
+ BIO_up_ref(rbio);
1210
+ SSL_set0_wbio(ssl, rbio);
1023
1211
  }
1024
1212
 
1025
1213
  return 1;
1026
1214
  }
1027
1215
 
1028
1216
  int SSL_set_rfd(SSL *ssl, int fd) {
1029
- if (ssl->wbio == NULL || BIO_method_type(ssl->wbio) != BIO_TYPE_FD ||
1030
- BIO_get_fd(ssl->wbio, NULL) != fd) {
1031
- BIO *bio = BIO_new(BIO_s_fd());
1217
+ BIO *wbio = SSL_get_wbio(ssl);
1218
+ if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
1219
+ BIO_get_fd(wbio, NULL) != fd) {
1220
+ BIO *bio = BIO_new(BIO_s_socket());
1032
1221
  if (bio == NULL) {
1033
1222
  OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1034
1223
  return 0;
1035
1224
  }
1036
1225
  BIO_set_fd(bio, fd, BIO_NOCLOSE);
1037
- SSL_set_bio(ssl, bio, SSL_get_wbio(ssl));
1226
+ SSL_set0_rbio(ssl, bio);
1038
1227
  } else {
1039
- SSL_set_bio(ssl, SSL_get_wbio(ssl), SSL_get_wbio(ssl));
1228
+ /* Copy the wbio over to the rbio. */
1229
+ BIO_up_ref(wbio);
1230
+ SSL_set0_rbio(ssl, wbio);
1040
1231
  }
1041
1232
  return 1;
1042
1233
  }
@@ -1076,7 +1267,11 @@ int SSL_get_verify_depth(const SSL *ssl) {
1076
1267
  }
1077
1268
 
1078
1269
  int SSL_get_extms_support(const SSL *ssl) {
1079
- return ssl->s3->tmp.extended_master_secret == 1;
1270
+ if (!ssl->s3->have_version) {
1271
+ return 0;
1272
+ }
1273
+ return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1274
+ ssl->s3->tmp.extended_master_secret == 1;
1080
1275
  }
1081
1276
 
1082
1277
  int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
@@ -1165,6 +1360,10 @@ int SSL_renegotiate_pending(SSL *ssl) {
1165
1360
  return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
1166
1361
  }
1167
1362
 
1363
+ int SSL_total_renegotiations(const SSL *ssl) {
1364
+ return ssl->s3->total_renegotiations;
1365
+ }
1366
+
1168
1367
  size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
1169
1368
  return ctx->max_cert_list;
1170
1369
  }
@@ -1212,7 +1411,7 @@ int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
1212
1411
  }
1213
1412
 
1214
1413
  int SSL_set_mtu(SSL *ssl, unsigned mtu) {
1215
- if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
1414
+ if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
1216
1415
  return 0;
1217
1416
  }
1218
1417
  ssl->d1->mtu = mtu;
@@ -1249,6 +1448,90 @@ int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
1249
1448
  return ctx->session_cache_mode;
1250
1449
  }
1251
1450
 
1451
+
1452
+ int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
1453
+ if (out == NULL) {
1454
+ return 48;
1455
+ }
1456
+ if (len != 48) {
1457
+ OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1458
+ return 0;
1459
+ }
1460
+ uint8_t *out_bytes = out;
1461
+ memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
1462
+ memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
1463
+ memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
1464
+ return 1;
1465
+ }
1466
+
1467
+ int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
1468
+ if (in == NULL) {
1469
+ return 48;
1470
+ }
1471
+ if (len != 48) {
1472
+ OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
1473
+ return 0;
1474
+ }
1475
+ const uint8_t *in_bytes = in;
1476
+ memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
1477
+ memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
1478
+ memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
1479
+ return 1;
1480
+ }
1481
+
1482
+ int SSL_CTX_set_tlsext_ticket_key_cb(
1483
+ SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
1484
+ EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
1485
+ int encrypt)) {
1486
+ ctx->tlsext_ticket_key_cb = callback;
1487
+ return 1;
1488
+ }
1489
+
1490
+ int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
1491
+ return tls1_set_curves(&ctx->supported_group_list,
1492
+ &ctx->supported_group_list_len, curves,
1493
+ curves_len);
1494
+ }
1495
+
1496
+ int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
1497
+ return tls1_set_curves(&ssl->supported_group_list,
1498
+ &ssl->supported_group_list_len, curves,
1499
+ curves_len);
1500
+ }
1501
+
1502
+ uint16_t SSL_get_curve_id(const SSL *ssl) {
1503
+ /* TODO(davidben): This checks the wrong session if there is a renegotiation in
1504
+ * progress. */
1505
+ SSL_SESSION *session = SSL_get_session(ssl);
1506
+ if (session == NULL ||
1507
+ session->cipher == NULL ||
1508
+ !SSL_CIPHER_is_ECDHE(session->cipher)) {
1509
+ return 0;
1510
+ }
1511
+
1512
+ return (uint16_t)session->key_exchange_info;
1513
+ }
1514
+
1515
+ int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
1516
+ DH_free(ctx->cert->dh_tmp);
1517
+ ctx->cert->dh_tmp = DHparams_dup(dh);
1518
+ if (ctx->cert->dh_tmp == NULL) {
1519
+ OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
1520
+ return 0;
1521
+ }
1522
+ return 1;
1523
+ }
1524
+
1525
+ int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
1526
+ DH_free(ssl->cert->dh_tmp);
1527
+ ssl->cert->dh_tmp = DHparams_dup(dh);
1528
+ if (ssl->cert->dh_tmp == NULL) {
1529
+ OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
1530
+ return 0;
1531
+ }
1532
+ return 1;
1533
+ }
1534
+
1252
1535
  STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1253
1536
  if (ssl == NULL) {
1254
1537
  return NULL;
@@ -1258,17 +1541,15 @@ STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
1258
1541
  return ssl->cipher_list->ciphers;
1259
1542
  }
1260
1543
 
1261
- if (ssl->version >= TLS1_1_VERSION && ssl->ctx != NULL &&
1262
- ssl->ctx->cipher_list_tls11 != NULL) {
1544
+ if (ssl->version >= TLS1_1_VERSION && ssl->ctx->cipher_list_tls11 != NULL) {
1263
1545
  return ssl->ctx->cipher_list_tls11->ciphers;
1264
1546
  }
1265
1547
 
1266
- if (ssl->version >= TLS1_VERSION && ssl->ctx != NULL &&
1267
- ssl->ctx->cipher_list_tls10 != NULL) {
1548
+ if (ssl->version >= TLS1_VERSION && ssl->ctx->cipher_list_tls10 != NULL) {
1268
1549
  return ssl->ctx->cipher_list_tls10->ciphers;
1269
1550
  }
1270
1551
 
1271
- if (ssl->ctx != NULL && ssl->ctx->cipher_list != NULL) {
1552
+ if (ssl->ctx->cipher_list != NULL) {
1272
1553
  return ssl->ctx->cipher_list->ciphers;
1273
1554
  }
1274
1555
 
@@ -1286,7 +1567,7 @@ STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *ssl) {
1286
1567
  return ssl->cipher_list_by_id;
1287
1568
  }
1288
1569
 
1289
- if (ssl->ctx != NULL && ssl->ctx->cipher_list_by_id != NULL) {
1570
+ if (ssl->ctx->cipher_list_by_id != NULL) {
1290
1571
  return ssl->ctx->cipher_list_by_id;
1291
1572
  }
1292
1573
 
@@ -1378,21 +1659,12 @@ int SSL_set_cipher_list(SSL *ssl, const char *str) {
1378
1659
  return 1;
1379
1660
  }
1380
1661
 
1381
- STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *ssl, const CBS *cbs) {
1382
- CBS cipher_suites = *cbs;
1383
- const SSL_CIPHER *c;
1384
- STACK_OF(SSL_CIPHER) *sk;
1385
-
1386
- if (ssl->s3) {
1387
- ssl->s3->send_connection_binding = 0;
1388
- }
1389
-
1390
- if (CBS_len(&cipher_suites) % 2 != 0) {
1391
- OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1392
- return NULL;
1393
- }
1662
+ STACK_OF(SSL_CIPHER) *
1663
+ ssl_parse_client_cipher_list(const struct ssl_early_callback_ctx *ctx) {
1664
+ CBS cipher_suites;
1665
+ CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len);
1394
1666
 
1395
- sk = sk_SSL_CIPHER_new_null();
1667
+ STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
1396
1668
  if (sk == NULL) {
1397
1669
  OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1398
1670
  goto err;
@@ -1402,35 +1674,11 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *ssl, const CBS *cbs) {
1402
1674
  uint16_t cipher_suite;
1403
1675
 
1404
1676
  if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
1405
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1677
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1406
1678
  goto err;
1407
1679
  }
1408
1680
 
1409
- /* Check for SCSV. */
1410
- if (ssl->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
1411
- /* SCSV is fatal if renegotiating. */
1412
- if (ssl->s3->initial_handshake_complete) {
1413
- OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1414
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1415
- goto err;
1416
- }
1417
- ssl->s3->send_connection_binding = 1;
1418
- continue;
1419
- }
1420
-
1421
- /* Check for FALLBACK_SCSV. */
1422
- if (ssl->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
1423
- uint16_t max_version = ssl3_get_max_server_version(ssl);
1424
- if (SSL_IS_DTLS(ssl) ? (uint16_t)ssl->version > max_version
1425
- : (uint16_t)ssl->version < max_version) {
1426
- OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
1427
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1428
- goto err;
1429
- }
1430
- continue;
1431
- }
1432
-
1433
- c = SSL_get_cipher_by_value(cipher_suite);
1681
+ const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
1434
1682
  if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
1435
1683
  OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1436
1684
  goto err;
@@ -1455,18 +1703,19 @@ const char *SSL_get_servername(const SSL *ssl, const int type) {
1455
1703
  return ssl->tlsext_hostname;
1456
1704
  }
1457
1705
 
1458
- if (ssl->session == NULL) {
1706
+ SSL_SESSION *session = SSL_get_session(ssl);
1707
+ if (session == NULL) {
1459
1708
  return NULL;
1460
1709
  }
1461
- return ssl->session->tlsext_hostname;
1710
+ return session->tlsext_hostname;
1462
1711
  }
1463
1712
 
1464
1713
  int SSL_get_servername_type(const SSL *ssl) {
1465
- if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
1466
- return TLSEXT_NAMETYPE_host_name;
1714
+ SSL_SESSION *session = SSL_get_session(ssl);
1715
+ if (session == NULL || session->tlsext_hostname == NULL) {
1716
+ return -1;
1467
1717
  }
1468
-
1469
- return -1;
1718
+ return TLSEXT_NAMETYPE_host_name;
1470
1719
  }
1471
1720
 
1472
1721
  void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
@@ -1489,7 +1738,7 @@ int SSL_enable_ocsp_stapling(SSL *ssl) {
1489
1738
 
1490
1739
  void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1491
1740
  size_t *out_len) {
1492
- SSL_SESSION *session = ssl->session;
1741
+ SSL_SESSION *session = SSL_get_session(ssl);
1493
1742
 
1494
1743
  *out_len = 0;
1495
1744
  *out = NULL;
@@ -1503,7 +1752,7 @@ void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
1503
1752
 
1504
1753
  void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
1505
1754
  size_t *out_len) {
1506
- SSL_SESSION *session = ssl->session;
1755
+ SSL_SESSION *session = SSL_get_session(ssl);
1507
1756
 
1508
1757
  *out_len = 0;
1509
1758
  *out = NULL;
@@ -1542,6 +1791,38 @@ int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
1542
1791
  return 1;
1543
1792
  }
1544
1793
 
1794
+ int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
1795
+ OPENSSL_free(ssl->tlsext_hostname);
1796
+ ssl->tlsext_hostname = NULL;
1797
+
1798
+ if (name == NULL) {
1799
+ return 1;
1800
+ }
1801
+
1802
+ size_t len = strlen(name);
1803
+ if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
1804
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
1805
+ return 0;
1806
+ }
1807
+ ssl->tlsext_hostname = BUF_strdup(name);
1808
+ if (ssl->tlsext_hostname == NULL) {
1809
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1810
+ return 0;
1811
+ }
1812
+ return 1;
1813
+ }
1814
+
1815
+ int SSL_CTX_set_tlsext_servername_callback(
1816
+ SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
1817
+ ctx->tlsext_servername_callback = callback;
1818
+ return 1;
1819
+ }
1820
+
1821
+ int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
1822
+ ctx->tlsext_servername_arg = arg;
1823
+ return 1;
1824
+ }
1825
+
1545
1826
  int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
1546
1827
  const uint8_t *server, unsigned server_len,
1547
1828
  const uint8_t *client, unsigned client_len) {
@@ -1647,6 +1928,60 @@ void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
1647
1928
  }
1648
1929
  }
1649
1930
 
1931
+
1932
+ int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
1933
+ ctx->tlsext_channel_id_enabled = 1;
1934
+ return 1;
1935
+ }
1936
+
1937
+ int SSL_enable_tls_channel_id(SSL *ssl) {
1938
+ ssl->tlsext_channel_id_enabled = 1;
1939
+ return 1;
1940
+ }
1941
+
1942
+ static int is_p256_key(EVP_PKEY *private_key) {
1943
+ const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
1944
+ return ec_key != NULL &&
1945
+ EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
1946
+ NID_X9_62_prime256v1;
1947
+ }
1948
+
1949
+ int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
1950
+ if (!is_p256_key(private_key)) {
1951
+ OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1952
+ return 0;
1953
+ }
1954
+
1955
+ EVP_PKEY_free(ctx->tlsext_channel_id_private);
1956
+ EVP_PKEY_up_ref(private_key);
1957
+ ctx->tlsext_channel_id_private = private_key;
1958
+ ctx->tlsext_channel_id_enabled = 1;
1959
+
1960
+ return 1;
1961
+ }
1962
+
1963
+ int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
1964
+ if (!is_p256_key(private_key)) {
1965
+ OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
1966
+ return 0;
1967
+ }
1968
+
1969
+ EVP_PKEY_free(ssl->tlsext_channel_id_private);
1970
+ EVP_PKEY_up_ref(private_key);
1971
+ ssl->tlsext_channel_id_private = private_key;
1972
+ ssl->tlsext_channel_id_enabled = 1;
1973
+
1974
+ return 1;
1975
+ }
1976
+
1977
+ size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
1978
+ if (!ssl->s3->tlsext_channel_id_valid) {
1979
+ return 0;
1980
+ }
1981
+ memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
1982
+ return 64;
1983
+ }
1984
+
1650
1985
  void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
1651
1986
  int (*cb)(X509_STORE_CTX *store_ctx,
1652
1987
  void *arg),
@@ -1674,59 +2009,53 @@ void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1674
2009
  ssl_cert_set_cert_cb(ssl->cert, cb, arg);
1675
2010
  }
1676
2011
 
2012
+ size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
2013
+ if (ssl->server) {
2014
+ *out_types = NULL;
2015
+ return 0;
2016
+ }
2017
+ *out_types = ssl->s3->tmp.certificate_types;
2018
+ return ssl->s3->tmp.num_certificate_types;
2019
+ }
2020
+
1677
2021
  void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
1678
2022
  uint32_t *out_mask_a) {
1679
- CERT *c = ssl->cert;
1680
- int have_rsa_cert = 0, dh_tmp;
1681
- uint32_t mask_k, mask_a;
1682
- int have_ecc_cert = 0, ecdsa_ok;
1683
- X509 *x;
1684
-
1685
- dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
2023
+ uint32_t mask_k = 0;
2024
+ uint32_t mask_a = 0;
1686
2025
 
1687
2026
  if (ssl->cert->x509 != NULL && ssl_has_private_key(ssl)) {
1688
- if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
1689
- have_rsa_cert = 1;
1690
- } else if (ssl_private_key_type(ssl) == EVP_PKEY_EC) {
1691
- have_ecc_cert = 1;
2027
+ int type = ssl_private_key_type(ssl);
2028
+ if (type == NID_rsaEncryption) {
2029
+ mask_k |= SSL_kRSA;
2030
+ mask_a |= SSL_aRSA;
2031
+ } else if (ssl_is_ecdsa_key_type(type)) {
2032
+ /* An ECC certificate may be usable for ECDSA cipher suites depending on
2033
+ * the key usage extension and on the client's group preferences. */
2034
+ X509 *x = ssl->cert->x509;
2035
+ /* This call populates extension flags (ex_flags). */
2036
+ X509_check_purpose(x, -1, 0);
2037
+ int ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
2038
+ ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
2039
+ : 1;
2040
+ if (ecdsa_ok && tls1_check_ec_cert(ssl, x)) {
2041
+ mask_a |= SSL_aECDSA;
2042
+ }
1692
2043
  }
1693
2044
  }
1694
2045
 
1695
- mask_k = 0;
1696
- mask_a = 0;
1697
-
1698
- if (dh_tmp) {
2046
+ if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
1699
2047
  mask_k |= SSL_kDHE;
1700
2048
  }
1701
- if (have_rsa_cert) {
1702
- mask_k |= SSL_kRSA;
1703
- mask_a |= SSL_aRSA;
1704
- }
1705
-
1706
- /* An ECC certificate may be usable for ECDSA cipher suites depending on the
1707
- * key usage extension and on the client's curve preferences. */
1708
- if (have_ecc_cert) {
1709
- x = c->x509;
1710
- /* This call populates extension flags (ex_flags). */
1711
- X509_check_purpose(x, -1, 0);
1712
- ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
1713
- ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
1714
- : 1;
1715
- if (!tls1_check_ec_cert(ssl, x)) {
1716
- ecdsa_ok = 0;
1717
- }
1718
- if (ecdsa_ok) {
1719
- mask_a |= SSL_aECDSA;
1720
- }
1721
- }
1722
2049
 
1723
- /* If we are considering an ECC cipher suite that uses an ephemeral EC
1724
- * key, check for a shared curve. */
2050
+ /* Check for a shared group to consider ECDHE ciphers. */
1725
2051
  uint16_t unused;
1726
- if (tls1_get_shared_curve(ssl, &unused)) {
2052
+ if (tls1_get_shared_group(ssl, &unused)) {
1727
2053
  mask_k |= SSL_kECDHE;
1728
2054
  }
1729
2055
 
2056
+ /* CECPQ1 ciphers are always acceptable if supported by both sides. */
2057
+ mask_k |= SSL_kCECPQ1;
2058
+
1730
2059
  /* PSK requires a server callback. */
1731
2060
  if (ssl->psk_server_callback != NULL) {
1732
2061
  mask_k |= SSL_kPSK;
@@ -1740,7 +2069,7 @@ void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
1740
2069
  void ssl_update_cache(SSL *ssl, int mode) {
1741
2070
  SSL_CTX *ctx = ssl->initial_ctx;
1742
2071
  /* Never cache sessions with empty session IDs. */
1743
- if (ssl->session->session_id_length == 0 ||
2072
+ if (ssl->s3->established_session->session_id_length == 0 ||
1744
2073
  (ctx->session_cache_mode & mode) != mode) {
1745
2074
  return;
1746
2075
  }
@@ -1752,14 +2081,17 @@ void ssl_update_cache(SSL *ssl, int mode) {
1752
2081
  /* A client may see new sessions on abbreviated handshakes if the server
1753
2082
  * decides to renew the ticket. Once the handshake is completed, it should be
1754
2083
  * inserted into the cache. */
1755
- if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
2084
+ if (ssl->s3->established_session != ssl->session ||
2085
+ (!ssl->server && ssl->tlsext_ticket_expected)) {
1756
2086
  if (use_internal_cache) {
1757
- SSL_CTX_add_session(ctx, ssl->session);
2087
+ SSL_CTX_add_session(ctx, ssl->s3->established_session);
1758
2088
  }
1759
- if (ctx->new_session_cb != NULL &&
1760
- !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
1761
- /* |new_session_cb|'s return value signals whether it took ownership. */
1762
- SSL_SESSION_free(ssl->session);
2089
+ if (ctx->new_session_cb != NULL) {
2090
+ SSL_SESSION_up_ref(ssl->s3->established_session);
2091
+ if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
2092
+ /* |new_session_cb|'s return value signals whether it took ownership. */
2093
+ SSL_SESSION_free(ssl->s3->established_session);
2094
+ }
1763
2095
  }
1764
2096
  }
1765
2097
 
@@ -1773,16 +2105,22 @@ void ssl_update_cache(SSL *ssl, int mode) {
1773
2105
  flush_cache = 1;
1774
2106
  ctx->handshakes_since_cache_flush = 0;
1775
2107
  }
1776
- CRYPTO_MUTEX_unlock(&ctx->lock);
2108
+ CRYPTO_MUTEX_unlock_write(&ctx->lock);
1777
2109
 
1778
2110
  if (flush_cache) {
1779
- SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
2111
+ struct timeval now;
2112
+ ssl_get_current_time(ssl, &now);
2113
+ SSL_CTX_flush_sessions(ctx, (long)now.tv_sec);
1780
2114
  }
1781
2115
  }
1782
2116
  }
1783
2117
 
1784
2118
  static const char *ssl_get_version(int version) {
1785
2119
  switch (version) {
2120
+ /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
2121
+ case TLS1_3_DRAFT_VERSION:
2122
+ return "TLSv1.3";
2123
+
1786
2124
  case TLS1_2_VERSION:
1787
2125
  return "TLSv1.2";
1788
2126
 
@@ -1853,42 +2191,36 @@ const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
1853
2191
  return ssl->s3->aead_write_ctx->cipher;
1854
2192
  }
1855
2193
 
2194
+ int SSL_session_reused(const SSL *ssl) {
2195
+ return ssl->s3->session_reused;
2196
+ }
2197
+
1856
2198
  const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
1857
2199
 
1858
2200
  const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
1859
2201
 
1860
- int ssl_init_wbio_buffer(SSL *ssl, int push) {
1861
- BIO *bbio;
2202
+ int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
1862
2203
 
1863
- if (ssl->bbio == NULL) {
1864
- bbio = BIO_new(BIO_f_buffer());
1865
- if (bbio == NULL) {
1866
- return 0;
1867
- }
1868
- ssl->bbio = bbio;
1869
- } else {
1870
- bbio = ssl->bbio;
1871
- if (ssl->bbio == ssl->wbio) {
1872
- ssl->wbio = BIO_pop(ssl->wbio);
1873
- }
1874
- }
2204
+ int ssl_is_wbio_buffered(const SSL *ssl) {
2205
+ return ssl->bbio != NULL;
2206
+ }
1875
2207
 
1876
- BIO_reset(bbio);
1877
- if (!BIO_set_read_buffer_size(bbio, 1)) {
1878
- OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1879
- return 0;
2208
+ int ssl_init_wbio_buffer(SSL *ssl) {
2209
+ if (ssl->bbio != NULL) {
2210
+ /* Already buffered. */
2211
+ assert(ssl->bbio == ssl->wbio);
2212
+ return 1;
1880
2213
  }
1881
2214
 
1882
- if (push) {
1883
- if (ssl->wbio != bbio) {
1884
- ssl->wbio = BIO_push(bbio, ssl->wbio);
1885
- }
1886
- } else {
1887
- if (ssl->wbio == bbio) {
1888
- ssl->wbio = BIO_pop(bbio);
1889
- }
2215
+ BIO *bbio = BIO_new(BIO_f_buffer());
2216
+ if (bbio == NULL ||
2217
+ !BIO_set_read_buffer_size(bbio, 1)) {
2218
+ BIO_free(bbio);
2219
+ return 0;
1890
2220
  }
1891
2221
 
2222
+ ssl->bbio = bbio;
2223
+ ssl->wbio = BIO_push(bbio, ssl->wbio);
1892
2224
  return 1;
1893
2225
  }
1894
2226
 
@@ -1897,11 +2229,9 @@ void ssl_free_wbio_buffer(SSL *ssl) {
1897
2229
  return;
1898
2230
  }
1899
2231
 
1900
- if (ssl->bbio == ssl->wbio) {
1901
- /* remove buffering */
1902
- ssl->wbio = BIO_pop(ssl->wbio);
1903
- }
2232
+ assert(ssl->bbio == ssl->wbio);
1904
2233
 
2234
+ ssl->wbio = BIO_pop(ssl->wbio);
1905
2235
  BIO_free(ssl->bbio);
1906
2236
  ssl->bbio = NULL;
1907
2237
  }
@@ -1923,14 +2253,41 @@ int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
1923
2253
  void SSL_set_shutdown(SSL *ssl, int mode) {
1924
2254
  /* It is an error to clear any bits that have already been set. (We can't try
1925
2255
  * to get a second close_notify or send two.) */
1926
- assert((ssl->shutdown & mode) == ssl->shutdown);
2256
+ assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
2257
+
2258
+ if (mode & SSL_RECEIVED_SHUTDOWN &&
2259
+ ssl->s3->recv_shutdown == ssl_shutdown_none) {
2260
+ ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
2261
+ }
2262
+
2263
+ if (mode & SSL_SENT_SHUTDOWN &&
2264
+ ssl->s3->send_shutdown == ssl_shutdown_none) {
2265
+ ssl->s3->send_shutdown = ssl_shutdown_close_notify;
2266
+ }
2267
+ }
1927
2268
 
1928
- ssl->shutdown |= mode;
2269
+ int SSL_get_shutdown(const SSL *ssl) {
2270
+ int ret = 0;
2271
+ if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
2272
+ /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
2273
+ * and fatal alert. */
2274
+ ret |= SSL_RECEIVED_SHUTDOWN;
2275
+ }
2276
+ if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
2277
+ /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
2278
+ ret |= SSL_SENT_SHUTDOWN;
2279
+ }
2280
+ return ret;
1929
2281
  }
1930
2282
 
1931
- int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
2283
+ int SSL_version(const SSL *ssl) {
2284
+ /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
2285
+ if (ssl->version == TLS1_3_DRAFT_VERSION) {
2286
+ return TLS1_3_VERSION;
2287
+ }
1932
2288
 
1933
- int SSL_version(const SSL *ssl) { return ssl->version; }
2289
+ return ssl->version;
2290
+ }
1934
2291
 
1935
2292
  SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
1936
2293
 
@@ -1980,11 +2337,27 @@ int SSL_state(const SSL *ssl) { return ssl->state; }
1980
2337
 
1981
2338
  void SSL_set_state(SSL *ssl, int state) { }
1982
2339
 
2340
+ char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
2341
+ if (len <= 0) {
2342
+ return NULL;
2343
+ }
2344
+ buf[0] = '\0';
2345
+ return buf;
2346
+ }
2347
+
1983
2348
  void SSL_set_verify_result(SSL *ssl, long result) {
1984
- ssl->verify_result = result;
2349
+ if (result != X509_V_OK) {
2350
+ abort();
2351
+ }
1985
2352
  }
1986
2353
 
1987
- long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
2354
+ long SSL_get_verify_result(const SSL *ssl) {
2355
+ SSL_SESSION *session = SSL_get_session(ssl);
2356
+ if (session == NULL) {
2357
+ return X509_V_ERR_INVALID_CALL;
2358
+ }
2359
+ return session->verify_result;
2360
+ }
1988
2361
 
1989
2362
  int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
1990
2363
  CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
@@ -2054,6 +2427,19 @@ void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
2054
2427
  ssl->cert->dh_tmp_cb = callback;
2055
2428
  }
2056
2429
 
2430
+ unsigned SSL_get_dhe_group_size(const SSL *ssl) {
2431
+ /* TODO(davidben): This checks the wrong session if there is a renegotiation in
2432
+ * progress. */
2433
+ SSL_SESSION *session = SSL_get_session(ssl);
2434
+ if (session == NULL ||
2435
+ session->cipher == NULL ||
2436
+ !SSL_CIPHER_is_DHE(session->cipher)) {
2437
+ return 0;
2438
+ }
2439
+
2440
+ return session->key_exchange_info;
2441
+ }
2442
+
2057
2443
  int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
2058
2444
  if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
2059
2445
  OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
@@ -2088,7 +2474,11 @@ int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
2088
2474
  OPENSSL_free(ssl->psk_identity_hint);
2089
2475
  ssl->psk_identity_hint = NULL;
2090
2476
 
2091
- if (identity_hint != NULL) {
2477
+ /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
2478
+ * send either no hint (omit ServerKeyExchange) or an empty hint, while
2479
+ * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
2480
+ * so we interpret empty and missing as identical. */
2481
+ if (identity_hint != NULL && identity_hint[0] != '\0') {
2092
2482
  ssl->psk_identity_hint = BUF_strdup(identity_hint);
2093
2483
  if (ssl->psk_identity_hint == NULL) {
2094
2484
  return 0;
@@ -2106,11 +2496,14 @@ const char *SSL_get_psk_identity_hint(const SSL *ssl) {
2106
2496
  }
2107
2497
 
2108
2498
  const char *SSL_get_psk_identity(const SSL *ssl) {
2109
- if (ssl == NULL || ssl->session == NULL) {
2499
+ if (ssl == NULL) {
2110
2500
  return NULL;
2111
2501
  }
2112
-
2113
- return ssl->session->psk_identity;
2502
+ SSL_SESSION *session = SSL_get_session(ssl);
2503
+ if (session == NULL) {
2504
+ return NULL;
2505
+ }
2506
+ return session->psk_identity;
2114
2507
  }
2115
2508
 
2116
2509
  void SSL_set_psk_client_callback(
@@ -2166,16 +2559,26 @@ void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
2166
2559
  ctx->keylog_callback = cb;
2167
2560
  }
2168
2561
 
2562
+ void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
2563
+ const char *line) {
2564
+ return ctx->keylog_callback;
2565
+ }
2566
+
2567
+ void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
2568
+ void (*cb)(const SSL *ssl,
2569
+ struct timeval *out_clock)) {
2570
+ ctx->current_time_cb = cb;
2571
+ }
2572
+
2169
2573
  static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
2170
2574
  static const char hextable[] = "0123456789abcdef";
2171
2575
  uint8_t *out;
2172
- size_t i;
2173
2576
 
2174
2577
  if (!CBB_add_space(cbb, &out, in_len * 2)) {
2175
2578
  return 0;
2176
2579
  }
2177
2580
 
2178
- for (i = 0; i < in_len; i++) {
2581
+ for (size_t i = 0; i < in_len; i++) {
2179
2582
  *(out++) = (uint8_t)hextable[in[i] >> 4];
2180
2583
  *(out++) = (uint8_t)hextable[in[i] & 0xf];
2181
2584
  }
@@ -2218,26 +2621,22 @@ int ssl_log_rsa_client_key_exchange(const SSL *ssl,
2218
2621
  return 1;
2219
2622
  }
2220
2623
 
2221
- int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
2222
- size_t client_random_len, const uint8_t *master,
2223
- size_t master_len) {
2624
+ int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
2625
+ size_t secret_len) {
2224
2626
  if (ssl->ctx->keylog_callback == NULL) {
2225
2627
  return 1;
2226
2628
  }
2227
2629
 
2228
- if (client_random_len != 32) {
2229
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2230
- return 0;
2231
- }
2232
-
2233
2630
  CBB cbb;
2234
2631
  uint8_t *out;
2235
2632
  size_t out_len;
2236
- if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
2237
- !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
2238
- !cbb_add_hex(&cbb, client_random, 32) ||
2633
+ if (!CBB_init(&cbb, strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
2634
+ secret_len * 2 + 1) ||
2635
+ !CBB_add_bytes(&cbb, (const uint8_t *)label, strlen(label)) ||
2636
+ !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2637
+ !cbb_add_hex(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
2239
2638
  !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
2240
- !cbb_add_hex(&cbb, master, master_len) ||
2639
+ !cbb_add_hex(&cbb, secret, secret_len) ||
2241
2640
  !CBB_add_u8(&cbb, 0 /* NUL */) ||
2242
2641
  !CBB_finish(&cbb, &out, &out_len)) {
2243
2642
  CBB_cleanup(&cbb);
@@ -2276,11 +2675,12 @@ int ssl3_can_false_start(const SSL *ssl) {
2276
2675
  const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
2277
2676
 
2278
2677
  /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
2279
- return !SSL_IS_DTLS(ssl) &&
2280
- SSL_version(ssl) >= TLS1_2_VERSION &&
2678
+ return !SSL_is_dtls(ssl) &&
2679
+ SSL_version(ssl) == TLS1_2_VERSION &&
2281
2680
  (ssl->s3->alpn_selected || ssl->s3->next_proto_neg_seen) &&
2282
2681
  cipher != NULL &&
2283
- cipher->algorithm_mkey == SSL_kECDHE &&
2682
+ (cipher->algorithm_mkey == SSL_kECDHE ||
2683
+ cipher->algorithm_mkey == SSL_kCECPQ1) &&
2284
2684
  cipher->algorithm_mac == SSL_AEAD;
2285
2685
  }
2286
2686
 
@@ -2292,8 +2692,7 @@ const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2292
2692
  case TLS1_VERSION:
2293
2693
  case TLS1_1_VERSION:
2294
2694
  case TLS1_2_VERSION:
2295
- case DTLS1_VERSION:
2296
- case DTLS1_2_VERSION:
2695
+ case TLS1_3_VERSION:
2297
2696
  return &TLSv1_enc_data;
2298
2697
 
2299
2698
  default:
@@ -2301,90 +2700,41 @@ const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
2301
2700
  }
2302
2701
  }
2303
2702
 
2304
- uint16_t ssl3_get_max_server_version(const SSL *ssl) {
2305
- uint16_t max_version;
2306
-
2307
- if (SSL_IS_DTLS(ssl)) {
2308
- max_version = (ssl->max_version != 0) ? ssl->max_version : DTLS1_2_VERSION;
2309
- if (!(ssl->options & SSL_OP_NO_DTLSv1_2) &&
2310
- DTLS1_2_VERSION >= max_version) {
2311
- return DTLS1_2_VERSION;
2312
- }
2313
- if (!(ssl->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
2314
- return DTLS1_VERSION;
2315
- }
2316
- return 0;
2317
- }
2318
-
2319
- max_version = (ssl->max_version != 0) ? ssl->max_version : TLS1_2_VERSION;
2320
- if (!(ssl->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
2321
- return TLS1_2_VERSION;
2322
- }
2323
- if (!(ssl->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
2324
- return TLS1_1_VERSION;
2325
- }
2326
- if (!(ssl->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
2327
- return TLS1_VERSION;
2328
- }
2329
- if (!(ssl->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
2330
- return SSL3_VERSION;
2331
- }
2332
- return 0;
2333
- }
2334
-
2335
- uint16_t ssl3_get_mutual_version(SSL *ssl, uint16_t client_version) {
2336
- uint16_t version = 0;
2337
-
2338
- if (SSL_IS_DTLS(ssl)) {
2339
- /* Clamp client_version to max_version. */
2340
- if (ssl->max_version != 0 && client_version < ssl->max_version) {
2341
- client_version = ssl->max_version;
2342
- }
2703
+ const struct {
2704
+ uint16_t version;
2705
+ uint32_t flag;
2706
+ } kVersions[] = {
2707
+ {SSL3_VERSION, SSL_OP_NO_SSLv3},
2708
+ {TLS1_VERSION, SSL_OP_NO_TLSv1},
2709
+ {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
2710
+ {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
2711
+ {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
2712
+ };
2343
2713
 
2344
- if (client_version <= DTLS1_2_VERSION &&
2345
- !(ssl->options & SSL_OP_NO_DTLSv1_2)) {
2346
- version = DTLS1_2_VERSION;
2347
- } else if (client_version <= DTLS1_VERSION &&
2348
- !(ssl->options & SSL_OP_NO_DTLSv1)) {
2349
- version = DTLS1_VERSION;
2350
- }
2714
+ static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
2351
2715
 
2352
- /* Check against min_version. */
2353
- if (version != 0 && ssl->min_version != 0 && version > ssl->min_version) {
2354
- return 0;
2355
- }
2356
- return version;
2357
- } else {
2358
- /* Clamp client_version to max_version. */
2359
- if (ssl->max_version != 0 && client_version > ssl->max_version) {
2360
- client_version = ssl->max_version;
2716
+ int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
2717
+ uint16_t *out_max_version) {
2718
+ /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
2719
+ * DTLS 1.0 should be mapped to TLS 1.1. */
2720
+ uint32_t options = ssl->options;
2721
+ if (SSL_is_dtls(ssl)) {
2722
+ options &= ~SSL_OP_NO_TLSv1_1;
2723
+ if (options & SSL_OP_NO_DTLSv1) {
2724
+ options |= SSL_OP_NO_TLSv1_1;
2361
2725
  }
2726
+ }
2362
2727
 
2363
- if (client_version >= TLS1_2_VERSION &&
2364
- !(ssl->options & SSL_OP_NO_TLSv1_2)) {
2365
- version = TLS1_2_VERSION;
2366
- } else if (client_version >= TLS1_1_VERSION &&
2367
- !(ssl->options & SSL_OP_NO_TLSv1_1)) {
2368
- version = TLS1_1_VERSION;
2369
- } else if (client_version >= TLS1_VERSION &&
2370
- !(ssl->options & SSL_OP_NO_TLSv1)) {
2371
- version = TLS1_VERSION;
2372
- } else if (client_version >= SSL3_VERSION &&
2373
- !(ssl->options & SSL_OP_NO_SSLv3)) {
2374
- version = SSL3_VERSION;
2375
- }
2728
+ uint16_t min_version = ssl->min_version;
2729
+ uint16_t max_version = ssl->max_version;
2376
2730
 
2377
- /* Check against min_version. */
2378
- if (version != 0 && ssl->min_version != 0 && version < ssl->min_version) {
2379
- return 0;
2380
- }
2381
- return version;
2731
+ /* Bound the range to only those implemented in this protocol. */
2732
+ if (min_version < ssl->method->min_version) {
2733
+ min_version = ssl->method->min_version;
2734
+ }
2735
+ if (max_version > ssl->method->max_version) {
2736
+ max_version = ssl->method->max_version;
2382
2737
  }
2383
- }
2384
-
2385
- uint16_t ssl3_get_max_client_version(SSL *ssl) {
2386
- uint32_t options = ssl->options;
2387
- uint16_t version = 0;
2388
2738
 
2389
2739
  /* OpenSSL's API for controlling versions entails blacklisting individual
2390
2740
  * protocols. This has two problems. First, on the client, the protocol can
@@ -2395,116 +2745,60 @@ uint16_t ssl3_get_max_client_version(SSL *ssl) {
2395
2745
  * To account for both of these, OpenSSL interprets the client-side bitmask
2396
2746
  * as a min/max range by picking the lowest contiguous non-empty range of
2397
2747
  * enabled protocols. Note that this means it is impossible to set a maximum
2398
- * version of TLS 1.2 in a future-proof way.
2399
- *
2400
- * By this scheme, the maximum version is the lowest version V such that V is
2401
- * enabled and V+1 is disabled or unimplemented. */
2402
- if (SSL_IS_DTLS(ssl)) {
2403
- if (!(options & SSL_OP_NO_DTLSv1_2)) {
2404
- version = DTLS1_2_VERSION;
2405
- }
2406
- if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
2407
- version = DTLS1_VERSION;
2408
- }
2409
- if (ssl->max_version != 0 && version < ssl->max_version) {
2410
- version = ssl->max_version;
2411
- }
2412
- } else {
2413
- if (!(options & SSL_OP_NO_TLSv1_2)) {
2414
- version = TLS1_2_VERSION;
2415
- }
2416
- if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
2417
- version = TLS1_1_VERSION;
2418
- }
2419
- if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
2420
- version = TLS1_VERSION;
2421
- }
2422
- if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
2423
- version = SSL3_VERSION;
2424
- }
2425
- if (ssl->max_version != 0 && version > ssl->max_version) {
2426
- version = ssl->max_version;
2427
- }
2428
- }
2429
-
2430
- return version;
2431
- }
2432
-
2433
- int ssl3_is_version_enabled(SSL *ssl, uint16_t version) {
2434
- if (SSL_IS_DTLS(ssl)) {
2435
- if (ssl->max_version != 0 && version < ssl->max_version) {
2436
- return 0;
2748
+ * version of the higest supported TLS version in a future-proof way. */
2749
+ int any_enabled = 0;
2750
+ for (size_t i = 0; i < kVersionsLen; i++) {
2751
+ /* Only look at the versions already enabled. */
2752
+ if (min_version > kVersions[i].version) {
2753
+ continue;
2437
2754
  }
2438
- if (ssl->min_version != 0 && version > ssl->min_version) {
2439
- return 0;
2755
+ if (max_version < kVersions[i].version) {
2756
+ break;
2440
2757
  }
2441
2758
 
2442
- switch (version) {
2443
- case DTLS1_VERSION:
2444
- return !(ssl->options & SSL_OP_NO_DTLSv1);
2445
-
2446
- case DTLS1_2_VERSION:
2447
- return !(ssl->options & SSL_OP_NO_DTLSv1_2);
2448
-
2449
- default:
2450
- return 0;
2451
- }
2452
- } else {
2453
- if (ssl->max_version != 0 && version > ssl->max_version) {
2454
- return 0;
2455
- }
2456
- if (ssl->min_version != 0 && version < ssl->min_version) {
2457
- return 0;
2759
+ if (!(options & kVersions[i].flag)) {
2760
+ /* The minimum version is the first enabled version. */
2761
+ if (!any_enabled) {
2762
+ any_enabled = 1;
2763
+ min_version = kVersions[i].version;
2764
+ }
2765
+ continue;
2458
2766
  }
2459
2767
 
2460
- switch (version) {
2461
- case SSL3_VERSION:
2462
- return !(ssl->options & SSL_OP_NO_SSLv3);
2463
-
2464
- case TLS1_VERSION:
2465
- return !(ssl->options & SSL_OP_NO_TLSv1);
2466
-
2467
- case TLS1_1_VERSION:
2468
- return !(ssl->options & SSL_OP_NO_TLSv1_1);
2469
-
2470
- case TLS1_2_VERSION:
2471
- return !(ssl->options & SSL_OP_NO_TLSv1_2);
2472
-
2473
- default:
2474
- return 0;
2768
+ /* If there is a disabled version after the first enabled one, all versions
2769
+ * after it are implicitly disabled. */
2770
+ if (any_enabled) {
2771
+ max_version = kVersions[i-1].version;
2772
+ break;
2475
2773
  }
2476
2774
  }
2477
- }
2478
2775
 
2479
- uint16_t ssl3_version_from_wire(const SSL *ssl, uint16_t wire_version) {
2480
- if (!SSL_IS_DTLS(ssl)) {
2481
- return wire_version;
2776
+ if (!any_enabled) {
2777
+ OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
2778
+ return 0;
2482
2779
  }
2483
2780
 
2484
- uint16_t tls_version = ~wire_version;
2485
- uint16_t version = tls_version + 0x0201;
2486
- /* If either component overflowed, clamp it so comparisons still work. */
2487
- if ((version >> 8) < (tls_version >> 8)) {
2488
- version = 0xff00 | (version & 0xff);
2489
- }
2490
- if ((version & 0xff) < (tls_version & 0xff)) {
2491
- version = (version & 0xff00) | 0xff;
2492
- }
2493
- /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
2494
- if (version == TLS1_VERSION) {
2495
- version = TLS1_1_VERSION;
2496
- }
2497
- return version;
2781
+ *out_min_version = min_version;
2782
+ *out_max_version = max_version;
2783
+ return 1;
2498
2784
  }
2499
2785
 
2500
2786
  uint16_t ssl3_protocol_version(const SSL *ssl) {
2501
2787
  assert(ssl->s3->have_version);
2502
- return ssl3_version_from_wire(ssl, ssl->version);
2788
+ uint16_t version;
2789
+ if (!ssl->method->version_from_wire(&version, ssl->version)) {
2790
+ /* TODO(davidben): Use the internal version representation for ssl->version
2791
+ * and map to the public API representation at API boundaries. */
2792
+ assert(0);
2793
+ return 0;
2794
+ }
2795
+
2796
+ return version;
2503
2797
  }
2504
2798
 
2505
- int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
2799
+ int SSL_is_server(const SSL *ssl) { return ssl->server; }
2506
2800
 
2507
- int SSL_is_server(SSL *ssl) { return ssl->server; }
2801
+ int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
2508
2802
 
2509
2803
  void SSL_CTX_set_select_certificate_cb(
2510
2804
  SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
@@ -2525,16 +2819,6 @@ void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
2525
2819
  ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
2526
2820
  }
2527
2821
 
2528
- int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
2529
- const RC4_KEY **write_key) {
2530
- if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
2531
- return 0;
2532
- }
2533
-
2534
- return EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_read_ctx->ctx, read_key) &&
2535
- EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_write_ctx->ctx, write_key);
2536
- }
2537
-
2538
2822
  int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
2539
2823
  const uint8_t **out_write_iv, size_t *out_iv_len) {
2540
2824
  if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
@@ -2562,7 +2846,7 @@ static uint64_t be_to_u64(const uint8_t in[8]) {
2562
2846
 
2563
2847
  uint64_t SSL_get_read_sequence(const SSL *ssl) {
2564
2848
  /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
2565
- if (SSL_IS_DTLS(ssl)) {
2849
+ if (SSL_is_dtls(ssl)) {
2566
2850
  /* max_seq_num already includes the epoch. */
2567
2851
  assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
2568
2852
  return ssl->d1->bitmap.max_seq_num;
@@ -2572,15 +2856,15 @@ uint64_t SSL_get_read_sequence(const SSL *ssl) {
2572
2856
 
2573
2857
  uint64_t SSL_get_write_sequence(const SSL *ssl) {
2574
2858
  uint64_t ret = be_to_u64(ssl->s3->write_sequence);
2575
- if (SSL_IS_DTLS(ssl)) {
2859
+ if (SSL_is_dtls(ssl)) {
2576
2860
  assert((ret >> 48) == 0);
2577
2861
  ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
2578
2862
  }
2579
2863
  return ret;
2580
2864
  }
2581
2865
 
2582
- uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
2583
- return ssl->s3->tmp.server_key_exchange_hash;
2866
+ uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
2867
+ return ssl->s3->tmp.peer_signature_algorithm;
2584
2868
  }
2585
2869
 
2586
2870
  size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
@@ -2616,43 +2900,29 @@ void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
2616
2900
  ctx->retain_only_sha256_of_client_certs = !!enabled;
2617
2901
  }
2618
2902
 
2903
+ void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
2904
+ ctx->grease_enabled = !!enabled;
2905
+ }
2906
+
2619
2907
  int SSL_clear(SSL *ssl) {
2620
2908
  if (ssl->method == NULL) {
2621
2909
  OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
2622
2910
  return 0;
2623
2911
  }
2624
2912
 
2625
- if (ssl_clear_bad_session(ssl)) {
2626
- SSL_SESSION_free(ssl->session);
2627
- ssl->session = NULL;
2628
- }
2629
-
2630
- ssl->hit = 0;
2631
- ssl->shutdown = 0;
2632
-
2633
- /* SSL_clear may be called before or after the |ssl| is initialized in either
2634
- * accept or connect state. In the latter case, SSL_clear should preserve the
2635
- * half and reset |ssl->state| accordingly. */
2636
- if (ssl->handshake_func != NULL) {
2637
- if (ssl->server) {
2638
- SSL_set_accept_state(ssl);
2639
- } else {
2640
- SSL_set_connect_state(ssl);
2641
- }
2642
- } else {
2643
- assert(ssl->state == 0);
2644
- }
2645
-
2646
2913
  /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
2647
2914
  * |SSL_clear| because it is per-connection state rather than configuration
2648
2915
  * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
2649
2916
  * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
2650
2917
  * |ssl3_new|. */
2651
2918
 
2919
+ ssl->state = SSL_ST_INIT;
2652
2920
  ssl->rwstate = SSL_NOTHING;
2653
2921
 
2654
2922
  BUF_MEM_free(ssl->init_buf);
2655
2923
  ssl->init_buf = NULL;
2924
+ ssl->init_msg = NULL;
2925
+ ssl->init_num = 0;
2656
2926
 
2657
2927
  /* The ssl->d1->mtu is simultaneously configuration (preserved across
2658
2928
  * clear) and connection-specific state (gets reset).
@@ -2668,7 +2938,7 @@ int SSL_clear(SSL *ssl) {
2668
2938
  return 0;
2669
2939
  }
2670
2940
 
2671
- if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2941
+ if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
2672
2942
  ssl->d1->mtu = mtu;
2673
2943
  }
2674
2944
 
@@ -2677,6 +2947,44 @@ int SSL_clear(SSL *ssl) {
2677
2947
  return 1;
2678
2948
  }
2679
2949
 
2950
+ void ssl_do_info_callback(const SSL *ssl, int type, int value) {
2951
+ void (*cb)(const SSL *ssl, int type, int value) = NULL;
2952
+ if (ssl->info_callback != NULL) {
2953
+ cb = ssl->info_callback;
2954
+ } else if (ssl->ctx->info_callback != NULL) {
2955
+ cb = ssl->ctx->info_callback;
2956
+ }
2957
+
2958
+ if (cb != NULL) {
2959
+ cb(ssl, type, value);
2960
+ }
2961
+ }
2962
+
2963
+ void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
2964
+ const void *buf, size_t len) {
2965
+ if (ssl->msg_callback == NULL) {
2966
+ return;
2967
+ }
2968
+
2969
+ /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
2970
+ * a V2ClientHello. */
2971
+ int version;
2972
+ switch (content_type) {
2973
+ case 0:
2974
+ /* V2ClientHello */
2975
+ version = SSL2_VERSION;
2976
+ break;
2977
+ case SSL3_RT_HEADER:
2978
+ version = 0;
2979
+ break;
2980
+ default:
2981
+ version = SSL_version(ssl);
2982
+ }
2983
+
2984
+ ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
2985
+ ssl->msg_callback_arg);
2986
+ }
2987
+
2680
2988
  int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
2681
2989
  int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
2682
2990
  int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
@@ -2688,5 +2996,68 @@ int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
2688
2996
  int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
2689
2997
  int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
2690
2998
  int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
2999
+
3000
+ int SSL_num_renegotiations(const SSL *ssl) {
3001
+ return SSL_total_renegotiations(ssl);
3002
+ }
3003
+
3004
+ int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
3005
+ int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
3006
+ int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
3007
+ int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
2691
3008
  void ERR_load_SSL_strings(void) {}
2692
3009
  void SSL_load_error_strings(void) {}
3010
+ int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
3011
+
3012
+ int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
3013
+ if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
3014
+ OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
3015
+ return 0;
3016
+ }
3017
+ int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
3018
+ return SSL_CTX_set1_curves(ctx, &nid, 1);
3019
+ }
3020
+
3021
+ int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
3022
+ if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
3023
+ OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
3024
+ return 0;
3025
+ }
3026
+ int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
3027
+ return SSL_set1_curves(ssl, &nid, 1);
3028
+ }
3029
+
3030
+ void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock) {
3031
+ if (ssl->ctx->current_time_cb != NULL) {
3032
+ ssl->ctx->current_time_cb(ssl, out_clock);
3033
+ return;
3034
+ }
3035
+
3036
+ #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
3037
+ out_clock->tv_sec = 1234;
3038
+ out_clock->tv_usec = 1234;
3039
+ #elif defined(OPENSSL_WINDOWS)
3040
+ struct _timeb time;
3041
+ _ftime(&time);
3042
+ out_clock->tv_sec = time.time;
3043
+ out_clock->tv_usec = time.millitm * 1000;
3044
+ #else
3045
+ gettimeofday(out_clock, NULL);
3046
+ #endif
3047
+ }
3048
+
3049
+ int SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
3050
+ return SSL_CTX_set_min_proto_version(ctx, version);
3051
+ }
3052
+
3053
+ int SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
3054
+ return SSL_CTX_set_max_proto_version(ctx, version);
3055
+ }
3056
+
3057
+ int SSL_set_min_version(SSL *ssl, uint16_t version) {
3058
+ return SSL_set_min_proto_version(ssl, version);
3059
+ }
3060
+
3061
+ int SSL_set_max_version(SSL *ssl, uint16_t version) {
3062
+ return SSL_set_max_proto_version(ssl, version);
3063
+ }