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 <grpc/grpc.h>
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/filters/client_channel/client_channel.h"
29 #include "src/core/ext/filters/client_channel/resolver_registry.h"
30 #include "src/core/ext/filters/client_channel/uri_parser.h"
31 #include "src/core/ext/transport/chttp2/client/chttp2_connector.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/gprpp/memory.h"
34 #include "src/core/lib/iomgr/sockaddr_utils.h"
35 #include "src/core/lib/security/credentials/credentials.h"
36 #include "src/core/lib/security/security_connector/security_connector.h"
37 #include "src/core/lib/security/transport/target_authority_table.h"
38 #include "src/core/lib/slice/slice_hash_table.h"
39 #include "src/core/lib/slice/slice_internal.h"
40 #include "src/core/lib/surface/api_trace.h"
41 #include "src/core/lib/surface/channel.h"
42
client_channel_factory_ref(grpc_client_channel_factory * cc_factory)43 static void client_channel_factory_ref(
44 grpc_client_channel_factory* cc_factory) {}
45
client_channel_factory_unref(grpc_client_channel_factory * cc_factory)46 static void client_channel_factory_unref(
47 grpc_client_channel_factory* cc_factory) {}
48
get_secure_naming_subchannel_args(const grpc_subchannel_args * args)49 static grpc_subchannel_args* get_secure_naming_subchannel_args(
50 const grpc_subchannel_args* args) {
51 grpc_channel_credentials* channel_credentials =
52 grpc_channel_credentials_find_in_args(args->args);
53 if (channel_credentials == nullptr) {
54 gpr_log(GPR_ERROR,
55 "Can't create subchannel: channel credentials missing for secure "
56 "channel.");
57 return nullptr;
58 }
59 // Make sure security connector does not already exist in args.
60 if (grpc_security_connector_find_in_args(args->args) != nullptr) {
61 gpr_log(GPR_ERROR,
62 "Can't create subchannel: security connector already present in "
63 "channel args.");
64 return nullptr;
65 }
66 // To which address are we connecting? By default, use the server URI.
67 const grpc_arg* server_uri_arg =
68 grpc_channel_args_find(args->args, GRPC_ARG_SERVER_URI);
69 const char* server_uri_str = grpc_channel_arg_get_string(server_uri_arg);
70 GPR_ASSERT(server_uri_str != nullptr);
71 grpc_uri* server_uri =
72 grpc_uri_parse(server_uri_str, true /* supress errors */);
73 GPR_ASSERT(server_uri != nullptr);
74 const grpc_core::TargetAuthorityTable* target_authority_table =
75 grpc_core::FindTargetAuthorityTableInArgs(args->args);
76 grpc_core::UniquePtr<char> authority;
77 if (target_authority_table != nullptr) {
78 // Find the authority for the target.
79 const char* target_uri_str =
80 grpc_get_subchannel_address_uri_arg(args->args);
81 grpc_uri* target_uri =
82 grpc_uri_parse(target_uri_str, false /* suppress errors */);
83 GPR_ASSERT(target_uri != nullptr);
84 if (target_uri->path[0] != '\0') { // "path" may be empty
85 const grpc_slice key = grpc_slice_from_static_string(
86 target_uri->path[0] == '/' ? target_uri->path + 1 : target_uri->path);
87 const grpc_core::UniquePtr<char>* value =
88 target_authority_table->Get(key);
89 if (value != nullptr) authority.reset(gpr_strdup(value->get()));
90 grpc_slice_unref_internal(key);
91 }
92 grpc_uri_destroy(target_uri);
93 }
94 // If the authority hasn't already been set (either because no target
95 // authority table was present or because the target was not present
96 // in the table), fall back to using the original server URI.
97 if (authority == nullptr) {
98 authority =
99 grpc_core::ResolverRegistry::GetDefaultAuthority(server_uri_str);
100 }
101 grpc_arg args_to_add[2];
102 size_t num_args_to_add = 0;
103 if (grpc_channel_args_find(args->args, GRPC_ARG_DEFAULT_AUTHORITY) ==
104 nullptr) {
105 // If the channel args don't already contain GRPC_ARG_DEFAULT_AUTHORITY, add
106 // the arg, setting it to the value just obtained.
107 args_to_add[num_args_to_add++] = grpc_channel_arg_string_create(
108 const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY), authority.get());
109 }
110 grpc_channel_args* args_with_authority =
111 grpc_channel_args_copy_and_add(args->args, args_to_add, num_args_to_add);
112 grpc_uri_destroy(server_uri);
113 grpc_channel_security_connector* subchannel_security_connector = nullptr;
114 // Create the security connector using the credentials and target name.
115 grpc_channel_args* new_args_from_connector = nullptr;
116 const grpc_security_status security_status =
117 grpc_channel_credentials_create_security_connector(
118 channel_credentials, authority.get(), args_with_authority,
119 &subchannel_security_connector, &new_args_from_connector);
120 if (security_status != GRPC_SECURITY_OK) {
121 gpr_log(GPR_ERROR,
122 "Failed to create secure subchannel for secure name '%s'",
123 authority.get());
124 grpc_channel_args_destroy(args_with_authority);
125 return nullptr;
126 }
127 grpc_arg new_security_connector_arg =
128 grpc_security_connector_to_arg(&subchannel_security_connector->base);
129
130 grpc_channel_args* new_args = grpc_channel_args_copy_and_add(
131 new_args_from_connector != nullptr ? new_args_from_connector
132 : args_with_authority,
133 &new_security_connector_arg, 1);
134
135 GRPC_SECURITY_CONNECTOR_UNREF(&subchannel_security_connector->base,
136 "lb_channel_create");
137 if (new_args_from_connector != nullptr) {
138 grpc_channel_args_destroy(new_args_from_connector);
139 }
140 grpc_channel_args_destroy(args_with_authority);
141 grpc_subchannel_args* final_sc_args =
142 static_cast<grpc_subchannel_args*>(gpr_malloc(sizeof(*final_sc_args)));
143 memcpy(final_sc_args, args, sizeof(*args));
144 final_sc_args->args = new_args;
145 return final_sc_args;
146 }
147
client_channel_factory_create_subchannel(grpc_client_channel_factory * cc_factory,const grpc_subchannel_args * args)148 static grpc_subchannel* client_channel_factory_create_subchannel(
149 grpc_client_channel_factory* cc_factory, const grpc_subchannel_args* args) {
150 grpc_subchannel_args* subchannel_args =
151 get_secure_naming_subchannel_args(args);
152 if (subchannel_args == nullptr) {
153 gpr_log(
154 GPR_ERROR,
155 "Failed to create subchannel arguments during subchannel creation.");
156 return nullptr;
157 }
158 grpc_connector* connector = grpc_chttp2_connector_create();
159 grpc_subchannel* s = grpc_subchannel_create(connector, subchannel_args);
160 grpc_connector_unref(connector);
161 grpc_channel_args_destroy(
162 const_cast<grpc_channel_args*>(subchannel_args->args));
163 gpr_free(subchannel_args);
164 return s;
165 }
166
client_channel_factory_create_channel(grpc_client_channel_factory * cc_factory,const char * target,grpc_client_channel_type type,const grpc_channel_args * args)167 static grpc_channel* client_channel_factory_create_channel(
168 grpc_client_channel_factory* cc_factory, const char* target,
169 grpc_client_channel_type type, const grpc_channel_args* args) {
170 if (target == nullptr) {
171 gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
172 return nullptr;
173 }
174 // Add channel arg containing the server URI.
175 grpc_core::UniquePtr<char> canonical_target =
176 grpc_core::ResolverRegistry::AddDefaultPrefixIfNeeded(target);
177 grpc_arg arg = grpc_channel_arg_string_create((char*)GRPC_ARG_SERVER_URI,
178 canonical_target.get());
179 const char* to_remove[] = {GRPC_ARG_SERVER_URI};
180 grpc_channel_args* new_args =
181 grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
182 grpc_channel* channel =
183 grpc_channel_create(target, new_args, GRPC_CLIENT_CHANNEL, nullptr);
184 grpc_channel_args_destroy(new_args);
185 return channel;
186 }
187
188 static const grpc_client_channel_factory_vtable client_channel_factory_vtable =
189 {client_channel_factory_ref, client_channel_factory_unref,
190 client_channel_factory_create_subchannel,
191 client_channel_factory_create_channel};
192
193 static grpc_client_channel_factory client_channel_factory = {
194 &client_channel_factory_vtable};
195
196 // Create a secure client channel:
197 // Asynchronously: - resolve target
198 // - connect to it (trying alternatives as presented)
199 // - perform handshakes
grpc_secure_channel_create(grpc_channel_credentials * creds,const char * target,const grpc_channel_args * args,void * reserved)200 grpc_channel* grpc_secure_channel_create(grpc_channel_credentials* creds,
201 const char* target,
202 const grpc_channel_args* args,
203 void* reserved) {
204 grpc_core::ExecCtx exec_ctx;
205 GRPC_API_TRACE(
206 "grpc_secure_channel_create(creds=%p, target=%s, args=%p, "
207 "reserved=%p)",
208 4, ((void*)creds, target, (void*)args, (void*)reserved));
209 GPR_ASSERT(reserved == nullptr);
210 grpc_channel* channel = nullptr;
211 if (creds != nullptr) {
212 // Add channel args containing the client channel factory and channel
213 // credentials.
214 grpc_arg args_to_add[] = {
215 grpc_client_channel_factory_create_channel_arg(&client_channel_factory),
216 grpc_channel_credentials_to_arg(creds)};
217 grpc_channel_args* new_args = grpc_channel_args_copy_and_add(
218 args, args_to_add, GPR_ARRAY_SIZE(args_to_add));
219 // Create channel.
220 channel = client_channel_factory_create_channel(
221 &client_channel_factory, target, GRPC_CLIENT_CHANNEL_TYPE_REGULAR,
222 new_args);
223 // Clean up.
224 grpc_channel_args_destroy(new_args);
225 }
226 return channel != nullptr ? channel
227 : grpc_lame_client_channel_create(
228 target, GRPC_STATUS_INTERNAL,
229 "Failed to create secure client channel");
230 }
231