Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Hello world example of a TLS client: fetch an HTTPS page |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
| 5 | * |
| 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | cdee2d9 | 2015-08-07 09:40:51 +0200 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #if !defined(TARGET_LIKE_MBED) |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | int main() { |
| 28 | printf("this program only works on mbed OS\n"); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | #else |
| 33 | |
| 34 | /** \file main.cpp |
| 35 | * \brief An example TLS Client application |
| 36 | * This application sends an HTTPS request to developer.mbed.org and searches for a string in |
| 37 | * the result. |
| 38 | * |
| 39 | * This example is implemented as a logic class (HelloHTTPS) wrapping a TCP socket. |
| 40 | * The logic class handles all events, leaving the main loop to just check if the process |
| 41 | * has finished. |
| 42 | */ |
| 43 | |
| 44 | /* Change to a number between 1 and 4 to debug the TLS connection */ |
| 45 | #define DEBUG_LEVEL 0 |
| 46 | |
| 47 | /* Change to 1 to skip certificate verification (UNSAFE, for debug only!) */ |
| 48 | #define UNSAFE 0 |
| 49 | |
| 50 | #include "mbed.h" |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 51 | #include "sal-iface-eth/EthernetInterface.h" |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 52 | #include "mbed-net-sockets/TCPStream.h" |
| 53 | #include "minar/minar.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 54 | |
| 55 | #include "mbedtls/ssl.h" |
| 56 | #include "mbedtls/entropy.h" |
| 57 | #include "mbedtls/ctr_drbg.h" |
| 58 | #include "mbedtls/error.h" |
| 59 | #if DEBUG_LEVEL > 0 |
| 60 | #include "mbedtls/debug.h" |
| 61 | #endif |
| 62 | |
| 63 | #include "lwipv4_init.h" |
| 64 | |
| 65 | namespace { |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 66 | const char *HTTPS_SERVER_NAME = "developer.mbed.org"; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 67 | const int HTTPS_SERVER_PORT = 443; |
| 68 | const int RECV_BUFFER_SIZE = 600; |
| 69 | |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 70 | const char HTTPS_PATH[] = "/media/uploads/mbed_official/hello.txt"; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 71 | const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1; |
| 72 | |
| 73 | /* Test related data */ |
| 74 | const char *HTTPS_OK_STR = "200 OK"; |
| 75 | const char *HTTPS_HELLO_STR = "Hello world!"; |
| 76 | |
| 77 | /* personalization string for the drbg */ |
| 78 | const char *DRBG_PERS = "mbed TLS helloword client"; |
| 79 | |
| 80 | /* List of trusted root CA certificates |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 81 | * currently only GlobalSign, the CA for developer.mbed.org |
| 82 | * |
| 83 | * To add more than one root, just concatenate them. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 84 | */ |
| 85 | const char SSL_CA_PEM[] = |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 86 | /* GlobalSign Root R1 SHA1/RSA/2048 |
| 87 | * Serial no. 04 00 00 00 00 01 15 4b 5a c3 94 */ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 88 | "-----BEGIN CERTIFICATE-----\n" |
| 89 | "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n" |
| 90 | "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n" |
| 91 | "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n" |
| 92 | "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n" |
| 93 | "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n" |
| 94 | "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n" |
| 95 | "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n" |
| 96 | "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n" |
| 97 | "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n" |
| 98 | "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n" |
| 99 | "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n" |
| 100 | "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n" |
| 101 | "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n" |
| 102 | "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n" |
| 103 | "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n" |
| 104 | "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n" |
| 105 | "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n" |
| 106 | "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" |
| 107 | "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" |
| 108 | "-----END CERTIFICATE-----\n"; |
| 109 | } |
| 110 | |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 111 | using namespace mbed::Sockets::v0; |
| 112 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 113 | /** |
| 114 | * \brief HelloHTTPS implements the logic for fetching a file from a webserver |
| 115 | * using a TCP socket and parsing the result. |
| 116 | */ |
| 117 | class HelloHTTPS { |
| 118 | public: |
| 119 | /** |
| 120 | * HelloHTTPS Constructor |
| 121 | * Initializes the TCP socket, sets up event handlers and flags. |
| 122 | * |
| 123 | * Note that CThunk is used for event handlers. This will be changed to a C++ |
| 124 | * function pointer in an upcoming release. |
| 125 | * |
| 126 | * |
| 127 | * @param[in] domain The domain name to fetch from |
| 128 | * @param[in] port The port of the HTTPS server |
| 129 | */ |
| 130 | HelloHTTPS(const char * domain, const uint16_t port) : |
| 131 | _stream(SOCKET_STACK_LWIP_IPV4), _domain(domain), _port(port) |
| 132 | { |
| 133 | |
| 134 | _error = false; |
| 135 | _gothello = false; |
| 136 | _got200 = false; |
| 137 | _bpos = 0; |
| 138 | _request_sent = 0; |
| 139 | _stream.open(SOCKET_AF_INET4); |
| 140 | |
| 141 | mbedtls_entropy_init(&_entropy); |
| 142 | mbedtls_ctr_drbg_init(&_ctr_drbg); |
| 143 | mbedtls_x509_crt_init(&_cacert); |
| 144 | mbedtls_ssl_init(&_ssl); |
| 145 | mbedtls_ssl_config_init(&_ssl_conf); |
| 146 | } |
| 147 | /** |
| 148 | * Initiate the test. |
| 149 | * |
| 150 | * Starts by clearing test flags, then resolves the address with DNS. |
| 151 | * |
| 152 | * @param[in] path The path of the file to fetch from the HTTPS server |
| 153 | * @return SOCKET_ERROR_NONE on success, or an error code on failure |
| 154 | */ |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 155 | void startTest(const char *path) { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 156 | /* Initialize the flags */ |
| 157 | _got200 = false; |
| 158 | _gothello = false; |
| 159 | _error = false; |
| 160 | _disconnected = false; |
| 161 | _request_sent = false; |
| 162 | /* Fill the request buffer */ |
| 163 | _bpos = snprintf(_buffer, sizeof(_buffer) - 1, "GET %s HTTP/1.1\nHost: %s\n\n", path, HTTPS_SERVER_NAME); |
| 164 | |
| 165 | /* |
| 166 | * Initialize TLS-related stuf. |
| 167 | */ |
| 168 | int ret; |
| 169 | if ((ret = mbedtls_ctr_drbg_seed(&_ctr_drbg, mbedtls_entropy_func, &_entropy, |
| 170 | (const unsigned char *) DRBG_PERS, |
| 171 | sizeof (DRBG_PERS))) != 0) { |
| 172 | print_mbedtls_error("mbedtls_crt_drbg_init", ret); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 173 | _error = true; |
| 174 | return; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if ((ret = mbedtls_x509_crt_parse(&_cacert, (const unsigned char *) SSL_CA_PEM, |
| 178 | sizeof (SSL_CA_PEM))) != 0) { |
| 179 | print_mbedtls_error("mbedtls_x509_crt_parse", ret); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 180 | _error = true; |
| 181 | return; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if ((ret = mbedtls_ssl_config_defaults(&_ssl_conf, |
| 185 | MBEDTLS_SSL_IS_CLIENT, |
| 186 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 187 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { |
| 188 | print_mbedtls_error("mbedtls_ssl_config_defaults", ret); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 189 | _error = true; |
| 190 | return; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | mbedtls_ssl_conf_ca_chain(&_ssl_conf, &_cacert, NULL); |
| 194 | mbedtls_ssl_conf_rng(&_ssl_conf, mbedtls_ctr_drbg_random, &_ctr_drbg); |
| 195 | |
| 196 | #if UNSAFE |
| 197 | mbedtls_ssl_conf_authmode(&_ssl_conf, MBEDTLS_SSL_VERIFY_OPTIONAL); |
| 198 | #endif |
| 199 | |
| 200 | #if DEBUG_LEVEL > 0 |
| 201 | mbedtls_ssl_conf_verify(&_ssl_conf, my_verify, NULL); |
| 202 | mbedtls_ssl_conf_dbg(&_ssl_conf, my_debug, NULL); |
| 203 | mbedtls_debug_set_threshold(DEBUG_LEVEL); |
| 204 | #endif |
| 205 | |
| 206 | if ((ret = mbedtls_ssl_setup(&_ssl, &_ssl_conf)) != 0) { |
| 207 | print_mbedtls_error("mbedtls_ssl_setup", ret); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 208 | _error = true; |
| 209 | return; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | mbedtls_ssl_set_hostname(&_ssl, HTTPS_SERVER_NAME); |
| 213 | |
| 214 | mbedtls_ssl_set_bio(&_ssl, static_cast<void *>(&_stream), |
| 215 | ssl_send, ssl_recv, NULL ); |
| 216 | |
| 217 | |
| 218 | /* Connect to the server */ |
| 219 | printf("Connecting to %s:%d\r\n", _domain, _port); |
| 220 | /* Resolve the domain name: */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 221 | socket_error_t err = _stream.resolve(_domain, TCPStream::DNSHandler_t(this, &HelloHTTPS::onDNS)); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 222 | if(err != SOCKET_ERROR_NONE) |
| 223 | _error = true; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 224 | } |
| 225 | /** |
| 226 | * Check if the test has completed. |
| 227 | * @return Returns true if done, false otherwise. |
| 228 | */ |
| 229 | bool done() { |
| 230 | return _error || (_got200 && _gothello); |
| 231 | } |
| 232 | /** |
| 233 | * Check if there was an error |
| 234 | * @return Returns true if there was an error, false otherwise. |
| 235 | */ |
| 236 | bool error() { |
| 237 | return _error; |
| 238 | } |
| 239 | /** |
| 240 | * Closes the TCP socket |
| 241 | */ |
| 242 | void close() { |
| 243 | _stream.close(); |
| 244 | while (!_disconnected) |
| 245 | __WFI(); |
| 246 | } |
| 247 | protected: |
| 248 | /** |
| 249 | * Helper for pretty-printing mbed TLS error codes |
| 250 | */ |
| 251 | static void print_mbedtls_error(const char *name, int err) { |
| 252 | char buf[128]; |
| 253 | mbedtls_strerror(err, buf, sizeof (buf)); |
| 254 | printf("%s() failed: -0x%04x (%d): %s\r\n", name, -err, err, buf); |
| 255 | } |
| 256 | |
| 257 | #if DEBUG_LEVEL > 0 |
| 258 | /** |
| 259 | * Debug callback for mbed TLS |
| 260 | * Just prints on the USB serial port |
| 261 | */ |
| 262 | static void my_debug(void *ctx, int level, const char *str) |
| 263 | { |
| 264 | (void) ctx; |
| 265 | (void) level; |
| 266 | |
| 267 | printf("%s", str); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Certificate verification callback for mbed TLS |
| 272 | * Here we only use it to display information on each cert in the chain |
| 273 | */ |
| 274 | static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, int *flags) |
| 275 | { |
| 276 | char buf[1024]; |
| 277 | (void) data; |
| 278 | |
| 279 | printf("\nVerifying certificate at depth %d:\n", depth); |
| 280 | mbedtls_x509_crt_info(buf, sizeof (buf) - 1, " ", crt); |
| 281 | printf("%s", buf); |
| 282 | |
| 283 | if (*flags == 0) |
| 284 | printf("No verification issue for this certificate\n"); |
| 285 | else |
| 286 | { |
| 287 | mbedtls_x509_crt_verify_info(buf, sizeof (buf), " ! ", *flags); |
| 288 | printf("%s\n", buf); |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | /** |
| 296 | * Receive callback for mbed TLS |
| 297 | */ |
| 298 | static int ssl_recv(void *ctx, unsigned char *buf, size_t len) { |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 299 | TCPStream *stream = static_cast<TCPStream *>(ctx); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 300 | socket_error_t err = stream->recv(buf, &len); |
| 301 | |
| 302 | if (err == SOCKET_ERROR_NONE) { |
| 303 | return static_cast<int>(len); |
| 304 | } else if (err == SOCKET_ERROR_WOULD_BLOCK) { |
| 305 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 306 | } else { |
| 307 | return -1; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Send callback for mbed TLS |
| 313 | */ |
| 314 | static int ssl_send(void *ctx, const unsigned char *buf, size_t len) { |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 315 | TCPStream *stream = static_cast<TCPStream *>(ctx); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 316 | |
| 317 | socket_error_t err = stream->send(buf, len); |
| 318 | |
| 319 | if (err == SOCKET_ERROR_NONE) { |
| 320 | return static_cast<int>(len); |
| 321 | } else if (err == SOCKET_ERROR_WOULD_BLOCK) { |
| 322 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 323 | } else { |
| 324 | return -1; |
| 325 | } |
| 326 | } |
| 327 | |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 328 | void onError(Socket *s, socket_error_t err) { |
| 329 | (void) s; |
| 330 | printf("MBED: Socket Error: %s (%d)\r\n", socket_strerror(err), err); |
| 331 | _stream.close(); |
| 332 | _error = true; |
| 333 | minar::Scheduler::stop(); |
| 334 | } |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 335 | /** |
| 336 | * On Connect handler |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 337 | * Starts the TLS handshake |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 338 | */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 339 | void onConnect(TCPStream *s) { |
| 340 | s->setOnReadable(TCPStream::ReadableHandler_t(this, &HelloHTTPS::onReceive)); |
| 341 | s->setOnDisconnect(TCPStream::DisconnectHandler_t(this, &HelloHTTPS::onDisconnect)); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 342 | |
| 343 | /* Start the handshake, the rest will be done in onReceive() */ |
| 344 | int ret = mbedtls_ssl_handshake(&_ssl); |
| 345 | if (ret < 0) { |
| 346 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 347 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 348 | print_mbedtls_error("mbedtls_ssl_handshake", ret); |
| 349 | _error = true; |
| 350 | } |
| 351 | return; |
| 352 | } |
| 353 | } |
| 354 | /** |
| 355 | * On Receive handler |
| 356 | * Parses the response from the server, to check for the HTTPS 200 status code and the expected response ("Hello World!") |
| 357 | */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 358 | void onReceive(Socket *s) { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 359 | if (_error) |
| 360 | return; |
| 361 | |
| 362 | /* Send request if not done yet */ |
| 363 | if (!_request_sent) { |
| 364 | int ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos); |
| 365 | if (ret < 0) { |
| 366 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 367 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 368 | print_mbedtls_error("mbedtls_ssl_write", ret); |
| 369 | _error = true; |
| 370 | } |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | /* If we get here, the request was sent */ |
| 375 | _request_sent = 1; |
| 376 | |
| 377 | /* It also means the handshake is done, time to print info */ |
| 378 | printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME); |
| 379 | { |
| 380 | char buf[1024]; |
| 381 | mbedtls_x509_crt_info(buf, sizeof(buf), "\r ", |
| 382 | mbedtls_ssl_get_peer_cert(&_ssl)); |
| 383 | printf("Server certificate:\r\n%s\r", buf); |
| 384 | |
| 385 | #if defined(UNSAFE) |
| 386 | uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl); |
| 387 | if( flags != 0 ) |
| 388 | { |
| 389 | mbedtls_x509_crt_verify_info(buf, sizeof (buf), "\r ! ", flags); |
| 390 | printf("Certificate verification failed:\r\n%s\r\r\n", buf); |
| 391 | } |
| 392 | else |
| 393 | #endif |
| 394 | printf("Certificate verification passed\r\n\r\n"); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* Read data out of the socket */ |
| 399 | int ret = mbedtls_ssl_read(&_ssl, (unsigned char *) _buffer, sizeof(_buffer)); |
| 400 | if (ret < 0) { |
| 401 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 402 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 403 | print_mbedtls_error("mbedtls_ssl_read", ret); |
| 404 | _error = true; |
| 405 | } |
| 406 | return; |
| 407 | } |
| 408 | _bpos = static_cast<size_t>(ret); |
| 409 | |
| 410 | _buffer[_bpos] = 0; |
| 411 | |
| 412 | /* Check each of the flags */ |
| 413 | _got200 = _got200 || strstr(_buffer, HTTPS_OK_STR) != NULL; |
| 414 | _gothello = _gothello || strstr(_buffer, HTTPS_HELLO_STR) != NULL; |
| 415 | |
| 416 | /* Print status messages */ |
| 417 | printf("HTTPS: Received %d chars from server\r\n", _bpos); |
| 418 | printf("HTTPS: Received 200 OK status ... %s\r\n", _got200 ? "[OK]" : "[FAIL]"); |
| 419 | printf("HTTPS: Received '%s' status ... %s\r\n", HTTPS_HELLO_STR, _gothello ? "[OK]" : "[FAIL]"); |
| 420 | printf("HTTPS: Received message:\r\n\r\n"); |
| 421 | printf("%s", _buffer); |
| 422 | _error = !(_got200 && _gothello); |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 423 | |
| 424 | s->close(); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 425 | } |
| 426 | /** |
| 427 | * On DNS Handler |
| 428 | * Reads the address returned by DNS, then starts the connect process. |
| 429 | */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 430 | void onDNS(Socket *s, struct socket_addr addr, const char *domain) { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 431 | /* Check that the result is a valid DNS response */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 432 | if (socket_addr_is_any(&addr)) { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 433 | /* Could not find DNS entry */ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 434 | printf("Could not find DNS entry for %s", HTTPS_SERVER_NAME); |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 435 | onError(s, SOCKET_ERROR_DNS_FAILED); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 436 | } else { |
| 437 | /* Start connecting to the remote host */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 438 | char buf[16]; |
| 439 | _remoteAddr.setAddr(&addr); |
| 440 | _remoteAddr.fmtIPv4(buf,sizeof(buf)); |
| 441 | printf("%s address: %s\r\n",domain, buf); |
| 442 | socket_error_t err = _stream.connect(_remoteAddr, _port, TCPStream::ConnectHandler_t(this, &HelloHTTPS::onConnect)); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 443 | |
| 444 | if (err != SOCKET_ERROR_NONE) { |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 445 | onError(s, err); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 449 | void onDisconnect(TCPStream *s) { |
| 450 | s->close(); |
| 451 | minar::Scheduler::stop(); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | protected: |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 455 | TCPStream _stream; /**< The TCP Socket */ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 456 | const char *_domain; /**< The domain name of the HTTPS server */ |
| 457 | const uint16_t _port; /**< The HTTPS server port */ |
| 458 | char _buffer[RECV_BUFFER_SIZE]; /**< The response buffer */ |
| 459 | size_t _bpos; /**< The current offset in the response buffer */ |
Manuel Pégourié-Gonnard | ae5398a | 2015-08-04 14:10:28 +0200 | [diff] [blame] | 460 | SocketAddr _remoteAddr; /**< The remote address */ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 461 | volatile bool _got200; /**< Status flag for HTTPS 200 */ |
| 462 | volatile bool _gothello; /**< Status flag for finding the test string */ |
| 463 | volatile bool _error; /**< Status flag for an error */ |
| 464 | volatile bool _disconnected; |
| 465 | volatile bool _request_sent; |
| 466 | |
| 467 | mbedtls_entropy_context _entropy; |
| 468 | mbedtls_ctr_drbg_context _ctr_drbg; |
| 469 | mbedtls_x509_crt _cacert; |
| 470 | mbedtls_ssl_context _ssl; |
| 471 | mbedtls_ssl_config _ssl_conf; |
| 472 | }; |
| 473 | |
| 474 | /** |
| 475 | * The main loop of the HTTPS Hello World test |
| 476 | */ |
| 477 | int example_client() { |
| 478 | EthernetInterface eth; |
| 479 | /* Initialise with DHCP, connect, and start up the stack */ |
| 480 | eth.init(); |
| 481 | eth.connect(); |
| 482 | lwipv4_socket_init(); |
| 483 | |
| 484 | printf("\r\n\r\n"); |
| 485 | printf("Client IP Address is %s\r\n", eth.getIPAddress()); |
| 486 | |
| 487 | HelloHTTPS hello(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 488 | { |
| 489 | mbed::FunctionPointer1<void, const char*> fp(&hello, &HelloHTTPS::startTest); |
| 490 | minar::Scheduler::postCallback(fp.bind(HTTPS_PATH)); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 491 | } |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 492 | |
| 493 | minar::Scheduler::start(); |
| 494 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 495 | eth.disconnect(); |
Manuel Pégourié-Gonnard | 8356fc2 | 2015-08-07 14:35:12 +0200 | [diff] [blame] | 496 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 497 | return static_cast<int>(hello.error()); |
| 498 | } |
| 499 | |
| 500 | #include "mbed/test_env.h" |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 501 | #include "minar/minar.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 502 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 503 | static void run() { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 504 | /* The default 9600 bps is too slow to print full TLS debug info and could |
| 505 | * cause the other party to time out. Select a higher baud rate for |
| 506 | * printf(), regardless of debug level for the sake of uniformity. */ |
| 507 | Serial pc(USBTX, USBRX); |
| 508 | pc.baud(115200); |
| 509 | |
| 510 | MBED_HOSTTEST_TIMEOUT(120); |
| 511 | MBED_HOSTTEST_SELECT(default); |
| 512 | MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client); |
| 513 | MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT"); |
| 514 | MBED_HOSTTEST_RESULT(example_client() == 0); |
| 515 | } |
| 516 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 517 | void app_start(int, char*[]) { |
| 518 | minar::Scheduler::postCallback(FunctionPointer0<void>(run).bind()); |
| 519 | } |
| 520 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 521 | #endif /* TARGET_LIKE_MBED */ |