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

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

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4