blob: 2c8a88d7e12b1ee430f61bbce8c27963c5b378f6 [file] [log] [blame]
Paul Bakker4fc45522010-03-18 20:11:58 +00001/*
2 * Certificate reading application
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker4fc45522010-03-18 20:11:58 +00009 * All rights reserved.
10 *
Paul Bakker4fc45522010-03-18 20:11:58 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
Paul Bakker5690efc2011-05-26 13:16:06 +000034#include "polarssl/config.h"
35
Paul Bakker508ad5a2011-12-04 17:09:26 +000036#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000038#include "polarssl/net.h"
39#include "polarssl/ssl.h"
40#include "polarssl/x509.h"
41
42#define MODE_NONE 0
43#define MODE_FILE 1
44#define MODE_SSL 2
45
46#define DFL_MODE MODE_NONE
47#define DFL_FILENAME "cert.crt"
Paul Bakker777a5752013-05-21 16:20:04 +020048#define DFL_CA_FILE ""
49#define DFL_CA_PATH ""
Paul Bakker4fc45522010-03-18 20:11:58 +000050#define DFL_SERVER_NAME "localhost"
51#define DFL_SERVER_PORT 4433
52#define DFL_DEBUG_LEVEL 0
Paul Bakker6c0ceb32011-12-04 12:24:18 +000053#define DFL_PERMISSIVE 0
Paul Bakker4fc45522010-03-18 20:11:58 +000054
55/*
56 * global options
57 */
58struct options
59{
60 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020061 const char *filename; /* filename of the certificate file */
62 const char *ca_file; /* the file with the CA certificate(s) */
63 const char *ca_path; /* the path with the CA certificate(s) reside */
64 const char *server_name; /* hostname of the server (client only) */
Paul Bakker4fc45522010-03-18 20:11:58 +000065 int server_port; /* port on which the ssl service runs */
66 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000067 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +000068} opt;
69
70void my_debug( void *ctx, int level, const char *str )
71{
72 if( level < opt.debug_level )
73 {
74 fprintf( (FILE *) ctx, "%s", str );
75 fflush( (FILE *) ctx );
76 }
77}
78
Paul Bakker777a5752013-05-21 16:20:04 +020079int my_verify( void *data, x509_cert *crt, int depth, int *flags )
80{
81 char buf[1024];
82 ((void) data);
83
84 printf( "\nVerify requested for (Depth %d):\n", depth );
85 x509parse_cert_info( buf, sizeof( buf ) - 1, "", crt );
86 printf( "%s", buf );
87
88 if( ( (*flags) & BADCERT_EXPIRED ) != 0 )
89 printf( " ! server certificate has expired\n" );
90
91 if( ( (*flags) & BADCERT_REVOKED ) != 0 )
92 printf( " ! server certificate has been revoked\n" );
93
94 if( ( (*flags) & BADCERT_CN_MISMATCH ) != 0 )
95 printf( " ! CN mismatch\n" );
96
97 if( ( (*flags) & BADCERT_NOT_TRUSTED ) != 0 )
98 printf( " ! self-signed or not signed by a trusted CA\n" );
99
100 if( ( (*flags) & BADCRL_NOT_TRUSTED ) != 0 )
101 printf( " ! CRL not trusted\n" );
102
103 if( ( (*flags) & BADCRL_EXPIRED ) != 0 )
104 printf( " ! CRL expired\n" );
105
106 if( ( (*flags) & BADCERT_OTHER ) != 0 )
107 printf( " ! other (unknown) flag\n" );
108
109 if ( ( *flags ) == 0 )
110 printf( " This certificate has no flags\n" );
111
112 return( 0 );
113}
114
115#define USAGE_IO \
116 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
117 " default: \"\" (none)\n" \
118 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
119 " default: \"\" (none) (overrides ca_file)\n"
120
Paul Bakker4fc45522010-03-18 20:11:58 +0000121#define USAGE \
122 "\n usage: cert_app param=<>...\n" \
123 "\n acceptable parameters:\n" \
Paul Bakker777a5752013-05-21 16:20:04 +0200124 " mode=file|ssl default: none\n" \
Paul Bakker4fc45522010-03-18 20:11:58 +0000125 " filename=%%s default: cert.crt\n" \
Paul Bakker777a5752013-05-21 16:20:04 +0200126 USAGE_IO \
Paul Bakker4fc45522010-03-18 20:11:58 +0000127 " server_name=%%s default: localhost\n" \
128 " server_port=%%d default: 4433\n" \
129 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000130 " permissive=%%d default: 0 (disabled)\n" \
Paul Bakker4fc45522010-03-18 20:11:58 +0000131 "\n"
132
Paul Bakker508ad5a2011-12-04 17:09:26 +0000133#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000134 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
135 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +0000136 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) || \
137 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000138int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000139{
Paul Bakkercce9d772011-11-18 14:26:47 +0000140 ((void) argc);
141 ((void) argv);
142
Paul Bakker508ad5a2011-12-04 17:09:26 +0000143 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000144 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
145 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +0000146 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO and/or "
147 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000148 return( 0 );
149}
150#else
Paul Bakker4fc45522010-03-18 20:11:58 +0000151int main( int argc, char *argv[] )
152{
153 int ret = 0, server_fd;
154 unsigned char buf[1024];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000155 entropy_context entropy;
156 ctr_drbg_context ctr_drbg;
Paul Bakker4fc45522010-03-18 20:11:58 +0000157 ssl_context ssl;
Paul Bakker777a5752013-05-21 16:20:04 +0200158 x509_cert cacert;
Paul Bakker4fc45522010-03-18 20:11:58 +0000159 x509_cert clicert;
160 rsa_context rsa;
161 int i, j, n;
Paul Bakker777a5752013-05-21 16:20:04 +0200162 int flags, verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000163 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200164 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000165
Paul Bakker1a207ec2011-02-06 13:22:40 +0000166 /*
167 * Set to sane values
168 */
169 server_fd = 0;
Paul Bakker777a5752013-05-21 16:20:04 +0200170 memset( &cacert, 0, sizeof( x509_cert ) );
Paul Bakker1a207ec2011-02-06 13:22:40 +0000171 memset( &clicert, 0, sizeof( x509_cert ) );
172 memset( &rsa, 0, sizeof( rsa_context ) );
173
Paul Bakker4fc45522010-03-18 20:11:58 +0000174 if( argc == 0 )
175 {
176 usage:
177 printf( USAGE );
178 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;
184 opt.ca_path = DFL_CA_PATH;
Paul Bakker4fc45522010-03-18 20:11:58 +0000185 opt.server_name = DFL_SERVER_NAME;
186 opt.server_port = DFL_SERVER_PORT;
187 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000188 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000189
190 for( i = 1; i < argc; i++ )
191 {
192 n = strlen( argv[i] );
193
194 for( j = 0; j < n; j++ )
195 {
196 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
197 argv[i][j] |= 0x20;
198 }
199
200 p = argv[i];
201 if( ( q = strchr( p, '=' ) ) == NULL )
202 goto usage;
203 *q++ = '\0';
204
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 Bakker777a5752013-05-21 16:20:04 +0200216 else if( strcmp( p, "ca_file" ) == 0 )
217 opt.ca_file = q;
218 else if( strcmp( p, "ca_path" ) == 0 )
219 opt.ca_path = q;
Paul Bakker4fc45522010-03-18 20:11:58 +0000220 else if( strcmp( p, "server_name" ) == 0 )
221 opt.server_name = q;
222 else if( strcmp( p, "server_port" ) == 0 )
223 {
224 opt.server_port = atoi( q );
225 if( opt.server_port < 1 || opt.server_port > 65535 )
226 goto usage;
227 }
228 else if( strcmp( p, "debug_level" ) == 0 )
229 {
230 opt.debug_level = atoi( q );
231 if( opt.debug_level < 0 || opt.debug_level > 65535 )
232 goto usage;
233 }
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000234 else if( strcmp( p, "permissive" ) == 0 )
235 {
236 opt.permissive = atoi( q );
237 if( opt.permissive < 0 || opt.permissive > 1 )
238 goto usage;
239 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000240 else
241 goto usage;
242 }
243
Paul Bakker777a5752013-05-21 16:20:04 +0200244 /*
245 * 1.1. Load the trusted CA
246 */
247 printf( " . Loading the CA root certificate ..." );
248 fflush( stdout );
249
250 if( strlen( opt.ca_path ) )
251 {
252 ret = x509parse_crtpath( &cacert, opt.ca_path );
253 verify = 1;
254 }
255 else if( strlen( opt.ca_file ) )
256 {
257 ret = x509parse_crtfile( &cacert, opt.ca_file );
258 verify = 1;
259 }
260
261 if( ret < 0 )
262 {
263 printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret );
264 goto exit;
265 }
266
267 printf( " ok (%d skipped)\n", ret );
268
Paul Bakker4fc45522010-03-18 20:11:58 +0000269 if( opt.mode == MODE_FILE )
270 {
271 x509_cert crt;
Paul Bakker14cb63a2011-11-25 12:44:31 +0000272 x509_cert *cur = &crt;
Paul Bakker4fc45522010-03-18 20:11:58 +0000273 memset( &crt, 0, sizeof( x509_cert ) );
274
275 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000276 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000277 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000278 printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000279 fflush( stdout );
280
Paul Bakker69e095c2011-12-10 21:55:01 +0000281 ret = x509parse_crtfile( &crt, opt.filename );
Paul Bakker4fc45522010-03-18 20:11:58 +0000282
Paul Bakker69e095c2011-12-10 21:55:01 +0000283 if( ret < 0 )
Paul Bakker4fc45522010-03-18 20:11:58 +0000284 {
285 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
286 x509_free( &crt );
287 goto exit;
288 }
289
Paul Bakker69e095c2011-12-10 21:55:01 +0000290 if( opt.permissive == 0 && ret > 0 )
291 {
292 printf( " failed\n ! x509parse_crt failed to parse %d certificates\n\n", ret );
293 x509_free( &crt );
294 goto exit;
295 }
296
Paul Bakker4fc45522010-03-18 20:11:58 +0000297 printf( " ok\n" );
298
299 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000300 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000301 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000302 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000303 {
Paul Bakker14cb63a2011-11-25 12:44:31 +0000304 printf( " . Peer certificate information ...\n" );
Paul Bakker5c356d62011-11-25 13:17:45 +0000305 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000306 if( ret == -1 )
307 {
308 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
309 x509_free( &crt );
310 goto exit;
311 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000312
Paul Bakker14cb63a2011-11-25 12:44:31 +0000313 printf( "%s\n", buf );
314
315 cur = cur->next;
316 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000317
Paul Bakker777a5752013-05-21 16:20:04 +0200318 /*
319 * 1.3 Verify the certificate
320 */
321 if( verify )
322 {
323 printf( " . Verifying X.509 certificate..." );
324
325 if( ( ret = x509parse_verify( &crt, &cacert, NULL, NULL, &flags,
326 my_verify, NULL ) ) != 0 )
327 {
328 printf( " failed\n" );
329
330 if( ( ret & BADCERT_EXPIRED ) != 0 )
331 printf( " ! server certificate has expired\n" );
332
333 if( ( ret & BADCERT_REVOKED ) != 0 )
334 printf( " ! server certificate has been revoked\n" );
335
336 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
337 printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
338
339 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
340 printf( " ! self-signed or not signed by a trusted CA\n" );
341
342 printf( "\n" );
343 }
344 else
345 printf( " ok\n" );
346 }
347
Paul Bakker4fc45522010-03-18 20:11:58 +0000348 x509_free( &crt );
349 }
350 else if( opt.mode == MODE_SSL )
351 {
352 /*
353 * 1. Initialize the RNG and the session data
354 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000355 printf( "\n . Seeding the random number generator..." );
356 fflush( stdout );
357
358 entropy_init( &entropy );
359 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200360 (const unsigned char *) pers,
361 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000362 {
363 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
364 goto exit;
365 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000366
Paul Bakker777a5752013-05-21 16:20:04 +0200367 printf( " ok\n" );
368
Paul Bakker4fc45522010-03-18 20:11:58 +0000369 /*
370 * 2. Start the connection
371 */
372 printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
373 opt.server_port );
374 fflush( stdout );
375
376 if( ( ret = net_connect( &server_fd, opt.server_name,
377 opt.server_port ) ) != 0 )
378 {
379 printf( " failed\n ! net_connect returned %d\n\n", ret );
380 goto exit;
381 }
382
383 /*
384 * 3. Setup stuff
385 */
386 if( ( ret = ssl_init( &ssl ) ) != 0 )
387 {
388 printf( " failed\n ! ssl_init returned %d\n\n", ret );
389 goto exit;
390 }
391
392 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Paul Bakker777a5752013-05-21 16:20:04 +0200393 if( verify )
394 {
395 ssl_set_authmode( &ssl, SSL_VERIFY_REQUIRED );
396 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
397 ssl_set_verify( &ssl, my_verify, NULL );
398 }
399 else
400 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
Paul Bakker4fc45522010-03-18 20:11:58 +0000401
Paul Bakker508ad5a2011-12-04 17:09:26 +0000402 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker4fc45522010-03-18 20:11:58 +0000403 ssl_set_dbg( &ssl, my_debug, stdout );
404 ssl_set_bio( &ssl, net_recv, &server_fd,
405 net_send, &server_fd );
406
Paul Bakker4fc45522010-03-18 20:11:58 +0000407 ssl_set_own_cert( &ssl, &clicert, &rsa );
408
409 ssl_set_hostname( &ssl, opt.server_name );
410
411 /*
412 * 4. Handshake
413 */
414 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
415 {
Paul Bakker831a7552011-05-18 13:32:51 +0000416 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000417 {
418 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000419 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000420 goto exit;
421 }
422 }
423
424 printf( " ok\n" );
425
426 /*
427 * 5. Print the certificate
428 */
429 printf( " . Peer certificate information ...\n" );
Paul Bakker48916f92012-09-16 19:57:18 +0000430 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
431 ssl.session->peer_cert );
Paul Bakker4fc45522010-03-18 20:11:58 +0000432 if( ret == -1 )
433 {
434 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
Paul Bakker9a736322012-11-14 12:39:52 +0000435 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000436 goto exit;
437 }
438
439 printf( "%s\n", buf );
440
441 ssl_close_notify( &ssl );
Paul Bakker9a736322012-11-14 12:39:52 +0000442 ssl_free( &ssl );
Paul Bakker4fc45522010-03-18 20:11:58 +0000443 }
444 else
445 goto usage;
446
447exit:
448
Paul Bakker1a207ec2011-02-06 13:22:40 +0000449 if( server_fd )
450 net_close( server_fd );
Paul Bakker777a5752013-05-21 16:20:04 +0200451 x509_free( &cacert );
Paul Bakker4fc45522010-03-18 20:11:58 +0000452 x509_free( &clicert );
453 rsa_free( &rsa );
Paul Bakker4fc45522010-03-18 20:11:58 +0000454
Paul Bakkercce9d772011-11-18 14:26:47 +0000455#if defined(_WIN32)
Paul Bakker4fc45522010-03-18 20:11:58 +0000456 printf( " + Press Enter to exit this program.\n" );
457 fflush( stdout ); getchar();
458#endif
459
460 return( ret );
461}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000462#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000463 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000464 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */