blob: 5036a7e4df915bae31866d510c28a031c3d99602 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker757e2502010-02-18 19:29:00 +000034#define SERVER_PORT 4433
Paul Bakker5121ce52009-01-03 21:22:43 +000035#define SERVER_NAME "localhost"
36#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker4593aea2009-02-09 22:32:35 +000038#define DEBUG_LEVEL 4
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakkerff60ee62010-03-16 21:09:09 +000040void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000041{
42 if( level < DEBUG_LEVEL )
43 {
44 fprintf( (FILE *) ctx, "%s", str );
45 fflush( (FILE *) ctx );
46 }
47}
48
49int main( void )
50{
51 int ret, len, server_fd;
52 unsigned char buf[1024];
53 havege_state hs;
54 ssl_context ssl;
55 ssl_session ssn;
56
57 /*
58 * 0. Initialize the RNG and the session data
59 */
60 havege_init( &hs );
61 memset( &ssn, 0, sizeof( ssl_session ) );
62
63 /*
64 * 1. Start the connection
65 */
66 printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME,
67 SERVER_PORT );
68 fflush( stdout );
69
70 if( ( ret = net_connect( &server_fd, SERVER_NAME,
71 SERVER_PORT ) ) != 0 )
72 {
73 printf( " failed\n ! net_connect returned %d\n\n", ret );
74 goto exit;
75 }
76
77 printf( " ok\n" );
78
79 /*
80 * 2. Setup stuff
81 */
82 printf( " . Setting up the SSL/TLS structure..." );
83 fflush( stdout );
84
85 if( ( ret = ssl_init( &ssl ) ) != 0 )
86 {
87 printf( " failed\n ! ssl_init returned %d\n\n", ret );
88 goto exit;
89 }
90
91 printf( " ok\n" );
92
93 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
94 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
95
96 ssl_set_rng( &ssl, havege_rand, &hs );
97 ssl_set_dbg( &ssl, my_debug, stdout );
98 ssl_set_bio( &ssl, net_recv, &server_fd,
99 net_send, &server_fd );
100
101 ssl_set_ciphers( &ssl, ssl_default_ciphers );
102 ssl_set_session( &ssl, 1, 600, &ssn );
103
104 /*
105 * 3. Write the GET request
106 */
107 printf( " > Write to server:" );
108 fflush( stdout );
109
110 len = sprintf( (char *) buf, GET_REQUEST );
111
112 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
113 {
Paul Bakker40e46942009-01-03 21:51:57 +0000114 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 {
116 printf( " failed\n ! ssl_write returned %d\n\n", ret );
117 goto exit;
118 }
119 }
120
121 len = ret;
122 printf( " %d bytes written\n\n%s", len, (char *) buf );
123
124 /*
125 * 7. Read the HTTP response
126 */
127 printf( " < Read from server:" );
128 fflush( stdout );
129
130 do
131 {
132 len = sizeof( buf ) - 1;
133 memset( buf, 0, sizeof( buf ) );
134 ret = ssl_read( &ssl, buf, len );
135
Paul Bakker40e46942009-01-03 21:51:57 +0000136 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 continue;
138
Paul Bakker40e46942009-01-03 21:51:57 +0000139 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 break;
141
142 if( ret <= 0 )
143 {
144 printf( "failed\n ! ssl_read returned %d\n\n", ret );
145 break;
146 }
147
148 len = ret;
149 printf( " %d bytes read\n\n%s", len, (char *) buf );
150 }
151 while( 0 );
152
153 ssl_close_notify( &ssl );
154
155exit:
156
157 net_close( server_fd );
158 ssl_free( &ssl );
159
160 memset( &ssl, 0, sizeof( ssl ) );
161
162#ifdef WIN32
163 printf( " + Press Enter to exit this program.\n" );
164 fflush( stdout ); getchar();
165#endif
166
167 return( ret );
168}