blob: be5d777eb4a438061a01722980d26ec44b3905e7 [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.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
Paul Bakker9caf2d22010-02-18 19:37:19 +000028#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000029#include <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/net.h"
32#include "polarssl/ssl.h"
33#include "polarssl/havege.h"
34#include "polarssl/certs.h"
35#include "polarssl/x509.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker9caf2d22010-02-18 19:37:19 +000037#define DFL_SERVER_NAME "localhost"
38#define DFL_SERVER_PORT 4433
39#define DFL_REQUEST_PAGE "/"
40#define DFL_DEBUG_LEVEL 0
Paul Bakker67968392010-07-18 08:28:20 +000041#define DFL_CRT_FILE ""
42#define DFL_KEY_FILE ""
Paul Bakker0e6975b2009-02-10 22:19:10 +000043
Paul Bakker9caf2d22010-02-18 19:37:19 +000044#define GET_REQUEST "GET %s HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Paul Bakker9caf2d22010-02-18 19:37:19 +000046/*
47 * global options
48 */
49struct options
50{
Paul Bakker67968392010-07-18 08:28:20 +000051 char *server_name; /* hostname of the server (client only) */
52 int server_port; /* port on which the ssl service runs */
53 int debug_level; /* level of debugging */
54 char *request_page; /* page on server to request */
55 char *crt_file; /* the file with the client certificate */
56 char *key_file; /* the file with the client key */
Paul Bakker9caf2d22010-02-18 19:37:19 +000057} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakkerff60ee62010-03-16 21:09:09 +000059void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000060{
Paul Bakker9caf2d22010-02-18 19:37:19 +000061 if( level < opt.debug_level )
Paul Bakker5121ce52009-01-03 21:22:43 +000062 {
63 fprintf( (FILE *) ctx, "%s", str );
64 fflush( (FILE *) ctx );
65 }
66}
67
Paul Bakker9caf2d22010-02-18 19:37:19 +000068#define USAGE \
Paul Bakker67968392010-07-18 08:28:20 +000069 "\n usage: ssl_client2 param=<>...\n" \
70 "\n acceptable parameters:\n" \
71 " server_name=%%s default: localhost\n" \
72 " server_port=%%d default: 4433\n" \
73 " debug_level=%%d default: 0 (disabled)\n" \
74 " request_page=%%s default: \".\"\n" \
75 " crt_file=%%s default: \"\" (pre-loaded)\n" \
76 " key_file=%%s default: \"\" (pre-loaded)\n" \
Paul Bakker9caf2d22010-02-18 19:37:19 +000077 "\n"
78
79int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Paul Bakkerf80d4532010-03-16 21:16:04 +000081 int ret = 0, len, server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000082 unsigned char buf[1024];
83 havege_state hs;
84 ssl_context ssl;
85 ssl_session ssn;
86 x509_cert cacert;
87 x509_cert clicert;
88 rsa_context rsa;
Paul Bakker9caf2d22010-02-18 19:37:19 +000089 int i, j, n;
90 char *p, *q;
91
92 if( argc == 0 )
93 {
94 usage:
95 printf( USAGE );
96 goto exit;
97 }
98
99 opt.server_name = DFL_SERVER_NAME;
100 opt.server_port = DFL_SERVER_PORT;
101 opt.debug_level = DFL_DEBUG_LEVEL;
102 opt.request_page = DFL_REQUEST_PAGE;
Paul Bakker67968392010-07-18 08:28:20 +0000103 opt.crt_file = DFL_CRT_FILE;
104 opt.key_file = DFL_KEY_FILE;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000105
106 for( i = 1; i < argc; i++ )
107 {
108 n = strlen( argv[i] );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000109
110 for( j = 0; j < n; j++ )
111 {
112 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
113 argv[i][j] |= 0x20;
114 }
115
116 p = argv[i];
117 if( ( q = strchr( p, '=' ) ) == NULL )
118 goto usage;
119 *q++ = '\0';
120
121 if( strcmp( p, "server_name" ) == 0 )
122 opt.server_name = q;
123 else if( strcmp( p, "server_port" ) == 0 )
124 {
125 opt.server_port = atoi( q );
126 if( opt.server_port < 1 || opt.server_port > 65535 )
127 goto usage;
128 }
129 else if( strcmp( p, "debug_level" ) == 0 )
130 {
131 opt.debug_level = atoi( q );
132 if( opt.debug_level < 0 || opt.debug_level > 65535 )
133 goto usage;
134 }
135 else if( strcmp( p, "request_page" ) == 0 )
136 opt.request_page = q;
Paul Bakker67968392010-07-18 08:28:20 +0000137 else if( strcmp( p, "crt_file" ) == 0 )
138 opt.crt_file = q;
139 else if( strcmp( p, "key_file" ) == 0 )
140 opt.key_file = q;
Paul Bakker9caf2d22010-02-18 19:37:19 +0000141 else
142 goto usage;
143 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 /*
146 * 0. Initialize the RNG and the session data
147 */
148 havege_init( &hs );
149 memset( &ssn, 0, sizeof( ssl_session ) );
150
151 /*
152 * 1.1. Load the trusted CA
153 */
154 printf( "\n . Loading the CA root certificate ..." );
155 fflush( stdout );
156
157 memset( &cacert, 0, sizeof( x509_cert ) );
158
159 /*
160 * Alternatively, you may load the CA certificates from a .pem or
161 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
162 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000163 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
164 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 if( ret != 0 )
166 {
167 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
168 goto exit;
169 }
170
171 printf( " ok\n" );
172
173 /*
174 * 1.2. Load own certificate and private key
175 *
176 * (can be skipped if client authentication is not required)
177 */
178 printf( " . Loading the client cert. and key..." );
179 fflush( stdout );
180
181 memset( &clicert, 0, sizeof( x509_cert ) );
182
Paul Bakker67968392010-07-18 08:28:20 +0000183 if( strlen( opt.crt_file ) )
184 ret = x509parse_crtfile( &clicert, opt.crt_file );
185 else
186 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
187 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 if( ret != 0 )
189 {
190 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
191 goto exit;
192 }
193
Paul Bakker67968392010-07-18 08:28:20 +0000194 if( strlen( opt.key_file ) )
195 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
196 else
197 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
198 strlen( test_cli_key ), NULL, 0 );
199
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 if( ret != 0 )
201 {
202 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
203 goto exit;
204 }
205
206 printf( " ok\n" );
207
208 /*
209 * 2. Start the connection
210 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000211 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
212 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 fflush( stdout );
214
Paul Bakker9caf2d22010-02-18 19:37:19 +0000215 if( ( ret = net_connect( &server_fd, opt.server_name,
216 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 {
218 printf( " failed\n ! net_connect returned %d\n\n", ret );
219 goto exit;
220 }
221
222 printf( " ok\n" );
223
224 /*
225 * 3. Setup stuff
226 */
227 printf( " . Setting up the SSL/TLS structure..." );
228 fflush( stdout );
229
230 havege_init( &hs );
231
232 if( ( ret = ssl_init( &ssl ) ) != 0 )
233 {
234 printf( " failed\n ! ssl_init returned %d\n\n", ret );
235 goto exit;
236 }
237
238 printf( " ok\n" );
239
240 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
241 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
242
243 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000244 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 ssl_set_bio( &ssl, net_recv, &server_fd,
246 net_send, &server_fd );
247
248 ssl_set_ciphers( &ssl, ssl_default_ciphers );
249 ssl_set_session( &ssl, 1, 600, &ssn );
250
Paul Bakker9caf2d22010-02-18 19:37:19 +0000251 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 ssl_set_own_cert( &ssl, &clicert, &rsa );
253
Paul Bakker9caf2d22010-02-18 19:37:19 +0000254 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256 /*
257 * 4. Handshake
258 */
259 printf( " . Performing the SSL/TLS handshake..." );
260 fflush( stdout );
261
262 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
263 {
Paul Bakker40e46942009-01-03 21:51:57 +0000264 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 {
266 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
267 goto exit;
268 }
269 }
270
271 printf( " ok\n [ Cipher is %s ]\n",
272 ssl_get_cipher( &ssl ) );
273
274 /*
275 * 5. Verify the server certificate
276 */
277 printf( " . Verifying peer X.509 certificate..." );
278
279 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
280 {
281 printf( " failed\n" );
282
283 if( ( ret & BADCERT_EXPIRED ) != 0 )
284 printf( " ! server certificate has expired\n" );
285
286 if( ( ret & BADCERT_REVOKED ) != 0 )
287 printf( " ! server certificate has been revoked\n" );
288
289 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000290 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
293 printf( " ! self-signed or not signed by a trusted CA\n" );
294
295 printf( "\n" );
296 }
297 else
298 printf( " ok\n" );
299
300 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000301 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
302 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304 /*
305 * 6. Write the GET request
306 */
307 printf( " > Write to server:" );
308 fflush( stdout );
309
Paul Bakker9caf2d22010-02-18 19:37:19 +0000310 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
313 {
Paul Bakker40e46942009-01-03 21:51:57 +0000314 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 {
316 printf( " failed\n ! ssl_write returned %d\n\n", ret );
317 goto exit;
318 }
319 }
320
321 len = ret;
322 printf( " %d bytes written\n\n%s", len, (char *) buf );
323
324 /*
325 * 7. Read the HTTP response
326 */
327 printf( " < Read from server:" );
328 fflush( stdout );
329
330 do
331 {
332 len = sizeof( buf ) - 1;
333 memset( buf, 0, sizeof( buf ) );
334 ret = ssl_read( &ssl, buf, len );
335
Paul Bakker40e46942009-01-03 21:51:57 +0000336 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 continue;
338
Paul Bakker40e46942009-01-03 21:51:57 +0000339 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 break;
341
342 if( ret <= 0 )
343 {
344 printf( "failed\n ! ssl_read returned %d\n\n", ret );
345 break;
346 }
347
348 len = ret;
349 printf( " %d bytes read\n\n%s", len, (char *) buf );
350 }
351 while( 0 );
352
353 ssl_close_notify( &ssl );
354
355exit:
356
357 net_close( server_fd );
358 x509_free( &clicert );
359 x509_free( &cacert );
360 rsa_free( &rsa );
361 ssl_free( &ssl );
362
363 memset( &ssl, 0, sizeof( ssl ) );
364
365#ifdef WIN32
366 printf( " + Press Enter to exit this program.\n" );
367 fflush( stdout ); getchar();
368#endif
369
370 return( ret );
371}