Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Certificate reading application |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, 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 | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 9 | * All rights reserved. |
| 10 | * |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +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> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdio.h> |
| 33 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 34 | #include "polarssl/config.h" |
| 35 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 36 | #include "polarssl/havege.h" |
| 37 | #include "polarssl/net.h" |
| 38 | #include "polarssl/ssl.h" |
| 39 | #include "polarssl/x509.h" |
| 40 | |
| 41 | #define MODE_NONE 0 |
| 42 | #define MODE_FILE 1 |
| 43 | #define MODE_SSL 2 |
| 44 | |
| 45 | #define DFL_MODE MODE_NONE |
| 46 | #define DFL_FILENAME "cert.crt" |
| 47 | #define DFL_SERVER_NAME "localhost" |
| 48 | #define DFL_SERVER_PORT 4433 |
| 49 | #define DFL_DEBUG_LEVEL 0 |
| 50 | |
| 51 | /* |
| 52 | * global options |
| 53 | */ |
| 54 | struct options |
| 55 | { |
| 56 | int mode; /* the mode to run the application in */ |
| 57 | char *filename; /* filename of the certificate file */ |
| 58 | char *server_name; /* hostname of the server (client only) */ |
| 59 | int server_port; /* port on which the ssl service runs */ |
| 60 | int debug_level; /* level of debugging */ |
| 61 | } opt; |
| 62 | |
| 63 | void my_debug( void *ctx, int level, const char *str ) |
| 64 | { |
| 65 | if( level < opt.debug_level ) |
| 66 | { |
| 67 | fprintf( (FILE *) ctx, "%s", str ); |
| 68 | fflush( (FILE *) ctx ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #define USAGE \ |
| 73 | "\n usage: cert_app param=<>...\n" \ |
| 74 | "\n acceptable parameters:\n" \ |
| 75 | " mode=file|ssl default: none\n" \ |
| 76 | " filename=%%s default: cert.crt\n" \ |
| 77 | " server_name=%%s default: localhost\n" \ |
| 78 | " server_port=%%d default: 4433\n" \ |
| 79 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 80 | "\n" |
| 81 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 82 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \ |
| 83 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
| 84 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \ |
| 85 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 86 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 87 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 88 | ((void) argc); |
| 89 | ((void) argv); |
| 90 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 91 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or " |
| 92 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or " |
| 93 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
| 94 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 95 | return( 0 ); |
| 96 | } |
| 97 | #else |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 98 | int main( int argc, char *argv[] ) |
| 99 | { |
| 100 | int ret = 0, server_fd; |
| 101 | unsigned char buf[1024]; |
| 102 | havege_state hs; |
| 103 | ssl_context ssl; |
| 104 | ssl_session ssn; |
| 105 | x509_cert clicert; |
| 106 | rsa_context rsa; |
| 107 | int i, j, n; |
| 108 | char *p, *q; |
| 109 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 110 | /* |
| 111 | * Set to sane values |
| 112 | */ |
| 113 | server_fd = 0; |
| 114 | memset( &ssl, 0, sizeof( ssl_context ) ); |
| 115 | memset( &ssn, 0, sizeof( ssl_session ) ); |
| 116 | memset( &clicert, 0, sizeof( x509_cert ) ); |
| 117 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 118 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 119 | if( argc == 0 ) |
| 120 | { |
| 121 | usage: |
| 122 | printf( USAGE ); |
| 123 | goto exit; |
| 124 | } |
| 125 | |
| 126 | opt.mode = DFL_MODE; |
| 127 | opt.filename = DFL_FILENAME; |
| 128 | opt.server_name = DFL_SERVER_NAME; |
| 129 | opt.server_port = DFL_SERVER_PORT; |
| 130 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 131 | |
| 132 | for( i = 1; i < argc; i++ ) |
| 133 | { |
| 134 | n = strlen( argv[i] ); |
| 135 | |
| 136 | for( j = 0; j < n; j++ ) |
| 137 | { |
| 138 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 139 | argv[i][j] |= 0x20; |
| 140 | } |
| 141 | |
| 142 | p = argv[i]; |
| 143 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 144 | goto usage; |
| 145 | *q++ = '\0'; |
| 146 | |
| 147 | if( strcmp( p, "mode" ) == 0 ) |
| 148 | { |
| 149 | if( strcmp( q, "file" ) == 0 ) |
| 150 | opt.mode = MODE_FILE; |
| 151 | else if( strcmp( q, "ssl" ) == 0 ) |
| 152 | opt.mode = MODE_SSL; |
| 153 | else |
| 154 | goto usage; |
| 155 | } |
| 156 | else if( strcmp( p, "filename" ) == 0 ) |
| 157 | opt.filename = q; |
| 158 | else if( strcmp( p, "server_name" ) == 0 ) |
| 159 | opt.server_name = q; |
| 160 | else if( strcmp( p, "server_port" ) == 0 ) |
| 161 | { |
| 162 | opt.server_port = atoi( q ); |
| 163 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 164 | goto usage; |
| 165 | } |
| 166 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 167 | { |
| 168 | opt.debug_level = atoi( q ); |
| 169 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 170 | goto usage; |
| 171 | } |
| 172 | else |
| 173 | goto usage; |
| 174 | } |
| 175 | |
| 176 | if( opt.mode == MODE_FILE ) |
| 177 | { |
| 178 | x509_cert crt; |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 179 | x509_cert *cur = &crt; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 180 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 181 | |
| 182 | /* |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 183 | * 1.1. Load the certificate(s) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 184 | */ |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 185 | printf( "\n . Loading the certificate(s) ..." ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 186 | fflush( stdout ); |
| 187 | |
| 188 | ret = x509parse_crtfile( &crt, opt.filename ); |
| 189 | |
| 190 | if( ret != 0 ) |
| 191 | { |
| 192 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 193 | x509_free( &crt ); |
| 194 | goto exit; |
| 195 | } |
| 196 | |
| 197 | printf( " ok\n" ); |
| 198 | |
| 199 | /* |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 200 | * 1.2 Print the certificate(s) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 201 | */ |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 202 | while( cur != NULL ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 203 | { |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 204 | printf( " . Peer certificate information ...\n" ); |
| 205 | ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", &crt ); |
| 206 | if( ret == -1 ) |
| 207 | { |
| 208 | printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret ); |
| 209 | x509_free( &crt ); |
| 210 | goto exit; |
| 211 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 212 | |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame^] | 213 | printf( "%s\n", buf ); |
| 214 | |
| 215 | cur = cur->next; |
| 216 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 217 | |
| 218 | x509_free( &crt ); |
| 219 | } |
| 220 | else if( opt.mode == MODE_SSL ) |
| 221 | { |
| 222 | /* |
| 223 | * 1. Initialize the RNG and the session data |
| 224 | */ |
| 225 | havege_init( &hs ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * 2. Start the connection |
| 229 | */ |
| 230 | printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name, |
| 231 | opt.server_port ); |
| 232 | fflush( stdout ); |
| 233 | |
| 234 | if( ( ret = net_connect( &server_fd, opt.server_name, |
| 235 | opt.server_port ) ) != 0 ) |
| 236 | { |
| 237 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * 3. Setup stuff |
| 243 | */ |
| 244 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 245 | { |
| 246 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
| 247 | goto exit; |
| 248 | } |
| 249 | |
| 250 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
| 251 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 252 | |
| 253 | ssl_set_rng( &ssl, havege_rand, &hs ); |
| 254 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 255 | ssl_set_bio( &ssl, net_recv, &server_fd, |
| 256 | net_send, &server_fd ); |
| 257 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 258 | ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 259 | ssl_set_session( &ssl, 1, 600, &ssn ); |
| 260 | |
| 261 | ssl_set_own_cert( &ssl, &clicert, &rsa ); |
| 262 | |
| 263 | ssl_set_hostname( &ssl, opt.server_name ); |
| 264 | |
| 265 | /* |
| 266 | * 4. Handshake |
| 267 | */ |
| 268 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 269 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 270 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 271 | { |
| 272 | printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
| 273 | goto exit; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | printf( " ok\n" ); |
| 278 | |
| 279 | /* |
| 280 | * 5. Print the certificate |
| 281 | */ |
| 282 | printf( " . Peer certificate information ...\n" ); |
| 283 | ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert ); |
| 284 | if( ret == -1 ) |
| 285 | { |
| 286 | printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret ); |
| 287 | goto exit; |
| 288 | } |
| 289 | |
| 290 | printf( "%s\n", buf ); |
| 291 | |
| 292 | ssl_close_notify( &ssl ); |
| 293 | } |
| 294 | else |
| 295 | goto usage; |
| 296 | |
| 297 | exit: |
| 298 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 299 | if( server_fd ) |
| 300 | net_close( server_fd ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 301 | x509_free( &clicert ); |
| 302 | rsa_free( &rsa ); |
| 303 | ssl_free( &ssl ); |
| 304 | |
| 305 | memset( &ssl, 0, sizeof( ssl ) ); |
| 306 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 307 | #if defined(_WIN32) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 308 | printf( " + Press Enter to exit this program.\n" ); |
| 309 | fflush( stdout ); getchar(); |
| 310 | #endif |
| 311 | |
| 312 | return( ret ); |
| 313 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 314 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C && |
| 315 | POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
| 316 | POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */ |