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