blob: 42f4d7efa553b4a50033e35abf79c5a6f8e1f558 [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 Bakker4593aea2009-02-09 22:32:35 +000041#define DEBUG_LEVEL 4
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 ) );
65
66 /*
67 * 1. Start the connection
68 */
69 printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME,
70 SERVER_PORT );
71 fflush( stdout );
72
73 if( ( ret = net_connect( &server_fd, SERVER_NAME,
74 SERVER_PORT ) ) != 0 )
75 {
76 printf( " failed\n ! net_connect returned %d\n\n", ret );
77 goto exit;
78 }
79
80 printf( " ok\n" );
81
82 /*
83 * 2. Setup stuff
84 */
85 printf( " . Setting up the SSL/TLS structure..." );
86 fflush( stdout );
87
88 if( ( ret = ssl_init( &ssl ) ) != 0 )
89 {
90 printf( " failed\n ! ssl_init returned %d\n\n", ret );
91 goto exit;
92 }
93
94 printf( " ok\n" );
95
96 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
97 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
98
99 ssl_set_rng( &ssl, havege_rand, &hs );
100 ssl_set_dbg( &ssl, my_debug, stdout );
101 ssl_set_bio( &ssl, net_recv, &server_fd,
102 net_send, &server_fd );
103
104 ssl_set_ciphers( &ssl, ssl_default_ciphers );
105 ssl_set_session( &ssl, 1, 600, &ssn );
106
107 /*
108 * 3. Write the GET request
109 */
110 printf( " > Write to server:" );
111 fflush( stdout );
112
113 len = sprintf( (char *) buf, GET_REQUEST );
114
115 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
116 {
Paul Bakker40e46942009-01-03 21:51:57 +0000117 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 {
119 printf( " failed\n ! ssl_write returned %d\n\n", ret );
120 goto exit;
121 }
122 }
123
124 len = ret;
125 printf( " %d bytes written\n\n%s", len, (char *) buf );
126
127 /*
128 * 7. Read the HTTP response
129 */
130 printf( " < Read from server:" );
131 fflush( stdout );
132
133 do
134 {
135 len = sizeof( buf ) - 1;
136 memset( buf, 0, sizeof( buf ) );
137 ret = ssl_read( &ssl, buf, len );
138
Paul Bakker40e46942009-01-03 21:51:57 +0000139 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 continue;
141
Paul Bakker40e46942009-01-03 21:51:57 +0000142 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 break;
144
145 if( ret <= 0 )
146 {
147 printf( "failed\n ! ssl_read returned %d\n\n", ret );
148 break;
149 }
150
151 len = ret;
152 printf( " %d bytes read\n\n%s", len, (char *) buf );
153 }
154 while( 0 );
155
156 ssl_close_notify( &ssl );
157
158exit:
159
160 net_close( server_fd );
161 ssl_free( &ssl );
162
163 memset( &ssl, 0, sizeof( ssl ) );
164
165#ifdef WIN32
166 printf( " + Press Enter to exit this program.\n" );
167 fflush( stdout ); getchar();
168#endif
169
170 return( ret );
171}