ssl_server.c 11 KB

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