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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/http/httpcli.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 40 | #include <grpc/support/string_util.h> |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 41 | |
| 42 | #include "src/core/lib/channel/channel_args.h" |
Mark D. Roth | 071cacf | 2016-11-17 13:17:56 -0800 | [diff] [blame] | 43 | #include "src/core/lib/security/transport/security_handshaker.h" |
Craig Tiller | 6822a7a | 2016-12-06 19:28:52 -0800 | [diff] [blame] | 44 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/support/string.h" |
| 46 | #include "src/core/lib/tsi/ssl_transport_security.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | typedef struct { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 49 | grpc_channel_security_connector base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | tsi_ssl_handshaker_factory *handshaker_factory; |
| 51 | char *secure_peer_name; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 52 | } grpc_httpcli_ssl_channel_security_connector; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
Craig Tiller | b6821f6 | 2016-12-06 15:10:42 -0800 | [diff] [blame] | 54 | static void httpcli_ssl_destroy(grpc_exec_ctx *exec_ctx, |
| 55 | grpc_security_connector *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | grpc_httpcli_ssl_channel_security_connector *c = |
| 57 | (grpc_httpcli_ssl_channel_security_connector *)sc; |
| 58 | if (c->handshaker_factory != NULL) { |
| 59 | tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); |
| 60 | } |
| 61 | if (c->secure_peer_name != NULL) gpr_free(c->secure_peer_name); |
| 62 | gpr_free(sc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Mark D. Roth | d9ef283 | 2016-12-06 07:37:27 -0800 | [diff] [blame] | 65 | static void httpcli_ssl_add_handshakers(grpc_exec_ctx *exec_ctx, |
| 66 | grpc_channel_security_connector *sc, |
| 67 | grpc_handshake_manager *handshake_mgr) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | grpc_httpcli_ssl_channel_security_connector *c = |
| 69 | (grpc_httpcli_ssl_channel_security_connector *)sc; |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 70 | tsi_handshaker *handshaker = NULL; |
| 71 | if (c->handshaker_factory != NULL) { |
| 72 | tsi_result result = tsi_ssl_handshaker_factory_create_handshaker( |
| 73 | c->handshaker_factory, c->secure_peer_name, &handshaker); |
| 74 | if (result != TSI_OK) { |
| 75 | gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.", |
| 76 | tsi_result_to_string(result)); |
| 77 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | } |
Mark D. Roth | d095340 | 2016-12-07 07:56:48 -0800 | [diff] [blame] | 79 | grpc_handshake_manager_add( |
| 80 | handshake_mgr, |
| 81 | grpc_security_handshaker_create(exec_ctx, handshaker, &sc->base)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 84 | static void httpcli_ssl_check_peer(grpc_exec_ctx *exec_ctx, |
Craig Tiller | be52c6e | 2016-01-04 15:35:26 -0800 | [diff] [blame] | 85 | grpc_security_connector *sc, tsi_peer peer, |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 86 | grpc_auth_context **auth_context, |
| 87 | grpc_closure *on_peer_checked) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | grpc_httpcli_ssl_channel_security_connector *c = |
| 89 | (grpc_httpcli_ssl_channel_security_connector *)sc; |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 90 | grpc_error *error = GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | |
| 92 | /* Check the peer name. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | if (c->secure_peer_name != NULL && |
| 94 | !tsi_ssl_peer_matches_name(&peer, c->secure_peer_name)) { |
Mark D. Roth | 3beb6c5 | 2016-11-29 11:25:12 -0800 | [diff] [blame] | 95 | char *msg; |
| 96 | gpr_asprintf(&msg, "Peer name %s is not in peer certificate", |
| 97 | c->secure_peer_name); |
| 98 | error = GRPC_ERROR_CREATE(msg); |
| 99 | gpr_free(msg); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 101 | grpc_closure_sched(exec_ctx, on_peer_checked, error); |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 102 | tsi_peer_destruct(&peer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 105 | static grpc_security_connector_vtable httpcli_ssl_vtable = { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 106 | httpcli_ssl_destroy, httpcli_ssl_check_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | static grpc_security_status httpcli_ssl_channel_security_connector_create( |
Craig Tiller | b6821f6 | 2016-12-06 15:10:42 -0800 | [diff] [blame] | 109 | grpc_exec_ctx *exec_ctx, const unsigned char *pem_root_certs, |
| 110 | size_t pem_root_certs_size, const char *secure_peer_name, |
| 111 | grpc_channel_security_connector **sc) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | tsi_result result = TSI_OK; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 113 | grpc_httpcli_ssl_channel_security_connector *c; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | if (secure_peer_name != NULL && pem_root_certs == NULL) { |
| 116 | gpr_log(GPR_ERROR, |
| 117 | "Cannot assert a secure peer name without a trust root."); |
| 118 | return GRPC_SECURITY_ERROR; |
| 119 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | c = gpr_malloc(sizeof(grpc_httpcli_ssl_channel_security_connector)); |
| 122 | memset(c, 0, sizeof(grpc_httpcli_ssl_channel_security_connector)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | gpr_ref_init(&c->base.base.refcount, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | c->base.base.vtable = &httpcli_ssl_vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | if (secure_peer_name != NULL) { |
| 127 | c->secure_peer_name = gpr_strdup(secure_peer_name); |
| 128 | } |
| 129 | result = tsi_create_ssl_client_handshaker_factory( |
| 130 | NULL, 0, NULL, 0, pem_root_certs, pem_root_certs_size, NULL, NULL, NULL, |
| 131 | 0, &c->handshaker_factory); |
| 132 | if (result != TSI_OK) { |
| 133 | gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", |
| 134 | tsi_result_to_string(result)); |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 135 | httpcli_ssl_destroy(exec_ctx, &c->base.base); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | *sc = NULL; |
| 137 | return GRPC_SECURITY_ERROR; |
| 138 | } |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 139 | c->base.add_handshakers = httpcli_ssl_add_handshakers; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 140 | *sc = &c->base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | return GRPC_SECURITY_OK; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 142 | } |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 143 | |
| 144 | /* handshaker */ |
| 145 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | typedef struct { |
| 147 | void (*func)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 148 | void *arg; |
Mark D. Roth | bd913a8 | 2016-12-02 16:47:35 +0000 | [diff] [blame] | 149 | grpc_handshake_manager *handshake_mgr; |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 150 | } on_done_closure; |
| 151 | |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 152 | static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg, |
| 153 | grpc_error *error) { |
| 154 | grpc_handshaker_args *args = arg; |
| 155 | on_done_closure *c = args->user_data; |
| 156 | if (error != GRPC_ERROR_NONE) { |
Mark D. Roth | a561785 | 2016-12-01 08:48:22 -0800 | [diff] [blame] | 157 | const char *msg = grpc_error_string(error); |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 158 | gpr_log(GPR_ERROR, "Secure transport setup failed: %s", msg); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 159 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | c->func(exec_ctx, c->arg, NULL); |
| 161 | } else { |
Craig Tiller | 397bff3 | 2016-12-06 15:05:59 -0800 | [diff] [blame] | 162 | grpc_channel_args_destroy(exec_ctx, args->args); |
Craig Tiller | 6822a7a | 2016-12-06 19:28:52 -0800 | [diff] [blame] | 163 | grpc_slice_buffer_destroy_internal(exec_ctx, args->read_buffer); |
Mark D. Roth | bd913a8 | 2016-12-02 16:47:35 +0000 | [diff] [blame] | 164 | gpr_free(args->read_buffer); |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 165 | c->func(exec_ctx, c->arg, args->endpoint); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 166 | } |
Mark D. Roth | bd913a8 | 2016-12-02 16:47:35 +0000 | [diff] [blame] | 167 | grpc_handshake_manager_destroy(exec_ctx, c->handshake_mgr); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | gpr_free(c); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | static void ssl_handshake(grpc_exec_ctx *exec_ctx, void *arg, |
| 172 | grpc_endpoint *tcp, const char *host, |
Craig Tiller | 449c64b | 2016-06-13 16:26:50 -0700 | [diff] [blame] | 173 | gpr_timespec deadline, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 174 | void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg, |
| 175 | grpc_endpoint *endpoint)) { |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 176 | grpc_channel_security_connector *sc = NULL; |
| 177 | const unsigned char *pem_root_certs = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 178 | on_done_closure *c = gpr_malloc(sizeof(*c)); |
| 179 | size_t pem_root_certs_size = grpc_get_default_ssl_roots(&pem_root_certs); |
| 180 | if (pem_root_certs == NULL || pem_root_certs_size == 0) { |
| 181 | gpr_log(GPR_ERROR, "Could not get default pem root certs."); |
| 182 | on_done(exec_ctx, arg, NULL); |
| 183 | gpr_free(c); |
| 184 | return; |
| 185 | } |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 186 | c->func = on_done; |
| 187 | c->arg = arg; |
Mark D. Roth | bd913a8 | 2016-12-02 16:47:35 +0000 | [diff] [blame] | 188 | c->handshake_mgr = grpc_handshake_manager_create(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | GPR_ASSERT(httpcli_ssl_channel_security_connector_create( |
Craig Tiller | b6821f6 | 2016-12-06 15:10:42 -0800 | [diff] [blame] | 190 | exec_ctx, pem_root_certs, pem_root_certs_size, host, &sc) == |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 191 | GRPC_SECURITY_OK); |
Mark D. Roth | 65b79c8 | 2016-12-06 07:20:20 -0800 | [diff] [blame] | 192 | grpc_channel_security_connector_add_handshakers(exec_ctx, sc, |
| 193 | c->handshake_mgr); |
Mark D. Roth | 963be37 | 2016-11-16 14:17:06 -0800 | [diff] [blame] | 194 | grpc_handshake_manager_do_handshake( |
Mark D. Roth | bd913a8 | 2016-12-02 16:47:35 +0000 | [diff] [blame] | 195 | exec_ctx, c->handshake_mgr, tcp, NULL /* channel_args */, deadline, |
| 196 | NULL /* acceptor */, on_handshake_done, c /* user_data */); |
Craig Tiller | b6821f6 | 2016-12-06 15:10:42 -0800 | [diff] [blame] | 197 | GRPC_SECURITY_CONNECTOR_UNREF(exec_ctx, &sc->base, "httpcli"); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 200 | const grpc_httpcli_handshaker grpc_httpcli_ssl = {"https", ssl_handshake}; |