blob: c97fa04d700a06c3f4e4ef06ea2af6dc0ce95c78 [file] [log] [blame]
Paul Bakker4fc45522010-03-18 20:11:58 +00001/*
2 * Certificate reading application
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker4fc45522010-03-18 20:11:58 +00008 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker4fc45522010-03-18 20:11:58 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000037#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) && \
Rich Evans18b78c72015-02-11 14:06:19 +000041 defined(POLARSSL_CTR_DRBG_C)
Paul Bakker508ad5a2011-12-04 17:09:26 +000042#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000044#include "polarssl/net.h"
45#include "polarssl/ssl.h"
46#include "polarssl/x509.h"
47
Rich Evans18b78c72015-02-11 14:06:19 +000048#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#endif
52
53#define MODE_NONE 0
54#define MODE_FILE 1
55#define MODE_SSL 2
56
57#define DFL_MODE MODE_NONE
58#define DFL_FILENAME "cert.crt"
59#define DFL_CA_FILE ""
60#define DFL_CRL_FILE ""
61#define DFL_CA_PATH ""
62#define DFL_SERVER_NAME "localhost"
63#define DFL_SERVER_PORT 4433
64#define DFL_DEBUG_LEVEL 0
65#define DFL_PERMISSIVE 0
66
Rich Evans85b05ec2015-02-12 11:37:29 +000067#define USAGE_IO \
68 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
69 " default: \"\" (none)\n" \
70 " crl_file=%%s The single CRL file you want to use\n" \
71 " default: \"\" (none)\n" \
72 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
73 " default: \"\" (none) (overrides ca_file)\n"
74
75#define USAGE \
76 "\n usage: cert_app param=<>...\n" \
77 "\n acceptable parameters:\n" \
78 " mode=file|ssl default: none\n" \
79 " filename=%%s default: cert.crt\n" \
80 USAGE_IO \
81 " server_name=%%s default: localhost\n" \
82 " server_port=%%d default: 4433\n" \
83 " debug_level=%%d default: 0 (disabled)\n" \
84 " permissive=%%d default: 0 (disabled)\n" \
85 "\n"
86
Paul Bakker80d44fe2013-09-09 15:59:20 +020087#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
88 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
89 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Paul Bakker80d44fe2013-09-09 15:59:20 +020091 !defined(POLARSSL_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000092int main( void )
Paul Bakker80d44fe2013-09-09 15:59:20 +020093{
Rich Evansf90016a2015-01-19 14:26:37 +000094 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker80d44fe2013-09-09 15:59:20 +020095 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
96 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097 "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker80d44fe2013-09-09 15:59:20 +020098 "POLARSSL_CTR_DRBG_C not defined.\n");
99 return( 0 );
100}
101#else
Paul Bakker4fc45522010-03-18 20:11:58 +0000102/*
103 * global options
104 */
105struct options
106{
107 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200108 const char *filename; /* filename of the certificate file */
109 const char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1fd32532014-05-28 11:36:16 +0200110 const char *crl_file; /* the file with the CRL to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200111 const char *ca_path; /* the path with the CA certificate(s) reside */
112 const char *server_name; /* hostname of the server (client only) */
Paul Bakker4fc45522010-03-18 20:11:58 +0000113 int server_port; /* port on which the ssl service runs */
114 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000115 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +0000116} opt;
117
Paul Bakker3c5ef712013-06-25 16:37:45 +0200118static void my_debug( void *ctx, int level, const char *str )
Paul Bakker4fc45522010-03-18 20:11:58 +0000119{
120 if( level < opt.debug_level )
121 {
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker4fc45522010-03-18 20:11:58 +0000123 fflush( (FILE *) ctx );
124 }
125}
126
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200127static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker777a5752013-05-21 16:20:04 +0200128{
129 char buf[1024];
130 ((void) data);
131
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200133 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( "%s", buf );
Paul Bakker777a5752013-05-21 16:20:04 +0200135
136 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000137 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200138
139 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200141
142 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( " ! CN mismatch\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200144
145 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200147
148 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000149 polarssl_printf( " ! CRL not trusted\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200150
151 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_printf( " ! CRL expired\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200153
154 if( ( (*flags) & BADCERT_OTHER ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000155 polarssl_printf( " ! other (unknown) flag\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200156
157 if ( ( *flags ) == 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " This certificate has no flags\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200159
160 return( 0 );
161}
162
Paul Bakker4fc45522010-03-18 20:11:58 +0000163int main( int argc, char *argv[] )
164{
165 int ret = 0, server_fd;
166 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000167 entropy_context entropy;
168 ctr_drbg_context ctr_drbg;
Paul Bakker4fc45522010-03-18 20:11:58 +0000169 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200170 x509_crt cacert;
171 x509_crt clicert;
Paul Bakker1fd32532014-05-28 11:36:16 +0200172 x509_crl cacrl;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200173 pk_context pkey;
Paul Bakker36713e82013-09-17 13:25:29 +0200174 int i, j;
Paul Bakker777a5752013-05-21 16:20:04 +0200175 int flags, verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000176 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200177 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000178
Paul Bakker1a207ec2011-02-06 13:22:40 +0000179 /*
180 * Set to sane values
181 */
182 server_fd = 0;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200183 x509_crt_init( &cacert );
184 x509_crt_init( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200185#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200186 x509_crl_init( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200187#else
188 /* Zeroize structure as CRL parsing is not supported and we have to pass
189 it to the verify function */
190 memset( &cacrl, 0, sizeof(x509_crl) );
191#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200192 pk_init( &pkey );
Paul Bakker1a207ec2011-02-06 13:22:40 +0000193
Paul Bakker4fc45522010-03-18 20:11:58 +0000194 if( argc == 0 )
195 {
196 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000197 polarssl_printf( USAGE );
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100198 ret = 2;
Paul Bakker4fc45522010-03-18 20:11:58 +0000199 goto exit;
200 }
201
202 opt.mode = DFL_MODE;
203 opt.filename = DFL_FILENAME;
Paul Bakker777a5752013-05-21 16:20:04 +0200204 opt.ca_file = DFL_CA_FILE;
Paul Bakker1fd32532014-05-28 11:36:16 +0200205 opt.crl_file = DFL_CRL_FILE;
Paul Bakker777a5752013-05-21 16:20:04 +0200206 opt.ca_path = DFL_CA_PATH;
Paul Bakker4fc45522010-03-18 20:11:58 +0000207 opt.server_name = DFL_SERVER_NAME;
208 opt.server_port = DFL_SERVER_PORT;
209 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000210 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000211
212 for( i = 1; i < argc; i++ )
213 {
Paul Bakker4fc45522010-03-18 20:11:58 +0000214 p = argv[i];
215 if( ( q = strchr( p, '=' ) ) == NULL )
216 goto usage;
217 *q++ = '\0';
218
Paul Bakkerace02862013-09-16 21:40:34 +0200219 for( j = 0; p + j < q; j++ )
220 {
221 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
222 argv[i][j] |= 0x20;
223 }
224
Paul Bakker4fc45522010-03-18 20:11:58 +0000225 if( strcmp( p, "mode" ) == 0 )
226 {
227 if( strcmp( q, "file" ) == 0 )
228 opt.mode = MODE_FILE;
229 else if( strcmp( q, "ssl" ) == 0 )
230 opt.mode = MODE_SSL;
231 else
232 goto usage;
233 }
234 else if( strcmp( p, "filename" ) == 0 )
235 opt.filename = q;
Paul Bakker777a5752013-05-21 16:20:04 +0200236 else if( strcmp( p, "ca_file" ) == 0 )
237 opt.ca_file = q;
Paul Bakker1fd32532014-05-28 11:36:16 +0200238 else if( strcmp( p, "crl_file" ) == 0 )
239 opt.crl_file = q;
Paul Bakker777a5752013-05-21 16:20:04 +0200240 else if( strcmp( p, "ca_path" ) == 0 )
241 opt.ca_path = q;
Paul Bakker4fc45522010-03-18 20:11:58 +0000242 else if( strcmp( p, "server_name" ) == 0 )
243 opt.server_name = q;
244 else if( strcmp( p, "server_port" ) == 0 )
245 {
246 opt.server_port = atoi( q );
247 if( opt.server_port < 1 || opt.server_port > 65535 )
248 goto usage;
249 }
250 else if( strcmp( p, "debug_level" ) == 0 )
251 {
252 opt.debug_level = atoi( q );
253 if( opt.debug_level < 0 || opt.debug_level > 65535 )
254 goto usage;
255 }
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000256 else if( strcmp( p, "permissive" ) == 0 )
257 {
258 opt.permissive = atoi( q );
259 if( opt.permissive < 0 || opt.permissive > 1 )
260 goto usage;
261 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000262 else
263 goto usage;
264 }
265
Paul Bakker777a5752013-05-21 16:20:04 +0200266 /*
267 * 1.1. Load the trusted CA
268 */
Rich Evansf90016a2015-01-19 14:26:37 +0000269 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker777a5752013-05-21 16:20:04 +0200270 fflush( stdout );
271
272 if( strlen( opt.ca_path ) )
273 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200274 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker777a5752013-05-21 16:20:04 +0200275 verify = 1;
276 }
277 else if( strlen( opt.ca_file ) )
278 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200279 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker777a5752013-05-21 16:20:04 +0200280 verify = 1;
281 }
282
283 if( ret < 0 )
284 {
Rich Evansf90016a2015-01-19 14:26:37 +0000285 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200286 goto exit;
287 }
288
Rich Evansf90016a2015-01-19 14:26:37 +0000289 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200290
Paul Bakker8880cb52014-06-12 23:22:26 +0200291#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200292 if( strlen( opt.crl_file ) )
293 {
Paul Bakker8880cb52014-06-12 23:22:26 +0200294 if( ( ret = x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 )
295 {
Rich Evansf90016a2015-01-19 14:26:37 +0000296 polarssl_printf( " failed\n ! x509_crl_parse returned -0x%x\n\n", -ret );
Paul Bakker8880cb52014-06-12 23:22:26 +0200297 goto exit;
298 }
299
Paul Bakker1fd32532014-05-28 11:36:16 +0200300 verify = 1;
301 }
Paul Bakker8880cb52014-06-12 23:22:26 +0200302#endif
Paul Bakker1fd32532014-05-28 11:36:16 +0200303
Paul Bakker4fc45522010-03-18 20:11:58 +0000304 if( opt.mode == MODE_FILE )
305 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200306 x509_crt crt;
307 x509_crt *cur = &crt;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200308 x509_crt_init( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000309
310 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000311 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000312 */
Rich Evansf90016a2015-01-19 14:26:37 +0000313 polarssl_printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000314 fflush( stdout );
315
Paul Bakkerddf26b42013-09-18 13:46:23 +0200316 ret = x509_crt_parse_file( &crt, opt.filename );
Paul Bakker4fc45522010-03-18 20:11:58 +0000317
Paul Bakker69e095c2011-12-10 21:55:01 +0000318 if( ret < 0 )
Paul Bakker4fc45522010-03-18 20:11:58 +0000319 {
Rich Evansf90016a2015-01-19 14:26:37 +0000320 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000322 goto exit;
323 }
324
Paul Bakker69e095c2011-12-10 21:55:01 +0000325 if( opt.permissive == 0 && ret > 0 )
326 {
Rich Evansf90016a2015-01-19 14:26:37 +0000327 polarssl_printf( " failed\n ! x509_crt_parse failed to parse %d certificates\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 x509_crt_free( &crt );
Paul Bakker69e095c2011-12-10 21:55:01 +0000329 goto exit;
330 }
331
Rich Evansf90016a2015-01-19 14:26:37 +0000332 polarssl_printf( " ok\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000333
334 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000335 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000336 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000337 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000338 {
Rich Evansf90016a2015-01-19 14:26:37 +0000339 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200340 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
341 cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000342 if( ret == -1 )
343 {
Rich Evansf90016a2015-01-19 14:26:37 +0000344 polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 x509_crt_free( &crt );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000346 goto exit;
347 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000348
Rich Evansf90016a2015-01-19 14:26:37 +0000349 polarssl_printf( "%s\n", buf );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000350
351 cur = cur->next;
352 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000353
Manuel Pégourié-Gonnard671589d2015-02-16 09:24:08 +0000354 ret = 0;
355
Paul Bakker777a5752013-05-21 16:20:04 +0200356 /*
357 * 1.3 Verify the certificate
358 */
359 if( verify )
360 {
Rich Evansf90016a2015-01-19 14:26:37 +0000361 polarssl_printf( " . Verifying X.509 certificate..." );
Paul Bakker777a5752013-05-21 16:20:04 +0200362
Paul Bakker1fd32532014-05-28 11:36:16 +0200363 if( ( ret = x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200364 my_verify, NULL ) ) != 0 )
Paul Bakker777a5752013-05-21 16:20:04 +0200365 {
Rich Evansf90016a2015-01-19 14:26:37 +0000366 polarssl_printf( " failed\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200367
368 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000369 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200370
371 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000372 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200373
374 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000375 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
Paul Bakker777a5752013-05-21 16:20:04 +0200376
377 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000378 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200379
Rich Evansf90016a2015-01-19 14:26:37 +0000380 polarssl_printf( "\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200381 }
382 else
Rich Evansf90016a2015-01-19 14:26:37 +0000383 polarssl_printf( " ok\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200384 }
385
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000387 }
388 else if( opt.mode == MODE_SSL )
389 {
390 /*
391 * 1. Initialize the RNG and the session data
392 */
Rich Evansf90016a2015-01-19 14:26:37 +0000393 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000394 fflush( stdout );
395
396 entropy_init( &entropy );
397 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200398 (const unsigned char *) pers,
399 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000400 {
Rich Evansf90016a2015-01-19 14:26:37 +0000401 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000402 goto exit;
403 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000404
Rich Evansf90016a2015-01-19 14:26:37 +0000405 polarssl_printf( " ok\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200406
Paul Bakker4fc45522010-03-18 20:11:58 +0000407 /*
408 * 2. Start the connection
409 */
Rich Evansf90016a2015-01-19 14:26:37 +0000410 polarssl_printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
Paul Bakker4fc45522010-03-18 20:11:58 +0000411 opt.server_port );
412 fflush( stdout );
413
414 if( ( ret = net_connect( &server_fd, opt.server_name,
415 opt.server_port ) ) != 0 )
416 {
Rich Evansf90016a2015-01-19 14:26:37 +0000417 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker4fc45522010-03-18 20:11:58 +0000418 goto exit;
419 }
420
421 /*
422 * 3. Setup stuff
423 */
424 if( ( ret = ssl_init( &ssl ) ) != 0 )
425 {
Rich Evansf90016a2015-01-19 14:26:37 +0000426 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker4fc45522010-03-18 20:11:58 +0000427 goto exit;
428 }
429
430 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker777a5752013-05-21 16:20:04 +0200431 if( verify )
432 {
433 ssl_set_authmode( &ssl, SSL_VERIFY_REQUIRED );
434 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
435 ssl_set_verify( &ssl, my_verify, NULL );
436 }
437 else
438 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
Paul Bakker4fc45522010-03-18 20:11:58 +0000439
Paul Bakker508ad5a2011-12-04 17:09:26 +0000440 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker4fc45522010-03-18 20:11:58 +0000441 ssl_set_dbg( &ssl, my_debug, stdout );
442 ssl_set_bio( &ssl, net_recv, &server_fd,
443 net_send, &server_fd );
444
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200445 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
446 {
Rich Evansf90016a2015-01-19 14:26:37 +0000447 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200448 goto exit;
449 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000450
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200451#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200452 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
453 {
Rich Evansf90016a2015-01-19 14:26:37 +0000454 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200455 goto exit;
456 }
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200457#endif
Paul Bakker4fc45522010-03-18 20:11:58 +0000458
459 /*
460 * 4. Handshake
461 */
462 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
463 {
Paul Bakker831a7552011-05-18 13:32:51 +0000464 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000465 {
Rich Evansf90016a2015-01-19 14:26:37 +0000466 polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000467 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000468 goto exit;
469 }
470 }
471
Rich Evansf90016a2015-01-19 14:26:37 +0000472 polarssl_printf( " ok\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000473
474 /*
475 * 5. Print the certificate
476 */
Rich Evansf90016a2015-01-19 14:26:37 +0000477 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200478 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
479 ssl.session->peer_cert );
Paul Bakker4fc45522010-03-18 20:11:58 +0000480 if( ret == -1 )
481 {
Rich Evansf90016a2015-01-19 14:26:37 +0000482 polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000483 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000484 goto exit;
485 }
486
Rich Evansf90016a2015-01-19 14:26:37 +0000487 polarssl_printf( "%s\n", buf );
Paul Bakker4fc45522010-03-18 20:11:58 +0000488
489 ssl_close_notify( &ssl );
Paul Bakker9a736322012-11-14 12:39:52 +0000490 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000491 }
492 else
493 goto usage;
494
495exit:
496
Paul Bakker1a207ec2011-02-06 13:22:40 +0000497 if( server_fd )
498 net_close( server_fd );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 x509_crt_free( &cacert );
500 x509_crt_free( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200501#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200502 x509_crl_free( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200503#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200504 pk_free( &pkey );
Paul Bakkera317a982014-06-18 16:44:11 +0200505 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200506 entropy_free( &entropy );
Paul Bakker4fc45522010-03-18 20:11:58 +0000507
Paul Bakkercce9d772011-11-18 14:26:47 +0000508#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000509 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000510 fflush( stdout ); getchar();
511#endif
512
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100513 if( ret < 0 )
514 ret = 1;
515
Paul Bakker4fc45522010-03-18 20:11:58 +0000516 return( ret );
517}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000518#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000519 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200520 POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */