• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/credentials/credentials.h"
22  
23  #include <stdio.h>
24  #include <string.h>
25  
26  #include "src/core/lib/channel/channel_args.h"
27  #include "src/core/lib/gpr/string.h"
28  #include "src/core/lib/http/httpcli.h"
29  #include "src/core/lib/http/parser.h"
30  #include "src/core/lib/iomgr/executor.h"
31  #include "src/core/lib/json/json.h"
32  #include "src/core/lib/surface/api_trace.h"
33  
34  #include <grpc/support/alloc.h>
35  #include <grpc/support/log.h>
36  #include <grpc/support/string_util.h>
37  #include <grpc/support/sync.h>
38  #include <grpc/support/time.h>
39  
40  /* -- Common. -- */
41  
grpc_channel_credentials_release(grpc_channel_credentials * creds)42  void grpc_channel_credentials_release(grpc_channel_credentials* creds) {
43    GRPC_API_TRACE("grpc_channel_credentials_release(creds=%p)", 1, (creds));
44    grpc_core::ExecCtx exec_ctx;
45    if (creds) creds->Unref();
46  }
47  
grpc_call_credentials_release(grpc_call_credentials * creds)48  void grpc_call_credentials_release(grpc_call_credentials* creds) {
49    GRPC_API_TRACE("grpc_call_credentials_release(creds=%p)", 1, (creds));
50    grpc_core::ExecCtx exec_ctx;
51    if (creds) creds->Unref();
52  }
53  
credentials_pointer_arg_destroy(void * p)54  static void credentials_pointer_arg_destroy(void* p) {
55    static_cast<grpc_channel_credentials*>(p)->Unref();
56  }
57  
credentials_pointer_arg_copy(void * p)58  static void* credentials_pointer_arg_copy(void* p) {
59    return static_cast<grpc_channel_credentials*>(p)->Ref().release();
60  }
61  
credentials_pointer_cmp(void * a,void * b)62  static int credentials_pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
63  
64  static const grpc_arg_pointer_vtable credentials_pointer_vtable = {
65      credentials_pointer_arg_copy, credentials_pointer_arg_destroy,
66      credentials_pointer_cmp};
67  
grpc_channel_credentials_to_arg(grpc_channel_credentials * credentials)68  grpc_arg grpc_channel_credentials_to_arg(
69      grpc_channel_credentials* credentials) {
70    return grpc_channel_arg_pointer_create(
71        const_cast<char*>(GRPC_ARG_CHANNEL_CREDENTIALS), credentials,
72        &credentials_pointer_vtable);
73  }
74  
grpc_channel_credentials_from_arg(const grpc_arg * arg)75  grpc_channel_credentials* grpc_channel_credentials_from_arg(
76      const grpc_arg* arg) {
77    if (strcmp(arg->key, GRPC_ARG_CHANNEL_CREDENTIALS) != 0) return nullptr;
78    if (arg->type != GRPC_ARG_POINTER) {
79      gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
80              GRPC_ARG_CHANNEL_CREDENTIALS);
81      return nullptr;
82    }
83    return static_cast<grpc_channel_credentials*>(arg->value.pointer.p);
84  }
85  
grpc_channel_credentials_find_in_args(const grpc_channel_args * args)86  grpc_channel_credentials* grpc_channel_credentials_find_in_args(
87      const grpc_channel_args* args) {
88    size_t i;
89    if (args == nullptr) return nullptr;
90    for (i = 0; i < args->num_args; i++) {
91      grpc_channel_credentials* credentials =
92          grpc_channel_credentials_from_arg(&args->args[i]);
93      if (credentials != nullptr) return credentials;
94    }
95    return nullptr;
96  }
97  
grpc_server_credentials_release(grpc_server_credentials * creds)98  void grpc_server_credentials_release(grpc_server_credentials* creds) {
99    GRPC_API_TRACE("grpc_server_credentials_release(creds=%p)", 1, (creds));
100    grpc_core::ExecCtx exec_ctx;
101    if (creds) creds->Unref();
102  }
103  
set_auth_metadata_processor(const grpc_auth_metadata_processor & processor)104  void grpc_server_credentials::set_auth_metadata_processor(
105      const grpc_auth_metadata_processor& processor) {
106    GRPC_API_TRACE(
107        "grpc_server_credentials_set_auth_metadata_processor("
108        "creds=%p, "
109        "processor=grpc_auth_metadata_processor { process: %p, state: %p })",
110        3, (this, (void*)(intptr_t)processor.process, processor.state));
111    DestroyProcessor();
112    processor_ = processor;
113  }
114  
grpc_server_credentials_set_auth_metadata_processor(grpc_server_credentials * creds,grpc_auth_metadata_processor processor)115  void grpc_server_credentials_set_auth_metadata_processor(
116      grpc_server_credentials* creds, grpc_auth_metadata_processor processor) {
117    GPR_DEBUG_ASSERT(creds != nullptr);
118    creds->set_auth_metadata_processor(processor);
119  }
120  
server_credentials_pointer_arg_destroy(void * p)121  static void server_credentials_pointer_arg_destroy(void* p) {
122    static_cast<grpc_server_credentials*>(p)->Unref();
123  }
124  
server_credentials_pointer_arg_copy(void * p)125  static void* server_credentials_pointer_arg_copy(void* p) {
126    return static_cast<grpc_server_credentials*>(p)->Ref().release();
127  }
128  
server_credentials_pointer_cmp(void * a,void * b)129  static int server_credentials_pointer_cmp(void* a, void* b) {
130    return GPR_ICMP(a, b);
131  }
132  
133  static const grpc_arg_pointer_vtable cred_ptr_vtable = {
134      server_credentials_pointer_arg_copy, server_credentials_pointer_arg_destroy,
135      server_credentials_pointer_cmp};
136  
grpc_server_credentials_to_arg(grpc_server_credentials * c)137  grpc_arg grpc_server_credentials_to_arg(grpc_server_credentials* c) {
138    return grpc_channel_arg_pointer_create(
139        const_cast<char*>(GRPC_SERVER_CREDENTIALS_ARG), c, &cred_ptr_vtable);
140  }
141  
grpc_server_credentials_from_arg(const grpc_arg * arg)142  grpc_server_credentials* grpc_server_credentials_from_arg(const grpc_arg* arg) {
143    if (strcmp(arg->key, GRPC_SERVER_CREDENTIALS_ARG) != 0) return nullptr;
144    if (arg->type != GRPC_ARG_POINTER) {
145      gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
146              GRPC_SERVER_CREDENTIALS_ARG);
147      return nullptr;
148    }
149    return static_cast<grpc_server_credentials*>(arg->value.pointer.p);
150  }
151  
grpc_find_server_credentials_in_args(const grpc_channel_args * args)152  grpc_server_credentials* grpc_find_server_credentials_in_args(
153      const grpc_channel_args* args) {
154    size_t i;
155    if (args == nullptr) return nullptr;
156    for (i = 0; i < args->num_args; i++) {
157      grpc_server_credentials* p =
158          grpc_server_credentials_from_arg(&args->args[i]);
159      if (p != nullptr) return p;
160    }
161    return nullptr;
162  }
163