hsts.c 13 KB

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