blob: 7ec9893a5654943287b937bf25b74a245d5d26fe [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker508ad5a2011-12-04 17:09:26 +000033#include "polarssl/entropy.h"
34#include "polarssl/ctr_drbg.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000035#include "polarssl/net.h"
36#include "polarssl/ssl.h"
37#include "polarssl/x509.h"
38
Paul Bakker80d44fe2013-09-09 15:59:20 +020039#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
40 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
41 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) || \
Paul Bakker80d44fe2013-09-09 15:59:20 +020043 !defined(POLARSSL_CTR_DRBG_C)
44int main( int argc, char *argv[] )
45{
46 ((void) argc);
47 ((void) argv);
48
49 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
50 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
51 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052 "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker80d44fe2013-09-09 15:59:20 +020053 "POLARSSL_CTR_DRBG_C not defined.\n");
54 return( 0 );
55}
56#else
57
Paul Bakker4fc45522010-03-18 20:11:58 +000058#define MODE_NONE 0
59#define MODE_FILE 1
60#define MODE_SSL 2
61
62#define DFL_MODE MODE_NONE
63#define DFL_FILENAME "cert.crt"
Paul Bakker777a5752013-05-21 16:20:04 +020064#define DFL_CA_FILE ""
Paul Bakker1fd32532014-05-28 11:36:16 +020065#define DFL_CRL_FILE ""
Paul Bakker777a5752013-05-21 16:20:04 +020066#define DFL_CA_PATH ""
Paul Bakker4fc45522010-03-18 20:11:58 +000067#define DFL_SERVER_NAME "localhost"
68#define DFL_SERVER_PORT 4433
69#define DFL_DEBUG_LEVEL 0
Paul Bakker6c0ceb32011-12-04 12:24:18 +000070#define DFL_PERMISSIVE 0
Paul Bakker4fc45522010-03-18 20:11:58 +000071
72/*
73 * global options
74 */
75struct options
76{
77 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020078 const char *filename; /* filename of the certificate file */
79 const char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1fd32532014-05-28 11:36:16 +020080 const char *crl_file; /* the file with the CRL to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020081 const char *ca_path; /* the path with the CA certificate(s) reside */
82 const char *server_name; /* hostname of the server (client only) */
Paul Bakker4fc45522010-03-18 20:11:58 +000083 int server_port; /* port on which the ssl service runs */
84 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000085 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +000086} opt;
87
Paul Bakker3c5ef712013-06-25 16:37:45 +020088static void my_debug( void *ctx, int level, const char *str )
Paul Bakker4fc45522010-03-18 20:11:58 +000089{
90 if( level < opt.debug_level )
91 {
92 fprintf( (FILE *) ctx, "%s", str );
93 fflush( (FILE *) ctx );
94 }
95}
96
Paul Bakkerc559c7a2013-09-18 14:13:26 +020097static int my_verify( void *data, x509_crt *crt, int depth, int *flags )
Paul Bakker777a5752013-05-21 16:20:04 +020098{
99 char buf[1024];
100 ((void) data);
101
102 printf( "\nVerify requested for (Depth %d):\n", depth );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200103 x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Paul Bakker777a5752013-05-21 16:20:04 +0200104 printf( "%s", buf );
105
106 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
107 printf( " ! server certificate has expired\n" );
108
109 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
110 printf( " ! server certificate has been revoked\n" );
111
112 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
113 printf( " ! CN mismatch\n" );
114
115 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
116 printf( " ! self-signed or not signed by a trusted CA\n" );
117
118 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
119 printf( " ! CRL not trusted\n" );
120
121 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
122 printf( " ! CRL expired\n" );
123
124 if( ( (*flags) & BADCERT_OTHER ) != 0 )
125 printf( " ! other (unknown) flag\n" );
126
127 if ( ( *flags ) == 0 )
128 printf( " This certificate has no flags\n" );
129
130 return( 0 );
131}
132
133#define USAGE_IO \
134 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
135 " default: \"\" (none)\n" \
Paul Bakker1fd32532014-05-28 11:36:16 +0200136 " crl_file=%%s The single CRL file you want to use\n" \
137 " default: \"\" (none)\n" \
Paul Bakker777a5752013-05-21 16:20:04 +0200138 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
139 " default: \"\" (none) (overrides ca_file)\n"
140
Paul Bakker4fc45522010-03-18 20:11:58 +0000141#define USAGE \
142 "\n usage: cert_app param=<>...\n" \
143 "\n acceptable parameters:\n" \
Paul Bakker777a5752013-05-21 16:20:04 +0200144 " mode=file|ssl default: none\n" \
Paul Bakker4fc45522010-03-18 20:11:58 +0000145 " filename=%%s default: cert.crt\n" \
Paul Bakker777a5752013-05-21 16:20:04 +0200146 USAGE_IO \
Paul Bakker4fc45522010-03-18 20:11:58 +0000147 " server_name=%%s default: localhost\n" \
148 " server_port=%%d default: 4433\n" \
149 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000150 " permissive=%%d default: 0 (disabled)\n" \
Paul Bakker4fc45522010-03-18 20:11:58 +0000151 "\n"
152
153int main( int argc, char *argv[] )
154{
155 int ret = 0, server_fd;
156 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000157 entropy_context entropy;
158 ctr_drbg_context ctr_drbg;
Paul Bakker4fc45522010-03-18 20:11:58 +0000159 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200160 x509_crt cacert;
161 x509_crt clicert;
Paul Bakker1fd32532014-05-28 11:36:16 +0200162 x509_crl cacrl;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200163 pk_context pkey;
Paul Bakker36713e82013-09-17 13:25:29 +0200164 int i, j;
Paul Bakker777a5752013-05-21 16:20:04 +0200165 int flags, verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000166 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200167 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000168
Paul Bakker1a207ec2011-02-06 13:22:40 +0000169 /*
170 * Set to sane values
171 */
172 server_fd = 0;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200173 x509_crt_init( &cacert );
174 x509_crt_init( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200175#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200176 x509_crl_init( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200177#else
178 /* Zeroize structure as CRL parsing is not supported and we have to pass
179 it to the verify function */
180 memset( &cacrl, 0, sizeof(x509_crl) );
181#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200182 pk_init( &pkey );
Paul Bakker1a207ec2011-02-06 13:22:40 +0000183
Paul Bakker4fc45522010-03-18 20:11:58 +0000184 if( argc == 0 )
185 {
186 usage:
187 printf( USAGE );
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100188 ret = 2;
Paul Bakker4fc45522010-03-18 20:11:58 +0000189 goto exit;
190 }
191
192 opt.mode = DFL_MODE;
193 opt.filename = DFL_FILENAME;
Paul Bakker777a5752013-05-21 16:20:04 +0200194 opt.ca_file = DFL_CA_FILE;
Paul Bakker1fd32532014-05-28 11:36:16 +0200195 opt.crl_file = DFL_CRL_FILE;
Paul Bakker777a5752013-05-21 16:20:04 +0200196 opt.ca_path = DFL_CA_PATH;
Paul Bakker4fc45522010-03-18 20:11:58 +0000197 opt.server_name = DFL_SERVER_NAME;
198 opt.server_port = DFL_SERVER_PORT;
199 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000200 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000201
202 for( i = 1; i < argc; i++ )
203 {
Paul Bakker4fc45522010-03-18 20:11:58 +0000204 p = argv[i];
205 if( ( q = strchr( p, '=' ) ) == NULL )
206 goto usage;
207 *q++ = '\0';
208
Paul Bakkerace02862013-09-16 21:40:34 +0200209 for( j = 0; p + j < q; j++ )
210 {
211 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
212 argv[i][j] |= 0x20;
213 }
214
Paul Bakker4fc45522010-03-18 20:11:58 +0000215 if( strcmp( p, "mode" ) == 0 )
216 {
217 if( strcmp( q, "file" ) == 0 )
218 opt.mode = MODE_FILE;
219 else if( strcmp( q, "ssl" ) == 0 )
220 opt.mode = MODE_SSL;
221 else
222 goto usage;
223 }
224 else if( strcmp( p, "filename" ) == 0 )
225 opt.filename = q;
Paul Bakker777a5752013-05-21 16:20:04 +0200226 else if( strcmp( p, "ca_file" ) == 0 )
227 opt.ca_file = q;
Paul Bakker1fd32532014-05-28 11:36:16 +0200228 else if( strcmp( p, "crl_file" ) == 0 )
229 opt.crl_file = q;
Paul Bakker777a5752013-05-21 16:20:04 +0200230 else if( strcmp( p, "ca_path" ) == 0 )
231 opt.ca_path = q;
Paul Bakker4fc45522010-03-18 20:11:58 +0000232 else if( strcmp( p, "server_name" ) == 0 )
233 opt.server_name = q;
234 else if( strcmp( p, "server_port" ) == 0 )
235 {
236 opt.server_port = atoi( q );
237 if( opt.server_port < 1 || opt.server_port > 65535 )
238 goto usage;
239 }
240 else if( strcmp( p, "debug_level" ) == 0 )
241 {
242 opt.debug_level = atoi( q );
243 if( opt.debug_level < 0 || opt.debug_level > 65535 )
244 goto usage;
245 }
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000246 else if( strcmp( p, "permissive" ) == 0 )
247 {
248 opt.permissive = atoi( q );
249 if( opt.permissive < 0 || opt.permissive > 1 )
250 goto usage;
251 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000252 else
253 goto usage;
254 }
255
Paul Bakker777a5752013-05-21 16:20:04 +0200256 /*
257 * 1.1. Load the trusted CA
258 */
259 printf( " . Loading the CA root certificate ..." );
260 fflush( stdout );
261
262 if( strlen( opt.ca_path ) )
263 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200264 ret = x509_crt_parse_path( &cacert, opt.ca_path );
Paul Bakker777a5752013-05-21 16:20:04 +0200265 verify = 1;
266 }
267 else if( strlen( opt.ca_file ) )
268 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200269 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker777a5752013-05-21 16:20:04 +0200270 verify = 1;
271 }
272
273 if( ret < 0 )
274 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200275 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker777a5752013-05-21 16:20:04 +0200276 goto exit;
277 }
278
279 printf( " ok (%d skipped)\n", ret );
280
Paul Bakker8880cb52014-06-12 23:22:26 +0200281#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200282 if( strlen( opt.crl_file ) )
283 {
Paul Bakker8880cb52014-06-12 23:22:26 +0200284 if( ( ret = x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 )
285 {
286 printf( " failed\n ! x509_crl_parse returned -0x%x\n\n", -ret );
287 goto exit;
288 }
289
Paul Bakker1fd32532014-05-28 11:36:16 +0200290 verify = 1;
291 }
Paul Bakker8880cb52014-06-12 23:22:26 +0200292#endif
Paul Bakker1fd32532014-05-28 11:36:16 +0200293
Paul Bakker4fc45522010-03-18 20:11:58 +0000294 if( opt.mode == MODE_FILE )
295 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200296 x509_crt crt;
297 x509_crt *cur = &crt;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200298 x509_crt_init( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000299
300 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000301 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000302 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000303 printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000304 fflush( stdout );
305
Paul Bakkerddf26b42013-09-18 13:46:23 +0200306 ret = x509_crt_parse_file( &crt, opt.filename );
Paul Bakker4fc45522010-03-18 20:11:58 +0000307
Paul Bakker69e095c2011-12-10 21:55:01 +0000308 if( ret < 0 )
Paul Bakker4fc45522010-03-18 20:11:58 +0000309 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200310 printf( " failed\n ! x509_crt_parse_file returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000312 goto exit;
313 }
314
Paul Bakker69e095c2011-12-10 21:55:01 +0000315 if( opt.permissive == 0 && ret > 0 )
316 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200317 printf( " failed\n ! x509_crt_parse failed to parse %d certificates\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318 x509_crt_free( &crt );
Paul Bakker69e095c2011-12-10 21:55:01 +0000319 goto exit;
320 }
321
Paul Bakker4fc45522010-03-18 20:11:58 +0000322 printf( " ok\n" );
323
324 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000325 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000326 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000327 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000328 {
Paul Bakker14cb63a2011-11-25 12:44:31 +0000329 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200330 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
331 cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000332 if( ret == -1 )
333 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200334 printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 x509_crt_free( &crt );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000336 goto exit;
337 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000338
Paul Bakker14cb63a2011-11-25 12:44:31 +0000339 printf( "%s\n", buf );
340
341 cur = cur->next;
342 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000343
Paul Bakker777a5752013-05-21 16:20:04 +0200344 /*
345 * 1.3 Verify the certificate
346 */
347 if( verify )
348 {
349 printf( " . Verifying X.509 certificate..." );
350
Paul Bakker1fd32532014-05-28 11:36:16 +0200351 if( ( ret = x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200352 my_verify, NULL ) ) != 0 )
Paul Bakker777a5752013-05-21 16:20:04 +0200353 {
354 printf( " failed\n" );
355
356 if( ( ret & BADCERT_EXPIRED ) != 0 )
357 printf( " ! server certificate has expired\n" );
358
359 if( ( ret & BADCERT_REVOKED ) != 0 )
360 printf( " ! server certificate has been revoked\n" );
361
362 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
363 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
364
365 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
366 printf( " ! self-signed or not signed by a trusted CA\n" );
367
368 printf( "\n" );
369 }
370 else
371 printf( " ok\n" );
372 }
373
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 x509_crt_free( &crt );
Paul Bakker4fc45522010-03-18 20:11:58 +0000375 }
376 else if( opt.mode == MODE_SSL )
377 {
378 /*
379 * 1. Initialize the RNG and the session data
380 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000381 printf( "\n . Seeding the random number generator..." );
382 fflush( stdout );
383
384 entropy_init( &entropy );
385 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200386 (const unsigned char *) pers,
387 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000388 {
389 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
390 goto exit;
391 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000392
Paul Bakker777a5752013-05-21 16:20:04 +0200393 printf( " ok\n" );
394
Paul Bakker4fc45522010-03-18 20:11:58 +0000395 /*
396 * 2. Start the connection
397 */
398 printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
399 opt.server_port );
400 fflush( stdout );
401
402 if( ( ret = net_connect( &server_fd, opt.server_name,
403 opt.server_port ) ) != 0 )
404 {
405 printf( " failed\n ! net_connect returned %d\n\n", ret );
406 goto exit;
407 }
408
409 /*
410 * 3. Setup stuff
411 */
412 if( ( ret = ssl_init( &ssl ) ) != 0 )
413 {
414 printf( " failed\n ! ssl_init returned %d\n\n", ret );
415 goto exit;
416 }
417
418 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker777a5752013-05-21 16:20:04 +0200419 if( verify )
420 {
421 ssl_set_authmode( &ssl, SSL_VERIFY_REQUIRED );
422 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
423 ssl_set_verify( &ssl, my_verify, NULL );
424 }
425 else
426 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
Paul Bakker4fc45522010-03-18 20:11:58 +0000427
Paul Bakker508ad5a2011-12-04 17:09:26 +0000428 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker4fc45522010-03-18 20:11:58 +0000429 ssl_set_dbg( &ssl, my_debug, stdout );
430 ssl_set_bio( &ssl, net_recv, &server_fd,
431 net_send, &server_fd );
432
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200433 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
434 {
435 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
436 goto exit;
437 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000438
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200439#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200440 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
441 {
442 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
443 goto exit;
444 }
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200445#endif
Paul Bakker4fc45522010-03-18 20:11:58 +0000446
447 /*
448 * 4. Handshake
449 */
450 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
451 {
Paul Bakker831a7552011-05-18 13:32:51 +0000452 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000453 {
454 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000455 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000456 goto exit;
457 }
458 }
459
460 printf( " ok\n" );
461
462 /*
463 * 5. Print the certificate
464 */
465 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200466 ret = x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
467 ssl.session->peer_cert );
Paul Bakker4fc45522010-03-18 20:11:58 +0000468 if( ret == -1 )
469 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200470 printf( " failed\n ! x509_crt_info returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000471 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000472 goto exit;
473 }
474
475 printf( "%s\n", buf );
476
477 ssl_close_notify( &ssl );
Paul Bakker9a736322012-11-14 12:39:52 +0000478 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000479 }
480 else
481 goto usage;
482
483exit:
484
Paul Bakker1a207ec2011-02-06 13:22:40 +0000485 if( server_fd )
486 net_close( server_fd );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 x509_crt_free( &cacert );
488 x509_crt_free( &clicert );
Paul Bakker8880cb52014-06-12 23:22:26 +0200489#if defined(POLARSSL_X509_CRL_PARSE_C)
Paul Bakker1fd32532014-05-28 11:36:16 +0200490 x509_crl_free( &cacrl );
Paul Bakker8880cb52014-06-12 23:22:26 +0200491#endif
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200492 pk_free( &pkey );
Paul Bakkera317a982014-06-18 16:44:11 +0200493 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200494 entropy_free( &entropy );
Paul Bakker4fc45522010-03-18 20:11:58 +0000495
Paul Bakkercce9d772011-11-18 14:26:47 +0000496#if defined(_WIN32)
Paul Bakker4fc45522010-03-18 20:11:58 +0000497 printf( " + Press Enter to exit this program.\n" );
498 fflush( stdout ); getchar();
499#endif
500
Manuel Pégourié-Gonnard49aa99e2014-11-10 16:40:16 +0100501 if( ret < 0 )
502 ret = 1;
503
Paul Bakker4fc45522010-03-18 20:11:58 +0000504 return( ret );
505}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000506#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000507 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200508 POLARSSL_X509_CRT_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */