blob: aaebed6f9a34d3565e0604f8395666822d8afb82 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, 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 Bakker508ad5a2011-12-04 17:09:26 +000042#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000043 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000044 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020045 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000046int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Paul Bakkercce9d772011-11-18 14:26:47 +000048 ((void) argc);
49 ((void) argv);
50
Paul Bakker508ad5a2011-12-04 17:09:26 +000051 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000052 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000053 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +020054 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C "
55 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000056 return( 0 );
57}
58#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020059
60#define SERVER_PORT 4433
61#define SERVER_NAME "localhost"
62#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
63
64#define DEBUG_LEVEL 1
65
66static void my_debug( void *ctx, int level, const char *str )
67{
68 if( level < DEBUG_LEVEL )
69 {
70 fprintf( (FILE *) ctx, "%s", str );
71 fflush( (FILE *) ctx );
72 }
73}
74
Paul Bakkercce9d772011-11-18 14:26:47 +000075int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
77 int ret, len, server_fd;
78 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000080
81 entropy_context entropy;
82 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000083 ssl_context ssl;
Paul Bakker75242c32012-11-17 00:03:46 +010084 x509_cert cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Paul Bakkercce9d772011-11-18 14:26:47 +000086 ((void) argc);
87 ((void) argv);
88
Paul Bakker5121ce52009-01-03 21:22:43 +000089 /*
90 * 0. Initialize the RNG and the session data
91 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000092 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker75242c32012-11-17 00:03:46 +010093 memset( &cacert, 0, sizeof( x509_cert ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakker508ad5a2011-12-04 17:09:26 +000095 printf( "\n . Seeding the random number generator..." );
96 fflush( stdout );
97
98 entropy_init( &entropy );
99 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200100 (const unsigned char *) pers,
101 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000102 {
103 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
104 goto exit;
105 }
106
107 printf( " ok\n" );
108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100110 * 0. Initialize certificates
111 */
112 printf( " . Loading the CA root certificate ..." );
113 fflush( stdout );
114
115#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200116 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker75242c32012-11-17 00:03:46 +0100117 strlen( test_ca_crt ) );
118#else
119 ret = 1;
120 printf("POLARSSL_CERTS_C not defined.");
121#endif
122
123 if( ret < 0 )
124 {
125 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
126 goto exit;
127 }
128
129 printf( " ok (%d skipped)\n", ret );
130
131 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 * 1. Start the connection
133 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000134 printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
135 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 fflush( stdout );
137
138 if( ( ret = net_connect( &server_fd, SERVER_NAME,
139 SERVER_PORT ) ) != 0 )
140 {
141 printf( " failed\n ! net_connect returned %d\n\n", ret );
142 goto exit;
143 }
144
145 printf( " ok\n" );
146
147 /*
148 * 2. Setup stuff
149 */
150 printf( " . Setting up the SSL/TLS structure..." );
151 fflush( stdout );
152
153 if( ( ret = ssl_init( &ssl ) ) != 0 )
154 {
155 printf( " failed\n ! ssl_init returned %d\n\n", ret );
156 goto exit;
157 }
158
159 printf( " ok\n" );
160
161 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker75242c32012-11-17 00:03:46 +0100162 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
163 ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakker508ad5a2011-12-04 17:09:26 +0000165 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 ssl_set_dbg( &ssl, my_debug, stdout );
167 ssl_set_bio( &ssl, net_recv, &server_fd,
168 net_send, &server_fd );
169
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100171 * 4. Handshake
172 */
173 printf( " . Performing the SSL/TLS handshake..." );
174 fflush( stdout );
175
176 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
177 {
178 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
179 {
180 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
181 goto exit;
182 }
183 }
184
185 printf( " ok\n" );
186
187 /*
188 * 5. Verify the server certificate
189 */
190 printf( " . Verifying peer X.509 certificate..." );
191
192 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
193 {
194 printf( " failed\n" );
195
196 if( ( ret & BADCERT_EXPIRED ) != 0 )
197 printf( " ! server certificate has expired\n" );
198
199 if( ( ret & BADCERT_REVOKED ) != 0 )
200 printf( " ! server certificate has been revoked\n" );
201
202 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
203 printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
204
205 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
206 printf( " ! self-signed or not signed by a trusted CA\n" );
207
208 printf( "\n" );
209 }
210 else
211 printf( " ok\n" );
212
213 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 * 3. Write the GET request
215 */
216 printf( " > Write to server:" );
217 fflush( stdout );
218
219 len = sprintf( (char *) buf, GET_REQUEST );
220
221 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
222 {
Paul Bakker831a7552011-05-18 13:32:51 +0000223 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 {
225 printf( " failed\n ! ssl_write returned %d\n\n", ret );
226 goto exit;
227 }
228 }
229
230 len = ret;
231 printf( " %d bytes written\n\n%s", len, (char *) buf );
232
233 /*
234 * 7. Read the HTTP response
235 */
236 printf( " < Read from server:" );
237 fflush( stdout );
238
239 do
240 {
241 len = sizeof( buf ) - 1;
242 memset( buf, 0, sizeof( buf ) );
243 ret = ssl_read( &ssl, buf, len );
244
Paul Bakker831a7552011-05-18 13:32:51 +0000245 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 continue;
247
Paul Bakker40e46942009-01-03 21:51:57 +0000248 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 break;
250
Paul Bakker61da7522011-11-11 10:28:58 +0000251 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 {
253 printf( "failed\n ! ssl_read returned %d\n\n", ret );
254 break;
255 }
256
Paul Bakker61da7522011-11-11 10:28:58 +0000257 if( ret == 0 )
258 {
259 printf( "\n\nEOF\n\n" );
260 break;
261 }
262
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 len = ret;
264 printf( " %d bytes read\n\n%s", len, (char *) buf );
265 }
Paul Bakker61da7522011-11-11 10:28:58 +0000266 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268 ssl_close_notify( &ssl );
269
270exit:
271
Paul Bakkera3d195c2011-11-27 21:07:34 +0000272#ifdef POLARSSL_ERROR_C
273 if( ret != 0 )
274 {
275 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200276 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000277 printf("Last error was: %d - %s\n\n", ret, error_buf );
278 }
279#endif
280
Paul Bakker75242c32012-11-17 00:03:46 +0100281 x509_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 net_close( server_fd );
283 ssl_free( &ssl );
284
285 memset( &ssl, 0, sizeof( ssl ) );
286
Paul Bakkercce9d772011-11-18 14:26:47 +0000287#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 printf( " + Press Enter to exit this program.\n" );
289 fflush( stdout ); getchar();
290#endif
291
292 return( ret );
293}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000294#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
295 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
296 POLARSSL_CTR_DRBG_C */