ssl_server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * SSL server demonstration program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
  22. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
  23. !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
  24. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  25. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO)
  26. int main(void)
  27. {
  28. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C "
  29. "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
  30. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  31. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
  32. "and/or MBEDTLS_PEM_PARSE_C not defined.\n");
  33. mbedtls_exit(0);
  34. }
  35. #else
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #if defined(_WIN32)
  39. #include <windows.h>
  40. #endif
  41. #include "mbedtls/entropy.h"
  42. #include "mbedtls/ctr_drbg.h"
  43. #include "mbedtls/x509.h"
  44. #include "mbedtls/ssl.h"
  45. #include "mbedtls/net_sockets.h"
  46. #include "mbedtls/error.h"
  47. #include "mbedtls/debug.h"
  48. #include "test/certs.h"
  49. #if defined(MBEDTLS_SSL_CACHE_C)
  50. #include "mbedtls/ssl_cache.h"
  51. #endif
  52. #define HTTP_RESPONSE \
  53. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  54. "<h2>mbed TLS Test Server</h2>\r\n" \
  55. "<p>Successful connection using: %s</p>\r\n"
  56. #define DEBUG_LEVEL 0
  57. static void my_debug(void *ctx, int level,
  58. const char *file, int line,
  59. const char *str)
  60. {
  61. ((void) level);
  62. mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
  63. fflush((FILE *) ctx);
  64. }
  65. int main(void)
  66. {
  67. int ret, len;
  68. mbedtls_net_context listen_fd, client_fd;
  69. unsigned char buf[1024];
  70. const char *pers = "ssl_server";
  71. mbedtls_entropy_context entropy;
  72. mbedtls_ctr_drbg_context ctr_drbg;
  73. mbedtls_ssl_context ssl;
  74. mbedtls_ssl_config conf;
  75. mbedtls_x509_crt srvcert;
  76. mbedtls_pk_context pkey;
  77. #if defined(MBEDTLS_SSL_CACHE_C)
  78. mbedtls_ssl_cache_context cache;
  79. #endif
  80. mbedtls_net_init(&listen_fd);
  81. mbedtls_net_init(&client_fd);
  82. mbedtls_ssl_init(&ssl);
  83. mbedtls_ssl_config_init(&conf);
  84. #if defined(MBEDTLS_SSL_CACHE_C)
  85. mbedtls_ssl_cache_init(&cache);
  86. #endif
  87. mbedtls_x509_crt_init(&srvcert);
  88. mbedtls_pk_init(&pkey);
  89. mbedtls_entropy_init(&entropy);
  90. mbedtls_ctr_drbg_init(&ctr_drbg);
  91. #if defined(MBEDTLS_DEBUG_C)
  92. mbedtls_debug_set_threshold(DEBUG_LEVEL);
  93. #endif
  94. /*
  95. * 1. Seed the RNG
  96. */
  97. mbedtls_printf(" . Seeding the random number generator...");
  98. fflush(stdout);
  99. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  100. (const unsigned char *) pers,
  101. strlen(pers))) != 0) {
  102. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  103. goto exit;
  104. }
  105. mbedtls_printf(" ok\n");
  106. /*
  107. * 2. Load the certificates and private RSA key
  108. */
  109. mbedtls_printf("\n . Loading the server cert. and key...");
  110. fflush(stdout);
  111. /*
  112. * This demonstration program uses embedded test certificates.
  113. * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
  114. * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
  115. */
  116. ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  117. mbedtls_test_srv_crt_len);
  118. if (ret != 0) {
  119. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
  120. goto exit;
  121. }
  122. ret = mbedtls_x509_crt_parse(&srvcert, (const unsigned char *) mbedtls_test_cas_pem,
  123. mbedtls_test_cas_pem_len);
  124. if (ret != 0) {
  125. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
  126. goto exit;
  127. }
  128. ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_srv_key,
  129. mbedtls_test_srv_key_len, NULL, 0,
  130. mbedtls_ctr_drbg_random, &ctr_drbg);
  131. if (ret != 0) {
  132. mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret);
  133. goto exit;
  134. }
  135. mbedtls_printf(" ok\n");
  136. /*
  137. * 3. Setup the listening TCP socket
  138. */
  139. mbedtls_printf(" . Bind on https://localhost:4433/ ...");
  140. fflush(stdout);
  141. if ((ret = mbedtls_net_bind(&listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP)) != 0) {
  142. mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
  143. goto exit;
  144. }
  145. mbedtls_printf(" ok\n");
  146. /*
  147. * 4. Setup stuff
  148. */
  149. mbedtls_printf(" . Setting up the SSL data....");
  150. fflush(stdout);
  151. if ((ret = mbedtls_ssl_config_defaults(&conf,
  152. MBEDTLS_SSL_IS_SERVER,
  153. MBEDTLS_SSL_TRANSPORT_STREAM,
  154. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  155. mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
  156. goto exit;
  157. }
  158. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  159. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  160. #if defined(MBEDTLS_SSL_CACHE_C)
  161. mbedtls_ssl_conf_session_cache(&conf, &cache,
  162. mbedtls_ssl_cache_get,
  163. mbedtls_ssl_cache_set);
  164. #endif
  165. mbedtls_ssl_conf_ca_chain(&conf, srvcert.next, NULL);
  166. if ((ret = mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey)) != 0) {
  167. mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
  168. goto exit;
  169. }
  170. if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
  171. mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
  172. goto exit;
  173. }
  174. mbedtls_printf(" ok\n");
  175. reset:
  176. #ifdef MBEDTLS_ERROR_C
  177. if (ret != 0) {
  178. char error_buf[100];
  179. mbedtls_strerror(ret, error_buf, 100);
  180. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
  181. }
  182. #endif
  183. mbedtls_net_free(&client_fd);
  184. mbedtls_ssl_session_reset(&ssl);
  185. /*
  186. * 3. Wait until a client connects
  187. */
  188. mbedtls_printf(" . Waiting for a remote connection ...");
  189. fflush(stdout);
  190. if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
  191. NULL, 0, NULL)) != 0) {
  192. mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
  193. goto exit;
  194. }
  195. mbedtls_ssl_set_bio(&ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  196. mbedtls_printf(" ok\n");
  197. /*
  198. * 5. Handshake
  199. */
  200. mbedtls_printf(" . Performing the SSL/TLS handshake...");
  201. fflush(stdout);
  202. while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
  203. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  204. mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret);
  205. goto reset;
  206. }
  207. }
  208. mbedtls_printf(" ok\n");
  209. /*
  210. * 6. Read the HTTP Request
  211. */
  212. mbedtls_printf(" < Read from client:");
  213. fflush(stdout);
  214. do {
  215. len = sizeof(buf) - 1;
  216. memset(buf, 0, sizeof(buf));
  217. ret = mbedtls_ssl_read(&ssl, buf, len);
  218. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  219. continue;
  220. }
  221. if (ret <= 0) {
  222. switch (ret) {
  223. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  224. mbedtls_printf(" connection was closed gracefully\n");
  225. break;
  226. case MBEDTLS_ERR_NET_CONN_RESET:
  227. mbedtls_printf(" connection was reset by peer\n");
  228. break;
  229. default:
  230. mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret);
  231. break;
  232. }
  233. break;
  234. }
  235. len = ret;
  236. mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf);
  237. if (ret > 0) {
  238. break;
  239. }
  240. } while (1);
  241. /*
  242. * 7. Write the 200 Response
  243. */
  244. mbedtls_printf(" > Write to client:");
  245. fflush(stdout);
  246. len = sprintf((char *) buf, HTTP_RESPONSE,
  247. mbedtls_ssl_get_ciphersuite(&ssl));
  248. while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
  249. if (ret == MBEDTLS_ERR_NET_CONN_RESET) {
  250. mbedtls_printf(" failed\n ! peer closed the connection\n\n");
  251. goto reset;
  252. }
  253. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  254. mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
  255. goto exit;
  256. }
  257. }
  258. len = ret;
  259. mbedtls_printf(" %d bytes written\n\n%s\n", len, (char *) buf);
  260. mbedtls_printf(" . Closing the connection...");
  261. while ((ret = mbedtls_ssl_close_notify(&ssl)) < 0) {
  262. if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
  263. ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  264. mbedtls_printf(" failed\n ! mbedtls_ssl_close_notify returned %d\n\n", ret);
  265. goto reset;
  266. }
  267. }
  268. mbedtls_printf(" ok\n");
  269. ret = 0;
  270. goto reset;
  271. exit:
  272. #ifdef MBEDTLS_ERROR_C
  273. if (ret != 0) {
  274. char error_buf[100];
  275. mbedtls_strerror(ret, error_buf, 100);
  276. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
  277. }
  278. #endif
  279. mbedtls_net_free(&client_fd);
  280. mbedtls_net_free(&listen_fd);
  281. mbedtls_x509_crt_free(&srvcert);
  282. mbedtls_pk_free(&pkey);
  283. mbedtls_ssl_free(&ssl);
  284. mbedtls_ssl_config_free(&conf);
  285. #if defined(MBEDTLS_SSL_CACHE_C)
  286. mbedtls_ssl_cache_free(&cache);
  287. #endif
  288. mbedtls_ctr_drbg_free(&ctr_drbg);
  289. mbedtls_entropy_free(&entropy);
  290. mbedtls_exit(ret);
  291. }
  292. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
  293. MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
  294. MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C
  295. && MBEDTLS_FS_IO && MBEDTLS_PEM_PARSE_C */