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