Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 35 | |
| 36 | #include <limits.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "src/core/surface/channel_init.h" |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 40 | #include "src/core/debug/trace.h" |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 41 | #include "src/core/security/auth_filters.h" |
| 42 | #include "src/core/security/credentials.h" |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 43 | #include "src/core/security/secure_endpoint.h" |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 44 | #include "src/core/security/security_connector.h" |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 45 | #include "src/core/tsi/transport_security_interface.h" |
| 46 | |
| 47 | void grpc_security_pre_init(void) { |
| 48 | grpc_register_tracer("secure_endpoint", &grpc_trace_secure_endpoint); |
Craig Tiller | 03dc06c | 2015-03-02 09:00:21 -0800 | [diff] [blame] | 49 | grpc_register_tracer("transport_security", &tsi_tracing_enabled); |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 50 | } |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame^] | 51 | |
| 52 | static 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 | |
| 67 | static 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 | |
| 82 | void 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 | } |