dtls_client.c 10 KB

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