blob: 78ad43e72db8d2268be481e93f70c94eed91e5c5 [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 Bakker75242c32012-11-17 00:03:46 +010040#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker757e2502010-02-18 19:29:00 +000042#define SERVER_PORT 4433
Paul Bakker5121ce52009-01-03 21:22:43 +000043#define SERVER_NAME "localhost"
44#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Paul Bakkereaca51d2010-08-16 12:00:14 +000046#define DEBUG_LEVEL 1
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Paul Bakkerff60ee62010-03-16 21:09:09 +000048void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000049{
50 if( level < DEBUG_LEVEL )
51 {
52 fprintf( (FILE *) ctx, "%s", str );
53 fflush( (FILE *) ctx );
54 }
55}
56
Paul Bakker508ad5a2011-12-04 17:09:26 +000057#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000058 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000059 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
60 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000061int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000062{
Paul Bakkercce9d772011-11-18 14:26:47 +000063 ((void) argc);
64 ((void) argv);
65
Paul Bakker508ad5a2011-12-04 17:09:26 +000066 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000067 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000068 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
69 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000070 return( 0 );
71}
72#else
Paul Bakkercce9d772011-11-18 14:26:47 +000073int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
75 int ret, len, server_fd;
76 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +000077 char *pers = "ssl_client1";
78
79 entropy_context entropy;
80 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000081 ssl_context ssl;
Paul Bakker75242c32012-11-17 00:03:46 +010082 x509_cert cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Paul Bakkercce9d772011-11-18 14:26:47 +000084 ((void) argc);
85 ((void) argv);
86
Paul Bakker5121ce52009-01-03 21:22:43 +000087 /*
88 * 0. Initialize the RNG and the session data
89 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000090 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker75242c32012-11-17 00:03:46 +010091 memset( &cacert, 0, sizeof( x509_cert ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakker508ad5a2011-12-04 17:09:26 +000093 printf( "\n . Seeding the random number generator..." );
94 fflush( stdout );
95
96 entropy_init( &entropy );
97 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
98 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
99 {
100 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
101 goto exit;
102 }
103
104 printf( " ok\n" );
105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100107 * 0. Initialize certificates
108 */
109 printf( " . Loading the CA root certificate ..." );
110 fflush( stdout );
111
112#if defined(POLARSSL_CERTS_C)
113 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
114 strlen( test_ca_crt ) );
115#else
116 ret = 1;
117 printf("POLARSSL_CERTS_C not defined.");
118#endif
119
120 if( ret < 0 )
121 {
122 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
123 goto exit;
124 }
125
126 printf( " ok (%d skipped)\n", ret );
127
128 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * 1. Start the connection
130 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000131 printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
132 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 fflush( stdout );
134
135 if( ( ret = net_connect( &server_fd, SERVER_NAME,
136 SERVER_PORT ) ) != 0 )
137 {
138 printf( " failed\n ! net_connect returned %d\n\n", ret );
139 goto exit;
140 }
141
142 printf( " ok\n" );
143
144 /*
145 * 2. Setup stuff
146 */
147 printf( " . Setting up the SSL/TLS structure..." );
148 fflush( stdout );
149
150 if( ( ret = ssl_init( &ssl ) ) != 0 )
151 {
152 printf( " failed\n ! ssl_init returned %d\n\n", ret );
153 goto exit;
154 }
155
156 printf( " ok\n" );
157
158 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker75242c32012-11-17 00:03:46 +0100159 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
160 ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 ssl_set_dbg( &ssl, my_debug, stdout );
164 ssl_set_bio( &ssl, net_recv, &server_fd,
165 net_send, &server_fd );
166
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100168 * 4. Handshake
169 */
170 printf( " . Performing the SSL/TLS handshake..." );
171 fflush( stdout );
172
173 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
174 {
175 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
176 {
177 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
178 goto exit;
179 }
180 }
181
182 printf( " ok\n" );
183
184 /*
185 * 5. Verify the server certificate
186 */
187 printf( " . Verifying peer X.509 certificate..." );
188
189 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
190 {
191 printf( " failed\n" );
192
193 if( ( ret & BADCERT_EXPIRED ) != 0 )
194 printf( " ! server certificate has expired\n" );
195
196 if( ( ret & BADCERT_REVOKED ) != 0 )
197 printf( " ! server certificate has been revoked\n" );
198
199 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
200 printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
201
202 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
203 printf( " ! self-signed or not signed by a trusted CA\n" );
204
205 printf( "\n" );
206 }
207 else
208 printf( " ok\n" );
209
210 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 * 3. Write the GET request
212 */
213 printf( " > Write to server:" );
214 fflush( stdout );
215
216 len = sprintf( (char *) buf, GET_REQUEST );
217
218 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
219 {
Paul Bakker831a7552011-05-18 13:32:51 +0000220 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 {
222 printf( " failed\n ! ssl_write returned %d\n\n", ret );
223 goto exit;
224 }
225 }
226
227 len = ret;
228 printf( " %d bytes written\n\n%s", len, (char *) buf );
229
230 /*
231 * 7. Read the HTTP response
232 */
233 printf( " < Read from server:" );
234 fflush( stdout );
235
236 do
237 {
238 len = sizeof( buf ) - 1;
239 memset( buf, 0, sizeof( buf ) );
240 ret = ssl_read( &ssl, buf, len );
241
Paul Bakker831a7552011-05-18 13:32:51 +0000242 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 continue;
244
Paul Bakker40e46942009-01-03 21:51:57 +0000245 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 break;
247
Paul Bakker61da7522011-11-11 10:28:58 +0000248 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 {
250 printf( "failed\n ! ssl_read returned %d\n\n", ret );
251 break;
252 }
253
Paul Bakker61da7522011-11-11 10:28:58 +0000254 if( ret == 0 )
255 {
256 printf( "\n\nEOF\n\n" );
257 break;
258 }
259
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 len = ret;
261 printf( " %d bytes read\n\n%s", len, (char *) buf );
262 }
Paul Bakker61da7522011-11-11 10:28:58 +0000263 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265 ssl_close_notify( &ssl );
266
267exit:
268
Paul Bakkera3d195c2011-11-27 21:07:34 +0000269#ifdef POLARSSL_ERROR_C
270 if( ret != 0 )
271 {
272 char error_buf[100];
273 error_strerror( ret, error_buf, 100 );
274 printf("Last error was: %d - %s\n\n", ret, error_buf );
275 }
276#endif
277
Paul Bakker75242c32012-11-17 00:03:46 +0100278 x509_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 net_close( server_fd );
280 ssl_free( &ssl );
281
282 memset( &ssl, 0, sizeof( ssl ) );
283
Paul Bakkercce9d772011-11-18 14:26:47 +0000284#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 printf( " + Press Enter to exit this program.\n" );
286 fflush( stdout ); getchar();
287#endif
288
289 return( ret );
290}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000291#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
292 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
293 POLARSSL_CTR_DRBG_C */