2
0

hsts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. #if defined(DEBUGBUILD) || defined(UNITTESTS)
  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_node *e;
  90. struct Curl_llist_node *n;
  91. for(e = Curl_llist_head(&h->list); e; e = n) {
  92. struct stsentry *sts = Curl_node_elem(e);
  93. n = Curl_node_next(e);
  94. hsts_free(sts);
  95. }
  96. free(h->filename);
  97. free(h);
  98. *hp = NULL;
  99. }
  100. }
  101. static CURLcode hsts_create(struct hsts *h,
  102. const char *hostname,
  103. bool subdomains,
  104. curl_off_t expires)
  105. {
  106. size_t hlen;
  107. DEBUGASSERT(h);
  108. DEBUGASSERT(hostname);
  109. hlen = strlen(hostname);
  110. if(hlen && (hostname[hlen - 1] == '.'))
  111. /* strip off any trailing dot */
  112. --hlen;
  113. if(hlen) {
  114. char *duphost;
  115. struct stsentry *sts = calloc(1, sizeof(struct stsentry));
  116. if(!sts)
  117. return CURLE_OUT_OF_MEMORY;
  118. duphost = Curl_memdup0(hostname, hlen);
  119. if(!duphost) {
  120. free(sts);
  121. return CURLE_OUT_OF_MEMORY;
  122. }
  123. sts->host = duphost;
  124. sts->expires = expires;
  125. sts->includeSubDomains = subdomains;
  126. Curl_llist_append(&h->list, sts, &sts->node);
  127. }
  128. return CURLE_OK;
  129. }
  130. CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
  131. const char *header)
  132. {
  133. const char *p = header;
  134. curl_off_t expires = 0;
  135. bool gotma = FALSE;
  136. bool gotinc = FALSE;
  137. bool subdomains = FALSE;
  138. struct stsentry *sts;
  139. time_t now = time(NULL);
  140. if(Curl_host_is_ipnum(hostname))
  141. /* "explicit IP address identification of all forms is excluded."
  142. / RFC 6797 */
  143. return CURLE_OK;
  144. do {
  145. while(*p && ISBLANK(*p))
  146. p++;
  147. if(strncasecompare("max-age", p, 7)) {
  148. bool quoted = FALSE;
  149. CURLofft offt;
  150. char *endp;
  151. if(gotma)
  152. return CURLE_BAD_FUNCTION_ARGUMENT;
  153. p += 7;
  154. while(*p && ISBLANK(*p))
  155. p++;
  156. if(*p++ != '=')
  157. return CURLE_BAD_FUNCTION_ARGUMENT;
  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_node_remove(&sts->node);
  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 hostname 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. struct stsentry *bestsub = NULL;
  233. if(h) {
  234. time_t now = time(NULL);
  235. size_t hlen = strlen(hostname);
  236. struct Curl_llist_node *e;
  237. struct Curl_llist_node *n;
  238. size_t blen = 0;
  239. if((hlen > MAX_HSTS_HOSTLEN) || !hlen)
  240. return NULL;
  241. if(hostname[hlen-1] == '.')
  242. /* remove the trailing dot */
  243. --hlen;
  244. for(e = Curl_llist_head(&h->list); e; e = n) {
  245. struct stsentry *sts = Curl_node_elem(e);
  246. size_t ntail;
  247. n = Curl_node_next(e);
  248. if(sts->expires <= now) {
  249. /* remove expired entries */
  250. Curl_node_remove(&sts->node);
  251. hsts_free(sts);
  252. continue;
  253. }
  254. ntail = strlen(sts->host);
  255. if((subdomain && sts->includeSubDomains) && (ntail < hlen)) {
  256. size_t offs = hlen - ntail;
  257. if((hostname[offs-1] == '.') &&
  258. strncasecompare(&hostname[offs], sts->host, ntail) &&
  259. (ntail > blen)) {
  260. /* save the tail match with the longest tail */
  261. bestsub = sts;
  262. blen = ntail;
  263. }
  264. }
  265. /* avoid strcasecompare because the host name is not null terminated */
  266. if((hlen == ntail) && strncasecompare(hostname, sts->host, hlen))
  267. return sts;
  268. }
  269. }
  270. return bestsub;
  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_node *e;
  329. struct Curl_llist_node *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 filename */
  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 = Curl_llist_head(&h->list); e; e = n) {
  348. struct stsentry *sts = Curl_node_elem(e);
  349. n = Curl_node_next(e);
  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 is a write callback */
  364. struct curl_index i; /* count */
  365. i.total = Curl_llist_count(&h->list);
  366. i.index = 0;
  367. for(e = Curl_llist_head(&h->list); e; e = n) {
  368. struct stsentry *sts = Curl_node_elem(e);
  369. bool stop;
  370. n = Curl_node_next(e);
  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 if(strcasecompare(p, e->host)) {
  408. /* the same hostname, 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. DEBUGASSERT(e.name[0]);
  440. if(!e.name[0])
  441. /* bail out if no name was stored */
  442. return CURLE_BAD_FUNCTION_ARGUMENT;
  443. if(e.expire[0])
  444. expires = Curl_getdate_capped(e.expire);
  445. else
  446. expires = TIME_T_MAX; /* the end of time */
  447. result = hsts_create(h, e.name,
  448. /* bitfield to bool conversion: */
  449. e.includeSubDomains ? TRUE : FALSE,
  450. expires);
  451. if(result)
  452. return result;
  453. }
  454. else if(sc == CURLSTS_FAIL)
  455. return CURLE_ABORTED_BY_CALLBACK;
  456. } while(sc == CURLSTS_OK);
  457. }
  458. return CURLE_OK;
  459. }
  460. /*
  461. * Load the HSTS cache from the given file. The text based line-oriented file
  462. * format is documented here: https://curl.se/docs/hsts.html
  463. *
  464. * This function only returns error on major problems that prevent hsts
  465. * handling to work completely. It will ignore individual syntactical errors
  466. * etc.
  467. */
  468. static CURLcode hsts_load(struct hsts *h, const char *file)
  469. {
  470. CURLcode result = CURLE_OK;
  471. FILE *fp;
  472. /* we need a private copy of the filename 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. struct dynbuf buf;
  481. Curl_dyn_init(&buf, MAX_HSTS_LINE);
  482. while(Curl_get_line(&buf, fp)) {
  483. char *lineptr = Curl_dyn_ptr(&buf);
  484. while(*lineptr && ISBLANK(*lineptr))
  485. lineptr++;
  486. /*
  487. * Skip empty or commented lines, since we know the line will have a
  488. * trailing newline from Curl_get_line we can treat length 1 as empty.
  489. */
  490. if((*lineptr == '#') || strlen(lineptr) <= 1)
  491. continue;
  492. hsts_add(h, lineptr);
  493. }
  494. Curl_dyn_free(&buf); /* free the line buffer */
  495. fclose(fp);
  496. }
  497. return result;
  498. }
  499. /*
  500. * Curl_hsts_loadfile() loads HSTS from file
  501. */
  502. CURLcode Curl_hsts_loadfile(struct Curl_easy *data,
  503. struct hsts *h, const char *file)
  504. {
  505. DEBUGASSERT(h);
  506. (void)data;
  507. return hsts_load(h, file);
  508. }
  509. /*
  510. * Curl_hsts_loadcb() loads HSTS from callback
  511. */
  512. CURLcode Curl_hsts_loadcb(struct Curl_easy *data, struct hsts *h)
  513. {
  514. if(h)
  515. return hsts_pull(data, h);
  516. return CURLE_OK;
  517. }
  518. void Curl_hsts_loadfiles(struct Curl_easy *data)
  519. {
  520. struct curl_slist *l = data->state.hstslist;
  521. if(l) {
  522. Curl_share_lock(data, CURL_LOCK_DATA_HSTS, CURL_LOCK_ACCESS_SINGLE);
  523. while(l) {
  524. (void)Curl_hsts_loadfile(data, data->hsts, l->data);
  525. l = l->next;
  526. }
  527. Curl_share_unlock(data, CURL_LOCK_DATA_HSTS);
  528. }
  529. }
  530. #endif /* CURL_DISABLE_HTTP || CURL_DISABLE_HSTS */