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