Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Certificate reading application |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
| 32 | #define polarssl_printf printf |
| 33 | #define polarssl_fprintf fprintf |
| 34 | #define polarssl_malloc malloc |
| 35 | #define polarssl_free free |
| 36 | #endif |
| 37 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <stdio.h> |
| 41 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 42 | #include "polarssl/entropy.h" |
| 43 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 44 | #include "polarssl/net.h" |
| 45 | #include "polarssl/ssl.h" |
| 46 | #include "polarssl/x509.h" |
| 47 | |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 48 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 49 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
| 50 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 51 | !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \ |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 52 | !defined(POLARSSL_CTR_DRBG_C) |
| 53 | int main( int argc, char *argv[] ) |
| 54 | { |
| 55 | ((void) argc); |
| 56 | ((void) argv); |
| 57 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 58 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 59 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or " |
| 60 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 61 | "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_FS_IO and/or " |
Paul Bakker | 80d44fe | 2013-09-09 15:59:20 +0200 | [diff] [blame] | 62 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 63 | return( 0 ); |
| 64 | } |
| 65 | #else |
| 66 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 67 | #define MODE_NONE 0 |
| 68 | #define MODE_FILE 1 |
| 69 | #define MODE_SSL 2 |
| 70 | |
| 71 | #define DFL_MODE MODE_NONE |
| 72 | #define DFL_FILENAME "cert.crt" |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 73 | #define DFL_CA_FILE "" |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 74 | #define DFL_CRL_FILE "" |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 75 | #define DFL_CA_PATH "" |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 76 | #define DFL_SERVER_NAME "localhost" |
| 77 | #define DFL_SERVER_PORT 4433 |
| 78 | #define DFL_DEBUG_LEVEL 0 |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 79 | #define DFL_PERMISSIVE 0 |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * global options |
| 83 | */ |
| 84 | struct options |
| 85 | { |
| 86 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 87 | const char *filename; /* filename of the certificate file */ |
| 88 | const char *ca_file; /* the file with the CA certificate(s) */ |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 89 | const char *crl_file; /* the file with the CRL to use */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 90 | const char *ca_path; /* the path with the CA certificate(s) reside */ |
| 91 | const char *server_name; /* hostname of the server (client only) */ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 92 | int server_port; /* port on which the ssl service runs */ |
| 93 | int debug_level; /* level of debugging */ |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 94 | int permissive; /* permissive parsing */ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 95 | } opt; |
| 96 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 97 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 98 | { |
| 99 | if( level < opt.debug_level ) |
| 100 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 101 | polarssl_fprintf( (FILE *) ctx, "%s", str ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 102 | fflush( (FILE *) ctx ); |
| 103 | } |
| 104 | } |
| 105 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 106 | static int my_verify( void *data, x509_crt *crt, int depth, int *flags ) |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 107 | { |
| 108 | char buf[1024]; |
| 109 | ((void) data); |
| 110 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 111 | polarssl_printf( "\nVerify requested for (Depth %d):\n", depth ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 112 | x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 113 | polarssl_printf( "%s", buf ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 114 | |
| 115 | if( ( (*flags) & BADCERT_EXPIRED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 116 | polarssl_printf( " ! server certificate has expired\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 117 | |
| 118 | if( ( (*flags) & BADCERT_REVOKED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 119 | polarssl_printf( " ! server certificate has been revoked\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 120 | |
| 121 | if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 122 | polarssl_printf( " ! CN mismatch\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 123 | |
| 124 | if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 125 | polarssl_printf( " ! self-signed or not signed by a trusted CA\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 126 | |
| 127 | if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 128 | polarssl_printf( " ! CRL not trusted\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 129 | |
| 130 | if( ( (*flags) & BADCRL_EXPIRED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 131 | polarssl_printf( " ! CRL expired\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 132 | |
| 133 | if( ( (*flags) & BADCERT_OTHER ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 134 | polarssl_printf( " ! other (unknown) flag\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 135 | |
| 136 | if ( ( *flags ) == 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 137 | polarssl_printf( " This certificate has no flags\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 138 | |
| 139 | return( 0 ); |
| 140 | } |
| 141 | |
| 142 | #define USAGE_IO \ |
| 143 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 144 | " default: \"\" (none)\n" \ |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 145 | " crl_file=%%s The single CRL file you want to use\n" \ |
| 146 | " default: \"\" (none)\n" \ |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 147 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 148 | " default: \"\" (none) (overrides ca_file)\n" |
| 149 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 150 | #define USAGE \ |
| 151 | "\n usage: cert_app param=<>...\n" \ |
| 152 | "\n acceptable parameters:\n" \ |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 153 | " mode=file|ssl default: none\n" \ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 154 | " filename=%%s default: cert.crt\n" \ |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 155 | USAGE_IO \ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 156 | " server_name=%%s default: localhost\n" \ |
| 157 | " server_port=%%d default: 4433\n" \ |
| 158 | " debug_level=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 159 | " permissive=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 160 | "\n" |
| 161 | |
| 162 | int main( int argc, char *argv[] ) |
| 163 | { |
| 164 | int ret = 0, server_fd; |
| 165 | unsigned char buf[1024]; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 166 | entropy_context entropy; |
| 167 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 168 | ssl_context ssl; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 169 | x509_crt cacert; |
| 170 | x509_crt clicert; |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 171 | x509_crl cacrl; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 172 | pk_context pkey; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 173 | int i, j; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 174 | int flags, verify = 0; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 175 | char *p, *q; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 176 | const char *pers = "cert_app"; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 177 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Set to sane values |
| 180 | */ |
| 181 | server_fd = 0; |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 182 | x509_crt_init( &cacert ); |
| 183 | x509_crt_init( &clicert ); |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 184 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 185 | x509_crl_init( &cacrl ); |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 186 | #else |
| 187 | /* Zeroize structure as CRL parsing is not supported and we have to pass |
| 188 | it to the verify function */ |
| 189 | memset( &cacrl, 0, sizeof(x509_crl) ); |
| 190 | #endif |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 191 | pk_init( &pkey ); |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 192 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 193 | if( argc == 0 ) |
| 194 | { |
| 195 | usage: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 196 | polarssl_printf( USAGE ); |
Manuel Pégourié-Gonnard | 49aa99e | 2014-11-10 16:40:16 +0100 | [diff] [blame] | 197 | ret = 2; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 198 | goto exit; |
| 199 | } |
| 200 | |
| 201 | opt.mode = DFL_MODE; |
| 202 | opt.filename = DFL_FILENAME; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 203 | opt.ca_file = DFL_CA_FILE; |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 204 | opt.crl_file = DFL_CRL_FILE; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 205 | opt.ca_path = DFL_CA_PATH; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 206 | opt.server_name = DFL_SERVER_NAME; |
| 207 | opt.server_port = DFL_SERVER_PORT; |
| 208 | opt.debug_level = DFL_DEBUG_LEVEL; |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 209 | opt.permissive = DFL_PERMISSIVE; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 210 | |
| 211 | for( i = 1; i < argc; i++ ) |
| 212 | { |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 213 | p = argv[i]; |
| 214 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 215 | goto usage; |
| 216 | *q++ = '\0'; |
| 217 | |
Paul Bakker | ace0286 | 2013-09-16 21:40:34 +0200 | [diff] [blame] | 218 | for( j = 0; p + j < q; j++ ) |
| 219 | { |
| 220 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 221 | argv[i][j] |= 0x20; |
| 222 | } |
| 223 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 224 | if( strcmp( p, "mode" ) == 0 ) |
| 225 | { |
| 226 | if( strcmp( q, "file" ) == 0 ) |
| 227 | opt.mode = MODE_FILE; |
| 228 | else if( strcmp( q, "ssl" ) == 0 ) |
| 229 | opt.mode = MODE_SSL; |
| 230 | else |
| 231 | goto usage; |
| 232 | } |
| 233 | else if( strcmp( p, "filename" ) == 0 ) |
| 234 | opt.filename = q; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 235 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 236 | opt.ca_file = q; |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 237 | else if( strcmp( p, "crl_file" ) == 0 ) |
| 238 | opt.crl_file = q; |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 239 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 240 | opt.ca_path = q; |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 241 | else if( strcmp( p, "server_name" ) == 0 ) |
| 242 | opt.server_name = q; |
| 243 | else if( strcmp( p, "server_port" ) == 0 ) |
| 244 | { |
| 245 | opt.server_port = atoi( q ); |
| 246 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 247 | goto usage; |
| 248 | } |
| 249 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 250 | { |
| 251 | opt.debug_level = atoi( q ); |
| 252 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 253 | goto usage; |
| 254 | } |
Paul Bakker | 6c0ceb3 | 2011-12-04 12:24:18 +0000 | [diff] [blame] | 255 | else if( strcmp( p, "permissive" ) == 0 ) |
| 256 | { |
| 257 | opt.permissive = atoi( q ); |
| 258 | if( opt.permissive < 0 || opt.permissive > 1 ) |
| 259 | goto usage; |
| 260 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 261 | else |
| 262 | goto usage; |
| 263 | } |
| 264 | |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 265 | /* |
| 266 | * 1.1. Load the trusted CA |
| 267 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 268 | polarssl_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 269 | fflush( stdout ); |
| 270 | |
| 271 | if( strlen( opt.ca_path ) ) |
| 272 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 273 | ret = x509_crt_parse_path( &cacert, opt.ca_path ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 274 | verify = 1; |
| 275 | } |
| 276 | else if( strlen( opt.ca_file ) ) |
| 277 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 278 | ret = x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 279 | verify = 1; |
| 280 | } |
| 281 | |
| 282 | if( ret < 0 ) |
| 283 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 284 | polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 285 | goto exit; |
| 286 | } |
| 287 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 288 | polarssl_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 289 | |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 290 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 291 | if( strlen( opt.crl_file ) ) |
| 292 | { |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 293 | if( ( ret = x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 ) |
| 294 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 295 | polarssl_printf( " failed\n ! x509_crl_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 296 | goto exit; |
| 297 | } |
| 298 | |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 299 | verify = 1; |
| 300 | } |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 301 | #endif |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 302 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 303 | if( opt.mode == MODE_FILE ) |
| 304 | { |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 305 | x509_crt crt; |
| 306 | x509_crt *cur = &crt; |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 307 | x509_crt_init( &crt ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 310 | * 1.1. Load the certificate(s) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 311 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 312 | polarssl_printf( "\n . Loading the certificate(s) ..." ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 313 | fflush( stdout ); |
| 314 | |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 315 | ret = x509_crt_parse_file( &crt, opt.filename ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 316 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 317 | if( ret < 0 ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 318 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 319 | polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 320 | x509_crt_free( &crt ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 321 | goto exit; |
| 322 | } |
| 323 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 324 | if( opt.permissive == 0 && ret > 0 ) |
| 325 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 326 | polarssl_printf( " failed\n ! x509_crt_parse failed to parse %d certificates\n\n", ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 327 | x509_crt_free( &crt ); |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 328 | goto exit; |
| 329 | } |
| 330 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 331 | polarssl_printf( " ok\n" ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 332 | |
| 333 | /* |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 334 | * 1.2 Print the certificate(s) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 335 | */ |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 336 | while( cur != NULL ) |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 337 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 338 | polarssl_printf( " . Peer certificate information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 339 | ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 340 | cur ); |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 341 | if( ret == -1 ) |
| 342 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 343 | polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 344 | x509_crt_free( &crt ); |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 345 | goto exit; |
| 346 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 347 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 348 | polarssl_printf( "%s\n", buf ); |
Paul Bakker | 14cb63a | 2011-11-25 12:44:31 +0000 | [diff] [blame] | 349 | |
| 350 | cur = cur->next; |
| 351 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 352 | |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 353 | /* |
| 354 | * 1.3 Verify the certificate |
| 355 | */ |
| 356 | if( verify ) |
| 357 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 358 | polarssl_printf( " . Verifying X.509 certificate..." ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 359 | |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 360 | if( ( ret = x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 361 | my_verify, NULL ) ) != 0 ) |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 362 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 363 | polarssl_printf( " failed\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 364 | |
| 365 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 366 | polarssl_printf( " ! server certificate has expired\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 367 | |
| 368 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 369 | polarssl_printf( " ! server certificate has been revoked\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 370 | |
| 371 | if( ( ret & BADCERT_CN_MISMATCH ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 372 | polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 373 | |
| 374 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 375 | polarssl_printf( " ! self-signed or not signed by a trusted CA\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 376 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 377 | polarssl_printf( "\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 378 | } |
| 379 | else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 380 | polarssl_printf( " ok\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 383 | x509_crt_free( &crt ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 384 | } |
| 385 | else if( opt.mode == MODE_SSL ) |
| 386 | { |
| 387 | /* |
| 388 | * 1. Initialize the RNG and the session data |
| 389 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 390 | polarssl_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 391 | fflush( stdout ); |
| 392 | |
| 393 | entropy_init( &entropy ); |
| 394 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 395 | (const unsigned char *) pers, |
| 396 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 397 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 398 | polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 399 | goto exit; |
| 400 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 401 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 402 | polarssl_printf( " ok\n" ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 403 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 404 | /* |
| 405 | * 2. Start the connection |
| 406 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 407 | polarssl_printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name, |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 408 | opt.server_port ); |
| 409 | fflush( stdout ); |
| 410 | |
| 411 | if( ( ret = net_connect( &server_fd, opt.server_name, |
| 412 | opt.server_port ) ) != 0 ) |
| 413 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 414 | polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 415 | goto exit; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * 3. Setup stuff |
| 420 | */ |
| 421 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 422 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 423 | polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 424 | goto exit; |
| 425 | } |
| 426 | |
| 427 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
Paul Bakker | 777a575 | 2013-05-21 16:20:04 +0200 | [diff] [blame] | 428 | if( verify ) |
| 429 | { |
| 430 | ssl_set_authmode( &ssl, SSL_VERIFY_REQUIRED ); |
| 431 | ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name ); |
| 432 | ssl_set_verify( &ssl, my_verify, NULL ); |
| 433 | } |
| 434 | else |
| 435 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 436 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 437 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 438 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 439 | ssl_set_bio( &ssl, net_recv, &server_fd, |
| 440 | net_send, &server_fd ); |
| 441 | |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 442 | if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 ) |
| 443 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 444 | polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 445 | goto exit; |
| 446 | } |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 447 | |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 448 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 449 | if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 ) |
| 450 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 451 | polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 452 | goto exit; |
| 453 | } |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 454 | #endif |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * 4. Handshake |
| 458 | */ |
| 459 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 460 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 461 | 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] | 462 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 463 | polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 464 | ssl_free( &ssl ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 465 | goto exit; |
| 466 | } |
| 467 | } |
| 468 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 469 | polarssl_printf( " ok\n" ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * 5. Print the certificate |
| 473 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 474 | polarssl_printf( " . Peer certificate information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 475 | ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 476 | ssl.session->peer_cert ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 477 | if( ret == -1 ) |
| 478 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 479 | polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret ); |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 480 | ssl_free( &ssl ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 481 | goto exit; |
| 482 | } |
| 483 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 484 | polarssl_printf( "%s\n", buf ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 485 | |
| 486 | ssl_close_notify( &ssl ); |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 487 | ssl_free( &ssl ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 488 | } |
| 489 | else |
| 490 | goto usage; |
| 491 | |
| 492 | exit: |
| 493 | |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 494 | if( server_fd ) |
| 495 | net_close( server_fd ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 496 | x509_crt_free( &cacert ); |
| 497 | x509_crt_free( &clicert ); |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 498 | #if defined(POLARSSL_X509_CRL_PARSE_C) |
Paul Bakker | 1fd3253 | 2014-05-28 11:36:16 +0200 | [diff] [blame] | 499 | x509_crl_free( &cacrl ); |
Paul Bakker | 8880cb5 | 2014-06-12 23:22:26 +0200 | [diff] [blame] | 500 | #endif |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 501 | pk_free( &pkey ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 502 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 503 | entropy_free( &entropy ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 504 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 505 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 506 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 507 | fflush( stdout ); getchar(); |
| 508 | #endif |
| 509 | |
Manuel Pégourié-Gonnard | 49aa99e | 2014-11-10 16:40:16 +0100 | [diff] [blame] | 510 | if( ret < 0 ) |
| 511 | ret = 1; |
| 512 | |
Paul Bakker | 4fc4552 | 2010-03-18 20:11:58 +0000 | [diff] [blame] | 513 | return( ret ); |
| 514 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 515 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 516 | POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 517 | POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */ |