Debian Codegraph @

src/core/lib/http/httpcli_security_connector.cc

raw
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
19 #include <grpc/support/port_platform.h>
20
21 #include <string.h>
22
23 #include <string>
24
25 #include "absl/status/status.h"
26 #include "absl/strings/str_cat.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/types/optional.h"
29
30 #include <grpc/grpc.h>
31 #include <grpc/grpc_security.h>
32 #include <grpc/impl/codegen/grpc_types.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35 #include <grpc/support/string_util.h>
36
37 #include "src/core/lib/channel/channel_args.h"
38 #include "src/core/lib/gprpp/debug_location.h"
39 #include "src/core/lib/gprpp/ref_counted_ptr.h"
40 #include "src/core/lib/gprpp/unique_type_name.h"
41 #include "src/core/lib/iomgr/closure.h"
42 #include "src/core/lib/iomgr/endpoint.h"
43 #include "src/core/lib/iomgr/error.h"
44 #include "src/core/lib/iomgr/exec_ctx.h"
45 #include "src/core/lib/iomgr/iomgr_fwd.h"
46 #include "src/core/lib/promise/arena_promise.h"
47 #include "src/core/lib/promise/promise.h"
48 #include "src/core/lib/security/credentials/credentials.h"
49 #include "src/core/lib/security/security_connector/security_connector.h"
50 #include "src/core/lib/security/security_connector/ssl_utils.h"
51 #include "src/core/lib/security/transport/security_handshaker.h"
52 #include "src/core/lib/transport/handshaker.h"
53 #include "src/core/tsi/ssl_transport_security.h"
54 #include "src/core/tsi/transport_security_interface.h"
55
56 namespace grpc_core {
57
58 namespace {
59
60 class grpc_httpcli_ssl_channel_security_connector final
61 : public grpc_channel_security_connector {
62 public:
63 explicit grpc_httpcli_ssl_channel_security_connector(char* secure_peer_name)
64 : grpc_channel_security_connector(
65 /*url_scheme=*/{},
66 /*channel_creds=*/nullptr,
67 /*request_metadata_creds=*/nullptr),
68 secure_peer_name_(secure_peer_name) {}
69
70 ~grpc_httpcli_ssl_channel_security_connector() override {
71 if (handshaker_factory_ != nullptr) {
72 tsi_ssl_client_handshaker_factory_unref(handshaker_factory_);
73 }
74 if (secure_peer_name_ != nullptr) {
75 gpr_free(secure_peer_name_);
76 }
77 }
78
79 tsi_result InitHandshakerFactory(const char* pem_root_certs,
80 const tsi_ssl_root_certs_store* root_store) {
81 tsi_ssl_client_handshaker_options options;
82 options.pem_root_certs = pem_root_certs;
83 options.root_store = root_store;
84 return tsi_create_ssl_client_handshaker_factory_with_options(
85 &options, &handshaker_factory_);
86 }
87
88 void add_handshakers(const ChannelArgs& args,
89 grpc_pollset_set* /*interested_parties*/,
90 HandshakeManager* handshake_mgr) override {
91 tsi_handshaker* handshaker = nullptr;
92 if (handshaker_factory_ != nullptr) {
93 tsi_result result = tsi_ssl_client_handshaker_factory_create_handshaker(
94 handshaker_factory_, secure_peer_name_, /*network_bio_buf_size=*/0,
95 /*ssl_bio_buf_size=*/0, &handshaker);
96 if (result != TSI_OK) {
97 gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.",
98 tsi_result_to_string(result));
99 }
100 }
101 handshake_mgr->Add(SecurityHandshakerCreate(handshaker, this, args));
102 }
103
104 tsi_ssl_client_handshaker_factory* handshaker_factory() const {
105 return handshaker_factory_;
106 }
107
108 void check_peer(tsi_peer peer, grpc_endpoint* /*ep*/,
109 const ChannelArgs& /*args*/,
110 RefCountedPtr<grpc_auth_context>* /*auth_context*/,
111 grpc_closure* on_peer_checked) override {
112 grpc_error_handle error;
113
114 /* Check the peer name. */
115 if (secure_peer_name_ != nullptr &&
116 !tsi_ssl_peer_matches_name(&peer, secure_peer_name_)) {
117 error = GRPC_ERROR_CREATE(absl::StrCat("Peer name ", secure_peer_name_,
118 " is not in peer certificate"));
119 }
120 ExecCtx::Run(DEBUG_LOCATION, on_peer_checked, error);
121 tsi_peer_destruct(&peer);
122 }
123
124 void cancel_check_peer(grpc_closure* /*on_peer_checked*/,
125 grpc_error_handle /*error*/) override {}
126
127 int cmp(const grpc_security_connector* other_sc) const override {
128 auto* other =
129 reinterpret_cast<const grpc_httpcli_ssl_channel_security_connector*>(
130 other_sc);
131 return strcmp(secure_peer_name_, other->secure_peer_name_);
132 }
133
134 ArenaPromise<absl::Status> CheckCallHost(absl::string_view,
135 grpc_auth_context*) override {
136 return ImmediateOkStatus();
137 }
138
139 const char* secure_peer_name() const { return secure_peer_name_; }
140
141 private:
142 tsi_ssl_client_handshaker_factory* handshaker_factory_ = nullptr;
143 char* secure_peer_name_;
144 };
145
146 RefCountedPtr<grpc_channel_security_connector>
147 httpcli_ssl_channel_security_connector_create(
148 const char* pem_root_certs, const tsi_ssl_root_certs_store* root_store,
149 const char* secure_peer_name) {
150 if (secure_peer_name != nullptr && pem_root_certs == nullptr) {
151 gpr_log(GPR_ERROR,
152 "Cannot assert a secure peer name without a trust root.");
153 return nullptr;
154 }
155 RefCountedPtr<grpc_httpcli_ssl_channel_security_connector> c =
156 MakeRefCounted<grpc_httpcli_ssl_channel_security_connector>(
157 secure_peer_name == nullptr ? nullptr : gpr_strdup(secure_peer_name));
158 tsi_result result = c->InitHandshakerFactory(pem_root_certs, root_store);
159 if (result != TSI_OK) {
160 gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
161 tsi_result_to_string(result));
162 return nullptr;
163 }
164 return c;
165 }
166
167 class HttpRequestSSLCredentials : public grpc_channel_credentials {
168 public:
169 RefCountedPtr<grpc_channel_security_connector> create_security_connector(
170 RefCountedPtr<grpc_call_credentials> /*call_creds*/, const char* target,
171 ChannelArgs* args) override {
172 const char* pem_root_certs = DefaultSslRootStore::GetPemRootCerts();
173 const tsi_ssl_root_certs_store* root_store =
174 DefaultSslRootStore::GetRootStore();
175 if (root_store == nullptr) {
176 gpr_log(GPR_ERROR, "Could not get default pem root certs.");
177 return nullptr;
178 }
179 absl::optional<std::string> target_string =
180 args->GetOwnedString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)
181 .value_or(target);
182 return httpcli_ssl_channel_security_connector_create(
183 pem_root_certs, root_store, target_string->c_str());
184 }
185
186 RefCountedPtr<grpc_channel_credentials> duplicate_without_call_credentials()
187 override {
188 return Ref();
189 }
190
191 UniqueTypeName type() const override {
192 static UniqueTypeName::Factory kFactory("HttpRequestSSL");
193 return kFactory.Create();
194 }
195
196 private:
197 int cmp_impl(const grpc_channel_credentials* /* other */) const override {
198 // There's no differentiating factor between two HttpRequestSSLCredentials
199 // objects.
200 return 0;
201 }
202 };
203
204 } // namespace
205
206 RefCountedPtr<grpc_channel_credentials> CreateHttpRequestSSLCredentials() {
207 // Create a singleton object for HttpRequestSSLCredentials so that channels to
208 // the same target with HttpRequestSSLCredentials can reuse the subchannels.
209 static auto* creds = new HttpRequestSSLCredentials();
210 return creds->Ref();
211 }
212
213 } // namespace grpc_core