hsts.c 13 KB

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