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