• 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 <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/security/credentials/credentials.h"
22 
23 #include <stdint.h>
24 #include <string.h>
25 
26 #include <grpc/support/log.h>
27 
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/debug/trace.h"
30 #include "src/core/lib/gpr/useful.h"
31 #include "src/core/lib/gprpp/crash.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/surface/api_trace.h"
34 
35 // -- Common. --
36 
grpc_channel_credentials_release(grpc_channel_credentials * creds)37 void grpc_channel_credentials_release(grpc_channel_credentials* creds) {
38   GRPC_API_TRACE("grpc_channel_credentials_release(creds=%p)", 1, (creds));
39   grpc_core::ExecCtx exec_ctx;
40   if (creds) creds->Unref();
41 }
42 
grpc_call_credentials_release(grpc_call_credentials * creds)43 void grpc_call_credentials_release(grpc_call_credentials* creds) {
44   GRPC_API_TRACE("grpc_call_credentials_release(creds=%p)", 1, (creds));
45   grpc_core::ExecCtx exec_ctx;
46   if (creds) creds->Unref();
47 }
48 
credentials_pointer_arg_destroy(void * p)49 static void credentials_pointer_arg_destroy(void* p) {
50   static_cast<grpc_channel_credentials*>(p)->Unref();
51 }
52 
credentials_pointer_arg_copy(void * p)53 static void* credentials_pointer_arg_copy(void* p) {
54   return static_cast<grpc_channel_credentials*>(p)->Ref().release();
55 }
56 
credentials_pointer_cmp(void * a,void * b)57 static int credentials_pointer_cmp(void* a, void* b) {
58   return static_cast<const grpc_channel_credentials*>(a)->cmp(
59       static_cast<const grpc_channel_credentials*>(b));
60 }
61 
62 static const grpc_arg_pointer_vtable credentials_pointer_vtable = {
63     credentials_pointer_arg_copy, credentials_pointer_arg_destroy,
64     credentials_pointer_cmp};
65 
grpc_channel_credentials_to_arg(grpc_channel_credentials * credentials)66 grpc_arg grpc_channel_credentials_to_arg(
67     grpc_channel_credentials* credentials) {
68   return grpc_channel_arg_pointer_create(
69       const_cast<char*>(GRPC_ARG_CHANNEL_CREDENTIALS), credentials,
70       &credentials_pointer_vtable);
71 }
72 
grpc_channel_credentials_from_arg(const grpc_arg * arg)73 grpc_channel_credentials* grpc_channel_credentials_from_arg(
74     const grpc_arg* arg) {
75   if (strcmp(arg->key, GRPC_ARG_CHANNEL_CREDENTIALS) != 0) return nullptr;
76   if (arg->type != GRPC_ARG_POINTER) {
77     gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
78             GRPC_ARG_CHANNEL_CREDENTIALS);
79     return nullptr;
80   }
81   return static_cast<grpc_channel_credentials*>(arg->value.pointer.p);
82 }
83 
grpc_channel_credentials_find_in_args(const grpc_channel_args * args)84 grpc_channel_credentials* grpc_channel_credentials_find_in_args(
85     const grpc_channel_args* args) {
86   size_t i;
87   if (args == nullptr) return nullptr;
88   for (i = 0; i < args->num_args; i++) {
89     grpc_channel_credentials* credentials =
90         grpc_channel_credentials_from_arg(&args->args[i]);
91     if (credentials != nullptr) return credentials;
92   }
93   return nullptr;
94 }
95 
grpc_server_credentials_release(grpc_server_credentials * creds)96 void grpc_server_credentials_release(grpc_server_credentials* creds) {
97   GRPC_API_TRACE("grpc_server_credentials_release(creds=%p)", 1, (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_API_TRACE(
105       "grpc_server_credentials_set_auth_metadata_processor("
106       "creds=%p, "
107       "processor=grpc_auth_metadata_processor { process: %p, state: %p })",
108       3, (this, (void*)(intptr_t)processor.process, processor.state));
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   GPR_DEBUG_ASSERT(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     gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
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