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/surface/init.h"
22
23 #include <limits.h>
24 #include <string.h>
25
26 #include "src/core/lib/debug/trace.h"
27 #include "src/core/lib/security/context/security_context.h"
28 #include "src/core/lib/security/credentials/credentials.h"
29 #include "src/core/lib/security/credentials/plugin/plugin_credentials.h"
30 #include "src/core/lib/security/security_connector/security_connector.h"
31 #include "src/core/lib/security/transport/auth_filters.h"
32 #include "src/core/lib/security/transport/secure_endpoint.h"
33 #include "src/core/lib/security/transport/security_handshaker.h"
34 #include "src/core/lib/surface/channel_init.h"
35 #include "src/core/tsi/transport_security_interface.h"
36
grpc_security_pre_init(void)37 void grpc_security_pre_init(void) {}
38
maybe_prepend_client_auth_filter(grpc_channel_stack_builder * builder,void * arg)39 static bool maybe_prepend_client_auth_filter(
40 grpc_channel_stack_builder* builder, void* arg) {
41 const grpc_channel_args* args =
42 grpc_channel_stack_builder_get_channel_arguments(builder);
43 if (args) {
44 for (size_t i = 0; i < args->num_args; i++) {
45 if (0 == strcmp(GRPC_ARG_SECURITY_CONNECTOR, args->args[i].key)) {
46 return grpc_channel_stack_builder_prepend_filter(
47 builder, &grpc_client_auth_filter, nullptr, nullptr);
48 }
49 }
50 }
51 return true;
52 }
53
maybe_prepend_server_auth_filter(grpc_channel_stack_builder * builder,void * arg)54 static bool maybe_prepend_server_auth_filter(
55 grpc_channel_stack_builder* builder, void* arg) {
56 const grpc_channel_args* args =
57 grpc_channel_stack_builder_get_channel_arguments(builder);
58 if (args) {
59 for (size_t i = 0; i < args->num_args; i++) {
60 if (0 == strcmp(GRPC_SERVER_CREDENTIALS_ARG, args->args[i].key)) {
61 return grpc_channel_stack_builder_prepend_filter(
62 builder, &grpc_server_auth_filter, nullptr, nullptr);
63 }
64 }
65 }
66 return true;
67 }
68
grpc_register_security_filters(void)69 void grpc_register_security_filters(void) {
70 // Register the auth client with a priority < INT_MAX to allow the authority
71 // filter -on which the auth filter depends- to be higher on the channel
72 // stack.
73 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, INT_MAX - 1,
74 maybe_prepend_client_auth_filter, nullptr);
75 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX - 1,
76 maybe_prepend_client_auth_filter, nullptr);
77 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
78 maybe_prepend_server_auth_filter, nullptr);
79 }
80
grpc_security_init()81 void grpc_security_init() { grpc_security_register_handshaker_factories(); }
82