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