blob: 7321fa55f53450f4089b56356a3a592af0210400 [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 Bakker0e6975b2009-02-10 22:19:10 +000046
Paul Bakker9caf2d22010-02-18 19:37:19 +000047#define GET_REQUEST "GET %s HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Paul Bakker9caf2d22010-02-18 19:37:19 +000049/*
50 * global options
51 */
52struct options
53{
Paul Bakker67968392010-07-18 08:28:20 +000054 char *server_name; /* hostname of the server (client only) */
55 int server_port; /* port on which the ssl service runs */
56 int debug_level; /* level of debugging */
57 char *request_page; /* page on server to request */
58 char *crt_file; /* the file with the client certificate */
59 char *key_file; /* the file with the client key */
Paul Bakker9caf2d22010-02-18 19:37:19 +000060} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Paul Bakkerff60ee62010-03-16 21:09:09 +000062void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
Paul Bakker9caf2d22010-02-18 19:37:19 +000064 if( level < opt.debug_level )
Paul Bakker5121ce52009-01-03 21:22:43 +000065 {
66 fprintf( (FILE *) ctx, "%s", str );
67 fflush( (FILE *) ctx );
68 }
69}
70
Paul Bakker9caf2d22010-02-18 19:37:19 +000071#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +000072 "\n usage: ssl_client2 param=<>...\n" \
73 "\n acceptable parameters:\n" \
74 " server_name=%%s default: localhost\n" \
75 " server_port=%%d default: 4433\n" \
76 " debug_level=%%d default: 0 (disabled)\n" \
77 " request_page=%%s default: \".\"\n" \
78 " crt_file=%%s default: \"\" (pre-loaded)\n" \
79 " key_file=%%s default: \"\" (pre-loaded)\n" \
Paul Bakker9caf2d22010-02-18 19:37:19 +000080 "\n"
81
82int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000083{
Paul Bakkerf80d4532010-03-16 21:16:04 +000084 int ret = 0, len, server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000085 unsigned char buf[1024];
86 havege_state hs;
87 ssl_context ssl;
88 ssl_session ssn;
89 x509_cert cacert;
90 x509_cert clicert;
91 rsa_context rsa;
Paul Bakker9caf2d22010-02-18 19:37:19 +000092 int i, j, n;
93 char *p, *q;
94
Paul Bakker1a207ec2011-02-06 13:22:40 +000095 /*
96 * Make sure memory references are valid.
97 */
98 server_fd = 0;
99 memset( &ssn, 0, sizeof( ssl_session ) );
100 memset( &ssl, 0, sizeof( ssl_context ) );
101 memset( &cacert, 0, sizeof( x509_cert ) );
102 memset( &clicert, 0, sizeof( x509_cert ) );
103 memset( &rsa, 0, sizeof( rsa_context ) );
104
Paul Bakker9caf2d22010-02-18 19:37:19 +0000105 if( argc == 0 )
106 {
107 usage:
108 printf( USAGE );
109 goto exit;
110 }
111
112 opt.server_name = DFL_SERVER_NAME;
113 opt.server_port = DFL_SERVER_PORT;
114 opt.debug_level = DFL_DEBUG_LEVEL;
115 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker67968392010-07-18 08:28:20 +0000116 opt.crt_file = DFL_CRT_FILE;
117 opt.key_file = DFL_KEY_FILE;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000118
119 for( i = 1; i < argc; i++ )
120 {
121 n = strlen( argv[i] );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000122
123 for( j = 0; j < n; j++ )
124 {
125 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
126 argv[i][j] |= 0x20;
127 }
128
129 p = argv[i];
130 if( ( q = strchr( p, '=' ) ) == NULL )
131 goto usage;
132 *q++ = '\0';
133
134 if( strcmp( p, "server_name" ) == 0 )
135 opt.server_name = q;
136 else if( strcmp( p, "server_port" ) == 0 )
137 {
138 opt.server_port = atoi( q );
139 if( opt.server_port < 1 || opt.server_port > 65535 )
140 goto usage;
141 }
142 else if( strcmp( p, "debug_level" ) == 0 )
143 {
144 opt.debug_level = atoi( q );
145 if( opt.debug_level < 0 || opt.debug_level > 65535 )
146 goto usage;
147 }
148 else if( strcmp( p, "request_page" ) == 0 )
149 opt.request_page = q;
Paul Bakker67968392010-07-18 08:28:20 +0000150 else if( strcmp( p, "crt_file" ) == 0 )
151 opt.crt_file = q;
152 else if( strcmp( p, "key_file" ) == 0 )
153 opt.key_file = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000154 else
155 goto usage;
156 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 /*
159 * 0. Initialize the RNG and the session data
160 */
161 havege_init( &hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 /*
164 * 1.1. Load the trusted CA
165 */
166 printf( "\n . Loading the CA root certificate ..." );
167 fflush( stdout );
168
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 /*
170 * Alternatively, you may load the CA certificates from a .pem or
171 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
172 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000173 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
174 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 if( ret != 0 )
176 {
177 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
178 goto exit;
179 }
180
181 printf( " ok\n" );
182
183 /*
184 * 1.2. Load own certificate and private key
185 *
186 * (can be skipped if client authentication is not required)
187 */
188 printf( " . Loading the client cert. and key..." );
189 fflush( stdout );
190
Paul Bakker67968392010-07-18 08:28:20 +0000191 if( strlen( opt.crt_file ) )
192 ret = x509parse_crtfile( &clicert, opt.crt_file );
193 else
194 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
195 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 if( ret != 0 )
197 {
198 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
199 goto exit;
200 }
201
Paul Bakker67968392010-07-18 08:28:20 +0000202 if( strlen( opt.key_file ) )
203 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
204 else
205 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
206 strlen( test_cli_key ), NULL, 0 );
207
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 if( ret != 0 )
209 {
210 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
211 goto exit;
212 }
213
214 printf( " ok\n" );
215
216 /*
217 * 2. Start the connection
218 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000219 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
220 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 fflush( stdout );
222
Paul Bakker9caf2d22010-02-18 19:37:19 +0000223 if( ( ret = net_connect( &server_fd, opt.server_name,
224 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 {
226 printf( " failed\n ! net_connect returned %d\n\n", ret );
227 goto exit;
228 }
229
230 printf( " ok\n" );
231
232 /*
233 * 3. Setup stuff
234 */
235 printf( " . Setting up the SSL/TLS structure..." );
236 fflush( stdout );
237
238 havege_init( &hs );
239
240 if( ( ret = ssl_init( &ssl ) ) != 0 )
241 {
242 printf( " failed\n ! ssl_init returned %d\n\n", ret );
243 goto exit;
244 }
245
246 printf( " ok\n" );
247
248 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
249 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
250
251 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000252 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 ssl_set_bio( &ssl, net_recv, &server_fd,
254 net_send, &server_fd );
255
Paul Bakkere3166ce2011-01-27 17:40:50 +0000256 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 ssl_set_session( &ssl, 1, 600, &ssn );
258
Paul Bakker9caf2d22010-02-18 19:37:19 +0000259 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 ssl_set_own_cert( &ssl, &clicert, &rsa );
261
Paul Bakker9caf2d22010-02-18 19:37:19 +0000262 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264 /*
265 * 4. Handshake
266 */
267 printf( " . Performing the SSL/TLS handshake..." );
268 fflush( stdout );
269
270 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
271 {
Paul Bakker40e46942009-01-03 21:51:57 +0000272 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 {
274 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
275 goto exit;
276 }
277 }
278
Paul Bakkere3166ce2011-01-27 17:40:50 +0000279 printf( " ok\n [ Ciphersuite is %s ]\n",
280 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282 /*
283 * 5. Verify the server certificate
284 */
285 printf( " . Verifying peer X.509 certificate..." );
286
287 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
288 {
289 printf( " failed\n" );
290
291 if( ( ret & BADCERT_EXPIRED ) != 0 )
292 printf( " ! server certificate has expired\n" );
293
294 if( ( ret & BADCERT_REVOKED ) != 0 )
295 printf( " ! server certificate has been revoked\n" );
296
297 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000298 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
301 printf( " ! self-signed or not signed by a trusted CA\n" );
302
303 printf( "\n" );
304 }
305 else
306 printf( " ok\n" );
307
308 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000309 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
310 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 /*
313 * 6. Write the GET request
314 */
315 printf( " > Write to server:" );
316 fflush( stdout );
317
Paul Bakker9caf2d22010-02-18 19:37:19 +0000318 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
321 {
Paul Bakker40e46942009-01-03 21:51:57 +0000322 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 {
324 printf( " failed\n ! ssl_write returned %d\n\n", ret );
325 goto exit;
326 }
327 }
328
329 len = ret;
330 printf( " %d bytes written\n\n%s", len, (char *) buf );
331
332 /*
333 * 7. Read the HTTP response
334 */
335 printf( " < Read from server:" );
336 fflush( stdout );
337
338 do
339 {
340 len = sizeof( buf ) - 1;
341 memset( buf, 0, sizeof( buf ) );
342 ret = ssl_read( &ssl, buf, len );
343
Paul Bakker40e46942009-01-03 21:51:57 +0000344 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 continue;
346
Paul Bakker40e46942009-01-03 21:51:57 +0000347 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 break;
349
350 if( ret <= 0 )
351 {
352 printf( "failed\n ! ssl_read returned %d\n\n", ret );
353 break;
354 }
355
356 len = ret;
357 printf( " %d bytes read\n\n%s", len, (char *) buf );
358 }
359 while( 0 );
360
361 ssl_close_notify( &ssl );
362
363exit:
364
Paul Bakker1a207ec2011-02-06 13:22:40 +0000365 if( server_fd )
366 net_close( server_fd );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 x509_free( &clicert );
368 x509_free( &cacert );
369 rsa_free( &rsa );
370 ssl_free( &ssl );
371
372 memset( &ssl, 0, sizeof( ssl ) );
373
374#ifdef WIN32
375 printf( " + Press Enter to exit this program.\n" );
376 fflush( stdout ); getchar();
377#endif
378
379 return( ret );
380}