ssl_client1.c 8.7 KB

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