Imported Upstream version 1.18.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 <string.h>
22
23 #include "src/core/lib/channel/channel_args.h"
24 #include "src/core/lib/gpr/arena.h"
25 #include "src/core/lib/gpr/string.h"
26 #include "src/core/lib/gprpp/ref_counted.h"
27 #include "src/core/lib/gprpp/ref_counted_ptr.h"
28 #include "src/core/lib/security/context/security_context.h"
29 #include "src/core/lib/surface/api_trace.h"
30 #include "src/core/lib/surface/call.h"
31
32 #include <grpc/grpc_security.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35 #include <grpc/support/string_util.h>
36
37 grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount(
38     false, "auth_context_refcount");
39
40 /* --- grpc_call --- */
41
42 grpc_call_error grpc_call_set_credentials(grpc_call* call,
43                                           grpc_call_credentials* creds) {
44   grpc_core::ExecCtx exec_ctx;
45   grpc_client_security_context* ctx = nullptr;
46   GRPC_API_TRACE("grpc_call_set_credentials(call=%p, creds=%p)", 2,
47                  (call, creds));
48   if (!grpc_call_is_client(call)) {
49     gpr_log(GPR_ERROR, "Method is client-side only.");
50     return GRPC_CALL_ERROR_NOT_ON_SERVER;
51   }
52   ctx = static_cast<grpc_client_security_context*>(
53       grpc_call_context_get(call, GRPC_CONTEXT_SECURITY));
54   if (ctx == nullptr) {
55     ctx = grpc_client_security_context_create(grpc_call_get_arena(call), creds);
56     grpc_call_context_set(call, GRPC_CONTEXT_SECURITY, ctx,
57                           grpc_client_security_context_destroy);
58   } else {
59     ctx->creds = creds != nullptr ? creds->Ref() : nullptr;
60   }
61
62   return GRPC_CALL_OK;
63 }
64
65 grpc_auth_context* grpc_call_auth_context(grpc_call* call) {
66   void* sec_ctx = grpc_call_context_get(call, GRPC_CONTEXT_SECURITY);
67   GRPC_API_TRACE("grpc_call_auth_context(call=%p)", 1, (call));
68   if (sec_ctx == nullptr) return nullptr;
69   if (grpc_call_is_client(call)) {
70     auto* sc = static_cast<grpc_client_security_context*>(sec_ctx);
71     if (sc->auth_context == nullptr) {
72       return nullptr;
73     } else {
74       return sc->auth_context
75           ->Ref(DEBUG_LOCATION, "grpc_call_auth_context client")
76           .release();
77     }
78   } else {
79     auto* sc = static_cast<grpc_server_security_context*>(sec_ctx);
80     if (sc->auth_context == nullptr) {
81       return nullptr;
82     } else {
83       return sc->auth_context
84           ->Ref(DEBUG_LOCATION, "grpc_call_auth_context server")
85           .release();
86     }
87   }
88 }
89
90 void grpc_auth_context_release(grpc_auth_context* context) {
91   GRPC_API_TRACE("grpc_auth_context_release(context=%p)", 1, (context));
92   if (context == nullptr) return;
93   context->Unref(DEBUG_LOCATION, "grpc_auth_context_unref");
94 }
95
96 /* --- grpc_client_security_context --- */
97 grpc_client_security_context::~grpc_client_security_context() {
98   auth_context.reset(DEBUG_LOCATION, "client_security_context");
99   if (extension.instance != nullptr && extension.destroy != nullptr) {
100     extension.destroy(extension.instance);
101   }
102 }
103
104 grpc_client_security_context* grpc_client_security_context_create(
105     gpr_arena* arena, grpc_call_credentials* creds) {
106   return new (gpr_arena_alloc(arena, sizeof(grpc_client_security_context)))
107       grpc_client_security_context(creds != nullptr ? creds->Ref() : nullptr);
108 }
109
110 void grpc_client_security_context_destroy(void* ctx) {
111   grpc_core::ExecCtx exec_ctx;
112   grpc_client_security_context* c =
113       static_cast<grpc_client_security_context*>(ctx);
114   c->~grpc_client_security_context();
115 }
116
117 /* --- grpc_server_security_context --- */
118 grpc_server_security_context::~grpc_server_security_context() {
119   auth_context.reset(DEBUG_LOCATION, "server_security_context");
120   if (extension.instance != nullptr && extension.destroy != nullptr) {
121     extension.destroy(extension.instance);
122   }
123 }
124
125 grpc_server_security_context* grpc_server_security_context_create(
126     gpr_arena* arena) {
127   return new (gpr_arena_alloc(arena, sizeof(grpc_server_security_context)))
128       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* p) {
299   return grpc_channel_arg_pointer_create((char*)GRPC_AUTH_CONTEXT_ARG, p,
300                                          &auth_context_pointer_vtable);
301 }
302
303 grpc_auth_context* grpc_auth_context_from_arg(const grpc_arg* arg) {
304   if (strcmp(arg->key, GRPC_AUTH_CONTEXT_ARG) != 0) return nullptr;
305   if (arg->type != GRPC_ARG_POINTER) {
306     gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
307             GRPC_AUTH_CONTEXT_ARG);
308     return nullptr;
309   }
310   return static_cast<grpc_auth_context*>(arg->value.pointer.p);
311 }
312
313 grpc_auth_context* grpc_find_auth_context_in_args(
314     const grpc_channel_args* args) {
315   size_t i;
316   if (args == nullptr) return nullptr;
317   for (i = 0; i < args->num_args; i++) {
318     grpc_auth_context* p = grpc_auth_context_from_arg(&args->args[i]);
319     if (p != nullptr) return p;
320   }
321   return nullptr;
322 }