grpc 1.78.1 → 1.80.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (426) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +22 -8
  3. data/include/grpc/credentials.h +47 -37
  4. data/include/grpc/credentials_cpp.h +39 -0
  5. data/include/grpc/event_engine/event_engine.h +8 -3
  6. data/include/grpc/grpc.h +4 -0
  7. data/include/grpc/impl/call.h +9 -0
  8. data/include/grpc/impl/channel_arg_names.h +7 -0
  9. data/include/grpc/module.modulemap +2 -0
  10. data/include/grpc/private_key_signer.h +104 -0
  11. data/include/grpc/support/port_platform.h +6 -0
  12. data/src/core/call/call_filters.h +101 -78
  13. data/src/core/call/call_spine.h +91 -68
  14. data/src/core/call/call_state.h +60 -4
  15. data/src/core/call/client_call.cc +9 -9
  16. data/src/core/call/client_call.h +1 -1
  17. data/src/core/call/metadata_batch.cc +2 -0
  18. data/src/core/call/metadata_batch.h +48 -1
  19. data/src/core/call/metadata_info.cc +35 -0
  20. data/src/core/call/metadata_info.h +2 -0
  21. data/src/core/call/simple_slice_based_metadata.h +2 -1
  22. data/src/core/channelz/channelz.cc +9 -6
  23. data/src/core/channelz/channelz.h +7 -4
  24. data/src/core/channelz/property_list.h +5 -0
  25. data/src/core/channelz/v2tov1/convert.cc +1 -1
  26. data/src/core/channelz/v2tov1/legacy_api.cc +164 -307
  27. data/src/core/client_channel/buffered_call.cc +7 -3
  28. data/src/core/client_channel/buffered_call.h +11 -5
  29. data/src/core/client_channel/client_channel.cc +106 -44
  30. data/src/core/client_channel/client_channel.h +3 -6
  31. data/src/core/client_channel/client_channel_filter.cc +90 -64
  32. data/src/core/client_channel/client_channel_filter.h +3 -6
  33. data/src/core/client_channel/client_channel_internal.h +5 -0
  34. data/src/core/client_channel/config_selector.h +17 -12
  35. data/src/core/client_channel/dynamic_filters.cc +8 -7
  36. data/src/core/client_channel/dynamic_filters.h +7 -5
  37. data/src/core/client_channel/retry_filter.cc +1 -1
  38. data/src/core/client_channel/retry_filter.h +2 -2
  39. data/src/core/client_channel/subchannel.cc +1682 -266
  40. data/src/core/client_channel/subchannel.h +411 -134
  41. data/src/core/client_channel/subchannel_stream_client.cc +22 -18
  42. data/src/core/client_channel/subchannel_stream_client.h +8 -9
  43. data/src/core/client_channel/subchannel_stream_limiter.cc +76 -0
  44. data/src/core/client_channel/subchannel_stream_limiter.h +51 -0
  45. data/src/core/config/config_vars.cc +9 -1
  46. data/src/core/config/config_vars.h +6 -0
  47. data/src/core/credentials/call/call_creds_registry.h +51 -22
  48. data/src/core/credentials/call/call_creds_registry_init.cc +86 -2
  49. data/src/core/credentials/call/external/aws_external_account_credentials.cc +2 -2
  50. data/src/core/credentials/call/external/external_account_credentials.cc +11 -4
  51. data/src/core/credentials/call/external/file_external_account_credentials.cc +2 -2
  52. data/src/core/credentials/transport/channel_creds_registry.h +71 -20
  53. data/src/core/credentials/transport/channel_creds_registry_init.cc +338 -29
  54. data/src/core/credentials/transport/ssl/ssl_credentials.cc +43 -24
  55. data/src/core/credentials/transport/ssl/ssl_credentials.h +7 -1
  56. data/src/core/credentials/transport/ssl/ssl_security_connector.cc +2 -8
  57. data/src/core/credentials/transport/ssl/ssl_security_connector.h +4 -3
  58. data/src/core/credentials/transport/tls/grpc_tls_certificate_distributor.cc +25 -5
  59. data/src/core/credentials/transport/tls/grpc_tls_certificate_distributor.h +7 -5
  60. data/src/core/credentials/transport/tls/grpc_tls_certificate_provider.cc +181 -109
  61. data/src/core/credentials/transport/tls/grpc_tls_certificate_provider.h +55 -42
  62. data/src/core/credentials/transport/tls/grpc_tls_credentials_options.cc +28 -23
  63. data/src/core/credentials/transport/tls/grpc_tls_credentials_options.h +26 -23
  64. data/src/core/credentials/transport/tls/spiffe_utils.cc +2 -2
  65. data/src/core/credentials/transport/tls/ssl_utils.cc +18 -18
  66. data/src/core/credentials/transport/tls/ssl_utils.h +12 -10
  67. data/src/core/credentials/transport/tls/tls_security_connector.cc +106 -74
  68. data/src/core/credentials/transport/tls/tls_security_connector.h +12 -8
  69. data/src/core/credentials/transport/xds/xds_credentials.cc +76 -32
  70. data/src/core/credentials/transport/xds/xds_credentials.h +4 -2
  71. data/src/core/ext/filters/fault_injection/fault_injection_filter.cc +117 -35
  72. data/src/core/ext/filters/fault_injection/fault_injection_filter.h +42 -4
  73. data/src/core/ext/filters/gcp_authentication/gcp_authentication_filter.cc +58 -29
  74. data/src/core/ext/filters/gcp_authentication/gcp_authentication_filter.h +19 -11
  75. data/src/core/ext/filters/stateful_session/stateful_session_filter.cc +82 -25
  76. data/src/core/ext/filters/stateful_session/stateful_session_filter.h +28 -3
  77. data/src/core/ext/filters/stateful_session/stateful_session_service_config_parser.cc +9 -7
  78. data/src/core/ext/filters/stateful_session/stateful_session_service_config_parser.h +1 -1
  79. data/src/core/ext/transport/chttp2/transport/call_tracer_wrapper.h +7 -1
  80. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +117 -67
  81. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +2 -0
  82. data/src/core/ext/transport/chttp2/transport/flow_control.h +11 -1
  83. data/src/core/ext/transport/chttp2/transport/frame.cc +2 -15
  84. data/src/core/ext/transport/chttp2/transport/frame.h +0 -4
  85. data/src/core/ext/transport/chttp2/transport/goaway.cc +17 -2
  86. data/src/core/ext/transport/chttp2/transport/goaway.h +27 -6
  87. data/src/core/ext/transport/chttp2/transport/header_assembler.h +8 -21
  88. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +101 -40
  89. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +95 -0
  90. data/src/core/ext/transport/chttp2/transport/http2_client_transport.cc +923 -772
  91. data/src/core/ext/transport/chttp2/transport/http2_client_transport.h +406 -423
  92. data/src/core/ext/transport/chttp2/transport/http2_settings.cc +1 -0
  93. data/src/core/ext/transport/chttp2/transport/http2_settings.h +8 -1
  94. data/src/core/ext/transport/chttp2/transport/http2_settings_promises.h +25 -13
  95. data/src/core/ext/transport/chttp2/transport/http2_transport.cc +71 -24
  96. data/src/core/ext/transport/chttp2/transport/http2_transport.h +25 -49
  97. data/src/core/ext/transport/chttp2/transport/http2_ztrace_collector.h +2 -2
  98. data/src/core/ext/transport/chttp2/transport/incoming_metadata_tracker.h +29 -9
  99. data/src/core/ext/transport/chttp2/transport/internal.h +6 -2
  100. data/src/core/ext/transport/chttp2/transport/keepalive.cc +14 -20
  101. data/src/core/ext/transport/chttp2/transport/keepalive.h +9 -6
  102. data/src/core/ext/transport/chttp2/transport/parsing.cc +11 -0
  103. data/src/core/ext/transport/chttp2/transport/ping_promise.cc +34 -74
  104. data/src/core/ext/transport/chttp2/transport/ping_promise.h +123 -79
  105. data/src/core/ext/transport/chttp2/transport/security_frame.h +233 -3
  106. data/src/core/ext/transport/chttp2/transport/stream.h +152 -73
  107. data/src/core/ext/transport/chttp2/transport/stream_data_queue.h +155 -85
  108. data/src/core/ext/transport/chttp2/transport/transport_common.h +0 -5
  109. data/src/core/ext/transport/chttp2/transport/writable_streams.h +8 -7
  110. data/src/core/ext/transport/chttp2/transport/write_cycle.cc +86 -0
  111. data/src/core/ext/transport/chttp2/transport/write_cycle.h +355 -0
  112. data/src/core/ext/transport/chttp2/transport/writing.cc +31 -29
  113. data/src/core/ext/upb-gen/cel/expr/checked.upb.h +1875 -0
  114. data/src/core/ext/upb-gen/cel/expr/checked.upb_minitable.c +409 -0
  115. data/src/core/ext/upb-gen/cel/expr/checked.upb_minitable.h +56 -0
  116. data/src/core/ext/upb-gen/cel/expr/syntax.upb.h +2223 -0
  117. data/src/core/ext/upb-gen/cel/expr/syntax.upb_minitable.c +489 -0
  118. data/src/core/ext/upb-gen/cel/expr/syntax.upb_minitable.h +60 -0
  119. data/src/core/ext/upb-gen/envoy/config/accesslog/v3/accesslog.upb.h +2 -1
  120. data/src/core/ext/upb-gen/envoy/config/bootstrap/v3/bootstrap.upb.h +130 -18
  121. data/src/core/ext/upb-gen/envoy/config/bootstrap/v3/bootstrap.upb_minitable.c +18 -13
  122. data/src/core/ext/upb-gen/envoy/config/cluster/v3/cluster.upb.h +70 -38
  123. data/src/core/ext/upb-gen/envoy/config/cluster/v3/cluster.upb_minitable.c +20 -17
  124. data/src/core/ext/upb-gen/envoy/config/common/matcher/v3/matcher.upb.h +26 -10
  125. data/src/core/ext/upb-gen/envoy/config/common/matcher/v3/matcher.upb_minitable.c +8 -7
  126. data/src/core/ext/upb-gen/envoy/config/common/mutation_rules/v3/mutation_rules.upb.h +495 -0
  127. data/src/core/ext/upb-gen/envoy/config/common/mutation_rules/v3/mutation_rules.upb_minitable.c +114 -0
  128. data/src/core/ext/upb-gen/envoy/config/common/mutation_rules/v3/mutation_rules.upb_minitable.h +36 -0
  129. data/src/core/ext/upb-gen/envoy/config/core/v3/address.upb.h +26 -10
  130. data/src/core/ext/upb-gen/envoy/config/core/v3/address.upb_minitable.c +8 -7
  131. data/src/core/ext/upb-gen/envoy/config/core/v3/cel.upb.h +121 -0
  132. data/src/core/ext/upb-gen/envoy/config/core/v3/cel.upb_minitable.c +54 -0
  133. data/src/core/ext/upb-gen/envoy/config/core/v3/cel.upb_minitable.h +32 -0
  134. data/src/core/ext/upb-gen/envoy/config/core/v3/grpc_service.upb.h +143 -9
  135. data/src/core/ext/upb-gen/envoy/config/core/v3/grpc_service.upb_minitable.c +18 -6
  136. data/src/core/ext/upb-gen/envoy/config/core/v3/protocol.upb.h +112 -11
  137. data/src/core/ext/upb-gen/envoy/config/core/v3/protocol.upb_minitable.c +22 -9
  138. data/src/core/ext/upb-gen/envoy/config/core/v3/proxy_protocol.upb.h +276 -0
  139. data/src/core/ext/upb-gen/envoy/config/core/v3/proxy_protocol.upb_minitable.c +60 -5
  140. data/src/core/ext/upb-gen/envoy/config/core/v3/proxy_protocol.upb_minitable.h +4 -0
  141. data/src/core/ext/upb-gen/envoy/config/endpoint/v3/endpoint_components.upb.h +72 -0
  142. data/src/core/ext/upb-gen/envoy/config/endpoint/v3/endpoint_components.upb_minitable.c +23 -2
  143. data/src/core/ext/upb-gen/envoy/config/endpoint/v3/endpoint_components.upb_minitable.h +2 -0
  144. data/src/core/ext/upb-gen/envoy/config/listener/v3/listener.upb.h +129 -13
  145. data/src/core/ext/upb-gen/envoy/config/listener/v3/listener.upb_minitable.c +36 -10
  146. data/src/core/ext/upb-gen/envoy/config/listener/v3/listener.upb_minitable.h +2 -0
  147. data/src/core/ext/upb-gen/envoy/config/listener/v3/quic_config.upb.h +30 -0
  148. data/src/core/ext/upb-gen/envoy/config/listener/v3/quic_config.upb_minitable.c +5 -3
  149. data/src/core/ext/upb-gen/envoy/config/metrics/v3/metrics_service.upb.h +16 -0
  150. data/src/core/ext/upb-gen/envoy/config/metrics/v3/metrics_service.upb_minitable.c +4 -3
  151. data/src/core/ext/upb-gen/envoy/config/metrics/v3/stats.upb.h +31 -0
  152. data/src/core/ext/upb-gen/envoy/config/metrics/v3/stats.upb_minitable.c +5 -3
  153. data/src/core/ext/upb-gen/envoy/config/overload/v3/overload.upb.h +2 -1
  154. data/src/core/ext/upb-gen/envoy/config/rbac/v3/rbac.upb.h +63 -0
  155. data/src/core/ext/upb-gen/envoy/config/rbac/v3/rbac.upb_minitable.c +12 -7
  156. data/src/core/ext/upb-gen/envoy/config/route/v3/route.upb.h +97 -81
  157. data/src/core/ext/upb-gen/envoy/config/route/v3/route.upb_minitable.c +40 -23
  158. data/src/core/ext/upb-gen/envoy/config/route/v3/route_components.upb.h +604 -228
  159. data/src/core/ext/upb-gen/envoy/config/route/v3/route_components.upb_minitable.c +146 -100
  160. data/src/core/ext/upb-gen/envoy/config/tap/v3/common.upb.h +30 -0
  161. data/src/core/ext/upb-gen/envoy/config/tap/v3/common.upb_minitable.c +5 -3
  162. data/src/core/ext/upb-gen/envoy/config/trace/v3/opentelemetry.upb.h +35 -3
  163. data/src/core/ext/upb-gen/envoy/config/trace/v3/opentelemetry.upb_minitable.c +7 -4
  164. data/src/core/ext/upb-gen/envoy/config/trace/v3/zipkin.upb.h +66 -14
  165. data/src/core/ext/upb-gen/envoy/config/trace/v3/zipkin.upb_minitable.c +22 -11
  166. data/src/core/ext/upb-gen/envoy/extensions/clusters/aggregate/v3/cluster.upb.h +87 -0
  167. data/src/core/ext/upb-gen/envoy/extensions/clusters/aggregate/v3/cluster.upb_minitable.c +29 -2
  168. data/src/core/ext/upb-gen/envoy/extensions/clusters/aggregate/v3/cluster.upb_minitable.h +2 -0
  169. data/src/core/ext/upb-gen/envoy/extensions/filters/http/rbac/v3/rbac.upb.h +0 -1
  170. data/src/core/ext/upb-gen/envoy/extensions/filters/http/rbac/v3/rbac.upb_minitable.c +0 -1
  171. data/src/core/ext/upb-gen/envoy/extensions/filters/http/stateful_session/v3/stateful_session.upb.h +20 -4
  172. data/src/core/ext/upb-gen/envoy/extensions/filters/http/stateful_session/v3/stateful_session.upb_minitable.c +5 -4
  173. data/src/core/ext/upb-gen/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.h +239 -60
  174. data/src/core/ext/upb-gen/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb_minitable.c +59 -28
  175. data/src/core/ext/upb-gen/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb_minitable.h +2 -0
  176. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/call_credentials/access_token/v3/access_token_credentials.upb.h +89 -0
  177. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/call_credentials/access_token/v3/access_token_credentials.upb_minitable.c +50 -0
  178. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/call_credentials/access_token/v3/access_token_credentials.upb_minitable.h +32 -0
  179. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/channel_credentials/tls/v3/tls_credentials.upb.h +135 -0
  180. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/channel_credentials/tls/v3/tls_credentials.upb_minitable.c +53 -0
  181. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/channel_credentials/tls/v3/tls_credentials.upb_minitable.h +32 -0
  182. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/channel_credentials/xds/v3/xds_credentials.upb.h +105 -0
  183. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/channel_credentials/xds/v3/xds_credentials.upb_minitable.c +51 -0
  184. data/src/core/ext/upb-gen/envoy/extensions/grpc_service/channel_credentials/xds/v3/xds_credentials.upb_minitable.h +32 -0
  185. data/src/core/ext/upb-gen/envoy/extensions/load_balancing_policies/client_side_weighted_round_robin/v3/client_side_weighted_round_robin.upb.h +32 -0
  186. data/src/core/ext/upb-gen/envoy/extensions/load_balancing_policies/client_side_weighted_round_robin/v3/client_side_weighted_round_robin.upb_minitable.c +6 -3
  187. data/src/core/ext/upb-gen/envoy/extensions/load_balancing_policies/common/v3/common.upb.h +206 -0
  188. data/src/core/ext/upb-gen/envoy/extensions/load_balancing_policies/common/v3/common.upb_minitable.c +41 -8
  189. data/src/core/ext/upb-gen/envoy/extensions/load_balancing_policies/common/v3/common.upb_minitable.h +2 -0
  190. data/src/core/ext/upb-gen/envoy/extensions/transport_sockets/tls/v3/common.upb.h +64 -0
  191. data/src/core/ext/upb-gen/envoy/extensions/transport_sockets/tls/v3/common.upb_minitable.c +4 -3
  192. data/src/core/ext/upb-gen/envoy/extensions/transport_sockets/tls/v3/secret.upb.h +64 -0
  193. data/src/core/ext/upb-gen/envoy/extensions/transport_sockets/tls/v3/secret.upb_minitable.c +31 -5
  194. data/src/core/ext/upb-gen/envoy/extensions/transport_sockets/tls/v3/secret.upb_minitable.h +2 -0
  195. data/src/core/ext/upb-gen/envoy/extensions/upstreams/http/v3/http_protocol_options.upb.h +283 -14
  196. data/src/core/ext/upb-gen/envoy/extensions/upstreams/http/v3/http_protocol_options.upb_minitable.c +48 -11
  197. data/src/core/ext/upb-gen/envoy/extensions/upstreams/http/v3/http_protocol_options.upb_minitable.h +2 -0
  198. data/src/core/ext/upb-gen/envoy/type/http/v3/cookie.upb.h +144 -6
  199. data/src/core/ext/upb-gen/envoy/type/http/v3/cookie.upb_minitable.c +35 -7
  200. data/src/core/ext/upb-gen/envoy/type/http/v3/cookie.upb_minitable.h +2 -0
  201. data/src/core/ext/upb-gen/envoy/type/tracing/v3/custom_tag.upb.h +42 -21
  202. data/src/core/ext/upb-gen/envoy/type/tracing/v3/custom_tag.upb_minitable.c +9 -8
  203. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/promise.upb.h +164 -1
  204. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/promise.upb_minitable.c +37 -6
  205. data/src/core/ext/upb-gen/src/proto/grpc/channelz/v2/promise.upb_minitable.h +2 -0
  206. data/src/core/ext/upb-gen/xds/type/matcher/v3/cel.upb.h +0 -1
  207. data/src/core/ext/upb-gen/xds/type/matcher/v3/cel.upb_minitable.c +0 -1
  208. data/src/core/ext/upb-gen/xds/type/matcher/v3/http_inputs.upb.h +0 -1
  209. data/src/core/ext/upb-gen/xds/type/matcher/v3/http_inputs.upb_minitable.c +0 -1
  210. data/src/core/ext/upb-gen/xds/type/matcher/v3/matcher.upb.h +26 -11
  211. data/src/core/ext/upb-gen/xds/type/matcher/v3/matcher.upb_minitable.c +8 -8
  212. data/src/core/ext/upb-gen/xds/type/matcher/v3/string.upb.h +33 -0
  213. data/src/core/ext/upb-gen/xds/type/matcher/v3/string.upb_minitable.c +14 -3
  214. data/src/core/ext/upb-gen/xds/type/v3/cel.upb.h +90 -10
  215. data/src/core/ext/upb-gen/xds/type/v3/cel.upb_minitable.c +18 -7
  216. data/src/core/ext/upbdefs-gen/cel/expr/checked.upbdefs.c +248 -0
  217. data/src/core/ext/upbdefs-gen/cel/expr/checked.upbdefs.h +97 -0
  218. data/src/core/ext/upbdefs-gen/cel/expr/syntax.upbdefs.c +283 -0
  219. data/src/core/ext/upbdefs-gen/cel/expr/syntax.upbdefs.h +107 -0
  220. data/src/core/ext/upbdefs-gen/envoy/config/accesslog/v3/accesslog.upbdefs.c +213 -211
  221. data/src/core/ext/upbdefs-gen/envoy/config/bootstrap/v3/bootstrap.upbdefs.c +635 -614
  222. data/src/core/ext/upbdefs-gen/envoy/config/cluster/v3/cluster.upbdefs.c +1012 -1000
  223. data/src/core/ext/upbdefs-gen/envoy/config/common/matcher/v3/matcher.upbdefs.c +276 -273
  224. data/src/core/ext/upbdefs-gen/envoy/config/common/mutation_rules/v3/mutation_rules.upbdefs.c +152 -0
  225. data/src/core/ext/upbdefs-gen/envoy/config/common/mutation_rules/v3/mutation_rules.upbdefs.h +47 -0
  226. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/address.upbdefs.c +149 -144
  227. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/base.upbdefs.c +367 -370
  228. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/cel.upbdefs.c +63 -0
  229. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/cel.upbdefs.h +37 -0
  230. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/grpc_service.upbdefs.c +297 -284
  231. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/protocol.upbdefs.c +492 -469
  232. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/proxy_protocol.upbdefs.c +74 -43
  233. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/proxy_protocol.upbdefs.h +10 -0
  234. data/src/core/ext/upbdefs-gen/envoy/config/core/v3/substitution_format_string.upbdefs.c +60 -59
  235. data/src/core/ext/upbdefs-gen/envoy/config/endpoint/v3/endpoint_components.upbdefs.c +202 -184
  236. data/src/core/ext/upbdefs-gen/envoy/config/endpoint/v3/endpoint_components.upbdefs.h +5 -0
  237. data/src/core/ext/upbdefs-gen/envoy/config/listener/v3/listener.upbdefs.c +354 -339
  238. data/src/core/ext/upbdefs-gen/envoy/config/listener/v3/listener.upbdefs.h +5 -0
  239. data/src/core/ext/upbdefs-gen/envoy/config/listener/v3/quic_config.upbdefs.c +28 -19
  240. data/src/core/ext/upbdefs-gen/envoy/config/metrics/v3/metrics_service.upbdefs.c +30 -27
  241. data/src/core/ext/upbdefs-gen/envoy/config/metrics/v3/stats.upbdefs.c +71 -66
  242. data/src/core/ext/upbdefs-gen/envoy/config/overload/v3/overload.upbdefs.c +94 -91
  243. data/src/core/ext/upbdefs-gen/envoy/config/rbac/v3/rbac.upbdefs.c +386 -369
  244. data/src/core/ext/upbdefs-gen/envoy/config/route/v3/route.upbdefs.c +60 -57
  245. data/src/core/ext/upbdefs-gen/envoy/config/route/v3/route_components.upbdefs.c +1974 -1884
  246. data/src/core/ext/upbdefs-gen/envoy/config/tap/v3/common.upbdefs.c +119 -112
  247. data/src/core/ext/upbdefs-gen/envoy/config/trace/v3/opentelemetry.upbdefs.c +62 -51
  248. data/src/core/ext/upbdefs-gen/envoy/config/trace/v3/zipkin.upbdefs.c +109 -88
  249. data/src/core/ext/upbdefs-gen/envoy/extensions/clusters/aggregate/v3/cluster.upbdefs.c +54 -36
  250. data/src/core/ext/upbdefs-gen/envoy/extensions/clusters/aggregate/v3/cluster.upbdefs.h +5 -0
  251. data/src/core/ext/upbdefs-gen/envoy/extensions/filters/http/rbac/v3/rbac.upbdefs.c +78 -84
  252. data/src/core/ext/upbdefs-gen/envoy/extensions/filters/http/stateful_session/v3/stateful_session.upbdefs.c +48 -46
  253. data/src/core/ext/upbdefs-gen/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upbdefs.c +1041 -984
  254. data/src/core/ext/upbdefs-gen/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upbdefs.h +5 -0
  255. data/src/core/ext/upbdefs-gen/envoy/extensions/transport_sockets/tls/v3/common.upbdefs.c +304 -290
  256. data/src/core/ext/upbdefs-gen/envoy/extensions/transport_sockets/tls/v3/secret.upbdefs.c +94 -77
  257. data/src/core/ext/upbdefs-gen/envoy/extensions/transport_sockets/tls/v3/secret.upbdefs.h +5 -0
  258. data/src/core/ext/upbdefs-gen/envoy/extensions/upstreams/http/v3/http_protocol_options.upbdefs.c +246 -193
  259. data/src/core/ext/upbdefs-gen/envoy/extensions/upstreams/http/v3/http_protocol_options.upbdefs.h +5 -0
  260. data/src/core/ext/upbdefs-gen/envoy/type/http/v3/cookie.upbdefs.c +37 -23
  261. data/src/core/ext/upbdefs-gen/envoy/type/http/v3/cookie.upbdefs.h +5 -0
  262. data/src/core/ext/upbdefs-gen/envoy/type/tracing/v3/custom_tag.upbdefs.c +5 -3
  263. data/src/core/ext/upbdefs-gen/google/api/http.upbdefs.c +4 -4
  264. data/src/core/ext/upbdefs-gen/google/api/httpbody.upbdefs.c +4 -5
  265. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/promise.upbdefs.c +113 -87
  266. data/src/core/ext/upbdefs-gen/src/proto/grpc/channelz/v2/promise.upbdefs.h +5 -0
  267. data/src/core/ext/upbdefs-gen/udpa/annotations/migrate.upbdefs.c +5 -5
  268. data/src/core/ext/upbdefs-gen/udpa/annotations/security.upbdefs.c +6 -5
  269. data/src/core/ext/upbdefs-gen/udpa/annotations/sensitive.upbdefs.c +5 -5
  270. data/src/core/ext/upbdefs-gen/udpa/annotations/status.upbdefs.c +5 -5
  271. data/src/core/ext/upbdefs-gen/udpa/annotations/versioning.upbdefs.c +5 -5
  272. data/src/core/ext/upbdefs-gen/xds/type/matcher/v3/cel.upbdefs.c +25 -30
  273. data/src/core/ext/upbdefs-gen/xds/type/matcher/v3/http_inputs.upbdefs.c +14 -20
  274. data/src/core/ext/upbdefs-gen/xds/type/matcher/v3/matcher.upbdefs.c +180 -183
  275. data/src/core/ext/upbdefs-gen/xds/type/matcher/v3/string.upbdefs.c +56 -47
  276. data/src/core/ext/upbdefs-gen/xds/type/v3/cel.upbdefs.c +69 -47
  277. data/src/core/filter/filter_chain.h +95 -0
  278. data/src/core/handshaker/http_connect/{http_connect_handshaker.cc → http_connect_client_handshaker.cc} +32 -31
  279. data/src/core/handshaker/http_connect/{http_connect_handshaker.h → http_connect_client_handshaker.h} +4 -4
  280. data/src/core/handshaker/http_connect/http_proxy_mapper.cc +1 -1
  281. data/src/core/handshaker/http_connect/xds_http_proxy_mapper.cc +1 -1
  282. data/src/core/handshaker/security/pipelined_secure_endpoint.cc +14 -13
  283. data/src/core/handshaker/security/secure_endpoint.cc +282 -68
  284. data/src/core/handshaker/security/secure_endpoint.h +0 -7
  285. data/src/core/lib/channel/channel_args.h +1 -1
  286. data/src/core/lib/channel/promise_based_filter.cc +17 -4
  287. data/src/core/lib/channel/promise_based_filter.h +3 -2
  288. data/src/core/lib/debug/trace_flags.cc +2 -0
  289. data/src/core/lib/debug/trace_flags.h +1 -0
  290. data/src/core/lib/event_engine/cf_engine/cfstream_endpoint.cc +35 -8
  291. data/src/core/lib/event_engine/cf_engine/dns_service_resolver.h +1 -2
  292. data/src/core/lib/event_engine/event_engine.cc +9 -0
  293. data/src/core/lib/event_engine/extensions/tcp_trace.h +0 -3
  294. data/src/core/lib/event_engine/posix_engine/ev_poll_posix.cc +2 -2
  295. data/src/core/lib/event_engine/posix_engine/posix_endpoint.h +1 -1
  296. data/src/core/lib/event_engine/posix_engine/posix_engine.cc +34 -9
  297. data/src/core/lib/event_engine/posix_engine/posix_engine.h +24 -2
  298. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.cc +1 -3
  299. data/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc +141 -14
  300. data/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.h +19 -2
  301. data/src/core/lib/event_engine/posix_engine/posix_interface.h +7 -0
  302. data/src/core/lib/event_engine/posix_engine/posix_interface_posix.cc +21 -3
  303. data/src/core/lib/event_engine/posix_engine/posix_interface_windows.cc +16 -0
  304. data/src/core/lib/experiments/experiments.cc +309 -201
  305. data/src/core/lib/experiments/experiments.h +141 -80
  306. data/src/core/lib/iomgr/event_engine_shims/endpoint.cc +2 -2
  307. data/src/core/lib/iomgr/resolve_address.h +0 -2
  308. data/src/core/lib/iomgr/resolved_address.h +0 -2
  309. data/src/core/lib/iomgr/tcp_posix.cc +13 -5
  310. data/src/core/lib/iomgr/tcp_server.cc +0 -5
  311. data/src/core/lib/iomgr/tcp_server.h +0 -7
  312. data/src/core/lib/iomgr/tcp_server_posix.cc +0 -17
  313. data/src/core/lib/iomgr/tcp_server_utils_posix.h +0 -3
  314. data/src/core/lib/iomgr/tcp_server_windows.cc +12 -51
  315. data/src/core/lib/promise/all_ok.h +17 -12
  316. data/src/core/lib/promise/cancel_callback.h +12 -13
  317. data/src/core/lib/promise/detail/join_state.h +626 -0
  318. data/src/core/lib/promise/detail/promise_factory.h +14 -14
  319. data/src/core/lib/promise/for_each.h +32 -8
  320. data/src/core/lib/promise/if.h +9 -7
  321. data/src/core/lib/promise/loop.h +18 -16
  322. data/src/core/lib/promise/map.h +54 -47
  323. data/src/core/lib/promise/mpsc.h +11 -10
  324. data/src/core/lib/promise/observable.h +6 -6
  325. data/src/core/lib/promise/party.h +25 -19
  326. data/src/core/lib/promise/poll.h +5 -5
  327. data/src/core/lib/promise/prioritized_race.h +10 -7
  328. data/src/core/lib/promise/promise.h +16 -11
  329. data/src/core/lib/promise/race.h +6 -5
  330. data/src/core/lib/promise/seq.h +109 -74
  331. data/src/core/lib/promise/try_join.h +14 -6
  332. data/src/core/lib/promise/try_seq.h +76 -60
  333. data/src/core/lib/resource_quota/api.cc +7 -0
  334. data/src/core/lib/resource_quota/arena.h +1 -1
  335. data/src/core/lib/resource_quota/memory_quota.cc +4 -1
  336. data/src/core/lib/resource_quota/resource_quota.cc +2 -1
  337. data/src/core/lib/resource_quota/resource_quota.h +3 -0
  338. data/src/core/lib/resource_quota/stream_quota.cc +77 -1
  339. data/src/core/lib/resource_quota/stream_quota.h +64 -1
  340. data/src/core/lib/resource_quota/telemetry.h +1 -1
  341. data/src/core/lib/surface/call.cc +13 -0
  342. data/src/core/lib/surface/call_utils.h +58 -43
  343. data/src/core/lib/surface/channel.h +1 -4
  344. data/src/core/lib/surface/completion_queue.cc +13 -6
  345. data/src/core/lib/surface/validate_metadata.cc +20 -15
  346. data/src/core/lib/surface/validate_metadata.h +3 -1
  347. data/src/core/lib/surface/version.cc +2 -2
  348. data/src/core/lib/transport/promise_endpoint.cc +1 -1
  349. data/src/core/lib/transport/promise_endpoint.h +1 -1
  350. data/src/core/lib/transport/transport.h +5 -0
  351. data/src/core/load_balancing/health_check_client.cc +1 -15
  352. data/src/core/load_balancing/health_check_client_internal.h +0 -2
  353. data/src/core/load_balancing/oob_backend_metric.cc +1 -5
  354. data/src/core/load_balancing/oob_backend_metric_internal.h +0 -1
  355. data/src/core/load_balancing/xds/xds_cluster_impl.cc +12 -9
  356. data/src/core/plugin_registry/grpc_plugin_registry.cc +3 -2
  357. data/src/core/resolver/xds/xds_resolver.cc +162 -116
  358. data/src/core/server/server.cc +18 -1
  359. data/src/core/server/server.h +2 -0
  360. data/src/core/server/xds_server_config_fetcher.cc +4 -4
  361. data/src/core/telemetry/call_tracer.cc +87 -2
  362. data/src/core/telemetry/call_tracer.h +46 -8
  363. data/src/core/telemetry/instrument.cc +102 -40
  364. data/src/core/telemetry/instrument.h +246 -65
  365. data/src/core/tsi/fake_transport_security.cc +3 -1
  366. data/src/core/tsi/ssl_transport_security.cc +516 -137
  367. data/src/core/tsi/ssl_transport_security.h +28 -22
  368. data/src/core/tsi/ssl_transport_security_utils.cc +2 -2
  369. data/src/core/tsi/ssl_transport_security_utils.h +2 -2
  370. data/src/core/util/bitset.h +6 -0
  371. data/src/core/util/function_signature.h +3 -1
  372. data/src/core/util/http_client/httpcli_security_connector.cc +2 -1
  373. data/src/core/util/json/json_reader.cc +0 -4
  374. data/src/core/xds/grpc/certificate_provider_store.cc +2 -1
  375. data/src/core/xds/grpc/certificate_provider_store.h +3 -17
  376. data/src/core/xds/grpc/certificate_provider_store_interface.h +61 -0
  377. data/src/core/xds/grpc/xds_bootstrap_grpc.cc +48 -0
  378. data/src/core/xds/grpc/xds_bootstrap_grpc.h +18 -0
  379. data/src/core/xds/grpc/xds_certificate_provider.cc +7 -2
  380. data/src/core/xds/grpc/xds_certificate_provider.h +13 -2
  381. data/src/core/xds/grpc/xds_client_grpc.cc +13 -6
  382. data/src/core/xds/grpc/xds_client_grpc.h +10 -7
  383. data/src/core/xds/grpc/xds_cluster.cc +18 -4
  384. data/src/core/xds/grpc/xds_cluster.h +17 -2
  385. data/src/core/xds/grpc/xds_cluster_parser.cc +36 -11
  386. data/src/core/xds/grpc/xds_common_types.cc +45 -0
  387. data/src/core/xds/grpc/xds_common_types.h +31 -0
  388. data/src/core/xds/grpc/xds_common_types_parser.cc +274 -16
  389. data/src/core/xds/grpc/xds_common_types_parser.h +12 -0
  390. data/src/core/xds/grpc/xds_http_fault_filter.cc +128 -24
  391. data/src/core/xds/grpc/xds_http_fault_filter.h +19 -10
  392. data/src/core/xds/grpc/xds_http_filter.cc +38 -0
  393. data/src/core/xds/grpc/xds_http_filter.h +70 -47
  394. data/src/core/xds/grpc/xds_http_filter_registry.cc +48 -14
  395. data/src/core/xds/grpc/xds_http_filter_registry.h +29 -15
  396. data/src/core/xds/grpc/xds_http_gcp_authn_filter.cc +88 -22
  397. data/src/core/xds/grpc/xds_http_gcp_authn_filter.h +22 -11
  398. data/src/core/xds/grpc/xds_http_rbac_filter.cc +36 -20
  399. data/src/core/xds/grpc/xds_http_rbac_filter.h +19 -10
  400. data/src/core/xds/grpc/xds_http_stateful_session_filter.cc +143 -26
  401. data/src/core/xds/grpc/xds_http_stateful_session_filter.h +19 -10
  402. data/src/core/xds/grpc/xds_listener.cc +4 -1
  403. data/src/core/xds/grpc/xds_listener.h +10 -2
  404. data/src/core/xds/grpc/xds_listener_parser.cc +23 -18
  405. data/src/core/xds/grpc/xds_matcher.cc +40 -5
  406. data/src/core/xds/grpc/xds_matcher.h +13 -0
  407. data/src/core/xds/grpc/xds_matcher_action.h +1 -1
  408. data/src/core/xds/grpc/xds_matcher_parse.cc +60 -40
  409. data/src/core/xds/grpc/xds_matcher_parse.h +2 -1
  410. data/src/core/xds/grpc/xds_route_config.cc +12 -1
  411. data/src/core/xds/grpc/xds_route_config.h +15 -2
  412. data/src/core/xds/grpc/xds_route_config_parser.cc +11 -5
  413. data/src/core/xds/grpc/xds_routing.cc +181 -6
  414. data/src/core/xds/grpc/xds_routing.h +57 -0
  415. data/src/core/xds/grpc/xds_server_grpc.cc +55 -43
  416. data/src/core/xds/grpc/xds_server_grpc.h +13 -6
  417. data/src/core/xds/grpc/xds_server_grpc_interface.h +3 -2
  418. data/src/core/xds/grpc/xds_transport_grpc.cc +12 -6
  419. data/src/core/xds/grpc/xds_transport_grpc.h +5 -1
  420. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +12 -8
  421. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +18 -12
  422. data/src/ruby/lib/grpc/version.rb +1 -1
  423. data/src/ruby/pb/generate_proto_ruby.sh +1 -1
  424. metadata +42 -6
  425. data/src/core/ext/transport/chttp2/transport/security_frame.cc +0 -31
  426. data/src/core/handshaker/security/legacy_secure_endpoint.cc +0 -597
@@ -18,11 +18,14 @@
18
18
 
19
19
  #include "src/core/tsi/ssl_transport_security.h"
20
20
 
21
+ #include <grpc/private_key_signer.h>
21
22
  #include <grpc/support/port_platform.h>
22
23
  #include <limits.h>
23
24
  #include <string.h>
24
25
 
26
+ #include <cstddef>
25
27
  #include <cstdlib>
28
+ #include <utility>
26
29
 
27
30
  // TODO(jboeuf): refactor inet_ntop into a portability header.
28
31
  // Note: for whomever reads this and tries to refactor this, this
@@ -55,6 +58,8 @@
55
58
 
56
59
  #include "src/core/credentials/transport/tls/grpc_tls_crl_provider.h"
57
60
  #include "src/core/credentials/transport/tls/ssl_utils.h"
61
+ #include "src/core/lib/event_engine/default_event_engine.h"
62
+ #include "src/core/lib/iomgr/exec_ctx.h"
58
63
  #include "src/core/lib/surface/init.h"
59
64
  #include "src/core/tsi/ssl/key_logging/ssl_key_logging.h"
60
65
  #include "src/core/tsi/ssl/session_cache/ssl_session_cache.h"
@@ -62,17 +67,19 @@
62
67
  #include "src/core/tsi/ssl_types.h"
63
68
  #include "src/core/tsi/transport_security.h"
64
69
  #include "src/core/tsi/transport_security_interface.h"
65
- #include "src/core/util/crash.h"
66
70
  #include "src/core/util/env.h"
67
71
  #include "src/core/util/grpc_check.h"
68
72
  #include "src/core/util/match.h"
73
+ #include "src/core/util/ref_counted.h"
74
+ #include "src/core/util/ref_counted_ptr.h"
69
75
  #include "src/core/util/sync.h"
70
76
  #include "src/core/util/useful.h"
77
+ #include "absl/base/thread_annotations.h"
78
+ #include "absl/functional/bind_front.h"
71
79
  #include "absl/log/log.h"
72
80
  #include "absl/strings/match.h"
73
81
  #include "absl/strings/numbers.h"
74
82
  #include "absl/strings/str_cat.h"
75
- #include "absl/strings/str_split.h"
76
83
  #include "absl/strings/string_view.h"
77
84
 
78
85
  // Name of the environment variable controlling OpenSSL cleanup timeout.
@@ -101,6 +108,9 @@ const size_t kMaxChainLength = 100;
101
108
 
102
109
  using TlsSessionKeyLogger = tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger;
103
110
 
111
+ using tsi::PrivateKey;
112
+ using tsi::RootCertInfo;
113
+
104
114
  // --- Structure definitions. ---
105
115
 
106
116
  struct tsi_ssl_root_certs_store {
@@ -110,8 +120,14 @@ struct tsi_ssl_root_certs_store {
110
120
  struct tsi_ssl_handshaker_factory {
111
121
  const tsi_ssl_handshaker_factory_vtable* vtable;
112
122
  gpr_refcount refcount;
123
+ #if defined(OPENSSL_IS_BORINGSSL)
124
+ std::shared_ptr<grpc_core::PrivateKeySigner> key_signer;
125
+ #endif
113
126
  };
114
127
 
128
+ static void tsi_ssl_handshaker_factory_unref(
129
+ tsi_ssl_handshaker_factory* factory);
130
+
115
131
  struct tsi_ssl_client_handshaker_factory {
116
132
  tsi_ssl_handshaker_factory base;
117
133
  SSL_CTX* ssl_context;
@@ -119,7 +135,7 @@ struct tsi_ssl_client_handshaker_factory {
119
135
  size_t alpn_protocol_list_length;
120
136
  grpc_core::RefCountedPtr<tsi::SslSessionLRUCache> session_cache;
121
137
  grpc_core::RefCountedPtr<TlsSessionKeyLogger> key_logger;
122
- std::shared_ptr<RootCertInfo> root_cert_info;
138
+ std::shared_ptr<tsi::RootCertInfo> root_cert_info;
123
139
  };
124
140
 
125
141
  struct tsi_ssl_server_handshaker_factory {
@@ -133,18 +149,67 @@ struct tsi_ssl_server_handshaker_factory {
133
149
  unsigned char* alpn_protocol_list;
134
150
  size_t alpn_protocol_list_length;
135
151
  grpc_core::RefCountedPtr<TlsSessionKeyLogger> key_logger;
136
- std::shared_ptr<RootCertInfo> root_cert_info;
152
+ std::shared_ptr<tsi::RootCertInfo> root_cert_info;
137
153
  };
138
154
 
139
- struct tsi_ssl_handshaker {
140
- tsi_handshaker base;
155
+ // Tracks the arguments for a pending call to tsi_handshaker_next().
156
+ struct HandshakerNextArgs {
157
+ // Input args.
158
+ std::vector<uint8_t> received_bytes;
159
+ size_t original_received_bytes_size = 0;
160
+ tsi_handshaker_on_next_done_cb cb;
161
+ void* user_data;
162
+ std::string* error_ptr = nullptr;
163
+
164
+ // Output args.
165
+ const unsigned char* bytes_to_send = nullptr;
166
+ size_t bytes_to_send_size = 0;
167
+ tsi_handshaker_result* handshaker_result = nullptr;
168
+ };
169
+
170
+ struct tsi_ssl_handshaker : public tsi_handshaker,
171
+ public grpc_core::RefCounted<tsi_ssl_handshaker> {
172
+ tsi_ssl_handshaker() = default;
173
+ ~tsi_ssl_handshaker() override {
174
+ SSL_free(ssl);
175
+ BIO_free(network_io);
176
+ gpr_free(outgoing_bytes_buffer);
177
+ tsi_ssl_handshaker_factory_unref(factory_ref);
178
+ }
179
+
141
180
  SSL* ssl;
142
181
  BIO* network_io;
143
182
  tsi_result result;
144
183
  unsigned char* outgoing_bytes_buffer;
145
184
  size_t outgoing_bytes_buffer_size;
146
185
  tsi_ssl_handshaker_factory* factory_ref;
186
+ grpc_core::Mutex mu;
187
+ bool is_shutdown ABSL_GUARDED_BY(mu) = false;
188
+
189
+ // Will be set if there is a pending call to tsi_handshaker_next(),
190
+ // or nullopt if not.
191
+ std::optional<HandshakerNextArgs> handshaker_next_args ABSL_GUARDED_BY(mu);
192
+ void MaybeSetError(std::string error) const
193
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&mu) {
194
+ if (!handshaker_next_args.has_value()) return;
195
+ if (handshaker_next_args->error_ptr == nullptr) return;
196
+ *handshaker_next_args->error_ptr = std::move(error);
197
+ }
198
+ #if defined(OPENSSL_IS_BORINGSSL)
199
+ // The signed_bytes are populated when the signature process is completed if
200
+ // the Private Key offload was successful. If there was an error during the
201
+ // signature, the status will be returned.
202
+ absl::StatusOr<std::string> signed_bytes ABSL_GUARDED_BY(mu) = "";
203
+ // The handle for an in-flight async signing operation.
204
+ std::shared_ptr<grpc_core::PrivateKeySigner::AsyncSigningHandle>
205
+ signing_handle ABSL_GUARDED_BY(mu);
206
+ #endif
147
207
  };
208
+
209
+ static std::pair<tsi_result, std::optional<HandshakerNextArgs>>
210
+ ssl_handshaker_next_async(tsi_ssl_handshaker* self)
211
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu);
212
+
148
213
  struct tsi_ssl_handshaker_result {
149
214
  tsi_handshaker_result base;
150
215
  SSL* ssl;
@@ -163,9 +228,43 @@ struct tsi_ssl_frame_protector {
163
228
  // concurrently.
164
229
  gpr_mu mu;
165
230
  };
166
- // --- Library Initialization. ---
167
231
 
168
232
  namespace {
233
+
234
+ // --- Library Initialization. ---
235
+
236
+ #if defined(OPENSSL_IS_BORINGSSL)
237
+
238
+ absl::StatusOr<grpc_core::PrivateKeySigner::SignatureAlgorithm>
239
+ ToSignatureAlgorithmClass(uint16_t algorithm) {
240
+ switch (algorithm) {
241
+ case SSL_SIGN_RSA_PKCS1_SHA256:
242
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPkcs1Sha256;
243
+ case SSL_SIGN_RSA_PKCS1_SHA384:
244
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPkcs1Sha384;
245
+ case SSL_SIGN_RSA_PKCS1_SHA512:
246
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPkcs1Sha512;
247
+ case SSL_SIGN_ECDSA_SECP256R1_SHA256:
248
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::
249
+ kEcdsaSecp256r1Sha256;
250
+ case SSL_SIGN_ECDSA_SECP384R1_SHA384:
251
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::
252
+ kEcdsaSecp384r1Sha384;
253
+ case SSL_SIGN_ECDSA_SECP521R1_SHA512:
254
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::
255
+ kEcdsaSecp521r1Sha512;
256
+ case SSL_SIGN_RSA_PSS_RSAE_SHA256:
257
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPssRsaeSha256;
258
+ case SSL_SIGN_RSA_PSS_RSAE_SHA384:
259
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPssRsaeSha384;
260
+ case SSL_SIGN_RSA_PSS_RSAE_SHA512:
261
+ return grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPssRsaeSha512;
262
+ }
263
+ return absl::InvalidArgumentError("Unknown signature algorithm.");
264
+ }
265
+
266
+ #endif // OPENSSL_IS_BORINGSSL
267
+
169
268
  // Builds the alpn protocol name list according to rfc 7301.
170
269
  // OpenSSL requires <const char**> for the input to the alpn methods.
171
270
  tsi_result BuildAlpnProtocolNameList(const char** alpn_protocols,
@@ -256,6 +355,122 @@ static int g_ssl_ctx_ex_crl_provider_index = -1;
256
355
  static int g_ssl_ctx_ex_spiffe_bundle_map_index = -1;
257
356
  static const unsigned char kSslSessionIdContext[] = {'g', 'r', 'p', 'c'};
258
357
  static int g_ssl_ex_verified_root_cert_index = -1;
358
+ static int g_ssl_ex_handshaker_index = -1;
359
+
360
+ #if defined(OPENSSL_IS_BORINGSSL)
361
+ static tsi_ssl_handshaker* GetHandshaker(const SSL* ssl) {
362
+ if (ssl == nullptr) return nullptr;
363
+ GRPC_CHECK_NE(g_ssl_ex_handshaker_index, -1);
364
+ return static_cast<tsi_ssl_handshaker*>(
365
+ SSL_get_ex_data(ssl, g_ssl_ex_handshaker_index));
366
+ }
367
+
368
+ // Invoked by the private key signer when it runs asynchronously.
369
+ void TlsOffloadSignDoneCallback(
370
+ grpc_core::RefCountedPtr<tsi_ssl_handshaker> handshaker,
371
+ absl::StatusOr<std::string> signed_data) {
372
+ grpc_core::ExecCtx exec_ctx;
373
+ std::optional<HandshakerNextArgs> next_args;
374
+ tsi_result result = TSI_INTERNAL_ERROR;
375
+ {
376
+ grpc_core::MutexLock lock(&handshaker->mu);
377
+ if (handshaker->is_shutdown) return;
378
+ handshaker->signed_bytes = std::move(signed_data);
379
+ handshaker->signing_handle.reset();
380
+ // Once the signed bytes are obtained, tell everything to resume the
381
+ // pending async operation.
382
+ auto async_result = ssl_handshaker_next_async(handshaker.get());
383
+ result = async_result.first;
384
+ next_args = std::move(async_result.second);
385
+ }
386
+ if (next_args.has_value() && next_args->cb != nullptr) {
387
+ next_args->cb(result, next_args->user_data, next_args->bytes_to_send,
388
+ next_args->bytes_to_send_size, next_args->handshaker_result);
389
+ }
390
+ }
391
+
392
+ // Invoked by BoringSSL to get the result of the private key signing.
393
+ enum ssl_private_key_result_t TlsPrivateKeyOffloadComplete(SSL* ssl,
394
+ uint8_t* out,
395
+ size_t* out_len,
396
+ size_t max_out)
397
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
398
+ auto* handshaker = GetHandshaker(ssl);
399
+ if (handshaker == nullptr) return ssl_private_key_failure;
400
+ if (!handshaker->signed_bytes.ok() || handshaker->signed_bytes->empty()) {
401
+ if (!handshaker->signed_bytes.ok()) {
402
+ handshaker->MaybeSetError(handshaker->signed_bytes.status().ToString());
403
+ }
404
+ return ssl_private_key_failure;
405
+ }
406
+ // Important bit is moving the signed data where it needs to go
407
+ const std::string& signed_data = *handshaker->signed_bytes;
408
+ if (signed_data.length() > max_out) {
409
+ // Result is too large.
410
+ handshaker->MaybeSetError("Result exceeds output limit");
411
+ return ssl_private_key_failure;
412
+ }
413
+ memcpy(out, signed_data.data(), signed_data.length());
414
+ *out_len = signed_data.length();
415
+ // Tell BoringSSL we're done
416
+ return ssl_private_key_success;
417
+ }
418
+
419
+ // Invoked by BoringSSL during the handshake to do private key signing.
420
+ enum ssl_private_key_result_t TlsPrivateKeySignWrapper(
421
+ SSL* ssl, uint8_t* out, size_t* out_len, size_t max_out,
422
+ uint16_t signature_algorithm, const uint8_t* in, size_t in_len)
423
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
424
+ tsi_ssl_handshaker* handshaker = GetHandshaker(ssl);
425
+ if (handshaker == nullptr) {
426
+ return ssl_private_key_failure;
427
+ }
428
+ if (handshaker->is_shutdown) {
429
+ handshaker->MaybeSetError("Handshaker is shuting down");
430
+ return ssl_private_key_failure;
431
+ }
432
+ // Create the completion callback by binding the current context.
433
+ auto done_callback =
434
+ absl::bind_front(TlsOffloadSignDoneCallback, handshaker->Ref());
435
+ // Call the user's sign function. It can be sync or async.
436
+ // When the user's sign function is async, the contract is that they MUST
437
+ // invoke the callback when complete in their implementation, and their impl
438
+ // MUST not block.
439
+ auto algorithm = ToSignatureAlgorithmClass(signature_algorithm);
440
+ if (!algorithm.ok()) {
441
+ handshaker->MaybeSetError(algorithm.status().ToString());
442
+ return ssl_private_key_failure;
443
+ }
444
+ grpc_core::PrivateKeySigner* signer =
445
+ handshaker->factory_ref->key_signer.get();
446
+ if (signer == nullptr) {
447
+ handshaker->MaybeSetError("PrivateKeySigner is null");
448
+ return ssl_private_key_failure;
449
+ }
450
+ auto result =
451
+ signer->Sign(absl::string_view(reinterpret_cast<const char*>(in), in_len),
452
+ *algorithm, done_callback);
453
+ // Handle synchronous return.
454
+ return grpc_core::MatchMutable(
455
+ &result,
456
+ [&](absl::StatusOr<std::string>* status_or_string)
457
+ ABSL_NO_THREAD_SAFETY_ANALYSIS {
458
+ handshaker->signed_bytes = std::move(*status_or_string);
459
+ return TlsPrivateKeyOffloadComplete(ssl, out, out_len, max_out);
460
+ },
461
+ [&](std::shared_ptr<grpc_core::PrivateKeySigner::AsyncSigningHandle>*
462
+ async_handler) ABSL_NO_THREAD_SAFETY_ANALYSIS {
463
+ handshaker->signing_handle = std::move(*async_handler);
464
+ return ssl_private_key_retry;
465
+ });
466
+ }
467
+
468
+ const SSL_PRIVATE_KEY_METHOD TlsOffloadPrivateKeyMethod = {
469
+ TlsPrivateKeySignWrapper,
470
+ nullptr, // decrypt not implemented for this use case
471
+ TlsPrivateKeyOffloadComplete};
472
+ #endif // defined(OPENSSL_IS_BORINGSSL)
473
+
259
474
  #if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_NO_ENGINE)
260
475
  static const char kSslEnginePrefix[] = "engine:";
261
476
  #endif
@@ -351,8 +566,11 @@ static void init_openssl(void) {
351
566
  g_ssl_ex_verified_root_cert_index = SSL_get_ex_new_index(
352
567
  0, nullptr, nullptr, nullptr, verified_root_cert_free);
353
568
  GRPC_CHECK_NE(g_ssl_ex_verified_root_cert_index, -1);
354
- }
355
569
 
570
+ g_ssl_ex_handshaker_index =
571
+ SSL_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr);
572
+ GRPC_CHECK_NE(g_ssl_ex_handshaker_index, -1);
573
+ }
356
574
  // --- Ssl utils. ---
357
575
 
358
576
  // TODO(jboeuf): Remove when we are past the debugging phase with this code.
@@ -918,21 +1136,40 @@ static tsi_result populate_ssl_context(
918
1136
  const char* cipher_list) {
919
1137
  tsi_result result = TSI_OK;
920
1138
  if (key_cert_pair != nullptr) {
921
- if (key_cert_pair->cert_chain != nullptr) {
922
- result = ssl_ctx_use_certificate_chain(context, key_cert_pair->cert_chain,
923
- strlen(key_cert_pair->cert_chain));
1139
+ if (!key_cert_pair->cert_chain.empty()) {
1140
+ result = ssl_ctx_use_certificate_chain(
1141
+ context, key_cert_pair->cert_chain.c_str(),
1142
+ key_cert_pair->cert_chain.length());
924
1143
  if (result != TSI_OK) {
925
1144
  LOG(ERROR) << "Invalid cert chain file.";
926
1145
  return result;
927
1146
  }
928
1147
  }
929
- if (key_cert_pair->private_key != nullptr) {
930
- result = ssl_ctx_use_private_key(context, key_cert_pair->private_key,
931
- strlen(key_cert_pair->private_key));
932
- if (result != TSI_OK || !SSL_CTX_check_private_key(context)) {
933
- LOG(ERROR) << "Invalid private key.";
934
- return result != TSI_OK ? result : TSI_INVALID_ARGUMENT;
935
- }
1148
+ result = grpc_core::Match(
1149
+ key_cert_pair->private_key,
1150
+ [&](const std::string& pem_root_certs) {
1151
+ tsi_result result = TSI_OK;
1152
+ result = ssl_ctx_use_private_key(context, pem_root_certs.data(),
1153
+ pem_root_certs.length());
1154
+ if (result != TSI_OK || !SSL_CTX_check_private_key(context)) {
1155
+ LOG(ERROR) << "Invalid private key.";
1156
+ return result != TSI_OK ? result : TSI_INVALID_ARGUMENT;
1157
+ }
1158
+ return result;
1159
+ },
1160
+ [&](const std::shared_ptr<grpc_core::PrivateKeySigner>& key_signer) {
1161
+ #if defined(OPENSSL_IS_BORINGSSL)
1162
+ if (key_signer != nullptr) {
1163
+ SSL_CTX_set_private_key_method(context,
1164
+ &TlsOffloadPrivateKeyMethod);
1165
+ }
1166
+ return TSI_OK;
1167
+ #else
1168
+ return TSI_UNIMPLEMENTED;
1169
+ #endif // defined(OPENSSL_IS_BORINGSSL)
1170
+ });
1171
+ if (result != TSI_OK) {
1172
+ return result;
936
1173
  }
937
1174
  }
938
1175
  if ((cipher_list != nullptr) &&
@@ -1100,12 +1337,12 @@ static absl::StatusOr<X509_CRL*> GetCrlFromProvider(
1100
1337
  if (provider == nullptr) {
1101
1338
  return absl::InvalidArgumentError("CrlProvider is null.");
1102
1339
  }
1103
- absl::StatusOr<std::string> issuer_name = grpc_core::IssuerFromCert(cert);
1340
+ absl::StatusOr<std::string> issuer_name = tsi::IssuerFromCert(cert);
1104
1341
  if (!issuer_name.ok()) {
1105
1342
  GRPC_TRACE_LOG(tsi, INFO) << "Could not get certificate issuer name";
1106
1343
  return absl::InvalidArgumentError(issuer_name.status().message());
1107
1344
  }
1108
- absl::StatusOr<std::string> akid = grpc_core::AkidFromCertificate(cert);
1345
+ absl::StatusOr<std::string> akid = tsi::AkidFromCertificate(cert);
1109
1346
  std::string akid_to_use;
1110
1347
  if (!akid.ok()) {
1111
1348
  GRPC_TRACE_LOG(tsi, INFO)
@@ -1140,12 +1377,12 @@ static bool ValidateCrl(X509* cert, X509* issuer, X509_CRL* crl) {
1140
1377
  // RFC5280 6.3.3
1141
1378
  // 6.3.3a we do not support distribution points
1142
1379
  // 6.3.3b verify issuer and scope
1143
- valid = grpc_core::VerifyCrlCertIssuerNamesMatch(crl, cert);
1380
+ valid = tsi::VerifyCrlCertIssuerNamesMatch(crl, cert);
1144
1381
  if (!valid) {
1145
1382
  VLOG(2) << "CRL and cert issuer names mismatched.";
1146
1383
  return valid;
1147
1384
  }
1148
- valid = grpc_core::HasCrlSignBit(issuer);
1385
+ valid = tsi::HasCrlSignBit(issuer);
1149
1386
  if (!valid) {
1150
1387
  VLOG(2) << "CRL issuer not allowed to sign CRLs.";
1151
1388
  return valid;
@@ -1156,7 +1393,7 @@ static bool ValidateCrl(X509* cert, X509* issuer, X509_CRL* crl) {
1156
1393
  // 6.3.3f We only support direct CRLs so these paths are by definition the
1157
1394
  // same.
1158
1395
  // 6.3.3g Verify CRL Signature
1159
- valid = grpc_core::VerifyCrlSignature(crl, issuer);
1396
+ valid = tsi::VerifyCrlSignature(crl, issuer);
1160
1397
  if (!valid) {
1161
1398
  VLOG(2) << "Crl signature check failed.";
1162
1399
  }
@@ -1287,7 +1524,7 @@ static absl::StatusOr<std::string> GetSpiffeUriFromCert(X509* cert) {
1287
1524
  "validation. Must "
1288
1525
  "have exactly one URI SAN that is the SPIFFE ID.");
1289
1526
  }
1290
- spiffe_uri = grpc_core::ParseUriString(subject_alt_name);
1527
+ spiffe_uri = tsi::ParseUriString(subject_alt_name);
1291
1528
  }
1292
1529
  }
1293
1530
  }
@@ -1509,7 +1746,7 @@ static tsi_result ssl_protector_protect(tsi_frame_protector* self,
1509
1746
  tsi_ssl_frame_protector* impl =
1510
1747
  reinterpret_cast<tsi_ssl_frame_protector*>(self);
1511
1748
  gpr_mu_lock(&impl->mu);
1512
- tsi_result result = grpc_core::SslProtectorProtect(
1749
+ tsi_result result = tsi::SslProtectorProtect(
1513
1750
  unprotected_bytes, impl->buffer_size, impl->buffer_offset, impl->buffer,
1514
1751
  impl->ssl, impl->network_io, unprotected_bytes_size,
1515
1752
  protected_output_frames, protected_output_frames_size);
@@ -1523,7 +1760,7 @@ static tsi_result ssl_protector_protect_flush(
1523
1760
  tsi_ssl_frame_protector* impl =
1524
1761
  reinterpret_cast<tsi_ssl_frame_protector*>(self);
1525
1762
  gpr_mu_lock(&impl->mu);
1526
- tsi_result result = grpc_core::SslProtectorProtectFlush(
1763
+ tsi_result result = tsi::SslProtectorProtectFlush(
1527
1764
  impl->buffer_offset, impl->buffer, impl->ssl, impl->network_io,
1528
1765
  protected_output_frames, protected_output_frames_size,
1529
1766
  still_pending_size);
@@ -1538,7 +1775,7 @@ static tsi_result ssl_protector_unprotect(
1538
1775
  tsi_ssl_frame_protector* impl =
1539
1776
  reinterpret_cast<tsi_ssl_frame_protector*>(self);
1540
1777
  gpr_mu_lock(&impl->mu);
1541
- tsi_result result = grpc_core::SslProtectorUnprotect(
1778
+ tsi_result result = tsi::SslProtectorUnprotect(
1542
1779
  protected_frames_bytes, impl->ssl, impl->network_io,
1543
1780
  protected_frames_bytes_size, unprotected_bytes, unprotected_bytes_size);
1544
1781
  gpr_mu_unlock(&impl->mu);
@@ -1791,13 +2028,13 @@ static const tsi_handshaker_result_vtable handshaker_result_vtable = {
1791
2028
  ssl_handshaker_result_destroy,
1792
2029
  };
1793
2030
 
1794
- static tsi_result ssl_handshaker_result_create(
1795
- tsi_ssl_handshaker* handshaker, unsigned char* unused_bytes,
1796
- size_t unused_bytes_size, tsi_handshaker_result** handshaker_result,
1797
- std::string* error) {
1798
- if (handshaker == nullptr || handshaker_result == nullptr ||
1799
- (unused_bytes_size > 0 && unused_bytes == nullptr)) {
1800
- if (error != nullptr) *error = "invalid argument";
2031
+ static tsi_result ssl_handshaker_result_create(tsi_ssl_handshaker* handshaker,
2032
+ unsigned char* unused_bytes,
2033
+ size_t unused_bytes_size)
2034
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
2035
+ if (handshaker == nullptr) return TSI_INVALID_ARGUMENT;
2036
+ if (unused_bytes_size > 0 && unused_bytes == nullptr) {
2037
+ handshaker->MaybeSetError("invalid argument");
1801
2038
  return TSI_INVALID_ARGUMENT;
1802
2039
  }
1803
2040
  tsi_ssl_handshaker_result* result =
@@ -1811,18 +2048,18 @@ static tsi_result ssl_handshaker_result_create(
1811
2048
  // Transfer ownership of |unused_bytes| to the handshaker result.
1812
2049
  result->unused_bytes = unused_bytes;
1813
2050
  result->unused_bytes_size = unused_bytes_size;
1814
- *handshaker_result = &result->base;
2051
+ handshaker->handshaker_next_args->handshaker_result = &result->base;
1815
2052
  return TSI_OK;
1816
2053
  }
1817
2054
 
1818
2055
  // --- tsi_handshaker methods implementation. ---
1819
2056
 
1820
2057
  static tsi_result ssl_handshaker_get_bytes_to_send_to_peer(
1821
- tsi_ssl_handshaker* impl, unsigned char* bytes, size_t* bytes_size,
1822
- std::string* error) {
2058
+ tsi_ssl_handshaker* impl, unsigned char* bytes, size_t* bytes_size)
2059
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
1823
2060
  int bytes_read_from_ssl = 0;
1824
2061
  if (bytes == nullptr || bytes_size == nullptr || *bytes_size > INT_MAX) {
1825
- if (error != nullptr) *error = "invalid argument";
2062
+ impl->MaybeSetError("invalid argument");
1826
2063
  return TSI_INVALID_ARGUMENT;
1827
2064
  }
1828
2065
  GRPC_CHECK_LE(*bytes_size, static_cast<size_t>(INT_MAX));
@@ -1831,7 +2068,7 @@ static tsi_result ssl_handshaker_get_bytes_to_send_to_peer(
1831
2068
  if (bytes_read_from_ssl < 0) {
1832
2069
  *bytes_size = 0;
1833
2070
  if (!BIO_should_retry(impl->network_io)) {
1834
- if (error != nullptr) *error = "error reading from BIO";
2071
+ impl->MaybeSetError("error reading from BIO");
1835
2072
  impl->result = TSI_INTERNAL_ERROR;
1836
2073
  return impl->result;
1837
2074
  } else {
@@ -1850,8 +2087,8 @@ static tsi_result ssl_handshaker_get_result(tsi_ssl_handshaker* impl) {
1850
2087
  return impl->result;
1851
2088
  }
1852
2089
 
1853
- static tsi_result ssl_handshaker_do_handshake(tsi_ssl_handshaker* impl,
1854
- std::string* error) {
2090
+ static tsi_result ssl_handshaker_do_handshake(tsi_ssl_handshaker* impl)
2091
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
1855
2092
  if (ssl_handshaker_get_result(impl) != TSI_HANDSHAKE_IN_PROGRESS) {
1856
2093
  impl->result = TSI_OK;
1857
2094
  return impl->result;
@@ -1872,6 +2109,10 @@ static tsi_result ssl_handshaker_do_handshake(tsi_ssl_handshaker* impl,
1872
2109
  return TSI_OK;
1873
2110
  case SSL_ERROR_WANT_WRITE:
1874
2111
  return TSI_DRAIN_BUFFER;
2112
+ #if defined(OPENSSL_IS_BORINGSSL)
2113
+ case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
2114
+ return TSI_ASYNC;
2115
+ #endif
1875
2116
  default: {
1876
2117
  char err_str[256];
1877
2118
  ERR_error_string_n(ERR_get_error(), err_str, sizeof(err_str));
@@ -1882,12 +2123,16 @@ static tsi_result ssl_handshaker_do_handshake(tsi_ssl_handshaker* impl,
1882
2123
  verify_result_str = absl::StrCat(": ", verify_err);
1883
2124
  }
1884
2125
  LOG(INFO) << "Handshake failed with error "
1885
- << grpc_core::SslErrorString(ssl_result) << ": " << err_str
2126
+ << tsi::SslErrorString(ssl_result) << ": " << err_str
1886
2127
  << verify_result_str;
1887
- if (error != nullptr) {
1888
- *error = absl::StrCat(grpc_core::SslErrorString(ssl_result), ": ",
1889
- err_str, verify_result_str);
1890
- }
2128
+ std::string signer_error;
2129
+ #if defined(OPENSSL_IS_BORINGSSL)
2130
+ signer_error =
2131
+ absl::StrCat(": ", impl->signed_bytes.status().ToString());
2132
+ #endif
2133
+ impl->MaybeSetError(absl::StrCat(tsi::SslErrorString(ssl_result), ": ",
2134
+ err_str, verify_result_str,
2135
+ signer_error));
1891
2136
  impl->result = TSI_PROTOCOL_FAILURE;
1892
2137
  return impl->result;
1893
2138
  }
@@ -1896,11 +2141,11 @@ static tsi_result ssl_handshaker_do_handshake(tsi_ssl_handshaker* impl,
1896
2141
  }
1897
2142
 
1898
2143
  static tsi_result ssl_handshaker_process_bytes_from_peer(
1899
- tsi_ssl_handshaker* impl, const unsigned char* bytes, size_t* bytes_size,
1900
- std::string* error) {
2144
+ tsi_ssl_handshaker* impl, const unsigned char* bytes, size_t* bytes_size)
2145
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
1901
2146
  int bytes_written_into_ssl_size = 0;
1902
2147
  if (bytes == nullptr || bytes_size == nullptr || *bytes_size > INT_MAX) {
1903
- if (error != nullptr) *error = "invalid argument";
2148
+ impl->MaybeSetError("invalid argument");
1904
2149
  return TSI_INVALID_ARGUMENT;
1905
2150
  }
1906
2151
  GRPC_CHECK_LE(*bytes_size, static_cast<size_t>(INT_MAX));
@@ -1908,32 +2153,28 @@ static tsi_result ssl_handshaker_process_bytes_from_peer(
1908
2153
  BIO_write(impl->network_io, bytes, static_cast<int>(*bytes_size));
1909
2154
  if (bytes_written_into_ssl_size < 0) {
1910
2155
  LOG(ERROR) << "Could not write to memory BIO.";
1911
- if (error != nullptr) *error = "could not write to memory BIO";
2156
+ impl->MaybeSetError("could not write to memory BIO");
1912
2157
  impl->result = TSI_INTERNAL_ERROR;
1913
2158
  return impl->result;
1914
2159
  }
1915
2160
  *bytes_size = static_cast<size_t>(bytes_written_into_ssl_size);
1916
- return ssl_handshaker_do_handshake(impl, error);
2161
+ return ssl_handshaker_do_handshake(impl);
1917
2162
  }
1918
2163
 
1919
2164
  static void ssl_handshaker_destroy(tsi_handshaker* self) {
1920
- tsi_ssl_handshaker* impl = reinterpret_cast<tsi_ssl_handshaker*>(self);
1921
- SSL_free(impl->ssl);
1922
- BIO_free(impl->network_io);
1923
- gpr_free(impl->outgoing_bytes_buffer);
1924
- tsi_ssl_handshaker_factory_unref(impl->factory_ref);
1925
- gpr_free(impl);
2165
+ tsi_ssl_handshaker* impl = static_cast<tsi_ssl_handshaker*>(self);
2166
+ impl->Unref();
1926
2167
  }
1927
2168
 
1928
2169
  // Removes the bytes remaining in |impl->SSL|'s read BIO and writes them to
1929
2170
  // |bytes_remaining|.
1930
2171
  static tsi_result ssl_bytes_remaining(tsi_ssl_handshaker* impl,
1931
2172
  unsigned char** bytes_remaining,
1932
- size_t* bytes_remaining_size,
1933
- std::string* error) {
1934
- if (impl == nullptr || bytes_remaining == nullptr ||
1935
- bytes_remaining_size == nullptr) {
1936
- if (error != nullptr) *error = "invalid argument";
2173
+ size_t* bytes_remaining_size)
2174
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
2175
+ if (impl == nullptr) return TSI_INVALID_ARGUMENT;
2176
+ if (bytes_remaining == nullptr || bytes_remaining_size == nullptr) {
2177
+ impl->MaybeSetError("invalid argument");
1937
2178
  return TSI_INVALID_ARGUMENT;
1938
2179
  }
1939
2180
  // Attempt to read all of the bytes in SSL's read BIO. These bytes should
@@ -1951,9 +2192,8 @@ static tsi_result ssl_bytes_remaining(tsi_ssl_handshaker* impl,
1951
2192
  << "Failed to read the expected number of bytes from SSL object.";
1952
2193
  gpr_free(*bytes_remaining);
1953
2194
  *bytes_remaining = nullptr;
1954
- if (error != nullptr) {
1955
- *error = "Failed to read the expected number of bytes from SSL object.";
1956
- }
2195
+ impl->MaybeSetError(
2196
+ "Failed to read the expected number of bytes from SSL object.");
1957
2197
  return TSI_INTERNAL_ERROR;
1958
2198
  }
1959
2199
  *bytes_remaining_size = static_cast<size_t>(bytes_read);
@@ -1964,16 +2204,15 @@ static tsi_result ssl_bytes_remaining(tsi_ssl_handshaker* impl,
1964
2204
  // By doing that, we drain SSL bio buffer used to hold handshake data.
1965
2205
  // This API needs to be repeatedly called until all handshake data are
1966
2206
  // received from SSL.
1967
- static tsi_result ssl_handshaker_write_output_buffer(tsi_handshaker* self,
1968
- size_t* bytes_written,
1969
- std::string* error) {
1970
- tsi_ssl_handshaker* impl = reinterpret_cast<tsi_ssl_handshaker*>(self);
2207
+ static tsi_result ssl_handshaker_write_output_buffer(tsi_ssl_handshaker* impl,
2208
+ size_t* bytes_written)
2209
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
1971
2210
  tsi_result status = TSI_OK;
1972
2211
  size_t offset = *bytes_written;
1973
2212
  do {
1974
2213
  size_t to_send_size = impl->outgoing_bytes_buffer_size - offset;
1975
2214
  status = ssl_handshaker_get_bytes_to_send_to_peer(
1976
- impl, impl->outgoing_bytes_buffer + offset, &to_send_size, error);
2215
+ impl, impl->outgoing_bytes_buffer + offset, &to_send_size);
1977
2216
  offset += to_send_size;
1978
2217
  if (status == TSI_INCOMPLETE_DATA) {
1979
2218
  impl->outgoing_bytes_buffer_size *= 2;
@@ -1985,28 +2224,17 @@ static tsi_result ssl_handshaker_write_output_buffer(tsi_handshaker* self,
1985
2224
  return status;
1986
2225
  }
1987
2226
 
1988
- static tsi_result ssl_handshaker_next(tsi_handshaker* self,
1989
- const unsigned char* received_bytes,
1990
- size_t received_bytes_size,
1991
- const unsigned char** bytes_to_send,
1992
- size_t* bytes_to_send_size,
1993
- tsi_handshaker_result** handshaker_result,
1994
- tsi_handshaker_on_next_done_cb /*cb*/,
1995
- void* /*user_data*/, std::string* error) {
1996
- // Input sanity check.
1997
- if ((received_bytes_size > 0 && received_bytes == nullptr) ||
1998
- bytes_to_send == nullptr || bytes_to_send_size == nullptr ||
1999
- handshaker_result == nullptr) {
2000
- if (error != nullptr) *error = "invalid argument";
2001
- return TSI_INVALID_ARGUMENT;
2002
- }
2227
+ static tsi_result ssl_handshaker_next_impl(tsi_ssl_handshaker* self)
2228
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
2003
2229
  // If there are received bytes, process them first.
2004
- tsi_ssl_handshaker* impl = reinterpret_cast<tsi_ssl_handshaker*>(self);
2005
2230
  tsi_result status = TSI_OK;
2006
2231
  size_t bytes_written = 0;
2007
- if (received_bytes_size > 0) {
2008
- unsigned char* remaining_bytes_to_write_to_openssl =
2009
- const_cast<unsigned char*>(received_bytes);
2232
+ if (!self->handshaker_next_args->received_bytes.empty()) {
2233
+ size_t received_bytes_size =
2234
+ self->handshaker_next_args->received_bytes.size();
2235
+ unsigned char* received_bytes =
2236
+ self->handshaker_next_args->received_bytes.data();
2237
+ unsigned char* remaining_bytes_to_write_to_openssl = received_bytes;
2010
2238
  size_t remaining_bytes_to_write_to_openssl_size = received_bytes_size;
2011
2239
  size_t number_bio_write_attempts = 0;
2012
2240
  while (remaining_bytes_to_write_to_openssl_size > 0 &&
@@ -2017,32 +2245,55 @@ static tsi_result ssl_handshaker_next(tsi_handshaker* self,
2017
2245
  size_t bytes_written_to_openssl =
2018
2246
  remaining_bytes_to_write_to_openssl_size;
2019
2247
  status = ssl_handshaker_process_bytes_from_peer(
2020
- impl, remaining_bytes_to_write_to_openssl, &bytes_written_to_openssl,
2021
- error);
2248
+ self, remaining_bytes_to_write_to_openssl, &bytes_written_to_openssl);
2022
2249
  // As long as the BIO is full, drive the SSL handshake to consume bytes
2023
2250
  // from the BIO. If the SSL handshake returns any bytes, write them to
2024
2251
  // the peer.
2025
2252
  while (status == TSI_DRAIN_BUFFER) {
2026
- status =
2027
- ssl_handshaker_write_output_buffer(self, &bytes_written, error);
2028
- if (status != TSI_OK) return status;
2029
- status = ssl_handshaker_do_handshake(impl, error);
2253
+ status = ssl_handshaker_write_output_buffer(self, &bytes_written);
2254
+ if (status != TSI_OK) {
2255
+ return status;
2256
+ }
2257
+ status = ssl_handshaker_do_handshake(self);
2030
2258
  }
2031
2259
  // Move the pointer to the first byte not yet successfully written to
2032
2260
  // the BIO.
2033
2261
  remaining_bytes_to_write_to_openssl_size -= bytes_written_to_openssl;
2034
2262
  remaining_bytes_to_write_to_openssl += bytes_written_to_openssl;
2035
2263
  }
2264
+ // Update the received_bytes in handshaker_next_args.
2265
+ if (remaining_bytes_to_write_to_openssl_size > 0) {
2266
+ size_t bytes_to_remove =
2267
+ self->handshaker_next_args->received_bytes.size() -
2268
+ remaining_bytes_to_write_to_openssl_size;
2269
+ self->handshaker_next_args->received_bytes.erase(
2270
+ self->handshaker_next_args->received_bytes.begin(),
2271
+ self->handshaker_next_args->received_bytes.begin() + bytes_to_remove);
2272
+ } else {
2273
+ self->handshaker_next_args->received_bytes.clear();
2274
+ }
2275
+ #if defined(OPENSSL_IS_BORINGSSL)
2276
+ } else if (self->factory_ref->key_signer != nullptr) {
2277
+ // During the PrivateKeyOffload signature, an empty call to
2278
+ // ssl_handshaker_do_handshake needs to be forced after the async offload
2279
+ // has completed.
2280
+ status = ssl_handshaker_do_handshake(self);
2281
+ #endif
2282
+ }
2283
+
2284
+ if (status != TSI_OK) {
2285
+ return status;
2036
2286
  }
2037
- if (status != TSI_OK) return status;
2038
2287
  // Get bytes to send to the peer, if available.
2039
- status = ssl_handshaker_write_output_buffer(self, &bytes_written, error);
2040
- if (status != TSI_OK) return status;
2041
- *bytes_to_send = impl->outgoing_bytes_buffer;
2042
- *bytes_to_send_size = bytes_written;
2288
+ status = ssl_handshaker_write_output_buffer(self, &bytes_written);
2289
+ if (status != TSI_OK) {
2290
+ return status;
2291
+ }
2292
+ self->handshaker_next_args->bytes_to_send = self->outgoing_bytes_buffer;
2293
+ self->handshaker_next_args->bytes_to_send_size = bytes_written;
2043
2294
  // If handshake completes, create tsi_handshaker_result.
2044
- if (ssl_handshaker_get_result(impl) == TSI_HANDSHAKE_IN_PROGRESS) {
2045
- *handshaker_result = nullptr;
2295
+ if (ssl_handshaker_get_result(self) == TSI_HANDSHAKE_IN_PROGRESS) {
2296
+ self->handshaker_next_args->handshaker_result = nullptr;
2046
2297
  } else {
2047
2298
  // Any bytes that remain in |impl->ssl|'s read BIO after the handshake is
2048
2299
  // complete must be extracted and set to the unused bytes of the
@@ -2050,17 +2301,19 @@ static tsi_result ssl_handshaker_next(tsi_handshaker* self,
2050
2301
  // bytes from the peer that must be processed.
2051
2302
  unsigned char* unused_bytes = nullptr;
2052
2303
  size_t unused_bytes_size = 0;
2053
- status =
2054
- ssl_bytes_remaining(impl, &unused_bytes, &unused_bytes_size, error);
2055
- if (status != TSI_OK) return status;
2056
- if (unused_bytes_size > received_bytes_size) {
2304
+ status = ssl_bytes_remaining(self, &unused_bytes, &unused_bytes_size);
2305
+ if (status != TSI_OK) {
2306
+ return status;
2307
+ }
2308
+ if (unused_bytes_size >
2309
+ self->handshaker_next_args->original_received_bytes_size) {
2057
2310
  LOG(ERROR) << "More unused bytes than received bytes.";
2058
2311
  gpr_free(unused_bytes);
2059
- if (error != nullptr) *error = "More unused bytes than received bytes.";
2312
+ self->MaybeSetError("More unused bytes than received bytes.");
2060
2313
  return TSI_INTERNAL_ERROR;
2061
2314
  }
2062
- status = ssl_handshaker_result_create(impl, unused_bytes, unused_bytes_size,
2063
- handshaker_result, error);
2315
+ status =
2316
+ ssl_handshaker_result_create(self, unused_bytes, unused_bytes_size);
2064
2317
  if (status == TSI_OK) {
2065
2318
  // Indicates that the handshake has completed and that a
2066
2319
  // handshaker_result has been created.
@@ -2068,7 +2321,8 @@ static tsi_result ssl_handshaker_next(tsi_handshaker* self,
2068
2321
  // Output Cipher information
2069
2322
  if (GRPC_TRACE_FLAG_ENABLED(tsi)) {
2070
2323
  tsi_ssl_handshaker_result* result =
2071
- reinterpret_cast<tsi_ssl_handshaker_result*>(*handshaker_result);
2324
+ reinterpret_cast<tsi_ssl_handshaker_result*>(
2325
+ self->handshaker_next_args->handshaker_result);
2072
2326
  auto cipher = SSL_get_current_cipher(result->ssl);
2073
2327
  if (cipher != nullptr) {
2074
2328
  GRPC_TRACE_LOG(tsi, INFO) << absl::StrFormat(
@@ -2081,6 +2335,104 @@ static tsi_result ssl_handshaker_next(tsi_handshaker* self,
2081
2335
  return status;
2082
2336
  }
2083
2337
 
2338
+ // Wrapper for ssl_handshaker_next_impl() when called from an async callback.
2339
+ // For example, this would be called from the key signer's callback.
2340
+ static std::pair<tsi_result, std::optional<HandshakerNextArgs>>
2341
+ ssl_handshaker_next_async(tsi_ssl_handshaker* self)
2342
+ ABSL_EXCLUSIVE_LOCKS_REQUIRED(&tsi_ssl_handshaker::mu) {
2343
+ if (self->is_shutdown || !self->handshaker_next_args.has_value()) {
2344
+ return {TSI_HANDSHAKE_SHUTDOWN, std::nullopt};
2345
+ }
2346
+ tsi_result result = ssl_handshaker_next_impl(self);
2347
+ if (result != TSI_ASYNC) {
2348
+ // We now have a result to return to the caller via the callback.
2349
+ std::optional<HandshakerNextArgs> args =
2350
+ std::move(self->handshaker_next_args);
2351
+ self->handshaker_next_args.reset();
2352
+ return {result, std::move(args)};
2353
+ }
2354
+ return {TSI_ASYNC, std::nullopt};
2355
+ }
2356
+
2357
+ // Entry point when the security handshaker calls tsi_handshaker_next().
2358
+ static tsi_result ssl_handshaker_next(
2359
+ tsi_handshaker* self, const unsigned char* received_bytes,
2360
+ size_t received_bytes_size, const unsigned char** bytes_to_send,
2361
+ size_t* bytes_to_send_size, tsi_handshaker_result** handshaker_result,
2362
+ tsi_handshaker_on_next_done_cb cb, void* user_data, std::string* error) {
2363
+ // Input sanity check.
2364
+ if ((received_bytes_size > 0 && received_bytes == nullptr) ||
2365
+ bytes_to_send == nullptr || bytes_to_send_size == nullptr ||
2366
+ handshaker_result == nullptr) {
2367
+ if (error != nullptr) *error = "invalid argument";
2368
+ return TSI_INVALID_ARGUMENT;
2369
+ }
2370
+ tsi_ssl_handshaker* impl = static_cast<tsi_ssl_handshaker*>(self);
2371
+ grpc_core::MutexLock lock(&impl->mu);
2372
+ if (impl->is_shutdown) {
2373
+ if (error != nullptr) *error = "Handshaker shutdown";
2374
+ return TSI_HANDSHAKE_SHUTDOWN;
2375
+ }
2376
+ // Store args in impl->handshaker_next_args.
2377
+ impl->handshaker_next_args.emplace();
2378
+ if (received_bytes_size > 0) {
2379
+ impl->handshaker_next_args->received_bytes.assign(
2380
+ received_bytes, received_bytes + received_bytes_size);
2381
+ }
2382
+ impl->handshaker_next_args->original_received_bytes_size =
2383
+ received_bytes_size;
2384
+ impl->handshaker_next_args->cb = cb;
2385
+ impl->handshaker_next_args->user_data = user_data;
2386
+ impl->handshaker_next_args->error_ptr = error;
2387
+ // Now do the actual work.
2388
+ tsi_result result = ssl_handshaker_next_impl(impl);
2389
+ // If returning synchronously, propagate output and clear args.
2390
+ if (result != TSI_ASYNC) {
2391
+ *bytes_to_send = impl->handshaker_next_args->bytes_to_send;
2392
+ *bytes_to_send_size = impl->handshaker_next_args->bytes_to_send_size;
2393
+ *handshaker_result = impl->handshaker_next_args->handshaker_result;
2394
+ impl->handshaker_next_args.reset();
2395
+ }
2396
+ return result;
2397
+ }
2398
+
2399
+ static void ssl_handshaker_shutdown(tsi_handshaker* self) {
2400
+ #if defined(OPENSSL_IS_BORINGSSL)
2401
+ tsi_ssl_handshaker* impl = static_cast<tsi_ssl_handshaker*>(self);
2402
+ std::shared_ptr<grpc_core::PrivateKeySigner::AsyncSigningHandle>
2403
+ signing_handle;
2404
+ std::optional<HandshakerNextArgs> next_args;
2405
+ {
2406
+ grpc_core::MutexLock lock(&impl->mu);
2407
+ if (impl->ssl == nullptr) return;
2408
+ impl->is_shutdown = true;
2409
+ if (impl->factory_ref->key_signer != nullptr &&
2410
+ impl->signing_handle != nullptr) {
2411
+ signing_handle = std::move(impl->signing_handle);
2412
+ }
2413
+ if (impl->handshaker_next_args.has_value()) {
2414
+ next_args = std::move(*impl->handshaker_next_args);
2415
+ impl->handshaker_next_args.reset();
2416
+ }
2417
+ }
2418
+ if (signing_handle != nullptr) {
2419
+ impl->factory_ref->key_signer->Cancel(signing_handle);
2420
+ }
2421
+ if (next_args.has_value()) {
2422
+ grpc_event_engine::experimental::GetDefaultEventEngine()->Run(
2423
+ [args = std::move(*next_args)]() mutable {
2424
+ if (args.error_ptr != nullptr) {
2425
+ *args.error_ptr = "Handshaker shutdown";
2426
+ }
2427
+ if (args.cb != nullptr) {
2428
+ args.cb(TSI_HANDSHAKE_SHUTDOWN, args.user_data, nullptr, 0,
2429
+ args.handshaker_result);
2430
+ }
2431
+ });
2432
+ }
2433
+ #endif // defined(OPENSSL_IS_BORINGSSL)
2434
+ }
2435
+
2084
2436
  static const tsi_handshaker_vtable handshaker_vtable = {
2085
2437
  nullptr, // get_bytes_to_send_to_peer -- deprecated
2086
2438
  nullptr, // process_bytes_from_peer -- deprecated
@@ -2089,7 +2441,7 @@ static const tsi_handshaker_vtable handshaker_vtable = {
2089
2441
  nullptr, // create_frame_protector -- deprecated
2090
2442
  ssl_handshaker_destroy,
2091
2443
  ssl_handshaker_next,
2092
- nullptr, // shutdown
2444
+ ssl_handshaker_shutdown,
2093
2445
  };
2094
2446
 
2095
2447
  // --- tsi_ssl_handshaker_factory common methods. ---
@@ -2196,7 +2548,7 @@ static tsi_result create_tsi_ssl_handshaker(
2196
2548
  if (ssl_result != SSL_ERROR_WANT_READ) {
2197
2549
  LOG(ERROR)
2198
2550
  << "Unexpected error received from first SSL_do_handshake call: "
2199
- << grpc_core::SslErrorString(ssl_result);
2551
+ << tsi::SslErrorString(ssl_result);
2200
2552
  SSL_free(ssl);
2201
2553
  BIO_free(network_io);
2202
2554
  return TSI_INTERNAL_ERROR;
@@ -2205,7 +2557,7 @@ static tsi_result create_tsi_ssl_handshaker(
2205
2557
  SSL_set_accept_state(ssl);
2206
2558
  }
2207
2559
 
2208
- impl = grpc_core::Zalloc<tsi_ssl_handshaker>();
2560
+ impl = new tsi_ssl_handshaker();
2209
2561
  impl->ssl = ssl;
2210
2562
  impl->network_io = network_io;
2211
2563
  impl->result = TSI_HANDSHAKE_IN_PROGRESS;
@@ -2213,9 +2565,14 @@ static tsi_result create_tsi_ssl_handshaker(
2213
2565
  TSI_SSL_HANDSHAKER_OUTGOING_BUFFER_INITIAL_SIZE;
2214
2566
  impl->outgoing_bytes_buffer =
2215
2567
  static_cast<unsigned char*>(gpr_zalloc(impl->outgoing_bytes_buffer_size));
2216
- impl->base.vtable = &handshaker_vtable;
2568
+ impl->vtable = &handshaker_vtable;
2217
2569
  impl->factory_ref = tsi_ssl_handshaker_factory_ref(factory);
2218
- *handshaker = &impl->base;
2570
+ *handshaker = impl;
2571
+
2572
+ if (!SSL_set_ex_data(ssl, g_ssl_ex_handshaker_index, impl)) {
2573
+ return TSI_INTERNAL_ERROR;
2574
+ }
2575
+
2219
2576
  return TSI_OK;
2220
2577
  }
2221
2578
 
@@ -2456,7 +2813,8 @@ tsi_result tsi_create_ssl_client_handshaker_factory(
2456
2813
  tsi_ssl_client_handshaker_options options;
2457
2814
  options.pem_key_cert_pair = pem_key_cert_pair;
2458
2815
  if (pem_root_certs != nullptr) {
2459
- options.root_cert_info = std::make_shared<RootCertInfo>(pem_root_certs);
2816
+ options.root_cert_info =
2817
+ std::make_shared<tsi::RootCertInfo>(pem_root_certs);
2460
2818
  }
2461
2819
  options.cipher_suites = cipher_suites;
2462
2820
  options.alpn_protocols = alpn_protocols;
@@ -2490,7 +2848,7 @@ tsi_result tsi_create_ssl_client_handshaker_factory_with_options(
2490
2848
  SSL_CTX_set_options(ssl_context, SSL_OP_NO_RENEGOTIATION);
2491
2849
  #endif
2492
2850
  if (ssl_context == nullptr) {
2493
- grpc_core::LogSslErrorStack();
2851
+ tsi::LogSslErrorStack();
2494
2852
  LOG(ERROR) << "Could not create ssl context.";
2495
2853
  return TSI_INVALID_ARGUMENT;
2496
2854
  }
@@ -2537,6 +2895,18 @@ tsi_result tsi_create_ssl_client_handshaker_factory_with_options(
2537
2895
  options->cipher_suites);
2538
2896
  if (result != TSI_OK) break;
2539
2897
 
2898
+ #if defined(OPENSSL_IS_BORINGSSL)
2899
+ if (options->pem_key_cert_pair != nullptr) {
2900
+ grpc_core::Match(
2901
+ options->pem_key_cert_pair->private_key, [](const std::string&) {},
2902
+ [&](const std::shared_ptr<grpc_core::PrivateKeySigner>& key_signer) {
2903
+ // The Handshaker Factory will own a shared copy of the reference
2904
+ // passed through the options.
2905
+ impl->base.key_signer = key_signer;
2906
+ });
2907
+ }
2908
+ #endif
2909
+
2540
2910
  #if OPENSSL_VERSION_NUMBER >= 0x10100000
2541
2911
  // X509_STORE_up_ref is only available since OpenSSL 1.1.
2542
2912
  if (options->root_store != nullptr) {
@@ -2639,30 +3009,28 @@ static tsi_ssl_handshaker_factory_vtable server_handshaker_factory_vtable = {
2639
3009
  tsi_ssl_server_handshaker_factory_destroy};
2640
3010
 
2641
3011
  tsi_result tsi_create_ssl_server_handshaker_factory(
2642
- const tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs,
2643
- size_t num_key_cert_pairs, const char* pem_client_root_certs,
2644
- int force_client_auth, const char* cipher_suites,
2645
- const char** alpn_protocols, uint16_t num_alpn_protocols,
2646
- tsi_ssl_server_handshaker_factory** factory) {
3012
+ std::vector<tsi_ssl_pem_key_cert_pair> pem_key_cert_pairs,
3013
+ const char* pem_client_root_certs, int force_client_auth,
3014
+ const char* cipher_suites, const char** alpn_protocols,
3015
+ uint16_t num_alpn_protocols, tsi_ssl_server_handshaker_factory** factory) {
2647
3016
  return tsi_create_ssl_server_handshaker_factory_ex(
2648
- pem_key_cert_pairs, num_key_cert_pairs, pem_client_root_certs,
3017
+ pem_key_cert_pairs, pem_client_root_certs,
2649
3018
  force_client_auth ? TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
2650
3019
  : TSI_DONT_REQUEST_CLIENT_CERTIFICATE,
2651
3020
  cipher_suites, alpn_protocols, num_alpn_protocols, factory);
2652
3021
  }
2653
3022
 
2654
3023
  tsi_result tsi_create_ssl_server_handshaker_factory_ex(
2655
- const tsi_ssl_pem_key_cert_pair* pem_key_cert_pairs,
2656
- size_t num_key_cert_pairs, const char* pem_client_root_certs,
3024
+ std::vector<tsi_ssl_pem_key_cert_pair> pem_key_cert_pairs,
3025
+ const char* pem_client_root_certs,
2657
3026
  tsi_client_certificate_request_type client_certificate_request,
2658
3027
  const char* cipher_suites, const char** alpn_protocols,
2659
3028
  uint16_t num_alpn_protocols, tsi_ssl_server_handshaker_factory** factory) {
2660
3029
  tsi_ssl_server_handshaker_options options;
2661
3030
  options.pem_key_cert_pairs = pem_key_cert_pairs;
2662
- options.num_key_cert_pairs = num_key_cert_pairs;
2663
3031
  if (pem_client_root_certs != nullptr) {
2664
3032
  options.root_cert_info =
2665
- std::make_shared<RootCertInfo>(pem_client_root_certs);
3033
+ std::make_shared<tsi::RootCertInfo>(pem_client_root_certs);
2666
3034
  }
2667
3035
  options.client_certificate_request = client_certificate_request;
2668
3036
  options.cipher_suites = cipher_suites;
@@ -2683,8 +3051,7 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
2683
3051
 
2684
3052
  if (factory == nullptr) return TSI_INVALID_ARGUMENT;
2685
3053
  *factory = nullptr;
2686
- if (options->num_key_cert_pairs == 0 ||
2687
- options->pem_key_cert_pairs == nullptr) {
3054
+ if (options->pem_key_cert_pairs.empty()) {
2688
3055
  return TSI_INVALID_ARGUMENT;
2689
3056
  }
2690
3057
 
@@ -2693,15 +3060,15 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
2693
3060
  impl->base.vtable = &server_handshaker_factory_vtable;
2694
3061
 
2695
3062
  impl->ssl_contexts = static_cast<SSL_CTX**>(
2696
- gpr_zalloc(options->num_key_cert_pairs * sizeof(SSL_CTX*)));
3063
+ gpr_zalloc(options->pem_key_cert_pairs.size() * sizeof(SSL_CTX*)));
2697
3064
  impl->ssl_context_x509_subject_names = static_cast<tsi_peer*>(
2698
- gpr_zalloc(options->num_key_cert_pairs * sizeof(tsi_peer)));
3065
+ gpr_zalloc(options->pem_key_cert_pairs.size() * sizeof(tsi_peer)));
2699
3066
  if (impl->ssl_contexts == nullptr ||
2700
3067
  impl->ssl_context_x509_subject_names == nullptr) {
2701
3068
  tsi_ssl_handshaker_factory_unref(&impl->base);
2702
3069
  return TSI_OUT_OF_RESOURCES;
2703
3070
  }
2704
- impl->ssl_context_count = options->num_key_cert_pairs;
3071
+ impl->ssl_context_count = options->pem_key_cert_pairs.size();
2705
3072
  if (options->root_cert_info != nullptr) {
2706
3073
  impl->root_cert_info = options->root_cert_info;
2707
3074
  }
@@ -2720,7 +3087,7 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
2720
3087
  impl->key_logger = options->key_logger->Ref();
2721
3088
  }
2722
3089
 
2723
- for (i = 0; i < options->num_key_cert_pairs; i++) {
3090
+ for (i = 0; i < options->pem_key_cert_pairs.size(); i++) {
2724
3091
  do {
2725
3092
  #if OPENSSL_VERSION_NUMBER >= 0x10100000
2726
3093
  impl->ssl_contexts[i] = SSL_CTX_new(TLS_method());
@@ -2731,7 +3098,7 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
2731
3098
  SSL_CTX_set_options(impl->ssl_contexts[i], SSL_OP_NO_RENEGOTIATION);
2732
3099
  #endif
2733
3100
  if (impl->ssl_contexts[i] == nullptr) {
2734
- grpc_core::LogSslErrorStack();
3101
+ tsi::LogSslErrorStack();
2735
3102
  LOG(ERROR) << "Could not create ssl context.";
2736
3103
  result = TSI_OUT_OF_RESOURCES;
2737
3104
  break;
@@ -2747,6 +3114,16 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
2747
3114
  options->cipher_suites);
2748
3115
  if (result != TSI_OK) break;
2749
3116
 
3117
+ #if defined(OPENSSL_IS_BORINGSSL)
3118
+ if (impl->base.key_signer == nullptr) {
3119
+ grpc_core::Match(
3120
+ options->pem_key_cert_pairs[i].private_key,
3121
+ [](const std::string&) {},
3122
+ [&](const std::shared_ptr<grpc_core::PrivateKeySigner>&
3123
+ key_signer) { impl->base.key_signer = key_signer; });
3124
+ }
3125
+ #endif
3126
+
2750
3127
  // TODO(elessar): Provide ability to disable session ticket keys.
2751
3128
 
2752
3129
  // Allow client cache sessions (it's needed for OpenSSL only).
@@ -2849,7 +3226,7 @@ tsi_result tsi_create_ssl_server_handshaker_factory_with_options(
2849
3226
  #endif
2850
3227
 
2851
3228
  result = tsi_ssl_extract_x509_subject_names_from_pem_cert(
2852
- options->pem_key_cert_pairs[i].cert_chain,
3229
+ options->pem_key_cert_pairs[i].cert_chain.c_str(),
2853
3230
  &impl->ssl_context_x509_subject_names[i]);
2854
3231
  if (result != TSI_OK) break;
2855
3232
 
@@ -2931,15 +3308,17 @@ int tsi_ssl_peer_matches_name(const tsi_peer* peer, absl::string_view name) {
2931
3308
  return 0; // Not found.
2932
3309
  }
2933
3310
 
2934
- bool IsRootCertInfoEmpty(const RootCertInfo* root_cert_info) {
3311
+ namespace tsi {
3312
+ bool IsRootCertInfoEmpty(const tsi::RootCertInfo* root_cert_info) {
2935
3313
  if (root_cert_info == nullptr) return true;
2936
- return Match(
3314
+ return grpc_core::Match(
2937
3315
  *root_cert_info,
2938
3316
  [&](const std::string& pem_root_certs) { return pem_root_certs.empty(); },
2939
3317
  [&](const grpc_core::SpiffeBundleMap& spiffe_bundle_map) {
2940
3318
  return spiffe_bundle_map.size() == 0;
2941
3319
  });
2942
3320
  }
3321
+ } // namespace tsi
2943
3322
 
2944
3323
  // --- Testing support. ---
2945
3324
  const tsi_ssl_handshaker_factory_vtable* tsi_ssl_handshaker_factory_swap_vtable(