ssl_server.c 11 KB

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