• 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/credentials/credentials.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <stdint.h>
23 #include <string.h>
24 
25 #include "absl/log/check.h"
26 #include "absl/log/log.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/debug/trace.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/util/useful.h"
31 
32 // -- Common. --
33 
grpc_channel_credentials_release(grpc_channel_credentials * creds)34 void grpc_channel_credentials_release(grpc_channel_credentials* creds) {
35   GRPC_TRACE_LOG(api, INFO)
36       << "grpc_channel_credentials_release(creds=" << creds << ")";
37   grpc_core::ExecCtx exec_ctx;
38   if (creds) creds->Unref();
39 }
40 
grpc_call_credentials_release(grpc_call_credentials * creds)41 void grpc_call_credentials_release(grpc_call_credentials* creds) {
42   GRPC_TRACE_LOG(api, INFO)
43       << "grpc_call_credentials_release(creds=" << creds << ")";
44   grpc_core::ExecCtx exec_ctx;
45   if (creds) creds->Unref();
46 }
47 
credentials_pointer_arg_destroy(void * p)48 static void credentials_pointer_arg_destroy(void* p) {
49   static_cast<grpc_channel_credentials*>(p)->Unref();
50 }
51 
credentials_pointer_arg_copy(void * p)52 static void* credentials_pointer_arg_copy(void* p) {
53   return static_cast<grpc_channel_credentials*>(p)->Ref().release();
54 }
55 
credentials_pointer_cmp(void * a,void * b)56 static int credentials_pointer_cmp(void* a, void* b) {
57   return static_cast<const grpc_channel_credentials*>(a)->cmp(
58       static_cast<const grpc_channel_credentials*>(b));
59 }
60 
61 static const grpc_arg_pointer_vtable credentials_pointer_vtable = {
62     credentials_pointer_arg_copy, credentials_pointer_arg_destroy,
63     credentials_pointer_cmp};
64 
grpc_channel_credentials_to_arg(grpc_channel_credentials * credentials)65 grpc_arg grpc_channel_credentials_to_arg(
66     grpc_channel_credentials* credentials) {
67   return grpc_channel_arg_pointer_create(
68       const_cast<char*>(GRPC_ARG_CHANNEL_CREDENTIALS), credentials,
69       &credentials_pointer_vtable);
70 }
71 
grpc_channel_credentials_from_arg(const grpc_arg * arg)72 grpc_channel_credentials* grpc_channel_credentials_from_arg(
73     const grpc_arg* arg) {
74   if (strcmp(arg->key, GRPC_ARG_CHANNEL_CREDENTIALS) != 0) return nullptr;
75   if (arg->type != GRPC_ARG_POINTER) {
76     LOG(ERROR) << "Invalid type " << arg->type << " for arg "
77                << GRPC_ARG_CHANNEL_CREDENTIALS;
78     return nullptr;
79   }
80   return static_cast<grpc_channel_credentials*>(arg->value.pointer.p);
81 }
82 
grpc_channel_credentials_find_in_args(const grpc_channel_args * args)83 grpc_channel_credentials* grpc_channel_credentials_find_in_args(
84     const grpc_channel_args* args) {
85   size_t i;
86   if (args == nullptr) return nullptr;
87   for (i = 0; i < args->num_args; i++) {
88     grpc_channel_credentials* credentials =
89         grpc_channel_credentials_from_arg(&args->args[i]);
90     if (credentials != nullptr) return credentials;
91   }
92   return nullptr;
93 }
94 
grpc_server_credentials_release(grpc_server_credentials * creds)95 void grpc_server_credentials_release(grpc_server_credentials* creds) {
96   GRPC_TRACE_LOG(api, INFO)
97       << "grpc_server_credentials_release(creds=" << creds << ")";
98   grpc_core::ExecCtx exec_ctx;
99   if (creds) creds->Unref();
100 }
101 
set_auth_metadata_processor(const grpc_auth_metadata_processor & processor)102 void grpc_server_credentials::set_auth_metadata_processor(
103     const grpc_auth_metadata_processor& processor) {
104   GRPC_TRACE_LOG(api, INFO)
105       << "grpc_server_credentials_set_auth_metadata_processor(creds=" << this
106       << ", processor=grpc_auth_metadata_processor { process: "
107       << (void*)(intptr_t)processor.process << ", state: " << processor.state
108       << " })";
109   DestroyProcessor();
110   processor_ = processor;
111 }
112 
grpc_server_credentials_set_auth_metadata_processor(grpc_server_credentials * creds,grpc_auth_metadata_processor processor)113 void grpc_server_credentials_set_auth_metadata_processor(
114     grpc_server_credentials* creds, grpc_auth_metadata_processor processor) {
115   DCHECK_NE(creds, nullptr);
116   creds->set_auth_metadata_processor(processor);
117 }
118 
server_credentials_pointer_arg_destroy(void * p)119 static void server_credentials_pointer_arg_destroy(void* p) {
120   static_cast<grpc_server_credentials*>(p)->Unref();
121 }
122 
server_credentials_pointer_arg_copy(void * p)123 static void* server_credentials_pointer_arg_copy(void* p) {
124   return static_cast<grpc_server_credentials*>(p)->Ref().release();
125 }
126 
server_credentials_pointer_cmp(void * a,void * b)127 static int server_credentials_pointer_cmp(void* a, void* b) {
128   return grpc_core::QsortCompare(a, b);
129 }
130 
131 static const grpc_arg_pointer_vtable cred_ptr_vtable = {
132     server_credentials_pointer_arg_copy, server_credentials_pointer_arg_destroy,
133     server_credentials_pointer_cmp};
134 
grpc_server_credentials_to_arg(grpc_server_credentials * c)135 grpc_arg grpc_server_credentials_to_arg(grpc_server_credentials* c) {
136   return grpc_channel_arg_pointer_create(
137       const_cast<char*>(GRPC_SERVER_CREDENTIALS_ARG), c, &cred_ptr_vtable);
138 }
139 
grpc_server_credentials_from_arg(const grpc_arg * arg)140 grpc_server_credentials* grpc_server_credentials_from_arg(const grpc_arg* arg) {
141   if (strcmp(arg->key, GRPC_SERVER_CREDENTIALS_ARG) != 0) return nullptr;
142   if (arg->type != GRPC_ARG_POINTER) {
143     LOG(ERROR) << "Invalid type " << arg->type << " for arg "
144                << GRPC_SERVER_CREDENTIALS_ARG;
145     return nullptr;
146   }
147   return static_cast<grpc_server_credentials*>(arg->value.pointer.p);
148 }
149 
grpc_find_server_credentials_in_args(const grpc_channel_args * args)150 grpc_server_credentials* grpc_find_server_credentials_in_args(
151     const grpc_channel_args* args) {
152   size_t i;
153   if (args == nullptr) return nullptr;
154   for (i = 0; i < args->num_args; i++) {
155     grpc_server_credentials* p =
156         grpc_server_credentials_from_arg(&args->args[i]);
157     if (p != nullptr) return p;
158   }
159   return nullptr;
160 }
161