blob: 8b575def988ab84ec9c428272d8905195bc43f2d [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
35#define SERVER_PORT 443
36/*
37#define SERVER_NAME "localhost"
38#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
39*/
Paul Bakker4593aea2009-02-09 22:32:35 +000040#define SERVER_NAME "mijn.postbank.nl"
Paul Bakker5121ce52009-01-03 21:22:43 +000041#define GET_REQUEST \
42 "GET /hello/ HTTP/1.1\r\n" \
Paul Bakker4593aea2009-02-09 22:32:35 +000043 "Host: mijn.postbank.nl\r\n\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakker4593aea2009-02-09 22:32:35 +000045#define DEBUG_LEVEL 4
Paul Bakker5121ce52009-01-03 21:22:43 +000046
47void my_debug( void *ctx, int level, char *str )
48{
49 if( level < DEBUG_LEVEL )
50 {
51 fprintf( (FILE *) ctx, "%s", str );
52 fflush( (FILE *) ctx );
53 }
54}
55
56int main( void )
57{
58 int ret, len, server_fd;
59 unsigned char buf[1024];
60 havege_state hs;
61 ssl_context ssl;
62 ssl_session ssn;
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. Start the connection
72 */
73 printf( "\n . Connecting to tcp/%s/%4d...", SERVER_NAME,
74 SERVER_PORT );
75 fflush( stdout );
76
77 if( ( ret = net_connect( &server_fd, SERVER_NAME,
78 SERVER_PORT ) ) != 0 )
79 {
80 printf( " failed\n ! net_connect returned %d\n\n", ret );
81 goto exit;
82 }
83
84 printf( " ok\n" );
85
86 /*
87 * 2. Setup stuff
88 */
89 printf( " . Setting up the SSL/TLS structure..." );
90 fflush( stdout );
91
92 if( ( ret = ssl_init( &ssl ) ) != 0 )
93 {
94 printf( " failed\n ! ssl_init returned %d\n\n", ret );
95 goto exit;
96 }
97
98 printf( " ok\n" );
99
100 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
101 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
102
103 ssl_set_rng( &ssl, havege_rand, &hs );
104 ssl_set_dbg( &ssl, my_debug, stdout );
105 ssl_set_bio( &ssl, net_recv, &server_fd,
106 net_send, &server_fd );
107
108 ssl_set_ciphers( &ssl, ssl_default_ciphers );
109 ssl_set_session( &ssl, 1, 600, &ssn );
110
111 /*
112 * 3. Write the GET request
113 */
114 printf( " > Write to server:" );
115 fflush( stdout );
116
117 len = sprintf( (char *) buf, GET_REQUEST );
118
119 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
120 {
Paul Bakker40e46942009-01-03 21:51:57 +0000121 if( ret != POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 {
123 printf( " failed\n ! ssl_write returned %d\n\n", ret );
124 goto exit;
125 }
126 }
127
128 len = ret;
129 printf( " %d bytes written\n\n%s", len, (char *) buf );
130
131 /*
132 * 7. Read the HTTP response
133 */
134 printf( " < Read from server:" );
135 fflush( stdout );
136
137 do
138 {
139 len = sizeof( buf ) - 1;
140 memset( buf, 0, sizeof( buf ) );
141 ret = ssl_read( &ssl, buf, len );
142
Paul Bakker40e46942009-01-03 21:51:57 +0000143 if( ret == POLARSSL_ERR_NET_TRY_AGAIN )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 continue;
145
Paul Bakker40e46942009-01-03 21:51:57 +0000146 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 break;
148
149 if( ret <= 0 )
150 {
151 printf( "failed\n ! ssl_read returned %d\n\n", ret );
152 break;
153 }
154
155 len = ret;
156 printf( " %d bytes read\n\n%s", len, (char *) buf );
157 }
158 while( 0 );
159
160 ssl_close_notify( &ssl );
161
162exit:
163
164 net_close( server_fd );
165 ssl_free( &ssl );
166
167 memset( &ssl, 0, sizeof( ssl ) );
168
169#ifdef WIN32
170 printf( " + Press Enter to exit this program.\n" );
171 fflush( stdout ); getchar();
172#endif
173
174 return( ret );
175}