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