blob: 97f319668149b638fe14ed3df0a69a8c41cb49c7 [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
95 if( argc == 0 )
96 {
97 usage:
98 printf( USAGE );
99 goto exit;
100 }
101
102 opt.server_name = DFL_SERVER_NAME;
103 opt.server_port = DFL_SERVER_PORT;
104 opt.debug_level = DFL_DEBUG_LEVEL;
105 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker67968392010-07-18 08:28:20 +0000106 opt.crt_file = DFL_CRT_FILE;
107 opt.key_file = DFL_KEY_FILE;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000108
109 for( i = 1; i < argc; i++ )
110 {
111 n = strlen( argv[i] );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000112
113 for( j = 0; j < n; j++ )
114 {
115 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
116 argv[i][j] |= 0x20;
117 }
118
119 p = argv[i];
120 if( ( q = strchr( p, '=' ) ) == NULL )
121 goto usage;
122 *q++ = '\0';
123
124 if( strcmp( p, "server_name" ) == 0 )
125 opt.server_name = q;
126 else if( strcmp( p, "server_port" ) == 0 )
127 {
128 opt.server_port = atoi( q );
129 if( opt.server_port < 1 || opt.server_port > 65535 )
130 goto usage;
131 }
132 else if( strcmp( p, "debug_level" ) == 0 )
133 {
134 opt.debug_level = atoi( q );
135 if( opt.debug_level < 0 || opt.debug_level > 65535 )
136 goto usage;
137 }
138 else if( strcmp( p, "request_page" ) == 0 )
139 opt.request_page = q;
Paul Bakker67968392010-07-18 08:28:20 +0000140 else if( strcmp( p, "crt_file" ) == 0 )
141 opt.crt_file = q;
142 else if( strcmp( p, "key_file" ) == 0 )
143 opt.key_file = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000144 else
145 goto usage;
146 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148 /*
149 * 0. Initialize the RNG and the session data
150 */
151 havege_init( &hs );
152 memset( &ssn, 0, sizeof( ssl_session ) );
153
154 /*
155 * 1.1. Load the trusted CA
156 */
157 printf( "\n . Loading the CA root certificate ..." );
158 fflush( stdout );
159
160 memset( &cacert, 0, sizeof( x509_cert ) );
161
162 /*
163 * Alternatively, you may load the CA certificates from a .pem or
164 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
165 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000166 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
167 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 if( ret != 0 )
169 {
170 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
171 goto exit;
172 }
173
174 printf( " ok\n" );
175
176 /*
177 * 1.2. Load own certificate and private key
178 *
179 * (can be skipped if client authentication is not required)
180 */
181 printf( " . Loading the client cert. and key..." );
182 fflush( stdout );
183
184 memset( &clicert, 0, sizeof( x509_cert ) );
185
Paul Bakker67968392010-07-18 08:28:20 +0000186 if( strlen( opt.crt_file ) )
187 ret = x509parse_crtfile( &clicert, opt.crt_file );
188 else
189 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
190 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 if( ret != 0 )
192 {
193 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
194 goto exit;
195 }
196
Paul Bakker67968392010-07-18 08:28:20 +0000197 if( strlen( opt.key_file ) )
198 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
199 else
200 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
201 strlen( test_cli_key ), NULL, 0 );
202
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 if( ret != 0 )
204 {
205 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
206 goto exit;
207 }
208
209 printf( " ok\n" );
210
211 /*
212 * 2. Start the connection
213 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000214 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
215 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 fflush( stdout );
217
Paul Bakker9caf2d22010-02-18 19:37:19 +0000218 if( ( ret = net_connect( &server_fd, opt.server_name,
219 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 {
221 printf( " failed\n ! net_connect returned %d\n\n", ret );
222 goto exit;
223 }
224
225 printf( " ok\n" );
226
227 /*
228 * 3. Setup stuff
229 */
230 printf( " . Setting up the SSL/TLS structure..." );
231 fflush( stdout );
232
233 havege_init( &hs );
234
235 if( ( ret = ssl_init( &ssl ) ) != 0 )
236 {
237 printf( " failed\n ! ssl_init returned %d\n\n", ret );
238 goto exit;
239 }
240
241 printf( " ok\n" );
242
243 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
244 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
245
246 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000247 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 ssl_set_bio( &ssl, net_recv, &server_fd,
249 net_send, &server_fd );
250
251 ssl_set_ciphers( &ssl, ssl_default_ciphers );
252 ssl_set_session( &ssl, 1, 600, &ssn );
253
Paul Bakker9caf2d22010-02-18 19:37:19 +0000254 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 ssl_set_own_cert( &ssl, &clicert, &rsa );
256
Paul Bakker9caf2d22010-02-18 19:37:19 +0000257 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
259 /*
260 * 4. Handshake
261 */
262 printf( " . Performing the SSL/TLS handshake..." );
263 fflush( stdout );
264
265 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
266 {
Paul Bakker40e46942009-01-03 21:51:57 +0000267 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 {
269 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
270 goto exit;
271 }
272 }
273
274 printf( " ok\n [ Cipher is %s ]\n",
275 ssl_get_cipher( &ssl ) );
276
277 /*
278 * 5. Verify the server certificate
279 */
280 printf( " . Verifying peer X.509 certificate..." );
281
282 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
283 {
284 printf( " failed\n" );
285
286 if( ( ret & BADCERT_EXPIRED ) != 0 )
287 printf( " ! server certificate has expired\n" );
288
289 if( ( ret & BADCERT_REVOKED ) != 0 )
290 printf( " ! server certificate has been revoked\n" );
291
292 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000293 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
296 printf( " ! self-signed or not signed by a trusted CA\n" );
297
298 printf( "\n" );
299 }
300 else
301 printf( " ok\n" );
302
303 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000304 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
305 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 /*
308 * 6. Write the GET request
309 */
310 printf( " > Write to server:" );
311 fflush( stdout );
312
Paul Bakker9caf2d22010-02-18 19:37:19 +0000313 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
316 {
Paul Bakker40e46942009-01-03 21:51:57 +0000317 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 {
319 printf( " failed\n ! ssl_write returned %d\n\n", ret );
320 goto exit;
321 }
322 }
323
324 len = ret;
325 printf( " %d bytes written\n\n%s", len, (char *) buf );
326
327 /*
328 * 7. Read the HTTP response
329 */
330 printf( " < Read from server:" );
331 fflush( stdout );
332
333 do
334 {
335 len = sizeof( buf ) - 1;
336 memset( buf, 0, sizeof( buf ) );
337 ret = ssl_read( &ssl, buf, len );
338
Paul Bakker40e46942009-01-03 21:51:57 +0000339 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 continue;
341
Paul Bakker40e46942009-01-03 21:51:57 +0000342 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 break;
344
345 if( ret <= 0 )
346 {
347 printf( "failed\n ! ssl_read returned %d\n\n", ret );
348 break;
349 }
350
351 len = ret;
352 printf( " %d bytes read\n\n%s", len, (char *) buf );
353 }
354 while( 0 );
355
356 ssl_close_notify( &ssl );
357
358exit:
359
360 net_close( server_fd );
361 x509_free( &clicert );
362 x509_free( &cacert );
363 rsa_free( &rsa );
364 ssl_free( &ssl );
365
366 memset( &ssl, 0, sizeof( ssl ) );
367
368#ifdef WIN32
369 printf( " + Press Enter to exit this program.\n" );
370 fflush( stdout ); getchar();
371#endif
372
373 return( ret );
374}