https_example.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file
  3. * HTTPD https example
  4. *
  5. * This file demonstrates how to initialize httpd for https.
  6. * To do this, it needs 2 files:
  7. * - server certificate
  8. * - server private key
  9. *
  10. * In addition to that, watch out for resource shortage. You'll need plenty of
  11. * heap (start with MEM_SIZE >= 200 KByte or monitor its err counters) and be
  12. * sure to at least set the following settings high enough (monitor
  13. * lwip_stats for an idea of what's needed):
  14. * - MEMP_NUM_TCP_PCB/MEMP_NUM_ALTCP_PCB
  15. * - MEMP_NUM_TCPIP_MSG_INPKT
  16. * - MEMP_NUM_TCP_SEG
  17. */
  18. /*
  19. * Copyright (c) 2017-2019 Simon Goldschmidt
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without modification,
  23. * are permitted provided that the following conditions are met:
  24. *
  25. * 1. Redistributions of source code must retain the above copyright notice,
  26. * this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright notice,
  28. * this list of conditions and the following disclaimer in the documentation
  29. * and/or other materials provided with the distribution.
  30. * 3. The name of the author may not be used to endorse or promote products
  31. * derived from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  34. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  35. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  36. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  37. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  38. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  41. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  42. * OF SUCH DAMAGE.
  43. *
  44. * This file is part of the lwIP TCP/IP stack.
  45. *
  46. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  47. *
  48. */
  49. #include "lwip/opt.h"
  50. #include "https_example.h"
  51. #include "lwip/altcp_tls.h"
  52. #include "lwip/apps/httpd.h"
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. /** define LWIP_HTTPD_EXAMPLE_HTTPS to 1 to enable this file system */
  57. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS
  58. #define LWIP_HTTPD_EXAMPLE_HTTPS 0
  59. #endif
  60. #if LWIP_HTTPD_EXAMPLE_HTTPS && LWIP_ALTCP_TLS
  61. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE
  62. #error "define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE to the created server private key"
  63. #endif
  64. /* If the key file is password-protected, define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS */
  65. #ifdef LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS
  66. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN
  67. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN strlen(LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS)
  68. #endif
  69. #else
  70. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS NULL
  71. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN 0
  72. #endif
  73. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE
  74. #error "define LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE to the created server certificate"
  75. #endif
  76. static u8_t *read_file(const char *filename, size_t *file_size)
  77. {
  78. u8_t *buf;
  79. long fsize;
  80. FILE *f = fopen(filename, "rb");
  81. if (!f) {
  82. return NULL;
  83. }
  84. fseek(f, 0, SEEK_END);
  85. fsize = ftell(f);
  86. fseek(f, 0, SEEK_SET);
  87. buf = (u8_t *)malloc(fsize + 1);
  88. if (!buf) {
  89. fclose(f);
  90. return NULL;
  91. }
  92. fread(buf, 1, fsize, f);
  93. fclose(f);
  94. buf[fsize] = 0;
  95. if (file_size) {
  96. /* Note: the '+ 1' is required for mbedTLS to correctly parse the buffer */
  97. *file_size = (size_t)(fsize + 1);
  98. }
  99. return buf;
  100. }
  101. /** This function loads a server certificate and private key as x509 from disk.
  102. * For information how to create such files, see mbedTLS tutorial ("How to
  103. * generate a self-signed certificate") or OpenSSL documentation ("How to
  104. * generate a self-signed certificate and private key using OpenSSL"), e.g.
  105. * 'openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt'
  106. * Copy the resulting files and define the path to them
  107. */
  108. void
  109. https_ex_init(void)
  110. {
  111. struct altcp_tls_config *conf;
  112. u8_t *privkey, *cert;
  113. size_t privkey_size, cert_size;
  114. privkey = read_file(LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE, &privkey_size);
  115. LWIP_ASSERT("Failed to open https server private key", privkey != NULL);
  116. cert = read_file(LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE, &cert_size);
  117. LWIP_ASSERT("Failed to open https server certificate", cert != NULL);
  118. conf = altcp_tls_create_config_server_privkey_cert(privkey, privkey_size,
  119. LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS, LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN, cert, cert_size);
  120. LWIP_ASSERT("Failed to create https server config", conf != NULL);
  121. httpd_inits(conf);
  122. /* secure erase should be done in production environment */
  123. free(privkey);
  124. free(cert);
  125. }
  126. #endif /* LWIP_HTTPD_EXAMPLE_HTTPS */