blob: 1df51988abd024519c139b0c64b6909ed6519c37 [file] [log] [blame]
Paul Bakker4fc45522010-03-18 20:11:58 +00001/*
2 * Certificate reading application
3 *
4 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
6 *
7 * Joined copyright on original XySSL code with: Christophe Devine
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
32#include "polarssl/havege.h"
33#include "polarssl/net.h"
34#include "polarssl/ssl.h"
35#include "polarssl/x509.h"
36
37#define MODE_NONE 0
38#define MODE_FILE 1
39#define MODE_SSL 2
40
41#define DFL_MODE MODE_NONE
42#define DFL_FILENAME "cert.crt"
43#define DFL_SERVER_NAME "localhost"
44#define DFL_SERVER_PORT 4433
45#define DFL_DEBUG_LEVEL 0
46
47/*
48 * global options
49 */
50struct options
51{
52 int mode; /* the mode to run the application in */
53 char *filename; /* filename of the certificate file */
54 char *server_name; /* hostname of the server (client only) */
55 int server_port; /* port on which the ssl service runs */
56 int debug_level; /* level of debugging */
57} opt;
58
59void my_debug( void *ctx, int level, const char *str )
60{
61 if( level < opt.debug_level )
62 {
63 fprintf( (FILE *) ctx, "%s", str );
64 fflush( (FILE *) ctx );
65 }
66}
67
68#define USAGE \
69 "\n usage: cert_app param=<>...\n" \
70 "\n acceptable parameters:\n" \
71 " mode=file|ssl default: none\n" \
72 " filename=%%s default: cert.crt\n" \
73 " server_name=%%s default: localhost\n" \
74 " server_port=%%d default: 4433\n" \
75 " debug_level=%%d default: 0 (disabled)\n" \
76 "\n"
77
78int main( int argc, char *argv[] )
79{
80 int ret = 0, server_fd;
81 unsigned char buf[1024];
82 havege_state hs;
83 ssl_context ssl;
84 ssl_session ssn;
85 x509_cert clicert;
86 rsa_context rsa;
87 int i, j, n;
88 char *p, *q;
89
90 if( argc == 0 )
91 {
92 usage:
93 printf( USAGE );
94 goto exit;
95 }
96
97 opt.mode = DFL_MODE;
98 opt.filename = DFL_FILENAME;
99 opt.server_name = DFL_SERVER_NAME;
100 opt.server_port = DFL_SERVER_PORT;
101 opt.debug_level = DFL_DEBUG_LEVEL;
102
103 for( i = 1; i < argc; i++ )
104 {
105 n = strlen( argv[i] );
106
107 for( j = 0; j < n; j++ )
108 {
109 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
110 argv[i][j] |= 0x20;
111 }
112
113 p = argv[i];
114 if( ( q = strchr( p, '=' ) ) == NULL )
115 goto usage;
116 *q++ = '\0';
117
118 if( strcmp( p, "mode" ) == 0 )
119 {
120 if( strcmp( q, "file" ) == 0 )
121 opt.mode = MODE_FILE;
122 else if( strcmp( q, "ssl" ) == 0 )
123 opt.mode = MODE_SSL;
124 else
125 goto usage;
126 }
127 else if( strcmp( p, "filename" ) == 0 )
128 opt.filename = q;
129 else if( strcmp( p, "server_name" ) == 0 )
130 opt.server_name = q;
131 else if( strcmp( p, "server_port" ) == 0 )
132 {
133 opt.server_port = atoi( q );
134 if( opt.server_port < 1 || opt.server_port > 65535 )
135 goto usage;
136 }
137 else if( strcmp( p, "debug_level" ) == 0 )
138 {
139 opt.debug_level = atoi( q );
140 if( opt.debug_level < 0 || opt.debug_level > 65535 )
141 goto usage;
142 }
143 else
144 goto usage;
145 }
146
147 if( opt.mode == MODE_FILE )
148 {
149 x509_cert crt;
150
151 memset( &crt, 0, sizeof( x509_cert ) );
152
153 /*
154 * 1.1. Load the certificate
155 */
156 printf( "\n . Loading the certificate ..." );
157 fflush( stdout );
158
159 ret = x509parse_crtfile( &crt, opt.filename );
160
161 if( ret != 0 )
162 {
163 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
164 x509_free( &crt );
165 goto exit;
166 }
167
168 printf( " ok\n" );
169
170 /*
171 * 1.2 Print the certificate
172 */
173 printf( " . Peer certificate information ...\n" );
174 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", &crt );
175 if( ret == -1 )
176 {
177 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
178 x509_free( &crt );
179 goto exit;
180 }
181
182 printf( "%s\n", buf );
183
184 x509_free( &crt );
185 }
186 else if( opt.mode == MODE_SSL )
187 {
188 /*
189 * 1. Initialize the RNG and the session data
190 */
191 havege_init( &hs );
192 memset( &ssn, 0, sizeof( ssl_session ) );
193
194 /*
195 * 2. Start the connection
196 */
197 printf( " . SSL connection to tcp/%s/%-4d...", opt.server_name,
198 opt.server_port );
199 fflush( stdout );
200
201 if( ( ret = net_connect( &server_fd, opt.server_name,
202 opt.server_port ) ) != 0 )
203 {
204 printf( " failed\n ! net_connect returned %d\n\n", ret );
205 goto exit;
206 }
207
208 /*
209 * 3. Setup stuff
210 */
211 if( ( ret = ssl_init( &ssl ) ) != 0 )
212 {
213 printf( " failed\n ! ssl_init returned %d\n\n", ret );
214 goto exit;
215 }
216
217 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
218 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
219
220 ssl_set_rng( &ssl, havege_rand, &hs );
221 ssl_set_dbg( &ssl, my_debug, stdout );
222 ssl_set_bio( &ssl, net_recv, &server_fd,
223 net_send, &server_fd );
224
225 ssl_set_ciphers( &ssl, ssl_default_ciphers );
226 ssl_set_session( &ssl, 1, 600, &ssn );
227
228 ssl_set_own_cert( &ssl, &clicert, &rsa );
229
230 ssl_set_hostname( &ssl, opt.server_name );
231
232 /*
233 * 4. Handshake
234 */
235 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
236 {
237 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
238 {
239 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
240 goto exit;
241 }
242 }
243
244 printf( " ok\n" );
245
246 /*
247 * 5. Print the certificate
248 */
249 printf( " . Peer certificate information ...\n" );
250 ret = x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
251 if( ret == -1 )
252 {
253 printf( " failed\n ! x509parse_cert_info returned %d\n\n", ret );
254 goto exit;
255 }
256
257 printf( "%s\n", buf );
258
259 ssl_close_notify( &ssl );
260 }
261 else
262 goto usage;
263
264exit:
265
266 net_close( server_fd );
267 x509_free( &clicert );
268 rsa_free( &rsa );
269 ssl_free( &ssl );
270
271 memset( &ssl, 0, sizeof( ssl ) );
272
273#ifdef WIN32
274 printf( " + Press Enter to exit this program.\n" );
275 fflush( stdout ); getchar();
276#endif
277
278 return( ret );
279}