blob: 3df3a87a967338ffa9053d6610041a31eef738e9 [file] [log] [blame]
Craig Tillerfaa84802015-03-01 21:56:38 -08001/*
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 "src/core/surface/init.h"
Craig Tiller178edfa2016-02-17 20:54:46 -080035
36#include <limits.h>
37#include <string.h>
38
39#include "src/core/surface/channel_init.h"
Craig Tillerfaa84802015-03-01 21:56:38 -080040#include "src/core/debug/trace.h"
Craig Tiller178edfa2016-02-17 20:54:46 -080041#include "src/core/security/auth_filters.h"
42#include "src/core/security/credentials.h"
Craig Tillerfaa84802015-03-01 21:56:38 -080043#include "src/core/security/secure_endpoint.h"
Craig Tiller178edfa2016-02-17 20:54:46 -080044#include "src/core/security/security_connector.h"
Craig Tillerfaa84802015-03-01 21:56:38 -080045#include "src/core/tsi/transport_security_interface.h"
46
47void grpc_security_pre_init(void) {
48 grpc_register_tracer("secure_endpoint", &grpc_trace_secure_endpoint);
Craig Tiller03dc06c2015-03-02 09:00:21 -080049 grpc_register_tracer("transport_security", &tsi_tracing_enabled);
Craig Tillerfaa84802015-03-01 21:56:38 -080050}
Craig Tiller178edfa2016-02-17 20:54:46 -080051
52static bool maybe_prepend_client_auth_filter(
53 grpc_channel_stack_builder *builder, void *arg) {
54 const grpc_channel_args *args =
55 grpc_channel_stack_builder_get_channel_arguments(builder);
56 if (args) {
57 for (size_t i = 0; i < args->num_args; i++) {
58 if (0 == strcmp(GRPC_SECURITY_CONNECTOR_ARG, args->args[i].key)) {
59 return grpc_channel_stack_builder_prepend_filter(
60 builder, &grpc_client_auth_filter, NULL, NULL);
61 }
62 }
63 }
64 return true;
65}
66
67static bool maybe_prepend_server_auth_filter(
68 grpc_channel_stack_builder *builder, void *arg) {
69 const grpc_channel_args *args =
70 grpc_channel_stack_builder_get_channel_arguments(builder);
71 if (args) {
72 for (size_t i = 0; i < args->num_args; i++) {
73 if (0 == strcmp(GRPC_SERVER_CREDENTIALS_ARG, args->args[i].key)) {
74 return grpc_channel_stack_builder_prepend_filter(
75 builder, &grpc_server_auth_filter, NULL, NULL);
76 }
77 }
78 }
79 return true;
80}
81
82void grpc_register_security_filters(void) {
83 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, INT_MAX,
84 maybe_prepend_client_auth_filter, NULL);
85 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
86 maybe_prepend_server_auth_filter, NULL);
87}