awesome-grpc-that-works 0.12.2 → 0.14.0.dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (724) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/Makefile +5783 -22121
  4. data/include/grpc/byte_buffer.h +2 -83
  5. data/include/grpc/census.h +173 -121
  6. data/include/grpc/compression.h +14 -38
  7. data/include/grpc/grpc.h +85 -434
  8. data/include/grpc/grpc_security.h +76 -46
  9. data/include/grpc/grpc_zookeeper.h +1 -1
  10. data/include/grpc/impl/codegen/alloc.h +74 -0
  11. data/include/grpc/impl/codegen/atm.h +92 -0
  12. data/include/grpc/impl/codegen/atm_gcc_atomic.h +72 -0
  13. data/include/grpc/impl/codegen/atm_gcc_sync.h +87 -0
  14. data/include/grpc/impl/codegen/atm_win32.h +125 -0
  15. data/include/grpc/impl/codegen/byte_buffer.h +121 -0
  16. data/include/grpc/impl/codegen/compression_types.h +73 -0
  17. data/include/grpc/impl/codegen/connectivity_state.h +59 -0
  18. data/include/grpc/impl/codegen/grpc_types.h +378 -0
  19. data/include/grpc/impl/codegen/log.h +110 -0
  20. data/include/grpc/impl/codegen/port_platform.h +370 -0
  21. data/include/grpc/impl/codegen/propagation_bits.h +67 -0
  22. data/include/grpc/impl/codegen/slice.h +182 -0
  23. data/include/grpc/impl/codegen/slice_buffer.h +105 -0
  24. data/include/grpc/impl/codegen/status.h +163 -0
  25. data/include/grpc/impl/codegen/sync.h +316 -0
  26. data/include/grpc/impl/codegen/sync_generic.h +55 -0
  27. data/{src/core/census/context.h → include/grpc/impl/codegen/sync_posix.h} +11 -11
  28. data/include/grpc/impl/codegen/sync_win32.h +49 -0
  29. data/include/grpc/impl/codegen/time.h +130 -0
  30. data/include/grpc/status.h +2 -126
  31. data/include/grpc/support/alloc.h +2 -35
  32. data/include/grpc/support/atm.h +2 -55
  33. data/include/grpc/support/atm_gcc_atomic.h +5 -38
  34. data/include/grpc/support/atm_gcc_sync.h +2 -50
  35. data/include/grpc/support/atm_win32.h +2 -88
  36. data/include/grpc/support/avl.h +10 -9
  37. data/include/grpc/support/cmdline.h +15 -13
  38. data/include/grpc/support/cpu.h +5 -3
  39. data/include/grpc/support/histogram.h +23 -20
  40. data/include/grpc/support/host_port.h +5 -3
  41. data/include/grpc/support/log.h +2 -71
  42. data/include/grpc/support/log_win32.h +2 -2
  43. data/include/grpc/support/port_platform.h +2 -319
  44. data/include/grpc/support/slice.h +2 -145
  45. data/include/grpc/support/slice_buffer.h +2 -65
  46. data/include/grpc/support/string_util.h +3 -3
  47. data/include/grpc/support/subprocess.h +8 -6
  48. data/include/grpc/support/sync.h +2 -278
  49. data/include/grpc/support/sync_generic.h +2 -18
  50. data/include/grpc/support/sync_posix.h +2 -10
  51. data/include/grpc/support/sync_win32.h +2 -12
  52. data/include/grpc/support/thd.h +11 -11
  53. data/include/grpc/support/time.h +2 -91
  54. data/include/grpc/support/tls.h +1 -1
  55. data/include/grpc/support/tls_gcc.h +1 -1
  56. data/include/grpc/support/tls_msvc.h +1 -1
  57. data/include/grpc/support/tls_pthread.h +2 -2
  58. data/include/grpc/support/useful.h +3 -1
  59. data/src/boringssl/err_data.c +1252 -0
  60. data/src/core/census/context.c +471 -8
  61. data/src/core/census/grpc_filter.c +5 -5
  62. data/src/core/census/initialize.c +4 -7
  63. data/src/core/census/mlog.c +600 -0
  64. data/src/core/census/mlog.h +95 -0
  65. data/src/core/census/operation.c +2 -2
  66. data/src/core/census/placeholders.c +109 -0
  67. data/src/core/census/rpc_metric_id.h +6 -6
  68. data/src/core/census/tracing.c +1 -1
  69. data/src/core/channel/channel_args.c +71 -9
  70. data/src/core/channel/channel_args.h +7 -1
  71. data/src/core/channel/channel_stack.c +1 -1
  72. data/src/core/channel/client_channel.c +33 -30
  73. data/src/core/channel/client_uchannel.c +3 -3
  74. data/src/core/channel/compress_filter.c +8 -8
  75. data/src/core/channel/http_client_filter.c +4 -4
  76. data/src/core/channel/http_server_filter.c +11 -11
  77. data/src/core/channel/subchannel_call_holder.c +11 -11
  78. data/src/core/client_config/connector.c +3 -2
  79. data/src/core/client_config/connector.h +2 -2
  80. data/src/core/client_config/lb_policies/load_balancer_api.c +163 -0
  81. data/src/core/client_config/lb_policies/load_balancer_api.h +85 -0
  82. data/src/core/client_config/lb_policies/pick_first.c +57 -40
  83. data/src/core/client_config/lb_policies/round_robin.c +14 -15
  84. data/src/core/client_config/lb_policy.c +3 -3
  85. data/src/core/client_config/lb_policy.h +3 -2
  86. data/src/core/client_config/resolvers/dns_resolver.c +3 -3
  87. data/src/core/client_config/resolvers/sockaddr_resolver.c +5 -5
  88. data/src/core/client_config/subchannel.c +84 -39
  89. data/src/core/client_config/subchannel.h +15 -6
  90. data/src/core/client_config/subchannel_index.c +261 -0
  91. data/src/core/client_config/subchannel_index.h +77 -0
  92. data/src/core/compression/{algorithm.c → compression_algorithm.c} +3 -3
  93. data/src/core/httpcli/format_request.c +1 -1
  94. data/src/core/httpcli/httpcli.c +18 -16
  95. data/src/core/httpcli/httpcli.h +3 -2
  96. data/src/core/httpcli/httpcli_security_connector.c +9 -10
  97. data/src/core/httpcli/parser.c +7 -7
  98. data/src/core/httpcli/parser.h +1 -1
  99. data/src/core/iomgr/closure.c +7 -7
  100. data/src/core/iomgr/closure.h +6 -5
  101. data/src/core/iomgr/exec_ctx.c +12 -8
  102. data/src/core/iomgr/exec_ctx.h +12 -5
  103. data/src/core/iomgr/executor.c +4 -4
  104. data/src/core/iomgr/executor.h +2 -2
  105. data/src/core/iomgr/fd_posix.c +32 -19
  106. data/src/core/iomgr/fd_posix.h +7 -4
  107. data/src/core/iomgr/iocp_windows.c +7 -9
  108. data/src/core/iomgr/iomgr.c +2 -2
  109. data/src/core/iomgr/pollset.h +9 -10
  110. data/src/core/iomgr/pollset_multipoller_with_epoll.c +71 -5
  111. data/src/core/iomgr/pollset_multipoller_with_poll_posix.c +9 -5
  112. data/src/core/iomgr/pollset_posix.c +44 -49
  113. data/src/core/iomgr/pollset_posix.h +14 -8
  114. data/src/core/iomgr/pollset_set.h +3 -9
  115. data/src/core/iomgr/pollset_set_posix.c +23 -3
  116. data/src/core/iomgr/pollset_set_posix.h +2 -18
  117. data/src/core/iomgr/pollset_set_windows.c +3 -3
  118. data/src/core/iomgr/pollset_set_windows.h +2 -2
  119. data/src/core/iomgr/pollset_windows.c +29 -37
  120. data/src/core/iomgr/pollset_windows.h +1 -5
  121. data/src/core/iomgr/resolve_address_posix.c +2 -2
  122. data/src/core/iomgr/resolve_address_windows.c +2 -2
  123. data/src/core/iomgr/sockaddr_utils.c +6 -6
  124. data/src/core/iomgr/sockaddr_win32.h +1 -6
  125. data/src/core/iomgr/tcp_client_posix.c +14 -12
  126. data/src/core/iomgr/tcp_client_windows.c +4 -4
  127. data/src/core/iomgr/tcp_posix.c +18 -10
  128. data/src/core/iomgr/tcp_posix.h +7 -1
  129. data/src/core/iomgr/tcp_server.h +40 -20
  130. data/src/core/iomgr/tcp_server_posix.c +106 -49
  131. data/src/core/iomgr/tcp_server_windows.c +98 -49
  132. data/src/core/iomgr/tcp_windows.c +11 -15
  133. data/src/core/iomgr/timer.c +10 -11
  134. data/src/core/iomgr/timer.h +22 -2
  135. data/src/core/iomgr/timer_heap.c +10 -10
  136. data/src/core/iomgr/timer_heap.h +2 -2
  137. data/src/core/iomgr/udp_server.c +7 -16
  138. data/src/core/iomgr/udp_server.h +2 -9
  139. data/src/core/iomgr/workqueue.h +2 -4
  140. data/src/core/iomgr/workqueue_posix.c +4 -3
  141. data/src/core/iomgr/workqueue_posix.h +3 -1
  142. data/src/core/json/json_reader.c +11 -12
  143. data/src/core/json/json_reader.h +4 -4
  144. data/src/core/json/json_string.c +19 -19
  145. data/src/core/json/json_writer.c +7 -9
  146. data/src/core/profiling/basic_timers.c +1 -1
  147. data/src/core/proto/grpc/lb/v0/load_balancer.pb.c +119 -0
  148. data/src/core/proto/grpc/lb/v0/load_balancer.pb.h +182 -0
  149. data/src/core/security/{base64.c → b64.c} +10 -10
  150. data/src/core/security/{base64.h → b64.h} +1 -1
  151. data/src/core/security/client_auth_filter.c +4 -5
  152. data/src/core/security/credentials.c +17 -10
  153. data/src/core/security/credentials.h +3 -3
  154. data/src/core/security/google_default_credentials.c +27 -22
  155. data/src/core/security/handshake.c +21 -12
  156. data/src/core/security/handshake.h +2 -1
  157. data/src/core/security/json_token.c +14 -8
  158. data/src/core/security/jwt_verifier.c +4 -3
  159. data/src/core/security/secure_endpoint.c +16 -16
  160. data/src/core/security/security_connector.c +131 -81
  161. data/src/core/security/security_connector.h +47 -27
  162. data/src/core/security/security_context.c +8 -3
  163. data/src/core/security/server_auth_filter.c +5 -5
  164. data/src/core/security/server_secure_chttp2.c +36 -37
  165. data/src/core/statistics/census_interface.h +2 -2
  166. data/src/core/statistics/census_rpc_stats.h +3 -3
  167. data/src/core/support/alloc.c +1 -1
  168. data/src/core/support/avl.c +2 -2
  169. data/src/core/support/cpu_posix.c +2 -2
  170. data/src/core/support/env_linux.c +28 -1
  171. data/src/core/support/env_win32.c +15 -7
  172. data/src/core/support/histogram.c +5 -5
  173. data/src/core/support/{file.c → load_file.c} +2 -2
  174. data/src/core/support/{file.h → load_file.h} +4 -12
  175. data/src/core/support/log_posix.c +1 -1
  176. data/src/core/support/log_win32.c +4 -3
  177. data/src/core/support/murmur_hash.c +11 -11
  178. data/src/core/support/murmur_hash.h +1 -1
  179. data/src/core/support/slice.c +11 -11
  180. data/src/core/support/slice_buffer.c +6 -6
  181. data/src/core/support/stack_lockfree.c +20 -10
  182. data/src/core/support/string.c +15 -15
  183. data/src/core/support/string.h +5 -5
  184. data/src/core/support/string_win32.c +5 -5
  185. data/src/core/support/subprocess_windows.c +141 -0
  186. data/src/core/support/sync.c +4 -4
  187. data/src/core/support/sync_posix.c +2 -2
  188. data/src/core/support/sync_win32.c +10 -5
  189. data/src/core/support/time.c +29 -29
  190. data/src/core/support/time_posix.c +15 -6
  191. data/src/core/support/time_precise.c +2 -2
  192. data/src/core/support/time_win32.c +18 -9
  193. data/src/core/support/tls_pthread.c +1 -1
  194. data/src/core/support/tmpfile.h +55 -0
  195. data/src/core/support/{file_posix.c → tmpfile_posix.c} +2 -2
  196. data/src/core/support/{file_win32.c → tmpfile_win32.c} +2 -2
  197. data/src/core/support/wrap_memcpy.c +53 -0
  198. data/src/core/surface/alarm.c +84 -0
  199. data/src/core/surface/byte_buffer_reader.c +1 -1
  200. data/src/core/surface/call.c +175 -116
  201. data/src/core/surface/call.h +2 -2
  202. data/src/core/surface/call_test_only.h +2 -2
  203. data/src/core/surface/channel.c +9 -9
  204. data/src/core/surface/channel.h +1 -1
  205. data/src/core/surface/channel_connectivity.c +3 -3
  206. data/src/core/surface/channel_create.c +4 -4
  207. data/src/core/surface/channel_ping.c +2 -2
  208. data/src/core/surface/completion_queue.c +84 -53
  209. data/src/core/surface/completion_queue.h +1 -1
  210. data/src/core/surface/init.c +11 -5
  211. data/src/core/surface/lame_client.c +2 -3
  212. data/src/core/surface/secure_channel_create.c +9 -10
  213. data/src/core/surface/server.c +30 -30
  214. data/src/core/surface/server_chttp2.c +8 -8
  215. data/src/core/surface/server_create.c +1 -4
  216. data/src/core/surface/validate_metadata.c +4 -4
  217. data/src/core/surface/version.c +2 -2
  218. data/src/core/transport/byte_stream.c +8 -6
  219. data/src/core/transport/byte_stream.h +6 -5
  220. data/src/core/transport/chttp2/bin_encoder.c +29 -29
  221. data/src/core/transport/chttp2/frame_data.c +29 -26
  222. data/src/core/transport/chttp2/frame_data.h +6 -6
  223. data/src/core/transport/chttp2/frame_goaway.c +30 -30
  224. data/src/core/transport/chttp2/frame_goaway.h +6 -6
  225. data/src/core/transport/chttp2/frame_ping.c +6 -6
  226. data/src/core/transport/chttp2/frame_ping.h +5 -5
  227. data/src/core/transport/chttp2/frame_rst_stream.c +18 -19
  228. data/src/core/transport/chttp2/frame_rst_stream.h +4 -4
  229. data/src/core/transport/chttp2/frame_settings.c +30 -30
  230. data/src/core/transport/chttp2/frame_settings.h +13 -13
  231. data/src/core/transport/chttp2/frame_window_update.c +17 -18
  232. data/src/core/transport/chttp2/frame_window_update.h +5 -7
  233. data/src/core/transport/chttp2/hpack_encoder.c +69 -73
  234. data/src/core/transport/chttp2/hpack_encoder.h +16 -16
  235. data/src/core/transport/chttp2/hpack_parser.c +167 -167
  236. data/src/core/transport/chttp2/hpack_parser.h +16 -16
  237. data/src/core/transport/chttp2/hpack_table.c +13 -13
  238. data/src/core/transport/chttp2/hpack_table.h +11 -11
  239. data/src/core/transport/chttp2/internal.h +103 -87
  240. data/src/core/transport/chttp2/parsing.c +25 -25
  241. data/src/core/transport/chttp2/stream_lists.c +38 -1
  242. data/src/core/transport/chttp2/stream_map.c +13 -14
  243. data/src/core/transport/chttp2/stream_map.h +6 -7
  244. data/src/core/transport/chttp2/timeout_encoding.c +19 -16
  245. data/src/core/transport/chttp2/varint.c +8 -9
  246. data/src/core/transport/chttp2/varint.h +7 -8
  247. data/src/core/transport/chttp2/writing.c +25 -22
  248. data/src/core/transport/chttp2_transport.c +147 -100
  249. data/src/core/transport/connectivity_state.c +6 -6
  250. data/src/core/transport/metadata.c +18 -18
  251. data/src/core/transport/metadata.h +5 -5
  252. data/src/core/transport/static_metadata.c +32 -33
  253. data/src/core/transport/static_metadata.h +8 -8
  254. data/src/core/transport/transport.c +6 -5
  255. data/src/core/transport/transport.h +17 -1
  256. data/src/core/tsi/fake_transport_security.c +7 -7
  257. data/src/core/tsi/ssl_transport_security.c +6 -4
  258. data/src/core/{iomgr/timer_internal.h → tsi/ssl_types.h} +19 -25
  259. data/src/ruby/ext/grpc/extconf.rb +52 -14
  260. data/src/ruby/ext/grpc/rb_byte_buffer.c +3 -1
  261. data/src/ruby/ext/grpc/rb_call.c +7 -3
  262. data/src/ruby/ext/grpc/rb_call_credentials.c +4 -0
  263. data/src/ruby/ext/grpc/rb_channel.c +4 -2
  264. data/src/ruby/ext/grpc/rb_channel_args.c +3 -1
  265. data/src/ruby/ext/grpc/rb_channel_credentials.c +3 -1
  266. data/src/ruby/ext/grpc/rb_completion_queue.c +3 -1
  267. data/src/ruby/ext/grpc/rb_event_thread.c +5 -0
  268. data/src/ruby/ext/grpc/rb_grpc.c +28 -19
  269. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +562 -0
  270. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +846 -0
  271. data/src/ruby/ext/grpc/rb_loader.c +72 -0
  272. data/src/ruby/ext/grpc/rb_loader.h +40 -0
  273. data/src/ruby/ext/grpc/rb_server.c +3 -1
  274. data/src/ruby/ext/grpc/rb_server_credentials.c +4 -2
  275. data/src/ruby/lib/grpc/core/time_consts.rb +2 -2
  276. data/src/ruby/lib/grpc/errors.rb +2 -2
  277. data/src/ruby/lib/grpc/generic/bidi_call.rb +1 -1
  278. data/src/ruby/lib/grpc/generic/rpc_desc.rb +1 -1
  279. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  280. data/src/ruby/lib/grpc/grpc.rb +34 -0
  281. data/src/ruby/lib/grpc/version.rb +1 -1
  282. data/src/ruby/pb/README.md +2 -2
  283. data/src/ruby/pb/generate_proto_ruby.sh +2 -2
  284. data/src/ruby/pb/grpc/health/checker.rb +11 -11
  285. data/src/ruby/pb/grpc/health/v1/health.rb +28 -0
  286. data/src/ruby/pb/grpc/health/{v1alpha → v1}/health_services.rb +4 -4
  287. data/src/ruby/spec/client_server_spec.rb +2 -1
  288. data/src/ruby/spec/pb/health/checker_spec.rb +23 -37
  289. data/third_party/boringssl/crypto/aes/aes.c +1142 -0
  290. data/third_party/boringssl/crypto/aes/internal.h +87 -0
  291. data/third_party/boringssl/crypto/aes/mode_wrappers.c +108 -0
  292. data/third_party/boringssl/crypto/asn1/a_bitstr.c +255 -0
  293. data/third_party/boringssl/crypto/asn1/a_bool.c +112 -0
  294. data/third_party/boringssl/crypto/asn1/a_bytes.c +317 -0
  295. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +286 -0
  296. data/third_party/boringssl/crypto/asn1/a_dup.c +103 -0
  297. data/third_party/boringssl/crypto/asn1/a_enum.c +183 -0
  298. data/third_party/boringssl/crypto/asn1/a_gentm.c +255 -0
  299. data/third_party/boringssl/crypto/asn1/a_i2d_fp.c +154 -0
  300. data/third_party/boringssl/crypto/asn1/a_int.c +456 -0
  301. data/third_party/boringssl/crypto/asn1/a_mbstr.c +390 -0
  302. data/third_party/boringssl/crypto/asn1/a_object.c +412 -0
  303. data/third_party/boringssl/crypto/asn1/a_octet.c +70 -0
  304. data/third_party/boringssl/crypto/asn1/a_print.c +119 -0
  305. data/third_party/boringssl/crypto/asn1/a_strnid.c +286 -0
  306. data/third_party/boringssl/crypto/asn1/a_time.c +221 -0
  307. data/third_party/boringssl/crypto/asn1/a_type.c +160 -0
  308. data/third_party/boringssl/crypto/asn1/a_utctm.c +342 -0
  309. data/third_party/boringssl/crypto/asn1/a_utf8.c +210 -0
  310. data/third_party/boringssl/crypto/asn1/asn1_lib.c +510 -0
  311. data/third_party/boringssl/crypto/asn1/asn1_locl.h +73 -0
  312. data/third_party/boringssl/crypto/asn1/asn1_par.c +444 -0
  313. data/third_party/boringssl/crypto/asn1/asn_pack.c +104 -0
  314. data/third_party/boringssl/crypto/asn1/bio_asn1.c +496 -0
  315. data/third_party/boringssl/crypto/asn1/bio_ndef.c +254 -0
  316. data/third_party/boringssl/crypto/asn1/f_enum.c +206 -0
  317. data/third_party/boringssl/crypto/asn1/f_int.c +210 -0
  318. data/third_party/boringssl/crypto/asn1/f_string.c +204 -0
  319. data/third_party/boringssl/crypto/asn1/t_bitst.c +102 -0
  320. data/third_party/boringssl/crypto/asn1/t_pkey.c +112 -0
  321. data/third_party/boringssl/crypto/asn1/tasn_dec.c +1342 -0
  322. data/third_party/boringssl/crypto/asn1/tasn_enc.c +695 -0
  323. data/third_party/boringssl/crypto/asn1/tasn_fre.c +264 -0
  324. data/third_party/boringssl/crypto/asn1/tasn_new.c +398 -0
  325. data/third_party/boringssl/crypto/asn1/tasn_prn.c +642 -0
  326. data/third_party/boringssl/crypto/asn1/tasn_typ.c +137 -0
  327. data/third_party/boringssl/crypto/asn1/tasn_utl.c +266 -0
  328. data/third_party/boringssl/crypto/asn1/x_bignum.c +143 -0
  329. data/third_party/boringssl/crypto/asn1/x_long.c +182 -0
  330. data/third_party/boringssl/crypto/base64/base64.c +478 -0
  331. data/third_party/boringssl/crypto/bio/bio.c +608 -0
  332. data/third_party/boringssl/crypto/bio/bio_mem.c +327 -0
  333. data/third_party/boringssl/crypto/bio/buffer.c +496 -0
  334. data/third_party/boringssl/crypto/bio/connect.c +544 -0
  335. data/third_party/boringssl/crypto/bio/fd.c +270 -0
  336. data/third_party/boringssl/crypto/bio/file.c +349 -0
  337. data/third_party/boringssl/crypto/bio/hexdump.c +192 -0
  338. data/third_party/boringssl/crypto/bio/internal.h +108 -0
  339. data/third_party/boringssl/crypto/bio/pair.c +803 -0
  340. data/third_party/boringssl/crypto/bio/printf.c +119 -0
  341. data/third_party/boringssl/crypto/bio/socket.c +195 -0
  342. data/third_party/boringssl/crypto/bio/socket_helper.c +113 -0
  343. data/third_party/boringssl/crypto/bn/add.c +377 -0
  344. data/third_party/boringssl/crypto/bn/asm/x86_64-gcc.c +599 -0
  345. data/third_party/boringssl/crypto/bn/bn.c +341 -0
  346. data/third_party/boringssl/crypto/bn/bn_asn1.c +93 -0
  347. data/third_party/boringssl/crypto/bn/cmp.c +200 -0
  348. data/third_party/boringssl/crypto/bn/convert.c +597 -0
  349. data/third_party/boringssl/crypto/bn/ctx.c +311 -0
  350. data/third_party/boringssl/crypto/bn/div.c +625 -0
  351. data/third_party/boringssl/crypto/bn/exponentiation.c +1544 -0
  352. data/third_party/boringssl/crypto/bn/gcd.c +711 -0
  353. data/third_party/boringssl/crypto/bn/generic.c +1019 -0
  354. data/third_party/boringssl/crypto/bn/internal.h +294 -0
  355. data/third_party/boringssl/crypto/bn/kronecker.c +175 -0
  356. data/third_party/boringssl/crypto/bn/montgomery.c +561 -0
  357. data/third_party/boringssl/crypto/bn/mul.c +888 -0
  358. data/third_party/boringssl/crypto/bn/prime.c +845 -0
  359. data/third_party/boringssl/crypto/bn/random.c +326 -0
  360. data/third_party/boringssl/crypto/bn/rsaz_exp.c +326 -0
  361. data/third_party/boringssl/crypto/bn/rsaz_exp.h +56 -0
  362. data/third_party/boringssl/crypto/bn/shift.c +299 -0
  363. data/third_party/boringssl/crypto/bn/sqrt.c +505 -0
  364. data/third_party/boringssl/crypto/buf/buf.c +235 -0
  365. data/third_party/boringssl/crypto/bytestring/ber.c +221 -0
  366. data/third_party/boringssl/crypto/bytestring/cbb.c +411 -0
  367. data/third_party/boringssl/crypto/bytestring/cbs.c +415 -0
  368. data/third_party/boringssl/crypto/bytestring/internal.h +46 -0
  369. data/third_party/boringssl/crypto/chacha/chacha_generic.c +140 -0
  370. data/third_party/boringssl/crypto/chacha/chacha_vec.c +323 -0
  371. data/third_party/boringssl/crypto/cipher/aead.c +167 -0
  372. data/third_party/boringssl/crypto/cipher/cipher.c +652 -0
  373. data/third_party/boringssl/crypto/cipher/derive_key.c +154 -0
  374. data/third_party/boringssl/crypto/cipher/e_aes.c +1767 -0
  375. data/third_party/boringssl/crypto/cipher/e_chacha20poly1305.c +311 -0
  376. data/third_party/boringssl/crypto/cipher/e_des.c +207 -0
  377. data/third_party/boringssl/crypto/cipher/e_null.c +85 -0
  378. data/third_party/boringssl/crypto/cipher/e_rc2.c +443 -0
  379. data/third_party/boringssl/crypto/cipher/e_rc4.c +87 -0
  380. data/third_party/boringssl/crypto/cipher/e_ssl3.c +463 -0
  381. data/third_party/boringssl/crypto/cipher/e_tls.c +673 -0
  382. data/third_party/boringssl/crypto/cipher/internal.h +164 -0
  383. data/third_party/boringssl/crypto/cipher/tls_cbc.c +495 -0
  384. data/third_party/boringssl/crypto/cmac/cmac.c +239 -0
  385. data/third_party/boringssl/crypto/conf/conf.c +778 -0
  386. data/third_party/boringssl/crypto/conf/conf_def.h +127 -0
  387. data/third_party/boringssl/crypto/conf/internal.h +31 -0
  388. data/third_party/boringssl/crypto/cpu-arm.c +199 -0
  389. data/third_party/boringssl/crypto/cpu-intel.c +261 -0
  390. data/third_party/boringssl/crypto/crypto.c +140 -0
  391. data/third_party/boringssl/crypto/curve25519/curve25519.c +4897 -0
  392. data/third_party/boringssl/crypto/des/des.c +771 -0
  393. data/third_party/boringssl/crypto/des/internal.h +212 -0
  394. data/third_party/boringssl/crypto/dh/check.c +180 -0
  395. data/third_party/boringssl/crypto/dh/dh.c +463 -0
  396. data/third_party/boringssl/crypto/dh/dh_asn1.c +84 -0
  397. data/third_party/boringssl/crypto/dh/internal.h +80 -0
  398. data/third_party/boringssl/crypto/dh/params.c +301 -0
  399. data/third_party/boringssl/crypto/digest/digest.c +248 -0
  400. data/third_party/boringssl/crypto/digest/digests.c +321 -0
  401. data/third_party/boringssl/crypto/digest/internal.h +112 -0
  402. data/third_party/boringssl/crypto/digest/md32_common.h +322 -0
  403. data/third_party/boringssl/crypto/directory.h +66 -0
  404. data/third_party/boringssl/crypto/directory_posix.c +108 -0
  405. data/third_party/boringssl/crypto/directory_win.c +144 -0
  406. data/third_party/boringssl/crypto/dsa/dsa.c +908 -0
  407. data/third_party/boringssl/crypto/dsa/dsa_asn1.c +150 -0
  408. data/third_party/boringssl/crypto/dsa/internal.h +78 -0
  409. data/third_party/boringssl/crypto/ec/ec.c +889 -0
  410. data/third_party/boringssl/crypto/ec/ec_asn1.c +586 -0
  411. data/third_party/boringssl/crypto/ec/ec_key.c +482 -0
  412. data/third_party/boringssl/crypto/ec/ec_montgomery.c +280 -0
  413. data/third_party/boringssl/crypto/ec/internal.h +318 -0
  414. data/third_party/boringssl/crypto/ec/oct.c +416 -0
  415. data/third_party/boringssl/crypto/ec/p224-64.c +1305 -0
  416. data/third_party/boringssl/crypto/ec/p256-64.c +1878 -0
  417. data/third_party/boringssl/crypto/ec/p256-x86_64-table.h +9548 -0
  418. data/third_party/boringssl/crypto/ec/p256-x86_64.c +596 -0
  419. data/third_party/boringssl/crypto/ec/simple.c +1313 -0
  420. data/third_party/boringssl/crypto/ec/util-64.c +183 -0
  421. data/third_party/boringssl/crypto/ec/wnaf.c +449 -0
  422. data/third_party/boringssl/crypto/ecdh/ecdh.c +153 -0
  423. data/third_party/boringssl/crypto/ecdsa/ecdsa.c +496 -0
  424. data/third_party/boringssl/crypto/ecdsa/ecdsa_asn1.c +240 -0
  425. data/third_party/boringssl/crypto/engine/engine.c +96 -0
  426. data/third_party/boringssl/crypto/err/err.c +756 -0
  427. data/third_party/boringssl/crypto/evp/algorithm.c +153 -0
  428. data/third_party/boringssl/crypto/evp/digestsign.c +159 -0
  429. data/third_party/boringssl/crypto/evp/evp.c +411 -0
  430. data/third_party/boringssl/crypto/evp/evp_asn1.c +179 -0
  431. data/third_party/boringssl/crypto/evp/evp_ctx.c +477 -0
  432. data/third_party/boringssl/crypto/evp/internal.h +278 -0
  433. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +588 -0
  434. data/third_party/boringssl/crypto/evp/p_ec.c +283 -0
  435. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +562 -0
  436. data/third_party/boringssl/crypto/evp/p_rsa.c +596 -0
  437. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +737 -0
  438. data/third_party/boringssl/crypto/evp/pbkdf.c +151 -0
  439. data/third_party/boringssl/crypto/evp/sign.c +151 -0
  440. data/third_party/boringssl/crypto/ex_data.c +294 -0
  441. data/third_party/boringssl/crypto/hkdf/hkdf.c +89 -0
  442. data/third_party/boringssl/crypto/hmac/hmac.c +213 -0
  443. data/third_party/boringssl/crypto/internal.h +532 -0
  444. data/third_party/boringssl/crypto/lhash/lhash.c +346 -0
  445. data/third_party/boringssl/crypto/md4/md4.c +225 -0
  446. data/third_party/boringssl/crypto/md5/md5.c +275 -0
  447. data/third_party/boringssl/crypto/mem.c +200 -0
  448. data/third_party/boringssl/crypto/modes/cbc.c +216 -0
  449. data/third_party/boringssl/crypto/modes/cfb.c +231 -0
  450. data/third_party/boringssl/crypto/modes/ctr.c +226 -0
  451. data/third_party/boringssl/crypto/modes/gcm.c +1252 -0
  452. data/third_party/boringssl/crypto/modes/internal.h +370 -0
  453. data/third_party/boringssl/crypto/modes/ofb.c +108 -0
  454. data/third_party/boringssl/crypto/obj/obj.c +664 -0
  455. data/third_party/boringssl/crypto/obj/obj_dat.h +5257 -0
  456. data/third_party/boringssl/crypto/obj/obj_xref.c +124 -0
  457. data/third_party/boringssl/crypto/obj/obj_xref.h +96 -0
  458. data/third_party/boringssl/crypto/pem/pem_all.c +281 -0
  459. data/third_party/boringssl/crypto/pem/pem_info.c +404 -0
  460. data/third_party/boringssl/crypto/pem/pem_lib.c +835 -0
  461. data/third_party/boringssl/crypto/pem/pem_oth.c +89 -0
  462. data/third_party/boringssl/crypto/pem/pem_pk8.c +244 -0
  463. data/third_party/boringssl/crypto/pem/pem_pkey.c +312 -0
  464. data/third_party/boringssl/crypto/pem/pem_x509.c +65 -0
  465. data/third_party/boringssl/crypto/pem/pem_xaux.c +66 -0
  466. data/third_party/boringssl/crypto/pkcs8/internal.h +83 -0
  467. data/third_party/boringssl/crypto/pkcs8/p5_pbe.c +150 -0
  468. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +440 -0
  469. data/third_party/boringssl/crypto/pkcs8/p8_pkey.c +85 -0
  470. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +1217 -0
  471. data/third_party/boringssl/crypto/poly1305/poly1305.c +331 -0
  472. data/third_party/boringssl/crypto/poly1305/poly1305_arm.c +301 -0
  473. data/third_party/boringssl/crypto/poly1305/poly1305_vec.c +892 -0
  474. data/third_party/boringssl/crypto/rand/internal.h +32 -0
  475. data/third_party/boringssl/crypto/rand/rand.c +239 -0
  476. data/third_party/boringssl/crypto/rand/urandom.c +223 -0
  477. data/third_party/boringssl/crypto/rand/windows.c +56 -0
  478. data/third_party/boringssl/crypto/rc4/rc4.c +283 -0
  479. data/third_party/boringssl/crypto/refcount_c11.c +67 -0
  480. data/third_party/boringssl/crypto/refcount_lock.c +53 -0
  481. data/third_party/boringssl/crypto/rsa/blinding.c +462 -0
  482. data/third_party/boringssl/crypto/rsa/internal.h +164 -0
  483. data/third_party/boringssl/crypto/rsa/padding.c +711 -0
  484. data/third_party/boringssl/crypto/rsa/rsa.c +808 -0
  485. data/third_party/boringssl/crypto/rsa/rsa_asn1.c +473 -0
  486. data/third_party/boringssl/crypto/rsa/rsa_impl.c +1138 -0
  487. data/third_party/boringssl/crypto/sha/sha1.c +337 -0
  488. data/third_party/boringssl/crypto/sha/sha256.c +327 -0
  489. data/third_party/boringssl/crypto/sha/sha512.c +607 -0
  490. data/third_party/boringssl/crypto/stack/stack.c +386 -0
  491. data/third_party/boringssl/crypto/test/scoped_types.h +137 -0
  492. data/third_party/boringssl/crypto/test/test_util.h +35 -0
  493. data/third_party/boringssl/crypto/thread.c +101 -0
  494. data/third_party/boringssl/crypto/thread_none.c +55 -0
  495. data/third_party/boringssl/crypto/thread_pthread.c +167 -0
  496. data/third_party/boringssl/crypto/thread_win.c +282 -0
  497. data/third_party/boringssl/crypto/time_support.c +212 -0
  498. data/third_party/boringssl/crypto/x509/a_digest.c +97 -0
  499. data/third_party/boringssl/crypto/x509/a_sign.c +136 -0
  500. data/third_party/boringssl/crypto/x509/a_strex.c +564 -0
  501. data/third_party/boringssl/crypto/x509/a_verify.c +133 -0
  502. data/third_party/boringssl/crypto/x509/asn1_gen.c +873 -0
  503. data/third_party/boringssl/crypto/x509/by_dir.c +491 -0
  504. data/third_party/boringssl/crypto/x509/by_file.c +295 -0
  505. data/third_party/boringssl/crypto/x509/charmap.h +15 -0
  506. data/third_party/boringssl/crypto/x509/i2d_pr.c +84 -0
  507. data/third_party/boringssl/crypto/x509/pkcs7.c +353 -0
  508. data/third_party/boringssl/crypto/x509/t_crl.c +129 -0
  509. data/third_party/boringssl/crypto/x509/t_req.c +246 -0
  510. data/third_party/boringssl/crypto/x509/t_x509.c +500 -0
  511. data/third_party/boringssl/crypto/x509/t_x509a.c +109 -0
  512. data/third_party/boringssl/crypto/x509/vpm_int.h +70 -0
  513. data/third_party/boringssl/crypto/x509/x509.c +152 -0
  514. data/third_party/boringssl/crypto/x509/x509_att.c +353 -0
  515. data/third_party/boringssl/crypto/x509/x509_cmp.c +490 -0
  516. data/third_party/boringssl/crypto/x509/x509_d2.c +105 -0
  517. data/third_party/boringssl/crypto/x509/x509_def.c +88 -0
  518. data/third_party/boringssl/crypto/x509/x509_ext.c +206 -0
  519. data/third_party/boringssl/crypto/x509/x509_lu.c +738 -0
  520. data/third_party/boringssl/crypto/x509/x509_obj.c +191 -0
  521. data/third_party/boringssl/crypto/x509/x509_r2x.c +113 -0
  522. data/third_party/boringssl/crypto/x509/x509_req.c +315 -0
  523. data/third_party/boringssl/crypto/x509/x509_set.c +154 -0
  524. data/third_party/boringssl/crypto/x509/x509_trs.c +304 -0
  525. data/third_party/boringssl/crypto/x509/x509_txt.c +209 -0
  526. data/third_party/boringssl/crypto/x509/x509_v3.c +271 -0
  527. data/third_party/boringssl/crypto/x509/x509_vfy.c +2456 -0
  528. data/third_party/boringssl/crypto/x509/x509_vpm.c +672 -0
  529. data/third_party/boringssl/crypto/x509/x509cset.c +172 -0
  530. data/third_party/boringssl/crypto/x509/x509name.c +381 -0
  531. data/third_party/boringssl/crypto/x509/x509rset.c +80 -0
  532. data/third_party/boringssl/crypto/x509/x509spki.c +135 -0
  533. data/third_party/boringssl/crypto/x509/x509type.c +128 -0
  534. data/third_party/boringssl/crypto/x509/x_algor.c +154 -0
  535. data/third_party/boringssl/crypto/x509/x_all.c +547 -0
  536. data/third_party/boringssl/crypto/x509/x_attrib.c +117 -0
  537. data/third_party/boringssl/crypto/x509/x_crl.c +560 -0
  538. data/third_party/boringssl/crypto/x509/x_exten.c +75 -0
  539. data/third_party/boringssl/crypto/x509/x_info.c +95 -0
  540. data/third_party/boringssl/crypto/x509/x_name.c +538 -0
  541. data/third_party/boringssl/crypto/x509/x_pkey.c +100 -0
  542. data/third_party/boringssl/crypto/x509/x_pubkey.c +384 -0
  543. data/third_party/boringssl/crypto/x509/x_req.c +112 -0
  544. data/third_party/boringssl/crypto/x509/x_sig.c +69 -0
  545. data/third_party/boringssl/crypto/x509/x_spki.c +78 -0
  546. data/third_party/boringssl/crypto/x509/x_val.c +69 -0
  547. data/third_party/boringssl/crypto/x509/x_x509.c +227 -0
  548. data/third_party/boringssl/crypto/x509/x_x509a.c +197 -0
  549. data/third_party/boringssl/crypto/x509v3/ext_dat.h +129 -0
  550. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +299 -0
  551. data/third_party/boringssl/crypto/x509v3/pcy_data.c +137 -0
  552. data/third_party/boringssl/crypto/x509v3/pcy_int.h +212 -0
  553. data/third_party/boringssl/crypto/x509v3/pcy_lib.c +165 -0
  554. data/third_party/boringssl/crypto/x509v3/pcy_map.c +133 -0
  555. data/third_party/boringssl/crypto/x509v3/pcy_node.c +197 -0
  556. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +876 -0
  557. data/third_party/boringssl/crypto/x509v3/v3_akey.c +212 -0
  558. data/third_party/boringssl/crypto/x509v3/v3_akeya.c +71 -0
  559. data/third_party/boringssl/crypto/x509v3/v3_alt.c +622 -0
  560. data/third_party/boringssl/crypto/x509v3/v3_bcons.c +126 -0
  561. data/third_party/boringssl/crypto/x509v3/v3_bitst.c +141 -0
  562. data/third_party/boringssl/crypto/x509v3/v3_conf.c +459 -0
  563. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +475 -0
  564. data/third_party/boringssl/crypto/x509v3/v3_crld.c +616 -0
  565. data/third_party/boringssl/crypto/x509v3/v3_enum.c +98 -0
  566. data/third_party/boringssl/crypto/x509v3/v3_extku.c +145 -0
  567. data/third_party/boringssl/crypto/x509v3/v3_genn.c +252 -0
  568. data/third_party/boringssl/crypto/x509v3/v3_ia5.c +117 -0
  569. data/third_party/boringssl/crypto/x509v3/v3_info.c +200 -0
  570. data/third_party/boringssl/crypto/x509v3/v3_int.c +87 -0
  571. data/third_party/boringssl/crypto/x509v3/v3_lib.c +335 -0
  572. data/third_party/boringssl/crypto/x509v3/v3_ncons.c +510 -0
  573. data/third_party/boringssl/crypto/x509v3/v3_pci.c +335 -0
  574. data/third_party/boringssl/crypto/x509v3/v3_pcia.c +56 -0
  575. data/third_party/boringssl/crypto/x509v3/v3_pcons.c +142 -0
  576. data/third_party/boringssl/crypto/x509v3/v3_pku.c +109 -0
  577. data/third_party/boringssl/crypto/x509v3/v3_pmaps.c +156 -0
  578. data/third_party/boringssl/crypto/x509v3/v3_prn.c +207 -0
  579. data/third_party/boringssl/crypto/x509v3/v3_purp.c +805 -0
  580. data/third_party/boringssl/crypto/x509v3/v3_skey.c +148 -0
  581. data/third_party/boringssl/crypto/x509v3/v3_sxnet.c +266 -0
  582. data/third_party/boringssl/crypto/x509v3/v3_utl.c +1322 -0
  583. data/third_party/boringssl/include/openssl/aead.h +346 -0
  584. data/third_party/boringssl/include/openssl/aes.h +158 -0
  585. data/third_party/boringssl/include/openssl/arm_arch.h +127 -0
  586. data/third_party/boringssl/include/openssl/asn1.h +1168 -0
  587. data/third_party/boringssl/include/openssl/asn1_mac.h +75 -0
  588. data/third_party/boringssl/include/openssl/asn1t.h +906 -0
  589. data/third_party/boringssl/include/openssl/base.h +261 -0
  590. data/third_party/boringssl/include/openssl/base64.h +184 -0
  591. data/third_party/boringssl/include/openssl/bio.h +902 -0
  592. data/third_party/boringssl/include/openssl/blowfish.h +93 -0
  593. data/third_party/boringssl/include/openssl/bn.h +885 -0
  594. data/third_party/boringssl/include/openssl/buf.h +118 -0
  595. data/third_party/boringssl/include/openssl/buffer.h +18 -0
  596. data/third_party/boringssl/include/openssl/bytestring.h +360 -0
  597. data/third_party/boringssl/include/openssl/cast.h +96 -0
  598. data/third_party/boringssl/include/openssl/chacha.h +37 -0
  599. data/third_party/boringssl/include/openssl/cipher.h +571 -0
  600. data/third_party/boringssl/include/openssl/cmac.h +76 -0
  601. data/third_party/boringssl/include/openssl/conf.h +145 -0
  602. data/third_party/boringssl/include/openssl/cpu.h +184 -0
  603. data/third_party/boringssl/include/openssl/crypto.h +68 -0
  604. data/third_party/boringssl/include/openssl/curve25519.h +88 -0
  605. data/third_party/boringssl/include/openssl/des.h +177 -0
  606. data/third_party/boringssl/include/openssl/dh.h +238 -0
  607. data/third_party/boringssl/include/openssl/digest.h +258 -0
  608. data/third_party/boringssl/include/openssl/dsa.h +343 -0
  609. data/third_party/boringssl/include/openssl/dtls1.h +16 -0
  610. data/third_party/boringssl/include/openssl/ec.h +355 -0
  611. data/third_party/boringssl/include/openssl/ec_key.h +280 -0
  612. data/third_party/boringssl/include/openssl/ecdh.h +102 -0
  613. data/third_party/boringssl/include/openssl/ecdsa.h +206 -0
  614. data/third_party/boringssl/include/openssl/engine.h +98 -0
  615. data/third_party/boringssl/include/openssl/err.h +487 -0
  616. data/third_party/boringssl/include/openssl/evp.h +750 -0
  617. data/third_party/boringssl/include/openssl/ex_data.h +213 -0
  618. data/third_party/boringssl/include/openssl/hkdf.h +44 -0
  619. data/third_party/boringssl/include/openssl/hmac.h +160 -0
  620. data/third_party/boringssl/include/openssl/lhash.h +192 -0
  621. data/third_party/boringssl/include/openssl/lhash_macros.h +132 -0
  622. data/third_party/boringssl/include/openssl/md4.h +102 -0
  623. data/third_party/boringssl/include/openssl/md5.h +107 -0
  624. data/third_party/boringssl/include/openssl/mem.h +140 -0
  625. data/third_party/boringssl/include/openssl/obj.h +198 -0
  626. data/third_party/boringssl/include/openssl/obj_mac.h +4140 -0
  627. data/third_party/boringssl/include/openssl/objects.h +18 -0
  628. data/third_party/boringssl/include/openssl/opensslfeatures.h +60 -0
  629. data/third_party/boringssl/include/openssl/opensslv.h +18 -0
  630. data/third_party/boringssl/include/openssl/ossl_typ.h +18 -0
  631. data/third_party/boringssl/include/openssl/pem.h +521 -0
  632. data/third_party/boringssl/include/openssl/pkcs12.h +18 -0
  633. data/third_party/boringssl/include/openssl/pkcs7.h +16 -0
  634. data/third_party/boringssl/include/openssl/pkcs8.h +220 -0
  635. data/third_party/boringssl/include/openssl/poly1305.h +51 -0
  636. data/third_party/boringssl/include/openssl/pqueue.h +146 -0
  637. data/third_party/boringssl/include/openssl/rand.h +113 -0
  638. data/third_party/boringssl/include/openssl/rc4.h +90 -0
  639. data/third_party/boringssl/include/openssl/rsa.h +637 -0
  640. data/third_party/boringssl/include/openssl/safestack.h +16 -0
  641. data/third_party/boringssl/include/openssl/sha.h +256 -0
  642. data/third_party/boringssl/include/openssl/srtp.h +18 -0
  643. data/third_party/boringssl/include/openssl/ssl.h +4466 -0
  644. data/third_party/boringssl/include/openssl/ssl3.h +441 -0
  645. data/third_party/boringssl/include/openssl/stack.h +298 -0
  646. data/third_party/boringssl/include/openssl/stack_macros.h +4190 -0
  647. data/third_party/boringssl/include/openssl/thread.h +173 -0
  648. data/third_party/boringssl/include/openssl/time_support.h +90 -0
  649. data/third_party/boringssl/include/openssl/tls1.h +653 -0
  650. data/third_party/boringssl/include/openssl/type_check.h +91 -0
  651. data/third_party/boringssl/include/openssl/x509.h +1258 -0
  652. data/third_party/boringssl/include/openssl/x509_vfy.h +611 -0
  653. data/third_party/boringssl/include/openssl/x509v3.h +798 -0
  654. data/third_party/boringssl/ssl/custom_extensions.c +257 -0
  655. data/third_party/boringssl/ssl/d1_both.c +880 -0
  656. data/third_party/boringssl/ssl/d1_clnt.c +566 -0
  657. data/third_party/boringssl/ssl/d1_lib.c +340 -0
  658. data/third_party/boringssl/ssl/d1_meth.c +130 -0
  659. data/third_party/boringssl/ssl/d1_pkt.c +578 -0
  660. data/third_party/boringssl/ssl/d1_srtp.c +234 -0
  661. data/third_party/boringssl/ssl/d1_srvr.c +485 -0
  662. data/third_party/boringssl/ssl/dtls_record.c +308 -0
  663. data/third_party/boringssl/ssl/internal.h +1276 -0
  664. data/third_party/boringssl/ssl/pqueue/pqueue.c +197 -0
  665. data/third_party/boringssl/ssl/s3_both.c +571 -0
  666. data/third_party/boringssl/ssl/s3_clnt.c +2241 -0
  667. data/third_party/boringssl/ssl/s3_enc.c +494 -0
  668. data/third_party/boringssl/ssl/s3_lib.c +587 -0
  669. data/third_party/boringssl/ssl/s3_meth.c +166 -0
  670. data/third_party/boringssl/ssl/s3_pkt.c +732 -0
  671. data/third_party/boringssl/ssl/s3_srvr.c +2536 -0
  672. data/third_party/boringssl/ssl/ssl_aead_ctx.c +300 -0
  673. data/third_party/boringssl/ssl/ssl_asn1.c +718 -0
  674. data/third_party/boringssl/ssl/ssl_buffer.c +319 -0
  675. data/third_party/boringssl/ssl/ssl_cert.c +539 -0
  676. data/third_party/boringssl/ssl/ssl_cipher.c +2003 -0
  677. data/third_party/boringssl/ssl/ssl_file.c +633 -0
  678. data/third_party/boringssl/ssl/ssl_lib.c +2653 -0
  679. data/third_party/boringssl/ssl/ssl_rsa.c +423 -0
  680. data/third_party/boringssl/ssl/ssl_session.c +764 -0
  681. data/third_party/boringssl/ssl/ssl_stat.c +591 -0
  682. data/third_party/boringssl/ssl/t1_enc.c +708 -0
  683. data/third_party/boringssl/ssl/t1_lib.c +2905 -0
  684. data/third_party/boringssl/ssl/test/async_bio.h +45 -0
  685. data/third_party/boringssl/ssl/test/packeted_bio.h +44 -0
  686. data/third_party/boringssl/ssl/test/scoped_types.h +28 -0
  687. data/third_party/boringssl/ssl/test/test_config.h +108 -0
  688. data/third_party/boringssl/ssl/tls_record.c +342 -0
  689. data/third_party/nanopb/pb.h +547 -0
  690. data/third_party/nanopb/pb_common.c +97 -0
  691. data/third_party/nanopb/pb_common.h +42 -0
  692. data/third_party/nanopb/pb_decode.c +1319 -0
  693. data/third_party/nanopb/pb_decode.h +149 -0
  694. data/third_party/nanopb/pb_encode.c +690 -0
  695. data/third_party/nanopb/pb_encode.h +154 -0
  696. data/third_party/zlib/adler32.c +179 -0
  697. data/third_party/zlib/compress.c +80 -0
  698. data/third_party/zlib/crc32.c +425 -0
  699. data/third_party/zlib/crc32.h +441 -0
  700. data/third_party/zlib/deflate.c +1967 -0
  701. data/third_party/zlib/deflate.h +346 -0
  702. data/third_party/zlib/gzclose.c +25 -0
  703. data/third_party/zlib/gzguts.h +209 -0
  704. data/third_party/zlib/gzlib.c +634 -0
  705. data/third_party/zlib/gzread.c +594 -0
  706. data/third_party/zlib/gzwrite.c +577 -0
  707. data/third_party/zlib/infback.c +640 -0
  708. data/third_party/zlib/inffast.c +340 -0
  709. data/third_party/zlib/inffast.h +11 -0
  710. data/third_party/zlib/inffixed.h +94 -0
  711. data/third_party/zlib/inflate.c +1512 -0
  712. data/third_party/zlib/inflate.h +122 -0
  713. data/third_party/zlib/inftrees.c +306 -0
  714. data/third_party/zlib/inftrees.h +62 -0
  715. data/third_party/zlib/trees.c +1226 -0
  716. data/third_party/zlib/trees.h +128 -0
  717. data/third_party/zlib/uncompr.c +59 -0
  718. data/third_party/zlib/zconf.h +511 -0
  719. data/third_party/zlib/zlib.h +1768 -0
  720. data/third_party/zlib/zutil.c +324 -0
  721. data/third_party/zlib/zutil.h +253 -0
  722. metadata +504 -20
  723. data/Rakefile +0 -63
  724. data/src/ruby/pb/grpc/health/v1alpha/health.rb +0 -29
@@ -0,0 +1,2905 @@
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
+ #include <openssl/ssl.h>
110
+
111
+ #include <assert.h>
112
+ #include <limits.h>
113
+ #include <stdio.h>
114
+ #include <stdlib.h>
115
+ #include <string.h>
116
+
117
+ #include <openssl/bytestring.h>
118
+ #include <openssl/digest.h>
119
+ #include <openssl/err.h>
120
+ #include <openssl/evp.h>
121
+ #include <openssl/hmac.h>
122
+ #include <openssl/mem.h>
123
+ #include <openssl/obj.h>
124
+ #include <openssl/rand.h>
125
+ #include <openssl/type_check.h>
126
+
127
+ #include "internal.h"
128
+
129
+
130
+ static int ssl_check_clienthello_tlsext(SSL *s);
131
+ static int ssl_check_serverhello_tlsext(SSL *s);
132
+
133
+ const SSL3_ENC_METHOD TLSv1_enc_data = {
134
+ tls1_prf,
135
+ tls1_setup_key_block,
136
+ tls1_generate_master_secret,
137
+ tls1_change_cipher_state,
138
+ tls1_final_finish_mac,
139
+ tls1_cert_verify_mac,
140
+ TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
141
+ TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
142
+ tls1_alert_code,
143
+ tls1_export_keying_material,
144
+ 0,
145
+ };
146
+
147
+ const SSL3_ENC_METHOD TLSv1_1_enc_data = {
148
+ tls1_prf,
149
+ tls1_setup_key_block,
150
+ tls1_generate_master_secret,
151
+ tls1_change_cipher_state,
152
+ tls1_final_finish_mac,
153
+ tls1_cert_verify_mac,
154
+ TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
155
+ TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
156
+ tls1_alert_code,
157
+ tls1_export_keying_material,
158
+ SSL_ENC_FLAG_EXPLICIT_IV,
159
+ };
160
+
161
+ const SSL3_ENC_METHOD TLSv1_2_enc_data = {
162
+ tls1_prf,
163
+ tls1_setup_key_block,
164
+ tls1_generate_master_secret,
165
+ tls1_change_cipher_state,
166
+ tls1_final_finish_mac,
167
+ tls1_cert_verify_mac,
168
+ TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
169
+ TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
170
+ tls1_alert_code,
171
+ tls1_export_keying_material,
172
+ SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF,
173
+ };
174
+
175
+ static int compare_uint16_t(const void *p1, const void *p2) {
176
+ uint16_t u1 = *((const uint16_t *)p1);
177
+ uint16_t u2 = *((const uint16_t *)p2);
178
+ if (u1 < u2) {
179
+ return -1;
180
+ } else if (u1 > u2) {
181
+ return 1;
182
+ } else {
183
+ return 0;
184
+ }
185
+ }
186
+
187
+ /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
188
+ * more than one extension of the same type in a ClientHello or ServerHello.
189
+ * This function does an initial scan over the extensions block to filter those
190
+ * out. */
191
+ static int tls1_check_duplicate_extensions(const CBS *cbs) {
192
+ CBS extensions = *cbs;
193
+ size_t num_extensions = 0, i = 0;
194
+ uint16_t *extension_types = NULL;
195
+ int ret = 0;
196
+
197
+ /* First pass: count the extensions. */
198
+ while (CBS_len(&extensions) > 0) {
199
+ uint16_t type;
200
+ CBS extension;
201
+
202
+ if (!CBS_get_u16(&extensions, &type) ||
203
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
204
+ goto done;
205
+ }
206
+
207
+ num_extensions++;
208
+ }
209
+
210
+ if (num_extensions == 0) {
211
+ return 1;
212
+ }
213
+
214
+ extension_types =
215
+ (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
216
+ if (extension_types == NULL) {
217
+ OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
218
+ goto done;
219
+ }
220
+
221
+ /* Second pass: gather the extension types. */
222
+ extensions = *cbs;
223
+ for (i = 0; i < num_extensions; i++) {
224
+ CBS extension;
225
+
226
+ if (!CBS_get_u16(&extensions, &extension_types[i]) ||
227
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
228
+ /* This should not happen. */
229
+ goto done;
230
+ }
231
+ }
232
+ assert(CBS_len(&extensions) == 0);
233
+
234
+ /* Sort the extensions and make sure there are no duplicates. */
235
+ qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
236
+ for (i = 1; i < num_extensions; i++) {
237
+ if (extension_types[i - 1] == extension_types[i]) {
238
+ goto done;
239
+ }
240
+ }
241
+
242
+ ret = 1;
243
+
244
+ done:
245
+ OPENSSL_free(extension_types);
246
+ return ret;
247
+ }
248
+
249
+ char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
250
+ CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
251
+
252
+ CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
253
+
254
+ if (/* Skip client version. */
255
+ !CBS_skip(&client_hello, 2) ||
256
+ /* Skip client nonce. */
257
+ !CBS_skip(&client_hello, 32) ||
258
+ /* Extract session_id. */
259
+ !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
260
+ return 0;
261
+ }
262
+
263
+ ctx->session_id = CBS_data(&session_id);
264
+ ctx->session_id_len = CBS_len(&session_id);
265
+
266
+ /* Skip past DTLS cookie */
267
+ if (SSL_IS_DTLS(ctx->ssl)) {
268
+ CBS cookie;
269
+
270
+ if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
271
+ return 0;
272
+ }
273
+ }
274
+
275
+ /* Extract cipher_suites. */
276
+ if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
277
+ CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
278
+ return 0;
279
+ }
280
+ ctx->cipher_suites = CBS_data(&cipher_suites);
281
+ ctx->cipher_suites_len = CBS_len(&cipher_suites);
282
+
283
+ /* Extract compression_methods. */
284
+ if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
285
+ CBS_len(&compression_methods) < 1) {
286
+ return 0;
287
+ }
288
+ ctx->compression_methods = CBS_data(&compression_methods);
289
+ ctx->compression_methods_len = CBS_len(&compression_methods);
290
+
291
+ /* If the ClientHello ends here then it's valid, but doesn't have any
292
+ * extensions. (E.g. SSLv3.) */
293
+ if (CBS_len(&client_hello) == 0) {
294
+ ctx->extensions = NULL;
295
+ ctx->extensions_len = 0;
296
+ return 1;
297
+ }
298
+
299
+ /* Extract extensions and check it is valid. */
300
+ if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
301
+ !tls1_check_duplicate_extensions(&extensions) ||
302
+ CBS_len(&client_hello) != 0) {
303
+ return 0;
304
+ }
305
+ ctx->extensions = CBS_data(&extensions);
306
+ ctx->extensions_len = CBS_len(&extensions);
307
+
308
+ return 1;
309
+ }
310
+
311
+ int SSL_early_callback_ctx_extension_get(
312
+ const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
313
+ const uint8_t **out_data, size_t *out_len) {
314
+ CBS extensions;
315
+
316
+ CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
317
+
318
+ while (CBS_len(&extensions) != 0) {
319
+ uint16_t type;
320
+ CBS extension;
321
+
322
+ /* Decode the next extension. */
323
+ if (!CBS_get_u16(&extensions, &type) ||
324
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
325
+ return 0;
326
+ }
327
+
328
+ if (type == extension_type) {
329
+ *out_data = CBS_data(&extension);
330
+ *out_len = CBS_len(&extension);
331
+ return 1;
332
+ }
333
+ }
334
+
335
+ return 0;
336
+ }
337
+
338
+ struct tls_curve {
339
+ uint16_t curve_id;
340
+ int nid;
341
+ const char curve_name[8];
342
+ };
343
+
344
+ /* ECC curves from RFC4492. */
345
+ static const struct tls_curve tls_curves[] = {
346
+ {21, NID_secp224r1, "P-224"},
347
+ {23, NID_X9_62_prime256v1, "P-256"},
348
+ {24, NID_secp384r1, "P-384"},
349
+ {25, NID_secp521r1, "P-521"},
350
+ };
351
+
352
+ static const uint16_t eccurves_default[] = {
353
+ 23, /* X9_62_prime256v1 */
354
+ 24, /* secp384r1 */
355
+ #if defined(BORINGSSL_ANDROID_SYSTEM)
356
+ 25, /* secp521r1 */
357
+ #endif
358
+ };
359
+
360
+ int tls1_ec_curve_id2nid(uint16_t curve_id) {
361
+ size_t i;
362
+ for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
363
+ if (curve_id == tls_curves[i].curve_id) {
364
+ return tls_curves[i].nid;
365
+ }
366
+ }
367
+ return NID_undef;
368
+ }
369
+
370
+ int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
371
+ size_t i;
372
+ for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
373
+ if (nid == tls_curves[i].nid) {
374
+ *out_curve_id = tls_curves[i].curve_id;
375
+ return 1;
376
+ }
377
+ }
378
+ return 0;
379
+ }
380
+
381
+ const char* tls1_ec_curve_id2name(uint16_t curve_id) {
382
+ size_t i;
383
+ for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
384
+ if (curve_id == tls_curves[i].curve_id) {
385
+ return tls_curves[i].curve_name;
386
+ }
387
+ }
388
+ return NULL;
389
+ }
390
+
391
+ /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
392
+ * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
393
+ * peer's curve list. Otherwise, return the preferred list. */
394
+ static void tls1_get_curvelist(SSL *s, int get_peer_curves,
395
+ const uint16_t **out_curve_ids,
396
+ size_t *out_curve_ids_len) {
397
+ if (get_peer_curves) {
398
+ /* Only clients send a curve list, so this function is only called
399
+ * on the server. */
400
+ assert(s->server);
401
+ *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
402
+ *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
403
+ return;
404
+ }
405
+
406
+ *out_curve_ids = s->tlsext_ellipticcurvelist;
407
+ *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
408
+ if (!*out_curve_ids) {
409
+ *out_curve_ids = eccurves_default;
410
+ *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
411
+ }
412
+ }
413
+
414
+ int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
415
+ uint8_t curve_type;
416
+ uint16_t curve_id;
417
+ const uint16_t *curves;
418
+ size_t curves_len, i;
419
+
420
+ /* Only support named curves. */
421
+ if (!CBS_get_u8(cbs, &curve_type) ||
422
+ curve_type != NAMED_CURVE_TYPE ||
423
+ !CBS_get_u16(cbs, &curve_id)) {
424
+ return 0;
425
+ }
426
+
427
+ tls1_get_curvelist(s, 0, &curves, &curves_len);
428
+ for (i = 0; i < curves_len; i++) {
429
+ if (curve_id == curves[i]) {
430
+ *out_curve_id = curve_id;
431
+ return 1;
432
+ }
433
+ }
434
+
435
+ return 0;
436
+ }
437
+
438
+ int tls1_get_shared_curve(SSL *s) {
439
+ const uint16_t *curves, *peer_curves, *pref, *supp;
440
+ size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
441
+
442
+ /* Can't do anything on client side */
443
+ if (s->server == 0) {
444
+ return NID_undef;
445
+ }
446
+
447
+ tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
448
+ tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
449
+
450
+ if (peer_curves_len == 0) {
451
+ /* Clients are not required to send a supported_curves extension. In this
452
+ * case, the server is free to pick any curve it likes. See RFC 4492,
453
+ * section 4, paragraph 3. */
454
+ return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
455
+ }
456
+
457
+ if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
458
+ pref = curves;
459
+ pref_len = curves_len;
460
+ supp = peer_curves;
461
+ supp_len = peer_curves_len;
462
+ } else {
463
+ pref = peer_curves;
464
+ pref_len = peer_curves_len;
465
+ supp = curves;
466
+ supp_len = curves_len;
467
+ }
468
+
469
+ for (i = 0; i < pref_len; i++) {
470
+ for (j = 0; j < supp_len; j++) {
471
+ if (pref[i] == supp[j]) {
472
+ return tls1_ec_curve_id2nid(pref[i]);
473
+ }
474
+ }
475
+ }
476
+
477
+ return NID_undef;
478
+ }
479
+
480
+ int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
481
+ const int *curves, size_t ncurves) {
482
+ uint16_t *curve_ids;
483
+ size_t i;
484
+
485
+ curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
486
+ if (curve_ids == NULL) {
487
+ return 0;
488
+ }
489
+
490
+ for (i = 0; i < ncurves; i++) {
491
+ if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
492
+ OPENSSL_free(curve_ids);
493
+ return 0;
494
+ }
495
+ }
496
+
497
+ OPENSSL_free(*out_curve_ids);
498
+ *out_curve_ids = curve_ids;
499
+ *out_curve_ids_len = ncurves;
500
+
501
+ return 1;
502
+ }
503
+
504
+ /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
505
+ * TLS curve ID and point format, respectively, for |ec|. It returns one on
506
+ * success and zero on failure. */
507
+ static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
508
+ uint8_t *out_comp_id, EC_KEY *ec) {
509
+ int nid;
510
+ uint16_t id;
511
+ const EC_GROUP *grp;
512
+
513
+ if (ec == NULL) {
514
+ return 0;
515
+ }
516
+
517
+ grp = EC_KEY_get0_group(ec);
518
+ if (grp == NULL) {
519
+ return 0;
520
+ }
521
+
522
+ /* Determine curve ID */
523
+ nid = EC_GROUP_get_curve_name(grp);
524
+ if (!tls1_ec_nid2curve_id(&id, nid)) {
525
+ return 0;
526
+ }
527
+
528
+ /* Set the named curve ID. Arbitrary explicit curves are not supported. */
529
+ *out_curve_id = id;
530
+
531
+ if (out_comp_id) {
532
+ if (EC_KEY_get0_public_key(ec) == NULL) {
533
+ return 0;
534
+ }
535
+ if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
536
+ *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
537
+ } else {
538
+ *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
539
+ }
540
+ }
541
+
542
+ return 1;
543
+ }
544
+
545
+ /* tls1_check_curve_id returns one if |curve_id| is consistent with both our
546
+ * and the peer's curve preferences. Note: if called as the client, only our
547
+ * preferences are checked; the peer (the server) does not send preferences. */
548
+ static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
549
+ const uint16_t *curves;
550
+ size_t curves_len, i, get_peer_curves;
551
+
552
+ /* Check against our list, then the peer's list. */
553
+ for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
554
+ if (get_peer_curves && !s->server) {
555
+ /* Servers do not present a preference list so, if we are a client, only
556
+ * check our list. */
557
+ continue;
558
+ }
559
+
560
+ tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
561
+ if (get_peer_curves && curves_len == 0) {
562
+ /* Clients are not required to send a supported_curves extension. In this
563
+ * case, the server is free to pick any curve it likes. See RFC 4492,
564
+ * section 4, paragraph 3. */
565
+ continue;
566
+ }
567
+ for (i = 0; i < curves_len; i++) {
568
+ if (curves[i] == curve_id) {
569
+ break;
570
+ }
571
+ }
572
+
573
+ if (i == curves_len) {
574
+ return 0;
575
+ }
576
+ }
577
+
578
+ return 1;
579
+ }
580
+
581
+ int tls1_check_ec_cert(SSL *s, X509 *x) {
582
+ int ret = 0;
583
+ EVP_PKEY *pkey = X509_get_pubkey(x);
584
+ uint16_t curve_id;
585
+ uint8_t comp_id;
586
+
587
+ if (!pkey) {
588
+ goto done;
589
+ }
590
+ EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
591
+ if (ec_key == NULL ||
592
+ !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) ||
593
+ !tls1_check_curve_id(s, curve_id) ||
594
+ comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
595
+ goto done;
596
+ }
597
+
598
+ ret = 1;
599
+
600
+ done:
601
+ EVP_PKEY_free(pkey);
602
+ return ret;
603
+ }
604
+
605
+ /* List of supported signature algorithms and hashes. Should make this
606
+ * customisable at some point, for now include everything we support. */
607
+
608
+ #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
609
+
610
+ #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
611
+
612
+ #define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
613
+
614
+ static const uint8_t tls12_sigalgs[] = {
615
+ tlsext_sigalg(TLSEXT_hash_sha512)
616
+ tlsext_sigalg(TLSEXT_hash_sha384)
617
+ tlsext_sigalg(TLSEXT_hash_sha256)
618
+ tlsext_sigalg(TLSEXT_hash_sha224)
619
+ tlsext_sigalg(TLSEXT_hash_sha1)
620
+ };
621
+
622
+ size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
623
+ *psigs = tls12_sigalgs;
624
+ return sizeof(tls12_sigalgs);
625
+ }
626
+
627
+ int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert,
628
+ uint8_t hash, uint8_t signature, EVP_PKEY *pkey) {
629
+ const uint8_t *sent_sigs;
630
+ size_t sent_sigslen, i;
631
+ int sigalg = tls12_get_sigid(pkey->type);
632
+
633
+ /* Should never happen */
634
+ if (sigalg == -1) {
635
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
636
+ *out_alert = SSL_AD_INTERNAL_ERROR;
637
+ return 0;
638
+ }
639
+
640
+ /* Check key type is consistent with signature */
641
+ if (sigalg != signature) {
642
+ OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
643
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
644
+ return 0;
645
+ }
646
+
647
+ /* Check signature matches a type we sent */
648
+ sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
649
+ for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
650
+ if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
651
+ break;
652
+ }
653
+ }
654
+
655
+ if (i == sent_sigslen) {
656
+ OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
657
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
658
+ return 0;
659
+ }
660
+
661
+ *out_md = tls12_get_hash(hash);
662
+ if (*out_md == NULL) {
663
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST);
664
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
665
+ return 0;
666
+ }
667
+
668
+ return 1;
669
+ }
670
+
671
+ /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
672
+ * supported or doesn't appear in supported signature algorithms. Unlike
673
+ * ssl_cipher_get_disabled this applies to a specific session and not global
674
+ * settings. */
675
+ void ssl_set_client_disabled(SSL *s) {
676
+ CERT *c = s->cert;
677
+ const uint8_t *sigalgs;
678
+ size_t i, sigalgslen;
679
+ int have_rsa = 0, have_ecdsa = 0;
680
+ c->mask_a = 0;
681
+ c->mask_k = 0;
682
+
683
+ /* Now go through all signature algorithms seeing if we support any for RSA,
684
+ * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
685
+ sigalgslen = tls12_get_psigalgs(s, &sigalgs);
686
+ for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
687
+ switch (sigalgs[1]) {
688
+ case TLSEXT_signature_rsa:
689
+ have_rsa = 1;
690
+ break;
691
+
692
+ case TLSEXT_signature_ecdsa:
693
+ have_ecdsa = 1;
694
+ break;
695
+ }
696
+ }
697
+
698
+ /* Disable auth if we don't include any appropriate signature algorithms. */
699
+ if (!have_rsa) {
700
+ c->mask_a |= SSL_aRSA;
701
+ }
702
+ if (!have_ecdsa) {
703
+ c->mask_a |= SSL_aECDSA;
704
+ }
705
+
706
+ /* with PSK there must be client callback set */
707
+ if (!s->psk_client_callback) {
708
+ c->mask_a |= SSL_aPSK;
709
+ c->mask_k |= SSL_kPSK;
710
+ }
711
+ }
712
+
713
+ /* tls_extension represents a TLS extension that is handled internally. The
714
+ * |init| function is called for each handshake, before any other functions of
715
+ * the extension. Then the add and parse callbacks are called as needed.
716
+ *
717
+ * The parse callbacks receive a |CBS| that contains the contents of the
718
+ * extension (i.e. not including the type and length bytes). If an extension is
719
+ * not received then the parse callbacks will be called with a NULL CBS so that
720
+ * they can do any processing needed to handle the absence of an extension.
721
+ *
722
+ * The add callbacks receive a |CBB| to which the extension can be appended but
723
+ * the function is responsible for appending the type and length bytes too.
724
+ *
725
+ * All callbacks return one for success and zero for error. If a parse function
726
+ * returns zero then a fatal alert with value |*out_alert| will be sent. If
727
+ * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
728
+ struct tls_extension {
729
+ uint16_t value;
730
+ void (*init)(SSL *ssl);
731
+
732
+ int (*add_clienthello)(SSL *ssl, CBB *out);
733
+ int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
734
+
735
+ int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
736
+ int (*add_serverhello)(SSL *ssl, CBB *out);
737
+ };
738
+
739
+
740
+ /* Server name indication (SNI).
741
+ *
742
+ * https://tools.ietf.org/html/rfc6066#section-3. */
743
+
744
+ static void ext_sni_init(SSL *ssl) {
745
+ ssl->s3->tmp.should_ack_sni = 0;
746
+ }
747
+
748
+ static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
749
+ if (ssl->tlsext_hostname == NULL) {
750
+ return 1;
751
+ }
752
+
753
+ CBB contents, server_name_list, name;
754
+ if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
755
+ !CBB_add_u16_length_prefixed(out, &contents) ||
756
+ !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
757
+ !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
758
+ !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
759
+ !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
760
+ strlen(ssl->tlsext_hostname)) ||
761
+ !CBB_flush(out)) {
762
+ return 0;
763
+ }
764
+
765
+ return 1;
766
+ }
767
+
768
+ static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
769
+ if (contents == NULL) {
770
+ return 1;
771
+ }
772
+
773
+ if (CBS_len(contents) != 0) {
774
+ return 0;
775
+ }
776
+
777
+ assert(ssl->tlsext_hostname != NULL);
778
+
779
+ if (!ssl->hit) {
780
+ assert(ssl->session->tlsext_hostname == NULL);
781
+ ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
782
+ if (!ssl->session->tlsext_hostname) {
783
+ *out_alert = SSL_AD_INTERNAL_ERROR;
784
+ return 0;
785
+ }
786
+ }
787
+
788
+ return 1;
789
+ }
790
+
791
+ static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
792
+ if (contents == NULL) {
793
+ return 1;
794
+ }
795
+
796
+ /* The servername extension is treated as follows:
797
+ *
798
+ * - Only the hostname type is supported with a maximum length of 255.
799
+ * - The servername is rejected if too long or if it contains zeros, in
800
+ * which case an fatal alert is generated.
801
+ * - The servername field is maintained together with the session cache.
802
+ * - When a session is resumed, the servername callback is invoked in order
803
+ * to allow the application to position itself to the right context.
804
+ * - The servername is acknowledged if it is new for a session or when
805
+ * it is identical to a previously used for the same session.
806
+ * Applications can control the behaviour. They can at any time
807
+ * set a 'desirable' servername for a new SSL object. This can be the
808
+ * case for example with HTTPS when a Host: header field is received and
809
+ * a renegotiation is requested. In this case, a possible servername
810
+ * presented in the new client hello is only acknowledged if it matches
811
+ * the value of the Host: field.
812
+ * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
813
+ * if they provide for changing an explicit servername context for the
814
+ * session,
815
+ * i.e. when the session has been established with a servername extension.
816
+ */
817
+
818
+ CBS server_name_list;
819
+ char have_seen_host_name = 0;
820
+
821
+ if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
822
+ CBS_len(&server_name_list) == 0 ||
823
+ CBS_len(contents) != 0) {
824
+ return 0;
825
+ }
826
+
827
+ /* Decode each ServerName in the extension. */
828
+ while (CBS_len(&server_name_list) > 0) {
829
+ uint8_t name_type;
830
+ CBS host_name;
831
+
832
+ if (!CBS_get_u8(&server_name_list, &name_type) ||
833
+ !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) {
834
+ return 0;
835
+ }
836
+
837
+ /* Only host_name is supported. */
838
+ if (name_type != TLSEXT_NAMETYPE_host_name) {
839
+ continue;
840
+ }
841
+
842
+ if (have_seen_host_name) {
843
+ /* The ServerNameList MUST NOT contain more than one name of the same
844
+ * name_type. */
845
+ return 0;
846
+ }
847
+
848
+ have_seen_host_name = 1;
849
+
850
+ if (CBS_len(&host_name) == 0 ||
851
+ CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
852
+ CBS_contains_zero_byte(&host_name)) {
853
+ *out_alert = SSL_AD_UNRECOGNIZED_NAME;
854
+ return 0;
855
+ }
856
+
857
+ if (!ssl->hit) {
858
+ assert(ssl->session->tlsext_hostname == NULL);
859
+ if (ssl->session->tlsext_hostname) {
860
+ /* This should be impossible. */
861
+ return 0;
862
+ }
863
+
864
+ /* Copy the hostname as a string. */
865
+ if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) {
866
+ *out_alert = SSL_AD_INTERNAL_ERROR;
867
+ return 0;
868
+ }
869
+
870
+ ssl->s3->tmp.should_ack_sni = 1;
871
+ }
872
+ }
873
+
874
+ return 1;
875
+ }
876
+
877
+ static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
878
+ if (ssl->hit ||
879
+ !ssl->s3->tmp.should_ack_sni ||
880
+ ssl->session->tlsext_hostname == NULL) {
881
+ return 1;
882
+ }
883
+
884
+ if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
885
+ !CBB_add_u16(out, 0 /* length */)) {
886
+ return 0;
887
+ }
888
+
889
+ return 1;
890
+ }
891
+
892
+
893
+ /* Renegotiation indication.
894
+ *
895
+ * https://tools.ietf.org/html/rfc5746 */
896
+
897
+ static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
898
+ CBB contents, prev_finished;
899
+ if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
900
+ !CBB_add_u16_length_prefixed(out, &contents) ||
901
+ !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
902
+ !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
903
+ ssl->s3->previous_client_finished_len) ||
904
+ !CBB_flush(out)) {
905
+ return 0;
906
+ }
907
+
908
+ return 1;
909
+ }
910
+
911
+ static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
912
+ CBS *contents) {
913
+ /* Servers may not switch between omitting the extension and supporting it.
914
+ * See RFC 5746, sections 3.5 and 4.2. */
915
+ if (ssl->s3->initial_handshake_complete &&
916
+ (contents != NULL) != ssl->s3->send_connection_binding) {
917
+ *out_alert = SSL_AD_HANDSHAKE_FAILURE;
918
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
919
+ return 0;
920
+ }
921
+
922
+ if (contents == NULL) {
923
+ /* Strictly speaking, if we want to avoid an attack we should *always* see
924
+ * RI even on initial ServerHello because the client doesn't see any
925
+ * renegotiation during an attack. However this would mean we could not
926
+ * connect to any server which doesn't support RI.
927
+ *
928
+ * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
929
+ * practical terms every client sets it so it's just assumed here. */
930
+ return 1;
931
+ }
932
+
933
+ const size_t expected_len = ssl->s3->previous_client_finished_len +
934
+ ssl->s3->previous_server_finished_len;
935
+
936
+ /* Check for logic errors */
937
+ assert(!expected_len || ssl->s3->previous_client_finished_len);
938
+ assert(!expected_len || ssl->s3->previous_server_finished_len);
939
+
940
+ /* Parse out the extension contents. */
941
+ CBS renegotiated_connection;
942
+ if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
943
+ CBS_len(contents) != 0) {
944
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
945
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
946
+ return 0;
947
+ }
948
+
949
+ /* Check that the extension matches. */
950
+ if (CBS_len(&renegotiated_connection) != expected_len) {
951
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
952
+ *out_alert = SSL_AD_HANDSHAKE_FAILURE;
953
+ return 0;
954
+ }
955
+
956
+ const uint8_t *d = CBS_data(&renegotiated_connection);
957
+ if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
958
+ ssl->s3->previous_client_finished_len)) {
959
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
960
+ *out_alert = SSL_AD_HANDSHAKE_FAILURE;
961
+ return 0;
962
+ }
963
+ d += ssl->s3->previous_client_finished_len;
964
+
965
+ if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
966
+ ssl->s3->previous_server_finished_len)) {
967
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
968
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
969
+ return 0;
970
+ }
971
+ ssl->s3->send_connection_binding = 1;
972
+
973
+ return 1;
974
+ }
975
+
976
+ static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
977
+ CBS *contents) {
978
+ /* Renegotiation isn't supported as a server so this function should never be
979
+ * called after the initial handshake. */
980
+ assert(!ssl->s3->initial_handshake_complete);
981
+
982
+ CBS fake_contents;
983
+ static const uint8_t kFakeExtension[] = {0};
984
+
985
+ if (contents == NULL) {
986
+ if (ssl->s3->send_connection_binding) {
987
+ /* The renegotiation SCSV was received so pretend that we received a
988
+ * renegotiation extension. */
989
+ CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
990
+ contents = &fake_contents;
991
+ /* We require that the renegotiation extension is at index zero of
992
+ * kExtensions. */
993
+ ssl->s3->tmp.extensions.received |= (1u << 0);
994
+ } else {
995
+ return 1;
996
+ }
997
+ }
998
+
999
+ CBS renegotiated_connection;
1000
+
1001
+ if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
1002
+ CBS_len(contents) != 0) {
1003
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
1004
+ return 0;
1005
+ }
1006
+
1007
+ /* Check that the extension matches */
1008
+ if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished,
1009
+ ssl->s3->previous_client_finished_len)) {
1010
+ OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
1011
+ *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1012
+ return 0;
1013
+ }
1014
+
1015
+ ssl->s3->send_connection_binding = 1;
1016
+
1017
+ return 1;
1018
+ }
1019
+
1020
+ static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
1021
+ CBB contents, prev_finished;
1022
+ if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
1023
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1024
+ !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
1025
+ !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
1026
+ ssl->s3->previous_client_finished_len) ||
1027
+ !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
1028
+ ssl->s3->previous_server_finished_len) ||
1029
+ !CBB_flush(out)) {
1030
+ return 0;
1031
+ }
1032
+
1033
+ return 1;
1034
+ }
1035
+
1036
+
1037
+ /* Extended Master Secret.
1038
+ *
1039
+ * https://tools.ietf.org/html/draft-ietf-tls-session-hash-05 */
1040
+
1041
+ static void ext_ems_init(SSL *ssl) {
1042
+ ssl->s3->tmp.extended_master_secret = 0;
1043
+ }
1044
+
1045
+ static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
1046
+ if (ssl->version == SSL3_VERSION) {
1047
+ return 1;
1048
+ }
1049
+
1050
+ if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1051
+ !CBB_add_u16(out, 0 /* length */)) {
1052
+ return 0;
1053
+ }
1054
+
1055
+ return 1;
1056
+ }
1057
+
1058
+ static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1059
+ CBS *contents) {
1060
+ if (contents == NULL) {
1061
+ return 1;
1062
+ }
1063
+
1064
+ if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) {
1065
+ return 0;
1066
+ }
1067
+
1068
+ ssl->s3->tmp.extended_master_secret = 1;
1069
+ return 1;
1070
+ }
1071
+
1072
+ static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1073
+ if (ssl->version == SSL3_VERSION || contents == NULL) {
1074
+ return 1;
1075
+ }
1076
+
1077
+ if (CBS_len(contents) != 0) {
1078
+ return 0;
1079
+ }
1080
+
1081
+ ssl->s3->tmp.extended_master_secret = 1;
1082
+ return 1;
1083
+ }
1084
+
1085
+ static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1086
+ if (!ssl->s3->tmp.extended_master_secret) {
1087
+ return 1;
1088
+ }
1089
+
1090
+ if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1091
+ !CBB_add_u16(out, 0 /* length */)) {
1092
+ return 0;
1093
+ }
1094
+
1095
+ return 1;
1096
+ }
1097
+
1098
+
1099
+ /* Session tickets.
1100
+ *
1101
+ * https://tools.ietf.org/html/rfc5077 */
1102
+
1103
+ static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1104
+ if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1105
+ return 1;
1106
+ }
1107
+
1108
+ const uint8_t *ticket_data = NULL;
1109
+ int ticket_len = 0;
1110
+
1111
+ /* Renegotiation does not participate in session resumption. However, still
1112
+ * advertise the extension to avoid potentially breaking servers which carry
1113
+ * over the state from the previous handshake, such as OpenSSL servers
1114
+ * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1115
+ if (!ssl->s3->initial_handshake_complete &&
1116
+ ssl->session != NULL &&
1117
+ ssl->session->tlsext_tick != NULL) {
1118
+ ticket_data = ssl->session->tlsext_tick;
1119
+ ticket_len = ssl->session->tlsext_ticklen;
1120
+ }
1121
+
1122
+ CBB ticket;
1123
+ if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1124
+ !CBB_add_u16_length_prefixed(out, &ticket) ||
1125
+ !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1126
+ !CBB_flush(out)) {
1127
+ return 0;
1128
+ }
1129
+
1130
+ return 1;
1131
+ }
1132
+
1133
+ static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1134
+ CBS *contents) {
1135
+ ssl->tlsext_ticket_expected = 0;
1136
+
1137
+ if (contents == NULL) {
1138
+ return 1;
1139
+ }
1140
+
1141
+ /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1142
+ * this function should never be called, even if the server tries to send the
1143
+ * extension. */
1144
+ assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1145
+
1146
+ if (CBS_len(contents) != 0) {
1147
+ return 0;
1148
+ }
1149
+
1150
+ ssl->tlsext_ticket_expected = 1;
1151
+ return 1;
1152
+ }
1153
+
1154
+ static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
1155
+ /* This function isn't used because the ticket extension from the client is
1156
+ * handled in ssl_session.c. */
1157
+ return 1;
1158
+ }
1159
+
1160
+ static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1161
+ if (!ssl->tlsext_ticket_expected) {
1162
+ return 1;
1163
+ }
1164
+
1165
+ /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1166
+ * true. */
1167
+ assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1168
+
1169
+ if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1170
+ !CBB_add_u16(out, 0 /* length */)) {
1171
+ return 0;
1172
+ }
1173
+
1174
+ return 1;
1175
+ }
1176
+
1177
+
1178
+ /* Signature Algorithms.
1179
+ *
1180
+ * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1181
+
1182
+ static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
1183
+ if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) {
1184
+ return 1;
1185
+ }
1186
+
1187
+ const uint8_t *sigalgs_data;
1188
+ const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1189
+
1190
+ CBB contents, sigalgs;
1191
+ if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1192
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1193
+ !CBB_add_u16_length_prefixed(&contents, &sigalgs) ||
1194
+ !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) ||
1195
+ !CBB_flush(out)) {
1196
+ return 0;
1197
+ }
1198
+
1199
+ return 1;
1200
+ }
1201
+
1202
+ static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1203
+ CBS *contents) {
1204
+ if (contents != NULL) {
1205
+ /* Servers MUST NOT send this extension. */
1206
+ *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1207
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER);
1208
+ return 0;
1209
+ }
1210
+
1211
+ return 1;
1212
+ }
1213
+
1214
+ static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1215
+ CBS *contents) {
1216
+ OPENSSL_free(ssl->cert->peer_sigalgs);
1217
+ ssl->cert->peer_sigalgs = NULL;
1218
+ ssl->cert->peer_sigalgslen = 0;
1219
+
1220
+ if (contents == NULL) {
1221
+ return 1;
1222
+ }
1223
+
1224
+ CBS supported_signature_algorithms;
1225
+ if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
1226
+ CBS_len(contents) != 0 ||
1227
+ CBS_len(&supported_signature_algorithms) == 0 ||
1228
+ !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1229
+ return 0;
1230
+ }
1231
+
1232
+ return 1;
1233
+ }
1234
+
1235
+ static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) {
1236
+ /* Servers MUST NOT send this extension. */
1237
+ return 1;
1238
+ }
1239
+
1240
+
1241
+ /* OCSP Stapling.
1242
+ *
1243
+ * https://tools.ietf.org/html/rfc6066#section-8 */
1244
+
1245
+ static void ext_ocsp_init(SSL *ssl) {
1246
+ ssl->s3->tmp.certificate_status_expected = 0;
1247
+ }
1248
+
1249
+ static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1250
+ if (!ssl->ocsp_stapling_enabled) {
1251
+ return 1;
1252
+ }
1253
+
1254
+ CBB contents;
1255
+ if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1256
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1257
+ !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1258
+ !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1259
+ !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1260
+ !CBB_flush(out)) {
1261
+ return 0;
1262
+ }
1263
+
1264
+ return 1;
1265
+ }
1266
+
1267
+ static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1268
+ CBS *contents) {
1269
+ if (contents == NULL) {
1270
+ return 1;
1271
+ }
1272
+
1273
+ if (CBS_len(contents) != 0) {
1274
+ return 0;
1275
+ }
1276
+
1277
+ ssl->s3->tmp.certificate_status_expected = 1;
1278
+ return 1;
1279
+ }
1280
+
1281
+ static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1282
+ CBS *contents) {
1283
+ if (contents == NULL) {
1284
+ return 1;
1285
+ }
1286
+
1287
+ uint8_t status_type;
1288
+ if (!CBS_get_u8(contents, &status_type)) {
1289
+ return 0;
1290
+ }
1291
+
1292
+ /* We cannot decide whether OCSP stapling will occur yet because the correct
1293
+ * SSL_CTX might not have been selected. */
1294
+ ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1295
+
1296
+ return 1;
1297
+ }
1298
+
1299
+ static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
1300
+ /* The extension shouldn't be sent when resuming sessions. */
1301
+ if (ssl->hit ||
1302
+ !ssl->s3->tmp.ocsp_stapling_requested ||
1303
+ ssl->ctx->ocsp_response_length == 0) {
1304
+ return 1;
1305
+ }
1306
+
1307
+ ssl->s3->tmp.certificate_status_expected = 1;
1308
+
1309
+ return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1310
+ CBB_add_u16(out, 0 /* length */);
1311
+ }
1312
+
1313
+
1314
+ /* Next protocol negotiation.
1315
+ *
1316
+ * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1317
+
1318
+ static void ext_npn_init(SSL *ssl) {
1319
+ ssl->s3->next_proto_neg_seen = 0;
1320
+ }
1321
+
1322
+ static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1323
+ if (ssl->s3->initial_handshake_complete ||
1324
+ ssl->ctx->next_proto_select_cb == NULL ||
1325
+ (ssl->options & SSL_OP_DISABLE_NPN) ||
1326
+ SSL_IS_DTLS(ssl)) {
1327
+ return 1;
1328
+ }
1329
+
1330
+ if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1331
+ !CBB_add_u16(out, 0 /* length */)) {
1332
+ return 0;
1333
+ }
1334
+
1335
+ return 1;
1336
+ }
1337
+
1338
+ static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1339
+ CBS *contents) {
1340
+ if (contents == NULL) {
1341
+ return 1;
1342
+ }
1343
+
1344
+ /* If any of these are false then we should never have sent the NPN
1345
+ * extension in the ClientHello and thus this function should never have been
1346
+ * called. */
1347
+ assert(!ssl->s3->initial_handshake_complete);
1348
+ assert(!SSL_IS_DTLS(ssl));
1349
+ assert(ssl->ctx->next_proto_select_cb != NULL);
1350
+ assert(!(ssl->options & SSL_OP_DISABLE_NPN));
1351
+
1352
+ if (ssl->s3->alpn_selected != NULL) {
1353
+ /* NPN and ALPN may not be negotiated in the same connection. */
1354
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1355
+ OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1356
+ return 0;
1357
+ }
1358
+
1359
+ const uint8_t *const orig_contents = CBS_data(contents);
1360
+ const size_t orig_len = CBS_len(contents);
1361
+
1362
+ while (CBS_len(contents) != 0) {
1363
+ CBS proto;
1364
+ if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1365
+ CBS_len(&proto) == 0) {
1366
+ return 0;
1367
+ }
1368
+ }
1369
+
1370
+ uint8_t *selected;
1371
+ uint8_t selected_len;
1372
+ if (ssl->ctx->next_proto_select_cb(
1373
+ ssl, &selected, &selected_len, orig_contents, orig_len,
1374
+ ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1375
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1376
+ return 0;
1377
+ }
1378
+
1379
+ OPENSSL_free(ssl->next_proto_negotiated);
1380
+ ssl->next_proto_negotiated = BUF_memdup(selected, selected_len);
1381
+ if (ssl->next_proto_negotiated == NULL) {
1382
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1383
+ return 0;
1384
+ }
1385
+
1386
+ ssl->next_proto_negotiated_len = selected_len;
1387
+ ssl->s3->next_proto_neg_seen = 1;
1388
+
1389
+ return 1;
1390
+ }
1391
+
1392
+ static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1393
+ CBS *contents) {
1394
+ if (contents != NULL && CBS_len(contents) != 0) {
1395
+ return 0;
1396
+ }
1397
+
1398
+ if (contents == NULL ||
1399
+ ssl->s3->initial_handshake_complete ||
1400
+ /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1401
+ * afterwards, parsing the ALPN extension will clear
1402
+ * |next_proto_neg_seen|. */
1403
+ ssl->s3->alpn_selected != NULL ||
1404
+ ssl->ctx->next_protos_advertised_cb == NULL ||
1405
+ SSL_IS_DTLS(ssl)) {
1406
+ return 1;
1407
+ }
1408
+
1409
+ ssl->s3->next_proto_neg_seen = 1;
1410
+ return 1;
1411
+ }
1412
+
1413
+ static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1414
+ /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1415
+ * parsed. */
1416
+ if (!ssl->s3->next_proto_neg_seen) {
1417
+ return 1;
1418
+ }
1419
+
1420
+ const uint8_t *npa;
1421
+ unsigned npa_len;
1422
+
1423
+ if (ssl->ctx->next_protos_advertised_cb(
1424
+ ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1425
+ SSL_TLSEXT_ERR_OK) {
1426
+ ssl->s3->next_proto_neg_seen = 0;
1427
+ return 1;
1428
+ }
1429
+
1430
+ CBB contents;
1431
+ if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1432
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1433
+ !CBB_add_bytes(&contents, npa, npa_len) ||
1434
+ !CBB_flush(out)) {
1435
+ return 0;
1436
+ }
1437
+
1438
+ return 1;
1439
+ }
1440
+
1441
+
1442
+ /* Signed certificate timestamps.
1443
+ *
1444
+ * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1445
+
1446
+ static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1447
+ if (!ssl->signed_cert_timestamps_enabled) {
1448
+ return 1;
1449
+ }
1450
+
1451
+ if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1452
+ !CBB_add_u16(out, 0 /* length */)) {
1453
+ return 0;
1454
+ }
1455
+
1456
+ return 1;
1457
+ }
1458
+
1459
+ static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1460
+ CBS *contents) {
1461
+ if (contents == NULL) {
1462
+ return 1;
1463
+ }
1464
+
1465
+ /* If this is false then we should never have sent the SCT extension in the
1466
+ * ClientHello and thus this function should never have been called. */
1467
+ assert(ssl->signed_cert_timestamps_enabled);
1468
+
1469
+ if (CBS_len(contents) == 0) {
1470
+ *out_alert = SSL_AD_DECODE_ERROR;
1471
+ return 0;
1472
+ }
1473
+
1474
+ /* Session resumption uses the original session information. */
1475
+ if (!ssl->hit &&
1476
+ !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list,
1477
+ &ssl->session->tlsext_signed_cert_timestamp_list_length)) {
1478
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1479
+ return 0;
1480
+ }
1481
+
1482
+ return 1;
1483
+ }
1484
+
1485
+ static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1486
+ CBS *contents) {
1487
+ return contents == NULL || CBS_len(contents) == 0;
1488
+ }
1489
+
1490
+ static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
1491
+ /* The extension shouldn't be sent when resuming sessions. */
1492
+ if (ssl->hit ||
1493
+ ssl->ctx->signed_cert_timestamp_list_length == 0) {
1494
+ return 1;
1495
+ }
1496
+
1497
+ CBB contents;
1498
+ return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1499
+ CBB_add_u16_length_prefixed(out, &contents) &&
1500
+ CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1501
+ ssl->ctx->signed_cert_timestamp_list_length) &&
1502
+ CBB_flush(out);
1503
+ }
1504
+
1505
+
1506
+ /* Application-level Protocol Negotiation.
1507
+ *
1508
+ * https://tools.ietf.org/html/rfc7301 */
1509
+
1510
+ static void ext_alpn_init(SSL *ssl) {
1511
+ OPENSSL_free(ssl->s3->alpn_selected);
1512
+ ssl->s3->alpn_selected = NULL;
1513
+ }
1514
+
1515
+ static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1516
+ if (ssl->alpn_client_proto_list == NULL ||
1517
+ ssl->s3->initial_handshake_complete) {
1518
+ return 1;
1519
+ }
1520
+
1521
+ CBB contents, proto_list;
1522
+ if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1523
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1524
+ !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1525
+ !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1526
+ ssl->alpn_client_proto_list_len) ||
1527
+ !CBB_flush(out)) {
1528
+ return 0;
1529
+ }
1530
+
1531
+ return 1;
1532
+ }
1533
+
1534
+ static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1535
+ CBS *contents) {
1536
+ if (contents == NULL) {
1537
+ return 1;
1538
+ }
1539
+
1540
+ assert(!ssl->s3->initial_handshake_complete);
1541
+ assert(ssl->alpn_client_proto_list != NULL);
1542
+
1543
+ if (ssl->s3->next_proto_neg_seen) {
1544
+ /* NPN and ALPN may not be negotiated in the same connection. */
1545
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1546
+ OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1547
+ return 0;
1548
+ }
1549
+
1550
+ /* The extension data consists of a ProtocolNameList which must have
1551
+ * exactly one ProtocolName. Each of these is length-prefixed. */
1552
+ CBS protocol_name_list, protocol_name;
1553
+ if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1554
+ CBS_len(contents) != 0 ||
1555
+ !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1556
+ /* Empty protocol names are forbidden. */
1557
+ CBS_len(&protocol_name) == 0 ||
1558
+ CBS_len(&protocol_name_list) != 0) {
1559
+ return 0;
1560
+ }
1561
+
1562
+ if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1563
+ &ssl->s3->alpn_selected_len)) {
1564
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1565
+ return 0;
1566
+ }
1567
+
1568
+ return 1;
1569
+ }
1570
+
1571
+ static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1572
+ CBS *contents) {
1573
+ if (contents == NULL) {
1574
+ return 1;
1575
+ }
1576
+
1577
+ if (ssl->ctx->alpn_select_cb == NULL ||
1578
+ ssl->s3->initial_handshake_complete) {
1579
+ return 1;
1580
+ }
1581
+
1582
+ /* ALPN takes precedence over NPN. */
1583
+ ssl->s3->next_proto_neg_seen = 0;
1584
+
1585
+ CBS protocol_name_list;
1586
+ if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1587
+ CBS_len(contents) != 0 ||
1588
+ CBS_len(&protocol_name_list) < 2) {
1589
+ return 0;
1590
+ }
1591
+
1592
+ /* Validate the protocol list. */
1593
+ CBS protocol_name_list_copy = protocol_name_list;
1594
+ while (CBS_len(&protocol_name_list_copy) > 0) {
1595
+ CBS protocol_name;
1596
+
1597
+ if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1598
+ /* Empty protocol names are forbidden. */
1599
+ CBS_len(&protocol_name) == 0) {
1600
+ return 0;
1601
+ }
1602
+ }
1603
+
1604
+ const uint8_t *selected;
1605
+ uint8_t selected_len;
1606
+ if (ssl->ctx->alpn_select_cb(
1607
+ ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1608
+ CBS_len(&protocol_name_list),
1609
+ ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1610
+ OPENSSL_free(ssl->s3->alpn_selected);
1611
+ ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1612
+ if (ssl->s3->alpn_selected == NULL) {
1613
+ *out_alert = SSL_AD_INTERNAL_ERROR;
1614
+ return 0;
1615
+ }
1616
+ ssl->s3->alpn_selected_len = selected_len;
1617
+ }
1618
+
1619
+ return 1;
1620
+ }
1621
+
1622
+ static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1623
+ if (ssl->s3->alpn_selected == NULL) {
1624
+ return 1;
1625
+ }
1626
+
1627
+ CBB contents, proto_list, proto;
1628
+ if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1629
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1630
+ !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1631
+ !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
1632
+ !CBB_add_bytes(&proto, ssl->s3->alpn_selected, ssl->s3->alpn_selected_len) ||
1633
+ !CBB_flush(out)) {
1634
+ return 0;
1635
+ }
1636
+
1637
+ return 1;
1638
+ }
1639
+
1640
+
1641
+ /* Channel ID.
1642
+ *
1643
+ * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1644
+
1645
+ static void ext_channel_id_init(SSL *ssl) {
1646
+ ssl->s3->tlsext_channel_id_valid = 0;
1647
+ }
1648
+
1649
+ static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1650
+ if (!ssl->tlsext_channel_id_enabled ||
1651
+ SSL_IS_DTLS(ssl)) {
1652
+ return 1;
1653
+ }
1654
+
1655
+ if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1656
+ !CBB_add_u16(out, 0 /* length */)) {
1657
+ return 0;
1658
+ }
1659
+
1660
+ return 1;
1661
+ }
1662
+
1663
+ static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1664
+ CBS *contents) {
1665
+ if (contents == NULL) {
1666
+ return 1;
1667
+ }
1668
+
1669
+ assert(!SSL_IS_DTLS(ssl));
1670
+ assert(ssl->tlsext_channel_id_enabled);
1671
+
1672
+ if (CBS_len(contents) != 0) {
1673
+ return 0;
1674
+ }
1675
+
1676
+ ssl->s3->tlsext_channel_id_valid = 1;
1677
+ return 1;
1678
+ }
1679
+
1680
+ static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1681
+ CBS *contents) {
1682
+ if (contents == NULL ||
1683
+ !ssl->tlsext_channel_id_enabled ||
1684
+ SSL_IS_DTLS(ssl)) {
1685
+ return 1;
1686
+ }
1687
+
1688
+ if (CBS_len(contents) != 0) {
1689
+ return 0;
1690
+ }
1691
+
1692
+ ssl->s3->tlsext_channel_id_valid = 1;
1693
+ return 1;
1694
+ }
1695
+
1696
+ static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
1697
+ if (!ssl->s3->tlsext_channel_id_valid) {
1698
+ return 1;
1699
+ }
1700
+
1701
+ if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1702
+ !CBB_add_u16(out, 0 /* length */)) {
1703
+ return 0;
1704
+ }
1705
+
1706
+ return 1;
1707
+ }
1708
+
1709
+
1710
+ /* Secure Real-time Transport Protocol (SRTP) extension.
1711
+ *
1712
+ * https://tools.ietf.org/html/rfc5764 */
1713
+
1714
+
1715
+ static void ext_srtp_init(SSL *ssl) {
1716
+ ssl->srtp_profile = NULL;
1717
+ }
1718
+
1719
+ static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1720
+ STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1721
+ if (profiles == NULL) {
1722
+ return 1;
1723
+ }
1724
+ const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1725
+ if (num_profiles == 0) {
1726
+ return 1;
1727
+ }
1728
+
1729
+ CBB contents, profile_ids;
1730
+ if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1731
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1732
+ !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1733
+ return 0;
1734
+ }
1735
+
1736
+ size_t i;
1737
+ for (i = 0; i < num_profiles; i++) {
1738
+ if (!CBB_add_u16(&profile_ids,
1739
+ sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1740
+ return 0;
1741
+ }
1742
+ }
1743
+
1744
+ if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1745
+ !CBB_flush(out)) {
1746
+ return 0;
1747
+ }
1748
+
1749
+ return 1;
1750
+ }
1751
+
1752
+ static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1753
+ CBS *contents) {
1754
+ if (contents == NULL) {
1755
+ return 1;
1756
+ }
1757
+
1758
+ /* The extension consists of a u16-prefixed profile ID list containing a
1759
+ * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1760
+ *
1761
+ * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1762
+ CBS profile_ids, srtp_mki;
1763
+ uint16_t profile_id;
1764
+ if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1765
+ !CBS_get_u16(&profile_ids, &profile_id) ||
1766
+ CBS_len(&profile_ids) != 0 ||
1767
+ !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1768
+ CBS_len(contents) != 0) {
1769
+ OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1770
+ return 0;
1771
+ }
1772
+
1773
+ if (CBS_len(&srtp_mki) != 0) {
1774
+ /* Must be no MKI, since we never offer one. */
1775
+ OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1776
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1777
+ return 0;
1778
+ }
1779
+
1780
+ STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1781
+
1782
+ /* Check to see if the server gave us something we support (and presumably
1783
+ * offered). */
1784
+ size_t i;
1785
+ for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1786
+ const SRTP_PROTECTION_PROFILE *profile =
1787
+ sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1788
+
1789
+ if (profile->id == profile_id) {
1790
+ ssl->srtp_profile = profile;
1791
+ return 1;
1792
+ }
1793
+ }
1794
+
1795
+ OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1796
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1797
+ return 0;
1798
+ }
1799
+
1800
+ static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1801
+ CBS *contents) {
1802
+ if (contents == NULL) {
1803
+ return 1;
1804
+ }
1805
+
1806
+ CBS profile_ids, srtp_mki;
1807
+ if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1808
+ CBS_len(&profile_ids) < 2 ||
1809
+ !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1810
+ CBS_len(contents) != 0) {
1811
+ OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1812
+ return 0;
1813
+ }
1814
+ /* Discard the MKI value for now. */
1815
+
1816
+ const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1817
+ SSL_get_srtp_profiles(ssl);
1818
+
1819
+ /* Pick the server's most preferred profile. */
1820
+ size_t i;
1821
+ for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1822
+ const SRTP_PROTECTION_PROFILE *server_profile =
1823
+ sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1824
+
1825
+ CBS profile_ids_tmp;
1826
+ CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1827
+
1828
+ while (CBS_len(&profile_ids_tmp) > 0) {
1829
+ uint16_t profile_id;
1830
+ if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1831
+ return 0;
1832
+ }
1833
+
1834
+ if (server_profile->id == profile_id) {
1835
+ ssl->srtp_profile = server_profile;
1836
+ return 1;
1837
+ }
1838
+ }
1839
+ }
1840
+
1841
+ return 1;
1842
+ }
1843
+
1844
+ static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1845
+ if (ssl->srtp_profile == NULL) {
1846
+ return 1;
1847
+ }
1848
+
1849
+ CBB contents, profile_ids;
1850
+ if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1851
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1852
+ !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1853
+ !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1854
+ !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1855
+ !CBB_flush(out)) {
1856
+ return 0;
1857
+ }
1858
+
1859
+ return 1;
1860
+ }
1861
+
1862
+
1863
+ /* EC point formats.
1864
+ *
1865
+ * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1866
+
1867
+ static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
1868
+ if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) {
1869
+ return 0;
1870
+ }
1871
+
1872
+ const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1873
+
1874
+ size_t i;
1875
+ for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1876
+ const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1877
+
1878
+ const uint32_t alg_k = cipher->algorithm_mkey;
1879
+ const uint32_t alg_a = cipher->algorithm_auth;
1880
+ if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1881
+ return 1;
1882
+ }
1883
+ }
1884
+
1885
+ return 0;
1886
+ }
1887
+
1888
+ static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
1889
+ CBB contents, formats;
1890
+ if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1891
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1892
+ !CBB_add_u8_length_prefixed(&contents, &formats) ||
1893
+ !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
1894
+ !CBB_flush(out)) {
1895
+ return 0;
1896
+ }
1897
+
1898
+ return 1;
1899
+ }
1900
+
1901
+ static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1902
+ if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1903
+ return 1;
1904
+ }
1905
+
1906
+ return ext_ec_point_add_extension(ssl, out);
1907
+ }
1908
+
1909
+ static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1910
+ CBS *contents) {
1911
+ if (contents == NULL) {
1912
+ return 1;
1913
+ }
1914
+
1915
+ CBS ec_point_format_list;
1916
+ if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1917
+ CBS_len(contents) != 0) {
1918
+ return 0;
1919
+ }
1920
+
1921
+ /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1922
+ * point format. */
1923
+ if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1924
+ CBS_len(&ec_point_format_list)) == NULL) {
1925
+ *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1926
+ return 0;
1927
+ }
1928
+
1929
+ return 1;
1930
+ }
1931
+
1932
+ static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1933
+ CBS *contents) {
1934
+ return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1935
+ }
1936
+
1937
+ static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
1938
+ const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1939
+ const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1940
+ const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1941
+
1942
+ if (!using_ecc) {
1943
+ return 1;
1944
+ }
1945
+
1946
+ return ext_ec_point_add_extension(ssl, out);
1947
+ }
1948
+
1949
+
1950
+ /* EC supported curves.
1951
+ *
1952
+ * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1953
+
1954
+ static void ext_ec_curves_init(SSL *ssl) {
1955
+ OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
1956
+ ssl->s3->tmp.peer_ellipticcurvelist = NULL;
1957
+ ssl->s3->tmp.peer_ellipticcurvelist_length = 0;
1958
+ }
1959
+
1960
+ static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) {
1961
+ if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1962
+ return 1;
1963
+ }
1964
+
1965
+ CBB contents, curves_bytes;
1966
+ if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) ||
1967
+ !CBB_add_u16_length_prefixed(out, &contents) ||
1968
+ !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) {
1969
+ return 0;
1970
+ }
1971
+
1972
+ const uint16_t *curves;
1973
+ size_t curves_len;
1974
+ tls1_get_curvelist(ssl, 0, &curves, &curves_len);
1975
+
1976
+ size_t i;
1977
+ for (i = 0; i < curves_len; i++) {
1978
+ if (!CBB_add_u16(&curves_bytes, curves[i])) {
1979
+ return 0;
1980
+ }
1981
+ }
1982
+
1983
+ return CBB_flush(out);
1984
+ }
1985
+
1986
+ static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1987
+ CBS *contents) {
1988
+ /* This extension is not expected to be echoed by servers and is ignored. */
1989
+ return 1;
1990
+ }
1991
+
1992
+ static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1993
+ CBS *contents) {
1994
+ if (contents == NULL) {
1995
+ return 1;
1996
+ }
1997
+
1998
+ CBS elliptic_curve_list;
1999
+ if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) ||
2000
+ CBS_len(&elliptic_curve_list) == 0 ||
2001
+ (CBS_len(&elliptic_curve_list) & 1) != 0 ||
2002
+ CBS_len(contents) != 0) {
2003
+ return 0;
2004
+ }
2005
+
2006
+ ssl->s3->tmp.peer_ellipticcurvelist =
2007
+ (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
2008
+
2009
+ if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) {
2010
+ *out_alert = SSL_AD_INTERNAL_ERROR;
2011
+ return 0;
2012
+ }
2013
+
2014
+ const size_t num_curves = CBS_len(&elliptic_curve_list) / 2;
2015
+ size_t i;
2016
+ for (i = 0; i < num_curves; i++) {
2017
+ if (!CBS_get_u16(&elliptic_curve_list,
2018
+ &ssl->s3->tmp.peer_ellipticcurvelist[i])) {
2019
+ goto err;
2020
+ }
2021
+ }
2022
+
2023
+ assert(CBS_len(&elliptic_curve_list) == 0);
2024
+ ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves;
2025
+
2026
+ return 1;
2027
+
2028
+ err:
2029
+ OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist);
2030
+ ssl->s3->tmp.peer_ellipticcurvelist = NULL;
2031
+ *out_alert = SSL_AD_INTERNAL_ERROR;
2032
+ return 0;
2033
+ }
2034
+
2035
+ static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) {
2036
+ /* Servers don't echo this extension. */
2037
+ return 1;
2038
+ }
2039
+
2040
+
2041
+ /* kExtensions contains all the supported extensions. */
2042
+ static const struct tls_extension kExtensions[] = {
2043
+ {
2044
+ /* The renegotiation extension must always be at index zero because the
2045
+ * |received| and |sent| bitsets need to be tweaked when the "extension" is
2046
+ * sent as an SCSV. */
2047
+ TLSEXT_TYPE_renegotiate,
2048
+ NULL,
2049
+ ext_ri_add_clienthello,
2050
+ ext_ri_parse_serverhello,
2051
+ ext_ri_parse_clienthello,
2052
+ ext_ri_add_serverhello,
2053
+ },
2054
+ {
2055
+ TLSEXT_TYPE_server_name,
2056
+ ext_sni_init,
2057
+ ext_sni_add_clienthello,
2058
+ ext_sni_parse_serverhello,
2059
+ ext_sni_parse_clienthello,
2060
+ ext_sni_add_serverhello,
2061
+ },
2062
+ {
2063
+ TLSEXT_TYPE_extended_master_secret,
2064
+ ext_ems_init,
2065
+ ext_ems_add_clienthello,
2066
+ ext_ems_parse_serverhello,
2067
+ ext_ems_parse_clienthello,
2068
+ ext_ems_add_serverhello,
2069
+ },
2070
+ {
2071
+ TLSEXT_TYPE_session_ticket,
2072
+ NULL,
2073
+ ext_ticket_add_clienthello,
2074
+ ext_ticket_parse_serverhello,
2075
+ ext_ticket_parse_clienthello,
2076
+ ext_ticket_add_serverhello,
2077
+ },
2078
+ {
2079
+ TLSEXT_TYPE_signature_algorithms,
2080
+ NULL,
2081
+ ext_sigalgs_add_clienthello,
2082
+ ext_sigalgs_parse_serverhello,
2083
+ ext_sigalgs_parse_clienthello,
2084
+ ext_sigalgs_add_serverhello,
2085
+ },
2086
+ {
2087
+ TLSEXT_TYPE_status_request,
2088
+ ext_ocsp_init,
2089
+ ext_ocsp_add_clienthello,
2090
+ ext_ocsp_parse_serverhello,
2091
+ ext_ocsp_parse_clienthello,
2092
+ ext_ocsp_add_serverhello,
2093
+ },
2094
+ {
2095
+ TLSEXT_TYPE_next_proto_neg,
2096
+ ext_npn_init,
2097
+ ext_npn_add_clienthello,
2098
+ ext_npn_parse_serverhello,
2099
+ ext_npn_parse_clienthello,
2100
+ ext_npn_add_serverhello,
2101
+ },
2102
+ {
2103
+ TLSEXT_TYPE_certificate_timestamp,
2104
+ NULL,
2105
+ ext_sct_add_clienthello,
2106
+ ext_sct_parse_serverhello,
2107
+ ext_sct_parse_clienthello,
2108
+ ext_sct_add_serverhello,
2109
+ },
2110
+ {
2111
+ TLSEXT_TYPE_application_layer_protocol_negotiation,
2112
+ ext_alpn_init,
2113
+ ext_alpn_add_clienthello,
2114
+ ext_alpn_parse_serverhello,
2115
+ ext_alpn_parse_clienthello,
2116
+ ext_alpn_add_serverhello,
2117
+ },
2118
+ {
2119
+ TLSEXT_TYPE_channel_id,
2120
+ ext_channel_id_init,
2121
+ ext_channel_id_add_clienthello,
2122
+ ext_channel_id_parse_serverhello,
2123
+ ext_channel_id_parse_clienthello,
2124
+ ext_channel_id_add_serverhello,
2125
+ },
2126
+ {
2127
+ TLSEXT_TYPE_srtp,
2128
+ ext_srtp_init,
2129
+ ext_srtp_add_clienthello,
2130
+ ext_srtp_parse_serverhello,
2131
+ ext_srtp_parse_clienthello,
2132
+ ext_srtp_add_serverhello,
2133
+ },
2134
+ {
2135
+ TLSEXT_TYPE_ec_point_formats,
2136
+ NULL,
2137
+ ext_ec_point_add_clienthello,
2138
+ ext_ec_point_parse_serverhello,
2139
+ ext_ec_point_parse_clienthello,
2140
+ ext_ec_point_add_serverhello,
2141
+ },
2142
+ {
2143
+ TLSEXT_TYPE_elliptic_curves,
2144
+ ext_ec_curves_init,
2145
+ ext_ec_curves_add_clienthello,
2146
+ ext_ec_curves_parse_serverhello,
2147
+ ext_ec_curves_parse_clienthello,
2148
+ ext_ec_curves_add_serverhello,
2149
+ },
2150
+ };
2151
+
2152
+ #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2153
+
2154
+ OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2155
+ sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
2156
+ too_many_extensions_for_sent_bitset);
2157
+ OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2158
+ sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2159
+ 8,
2160
+ too_many_extensions_for_received_bitset);
2161
+
2162
+ static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2163
+ uint16_t value) {
2164
+ unsigned i;
2165
+ for (i = 0; i < kNumExtensions; i++) {
2166
+ if (kExtensions[i].value == value) {
2167
+ *out_index = i;
2168
+ return &kExtensions[i];
2169
+ }
2170
+ }
2171
+
2172
+ return NULL;
2173
+ }
2174
+
2175
+ int SSL_extension_supported(unsigned extension_value) {
2176
+ uint32_t index;
2177
+ return extension_value == TLSEXT_TYPE_padding ||
2178
+ tls_extension_find(&index, extension_value) != NULL;
2179
+ }
2180
+
2181
+ int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
2182
+ /* don't add extensions for SSLv3 unless doing secure renegotiation */
2183
+ if (ssl->client_version == SSL3_VERSION &&
2184
+ !ssl->s3->send_connection_binding) {
2185
+ return 1;
2186
+ }
2187
+
2188
+ CBB extensions;
2189
+ if (!CBB_add_u16_length_prefixed(out, &extensions)) {
2190
+ goto err;
2191
+ }
2192
+
2193
+ ssl->s3->tmp.extensions.sent = 0;
2194
+ ssl->s3->tmp.custom_extensions.sent = 0;
2195
+
2196
+ size_t i;
2197
+ for (i = 0; i < kNumExtensions; i++) {
2198
+ if (kExtensions[i].init != NULL) {
2199
+ kExtensions[i].init(ssl);
2200
+ }
2201
+ }
2202
+
2203
+ for (i = 0; i < kNumExtensions; i++) {
2204
+ const size_t len_before = CBB_len(&extensions);
2205
+ if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
2206
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2207
+ ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2208
+ goto err;
2209
+ }
2210
+
2211
+ if (CBB_len(&extensions) != len_before) {
2212
+ ssl->s3->tmp.extensions.sent |= (1u << i);
2213
+ }
2214
+ }
2215
+
2216
+ if (!custom_ext_add_clienthello(ssl, &extensions)) {
2217
+ goto err;
2218
+ }
2219
+
2220
+ if (!SSL_IS_DTLS(ssl)) {
2221
+ header_len += 2 + CBB_len(&extensions);
2222
+ if (header_len > 0xff && header_len < 0x200) {
2223
+ /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
2224
+ *
2225
+ * NB: because this code works out the length of all existing extensions
2226
+ * it MUST always appear last. */
2227
+ size_t padding_len = 0x200 - header_len;
2228
+ /* Extensions take at least four bytes to encode. Always include least
2229
+ * one byte of data if including the extension. WebSphere Application
2230
+ * Server 7.0 is intolerant to the last extension being zero-length. */
2231
+ if (padding_len >= 4 + 1) {
2232
+ padding_len -= 4;
2233
+ } else {
2234
+ padding_len = 1;
2235
+ }
2236
+
2237
+ uint8_t *padding_bytes;
2238
+ if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2239
+ !CBB_add_u16(&extensions, padding_len) ||
2240
+ !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2241
+ goto err;
2242
+ }
2243
+
2244
+ memset(padding_bytes, 0, padding_len);
2245
+ }
2246
+ }
2247
+
2248
+ /* Discard empty extensions blocks. */
2249
+ if (CBB_len(&extensions) == 0) {
2250
+ CBB_discard_child(out);
2251
+ }
2252
+
2253
+ return CBB_flush(out);
2254
+
2255
+ err:
2256
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2257
+ return 0;
2258
+ }
2259
+
2260
+ int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
2261
+ CBB extensions;
2262
+ if (!CBB_add_u16_length_prefixed(out, &extensions)) {
2263
+ goto err;
2264
+ }
2265
+
2266
+ unsigned i;
2267
+ for (i = 0; i < kNumExtensions; i++) {
2268
+ if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
2269
+ /* Don't send extensions that were not received. */
2270
+ continue;
2271
+ }
2272
+
2273
+ if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
2274
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2275
+ ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2276
+ goto err;
2277
+ }
2278
+ }
2279
+
2280
+ if (!custom_ext_add_serverhello(ssl, &extensions)) {
2281
+ goto err;
2282
+ }
2283
+
2284
+ /* Discard empty extensions blocks. */
2285
+ if (CBB_len(&extensions) == 0) {
2286
+ CBB_discard_child(out);
2287
+ }
2288
+
2289
+ return CBB_flush(out);
2290
+
2291
+ err:
2292
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2293
+ return 0;
2294
+ }
2295
+
2296
+ static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
2297
+ size_t i;
2298
+ for (i = 0; i < kNumExtensions; i++) {
2299
+ if (kExtensions[i].init != NULL) {
2300
+ kExtensions[i].init(s);
2301
+ }
2302
+ }
2303
+
2304
+ s->s3->tmp.extensions.received = 0;
2305
+ s->s3->tmp.custom_extensions.received = 0;
2306
+ /* The renegotiation extension must always be at index zero because the
2307
+ * |received| and |sent| bitsets need to be tweaked when the "extension" is
2308
+ * sent as an SCSV. */
2309
+ assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
2310
+
2311
+ /* There may be no extensions. */
2312
+ if (CBS_len(cbs) != 0) {
2313
+ /* Decode the extensions block and check it is valid. */
2314
+ CBS extensions;
2315
+ if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2316
+ !tls1_check_duplicate_extensions(&extensions)) {
2317
+ *out_alert = SSL_AD_DECODE_ERROR;
2318
+ return 0;
2319
+ }
2320
+
2321
+ while (CBS_len(&extensions) != 0) {
2322
+ uint16_t type;
2323
+ CBS extension;
2324
+
2325
+ /* Decode the next extension. */
2326
+ if (!CBS_get_u16(&extensions, &type) ||
2327
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2328
+ *out_alert = SSL_AD_DECODE_ERROR;
2329
+ return 0;
2330
+ }
2331
+
2332
+ /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2333
+ * ambiguous. Ignore all but the renegotiation_info extension. */
2334
+ if (s->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2335
+ continue;
2336
+ }
2337
+
2338
+ unsigned ext_index;
2339
+ const struct tls_extension *const ext =
2340
+ tls_extension_find(&ext_index, type);
2341
+
2342
+ if (ext == NULL) {
2343
+ if (!custom_ext_parse_clienthello(s, out_alert, type, &extension)) {
2344
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2345
+ return 0;
2346
+ }
2347
+ continue;
2348
+ }
2349
+
2350
+ s->s3->tmp.extensions.received |= (1u << ext_index);
2351
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2352
+ if (!ext->parse_clienthello(s, &alert, &extension)) {
2353
+ *out_alert = alert;
2354
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2355
+ ERR_add_error_dataf("extension: %u", (unsigned)type);
2356
+ return 0;
2357
+ }
2358
+ }
2359
+ }
2360
+
2361
+ for (i = 0; i < kNumExtensions; i++) {
2362
+ if (!(s->s3->tmp.extensions.received & (1u << i))) {
2363
+ /* Extension wasn't observed so call the callback with a NULL
2364
+ * parameter. */
2365
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2366
+ if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) {
2367
+ OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2368
+ ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2369
+ *out_alert = alert;
2370
+ return 0;
2371
+ }
2372
+ }
2373
+ }
2374
+
2375
+ return 1;
2376
+ }
2377
+
2378
+ int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
2379
+ int alert = -1;
2380
+ if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
2381
+ ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2382
+ return 0;
2383
+ }
2384
+
2385
+ if (ssl_check_clienthello_tlsext(s) <= 0) {
2386
+ OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
2387
+ return 0;
2388
+ }
2389
+
2390
+ return 1;
2391
+ }
2392
+
2393
+ OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2394
+
2395
+ static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
2396
+ uint32_t received = 0;
2397
+
2398
+ if (CBS_len(cbs) != 0) {
2399
+ /* Decode the extensions block and check it is valid. */
2400
+ CBS extensions;
2401
+ if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2402
+ !tls1_check_duplicate_extensions(&extensions)) {
2403
+ *out_alert = SSL_AD_DECODE_ERROR;
2404
+ return 0;
2405
+ }
2406
+
2407
+
2408
+ while (CBS_len(&extensions) != 0) {
2409
+ uint16_t type;
2410
+ CBS extension;
2411
+
2412
+ /* Decode the next extension. */
2413
+ if (!CBS_get_u16(&extensions, &type) ||
2414
+ !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2415
+ *out_alert = SSL_AD_DECODE_ERROR;
2416
+ return 0;
2417
+ }
2418
+
2419
+ unsigned ext_index;
2420
+ const struct tls_extension *const ext =
2421
+ tls_extension_find(&ext_index, type);
2422
+
2423
+ if (ext == NULL) {
2424
+ if (!custom_ext_parse_serverhello(s, out_alert, type, &extension)) {
2425
+ return 0;
2426
+ }
2427
+ continue;
2428
+ }
2429
+
2430
+ if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) {
2431
+ /* If the extension was never sent then it is illegal. */
2432
+ OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2433
+ ERR_add_error_dataf("extension :%u", (unsigned)type);
2434
+ *out_alert = SSL_AD_DECODE_ERROR;
2435
+ return 0;
2436
+ }
2437
+
2438
+ received |= (1u << ext_index);
2439
+
2440
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2441
+ if (!ext->parse_serverhello(s, &alert, &extension)) {
2442
+ OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2443
+ ERR_add_error_dataf("extension: %u", (unsigned)type);
2444
+ *out_alert = alert;
2445
+ return 0;
2446
+ }
2447
+ }
2448
+ }
2449
+
2450
+ size_t i;
2451
+ for (i = 0; i < kNumExtensions; i++) {
2452
+ if (!(received & (1u << i))) {
2453
+ /* Extension wasn't observed so call the callback with a NULL
2454
+ * parameter. */
2455
+ uint8_t alert = SSL_AD_DECODE_ERROR;
2456
+ if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) {
2457
+ OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2458
+ ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2459
+ *out_alert = alert;
2460
+ return 0;
2461
+ }
2462
+ }
2463
+ }
2464
+
2465
+ return 1;
2466
+ }
2467
+
2468
+ static int ssl_check_clienthello_tlsext(SSL *s) {
2469
+ int ret = SSL_TLSEXT_ERR_NOACK;
2470
+ int al = SSL_AD_UNRECOGNIZED_NAME;
2471
+
2472
+ /* The handling of the ECPointFormats extension is done elsewhere, namely in
2473
+ * ssl3_choose_cipher in s3_lib.c. */
2474
+
2475
+ if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2476
+ ret = s->ctx->tlsext_servername_callback(s, &al,
2477
+ s->ctx->tlsext_servername_arg);
2478
+ } else if (s->initial_ctx != NULL &&
2479
+ s->initial_ctx->tlsext_servername_callback != 0) {
2480
+ ret = s->initial_ctx->tlsext_servername_callback(
2481
+ s, &al, s->initial_ctx->tlsext_servername_arg);
2482
+ }
2483
+
2484
+ switch (ret) {
2485
+ case SSL_TLSEXT_ERR_ALERT_FATAL:
2486
+ ssl3_send_alert(s, SSL3_AL_FATAL, al);
2487
+ return -1;
2488
+
2489
+ case SSL_TLSEXT_ERR_ALERT_WARNING:
2490
+ ssl3_send_alert(s, SSL3_AL_WARNING, al);
2491
+ return 1;
2492
+
2493
+ case SSL_TLSEXT_ERR_NOACK:
2494
+ s->s3->tmp.should_ack_sni = 0;
2495
+ return 1;
2496
+
2497
+ default:
2498
+ return 1;
2499
+ }
2500
+ }
2501
+
2502
+ static int ssl_check_serverhello_tlsext(SSL *s) {
2503
+ int ret = SSL_TLSEXT_ERR_OK;
2504
+ int al = SSL_AD_UNRECOGNIZED_NAME;
2505
+
2506
+ if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2507
+ ret = s->ctx->tlsext_servername_callback(s, &al,
2508
+ s->ctx->tlsext_servername_arg);
2509
+ } else if (s->initial_ctx != NULL &&
2510
+ s->initial_ctx->tlsext_servername_callback != 0) {
2511
+ ret = s->initial_ctx->tlsext_servername_callback(
2512
+ s, &al, s->initial_ctx->tlsext_servername_arg);
2513
+ }
2514
+
2515
+ switch (ret) {
2516
+ case SSL_TLSEXT_ERR_ALERT_FATAL:
2517
+ ssl3_send_alert(s, SSL3_AL_FATAL, al);
2518
+ return -1;
2519
+
2520
+ case SSL_TLSEXT_ERR_ALERT_WARNING:
2521
+ ssl3_send_alert(s, SSL3_AL_WARNING, al);
2522
+ return 1;
2523
+
2524
+ default:
2525
+ return 1;
2526
+ }
2527
+ }
2528
+
2529
+ int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2530
+ int alert = -1;
2531
+ if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2532
+ ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2533
+ return 0;
2534
+ }
2535
+
2536
+ if (ssl_check_serverhello_tlsext(s) <= 0) {
2537
+ OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
2538
+ return 0;
2539
+ }
2540
+
2541
+ return 1;
2542
+ }
2543
+
2544
+ int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
2545
+ int *out_send_ticket, const uint8_t *ticket,
2546
+ size_t ticket_len, const uint8_t *session_id,
2547
+ size_t session_id_len) {
2548
+ int ret = 1; /* Most errors are non-fatal. */
2549
+ SSL_CTX *ssl_ctx = ssl->initial_ctx;
2550
+ uint8_t *plaintext = NULL;
2551
+
2552
+ HMAC_CTX hmac_ctx;
2553
+ HMAC_CTX_init(&hmac_ctx);
2554
+ EVP_CIPHER_CTX cipher_ctx;
2555
+ EVP_CIPHER_CTX_init(&cipher_ctx);
2556
+
2557
+ *out_send_ticket = 0;
2558
+ *out_session = NULL;
2559
+
2560
+ if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2561
+ goto done;
2562
+ }
2563
+
2564
+ if (ticket_len == 0) {
2565
+ /* The client will accept a ticket but doesn't currently have one. */
2566
+ *out_send_ticket = 1;
2567
+ goto done;
2568
+ }
2569
+
2570
+ /* Ensure there is room for the key name and the largest IV
2571
+ * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2572
+ * the maximum IV length should be well under the minimum size for the
2573
+ * session material and HMAC. */
2574
+ if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2575
+ goto done;
2576
+ }
2577
+ const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
2578
+
2579
+ if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
2580
+ int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */,
2581
+ (uint8_t*)iv, &cipher_ctx, &hmac_ctx,
2582
+ 0 /* decrypt */);
2583
+ if (cb_ret < 0) {
2584
+ ret = 0;
2585
+ goto done;
2586
+ }
2587
+ if (cb_ret == 0) {
2588
+ goto done;
2589
+ }
2590
+ if (cb_ret == 2) {
2591
+ *out_send_ticket = 1;
2592
+ }
2593
+ } else {
2594
+ /* Check the key name matches. */
2595
+ if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2596
+ SSL_TICKET_KEY_NAME_LEN) != 0) {
2597
+ goto done;
2598
+ }
2599
+ if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2600
+ sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
2601
+ NULL) ||
2602
+ !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2603
+ ssl_ctx->tlsext_tick_aes_key, iv)) {
2604
+ ret = 0;
2605
+ goto done;
2606
+ }
2607
+ }
2608
+ size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
2609
+
2610
+ /* Check the MAC at the end of the ticket. */
2611
+ uint8_t mac[EVP_MAX_MD_SIZE];
2612
+ size_t mac_len = HMAC_size(&hmac_ctx);
2613
+ if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
2614
+ /* The ticket must be large enough for key name, IV, data, and MAC. */
2615
+ goto done;
2616
+ }
2617
+ HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2618
+ HMAC_Final(&hmac_ctx, mac, NULL);
2619
+ if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2620
+ goto done;
2621
+ }
2622
+
2623
+ /* Decrypt the session data. */
2624
+ const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2625
+ size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2626
+ mac_len;
2627
+ plaintext = OPENSSL_malloc(ciphertext_len);
2628
+ if (plaintext == NULL) {
2629
+ ret = 0;
2630
+ goto done;
2631
+ }
2632
+ if (ciphertext_len >= INT_MAX) {
2633
+ goto done;
2634
+ }
2635
+ int len1, len2;
2636
+ if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2637
+ (int)ciphertext_len) ||
2638
+ !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2639
+ ERR_clear_error(); /* Don't leave an error on the queue. */
2640
+ goto done;
2641
+ }
2642
+
2643
+ /* Decode the session. */
2644
+ SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2645
+ if (session == NULL) {
2646
+ ERR_clear_error(); /* Don't leave an error on the queue. */
2647
+ goto done;
2648
+ }
2649
+
2650
+ /* Copy the client's session ID into the new session, to denote the ticket has
2651
+ * been accepted. */
2652
+ memcpy(session->session_id, session_id, session_id_len);
2653
+ session->session_id_length = session_id_len;
2654
+
2655
+ *out_session = session;
2656
+
2657
+ done:
2658
+ OPENSSL_free(plaintext);
2659
+ HMAC_CTX_cleanup(&hmac_ctx);
2660
+ EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2661
+ return ret;
2662
+ }
2663
+
2664
+ /* Tables to translate from NIDs to TLS v1.2 ids */
2665
+ typedef struct {
2666
+ int nid;
2667
+ int id;
2668
+ } tls12_lookup;
2669
+
2670
+ static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2671
+ {NID_sha1, TLSEXT_hash_sha1},
2672
+ {NID_sha224, TLSEXT_hash_sha224},
2673
+ {NID_sha256, TLSEXT_hash_sha256},
2674
+ {NID_sha384, TLSEXT_hash_sha384},
2675
+ {NID_sha512, TLSEXT_hash_sha512}};
2676
+
2677
+ static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2678
+ {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
2679
+
2680
+ static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2681
+ size_t i;
2682
+ for (i = 0; i < tlen; i++) {
2683
+ if (table[i].nid == nid) {
2684
+ return table[i].id;
2685
+ }
2686
+ }
2687
+
2688
+ return -1;
2689
+ }
2690
+
2691
+ int tls12_get_sigid(int pkey_type) {
2692
+ return tls12_find_id(pkey_type, tls12_sig,
2693
+ sizeof(tls12_sig) / sizeof(tls12_lookup));
2694
+ }
2695
+
2696
+ int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_MD *md) {
2697
+ int sig_id, md_id;
2698
+
2699
+ if (!md) {
2700
+ return 0;
2701
+ }
2702
+
2703
+ md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2704
+ sizeof(tls12_md) / sizeof(tls12_lookup));
2705
+ if (md_id == -1) {
2706
+ return 0;
2707
+ }
2708
+
2709
+ sig_id = tls12_get_sigid(ssl_private_key_type(ssl));
2710
+ if (sig_id == -1) {
2711
+ return 0;
2712
+ }
2713
+
2714
+ p[0] = (uint8_t)md_id;
2715
+ p[1] = (uint8_t)sig_id;
2716
+ return 1;
2717
+ }
2718
+
2719
+ const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2720
+ switch (hash_alg) {
2721
+ case TLSEXT_hash_md5:
2722
+ return EVP_md5();
2723
+
2724
+ case TLSEXT_hash_sha1:
2725
+ return EVP_sha1();
2726
+
2727
+ case TLSEXT_hash_sha224:
2728
+ return EVP_sha224();
2729
+
2730
+ case TLSEXT_hash_sha256:
2731
+ return EVP_sha256();
2732
+
2733
+ case TLSEXT_hash_sha384:
2734
+ return EVP_sha384();
2735
+
2736
+ case TLSEXT_hash_sha512:
2737
+ return EVP_sha512();
2738
+
2739
+ default:
2740
+ return NULL;
2741
+ }
2742
+ }
2743
+
2744
+ /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2745
+ * algorithm |sig_alg|. It returns -1 if the type is unknown. */
2746
+ static int tls12_get_pkey_type(uint8_t sig_alg) {
2747
+ switch (sig_alg) {
2748
+ case TLSEXT_signature_rsa:
2749
+ return EVP_PKEY_RSA;
2750
+
2751
+ case TLSEXT_signature_ecdsa:
2752
+ return EVP_PKEY_EC;
2753
+
2754
+ default:
2755
+ return -1;
2756
+ }
2757
+ }
2758
+
2759
+ OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2,
2760
+ sizeof_tls_sigalgs_is_not_two);
2761
+
2762
+ int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
2763
+ /* Extension ignored for inappropriate versions */
2764
+ if (!SSL_USE_SIGALGS(ssl)) {
2765
+ return 1;
2766
+ }
2767
+
2768
+ CERT *const cert = ssl->cert;
2769
+ OPENSSL_free(cert->peer_sigalgs);
2770
+ cert->peer_sigalgs = NULL;
2771
+ cert->peer_sigalgslen = 0;
2772
+
2773
+ size_t num_sigalgs = CBS_len(in_sigalgs);
2774
+
2775
+ if (num_sigalgs % 2 != 0) {
2776
+ return 0;
2777
+ }
2778
+ num_sigalgs /= 2;
2779
+
2780
+ /* supported_signature_algorithms in the certificate request is
2781
+ * allowed to be empty. */
2782
+ if (num_sigalgs == 0) {
2783
+ return 1;
2784
+ }
2785
+
2786
+ /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two
2787
+ * (statically asserted above) and we just divided |num_sigalgs| by two. */
2788
+ cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS));
2789
+ if (cert->peer_sigalgs == NULL) {
2790
+ return 0;
2791
+ }
2792
+ cert->peer_sigalgslen = num_sigalgs;
2793
+
2794
+ CBS sigalgs;
2795
+ CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2796
+
2797
+ size_t i;
2798
+ for (i = 0; i < num_sigalgs; i++) {
2799
+ TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i];
2800
+ if (!CBS_get_u8(&sigalgs, &sigalg->rhash) ||
2801
+ !CBS_get_u8(&sigalgs, &sigalg->rsign)) {
2802
+ return 0;
2803
+ }
2804
+ }
2805
+
2806
+ return 1;
2807
+ }
2808
+
2809
+ const EVP_MD *tls1_choose_signing_digest(SSL *ssl) {
2810
+ CERT *cert = ssl->cert;
2811
+ int type = ssl_private_key_type(ssl);
2812
+ size_t i, j;
2813
+
2814
+ static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512,
2815
+ NID_sha224, NID_sha1};
2816
+
2817
+ const int *digest_nids = kDefaultDigestList;
2818
+ size_t num_digest_nids =
2819
+ sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]);
2820
+ if (cert->digest_nids != NULL) {
2821
+ digest_nids = cert->digest_nids;
2822
+ num_digest_nids = cert->num_digest_nids;
2823
+ }
2824
+
2825
+ for (i = 0; i < num_digest_nids; i++) {
2826
+ const int digest_nid = digest_nids[i];
2827
+ for (j = 0; j < cert->peer_sigalgslen; j++) {
2828
+ const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash);
2829
+ if (md == NULL ||
2830
+ digest_nid != EVP_MD_type(md) ||
2831
+ tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) {
2832
+ continue;
2833
+ }
2834
+
2835
+ return md;
2836
+ }
2837
+ }
2838
+
2839
+ /* If no suitable digest may be found, default to SHA-1. */
2840
+ return EVP_sha1();
2841
+ }
2842
+
2843
+ int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2844
+ int ret = 0;
2845
+ EVP_MD_CTX ctx;
2846
+
2847
+ EVP_MD_CTX_init(&ctx);
2848
+ if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2849
+ goto err;
2850
+ }
2851
+
2852
+ static const char kClientIDMagic[] = "TLS Channel ID signature";
2853
+ EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2854
+
2855
+ if (ssl->hit) {
2856
+ static const char kResumptionMagic[] = "Resumption";
2857
+ EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2858
+ if (ssl->session->original_handshake_hash_len == 0) {
2859
+ OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2860
+ goto err;
2861
+ }
2862
+ EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2863
+ ssl->session->original_handshake_hash_len);
2864
+ }
2865
+
2866
+ uint8_t handshake_hash[EVP_MAX_MD_SIZE];
2867
+ int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
2868
+ sizeof(handshake_hash));
2869
+ if (handshake_hash_len < 0) {
2870
+ goto err;
2871
+ }
2872
+ EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
2873
+ unsigned len_u;
2874
+ EVP_DigestFinal_ex(&ctx, out, &len_u);
2875
+ *out_len = len_u;
2876
+
2877
+ ret = 1;
2878
+
2879
+ err:
2880
+ EVP_MD_CTX_cleanup(&ctx);
2881
+ return ret;
2882
+ }
2883
+
2884
+ /* tls1_record_handshake_hashes_for_channel_id records the current handshake
2885
+ * hashes in |s->session| so that Channel ID resumptions can sign that data. */
2886
+ int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2887
+ int digest_len;
2888
+ /* This function should never be called for a resumed session because the
2889
+ * handshake hashes that we wish to record are for the original, full
2890
+ * handshake. */
2891
+ if (s->hit) {
2892
+ return -1;
2893
+ }
2894
+
2895
+ digest_len =
2896
+ tls1_handshake_digest(s, s->session->original_handshake_hash,
2897
+ sizeof(s->session->original_handshake_hash));
2898
+ if (digest_len < 0) {
2899
+ return -1;
2900
+ }
2901
+
2902
+ s->session->original_handshake_hash_len = digest_len;
2903
+
2904
+ return 1;
2905
+ }