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> |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 41 | #include "src/core/lib/security/transport/handshake.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/support/string.h" |
| 43 | #include "src/core/lib/tsi/ssl_transport_security.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 45 | typedef struct { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 46 | grpc_channel_security_connector base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | tsi_ssl_handshaker_factory *handshaker_factory; |
| 48 | char *secure_peer_name; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 49 | } grpc_httpcli_ssl_channel_security_connector; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame^] | 51 | static void httpcli_ssl_destroy(grpc_exec_ctx *exec_ctx, |
| 52 | grpc_security_connector *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | grpc_httpcli_ssl_channel_security_connector *c = |
| 54 | (grpc_httpcli_ssl_channel_security_connector *)sc; |
| 55 | if (c->handshaker_factory != NULL) { |
| 56 | tsi_ssl_handshaker_factory_destroy(c->handshaker_factory); |
| 57 | } |
| 58 | if (c->secure_peer_name != NULL) gpr_free(c->secure_peer_name); |
| 59 | gpr_free(sc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 62 | static void httpcli_ssl_do_handshake(grpc_exec_ctx *exec_ctx, |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 63 | grpc_channel_security_connector *sc, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | grpc_endpoint *nonsecure_endpoint, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 65 | grpc_slice_buffer *read_buffer, |
Craig Tiller | 449c64b | 2016-06-13 16:26:50 -0700 | [diff] [blame] | 66 | gpr_timespec deadline, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | grpc_security_handshake_done_cb cb, |
| 68 | void *user_data) { |
| 69 | grpc_httpcli_ssl_channel_security_connector *c = |
| 70 | (grpc_httpcli_ssl_channel_security_connector *)sc; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | tsi_result result = TSI_OK; |
Julien Boeuf | db5282b | 2015-08-21 15:23:52 -0700 | [diff] [blame] | 72 | tsi_handshaker *handshaker; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 73 | if (c->handshaker_factory == NULL) { |
Mark D. Roth | 7d9f276 | 2016-08-04 11:06:49 -0700 | [diff] [blame] | 74 | gpr_free(read_buffer); |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 75 | cb(exec_ctx, user_data, GRPC_SECURITY_ERROR, NULL, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | result = tsi_ssl_handshaker_factory_create_handshaker( |
| 79 | c->handshaker_factory, c->secure_peer_name, &handshaker); |
| 80 | if (result != TSI_OK) { |
| 81 | gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.", |
| 82 | tsi_result_to_string(result)); |
Mark D. Roth | 7d9f276 | 2016-08-04 11:06:49 -0700 | [diff] [blame] | 83 | gpr_free(read_buffer); |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 84 | cb(exec_ctx, user_data, GRPC_SECURITY_ERROR, NULL, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | } else { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 86 | grpc_do_security_handshake(exec_ctx, handshaker, &sc->base, true, |
Mark D. Roth | 7d9f276 | 2016-08-04 11:06:49 -0700 | [diff] [blame] | 87 | nonsecure_endpoint, read_buffer, deadline, cb, |
| 88 | user_data); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 92 | static void httpcli_ssl_check_peer(grpc_exec_ctx *exec_ctx, |
Craig Tiller | be52c6e | 2016-01-04 15:35:26 -0800 | [diff] [blame] | 93 | grpc_security_connector *sc, tsi_peer peer, |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 94 | grpc_security_peer_check_cb cb, |
| 95 | void *user_data) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | grpc_httpcli_ssl_channel_security_connector *c = |
| 97 | (grpc_httpcli_ssl_channel_security_connector *)sc; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 98 | grpc_security_status status = GRPC_SECURITY_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | |
| 100 | /* Check the peer name. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | if (c->secure_peer_name != NULL && |
| 102 | !tsi_ssl_peer_matches_name(&peer, c->secure_peer_name)) { |
| 103 | gpr_log(GPR_ERROR, "Peer name %s is not in peer certificate", |
| 104 | c->secure_peer_name); |
| 105 | status = GRPC_SECURITY_ERROR; |
| 106 | } |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 107 | cb(exec_ctx, user_data, status, NULL); |
Julien Boeuf | 1d9ac66 | 2015-12-17 21:35:47 -0800 | [diff] [blame] | 108 | tsi_peer_destruct(&peer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 111 | static grpc_security_connector_vtable httpcli_ssl_vtable = { |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 112 | httpcli_ssl_destroy, httpcli_ssl_check_peer}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | static grpc_security_status httpcli_ssl_channel_security_connector_create( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame^] | 115 | grpc_exec_ctx *exec_ctx, const unsigned char *pem_root_certs, |
| 116 | size_t pem_root_certs_size, const char *secure_peer_name, |
| 117 | grpc_channel_security_connector **sc) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | tsi_result result = TSI_OK; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 119 | grpc_httpcli_ssl_channel_security_connector *c; |
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 | if (secure_peer_name != NULL && pem_root_certs == NULL) { |
| 122 | gpr_log(GPR_ERROR, |
| 123 | "Cannot assert a secure peer name without a trust root."); |
| 124 | return GRPC_SECURITY_ERROR; |
| 125 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | c = gpr_malloc(sizeof(grpc_httpcli_ssl_channel_security_connector)); |
| 128 | memset(c, 0, sizeof(grpc_httpcli_ssl_channel_security_connector)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | gpr_ref_init(&c->base.base.refcount, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | c->base.base.vtable = &httpcli_ssl_vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | if (secure_peer_name != NULL) { |
| 133 | c->secure_peer_name = gpr_strdup(secure_peer_name); |
| 134 | } |
| 135 | result = tsi_create_ssl_client_handshaker_factory( |
| 136 | NULL, 0, NULL, 0, pem_root_certs, pem_root_certs_size, NULL, NULL, NULL, |
| 137 | 0, &c->handshaker_factory); |
| 138 | if (result != TSI_OK) { |
| 139 | gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.", |
| 140 | tsi_result_to_string(result)); |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame^] | 141 | httpcli_ssl_destroy(exec_ctx, &c->base.base); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 142 | *sc = NULL; |
| 143 | return GRPC_SECURITY_ERROR; |
| 144 | } |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 145 | c->base.do_handshake = httpcli_ssl_do_handshake; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 146 | *sc = &c->base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | return GRPC_SECURITY_OK; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 148 | } |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 149 | |
| 150 | /* handshaker */ |
| 151 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | typedef struct { |
| 153 | void (*func)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 154 | void *arg; |
| 155 | } on_done_closure; |
| 156 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | static void on_secure_transport_setup_done(grpc_exec_ctx *exec_ctx, void *rp, |
| 158 | grpc_security_status status, |
Julien Boeuf | 366f42c | 2015-12-16 22:05:46 -0800 | [diff] [blame] | 159 | grpc_endpoint *secure_endpoint, |
| 160 | grpc_auth_context *auth_context) { |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 161 | on_done_closure *c = rp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | if (status != GRPC_SECURITY_OK) { |
| 163 | gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status); |
| 164 | c->func(exec_ctx, c->arg, NULL); |
| 165 | } else { |
| 166 | c->func(exec_ctx, c->arg, secure_endpoint); |
| 167 | } |
| 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; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 188 | GPR_ASSERT(httpcli_ssl_channel_security_connector_create( |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame^] | 189 | exec_ctx, pem_root_certs, pem_root_certs_size, host, &sc) == |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | GRPC_SECURITY_OK); |
Julien Boeuf | 4f4d37c | 2016-02-24 22:07:36 -0800 | [diff] [blame] | 191 | grpc_channel_security_connector_do_handshake( |
Mark D. Roth | 7d9f276 | 2016-08-04 11:06:49 -0700 | [diff] [blame] | 192 | exec_ctx, sc, tcp, NULL, deadline, on_secure_transport_setup_done, c); |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame^] | 193 | GRPC_SECURITY_CONNECTOR_UNREF(exec_ctx, &sc->base, "httpcli"); |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | const grpc_httpcli_handshaker grpc_httpcli_ssl = {"https", ssl_handshake}; |