hsts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. * The Strict-Transport-Security header is defined in RFC 6797:
  26. * https://datatracker.ietf.org/doc/html/rfc6797
  27. */
  28. #include "curl_setup.h"
  29. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HSTS)
  30. #include <curl/curl.h>
  31. #include "urldata.h"
  32. #include "llist.h"
  33. #include "hsts.h"
  34. #include "curl_get_line.h"
  35. #include "strcase.h"
  36. #include "sendf.h"
  37. #include "strtoofft.h"
  38. #include "parsedate.h"
  39. #include "fopen.h"
  40. #include "rename.h"
  41. #include "share.h"
  42. #include "strdup.h"
  43. /* The last 3 #include files should be in this order */
  44. #include "curl_printf.h"
  45. #include "curl_memory.h"
  46. #include "memdebug.h"
  47. #define MAX_HSTS_LINE 4095
  48. #define MAX_HSTS_HOSTLEN 256
  49. #define MAX_HSTS_HOSTLENSTR "256"
  50. #define MAX_HSTS_DATELEN 64
  51. #define MAX_HSTS_DATELENSTR "64"
  52. #define UNLIMITED "unlimited"
  53. #ifdef DEBUGBUILD
  54. /* to play well with debug builds, we can *set* a fixed time this will
  55. return */
  56. time_t deltatime; /* allow for "adjustments" for unit test purposes */
  57. static time_t hsts_debugtime(void *unused)
  58. {
  59. char *timestr = getenv("CURL_TIME");
  60. (void)unused;
  61. if(timestr) {
  62. curl_off_t val;
  63. (void)curlx_strtoofft(timestr, NULL, 10, &val);
  64. val += (curl_off_t)deltatime;
  65. return (time_t)val;
  66. }
  67. return time(NULL);
  68. }
  69. #undef time
  70. #define time(x) hsts_debugtime(x)
  71. #endif
  72. struct hsts *Curl_hsts_init(void)
  73. {
  74. struct hsts *h = calloc(1, sizeof(struct hsts));
  75. if(h) {
  76. Curl_llist_init(&h->list, NULL);
  77. }
  78. return h;
  79. }
  80. static void hsts_free(struct stsentry *e)
  81. {
  82. free((char *)e->host);
  83. free(e);
  84. }
  85. void Curl_hsts_cleanup(struct hsts **hp)
  86. {
  87. struct hsts *h = *hp;
  88. if(h) {
  89. struct Curl_llist_element *e;
  90. struct Curl_llist_element *n;
  91. for(e = h->list.head; e; e = n) {
  92. struct stsentry *sts = e->ptr;
  93. n = e->next;
  94. hsts_free(sts);
  95. }
  96. free(h->filename);
  97. free(h);
  98. *hp = NULL;
  99. }
  100. }
  101. static struct stsentry *hsts_entry(void)
  102. {
  103. return calloc(1, sizeof(struct stsentry));
  104. }
  105. static CURLcode hsts_create(struct hsts *h,
  106. const char *hostname,
  107. bool subdomains,
  108. curl_off_t expires)
  109. {
  110. size_t hlen;
  111. DEBUGASSERT(h);
  112. DEBUGASSERT(hostname);
  113. hlen = strlen(hostname);
  114. if(hlen && (hostname[hlen - 1] == '.'))
  115. /* strip off any trailing dot */
  116. --hlen;
  117. if(hlen) {
  118. char *duphost;
  119. struct stsentry *sts = hsts_entry();
  120. if(!sts)
  121. return CURLE_OUT_OF_MEMORY;
  122. duphost = Curl_memdup0(hostname, hlen);
  123. if(!duphost) {
  124. free(sts);
  125. return CURLE_OUT_OF_MEMORY;
  126. }
  127. sts->host = duphost;
  128. sts->expires = expires;
  129. sts->includeSubDomains = subdomains;
  130. Curl_llist_insert_next(&h->list, h->list.tail, sts, &sts->node);
  131. }
  132. return CURLE_OK;
  133. }
  134. CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
  135. const char *header)
  136. {
  137. const char *p = header;
  138. curl_off_t expires = 0;
  139. bool gotma = FALSE;
  140. bool gotinc = FALSE;
  141. bool subdomains = FALSE;
  142. struct stsentry *sts;
  143. time_t now = time(NULL);
  144. if(Curl_host_is_ipnum(hostname))
  145. /* "explicit IP address identification of all forms is excluded."
  146. / RFC 6797 */
  147. return CURLE_OK;
  148. do {
  149. while(*p && ISBLANK(*p))
  150. p++;
  151. if(strncasecompare("max-age=", p, 8)) {
  152. bool quoted = FALSE;
  153. CURLofft offt;
  154. char *endp;
  155. if(gotma)
  156. return CURLE_BAD_FUNCTION_ARGUMENT;
  157. p += 8;
  158. while(*p && ISBLANK(*p))
  159. p++;
  160. if(*p == '\"') {
  161. p++;
  162. quoted = TRUE;
  163. }
  164. offt = curlx_strtoofft(p, &endp, 10, &expires);
  165. if(offt == CURL_OFFT_FLOW)
  166. expires = CURL_OFF_T_MAX;
  167. else if(offt)
  168. /* invalid max-age */
  169. return CURLE_BAD_FUNCTION_ARGUMENT;
  170. p = endp;
  171. if(quoted) {
  172. if(*p != '\"')
  173. return CURLE_BAD_FUNCTION_ARGUMENT;
  174. p++;
  175. }
  176. gotma = TRUE;
  177. }
  178. else if(strncasecompare("includesubdomains", p, 17)) {
  179. if(gotinc)
  180. return CURLE_BAD_FUNCTION_ARGUMENT;
  181. subdomains = TRUE;
  182. p += 17;
  183. gotinc = TRUE;
  184. }
  185. else {
  186. /* unknown directive, do a lame attempt to skip */
  187. while(*p && (*p != ';'))
  188. p++;
  189. }
  190. while(*p && ISBLANK(*p))
  191. p++;
  192. if(*p == ';')
  193. p++;
  194. } while(*p);
  195. if(!gotma)
  196. /* max-age is mandatory */
  197. return CURLE_BAD_FUNCTION_ARGUMENT;
  198. if(!expires) {
  199. /* remove the entry if present verbatim (without subdomain match) */
  200. sts = Curl_hsts(h, hostname, FALSE);
  201. if(sts) {
  202. Curl_llist_remove(&h->list, &sts->node, NULL);
  203. hsts_free(sts);
  204. }
  205. return CURLE_OK;
  206. }
  207. if(CURL_OFF_T_MAX - now < expires)
  208. /* would overflow, use maximum value */
  209. expires = CURL_OFF_T_MAX;
  210. else
  211. expires += now;
  212. /* check if it already exists */
  213. sts = Curl_hsts(h, hostname, FALSE);
  214. if(sts) {
  215. /* just update these fields */
  216. sts->expires = expires;
  217. sts->includeSubDomains = subdomains;
  218. }
  219. else
  220. return hsts_create(h, hostname, subdomains, expires);
  221. return CURLE_OK;
  222. }
  223. /*
  224. * Return TRUE if the given host name is currently an HSTS one.
  225. *
  226. * The 'subdomain' argument tells the function if subdomain matching should be
  227. * attempted.
  228. */
  229. struct stsentry *Curl_hsts(struct hsts *h, const char *hostname,
  230. bool subdomain)
  231. {
  232. if(h) {
  233. char buffer[MAX_HSTS_HOSTLEN + 1];
  234. time_t now = time(NULL);
  235. size_t hlen = strlen(hostname);
  236. struct Curl_llist_element *e;
  237. struct Curl_llist_element *n;
  238. if((hlen > MAX_HSTS_HOSTLEN) || !hlen)
  239. return NULL;
  240. memcpy(buffer, hostname, hlen);
  241. if(hostname[hlen-1] == '.')
  242. /* remove the trailing dot */
  243. --hlen;
  244. buffer[hlen] = 0;
  245. hostname = buffer;
  246. for(e = h->list.head; e; e = n) {
  247. struct stsentry *sts = e->ptr;
  248. n = e->next;
  249. if(sts->expires <= now) {
  250. /* remove expired entries */
  251. Curl_llist_remove(&h->list, &sts->node, NULL);
  252. hsts_free(sts);
  253. continue;
  254. }
  255. if(subdomain && sts->includeSubDomains) {
  256. size_t ntail = strlen(sts->host);
  257. if(ntail < hlen) {
  258. size_t offs = hlen - ntail;
  259. if((hostname[offs-1] == '.') &&
  260. strncasecompare(&hostname[offs], sts->host, ntail))
  261. return sts;
  262. }
  263. }
  264. if(strcasecompare(hostname, sts->host))
  265. return sts;
  266. }
  267. }
  268. return NULL; /* no match */
  269. }
  270. /*
  271. * Send this HSTS entry to the write callback.
  272. */
  273. static CURLcode hsts_push(struct Curl_easy *data,
  274. struct curl_index *i,
  275. struct stsentry *sts,
  276. bool *stop)
  277. {
  278. struct curl_hstsentry e;
  279. CURLSTScode sc;
  280. struct tm stamp;
  281. CURLcode result;
  282. e.name = (char *)sts->host;
  283. e.namelen = strlen(sts->host);
  284. e.includeSubDomains = sts->includeSubDomains;
  285. if(sts->expires != TIME_T_MAX) {
  286. result = Curl_gmtime((time_t)sts->expires, &stamp);
  287. if(result)
  288. return result;
  289. msnprintf(e.expire, sizeof(e.expire), "%d%02d%02d %02d:%02d:%02d",
  290. stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
  291. stamp.tm_hour, stamp.tm_min, stamp.tm_sec);
  292. }
  293. else
  294. strcpy(e.expire, UNLIMITED);
  295. sc = data->set.hsts_write(data, &e, i,
  296. data->set.hsts_write_userp);
  297. *stop = (sc != CURLSTS_OK);
  298. return sc == CURLSTS_FAIL ? CURLE_BAD_FUNCTION_ARGUMENT : CURLE_OK;
  299. }
  300. /*
  301. * Write this single hsts entry to a single output line
  302. */
  303. static CURLcode hsts_out(struct stsentry *sts, FILE *fp)
  304. {
  305. struct tm stamp;
  306. if(sts->expires != TIME_T_MAX) {
  307. CURLcode result = Curl_gmtime((time_t)sts->expires, &stamp);
  308. if(result)
  309. return result;
  310. fprintf(fp, "%s%s \"%d%02d%02d %02d:%02d:%02d\"\n",
  311. sts->includeSubDomains ? ".": "", sts->host,
  312. stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
  313. stamp.tm_hour, stamp.tm_min, stamp.tm_sec);
  314. }
  315. else
  316. fprintf(fp, "%s%s \"%s\"\n",
  317. sts->includeSubDomains ? ".": "", sts->host, UNLIMITED);
  318. return CURLE_OK;
  319. }
  320. /*
  321. * Curl_https_save() writes the HSTS cache to file and callback.
  322. */
  323. CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h,
  324. const char *file)
  325. {
  326. struct Curl_llist_element *e;
  327. struct Curl_llist_element *n;
  328. CURLcode result = CURLE_OK;
  329. FILE *out;
  330. char *tempstore = NULL;
  331. if(!h)
  332. /* no cache activated */
  333. return CURLE_OK;
  334. /* if no new name is given, use the one we stored from the load */
  335. if(!file && h->filename)
  336. file = h->filename;
  337. if((h->flags & CURLHSTS_READONLYFILE) || !file || !file[0])
  338. /* marked as read-only, no file or zero length file name */
  339. goto skipsave;
  340. result = Curl_fopen(data, file, &out, &tempstore);
  341. if(!result) {
  342. fputs("# Your HSTS cache. https://curl.se/docs/hsts.html\n"
  343. "# This file was generated by libcurl! Edit at your own risk.\n",
  344. out);
  345. for(e = h->list.head; e; e = n) {
  346. struct stsentry *sts = e->ptr;
  347. n = e->next;
  348. result = hsts_out(sts, out);
  349. if(result)
  350. break;
  351. }
  352. fclose(out);
  353. if(!result && tempstore && Curl_rename(tempstore, file))
  354. result = CURLE_WRITE_ERROR;
  355. if(result && tempstore)
  356. unlink(tempstore);
  357. }
  358. free(tempstore);
  359. skipsave:
  360. if(data->set.hsts_write) {
  361. /* if there's a write callback */
  362. struct curl_index i; /* count */
  363. i.total = h->list.size;
  364. i.index = 0;
  365. for(e = h->list.head; e; e = n) {
  366. struct stsentry *sts = e->ptr;
  367. bool stop;
  368. n = e->next;
  369. result = hsts_push(data, &i, sts, &stop);
  370. if(result || stop)
  371. break;
  372. i.index++;
  373. }
  374. }
  375. return result;
  376. }
  377. /* only returns SERIOUS errors */
  378. static CURLcode hsts_add(struct hsts *h, char *line)
  379. {
  380. /* Example lines:
  381. example.com "20191231 10:00:00"
  382. .example.net "20191231 10:00:00"
  383. */
  384. char host[MAX_HSTS_HOSTLEN + 1];
  385. char date[MAX_HSTS_DATELEN + 1];
  386. int rc;
  387. rc = sscanf(line,
  388. "%" MAX_HSTS_HOSTLENSTR "s \"%" MAX_HSTS_DATELENSTR "[^\"]\"",
  389. host, date);
  390. if(2 == rc) {
  391. time_t expires = strcmp(date, UNLIMITED) ? Curl_getdate_capped(date) :
  392. TIME_T_MAX;
  393. CURLcode result = CURLE_OK;
  394. char *p = host;
  395. bool subdomain = FALSE;
  396. struct stsentry *e;
  397. if(p[0] == '.') {
  398. p++;
  399. subdomain = TRUE;
  400. }
  401. /* only add it if not already present */
  402. e = Curl_hsts(h, p, subdomain);
  403. if(!e)
  404. result = hsts_create(h, p, subdomain, expires);
  405. else {
  406. /* the same host name, use the largest expire time */
  407. if(expires > e->expires)
  408. e->expires = expires;
  409. }
  410. if(result)
  411. return result;
  412. }
  413. return CURLE_OK;
  414. }
  415. /*
  416. * Load HSTS data from callback.
  417. *
  418. */
  419. static CURLcode hsts_pull(struct Curl_easy *data, struct hsts *h)
  420. {
  421. /* if the HSTS read callback is set, use it */
  422. if(data->set.hsts_read) {
  423. CURLSTScode sc;
  424. DEBUGASSERT(h);
  425. do {
  426. char buffer[MAX_HSTS_HOSTLEN + 1];
  427. struct curl_hstsentry e;
  428. e.name = buffer;
  429. e.namelen = sizeof(buffer)-1;
  430. e.includeSubDomains = FALSE; /* default */
  431. e.expire[0] = 0;
  432. e.name[0] = 0; /* just to make it clean */
  433. sc = data->set.hsts_read(data, &e, data->set.hsts_read_userp);
  434. if(sc == CURLSTS_OK) {
  435. time_t expires;
  436. CURLcode result;
  437. DEBUGASSERT(e.name[0]);
  438. if(!e.name[0])
  439. /* bail out if no name was stored */
  440. return CURLE_BAD_FUNCTION_ARGUMENT;
  441. if(e.expire[0])
  442. expires = Curl_getdate_capped(e.expire);
  443. else
  444. expires = TIME_T_MAX; /* the end of time */
  445. result = hsts_create(h, e.name,
  446. /* bitfield to bool conversion: */
  447. e.includeSubDomains ? TRUE : FALSE,
  448. expires);
  449. if(result)
  450. return result;
  451. }
  452. else if(sc == CURLSTS_FAIL)
  453. return CURLE_ABORTED_BY_CALLBACK;
  454. } while(sc == CURLSTS_OK);
  455. }
  456. return CURLE_OK;
  457. }
  458. /*
  459. * Load the HSTS cache from the given file. The text based line-oriented file
  460. * format is documented here: https://curl.se/docs/hsts.html
  461. *
  462. * This function only returns error on major problems that prevent hsts
  463. * handling to work completely. It will ignore individual syntactical errors
  464. * etc.
  465. */
  466. static CURLcode hsts_load(struct hsts *h, const char *file)
  467. {
  468. CURLcode result = CURLE_OK;
  469. FILE *fp;
  470. /* we need a private copy of the file name so that the hsts cache file
  471. name survives an easy handle reset */
  472. free(h->filename);
  473. h->filename = strdup(file);
  474. if(!h->filename)
  475. return CURLE_OUT_OF_MEMORY;
  476. fp = fopen(file, FOPEN_READTEXT);
  477. if(fp) {
  478. struct dynbuf buf;
  479. Curl_dyn_init(&buf, MAX_HSTS_LINE);
  480. while(Curl_get_line(&buf, fp)) {
  481. char *lineptr = Curl_dyn_ptr(&buf);
  482. while(*lineptr && ISBLANK(*lineptr))
  483. lineptr++;
  484. if(*lineptr == '#')
  485. /* skip commented lines */
  486. continue;
  487. hsts_add(h, lineptr);
  488. }
  489. Curl_dyn_free(&buf); /* free the line buffer */
  490. fclose(fp);
  491. }
  492. return result;
  493. }
  494. /*
  495. * Curl_hsts_loadfile() loads HSTS from file
  496. */
  497. CURLcode Curl_hsts_loadfile(struct Curl_easy *data,
  498. struct hsts *h, const char *file)
  499. {
  500. DEBUGASSERT(h);
  501. (void)data;
  502. return hsts_load(h, file);
  503. }
  504. /*
  505. * Curl_hsts_loadcb() loads HSTS from callback
  506. */
  507. CURLcode Curl_hsts_loadcb(struct Curl_easy *data, struct hsts *h)
  508. {
  509. if(h)
  510. return hsts_pull(data, h);
  511. return CURLE_OK;
  512. }
  513. void Curl_hsts_loadfiles(struct Curl_easy *data)
  514. {
  515. struct curl_slist *l = data->state.hstslist;
  516. if(l) {
  517. Curl_share_lock(data, CURL_LOCK_DATA_HSTS, CURL_LOCK_ACCESS_SINGLE);
  518. while(l) {
  519. (void)Curl_hsts_loadfile(data, data->hsts, l->data);
  520. l = l->next;
  521. }
  522. Curl_share_unlock(data, CURL_LOCK_DATA_HSTS);
  523. }
  524. }
  525. #endif /* CURL_DISABLE_HTTP || CURL_DISABLE_HSTS */