1 /*
2  *
3  * Copyright 2018 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/local/local_credentials.h"
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/security/security_connector/local_security_connector.h"
29 
30 #define GRPC_CREDENTIALS_TYPE_LOCAL "Local"
31 
local_credentials_destruct(grpc_channel_credentials * creds)32 static void local_credentials_destruct(grpc_channel_credentials* creds) {}
33 
local_server_credentials_destruct(grpc_server_credentials * creds)34 static void local_server_credentials_destruct(grpc_server_credentials* creds) {}
35 
local_create_security_connector(grpc_channel_credentials * creds,grpc_call_credentials * request_metadata_creds,const char * target_name,const grpc_channel_args * args,grpc_channel_security_connector ** sc,grpc_channel_args ** new_args)36 static grpc_security_status local_create_security_connector(
37     grpc_channel_credentials* creds,
38     grpc_call_credentials* request_metadata_creds, const char* target_name,
39     const grpc_channel_args* args, grpc_channel_security_connector** sc,
40     grpc_channel_args** new_args) {
41   return grpc_local_channel_security_connector_create(
42       creds, request_metadata_creds, args, target_name, sc);
43 }
44 
local_server_create_security_connector(grpc_server_credentials * creds,grpc_server_security_connector ** sc)45 static grpc_security_status local_server_create_security_connector(
46     grpc_server_credentials* creds, grpc_server_security_connector** sc) {
47   return grpc_local_server_security_connector_create(creds, sc);
48 }
49 
50 static const grpc_channel_credentials_vtable local_credentials_vtable = {
51     local_credentials_destruct, local_create_security_connector,
52     /*duplicate_without_call_credentials=*/nullptr};
53 
54 static const grpc_server_credentials_vtable local_server_credentials_vtable = {
55     local_server_credentials_destruct, local_server_create_security_connector};
56 
grpc_local_credentials_create(grpc_local_connect_type connect_type)57 grpc_channel_credentials* grpc_local_credentials_create(
58     grpc_local_connect_type connect_type) {
59   auto creds = static_cast<grpc_local_credentials*>(
60       gpr_zalloc(sizeof(grpc_local_credentials)));
61   creds->connect_type = connect_type;
62   creds->base.type = GRPC_CREDENTIALS_TYPE_LOCAL;
63   creds->base.vtable = &local_credentials_vtable;
64   gpr_ref_init(&creds->base.refcount, 1);
65   return &creds->base;
66 }
67 
grpc_local_server_credentials_create(grpc_local_connect_type connect_type)68 grpc_server_credentials* grpc_local_server_credentials_create(
69     grpc_local_connect_type connect_type) {
70   auto creds = static_cast<grpc_local_server_credentials*>(
71       gpr_zalloc(sizeof(grpc_local_server_credentials)));
72   creds->connect_type = connect_type;
73   creds->base.type = GRPC_CREDENTIALS_TYPE_LOCAL;
74   creds->base.vtable = &local_server_credentials_vtable;
75   gpr_ref_init(&creds->base.refcount, 1);
76   return &creds->base;
77 }
78