Manuel Pégourié-Gonnard | a6fc5b2 | 2014-11-24 14:05:25 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Minimal SSL client, used for memory measurements. |
| 3 | * |
| 4 | * Copyright (C) 2014, ARM Limited, All Rights Reserved |
| 5 | * |
| 6 | * This file is part of mbed TLS (https://polarssl.org) |
| 7 | * |
| 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 | |
| 23 | #if !defined(POLARSSL_CONFIG_FILE) |
| 24 | #include "polarssl/config.h" |
| 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | /* |
| 30 | * We're creating and connecting the socket "manually" rather than using the |
| 31 | * NET module, in order to avoid the overhead of getaddrinfo() which tends to |
| 32 | * dominate memory usage in small configurations. For the sake of simplicity, |
| 33 | * only a Unix version is implemented. |
| 34 | */ |
| 35 | #if defined(unix) || defined(__unix__) || defined(__unix) |
| 36 | #define UNIX |
| 37 | #endif |
| 38 | |
| 39 | #if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 40 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
| 41 | !defined(UNIX) |
| 42 | #if defined(POLARSSL_PLATFORM_C) |
| 43 | #include "polarssl/platform.h" |
| 44 | #else |
| 45 | #include <stdio.h> |
| 46 | #define polarssl_printf printf |
| 47 | #endif |
| 48 | int main( void ) |
| 49 | { |
| 50 | polarssl_printf( "POLARSSL_CTR_DRBG_C and/or POLARSSL_ENTROPY_C and/or " |
| 51 | "POLARSSL_NET_C and/or POLARSSL_SSL_CLI_C and/or UNIX " |
| 52 | "not defined.\n"); |
| 53 | return( 0 ); |
| 54 | } |
| 55 | #else |
| 56 | |
| 57 | #include <string.h> |
| 58 | |
| 59 | #include "polarssl/net.h" |
| 60 | #include "polarssl/ssl.h" |
| 61 | #include "polarssl/entropy.h" |
| 62 | #include "polarssl/ctr_drbg.h" |
| 63 | |
| 64 | #include <sys/socket.h> |
| 65 | #include <netinet/in.h> |
| 66 | #include <arpa/inet.h> |
| 67 | |
| 68 | /* |
| 69 | * Hardcoded values for server host and port |
| 70 | */ |
| 71 | #define PORT_BE 0x1151 /* 4433 */ |
| 72 | #define PORT_LE 0x5111 |
| 73 | #define ADDR_BE 0x7f000001 /* 127.0.0.1 */ |
| 74 | #define ADDR_LE 0x0100007f |
| 75 | |
| 76 | #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n" |
| 77 | |
| 78 | const unsigned char psk[] = { |
| 79 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 80 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 81 | }; |
| 82 | const char psk_id[] = "Client_identity"; |
| 83 | |
| 84 | const char *pers = "mini_client"; |
| 85 | |
| 86 | int main( void ) |
| 87 | { |
| 88 | int ret = 0; |
| 89 | int server_fd = -1; |
| 90 | struct sockaddr_in addr; |
| 91 | |
| 92 | entropy_context entropy; |
| 93 | ctr_drbg_context ctr_drbg; |
| 94 | ssl_context ssl; |
| 95 | |
| 96 | /* |
| 97 | * 1. Initialize and setup stuff |
| 98 | */ |
| 99 | memset( &ssl, 0, sizeof( ssl_context ) ); |
| 100 | |
| 101 | entropy_init( &entropy ); |
| 102 | if( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 103 | (const unsigned char *) pers, strlen( pers ) ) != 0 ) |
| 104 | { |
| 105 | ret = 1; |
| 106 | goto exit; |
| 107 | } |
| 108 | |
| 109 | if( ssl_init( &ssl ) != 0 ) |
| 110 | { |
| 111 | ret = 2; |
| 112 | goto exit; |
| 113 | } |
| 114 | |
| 115 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
| 116 | |
| 117 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
| 118 | |
| 119 | ssl_set_psk( &ssl, psk, sizeof( psk ), |
| 120 | (const unsigned char *) psk_id, sizeof( psk_id ) - 1 ); |
| 121 | |
| 122 | /* |
| 123 | * 1. Start the connection |
| 124 | */ |
| 125 | memset( &addr, 0, sizeof( addr ) ); |
| 126 | addr.sin_family = AF_INET; |
| 127 | |
| 128 | ret = 1; /* for endianness detection */ |
| 129 | addr.sin_port = *((char *) &ret) == ret ? PORT_LE : PORT_BE; |
| 130 | addr.sin_addr.s_addr = *((char *) &ret) == ret ? ADDR_LE : ADDR_BE; |
| 131 | ret = 0; |
| 132 | |
| 133 | if( ( server_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) |
| 134 | { |
| 135 | ret = 3; |
| 136 | goto exit; |
| 137 | } |
| 138 | |
| 139 | if( connect( server_fd, |
| 140 | (const struct sockaddr *) &addr, sizeof( addr ) ) < 0 ) |
| 141 | { |
| 142 | ret = 4; |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | ssl_set_bio( &ssl, net_recv, &server_fd, net_send, &server_fd ); |
| 147 | |
| 148 | if( ssl_handshake( &ssl ) != 0 ) |
| 149 | { |
| 150 | ret = 5; |
| 151 | goto exit; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * 2. Write the GET request and close the connection |
| 156 | */ |
| 157 | if( ssl_write( &ssl, (const unsigned char *) GET_REQUEST, |
| 158 | sizeof( GET_REQUEST ) - 1 ) <= 0 ) |
| 159 | { |
| 160 | ret = 6; |
| 161 | goto exit; |
| 162 | } |
| 163 | |
| 164 | ssl_close_notify( &ssl ); |
| 165 | |
| 166 | exit: |
| 167 | if( server_fd != -1 ) |
| 168 | net_close( server_fd ); |
| 169 | |
| 170 | ssl_free( &ssl ); |
| 171 | ctr_drbg_free( &ctr_drbg ); |
| 172 | entropy_free( &entropy ); |
| 173 | |
| 174 | return( ret ); |
| 175 | } |
| 176 | #endif |