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 "src/core/lib/security/security_connector/security_connector.h"
22
23 #include <grpc/slice_buffer.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/transport/chttp2/alpn/alpn.h"
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/channel/handshaker.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/gprpp/host_port.h"
33 #include "src/core/lib/iomgr/load_file.h"
34 #include "src/core/lib/security/context/security_context.h"
35 #include "src/core/lib/security/credentials/credentials.h"
36 #include "src/core/lib/security/security_connector/load_system_roots.h"
37 #include "src/core/lib/security/security_connector/security_connector.h"
38 #include "src/core/lib/security/transport/security_handshaker.h"
39
40 grpc_core::DebugOnlyTraceFlag grpc_trace_security_connector_refcount(
41 false, "security_connector_refcount");
42
grpc_server_security_connector(const char * url_scheme,grpc_core::RefCountedPtr<grpc_server_credentials> server_creds)43 grpc_server_security_connector::grpc_server_security_connector(
44 const char* url_scheme,
45 grpc_core::RefCountedPtr<grpc_server_credentials> server_creds)
46 : grpc_security_connector(url_scheme),
47 server_creds_(std::move(server_creds)) {}
48
49 grpc_server_security_connector::~grpc_server_security_connector() = default;
50
grpc_channel_security_connector(const char * url_scheme,grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,grpc_core::RefCountedPtr<grpc_call_credentials> request_metadata_creds)51 grpc_channel_security_connector::grpc_channel_security_connector(
52 const char* url_scheme,
53 grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,
54 grpc_core::RefCountedPtr<grpc_call_credentials> request_metadata_creds)
55 : grpc_security_connector(url_scheme),
56 channel_creds_(std::move(channel_creds)),
57 request_metadata_creds_(std::move(request_metadata_creds)) {}
58
~grpc_channel_security_connector()59 grpc_channel_security_connector::~grpc_channel_security_connector() {}
60
grpc_security_connector_cmp(const grpc_security_connector * sc,const grpc_security_connector * other)61 int grpc_security_connector_cmp(const grpc_security_connector* sc,
62 const grpc_security_connector* other) {
63 if (sc == nullptr || other == nullptr) return GPR_ICMP(sc, other);
64 return sc->cmp(other);
65 }
66
channel_security_connector_cmp(const grpc_channel_security_connector * other) const67 int grpc_channel_security_connector::channel_security_connector_cmp(
68 const grpc_channel_security_connector* other) const {
69 const grpc_channel_security_connector* other_sc =
70 static_cast<const grpc_channel_security_connector*>(other);
71 GPR_ASSERT(channel_creds() != nullptr);
72 GPR_ASSERT(other_sc->channel_creds() != nullptr);
73 int c = GPR_ICMP(channel_creds(), other_sc->channel_creds());
74 if (c != 0) return c;
75 return GPR_ICMP(request_metadata_creds(), other_sc->request_metadata_creds());
76 }
77
server_security_connector_cmp(const grpc_server_security_connector * other) const78 int grpc_server_security_connector::server_security_connector_cmp(
79 const grpc_server_security_connector* other) const {
80 const grpc_server_security_connector* other_sc =
81 static_cast<const grpc_server_security_connector*>(other);
82 GPR_ASSERT(server_creds() != nullptr);
83 GPR_ASSERT(other_sc->server_creds() != nullptr);
84 return GPR_ICMP(server_creds(), other_sc->server_creds());
85 }
86
connector_arg_destroy(void * p)87 static void connector_arg_destroy(void* p) {
88 static_cast<grpc_security_connector*>(p)->Unref(DEBUG_LOCATION,
89 "connector_arg_destroy");
90 }
91
connector_arg_copy(void * p)92 static void* connector_arg_copy(void* p) {
93 return static_cast<grpc_security_connector*>(p)
94 ->Ref(DEBUG_LOCATION, "connector_arg_copy")
95 .release();
96 }
97
connector_cmp(void * a,void * b)98 static int connector_cmp(void* a, void* b) {
99 return static_cast<grpc_security_connector*>(a)->cmp(
100 static_cast<grpc_security_connector*>(b));
101 }
102
103 static const grpc_arg_pointer_vtable connector_arg_vtable = {
104 connector_arg_copy, connector_arg_destroy, connector_cmp};
105
grpc_security_connector_to_arg(grpc_security_connector * sc)106 grpc_arg grpc_security_connector_to_arg(grpc_security_connector* sc) {
107 return grpc_channel_arg_pointer_create(
108 const_cast<char*>(GRPC_ARG_SECURITY_CONNECTOR), sc,
109 &connector_arg_vtable);
110 }
111
grpc_security_connector_from_arg(const grpc_arg * arg)112 grpc_security_connector* grpc_security_connector_from_arg(const grpc_arg* arg) {
113 if (strcmp(arg->key, GRPC_ARG_SECURITY_CONNECTOR) != 0) return nullptr;
114 if (arg->type != GRPC_ARG_POINTER) {
115 gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
116 GRPC_ARG_SECURITY_CONNECTOR);
117 return nullptr;
118 }
119 return static_cast<grpc_security_connector*>(arg->value.pointer.p);
120 }
121
grpc_security_connector_find_in_args(const grpc_channel_args * args)122 grpc_security_connector* grpc_security_connector_find_in_args(
123 const grpc_channel_args* args) {
124 size_t i;
125 if (args == nullptr) return nullptr;
126 for (i = 0; i < args->num_args; i++) {
127 grpc_security_connector* sc =
128 grpc_security_connector_from_arg(&args->args[i]);
129 if (sc != nullptr) return sc;
130 }
131 return nullptr;
132 }
133