blob: b57fe8db688d3d92de5374c74733a9e58205930a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/net.h"
36#include "polarssl/ssl.h"
37#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker757e2502010-02-18 19:29:00 +000039#define SERVER_PORT 4433
Paul Bakker5121ce52009-01-03 21:22:43 +000040#define SERVER_NAME "localhost"
41#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Paul Bakkereaca51d2010-08-16 12:00:14 +000043#define DEBUG_LEVEL 1
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakkerff60ee62010-03-16 21:09:09 +000045void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000046{
47 if( level < DEBUG_LEVEL )
48 {
49 fprintf( (FILE *) ctx, "%s", str );
50 fflush( (FILE *) ctx );
51 }
52}
53
Paul Bakker5690efc2011-05-26 13:16:06 +000054#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
55 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
56 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000057int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000058{
Paul Bakkercce9d772011-11-18 14:26:47 +000059 ((void) argc);
60 ((void) argv);
61
Paul Bakker5690efc2011-05-26 13:16:06 +000062 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
63 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
64 "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
65 return( 0 );
66}
67#else
Paul Bakkercce9d772011-11-18 14:26:47 +000068int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
70 int ret, len, server_fd;
71 unsigned char buf[1024];
72 havege_state hs;
73 ssl_context ssl;
74 ssl_session ssn;
75
Paul Bakkercce9d772011-11-18 14:26:47 +000076 ((void) argc);
77 ((void) argv);
78
Paul Bakker5121ce52009-01-03 21:22:43 +000079 /*
80 * 0. Initialize the RNG and the session data
81 */
82 havege_init( &hs );
83 memset( &ssn, 0, sizeof( ssl_session ) );
Paul Bakker1a207ec2011-02-06 13:22:40 +000084 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86 /*
87 * 1. Start the connection
88 */
89 printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME,
90 SERVER_PORT );
91 fflush( stdout );
92
93 if( ( ret = net_connect( &server_fd, SERVER_NAME,
94 SERVER_PORT ) ) != 0 )
95 {
96 printf( " failed\n ! net_connect returned %d\n\n", ret );
97 goto exit;
98 }
99
100 printf( " ok\n" );
101
102 /*
103 * 2. Setup stuff
104 */
105 printf( " . Setting up the SSL/TLS structure..." );
106 fflush( stdout );
107
108 if( ( ret = ssl_init( &ssl ) ) != 0 )
109 {
110 printf( " failed\n ! ssl_init returned %d\n\n", ret );
111 goto exit;
112 }
113
114 printf( " ok\n" );
115
116 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
117 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
118
119 ssl_set_rng( &ssl, havege_rand, &hs );
120 ssl_set_dbg( &ssl, my_debug, stdout );
121 ssl_set_bio( &ssl, net_recv, &server_fd,
122 net_send, &server_fd );
123
Paul Bakkere3166ce2011-01-27 17:40:50 +0000124 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 ssl_set_session( &ssl, 1, 600, &ssn );
126
127 /*
128 * 3. Write the GET request
129 */
130 printf( " > Write to server:" );
131 fflush( stdout );
132
133 len = sprintf( (char *) buf, GET_REQUEST );
134
135 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
136 {
Paul Bakker831a7552011-05-18 13:32:51 +0000137 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 {
139 printf( " failed\n ! ssl_write returned %d\n\n", ret );
140 goto exit;
141 }
142 }
143
144 len = ret;
145 printf( " %d bytes written\n\n%s", len, (char *) buf );
146
147 /*
148 * 7. Read the HTTP response
149 */
150 printf( " < Read from server:" );
151 fflush( stdout );
152
153 do
154 {
155 len = sizeof( buf ) - 1;
156 memset( buf, 0, sizeof( buf ) );
157 ret = ssl_read( &ssl, buf, len );
158
Paul Bakker831a7552011-05-18 13:32:51 +0000159 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 continue;
161
Paul Bakker40e46942009-01-03 21:51:57 +0000162 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 break;
164
Paul Bakker61da7522011-11-11 10:28:58 +0000165 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 {
167 printf( "failed\n ! ssl_read returned %d\n\n", ret );
168 break;
169 }
170
Paul Bakker61da7522011-11-11 10:28:58 +0000171 if( ret == 0 )
172 {
173 printf( "\n\nEOF\n\n" );
174 break;
175 }
176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 len = ret;
178 printf( " %d bytes read\n\n%s", len, (char *) buf );
179 }
Paul Bakker61da7522011-11-11 10:28:58 +0000180 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182 ssl_close_notify( &ssl );
183
184exit:
185
186 net_close( server_fd );
187 ssl_free( &ssl );
188
189 memset( &ssl, 0, sizeof( ssl ) );
190
Paul Bakkercce9d772011-11-18 14:26:47 +0000191#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 printf( " + Press Enter to exit this program.\n" );
193 fflush( stdout ); getchar();
194#endif
195
196 return( ret );
197}
Paul Bakker5690efc2011-05-26 13:16:06 +0000198#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
199 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C */