Debian Codegraph @

src/core/lib/security/credentials/tls/tls_credentials.cc

raw
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/security/credentials/tls/tls_credentials.h"
22
23 #include <string>
24 #include <utility>
25
26 #include "absl/strings/string_view.h"
27 #include "absl/types/optional.h"
28
29 #include <grpc/grpc.h>
30 #include <grpc/grpc_security_constants.h>
31 #include <grpc/impl/codegen/grpc_types.h>
32 #include <grpc/support/log.h>
33
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/security/credentials/tls/grpc_tls_certificate_verifier.h"
37 #include "src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h"
38 #include "src/core/lib/security/security_connector/tls/tls_security_connector.h"
39 #include "src/core/tsi/ssl/session_cache/ssl_session_cache.h"
40
41 namespace {
42
43 bool CredentialOptionSanityCheck(grpc_tls_credentials_options* options,
44 bool is_client) {
45 if (options == nullptr) {
46 gpr_log(GPR_ERROR, "TLS credentials options is nullptr.");
47 return false;
48 }
49 // In the following conditions, there won't be any issues, but it might
50 // indicate callers are doing something wrong with the API.
51 if (is_client && options->cert_request_type() !=
52 GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE) {
53 gpr_log(GPR_ERROR,
54 "Client's credentials options should not set cert_request_type.");
55 }
56 if (!is_client && !options->verify_server_cert()) {
57 gpr_log(GPR_ERROR,
58 "Server's credentials options should not set verify_server_cert.");
59 }
60 // In the following conditions, there could be severe security issues.
61 if (is_client && options->certificate_verifier() == nullptr) {
62 // If no verifier is specified on the client side, use the hostname verifier
63 // as default. Users who want to bypass all the verifier check should
64 // implement an external verifier instead.
65 gpr_log(GPR_INFO,
66 "No verifier specified on the client side. Using default hostname "
67 "verifier");
68 options->set_certificate_verifier(
69 grpc_core::MakeRefCounted<grpc_core::HostNameCertificateVerifier>());
70 }
71 return true;
72 }
73
74 } // namespace
75
76 TlsCredentials::TlsCredentials(
77 grpc_core::RefCountedPtr<grpc_tls_credentials_options> options)
78 : options_(std::move(options)) {}
79
80 TlsCredentials::~TlsCredentials() {}
81
82 grpc_core::RefCountedPtr<grpc_channel_security_connector>
83 TlsCredentials::create_security_connector(
84 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
85 const char* target_name, grpc_core::ChannelArgs* args) {
86 absl::optional<std::string> overridden_target_name =
87 args->GetOwnedString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
88 auto* ssl_session_cache = args->GetObject<tsi::SslSessionLRUCache>();
89 grpc_core::RefCountedPtr<grpc_channel_security_connector> sc =
90 grpc_core::TlsChannelSecurityConnector::CreateTlsChannelSecurityConnector(
91 this->Ref(), options_, std::move(call_creds), target_name,
92 overridden_target_name.has_value() ? overridden_target_name->c_str()
93 : nullptr,
94 ssl_session_cache == nullptr ? nullptr : ssl_session_cache->c_ptr());
95 if (sc == nullptr) {
96 return nullptr;
97 }
98 *args = args->Set(GRPC_ARG_HTTP2_SCHEME, "https");
99 return sc;
100 }
101
102 grpc_core::UniqueTypeName TlsCredentials::type() const {
103 static grpc_core::UniqueTypeName::Factory kFactory("Tls");
104 return kFactory.Create();
105 }
106
107 int TlsCredentials::cmp_impl(const grpc_channel_credentials* other) const {
108 const TlsCredentials* o = static_cast<const TlsCredentials*>(other);
109 if (*options_ == *o->options_) return 0;
110 return grpc_core::QsortCompare(
111 static_cast<const grpc_channel_credentials*>(this), other);
112 }
113
114 TlsServerCredentials::TlsServerCredentials(
115 grpc_core::RefCountedPtr<grpc_tls_credentials_options> options)
116 : options_(std::move(options)) {}
117
118 TlsServerCredentials::~TlsServerCredentials() {}
119
120 grpc_core::RefCountedPtr<grpc_server_security_connector>
121 TlsServerCredentials::create_security_connector(
122 const grpc_core::ChannelArgs& /* args */) {
123 return grpc_core::TlsServerSecurityConnector::
124 CreateTlsServerSecurityConnector(this->Ref(), options_);
125 }
126
127 grpc_core::UniqueTypeName TlsServerCredentials::type() const {
128 static grpc_core::UniqueTypeName::Factory kFactory("Tls");
129 return kFactory.Create();
130 }
131
132 /** -- Wrapper APIs declared in grpc_security.h -- **/
133
134 grpc_channel_credentials* grpc_tls_credentials_create(
135 grpc_tls_credentials_options* options) {
136 if (!CredentialOptionSanityCheck(options, true /* is_client */)) {
137 return nullptr;
138 }
139 return new TlsCredentials(
140 grpc_core::RefCountedPtr<grpc_tls_credentials_options>(options));
141 }
142
143 grpc_server_credentials* grpc_tls_server_credentials_create(
144 grpc_tls_credentials_options* options) {
145 if (!CredentialOptionSanityCheck(options, false /* is_client */)) {
146 return nullptr;
147 }
148 return new TlsServerCredentials(
149 grpc_core::RefCountedPtr<grpc_tls_credentials_options>(options));
150 }