ssl_server.c 11 KB

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