https_example.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE "f:\\dev\\lwip\\https_test\\privateKey.key"
  61. #define LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE "f:\\dev\\lwip\\https_test\\certificate.crt"
  62. #if LWIP_HTTPD_EXAMPLE_HTTPS
  63. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE
  64. #error "define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE to the created server private key"
  65. #endif
  66. /* If the key file is password-protected, define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS */
  67. #ifdef LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS
  68. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN
  69. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN strlen(LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS)
  70. #endif
  71. #else
  72. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS NULL
  73. #define LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN 0
  74. #endif
  75. #ifndef LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE
  76. #error "define LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE to the created server certificate"
  77. #endif
  78. static u8_t *read_file(const char *filename, size_t *file_size)
  79. {
  80. u8_t *buf;
  81. long fsize;
  82. FILE *f = fopen(filename, "rb");
  83. if (!f) {
  84. return NULL;
  85. }
  86. fseek(f, 0, SEEK_END);
  87. fsize = ftell(f);
  88. fseek(f, 0, SEEK_SET);
  89. buf = malloc(fsize + 1);
  90. if (!buf) {
  91. fclose(f);
  92. return NULL;
  93. }
  94. fread(buf, 1, fsize, f);
  95. fclose(f);
  96. buf[fsize] = 0;
  97. if (file_size) {
  98. /* Note: the '+ 1' is required for mbedTLS to correctly parse the buffer */
  99. *file_size = (size_t)(fsize + 1);
  100. }
  101. return buf;
  102. }
  103. /** This function loads a server certificate and private key as x509 from disk.
  104. * For information how to create such files, see mbedTLS tutorial ("How to
  105. * generate a self-signed certificate") or OpenSSL documentation ("How to
  106. * generate a self-signed certificate and private key using OpenSSL"), e.g.
  107. * 'openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt'
  108. * Copy the resulting files and define the path to them
  109. */
  110. void
  111. https_ex_init(void)
  112. {
  113. struct altcp_tls_config *conf;
  114. u8_t *privkey, *cert;
  115. size_t privkey_size, cert_size;
  116. privkey = read_file(LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE, &privkey_size);
  117. LWIP_ASSERT("Failed to open https server private key", privkey != NULL);
  118. cert = read_file(LWIP_HTTPD_EXAMPLE_HTTPS_CERT_FILE, &cert_size);
  119. LWIP_ASSERT("Failed to open https server certificate", cert != NULL);
  120. conf = altcp_tls_create_config_server_privkey_cert(privkey, privkey_size,
  121. LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS, LWIP_HTTPD_EXAMPLE_HTTPS_KEY_FILE_PASS_LEN, cert, cert_size);
  122. LWIP_ASSERT("Failed to create https server config", conf != NULL);
  123. httpd_inits(conf);
  124. /* secure erase should be done in production environment */
  125. free(privkey);
  126. free(cert);
  127. }
  128. #endif /* LWIP_HTTPD_EXAMPLE_HTTPS */