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