blob: 1ded9dd7869dfa42af7948e58934ec7dd95c8597 [file] [log] [blame]
Paul Bakker4fc45522010-03-18 20:11:58 +00001/*
2 * Certificate reading application
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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)
86int main( void )
87{
88 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
89 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
90 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
91 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
92 return( 0 );
93}
94#else
Paul Bakker4fc45522010-03-18 20:11:58 +000095int main( int argc, char *argv[] )
96{
97 int ret = 0, server_fd;
98 unsigned char buf[1024];
99 havege_state hs;
100 ssl_context ssl;
101 ssl_session ssn;
102 x509_cert clicert;
103 rsa_context rsa;
104 int i, j, n;
105 char *p, *q;
106
Paul Bakker1a207ec2011-02-06 13:22:40 +0000107 /*
108 * Set to sane values
109 */
110 server_fd = 0;
111 memset( &ssl, 0, sizeof( ssl_context ) );
112 memset( &ssn, 0, sizeof( ssl_session ) );
113 memset( &clicert, 0, sizeof( x509_cert ) );
114 memset( &rsa, 0, sizeof( rsa_context ) );
115
Paul Bakker4fc45522010-03-18 20:11:58 +0000116 if( argc == 0 )
117 {
118 usage:
119 printf( USAGE );
120 goto exit;
121 }
122
123 opt.mode = DFL_MODE;
124 opt.filename = DFL_FILENAME;
125 opt.server_name = DFL_SERVER_NAME;
126 opt.server_port = DFL_SERVER_PORT;
127 opt.debug_level = DFL_DEBUG_LEVEL;
128
129 for( i = 1; i < argc; i++ )
130 {
131 n = strlen( argv[i] );
132
133 for( j = 0; j < n; j++ )
134 {
135 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
136 argv[i][j] |= 0x20;
137 }
138
139 p = argv[i];
140 if( ( q = strchr( p, '=' ) ) == NULL )
141 goto usage;
142 *q++ = '\0';
143
144 if( strcmp( p, "mode" ) == 0 )
145 {
146 if( strcmp( q, "file" ) == 0 )
147 opt.mode = MODE_FILE;
148 else if( strcmp( q, "ssl" ) == 0 )
149 opt.mode = MODE_SSL;
150 else
151 goto usage;
152 }
153 else if( strcmp( p, "filename" ) == 0 )
154 opt.filename = q;
155 else if( strcmp( p, "server_name" ) == 0 )
156 opt.server_name = q;
157 else if( strcmp( p, "server_port" ) == 0 )
158 {
159 opt.server_port = atoi( q );
160 if( opt.server_port < 1 || opt.server_port > 65535 )
161 goto usage;
162 }
163 else if( strcmp( p, "debug_level" ) == 0 )
164 {
165 opt.debug_level = atoi( q );
166 if( opt.debug_level < 0 || opt.debug_level > 65535 )
167 goto usage;
168 }
169 else
170 goto usage;
171 }
172
173 if( opt.mode == MODE_FILE )
174 {
175 x509_cert crt;
Paul Bakker4fc45522010-03-18 20:11:58 +0000176 memset( &crt, 0, sizeof( x509_cert ) );
177
178 /*
179 * 1.1. Load the certificate
180 */
181 printf( "\n . Loading the certificate ..." );
182 fflush( stdout );
183
184 ret = x509parse_crtfile( &crt, opt.filename );
185
186 if( ret != 0 )
187 {
188 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
189 x509_free( &crt );
190 goto exit;
191 }
192
193 printf( " ok\n" );
194
195 /*
196 * 1.2 Print the certificate
197 */
198 printf( " . Peer certificate information ...\n" );
199 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", &crt );
200 if( ret == -1 )
201 {
202 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
203 x509_free( &crt );
204 goto exit;
205 }
206
207 printf( "%s\n", buf );
208
209 x509_free( &crt );
210 }
211 else if( opt.mode == MODE_SSL )
212 {
213 /*
214 * 1. Initialize the RNG and the session data
215 */
216 havege_init( &hs );
Paul Bakker4fc45522010-03-18 20:11:58 +0000217
218 /*
219 * 2. Start the connection
220 */
221 printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
222 opt.server_port );
223 fflush( stdout );
224
225 if( ( ret = net_connect( &server_fd, opt.server_name,
226 opt.server_port ) ) != 0 )
227 {
228 printf( " failed\n ! net_connect returned %d\n\n", ret );
229 goto exit;
230 }
231
232 /*
233 * 3. Setup stuff
234 */
235 if( ( ret = ssl_init( &ssl ) ) != 0 )
236 {
237 printf( " failed\n ! ssl_init returned %d\n\n", ret );
238 goto exit;
239 }
240
241 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
242 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
243
244 ssl_set_rng( &ssl, havege_rand, &hs );
245 ssl_set_dbg( &ssl, my_debug, stdout );
246 ssl_set_bio( &ssl, net_recv, &server_fd,
247 net_send, &server_fd );
248
Paul Bakkere3166ce2011-01-27 17:40:50 +0000249 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
Paul Bakker4fc45522010-03-18 20:11:58 +0000250 ssl_set_session( &ssl, 1, 600, &ssn );
251
252 ssl_set_own_cert( &ssl, &clicert, &rsa );
253
254 ssl_set_hostname( &ssl, opt.server_name );
255
256 /*
257 * 4. Handshake
258 */
259 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
260 {
Paul Bakker831a7552011-05-18 13:32:51 +0000261 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker4fc45522010-03-18 20:11:58 +0000262 {
263 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
264 goto exit;
265 }
266 }
267
268 printf( " ok\n" );
269
270 /*
271 * 5. Print the certificate
272 */
273 printf( " . Peer certificate information ...\n" );
274 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
275 if( ret == -1 )
276 {
277 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
278 goto exit;
279 }
280
281 printf( "%s\n", buf );
282
283 ssl_close_notify( &ssl );
284 }
285 else
286 goto usage;
287
288exit:
289
Paul Bakker1a207ec2011-02-06 13:22:40 +0000290 if( server_fd )
291 net_close( server_fd );
Paul Bakker4fc45522010-03-18 20:11:58 +0000292 x509_free( &clicert );
293 rsa_free( &rsa );
294 ssl_free( &ssl );
295
296 memset( &ssl, 0, sizeof( ssl ) );
297
298#ifdef WIN32
299 printf( " + Press Enter to exit this program.\n" );
300 fflush( stdout ); getchar();
301#endif
302
303 return( ret );
304}
Paul Bakker5690efc2011-05-26 13:16:06 +0000305#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
306 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
307 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */