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