1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
22
23 /* This file is for implementing all "generic" SSL functions that all libcurl
24 internals should use. It is then responsible for calling the proper
25 "backend" function.
26
27 SSL-functions in libcurl should call functions in this source file, and not
28 to any specific SSL-layer.
29
30 Curl_ssl_ - prefix for generic ones
31
32 Note that this source code uses the functions of the configured SSL
33 backend via the global Curl_ssl instance.
34
35 "SSL/TLS Strong Encryption: An Introduction"
36 https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html
37 */
38
39 #include "curl_setup.h"
40
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #include <sys/stat.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50
51 #include "urldata.h"
52
53 #include "vtls.h" /* generic SSL protos etc */
54 #include "slist.h"
55 #include "sendf.h"
56 #include "strcase.h"
57 #include "url.h"
58 #include "progress.h"
59 #include "share.h"
60 #include "multiif.h"
61 #include "timeval.h"
62 #include "curl_md5.h"
63 #include "warnless.h"
64 #include "curl_base64.h"
65 #include "curl_printf.h"
66 #include "strdup.h"
67
68 /* The last #include files should be: */
69 #include "curl_memory.h"
70 #include "memdebug.h"
71
72 /* convenience macro to check if this handle is using a shared SSL session */
73 #define SSLSESSION_SHARED(data) (data->share && \
74 (data->share->specifier & \
75 (1<<CURL_LOCK_DATA_SSL_SESSION)))
76
77 #define CLONE_STRING(var) \
78 do { \
79 if(source->var) { \
80 dest->var = strdup(source->var); \
81 if(!dest->var) \
82 return FALSE; \
83 } \
84 else \
85 dest->var = NULL; \
86 } while(0)
87
88 #define CLONE_BLOB(var) \
89 do { \
90 if(blobdup(&dest->var, source->var)) \
91 return FALSE; \
92 } while(0)
93
blobdup(struct curl_blob ** dest,struct curl_blob * src)94 static CURLcode blobdup(struct curl_blob **dest,
95 struct curl_blob *src)
96 {
97 DEBUGASSERT(dest);
98 DEBUGASSERT(!*dest);
99 if(src) {
100 /* only if there's data to dupe! */
101 struct curl_blob *d;
102 d = malloc(sizeof(struct curl_blob) + src->len);
103 if(!d)
104 return CURLE_OUT_OF_MEMORY;
105 d->len = src->len;
106 /* Always duplicate because the connection may survive longer than the
107 handle that passed in the blob. */
108 d->flags = CURL_BLOB_COPY;
109 d->data = (void *)((char *)d + sizeof(struct curl_blob));
110 memcpy(d->data, src->data, src->len);
111 *dest = d;
112 }
113 return CURLE_OK;
114 }
115
116 /* returns TRUE if the blobs are identical */
blobcmp(struct curl_blob * first,struct curl_blob * second)117 static bool blobcmp(struct curl_blob *first, struct curl_blob *second)
118 {
119 if(!first && !second) /* both are NULL */
120 return TRUE;
121 if(!first || !second) /* one is NULL */
122 return FALSE;
123 if(first->len != second->len) /* different sizes */
124 return FALSE;
125 return !memcmp(first->data, second->data, first->len); /* same data */
126 }
127
128
129 bool
Curl_ssl_config_matches(struct ssl_primary_config * data,struct ssl_primary_config * needle)130 Curl_ssl_config_matches(struct ssl_primary_config *data,
131 struct ssl_primary_config *needle)
132 {
133 if((data->version == needle->version) &&
134 (data->version_max == needle->version_max) &&
135 (data->ssl_options == needle->ssl_options) &&
136 (data->verifypeer == needle->verifypeer) &&
137 (data->verifyhost == needle->verifyhost) &&
138 (data->verifystatus == needle->verifystatus) &&
139 blobcmp(data->cert_blob, needle->cert_blob) &&
140 blobcmp(data->ca_info_blob, needle->ca_info_blob) &&
141 blobcmp(data->issuercert_blob, needle->issuercert_blob) &&
142 Curl_safecmp(data->CApath, needle->CApath) &&
143 Curl_safecmp(data->CAfile, needle->CAfile) &&
144 Curl_safecmp(data->issuercert, needle->issuercert) &&
145 Curl_safecmp(data->clientcert, needle->clientcert) &&
146 Curl_safecmp(data->random_file, needle->random_file) &&
147 Curl_safecmp(data->egdsocket, needle->egdsocket) &&
148 #ifdef USE_TLS_SRP
149 !Curl_timestrcmp(data->username, needle->username) &&
150 !Curl_timestrcmp(data->password, needle->password) &&
151 (data->authtype == needle->authtype) &&
152 #endif
153 Curl_safe_strcasecompare(data->cipher_list, needle->cipher_list) &&
154 Curl_safe_strcasecompare(data->cipher_list13, needle->cipher_list13) &&
155 Curl_safe_strcasecompare(data->curves, needle->curves) &&
156 Curl_safe_strcasecompare(data->CRLfile, needle->CRLfile) &&
157 Curl_safe_strcasecompare(data->pinned_key, needle->pinned_key))
158 return TRUE;
159
160 return FALSE;
161 }
162
163 bool
Curl_clone_primary_ssl_config(struct ssl_primary_config * source,struct ssl_primary_config * dest)164 Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
165 struct ssl_primary_config *dest)
166 {
167 dest->version = source->version;
168 dest->version_max = source->version_max;
169 dest->verifypeer = source->verifypeer;
170 dest->verifyhost = source->verifyhost;
171 dest->verifystatus = source->verifystatus;
172 dest->sessionid = source->sessionid;
173 dest->ssl_options = source->ssl_options;
174 #ifdef USE_TLS_SRP
175 dest->authtype = source->authtype;
176 #endif
177
178 CLONE_BLOB(cert_blob);
179 CLONE_BLOB(ca_info_blob);
180 CLONE_BLOB(issuercert_blob);
181 CLONE_STRING(CApath);
182 CLONE_STRING(CAfile);
183 CLONE_STRING(issuercert);
184 CLONE_STRING(clientcert);
185 CLONE_STRING(random_file);
186 CLONE_STRING(egdsocket);
187 CLONE_STRING(cipher_list);
188 CLONE_STRING(cipher_list13);
189 CLONE_STRING(pinned_key);
190 CLONE_STRING(curves);
191 CLONE_STRING(CRLfile);
192 #ifdef USE_TLS_SRP
193 CLONE_STRING(username);
194 CLONE_STRING(password);
195 #endif
196
197 return TRUE;
198 }
199
Curl_free_primary_ssl_config(struct ssl_primary_config * sslc)200 void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc)
201 {
202 Curl_safefree(sslc->CApath);
203 Curl_safefree(sslc->CAfile);
204 Curl_safefree(sslc->issuercert);
205 Curl_safefree(sslc->clientcert);
206 Curl_safefree(sslc->random_file);
207 Curl_safefree(sslc->egdsocket);
208 Curl_safefree(sslc->cipher_list);
209 Curl_safefree(sslc->cipher_list13);
210 Curl_safefree(sslc->pinned_key);
211 Curl_safefree(sslc->cert_blob);
212 Curl_safefree(sslc->ca_info_blob);
213 Curl_safefree(sslc->issuercert_blob);
214 Curl_safefree(sslc->curves);
215 Curl_safefree(sslc->CRLfile);
216 #ifdef USE_TLS_SRP
217 Curl_safefree(sslc->username);
218 Curl_safefree(sslc->password);
219 #endif
220 }
221
222 #ifdef USE_SSL
223 static int multissl_setup(const struct Curl_ssl *backend);
224 #endif
225
Curl_ssl_backend(void)226 int Curl_ssl_backend(void)
227 {
228 #ifdef USE_SSL
229 multissl_setup(NULL);
230 return Curl_ssl->info.id;
231 #else
232 return (int)CURLSSLBACKEND_NONE;
233 #endif
234 }
235
236 #ifdef USE_SSL
237
238 /* "global" init done? */
239 static bool init_ssl = FALSE;
240
241 /**
242 * Global SSL init
243 *
244 * @retval 0 error initializing SSL
245 * @retval 1 SSL initialized successfully
246 */
Curl_ssl_init(void)247 int Curl_ssl_init(void)
248 {
249 /* make sure this is only done once */
250 if(init_ssl)
251 return 1;
252 init_ssl = TRUE; /* never again */
253
254 return Curl_ssl->init();
255 }
256
257 #if defined(CURL_WITH_MULTI_SSL)
258 static const struct Curl_ssl Curl_ssl_multi;
259 #endif
260
261 /* Global cleanup */
Curl_ssl_cleanup(void)262 void Curl_ssl_cleanup(void)
263 {
264 if(init_ssl) {
265 /* only cleanup if we did a previous init */
266 Curl_ssl->cleanup();
267 #if defined(CURL_WITH_MULTI_SSL)
268 Curl_ssl = &Curl_ssl_multi;
269 #endif
270 init_ssl = FALSE;
271 }
272 }
273
ssl_prefs_check(struct Curl_easy * data)274 static bool ssl_prefs_check(struct Curl_easy *data)
275 {
276 /* check for CURLOPT_SSLVERSION invalid parameter value */
277 const long sslver = data->set.ssl.primary.version;
278 if((sslver < 0) || (sslver >= CURL_SSLVERSION_LAST)) {
279 failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION");
280 return FALSE;
281 }
282
283 switch(data->set.ssl.primary.version_max) {
284 case CURL_SSLVERSION_MAX_NONE:
285 case CURL_SSLVERSION_MAX_DEFAULT:
286 break;
287
288 default:
289 if((data->set.ssl.primary.version_max >> 16) < sslver) {
290 failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION");
291 return FALSE;
292 }
293 }
294
295 return TRUE;
296 }
297
298 #ifndef CURL_DISABLE_PROXY
299 static CURLcode
ssl_connect_init_proxy(struct connectdata * conn,int sockindex)300 ssl_connect_init_proxy(struct connectdata *conn, int sockindex)
301 {
302 DEBUGASSERT(conn->bits.proxy_ssl_connected[sockindex]);
303 if(ssl_connection_complete == conn->ssl[sockindex].state &&
304 !conn->proxy_ssl[sockindex].use) {
305 struct ssl_backend_data *pbdata;
306
307 if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY))
308 return CURLE_NOT_BUILT_IN;
309
310 /* The pointers to the ssl backend data, which is opaque here, are swapped
311 rather than move the contents. */
312 pbdata = conn->proxy_ssl[sockindex].backend;
313 conn->proxy_ssl[sockindex] = conn->ssl[sockindex];
314
315 memset(&conn->ssl[sockindex], 0, sizeof(conn->ssl[sockindex]));
316 memset(pbdata, 0, Curl_ssl->sizeof_ssl_backend_data);
317
318 conn->ssl[sockindex].backend = pbdata;
319 }
320 return CURLE_OK;
321 }
322 #endif
323
324 CURLcode
Curl_ssl_connect(struct Curl_easy * data,struct connectdata * conn,int sockindex)325 Curl_ssl_connect(struct Curl_easy *data, struct connectdata *conn,
326 int sockindex)
327 {
328 CURLcode result;
329
330 #ifndef CURL_DISABLE_PROXY
331 if(conn->bits.proxy_ssl_connected[sockindex]) {
332 result = ssl_connect_init_proxy(conn, sockindex);
333 if(result)
334 return result;
335 }
336 #endif
337
338 if(!ssl_prefs_check(data))
339 return CURLE_SSL_CONNECT_ERROR;
340
341 /* mark this is being ssl-enabled from here on. */
342 conn->ssl[sockindex].use = TRUE;
343 conn->ssl[sockindex].state = ssl_connection_negotiating;
344
345 result = Curl_ssl->connect_blocking(data, conn, sockindex);
346
347 if(!result)
348 Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSL is connected */
349 else
350 conn->ssl[sockindex].use = FALSE;
351
352 return result;
353 }
354
355 CURLcode
Curl_ssl_connect_nonblocking(struct Curl_easy * data,struct connectdata * conn,bool isproxy,int sockindex,bool * done)356 Curl_ssl_connect_nonblocking(struct Curl_easy *data, struct connectdata *conn,
357 bool isproxy, int sockindex, bool *done)
358 {
359 CURLcode result;
360
361 #ifndef CURL_DISABLE_PROXY
362 if(conn->bits.proxy_ssl_connected[sockindex]) {
363 result = ssl_connect_init_proxy(conn, sockindex);
364 if(result)
365 return result;
366 }
367 #endif
368 if(!ssl_prefs_check(data))
369 return CURLE_SSL_CONNECT_ERROR;
370
371 /* mark this is being ssl requested from here on. */
372 conn->ssl[sockindex].use = TRUE;
373 result = Curl_ssl->connect_nonblocking(data, conn, sockindex, done);
374 if(result)
375 conn->ssl[sockindex].use = FALSE;
376 else if(*done && !isproxy)
377 Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSL is connected */
378 return result;
379 }
380
381 /*
382 * Lock shared SSL session data
383 */
Curl_ssl_sessionid_lock(struct Curl_easy * data)384 void Curl_ssl_sessionid_lock(struct Curl_easy *data)
385 {
386 if(SSLSESSION_SHARED(data))
387 Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);
388 }
389
390 /*
391 * Unlock shared SSL session data
392 */
Curl_ssl_sessionid_unlock(struct Curl_easy * data)393 void Curl_ssl_sessionid_unlock(struct Curl_easy *data)
394 {
395 if(SSLSESSION_SHARED(data))
396 Curl_share_unlock(data, CURL_LOCK_DATA_SSL_SESSION);
397 }
398
399 /*
400 * Check if there's a session ID for the given connection in the cache, and if
401 * there's one suitable, it is provided. Returns TRUE when no entry matched.
402 */
Curl_ssl_getsessionid(struct Curl_easy * data,struct connectdata * conn,const bool isProxy,void ** ssl_sessionid,size_t * idsize,int sockindex)403 bool Curl_ssl_getsessionid(struct Curl_easy *data,
404 struct connectdata *conn,
405 const bool isProxy,
406 void **ssl_sessionid,
407 size_t *idsize, /* set 0 if unknown */
408 int sockindex)
409 {
410 struct Curl_ssl_session *check;
411 size_t i;
412 long *general_age;
413 bool no_match = TRUE;
414
415 #ifndef CURL_DISABLE_PROXY
416 struct ssl_primary_config * const ssl_config = isProxy ?
417 &conn->proxy_ssl_config :
418 &conn->ssl_config;
419 const char * const name = isProxy ?
420 conn->http_proxy.host.name : conn->host.name;
421 int port = isProxy ? (int)conn->port : conn->remote_port;
422 #else
423 /* no proxy support */
424 struct ssl_primary_config * const ssl_config = &conn->ssl_config;
425 const char * const name = conn->host.name;
426 int port = conn->remote_port;
427 #endif
428 (void)sockindex;
429 *ssl_sessionid = NULL;
430
431 #ifdef CURL_DISABLE_PROXY
432 if(isProxy)
433 return TRUE;
434 #endif
435
436 DEBUGASSERT(SSL_SET_OPTION(primary.sessionid));
437
438 if(!SSL_SET_OPTION(primary.sessionid) || !data->state.session)
439 /* session ID re-use is disabled or the session cache has not been
440 setup */
441 return TRUE;
442
443 /* Lock if shared */
444 if(SSLSESSION_SHARED(data))
445 general_age = &data->share->sessionage;
446 else
447 general_age = &data->state.sessionage;
448
449 for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
450 check = &data->state.session[i];
451 if(!check->sessionid)
452 /* not session ID means blank entry */
453 continue;
454 if(strcasecompare(name, check->name) &&
455 ((!conn->bits.conn_to_host && !check->conn_to_host) ||
456 (conn->bits.conn_to_host && check->conn_to_host &&
457 strcasecompare(conn->conn_to_host.name, check->conn_to_host))) &&
458 ((!conn->bits.conn_to_port && check->conn_to_port == -1) ||
459 (conn->bits.conn_to_port && check->conn_to_port != -1 &&
460 conn->conn_to_port == check->conn_to_port)) &&
461 (port == check->remote_port) &&
462 strcasecompare(conn->handler->scheme, check->scheme) &&
463 Curl_ssl_config_matches(ssl_config, &check->ssl_config)) {
464 /* yes, we have a session ID! */
465 (*general_age)++; /* increase general age */
466 check->age = *general_age; /* set this as used in this age */
467 *ssl_sessionid = check->sessionid;
468 if(idsize)
469 *idsize = check->idsize;
470 no_match = FALSE;
471 break;
472 }
473 }
474
475 DEBUGF(infof(data, "%s Session ID in cache for %s %s://%s:%d",
476 no_match? "Didn't find": "Found",
477 isProxy ? "proxy" : "host",
478 conn->handler->scheme, name, port));
479 return no_match;
480 }
481
482 /*
483 * Kill a single session ID entry in the cache.
484 */
Curl_ssl_kill_session(struct Curl_ssl_session * session)485 void Curl_ssl_kill_session(struct Curl_ssl_session *session)
486 {
487 if(session->sessionid) {
488 /* defensive check */
489
490 /* free the ID the SSL-layer specific way */
491 Curl_ssl->session_free(session->sessionid);
492
493 session->sessionid = NULL;
494 session->age = 0; /* fresh */
495
496 Curl_free_primary_ssl_config(&session->ssl_config);
497
498 Curl_safefree(session->name);
499 Curl_safefree(session->conn_to_host);
500 }
501 }
502
503 /*
504 * Delete the given session ID from the cache.
505 */
Curl_ssl_delsessionid(struct Curl_easy * data,void * ssl_sessionid)506 void Curl_ssl_delsessionid(struct Curl_easy *data, void *ssl_sessionid)
507 {
508 size_t i;
509
510 for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) {
511 struct Curl_ssl_session *check = &data->state.session[i];
512
513 if(check->sessionid == ssl_sessionid) {
514 Curl_ssl_kill_session(check);
515 break;
516 }
517 }
518 }
519
520 /*
521 * Store session id in the session cache. The ID passed on to this function
522 * must already have been extracted and allocated the proper way for the SSL
523 * layer. Curl_XXXX_session_free() will be called to free/kill the session ID
524 * later on.
525 */
Curl_ssl_addsessionid(struct Curl_easy * data,struct connectdata * conn,const bool isProxy,void * ssl_sessionid,size_t idsize,int sockindex)526 CURLcode Curl_ssl_addsessionid(struct Curl_easy *data,
527 struct connectdata *conn,
528 const bool isProxy,
529 void *ssl_sessionid,
530 size_t idsize,
531 int sockindex)
532 {
533 size_t i;
534 struct Curl_ssl_session *store;
535 long oldest_age;
536 char *clone_host;
537 char *clone_conn_to_host;
538 int conn_to_port;
539 long *general_age;
540 #ifndef CURL_DISABLE_PROXY
541 struct ssl_primary_config * const ssl_config = isProxy ?
542 &conn->proxy_ssl_config :
543 &conn->ssl_config;
544 const char *hostname = isProxy ? conn->http_proxy.host.name :
545 conn->host.name;
546 #else
547 struct ssl_primary_config * const ssl_config = &conn->ssl_config;
548 const char *hostname = conn->host.name;
549 #endif
550 (void)sockindex;
551 if(!data->state.session)
552 return CURLE_OK;
553
554 store = &data->state.session[0];
555 oldest_age = data->state.session[0].age; /* zero if unused */
556 DEBUGASSERT(SSL_SET_OPTION(primary.sessionid));
557
558 clone_host = strdup(hostname);
559 if(!clone_host)
560 return CURLE_OUT_OF_MEMORY; /* bail out */
561
562 if(conn->bits.conn_to_host) {
563 clone_conn_to_host = strdup(conn->conn_to_host.name);
564 if(!clone_conn_to_host) {
565 free(clone_host);
566 return CURLE_OUT_OF_MEMORY; /* bail out */
567 }
568 }
569 else
570 clone_conn_to_host = NULL;
571
572 if(conn->bits.conn_to_port)
573 conn_to_port = conn->conn_to_port;
574 else
575 conn_to_port = -1;
576
577 /* Now we should add the session ID and the host name to the cache, (remove
578 the oldest if necessary) */
579
580 /* If using shared SSL session, lock! */
581 if(SSLSESSION_SHARED(data)) {
582 general_age = &data->share->sessionage;
583 }
584 else {
585 general_age = &data->state.sessionage;
586 }
587
588 /* find an empty slot for us, or find the oldest */
589 for(i = 1; (i < data->set.general_ssl.max_ssl_sessions) &&
590 data->state.session[i].sessionid; i++) {
591 if(data->state.session[i].age < oldest_age) {
592 oldest_age = data->state.session[i].age;
593 store = &data->state.session[i];
594 }
595 }
596 if(i == data->set.general_ssl.max_ssl_sessions)
597 /* cache is full, we must "kill" the oldest entry! */
598 Curl_ssl_kill_session(store);
599 else
600 store = &data->state.session[i]; /* use this slot */
601
602 /* now init the session struct wisely */
603 store->sessionid = ssl_sessionid;
604 store->idsize = idsize;
605 store->age = *general_age; /* set current age */
606 /* free it if there's one already present */
607 free(store->name);
608 free(store->conn_to_host);
609 store->name = clone_host; /* clone host name */
610 store->conn_to_host = clone_conn_to_host; /* clone connect to host name */
611 store->conn_to_port = conn_to_port; /* connect to port number */
612 /* port number */
613 store->remote_port = isProxy ? (int)conn->port : conn->remote_port;
614 store->scheme = conn->handler->scheme;
615
616 if(!Curl_clone_primary_ssl_config(ssl_config, &store->ssl_config)) {
617 Curl_free_primary_ssl_config(&store->ssl_config);
618 store->sessionid = NULL; /* let caller free sessionid */
619 free(clone_host);
620 free(clone_conn_to_host);
621 return CURLE_OUT_OF_MEMORY;
622 }
623
624 DEBUGF(infof(data, "Added Session ID to cache for %s://%s:%d [%s]",
625 store->scheme, store->name, store->remote_port,
626 isProxy ? "PROXY" : "server"));
627 return CURLE_OK;
628 }
629
Curl_ssl_associate_conn(struct Curl_easy * data,struct connectdata * conn)630 void Curl_ssl_associate_conn(struct Curl_easy *data,
631 struct connectdata *conn)
632 {
633 if(Curl_ssl->associate_connection) {
634 Curl_ssl->associate_connection(data, conn, FIRSTSOCKET);
635 if(conn->sock[SECONDARYSOCKET] && conn->bits.sock_accepted)
636 Curl_ssl->associate_connection(data, conn, SECONDARYSOCKET);
637 }
638 }
639
Curl_ssl_detach_conn(struct Curl_easy * data,struct connectdata * conn)640 void Curl_ssl_detach_conn(struct Curl_easy *data,
641 struct connectdata *conn)
642 {
643 if(Curl_ssl->disassociate_connection) {
644 Curl_ssl->disassociate_connection(data, FIRSTSOCKET);
645 if(conn->sock[SECONDARYSOCKET] && conn->bits.sock_accepted)
646 Curl_ssl->disassociate_connection(data, SECONDARYSOCKET);
647 }
648 }
649
Curl_ssl_close_all(struct Curl_easy * data)650 void Curl_ssl_close_all(struct Curl_easy *data)
651 {
652 /* kill the session ID cache if not shared */
653 if(data->state.session && !SSLSESSION_SHARED(data)) {
654 size_t i;
655 for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++)
656 /* the single-killer function handles empty table slots */
657 Curl_ssl_kill_session(&data->state.session[i]);
658
659 /* free the cache data */
660 Curl_safefree(data->state.session);
661 }
662
663 Curl_ssl->close_all(data);
664 }
665
Curl_ssl_getsock(struct connectdata * conn,curl_socket_t * socks)666 int Curl_ssl_getsock(struct connectdata *conn, curl_socket_t *socks)
667 {
668 struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
669
670 if(connssl->connecting_state == ssl_connect_2_writing) {
671 /* write mode */
672 socks[0] = conn->sock[FIRSTSOCKET];
673 return GETSOCK_WRITESOCK(0);
674 }
675 if(connssl->connecting_state == ssl_connect_2_reading) {
676 /* read mode */
677 socks[0] = conn->sock[FIRSTSOCKET];
678 return GETSOCK_READSOCK(0);
679 }
680
681 return GETSOCK_BLANK;
682 }
683
Curl_ssl_close(struct Curl_easy * data,struct connectdata * conn,int sockindex)684 void Curl_ssl_close(struct Curl_easy *data, struct connectdata *conn,
685 int sockindex)
686 {
687 DEBUGASSERT((sockindex <= 1) && (sockindex >= -1));
688 Curl_ssl->close_one(data, conn, sockindex);
689 conn->ssl[sockindex].state = ssl_connection_none;
690 }
691
Curl_ssl_shutdown(struct Curl_easy * data,struct connectdata * conn,int sockindex)692 CURLcode Curl_ssl_shutdown(struct Curl_easy *data, struct connectdata *conn,
693 int sockindex)
694 {
695 if(Curl_ssl->shut_down(data, conn, sockindex))
696 return CURLE_SSL_SHUTDOWN_FAILED;
697
698 conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */
699 conn->ssl[sockindex].state = ssl_connection_none;
700
701 conn->recv[sockindex] = Curl_recv_plain;
702 conn->send[sockindex] = Curl_send_plain;
703
704 return CURLE_OK;
705 }
706
707 /* Selects an SSL crypto engine
708 */
Curl_ssl_set_engine(struct Curl_easy * data,const char * engine)709 CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine)
710 {
711 return Curl_ssl->set_engine(data, engine);
712 }
713
714 /* Selects the default SSL crypto engine
715 */
Curl_ssl_set_engine_default(struct Curl_easy * data)716 CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data)
717 {
718 return Curl_ssl->set_engine_default(data);
719 }
720
721 /* Return list of OpenSSL crypto engine names. */
Curl_ssl_engines_list(struct Curl_easy * data)722 struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data)
723 {
724 return Curl_ssl->engines_list(data);
725 }
726
727 /*
728 * This sets up a session ID cache to the specified size. Make sure this code
729 * is agnostic to what underlying SSL technology we use.
730 */
Curl_ssl_initsessions(struct Curl_easy * data,size_t amount)731 CURLcode Curl_ssl_initsessions(struct Curl_easy *data, size_t amount)
732 {
733 struct Curl_ssl_session *session;
734
735 if(data->state.session)
736 /* this is just a precaution to prevent multiple inits */
737 return CURLE_OK;
738
739 session = calloc(amount, sizeof(struct Curl_ssl_session));
740 if(!session)
741 return CURLE_OUT_OF_MEMORY;
742
743 /* store the info in the SSL section */
744 data->set.general_ssl.max_ssl_sessions = amount;
745 data->state.session = session;
746 data->state.sessionage = 1; /* this is brand new */
747 return CURLE_OK;
748 }
749
750 static size_t multissl_version(char *buffer, size_t size);
751
Curl_ssl_version(char * buffer,size_t size)752 void Curl_ssl_version(char *buffer, size_t size)
753 {
754 #ifdef CURL_WITH_MULTI_SSL
755 (void)multissl_version(buffer, size);
756 #else
757 (void)Curl_ssl->version(buffer, size);
758 #endif
759 }
760
761 /*
762 * This function tries to determine connection status.
763 *
764 * Return codes:
765 * 1 means the connection is still in place
766 * 0 means the connection has been closed
767 * -1 means the connection status is unknown
768 */
Curl_ssl_check_cxn(struct connectdata * conn)769 int Curl_ssl_check_cxn(struct connectdata *conn)
770 {
771 return Curl_ssl->check_cxn(conn);
772 }
773
Curl_ssl_data_pending(const struct connectdata * conn,int connindex)774 bool Curl_ssl_data_pending(const struct connectdata *conn,
775 int connindex)
776 {
777 return Curl_ssl->data_pending(conn, connindex);
778 }
779
Curl_ssl_free_certinfo(struct Curl_easy * data)780 void Curl_ssl_free_certinfo(struct Curl_easy *data)
781 {
782 struct curl_certinfo *ci = &data->info.certs;
783
784 if(ci->num_of_certs) {
785 /* free all individual lists used */
786 int i;
787 for(i = 0; i<ci->num_of_certs; i++) {
788 curl_slist_free_all(ci->certinfo[i]);
789 ci->certinfo[i] = NULL;
790 }
791
792 free(ci->certinfo); /* free the actual array too */
793 ci->certinfo = NULL;
794 ci->num_of_certs = 0;
795 }
796 }
797
Curl_ssl_init_certinfo(struct Curl_easy * data,int num)798 CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num)
799 {
800 struct curl_certinfo *ci = &data->info.certs;
801 struct curl_slist **table;
802
803 /* Free any previous certificate information structures */
804 Curl_ssl_free_certinfo(data);
805
806 /* Allocate the required certificate information structures */
807 table = calloc((size_t) num, sizeof(struct curl_slist *));
808 if(!table)
809 return CURLE_OUT_OF_MEMORY;
810
811 ci->num_of_certs = num;
812 ci->certinfo = table;
813
814 return CURLE_OK;
815 }
816
817 /*
818 * 'value' is NOT a null-terminated string
819 */
Curl_ssl_push_certinfo_len(struct Curl_easy * data,int certnum,const char * label,const char * value,size_t valuelen)820 CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data,
821 int certnum,
822 const char *label,
823 const char *value,
824 size_t valuelen)
825 {
826 struct curl_certinfo *ci = &data->info.certs;
827 char *output;
828 struct curl_slist *nl;
829 CURLcode result = CURLE_OK;
830 size_t labellen = strlen(label);
831 size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */
832
833 output = malloc(outlen);
834 if(!output)
835 return CURLE_OUT_OF_MEMORY;
836
837 /* sprintf the label and colon */
838 msnprintf(output, outlen, "%s:", label);
839
840 /* memcpy the value (it might not be null-terminated) */
841 memcpy(&output[labellen + 1], value, valuelen);
842
843 /* null-terminate the output */
844 output[labellen + 1 + valuelen] = 0;
845
846 nl = Curl_slist_append_nodup(ci->certinfo[certnum], output);
847 if(!nl) {
848 free(output);
849 curl_slist_free_all(ci->certinfo[certnum]);
850 result = CURLE_OUT_OF_MEMORY;
851 }
852
853 ci->certinfo[certnum] = nl;
854 return result;
855 }
856
857 /*
858 * This is a convenience function for push_certinfo_len that takes a zero
859 * terminated value.
860 */
Curl_ssl_push_certinfo(struct Curl_easy * data,int certnum,const char * label,const char * value)861 CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data,
862 int certnum,
863 const char *label,
864 const char *value)
865 {
866 size_t valuelen = strlen(value);
867
868 return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
869 }
870
Curl_ssl_random(struct Curl_easy * data,unsigned char * entropy,size_t length)871 CURLcode Curl_ssl_random(struct Curl_easy *data,
872 unsigned char *entropy,
873 size_t length)
874 {
875 return Curl_ssl->random(data, entropy, length);
876 }
877
878 /*
879 * Public key pem to der conversion
880 */
881
pubkey_pem_to_der(const char * pem,unsigned char ** der,size_t * der_len)882 static CURLcode pubkey_pem_to_der(const char *pem,
883 unsigned char **der, size_t *der_len)
884 {
885 char *stripped_pem, *begin_pos, *end_pos;
886 size_t pem_count, stripped_pem_count = 0, pem_len;
887 CURLcode result;
888
889 /* if no pem, exit. */
890 if(!pem)
891 return CURLE_BAD_CONTENT_ENCODING;
892
893 begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
894 if(!begin_pos)
895 return CURLE_BAD_CONTENT_ENCODING;
896
897 pem_count = begin_pos - pem;
898 /* Invalid if not at beginning AND not directly following \n */
899 if(0 != pem_count && '\n' != pem[pem_count - 1])
900 return CURLE_BAD_CONTENT_ENCODING;
901
902 /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
903 pem_count += 26;
904
905 /* Invalid if not directly following \n */
906 end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
907 if(!end_pos)
908 return CURLE_BAD_CONTENT_ENCODING;
909
910 pem_len = end_pos - pem;
911
912 stripped_pem = malloc(pem_len - pem_count + 1);
913 if(!stripped_pem)
914 return CURLE_OUT_OF_MEMORY;
915
916 /*
917 * Here we loop through the pem array one character at a time between the
918 * correct indices, and place each character that is not '\n' or '\r'
919 * into the stripped_pem array, which should represent the raw base64 string
920 */
921 while(pem_count < pem_len) {
922 if('\n' != pem[pem_count] && '\r' != pem[pem_count])
923 stripped_pem[stripped_pem_count++] = pem[pem_count];
924 ++pem_count;
925 }
926 /* Place the null terminator in the correct place */
927 stripped_pem[stripped_pem_count] = '\0';
928
929 result = Curl_base64_decode(stripped_pem, der, der_len);
930
931 Curl_safefree(stripped_pem);
932
933 return result;
934 }
935
936 /*
937 * Generic pinned public key check.
938 */
939
Curl_pin_peer_pubkey(struct Curl_easy * data,const char * pinnedpubkey,const unsigned char * pubkey,size_t pubkeylen)940 CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
941 const char *pinnedpubkey,
942 const unsigned char *pubkey, size_t pubkeylen)
943 {
944 FILE *fp;
945 unsigned char *buf = NULL, *pem_ptr = NULL;
946 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
947
948 /* if a path wasn't specified, don't pin */
949 if(!pinnedpubkey)
950 return CURLE_OK;
951 if(!pubkey || !pubkeylen)
952 return result;
953
954 /* only do this if pinnedpubkey starts with "sha256//", length 8 */
955 if(strncmp(pinnedpubkey, "sha256//", 8) == 0) {
956 CURLcode encode;
957 size_t encodedlen, pinkeylen;
958 char *encoded, *pinkeycopy, *begin_pos, *end_pos;
959 unsigned char *sha256sumdigest;
960
961 if(!Curl_ssl->sha256sum) {
962 /* without sha256 support, this cannot match */
963 return result;
964 }
965
966 /* compute sha256sum of public key */
967 sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH);
968 if(!sha256sumdigest)
969 return CURLE_OUT_OF_MEMORY;
970 encode = Curl_ssl->sha256sum(pubkey, pubkeylen,
971 sha256sumdigest, CURL_SHA256_DIGEST_LENGTH);
972
973 if(encode != CURLE_OK)
974 return encode;
975
976 encode = Curl_base64_encode(data, (char *)sha256sumdigest,
977 CURL_SHA256_DIGEST_LENGTH, &encoded,
978 &encodedlen);
979 Curl_safefree(sha256sumdigest);
980
981 if(encode)
982 return encode;
983
984 infof(data, " public key hash: sha256//%s", encoded);
985
986 /* it starts with sha256//, copy so we can modify it */
987 pinkeylen = strlen(pinnedpubkey) + 1;
988 pinkeycopy = malloc(pinkeylen);
989 if(!pinkeycopy) {
990 Curl_safefree(encoded);
991 return CURLE_OUT_OF_MEMORY;
992 }
993 memcpy(pinkeycopy, pinnedpubkey, pinkeylen);
994 /* point begin_pos to the copy, and start extracting keys */
995 begin_pos = pinkeycopy;
996 do {
997 end_pos = strstr(begin_pos, ";sha256//");
998 /*
999 * if there is an end_pos, null terminate,
1000 * otherwise it'll go to the end of the original string
1001 */
1002 if(end_pos)
1003 end_pos[0] = '\0';
1004
1005 /* compare base64 sha256 digests, 8 is the length of "sha256//" */
1006 if(encodedlen == strlen(begin_pos + 8) &&
1007 !memcmp(encoded, begin_pos + 8, encodedlen)) {
1008 result = CURLE_OK;
1009 break;
1010 }
1011
1012 /*
1013 * change back the null-terminator we changed earlier,
1014 * and look for next begin
1015 */
1016 if(end_pos) {
1017 end_pos[0] = ';';
1018 begin_pos = strstr(end_pos, "sha256//");
1019 }
1020 } while(end_pos && begin_pos);
1021 Curl_safefree(encoded);
1022 Curl_safefree(pinkeycopy);
1023 return result;
1024 }
1025
1026 fp = fopen(pinnedpubkey, "rb");
1027 if(!fp)
1028 return result;
1029
1030 do {
1031 long filesize;
1032 size_t size, pem_len;
1033 CURLcode pem_read;
1034
1035 /* Determine the file's size */
1036 if(fseek(fp, 0, SEEK_END))
1037 break;
1038 filesize = ftell(fp);
1039 if(fseek(fp, 0, SEEK_SET))
1040 break;
1041 if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
1042 break;
1043
1044 /*
1045 * if the size of our certificate is bigger than the file
1046 * size then it can't match
1047 */
1048 size = curlx_sotouz((curl_off_t) filesize);
1049 if(pubkeylen > size)
1050 break;
1051
1052 /*
1053 * Allocate buffer for the pinned key
1054 * With 1 additional byte for null terminator in case of PEM key
1055 */
1056 buf = malloc(size + 1);
1057 if(!buf)
1058 break;
1059
1060 /* Returns number of elements read, which should be 1 */
1061 if((int) fread(buf, size, 1, fp) != 1)
1062 break;
1063
1064 /* If the sizes are the same, it can't be base64 encoded, must be der */
1065 if(pubkeylen == size) {
1066 if(!memcmp(pubkey, buf, pubkeylen))
1067 result = CURLE_OK;
1068 break;
1069 }
1070
1071 /*
1072 * Otherwise we will assume it's PEM and try to decode it
1073 * after placing null terminator
1074 */
1075 buf[size] = '\0';
1076 pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len);
1077 /* if it wasn't read successfully, exit */
1078 if(pem_read)
1079 break;
1080
1081 /*
1082 * if the size of our certificate doesn't match the size of
1083 * the decoded file, they can't be the same, otherwise compare
1084 */
1085 if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
1086 result = CURLE_OK;
1087 } while(0);
1088
1089 Curl_safefree(buf);
1090 Curl_safefree(pem_ptr);
1091 fclose(fp);
1092
1093 return result;
1094 }
1095
1096 /*
1097 * Check whether the SSL backend supports the status_request extension.
1098 */
Curl_ssl_cert_status_request(void)1099 bool Curl_ssl_cert_status_request(void)
1100 {
1101 return Curl_ssl->cert_status_request();
1102 }
1103
1104 /*
1105 * Check whether the SSL backend supports false start.
1106 */
Curl_ssl_false_start(void)1107 bool Curl_ssl_false_start(void)
1108 {
1109 return Curl_ssl->false_start();
1110 }
1111
1112 /*
1113 * Check whether the SSL backend supports setting TLS 1.3 cipher suites
1114 */
Curl_ssl_tls13_ciphersuites(void)1115 bool Curl_ssl_tls13_ciphersuites(void)
1116 {
1117 return Curl_ssl->supports & SSLSUPP_TLS13_CIPHERSUITES;
1118 }
1119
1120 /*
1121 * Default implementations for unsupported functions.
1122 */
1123
Curl_none_init(void)1124 int Curl_none_init(void)
1125 {
1126 return 1;
1127 }
1128
Curl_none_cleanup(void)1129 void Curl_none_cleanup(void)
1130 { }
1131
Curl_none_shutdown(struct Curl_easy * data UNUSED_PARAM,struct connectdata * conn UNUSED_PARAM,int sockindex UNUSED_PARAM)1132 int Curl_none_shutdown(struct Curl_easy *data UNUSED_PARAM,
1133 struct connectdata *conn UNUSED_PARAM,
1134 int sockindex UNUSED_PARAM)
1135 {
1136 (void)data;
1137 (void)conn;
1138 (void)sockindex;
1139 return 0;
1140 }
1141
Curl_none_check_cxn(struct connectdata * conn UNUSED_PARAM)1142 int Curl_none_check_cxn(struct connectdata *conn UNUSED_PARAM)
1143 {
1144 (void)conn;
1145 return -1;
1146 }
1147
Curl_none_random(struct Curl_easy * data UNUSED_PARAM,unsigned char * entropy UNUSED_PARAM,size_t length UNUSED_PARAM)1148 CURLcode Curl_none_random(struct Curl_easy *data UNUSED_PARAM,
1149 unsigned char *entropy UNUSED_PARAM,
1150 size_t length UNUSED_PARAM)
1151 {
1152 (void)data;
1153 (void)entropy;
1154 (void)length;
1155 return CURLE_NOT_BUILT_IN;
1156 }
1157
Curl_none_close_all(struct Curl_easy * data UNUSED_PARAM)1158 void Curl_none_close_all(struct Curl_easy *data UNUSED_PARAM)
1159 {
1160 (void)data;
1161 }
1162
Curl_none_session_free(void * ptr UNUSED_PARAM)1163 void Curl_none_session_free(void *ptr UNUSED_PARAM)
1164 {
1165 (void)ptr;
1166 }
1167
Curl_none_data_pending(const struct connectdata * conn UNUSED_PARAM,int connindex UNUSED_PARAM)1168 bool Curl_none_data_pending(const struct connectdata *conn UNUSED_PARAM,
1169 int connindex UNUSED_PARAM)
1170 {
1171 (void)conn;
1172 (void)connindex;
1173 return 0;
1174 }
1175
Curl_none_cert_status_request(void)1176 bool Curl_none_cert_status_request(void)
1177 {
1178 return FALSE;
1179 }
1180
Curl_none_set_engine(struct Curl_easy * data UNUSED_PARAM,const char * engine UNUSED_PARAM)1181 CURLcode Curl_none_set_engine(struct Curl_easy *data UNUSED_PARAM,
1182 const char *engine UNUSED_PARAM)
1183 {
1184 (void)data;
1185 (void)engine;
1186 return CURLE_NOT_BUILT_IN;
1187 }
1188
Curl_none_set_engine_default(struct Curl_easy * data UNUSED_PARAM)1189 CURLcode Curl_none_set_engine_default(struct Curl_easy *data UNUSED_PARAM)
1190 {
1191 (void)data;
1192 return CURLE_NOT_BUILT_IN;
1193 }
1194
Curl_none_engines_list(struct Curl_easy * data UNUSED_PARAM)1195 struct curl_slist *Curl_none_engines_list(struct Curl_easy *data UNUSED_PARAM)
1196 {
1197 (void)data;
1198 return (struct curl_slist *)NULL;
1199 }
1200
Curl_none_false_start(void)1201 bool Curl_none_false_start(void)
1202 {
1203 return FALSE;
1204 }
1205
multissl_init(void)1206 static int multissl_init(void)
1207 {
1208 if(multissl_setup(NULL))
1209 return 1;
1210 return Curl_ssl->init();
1211 }
1212
multissl_connect(struct Curl_easy * data,struct connectdata * conn,int sockindex)1213 static CURLcode multissl_connect(struct Curl_easy *data,
1214 struct connectdata *conn, int sockindex)
1215 {
1216 if(multissl_setup(NULL))
1217 return CURLE_FAILED_INIT;
1218 return Curl_ssl->connect_blocking(data, conn, sockindex);
1219 }
1220
multissl_connect_nonblocking(struct Curl_easy * data,struct connectdata * conn,int sockindex,bool * done)1221 static CURLcode multissl_connect_nonblocking(struct Curl_easy *data,
1222 struct connectdata *conn,
1223 int sockindex, bool *done)
1224 {
1225 if(multissl_setup(NULL))
1226 return CURLE_FAILED_INIT;
1227 return Curl_ssl->connect_nonblocking(data, conn, sockindex, done);
1228 }
1229
multissl_getsock(struct connectdata * conn,curl_socket_t * socks)1230 static int multissl_getsock(struct connectdata *conn, curl_socket_t *socks)
1231 {
1232 if(multissl_setup(NULL))
1233 return 0;
1234 return Curl_ssl->getsock(conn, socks);
1235 }
1236
multissl_get_internals(struct ssl_connect_data * connssl,CURLINFO info)1237 static void *multissl_get_internals(struct ssl_connect_data *connssl,
1238 CURLINFO info)
1239 {
1240 if(multissl_setup(NULL))
1241 return NULL;
1242 return Curl_ssl->get_internals(connssl, info);
1243 }
1244
multissl_close(struct Curl_easy * data,struct connectdata * conn,int sockindex)1245 static void multissl_close(struct Curl_easy *data, struct connectdata *conn,
1246 int sockindex)
1247 {
1248 if(multissl_setup(NULL))
1249 return;
1250 Curl_ssl->close_one(data, conn, sockindex);
1251 }
1252
1253 static const struct Curl_ssl Curl_ssl_multi = {
1254 { CURLSSLBACKEND_NONE, "multi" }, /* info */
1255 0, /* supports nothing */
1256 (size_t)-1, /* something insanely large to be on the safe side */
1257
1258 multissl_init, /* init */
1259 Curl_none_cleanup, /* cleanup */
1260 multissl_version, /* version */
1261 Curl_none_check_cxn, /* check_cxn */
1262 Curl_none_shutdown, /* shutdown */
1263 Curl_none_data_pending, /* data_pending */
1264 Curl_none_random, /* random */
1265 Curl_none_cert_status_request, /* cert_status_request */
1266 multissl_connect, /* connect */
1267 multissl_connect_nonblocking, /* connect_nonblocking */
1268 multissl_getsock, /* getsock */
1269 multissl_get_internals, /* get_internals */
1270 multissl_close, /* close_one */
1271 Curl_none_close_all, /* close_all */
1272 Curl_none_session_free, /* session_free */
1273 Curl_none_set_engine, /* set_engine */
1274 Curl_none_set_engine_default, /* set_engine_default */
1275 Curl_none_engines_list, /* engines_list */
1276 Curl_none_false_start, /* false_start */
1277 NULL, /* sha256sum */
1278 NULL, /* associate_connection */
1279 NULL /* disassociate_connection */
1280 };
1281
1282 const struct Curl_ssl *Curl_ssl =
1283 #if defined(CURL_WITH_MULTI_SSL)
1284 &Curl_ssl_multi;
1285 #elif defined(USE_WOLFSSL)
1286 &Curl_ssl_wolfssl;
1287 #elif defined(USE_SECTRANSP)
1288 &Curl_ssl_sectransp;
1289 #elif defined(USE_GNUTLS)
1290 &Curl_ssl_gnutls;
1291 #elif defined(USE_GSKIT)
1292 &Curl_ssl_gskit;
1293 #elif defined(USE_MBEDTLS)
1294 &Curl_ssl_mbedtls;
1295 #elif defined(USE_NSS)
1296 &Curl_ssl_nss;
1297 #elif defined(USE_RUSTLS)
1298 &Curl_ssl_rustls;
1299 #elif defined(USE_OPENSSL)
1300 &Curl_ssl_openssl;
1301 #elif defined(USE_SCHANNEL)
1302 &Curl_ssl_schannel;
1303 #elif defined(USE_MESALINK)
1304 &Curl_ssl_mesalink;
1305 #elif defined(USE_BEARSSL)
1306 &Curl_ssl_bearssl;
1307 #else
1308 #error "Missing struct Curl_ssl for selected SSL backend"
1309 #endif
1310
1311 static const struct Curl_ssl *available_backends[] = {
1312 #if defined(USE_WOLFSSL)
1313 &Curl_ssl_wolfssl,
1314 #endif
1315 #if defined(USE_SECTRANSP)
1316 &Curl_ssl_sectransp,
1317 #endif
1318 #if defined(USE_GNUTLS)
1319 &Curl_ssl_gnutls,
1320 #endif
1321 #if defined(USE_GSKIT)
1322 &Curl_ssl_gskit,
1323 #endif
1324 #if defined(USE_MBEDTLS)
1325 &Curl_ssl_mbedtls,
1326 #endif
1327 #if defined(USE_NSS)
1328 &Curl_ssl_nss,
1329 #endif
1330 #if defined(USE_OPENSSL)
1331 &Curl_ssl_openssl,
1332 #endif
1333 #if defined(USE_SCHANNEL)
1334 &Curl_ssl_schannel,
1335 #endif
1336 #if defined(USE_MESALINK)
1337 &Curl_ssl_mesalink,
1338 #endif
1339 #if defined(USE_BEARSSL)
1340 &Curl_ssl_bearssl,
1341 #endif
1342 #if defined(USE_RUSTLS)
1343 &Curl_ssl_rustls,
1344 #endif
1345 NULL
1346 };
1347
multissl_version(char * buffer,size_t size)1348 static size_t multissl_version(char *buffer, size_t size)
1349 {
1350 static const struct Curl_ssl *selected;
1351 static char backends[200];
1352 static size_t backends_len;
1353 const struct Curl_ssl *current;
1354
1355 current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl;
1356
1357 if(current != selected) {
1358 char *p = backends;
1359 char *end = backends + sizeof(backends);
1360 int i;
1361
1362 selected = current;
1363
1364 backends[0] = '\0';
1365
1366 for(i = 0; available_backends[i]; ++i) {
1367 char vb[200];
1368 bool paren = (selected != available_backends[i]);
1369
1370 if(available_backends[i]->version(vb, sizeof(vb))) {
1371 p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""),
1372 (paren ? "(" : ""), vb, (paren ? ")" : ""));
1373 }
1374 }
1375
1376 backends_len = p - backends;
1377 }
1378
1379 if(!size)
1380 return 0;
1381
1382 if(size <= backends_len) {
1383 strncpy(buffer, backends, size - 1);
1384 buffer[size - 1] = '\0';
1385 return size - 1;
1386 }
1387
1388 strcpy(buffer, backends);
1389 return backends_len;
1390 }
1391
multissl_setup(const struct Curl_ssl * backend)1392 static int multissl_setup(const struct Curl_ssl *backend)
1393 {
1394 const char *env;
1395 char *env_tmp;
1396
1397 if(Curl_ssl != &Curl_ssl_multi)
1398 return 1;
1399
1400 if(backend) {
1401 Curl_ssl = backend;
1402 return 0;
1403 }
1404
1405 if(!available_backends[0])
1406 return 1;
1407
1408 env = env_tmp = curl_getenv("CURL_SSL_BACKEND");
1409 #ifdef CURL_DEFAULT_SSL_BACKEND
1410 if(!env)
1411 env = CURL_DEFAULT_SSL_BACKEND;
1412 #endif
1413 if(env) {
1414 int i;
1415 for(i = 0; available_backends[i]; i++) {
1416 if(strcasecompare(env, available_backends[i]->info.name)) {
1417 Curl_ssl = available_backends[i];
1418 free(env_tmp);
1419 return 0;
1420 }
1421 }
1422 }
1423
1424 /* Fall back to first available backend */
1425 Curl_ssl = available_backends[0];
1426 free(env_tmp);
1427 return 0;
1428 }
1429
curl_global_sslset(curl_sslbackend id,const char * name,const curl_ssl_backend *** avail)1430 CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
1431 const curl_ssl_backend ***avail)
1432 {
1433 int i;
1434
1435 if(avail)
1436 *avail = (const curl_ssl_backend **)&available_backends;
1437
1438 if(Curl_ssl != &Curl_ssl_multi)
1439 return id == Curl_ssl->info.id ||
1440 (name && strcasecompare(name, Curl_ssl->info.name)) ?
1441 CURLSSLSET_OK :
1442 #if defined(CURL_WITH_MULTI_SSL)
1443 CURLSSLSET_TOO_LATE;
1444 #else
1445 CURLSSLSET_UNKNOWN_BACKEND;
1446 #endif
1447
1448 for(i = 0; available_backends[i]; i++) {
1449 if(available_backends[i]->info.id == id ||
1450 (name && strcasecompare(available_backends[i]->info.name, name))) {
1451 multissl_setup(available_backends[i]);
1452 return CURLSSLSET_OK;
1453 }
1454 }
1455
1456 return CURLSSLSET_UNKNOWN_BACKEND;
1457 }
1458
1459 #else /* USE_SSL */
curl_global_sslset(curl_sslbackend id,const char * name,const curl_ssl_backend *** avail)1460 CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
1461 const curl_ssl_backend ***avail)
1462 {
1463 (void)id;
1464 (void)name;
1465 (void)avail;
1466 return CURLSSLSET_NO_BACKENDS;
1467 }
1468
1469 #endif /* !USE_SSL */
1470