gtls.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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: don't 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 "select.h"
  49. #include "strcase.h"
  50. #include "warnless.h"
  51. #include "x509asn1.h"
  52. #include "multiif.h"
  53. #include "curl_printf.h"
  54. #include "curl_memory.h"
  55. /* The last #include file should be: */
  56. #include "memdebug.h"
  57. #ifndef ARRAYSIZE
  58. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  59. #endif
  60. #define QUIC_PRIORITY \
  61. "NORMAL:-VERS-ALL:+VERS-TLS1.3:-CIPHER-ALL:+AES-128-GCM:+AES-256-GCM:" \
  62. "+CHACHA20-POLY1305:+AES-128-CCM:-GROUP-ALL:+GROUP-SECP256R1:" \
  63. "+GROUP-X25519:+GROUP-SECP384R1:+GROUP-SECP521R1:" \
  64. "%DISABLE_TLS13_COMPAT_MODE"
  65. /* Enable GnuTLS debugging by defining GTLSDEBUG */
  66. /*#define GTLSDEBUG */
  67. #ifdef GTLSDEBUG
  68. static void tls_log_func(int level, const char *str)
  69. {
  70. fprintf(stderr, "|<%d>| %s", level, str);
  71. }
  72. #endif
  73. static bool gtls_inited = FALSE;
  74. #if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x03010a)
  75. #error "too old GnuTLS version"
  76. #endif
  77. # include <gnutls/ocsp.h>
  78. struct gtls_ssl_backend_data {
  79. struct gtls_ctx gtls;
  80. };
  81. static ssize_t gtls_push(void *s, const void *buf, size_t blen)
  82. {
  83. struct Curl_cfilter *cf = s;
  84. struct ssl_connect_data *connssl = cf->ctx;
  85. struct gtls_ssl_backend_data *backend =
  86. (struct gtls_ssl_backend_data *)connssl->backend;
  87. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  88. ssize_t nwritten;
  89. CURLcode result;
  90. DEBUGASSERT(data);
  91. nwritten = Curl_conn_cf_send(cf->next, data, buf, blen, &result);
  92. CURL_TRC_CF(data, cf, "gtls_push(len=%zu) -> %zd, err=%d",
  93. blen, nwritten, result);
  94. backend->gtls.io_result = result;
  95. if(nwritten < 0) {
  96. gnutls_transport_set_errno(backend->gtls.session,
  97. (CURLE_AGAIN == result)? EAGAIN : EINVAL);
  98. nwritten = -1;
  99. }
  100. return nwritten;
  101. }
  102. static ssize_t gtls_pull(void *s, void *buf, size_t blen)
  103. {
  104. struct Curl_cfilter *cf = s;
  105. struct ssl_connect_data *connssl = cf->ctx;
  106. struct gtls_ssl_backend_data *backend =
  107. (struct gtls_ssl_backend_data *)connssl->backend;
  108. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  109. ssize_t nread;
  110. CURLcode result;
  111. DEBUGASSERT(data);
  112. if(!backend->gtls.trust_setup) {
  113. result = Curl_gtls_client_trust_setup(cf, data, &backend->gtls);
  114. if(result) {
  115. gnutls_transport_set_errno(backend->gtls.session, EINVAL);
  116. backend->gtls.io_result = result;
  117. return -1;
  118. }
  119. }
  120. nread = Curl_conn_cf_recv(cf->next, data, buf, blen, &result);
  121. CURL_TRC_CF(data, cf, "glts_pull(len=%zu) -> %zd, err=%d",
  122. blen, nread, result);
  123. backend->gtls.io_result = result;
  124. if(nread < 0) {
  125. gnutls_transport_set_errno(backend->gtls.session,
  126. (CURLE_AGAIN == result)? EAGAIN : EINVAL);
  127. nread = -1;
  128. }
  129. else if(nread == 0)
  130. connssl->peer_closed = TRUE;
  131. return nread;
  132. }
  133. /* gtls_init()
  134. *
  135. * Global GnuTLS init, called from Curl_ssl_init(). This calls functions that
  136. * are not thread-safe and thus this function itself is not thread-safe and
  137. * must only be called from within curl_global_init() to keep the thread
  138. * situation under control!
  139. */
  140. static int gtls_init(void)
  141. {
  142. int ret = 1;
  143. if(!gtls_inited) {
  144. ret = gnutls_global_init()?0:1;
  145. #ifdef GTLSDEBUG
  146. gnutls_global_set_log_function(tls_log_func);
  147. gnutls_global_set_log_level(2);
  148. #endif
  149. gtls_inited = TRUE;
  150. }
  151. return ret;
  152. }
  153. static void gtls_cleanup(void)
  154. {
  155. if(gtls_inited) {
  156. gnutls_global_deinit();
  157. gtls_inited = FALSE;
  158. }
  159. }
  160. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  161. static void showtime(struct Curl_easy *data,
  162. const char *text,
  163. time_t stamp)
  164. {
  165. struct tm buffer;
  166. const struct tm *tm = &buffer;
  167. char str[96];
  168. CURLcode result = Curl_gmtime(stamp, &buffer);
  169. if(result)
  170. return;
  171. msnprintf(str,
  172. sizeof(str),
  173. " %s: %s, %02d %s %4d %02d:%02d:%02d GMT",
  174. text,
  175. Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
  176. tm->tm_mday,
  177. Curl_month[tm->tm_mon],
  178. tm->tm_year + 1900,
  179. tm->tm_hour,
  180. tm->tm_min,
  181. tm->tm_sec);
  182. infof(data, "%s", str);
  183. }
  184. #endif
  185. static gnutls_datum_t load_file(const char *file)
  186. {
  187. FILE *f;
  188. gnutls_datum_t loaded_file = { NULL, 0 };
  189. long filelen;
  190. void *ptr;
  191. f = fopen(file, "rb");
  192. if(!f)
  193. return loaded_file;
  194. if(fseek(f, 0, SEEK_END) != 0
  195. || (filelen = ftell(f)) < 0
  196. || fseek(f, 0, SEEK_SET) != 0
  197. || !(ptr = malloc((size_t)filelen)))
  198. goto out;
  199. if(fread(ptr, 1, (size_t)filelen, f) < (size_t)filelen) {
  200. free(ptr);
  201. goto out;
  202. }
  203. loaded_file.data = ptr;
  204. loaded_file.size = (unsigned int)filelen;
  205. out:
  206. fclose(f);
  207. return loaded_file;
  208. }
  209. static void unload_file(gnutls_datum_t data)
  210. {
  211. free(data.data);
  212. }
  213. /* this function does a SSL/TLS (re-)handshake */
  214. static CURLcode handshake(struct Curl_cfilter *cf,
  215. struct Curl_easy *data,
  216. bool duringconnect,
  217. bool nonblocking)
  218. {
  219. struct ssl_connect_data *connssl = cf->ctx;
  220. struct gtls_ssl_backend_data *backend =
  221. (struct gtls_ssl_backend_data *)connssl->backend;
  222. gnutls_session_t session;
  223. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  224. DEBUGASSERT(backend);
  225. session = backend->gtls.session;
  226. for(;;) {
  227. timediff_t timeout_ms;
  228. int rc;
  229. /* check allowed time left */
  230. timeout_ms = Curl_timeleft(data, NULL, duringconnect);
  231. if(timeout_ms < 0) {
  232. /* no need to continue if time already is up */
  233. failf(data, "SSL connection timeout");
  234. return CURLE_OPERATION_TIMEDOUT;
  235. }
  236. /* if ssl is expecting something, check if it's available. */
  237. if(connssl->connecting_state == ssl_connect_2_reading
  238. || connssl->connecting_state == ssl_connect_2_writing) {
  239. int what;
  240. curl_socket_t writefd = ssl_connect_2_writing ==
  241. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  242. curl_socket_t readfd = ssl_connect_2_reading ==
  243. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  244. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  245. nonblocking?0:
  246. timeout_ms?timeout_ms:1000);
  247. if(what < 0) {
  248. /* fatal error */
  249. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  250. return CURLE_SSL_CONNECT_ERROR;
  251. }
  252. else if(0 == what) {
  253. if(nonblocking)
  254. return CURLE_OK;
  255. else if(timeout_ms) {
  256. /* timeout */
  257. failf(data, "SSL connection timeout at %ld", (long)timeout_ms);
  258. return CURLE_OPERATION_TIMEDOUT;
  259. }
  260. }
  261. /* socket is readable or writable */
  262. }
  263. backend->gtls.io_result = CURLE_OK;
  264. rc = gnutls_handshake(session);
  265. if(!backend->gtls.trust_setup) {
  266. /* After having send off the ClientHello, we prepare the trust
  267. * store to verify the coming certificate from the server */
  268. CURLcode result = Curl_gtls_client_trust_setup(cf, data, &backend->gtls);
  269. if(result)
  270. return result;
  271. }
  272. if((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED)) {
  273. connssl->connecting_state =
  274. gnutls_record_get_direction(session)?
  275. ssl_connect_2_writing:ssl_connect_2_reading;
  276. continue;
  277. }
  278. else if((rc < 0) && !gnutls_error_is_fatal(rc)) {
  279. const char *strerr = NULL;
  280. if(rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
  281. int alert = gnutls_alert_get(session);
  282. strerr = gnutls_alert_get_name(alert);
  283. }
  284. if(!strerr)
  285. strerr = gnutls_strerror(rc);
  286. infof(data, "gnutls_handshake() warning: %s", strerr);
  287. continue;
  288. }
  289. else if((rc < 0) && backend->gtls.io_result) {
  290. return backend->gtls.io_result;
  291. }
  292. else if(rc < 0) {
  293. const char *strerr = NULL;
  294. if(rc == GNUTLS_E_FATAL_ALERT_RECEIVED) {
  295. int alert = gnutls_alert_get(session);
  296. strerr = gnutls_alert_get_name(alert);
  297. }
  298. if(!strerr)
  299. strerr = gnutls_strerror(rc);
  300. failf(data, "gnutls_handshake() failed: %s", strerr);
  301. return CURLE_SSL_CONNECT_ERROR;
  302. }
  303. /* Reset our connect state machine */
  304. connssl->connecting_state = ssl_connect_1;
  305. return CURLE_OK;
  306. }
  307. }
  308. static gnutls_x509_crt_fmt_t do_file_type(const char *type)
  309. {
  310. if(!type || !type[0])
  311. return GNUTLS_X509_FMT_PEM;
  312. if(strcasecompare(type, "PEM"))
  313. return GNUTLS_X509_FMT_PEM;
  314. if(strcasecompare(type, "DER"))
  315. return GNUTLS_X509_FMT_DER;
  316. return GNUTLS_X509_FMT_PEM; /* default to PEM */
  317. }
  318. #define GNUTLS_CIPHERS "NORMAL:-ARCFOUR-128:-CTYPE-ALL:+CTYPE-X509"
  319. /* If GnuTLS was compiled without support for SRP it will error out if SRP is
  320. requested in the priority string, so treat it specially
  321. */
  322. #define GNUTLS_SRP "+SRP"
  323. static CURLcode
  324. set_ssl_version_min_max(struct Curl_easy *data,
  325. struct ssl_peer *peer,
  326. struct ssl_primary_config *conn_config,
  327. const char **prioritylist,
  328. const char *tls13support)
  329. {
  330. long ssl_version = conn_config->version;
  331. long ssl_version_max = conn_config->version_max;
  332. if(peer->transport == TRNSPRT_QUIC) {
  333. if((ssl_version != CURL_SSLVERSION_DEFAULT) &&
  334. (ssl_version < CURL_SSLVERSION_TLSv1_3)) {
  335. failf(data, "QUIC needs at least TLS version 1.3");
  336. return CURLE_SSL_CONNECT_ERROR;
  337. }
  338. *prioritylist = QUIC_PRIORITY;
  339. return CURLE_OK;
  340. }
  341. if((ssl_version == CURL_SSLVERSION_DEFAULT) ||
  342. (ssl_version == CURL_SSLVERSION_TLSv1))
  343. ssl_version = CURL_SSLVERSION_TLSv1_0;
  344. if(ssl_version_max == CURL_SSLVERSION_MAX_NONE)
  345. ssl_version_max = CURL_SSLVERSION_MAX_DEFAULT;
  346. if(!tls13support) {
  347. /* If the running GnuTLS doesn't support TLS 1.3, we must not specify a
  348. prioritylist involving that since it will make GnuTLS return an en
  349. error back at us */
  350. if((ssl_version_max == CURL_SSLVERSION_MAX_TLSv1_3) ||
  351. (ssl_version_max == CURL_SSLVERSION_MAX_DEFAULT)) {
  352. ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
  353. }
  354. }
  355. else if(ssl_version_max == CURL_SSLVERSION_MAX_DEFAULT) {
  356. ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_3;
  357. }
  358. switch(ssl_version | ssl_version_max) {
  359. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_0:
  360. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  361. "+VERS-TLS1.0";
  362. return CURLE_OK;
  363. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_1:
  364. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  365. "+VERS-TLS1.1:+VERS-TLS1.0";
  366. return CURLE_OK;
  367. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_2:
  368. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  369. "+VERS-TLS1.2:+VERS-TLS1.1:+VERS-TLS1.0";
  370. return CURLE_OK;
  371. case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_1:
  372. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  373. "+VERS-TLS1.1";
  374. return CURLE_OK;
  375. case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_2:
  376. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  377. "+VERS-TLS1.2:+VERS-TLS1.1";
  378. return CURLE_OK;
  379. case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_2:
  380. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  381. "+VERS-TLS1.2";
  382. return CURLE_OK;
  383. case CURL_SSLVERSION_TLSv1_3 | CURL_SSLVERSION_MAX_TLSv1_3:
  384. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  385. "+VERS-TLS1.3";
  386. return CURLE_OK;
  387. case CURL_SSLVERSION_TLSv1_0 | CURL_SSLVERSION_MAX_TLSv1_3:
  388. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0";
  389. return CURLE_OK;
  390. case CURL_SSLVERSION_TLSv1_1 | CURL_SSLVERSION_MAX_TLSv1_3:
  391. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  392. "+VERS-TLS1.3:+VERS-TLS1.2:+VERS-TLS1.1";
  393. return CURLE_OK;
  394. case CURL_SSLVERSION_TLSv1_2 | CURL_SSLVERSION_MAX_TLSv1_3:
  395. *prioritylist = GNUTLS_CIPHERS ":-VERS-SSL3.0:-VERS-TLS-ALL:"
  396. "+VERS-TLS1.3:+VERS-TLS1.2";
  397. return CURLE_OK;
  398. }
  399. failf(data, "GnuTLS: cannot set ssl protocol");
  400. return CURLE_SSL_CONNECT_ERROR;
  401. }
  402. CURLcode Curl_gtls_client_trust_setup(struct Curl_cfilter *cf,
  403. struct Curl_easy *data,
  404. struct gtls_ctx *gtls)
  405. {
  406. struct ssl_primary_config *config = Curl_ssl_cf_get_primary_config(cf);
  407. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  408. int rc;
  409. CURL_TRC_CF(data, cf, "setup trust anchors and CRLs");
  410. if(config->verifypeer) {
  411. bool imported_native_ca = false;
  412. if(ssl_config->native_ca_store) {
  413. rc = gnutls_certificate_set_x509_system_trust(gtls->cred);
  414. if(rc < 0)
  415. infof(data, "error reading native ca store (%s), continuing anyway",
  416. gnutls_strerror(rc));
  417. else {
  418. infof(data, "found %d certificates in native ca store", rc);
  419. if(rc > 0)
  420. imported_native_ca = true;
  421. }
  422. }
  423. if(config->CAfile) {
  424. /* set the trusted CA cert bundle file */
  425. gnutls_certificate_set_verify_flags(gtls->cred,
  426. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
  427. rc = gnutls_certificate_set_x509_trust_file(gtls->cred,
  428. config->CAfile,
  429. GNUTLS_X509_FMT_PEM);
  430. if(rc < 0) {
  431. infof(data, "error reading ca cert file %s (%s)%s",
  432. config->CAfile, gnutls_strerror(rc),
  433. (imported_native_ca ? ", continuing anyway" : ""));
  434. if(!imported_native_ca) {
  435. ssl_config->certverifyresult = rc;
  436. return CURLE_SSL_CACERT_BADFILE;
  437. }
  438. }
  439. else
  440. infof(data, "found %d certificates in %s", rc, config->CAfile);
  441. }
  442. if(config->CApath) {
  443. /* set the trusted CA cert directory */
  444. rc = gnutls_certificate_set_x509_trust_dir(gtls->cred,
  445. config->CApath,
  446. GNUTLS_X509_FMT_PEM);
  447. if(rc < 0) {
  448. infof(data, "error reading ca cert file %s (%s)%s",
  449. config->CApath, gnutls_strerror(rc),
  450. (imported_native_ca ? ", continuing anyway" : ""));
  451. if(!imported_native_ca) {
  452. ssl_config->certverifyresult = rc;
  453. return CURLE_SSL_CACERT_BADFILE;
  454. }
  455. }
  456. else
  457. infof(data, "found %d certificates in %s", rc, config->CApath);
  458. }
  459. }
  460. if(config->CRLfile) {
  461. /* set the CRL list file */
  462. rc = gnutls_certificate_set_x509_crl_file(gtls->cred,
  463. config->CRLfile,
  464. GNUTLS_X509_FMT_PEM);
  465. if(rc < 0) {
  466. failf(data, "error reading crl file %s (%s)",
  467. config->CRLfile, gnutls_strerror(rc));
  468. return CURLE_SSL_CRL_BADFILE;
  469. }
  470. else
  471. infof(data, "found %d CRL in %s", rc, config->CRLfile);
  472. }
  473. gtls->trust_setup = TRUE;
  474. return CURLE_OK;
  475. }
  476. static void gtls_sessionid_free(void *sessionid, size_t idsize)
  477. {
  478. (void)idsize;
  479. free(sessionid);
  480. }
  481. static CURLcode gtls_update_session_id(struct Curl_cfilter *cf,
  482. struct Curl_easy *data,
  483. gnutls_session_t session)
  484. {
  485. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  486. struct ssl_connect_data *connssl = cf->ctx;
  487. CURLcode result = CURLE_OK;
  488. if(ssl_config->primary.sessionid) {
  489. /* we always unconditionally get the session id here, as even if we
  490. already got it from the cache and asked to use it in the connection, it
  491. might've been rejected and then a new one is in use now and we need to
  492. detect that. */
  493. void *connect_sessionid;
  494. size_t connect_idsize = 0;
  495. /* get the session ID data size */
  496. gnutls_session_get_data(session, NULL, &connect_idsize);
  497. connect_sessionid = malloc(connect_idsize); /* get a buffer for it */
  498. if(!connect_sessionid) {
  499. return CURLE_OUT_OF_MEMORY;
  500. }
  501. else {
  502. bool incache;
  503. void *ssl_sessionid;
  504. /* extract session ID to the allocated buffer */
  505. gnutls_session_get_data(session, connect_sessionid, &connect_idsize);
  506. DEBUGF(infof(data, "get session id (len=%zu) and store in cache",
  507. connect_idsize));
  508. Curl_ssl_sessionid_lock(data);
  509. incache = !(Curl_ssl_getsessionid(cf, data, &connssl->peer,
  510. &ssl_sessionid, NULL));
  511. if(incache) {
  512. /* there was one before in the cache, so instead of risking that the
  513. previous one was rejected, we just kill that and store the new */
  514. Curl_ssl_delsessionid(data, ssl_sessionid);
  515. }
  516. /* store this session id, takes ownership */
  517. result = Curl_ssl_addsessionid(cf, data, &connssl->peer,
  518. connect_sessionid, connect_idsize,
  519. gtls_sessionid_free);
  520. Curl_ssl_sessionid_unlock(data);
  521. }
  522. }
  523. return result;
  524. }
  525. static int gtls_handshake_cb(gnutls_session_t session, unsigned int htype,
  526. unsigned when, unsigned int incoming,
  527. const gnutls_datum_t *msg)
  528. {
  529. struct Curl_cfilter *cf = gnutls_session_get_ptr(session);
  530. (void)msg;
  531. (void)incoming;
  532. if(when) { /* after message has been processed */
  533. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  534. if(data) {
  535. DEBUGF(infof(data, "handshake: %s message type %d",
  536. incoming? "incoming" : "outgoing", htype));
  537. switch(htype) {
  538. case GNUTLS_HANDSHAKE_NEW_SESSION_TICKET: {
  539. gtls_update_session_id(cf, data, session);
  540. break;
  541. }
  542. default:
  543. break;
  544. }
  545. }
  546. }
  547. return 0;
  548. }
  549. static CURLcode gtls_client_init(struct Curl_cfilter *cf,
  550. struct Curl_easy *data,
  551. struct ssl_peer *peer,
  552. struct gtls_ctx *gtls)
  553. {
  554. struct ssl_primary_config *config = Curl_ssl_cf_get_primary_config(cf);
  555. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  556. unsigned int init_flags;
  557. int rc;
  558. bool sni = TRUE; /* default is SNI enabled */
  559. const char *prioritylist;
  560. const char *err = NULL;
  561. const char *tls13support;
  562. CURLcode result;
  563. if(!gtls_inited)
  564. gtls_init();
  565. if(config->version == CURL_SSLVERSION_SSLv2) {
  566. failf(data, "GnuTLS does not support SSLv2");
  567. return CURLE_SSL_CONNECT_ERROR;
  568. }
  569. else if(config->version == CURL_SSLVERSION_SSLv3)
  570. sni = FALSE; /* SSLv3 has no SNI */
  571. /* allocate a cred struct */
  572. rc = gnutls_certificate_allocate_credentials(&gtls->cred);
  573. if(rc != GNUTLS_E_SUCCESS) {
  574. failf(data, "gnutls_cert_all_cred() failed: %s", gnutls_strerror(rc));
  575. return CURLE_SSL_CONNECT_ERROR;
  576. }
  577. #ifdef USE_GNUTLS_SRP
  578. if(config->username && Curl_auth_allowed_to_host(data)) {
  579. infof(data, "Using TLS-SRP username: %s", config->username);
  580. rc = gnutls_srp_allocate_client_credentials(&gtls->srp_client_cred);
  581. if(rc != GNUTLS_E_SUCCESS) {
  582. failf(data, "gnutls_srp_allocate_client_cred() failed: %s",
  583. gnutls_strerror(rc));
  584. return CURLE_OUT_OF_MEMORY;
  585. }
  586. rc = gnutls_srp_set_client_credentials(gtls->srp_client_cred,
  587. config->username,
  588. config->password);
  589. if(rc != GNUTLS_E_SUCCESS) {
  590. failf(data, "gnutls_srp_set_client_cred() failed: %s",
  591. gnutls_strerror(rc));
  592. return CURLE_BAD_FUNCTION_ARGUMENT;
  593. }
  594. }
  595. #endif
  596. ssl_config->certverifyresult = 0;
  597. /* Initialize TLS session as a client */
  598. init_flags = GNUTLS_CLIENT;
  599. #if defined(GNUTLS_FORCE_CLIENT_CERT)
  600. init_flags |= GNUTLS_FORCE_CLIENT_CERT;
  601. #endif
  602. #if defined(GNUTLS_NO_TICKETS)
  603. /* Disable TLS session tickets */
  604. init_flags |= GNUTLS_NO_TICKETS;
  605. #endif
  606. rc = gnutls_init(&gtls->session, init_flags);
  607. if(rc != GNUTLS_E_SUCCESS) {
  608. failf(data, "gnutls_init() failed: %d", rc);
  609. return CURLE_SSL_CONNECT_ERROR;
  610. }
  611. if(sni && peer->sni) {
  612. if(gnutls_server_name_set(gtls->session, GNUTLS_NAME_DNS,
  613. peer->sni, strlen(peer->sni)) < 0) {
  614. failf(data, "Failed to set SNI");
  615. return CURLE_SSL_CONNECT_ERROR;
  616. }
  617. }
  618. /* Use default priorities */
  619. rc = gnutls_set_default_priority(gtls->session);
  620. if(rc != GNUTLS_E_SUCCESS)
  621. return CURLE_SSL_CONNECT_ERROR;
  622. /* "In GnuTLS 3.6.5, TLS 1.3 is enabled by default" */
  623. tls13support = gnutls_check_version("3.6.5");
  624. /* Ensure +SRP comes at the *end* of all relevant strings so that it can be
  625. * removed if a run-time error indicates that SRP is not supported by this
  626. * GnuTLS version */
  627. if(config->version == CURL_SSLVERSION_SSLv2 ||
  628. config->version == CURL_SSLVERSION_SSLv3) {
  629. failf(data, "GnuTLS does not support SSLv2 or SSLv3");
  630. return CURLE_SSL_CONNECT_ERROR;
  631. }
  632. if(config->version == CURL_SSLVERSION_TLSv1_3) {
  633. if(!tls13support) {
  634. failf(data, "This GnuTLS installation does not support TLS 1.3");
  635. return CURLE_SSL_CONNECT_ERROR;
  636. }
  637. }
  638. /* At this point we know we have a supported TLS version, so set it */
  639. result = set_ssl_version_min_max(data, peer,
  640. config, &prioritylist, tls13support);
  641. if(result)
  642. return result;
  643. #ifdef USE_GNUTLS_SRP
  644. /* Only add SRP to the cipher list if SRP is requested. Otherwise
  645. * GnuTLS will disable TLS 1.3 support. */
  646. if(config->username) {
  647. char *prioritysrp = aprintf("%s:" GNUTLS_SRP, prioritylist);
  648. if(!prioritysrp)
  649. return CURLE_OUT_OF_MEMORY;
  650. rc = gnutls_priority_set_direct(gtls->session, prioritysrp, &err);
  651. free(prioritysrp);
  652. if((rc == GNUTLS_E_INVALID_REQUEST) && err) {
  653. infof(data, "This GnuTLS does not support SRP");
  654. }
  655. }
  656. else {
  657. #endif
  658. infof(data, "GnuTLS ciphers: %s", prioritylist);
  659. rc = gnutls_priority_set_direct(gtls->session, prioritylist, &err);
  660. #ifdef USE_GNUTLS_SRP
  661. }
  662. #endif
  663. if(rc != GNUTLS_E_SUCCESS) {
  664. failf(data, "Error %d setting GnuTLS cipher list starting with %s",
  665. rc, err);
  666. return CURLE_SSL_CONNECT_ERROR;
  667. }
  668. if(config->clientcert) {
  669. if(!gtls->trust_setup) {
  670. result = Curl_gtls_client_trust_setup(cf, data, gtls);
  671. if(result)
  672. return result;
  673. }
  674. if(ssl_config->key_passwd) {
  675. const unsigned int supported_key_encryption_algorithms =
  676. GNUTLS_PKCS_USE_PKCS12_3DES | GNUTLS_PKCS_USE_PKCS12_ARCFOUR |
  677. GNUTLS_PKCS_USE_PKCS12_RC2_40 | GNUTLS_PKCS_USE_PBES2_3DES |
  678. GNUTLS_PKCS_USE_PBES2_AES_128 | GNUTLS_PKCS_USE_PBES2_AES_192 |
  679. GNUTLS_PKCS_USE_PBES2_AES_256;
  680. rc = gnutls_certificate_set_x509_key_file2(
  681. gtls->cred,
  682. config->clientcert,
  683. ssl_config->key ? ssl_config->key : config->clientcert,
  684. do_file_type(ssl_config->cert_type),
  685. ssl_config->key_passwd,
  686. supported_key_encryption_algorithms);
  687. if(rc != GNUTLS_E_SUCCESS) {
  688. failf(data,
  689. "error reading X.509 potentially-encrypted key file: %s",
  690. gnutls_strerror(rc));
  691. return CURLE_SSL_CONNECT_ERROR;
  692. }
  693. }
  694. else {
  695. if(gnutls_certificate_set_x509_key_file(
  696. gtls->cred,
  697. config->clientcert,
  698. ssl_config->key ? ssl_config->key : config->clientcert,
  699. do_file_type(ssl_config->cert_type) ) !=
  700. GNUTLS_E_SUCCESS) {
  701. failf(data, "error reading X.509 key or certificate file");
  702. return CURLE_SSL_CONNECT_ERROR;
  703. }
  704. }
  705. }
  706. #ifdef USE_GNUTLS_SRP
  707. /* put the credentials to the current session */
  708. if(config->username) {
  709. rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_SRP,
  710. gtls->srp_client_cred);
  711. if(rc != GNUTLS_E_SUCCESS) {
  712. failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
  713. return CURLE_SSL_CONNECT_ERROR;
  714. }
  715. }
  716. else
  717. #endif
  718. {
  719. rc = gnutls_credentials_set(gtls->session, GNUTLS_CRD_CERTIFICATE,
  720. gtls->cred);
  721. if(rc != GNUTLS_E_SUCCESS) {
  722. failf(data, "gnutls_credentials_set() failed: %s", gnutls_strerror(rc));
  723. return CURLE_SSL_CONNECT_ERROR;
  724. }
  725. }
  726. if(config->verifystatus) {
  727. rc = gnutls_ocsp_status_request_enable_client(gtls->session,
  728. NULL, 0, NULL);
  729. if(rc != GNUTLS_E_SUCCESS) {
  730. failf(data, "gnutls_ocsp_status_request_enable_client() failed: %d", rc);
  731. return CURLE_SSL_CONNECT_ERROR;
  732. }
  733. }
  734. return CURLE_OK;
  735. }
  736. static int keylog_callback(gnutls_session_t session, const char *label,
  737. const gnutls_datum_t *secret)
  738. {
  739. gnutls_datum_t crandom;
  740. gnutls_datum_t srandom;
  741. gnutls_session_get_random(session, &crandom, &srandom);
  742. if(crandom.size != 32) {
  743. return -1;
  744. }
  745. Curl_tls_keylog_write(label, crandom.data, secret->data, secret->size);
  746. return 0;
  747. }
  748. CURLcode Curl_gtls_ctx_init(struct gtls_ctx *gctx,
  749. struct Curl_cfilter *cf,
  750. struct Curl_easy *data,
  751. struct ssl_peer *peer,
  752. const unsigned char *alpn, size_t alpn_len,
  753. Curl_gtls_ctx_setup_cb *cb_setup,
  754. void *cb_user_data,
  755. void *ssl_user_data)
  756. {
  757. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  758. CURLcode result;
  759. DEBUGASSERT(gctx);
  760. result = gtls_client_init(cf, data, peer, gctx);
  761. if(result)
  762. return result;
  763. gnutls_session_set_ptr(gctx->session, ssl_user_data);
  764. if(cb_setup) {
  765. result = cb_setup(cf, data, cb_user_data);
  766. if(result)
  767. return result;
  768. }
  769. /* Open the file if a TLS or QUIC backend has not done this before. */
  770. Curl_tls_keylog_open();
  771. if(Curl_tls_keylog_enabled()) {
  772. gnutls_session_set_keylog_function(gctx->session, keylog_callback);
  773. }
  774. /* convert the ALPN string from our arguments to a list of strings
  775. * that gnutls wants and will convert internally back to this very
  776. * string for sending to the server. nice. */
  777. if(alpn && alpn_len) {
  778. gnutls_datum_t alpns[5];
  779. size_t i, alen = alpn_len;
  780. unsigned char *s = (unsigned char *)alpn;
  781. unsigned char slen;
  782. for(i = 0; (i < ARRAYSIZE(alpns)) && alen; ++i) {
  783. slen = s[0];
  784. if(slen >= alen)
  785. return CURLE_FAILED_INIT;
  786. alpns[i].data = s + 1;
  787. alpns[i].size = slen;
  788. s += slen + 1;
  789. alen -= (size_t)slen + 1;
  790. }
  791. if(alen) /* not all alpn chars used, wrong format or too many */
  792. return CURLE_FAILED_INIT;
  793. if(i && gnutls_alpn_set_protocols(gctx->session,
  794. alpns, (unsigned int)i,
  795. GNUTLS_ALPN_MANDATORY)) {
  796. failf(data, "failed setting ALPN");
  797. return CURLE_SSL_CONNECT_ERROR;
  798. }
  799. }
  800. /* This might be a reconnect, so we check for a session ID in the cache
  801. to speed up things */
  802. if(conn_config->sessionid) {
  803. void *ssl_sessionid;
  804. size_t ssl_idsize;
  805. Curl_ssl_sessionid_lock(data);
  806. if(!Curl_ssl_getsessionid(cf, data, peer, &ssl_sessionid, &ssl_idsize)) {
  807. /* we got a session id, use it! */
  808. int rc;
  809. rc = gnutls_session_set_data(gctx->session, ssl_sessionid, ssl_idsize);
  810. if(rc < 0)
  811. infof(data, "SSL failed to set session ID");
  812. else
  813. infof(data, "SSL reusing session ID (size=%zu)", ssl_idsize);
  814. }
  815. Curl_ssl_sessionid_unlock(data);
  816. }
  817. return CURLE_OK;
  818. }
  819. static CURLcode
  820. gtls_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
  821. {
  822. struct ssl_connect_data *connssl = cf->ctx;
  823. struct gtls_ssl_backend_data *backend =
  824. (struct gtls_ssl_backend_data *)connssl->backend;
  825. struct alpn_proto_buf proto;
  826. CURLcode result;
  827. DEBUGASSERT(backend);
  828. DEBUGASSERT(ssl_connect_1 == connssl->connecting_state);
  829. if(connssl->state == ssl_connection_complete)
  830. /* to make us tolerant against being called more than once for the
  831. same connection */
  832. return CURLE_OK;
  833. memset(&proto, 0, sizeof(proto));
  834. if(connssl->alpn) {
  835. result = Curl_alpn_to_proto_buf(&proto, connssl->alpn);
  836. if(result) {
  837. failf(data, "Error determining ALPN");
  838. return CURLE_SSL_CONNECT_ERROR;
  839. }
  840. }
  841. result = Curl_gtls_ctx_init(&backend->gtls, cf, data, &connssl->peer,
  842. proto.data, proto.len, NULL, NULL, cf);
  843. if(result)
  844. return result;
  845. gnutls_handshake_set_hook_function(backend->gtls.session,
  846. GNUTLS_HANDSHAKE_ANY, GNUTLS_HOOK_POST,
  847. gtls_handshake_cb);
  848. /* register callback functions and handle to send and receive data. */
  849. gnutls_transport_set_ptr(backend->gtls.session, cf);
  850. gnutls_transport_set_push_function(backend->gtls.session, gtls_push);
  851. gnutls_transport_set_pull_function(backend->gtls.session, gtls_pull);
  852. return CURLE_OK;
  853. }
  854. static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
  855. gnutls_x509_crt_t cert,
  856. const char *pinnedpubkey)
  857. {
  858. /* Scratch */
  859. size_t len1 = 0, len2 = 0;
  860. unsigned char *buff1 = NULL;
  861. gnutls_pubkey_t key = NULL;
  862. /* Result is returned to caller */
  863. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  864. /* if a path wasn't specified, don't pin */
  865. if(!pinnedpubkey)
  866. return CURLE_OK;
  867. if(!cert)
  868. return result;
  869. do {
  870. int ret;
  871. /* Begin Gyrations to get the public key */
  872. gnutls_pubkey_init(&key);
  873. ret = gnutls_pubkey_import_x509(key, cert, 0);
  874. if(ret < 0)
  875. break; /* failed */
  876. ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, NULL, &len1);
  877. if(ret != GNUTLS_E_SHORT_MEMORY_BUFFER || len1 == 0)
  878. break; /* failed */
  879. buff1 = malloc(len1);
  880. if(!buff1)
  881. break; /* failed */
  882. len2 = len1;
  883. ret = gnutls_pubkey_export(key, GNUTLS_X509_FMT_DER, buff1, &len2);
  884. if(ret < 0 || len1 != len2)
  885. break; /* failed */
  886. /* End Gyrations */
  887. /* The one good exit point */
  888. result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
  889. } while(0);
  890. if(key)
  891. gnutls_pubkey_deinit(key);
  892. Curl_safefree(buff1);
  893. return result;
  894. }
  895. CURLcode
  896. Curl_gtls_verifyserver(struct Curl_easy *data,
  897. gnutls_session_t session,
  898. struct ssl_primary_config *config,
  899. struct ssl_config_data *ssl_config,
  900. struct ssl_peer *peer,
  901. const char *pinned_key)
  902. {
  903. unsigned int cert_list_size;
  904. const gnutls_datum_t *chainp;
  905. unsigned int verify_status = 0;
  906. gnutls_x509_crt_t x509_cert, x509_issuer;
  907. gnutls_datum_t issuerp;
  908. gnutls_datum_t certfields;
  909. char certname[65] = ""; /* limited to 64 chars by ASN.1 */
  910. size_t size;
  911. time_t certclock;
  912. int rc;
  913. CURLcode result = CURLE_OK;
  914. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  915. const char *ptr;
  916. unsigned int algo;
  917. unsigned int bits;
  918. gnutls_protocol_t version = gnutls_protocol_get_version(session);
  919. #endif
  920. long * const certverifyresult = &ssl_config->certverifyresult;
  921. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  922. /* the name of the cipher suite used, e.g. ECDHE_RSA_AES_256_GCM_SHA384. */
  923. ptr = gnutls_cipher_suite_get_name(gnutls_kx_get(session),
  924. gnutls_cipher_get(session),
  925. gnutls_mac_get(session));
  926. infof(data, "SSL connection using %s / %s",
  927. gnutls_protocol_get_name(version), ptr);
  928. #endif
  929. /* This function will return the peer's raw certificate (chain) as sent by
  930. the peer. These certificates are in raw format (DER encoded for
  931. X.509). In case of a X.509 then a certificate list may be present. The
  932. first certificate in the list is the peer's certificate, following the
  933. issuer's certificate, then the issuer's issuer etc. */
  934. chainp = gnutls_certificate_get_peers(session, &cert_list_size);
  935. if(!chainp) {
  936. if(config->verifypeer ||
  937. config->verifyhost ||
  938. config->issuercert) {
  939. #ifdef USE_GNUTLS_SRP
  940. if(ssl_config->primary.username && !config->verifypeer &&
  941. gnutls_cipher_get(session)) {
  942. /* no peer cert, but auth is ok if we have SRP user and cipher and no
  943. peer verify */
  944. }
  945. else {
  946. #endif
  947. failf(data, "failed to get server cert");
  948. *certverifyresult = GNUTLS_E_NO_CERTIFICATE_FOUND;
  949. return CURLE_PEER_FAILED_VERIFICATION;
  950. #ifdef USE_GNUTLS_SRP
  951. }
  952. #endif
  953. }
  954. infof(data, " common name: WARNING couldn't obtain");
  955. }
  956. if(data->set.ssl.certinfo && chainp) {
  957. unsigned int i;
  958. result = Curl_ssl_init_certinfo(data, cert_list_size);
  959. if(result)
  960. return result;
  961. for(i = 0; i < cert_list_size; i++) {
  962. const char *beg = (const char *) chainp[i].data;
  963. const char *end = beg + chainp[i].size;
  964. result = Curl_extract_certinfo(data, i, beg, end);
  965. if(result)
  966. return result;
  967. }
  968. }
  969. if(config->verifypeer) {
  970. /* This function will try to verify the peer's certificate and return its
  971. status (trusted, invalid etc.). The value of status should be one or
  972. more of the gnutls_certificate_status_t enumerated elements bitwise
  973. or'd. To avoid denial of service attacks some default upper limits
  974. regarding the certificate key size and chain size are set. To override
  975. them use gnutls_certificate_set_verify_limits(). */
  976. rc = gnutls_certificate_verify_peers2(session, &verify_status);
  977. if(rc < 0) {
  978. failf(data, "server cert verify failed: %d", rc);
  979. *certverifyresult = rc;
  980. return CURLE_SSL_CONNECT_ERROR;
  981. }
  982. *certverifyresult = verify_status;
  983. /* verify_status is a bitmask of gnutls_certificate_status bits */
  984. if(verify_status & GNUTLS_CERT_INVALID) {
  985. if(config->verifypeer) {
  986. failf(data, "server certificate verification failed. CAfile: %s "
  987. "CRLfile: %s", config->CAfile ? config->CAfile:
  988. "none",
  989. ssl_config->primary.CRLfile ?
  990. ssl_config->primary.CRLfile : "none");
  991. return CURLE_PEER_FAILED_VERIFICATION;
  992. }
  993. else
  994. infof(data, " server certificate verification FAILED");
  995. }
  996. else
  997. infof(data, " server certificate verification OK");
  998. }
  999. else
  1000. infof(data, " server certificate verification SKIPPED");
  1001. if(config->verifystatus) {
  1002. if(gnutls_ocsp_status_request_is_checked(session, 0) == 0) {
  1003. gnutls_datum_t status_request;
  1004. gnutls_ocsp_resp_t ocsp_resp;
  1005. gnutls_ocsp_cert_status_t status;
  1006. gnutls_x509_crl_reason_t reason;
  1007. rc = gnutls_ocsp_status_request_get(session, &status_request);
  1008. infof(data, " server certificate status verification FAILED");
  1009. if(rc == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
  1010. failf(data, "No OCSP response received");
  1011. return CURLE_SSL_INVALIDCERTSTATUS;
  1012. }
  1013. if(rc < 0) {
  1014. failf(data, "Invalid OCSP response received");
  1015. return CURLE_SSL_INVALIDCERTSTATUS;
  1016. }
  1017. gnutls_ocsp_resp_init(&ocsp_resp);
  1018. rc = gnutls_ocsp_resp_import(ocsp_resp, &status_request);
  1019. if(rc < 0) {
  1020. failf(data, "Invalid OCSP response received");
  1021. return CURLE_SSL_INVALIDCERTSTATUS;
  1022. }
  1023. (void)gnutls_ocsp_resp_get_single(ocsp_resp, 0, NULL, NULL, NULL, NULL,
  1024. &status, NULL, NULL, NULL, &reason);
  1025. switch(status) {
  1026. case GNUTLS_OCSP_CERT_GOOD:
  1027. break;
  1028. case GNUTLS_OCSP_CERT_REVOKED: {
  1029. const char *crl_reason;
  1030. switch(reason) {
  1031. default:
  1032. case GNUTLS_X509_CRLREASON_UNSPECIFIED:
  1033. crl_reason = "unspecified reason";
  1034. break;
  1035. case GNUTLS_X509_CRLREASON_KEYCOMPROMISE:
  1036. crl_reason = "private key compromised";
  1037. break;
  1038. case GNUTLS_X509_CRLREASON_CACOMPROMISE:
  1039. crl_reason = "CA compromised";
  1040. break;
  1041. case GNUTLS_X509_CRLREASON_AFFILIATIONCHANGED:
  1042. crl_reason = "affiliation has changed";
  1043. break;
  1044. case GNUTLS_X509_CRLREASON_SUPERSEDED:
  1045. crl_reason = "certificate superseded";
  1046. break;
  1047. case GNUTLS_X509_CRLREASON_CESSATIONOFOPERATION:
  1048. crl_reason = "operation has ceased";
  1049. break;
  1050. case GNUTLS_X509_CRLREASON_CERTIFICATEHOLD:
  1051. crl_reason = "certificate is on hold";
  1052. break;
  1053. case GNUTLS_X509_CRLREASON_REMOVEFROMCRL:
  1054. crl_reason = "will be removed from delta CRL";
  1055. break;
  1056. case GNUTLS_X509_CRLREASON_PRIVILEGEWITHDRAWN:
  1057. crl_reason = "privilege withdrawn";
  1058. break;
  1059. case GNUTLS_X509_CRLREASON_AACOMPROMISE:
  1060. crl_reason = "AA compromised";
  1061. break;
  1062. }
  1063. failf(data, "Server certificate was revoked: %s", crl_reason);
  1064. break;
  1065. }
  1066. default:
  1067. case GNUTLS_OCSP_CERT_UNKNOWN:
  1068. failf(data, "Server certificate status is unknown");
  1069. break;
  1070. }
  1071. gnutls_ocsp_resp_deinit(ocsp_resp);
  1072. return CURLE_SSL_INVALIDCERTSTATUS;
  1073. }
  1074. else
  1075. infof(data, " server certificate status verification OK");
  1076. }
  1077. else
  1078. infof(data, " server certificate status verification SKIPPED");
  1079. /* initialize an X.509 certificate structure. */
  1080. gnutls_x509_crt_init(&x509_cert);
  1081. if(chainp)
  1082. /* convert the given DER or PEM encoded Certificate to the native
  1083. gnutls_x509_crt_t format */
  1084. gnutls_x509_crt_import(x509_cert, chainp, GNUTLS_X509_FMT_DER);
  1085. if(config->issuercert) {
  1086. gnutls_x509_crt_init(&x509_issuer);
  1087. issuerp = load_file(config->issuercert);
  1088. gnutls_x509_crt_import(x509_issuer, &issuerp, GNUTLS_X509_FMT_PEM);
  1089. rc = gnutls_x509_crt_check_issuer(x509_cert, x509_issuer);
  1090. gnutls_x509_crt_deinit(x509_issuer);
  1091. unload_file(issuerp);
  1092. if(rc <= 0) {
  1093. failf(data, "server certificate issuer check failed (IssuerCert: %s)",
  1094. config->issuercert?config->issuercert:"none");
  1095. gnutls_x509_crt_deinit(x509_cert);
  1096. return CURLE_SSL_ISSUER_ERROR;
  1097. }
  1098. infof(data, " server certificate issuer check OK (Issuer Cert: %s)",
  1099. config->issuercert?config->issuercert:"none");
  1100. }
  1101. size = sizeof(certname);
  1102. rc = gnutls_x509_crt_get_dn_by_oid(x509_cert, GNUTLS_OID_X520_COMMON_NAME,
  1103. 0, /* the first and only one */
  1104. FALSE,
  1105. certname,
  1106. &size);
  1107. if(rc) {
  1108. infof(data, "error fetching CN from cert:%s",
  1109. gnutls_strerror(rc));
  1110. }
  1111. /* This function will check if the given certificate's subject matches the
  1112. given hostname. This is a basic implementation of the matching described
  1113. in RFC2818 (HTTPS), which takes into account wildcards, and the subject
  1114. alternative name PKIX extension. Returns non zero on success, and zero on
  1115. failure. */
  1116. rc = gnutls_x509_crt_check_hostname(x509_cert, peer->hostname);
  1117. #if GNUTLS_VERSION_NUMBER < 0x030306
  1118. /* Before 3.3.6, gnutls_x509_crt_check_hostname() didn't check IP
  1119. addresses. */
  1120. if(!rc) {
  1121. #ifdef USE_IPV6
  1122. #define use_addr in6_addr
  1123. #else
  1124. #define use_addr in_addr
  1125. #endif
  1126. unsigned char addrbuf[sizeof(struct use_addr)];
  1127. size_t addrlen = 0;
  1128. if(Curl_inet_pton(AF_INET, peer->hostname, addrbuf) > 0)
  1129. addrlen = 4;
  1130. #ifdef USE_IPV6
  1131. else if(Curl_inet_pton(AF_INET6, peer->hostname, addrbuf) > 0)
  1132. addrlen = 16;
  1133. #endif
  1134. if(addrlen) {
  1135. unsigned char certaddr[sizeof(struct use_addr)];
  1136. int i;
  1137. for(i = 0; ; i++) {
  1138. size_t certaddrlen = sizeof(certaddr);
  1139. int ret = gnutls_x509_crt_get_subject_alt_name(x509_cert, i, certaddr,
  1140. &certaddrlen, NULL);
  1141. /* If this happens, it wasn't an IP address. */
  1142. if(ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
  1143. continue;
  1144. if(ret < 0)
  1145. break;
  1146. if(ret != GNUTLS_SAN_IPADDRESS)
  1147. continue;
  1148. if(certaddrlen == addrlen && !memcmp(addrbuf, certaddr, addrlen)) {
  1149. rc = 1;
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. #endif
  1156. if(!rc) {
  1157. if(config->verifyhost) {
  1158. failf(data, "SSL: certificate subject name (%s) does not match "
  1159. "target host name '%s'", certname, peer->dispname);
  1160. gnutls_x509_crt_deinit(x509_cert);
  1161. return CURLE_PEER_FAILED_VERIFICATION;
  1162. }
  1163. else
  1164. infof(data, " common name: %s (does not match '%s')",
  1165. certname, peer->dispname);
  1166. }
  1167. else
  1168. infof(data, " common name: %s (matched)", certname);
  1169. /* Check for time-based validity */
  1170. certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
  1171. if(certclock == (time_t)-1) {
  1172. if(config->verifypeer) {
  1173. failf(data, "server cert expiration date verify failed");
  1174. *certverifyresult = GNUTLS_CERT_EXPIRED;
  1175. gnutls_x509_crt_deinit(x509_cert);
  1176. return CURLE_SSL_CONNECT_ERROR;
  1177. }
  1178. else
  1179. infof(data, " server certificate expiration date verify FAILED");
  1180. }
  1181. else {
  1182. if(certclock < time(NULL)) {
  1183. if(config->verifypeer) {
  1184. failf(data, "server certificate expiration date has passed.");
  1185. *certverifyresult = GNUTLS_CERT_EXPIRED;
  1186. gnutls_x509_crt_deinit(x509_cert);
  1187. return CURLE_PEER_FAILED_VERIFICATION;
  1188. }
  1189. else
  1190. infof(data, " server certificate expiration date FAILED");
  1191. }
  1192. else
  1193. infof(data, " server certificate expiration date OK");
  1194. }
  1195. certclock = gnutls_x509_crt_get_activation_time(x509_cert);
  1196. if(certclock == (time_t)-1) {
  1197. if(config->verifypeer) {
  1198. failf(data, "server cert activation date verify failed");
  1199. *certverifyresult = GNUTLS_CERT_NOT_ACTIVATED;
  1200. gnutls_x509_crt_deinit(x509_cert);
  1201. return CURLE_SSL_CONNECT_ERROR;
  1202. }
  1203. else
  1204. infof(data, " server certificate activation date verify FAILED");
  1205. }
  1206. else {
  1207. if(certclock > time(NULL)) {
  1208. if(config->verifypeer) {
  1209. failf(data, "server certificate not activated yet.");
  1210. *certverifyresult = GNUTLS_CERT_NOT_ACTIVATED;
  1211. gnutls_x509_crt_deinit(x509_cert);
  1212. return CURLE_PEER_FAILED_VERIFICATION;
  1213. }
  1214. else
  1215. infof(data, " server certificate activation date FAILED");
  1216. }
  1217. else
  1218. infof(data, " server certificate activation date OK");
  1219. }
  1220. if(pinned_key) {
  1221. result = pkp_pin_peer_pubkey(data, x509_cert, pinned_key);
  1222. if(result != CURLE_OK) {
  1223. failf(data, "SSL: public key does not match pinned public key");
  1224. gnutls_x509_crt_deinit(x509_cert);
  1225. return result;
  1226. }
  1227. }
  1228. /* Show:
  1229. - subject
  1230. - start date
  1231. - expire date
  1232. - common name
  1233. - issuer
  1234. */
  1235. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1236. /* public key algorithm's parameters */
  1237. algo = gnutls_x509_crt_get_pk_algorithm(x509_cert, &bits);
  1238. infof(data, " certificate public key: %s",
  1239. gnutls_pk_algorithm_get_name(algo));
  1240. /* version of the X.509 certificate. */
  1241. infof(data, " certificate version: #%d",
  1242. gnutls_x509_crt_get_version(x509_cert));
  1243. rc = gnutls_x509_crt_get_dn2(x509_cert, &certfields);
  1244. if(rc)
  1245. infof(data, "Failed to get certificate name");
  1246. else {
  1247. infof(data, " subject: %s", certfields.data);
  1248. certclock = gnutls_x509_crt_get_activation_time(x509_cert);
  1249. showtime(data, "start date", certclock);
  1250. certclock = gnutls_x509_crt_get_expiration_time(x509_cert);
  1251. showtime(data, "expire date", certclock);
  1252. gnutls_free(certfields.data);
  1253. }
  1254. rc = gnutls_x509_crt_get_issuer_dn2(x509_cert, &certfields);
  1255. if(rc)
  1256. infof(data, "Failed to get certificate issuer");
  1257. else {
  1258. infof(data, " issuer: %s", certfields.data);
  1259. gnutls_free(certfields.data);
  1260. }
  1261. #endif
  1262. gnutls_x509_crt_deinit(x509_cert);
  1263. return result;
  1264. }
  1265. static CURLcode gtls_verifyserver(struct Curl_cfilter *cf,
  1266. struct Curl_easy *data,
  1267. gnutls_session_t session)
  1268. {
  1269. struct ssl_connect_data *connssl = cf->ctx;
  1270. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  1271. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1272. #ifndef CURL_DISABLE_PROXY
  1273. const char *pinned_key = Curl_ssl_cf_is_proxy(cf)?
  1274. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  1275. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  1276. #else
  1277. const char *pinned_key = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  1278. #endif
  1279. CURLcode result;
  1280. result = Curl_gtls_verifyserver(data, session, conn_config, ssl_config,
  1281. &connssl->peer, pinned_key);
  1282. if(result)
  1283. goto out;
  1284. if(connssl->alpn) {
  1285. gnutls_datum_t proto;
  1286. int rc;
  1287. rc = gnutls_alpn_get_selected_protocol(session, &proto);
  1288. if(rc == 0)
  1289. Curl_alpn_set_negotiated(cf, data, proto.data, proto.size);
  1290. else
  1291. Curl_alpn_set_negotiated(cf, data, NULL, 0);
  1292. }
  1293. /* Only on TLSv1.2 or lower do we have the session id now. For
  1294. * TLSv1.3 we get it via a SESSION_TICKET message that arrives later. */
  1295. if(gnutls_protocol_get_version(session) < GNUTLS_TLS1_3)
  1296. result = gtls_update_session_id(cf, data, session);
  1297. out:
  1298. return result;
  1299. }
  1300. /*
  1301. * This function is called after the TCP connect has completed. Setup the TLS
  1302. * layer and do all necessary magic.
  1303. */
  1304. /* We use connssl->connecting_state to keep track of the connection status;
  1305. there are three states: 'ssl_connect_1' (not started yet or complete),
  1306. 'ssl_connect_2_reading' (waiting for data from server), and
  1307. 'ssl_connect_2_writing' (waiting to be able to write).
  1308. */
  1309. static CURLcode
  1310. gtls_connect_common(struct Curl_cfilter *cf,
  1311. struct Curl_easy *data,
  1312. bool nonblocking,
  1313. bool *done)
  1314. {
  1315. struct ssl_connect_data *connssl = cf->ctx;
  1316. int rc;
  1317. CURLcode result = CURLE_OK;
  1318. /* Initiate the connection, if not already done */
  1319. if(ssl_connect_1 == connssl->connecting_state) {
  1320. rc = gtls_connect_step1(cf, data);
  1321. if(rc) {
  1322. result = rc;
  1323. goto out;
  1324. }
  1325. }
  1326. rc = handshake(cf, data, TRUE, nonblocking);
  1327. if(rc) {
  1328. /* handshake() sets its own error message with failf() */
  1329. result = rc;
  1330. goto out;
  1331. }
  1332. /* Finish connecting once the handshake is done */
  1333. if(ssl_connect_1 == connssl->connecting_state) {
  1334. struct gtls_ssl_backend_data *backend =
  1335. (struct gtls_ssl_backend_data *)connssl->backend;
  1336. gnutls_session_t session;
  1337. DEBUGASSERT(backend);
  1338. session = backend->gtls.session;
  1339. rc = gtls_verifyserver(cf, data, session);
  1340. if(rc) {
  1341. result = rc;
  1342. goto out;
  1343. }
  1344. connssl->state = ssl_connection_complete;
  1345. }
  1346. out:
  1347. *done = ssl_connect_1 == connssl->connecting_state;
  1348. return result;
  1349. }
  1350. static CURLcode gtls_connect_nonblocking(struct Curl_cfilter *cf,
  1351. struct Curl_easy *data,
  1352. bool *done)
  1353. {
  1354. return gtls_connect_common(cf, data, TRUE, done);
  1355. }
  1356. static CURLcode gtls_connect(struct Curl_cfilter *cf,
  1357. struct Curl_easy *data)
  1358. {
  1359. CURLcode result;
  1360. bool done = FALSE;
  1361. result = gtls_connect_common(cf, data, FALSE, &done);
  1362. if(result)
  1363. return result;
  1364. DEBUGASSERT(done);
  1365. return CURLE_OK;
  1366. }
  1367. static bool gtls_data_pending(struct Curl_cfilter *cf,
  1368. const struct Curl_easy *data)
  1369. {
  1370. struct ssl_connect_data *ctx = cf->ctx;
  1371. struct gtls_ssl_backend_data *backend;
  1372. (void)data;
  1373. DEBUGASSERT(ctx && ctx->backend);
  1374. backend = (struct gtls_ssl_backend_data *)ctx->backend;
  1375. if(backend->gtls.session &&
  1376. 0 != gnutls_record_check_pending(backend->gtls.session))
  1377. return TRUE;
  1378. return FALSE;
  1379. }
  1380. static ssize_t gtls_send(struct Curl_cfilter *cf,
  1381. struct Curl_easy *data,
  1382. const void *mem,
  1383. size_t len,
  1384. CURLcode *curlcode)
  1385. {
  1386. struct ssl_connect_data *connssl = cf->ctx;
  1387. struct gtls_ssl_backend_data *backend =
  1388. (struct gtls_ssl_backend_data *)connssl->backend;
  1389. ssize_t rc;
  1390. (void)data;
  1391. DEBUGASSERT(backend);
  1392. backend->gtls.io_result = CURLE_OK;
  1393. rc = gnutls_record_send(backend->gtls.session, mem, len);
  1394. if(rc < 0) {
  1395. *curlcode = (rc == GNUTLS_E_AGAIN)?
  1396. CURLE_AGAIN :
  1397. (backend->gtls.io_result? backend->gtls.io_result : CURLE_SEND_ERROR);
  1398. rc = -1;
  1399. }
  1400. return rc;
  1401. }
  1402. static void gtls_close(struct Curl_cfilter *cf,
  1403. struct Curl_easy *data)
  1404. {
  1405. struct ssl_connect_data *connssl = cf->ctx;
  1406. struct gtls_ssl_backend_data *backend =
  1407. (struct gtls_ssl_backend_data *)connssl->backend;
  1408. (void) data;
  1409. DEBUGASSERT(backend);
  1410. if(backend->gtls.session) {
  1411. char buf[32];
  1412. /* Maybe the server has already sent a close notify alert.
  1413. Read it to avoid an RST on the TCP connection. */
  1414. (void)gnutls_record_recv(backend->gtls.session, buf, sizeof(buf));
  1415. gnutls_bye(backend->gtls.session, GNUTLS_SHUT_WR);
  1416. gnutls_deinit(backend->gtls.session);
  1417. backend->gtls.session = NULL;
  1418. }
  1419. if(backend->gtls.cred) {
  1420. gnutls_certificate_free_credentials(backend->gtls.cred);
  1421. backend->gtls.cred = NULL;
  1422. }
  1423. #ifdef USE_GNUTLS_SRP
  1424. if(backend->gtls.srp_client_cred) {
  1425. gnutls_srp_free_client_credentials(backend->gtls.srp_client_cred);
  1426. backend->gtls.srp_client_cred = NULL;
  1427. }
  1428. #endif
  1429. }
  1430. /*
  1431. * This function is called to shut down the SSL layer but keep the
  1432. * socket open (CCC - Clear Command Channel)
  1433. */
  1434. static int gtls_shutdown(struct Curl_cfilter *cf,
  1435. struct Curl_easy *data)
  1436. {
  1437. struct ssl_connect_data *connssl = cf->ctx;
  1438. struct gtls_ssl_backend_data *backend =
  1439. (struct gtls_ssl_backend_data *)connssl->backend;
  1440. int retval = 0;
  1441. DEBUGASSERT(backend);
  1442. #ifndef CURL_DISABLE_FTP
  1443. /* This has only been tested on the proftpd server, and the mod_tls code
  1444. sends a close notify alert without waiting for a close notify alert in
  1445. response. Thus we wait for a close notify alert from the server, but
  1446. we do not send one. Let's hope other servers do the same... */
  1447. if(data->set.ftp_ccc == CURLFTPSSL_CCC_ACTIVE)
  1448. gnutls_bye(backend->gtls.session, GNUTLS_SHUT_WR);
  1449. #endif
  1450. if(backend->gtls.session) {
  1451. ssize_t result;
  1452. bool done = FALSE;
  1453. char buf[120];
  1454. while(!done && !connssl->peer_closed) {
  1455. int what = SOCKET_READABLE(Curl_conn_cf_get_socket(cf, data),
  1456. SSL_SHUTDOWN_TIMEOUT);
  1457. if(what > 0) {
  1458. /* Something to read, let's do it and hope that it is the close
  1459. notify alert from the server */
  1460. result = gnutls_record_recv(backend->gtls.session,
  1461. buf, sizeof(buf));
  1462. switch(result) {
  1463. case 0:
  1464. /* This is the expected response. There was no data but only
  1465. the close notify alert */
  1466. done = TRUE;
  1467. break;
  1468. case GNUTLS_E_AGAIN:
  1469. case GNUTLS_E_INTERRUPTED:
  1470. infof(data, "GNUTLS_E_AGAIN || GNUTLS_E_INTERRUPTED");
  1471. break;
  1472. default:
  1473. retval = -1;
  1474. done = TRUE;
  1475. break;
  1476. }
  1477. }
  1478. else if(0 == what) {
  1479. /* timeout */
  1480. failf(data, "SSL shutdown timeout");
  1481. done = TRUE;
  1482. }
  1483. else {
  1484. /* anything that gets here is fatally bad */
  1485. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1486. retval = -1;
  1487. done = TRUE;
  1488. }
  1489. }
  1490. gnutls_deinit(backend->gtls.session);
  1491. }
  1492. gnutls_certificate_free_credentials(backend->gtls.cred);
  1493. #ifdef USE_GNUTLS_SRP
  1494. {
  1495. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1496. if(ssl_config->primary.username)
  1497. gnutls_srp_free_client_credentials(backend->gtls.srp_client_cred);
  1498. }
  1499. #endif
  1500. backend->gtls.cred = NULL;
  1501. backend->gtls.session = NULL;
  1502. return retval;
  1503. }
  1504. static ssize_t gtls_recv(struct Curl_cfilter *cf,
  1505. struct Curl_easy *data,
  1506. char *buf,
  1507. size_t buffersize,
  1508. CURLcode *curlcode)
  1509. {
  1510. struct ssl_connect_data *connssl = cf->ctx;
  1511. struct gtls_ssl_backend_data *backend =
  1512. (struct gtls_ssl_backend_data *)connssl->backend;
  1513. ssize_t ret;
  1514. (void)data;
  1515. DEBUGASSERT(backend);
  1516. backend->gtls.io_result = CURLE_OK;
  1517. ret = gnutls_record_recv(backend->gtls.session, buf, buffersize);
  1518. if((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)) {
  1519. *curlcode = CURLE_AGAIN;
  1520. ret = -1;
  1521. goto out;
  1522. }
  1523. if(ret == GNUTLS_E_REHANDSHAKE) {
  1524. /* BLOCKING call, this is bad but a work-around for now. Fixing this "the
  1525. proper way" takes a whole lot of work. */
  1526. CURLcode result = handshake(cf, data, FALSE, FALSE);
  1527. if(result)
  1528. /* handshake() writes error message on its own */
  1529. *curlcode = result;
  1530. else
  1531. *curlcode = CURLE_AGAIN; /* then return as if this was a wouldblock */
  1532. ret = -1;
  1533. goto out;
  1534. }
  1535. if(ret < 0) {
  1536. failf(data, "GnuTLS recv error (%d): %s",
  1537. (int)ret, gnutls_strerror((int)ret));
  1538. *curlcode = backend->gtls.io_result?
  1539. backend->gtls.io_result : CURLE_RECV_ERROR;
  1540. ret = -1;
  1541. goto out;
  1542. }
  1543. out:
  1544. return ret;
  1545. }
  1546. static size_t gtls_version(char *buffer, size_t size)
  1547. {
  1548. return msnprintf(buffer, size, "GnuTLS/%s", gnutls_check_version(NULL));
  1549. }
  1550. /* data might be NULL! */
  1551. static CURLcode gtls_random(struct Curl_easy *data,
  1552. unsigned char *entropy, size_t length)
  1553. {
  1554. int rc;
  1555. (void)data;
  1556. rc = gnutls_rnd(GNUTLS_RND_RANDOM, entropy, length);
  1557. return rc?CURLE_FAILED_INIT:CURLE_OK;
  1558. }
  1559. static CURLcode gtls_sha256sum(const unsigned char *tmp, /* input */
  1560. size_t tmplen,
  1561. unsigned char *sha256sum, /* output */
  1562. size_t sha256len)
  1563. {
  1564. struct sha256_ctx SHA256pw;
  1565. sha256_init(&SHA256pw);
  1566. sha256_update(&SHA256pw, (unsigned int)tmplen, tmp);
  1567. sha256_digest(&SHA256pw, (unsigned int)sha256len, sha256sum);
  1568. return CURLE_OK;
  1569. }
  1570. static bool gtls_cert_status_request(void)
  1571. {
  1572. return TRUE;
  1573. }
  1574. static void *gtls_get_internals(struct ssl_connect_data *connssl,
  1575. CURLINFO info UNUSED_PARAM)
  1576. {
  1577. struct gtls_ssl_backend_data *backend =
  1578. (struct gtls_ssl_backend_data *)connssl->backend;
  1579. (void)info;
  1580. DEBUGASSERT(backend);
  1581. return backend->gtls.session;
  1582. }
  1583. const struct Curl_ssl Curl_ssl_gnutls = {
  1584. { CURLSSLBACKEND_GNUTLS, "gnutls" }, /* info */
  1585. SSLSUPP_CA_PATH |
  1586. SSLSUPP_CERTINFO |
  1587. SSLSUPP_PINNEDPUBKEY |
  1588. SSLSUPP_HTTPS_PROXY,
  1589. sizeof(struct gtls_ssl_backend_data),
  1590. gtls_init, /* init */
  1591. gtls_cleanup, /* cleanup */
  1592. gtls_version, /* version */
  1593. Curl_none_check_cxn, /* check_cxn */
  1594. gtls_shutdown, /* shutdown */
  1595. gtls_data_pending, /* data_pending */
  1596. gtls_random, /* random */
  1597. gtls_cert_status_request, /* cert_status_request */
  1598. gtls_connect, /* connect */
  1599. gtls_connect_nonblocking, /* connect_nonblocking */
  1600. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1601. gtls_get_internals, /* get_internals */
  1602. gtls_close, /* close_one */
  1603. Curl_none_close_all, /* close_all */
  1604. Curl_none_set_engine, /* set_engine */
  1605. Curl_none_set_engine_default, /* set_engine_default */
  1606. Curl_none_engines_list, /* engines_list */
  1607. Curl_none_false_start, /* false_start */
  1608. gtls_sha256sum, /* sha256sum */
  1609. NULL, /* associate_connection */
  1610. NULL, /* disassociate_connection */
  1611. NULL, /* free_multi_ssl_backend_data */
  1612. gtls_recv, /* recv decrypted data */
  1613. gtls_send, /* send data to encrypt */
  1614. };
  1615. #endif /* USE_GNUTLS */