2
0

gtls.c 74 KB

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