1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | * Copyright (C) Hoi-Ho Chan, <[email protected]>
|
---|
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
|
---|
13 | * are also available at https://curl.se/docs/copyright.html.
|
---|
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 | * SPDX-License-Identifier: curl
|
---|
23 | *
|
---|
24 | ***************************************************************************/
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * Source file for all mbedTLS-specific code for the TLS/SSL layer. No code
|
---|
28 | * but vtls.c should ever call or use these functions.
|
---|
29 | *
|
---|
30 | */
|
---|
31 |
|
---|
32 | #include "curl_setup.h"
|
---|
33 |
|
---|
34 | #ifdef USE_MBEDTLS
|
---|
35 |
|
---|
36 | /* Define this to enable lots of debugging for mbedTLS */
|
---|
37 | /* #define MBEDTLS_DEBUG */
|
---|
38 |
|
---|
39 | #include <mbedtls/version.h>
|
---|
40 | #if MBEDTLS_VERSION_NUMBER >= 0x02040000
|
---|
41 | #include <mbedtls/net_sockets.h>
|
---|
42 | #else
|
---|
43 | #include <mbedtls/net.h>
|
---|
44 | #endif
|
---|
45 | #include <mbedtls/ssl.h>
|
---|
46 | #include <mbedtls/x509.h>
|
---|
47 |
|
---|
48 | #include <mbedtls/error.h>
|
---|
49 | #include <mbedtls/entropy.h>
|
---|
50 | #include <mbedtls/ctr_drbg.h>
|
---|
51 | #include <mbedtls/sha256.h>
|
---|
52 |
|
---|
53 | #if MBEDTLS_VERSION_MAJOR >= 2
|
---|
54 | # ifdef MBEDTLS_DEBUG
|
---|
55 | # include <mbedtls/debug.h>
|
---|
56 | # endif
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #include "urldata.h"
|
---|
60 | #include "sendf.h"
|
---|
61 | #include "inet_pton.h"
|
---|
62 | #include "mbedtls.h"
|
---|
63 | #include "vtls.h"
|
---|
64 | #include "vtls_int.h"
|
---|
65 | #include "parsedate.h"
|
---|
66 | #include "connect.h" /* for the connect timeout */
|
---|
67 | #include "select.h"
|
---|
68 | #include "multiif.h"
|
---|
69 | #include "mbedtls_threadlock.h"
|
---|
70 |
|
---|
71 | /* The last 3 #include files should be in this order */
|
---|
72 | #include "curl_printf.h"
|
---|
73 | #include "curl_memory.h"
|
---|
74 | #include "memdebug.h"
|
---|
75 |
|
---|
76 | /* ALPN for http2 */
|
---|
77 | #ifdef USE_HTTP2
|
---|
78 | # undef HAS_ALPN
|
---|
79 | # ifdef MBEDTLS_SSL_ALPN
|
---|
80 | # define HAS_ALPN
|
---|
81 | # endif
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | struct ssl_backend_data {
|
---|
85 | mbedtls_ctr_drbg_context ctr_drbg;
|
---|
86 | mbedtls_entropy_context entropy;
|
---|
87 | mbedtls_ssl_context ssl;
|
---|
88 | mbedtls_x509_crt cacert;
|
---|
89 | mbedtls_x509_crt clicert;
|
---|
90 | #ifdef MBEDTLS_X509_CRL_PARSE_C
|
---|
91 | mbedtls_x509_crl crl;
|
---|
92 | #endif
|
---|
93 | mbedtls_pk_context pk;
|
---|
94 | mbedtls_ssl_config config;
|
---|
95 | #ifdef HAS_ALPN
|
---|
96 | const char *protocols[3];
|
---|
97 | #endif
|
---|
98 | };
|
---|
99 |
|
---|
100 | /* apply threading? */
|
---|
101 | #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
|
---|
102 | #define THREADING_SUPPORT
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | #ifndef MBEDTLS_ERROR_C
|
---|
106 | #define mbedtls_strerror(a,b,c) b[0] = 0
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | #if defined(THREADING_SUPPORT)
|
---|
110 | static mbedtls_entropy_context ts_entropy;
|
---|
111 |
|
---|
112 | static int entropy_init_initialized = 0;
|
---|
113 |
|
---|
114 | /* start of entropy_init_mutex() */
|
---|
115 | static void entropy_init_mutex(mbedtls_entropy_context *ctx)
|
---|
116 | {
|
---|
117 | /* lock 0 = entropy_init_mutex() */
|
---|
118 | Curl_mbedtlsthreadlock_lock_function(0);
|
---|
119 | if(entropy_init_initialized == 0) {
|
---|
120 | mbedtls_entropy_init(ctx);
|
---|
121 | entropy_init_initialized = 1;
|
---|
122 | }
|
---|
123 | Curl_mbedtlsthreadlock_unlock_function(0);
|
---|
124 | }
|
---|
125 | /* end of entropy_init_mutex() */
|
---|
126 |
|
---|
127 | /* start of entropy_func_mutex() */
|
---|
128 | static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
|
---|
129 | {
|
---|
130 | int ret;
|
---|
131 | /* lock 1 = entropy_func_mutex() */
|
---|
132 | Curl_mbedtlsthreadlock_lock_function(1);
|
---|
133 | ret = mbedtls_entropy_func(data, output, len);
|
---|
134 | Curl_mbedtlsthreadlock_unlock_function(1);
|
---|
135 |
|
---|
136 | return ret;
|
---|
137 | }
|
---|
138 | /* end of entropy_func_mutex() */
|
---|
139 |
|
---|
140 | #endif /* THREADING_SUPPORT */
|
---|
141 |
|
---|
142 | #ifdef MBEDTLS_DEBUG
|
---|
143 | static void mbed_debug(void *context, int level, const char *f_name,
|
---|
144 | int line_nb, const char *line)
|
---|
145 | {
|
---|
146 | struct Curl_easy *data = NULL;
|
---|
147 |
|
---|
148 | if(!context)
|
---|
149 | return;
|
---|
150 |
|
---|
151 | data = (struct Curl_easy *)context;
|
---|
152 |
|
---|
153 | infof(data, "%s", line);
|
---|
154 | (void) level;
|
---|
155 | }
|
---|
156 | #else
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | static int bio_cf_write(void *bio, const unsigned char *buf, size_t blen)
|
---|
160 | {
|
---|
161 | struct Curl_cfilter *cf = bio;
|
---|
162 | struct Curl_easy *data = CF_DATA_CURRENT(cf);
|
---|
163 | ssize_t nwritten;
|
---|
164 | CURLcode result;
|
---|
165 |
|
---|
166 | DEBUGASSERT(data);
|
---|
167 | nwritten = Curl_conn_cf_send(cf->next, data, (char *)buf, blen, &result);
|
---|
168 | DEBUGF(LOG_CF(data, cf, "bio_cf_out_write(len=%zu) -> %zd, err=%d",
|
---|
169 | blen, nwritten, result));
|
---|
170 | if(nwritten < 0 && CURLE_AGAIN == result) {
|
---|
171 | nwritten = MBEDTLS_ERR_SSL_WANT_WRITE;
|
---|
172 | }
|
---|
173 | return (int)nwritten;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static int bio_cf_read(void *bio, unsigned char *buf, size_t blen)
|
---|
177 | {
|
---|
178 | struct Curl_cfilter *cf = bio;
|
---|
179 | struct Curl_easy *data = CF_DATA_CURRENT(cf);
|
---|
180 | ssize_t nread;
|
---|
181 | CURLcode result;
|
---|
182 |
|
---|
183 | DEBUGASSERT(data);
|
---|
184 | /* OpenSSL catches this case, so should we. */
|
---|
185 | if(!buf)
|
---|
186 | return 0;
|
---|
187 |
|
---|
188 | nread = Curl_conn_cf_recv(cf->next, data, (char *)buf, blen, &result);
|
---|
189 | DEBUGF(LOG_CF(data, cf, "bio_cf_in_read(len=%zu) -> %zd, err=%d",
|
---|
190 | blen, nread, result));
|
---|
191 | if(nread < 0 && CURLE_AGAIN == result) {
|
---|
192 | nread = MBEDTLS_ERR_SSL_WANT_READ;
|
---|
193 | }
|
---|
194 | return (int)nread;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * profile
|
---|
199 | */
|
---|
200 | static const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_fr =
|
---|
201 | {
|
---|
202 | /* Hashes from SHA-1 and above */
|
---|
203 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
|
---|
204 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
|
---|
205 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
|
---|
206 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
|
---|
207 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
|
---|
208 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
|
---|
209 | 0xFFFFFFF, /* Any PK alg */
|
---|
210 | 0xFFFFFFF, /* Any curve */
|
---|
211 | 1024, /* RSA min key len */
|
---|
212 | };
|
---|
213 |
|
---|
214 | /* See https://tls.mbed.org/discussions/generic/
|
---|
215 | howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der
|
---|
216 | */
|
---|
217 | #define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
|
---|
218 | #define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES)
|
---|
219 |
|
---|
220 | #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
|
---|
221 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
|
---|
222 |
|
---|
223 | static CURLcode mbedtls_version_from_curl(int *mbedver, long version)
|
---|
224 | {
|
---|
225 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
---|
226 | switch(version) {
|
---|
227 | case CURL_SSLVERSION_TLSv1_0:
|
---|
228 | case CURL_SSLVERSION_TLSv1_1:
|
---|
229 | case CURL_SSLVERSION_TLSv1_2:
|
---|
230 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_3;
|
---|
231 | return CURLE_OK;
|
---|
232 | case CURL_SSLVERSION_TLSv1_3:
|
---|
233 | break;
|
---|
234 | }
|
---|
235 | #else
|
---|
236 | switch(version) {
|
---|
237 | case CURL_SSLVERSION_TLSv1_0:
|
---|
238 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_1;
|
---|
239 | return CURLE_OK;
|
---|
240 | case CURL_SSLVERSION_TLSv1_1:
|
---|
241 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_2;
|
---|
242 | return CURLE_OK;
|
---|
243 | case CURL_SSLVERSION_TLSv1_2:
|
---|
244 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_3;
|
---|
245 | return CURLE_OK;
|
---|
246 | case CURL_SSLVERSION_TLSv1_3:
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | #endif
|
---|
250 |
|
---|
251 | return CURLE_SSL_CONNECT_ERROR;
|
---|
252 | }
|
---|
253 |
|
---|
254 | static CURLcode
|
---|
255 | set_ssl_version_min_max(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
256 | {
|
---|
257 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
258 | struct ssl_backend_data *backend = connssl->backend;
|
---|
259 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
260 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
---|
261 | int mbedtls_ver_min = MBEDTLS_SSL_MINOR_VERSION_3;
|
---|
262 | int mbedtls_ver_max = MBEDTLS_SSL_MINOR_VERSION_3;
|
---|
263 | #else
|
---|
264 | int mbedtls_ver_min = MBEDTLS_SSL_MINOR_VERSION_1;
|
---|
265 | int mbedtls_ver_max = MBEDTLS_SSL_MINOR_VERSION_1;
|
---|
266 | #endif
|
---|
267 | long ssl_version = conn_config->version;
|
---|
268 | long ssl_version_max = conn_config->version_max;
|
---|
269 | CURLcode result = CURLE_OK;
|
---|
270 |
|
---|
271 | DEBUGASSERT(backend);
|
---|
272 |
|
---|
273 | switch(ssl_version) {
|
---|
274 | case CURL_SSLVERSION_DEFAULT:
|
---|
275 | case CURL_SSLVERSION_TLSv1:
|
---|
276 | ssl_version = CURL_SSLVERSION_TLSv1_0;
|
---|
277 | break;
|
---|
278 | }
|
---|
279 |
|
---|
280 | switch(ssl_version_max) {
|
---|
281 | case CURL_SSLVERSION_MAX_NONE:
|
---|
282 | case CURL_SSLVERSION_MAX_DEFAULT:
|
---|
283 | ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
|
---|
284 | break;
|
---|
285 | }
|
---|
286 |
|
---|
287 | result = mbedtls_version_from_curl(&mbedtls_ver_min, ssl_version);
|
---|
288 | if(result) {
|
---|
289 | failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
|
---|
290 | return result;
|
---|
291 | }
|
---|
292 | result = mbedtls_version_from_curl(&mbedtls_ver_max, ssl_version_max >> 16);
|
---|
293 | if(result) {
|
---|
294 | failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
|
---|
295 | return result;
|
---|
296 | }
|
---|
297 |
|
---|
298 | mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
---|
299 | mbedtls_ver_min);
|
---|
300 | mbedtls_ssl_conf_max_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
---|
301 | mbedtls_ver_max);
|
---|
302 |
|
---|
303 | return result;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static CURLcode
|
---|
307 | mbed_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
308 | {
|
---|
309 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
310 | struct ssl_backend_data *backend = connssl->backend;
|
---|
311 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
312 | const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
|
---|
313 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
314 | const char * const ssl_cafile =
|
---|
315 | /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
|
---|
316 | (ca_info_blob ? NULL : conn_config->CAfile);
|
---|
317 | const bool verifypeer = conn_config->verifypeer;
|
---|
318 | const char * const ssl_capath = conn_config->CApath;
|
---|
319 | char * const ssl_cert = ssl_config->primary.clientcert;
|
---|
320 | const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob;
|
---|
321 | const char * const ssl_crlfile = ssl_config->primary.CRLfile;
|
---|
322 | const char *hostname = connssl->hostname;
|
---|
323 | int ret = -1;
|
---|
324 | char errorbuf[128];
|
---|
325 |
|
---|
326 | DEBUGASSERT(backend);
|
---|
327 |
|
---|
328 | if((conn_config->version == CURL_SSLVERSION_SSLv2) ||
|
---|
329 | (conn_config->version == CURL_SSLVERSION_SSLv3)) {
|
---|
330 | failf(data, "Not supported SSL version");
|
---|
331 | return CURLE_NOT_BUILT_IN;
|
---|
332 | }
|
---|
333 |
|
---|
334 | #ifdef THREADING_SUPPORT
|
---|
335 | entropy_init_mutex(&ts_entropy);
|
---|
336 | mbedtls_ctr_drbg_init(&backend->ctr_drbg);
|
---|
337 |
|
---|
338 | ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, entropy_func_mutex,
|
---|
339 | &ts_entropy, NULL, 0);
|
---|
340 | if(ret) {
|
---|
341 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
342 | failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s",
|
---|
343 | -ret, errorbuf);
|
---|
344 | return CURLE_FAILED_INIT;
|
---|
345 | }
|
---|
346 | #else
|
---|
347 | mbedtls_entropy_init(&backend->entropy);
|
---|
348 | mbedtls_ctr_drbg_init(&backend->ctr_drbg);
|
---|
349 |
|
---|
350 | ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, mbedtls_entropy_func,
|
---|
351 | &backend->entropy, NULL, 0);
|
---|
352 | if(ret) {
|
---|
353 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
354 | failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s",
|
---|
355 | -ret, errorbuf);
|
---|
356 | return CURLE_FAILED_INIT;
|
---|
357 | }
|
---|
358 | #endif /* THREADING_SUPPORT */
|
---|
359 |
|
---|
360 | /* Load the trusted CA */
|
---|
361 | mbedtls_x509_crt_init(&backend->cacert);
|
---|
362 |
|
---|
363 | if(ca_info_blob && verifypeer) {
|
---|
364 | /* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null
|
---|
365 | terminated even when provided the exact length, forcing us to waste
|
---|
366 | extra memory here. */
|
---|
367 | unsigned char *newblob = malloc(ca_info_blob->len + 1);
|
---|
368 | if(!newblob)
|
---|
369 | return CURLE_OUT_OF_MEMORY;
|
---|
370 | memcpy(newblob, ca_info_blob->data, ca_info_blob->len);
|
---|
371 | newblob[ca_info_blob->len] = 0; /* null terminate */
|
---|
372 | ret = mbedtls_x509_crt_parse(&backend->cacert, newblob,
|
---|
373 | ca_info_blob->len + 1);
|
---|
374 | free(newblob);
|
---|
375 | if(ret<0) {
|
---|
376 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
377 | failf(data, "Error importing ca cert blob - mbedTLS: (-0x%04X) %s",
|
---|
378 | -ret, errorbuf);
|
---|
379 | return CURLE_SSL_CERTPROBLEM;
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | if(ssl_cafile && verifypeer) {
|
---|
384 | #ifdef MBEDTLS_FS_IO
|
---|
385 | ret = mbedtls_x509_crt_parse_file(&backend->cacert, ssl_cafile);
|
---|
386 |
|
---|
387 | if(ret<0) {
|
---|
388 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
389 | failf(data, "Error reading ca cert file %s - mbedTLS: (-0x%04X) %s",
|
---|
390 | ssl_cafile, -ret, errorbuf);
|
---|
391 | return CURLE_SSL_CACERT_BADFILE;
|
---|
392 | }
|
---|
393 | #else
|
---|
394 | failf(data, "mbedtls: functions that use the filesystem not built in");
|
---|
395 | return CURLE_NOT_BUILT_IN;
|
---|
396 | #endif
|
---|
397 | }
|
---|
398 |
|
---|
399 | if(ssl_capath) {
|
---|
400 | #ifdef MBEDTLS_FS_IO
|
---|
401 | ret = mbedtls_x509_crt_parse_path(&backend->cacert, ssl_capath);
|
---|
402 |
|
---|
403 | if(ret<0) {
|
---|
404 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
405 | failf(data, "Error reading ca cert path %s - mbedTLS: (-0x%04X) %s",
|
---|
406 | ssl_capath, -ret, errorbuf);
|
---|
407 |
|
---|
408 | if(verifypeer)
|
---|
409 | return CURLE_SSL_CACERT_BADFILE;
|
---|
410 | }
|
---|
411 | #else
|
---|
412 | failf(data, "mbedtls: functions that use the filesystem not built in");
|
---|
413 | return CURLE_NOT_BUILT_IN;
|
---|
414 | #endif
|
---|
415 | }
|
---|
416 |
|
---|
417 | /* Load the client certificate */
|
---|
418 | mbedtls_x509_crt_init(&backend->clicert);
|
---|
419 |
|
---|
420 | if(ssl_cert) {
|
---|
421 | #ifdef MBEDTLS_FS_IO
|
---|
422 | ret = mbedtls_x509_crt_parse_file(&backend->clicert, ssl_cert);
|
---|
423 |
|
---|
424 | if(ret) {
|
---|
425 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
426 | failf(data, "Error reading client cert file %s - mbedTLS: (-0x%04X) %s",
|
---|
427 | ssl_cert, -ret, errorbuf);
|
---|
428 |
|
---|
429 | return CURLE_SSL_CERTPROBLEM;
|
---|
430 | }
|
---|
431 | #else
|
---|
432 | failf(data, "mbedtls: functions that use the filesystem not built in");
|
---|
433 | return CURLE_NOT_BUILT_IN;
|
---|
434 | #endif
|
---|
435 | }
|
---|
436 |
|
---|
437 | if(ssl_cert_blob) {
|
---|
438 | /* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null
|
---|
439 | terminated even when provided the exact length, forcing us to waste
|
---|
440 | extra memory here. */
|
---|
441 | unsigned char *newblob = malloc(ssl_cert_blob->len + 1);
|
---|
442 | if(!newblob)
|
---|
443 | return CURLE_OUT_OF_MEMORY;
|
---|
444 | memcpy(newblob, ssl_cert_blob->data, ssl_cert_blob->len);
|
---|
445 | newblob[ssl_cert_blob->len] = 0; /* null terminate */
|
---|
446 | ret = mbedtls_x509_crt_parse(&backend->clicert, newblob,
|
---|
447 | ssl_cert_blob->len + 1);
|
---|
448 | free(newblob);
|
---|
449 |
|
---|
450 | if(ret) {
|
---|
451 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
452 | failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s",
|
---|
453 | ssl_config->key, -ret, errorbuf);
|
---|
454 | return CURLE_SSL_CERTPROBLEM;
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | /* Load the client private key */
|
---|
459 | mbedtls_pk_init(&backend->pk);
|
---|
460 |
|
---|
461 | if(ssl_config->key || ssl_config->key_blob) {
|
---|
462 | if(ssl_config->key) {
|
---|
463 | #ifdef MBEDTLS_FS_IO
|
---|
464 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
---|
465 | ret = mbedtls_pk_parse_keyfile(&backend->pk, ssl_config->key,
|
---|
466 | ssl_config->key_passwd,
|
---|
467 | mbedtls_ctr_drbg_random,
|
---|
468 | &backend->ctr_drbg);
|
---|
469 | #else
|
---|
470 | ret = mbedtls_pk_parse_keyfile(&backend->pk, ssl_config->key,
|
---|
471 | ssl_config->key_passwd);
|
---|
472 | #endif
|
---|
473 |
|
---|
474 | if(ret) {
|
---|
475 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
476 | failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s",
|
---|
477 | ssl_config->key, -ret, errorbuf);
|
---|
478 | return CURLE_SSL_CERTPROBLEM;
|
---|
479 | }
|
---|
480 | #else
|
---|
481 | failf(data, "mbedtls: functions that use the filesystem not built in");
|
---|
482 | return CURLE_NOT_BUILT_IN;
|
---|
483 | #endif
|
---|
484 | }
|
---|
485 | else {
|
---|
486 | const struct curl_blob *ssl_key_blob = ssl_config->key_blob;
|
---|
487 | const unsigned char *key_data =
|
---|
488 | (const unsigned char *)ssl_key_blob->data;
|
---|
489 | const char *passwd = ssl_config->key_passwd;
|
---|
490 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
---|
491 | ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len,
|
---|
492 | (const unsigned char *)passwd,
|
---|
493 | passwd ? strlen(passwd) : 0,
|
---|
494 | mbedtls_ctr_drbg_random,
|
---|
495 | &backend->ctr_drbg);
|
---|
496 | #else
|
---|
497 | ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len,
|
---|
498 | (const unsigned char *)passwd,
|
---|
499 | passwd ? strlen(passwd) : 0);
|
---|
500 | #endif
|
---|
501 |
|
---|
502 | if(ret) {
|
---|
503 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
504 | failf(data, "Error parsing private key - mbedTLS: (-0x%04X) %s",
|
---|
505 | -ret, errorbuf);
|
---|
506 | return CURLE_SSL_CERTPROBLEM;
|
---|
507 | }
|
---|
508 | }
|
---|
509 |
|
---|
510 | if(ret == 0 && !(mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_RSA) ||
|
---|
511 | mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_ECKEY)))
|
---|
512 | ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /* Load the CRL */
|
---|
516 | #ifdef MBEDTLS_X509_CRL_PARSE_C
|
---|
517 | mbedtls_x509_crl_init(&backend->crl);
|
---|
518 |
|
---|
519 | if(ssl_crlfile) {
|
---|
520 | #ifdef MBEDTLS_FS_IO
|
---|
521 | ret = mbedtls_x509_crl_parse_file(&backend->crl, ssl_crlfile);
|
---|
522 |
|
---|
523 | if(ret) {
|
---|
524 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
525 | failf(data, "Error reading CRL file %s - mbedTLS: (-0x%04X) %s",
|
---|
526 | ssl_crlfile, -ret, errorbuf);
|
---|
527 |
|
---|
528 | return CURLE_SSL_CRL_BADFILE;
|
---|
529 | }
|
---|
530 | #else
|
---|
531 | failf(data, "mbedtls: functions that use the filesystem not built in");
|
---|
532 | return CURLE_NOT_BUILT_IN;
|
---|
533 | #endif
|
---|
534 | }
|
---|
535 | #else
|
---|
536 | if(ssl_crlfile) {
|
---|
537 | failf(data, "mbedtls: crl support not built in");
|
---|
538 | return CURLE_NOT_BUILT_IN;
|
---|
539 | }
|
---|
540 | #endif
|
---|
541 |
|
---|
542 | infof(data, "mbedTLS: Connecting to %s:%d", hostname, connssl->port);
|
---|
543 |
|
---|
544 | mbedtls_ssl_config_init(&backend->config);
|
---|
545 | ret = mbedtls_ssl_config_defaults(&backend->config,
|
---|
546 | MBEDTLS_SSL_IS_CLIENT,
|
---|
547 | MBEDTLS_SSL_TRANSPORT_STREAM,
|
---|
548 | MBEDTLS_SSL_PRESET_DEFAULT);
|
---|
549 | if(ret) {
|
---|
550 | failf(data, "mbedTLS: ssl_config failed");
|
---|
551 | return CURLE_SSL_CONNECT_ERROR;
|
---|
552 | }
|
---|
553 |
|
---|
554 | mbedtls_ssl_init(&backend->ssl);
|
---|
555 | if(mbedtls_ssl_setup(&backend->ssl, &backend->config)) {
|
---|
556 | failf(data, "mbedTLS: ssl_init failed");
|
---|
557 | return CURLE_SSL_CONNECT_ERROR;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /* new profile with RSA min key len = 1024 ... */
|
---|
561 | mbedtls_ssl_conf_cert_profile(&backend->config,
|
---|
562 | &mbedtls_x509_crt_profile_fr);
|
---|
563 |
|
---|
564 | switch(conn_config->version) {
|
---|
565 | case CURL_SSLVERSION_DEFAULT:
|
---|
566 | case CURL_SSLVERSION_TLSv1:
|
---|
567 | #if MBEDTLS_VERSION_NUMBER < 0x03000000
|
---|
568 | mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
---|
569 | MBEDTLS_SSL_MINOR_VERSION_1);
|
---|
570 | infof(data, "mbedTLS: Set min SSL version to TLS 1.0");
|
---|
571 | break;
|
---|
572 | #endif
|
---|
573 | case CURL_SSLVERSION_TLSv1_0:
|
---|
574 | case CURL_SSLVERSION_TLSv1_1:
|
---|
575 | case CURL_SSLVERSION_TLSv1_2:
|
---|
576 | case CURL_SSLVERSION_TLSv1_3:
|
---|
577 | {
|
---|
578 | CURLcode result = set_ssl_version_min_max(cf, data);
|
---|
579 | if(result != CURLE_OK)
|
---|
580 | return result;
|
---|
581 | break;
|
---|
582 | }
|
---|
583 | default:
|
---|
584 | failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
---|
585 | return CURLE_SSL_CONNECT_ERROR;
|
---|
586 | }
|
---|
587 |
|
---|
588 | mbedtls_ssl_conf_authmode(&backend->config, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
---|
589 |
|
---|
590 | mbedtls_ssl_conf_rng(&backend->config, mbedtls_ctr_drbg_random,
|
---|
591 | &backend->ctr_drbg);
|
---|
592 | mbedtls_ssl_set_bio(&backend->ssl, cf, bio_cf_write, bio_cf_read,
|
---|
593 | NULL /* rev_timeout() */);
|
---|
594 |
|
---|
595 | mbedtls_ssl_conf_ciphersuites(&backend->config,
|
---|
596 | mbedtls_ssl_list_ciphersuites());
|
---|
597 |
|
---|
598 | #if defined(MBEDTLS_SSL_RENEGOTIATION)
|
---|
599 | mbedtls_ssl_conf_renegotiation(&backend->config,
|
---|
600 | MBEDTLS_SSL_RENEGOTIATION_ENABLED);
|
---|
601 | #endif
|
---|
602 |
|
---|
603 | #if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
---|
604 | mbedtls_ssl_conf_session_tickets(&backend->config,
|
---|
605 | MBEDTLS_SSL_SESSION_TICKETS_DISABLED);
|
---|
606 | #endif
|
---|
607 |
|
---|
608 | /* Check if there's a cached ID we can/should use here! */
|
---|
609 | if(ssl_config->primary.sessionid) {
|
---|
610 | void *old_session = NULL;
|
---|
611 |
|
---|
612 | Curl_ssl_sessionid_lock(data);
|
---|
613 | if(!Curl_ssl_getsessionid(cf, data, &old_session, NULL)) {
|
---|
614 | ret = mbedtls_ssl_set_session(&backend->ssl, old_session);
|
---|
615 | if(ret) {
|
---|
616 | Curl_ssl_sessionid_unlock(data);
|
---|
617 | failf(data, "mbedtls_ssl_set_session returned -0x%x", -ret);
|
---|
618 | return CURLE_SSL_CONNECT_ERROR;
|
---|
619 | }
|
---|
620 | infof(data, "mbedTLS re-using session");
|
---|
621 | }
|
---|
622 | Curl_ssl_sessionid_unlock(data);
|
---|
623 | }
|
---|
624 |
|
---|
625 | mbedtls_ssl_conf_ca_chain(&backend->config,
|
---|
626 | &backend->cacert,
|
---|
627 | #ifdef MBEDTLS_X509_CRL_PARSE_C
|
---|
628 | &backend->crl);
|
---|
629 | #else
|
---|
630 | NULL);
|
---|
631 | #endif
|
---|
632 |
|
---|
633 | if(ssl_config->key || ssl_config->key_blob) {
|
---|
634 | mbedtls_ssl_conf_own_cert(&backend->config,
|
---|
635 | &backend->clicert, &backend->pk);
|
---|
636 | }
|
---|
637 | {
|
---|
638 | char *snihost = Curl_ssl_snihost(data, hostname, NULL);
|
---|
639 | if(!snihost || mbedtls_ssl_set_hostname(&backend->ssl, snihost)) {
|
---|
640 | /* mbedtls_ssl_set_hostname() sets the name to use in CN/SAN checks and
|
---|
641 | the name to set in the SNI extension. So even if curl connects to a
|
---|
642 | host specified as an IP address, this function must be used. */
|
---|
643 | failf(data, "Failed to set SNI");
|
---|
644 | return CURLE_SSL_CONNECT_ERROR;
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 | #ifdef HAS_ALPN
|
---|
649 | if(connssl->alpn) {
|
---|
650 | struct alpn_proto_buf proto;
|
---|
651 | size_t i;
|
---|
652 |
|
---|
653 | for(i = 0; i < connssl->alpn->count; ++i) {
|
---|
654 | backend->protocols[i] = connssl->alpn->entries[i];
|
---|
655 | }
|
---|
656 | /* this function doesn't clone the protocols array, which is why we need
|
---|
657 | to keep it around */
|
---|
658 | if(mbedtls_ssl_conf_alpn_protocols(&backend->config,
|
---|
659 | &backend->protocols[0])) {
|
---|
660 | failf(data, "Failed setting ALPN protocols");
|
---|
661 | return CURLE_SSL_CONNECT_ERROR;
|
---|
662 | }
|
---|
663 | Curl_alpn_to_proto_str(&proto, connssl->alpn);
|
---|
664 | infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
|
---|
665 | }
|
---|
666 | #endif
|
---|
667 |
|
---|
668 | #ifdef MBEDTLS_DEBUG
|
---|
669 | /* In order to make that work in mbedtls MBEDTLS_DEBUG_C must be defined. */
|
---|
670 | mbedtls_ssl_conf_dbg(&backend->config, mbed_debug, data);
|
---|
671 | /* - 0 No debug
|
---|
672 | * - 1 Error
|
---|
673 | * - 2 State change
|
---|
674 | * - 3 Informational
|
---|
675 | * - 4 Verbose
|
---|
676 | */
|
---|
677 | mbedtls_debug_set_threshold(4);
|
---|
678 | #endif
|
---|
679 |
|
---|
680 | /* give application a chance to interfere with mbedTLS set up. */
|
---|
681 | if(data->set.ssl.fsslctx) {
|
---|
682 | ret = (*data->set.ssl.fsslctx)(data, &backend->config,
|
---|
683 | data->set.ssl.fsslctxp);
|
---|
684 | if(ret) {
|
---|
685 | failf(data, "error signaled by ssl ctx callback");
|
---|
686 | return ret;
|
---|
687 | }
|
---|
688 | }
|
---|
689 |
|
---|
690 | connssl->connecting_state = ssl_connect_2;
|
---|
691 |
|
---|
692 | return CURLE_OK;
|
---|
693 | }
|
---|
694 |
|
---|
695 | static CURLcode
|
---|
696 | mbed_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
697 | {
|
---|
698 | int ret;
|
---|
699 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
700 | struct ssl_backend_data *backend = connssl->backend;
|
---|
701 | struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
|
---|
702 | const mbedtls_x509_crt *peercert;
|
---|
703 | const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf)?
|
---|
704 | data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
|
---|
705 | data->set.str[STRING_SSL_PINNEDPUBLICKEY];
|
---|
706 |
|
---|
707 | DEBUGASSERT(backend);
|
---|
708 |
|
---|
709 | ret = mbedtls_ssl_handshake(&backend->ssl);
|
---|
710 |
|
---|
711 | if(ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
---|
712 | connssl->connecting_state = ssl_connect_2_reading;
|
---|
713 | return CURLE_OK;
|
---|
714 | }
|
---|
715 | else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
---|
716 | connssl->connecting_state = ssl_connect_2_writing;
|
---|
717 | return CURLE_OK;
|
---|
718 | }
|
---|
719 | else if(ret) {
|
---|
720 | char errorbuf[128];
|
---|
721 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
722 | failf(data, "ssl_handshake returned - mbedTLS: (-0x%04X) %s",
|
---|
723 | -ret, errorbuf);
|
---|
724 | return CURLE_SSL_CONNECT_ERROR;
|
---|
725 | }
|
---|
726 |
|
---|
727 | infof(data, "mbedTLS: Handshake complete, cipher is %s",
|
---|
728 | mbedtls_ssl_get_ciphersuite(&backend->ssl));
|
---|
729 |
|
---|
730 | ret = mbedtls_ssl_get_verify_result(&backend->ssl);
|
---|
731 |
|
---|
732 | if(!conn_config->verifyhost)
|
---|
733 | /* Ignore hostname errors if verifyhost is disabled */
|
---|
734 | ret &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
|
---|
735 |
|
---|
736 | if(ret && conn_config->verifypeer) {
|
---|
737 | if(ret & MBEDTLS_X509_BADCERT_EXPIRED)
|
---|
738 | failf(data, "Cert verify failed: BADCERT_EXPIRED");
|
---|
739 |
|
---|
740 | else if(ret & MBEDTLS_X509_BADCERT_REVOKED)
|
---|
741 | failf(data, "Cert verify failed: BADCERT_REVOKED");
|
---|
742 |
|
---|
743 | else if(ret & MBEDTLS_X509_BADCERT_CN_MISMATCH)
|
---|
744 | failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
|
---|
745 |
|
---|
746 | else if(ret & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
|
---|
747 | failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
|
---|
748 |
|
---|
749 | else if(ret & MBEDTLS_X509_BADCERT_FUTURE)
|
---|
750 | failf(data, "Cert verify failed: BADCERT_FUTURE");
|
---|
751 |
|
---|
752 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
753 | }
|
---|
754 |
|
---|
755 | peercert = mbedtls_ssl_get_peer_cert(&backend->ssl);
|
---|
756 |
|
---|
757 | if(peercert && data->set.verbose) {
|
---|
758 | const size_t bufsize = 16384;
|
---|
759 | char *buffer = malloc(bufsize);
|
---|
760 |
|
---|
761 | if(!buffer)
|
---|
762 | return CURLE_OUT_OF_MEMORY;
|
---|
763 |
|
---|
764 | if(mbedtls_x509_crt_info(buffer, bufsize, "* ", peercert) > 0)
|
---|
765 | infof(data, "Dumping cert info: %s", buffer);
|
---|
766 | else
|
---|
767 | infof(data, "Unable to dump certificate information");
|
---|
768 |
|
---|
769 | free(buffer);
|
---|
770 | }
|
---|
771 |
|
---|
772 | if(pinnedpubkey) {
|
---|
773 | int size;
|
---|
774 | CURLcode result;
|
---|
775 | mbedtls_x509_crt *p = NULL;
|
---|
776 | unsigned char *pubkey = NULL;
|
---|
777 |
|
---|
778 | #if MBEDTLS_VERSION_NUMBER == 0x03000000
|
---|
779 | if(!peercert || !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p) ||
|
---|
780 | !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len)) {
|
---|
781 | #else
|
---|
782 | if(!peercert || !peercert->raw.p || !peercert->raw.len) {
|
---|
783 | #endif
|
---|
784 | failf(data, "Failed due to missing peer certificate");
|
---|
785 | return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
---|
786 | }
|
---|
787 |
|
---|
788 | p = calloc(1, sizeof(*p));
|
---|
789 |
|
---|
790 | if(!p)
|
---|
791 | return CURLE_OUT_OF_MEMORY;
|
---|
792 |
|
---|
793 | pubkey = malloc(PUB_DER_MAX_BYTES);
|
---|
794 |
|
---|
795 | if(!pubkey) {
|
---|
796 | result = CURLE_OUT_OF_MEMORY;
|
---|
797 | goto pinnedpubkey_error;
|
---|
798 | }
|
---|
799 |
|
---|
800 | mbedtls_x509_crt_init(p);
|
---|
801 |
|
---|
802 | /* Make a copy of our const peercert because mbedtls_pk_write_pubkey_der
|
---|
803 | needs a non-const key, for now.
|
---|
804 | https://github.com/ARMmbed/mbedtls/issues/396 */
|
---|
805 | #if MBEDTLS_VERSION_NUMBER == 0x03000000
|
---|
806 | if(mbedtls_x509_crt_parse_der(p,
|
---|
807 | peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p),
|
---|
808 | peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len))) {
|
---|
809 | #else
|
---|
810 | if(mbedtls_x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) {
|
---|
811 | #endif
|
---|
812 | failf(data, "Failed copying peer certificate");
|
---|
813 | result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
---|
814 | goto pinnedpubkey_error;
|
---|
815 | }
|
---|
816 |
|
---|
817 | #if MBEDTLS_VERSION_NUMBER == 0x03000000
|
---|
818 | size = mbedtls_pk_write_pubkey_der(&p->MBEDTLS_PRIVATE(pk), pubkey,
|
---|
819 | PUB_DER_MAX_BYTES);
|
---|
820 | #else
|
---|
821 | size = mbedtls_pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES);
|
---|
822 | #endif
|
---|
823 |
|
---|
824 | if(size <= 0) {
|
---|
825 | failf(data, "Failed copying public key from peer certificate");
|
---|
826 | result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
---|
827 | goto pinnedpubkey_error;
|
---|
828 | }
|
---|
829 |
|
---|
830 | /* mbedtls_pk_write_pubkey_der writes data at the end of the buffer. */
|
---|
831 | result = Curl_pin_peer_pubkey(data,
|
---|
832 | pinnedpubkey,
|
---|
833 | &pubkey[PUB_DER_MAX_BYTES - size], size);
|
---|
834 | pinnedpubkey_error:
|
---|
835 | mbedtls_x509_crt_free(p);
|
---|
836 | free(p);
|
---|
837 | free(pubkey);
|
---|
838 | if(result) {
|
---|
839 | return result;
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 | #ifdef HAS_ALPN
|
---|
844 | if(connssl->alpn) {
|
---|
845 | const char *proto = mbedtls_ssl_get_alpn_protocol(&backend->ssl);
|
---|
846 |
|
---|
847 | Curl_alpn_set_negotiated(cf, data, (const unsigned char *)proto,
|
---|
848 | proto? strlen(proto) : 0);
|
---|
849 | }
|
---|
850 | #endif
|
---|
851 |
|
---|
852 | connssl->connecting_state = ssl_connect_3;
|
---|
853 | infof(data, "SSL connected");
|
---|
854 |
|
---|
855 | return CURLE_OK;
|
---|
856 | }
|
---|
857 |
|
---|
858 | static CURLcode
|
---|
859 | mbed_connect_step3(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
860 | {
|
---|
861 | CURLcode retcode = CURLE_OK;
|
---|
862 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
863 | struct ssl_backend_data *backend = connssl->backend;
|
---|
864 | struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
|
---|
865 |
|
---|
866 | DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
---|
867 | DEBUGASSERT(backend);
|
---|
868 |
|
---|
869 | if(ssl_config->primary.sessionid) {
|
---|
870 | int ret;
|
---|
871 | mbedtls_ssl_session *our_ssl_sessionid;
|
---|
872 | void *old_ssl_sessionid = NULL;
|
---|
873 | bool added = FALSE;
|
---|
874 |
|
---|
875 | our_ssl_sessionid = malloc(sizeof(mbedtls_ssl_session));
|
---|
876 | if(!our_ssl_sessionid)
|
---|
877 | return CURLE_OUT_OF_MEMORY;
|
---|
878 |
|
---|
879 | mbedtls_ssl_session_init(our_ssl_sessionid);
|
---|
880 |
|
---|
881 | ret = mbedtls_ssl_get_session(&backend->ssl, our_ssl_sessionid);
|
---|
882 | if(ret) {
|
---|
883 | if(ret != MBEDTLS_ERR_SSL_ALLOC_FAILED)
|
---|
884 | mbedtls_ssl_session_free(our_ssl_sessionid);
|
---|
885 | free(our_ssl_sessionid);
|
---|
886 | failf(data, "mbedtls_ssl_get_session returned -0x%x", -ret);
|
---|
887 | return CURLE_SSL_CONNECT_ERROR;
|
---|
888 | }
|
---|
889 |
|
---|
890 | /* If there's already a matching session in the cache, delete it */
|
---|
891 | Curl_ssl_sessionid_lock(data);
|
---|
892 | if(!Curl_ssl_getsessionid(cf, data, &old_ssl_sessionid, NULL))
|
---|
893 | Curl_ssl_delsessionid(data, old_ssl_sessionid);
|
---|
894 |
|
---|
895 | retcode = Curl_ssl_addsessionid(cf, data, our_ssl_sessionid,
|
---|
896 | 0, &added);
|
---|
897 | Curl_ssl_sessionid_unlock(data);
|
---|
898 | if(!added) {
|
---|
899 | mbedtls_ssl_session_free(our_ssl_sessionid);
|
---|
900 | free(our_ssl_sessionid);
|
---|
901 | }
|
---|
902 | if(retcode) {
|
---|
903 | failf(data, "failed to store ssl session");
|
---|
904 | return retcode;
|
---|
905 | }
|
---|
906 | }
|
---|
907 |
|
---|
908 | connssl->connecting_state = ssl_connect_done;
|
---|
909 |
|
---|
910 | return CURLE_OK;
|
---|
911 | }
|
---|
912 |
|
---|
913 | static ssize_t mbed_send(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
914 | const void *mem, size_t len,
|
---|
915 | CURLcode *curlcode)
|
---|
916 | {
|
---|
917 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
918 | struct ssl_backend_data *backend = connssl->backend;
|
---|
919 | int ret = -1;
|
---|
920 |
|
---|
921 | (void)data;
|
---|
922 | DEBUGASSERT(backend);
|
---|
923 | ret = mbedtls_ssl_write(&backend->ssl, (unsigned char *)mem, len);
|
---|
924 |
|
---|
925 | if(ret < 0) {
|
---|
926 | *curlcode = (ret == MBEDTLS_ERR_SSL_WANT_WRITE) ?
|
---|
927 | CURLE_AGAIN : CURLE_SEND_ERROR;
|
---|
928 | ret = -1;
|
---|
929 | }
|
---|
930 |
|
---|
931 | return ret;
|
---|
932 | }
|
---|
933 |
|
---|
934 | static void mbedtls_close_all(struct Curl_easy *data)
|
---|
935 | {
|
---|
936 | (void)data;
|
---|
937 | }
|
---|
938 |
|
---|
939 | static void mbedtls_close(struct Curl_cfilter *cf, struct Curl_easy *data)
|
---|
940 | {
|
---|
941 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
942 | struct ssl_backend_data *backend = connssl->backend;
|
---|
943 | char buf[32];
|
---|
944 |
|
---|
945 | (void)data;
|
---|
946 | DEBUGASSERT(backend);
|
---|
947 |
|
---|
948 | /* Maybe the server has already sent a close notify alert.
|
---|
949 | Read it to avoid an RST on the TCP connection. */
|
---|
950 | (void)mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf, sizeof(buf));
|
---|
951 |
|
---|
952 | mbedtls_pk_free(&backend->pk);
|
---|
953 | mbedtls_x509_crt_free(&backend->clicert);
|
---|
954 | mbedtls_x509_crt_free(&backend->cacert);
|
---|
955 | #ifdef MBEDTLS_X509_CRL_PARSE_C
|
---|
956 | mbedtls_x509_crl_free(&backend->crl);
|
---|
957 | #endif
|
---|
958 | mbedtls_ssl_config_free(&backend->config);
|
---|
959 | mbedtls_ssl_free(&backend->ssl);
|
---|
960 | mbedtls_ctr_drbg_free(&backend->ctr_drbg);
|
---|
961 | #ifndef THREADING_SUPPORT
|
---|
962 | mbedtls_entropy_free(&backend->entropy);
|
---|
963 | #endif /* THREADING_SUPPORT */
|
---|
964 | }
|
---|
965 |
|
---|
966 | static ssize_t mbed_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
967 | char *buf, size_t buffersize,
|
---|
968 | CURLcode *curlcode)
|
---|
969 | {
|
---|
970 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
971 | struct ssl_backend_data *backend = connssl->backend;
|
---|
972 | int ret = -1;
|
---|
973 | ssize_t len = -1;
|
---|
974 |
|
---|
975 | (void)data;
|
---|
976 | DEBUGASSERT(backend);
|
---|
977 |
|
---|
978 | ret = mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf,
|
---|
979 | buffersize);
|
---|
980 |
|
---|
981 | if(ret <= 0) {
|
---|
982 | if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
|
---|
983 | return 0;
|
---|
984 |
|
---|
985 | *curlcode = (ret == MBEDTLS_ERR_SSL_WANT_READ) ?
|
---|
986 | CURLE_AGAIN : CURLE_RECV_ERROR;
|
---|
987 | return -1;
|
---|
988 | }
|
---|
989 |
|
---|
990 | len = ret;
|
---|
991 |
|
---|
992 | return len;
|
---|
993 | }
|
---|
994 |
|
---|
995 | static void mbedtls_session_free(void *ptr)
|
---|
996 | {
|
---|
997 | mbedtls_ssl_session_free(ptr);
|
---|
998 | free(ptr);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | static size_t mbedtls_version(char *buffer, size_t size)
|
---|
1002 | {
|
---|
1003 | #ifdef MBEDTLS_VERSION_C
|
---|
1004 | /* if mbedtls_version_get_number() is available it is better */
|
---|
1005 | unsigned int version = mbedtls_version_get_number();
|
---|
1006 | return msnprintf(buffer, size, "mbedTLS/%u.%u.%u", version>>24,
|
---|
1007 | (version>>16)&0xff, (version>>8)&0xff);
|
---|
1008 | #else
|
---|
1009 | return msnprintf(buffer, size, "mbedTLS/%s", MBEDTLS_VERSION_STRING);
|
---|
1010 | #endif
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | static CURLcode mbedtls_random(struct Curl_easy *data,
|
---|
1014 | unsigned char *entropy, size_t length)
|
---|
1015 | {
|
---|
1016 | #if defined(MBEDTLS_CTR_DRBG_C)
|
---|
1017 | int ret = -1;
|
---|
1018 | char errorbuf[128];
|
---|
1019 | mbedtls_entropy_context ctr_entropy;
|
---|
1020 | mbedtls_ctr_drbg_context ctr_drbg;
|
---|
1021 | mbedtls_entropy_init(&ctr_entropy);
|
---|
1022 | mbedtls_ctr_drbg_init(&ctr_drbg);
|
---|
1023 |
|
---|
1024 | ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
|
---|
1025 | &ctr_entropy, NULL, 0);
|
---|
1026 |
|
---|
1027 | if(ret) {
|
---|
1028 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
1029 | failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s",
|
---|
1030 | -ret, errorbuf);
|
---|
1031 | }
|
---|
1032 | else {
|
---|
1033 | ret = mbedtls_ctr_drbg_random(&ctr_drbg, entropy, length);
|
---|
1034 |
|
---|
1035 | if(ret) {
|
---|
1036 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
---|
1037 | failf(data, "mbedtls_ctr_drbg_random returned (-0x%04X) %s",
|
---|
1038 | -ret, errorbuf);
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | mbedtls_ctr_drbg_free(&ctr_drbg);
|
---|
1043 | mbedtls_entropy_free(&ctr_entropy);
|
---|
1044 |
|
---|
1045 | return ret == 0 ? CURLE_OK : CURLE_FAILED_INIT;
|
---|
1046 | #elif defined(MBEDTLS_HAVEGE_C)
|
---|
1047 | mbedtls_havege_state hs;
|
---|
1048 | mbedtls_havege_init(&hs);
|
---|
1049 | mbedtls_havege_random(&hs, entropy, length);
|
---|
1050 | mbedtls_havege_free(&hs);
|
---|
1051 | return CURLE_OK;
|
---|
1052 | #else
|
---|
1053 | return CURLE_NOT_BUILT_IN;
|
---|
1054 | #endif
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | static CURLcode
|
---|
1058 | mbed_connect_common(struct Curl_cfilter *cf, struct Curl_easy *data,
|
---|
1059 | bool nonblocking,
|
---|
1060 | bool *done)
|
---|
1061 | {
|
---|
1062 | CURLcode retcode;
|
---|
1063 | struct ssl_connect_data *connssl = cf->ctx;
|
---|
1064 | curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
|
---|
1065 | timediff_t timeout_ms;
|
---|
1066 | int what;
|
---|
1067 |
|
---|
1068 | /* check if the connection has already been established */
|
---|
1069 | if(ssl_connection_complete == connssl->state) {
|
---|
1070 | *done = TRUE;
|
---|
1071 | return CURLE_OK;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | if(ssl_connect_1 == connssl->connecting_state) {
|
---|
1075 | /* Find out how much more time we're allowed */
|
---|
1076 | timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
---|
1077 |
|
---|
1078 | if(timeout_ms < 0) {
|
---|
1079 | /* no need to continue if time already is up */
|
---|
1080 | failf(data, "SSL connection timeout");
|
---|
1081 | return CURLE_OPERATION_TIMEDOUT;
|
---|
1082 | }
|
---|
1083 | retcode = mbed_connect_step1(cf, data);
|
---|
1084 | if(retcode)
|
---|
1085 | return retcode;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | while(ssl_connect_2 == connssl->connecting_state ||
|
---|
1089 | ssl_connect_2_reading == connssl->connecting_state ||
|
---|
1090 | ssl_connect_2_writing == connssl->connecting_state) {
|
---|
1091 |
|
---|
1092 | /* check allowed time left */
|
---|
1093 | timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
---|
1094 |
|
---|
1095 | if(timeout_ms < 0) {
|
---|
1096 | /* no need to continue if time already is up */
|
---|
1097 | failf(data, "SSL connection timeout");
|
---|
1098 | return CURLE_OPERATION_TIMEDOUT;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /* if ssl is expecting something, check if it's available. */
|
---|
1102 | if(connssl->connecting_state == ssl_connect_2_reading
|
---|
1103 | || connssl->connecting_state == ssl_connect_2_writing) {
|
---|
1104 |
|
---|
1105 | curl_socket_t writefd = ssl_connect_2_writing ==
|
---|
1106 | connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
---|
1107 | curl_socket_t readfd = ssl_connect_2_reading ==
|
---|
1108 | connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
---|
1109 |
|
---|
1110 | what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
|
---|
1111 | nonblocking ? 0 : timeout_ms);
|
---|
1112 | if(what < 0) {
|
---|
1113 | /* fatal error */
|
---|
1114 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
---|
1115 | return CURLE_SSL_CONNECT_ERROR;
|
---|
1116 | }
|
---|
1117 | else if(0 == what) {
|
---|
1118 | if(nonblocking) {
|
---|
1119 | *done = FALSE;
|
---|
1120 | return CURLE_OK;
|
---|
1121 | }
|
---|
1122 | else {
|
---|
1123 | /* timeout */
|
---|
1124 | failf(data, "SSL connection timeout");
|
---|
1125 | return CURLE_OPERATION_TIMEDOUT;
|
---|
1126 | }
|
---|
1127 | }
|
---|
1128 | /* socket is readable or writable */
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | /* Run transaction, and return to the caller if it failed or if
|
---|
1132 | * this connection is part of a multi handle and this loop would
|
---|
1133 | * execute again. This permits the owner of a multi handle to
|
---|
1134 | * abort a connection attempt before step2 has completed while
|
---|
1135 | * ensuring that a client using select() or epoll() will always
|
---|
1136 | * have a valid fdset to wait on.
|
---|
1137 | */
|
---|
1138 | retcode = mbed_connect_step2(cf, data);
|
---|
1139 | if(retcode || (nonblocking &&
|
---|
1140 | (ssl_connect_2 == connssl->connecting_state ||
|
---|
1141 | ssl_connect_2_reading == connssl->connecting_state ||
|
---|
1142 | ssl_connect_2_writing == connssl->connecting_state)))
|
---|
1143 | return retcode;
|
---|
1144 |
|
---|
1145 | } /* repeat step2 until all transactions are done. */
|
---|
1146 |
|
---|
1147 | if(ssl_connect_3 == connssl->connecting_state) {
|
---|
1148 | retcode = mbed_connect_step3(cf, data);
|
---|
1149 | if(retcode)
|
---|
1150 | return retcode;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | if(ssl_connect_done == connssl->connecting_state) {
|
---|
1154 | connssl->state = ssl_connection_complete;
|
---|
1155 | *done = TRUE;
|
---|
1156 | }
|
---|
1157 | else
|
---|
1158 | *done = FALSE;
|
---|
1159 |
|
---|
1160 | /* Reset our connect state machine */
|
---|
1161 | connssl->connecting_state = ssl_connect_1;
|
---|
1162 |
|
---|
1163 | return CURLE_OK;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | static CURLcode mbedtls_connect_nonblocking(struct Curl_cfilter *cf,
|
---|
1167 | struct Curl_easy *data,
|
---|
1168 | bool *done)
|
---|
1169 | {
|
---|
1170 | return mbed_connect_common(cf, data, TRUE, done);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 |
|
---|
1174 | static CURLcode mbedtls_connect(struct Curl_cfilter *cf,
|
---|
1175 | struct Curl_easy *data)
|
---|
1176 | {
|
---|
1177 | CURLcode retcode;
|
---|
1178 | bool done = FALSE;
|
---|
1179 |
|
---|
1180 | retcode = mbed_connect_common(cf, data, FALSE, &done);
|
---|
1181 | if(retcode)
|
---|
1182 | return retcode;
|
---|
1183 |
|
---|
1184 | DEBUGASSERT(done);
|
---|
1185 |
|
---|
1186 | return CURLE_OK;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | /*
|
---|
1190 | * return 0 error initializing SSL
|
---|
1191 | * return 1 SSL initialized successfully
|
---|
1192 | */
|
---|
1193 | static int mbedtls_init(void)
|
---|
1194 | {
|
---|
1195 | return Curl_mbedtlsthreadlock_thread_setup();
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | static void mbedtls_cleanup(void)
|
---|
1199 | {
|
---|
1200 | (void)Curl_mbedtlsthreadlock_thread_cleanup();
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | static bool mbedtls_data_pending(struct Curl_cfilter *cf,
|
---|
1204 | const struct Curl_easy *data)
|
---|
1205 | {
|
---|
1206 | struct ssl_connect_data *ctx = cf->ctx;
|
---|
1207 |
|
---|
1208 | (void)data;
|
---|
1209 | DEBUGASSERT(ctx && ctx->backend);
|
---|
1210 | return mbedtls_ssl_get_bytes_avail(&ctx->backend->ssl) != 0;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | static CURLcode mbedtls_sha256sum(const unsigned char *input,
|
---|
1214 | size_t inputlen,
|
---|
1215 | unsigned char *sha256sum,
|
---|
1216 | size_t sha256len UNUSED_PARAM)
|
---|
1217 | {
|
---|
1218 | /* TODO: explain this for different mbedtls 2.x vs 3 version */
|
---|
1219 | (void)sha256len;
|
---|
1220 | #if MBEDTLS_VERSION_NUMBER < 0x02070000
|
---|
1221 | mbedtls_sha256(input, inputlen, sha256sum, 0);
|
---|
1222 | #else
|
---|
1223 | /* returns 0 on success, otherwise failure */
|
---|
1224 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
---|
1225 | if(mbedtls_sha256(input, inputlen, sha256sum, 0) != 0)
|
---|
1226 | #else
|
---|
1227 | if(mbedtls_sha256_ret(input, inputlen, sha256sum, 0) != 0)
|
---|
1228 | #endif
|
---|
1229 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
1230 | #endif
|
---|
1231 | return CURLE_OK;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | static void *mbedtls_get_internals(struct ssl_connect_data *connssl,
|
---|
1235 | CURLINFO info UNUSED_PARAM)
|
---|
1236 | {
|
---|
1237 | struct ssl_backend_data *backend = connssl->backend;
|
---|
1238 | (void)info;
|
---|
1239 | DEBUGASSERT(backend);
|
---|
1240 | return &backend->ssl;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | const struct Curl_ssl Curl_ssl_mbedtls = {
|
---|
1244 | { CURLSSLBACKEND_MBEDTLS, "mbedtls" }, /* info */
|
---|
1245 |
|
---|
1246 | SSLSUPP_CA_PATH |
|
---|
1247 | SSLSUPP_CAINFO_BLOB |
|
---|
1248 | SSLSUPP_PINNEDPUBKEY |
|
---|
1249 | SSLSUPP_SSL_CTX |
|
---|
1250 | SSLSUPP_HTTPS_PROXY,
|
---|
1251 |
|
---|
1252 | sizeof(struct ssl_backend_data),
|
---|
1253 |
|
---|
1254 | mbedtls_init, /* init */
|
---|
1255 | mbedtls_cleanup, /* cleanup */
|
---|
1256 | mbedtls_version, /* version */
|
---|
1257 | Curl_none_check_cxn, /* check_cxn */
|
---|
1258 | Curl_none_shutdown, /* shutdown */
|
---|
1259 | mbedtls_data_pending, /* data_pending */
|
---|
1260 | mbedtls_random, /* random */
|
---|
1261 | Curl_none_cert_status_request, /* cert_status_request */
|
---|
1262 | mbedtls_connect, /* connect */
|
---|
1263 | mbedtls_connect_nonblocking, /* connect_nonblocking */
|
---|
1264 | Curl_ssl_get_select_socks, /* getsock */
|
---|
1265 | mbedtls_get_internals, /* get_internals */
|
---|
1266 | mbedtls_close, /* close_one */
|
---|
1267 | mbedtls_close_all, /* close_all */
|
---|
1268 | mbedtls_session_free, /* session_free */
|
---|
1269 | Curl_none_set_engine, /* set_engine */
|
---|
1270 | Curl_none_set_engine_default, /* set_engine_default */
|
---|
1271 | Curl_none_engines_list, /* engines_list */
|
---|
1272 | Curl_none_false_start, /* false_start */
|
---|
1273 | mbedtls_sha256sum, /* sha256sum */
|
---|
1274 | NULL, /* associate_connection */
|
---|
1275 | NULL, /* disassociate_connection */
|
---|
1276 | NULL, /* free_multi_ssl_backend_data */
|
---|
1277 | mbed_recv, /* recv decrypted data */
|
---|
1278 | mbed_send, /* send data to encrypt */
|
---|
1279 | };
|
---|
1280 |
|
---|
1281 | #endif /* USE_MBEDTLS */
|
---|