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