blob: c90707b5c0bdacaa1bf17de1f9119d3f387e8808 [file] [log] [blame]
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +02001/*
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é-Gonnardcdee2d92015-08-07 09:40:51 +02007 *
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é-Gonnard63e7eba2015-07-28 14:17:48 +020021 */
22
23#if !defined(TARGET_LIKE_MBED)
24
25#include <stdio.h>
26
27int 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é-Gonnardbd6d0ab2015-08-18 18:18:16 +020051#include "EthernetInterface.h"
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +020052#include "mbed-net-sockets/TCPStream.h"
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +020053#include "test_env.h"
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +020054#include "minar/minar.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020055
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +020056#include "lwipv4_init.h"
57
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020058#include "mbedtls/ssl.h"
59#include "mbedtls/entropy.h"
60#include "mbedtls/ctr_drbg.h"
61#include "mbedtls/error.h"
62#if DEBUG_LEVEL > 0
63#include "mbedtls/debug.h"
64#endif
65
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020066namespace {
Manuel Pégourié-Gonnard42618b82015-08-17 14:19:38 +020067const char *HTTPS_SERVER_NAME = "developer.mbed.org";
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020068const int HTTPS_SERVER_PORT = 443;
69const int RECV_BUFFER_SIZE = 600;
70
Manuel Pégourié-Gonnard42618b82015-08-17 14:19:38 +020071const char HTTPS_PATH[] = "/media/uploads/mbed_official/hello.txt";
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020072const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1;
73
74/* Test related data */
75const char *HTTPS_OK_STR = "200 OK";
76const char *HTTPS_HELLO_STR = "Hello world!";
77
78/* personalization string for the drbg */
79const char *DRBG_PERS = "mbed TLS helloword client";
80
81/* List of trusted root CA certificates
Simon Butcherdf819242015-08-19 19:33:27 +010082 * currently only GlobalSign, the CA for developer.mbed.org
83 *
84 * To add more than one root, just concatenate them.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020085 */
86const char SSL_CA_PEM[] =
Simon Butcherdf819242015-08-19 19:33:27 +010087/* GlobalSign Root R1 SHA1/RSA/2048
88 * Serial no. 04 00 00 00 00 01 15 4b 5a c3 94 */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020089"-----BEGIN CERTIFICATE-----\n"
90"MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n"
91"A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n"
92"b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n"
93"MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n"
94"YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n"
95"aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n"
96"jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n"
97"xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n"
98"1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n"
99"snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n"
100"U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n"
101"9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n"
102"BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n"
103"AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n"
104"yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n"
105"38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n"
106"AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n"
107"DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n"
108"HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n"
109"-----END CERTIFICATE-----\n";
110}
111
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200112using namespace mbed::Sockets::v0;
113
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200114/**
115 * \brief HelloHTTPS implements the logic for fetching a file from a webserver
116 * using a TCP socket and parsing the result.
117 */
118class HelloHTTPS {
119public:
120 /**
121 * HelloHTTPS Constructor
122 * Initializes the TCP socket, sets up event handlers and flags.
123 *
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200124 * @param[in] domain The domain name to fetch from
125 * @param[in] port The port of the HTTPS server
126 */
127 HelloHTTPS(const char * domain, const uint16_t port) :
128 _stream(SOCKET_STACK_LWIP_IPV4), _domain(domain), _port(port)
129 {
130
131 _error = false;
132 _gothello = false;
133 _got200 = false;
134 _bpos = 0;
135 _request_sent = 0;
136 _stream.open(SOCKET_AF_INET4);
137
138 mbedtls_entropy_init(&_entropy);
139 mbedtls_ctr_drbg_init(&_ctr_drbg);
140 mbedtls_x509_crt_init(&_cacert);
141 mbedtls_ssl_init(&_ssl);
142 mbedtls_ssl_config_init(&_ssl_conf);
143 }
144 /**
Manuel Pégourié-Gonnardfe626132015-08-20 12:00:58 +0200145 * HelloHTTPS Desctructor
146 */
147 ~HelloHTTPS() {
148 mbedtls_entropy_free(&_entropy);
149 mbedtls_ctr_drbg_free(&_ctr_drbg);
150 mbedtls_x509_crt_free(&_cacert);
151 mbedtls_ssl_free(&_ssl);
152 mbedtls_ssl_config_free(&_ssl_conf);
153 }
154 /**
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200155 * Initiate the test.
156 *
157 * Starts by clearing test flags, then resolves the address with DNS.
158 *
159 * @param[in] path The path of the file to fetch from the HTTPS server
160 * @return SOCKET_ERROR_NONE on success, or an error code on failure
161 */
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200162 void startTest(const char *path) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200163 /* Initialize the flags */
164 _got200 = false;
165 _gothello = false;
166 _error = false;
167 _disconnected = false;
168 _request_sent = false;
169 /* Fill the request buffer */
170 _bpos = snprintf(_buffer, sizeof(_buffer) - 1, "GET %s HTTP/1.1\nHost: %s\n\n", path, HTTPS_SERVER_NAME);
171
172 /*
173 * Initialize TLS-related stuf.
174 */
175 int ret;
176 if ((ret = mbedtls_ctr_drbg_seed(&_ctr_drbg, mbedtls_entropy_func, &_entropy,
177 (const unsigned char *) DRBG_PERS,
178 sizeof (DRBG_PERS))) != 0) {
179 print_mbedtls_error("mbedtls_crt_drbg_init", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200180 _error = true;
181 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200182 }
183
184 if ((ret = mbedtls_x509_crt_parse(&_cacert, (const unsigned char *) SSL_CA_PEM,
185 sizeof (SSL_CA_PEM))) != 0) {
186 print_mbedtls_error("mbedtls_x509_crt_parse", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200187 _error = true;
188 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200189 }
190
191 if ((ret = mbedtls_ssl_config_defaults(&_ssl_conf,
192 MBEDTLS_SSL_IS_CLIENT,
193 MBEDTLS_SSL_TRANSPORT_STREAM,
194 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
195 print_mbedtls_error("mbedtls_ssl_config_defaults", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200196 _error = true;
197 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200198 }
199
200 mbedtls_ssl_conf_ca_chain(&_ssl_conf, &_cacert, NULL);
201 mbedtls_ssl_conf_rng(&_ssl_conf, mbedtls_ctr_drbg_random, &_ctr_drbg);
202
203#if UNSAFE
204 mbedtls_ssl_conf_authmode(&_ssl_conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
205#endif
206
207#if DEBUG_LEVEL > 0
208 mbedtls_ssl_conf_verify(&_ssl_conf, my_verify, NULL);
209 mbedtls_ssl_conf_dbg(&_ssl_conf, my_debug, NULL);
210 mbedtls_debug_set_threshold(DEBUG_LEVEL);
211#endif
212
213 if ((ret = mbedtls_ssl_setup(&_ssl, &_ssl_conf)) != 0) {
214 print_mbedtls_error("mbedtls_ssl_setup", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200215 _error = true;
216 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200217 }
218
219 mbedtls_ssl_set_hostname(&_ssl, HTTPS_SERVER_NAME);
220
221 mbedtls_ssl_set_bio(&_ssl, static_cast<void *>(&_stream),
222 ssl_send, ssl_recv, NULL );
223
224
225 /* Connect to the server */
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200226 printf("Starting DNS lookup for %s\r\n", _domain);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200227 /* Resolve the domain name: */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200228 socket_error_t err = _stream.resolve(_domain, TCPStream::DNSHandler_t(this, &HelloHTTPS::onDNS));
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200229 _stream.error_check(err);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200230 }
231 /**
232 * Check if the test has completed.
233 * @return Returns true if done, false otherwise.
234 */
235 bool done() {
236 return _error || (_got200 && _gothello);
237 }
238 /**
239 * Check if there was an error
240 * @return Returns true if there was an error, false otherwise.
241 */
242 bool error() {
243 return _error;
244 }
245 /**
246 * Closes the TCP socket
247 */
248 void close() {
249 _stream.close();
250 while (!_disconnected)
251 __WFI();
252 }
253protected:
254 /**
255 * Helper for pretty-printing mbed TLS error codes
256 */
257 static void print_mbedtls_error(const char *name, int err) {
258 char buf[128];
259 mbedtls_strerror(err, buf, sizeof (buf));
260 printf("%s() failed: -0x%04x (%d): %s\r\n", name, -err, err, buf);
261 }
262
263#if DEBUG_LEVEL > 0
264 /**
265 * Debug callback for mbed TLS
266 * Just prints on the USB serial port
267 */
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200268 static void my_debug(void *ctx, int level, const char *file, int line,
269 const char *str)
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200270 {
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200271 const char *p, *basename;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200272 (void) ctx;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200273
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200274 /* Extract basename from file */
275 for(p = basename = file; *p != '\0'; p++) {
276 if(*p == '/' || *p == '\\') {
277 basename = p + 1;
278 }
279 }
280
281 printf("%s:%04d: |%d| %s", basename, line, level, str);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200282 }
283
284 /**
285 * Certificate verification callback for mbed TLS
286 * Here we only use it to display information on each cert in the chain
287 */
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200288 static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200289 {
290 char buf[1024];
291 (void) data;
292
293 printf("\nVerifying certificate at depth %d:\n", depth);
294 mbedtls_x509_crt_info(buf, sizeof (buf) - 1, " ", crt);
295 printf("%s", buf);
296
297 if (*flags == 0)
298 printf("No verification issue for this certificate\n");
299 else
300 {
301 mbedtls_x509_crt_verify_info(buf, sizeof (buf), " ! ", *flags);
302 printf("%s\n", buf);
303 }
304
305 return 0;
306 }
307#endif
308
309 /**
310 * Receive callback for mbed TLS
311 */
312 static int ssl_recv(void *ctx, unsigned char *buf, size_t len) {
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200313 TCPStream *stream = static_cast<TCPStream *>(ctx);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200314 socket_error_t err = stream->recv(buf, &len);
315
316 if (err == SOCKET_ERROR_NONE) {
317 return static_cast<int>(len);
318 } else if (err == SOCKET_ERROR_WOULD_BLOCK) {
319 return MBEDTLS_ERR_SSL_WANT_READ;
320 } else {
321 return -1;
322 }
323 }
324
325 /**
326 * Send callback for mbed TLS
327 */
328 static int ssl_send(void *ctx, const unsigned char *buf, size_t len) {
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200329 TCPStream *stream = static_cast<TCPStream *>(ctx);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200330
331 socket_error_t err = stream->send(buf, len);
332
333 if (err == SOCKET_ERROR_NONE) {
334 return static_cast<int>(len);
335 } else if (err == SOCKET_ERROR_WOULD_BLOCK) {
336 return MBEDTLS_ERR_SSL_WANT_WRITE;
337 } else {
338 return -1;
339 }
340 }
341
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200342 void onError(Socket *s, socket_error_t err) {
343 (void) s;
344 printf("MBED: Socket Error: %s (%d)\r\n", socket_strerror(err), err);
345 _stream.close();
346 _error = true;
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200347 MBED_HOSTTEST_RESULT(false);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200348 }
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200349 /**
350 * On Connect handler
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200351 * Starts the TLS handshake
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200352 */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200353 void onConnect(TCPStream *s) {
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200354 char buf[16];
355 _remoteAddr.fmtIPv4(buf,sizeof(buf));
356 printf("Connected to %s:%d\r\n", buf, _port);
357
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200358 s->setOnReadable(TCPStream::ReadableHandler_t(this, &HelloHTTPS::onReceive));
359 s->setOnDisconnect(TCPStream::DisconnectHandler_t(this, &HelloHTTPS::onDisconnect));
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200360
361 /* Start the handshake, the rest will be done in onReceive() */
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200362 printf("Starting the TLS handshake...\r\n");
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200363 int ret = mbedtls_ssl_handshake(&_ssl);
364 if (ret < 0) {
365 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
366 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
367 print_mbedtls_error("mbedtls_ssl_handshake", ret);
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200368 onError(s, SOCKET_ERROR_UNKNOWN);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200369 }
370 return;
371 }
372 }
373 /**
374 * On Receive handler
375 * Parses the response from the server, to check for the HTTPS 200 status code and the expected response ("Hello World!")
376 */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200377 void onReceive(Socket *s) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200378 /* Send request if not done yet */
379 if (!_request_sent) {
380 int ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos);
381 if (ret < 0) {
382 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
383 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
384 print_mbedtls_error("mbedtls_ssl_write", ret);
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200385 onError(s, SOCKET_ERROR_UNKNOWN);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200386 }
387 return;
388 }
389
390 /* If we get here, the request was sent */
391 _request_sent = 1;
392
393 /* It also means the handshake is done, time to print info */
394 printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME);
395 {
396 char buf[1024];
397 mbedtls_x509_crt_info(buf, sizeof(buf), "\r ",
398 mbedtls_ssl_get_peer_cert(&_ssl));
399 printf("Server certificate:\r\n%s\r", buf);
400
401#if defined(UNSAFE)
402 uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl);
403 if( flags != 0 )
404 {
405 mbedtls_x509_crt_verify_info(buf, sizeof (buf), "\r ! ", flags);
406 printf("Certificate verification failed:\r\n%s\r\r\n", buf);
407 }
408 else
409#endif
410 printf("Certificate verification passed\r\n\r\n");
411 }
412 }
413
414 /* Read data out of the socket */
415 int ret = mbedtls_ssl_read(&_ssl, (unsigned char *) _buffer, sizeof(_buffer));
416 if (ret < 0) {
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200417 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
418 print_mbedtls_error("mbedtls_ssl_read", ret);
419 onError(s, SOCKET_ERROR_UNKNOWN);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200420 }
421 return;
422 }
423 _bpos = static_cast<size_t>(ret);
424
425 _buffer[_bpos] = 0;
426
427 /* Check each of the flags */
428 _got200 = _got200 || strstr(_buffer, HTTPS_OK_STR) != NULL;
429 _gothello = _gothello || strstr(_buffer, HTTPS_HELLO_STR) != NULL;
430
431 /* Print status messages */
432 printf("HTTPS: Received %d chars from server\r\n", _bpos);
433 printf("HTTPS: Received 200 OK status ... %s\r\n", _got200 ? "[OK]" : "[FAIL]");
434 printf("HTTPS: Received '%s' status ... %s\r\n", HTTPS_HELLO_STR, _gothello ? "[OK]" : "[FAIL]");
435 printf("HTTPS: Received message:\r\n\r\n");
436 printf("%s", _buffer);
437 _error = !(_got200 && _gothello);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200438
439 s->close();
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200440 }
441 /**
442 * On DNS Handler
443 * Reads the address returned by DNS, then starts the connect process.
444 */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200445 void onDNS(Socket *s, struct socket_addr addr, const char *domain) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200446 /* Check that the result is a valid DNS response */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200447 if (socket_addr_is_any(&addr)) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200448 /* Could not find DNS entry */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200449 printf("Could not find DNS entry for %s", HTTPS_SERVER_NAME);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200450 onError(s, SOCKET_ERROR_DNS_FAILED);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200451 } else {
452 /* Start connecting to the remote host */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200453 char buf[16];
454 _remoteAddr.setAddr(&addr);
455 _remoteAddr.fmtIPv4(buf,sizeof(buf));
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200456 printf("DNS Response Received:\r\n%s: %s\r\n", domain, buf);
457 printf("Connecting to %s:%d\r\n", buf, _port);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200458 socket_error_t err = _stream.connect(_remoteAddr, _port, TCPStream::ConnectHandler_t(this, &HelloHTTPS::onConnect));
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200459
460 if (err != SOCKET_ERROR_NONE) {
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200461 onError(s, err);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200462 }
463 }
464 }
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200465 void onDisconnect(TCPStream *s) {
466 s->close();
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200467 MBED_HOSTTEST_RESULT(!error());
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200468 }
469
470protected:
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200471 TCPStream _stream; /**< The TCP Socket */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200472 const char *_domain; /**< The domain name of the HTTPS server */
473 const uint16_t _port; /**< The HTTPS server port */
474 char _buffer[RECV_BUFFER_SIZE]; /**< The response buffer */
475 size_t _bpos; /**< The current offset in the response buffer */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200476 SocketAddr _remoteAddr; /**< The remote address */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200477 volatile bool _got200; /**< Status flag for HTTPS 200 */
478 volatile bool _gothello; /**< Status flag for finding the test string */
479 volatile bool _error; /**< Status flag for an error */
480 volatile bool _disconnected;
481 volatile bool _request_sent;
482
483 mbedtls_entropy_context _entropy;
484 mbedtls_ctr_drbg_context _ctr_drbg;
485 mbedtls_x509_crt _cacert;
486 mbedtls_ssl_context _ssl;
487 mbedtls_ssl_config _ssl_conf;
488};
489
490/**
491 * The main loop of the HTTPS Hello World test
492 */
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200493EthernetInterface eth;
494HelloHTTPS *hello;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200495
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200496void app_start(int, char*[]) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200497 /* The default 9600 bps is too slow to print full TLS debug info and could
498 * cause the other party to time out. Select a higher baud rate for
499 * printf(), regardless of debug level for the sake of uniformity. */
500 Serial pc(USBTX, USBRX);
501 pc.baud(115200);
502
503 MBED_HOSTTEST_TIMEOUT(120);
504 MBED_HOSTTEST_SELECT(default);
505 MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client);
506 MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT");
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200507
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200508 /* Initialise with DHCP, connect, and start up the stack */
509 eth.init();
510 eth.connect();
511 lwipv4_socket_init();
512
513 hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
514
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200515 printf("Client IP Address is %s\r\n", eth.getIPAddress());
516
517 mbed::FunctionPointer1<void, const char*> fp(hello, &HelloHTTPS::startTest);
518 minar::Scheduler::postCallback(fp.bind(HTTPS_PATH));
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200519}
520
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200521#endif /* TARGET_LIKE_MBED */