Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client demonstration program |
| 3 | * |
| 4 | * Copyright (C) 2006-2007 Christophe Devine |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 22 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 23 | #endif |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 28 | #include "polarssl/net.h" |
| 29 | #include "polarssl/ssl.h" |
| 30 | #include "polarssl/havege.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
| 32 | #define SERVER_PORT 443 |
| 33 | /* |
| 34 | #define SERVER_NAME "localhost" |
| 35 | #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n" |
| 36 | */ |
| 37 | #define SERVER_NAME "xyssl.org" |
| 38 | #define GET_REQUEST \ |
| 39 | "GET /hello/ HTTP/1.1\r\n" \ |
| 40 | "Host: xyssl.org\r\n\r\n" |
| 41 | |
| 42 | #define DEBUG_LEVEL 0 |
| 43 | |
| 44 | void my_debug( void *ctx, int level, char *str ) |
| 45 | { |
| 46 | if( level < DEBUG_LEVEL ) |
| 47 | { |
| 48 | fprintf( (FILE *) ctx, "%s", str ); |
| 49 | fflush( (FILE *) ctx ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | int main( void ) |
| 54 | { |
| 55 | int ret, len, server_fd; |
| 56 | unsigned char buf[1024]; |
| 57 | havege_state hs; |
| 58 | ssl_context ssl; |
| 59 | ssl_session ssn; |
| 60 | |
| 61 | /* |
| 62 | * 0. Initialize the RNG and the session data |
| 63 | */ |
| 64 | havege_init( &hs ); |
| 65 | memset( &ssn, 0, sizeof( ssl_session ) ); |
| 66 | |
| 67 | /* |
| 68 | * 1. Start the connection |
| 69 | */ |
| 70 | printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME, |
| 71 | SERVER_PORT ); |
| 72 | fflush( stdout ); |
| 73 | |
| 74 | if( ( ret = net_connect( &server_fd, SERVER_NAME, |
| 75 | SERVER_PORT ) ) != 0 ) |
| 76 | { |
| 77 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 78 | goto exit; |
| 79 | } |
| 80 | |
| 81 | printf( " ok\n" ); |
| 82 | |
| 83 | /* |
| 84 | * 2. Setup stuff |
| 85 | */ |
| 86 | printf( " . Setting up the SSL/TLS structure..." ); |
| 87 | fflush( stdout ); |
| 88 | |
| 89 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 90 | { |
| 91 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
| 92 | goto exit; |
| 93 | } |
| 94 | |
| 95 | printf( " ok\n" ); |
| 96 | |
| 97 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
| 98 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 99 | |
| 100 | ssl_set_rng( &ssl, havege_rand, &hs ); |
| 101 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 102 | ssl_set_bio( &ssl, net_recv, &server_fd, |
| 103 | net_send, &server_fd ); |
| 104 | |
| 105 | ssl_set_ciphers( &ssl, ssl_default_ciphers ); |
| 106 | ssl_set_session( &ssl, 1, 600, &ssn ); |
| 107 | |
| 108 | /* |
| 109 | * 3. Write the GET request |
| 110 | */ |
| 111 | printf( " > Write to server:" ); |
| 112 | fflush( stdout ); |
| 113 | |
| 114 | len = sprintf( (char *) buf, GET_REQUEST ); |
| 115 | |
| 116 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 117 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 118 | if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | { |
| 120 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 121 | goto exit; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | len = ret; |
| 126 | printf( " %d bytes written\n\n%s", len, (char *) buf ); |
| 127 | |
| 128 | /* |
| 129 | * 7. Read the HTTP response |
| 130 | */ |
| 131 | printf( " < Read from server:" ); |
| 132 | fflush( stdout ); |
| 133 | |
| 134 | do |
| 135 | { |
| 136 | len = sizeof( buf ) - 1; |
| 137 | memset( buf, 0, sizeof( buf ) ); |
| 138 | ret = ssl_read( &ssl, buf, len ); |
| 139 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 140 | if( ret == POLARSSL_ERR_NET_TRY_AGAIN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | continue; |
| 142 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 143 | if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | if( ret <= 0 ) |
| 147 | { |
| 148 | printf( "failed\n ! ssl_read returned %d\n\n", ret ); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | len = ret; |
| 153 | printf( " %d bytes read\n\n%s", len, (char *) buf ); |
| 154 | } |
| 155 | while( 0 ); |
| 156 | |
| 157 | ssl_close_notify( &ssl ); |
| 158 | |
| 159 | exit: |
| 160 | |
| 161 | net_close( server_fd ); |
| 162 | ssl_free( &ssl ); |
| 163 | |
| 164 | memset( &ssl, 0, sizeof( ssl ) ); |
| 165 | |
| 166 | #ifdef WIN32 |
| 167 | printf( " + Press Enter to exit this program.\n" ); |
| 168 | fflush( stdout ); getchar(); |
| 169 | #endif |
| 170 | |
| 171 | return( ret ); |
| 172 | } |