blob: c86962975e3ca9d1781627858e90fcbf696b3bba [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{
Paul Bakkerf80d4532010-03-16 21:16:04 +000076 int ret = 0, len, server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000077 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] );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000102
103 for( j = 0; j < n; j++ )
104 {
105 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
106 argv[i][j] |= 0x20;
107 }
108
109 p = argv[i];
110 if( ( q = strchr( p, '=' ) ) == NULL )
111 goto usage;
112 *q++ = '\0';
113
114 if( strcmp( p, "server_name" ) == 0 )
115 opt.server_name = q;
116 else if( strcmp( p, "server_port" ) == 0 )
117 {
118 opt.server_port = atoi( q );
119 if( opt.server_port < 1 || opt.server_port > 65535 )
120 goto usage;
121 }
122 else if( strcmp( p, "debug_level" ) == 0 )
123 {
124 opt.debug_level = atoi( q );
125 if( opt.debug_level < 0 || opt.debug_level > 65535 )
126 goto usage;
127 }
128 else if( strcmp( p, "request_page" ) == 0 )
129 opt.request_page = q;
130 else
131 goto usage;
132 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134 /*
135 * 0. Initialize the RNG and the session data
136 */
137 havege_init( &hs );
138 memset( &ssn, 0, sizeof( ssl_session ) );
139
140 /*
141 * 1.1. Load the trusted CA
142 */
143 printf( "\n . Loading the CA root certificate ..." );
144 fflush( stdout );
145
146 memset( &cacert, 0, sizeof( x509_cert ) );
147
148 /*
149 * Alternatively, you may load the CA certificates from a .pem or
150 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
151 */
Paul Bakker0e6975b2009-02-10 22:19:10 +0000152 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
153 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 if( ret != 0 )
155 {
156 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
157 goto exit;
158 }
159
160 printf( " ok\n" );
161
162 /*
163 * 1.2. Load own certificate and private key
164 *
165 * (can be skipped if client authentication is not required)
166 */
167 printf( " . Loading the client cert. and key..." );
168 fflush( stdout );
169
170 memset( &clicert, 0, sizeof( x509_cert ) );
171
172 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
173 strlen( test_cli_crt ) );
174 if( ret != 0 )
175 {
176 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
177 goto exit;
178 }
179
180 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
181 strlen( test_cli_key ), NULL, 0 );
182 if( ret != 0 )
183 {
184 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
185 goto exit;
186 }
187
188 printf( " ok\n" );
189
190 /*
191 * 2. Start the connection
192 */
Paul Bakker9caf2d22010-02-18 19:37:19 +0000193 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
194 opt.server_port );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 fflush( stdout );
196
Paul Bakker9caf2d22010-02-18 19:37:19 +0000197 if( ( ret = net_connect( &server_fd, opt.server_name,
198 opt.server_port ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 {
200 printf( " failed\n ! net_connect returned %d\n\n", ret );
201 goto exit;
202 }
203
204 printf( " ok\n" );
205
206 /*
207 * 3. Setup stuff
208 */
209 printf( " . Setting up the SSL/TLS structure..." );
210 fflush( stdout );
211
212 havege_init( &hs );
213
214 if( ( ret = ssl_init( &ssl ) ) != 0 )
215 {
216 printf( " failed\n ! ssl_init returned %d\n\n", ret );
217 goto exit;
218 }
219
220 printf( " ok\n" );
221
222 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
223 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
224
225 ssl_set_rng( &ssl, havege_rand, &hs );
Paul Bakker9caf2d22010-02-18 19:37:19 +0000226 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 ssl_set_bio( &ssl, net_recv, &server_fd,
228 net_send, &server_fd );
229
230 ssl_set_ciphers( &ssl, ssl_default_ciphers );
231 ssl_set_session( &ssl, 1, 600, &ssn );
232
Paul Bakker9caf2d22010-02-18 19:37:19 +0000233 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 ssl_set_own_cert( &ssl, &clicert, &rsa );
235
Paul Bakker9caf2d22010-02-18 19:37:19 +0000236 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 /*
239 * 4. Handshake
240 */
241 printf( " . Performing the SSL/TLS handshake..." );
242 fflush( stdout );
243
244 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
245 {
Paul Bakker40e46942009-01-03 21:51:57 +0000246 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 {
248 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
249 goto exit;
250 }
251 }
252
253 printf( " ok\n [ Cipher is %s ]\n",
254 ssl_get_cipher( &ssl ) );
255
256 /*
257 * 5. Verify the server certificate
258 */
259 printf( " . Verifying peer X.509 certificate..." );
260
261 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
262 {
263 printf( " failed\n" );
264
265 if( ( ret & BADCERT_EXPIRED ) != 0 )
266 printf( " ! server certificate has expired\n" );
267
268 if( ( ret & BADCERT_REVOKED ) != 0 )
269 printf( " ! server certificate has been revoked\n" );
270
271 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Paul Bakker9caf2d22010-02-18 19:37:19 +0000272 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
275 printf( " ! self-signed or not signed by a trusted CA\n" );
276
277 printf( "\n" );
278 }
279 else
280 printf( " ok\n" );
281
282 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000283 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
284 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286 /*
287 * 6. Write the GET request
288 */
289 printf( " > Write to server:" );
290 fflush( stdout );
291
Paul Bakker9caf2d22010-02-18 19:37:19 +0000292 len = sprintf( (char *) buf, GET_REQUEST, opt.request_page );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
295 {
Paul Bakker40e46942009-01-03 21:51:57 +0000296 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 {
298 printf( " failed\n ! ssl_write returned %d\n\n", ret );
299 goto exit;
300 }
301 }
302
303 len = ret;
304 printf( " %d bytes written\n\n%s", len, (char *) buf );
305
306 /*
307 * 7. Read the HTTP response
308 */
309 printf( " < Read from server:" );
310 fflush( stdout );
311
312 do
313 {
314 len = sizeof( buf ) - 1;
315 memset( buf, 0, sizeof( buf ) );
316 ret = ssl_read( &ssl, buf, len );
317
Paul Bakker40e46942009-01-03 21:51:57 +0000318 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 continue;
320
Paul Bakker40e46942009-01-03 21:51:57 +0000321 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 break;
323
324 if( ret <= 0 )
325 {
326 printf( "failed\n ! ssl_read returned %d\n\n", ret );
327 break;
328 }
329
330 len = ret;
331 printf( " %d bytes read\n\n%s", len, (char *) buf );
332 }
333 while( 0 );
334
335 ssl_close_notify( &ssl );
336
337exit:
338
339 net_close( server_fd );
340 x509_free( &clicert );
341 x509_free( &cacert );
342 rsa_free( &rsa );
343 ssl_free( &ssl );
344
345 memset( &ssl, 0, sizeof( ssl ) );
346
347#ifdef WIN32
348 printf( " + Press Enter to exit this program.\n" );
349 fflush( stdout ); getchar();
350#endif
351
352 return( ret );
353}