hsts.c 15 KB

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