Built motion from commit 44377920.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / security / security_connector / security_connector.cc
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
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_channel_security_connector::grpc_channel_security_connector(
50     const char* url_scheme,
51     grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,
52     grpc_core::RefCountedPtr<grpc_call_credentials> request_metadata_creds)
53     : grpc_security_connector(url_scheme),
54       channel_creds_(std::move(channel_creds)),
55       request_metadata_creds_(std::move(request_metadata_creds)) {}
56 grpc_channel_security_connector::~grpc_channel_security_connector() {}
57
58 int grpc_security_connector_cmp(const grpc_security_connector* sc,
59                                 const grpc_security_connector* other) {
60   if (sc == nullptr || other == nullptr) return GPR_ICMP(sc, other);
61   return sc->cmp(other);
62 }
63
64 int grpc_channel_security_connector::channel_security_connector_cmp(
65     const grpc_channel_security_connector* other) const {
66   const grpc_channel_security_connector* other_sc =
67       static_cast<const grpc_channel_security_connector*>(other);
68   GPR_ASSERT(channel_creds() != nullptr);
69   GPR_ASSERT(other_sc->channel_creds() != nullptr);
70   int c = GPR_ICMP(channel_creds(), other_sc->channel_creds());
71   if (c != 0) return c;
72   return GPR_ICMP(request_metadata_creds(), other_sc->request_metadata_creds());
73 }
74
75 int grpc_server_security_connector::server_security_connector_cmp(
76     const grpc_server_security_connector* other) const {
77   const grpc_server_security_connector* other_sc =
78       static_cast<const grpc_server_security_connector*>(other);
79   GPR_ASSERT(server_creds() != nullptr);
80   GPR_ASSERT(other_sc->server_creds() != nullptr);
81   return GPR_ICMP(server_creds(), other_sc->server_creds());
82 }
83
84 static void connector_arg_destroy(void* p) {
85   static_cast<grpc_security_connector*>(p)->Unref(DEBUG_LOCATION,
86                                                   "connector_arg_destroy");
87 }
88
89 static void* connector_arg_copy(void* p) {
90   return static_cast<grpc_security_connector*>(p)
91       ->Ref(DEBUG_LOCATION, "connector_arg_copy")
92       .release();
93 }
94
95 static int connector_cmp(void* a, void* b) {
96   return static_cast<grpc_security_connector*>(a)->cmp(
97       static_cast<grpc_security_connector*>(b));
98 }
99
100 static const grpc_arg_pointer_vtable connector_arg_vtable = {
101     connector_arg_copy, connector_arg_destroy, connector_cmp};
102
103 grpc_arg grpc_security_connector_to_arg(grpc_security_connector* sc) {
104   return grpc_channel_arg_pointer_create((char*)GRPC_ARG_SECURITY_CONNECTOR, sc,
105                                          &connector_arg_vtable);
106 }
107
108 grpc_security_connector* grpc_security_connector_from_arg(const grpc_arg* arg) {
109   if (strcmp(arg->key, GRPC_ARG_SECURITY_CONNECTOR)) return nullptr;
110   if (arg->type != GRPC_ARG_POINTER) {
111     gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
112             GRPC_ARG_SECURITY_CONNECTOR);
113     return nullptr;
114   }
115   return static_cast<grpc_security_connector*>(arg->value.pointer.p);
116 }
117
118 grpc_security_connector* grpc_security_connector_find_in_args(
119     const grpc_channel_args* args) {
120   size_t i;
121   if (args == nullptr) return nullptr;
122   for (i = 0; i < args->num_args; i++) {
123     grpc_security_connector* sc =
124         grpc_security_connector_from_arg(&args->args[i]);
125     if (sc != nullptr) return sc;
126   }
127   return nullptr;
128 }