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
@@ -1,197 +0,0 @@
1
- /*
2
- * DTLS implementation written by Nagendra Modadugu
3
- * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
4
- */
5
- /* ====================================================================
6
- * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
7
- *
8
- * Redistribution and use in source and binary forms, with or without
9
- * modification, are permitted provided that the following conditions
10
- * are met:
11
- *
12
- * 1. Redistributions of source code must retain the above copyright
13
- * notice, this list of conditions and the following disclaimer.
14
- *
15
- * 2. Redistributions in binary form must reproduce the above copyright
16
- * notice, this list of conditions and the following disclaimer in
17
- * the documentation and/or other materials provided with the
18
- * distribution.
19
- *
20
- * 3. All advertising materials mentioning features or use of this
21
- * software must display the following acknowledgment:
22
- * "This product includes software developed by the OpenSSL Project
23
- * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
- *
25
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
- * endorse or promote products derived from this software without
27
- * prior written permission. For written permission, please contact
28
- * openssl-core@OpenSSL.org.
29
- *
30
- * 5. Products derived from this software may not be called "OpenSSL"
31
- * nor may "OpenSSL" appear in their names without prior written
32
- * permission of the OpenSSL Project.
33
- *
34
- * 6. Redistributions of any form whatsoever must retain the following
35
- * acknowledgment:
36
- * "This product includes software developed by the OpenSSL Project
37
- * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
- *
39
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
- * OF THE POSSIBILITY OF SUCH DAMAGE.
51
- * ====================================================================
52
- *
53
- * This product includes cryptographic software written by Eric Young
54
- * (eay@cryptsoft.com). This product includes software written by Tim
55
- * Hudson (tjh@cryptsoft.com). */
56
-
57
- #include <openssl/pqueue.h>
58
-
59
- #include <assert.h>
60
- #include <string.h>
61
-
62
- #include <openssl/mem.h>
63
-
64
-
65
- typedef struct _pqueue {
66
- pitem *items;
67
- unsigned count;
68
- } pqueue_s;
69
-
70
-
71
- pitem *pitem_new(uint8_t prio64be[8], void *data) {
72
- pitem *item = OPENSSL_malloc(sizeof(pitem));
73
- if (item == NULL) {
74
- return NULL;
75
- }
76
-
77
- memcpy(item->priority, prio64be, sizeof(item->priority));
78
-
79
- item->data = data;
80
- item->next = NULL;
81
-
82
- return item;
83
- }
84
-
85
- void pitem_free(pitem *item) {
86
- if (item == NULL) {
87
- return;
88
- }
89
-
90
- OPENSSL_free(item);
91
- }
92
-
93
- pqueue pqueue_new(void) {
94
- pqueue_s *pq = OPENSSL_malloc(sizeof(pqueue_s));
95
- if (pq == NULL) {
96
- return NULL;
97
- }
98
-
99
- memset(pq, 0, sizeof(pqueue_s));
100
- return pq;
101
- }
102
-
103
- void pqueue_free(pqueue_s *pq) {
104
- if (pq == NULL) {
105
- return;
106
- }
107
-
108
- /* The queue must be empty. */
109
- assert(pq->items == NULL);
110
- OPENSSL_free(pq);
111
- }
112
-
113
- pitem *pqueue_peek(pqueue_s *pq) { return pq->items; }
114
-
115
- pitem *pqueue_find(pqueue_s *pq, uint8_t *prio64be) {
116
- pitem *curr;
117
-
118
- for (curr = pq->items; curr; curr = curr->next) {
119
- if (memcmp(curr->priority, prio64be, sizeof(curr->priority)) == 0) {
120
- return curr;
121
- }
122
- }
123
-
124
- return NULL;
125
- }
126
-
127
- size_t pqueue_size(pqueue_s *pq) {
128
- pitem *item = pq->items;
129
- size_t count = 0;
130
-
131
- while (item != NULL) {
132
- count++;
133
- item = item->next;
134
- }
135
- return count;
136
- }
137
-
138
- piterator pqueue_iterator(pqueue_s *pq) { return pq->items; }
139
-
140
- pitem *pqueue_next(piterator *item) {
141
- pitem *ret;
142
-
143
- if (item == NULL || *item == NULL) {
144
- return NULL;
145
- }
146
-
147
- ret = *item;
148
- *item = (*item)->next;
149
-
150
- return ret;
151
- }
152
-
153
- pitem *pqueue_insert(pqueue_s *pq, pitem *item) {
154
- pitem *curr, *next;
155
-
156
- if (pq->items == NULL) {
157
- pq->items = item;
158
- return item;
159
- }
160
-
161
- for (curr = NULL, next = pq->items; next != NULL;
162
- curr = next, next = next->next) {
163
- /* we can compare 64-bit value in big-endian encoding with memcmp. */
164
- int cmp = memcmp(next->priority, item->priority, sizeof(item->priority));
165
- if (cmp > 0) {
166
- /* next > item */
167
- item->next = next;
168
-
169
- if (curr == NULL) {
170
- pq->items = item;
171
- } else {
172
- curr->next = item;
173
- }
174
-
175
- return item;
176
- } else if (cmp == 0) {
177
- /* duplicates not allowed */
178
- return NULL;
179
- }
180
- }
181
-
182
- item->next = NULL;
183
- curr->next = item;
184
-
185
- return item;
186
- }
187
-
188
-
189
- pitem *pqueue_pop(pqueue_s *pq) {
190
- pitem *item = pq->items;
191
-
192
- if (pq->items != NULL) {
193
- pq->items = pq->items->next;
194
- }
195
-
196
- return item;
197
- }
@@ -1,2272 +0,0 @@
1
- /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
- * All rights reserved.
3
- *
4
- * This package is an SSL implementation written
5
- * by Eric Young (eay@cryptsoft.com).
6
- * The implementation was written so as to conform with Netscapes SSL.
7
- *
8
- * This library is free for commercial and non-commercial use as long as
9
- * the following conditions are aheared to. The following conditions
10
- * apply to all code found in this distribution, be it the RC4, RSA,
11
- * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12
- * included with this distribution is covered by the same copyright terms
13
- * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
- *
15
- * Copyright remains Eric Young's, and as such any Copyright notices in
16
- * the code are not to be removed.
17
- * If this package is used in a product, Eric Young should be given attribution
18
- * as the author of the parts of the library used.
19
- * This can be in the form of a textual message at program startup or
20
- * in documentation (online or textual) provided with the package.
21
- *
22
- * Redistribution and use in source and binary forms, with or without
23
- * modification, are permitted provided that the following conditions
24
- * are met:
25
- * 1. Redistributions of source code must retain the copyright
26
- * notice, this list of conditions and the following disclaimer.
27
- * 2. Redistributions in binary form must reproduce the above copyright
28
- * notice, this list of conditions and the following disclaimer in the
29
- * documentation and/or other materials provided with the distribution.
30
- * 3. All advertising materials mentioning features or use of this software
31
- * must display the following acknowledgement:
32
- * "This product includes cryptographic software written by
33
- * Eric Young (eay@cryptsoft.com)"
34
- * The word 'cryptographic' can be left out if the rouines from the library
35
- * being used are not cryptographic related :-).
36
- * 4. If you include any Windows specific code (or a derivative thereof) from
37
- * the apps directory (application code) you must include an acknowledgement:
38
- * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
- *
40
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
- * SUCH DAMAGE.
51
- *
52
- * The licence and distribution terms for any publically available version or
53
- * derivative of this code cannot be changed. i.e. this code cannot simply be
54
- * copied and put under another distribution licence
55
- * [including the GNU Public Licence.]
56
- */
57
- /* ====================================================================
58
- * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59
- *
60
- * Redistribution and use in source and binary forms, with or without
61
- * modification, are permitted provided that the following conditions
62
- * are met:
63
- *
64
- * 1. Redistributions of source code must retain the above copyright
65
- * notice, this list of conditions and the following disclaimer.
66
- *
67
- * 2. Redistributions in binary form must reproduce the above copyright
68
- * notice, this list of conditions and the following disclaimer in
69
- * the documentation and/or other materials provided with the
70
- * distribution.
71
- *
72
- * 3. All advertising materials mentioning features or use of this
73
- * software must display the following acknowledgment:
74
- * "This product includes software developed by the OpenSSL Project
75
- * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76
- *
77
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78
- * endorse or promote products derived from this software without
79
- * prior written permission. For written permission, please contact
80
- * openssl-core@openssl.org.
81
- *
82
- * 5. Products derived from this software may not be called "OpenSSL"
83
- * nor may "OpenSSL" appear in their names without prior written
84
- * permission of the OpenSSL Project.
85
- *
86
- * 6. Redistributions of any form whatsoever must retain the following
87
- * acknowledgment:
88
- * "This product includes software developed by the OpenSSL Project
89
- * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90
- *
91
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102
- * OF THE POSSIBILITY OF SUCH DAMAGE.
103
- * ====================================================================
104
- *
105
- * This product includes cryptographic software written by Eric Young
106
- * (eay@cryptsoft.com). This product includes software written by Tim
107
- * Hudson (tjh@cryptsoft.com).
108
- *
109
- */
110
- /* ====================================================================
111
- * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112
- *
113
- * Portions of the attached software ("Contribution") are developed by
114
- * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115
- *
116
- * The Contribution is licensed pursuant to the OpenSSL open source
117
- * license provided above.
118
- *
119
- * ECC cipher suite support in OpenSSL originally written by
120
- * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121
- *
122
- */
123
- /* ====================================================================
124
- * Copyright 2005 Nokia. All rights reserved.
125
- *
126
- * The portions of the attached software ("Contribution") is developed by
127
- * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128
- * license.
129
- *
130
- * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131
- * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132
- * support (see RFC 4279) to OpenSSL.
133
- *
134
- * No patent licenses or other rights except those expressly stated in
135
- * the OpenSSL open source license shall be deemed granted or received
136
- * expressly, by implication, estoppel, or otherwise.
137
- *
138
- * No assurances are provided by Nokia that the Contribution does not
139
- * infringe the patent or other intellectual property rights of any third
140
- * party or that the license provides you with all the necessary rights
141
- * to make use of the Contribution.
142
- *
143
- * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144
- * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145
- * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146
- * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147
- * OTHERWISE. */
148
-
149
- #include <openssl/ssl.h>
150
-
151
- #include <assert.h>
152
- #include <stdio.h>
153
- #include <string.h>
154
-
155
- #include <openssl/bn.h>
156
- #include <openssl/buf.h>
157
- #include <openssl/bytestring.h>
158
- #include <openssl/cipher.h>
159
- #include <openssl/dh.h>
160
- #include <openssl/ec.h>
161
- #include <openssl/ecdsa.h>
162
- #include <openssl/err.h>
163
- #include <openssl/evp.h>
164
- #include <openssl/hmac.h>
165
- #include <openssl/md5.h>
166
- #include <openssl/mem.h>
167
- #include <openssl/obj.h>
168
- #include <openssl/rand.h>
169
- #include <openssl/sha.h>
170
- #include <openssl/x509.h>
171
-
172
- #include "internal.h"
173
- #include "../crypto/internal.h"
174
- #include "../crypto/dh/internal.h"
175
-
176
-
177
- int ssl3_accept(SSL *ssl) {
178
- BUF_MEM *buf = NULL;
179
- uint32_t alg_a;
180
- void (*cb)(const SSL *ssl, int type, int value) = NULL;
181
- int ret = -1;
182
- int new_state, state, skip = 0;
183
-
184
- assert(ssl->handshake_func == ssl3_accept);
185
- assert(ssl->server);
186
- assert(!SSL_IS_DTLS(ssl));
187
-
188
- ERR_clear_error();
189
- ERR_clear_system_error();
190
-
191
- if (ssl->info_callback != NULL) {
192
- cb = ssl->info_callback;
193
- } else if (ssl->ctx->info_callback != NULL) {
194
- cb = ssl->ctx->info_callback;
195
- }
196
-
197
- ssl->in_handshake++;
198
-
199
- if (ssl->cert == NULL) {
200
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
201
- return -1;
202
- }
203
-
204
- for (;;) {
205
- state = ssl->state;
206
-
207
- switch (ssl->state) {
208
- case SSL_ST_ACCEPT:
209
- if (cb != NULL) {
210
- cb(ssl, SSL_CB_HANDSHAKE_START, 1);
211
- }
212
-
213
- if (ssl->init_buf == NULL) {
214
- buf = BUF_MEM_new();
215
- if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
216
- ret = -1;
217
- goto end;
218
- }
219
- ssl->init_buf = buf;
220
- buf = NULL;
221
- }
222
- ssl->init_num = 0;
223
-
224
- /* Enable a write buffer. This groups handshake messages within a flight
225
- * into a single write. */
226
- if (!ssl_init_wbio_buffer(ssl, 1)) {
227
- ret = -1;
228
- goto end;
229
- }
230
-
231
- if (!ssl3_init_handshake_buffer(ssl)) {
232
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
233
- ret = -1;
234
- goto end;
235
- }
236
-
237
- if (!ssl->s3->have_version) {
238
- ssl->state = SSL3_ST_SR_INITIAL_BYTES;
239
- } else {
240
- ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
241
- }
242
- break;
243
-
244
- case SSL3_ST_SR_INITIAL_BYTES:
245
- ret = ssl3_get_initial_bytes(ssl);
246
- if (ret <= 0) {
247
- goto end;
248
- }
249
- /* ssl3_get_initial_bytes sets ssl->state to one of
250
- * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
251
- break;
252
-
253
- case SSL3_ST_SR_V2_CLIENT_HELLO:
254
- ret = ssl3_get_v2_client_hello(ssl);
255
- if (ret <= 0) {
256
- goto end;
257
- }
258
- ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
259
- break;
260
-
261
- case SSL3_ST_SR_CLNT_HELLO_A:
262
- case SSL3_ST_SR_CLNT_HELLO_B:
263
- case SSL3_ST_SR_CLNT_HELLO_C:
264
- case SSL3_ST_SR_CLNT_HELLO_D:
265
- ssl->shutdown = 0;
266
- ret = ssl3_get_client_hello(ssl);
267
- if (ret <= 0) {
268
- goto end;
269
- }
270
- ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
271
- ssl->init_num = 0;
272
- break;
273
-
274
- case SSL3_ST_SW_SRVR_HELLO_A:
275
- case SSL3_ST_SW_SRVR_HELLO_B:
276
- ret = ssl3_send_server_hello(ssl);
277
- if (ret <= 0) {
278
- goto end;
279
- }
280
- if (ssl->hit) {
281
- if (ssl->tlsext_ticket_expected) {
282
- ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
283
- } else {
284
- ssl->state = SSL3_ST_SW_CHANGE_A;
285
- }
286
- } else {
287
- ssl->state = SSL3_ST_SW_CERT_A;
288
- }
289
- ssl->init_num = 0;
290
- break;
291
-
292
- case SSL3_ST_SW_CERT_A:
293
- case SSL3_ST_SW_CERT_B:
294
- if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
295
- ret = ssl3_send_server_certificate(ssl);
296
- if (ret <= 0) {
297
- goto end;
298
- }
299
- if (ssl->s3->tmp.certificate_status_expected) {
300
- ssl->state = SSL3_ST_SW_CERT_STATUS_A;
301
- } else {
302
- ssl->state = SSL3_ST_SW_KEY_EXCH_A;
303
- }
304
- } else {
305
- skip = 1;
306
- ssl->state = SSL3_ST_SW_KEY_EXCH_A;
307
- }
308
- ssl->init_num = 0;
309
- break;
310
-
311
- case SSL3_ST_SW_CERT_STATUS_A:
312
- case SSL3_ST_SW_CERT_STATUS_B:
313
- ret = ssl3_send_certificate_status(ssl);
314
- if (ret <= 0) {
315
- goto end;
316
- }
317
- ssl->state = SSL3_ST_SW_KEY_EXCH_A;
318
- ssl->init_num = 0;
319
- break;
320
-
321
- case SSL3_ST_SW_KEY_EXCH_A:
322
- case SSL3_ST_SW_KEY_EXCH_B:
323
- case SSL3_ST_SW_KEY_EXCH_C:
324
- alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
325
-
326
- /* Send a ServerKeyExchange message if:
327
- * - The key exchange is ephemeral or anonymous
328
- * Diffie-Hellman.
329
- * - There is a PSK identity hint.
330
- *
331
- * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix
332
- * this. In the meantime, keep them in sync. */
333
- if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
334
- ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
335
- ret = ssl3_send_server_key_exchange(ssl);
336
- if (ret <= 0) {
337
- goto end;
338
- }
339
- } else {
340
- skip = 1;
341
- }
342
-
343
- ssl->state = SSL3_ST_SW_CERT_REQ_A;
344
- ssl->init_num = 0;
345
- break;
346
-
347
- case SSL3_ST_SW_CERT_REQ_A:
348
- case SSL3_ST_SW_CERT_REQ_B:
349
- if (ssl->s3->tmp.cert_request) {
350
- ret = ssl3_send_certificate_request(ssl);
351
- if (ret <= 0) {
352
- goto end;
353
- }
354
- } else {
355
- skip = 1;
356
- }
357
- ssl->state = SSL3_ST_SW_SRVR_DONE_A;
358
- ssl->init_num = 0;
359
- break;
360
-
361
- case SSL3_ST_SW_SRVR_DONE_A:
362
- case SSL3_ST_SW_SRVR_DONE_B:
363
- ret = ssl3_send_server_done(ssl);
364
- if (ret <= 0) {
365
- goto end;
366
- }
367
- ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
368
- ssl->state = SSL3_ST_SW_FLUSH;
369
- ssl->init_num = 0;
370
- break;
371
-
372
- case SSL3_ST_SW_FLUSH:
373
- /* This code originally checked to see if any data was pending using
374
- * BIO_CTRL_INFO and then flushed. This caused problems as documented
375
- * in PR#1939. The proposed fix doesn't completely resolve this issue
376
- * as buggy implementations of BIO_CTRL_PENDING still exist. So instead
377
- * we just flush unconditionally. */
378
- ssl->rwstate = SSL_WRITING;
379
- if (BIO_flush(ssl->wbio) <= 0) {
380
- ret = -1;
381
- goto end;
382
- }
383
- ssl->rwstate = SSL_NOTHING;
384
-
385
- ssl->state = ssl->s3->tmp.next_state;
386
- break;
387
-
388
- case SSL3_ST_SR_CERT_A:
389
- case SSL3_ST_SR_CERT_B:
390
- if (ssl->s3->tmp.cert_request) {
391
- ret = ssl3_get_client_certificate(ssl);
392
- if (ret <= 0) {
393
- goto end;
394
- }
395
- }
396
- ssl->init_num = 0;
397
- ssl->state = SSL3_ST_SR_KEY_EXCH_A;
398
- break;
399
-
400
- case SSL3_ST_SR_KEY_EXCH_A:
401
- case SSL3_ST_SR_KEY_EXCH_B:
402
- case SSL3_ST_SR_KEY_EXCH_C:
403
- ret = ssl3_get_client_key_exchange(ssl);
404
- if (ret <= 0) {
405
- goto end;
406
- }
407
- ssl->state = SSL3_ST_SR_CERT_VRFY_A;
408
- ssl->init_num = 0;
409
- break;
410
-
411
- case SSL3_ST_SR_CERT_VRFY_A:
412
- case SSL3_ST_SR_CERT_VRFY_B:
413
- ret = ssl3_get_cert_verify(ssl);
414
- if (ret <= 0) {
415
- goto end;
416
- }
417
-
418
- ssl->state = SSL3_ST_SR_CHANGE;
419
- ssl->init_num = 0;
420
- break;
421
-
422
- case SSL3_ST_SR_CHANGE:
423
- ret = ssl->method->ssl_read_change_cipher_spec(ssl);
424
- if (ret <= 0) {
425
- goto end;
426
- }
427
-
428
- if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
429
- ret = -1;
430
- goto end;
431
- }
432
-
433
- if (ssl->s3->next_proto_neg_seen) {
434
- ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
435
- } else if (ssl->s3->tlsext_channel_id_valid) {
436
- ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
437
- } else {
438
- ssl->state = SSL3_ST_SR_FINISHED_A;
439
- }
440
- break;
441
-
442
- case SSL3_ST_SR_NEXT_PROTO_A:
443
- case SSL3_ST_SR_NEXT_PROTO_B:
444
- ret = ssl3_get_next_proto(ssl);
445
- if (ret <= 0) {
446
- goto end;
447
- }
448
- ssl->init_num = 0;
449
- if (ssl->s3->tlsext_channel_id_valid) {
450
- ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
451
- } else {
452
- ssl->state = SSL3_ST_SR_FINISHED_A;
453
- }
454
- break;
455
-
456
- case SSL3_ST_SR_CHANNEL_ID_A:
457
- case SSL3_ST_SR_CHANNEL_ID_B:
458
- ret = ssl3_get_channel_id(ssl);
459
- if (ret <= 0) {
460
- goto end;
461
- }
462
- ssl->init_num = 0;
463
- ssl->state = SSL3_ST_SR_FINISHED_A;
464
- break;
465
-
466
- case SSL3_ST_SR_FINISHED_A:
467
- case SSL3_ST_SR_FINISHED_B:
468
- ret = ssl3_get_finished(ssl, SSL3_ST_SR_FINISHED_A,
469
- SSL3_ST_SR_FINISHED_B);
470
- if (ret <= 0) {
471
- goto end;
472
- }
473
-
474
- if (ssl->hit) {
475
- ssl->state = SSL_ST_OK;
476
- } else if (ssl->tlsext_ticket_expected) {
477
- ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
478
- } else {
479
- ssl->state = SSL3_ST_SW_CHANGE_A;
480
- }
481
- /* If this is a full handshake with ChannelID then record the hashshake
482
- * hashes in |ssl->session| in case we need them to verify a ChannelID
483
- * signature on a resumption of this session in the future. */
484
- if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
485
- ret = tls1_record_handshake_hashes_for_channel_id(ssl);
486
- if (ret <= 0) {
487
- goto end;
488
- }
489
- }
490
- ssl->init_num = 0;
491
- break;
492
-
493
- case SSL3_ST_SW_SESSION_TICKET_A:
494
- case SSL3_ST_SW_SESSION_TICKET_B:
495
- ret = ssl3_send_new_session_ticket(ssl);
496
- if (ret <= 0) {
497
- goto end;
498
- }
499
- ssl->state = SSL3_ST_SW_CHANGE_A;
500
- ssl->init_num = 0;
501
- break;
502
-
503
- case SSL3_ST_SW_CHANGE_A:
504
- case SSL3_ST_SW_CHANGE_B:
505
- ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
506
- SSL3_ST_SW_CHANGE_B);
507
- if (ret <= 0) {
508
- goto end;
509
- }
510
- ssl->state = SSL3_ST_SW_FINISHED_A;
511
- ssl->init_num = 0;
512
-
513
- if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
514
- ret = -1;
515
- goto end;
516
- }
517
- break;
518
-
519
- case SSL3_ST_SW_FINISHED_A:
520
- case SSL3_ST_SW_FINISHED_B:
521
- ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
522
- SSL3_ST_SW_FINISHED_B);
523
- if (ret <= 0) {
524
- goto end;
525
- }
526
- ssl->state = SSL3_ST_SW_FLUSH;
527
- if (ssl->hit) {
528
- ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
529
- } else {
530
- ssl->s3->tmp.next_state = SSL_ST_OK;
531
- }
532
- ssl->init_num = 0;
533
- break;
534
-
535
- case SSL_ST_OK:
536
- /* clean a few things up */
537
- ssl3_cleanup_key_block(ssl);
538
-
539
- BUF_MEM_free(ssl->init_buf);
540
- ssl->init_buf = NULL;
541
-
542
- /* remove buffering on output */
543
- ssl_free_wbio_buffer(ssl);
544
-
545
- ssl->init_num = 0;
546
-
547
- /* If we aren't retaining peer certificates then we can discard it
548
- * now. */
549
- if (ssl->ctx->retain_only_sha256_of_client_certs) {
550
- X509_free(ssl->session->peer);
551
- ssl->session->peer = NULL;
552
- sk_X509_pop_free(ssl->session->cert_chain, X509_free);
553
- ssl->session->cert_chain = NULL;
554
- }
555
-
556
- ssl->s3->initial_handshake_complete = 1;
557
-
558
- ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
559
-
560
- if (cb != NULL) {
561
- cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
562
- }
563
-
564
- ret = 1;
565
- goto end;
566
-
567
- default:
568
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
569
- ret = -1;
570
- goto end;
571
- }
572
-
573
- if (!ssl->s3->tmp.reuse_message && !skip && cb != NULL &&
574
- ssl->state != state) {
575
- new_state = ssl->state;
576
- ssl->state = state;
577
- cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
578
- ssl->state = new_state;
579
- }
580
- skip = 0;
581
- }
582
-
583
- end:
584
- ssl->in_handshake--;
585
- BUF_MEM_free(buf);
586
- if (cb != NULL) {
587
- cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
588
- }
589
- return ret;
590
- }
591
-
592
- int ssl3_get_initial_bytes(SSL *ssl) {
593
- /* Read the first 5 bytes, the size of the TLS record header. This is
594
- * sufficient to detect a V2ClientHello and ensures that we never read beyond
595
- * the first record. */
596
- int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
597
- if (ret <= 0) {
598
- return ret;
599
- }
600
- assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH);
601
- const uint8_t *p = ssl_read_buffer(ssl);
602
-
603
- /* Some dedicated error codes for protocol mixups should the application wish
604
- * to interpret them differently. (These do not overlap with ClientHello or
605
- * V2ClientHello.) */
606
- if (strncmp("GET ", (const char *)p, 4) == 0 ||
607
- strncmp("POST ", (const char *)p, 5) == 0 ||
608
- strncmp("HEAD ", (const char *)p, 5) == 0 ||
609
- strncmp("PUT ", (const char *)p, 4) == 0) {
610
- OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
611
- return -1;
612
- }
613
- if (strncmp("CONNE", (const char *)p, 5) == 0) {
614
- OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
615
- return -1;
616
- }
617
-
618
- /* Determine if this is a V2ClientHello. */
619
- if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
620
- p[3] >= SSL3_VERSION_MAJOR) {
621
- /* This is a V2ClientHello. */
622
- ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO;
623
- return 1;
624
- }
625
-
626
- /* Fall through to the standard logic. */
627
- ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
628
- return 1;
629
- }
630
-
631
- int ssl3_get_v2_client_hello(SSL *ssl) {
632
- const uint8_t *p;
633
- int ret;
634
- CBS v2_client_hello, cipher_specs, session_id, challenge;
635
- size_t msg_length, rand_len, len;
636
- uint8_t msg_type;
637
- uint16_t version, cipher_spec_length, session_id_length, challenge_length;
638
- CBB client_hello, hello_body, cipher_suites;
639
- uint8_t random[SSL3_RANDOM_SIZE];
640
-
641
- /* Determine the length of the V2ClientHello. */
642
- assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH);
643
- p = ssl_read_buffer(ssl);
644
- msg_length = ((p[0] & 0x7f) << 8) | p[1];
645
- if (msg_length > (1024 * 4)) {
646
- OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
647
- return -1;
648
- }
649
- if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
650
- /* Reject lengths that are too short early. We have already read
651
- * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
652
- * (invalid) V2ClientHello which would be shorter than that. */
653
- OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
654
- return -1;
655
- }
656
-
657
- /* Read the remainder of the V2ClientHello. */
658
- ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
659
- if (ret <= 0) {
660
- return ret;
661
- }
662
- assert(ssl_read_buffer_len(ssl) == msg_length + 2);
663
- CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
664
-
665
- /* The V2ClientHello without the length is incorporated into the handshake
666
- * hash. */
667
- if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
668
- CBS_len(&v2_client_hello))) {
669
- return -1;
670
- }
671
- if (ssl->msg_callback) {
672
- ssl->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello),
673
- CBS_len(&v2_client_hello), ssl, ssl->msg_callback_arg);
674
- }
675
-
676
- if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
677
- !CBS_get_u16(&v2_client_hello, &version) ||
678
- !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
679
- !CBS_get_u16(&v2_client_hello, &session_id_length) ||
680
- !CBS_get_u16(&v2_client_hello, &challenge_length) ||
681
- !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
682
- !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
683
- !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
684
- CBS_len(&v2_client_hello) != 0) {
685
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
686
- return -1;
687
- }
688
-
689
- /* msg_type has already been checked. */
690
- assert(msg_type == SSL2_MT_CLIENT_HELLO);
691
-
692
- /* The client_random is the V2ClientHello challenge. Truncate or
693
- * left-pad with zeros as needed. */
694
- memset(random, 0, SSL3_RANDOM_SIZE);
695
- rand_len = CBS_len(&challenge);
696
- if (rand_len > SSL3_RANDOM_SIZE) {
697
- rand_len = SSL3_RANDOM_SIZE;
698
- }
699
- memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
700
- rand_len);
701
-
702
- /* Write out an equivalent SSLv3 ClientHello. */
703
- CBB_zero(&client_hello);
704
- if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
705
- ssl->init_buf->max) ||
706
- !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
707
- !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
708
- !CBB_add_u16(&hello_body, version) ||
709
- !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
710
- /* No session id. */
711
- !CBB_add_u8(&hello_body, 0) ||
712
- !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
713
- CBB_cleanup(&client_hello);
714
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
715
- return -1;
716
- }
717
-
718
- /* Copy the cipher suites. */
719
- while (CBS_len(&cipher_specs) > 0) {
720
- uint32_t cipher_spec;
721
- if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
722
- CBB_cleanup(&client_hello);
723
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
724
- return -1;
725
- }
726
-
727
- /* Skip SSLv2 ciphers. */
728
- if ((cipher_spec & 0xff0000) != 0) {
729
- continue;
730
- }
731
- if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
732
- CBB_cleanup(&client_hello);
733
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
734
- return -1;
735
- }
736
- }
737
-
738
- /* Add the null compression scheme and finish. */
739
- if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
740
- !CBB_finish(&client_hello, NULL, &len)) {
741
- CBB_cleanup(&client_hello);
742
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
743
- return -1;
744
- }
745
-
746
- /* Mark the message for "re"-use by the version-specific method. */
747
- ssl->s3->tmp.reuse_message = 1;
748
- ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
749
- /* The handshake message header is 4 bytes. */
750
- ssl->s3->tmp.message_size = len - 4;
751
-
752
- /* Consume and discard the V2ClientHello. */
753
- ssl_read_buffer_consume(ssl, 2 + msg_length);
754
- ssl_read_buffer_discard(ssl);
755
-
756
- return 1;
757
- }
758
-
759
- int ssl3_get_client_hello(SSL *ssl) {
760
- int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
761
- long n;
762
- const SSL_CIPHER *c;
763
- STACK_OF(SSL_CIPHER) *ciphers = NULL;
764
- struct ssl_early_callback_ctx early_ctx;
765
- CBS client_hello;
766
- uint16_t client_version;
767
- CBS client_random, session_id, cipher_suites, compression_methods;
768
- SSL_SESSION *session = NULL;
769
-
770
- /* We do this so that we will respond with our native type. If we are TLSv1
771
- * and we get SSLv3, we will respond with TLSv1, This down switching should
772
- * be handled by a different method. If we are SSLv3, we will respond with
773
- * SSLv3, even if prompted with TLSv1. */
774
- switch (ssl->state) {
775
- case SSL3_ST_SR_CLNT_HELLO_A:
776
- case SSL3_ST_SR_CLNT_HELLO_B:
777
- n = ssl->method->ssl_get_message(
778
- ssl, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B,
779
- SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH,
780
- ssl_hash_message, &ok);
781
-
782
- if (!ok) {
783
- return n;
784
- }
785
-
786
- ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
787
- /* fallthrough */
788
- case SSL3_ST_SR_CLNT_HELLO_C:
789
- case SSL3_ST_SR_CLNT_HELLO_D:
790
- /* We have previously parsed the ClientHello message, and can't call
791
- * ssl_get_message again without hashing the message into the Finished
792
- * digest again. */
793
- n = ssl->init_num;
794
-
795
- memset(&early_ctx, 0, sizeof(early_ctx));
796
- early_ctx.ssl = ssl;
797
- early_ctx.client_hello = ssl->init_msg;
798
- early_ctx.client_hello_len = n;
799
- if (!ssl_early_callback_init(&early_ctx)) {
800
- al = SSL_AD_DECODE_ERROR;
801
- OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
802
- goto f_err;
803
- }
804
-
805
- if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C &&
806
- ssl->ctx->select_certificate_cb != NULL) {
807
- ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
808
- switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
809
- case 0:
810
- ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
811
- goto err;
812
-
813
- case -1:
814
- /* Connection rejected. */
815
- al = SSL_AD_ACCESS_DENIED;
816
- OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
817
- goto f_err;
818
-
819
- default:
820
- /* fallthrough */;
821
- }
822
- }
823
- ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
824
- break;
825
-
826
- default:
827
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
828
- return -1;
829
- }
830
-
831
- CBS_init(&client_hello, ssl->init_msg, n);
832
- if (!CBS_get_u16(&client_hello, &client_version) ||
833
- !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
834
- !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
835
- CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
836
- al = SSL_AD_DECODE_ERROR;
837
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
838
- goto f_err;
839
- }
840
-
841
- /* use version from inside client hello, not from record header (may differ:
842
- * see RFC 2246, Appendix E, second paragraph) */
843
- ssl->client_version = client_version;
844
-
845
- /* Load the client random. */
846
- memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
847
-
848
- if (SSL_IS_DTLS(ssl)) {
849
- CBS cookie;
850
-
851
- if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
852
- CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
853
- al = SSL_AD_DECODE_ERROR;
854
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
855
- goto f_err;
856
- }
857
- }
858
-
859
- /* Note: This codepath may run twice if |ssl_get_prev_session| completes
860
- * asynchronously.
861
- *
862
- * TODO(davidben): Clean up the order of events around ClientHello
863
- * processing. */
864
- if (!ssl->s3->have_version) {
865
- /* Select version to use */
866
- uint16_t version = ssl3_get_mutual_version(ssl, client_version);
867
- if (version == 0) {
868
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
869
- ssl->version = ssl->client_version;
870
- al = SSL_AD_PROTOCOL_VERSION;
871
- goto f_err;
872
- }
873
- ssl->version = version;
874
- ssl->s3->enc_method = ssl3_get_enc_method(version);
875
- assert(ssl->s3->enc_method != NULL);
876
- /* At this point, the connection's version is known and |ssl->version| is
877
- * fixed. Begin enforcing the record-layer version. */
878
- ssl->s3->have_version = 1;
879
- } else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version)
880
- : (ssl->client_version < ssl->version)) {
881
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
882
- al = SSL_AD_PROTOCOL_VERSION;
883
- goto f_err;
884
- }
885
-
886
- ssl->hit = 0;
887
- int send_new_ticket = 0;
888
- switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
889
- case ssl_session_success:
890
- break;
891
- case ssl_session_error:
892
- goto err;
893
- case ssl_session_retry:
894
- ssl->rwstate = SSL_PENDING_SESSION;
895
- goto err;
896
- }
897
- ssl->tlsext_ticket_expected = send_new_ticket;
898
-
899
- /* The EMS state is needed when making the resumption decision, but
900
- * extensions are not normally parsed until later. This detects the EMS
901
- * extension for the resumption decision and it's checked against the result
902
- * of the normal parse later in this function. */
903
- const uint8_t *ems_data;
904
- size_t ems_len;
905
- int have_extended_master_secret =
906
- ssl->version != SSL3_VERSION &&
907
- SSL_early_callback_ctx_extension_get(&early_ctx,
908
- TLSEXT_TYPE_extended_master_secret,
909
- &ems_data, &ems_len) &&
910
- ems_len == 0;
911
-
912
- if (session != NULL) {
913
- if (session->extended_master_secret &&
914
- !have_extended_master_secret) {
915
- /* A ClientHello without EMS that attempts to resume a session with EMS
916
- * is fatal to the connection. */
917
- al = SSL_AD_HANDSHAKE_FAILURE;
918
- OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
919
- goto f_err;
920
- }
921
-
922
- ssl->hit =
923
- /* Only resume if the session's version matches the negotiated version:
924
- * most clients do not accept a mismatch. */
925
- ssl->version == session->ssl_version &&
926
- /* If the client offers the EMS extension, but the previous session
927
- * didn't use it, then negotiate a new session. */
928
- have_extended_master_secret == session->extended_master_secret;
929
- }
930
-
931
- if (ssl->hit) {
932
- /* Use the new session. */
933
- SSL_SESSION_free(ssl->session);
934
- ssl->session = session;
935
- session = NULL;
936
-
937
- ssl->verify_result = ssl->session->verify_result;
938
- } else {
939
- if (!ssl_get_new_session(ssl, 1 /* server */)) {
940
- goto err;
941
- }
942
-
943
- /* Clear the session ID if we want the session to be single-use. */
944
- if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
945
- ssl->session->session_id_length = 0;
946
- }
947
- }
948
-
949
- if (ssl->ctx->dos_protection_cb != NULL &&
950
- ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
951
- /* Connection rejected for DOS reasons. */
952
- al = SSL_AD_ACCESS_DENIED;
953
- OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
954
- goto f_err;
955
- }
956
-
957
- if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
958
- CBS_len(&cipher_suites) == 0 ||
959
- CBS_len(&cipher_suites) % 2 != 0 ||
960
- !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
961
- CBS_len(&compression_methods) == 0) {
962
- al = SSL_AD_DECODE_ERROR;
963
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
964
- goto f_err;
965
- }
966
-
967
- ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites);
968
- if (ciphers == NULL) {
969
- goto err;
970
- }
971
-
972
- /* If it is a hit, check that the cipher is in the list. */
973
- if (ssl->hit) {
974
- size_t j;
975
- int found_cipher = 0;
976
- uint32_t id = ssl->session->cipher->id;
977
-
978
- for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
979
- c = sk_SSL_CIPHER_value(ciphers, j);
980
- if (c->id == id) {
981
- found_cipher = 1;
982
- break;
983
- }
984
- }
985
-
986
- if (!found_cipher) {
987
- /* we need to have the cipher in the cipher list if we are asked to reuse
988
- * it */
989
- al = SSL_AD_ILLEGAL_PARAMETER;
990
- OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
991
- goto f_err;
992
- }
993
- }
994
-
995
- /* Only null compression is supported. */
996
- if (memchr(CBS_data(&compression_methods), 0,
997
- CBS_len(&compression_methods)) == NULL) {
998
- al = SSL_AD_ILLEGAL_PARAMETER;
999
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
1000
- goto f_err;
1001
- }
1002
-
1003
- /* TLS extensions. */
1004
- if (ssl->version >= SSL3_VERSION &&
1005
- !ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
1006
- OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
1007
- goto err;
1008
- }
1009
-
1010
- /* There should be nothing left over in the record. */
1011
- if (CBS_len(&client_hello) != 0) {
1012
- /* wrong packet length */
1013
- al = SSL_AD_DECODE_ERROR;
1014
- OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
1015
- goto f_err;
1016
- }
1017
-
1018
- if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
1019
- al = SSL_AD_INTERNAL_ERROR;
1020
- OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
1021
- goto f_err;
1022
- }
1023
-
1024
- /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
1025
- if (!ssl->hit) {
1026
- if (ciphers == NULL) {
1027
- al = SSL_AD_ILLEGAL_PARAMETER;
1028
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
1029
- goto f_err;
1030
- }
1031
-
1032
- /* Let cert callback update server certificates if required */
1033
- if (ssl->cert->cert_cb) {
1034
- int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1035
- if (rv == 0) {
1036
- al = SSL_AD_INTERNAL_ERROR;
1037
- OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1038
- goto f_err;
1039
- }
1040
- if (rv < 0) {
1041
- ssl->rwstate = SSL_X509_LOOKUP;
1042
- goto err;
1043
- }
1044
- ssl->rwstate = SSL_NOTHING;
1045
- }
1046
- c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
1047
-
1048
- if (c == NULL) {
1049
- al = SSL_AD_HANDSHAKE_FAILURE;
1050
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
1051
- goto f_err;
1052
- }
1053
- ssl->session->cipher = c;
1054
- ssl->s3->tmp.new_cipher = c;
1055
-
1056
- /* Determine whether to request a client certificate. */
1057
- ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
1058
- /* Only request a certificate if Channel ID isn't negotiated. */
1059
- if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1060
- ssl->s3->tlsext_channel_id_valid) {
1061
- ssl->s3->tmp.cert_request = 0;
1062
- }
1063
- /* Plain PSK forbids Certificate and CertificateRequest. */
1064
- if (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) {
1065
- ssl->s3->tmp.cert_request = 0;
1066
- }
1067
- } else {
1068
- /* Session-id reuse */
1069
- ssl->s3->tmp.new_cipher = ssl->session->cipher;
1070
- ssl->s3->tmp.cert_request = 0;
1071
- }
1072
-
1073
- /* Now that the cipher is known, initialize the handshake hash. */
1074
- if (!ssl3_init_handshake_hash(ssl)) {
1075
- goto f_err;
1076
- }
1077
-
1078
- /* In TLS 1.2, client authentication requires hashing the handshake transcript
1079
- * under a different hash. Otherwise, release the handshake buffer. */
1080
- if (!ssl->s3->tmp.cert_request ||
1081
- ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
1082
- ssl3_free_handshake_buffer(ssl);
1083
- }
1084
-
1085
- /* we now have the following setup;
1086
- * client_random
1087
- * cipher_list - our prefered list of ciphers
1088
- * ciphers - the clients prefered list of ciphers
1089
- * compression - basically ignored right now
1090
- * ssl version is set - sslv3
1091
- * ssl->session - The ssl session has been setup.
1092
- * ssl->hit - session reuse flag
1093
- * ssl->tmp.new_cipher - the new cipher to use. */
1094
-
1095
- ret = 1;
1096
-
1097
- if (0) {
1098
- f_err:
1099
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1100
- }
1101
-
1102
- err:
1103
- sk_SSL_CIPHER_free(ciphers);
1104
- SSL_SESSION_free(session);
1105
- return ret;
1106
- }
1107
-
1108
- int ssl3_send_server_hello(SSL *ssl) {
1109
- if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
1110
- return ssl_do_write(ssl);
1111
- }
1112
-
1113
- assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1114
-
1115
- /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1116
- * known attack while we fix ChannelID itself. */
1117
- if (ssl->s3->tlsext_channel_id_valid &&
1118
- (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1119
- ssl->s3->tlsext_channel_id_valid = 0;
1120
- }
1121
-
1122
- /* If this is a resumption and the original handshake didn't support
1123
- * ChannelID then we didn't record the original handshake hashes in the
1124
- * session and so cannot resume with ChannelIDs. */
1125
- if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
1126
- ssl->s3->tlsext_channel_id_valid = 0;
1127
- }
1128
-
1129
- if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
1130
- 1 /* server */)) {
1131
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1132
- return -1;
1133
- }
1134
-
1135
- CBB cbb, session_id;
1136
- size_t length;
1137
- CBB_zero(&cbb);
1138
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1139
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1140
- !CBB_add_u16(&cbb, ssl->version) ||
1141
- !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1142
- !CBB_add_u8_length_prefixed(&cbb, &session_id) ||
1143
- !CBB_add_bytes(&session_id, ssl->session->session_id,
1144
- ssl->session->session_id_length) ||
1145
- !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1146
- !CBB_add_u8(&cbb, 0 /* no compression */) ||
1147
- !ssl_add_serverhello_tlsext(ssl, &cbb) ||
1148
- !CBB_finish(&cbb, NULL, &length) ||
1149
- !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
1150
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1151
- CBB_cleanup(&cbb);
1152
- return -1;
1153
- }
1154
-
1155
- ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
1156
- return ssl_do_write(ssl);
1157
- }
1158
-
1159
- int ssl3_send_certificate_status(SSL *ssl) {
1160
- if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
1161
- CBB out, ocsp_response;
1162
- size_t length;
1163
-
1164
- CBB_zero(&out);
1165
- if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
1166
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1167
- !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) ||
1168
- !CBB_add_u24_length_prefixed(&out, &ocsp_response) ||
1169
- !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1170
- ssl->ctx->ocsp_response_length) ||
1171
- !CBB_finish(&out, NULL, &length) ||
1172
- !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
1173
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1174
- CBB_cleanup(&out);
1175
- return -1;
1176
- }
1177
-
1178
- ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1179
- }
1180
-
1181
- /* SSL3_ST_SW_CERT_STATUS_B */
1182
- return ssl_do_write(ssl);
1183
- }
1184
-
1185
- int ssl3_send_server_done(SSL *ssl) {
1186
- if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) {
1187
- if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_DONE, 0)) {
1188
- return -1;
1189
- }
1190
- ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1191
- }
1192
-
1193
- /* SSL3_ST_SW_SRVR_DONE_B */
1194
- return ssl_do_write(ssl);
1195
- }
1196
-
1197
- int ssl3_send_server_key_exchange(SSL *ssl) {
1198
- if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
1199
- return ssl_do_write(ssl);
1200
- }
1201
-
1202
- CBB cbb, child;
1203
- if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1204
- ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1205
- goto err;
1206
- }
1207
-
1208
- if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1209
- /* This is the first iteration, so write parameters. */
1210
- uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1211
- uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1212
-
1213
- /* PSK ciphers begin with an identity hint. */
1214
- if (alg_a & SSL_aPSK) {
1215
- size_t len =
1216
- (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1217
- if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1218
- !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1219
- len)) {
1220
- goto err;
1221
- }
1222
- }
1223
-
1224
- if (alg_k & SSL_kDHE) {
1225
- /* Determine the group to use. */
1226
- DH *params = ssl->cert->dh_tmp;
1227
- if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1228
- params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
1229
- }
1230
- if (params == NULL) {
1231
- OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
1232
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1233
- goto err;
1234
- }
1235
- ssl->session->key_exchange_info = DH_num_bits(params);
1236
-
1237
- /* Set up DH, generate a key, and emit the public half. */
1238
- DH *dh = DHparams_dup(params);
1239
- if (dh == NULL) {
1240
- goto err;
1241
- }
1242
-
1243
- SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
1244
- if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1245
- !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
1246
- !CBB_add_u16_length_prefixed(&cbb, &child) ||
1247
- !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
1248
- !CBB_add_u16_length_prefixed(&cbb, &child) ||
1249
- !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child)) {
1250
- goto err;
1251
- }
1252
- } else if (alg_k & SSL_kECDHE) {
1253
- /* Determine the curve to use. */
1254
- uint16_t curve_id;
1255
- if (!tls1_get_shared_curve(ssl, &curve_id)) {
1256
- OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
1257
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1258
- goto err;
1259
- }
1260
- ssl->session->key_exchange_info = curve_id;
1261
-
1262
- /* Set up ECDH, generate a key, and emit the public half. */
1263
- if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) ||
1264
- !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
1265
- !CBB_add_u16(&cbb, curve_id) ||
1266
- !CBB_add_u8_length_prefixed(&cbb, &child) ||
1267
- !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child)) {
1268
- goto err;
1269
- }
1270
- } else {
1271
- assert(alg_k & SSL_kPSK);
1272
- }
1273
-
1274
- /* Otherwise, restore |cbb| from the previous iteration.
1275
- * TODO(davidben): When |ssl->init_buf| is gone, come up with a simpler
1276
- * pattern. Probably keep the |CBB| around in the handshake state. */
1277
- } else if (!CBB_did_write(&cbb, ssl->init_num - SSL_HM_HEADER_LENGTH(ssl))) {
1278
- goto err;
1279
- }
1280
-
1281
- /* Add a signature. */
1282
- if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
1283
- if (!ssl_has_private_key(ssl)) {
1284
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1285
- goto err;
1286
- }
1287
-
1288
- const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1289
- size_t sig_len;
1290
- enum ssl_private_key_result_t sign_result;
1291
- if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1292
- /* This is the first iteration, so set up the signature. Sample the
1293
- * parameter length before adding a signature algorithm. */
1294
- if (!CBB_flush(&cbb)) {
1295
- goto err;
1296
- }
1297
- size_t params_len = CBB_len(&cbb);
1298
-
1299
- /* Determine signature algorithm. */
1300
- const EVP_MD *md;
1301
- if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1302
- md = tls1_choose_signing_digest(ssl);
1303
- if (!tls12_add_sigandhash(ssl, &cbb, md)) {
1304
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1305
- ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1306
- goto err;
1307
- }
1308
- } else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
1309
- md = EVP_md5_sha1();
1310
- } else {
1311
- md = EVP_sha1();
1312
- }
1313
-
1314
- /* Compute the digest and sign it. */
1315
- uint8_t digest[EVP_MAX_MD_SIZE];
1316
- unsigned digest_len = 0;
1317
- EVP_MD_CTX md_ctx;
1318
- EVP_MD_CTX_init(&md_ctx);
1319
- int digest_ret =
1320
- EVP_DigestInit_ex(&md_ctx, md, NULL) &&
1321
- EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) &&
1322
- EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) &&
1323
- EVP_DigestUpdate(&md_ctx, CBB_data(&cbb), params_len) &&
1324
- EVP_DigestFinal_ex(&md_ctx, digest, &digest_len);
1325
- EVP_MD_CTX_cleanup(&md_ctx);
1326
- uint8_t *ptr;
1327
- if (!digest_ret ||
1328
- !CBB_add_u16_length_prefixed(&cbb, &child) ||
1329
- !CBB_reserve(&child, &ptr, max_sig_len)) {
1330
- goto err;
1331
- }
1332
- sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1333
- digest, digest_len);
1334
- } else {
1335
- assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
1336
-
1337
- /* Retry the signature. */
1338
- uint8_t *ptr;
1339
- if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1340
- !CBB_reserve(&child, &ptr, max_sig_len)) {
1341
- goto err;
1342
- }
1343
- sign_result =
1344
- ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
1345
- }
1346
-
1347
- switch (sign_result) {
1348
- case ssl_private_key_success:
1349
- ssl->rwstate = SSL_NOTHING;
1350
- if (!CBB_did_write(&child, sig_len)) {
1351
- goto err;
1352
- }
1353
- break;
1354
- case ssl_private_key_failure:
1355
- ssl->rwstate = SSL_NOTHING;
1356
- goto err;
1357
- case ssl_private_key_retry:
1358
- /* Discard the unfinished signature and save the state of |cbb| for the
1359
- * next iteration. */
1360
- CBB_discard_child(&cbb);
1361
- ssl->init_num = SSL_HM_HEADER_LENGTH(ssl) + CBB_len(&cbb);
1362
- ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1363
- ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1364
- goto err;
1365
- }
1366
- }
1367
-
1368
- size_t length;
1369
- if (!CBB_finish(&cbb, NULL, &length) ||
1370
- !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) {
1371
- goto err;
1372
- }
1373
- ssl->state = SSL3_ST_SW_KEY_EXCH_C;
1374
- return ssl_do_write(ssl);
1375
-
1376
- err:
1377
- CBB_cleanup(&cbb);
1378
- return -1;
1379
- }
1380
-
1381
- int ssl3_send_certificate_request(SSL *ssl) {
1382
- uint8_t *p, *d;
1383
- size_t i;
1384
- int j, nl, off, n;
1385
- STACK_OF(X509_NAME) *sk = NULL;
1386
- X509_NAME *name;
1387
- BUF_MEM *buf;
1388
-
1389
- if (ssl->state == SSL3_ST_SW_CERT_REQ_A) {
1390
- buf = ssl->init_buf;
1391
-
1392
- d = p = ssl_handshake_start(ssl);
1393
-
1394
- /* get the list of acceptable cert types */
1395
- p++;
1396
- n = ssl3_get_req_cert_type(ssl, p);
1397
- d[0] = n;
1398
- p += n;
1399
- n++;
1400
-
1401
- if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1402
- const uint8_t *psigs;
1403
- nl = tls12_get_psigalgs(ssl, &psigs);
1404
- s2n(nl, p);
1405
- memcpy(p, psigs, nl);
1406
- p += nl;
1407
- n += nl + 2;
1408
- }
1409
-
1410
- off = n;
1411
- p += 2;
1412
- n += 2;
1413
-
1414
- sk = SSL_get_client_CA_list(ssl);
1415
- nl = 0;
1416
- if (sk != NULL) {
1417
- for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1418
- name = sk_X509_NAME_value(sk, i);
1419
- j = i2d_X509_NAME(name, NULL);
1420
- if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) {
1421
- OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
1422
- goto err;
1423
- }
1424
- p = ssl_handshake_start(ssl) + n;
1425
- s2n(j, p);
1426
- i2d_X509_NAME(name, &p);
1427
- n += 2 + j;
1428
- nl += 2 + j;
1429
- }
1430
- }
1431
-
1432
- /* else no CA names */
1433
- p = ssl_handshake_start(ssl) + off;
1434
- s2n(nl, p);
1435
-
1436
- if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) {
1437
- goto err;
1438
- }
1439
- ssl->state = SSL3_ST_SW_CERT_REQ_B;
1440
- }
1441
-
1442
- /* SSL3_ST_SW_CERT_REQ_B */
1443
- return ssl_do_write(ssl);
1444
-
1445
- err:
1446
- return -1;
1447
- }
1448
-
1449
- int ssl3_get_client_key_exchange(SSL *ssl) {
1450
- int al;
1451
- CBS client_key_exchange;
1452
- uint32_t alg_k;
1453
- uint32_t alg_a;
1454
- uint8_t *premaster_secret = NULL;
1455
- size_t premaster_secret_len = 0;
1456
- uint8_t *decrypt_buf = NULL;
1457
-
1458
- unsigned psk_len = 0;
1459
- uint8_t psk[PSK_MAX_PSK_LEN];
1460
-
1461
- if (ssl->state == SSL3_ST_SR_KEY_EXCH_A ||
1462
- ssl->state == SSL3_ST_SR_KEY_EXCH_B) {
1463
- int ok;
1464
- const long n = ssl->method->ssl_get_message(
1465
- ssl, SSL3_ST_SR_KEY_EXCH_A, SSL3_ST_SR_KEY_EXCH_B,
1466
- SSL3_MT_CLIENT_KEY_EXCHANGE, 2048 /* ??? */, ssl_hash_message, &ok);
1467
- if (!ok) {
1468
- return n;
1469
- }
1470
- }
1471
-
1472
- CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1473
- alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1474
- alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1475
-
1476
- /* If using a PSK key exchange, prepare the pre-shared key. */
1477
- if (alg_a & SSL_aPSK) {
1478
- CBS psk_identity;
1479
-
1480
- /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1481
- * then this is the only field in the message. */
1482
- if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1483
- ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
1484
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1485
- al = SSL_AD_DECODE_ERROR;
1486
- goto f_err;
1487
- }
1488
-
1489
- if (ssl->psk_server_callback == NULL) {
1490
- OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
1491
- al = SSL_AD_INTERNAL_ERROR;
1492
- goto f_err;
1493
- }
1494
-
1495
- if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1496
- CBS_contains_zero_byte(&psk_identity)) {
1497
- OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
1498
- al = SSL_AD_ILLEGAL_PARAMETER;
1499
- goto f_err;
1500
- }
1501
-
1502
- if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) {
1503
- al = SSL_AD_INTERNAL_ERROR;
1504
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1505
- goto f_err;
1506
- }
1507
-
1508
- /* Look up the key for the identity. */
1509
- psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk,
1510
- sizeof(psk));
1511
- if (psk_len > PSK_MAX_PSK_LEN) {
1512
- OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1513
- al = SSL_AD_INTERNAL_ERROR;
1514
- goto f_err;
1515
- } else if (psk_len == 0) {
1516
- /* PSK related to the given identity not found */
1517
- OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1518
- al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1519
- goto f_err;
1520
- }
1521
- }
1522
-
1523
- /* Depending on the key exchange method, compute |premaster_secret| and
1524
- * |premaster_secret_len|. */
1525
- if (alg_k & SSL_kRSA) {
1526
- /* Allocate a buffer large enough for an RSA decryption. */
1527
- const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
1528
- decrypt_buf = OPENSSL_malloc(rsa_size);
1529
- if (decrypt_buf == NULL) {
1530
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1531
- goto err;
1532
- }
1533
-
1534
- enum ssl_private_key_result_t decrypt_result;
1535
- size_t decrypt_len;
1536
- if (ssl->state == SSL3_ST_SR_KEY_EXCH_B) {
1537
- if (!ssl_has_private_key(ssl) ||
1538
- ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
1539
- al = SSL_AD_HANDSHAKE_FAILURE;
1540
- OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1541
- goto f_err;
1542
- }
1543
- CBS encrypted_premaster_secret;
1544
- if (ssl->version > SSL3_VERSION) {
1545
- if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1546
- &encrypted_premaster_secret) ||
1547
- CBS_len(&client_key_exchange) != 0) {
1548
- al = SSL_AD_DECODE_ERROR;
1549
- OPENSSL_PUT_ERROR(SSL,
1550
- SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1551
- goto f_err;
1552
- }
1553
- } else {
1554
- encrypted_premaster_secret = client_key_exchange;
1555
- }
1556
-
1557
- /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1558
- * timing-sensitive code below. */
1559
- decrypt_result = ssl_private_key_decrypt(
1560
- ssl, decrypt_buf, &decrypt_len, rsa_size,
1561
- CBS_data(&encrypted_premaster_secret),
1562
- CBS_len(&encrypted_premaster_secret));
1563
- } else {
1564
- assert(ssl->state == SSL3_ST_SR_KEY_EXCH_C);
1565
- /* Complete async decrypt. */
1566
- decrypt_result = ssl_private_key_decrypt_complete(
1567
- ssl, decrypt_buf, &decrypt_len, rsa_size);
1568
- }
1569
-
1570
- switch (decrypt_result) {
1571
- case ssl_private_key_success:
1572
- ssl->rwstate = SSL_NOTHING;
1573
- break;
1574
- case ssl_private_key_failure:
1575
- ssl->rwstate = SSL_NOTHING;
1576
- goto err;
1577
- case ssl_private_key_retry:
1578
- ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1579
- ssl->state = SSL3_ST_SR_KEY_EXCH_C;
1580
- goto err;
1581
- }
1582
-
1583
- assert(decrypt_len == rsa_size);
1584
-
1585
- /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1586
- * section 7.4.7.1. */
1587
- premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1588
- premaster_secret = OPENSSL_malloc(premaster_secret_len);
1589
- if (premaster_secret == NULL) {
1590
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1591
- goto err;
1592
- }
1593
- if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1594
- goto err;
1595
- }
1596
-
1597
- /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1598
- * publicly invalid. */
1599
- if (decrypt_len < 11 + premaster_secret_len) {
1600
- al = SSL_AD_DECRYPT_ERROR;
1601
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1602
- goto f_err;
1603
- }
1604
-
1605
- /* Check the padding. See RFC 3447, section 7.2.2. */
1606
- size_t padding_len = decrypt_len - premaster_secret_len;
1607
- uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1608
- constant_time_eq_int_8(decrypt_buf[1], 2);
1609
- size_t i;
1610
- for (i = 2; i < padding_len - 1; i++) {
1611
- good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1612
- }
1613
- good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1614
-
1615
- /* The premaster secret must begin with |client_version|. This too must be
1616
- * checked in constant time (http://eprint.iacr.org/2003/052/). */
1617
- good &= constant_time_eq_8(decrypt_buf[padding_len],
1618
- (unsigned)(ssl->client_version >> 8));
1619
- good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
1620
- (unsigned)(ssl->client_version & 0xff));
1621
-
1622
- /* Select, in constant time, either the decrypted premaster or the random
1623
- * premaster based on |good|. */
1624
- for (i = 0; i < premaster_secret_len; i++) {
1625
- premaster_secret[i] = constant_time_select_8(
1626
- good, decrypt_buf[padding_len + i], premaster_secret[i]);
1627
- }
1628
-
1629
- OPENSSL_free(decrypt_buf);
1630
- decrypt_buf = NULL;
1631
- } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
1632
- /* Parse the ClientKeyExchange. ECDHE uses a u8 length prefix while DHE uses
1633
- * u16. */
1634
- CBS peer_key;
1635
- int peer_key_ok;
1636
- if (alg_k & SSL_kECDHE) {
1637
- peer_key_ok = CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key);
1638
- } else {
1639
- peer_key_ok =
1640
- CBS_get_u16_length_prefixed(&client_key_exchange, &peer_key);
1641
- }
1642
-
1643
- if (!peer_key_ok || CBS_len(&client_key_exchange) != 0) {
1644
- al = SSL_AD_DECODE_ERROR;
1645
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1646
- goto f_err;
1647
- }
1648
-
1649
- /* Compute the premaster. */
1650
- uint8_t alert;
1651
- if (!SSL_ECDH_CTX_compute_secret(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1652
- &premaster_secret_len, &alert,
1653
- CBS_data(&peer_key), CBS_len(&peer_key))) {
1654
- al = alert;
1655
- goto f_err;
1656
- }
1657
-
1658
- /* The key exchange state may now be discarded. */
1659
- SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
1660
- } else if (alg_k & SSL_kPSK) {
1661
- /* For plain PSK, other_secret is a block of 0s with the same length as the
1662
- * pre-shared key. */
1663
- premaster_secret_len = psk_len;
1664
- premaster_secret = OPENSSL_malloc(premaster_secret_len);
1665
- if (premaster_secret == NULL) {
1666
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1667
- goto err;
1668
- }
1669
- memset(premaster_secret, 0, premaster_secret_len);
1670
- } else {
1671
- al = SSL_AD_HANDSHAKE_FAILURE;
1672
- OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
1673
- goto f_err;
1674
- }
1675
-
1676
- /* For a PSK cipher suite, the actual pre-master secret is combined with the
1677
- * pre-shared key. */
1678
- if (alg_a & SSL_aPSK) {
1679
- CBB new_premaster, child;
1680
- uint8_t *new_data;
1681
- size_t new_len;
1682
-
1683
- CBB_zero(&new_premaster);
1684
- if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1685
- !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1686
- !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1687
- !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1688
- !CBB_add_bytes(&child, psk, psk_len) ||
1689
- !CBB_finish(&new_premaster, &new_data, &new_len)) {
1690
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1691
- CBB_cleanup(&new_premaster);
1692
- goto err;
1693
- }
1694
-
1695
- OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1696
- OPENSSL_free(premaster_secret);
1697
- premaster_secret = new_data;
1698
- premaster_secret_len = new_len;
1699
- }
1700
-
1701
- /* Compute the master secret */
1702
- ssl->session->master_key_length = tls1_generate_master_secret(
1703
- ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
1704
- if (ssl->session->master_key_length == 0) {
1705
- goto err;
1706
- }
1707
- ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
1708
-
1709
- OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1710
- OPENSSL_free(premaster_secret);
1711
- return 1;
1712
-
1713
- f_err:
1714
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1715
- err:
1716
- if (premaster_secret != NULL) {
1717
- OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1718
- OPENSSL_free(premaster_secret);
1719
- }
1720
- OPENSSL_free(decrypt_buf);
1721
-
1722
- return -1;
1723
- }
1724
-
1725
- int ssl3_get_cert_verify(SSL *ssl) {
1726
- int al, ok, ret = 0;
1727
- long n;
1728
- CBS certificate_verify, signature;
1729
- X509 *peer = ssl->session->peer;
1730
- EVP_PKEY *pkey = NULL;
1731
- const EVP_MD *md = NULL;
1732
- uint8_t digest[EVP_MAX_MD_SIZE];
1733
- size_t digest_length;
1734
- EVP_PKEY_CTX *pctx = NULL;
1735
-
1736
- /* Only RSA and ECDSA client certificates are supported, so a
1737
- * CertificateVerify is required if and only if there's a client certificate.
1738
- * */
1739
- if (peer == NULL) {
1740
- ssl3_free_handshake_buffer(ssl);
1741
- return 1;
1742
- }
1743
-
1744
- n = ssl->method->ssl_get_message(
1745
- ssl, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B,
1746
- SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH,
1747
- ssl_dont_hash_message, &ok);
1748
-
1749
- if (!ok) {
1750
- return n;
1751
- }
1752
-
1753
- /* Filter out unsupported certificate types. */
1754
- pkey = X509_get_pubkey(peer);
1755
- if (pkey == NULL) {
1756
- goto err;
1757
- }
1758
- if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
1759
- (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
1760
- al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1761
- OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1762
- goto f_err;
1763
- }
1764
-
1765
- CBS_init(&certificate_verify, ssl->init_msg, n);
1766
-
1767
- /* Determine the digest type if needbe. */
1768
- if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1769
- uint8_t hash, signature_type;
1770
- if (!CBS_get_u8(&certificate_verify, &hash) ||
1771
- !CBS_get_u8(&certificate_verify, &signature_type)) {
1772
- al = SSL_AD_DECODE_ERROR;
1773
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1774
- goto f_err;
1775
- }
1776
- if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) {
1777
- goto f_err;
1778
- }
1779
- }
1780
-
1781
- /* Compute the digest. */
1782
- if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) {
1783
- goto err;
1784
- }
1785
-
1786
- /* The handshake buffer is no longer necessary, and we may hash the current
1787
- * message.*/
1788
- ssl3_free_handshake_buffer(ssl);
1789
- if (!ssl3_hash_current_message(ssl)) {
1790
- goto err;
1791
- }
1792
-
1793
- /* Parse and verify the signature. */
1794
- if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1795
- CBS_len(&certificate_verify) != 0) {
1796
- al = SSL_AD_DECODE_ERROR;
1797
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1798
- goto f_err;
1799
- }
1800
-
1801
- pctx = EVP_PKEY_CTX_new(pkey, NULL);
1802
- if (pctx == NULL) {
1803
- goto err;
1804
- }
1805
- if (!EVP_PKEY_verify_init(pctx) ||
1806
- !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
1807
- !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest,
1808
- digest_length)) {
1809
- al = SSL_AD_DECRYPT_ERROR;
1810
- OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
1811
- goto f_err;
1812
- }
1813
-
1814
- ret = 1;
1815
-
1816
- if (0) {
1817
- f_err:
1818
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1819
- }
1820
-
1821
- err:
1822
- EVP_PKEY_CTX_free(pctx);
1823
- EVP_PKEY_free(pkey);
1824
-
1825
- return ret;
1826
- }
1827
-
1828
- int ssl3_get_client_certificate(SSL *ssl) {
1829
- int i, ok, al, ret = -1;
1830
- X509 *x = NULL;
1831
- unsigned long n;
1832
- STACK_OF(X509) *sk = NULL;
1833
- SHA256_CTX sha256;
1834
- CBS certificate_msg, certificate_list;
1835
- int is_first_certificate = 1;
1836
-
1837
- n = ssl->method->ssl_get_message(ssl, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B,
1838
- -1, (long)ssl->max_cert_list,
1839
- ssl_hash_message, &ok);
1840
-
1841
- if (!ok) {
1842
- return n;
1843
- }
1844
-
1845
- if (ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1846
- if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1847
- (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1848
- OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1849
- al = SSL_AD_HANDSHAKE_FAILURE;
1850
- goto f_err;
1851
- }
1852
-
1853
- /* If tls asked for a client cert, the client must return a 0 list */
1854
- if (ssl->version > SSL3_VERSION && ssl->s3->tmp.cert_request) {
1855
- OPENSSL_PUT_ERROR(SSL,
1856
- SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1857
- al = SSL_AD_UNEXPECTED_MESSAGE;
1858
- goto f_err;
1859
- }
1860
- ssl->s3->tmp.reuse_message = 1;
1861
-
1862
- return 1;
1863
- }
1864
-
1865
- if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1866
- al = SSL_AD_UNEXPECTED_MESSAGE;
1867
- OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
1868
- goto f_err;
1869
- }
1870
-
1871
- CBS_init(&certificate_msg, ssl->init_msg, n);
1872
-
1873
- sk = sk_X509_new_null();
1874
- if (sk == NULL) {
1875
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1876
- goto err;
1877
- }
1878
-
1879
- if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
1880
- CBS_len(&certificate_msg) != 0) {
1881
- al = SSL_AD_DECODE_ERROR;
1882
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1883
- goto f_err;
1884
- }
1885
-
1886
- while (CBS_len(&certificate_list) > 0) {
1887
- CBS certificate;
1888
- const uint8_t *data;
1889
-
1890
- if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1891
- al = SSL_AD_DECODE_ERROR;
1892
- OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1893
- goto f_err;
1894
- }
1895
-
1896
- if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
1897
- /* If this is the first certificate, and we don't want to keep peer
1898
- * certificates in memory, then we hash it right away. */
1899
- SHA256_Init(&sha256);
1900
- SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
1901
- SHA256_Final(ssl->session->peer_sha256, &sha256);
1902
- ssl->session->peer_sha256_valid = 1;
1903
- }
1904
- is_first_certificate = 0;
1905
-
1906
- /* A u24 length cannot overflow a long. */
1907
- data = CBS_data(&certificate);
1908
- x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
1909
- if (x == NULL) {
1910
- al = SSL_AD_BAD_CERTIFICATE;
1911
- OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
1912
- goto f_err;
1913
- }
1914
- if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1915
- al = SSL_AD_DECODE_ERROR;
1916
- OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
1917
- goto f_err;
1918
- }
1919
- if (!sk_X509_push(sk, x)) {
1920
- OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1921
- goto err;
1922
- }
1923
- x = NULL;
1924
- }
1925
-
1926
- if (sk_X509_num(sk) <= 0) {
1927
- /* No client certificate so the handshake buffer may be discarded. */
1928
- ssl3_free_handshake_buffer(ssl);
1929
-
1930
- /* TLS does not mind 0 certs returned */
1931
- if (ssl->version == SSL3_VERSION) {
1932
- al = SSL_AD_HANDSHAKE_FAILURE;
1933
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
1934
- goto f_err;
1935
- } else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1936
- (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1937
- /* Fail for TLS only if we required a certificate */
1938
- OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1939
- al = SSL_AD_HANDSHAKE_FAILURE;
1940
- goto f_err;
1941
- }
1942
- } else {
1943
- i = ssl_verify_cert_chain(ssl, sk);
1944
- if (i <= 0) {
1945
- al = ssl_verify_alarm_type(ssl->verify_result);
1946
- OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
1947
- goto f_err;
1948
- }
1949
- }
1950
-
1951
- X509_free(ssl->session->peer);
1952
- ssl->session->peer = sk_X509_shift(sk);
1953
- ssl->session->verify_result = ssl->verify_result;
1954
-
1955
- sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1956
- ssl->session->cert_chain = sk;
1957
- /* Inconsistency alert: cert_chain does *not* include the peer's own
1958
- * certificate, while we do include it in s3_clnt.c */
1959
-
1960
- sk = NULL;
1961
-
1962
- ret = 1;
1963
-
1964
- if (0) {
1965
- f_err:
1966
- ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1967
- }
1968
-
1969
- err:
1970
- X509_free(x);
1971
- sk_X509_pop_free(sk, X509_free);
1972
- return ret;
1973
- }
1974
-
1975
- int ssl3_send_server_certificate(SSL *ssl) {
1976
- if (ssl->state == SSL3_ST_SW_CERT_A) {
1977
- if (!ssl3_output_cert_chain(ssl)) {
1978
- return 0;
1979
- }
1980
- ssl->state = SSL3_ST_SW_CERT_B;
1981
- }
1982
-
1983
- /* SSL3_ST_SW_CERT_B */
1984
- return ssl_do_write(ssl);
1985
- }
1986
-
1987
- /* send a new session ticket (not necessarily for a new session) */
1988
- int ssl3_send_new_session_ticket(SSL *ssl) {
1989
- int ret = -1;
1990
- uint8_t *session = NULL;
1991
- size_t session_len;
1992
- EVP_CIPHER_CTX ctx;
1993
- HMAC_CTX hctx;
1994
-
1995
- EVP_CIPHER_CTX_init(&ctx);
1996
- HMAC_CTX_init(&hctx);
1997
-
1998
- if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) {
1999
- uint8_t *p, *macstart;
2000
- int len;
2001
- unsigned int hlen;
2002
- SSL_CTX *tctx = ssl->initial_ctx;
2003
- uint8_t iv[EVP_MAX_IV_LENGTH];
2004
- uint8_t key_name[16];
2005
- /* The maximum overhead of encrypting the session is 16 (key name) + IV +
2006
- * one block of encryption overhead + HMAC. */
2007
- const size_t max_ticket_overhead =
2008
- 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
2009
-
2010
- /* Serialize the SSL_SESSION to be encoded into the ticket. */
2011
- if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
2012
- &session_len)) {
2013
- goto err;
2014
- }
2015
-
2016
- /* If the session is too long, emit a dummy value rather than abort the
2017
- * connection. */
2018
- if (session_len > 0xFFFF - max_ticket_overhead) {
2019
- static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
2020
- const size_t placeholder_len = strlen(kTicketPlaceholder);
2021
-
2022
- OPENSSL_free(session);
2023
- session = NULL;
2024
-
2025
- p = ssl_handshake_start(ssl);
2026
- /* Emit ticket_lifetime_hint. */
2027
- l2n(0, p);
2028
- /* Emit ticket. */
2029
- s2n(placeholder_len, p);
2030
- memcpy(p, kTicketPlaceholder, placeholder_len);
2031
- p += placeholder_len;
2032
-
2033
- len = p - ssl_handshake_start(ssl);
2034
- if (!ssl_set_handshake_header(ssl, SSL3_MT_NEWSESSION_TICKET, len)) {
2035
- goto err;
2036
- }
2037
- ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
2038
- return ssl_do_write(ssl);
2039
- }
2040
-
2041
- /* Grow buffer if need be: the length calculation is as follows:
2042
- * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2043
- * max_ticket_overhead + * session_length */
2044
- if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 +
2045
- max_ticket_overhead + session_len)) {
2046
- goto err;
2047
- }
2048
- p = ssl_handshake_start(ssl);
2049
- /* Initialize HMAC and cipher contexts. If callback present it does all the
2050
- * work otherwise use generated values from parent ctx. */
2051
- if (tctx->tlsext_ticket_key_cb) {
2052
- if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
2053
- 1 /* encrypt */) < 0) {
2054
- goto err;
2055
- }
2056
- } else {
2057
- if (!RAND_bytes(iv, 16) ||
2058
- !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2059
- tctx->tlsext_tick_aes_key, iv) ||
2060
- !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2061
- NULL)) {
2062
- goto err;
2063
- }
2064
- memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2065
- }
2066
-
2067
- /* Ticket lifetime hint (advisory only): We leave this unspecified for
2068
- * resumed session (for simplicity), and guess that tickets for new
2069
- * sessions will live as long as their sessions. */
2070
- l2n(ssl->hit ? 0 : ssl->session->timeout, p);
2071
-
2072
- /* Skip ticket length for now */
2073
- p += 2;
2074
- /* Output key name */
2075
- macstart = p;
2076
- memcpy(p, key_name, 16);
2077
- p += 16;
2078
- /* output IV */
2079
- memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2080
- p += EVP_CIPHER_CTX_iv_length(&ctx);
2081
- /* Encrypt session data */
2082
- if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2083
- goto err;
2084
- }
2085
- p += len;
2086
- if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2087
- goto err;
2088
- }
2089
- p += len;
2090
-
2091
- if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2092
- !HMAC_Final(&hctx, p, &hlen)) {
2093
- goto err;
2094
- }
2095
-
2096
- p += hlen;
2097
- /* Now write out lengths: p points to end of data written */
2098
- /* Total length */
2099
- len = p - ssl_handshake_start(ssl);
2100
- /* Skip ticket lifetime hint */
2101
- p = ssl_handshake_start(ssl) + 4;
2102
- s2n(len - 6, p);
2103
- if (!ssl_set_handshake_header(ssl, SSL3_MT_NEWSESSION_TICKET, len)) {
2104
- goto err;
2105
- }
2106
- ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
2107
- }
2108
-
2109
- /* SSL3_ST_SW_SESSION_TICKET_B */
2110
- ret = ssl_do_write(ssl);
2111
-
2112
- err:
2113
- OPENSSL_free(session);
2114
- EVP_CIPHER_CTX_cleanup(&ctx);
2115
- HMAC_CTX_cleanup(&hctx);
2116
- return ret;
2117
- }
2118
-
2119
- /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2120
- * sets the next_proto member in s if found */
2121
- int ssl3_get_next_proto(SSL *ssl) {
2122
- int ok;
2123
- long n;
2124
- CBS next_protocol, selected_protocol, padding;
2125
-
2126
- /* Clients cannot send a NextProtocol message if we didn't see the extension
2127
- * in their ClientHello */
2128
- if (!ssl->s3->next_proto_neg_seen) {
2129
- OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
2130
- return -1;
2131
- }
2132
-
2133
- n = ssl->method->ssl_get_message(ssl, SSL3_ST_SR_NEXT_PROTO_A,
2134
- SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,
2135
- 514, /* See the payload format below */
2136
- ssl_hash_message, &ok);
2137
-
2138
- if (!ok) {
2139
- return n;
2140
- }
2141
-
2142
- CBS_init(&next_protocol, ssl->init_msg, n);
2143
-
2144
- /* The payload looks like:
2145
- * uint8 proto_len;
2146
- * uint8 proto[proto_len];
2147
- * uint8 padding_len;
2148
- * uint8 padding[padding_len]; */
2149
- if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2150
- !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2151
- CBS_len(&next_protocol) != 0 ||
2152
- !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
2153
- &ssl->s3->next_proto_negotiated_len)) {
2154
- return 0;
2155
- }
2156
-
2157
- return 1;
2158
- }
2159
-
2160
- /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
2161
- int ssl3_get_channel_id(SSL *ssl) {
2162
- int ret = -1, ok;
2163
- long n;
2164
- uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
2165
- size_t channel_id_hash_len;
2166
- const uint8_t *p;
2167
- uint16_t extension_type;
2168
- EC_GROUP *p256 = NULL;
2169
- EC_KEY *key = NULL;
2170
- EC_POINT *point = NULL;
2171
- ECDSA_SIG sig;
2172
- BIGNUM x, y;
2173
- CBS encrypted_extensions, extension;
2174
-
2175
- n = ssl->method->ssl_get_message(
2176
- ssl, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B,
2177
- SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
2178
- ssl_dont_hash_message, &ok);
2179
-
2180
- if (!ok) {
2181
- return n;
2182
- }
2183
-
2184
- /* Before incorporating the EncryptedExtensions message to the handshake
2185
- * hash, compute the hash that should have been signed. */
2186
- if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
2187
- return -1;
2188
- }
2189
- assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
2190
-
2191
- if (!ssl3_hash_current_message(ssl)) {
2192
- return -1;
2193
- }
2194
-
2195
- CBS_init(&encrypted_extensions, ssl->init_msg, n);
2196
-
2197
- /* EncryptedExtensions could include multiple extensions, but the only
2198
- * extension that could be negotiated is ChannelID, so there can only be one
2199
- * entry.
2200
- *
2201
- * The payload looks like:
2202
- * uint16 extension_type
2203
- * uint16 extension_len;
2204
- * uint8 x[32];
2205
- * uint8 y[32];
2206
- * uint8 r[32];
2207
- * uint8 s[32]; */
2208
-
2209
- if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2210
- !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2211
- CBS_len(&encrypted_extensions) != 0 ||
2212
- extension_type != TLSEXT_TYPE_channel_id ||
2213
- CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
2214
- OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
2215
- return -1;
2216
- }
2217
-
2218
- p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2219
- if (!p256) {
2220
- OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
2221
- return -1;
2222
- }
2223
-
2224
- BN_init(&x);
2225
- BN_init(&y);
2226
- sig.r = BN_new();
2227
- sig.s = BN_new();
2228
- if (sig.r == NULL || sig.s == NULL) {
2229
- goto err;
2230
- }
2231
-
2232
- p = CBS_data(&extension);
2233
- if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2234
- BN_bin2bn(p + 32, 32, &y) == NULL ||
2235
- BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2236
- BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2237
- goto err;
2238
- }
2239
-
2240
- point = EC_POINT_new(p256);
2241
- if (!point ||
2242
- !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
2243
- goto err;
2244
- }
2245
-
2246
- key = EC_KEY_new();
2247
- if (!key || !EC_KEY_set_group(key, p256) ||
2248
- !EC_KEY_set_public_key(key, point)) {
2249
- goto err;
2250
- }
2251
-
2252
- /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2253
- * were called. */
2254
- if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
2255
- OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
2256
- ssl->s3->tlsext_channel_id_valid = 0;
2257
- goto err;
2258
- }
2259
-
2260
- memcpy(ssl->s3->tlsext_channel_id, p, 64);
2261
- ret = 1;
2262
-
2263
- err:
2264
- BN_free(&x);
2265
- BN_free(&y);
2266
- BN_free(sig.r);
2267
- BN_free(sig.s);
2268
- EC_KEY_free(key);
2269
- EC_POINT_free(point);
2270
- EC_GROUP_free(p256);
2271
- return ret;
2272
- }