/src/PROJ/curl/lib/vtls/vtls.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | /* This file is for implementing all "generic" SSL functions that all libcurl |
26 | | internals should use. It is then responsible for calling the proper |
27 | | "backend" function. |
28 | | |
29 | | SSL-functions in libcurl should call functions in this source file, and not |
30 | | to any specific SSL-layer. |
31 | | |
32 | | Curl_ssl_ - prefix for generic ones |
33 | | |
34 | | Note that this source code uses the functions of the configured SSL |
35 | | backend via the global Curl_ssl instance. |
36 | | |
37 | | "SSL/TLS Strong Encryption: An Introduction" |
38 | | https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html |
39 | | */ |
40 | | |
41 | | #include "../curl_setup.h" |
42 | | |
43 | | #ifdef HAVE_SYS_TYPES_H |
44 | | #include <sys/types.h> |
45 | | #endif |
46 | | #ifdef HAVE_SYS_STAT_H |
47 | | #include <sys/stat.h> |
48 | | #endif |
49 | | #ifdef HAVE_FCNTL_H |
50 | | #include <fcntl.h> |
51 | | #endif |
52 | | |
53 | | #include "../urldata.h" |
54 | | #include "../cfilters.h" |
55 | | |
56 | | #include "vtls.h" /* generic SSL protos etc */ |
57 | | #include "vtls_int.h" |
58 | | #include "vtls_scache.h" |
59 | | |
60 | | #include "openssl.h" /* OpenSSL versions */ |
61 | | #include "gtls.h" /* GnuTLS versions */ |
62 | | #include "wolfssl.h" /* wolfSSL versions */ |
63 | | #include "schannel.h" /* Schannel SSPI version */ |
64 | | #include "mbedtls.h" /* mbedTLS versions */ |
65 | | #include "rustls.h" /* Rustls versions */ |
66 | | |
67 | | #include "../slist.h" |
68 | | #include "../sendf.h" |
69 | | #include "../strcase.h" |
70 | | #include "../url.h" |
71 | | #include "../progress.h" |
72 | | #include "../share.h" |
73 | | #include "../multiif.h" |
74 | | #include "../curlx/timeval.h" |
75 | | #include "../curl_md5.h" |
76 | | #include "../curl_sha256.h" |
77 | | #include "../curlx/warnless.h" |
78 | | #include "../curlx/base64.h" |
79 | | #include "../curl_printf.h" |
80 | | #include "../curlx/inet_pton.h" |
81 | | #include "../connect.h" |
82 | | #include "../select.h" |
83 | | #include "../strdup.h" |
84 | | #include "../rand.h" |
85 | | |
86 | | /* The last #include files should be: */ |
87 | | #include "../curl_memory.h" |
88 | | #include "../memdebug.h" |
89 | | |
90 | | |
91 | | #define CLONE_STRING(var) \ |
92 | 0 | do { \ |
93 | 0 | if(source->var) { \ |
94 | 0 | dest->var = strdup(source->var); \ |
95 | 0 | if(!dest->var) \ |
96 | 0 | return FALSE; \ |
97 | 0 | } \ |
98 | 0 | else \ |
99 | 0 | dest->var = NULL; \ |
100 | 0 | } while(0) |
101 | | |
102 | | #define CLONE_BLOB(var) \ |
103 | 0 | do { \ |
104 | 0 | if(blobdup(&dest->var, source->var)) \ |
105 | 0 | return FALSE; \ |
106 | 0 | } while(0) |
107 | | |
108 | | static CURLcode blobdup(struct curl_blob **dest, |
109 | | struct curl_blob *src) |
110 | 0 | { |
111 | 0 | DEBUGASSERT(dest); |
112 | 0 | DEBUGASSERT(!*dest); |
113 | 0 | if(src) { |
114 | | /* only if there is data to dupe! */ |
115 | 0 | struct curl_blob *d; |
116 | 0 | d = malloc(sizeof(struct curl_blob) + src->len); |
117 | 0 | if(!d) |
118 | 0 | return CURLE_OUT_OF_MEMORY; |
119 | 0 | d->len = src->len; |
120 | | /* Always duplicate because the connection may survive longer than the |
121 | | handle that passed in the blob. */ |
122 | 0 | d->flags = CURL_BLOB_COPY; |
123 | 0 | d->data = (void *)((char *)d + sizeof(struct curl_blob)); |
124 | 0 | memcpy(d->data, src->data, src->len); |
125 | 0 | *dest = d; |
126 | 0 | } |
127 | 0 | return CURLE_OK; |
128 | 0 | } |
129 | | |
130 | | /* returns TRUE if the blobs are identical */ |
131 | | static bool blobcmp(struct curl_blob *first, struct curl_blob *second) |
132 | 0 | { |
133 | 0 | if(!first && !second) /* both are NULL */ |
134 | 0 | return TRUE; |
135 | 0 | if(!first || !second) /* one is NULL */ |
136 | 0 | return FALSE; |
137 | 0 | if(first->len != second->len) /* different sizes */ |
138 | 0 | return FALSE; |
139 | 0 | return !memcmp(first->data, second->data, first->len); /* same data */ |
140 | 0 | } |
141 | | |
142 | | #ifdef USE_SSL |
143 | | static const struct alpn_spec ALPN_SPEC_H11 = { |
144 | | { ALPN_HTTP_1_1 }, 1 |
145 | | }; |
146 | | #ifdef USE_HTTP2 |
147 | | static const struct alpn_spec ALPN_SPEC_H2 = { |
148 | | { ALPN_H2 }, 1 |
149 | | }; |
150 | | static const struct alpn_spec ALPN_SPEC_H2_H11 = { |
151 | | { ALPN_H2, ALPN_HTTP_1_1 }, 2 |
152 | | }; |
153 | | #endif |
154 | | |
155 | | static const struct alpn_spec * |
156 | | alpn_get_spec(http_majors allowed, bool use_alpn) |
157 | 0 | { |
158 | 0 | if(!use_alpn) |
159 | 0 | return NULL; |
160 | | #ifdef USE_HTTP2 |
161 | | if(allowed & CURL_HTTP_V2x) { |
162 | | if(allowed & CURL_HTTP_V1x) |
163 | | return &ALPN_SPEC_H2_H11; |
164 | | return &ALPN_SPEC_H2; |
165 | | } |
166 | | #else |
167 | 0 | (void)allowed; |
168 | 0 | #endif |
169 | | /* Use the ALPN protocol "http/1.1" for HTTP/1.x. |
170 | | Avoid "http/1.0" because some servers do not support it. */ |
171 | 0 | return &ALPN_SPEC_H11; |
172 | 0 | } |
173 | | #endif /* USE_SSL */ |
174 | | |
175 | | |
176 | | void Curl_ssl_easy_config_init(struct Curl_easy *data) |
177 | 0 | { |
178 | | /* |
179 | | * libcurl 7.10 introduced SSL verification *by default*! This needs to be |
180 | | * switched off unless wanted. |
181 | | */ |
182 | 0 | data->set.ssl.primary.verifypeer = TRUE; |
183 | 0 | data->set.ssl.primary.verifyhost = TRUE; |
184 | 0 | data->set.ssl.primary.cache_session = TRUE; /* caching by default */ |
185 | 0 | #ifndef CURL_DISABLE_PROXY |
186 | 0 | data->set.proxy_ssl = data->set.ssl; |
187 | 0 | #endif |
188 | 0 | } |
189 | | |
190 | | static bool |
191 | | match_ssl_primary_config(struct Curl_easy *data, |
192 | | struct ssl_primary_config *c1, |
193 | | struct ssl_primary_config *c2) |
194 | 0 | { |
195 | 0 | (void)data; |
196 | 0 | if((c1->version == c2->version) && |
197 | 0 | (c1->version_max == c2->version_max) && |
198 | 0 | (c1->ssl_options == c2->ssl_options) && |
199 | 0 | (c1->verifypeer == c2->verifypeer) && |
200 | 0 | (c1->verifyhost == c2->verifyhost) && |
201 | 0 | (c1->verifystatus == c2->verifystatus) && |
202 | 0 | blobcmp(c1->cert_blob, c2->cert_blob) && |
203 | 0 | blobcmp(c1->ca_info_blob, c2->ca_info_blob) && |
204 | 0 | blobcmp(c1->issuercert_blob, c2->issuercert_blob) && |
205 | 0 | Curl_safecmp(c1->CApath, c2->CApath) && |
206 | 0 | Curl_safecmp(c1->CAfile, c2->CAfile) && |
207 | 0 | Curl_safecmp(c1->issuercert, c2->issuercert) && |
208 | 0 | Curl_safecmp(c1->clientcert, c2->clientcert) && |
209 | 0 | #ifdef USE_TLS_SRP |
210 | 0 | !Curl_timestrcmp(c1->username, c2->username) && |
211 | 0 | !Curl_timestrcmp(c1->password, c2->password) && |
212 | 0 | #endif |
213 | 0 | strcasecompare(c1->cipher_list, c2->cipher_list) && |
214 | 0 | strcasecompare(c1->cipher_list13, c2->cipher_list13) && |
215 | 0 | strcasecompare(c1->curves, c2->curves) && |
216 | 0 | strcasecompare(c1->signature_algorithms, c2->signature_algorithms) && |
217 | 0 | strcasecompare(c1->CRLfile, c2->CRLfile) && |
218 | 0 | strcasecompare(c1->pinned_key, c2->pinned_key)) |
219 | 0 | return TRUE; |
220 | | |
221 | 0 | return FALSE; |
222 | 0 | } |
223 | | |
224 | | bool Curl_ssl_conn_config_match(struct Curl_easy *data, |
225 | | struct connectdata *candidate, |
226 | | bool proxy) |
227 | 0 | { |
228 | 0 | #ifndef CURL_DISABLE_PROXY |
229 | 0 | if(proxy) |
230 | 0 | return match_ssl_primary_config(data, &data->set.proxy_ssl.primary, |
231 | 0 | &candidate->proxy_ssl_config); |
232 | | #else |
233 | | (void)proxy; |
234 | | #endif |
235 | 0 | return match_ssl_primary_config(data, &data->set.ssl.primary, |
236 | 0 | &candidate->ssl_config); |
237 | 0 | } |
238 | | |
239 | | static bool clone_ssl_primary_config(struct ssl_primary_config *source, |
240 | | struct ssl_primary_config *dest) |
241 | 0 | { |
242 | 0 | dest->version = source->version; |
243 | 0 | dest->version_max = source->version_max; |
244 | 0 | dest->verifypeer = source->verifypeer; |
245 | 0 | dest->verifyhost = source->verifyhost; |
246 | 0 | dest->verifystatus = source->verifystatus; |
247 | 0 | dest->cache_session = source->cache_session; |
248 | 0 | dest->ssl_options = source->ssl_options; |
249 | |
|
250 | 0 | CLONE_BLOB(cert_blob); |
251 | 0 | CLONE_BLOB(ca_info_blob); |
252 | 0 | CLONE_BLOB(issuercert_blob); |
253 | 0 | CLONE_STRING(CApath); |
254 | 0 | CLONE_STRING(CAfile); |
255 | 0 | CLONE_STRING(issuercert); |
256 | 0 | CLONE_STRING(clientcert); |
257 | 0 | CLONE_STRING(cipher_list); |
258 | 0 | CLONE_STRING(cipher_list13); |
259 | 0 | CLONE_STRING(pinned_key); |
260 | 0 | CLONE_STRING(curves); |
261 | 0 | CLONE_STRING(signature_algorithms); |
262 | 0 | CLONE_STRING(CRLfile); |
263 | 0 | #ifdef USE_TLS_SRP |
264 | 0 | CLONE_STRING(username); |
265 | 0 | CLONE_STRING(password); |
266 | 0 | #endif |
267 | | |
268 | 0 | return TRUE; |
269 | 0 | } |
270 | | |
271 | | static void free_primary_ssl_config(struct ssl_primary_config *sslc) |
272 | 0 | { |
273 | 0 | Curl_safefree(sslc->CApath); |
274 | 0 | Curl_safefree(sslc->CAfile); |
275 | 0 | Curl_safefree(sslc->issuercert); |
276 | 0 | Curl_safefree(sslc->clientcert); |
277 | 0 | Curl_safefree(sslc->cipher_list); |
278 | 0 | Curl_safefree(sslc->cipher_list13); |
279 | 0 | Curl_safefree(sslc->pinned_key); |
280 | 0 | Curl_safefree(sslc->cert_blob); |
281 | 0 | Curl_safefree(sslc->ca_info_blob); |
282 | 0 | Curl_safefree(sslc->issuercert_blob); |
283 | 0 | Curl_safefree(sslc->curves); |
284 | 0 | Curl_safefree(sslc->signature_algorithms); |
285 | 0 | Curl_safefree(sslc->CRLfile); |
286 | 0 | #ifdef USE_TLS_SRP |
287 | 0 | Curl_safefree(sslc->username); |
288 | 0 | Curl_safefree(sslc->password); |
289 | 0 | #endif |
290 | 0 | } |
291 | | |
292 | | CURLcode Curl_ssl_easy_config_complete(struct Curl_easy *data) |
293 | 0 | { |
294 | 0 | data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH]; |
295 | 0 | data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE]; |
296 | 0 | data->set.ssl.primary.CRLfile = data->set.str[STRING_SSL_CRLFILE]; |
297 | 0 | data->set.ssl.primary.issuercert = data->set.str[STRING_SSL_ISSUERCERT]; |
298 | 0 | data->set.ssl.primary.issuercert_blob = data->set.blobs[BLOB_SSL_ISSUERCERT]; |
299 | 0 | data->set.ssl.primary.cipher_list = |
300 | 0 | data->set.str[STRING_SSL_CIPHER_LIST]; |
301 | 0 | data->set.ssl.primary.cipher_list13 = |
302 | 0 | data->set.str[STRING_SSL_CIPHER13_LIST]; |
303 | 0 | data->set.ssl.primary.signature_algorithms = |
304 | 0 | data->set.str[STRING_SSL_SIGNATURE_ALGORITHMS]; |
305 | 0 | data->set.ssl.primary.pinned_key = |
306 | 0 | data->set.str[STRING_SSL_PINNEDPUBLICKEY]; |
307 | 0 | data->set.ssl.primary.cert_blob = data->set.blobs[BLOB_CERT]; |
308 | 0 | data->set.ssl.primary.ca_info_blob = data->set.blobs[BLOB_CAINFO]; |
309 | 0 | data->set.ssl.primary.curves = data->set.str[STRING_SSL_EC_CURVES]; |
310 | 0 | #ifdef USE_TLS_SRP |
311 | 0 | data->set.ssl.primary.username = data->set.str[STRING_TLSAUTH_USERNAME]; |
312 | 0 | data->set.ssl.primary.password = data->set.str[STRING_TLSAUTH_PASSWORD]; |
313 | 0 | #endif |
314 | 0 | data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE]; |
315 | 0 | data->set.ssl.key = data->set.str[STRING_KEY]; |
316 | 0 | data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE]; |
317 | 0 | data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD]; |
318 | 0 | data->set.ssl.primary.clientcert = data->set.str[STRING_CERT]; |
319 | 0 | data->set.ssl.key_blob = data->set.blobs[BLOB_KEY]; |
320 | |
|
321 | 0 | #ifndef CURL_DISABLE_PROXY |
322 | 0 | data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY]; |
323 | 0 | data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY]; |
324 | 0 | data->set.proxy_ssl.primary.cipher_list = |
325 | 0 | data->set.str[STRING_SSL_CIPHER_LIST_PROXY]; |
326 | 0 | data->set.proxy_ssl.primary.cipher_list13 = |
327 | 0 | data->set.str[STRING_SSL_CIPHER13_LIST_PROXY]; |
328 | 0 | data->set.proxy_ssl.primary.pinned_key = |
329 | 0 | data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]; |
330 | 0 | data->set.proxy_ssl.primary.cert_blob = data->set.blobs[BLOB_CERT_PROXY]; |
331 | 0 | data->set.proxy_ssl.primary.ca_info_blob = |
332 | 0 | data->set.blobs[BLOB_CAINFO_PROXY]; |
333 | 0 | data->set.proxy_ssl.primary.issuercert = |
334 | 0 | data->set.str[STRING_SSL_ISSUERCERT_PROXY]; |
335 | 0 | data->set.proxy_ssl.primary.issuercert_blob = |
336 | 0 | data->set.blobs[BLOB_SSL_ISSUERCERT_PROXY]; |
337 | 0 | data->set.proxy_ssl.primary.CRLfile = |
338 | 0 | data->set.str[STRING_SSL_CRLFILE_PROXY]; |
339 | 0 | data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY]; |
340 | 0 | data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY]; |
341 | 0 | data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY]; |
342 | 0 | data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY]; |
343 | 0 | data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY]; |
344 | 0 | data->set.proxy_ssl.key_blob = data->set.blobs[BLOB_KEY_PROXY]; |
345 | 0 | #ifdef USE_TLS_SRP |
346 | 0 | data->set.proxy_ssl.primary.username = |
347 | 0 | data->set.str[STRING_TLSAUTH_USERNAME_PROXY]; |
348 | 0 | data->set.proxy_ssl.primary.password = |
349 | 0 | data->set.str[STRING_TLSAUTH_PASSWORD_PROXY]; |
350 | 0 | #endif |
351 | 0 | #endif /* CURL_DISABLE_PROXY */ |
352 | |
|
353 | 0 | return CURLE_OK; |
354 | 0 | } |
355 | | |
356 | | CURLcode Curl_ssl_conn_config_init(struct Curl_easy *data, |
357 | | struct connectdata *conn) |
358 | 0 | { |
359 | | /* Clone "primary" SSL configurations from the esay handle to |
360 | | * the connection. They are used for connection cache matching and |
361 | | * probably outlive the easy handle */ |
362 | 0 | if(!clone_ssl_primary_config(&data->set.ssl.primary, &conn->ssl_config)) |
363 | 0 | return CURLE_OUT_OF_MEMORY; |
364 | 0 | #ifndef CURL_DISABLE_PROXY |
365 | 0 | if(!clone_ssl_primary_config(&data->set.proxy_ssl.primary, |
366 | 0 | &conn->proxy_ssl_config)) |
367 | 0 | return CURLE_OUT_OF_MEMORY; |
368 | 0 | #endif |
369 | 0 | return CURLE_OK; |
370 | 0 | } |
371 | | |
372 | | void Curl_ssl_conn_config_cleanup(struct connectdata *conn) |
373 | 0 | { |
374 | 0 | free_primary_ssl_config(&conn->ssl_config); |
375 | 0 | #ifndef CURL_DISABLE_PROXY |
376 | 0 | free_primary_ssl_config(&conn->proxy_ssl_config); |
377 | 0 | #endif |
378 | 0 | } |
379 | | |
380 | | void Curl_ssl_conn_config_update(struct Curl_easy *data, bool for_proxy) |
381 | 0 | { |
382 | | /* May be called on an easy that has no connection yet */ |
383 | 0 | if(data->conn) { |
384 | 0 | struct ssl_primary_config *src, *dest; |
385 | 0 | #ifndef CURL_DISABLE_PROXY |
386 | 0 | src = for_proxy ? &data->set.proxy_ssl.primary : &data->set.ssl.primary; |
387 | 0 | dest = for_proxy ? &data->conn->proxy_ssl_config : &data->conn->ssl_config; |
388 | | #else |
389 | | (void)for_proxy; |
390 | | src = &data->set.ssl.primary; |
391 | | dest = &data->conn->ssl_config; |
392 | | #endif |
393 | 0 | dest->verifyhost = src->verifyhost; |
394 | 0 | dest->verifypeer = src->verifypeer; |
395 | 0 | dest->verifystatus = src->verifystatus; |
396 | 0 | } |
397 | 0 | } |
398 | | |
399 | | #ifdef USE_SSL |
400 | | static int multissl_setup(const struct Curl_ssl *backend); |
401 | | #endif |
402 | | |
403 | | curl_sslbackend Curl_ssl_backend(void) |
404 | 0 | { |
405 | 0 | #ifdef USE_SSL |
406 | 0 | multissl_setup(NULL); |
407 | 0 | return Curl_ssl->info.id; |
408 | | #else |
409 | | return CURLSSLBACKEND_NONE; |
410 | | #endif |
411 | 0 | } |
412 | | |
413 | | #ifdef USE_SSL |
414 | | |
415 | | /* "global" init done? */ |
416 | | static bool init_ssl = FALSE; |
417 | | |
418 | | /** |
419 | | * Global SSL init |
420 | | * |
421 | | * @retval 0 error initializing SSL |
422 | | * @retval 1 SSL initialized successfully |
423 | | */ |
424 | | int Curl_ssl_init(void) |
425 | 0 | { |
426 | | /* make sure this is only done once */ |
427 | 0 | if(init_ssl) |
428 | 0 | return 1; |
429 | 0 | init_ssl = TRUE; /* never again */ |
430 | |
|
431 | 0 | if(Curl_ssl->init) |
432 | 0 | return Curl_ssl->init(); |
433 | 0 | return 1; |
434 | 0 | } |
435 | | |
436 | | static bool ssl_prefs_check(struct Curl_easy *data) |
437 | 0 | { |
438 | | /* check for CURLOPT_SSLVERSION invalid parameter value */ |
439 | 0 | const unsigned char sslver = data->set.ssl.primary.version; |
440 | 0 | if(sslver >= CURL_SSLVERSION_LAST) { |
441 | 0 | failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION"); |
442 | 0 | return FALSE; |
443 | 0 | } |
444 | | |
445 | 0 | switch(data->set.ssl.primary.version_max) { |
446 | 0 | case CURL_SSLVERSION_MAX_NONE: |
447 | 0 | case CURL_SSLVERSION_MAX_DEFAULT: |
448 | 0 | break; |
449 | | |
450 | 0 | default: |
451 | 0 | if((data->set.ssl.primary.version_max >> 16) < sslver) { |
452 | 0 | failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION"); |
453 | 0 | return FALSE; |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | 0 | return TRUE; |
458 | 0 | } |
459 | | |
460 | | static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data, |
461 | | const struct alpn_spec *alpn) |
462 | 0 | { |
463 | 0 | struct ssl_connect_data *ctx; |
464 | |
|
465 | 0 | (void)data; |
466 | 0 | ctx = calloc(1, sizeof(*ctx)); |
467 | 0 | if(!ctx) |
468 | 0 | return NULL; |
469 | | |
470 | 0 | ctx->ssl_impl = Curl_ssl; |
471 | 0 | ctx->alpn = alpn; |
472 | 0 | Curl_bufq_init2(&ctx->earlydata, CURL_SSL_EARLY_MAX, 1, BUFQ_OPT_NO_SPARES); |
473 | 0 | ctx->backend = calloc(1, ctx->ssl_impl->sizeof_ssl_backend_data); |
474 | 0 | if(!ctx->backend) { |
475 | 0 | free(ctx); |
476 | 0 | return NULL; |
477 | 0 | } |
478 | 0 | return ctx; |
479 | 0 | } |
480 | | |
481 | | static void cf_ctx_free(struct ssl_connect_data *ctx) |
482 | 0 | { |
483 | 0 | if(ctx) { |
484 | 0 | Curl_safefree(ctx->negotiated.alpn); |
485 | 0 | Curl_bufq_free(&ctx->earlydata); |
486 | 0 | free(ctx->backend); |
487 | 0 | free(ctx); |
488 | 0 | } |
489 | 0 | } |
490 | | |
491 | | CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, int sockindex, |
492 | | struct dynbuf *binding) |
493 | 0 | { |
494 | 0 | if(Curl_ssl->get_channel_binding) |
495 | 0 | return Curl_ssl->get_channel_binding(data, sockindex, binding); |
496 | 0 | return CURLE_OK; |
497 | 0 | } |
498 | | |
499 | | void Curl_ssl_close_all(struct Curl_easy *data) |
500 | 0 | { |
501 | 0 | if(Curl_ssl->close_all) |
502 | 0 | Curl_ssl->close_all(data); |
503 | 0 | } |
504 | | |
505 | | void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data, |
506 | | struct easy_pollset *ps) |
507 | 0 | { |
508 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
509 | |
|
510 | 0 | if(connssl->io_need) { |
511 | 0 | curl_socket_t sock = Curl_conn_cf_get_socket(cf->next, data); |
512 | 0 | if(sock != CURL_SOCKET_BAD) { |
513 | 0 | if(connssl->io_need & CURL_SSL_IO_NEED_SEND) { |
514 | 0 | Curl_pollset_set_out_only(data, ps, sock); |
515 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%" FMT_SOCKET_T, |
516 | 0 | sock); |
517 | 0 | } |
518 | 0 | else { |
519 | 0 | Curl_pollset_set_in_only(data, ps, sock); |
520 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%" FMT_SOCKET_T, |
521 | 0 | sock); |
522 | 0 | } |
523 | 0 | } |
524 | 0 | } |
525 | 0 | } |
526 | | |
527 | | /* Selects an SSL crypto engine |
528 | | */ |
529 | | CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine) |
530 | 0 | { |
531 | 0 | if(Curl_ssl->set_engine) |
532 | 0 | return Curl_ssl->set_engine(data, engine); |
533 | 0 | return CURLE_NOT_BUILT_IN; |
534 | 0 | } |
535 | | |
536 | | /* Selects the default SSL crypto engine |
537 | | */ |
538 | | CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data) |
539 | 0 | { |
540 | 0 | if(Curl_ssl->set_engine_default) |
541 | 0 | return Curl_ssl->set_engine_default(data); |
542 | 0 | return CURLE_NOT_BUILT_IN; |
543 | 0 | } |
544 | | |
545 | | /* Return list of OpenSSL crypto engine names. */ |
546 | | struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data) |
547 | 0 | { |
548 | 0 | if(Curl_ssl->engines_list) |
549 | 0 | return Curl_ssl->engines_list(data); |
550 | 0 | return NULL; |
551 | 0 | } |
552 | | |
553 | | static size_t multissl_version(char *buffer, size_t size); |
554 | | |
555 | | void Curl_ssl_version(char *buffer, size_t size) |
556 | 0 | { |
557 | | #ifdef CURL_WITH_MULTI_SSL |
558 | | (void)multissl_version(buffer, size); |
559 | | #else |
560 | 0 | (void)Curl_ssl->version(buffer, size); |
561 | 0 | #endif |
562 | 0 | } |
563 | | |
564 | | void Curl_ssl_free_certinfo(struct Curl_easy *data) |
565 | 0 | { |
566 | 0 | struct curl_certinfo *ci = &data->info.certs; |
567 | |
|
568 | 0 | if(ci->num_of_certs) { |
569 | | /* free all individual lists used */ |
570 | 0 | int i; |
571 | 0 | for(i = 0; i < ci->num_of_certs; i++) { |
572 | 0 | curl_slist_free_all(ci->certinfo[i]); |
573 | 0 | ci->certinfo[i] = NULL; |
574 | 0 | } |
575 | |
|
576 | 0 | free(ci->certinfo); /* free the actual array too */ |
577 | 0 | ci->certinfo = NULL; |
578 | 0 | ci->num_of_certs = 0; |
579 | 0 | } |
580 | 0 | } |
581 | | |
582 | | CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num) |
583 | 0 | { |
584 | 0 | struct curl_certinfo *ci = &data->info.certs; |
585 | 0 | struct curl_slist **table; |
586 | | |
587 | | /* Free any previous certificate information structures */ |
588 | 0 | Curl_ssl_free_certinfo(data); |
589 | | |
590 | | /* Allocate the required certificate information structures */ |
591 | 0 | table = calloc((size_t) num, sizeof(struct curl_slist *)); |
592 | 0 | if(!table) |
593 | 0 | return CURLE_OUT_OF_MEMORY; |
594 | | |
595 | 0 | ci->num_of_certs = num; |
596 | 0 | ci->certinfo = table; |
597 | |
|
598 | 0 | return CURLE_OK; |
599 | 0 | } |
600 | | |
601 | | /* |
602 | | * 'value' is NOT a null-terminated string |
603 | | */ |
604 | | CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, |
605 | | int certnum, |
606 | | const char *label, |
607 | | const char *value, |
608 | | size_t valuelen) |
609 | 0 | { |
610 | 0 | struct curl_certinfo *ci = &data->info.certs; |
611 | 0 | struct curl_slist *nl; |
612 | 0 | CURLcode result = CURLE_OK; |
613 | 0 | struct dynbuf build; |
614 | |
|
615 | 0 | DEBUGASSERT(certnum < ci->num_of_certs); |
616 | |
|
617 | 0 | curlx_dyn_init(&build, CURL_X509_STR_MAX); |
618 | |
|
619 | 0 | if(curlx_dyn_add(&build, label) || |
620 | 0 | curlx_dyn_addn(&build, ":", 1) || |
621 | 0 | curlx_dyn_addn(&build, value, valuelen)) |
622 | 0 | return CURLE_OUT_OF_MEMORY; |
623 | | |
624 | 0 | nl = Curl_slist_append_nodup(ci->certinfo[certnum], |
625 | 0 | curlx_dyn_ptr(&build)); |
626 | 0 | if(!nl) { |
627 | 0 | curlx_dyn_free(&build); |
628 | 0 | curl_slist_free_all(ci->certinfo[certnum]); |
629 | 0 | result = CURLE_OUT_OF_MEMORY; |
630 | 0 | } |
631 | |
|
632 | 0 | ci->certinfo[certnum] = nl; |
633 | 0 | return result; |
634 | 0 | } |
635 | | |
636 | | /* get length bytes of randomness */ |
637 | | CURLcode Curl_ssl_random(struct Curl_easy *data, |
638 | | unsigned char *entropy, |
639 | | size_t length) |
640 | 0 | { |
641 | 0 | DEBUGASSERT(length == sizeof(int)); |
642 | 0 | if(Curl_ssl->random) |
643 | 0 | return Curl_ssl->random(data, entropy, length); |
644 | 0 | else |
645 | 0 | return CURLE_NOT_BUILT_IN; |
646 | 0 | } |
647 | | |
648 | | /* |
649 | | * Public key pem to der conversion |
650 | | */ |
651 | | |
652 | | static CURLcode pubkey_pem_to_der(const char *pem, |
653 | | unsigned char **der, size_t *der_len) |
654 | 0 | { |
655 | 0 | char *begin_pos, *end_pos; |
656 | 0 | size_t pem_count, pem_len; |
657 | 0 | CURLcode result; |
658 | 0 | struct dynbuf pbuf; |
659 | | |
660 | | /* if no pem, exit. */ |
661 | 0 | if(!pem) |
662 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
663 | | |
664 | 0 | curlx_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE); |
665 | |
|
666 | 0 | begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----"); |
667 | 0 | if(!begin_pos) |
668 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
669 | | |
670 | 0 | pem_count = begin_pos - pem; |
671 | | /* Invalid if not at beginning AND not directly following \n */ |
672 | 0 | if(0 != pem_count && '\n' != pem[pem_count - 1]) |
673 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
674 | | |
675 | | /* 26 is length of "-----BEGIN PUBLIC KEY-----" */ |
676 | 0 | pem_count += 26; |
677 | | |
678 | | /* Invalid if not directly following \n */ |
679 | 0 | end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----"); |
680 | 0 | if(!end_pos) |
681 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
682 | | |
683 | 0 | pem_len = end_pos - pem; |
684 | | |
685 | | /* |
686 | | * Here we loop through the pem array one character at a time between the |
687 | | * correct indices, and place each character that is not '\n' or '\r' |
688 | | * into the stripped_pem array, which should represent the raw base64 string |
689 | | */ |
690 | 0 | while(pem_count < pem_len) { |
691 | 0 | if('\n' != pem[pem_count] && '\r' != pem[pem_count]) { |
692 | 0 | result = curlx_dyn_addn(&pbuf, &pem[pem_count], 1); |
693 | 0 | if(result) |
694 | 0 | return result; |
695 | 0 | } |
696 | 0 | ++pem_count; |
697 | 0 | } |
698 | | |
699 | 0 | if(curlx_dyn_len(&pbuf)) { |
700 | 0 | result = curlx_base64_decode(curlx_dyn_ptr(&pbuf), der, der_len); |
701 | 0 | curlx_dyn_free(&pbuf); |
702 | 0 | } |
703 | 0 | else |
704 | 0 | result = CURLE_BAD_CONTENT_ENCODING; |
705 | |
|
706 | 0 | return result; |
707 | 0 | } |
708 | | |
709 | | /* |
710 | | * Generic pinned public key check. |
711 | | */ |
712 | | |
713 | | CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data, |
714 | | const char *pinnedpubkey, |
715 | | const unsigned char *pubkey, size_t pubkeylen) |
716 | 0 | { |
717 | 0 | CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
718 | | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
719 | | (void)data; |
720 | | #endif |
721 | | |
722 | | /* if a path was not specified, do not pin */ |
723 | 0 | if(!pinnedpubkey) |
724 | 0 | return CURLE_OK; |
725 | 0 | if(!pubkey || !pubkeylen) |
726 | 0 | return result; |
727 | | |
728 | | /* only do this if pinnedpubkey starts with "sha256//", length 8 */ |
729 | 0 | if(!strncmp(pinnedpubkey, "sha256//", 8)) { |
730 | 0 | CURLcode encode; |
731 | 0 | size_t encodedlen = 0; |
732 | 0 | char *encoded = NULL, *pinkeycopy, *begin_pos, *end_pos; |
733 | 0 | unsigned char *sha256sumdigest; |
734 | |
|
735 | 0 | if(!Curl_ssl->sha256sum) { |
736 | | /* without sha256 support, this cannot match */ |
737 | 0 | return result; |
738 | 0 | } |
739 | | |
740 | | /* compute sha256sum of public key */ |
741 | 0 | sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH); |
742 | 0 | if(!sha256sumdigest) |
743 | 0 | return CURLE_OUT_OF_MEMORY; |
744 | 0 | encode = Curl_ssl->sha256sum(pubkey, pubkeylen, |
745 | 0 | sha256sumdigest, CURL_SHA256_DIGEST_LENGTH); |
746 | |
|
747 | 0 | if(!encode) |
748 | 0 | encode = curlx_base64_encode((char *)sha256sumdigest, |
749 | 0 | CURL_SHA256_DIGEST_LENGTH, &encoded, |
750 | 0 | &encodedlen); |
751 | 0 | Curl_safefree(sha256sumdigest); |
752 | |
|
753 | 0 | if(encode) |
754 | 0 | return encode; |
755 | | |
756 | 0 | infof(data, " public key hash: sha256//%s", encoded); |
757 | | |
758 | | /* it starts with sha256//, copy so we can modify it */ |
759 | 0 | pinkeycopy = strdup(pinnedpubkey); |
760 | 0 | if(!pinkeycopy) { |
761 | 0 | Curl_safefree(encoded); |
762 | 0 | return CURLE_OUT_OF_MEMORY; |
763 | 0 | } |
764 | | /* point begin_pos to the copy, and start extracting keys */ |
765 | 0 | begin_pos = pinkeycopy; |
766 | 0 | do { |
767 | 0 | end_pos = strstr(begin_pos, ";sha256//"); |
768 | | /* |
769 | | * if there is an end_pos, null-terminate, otherwise it will go to the |
770 | | * end of the original string |
771 | | */ |
772 | 0 | if(end_pos) |
773 | 0 | end_pos[0] = '\0'; |
774 | | |
775 | | /* compare base64 sha256 digests, 8 is the length of "sha256//" */ |
776 | 0 | if(encodedlen == strlen(begin_pos + 8) && |
777 | 0 | !memcmp(encoded, begin_pos + 8, encodedlen)) { |
778 | 0 | result = CURLE_OK; |
779 | 0 | break; |
780 | 0 | } |
781 | | |
782 | | /* |
783 | | * change back the null-terminator we changed earlier, |
784 | | * and look for next begin |
785 | | */ |
786 | 0 | if(end_pos) { |
787 | 0 | end_pos[0] = ';'; |
788 | 0 | begin_pos = strstr(end_pos, "sha256//"); |
789 | 0 | } |
790 | 0 | } while(end_pos && begin_pos); |
791 | 0 | Curl_safefree(encoded); |
792 | 0 | Curl_safefree(pinkeycopy); |
793 | 0 | } |
794 | 0 | else { |
795 | 0 | long filesize; |
796 | 0 | size_t size, pem_len; |
797 | 0 | CURLcode pem_read; |
798 | 0 | struct dynbuf buf; |
799 | 0 | char unsigned *pem_ptr = NULL; |
800 | 0 | size_t left; |
801 | 0 | FILE *fp = fopen(pinnedpubkey, "rb"); |
802 | 0 | if(!fp) |
803 | 0 | return result; |
804 | | |
805 | 0 | curlx_dyn_init(&buf, MAX_PINNED_PUBKEY_SIZE); |
806 | | |
807 | | /* Determine the file's size */ |
808 | 0 | if(fseek(fp, 0, SEEK_END)) |
809 | 0 | goto end; |
810 | 0 | filesize = ftell(fp); |
811 | 0 | if(fseek(fp, 0, SEEK_SET)) |
812 | 0 | goto end; |
813 | 0 | if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE) |
814 | 0 | goto end; |
815 | | |
816 | | /* |
817 | | * if the size of our certificate is bigger than the file |
818 | | * size then it cannot match |
819 | | */ |
820 | 0 | size = curlx_sotouz((curl_off_t) filesize); |
821 | 0 | if(pubkeylen > size) |
822 | 0 | goto end; |
823 | | |
824 | | /* |
825 | | * Read the file into the dynbuf |
826 | | */ |
827 | 0 | left = size; |
828 | 0 | do { |
829 | 0 | char buffer[1024]; |
830 | 0 | size_t want = left > sizeof(buffer) ? sizeof(buffer) : left; |
831 | 0 | if(want != fread(buffer, 1, want, fp)) |
832 | 0 | goto end; |
833 | 0 | if(curlx_dyn_addn(&buf, buffer, want)) |
834 | 0 | goto end; |
835 | 0 | left -= want; |
836 | 0 | } while(left); |
837 | | |
838 | | /* If the sizes are the same, it cannot be base64 encoded, must be der */ |
839 | 0 | if(pubkeylen == size) { |
840 | 0 | if(!memcmp(pubkey, curlx_dyn_ptr(&buf), pubkeylen)) |
841 | 0 | result = CURLE_OK; |
842 | 0 | goto end; |
843 | 0 | } |
844 | | |
845 | | /* |
846 | | * Otherwise we will assume it is PEM and try to decode it after placing |
847 | | * null-terminator |
848 | | */ |
849 | 0 | pem_read = pubkey_pem_to_der(curlx_dyn_ptr(&buf), &pem_ptr, &pem_len); |
850 | | /* if it was not read successfully, exit */ |
851 | 0 | if(pem_read) |
852 | 0 | goto end; |
853 | | |
854 | | /* |
855 | | * if the size of our certificate does not match the size of |
856 | | * the decoded file, they cannot be the same, otherwise compare |
857 | | */ |
858 | 0 | if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen)) |
859 | 0 | result = CURLE_OK; |
860 | 0 | end: |
861 | 0 | curlx_dyn_free(&buf); |
862 | 0 | Curl_safefree(pem_ptr); |
863 | 0 | fclose(fp); |
864 | 0 | } |
865 | | |
866 | 0 | return result; |
867 | 0 | } |
868 | | |
869 | | /* |
870 | | * Check whether the SSL backend supports the status_request extension. |
871 | | */ |
872 | | bool Curl_ssl_cert_status_request(void) |
873 | 0 | { |
874 | 0 | if(Curl_ssl->cert_status_request) |
875 | 0 | return Curl_ssl->cert_status_request(); |
876 | 0 | return FALSE; |
877 | 0 | } |
878 | | |
879 | | static int multissl_init(void) |
880 | 0 | { |
881 | 0 | if(multissl_setup(NULL)) |
882 | 0 | return 1; |
883 | 0 | if(Curl_ssl->init) |
884 | 0 | return Curl_ssl->init(); |
885 | 0 | return 1; |
886 | 0 | } |
887 | | |
888 | | static CURLcode multissl_connect(struct Curl_cfilter *cf, |
889 | | struct Curl_easy *data, bool *done) |
890 | 0 | { |
891 | 0 | if(multissl_setup(NULL)) |
892 | 0 | return CURLE_FAILED_INIT; |
893 | 0 | return Curl_ssl->do_connect(cf, data, done); |
894 | 0 | } |
895 | | |
896 | | static void multissl_adjust_pollset(struct Curl_cfilter *cf, |
897 | | struct Curl_easy *data, |
898 | | struct easy_pollset *ps) |
899 | 0 | { |
900 | 0 | if(multissl_setup(NULL)) |
901 | 0 | return; |
902 | 0 | Curl_ssl->adjust_pollset(cf, data, ps); |
903 | 0 | } |
904 | | |
905 | | static void *multissl_get_internals(struct ssl_connect_data *connssl, |
906 | | CURLINFO info) |
907 | 0 | { |
908 | 0 | if(multissl_setup(NULL)) |
909 | 0 | return NULL; |
910 | 0 | return Curl_ssl->get_internals(connssl, info); |
911 | 0 | } |
912 | | |
913 | | static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
914 | 0 | { |
915 | 0 | if(multissl_setup(NULL)) |
916 | 0 | return; |
917 | 0 | Curl_ssl->close(cf, data); |
918 | 0 | } |
919 | | |
920 | | static ssize_t multissl_recv_plain(struct Curl_cfilter *cf, |
921 | | struct Curl_easy *data, |
922 | | char *buf, size_t len, CURLcode *code) |
923 | 0 | { |
924 | 0 | if(multissl_setup(NULL)) |
925 | 0 | return CURLE_FAILED_INIT; |
926 | 0 | return Curl_ssl->recv_plain(cf, data, buf, len, code); |
927 | 0 | } |
928 | | |
929 | | static ssize_t multissl_send_plain(struct Curl_cfilter *cf, |
930 | | struct Curl_easy *data, |
931 | | const void *mem, size_t len, |
932 | | CURLcode *code) |
933 | 0 | { |
934 | 0 | if(multissl_setup(NULL)) |
935 | 0 | return CURLE_FAILED_INIT; |
936 | 0 | return Curl_ssl->send_plain(cf, data, mem, len, code); |
937 | 0 | } |
938 | | |
939 | | static const struct Curl_ssl Curl_ssl_multi = { |
940 | | { CURLSSLBACKEND_NONE, "multi" }, /* info */ |
941 | | 0, /* supports nothing */ |
942 | | (size_t)-1, /* something insanely large to be on the safe side */ |
943 | | |
944 | | multissl_init, /* init */ |
945 | | NULL, /* cleanup */ |
946 | | multissl_version, /* version */ |
947 | | NULL, /* shutdown */ |
948 | | NULL, /* data_pending */ |
949 | | NULL, /* random */ |
950 | | NULL, /* cert_status_request */ |
951 | | multissl_connect, /* connect */ |
952 | | multissl_adjust_pollset, /* adjust_pollset */ |
953 | | multissl_get_internals, /* get_internals */ |
954 | | multissl_close, /* close_one */ |
955 | | NULL, /* close_all */ |
956 | | NULL, /* set_engine */ |
957 | | NULL, /* set_engine_default */ |
958 | | NULL, /* engines_list */ |
959 | | NULL, /* sha256sum */ |
960 | | multissl_recv_plain, /* recv decrypted data */ |
961 | | multissl_send_plain, /* send data to encrypt */ |
962 | | NULL, /* get_channel_binding */ |
963 | | }; |
964 | | |
965 | | const struct Curl_ssl *Curl_ssl = |
966 | | #if defined(CURL_WITH_MULTI_SSL) |
967 | | &Curl_ssl_multi; |
968 | | #elif defined(USE_WOLFSSL) |
969 | | &Curl_ssl_wolfssl; |
970 | | #elif defined(USE_GNUTLS) |
971 | | &Curl_ssl_gnutls; |
972 | | #elif defined(USE_MBEDTLS) |
973 | | &Curl_ssl_mbedtls; |
974 | | #elif defined(USE_RUSTLS) |
975 | | &Curl_ssl_rustls; |
976 | | #elif defined(USE_OPENSSL) |
977 | | &Curl_ssl_openssl; |
978 | | #elif defined(USE_SCHANNEL) |
979 | | &Curl_ssl_schannel; |
980 | | #else |
981 | | #error "Missing struct Curl_ssl for selected SSL backend" |
982 | | #endif |
983 | | |
984 | | static const struct Curl_ssl *available_backends[] = { |
985 | | #if defined(USE_WOLFSSL) |
986 | | &Curl_ssl_wolfssl, |
987 | | #endif |
988 | | #if defined(USE_GNUTLS) |
989 | | &Curl_ssl_gnutls, |
990 | | #endif |
991 | | #if defined(USE_MBEDTLS) |
992 | | &Curl_ssl_mbedtls, |
993 | | #endif |
994 | | #if defined(USE_OPENSSL) |
995 | | &Curl_ssl_openssl, |
996 | | #endif |
997 | | #if defined(USE_SCHANNEL) |
998 | | &Curl_ssl_schannel, |
999 | | #endif |
1000 | | #if defined(USE_RUSTLS) |
1001 | | &Curl_ssl_rustls, |
1002 | | #endif |
1003 | | NULL |
1004 | | }; |
1005 | | |
1006 | | /* Global cleanup */ |
1007 | | void Curl_ssl_cleanup(void) |
1008 | 0 | { |
1009 | 0 | if(init_ssl) { |
1010 | | /* only cleanup if we did a previous init */ |
1011 | 0 | if(Curl_ssl->cleanup) |
1012 | 0 | Curl_ssl->cleanup(); |
1013 | | #if defined(CURL_WITH_MULTI_SSL) |
1014 | | Curl_ssl = &Curl_ssl_multi; |
1015 | | #endif |
1016 | 0 | init_ssl = FALSE; |
1017 | 0 | } |
1018 | 0 | } |
1019 | | |
1020 | | static size_t multissl_version(char *buffer, size_t size) |
1021 | 0 | { |
1022 | 0 | static const struct Curl_ssl *selected; |
1023 | 0 | static char backends[200]; |
1024 | 0 | static size_t backends_len; |
1025 | 0 | const struct Curl_ssl *current; |
1026 | |
|
1027 | 0 | current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl; |
1028 | |
|
1029 | 0 | if(current != selected) { |
1030 | 0 | char *p = backends; |
1031 | 0 | char *end = backends + sizeof(backends); |
1032 | 0 | int i; |
1033 | |
|
1034 | 0 | selected = current; |
1035 | |
|
1036 | 0 | backends[0] = '\0'; |
1037 | |
|
1038 | 0 | for(i = 0; available_backends[i]; ++i) { |
1039 | 0 | char vb[200]; |
1040 | 0 | bool paren = (selected != available_backends[i]); |
1041 | |
|
1042 | 0 | if(available_backends[i]->version(vb, sizeof(vb))) { |
1043 | 0 | p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""), |
1044 | 0 | (paren ? "(" : ""), vb, (paren ? ")" : "")); |
1045 | 0 | } |
1046 | 0 | } |
1047 | |
|
1048 | 0 | backends_len = p - backends; |
1049 | 0 | } |
1050 | |
|
1051 | 0 | if(size) { |
1052 | 0 | if(backends_len < size) |
1053 | 0 | strcpy(buffer, backends); |
1054 | 0 | else |
1055 | 0 | *buffer = 0; /* did not fit */ |
1056 | 0 | } |
1057 | 0 | return 0; |
1058 | 0 | } |
1059 | | |
1060 | | static int multissl_setup(const struct Curl_ssl *backend) |
1061 | 0 | { |
1062 | 0 | int i; |
1063 | 0 | char *env; |
1064 | |
|
1065 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
1066 | 0 | return 1; |
1067 | | |
1068 | 0 | if(backend) { |
1069 | 0 | Curl_ssl = backend; |
1070 | 0 | return 0; |
1071 | 0 | } |
1072 | | |
1073 | 0 | if(!available_backends[0]) |
1074 | 0 | return 1; |
1075 | | |
1076 | 0 | env = curl_getenv("CURL_SSL_BACKEND"); |
1077 | 0 | if(env) { |
1078 | 0 | for(i = 0; available_backends[i]; i++) { |
1079 | 0 | if(strcasecompare(env, available_backends[i]->info.name)) { |
1080 | 0 | Curl_ssl = available_backends[i]; |
1081 | 0 | free(env); |
1082 | 0 | return 0; |
1083 | 0 | } |
1084 | 0 | } |
1085 | 0 | } |
1086 | | |
1087 | | #ifdef CURL_DEFAULT_SSL_BACKEND |
1088 | | for(i = 0; available_backends[i]; i++) { |
1089 | | if(strcasecompare(CURL_DEFAULT_SSL_BACKEND, |
1090 | | available_backends[i]->info.name)) { |
1091 | | Curl_ssl = available_backends[i]; |
1092 | | free(env); |
1093 | | return 0; |
1094 | | } |
1095 | | } |
1096 | | #endif |
1097 | | |
1098 | | /* Fall back to first available backend */ |
1099 | 0 | Curl_ssl = available_backends[0]; |
1100 | 0 | free(env); |
1101 | 0 | return 0; |
1102 | 0 | } |
1103 | | |
1104 | | /* This function is used to select the SSL backend to use. It is called by |
1105 | | curl_global_sslset (easy.c) which uses the global init lock. */ |
1106 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
1107 | | const curl_ssl_backend ***avail) |
1108 | 0 | { |
1109 | 0 | int i; |
1110 | |
|
1111 | 0 | if(avail) |
1112 | 0 | *avail = (const curl_ssl_backend **)&available_backends; |
1113 | |
|
1114 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
1115 | 0 | return id == Curl_ssl->info.id || |
1116 | 0 | (name && strcasecompare(name, Curl_ssl->info.name)) ? |
1117 | 0 | CURLSSLSET_OK : |
1118 | | #if defined(CURL_WITH_MULTI_SSL) |
1119 | | CURLSSLSET_TOO_LATE; |
1120 | | #else |
1121 | 0 | CURLSSLSET_UNKNOWN_BACKEND; |
1122 | 0 | #endif |
1123 | | |
1124 | 0 | for(i = 0; available_backends[i]; i++) { |
1125 | 0 | if(available_backends[i]->info.id == id || |
1126 | 0 | (name && strcasecompare(available_backends[i]->info.name, name))) { |
1127 | 0 | multissl_setup(available_backends[i]); |
1128 | 0 | return CURLSSLSET_OK; |
1129 | 0 | } |
1130 | 0 | } |
1131 | | |
1132 | 0 | return CURLSSLSET_UNKNOWN_BACKEND; |
1133 | 0 | } |
1134 | | |
1135 | | #else /* USE_SSL */ |
1136 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
1137 | | const curl_ssl_backend ***avail) |
1138 | | { |
1139 | | (void)id; |
1140 | | (void)name; |
1141 | | (void)avail; |
1142 | | return CURLSSLSET_NO_BACKENDS; |
1143 | | } |
1144 | | |
1145 | | #endif /* !USE_SSL */ |
1146 | | |
1147 | | #ifdef USE_SSL |
1148 | | |
1149 | | void Curl_ssl_peer_cleanup(struct ssl_peer *peer) |
1150 | 0 | { |
1151 | 0 | Curl_safefree(peer->sni); |
1152 | 0 | if(peer->dispname != peer->hostname) |
1153 | 0 | free(peer->dispname); |
1154 | 0 | peer->dispname = NULL; |
1155 | 0 | Curl_safefree(peer->hostname); |
1156 | 0 | Curl_safefree(peer->scache_key); |
1157 | 0 | peer->type = CURL_SSL_PEER_DNS; |
1158 | 0 | } |
1159 | | |
1160 | | static void cf_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
1161 | 0 | { |
1162 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1163 | 0 | if(connssl) { |
1164 | 0 | connssl->ssl_impl->close(cf, data); |
1165 | 0 | connssl->state = ssl_connection_none; |
1166 | 0 | Curl_ssl_peer_cleanup(&connssl->peer); |
1167 | 0 | } |
1168 | 0 | cf->connected = FALSE; |
1169 | 0 | } |
1170 | | |
1171 | | static ssl_peer_type get_peer_type(const char *hostname) |
1172 | 0 | { |
1173 | 0 | if(hostname && hostname[0]) { |
1174 | 0 | #ifdef USE_IPV6 |
1175 | 0 | struct in6_addr addr; |
1176 | | #else |
1177 | | struct in_addr addr; |
1178 | | #endif |
1179 | 0 | if(curlx_inet_pton(AF_INET, hostname, &addr)) |
1180 | 0 | return CURL_SSL_PEER_IPV4; |
1181 | 0 | #ifdef USE_IPV6 |
1182 | 0 | else if(curlx_inet_pton(AF_INET6, hostname, &addr)) { |
1183 | 0 | return CURL_SSL_PEER_IPV6; |
1184 | 0 | } |
1185 | 0 | #endif |
1186 | 0 | } |
1187 | 0 | return CURL_SSL_PEER_DNS; |
1188 | 0 | } |
1189 | | |
1190 | | CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, |
1191 | | struct Curl_cfilter *cf, |
1192 | | const char *tls_id, |
1193 | | int transport) |
1194 | 0 | { |
1195 | 0 | const char *ehostname, *edispname; |
1196 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
1197 | | |
1198 | | /* We expect a clean struct, e.g. called only ONCE */ |
1199 | 0 | DEBUGASSERT(peer); |
1200 | 0 | DEBUGASSERT(!peer->hostname); |
1201 | 0 | DEBUGASSERT(!peer->dispname); |
1202 | 0 | DEBUGASSERT(!peer->sni); |
1203 | | /* We need the hostname for SNI negotiation. Once handshaked, this remains |
1204 | | * the SNI hostname for the TLS connection. When the connection is reused, |
1205 | | * the settings in cf->conn might change. We keep a copy of the hostname we |
1206 | | * use for SNI. |
1207 | | */ |
1208 | 0 | peer->transport = transport; |
1209 | 0 | #ifndef CURL_DISABLE_PROXY |
1210 | 0 | if(Curl_ssl_cf_is_proxy(cf)) { |
1211 | 0 | ehostname = cf->conn->http_proxy.host.name; |
1212 | 0 | edispname = cf->conn->http_proxy.host.dispname; |
1213 | 0 | peer->port = cf->conn->http_proxy.port; |
1214 | 0 | } |
1215 | 0 | else |
1216 | 0 | #endif |
1217 | 0 | { |
1218 | 0 | ehostname = cf->conn->host.name; |
1219 | 0 | edispname = cf->conn->host.dispname; |
1220 | 0 | peer->port = cf->conn->remote_port; |
1221 | 0 | } |
1222 | | |
1223 | | /* hostname MUST exist and not be empty */ |
1224 | 0 | if(!ehostname || !ehostname[0]) { |
1225 | 0 | result = CURLE_FAILED_INIT; |
1226 | 0 | goto out; |
1227 | 0 | } |
1228 | | |
1229 | 0 | peer->hostname = strdup(ehostname); |
1230 | 0 | if(!peer->hostname) |
1231 | 0 | goto out; |
1232 | 0 | if(!edispname || !strcmp(ehostname, edispname)) |
1233 | 0 | peer->dispname = peer->hostname; |
1234 | 0 | else { |
1235 | 0 | peer->dispname = strdup(edispname); |
1236 | 0 | if(!peer->dispname) |
1237 | 0 | goto out; |
1238 | 0 | } |
1239 | 0 | peer->type = get_peer_type(peer->hostname); |
1240 | 0 | if(peer->type == CURL_SSL_PEER_DNS) { |
1241 | | /* not an IP address, normalize according to RCC 6066 ch. 3, |
1242 | | * max len of SNI is 2^16-1, no trailing dot */ |
1243 | 0 | size_t len = strlen(peer->hostname); |
1244 | 0 | if(len && (peer->hostname[len-1] == '.')) |
1245 | 0 | len--; |
1246 | 0 | if(len < USHRT_MAX) { |
1247 | 0 | peer->sni = calloc(1, len + 1); |
1248 | 0 | if(!peer->sni) |
1249 | 0 | goto out; |
1250 | 0 | Curl_strntolower(peer->sni, peer->hostname, len); |
1251 | 0 | peer->sni[len] = 0; |
1252 | 0 | } |
1253 | 0 | } |
1254 | | |
1255 | 0 | result = Curl_ssl_peer_key_make(cf, peer, tls_id, &peer->scache_key); |
1256 | |
|
1257 | 0 | out: |
1258 | 0 | if(result) |
1259 | 0 | Curl_ssl_peer_cleanup(peer); |
1260 | 0 | return result; |
1261 | 0 | } |
1262 | | |
1263 | | static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
1264 | 0 | { |
1265 | 0 | struct cf_call_data save; |
1266 | |
|
1267 | 0 | CF_DATA_SAVE(save, cf, data); |
1268 | 0 | cf_close(cf, data); |
1269 | 0 | CF_DATA_RESTORE(cf, save); |
1270 | 0 | cf_ctx_free(cf->ctx); |
1271 | 0 | cf->ctx = NULL; |
1272 | 0 | } |
1273 | | |
1274 | | static void ssl_cf_close(struct Curl_cfilter *cf, |
1275 | | struct Curl_easy *data) |
1276 | 0 | { |
1277 | 0 | struct cf_call_data save; |
1278 | |
|
1279 | 0 | CF_DATA_SAVE(save, cf, data); |
1280 | 0 | cf_close(cf, data); |
1281 | 0 | if(cf->next) |
1282 | 0 | cf->next->cft->do_close(cf->next, data); |
1283 | 0 | CF_DATA_RESTORE(cf, save); |
1284 | 0 | } |
1285 | | |
1286 | | static CURLcode ssl_cf_connect(struct Curl_cfilter *cf, |
1287 | | struct Curl_easy *data, |
1288 | | bool *done) |
1289 | 0 | { |
1290 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1291 | 0 | struct cf_call_data save; |
1292 | 0 | CURLcode result; |
1293 | |
|
1294 | 0 | if(cf->connected && (connssl->state != ssl_connection_deferred)) { |
1295 | 0 | *done = TRUE; |
1296 | 0 | return CURLE_OK; |
1297 | 0 | } |
1298 | | |
1299 | 0 | if(!cf->next) { |
1300 | 0 | *done = FALSE; |
1301 | 0 | return CURLE_FAILED_INIT; |
1302 | 0 | } |
1303 | | |
1304 | 0 | if(!cf->next->connected) { |
1305 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
1306 | 0 | if(result || !*done) |
1307 | 0 | return result; |
1308 | 0 | } |
1309 | | |
1310 | 0 | CF_DATA_SAVE(save, cf, data); |
1311 | 0 | CURL_TRC_CF(data, cf, "cf_connect()"); |
1312 | 0 | DEBUGASSERT(connssl); |
1313 | |
|
1314 | 0 | *done = FALSE; |
1315 | 0 | if(!connssl->peer.hostname) { |
1316 | 0 | char tls_id[80]; |
1317 | 0 | connssl->ssl_impl->version(tls_id, sizeof(tls_id) - 1); |
1318 | 0 | result = Curl_ssl_peer_init(&connssl->peer, cf, tls_id, TRNSPRT_TCP); |
1319 | 0 | if(result) |
1320 | 0 | goto out; |
1321 | 0 | } |
1322 | | |
1323 | 0 | if(!connssl->prefs_checked) { |
1324 | 0 | if(!ssl_prefs_check(data)) |
1325 | 0 | return CURLE_SSL_CONNECT_ERROR; |
1326 | 0 | connssl->prefs_checked = TRUE; |
1327 | 0 | } |
1328 | | |
1329 | 0 | result = connssl->ssl_impl->do_connect(cf, data, done); |
1330 | |
|
1331 | 0 | if(!result && *done) { |
1332 | 0 | cf->connected = TRUE; |
1333 | 0 | if(connssl->state == ssl_connection_complete) |
1334 | 0 | connssl->handshake_done = curlx_now(); |
1335 | | /* Connection can be deferred when sending early data */ |
1336 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete || |
1337 | 0 | connssl->state == ssl_connection_deferred); |
1338 | 0 | DEBUGASSERT(connssl->state != ssl_connection_deferred || |
1339 | 0 | connssl->earlydata_state > ssl_earlydata_none); |
1340 | 0 | } |
1341 | 0 | out: |
1342 | 0 | CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", result, *done); |
1343 | 0 | CF_DATA_RESTORE(cf, save); |
1344 | 0 | return result; |
1345 | 0 | } |
1346 | | |
1347 | | static CURLcode ssl_cf_set_earlydata(struct Curl_cfilter *cf, |
1348 | | struct Curl_easy *data, |
1349 | | const void *buf, size_t blen) |
1350 | 0 | { |
1351 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1352 | 0 | ssize_t nwritten = 0; |
1353 | 0 | CURLcode result = CURLE_OK; |
1354 | |
|
1355 | 0 | DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_await); |
1356 | 0 | DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata)); |
1357 | 0 | if(blen) { |
1358 | 0 | if(blen > connssl->earlydata_max) |
1359 | 0 | blen = connssl->earlydata_max; |
1360 | 0 | nwritten = Curl_bufq_write(&connssl->earlydata, buf, blen, &result); |
1361 | 0 | CURL_TRC_CF(data, cf, "ssl_cf_set_earlydata(len=%zu) -> %zd", |
1362 | 0 | blen, nwritten); |
1363 | 0 | if(nwritten < 0) |
1364 | 0 | return result; |
1365 | 0 | } |
1366 | 0 | return CURLE_OK; |
1367 | 0 | } |
1368 | | |
1369 | | static CURLcode ssl_cf_connect_deferred(struct Curl_cfilter *cf, |
1370 | | struct Curl_easy *data, |
1371 | | const void *buf, size_t blen, |
1372 | | bool *done) |
1373 | 0 | { |
1374 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1375 | 0 | CURLcode result = CURLE_OK; |
1376 | |
|
1377 | 0 | DEBUGASSERT(connssl->state == ssl_connection_deferred); |
1378 | 0 | *done = FALSE; |
1379 | 0 | if(connssl->earlydata_state == ssl_earlydata_await) { |
1380 | 0 | result = ssl_cf_set_earlydata(cf, data, buf, blen); |
1381 | 0 | if(result) |
1382 | 0 | return result; |
1383 | | /* we buffered any early data we'd like to send. Actually |
1384 | | * do the connect now which sends it and performs the handshake. */ |
1385 | 0 | connssl->earlydata_state = ssl_earlydata_sending; |
1386 | 0 | connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata); |
1387 | 0 | } |
1388 | | |
1389 | 0 | result = ssl_cf_connect(cf, data, done); |
1390 | |
|
1391 | 0 | if(!result && *done) { |
1392 | 0 | Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
1393 | 0 | switch(connssl->earlydata_state) { |
1394 | 0 | case ssl_earlydata_none: |
1395 | 0 | break; |
1396 | 0 | case ssl_earlydata_accepted: |
1397 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1398 | 0 | Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip); |
1399 | 0 | infof(data, "Server accepted %zu bytes of TLS early data.", |
1400 | 0 | connssl->earlydata_skip); |
1401 | 0 | break; |
1402 | 0 | case ssl_earlydata_rejected: |
1403 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1404 | 0 | Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip); |
1405 | 0 | infof(data, "Server rejected TLS early data."); |
1406 | 0 | connssl->earlydata_skip = 0; |
1407 | 0 | break; |
1408 | 0 | default: |
1409 | | /* This should not happen. Either we do not use early data or we |
1410 | | * should know if it was accepted or not. */ |
1411 | 0 | DEBUGASSERT(NULL); |
1412 | 0 | break; |
1413 | 0 | } |
1414 | 0 | } |
1415 | 0 | return result; |
1416 | 0 | } |
1417 | | |
1418 | | static bool ssl_cf_data_pending(struct Curl_cfilter *cf, |
1419 | | const struct Curl_easy *data) |
1420 | 0 | { |
1421 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1422 | 0 | struct cf_call_data save; |
1423 | 0 | bool result; |
1424 | |
|
1425 | 0 | CF_DATA_SAVE(save, cf, data); |
1426 | 0 | if(connssl->ssl_impl->data_pending && |
1427 | 0 | connssl->ssl_impl->data_pending(cf, data)) |
1428 | 0 | result = TRUE; |
1429 | 0 | else |
1430 | 0 | result = cf->next->cft->has_data_pending(cf->next, data); |
1431 | 0 | CF_DATA_RESTORE(cf, save); |
1432 | 0 | return result; |
1433 | 0 | } |
1434 | | |
1435 | | static CURLcode ssl_cf_send(struct Curl_cfilter *cf, |
1436 | | struct Curl_easy *data, |
1437 | | const void *buf, size_t blen, |
1438 | | bool eos, size_t *pnwritten) |
1439 | 0 | { |
1440 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1441 | 0 | struct cf_call_data save; |
1442 | 0 | CURLcode result = CURLE_OK; |
1443 | |
|
1444 | 0 | (void)eos; |
1445 | 0 | *pnwritten = 0; |
1446 | 0 | CF_DATA_SAVE(save, cf, data); |
1447 | |
|
1448 | 0 | if(connssl->state == ssl_connection_deferred) { |
1449 | 0 | bool done = FALSE; |
1450 | 0 | result = ssl_cf_connect_deferred(cf, data, buf, blen, &done); |
1451 | 0 | if(result) |
1452 | 0 | goto out; |
1453 | 0 | else if(!done) { |
1454 | 0 | result = CURLE_AGAIN; |
1455 | 0 | goto out; |
1456 | 0 | } |
1457 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1458 | 0 | } |
1459 | | |
1460 | 0 | if(connssl->earlydata_skip) { |
1461 | 0 | if(connssl->earlydata_skip >= blen) { |
1462 | 0 | connssl->earlydata_skip -= blen; |
1463 | 0 | result = CURLE_OK; |
1464 | 0 | *pnwritten = blen; |
1465 | 0 | goto out; |
1466 | 0 | } |
1467 | 0 | else { |
1468 | 0 | *pnwritten = connssl->earlydata_skip; |
1469 | 0 | buf = ((const char *)buf) + connssl->earlydata_skip; |
1470 | 0 | blen -= connssl->earlydata_skip; |
1471 | 0 | connssl->earlydata_skip = 0; |
1472 | 0 | } |
1473 | 0 | } |
1474 | | |
1475 | | /* OpenSSL and maybe other TLS libs do not like 0-length writes. Skip. */ |
1476 | 0 | if(blen > 0) { |
1477 | 0 | ssize_t nwritten; |
1478 | 0 | nwritten = connssl->ssl_impl->send_plain(cf, data, buf, blen, &result); |
1479 | 0 | if(nwritten > 0) |
1480 | 0 | *pnwritten += (size_t)nwritten; |
1481 | 0 | } |
1482 | |
|
1483 | 0 | out: |
1484 | 0 | CF_DATA_RESTORE(cf, save); |
1485 | 0 | return result; |
1486 | 0 | } |
1487 | | |
1488 | | static CURLcode ssl_cf_recv(struct Curl_cfilter *cf, |
1489 | | struct Curl_easy *data, char *buf, size_t len, |
1490 | | size_t *pnread) |
1491 | 0 | { |
1492 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1493 | 0 | struct cf_call_data save; |
1494 | 0 | CURLcode result = CURLE_OK; |
1495 | 0 | ssize_t nread; |
1496 | |
|
1497 | 0 | CF_DATA_SAVE(save, cf, data); |
1498 | 0 | *pnread = 0; |
1499 | 0 | if(connssl->state == ssl_connection_deferred) { |
1500 | 0 | bool done = FALSE; |
1501 | 0 | result = ssl_cf_connect_deferred(cf, data, NULL, 0, &done); |
1502 | 0 | if(result) |
1503 | 0 | goto out; |
1504 | 0 | else if(!done) { |
1505 | 0 | result = CURLE_AGAIN; |
1506 | 0 | goto out; |
1507 | 0 | } |
1508 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1509 | 0 | } |
1510 | | |
1511 | 0 | nread = connssl->ssl_impl->recv_plain(cf, data, buf, len, &result); |
1512 | 0 | if(nread > 0) { |
1513 | 0 | DEBUGASSERT((size_t)nread <= len); |
1514 | 0 | *pnread = (size_t)nread; |
1515 | 0 | } |
1516 | 0 | else if(nread == 0) { |
1517 | | /* eof */ |
1518 | 0 | result = CURLE_OK; |
1519 | 0 | } |
1520 | |
|
1521 | 0 | out: |
1522 | 0 | CURL_TRC_CF(data, cf, "cf_recv(len=%zu) -> %d, %zd", len, |
1523 | 0 | result, *pnread); |
1524 | 0 | CF_DATA_RESTORE(cf, save); |
1525 | 0 | return result; |
1526 | 0 | } |
1527 | | |
1528 | | static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf, |
1529 | | struct Curl_easy *data, |
1530 | | bool *done) |
1531 | 0 | { |
1532 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1533 | 0 | CURLcode result = CURLE_OK; |
1534 | |
|
1535 | 0 | *done = TRUE; |
1536 | | /* If we have done the SSL handshake, shut down the connection cleanly */ |
1537 | 0 | if(cf->connected && (connssl->state == ssl_connection_complete) && |
1538 | 0 | !cf->shutdown && Curl_ssl->shut_down) { |
1539 | 0 | struct cf_call_data save; |
1540 | |
|
1541 | 0 | CF_DATA_SAVE(save, cf, data); |
1542 | 0 | result = connssl->ssl_impl->shut_down(cf, data, TRUE, done); |
1543 | 0 | CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", result, *done); |
1544 | 0 | CF_DATA_RESTORE(cf, save); |
1545 | 0 | cf->shutdown = (result || *done); |
1546 | 0 | } |
1547 | 0 | return result; |
1548 | 0 | } |
1549 | | |
1550 | | static void ssl_cf_adjust_pollset(struct Curl_cfilter *cf, |
1551 | | struct Curl_easy *data, |
1552 | | struct easy_pollset *ps) |
1553 | 0 | { |
1554 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1555 | 0 | struct cf_call_data save; |
1556 | |
|
1557 | 0 | CF_DATA_SAVE(save, cf, data); |
1558 | 0 | connssl->ssl_impl->adjust_pollset(cf, data, ps); |
1559 | 0 | CF_DATA_RESTORE(cf, save); |
1560 | 0 | } |
1561 | | |
1562 | | static CURLcode ssl_cf_query(struct Curl_cfilter *cf, |
1563 | | struct Curl_easy *data, |
1564 | | int query, int *pres1, void *pres2) |
1565 | 0 | { |
1566 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1567 | |
|
1568 | 0 | switch(query) { |
1569 | 0 | case CF_QUERY_TIMER_APPCONNECT: { |
1570 | 0 | struct curltime *when = pres2; |
1571 | 0 | if(cf->connected && !Curl_ssl_cf_is_proxy(cf)) |
1572 | 0 | *when = connssl->handshake_done; |
1573 | 0 | return CURLE_OK; |
1574 | 0 | } |
1575 | 0 | default: |
1576 | 0 | break; |
1577 | 0 | } |
1578 | 0 | return cf->next ? |
1579 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1580 | 0 | CURLE_UNKNOWN_OPTION; |
1581 | 0 | } |
1582 | | |
1583 | | static bool cf_ssl_is_alive(struct Curl_cfilter *cf, struct Curl_easy *data, |
1584 | | bool *input_pending) |
1585 | 0 | { |
1586 | | /* |
1587 | | * This function tries to determine connection status. |
1588 | | */ |
1589 | 0 | return cf->next ? |
1590 | 0 | cf->next->cft->is_alive(cf->next, data, input_pending) : |
1591 | 0 | FALSE; /* pessimistic in absence of data */ |
1592 | 0 | } |
1593 | | |
1594 | | struct Curl_cftype Curl_cft_ssl = { |
1595 | | "SSL", |
1596 | | CF_TYPE_SSL, |
1597 | | CURL_LOG_LVL_NONE, |
1598 | | ssl_cf_destroy, |
1599 | | ssl_cf_connect, |
1600 | | ssl_cf_close, |
1601 | | ssl_cf_shutdown, |
1602 | | Curl_cf_def_get_host, |
1603 | | ssl_cf_adjust_pollset, |
1604 | | ssl_cf_data_pending, |
1605 | | ssl_cf_send, |
1606 | | ssl_cf_recv, |
1607 | | Curl_cf_def_cntrl, |
1608 | | cf_ssl_is_alive, |
1609 | | Curl_cf_def_conn_keep_alive, |
1610 | | ssl_cf_query, |
1611 | | }; |
1612 | | |
1613 | | #ifndef CURL_DISABLE_PROXY |
1614 | | |
1615 | | struct Curl_cftype Curl_cft_ssl_proxy = { |
1616 | | "SSL-PROXY", |
1617 | | CF_TYPE_SSL|CF_TYPE_PROXY, |
1618 | | CURL_LOG_LVL_NONE, |
1619 | | ssl_cf_destroy, |
1620 | | ssl_cf_connect, |
1621 | | ssl_cf_close, |
1622 | | ssl_cf_shutdown, |
1623 | | Curl_cf_def_get_host, |
1624 | | ssl_cf_adjust_pollset, |
1625 | | ssl_cf_data_pending, |
1626 | | ssl_cf_send, |
1627 | | ssl_cf_recv, |
1628 | | Curl_cf_def_cntrl, |
1629 | | cf_ssl_is_alive, |
1630 | | Curl_cf_def_conn_keep_alive, |
1631 | | Curl_cf_def_query, |
1632 | | }; |
1633 | | |
1634 | | #endif /* !CURL_DISABLE_PROXY */ |
1635 | | |
1636 | | static CURLcode cf_ssl_create(struct Curl_cfilter **pcf, |
1637 | | struct Curl_easy *data, |
1638 | | struct connectdata *conn) |
1639 | 0 | { |
1640 | 0 | struct Curl_cfilter *cf = NULL; |
1641 | 0 | struct ssl_connect_data *ctx; |
1642 | 0 | CURLcode result; |
1643 | |
|
1644 | 0 | DEBUGASSERT(data->conn); |
1645 | |
|
1646 | | #ifdef CURL_DISABLE_HTTP |
1647 | | /* We only support ALPN for HTTP so far. */ |
1648 | | DEBUGASSERT(!conn->bits.tls_enable_alpn); |
1649 | | ctx = cf_ctx_new(data, NULL); |
1650 | | #else |
1651 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(data->state.http_neg.wanted, |
1652 | 0 | conn->bits.tls_enable_alpn)); |
1653 | 0 | #endif |
1654 | 0 | if(!ctx) { |
1655 | 0 | result = CURLE_OUT_OF_MEMORY; |
1656 | 0 | goto out; |
1657 | 0 | } |
1658 | | |
1659 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx); |
1660 | |
|
1661 | 0 | out: |
1662 | 0 | if(result) |
1663 | 0 | cf_ctx_free(ctx); |
1664 | 0 | *pcf = result ? NULL : cf; |
1665 | 0 | return result; |
1666 | 0 | } |
1667 | | |
1668 | | CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data, |
1669 | | struct connectdata *conn, |
1670 | | int sockindex) |
1671 | 0 | { |
1672 | 0 | struct Curl_cfilter *cf; |
1673 | 0 | CURLcode result; |
1674 | |
|
1675 | 0 | result = cf_ssl_create(&cf, data, conn); |
1676 | 0 | if(!result) |
1677 | 0 | Curl_conn_cf_add(data, conn, sockindex, cf); |
1678 | 0 | return result; |
1679 | 0 | } |
1680 | | |
1681 | | CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at, |
1682 | | struct Curl_easy *data) |
1683 | 0 | { |
1684 | 0 | struct Curl_cfilter *cf; |
1685 | 0 | CURLcode result; |
1686 | |
|
1687 | 0 | result = cf_ssl_create(&cf, data, cf_at->conn); |
1688 | 0 | if(!result) |
1689 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1690 | 0 | return result; |
1691 | 0 | } |
1692 | | |
1693 | | #ifndef CURL_DISABLE_PROXY |
1694 | | |
1695 | | static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf, |
1696 | | struct Curl_easy *data, |
1697 | | struct connectdata *conn) |
1698 | 0 | { |
1699 | 0 | struct Curl_cfilter *cf = NULL; |
1700 | 0 | struct ssl_connect_data *ctx; |
1701 | 0 | CURLcode result; |
1702 | 0 | bool use_alpn = conn->bits.tls_enable_alpn; |
1703 | 0 | http_majors allowed = CURL_HTTP_V1x; |
1704 | |
|
1705 | | #ifdef USE_HTTP2 |
1706 | | if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) { |
1707 | | use_alpn = TRUE; |
1708 | | allowed = (CURL_HTTP_V1x|CURL_HTTP_V2x); |
1709 | | } |
1710 | | #endif |
1711 | |
|
1712 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(allowed, use_alpn)); |
1713 | 0 | if(!ctx) { |
1714 | 0 | result = CURLE_OUT_OF_MEMORY; |
1715 | 0 | goto out; |
1716 | 0 | } |
1717 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx); |
1718 | |
|
1719 | 0 | out: |
1720 | 0 | if(result) |
1721 | 0 | cf_ctx_free(ctx); |
1722 | 0 | *pcf = result ? NULL : cf; |
1723 | 0 | return result; |
1724 | 0 | } |
1725 | | |
1726 | | CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at, |
1727 | | struct Curl_easy *data) |
1728 | 0 | { |
1729 | 0 | struct Curl_cfilter *cf; |
1730 | 0 | CURLcode result; |
1731 | |
|
1732 | 0 | result = cf_ssl_proxy_create(&cf, data, cf_at->conn); |
1733 | 0 | if(!result) |
1734 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1735 | 0 | return result; |
1736 | 0 | } |
1737 | | |
1738 | | #endif /* !CURL_DISABLE_PROXY */ |
1739 | | |
1740 | | bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option) |
1741 | 0 | { |
1742 | 0 | (void)data; |
1743 | 0 | return (Curl_ssl->supports & ssl_option); |
1744 | 0 | } |
1745 | | |
1746 | | static struct Curl_cfilter *get_ssl_filter(struct Curl_cfilter *cf) |
1747 | 0 | { |
1748 | 0 | for(; cf; cf = cf->next) { |
1749 | 0 | if(cf->cft == &Curl_cft_ssl) |
1750 | 0 | return cf; |
1751 | 0 | #ifndef CURL_DISABLE_PROXY |
1752 | 0 | if(cf->cft == &Curl_cft_ssl_proxy) |
1753 | 0 | return cf; |
1754 | 0 | #endif |
1755 | 0 | } |
1756 | 0 | return NULL; |
1757 | 0 | } |
1758 | | |
1759 | | |
1760 | | void *Curl_ssl_get_internals(struct Curl_easy *data, int sockindex, |
1761 | | CURLINFO info, int n) |
1762 | 0 | { |
1763 | 0 | void *result = NULL; |
1764 | 0 | (void)n; |
1765 | 0 | if(data->conn) { |
1766 | 0 | struct Curl_cfilter *cf; |
1767 | | /* get first SSL filter in chain, if any is present */ |
1768 | 0 | cf = get_ssl_filter(data->conn->cfilter[sockindex]); |
1769 | 0 | if(cf) { |
1770 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1771 | 0 | struct cf_call_data save; |
1772 | 0 | CF_DATA_SAVE(save, cf, data); |
1773 | 0 | result = connssl->ssl_impl->get_internals(cf->ctx, info); |
1774 | 0 | CF_DATA_RESTORE(cf, save); |
1775 | 0 | } |
1776 | 0 | } |
1777 | 0 | return result; |
1778 | 0 | } |
1779 | | |
1780 | | static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf, |
1781 | | struct Curl_easy *data, |
1782 | | bool send_shutdown, bool *done) |
1783 | 0 | { |
1784 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1785 | 0 | struct cf_call_data save; |
1786 | 0 | CURLcode result = CURLE_OK; |
1787 | 0 | timediff_t timeout_ms; |
1788 | 0 | int what, loop = 10; |
1789 | |
|
1790 | 0 | if(cf->shutdown) { |
1791 | 0 | *done = TRUE; |
1792 | 0 | return CURLE_OK; |
1793 | 0 | } |
1794 | 0 | CF_DATA_SAVE(save, cf, data); |
1795 | |
|
1796 | 0 | *done = FALSE; |
1797 | 0 | while(!result && !*done && loop--) { |
1798 | 0 | timeout_ms = Curl_shutdown_timeleft(cf->conn, cf->sockindex, NULL); |
1799 | |
|
1800 | 0 | if(timeout_ms < 0) { |
1801 | | /* no need to continue if time is already up */ |
1802 | 0 | failf(data, "SSL shutdown timeout"); |
1803 | 0 | return CURLE_OPERATION_TIMEDOUT; |
1804 | 0 | } |
1805 | | |
1806 | 0 | result = connssl->ssl_impl->shut_down(cf, data, send_shutdown, done); |
1807 | 0 | if(result ||*done) |
1808 | 0 | goto out; |
1809 | | |
1810 | 0 | if(connssl->io_need) { |
1811 | 0 | what = Curl_conn_cf_poll(cf, data, timeout_ms); |
1812 | 0 | if(what < 0) { |
1813 | | /* fatal error */ |
1814 | 0 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); |
1815 | 0 | result = CURLE_RECV_ERROR; |
1816 | 0 | goto out; |
1817 | 0 | } |
1818 | 0 | else if(0 == what) { |
1819 | | /* timeout */ |
1820 | 0 | failf(data, "SSL shutdown timeout"); |
1821 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1822 | 0 | goto out; |
1823 | 0 | } |
1824 | | /* socket is readable or writable */ |
1825 | 0 | } |
1826 | 0 | } |
1827 | 0 | out: |
1828 | 0 | CF_DATA_RESTORE(cf, save); |
1829 | 0 | cf->shutdown = (result || *done); |
1830 | 0 | return result; |
1831 | 0 | } |
1832 | | |
1833 | | CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data, |
1834 | | int sockindex, bool send_shutdown) |
1835 | 0 | { |
1836 | 0 | struct Curl_cfilter *cf, *head; |
1837 | 0 | CURLcode result = CURLE_OK; |
1838 | |
|
1839 | 0 | head = data->conn ? data->conn->cfilter[sockindex] : NULL; |
1840 | 0 | for(cf = head; cf; cf = cf->next) { |
1841 | 0 | if(cf->cft == &Curl_cft_ssl) { |
1842 | 0 | bool done; |
1843 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, start"); |
1844 | 0 | Curl_shutdown_start(data, sockindex, 0, NULL); |
1845 | 0 | result = vtls_shutdown_blocking(cf, data, send_shutdown, &done); |
1846 | 0 | Curl_shutdown_clear(data, sockindex); |
1847 | 0 | if(!result && !done) /* blocking failed? */ |
1848 | 0 | result = CURLE_SSL_SHUTDOWN_FAILED; |
1849 | 0 | Curl_conn_cf_discard_sub(head, cf, data, FALSE); |
1850 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d", result); |
1851 | 0 | break; |
1852 | 0 | } |
1853 | 0 | } |
1854 | 0 | return result; |
1855 | 0 | } |
1856 | | |
1857 | | bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf) |
1858 | 0 | { |
1859 | 0 | return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY); |
1860 | 0 | } |
1861 | | |
1862 | | struct ssl_config_data * |
1863 | | Curl_ssl_cf_get_config(struct Curl_cfilter *cf, struct Curl_easy *data) |
1864 | 0 | { |
1865 | | #ifdef CURL_DISABLE_PROXY |
1866 | | (void)cf; |
1867 | | return &data->set.ssl; |
1868 | | #else |
1869 | 0 | return Curl_ssl_cf_is_proxy(cf) ? &data->set.proxy_ssl : &data->set.ssl; |
1870 | 0 | #endif |
1871 | 0 | } |
1872 | | |
1873 | | struct ssl_primary_config * |
1874 | | Curl_ssl_cf_get_primary_config(struct Curl_cfilter *cf) |
1875 | 0 | { |
1876 | | #ifdef CURL_DISABLE_PROXY |
1877 | | return &cf->conn->ssl_config; |
1878 | | #else |
1879 | 0 | return Curl_ssl_cf_is_proxy(cf) ? |
1880 | 0 | &cf->conn->proxy_ssl_config : &cf->conn->ssl_config; |
1881 | 0 | #endif |
1882 | 0 | } |
1883 | | |
1884 | | CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf, |
1885 | | const struct alpn_spec *spec) |
1886 | 0 | { |
1887 | 0 | size_t i, len; |
1888 | 0 | int off = 0; |
1889 | 0 | unsigned char blen; |
1890 | |
|
1891 | 0 | memset(buf, 0, sizeof(*buf)); |
1892 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1893 | 0 | len = strlen(spec->entries[i]); |
1894 | 0 | if(len >= ALPN_NAME_MAX) |
1895 | 0 | return CURLE_FAILED_INIT; |
1896 | 0 | blen = (unsigned char)len; |
1897 | 0 | if(off + blen + 1 >= (int)sizeof(buf->data)) |
1898 | 0 | return CURLE_FAILED_INIT; |
1899 | 0 | buf->data[off++] = blen; |
1900 | 0 | memcpy(buf->data + off, spec->entries[i], blen); |
1901 | 0 | off += blen; |
1902 | 0 | } |
1903 | 0 | buf->len = off; |
1904 | 0 | return CURLE_OK; |
1905 | 0 | } |
1906 | | |
1907 | | CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf, |
1908 | | const struct alpn_spec *spec) |
1909 | 0 | { |
1910 | 0 | size_t i, len; |
1911 | 0 | size_t off = 0; |
1912 | |
|
1913 | 0 | memset(buf, 0, sizeof(*buf)); |
1914 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1915 | 0 | len = strlen(spec->entries[i]); |
1916 | 0 | if(len >= ALPN_NAME_MAX) |
1917 | 0 | return CURLE_FAILED_INIT; |
1918 | 0 | if(off + len + 2 >= sizeof(buf->data)) |
1919 | 0 | return CURLE_FAILED_INIT; |
1920 | 0 | if(off) |
1921 | 0 | buf->data[off++] = ','; |
1922 | 0 | memcpy(buf->data + off, spec->entries[i], len); |
1923 | 0 | off += len; |
1924 | 0 | } |
1925 | 0 | buf->data[off] = '\0'; |
1926 | 0 | buf->len = (int)off; |
1927 | 0 | return CURLE_OK; |
1928 | 0 | } |
1929 | | |
1930 | | bool Curl_alpn_contains_proto(const struct alpn_spec *spec, |
1931 | | const char *proto) |
1932 | 0 | { |
1933 | 0 | size_t i, plen = proto ? strlen(proto) : 0; |
1934 | 0 | for(i = 0; spec && plen && i < spec->count; ++i) { |
1935 | 0 | size_t slen = strlen(spec->entries[i]); |
1936 | 0 | if((slen == plen) && !memcmp(proto, spec->entries[i], plen)) |
1937 | 0 | return TRUE; |
1938 | 0 | } |
1939 | 0 | return FALSE; |
1940 | 0 | } |
1941 | | |
1942 | | void Curl_alpn_restrict_to(struct alpn_spec *spec, const char *proto) |
1943 | 0 | { |
1944 | 0 | size_t plen = strlen(proto); |
1945 | 0 | DEBUGASSERT(plen < sizeof(spec->entries[0])); |
1946 | 0 | if(plen < sizeof(spec->entries[0])) { |
1947 | 0 | memcpy(spec->entries[0], proto, plen + 1); |
1948 | 0 | spec->count = 1; |
1949 | 0 | } |
1950 | 0 | } |
1951 | | |
1952 | | void Curl_alpn_copy(struct alpn_spec *dest, const struct alpn_spec *src) |
1953 | 0 | { |
1954 | 0 | if(src) |
1955 | 0 | memcpy(dest, src, sizeof(*dest)); |
1956 | 0 | else |
1957 | 0 | memset(dest, 0, sizeof(*dest)); |
1958 | 0 | } |
1959 | | |
1960 | | CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf, |
1961 | | struct Curl_easy *data, |
1962 | | struct ssl_connect_data *connssl, |
1963 | | const unsigned char *proto, |
1964 | | size_t proto_len) |
1965 | 0 | { |
1966 | 0 | CURLcode result = CURLE_OK; |
1967 | 0 | unsigned char *palpn = |
1968 | 0 | #ifndef CURL_DISABLE_PROXY |
1969 | 0 | (cf->conn->bits.tunnel_proxy && Curl_ssl_cf_is_proxy(cf)) ? |
1970 | 0 | &cf->conn->proxy_alpn : &cf->conn->alpn |
1971 | | #else |
1972 | | &cf->conn->alpn |
1973 | | #endif |
1974 | 0 | ; |
1975 | |
|
1976 | 0 | if(connssl->negotiated.alpn) { |
1977 | | /* When we ask for a specific ALPN protocol, we need the confirmation |
1978 | | * of it by the server, as we have installed protocol handler and |
1979 | | * connection filter chain for exactly this protocol. */ |
1980 | 0 | if(!proto_len) { |
1981 | 0 | failf(data, "ALPN: asked for '%s' from previous session, " |
1982 | 0 | "but server did not confirm it. Refusing to continue.", |
1983 | 0 | connssl->negotiated.alpn); |
1984 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1985 | 0 | goto out; |
1986 | 0 | } |
1987 | 0 | else if((strlen(connssl->negotiated.alpn) != proto_len) || |
1988 | 0 | memcmp(connssl->negotiated.alpn, proto, proto_len)) { |
1989 | 0 | failf(data, "ALPN: asked for '%s' from previous session, but server " |
1990 | 0 | "selected '%.*s'. Refusing to continue.", |
1991 | 0 | connssl->negotiated.alpn, (int)proto_len, proto); |
1992 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1993 | 0 | goto out; |
1994 | 0 | } |
1995 | | /* ALPN is exactly what we asked for, done. */ |
1996 | 0 | infof(data, "ALPN: server confirmed to use '%s'", |
1997 | 0 | connssl->negotiated.alpn); |
1998 | 0 | goto out; |
1999 | 0 | } |
2000 | | |
2001 | 0 | if(proto && proto_len) { |
2002 | 0 | if(memchr(proto, '\0', proto_len)) { |
2003 | 0 | failf(data, "ALPN: server selected protocol contains NUL. " |
2004 | 0 | "Refusing to continue."); |
2005 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
2006 | 0 | goto out; |
2007 | 0 | } |
2008 | 0 | connssl->negotiated.alpn = malloc(proto_len + 1); |
2009 | 0 | if(!connssl->negotiated.alpn) |
2010 | 0 | return CURLE_OUT_OF_MEMORY; |
2011 | 0 | memcpy(connssl->negotiated.alpn, proto, proto_len); |
2012 | 0 | connssl->negotiated.alpn[proto_len] = 0; |
2013 | 0 | } |
2014 | | |
2015 | 0 | if(proto && proto_len) { |
2016 | 0 | if(proto_len == ALPN_HTTP_1_1_LENGTH && |
2017 | 0 | !memcmp(ALPN_HTTP_1_1, proto, ALPN_HTTP_1_1_LENGTH)) { |
2018 | 0 | *palpn = CURL_HTTP_VERSION_1_1; |
2019 | 0 | } |
2020 | | #ifdef USE_HTTP2 |
2021 | | else if(proto_len == ALPN_H2_LENGTH && |
2022 | | !memcmp(ALPN_H2, proto, ALPN_H2_LENGTH)) { |
2023 | | *palpn = CURL_HTTP_VERSION_2; |
2024 | | } |
2025 | | #endif |
2026 | | #ifdef USE_HTTP3 |
2027 | | else if(proto_len == ALPN_H3_LENGTH && |
2028 | | !memcmp(ALPN_H3, proto, ALPN_H3_LENGTH)) { |
2029 | | *palpn = CURL_HTTP_VERSION_3; |
2030 | | } |
2031 | | #endif |
2032 | 0 | else { |
2033 | 0 | *palpn = CURL_HTTP_VERSION_NONE; |
2034 | 0 | failf(data, "unsupported ALPN protocol: '%.*s'", (int)proto_len, proto); |
2035 | | /* Previous code just ignored it and some vtls backends even ignore the |
2036 | | * return code of this function. */ |
2037 | | /* return CURLE_NOT_BUILT_IN; */ |
2038 | 0 | goto out; |
2039 | 0 | } |
2040 | | |
2041 | 0 | if(connssl->state == ssl_connection_deferred) |
2042 | 0 | infof(data, VTLS_INFOF_ALPN_DEFERRED, (int)proto_len, proto); |
2043 | 0 | else |
2044 | 0 | infof(data, VTLS_INFOF_ALPN_ACCEPTED, (int)proto_len, proto); |
2045 | 0 | } |
2046 | 0 | else { |
2047 | 0 | *palpn = CURL_HTTP_VERSION_NONE; |
2048 | 0 | if(connssl->state == ssl_connection_deferred) |
2049 | 0 | infof(data, VTLS_INFOF_NO_ALPN_DEFERRED); |
2050 | 0 | else |
2051 | 0 | infof(data, VTLS_INFOF_NO_ALPN); |
2052 | 0 | } |
2053 | | |
2054 | 0 | out: |
2055 | 0 | return result; |
2056 | 0 | } |
2057 | | |
2058 | | #endif /* USE_SSL */ |