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>
23 #include "src/core/lib/channel/channel_args.h"
24 #include "src/core/lib/gpr/string.h"
25 #include "src/core/lib/gprpp/arena.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"
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>
37 grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount(
38 false, "auth_context_refcount");
40 /* --- grpc_call --- */
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,
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;
52 ctx = static_cast<grpc_client_security_context*>(
53 grpc_call_context_get(call, GRPC_CONTEXT_SECURITY));
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);
59 ctx->creds = creds != nullptr ? creds->Ref() : nullptr;
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) {
74 return sc->auth_context
75 ->Ref(DEBUG_LOCATION, "grpc_call_auth_context client")
79 auto* sc = static_cast<grpc_server_security_context*>(sec_ctx);
80 if (sc->auth_context == nullptr) {
83 return sc->auth_context
84 ->Ref(DEBUG_LOCATION, "grpc_call_auth_context server")
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");
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);
104 grpc_client_security_context* grpc_client_security_context_create(
105 grpc_core::Arena* arena, grpc_call_credentials* creds) {
106 return arena->New<grpc_client_security_context>(
107 creds != nullptr ? creds->Ref() : nullptr);
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();
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);
125 grpc_server_security_context* grpc_server_security_context_create(
126 grpc_core::Arena* arena) {
127 return arena->New<grpc_server_security_context>();
130 void grpc_server_security_context_destroy(void* ctx) {
131 grpc_server_security_context* c =
132 static_cast<grpc_server_security_context*>(ctx);
133 c->~grpc_server_security_context();
136 /* --- grpc_auth_context --- */
138 static grpc_auth_property_iterator empty_iterator = {nullptr, 0, nullptr};
140 const char* grpc_auth_context_peer_identity_property_name(
141 const grpc_auth_context* ctx) {
142 GRPC_API_TRACE("grpc_auth_context_peer_identity_property_name(ctx=%p)", 1,
144 return ctx->peer_identity_property_name();
147 int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context* ctx,
149 grpc_auth_property_iterator it =
150 grpc_auth_context_find_properties_by_name(ctx, name);
151 const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it);
153 "grpc_auth_context_set_peer_identity_property_name(ctx=%p, name=%s)", 2,
155 if (prop == nullptr) {
156 gpr_log(GPR_ERROR, "Property name %s not found in auth context.",
157 name != nullptr ? name : "NULL");
160 ctx->set_peer_identity_property_name(prop->name);
164 int grpc_auth_context_peer_is_authenticated(const grpc_auth_context* ctx) {
165 GRPC_API_TRACE("grpc_auth_context_peer_is_authenticated(ctx=%p)", 1, (ctx));
166 return ctx->is_authenticated();
169 grpc_auth_property_iterator grpc_auth_context_property_iterator(
170 const grpc_auth_context* ctx) {
171 grpc_auth_property_iterator it = empty_iterator;
172 GRPC_API_TRACE("grpc_auth_context_property_iterator(ctx=%p)", 1, (ctx));
173 if (ctx == nullptr) return it;
178 const grpc_auth_property* grpc_auth_property_iterator_next(
179 grpc_auth_property_iterator* it) {
180 GRPC_API_TRACE("grpc_auth_property_iterator_next(it=%p)", 1, (it));
181 if (it == nullptr || it->ctx == nullptr) return nullptr;
182 while (it->index == it->ctx->properties().count) {
183 if (it->ctx->chained() == nullptr) return nullptr;
184 it->ctx = it->ctx->chained();
187 if (it->name == nullptr) {
188 return &it->ctx->properties().array[it->index++];
190 while (it->index < it->ctx->properties().count) {
191 const grpc_auth_property* prop =
192 &it->ctx->properties().array[it->index++];
193 GPR_ASSERT(prop->name != nullptr);
194 if (strcmp(it->name, prop->name) == 0) {
198 /* We could not find the name, try another round. */
199 return grpc_auth_property_iterator_next(it);
203 grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(
204 const grpc_auth_context* ctx, const char* name) {
205 grpc_auth_property_iterator it = empty_iterator;
206 GRPC_API_TRACE("grpc_auth_context_find_properties_by_name(ctx=%p, name=%s)",
208 if (ctx == nullptr || name == nullptr) return empty_iterator;
214 grpc_auth_property_iterator grpc_auth_context_peer_identity(
215 const grpc_auth_context* ctx) {
216 GRPC_API_TRACE("grpc_auth_context_peer_identity(ctx=%p)", 1, (ctx));
217 if (ctx == nullptr) return empty_iterator;
218 return grpc_auth_context_find_properties_by_name(
219 ctx, ctx->peer_identity_property_name());
222 void grpc_auth_context::ensure_capacity() {
223 if (properties_.count == properties_.capacity) {
224 properties_.capacity =
225 GPR_MAX(properties_.capacity + 8, properties_.capacity * 2);
226 properties_.array = static_cast<grpc_auth_property*>(gpr_realloc(
227 properties_.array, properties_.capacity * sizeof(grpc_auth_property)));
231 void grpc_auth_context::add_property(const char* name, const char* value,
232 size_t value_length) {
234 grpc_auth_property* prop = &properties_.array[properties_.count++];
235 prop->name = gpr_strdup(name);
236 prop->value = static_cast<char*>(gpr_malloc(value_length + 1));
237 memcpy(prop->value, value, value_length);
238 prop->value[value_length] = '\0';
239 prop->value_length = value_length;
242 void grpc_auth_context_add_property(grpc_auth_context* ctx, const char* name,
243 const char* value, size_t value_length) {
245 "grpc_auth_context_add_property(ctx=%p, name=%s, value=%*.*s, "
248 (ctx, name, (int)value_length, (int)value_length, value,
249 (unsigned long)value_length));
250 ctx->add_property(name, value, value_length);
253 void grpc_auth_context::add_cstring_property(const char* name,
256 grpc_auth_property* prop = &properties_.array[properties_.count++];
257 prop->name = gpr_strdup(name);
258 prop->value = gpr_strdup(value);
259 prop->value_length = strlen(value);
262 void grpc_auth_context_add_cstring_property(grpc_auth_context* ctx,
266 "grpc_auth_context_add_cstring_property(ctx=%p, name=%s, value=%s)", 3,
268 ctx->add_cstring_property(name, value);
271 void grpc_auth_property_reset(grpc_auth_property* property) {
272 gpr_free(property->name);
273 gpr_free(property->value);
274 memset(property, 0, sizeof(grpc_auth_property));
277 static void auth_context_pointer_arg_destroy(void* p) {
279 static_cast<grpc_auth_context*>(p)->Unref(DEBUG_LOCATION,
280 "auth_context_pointer_arg");
284 static void* auth_context_pointer_arg_copy(void* p) {
285 auto* ctx = static_cast<grpc_auth_context*>(p);
286 return ctx == nullptr
288 : ctx->Ref(DEBUG_LOCATION, "auth_context_pointer_arg").release();
291 static int auth_context_pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
293 static const grpc_arg_pointer_vtable auth_context_pointer_vtable = {
294 auth_context_pointer_arg_copy, auth_context_pointer_arg_destroy,
295 auth_context_pointer_cmp};
297 grpc_arg grpc_auth_context_to_arg(grpc_auth_context* p) {
298 return grpc_channel_arg_pointer_create((char*)GRPC_AUTH_CONTEXT_ARG, p,
299 &auth_context_pointer_vtable);
302 grpc_auth_context* grpc_auth_context_from_arg(const grpc_arg* arg) {
303 if (strcmp(arg->key, GRPC_AUTH_CONTEXT_ARG) != 0) return nullptr;
304 if (arg->type != GRPC_ARG_POINTER) {
305 gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
306 GRPC_AUTH_CONTEXT_ARG);
309 return static_cast<grpc_auth_context*>(arg->value.pointer.p);
312 grpc_auth_context* grpc_find_auth_context_in_args(
313 const grpc_channel_args* args) {
315 if (args == nullptr) return nullptr;
316 for (i = 0; i < args->num_args; i++) {
317 grpc_auth_context* p = grpc_auth_context_from_arg(&args->args[i]);
318 if (p != nullptr) return p;