httpcli_security_connector.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 
20 
21 #include <string.h>
22 
23 #include "absl/status/status.h"
24 #include "absl/strings/str_cat.h"
25 #include "absl/strings/string_view.h"
26 
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_security.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
33 
53 
54 namespace grpc_core {
55 
56 namespace {
57 
58 class grpc_httpcli_ssl_channel_security_connector final
60  public:
61  explicit grpc_httpcli_ssl_channel_security_connector(char* secure_peer_name)
63  /*url_scheme=*/{},
64  /*channel_creds=*/nullptr,
65  /*request_metadata_creds=*/nullptr),
66  secure_peer_name_(secure_peer_name) {}
67 
68  ~grpc_httpcli_ssl_channel_security_connector() override {
69  if (handshaker_factory_ != nullptr) {
71  }
72  if (secure_peer_name_ != nullptr) {
74  }
75  }
76 
77  tsi_result InitHandshakerFactory(const char* pem_root_certs,
78  const tsi_ssl_root_certs_store* root_store) {
80  options.pem_root_certs = pem_root_certs;
81  options.root_store = root_store;
84  }
85 
86  void add_handshakers(const grpc_channel_args* args,
87  grpc_pollset_set* /*interested_parties*/,
88  HandshakeManager* handshake_mgr) override {
89  tsi_handshaker* handshaker = nullptr;
90  if (handshaker_factory_ != nullptr) {
92  handshaker_factory_, secure_peer_name_, /*network_bio_buf_size=*/0,
93  /*ssl_bio_buf_size=*/0, &handshaker);
94  if (result != TSI_OK) {
95  gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.",
97  }
98  }
99  handshake_mgr->Add(SecurityHandshakerCreate(handshaker, this, args));
100  }
101 
102  tsi_ssl_client_handshaker_factory* handshaker_factory() const {
103  return handshaker_factory_;
104  }
105 
106  void check_peer(tsi_peer peer, grpc_endpoint* /*ep*/,
107  RefCountedPtr<grpc_auth_context>* /*auth_context*/,
108  grpc_closure* on_peer_checked) override {
110 
111  /* Check the peer name. */
112  if (secure_peer_name_ != nullptr &&
115  "Peer name ", secure_peer_name_, " is not in peer certificate"));
116  }
117  ExecCtx::Run(DEBUG_LOCATION, on_peer_checked, error);
118  tsi_peer_destruct(&peer);
119  }
120 
121  void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
122  grpc_error_handle error) override {
124  }
125 
126  int cmp(const grpc_security_connector* other_sc) const override {
127  auto* other =
128  reinterpret_cast<const grpc_httpcli_ssl_channel_security_connector*>(
129  other_sc);
130  return strcmp(secure_peer_name_, other->secure_peer_name_);
131  }
132 
133  ArenaPromise<absl::Status> CheckCallHost(absl::string_view,
134  grpc_auth_context*) override {
135  return ImmediateOkStatus();
136  }
137 
138  const char* secure_peer_name() const { return secure_peer_name_; }
139 
140  private:
143 };
144 
145 RefCountedPtr<grpc_channel_security_connector>
146 httpcli_ssl_channel_security_connector_create(
147  const char* pem_root_certs, const tsi_ssl_root_certs_store* root_store,
148  const char* secure_peer_name) {
149  if (secure_peer_name != nullptr && pem_root_certs == nullptr) {
151  "Cannot assert a secure peer name without a trust root.");
152  return nullptr;
153  }
154  RefCountedPtr<grpc_httpcli_ssl_channel_security_connector> c =
155  MakeRefCounted<grpc_httpcli_ssl_channel_security_connector>(
156  secure_peer_name == nullptr ? nullptr : gpr_strdup(secure_peer_name));
157  tsi_result result = c->InitHandshakerFactory(pem_root_certs, root_store);
158  if (result != TSI_OK) {
159  gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
161  return nullptr;
162  }
163  return c;
164 }
165 
166 class HttpRequestSSLCredentials : public grpc_channel_credentials {
167  public:
168  RefCountedPtr<grpc_channel_security_connector> create_security_connector(
169  RefCountedPtr<grpc_call_credentials> /*call_creds*/, const char* target,
170  const grpc_channel_args* args,
171  grpc_channel_args** /*new_args*/) override {
173  const tsi_ssl_root_certs_store* root_store =
175  if (root_store == nullptr) {
176  gpr_log(GPR_ERROR, "Could not get default pem root certs.");
177  return nullptr;
178  }
179  const char* ssl_host_override =
181  if (ssl_host_override != nullptr) {
182  target = ssl_host_override;
183  }
184  return httpcli_ssl_channel_security_connector_create(pem_root_certs,
185  root_store, target);
186  }
187 
188  RefCountedPtr<grpc_channel_credentials> duplicate_without_call_credentials()
189  override {
190  return Ref();
191  }
192 
193  UniqueTypeName type() const override {
194  static UniqueTypeName::Factory kFactory("HttpRequestSSL");
195  return kFactory.Create();
196  }
197 
198  private:
199  int cmp_impl(const grpc_channel_credentials* /* other */) const override {
200  // There's no differentiating factor between two HttpRequestSSLCredentials
201  // objects.
202  return 0;
203  }
204 };
205 
206 } // namespace
207 
209  // Create a singleton object for HttpRequestSSLCredentials so that channels to
210  // the same target with HttpRequestSSLCredentials can reuse the subchannels.
211  static auto* creds = new HttpRequestSSLCredentials();
212  return creds->Ref();
213 }
214 
215 } // namespace grpc_core
grpc_channel_args_find_string
char * grpc_channel_args_find_string(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:441
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc_auth_context
Definition: security_context.h:63
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
tsi_create_ssl_client_handshaker_factory_with_options
tsi_result tsi_create_ssl_client_handshaker_factory_with_options(const tsi_ssl_client_handshaker_options *options, tsi_ssl_client_handshaker_factory **factory)
Definition: ssl_transport_security.cc:2007
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
pem_root_certs
static char * pem_root_certs
Definition: rb_channel_credentials.c:38
tsi_handshaker
Definition: transport_security.h:84
grpc_core::SecurityHandshakerCreate
RefCountedPtr< Handshaker > SecurityHandshakerCreate(tsi_handshaker *handshaker, grpc_security_connector *connector, const grpc_channel_args *args)
Creates a security handshaker using handshaker.
Definition: security_handshaker.cc:658
grpc_core
Definition: call_metric_recorder.h:31
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
tsi_ssl_client_handshaker_factory_unref
void tsi_ssl_client_handshaker_factory_unref(tsi_ssl_client_handshaker_factory *factory)
Definition: ssl_transport_security.cc:1783
string.h
options
double_dict options[]
Definition: capstone_test.c:55
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
tsi_ssl_client_handshaker_factory
Definition: ssl_transport_security.cc:93
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
error
grpc_error_handle error
Definition: retry_filter.cc:499
closure.h
grpc_core::DefaultSslRootStore::GetPemRootCerts
static const char * GetPemRootCerts()
Definition: ssl_utils.cc:568
grpc_core::CreateHttpRequestSSLCredentials
RefCountedPtr< grpc_channel_credentials > CreateHttpRequestSSLCredentials()
Definition: httpcli_security_connector.cc:208
grpc_security.h
ssl_transport_security.h
credentials.h
grpc_channel_args
Definition: grpc_types.h:132
grpc_types.h
grpc_core::DefaultSslRootStore::GetRootStore
static const tsi_ssl_root_certs_store * GetRootStore()
Definition: ssl_utils.cc:563
grpc_security_connector
Definition: security_connector.h:61
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
string_util.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr< grpc_channel_credentials >
GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
#define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
Definition: grpc_types.h:278
tsi_ssl_peer_matches_name
int tsi_ssl_peer_matches_name(const tsi_peer *peer, absl::string_view name)
Definition: ssl_transport_security.cc:2368
secure_peer_name_
char * secure_peer_name_
Definition: httpcli_security_connector.cc:142
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
security_connector.h
tsi_result
tsi_result
Definition: transport_security_interface.h:31
arena_promise.h
error.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
tests.google.protobuf.internal.message_test.cmp
cmp
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:61
transport_security_interface.h
promise.h
security_handshaker.h
tsi_peer
Definition: transport_security_interface.h:238
debug_location.h
tsi_ssl_root_certs_store
Definition: ssl_transport_security.cc:84
tsi_result_to_string
const char * tsi_result_to_string(tsi_result result)
Definition: transport_security.cc:35
poll.h
check_peer
static void check_peer(char *peer_name)
Definition: end2end/tests/simple_request.cc:91
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
alloc.h
exec_ctx.h
handshaker.h
unique_type_name.h
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
ref_counted_ptr.h
channel_args.h
handshaker_factory_
tsi_ssl_client_handshaker_factory * handshaker_factory_
Definition: httpcli_security_connector.cc:141
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_channel_security_connector
Definition: security_connector.h:118
tsi_ssl_client_handshaker_options
Definition: ssl_transport_security.h:137
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
iomgr_fwd.h
endpoint.h
grpc_error
Definition: error_internal.h:42
testing::Ref
internal::RefMatcher< T & > Ref(T &x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8628
grpc_closure
Definition: closure.h:56
tsi_peer_destruct
void tsi_peer_destruct(tsi_peer *self)
Definition: transport_security.cc:320
grpc_endpoint
Definition: endpoint.h:105
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
tsi_ssl_client_handshaker_factory_create_handshaker
tsi_result tsi_ssl_client_handshaker_factory_create_handshaker(tsi_ssl_client_handshaker_factory *factory, const char *server_name_indication, size_t network_bio_buf_size, size_t ssl_bio_buf_size, tsi_handshaker **handshaker)
Definition: ssl_transport_security.cc:1774
ssl_utils.h
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:02