PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/programs/ssl/ssl_client1.c

https://github.com/leg0/polarssl
C | 293 lines | 188 code | 55 blank | 50 comment | 44 complexity | f4df3f1d36f38b64932cda17df696bce MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * SSL client demonstration program
  3. *
  4. * Copyright (C) 2006-2011, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "polarssl/config.h"
  31. #include "polarssl/net.h"
  32. #include "polarssl/ssl.h"
  33. #include "polarssl/entropy.h"
  34. #include "polarssl/ctr_drbg.h"
  35. #include "polarssl/error.h"
  36. #include "polarssl/certs.h"
  37. #define SERVER_PORT 4433
  38. #define SERVER_NAME "localhost"
  39. #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
  40. #define DEBUG_LEVEL 1
  41. void my_debug( void *ctx, int level, const char *str )
  42. {
  43. if( level < DEBUG_LEVEL )
  44. {
  45. fprintf( (FILE *) ctx, "%s", str );
  46. fflush( (FILE *) ctx );
  47. }
  48. }
  49. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
  50. !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
  51. !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
  52. !defined(POLARSSL_CTR_DRBG_C)
  53. int main( int argc, char *argv[] )
  54. {
  55. ((void) argc);
  56. ((void) argv);
  57. printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
  58. "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
  59. "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
  60. "POLARSSL_CTR_DRBG_C not defined.\n");
  61. return( 0 );
  62. }
  63. #else
  64. int main( int argc, char *argv[] )
  65. {
  66. int ret, len, server_fd;
  67. unsigned char buf[1024];
  68. char *pers = "ssl_client1";
  69. entropy_context entropy;
  70. ctr_drbg_context ctr_drbg;
  71. ssl_context ssl;
  72. x509_cert cacert;
  73. ((void) argc);
  74. ((void) argv);
  75. /*
  76. * 0. Initialize the RNG and the session data
  77. */
  78. memset( &ssl, 0, sizeof( ssl_context ) );
  79. memset( &cacert, 0, sizeof( x509_cert ) );
  80. printf( "\n . Seeding the random number generator..." );
  81. fflush( stdout );
  82. entropy_init( &entropy );
  83. if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
  84. (unsigned char *) pers, strlen( pers ) ) ) != 0 )
  85. {
  86. printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
  87. goto exit;
  88. }
  89. printf( " ok\n" );
  90. /*
  91. * 0. Initialize certificates
  92. */
  93. printf( " . Loading the CA root certificate ..." );
  94. fflush( stdout );
  95. #if defined(POLARSSL_CERTS_C)
  96. ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
  97. strlen( test_ca_crt ) );
  98. #else
  99. ret = 1;
  100. printf("POLARSSL_CERTS_C not defined.");
  101. #endif
  102. if( ret < 0 )
  103. {
  104. printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
  105. goto exit;
  106. }
  107. printf( " ok (%d skipped)\n", ret );
  108. /*
  109. * 1. Start the connection
  110. */
  111. printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
  112. SERVER_PORT );
  113. fflush( stdout );
  114. if( ( ret = net_connect( &server_fd, SERVER_NAME,
  115. SERVER_PORT ) ) != 0 )
  116. {
  117. printf( " failed\n ! net_connect returned %d\n\n", ret );
  118. goto exit;
  119. }
  120. printf( " ok\n" );
  121. /*
  122. * 2. Setup stuff
  123. */
  124. printf( " . Setting up the SSL/TLS structure..." );
  125. fflush( stdout );
  126. if( ( ret = ssl_init( &ssl ) ) != 0 )
  127. {
  128. printf( " failed\n ! ssl_init returned %d\n\n", ret );
  129. goto exit;
  130. }
  131. printf( " ok\n" );
  132. ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
  133. ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
  134. ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
  135. ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
  136. ssl_set_dbg( &ssl, my_debug, stdout );
  137. ssl_set_bio( &ssl, net_recv, &server_fd,
  138. net_send, &server_fd );
  139. /*
  140. * 4. Handshake
  141. */
  142. printf( " . Performing the SSL/TLS handshake..." );
  143. fflush( stdout );
  144. while( ( ret = ssl_handshake( &ssl ) ) != 0 )
  145. {
  146. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  147. {
  148. printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
  149. goto exit;
  150. }
  151. }
  152. printf( " ok\n" );
  153. /*
  154. * 5. Verify the server certificate
  155. */
  156. printf( " . Verifying peer X.509 certificate..." );
  157. if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
  158. {
  159. printf( " failed\n" );
  160. if( ( ret & BADCERT_EXPIRED ) != 0 )
  161. printf( " ! server certificate has expired\n" );
  162. if( ( ret & BADCERT_REVOKED ) != 0 )
  163. printf( " ! server certificate has been revoked\n" );
  164. if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
  165. printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
  166. if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
  167. printf( " ! self-signed or not signed by a trusted CA\n" );
  168. printf( "\n" );
  169. }
  170. else
  171. printf( " ok\n" );
  172. /*
  173. * 3. Write the GET request
  174. */
  175. printf( " > Write to server:" );
  176. fflush( stdout );
  177. len = sprintf( (char *) buf, GET_REQUEST );
  178. while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
  179. {
  180. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  181. {
  182. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  183. goto exit;
  184. }
  185. }
  186. len = ret;
  187. printf( " %d bytes written\n\n%s", len, (char *) buf );
  188. /*
  189. * 7. Read the HTTP response
  190. */
  191. printf( " < Read from server:" );
  192. fflush( stdout );
  193. do
  194. {
  195. len = sizeof( buf ) - 1;
  196. memset( buf, 0, sizeof( buf ) );
  197. ret = ssl_read( &ssl, buf, len );
  198. if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
  199. continue;
  200. if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
  201. break;
  202. if( ret < 0 )
  203. {
  204. printf( "failed\n ! ssl_read returned %d\n\n", ret );
  205. break;
  206. }
  207. if( ret == 0 )
  208. {
  209. printf( "\n\nEOF\n\n" );
  210. break;
  211. }
  212. len = ret;
  213. printf( " %d bytes read\n\n%s", len, (char *) buf );
  214. }
  215. while( 1 );
  216. ssl_close_notify( &ssl );
  217. exit:
  218. #ifdef POLARSSL_ERROR_C
  219. if( ret != 0 )
  220. {
  221. char error_buf[100];
  222. error_strerror( ret, error_buf, 100 );
  223. printf("Last error was: %d - %s\n\n", ret, error_buf );
  224. }
  225. #endif
  226. x509_free( &cacert );
  227. net_close( server_fd );
  228. ssl_free( &ssl );
  229. memset( &ssl, 0, sizeof( ssl ) );
  230. #if defined(_WIN32)
  231. printf( " + Press Enter to exit this program.\n" );
  232. fflush( stdout ); getchar();
  233. #endif
  234. return( ret );
  235. }
  236. #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
  237. POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
  238. POLARSSL_CTR_DRBG_C */