blob: fefb0410bda3c03ac94b142467f473cd36cb4bff [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.
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 Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +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 <stdio.h>
32
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/net.h"
34#include "polarssl/ssl.h"
35#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker757e2502010-02-18 19:29:00 +000037#define SERVER_PORT 4433
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
Paul Bakkereaca51d2010-08-16 12:00:14 +000041#define DEBUG_LEVEL 1
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Paul Bakkerff60ee62010-03-16 21:09:09 +000043void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000044{
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
60 /*
61 * 0. Initialize the RNG and the session data
62 */
63 havege_init( &hs );
64 memset( &ssn, 0, sizeof( ssl_session ) );
Paul Bakker1a207ec2011-02-06 13:22:40 +000065 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67 /*
68 * 1. Start the connection
69 */
70 printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME,
71 SERVER_PORT );
72 fflush( stdout );
73
74 if( ( ret = net_connect( &server_fd, SERVER_NAME,
75 SERVER_PORT ) ) != 0 )
76 {
77 printf( " failed\n ! net_connect returned %d\n\n", ret );
78 goto exit;
79 }
80
81 printf( " ok\n" );
82
83 /*
84 * 2. Setup stuff
85 */
86 printf( " . Setting up the SSL/TLS structure..." );
87 fflush( stdout );
88
89 if( ( ret = ssl_init( &ssl ) ) != 0 )
90 {
91 printf( " failed\n ! ssl_init returned %d\n\n", ret );
92 goto exit;
93 }
94
95 printf( " ok\n" );
96
97 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
98 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
99
100 ssl_set_rng( &ssl, havege_rand, &hs );
101 ssl_set_dbg( &ssl, my_debug, stdout );
102 ssl_set_bio( &ssl, net_recv, &server_fd,
103 net_send, &server_fd );
104
Paul Bakkere3166ce2011-01-27 17:40:50 +0000105 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 ssl_set_session( &ssl, 1, 600, &ssn );
107
108 /*
109 * 3. Write the GET request
110 */
111 printf( " > Write to server:" );
112 fflush( stdout );
113
114 len = sprintf( (char *) buf, GET_REQUEST );
115
116 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
117 {
Paul Bakker831a7552011-05-18 13:32:51 +0000118 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 {
120 printf( " failed\n ! ssl_write returned %d\n\n", ret );
121 goto exit;
122 }
123 }
124
125 len = ret;
126 printf( " %d bytes written\n\n%s", len, (char *) buf );
127
128 /*
129 * 7. Read the HTTP response
130 */
131 printf( " < Read from server:" );
132 fflush( stdout );
133
134 do
135 {
136 len = sizeof( buf ) - 1;
137 memset( buf, 0, sizeof( buf ) );
138 ret = ssl_read( &ssl, buf, len );
139
Paul Bakker831a7552011-05-18 13:32:51 +0000140 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 continue;
142
Paul Bakker40e46942009-01-03 21:51:57 +0000143 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 break;
145
146 if( ret <= 0 )
147 {
148 printf( "failed\n ! ssl_read returned %d\n\n", ret );
149 break;
150 }
151
152 len = ret;
153 printf( " %d bytes read\n\n%s", len, (char *) buf );
154 }
155 while( 0 );
156
157 ssl_close_notify( &ssl );
158
159exit:
160
161 net_close( server_fd );
162 ssl_free( &ssl );
163
164 memset( &ssl, 0, sizeof( ssl ) );
165
166#ifdef WIN32
167 printf( " + Press Enter to exit this program.\n" );
168 fflush( stdout ); getchar();
169#endif
170
171 return( ret );
172}