blob: 15a0bb0a9b036a66546383cafb96435b7eab24bf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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>
Paul Bakker9caf2d22010-02-18 19:37:19 +000031#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000032#include <stdio.h>
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/net.h"
35#include "polarssl/ssl.h"
36#include "polarssl/havege.h"
37#include "polarssl/certs.h"
38#include "polarssl/x509.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker9caf2d22010-02-18 19:37:19 +000040#define DFL_SERVER_NAME "localhost"
41#define DFL_SERVER_PORT 4433
42#define DFL_REQUEST_PAGE "/"
43#define DFL_DEBUG_LEVEL 0
Paul Bakker67968392010-07-18 08:28:20 +000044#define DFL_CRT_FILE ""
45#define DFL_KEY_FILE ""
Paul Bakker51936882011-02-20 16:05:58 +000046#define DFL_FORCE_CIPHER 0
Paul Bakker0e6975b2009-02-10 22:19:10 +000047
Paul Bakker9caf2d22010-02-18 19:37:19 +000048#define GET_REQUEST "GET %s HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakker9caf2d22010-02-18 19:37:19 +000050/*
51 * global options
52 */
53struct options
54{
Paul Bakker67968392010-07-18 08:28:20 +000055 char *server_name; /* hostname of the server (client only) */
56 int server_port; /* port on which the ssl service runs */
57 int debug_level; /* level of debugging */
58 char *request_page; /* page on server to request */
59 char *crt_file; /* the file with the client certificate */
60 char *key_file; /* the file with the client key */
Paul Bakker51936882011-02-20 16:05:58 +000061 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker9caf2d22010-02-18 19:37:19 +000062} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Paul Bakkerff60ee62010-03-16 21:09:09 +000064void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000065{
Paul Bakker9caf2d22010-02-18 19:37:19 +000066 if( level < opt.debug_level )
Paul Bakker5121ce52009-01-03 21:22:43 +000067 {
68 fprintf( (FILE *) ctx, "%s", str );
69 fflush( (FILE *) ctx );
70 }
71}
72
Paul Bakker9caf2d22010-02-18 19:37:19 +000073#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +000074 "\n usage: ssl_client2 param=<>...\n" \
75 "\n acceptable parameters:\n" \
76 " server_name=%%s default: localhost\n" \
77 " server_port=%%d default: 4433\n" \
78 " debug_level=%%d default: 0 (disabled)\n" \
79 " request_page=%%s default: \".\"\n" \
80 " crt_file=%%s default: \"\" (pre-loaded)\n" \
81 " key_file=%%s default: \"\" (pre-loaded)\n" \
Paul Bakker51936882011-02-20 16:05:58 +000082 " force_ciphersuite=<name> default: all enabled\n"\
83 " acceptable ciphersuite names:\n"
Paul Bakker9caf2d22010-02-18 19:37:19 +000084
85int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
Paul Bakkerf80d4532010-03-16 21:16:04 +000087 int ret = 0, len, server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000088 unsigned char buf[1024];
89 havege_state hs;
90 ssl_context ssl;
91 ssl_session ssn;
92 x509_cert cacert;
93 x509_cert clicert;
94 rsa_context rsa;
Paul Bakker9caf2d22010-02-18 19:37:19 +000095 int i, j, n;
96 char *p, *q;
Paul Bakker51936882011-02-20 16:05:58 +000097 const int *list;
Paul Bakker9caf2d22010-02-18 19:37:19 +000098
Paul Bakker1a207ec2011-02-06 13:22:40 +000099 /*
100 * Make sure memory references are valid.
101 */
102 server_fd = 0;
103 memset( &ssn, 0, sizeof( ssl_session ) );
104 memset( &ssl, 0, sizeof( ssl_context ) );
105 memset( &cacert, 0, sizeof( x509_cert ) );
106 memset( &clicert, 0, sizeof( x509_cert ) );
107 memset( &rsa, 0, sizeof( rsa_context ) );
108
Paul Bakker9caf2d22010-02-18 19:37:19 +0000109 if( argc == 0 )
110 {
111 usage:
112 printf( USAGE );
Paul Bakker51936882011-02-20 16:05:58 +0000113
114 list = ssl_list_ciphersuites();
115 while( *list )
116 {
117 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
118 list++;
119 }
120 printf("\n");
Paul Bakker9caf2d22010-02-18 19:37:19 +0000121 goto exit;
122 }
123
124 opt.server_name = DFL_SERVER_NAME;
125 opt.server_port = DFL_SERVER_PORT;
126 opt.debug_level = DFL_DEBUG_LEVEL;
127 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker67968392010-07-18 08:28:20 +0000128 opt.crt_file = DFL_CRT_FILE;
129 opt.key_file = DFL_KEY_FILE;
Paul Bakker51936882011-02-20 16:05:58 +0000130 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000131
132 for( i = 1; i < argc; i++ )
133 {
134 n = strlen( argv[i] );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000135
136 for( j = 0; j < n; j++ )
137 {
138 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
139 argv[i][j] |= 0x20;
140 }
141
142 p = argv[i];
143 if( ( q = strchr( p, '=' ) ) == NULL )
144 goto usage;
145 *q++ = '\0';
146
147 if( strcmp( p, "server_name" ) == 0 )
148 opt.server_name = q;
149 else if( strcmp( p, "server_port" ) == 0 )
150 {
151 opt.server_port = atoi( q );
152 if( opt.server_port < 1 || opt.server_port > 65535 )
153 goto usage;
154 }
155 else if( strcmp( p, "debug_level" ) == 0 )
156 {
157 opt.debug_level = atoi( q );
158 if( opt.debug_level < 0 || opt.debug_level > 65535 )
159 goto usage;
160 }
161 else if( strcmp( p, "request_page" ) == 0 )
162 opt.request_page = q;
Paul Bakker67968392010-07-18 08:28:20 +0000163 else if( strcmp( p, "crt_file" ) == 0 )
164 opt.crt_file = q;
165 else if( strcmp( p, "key_file" ) == 0 )
166 opt.key_file = q;
Paul Bakker51936882011-02-20 16:05:58 +0000167 else if( strcmp( p, "force_ciphersuite" ) == 0 )
168 {
169 opt.force_ciphersuite[0] = -1;
170
171 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
172
173 if( opt.force_ciphersuite[0] <= 0 )
174 goto usage;
175
176 opt.force_ciphersuite[1] = 0;
177 }
Paul Bakker9caf2d22010-02-18 19:37:19 +0000178 else
179 goto usage;
180 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182 /*
183 * 0. Initialize the RNG and the session data
184 */
185 havege_init( &hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187 /*
188 * 1.1. Load the trusted CA
189 */
190 printf( "\n . Loading the CA root certificate ..." );
191 fflush( stdout );
192
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 /*
194 * Alternatively, you may load the CA certificates from a .pem or
195 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
196 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000197 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
198 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 if( ret != 0 )
200 {
201 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
202 goto exit;
203 }
204
205 printf( " ok\n" );
206
207 /*
208 * 1.2. Load own certificate and private key
209 *
210 * (can be skipped if client authentication is not required)
211 */
212 printf( " . Loading the client cert. and key..." );
213 fflush( stdout );
214
Paul Bakker67968392010-07-18 08:28:20 +0000215 if( strlen( opt.crt_file ) )
216 ret = x509parse_crtfile( &clicert, opt.crt_file );
217 else
218 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
219 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 if( ret != 0 )
221 {
222 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
223 goto exit;
224 }
225
Paul Bakker67968392010-07-18 08:28:20 +0000226 if( strlen( opt.key_file ) )
227 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
228 else
229 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
230 strlen( test_cli_key ), NULL, 0 );
231
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 if( ret != 0 )
233 {
234 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
235 goto exit;
236 }
237
238 printf( " ok\n" );
239
240 /*
241 * 2. Start the connection
242 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000243 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
244 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 fflush( stdout );
246
Paul Bakker9caf2d22010-02-18 19:37:19 +0000247 if( ( ret = net_connect( &server_fd, opt.server_name,
248 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 {
250 printf( " failed\n ! net_connect returned %d\n\n", ret );
251 goto exit;
252 }
253
254 printf( " ok\n" );
255
256 /*
257 * 3. Setup stuff
258 */
259 printf( " . Setting up the SSL/TLS structure..." );
260 fflush( stdout );
261
262 havege_init( &hs );
263
264 if( ( ret = ssl_init( &ssl ) ) != 0 )
265 {
266 printf( " failed\n ! ssl_init returned %d\n\n", ret );
267 goto exit;
268 }
269
270 printf( " ok\n" );
271
272 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
273 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
274
275 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000276 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 ssl_set_bio( &ssl, net_recv, &server_fd,
278 net_send, &server_fd );
279
Paul Bakker51936882011-02-20 16:05:58 +0000280 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
281 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
282 else
283 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
284
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 ssl_set_session( &ssl, 1, 600, &ssn );
286
Paul Bakker9caf2d22010-02-18 19:37:19 +0000287 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 ssl_set_own_cert( &ssl, &clicert, &rsa );
289
Paul Bakker9caf2d22010-02-18 19:37:19 +0000290 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 /*
293 * 4. Handshake
294 */
295 printf( " . Performing the SSL/TLS handshake..." );
296 fflush( stdout );
297
298 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
299 {
Paul Bakker40e46942009-01-03 21:51:57 +0000300 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 {
302 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
303 goto exit;
304 }
305 }
306
Paul Bakkere3166ce2011-01-27 17:40:50 +0000307 printf( " ok\n [ Ciphersuite is %s ]\n",
308 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310 /*
311 * 5. Verify the server certificate
312 */
313 printf( " . Verifying peer X.509 certificate..." );
314
315 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
316 {
317 printf( " failed\n" );
318
319 if( ( ret & BADCERT_EXPIRED ) != 0 )
320 printf( " ! server certificate has expired\n" );
321
322 if( ( ret & BADCERT_REVOKED ) != 0 )
323 printf( " ! server certificate has been revoked\n" );
324
325 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000326 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
329 printf( " ! self-signed or not signed by a trusted CA\n" );
330
331 printf( "\n" );
332 }
333 else
334 printf( " ok\n" );
335
336 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000337 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
338 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 /*
341 * 6. Write the GET request
342 */
343 printf( " > Write to server:" );
344 fflush( stdout );
345
Paul Bakker9caf2d22010-02-18 19:37:19 +0000346 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
349 {
Paul Bakker40e46942009-01-03 21:51:57 +0000350 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 {
352 printf( " failed\n ! ssl_write returned %d\n\n", ret );
353 goto exit;
354 }
355 }
356
357 len = ret;
358 printf( " %d bytes written\n\n%s", len, (char *) buf );
359
360 /*
361 * 7. Read the HTTP response
362 */
363 printf( " < Read from server:" );
364 fflush( stdout );
365
366 do
367 {
368 len = sizeof( buf ) - 1;
369 memset( buf, 0, sizeof( buf ) );
370 ret = ssl_read( &ssl, buf, len );
371
Paul Bakker40e46942009-01-03 21:51:57 +0000372 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 continue;
374
Paul Bakker40e46942009-01-03 21:51:57 +0000375 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 break;
377
378 if( ret <= 0 )
379 {
380 printf( "failed\n ! ssl_read returned %d\n\n", ret );
381 break;
382 }
383
384 len = ret;
385 printf( " %d bytes read\n\n%s", len, (char *) buf );
386 }
387 while( 0 );
388
389 ssl_close_notify( &ssl );
390
391exit:
392
Paul Bakker1a207ec2011-02-06 13:22:40 +0000393 if( server_fd )
394 net_close( server_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 x509_free( &clicert );
396 x509_free( &cacert );
397 rsa_free( &rsa );
398 ssl_free( &ssl );
399
400 memset( &ssl, 0, sizeof( ssl ) );
401
402#ifdef WIN32
403 printf( " + Press Enter to exit this program.\n" );
404 fflush( stdout ); getchar();
405#endif
406
407 return( ret );
408}