dtls_client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Simple DTLS client 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. #define mbedtls_printf printf
  31. #define mbedtls_fprintf fprintf
  32. #define mbedtls_exit exit
  33. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  34. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  35. #endif
  36. #if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
  37. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
  38. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  39. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
  40. !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
  41. int main( void )
  42. {
  43. mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
  44. "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
  45. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
  46. "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
  47. "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
  48. return( 0 );
  49. }
  50. #else
  51. #include <string.h>
  52. #include "mbedtls/net_sockets.h"
  53. #include "mbedtls/debug.h"
  54. #include "mbedtls/ssl.h"
  55. #include "mbedtls/entropy.h"
  56. #include "mbedtls/ctr_drbg.h"
  57. #include "mbedtls/error.h"
  58. #include "mbedtls/certs.h"
  59. #include "mbedtls/timing.h"
  60. /* Uncomment out the following line to default to IPv4 and disable IPv6 */
  61. //#define FORCE_IPV4
  62. #define SERVER_PORT "4433"
  63. #define SERVER_NAME "localhost"
  64. #ifdef FORCE_IPV4
  65. #define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
  66. #else
  67. #define SERVER_ADDR "::1"
  68. #endif
  69. #define MESSAGE "Echo this"
  70. #define READ_TIMEOUT_MS 1000
  71. #define MAX_RETRY 5
  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( int argc, char *argv[] )
  82. {
  83. int ret, len;
  84. mbedtls_net_context server_fd;
  85. uint32_t flags;
  86. unsigned char buf[1024];
  87. const char *pers = "dtls_client";
  88. int retry_left = MAX_RETRY;
  89. mbedtls_entropy_context entropy;
  90. mbedtls_ctr_drbg_context ctr_drbg;
  91. mbedtls_ssl_context ssl;
  92. mbedtls_ssl_config conf;
  93. mbedtls_x509_crt cacert;
  94. mbedtls_timing_delay_context timer;
  95. ((void) argc);
  96. ((void) argv);
  97. #if defined(MBEDTLS_DEBUG_C)
  98. mbedtls_debug_set_threshold( DEBUG_LEVEL );
  99. #endif
  100. /*
  101. * 0. Initialize the RNG and the session data
  102. */
  103. mbedtls_net_init( &server_fd );
  104. mbedtls_ssl_init( &ssl );
  105. mbedtls_ssl_config_init( &conf );
  106. mbedtls_x509_crt_init( &cacert );
  107. mbedtls_ctr_drbg_init( &ctr_drbg );
  108. mbedtls_printf( "\n . Seeding the random number generator..." );
  109. fflush( stdout );
  110. mbedtls_entropy_init( &entropy );
  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. * 0. Load certificates
  121. */
  122. mbedtls_printf( " . Loading the CA root certificate ..." );
  123. fflush( stdout );
  124. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
  125. mbedtls_test_cas_pem_len );
  126. if( ret < 0 )
  127. {
  128. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  129. goto exit;
  130. }
  131. mbedtls_printf( " ok (%d skipped)\n", ret );
  132. /*
  133. * 1. Start the connection
  134. */
  135. mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
  136. fflush( stdout );
  137. if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
  138. SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  139. {
  140. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
  141. goto exit;
  142. }
  143. mbedtls_printf( " ok\n" );
  144. /*
  145. * 2. Setup stuff
  146. */
  147. mbedtls_printf( " . Setting up the DTLS structure..." );
  148. fflush( stdout );
  149. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  150. MBEDTLS_SSL_IS_CLIENT,
  151. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  152. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  153. {
  154. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  155. goto exit;
  156. }
  157. /* OPTIONAL is usually a bad choice for security, but makes interop easier
  158. * in this simplified example, in which the ca chain is hardcoded.
  159. * Production code should set a proper ca chain and use REQUIRED. */
  160. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
  161. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  162. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  163. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  164. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  165. {
  166. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  167. goto exit;
  168. }
  169. if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
  170. {
  171. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  172. goto exit;
  173. }
  174. mbedtls_ssl_set_bio( &ssl, &server_fd,
  175. mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
  176. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  177. mbedtls_timing_get_delay );
  178. mbedtls_printf( " ok\n" );
  179. /*
  180. * 4. Handshake
  181. */
  182. mbedtls_printf( " . Performing the DTLS handshake..." );
  183. fflush( stdout );
  184. do ret = mbedtls_ssl_handshake( &ssl );
  185. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  186. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  187. if( ret != 0 )
  188. {
  189. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  190. goto exit;
  191. }
  192. mbedtls_printf( " ok\n" );
  193. /*
  194. * 5. Verify the server certificate
  195. */
  196. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  197. /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
  198. * handshake would not succeed if the peer's cert is bad. Even if we used
  199. * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
  200. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  201. {
  202. char vrfy_buf[512];
  203. mbedtls_printf( " failed\n" );
  204. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  205. mbedtls_printf( "%s\n", vrfy_buf );
  206. }
  207. else
  208. mbedtls_printf( " ok\n" );
  209. /*
  210. * 6. Write the echo request
  211. */
  212. send_request:
  213. mbedtls_printf( " > Write to server:" );
  214. fflush( stdout );
  215. len = sizeof( MESSAGE ) - 1;
  216. do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
  217. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  218. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  219. if( ret < 0 )
  220. {
  221. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  222. goto exit;
  223. }
  224. len = ret;
  225. mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
  226. /*
  227. * 7. Read the echo response
  228. */
  229. mbedtls_printf( " < Read from server:" );
  230. fflush( stdout );
  231. len = sizeof( buf ) - 1;
  232. memset( buf, 0, sizeof( buf ) );
  233. do ret = mbedtls_ssl_read( &ssl, buf, len );
  234. while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  235. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  236. if( ret <= 0 )
  237. {
  238. switch( ret )
  239. {
  240. case MBEDTLS_ERR_SSL_TIMEOUT:
  241. mbedtls_printf( " timeout\n\n" );
  242. if( retry_left-- > 0 )
  243. goto send_request;
  244. goto exit;
  245. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  246. mbedtls_printf( " connection was closed gracefully\n" );
  247. ret = 0;
  248. goto close_notify;
  249. default:
  250. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
  251. goto exit;
  252. }
  253. }
  254. len = ret;
  255. mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
  256. /*
  257. * 8. Done, cleanly close the connection
  258. */
  259. close_notify:
  260. mbedtls_printf( " . Closing the connection..." );
  261. /* No error checking, the connection might be closed already */
  262. do ret = mbedtls_ssl_close_notify( &ssl );
  263. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  264. ret = 0;
  265. mbedtls_printf( " done\n" );
  266. /*
  267. * 9. Final clean-ups and exit
  268. */
  269. exit:
  270. #ifdef MBEDTLS_ERROR_C
  271. if( ret != 0 )
  272. {
  273. char error_buf[100];
  274. mbedtls_strerror( ret, error_buf, 100 );
  275. mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
  276. }
  277. #endif
  278. mbedtls_net_free( &server_fd );
  279. mbedtls_x509_crt_free( &cacert );
  280. mbedtls_ssl_free( &ssl );
  281. mbedtls_ssl_config_free( &conf );
  282. mbedtls_ctr_drbg_free( &ctr_drbg );
  283. mbedtls_entropy_free( &entropy );
  284. #if defined(_WIN32)
  285. mbedtls_printf( " + Press Enter to exit this program.\n" );
  286. fflush( stdout ); getchar();
  287. #endif
  288. /* Shell can not handle large exit numbers -> 1 for errors */
  289. if( ret < 0 )
  290. ret = 1;
  291. return( ret );
  292. }
  293. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
  294. MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
  295. MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
  296. MBEDTLS_PEM_PARSE_C */