ec_example.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Example for embedding a server including the HTTPS server key and
  2. * certificate. All response is generated from a callback. The server
  3. * does not access the file system at all. */
  4. /* Building: change into the examples/embed_certificate folder and
  5. * use "./build.sh" on Linux (no, you don't need a Makefile). */
  6. #include <openssl/ssl.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include "civetweb.h"
  12. /* This array corresponds to the content of the file
  13. * /resources/cert/server.crt converted from BASE64 to HEX.
  14. * When using this code, you need to REPLACE IT BY YOUR OWN CERTIFICATE!
  15. */
  16. const uint8_t SSL_CERT_ASN1[] = {
  17. 0x30, 0x82, 0x04, 0x40, 0x30, 0x82, 0x03, 0x28, 0xa0, 0x03, 0x02, 0x01,
  18. 0x02, 0x02, 0x14, 0x49, 0x65, 0x5b, 0x35, 0xce, 0x42, 0x20, 0x15, 0xa7,
  19. 0xc4, 0x8a, 0x29, 0xe6, 0x44, 0x58, 0x6c, 0x11, 0xe6, 0x8c, 0xa7, 0x30,
  20. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  21. 0x05, 0x00, 0x30, 0x71, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  22. 0x06, 0x13, 0x02, 0x41, 0x41, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
  23. 0x04, 0x08, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
  24. 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09,
  25. 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30,
  26. 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61,
  27. 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
  28. 0x04, 0x0b, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
  29. 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09,
  30. 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17,
  31. 0x0d, 0x32, 0x31, 0x30, 0x34, 0x30, 0x34, 0x31, 0x38, 0x33, 0x39, 0x35,
  32. 0x34, 0x5a, 0x17, 0x0d, 0x33, 0x31, 0x30, 0x34, 0x30, 0x32, 0x31, 0x38,
  33. 0x33, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x71, 0x31, 0x0b, 0x30, 0x09, 0x06,
  34. 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x41, 0x31, 0x12, 0x30, 0x10,
  35. 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
  36. 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04,
  37. 0x07, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
  38. 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x6c,
  39. 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10,
  40. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
  41. 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04,
  42. 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
  43. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  44. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  45. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xca, 0x25, 0x51,
  46. 0xe7, 0xb4, 0x06, 0x2d, 0x11, 0xe1, 0x33, 0xfb, 0xde, 0xe1, 0x92, 0xbe,
  47. 0x63, 0x29, 0xf9, 0x1a, 0xea, 0xcd, 0x2d, 0x71, 0xf8, 0xc9, 0xae, 0x2b,
  48. 0x35, 0xae, 0x6f, 0xd1, 0x71, 0xae, 0xf5, 0x6f, 0xcc, 0xdd, 0xcf, 0x25,
  49. 0xd1, 0x59, 0x78, 0x30, 0x82, 0xec, 0x65, 0xe3, 0xd7, 0xa2, 0x6f, 0x7c,
  50. 0x84, 0x82, 0xec, 0x2f, 0xd4, 0x37, 0x7d, 0xbc, 0x6c, 0x36, 0x07, 0x82,
  51. 0x24, 0xed, 0x21, 0xe5, 0x81, 0xc3, 0x12, 0x2b, 0x54, 0x96, 0x44, 0xf7,
  52. 0xdd, 0x6a, 0x58, 0xe1, 0x2f, 0x3f, 0xcd, 0x67, 0x1e, 0x32, 0xc8, 0x99,
  53. 0x9b, 0xda, 0x33, 0x54, 0xdb, 0x52, 0x5d, 0xb2, 0xc5, 0x80, 0x50, 0x3b,
  54. 0x41, 0xe1, 0xd3, 0xa8, 0x0c, 0x0d, 0x23, 0x90, 0xe4, 0x62, 0xf4, 0x4f,
  55. 0xd6, 0x1b, 0xc5, 0x5a, 0xe0, 0x18, 0xa9, 0x3d, 0x74, 0xbb, 0x30, 0x20,
  56. 0xbd, 0x85, 0xbd, 0xc3, 0xbd, 0x69, 0x86, 0x62, 0x75, 0xa6, 0x9e, 0x1c,
  57. 0x74, 0x45, 0xa2, 0x5e, 0x67, 0x4c, 0xe0, 0xa2, 0x87, 0x13, 0x28, 0x8c,
  58. 0x76, 0xbf, 0x2e, 0xdf, 0xe7, 0xaf, 0x79, 0x12, 0xa9, 0x65, 0x42, 0x66,
  59. 0x1c, 0x25, 0xee, 0xca, 0x45, 0x29, 0x30, 0xff, 0x9c, 0x26, 0x14, 0x06,
  60. 0x63, 0x91, 0x08, 0xa4, 0x58, 0x8d, 0x22, 0x0b, 0xc8, 0x18, 0x8e, 0x42,
  61. 0xcd, 0x16, 0xb5, 0xdf, 0xa8, 0x7b, 0x7a, 0xb4, 0x71, 0x5f, 0xc2, 0xaa,
  62. 0x60, 0x99, 0xc5, 0x2f, 0xef, 0x13, 0xd1, 0x9b, 0x9c, 0x0a, 0x8c, 0x14,
  63. 0x7a, 0x8a, 0xb9, 0xdf, 0xdf, 0x9d, 0xaa, 0x76, 0x52, 0xff, 0x0c, 0x93,
  64. 0x68, 0x3f, 0x73, 0x0d, 0xb7, 0xc0, 0x21, 0x67, 0x48, 0xfd, 0xf2, 0xe2,
  65. 0xc3, 0x9c, 0xf5, 0xd4, 0x89, 0x86, 0xa9, 0x7b, 0x6a, 0x1b, 0x81, 0xa0,
  66. 0x89, 0x39, 0x71, 0xc9, 0x3a, 0x65, 0x54, 0xdb, 0x06, 0x22, 0x82, 0x7f,
  67. 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81, 0xcf, 0x30, 0x81, 0xcc,
  68. 0x30, 0x81, 0x98, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0x90, 0x30,
  69. 0x81, 0x8d, 0xa1, 0x75, 0xa4, 0x73, 0x30, 0x71, 0x31, 0x0b, 0x30, 0x09,
  70. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x41, 0x31, 0x12, 0x30,
  71. 0x10, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61,
  72. 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
  73. 0x04, 0x07, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
  74. 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09,
  75. 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30,
  76. 0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61,
  77. 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
  78. 0x04, 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
  79. 0x74, 0x82, 0x14, 0x60, 0x3a, 0x75, 0xa0, 0xfc, 0x63, 0x14, 0xe1, 0x70,
  80. 0x07, 0x16, 0x23, 0x7b, 0x84, 0x63, 0x3d, 0xe1, 0x1d, 0x07, 0x12, 0x30,
  81. 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30,
  82. 0x00, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02,
  83. 0x04, 0xf0, 0x30, 0x14, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x0d, 0x30,
  84. 0x0b, 0x82, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
  85. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  86. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x13, 0x3c, 0x41, 0x03,
  87. 0xb2, 0x58, 0x00, 0x0a, 0x73, 0x1f, 0xb4, 0x15, 0xc7, 0x5f, 0xa5, 0x07,
  88. 0x14, 0xa6, 0xee, 0x7c, 0x83, 0x40, 0x85, 0x70, 0x40, 0x6c, 0x08, 0x94,
  89. 0x71, 0x77, 0x1c, 0xd8, 0x9a, 0xb3, 0x8d, 0x7c, 0xfa, 0x45, 0x96, 0x03,
  90. 0x51, 0x92, 0x46, 0xf8, 0x84, 0x6d, 0x09, 0x40, 0x3a, 0x53, 0x07, 0xc3,
  91. 0xd6, 0x1f, 0x72, 0xe6, 0x15, 0xeb, 0x49, 0x3a, 0x63, 0x2d, 0x11, 0x58,
  92. 0x73, 0xe3, 0x25, 0xde, 0xb0, 0x85, 0x5b, 0x45, 0xc6, 0xb9, 0x46, 0x2d,
  93. 0x86, 0x2d, 0xc9, 0x9b, 0x1f, 0x73, 0x6a, 0x18, 0xe7, 0x94, 0x49, 0x4c,
  94. 0x77, 0xea, 0x0b, 0xb6, 0xd1, 0xfb, 0x77, 0x6a, 0x1b, 0xfc, 0xb0, 0xcf,
  95. 0x5e, 0x29, 0xf1, 0xd5, 0xfc, 0x93, 0x8f, 0x48, 0xe3, 0xe0, 0xbd, 0x09,
  96. 0xa5, 0x7d, 0xa1, 0x50, 0x2c, 0x9f, 0xdc, 0x44, 0xe4, 0x85, 0xbb, 0xca,
  97. 0xab, 0xbd, 0x73, 0xa7, 0xd8, 0x3e, 0x31, 0x53, 0xfe, 0xd4, 0xc3, 0xd7,
  98. 0x05, 0x3c, 0x93, 0x72, 0xcc, 0x16, 0x2b, 0xe6, 0x33, 0xf0, 0xbd, 0xea,
  99. 0x9a, 0x5f, 0x24, 0x39, 0x7e, 0xfe, 0x82, 0x34, 0xa6, 0xb2, 0x23, 0xe4,
  100. 0x67, 0x8f, 0xf9, 0x60, 0x6b, 0xd7, 0x2e, 0xd9, 0xfb, 0x9a, 0x12, 0x3c,
  101. 0xb5, 0x3b, 0xf9, 0x9d, 0x0a, 0xbf, 0xff, 0x2d, 0x6b, 0x33, 0x82, 0xe6,
  102. 0x54, 0x75, 0x2b, 0xf2, 0x74, 0x3b, 0xc7, 0x96, 0xa5, 0x06, 0x70, 0xb6,
  103. 0x0a, 0x43, 0x7a, 0x7b, 0xa1, 0xc3, 0x8e, 0x2a, 0x58, 0xcc, 0xff, 0x49,
  104. 0xeb, 0x51, 0xb6, 0x7c, 0x7e, 0xa2, 0x68, 0x9f, 0x82, 0xe8, 0x2e, 0x4e,
  105. 0xf9, 0x14, 0xc7, 0xbe, 0x60, 0x42, 0x65, 0x9d, 0x5a, 0xce, 0xca, 0x09,
  106. 0xf7, 0xee, 0x48, 0x41, 0x93, 0x69, 0x7c, 0x3e, 0x47, 0xf0, 0x5e, 0x60,
  107. 0x5d, 0x1d, 0xa3, 0x67, 0x59, 0x3c, 0xf9, 0x3c, 0x49, 0x63, 0x74, 0x84};
  108. /* This array corresponds to the content of the file
  109. * /resources/cert/server.key converted from BASE64 to HEX.
  110. * When using this code, you need to REPLACE IT BY YOUR OWN PRIVATE KEY!
  111. */
  112. const uint8_t SSL_KEY_ASN1[] = {
  113. 0x30, 0x82, 0x04, 0xa2, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  114. 0xca, 0x25, 0x51, 0xe7, 0xb4, 0x06, 0x2d, 0x11, 0xe1, 0x33, 0xfb, 0xde,
  115. 0xe1, 0x92, 0xbe, 0x63, 0x29, 0xf9, 0x1a, 0xea, 0xcd, 0x2d, 0x71, 0xf8,
  116. 0xc9, 0xae, 0x2b, 0x35, 0xae, 0x6f, 0xd1, 0x71, 0xae, 0xf5, 0x6f, 0xcc,
  117. 0xdd, 0xcf, 0x25, 0xd1, 0x59, 0x78, 0x30, 0x82, 0xec, 0x65, 0xe3, 0xd7,
  118. 0xa2, 0x6f, 0x7c, 0x84, 0x82, 0xec, 0x2f, 0xd4, 0x37, 0x7d, 0xbc, 0x6c,
  119. 0x36, 0x07, 0x82, 0x24, 0xed, 0x21, 0xe5, 0x81, 0xc3, 0x12, 0x2b, 0x54,
  120. 0x96, 0x44, 0xf7, 0xdd, 0x6a, 0x58, 0xe1, 0x2f, 0x3f, 0xcd, 0x67, 0x1e,
  121. 0x32, 0xc8, 0x99, 0x9b, 0xda, 0x33, 0x54, 0xdb, 0x52, 0x5d, 0xb2, 0xc5,
  122. 0x80, 0x50, 0x3b, 0x41, 0xe1, 0xd3, 0xa8, 0x0c, 0x0d, 0x23, 0x90, 0xe4,
  123. 0x62, 0xf4, 0x4f, 0xd6, 0x1b, 0xc5, 0x5a, 0xe0, 0x18, 0xa9, 0x3d, 0x74,
  124. 0xbb, 0x30, 0x20, 0xbd, 0x85, 0xbd, 0xc3, 0xbd, 0x69, 0x86, 0x62, 0x75,
  125. 0xa6, 0x9e, 0x1c, 0x74, 0x45, 0xa2, 0x5e, 0x67, 0x4c, 0xe0, 0xa2, 0x87,
  126. 0x13, 0x28, 0x8c, 0x76, 0xbf, 0x2e, 0xdf, 0xe7, 0xaf, 0x79, 0x12, 0xa9,
  127. 0x65, 0x42, 0x66, 0x1c, 0x25, 0xee, 0xca, 0x45, 0x29, 0x30, 0xff, 0x9c,
  128. 0x26, 0x14, 0x06, 0x63, 0x91, 0x08, 0xa4, 0x58, 0x8d, 0x22, 0x0b, 0xc8,
  129. 0x18, 0x8e, 0x42, 0xcd, 0x16, 0xb5, 0xdf, 0xa8, 0x7b, 0x7a, 0xb4, 0x71,
  130. 0x5f, 0xc2, 0xaa, 0x60, 0x99, 0xc5, 0x2f, 0xef, 0x13, 0xd1, 0x9b, 0x9c,
  131. 0x0a, 0x8c, 0x14, 0x7a, 0x8a, 0xb9, 0xdf, 0xdf, 0x9d, 0xaa, 0x76, 0x52,
  132. 0xff, 0x0c, 0x93, 0x68, 0x3f, 0x73, 0x0d, 0xb7, 0xc0, 0x21, 0x67, 0x48,
  133. 0xfd, 0xf2, 0xe2, 0xc3, 0x9c, 0xf5, 0xd4, 0x89, 0x86, 0xa9, 0x7b, 0x6a,
  134. 0x1b, 0x81, 0xa0, 0x89, 0x39, 0x71, 0xc9, 0x3a, 0x65, 0x54, 0xdb, 0x06,
  135. 0x22, 0x82, 0x7f, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  136. 0x00, 0x73, 0xbb, 0x54, 0x1e, 0x34, 0xca, 0x48, 0x69, 0x71, 0x26, 0xc2,
  137. 0xf0, 0x02, 0xf3, 0x71, 0xbe, 0xf2, 0x5b, 0xe5, 0x16, 0x42, 0xeb, 0xde,
  138. 0xd1, 0x92, 0x1d, 0xfe, 0x2d, 0x18, 0xb6, 0x7a, 0x11, 0xfd, 0x1a, 0x15,
  139. 0xad, 0x13, 0xdc, 0xb2, 0x09, 0x1e, 0x91, 0x1a, 0x2d, 0x0a, 0xcc, 0xf6,
  140. 0xda, 0x10, 0xec, 0x85, 0x3c, 0x94, 0x7c, 0x46, 0x91, 0xd8, 0x47, 0x4b,
  141. 0x66, 0x24, 0xb4, 0xbd, 0xc5, 0x08, 0x62, 0x9c, 0xb4, 0x63, 0x0b, 0x76,
  142. 0xf5, 0x51, 0xa7, 0x20, 0xc5, 0x8a, 0x4a, 0x62, 0x7a, 0x1b, 0xac, 0x2c,
  143. 0x7a, 0x74, 0x96, 0xb6, 0xa3, 0x2d, 0x14, 0xb0, 0x63, 0x74, 0xcf, 0xa2,
  144. 0x37, 0x42, 0xd4, 0x2c, 0x68, 0xf6, 0xb2, 0xa8, 0x06, 0x66, 0x4b, 0x53,
  145. 0x7b, 0xfe, 0x4f, 0x63, 0x99, 0xf0, 0x82, 0x58, 0x19, 0xee, 0xe4, 0x8e,
  146. 0x03, 0xd3, 0xdb, 0xa5, 0x12, 0xfc, 0x8b, 0xfd, 0x90, 0xe2, 0x55, 0xd8,
  147. 0xb4, 0x59, 0xc8, 0x75, 0xab, 0x14, 0x02, 0x65, 0xee, 0x00, 0xf0, 0xdc,
  148. 0xa4, 0x5f, 0x1e, 0xbb, 0x10, 0x90, 0x57, 0xea, 0x45, 0x33, 0x75, 0xfd,
  149. 0x0c, 0x97, 0xe6, 0xd6, 0xe6, 0xaa, 0x08, 0x22, 0x26, 0x4a, 0x34, 0x0d,
  150. 0xfe, 0xb5, 0xde, 0xdb, 0xa8, 0xc0, 0x67, 0x83, 0x5f, 0x27, 0x62, 0x55,
  151. 0x5a, 0xa0, 0x83, 0x25, 0xe6, 0x19, 0xd9, 0x78, 0x33, 0x7b, 0x2b, 0xa0,
  152. 0x53, 0x1d, 0xed, 0x62, 0xd1, 0x10, 0x95, 0x9f, 0xb0, 0xa2, 0xe7, 0xe8,
  153. 0x58, 0x39, 0x31, 0x76, 0x65, 0x53, 0x28, 0x23, 0x98, 0xa9, 0xab, 0xc2,
  154. 0x9c, 0x57, 0x0e, 0x9c, 0x17, 0x10, 0x14, 0x35, 0x5b, 0x5d, 0xce, 0x94,
  155. 0x1b, 0xdf, 0x8b, 0x42, 0xc0, 0xc6, 0x0e, 0x5c, 0x48, 0x67, 0x4f, 0xaf,
  156. 0x27, 0x3d, 0xc4, 0xda, 0xfa, 0xb4, 0xbd, 0x8e, 0x55, 0xdd, 0xa8, 0x18,
  157. 0x34, 0x01, 0xb6, 0xd8, 0x09, 0x02, 0x81, 0x81, 0x00, 0xe9, 0x53, 0x5f,
  158. 0xf9, 0x0e, 0xdf, 0x1e, 0x18, 0x90, 0xcb, 0xb8, 0x66, 0xea, 0x70, 0x6a,
  159. 0x72, 0xc3, 0x6e, 0x87, 0x7a, 0x79, 0x2f, 0xc4, 0x50, 0x32, 0x32, 0xcf,
  160. 0x97, 0xa7, 0x3b, 0x2b, 0x90, 0xd1, 0x05, 0x39, 0x5f, 0x51, 0x47, 0x79,
  161. 0xc3, 0x1d, 0xd8, 0xaa, 0xca, 0xda, 0x1a, 0x38, 0x6b, 0xfa, 0x02, 0x29,
  162. 0xbd, 0x43, 0x45, 0xed, 0xe8, 0xae, 0xf5, 0xc0, 0xaa, 0xde, 0xe8, 0xac,
  163. 0x21, 0xf5, 0x62, 0x99, 0xea, 0xeb, 0xb2, 0x54, 0xfd, 0xf3, 0x6a, 0x9d,
  164. 0x13, 0xbe, 0x09, 0x51, 0xef, 0x0f, 0x12, 0xb2, 0x14, 0x20, 0xe9, 0x6d,
  165. 0xfe, 0x6c, 0x63, 0x02, 0x7c, 0xd7, 0x0e, 0xa9, 0x2f, 0x2b, 0x52, 0x68,
  166. 0x83, 0x50, 0xdd, 0xc2, 0xf1, 0x86, 0x7c, 0x33, 0xe8, 0x62, 0x6e, 0x8e,
  167. 0x48, 0x50, 0x5c, 0x84, 0x7e, 0x22, 0x36, 0x60, 0x74, 0x16, 0x27, 0xd5,
  168. 0x77, 0xb6, 0x94, 0x7e, 0x75, 0x02, 0x81, 0x81, 0x00, 0xdd, 0xca, 0x42,
  169. 0x1f, 0x3d, 0x3f, 0xc7, 0x4e, 0xce, 0x7d, 0x37, 0x09, 0xef, 0xf8, 0xee,
  170. 0x67, 0x97, 0xbb, 0xf8, 0x34, 0x49, 0x44, 0xa9, 0x9a, 0x07, 0x7f, 0x48,
  171. 0xaa, 0xb9, 0x77, 0xb6, 0x22, 0xfd, 0x88, 0x97, 0x77, 0x20, 0x6e, 0x0c,
  172. 0x67, 0x19, 0x2e, 0xc9, 0x58, 0x3c, 0xfd, 0xdb, 0x3b, 0xfb, 0x0b, 0xfb,
  173. 0x86, 0xa2, 0x74, 0x31, 0x60, 0xaa, 0x27, 0x41, 0x3d, 0xdf, 0x9a, 0xaa,
  174. 0xb3, 0xd8, 0x9a, 0x0a, 0x2d, 0xf9, 0xd7, 0xee, 0x67, 0xdc, 0x49, 0x40,
  175. 0x74, 0x30, 0x32, 0xb7, 0x94, 0xfd, 0x84, 0x13, 0xb8, 0x24, 0x89, 0xdf,
  176. 0xee, 0x7d, 0xe3, 0x1b, 0xe5, 0x76, 0xc4, 0x1b, 0x81, 0x32, 0xa6, 0x0f,
  177. 0x07, 0x26, 0x87, 0x3b, 0xff, 0xaf, 0xa9, 0x25, 0x71, 0xd0, 0x70, 0x2e,
  178. 0xa8, 0xbc, 0x7e, 0xe2, 0xe2, 0x6f, 0x71, 0x5e, 0xe2, 0xad, 0xc1, 0x22,
  179. 0x0c, 0x3f, 0xc4, 0x35, 0xcb, 0x02, 0x81, 0x80, 0x46, 0x40, 0x08, 0x21,
  180. 0x60, 0xcc, 0xe4, 0xae, 0xd8, 0xc9, 0xbd, 0x97, 0x9e, 0xf6, 0x81, 0xd6,
  181. 0x53, 0xe9, 0x2f, 0x79, 0x3c, 0x8b, 0x99, 0x3b, 0xdc, 0x21, 0x58, 0x47,
  182. 0x7c, 0xde, 0x5f, 0xdb, 0x96, 0x53, 0x50, 0x56, 0xd6, 0x8e, 0x02, 0xa7,
  183. 0x30, 0x91, 0x4f, 0xbb, 0x0b, 0xb7, 0xe1, 0x4d, 0x01, 0x55, 0x2d, 0x64,
  184. 0x02, 0xa1, 0x47, 0x64, 0x4b, 0x69, 0x4a, 0xbd, 0x27, 0xa8, 0x3e, 0x4b,
  185. 0x6b, 0x2a, 0x68, 0xd5, 0x46, 0x69, 0xc7, 0x15, 0x3e, 0xf8, 0xd6, 0x9a,
  186. 0x5f, 0x19, 0x47, 0x46, 0x06, 0xef, 0xc6, 0x16, 0x31, 0x62, 0x96, 0xef,
  187. 0x87, 0x8a, 0xb7, 0xf1, 0x06, 0x7f, 0x2f, 0x89, 0x38, 0x2d, 0xf3, 0xb1,
  188. 0xb5, 0xe3, 0x4f, 0x12, 0x91, 0x3f, 0x4c, 0x11, 0xa7, 0xb1, 0x49, 0xbd,
  189. 0x94, 0x14, 0x86, 0xff, 0xc3, 0x25, 0x44, 0x1d, 0x2f, 0x9e, 0x86, 0xb3,
  190. 0x28, 0x91, 0xc5, 0x11, 0x02, 0x81, 0x80, 0x57, 0x5c, 0xef, 0x54, 0xcc,
  191. 0xd4, 0x8d, 0x96, 0x9e, 0x41, 0xb6, 0x67, 0x64, 0xae, 0x62, 0x82, 0x4d,
  192. 0xc3, 0x8e, 0x0e, 0x52, 0x7a, 0x08, 0x70, 0x92, 0xd9, 0x71, 0x6f, 0x46,
  193. 0x65, 0x40, 0x4a, 0x62, 0x21, 0xe6, 0xbf, 0xd6, 0xf7, 0x62, 0x4d, 0x4e,
  194. 0x1f, 0x1e, 0xd2, 0x72, 0x1b, 0xf0, 0xba, 0x9c, 0xb5, 0xe8, 0x9a, 0xec,
  195. 0xec, 0xe5, 0xf2, 0x54, 0xb3, 0xe7, 0xc0, 0x0e, 0x8f, 0x27, 0x04, 0x76,
  196. 0xa2, 0x9e, 0xb5, 0xe3, 0x7f, 0x49, 0xfa, 0x81, 0x4c, 0x1d, 0x66, 0x67,
  197. 0x01, 0xe3, 0x4c, 0x7d, 0xdc, 0x03, 0xc4, 0x7a, 0x28, 0x11, 0x1c, 0x29,
  198. 0x5c, 0x47, 0x45, 0xc8, 0xd5, 0x90, 0x9c, 0x00, 0xae, 0x66, 0xa7, 0x03,
  199. 0x67, 0x2b, 0x9c, 0x18, 0xbe, 0x80, 0xf0, 0x67, 0x11, 0x79, 0x5f, 0x9f,
  200. 0xf8, 0x3f, 0x38, 0xc0, 0x7b, 0x20, 0xcc, 0x1b, 0x73, 0x43, 0x0d, 0x1e,
  201. 0x25, 0x14, 0xa7, 0x02, 0x81, 0x80, 0x09, 0xe0, 0xa5, 0xb9, 0x56, 0x96,
  202. 0x64, 0xf7, 0xd3, 0xc5, 0xbc, 0x19, 0x2b, 0x20, 0xcb, 0x08, 0x9e, 0x0b,
  203. 0x3e, 0x5e, 0xcc, 0x8e, 0xf2, 0x72, 0x83, 0x9b, 0x9f, 0x52, 0xf4, 0x0a,
  204. 0xda, 0xe0, 0x0b, 0x91, 0x14, 0x51, 0x8e, 0x19, 0x1b, 0x77, 0x7d, 0x2a,
  205. 0xec, 0x9a, 0xdc, 0xd1, 0x83, 0xff, 0x25, 0x75, 0xb6, 0xb7, 0xe4, 0x51,
  206. 0xb0, 0xa1, 0x22, 0x7b, 0x1f, 0xb6, 0xcd, 0x7d, 0xe0, 0x55, 0x2f, 0x3d,
  207. 0xe4, 0x1e, 0xe9, 0x4e, 0x77, 0x2e, 0xe5, 0x8c, 0xcb, 0x82, 0x5e, 0xee,
  208. 0xe7, 0x4e, 0x08, 0x09, 0x67, 0xb1, 0xcc, 0x67, 0x66, 0x68, 0xd1, 0x2c,
  209. 0x65, 0x08, 0xc7, 0x8a, 0x23, 0xc4, 0x5b, 0x7c, 0x9c, 0x54, 0x44, 0x2e,
  210. 0xe1, 0xad, 0x8f, 0x99, 0x2a, 0xd8, 0x8f, 0x31, 0x05, 0x7c, 0x9f, 0xbc,
  211. 0x79, 0x67, 0x40, 0x5e, 0xda, 0x2c, 0x38, 0x7d, 0x3f, 0x0b, 0x6c, 0x83,
  212. 0xcc, 0x75};
  213. /* This is an example for a custom SSL initialization function.
  214. * It will setup the certificate and private key defined in the arrays above.
  215. * Thus, they do not need to be located in files in the file system.
  216. * To slightly improve security, you could use some encryption for the arrays
  217. * above, and only decrypt it in memory before passing to the SSL_CTX_use_*
  218. * functions. Then the data will not exist in the executable stored at the
  219. * disk, but only in memory at runtime - making reverse engineering more
  220. * complex. Here we use unencrypted versions, so you could test the conversion
  221. * process from server.key/server.crt to these arrays on your own, and see if
  222. * you come to the same result.
  223. */
  224. static int
  225. init_ssl(void *ssl_ctx, void *user_data)
  226. {
  227. SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
  228. SSL_CTX_use_certificate_ASN1(ctx, sizeof(SSL_CERT_ASN1), SSL_CERT_ASN1);
  229. SSL_CTX_use_PrivateKey_ASN1(EVP_PKEY_RSA,
  230. ctx,
  231. SSL_KEY_ASN1,
  232. sizeof(SSL_KEY_ASN1));
  233. if (SSL_CTX_check_private_key(ctx) == 0) {
  234. printf("SSL data inconsistency detected\n");
  235. return -1;
  236. }
  237. return 0; /* let CivetWeb set up the rest of OpenSSL */
  238. }
  239. /* This handler function will serve all requests to the server.
  240. * In a realistic example, you would have multiple handler functions for
  241. * different URLs.
  242. */
  243. static int
  244. request_handler(struct mg_connection *conn, void *cbdata)
  245. {
  246. /* Get the URI from the request info. */
  247. const struct mg_request_info *ri = mg_get_request_info(conn);
  248. unsigned uri_len = (unsigned)strlen(ri->local_uri);
  249. /* Example: get a cookie named "c" from the client. */
  250. const char *cookie = mg_get_header(conn, "Cookie");
  251. unsigned long cookie_number = 0;
  252. if (cookie) {
  253. char dst[32] = {0};
  254. if (mg_get_cookie(cookie, "c", dst, sizeof(dst)) > 0) {
  255. long val = atol(dst);
  256. if (val > 0) {
  257. cookie_number = val;
  258. }
  259. }
  260. }
  261. /* Calculate a value for a new cookie. Here just increment the previous
  262. * value. Thus, we count the number of times a client visited this a
  263. * page generated by this handler function. */
  264. char new_cookie[32];
  265. sprintf(new_cookie, "c=%lu", cookie_number + 1);
  266. /* Generate a response text and status code. */
  267. int status;
  268. char response[1024];
  269. if (uri_len <= 100) {
  270. status = 200; /* 200 = OK */
  271. sprintf(response,
  272. "Hello at %s\nYou have visited %lu sites here before!",
  273. ri->local_uri,
  274. cookie_number);
  275. } else {
  276. /* We don't like this URL */
  277. status = 404; /* 404 = Not Found */
  278. sprintf(response, "No such URL\n");
  279. }
  280. /* This sequence can be used to send a response including a cookie. */
  281. unsigned long content_len = (unsigned long)strlen(response);
  282. char content_len_text[32];
  283. sprintf(content_len_text, "%lu", content_len);
  284. mg_response_header_start(conn, status);
  285. mg_response_header_add(conn, "Set-Cookie", new_cookie, -1);
  286. mg_response_header_add(conn,
  287. "Content-Type",
  288. "text/plain; charset=utf-8",
  289. -1);
  290. mg_response_header_add(conn, "Content-Length", content_len_text, -1);
  291. mg_response_header_send(conn);
  292. mg_write(conn, response, content_len);
  293. return status;
  294. }
  295. /* Main: Initialize and run the server */
  296. int
  297. main(int argc, char *argv[])
  298. {
  299. int ret = mg_init_library(MG_FEATURES_TLS);
  300. if (ret != MG_FEATURES_TLS) {
  301. printf("Initializing SSL libraries failed\n");
  302. return 1;
  303. }
  304. printf("Starting example server at https://localhost:443/\n");
  305. struct mg_init_data init = {0};
  306. struct mg_callbacks callbacks = {0};
  307. callbacks.init_ssl = init_ssl;
  308. init.callbacks = &callbacks;
  309. init.user_data = NULL;
  310. const char *options[] = {"listening_ports",
  311. "80r,443s",
  312. "authentication_domain",
  313. "localhost",
  314. "enable_auth_domain_check",
  315. "no",
  316. "ssl_protocol_version",
  317. "4",
  318. "ssl_cipher_list",
  319. "ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS",
  320. "strict_transport_security_max_age",
  321. "15552000",
  322. NULL,
  323. NULL};
  324. init.configuration_options = options;
  325. struct mg_error_data error = {0};
  326. unsigned error_code = 0;
  327. error.code = &error_code;
  328. char error_text[256] = {0};
  329. error.text = error_text;
  330. error.text_buffer_size = sizeof(error_text);
  331. struct mg_context *ctx = mg_start2(&init, &error);
  332. if (ctx) {
  333. mg_set_request_handler(ctx, "/", request_handler, NULL);
  334. printf("Server running for 60 seconds\n");
  335. sleep(60);
  336. printf("Server exit\n");
  337. mg_stop(ctx);
  338. } else {
  339. printf("Initialization failed: %u\n%s\n\n", error_code, error_text);
  340. }
  341. mg_exit_library();
  342. return 0;
  343. }