| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364 |
- /***************************************************************************
- * _ _ ____ _
- * Project ___| | | | _ \| |
- * / __| | | | |_) | |
- * | (__| |_| | _ <| |___
- * \___|\___/|_| \_\_____|
- *
- * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
- *
- * This software is licensed as described in the file COPYING, which
- * you should have received as part of this distribution. The terms
- * are also available at https://curl.se/docs/copyright.html.
- *
- * You may opt to use, copy, modify, merge, publish, distribute and/or sell
- * copies of the Software, and permit persons to whom the Software is
- * furnished to do so, under the terms of the COPYING file.
- *
- * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
- * KIND, either express or implied.
- *
- * SPDX-License-Identifier: curl
- *
- ***************************************************************************/
- /*
- * Source file for all GnuTLS-specific code for the TLS/SSL layer. No code
- * but vtls.c should ever call or use these functions.
- *
- * Note: do not use the GnuTLS' *_t variable type names in this source code,
- * since they were not present in 1.0.X.
- */
- #include "../curl_setup.h"
- #ifdef USE_GNUTLS
- #include <gnutls/abstract.h>
- #include <gnutls/gnutls.h>
- #include <gnutls/x509.h>
- #include <gnutls/crypto.h>
- #include <nettle/sha2.h>
- #include "../urldata.h"
- #include "../sendf.h"
- #include "../curlx/inet_pton.h"
- #include "keylog.h"
- #include "gtls.h"
- #include "vtls.h"
- #include "vtls_int.h"
- #include "vtls_scache.h"
- #include "apple.h"
- #include "../vauth/vauth.h"
- #include "../parsedate.h"
- #include "../connect.h" /* for the connect timeout */
- #include "../progress.h"
- #include "../select.h"
- #include "../strdup.h"
- #include "../curlx/fopen.h"
- #include "../curlx/warnless.h"
- #include "x509asn1.h"
- #include "../multiif.h"
- #include "../curl_memory.h"
- /* The last #include file should be: */
- #include "../memdebug.h"
- /* Enable GnuTLS debugging by defining GTLSDEBUG */
- /*#define GTLSDEBUG */
- #ifdef GTLSDEBUG
- static void tls_log_func(int level, const char *str)
- {
- curl_mfprintf(stderr, "|<%d>| %s", level, str);
- }
- #endif
- static bool gtls_inited = FALSE;
- #if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x03010a)
- #error "too old GnuTLS version"
- #endif
- #undef CURL_GNUTLS_EARLY_DATA
- #if GNUTLS_VERSION_NUMBER >= 0x03060d
- #define CURL_GNUTLS_EARLY_DATA
- #endif
- #include <gnutls/ocsp.h>
- struct gtls_ssl_backend_data {
- struct gtls_ctx gtls;
- };
- static ssize_t gtls_push(void *s, const void *buf, size_t blen)
- {
- struct Curl_cfilter *cf = s;
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- struct Curl_easy *data = CF_DATA_CURRENT(cf);
- size_t nwritten;
- CURLcode result;
- DEBUGASSERT(data);
- result = Curl_conn_cf_send(cf->next, data, buf, blen, FALSE, &nwritten);
- CURL_TRC_CF(data, cf, "gtls_push(len=%zu) -> %d, %zu",
- blen, result, nwritten);
- backend->gtls.io_result = result;
- if(result) {
- /* !checksrc! disable ERRNOVAR 1 */
- gnutls_transport_set_errno(backend->gtls.session,
- (CURLE_AGAIN == result) ? EAGAIN : EINVAL);
- return -1;
- }
- return (ssize_t)nwritten;
- }
- static ssize_t gtls_pull(void *s, void *buf, size_t blen)
- {
- struct Curl_cfilter *cf = s;
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- struct Curl_easy *data = CF_DATA_CURRENT(cf);
- size_t nread;
- CURLcode result;
- DEBUGASSERT(data);
- if(!backend->gtls.shared_creds->trust_setup) {
- result = Curl_gtls_client_trust_setup(cf, data, &backend->gtls);
- if(result) {
- /* !checksrc! disable ERRNOVAR 1 */
- gnutls_transport_set_errno(backend->gtls.session, EINVAL);
- backend->gtls.io_result = result;
- return -1;
- }
- }
- result = Curl_conn_cf_recv(cf->next, data, buf, blen, &nread);
- CURL_TRC_CF(data, cf, "glts_pull(len=%zu) -> %d, %zd",
- blen, result, nread);
- backend->gtls.io_result = result;
- if(result) {
- /* !checksrc! disable ERRNOVAR 1 */
- gnutls_transport_set_errno(backend->gtls.session,
- (CURLE_AGAIN == result) ? EAGAIN : EINVAL);
- return -1;
- }
- else if(nread == 0)
- connssl->peer_closed = TRUE;
- return (ssize_t)nread;
- }
- /* gtls_init()
- *
- * Global GnuTLS init, called from Curl_ssl_init(). This calls functions that
- * are not thread-safe and thus this function itself is not thread-safe and
- * must only be called from within curl_global_init() to keep the thread
- * situation under control!
- */
- static int gtls_init(void)
- {
- int ret = 1;
- if(!gtls_inited) {
- ret = gnutls_global_init() ? 0 : 1;
- #ifdef GTLSDEBUG
- gnutls_global_set_log_function(tls_log_func);
- gnutls_global_set_log_level(2);
- #endif
- gtls_inited = TRUE;
- }
- return ret;
- }
- static void gtls_cleanup(void)
- {
- if(gtls_inited) {
- gnutls_global_deinit();
- gtls_inited = FALSE;
- }
- }
- #ifndef CURL_DISABLE_VERBOSE_STRINGS
- static void showtime(struct Curl_easy *data,
- const char *text,
- time_t stamp)
- {
- struct tm buffer;
- const struct tm *tm = &buffer;
- char str[96];
- CURLcode result = Curl_gmtime(stamp, &buffer);
- if(result)
- return;
- curl_msnprintf(str,
- sizeof(str),
- " %s: %s, %02d %s %4d %02d:%02d:%02d GMT",
- text,
- Curl_wkday[tm->tm_wday ? tm->tm_wday-1 : 6],
- tm->tm_mday,
- Curl_month[tm->tm_mon],
- tm->tm_year + 1900,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec);
- infof(data, "%s", str);
- }
- #endif
- static gnutls_datum_t load_file(const char *file)
- {
- FILE *f;
- gnutls_datum_t loaded_file = { NULL, 0 };
- long filelen;
- void *ptr;
- f = curlx_fopen(file, "rb");
- if(!f)
- return loaded_file;
- if(fseek(f, 0, SEEK_END) != 0
- || (filelen = ftell(f)) < 0
- || fseek(f, 0, SEEK_SET) != 0
- || !(ptr = malloc((size_t)filelen)))
- goto out;
- if(fread(ptr, 1, (size_t)filelen, f) < (size_t)filelen) {
- free(ptr);
- goto out;
- }
- loaded_file.data = ptr;
- loaded_file.size = (unsigned int)filelen;
- out:
- curlx_fclose(f);
- return loaded_file;
- }
- static void unload_file(gnutls_datum_t data)
- {
- free(data.data);
- }
- /* this function does an SSL/TLS (re-)handshake */
- static CURLcode cf_gtls_handshake(struct Curl_cfilter *cf,
- struct Curl_easy *data)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- gnutls_session_t session;
- int rc;
- DEBUGASSERT(backend);
- session = backend->gtls.session;
- connssl->io_need = CURL_SSL_IO_NEED_NONE;
- backend->gtls.io_result = CURLE_OK;
- rc = gnutls_handshake(session);
- if(!backend->gtls.shared_creds->trust_setup) {
- /* After having send off the ClientHello, we prepare the trust
- * store to verify the coming certificate from the server */
- CURLcode result = Curl_gtls_client_trust_setup(cf, data, &backend->gtls);
- if(result)
- return result;
- }
- if((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED)) {
- connssl->io_need =
- gnutls_record_get_direction(session) ?
- CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
- return CURLE_AGAIN;
- }
- else if((rc < 0) && !gnutls_error_is_fatal(rc)) {
- const char *strerr = NULL;
- if(rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
- gnutls_alert_description_t alert = gnutls_alert_get(session);
- strerr = gnutls_alert_get_name(alert);
- }
- if(!strerr)
- strerr = gnutls_strerror(rc);
- infof(data, "gnutls_handshake() warning: %s", strerr);
- return CURLE_AGAIN;
- }
- else if((rc < 0) && backend->gtls.io_result) {
- return backend->gtls.io_result;
- }
- else if(rc < 0) {
- const char *strerr = NULL;
- if(rc == GNUTLS_E_FATAL_ALERT_RECEIVED) {
- gnutls_alert_description_t alert = gnutls_alert_get(session);
- strerr = gnutls_alert_get_name(alert);
- }
- if(!strerr)
- strerr = gnutls_strerror(rc);
- failf(data, "GnuTLS, handshake failed: %s", strerr);
- return CURLE_SSL_CONNECT_ERROR;
- }
- return CURLE_OK;
- }
- static gnutls_x509_crt_fmt_t gnutls_do_file_type(const char *type)
- {
- if(!type || !type[0])
- return GNUTLS_X509_FMT_PEM;
- if(curl_strequal(type, "PEM"))
- return GNUTLS_X509_FMT_PEM;
- if(curl_strequal(type, "DER"))
- return GNUTLS_X509_FMT_DER;
- return GNUTLS_X509_FMT_PEM; /* default to PEM */
- }
- #define GNUTLS_CIPHERS "NORMAL:-ARCFOUR-128:-CTYPE-ALL:+CTYPE-X509"
- /* If GnuTLS was compiled without support for SRP it will error out if SRP is
- requested in the priority string, so treat it specially
- */
- #define GNUTLS_SRP "+SRP"
- #define QUIC_PRIORITY \
- "NORMAL:-VERS-ALL:+VERS-TLS1.3:-CIPHER-ALL:+AES-128-GCM:+AES-256-GCM:" \
- "+CHACHA20-POLY1305:+AES-128-CCM:-GROUP-ALL:+GROUP-SECP256R1:" \
- "+GROUP-X25519:+GROUP-SECP384R1:+GROUP-SECP521R1:" \
- "%DISABLE_TLS13_COMPAT_MODE"
- static CURLcode
- gnutls_set_ssl_version_min_max(struct Curl_easy *data,
- struct ssl_peer *peer,
- struct ssl_primary_config *conn_config,
- const char **prioritylist,
- bool tls13support)
- {
- long ssl_version = conn_config->version;
- long ssl_version_max = conn_config->version_max;
- if((ssl_version == CURL_SSLVERSION_DEFAULT) ||
- (ssl_version == CURL_SSLVERSION_TLSv1))
- ssl_version = CURL_SSLVERSION_TLSv1_0;
- if((ssl_version_max == CURL_SSLVERSION_MAX_NONE) ||
- (ssl_version_max == CURL_SSLVERSION_MAX_DEFAULT))
- ssl_version_max = tls13support ?
- CURL_SSLVERSION_MAX_TLSv1_3 : CURL_SSLVERSION_MAX_TLSv1_2;
- if(peer->transport == TRNSPRT_QUIC) {
- if(ssl_version_max < CURL_SSLVERSION_MAX_TLSv1_3) {
- failf(data, "QUIC needs at least TLS version 1.3");
- return CURLE_SSL_CONNECT_ERROR;
- }
- *prioritylist = QUIC_PRIORITY;
- return CURLE_OK;
- }
- switch(ssl_version | ssl_version_max) {
- case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_0:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.0";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_1:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.1:+VERS-TLS1.0";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_2:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.2:+VERS-TLS1.1:+VERS-TLS1.0";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_1:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.1";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_2:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.2:+VERS-TLS1.1";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_2:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.2";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_3 | CURL_SSLVERSION_MAX_TLSv1_3:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.3";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_3:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_3:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.1";
- return CURLE_OK;
- case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_3:
- *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
- "+VERS-TLS1.3:+VERS-TLS1.2";
- return CURLE_OK;
- }
- failf(data, "GnuTLS: cannot set ssl protocol");
- return CURLE_SSL_CONNECT_ERROR;
- }
- CURLcode Curl_gtls_shared_creds_create(struct Curl_easy *data,
- struct gtls_shared_creds **pcreds)
- {
- struct gtls_shared_creds *shared;
- int rc;
- *pcreds = NULL;
- shared = calloc(1, sizeof(*shared));
- if(!shared)
- return CURLE_OUT_OF_MEMORY;
- rc = gnutls_certificate_allocate_credentials(&shared->creds);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_cert_all_cred() failed: %s", gnutls_strerror(rc));
- free(shared);
- return CURLE_SSL_CONNECT_ERROR;
- }
- shared->refcount = 1;
- shared->time = curlx_now();
- *pcreds = shared;
- return CURLE_OK;
- }
- CURLcode Curl_gtls_shared_creds_up_ref(struct gtls_shared_creds *creds)
- {
- DEBUGASSERT(creds);
- if(creds->refcount < SIZE_MAX) {
- ++creds->refcount;
- return CURLE_OK;
- }
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- void Curl_gtls_shared_creds_free(struct gtls_shared_creds **pcreds)
- {
- struct gtls_shared_creds *shared = *pcreds;
- *pcreds = NULL;
- if(shared) {
- --shared->refcount;
- if(!shared->refcount) {
- gnutls_certificate_free_credentials(shared->creds);
- free(shared->CAfile);
- free(shared);
- }
- }
- }
- static CURLcode gtls_populate_creds(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- gnutls_certificate_credentials_t creds)
- {
- struct ssl_primary_config *config = Curl_ssl_cf_get_primary_config(cf);
- struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
- bool creds_are_empty = TRUE;
- int rc;
- if(!config->verifypeer) {
- infof(data, "SSL Trust: peer verification disabled");
- return CURLE_OK;
- }
- infof(data, "SSL Trust Anchors:");
- if(ssl_config->native_ca_store) {
- #ifdef USE_APPLE_SECTRUST
- infof(data, " Native: Apple SecTrust");
- creds_are_empty = FALSE;
- #else
- rc = gnutls_certificate_set_x509_system_trust(creds);
- if(rc < 0)
- infof(data, "error reading native ca store (%s), continuing anyway",
- gnutls_strerror(rc));
- else {
- infof(data, " Native: %d certificates from system trust", rc);
- if(rc > 0)
- creds_are_empty = FALSE;
- }
- #endif
- }
- if(config->CAfile) {
- /* set the trusted CA cert bundle file */
- gnutls_certificate_set_verify_flags(creds,
- GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
- rc = gnutls_certificate_set_x509_trust_file(creds,
- config->CAfile,
- GNUTLS_X509_FMT_PEM);
- creds_are_empty = creds_are_empty && (rc <= 0);
- if(rc < 0) {
- infof(data, "error reading ca cert file %s (%s)%s",
- config->CAfile, gnutls_strerror(rc),
- (creds_are_empty ? "" : ", continuing anyway"));
- if(creds_are_empty) {
- ssl_config->certverifyresult = rc;
- return CURLE_SSL_CACERT_BADFILE;
- }
- }
- else
- infof(data, " CAfile: %d certificates in %s", rc, config->CAfile);
- }
- if(config->CApath) {
- /* set the trusted CA cert directory */
- rc = gnutls_certificate_set_x509_trust_dir(creds, config->CApath,
- GNUTLS_X509_FMT_PEM);
- creds_are_empty = creds_are_empty && (rc <= 0);
- if(rc < 0) {
- infof(data, "error reading ca cert file %s (%s)%s",
- config->CApath, gnutls_strerror(rc),
- (creds_are_empty ? "" : ", continuing anyway"));
- if(creds_are_empty) {
- ssl_config->certverifyresult = rc;
- return CURLE_SSL_CACERT_BADFILE;
- }
- }
- else
- infof(data, " CApath: %d certificates in %s", rc, config->CApath);
- }
- if(creds_are_empty)
- infof(data, " no trust anchors configured");
- if(config->CRLfile) {
- /* set the CRL list file */
- rc = gnutls_certificate_set_x509_crl_file(creds, config->CRLfile,
- GNUTLS_X509_FMT_PEM);
- if(rc < 0) {
- failf(data, "error reading crl file %s (%s)",
- config->CRLfile, gnutls_strerror(rc));
- return CURLE_SSL_CRL_BADFILE;
- }
- else
- infof(data, " CRLfile: %d CRL in %s", rc, config->CRLfile);
- }
- return CURLE_OK;
- }
- /* key to use at `multi->proto_hash` */
- #define MPROTO_GTLS_X509_KEY "tls:gtls:x509:share"
- static bool gtls_shared_creds_expired(const struct Curl_easy *data,
- const struct gtls_shared_creds *sc)
- {
- const struct ssl_general_config *cfg = &data->set.general_ssl;
- struct curltime now = curlx_now();
- timediff_t elapsed_ms = curlx_timediff(now, sc->time);
- timediff_t timeout_ms = cfg->ca_cache_timeout * (timediff_t)1000;
- if(timeout_ms < 0)
- return FALSE;
- return elapsed_ms >= timeout_ms;
- }
- static bool gtls_shared_creds_different(struct Curl_cfilter *cf,
- const struct gtls_shared_creds *sc)
- {
- struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
- if(!sc->CAfile || !conn_config->CAfile)
- return sc->CAfile != conn_config->CAfile;
- return strcmp(sc->CAfile, conn_config->CAfile);
- }
- static struct gtls_shared_creds*
- gtls_get_cached_creds(struct Curl_cfilter *cf, struct Curl_easy *data)
- {
- struct gtls_shared_creds *shared_creds;
- if(data->multi) {
- shared_creds = Curl_hash_pick(&data->multi->proto_hash,
- CURL_UNCONST(MPROTO_GTLS_X509_KEY),
- sizeof(MPROTO_GTLS_X509_KEY)-1);
- if(shared_creds && shared_creds->creds &&
- !gtls_shared_creds_expired(data, shared_creds) &&
- !gtls_shared_creds_different(cf, shared_creds)) {
- return shared_creds;
- }
- }
- return NULL;
- }
- static void gtls_shared_creds_hash_free(void *key, size_t key_len, void *p)
- {
- struct gtls_shared_creds *sc = p;
- DEBUGASSERT(key_len == (sizeof(MPROTO_GTLS_X509_KEY)-1));
- DEBUGASSERT(!memcmp(MPROTO_GTLS_X509_KEY, key, key_len));
- (void)key;
- (void)key_len;
- Curl_gtls_shared_creds_free(&sc); /* down reference */
- }
- static void gtls_set_cached_creds(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct gtls_shared_creds *sc)
- {
- struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
- DEBUGASSERT(sc);
- DEBUGASSERT(sc->creds);
- DEBUGASSERT(!sc->CAfile);
- DEBUGASSERT(sc->refcount == 1);
- if(!data->multi)
- return;
- if(conn_config->CAfile) {
- sc->CAfile = strdup(conn_config->CAfile);
- if(!sc->CAfile)
- return;
- }
- if(Curl_gtls_shared_creds_up_ref(sc))
- return;
- if(!Curl_hash_add2(&data->multi->proto_hash,
- CURL_UNCONST(MPROTO_GTLS_X509_KEY),
- sizeof(MPROTO_GTLS_X509_KEY)-1,
- sc, gtls_shared_creds_hash_free)) {
- Curl_gtls_shared_creds_free(&sc); /* down reference again */
- return;
- }
- }
- CURLcode Curl_gtls_client_trust_setup(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct gtls_ctx *gtls)
- {
- struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
- struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
- struct gtls_shared_creds *cached_creds = NULL;
- bool cache_criteria_met;
- CURLcode result;
- int rc;
- /* Consider the X509 store cacheable if it comes exclusively from a CAfile,
- or no source is provided and we are falling back to OpenSSL's built-in
- default. */
- cache_criteria_met = (data->set.general_ssl.ca_cache_timeout != 0) &&
- conn_config->verifypeer &&
- !conn_config->CApath &&
- !conn_config->ca_info_blob &&
- !ssl_config->primary.CRLfile &&
- !ssl_config->native_ca_store &&
- !conn_config->clientcert; /* GnuTLS adds client cert to its credentials! */
- if(cache_criteria_met)
- cached_creds = gtls_get_cached_creds(cf, data);
- if(cached_creds && !Curl_gtls_shared_creds_up_ref(cached_creds)) {
- CURL_TRC_CF(data, cf, "using shared trust anchors and CRLs");
- Curl_gtls_shared_creds_free(>ls->shared_creds);
- gtls->shared_creds = cached_creds;
- rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_CERTIFICATE,
- gtls->shared_creds->creds);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- else {
- CURL_TRC_CF(data, cf, "loading trust anchors and CRLs");
- result = gtls_populate_creds(cf, data, gtls->shared_creds->creds);
- if(result)
- return result;
- gtls->shared_creds->trust_setup = TRUE;
- if(cache_criteria_met)
- gtls_set_cached_creds(cf, data, gtls->shared_creds);
- }
- return CURLE_OK;
- }
- #ifdef CURL_GNUTLS_EARLY_DATA
- CURLcode Curl_gtls_cache_session(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- const char *ssl_peer_key,
- gnutls_session_t session,
- curl_off_t valid_until,
- const char *alpn,
- unsigned char *quic_tp,
- size_t quic_tp_len)
- {
- struct Curl_ssl_session *sc_session;
- unsigned char *sdata, *qtp_clone = NULL;
- size_t sdata_len = 0;
- size_t earlydata_max = 0;
- CURLcode result = CURLE_OK;
- if(!Curl_ssl_scache_use(cf, data))
- return CURLE_OK;
- /* we always unconditionally get the session id here, as even if we
- already got it from the cache and asked to use it in the connection, it
- might've been rejected and then a new one is in use now and we need to
- detect that. */
- /* get the session ID data size */
- gnutls_session_get_data(session, NULL, &sdata_len);
- if(!sdata_len) /* gnutls does this for some version combinations */
- return CURLE_OK;
- sdata = malloc(sdata_len); /* get a buffer for it */
- if(!sdata)
- return CURLE_OUT_OF_MEMORY;
- /* extract session ID to the allocated buffer */
- gnutls_session_get_data(session, sdata, &sdata_len);
- earlydata_max = gnutls_record_get_max_early_data_size(session);
- CURL_TRC_CF(data, cf, "get session id (len=%zu, alpn=%s, earlymax=%zu) "
- "and store in cache", sdata_len, alpn ? alpn : "-",
- earlydata_max);
- if(quic_tp && quic_tp_len) {
- qtp_clone = Curl_memdup0((char *)quic_tp, quic_tp_len);
- if(!qtp_clone) {
- free(sdata);
- return CURLE_OUT_OF_MEMORY;
- }
- }
- result = Curl_ssl_session_create2(sdata, sdata_len,
- Curl_glts_get_ietf_proto(session),
- alpn, valid_until, earlydata_max,
- qtp_clone, quic_tp_len,
- &sc_session);
- /* call took ownership of `sdata` and `qtp_clone` */
- if(!result) {
- result = Curl_ssl_scache_put(cf, data, ssl_peer_key, sc_session);
- /* took ownership of `sc_session` */
- }
- return result;
- }
- #endif
- int Curl_glts_get_ietf_proto(gnutls_session_t session)
- {
- switch(gnutls_protocol_get_version(session)) {
- case GNUTLS_SSL3:
- return CURL_IETF_PROTO_SSL3;
- case GNUTLS_TLS1_0:
- return CURL_IETF_PROTO_TLS1;
- case GNUTLS_TLS1_1:
- return CURL_IETF_PROTO_TLS1_1;
- case GNUTLS_TLS1_2:
- return CURL_IETF_PROTO_TLS1_2;
- #if GNUTLS_VERSION_NUMBER >= 0x030603
- case GNUTLS_TLS1_3:
- return CURL_IETF_PROTO_TLS1_3;
- #endif
- default:
- return CURL_IETF_PROTO_UNKNOWN;
- }
- }
- #ifdef CURL_GNUTLS_EARLY_DATA
- static CURLcode cf_gtls_update_session_id(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- gnutls_session_t session)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- return Curl_gtls_cache_session(cf, data, connssl->peer.scache_key,
- session, 0, connssl->negotiated.alpn,
- NULL, 0);
- }
- static int gtls_handshake_cb(gnutls_session_t session, unsigned int htype,
- unsigned when, unsigned int incoming,
- const gnutls_datum_t *msg)
- {
- struct Curl_cfilter *cf = gnutls_session_get_ptr(session);
- (void)msg;
- (void)incoming;
- if(when) { /* after message has been processed */
- struct Curl_easy *data = CF_DATA_CURRENT(cf);
- if(data) {
- CURL_TRC_CF(data, cf, "handshake: %s message type %d",
- incoming ? "incoming" : "outgoing", htype);
- switch(htype) {
- case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET: {
- cf_gtls_update_session_id(cf, data, session);
- break;
- }
- default:
- break;
- }
- }
- }
- return 0;
- }
- #endif
- static CURLcode gtls_set_priority(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct gtls_ctx *gtls,
- const char *priority)
- {
- struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
- struct dynbuf buf;
- const char *err = NULL;
- CURLcode result = CURLE_OK;
- int rc;
- curlx_dyn_init(&buf, 4096);
- #ifdef USE_GNUTLS_SRP
- if(conn_config->username) {
- /* Only add SRP to the cipher list if SRP is requested. Otherwise
- * GnuTLS will disable TLS 1.3 support. */
- result = curlx_dyn_add(&buf, priority);
- if(!result)
- result = curlx_dyn_add(&buf, ":" GNUTLS_SRP);
- if(result)
- goto out;
- priority = curlx_dyn_ptr(&buf);
- }
- #endif
- if(conn_config->cipher_list) {
- if((conn_config->cipher_list[0] == '+') ||
- (conn_config->cipher_list[0] == '-') ||
- (conn_config->cipher_list[0] == '!')) {
- /* add it to out own */
- if(!curlx_dyn_len(&buf)) { /* not added yet */
- result = curlx_dyn_add(&buf, priority);
- if(result)
- goto out;
- }
- result = curlx_dyn_addf(&buf, ":%s", conn_config->cipher_list);
- if(result)
- goto out;
- priority = curlx_dyn_ptr(&buf);
- }
- else /* replace our own completely */
- priority = conn_config->cipher_list;
- }
- infof(data, "GnuTLS priority: %s", priority);
- rc = gnutls_priority_set_direct(gtls->session, priority, &err);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "Error %d setting GnuTLS priority: %s", rc, err);
- result = CURLE_SSL_CONNECT_ERROR;
- }
- out:
- curlx_dyn_free(&buf);
- return result;
- }
- static CURLcode gtls_client_init(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct ssl_peer *peer,
- size_t earlydata_max,
- struct gtls_ctx *gtls)
- {
- struct ssl_primary_config *config = Curl_ssl_cf_get_primary_config(cf);
- struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
- unsigned int init_flags;
- int rc;
- bool sni = TRUE; /* default is SNI enabled */
- const char *prioritylist;
- bool tls13support;
- CURLcode result;
- if(!gtls_inited)
- gtls_init();
- if(config->version == CURL_SSLVERSION_SSLv2) {
- failf(data, "GnuTLS does not support SSLv2");
- return CURLE_SSL_CONNECT_ERROR;
- }
- else if(config->version == CURL_SSLVERSION_SSLv3)
- sni = FALSE; /* SSLv3 has no SNI */
- /* allocate a shared creds struct */
- result = Curl_gtls_shared_creds_create(data, >ls->shared_creds);
- if(result)
- return result;
- #ifdef USE_GNUTLS_SRP
- if(config->username && Curl_auth_allowed_to_host(data)) {
- infof(data, "Using TLS-SRP username: %s", config->username);
- rc = gnutls_srp_allocate_client_credentials(>ls->srp_client_cred);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_srp_allocate_client_cred() failed: %s",
- gnutls_strerror(rc));
- return CURLE_OUT_OF_MEMORY;
- }
- rc = gnutls_srp_set_client_credentials(gtls->srp_client_cred,
- config->username,
- config->password);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_srp_set_client_cred() failed: %s",
- gnutls_strerror(rc));
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- }
- #endif
- ssl_config->certverifyresult = 0;
- /* Initialize TLS session as a client */
- init_flags = GNUTLS_CLIENT;
- #ifdef CURL_GNUTLS_EARLY_DATA
- if(peer->transport == TRNSPRT_QUIC && earlydata_max > 0)
- init_flags |= GNUTLS_ENABLE_EARLY_DATA | GNUTLS_NO_END_OF_EARLY_DATA;
- else if(earlydata_max > 0 && earlydata_max != 0xFFFFFFFFUL)
- /* See https://gitlab.com/gnutls/gnutls/-/issues/1619
- * We cannot differentiate between a session announcing no earldata
- * and one announcing 0xFFFFFFFFUL. On TCP+TLS, this is unlikely, but
- * on QUIC this is common. */
- init_flags |= GNUTLS_ENABLE_EARLY_DATA;
- #endif
- #ifdef GNUTLS_FORCE_CLIENT_CERT
- init_flags |= GNUTLS_FORCE_CLIENT_CERT;
- #endif
- #ifdef GNUTLS_NO_TICKETS_TLS12
- init_flags |= GNUTLS_NO_TICKETS_TLS12;
- #endif
- #ifdef GNUTLS_NO_STATUS_REQUEST
- if(!config->verifystatus)
- /* Disable the "status_request" TLS extension, enabled by default since
- GnuTLS 3.8.0. */
- init_flags |= GNUTLS_NO_STATUS_REQUEST;
- #endif
- CURL_TRC_CF(data, cf, "gnutls_init(flags=%x), earlydata=%zu",
- init_flags, earlydata_max);
- rc = gnutls_init(>ls->session, init_flags);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_init() failed: %d", rc);
- return CURLE_SSL_CONNECT_ERROR;
- }
- if(sni && peer->sni) {
- if(gnutls_server_name_set(gtls->session, GNUTLS_NAME_DNS,
- peer->sni, strlen(peer->sni)) < 0) {
- failf(data, "Failed to set SNI");
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- /* Use default priorities */
- rc = gnutls_set_default_priority(gtls->session);
- if(rc != GNUTLS_E_SUCCESS)
- return CURLE_SSL_CONNECT_ERROR;
- /* "In GnuTLS 3.6.5, TLS 1.3 is enabled by default" */
- tls13support = !!gnutls_check_version("3.6.5");
- /* Ensure +SRP comes at the *end* of all relevant strings so that it can be
- * removed if a runtime error indicates that SRP is not supported by this
- * GnuTLS version */
- if(config->version == CURL_SSLVERSION_SSLv2 ||
- config->version == CURL_SSLVERSION_SSLv3) {
- failf(data, "GnuTLS does not support SSLv2 or SSLv3");
- return CURLE_SSL_CONNECT_ERROR;
- }
- if(config->version == CURL_SSLVERSION_TLSv1_3) {
- if(!tls13support) {
- failf(data, "This GnuTLS installation does not support TLS 1.3");
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- /* At this point we know we have a supported TLS version, so set it */
- result = gnutls_set_ssl_version_min_max(data, peer,
- config, &prioritylist, tls13support);
- if(result)
- return result;
- result = gtls_set_priority(cf, data, gtls, prioritylist);
- if(result)
- return result;
- if(config->clientcert) {
- if(!gtls->shared_creds->trust_setup) {
- result = Curl_gtls_client_trust_setup(cf, data, gtls);
- if(result)
- return result;
- }
- if(ssl_config->cert_type && curl_strequal(ssl_config->cert_type, "P12")) {
- rc = gnutls_certificate_set_x509_simple_pkcs12_file(
- gtls->shared_creds->creds, config->clientcert, GNUTLS_X509_FMT_DER,
- ssl_config->key_passwd ? ssl_config->key_passwd : "");
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data,
- "error reading X.509 potentially-encrypted key or certificate "
- "file: %s",
- gnutls_strerror(rc));
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- else {
- const unsigned int supported_key_encryption_algorithms =
- GNUTLS_PKCS_USE_PKCS12_3DES | GNUTLS_PKCS_USE_PKCS12_ARCFOUR |
- GNUTLS_PKCS_USE_PKCS12_RC2_40 | GNUTLS_PKCS_USE_PBES2_3DES |
- GNUTLS_PKCS_USE_PBES2_AES_128 | GNUTLS_PKCS_USE_PBES2_AES_192 |
- GNUTLS_PKCS_USE_PBES2_AES_256;
- rc = gnutls_certificate_set_x509_key_file2(
- gtls->shared_creds->creds,
- config->clientcert,
- ssl_config->key ? ssl_config->key : config->clientcert,
- gnutls_do_file_type(ssl_config->cert_type),
- ssl_config->key_passwd,
- supported_key_encryption_algorithms);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data,
- "error reading X.509 %skey file: %s",
- ssl_config->key_passwd ? "potentially-encrypted " : "",
- gnutls_strerror(rc));
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- }
- #ifdef USE_GNUTLS_SRP
- /* put the credentials to the current session */
- if(config->username) {
- rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_SRP,
- gtls->srp_client_cred);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- else
- #endif
- {
- rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_CERTIFICATE,
- gtls->shared_creds->creds);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- if(config->verifystatus) {
- rc = gnutls_ocsp_status_request_enable_client(gtls->session,
- NULL, 0, NULL);
- if(rc != GNUTLS_E_SUCCESS) {
- failf(data, "gnutls_ocsp_status_request_enable_client() failed: %d", rc);
- return CURLE_SSL_CONNECT_ERROR;
- }
- }
- return CURLE_OK;
- }
- #ifdef CURL_GNUTLS_EARLY_DATA
- static int keylog_callback(gnutls_session_t session, const char *label,
- const gnutls_datum_t *secret)
- {
- gnutls_datum_t crandom;
- gnutls_datum_t srandom;
- gnutls_session_get_random(session, &crandom, &srandom);
- if(crandom.size != 32) {
- return -1;
- }
- Curl_tls_keylog_write(label, crandom.data, secret->data, secret->size);
- return 0;
- }
- static CURLcode gtls_on_session_reuse(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct alpn_spec *alpns,
- struct Curl_ssl_session *scs,
- bool *do_early_data)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- CURLcode result = CURLE_OK;
- *do_early_data = FALSE;
- connssl->earlydata_max =
- gnutls_record_get_max_early_data_size(backend->gtls.session);
- if((!connssl->earlydata_max || connssl->earlydata_max == 0xFFFFFFFFUL)) {
- /* Seems to be no GnuTLS way to signal no EarlyData in session */
- CURL_TRC_CF(data, cf, "SSL session does not allow earlydata");
- }
- else if(!Curl_alpn_contains_proto(alpns, scs->alpn)) {
- CURL_TRC_CF(data, cf, "SSL session has different ALPN, no early data");
- }
- else {
- infof(data, "SSL session allows %zu bytes of early data, "
- "reusing ALPN '%s'", connssl->earlydata_max, scs->alpn);
- connssl->earlydata_state = ssl_earlydata_await;
- connssl->state = ssl_connection_deferred;
- result = Curl_alpn_set_negotiated(cf, data, connssl,
- (const unsigned char *)scs->alpn,
- scs->alpn ? strlen(scs->alpn) : 0);
- *do_early_data = !result;
- }
- return result;
- }
- #endif
- CURLcode Curl_gtls_ctx_init(struct gtls_ctx *gctx,
- struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct ssl_peer *peer,
- const struct alpn_spec *alpns_requested,
- Curl_gtls_ctx_setup_cb *cb_setup,
- void *cb_user_data,
- void *ssl_user_data,
- Curl_gtls_init_session_reuse_cb *sess_reuse_cb)
- {
- struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
- struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
- struct Curl_ssl_session *scs = NULL;
- gnutls_datum_t gtls_alpns[ALPN_ENTRIES_MAX];
- size_t gtls_alpns_count = 0;
- bool gtls_session_setup = FALSE;
- struct alpn_spec alpns;
- CURLcode result = CURLE_OK;
- int rc;
- DEBUGASSERT(gctx);
- Curl_alpn_copy(&alpns, alpns_requested);
- /* This might be a reconnect, so we check for a session ID in the cache
- to speed up things. We need to do this before constructing the gnutls
- session since we need to set flags depending on the kind of reuse. */
- if(conn_config->cache_session) {
- result = Curl_ssl_scache_take(cf, data, peer->scache_key, &scs);
- if(result)
- goto out;
- if(scs && scs->sdata && scs->sdata_len &&
- (!scs->alpn || Curl_alpn_contains_proto(&alpns, scs->alpn))) {
- /* we got a cached session, use it! */
- result = gtls_client_init(cf, data, peer, scs->earlydata_max, gctx);
- if(result)
- goto out;
- gtls_session_setup = TRUE;
- rc = gnutls_session_set_data(gctx->session, scs->sdata, scs->sdata_len);
- if(rc < 0)
- infof(data, "SSL session not accepted by GnuTLS, continuing without");
- else {
- infof(data, "SSL reusing session with ALPN '%s'",
- scs->alpn ? scs->alpn : "-");
- if(ssl_config->earlydata && scs->alpn && !cf->conn->connect_only) {
- bool do_early_data = FALSE;
- if(sess_reuse_cb) {
- result = sess_reuse_cb(cf, data, &alpns, scs, &do_early_data);
- if(result)
- goto out;
- }
- if(do_early_data) {
- /* We only try the ALPN protocol the session used before,
- * otherwise we might send early data for the wrong protocol */
- Curl_alpn_restrict_to(&alpns, scs->alpn);
- }
- }
- }
- }
- }
- if(!gtls_session_setup) {
- result = gtls_client_init(cf, data, peer, 0, gctx);
- if(result)
- goto out;
- }
- gnutls_session_set_ptr(gctx->session, ssl_user_data);
- if(cb_setup) {
- result = cb_setup(cf, data, cb_user_data);
- if(result)
- goto out;
- }
- #ifdef CURL_GNUTLS_EARLY_DATA
- /* Open the file if a TLS or QUIC backend has not done this before. */
- Curl_tls_keylog_open();
- if(Curl_tls_keylog_enabled()) {
- gnutls_session_set_keylog_function(gctx->session, keylog_callback);
- }
- #endif
- /* convert the ALPN string from our arguments to a list of strings that
- * gnutls wants and will convert internally back to this string for sending
- * to the server. nice. */
- if(!gtls_alpns_count && alpns.count) {
- size_t i;
- DEBUGASSERT(CURL_ARRAYSIZE(gtls_alpns) >= alpns.count);
- for(i = 0; i < alpns.count; ++i) {
- gtls_alpns[i].data = (unsigned char *)alpns.entries[i];
- gtls_alpns[i].size = (unsigned int)strlen(alpns.entries[i]);
- }
- gtls_alpns_count = alpns.count;
- }
- if(gtls_alpns_count &&
- gnutls_alpn_set_protocols(gctx->session,
- gtls_alpns, (unsigned int)gtls_alpns_count,
- GNUTLS_ALPN_MANDATORY)) {
- failf(data, "failed setting ALPN");
- result = CURLE_SSL_CONNECT_ERROR;
- }
- out:
- Curl_ssl_scache_return(cf, data, peer->scache_key, scs);
- return result;
- }
- static CURLcode
- gtls_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- CURLcode result;
- DEBUGASSERT(backend);
- if(connssl->state == ssl_connection_complete)
- /* to make us tolerant against being called more than once for the
- same connection */
- return CURLE_OK;
- result = Curl_gtls_ctx_init(&backend->gtls, cf, data, &connssl->peer,
- connssl->alpn, NULL, NULL, cf,
- #ifdef CURL_GNUTLS_EARLY_DATA
- gtls_on_session_reuse
- #else
- NULL
- #endif
- );
- if(result)
- return result;
- if(connssl->alpn && (connssl->state != ssl_connection_deferred)) {
- struct alpn_proto_buf proto;
- memset(&proto, 0, sizeof(proto));
- Curl_alpn_to_proto_str(&proto, connssl->alpn);
- infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
- }
- #ifdef CURL_GNUTLS_EARLY_DATA
- gnutls_handshake_set_hook_function(backend->gtls.session,
- GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST,
- gtls_handshake_cb);
- #endif
- /* register callback functions and handle to send and receive data. */
- gnutls_transport_set_ptr(backend->gtls.session, cf);
- gnutls_transport_set_push_function(backend->gtls.session, gtls_push);
- gnutls_transport_set_pull_function(backend->gtls.session, gtls_pull);
- return CURLE_OK;
- }
- static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
- gnutls_x509_crt_t cert,
- const char *pinnedpubkey)
- {
- /* Scratch */
- size_t len1 = 0, len2 = 0;
- unsigned char *buff1 = NULL;
- gnutls_pubkey_t key = NULL;
- /* Result is returned to caller */
- CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
- /* if a path was not specified, do not pin */
- if(!pinnedpubkey)
- return CURLE_OK;
- if(!cert)
- return result;
- do {
- int ret;
- /* Begin Gyrations to get the public key */
- ret = gnutls_pubkey_init(&key);
- if(ret < 0)
- break; /* failed */
- ret = gnutls_pubkey_import_x509(key, cert, 0);
- if(ret < 0)
- break; /* failed */
- ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, NULL, &len1);
- if(ret != GNUTLS_E_SHORT_MEMORY_BUFFER || len1 == 0)
- break; /* failed */
- buff1 = malloc(len1);
- if(!buff1)
- break; /* failed */
- len2 = len1;
- ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, buff1, &len2);
- if(ret < 0 || len1 != len2)
- break; /* failed */
- /* End Gyrations */
- /* The one good exit point */
- result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
- } while(0);
- if(key)
- gnutls_pubkey_deinit(key);
- Curl_safefree(buff1);
- return result;
- }
- void Curl_gtls_report_handshake(struct Curl_easy *data,
- struct gtls_ctx *gctx)
- {
- #ifndef CURL_DISABLE_VERBOSE_STRINGS
- if(Curl_trc_is_verbose(data)) {
- const char *ptr;
- gnutls_protocol_t version = gnutls_protocol_get_version(gctx->session);
- /* the name of the cipher suite used, e.g. ECDHE_RSA_AES_256_GCM_SHA384. */
- ptr = gnutls_cipher_suite_get_name(gnutls_kx_get(gctx->session),
- gnutls_cipher_get(gctx->session),
- gnutls_mac_get(gctx->session));
- infof(data, "SSL connection using %s / %s",
- gnutls_protocol_get_name(version), ptr);
- }
- #else
- (void)data;
- (void)gctx;
- #endif
- }
- static void gtls_msg_verify_result(struct Curl_easy *data,
- struct ssl_peer *peer,
- gnutls_x509_crt_t x509_cert,
- bool was_verified,
- bool needs_verified)
- {
- char certname[65] = ""; /* limited to 64 chars by ASN.1 */
- size_t size = sizeof(certname);
- int rc;
- rc = gnutls_x509_crt_get_dn_by_oid(x509_cert, GNUTLS_OID_X520_COMMON_NAME,
- 0, /* the first and only one */
- FALSE, certname, &size);
- if(rc) {
- infof(data, "error fetching CN from cert:%s", gnutls_strerror(rc));
- certname[0] = 0;
- }
- if(!was_verified) {
- if(needs_verified) {
- failf(data, "SSL: certificate subject name (%s) does not match "
- "target hostname '%s'", certname, peer->dispname);
- }
- else
- infof(data, " common name: %s (does not match '%s')",
- certname, peer->dispname);
- }
- else
- infof(data, " common name: %s (matched)", certname);
- }
- static void gtls_infof_cert(struct Curl_easy *data,
- gnutls_x509_crt_t x509_cert)
- {
- #ifndef CURL_DISABLE_VERBOSE_STRINGS
- if(Curl_trc_is_verbose(data)) {
- gnutls_datum_t certfields;
- int rc, algo;
- time_t tstamp;
- unsigned int bits;
- /* public key algorithm's parameters */
- algo = gnutls_x509_crt_get_pk_algorithm(x509_cert, &bits);
- infof(data, " certificate public key: %s",
- gnutls_pk_algorithm_get_name((gnutls_pk_algorithm_t)algo));
- /* version of the X.509 certificate. */
- infof(data, " certificate version: #%d",
- gnutls_x509_crt_get_version(x509_cert));
- rc = gnutls_x509_crt_get_dn2(x509_cert, &certfields);
- if(rc)
- infof(data, "Failed to get certificate name");
- else {
- infof(data, " subject: %s", certfields.data);
- tstamp = gnutls_x509_crt_get_activation_time(x509_cert);
- showtime(data, "start date", tstamp);
- tstamp = gnutls_x509_crt_get_expiration_time(x509_cert);
- showtime(data, "expire date", tstamp);
- gnutls_free(certfields.data);
- }
- rc = gnutls_x509_crt_get_issuer_dn2(x509_cert, &certfields);
- if(rc)
- infof(data, "Failed to get certificate issuer");
- else {
- infof(data, " issuer: %s", certfields.data);
- gnutls_free(certfields.data);
- }
- }
- #else
- (void)data;
- (void)x509_cert;
- #endif
- }
- static CURLcode gtls_verify_ocsp_status(struct Curl_easy *data,
- gnutls_session_t session)
- {
- gnutls_ocsp_resp_t ocsp_resp = NULL;
- gnutls_datum_t status_request;
- gnutls_ocsp_cert_status_t status;
- gnutls_x509_crl_reason_t reason;
- CURLcode result = CURLE_OK;
- int rc;
- rc = gnutls_ocsp_status_request_get(session, &status_request);
- if(rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
- failf(data, "No OCSP response received");
- result = CURLE_SSL_INVALIDCERTSTATUS;
- goto out;
- }
- else if(rc < 0) {
- failf(data, "Invalid OCSP response received");
- result = CURLE_SSL_INVALIDCERTSTATUS;
- goto out;
- }
- gnutls_ocsp_resp_init(&ocsp_resp);
- rc = gnutls_ocsp_resp_import(ocsp_resp, &status_request);
- if(rc < 0) {
- failf(data, "Invalid OCSP response received");
- result = CURLE_SSL_INVALIDCERTSTATUS;
- goto out;
- }
- (void)gnutls_ocsp_resp_get_single(ocsp_resp, 0, NULL, NULL, NULL, NULL,
- &status, NULL, NULL, NULL, &reason);
- switch(status) {
- case GNUTLS_OCSP_CERT_GOOD:
- break;
- case GNUTLS_OCSP_CERT_REVOKED: {
- const char *crl_reason;
- switch(reason) {
- default:
- case GNUTLS_X509_CRLREASON_UNSPECIFIED:
- crl_reason = "unspecified reason";
- break;
- case GNUTLS_X509_CRLREASON_KEYCOMPROMISE:
- crl_reason = "private key compromised";
- break;
- case GNUTLS_X509_CRLREASON_CACOMPROMISE:
- crl_reason = "CA compromised";
- break;
- case GNUTLS_X509_CRLREASON_AFFILIATIONCHANGED:
- crl_reason = "affiliation has changed";
- break;
- case GNUTLS_X509_CRLREASON_SUPERSEDED:
- crl_reason = "certificate superseded";
- break;
- case GNUTLS_X509_CRLREASON_CESSATIONOFOPERATION:
- crl_reason = "operation has ceased";
- break;
- case GNUTLS_X509_CRLREASON_CERTIFICATEHOLD:
- crl_reason = "certificate is on hold";
- break;
- case GNUTLS_X509_CRLREASON_REMOVEFROMCRL:
- crl_reason = "will be removed from delta CRL";
- break;
- case GNUTLS_X509_CRLREASON_PRIVILEGEWITHDRAWN:
- crl_reason = "privilege withdrawn";
- break;
- case GNUTLS_X509_CRLREASON_AACOMPROMISE:
- crl_reason = "AA compromised";
- break;
- }
- failf(data, "Server certificate was revoked: %s", crl_reason);
- break;
- }
- default:
- case GNUTLS_OCSP_CERT_UNKNOWN:
- failf(data, "Server certificate status is unknown");
- break;
- }
- result = (status != GNUTLS_OCSP_CERT_GOOD) ?
- CURLE_SSL_INVALIDCERTSTATUS : CURLE_OK;
- out:
- if(ocsp_resp)
- gnutls_ocsp_resp_deinit(ocsp_resp);
- return result;
- }
- struct gtls_cert_chain {
- const gnutls_datum_t *certs;
- unsigned int num_certs;
- };
- #ifdef USE_APPLE_SECTRUST
- static CURLcode gtls_chain_get_der(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- void *user_data,
- size_t i,
- unsigned char **pder,
- size_t *pder_len)
- {
- struct gtls_cert_chain *chain = user_data;
- (void)cf;
- (void)data;
- *pder_len = 0;
- *pder = NULL;
- if(i >= chain->num_certs)
- return CURLE_TOO_LARGE;
- *pder = chain->certs[i].data;
- *pder_len = (size_t)chain->certs[i].size;
- return CURLE_OK;
- }
- static CURLcode glts_apple_verify(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- struct ssl_peer *peer,
- struct gtls_cert_chain *chain,
- bool *pverified)
- {
- CURLcode result;
- result = Curl_vtls_apple_verify(cf, data, peer, chain->num_certs,
- gtls_chain_get_der, chain,
- NULL, 0);
- *pverified = !result;
- if(*pverified)
- infof(data, " SSL certificate verified by Apple SecTrust.");
- return result;
- }
- #endif /* USE_APPLE_SECTRUST */
- CURLcode
- Curl_gtls_verifyserver(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- gnutls_session_t session,
- struct ssl_primary_config *config,
- struct ssl_config_data *ssl_config,
- struct ssl_peer *peer,
- const char *pinned_key)
- {
- struct gtls_cert_chain chain;
- gnutls_x509_crt_t x509_cert = NULL, x509_issuer = NULL;
- time_t certclock;
- int rc;
- CURLcode result = CURLE_OK;
- long * const certverifyresult = &ssl_config->certverifyresult;
- (void)cf;
- /* This function will return the peer's raw certificate (chain) as sent by
- the peer. These certificates are in raw format (DER encoded for
- X.509). In case of a X.509 then a certificate list may be present. The
- first certificate in the list is the peer's certificate, following the
- issuer's certificate, then the issuer's issuer etc. */
- chain.certs = gnutls_certificate_get_peers(session, &chain.num_certs);
- if(!chain.certs) {
- if(config->verifypeer ||
- config->verifyhost ||
- config->issuercert) {
- #ifdef USE_GNUTLS_SRP
- if(ssl_config->primary.username && !config->verifypeer &&
- gnutls_cipher_get(session)) {
- /* no peer cert, but auth is ok if we have SRP user and cipher and no
- peer verify */
- }
- else {
- #endif
- failf(data, "failed to get server cert");
- *certverifyresult = GNUTLS_E_NO_CERTIFICATE_FOUND;
- result = CURLE_PEER_FAILED_VERIFICATION;
- goto out;
- #ifdef USE_GNUTLS_SRP
- }
- #endif
- }
- infof(data, " common name: WARNING could not obtain");
- }
- if(data->set.ssl.certinfo && chain.certs) {
- unsigned int i;
- result = Curl_ssl_init_certinfo(data, (int)chain.num_certs);
- if(result)
- goto out;
- for(i = 0; i < chain.num_certs; i++) {
- const char *beg = (const char *) chain.certs[i].data;
- const char *end = beg + chain.certs[i].size;
- result = Curl_extract_certinfo(data, (int)i, beg, end);
- if(result)
- goto out;
- }
- }
- if(config->verifypeer) {
- bool verified = FALSE;
- unsigned int verify_status = 0;
- /* This function will try to verify the peer's certificate and return
- its status (trusted, invalid etc.). The value of status should be
- one or more of the gnutls_certificate_status_t enumerated elements
- bitwise or'd. To avoid denial of service attacks some default
- upper limits regarding the certificate key size and chain size
- are set. To override them use
- gnutls_certificate_set_verify_limits(). */
- rc = gnutls_certificate_verify_peers2(session, &verify_status);
- if(rc < 0) {
- failf(data, "server cert verify failed: %d", rc);
- *certverifyresult = rc;
- result = CURLE_SSL_CONNECT_ERROR;
- goto out;
- }
- *certverifyresult = verify_status;
- verified = !(verify_status & GNUTLS_CERT_INVALID);
- if(verified)
- infof(data, " SSL certificate verified by GnuTLS");
- #ifdef USE_APPLE_SECTRUST
- if(!verified && ssl_config->native_ca_store &&
- (verify_status & GNUTLS_CERT_SIGNER_NOT_FOUND)) {
- result = glts_apple_verify(cf, data, peer, &chain, &verified);
- if(result && (result != CURLE_PEER_FAILED_VERIFICATION))
- goto out; /* unexpected error */
- if(verified) {
- infof(data, "SSL certificate verified via Apple SecTrust.");
- *certverifyresult = 0;
- }
- }
- #endif
- if(!verified) {
- /* verify_status is a bitmask of gnutls_certificate_status bits */
- const char *cause = "certificate error, no details available";
- if(verify_status & GNUTLS_CERT_EXPIRED)
- cause = "certificate has expired";
- else if(verify_status & GNUTLS_CERT_SIGNER_NOT_FOUND)
- cause = "certificate signer not trusted";
- else if(verify_status & GNUTLS_CERT_INSECURE_ALGORITHM)
- cause = "certificate uses insecure algorithm";
- else if(verify_status & GNUTLS_CERT_INVALID_OCSP_STATUS)
- cause = "attached OCSP status response is invalid";
- failf(data, "SSL certificate verification failed: %s. (CAfile: %s "
- "CRLfile: %s)", cause,
- config->CAfile ? config->CAfile : "none",
- ssl_config->primary.CRLfile ?
- ssl_config->primary.CRLfile : "none");
- result = CURLE_PEER_FAILED_VERIFICATION;
- goto out;
- }
- }
- else
- infof(data, " SSL certificate verification SKIPPED");
- /* initialize an X.509 certificate structure. */
- if(gnutls_x509_crt_init(&x509_cert)) {
- failf(data, "failed to init gnutls x509_crt");
- *certverifyresult = GNUTLS_E_NO_CERTIFICATE_FOUND;
- result = CURLE_SSL_CONNECT_ERROR;
- goto out;
- }
- if(chain.certs) {
- /* convert the given DER or PEM encoded Certificate to the native
- gnutls_x509_crt_t format */
- rc = gnutls_x509_crt_import(x509_cert, chain.certs, GNUTLS_X509_FMT_DER);
- if(rc) {
- failf(data, "error parsing server's certificate chain");
- *certverifyresult = GNUTLS_E_NO_CERTIFICATE_FOUND;
- result = CURLE_SSL_CONNECT_ERROR;
- goto out;
- }
- }
- /* Check for time-based validity */
- certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
- if(certclock == (time_t)-1) {
- if(config->verifypeer) {
- failf(data, "server cert expiration date verify failed");
- *certverifyresult = GNUTLS_CERT_EXPIRED;
- result = CURLE_SSL_CONNECT_ERROR;
- goto out;
- }
- else
- infof(data, " SSL certificate expiration date verify FAILED");
- }
- else {
- if(certclock < time(NULL)) {
- if(config->verifypeer) {
- failf(data, "server certificate expiration date has passed.");
- *certverifyresult = GNUTLS_CERT_EXPIRED;
- result = CURLE_PEER_FAILED_VERIFICATION;
- goto out;
- }
- else
- infof(data, " SSL certificate expiration date FAILED");
- }
- else
- infof(data, " SSL certificate expiration date OK");
- }
- certclock = gnutls_x509_crt_get_activation_time(x509_cert);
- if(certclock == (time_t)-1) {
- if(config->verifypeer) {
- failf(data, "server cert activation date verify failed");
- *certverifyresult = GNUTLS_CERT_NOT_ACTIVATED;
- result = CURLE_SSL_CONNECT_ERROR;
- goto out;
- }
- else
- infof(data, " SSL certificate activation date verify FAILED");
- }
- else {
- if(certclock > time(NULL)) {
- if(config->verifypeer) {
- failf(data, "server certificate not activated yet.");
- *certverifyresult = GNUTLS_CERT_NOT_ACTIVATED;
- result = CURLE_PEER_FAILED_VERIFICATION;
- goto out;
- }
- else
- infof(data, " SSL certificate activation date FAILED");
- }
- else
- infof(data, " SSL certificate activation date OK");
- }
- if(config->verifystatus) {
- result = gtls_verify_ocsp_status(data, session);
- if(result)
- goto out;
- }
- else
- infof(data, " SSL certificate status verification SKIPPED");
- if(config->issuercert) {
- gnutls_datum_t issuerp;
- if(gnutls_x509_crt_init(&x509_issuer)) {
- failf(data, "failed to init gnutls x509_crt for issuer");
- result = CURLE_SSL_ISSUER_ERROR;
- goto out;
- }
- issuerp = load_file(config->issuercert);
- rc = gnutls_x509_crt_import(x509_issuer, &issuerp, GNUTLS_X509_FMT_PEM);
- if(!rc)
- rc = (int)gnutls_x509_crt_check_issuer(x509_cert, x509_issuer);
- unload_file(issuerp);
- if(rc <= 0) {
- failf(data, "server certificate issuer check failed (IssuerCert: %s)",
- config->issuercert ? config->issuercert : "none");
- result = CURLE_SSL_ISSUER_ERROR;
- goto out;
- }
- infof(data, " SSL certificate issuer check OK (Issuer Cert: %s)",
- config->issuercert ? config->issuercert : "none");
- }
- /* This function will check if the given certificate's subject matches the
- given hostname. This is a basic implementation of the matching described
- in RFC2818 (HTTPS), which takes into account wildcards, and the subject
- alternative name PKIX extension. Returns non zero on success, and zero on
- failure. */
- /* This function does not handle trailing dots, so if we have an SNI name
- use that and fallback to the hostname only if there is no SNI (like for
- IP addresses) */
- rc = (int)gnutls_x509_crt_check_hostname(x509_cert,
- peer->sni ? peer->sni :
- peer->hostname);
- #if GNUTLS_VERSION_NUMBER < 0x030306
- /* Before 3.3.6, gnutls_x509_crt_check_hostname() did not check IP
- addresses. */
- if(!rc) {
- #ifdef USE_IPV6
- #define use_addr in6_addr
- #else
- #define use_addr in_addr
- #endif
- unsigned char addrbuf[sizeof(struct use_addr)];
- size_t addrlen = 0;
- if(curlx_inet_pton(AF_INET, peer->hostname, addrbuf) > 0)
- addrlen = 4;
- #ifdef USE_IPV6
- else if(curlx_inet_pton(AF_INET6, peer->hostname, addrbuf) > 0)
- addrlen = 16;
- #endif
- if(addrlen) {
- unsigned char certaddr[sizeof(struct use_addr)];
- int i;
- for(i = 0; ; i++) {
- size_t certaddrlen = sizeof(certaddr);
- int ret = gnutls_x509_crt_get_subject_alt_name(x509_cert, i, certaddr,
- &certaddrlen, NULL);
- /* If this happens, it was not an IP address. */
- if(ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
- continue;
- if(ret < 0)
- break;
- if(ret != GNUTLS_SAN_IPADDRESS)
- continue;
- if(certaddrlen == addrlen && !memcmp(addrbuf, certaddr, addrlen)) {
- rc = 1;
- break;
- }
- }
- }
- }
- #endif
- result = (!rc && config->verifyhost) ?
- CURLE_PEER_FAILED_VERIFICATION : CURLE_OK;
- gtls_msg_verify_result(data, peer, x509_cert, rc, config->verifyhost);
- if(result)
- goto out;
- if(pinned_key) {
- result = pkp_pin_peer_pubkey(data, x509_cert, pinned_key);
- if(result != CURLE_OK) {
- failf(data, "SSL: public key does not match pinned public key");
- goto out;
- }
- }
- gtls_infof_cert(data, x509_cert);
- out:
- if(x509_issuer)
- gnutls_x509_crt_deinit(x509_issuer);
- if(x509_cert)
- gnutls_x509_crt_deinit(x509_cert);
- return result;
- }
- static CURLcode gtls_verifyserver(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- gnutls_session_t session)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
- struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
- #ifndef CURL_DISABLE_PROXY
- const char *pinned_key = Curl_ssl_cf_is_proxy(cf) ?
- data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
- data->set.str[STRING_SSL_PINNEDPUBLICKEY];
- #else
- const char *pinned_key = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
- #endif
- CURLcode result;
- result = Curl_gtls_verifyserver(cf, data, session, conn_config, ssl_config,
- &connssl->peer, pinned_key);
- if(result)
- goto out;
- #ifdef CURL_GNUTLS_EARLY_DATA
- /* Only on TLSv1.2 or lower do we have the session id now. For
- * TLSv1.3 we get it via a SESSION_TICKET message that arrives later. */
- if(gnutls_protocol_get_version(session) < GNUTLS_TLS1_3)
- result = cf_gtls_update_session_id(cf, data, session);
- #endif
- out:
- return result;
- }
- #ifdef CURL_GNUTLS_EARLY_DATA
- static CURLcode gtls_send_earlydata(struct Curl_cfilter *cf,
- struct Curl_easy *data)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- CURLcode result = CURLE_OK;
- const unsigned char *buf;
- size_t blen;
- ssize_t n;
- DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sending);
- backend->gtls.io_result = CURLE_OK;
- while(Curl_bufq_peek(&connssl->earlydata, &buf, &blen)) {
- n = gnutls_record_send_early_data(backend->gtls.session, buf, blen);
- CURL_TRC_CF(data, cf, "gtls_send_earlydata(len=%zu) -> %zd",
- blen, n);
- if(n < 0) {
- if(n == GNUTLS_E_AGAIN)
- result = CURLE_AGAIN;
- else
- result = backend->gtls.io_result ?
- backend->gtls.io_result : CURLE_SEND_ERROR;
- goto out;
- }
- else if(!n) {
- /* gnutls is buggy, it *SHOULD* return the amount of bytes it took in.
- * Instead it returns 0 if everything was written. */
- n = (ssize_t)blen;
- }
- Curl_bufq_skip(&connssl->earlydata, (size_t)n);
- }
- /* sent everything there was */
- infof(data, "SSL sending %zu bytes of early data", connssl->earlydata_skip);
- out:
- return result;
- }
- #endif
- /*
- * This function is called after the TCP connect has completed. Setup the TLS
- * layer and do all necessary magic.
- */
- /* We use connssl->connecting_state to keep track of the connection status;
- there are three states: 'ssl_connect_1' (not started yet or complete),
- 'ssl_connect_2' (doing handshake with the server), and
- 'ssl_connect_3' (verifying and getting stats).
- */
- static CURLcode gtls_connect_common(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- bool *done) {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- CURLcode result = CURLE_OK;
- DEBUGASSERT(backend);
- /* check if the connection has already been established */
- if(ssl_connection_complete == connssl->state) {
- *done = TRUE;
- return CURLE_OK;
- }
- *done = FALSE;
- /* Initiate the connection, if not already done */
- if(connssl->connecting_state == ssl_connect_1) {
- result = gtls_connect_step1(cf, data);
- if(result)
- goto out;
- connssl->connecting_state = ssl_connect_2;
- }
- if(connssl->connecting_state == ssl_connect_2) {
- #ifdef CURL_GNUTLS_EARLY_DATA
- if(connssl->earlydata_state == ssl_earlydata_await) {
- goto out;
- }
- else if(connssl->earlydata_state == ssl_earlydata_sending) {
- result = gtls_send_earlydata(cf, data);
- if(result)
- goto out;
- connssl->earlydata_state = ssl_earlydata_sent;
- }
- DEBUGASSERT((connssl->earlydata_state == ssl_earlydata_none) ||
- (connssl->earlydata_state == ssl_earlydata_sent));
- #endif
- result = cf_gtls_handshake(cf, data);
- if(result)
- goto out;
- connssl->connecting_state = ssl_connect_3;
- }
- /* Finish connecting once the handshake is done */
- if(connssl->connecting_state == ssl_connect_3) {
- gnutls_datum_t proto;
- int rc;
- Curl_gtls_report_handshake(data, &backend->gtls);
- result = gtls_verifyserver(cf, data, backend->gtls.session);
- if(result)
- goto out;
- connssl->state = ssl_connection_complete;
- rc = gnutls_alpn_get_selected_protocol(backend->gtls.session, &proto);
- if(rc) { /* No ALPN from server */
- proto.data = NULL;
- proto.size = 0;
- }
- result = Curl_alpn_set_negotiated(cf, data, connssl,
- proto.data, proto.size);
- if(result)
- goto out;
- #ifdef CURL_GNUTLS_EARLY_DATA
- if(connssl->earlydata_state > ssl_earlydata_none) {
- /* We should be in this state by now */
- DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sent);
- connssl->earlydata_state =
- (gnutls_session_get_flags(backend->gtls.session) &
- GNUTLS_SFLAGS_EARLY_DATA) ?
- ssl_earlydata_accepted : ssl_earlydata_rejected;
- }
- #endif
- connssl->connecting_state = ssl_connect_done;
- }
- if(connssl->connecting_state == ssl_connect_done)
- DEBUGASSERT(connssl->state == ssl_connection_complete);
- out:
- if(result == CURLE_AGAIN) {
- *done = FALSE;
- return CURLE_OK;
- }
- *done = ((connssl->state == ssl_connection_complete) ||
- (connssl->state == ssl_connection_deferred));
- CURL_TRC_CF(data, cf, "gtls_connect_common() -> %d, done=%d", result, *done);
- return result;
- }
- static CURLcode gtls_connect(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- bool *done)
- {
- #ifdef CURL_GNUTLS_EARLY_DATA
- struct ssl_connect_data *connssl = cf->ctx;
- if((connssl->state == ssl_connection_deferred) &&
- (connssl->earlydata_state == ssl_earlydata_await)) {
- /* We refuse to be pushed, we are waiting for someone to send/recv. */
- *done = TRUE;
- return CURLE_OK;
- }
- #endif
- return gtls_connect_common(cf, data, done);
- }
- static bool gtls_data_pending(struct Curl_cfilter *cf,
- const struct Curl_easy *data)
- {
- struct ssl_connect_data *ctx = cf->ctx;
- struct gtls_ssl_backend_data *backend;
- (void)data;
- DEBUGASSERT(ctx && ctx->backend);
- backend = (struct gtls_ssl_backend_data *)ctx->backend;
- if(backend->gtls.session &&
- gnutls_record_check_pending(backend->gtls.session) != 0)
- return TRUE;
- return FALSE;
- }
- static CURLcode gtls_send(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- const void *buf,
- size_t blen,
- size_t *pnwritten)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- CURLcode result = CURLE_OK;
- ssize_t nwritten;
- size_t remain = blen;
- (void)data;
- DEBUGASSERT(backend);
- *pnwritten = 0;
- while(remain) {
- backend->gtls.io_result = CURLE_OK;
- nwritten = gnutls_record_send(backend->gtls.session, buf, remain);
- if(nwritten >= 0) {
- *pnwritten += (size_t)nwritten;
- DEBUGASSERT((size_t)nwritten <= remain);
- buf = (char *)CURL_UNCONST(buf) + (size_t)nwritten;
- remain -= (size_t)nwritten;
- }
- else {
- if(*pnwritten && (nwritten == GNUTLS_E_AGAIN)) {
- result = CURLE_OK;
- goto out;
- }
- result = (nwritten == GNUTLS_E_AGAIN) ?
- CURLE_AGAIN :
- (backend->gtls.io_result ? backend->gtls.io_result : CURLE_SEND_ERROR);
- goto out;
- }
- }
- out:
- CURL_TRC_CF(data, cf, "gtls_send(len=%zu) -> %d, %zu",
- blen, result, *pnwritten);
- return result;
- }
- /*
- * This function is called to shut down the SSL layer but keep the
- * socket open (CCC - Clear Command Channel)
- */
- static CURLcode gtls_shutdown(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- bool send_shutdown, bool *done)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- char buf[1024];
- CURLcode result = CURLE_OK;
- ssize_t nread = 0;
- size_t i;
- DEBUGASSERT(backend);
- /* If we have no handshaked connection or already shut down */
- if(!backend->gtls.session || cf->shutdown ||
- connssl->state != ssl_connection_complete) {
- *done = TRUE;
- goto out;
- }
- connssl->io_need = CURL_SSL_IO_NEED_NONE;
- *done = FALSE;
- if(!backend->gtls.sent_shutdown) {
- /* do this only once */
- backend->gtls.sent_shutdown = TRUE;
- if(send_shutdown) {
- int ret = gnutls_bye(backend->gtls.session, GNUTLS_SHUT_RDWR);
- if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
- CURL_TRC_CF(data, cf, "SSL shutdown, gnutls_bye EAGAIN");
- connssl->io_need = gnutls_record_get_direction(backend->gtls.session) ?
- CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
- backend->gtls.sent_shutdown = FALSE;
- result = CURLE_OK;
- goto out;
- }
- if(ret != GNUTLS_E_SUCCESS) {
- CURL_TRC_CF(data, cf, "SSL shutdown, gnutls_bye error: '%s'(%d)",
- gnutls_strerror((int)ret), (int)ret);
- result = CURLE_RECV_ERROR;
- goto out;
- }
- }
- }
- /* SSL should now have started the shutdown from our side. Since it
- * was not complete, we are lacking the close notify from the server. */
- for(i = 0; i < 10; ++i) {
- nread = gnutls_record_recv(backend->gtls.session, buf, sizeof(buf));
- if(nread <= 0)
- break;
- }
- if(nread > 0) {
- /* still data coming in? */
- }
- else if(nread == 0) {
- /* We got the close notify alert and are done. */
- *done = TRUE;
- }
- else if((nread == GNUTLS_E_AGAIN) || (nread == GNUTLS_E_INTERRUPTED)) {
- connssl->io_need = gnutls_record_get_direction(backend->gtls.session) ?
- CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
- }
- else {
- CURL_TRC_CF(data, cf, "SSL shutdown, error: '%s'(%d)",
- gnutls_strerror((int)nread), (int)nread);
- result = CURLE_RECV_ERROR;
- }
- out:
- cf->shutdown = (result || *done);
- return result;
- }
- static void gtls_close(struct Curl_cfilter *cf,
- struct Curl_easy *data)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- (void)data;
- DEBUGASSERT(backend);
- CURL_TRC_CF(data, cf, "close");
- if(backend->gtls.session) {
- gnutls_deinit(backend->gtls.session);
- backend->gtls.session = NULL;
- }
- if(backend->gtls.shared_creds) {
- Curl_gtls_shared_creds_free(&backend->gtls.shared_creds);
- }
- #ifdef USE_GNUTLS_SRP
- if(backend->gtls.srp_client_cred) {
- gnutls_srp_free_client_credentials(backend->gtls.srp_client_cred);
- backend->gtls.srp_client_cred = NULL;
- }
- #endif
- }
- static CURLcode gtls_recv(struct Curl_cfilter *cf,
- struct Curl_easy *data,
- char *buf, size_t blen,
- size_t *pnread)
- {
- struct ssl_connect_data *connssl = cf->ctx;
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- CURLcode result = CURLE_OK;
- ssize_t nread;
- (void)data;
- DEBUGASSERT(backend);
- nread = gnutls_record_recv(backend->gtls.session, buf, blen);
- if(nread >= 0)
- *pnread = (size_t)nread;
- else {
- if((nread == GNUTLS_E_AGAIN) || (nread == GNUTLS_E_INTERRUPTED)) {
- result = CURLE_AGAIN;
- goto out;
- }
- else if(nread == GNUTLS_E_REHANDSHAKE) {
- /* Either TLSv1.2 renegotiate or a TLSv1.3 session key update. */
- result = cf_gtls_handshake(cf, data);
- if(!result)
- result = CURLE_AGAIN; /* make us get called again. */
- goto out;
- }
- else {
- failf(data, "GnuTLS recv error (%d): %s",
- (int)nread, gnutls_strerror((int)nread));
- result = backend->gtls.io_result ?
- backend->gtls.io_result : CURLE_RECV_ERROR;
- goto out;
- }
- }
- out:
- CURL_TRC_CF(data, cf, "gtls_recv(len=%zu) -> 0, %zd", blen, nread);
- return result;
- }
- size_t Curl_gtls_version(char *buffer, size_t size)
- {
- return curl_msnprintf(buffer, size, "GnuTLS/%s", gnutls_check_version(NULL));
- }
- /* data might be NULL! */
- static CURLcode gtls_random(struct Curl_easy *data,
- unsigned char *entropy, size_t length)
- {
- int rc;
- (void)data;
- rc = gnutls_rnd(GNUTLS_RND_RANDOM, entropy, length);
- return rc ? CURLE_FAILED_INIT : CURLE_OK;
- }
- static CURLcode gtls_sha256sum(const unsigned char *tmp, /* input */
- size_t tmplen,
- unsigned char *sha256sum, /* output */
- size_t sha256len)
- {
- struct sha256_ctx SHA256pw;
- sha256_init(&SHA256pw);
- sha256_update(&SHA256pw, (unsigned int)tmplen, tmp);
- sha256_digest(&SHA256pw, (unsigned int)sha256len, sha256sum);
- return CURLE_OK;
- }
- static bool gtls_cert_status_request(void)
- {
- return TRUE;
- }
- static void *gtls_get_internals(struct ssl_connect_data *connssl,
- CURLINFO info)
- {
- struct gtls_ssl_backend_data *backend =
- (struct gtls_ssl_backend_data *)connssl->backend;
- (void)info;
- DEBUGASSERT(backend);
- return backend->gtls.session;
- }
- const struct Curl_ssl Curl_ssl_gnutls = {
- { CURLSSLBACKEND_GNUTLS, "gnutls" }, /* info */
- SSLSUPP_CA_PATH |
- SSLSUPP_CERTINFO |
- SSLSUPP_PINNEDPUBKEY |
- SSLSUPP_HTTPS_PROXY |
- SSLSUPP_CIPHER_LIST |
- SSLSUPP_CA_CACHE,
- sizeof(struct gtls_ssl_backend_data),
- gtls_init, /* init */
- gtls_cleanup, /* cleanup */
- Curl_gtls_version, /* version */
- gtls_shutdown, /* shutdown */
- gtls_data_pending, /* data_pending */
- gtls_random, /* random */
- gtls_cert_status_request, /* cert_status_request */
- gtls_connect, /* connect */
- Curl_ssl_adjust_pollset, /* adjust_pollset */
- gtls_get_internals, /* get_internals */
- gtls_close, /* close_one */
- NULL, /* close_all */
- NULL, /* set_engine */
- NULL, /* set_engine_default */
- NULL, /* engines_list */
- gtls_sha256sum, /* sha256sum */
- gtls_recv, /* recv decrypted data */
- gtls_send, /* send data to encrypt */
- NULL, /* get_channel_binding */
- };
- #endif /* USE_GNUTLS */
|