Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 34 | #include "src/core/lib/security/transport/security_connector.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 36 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <string.h> |
| 38 | |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 39 | #include <grpc/slice_buffer.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
Julien Boeuf | 7fa3f41 | 2015-02-24 19:30:41 -0800 | [diff] [blame] | 41 | #include <grpc/support/host_port.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 43 | #include <grpc/support/string_util.h> |
Craig Tiller | 732a875 | 2016-02-22 15:59:19 -0800 | [diff] [blame] | 44 | |
Craig Tiller | d1697d9 | 2016-04-05 16:05:46 -0700 | [diff] [blame] | 45 | #include "src/core/ext/transport/chttp2/alpn/alpn.h" |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 46 | #include "src/core/lib/channel/channel_args.h" |
Mark D. Roth | d095340 | 2016-12-07 07:56:48 -0800 | [diff] [blame] | 47 | #include "src/core/lib/channel/handshaker.h" |
Craig Tiller | 8517886 | 2016-05-18 16:09:16 -0700 | [diff] [blame] | 48 | #include "src/core/lib/iomgr/load_file.h" |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 49 | #include "src/core/lib/security/context/security_context.h" |
| 50 | #include "src/core/lib/security/credentials/credentials.h" |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 51 | #include "src/core/lib/security/credentials/fake/fake_credentials.h" |
| 52 | #include "src/core/lib/security/transport/lb_targets_info.h" |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 53 | #include "src/core/lib/security/transport/secure_endpoint.h" |
Mark D. Roth | 071cacf | 2016-11-17 13:17:56 -0800 | [diff] [blame] | 54 | #include "src/core/lib/security/transport/security_handshaker.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 55 | #include "src/core/lib/support/env.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 56 | #include "src/core/lib/support/string.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 57 | #include "src/core/lib/tsi/fake_transport_security.h" |
| 58 | #include "src/core/lib/tsi/ssl_transport_security.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | |
| 60 | /* -- Constants. -- */ |
| 61 | |
Nicolas "Pixel" Noble | 7274382 | 2015-02-20 20:59:29 +0100 | [diff] [blame] | 62 | #ifndef INSTALL_PREFIX |
Nicolas "Pixel" Noble | 161ea23 | 2015-02-22 05:48:53 +0100 | [diff] [blame] | 63 | static const char *installed_roots_path = "/usr/share/grpc/roots.pem"; |
Nicolas "Pixel" Noble | 7274382 | 2015-02-20 20:59:29 +0100 | [diff] [blame] | 64 | #else |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | static const char *installed_roots_path = |
| 66 | INSTALL_PREFIX "/share/grpc/roots.pem"; |
Nicolas "Pixel" Noble | 7274382 | 2015-02-20 20:59:29 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
Julien Boeuf | a50da47 | 2016-01-27 16:23:41 -0800 | [diff] [blame] | 69 | /* -- Overridden default roots. -- */ |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 70 | |
Julien Boeuf | aaebf7a | 2016-01-28 17:04:42 -0800 | [diff] [blame] | 71 | static grpc_ssl_roots_override_callback ssl_roots_override_cb = NULL; |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 72 | |
Julien Boeuf | aaebf7a | 2016-01-28 17:04:42 -0800 | [diff] [blame] | 73 | void grpc_set_ssl_roots_override_callback(grpc_ssl_roots_override_callback cb) { |
| 74 | ssl_roots_override_cb = cb; |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Julien Boeuf | 8d6ec91 | 2015-02-24 20:07:59 -0800 | [diff] [blame] | 77 | /* -- Cipher suites. -- */ |
| 78 | |
| 79 | /* Defines the cipher suites that we accept by default. All these cipher suites |
| 80 | are compliant with HTTP2. */ |
Julien Boeuf | d43f0c3 | 2015-02-24 20:54:12 -0800 | [diff] [blame] | 81 | #define GRPC_SSL_CIPHER_SUITES \ |
| 82 | "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-" \ |
| 83 | "SHA384:ECDHE-RSA-AES256-GCM-SHA384" |
Julien Boeuf | 8d6ec91 | 2015-02-24 20:07:59 -0800 | [diff] [blame] | 84 | |
| 85 | static gpr_once cipher_suites_once = GPR_ONCE_INIT; |
| 86 | static const char *cipher_suites = NULL; |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | static void init_cipher_suites(void) { |
| 89 | char *overridden = gpr_getenv("GRPC_SSL_CIPHER_SUITES"); |
Julien Boeuf | 8d6ec91 | 2015-02-24 20:07:59 -0800 | [diff] [blame] | 90 | cipher_suites = overridden != NULL ? overridden : GRPC_SSL_CIPHER_SUITES; |
| 91 | } |
| 92 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | static const char *ssl_cipher_suites(void) { |
| 94 | gpr_once_init(&cipher_suites_once, init_cipher_suites); |
Julien Boeuf | 8d6ec91 | 2015-02-24 20:07:59 -0800 | [diff] [blame] | 95 | return cipher_suites; |
| 96 | } |
| 97 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | /* -- Common methods. -- */ |
| 99 | |
Julien Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 100 | /* Returns the first property with that name. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | const tsi_peer_property *tsi_peer_get_property_by_name(const tsi_peer *peer, |
| 102 | const char *name) { |
Julien Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 103 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | if (peer == NULL) return NULL; |
| 105 | for (i = 0; i < peer->property_count; i++) { |
| 106 | const tsi_peer_property *property = &peer->properties[i]; |
| 107 | if (name == NULL && property->name == NULL) { |
| 108 | return property; |
Julien Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 109 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | if (name != NULL && property->name != NULL && |
| 111 | strcmp(property->name, name) == 0) { |
| 112 | return property; |
| 113 | } |
| 114 | } |
Julien Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
| 117 | |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 118 | void grpc_channel_security_connector_add_handshakers( |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 119 | grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *connector, |
| 120 | grpc_handshake_manager *handshake_mgr) { |
| 121 | if (connector != NULL) { |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 122 | connector->add_handshakers(exec_ctx, connector, handshake_mgr); |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 126 | void grpc_server_security_connector_add_handshakers( |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 127 | grpc_exec_ctx *exec_ctx, grpc_server_security_connector *connector, |
| 128 | grpc_handshake_manager *handshake_mgr) { |
| 129 | if (connector != NULL) { |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 130 | connector->add_handshakers(exec_ctx, connector, handshake_mgr); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 134 | void grpc_security_connector_check_peer(grpc_exec_ctx *exec_ctx, |
| 135 | grpc_security_connector *sc, |
| 136 | tsi_peer peer, |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 137 | grpc_auth_context **auth_context, |
| 138 | grpc_closure *on_peer_checked) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | if (sc == NULL) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 140 | grpc_closure_sched( |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 141 | exec_ctx, on_peer_checked, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 142 | GRPC_ERROR_CREATE("cannot check peer -- no security connector")); |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 143 | tsi_peer_destruct(&peer); |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 144 | } else { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 145 | sc->vtable->check_peer(exec_ctx, sc, peer, auth_context, on_peer_checked); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 149 | void grpc_channel_security_connector_check_call_host( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc, |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 151 | const char *host, grpc_auth_context *auth_context, |
| 152 | grpc_security_call_host_check_cb cb, void *user_data) { |
| 153 | if (sc == NULL || sc->check_call_host == NULL) { |
| 154 | cb(exec_ctx, user_data, GRPC_SECURITY_ERROR); |
| 155 | } else { |
| 156 | sc->check_call_host(exec_ctx, sc, host, auth_context, cb, user_data); |
| 157 | } |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 160 | #ifdef GRPC_SECURITY_CONNECTOR_REFCOUNT_DEBUG |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | grpc_security_connector *grpc_security_connector_ref( |
| 162 | grpc_security_connector *sc, const char *file, int line, |
| 163 | const char *reason) { |
| 164 | if (sc == NULL) return NULL; |
| 165 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 166 | "SECURITY_CONNECTOR:%p ref %d -> %d %s", sc, |
| 167 | (int)sc->refcount.count, (int)sc->refcount.count + 1, reason); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 168 | #else |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | grpc_security_connector *grpc_security_connector_ref( |
| 170 | grpc_security_connector *sc) { |
| 171 | if (sc == NULL) return NULL; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 172 | #endif |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | gpr_ref(&sc->refcount); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 174 | return sc; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 177 | #ifdef GRPC_SECURITY_CONNECTOR_REFCOUNT_DEBUG |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 178 | void grpc_security_connector_unref(grpc_exec_ctx *exec_ctx, |
| 179 | grpc_security_connector *sc, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | const char *file, int line, |
| 181 | const char *reason) { |
| 182 | if (sc == NULL) return; |
| 183 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, |
| 184 | "SECURITY_CONNECTOR:%p unref %d -> %d %s", sc, |
| 185 | (int)sc->refcount.count, (int)sc->refcount.count - 1, reason); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 186 | #else |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 187 | void grpc_security_connector_unref(grpc_exec_ctx *exec_ctx, |
| 188 | grpc_security_connector *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | if (sc == NULL) return; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 190 | #endif |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 191 | if (gpr_unref(&sc->refcount)) sc->vtable->destroy(exec_ctx, sc); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 194 | static void connector_pointer_arg_destroy(grpc_exec_ctx *exec_ctx, void *p) { |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 195 | GRPC_SECURITY_CONNECTOR_UNREF(exec_ctx, p, "connector_pointer_arg_destroy"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | static void *connector_pointer_arg_copy(void *p) { |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 199 | return GRPC_SECURITY_CONNECTOR_REF(p, "connector_pointer_arg_copy"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 202 | static int connector_pointer_cmp(void *a, void *b) { return GPR_ICMP(a, b); } |
Craig Tiller | edc2fff | 2016-01-13 06:54:27 -0800 | [diff] [blame] | 203 | |
| 204 | static const grpc_arg_pointer_vtable connector_pointer_vtable = { |
Craig Tiller | 5de79ee | 2016-01-25 08:16:02 -0800 | [diff] [blame] | 205 | connector_pointer_arg_copy, connector_pointer_arg_destroy, |
| 206 | connector_pointer_cmp}; |
Craig Tiller | edc2fff | 2016-01-13 06:54:27 -0800 | [diff] [blame] | 207 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | grpc_arg result; |
| 210 | result.type = GRPC_ARG_POINTER; |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 211 | result.key = GRPC_ARG_SECURITY_CONNECTOR; |
Craig Tiller | edc2fff | 2016-01-13 06:54:27 -0800 | [diff] [blame] | 212 | result.value.pointer.vtable = &connector_pointer_vtable; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 213 | result.value.pointer.p = sc; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | return result; |
| 215 | } |
| 216 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg) { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 218 | if (strcmp(arg->key, GRPC_ARG_SECURITY_CONNECTOR)) return NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | if (arg->type != GRPC_ARG_POINTER) { |
| 220 | gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type, |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 221 | GRPC_ARG_SECURITY_CONNECTOR); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | return NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | return arg->value.pointer.p; |
| 225 | } |
| 226 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 227 | grpc_security_connector *grpc_security_connector_find_in_args( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 228 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 229 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | if (args == NULL) return NULL; |
| 231 | for (i = 0; i < args->num_args; i++) { |
| 232 | grpc_security_connector *sc = |
| 233 | grpc_security_connector_from_arg(&args->args[i]); |
| 234 | if (sc != NULL) return sc; |
| 235 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | return NULL; |
| 237 | } |
| 238 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | /* -- Fake implementation. -- */ |
| 240 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 241 | typedef struct { |
| 242 | grpc_channel_security_connector base; |
| 243 | char *target; |
| 244 | char *expected_targets; |
| 245 | bool is_lb_channel; |
| 246 | } grpc_fake_channel_security_connector; |
| 247 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 248 | static void fake_channel_destroy(grpc_exec_ctx *exec_ctx, |
| 249 | grpc_security_connector *sc) { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 250 | grpc_fake_channel_security_connector *c = |
| 251 | (grpc_fake_channel_security_connector *)sc; |
| 252 | grpc_call_credentials_unref(exec_ctx, c->base.request_metadata_creds); |
| 253 | gpr_free(c->target); |
| 254 | gpr_free(c->expected_targets); |
| 255 | gpr_free(c); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 258 | static void fake_server_destroy(grpc_exec_ctx *exec_ctx, |
| 259 | grpc_security_connector *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | gpr_free(sc); |
Julien Boeuf | 84d964a | 2015-04-29 11:31:06 -0700 | [diff] [blame] | 261 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 262 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 263 | static bool fake_check_target(const char *target_type, const char *target, |
| 264 | const char *set_str) { |
| 265 | GPR_ASSERT(target_type != NULL); |
| 266 | GPR_ASSERT(target != NULL); |
| 267 | char **set = NULL; |
| 268 | size_t set_size = 0; |
| 269 | gpr_string_split(set_str, ",", &set, &set_size); |
| 270 | bool found = false; |
| 271 | for (size_t i = 0; i < set_size; ++i) { |
| 272 | if (set[i] != NULL && strcmp(target, set[i]) == 0) found = true; |
| 273 | } |
| 274 | for (size_t i = 0; i < set_size; ++i) { |
| 275 | gpr_free(set[i]); |
| 276 | } |
| 277 | gpr_free(set); |
| 278 | return found; |
| 279 | } |
| 280 | |
| 281 | static void fake_secure_name_check(const char *target, |
| 282 | const char *expected_targets, |
| 283 | bool is_lb_channel) { |
| 284 | if (expected_targets == NULL) return; |
| 285 | char **lbs_and_backends = NULL; |
| 286 | size_t lbs_and_backends_size = 0; |
| 287 | bool success = false; |
| 288 | gpr_string_split(expected_targets, ";", &lbs_and_backends, |
| 289 | &lbs_and_backends_size); |
| 290 | if (lbs_and_backends_size > 2 || lbs_and_backends_size == 0) { |
| 291 | gpr_log(GPR_ERROR, "Invalid expected targets arg value: '%s'", |
| 292 | expected_targets); |
| 293 | goto done; |
| 294 | } |
| 295 | if (is_lb_channel) { |
| 296 | if (lbs_and_backends_size != 2) { |
| 297 | gpr_log(GPR_ERROR, |
| 298 | "Invalid expected targets arg value: '%s'. Expectations for LB " |
| 299 | "channels must be of the form 'be1,be2,be3,...;lb1,lb2,...", |
| 300 | expected_targets); |
| 301 | goto done; |
| 302 | } |
| 303 | if (!fake_check_target("LB", target, lbs_and_backends[1])) { |
| 304 | gpr_log(GPR_ERROR, "LB target '%s' not found in expected set '%s'", |
| 305 | target, lbs_and_backends[1]); |
| 306 | goto done; |
| 307 | } |
| 308 | success = true; |
| 309 | } else { |
| 310 | if (!fake_check_target("Backend", target, lbs_and_backends[0])) { |
| 311 | gpr_log(GPR_ERROR, "Backend target '%s' not found in expected set '%s'", |
| 312 | target, lbs_and_backends[0]); |
| 313 | goto done; |
| 314 | } |
| 315 | success = true; |
| 316 | } |
| 317 | done: |
| 318 | for (size_t i = 0; i < lbs_and_backends_size; ++i) { |
| 319 | gpr_free(lbs_and_backends[i]); |
| 320 | } |
| 321 | gpr_free(lbs_and_backends); |
| 322 | if (!success) abort(); |
| 323 | } |
| 324 | |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 325 | static void fake_check_peer(grpc_exec_ctx *exec_ctx, |
| 326 | grpc_security_connector *sc, tsi_peer peer, |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 327 | grpc_auth_context **auth_context, |
| 328 | grpc_closure *on_peer_checked) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 329 | const char *prop_name; |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 330 | grpc_error *error = GRPC_ERROR_NONE; |
| 331 | *auth_context = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 332 | if (peer.property_count != 1) { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 333 | error = GRPC_ERROR_CREATE("Fake peers should only have 1 property."); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | goto end; |
| 335 | } |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 336 | prop_name = peer.properties[0].name; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 337 | if (prop_name == NULL || |
| 338 | strcmp(prop_name, TSI_CERTIFICATE_TYPE_PEER_PROPERTY)) { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 339 | char *msg; |
| 340 | gpr_asprintf(&msg, "Unexpected property in fake peer: %s.", |
| 341 | prop_name == NULL ? "<EMPTY>" : prop_name); |
| 342 | error = GRPC_ERROR_CREATE(msg); |
| 343 | gpr_free(msg); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 344 | goto end; |
| 345 | } |
| 346 | if (strncmp(peer.properties[0].value.data, TSI_FAKE_CERTIFICATE_TYPE, |
| 347 | peer.properties[0].value.length)) { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 348 | error = GRPC_ERROR_CREATE("Invalid value for cert type property."); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 349 | goto end; |
| 350 | } |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 351 | *auth_context = grpc_auth_context_create(NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 352 | grpc_auth_context_add_cstring_property( |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 353 | *auth_context, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 354 | GRPC_FAKE_TRANSPORT_SECURITY_TYPE); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 355 | end: |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 356 | grpc_closure_sched(exec_ctx, on_peer_checked, error); |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 357 | tsi_peer_destruct(&peer); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 358 | } |
| 359 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 360 | static void fake_channel_check_peer(grpc_exec_ctx *exec_ctx, |
| 361 | grpc_security_connector *sc, tsi_peer peer, |
| 362 | grpc_auth_context **auth_context, |
| 363 | grpc_closure *on_peer_checked) { |
| 364 | fake_check_peer(exec_ctx, sc, peer, auth_context, on_peer_checked); |
| 365 | grpc_fake_channel_security_connector *c = |
| 366 | (grpc_fake_channel_security_connector *)sc; |
| 367 | fake_secure_name_check(c->target, c->expected_targets, c->is_lb_channel); |
| 368 | } |
| 369 | |
| 370 | static void fake_server_check_peer(grpc_exec_ctx *exec_ctx, |
| 371 | grpc_security_connector *sc, tsi_peer peer, |
| 372 | grpc_auth_context **auth_context, |
| 373 | grpc_closure *on_peer_checked) { |
| 374 | fake_check_peer(exec_ctx, sc, peer, auth_context, on_peer_checked); |
| 375 | } |
| 376 | |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 377 | static void fake_channel_check_call_host(grpc_exec_ctx *exec_ctx, |
| 378 | grpc_channel_security_connector *sc, |
| 379 | const char *host, |
| 380 | grpc_auth_context *auth_context, |
| 381 | grpc_security_call_host_check_cb cb, |
| 382 | void *user_data) { |
| 383 | cb(exec_ctx, user_data, GRPC_SECURITY_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 386 | static void fake_channel_add_handshakers( |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 387 | grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc, |
| 388 | grpc_handshake_manager *handshake_mgr) { |
Mark D. Roth | d095340 | 2016-12-07 07:56:48 -0800 | [diff] [blame] | 389 | grpc_handshake_manager_add( |
| 390 | handshake_mgr, |
| 391 | grpc_security_handshaker_create( |
| 392 | exec_ctx, tsi_create_fake_handshaker(true /* is_client */), |
| 393 | &sc->base)); |
Julien Boeuf | 87047d7 | 2015-08-21 14:30:33 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Mark D. Roth | 5b850b2 | 2016-12-08 09:40:58 -0800 | [diff] [blame] | 396 | static void fake_server_add_handshakers(grpc_exec_ctx *exec_ctx, |
| 397 | grpc_server_security_connector *sc, |
| 398 | grpc_handshake_manager *handshake_mgr) { |
Mark D. Roth | d095340 | 2016-12-07 07:56:48 -0800 | [diff] [blame] | 399 | grpc_handshake_manager_add( |
| 400 | handshake_mgr, |
| 401 | grpc_security_handshaker_create( |
| 402 | exec_ctx, tsi_create_fake_handshaker(false /* is_client */), |
| 403 | &sc->base)); |
Julien Boeuf | 87047d7 | 2015-08-21 14:30:33 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 406 | static grpc_security_connector_vtable fake_channel_vtable = { |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 407 | fake_channel_destroy, fake_channel_check_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 408 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 409 | static grpc_security_connector_vtable fake_server_vtable = { |
| 410 | fake_server_destroy, fake_server_check_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 411 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 412 | grpc_channel_security_connector *grpc_fake_channel_security_connector_create( |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 413 | grpc_call_credentials *request_metadata_creds, const char *target, |
| 414 | const grpc_channel_args *args) { |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 415 | grpc_fake_channel_security_connector *c = gpr_zalloc(sizeof(*c)); |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 416 | gpr_ref_init(&c->base.base.refcount, 1); |
| 417 | c->base.base.url_scheme = GRPC_FAKE_SECURITY_URL_SCHEME; |
| 418 | c->base.base.vtable = &fake_channel_vtable; |
| 419 | c->base.request_metadata_creds = |
| 420 | grpc_call_credentials_ref(request_metadata_creds); |
| 421 | c->base.check_call_host = fake_channel_check_call_host; |
| 422 | c->base.add_handshakers = fake_channel_add_handshakers; |
| 423 | c->target = gpr_strdup(target); |
| 424 | const grpc_arg *expected_target_arg = |
| 425 | grpc_channel_args_find(args, GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS); |
| 426 | if (expected_target_arg != NULL) { |
| 427 | GPR_ASSERT(expected_target_arg->type == GRPC_ARG_STRING); |
| 428 | c->expected_targets = gpr_strdup(expected_target_arg->value.string); |
| 429 | } |
| 430 | c->is_lb_channel = (grpc_lb_targets_info_find_in_args(args) != NULL); |
| 431 | return &c->base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 434 | grpc_server_security_connector *grpc_fake_server_security_connector_create( |
| 435 | void) { |
| 436 | grpc_server_security_connector *c = |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 437 | gpr_zalloc(sizeof(grpc_server_security_connector)); |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 438 | gpr_ref_init(&c->base.refcount, 1); |
| 439 | c->base.vtable = &fake_server_vtable; |
| 440 | c->base.url_scheme = GRPC_FAKE_SECURITY_URL_SCHEME; |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 441 | c->add_handshakers = fake_server_add_handshakers; |
yang-g | 5e72a35 | 2015-11-20 09:49:36 -0800 | [diff] [blame] | 442 | return c; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* --- Ssl implementation. --- */ |
| 446 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 447 | typedef struct { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 448 | grpc_channel_security_connector base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 449 | tsi_ssl_handshaker_factory *handshaker_factory; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 450 | char *target_name; |
| 451 | char *overridden_target_name; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 452 | } grpc_ssl_channel_security_connector; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 453 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 454 | typedef struct { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 455 | grpc_server_security_connector base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 456 | tsi_ssl_handshaker_factory *handshaker_factory; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 457 | } grpc_ssl_server_security_connector; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 458 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 459 | static void ssl_channel_destroy(grpc_exec_ctx *exec_ctx, |
| 460 | grpc_security_connector *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | grpc_ssl_channel_security_connector *c = |
| 462 | (grpc_ssl_channel_security_connector *)sc; |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 463 | grpc_call_credentials_unref(exec_ctx, c->base.request_metadata_creds); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 464 | if (c->handshaker_factory != NULL) { |
| 465 | tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); |
| 466 | } |
| 467 | if (c->target_name != NULL) gpr_free(c->target_name); |
| 468 | if (c->overridden_target_name != NULL) gpr_free(c->overridden_target_name); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 469 | gpr_free(sc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 472 | static void ssl_server_destroy(grpc_exec_ctx *exec_ctx, |
| 473 | grpc_security_connector *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 474 | grpc_ssl_server_security_connector *c = |
| 475 | (grpc_ssl_server_security_connector *)sc; |
| 476 | if (c->handshaker_factory != NULL) { |
| 477 | tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); |
| 478 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 479 | gpr_free(sc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 482 | static grpc_security_status ssl_create_handshaker( |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 483 | tsi_ssl_handshaker_factory *handshaker_factory, bool is_client, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 484 | const char *peer_name, tsi_handshaker **handshaker) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 485 | tsi_result result = TSI_OK; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 486 | if (handshaker_factory == NULL) return GRPC_SECURITY_ERROR; |
| 487 | result = tsi_ssl_handshaker_factory_create_handshaker( |
| 488 | handshaker_factory, is_client ? peer_name : NULL, handshaker); |
| 489 | if (result != TSI_OK) { |
| 490 | gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.", |
| 491 | tsi_result_to_string(result)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 492 | return GRPC_SECURITY_ERROR; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 493 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 494 | return GRPC_SECURITY_OK; |
| 495 | } |
| 496 | |
Mark D. Roth | d9ef283 | 2016-12-06 07:37:27 -0800 | [diff] [blame] | 497 | static void ssl_channel_add_handshakers(grpc_exec_ctx *exec_ctx, |
| 498 | grpc_channel_security_connector *sc, |
| 499 | grpc_handshake_manager *handshake_mgr) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 500 | grpc_ssl_channel_security_connector *c = |
| 501 | (grpc_ssl_channel_security_connector *)sc; |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 502 | // Instantiate TSI handshaker. |
| 503 | tsi_handshaker *tsi_hs = NULL; |
Mark D. Roth | a561785 | 2016-12-01 08:48:22 -0800 | [diff] [blame] | 504 | ssl_create_handshaker(c->handshaker_factory, true /* is_client */, |
| 505 | c->overridden_target_name != NULL |
| 506 | ? c->overridden_target_name |
| 507 | : c->target_name, |
| 508 | &tsi_hs); |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 509 | // Create handshakers. |
Mark D. Roth | cc8487e | 2016-12-07 08:17:06 -0800 | [diff] [blame] | 510 | grpc_handshake_manager_add(handshake_mgr, grpc_security_handshaker_create( |
| 511 | exec_ctx, tsi_hs, &sc->base)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Mark D. Roth | d9ef283 | 2016-12-06 07:37:27 -0800 | [diff] [blame] | 514 | static void ssl_server_add_handshakers(grpc_exec_ctx *exec_ctx, |
| 515 | grpc_server_security_connector *sc, |
| 516 | grpc_handshake_manager *handshake_mgr) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 517 | grpc_ssl_server_security_connector *c = |
| 518 | (grpc_ssl_server_security_connector *)sc; |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 519 | // Instantiate TSI handshaker. |
| 520 | tsi_handshaker *tsi_hs = NULL; |
Mark D. Roth | a561785 | 2016-12-01 08:48:22 -0800 | [diff] [blame] | 521 | ssl_create_handshaker(c->handshaker_factory, false /* is_client */, |
| 522 | NULL /* peer_name */, &tsi_hs); |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 523 | // Create handshakers. |
Mark D. Roth | cc8487e | 2016-12-07 08:17:06 -0800 | [diff] [blame] | 524 | grpc_handshake_manager_add(handshake_mgr, grpc_security_handshaker_create( |
| 525 | exec_ctx, tsi_hs, &sc->base)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 528 | static int ssl_host_matches_name(const tsi_peer *peer, const char *peer_name) { |
Craig Tiller | deb49dd | 2015-02-25 11:58:49 -0800 | [diff] [blame] | 529 | char *allocated_name = NULL; |
| 530 | int r; |
| 531 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 532 | if (strchr(peer_name, ':') != NULL) { |
| 533 | char *ignored_port; |
| 534 | gpr_split_host_port(peer_name, &allocated_name, &ignored_port); |
| 535 | gpr_free(ignored_port); |
| 536 | peer_name = allocated_name; |
| 537 | if (!peer_name) return 0; |
| 538 | } |
| 539 | r = tsi_ssl_peer_matches_name(peer, peer_name); |
| 540 | gpr_free(allocated_name); |
Craig Tiller | deb49dd | 2015-02-25 11:58:49 -0800 | [diff] [blame] | 541 | return r; |
| 542 | } |
| 543 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 544 | grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer) { |
Julien Boeuf | 84d964a | 2015-04-29 11:31:06 -0700 | [diff] [blame] | 545 | size_t i; |
Julien Boeuf | 84d964a | 2015-04-29 11:31:06 -0700 | [diff] [blame] | 546 | grpc_auth_context *ctx = NULL; |
Julien Boeuf | ea456fc | 2015-07-07 15:23:30 -0700 | [diff] [blame] | 547 | const char *peer_identity_property_name = NULL; |
Julien Boeuf | a701ade | 2015-06-18 15:23:40 +0200 | [diff] [blame] | 548 | |
| 549 | /* The caller has checked the certificate type property. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 550 | GPR_ASSERT(peer->property_count >= 1); |
| 551 | ctx = grpc_auth_context_create(NULL); |
| 552 | grpc_auth_context_add_cstring_property( |
| 553 | ctx, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME, |
| 554 | GRPC_SSL_TRANSPORT_SECURITY_TYPE); |
| 555 | for (i = 0; i < peer->property_count; i++) { |
| 556 | const tsi_peer_property *prop = &peer->properties[i]; |
| 557 | if (prop->name == NULL) continue; |
| 558 | if (strcmp(prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) { |
| 559 | /* If there is no subject alt name, have the CN as the identity. */ |
| 560 | if (peer_identity_property_name == NULL) { |
| 561 | peer_identity_property_name = GRPC_X509_CN_PROPERTY_NAME; |
| 562 | } |
| 563 | grpc_auth_context_add_property(ctx, GRPC_X509_CN_PROPERTY_NAME, |
| 564 | prop->value.data, prop->value.length); |
| 565 | } else if (strcmp(prop->name, |
| 566 | TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) { |
| 567 | peer_identity_property_name = GRPC_X509_SAN_PROPERTY_NAME; |
| 568 | grpc_auth_context_add_property(ctx, GRPC_X509_SAN_PROPERTY_NAME, |
| 569 | prop->value.data, prop->value.length); |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 570 | } else if (strcmp(prop->name, TSI_X509_PEM_CERT_PROPERTY) == 0) { |
| 571 | grpc_auth_context_add_property(ctx, GRPC_X509_PEM_CERT_PROPERTY_NAME, |
| 572 | prop->value.data, prop->value.length); |
Julien Boeuf | 84d964a | 2015-04-29 11:31:06 -0700 | [diff] [blame] | 573 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 574 | } |
| 575 | if (peer_identity_property_name != NULL) { |
| 576 | GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name( |
| 577 | ctx, peer_identity_property_name) == 1); |
| 578 | } |
Julien Boeuf | 84d964a | 2015-04-29 11:31:06 -0700 | [diff] [blame] | 579 | return ctx; |
| 580 | } |
| 581 | |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 582 | static grpc_error *ssl_check_peer(grpc_security_connector *sc, |
| 583 | const char *peer_name, const tsi_peer *peer, |
| 584 | grpc_auth_context **auth_context) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 585 | /* Check the ALPN. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 586 | const tsi_peer_property *p = |
| 587 | tsi_peer_get_property_by_name(peer, TSI_SSL_ALPN_SELECTED_PROTOCOL); |
| 588 | if (p == NULL) { |
Mark D. Roth | a561785 | 2016-12-01 08:48:22 -0800 | [diff] [blame] | 589 | return GRPC_ERROR_CREATE( |
| 590 | "Cannot check peer: missing selected ALPN property."); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 591 | } |
| 592 | if (!grpc_chttp2_is_alpn_version_supported(p->value.data, p->value.length)) { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 593 | return GRPC_ERROR_CREATE("Cannot check peer: invalid ALPN value."); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 594 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 595 | |
| 596 | /* Check the peer name if specified. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 597 | if (peer_name != NULL && !ssl_host_matches_name(peer, peer_name)) { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 598 | char *msg; |
| 599 | gpr_asprintf(&msg, "Peer name %s is not in peer certificate", peer_name); |
| 600 | grpc_error *error = GRPC_ERROR_CREATE(msg); |
| 601 | gpr_free(msg); |
| 602 | return error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 603 | } |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 604 | *auth_context = tsi_ssl_peer_to_auth_context(peer); |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 605 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Craig Tiller | be52c6e | 2016-01-04 15:35:26 -0800 | [diff] [blame] | 608 | static void ssl_channel_check_peer(grpc_exec_ctx *exec_ctx, |
| 609 | grpc_security_connector *sc, tsi_peer peer, |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 610 | grpc_auth_context **auth_context, |
| 611 | grpc_closure *on_peer_checked) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 612 | grpc_ssl_channel_security_connector *c = |
| 613 | (grpc_ssl_channel_security_connector *)sc; |
Craig Tiller | cea72a0 | 2017-02-17 07:04:21 -0800 | [diff] [blame] | 614 | grpc_error *error = ssl_check_peer(sc, c->overridden_target_name != NULL |
| 615 | ? c->overridden_target_name |
| 616 | : c->target_name, |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 617 | &peer, auth_context); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 618 | grpc_closure_sched(exec_ctx, on_peer_checked, error); |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 619 | tsi_peer_destruct(&peer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Craig Tiller | be52c6e | 2016-01-04 15:35:26 -0800 | [diff] [blame] | 622 | static void ssl_server_check_peer(grpc_exec_ctx *exec_ctx, |
| 623 | grpc_security_connector *sc, tsi_peer peer, |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 624 | grpc_auth_context **auth_context, |
| 625 | grpc_closure *on_peer_checked) { |
| 626 | grpc_error *error = ssl_check_peer(sc, NULL, &peer, auth_context); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 627 | tsi_peer_destruct(&peer); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 628 | grpc_closure_sched(exec_ctx, on_peer_checked, error); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 631 | static void add_shallow_auth_property_to_peer(tsi_peer *peer, |
| 632 | const grpc_auth_property *prop, |
| 633 | const char *tsi_prop_name) { |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 634 | tsi_peer_property *tsi_prop = &peer->properties[peer->property_count++]; |
| 635 | tsi_prop->name = (char *)tsi_prop_name; |
| 636 | tsi_prop->value.data = prop->value; |
| 637 | tsi_prop->value.length = prop->value_length; |
| 638 | } |
| 639 | |
| 640 | tsi_peer tsi_shallow_peer_from_ssl_auth_context( |
| 641 | const grpc_auth_context *auth_context) { |
| 642 | size_t max_num_props = 0; |
| 643 | grpc_auth_property_iterator it; |
| 644 | const grpc_auth_property *prop; |
| 645 | tsi_peer peer; |
| 646 | memset(&peer, 0, sizeof(peer)); |
| 647 | |
| 648 | it = grpc_auth_context_property_iterator(auth_context); |
| 649 | while (grpc_auth_property_iterator_next(&it) != NULL) max_num_props++; |
| 650 | |
| 651 | if (max_num_props > 0) { |
| 652 | peer.properties = gpr_malloc(max_num_props * sizeof(tsi_peer_property)); |
| 653 | it = grpc_auth_context_property_iterator(auth_context); |
| 654 | while ((prop = grpc_auth_property_iterator_next(&it)) != NULL) { |
| 655 | if (strcmp(prop->name, GRPC_X509_SAN_PROPERTY_NAME) == 0) { |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 656 | add_shallow_auth_property_to_peer( |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 657 | &peer, prop, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY); |
| 658 | } else if (strcmp(prop->name, GRPC_X509_CN_PROPERTY_NAME) == 0) { |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 659 | add_shallow_auth_property_to_peer( |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 660 | &peer, prop, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY); |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 661 | } else if (strcmp(prop->name, GRPC_X509_PEM_CERT_PROPERTY_NAME) == 0) { |
| 662 | add_shallow_auth_property_to_peer(&peer, prop, |
| 663 | TSI_X509_PEM_CERT_PROPERTY); |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | return peer; |
| 668 | } |
| 669 | |
| 670 | void tsi_shallow_peer_destruct(tsi_peer *peer) { |
| 671 | if (peer->properties != NULL) gpr_free(peer->properties); |
| 672 | } |
| 673 | |
| 674 | static void ssl_channel_check_call_host(grpc_exec_ctx *exec_ctx, |
| 675 | grpc_channel_security_connector *sc, |
| 676 | const char *host, |
| 677 | grpc_auth_context *auth_context, |
| 678 | grpc_security_call_host_check_cb cb, |
| 679 | void *user_data) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 680 | grpc_ssl_channel_security_connector *c = |
| 681 | (grpc_ssl_channel_security_connector *)sc; |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 682 | grpc_security_status status = GRPC_SECURITY_ERROR; |
| 683 | tsi_peer peer = tsi_shallow_peer_from_ssl_auth_context(auth_context); |
| 684 | if (ssl_host_matches_name(&peer, host)) status = GRPC_SECURITY_OK; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 685 | |
| 686 | /* If the target name was overridden, then the original target_name was |
| 687 | 'checked' transitively during the previous peer check at the end of the |
| 688 | handshake. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 689 | if (c->overridden_target_name != NULL && strcmp(host, c->target_name) == 0) { |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 690 | status = GRPC_SECURITY_OK; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 691 | } |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 692 | cb(exec_ctx, user_data, status); |
| 693 | tsi_shallow_peer_destruct(&peer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 694 | } |
| 695 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 696 | static grpc_security_connector_vtable ssl_channel_vtable = { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 697 | ssl_channel_destroy, ssl_channel_check_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 698 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 699 | static grpc_security_connector_vtable ssl_server_vtable = { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 700 | ssl_server_destroy, ssl_server_check_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 701 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 702 | static grpc_slice compute_default_pem_root_certs_once(void) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 703 | grpc_slice result = grpc_empty_slice(); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 704 | |
Julien Boeuf | 3e00179 | 2015-02-20 15:02:36 -0800 | [diff] [blame] | 705 | /* First try to load the roots from the environment. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 706 | char *default_root_certs_path = |
| 707 | gpr_getenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR); |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 708 | if (default_root_certs_path != NULL) { |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 709 | GRPC_LOG_IF_ERROR("load_file", |
Craig Tiller | 8517886 | 2016-05-18 16:09:16 -0700 | [diff] [blame] | 710 | grpc_load_file(default_root_certs_path, 0, &result)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 711 | gpr_free(default_root_certs_path); |
| 712 | } |
Julien Boeuf | 3e00179 | 2015-02-20 15:02:36 -0800 | [diff] [blame] | 713 | |
Julien Boeuf | 434eda4 | 2016-01-28 17:06:57 -0800 | [diff] [blame] | 714 | /* Try overridden roots if needed. */ |
Julien Boeuf | aaebf7a | 2016-01-28 17:04:42 -0800 | [diff] [blame] | 715 | grpc_ssl_roots_override_result ovrd_res = GRPC_SSL_ROOTS_OVERRIDE_FAIL; |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 716 | if (GRPC_SLICE_IS_EMPTY(result) && ssl_roots_override_cb != NULL) { |
Julien Boeuf | aaebf7a | 2016-01-28 17:04:42 -0800 | [diff] [blame] | 717 | char *pem_root_certs = NULL; |
| 718 | ovrd_res = ssl_roots_override_cb(&pem_root_certs); |
| 719 | if (ovrd_res == GRPC_SSL_ROOTS_OVERRIDE_OK) { |
| 720 | GPR_ASSERT(pem_root_certs != NULL); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 721 | result = grpc_slice_new(pem_root_certs, strlen(pem_root_certs), gpr_free); |
Julien Boeuf | aaebf7a | 2016-01-28 17:04:42 -0800 | [diff] [blame] | 722 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 723 | } |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 724 | |
| 725 | /* Fall back to installed certs if needed. */ |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 726 | if (GRPC_SLICE_IS_EMPTY(result) && |
Julien Boeuf | aaebf7a | 2016-01-28 17:04:42 -0800 | [diff] [blame] | 727 | ovrd_res != GRPC_SSL_ROOTS_OVERRIDE_FAIL_PERMANENTLY) { |
Craig Tiller | 4727b9b | 2016-05-17 17:19:19 -0700 | [diff] [blame] | 728 | GRPC_LOG_IF_ERROR("load_file", |
Craig Tiller | 8517886 | 2016-05-18 16:09:16 -0700 | [diff] [blame] | 729 | grpc_load_file(installed_roots_path, 0, &result)); |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 730 | } |
| 731 | return result; |
| 732 | } |
| 733 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 734 | static grpc_slice default_pem_root_certs; |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 735 | |
| 736 | static void init_default_pem_root_certs(void) { |
| 737 | default_pem_root_certs = compute_default_pem_root_certs_once(); |
| 738 | } |
| 739 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 740 | grpc_slice grpc_get_default_ssl_roots_for_testing(void) { |
Julien Boeuf | 373debd | 2016-01-27 15:41:12 -0800 | [diff] [blame] | 741 | return compute_default_pem_root_certs_once(); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 744 | static tsi_client_certificate_request_type |
| 745 | get_tsi_client_certificate_request_type( |
| 746 | grpc_ssl_client_certificate_request_type grpc_request_type) { |
| 747 | switch (grpc_request_type) { |
| 748 | case GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE: |
| 749 | return TSI_DONT_REQUEST_CLIENT_CERTIFICATE; |
| 750 | |
| 751 | case GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY: |
| 752 | return TSI_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY; |
| 753 | |
| 754 | case GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY: |
| 755 | return TSI_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY; |
| 756 | |
| 757 | case GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY: |
| 758 | return TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY; |
| 759 | |
| 760 | case GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY: |
| 761 | return TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY; |
| 762 | |
| 763 | default: |
| 764 | // Is this a sane default |
| 765 | return TSI_DONT_REQUEST_CLIENT_CERTIFICATE; |
| 766 | } |
| 767 | } |
| 768 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 769 | size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs) { |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 770 | /* TODO(jboeuf@google.com): Maybe revisit the approach which consists in |
| 771 | loading all the roots once for the lifetime of the process. */ |
| 772 | static gpr_once once = GPR_ONCE_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 773 | gpr_once_init(&once, init_default_pem_root_certs); |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 774 | *pem_root_certs = GRPC_SLICE_START_PTR(default_pem_root_certs); |
| 775 | return GRPC_SLICE_LENGTH(default_pem_root_certs); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 778 | grpc_security_status grpc_ssl_channel_security_connector_create( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 779 | grpc_exec_ctx *exec_ctx, grpc_call_credentials *request_metadata_creds, |
Craig Tiller | b113649 | 2015-11-18 11:30:17 -0800 | [diff] [blame] | 780 | const grpc_ssl_config *config, const char *target_name, |
| 781 | const char *overridden_target_name, grpc_channel_security_connector **sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 782 | size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions(); |
| 783 | const unsigned char **alpn_protocol_strings = |
| 784 | gpr_malloc(sizeof(const char *) * num_alpn_protocols); |
| 785 | unsigned char *alpn_protocol_string_lengths = |
| 786 | gpr_malloc(sizeof(unsigned char) * num_alpn_protocols); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 787 | tsi_result result = TSI_OK; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 788 | grpc_ssl_channel_security_connector *c; |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 789 | size_t i; |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 790 | const unsigned char *pem_root_certs; |
| 791 | size_t pem_root_certs_size; |
Julien Boeuf | 7fa3f41 | 2015-02-24 19:30:41 -0800 | [diff] [blame] | 792 | char *port; |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 793 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 794 | for (i = 0; i < num_alpn_protocols; i++) { |
| 795 | alpn_protocol_strings[i] = |
| 796 | (const unsigned char *)grpc_chttp2_get_alpn_version_index(i); |
| 797 | alpn_protocol_string_lengths[i] = |
| 798 | (unsigned char)strlen(grpc_chttp2_get_alpn_version_index(i)); |
| 799 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 800 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 801 | if (config == NULL || target_name == NULL) { |
| 802 | gpr_log(GPR_ERROR, "An ssl channel needs a config and a target name."); |
| 803 | goto error; |
| 804 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 805 | if (config->pem_root_certs == NULL) { |
| 806 | pem_root_certs_size = grpc_get_default_ssl_roots(&pem_root_certs); |
| 807 | if (pem_root_certs == NULL || pem_root_certs_size == 0) { |
| 808 | gpr_log(GPR_ERROR, "Could not get default pem root certs."); |
yang-g | 46f2d34 | 2015-08-24 10:43:51 -0700 | [diff] [blame] | 809 | goto error; |
| 810 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 811 | } else { |
| 812 | pem_root_certs = config->pem_root_certs; |
| 813 | pem_root_certs_size = config->pem_root_certs_size; |
| 814 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 815 | |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 816 | c = gpr_zalloc(sizeof(grpc_ssl_channel_security_connector)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 817 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 818 | gpr_ref_init(&c->base.base.refcount, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 819 | c->base.base.vtable = &ssl_channel_vtable; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 820 | c->base.base.url_scheme = GRPC_SSL_URL_SCHEME; |
Julien Boeuf | 441176d | 2015-10-09 21:14:07 -0700 | [diff] [blame] | 821 | c->base.request_metadata_creds = |
| 822 | grpc_call_credentials_ref(request_metadata_creds); |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 823 | c->base.check_call_host = ssl_channel_check_call_host; |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 824 | c->base.add_handshakers = ssl_channel_add_handshakers; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 825 | gpr_split_host_port(target_name, &c->target_name, &port); |
| 826 | gpr_free(port); |
| 827 | if (overridden_target_name != NULL) { |
| 828 | c->overridden_target_name = gpr_strdup(overridden_target_name); |
| 829 | } |
| 830 | result = tsi_create_ssl_client_handshaker_factory( |
| 831 | config->pem_private_key, config->pem_private_key_size, |
| 832 | config->pem_cert_chain, config->pem_cert_chain_size, pem_root_certs, |
| 833 | pem_root_certs_size, ssl_cipher_suites(), alpn_protocol_strings, |
| 834 | alpn_protocol_string_lengths, (uint16_t)num_alpn_protocols, |
| 835 | &c->handshaker_factory); |
| 836 | if (result != TSI_OK) { |
| 837 | gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", |
| 838 | tsi_result_to_string(result)); |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 839 | ssl_channel_destroy(exec_ctx, &c->base.base); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 840 | *sc = NULL; |
| 841 | goto error; |
| 842 | } |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 843 | *sc = &c->base; |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 844 | gpr_free((void *)alpn_protocol_strings); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 845 | gpr_free(alpn_protocol_string_lengths); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 846 | return GRPC_SECURITY_OK; |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 847 | |
| 848 | error: |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 849 | gpr_free((void *)alpn_protocol_strings); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 850 | gpr_free(alpn_protocol_string_lengths); |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 851 | return GRPC_SECURITY_ERROR; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 854 | grpc_security_status grpc_ssl_server_security_connector_create( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 855 | grpc_exec_ctx *exec_ctx, const grpc_ssl_server_config *config, |
| 856 | grpc_server_security_connector **sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 857 | size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions(); |
| 858 | const unsigned char **alpn_protocol_strings = |
| 859 | gpr_malloc(sizeof(const char *) * num_alpn_protocols); |
| 860 | unsigned char *alpn_protocol_string_lengths = |
| 861 | gpr_malloc(sizeof(unsigned char) * num_alpn_protocols); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 862 | tsi_result result = TSI_OK; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 863 | grpc_ssl_server_security_connector *c; |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 864 | size_t i; |
| 865 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 866 | for (i = 0; i < num_alpn_protocols; i++) { |
| 867 | alpn_protocol_strings[i] = |
| 868 | (const unsigned char *)grpc_chttp2_get_alpn_version_index(i); |
| 869 | alpn_protocol_string_lengths[i] = |
| 870 | (unsigned char)strlen(grpc_chttp2_get_alpn_version_index(i)); |
| 871 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 872 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 873 | if (config == NULL || config->num_key_cert_pairs == 0) { |
| 874 | gpr_log(GPR_ERROR, "An SSL server needs a key and a cert."); |
| 875 | goto error; |
| 876 | } |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 877 | c = gpr_zalloc(sizeof(grpc_ssl_server_security_connector)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 878 | |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 879 | gpr_ref_init(&c->base.base.refcount, 1); |
| 880 | c->base.base.url_scheme = GRPC_SSL_URL_SCHEME; |
| 881 | c->base.base.vtable = &ssl_server_vtable; |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 882 | result = tsi_create_ssl_server_handshaker_factory_ex( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 883 | (const unsigned char **)config->pem_private_keys, |
| 884 | config->pem_private_keys_sizes, |
| 885 | (const unsigned char **)config->pem_cert_chains, |
| 886 | config->pem_cert_chains_sizes, config->num_key_cert_pairs, |
| 887 | config->pem_root_certs, config->pem_root_certs_size, |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 888 | get_tsi_client_certificate_request_type( |
| 889 | config->client_certificate_request), |
| 890 | ssl_cipher_suites(), alpn_protocol_strings, alpn_protocol_string_lengths, |
| 891 | (uint16_t)num_alpn_protocols, &c->handshaker_factory); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 892 | if (result != TSI_OK) { |
| 893 | gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", |
| 894 | tsi_result_to_string(result)); |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 895 | ssl_server_destroy(exec_ctx, &c->base.base); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 896 | *sc = NULL; |
| 897 | goto error; |
| 898 | } |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 899 | c->base.add_handshakers = ssl_server_add_handshakers; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 900 | *sc = &c->base; |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 901 | gpr_free((void *)alpn_protocol_strings); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 902 | gpr_free(alpn_protocol_string_lengths); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 903 | return GRPC_SECURITY_OK; |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 904 | |
| 905 | error: |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 906 | gpr_free((void *)alpn_protocol_strings); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 907 | gpr_free(alpn_protocol_string_lengths); |
ctiller | 48b5a45 | 2014-12-10 08:43:47 -0800 | [diff] [blame] | 908 | return GRPC_SECURITY_ERROR; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 909 | } |