• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/lib/security/security_connector/security_connector.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <string.h>
23 
24 #include <utility>
25 
26 #include "absl/log/check.h"
27 #include "absl/log/log.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/security/credentials/credentials.h"
30 #include "src/core/util/debug_location.h"
31 #include "src/core/util/useful.h"
32 
grpc_channel_security_connector(absl::string_view url_scheme,grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,grpc_core::RefCountedPtr<grpc_call_credentials> request_metadata_creds)33 grpc_channel_security_connector::grpc_channel_security_connector(
34     absl::string_view url_scheme,
35     grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,
36     grpc_core::RefCountedPtr<grpc_call_credentials> request_metadata_creds)
37     : grpc_security_connector(url_scheme),
38       channel_creds_(std::move(channel_creds)),
39       request_metadata_creds_(std::move(request_metadata_creds)) {}
40 
channel_security_connector_cmp(const grpc_channel_security_connector * other) const41 int grpc_channel_security_connector::channel_security_connector_cmp(
42     const grpc_channel_security_connector* other) const {
43   const grpc_channel_security_connector* other_sc =
44       static_cast<const grpc_channel_security_connector*>(other);
45   CHECK_NE(channel_creds(), nullptr);
46   CHECK_NE(other_sc->channel_creds(), nullptr);
47   int c = channel_creds()->cmp(other_sc->channel_creds());
48   if (c != 0) return c;
49   return grpc_core::QsortCompare(request_metadata_creds(),
50                                  other_sc->request_metadata_creds());
51 }
52 
type() const53 grpc_core::UniqueTypeName grpc_channel_security_connector::type() const {
54   return channel_creds_->type();
55 }
56 
grpc_server_security_connector(absl::string_view url_scheme,grpc_core::RefCountedPtr<grpc_server_credentials> server_creds)57 grpc_server_security_connector::grpc_server_security_connector(
58     absl::string_view url_scheme,
59     grpc_core::RefCountedPtr<grpc_server_credentials> server_creds)
60     : grpc_security_connector(url_scheme),
61       server_creds_(std::move(server_creds)) {}
62 
server_security_connector_cmp(const grpc_server_security_connector * other) const63 int grpc_server_security_connector::server_security_connector_cmp(
64     const grpc_server_security_connector* other) const {
65   const grpc_server_security_connector* other_sc =
66       static_cast<const grpc_server_security_connector*>(other);
67   CHECK_NE(server_creds(), nullptr);
68   CHECK_NE(other_sc->server_creds(), nullptr);
69   return grpc_core::QsortCompare(server_creds(), other_sc->server_creds());
70 }
71 
type() const72 grpc_core::UniqueTypeName grpc_server_security_connector::type() const {
73   return server_creds_->type();
74 }
75 
connector_arg_destroy(void * p)76 static void connector_arg_destroy(void* p) {
77   if (p == nullptr) return;
78   static_cast<grpc_security_connector*>(p)->Unref(DEBUG_LOCATION,
79                                                   "connector_arg_destroy");
80 }
81 
connector_arg_copy(void * p)82 static void* connector_arg_copy(void* p) {
83   if (p == nullptr) return nullptr;
84   return static_cast<grpc_security_connector*>(p)
85       ->Ref(DEBUG_LOCATION, "connector_arg_copy")
86       .release();
87 }
88 
connector_cmp(void * a,void * b)89 static int connector_cmp(void* a, void* b) {
90   return static_cast<grpc_security_connector*>(a)->cmp(
91       static_cast<grpc_security_connector*>(b));
92 }
93 
94 static const grpc_arg_pointer_vtable connector_arg_vtable = {
95     connector_arg_copy, connector_arg_destroy, connector_cmp};
96 
grpc_security_connector_to_arg(grpc_security_connector * sc)97 grpc_arg grpc_security_connector_to_arg(grpc_security_connector* sc) {
98   return grpc_channel_arg_pointer_create(
99       const_cast<char*>(GRPC_ARG_SECURITY_CONNECTOR), sc,
100       &connector_arg_vtable);
101 }
102 
grpc_security_connector_from_arg(const grpc_arg * arg)103 grpc_security_connector* grpc_security_connector_from_arg(const grpc_arg* arg) {
104   if (strcmp(arg->key, GRPC_ARG_SECURITY_CONNECTOR) != 0) return nullptr;
105   if (arg->type != GRPC_ARG_POINTER) {
106     LOG(ERROR) << "Invalid type " << arg->type << " for arg "
107                << GRPC_ARG_SECURITY_CONNECTOR;
108     return nullptr;
109   }
110   return static_cast<grpc_security_connector*>(arg->value.pointer.p);
111 }
112 
grpc_security_connector_find_in_args(const grpc_channel_args * args)113 grpc_security_connector* grpc_security_connector_find_in_args(
114     const grpc_channel_args* args) {
115   size_t i;
116   if (args == nullptr) return nullptr;
117   for (i = 0; i < args->num_args; i++) {
118     grpc_security_connector* sc =
119         grpc_security_connector_from_arg(&args->args[i]);
120     if (sc != nullptr) return sc;
121   }
122   return nullptr;
123 }
124