/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/ssl/ssl_client1.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 174 $ : Last Committed Revision
00003  * $Date: 2008-06-24 10:50:29 -0700 (Tue, 24 Jun 2008) $ : Last Committed Date */
00004 /*
00005  *  Basic SSL client demonstration program
00006  *
00007  *  Copyright (C) 2006-2007  Christophe Devine
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License, version 2.1 as published by the Free Software Foundation.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021  *  MA  02110-1301  USA
00022  */
00023 
00024 #ifndef _CRT_SECURE_NO_DEPRECATE
00025 #define _CRT_SECURE_NO_DEPRECATE 1
00026 #endif
00027 
00028 #include <string.h>
00029 #include <stdio.h>
00030 
00031 #include "xyssl/net.h"
00032 #include "xyssl/ssl.h"
00033 #include "xyssl/havege.h"
00034 
00035 #define SERVER_PORT 4433
00036 /*
00037 #define SERVER_NAME "localhost"
00038 #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
00039 */
00040 #define SERVER_NAME "xyssl.org"
00041 #define GET_REQUEST \
00042     "GET /hello/ HTTP/1.0\r\n" \
00043     "Host: xyssl.org\r\n\r\n"
00044 
00045 int main( void )
00046 {
00047     int ret, len;
00048     int server_fd;
00049     unsigned char buf[1024];
00050     havege_state hs;
00051     ssl_context ssl;
00052 
00053     /*
00054      * 1. Initiate the connection
00055      */
00056     printf( "\n  . Connecting to tcp/%s/%4d...", SERVER_NAME,
00057                                                  SERVER_PORT );
00058     fflush( stdout );
00059 
00060     if( ( ret = net_connect( &server_fd, SERVER_NAME,
00061                                          SERVER_PORT ) ) != 0 )
00062     {
00063         printf( " failed\n  ! net_connect returned %08x\n\n", ret );
00064         goto exit;
00065     }
00066 
00067     printf( " ok\n" );
00068 
00069     /*
00070      * 2. Setup stuff
00071      */
00072     printf( "  . Setting up the RNG and SSL state..." );
00073     fflush( stdout );
00074 
00075     havege_init( &hs );
00076 
00077     if( ( ret = ssl_init( &ssl, 1 ) ) != 0 )
00078     {
00079         printf( " failed\n  ! ssl_init returned %08x\n\n", ret );
00080         goto exit;
00081     }
00082 
00083     printf( " ok\n" );
00084 
00085     ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
00086     ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
00087 
00088     ssl_set_rng_func( &ssl, havege_rand, &hs );
00089     ssl_set_io_files( &ssl, server_fd, server_fd );
00090     ssl_set_ciphlist( &ssl, ssl_default_ciphers );
00091 
00092     /*
00093      * 3. Write the GET request
00094      */
00095     printf( "  > Write to server:" );
00096 
00097     len = sprintf( (char *) buf, GET_REQUEST );
00098 
00099     while( ( ret = ssl_write( &ssl, buf, len ) ) != 0 )
00100     {
00101         if( ret != ERR_NET_WOULD_BLOCK )
00102         {
00103             printf( " failed\n  ! ssl_write returned %08x\n\n", ret );
00104             goto exit;
00105         }
00106     }
00107 
00108     printf( "\n\n%s", buf );
00109 
00110     /*
00111      * 7. Read the HTTP response
00112      */
00113     printf( "  < Read from server:\n\n" );
00114 
00115     while( 1 )
00116     {
00117         len = sizeof( buf ) - 1;
00118         ret = ssl_read( &ssl, buf, &len );
00119         if( ret == ERR_NET_WOULD_BLOCK )
00120             continue;
00121 
00122         if( ret == ERR_SSL_PEER_CLOSE_NOTIFY )
00123             break;
00124 
00125         if( ret != 0 )
00126         {
00127             printf( "  ! ssl_read returned %08x\n\n", ret );
00128             break;
00129         }
00130 
00131         buf[len] = '\0';
00132         printf( "%s", buf );
00133     }
00134 
00135     ssl_close_notify( &ssl );
00136 
00137 exit:
00138 
00139     net_close( server_fd );
00140     ssl_free( &ssl );
00141 
00142     memset( &ssl, 0, sizeof( ssl ) );
00143 
00144 #ifdef WIN32
00145     printf( "  + Press Enter to exit this program.\n" );
00146     fflush( stdout ); getchar();
00147 #endif
00148 
00149     return( ret );
00150 }

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4