blob: 123029d06f9fdba72cb1e7f36854aed00f04ac97 [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 Bakker0e6975b2009-02-10 22:19:10 +000040
Paul Bakker9caf2d22010-02-18 19:37:19 +000041#define GET_REQUEST "GET %s HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Paul Bakker9caf2d22010-02-18 19:37:19 +000043/*
44 * global options
45 */
46struct options
47{
48 char *server_name; /* hostname of the server (client only) */
49 int server_port; /* port on which the ssl service runs */
50 int debug_level; /* level of debugging */
51 char *request_page; /* page on server to request */
52} opt;
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkerff60ee62010-03-16 21:09:09 +000054void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000055{
Paul Bakker9caf2d22010-02-18 19:37:19 +000056 if( level < opt.debug_level )
Paul Bakker5121ce52009-01-03 21:22:43 +000057 {
58 fprintf( (FILE *) ctx, "%s", str );
59 fflush( (FILE *) ctx );
60 }
61}
62
Paul Bakker9caf2d22010-02-18 19:37:19 +000063#define USAGE \
64 "\n usage: ssl_client2 param=<>...\n" \
65 "\n acceptable parameters:\n" \
66 " server_name=%%s default: localhost\n" \
67 " server_port=%%d default: 4433\n" \
68 " debug_level=%%d default: 0 (disabled)\n" \
69 " request_page=%%s default: \".\"\n" \
70 "\n"
71
72int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
Paul Bakkerf80d4532010-03-16 21:16:04 +000074 int ret = 0, len, server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000075 unsigned char buf[1024];
76 havege_state hs;
77 ssl_context ssl;
78 ssl_session ssn;
79 x509_cert cacert;
80 x509_cert clicert;
81 rsa_context rsa;
Paul Bakker9caf2d22010-02-18 19:37:19 +000082 int i, j, n;
83 char *p, *q;
84
85 if( argc == 0 )
86 {
87 usage:
88 printf( USAGE );
89 goto exit;
90 }
91
92 opt.server_name = DFL_SERVER_NAME;
93 opt.server_port = DFL_SERVER_PORT;
94 opt.debug_level = DFL_DEBUG_LEVEL;
95 opt.request_page = DFL_REQUEST_PAGE;
96
97 for( i = 1; i < argc; i++ )
98 {
99 n = strlen( argv[i] );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000100
101 for( j = 0; j < n; j++ )
102 {
103 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
104 argv[i][j] |= 0x20;
105 }
106
107 p = argv[i];
108 if( ( q = strchr( p, '=' ) ) == NULL )
109 goto usage;
110 *q++ = '\0';
111
112 if( strcmp( p, "server_name" ) == 0 )
113 opt.server_name = q;
114 else if( strcmp( p, "server_port" ) == 0 )
115 {
116 opt.server_port = atoi( q );
117 if( opt.server_port < 1 || opt.server_port > 65535 )
118 goto usage;
119 }
120 else if( strcmp( p, "debug_level" ) == 0 )
121 {
122 opt.debug_level = atoi( q );
123 if( opt.debug_level < 0 || opt.debug_level > 65535 )
124 goto usage;
125 }
126 else if( strcmp( p, "request_page" ) == 0 )
127 opt.request_page = q;
128 else
129 goto usage;
130 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132 /*
133 * 0. Initialize the RNG and the session data
134 */
135 havege_init( &hs );
136 memset( &ssn, 0, sizeof( ssl_session ) );
137
138 /*
139 * 1.1. Load the trusted CA
140 */
141 printf( "\n . Loading the CA root certificate ..." );
142 fflush( stdout );
143
144 memset( &cacert, 0, sizeof( x509_cert ) );
145
146 /*
147 * Alternatively, you may load the CA certificates from a .pem or
148 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
149 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000150 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
151 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 if( ret != 0 )
153 {
154 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
155 goto exit;
156 }
157
158 printf( " ok\n" );
159
160 /*
161 * 1.2. Load own certificate and private key
162 *
163 * (can be skipped if client authentication is not required)
164 */
165 printf( " . Loading the client cert. and key..." );
166 fflush( stdout );
167
168 memset( &clicert, 0, sizeof( x509_cert ) );
169
170 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
171 strlen( test_cli_crt ) );
172 if( ret != 0 )
173 {
174 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
175 goto exit;
176 }
177
178 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
179 strlen( test_cli_key ), NULL, 0 );
180 if( ret != 0 )
181 {
182 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
183 goto exit;
184 }
185
186 printf( " ok\n" );
187
188 /*
189 * 2. Start the connection
190 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000191 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
192 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 fflush( stdout );
194
Paul Bakker9caf2d22010-02-18 19:37:19 +0000195 if( ( ret = net_connect( &server_fd, opt.server_name,
196 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 {
198 printf( " failed\n ! net_connect returned %d\n\n", ret );
199 goto exit;
200 }
201
202 printf( " ok\n" );
203
204 /*
205 * 3. Setup stuff
206 */
207 printf( " . Setting up the SSL/TLS structure..." );
208 fflush( stdout );
209
210 havege_init( &hs );
211
212 if( ( ret = ssl_init( &ssl ) ) != 0 )
213 {
214 printf( " failed\n ! ssl_init returned %d\n\n", ret );
215 goto exit;
216 }
217
218 printf( " ok\n" );
219
220 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
221 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
222
223 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000224 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 ssl_set_bio( &ssl, net_recv, &server_fd,
226 net_send, &server_fd );
227
228 ssl_set_ciphers( &ssl, ssl_default_ciphers );
229 ssl_set_session( &ssl, 1, 600, &ssn );
230
Paul Bakker9caf2d22010-02-18 19:37:19 +0000231 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 ssl_set_own_cert( &ssl, &clicert, &rsa );
233
Paul Bakker9caf2d22010-02-18 19:37:19 +0000234 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236 /*
237 * 4. Handshake
238 */
239 printf( " . Performing the SSL/TLS handshake..." );
240 fflush( stdout );
241
242 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
243 {
Paul Bakker40e46942009-01-03 21:51:57 +0000244 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 {
246 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
247 goto exit;
248 }
249 }
250
251 printf( " ok\n [ Cipher is %s ]\n",
252 ssl_get_cipher( &ssl ) );
253
254 /*
255 * 5. Verify the server certificate
256 */
257 printf( " . Verifying peer X.509 certificate..." );
258
259 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
260 {
261 printf( " failed\n" );
262
263 if( ( ret & BADCERT_EXPIRED ) != 0 )
264 printf( " ! server certificate has expired\n" );
265
266 if( ( ret & BADCERT_REVOKED ) != 0 )
267 printf( " ! server certificate has been revoked\n" );
268
269 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000270 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
273 printf( " ! self-signed or not signed by a trusted CA\n" );
274
275 printf( "\n" );
276 }
277 else
278 printf( " ok\n" );
279
280 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000281 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
282 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 /*
285 * 6. Write the GET request
286 */
287 printf( " > Write to server:" );
288 fflush( stdout );
289
Paul Bakker9caf2d22010-02-18 19:37:19 +0000290 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
293 {
Paul Bakker40e46942009-01-03 21:51:57 +0000294 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 {
296 printf( " failed\n ! ssl_write returned %d\n\n", ret );
297 goto exit;
298 }
299 }
300
301 len = ret;
302 printf( " %d bytes written\n\n%s", len, (char *) buf );
303
304 /*
305 * 7. Read the HTTP response
306 */
307 printf( " < Read from server:" );
308 fflush( stdout );
309
310 do
311 {
312 len = sizeof( buf ) - 1;
313 memset( buf, 0, sizeof( buf ) );
314 ret = ssl_read( &ssl, buf, len );
315
Paul Bakker40e46942009-01-03 21:51:57 +0000316 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 continue;
318
Paul Bakker40e46942009-01-03 21:51:57 +0000319 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 break;
321
322 if( ret <= 0 )
323 {
324 printf( "failed\n ! ssl_read returned %d\n\n", ret );
325 break;
326 }
327
328 len = ret;
329 printf( " %d bytes read\n\n%s", len, (char *) buf );
330 }
331 while( 0 );
332
333 ssl_close_notify( &ssl );
334
335exit:
336
337 net_close( server_fd );
338 x509_free( &clicert );
339 x509_free( &cacert );
340 rsa_free( &rsa );
341 ssl_free( &ssl );
342
343 memset( &ssl, 0, sizeof( ssl ) );
344
345#ifdef WIN32
346 printf( " + Press Enter to exit this program.\n" );
347 fflush( stdout ); getchar();
348#endif
349
350 return( ret );
351}