3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/security/context/security_context.h"
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>
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"
38 grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount(
39 false, "auth_context_refcount");
41 /* --- grpc_call --- */
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,
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;
53 ctx = static_cast<grpc_client_security_context*>(
54 grpc_call_context_get(call, GRPC_CONTEXT_SECURITY));
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);
60 ctx->creds = creds != nullptr ? creds->Ref() : nullptr;
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) {
75 return sc->auth_context
76 ->Ref(DEBUG_LOCATION, "grpc_call_auth_context client")
80 auto* sc = static_cast<grpc_server_security_context*>(sec_ctx);
81 if (sc->auth_context == nullptr) {
84 return sc->auth_context
85 ->Ref(DEBUG_LOCATION, "grpc_call_auth_context server")
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");
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);
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);
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();
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);
126 grpc_server_security_context* grpc_server_security_context_create(
127 grpc_core::Arena* arena) {
128 return arena->New<grpc_server_security_context>();
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();
137 /* --- grpc_auth_context --- */
139 static grpc_auth_property_iterator empty_iterator = {nullptr, 0, nullptr};
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,
145 return ctx->peer_identity_property_name();
148 int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context* ctx,
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);
154 "grpc_auth_context_set_peer_identity_property_name(ctx=%p, name=%s)", 2,
156 if (prop == nullptr) {
157 gpr_log(GPR_ERROR, "Property name %s not found in auth context.",
158 name != nullptr ? name : "NULL");
161 ctx->set_peer_identity_property_name(prop->name);
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();
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;
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();
188 if (it->name == nullptr) {
189 return &it->ctx->properties().array[it->index++];
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) {
199 /* We could not find the name, try another round. */
200 return grpc_auth_property_iterator_next(it);
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)",
209 if (ctx == nullptr || name == nullptr) return empty_iterator;
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());
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)));
232 void grpc_auth_context::add_property(const char* name, const char* value,
233 size_t value_length) {
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;
243 void grpc_auth_context_add_property(grpc_auth_context* ctx, const char* name,
244 const char* value, size_t value_length) {
246 "grpc_auth_context_add_property(ctx=%p, name=%s, value=%*.*s, "
249 (ctx, name, (int)value_length, (int)value_length, value,
250 (unsigned long)value_length));
251 ctx->add_property(name, value, value_length);
254 void grpc_auth_context::add_cstring_property(const char* name,
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);
263 void grpc_auth_context_add_cstring_property(grpc_auth_context* ctx,
267 "grpc_auth_context_add_cstring_property(ctx=%p, name=%s, value=%s)", 3,
269 ctx->add_cstring_property(name, value);
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));
278 static void auth_context_pointer_arg_destroy(void* p) {
280 static_cast<grpc_auth_context*>(p)->Unref(DEBUG_LOCATION,
281 "auth_context_pointer_arg");
285 static void* auth_context_pointer_arg_copy(void* p) {
286 auto* ctx = static_cast<grpc_auth_context*>(p);
287 return ctx == nullptr
289 : ctx->Ref(DEBUG_LOCATION, "auth_context_pointer_arg").release();
292 static int auth_context_pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
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};
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);
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);
311 return static_cast<grpc_auth_context*>(arg->value.pointer.p);
314 grpc_auth_context* grpc_find_auth_context_in_args(
315 const grpc_channel_args* args) {
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;