blob: 3d175281a2c3b72dfc4f8dd3ca56d95ea35d23a8 [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"
Simon Butcher23abd162016-09-26 22:03:55 +010058#include "polarssl/debug.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000059
Rich Evans18b78c72015-02-11 14:06:19 +000060#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000063
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 Evans85b05ec2015-02-12 11:37:29 +000078#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 Bakker4fc45522010-03-18 20:11:58 +000098/*
99 * global options
100 */
101struct options
102{
103 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 const char *filename; /* filename of the certificate file */
105 const char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1fd32532014-05-28 11:36:16 +0200106 const char *crl_file; /* the file with the CRL to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200107 const char *ca_path; /* the path with the CA certificate(s) reside */
108 const char *server_name; /* hostname of the server (client only) */
Paul Bakker4fc45522010-03-18 20:11:58 +0000109 int server_port; /* port on which the ssl service runs */
110 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000111 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +0000112} opt;
113
Paul Bakker3c5ef712013-06-25 16:37:45 +0200114static void my_debug( void *ctx, int level, const char *str )
Paul Bakker4fc45522010-03-18 20:11:58 +0000115{
116 if( level < opt.debug_level )
117 {
Rich Evansf90016a2015-01-19 14:26:37 +0000118 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker4fc45522010-03-18 20:11:58 +0000119 fflush( (FILE *) ctx );
120 }
121}
122
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200123static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker777a5752013-05-21 16:20:04 +0200124{
125 char buf[1024];
126 ((void) data);
127
Rich Evansf90016a2015-01-19 14:26:37 +0000128 polarssl_printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200129 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( "%s", buf );
Paul Bakker777a5752013-05-21 16:20:04 +0200131
Paul Bakker777a5752013-05-21 16:20:04 +0200132 if ( ( *flags ) == 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000133 polarssl_printf( " This certificate has no flags\n" );
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200134 else
135 {
136 x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
137 polarssl_printf( "%s\n", buf );
138 }
Paul Bakker777a5752013-05-21 16:20:04 +0200139
140 return( 0 );
141}
142
Paul Bakker4fc45522010-03-18 20:11:58 +0000143int main( int argc, char *argv[] )
144{
145 int ret = 0, server_fd;
146 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000147 entropy_context entropy;
148 ctr_drbg_context ctr_drbg;
Paul Bakker4fc45522010-03-18 20:11:58 +0000149 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200150 x509_crt cacert;
151 x509_crt clicert;
Paul Bakker1fd32532014-05-28 11:36:16 +0200152 x509_crl cacrl;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200153 pk_context pkey;
Paul Bakker36713e82013-09-17 13:25:29 +0200154 int i, j;
Paul Bakker777a5752013-05-21 16:20:04 +0200155 int flags, verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000156 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200157 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000158
Paul Bakker1a207ec2011-02-06 13:22:40 +0000159 /*
160 * Set to sane values
161 */
162 server_fd = 0;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200163 x509_crt_init( &cacert );
164 x509_crt_init( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200165#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200166 x509_crl_init( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200167#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é-Gonnardac755232013-08-19 14:10:16 +0200172 pk_init( &pkey );
Paul Bakker1a207ec2011-02-06 13:22:40 +0000173
Paul Bakker4fc45522010-03-18 20:11:58 +0000174 if( argc == 0 )
175 {
176 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000177 polarssl_printf( USAGE );
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100178 ret = 2;
Paul Bakker4fc45522010-03-18 20:11:58 +0000179 goto exit;
180 }
181
182 opt.mode = DFL_MODE;
183 opt.filename = DFL_FILENAME;
Paul Bakker777a5752013-05-21 16:20:04 +0200184 opt.ca_file = DFL_CA_FILE;
Paul Bakker1fd32532014-05-28 11:36:16 +0200185 opt.crl_file = DFL_CRL_FILE;
Paul Bakker777a5752013-05-21 16:20:04 +0200186 opt.ca_path = DFL_CA_PATH;
Paul Bakker4fc45522010-03-18 20:11:58 +0000187 opt.server_name = DFL_SERVER_NAME;
188 opt.server_port = DFL_SERVER_PORT;
189 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000190 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000191
192 for( i = 1; i < argc; i++ )
193 {
Paul Bakker4fc45522010-03-18 20:11:58 +0000194 p = argv[i];
195 if( ( q = strchr( p, '=' ) ) == NULL )
196 goto usage;
197 *q++ = '\0';
198
Paul Bakkerace02862013-09-16 21:40:34 +0200199 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 Bakker4fc45522010-03-18 20:11:58 +0000205 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 Bakker777a5752013-05-21 16:20:04 +0200216 else if( strcmp( p, "ca_file" ) == 0 )
217 opt.ca_file = q;
Paul Bakker1fd32532014-05-28 11:36:16 +0200218 else if( strcmp( p, "crl_file" ) == 0 )
219 opt.crl_file = q;
Paul Bakker777a5752013-05-21 16:20:04 +0200220 else if( strcmp( p, "ca_path" ) == 0 )
221 opt.ca_path = q;
Paul Bakker4fc45522010-03-18 20:11:58 +0000222 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 Bakker6c0ceb32011-12-04 12:24:18 +0000236 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 Bakker4fc45522010-03-18 20:11:58 +0000242 else
243 goto usage;
244 }
245
Paul Bakker777a5752013-05-21 16:20:04 +0200246 /*
247 * 1.1. Load the trusted CA
248 */
Rich Evansf90016a2015-01-19 14:26:37 +0000249 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker777a5752013-05-21 16:20:04 +0200250 fflush( stdout );
251
252 if( strlen( opt.ca_path ) )
253 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200254 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker777a5752013-05-21 16:20:04 +0200255 verify = 1;
256 }
257 else if( strlen( opt.ca_file ) )
258 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200259 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker777a5752013-05-21 16:20:04 +0200260 verify = 1;
261 }
262
263 if( ret < 0 )
264 {
Rich Evansf90016a2015-01-19 14:26:37 +0000265 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200266 goto exit;
267 }
268
Rich Evansf90016a2015-01-19 14:26:37 +0000269 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200270
Paul Bakker8880cb52014-06-12 23:22:26 +0200271#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200272 if( strlen( opt.crl_file ) )
273 {
Paul Bakker8880cb52014-06-12 23:22:26 +0200274 if( ( ret = x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 )
275 {
Rich Evansf90016a2015-01-19 14:26:37 +0000276 polarssl_printf( " failed\n ! x509_crl_parse returned -0x%x\n\n", -ret );
Paul Bakker8880cb52014-06-12 23:22:26 +0200277 goto exit;
278 }
279
Paul Bakker1fd32532014-05-28 11:36:16 +0200280 verify = 1;
281 }
Paul Bakker8880cb52014-06-12 23:22:26 +0200282#endif
Paul Bakker1fd32532014-05-28 11:36:16 +0200283
Paul Bakker4fc45522010-03-18 20:11:58 +0000284 if( opt.mode == MODE_FILE )
285 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200286 x509_crt crt;
287 x509_crt *cur = &crt;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200288 x509_crt_init( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000289
290 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000291 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000292 */
Rich Evansf90016a2015-01-19 14:26:37 +0000293 polarssl_printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000294 fflush( stdout );
295
Paul Bakkerddf26b42013-09-18 13:46:23 +0200296 ret = x509_crt_parse_file( &crt, opt.filename );
Paul Bakker4fc45522010-03-18 20:11:58 +0000297
Paul Bakker69e095c2011-12-10 21:55:01 +0000298 if( ret < 0 )
Paul Bakker4fc45522010-03-18 20:11:58 +0000299 {
Rich Evansf90016a2015-01-19 14:26:37 +0000300 polarssl_printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000302 goto exit;
303 }
304
Paul Bakker69e095c2011-12-10 21:55:01 +0000305 if( opt.permissive == 0 && ret > 0 )
306 {
Rich Evansf90016a2015-01-19 14:26:37 +0000307 polarssl_printf( " failed\n ! x509_crt_parse failed to parse %d certificates\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 x509_crt_free( &crt );
Paul Bakker69e095c2011-12-10 21:55:01 +0000309 goto exit;
310 }
311
Rich Evansf90016a2015-01-19 14:26:37 +0000312 polarssl_printf( " ok\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000313
314 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000315 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000316 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000317 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000318 {
Rich Evansf90016a2015-01-19 14:26:37 +0000319 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200320 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
321 cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000322 if( ret == -1 )
323 {
Rich Evansf90016a2015-01-19 14:26:37 +0000324 polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 x509_crt_free( &crt );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000326 goto exit;
327 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000328
Rich Evansf90016a2015-01-19 14:26:37 +0000329 polarssl_printf( "%s\n", buf );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000330
331 cur = cur->next;
332 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000333
Manuel Pégourié-Gonnard671589d2015-02-16 09:24:08 +0000334 ret = 0;
335
Paul Bakker777a5752013-05-21 16:20:04 +0200336 /*
337 * 1.3 Verify the certificate
338 */
339 if( verify )
340 {
Rich Evansf90016a2015-01-19 14:26:37 +0000341 polarssl_printf( " . Verifying X.509 certificate..." );
Paul Bakker777a5752013-05-21 16:20:04 +0200342
Paul Bakker1fd32532014-05-28 11:36:16 +0200343 if( ( ret = x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200344 my_verify, NULL ) ) != 0 )
Paul Bakker777a5752013-05-21 16:20:04 +0200345 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200346 char vrfy_buf[512];
347
Rich Evansf90016a2015-01-19 14:26:37 +0000348 polarssl_printf( " failed\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200349
Manuel Pégourié-Gonnard637376c2015-04-24 14:07:07 +0200350 x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker777a5752013-05-21 16:20:04 +0200351
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200352 polarssl_printf( "%s\n", vrfy_buf );
Paul Bakker777a5752013-05-21 16:20:04 +0200353 }
354 else
Rich Evansf90016a2015-01-19 14:26:37 +0000355 polarssl_printf( " ok\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200356 }
357
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000359 }
360 else if( opt.mode == MODE_SSL )
361 {
362 /*
363 * 1. Initialize the RNG and the session data
364 */
Rich Evansf90016a2015-01-19 14:26:37 +0000365 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000366 fflush( stdout );
367
368 entropy_init( &entropy );
369 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200370 (const unsigned char *) pers,
371 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000372 {
Rich Evansf90016a2015-01-19 14:26:37 +0000373 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000374 goto exit;
375 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000376
Rich Evansf90016a2015-01-19 14:26:37 +0000377 polarssl_printf( " ok\n" );
Paul Bakker777a5752013-05-21 16:20:04 +0200378
Simon Butcher23abd162016-09-26 22:03:55 +0100379#if defined(POLARSSL_DEBUG_C)
380 debug_set_threshold( opt.debug_level );
381#endif
382
Paul Bakker4fc45522010-03-18 20:11:58 +0000383 /*
384 * 2. Start the connection
385 */
Rich Evansf90016a2015-01-19 14:26:37 +0000386 polarssl_printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
Paul Bakker4fc45522010-03-18 20:11:58 +0000387 opt.server_port );
388 fflush( stdout );
389
390 if( ( ret = net_connect( &server_fd, opt.server_name,
391 opt.server_port ) ) != 0 )
392 {
Rich Evansf90016a2015-01-19 14:26:37 +0000393 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker4fc45522010-03-18 20:11:58 +0000394 goto exit;
395 }
396
397 /*
398 * 3. Setup stuff
399 */
400 if( ( ret = ssl_init( &ssl ) ) != 0 )
401 {
Rich Evansf90016a2015-01-19 14:26:37 +0000402 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker4fc45522010-03-18 20:11:58 +0000403 goto exit;
404 }
405
406 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker777a5752013-05-21 16:20:04 +0200407 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 Bakker4fc45522010-03-18 20:11:58 +0000415
Paul Bakker508ad5a2011-12-04 17:09:26 +0000416 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker4fc45522010-03-18 20:11:58 +0000417 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é-Gonnardc5fd3912014-07-08 14:05:52 +0200421 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
422 {
Rich Evansf90016a2015-01-19 14:26:37 +0000423 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200424 goto exit;
425 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000426
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200427#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200428 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
429 {
Rich Evansf90016a2015-01-19 14:26:37 +0000430 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200431 goto exit;
432 }
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200433#endif
Paul Bakker4fc45522010-03-18 20:11:58 +0000434
435 /*
436 * 4. Handshake
437 */
438 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
439 {
Paul Bakker831a7552011-05-18 13:32:51 +0000440 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000441 {
Rich Evansf90016a2015-01-19 14:26:37 +0000442 polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000443 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000444 goto exit;
445 }
446 }
447
Rich Evansf90016a2015-01-19 14:26:37 +0000448 polarssl_printf( " ok\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000449
450 /*
451 * 5. Print the certificate
452 */
Rich Evansf90016a2015-01-19 14:26:37 +0000453 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200454 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
455 ssl.session->peer_cert );
Paul Bakker4fc45522010-03-18 20:11:58 +0000456 if( ret == -1 )
457 {
Rich Evansf90016a2015-01-19 14:26:37 +0000458 polarssl_printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000459 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000460 goto exit;
461 }
462
Rich Evansf90016a2015-01-19 14:26:37 +0000463 polarssl_printf( "%s\n", buf );
Paul Bakker4fc45522010-03-18 20:11:58 +0000464
465 ssl_close_notify( &ssl );
Paul Bakker9a736322012-11-14 12:39:52 +0000466 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000467 }
468 else
469 goto usage;
470
471exit:
472
Paul Bakker1a207ec2011-02-06 13:22:40 +0000473 if( server_fd )
474 net_close( server_fd );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 x509_crt_free( &cacert );
476 x509_crt_free( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200477#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200478 x509_crl_free( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200479#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200480 pk_free( &pkey );
Paul Bakkera317a982014-06-18 16:44:11 +0200481 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200482 entropy_free( &entropy );
Paul Bakker4fc45522010-03-18 20:11:58 +0000483
Paul Bakkercce9d772011-11-18 14:26:47 +0000484#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000485 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker4fc45522010-03-18 20:11:58 +0000486 fflush( stdout ); getchar();
487#endif
488
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100489 if( ret < 0 )
490 ret = 1;
491
Paul Bakker4fc45522010-03-18 20:11:58 +0000492 return( ret );
493}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000494#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000495 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200496 POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */