ssl_client1.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * SSL 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. #include <stdlib.h>
  31. #define mbedtls_time time
  32. #define mbedtls_time_t time_t
  33. #define mbedtls_fprintf fprintf
  34. #define mbedtls_printf printf
  35. #define mbedtls_exit exit
  36. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  37. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  38. #endif /* MBEDTLS_PLATFORM_C */
  39. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  40. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  41. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  42. !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
  43. !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
  44. int main( void )
  45. {
  46. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  47. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  48. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  49. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
  50. "not defined.\n");
  51. return( 0 );
  52. }
  53. #else
  54. #include "mbedtls/net_sockets.h"
  55. #include "mbedtls/debug.h"
  56. #include "mbedtls/ssl.h"
  57. #include "mbedtls/entropy.h"
  58. #include "mbedtls/ctr_drbg.h"
  59. #include "mbedtls/error.h"
  60. #include "mbedtls/certs.h"
  61. #include <string.h>
  62. #define SERVER_PORT "4433"
  63. #define SERVER_NAME "localhost"
  64. #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
  65. #define DEBUG_LEVEL 1
  66. static void my_debug( void *ctx, int level,
  67. const char *file, int line,
  68. const char *str )
  69. {
  70. ((void) level);
  71. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  72. fflush( (FILE *) ctx );
  73. }
  74. int main( void )
  75. {
  76. int ret = 1, len;
  77. int exit_code = MBEDTLS_EXIT_FAILURE;
  78. mbedtls_net_context server_fd;
  79. uint32_t flags;
  80. unsigned char buf[1024];
  81. const char *pers = "ssl_client1";
  82. mbedtls_entropy_context entropy;
  83. mbedtls_ctr_drbg_context ctr_drbg;
  84. mbedtls_ssl_context ssl;
  85. mbedtls_ssl_config conf;
  86. mbedtls_x509_crt cacert;
  87. #if defined(MBEDTLS_DEBUG_C)
  88. mbedtls_debug_set_threshold( DEBUG_LEVEL );
  89. #endif
  90. /*
  91. * 0. Initialize the RNG and the session data
  92. */
  93. mbedtls_net_init( &server_fd );
  94. mbedtls_ssl_init( &ssl );
  95. mbedtls_ssl_config_init( &conf );
  96. mbedtls_x509_crt_init( &cacert );
  97. mbedtls_ctr_drbg_init( &ctr_drbg );
  98. mbedtls_printf( "\n . Seeding the random number generator..." );
  99. fflush( stdout );
  100. mbedtls_entropy_init( &entropy );
  101. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  102. (const unsigned char *) pers,
  103. strlen( pers ) ) ) != 0 )
  104. {
  105. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  106. goto exit;
  107. }
  108. mbedtls_printf( " ok\n" );
  109. /*
  110. * 0. Initialize certificates
  111. */
  112. mbedtls_printf( " . Loading the CA root certificate ..." );
  113. fflush( stdout );
  114. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
  115. mbedtls_test_cas_pem_len );
  116. if( ret < 0 )
  117. {
  118. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  119. goto exit;
  120. }
  121. mbedtls_printf( " ok (%d skipped)\n", ret );
  122. /*
  123. * 1. Start the connection
  124. */
  125. mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
  126. fflush( stdout );
  127. if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
  128. SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  129. {
  130. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
  131. goto exit;
  132. }
  133. mbedtls_printf( " ok\n" );
  134. /*
  135. * 2. Setup stuff
  136. */
  137. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  138. fflush( stdout );
  139. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  140. MBEDTLS_SSL_IS_CLIENT,
  141. MBEDTLS_SSL_TRANSPORT_STREAM,
  142. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  143. {
  144. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  145. goto exit;
  146. }
  147. mbedtls_printf( " ok\n" );
  148. /* OPTIONAL is not optimal for security,
  149. * but makes interop easier in this simplified example */
  150. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
  151. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  152. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  153. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  154. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  155. {
  156. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  157. goto exit;
  158. }
  159. if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
  160. {
  161. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  162. goto exit;
  163. }
  164. mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  165. /*
  166. * 4. Handshake
  167. */
  168. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  169. fflush( stdout );
  170. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  171. {
  172. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  173. {
  174. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  175. goto exit;
  176. }
  177. }
  178. mbedtls_printf( " ok\n" );
  179. /*
  180. * 5. Verify the server certificate
  181. */
  182. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  183. /* In real life, we probably want to bail out when ret != 0 */
  184. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  185. {
  186. char vrfy_buf[512];
  187. mbedtls_printf( " failed\n" );
  188. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  189. mbedtls_printf( "%s\n", vrfy_buf );
  190. }
  191. else
  192. mbedtls_printf( " ok\n" );
  193. /*
  194. * 3. Write the GET request
  195. */
  196. mbedtls_printf( " > Write to server:" );
  197. fflush( stdout );
  198. len = sprintf( (char *) buf, GET_REQUEST );
  199. while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
  200. {
  201. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  202. {
  203. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  204. goto exit;
  205. }
  206. }
  207. len = ret;
  208. mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
  209. /*
  210. * 7. Read the HTTP response
  211. */
  212. mbedtls_printf( " < Read from server:" );
  213. fflush( stdout );
  214. do
  215. {
  216. len = sizeof( buf ) - 1;
  217. memset( buf, 0, sizeof( buf ) );
  218. ret = mbedtls_ssl_read( &ssl, buf, len );
  219. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  220. continue;
  221. if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
  222. break;
  223. if( ret < 0 )
  224. {
  225. mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
  226. break;
  227. }
  228. if( ret == 0 )
  229. {
  230. mbedtls_printf( "\n\nEOF\n\n" );
  231. break;
  232. }
  233. len = ret;
  234. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  235. }
  236. while( 1 );
  237. mbedtls_ssl_close_notify( &ssl );
  238. exit_code = MBEDTLS_EXIT_SUCCESS;
  239. exit:
  240. #ifdef MBEDTLS_ERROR_C
  241. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  242. {
  243. char error_buf[100];
  244. mbedtls_strerror( ret, error_buf, 100 );
  245. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  246. }
  247. #endif
  248. mbedtls_net_free( &server_fd );
  249. mbedtls_x509_crt_free( &cacert );
  250. mbedtls_ssl_free( &ssl );
  251. mbedtls_ssl_config_free( &conf );
  252. mbedtls_ctr_drbg_free( &ctr_drbg );
  253. mbedtls_entropy_free( &entropy );
  254. #if defined(_WIN32)
  255. mbedtls_printf( " + Press Enter to exit this program.\n" );
  256. fflush( stdout ); getchar();
  257. #endif
  258. return( exit_code );
  259. }
  260. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  261. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  262. MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
  263. MBEDTLS_X509_CRT_PARSE_C */