hsts-preload.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /* <DESC>
  25. * Preload domains to HSTS
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. struct entry {
  32. const char *name;
  33. const char *exp;
  34. };
  35. static const struct entry preload_hosts[] = {
  36. { "example.com", "20370320 01:02:03" },
  37. { "curl.se", "20370320 03:02:01" },
  38. { NULL, NULL } /* end of list marker */
  39. };
  40. struct state {
  41. int index;
  42. };
  43. /* "read" is from the point of the library, it wants data from us. One domain
  44. entry per invoke. */
  45. static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
  46. void *userp)
  47. {
  48. const char *host;
  49. const char *expire;
  50. struct state *s = (struct state *)userp;
  51. (void)easy;
  52. host = preload_hosts[s->index].name;
  53. expire = preload_hosts[s->index++].exp;
  54. if(host && (strlen(host) < e->namelen)) {
  55. strcpy(e->name, host);
  56. e->includeSubDomains = 0;
  57. strcpy(e->expire, expire);
  58. fprintf(stderr, "HSTS preload '%s' until '%s'\n", host, expire);
  59. }
  60. else
  61. return CURLSTS_DONE;
  62. return CURLSTS_OK;
  63. }
  64. static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
  65. struct curl_index *i, void *userp)
  66. {
  67. (void)easy;
  68. (void)userp; /* we have no custom input */
  69. printf("[%u/%u] %s %s\n", (unsigned int)i->index, (unsigned int)i->total,
  70. e->name, e->expire);
  71. return CURLSTS_OK;
  72. }
  73. int main(void)
  74. {
  75. CURL *curl;
  76. CURLcode res;
  77. curl = curl_easy_init();
  78. if(curl) {
  79. struct state st = {0};
  80. /* enable HSTS for this handle */
  81. curl_easy_setopt(curl, CURLOPT_HSTS_CTRL, (long)CURLHSTS_ENABLE);
  82. /* function to call at first to populate the cache before the transfer */
  83. curl_easy_setopt(curl, CURLOPT_HSTSREADFUNCTION, hstsread);
  84. curl_easy_setopt(curl, CURLOPT_HSTSREADDATA, &st);
  85. /* function to call after transfer to store the new state of the HSTS
  86. cache */
  87. curl_easy_setopt(curl, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  88. curl_easy_setopt(curl, CURLOPT_HSTSWRITEDATA, NULL);
  89. /* use the domain with HTTP but due to the preload, it should do the
  90. transfer using HTTPS */
  91. curl_easy_setopt(curl, CURLOPT_URL, "http://curl.se");
  92. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  93. /* Perform the request, res gets the return code */
  94. res = curl_easy_perform(curl);
  95. /* Check for errors */
  96. if(res != CURLE_OK)
  97. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  98. curl_easy_strerror(res));
  99. /* always cleanup */
  100. curl_easy_cleanup(curl);
  101. }
  102. return 0;
  103. }