• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstring>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/security/security_connector/tls/tls_security_connector.h"
32 
33 #define GRPC_CREDENTIALS_TYPE_TLS "Tls"
34 
35 namespace {
36 
CredentialOptionSanityCheck(const grpc_tls_credentials_options * options,bool is_client)37 bool CredentialOptionSanityCheck(const grpc_tls_credentials_options* options,
38                                  bool is_client) {
39   if (options == nullptr) {
40     gpr_log(GPR_ERROR, "TLS credentials options is nullptr.");
41     return false;
42   }
43   if (options->key_materials_config() == nullptr &&
44       options->credential_reload_config() == nullptr) {
45     gpr_log(GPR_ERROR,
46             "TLS credentials options must specify either key materials or "
47             "credential reload config.");
48     return false;
49   }
50   if (!is_client && options->server_authorization_check_config() != nullptr) {
51     gpr_log(GPR_INFO,
52             "Server's credentials options should not contain server "
53             "authorization check config.");
54   }
55   return true;
56 }
57 
58 }  // namespace
59 
TlsCredentials(grpc_core::RefCountedPtr<grpc_tls_credentials_options> options)60 TlsCredentials::TlsCredentials(
61     grpc_core::RefCountedPtr<grpc_tls_credentials_options> options)
62     : grpc_channel_credentials(GRPC_CREDENTIALS_TYPE_TLS),
63       options_(std::move(options)) {}
64 
~TlsCredentials()65 TlsCredentials::~TlsCredentials() {}
66 
67 grpc_core::RefCountedPtr<grpc_channel_security_connector>
create_security_connector(grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,const char * target_name,const grpc_channel_args * args,grpc_channel_args ** new_args)68 TlsCredentials::create_security_connector(
69     grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
70     const char* target_name, const grpc_channel_args* args,
71     grpc_channel_args** new_args) {
72   const char* overridden_target_name = nullptr;
73   tsi_ssl_session_cache* ssl_session_cache = nullptr;
74   for (size_t i = 0; args != nullptr && i < args->num_args; i++) {
75     grpc_arg* arg = &args->args[i];
76     if (strcmp(arg->key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == 0 &&
77         arg->type == GRPC_ARG_STRING) {
78       overridden_target_name = arg->value.string;
79     }
80     if (strcmp(arg->key, GRPC_SSL_SESSION_CACHE_ARG) == 0 &&
81         arg->type == GRPC_ARG_POINTER) {
82       ssl_session_cache =
83           static_cast<tsi_ssl_session_cache*>(arg->value.pointer.p);
84     }
85   }
86   grpc_core::RefCountedPtr<grpc_channel_security_connector> sc =
87       grpc_core::TlsChannelSecurityConnector::CreateTlsChannelSecurityConnector(
88           this->Ref(), std::move(call_creds), target_name,
89           overridden_target_name, ssl_session_cache);
90   if (sc == nullptr) {
91     return nullptr;
92   }
93   grpc_arg new_arg = grpc_channel_arg_string_create(
94       (char*)GRPC_ARG_HTTP2_SCHEME, (char*)"https");
95   *new_args = grpc_channel_args_copy_and_add(args, &new_arg, 1);
96   return sc;
97 }
98 
TlsServerCredentials(grpc_core::RefCountedPtr<grpc_tls_credentials_options> options)99 TlsServerCredentials::TlsServerCredentials(
100     grpc_core::RefCountedPtr<grpc_tls_credentials_options> options)
101     : grpc_server_credentials(GRPC_CREDENTIALS_TYPE_TLS),
102       options_(std::move(options)) {}
103 
~TlsServerCredentials()104 TlsServerCredentials::~TlsServerCredentials() {}
105 
106 grpc_core::RefCountedPtr<grpc_server_security_connector>
create_security_connector()107 TlsServerCredentials::create_security_connector() {
108   return grpc_core::TlsServerSecurityConnector::
109       CreateTlsServerSecurityConnector(this->Ref());
110 }
111 
grpc_tls_credentials_create(grpc_tls_credentials_options * options)112 grpc_channel_credentials* grpc_tls_credentials_create(
113     grpc_tls_credentials_options* options) {
114   if (!CredentialOptionSanityCheck(options, true /* is_client */)) {
115     return nullptr;
116   }
117   return new TlsCredentials(
118       grpc_core::RefCountedPtr<grpc_tls_credentials_options>(options));
119 }
120 
grpc_tls_server_credentials_create(grpc_tls_credentials_options * options)121 grpc_server_credentials* grpc_tls_server_credentials_create(
122     grpc_tls_credentials_options* options) {
123   if (!CredentialOptionSanityCheck(options, false /* is_client */)) {
124     return nullptr;
125   }
126   return new TlsServerCredentials(
127       grpc_core::RefCountedPtr<grpc_tls_credentials_options>(options));
128 }
129