blob: 1350fc14b6144c7bd783a9af3eb349e5c9be5882 [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
50
51/*
52 * global options
53 */
54struct options
55{
56 int mode; /* the mode to run the application in */
57 char *filename; /* filename of the certificate file */
58 char *server_name; /* hostname of the server (client only) */
59 int server_port; /* port on which the ssl service runs */
60 int debug_level; /* level of debugging */
61} opt;
62
63void my_debug( void *ctx, int level, const char *str )
64{
65 if( level < opt.debug_level )
66 {
67 fprintf( (FILE *) ctx, "%s", str );
68 fflush( (FILE *) ctx );
69 }
70}
71
72#define USAGE \
73 "\n usage: cert_app param=<>...\n" \
74 "\n acceptable parameters:\n" \
75 " mode=file|ssl default: none\n" \
76 " filename=%%s default: cert.crt\n" \
77 " server_name=%%s default: localhost\n" \
78 " server_port=%%d default: 4433\n" \
79 " debug_level=%%d default: 0 (disabled)\n" \
80 "\n"
81
Paul Bakker5690efc2011-05-26 13:16:06 +000082#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
83 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
84 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
85 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000086int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000087{
Paul Bakkercce9d772011-11-18 14:26:47 +000088 ((void) argc);
89 ((void) argv);
90
Paul Bakker5690efc2011-05-26 13:16:06 +000091 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
92 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
93 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
94 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
95 return( 0 );
96}
97#else
Paul Bakker4fc45522010-03-18 20:11:58 +000098int main( int argc, char *argv[] )
99{
100 int ret = 0, server_fd;
101 unsigned char buf[1024];
102 havege_state hs;
103 ssl_context ssl;
104 ssl_session ssn;
105 x509_cert clicert;
106 rsa_context rsa;
107 int i, j, n;
108 char *p, *q;
109
Paul Bakker1a207ec2011-02-06 13:22:40 +0000110 /*
111 * Set to sane values
112 */
113 server_fd = 0;
114 memset( &ssl, 0, sizeof( ssl_context ) );
115 memset( &ssn, 0, sizeof( ssl_session ) );
116 memset( &clicert, 0, sizeof( x509_cert ) );
117 memset( &rsa, 0, sizeof( rsa_context ) );
118
Paul Bakker4fc45522010-03-18 20:11:58 +0000119 if( argc == 0 )
120 {
121 usage:
122 printf( USAGE );
123 goto exit;
124 }
125
126 opt.mode = DFL_MODE;
127 opt.filename = DFL_FILENAME;
128 opt.server_name = DFL_SERVER_NAME;
129 opt.server_port = DFL_SERVER_PORT;
130 opt.debug_level = DFL_DEBUG_LEVEL;
131
132 for( i = 1; i < argc; i++ )
133 {
134 n = strlen( argv[i] );
135
136 for( j = 0; j < n; j++ )
137 {
138 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
139 argv[i][j] |= 0x20;
140 }
141
142 p = argv[i];
143 if( ( q = strchr( p, '=' ) ) == NULL )
144 goto usage;
145 *q++ = '\0';
146
147 if( strcmp( p, "mode" ) == 0 )
148 {
149 if( strcmp( q, "file" ) == 0 )
150 opt.mode = MODE_FILE;
151 else if( strcmp( q, "ssl" ) == 0 )
152 opt.mode = MODE_SSL;
153 else
154 goto usage;
155 }
156 else if( strcmp( p, "filename" ) == 0 )
157 opt.filename = q;
158 else if( strcmp( p, "server_name" ) == 0 )
159 opt.server_name = q;
160 else if( strcmp( p, "server_port" ) == 0 )
161 {
162 opt.server_port = atoi( q );
163 if( opt.server_port < 1 || opt.server_port > 65535 )
164 goto usage;
165 }
166 else if( strcmp( p, "debug_level" ) == 0 )
167 {
168 opt.debug_level = atoi( q );
169 if( opt.debug_level < 0 || opt.debug_level > 65535 )
170 goto usage;
171 }
172 else
173 goto usage;
174 }
175
176 if( opt.mode == MODE_FILE )
177 {
178 x509_cert crt;
Paul Bakker14cb63a2011-11-25 12:44:31 +0000179 x509_cert *cur = &crt;
Paul Bakker4fc45522010-03-18 20:11:58 +0000180 memset( &crt, 0, sizeof( x509_cert ) );
181
182 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000183 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000184 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000185 printf( "\n . Loading the certificate(s) ..." );
Paul Bakker4fc45522010-03-18 20:11:58 +0000186 fflush( stdout );
187
188 ret = x509parse_crtfile( &crt, opt.filename );
189
190 if( ret != 0 )
191 {
192 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
193 x509_free( &crt );
194 goto exit;
195 }
196
197 printf( " ok\n" );
198
199 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000200 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000201 */
Paul Bakker14cb63a2011-11-25 12:44:31 +0000202 while( cur != NULL )
Paul Bakker4fc45522010-03-18 20:11:58 +0000203 {
Paul Bakker14cb63a2011-11-25 12:44:31 +0000204 printf( " . Peer certificate information ...\n" );
Paul Bakker5c356d62011-11-25 13:17:45 +0000205 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", cur );
Paul Bakker14cb63a2011-11-25 12:44:31 +0000206 if( ret == -1 )
207 {
208 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
209 x509_free( &crt );
210 goto exit;
211 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000212
Paul Bakker14cb63a2011-11-25 12:44:31 +0000213 printf( "%s\n", buf );
214
215 cur = cur->next;
216 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000217
218 x509_free( &crt );
219 }
220 else if( opt.mode == MODE_SSL )
221 {
222 /*
223 * 1. Initialize the RNG and the session data
224 */
225 havege_init( &hs );
Paul Bakker4fc45522010-03-18 20:11:58 +0000226
227 /*
228 * 2. Start the connection
229 */
230 printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
231 opt.server_port );
232 fflush( stdout );
233
234 if( ( ret = net_connect( &server_fd, opt.server_name,
235 opt.server_port ) ) != 0 )
236 {
237 printf( " failed\n ! net_connect returned %d\n\n", ret );
238 goto exit;
239 }
240
241 /*
242 * 3. Setup stuff
243 */
244 if( ( ret = ssl_init( &ssl ) ) != 0 )
245 {
246 printf( " failed\n ! ssl_init returned %d\n\n", ret );
247 goto exit;
248 }
249
250 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
251 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
252
253 ssl_set_rng( &ssl, havege_rand, &hs );
254 ssl_set_dbg( &ssl, my_debug, stdout );
255 ssl_set_bio( &ssl, net_recv, &server_fd,
256 net_send, &server_fd );
257
Paul Bakkere3166ce2011-01-27 17:40:50 +0000258 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
Paul Bakker4fc45522010-03-18 20:11:58 +0000259 ssl_set_session( &ssl, 1, 600, &ssn );
260
261 ssl_set_own_cert( &ssl, &clicert, &rsa );
262
263 ssl_set_hostname( &ssl, opt.server_name );
264
265 /*
266 * 4. Handshake
267 */
268 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
269 {
Paul Bakker831a7552011-05-18 13:32:51 +0000270 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000271 {
272 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
273 goto exit;
274 }
275 }
276
277 printf( " ok\n" );
278
279 /*
280 * 5. Print the certificate
281 */
282 printf( " . Peer certificate information ...\n" );
283 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
284 if( ret == -1 )
285 {
286 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
287 goto exit;
288 }
289
290 printf( "%s\n", buf );
291
292 ssl_close_notify( &ssl );
293 }
294 else
295 goto usage;
296
297exit:
298
Paul Bakker1a207ec2011-02-06 13:22:40 +0000299 if( server_fd )
300 net_close( server_fd );
Paul Bakker4fc45522010-03-18 20:11:58 +0000301 x509_free( &clicert );
302 rsa_free( &rsa );
303 ssl_free( &ssl );
304
305 memset( &ssl, 0, sizeof( ssl ) );
306
Paul Bakkercce9d772011-11-18 14:26:47 +0000307#if defined(_WIN32)
Paul Bakker4fc45522010-03-18 20:11:58 +0000308 printf( " + Press Enter to exit this program.\n" );
309 fflush( stdout ); getchar();
310#endif
311
312 return( ret );
313}
Paul Bakker5690efc2011-05-26 13:16:06 +0000314#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
315 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
316 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */