2
0

hsts.c 15 KB

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