security_context.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <string.h>
  34. #include "src/core/security/security_context.h"
  35. #include "src/core/surface/call.h"
  36. #include "src/core/support/string.h"
  37. #include <grpc/grpc_security.h>
  38. #include <grpc/support/alloc.h>
  39. #include <grpc/support/log.h>
  40. #include <grpc/support/string_util.h>
  41. /* --- grpc_process_auth_metadata_func --- */
  42. static grpc_auth_metadata_processor server_processor = {NULL, NULL};
  43. grpc_auth_metadata_processor grpc_server_get_auth_metadata_processor(void) {
  44. return server_processor;
  45. }
  46. void grpc_server_register_auth_metadata_processor(
  47. grpc_auth_metadata_processor processor) {
  48. server_processor = processor;
  49. }
  50. /* --- grpc_call --- */
  51. grpc_call_error grpc_call_set_credentials(grpc_call *call,
  52. grpc_credentials *creds) {
  53. grpc_client_security_context *ctx = NULL;
  54. if (!grpc_call_is_client(call)) {
  55. gpr_log(GPR_ERROR, "Method is client-side only.");
  56. return GRPC_CALL_ERROR_NOT_ON_SERVER;
  57. }
  58. if (creds != NULL && !grpc_credentials_has_request_metadata_only(creds)) {
  59. gpr_log(GPR_ERROR, "Incompatible credentials to set on a call.");
  60. return GRPC_CALL_ERROR;
  61. }
  62. ctx = (grpc_client_security_context *)grpc_call_context_get(
  63. call, GRPC_CONTEXT_SECURITY);
  64. if (ctx == NULL) {
  65. ctx = grpc_client_security_context_create();
  66. ctx->creds = grpc_credentials_ref(creds);
  67. grpc_call_context_set(call, GRPC_CONTEXT_SECURITY, ctx,
  68. grpc_client_security_context_destroy);
  69. } else {
  70. grpc_credentials_unref(ctx->creds);
  71. ctx->creds = grpc_credentials_ref(creds);
  72. }
  73. return GRPC_CALL_OK;
  74. }
  75. grpc_auth_context *grpc_call_auth_context(grpc_call *call) {
  76. void *sec_ctx = grpc_call_context_get(call, GRPC_CONTEXT_SECURITY);
  77. if (sec_ctx == NULL) return NULL;
  78. return grpc_call_is_client(call)
  79. ? GRPC_AUTH_CONTEXT_REF(
  80. ((grpc_client_security_context *)sec_ctx)->auth_context,
  81. "grpc_call_auth_context client")
  82. : GRPC_AUTH_CONTEXT_REF(
  83. ((grpc_server_security_context *)sec_ctx)->auth_context,
  84. "grpc_call_auth_context server");
  85. }
  86. void grpc_auth_context_release(grpc_auth_context *context) {
  87. GRPC_AUTH_CONTEXT_UNREF(context, "grpc_auth_context_unref");
  88. }
  89. /* --- grpc_client_security_context --- */
  90. grpc_client_security_context *grpc_client_security_context_create(void) {
  91. grpc_client_security_context *ctx =
  92. gpr_malloc(sizeof(grpc_client_security_context));
  93. memset(ctx, 0, sizeof(grpc_client_security_context));
  94. return ctx;
  95. }
  96. void grpc_client_security_context_destroy(void *ctx) {
  97. grpc_client_security_context *c = (grpc_client_security_context *)ctx;
  98. grpc_credentials_unref(c->creds);
  99. GRPC_AUTH_CONTEXT_UNREF(c->auth_context, "client_security_context");
  100. gpr_free(ctx);
  101. }
  102. /* --- grpc_server_security_context --- */
  103. grpc_server_security_context *grpc_server_security_context_create(void) {
  104. grpc_server_security_context *ctx =
  105. gpr_malloc(sizeof(grpc_server_security_context));
  106. memset(ctx, 0, sizeof(grpc_server_security_context));
  107. return ctx;
  108. }
  109. void grpc_server_security_context_destroy(void *ctx) {
  110. grpc_server_security_context *c = (grpc_server_security_context *)ctx;
  111. GRPC_AUTH_CONTEXT_UNREF(c->auth_context, "server_security_context");
  112. gpr_free(ctx);
  113. }
  114. /* --- grpc_auth_context --- */
  115. static grpc_auth_property_iterator empty_iterator = {NULL, 0, NULL};
  116. grpc_auth_context *grpc_auth_context_create(grpc_auth_context *chained) {
  117. grpc_auth_context *ctx = gpr_malloc(sizeof(grpc_auth_context));
  118. memset(ctx, 0, sizeof(grpc_auth_context));
  119. gpr_ref_init(&ctx->refcount, 1);
  120. if (chained != NULL) {
  121. ctx->chained = GRPC_AUTH_CONTEXT_REF(chained, "chained");
  122. ctx->peer_identity_property_name =
  123. ctx->chained->peer_identity_property_name;
  124. }
  125. return ctx;
  126. }
  127. #ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG
  128. grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *ctx,
  129. const char *file, int line,
  130. const char *reason) {
  131. if (ctx == NULL) return NULL;
  132. gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
  133. "AUTH_CONTEXT:%p ref %d -> %d %s", ctx, (int)ctx->refcount.count,
  134. (int)ctx->refcount.count + 1, reason);
  135. #else
  136. grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *ctx) {
  137. if (ctx == NULL) return NULL;
  138. #endif
  139. gpr_ref(&ctx->refcount);
  140. return ctx;
  141. }
  142. #ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG
  143. void grpc_auth_context_unref(grpc_auth_context *ctx, const char *file, int line,
  144. const char *reason) {
  145. if (ctx == NULL) return;
  146. gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
  147. "AUTH_CONTEXT:%p unref %d -> %d %s", ctx, (int)ctx->refcount.count,
  148. (int)ctx->refcount.count - 1, reason);
  149. #else
  150. void grpc_auth_context_unref(grpc_auth_context *ctx) {
  151. if (ctx == NULL) return;
  152. #endif
  153. if (gpr_unref(&ctx->refcount)) {
  154. size_t i;
  155. GRPC_AUTH_CONTEXT_UNREF(ctx->chained, "chained");
  156. if (ctx->properties.array != NULL) {
  157. for (i = 0; i < ctx->properties.count; i++) {
  158. grpc_auth_property_reset(&ctx->properties.array[i]);
  159. }
  160. gpr_free(ctx->properties.array);
  161. }
  162. gpr_free(ctx);
  163. }
  164. }
  165. const char *grpc_auth_context_peer_identity_property_name(
  166. const grpc_auth_context *ctx) {
  167. return ctx->peer_identity_property_name;
  168. }
  169. int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context *ctx,
  170. const char *name) {
  171. grpc_auth_property_iterator it =
  172. grpc_auth_context_find_properties_by_name(ctx, name);
  173. const grpc_auth_property *prop = grpc_auth_property_iterator_next(&it);
  174. if (prop == NULL) {
  175. gpr_log(GPR_ERROR, "Property name %s not found in auth context.",
  176. name != NULL ? name : "NULL");
  177. return 0;
  178. }
  179. ctx->peer_identity_property_name = prop->name;
  180. return 1;
  181. }
  182. int grpc_auth_context_peer_is_authenticated(
  183. const grpc_auth_context *ctx) {
  184. return ctx->peer_identity_property_name == NULL ? 0 : 1;
  185. }
  186. grpc_auth_property_iterator grpc_auth_context_property_iterator(
  187. const grpc_auth_context *ctx) {
  188. grpc_auth_property_iterator it = empty_iterator;
  189. if (ctx == NULL) return it;
  190. it.ctx = ctx;
  191. return it;
  192. }
  193. const grpc_auth_property *grpc_auth_property_iterator_next(
  194. grpc_auth_property_iterator *it) {
  195. if (it == NULL || it->ctx == NULL) return NULL;
  196. while (it->index == it->ctx->properties.count) {
  197. if (it->ctx->chained == NULL) return NULL;
  198. it->ctx = it->ctx->chained;
  199. it->index = 0;
  200. }
  201. if (it->name == NULL) {
  202. return &it->ctx->properties.array[it->index++];
  203. } else {
  204. while (it->index < it->ctx->properties.count) {
  205. const grpc_auth_property *prop = &it->ctx->properties.array[it->index++];
  206. GPR_ASSERT(prop->name != NULL);
  207. if (strcmp(it->name, prop->name) == 0) {
  208. return prop;
  209. }
  210. }
  211. /* We could not find the name, try another round. */
  212. return grpc_auth_property_iterator_next(it);
  213. }
  214. }
  215. grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(
  216. const grpc_auth_context *ctx, const char *name) {
  217. grpc_auth_property_iterator it = empty_iterator;
  218. if (ctx == NULL || name == NULL) return empty_iterator;
  219. it.ctx = ctx;
  220. it.name = name;
  221. return it;
  222. }
  223. grpc_auth_property_iterator grpc_auth_context_peer_identity(
  224. const grpc_auth_context *ctx) {
  225. if (ctx == NULL) return empty_iterator;
  226. return grpc_auth_context_find_properties_by_name(
  227. ctx, ctx->peer_identity_property_name);
  228. }
  229. static void ensure_auth_context_capacity(grpc_auth_context *ctx) {
  230. if (ctx->properties.count == ctx->properties.capacity) {
  231. ctx->properties.capacity =
  232. GPR_MAX(ctx->properties.capacity + 8, ctx->properties.capacity * 2);
  233. ctx->properties.array =
  234. gpr_realloc(ctx->properties.array,
  235. ctx->properties.capacity * sizeof(grpc_auth_property));
  236. }
  237. }
  238. void grpc_auth_context_add_property(grpc_auth_context *ctx, const char *name,
  239. const char *value, size_t value_length) {
  240. grpc_auth_property *prop;
  241. ensure_auth_context_capacity(ctx);
  242. prop = &ctx->properties.array[ctx->properties.count++];
  243. prop->name = gpr_strdup(name);
  244. prop->value = gpr_malloc(value_length + 1);
  245. memcpy(prop->value, value, value_length);
  246. prop->value[value_length] = '\0';
  247. prop->value_length = value_length;
  248. }
  249. void grpc_auth_context_add_cstring_property(grpc_auth_context *ctx,
  250. const char *name,
  251. const char *value) {
  252. grpc_auth_property *prop;
  253. ensure_auth_context_capacity(ctx);
  254. prop = &ctx->properties.array[ctx->properties.count++];
  255. prop->name = gpr_strdup(name);
  256. prop->value = gpr_strdup(value);
  257. prop->value_length = strlen(value);
  258. }
  259. void grpc_auth_property_reset(grpc_auth_property *property) {
  260. gpr_free(property->name);
  261. gpr_free(property->value);
  262. memset(property, 0, sizeof(grpc_auth_property));
  263. }
  264. grpc_arg grpc_auth_metadata_processor_to_arg(grpc_auth_metadata_processor *p) {
  265. grpc_arg arg;
  266. memset(&arg, 0, sizeof(grpc_arg));
  267. arg.type = GRPC_ARG_POINTER;
  268. arg.key = GRPC_AUTH_METADATA_PROCESSOR_ARG;
  269. arg.value.pointer.p = p;
  270. return arg;
  271. }
  272. grpc_auth_metadata_processor *grpc_auth_metadata_processor_from_arg(
  273. const grpc_arg *arg) {
  274. if (strcmp(arg->key, GRPC_AUTH_METADATA_PROCESSOR_ARG) != 0) return NULL;
  275. if (arg->type != GRPC_ARG_POINTER) {
  276. gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
  277. GRPC_AUTH_METADATA_PROCESSOR_ARG);
  278. return NULL;
  279. }
  280. return arg->value.pointer.p;
  281. }
  282. grpc_auth_metadata_processor *grpc_find_auth_metadata_processor_in_args(
  283. const grpc_channel_args *args) {
  284. size_t i;
  285. if (args == NULL) return NULL;
  286. for (i = 0; i < args->num_args; i++) {
  287. grpc_auth_metadata_processor *p =
  288. grpc_auth_metadata_processor_from_arg(&args->args[i]);
  289. if (p != NULL) return p;
  290. }
  291. return NULL;
  292. }