blob: a365002a74e029ae4df2b566ce7ba38f845d39de [file] [log] [blame]
Julien Boeuf9f218dd2015-04-23 10:24:02 -07001/*
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
34#include <string.h>
35
36#include "src/core/security/context.h"
37#include "src/core/surface/call.h"
38
39#include <grpc/grpc_security.h>
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42
43grpc_call_error grpc_call_set_credentials(grpc_call *call,
44 grpc_credentials *creds) {
45 grpc_client_security_context *ctx = NULL;
46 if (!grpc_call_is_client(call)) {
47 gpr_log(GPR_ERROR, "Method is client-side only.");
48 return GRPC_CALL_ERROR_NOT_ON_SERVER;
49 }
50 if (creds != NULL && !grpc_credentials_has_request_metadata_only(creds)) {
51 gpr_log(GPR_ERROR, "Incompatible credentials to set on a call.");
52 return GRPC_CALL_ERROR;
53 }
54 ctx = (grpc_client_security_context *)grpc_call_context_get(
55 call, GRPC_CONTEXT_SECURITY);
56 if (ctx == NULL) {
57 ctx = grpc_client_security_context_create();
58 ctx->creds = grpc_credentials_ref(creds);
59 grpc_call_context_set(call, GRPC_CONTEXT_SECURITY, ctx,
60 grpc_client_security_context_destroy);
61 } else {
62 grpc_credentials_unref(ctx->creds);
63 ctx->creds = grpc_credentials_ref(creds);
64 }
65 return GRPC_CALL_OK;
66}
67
68grpc_client_security_context *grpc_client_security_context_create(void) {
69 grpc_client_security_context *ctx =
70 gpr_malloc(sizeof(grpc_client_security_context));
71 memset(ctx, 0, sizeof(grpc_client_security_context));
72 return ctx;
73}
74
75void grpc_client_security_context_destroy(void *ctx) {
76 grpc_client_security_context *c = (grpc_client_security_context *)ctx;
77 grpc_credentials_unref(c->creds);
78 gpr_free(ctx);
79}