blob: e33c9a7b077d926adfda9884cd4ac479f11fa6cf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client with certificate authentication
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
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 <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/net.h"
32#include "polarssl/ssl.h"
33#include "polarssl/havege.h"
34#include "polarssl/certs.h"
35#include "polarssl/x509.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker0e6975b2009-02-10 22:19:10 +000037#define SERVER_PORT 4433
38
Paul Bakker5121ce52009-01-03 21:22:43 +000039#define SERVER_NAME "localhost"
40#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42#define DEBUG_LEVEL 0
43
44void my_debug( void *ctx, int level, char *str )
45{
46 if( level < DEBUG_LEVEL )
47 {
48 fprintf( (FILE *) ctx, "%s", str );
49 fflush( (FILE *) ctx );
50 }
51}
52
53int main( void )
54{
55 int ret, len, server_fd;
56 unsigned char buf[1024];
57 havege_state hs;
58 ssl_context ssl;
59 ssl_session ssn;
60 x509_cert cacert;
61 x509_cert clicert;
62 rsa_context rsa;
63
64 /*
65 * 0. Initialize the RNG and the session data
66 */
67 havege_init( &hs );
68 memset( &ssn, 0, sizeof( ssl_session ) );
69
70 /*
71 * 1.1. Load the trusted CA
72 */
73 printf( "\n . Loading the CA root certificate ..." );
74 fflush( stdout );
75
76 memset( &cacert, 0, sizeof( x509_cert ) );
77
78 /*
79 * Alternatively, you may load the CA certificates from a .pem or
80 * .crt file by calling x509parse_crtfile( &cacert, "myca.crt" ).
81 */
Paul Bakker0e6975b2009-02-10 22:19:10 +000082 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
83 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000084 if( ret != 0 )
85 {
86 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
87 goto exit;
88 }
89
90 printf( " ok\n" );
91
92 /*
93 * 1.2. Load own certificate and private key
94 *
95 * (can be skipped if client authentication is not required)
96 */
97 printf( " . Loading the client cert. and key..." );
98 fflush( stdout );
99
100 memset( &clicert, 0, sizeof( x509_cert ) );
101
102 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
103 strlen( test_cli_crt ) );
104 if( ret != 0 )
105 {
106 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
107 goto exit;
108 }
109
110 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
111 strlen( test_cli_key ), NULL, 0 );
112 if( ret != 0 )
113 {
114 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
115 goto exit;
116 }
117
118 printf( " ok\n" );
119
120 /*
121 * 2. Start the connection
122 */
123 printf( " . Connecting to tcp/%s/%-4d...", SERVER_NAME,
124 SERVER_PORT );
125 fflush( stdout );
126
127 if( ( ret = net_connect( &server_fd, SERVER_NAME,
128 SERVER_PORT ) ) != 0 )
129 {
130 printf( " failed\n ! net_connect returned %d\n\n", ret );
131 goto exit;
132 }
133
134 printf( " ok\n" );
135
136 /*
137 * 3. Setup stuff
138 */
139 printf( " . Setting up the SSL/TLS structure..." );
140 fflush( stdout );
141
142 havege_init( &hs );
143
144 if( ( ret = ssl_init( &ssl ) ) != 0 )
145 {
146 printf( " failed\n ! ssl_init returned %d\n\n", ret );
147 goto exit;
148 }
149
150 printf( " ok\n" );
151
152 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
153 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
154
155 ssl_set_rng( &ssl, havege_rand, &hs );
156 ssl_set_bio( &ssl, net_recv, &server_fd,
157 net_send, &server_fd );
158
159 ssl_set_ciphers( &ssl, ssl_default_ciphers );
160 ssl_set_session( &ssl, 1, 600, &ssn );
161
Paul Bakker40ea7de2009-05-03 10:18:48 +0000162 ssl_set_ca_chain( &ssl, &cacert, NULL, SERVER_NAME );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 ssl_set_own_cert( &ssl, &clicert, &rsa );
164
165 ssl_set_hostname( &ssl, SERVER_NAME );
166
167 /*
168 * 4. Handshake
169 */
170 printf( " . Performing the SSL/TLS handshake..." );
171 fflush( stdout );
172
173 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
174 {
Paul Bakker40e46942009-01-03 21:51:57 +0000175 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 {
177 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
178 goto exit;
179 }
180 }
181
182 printf( " ok\n [ Cipher is %s ]\n",
183 ssl_get_cipher( &ssl ) );
184
185 /*
186 * 5. Verify the server certificate
187 */
188 printf( " . Verifying peer X.509 certificate..." );
189
190 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
191 {
192 printf( " failed\n" );
193
194 if( ( ret & BADCERT_EXPIRED ) != 0 )
195 printf( " ! server certificate has expired\n" );
196
197 if( ( ret & BADCERT_REVOKED ) != 0 )
198 printf( " ! server certificate has been revoked\n" );
199
200 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
201 printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
202
203 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
204 printf( " ! self-signed or not signed by a trusted CA\n" );
205
206 printf( "\n" );
207 }
208 else
209 printf( " ok\n" );
210
211 printf( " . Peer certificate information ...\n" );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000212 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl.peer_cert );
213 printf( "%s\n", buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 /*
216 * 6. Write the GET request
217 */
218 printf( " > Write to server:" );
219 fflush( stdout );
220
221 len = sprintf( (char *) buf, GET_REQUEST );
222
223 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
224 {
Paul Bakker40e46942009-01-03 21:51:57 +0000225 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 {
227 printf( " failed\n ! ssl_write returned %d\n\n", ret );
228 goto exit;
229 }
230 }
231
232 len = ret;
233 printf( " %d bytes written\n\n%s", len, (char *) buf );
234
235 /*
236 * 7. Read the HTTP response
237 */
238 printf( " < Read from server:" );
239 fflush( stdout );
240
241 do
242 {
243 len = sizeof( buf ) - 1;
244 memset( buf, 0, sizeof( buf ) );
245 ret = ssl_read( &ssl, buf, len );
246
Paul Bakker40e46942009-01-03 21:51:57 +0000247 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 continue;
249
Paul Bakker40e46942009-01-03 21:51:57 +0000250 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 break;
252
253 if( ret <= 0 )
254 {
255 printf( "failed\n ! ssl_read returned %d\n\n", ret );
256 break;
257 }
258
259 len = ret;
260 printf( " %d bytes read\n\n%s", len, (char *) buf );
261 }
262 while( 0 );
263
264 ssl_close_notify( &ssl );
265
266exit:
267
268 net_close( server_fd );
269 x509_free( &clicert );
270 x509_free( &cacert );
271 rsa_free( &rsa );
272 ssl_free( &ssl );
273
274 memset( &ssl, 0, sizeof( ssl ) );
275
276#ifdef WIN32
277 printf( " + Press Enter to exit this program.\n" );
278 fflush( stdout ); getchar();
279#endif
280
281 return( ret );
282}