blob: 04613e7c15d9dfac1e2df161ce075858eefba6ca [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
28#include <string.h>
Paul Bakker9caf2d22010-02-18 19:37:19 +000029#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000030#include <stdio.h>
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/net.h"
33#include "polarssl/ssl.h"
34#include "polarssl/havege.h"
35#include "polarssl/certs.h"
36#include "polarssl/x509.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker9caf2d22010-02-18 19:37:19 +000038#define DFL_SERVER_NAME "localhost"
39#define DFL_SERVER_PORT 4433
40#define DFL_REQUEST_PAGE "/"
41#define DFL_DEBUG_LEVEL 0
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{
50 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} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkerff60ee62010-03-16 21:09:09 +000056void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
Paul Bakker9caf2d22010-02-18 19:37:19 +000058 if( level < opt.debug_level )
Paul Bakker5121ce52009-01-03 21:22:43 +000059 {
60 fprintf( (FILE *) ctx, "%s", str );
61 fflush( (FILE *) ctx );
62 }
63}
64
Paul Bakker9caf2d22010-02-18 19:37:19 +000065#define USAGE \
66 "\n usage: ssl_client2 param=<>...\n" \
67 "\n acceptable parameters:\n" \
68 " server_name=%%s default: localhost\n" \
69 " server_port=%%d default: 4433\n" \
70 " debug_level=%%d default: 0 (disabled)\n" \
71 " request_page=%%s default: \".\"\n" \
72 "\n"
73
74int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000075{
76 int ret, len, server_fd;
77 unsigned char buf[1024];
78 havege_state hs;
79 ssl_context ssl;
80 ssl_session ssn;
81 x509_cert cacert;
82 x509_cert clicert;
83 rsa_context rsa;
Paul Bakker9caf2d22010-02-18 19:37:19 +000084 int i, j, n;
85 char *p, *q;
86
87 if( argc == 0 )
88 {
89 usage:
90 printf( USAGE );
91 goto exit;
92 }
93
94 opt.server_name = DFL_SERVER_NAME;
95 opt.server_port = DFL_SERVER_PORT;
96 opt.debug_level = DFL_DEBUG_LEVEL;
97 opt.request_page = DFL_REQUEST_PAGE;
98
99 for( i = 1; i < argc; i++ )
100 {
101 n = strlen( argv[i] );
102 printf("a");
103
104 for( j = 0; j < n; j++ )
105 {
106 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
107 argv[i][j] |= 0x20;
108 }
109
110 p = argv[i];
111 if( ( q = strchr( p, '=' ) ) == NULL )
112 goto usage;
113 *q++ = '\0';
114
115 if( strcmp( p, "server_name" ) == 0 )
116 opt.server_name = q;
117 else if( strcmp( p, "server_port" ) == 0 )
118 {
119 opt.server_port = atoi( q );
120 if( opt.server_port < 1 || opt.server_port > 65535 )
121 goto usage;
122 }
123 else if( strcmp( p, "debug_level" ) == 0 )
124 {
125 opt.debug_level = atoi( q );
126 if( opt.debug_level < 0 || opt.debug_level > 65535 )
127 goto usage;
128 }
129 else if( strcmp( p, "request_page" ) == 0 )
130 opt.request_page = q;
131 else
132 goto usage;
133 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 /*
136 * 0. Initialize the RNG and the session data
137 */
138 havege_init( &hs );
139 memset( &ssn, 0, sizeof( ssl_session ) );
140
141 /*
142 * 1.1. Load the trusted CA
143 */
144 printf( "\n . Loading the CA root certificate ..." );
145 fflush( stdout );
146
147 memset( &cacert, 0, sizeof( x509_cert ) );
148
149 /*
150 * Alternatively, you may load the CA certificates from a .pem or
151 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
152 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000153 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
154 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 if( ret != 0 )
156 {
157 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
158 goto exit;
159 }
160
161 printf( " ok\n" );
162
163 /*
164 * 1.2. Load own certificate and private key
165 *
166 * (can be skipped if client authentication is not required)
167 */
168 printf( " . Loading the client cert. and key..." );
169 fflush( stdout );
170
171 memset( &clicert, 0, sizeof( x509_cert ) );
172
173 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
174 strlen( test_cli_crt ) );
175 if( ret != 0 )
176 {
177 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
178 goto exit;
179 }
180
181 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
182 strlen( test_cli_key ), NULL, 0 );
183 if( ret != 0 )
184 {
185 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
186 goto exit;
187 }
188
189 printf( " ok\n" );
190
191 /*
192 * 2. Start the connection
193 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000194 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
195 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 fflush( stdout );
197
Paul Bakker9caf2d22010-02-18 19:37:19 +0000198 if( ( ret = net_connect( &server_fd, opt.server_name,
199 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 {
201 printf( " failed\n ! net_connect returned %d\n\n", ret );
202 goto exit;
203 }
204
205 printf( " ok\n" );
206
207 /*
208 * 3. Setup stuff
209 */
210 printf( " . Setting up the SSL/TLS structure..." );
211 fflush( stdout );
212
213 havege_init( &hs );
214
215 if( ( ret = ssl_init( &ssl ) ) != 0 )
216 {
217 printf( " failed\n ! ssl_init returned %d\n\n", ret );
218 goto exit;
219 }
220
221 printf( " ok\n" );
222
223 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
224 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
225
226 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000227 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 ssl_set_bio( &ssl, net_recv, &server_fd,
229 net_send, &server_fd );
230
231 ssl_set_ciphers( &ssl, ssl_default_ciphers );
232 ssl_set_session( &ssl, 1, 600, &ssn );
233
Paul Bakker9caf2d22010-02-18 19:37:19 +0000234 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 ssl_set_own_cert( &ssl, &clicert, &rsa );
236
Paul Bakker9caf2d22010-02-18 19:37:19 +0000237 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 /*
240 * 4. Handshake
241 */
242 printf( " . Performing the SSL/TLS handshake..." );
243 fflush( stdout );
244
245 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
246 {
Paul Bakker40e46942009-01-03 21:51:57 +0000247 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 {
249 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
250 goto exit;
251 }
252 }
253
254 printf( " ok\n [ Cipher is %s ]\n",
255 ssl_get_cipher( &ssl ) );
256
257 /*
258 * 5. Verify the server certificate
259 */
260 printf( " . Verifying peer X.509 certificate..." );
261
262 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
263 {
264 printf( " failed\n" );
265
266 if( ( ret & BADCERT_EXPIRED ) != 0 )
267 printf( " ! server certificate has expired\n" );
268
269 if( ( ret & BADCERT_REVOKED ) != 0 )
270 printf( " ! server certificate has been revoked\n" );
271
272 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000273 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
276 printf( " ! self-signed or not signed by a trusted CA\n" );
277
278 printf( "\n" );
279 }
280 else
281 printf( " ok\n" );
282
283 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000284 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
285 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287 /*
288 * 6. Write the GET request
289 */
290 printf( " > Write to server:" );
291 fflush( stdout );
292
Paul Bakker9caf2d22010-02-18 19:37:19 +0000293 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
296 {
Paul Bakker40e46942009-01-03 21:51:57 +0000297 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 {
299 printf( " failed\n ! ssl_write returned %d\n\n", ret );
300 goto exit;
301 }
302 }
303
304 len = ret;
305 printf( " %d bytes written\n\n%s", len, (char *) buf );
306
307 /*
308 * 7. Read the HTTP response
309 */
310 printf( " < Read from server:" );
311 fflush( stdout );
312
313 do
314 {
315 len = sizeof( buf ) - 1;
316 memset( buf, 0, sizeof( buf ) );
317 ret = ssl_read( &ssl, buf, len );
318
Paul Bakker40e46942009-01-03 21:51:57 +0000319 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 continue;
321
Paul Bakker40e46942009-01-03 21:51:57 +0000322 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 break;
324
325 if( ret <= 0 )
326 {
327 printf( "failed\n ! ssl_read returned %d\n\n", ret );
328 break;
329 }
330
331 len = ret;
332 printf( " %d bytes read\n\n%s", len, (char *) buf );
333 }
334 while( 0 );
335
336 ssl_close_notify( &ssl );
337
338exit:
339
340 net_close( server_fd );
341 x509_free( &clicert );
342 x509_free( &cacert );
343 rsa_free( &rsa );
344 ssl_free( &ssl );
345
346 memset( &ssl, 0, sizeof( ssl ) );
347
348#ifdef WIN32
349 printf( " + Press Enter to exit this program.\n" );
350 fflush( stdout ); getchar();
351#endif
352
353 return( ret );
354}