blob: 7631a226b3d3bd58b61753bf3a1f967c4358f54b [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"
Paul Bakker508ad5a2011-12-04 17:09:26 +000037#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000039#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker757e2502010-02-18 19:29:00 +000041#define SERVER_PORT 4433
Paul Bakker5121ce52009-01-03 21:22:43 +000042#define SERVER_NAME "localhost"
43#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakkereaca51d2010-08-16 12:00:14 +000045#define DEBUG_LEVEL 1
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Paul Bakkerff60ee62010-03-16 21:09:09 +000047void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000048{
49 if( level < DEBUG_LEVEL )
50 {
51 fprintf( (FILE *) ctx, "%s", str );
52 fflush( (FILE *) ctx );
53 }
54}
55
Paul Bakker508ad5a2011-12-04 17:09:26 +000056#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000057 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000058 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
59 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000060int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000061{
Paul Bakkercce9d772011-11-18 14:26:47 +000062 ((void) argc);
63 ((void) argv);
64
Paul Bakker508ad5a2011-12-04 17:09:26 +000065 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000066 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000067 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
68 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000069 return( 0 );
70}
71#else
Paul Bakkercce9d772011-11-18 14:26:47 +000072int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
74 int ret, len, server_fd;
75 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +000076 char *pers = "ssl_client1";
77
78 entropy_context entropy;
79 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000080 ssl_context ssl;
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Paul Bakkercce9d772011-11-18 14:26:47 +000082 ((void) argc);
83 ((void) argv);
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085 /*
86 * 0. Initialize the RNG and the session data
87 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000088 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Paul Bakker508ad5a2011-12-04 17:09:26 +000090 printf( "\n . Seeding the random number generator..." );
91 fflush( stdout );
92
93 entropy_init( &entropy );
94 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
95 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
96 {
97 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
98 goto exit;
99 }
100
101 printf( " ok\n" );
102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 /*
104 * 1. Start the connection
105 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106 printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
107 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 fflush( stdout );
109
110 if( ( ret = net_connect( &server_fd, SERVER_NAME,
111 SERVER_PORT ) ) != 0 )
112 {
113 printf( " failed\n ! net_connect returned %d\n\n", ret );
114 goto exit;
115 }
116
117 printf( " ok\n" );
118
119 /*
120 * 2. Setup stuff
121 */
122 printf( " . Setting up the SSL/TLS structure..." );
123 fflush( stdout );
124
125 if( ( ret = ssl_init( &ssl ) ) != 0 )
126 {
127 printf( " failed\n ! ssl_init returned %d\n\n", ret );
128 goto exit;
129 }
130
131 printf( " ok\n" );
132
133 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
134 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
135
Paul Bakker508ad5a2011-12-04 17:09:26 +0000136 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 ssl_set_dbg( &ssl, my_debug, stdout );
138 ssl_set_bio( &ssl, net_recv, &server_fd,
139 net_send, &server_fd );
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 /*
142 * 3. Write the GET request
143 */
144 printf( " > Write to server:" );
145 fflush( stdout );
146
147 len = sprintf( (char *) buf, GET_REQUEST );
148
149 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
150 {
Paul Bakker831a7552011-05-18 13:32:51 +0000151 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 {
153 printf( " failed\n ! ssl_write returned %d\n\n", ret );
154 goto exit;
155 }
156 }
157
158 len = ret;
159 printf( " %d bytes written\n\n%s", len, (char *) buf );
160
161 /*
162 * 7. Read the HTTP response
163 */
164 printf( " < Read from server:" );
165 fflush( stdout );
166
167 do
168 {
169 len = sizeof( buf ) - 1;
170 memset( buf, 0, sizeof( buf ) );
171 ret = ssl_read( &ssl, buf, len );
172
Paul Bakker831a7552011-05-18 13:32:51 +0000173 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 continue;
175
Paul Bakker40e46942009-01-03 21:51:57 +0000176 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 break;
178
Paul Bakker61da7522011-11-11 10:28:58 +0000179 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 {
181 printf( "failed\n ! ssl_read returned %d\n\n", ret );
182 break;
183 }
184
Paul Bakker61da7522011-11-11 10:28:58 +0000185 if( ret == 0 )
186 {
187 printf( "\n\nEOF\n\n" );
188 break;
189 }
190
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 len = ret;
192 printf( " %d bytes read\n\n%s", len, (char *) buf );
193 }
Paul Bakker61da7522011-11-11 10:28:58 +0000194 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 ssl_close_notify( &ssl );
197
198exit:
199
Paul Bakkera3d195c2011-11-27 21:07:34 +0000200#ifdef POLARSSL_ERROR_C
201 if( ret != 0 )
202 {
203 char error_buf[100];
204 error_strerror( ret, error_buf, 100 );
205 printf("Last error was: %d - %s\n\n", ret, error_buf );
206 }
207#endif
208
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 net_close( server_fd );
210 ssl_free( &ssl );
211
212 memset( &ssl, 0, sizeof( ssl ) );
213
Paul Bakkercce9d772011-11-18 14:26:47 +0000214#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 printf( " + Press Enter to exit this program.\n" );
216 fflush( stdout ); getchar();
217#endif
218
219 return( ret );
220}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000221#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
222 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
223 POLARSSL_CTR_DRBG_C */