tls_security_connector.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_TLS_TLS_SECURITY_CONNECTOR_H
20 #define GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_TLS_TLS_SECURITY_CONNECTOR_H
21 
23 
24 #include <map>
25 #include <string>
26 
27 #include "absl/base/thread_annotations.h"
28 #include "absl/status/status.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/types/optional.h"
31 
32 #include <grpc/grpc.h>
33 #include <grpc/grpc_security.h>
35 
50 
52 
53 namespace grpc_core {
54 
55 // Channel security connector using TLS as transport security protocol.
58  public:
59  // static factory method to create a TLS channel security connector.
65  const char* target_name, const char* overridden_target_name,
66  tsi_ssl_session_cache* ssl_session_cache);
67 
72  const char* target_name, const char* overridden_target_name,
73  tsi_ssl_session_cache* ssl_session_cache);
74 
76 
78  grpc_pollset_set* interested_parties,
79  HandshakeManager* handshake_mgr) override;
80 
81  void check_peer(tsi_peer peer, grpc_endpoint* ep,
83  grpc_closure* on_peer_checked) override;
84 
85  void cancel_check_peer(grpc_closure* on_peer_checked,
86  grpc_error_handle error) override;
87 
88  int cmp(const grpc_security_connector* other_sc) const override;
89 
91  absl::string_view host, grpc_auth_context* auth_context) override;
92 
94  MutexLock lock(&mu_);
95  return client_handshaker_factory_;
96  };
97 
99  MutexLock lock(&mu_);
100  return pem_root_certs_;
101  }
102 
104  MutexLock lock(&mu_);
105  return pem_key_cert_pair_list_;
106  }
107 
108  private:
109  // A watcher that watches certificate updates from
110  // grpc_tls_certificate_distributor. It will never outlive
111  // |security_connector_|.
113  TlsCertificatesWatcherInterface {
114  public:
116  TlsChannelSecurityConnector* security_connector)
117  : security_connector_(security_connector) {}
120  absl::optional<PemKeyCertPairList> key_cert_pairs) override;
121  void OnError(grpc_error_handle root_cert_error,
122  grpc_error_handle identity_cert_error) override;
123 
124  private:
126  };
127 
128  // Use "new" to create a new instance, and no need to delete it later, since
129  // it will be self-destroyed in |OnVerifyDone|.
131  public:
134  grpc_closure* on_peer_checked, tsi_peer peer, const char* target_name);
135 
137 
138  void Start();
139 
141 
142  private:
143  void OnVerifyDone(bool run_callback_inline, absl::Status status);
144  // The request will keep a reference of the security connector to make sure
145  // it won't be destroyed while the request is still ongoing.
149  };
150 
151  // Updates |client_handshaker_factory_| when the certificates that
152  // |certificate_watcher_| is watching get updated.
155 
157  // We need a separate mutex for |pending_verifier_requests_|, otherwise there
158  // would be deadlock errors.
161  grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface*
163  std::string target_name_;
165  tsi_ssl_client_handshaker_factory* client_handshaker_factory_
166  ABSL_GUARDED_BY(mu_) = nullptr;
167  tsi_ssl_session_cache* ssl_session_cache_ ABSL_GUARDED_BY(mu_) = nullptr;
169  absl::optional<absl::string_view> pem_root_certs_ ABSL_GUARDED_BY(mu_);
170  absl::optional<PemKeyCertPairList> pem_key_cert_pair_list_
172  std::map<grpc_closure* /*on_peer_checked*/, ChannelPendingVerifierRequest*>
173  pending_verifier_requests_ ABSL_GUARDED_BY(verifier_request_map_mu_);
174 };
175 
176 // Server security connector using TLS as transport security protocol.
178  public:
179  // static factory method to create a TLS server security connector.
181  CreateTlsServerSecurityConnector(
184 
188  ~TlsServerSecurityConnector() override;
189 
191  grpc_pollset_set* interested_parties,
192  HandshakeManager* handshake_mgr) override;
193 
194  void check_peer(tsi_peer peer, grpc_endpoint* ep,
195  RefCountedPtr<grpc_auth_context>* auth_context,
196  grpc_closure* on_peer_checked) override;
197 
198  void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
199  grpc_error_handle error) override;
200 
201  int cmp(const grpc_security_connector* other) const override;
202 
204  MutexLock lock(&mu_);
205  return server_handshaker_factory_;
206  };
207 
209  MutexLock lock(&mu_);
210  return pem_root_certs_;
211  }
212 
214  MutexLock lock(&mu_);
215  return pem_key_cert_pair_list_;
216  }
217 
218  private:
219  // A watcher that watches certificate updates from
220  // grpc_tls_certificate_distributor. It will never outlive
221  // |security_connector_|.
223  TlsCertificatesWatcherInterface {
224  public:
226  TlsServerSecurityConnector* security_connector)
227  : security_connector_(security_connector) {}
228  void OnCertificatesChanged(
230  absl::optional<PemKeyCertPairList> key_cert_pairs) override;
231 
232  void OnError(grpc_error_handle root_cert_error,
233  grpc_error_handle identity_cert_error) override;
234 
235  private:
236  TlsServerSecurityConnector* security_connector_ = nullptr;
237  };
238 
239  // Use "new" to create a new instance, and no need to delete it later, since
240  // it will be self-destroyed in |OnVerifyDone|.
242  public:
245  grpc_closure* on_peer_checked, tsi_peer peer);
246 
248 
249  void Start();
250 
252 
253  private:
254  void OnVerifyDone(bool run_callback_inline, absl::Status status);
255  // The request will keep a reference of the security connector to make sure
256  // it won't be destroyed while the request is still ongoing.
260  };
261 
262  // Updates |server_handshaker_factory_| when the certificates that
263  // |certificate_watcher_| is watching get updated.
266 
268  // We need a separate mutex for |pending_verifier_requests_|, otherwise there
269  // would be deadlock errors.
272  grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface*
274  tsi_ssl_server_handshaker_factory* server_handshaker_factory_
275  ABSL_GUARDED_BY(mu_) = nullptr;
276  absl::optional<absl::string_view> pem_root_certs_ ABSL_GUARDED_BY(mu_);
277  absl::optional<PemKeyCertPairList> pem_key_cert_pair_list_
280  std::map<grpc_closure* /*on_peer_checked*/, ServerPendingVerifierRequest*>
281  pending_verifier_requests_ ABSL_GUARDED_BY(verifier_request_map_mu_);
282 };
283 
284 } // namespace grpc_core
285 
286 #endif // GRPC_CORE_LIB_SECURITY_SECURITY_CONNECTOR_TLS_TLS_SECURITY_CONNECTOR_H
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::request
grpc_tls_custom_verification_check_request * request()
Definition: tls_security_connector.h:140
grpc_auth_context
Definition: security_context.h:63
grpc_core::TlsChannelSecurityConnector::cancel_check_peer
void cancel_check_peer(grpc_closure *on_peer_checked, grpc_error_handle error) override
Definition: tls_security_connector.cc:379
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::security_connector_
RefCountedPtr< TlsChannelSecurityConnector > security_connector_
Definition: tls_security_connector.h:146
grpc_tls_certificate_distributor.h
grpc_core::TlsChannelSecurityConnector::CreateTlsChannelSecurityConnector
static RefCountedPtr< grpc_channel_security_connector > CreateTlsChannelSecurityConnector(RefCountedPtr< grpc_channel_credentials > channel_creds, RefCountedPtr< grpc_tls_credentials_options > options, RefCountedPtr< grpc_call_credentials > request_metadata_creds, const char *target_name, const char *overridden_target_name, tsi_ssl_session_cache *ssl_session_cache)
Definition: tls_security_connector.cc:231
grpc_server_security_connector
Definition: security_connector.h:171
grpc_core::TlsChannelSecurityConnector::mu_
Mutex mu_
Definition: tls_security_connector.h:156
grpc_core::TlsChannelSecurityConnector::tls_session_key_logger_
RefCountedPtr< TlsSessionKeyLogger > tls_session_key_logger_
Definition: tls_security_connector.h:168
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
options
double_dict options[]
Definition: capstone_test.c:55
grpc_core::TlsChannelSecurityConnector::CheckCallHost
ArenaPromise< absl::Status > CheckCallHost(absl::string_view host, grpc_auth_context *auth_context) override
Definition: tls_security_connector.cc:421
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
tsi_ssl_client_handshaker_factory
Definition: ssl_transport_security.cc:93
grpc_core::TlsChannelSecurityConnector::~TlsChannelSecurityConnector
~TlsChannelSecurityConnector() override
Definition: tls_security_connector.cc:317
grpc_core::TlsChannelSecurityConnector
Definition: tls_security_connector.h:56
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::TlsChannelSecurityConnector::certificate_watcher_
grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface * certificate_watcher_
Definition: tls_security_connector.h:162
closure.h
grpc_core::TlsServerSecurityConnector::KeyCertPairListForTesting
const absl::optional< PemKeyCertPairList > & KeyCertPairListForTesting()
Definition: tls_security_connector.h:213
grpc_tls_certificate_distributor
Definition: grpc_tls_certificate_distributor.h:43
status
absl::Status status
Definition: rls.cc:251
grpc_core::TlsServerSecurityConnector
Definition: tls_security_connector.h:177
grpc_core::TlsChannelSecurityConnector::TlsChannelCertificateWatcher::security_connector_
TlsChannelSecurityConnector * security_connector_
Definition: tls_security_connector.h:125
grpc_core::TlsServerSecurityConnector::TlsServerCertificateWatcher
Definition: tls_security_connector.h:222
grpc_security.h
ssl_transport_security.h
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
grpc_core::TlsChannelSecurityConnector::TlsChannelCertificateWatcher::OnCertificatesChanged
void OnCertificatesChanged(absl::optional< absl::string_view > root_certs, absl::optional< PemKeyCertPairList > key_cert_pairs) override
Definition: tls_security_connector.cc:432
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::Start
void Start()
Definition: tls_security_connector.cc:488
grpc_types.h
grpc_core::TlsChannelSecurityConnector::TlsChannelSecurityConnector
TlsChannelSecurityConnector(RefCountedPtr< grpc_channel_credentials > channel_creds, RefCountedPtr< grpc_tls_credentials_options > options, RefCountedPtr< grpc_call_credentials > request_metadata_creds, const char *target_name, const char *overridden_target_name, tsi_ssl_session_cache *ssl_session_cache)
Definition: tls_security_connector.cc:261
ssl_key_logging.h
grpc_security_connector
Definition: security_connector.h:61
grpc_core::TlsServerSecurityConnector::ServerHandshakerFactoryForTesting
tsi_ssl_server_handshaker_factory * ServerHandshakerFactoryForTesting()
Definition: tls_security_connector.h:203
grpc_core::TlsChannelSecurityConnector::verifier_request_map_mu_
Mutex verifier_request_map_mu_
Definition: tls_security_connector.h:159
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::TlsServerSecurityConnector::TlsServerCertificateWatcher::TlsServerCertificateWatcher
TlsServerCertificateWatcher(TlsServerSecurityConnector *security_connector)
Definition: tls_security_connector.h:225
TlsSessionKeyLogger
tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger TlsSessionKeyLogger
Definition: tls_security_connector.h:51
grpc_core::RefCountedPtr< grpc_channel_security_connector >
grpc_core::TlsChannelSecurityConnector::TlsChannelCertificateWatcher
Definition: tls_security_connector.h:112
grpc_core::TlsChannelSecurityConnector::RootCertsForTesting
absl::optional< absl::string_view > RootCertsForTesting()
Definition: tls_security_connector.h:98
ABSL_EXCLUSIVE_LOCKS_REQUIRED
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:145
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::~ChannelPendingVerifierRequest
~ChannelPendingVerifierRequest()
Definition: tls_security_connector.cc:484
grpc.h
security_connector.h
grpc_core::TlsServerSecurityConnector::ServerPendingVerifierRequest::request_
grpc_tls_custom_verification_check_request request_
Definition: tls_security_connector.h:258
grpc_core::TlsChannelSecurityConnector::TlsChannelCertificateWatcher::TlsChannelCertificateWatcher
TlsChannelCertificateWatcher(TlsChannelSecurityConnector *security_connector)
Definition: tls_security_connector.h:115
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::OnVerifyDone
void OnVerifyDone(bool run_callback_inline, absl::Status status)
Definition: tls_security_connector.cc:502
absl::optional< absl::string_view >
gen_settings_ids.OnError
OnError
Definition: gen_settings_ids.py:27
arena_promise.h
grpc_core::TlsChannelSecurityConnector::options_
RefCountedPtr< grpc_tls_credentials_options > options_
Definition: tls_security_connector.h:160
error.h
grpc_core::TlsChannelSecurityConnector::ABSL_GUARDED_BY
tsi_ssl_client_handshaker_factory *client_handshaker_factory_ ABSL_GUARDED_BY(mu_)
transport_security_interface.h
request_
EchoRequest request_
Definition: client_callback_end2end_test.cc:724
tsi_ssl_server_handshaker_factory
Definition: ssl_transport_security.cc:102
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest
Definition: tls_security_connector.h:130
grpc_core::TlsServerSecurityConnector::ServerPendingVerifierRequest
Definition: tls_security_connector.h:241
grpc_core::TlsServerSecurityConnector::RootCertsForTesting
const absl::optional< absl::string_view > & RootCertsForTesting()
Definition: tls_security_connector.h:208
tsi_peer
Definition: transport_security_interface.h:238
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::request_
grpc_tls_custom_verification_check_request request_
Definition: tls_security_connector.h:147
grpc_core::TlsChannelSecurityConnector::UpdateHandshakerFactoryLocked
grpc_security_status UpdateHandshakerFactoryLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: tls_security_connector.cc:526
grpc_core::ArenaPromise
Definition: arena_promise.h:152
tsi_ssl_session_cache
struct tsi_ssl_session_cache tsi_ssl_session_cache
Definition: ssl_transport_security.h:68
grpc_core::TlsServerSecurityConnector::ServerPendingVerifierRequest::request
grpc_tls_custom_verification_check_request * request()
Definition: tls_security_connector.h:251
grpc_core::TlsChannelSecurityConnector::TlsChannelCertificateWatcher::OnError
void OnError(grpc_error_handle root_cert_error, grpc_error_handle identity_cert_error) override
Definition: tls_security_connector.cc:457
grpc_core::PemKeyCertPairList
std::vector< PemKeyCertPair > PemKeyCertPairList
Definition: ssl_utils.h:183
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_core::TlsServerSecurityConnector::ServerPendingVerifierRequest::security_connector_
RefCountedPtr< TlsServerSecurityConnector > security_connector_
Definition: tls_security_connector.h:257
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::ChannelPendingVerifierRequest
ChannelPendingVerifierRequest(RefCountedPtr< TlsChannelSecurityConnector > security_connector, grpc_closure *on_peer_checked, tsi_peer peer, const char *target_name)
Definition: tls_security_connector.cc:474
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_tls_credentials_options
Definition: grpc_tls_credentials_options.h:39
tsi::TlsSessionKeyLoggerCache::TlsSessionKeyLogger
Definition: ssl_key_logging.h:46
grpc_core::TlsChannelSecurityConnector::ClientHandshakerFactoryForTesting
tsi_ssl_client_handshaker_factory * ClientHandshakerFactoryForTesting()
Definition: tls_security_connector.h:93
handshaker.h
ref_counted_ptr.h
grpc_tls_custom_verification_check_request
Definition: grpc_security.h:907
grpc_core::TlsChannelSecurityConnector::overridden_target_name_
std::string overridden_target_name_
Definition: tls_security_connector.h:164
grpc_channel_security_connector
Definition: security_connector.h:118
grpc_core::TlsServerSecurityConnector::ServerPendingVerifierRequest::on_peer_checked_
grpc_closure * on_peer_checked_
Definition: tls_security_connector.h:259
grpc_core::TlsChannelSecurityConnector::cmp
int cmp(const grpc_security_connector *other_sc) const override
Definition: tls_security_connector.cc:409
grpc_core::TlsChannelSecurityConnector::ChannelPendingVerifierRequest::on_peer_checked_
grpc_closure * on_peer_checked_
Definition: tls_security_connector.h:148
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
grpc_core::HandshakeManager
Definition: handshaker.h:98
iomgr_fwd.h
grpc_core::TlsChannelSecurityConnector::check_peer
void check_peer(tsi_peer peer, grpc_endpoint *ep, RefCountedPtr< grpc_auth_context > *auth_context, grpc_closure *on_peer_checked) override
Definition: tls_security_connector.cc:354
endpoint.h
grpc_channel_security_connector::channel_creds
const grpc_channel_credentials * channel_creds() const
Definition: security_connector.h:135
grpc_error
Definition: error_internal.h:42
grpc_closure
Definition: closure.h:56
grpc_channel_security_connector::request_metadata_creds
const grpc_call_credentials * request_metadata_creds() const
Definition: security_connector.h:141
grpc_endpoint
Definition: endpoint.h:105
grpc_security_status
grpc_security_status
Definition: security_connector.h:52
grpc_core::TlsChannelSecurityConnector::target_name_
std::string target_name_
Definition: tls_security_connector.h:163
sync.h
ssl_utils.h
grpc_core::TlsChannelSecurityConnector::add_handshakers
void add_handshakers(const grpc_channel_args *args, grpc_pollset_set *interested_parties, HandshakeManager *handshake_mgr) override
Registers handshakers with handshake_mgr.
Definition: tls_security_connector.cc:332
grpc_core::TlsChannelSecurityConnector::KeyCertPairListForTesting
absl::optional< PemKeyCertPairList > KeyCertPairListForTesting()
Definition: tls_security_connector.h:103
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40