blob: 24b97270e7d0c4a186a37b6cfedd33046860f762 [file] [log] [blame]
Paul Bakker4fc45522010-03-18 20:11:58 +00001/*
2 * Certificate reading application
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, 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 Bakker4fc45522010-03-18 20:11:58 +000036#include "polarssl/havege.h"
37#include "polarssl/net.h"
38#include "polarssl/ssl.h"
39#include "polarssl/x509.h"
40
41#define MODE_NONE 0
42#define MODE_FILE 1
43#define MODE_SSL 2
44
45#define DFL_MODE MODE_NONE
46#define DFL_FILENAME "cert.crt"
47#define DFL_SERVER_NAME "localhost"
48#define DFL_SERVER_PORT 4433
49#define DFL_DEBUG_LEVEL 0
Paul Bakker6c0ceb32011-12-04 12:24:18 +000050#define DFL_PERMISSIVE 0
Paul Bakker4fc45522010-03-18 20:11:58 +000051
52/*
53 * global options
54 */
55struct options
56{
57 int mode; /* the mode to run the application in */
58 char *filename; /* filename of the certificate file */
59 char *server_name; /* hostname of the server (client only) */
60 int server_port; /* port on which the ssl service runs */
61 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000062 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +000063} opt;
64
65void my_debug( void *ctx, int level, const char *str )
66{
67 if( level < opt.debug_level )
68 {
69 fprintf( (FILE *) ctx, "%s", str );
70 fflush( (FILE *) ctx );
71 }
72}
73
74#define USAGE \
75 "\n usage: cert_app param=<>...\n" \
76 "\n acceptable parameters:\n" \
77 " mode=file|ssl default: none\n" \
78 " filename=%%s default: cert.crt\n" \
79 " server_name=%%s default: localhost\n" \
80 " server_port=%%d default: 4433\n" \
81 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker6c0ceb32011-12-04 12:24:18 +000082 " permissive=%%d default: 0 (disabled)\n" \
Paul Bakker4fc45522010-03-18 20:11:58 +000083 "\n"
84
Paul Bakker5690efc2011-05-26 13:16:06 +000085#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
86 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
87 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
88 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000089int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000090{
Paul Bakkercce9d772011-11-18 14:26:47 +000091 ((void) argc);
92 ((void) argv);
93
Paul Bakker5690efc2011-05-26 13:16:06 +000094 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
95 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
96 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
97 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
98 return( 0 );
99}
100#else
Paul Bakker4fc45522010-03-18 20:11:58 +0000101int main( int argc, char *argv[] )
102{
103 int ret = 0, server_fd;
104 unsigned char buf[1024];
105 havege_state hs;
106 ssl_context ssl;
107 ssl_session ssn;
108 x509_cert clicert;
109 rsa_context rsa;
110 int i, j, n;
111 char *p, *q;
112
Paul Bakker1a207ec2011-02-06 13:22:40 +0000113 /*
114 * Set to sane values
115 */
116 server_fd = 0;
117 memset( &ssl, 0, sizeof( ssl_context ) );
118 memset( &ssn, 0, sizeof( ssl_session ) );
119 memset( &clicert, 0, sizeof( x509_cert ) );
120 memset( &rsa, 0, sizeof( rsa_context ) );
121
Paul Bakker4fc45522010-03-18 20:11:58 +0000122 if( argc == 0 )
123 {
124 usage:
125 printf( USAGE );
126 goto exit;
127 }
128
129 opt.mode = DFL_MODE;
130 opt.filename = DFL_FILENAME;
131 opt.server_name = DFL_SERVER_NAME;
132 opt.server_port = DFL_SERVER_PORT;
133 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000134 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000135
136 for( i = 1; i < argc; i++ )
137 {
138 n = strlen( argv[i] );
139
140 for( j = 0; j < n; j++ )
141 {
142 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
143 argv[i][j] |= 0x20;
144 }
145
146 p = argv[i];
147 if( ( q = strchr( p, '=' ) ) == NULL )
148 goto usage;
149 *q++ = '\0';
150
151 if( strcmp( p, "mode" ) == 0 )
152 {
153 if( strcmp( q, "file" ) == 0 )
154 opt.mode = MODE_FILE;
155 else if( strcmp( q, "ssl" ) == 0 )
156 opt.mode = MODE_SSL;
157 else
158 goto usage;
159 }
160 else if( strcmp( p, "filename" ) == 0 )
161 opt.filename = q;
162 else if( strcmp( p, "server_name" ) == 0 )
163 opt.server_name = q;
164 else if( strcmp( p, "server_port" ) == 0 )
165 {
166 opt.server_port = atoi( q );
167 if( opt.server_port < 1 || opt.server_port > 65535 )
168 goto usage;
169 }
170 else if( strcmp( p, "debug_level" ) == 0 )
171 {
172 opt.debug_level = atoi( q );
173 if( opt.debug_level < 0 || opt.debug_level > 65535 )
174 goto usage;
175 }
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000176 else if( strcmp( p, "permissive" ) == 0 )
177 {
178 opt.permissive = atoi( q );
179 if( opt.permissive < 0 || opt.permissive > 1 )
180 goto usage;
181 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000182 else
183 goto usage;
184 }
185
186 if( opt.mode == MODE_FILE )
187 {
188 x509_cert crt;
Paul Bakker14cb63a2011-11-25 12:44:31 +0000189 x509_cert *cur = &crt;
Paul Bakker4fc45522010-03-18 20:11:58 +0000190 memset( &crt, 0, sizeof( x509_cert ) );
191
192 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000193 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000194 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000195 printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000196 fflush( stdout );
197
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000198 ret = x509parse_crtfile( &crt, opt.filename, opt.permissive );
Paul Bakker4fc45522010-03-18 20:11:58 +0000199
200 if( ret != 0 )
201 {
202 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
203 x509_free( &crt );
204 goto exit;
205 }
206
207 printf( " ok\n" );
208
209 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000210 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000211 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000212 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000213 {
Paul Bakker14cb63a2011-11-25 12:44:31 +0000214 printf( " . Peer certificate information ...\n" );
Paul Bakker5c356d62011-11-25 13:17:45 +0000215 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000216 if( ret == -1 )
217 {
218 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
219 x509_free( &crt );
220 goto exit;
221 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000222
Paul Bakker14cb63a2011-11-25 12:44:31 +0000223 printf( "%s\n", buf );
224
225 cur = cur->next;
226 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000227
228 x509_free( &crt );
229 }
230 else if( opt.mode == MODE_SSL )
231 {
232 /*
233 * 1. Initialize the RNG and the session data
234 */
235 havege_init( &hs );
Paul Bakker4fc45522010-03-18 20:11:58 +0000236
237 /*
238 * 2. Start the connection
239 */
240 printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
241 opt.server_port );
242 fflush( stdout );
243
244 if( ( ret = net_connect( &server_fd, opt.server_name,
245 opt.server_port ) ) != 0 )
246 {
247 printf( " failed\n ! net_connect returned %d\n\n", ret );
248 goto exit;
249 }
250
251 /*
252 * 3. Setup stuff
253 */
254 if( ( ret = ssl_init( &ssl ) ) != 0 )
255 {
256 printf( " failed\n ! ssl_init returned %d\n\n", ret );
257 goto exit;
258 }
259
260 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
261 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
262
Paul Bakkera3d195c2011-11-27 21:07:34 +0000263 ssl_set_rng( &ssl, havege_random, &hs );
Paul Bakker4fc45522010-03-18 20:11:58 +0000264 ssl_set_dbg( &ssl, my_debug, stdout );
265 ssl_set_bio( &ssl, net_recv, &server_fd,
266 net_send, &server_fd );
267
Paul Bakkere3166ce2011-01-27 17:40:50 +0000268 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
Paul Bakker4fc45522010-03-18 20:11:58 +0000269 ssl_set_session( &ssl, 1, 600, &ssn );
270
271 ssl_set_own_cert( &ssl, &clicert, &rsa );
272
273 ssl_set_hostname( &ssl, opt.server_name );
274
275 /*
276 * 4. Handshake
277 */
278 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
279 {
Paul Bakker831a7552011-05-18 13:32:51 +0000280 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000281 {
282 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
283 goto exit;
284 }
285 }
286
287 printf( " ok\n" );
288
289 /*
290 * 5. Print the certificate
291 */
292 printf( " . Peer certificate information ...\n" );
293 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
294 if( ret == -1 )
295 {
296 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
297 goto exit;
298 }
299
300 printf( "%s\n", buf );
301
302 ssl_close_notify( &ssl );
303 }
304 else
305 goto usage;
306
307exit:
308
Paul Bakker1a207ec2011-02-06 13:22:40 +0000309 if( server_fd )
310 net_close( server_fd );
Paul Bakker4fc45522010-03-18 20:11:58 +0000311 x509_free( &clicert );
312 rsa_free( &rsa );
313 ssl_free( &ssl );
314
315 memset( &ssl, 0, sizeof( ssl ) );
316
Paul Bakkercce9d772011-11-18 14:26:47 +0000317#if defined(_WIN32)
Paul Bakker4fc45522010-03-18 20:11:58 +0000318 printf( " + Press Enter to exit this program.\n" );
319 fflush( stdout ); getchar();
320#endif
321
322 return( ret );
323}
Paul Bakker5690efc2011-05-26 13:16:06 +0000324#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
325 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
326 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */