grpc 1.18.0 → 1.19.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +301 -33
  3. data/include/grpc/grpc_security.h +195 -0
  4. data/include/grpc/impl/codegen/grpc_types.h +17 -1
  5. data/include/grpc/impl/codegen/port_platform.h +36 -0
  6. data/include/grpc/impl/codegen/slice.h +1 -1
  7. data/src/core/ext/filters/client_channel/channel_connectivity.cc +2 -0
  8. data/src/core/ext/filters/client_channel/client_channel.cc +74 -69
  9. data/src/core/ext/filters/client_channel/client_channel.h +2 -2
  10. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +5 -6
  11. data/src/core/ext/filters/client_channel/client_channel_channelz.h +5 -4
  12. data/src/core/ext/filters/client_channel/client_channel_factory.cc +2 -2
  13. data/src/core/ext/filters/client_channel/client_channel_factory.h +4 -4
  14. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +3 -3
  15. data/src/core/ext/filters/client_channel/global_subchannel_pool.cc +176 -0
  16. data/src/core/ext/filters/client_channel/global_subchannel_pool.h +68 -0
  17. data/src/core/ext/filters/client_channel/health/health_check_client.cc +10 -8
  18. data/src/core/ext/filters/client_channel/health/health_check_client.h +1 -1
  19. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +146 -156
  20. data/src/core/ext/filters/client_channel/lb_policy.cc +30 -1
  21. data/src/core/ext/filters/client_channel/lb_policy.h +29 -1
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +28 -30
  23. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +5 -8
  24. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +5 -8
  25. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +23 -24
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +80 -15
  27. data/src/core/ext/filters/client_channel/lb_policy_factory.h +6 -1
  28. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +2 -2
  29. data/src/core/ext/filters/client_channel/lb_policy_registry.h +1 -1
  30. data/src/core/ext/filters/client_channel/local_subchannel_pool.cc +96 -0
  31. data/src/core/ext/filters/client_channel/local_subchannel_pool.h +56 -0
  32. data/src/core/ext/filters/client_channel/parse_address.cc +24 -5
  33. data/src/core/ext/filters/client_channel/request_routing.cc +13 -3
  34. data/src/core/ext/filters/client_channel/request_routing.h +5 -1
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +11 -6
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +2 -2
  37. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +7 -35
  38. data/src/core/ext/filters/client_channel/subchannel.cc +698 -791
  39. data/src/core/ext/filters/client_channel/subchannel.h +213 -123
  40. data/src/core/ext/filters/client_channel/subchannel_pool_interface.cc +97 -0
  41. data/src/core/ext/filters/client_channel/subchannel_pool_interface.h +94 -0
  42. data/src/core/ext/filters/http/client_authority_filter.cc +5 -2
  43. data/src/core/ext/filters/max_age/max_age_filter.cc +1 -1
  44. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +13 -12
  45. data/src/core/ext/transport/chttp2/client/insecure/channel_create.cc +5 -7
  46. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +19 -27
  47. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +18 -19
  48. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +27 -6
  49. data/src/core/ext/transport/chttp2/transport/flow_control.cc +1 -1
  50. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +3 -2
  51. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +1 -1
  52. data/src/core/ext/transport/chttp2/transport/writing.cc +8 -5
  53. data/src/core/lib/channel/handshaker.cc +141 -214
  54. data/src/core/lib/channel/handshaker.h +110 -101
  55. data/src/core/lib/channel/handshaker_factory.h +11 -19
  56. data/src/core/lib/channel/handshaker_registry.cc +64 -52
  57. data/src/core/lib/channel/handshaker_registry.h +21 -16
  58. data/src/core/lib/gpr/log_posix.cc +2 -1
  59. data/src/core/lib/gpr/time.cc +8 -0
  60. data/src/core/lib/gpr/time_posix.cc +8 -2
  61. data/src/core/lib/gprpp/optional.h +47 -0
  62. data/src/core/lib/http/httpcli_security_connector.cc +13 -14
  63. data/src/core/lib/iomgr/buffer_list.cc +182 -24
  64. data/src/core/lib/iomgr/buffer_list.h +70 -8
  65. data/src/core/lib/iomgr/combiner.cc +11 -3
  66. data/src/core/lib/iomgr/error.cc +9 -5
  67. data/src/core/lib/iomgr/ev_epoll1_linux.cc +3 -0
  68. data/src/core/lib/iomgr/ev_epollex_linux.cc +136 -162
  69. data/src/core/lib/iomgr/ev_poll_posix.cc +3 -0
  70. data/src/core/lib/iomgr/ev_posix.cc +4 -0
  71. data/src/core/lib/iomgr/ev_posix.h +4 -0
  72. data/src/core/lib/iomgr/exec_ctx.cc +1 -0
  73. data/src/core/lib/iomgr/exec_ctx.h +137 -8
  74. data/src/core/lib/iomgr/executor.cc +122 -87
  75. data/src/core/lib/iomgr/executor.h +53 -48
  76. data/src/core/lib/iomgr/fork_posix.cc +6 -4
  77. data/src/core/lib/iomgr/{network_status_tracker.cc → grpc_if_nametoindex.h} +8 -14
  78. data/src/core/lib/iomgr/grpc_if_nametoindex_posix.cc +42 -0
  79. data/src/core/lib/iomgr/{network_status_tracker.h → grpc_if_nametoindex_unsupported.cc} +15 -9
  80. data/src/core/lib/iomgr/internal_errqueue.h +105 -3
  81. data/src/core/lib/iomgr/iomgr.cc +6 -5
  82. data/src/core/lib/iomgr/iomgr.h +8 -0
  83. data/src/core/lib/iomgr/iomgr_custom.cc +6 -2
  84. data/src/core/lib/iomgr/iomgr_internal.cc +4 -0
  85. data/src/core/lib/iomgr/iomgr_internal.h +4 -0
  86. data/src/core/lib/iomgr/iomgr_posix.cc +10 -1
  87. data/src/core/lib/iomgr/iomgr_windows.cc +8 -1
  88. data/src/core/lib/iomgr/port.h +1 -0
  89. data/src/core/lib/iomgr/resolve_address_posix.cc +4 -3
  90. data/src/core/lib/iomgr/resolve_address_windows.cc +2 -1
  91. data/src/core/lib/iomgr/tcp_custom.cc +0 -4
  92. data/src/core/lib/iomgr/tcp_posix.cc +58 -44
  93. data/src/core/lib/iomgr/tcp_uv.cc +0 -1
  94. data/src/core/lib/iomgr/tcp_windows.cc +0 -4
  95. data/src/core/lib/iomgr/timer_manager.cc +8 -0
  96. data/src/core/lib/iomgr/udp_server.cc +6 -4
  97. data/src/core/lib/json/json.cc +1 -4
  98. data/src/core/lib/security/credentials/alts/alts_credentials.cc +1 -1
  99. data/src/core/lib/security/credentials/alts/check_gcp_environment_no_op.cc +2 -2
  100. data/src/core/lib/security/credentials/composite/composite_credentials.h +4 -0
  101. data/src/core/lib/security/credentials/credentials.h +9 -1
  102. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +15 -2
  103. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +2 -0
  104. data/src/core/lib/security/credentials/jwt/json_token.cc +1 -1
  105. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +1 -0
  106. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +3 -2
  107. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +2 -2
  108. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +1 -0
  109. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +192 -0
  110. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +213 -0
  111. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +10 -8
  112. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +6 -10
  113. data/src/core/lib/security/security_connector/local/local_security_connector.cc +10 -8
  114. data/src/core/lib/security/security_connector/security_connector.h +2 -2
  115. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +4 -6
  116. data/src/core/lib/security/security_connector/ssl_utils.h +33 -0
  117. data/src/core/lib/security/transport/security_handshaker.cc +267 -300
  118. data/src/core/lib/security/transport/security_handshaker.h +11 -2
  119. data/src/core/lib/security/transport/server_auth_filter.cc +1 -0
  120. data/src/core/lib/surface/call.cc +5 -1
  121. data/src/core/lib/surface/channel_init.h +5 -0
  122. data/src/core/lib/surface/completion_queue.cc +4 -7
  123. data/src/core/lib/surface/init.cc +5 -3
  124. data/src/core/lib/surface/init_secure.cc +1 -1
  125. data/src/core/lib/surface/server.cc +19 -17
  126. data/src/core/lib/surface/version.cc +1 -1
  127. data/src/core/lib/transport/service_config.h +1 -0
  128. data/src/core/lib/transport/static_metadata.cc +279 -279
  129. data/src/core/lib/transport/transport.cc +5 -3
  130. data/src/core/tsi/ssl_transport_security.cc +10 -4
  131. data/src/ruby/ext/grpc/extconf.rb +12 -4
  132. data/src/ruby/ext/grpc/rb_call_credentials.c +8 -5
  133. data/src/ruby/ext/grpc/rb_channel.c +14 -10
  134. data/src/ruby/ext/grpc/rb_channel_credentials.c +8 -4
  135. data/src/ruby/ext/grpc/rb_compression_options.c +9 -7
  136. data/src/ruby/ext/grpc/rb_event_thread.c +2 -0
  137. data/src/ruby/ext/grpc/rb_grpc.c +22 -23
  138. data/src/ruby/ext/grpc/rb_grpc.h +4 -2
  139. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +18 -0
  140. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +27 -0
  141. data/src/ruby/ext/grpc/rb_server.c +8 -4
  142. data/src/ruby/lib/grpc/version.rb +1 -1
  143. metadata +46 -39
  144. data/src/core/ext/filters/client_channel/subchannel_index.cc +0 -248
  145. data/src/core/ext/filters/client_channel/subchannel_index.h +0 -76
  146. data/src/core/lib/channel/handshaker_factory.cc +0 -42
@@ -80,8 +80,9 @@ class grpc_alts_channel_security_connector final
80
80
 
81
81
  ~grpc_alts_channel_security_connector() override { gpr_free(target_name_); }
82
82
 
83
- void add_handshakers(grpc_pollset_set* interested_parties,
84
- grpc_handshake_manager* handshake_manager) override {
83
+ void add_handshakers(
84
+ grpc_pollset_set* interested_parties,
85
+ grpc_core::HandshakeManager* handshake_manager) override {
85
86
  tsi_handshaker* handshaker = nullptr;
86
87
  const grpc_alts_credentials* creds =
87
88
  static_cast<const grpc_alts_credentials*>(channel_creds());
@@ -89,8 +90,8 @@ class grpc_alts_channel_security_connector final
89
90
  creds->handshaker_service_url(), true,
90
91
  interested_parties,
91
92
  &handshaker) == TSI_OK);
92
- grpc_handshake_manager_add(
93
- handshake_manager, grpc_security_handshaker_create(handshaker, this));
93
+ handshake_manager->Add(
94
+ grpc_core::SecurityHandshakerCreate(handshaker, this));
94
95
  }
95
96
 
96
97
  void check_peer(tsi_peer peer, grpc_endpoint* ep,
@@ -139,16 +140,17 @@ class grpc_alts_server_security_connector final
139
140
  }
140
141
  ~grpc_alts_server_security_connector() override = default;
141
142
 
142
- void add_handshakers(grpc_pollset_set* interested_parties,
143
- grpc_handshake_manager* handshake_manager) override {
143
+ void add_handshakers(
144
+ grpc_pollset_set* interested_parties,
145
+ grpc_core::HandshakeManager* handshake_manager) override {
144
146
  tsi_handshaker* handshaker = nullptr;
145
147
  const grpc_alts_server_credentials* creds =
146
148
  static_cast<const grpc_alts_server_credentials*>(server_creds());
147
149
  GPR_ASSERT(alts_tsi_handshaker_create(
148
150
  creds->options(), nullptr, creds->handshaker_service_url(),
149
151
  false, interested_parties, &handshaker) == TSI_OK);
150
- grpc_handshake_manager_add(
151
- handshake_manager, grpc_security_handshaker_create(handshaker, this));
152
+ handshake_manager->Add(
153
+ grpc_core::SecurityHandshakerCreate(handshaker, this));
152
154
  }
153
155
 
154
156
  void check_peer(tsi_peer peer, grpc_endpoint* ep,
@@ -92,11 +92,9 @@ class grpc_fake_channel_security_connector final
92
92
  }
93
93
 
94
94
  void add_handshakers(grpc_pollset_set* interested_parties,
95
- grpc_handshake_manager* handshake_mgr) override {
96
- grpc_handshake_manager_add(
97
- handshake_mgr,
98
- grpc_security_handshaker_create(
99
- tsi_create_fake_handshaker(/*is_client=*/true), this));
95
+ grpc_core::HandshakeManager* handshake_mgr) override {
96
+ handshake_mgr->Add(grpc_core::SecurityHandshakerCreate(
97
+ tsi_create_fake_handshaker(/*is_client=*/true), this));
100
98
  }
101
99
 
102
100
  bool check_call_host(const char* host, grpc_auth_context* auth_context,
@@ -273,11 +271,9 @@ class grpc_fake_server_security_connector
273
271
  }
274
272
 
275
273
  void add_handshakers(grpc_pollset_set* interested_parties,
276
- grpc_handshake_manager* handshake_mgr) override {
277
- grpc_handshake_manager_add(
278
- handshake_mgr,
279
- grpc_security_handshaker_create(
280
- tsi_create_fake_handshaker(/*=is_client*/ false), this));
274
+ grpc_core::HandshakeManager* handshake_mgr) override {
275
+ handshake_mgr->Add(grpc_core::SecurityHandshakerCreate(
276
+ tsi_create_fake_handshaker(/*=is_client*/ false), this));
281
277
  }
282
278
 
283
279
  int cmp(const grpc_security_connector* other) const override {
@@ -128,13 +128,14 @@ class grpc_local_channel_security_connector final
128
128
 
129
129
  ~grpc_local_channel_security_connector() override { gpr_free(target_name_); }
130
130
 
131
- void add_handshakers(grpc_pollset_set* interested_parties,
132
- grpc_handshake_manager* handshake_manager) override {
131
+ void add_handshakers(
132
+ grpc_pollset_set* interested_parties,
133
+ grpc_core::HandshakeManager* handshake_manager) override {
133
134
  tsi_handshaker* handshaker = nullptr;
134
135
  GPR_ASSERT(local_tsi_handshaker_create(true /* is_client */, &handshaker) ==
135
136
  TSI_OK);
136
- grpc_handshake_manager_add(
137
- handshake_manager, grpc_security_handshaker_create(handshaker, this));
137
+ handshake_manager->Add(
138
+ grpc_core::SecurityHandshakerCreate(handshaker, this));
138
139
  }
139
140
 
140
141
  int cmp(const grpc_security_connector* other_sc) const override {
@@ -184,13 +185,14 @@ class grpc_local_server_security_connector final
184
185
  : grpc_server_security_connector(nullptr, std::move(server_creds)) {}
185
186
  ~grpc_local_server_security_connector() override = default;
186
187
 
187
- void add_handshakers(grpc_pollset_set* interested_parties,
188
- grpc_handshake_manager* handshake_manager) override {
188
+ void add_handshakers(
189
+ grpc_pollset_set* interested_parties,
190
+ grpc_core::HandshakeManager* handshake_manager) override {
189
191
  tsi_handshaker* handshaker = nullptr;
190
192
  GPR_ASSERT(local_tsi_handshaker_create(false /* is_client */,
191
193
  &handshaker) == TSI_OK);
192
- grpc_handshake_manager_add(
193
- handshake_manager, grpc_security_handshaker_create(handshaker, this));
194
+ handshake_manager->Add(
195
+ grpc_core::SecurityHandshakerCreate(handshaker, this));
194
196
  }
195
197
 
196
198
  void check_peer(tsi_peer peer, grpc_endpoint* ep,
@@ -109,7 +109,7 @@ class grpc_channel_security_connector : public grpc_security_connector {
109
109
  grpc_error* error) GRPC_ABSTRACT;
110
110
  /// Registers handshakers with \a handshake_mgr.
111
111
  virtual void add_handshakers(grpc_pollset_set* interested_parties,
112
- grpc_handshake_manager* handshake_mgr)
112
+ grpc_core::HandshakeManager* handshake_mgr)
113
113
  GRPC_ABSTRACT;
114
114
 
115
115
  const grpc_channel_credentials* channel_creds() const {
@@ -150,7 +150,7 @@ class grpc_server_security_connector : public grpc_security_connector {
150
150
  ~grpc_server_security_connector() override = default;
151
151
 
152
152
  virtual void add_handshakers(grpc_pollset_set* interested_parties,
153
- grpc_handshake_manager* handshake_mgr)
153
+ grpc_core::HandshakeManager* handshake_mgr)
154
154
  GRPC_ABSTRACT;
155
155
 
156
156
  const grpc_server_credentials* server_creds() const {
@@ -128,7 +128,7 @@ class grpc_ssl_channel_security_connector final
128
128
  }
129
129
 
130
130
  void add_handshakers(grpc_pollset_set* interested_parties,
131
- grpc_handshake_manager* handshake_mgr) override {
131
+ grpc_core::HandshakeManager* handshake_mgr) override {
132
132
  // Instantiate TSI handshaker.
133
133
  tsi_handshaker* tsi_hs = nullptr;
134
134
  tsi_result result = tsi_ssl_client_handshaker_factory_create_handshaker(
@@ -142,8 +142,7 @@ class grpc_ssl_channel_security_connector final
142
142
  return;
143
143
  }
144
144
  // Create handshakers.
145
- grpc_handshake_manager_add(handshake_mgr,
146
- grpc_security_handshaker_create(tsi_hs, this));
145
+ handshake_mgr->Add(grpc_core::SecurityHandshakerCreate(tsi_hs, this));
147
146
  }
148
147
 
149
148
  void check_peer(tsi_peer peer, grpc_endpoint* ep,
@@ -283,7 +282,7 @@ class grpc_ssl_server_security_connector
283
282
  }
284
283
 
285
284
  void add_handshakers(grpc_pollset_set* interested_parties,
286
- grpc_handshake_manager* handshake_mgr) override {
285
+ grpc_core::HandshakeManager* handshake_mgr) override {
287
286
  // Instantiate TSI handshaker.
288
287
  try_fetch_ssl_server_credentials();
289
288
  tsi_handshaker* tsi_hs = nullptr;
@@ -295,8 +294,7 @@ class grpc_ssl_server_security_connector
295
294
  return;
296
295
  }
297
296
  // Create handshakers.
298
- grpc_handshake_manager_add(handshake_mgr,
299
- grpc_security_handshaker_create(tsi_hs, this));
297
+ handshake_mgr->Add(grpc_core::SecurityHandshakerCreate(tsi_hs, this));
300
298
  }
301
299
 
302
300
  void check_peer(tsi_peer peer, grpc_endpoint* ep,
@@ -89,6 +89,39 @@ class DefaultSslRootStore {
89
89
  static grpc_slice default_pem_root_certs_;
90
90
  };
91
91
 
92
+ class PemKeyCertPair {
93
+ public:
94
+ // Construct from the C struct. We steal its members and then immediately
95
+ // free it.
96
+ explicit PemKeyCertPair(grpc_ssl_pem_key_cert_pair* pair)
97
+ : private_key_(const_cast<char*>(pair->private_key)),
98
+ cert_chain_(const_cast<char*>(pair->cert_chain)) {
99
+ gpr_free(pair);
100
+ }
101
+
102
+ // Movable.
103
+ PemKeyCertPair(PemKeyCertPair&& other) {
104
+ private_key_ = std::move(other.private_key_);
105
+ cert_chain_ = std::move(other.cert_chain_);
106
+ }
107
+ PemKeyCertPair& operator=(PemKeyCertPair&& other) {
108
+ private_key_ = std::move(other.private_key_);
109
+ cert_chain_ = std::move(other.cert_chain_);
110
+ return *this;
111
+ }
112
+
113
+ // Not copyable.
114
+ PemKeyCertPair(const PemKeyCertPair&) = delete;
115
+ PemKeyCertPair& operator=(const PemKeyCertPair&) = delete;
116
+
117
+ char* private_key() const { return private_key_.get(); }
118
+ char* cert_chain() const { return cert_chain_.get(); }
119
+
120
+ private:
121
+ grpc_core::UniquePtr<char> private_key_;
122
+ grpc_core::UniquePtr<char> cert_chain_;
123
+ };
124
+
92
125
  } // namespace grpc_core
93
126
 
94
127
  #endif /* GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_SSL_UTILS_H \
@@ -39,74 +39,113 @@
39
39
 
40
40
  #define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256
41
41
 
42
- namespace {
43
- struct security_handshaker {
44
- security_handshaker(tsi_handshaker* handshaker,
45
- grpc_security_connector* connector);
46
- ~security_handshaker() {
47
- gpr_mu_destroy(&mu);
48
- tsi_handshaker_destroy(handshaker);
49
- tsi_handshaker_result_destroy(handshaker_result);
50
- if (endpoint_to_destroy != nullptr) {
51
- grpc_endpoint_destroy(endpoint_to_destroy);
52
- }
53
- if (read_buffer_to_destroy != nullptr) {
54
- grpc_slice_buffer_destroy_internal(read_buffer_to_destroy);
55
- gpr_free(read_buffer_to_destroy);
56
- }
57
- gpr_free(handshake_buffer);
58
- grpc_slice_buffer_destroy_internal(&outgoing);
59
- auth_context.reset(DEBUG_LOCATION, "handshake");
60
- connector.reset(DEBUG_LOCATION, "handshake");
61
- }
42
+ namespace grpc_core {
62
43
 
63
- void Ref() { refs.Ref(); }
64
- void Unref() {
65
- if (refs.Unref()) {
66
- grpc_core::Delete(this);
67
- }
68
- }
44
+ namespace {
69
45
 
70
- grpc_handshaker base;
46
+ class SecurityHandshaker : public Handshaker {
47
+ public:
48
+ SecurityHandshaker(tsi_handshaker* handshaker,
49
+ grpc_security_connector* connector);
50
+ ~SecurityHandshaker() override;
51
+ void Shutdown(grpc_error* why) override;
52
+ void DoHandshake(grpc_tcp_server_acceptor* acceptor,
53
+ grpc_closure* on_handshake_done,
54
+ HandshakerArgs* args) override;
55
+ const char* name() const override { return "security"; }
56
+
57
+ private:
58
+ grpc_error* DoHandshakerNextLocked(const unsigned char* bytes_received,
59
+ size_t bytes_received_size);
60
+
61
+ grpc_error* OnHandshakeNextDoneLocked(
62
+ tsi_result result, const unsigned char* bytes_to_send,
63
+ size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result);
64
+ void HandshakeFailedLocked(grpc_error* error);
65
+ void CleanupArgsForFailureLocked();
66
+
67
+ static void OnHandshakeDataReceivedFromPeerFn(void* arg, grpc_error* error);
68
+ static void OnHandshakeDataSentToPeerFn(void* arg, grpc_error* error);
69
+ static void OnHandshakeNextDoneGrpcWrapper(
70
+ tsi_result result, void* user_data, const unsigned char* bytes_to_send,
71
+ size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result);
72
+ static void OnPeerCheckedFn(void* arg, grpc_error* error);
73
+ void OnPeerCheckedInner(grpc_error* error);
74
+ size_t MoveReadBufferIntoHandshakeBuffer();
75
+ grpc_error* CheckPeerLocked();
71
76
 
72
77
  // State set at creation time.
73
- tsi_handshaker* handshaker;
74
- grpc_core::RefCountedPtr<grpc_security_connector> connector;
78
+ tsi_handshaker* handshaker_;
79
+ RefCountedPtr<grpc_security_connector> connector_;
75
80
 
76
- gpr_mu mu;
77
- grpc_core::RefCount refs;
81
+ gpr_mu mu_;
78
82
 
79
- bool shutdown = false;
83
+ bool is_shutdown_ = false;
80
84
  // Endpoint and read buffer to destroy after a shutdown.
81
- grpc_endpoint* endpoint_to_destroy = nullptr;
82
- grpc_slice_buffer* read_buffer_to_destroy = nullptr;
85
+ grpc_endpoint* endpoint_to_destroy_ = nullptr;
86
+ grpc_slice_buffer* read_buffer_to_destroy_ = nullptr;
83
87
 
84
88
  // State saved while performing the handshake.
85
- grpc_handshaker_args* args = nullptr;
86
- grpc_closure* on_handshake_done = nullptr;
87
-
88
- size_t handshake_buffer_size;
89
- unsigned char* handshake_buffer;
90
- grpc_slice_buffer outgoing;
91
- grpc_closure on_handshake_data_sent_to_peer;
92
- grpc_closure on_handshake_data_received_from_peer;
93
- grpc_closure on_peer_checked;
94
- grpc_core::RefCountedPtr<grpc_auth_context> auth_context;
95
- tsi_handshaker_result* handshaker_result = nullptr;
89
+ HandshakerArgs* args_ = nullptr;
90
+ grpc_closure* on_handshake_done_ = nullptr;
91
+
92
+ size_t handshake_buffer_size_;
93
+ unsigned char* handshake_buffer_;
94
+ grpc_slice_buffer outgoing_;
95
+ grpc_closure on_handshake_data_sent_to_peer_;
96
+ grpc_closure on_handshake_data_received_from_peer_;
97
+ grpc_closure on_peer_checked_;
98
+ RefCountedPtr<grpc_auth_context> auth_context_;
99
+ tsi_handshaker_result* handshaker_result_ = nullptr;
96
100
  };
97
- } // namespace
98
101
 
99
- static size_t move_read_buffer_into_handshake_buffer(security_handshaker* h) {
100
- size_t bytes_in_read_buffer = h->args->read_buffer->length;
101
- if (h->handshake_buffer_size < bytes_in_read_buffer) {
102
- h->handshake_buffer = static_cast<uint8_t*>(
103
- gpr_realloc(h->handshake_buffer, bytes_in_read_buffer));
104
- h->handshake_buffer_size = bytes_in_read_buffer;
102
+ SecurityHandshaker::SecurityHandshaker(tsi_handshaker* handshaker,
103
+ grpc_security_connector* connector)
104
+ : handshaker_(handshaker),
105
+ connector_(connector->Ref(DEBUG_LOCATION, "handshake")),
106
+ handshake_buffer_size_(GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE),
107
+ handshake_buffer_(
108
+ static_cast<uint8_t*>(gpr_malloc(handshake_buffer_size_))) {
109
+ gpr_mu_init(&mu_);
110
+ grpc_slice_buffer_init(&outgoing_);
111
+ GRPC_CLOSURE_INIT(&on_handshake_data_sent_to_peer_,
112
+ &SecurityHandshaker::OnHandshakeDataSentToPeerFn, this,
113
+ grpc_schedule_on_exec_ctx);
114
+ GRPC_CLOSURE_INIT(&on_handshake_data_received_from_peer_,
115
+ &SecurityHandshaker::OnHandshakeDataReceivedFromPeerFn,
116
+ this, grpc_schedule_on_exec_ctx);
117
+ GRPC_CLOSURE_INIT(&on_peer_checked_, &SecurityHandshaker::OnPeerCheckedFn,
118
+ this, grpc_schedule_on_exec_ctx);
119
+ }
120
+
121
+ SecurityHandshaker::~SecurityHandshaker() {
122
+ gpr_mu_destroy(&mu_);
123
+ tsi_handshaker_destroy(handshaker_);
124
+ tsi_handshaker_result_destroy(handshaker_result_);
125
+ if (endpoint_to_destroy_ != nullptr) {
126
+ grpc_endpoint_destroy(endpoint_to_destroy_);
127
+ }
128
+ if (read_buffer_to_destroy_ != nullptr) {
129
+ grpc_slice_buffer_destroy_internal(read_buffer_to_destroy_);
130
+ gpr_free(read_buffer_to_destroy_);
131
+ }
132
+ gpr_free(handshake_buffer_);
133
+ grpc_slice_buffer_destroy_internal(&outgoing_);
134
+ auth_context_.reset(DEBUG_LOCATION, "handshake");
135
+ connector_.reset(DEBUG_LOCATION, "handshake");
136
+ }
137
+
138
+ size_t SecurityHandshaker::MoveReadBufferIntoHandshakeBuffer() {
139
+ size_t bytes_in_read_buffer = args_->read_buffer->length;
140
+ if (handshake_buffer_size_ < bytes_in_read_buffer) {
141
+ handshake_buffer_ = static_cast<uint8_t*>(
142
+ gpr_realloc(handshake_buffer_, bytes_in_read_buffer));
143
+ handshake_buffer_size_ = bytes_in_read_buffer;
105
144
  }
106
145
  size_t offset = 0;
107
- while (h->args->read_buffer->count > 0) {
108
- grpc_slice next_slice = grpc_slice_buffer_take_first(h->args->read_buffer);
109
- memcpy(h->handshake_buffer + offset, GRPC_SLICE_START_PTR(next_slice),
146
+ while (args_->read_buffer->count > 0) {
147
+ grpc_slice next_slice = grpc_slice_buffer_take_first(args_->read_buffer);
148
+ memcpy(handshake_buffer_ + offset, GRPC_SLICE_START_PTR(next_slice),
110
149
  GRPC_SLICE_LENGTH(next_slice));
111
150
  offset += GRPC_SLICE_LENGTH(next_slice);
112
151
  grpc_slice_unref_internal(next_slice);
@@ -114,21 +153,20 @@ static size_t move_read_buffer_into_handshake_buffer(security_handshaker* h) {
114
153
  return bytes_in_read_buffer;
115
154
  }
116
155
 
117
- // Set args fields to NULL, saving the endpoint and read buffer for
156
+ // Set args_ fields to NULL, saving the endpoint and read buffer for
118
157
  // later destruction.
119
- static void cleanup_args_for_failure_locked(security_handshaker* h) {
120
- h->endpoint_to_destroy = h->args->endpoint;
121
- h->args->endpoint = nullptr;
122
- h->read_buffer_to_destroy = h->args->read_buffer;
123
- h->args->read_buffer = nullptr;
124
- grpc_channel_args_destroy(h->args->args);
125
- h->args->args = nullptr;
158
+ void SecurityHandshaker::CleanupArgsForFailureLocked() {
159
+ endpoint_to_destroy_ = args_->endpoint;
160
+ args_->endpoint = nullptr;
161
+ read_buffer_to_destroy_ = args_->read_buffer;
162
+ args_->read_buffer = nullptr;
163
+ grpc_channel_args_destroy(args_->args);
164
+ args_->args = nullptr;
126
165
  }
127
166
 
128
167
  // If the handshake failed or we're shutting down, clean up and invoke the
129
168
  // callback with the error.
130
- static void security_handshake_failed_locked(security_handshaker* h,
131
- grpc_error* error) {
169
+ void SecurityHandshaker::HandshakeFailedLocked(grpc_error* error) {
132
170
  if (error == GRPC_ERROR_NONE) {
133
171
  // If we were shut down after the handshake succeeded but before an
134
172
  // endpoint callback was invoked, we need to generate our own error.
@@ -137,50 +175,51 @@ static void security_handshake_failed_locked(security_handshaker* h,
137
175
  const char* msg = grpc_error_string(error);
138
176
  gpr_log(GPR_DEBUG, "Security handshake failed: %s", msg);
139
177
 
140
- if (!h->shutdown) {
178
+ if (!is_shutdown_) {
141
179
  // TODO(ctiller): It is currently necessary to shutdown endpoints
142
180
  // before destroying them, even if we know that there are no
143
181
  // pending read/write callbacks. This should be fixed, at which
144
182
  // point this can be removed.
145
- grpc_endpoint_shutdown(h->args->endpoint, GRPC_ERROR_REF(error));
183
+ grpc_endpoint_shutdown(args_->endpoint, GRPC_ERROR_REF(error));
146
184
  // Not shutting down, so the write failed. Clean up before
147
185
  // invoking the callback.
148
- cleanup_args_for_failure_locked(h);
186
+ CleanupArgsForFailureLocked();
149
187
  // Set shutdown to true so that subsequent calls to
150
188
  // security_handshaker_shutdown() do nothing.
151
- h->shutdown = true;
189
+ is_shutdown_ = true;
152
190
  }
153
191
  // Invoke callback.
154
- GRPC_CLOSURE_SCHED(h->on_handshake_done, error);
192
+ GRPC_CLOSURE_SCHED(on_handshake_done_, error);
155
193
  }
156
194
 
157
- static void on_peer_checked_inner(security_handshaker* h, grpc_error* error) {
158
- if (error != GRPC_ERROR_NONE || h->shutdown) {
159
- security_handshake_failed_locked(h, GRPC_ERROR_REF(error));
195
+ void SecurityHandshaker::OnPeerCheckedInner(grpc_error* error) {
196
+ MutexLock lock(&mu_);
197
+ if (error != GRPC_ERROR_NONE || is_shutdown_) {
198
+ HandshakeFailedLocked(GRPC_ERROR_REF(error));
160
199
  return;
161
200
  }
162
201
  // Create zero-copy frame protector, if implemented.
163
202
  tsi_zero_copy_grpc_protector* zero_copy_protector = nullptr;
164
203
  tsi_result result = tsi_handshaker_result_create_zero_copy_grpc_protector(
165
- h->handshaker_result, nullptr, &zero_copy_protector);
204
+ handshaker_result_, nullptr, &zero_copy_protector);
166
205
  if (result != TSI_OK && result != TSI_UNIMPLEMENTED) {
167
206
  error = grpc_set_tsi_error_result(
168
207
  GRPC_ERROR_CREATE_FROM_STATIC_STRING(
169
208
  "Zero-copy frame protector creation failed"),
170
209
  result);
171
- security_handshake_failed_locked(h, error);
210
+ HandshakeFailedLocked(error);
172
211
  return;
173
212
  }
174
213
  // Create frame protector if zero-copy frame protector is NULL.
175
214
  tsi_frame_protector* protector = nullptr;
176
215
  if (zero_copy_protector == nullptr) {
177
- result = tsi_handshaker_result_create_frame_protector(h->handshaker_result,
216
+ result = tsi_handshaker_result_create_frame_protector(handshaker_result_,
178
217
  nullptr, &protector);
179
218
  if (result != TSI_OK) {
180
219
  error = grpc_set_tsi_error_result(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
181
220
  "Frame protector creation failed"),
182
221
  result);
183
- security_handshake_failed_locked(h, error);
222
+ HandshakeFailedLocked(error);
184
223
  return;
185
224
  }
186
225
  }
@@ -188,68 +227,63 @@ static void on_peer_checked_inner(security_handshaker* h, grpc_error* error) {
188
227
  const unsigned char* unused_bytes = nullptr;
189
228
  size_t unused_bytes_size = 0;
190
229
  result = tsi_handshaker_result_get_unused_bytes(
191
- h->handshaker_result, &unused_bytes, &unused_bytes_size);
230
+ handshaker_result_, &unused_bytes, &unused_bytes_size);
192
231
  // Create secure endpoint.
193
232
  if (unused_bytes_size > 0) {
194
233
  grpc_slice slice =
195
234
  grpc_slice_from_copied_buffer((char*)unused_bytes, unused_bytes_size);
196
- h->args->endpoint = grpc_secure_endpoint_create(
197
- protector, zero_copy_protector, h->args->endpoint, &slice, 1);
235
+ args_->endpoint = grpc_secure_endpoint_create(
236
+ protector, zero_copy_protector, args_->endpoint, &slice, 1);
198
237
  grpc_slice_unref_internal(slice);
199
238
  } else {
200
- h->args->endpoint = grpc_secure_endpoint_create(
201
- protector, zero_copy_protector, h->args->endpoint, nullptr, 0);
239
+ args_->endpoint = grpc_secure_endpoint_create(
240
+ protector, zero_copy_protector, args_->endpoint, nullptr, 0);
202
241
  }
203
- tsi_handshaker_result_destroy(h->handshaker_result);
204
- h->handshaker_result = nullptr;
242
+ tsi_handshaker_result_destroy(handshaker_result_);
243
+ handshaker_result_ = nullptr;
205
244
  // Add auth context to channel args.
206
- grpc_arg auth_context_arg = grpc_auth_context_to_arg(h->auth_context.get());
207
- grpc_channel_args* tmp_args = h->args->args;
208
- h->args->args =
209
- grpc_channel_args_copy_and_add(tmp_args, &auth_context_arg, 1);
245
+ grpc_arg auth_context_arg = grpc_auth_context_to_arg(auth_context_.get());
246
+ grpc_channel_args* tmp_args = args_->args;
247
+ args_->args = grpc_channel_args_copy_and_add(tmp_args, &auth_context_arg, 1);
210
248
  grpc_channel_args_destroy(tmp_args);
211
249
  // Invoke callback.
212
- GRPC_CLOSURE_SCHED(h->on_handshake_done, GRPC_ERROR_NONE);
250
+ GRPC_CLOSURE_SCHED(on_handshake_done_, GRPC_ERROR_NONE);
213
251
  // Set shutdown to true so that subsequent calls to
214
252
  // security_handshaker_shutdown() do nothing.
215
- h->shutdown = true;
253
+ is_shutdown_ = true;
216
254
  }
217
255
 
218
- static void on_peer_checked(void* arg, grpc_error* error) {
219
- security_handshaker* h = static_cast<security_handshaker*>(arg);
220
- gpr_mu_lock(&h->mu);
221
- on_peer_checked_inner(h, error);
222
- gpr_mu_unlock(&h->mu);
223
- h->Unref();
256
+ void SecurityHandshaker::OnPeerCheckedFn(void* arg, grpc_error* error) {
257
+ RefCountedPtr<SecurityHandshaker>(static_cast<SecurityHandshaker*>(arg))
258
+ ->OnPeerCheckedInner(error);
224
259
  }
225
260
 
226
- static grpc_error* check_peer_locked(security_handshaker* h) {
261
+ grpc_error* SecurityHandshaker::CheckPeerLocked() {
227
262
  tsi_peer peer;
228
263
  tsi_result result =
229
- tsi_handshaker_result_extract_peer(h->handshaker_result, &peer);
264
+ tsi_handshaker_result_extract_peer(handshaker_result_, &peer);
230
265
  if (result != TSI_OK) {
231
266
  return grpc_set_tsi_error_result(
232
267
  GRPC_ERROR_CREATE_FROM_STATIC_STRING("Peer extraction failed"), result);
233
268
  }
234
- h->connector->check_peer(peer, h->args->endpoint, &h->auth_context,
235
- &h->on_peer_checked);
269
+ connector_->check_peer(peer, args_->endpoint, &auth_context_,
270
+ &on_peer_checked_);
236
271
  return GRPC_ERROR_NONE;
237
272
  }
238
273
 
239
- static grpc_error* on_handshake_next_done_locked(
240
- security_handshaker* h, tsi_result result,
241
- const unsigned char* bytes_to_send, size_t bytes_to_send_size,
242
- tsi_handshaker_result* handshaker_result) {
274
+ grpc_error* SecurityHandshaker::OnHandshakeNextDoneLocked(
275
+ tsi_result result, const unsigned char* bytes_to_send,
276
+ size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result) {
243
277
  grpc_error* error = GRPC_ERROR_NONE;
244
278
  // Handshaker was shutdown.
245
- if (h->shutdown) {
279
+ if (is_shutdown_) {
246
280
  return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Handshaker shutdown");
247
281
  }
248
282
  // Read more if we need to.
249
283
  if (result == TSI_INCOMPLETE_DATA) {
250
284
  GPR_ASSERT(bytes_to_send_size == 0);
251
- grpc_endpoint_read(h->args->endpoint, h->args->read_buffer,
252
- &h->on_handshake_data_received_from_peer);
285
+ grpc_endpoint_read(args_->endpoint, args_->read_buffer,
286
+ &on_handshake_data_received_from_peer_);
253
287
  return error;
254
288
  }
255
289
  if (result != TSI_OK) {
@@ -258,55 +292,52 @@ static grpc_error* on_handshake_next_done_locked(
258
292
  }
259
293
  // Update handshaker result.
260
294
  if (handshaker_result != nullptr) {
261
- GPR_ASSERT(h->handshaker_result == nullptr);
262
- h->handshaker_result = handshaker_result;
295
+ GPR_ASSERT(handshaker_result_ == nullptr);
296
+ handshaker_result_ = handshaker_result;
263
297
  }
264
298
  if (bytes_to_send_size > 0) {
265
299
  // Send data to peer, if needed.
266
300
  grpc_slice to_send = grpc_slice_from_copied_buffer(
267
301
  reinterpret_cast<const char*>(bytes_to_send), bytes_to_send_size);
268
- grpc_slice_buffer_reset_and_unref_internal(&h->outgoing);
269
- grpc_slice_buffer_add(&h->outgoing, to_send);
270
- grpc_endpoint_write(h->args->endpoint, &h->outgoing,
271
- &h->on_handshake_data_sent_to_peer, nullptr);
302
+ grpc_slice_buffer_reset_and_unref_internal(&outgoing_);
303
+ grpc_slice_buffer_add(&outgoing_, to_send);
304
+ grpc_endpoint_write(args_->endpoint, &outgoing_,
305
+ &on_handshake_data_sent_to_peer_, nullptr);
272
306
  } else if (handshaker_result == nullptr) {
273
307
  // There is nothing to send, but need to read from peer.
274
- grpc_endpoint_read(h->args->endpoint, h->args->read_buffer,
275
- &h->on_handshake_data_received_from_peer);
308
+ grpc_endpoint_read(args_->endpoint, args_->read_buffer,
309
+ &on_handshake_data_received_from_peer_);
276
310
  } else {
277
311
  // Handshake has finished, check peer and so on.
278
- error = check_peer_locked(h);
312
+ error = CheckPeerLocked();
279
313
  }
280
314
  return error;
281
315
  }
282
316
 
283
- static void on_handshake_next_done_grpc_wrapper(
317
+ void SecurityHandshaker::OnHandshakeNextDoneGrpcWrapper(
284
318
  tsi_result result, void* user_data, const unsigned char* bytes_to_send,
285
319
  size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result) {
286
- security_handshaker* h = static_cast<security_handshaker*>(user_data);
287
- gpr_mu_lock(&h->mu);
288
- grpc_error* error = on_handshake_next_done_locked(
289
- h, result, bytes_to_send, bytes_to_send_size, handshaker_result);
320
+ RefCountedPtr<SecurityHandshaker> h(
321
+ static_cast<SecurityHandshaker*>(user_data));
322
+ MutexLock lock(&h->mu_);
323
+ grpc_error* error = h->OnHandshakeNextDoneLocked(
324
+ result, bytes_to_send, bytes_to_send_size, handshaker_result);
290
325
  if (error != GRPC_ERROR_NONE) {
291
- security_handshake_failed_locked(h, error);
292
- gpr_mu_unlock(&h->mu);
293
- h->Unref();
326
+ h->HandshakeFailedLocked(error);
294
327
  } else {
295
- gpr_mu_unlock(&h->mu);
328
+ h.release(); // Avoid unref
296
329
  }
297
330
  }
298
331
 
299
- static grpc_error* do_handshaker_next_locked(
300
- security_handshaker* h, const unsigned char* bytes_received,
301
- size_t bytes_received_size) {
332
+ grpc_error* SecurityHandshaker::DoHandshakerNextLocked(
333
+ const unsigned char* bytes_received, size_t bytes_received_size) {
302
334
  // Invoke TSI handshaker.
303
335
  const unsigned char* bytes_to_send = nullptr;
304
336
  size_t bytes_to_send_size = 0;
305
- tsi_handshaker_result* handshaker_result = nullptr;
337
+ tsi_handshaker_result* hs_result = nullptr;
306
338
  tsi_result result = tsi_handshaker_next(
307
- h->handshaker, bytes_received, bytes_received_size, &bytes_to_send,
308
- &bytes_to_send_size, &handshaker_result,
309
- &on_handshake_next_done_grpc_wrapper, h);
339
+ handshaker_, bytes_received, bytes_received_size, &bytes_to_send,
340
+ &bytes_to_send_size, &hs_result, &OnHandshakeNextDoneGrpcWrapper, this);
310
341
  if (result == TSI_ASYNC) {
311
342
  // Handshaker operating asynchronously. Nothing else to do here;
312
343
  // callback will be invoked in a TSI thread.
@@ -314,233 +345,169 @@ static grpc_error* do_handshaker_next_locked(
314
345
  }
315
346
  // Handshaker returned synchronously. Invoke callback directly in
316
347
  // this thread with our existing exec_ctx.
317
- return on_handshake_next_done_locked(h, result, bytes_to_send,
318
- bytes_to_send_size, handshaker_result);
348
+ return OnHandshakeNextDoneLocked(result, bytes_to_send, bytes_to_send_size,
349
+ hs_result);
319
350
  }
320
351
 
321
- static void on_handshake_data_received_from_peer(void* arg, grpc_error* error) {
322
- security_handshaker* h = static_cast<security_handshaker*>(arg);
323
- gpr_mu_lock(&h->mu);
324
- if (error != GRPC_ERROR_NONE || h->shutdown) {
325
- security_handshake_failed_locked(
326
- h, GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
327
- "Handshake read failed", &error, 1));
328
- gpr_mu_unlock(&h->mu);
329
- h->Unref();
352
+ void SecurityHandshaker::OnHandshakeDataReceivedFromPeerFn(void* arg,
353
+ grpc_error* error) {
354
+ RefCountedPtr<SecurityHandshaker> h(static_cast<SecurityHandshaker*>(arg));
355
+ MutexLock lock(&h->mu_);
356
+ if (error != GRPC_ERROR_NONE || h->is_shutdown_) {
357
+ h->HandshakeFailedLocked(GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
358
+ "Handshake read failed", &error, 1));
330
359
  return;
331
360
  }
332
361
  // Copy all slices received.
333
- size_t bytes_received_size = move_read_buffer_into_handshake_buffer(h);
362
+ size_t bytes_received_size = h->MoveReadBufferIntoHandshakeBuffer();
334
363
  // Call TSI handshaker.
335
- error =
336
- do_handshaker_next_locked(h, h->handshake_buffer, bytes_received_size);
364
+ error = h->DoHandshakerNextLocked(h->handshake_buffer_, bytes_received_size);
337
365
 
338
366
  if (error != GRPC_ERROR_NONE) {
339
- security_handshake_failed_locked(h, error);
340
- gpr_mu_unlock(&h->mu);
341
- h->Unref();
367
+ h->HandshakeFailedLocked(error);
342
368
  } else {
343
- gpr_mu_unlock(&h->mu);
369
+ h.release(); // Avoid unref
344
370
  }
345
371
  }
346
372
 
347
- static void on_handshake_data_sent_to_peer(void* arg, grpc_error* error) {
348
- security_handshaker* h = static_cast<security_handshaker*>(arg);
349
- gpr_mu_lock(&h->mu);
350
- if (error != GRPC_ERROR_NONE || h->shutdown) {
351
- security_handshake_failed_locked(
352
- h, GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
353
- "Handshake write failed", &error, 1));
354
- gpr_mu_unlock(&h->mu);
355
- h->Unref();
373
+ void SecurityHandshaker::OnHandshakeDataSentToPeerFn(void* arg,
374
+ grpc_error* error) {
375
+ RefCountedPtr<SecurityHandshaker> h(static_cast<SecurityHandshaker*>(arg));
376
+ MutexLock lock(&h->mu_);
377
+ if (error != GRPC_ERROR_NONE || h->is_shutdown_) {
378
+ h->HandshakeFailedLocked(GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
379
+ "Handshake write failed", &error, 1));
356
380
  return;
357
381
  }
358
382
  // We may be done.
359
- if (h->handshaker_result == nullptr) {
360
- grpc_endpoint_read(h->args->endpoint, h->args->read_buffer,
361
- &h->on_handshake_data_received_from_peer);
383
+ if (h->handshaker_result_ == nullptr) {
384
+ grpc_endpoint_read(h->args_->endpoint, h->args_->read_buffer,
385
+ &h->on_handshake_data_received_from_peer_);
362
386
  } else {
363
- error = check_peer_locked(h);
387
+ error = h->CheckPeerLocked();
364
388
  if (error != GRPC_ERROR_NONE) {
365
- security_handshake_failed_locked(h, error);
366
- gpr_mu_unlock(&h->mu);
367
- h->Unref();
389
+ h->HandshakeFailedLocked(error);
368
390
  return;
369
391
  }
370
392
  }
371
- gpr_mu_unlock(&h->mu);
393
+ h.release(); // Avoid unref
372
394
  }
373
395
 
374
396
  //
375
397
  // public handshaker API
376
398
  //
377
399
 
378
- static void security_handshaker_destroy(grpc_handshaker* handshaker) {
379
- security_handshaker* h = reinterpret_cast<security_handshaker*>(handshaker);
380
- h->Unref();
381
- }
382
-
383
- static void security_handshaker_shutdown(grpc_handshaker* handshaker,
384
- grpc_error* why) {
385
- security_handshaker* h = reinterpret_cast<security_handshaker*>(handshaker);
386
- gpr_mu_lock(&h->mu);
387
- if (!h->shutdown) {
388
- h->shutdown = true;
389
- tsi_handshaker_shutdown(h->handshaker);
390
- grpc_endpoint_shutdown(h->args->endpoint, GRPC_ERROR_REF(why));
391
- cleanup_args_for_failure_locked(h);
400
+ void SecurityHandshaker::Shutdown(grpc_error* why) {
401
+ MutexLock lock(&mu_);
402
+ if (!is_shutdown_) {
403
+ is_shutdown_ = true;
404
+ tsi_handshaker_shutdown(handshaker_);
405
+ grpc_endpoint_shutdown(args_->endpoint, GRPC_ERROR_REF(why));
406
+ CleanupArgsForFailureLocked();
392
407
  }
393
- gpr_mu_unlock(&h->mu);
394
408
  GRPC_ERROR_UNREF(why);
395
409
  }
396
410
 
397
- static void security_handshaker_do_handshake(grpc_handshaker* handshaker,
398
- grpc_tcp_server_acceptor* acceptor,
399
- grpc_closure* on_handshake_done,
400
- grpc_handshaker_args* args) {
401
- security_handshaker* h = reinterpret_cast<security_handshaker*>(handshaker);
402
- gpr_mu_lock(&h->mu);
403
- h->args = args;
404
- h->on_handshake_done = on_handshake_done;
405
- h->Ref();
406
- size_t bytes_received_size = move_read_buffer_into_handshake_buffer(h);
411
+ void SecurityHandshaker::DoHandshake(grpc_tcp_server_acceptor* acceptor,
412
+ grpc_closure* on_handshake_done,
413
+ HandshakerArgs* args) {
414
+ auto ref = Ref();
415
+ MutexLock lock(&mu_);
416
+ args_ = args;
417
+ on_handshake_done_ = on_handshake_done;
418
+ size_t bytes_received_size = MoveReadBufferIntoHandshakeBuffer();
407
419
  grpc_error* error =
408
- do_handshaker_next_locked(h, h->handshake_buffer, bytes_received_size);
420
+ DoHandshakerNextLocked(handshake_buffer_, bytes_received_size);
409
421
  if (error != GRPC_ERROR_NONE) {
410
- security_handshake_failed_locked(h, error);
411
- gpr_mu_unlock(&h->mu);
412
- h->Unref();
413
- return;
422
+ HandshakeFailedLocked(error);
423
+ } else {
424
+ ref.release(); // Avoid unref
414
425
  }
415
- gpr_mu_unlock(&h->mu);
416
- }
417
-
418
- static const grpc_handshaker_vtable security_handshaker_vtable = {
419
- security_handshaker_destroy, security_handshaker_shutdown,
420
- security_handshaker_do_handshake, "security"};
421
-
422
- namespace {
423
- security_handshaker::security_handshaker(tsi_handshaker* handshaker,
424
- grpc_security_connector* connector)
425
- : handshaker(handshaker),
426
- connector(connector->Ref(DEBUG_LOCATION, "handshake")),
427
- handshake_buffer_size(GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE),
428
- handshake_buffer(
429
- static_cast<uint8_t*>(gpr_malloc(handshake_buffer_size))) {
430
- grpc_handshaker_init(&security_handshaker_vtable, &base);
431
- gpr_mu_init(&mu);
432
- grpc_slice_buffer_init(&outgoing);
433
- GRPC_CLOSURE_INIT(&on_handshake_data_sent_to_peer,
434
- ::on_handshake_data_sent_to_peer, this,
435
- grpc_schedule_on_exec_ctx);
436
- GRPC_CLOSURE_INIT(&on_handshake_data_received_from_peer,
437
- ::on_handshake_data_received_from_peer, this,
438
- grpc_schedule_on_exec_ctx);
439
- GRPC_CLOSURE_INIT(&on_peer_checked, ::on_peer_checked, this,
440
- grpc_schedule_on_exec_ctx);
441
- }
442
- } // namespace
443
-
444
- static grpc_handshaker* security_handshaker_create(
445
- tsi_handshaker* handshaker, grpc_security_connector* connector) {
446
- security_handshaker* h =
447
- grpc_core::New<security_handshaker>(handshaker, connector);
448
- return &h->base;
449
426
  }
450
427
 
451
428
  //
452
- // fail_handshaker
429
+ // FailHandshaker
453
430
  //
454
431
 
455
- static void fail_handshaker_destroy(grpc_handshaker* handshaker) {
456
- gpr_free(handshaker);
457
- }
458
-
459
- static void fail_handshaker_shutdown(grpc_handshaker* handshaker,
460
- grpc_error* why) {
461
- GRPC_ERROR_UNREF(why);
462
- }
463
-
464
- static void fail_handshaker_do_handshake(grpc_handshaker* handshaker,
465
- grpc_tcp_server_acceptor* acceptor,
466
- grpc_closure* on_handshake_done,
467
- grpc_handshaker_args* args) {
468
- GRPC_CLOSURE_SCHED(on_handshake_done,
469
- GRPC_ERROR_CREATE_FROM_STATIC_STRING(
470
- "Failed to create security handshaker"));
471
- }
472
-
473
- static const grpc_handshaker_vtable fail_handshaker_vtable = {
474
- fail_handshaker_destroy, fail_handshaker_shutdown,
475
- fail_handshaker_do_handshake, "security_fail"};
432
+ class FailHandshaker : public Handshaker {
433
+ public:
434
+ const char* name() const override { return "security_fail"; }
435
+ void Shutdown(grpc_error* why) override { GRPC_ERROR_UNREF(why); }
436
+ void DoHandshake(grpc_tcp_server_acceptor* acceptor,
437
+ grpc_closure* on_handshake_done,
438
+ HandshakerArgs* args) override {
439
+ GRPC_CLOSURE_SCHED(on_handshake_done,
440
+ GRPC_ERROR_CREATE_FROM_STATIC_STRING(
441
+ "Failed to create security handshaker"));
442
+ }
476
443
 
477
- static grpc_handshaker* fail_handshaker_create() {
478
- grpc_handshaker* h = static_cast<grpc_handshaker*>(gpr_malloc(sizeof(*h)));
479
- grpc_handshaker_init(&fail_handshaker_vtable, h);
480
- return h;
481
- }
444
+ private:
445
+ virtual ~FailHandshaker() = default;
446
+ };
482
447
 
483
448
  //
484
449
  // handshaker factories
485
450
  //
486
451
 
487
- static void client_handshaker_factory_add_handshakers(
488
- grpc_handshaker_factory* handshaker_factory, const grpc_channel_args* args,
489
- grpc_pollset_set* interested_parties,
490
- grpc_handshake_manager* handshake_mgr) {
491
- grpc_channel_security_connector* security_connector =
492
- reinterpret_cast<grpc_channel_security_connector*>(
493
- grpc_security_connector_find_in_args(args));
494
- if (security_connector) {
495
- security_connector->add_handshakers(interested_parties, handshake_mgr);
452
+ class ClientSecurityHandshakerFactory : public HandshakerFactory {
453
+ public:
454
+ void AddHandshakers(const grpc_channel_args* args,
455
+ grpc_pollset_set* interested_parties,
456
+ HandshakeManager* handshake_mgr) override {
457
+ auto* security_connector =
458
+ reinterpret_cast<grpc_channel_security_connector*>(
459
+ grpc_security_connector_find_in_args(args));
460
+ if (security_connector) {
461
+ security_connector->add_handshakers(interested_parties, handshake_mgr);
462
+ }
496
463
  }
497
- }
464
+ ~ClientSecurityHandshakerFactory() override = default;
465
+ };
498
466
 
499
- static void server_handshaker_factory_add_handshakers(
500
- grpc_handshaker_factory* hf, const grpc_channel_args* args,
501
- grpc_pollset_set* interested_parties,
502
- grpc_handshake_manager* handshake_mgr) {
503
- grpc_server_security_connector* security_connector =
504
- reinterpret_cast<grpc_server_security_connector*>(
505
- grpc_security_connector_find_in_args(args));
506
- if (security_connector) {
507
- security_connector->add_handshakers(interested_parties, handshake_mgr);
467
+ class ServerSecurityHandshakerFactory : public HandshakerFactory {
468
+ public:
469
+ void AddHandshakers(const grpc_channel_args* args,
470
+ grpc_pollset_set* interested_parties,
471
+ HandshakeManager* handshake_mgr) override {
472
+ auto* security_connector =
473
+ reinterpret_cast<grpc_server_security_connector*>(
474
+ grpc_security_connector_find_in_args(args));
475
+ if (security_connector) {
476
+ security_connector->add_handshakers(interested_parties, handshake_mgr);
477
+ }
508
478
  }
509
- }
510
-
511
- static void handshaker_factory_destroy(
512
- grpc_handshaker_factory* handshaker_factory) {}
513
-
514
- static const grpc_handshaker_factory_vtable client_handshaker_factory_vtable = {
515
- client_handshaker_factory_add_handshakers, handshaker_factory_destroy};
516
-
517
- static grpc_handshaker_factory client_handshaker_factory = {
518
- &client_handshaker_factory_vtable};
519
-
520
- static const grpc_handshaker_factory_vtable server_handshaker_factory_vtable = {
521
- server_handshaker_factory_add_handshakers, handshaker_factory_destroy};
479
+ ~ServerSecurityHandshakerFactory() override = default;
480
+ };
522
481
 
523
- static grpc_handshaker_factory server_handshaker_factory = {
524
- &server_handshaker_factory_vtable};
482
+ } // namespace
525
483
 
526
484
  //
527
485
  // exported functions
528
486
  //
529
487
 
530
- grpc_handshaker* grpc_security_handshaker_create(
488
+ RefCountedPtr<Handshaker> SecurityHandshakerCreate(
531
489
  tsi_handshaker* handshaker, grpc_security_connector* connector) {
532
490
  // If no TSI handshaker was created, return a handshaker that always fails.
533
491
  // Otherwise, return a real security handshaker.
534
492
  if (handshaker == nullptr) {
535
- return fail_handshaker_create();
493
+ return MakeRefCounted<FailHandshaker>();
536
494
  } else {
537
- return security_handshaker_create(handshaker, connector);
495
+ return MakeRefCounted<SecurityHandshaker>(handshaker, connector);
538
496
  }
539
497
  }
540
498
 
541
- void grpc_security_register_handshaker_factories() {
542
- grpc_handshaker_factory_register(false /* at_start */, HANDSHAKER_CLIENT,
543
- &client_handshaker_factory);
544
- grpc_handshaker_factory_register(false /* at_start */, HANDSHAKER_SERVER,
545
- &server_handshaker_factory);
499
+ void SecurityRegisterHandshakerFactories() {
500
+ HandshakerRegistry::RegisterHandshakerFactory(
501
+ false /* at_start */, HANDSHAKER_CLIENT,
502
+ UniquePtr<HandshakerFactory>(New<ClientSecurityHandshakerFactory>()));
503
+ HandshakerRegistry::RegisterHandshakerFactory(
504
+ false /* at_start */, HANDSHAKER_SERVER,
505
+ UniquePtr<HandshakerFactory>(New<ServerSecurityHandshakerFactory>()));
506
+ }
507
+
508
+ } // namespace grpc_core
509
+
510
+ grpc_handshaker* grpc_security_handshaker_create(
511
+ tsi_handshaker* handshaker, grpc_security_connector* connector) {
512
+ return SecurityHandshakerCreate(handshaker, connector).release();
546
513
  }