Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client demonstration program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * 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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 34 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 37 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) &&\ |
| 38 | defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_CLI_C) &&\ |
| 39 | defined(POLARSSL_NET_C) && defined(POLARSSL_RSA_C) &&\ |
| 40 | defined(POLARSSL_CTR_DRBG_C) && defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 41 | #include "polarssl/net.h" |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 42 | #include "polarssl/debug.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 43 | #include "polarssl/ssl.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 44 | #include "polarssl/entropy.h" |
| 45 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 46 | #include "polarssl/error.h" |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 47 | #include "polarssl/certs.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 49 | #include <stdio.h> |
| 50 | #include <string.h> |
| 51 | #endif |
| 52 | |
| 53 | #define SERVER_PORT 4433 |
| 54 | #define SERVER_NAME "localhost" |
| 55 | #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n" |
| 56 | |
| 57 | #define DEBUG_LEVEL 1 |
| 58 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 59 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 60 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 61 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 62 | !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 63 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 64 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 65 | ((void) argc); |
| 66 | ((void) argv); |
| 67 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 68 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 69 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 70 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 71 | "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C " |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 72 | "not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 73 | return( 0 ); |
| 74 | } |
| 75 | #else |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 76 | static void my_debug( void *ctx, int level, const char *str ) |
| 77 | { |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 78 | ((void) level); |
| 79 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 80 | polarssl_fprintf( (FILE *) ctx, "%s", str ); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 81 | fflush( (FILE *) ctx ); |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 84 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | { |
Manuel Pégourié-Gonnard | 68821da | 2013-09-16 12:34:33 +0200 | [diff] [blame] | 86 | int ret, len, server_fd = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | unsigned char buf[1024]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 88 | const char *pers = "ssl_client1"; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 89 | |
| 90 | entropy_context entropy; |
| 91 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | ssl_context ssl; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 93 | x509_crt cacert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 95 | ((void) argc); |
| 96 | ((void) argv); |
| 97 | |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 98 | #if defined(POLARSSL_DEBUG_C) |
| 99 | debug_set_threshold( DEBUG_LEVEL ); |
| 100 | #endif |
| 101 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | /* |
| 103 | * 0. Initialize the RNG and the session data |
| 104 | */ |
Paul Bakker | 1a207ec | 2011-02-06 13:22:40 +0000 | [diff] [blame] | 105 | memset( &ssl, 0, sizeof( ssl_context ) ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 106 | x509_crt_init( &cacert ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 108 | polarssl_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 109 | fflush( stdout ); |
| 110 | |
| 111 | entropy_init( &entropy ); |
| 112 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 113 | (const unsigned char *) pers, |
| 114 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 115 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 116 | polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 117 | goto exit; |
| 118 | } |
| 119 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 120 | polarssl_printf( " ok\n" ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 121 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | /* |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 123 | * 0. Initialize certificates |
| 124 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 125 | polarssl_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 126 | fflush( stdout ); |
| 127 | |
| 128 | #if defined(POLARSSL_CERTS_C) |
Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 129 | ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list, |
| 130 | strlen( test_ca_list ) ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 131 | #else |
| 132 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 133 | polarssl_printf("POLARSSL_CERTS_C not defined."); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 134 | #endif |
| 135 | |
| 136 | if( ret < 0 ) |
| 137 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 138 | polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 139 | goto exit; |
| 140 | } |
| 141 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 142 | polarssl_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 143 | |
| 144 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | * 1. Start the connection |
| 146 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 147 | polarssl_printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME, |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 148 | SERVER_PORT ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | fflush( stdout ); |
| 150 | |
| 151 | if( ( ret = net_connect( &server_fd, SERVER_NAME, |
| 152 | SERVER_PORT ) ) != 0 ) |
| 153 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 154 | polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | goto exit; |
| 156 | } |
| 157 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 158 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * 2. Setup stuff |
| 162 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 163 | polarssl_printf( " . Setting up the SSL/TLS structure..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | fflush( stdout ); |
| 165 | |
| 166 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 167 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 168 | polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | goto exit; |
| 170 | } |
| 171 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 172 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | |
| 174 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 175 | /* OPTIONAL is not optimal for security, |
| 176 | * but makes interop easier in this simplified example */ |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 177 | ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL ); |
| 178 | ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | |
Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 180 | /* SSLv3 is deprecated, set minimum to TLS 1.0 */ |
| 181 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 ); |
Manuel Pégourié-Gonnard | fa06581 | 2015-01-12 14:05:33 +0100 | [diff] [blame] | 182 | /* RC4 is deprecated, disable it */ |
| 183 | ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED ); |
Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 184 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 185 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 187 | ssl_set_bio( &ssl, net_recv, &server_fd, |
| 188 | net_send, &server_fd ); |
| 189 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | /* |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 191 | * 4. Handshake |
| 192 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 193 | polarssl_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 194 | fflush( stdout ); |
| 195 | |
| 196 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 197 | { |
| 198 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 199 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 200 | polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 201 | goto exit; |
| 202 | } |
| 203 | } |
| 204 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 205 | polarssl_printf( " ok\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * 5. Verify the server certificate |
| 209 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 210 | polarssl_printf( " . Verifying peer X.509 certificate..." ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 212 | /* In real life, we may want to bail out when ret != 0 */ |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 213 | if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 ) |
| 214 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 215 | polarssl_printf( " failed\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 216 | |
| 217 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 218 | polarssl_printf( " ! server certificate has expired\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 219 | |
| 220 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 221 | polarssl_printf( " ! server certificate has been revoked\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 222 | |
| 223 | if( ( ret & BADCERT_CN_MISMATCH ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 224 | polarssl_printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 225 | |
| 226 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 227 | polarssl_printf( " ! self-signed or not signed by a trusted CA\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 228 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 229 | polarssl_printf( "\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 230 | } |
| 231 | else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 232 | polarssl_printf( " ok\n" ); |
Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 233 | |
| 234 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | * 3. Write the GET request |
| 236 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 237 | polarssl_printf( " > Write to server:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | fflush( stdout ); |
| 239 | |
| 240 | len = sprintf( (char *) buf, GET_REQUEST ); |
| 241 | |
| 242 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 243 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 244 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 246 | polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 | goto exit; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | len = ret; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 252 | polarssl_printf( " %d bytes written\n\n%s", len, (char *) buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 253 | |
| 254 | /* |
| 255 | * 7. Read the HTTP response |
| 256 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 257 | polarssl_printf( " < Read from server:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 258 | fflush( stdout ); |
| 259 | |
| 260 | do |
| 261 | { |
| 262 | len = sizeof( buf ) - 1; |
| 263 | memset( buf, 0, sizeof( buf ) ); |
| 264 | ret = ssl_read( &ssl, buf, len ); |
| 265 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 266 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | continue; |
| 268 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 269 | if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | break; |
| 271 | |
Paul Bakker | 61da752 | 2011-11-11 10:28:58 +0000 | [diff] [blame] | 272 | if( ret < 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 274 | polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | |
Paul Bakker | 61da752 | 2011-11-11 10:28:58 +0000 | [diff] [blame] | 278 | if( ret == 0 ) |
| 279 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 280 | polarssl_printf( "\n\nEOF\n\n" ); |
Paul Bakker | 61da752 | 2011-11-11 10:28:58 +0000 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | len = ret; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 285 | polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 286 | } |
Paul Bakker | 61da752 | 2011-11-11 10:28:58 +0000 | [diff] [blame] | 287 | while( 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 288 | |
| 289 | ssl_close_notify( &ssl ); |
| 290 | |
| 291 | exit: |
| 292 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 293 | #ifdef POLARSSL_ERROR_C |
| 294 | if( ret != 0 ) |
| 295 | { |
| 296 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 297 | polarssl_strerror( ret, error_buf, 100 ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 298 | polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 299 | } |
| 300 | #endif |
| 301 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 302 | if( server_fd != -1 ) |
| 303 | net_close( server_fd ); |
| 304 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 305 | x509_crt_free( &cacert ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 306 | ssl_free( &ssl ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 307 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 308 | entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | |
| 310 | memset( &ssl, 0, sizeof( ssl ) ); |
| 311 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 312 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 313 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 | fflush( stdout ); getchar(); |
| 315 | #endif |
| 316 | |
| 317 | return( ret ); |
| 318 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 319 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
| 320 | POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
| 321 | POLARSSL_CTR_DRBG_C */ |