2
0

gtls.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. * Source file for all GnuTLS-specific code for the TLS/SSL layer. No code
  26. * but vtls.c should ever call or use these functions.
  27. *
  28. * Note: do not use the GnuTLS' *_t variable type names in this source code,
  29. * since they were not present in 1.0.X.
  30. */
  31. #include "curl_setup.h"
  32. #ifdef USE_GNUTLS
  33. #include <gnutls/abstract.h>
  34. #include <gnutls/gnutls.h>
  35. #include <gnutls/x509.h>
  36. #include <gnutls/crypto.h>
  37. #include <nettle/sha2.h>
  38. #include "urldata.h"
  39. #include "sendf.h"
  40. #include "inet_pton.h"
  41. #include "keylog.h"
  42. #include "gtls.h"
  43. #include "vtls.h"
  44. #include "vtls_int.h"
  45. #include "vauth/vauth.h"
  46. #include "parsedate.h"
  47. #include "connect.h" /* for the connect timeout */
  48. #include "progress.h"
  49. #include "select.h"
  50. #include "strcase.h"
  51. #include "warnless.h"
  52. #include "x509asn1.h"
  53. #include "multiif.h"
  54. #include "curl_printf.h"
  55. #include "curl_memory.h"
  56. /* The last #include file should be: */
  57. #include "memdebug.h"
  58. #ifndef ARRAYSIZE
  59. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  60. #endif
  61. #define QUIC_PRIORITY \
  62. "NORMAL:-VERS-ALL:+VERS-TLS1.3:-CIPHER-ALL:+AES-128-GCM:+AES-256-GCM:" \
  63. "+CHACHA20-POLY1305:+AES-128-CCM:-GROUP-ALL:+GROUP-SECP256R1:" \
  64. "+GROUP-X25519:+GROUP-SECP384R1:+GROUP-SECP521R1:" \
  65. "%DISABLE_TLS13_COMPAT_MODE"
  66. /* Enable GnuTLS debugging by defining GTLSDEBUG */
  67. /*#define GTLSDEBUG */
  68. #ifdef GTLSDEBUG
  69. static void tls_log_func(int level, const char *str)
  70. {
  71. fprintf(stderr, "|<%d>| %s", level, str);
  72. }
  73. #endif
  74. static bool gtls_inited = FALSE;
  75. #if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x03010a)
  76. #error "too old GnuTLS version"
  77. #endif
  78. # include <gnutls/ocsp.h>
  79. struct gtls_ssl_backend_data {
  80. struct gtls_ctx gtls;
  81. };
  82. static ssize_t gtls_push(void *s, const void *buf, size_t blen)
  83. {
  84. struct Curl_cfilter *cf = s;
  85. struct ssl_connect_data *connssl = cf->ctx;
  86. struct gtls_ssl_backend_data *backend =
  87. (struct gtls_ssl_backend_data *)connssl->backend;
  88. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  89. ssize_t nwritten;
  90. CURLcode result;
  91. DEBUGASSERT(data);
  92. nwritten = Curl_conn_cf_send(cf->next, data, buf, blen, FALSE, &result);
  93. CURL_TRC_CF(data, cf, "gtls_push(len=%zu) -> %zd, err=%d",
  94. blen, nwritten, result);
  95. backend->gtls.io_result = result;
  96. if(nwritten < 0) {
  97. gnutls_transport_set_errno(backend->gtls.session,
  98. (CURLE_AGAIN == result) ? EAGAIN : EINVAL);
  99. nwritten = -1;
  100. }
  101. return nwritten;
  102. }
  103. static ssize_t gtls_pull(void *s, void *buf, size_t blen)
  104. {
  105. struct Curl_cfilter *cf = s;
  106. struct ssl_connect_data *connssl = cf->ctx;
  107. struct gtls_ssl_backend_data *backend =
  108. (struct gtls_ssl_backend_data *)connssl->backend;
  109. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  110. ssize_t nread;
  111. CURLcode result;
  112. DEBUGASSERT(data);
  113. if(!backend->gtls.shared_creds->trust_setup) {
  114. result = Curl_gtls_client_trust_setup(cf, data, &backend->gtls);
  115. if(result) {
  116. gnutls_transport_set_errno(backend->gtls.session, EINVAL);
  117. backend->gtls.io_result = result;
  118. return -1;
  119. }
  120. }
  121. nread = Curl_conn_cf_recv(cf->next, data, buf, blen, &result);
  122. CURL_TRC_CF(data, cf, "glts_pull(len=%zu) -> %zd, err=%d",
  123. blen, nread, result);
  124. backend->gtls.io_result = result;
  125. if(nread < 0) {
  126. gnutls_transport_set_errno(backend->gtls.session,
  127. (CURLE_AGAIN == result) ? EAGAIN : EINVAL);
  128. nread = -1;
  129. }
  130. else if(nread == 0)
  131. connssl->peer_closed = TRUE;
  132. return nread;
  133. }
  134. /* gtls_init()
  135. *
  136. * Global GnuTLS init, called from Curl_ssl_init(). This calls functions that
  137. * are not thread-safe and thus this function itself is not thread-safe and
  138. * must only be called from within curl_global_init() to keep the thread
  139. * situation under control!
  140. */
  141. static int gtls_init(void)
  142. {
  143. int ret = 1;
  144. if(!gtls_inited) {
  145. ret = gnutls_global_init() ? 0 : 1;
  146. #ifdef GTLSDEBUG
  147. gnutls_global_set_log_function(tls_log_func);
  148. gnutls_global_set_log_level(2);
  149. #endif
  150. gtls_inited = TRUE;
  151. }
  152. return ret;
  153. }
  154. static void gtls_cleanup(void)
  155. {
  156. if(gtls_inited) {
  157. gnutls_global_deinit();
  158. gtls_inited = FALSE;
  159. }
  160. }
  161. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  162. static void showtime(struct Curl_easy *data,
  163. const char *text,
  164. time_t stamp)
  165. {
  166. struct tm buffer;
  167. const struct tm *tm = &buffer;
  168. char str[96];
  169. CURLcode result = Curl_gmtime(stamp, &buffer);
  170. if(result)
  171. return;
  172. msnprintf(str,
  173. sizeof(str),
  174. " %s: %s, %02d %s %4d %02d:%02d:%02d GMT",
  175. text,
  176. Curl_wkday[tm->tm_wday ? tm->tm_wday-1 : 6],
  177. tm->tm_mday,
  178. Curl_month[tm->tm_mon],
  179. tm->tm_year + 1900,
  180. tm->tm_hour,
  181. tm->tm_min,
  182. tm->tm_sec);
  183. infof(data, "%s", str);
  184. }
  185. #endif
  186. static gnutls_datum_t load_file(const char *file)
  187. {
  188. FILE *f;
  189. gnutls_datum_t loaded_file = { NULL, 0 };
  190. long filelen;
  191. void *ptr;
  192. f = fopen(file, "rb");
  193. if(!f)
  194. return loaded_file;
  195. if(fseek(f, 0, SEEK_END) != 0
  196. || (filelen = ftell(f)) < 0
  197. || fseek(f, 0, SEEK_SET) != 0
  198. || !(ptr = malloc((size_t)filelen)))
  199. goto out;
  200. if(fread(ptr, 1, (size_t)filelen, f) < (size_t)filelen) {
  201. free(ptr);
  202. goto out;
  203. }
  204. loaded_file.data = ptr;
  205. loaded_file.size = (unsigned int)filelen;
  206. out:
  207. fclose(f);
  208. return loaded_file;
  209. }
  210. static void unload_file(gnutls_datum_t data)
  211. {
  212. free(data.data);
  213. }
  214. /* this function does a SSL/TLS (re-)handshake */
  215. static CURLcode handshake(struct Curl_cfilter *cf,
  216. struct Curl_easy *data,
  217. bool duringconnect,
  218. bool nonblocking)
  219. {
  220. struct ssl_connect_data *connssl = cf->ctx;
  221. struct gtls_ssl_backend_data *backend =
  222. (struct gtls_ssl_backend_data *)connssl->backend;
  223. gnutls_session_t session;
  224. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  225. DEBUGASSERT(backend);
  226. session = backend->gtls.session;
  227. connssl->connecting_state = ssl_connect_2;
  228. for(;;) {
  229. timediff_t timeout_ms;
  230. int rc;
  231. /* check allowed time left */
  232. timeout_ms = Curl_timeleft(data, NULL, duringconnect);
  233. if(timeout_ms < 0) {
  234. /* no need to continue if time already is up */
  235. failf(data, "SSL connection timeout");
  236. return CURLE_OPERATION_TIMEDOUT;
  237. }
  238. /* if ssl is expecting something, check if it is available. */
  239. if(connssl->io_need) {
  240. int what;
  241. curl_socket_t writefd = (connssl->io_need & CURL_SSL_IO_NEED_SEND) ?
  242. sockfd : CURL_SOCKET_BAD;
  243. curl_socket_t readfd = (connssl->io_need & CURL_SSL_IO_NEED_RECV) ?
  244. sockfd : CURL_SOCKET_BAD;
  245. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  246. nonblocking ? 0 :
  247. timeout_ms ? timeout_ms : 1000);
  248. if(what < 0) {
  249. /* fatal error */
  250. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  251. return CURLE_SSL_CONNECT_ERROR;
  252. }
  253. else if(0 == what) {
  254. if(nonblocking)
  255. return CURLE_AGAIN;
  256. else if(timeout_ms) {
  257. /* timeout */
  258. failf(data, "SSL connection timeout at %ld", (long)timeout_ms);
  259. return CURLE_OPERATION_TIMEDOUT;
  260. }
  261. }
  262. /* socket is readable or writable */
  263. }
  264. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  265. backend->gtls.io_result = CURLE_OK;
  266. rc = gnutls_handshake(session);
  267. if(!backend->gtls.shared_creds->trust_setup) {
  268. /* After having send off the ClientHello, we prepare the trust
  269. * store to verify the coming certificate from the server */
  270. CURLcode result = Curl_gtls_client_trust_setup(cf, data, &backend->gtls);
  271. if(result)
  272. return result;
  273. }
  274. if((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED)) {
  275. connssl->io_need =
  276. gnutls_record_get_direction(session) ?
  277. CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
  278. continue;
  279. }
  280. else if((rc < 0) && !gnutls_error_is_fatal(rc)) {
  281. const char *strerr = NULL;
  282. if(rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
  283. gnutls_alert_description_t alert = gnutls_alert_get(session);
  284. strerr = gnutls_alert_get_name(alert);
  285. }
  286. if(!strerr)
  287. strerr = gnutls_strerror(rc);
  288. infof(data, "gnutls_handshake() warning: %s", strerr);
  289. continue;
  290. }
  291. else if((rc < 0) && backend->gtls.io_result) {
  292. return backend->gtls.io_result;
  293. }
  294. else if(rc < 0) {
  295. const char *strerr = NULL;
  296. if(rc == GNUTLS_E_FATAL_ALERT_RECEIVED) {
  297. gnutls_alert_description_t alert = gnutls_alert_get(session);
  298. strerr = gnutls_alert_get_name(alert);
  299. }
  300. if(!strerr)
  301. strerr = gnutls_strerror(rc);
  302. failf(data, "GnuTLS, handshake failed: %s", strerr);
  303. return CURLE_SSL_CONNECT_ERROR;
  304. }
  305. /* Reset our connect state machine */
  306. connssl->connecting_state = ssl_connect_1;
  307. return CURLE_OK;
  308. }
  309. }
  310. static gnutls_x509_crt_fmt_t gnutls_do_file_type(const char *type)
  311. {
  312. if(!type || !type[0])
  313. return GNUTLS_X509_FMT_PEM;
  314. if(strcasecompare(type, "PEM"))
  315. return GNUTLS_X509_FMT_PEM;
  316. if(strcasecompare(type, "DER"))
  317. return GNUTLS_X509_FMT_DER;
  318. return GNUTLS_X509_FMT_PEM; /* default to PEM */
  319. }
  320. #define GNUTLS_CIPHERS "NORMAL:-ARCFOUR-128:-CTYPE-ALL:+CTYPE-X509"
  321. /* If GnuTLS was compiled without support for SRP it will error out if SRP is
  322. requested in the priority string, so treat it specially
  323. */
  324. #define GNUTLS_SRP "+SRP"
  325. static CURLcode
  326. gnutls_set_ssl_version_min_max(struct Curl_easy *data,
  327. struct ssl_peer *peer,
  328. struct ssl_primary_config *conn_config,
  329. const char **prioritylist,
  330. const char *tls13support)
  331. {
  332. long ssl_version = conn_config->version;
  333. long ssl_version_max = conn_config->version_max;
  334. if((ssl_version == CURL_SSLVERSION_DEFAULT) ||
  335. (ssl_version == CURL_SSLVERSION_TLSv1))
  336. ssl_version = CURL_SSLVERSION_TLSv1_0;
  337. if(ssl_version_max == CURL_SSLVERSION_MAX_NONE)
  338. ssl_version_max = CURL_SSLVERSION_MAX_DEFAULT;
  339. if(peer->transport == TRNSPRT_QUIC) {
  340. if((ssl_version_max != CURL_SSLVERSION_MAX_DEFAULT) &&
  341. (ssl_version_max < CURL_SSLVERSION_MAX_TLSv1_3)) {
  342. failf(data, "QUIC needs at least TLS version 1.3");
  343. return CURLE_SSL_CONNECT_ERROR;
  344. }
  345. *prioritylist = QUIC_PRIORITY;
  346. return CURLE_OK;
  347. }
  348. if(!tls13support) {
  349. /* If the running GnuTLS does not support TLS 1.3, we must not specify a
  350. prioritylist involving that since it will make GnuTLS return an en
  351. error back at us */
  352. if((ssl_version_max == CURL_SSLVERSION_MAX_TLSv1_3) ||
  353. (ssl_version_max == CURL_SSLVERSION_MAX_DEFAULT)) {
  354. ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
  355. }
  356. }
  357. else if(ssl_version_max == CURL_SSLVERSION_MAX_DEFAULT) {
  358. ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_3;
  359. }
  360. switch(ssl_version | ssl_version_max) {
  361. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_0:
  362. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  363. "+VERS-TLS1.0";
  364. return CURLE_OK;
  365. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_1:
  366. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  367. "+VERS-TLS1.1:+VERS-TLS1.0";
  368. return CURLE_OK;
  369. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_2:
  370. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  371. "+VERS-TLS1.2:+VERS-TLS1.1:+VERS-TLS1.0";
  372. return CURLE_OK;
  373. case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_1:
  374. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  375. "+VERS-TLS1.1";
  376. return CURLE_OK;
  377. case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_2:
  378. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  379. "+VERS-TLS1.2:+VERS-TLS1.1";
  380. return CURLE_OK;
  381. case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_2:
  382. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  383. "+VERS-TLS1.2";
  384. return CURLE_OK;
  385. case CURL_SSLVERSION_TLSv1_3 | CURL_SSLVERSION_MAX_TLSv1_3:
  386. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  387. "+VERS-TLS1.3";
  388. return CURLE_OK;
  389. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_3:
  390. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0";
  391. return CURLE_OK;
  392. case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_3:
  393. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  394. "+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.1";
  395. return CURLE_OK;
  396. case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_3:
  397. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  398. "+VERS-TLS1.3:+VERS-TLS1.2";
  399. return CURLE_OK;
  400. }
  401. failf(data, "GnuTLS: cannot set ssl protocol");
  402. return CURLE_SSL_CONNECT_ERROR;
  403. }
  404. CURLcode Curl_gtls_shared_creds_create(struct Curl_easy *data,
  405. struct gtls_shared_creds **pcreds)
  406. {
  407. struct gtls_shared_creds *shared;
  408. int rc;
  409. *pcreds = NULL;
  410. shared = calloc(1, sizeof(*shared));
  411. if(!shared)
  412. return CURLE_OUT_OF_MEMORY;
  413. rc = gnutls_certificate_allocate_credentials(&shared->creds);
  414. if(rc != GNUTLS_E_SUCCESS) {
  415. failf(data, "gnutls_cert_all_cred() failed: %s", gnutls_strerror(rc));
  416. free(shared);
  417. return CURLE_SSL_CONNECT_ERROR;
  418. }
  419. shared->refcount = 1;
  420. shared->time = Curl_now();
  421. *pcreds = shared;
  422. return CURLE_OK;
  423. }
  424. CURLcode Curl_gtls_shared_creds_up_ref(struct gtls_shared_creds *creds)
  425. {
  426. DEBUGASSERT(creds);
  427. if(creds->refcount < SIZE_T_MAX) {
  428. ++creds->refcount;
  429. return CURLE_OK;
  430. }
  431. return CURLE_BAD_FUNCTION_ARGUMENT;
  432. }
  433. void Curl_gtls_shared_creds_free(struct gtls_shared_creds **pcreds)
  434. {
  435. struct gtls_shared_creds *shared = *pcreds;
  436. *pcreds = NULL;
  437. if(shared) {
  438. --shared->refcount;
  439. if(!shared->refcount) {
  440. gnutls_certificate_free_credentials(shared->creds);
  441. free(shared->CAfile);
  442. free(shared);
  443. }
  444. }
  445. }
  446. static CURLcode gtls_populate_creds(struct Curl_cfilter *cf,
  447. struct Curl_easy *data,
  448. gnutls_certificate_credentials_t creds)
  449. {
  450. struct ssl_primary_config *config = Curl_ssl_cf_get_primary_config(cf);
  451. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  452. int rc;
  453. if(config->verifypeer) {
  454. bool imported_native_ca = FALSE;
  455. if(ssl_config->native_ca_store) {
  456. rc = gnutls_certificate_set_x509_system_trust(creds);
  457. if(rc < 0)
  458. infof(data, "error reading native ca store (%s), continuing anyway",
  459. gnutls_strerror(rc));
  460. else {
  461. infof(data, "found %d certificates in native ca store", rc);
  462. if(rc > 0)
  463. imported_native_ca = TRUE;
  464. }
  465. }
  466. if(config->CAfile) {
  467. /* set the trusted CA cert bundle file */
  468. gnutls_certificate_set_verify_flags(creds,
  469. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
  470. rc = gnutls_certificate_set_x509_trust_file(creds,
  471. config->CAfile,
  472. GNUTLS_X509_FMT_PEM);
  473. if(rc < 0) {
  474. infof(data, "error reading ca cert file %s (%s)%s",
  475. config->CAfile, gnutls_strerror(rc),
  476. (imported_native_ca ? ", continuing anyway" : ""));
  477. if(!imported_native_ca) {
  478. ssl_config->certverifyresult = rc;
  479. return CURLE_SSL_CACERT_BADFILE;
  480. }
  481. }
  482. else
  483. infof(data, "found %d certificates in %s", rc, config->CAfile);
  484. }
  485. if(config->CApath) {
  486. /* set the trusted CA cert directory */
  487. rc = gnutls_certificate_set_x509_trust_dir(creds, config->CApath,
  488. GNUTLS_X509_FMT_PEM);
  489. if(rc < 0) {
  490. infof(data, "error reading ca cert file %s (%s)%s",
  491. config->CApath, gnutls_strerror(rc),
  492. (imported_native_ca ? ", continuing anyway" : ""));
  493. if(!imported_native_ca) {
  494. ssl_config->certverifyresult = rc;
  495. return CURLE_SSL_CACERT_BADFILE;
  496. }
  497. }
  498. else
  499. infof(data, "found %d certificates in %s", rc, config->CApath);
  500. }
  501. }
  502. if(config->CRLfile) {
  503. /* set the CRL list file */
  504. rc = gnutls_certificate_set_x509_crl_file(creds, config->CRLfile,
  505. GNUTLS_X509_FMT_PEM);
  506. if(rc < 0) {
  507. failf(data, "error reading crl file %s (%s)",
  508. config->CRLfile, gnutls_strerror(rc));
  509. return CURLE_SSL_CRL_BADFILE;
  510. }
  511. else
  512. infof(data, "found %d CRL in %s", rc, config->CRLfile);
  513. }
  514. return CURLE_OK;
  515. }
  516. /* key to use at `multi->proto_hash` */
  517. #define MPROTO_GTLS_X509_KEY "tls:gtls:x509:share"
  518. static bool gtls_shared_creds_expired(const struct Curl_easy *data,
  519. const struct gtls_shared_creds *sc)
  520. {
  521. const struct ssl_general_config *cfg = &data->set.general_ssl;
  522. struct curltime now = Curl_now();
  523. timediff_t elapsed_ms = Curl_timediff(now, sc->time);
  524. timediff_t timeout_ms = cfg->ca_cache_timeout * (timediff_t)1000;
  525. if(timeout_ms < 0)
  526. return FALSE;
  527. return elapsed_ms >= timeout_ms;
  528. }
  529. static bool gtls_shared_creds_different(struct Curl_cfilter *cf,
  530. const struct gtls_shared_creds *sc)
  531. {
  532. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  533. if(!sc->CAfile || !conn_config->CAfile)
  534. return sc->CAfile != conn_config->CAfile;
  535. return strcmp(sc->CAfile, conn_config->CAfile);
  536. }
  537. static struct gtls_shared_creds*
  538. gtls_get_cached_creds(struct Curl_cfilter *cf, struct Curl_easy *data)
  539. {
  540. struct gtls_shared_creds *shared_creds;
  541. if(data->multi) {
  542. shared_creds = Curl_hash_pick(&data->multi->proto_hash,
  543. (void *)MPROTO_GTLS_X509_KEY,
  544. sizeof(MPROTO_GTLS_X509_KEY)-1);
  545. if(shared_creds && shared_creds->creds &&
  546. !gtls_shared_creds_expired(data, shared_creds) &&
  547. !gtls_shared_creds_different(cf, shared_creds)) {
  548. return shared_creds;
  549. }
  550. }
  551. return NULL;
  552. }
  553. static void gtls_shared_creds_hash_free(void *key, size_t key_len, void *p)
  554. {
  555. struct gtls_shared_creds *sc = p;
  556. DEBUGASSERT(key_len == (sizeof(MPROTO_GTLS_X509_KEY)-1));
  557. DEBUGASSERT(!memcmp(MPROTO_GTLS_X509_KEY, key, key_len));
  558. (void)key;
  559. (void)key_len;
  560. Curl_gtls_shared_creds_free(&sc); /* down reference */
  561. }
  562. static void gtls_set_cached_creds(struct Curl_cfilter *cf,
  563. struct Curl_easy *data,
  564. struct gtls_shared_creds *sc)
  565. {
  566. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  567. DEBUGASSERT(sc);
  568. DEBUGASSERT(sc->creds);
  569. DEBUGASSERT(!sc->CAfile);
  570. DEBUGASSERT(sc->refcount == 1);
  571. if(!data->multi)
  572. return;
  573. if(conn_config->CAfile) {
  574. sc->CAfile = strdup(conn_config->CAfile);
  575. if(!sc->CAfile)
  576. return;
  577. }
  578. if(Curl_gtls_shared_creds_up_ref(sc))
  579. return;
  580. if(!Curl_hash_add2(&data->multi->proto_hash,
  581. (void *)MPROTO_GTLS_X509_KEY,
  582. sizeof(MPROTO_GTLS_X509_KEY)-1,
  583. sc, gtls_shared_creds_hash_free)) {
  584. Curl_gtls_shared_creds_free(&sc); /* down reference again */
  585. return;
  586. }
  587. }
  588. CURLcode Curl_gtls_client_trust_setup(struct Curl_cfilter *cf,
  589. struct Curl_easy *data,
  590. struct gtls_ctx *gtls)
  591. {
  592. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  593. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  594. struct gtls_shared_creds *cached_creds = NULL;
  595. bool cache_criteria_met;
  596. CURLcode result;
  597. int rc;
  598. /* Consider the X509 store cacheable if it comes exclusively from a CAfile,
  599. or no source is provided and we are falling back to OpenSSL's built-in
  600. default. */
  601. cache_criteria_met = (data->set.general_ssl.ca_cache_timeout != 0) &&
  602. conn_config->verifypeer &&
  603. !conn_config->CApath &&
  604. !conn_config->ca_info_blob &&
  605. !ssl_config->primary.CRLfile &&
  606. !ssl_config->native_ca_store &&
  607. !conn_config->clientcert; /* GnuTLS adds client cert to its credentials! */
  608. if(cache_criteria_met)
  609. cached_creds = gtls_get_cached_creds(cf, data);
  610. if(cached_creds && !Curl_gtls_shared_creds_up_ref(cached_creds)) {
  611. CURL_TRC_CF(data, cf, "using shared trust anchors and CRLs");
  612. Curl_gtls_shared_creds_free(&gtls->shared_creds);
  613. gtls->shared_creds = cached_creds;
  614. rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_CERTIFICATE,
  615. gtls->shared_creds->creds);
  616. if(rc != GNUTLS_E_SUCCESS) {
  617. failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
  618. return CURLE_SSL_CONNECT_ERROR;
  619. }
  620. }
  621. else {
  622. CURL_TRC_CF(data, cf, "loading trust anchors and CRLs");
  623. result = gtls_populate_creds(cf, data, gtls->shared_creds->creds);
  624. if(result)
  625. return result;
  626. gtls->shared_creds->trust_setup = TRUE;
  627. if(cache_criteria_met)
  628. gtls_set_cached_creds(cf, data, gtls->shared_creds);
  629. }
  630. return CURLE_OK;
  631. }
  632. static void gtls_sessionid_free(void *sessionid, size_t idsize)
  633. {
  634. (void)idsize;
  635. free(sessionid);
  636. }
  637. CURLcode Curl_gtls_update_session_id(struct Curl_cfilter *cf,
  638. struct Curl_easy *data,
  639. gnutls_session_t session,
  640. struct ssl_peer *peer,
  641. const char *alpn)
  642. {
  643. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  644. void *connect_sessionid;
  645. size_t connect_idsize = 0;
  646. CURLcode result = CURLE_OK;
  647. if(!ssl_config->primary.cache_session)
  648. return CURLE_OK;
  649. /* we always unconditionally get the session id here, as even if we
  650. already got it from the cache and asked to use it in the connection, it
  651. might've been rejected and then a new one is in use now and we need to
  652. detect that. */
  653. /* get the session ID data size */
  654. gnutls_session_get_data(session, NULL, &connect_idsize);
  655. if(!connect_idsize) /* gnutls does this for some version combinations */
  656. return CURLE_OK;
  657. connect_sessionid = malloc(connect_idsize); /* get a buffer for it */
  658. if(!connect_sessionid)
  659. return CURLE_OUT_OF_MEMORY;
  660. /* extract session ID to the allocated buffer */
  661. gnutls_session_get_data(session, connect_sessionid, &connect_idsize);
  662. CURL_TRC_CF(data, cf, "get session id (len=%zu, alpn=%s) and store in cache",
  663. connect_idsize, alpn ? alpn : "-");
  664. Curl_ssl_sessionid_lock(data);
  665. /* store this session id, takes ownership */
  666. result = Curl_ssl_set_sessionid(cf, data, peer, alpn,
  667. connect_sessionid, connect_idsize,
  668. gtls_sessionid_free);
  669. Curl_ssl_sessionid_unlock(data);
  670. return result;
  671. }
  672. static CURLcode cf_gtls_update_session_id(struct Curl_cfilter *cf,
  673. struct Curl_easy *data,
  674. gnutls_session_t session)
  675. {
  676. struct ssl_connect_data *connssl = cf->ctx;
  677. return Curl_gtls_update_session_id(cf, data, session, &connssl->peer,
  678. connssl->alpn_negotiated);
  679. }
  680. static int gtls_handshake_cb(gnutls_session_t session, unsigned int htype,
  681. unsigned when, unsigned int incoming,
  682. const gnutls_datum_t *msg)
  683. {
  684. struct Curl_cfilter *cf = gnutls_session_get_ptr(session);
  685. (void)msg;
  686. (void)incoming;
  687. if(when) { /* after message has been processed */
  688. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  689. if(data) {
  690. CURL_TRC_CF(data, cf, "handshake: %s message type %d",
  691. incoming ? "incoming" : "outgoing", htype);
  692. switch(htype) {
  693. case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET: {
  694. cf_gtls_update_session_id(cf, data, session);
  695. break;
  696. }
  697. default:
  698. break;
  699. }
  700. }
  701. }
  702. return 0;
  703. }
  704. static CURLcode gtls_client_init(struct Curl_cfilter *cf,
  705. struct Curl_easy *data,
  706. struct ssl_peer *peer,
  707. struct gtls_ctx *gtls)
  708. {
  709. struct ssl_primary_config *config = Curl_ssl_cf_get_primary_config(cf);
  710. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  711. unsigned int init_flags;
  712. int rc;
  713. bool sni = TRUE; /* default is SNI enabled */
  714. const char *prioritylist;
  715. const char *err = NULL;
  716. const char *tls13support;
  717. CURLcode result;
  718. if(!gtls_inited)
  719. gtls_init();
  720. if(config->version == CURL_SSLVERSION_SSLv2) {
  721. failf(data, "GnuTLS does not support SSLv2");
  722. return CURLE_SSL_CONNECT_ERROR;
  723. }
  724. else if(config->version == CURL_SSLVERSION_SSLv3)
  725. sni = FALSE; /* SSLv3 has no SNI */
  726. /* allocate a shared creds struct */
  727. result = Curl_gtls_shared_creds_create(data, &gtls->shared_creds);
  728. if(result)
  729. return result;
  730. #ifdef USE_GNUTLS_SRP
  731. if(config->username && Curl_auth_allowed_to_host(data)) {
  732. infof(data, "Using TLS-SRP username: %s", config->username);
  733. rc = gnutls_srp_allocate_client_credentials(&gtls->srp_client_cred);
  734. if(rc != GNUTLS_E_SUCCESS) {
  735. failf(data, "gnutls_srp_allocate_client_cred() failed: %s",
  736. gnutls_strerror(rc));
  737. return CURLE_OUT_OF_MEMORY;
  738. }
  739. rc = gnutls_srp_set_client_credentials(gtls->srp_client_cred,
  740. config->username,
  741. config->password);
  742. if(rc != GNUTLS_E_SUCCESS) {
  743. failf(data, "gnutls_srp_set_client_cred() failed: %s",
  744. gnutls_strerror(rc));
  745. return CURLE_BAD_FUNCTION_ARGUMENT;
  746. }
  747. }
  748. #endif
  749. ssl_config->certverifyresult = 0;
  750. /* Initialize TLS session as a client */
  751. init_flags = GNUTLS_CLIENT;
  752. #if defined(GNUTLS_FORCE_CLIENT_CERT)
  753. init_flags |= GNUTLS_FORCE_CLIENT_CERT;
  754. #endif
  755. #if defined(GNUTLS_NO_TICKETS_TLS12)
  756. init_flags |= GNUTLS_NO_TICKETS_TLS12;
  757. #elif defined(GNUTLS_NO_TICKETS)
  758. /* Disable TLS session tickets for non 1.3 connections */
  759. if((config->version != CURL_SSLVERSION_TLSv1_3) &&
  760. (config->version != CURL_SSLVERSION_DEFAULT))
  761. init_flags |= GNUTLS_NO_TICKETS;
  762. #endif
  763. #if defined(GNUTLS_NO_STATUS_REQUEST)
  764. if(!config->verifystatus)
  765. /* Disable the "status_request" TLS extension, enabled by default since
  766. GnuTLS 3.8.0. */
  767. init_flags |= GNUTLS_NO_STATUS_REQUEST;
  768. #endif
  769. rc = gnutls_init(&gtls->session, init_flags);
  770. if(rc != GNUTLS_E_SUCCESS) {
  771. failf(data, "gnutls_init() failed: %d", rc);
  772. return CURLE_SSL_CONNECT_ERROR;
  773. }
  774. if(sni && peer->sni) {
  775. if(gnutls_server_name_set(gtls->session, GNUTLS_NAME_DNS,
  776. peer->sni, strlen(peer->sni)) < 0) {
  777. failf(data, "Failed to set SNI");
  778. return CURLE_SSL_CONNECT_ERROR;
  779. }
  780. }
  781. /* Use default priorities */
  782. rc = gnutls_set_default_priority(gtls->session);
  783. if(rc != GNUTLS_E_SUCCESS)
  784. return CURLE_SSL_CONNECT_ERROR;
  785. /* "In GnuTLS 3.6.5, TLS 1.3 is enabled by default" */
  786. tls13support = gnutls_check_version("3.6.5");
  787. /* Ensure +SRP comes at the *end* of all relevant strings so that it can be
  788. * removed if a runtime error indicates that SRP is not supported by this
  789. * GnuTLS version */
  790. if(config->version == CURL_SSLVERSION_SSLv2 ||
  791. config->version == CURL_SSLVERSION_SSLv3) {
  792. failf(data, "GnuTLS does not support SSLv2 or SSLv3");
  793. return CURLE_SSL_CONNECT_ERROR;
  794. }
  795. if(config->version == CURL_SSLVERSION_TLSv1_3) {
  796. if(!tls13support) {
  797. failf(data, "This GnuTLS installation does not support TLS 1.3");
  798. return CURLE_SSL_CONNECT_ERROR;
  799. }
  800. }
  801. /* At this point we know we have a supported TLS version, so set it */
  802. result = gnutls_set_ssl_version_min_max(data, peer,
  803. config, &prioritylist, tls13support);
  804. if(result)
  805. return result;
  806. #ifdef USE_GNUTLS_SRP
  807. /* Only add SRP to the cipher list if SRP is requested. Otherwise
  808. * GnuTLS will disable TLS 1.3 support. */
  809. if(config->username) {
  810. char *prioritysrp = aprintf("%s:" GNUTLS_SRP, prioritylist);
  811. if(!prioritysrp)
  812. return CURLE_OUT_OF_MEMORY;
  813. rc = gnutls_priority_set_direct(gtls->session, prioritysrp, &err);
  814. free(prioritysrp);
  815. if((rc == GNUTLS_E_INVALID_REQUEST) && err) {
  816. infof(data, "This GnuTLS does not support SRP");
  817. }
  818. }
  819. else {
  820. #endif
  821. infof(data, "GnuTLS ciphers: %s", prioritylist);
  822. rc = gnutls_priority_set_direct(gtls->session, prioritylist, &err);
  823. #ifdef USE_GNUTLS_SRP
  824. }
  825. #endif
  826. if(rc != GNUTLS_E_SUCCESS) {
  827. failf(data, "Error %d setting GnuTLS cipher list starting with %s",
  828. rc, err);
  829. return CURLE_SSL_CONNECT_ERROR;
  830. }
  831. if(config->clientcert) {
  832. if(!gtls->shared_creds->trust_setup) {
  833. result = Curl_gtls_client_trust_setup(cf, data, gtls);
  834. if(result)
  835. return result;
  836. }
  837. if(ssl_config->cert_type && strcasecompare(ssl_config->cert_type, "P12")) {
  838. rc = gnutls_certificate_set_x509_simple_pkcs12_file(
  839. gtls->shared_creds->creds, config->clientcert, GNUTLS_X509_FMT_DER,
  840. ssl_config->key_passwd ? ssl_config->key_passwd : "");
  841. if(rc != GNUTLS_E_SUCCESS) {
  842. failf(data,
  843. "error reading X.509 potentially-encrypted key or certificate "
  844. "file: %s",
  845. gnutls_strerror(rc));
  846. return CURLE_SSL_CONNECT_ERROR;
  847. }
  848. }
  849. else if(ssl_config->key_passwd) {
  850. const unsigned int supported_key_encryption_algorithms =
  851. GNUTLS_PKCS_USE_PKCS12_3DES | GNUTLS_PKCS_USE_PKCS12_ARCFOUR |
  852. GNUTLS_PKCS_USE_PKCS12_RC2_40 | GNUTLS_PKCS_USE_PBES2_3DES |
  853. GNUTLS_PKCS_USE_PBES2_AES_128 | GNUTLS_PKCS_USE_PBES2_AES_192 |
  854. GNUTLS_PKCS_USE_PBES2_AES_256;
  855. rc = gnutls_certificate_set_x509_key_file2(
  856. gtls->shared_creds->creds,
  857. config->clientcert,
  858. ssl_config->key ? ssl_config->key : config->clientcert,
  859. gnutls_do_file_type(ssl_config->cert_type),
  860. ssl_config->key_passwd,
  861. supported_key_encryption_algorithms);
  862. if(rc != GNUTLS_E_SUCCESS) {
  863. failf(data,
  864. "error reading X.509 potentially-encrypted key file: %s",
  865. gnutls_strerror(rc));
  866. return CURLE_SSL_CONNECT_ERROR;
  867. }
  868. }
  869. else {
  870. if(gnutls_certificate_set_x509_key_file(
  871. gtls->shared_creds->creds,
  872. config->clientcert,
  873. ssl_config->key ? ssl_config->key : config->clientcert,
  874. gnutls_do_file_type(ssl_config->cert_type) ) !=
  875. GNUTLS_E_SUCCESS) {
  876. failf(data, "error reading X.509 key or certificate file");
  877. return CURLE_SSL_CONNECT_ERROR;
  878. }
  879. }
  880. }
  881. #ifdef USE_GNUTLS_SRP
  882. /* put the credentials to the current session */
  883. if(config->username) {
  884. rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_SRP,
  885. gtls->srp_client_cred);
  886. if(rc != GNUTLS_E_SUCCESS) {
  887. failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
  888. return CURLE_SSL_CONNECT_ERROR;
  889. }
  890. }
  891. else
  892. #endif
  893. {
  894. rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_CERTIFICATE,
  895. gtls->shared_creds->creds);
  896. if(rc != GNUTLS_E_SUCCESS) {
  897. failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
  898. return CURLE_SSL_CONNECT_ERROR;
  899. }
  900. }
  901. if(config->verifystatus) {
  902. rc = gnutls_ocsp_status_request_enable_client(gtls->session,
  903. NULL, 0, NULL);
  904. if(rc != GNUTLS_E_SUCCESS) {
  905. failf(data, "gnutls_ocsp_status_request_enable_client() failed: %d", rc);
  906. return CURLE_SSL_CONNECT_ERROR;
  907. }
  908. }
  909. return CURLE_OK;
  910. }
  911. static int keylog_callback(gnutls_session_t session, const char *label,
  912. const gnutls_datum_t *secret)
  913. {
  914. gnutls_datum_t crandom;
  915. gnutls_datum_t srandom;
  916. gnutls_session_get_random(session, &crandom, &srandom);
  917. if(crandom.size != 32) {
  918. return -1;
  919. }
  920. Curl_tls_keylog_write(label, crandom.data, secret->data, secret->size);
  921. return 0;
  922. }
  923. CURLcode Curl_gtls_ctx_init(struct gtls_ctx *gctx,
  924. struct Curl_cfilter *cf,
  925. struct Curl_easy *data,
  926. struct ssl_peer *peer,
  927. const unsigned char *alpn, size_t alpn_len,
  928. struct ssl_connect_data *connssl,
  929. Curl_gtls_ctx_setup_cb *cb_setup,
  930. void *cb_user_data,
  931. void *ssl_user_data)
  932. {
  933. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  934. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  935. gnutls_datum_t gtls_alpns[5];
  936. size_t gtls_alpns_count = 0;
  937. CURLcode result;
  938. DEBUGASSERT(gctx);
  939. result = gtls_client_init(cf, data, peer, gctx);
  940. if(result)
  941. return result;
  942. gnutls_session_set_ptr(gctx->session, ssl_user_data);
  943. if(cb_setup) {
  944. result = cb_setup(cf, data, cb_user_data);
  945. if(result)
  946. return result;
  947. }
  948. /* Open the file if a TLS or QUIC backend has not done this before. */
  949. Curl_tls_keylog_open();
  950. if(Curl_tls_keylog_enabled()) {
  951. gnutls_session_set_keylog_function(gctx->session, keylog_callback);
  952. }
  953. /* This might be a reconnect, so we check for a session ID in the cache
  954. to speed up things */
  955. if(conn_config->cache_session) {
  956. void *ssl_sessionid;
  957. size_t ssl_idsize;
  958. char *session_alpn;
  959. Curl_ssl_sessionid_lock(data);
  960. if(!Curl_ssl_getsessionid(cf, data, peer,
  961. &ssl_sessionid, &ssl_idsize, &session_alpn)) {
  962. /* we got a session id, use it! */
  963. int rc;
  964. rc = gnutls_session_set_data(gctx->session, ssl_sessionid, ssl_idsize);
  965. if(rc < 0)
  966. infof(data, "SSL failed to set session ID");
  967. else {
  968. infof(data, "SSL reusing session ID (size=%zu, alpn=%s)",
  969. ssl_idsize, session_alpn ? session_alpn : "-");
  970. #ifdef DEBUGBUILD
  971. if((ssl_config->earlydata || !!getenv("CURL_USE_EARLYDATA")) &&
  972. #else
  973. if(ssl_config->earlydata &&
  974. #endif
  975. !cf->conn->connect_only && connssl &&
  976. (gnutls_protocol_get_version(gctx->session) == GNUTLS_TLS1_3) &&
  977. Curl_alpn_contains_proto(connssl->alpn, session_alpn)) {
  978. connssl->earlydata_max =
  979. gnutls_record_get_max_early_data_size(gctx->session);
  980. if((!connssl->earlydata_max ||
  981. connssl->earlydata_max == 0xFFFFFFFFUL)) {
  982. /* Seems to be GnuTLS way to signal no EarlyData in session */
  983. CURL_TRC_CF(data, cf, "TLS session does not allow earlydata");
  984. }
  985. else {
  986. CURL_TRC_CF(data, cf, "TLS session allows %zu earlydata bytes, "
  987. "reusing ALPN '%s'",
  988. connssl->earlydata_max, session_alpn);
  989. connssl->earlydata_state = ssl_earlydata_use;
  990. connssl->state = ssl_connection_deferred;
  991. result = Curl_alpn_set_negotiated(cf, data, connssl,
  992. (const unsigned char *)session_alpn,
  993. session_alpn ? strlen(session_alpn) : 0);
  994. if(result)
  995. return result;
  996. /* We only try the ALPN protocol the session used before,
  997. * otherwise we might send early data for the wrong protocol */
  998. gtls_alpns[0].data = (unsigned char *)session_alpn;
  999. gtls_alpns[0].size = (unsigned)strlen(session_alpn);
  1000. gtls_alpns_count = 1;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. Curl_ssl_sessionid_unlock(data);
  1006. }
  1007. /* convert the ALPN string from our arguments to a list of strings that
  1008. * gnutls wants and will convert internally back to this string for sending
  1009. * to the server. nice. */
  1010. if(!gtls_alpns_count && alpn && alpn_len) {
  1011. size_t i, alen = alpn_len;
  1012. unsigned char *s = (unsigned char *)alpn;
  1013. unsigned char slen;
  1014. for(i = 0; (i < ARRAYSIZE(gtls_alpns)) && alen; ++i) {
  1015. slen = s[0];
  1016. if(slen >= alen)
  1017. return CURLE_FAILED_INIT;
  1018. gtls_alpns[i].data = s + 1;
  1019. gtls_alpns[i].size = slen;
  1020. s += slen + 1;
  1021. alen -= (size_t)slen + 1;
  1022. }
  1023. if(alen) /* not all alpn chars used, wrong format or too many */
  1024. return CURLE_FAILED_INIT;
  1025. gtls_alpns_count = i;
  1026. }
  1027. if(gtls_alpns_count &&
  1028. gnutls_alpn_set_protocols(gctx->session,
  1029. gtls_alpns, (unsigned int)gtls_alpns_count,
  1030. GNUTLS_ALPN_MANDATORY)) {
  1031. failf(data, "failed setting ALPN");
  1032. return CURLE_SSL_CONNECT_ERROR;
  1033. }
  1034. return CURLE_OK;
  1035. }
  1036. static CURLcode
  1037. gtls_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
  1038. {
  1039. struct ssl_connect_data *connssl = cf->ctx;
  1040. struct gtls_ssl_backend_data *backend =
  1041. (struct gtls_ssl_backend_data *)connssl->backend;
  1042. struct alpn_proto_buf proto;
  1043. CURLcode result;
  1044. DEBUGASSERT(backend);
  1045. DEBUGASSERT(ssl_connect_1 == connssl->connecting_state);
  1046. if(connssl->state == ssl_connection_complete)
  1047. /* to make us tolerant against being called more than once for the
  1048. same connection */
  1049. return CURLE_OK;
  1050. memset(&proto, 0, sizeof(proto));
  1051. if(connssl->alpn) {
  1052. result = Curl_alpn_to_proto_buf(&proto, connssl->alpn);
  1053. if(result) {
  1054. failf(data, "Error determining ALPN");
  1055. return CURLE_SSL_CONNECT_ERROR;
  1056. }
  1057. }
  1058. result = Curl_gtls_ctx_init(&backend->gtls, cf, data, &connssl->peer,
  1059. proto.data, proto.len, connssl, NULL, NULL, cf);
  1060. if(result)
  1061. return result;
  1062. if(connssl->alpn && (connssl->state != ssl_connection_deferred)) {
  1063. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  1064. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  1065. }
  1066. gnutls_handshake_set_hook_function(backend->gtls.session,
  1067. GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST,
  1068. gtls_handshake_cb);
  1069. /* register callback functions and handle to send and receive data. */
  1070. gnutls_transport_set_ptr(backend->gtls.session, cf);
  1071. gnutls_transport_set_push_function(backend->gtls.session, gtls_push);
  1072. gnutls_transport_set_pull_function(backend->gtls.session, gtls_pull);
  1073. return CURLE_OK;
  1074. }
  1075. static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
  1076. gnutls_x509_crt_t cert,
  1077. const char *pinnedpubkey)
  1078. {
  1079. /* Scratch */
  1080. size_t len1 = 0, len2 = 0;
  1081. unsigned char *buff1 = NULL;
  1082. gnutls_pubkey_t key = NULL;
  1083. /* Result is returned to caller */
  1084. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  1085. /* if a path was not specified, do not pin */
  1086. if(!pinnedpubkey)
  1087. return CURLE_OK;
  1088. if(!cert)
  1089. return result;
  1090. do {
  1091. int ret;
  1092. /* Begin Gyrations to get the public key */
  1093. gnutls_pubkey_init(&key);
  1094. ret = gnutls_pubkey_import_x509(key, cert, 0);
  1095. if(ret < 0)
  1096. break; /* failed */
  1097. ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, NULL, &len1);
  1098. if(ret != GNUTLS_E_SHORT_MEMORY_BUFFER || len1 == 0)
  1099. break; /* failed */
  1100. buff1 = malloc(len1);
  1101. if(!buff1)
  1102. break; /* failed */
  1103. len2 = len1;
  1104. ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, buff1, &len2);
  1105. if(ret < 0 || len1 != len2)
  1106. break; /* failed */
  1107. /* End Gyrations */
  1108. /* The one good exit point */
  1109. result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
  1110. } while(0);
  1111. if(key)
  1112. gnutls_pubkey_deinit(key);
  1113. Curl_safefree(buff1);
  1114. return result;
  1115. }
  1116. CURLcode
  1117. Curl_gtls_verifyserver(struct Curl_easy *data,
  1118. gnutls_session_t session,
  1119. struct ssl_primary_config *config,
  1120. struct ssl_config_data *ssl_config,
  1121. struct ssl_peer *peer,
  1122. const char *pinned_key)
  1123. {
  1124. unsigned int cert_list_size;
  1125. const gnutls_datum_t *chainp;
  1126. unsigned int verify_status = 0;
  1127. gnutls_x509_crt_t x509_cert, x509_issuer;
  1128. gnutls_datum_t issuerp;
  1129. gnutls_datum_t certfields;
  1130. char certname[65] = ""; /* limited to 64 chars by ASN.1 */
  1131. size_t size;
  1132. time_t certclock;
  1133. int rc;
  1134. CURLcode result = CURLE_OK;
  1135. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1136. const char *ptr;
  1137. int algo;
  1138. unsigned int bits;
  1139. gnutls_protocol_t version = gnutls_protocol_get_version(session);
  1140. #endif
  1141. long * const certverifyresult = &ssl_config->certverifyresult;
  1142. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1143. /* the name of the cipher suite used, e.g. ECDHE_RSA_AES_256_GCM_SHA384. */
  1144. ptr = gnutls_cipher_suite_get_name(gnutls_kx_get(session),
  1145. gnutls_cipher_get(session),
  1146. gnutls_mac_get(session));
  1147. infof(data, "SSL connection using %s / %s",
  1148. gnutls_protocol_get_name(version), ptr);
  1149. #endif
  1150. /* This function will return the peer's raw certificate (chain) as sent by
  1151. the peer. These certificates are in raw format (DER encoded for
  1152. X.509). In case of a X.509 then a certificate list may be present. The
  1153. first certificate in the list is the peer's certificate, following the
  1154. issuer's certificate, then the issuer's issuer etc. */
  1155. chainp = gnutls_certificate_get_peers(session, &cert_list_size);
  1156. if(!chainp) {
  1157. if(config->verifypeer ||
  1158. config->verifyhost ||
  1159. config->issuercert) {
  1160. #ifdef USE_GNUTLS_SRP
  1161. if(ssl_config->primary.username && !config->verifypeer &&
  1162. gnutls_cipher_get(session)) {
  1163. /* no peer cert, but auth is ok if we have SRP user and cipher and no
  1164. peer verify */
  1165. }
  1166. else {
  1167. #endif
  1168. failf(data, "failed to get server cert");
  1169. *certverifyresult = GNUTLS_E_NO_CERTIFICATE_FOUND;
  1170. return CURLE_PEER_FAILED_VERIFICATION;
  1171. #ifdef USE_GNUTLS_SRP
  1172. }
  1173. #endif
  1174. }
  1175. infof(data, " common name: WARNING could not obtain");
  1176. }
  1177. if(data->set.ssl.certinfo && chainp) {
  1178. unsigned int i;
  1179. result = Curl_ssl_init_certinfo(data, (int)cert_list_size);
  1180. if(result)
  1181. return result;
  1182. for(i = 0; i < cert_list_size; i++) {
  1183. const char *beg = (const char *) chainp[i].data;
  1184. const char *end = beg + chainp[i].size;
  1185. result = Curl_extract_certinfo(data, (int)i, beg, end);
  1186. if(result)
  1187. return result;
  1188. }
  1189. }
  1190. if(config->verifypeer) {
  1191. /* This function will try to verify the peer's certificate and return its
  1192. status (trusted, invalid etc.). The value of status should be one or
  1193. more of the gnutls_certificate_status_t enumerated elements bitwise
  1194. or'd. To avoid denial of service attacks some default upper limits
  1195. regarding the certificate key size and chain size are set. To override
  1196. them use gnutls_certificate_set_verify_limits(). */
  1197. rc = gnutls_certificate_verify_peers2(session, &verify_status);
  1198. if(rc < 0) {
  1199. failf(data, "server cert verify failed: %d", rc);
  1200. *certverifyresult = rc;
  1201. return CURLE_SSL_CONNECT_ERROR;
  1202. }
  1203. *certverifyresult = verify_status;
  1204. /* verify_status is a bitmask of gnutls_certificate_status bits */
  1205. if(verify_status & GNUTLS_CERT_INVALID) {
  1206. if(config->verifypeer) {
  1207. const char *cause = "certificate error, no details available";
  1208. if(verify_status & GNUTLS_CERT_EXPIRED)
  1209. cause = "certificate has expired";
  1210. else if(verify_status & GNUTLS_CERT_SIGNER_NOT_FOUND)
  1211. cause = "certificate signer not trusted";
  1212. else if(verify_status & GNUTLS_CERT_INSECURE_ALGORITHM)
  1213. cause = "certificate uses insecure algorithm";
  1214. else if(verify_status & GNUTLS_CERT_INVALID_OCSP_STATUS)
  1215. cause = "attached OCSP status response is invalid";
  1216. failf(data, "server verification failed: %s. (CAfile: %s "
  1217. "CRLfile: %s)", cause,
  1218. config->CAfile ? config->CAfile : "none",
  1219. ssl_config->primary.CRLfile ?
  1220. ssl_config->primary.CRLfile : "none");
  1221. return CURLE_PEER_FAILED_VERIFICATION;
  1222. }
  1223. else
  1224. infof(data, " server certificate verification FAILED");
  1225. }
  1226. else
  1227. infof(data, " server certificate verification OK");
  1228. }
  1229. else
  1230. infof(data, " server certificate verification SKIPPED");
  1231. if(config->verifystatus) {
  1232. gnutls_datum_t status_request;
  1233. gnutls_ocsp_resp_t ocsp_resp;
  1234. gnutls_ocsp_cert_status_t status;
  1235. gnutls_x509_crl_reason_t reason;
  1236. rc = gnutls_ocsp_status_request_get(session, &status_request);
  1237. if(rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
  1238. failf(data, "No OCSP response received");
  1239. return CURLE_SSL_INVALIDCERTSTATUS;
  1240. }
  1241. if(rc < 0) {
  1242. failf(data, "Invalid OCSP response received");
  1243. return CURLE_SSL_INVALIDCERTSTATUS;
  1244. }
  1245. gnutls_ocsp_resp_init(&ocsp_resp);
  1246. rc = gnutls_ocsp_resp_import(ocsp_resp, &status_request);
  1247. if(rc < 0) {
  1248. failf(data, "Invalid OCSP response received");
  1249. return CURLE_SSL_INVALIDCERTSTATUS;
  1250. }
  1251. (void)gnutls_ocsp_resp_get_single(ocsp_resp, 0, NULL, NULL, NULL, NULL,
  1252. &status, NULL, NULL, NULL, &reason);
  1253. switch(status) {
  1254. case GNUTLS_OCSP_CERT_GOOD:
  1255. break;
  1256. case GNUTLS_OCSP_CERT_REVOKED: {
  1257. const char *crl_reason;
  1258. switch(reason) {
  1259. default:
  1260. case GNUTLS_X509_CRLREASON_UNSPECIFIED:
  1261. crl_reason = "unspecified reason";
  1262. break;
  1263. case GNUTLS_X509_CRLREASON_KEYCOMPROMISE:
  1264. crl_reason = "private key compromised";
  1265. break;
  1266. case GNUTLS_X509_CRLREASON_CACOMPROMISE:
  1267. crl_reason = "CA compromised";
  1268. break;
  1269. case GNUTLS_X509_CRLREASON_AFFILIATIONCHANGED:
  1270. crl_reason = "affiliation has changed";
  1271. break;
  1272. case GNUTLS_X509_CRLREASON_SUPERSEDED:
  1273. crl_reason = "certificate superseded";
  1274. break;
  1275. case GNUTLS_X509_CRLREASON_CESSATIONOFOPERATION:
  1276. crl_reason = "operation has ceased";
  1277. break;
  1278. case GNUTLS_X509_CRLREASON_CERTIFICATEHOLD:
  1279. crl_reason = "certificate is on hold";
  1280. break;
  1281. case GNUTLS_X509_CRLREASON_REMOVEFROMCRL:
  1282. crl_reason = "will be removed from delta CRL";
  1283. break;
  1284. case GNUTLS_X509_CRLREASON_PRIVILEGEWITHDRAWN:
  1285. crl_reason = "privilege withdrawn";
  1286. break;
  1287. case GNUTLS_X509_CRLREASON_AACOMPROMISE:
  1288. crl_reason = "AA compromised";
  1289. break;
  1290. }
  1291. failf(data, "Server certificate was revoked: %s", crl_reason);
  1292. break;
  1293. }
  1294. default:
  1295. case GNUTLS_OCSP_CERT_UNKNOWN:
  1296. failf(data, "Server certificate status is unknown");
  1297. break;
  1298. }
  1299. gnutls_ocsp_resp_deinit(ocsp_resp);
  1300. if(status != GNUTLS_OCSP_CERT_GOOD)
  1301. return CURLE_SSL_INVALIDCERTSTATUS;
  1302. }
  1303. else
  1304. infof(data, " server certificate status verification SKIPPED");
  1305. /* initialize an X.509 certificate structure. */
  1306. gnutls_x509_crt_init(&x509_cert);
  1307. if(chainp)
  1308. /* convert the given DER or PEM encoded Certificate to the native
  1309. gnutls_x509_crt_t format */
  1310. gnutls_x509_crt_import(x509_cert, chainp, GNUTLS_X509_FMT_DER);
  1311. if(config->issuercert) {
  1312. gnutls_x509_crt_init(&x509_issuer);
  1313. issuerp = load_file(config->issuercert);
  1314. gnutls_x509_crt_import(x509_issuer, &issuerp, GNUTLS_X509_FMT_PEM);
  1315. rc = (int)gnutls_x509_crt_check_issuer(x509_cert, x509_issuer);
  1316. gnutls_x509_crt_deinit(x509_issuer);
  1317. unload_file(issuerp);
  1318. if(rc <= 0) {
  1319. failf(data, "server certificate issuer check failed (IssuerCert: %s)",
  1320. config->issuercert ? config->issuercert : "none");
  1321. gnutls_x509_crt_deinit(x509_cert);
  1322. return CURLE_SSL_ISSUER_ERROR;
  1323. }
  1324. infof(data, " server certificate issuer check OK (Issuer Cert: %s)",
  1325. config->issuercert ? config->issuercert : "none");
  1326. }
  1327. size = sizeof(certname);
  1328. rc = gnutls_x509_crt_get_dn_by_oid(x509_cert, GNUTLS_OID_X520_COMMON_NAME,
  1329. 0, /* the first and only one */
  1330. FALSE,
  1331. certname,
  1332. &size);
  1333. if(rc) {
  1334. infof(data, "error fetching CN from cert:%s",
  1335. gnutls_strerror(rc));
  1336. }
  1337. /* This function will check if the given certificate's subject matches the
  1338. given hostname. This is a basic implementation of the matching described
  1339. in RFC2818 (HTTPS), which takes into account wildcards, and the subject
  1340. alternative name PKIX extension. Returns non zero on success, and zero on
  1341. failure. */
  1342. /* This function does not handle trailing dots, so if we have an SNI name
  1343. use that and fallback to the hostname only if there is no SNI (like for
  1344. IP addresses) */
  1345. rc = (int)gnutls_x509_crt_check_hostname(x509_cert,
  1346. peer->sni ? peer->sni :
  1347. peer->hostname);
  1348. #if GNUTLS_VERSION_NUMBER < 0x030306
  1349. /* Before 3.3.6, gnutls_x509_crt_check_hostname() did not check IP
  1350. addresses. */
  1351. if(!rc) {
  1352. #ifdef USE_IPV6
  1353. #define use_addr in6_addr
  1354. #else
  1355. #define use_addr in_addr
  1356. #endif
  1357. unsigned char addrbuf[sizeof(struct use_addr)];
  1358. size_t addrlen = 0;
  1359. if(Curl_inet_pton(AF_INET, peer->hostname, addrbuf) > 0)
  1360. addrlen = 4;
  1361. #ifdef USE_IPV6
  1362. else if(Curl_inet_pton(AF_INET6, peer->hostname, addrbuf) > 0)
  1363. addrlen = 16;
  1364. #endif
  1365. if(addrlen) {
  1366. unsigned char certaddr[sizeof(struct use_addr)];
  1367. int i;
  1368. for(i = 0; ; i++) {
  1369. size_t certaddrlen = sizeof(certaddr);
  1370. int ret = gnutls_x509_crt_get_subject_alt_name(x509_cert, i, certaddr,
  1371. &certaddrlen, NULL);
  1372. /* If this happens, it was not an IP address. */
  1373. if(ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
  1374. continue;
  1375. if(ret < 0)
  1376. break;
  1377. if(ret != GNUTLS_SAN_IPADDRESS)
  1378. continue;
  1379. if(certaddrlen == addrlen && !memcmp(addrbuf, certaddr, addrlen)) {
  1380. rc = 1;
  1381. break;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. #endif
  1387. if(!rc) {
  1388. if(config->verifyhost) {
  1389. failf(data, "SSL: certificate subject name (%s) does not match "
  1390. "target hostname '%s'", certname, peer->dispname);
  1391. gnutls_x509_crt_deinit(x509_cert);
  1392. return CURLE_PEER_FAILED_VERIFICATION;
  1393. }
  1394. else
  1395. infof(data, " common name: %s (does not match '%s')",
  1396. certname, peer->dispname);
  1397. }
  1398. else
  1399. infof(data, " common name: %s (matched)", certname);
  1400. /* Check for time-based validity */
  1401. certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
  1402. if(certclock == (time_t)-1) {
  1403. if(config->verifypeer) {
  1404. failf(data, "server cert expiration date verify failed");
  1405. *certverifyresult = GNUTLS_CERT_EXPIRED;
  1406. gnutls_x509_crt_deinit(x509_cert);
  1407. return CURLE_SSL_CONNECT_ERROR;
  1408. }
  1409. else
  1410. infof(data, " server certificate expiration date verify FAILED");
  1411. }
  1412. else {
  1413. if(certclock < time(NULL)) {
  1414. if(config->verifypeer) {
  1415. failf(data, "server certificate expiration date has passed.");
  1416. *certverifyresult = GNUTLS_CERT_EXPIRED;
  1417. gnutls_x509_crt_deinit(x509_cert);
  1418. return CURLE_PEER_FAILED_VERIFICATION;
  1419. }
  1420. else
  1421. infof(data, " server certificate expiration date FAILED");
  1422. }
  1423. else
  1424. infof(data, " server certificate expiration date OK");
  1425. }
  1426. certclock = gnutls_x509_crt_get_activation_time(x509_cert);
  1427. if(certclock == (time_t)-1) {
  1428. if(config->verifypeer) {
  1429. failf(data, "server cert activation date verify failed");
  1430. *certverifyresult = GNUTLS_CERT_NOT_ACTIVATED;
  1431. gnutls_x509_crt_deinit(x509_cert);
  1432. return CURLE_SSL_CONNECT_ERROR;
  1433. }
  1434. else
  1435. infof(data, " server certificate activation date verify FAILED");
  1436. }
  1437. else {
  1438. if(certclock > time(NULL)) {
  1439. if(config->verifypeer) {
  1440. failf(data, "server certificate not activated yet.");
  1441. *certverifyresult = GNUTLS_CERT_NOT_ACTIVATED;
  1442. gnutls_x509_crt_deinit(x509_cert);
  1443. return CURLE_PEER_FAILED_VERIFICATION;
  1444. }
  1445. else
  1446. infof(data, " server certificate activation date FAILED");
  1447. }
  1448. else
  1449. infof(data, " server certificate activation date OK");
  1450. }
  1451. if(pinned_key) {
  1452. result = pkp_pin_peer_pubkey(data, x509_cert, pinned_key);
  1453. if(result != CURLE_OK) {
  1454. failf(data, "SSL: public key does not match pinned public key");
  1455. gnutls_x509_crt_deinit(x509_cert);
  1456. return result;
  1457. }
  1458. }
  1459. /* Show:
  1460. - subject
  1461. - start date
  1462. - expire date
  1463. - common name
  1464. - issuer
  1465. */
  1466. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1467. /* public key algorithm's parameters */
  1468. algo = gnutls_x509_crt_get_pk_algorithm(x509_cert, &bits);
  1469. infof(data, " certificate public key: %s",
  1470. gnutls_pk_algorithm_get_name((gnutls_pk_algorithm_t)algo));
  1471. /* version of the X.509 certificate. */
  1472. infof(data, " certificate version: #%d",
  1473. gnutls_x509_crt_get_version(x509_cert));
  1474. rc = gnutls_x509_crt_get_dn2(x509_cert, &certfields);
  1475. if(rc)
  1476. infof(data, "Failed to get certificate name");
  1477. else {
  1478. infof(data, " subject: %s", certfields.data);
  1479. certclock = gnutls_x509_crt_get_activation_time(x509_cert);
  1480. showtime(data, "start date", certclock);
  1481. certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
  1482. showtime(data, "expire date", certclock);
  1483. gnutls_free(certfields.data);
  1484. }
  1485. rc = gnutls_x509_crt_get_issuer_dn2(x509_cert, &certfields);
  1486. if(rc)
  1487. infof(data, "Failed to get certificate issuer");
  1488. else {
  1489. infof(data, " issuer: %s", certfields.data);
  1490. gnutls_free(certfields.data);
  1491. }
  1492. #endif
  1493. gnutls_x509_crt_deinit(x509_cert);
  1494. return result;
  1495. }
  1496. static CURLcode gtls_verifyserver(struct Curl_cfilter *cf,
  1497. struct Curl_easy *data,
  1498. gnutls_session_t session)
  1499. {
  1500. struct ssl_connect_data *connssl = cf->ctx;
  1501. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  1502. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1503. #ifndef CURL_DISABLE_PROXY
  1504. const char *pinned_key = Curl_ssl_cf_is_proxy(cf) ?
  1505. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
  1506. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  1507. #else
  1508. const char *pinned_key = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  1509. #endif
  1510. CURLcode result;
  1511. result = Curl_gtls_verifyserver(data, session, conn_config, ssl_config,
  1512. &connssl->peer, pinned_key);
  1513. if(result)
  1514. goto out;
  1515. /* Only on TLSv1.2 or lower do we have the session id now. For
  1516. * TLSv1.3 we get it via a SESSION_TICKET message that arrives later. */
  1517. if(gnutls_protocol_get_version(session) < GNUTLS_TLS1_3)
  1518. result = cf_gtls_update_session_id(cf, data, session);
  1519. out:
  1520. return result;
  1521. }
  1522. static CURLcode gtls_set_earlydata(struct Curl_cfilter *cf,
  1523. struct Curl_easy *data,
  1524. const void *buf, size_t blen)
  1525. {
  1526. struct ssl_connect_data *connssl = cf->ctx;
  1527. ssize_t nwritten = 0;
  1528. CURLcode result = CURLE_OK;
  1529. DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_use);
  1530. DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata));
  1531. if(blen) {
  1532. if(blen > connssl->earlydata_max)
  1533. blen = connssl->earlydata_max;
  1534. nwritten = Curl_bufq_write(&connssl->earlydata, buf, blen, &result);
  1535. CURL_TRC_CF(data, cf, "gtls_set_earlydata(len=%zu) -> %zd",
  1536. blen, nwritten);
  1537. if(nwritten < 0)
  1538. return result;
  1539. }
  1540. connssl->earlydata_state = ssl_earlydata_sending;
  1541. connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata);
  1542. return CURLE_OK;
  1543. }
  1544. static CURLcode gtls_send_earlydata(struct Curl_cfilter *cf,
  1545. struct Curl_easy *data)
  1546. {
  1547. struct ssl_connect_data *connssl = cf->ctx;
  1548. struct gtls_ssl_backend_data *backend =
  1549. (struct gtls_ssl_backend_data *)connssl->backend;
  1550. CURLcode result = CURLE_OK;
  1551. const unsigned char *buf;
  1552. size_t blen;
  1553. ssize_t n;
  1554. DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sending);
  1555. backend->gtls.io_result = CURLE_OK;
  1556. while(Curl_bufq_peek(&connssl->earlydata, &buf, &blen)) {
  1557. n = gnutls_record_send_early_data(backend->gtls.session, buf, blen);
  1558. CURL_TRC_CF(data, cf, "gtls_send_earlydata(len=%zu) -> %zd",
  1559. blen, n);
  1560. if(n < 0) {
  1561. if(n == GNUTLS_E_AGAIN)
  1562. result = CURLE_AGAIN;
  1563. else
  1564. result = backend->gtls.io_result ?
  1565. backend->gtls.io_result : CURLE_SEND_ERROR;
  1566. goto out;
  1567. }
  1568. else if(!n) {
  1569. /* gnutls is buggy, it *SHOULD* return the amount of bytes it took in.
  1570. * Instead it returns 0 if everything was written. */
  1571. n = (ssize_t)blen;
  1572. }
  1573. Curl_bufq_skip(&connssl->earlydata, (size_t)n);
  1574. }
  1575. /* sent everything there was */
  1576. infof(data, "SSL sending %" FMT_OFF_T " bytes of early data",
  1577. connssl->earlydata_skip);
  1578. out:
  1579. return result;
  1580. }
  1581. /*
  1582. * This function is called after the TCP connect has completed. Setup the TLS
  1583. * layer and do all necessary magic.
  1584. */
  1585. /* We use connssl->connecting_state to keep track of the connection status;
  1586. there are three states: 'ssl_connect_1' (not started yet or complete),
  1587. 'ssl_connect_2' (doing handshake with the server), and
  1588. 'ssl_connect_3' (verifying and getting stats).
  1589. */
  1590. static CURLcode
  1591. gtls_connect_common(struct Curl_cfilter *cf,
  1592. struct Curl_easy *data,
  1593. bool nonblocking,
  1594. bool *done) {
  1595. struct ssl_connect_data *connssl = cf->ctx;
  1596. struct gtls_ssl_backend_data *backend =
  1597. (struct gtls_ssl_backend_data *)connssl->backend;
  1598. CURLcode result = CURLE_OK;
  1599. DEBUGASSERT(backend);
  1600. /* Initiate the connection, if not already done */
  1601. if(connssl->connecting_state == ssl_connect_1) {
  1602. result = gtls_connect_step1(cf, data);
  1603. if(result)
  1604. goto out;
  1605. connssl->connecting_state = ssl_connect_2;
  1606. }
  1607. if(connssl->connecting_state == ssl_connect_2) {
  1608. if(connssl->earlydata_state == ssl_earlydata_use) {
  1609. goto out;
  1610. }
  1611. else if(connssl->earlydata_state == ssl_earlydata_sending) {
  1612. result = gtls_send_earlydata(cf, data);
  1613. if(result)
  1614. goto out;
  1615. connssl->earlydata_state = ssl_earlydata_sent;
  1616. if(!Curl_ssl_cf_is_proxy(cf))
  1617. Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip);
  1618. }
  1619. DEBUGASSERT((connssl->earlydata_state == ssl_earlydata_none) ||
  1620. (connssl->earlydata_state == ssl_earlydata_sent));
  1621. result = handshake(cf, data, TRUE, nonblocking);
  1622. if(result)
  1623. goto out;
  1624. connssl->connecting_state = ssl_connect_3;
  1625. }
  1626. /* Finish connecting once the handshake is done */
  1627. if(connssl->connecting_state == ssl_connect_3) {
  1628. gnutls_datum_t proto;
  1629. int rc;
  1630. result = gtls_verifyserver(cf, data, backend->gtls.session);
  1631. if(result)
  1632. goto out;
  1633. connssl->state = ssl_connection_complete;
  1634. connssl->connecting_state = ssl_connect_1;
  1635. rc = gnutls_alpn_get_selected_protocol(backend->gtls.session, &proto);
  1636. if(rc) { /* No ALPN from server */
  1637. proto.data = NULL;
  1638. proto.size = 0;
  1639. }
  1640. result = Curl_alpn_set_negotiated(cf, data, connssl,
  1641. proto.data, proto.size);
  1642. if(result)
  1643. goto out;
  1644. if(connssl->earlydata_state == ssl_earlydata_sent) {
  1645. if(gnutls_session_get_flags(backend->gtls.session) &
  1646. GNUTLS_SFLAGS_EARLY_DATA) {
  1647. connssl->earlydata_state = ssl_earlydata_accepted;
  1648. infof(data, "Server accepted %zu bytes of TLS early data.",
  1649. connssl->earlydata_skip);
  1650. }
  1651. else {
  1652. connssl->earlydata_state = ssl_earlydata_rejected;
  1653. if(!Curl_ssl_cf_is_proxy(cf))
  1654. Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip);
  1655. infof(data, "Server rejected TLS early data.");
  1656. connssl->earlydata_skip = 0;
  1657. }
  1658. }
  1659. }
  1660. out:
  1661. if(result == CURLE_AGAIN) {
  1662. *done = FALSE;
  1663. return CURLE_OK;
  1664. }
  1665. *done = ((connssl->connecting_state == ssl_connect_1) ||
  1666. (connssl->state == ssl_connection_deferred));
  1667. return result;
  1668. }
  1669. static CURLcode gtls_connect_nonblocking(struct Curl_cfilter *cf,
  1670. struct Curl_easy *data,
  1671. bool *done)
  1672. {
  1673. struct ssl_connect_data *connssl = cf->ctx;
  1674. if(connssl->state == ssl_connection_deferred) {
  1675. /* We refuse to be pushed, we are waiting for someone to send/recv. */
  1676. *done = TRUE;
  1677. return CURLE_OK;
  1678. }
  1679. return gtls_connect_common(cf, data, TRUE, done);
  1680. }
  1681. static CURLcode gtls_connect(struct Curl_cfilter *cf,
  1682. struct Curl_easy *data)
  1683. {
  1684. CURLcode result;
  1685. bool done = FALSE;
  1686. result = gtls_connect_common(cf, data, FALSE, &done);
  1687. if(result)
  1688. return result;
  1689. DEBUGASSERT(done);
  1690. return CURLE_OK;
  1691. }
  1692. static CURLcode gtls_connect_deferred(struct Curl_cfilter *cf,
  1693. struct Curl_easy *data,
  1694. const void *buf,
  1695. size_t blen,
  1696. bool *done)
  1697. {
  1698. struct ssl_connect_data *connssl = cf->ctx;
  1699. CURLcode result = CURLE_OK;
  1700. DEBUGASSERT(connssl->state == ssl_connection_deferred);
  1701. *done = FALSE;
  1702. if(connssl->earlydata_state == ssl_earlydata_use) {
  1703. result = gtls_set_earlydata(cf, data, buf, blen);
  1704. if(result)
  1705. return result;
  1706. }
  1707. return gtls_connect_common(cf, data, TRUE, done);
  1708. }
  1709. static bool gtls_data_pending(struct Curl_cfilter *cf,
  1710. const struct Curl_easy *data)
  1711. {
  1712. struct ssl_connect_data *ctx = cf->ctx;
  1713. struct gtls_ssl_backend_data *backend;
  1714. (void)data;
  1715. DEBUGASSERT(ctx && ctx->backend);
  1716. backend = (struct gtls_ssl_backend_data *)ctx->backend;
  1717. if(backend->gtls.session &&
  1718. 0 != gnutls_record_check_pending(backend->gtls.session))
  1719. return TRUE;
  1720. return FALSE;
  1721. }
  1722. static ssize_t gtls_send(struct Curl_cfilter *cf,
  1723. struct Curl_easy *data,
  1724. const void *buf,
  1725. size_t blen,
  1726. CURLcode *curlcode)
  1727. {
  1728. struct ssl_connect_data *connssl = cf->ctx;
  1729. struct gtls_ssl_backend_data *backend =
  1730. (struct gtls_ssl_backend_data *)connssl->backend;
  1731. ssize_t rc;
  1732. size_t nwritten, total_written = 0;
  1733. DEBUGASSERT(backend);
  1734. if(connssl->state == ssl_connection_deferred) {
  1735. bool done = FALSE;
  1736. *curlcode = gtls_connect_deferred(cf, data, buf, blen, &done);
  1737. if(*curlcode) {
  1738. rc = -1;
  1739. goto out;
  1740. }
  1741. else if(!done) {
  1742. *curlcode = CURLE_AGAIN;
  1743. rc = -1;
  1744. goto out;
  1745. }
  1746. DEBUGASSERT(connssl->state == ssl_connection_complete);
  1747. }
  1748. if(connssl->earlydata_skip) {
  1749. if(connssl->earlydata_skip >= blen) {
  1750. connssl->earlydata_skip -= blen;
  1751. *curlcode = CURLE_OK;
  1752. rc = (ssize_t)blen;
  1753. goto out;
  1754. }
  1755. else {
  1756. total_written += connssl->earlydata_skip;
  1757. buf = ((const char *)buf) + connssl->earlydata_skip;
  1758. blen -= connssl->earlydata_skip;
  1759. connssl->earlydata_skip = 0;
  1760. }
  1761. }
  1762. while(blen) {
  1763. backend->gtls.io_result = CURLE_OK;
  1764. rc = gnutls_record_send(backend->gtls.session, buf, blen);
  1765. if(rc < 0) {
  1766. if(total_written && (rc == GNUTLS_E_AGAIN)) {
  1767. *curlcode = CURLE_OK;
  1768. rc = (ssize_t)total_written;
  1769. goto out;
  1770. }
  1771. *curlcode = (rc == GNUTLS_E_AGAIN) ?
  1772. CURLE_AGAIN :
  1773. (backend->gtls.io_result ? backend->gtls.io_result : CURLE_SEND_ERROR);
  1774. rc = -1;
  1775. goto out;
  1776. }
  1777. nwritten = (size_t)rc;
  1778. total_written += nwritten;
  1779. DEBUGASSERT(nwritten <= blen);
  1780. buf = (char *)buf + nwritten;
  1781. blen -= nwritten;
  1782. }
  1783. rc = total_written;
  1784. out:
  1785. return rc;
  1786. }
  1787. /*
  1788. * This function is called to shut down the SSL layer but keep the
  1789. * socket open (CCC - Clear Command Channel)
  1790. */
  1791. static CURLcode gtls_shutdown(struct Curl_cfilter *cf,
  1792. struct Curl_easy *data,
  1793. bool send_shutdown, bool *done)
  1794. {
  1795. struct ssl_connect_data *connssl = cf->ctx;
  1796. struct gtls_ssl_backend_data *backend =
  1797. (struct gtls_ssl_backend_data *)connssl->backend;
  1798. char buf[1024];
  1799. CURLcode result = CURLE_OK;
  1800. ssize_t nread;
  1801. size_t i;
  1802. DEBUGASSERT(backend);
  1803. /* If we have no handshaked connection or already shut down */
  1804. if(!backend->gtls.session || cf->shutdown ||
  1805. connssl->state != ssl_connection_complete) {
  1806. *done = TRUE;
  1807. goto out;
  1808. }
  1809. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  1810. *done = FALSE;
  1811. if(!backend->gtls.sent_shutdown) {
  1812. /* do this only once */
  1813. backend->gtls.sent_shutdown = TRUE;
  1814. if(send_shutdown) {
  1815. int ret = gnutls_bye(backend->gtls.session, GNUTLS_SHUT_RDWR);
  1816. if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
  1817. CURL_TRC_CF(data, cf, "SSL shutdown, gnutls_bye EAGAIN");
  1818. connssl->io_need = gnutls_record_get_direction(backend->gtls.session) ?
  1819. CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
  1820. backend->gtls.sent_shutdown = FALSE;
  1821. result = CURLE_OK;
  1822. goto out;
  1823. }
  1824. if(ret != GNUTLS_E_SUCCESS) {
  1825. CURL_TRC_CF(data, cf, "SSL shutdown, gnutls_bye error: '%s'(%d)",
  1826. gnutls_strerror((int)ret), (int)ret);
  1827. result = CURLE_RECV_ERROR;
  1828. goto out;
  1829. }
  1830. }
  1831. }
  1832. /* SSL should now have started the shutdown from our side. Since it
  1833. * was not complete, we are lacking the close notify from the server. */
  1834. for(i = 0; i < 10; ++i) {
  1835. nread = gnutls_record_recv(backend->gtls.session, buf, sizeof(buf));
  1836. if(nread <= 0)
  1837. break;
  1838. }
  1839. if(nread > 0) {
  1840. /* still data coming in? */
  1841. }
  1842. else if(nread == 0) {
  1843. /* We got the close notify alert and are done. */
  1844. *done = TRUE;
  1845. }
  1846. else if((nread == GNUTLS_E_AGAIN) || (nread == GNUTLS_E_INTERRUPTED)) {
  1847. connssl->io_need = gnutls_record_get_direction(backend->gtls.session) ?
  1848. CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
  1849. }
  1850. else {
  1851. CURL_TRC_CF(data, cf, "SSL shutdown, error: '%s'(%d)",
  1852. gnutls_strerror((int)nread), (int)nread);
  1853. result = CURLE_RECV_ERROR;
  1854. }
  1855. out:
  1856. cf->shutdown = (result || *done);
  1857. return result;
  1858. }
  1859. static void gtls_close(struct Curl_cfilter *cf,
  1860. struct Curl_easy *data)
  1861. {
  1862. struct ssl_connect_data *connssl = cf->ctx;
  1863. struct gtls_ssl_backend_data *backend =
  1864. (struct gtls_ssl_backend_data *)connssl->backend;
  1865. (void) data;
  1866. DEBUGASSERT(backend);
  1867. CURL_TRC_CF(data, cf, "close");
  1868. if(backend->gtls.session) {
  1869. gnutls_deinit(backend->gtls.session);
  1870. backend->gtls.session = NULL;
  1871. }
  1872. if(backend->gtls.shared_creds) {
  1873. Curl_gtls_shared_creds_free(&backend->gtls.shared_creds);
  1874. }
  1875. #ifdef USE_GNUTLS_SRP
  1876. if(backend->gtls.srp_client_cred) {
  1877. gnutls_srp_free_client_credentials(backend->gtls.srp_client_cred);
  1878. backend->gtls.srp_client_cred = NULL;
  1879. }
  1880. #endif
  1881. }
  1882. static ssize_t gtls_recv(struct Curl_cfilter *cf,
  1883. struct Curl_easy *data,
  1884. char *buf,
  1885. size_t buffersize,
  1886. CURLcode *curlcode)
  1887. {
  1888. struct ssl_connect_data *connssl = cf->ctx;
  1889. struct gtls_ssl_backend_data *backend =
  1890. (struct gtls_ssl_backend_data *)connssl->backend;
  1891. ssize_t ret;
  1892. (void)data;
  1893. DEBUGASSERT(backend);
  1894. if(connssl->state == ssl_connection_deferred) {
  1895. bool done = FALSE;
  1896. *curlcode = gtls_connect_deferred(cf, data, NULL, 0, &done);
  1897. if(*curlcode) {
  1898. ret = -1;
  1899. goto out;
  1900. }
  1901. else if(!done) {
  1902. *curlcode = CURLE_AGAIN;
  1903. ret = -1;
  1904. goto out;
  1905. }
  1906. DEBUGASSERT(connssl->state == ssl_connection_complete);
  1907. }
  1908. ret = gnutls_record_recv(backend->gtls.session, buf, buffersize);
  1909. if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
  1910. *curlcode = CURLE_AGAIN;
  1911. ret = -1;
  1912. goto out;
  1913. }
  1914. if(ret == GNUTLS_E_REHANDSHAKE) {
  1915. /* BLOCKING call, this is bad but a work-around for now. Fixing this "the
  1916. proper way" takes a whole lot of work. */
  1917. CURLcode result = handshake(cf, data, FALSE, FALSE);
  1918. if(result)
  1919. /* handshake() writes error message on its own */
  1920. *curlcode = result;
  1921. else
  1922. *curlcode = CURLE_AGAIN; /* then return as if this was a wouldblock */
  1923. ret = -1;
  1924. goto out;
  1925. }
  1926. if(ret < 0) {
  1927. failf(data, "GnuTLS recv error (%d): %s",
  1928. (int)ret, gnutls_strerror((int)ret));
  1929. *curlcode = backend->gtls.io_result ?
  1930. backend->gtls.io_result : CURLE_RECV_ERROR;
  1931. ret = -1;
  1932. goto out;
  1933. }
  1934. out:
  1935. return ret;
  1936. }
  1937. static size_t gtls_version(char *buffer, size_t size)
  1938. {
  1939. return msnprintf(buffer, size, "GnuTLS/%s", gnutls_check_version(NULL));
  1940. }
  1941. /* data might be NULL! */
  1942. static CURLcode gtls_random(struct Curl_easy *data,
  1943. unsigned char *entropy, size_t length)
  1944. {
  1945. int rc;
  1946. (void)data;
  1947. rc = gnutls_rnd(GNUTLS_RND_RANDOM, entropy, length);
  1948. return rc ? CURLE_FAILED_INIT : CURLE_OK;
  1949. }
  1950. static CURLcode gtls_sha256sum(const unsigned char *tmp, /* input */
  1951. size_t tmplen,
  1952. unsigned char *sha256sum, /* output */
  1953. size_t sha256len)
  1954. {
  1955. struct sha256_ctx SHA256pw;
  1956. sha256_init(&SHA256pw);
  1957. sha256_update(&SHA256pw, (unsigned int)tmplen, tmp);
  1958. sha256_digest(&SHA256pw, (unsigned int)sha256len, sha256sum);
  1959. return CURLE_OK;
  1960. }
  1961. static bool gtls_cert_status_request(void)
  1962. {
  1963. return TRUE;
  1964. }
  1965. static void *gtls_get_internals(struct ssl_connect_data *connssl,
  1966. CURLINFO info UNUSED_PARAM)
  1967. {
  1968. struct gtls_ssl_backend_data *backend =
  1969. (struct gtls_ssl_backend_data *)connssl->backend;
  1970. (void)info;
  1971. DEBUGASSERT(backend);
  1972. return backend->gtls.session;
  1973. }
  1974. const struct Curl_ssl Curl_ssl_gnutls = {
  1975. { CURLSSLBACKEND_GNUTLS, "gnutls" }, /* info */
  1976. SSLSUPP_CA_PATH |
  1977. SSLSUPP_CERTINFO |
  1978. SSLSUPP_PINNEDPUBKEY |
  1979. SSLSUPP_HTTPS_PROXY |
  1980. SSLSUPP_CA_CACHE,
  1981. sizeof(struct gtls_ssl_backend_data),
  1982. gtls_init, /* init */
  1983. gtls_cleanup, /* cleanup */
  1984. gtls_version, /* version */
  1985. Curl_none_check_cxn, /* check_cxn */
  1986. gtls_shutdown, /* shutdown */
  1987. gtls_data_pending, /* data_pending */
  1988. gtls_random, /* random */
  1989. gtls_cert_status_request, /* cert_status_request */
  1990. gtls_connect, /* connect */
  1991. gtls_connect_nonblocking, /* connect_nonblocking */
  1992. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1993. gtls_get_internals, /* get_internals */
  1994. gtls_close, /* close_one */
  1995. Curl_none_close_all, /* close_all */
  1996. Curl_none_set_engine, /* set_engine */
  1997. Curl_none_set_engine_default, /* set_engine_default */
  1998. Curl_none_engines_list, /* engines_list */
  1999. Curl_none_false_start, /* false_start */
  2000. gtls_sha256sum, /* sha256sum */
  2001. NULL, /* associate_connection */
  2002. NULL, /* disassociate_connection */
  2003. gtls_recv, /* recv decrypted data */
  2004. gtls_send, /* send data to encrypt */
  2005. NULL, /* get_channel_binding */
  2006. };
  2007. #endif /* USE_GNUTLS */