Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / lib / security / context / security_context.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/context/security_context.h"
22
23 #include <string.h>
24
25 #include <grpc/grpc_security.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/gprpp/arena.h"
33 #include "src/core/lib/gprpp/ref_counted.h"
34 #include "src/core/lib/gprpp/ref_counted_ptr.h"
35 #include "src/core/lib/surface/api_trace.h"
36 #include "src/core/lib/surface/call.h"
37
38 grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount(
39     false, "auth_context_refcount");
40
41 /* --- grpc_call --- */
42
43 grpc_call_error grpc_call_set_credentials(grpc_call* call,
44                                           grpc_call_credentials* creds) {
45   grpc_core::ExecCtx exec_ctx;
46   grpc_client_security_context* ctx = nullptr;
47   GRPC_API_TRACE("grpc_call_set_credentials(call=%p, creds=%p)", 2,
48                  (call, creds));
49   if (!grpc_call_is_client(call)) {
50     gpr_log(GPR_ERROR, "Method is client-side only.");
51     return GRPC_CALL_ERROR_NOT_ON_SERVER;
52   }
53   ctx = static_cast<grpc_client_security_context*>(
54       grpc_call_context_get(call, GRPC_CONTEXT_SECURITY));
55   if (ctx == nullptr) {
56     ctx = grpc_client_security_context_create(grpc_call_get_arena(call), creds);
57     grpc_call_context_set(call, GRPC_CONTEXT_SECURITY, ctx,
58                           grpc_client_security_context_destroy);
59   } else {
60     ctx->creds = creds != nullptr ? creds->Ref() : nullptr;
61   }
62
63   return GRPC_CALL_OK;
64 }
65
66 grpc_auth_context* grpc_call_auth_context(grpc_call* call) {
67   void* sec_ctx = grpc_call_context_get(call, GRPC_CONTEXT_SECURITY);
68   GRPC_API_TRACE("grpc_call_auth_context(call=%p)", 1, (call));
69   if (sec_ctx == nullptr) return nullptr;
70   if (grpc_call_is_client(call)) {
71     auto* sc = static_cast<grpc_client_security_context*>(sec_ctx);
72     if (sc->auth_context == nullptr) {
73       return nullptr;
74     } else {
75       return sc->auth_context
76           ->Ref(DEBUG_LOCATION, "grpc_call_auth_context client")
77           .release();
78     }
79   } else {
80     auto* sc = static_cast<grpc_server_security_context*>(sec_ctx);
81     if (sc->auth_context == nullptr) {
82       return nullptr;
83     } else {
84       return sc->auth_context
85           ->Ref(DEBUG_LOCATION, "grpc_call_auth_context server")
86           .release();
87     }
88   }
89 }
90
91 void grpc_auth_context_release(grpc_auth_context* context) {
92   GRPC_API_TRACE("grpc_auth_context_release(context=%p)", 1, (context));
93   if (context == nullptr) return;
94   context->Unref(DEBUG_LOCATION, "grpc_auth_context_unref");
95 }
96
97 /* --- grpc_client_security_context --- */
98 grpc_client_security_context::~grpc_client_security_context() {
99   auth_context.reset(DEBUG_LOCATION, "client_security_context");
100   if (extension.instance != nullptr && extension.destroy != nullptr) {
101     extension.destroy(extension.instance);
102   }
103 }
104
105 grpc_client_security_context* grpc_client_security_context_create(
106     grpc_core::Arena* arena, grpc_call_credentials* creds) {
107   return arena->New<grpc_client_security_context>(
108       creds != nullptr ? creds->Ref() : nullptr);
109 }
110
111 void grpc_client_security_context_destroy(void* ctx) {
112   grpc_core::ExecCtx exec_ctx;
113   grpc_client_security_context* c =
114       static_cast<grpc_client_security_context*>(ctx);
115   c->~grpc_client_security_context();
116 }
117
118 /* --- grpc_server_security_context --- */
119 grpc_server_security_context::~grpc_server_security_context() {
120   auth_context.reset(DEBUG_LOCATION, "server_security_context");
121   if (extension.instance != nullptr && extension.destroy != nullptr) {
122     extension.destroy(extension.instance);
123   }
124 }
125
126 grpc_server_security_context* grpc_server_security_context_create(
127     grpc_core::Arena* arena) {
128   return arena->New<grpc_server_security_context>();
129 }
130
131 void grpc_server_security_context_destroy(void* ctx) {
132   grpc_server_security_context* c =
133       static_cast<grpc_server_security_context*>(ctx);
134   c->~grpc_server_security_context();
135 }
136
137 /* --- grpc_auth_context --- */
138
139 static grpc_auth_property_iterator empty_iterator = {nullptr, 0, nullptr};
140
141 const char* grpc_auth_context_peer_identity_property_name(
142     const grpc_auth_context* ctx) {
143   GRPC_API_TRACE("grpc_auth_context_peer_identity_property_name(ctx=%p)", 1,
144                  (ctx));
145   return ctx->peer_identity_property_name();
146 }
147
148 int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context* ctx,
149                                                       const char* name) {
150   grpc_auth_property_iterator it =
151       grpc_auth_context_find_properties_by_name(ctx, name);
152   const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it);
153   GRPC_API_TRACE(
154       "grpc_auth_context_set_peer_identity_property_name(ctx=%p, name=%s)", 2,
155       (ctx, name));
156   if (prop == nullptr) {
157     gpr_log(GPR_ERROR, "Property name %s not found in auth context.",
158             name != nullptr ? name : "NULL");
159     return 0;
160   }
161   ctx->set_peer_identity_property_name(prop->name);
162   return 1;
163 }
164
165 int grpc_auth_context_peer_is_authenticated(const grpc_auth_context* ctx) {
166   GRPC_API_TRACE("grpc_auth_context_peer_is_authenticated(ctx=%p)", 1, (ctx));
167   return ctx->is_authenticated();
168 }
169
170 grpc_auth_property_iterator grpc_auth_context_property_iterator(
171     const grpc_auth_context* ctx) {
172   grpc_auth_property_iterator it = empty_iterator;
173   GRPC_API_TRACE("grpc_auth_context_property_iterator(ctx=%p)", 1, (ctx));
174   if (ctx == nullptr) return it;
175   it.ctx = ctx;
176   return it;
177 }
178
179 const grpc_auth_property* grpc_auth_property_iterator_next(
180     grpc_auth_property_iterator* it) {
181   GRPC_API_TRACE("grpc_auth_property_iterator_next(it=%p)", 1, (it));
182   if (it == nullptr || it->ctx == nullptr) return nullptr;
183   while (it->index == it->ctx->properties().count) {
184     if (it->ctx->chained() == nullptr) return nullptr;
185     it->ctx = it->ctx->chained();
186     it->index = 0;
187   }
188   if (it->name == nullptr) {
189     return &it->ctx->properties().array[it->index++];
190   } else {
191     while (it->index < it->ctx->properties().count) {
192       const grpc_auth_property* prop =
193           &it->ctx->properties().array[it->index++];
194       GPR_ASSERT(prop->name != nullptr);
195       if (strcmp(it->name, prop->name) == 0) {
196         return prop;
197       }
198     }
199     /* We could not find the name, try another round. */
200     return grpc_auth_property_iterator_next(it);
201   }
202 }
203
204 grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(
205     const grpc_auth_context* ctx, const char* name) {
206   grpc_auth_property_iterator it = empty_iterator;
207   GRPC_API_TRACE("grpc_auth_context_find_properties_by_name(ctx=%p, name=%s)",
208                  2, (ctx, name));
209   if (ctx == nullptr || name == nullptr) return empty_iterator;
210   it.ctx = ctx;
211   it.name = name;
212   return it;
213 }
214
215 grpc_auth_property_iterator grpc_auth_context_peer_identity(
216     const grpc_auth_context* ctx) {
217   GRPC_API_TRACE("grpc_auth_context_peer_identity(ctx=%p)", 1, (ctx));
218   if (ctx == nullptr) return empty_iterator;
219   return grpc_auth_context_find_properties_by_name(
220       ctx, ctx->peer_identity_property_name());
221 }
222
223 void grpc_auth_context::ensure_capacity() {
224   if (properties_.count == properties_.capacity) {
225     properties_.capacity =
226         GPR_MAX(properties_.capacity + 8, properties_.capacity * 2);
227     properties_.array = static_cast<grpc_auth_property*>(gpr_realloc(
228         properties_.array, properties_.capacity * sizeof(grpc_auth_property)));
229   }
230 }
231
232 void grpc_auth_context::add_property(const char* name, const char* value,
233                                      size_t value_length) {
234   ensure_capacity();
235   grpc_auth_property* prop = &properties_.array[properties_.count++];
236   prop->name = gpr_strdup(name);
237   prop->value = static_cast<char*>(gpr_malloc(value_length + 1));
238   memcpy(prop->value, value, value_length);
239   prop->value[value_length] = '\0';
240   prop->value_length = value_length;
241 }
242
243 void grpc_auth_context_add_property(grpc_auth_context* ctx, const char* name,
244                                     const char* value, size_t value_length) {
245   GRPC_API_TRACE(
246       "grpc_auth_context_add_property(ctx=%p, name=%s, value=%*.*s, "
247       "value_length=%lu)",
248       6,
249       (ctx, name, (int)value_length, (int)value_length, value,
250        (unsigned long)value_length));
251   ctx->add_property(name, value, value_length);
252 }
253
254 void grpc_auth_context::add_cstring_property(const char* name,
255                                              const char* value) {
256   ensure_capacity();
257   grpc_auth_property* prop = &properties_.array[properties_.count++];
258   prop->name = gpr_strdup(name);
259   prop->value = gpr_strdup(value);
260   prop->value_length = strlen(value);
261 }
262
263 void grpc_auth_context_add_cstring_property(grpc_auth_context* ctx,
264                                             const char* name,
265                                             const char* value) {
266   GRPC_API_TRACE(
267       "grpc_auth_context_add_cstring_property(ctx=%p, name=%s, value=%s)", 3,
268       (ctx, name, value));
269   ctx->add_cstring_property(name, value);
270 }
271
272 void grpc_auth_property_reset(grpc_auth_property* property) {
273   gpr_free(property->name);
274   gpr_free(property->value);
275   memset(property, 0, sizeof(grpc_auth_property));
276 }
277
278 static void auth_context_pointer_arg_destroy(void* p) {
279   if (p != nullptr) {
280     static_cast<grpc_auth_context*>(p)->Unref(DEBUG_LOCATION,
281                                               "auth_context_pointer_arg");
282   }
283 }
284
285 static void* auth_context_pointer_arg_copy(void* p) {
286   auto* ctx = static_cast<grpc_auth_context*>(p);
287   return ctx == nullptr
288              ? nullptr
289              : ctx->Ref(DEBUG_LOCATION, "auth_context_pointer_arg").release();
290 }
291
292 static int auth_context_pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
293
294 static const grpc_arg_pointer_vtable auth_context_pointer_vtable = {
295     auth_context_pointer_arg_copy, auth_context_pointer_arg_destroy,
296     auth_context_pointer_cmp};
297
298 grpc_arg grpc_auth_context_to_arg(grpc_auth_context* c) {
299   return grpc_channel_arg_pointer_create(
300       const_cast<char*>(GRPC_AUTH_CONTEXT_ARG), c,
301       &auth_context_pointer_vtable);
302 }
303
304 grpc_auth_context* grpc_auth_context_from_arg(const grpc_arg* arg) {
305   if (strcmp(arg->key, GRPC_AUTH_CONTEXT_ARG) != 0) return nullptr;
306   if (arg->type != GRPC_ARG_POINTER) {
307     gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
308             GRPC_AUTH_CONTEXT_ARG);
309     return nullptr;
310   }
311   return static_cast<grpc_auth_context*>(arg->value.pointer.p);
312 }
313
314 grpc_auth_context* grpc_find_auth_context_in_args(
315     const grpc_channel_args* args) {
316   size_t i;
317   if (args == nullptr) return nullptr;
318   for (i = 0; i < args->num_args; i++) {
319     grpc_auth_context* p = grpc_auth_context_from_arg(&args->args[i]);
320     if (p != nullptr) return p;
321   }
322   return nullptr;
323 }