dtls_client.c 11 KB

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