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