ssl_client1.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * SSL client demonstration program
  3. *
  4. * Copyright (C) 2006-2010, 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. #ifdef PRINTF_STDLIB
  30. #include <stdio.h>
  31. #endif
  32. #ifdef PRINTF_CUSTOM
  33. #include "tinystdio.h"
  34. #endif
  35. #include "polarssl/config.h"
  36. #include "polarssl/net.h"
  37. #include "polarssl/ssl.h"
  38. #include "polarssl/havege.h"
  39. #define SERVER_PORT 4433
  40. #define SERVER_NAME "localhost"
  41. #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
  42. #define DEBUG_LEVEL 1
  43. void my_debug( void *ctx, int level, const char *str )
  44. {
  45. if( level < DEBUG_LEVEL )
  46. {
  47. fprintf( (FILE *) ctx, "%s", str );
  48. fflush( (FILE *) ctx );
  49. }
  50. }
  51. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  52. !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
  53. !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C)
  54. int main( void )
  55. {
  56. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  57. "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
  58. "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
  59. return( 0 );
  60. }
  61. #else
  62. int main( void )
  63. {
  64. int ret, len, server_fd;
  65. unsigned char buf[1024];
  66. havege_state hs;
  67. ssl_context ssl;
  68. ssl_session ssn;
  69. /*
  70. * 0. Initialize the RNG and the session data
  71. */
  72. havege_init( &hs );
  73. memset( &ssn, 0, sizeof( ssl_session ) );
  74. memset( &ssl, 0, sizeof( ssl_context ) );
  75. /*
  76. * 1. Start the connection
  77. */
  78. printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME,
  79. SERVER_PORT );
  80. fflush( stdout );
  81. if( ( ret = net_connect( &server_fd, SERVER_NAME,
  82. SERVER_PORT ) ) != 0 )
  83. {
  84. printf( " failed\n ! net_connect returned %d\n\n", ret );
  85. goto exit;
  86. }
  87. printf( " ok\n" );
  88. /*
  89. * 2. Setup stuff
  90. */
  91. printf( " . Setting up the SSL/TLS structure..." );
  92. fflush( stdout );
  93. if( ( ret = ssl_init( &ssl ) ) != 0 )
  94. {
  95. printf( " failed\n ! ssl_init returned %d\n\n", ret );
  96. goto exit;
  97. }
  98. printf( " ok\n" );
  99. ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
  100. ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
  101. ssl_set_rng( &ssl, havege_rand, &hs );
  102. ssl_set_dbg( &ssl, my_debug, stdout );
  103. ssl_set_bio( &ssl, net_recv, &server_fd,
  104. net_send, &server_fd );
  105. ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
  106. ssl_set_session( &ssl, 1, 600, &ssn );
  107. /*
  108. * 3. Write the GET request
  109. */
  110. printf( " > Write to server:" );
  111. fflush( stdout );
  112. len = sprintf( (char *) buf, GET_REQUEST );
  113. while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
  114. {
  115. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  116. {
  117. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  118. goto exit;
  119. }
  120. }
  121. len = ret;
  122. printf( " %d bytes written\n\n%s", len, (char *) buf );
  123. /*
  124. * 7. Read the HTTP response
  125. */
  126. printf( " < Read from server:" );
  127. fflush( stdout );
  128. do
  129. {
  130. len = sizeof( buf ) - 1;
  131. memset( buf, 0, sizeof( buf ) );
  132. ret = ssl_read( &ssl, buf, len );
  133. if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
  134. continue;
  135. if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
  136. break;
  137. if( ret <= 0 )
  138. {
  139. printf( "failed\n ! ssl_read returned %d\n\n", ret );
  140. break;
  141. }
  142. len = ret;
  143. printf( " %d bytes read\n\n%s", len, (char *) buf );
  144. }
  145. while( 0 );
  146. ssl_close_notify( &ssl );
  147. exit:
  148. net_close( server_fd );
  149. ssl_free( &ssl );
  150. memset( &ssl, 0, sizeof( ssl ) );
  151. #ifdef WIN32
  152. printf( " + Press Enter to exit this program.\n" );
  153. fflush( stdout ); getchar();
  154. #endif
  155. return( ret );
  156. }
  157. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
  158. POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C */