Debian Codegraph @

src/core/lib/security/credentials/credentials.cc

raw
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/iomgr/exec_ctx.h"
32 #include "src/core/lib/surface/api_trace.h"
33
34 /* -- Common. -- */
35
36 void grpc_channel_credentials_release(grpc_channel_credentials* creds) {
37 GRPC_API_TRACE("grpc_channel_credentials_release(creds=%p)", 1, (creds));
38 grpc_core::ExecCtx exec_ctx;
39 if (creds) creds->Unref();
40 }
41
42 void grpc_call_credentials_release(grpc_call_credentials* creds) {
43 GRPC_API_TRACE("grpc_call_credentials_release(creds=%p)", 1, (creds));
44 grpc_core::ExecCtx exec_ctx;
45 if (creds) creds->Unref();
46 }
47
48 static void credentials_pointer_arg_destroy(void* p) {
49 static_cast<grpc_channel_credentials*>(p)->Unref();
50 }
51
52 static void* credentials_pointer_arg_copy(void* p) {
53 return static_cast<grpc_channel_credentials*>(p)->Ref().release();
54 }
55
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
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
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 gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
77 GRPC_ARG_CHANNEL_CREDENTIALS);
78 return nullptr;
79 }
80 return static_cast<grpc_channel_credentials*>(arg->value.pointer.p);
81 }
82
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
95 void grpc_server_credentials_release(grpc_server_credentials* creds) {
96 GRPC_API_TRACE("grpc_server_credentials_release(creds=%p)", 1, (creds));
97 grpc_core::ExecCtx exec_ctx;
98 if (creds) creds->Unref();
99 }
100
101 void grpc_server_credentials::set_auth_metadata_processor(
102 const grpc_auth_metadata_processor& processor) {
103 GRPC_API_TRACE(
104 "grpc_server_credentials_set_auth_metadata_processor("
105 "creds=%p, "
106 "processor=grpc_auth_metadata_processor { process: %p, state: %p })",
107 3, (this, (void*)(intptr_t)processor.process, processor.state));
108 DestroyProcessor();
109 processor_ = processor;
110 }
111
112 void grpc_server_credentials_set_auth_metadata_processor(
113 grpc_server_credentials* creds, grpc_auth_metadata_processor processor) {
114 GPR_DEBUG_ASSERT(creds != nullptr);
115 creds->set_auth_metadata_processor(processor);
116 }
117
118 static void server_credentials_pointer_arg_destroy(void* p) {
119 static_cast<grpc_server_credentials*>(p)->Unref();
120 }
121
122 static void* server_credentials_pointer_arg_copy(void* p) {
123 return static_cast<grpc_server_credentials*>(p)->Ref().release();
124 }
125
126 static int server_credentials_pointer_cmp(void* a, void* b) {
127 return grpc_core::QsortCompare(a, b);
128 }
129
130 static const grpc_arg_pointer_vtable cred_ptr_vtable = {
131 server_credentials_pointer_arg_copy, server_credentials_pointer_arg_destroy,
132 server_credentials_pointer_cmp};
133
134 grpc_arg grpc_server_credentials_to_arg(grpc_server_credentials* c) {
135 return grpc_channel_arg_pointer_create(
136 const_cast<char*>(GRPC_SERVER_CREDENTIALS_ARG), c, &cred_ptr_vtable);
137 }
138
139 grpc_server_credentials* grpc_server_credentials_from_arg(const grpc_arg* arg) {
140 if (strcmp(arg->key, GRPC_SERVER_CREDENTIALS_ARG) != 0) return nullptr;
141 if (arg->type != GRPC_ARG_POINTER) {
142 gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
143 GRPC_SERVER_CREDENTIALS_ARG);
144 return nullptr;
145 }
146 return static_cast<grpc_server_credentials*>(arg->value.pointer.p);
147 }
148
149 grpc_server_credentials* grpc_find_server_credentials_in_args(
150 const grpc_channel_args* args) {
151 size_t i;
152 if (args == nullptr) return nullptr;
153 for (i = 0; i < args->num_args; i++) {
154 grpc_server_credentials* p =
155 grpc_server_credentials_from_arg(&args->args[i]);
156 if (p != nullptr) return p;
157 }
158 return nullptr;
159 }