blob: 3a6ae87e4fc444e14017c6765b55afc0ee9733fd [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard8d649c62015-03-31 15:10:03 +020037#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)
42int 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 Bakker508ad5a2011-12-04 17:09:26 +000053#include "polarssl/entropy.h"
54#include "polarssl/ctr_drbg.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000055#include "polarssl/net.h"
56#include "polarssl/ssl.h"
57#include "polarssl/x509.h"
58
Rich Evans18b78c72015-02-11 14:06:19 +000059#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000062
63#define MODE_NONE 0
64#define MODE_FILE 1
65#define MODE_SSL 2
66
67#define DFL_MODE MODE_NONE
68#define DFL_FILENAME "cert.crt"
69#define DFL_CA_FILE ""
70#define DFL_CRL_FILE ""
71#define DFL_CA_PATH ""
72#define DFL_SERVER_NAME "localhost"
73#define DFL_SERVER_PORT 4433
74#define DFL_DEBUG_LEVEL 0
75#define DFL_PERMISSIVE 0
76
Rich Evans85b05ec2015-02-12 11:37:29 +000077#define USAGE_IO \
78 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
79 " default: \"\" (none)\n" \
80 " crl_file=%%s The single CRL file you want to use\n" \
81 " default: \"\" (none)\n" \
82 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
83 " default: \"\" (none) (overrides ca_file)\n"
84
85#define USAGE \
86 "\n usage: cert_app param=<>...\n" \
87 "\n acceptable parameters:\n" \
88 " mode=file|ssl default: none\n" \
89 " filename=%%s default: cert.crt\n" \
90 USAGE_IO \
91 " server_name=%%s default: localhost\n" \
92 " server_port=%%d default: 4433\n" \
93 " debug_level=%%d default: 0 (disabled)\n" \
94 " permissive=%%d default: 0 (disabled)\n" \
95 "\n"
96
Paul Bakker4fc45522010-03-18 20:11:58 +000097/*
98 * global options
99 */
100struct options
101{
102 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200103 const char *filename; /* filename of the certificate file */
104 const char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1fd32532014-05-28 11:36:16 +0200105 const char *crl_file; /* the file with the CRL to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200106 const char *ca_path; /* the path with the CA certificate(s) reside */
107 const char *server_name; /* hostname of the server (client only) */
Paul Bakker4fc45522010-03-18 20:11:58 +0000108 int server_port; /* port on which the ssl service runs */
109 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000110 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +0000111} opt;
112
Paul Bakker3c5ef712013-06-25 16:37:45 +0200113static void my_debug( void *ctx, int level, const char *str )
Paul Bakker4fc45522010-03-18 20:11:58 +0000114{
115 if( level < opt.debug_level )
116 {
Rich Evansf90016a2015-01-19 14:26:37 +0000117 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker4fc45522010-03-18 20:11:58 +0000118 fflush( (FILE *) ctx );
119 }
120}
121
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200122static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker777a5752013-05-21 16:20:04 +0200123{
124 char buf[1024];
125 ((void) data);
126
Rich Evansf90016a2015-01-19 14:26:37 +0000127 polarssl_printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200128 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( "%s", buf );
Paul Bakker777a5752013-05-21 16:20:04 +0200130
Paul Bakker777a5752013-05-21 16:20:04 +0200131 if ( ( *flags ) == 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200133 else
134 {
135 x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
136 polarssl_printf( "%s\n", buf );
137 }
Paul Bakker777a5752013-05-21 16:20:04 +0200138
139 return( 0 );
140}
141
Paul Bakker4fc45522010-03-18 20:11:58 +0000142int main( int argc, char *argv[] )
143{
144 int ret = 0, server_fd;
145 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000146 entropy_context entropy;
147 ctr_drbg_context ctr_drbg;
Paul Bakker4fc45522010-03-18 20:11:58 +0000148 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200149 x509_crt cacert;
150 x509_crt clicert;
Paul Bakker1fd32532014-05-28 11:36:16 +0200151 x509_crl cacrl;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200152 pk_context pkey;
Paul Bakker36713e82013-09-17 13:25:29 +0200153 int i, j;
Paul Bakker777a5752013-05-21 16:20:04 +0200154 int flags, verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000155 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200156 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000157
Paul Bakker1a207ec2011-02-06 13:22:40 +0000158 /*
159 * Set to sane values
160 */
161 server_fd = 0;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200162 x509_crt_init( &cacert );
163 x509_crt_init( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200164#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200165 x509_crl_init( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200166#else
167 /* Zeroize structure as CRL parsing is not supported and we have to pass
168 it to the verify function */
169 memset( &cacrl, 0, sizeof(x509_crl) );
170#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200171 pk_init( &pkey );
Paul Bakker1a207ec2011-02-06 13:22:40 +0000172
Paul Bakker4fc45522010-03-18 20:11:58 +0000173 if( argc == 0 )
174 {
175 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000176 polarssl_printf( USAGE );
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100177 ret = 2;
Paul Bakker4fc45522010-03-18 20:11:58 +0000178 goto exit;
179 }
180
181 opt.mode = DFL_MODE;
182 opt.filename = DFL_FILENAME;
Paul Bakker777a5752013-05-21 16:20:04 +0200183 opt.ca_file = DFL_CA_FILE;
Paul Bakker1fd32532014-05-28 11:36:16 +0200184 opt.crl_file = DFL_CRL_FILE;
Paul Bakker777a5752013-05-21 16:20:04 +0200185 opt.ca_path = DFL_CA_PATH;
Paul Bakker4fc45522010-03-18 20:11:58 +0000186 opt.server_name = DFL_SERVER_NAME;
187 opt.server_port = DFL_SERVER_PORT;
188 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000189 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000190
191 for( i = 1; i < argc; i++ )
192 {
Paul Bakker4fc45522010-03-18 20:11:58 +0000193 p = argv[i];
194 if( ( q = strchr( p, '=' ) ) == NULL )
195 goto usage;
196 *q++ = '\0';
197
Paul Bakkerace02862013-09-16 21:40:34 +0200198 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 Bakker4fc45522010-03-18 20:11:58 +0000204 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 Bakker777a5752013-05-21 16:20:04 +0200215 else if( strcmp( p, "ca_file" ) == 0 )
216 opt.ca_file = q;
Paul Bakker1fd32532014-05-28 11:36:16 +0200217 else if( strcmp( p, "crl_file" ) == 0 )
218 opt.crl_file = q;
Paul Bakker777a5752013-05-21 16:20:04 +0200219 else if( strcmp( p, "ca_path" ) == 0 )
220 opt.ca_path = q;
Paul Bakker4fc45522010-03-18 20:11:58 +0000221 else if( strcmp( p, "server_name" ) == 0 )
222 opt.server_name = q;
223 else if( strcmp( p, "server_port" ) == 0 )
224 {
225 opt.server_port = atoi( q );
226 if( opt.server_port < 1 || opt.server_port > 65535 )
227 goto usage;
228 }
229 else if( strcmp( p, "debug_level" ) == 0 )
230 {
231 opt.debug_level = atoi( q );
232 if( opt.debug_level < 0 || opt.debug_level > 65535 )
233 goto usage;
234 }
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000235 else if( strcmp( p, "permissive" ) == 0 )
236 {
237 opt.permissive = atoi( q );
238 if( opt.permissive < 0 || opt.permissive > 1 )
239 goto usage;
240 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000241 else
242 goto usage;
243 }
244
Paul Bakker777a5752013-05-21 16:20:04 +0200245 /*
246 * 1.1. Load the trusted CA
247 */
Rich Evansf90016a2015-01-19 14:26:37 +0000248 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker777a5752013-05-21 16:20:04 +0200249 fflush( stdout );
250
251 if( strlen( opt.ca_path ) )
252 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200253 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker777a5752013-05-21 16:20:04 +0200254 verify = 1;
255 }
256 else if( strlen( opt.ca_file ) )
257 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200258 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker777a5752013-05-21 16:20:04 +0200259 verify = 1;
260 }
261
262 if( ret < 0 )
263 {
Rich Evansf90016a2015-01-19 14:26:37 +0000264 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200265 goto exit;
266 }
267
Rich Evansf90016a2015-01-19 14:26:37 +0000268 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200269
Paul Bakker8880cb52014-06-12 23:22:26 +0200270#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200271 if( strlen( opt.crl_file ) )
272 {
Paul Bakker8880cb52014-06-12 23:22:26 +0200273 if( ( ret = x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 )
274 {
Rich Evansf90016a2015-01-19 14:26:37 +0000275 polarssl_printf( " failed\n ! x509_crl_parse returned -0x%x\n\n", -ret );
Paul Bakker8880cb52014-06-12 23:22:26 +0200276 goto exit;
277 }
278
Paul Bakker1fd32532014-05-28 11:36:16 +0200279 verify = 1;
280 }
Paul Bakker8880cb52014-06-12 23:22:26 +0200281#endif
Paul Bakker1fd32532014-05-28 11:36:16 +0200282
Paul Bakker4fc45522010-03-18 20:11:58 +0000283 if( opt.mode == MODE_FILE )
284 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200285 x509_crt crt;
286 x509_crt *cur = &crt;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200287 x509_crt_init( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000288
289 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000290 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000291 */
Rich Evansf90016a2015-01-19 14:26:37 +0000292 polarssl_printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000293 fflush( stdout );
294
Paul Bakkerddf26b42013-09-18 13:46:23 +0200295 ret = x509_crt_parse_file( &crt, opt.filename );
Paul Bakker4fc45522010-03-18 20:11:58 +0000296
Paul Bakker69e095c2011-12-10 21:55:01 +0000297 if( ret < 0 )
Paul Bakker4fc45522010-03-18 20:11:58 +0000298 {
Rich Evansf90016a2015-01-19 14:26:37 +0000299 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000301 goto exit;
302 }
303
Paul Bakker69e095c2011-12-10 21:55:01 +0000304 if( opt.permissive == 0 && ret > 0 )
305 {
Rich Evansf90016a2015-01-19 14:26:37 +0000306 polarssl_printf( " failed\n ! x509_crt_parse failed to parse %d certificates\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307 x509_crt_free( &crt );
Paul Bakker69e095c2011-12-10 21:55:01 +0000308 goto exit;
309 }
310
Rich Evansf90016a2015-01-19 14:26:37 +0000311 polarssl_printf( " ok\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000312
313 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000314 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000315 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000316 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000317 {
Rich Evansf90016a2015-01-19 14:26:37 +0000318 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200319 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
320 cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000321 if( ret == -1 )
322 {
Rich Evansf90016a2015-01-19 14:26:37 +0000323 polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324 x509_crt_free( &crt );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000325 goto exit;
326 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000327
Rich Evansf90016a2015-01-19 14:26:37 +0000328 polarssl_printf( "%s\n", buf );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000329
330 cur = cur->next;
331 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000332
Manuel Pégourié-Gonnard671589d2015-02-16 09:24:08 +0000333 ret = 0;
334
Paul Bakker777a5752013-05-21 16:20:04 +0200335 /*
336 * 1.3 Verify the certificate
337 */
338 if( verify )
339 {
Rich Evansf90016a2015-01-19 14:26:37 +0000340 polarssl_printf( " . Verifying X.509 certificate..." );
Paul Bakker777a5752013-05-21 16:20:04 +0200341
Paul Bakker1fd32532014-05-28 11:36:16 +0200342 if( ( ret = x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200343 my_verify, NULL ) ) != 0 )
Paul Bakker777a5752013-05-21 16:20:04 +0200344 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200345 char vrfy_buf[512];
346
Rich Evansf90016a2015-01-19 14:26:37 +0000347 polarssl_printf( " failed\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200348
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200349 x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200350
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200351 polarssl_printf( "%s\n", vrfy_buf );
Paul Bakker777a5752013-05-21 16:20:04 +0200352 }
353 else
Rich Evansf90016a2015-01-19 14:26:37 +0000354 polarssl_printf( " ok\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200355 }
356
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000358 }
359 else if( opt.mode == MODE_SSL )
360 {
361 /*
362 * 1. Initialize the RNG and the session data
363 */
Rich Evansf90016a2015-01-19 14:26:37 +0000364 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000365 fflush( stdout );
366
367 entropy_init( &entropy );
368 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200369 (const unsigned char *) pers,
370 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000371 {
Rich Evansf90016a2015-01-19 14:26:37 +0000372 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000373 goto exit;
374 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000375
Rich Evansf90016a2015-01-19 14:26:37 +0000376 polarssl_printf( " ok\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200377
Paul Bakker4fc45522010-03-18 20:11:58 +0000378 /*
379 * 2. Start the connection
380 */
Rich Evansf90016a2015-01-19 14:26:37 +0000381 polarssl_printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
Paul Bakker4fc45522010-03-18 20:11:58 +0000382 opt.server_port );
383 fflush( stdout );
384
385 if( ( ret = net_connect( &server_fd, opt.server_name,
386 opt.server_port ) ) != 0 )
387 {
Rich Evansf90016a2015-01-19 14:26:37 +0000388 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker4fc45522010-03-18 20:11:58 +0000389 goto exit;
390 }
391
392 /*
393 * 3. Setup stuff
394 */
395 if( ( ret = ssl_init( &ssl ) ) != 0 )
396 {
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker4fc45522010-03-18 20:11:58 +0000398 goto exit;
399 }
400
401 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker777a5752013-05-21 16:20:04 +0200402 if( verify )
403 {
404 ssl_set_authmode( &ssl, SSL_VERIFY_REQUIRED );
405 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
406 ssl_set_verify( &ssl, my_verify, NULL );
407 }
408 else
409 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
Paul Bakker4fc45522010-03-18 20:11:58 +0000410
Paul Bakker508ad5a2011-12-04 17:09:26 +0000411 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker4fc45522010-03-18 20:11:58 +0000412 ssl_set_dbg( &ssl, my_debug, stdout );
413 ssl_set_bio( &ssl, net_recv, &server_fd,
414 net_send, &server_fd );
415
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200416 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
417 {
Rich Evansf90016a2015-01-19 14:26:37 +0000418 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200419 goto exit;
420 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000421
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200422#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200423 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
424 {
Rich Evansf90016a2015-01-19 14:26:37 +0000425 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200426 goto exit;
427 }
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200428#endif
Paul Bakker4fc45522010-03-18 20:11:58 +0000429
430 /*
431 * 4. Handshake
432 */
433 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
434 {
Paul Bakker831a7552011-05-18 13:32:51 +0000435 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000436 {
Rich Evansf90016a2015-01-19 14:26:37 +0000437 polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000438 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000439 goto exit;
440 }
441 }
442
Rich Evansf90016a2015-01-19 14:26:37 +0000443 polarssl_printf( " ok\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000444
445 /*
446 * 5. Print the certificate
447 */
Rich Evansf90016a2015-01-19 14:26:37 +0000448 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200449 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
450 ssl.session->peer_cert );
Paul Bakker4fc45522010-03-18 20:11:58 +0000451 if( ret == -1 )
452 {
Rich Evansf90016a2015-01-19 14:26:37 +0000453 polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000454 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000455 goto exit;
456 }
457
Rich Evansf90016a2015-01-19 14:26:37 +0000458 polarssl_printf( "%s\n", buf );
Paul Bakker4fc45522010-03-18 20:11:58 +0000459
460 ssl_close_notify( &ssl );
Paul Bakker9a736322012-11-14 12:39:52 +0000461 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000462 }
463 else
464 goto usage;
465
466exit:
467
Paul Bakker1a207ec2011-02-06 13:22:40 +0000468 if( server_fd )
469 net_close( server_fd );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470 x509_crt_free( &cacert );
471 x509_crt_free( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200472#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200473 x509_crl_free( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200474#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200475 pk_free( &pkey );
Paul Bakkera317a982014-06-18 16:44:11 +0200476 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200477 entropy_free( &entropy );
Paul Bakker4fc45522010-03-18 20:11:58 +0000478
Paul Bakkercce9d772011-11-18 14:26:47 +0000479#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000480 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000481 fflush( stdout ); getchar();
482#endif
483
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100484 if( ret < 0 )
485 ret = 1;
486
Paul Bakker4fc45522010-03-18 20:11:58 +0000487 return( ret );
488}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000489#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000490 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200491 POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */