blob: f4f6f3c27aaae3e5a9f5ebbbe3873190b88b091c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/http/httpcli.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36#include <string.h>
37
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <grpc/support/alloc.h>
39#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070040#include <grpc/support/string_util.h>
Mark D. Roth963be372016-11-16 14:17:06 -080041
42#include "src/core/lib/channel/channel_args.h"
Mark D. Roth071cacf2016-11-17 13:17:56 -080043#include "src/core/lib/security/transport/security_handshaker.h"
Craig Tiller6822a7a2016-12-06 19:28:52 -080044#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/support/string.h"
46#include "src/core/lib/tsi/ssl_transport_security.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tillera82950e2015-09-22 12:33:20 -070048typedef struct {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070049 grpc_channel_security_connector base;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050 tsi_ssl_handshaker_factory *handshaker_factory;
51 char *secure_peer_name;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070052} grpc_httpcli_ssl_channel_security_connector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Craig Tillerb6821f62016-12-06 15:10:42 -080054static void httpcli_ssl_destroy(grpc_exec_ctx *exec_ctx,
55 grpc_security_connector *sc) {
Craig Tillera82950e2015-09-22 12:33:20 -070056 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 Nobleb7ebd3b2014-11-26 16:33:03 -080063}
64
Mark D. Rothd9ef2832016-12-06 07:37:27 -080065static void httpcli_ssl_add_handshakers(grpc_exec_ctx *exec_ctx,
66 grpc_channel_security_connector *sc,
67 grpc_handshake_manager *handshake_mgr) {
Craig Tillera82950e2015-09-22 12:33:20 -070068 grpc_httpcli_ssl_channel_security_connector *c =
69 (grpc_httpcli_ssl_channel_security_connector *)sc;
Mark D. Roth963be372016-11-16 14:17:06 -080070 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 Tillera82950e2015-09-22 12:33:20 -070078 }
Mark D. Rothd0953402016-12-07 07:56:48 -080079 grpc_handshake_manager_add(
80 handshake_mgr,
81 grpc_security_handshaker_create(exec_ctx, handshaker, &sc->base));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082}
83
Julien Boeuf366f42c2015-12-16 22:05:46 -080084static void httpcli_ssl_check_peer(grpc_exec_ctx *exec_ctx,
Craig Tillerbe52c6e2016-01-04 15:35:26 -080085 grpc_security_connector *sc, tsi_peer peer,
Mark D. Roth3beb6c52016-11-29 11:25:12 -080086 grpc_auth_context **auth_context,
87 grpc_closure *on_peer_checked) {
Craig Tillera82950e2015-09-22 12:33:20 -070088 grpc_httpcli_ssl_channel_security_connector *c =
89 (grpc_httpcli_ssl_channel_security_connector *)sc;
Mark D. Roth3beb6c52016-11-29 11:25:12 -080090 grpc_error *error = GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091
92 /* Check the peer name. */
Craig Tillera82950e2015-09-22 12:33:20 -070093 if (c->secure_peer_name != NULL &&
94 !tsi_ssl_peer_matches_name(&peer, c->secure_peer_name)) {
Mark D. Roth3beb6c52016-11-29 11:25:12 -080095 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 Tillera82950e2015-09-22 12:33:20 -0700100 }
Craig Tiller91031da2016-12-28 15:44:25 -0800101 grpc_closure_sched(exec_ctx, on_peer_checked, error);
Julien Boeuf1d9ac662015-12-17 21:35:47 -0800102 tsi_peer_destruct(&peer);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103}
104
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700105static grpc_security_connector_vtable httpcli_ssl_vtable = {
Julien Boeuf4f4d37c2016-02-24 22:07:36 -0800106 httpcli_ssl_destroy, httpcli_ssl_check_peer};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107
Craig Tillera82950e2015-09-22 12:33:20 -0700108static grpc_security_status httpcli_ssl_channel_security_connector_create(
Craig Tillerb6821f62016-12-06 15:10:42 -0800109 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800112 tsi_result result = TSI_OK;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700113 grpc_httpcli_ssl_channel_security_connector *c;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114
Craig Tillera82950e2015-09-22 12:33:20 -0700115 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800120
Craig Tillera82950e2015-09-22 12:33:20 -0700121 c = gpr_malloc(sizeof(grpc_httpcli_ssl_channel_security_connector));
122 memset(c, 0, sizeof(grpc_httpcli_ssl_channel_security_connector));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123
Craig Tillera82950e2015-09-22 12:33:20 -0700124 gpr_ref_init(&c->base.base.refcount, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125 c->base.base.vtable = &httpcli_ssl_vtable;
Craig Tillera82950e2015-09-22 12:33:20 -0700126 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 Tillerbd1795c2016-10-31 15:30:00 -0700135 httpcli_ssl_destroy(exec_ctx, &c->base.base);
Craig Tillera82950e2015-09-22 12:33:20 -0700136 *sc = NULL;
137 return GRPC_SECURITY_ERROR;
138 }
Mark D. Roth65b79c82016-12-06 07:20:20 -0800139 c->base.add_handshakers = httpcli_ssl_add_handshakers;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700140 *sc = &c->base;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 return GRPC_SECURITY_OK;
Craig Tiller190d3602015-02-18 09:23:38 -0800142}
Craig Tillerf53d9c82015-08-04 14:19:43 -0700143
144/* handshaker */
145
Craig Tillera82950e2015-09-22 12:33:20 -0700146typedef struct {
147 void (*func)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint);
Craig Tillerf53d9c82015-08-04 14:19:43 -0700148 void *arg;
Mark D. Rothbd913a82016-12-02 16:47:35 +0000149 grpc_handshake_manager *handshake_mgr;
Craig Tillerf53d9c82015-08-04 14:19:43 -0700150} on_done_closure;
151
Mark D. Roth963be372016-11-16 14:17:06 -0800152static 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. Rotha5617852016-12-01 08:48:22 -0800157 const char *msg = grpc_error_string(error);
Mark D. Roth963be372016-11-16 14:17:06 -0800158 gpr_log(GPR_ERROR, "Secure transport setup failed: %s", msg);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800159
Craig Tillera82950e2015-09-22 12:33:20 -0700160 c->func(exec_ctx, c->arg, NULL);
161 } else {
Craig Tiller397bff32016-12-06 15:05:59 -0800162 grpc_channel_args_destroy(exec_ctx, args->args);
Craig Tiller6822a7a2016-12-06 19:28:52 -0800163 grpc_slice_buffer_destroy_internal(exec_ctx, args->read_buffer);
Mark D. Rothbd913a82016-12-02 16:47:35 +0000164 gpr_free(args->read_buffer);
Mark D. Roth963be372016-11-16 14:17:06 -0800165 c->func(exec_ctx, c->arg, args->endpoint);
Craig Tillera82950e2015-09-22 12:33:20 -0700166 }
Mark D. Rothbd913a82016-12-02 16:47:35 +0000167 grpc_handshake_manager_destroy(exec_ctx, c->handshake_mgr);
Craig Tillera82950e2015-09-22 12:33:20 -0700168 gpr_free(c);
Craig Tillerf53d9c82015-08-04 14:19:43 -0700169}
170
Craig Tillera82950e2015-09-22 12:33:20 -0700171static void ssl_handshake(grpc_exec_ctx *exec_ctx, void *arg,
172 grpc_endpoint *tcp, const char *host,
Craig Tiller449c64b2016-06-13 16:26:50 -0700173 gpr_timespec deadline,
Craig Tillera82950e2015-09-22 12:33:20 -0700174 void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg,
175 grpc_endpoint *endpoint)) {
Craig Tillerf53d9c82015-08-04 14:19:43 -0700176 grpc_channel_security_connector *sc = NULL;
177 const unsigned char *pem_root_certs = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700178 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 Tillerf53d9c82015-08-04 14:19:43 -0700186 c->func = on_done;
187 c->arg = arg;
Mark D. Rothbd913a82016-12-02 16:47:35 +0000188 c->handshake_mgr = grpc_handshake_manager_create();
Craig Tillera82950e2015-09-22 12:33:20 -0700189 GPR_ASSERT(httpcli_ssl_channel_security_connector_create(
Craig Tillerb6821f62016-12-06 15:10:42 -0800190 exec_ctx, pem_root_certs, pem_root_certs_size, host, &sc) ==
Craig Tillera82950e2015-09-22 12:33:20 -0700191 GRPC_SECURITY_OK);
Mark D. Roth65b79c82016-12-06 07:20:20 -0800192 grpc_channel_security_connector_add_handshakers(exec_ctx, sc,
193 c->handshake_mgr);
Mark D. Roth963be372016-11-16 14:17:06 -0800194 grpc_handshake_manager_do_handshake(
Mark D. Rothbd913a82016-12-02 16:47:35 +0000195 exec_ctx, c->handshake_mgr, tcp, NULL /* channel_args */, deadline,
196 NULL /* acceptor */, on_handshake_done, c /* user_data */);
Craig Tillerb6821f62016-12-06 15:10:42 -0800197 GRPC_SECURITY_CONNECTOR_UNREF(exec_ctx, &sc->base, "httpcli");
Craig Tillerf53d9c82015-08-04 14:19:43 -0700198}
199
Craig Tillera82950e2015-09-22 12:33:20 -0700200const grpc_httpcli_handshaker grpc_httpcli_ssl = {"https", ssl_handshake};