ssl_server.c 12 KB

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