Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame^] | 8 | * Copyright (C) 2012 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 9 | * Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com> |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 10 | * |
| 11 | * This software is licensed as described in the file COPYING, which |
| 12 | * you should have received as part of this distribution. The terms |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 13 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 14 | * |
| 15 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 16 | * copies of the Software, and permit persons to whom the Software is |
| 17 | * furnished to do so, under the terms of the COPYING file. |
| 18 | * |
| 19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 | * KIND, either express or implied. |
| 21 | * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | /* |
| 25 | * Source file for all PolarSSL-specific code for the TLS/SSL layer. No code |
| 26 | * but vtls.c should ever call or use these functions. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include "curl_setup.h" |
| 31 | |
| 32 | #ifdef USE_POLARSSL |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 33 | #include <polarssl/net.h> |
| 34 | #include <polarssl/ssl.h> |
| 35 | #include <polarssl/certs.h> |
| 36 | #include <polarssl/x509.h> |
| 37 | #include <polarssl/version.h> |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 38 | #include <polarssl/sha256.h> |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 39 | |
| 40 | #if POLARSSL_VERSION_NUMBER < 0x01030000 |
| 41 | #error too old PolarSSL |
| 42 | #endif |
| 43 | |
| 44 | #include <polarssl/error.h> |
| 45 | #include <polarssl/entropy.h> |
| 46 | #include <polarssl/ctr_drbg.h> |
| 47 | |
| 48 | #include "urldata.h" |
| 49 | #include "sendf.h" |
| 50 | #include "inet_pton.h" |
| 51 | #include "polarssl.h" |
| 52 | #include "vtls.h" |
| 53 | #include "parsedate.h" |
| 54 | #include "connect.h" /* for the connect timeout */ |
| 55 | #include "select.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 56 | #include "strcase.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 57 | #include "polarssl_threadlock.h" |
| 58 | #include "curl_printf.h" |
| 59 | #include "curl_memory.h" |
| 60 | /* The last #include file should be: */ |
| 61 | #include "memdebug.h" |
| 62 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 63 | /* See https://tls.mbed.org/discussions/generic/ |
| 64 | howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der |
| 65 | */ |
| 66 | #define RSA_PUB_DER_MAX_BYTES (38 + 2 * POLARSSL_MPI_MAX_SIZE) |
| 67 | #define ECP_PUB_DER_MAX_BYTES (30 + 2 * POLARSSL_ECP_MAX_BYTES) |
| 68 | |
| 69 | #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ |
| 70 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES) |
| 71 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 72 | struct ssl_backend_data { |
| 73 | ctr_drbg_context ctr_drbg; |
| 74 | entropy_context entropy; |
| 75 | ssl_context ssl; |
| 76 | int server_fd; |
| 77 | x509_crt cacert; |
| 78 | x509_crt clicert; |
| 79 | x509_crl crl; |
| 80 | rsa_context rsa; |
| 81 | }; |
| 82 | |
| 83 | #define BACKEND connssl->backend |
| 84 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 85 | /* apply threading? */ |
| 86 | #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32) |
| 87 | #define THREADING_SUPPORT |
| 88 | #endif |
| 89 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 90 | #ifndef POLARSSL_ERROR_C |
| 91 | #define error_strerror(x,y,z) |
| 92 | #endif /* POLARSSL_ERROR_C */ |
| 93 | |
| 94 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 95 | #if defined(THREADING_SUPPORT) |
| 96 | static entropy_context entropy; |
| 97 | |
| 98 | static int entropy_init_initialized = 0; |
| 99 | |
| 100 | /* start of entropy_init_mutex() */ |
| 101 | static void entropy_init_mutex(entropy_context *ctx) |
| 102 | { |
| 103 | /* lock 0 = entropy_init_mutex() */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 104 | Curl_polarsslthreadlock_lock_function(0); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 105 | if(entropy_init_initialized == 0) { |
| 106 | entropy_init(ctx); |
| 107 | entropy_init_initialized = 1; |
| 108 | } |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 109 | Curl_polarsslthreadlock_unlock_function(0); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 110 | } |
| 111 | /* end of entropy_init_mutex() */ |
| 112 | |
| 113 | /* start of entropy_func_mutex() */ |
| 114 | static int entropy_func_mutex(void *data, unsigned char *output, size_t len) |
| 115 | { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 116 | int ret; |
| 117 | /* lock 1 = entropy_func_mutex() */ |
| 118 | Curl_polarsslthreadlock_lock_function(1); |
| 119 | ret = entropy_func(data, output, len); |
| 120 | Curl_polarsslthreadlock_unlock_function(1); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 121 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 122 | return ret; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 123 | } |
| 124 | /* end of entropy_func_mutex() */ |
| 125 | |
| 126 | #endif /* THREADING_SUPPORT */ |
| 127 | |
| 128 | /* Define this to enable lots of debugging for PolarSSL */ |
| 129 | #undef POLARSSL_DEBUG |
| 130 | |
| 131 | #ifdef POLARSSL_DEBUG |
| 132 | static void polarssl_debug(void *context, int level, const char *line) |
| 133 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 134 | struct Curl_easy *data = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 135 | |
| 136 | if(!context) |
| 137 | return; |
| 138 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 139 | data = (struct Curl_easy *)context; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 140 | |
| 141 | infof(data, "%s", line); |
| 142 | (void) level; |
| 143 | } |
| 144 | #else |
| 145 | #endif |
| 146 | |
| 147 | /* ALPN for http2? */ |
| 148 | #ifdef POLARSSL_SSL_ALPN |
| 149 | # define HAS_ALPN |
| 150 | #endif |
| 151 | |
| 152 | static Curl_recv polarssl_recv; |
| 153 | static Curl_send polarssl_send; |
| 154 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 155 | static CURLcode polarssl_version_from_curl(int *polarver, long ssl_version) |
| 156 | { |
| 157 | switch(ssl_version) { |
| 158 | case CURL_SSLVERSION_TLSv1_0: |
| 159 | *polarver = SSL_MINOR_VERSION_1; |
| 160 | return CURLE_OK; |
| 161 | case CURL_SSLVERSION_TLSv1_1: |
| 162 | *polarver = SSL_MINOR_VERSION_2; |
| 163 | return CURLE_OK; |
| 164 | case CURL_SSLVERSION_TLSv1_2: |
| 165 | *polarver = SSL_MINOR_VERSION_3; |
| 166 | return CURLE_OK; |
| 167 | case CURL_SSLVERSION_TLSv1_3: |
| 168 | break; |
| 169 | } |
| 170 | return CURLE_SSL_CONNECT_ERROR; |
| 171 | } |
| 172 | |
| 173 | static CURLcode |
| 174 | set_ssl_version_min_max(struct connectdata *conn, int sockindex) |
| 175 | { |
| 176 | struct Curl_easy *data = conn->data; |
| 177 | struct ssl_connect_data* connssl = &conn->ssl[sockindex]; |
| 178 | long ssl_version = SSL_CONN_CONFIG(version); |
| 179 | long ssl_version_max = SSL_CONN_CONFIG(version_max); |
| 180 | int ssl_min_ver = SSL_MINOR_VERSION_1; |
| 181 | int ssl_max_ver = SSL_MINOR_VERSION_1; |
| 182 | CURLcode result = CURLE_OK; |
| 183 | |
| 184 | switch(ssl_version) { |
| 185 | case CURL_SSLVERSION_DEFAULT: |
| 186 | case CURL_SSLVERSION_TLSv1: |
| 187 | ssl_version = CURL_SSLVERSION_TLSv1_0; |
| 188 | ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2; |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | switch(ssl_version_max) { |
| 193 | case CURL_SSLVERSION_MAX_NONE: |
| 194 | ssl_version_max = ssl_version << 16; |
| 195 | break; |
| 196 | case CURL_SSLVERSION_MAX_DEFAULT: |
| 197 | ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | result = polarssl_version_from_curl(&ssl_min_ver, ssl_version); |
| 202 | if(result) { |
| 203 | failf(data, "unsupported min version passed via CURLOPT_SSLVERSION"); |
| 204 | return result; |
| 205 | } |
| 206 | result = polarssl_version_from_curl(&ssl_max_ver, ssl_version_max >> 16); |
| 207 | if(result) { |
| 208 | failf(data, "unsupported max version passed via CURLOPT_SSLVERSION"); |
| 209 | return result; |
| 210 | } |
| 211 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 212 | ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, ssl_min_ver); |
| 213 | ssl_set_max_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, ssl_max_ver); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 214 | |
| 215 | return result; |
| 216 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 217 | |
| 218 | static CURLcode |
| 219 | polarssl_connect_step1(struct connectdata *conn, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 220 | int sockindex) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 221 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 222 | struct Curl_easy *data = conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 223 | struct ssl_connect_data* connssl = &conn->ssl[sockindex]; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 224 | const char *capath = SSL_CONN_CONFIG(CApath); |
| 225 | const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name : |
| 226 | conn->host.name; |
| 227 | const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 228 | int ret = -1; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 229 | char errorbuf[128]; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 230 | errorbuf[0] = 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 231 | |
| 232 | /* PolarSSL only supports SSLv3 and TLSv1 */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 233 | if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 234 | failf(data, "PolarSSL does not support SSLv2"); |
| 235 | return CURLE_SSL_CONNECT_ERROR; |
| 236 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 237 | |
| 238 | #ifdef THREADING_SUPPORT |
| 239 | entropy_init_mutex(&entropy); |
| 240 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 241 | if((ret = ctr_drbg_init(&BACKEND->ctr_drbg, entropy_func_mutex, &entropy, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 242 | NULL, 0)) != 0) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 243 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 244 | failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n", |
| 245 | -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 246 | } |
| 247 | #else |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 248 | entropy_init(&BACKEND->entropy); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 249 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 250 | if((ret = ctr_drbg_init(&BACKEND->ctr_drbg, entropy_func, &BACKEND->entropy, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 251 | NULL, 0)) != 0) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 252 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 253 | failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n", |
| 254 | -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 255 | } |
| 256 | #endif /* THREADING_SUPPORT */ |
| 257 | |
| 258 | /* Load the trusted CA */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 259 | memset(&BACKEND->cacert, 0, sizeof(x509_crt)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 260 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 261 | if(SSL_CONN_CONFIG(CAfile)) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 262 | ret = x509_crt_parse_file(&BACKEND->cacert, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 263 | SSL_CONN_CONFIG(CAfile)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 264 | |
| 265 | if(ret<0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 266 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 267 | failf(data, "Error reading ca cert file %s - PolarSSL: (-0x%04X) %s", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 268 | SSL_CONN_CONFIG(CAfile), -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 269 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 270 | if(SSL_CONN_CONFIG(verifypeer)) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 271 | return CURLE_SSL_CACERT_BADFILE; |
| 272 | } |
| 273 | } |
| 274 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 275 | if(capath) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 276 | ret = x509_crt_parse_path(&BACKEND->cacert, capath); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 277 | |
| 278 | if(ret<0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 279 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 280 | failf(data, "Error reading ca cert path %s - PolarSSL: (-0x%04X) %s", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 281 | capath, -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 282 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 283 | if(SSL_CONN_CONFIG(verifypeer)) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 284 | return CURLE_SSL_CACERT_BADFILE; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* Load the client certificate */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 289 | memset(&BACKEND->clicert, 0, sizeof(x509_crt)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 290 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 291 | if(SSL_SET_OPTION(cert)) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 292 | ret = x509_crt_parse_file(&BACKEND->clicert, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 293 | SSL_SET_OPTION(cert)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 294 | |
| 295 | if(ret) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 296 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 297 | failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 298 | SSL_SET_OPTION(cert), -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 299 | |
| 300 | return CURLE_SSL_CERTPROBLEM; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* Load the client private key */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 305 | if(SSL_SET_OPTION(key)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 306 | pk_context pk; |
| 307 | pk_init(&pk); |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 308 | ret = pk_parse_keyfile(&pk, SSL_SET_OPTION(key), |
| 309 | SSL_SET_OPTION(key_passwd)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 310 | if(ret == 0 && !pk_can_do(&pk, POLARSSL_PK_RSA)) |
| 311 | ret = POLARSSL_ERR_PK_TYPE_MISMATCH; |
| 312 | if(ret == 0) |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 313 | rsa_copy(&BACKEND->rsa, pk_rsa(pk)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 314 | else |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 315 | rsa_free(&BACKEND->rsa); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 316 | pk_free(&pk); |
| 317 | |
| 318 | if(ret) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 319 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 320 | failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 321 | SSL_SET_OPTION(key), -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 322 | |
| 323 | return CURLE_SSL_CERTPROBLEM; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* Load the CRL */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 328 | memset(&BACKEND->crl, 0, sizeof(x509_crl)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 329 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 330 | if(SSL_SET_OPTION(CRLfile)) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 331 | ret = x509_crl_parse_file(&BACKEND->crl, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 332 | SSL_SET_OPTION(CRLfile)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 333 | |
| 334 | if(ret) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 335 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 336 | failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s", |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 337 | SSL_SET_OPTION(CRLfile), -ret, errorbuf); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 338 | |
| 339 | return CURLE_SSL_CRL_BADFILE; |
| 340 | } |
| 341 | } |
| 342 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 343 | infof(data, "PolarSSL: Connecting to %s:%d\n", hostname, port); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 344 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 345 | if(ssl_init(&BACKEND->ssl)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 346 | failf(data, "PolarSSL: ssl_init failed"); |
| 347 | return CURLE_SSL_CONNECT_ERROR; |
| 348 | } |
| 349 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 350 | switch(SSL_CONN_CONFIG(version)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 351 | case CURL_SSLVERSION_DEFAULT: |
| 352 | case CURL_SSLVERSION_TLSv1: |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 353 | ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 354 | SSL_MINOR_VERSION_1); |
| 355 | break; |
| 356 | case CURL_SSLVERSION_SSLv3: |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 357 | ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 358 | SSL_MINOR_VERSION_0); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 359 | ssl_set_max_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 360 | SSL_MINOR_VERSION_0); |
| 361 | infof(data, "PolarSSL: Forced min. SSL Version to be SSLv3\n"); |
| 362 | break; |
| 363 | case CURL_SSLVERSION_TLSv1_0: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 364 | case CURL_SSLVERSION_TLSv1_1: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 365 | case CURL_SSLVERSION_TLSv1_2: |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 366 | case CURL_SSLVERSION_TLSv1_3: |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 367 | { |
| 368 | CURLcode result = set_ssl_version_min_max(conn, sockindex); |
| 369 | if(result != CURLE_OK) |
| 370 | return result; |
| 371 | break; |
| 372 | } |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 373 | default: |
| 374 | failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION"); |
| 375 | return CURLE_SSL_CONNECT_ERROR; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 378 | ssl_set_endpoint(&BACKEND->ssl, SSL_IS_CLIENT); |
| 379 | ssl_set_authmode(&BACKEND->ssl, SSL_VERIFY_OPTIONAL); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 380 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 381 | ssl_set_rng(&BACKEND->ssl, ctr_drbg_random, |
| 382 | &BACKEND->ctr_drbg); |
| 383 | ssl_set_bio(&BACKEND->ssl, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 384 | net_recv, &conn->sock[sockindex], |
| 385 | net_send, &conn->sock[sockindex]); |
| 386 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 387 | ssl_set_ciphersuites(&BACKEND->ssl, ssl_list_ciphersuites()); |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 388 | |
| 389 | /* Check if there's a cached ID we can/should use here! */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 390 | if(SSL_SET_OPTION(primary.sessionid)) { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 391 | void *old_session = NULL; |
| 392 | |
| 393 | Curl_ssl_sessionid_lock(conn); |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 394 | if(!Curl_ssl_getsessionid(conn, &old_session, NULL, sockindex)) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 395 | ret = ssl_set_session(&BACKEND->ssl, old_session); |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 396 | if(ret) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 397 | Curl_ssl_sessionid_unlock(conn); |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 398 | failf(data, "ssl_set_session returned -0x%x", -ret); |
| 399 | return CURLE_SSL_CONNECT_ERROR; |
| 400 | } |
| 401 | infof(data, "PolarSSL re-using session\n"); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 402 | } |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 403 | Curl_ssl_sessionid_unlock(conn); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 406 | ssl_set_ca_chain(&BACKEND->ssl, |
| 407 | &BACKEND->cacert, |
| 408 | &BACKEND->crl, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 409 | hostname); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 410 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 411 | ssl_set_own_cert_rsa(&BACKEND->ssl, |
| 412 | &BACKEND->clicert, &BACKEND->rsa); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 413 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 414 | if(ssl_set_hostname(&BACKEND->ssl, hostname)) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 415 | /* ssl_set_hostname() sets the name to use in CN/SAN checks *and* the name |
| 416 | to set in the SNI extension. So even if curl connects to a host |
| 417 | specified as an IP address, this function must be used. */ |
| 418 | failf(data, "couldn't set hostname in PolarSSL"); |
| 419 | return CURLE_SSL_CONNECT_ERROR; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | #ifdef HAS_ALPN |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 423 | if(conn->bits.tls_enable_alpn) { |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 424 | static const char *protocols[3]; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 425 | int cur = 0; |
| 426 | |
| 427 | #ifdef USE_NGHTTP2 |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 428 | if(data->set.httpversion >= CURL_HTTP_VERSION_2) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 429 | protocols[cur++] = NGHTTP2_PROTO_VERSION_ID; |
| 430 | infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID); |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | protocols[cur++] = ALPN_HTTP_1_1; |
| 435 | infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1); |
| 436 | |
| 437 | protocols[cur] = NULL; |
| 438 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 439 | ssl_set_alpn_protocols(&BACKEND->ssl, protocols); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 440 | } |
| 441 | #endif |
| 442 | |
| 443 | #ifdef POLARSSL_DEBUG |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 444 | ssl_set_dbg(&BACKEND->ssl, polarssl_debug, data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 445 | #endif |
| 446 | |
| 447 | connssl->connecting_state = ssl_connect_2; |
| 448 | |
| 449 | return CURLE_OK; |
| 450 | } |
| 451 | |
| 452 | static CURLcode |
| 453 | polarssl_connect_step2(struct connectdata *conn, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 454 | int sockindex) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 455 | { |
| 456 | int ret; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 457 | struct Curl_easy *data = conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 458 | struct ssl_connect_data* connssl = &conn->ssl[sockindex]; |
| 459 | char buffer[1024]; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 460 | const char * const pinnedpubkey = SSL_IS_PROXY() ? |
| 461 | data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] : |
| 462 | data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG]; |
| 463 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 464 | |
| 465 | char errorbuf[128]; |
| 466 | errorbuf[0] = 0; |
| 467 | |
| 468 | conn->recv[sockindex] = polarssl_recv; |
| 469 | conn->send[sockindex] = polarssl_send; |
| 470 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 471 | ret = ssl_handshake(&BACKEND->ssl); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 472 | |
| 473 | switch(ret) { |
| 474 | case 0: |
| 475 | break; |
| 476 | |
| 477 | case POLARSSL_ERR_NET_WANT_READ: |
| 478 | connssl->connecting_state = ssl_connect_2_reading; |
| 479 | return CURLE_OK; |
| 480 | |
| 481 | case POLARSSL_ERR_NET_WANT_WRITE: |
| 482 | connssl->connecting_state = ssl_connect_2_writing; |
| 483 | return CURLE_OK; |
| 484 | |
| 485 | default: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 486 | error_strerror(ret, errorbuf, sizeof(errorbuf)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 487 | failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s", |
| 488 | -ret, errorbuf); |
| 489 | return CURLE_SSL_CONNECT_ERROR; |
| 490 | } |
| 491 | |
| 492 | infof(data, "PolarSSL: Handshake complete, cipher is %s\n", |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 493 | ssl_get_ciphersuite(&BACKEND->ssl) ); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 494 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 495 | ret = ssl_get_verify_result(&BACKEND->ssl); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 496 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 497 | if(ret && SSL_CONN_CONFIG(verifypeer)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 498 | if(ret & BADCERT_EXPIRED) |
| 499 | failf(data, "Cert verify failed: BADCERT_EXPIRED"); |
| 500 | |
| 501 | if(ret & BADCERT_REVOKED) { |
| 502 | failf(data, "Cert verify failed: BADCERT_REVOKED"); |
| 503 | return CURLE_SSL_CACERT; |
| 504 | } |
| 505 | |
| 506 | if(ret & BADCERT_CN_MISMATCH) |
| 507 | failf(data, "Cert verify failed: BADCERT_CN_MISMATCH"); |
| 508 | |
| 509 | if(ret & BADCERT_NOT_TRUSTED) |
| 510 | failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED"); |
| 511 | |
| 512 | return CURLE_PEER_FAILED_VERIFICATION; |
| 513 | } |
| 514 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 515 | if(ssl_get_peer_cert(&(BACKEND->ssl))) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 516 | /* If the session was resumed, there will be no peer certs */ |
| 517 | memset(buffer, 0, sizeof(buffer)); |
| 518 | |
| 519 | if(x509_crt_info(buffer, sizeof(buffer), (char *)"* ", |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 520 | ssl_get_peer_cert(&(BACKEND->ssl))) != -1) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 521 | infof(data, "Dumping cert info:\n%s\n", buffer); |
| 522 | } |
| 523 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 524 | /* adapted from mbedtls.c */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 525 | if(pinnedpubkey) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 526 | int size; |
| 527 | CURLcode result; |
| 528 | x509_crt *p; |
| 529 | unsigned char pubkey[PUB_DER_MAX_BYTES]; |
| 530 | const x509_crt *peercert; |
| 531 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 532 | peercert = ssl_get_peer_cert(&BACKEND->ssl); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 533 | |
| 534 | if(!peercert || !peercert->raw.p || !peercert->raw.len) { |
| 535 | failf(data, "Failed due to missing peer certificate"); |
| 536 | return CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 537 | } |
| 538 | |
| 539 | p = calloc(1, sizeof(*p)); |
| 540 | |
| 541 | if(!p) |
| 542 | return CURLE_OUT_OF_MEMORY; |
| 543 | |
| 544 | x509_crt_init(p); |
| 545 | |
| 546 | /* Make a copy of our const peercert because pk_write_pubkey_der |
| 547 | needs a non-const key, for now. |
| 548 | https://github.com/ARMmbed/mbedtls/issues/396 */ |
| 549 | if(x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) { |
| 550 | failf(data, "Failed copying peer certificate"); |
| 551 | x509_crt_free(p); |
| 552 | free(p); |
| 553 | return CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 554 | } |
| 555 | |
| 556 | size = pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES); |
| 557 | |
| 558 | if(size <= 0) { |
| 559 | failf(data, "Failed copying public key from peer certificate"); |
| 560 | x509_crt_free(p); |
| 561 | free(p); |
| 562 | return CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 563 | } |
| 564 | |
| 565 | /* pk_write_pubkey_der writes data at the end of the buffer. */ |
| 566 | result = Curl_pin_peer_pubkey(data, |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 567 | pinnedpubkey, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 568 | &pubkey[PUB_DER_MAX_BYTES - size], size); |
| 569 | if(result) { |
| 570 | x509_crt_free(p); |
| 571 | free(p); |
| 572 | return result; |
| 573 | } |
| 574 | |
| 575 | x509_crt_free(p); |
| 576 | free(p); |
| 577 | } |
| 578 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 579 | #ifdef HAS_ALPN |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 580 | if(conn->bits.tls_enable_alpn) { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 581 | const char *next_protocol = ssl_get_alpn_protocol(&BACKEND->ssl); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 582 | |
| 583 | if(next_protocol != NULL) { |
| 584 | infof(data, "ALPN, server accepted to use %s\n", next_protocol); |
| 585 | |
| 586 | #ifdef USE_NGHTTP2 |
| 587 | if(!strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID, |
| 588 | NGHTTP2_PROTO_VERSION_ID_LEN)) { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 589 | conn->negnpn = CURL_HTTP_VERSION_2; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 590 | } |
| 591 | else |
| 592 | #endif |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 593 | if(!strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH)) { |
| 594 | conn->negnpn = CURL_HTTP_VERSION_1_1; |
| 595 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 596 | } |
| 597 | else |
| 598 | infof(data, "ALPN, server did not agree to a protocol\n"); |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | connssl->connecting_state = ssl_connect_3; |
| 603 | infof(data, "SSL connected\n"); |
| 604 | |
| 605 | return CURLE_OK; |
| 606 | } |
| 607 | |
| 608 | static CURLcode |
| 609 | polarssl_connect_step3(struct connectdata *conn, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 610 | int sockindex) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 611 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 612 | CURLcode retcode = CURLE_OK; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 613 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 614 | struct Curl_easy *data = conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 615 | |
| 616 | DEBUGASSERT(ssl_connect_3 == connssl->connecting_state); |
| 617 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 618 | if(SSL_SET_OPTION(primary.sessionid)) { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 619 | int ret; |
| 620 | ssl_session *our_ssl_sessionid; |
| 621 | void *old_ssl_sessionid = NULL; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 622 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame^] | 623 | our_ssl_sessionid = calloc(1, sizeof(ssl_session)); |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 624 | if(!our_ssl_sessionid) |
| 625 | return CURLE_OUT_OF_MEMORY; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 626 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 627 | ret = ssl_get_session(&BACKEND->ssl, our_ssl_sessionid); |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 628 | if(ret) { |
| 629 | failf(data, "ssl_get_session returned -0x%x", -ret); |
| 630 | return CURLE_SSL_CONNECT_ERROR; |
| 631 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 632 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 633 | /* If there's already a matching session in the cache, delete it */ |
| 634 | Curl_ssl_sessionid_lock(conn); |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 635 | if(!Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL, sockindex)) |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 636 | Curl_ssl_delsessionid(conn, old_ssl_sessionid); |
| 637 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 638 | retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid, 0, sockindex); |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 639 | Curl_ssl_sessionid_unlock(conn); |
| 640 | if(retcode) { |
| 641 | free(our_ssl_sessionid); |
| 642 | failf(data, "failed to store ssl session"); |
| 643 | return retcode; |
| 644 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | connssl->connecting_state = ssl_connect_done; |
| 648 | |
| 649 | return CURLE_OK; |
| 650 | } |
| 651 | |
| 652 | static ssize_t polarssl_send(struct connectdata *conn, |
| 653 | int sockindex, |
| 654 | const void *mem, |
| 655 | size_t len, |
| 656 | CURLcode *curlcode) |
| 657 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 658 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 659 | int ret = -1; |
| 660 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 661 | ret = ssl_write(&BACKEND->ssl, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 662 | (unsigned char *)mem, len); |
| 663 | |
| 664 | if(ret < 0) { |
| 665 | *curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ? |
| 666 | CURLE_AGAIN : CURLE_SEND_ERROR; |
| 667 | ret = -1; |
| 668 | } |
| 669 | |
| 670 | return ret; |
| 671 | } |
| 672 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 673 | static void Curl_polarssl_close(struct connectdata *conn, int sockindex) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 674 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 675 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 676 | rsa_free(&BACKEND->rsa); |
| 677 | x509_crt_free(&BACKEND->clicert); |
| 678 | x509_crt_free(&BACKEND->cacert); |
| 679 | x509_crl_free(&BACKEND->crl); |
| 680 | ssl_free(&BACKEND->ssl); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static ssize_t polarssl_recv(struct connectdata *conn, |
| 684 | int num, |
| 685 | char *buf, |
| 686 | size_t buffersize, |
| 687 | CURLcode *curlcode) |
| 688 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 689 | struct ssl_connect_data *connssl = &conn->ssl[num]; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 690 | int ret = -1; |
| 691 | ssize_t len = -1; |
| 692 | |
| 693 | memset(buf, 0, buffersize); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 694 | ret = ssl_read(&BACKEND->ssl, (unsigned char *)buf, buffersize); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 695 | |
| 696 | if(ret <= 0) { |
| 697 | if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) |
| 698 | return 0; |
| 699 | |
| 700 | *curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ? |
| 701 | CURLE_AGAIN : CURLE_RECV_ERROR; |
| 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | len = ret; |
| 706 | |
| 707 | return len; |
| 708 | } |
| 709 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 710 | static void Curl_polarssl_session_free(void *ptr) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 711 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 712 | ssl_session_free(ptr); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 713 | free(ptr); |
| 714 | } |
| 715 | |
| 716 | /* 1.3.10 was the first rebranded version. All new releases (in 1.3 branch and |
| 717 | higher) will be mbed TLS branded.. */ |
| 718 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 719 | static size_t Curl_polarssl_version(char *buffer, size_t size) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 720 | { |
| 721 | unsigned int version = version_get_number(); |
| 722 | return snprintf(buffer, size, "%s/%d.%d.%d", |
| 723 | version >= 0x01030A00?"mbedTLS":"PolarSSL", |
| 724 | version>>24, (version>>16)&0xff, (version>>8)&0xff); |
| 725 | } |
| 726 | |
| 727 | static CURLcode |
| 728 | polarssl_connect_common(struct connectdata *conn, |
| 729 | int sockindex, |
| 730 | bool nonblocking, |
| 731 | bool *done) |
| 732 | { |
| 733 | CURLcode result; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 734 | struct Curl_easy *data = conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 735 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 736 | curl_socket_t sockfd = conn->sock[sockindex]; |
| 737 | long timeout_ms; |
| 738 | int what; |
| 739 | |
| 740 | /* check if the connection has already been established */ |
| 741 | if(ssl_connection_complete == connssl->state) { |
| 742 | *done = TRUE; |
| 743 | return CURLE_OK; |
| 744 | } |
| 745 | |
| 746 | if(ssl_connect_1 == connssl->connecting_state) { |
| 747 | /* Find out how much more time we're allowed */ |
| 748 | timeout_ms = Curl_timeleft(data, NULL, TRUE); |
| 749 | |
| 750 | if(timeout_ms < 0) { |
| 751 | /* no need to continue if time already is up */ |
| 752 | failf(data, "SSL connection timeout"); |
| 753 | return CURLE_OPERATION_TIMEDOUT; |
| 754 | } |
| 755 | |
| 756 | result = polarssl_connect_step1(conn, sockindex); |
| 757 | if(result) |
| 758 | return result; |
| 759 | } |
| 760 | |
| 761 | while(ssl_connect_2 == connssl->connecting_state || |
| 762 | ssl_connect_2_reading == connssl->connecting_state || |
| 763 | ssl_connect_2_writing == connssl->connecting_state) { |
| 764 | |
| 765 | /* check allowed time left */ |
| 766 | timeout_ms = Curl_timeleft(data, NULL, TRUE); |
| 767 | |
| 768 | if(timeout_ms < 0) { |
| 769 | /* no need to continue if time already is up */ |
| 770 | failf(data, "SSL connection timeout"); |
| 771 | return CURLE_OPERATION_TIMEDOUT; |
| 772 | } |
| 773 | |
| 774 | /* if ssl is expecting something, check if it's available. */ |
| 775 | if(connssl->connecting_state == ssl_connect_2_reading || |
| 776 | connssl->connecting_state == ssl_connect_2_writing) { |
| 777 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 778 | curl_socket_t writefd = ssl_connect_2_writing == |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 779 | connssl->connecting_state?sockfd:CURL_SOCKET_BAD; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 780 | curl_socket_t readfd = ssl_connect_2_reading == |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 781 | connssl->connecting_state?sockfd:CURL_SOCKET_BAD; |
| 782 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 783 | what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, |
| 784 | nonblocking?0:timeout_ms); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 785 | if(what < 0) { |
| 786 | /* fatal error */ |
| 787 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); |
| 788 | return CURLE_SSL_CONNECT_ERROR; |
| 789 | } |
| 790 | else if(0 == what) { |
| 791 | if(nonblocking) { |
| 792 | *done = FALSE; |
| 793 | return CURLE_OK; |
| 794 | } |
| 795 | else { |
| 796 | /* timeout */ |
| 797 | failf(data, "SSL connection timeout"); |
| 798 | return CURLE_OPERATION_TIMEDOUT; |
| 799 | } |
| 800 | } |
| 801 | /* socket is readable or writable */ |
| 802 | } |
| 803 | |
| 804 | /* Run transaction, and return to the caller if it failed or if |
| 805 | * this connection is part of a multi handle and this loop would |
| 806 | * execute again. This permits the owner of a multi handle to |
| 807 | * abort a connection attempt before step2 has completed while |
| 808 | * ensuring that a client using select() or epoll() will always |
| 809 | * have a valid fdset to wait on. |
| 810 | */ |
| 811 | result = polarssl_connect_step2(conn, sockindex); |
| 812 | if(result || (nonblocking && |
| 813 | (ssl_connect_2 == connssl->connecting_state || |
| 814 | ssl_connect_2_reading == connssl->connecting_state || |
| 815 | ssl_connect_2_writing == connssl->connecting_state))) |
| 816 | return result; |
| 817 | |
| 818 | } /* repeat step2 until all transactions are done. */ |
| 819 | |
| 820 | if(ssl_connect_3 == connssl->connecting_state) { |
| 821 | result = polarssl_connect_step3(conn, sockindex); |
| 822 | if(result) |
| 823 | return result; |
| 824 | } |
| 825 | |
| 826 | if(ssl_connect_done == connssl->connecting_state) { |
| 827 | connssl->state = ssl_connection_complete; |
| 828 | conn->recv[sockindex] = polarssl_recv; |
| 829 | conn->send[sockindex] = polarssl_send; |
| 830 | *done = TRUE; |
| 831 | } |
| 832 | else |
| 833 | *done = FALSE; |
| 834 | |
| 835 | /* Reset our connect state machine */ |
| 836 | connssl->connecting_state = ssl_connect_1; |
| 837 | |
| 838 | return CURLE_OK; |
| 839 | } |
| 840 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 841 | static CURLcode Curl_polarssl_connect_nonblocking(struct connectdata *conn, |
| 842 | int sockindex, bool *done) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 843 | { |
| 844 | return polarssl_connect_common(conn, sockindex, TRUE, done); |
| 845 | } |
| 846 | |
| 847 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 848 | static CURLcode Curl_polarssl_connect(struct connectdata *conn, int sockindex) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 849 | { |
| 850 | CURLcode result; |
| 851 | bool done = FALSE; |
| 852 | |
| 853 | result = polarssl_connect_common(conn, sockindex, FALSE, &done); |
| 854 | if(result) |
| 855 | return result; |
| 856 | |
| 857 | DEBUGASSERT(done); |
| 858 | |
| 859 | return CURLE_OK; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * return 0 error initializing SSL |
| 864 | * return 1 SSL initialized successfully |
| 865 | */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 866 | static int Curl_polarssl_init(void) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 867 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 868 | return Curl_polarsslthreadlock_thread_setup(); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 871 | static void Curl_polarssl_cleanup(void) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 872 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 873 | (void)Curl_polarsslthreadlock_thread_cleanup(); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 876 | static bool Curl_polarssl_data_pending(const struct connectdata *conn, |
| 877 | int sockindex) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 878 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 879 | const struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 880 | return ssl_get_bytes_avail(&BACKEND->ssl) != 0; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame^] | 883 | static CURLcode Curl_polarssl_sha256sum(const unsigned char *input, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 884 | size_t inputlen, |
| 885 | unsigned char *sha256sum, |
| 886 | size_t sha256len UNUSED_PARAM) |
| 887 | { |
| 888 | (void)sha256len; |
| 889 | sha256(input, inputlen, sha256sum, 0); |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame^] | 890 | return CURLE_OK; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static void *Curl_polarssl_get_internals(struct ssl_connect_data *connssl, |
| 894 | CURLINFO info UNUSED_PARAM) |
| 895 | { |
| 896 | (void)info; |
| 897 | return &BACKEND->ssl; |
| 898 | } |
| 899 | |
| 900 | const struct Curl_ssl Curl_ssl_polarssl = { |
| 901 | { CURLSSLBACKEND_POLARSSL, "polarssl" }, /* info */ |
| 902 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame^] | 903 | SSLSUPP_CA_PATH | |
| 904 | SSLSUPP_PINNEDPUBKEY, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 905 | |
| 906 | sizeof(struct ssl_backend_data), |
| 907 | |
| 908 | Curl_polarssl_init, /* init */ |
| 909 | Curl_polarssl_cleanup, /* cleanup */ |
| 910 | Curl_polarssl_version, /* version */ |
| 911 | Curl_none_check_cxn, /* check_cxn */ |
| 912 | Curl_none_shutdown, /* shutdown */ |
| 913 | Curl_polarssl_data_pending, /* data_pending */ |
| 914 | /* This might cause libcurl to use a weeker random! |
| 915 | * TODO: use Polarssl's CTR-DRBG or HMAC-DRBG |
| 916 | */ |
| 917 | Curl_none_random, /* random */ |
| 918 | Curl_none_cert_status_request, /* cert_status_request */ |
| 919 | Curl_polarssl_connect, /* connect */ |
| 920 | Curl_polarssl_connect_nonblocking, /* connect_nonblocking */ |
| 921 | Curl_polarssl_get_internals, /* get_internals */ |
| 922 | Curl_polarssl_close, /* close_one */ |
| 923 | Curl_none_close_all, /* close_all */ |
| 924 | Curl_polarssl_session_free, /* session_free */ |
| 925 | Curl_none_set_engine, /* set_engine */ |
| 926 | Curl_none_set_engine_default, /* set_engine_default */ |
| 927 | Curl_none_engines_list, /* engines_list */ |
| 928 | Curl_none_false_start, /* false_start */ |
| 929 | Curl_none_md5sum, /* md5sum */ |
| 930 | Curl_polarssl_sha256sum /* sha256sum */ |
| 931 | }; |
| 932 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 933 | #endif /* USE_POLARSSL */ |