Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Certificate reading application |
| 3 | * |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, 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 | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 36 | #include "polarssl/entropy.h" |
| 37 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 38 | #include "polarssl/net.h" |
| 39 | #include "polarssl/ssl.h" |
| 40 | #include "polarssl/x509.h" |
| 41 | |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 42 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 43 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
| 44 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \ |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 46 | !defined(POLARSSL_CTR_DRBG_C) |
| 47 | int main( int argc, char *argv[] ) |
| 48 | { |
| 49 | ((void) argc); |
| 50 | ((void) argv); |
| 51 | |
| 52 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
| 53 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or " |
| 54 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 55 | "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_FS_IO and/or " |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 56 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 57 | return( 0 ); |
| 58 | } |
| 59 | #else |
| 60 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 61 | #define MODE_NONE 0 |
| 62 | #define MODE_FILE 1 |
| 63 | #define MODE_SSL 2 |
| 64 | |
| 65 | #define DFL_MODE MODE_NONE |
| 66 | #define DFL_FILENAME "cert.crt" |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 67 | #define DFL_CA_FILE "" |
| 68 | #define DFL_CA_PATH "" |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 69 | #define DFL_SERVER_NAME "localhost" |
| 70 | #define DFL_SERVER_PORT 4433 |
| 71 | #define DFL_DEBUG_LEVEL 0 |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 72 | #define DFL_PERMISSIVE 0 |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * global options |
| 76 | */ |
| 77 | struct options |
| 78 | { |
| 79 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 80 | const char *filename; /* filename of the certificate file */ |
| 81 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 82 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
| 83 | const char *server_name; /* hostname of the server (client only) */ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 84 | int server_port; /* port on which the ssl service runs */ |
| 85 | int debug_level; /* level of debugging */ |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 86 | int permissive; /* permissive parsing */ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 87 | } opt; |
| 88 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 89 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 90 | { |
| 91 | if( level < opt.debug_level ) |
| 92 | { |
| 93 | fprintf( (FILE *) ctx, "%s", str ); |
| 94 | fflush( (FILE *) ctx ); |
| 95 | } |
| 96 | } |
| 97 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 98 | static int my_verify( void *data, x509_cert *crt, int depth, int *flags ) |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 99 | { |
| 100 | char buf[1024]; |
| 101 | ((void) data); |
| 102 | |
| 103 | printf( "\nVerify requested for (Depth %d):\n", depth ); |
| 104 | x509parse_cert_info( buf, sizeof( buf ) - 1, "", crt ); |
| 105 | printf( "%s", buf ); |
| 106 | |
| 107 | if( ( (*flags) & BADCERT_EXPIRED ) != 0 ) |
| 108 | printf( " ! server certificate has expired\n" ); |
| 109 | |
| 110 | if( ( (*flags) & BADCERT_REVOKED ) != 0 ) |
| 111 | printf( " ! server certificate has been revoked\n" ); |
| 112 | |
| 113 | if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 ) |
| 114 | printf( " ! CN mismatch\n" ); |
| 115 | |
| 116 | if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 ) |
| 117 | printf( " ! self-signed or not signed by a trusted CA\n" ); |
| 118 | |
| 119 | if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 ) |
| 120 | printf( " ! CRL not trusted\n" ); |
| 121 | |
| 122 | if( ( (*flags) & BADCRL_EXPIRED ) != 0 ) |
| 123 | printf( " ! CRL expired\n" ); |
| 124 | |
| 125 | if( ( (*flags) & BADCERT_OTHER ) != 0 ) |
| 126 | printf( " ! other (unknown) flag\n" ); |
| 127 | |
| 128 | if ( ( *flags ) == 0 ) |
| 129 | printf( " This certificate has no flags\n" ); |
| 130 | |
| 131 | return( 0 ); |
| 132 | } |
| 133 | |
| 134 | #define USAGE_IO \ |
| 135 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 136 | " default: \"\" (none)\n" \ |
| 137 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 138 | " default: \"\" (none) (overrides ca_file)\n" |
| 139 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 140 | #define USAGE \ |
| 141 | "\n usage: cert_app param=<>...\n" \ |
| 142 | "\n acceptable parameters:\n" \ |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 143 | " mode=file|ssl default: none\n" \ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 144 | " filename=%%s default: cert.crt\n" \ |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 145 | USAGE_IO \ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 146 | " server_name=%%s default: localhost\n" \ |
| 147 | " server_port=%%d default: 4433\n" \ |
| 148 | " debug_level=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 149 | " permissive=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 150 | "\n" |
| 151 | |
| 152 | int main( int argc, char *argv[] ) |
| 153 | { |
| 154 | int ret = 0, server_fd; |
| 155 | unsigned char buf[1024]; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 156 | entropy_context entropy; |
| 157 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 158 | ssl_context ssl; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 159 | x509_cert cacert; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 160 | x509_cert clicert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 161 | pk_context pkey; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 162 | int i, j; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 163 | int flags, verify = 0; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 164 | char *p, *q; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 165 | const char *pers = "cert_app"; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 166 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 167 | /* |
| 168 | * Set to sane values |
| 169 | */ |
| 170 | server_fd = 0; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 171 | memset( &cacert, 0, sizeof( x509_cert ) ); |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 172 | memset( &clicert, 0, sizeof( x509_cert ) ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 173 | pk_init( &pkey ); |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 174 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 175 | if( argc == 0 ) |
| 176 | { |
| 177 | usage: |
| 178 | printf( USAGE ); |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | opt.mode = DFL_MODE; |
| 183 | opt.filename = DFL_FILENAME; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 184 | opt.ca_file = DFL_CA_FILE; |
| 185 | opt.ca_path = DFL_CA_PATH; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 186 | opt.server_name = DFL_SERVER_NAME; |
| 187 | opt.server_port = DFL_SERVER_PORT; |
| 188 | opt.debug_level = DFL_DEBUG_LEVEL; |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 189 | opt.permissive = DFL_PERMISSIVE; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 190 | |
| 191 | for( i = 1; i < argc; i++ ) |
| 192 | { |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 193 | p = argv[i]; |
| 194 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 195 | goto usage; |
| 196 | *q++ = '\0'; |
| 197 | |
Paul Bakker | ace0286 | 2013-09-16 21:40:34 +0200 | [diff] [blame] | 198 | for( j = 0; p + j < q; j++ ) |
| 199 | { |
| 200 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 201 | argv[i][j] |= 0x20; |
| 202 | } |
| 203 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 204 | if( strcmp( p, "mode" ) == 0 ) |
| 205 | { |
| 206 | if( strcmp( q, "file" ) == 0 ) |
| 207 | opt.mode = MODE_FILE; |
| 208 | else if( strcmp( q, "ssl" ) == 0 ) |
| 209 | opt.mode = MODE_SSL; |
| 210 | else |
| 211 | goto usage; |
| 212 | } |
| 213 | else if( strcmp( p, "filename" ) == 0 ) |
| 214 | opt.filename = q; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 215 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 216 | opt.ca_file = q; |
| 217 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 218 | opt.ca_path = q; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 219 | else if( strcmp( p, "server_name" ) == 0 ) |
| 220 | opt.server_name = q; |
| 221 | else if( strcmp( p, "server_port" ) == 0 ) |
| 222 | { |
| 223 | opt.server_port = atoi( q ); |
| 224 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 225 | goto usage; |
| 226 | } |
| 227 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 228 | { |
| 229 | opt.debug_level = atoi( q ); |
| 230 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 231 | goto usage; |
| 232 | } |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 233 | else if( strcmp( p, "permissive" ) == 0 ) |
| 234 | { |
| 235 | opt.permissive = atoi( q ); |
| 236 | if( opt.permissive < 0 || opt.permissive > 1 ) |
| 237 | goto usage; |
| 238 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 239 | else |
| 240 | goto usage; |
| 241 | } |
| 242 | |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 243 | /* |
| 244 | * 1.1. Load the trusted CA |
| 245 | */ |
| 246 | printf( " . Loading the CA root certificate ..." ); |
| 247 | fflush( stdout ); |
| 248 | |
| 249 | if( strlen( opt.ca_path ) ) |
| 250 | { |
| 251 | ret = x509parse_crtpath( &cacert, opt.ca_path ); |
| 252 | verify = 1; |
| 253 | } |
| 254 | else if( strlen( opt.ca_file ) ) |
| 255 | { |
| 256 | ret = x509parse_crtfile( &cacert, opt.ca_file ); |
| 257 | verify = 1; |
| 258 | } |
| 259 | |
| 260 | if( ret < 0 ) |
| 261 | { |
| 262 | printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret ); |
| 263 | goto exit; |
| 264 | } |
| 265 | |
| 266 | printf( " ok (%d skipped)\n", ret ); |
| 267 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 268 | if( opt.mode == MODE_FILE ) |
| 269 | { |
| 270 | x509_cert crt; |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 271 | x509_cert *cur = &crt; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 272 | memset( &crt, 0, sizeof( x509_cert ) ); |
| 273 | |
| 274 | /* |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 275 | * 1.1. Load the certificate(s) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 276 | */ |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 277 | printf( "\n . Loading the certificate(s) ..." ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 278 | fflush( stdout ); |
| 279 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 280 | ret = x509parse_crtfile( &crt, opt.filename ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 281 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 282 | if( ret < 0 ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 283 | { |
| 284 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 285 | x509_crt_free( &crt ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 286 | goto exit; |
| 287 | } |
| 288 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 289 | if( opt.permissive == 0 && ret > 0 ) |
| 290 | { |
| 291 | printf( " failed\n ! x509parse_crt failed to parse %d certificates\n\n", ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 292 | x509_crt_free( &crt ); |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 293 | goto exit; |
| 294 | } |
| 295 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 296 | printf( " ok\n" ); |
| 297 | |
| 298 | /* |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 299 | * 1.2 Print the certificate(s) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 300 | */ |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 301 | while( cur != NULL ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 302 | { |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 303 | printf( " . Peer certificate information ...\n" ); |
Paul Bakker | 5c356d6 | 2011-11-25 13:17:45 +0000 | [diff] [blame] | 304 | ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", cur ); |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 305 | if( ret == -1 ) |
| 306 | { |
| 307 | printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 308 | x509_crt_free( &crt ); |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 309 | goto exit; |
| 310 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 311 | |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 312 | printf( "%s\n", buf ); |
| 313 | |
| 314 | cur = cur->next; |
| 315 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 316 | |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 317 | /* |
| 318 | * 1.3 Verify the certificate |
| 319 | */ |
| 320 | if( verify ) |
| 321 | { |
| 322 | printf( " . Verifying X.509 certificate..." ); |
| 323 | |
| 324 | if( ( ret = x509parse_verify( &crt, &cacert, NULL, NULL, &flags, |
| 325 | my_verify, NULL ) ) != 0 ) |
| 326 | { |
| 327 | printf( " failed\n" ); |
| 328 | |
| 329 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
| 330 | printf( " ! server certificate has expired\n" ); |
| 331 | |
| 332 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
| 333 | printf( " ! server certificate has been revoked\n" ); |
| 334 | |
| 335 | if( ( ret & BADCERT_CN_MISMATCH ) != 0 ) |
| 336 | printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name ); |
| 337 | |
| 338 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
| 339 | printf( " ! self-signed or not signed by a trusted CA\n" ); |
| 340 | |
| 341 | printf( "\n" ); |
| 342 | } |
| 343 | else |
| 344 | printf( " ok\n" ); |
| 345 | } |
| 346 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 347 | x509_crt_free( &crt ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 348 | } |
| 349 | else if( opt.mode == MODE_SSL ) |
| 350 | { |
| 351 | /* |
| 352 | * 1. Initialize the RNG and the session data |
| 353 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 354 | printf( "\n . Seeding the random number generator..." ); |
| 355 | fflush( stdout ); |
| 356 | |
| 357 | entropy_init( &entropy ); |
| 358 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 359 | (const unsigned char *) pers, |
| 360 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 361 | { |
| 362 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 363 | goto exit; |
| 364 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 365 | |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 366 | printf( " ok\n" ); |
| 367 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 368 | /* |
| 369 | * 2. Start the connection |
| 370 | */ |
| 371 | printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name, |
| 372 | opt.server_port ); |
| 373 | fflush( stdout ); |
| 374 | |
| 375 | if( ( ret = net_connect( &server_fd, opt.server_name, |
| 376 | opt.server_port ) ) != 0 ) |
| 377 | { |
| 378 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 379 | goto exit; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * 3. Setup stuff |
| 384 | */ |
| 385 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 386 | { |
| 387 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
| 388 | goto exit; |
| 389 | } |
| 390 | |
| 391 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 392 | if( verify ) |
| 393 | { |
| 394 | ssl_set_authmode( &ssl, SSL_VERIFY_REQUIRED ); |
| 395 | ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name ); |
| 396 | ssl_set_verify( &ssl, my_verify, NULL ); |
| 397 | } |
| 398 | else |
| 399 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 400 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 401 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 402 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 403 | ssl_set_bio( &ssl, net_recv, &server_fd, |
| 404 | net_send, &server_fd ); |
| 405 | |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 406 | ssl_set_own_cert( &ssl, &clicert, &pkey ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 407 | |
| 408 | ssl_set_hostname( &ssl, opt.server_name ); |
| 409 | |
| 410 | /* |
| 411 | * 4. Handshake |
| 412 | */ |
| 413 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 414 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 415 | 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] | 416 | { |
| 417 | printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 418 | ssl_free( &ssl ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 419 | goto exit; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | printf( " ok\n" ); |
| 424 | |
| 425 | /* |
| 426 | * 5. Print the certificate |
| 427 | */ |
| 428 | printf( " . Peer certificate information ...\n" ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 429 | ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 430 | ssl.session->peer_cert ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 431 | if( ret == -1 ) |
| 432 | { |
| 433 | printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret ); |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 434 | ssl_free( &ssl ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 435 | goto exit; |
| 436 | } |
| 437 | |
| 438 | printf( "%s\n", buf ); |
| 439 | |
| 440 | ssl_close_notify( &ssl ); |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 441 | ssl_free( &ssl ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 442 | } |
| 443 | else |
| 444 | goto usage; |
| 445 | |
| 446 | exit: |
| 447 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 448 | if( server_fd ) |
| 449 | net_close( server_fd ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 450 | x509_crt_free( &cacert ); |
| 451 | x509_crt_free( &clicert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 452 | pk_free( &pkey ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 453 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 454 | #if defined(_WIN32) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 455 | printf( " + Press Enter to exit this program.\n" ); |
| 456 | fflush( stdout ); getchar(); |
| 457 | #endif |
| 458 | |
| 459 | return( ret ); |
| 460 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 461 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 462 | POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 463 | POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */ |