blob: 66e0c69a102e42bc8cbcc8d993a8c4548d0c78fc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdio.h>
29
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/net.h"
31#include "polarssl/ssl.h"
32#include "polarssl/havege.h"
33#include "polarssl/certs.h"
34#include "polarssl/x509.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker0e6975b2009-02-10 22:19:10 +000036#define SERVER_PORT 4433
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038#define SERVER_NAME "localhost"
39#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#define DEBUG_LEVEL 0
42
43void my_debug( void *ctx, int level, char *str )
44{
45 if( level < DEBUG_LEVEL )
46 {
47 fprintf( (FILE *) ctx, "%s", str );
48 fflush( (FILE *) ctx );
49 }
50}
51
52int main( void )
53{
54 int ret, len, server_fd;
55 unsigned char buf[1024];
56 havege_state hs;
57 ssl_context ssl;
58 ssl_session ssn;
59 x509_cert cacert;
60 x509_cert clicert;
61 rsa_context rsa;
62
63 /*
64 * 0. Initialize the RNG and the session data
65 */
66 havege_init( &hs );
67 memset( &ssn, 0, sizeof( ssl_session ) );
68
69 /*
70 * 1.1. Load the trusted CA
71 */
72 printf( "\n . Loading the CA root certificate ..." );
73 fflush( stdout );
74
75 memset( &cacert, 0, sizeof( x509_cert ) );
76
77 /*
78 * Alternatively, you may load the CA certificates from a .pem or
79 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
80 */
Paul Bakker0e6975b2009-02-10 22:19:10 +000081 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
82 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000083 if( ret != 0 )
84 {
85 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
86 goto exit;
87 }
88
89 printf( " ok\n" );
90
91 /*
92 * 1.2. Load own certificate and private key
93 *
94 * (can be skipped if client authentication is not required)
95 */
96 printf( " . Loading the client cert. and key..." );
97 fflush( stdout );
98
99 memset( &clicert, 0, sizeof( x509_cert ) );
100
101 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
102 strlen( test_cli_crt ) );
103 if( ret != 0 )
104 {
105 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
106 goto exit;
107 }
108
109 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
110 strlen( test_cli_key ), NULL, 0 );
111 if( ret != 0 )
112 {
113 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
114 goto exit;
115 }
116
117 printf( " ok\n" );
118
119 /*
120 * 2. Start the connection
121 */
122 printf( " . Connecting to tcp/%s/%-4d...", SERVER_NAME,
123 SERVER_PORT );
124 fflush( stdout );
125
126 if( ( ret = net_connect( &server_fd, SERVER_NAME,
127 SERVER_PORT ) ) != 0 )
128 {
129 printf( " failed\n ! net_connect returned %d\n\n", ret );
130 goto exit;
131 }
132
133 printf( " ok\n" );
134
135 /*
136 * 3. Setup stuff
137 */
138 printf( " . Setting up the SSL/TLS structure..." );
139 fflush( stdout );
140
141 havege_init( &hs );
142
143 if( ( ret = ssl_init( &ssl ) ) != 0 )
144 {
145 printf( " failed\n ! ssl_init returned %d\n\n", ret );
146 goto exit;
147 }
148
149 printf( " ok\n" );
150
151 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
152 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
153
154 ssl_set_rng( &ssl, havege_rand, &hs );
155 ssl_set_bio( &ssl, net_recv, &server_fd,
156 net_send, &server_fd );
157
158 ssl_set_ciphers( &ssl, ssl_default_ciphers );
159 ssl_set_session( &ssl, 1, 600, &ssn );
160
161 ssl_set_ca_chain( &ssl, &cacert, SERVER_NAME );
162 ssl_set_own_cert( &ssl, &clicert, &rsa );
163
164 ssl_set_hostname( &ssl, SERVER_NAME );
165
166 /*
167 * 4. Handshake
168 */
169 printf( " . Performing the SSL/TLS handshake..." );
170 fflush( stdout );
171
172 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
173 {
Paul Bakker40e46942009-01-03 21:51:57 +0000174 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 {
176 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
177 goto exit;
178 }
179 }
180
181 printf( " ok\n [ Cipher is %s ]\n",
182 ssl_get_cipher( &ssl ) );
183
184 /*
185 * 5. Verify the server certificate
186 */
187 printf( " . Verifying peer X.509 certificate..." );
188
189 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
190 {
191 printf( " failed\n" );
192
193 if( ( ret & BADCERT_EXPIRED ) != 0 )
194 printf( " ! server certificate has expired\n" );
195
196 if( ( ret & BADCERT_REVOKED ) != 0 )
197 printf( " ! server certificate has been revoked\n" );
198
199 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
200 printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
201
202 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
203 printf( " ! self-signed or not signed by a trusted CA\n" );
204
205 printf( "\n" );
206 }
207 else
208 printf( " ok\n" );
209
210 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000211 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
212 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214 /*
215 * 6. Write the GET request
216 */
217 printf( " > Write to server:" );
218 fflush( stdout );
219
220 len = sprintf( (char *) buf, GET_REQUEST );
221
222 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
223 {
Paul Bakker40e46942009-01-03 21:51:57 +0000224 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 {
226 printf( " failed\n ! ssl_write returned %d\n\n", ret );
227 goto exit;
228 }
229 }
230
231 len = ret;
232 printf( " %d bytes written\n\n%s", len, (char *) buf );
233
234 /*
235 * 7. Read the HTTP response
236 */
237 printf( " < Read from server:" );
238 fflush( stdout );
239
240 do
241 {
242 len = sizeof( buf ) - 1;
243 memset( buf, 0, sizeof( buf ) );
244 ret = ssl_read( &ssl, buf, len );
245
Paul Bakker40e46942009-01-03 21:51:57 +0000246 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 continue;
248
Paul Bakker40e46942009-01-03 21:51:57 +0000249 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 break;
251
252 if( ret <= 0 )
253 {
254 printf( "failed\n ! ssl_read returned %d\n\n", ret );
255 break;
256 }
257
258 len = ret;
259 printf( " %d bytes read\n\n%s", len, (char *) buf );
260 }
261 while( 0 );
262
263 ssl_close_notify( &ssl );
264
265exit:
266
267 net_close( server_fd );
268 x509_free( &clicert );
269 x509_free( &cacert );
270 rsa_free( &rsa );
271 ssl_free( &ssl );
272
273 memset( &ssl, 0, sizeof( ssl ) );
274
275#ifdef WIN32
276 printf( " + Press Enter to exit this program.\n" );
277 fflush( stdout ); getchar();
278#endif
279
280 return( ret );
281}