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 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 34 | #include "src/core/lib/tsi/ssl_transport_security.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 36 | #include <grpc/support/port_platform.h> |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <limits.h> |
Julien Boeuf | 330f4c8 | 2015-04-27 10:37:42 -0700 | [diff] [blame] | 39 | #include <string.h> |
Paul Querna | 27df689 | 2016-03-13 13:34:27 -0700 | [diff] [blame] | 40 | |
Nicolas "Pixel" Noble | e3df5a0 | 2016-10-27 00:41:13 +0200 | [diff] [blame] | 41 | /* TODO(jboeuf): refactor inet_ntop into a portability header. */ |
| 42 | /* Note: for whomever reads this and tries to refactor this, this |
| 43 | can't be in grpc, it has to be in gpr. */ |
| 44 | #ifdef GPR_WINDOWS |
| 45 | #include <ws2tcpip.h> |
| 46 | #else |
| 47 | #include <arpa/inet.h> |
| 48 | #endif |
| 49 | |
Nicolas "Pixel" Noble | b29d8cf | 2016-04-08 01:38:29 +0200 | [diff] [blame] | 50 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | #include <grpc/support/log.h> |
nnoble | 8a23a3d | 2014-12-16 10:10:29 -0800 | [diff] [blame] | 52 | #include <grpc/support/sync.h> |
Julien Boeuf | 4a0a394 | 2015-02-03 15:04:45 -0800 | [diff] [blame] | 53 | #include <grpc/support/thd.h> |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 54 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | |
| 56 | #include <openssl/bio.h> |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | #include <openssl/crypto.h> /* For OPENSSL_free */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | #include <openssl/err.h> |
| 59 | #include <openssl/ssl.h> |
| 60 | #include <openssl/x509.h> |
| 61 | #include <openssl/x509v3.h> |
| 62 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 63 | #include "src/core/lib/tsi/ssl_types.h" |
| 64 | #include "src/core/lib/tsi/transport_security.h" |
Craig Tiller | 0fe5ee7 | 2015-12-22 12:50:36 -0800 | [diff] [blame] | 65 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | /* --- Constants. ---*/ |
| 67 | |
| 68 | #define TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND 16384 |
| 69 | #define TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND 1024 |
| 70 | |
Julien Boeuf | 3e29de1 | 2015-06-18 23:29:13 +0200 | [diff] [blame] | 71 | /* Putting a macro like this and littering the source file with #if is really |
| 72 | bad practice. |
| 73 | TODO(jboeuf): refactor all the #if / #endif in a separate module. */ |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 74 | #ifndef TSI_OPENSSL_ALPN_SUPPORT |
| 75 | #define TSI_OPENSSL_ALPN_SUPPORT 1 |
| 76 | #endif |
| 77 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | /* TODO(jboeuf): I have not found a way to get this number dynamically from the |
Julien Boeuf | 3e29de1 | 2015-06-18 23:29:13 +0200 | [diff] [blame] | 79 | SSL structure. This is what we would ultimately want though... */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | #define TSI_SSL_MAX_PROTECTION_OVERHEAD 100 |
| 81 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | /* --- Structure definitions. ---*/ |
| 83 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | struct tsi_ssl_handshaker_factory { |
| 85 | tsi_result (*create_handshaker)(tsi_ssl_handshaker_factory *self, |
| 86 | const char *server_name_indication, |
| 87 | tsi_handshaker **handshaker); |
| 88 | void (*destroy)(tsi_ssl_handshaker_factory *self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | tsi_ssl_handshaker_factory base; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 93 | SSL_CTX *ssl_context; |
| 94 | unsigned char *alpn_protocol_list; |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 95 | size_t alpn_protocol_list_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | } tsi_ssl_client_handshaker_factory; |
| 97 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | tsi_ssl_handshaker_factory base; |
| 100 | |
| 101 | /* Several contexts to support SNI. |
| 102 | The tsi_peer array contains the subject names of the server certificates |
| 103 | associated with the contexts at the same index. */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 104 | SSL_CTX **ssl_contexts; |
| 105 | tsi_peer *ssl_context_x509_subject_names; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 106 | size_t ssl_context_count; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 107 | unsigned char *alpn_protocol_list; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 108 | size_t alpn_protocol_list_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | } tsi_ssl_server_handshaker_factory; |
| 110 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | tsi_handshaker base; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 113 | SSL *ssl; |
| 114 | BIO *into_ssl; |
| 115 | BIO *from_ssl; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | tsi_result result; |
| 117 | } tsi_ssl_handshaker; |
| 118 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | tsi_frame_protector base; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 121 | SSL *ssl; |
| 122 | BIO *into_ssl; |
| 123 | BIO *from_ssl; |
| 124 | unsigned char *buffer; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 125 | size_t buffer_size; |
| 126 | size_t buffer_offset; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | } tsi_ssl_frame_protector; |
| 128 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | /* --- Library Initialization. ---*/ |
| 130 | |
nnoble | 8a23a3d | 2014-12-16 10:10:29 -0800 | [diff] [blame] | 131 | static gpr_once init_openssl_once = GPR_ONCE_INIT; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 132 | static gpr_mu *openssl_mutexes = NULL; |
Julien Boeuf | 4a0a394 | 2015-02-03 15:04:45 -0800 | [diff] [blame] | 133 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | static void openssl_locking_cb(int mode, int type, const char *file, int line) { |
| 135 | if (mode & CRYPTO_LOCK) { |
| 136 | gpr_mu_lock(&openssl_mutexes[type]); |
| 137 | } else { |
| 138 | gpr_mu_unlock(&openssl_mutexes[type]); |
| 139 | } |
Julien Boeuf | 4a0a394 | 2015-02-03 15:04:45 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 142 | static unsigned long openssl_thread_id_cb(void) { |
| 143 | return (unsigned long)gpr_thd_currentid(); |
Julien Boeuf | 4a0a394 | 2015-02-03 15:04:45 -0800 | [diff] [blame] | 144 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | static void init_openssl(void) { |
Julien Boeuf | 4a0a394 | 2015-02-03 15:04:45 -0800 | [diff] [blame] | 147 | int i; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 148 | int num_locks; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | SSL_library_init(); |
| 150 | SSL_load_error_strings(); |
| 151 | OpenSSL_add_all_algorithms(); |
| 152 | num_locks = CRYPTO_num_locks(); |
| 153 | GPR_ASSERT(num_locks > 0); |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 154 | openssl_mutexes = gpr_malloc((size_t)num_locks * sizeof(gpr_mu)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | for (i = 0; i < CRYPTO_num_locks(); i++) { |
| 156 | gpr_mu_init(&openssl_mutexes[i]); |
| 157 | } |
| 158 | CRYPTO_set_locking_callback(openssl_locking_cb); |
| 159 | CRYPTO_set_id_callback(openssl_thread_id_cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* --- Ssl utils. ---*/ |
| 163 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 164 | static const char *ssl_error_string(int error) { |
| 165 | switch (error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | case SSL_ERROR_NONE: |
| 167 | return "SSL_ERROR_NONE"; |
| 168 | case SSL_ERROR_ZERO_RETURN: |
| 169 | return "SSL_ERROR_ZERO_RETURN"; |
| 170 | case SSL_ERROR_WANT_READ: |
| 171 | return "SSL_ERROR_WANT_READ"; |
| 172 | case SSL_ERROR_WANT_WRITE: |
| 173 | return "SSL_ERROR_WANT_WRITE"; |
| 174 | case SSL_ERROR_WANT_CONNECT: |
| 175 | return "SSL_ERROR_WANT_CONNECT"; |
| 176 | case SSL_ERROR_WANT_ACCEPT: |
| 177 | return "SSL_ERROR_WANT_ACCEPT"; |
| 178 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 179 | return "SSL_ERROR_WANT_X509_LOOKUP"; |
| 180 | case SSL_ERROR_SYSCALL: |
| 181 | return "SSL_ERROR_SYSCALL"; |
| 182 | case SSL_ERROR_SSL: |
| 183 | return "SSL_ERROR_SSL"; |
| 184 | default: |
| 185 | return "Unknown error"; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* TODO(jboeuf): Remove when we are past the debugging phase with this code. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | static void ssl_log_where_info(const SSL *ssl, int where, int flag, |
| 191 | const char *msg) { |
| 192 | if ((where & flag) && tsi_tracing_enabled) { |
| 193 | gpr_log(GPR_INFO, "%20.20s - %30.30s - %5.10s", msg, |
| 194 | SSL_state_string_long(ssl), SSL_state_string(ssl)); |
| 195 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Used for debugging. TODO(jboeuf): Remove when code is mature enough. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | static void ssl_info_callback(const SSL *ssl, int where, int ret) { |
| 200 | if (ret == 0) { |
| 201 | gpr_log(GPR_ERROR, "ssl_info_callback: error occured.\n"); |
| 202 | return; |
| 203 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | ssl_log_where_info(ssl, where, SSL_CB_LOOP, "LOOP"); |
| 206 | ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_START, "HANDSHAKE START"); |
| 207 | ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_DONE, "HANDSHAKE DONE"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Julien Boeuf | 0170a6c | 2015-02-24 18:08:01 -0800 | [diff] [blame] | 210 | /* Returns 1 if name looks like an IP address, 0 otherwise. |
Paul Querna | 4a9e7c4 | 2016-03-13 14:00:11 -0700 | [diff] [blame] | 211 | This is a very rough heuristic, and only handles IPv6 in hexadecimal form. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 212 | static int looks_like_ip_address(const char *name) { |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 213 | size_t i; |
| 214 | size_t dot_count = 0; |
| 215 | size_t num_size = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | for (i = 0; i < strlen(name); i++) { |
Paul Querna | 4a9e7c4 | 2016-03-13 14:00:11 -0700 | [diff] [blame] | 217 | if (name[i] == ':') { |
| 218 | /* IPv6 Address in hexadecimal form, : is not allowed in DNS names. */ |
| 219 | return 1; |
| 220 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 221 | if (name[i] >= '0' && name[i] <= '9') { |
| 222 | if (num_size > 3) return 0; |
| 223 | num_size++; |
| 224 | } else if (name[i] == '.') { |
| 225 | if (dot_count > 3 || num_size == 0) return 0; |
| 226 | dot_count++; |
| 227 | num_size = 0; |
| 228 | } else { |
| 229 | return 0; |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 230 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 231 | } |
| 232 | if (dot_count < 3 || num_size == 0) return 0; |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 233 | return 1; |
| 234 | } |
| 235 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | /* Gets the subject CN from an X509 cert. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | static tsi_result ssl_get_x509_common_name(X509 *cert, unsigned char **utf8, |
| 238 | size_t *utf8_size) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | int common_name_index = -1; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 240 | X509_NAME_ENTRY *common_name_entry = NULL; |
| 241 | ASN1_STRING *common_name_asn1 = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | X509_NAME *subject_name = X509_get_subject_name(cert); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | int utf8_returned_size = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | if (subject_name == NULL) { |
| 245 | gpr_log(GPR_ERROR, "Could not get subject name from certificate."); |
| 246 | return TSI_NOT_FOUND; |
| 247 | } |
| 248 | common_name_index = |
| 249 | X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1); |
| 250 | if (common_name_index == -1) { |
| 251 | gpr_log(GPR_ERROR, |
| 252 | "Could not get common name of subject from certificate."); |
| 253 | return TSI_NOT_FOUND; |
| 254 | } |
| 255 | common_name_entry = X509_NAME_get_entry(subject_name, common_name_index); |
| 256 | if (common_name_entry == NULL) { |
| 257 | gpr_log(GPR_ERROR, "Could not get common name entry from certificate."); |
| 258 | return TSI_INTERNAL_ERROR; |
| 259 | } |
| 260 | common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry); |
| 261 | if (common_name_asn1 == NULL) { |
| 262 | gpr_log(GPR_ERROR, |
| 263 | "Could not get common name entry asn1 from certificate."); |
| 264 | return TSI_INTERNAL_ERROR; |
| 265 | } |
| 266 | utf8_returned_size = ASN1_STRING_to_UTF8(utf8, common_name_asn1); |
| 267 | if (utf8_returned_size < 0) { |
| 268 | gpr_log(GPR_ERROR, "Could not extract utf8 from asn1 string."); |
| 269 | return TSI_OUT_OF_RESOURCES; |
| 270 | } |
| 271 | *utf8_size = (size_t)utf8_returned_size; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 272 | return TSI_OK; |
| 273 | } |
| 274 | |
| 275 | /* Gets the subject CN of an X509 cert as a tsi_peer_property. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | static tsi_result peer_property_from_x509_common_name( |
| 277 | X509 *cert, tsi_peer_property *property) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 278 | unsigned char *common_name; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 279 | size_t common_name_size; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | tsi_result result = |
| 281 | ssl_get_x509_common_name(cert, &common_name, &common_name_size); |
| 282 | if (result != TSI_OK) { |
| 283 | if (result == TSI_NOT_FOUND) { |
| 284 | common_name = NULL; |
| 285 | common_name_size = 0; |
| 286 | } else { |
| 287 | return result; |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 288 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | } |
| 290 | result = tsi_construct_string_peer_property( |
| 291 | TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, |
| 292 | common_name == NULL ? "" : (const char *)common_name, common_name_size, |
| 293 | property); |
| 294 | OPENSSL_free(common_name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 295 | return result; |
| 296 | } |
| 297 | |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 298 | /* Gets the X509 cert in PEM format as a tsi_peer_property. */ |
| 299 | static tsi_result add_pem_certificate(X509 *cert, tsi_peer_property *property) { |
| 300 | BIO *bio = BIO_new(BIO_s_mem()); |
| 301 | if (!PEM_write_bio_X509(bio, cert)) { |
| 302 | BIO_free(bio); |
| 303 | return TSI_INTERNAL_ERROR; |
| 304 | } |
| 305 | char *contents; |
| 306 | long len = BIO_get_mem_data(bio, &contents); |
| 307 | if (len <= 0) { |
| 308 | BIO_free(bio); |
| 309 | return TSI_INTERNAL_ERROR; |
| 310 | } |
| 311 | tsi_result result = tsi_construct_string_peer_property( |
| 312 | TSI_X509_PEM_CERT_PROPERTY, (const char *)contents, (size_t)len, |
| 313 | property); |
| 314 | BIO_free(bio); |
| 315 | return result; |
| 316 | } |
| 317 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 318 | /* Gets the subject SANs from an X509 cert as a tsi_peer_property. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | static tsi_result add_subject_alt_names_properties_to_peer( |
| 320 | tsi_peer *peer, GENERAL_NAMES *subject_alt_names, |
| 321 | size_t subject_alt_name_count) { |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 322 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 323 | tsi_result result = TSI_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 324 | |
| 325 | /* Reset for DNS entries filtering. */ |
Julien Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 326 | peer->property_count -= subject_alt_name_count; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 327 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 328 | for (i = 0; i < subject_alt_name_count; i++) { |
| 329 | GENERAL_NAME *subject_alt_name = |
Craig Tiller | 0fe5ee7 | 2015-12-22 12:50:36 -0800 | [diff] [blame] | 330 | sk_GENERAL_NAME_value(subject_alt_names, TSI_SIZE_AS_SIZE(i)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | /* Filter out the non-dns entries names. */ |
| 332 | if (subject_alt_name->type == GEN_DNS) { |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 333 | unsigned char *name = NULL; |
| 334 | int name_size; |
| 335 | name_size = ASN1_STRING_to_UTF8(&name, subject_alt_name->d.dNSName); |
| 336 | if (name_size < 0) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 337 | gpr_log(GPR_ERROR, "Could not get utf8 from asn1 string."); |
| 338 | result = TSI_INTERNAL_ERROR; |
| 339 | break; |
| 340 | } |
| 341 | result = tsi_construct_string_peer_property( |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 342 | TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, (const char *)name, |
| 343 | (size_t)name_size, &peer->properties[peer->property_count++]); |
| 344 | OPENSSL_free(name); |
| 345 | } else if (subject_alt_name->type == GEN_IPADD) { |
| 346 | char ntop_buf[INET6_ADDRSTRLEN]; |
| 347 | int af; |
| 348 | |
| 349 | if (subject_alt_name->d.iPAddress->length == 4) { |
| 350 | af = AF_INET; |
| 351 | } else if (subject_alt_name->d.iPAddress->length == 16) { |
| 352 | af = AF_INET6; |
| 353 | } else { |
| 354 | gpr_log(GPR_ERROR, "SAN IP Address contained invalid IP"); |
| 355 | result = TSI_INTERNAL_ERROR; |
| 356 | break; |
| 357 | } |
Nicolas "Pixel" Noble | e3df5a0 | 2016-10-27 00:41:13 +0200 | [diff] [blame] | 358 | const char *name = inet_ntop(af, subject_alt_name->d.iPAddress->data, |
| 359 | ntop_buf, INET6_ADDRSTRLEN); |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 360 | if (name == NULL) { |
| 361 | gpr_log(GPR_ERROR, "Could not get IP string from asn1 octet."); |
| 362 | result = TSI_INTERNAL_ERROR; |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | result = tsi_construct_string_peer_property_from_cstring( |
| 367 | TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, name, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | &peer->properties[peer->property_count++]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 369 | } |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 370 | if (result != TSI_OK) break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 371 | } |
Julien Boeuf | 77e8c1c | 2015-05-13 13:50:59 -0700 | [diff] [blame] | 372 | return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* Gets information about the peer's X509 cert as a tsi_peer object. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 376 | static tsi_result peer_from_x509(X509 *cert, int include_certificate_type, |
| 377 | tsi_peer *peer) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 378 | /* TODO(jboeuf): Maybe add more properties. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | GENERAL_NAMES *subject_alt_names = |
| 380 | X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0); |
Julien Boeuf | 01ac4f0 | 2015-10-09 16:08:24 -0700 | [diff] [blame] | 381 | int subject_alt_name_count = (subject_alt_names != NULL) |
| 382 | ? (int)sk_GENERAL_NAME_num(subject_alt_names) |
| 383 | : 0; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 384 | size_t property_count; |
| 385 | tsi_result result; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 386 | GPR_ASSERT(subject_alt_name_count >= 0); |
| 387 | property_count = (include_certificate_type ? (size_t)1 : 0) + |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 388 | 2 /* common name, certificate */ + |
| 389 | (size_t)subject_alt_name_count; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 390 | result = tsi_construct_peer(property_count, peer); |
| 391 | if (result != TSI_OK) return result; |
| 392 | do { |
| 393 | if (include_certificate_type) { |
| 394 | result = tsi_construct_string_peer_property_from_cstring( |
| 395 | TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, |
| 396 | &peer->properties[0]); |
| 397 | if (result != TSI_OK) break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 398 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 399 | result = peer_property_from_x509_common_name( |
| 400 | cert, &peer->properties[include_certificate_type ? 1 : 0]); |
| 401 | if (result != TSI_OK) break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 402 | |
Deepak Lukose | e61cbe3 | 2016-03-14 14:10:44 -0700 | [diff] [blame] | 403 | result = add_pem_certificate( |
| 404 | cert, &peer->properties[include_certificate_type ? 2 : 1]); |
| 405 | if (result != TSI_OK) break; |
| 406 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 407 | if (subject_alt_name_count != 0) { |
| 408 | result = add_subject_alt_names_properties_to_peer( |
| 409 | peer, subject_alt_names, (size_t)subject_alt_name_count); |
| 410 | if (result != TSI_OK) break; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 411 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 412 | } while (0); |
| 413 | |
| 414 | if (subject_alt_names != NULL) { |
| 415 | sk_GENERAL_NAME_pop_free(subject_alt_names, GENERAL_NAME_free); |
| 416 | } |
| 417 | if (result != TSI_OK) tsi_peer_destruct(peer); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 418 | return result; |
| 419 | } |
| 420 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 421 | /* Logs the SSL error stack. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 422 | static void log_ssl_error_stack(void) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 423 | unsigned long err; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 424 | while ((err = ERR_get_error()) != 0) { |
| 425 | char details[256]; |
Julien Boeuf | 01ac4f0 | 2015-10-09 16:08:24 -0700 | [diff] [blame] | 426 | ERR_error_string_n((uint32_t)err, details, sizeof(details)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 427 | gpr_log(GPR_ERROR, "%s", details); |
| 428 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 431 | /* Performs an SSL_read and handle errors. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 432 | static tsi_result do_ssl_read(SSL *ssl, unsigned char *unprotected_bytes, |
| 433 | size_t *unprotected_bytes_size) { |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 434 | int read_from_ssl; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | GPR_ASSERT(*unprotected_bytes_size <= INT_MAX); |
| 436 | read_from_ssl = |
| 437 | SSL_read(ssl, unprotected_bytes, (int)*unprotected_bytes_size); |
| 438 | if (read_from_ssl == 0) { |
| 439 | gpr_log(GPR_ERROR, "SSL_read returned 0 unexpectedly."); |
| 440 | return TSI_INTERNAL_ERROR; |
| 441 | } |
| 442 | if (read_from_ssl < 0) { |
| 443 | read_from_ssl = SSL_get_error(ssl, read_from_ssl); |
| 444 | switch (read_from_ssl) { |
| 445 | case SSL_ERROR_WANT_READ: |
| 446 | /* We need more data to finish the frame. */ |
| 447 | *unprotected_bytes_size = 0; |
| 448 | return TSI_OK; |
| 449 | case SSL_ERROR_WANT_WRITE: |
| 450 | gpr_log( |
| 451 | GPR_ERROR, |
| 452 | "Peer tried to renegotiate SSL connection. This is unsupported."); |
| 453 | return TSI_UNIMPLEMENTED; |
| 454 | case SSL_ERROR_SSL: |
| 455 | gpr_log(GPR_ERROR, "Corruption detected."); |
| 456 | log_ssl_error_stack(); |
| 457 | return TSI_DATA_CORRUPTED; |
| 458 | default: |
| 459 | gpr_log(GPR_ERROR, "SSL_read failed with error %s.", |
| 460 | ssl_error_string(read_from_ssl)); |
| 461 | return TSI_PROTOCOL_FAILURE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 462 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 463 | } |
| 464 | *unprotected_bytes_size = (size_t)read_from_ssl; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 465 | return TSI_OK; |
| 466 | } |
| 467 | |
| 468 | /* Performs an SSL_write and handle errors. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 469 | static tsi_result do_ssl_write(SSL *ssl, unsigned char *unprotected_bytes, |
| 470 | size_t unprotected_bytes_size) { |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 471 | int ssl_write_result; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 472 | GPR_ASSERT(unprotected_bytes_size <= INT_MAX); |
| 473 | ssl_write_result = |
| 474 | SSL_write(ssl, unprotected_bytes, (int)unprotected_bytes_size); |
| 475 | if (ssl_write_result < 0) { |
| 476 | ssl_write_result = SSL_get_error(ssl, ssl_write_result); |
| 477 | if (ssl_write_result == SSL_ERROR_WANT_READ) { |
| 478 | gpr_log(GPR_ERROR, |
| 479 | "Peer tried to renegotiate SSL connection. This is unsupported."); |
| 480 | return TSI_UNIMPLEMENTED; |
| 481 | } else { |
| 482 | gpr_log(GPR_ERROR, "SSL_write failed with error %s.", |
| 483 | ssl_error_string(ssl_write_result)); |
| 484 | return TSI_INTERNAL_ERROR; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 485 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 486 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 487 | return TSI_OK; |
| 488 | } |
| 489 | |
| 490 | /* Loads an in-memory PEM certificate chain into the SSL context. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 491 | static tsi_result ssl_ctx_use_certificate_chain( |
| 492 | SSL_CTX *context, const unsigned char *pem_cert_chain, |
| 493 | size_t pem_cert_chain_size) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 494 | tsi_result result = TSI_OK; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 495 | X509 *certificate = NULL; |
| 496 | BIO *pem; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 497 | GPR_ASSERT(pem_cert_chain_size <= INT_MAX); |
| 498 | pem = BIO_new_mem_buf((void *)pem_cert_chain, (int)pem_cert_chain_size); |
| 499 | if (pem == NULL) return TSI_OUT_OF_RESOURCES; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 500 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 501 | do { |
| 502 | certificate = PEM_read_bio_X509_AUX(pem, NULL, NULL, ""); |
| 503 | if (certificate == NULL) { |
| 504 | result = TSI_INVALID_ARGUMENT; |
| 505 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 506 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 507 | if (!SSL_CTX_use_certificate(context, certificate)) { |
| 508 | result = TSI_INVALID_ARGUMENT; |
| 509 | break; |
| 510 | } |
| 511 | while (1) { |
| 512 | X509 *certificate_authority = PEM_read_bio_X509(pem, NULL, NULL, ""); |
| 513 | if (certificate_authority == NULL) { |
| 514 | ERR_clear_error(); |
| 515 | break; /* Done reading. */ |
| 516 | } |
| 517 | if (!SSL_CTX_add_extra_chain_cert(context, certificate_authority)) { |
| 518 | X509_free(certificate_authority); |
| 519 | result = TSI_INVALID_ARGUMENT; |
| 520 | break; |
| 521 | } |
| 522 | /* We don't need to free certificate_authority as its ownership has been |
| 523 | transfered to the context. That is not the case for certificate though. |
| 524 | */ |
| 525 | } |
| 526 | } while (0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 527 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 528 | if (certificate != NULL) X509_free(certificate); |
| 529 | BIO_free(pem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 530 | return result; |
| 531 | } |
| 532 | |
| 533 | /* Loads an in-memory PEM private key into the SSL context. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 534 | static tsi_result ssl_ctx_use_private_key(SSL_CTX *context, |
| 535 | const unsigned char *pem_key, |
| 536 | size_t pem_key_size) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 537 | tsi_result result = TSI_OK; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 538 | EVP_PKEY *private_key = NULL; |
| 539 | BIO *pem; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 540 | GPR_ASSERT(pem_key_size <= INT_MAX); |
| 541 | pem = BIO_new_mem_buf((void *)pem_key, (int)pem_key_size); |
| 542 | if (pem == NULL) return TSI_OUT_OF_RESOURCES; |
| 543 | do { |
| 544 | private_key = PEM_read_bio_PrivateKey(pem, NULL, NULL, ""); |
| 545 | if (private_key == NULL) { |
| 546 | result = TSI_INVALID_ARGUMENT; |
| 547 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 548 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 549 | if (!SSL_CTX_use_PrivateKey(context, private_key)) { |
| 550 | result = TSI_INVALID_ARGUMENT; |
| 551 | break; |
| 552 | } |
| 553 | } while (0); |
| 554 | if (private_key != NULL) EVP_PKEY_free(private_key); |
| 555 | BIO_free(pem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 556 | return result; |
| 557 | } |
| 558 | |
| 559 | /* Loads in-memory PEM verification certs into the SSL context and optionally |
| 560 | returns the verification cert names (root_names can be NULL). */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 561 | static tsi_result ssl_ctx_load_verification_certs( |
| 562 | SSL_CTX *context, const unsigned char *pem_roots, size_t pem_roots_size, |
| 563 | STACK_OF(X509_NAME) * *root_names) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 564 | tsi_result result = TSI_OK; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 565 | size_t num_roots = 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 566 | X509 *root = NULL; |
| 567 | X509_NAME *root_name = NULL; |
| 568 | BIO *pem; |
| 569 | X509_STORE *root_store; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 570 | GPR_ASSERT(pem_roots_size <= INT_MAX); |
| 571 | pem = BIO_new_mem_buf((void *)pem_roots, (int)pem_roots_size); |
| 572 | root_store = SSL_CTX_get_cert_store(context); |
| 573 | if (root_store == NULL) return TSI_INVALID_ARGUMENT; |
| 574 | if (pem == NULL) return TSI_OUT_OF_RESOURCES; |
| 575 | if (root_names != NULL) { |
| 576 | *root_names = sk_X509_NAME_new_null(); |
| 577 | if (*root_names == NULL) return TSI_OUT_OF_RESOURCES; |
| 578 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 579 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 580 | while (1) { |
| 581 | root = PEM_read_bio_X509_AUX(pem, NULL, NULL, ""); |
| 582 | if (root == NULL) { |
| 583 | ERR_clear_error(); |
| 584 | break; /* We're at the end of stream. */ |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 585 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 586 | if (root_names != NULL) { |
| 587 | root_name = X509_get_subject_name(root); |
| 588 | if (root_name == NULL) { |
| 589 | gpr_log(GPR_ERROR, "Could not get name from root certificate."); |
| 590 | result = TSI_INVALID_ARGUMENT; |
| 591 | break; |
| 592 | } |
| 593 | root_name = X509_NAME_dup(root_name); |
| 594 | if (root_name == NULL) { |
| 595 | result = TSI_OUT_OF_RESOURCES; |
| 596 | break; |
| 597 | } |
| 598 | sk_X509_NAME_push(*root_names, root_name); |
| 599 | root_name = NULL; |
| 600 | } |
| 601 | if (!X509_STORE_add_cert(root_store, root)) { |
| 602 | gpr_log(GPR_ERROR, "Could not add root certificate to ssl context."); |
| 603 | result = TSI_INTERNAL_ERROR; |
| 604 | break; |
| 605 | } |
| 606 | X509_free(root); |
| 607 | num_roots++; |
| 608 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 609 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 610 | if (num_roots == 0) { |
| 611 | gpr_log(GPR_ERROR, "Could not load any root certificate."); |
| 612 | result = TSI_INVALID_ARGUMENT; |
| 613 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 614 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 615 | if (result != TSI_OK) { |
| 616 | if (root != NULL) X509_free(root); |
| 617 | if (root_names != NULL) { |
| 618 | sk_X509_NAME_pop_free(*root_names, X509_NAME_free); |
| 619 | *root_names = NULL; |
| 620 | if (root_name != NULL) X509_NAME_free(root_name); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 621 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 622 | } |
| 623 | BIO_free(pem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 624 | return result; |
| 625 | } |
| 626 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 627 | /* Populates the SSL context with a private key and a cert chain, and sets the |
| 628 | cipher list and the ephemeral ECDH key. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 629 | static tsi_result populate_ssl_context( |
| 630 | SSL_CTX *context, const unsigned char *pem_private_key, |
| 631 | size_t pem_private_key_size, const unsigned char *pem_certificate_chain, |
| 632 | size_t pem_certificate_chain_size, const char *cipher_list) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 633 | tsi_result result = TSI_OK; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 634 | if (pem_certificate_chain != NULL) { |
| 635 | result = ssl_ctx_use_certificate_chain(context, pem_certificate_chain, |
| 636 | pem_certificate_chain_size); |
| 637 | if (result != TSI_OK) { |
| 638 | gpr_log(GPR_ERROR, "Invalid cert chain file."); |
| 639 | return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 640 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 641 | } |
| 642 | if (pem_private_key != NULL) { |
| 643 | result = |
| 644 | ssl_ctx_use_private_key(context, pem_private_key, pem_private_key_size); |
| 645 | if (result != TSI_OK || !SSL_CTX_check_private_key(context)) { |
| 646 | gpr_log(GPR_ERROR, "Invalid private key."); |
| 647 | return result != TSI_OK ? result : TSI_INVALID_ARGUMENT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 648 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 649 | } |
| 650 | if ((cipher_list != NULL) && !SSL_CTX_set_cipher_list(context, cipher_list)) { |
| 651 | gpr_log(GPR_ERROR, "Invalid cipher list: %s.", cipher_list); |
| 652 | return TSI_INVALID_ARGUMENT; |
| 653 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 654 | { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 655 | EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 656 | if (!SSL_CTX_set_tmp_ecdh(context, ecdh)) { |
| 657 | gpr_log(GPR_ERROR, "Could not set ephemeral ECDH key."); |
| 658 | EC_KEY_free(ecdh); |
| 659 | return TSI_INTERNAL_ERROR; |
| 660 | } |
| 661 | SSL_CTX_set_options(context, SSL_OP_SINGLE_ECDH_USE); |
| 662 | EC_KEY_free(ecdh); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 663 | } |
| 664 | return TSI_OK; |
| 665 | } |
| 666 | |
| 667 | /* Extracts the CN and the SANs from an X509 cert as a peer object. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 668 | static tsi_result extract_x509_subject_names_from_pem_cert( |
| 669 | const unsigned char *pem_cert, size_t pem_cert_size, tsi_peer *peer) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 670 | tsi_result result = TSI_OK; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 671 | X509 *cert = NULL; |
| 672 | BIO *pem; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 673 | GPR_ASSERT(pem_cert_size <= INT_MAX); |
| 674 | pem = BIO_new_mem_buf((void *)pem_cert, (int)pem_cert_size); |
| 675 | if (pem == NULL) return TSI_OUT_OF_RESOURCES; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 676 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 677 | cert = PEM_read_bio_X509(pem, NULL, NULL, ""); |
| 678 | if (cert == NULL) { |
| 679 | gpr_log(GPR_ERROR, "Invalid certificate"); |
| 680 | result = TSI_INVALID_ARGUMENT; |
| 681 | } else { |
| 682 | result = peer_from_x509(cert, 0, peer); |
| 683 | } |
| 684 | if (cert != NULL) X509_free(cert); |
| 685 | BIO_free(pem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 686 | return result; |
| 687 | } |
| 688 | |
| 689 | /* Builds the alpn protocol name list according to rfc 7301. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 690 | static tsi_result build_alpn_protocol_name_list( |
| 691 | const unsigned char **alpn_protocols, |
| 692 | const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, |
| 693 | unsigned char **protocol_name_list, size_t *protocol_name_list_length) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 694 | uint16_t i; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 695 | unsigned char *current; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 696 | *protocol_name_list = NULL; |
| 697 | *protocol_name_list_length = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 698 | if (num_alpn_protocols == 0) return TSI_INVALID_ARGUMENT; |
| 699 | for (i = 0; i < num_alpn_protocols; i++) { |
| 700 | if (alpn_protocols_lengths[i] == 0) { |
| 701 | gpr_log(GPR_ERROR, "Invalid 0-length protocol name."); |
| 702 | return TSI_INVALID_ARGUMENT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 703 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 704 | *protocol_name_list_length += (size_t)alpn_protocols_lengths[i] + 1; |
| 705 | } |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 706 | *protocol_name_list = gpr_malloc(*protocol_name_list_length); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 707 | if (*protocol_name_list == NULL) return TSI_OUT_OF_RESOURCES; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 708 | current = *protocol_name_list; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 709 | for (i = 0; i < num_alpn_protocols; i++) { |
| 710 | *(current++) = alpn_protocols_lengths[i]; |
| 711 | memcpy(current, alpn_protocols[i], alpn_protocols_lengths[i]); |
| 712 | current += alpn_protocols_lengths[i]; |
| 713 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 714 | /* Safety check. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 715 | if ((current < *protocol_name_list) || |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 716 | ((uintptr_t)(current - *protocol_name_list) != |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 717 | *protocol_name_list_length)) { |
| 718 | return TSI_INTERNAL_ERROR; |
| 719 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 720 | return TSI_OK; |
| 721 | } |
| 722 | |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 723 | // The verification callback is used for clients that don't really care about |
| 724 | // the server's certificate, but we need to pull it anyway, in case a higher |
| 725 | // layer wants to look at it. In this case the verification may fail, but |
| 726 | // we don't really care. |
| 727 | static int NullVerifyCallback(int preverify_ok, X509_STORE_CTX *ctx) { |
| 728 | return 1; |
| 729 | } |
| 730 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 731 | /* --- tsi_frame_protector methods implementation. ---*/ |
| 732 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 733 | static tsi_result ssl_protector_protect(tsi_frame_protector *self, |
| 734 | const unsigned char *unprotected_bytes, |
| 735 | size_t *unprotected_bytes_size, |
| 736 | unsigned char *protected_output_frames, |
| 737 | size_t *protected_output_frames_size) { |
| 738 | tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 739 | int read_from_ssl; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 740 | size_t available; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 741 | tsi_result result = TSI_OK; |
| 742 | |
| 743 | /* First see if we have some pending data in the SSL BIO. */ |
Julien Boeuf | 01ac4f0 | 2015-10-09 16:08:24 -0700 | [diff] [blame] | 744 | int pending_in_ssl = (int)BIO_pending(impl->from_ssl); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 745 | if (pending_in_ssl > 0) { |
| 746 | *unprotected_bytes_size = 0; |
| 747 | GPR_ASSERT(*protected_output_frames_size <= INT_MAX); |
| 748 | read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames, |
| 749 | (int)*protected_output_frames_size); |
| 750 | if (read_from_ssl < 0) { |
| 751 | gpr_log(GPR_ERROR, |
| 752 | "Could not read from BIO even though some data is pending"); |
| 753 | return TSI_INTERNAL_ERROR; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 754 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 755 | *protected_output_frames_size = (size_t)read_from_ssl; |
| 756 | return TSI_OK; |
| 757 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 758 | |
| 759 | /* Now see if we can send a complete frame. */ |
| 760 | available = impl->buffer_size - impl->buffer_offset; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 761 | if (available > *unprotected_bytes_size) { |
| 762 | /* If we cannot, just copy the data in our internal buffer. */ |
| 763 | memcpy(impl->buffer + impl->buffer_offset, unprotected_bytes, |
| 764 | *unprotected_bytes_size); |
| 765 | impl->buffer_offset += *unprotected_bytes_size; |
| 766 | *protected_output_frames_size = 0; |
| 767 | return TSI_OK; |
| 768 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 769 | |
| 770 | /* If we can, prepare the buffer, send it to SSL_write and read. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 771 | memcpy(impl->buffer + impl->buffer_offset, unprotected_bytes, available); |
| 772 | result = do_ssl_write(impl->ssl, impl->buffer, impl->buffer_size); |
| 773 | if (result != TSI_OK) return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 774 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 775 | GPR_ASSERT(*protected_output_frames_size <= INT_MAX); |
| 776 | read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames, |
| 777 | (int)*protected_output_frames_size); |
| 778 | if (read_from_ssl < 0) { |
| 779 | gpr_log(GPR_ERROR, "Could not read from BIO after SSL_write."); |
| 780 | return TSI_INTERNAL_ERROR; |
| 781 | } |
| 782 | *protected_output_frames_size = (size_t)read_from_ssl; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 783 | *unprotected_bytes_size = available; |
| 784 | impl->buffer_offset = 0; |
| 785 | return TSI_OK; |
| 786 | } |
| 787 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 788 | static tsi_result ssl_protector_protect_flush( |
| 789 | tsi_frame_protector *self, unsigned char *protected_output_frames, |
| 790 | size_t *protected_output_frames_size, size_t *still_pending_size) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 791 | tsi_result result = TSI_OK; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 792 | tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 793 | int read_from_ssl = 0; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 794 | int pending; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 795 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 796 | if (impl->buffer_offset != 0) { |
| 797 | result = do_ssl_write(impl->ssl, impl->buffer, impl->buffer_offset); |
| 798 | if (result != TSI_OK) return result; |
| 799 | impl->buffer_offset = 0; |
| 800 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 801 | |
Julien Boeuf | 01ac4f0 | 2015-10-09 16:08:24 -0700 | [diff] [blame] | 802 | pending = (int)BIO_pending(impl->from_ssl); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 803 | GPR_ASSERT(pending >= 0); |
| 804 | *still_pending_size = (size_t)pending; |
| 805 | if (*still_pending_size == 0) return TSI_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 806 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 807 | GPR_ASSERT(*protected_output_frames_size <= INT_MAX); |
| 808 | read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames, |
| 809 | (int)*protected_output_frames_size); |
| 810 | if (read_from_ssl <= 0) { |
| 811 | gpr_log(GPR_ERROR, "Could not read from BIO after SSL_write."); |
| 812 | return TSI_INTERNAL_ERROR; |
| 813 | } |
| 814 | *protected_output_frames_size = (size_t)read_from_ssl; |
Julien Boeuf | 01ac4f0 | 2015-10-09 16:08:24 -0700 | [diff] [blame] | 815 | pending = (int)BIO_pending(impl->from_ssl); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 816 | GPR_ASSERT(pending >= 0); |
| 817 | *still_pending_size = (size_t)pending; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 818 | return TSI_OK; |
| 819 | } |
| 820 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 821 | static tsi_result ssl_protector_unprotect( |
| 822 | tsi_frame_protector *self, const unsigned char *protected_frames_bytes, |
| 823 | size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes, |
| 824 | size_t *unprotected_bytes_size) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 825 | tsi_result result = TSI_OK; |
| 826 | int written_into_ssl = 0; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 827 | size_t output_bytes_size = *unprotected_bytes_size; |
| 828 | size_t output_bytes_offset = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 829 | tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 830 | |
| 831 | /* First, try to read remaining data from ssl. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 832 | result = do_ssl_read(impl->ssl, unprotected_bytes, unprotected_bytes_size); |
| 833 | if (result != TSI_OK) return result; |
| 834 | if (*unprotected_bytes_size == output_bytes_size) { |
| 835 | /* We have read everything we could and cannot process any more input. */ |
| 836 | *protected_frames_bytes_size = 0; |
| 837 | return TSI_OK; |
| 838 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 839 | output_bytes_offset = *unprotected_bytes_size; |
| 840 | unprotected_bytes += output_bytes_offset; |
| 841 | *unprotected_bytes_size = output_bytes_size - output_bytes_offset; |
| 842 | |
| 843 | /* Then, try to write some data to ssl. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 844 | GPR_ASSERT(*protected_frames_bytes_size <= INT_MAX); |
| 845 | written_into_ssl = BIO_write(impl->into_ssl, protected_frames_bytes, |
| 846 | (int)*protected_frames_bytes_size); |
| 847 | if (written_into_ssl < 0) { |
| 848 | gpr_log(GPR_ERROR, "Sending protected frame to ssl failed with %d", |
| 849 | written_into_ssl); |
| 850 | return TSI_INTERNAL_ERROR; |
| 851 | } |
| 852 | *protected_frames_bytes_size = (size_t)written_into_ssl; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 853 | |
| 854 | /* Now try to read some data again. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 855 | result = do_ssl_read(impl->ssl, unprotected_bytes, unprotected_bytes_size); |
| 856 | if (result == TSI_OK) { |
| 857 | /* Don't forget to output the total number of bytes read. */ |
| 858 | *unprotected_bytes_size += output_bytes_offset; |
| 859 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 860 | return result; |
| 861 | } |
| 862 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 863 | static void ssl_protector_destroy(tsi_frame_protector *self) { |
| 864 | tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self; |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 865 | if (impl->buffer != NULL) gpr_free(impl->buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 866 | if (impl->ssl != NULL) SSL_free(impl->ssl); |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 867 | gpr_free(self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static const tsi_frame_protector_vtable frame_protector_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 871 | ssl_protector_protect, ssl_protector_protect_flush, ssl_protector_unprotect, |
| 872 | ssl_protector_destroy, |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 873 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 874 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 875 | /* --- tsi_handshaker methods implementation. ---*/ |
| 876 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 877 | static tsi_result ssl_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self, |
| 878 | unsigned char *bytes, |
| 879 | size_t *bytes_size) { |
| 880 | tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 881 | int bytes_read_from_ssl = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 882 | if (bytes == NULL || bytes_size == NULL || *bytes_size == 0 || |
| 883 | *bytes_size > INT_MAX) { |
| 884 | return TSI_INVALID_ARGUMENT; |
| 885 | } |
| 886 | GPR_ASSERT(*bytes_size <= INT_MAX); |
| 887 | bytes_read_from_ssl = BIO_read(impl->from_ssl, bytes, (int)*bytes_size); |
| 888 | if (bytes_read_from_ssl < 0) { |
| 889 | *bytes_size = 0; |
| 890 | if (!BIO_should_retry(impl->from_ssl)) { |
| 891 | impl->result = TSI_INTERNAL_ERROR; |
| 892 | return impl->result; |
| 893 | } else { |
| 894 | return TSI_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 895 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 896 | } |
| 897 | *bytes_size = (size_t)bytes_read_from_ssl; |
| 898 | return BIO_pending(impl->from_ssl) == 0 ? TSI_OK : TSI_INCOMPLETE_DATA; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 901 | static tsi_result ssl_handshaker_get_result(tsi_handshaker *self) { |
| 902 | tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; |
| 903 | if ((impl->result == TSI_HANDSHAKE_IN_PROGRESS) && |
| 904 | SSL_is_init_finished(impl->ssl)) { |
| 905 | impl->result = TSI_OK; |
| 906 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 907 | return impl->result; |
| 908 | } |
| 909 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 910 | static tsi_result ssl_handshaker_process_bytes_from_peer( |
| 911 | tsi_handshaker *self, const unsigned char *bytes, size_t *bytes_size) { |
| 912 | tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 913 | int bytes_written_into_ssl_size = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 914 | if (bytes == NULL || bytes_size == 0 || *bytes_size > INT_MAX) { |
| 915 | return TSI_INVALID_ARGUMENT; |
| 916 | } |
| 917 | GPR_ASSERT(*bytes_size <= INT_MAX); |
| 918 | bytes_written_into_ssl_size = |
| 919 | BIO_write(impl->into_ssl, bytes, (int)*bytes_size); |
| 920 | if (bytes_written_into_ssl_size < 0) { |
| 921 | gpr_log(GPR_ERROR, "Could not write to memory BIO."); |
| 922 | impl->result = TSI_INTERNAL_ERROR; |
| 923 | return impl->result; |
| 924 | } |
| 925 | *bytes_size = (size_t)bytes_written_into_ssl_size; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 926 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 927 | if (!tsi_handshaker_is_in_progress(self)) { |
| 928 | impl->result = TSI_OK; |
| 929 | return impl->result; |
| 930 | } else { |
| 931 | /* Get ready to get some bytes from SSL. */ |
| 932 | int ssl_result = SSL_do_handshake(impl->ssl); |
| 933 | ssl_result = SSL_get_error(impl->ssl, ssl_result); |
| 934 | switch (ssl_result) { |
| 935 | case SSL_ERROR_WANT_READ: |
| 936 | if (BIO_pending(impl->from_ssl) == 0) { |
| 937 | /* We need more data. */ |
| 938 | return TSI_INCOMPLETE_DATA; |
| 939 | } else { |
| 940 | return TSI_OK; |
| 941 | } |
| 942 | case SSL_ERROR_NONE: |
| 943 | return TSI_OK; |
| 944 | default: { |
| 945 | char err_str[256]; |
| 946 | ERR_error_string_n(ERR_get_error(), err_str, sizeof(err_str)); |
| 947 | gpr_log(GPR_ERROR, "Handshake failed with fatal error %s: %s.", |
| 948 | ssl_error_string(ssl_result), err_str); |
| 949 | impl->result = TSI_PROTOCOL_FAILURE; |
| 950 | return impl->result; |
| 951 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 952 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 953 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 956 | static tsi_result ssl_handshaker_extract_peer(tsi_handshaker *self, |
| 957 | tsi_peer *peer) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 958 | tsi_result result = TSI_OK; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 959 | const unsigned char *alpn_selected = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 960 | unsigned int alpn_selected_len; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 961 | tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; |
| 962 | X509 *peer_cert = SSL_get_peer_certificate(impl->ssl); |
| 963 | if (peer_cert != NULL) { |
| 964 | result = peer_from_x509(peer_cert, 1, peer); |
| 965 | X509_free(peer_cert); |
| 966 | if (result != TSI_OK) return result; |
| 967 | } |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 968 | #if TSI_OPENSSL_ALPN_SUPPORT |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 969 | SSL_get0_alpn_selected(impl->ssl, &alpn_selected, &alpn_selected_len); |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 970 | #endif /* TSI_OPENSSL_ALPN_SUPPORT */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 971 | if (alpn_selected == NULL) { |
| 972 | /* Try npn. */ |
| 973 | SSL_get0_next_proto_negotiated(impl->ssl, &alpn_selected, |
| 974 | &alpn_selected_len); |
| 975 | } |
| 976 | if (alpn_selected != NULL) { |
| 977 | size_t i; |
| 978 | tsi_peer_property *new_properties = |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 979 | gpr_malloc(sizeof(*new_properties) * (peer->property_count + 1)); |
Nicolas "Pixel" Noble | 5d203f5 | 2016-03-26 01:35:33 +0100 | [diff] [blame] | 980 | memset(new_properties, 0, |
| 981 | sizeof(*new_properties) * (peer->property_count + 1)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 982 | for (i = 0; i < peer->property_count; i++) { |
| 983 | new_properties[i] = peer->properties[i]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 984 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 985 | result = tsi_construct_string_peer_property( |
| 986 | TSI_SSL_ALPN_SELECTED_PROTOCOL, (const char *)alpn_selected, |
| 987 | alpn_selected_len, &new_properties[peer->property_count]); |
| 988 | if (result != TSI_OK) { |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 989 | gpr_free(new_properties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 990 | return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 991 | } |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 992 | if (peer->properties != NULL) gpr_free(peer->properties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 993 | peer->property_count++; |
| 994 | peer->properties = new_properties; |
| 995 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 996 | return result; |
| 997 | } |
| 998 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 999 | static tsi_result ssl_handshaker_create_frame_protector( |
| 1000 | tsi_handshaker *self, size_t *max_output_protected_frame_size, |
| 1001 | tsi_frame_protector **protector) { |
| 1002 | size_t actual_max_output_protected_frame_size = |
| 1003 | TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND; |
| 1004 | tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; |
Nicolas "Pixel" Noble | 5d203f5 | 2016-03-26 01:35:33 +0100 | [diff] [blame] | 1005 | tsi_ssl_frame_protector *protector_impl = gpr_malloc(sizeof(*protector_impl)); |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1006 | memset(protector_impl, 0, sizeof(*protector_impl)); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1007 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1008 | if (max_output_protected_frame_size != NULL) { |
| 1009 | if (*max_output_protected_frame_size > |
| 1010 | TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND) { |
| 1011 | *max_output_protected_frame_size = |
| 1012 | TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND; |
| 1013 | } else if (*max_output_protected_frame_size < |
| 1014 | TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND) { |
| 1015 | *max_output_protected_frame_size = |
| 1016 | TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1017 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1018 | actual_max_output_protected_frame_size = *max_output_protected_frame_size; |
| 1019 | } |
| 1020 | protector_impl->buffer_size = |
| 1021 | actual_max_output_protected_frame_size - TSI_SSL_MAX_PROTECTION_OVERHEAD; |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1022 | protector_impl->buffer = gpr_malloc(protector_impl->buffer_size); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1023 | if (protector_impl->buffer == NULL) { |
| 1024 | gpr_log(GPR_ERROR, |
| 1025 | "Could not allocated buffer for tsi_ssl_frame_protector."); |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1026 | gpr_free(protector_impl); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1027 | return TSI_INTERNAL_ERROR; |
| 1028 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1029 | |
| 1030 | /* Transfer ownership of ssl to the frame protector. It is OK as the caller |
| 1031 | * cannot call anything else but destroy on the handshaker after this call. */ |
| 1032 | protector_impl->ssl = impl->ssl; |
| 1033 | impl->ssl = NULL; |
| 1034 | protector_impl->into_ssl = impl->into_ssl; |
| 1035 | protector_impl->from_ssl = impl->from_ssl; |
| 1036 | |
| 1037 | protector_impl->base.vtable = &frame_protector_vtable; |
| 1038 | *protector = &protector_impl->base; |
| 1039 | return TSI_OK; |
| 1040 | } |
| 1041 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1042 | static void ssl_handshaker_destroy(tsi_handshaker *self) { |
| 1043 | tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self; |
| 1044 | SSL_free(impl->ssl); /* The BIO objects are owned by ssl */ |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1045 | gpr_free(impl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static const tsi_handshaker_vtable handshaker_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1049 | ssl_handshaker_get_bytes_to_send_to_peer, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1050 | ssl_handshaker_process_bytes_from_peer, |
| 1051 | ssl_handshaker_get_result, |
| 1052 | ssl_handshaker_extract_peer, |
| 1053 | ssl_handshaker_create_frame_protector, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1054 | ssl_handshaker_destroy, |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 1055 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1056 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1057 | /* --- tsi_ssl_handshaker_factory common methods. --- */ |
| 1058 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1059 | tsi_result tsi_ssl_handshaker_factory_create_handshaker( |
| 1060 | tsi_ssl_handshaker_factory *self, const char *server_name_indication, |
| 1061 | tsi_handshaker **handshaker) { |
| 1062 | if (self == NULL || handshaker == NULL) return TSI_INVALID_ARGUMENT; |
| 1063 | return self->create_handshaker(self, server_name_indication, handshaker); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1066 | void tsi_ssl_handshaker_factory_destroy(tsi_ssl_handshaker_factory *self) { |
| 1067 | if (self == NULL) return; |
| 1068 | self->destroy(self); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1071 | static tsi_result create_tsi_ssl_handshaker(SSL_CTX *ctx, int is_client, |
| 1072 | const char *server_name_indication, |
| 1073 | tsi_handshaker **handshaker) { |
| 1074 | SSL *ssl = SSL_new(ctx); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1075 | BIO *into_ssl = NULL; |
| 1076 | BIO *from_ssl = NULL; |
| 1077 | tsi_ssl_handshaker *impl = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1078 | *handshaker = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1079 | if (ctx == NULL) { |
| 1080 | gpr_log(GPR_ERROR, "SSL Context is null. Should never happen."); |
| 1081 | return TSI_INTERNAL_ERROR; |
| 1082 | } |
| 1083 | if (ssl == NULL) { |
| 1084 | return TSI_OUT_OF_RESOURCES; |
| 1085 | } |
| 1086 | SSL_set_info_callback(ssl, ssl_info_callback); |
| 1087 | |
| 1088 | into_ssl = BIO_new(BIO_s_mem()); |
| 1089 | from_ssl = BIO_new(BIO_s_mem()); |
| 1090 | if (into_ssl == NULL || from_ssl == NULL) { |
| 1091 | gpr_log(GPR_ERROR, "BIO_new failed."); |
| 1092 | SSL_free(ssl); |
| 1093 | if (into_ssl != NULL) BIO_free(into_ssl); |
| 1094 | if (from_ssl != NULL) BIO_free(into_ssl); |
| 1095 | return TSI_OUT_OF_RESOURCES; |
| 1096 | } |
| 1097 | SSL_set_bio(ssl, into_ssl, from_ssl); |
| 1098 | if (is_client) { |
| 1099 | int ssl_result; |
| 1100 | SSL_set_connect_state(ssl); |
| 1101 | if (server_name_indication != NULL) { |
| 1102 | if (!SSL_set_tlsext_host_name(ssl, server_name_indication)) { |
| 1103 | gpr_log(GPR_ERROR, "Invalid server name indication %s.", |
| 1104 | server_name_indication); |
| 1105 | SSL_free(ssl); |
| 1106 | return TSI_INTERNAL_ERROR; |
| 1107 | } |
| 1108 | } |
| 1109 | ssl_result = SSL_do_handshake(ssl); |
| 1110 | ssl_result = SSL_get_error(ssl, ssl_result); |
| 1111 | if (ssl_result != SSL_ERROR_WANT_READ) { |
| 1112 | gpr_log(GPR_ERROR, |
| 1113 | "Unexpected error received from first SSL_do_handshake call: %s", |
| 1114 | ssl_error_string(ssl_result)); |
| 1115 | SSL_free(ssl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1116 | return TSI_INTERNAL_ERROR; |
| 1117 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1118 | } else { |
| 1119 | SSL_set_accept_state(ssl); |
| 1120 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1121 | |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1122 | impl = gpr_malloc(sizeof(*impl)); |
| 1123 | memset(impl, 0, sizeof(*impl)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1124 | impl->ssl = ssl; |
| 1125 | impl->into_ssl = into_ssl; |
| 1126 | impl->from_ssl = from_ssl; |
| 1127 | impl->result = TSI_HANDSHAKE_IN_PROGRESS; |
| 1128 | impl->base.vtable = &handshaker_vtable; |
| 1129 | *handshaker = &impl->base; |
| 1130 | return TSI_OK; |
| 1131 | } |
| 1132 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1133 | static int select_protocol_list(const unsigned char **out, |
| 1134 | unsigned char *outlen, |
| 1135 | const unsigned char *client_list, |
| 1136 | size_t client_list_len, |
| 1137 | const unsigned char *server_list, |
| 1138 | size_t server_list_len) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1139 | const unsigned char *client_current = client_list; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1140 | while ((unsigned int)(client_current - client_list) < client_list_len) { |
| 1141 | unsigned char client_current_len = *(client_current++); |
| 1142 | const unsigned char *server_current = server_list; |
| 1143 | while ((server_current >= server_list) && |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1144 | (uintptr_t)(server_current - server_list) < server_list_len) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1145 | unsigned char server_current_len = *(server_current++); |
| 1146 | if ((client_current_len == server_current_len) && |
| 1147 | !memcmp(client_current, server_current, server_current_len)) { |
| 1148 | *out = server_current; |
| 1149 | *outlen = server_current_len; |
| 1150 | return SSL_TLSEXT_ERR_OK; |
| 1151 | } |
| 1152 | server_current += server_current_len; |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1153 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1154 | client_current += client_current_len; |
| 1155 | } |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1156 | return SSL_TLSEXT_ERR_NOACK; |
| 1157 | } |
| 1158 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1159 | /* --- tsi_ssl__client_handshaker_factory methods implementation. --- */ |
| 1160 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1161 | static tsi_result ssl_client_handshaker_factory_create_handshaker( |
| 1162 | tsi_ssl_handshaker_factory *self, const char *server_name_indication, |
| 1163 | tsi_handshaker **handshaker) { |
| 1164 | tsi_ssl_client_handshaker_factory *impl = |
| 1165 | (tsi_ssl_client_handshaker_factory *)self; |
| 1166 | return create_tsi_ssl_handshaker(impl->ssl_context, 1, server_name_indication, |
| 1167 | handshaker); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1170 | static void ssl_client_handshaker_factory_destroy( |
| 1171 | tsi_ssl_handshaker_factory *self) { |
| 1172 | tsi_ssl_client_handshaker_factory *impl = |
| 1173 | (tsi_ssl_client_handshaker_factory *)self; |
| 1174 | if (impl->ssl_context != NULL) SSL_CTX_free(impl->ssl_context); |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1175 | if (impl->alpn_protocol_list != NULL) gpr_free(impl->alpn_protocol_list); |
| 1176 | gpr_free(impl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1179 | static int client_handshaker_factory_npn_callback(SSL *ssl, unsigned char **out, |
| 1180 | unsigned char *outlen, |
| 1181 | const unsigned char *in, |
| 1182 | unsigned int inlen, |
| 1183 | void *arg) { |
| 1184 | tsi_ssl_client_handshaker_factory *factory = |
| 1185 | (tsi_ssl_client_handshaker_factory *)arg; |
| 1186 | return select_protocol_list((const unsigned char **)out, outlen, |
| 1187 | factory->alpn_protocol_list, |
| 1188 | factory->alpn_protocol_list_length, in, inlen); |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1191 | /* --- tsi_ssl_server_handshaker_factory methods implementation. --- */ |
| 1192 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1193 | static tsi_result ssl_server_handshaker_factory_create_handshaker( |
| 1194 | tsi_ssl_handshaker_factory *self, const char *server_name_indication, |
| 1195 | tsi_handshaker **handshaker) { |
| 1196 | tsi_ssl_server_handshaker_factory *impl = |
| 1197 | (tsi_ssl_server_handshaker_factory *)self; |
| 1198 | if (impl->ssl_context_count == 0 || server_name_indication != NULL) { |
| 1199 | return TSI_INVALID_ARGUMENT; |
| 1200 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1201 | /* Create the handshaker with the first context. We will switch if needed |
| 1202 | because of SNI in ssl_server_handshaker_factory_servername_callback. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1203 | return create_tsi_ssl_handshaker(impl->ssl_contexts[0], 0, NULL, handshaker); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1206 | static void ssl_server_handshaker_factory_destroy( |
| 1207 | tsi_ssl_handshaker_factory *self) { |
| 1208 | tsi_ssl_server_handshaker_factory *impl = |
| 1209 | (tsi_ssl_server_handshaker_factory *)self; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 1210 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1211 | for (i = 0; i < impl->ssl_context_count; i++) { |
| 1212 | if (impl->ssl_contexts[i] != NULL) { |
| 1213 | SSL_CTX_free(impl->ssl_contexts[i]); |
| 1214 | tsi_peer_destruct(&impl->ssl_context_x509_subject_names[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1215 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1216 | } |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1217 | if (impl->ssl_contexts != NULL) gpr_free(impl->ssl_contexts); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1218 | if (impl->ssl_context_x509_subject_names != NULL) { |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1219 | gpr_free(impl->ssl_context_x509_subject_names); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1220 | } |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1221 | if (impl->alpn_protocol_list != NULL) gpr_free(impl->alpn_protocol_list); |
| 1222 | gpr_free(impl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1225 | static int does_entry_match_name(const char *entry, size_t entry_length, |
| 1226 | const char *name) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1227 | const char *dot; |
| 1228 | const char *name_subdomain = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1229 | size_t name_length = strlen(name); |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 1230 | size_t name_subdomain_length; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1231 | if (entry_length == 0) return 0; |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 1232 | |
| 1233 | /* Take care of '.' terminations. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1234 | if (name[name_length - 1] == '.') { |
| 1235 | name_length--; |
| 1236 | } |
| 1237 | if (entry[entry_length - 1] == '.') { |
| 1238 | entry_length--; |
| 1239 | if (entry_length == 0) return 0; |
| 1240 | } |
Julien Boeuf | 9fff77e | 2015-02-24 16:50:35 -0800 | [diff] [blame] | 1241 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1242 | if ((name_length == entry_length) && |
| 1243 | strncmp(name, entry, entry_length) == 0) { |
| 1244 | return 1; /* Perfect match. */ |
| 1245 | } |
| 1246 | if (entry[0] != '*') return 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1247 | |
| 1248 | /* Wildchar subdomain matching. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1249 | if (entry_length < 3 || entry[1] != '.') { /* At least *.x */ |
| 1250 | gpr_log(GPR_ERROR, "Invalid wildchar entry."); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1251 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1252 | } |
| 1253 | name_subdomain = strchr(name, '.'); |
| 1254 | if (name_subdomain == NULL) return 0; |
| 1255 | name_subdomain_length = strlen(name_subdomain); |
| 1256 | if (name_subdomain_length < 2) return 0; |
| 1257 | name_subdomain++; /* Starts after the dot. */ |
Julien Boeuf | 0170a6c | 2015-02-24 18:08:01 -0800 | [diff] [blame] | 1258 | name_subdomain_length--; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1259 | entry += 2; /* Remove *. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1260 | entry_length -= 2; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1261 | dot = strchr(name_subdomain, '.'); |
| 1262 | if ((dot == NULL) || (dot == &name_subdomain[name_subdomain_length - 1])) { |
| 1263 | gpr_log(GPR_ERROR, "Invalid toplevel subdomain: %s", name_subdomain); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | if (name_subdomain[name_subdomain_length - 1] == '.') { |
| 1267 | name_subdomain_length--; |
| 1268 | } |
| 1269 | return ((entry_length > 0) && (name_subdomain_length == entry_length) && |
| 1270 | strncmp(entry, name_subdomain, entry_length) == 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1273 | static int ssl_server_handshaker_factory_servername_callback(SSL *ssl, int *ap, |
| 1274 | void *arg) { |
| 1275 | tsi_ssl_server_handshaker_factory *impl = |
| 1276 | (tsi_ssl_server_handshaker_factory *)arg; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 1277 | size_t i = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1278 | const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 1279 | if (servername == NULL || strlen(servername) == 0) { |
| 1280 | return SSL_TLSEXT_ERR_NOACK; |
| 1281 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1282 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1283 | for (i = 0; i < impl->ssl_context_count; i++) { |
| 1284 | if (tsi_ssl_peer_matches_name(&impl->ssl_context_x509_subject_names[i], |
| 1285 | servername)) { |
| 1286 | SSL_set_SSL_CTX(ssl, impl->ssl_contexts[i]); |
| 1287 | return SSL_TLSEXT_ERR_OK; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1288 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1289 | } |
| 1290 | gpr_log(GPR_ERROR, "No match found for server name: %s.", servername); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1291 | return SSL_TLSEXT_ERR_ALERT_WARNING; |
| 1292 | } |
| 1293 | |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1294 | #if TSI_OPENSSL_ALPN_SUPPORT |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1295 | static int server_handshaker_factory_alpn_callback( |
| 1296 | SSL *ssl, const unsigned char **out, unsigned char *outlen, |
| 1297 | const unsigned char *in, unsigned int inlen, void *arg) { |
| 1298 | tsi_ssl_server_handshaker_factory *factory = |
| 1299 | (tsi_ssl_server_handshaker_factory *)arg; |
| 1300 | return select_protocol_list(out, outlen, in, inlen, |
| 1301 | factory->alpn_protocol_list, |
| 1302 | factory->alpn_protocol_list_length); |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1303 | } |
| 1304 | #endif /* TSI_OPENSSL_ALPN_SUPPORT */ |
| 1305 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1306 | static int server_handshaker_factory_npn_advertised_callback( |
| 1307 | SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg) { |
| 1308 | tsi_ssl_server_handshaker_factory *factory = |
| 1309 | (tsi_ssl_server_handshaker_factory *)arg; |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1310 | *out = factory->alpn_protocol_list; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1311 | GPR_ASSERT(factory->alpn_protocol_list_length <= UINT_MAX); |
| 1312 | *outlen = (unsigned int)factory->alpn_protocol_list_length; |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1313 | return SSL_TLSEXT_ERR_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1316 | /* --- tsi_ssl_handshaker_factory constructors. --- */ |
| 1317 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1318 | tsi_result tsi_create_ssl_client_handshaker_factory( |
| 1319 | const unsigned char *pem_private_key, size_t pem_private_key_size, |
| 1320 | const unsigned char *pem_cert_chain, size_t pem_cert_chain_size, |
| 1321 | const unsigned char *pem_root_certs, size_t pem_root_certs_size, |
| 1322 | const char *cipher_list, const unsigned char **alpn_protocols, |
| 1323 | const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, |
| 1324 | tsi_ssl_handshaker_factory **factory) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1325 | SSL_CTX *ssl_context = NULL; |
| 1326 | tsi_ssl_client_handshaker_factory *impl = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1327 | tsi_result result = TSI_OK; |
| 1328 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1329 | gpr_once_init(&init_openssl_once, init_openssl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1330 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1331 | if (factory == NULL) return TSI_INVALID_ARGUMENT; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1332 | *factory = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1333 | if (pem_root_certs == NULL) return TSI_INVALID_ARGUMENT; |
| 1334 | |
| 1335 | ssl_context = SSL_CTX_new(TLSv1_2_method()); |
| 1336 | if (ssl_context == NULL) { |
| 1337 | gpr_log(GPR_ERROR, "Could not create ssl context."); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1338 | return TSI_INVALID_ARGUMENT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1339 | } |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1340 | |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1341 | impl = gpr_malloc(sizeof(*impl)); |
| 1342 | memset(impl, 0, sizeof(*impl)); |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1343 | impl->ssl_context = ssl_context; |
| 1344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1345 | do { |
| 1346 | result = |
| 1347 | populate_ssl_context(ssl_context, pem_private_key, pem_private_key_size, |
| 1348 | pem_cert_chain, pem_cert_chain_size, cipher_list); |
| 1349 | if (result != TSI_OK) break; |
| 1350 | result = ssl_ctx_load_verification_certs(ssl_context, pem_root_certs, |
| 1351 | pem_root_certs_size, NULL); |
| 1352 | if (result != TSI_OK) { |
| 1353 | gpr_log(GPR_ERROR, "Cannot load server root certificates."); |
| 1354 | break; |
| 1355 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1356 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1357 | if (num_alpn_protocols != 0) { |
| 1358 | result = build_alpn_protocol_name_list( |
| 1359 | alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, |
| 1360 | &impl->alpn_protocol_list, &impl->alpn_protocol_list_length); |
| 1361 | if (result != TSI_OK) { |
| 1362 | gpr_log(GPR_ERROR, "Building alpn list failed with error %s.", |
| 1363 | tsi_result_to_string(result)); |
| 1364 | break; |
| 1365 | } |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1366 | #if TSI_OPENSSL_ALPN_SUPPORT |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1367 | GPR_ASSERT(impl->alpn_protocol_list_length < UINT_MAX); |
| 1368 | if (SSL_CTX_set_alpn_protos( |
| 1369 | ssl_context, impl->alpn_protocol_list, |
| 1370 | (unsigned int)impl->alpn_protocol_list_length)) { |
| 1371 | gpr_log(GPR_ERROR, "Could not set alpn protocol list to context."); |
| 1372 | result = TSI_INVALID_ARGUMENT; |
| 1373 | break; |
| 1374 | } |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1375 | #endif /* TSI_OPENSSL_ALPN_SUPPORT */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1376 | SSL_CTX_set_next_proto_select_cb( |
| 1377 | ssl_context, client_handshaker_factory_npn_callback, impl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1378 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1379 | } while (0); |
| 1380 | if (result != TSI_OK) { |
| 1381 | ssl_client_handshaker_factory_destroy(&impl->base); |
| 1382 | return result; |
| 1383 | } |
| 1384 | SSL_CTX_set_verify(ssl_context, SSL_VERIFY_PEER, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1385 | /* TODO(jboeuf): Add revocation verification. */ |
| 1386 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1387 | impl->base.create_handshaker = |
| 1388 | ssl_client_handshaker_factory_create_handshaker; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1389 | impl->base.destroy = ssl_client_handshaker_factory_destroy; |
| 1390 | *factory = &impl->base; |
| 1391 | return TSI_OK; |
| 1392 | } |
| 1393 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1394 | tsi_result tsi_create_ssl_server_handshaker_factory( |
| 1395 | const unsigned char **pem_private_keys, |
| 1396 | const size_t *pem_private_keys_sizes, const unsigned char **pem_cert_chains, |
| 1397 | const size_t *pem_cert_chains_sizes, size_t key_cert_pair_count, |
| 1398 | const unsigned char *pem_client_root_certs, |
| 1399 | size_t pem_client_root_certs_size, int force_client_auth, |
| 1400 | const char *cipher_list, const unsigned char **alpn_protocols, |
| 1401 | const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, |
| 1402 | tsi_ssl_handshaker_factory **factory) { |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 1403 | return tsi_create_ssl_server_handshaker_factory_ex( |
| 1404 | pem_private_keys, pem_private_keys_sizes, pem_cert_chains, |
| 1405 | pem_cert_chains_sizes, key_cert_pair_count, pem_client_root_certs, |
| 1406 | pem_client_root_certs_size, |
| 1407 | force_client_auth ? TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY |
| 1408 | : TSI_DONT_REQUEST_CLIENT_CERTIFICATE, |
| 1409 | cipher_list, alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, |
| 1410 | factory); |
| 1411 | } |
| 1412 | |
| 1413 | tsi_result tsi_create_ssl_server_handshaker_factory_ex( |
| 1414 | const unsigned char **pem_private_keys, |
| 1415 | const size_t *pem_private_keys_sizes, const unsigned char **pem_cert_chains, |
| 1416 | const size_t *pem_cert_chains_sizes, size_t key_cert_pair_count, |
| 1417 | const unsigned char *pem_client_root_certs, |
| 1418 | size_t pem_client_root_certs_size, |
| 1419 | tsi_client_certificate_request_type client_certificate_request, |
| 1420 | const char *cipher_list, const unsigned char **alpn_protocols, |
| 1421 | const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, |
| 1422 | tsi_ssl_handshaker_factory **factory) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1423 | tsi_ssl_server_handshaker_factory *impl = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1424 | tsi_result result = TSI_OK; |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 1425 | size_t i = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1426 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1427 | gpr_once_init(&init_openssl_once, init_openssl); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1428 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1429 | if (factory == NULL) return TSI_INVALID_ARGUMENT; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1430 | *factory = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1431 | if (key_cert_pair_count == 0 || pem_private_keys == NULL || |
| 1432 | pem_cert_chains == NULL) { |
| 1433 | return TSI_INVALID_ARGUMENT; |
| 1434 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1435 | |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1436 | impl = gpr_malloc(sizeof(*impl)); |
| 1437 | memset(impl, 0, sizeof(*impl)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1438 | impl->base.create_handshaker = |
| 1439 | ssl_server_handshaker_factory_create_handshaker; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1440 | impl->base.destroy = ssl_server_handshaker_factory_destroy; |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1441 | impl->ssl_contexts = gpr_malloc(key_cert_pair_count * sizeof(SSL_CTX *)); |
| 1442 | memset(impl->ssl_contexts, 0, key_cert_pair_count * sizeof(SSL_CTX *)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1443 | impl->ssl_context_x509_subject_names = |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 1444 | gpr_malloc(key_cert_pair_count * sizeof(tsi_peer)); |
Nicolas "Pixel" Noble | 5d203f5 | 2016-03-26 01:35:33 +0100 | [diff] [blame] | 1445 | memset(impl->ssl_context_x509_subject_names, 0, |
| 1446 | key_cert_pair_count * sizeof(tsi_peer)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1447 | if (impl->ssl_contexts == NULL || |
| 1448 | impl->ssl_context_x509_subject_names == NULL) { |
| 1449 | tsi_ssl_handshaker_factory_destroy(&impl->base); |
| 1450 | return TSI_OUT_OF_RESOURCES; |
| 1451 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1452 | impl->ssl_context_count = key_cert_pair_count; |
| 1453 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1454 | if (num_alpn_protocols > 0) { |
| 1455 | result = build_alpn_protocol_name_list( |
| 1456 | alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, |
| 1457 | &impl->alpn_protocol_list, &impl->alpn_protocol_list_length); |
| 1458 | if (result != TSI_OK) { |
| 1459 | tsi_ssl_handshaker_factory_destroy(&impl->base); |
| 1460 | return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1461 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1462 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1463 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1464 | for (i = 0; i < key_cert_pair_count; i++) { |
| 1465 | do { |
| 1466 | impl->ssl_contexts[i] = SSL_CTX_new(TLSv1_2_method()); |
| 1467 | if (impl->ssl_contexts[i] == NULL) { |
| 1468 | gpr_log(GPR_ERROR, "Could not create ssl context."); |
| 1469 | result = TSI_OUT_OF_RESOURCES; |
| 1470 | break; |
| 1471 | } |
| 1472 | result = populate_ssl_context( |
| 1473 | impl->ssl_contexts[i], pem_private_keys[i], pem_private_keys_sizes[i], |
| 1474 | pem_cert_chains[i], pem_cert_chains_sizes[i], cipher_list); |
| 1475 | if (result != TSI_OK) break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1476 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1477 | if (pem_client_root_certs != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1478 | STACK_OF(X509_NAME) *root_names = NULL; |
| 1479 | result = ssl_ctx_load_verification_certs( |
| 1480 | impl->ssl_contexts[i], pem_client_root_certs, |
| 1481 | pem_client_root_certs_size, &root_names); |
| 1482 | if (result != TSI_OK) { |
| 1483 | gpr_log(GPR_ERROR, "Invalid verification certs."); |
| 1484 | break; |
| 1485 | } |
| 1486 | SSL_CTX_set_client_CA_list(impl->ssl_contexts[i], root_names); |
Deepak Lukose | dba4c5f | 2016-03-25 12:54:25 -0700 | [diff] [blame] | 1487 | switch (client_certificate_request) { |
| 1488 | case TSI_DONT_REQUEST_CLIENT_CERTIFICATE: |
| 1489 | SSL_CTX_set_verify(impl->ssl_contexts[i], SSL_VERIFY_NONE, NULL); |
| 1490 | break; |
| 1491 | case TSI_REQUEST_CLIENT_CERTIFICATE_BUT_DONT_VERIFY: |
| 1492 | SSL_CTX_set_verify(impl->ssl_contexts[i], SSL_VERIFY_PEER, |
| 1493 | NullVerifyCallback); |
| 1494 | break; |
| 1495 | case TSI_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY: |
| 1496 | SSL_CTX_set_verify(impl->ssl_contexts[i], SSL_VERIFY_PEER, NULL); |
| 1497 | break; |
| 1498 | case TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_BUT_DONT_VERIFY: |
| 1499 | SSL_CTX_set_verify( |
| 1500 | impl->ssl_contexts[i], |
| 1501 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 1502 | NullVerifyCallback); |
| 1503 | break; |
| 1504 | case TSI_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY: |
| 1505 | SSL_CTX_set_verify( |
| 1506 | impl->ssl_contexts[i], |
| 1507 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); |
| 1508 | break; |
| 1509 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1510 | /* TODO(jboeuf): Add revocation verification. */ |
| 1511 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1512 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1513 | result = extract_x509_subject_names_from_pem_cert( |
| 1514 | pem_cert_chains[i], pem_cert_chains_sizes[i], |
| 1515 | &impl->ssl_context_x509_subject_names[i]); |
| 1516 | if (result != TSI_OK) break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1517 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1518 | SSL_CTX_set_tlsext_servername_callback( |
| 1519 | impl->ssl_contexts[i], |
| 1520 | ssl_server_handshaker_factory_servername_callback); |
| 1521 | SSL_CTX_set_tlsext_servername_arg(impl->ssl_contexts[i], impl); |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1522 | #if TSI_OPENSSL_ALPN_SUPPORT |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1523 | SSL_CTX_set_alpn_select_cb(impl->ssl_contexts[i], |
| 1524 | server_handshaker_factory_alpn_callback, impl); |
Julien Boeuf | d153132 | 2015-06-18 11:05:39 +0200 | [diff] [blame] | 1525 | #endif /* TSI_OPENSSL_ALPN_SUPPORT */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1526 | SSL_CTX_set_next_protos_advertised_cb( |
| 1527 | impl->ssl_contexts[i], |
| 1528 | server_handshaker_factory_npn_advertised_callback, impl); |
| 1529 | } while (0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1530 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1531 | if (result != TSI_OK) { |
| 1532 | tsi_ssl_handshaker_factory_destroy(&impl->base); |
| 1533 | return result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1534 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1535 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1536 | *factory = &impl->base; |
| 1537 | return TSI_OK; |
| 1538 | } |
| 1539 | |
| 1540 | /* --- tsi_ssl utils. --- */ |
| 1541 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1542 | int tsi_ssl_peer_matches_name(const tsi_peer *peer, const char *name) { |
Julien Boeuf | b222b4d | 2015-01-15 17:01:39 -0800 | [diff] [blame] | 1543 | size_t i = 0; |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 1544 | size_t san_count = 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1545 | const tsi_peer_property *cn_property = NULL; |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 1546 | int like_ip = looks_like_ip_address(name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1547 | |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 1548 | /* Check the SAN first. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1549 | for (i = 0; i < peer->property_count; i++) { |
| 1550 | const tsi_peer_property *property = &peer->properties[i]; |
| 1551 | if (property->name == NULL) continue; |
| 1552 | if (strcmp(property->name, |
| 1553 | TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) { |
| 1554 | san_count++; |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 1555 | |
| 1556 | if (!like_ip && does_entry_match_name(property->value.data, |
| 1557 | property->value.length, name)) { |
| 1558 | return 1; |
| 1559 | } else if (like_ip && |
| 1560 | strncmp(name, property->value.data, property->value.length) == |
| 1561 | 0 && |
| 1562 | strlen(name) == property->value.length) { |
| 1563 | /* IP Addresses are exact matches only. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1564 | return 1; |
| 1565 | } |
| 1566 | } else if (strcmp(property->name, |
| 1567 | TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) { |
| 1568 | cn_property = property; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1569 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1570 | } |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 1571 | |
Paul Querna | 47d841d | 2016-03-10 11:19:17 -0800 | [diff] [blame] | 1572 | /* If there's no SAN, try the CN, but only if its not like an IP Address */ |
| 1573 | if (san_count == 0 && cn_property != NULL && !like_ip) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1574 | if (does_entry_match_name(cn_property->value.data, |
| 1575 | cn_property->value.length, name)) { |
| 1576 | return 1; |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 1577 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1578 | } |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 1579 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1580 | return 0; /* Not found. */ |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1581 | } |