VirtualBox

source: vbox/trunk/src/libs/curl-7.64.0/lib/vtls/vtls.c@ 86643

Last change on this file since 86643 was 85671, checked in by vboxsync, 4 years ago

Export out internal curl copy to make it a lot simpler to build VBox (OSE) on Windows. bugref:9814

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette