dtls_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Simple DTLS 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_printf printf
  57. #define mbedtls_fprintf fprintf
  58. #define mbedtls_time_t time_t
  59. #define mbedtls_exit exit
  60. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  61. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  62. #endif
  63. /* Uncomment out the following line to default to IPv4 and disable IPv6 */
  64. //#define FORCE_IPV4
  65. #ifdef FORCE_IPV4
  66. #define BIND_IP "0.0.0.0" /* Forces IPv4 */
  67. #else
  68. #define BIND_IP "::"
  69. #endif
  70. #if !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
  71. !defined(MBEDTLS_SSL_COOKIE_C) || !defined(MBEDTLS_NET_C) || \
  72. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  73. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
  74. !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
  75. !defined(MBEDTLS_TIMING_C)
  76. int main( void )
  77. {
  78. printf( "MBEDTLS_SSL_SRV_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
  79. "MBEDTLS_SSL_COOKIE_C and/or MBEDTLS_NET_C and/or "
  80. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
  81. "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
  82. "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C and/or "
  83. "MBEDTLS_TIMING_C not defined.\n" );
  84. mbedtls_exit( 0 );
  85. }
  86. #else
  87. #if defined(_WIN32)
  88. #include <windows.h>
  89. #endif
  90. #include <string.h>
  91. #include <stdlib.h>
  92. #include <stdio.h>
  93. #include "mbedtls/entropy.h"
  94. #include "mbedtls/ctr_drbg.h"
  95. #include "mbedtls/certs.h"
  96. #include "mbedtls/x509.h"
  97. #include "mbedtls/ssl.h"
  98. #include "mbedtls/ssl_cookie.h"
  99. #include "mbedtls/net_sockets.h"
  100. #include "mbedtls/error.h"
  101. #include "mbedtls/debug.h"
  102. #include "mbedtls/timing.h"
  103. #if defined(MBEDTLS_SSL_CACHE_C)
  104. #include "mbedtls/ssl_cache.h"
  105. #endif
  106. #define READ_TIMEOUT_MS 10000 /* 10 seconds */
  107. #define DEBUG_LEVEL 0
  108. static void my_debug( void *ctx, int level,
  109. const char *file, int line,
  110. const char *str )
  111. {
  112. ((void) level);
  113. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  114. fflush( (FILE *) ctx );
  115. }
  116. int main( void )
  117. {
  118. int ret, len;
  119. mbedtls_net_context listen_fd, client_fd;
  120. unsigned char buf[1024];
  121. const char *pers = "dtls_server";
  122. unsigned char client_ip[16] = { 0 };
  123. size_t cliip_len;
  124. mbedtls_ssl_cookie_ctx cookie_ctx;
  125. mbedtls_entropy_context entropy;
  126. mbedtls_ctr_drbg_context ctr_drbg;
  127. mbedtls_ssl_context ssl;
  128. mbedtls_ssl_config conf;
  129. mbedtls_x509_crt srvcert;
  130. mbedtls_pk_context pkey;
  131. mbedtls_timing_delay_context timer;
  132. #if defined(MBEDTLS_SSL_CACHE_C)
  133. mbedtls_ssl_cache_context cache;
  134. #endif
  135. mbedtls_net_init( &listen_fd );
  136. mbedtls_net_init( &client_fd );
  137. mbedtls_ssl_init( &ssl );
  138. mbedtls_ssl_config_init( &conf );
  139. mbedtls_ssl_cookie_init( &cookie_ctx );
  140. #if defined(MBEDTLS_SSL_CACHE_C)
  141. mbedtls_ssl_cache_init( &cache );
  142. #endif
  143. mbedtls_x509_crt_init( &srvcert );
  144. mbedtls_pk_init( &pkey );
  145. mbedtls_entropy_init( &entropy );
  146. mbedtls_ctr_drbg_init( &ctr_drbg );
  147. #if defined(MBEDTLS_DEBUG_C)
  148. mbedtls_debug_set_threshold( DEBUG_LEVEL );
  149. #endif
  150. /*
  151. * 1. Load the certificates and private RSA key
  152. */
  153. printf( "\n . Loading the server cert. and key..." );
  154. fflush( stdout );
  155. /*
  156. * This demonstration program uses embedded test certificates.
  157. * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
  158. * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
  159. */
  160. ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
  161. mbedtls_test_srv_crt_len );
  162. if( ret != 0 )
  163. {
  164. printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
  165. goto exit;
  166. }
  167. ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
  168. mbedtls_test_cas_pem_len );
  169. if( ret != 0 )
  170. {
  171. printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
  172. goto exit;
  173. }
  174. ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
  175. mbedtls_test_srv_key_len, NULL, 0 );
  176. if( ret != 0 )
  177. {
  178. printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
  179. goto exit;
  180. }
  181. printf( " ok\n" );
  182. /*
  183. * 2. Setup the "listening" UDP socket
  184. */
  185. printf( " . Bind on udp/*/4433 ..." );
  186. fflush( stdout );
  187. if( ( ret = mbedtls_net_bind( &listen_fd, BIND_IP, "4433", MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  188. {
  189. printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
  190. goto exit;
  191. }
  192. printf( " ok\n" );
  193. /*
  194. * 3. Seed the RNG
  195. */
  196. printf( " . Seeding the random number generator..." );
  197. fflush( stdout );
  198. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  199. (const unsigned char *) pers,
  200. strlen( pers ) ) ) != 0 )
  201. {
  202. printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  203. goto exit;
  204. }
  205. printf( " ok\n" );
  206. /*
  207. * 4. Setup stuff
  208. */
  209. printf( " . Setting up the DTLS data..." );
  210. fflush( stdout );
  211. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  212. MBEDTLS_SSL_IS_SERVER,
  213. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  214. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  215. {
  216. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  217. goto exit;
  218. }
  219. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  220. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  221. mbedtls_ssl_conf_read_timeout( &conf, READ_TIMEOUT_MS );
  222. #if defined(MBEDTLS_SSL_CACHE_C)
  223. mbedtls_ssl_conf_session_cache( &conf, &cache,
  224. mbedtls_ssl_cache_get,
  225. mbedtls_ssl_cache_set );
  226. #endif
  227. mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
  228. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
  229. {
  230. printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  231. goto exit;
  232. }
  233. if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
  234. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  235. {
  236. printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
  237. goto exit;
  238. }
  239. mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
  240. &cookie_ctx );
  241. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  242. {
  243. printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  244. goto exit;
  245. }
  246. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  247. mbedtls_timing_get_delay );
  248. printf( " ok\n" );
  249. reset:
  250. #ifdef MBEDTLS_ERROR_C
  251. if( ret != 0 )
  252. {
  253. char error_buf[100];
  254. mbedtls_strerror( ret, error_buf, 100 );
  255. printf("Last error was: %d - %s\n\n", ret, error_buf );
  256. }
  257. #endif
  258. mbedtls_net_free( &client_fd );
  259. mbedtls_ssl_session_reset( &ssl );
  260. /*
  261. * 3. Wait until a client connects
  262. */
  263. printf( " . Waiting for a remote connection ..." );
  264. fflush( stdout );
  265. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  266. client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
  267. {
  268. printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
  269. goto exit;
  270. }
  271. /* For HelloVerifyRequest cookies */
  272. if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
  273. client_ip, cliip_len ) ) != 0 )
  274. {
  275. printf( " failed\n ! "
  276. "mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", -ret );
  277. goto exit;
  278. }
  279. mbedtls_ssl_set_bio( &ssl, &client_fd,
  280. mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
  281. printf( " ok\n" );
  282. /*
  283. * 5. Handshake
  284. */
  285. printf( " . Performing the DTLS handshake..." );
  286. fflush( stdout );
  287. do ret = mbedtls_ssl_handshake( &ssl );
  288. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  289. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  290. if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
  291. {
  292. printf( " hello verification requested\n" );
  293. ret = 0;
  294. goto reset;
  295. }
  296. else if( ret != 0 )
  297. {
  298. printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  299. goto reset;
  300. }
  301. printf( " ok\n" );
  302. /*
  303. * 6. Read the echo Request
  304. */
  305. printf( " < Read from client:" );
  306. fflush( stdout );
  307. len = sizeof( buf ) - 1;
  308. memset( buf, 0, sizeof( buf ) );
  309. do ret = mbedtls_ssl_read( &ssl, buf, len );
  310. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  311. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  312. if( ret <= 0 )
  313. {
  314. switch( ret )
  315. {
  316. case MBEDTLS_ERR_SSL_TIMEOUT:
  317. printf( " timeout\n\n" );
  318. goto reset;
  319. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  320. printf( " connection was closed gracefully\n" );
  321. ret = 0;
  322. goto close_notify;
  323. default:
  324. printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
  325. goto reset;
  326. }
  327. }
  328. len = ret;
  329. printf( " %d bytes read\n\n%s\n\n", len, buf );
  330. /*
  331. * 7. Write the 200 Response
  332. */
  333. printf( " > Write to client:" );
  334. fflush( stdout );
  335. do ret = mbedtls_ssl_write( &ssl, buf, len );
  336. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  337. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  338. if( ret < 0 )
  339. {
  340. printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  341. goto exit;
  342. }
  343. len = ret;
  344. printf( " %d bytes written\n\n%s\n\n", len, buf );
  345. /*
  346. * 8. Done, cleanly close the connection
  347. */
  348. close_notify:
  349. printf( " . Closing the connection..." );
  350. /* No error checking, the connection might be closed already */
  351. do ret = mbedtls_ssl_close_notify( &ssl );
  352. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  353. ret = 0;
  354. printf( " done\n" );
  355. goto reset;
  356. /*
  357. * Final clean-ups and exit
  358. */
  359. exit:
  360. #ifdef MBEDTLS_ERROR_C
  361. if( ret != 0 )
  362. {
  363. char error_buf[100];
  364. mbedtls_strerror( ret, error_buf, 100 );
  365. printf( "Last error was: %d - %s\n\n", ret, error_buf );
  366. }
  367. #endif
  368. mbedtls_net_free( &client_fd );
  369. mbedtls_net_free( &listen_fd );
  370. mbedtls_x509_crt_free( &srvcert );
  371. mbedtls_pk_free( &pkey );
  372. mbedtls_ssl_free( &ssl );
  373. mbedtls_ssl_config_free( &conf );
  374. mbedtls_ssl_cookie_free( &cookie_ctx );
  375. #if defined(MBEDTLS_SSL_CACHE_C)
  376. mbedtls_ssl_cache_free( &cache );
  377. #endif
  378. mbedtls_ctr_drbg_free( &ctr_drbg );
  379. mbedtls_entropy_free( &entropy );
  380. #if defined(_WIN32)
  381. printf( " Press Enter to exit this program.\n" );
  382. fflush( stdout ); getchar();
  383. #endif
  384. /* Shell can not handle large exit numbers -> 1 for errors */
  385. if( ret < 0 )
  386. ret = 1;
  387. mbedtls_exit( ret );
  388. }
  389. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
  390. MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
  391. MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
  392. && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_TIMING_C */