ssl_client1.c 5.0 KB

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