blob: 907cfb6d4b2d0b96328a6dc8b5a0a48c35fdb10f [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020018 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(TARGET_LIKE_MBED)
23
24#include <stdio.h>
25
26int main() {
27 printf("this program only works on mbed OS\n");
28 return 0;
29}
30
31#else
32
33/** \file main.cpp
34 * \brief An example TLS Client application
35 * This application sends an HTTPS request to developer.mbed.org and searches for a string in
36 * the result.
37 *
38 * This example is implemented as a logic class (HelloHTTPS) wrapping a TCP socket.
39 * The logic class handles all events, leaving the main loop to just check if the process
40 * has finished.
41 */
42
43/* Change to a number between 1 and 4 to debug the TLS connection */
44#define DEBUG_LEVEL 0
45
46/* Change to 1 to skip certificate verification (UNSAFE, for debug only!) */
47#define UNSAFE 0
48
49#include "mbed.h"
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +020050#include "EthernetInterface.h"
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +020051#include "mbed-net-sockets/TCPStream.h"
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +020052#include "test_env.h"
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +020053#include "minar/minar.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020054
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +020055#include "lwipv4_init.h"
56
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020057#include "mbedtls/ssl.h"
58#include "mbedtls/entropy.h"
59#include "mbedtls/ctr_drbg.h"
60#include "mbedtls/error.h"
61#if DEBUG_LEVEL > 0
62#include "mbedtls/debug.h"
63#endif
64
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020065namespace {
Manuel Pégourié-Gonnard42618b82015-08-17 14:19:38 +020066const char *HTTPS_SERVER_NAME = "developer.mbed.org";
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020067const int HTTPS_SERVER_PORT = 443;
68const int RECV_BUFFER_SIZE = 600;
69
Manuel Pégourié-Gonnard42618b82015-08-17 14:19:38 +020070const char HTTPS_PATH[] = "/media/uploads/mbed_official/hello.txt";
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020071const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1;
72
73/* Test related data */
74const char *HTTPS_OK_STR = "200 OK";
75const char *HTTPS_HELLO_STR = "Hello world!";
76
77/* personalization string for the drbg */
78const char *DRBG_PERS = "mbed TLS helloword client";
79
80/* List of trusted root CA certificates
Simon Butcherdf819242015-08-19 19:33:27 +010081 * currently only GlobalSign, the CA for developer.mbed.org
82 *
83 * To add more than one root, just concatenate them.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020084 */
85const char SSL_CA_PEM[] =
Simon Butcherdf819242015-08-19 19:33:27 +010086/* GlobalSign Root R1 SHA1/RSA/2048
87 * Serial no. 04 00 00 00 00 01 15 4b 5a c3 94 */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020088"-----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é-Gonnardae5398a2015-08-04 14:10:28 +0200111using namespace mbed::Sockets::v0;
112
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200113/**
114 * \brief HelloHTTPS implements the logic for fetching a file from a webserver
115 * using a TCP socket and parsing the result.
116 */
117class HelloHTTPS {
118public:
119 /**
120 * HelloHTTPS Constructor
121 * Initializes the TCP socket, sets up event handlers and flags.
122 *
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200123 * @param[in] domain The domain name to fetch from
124 * @param[in] port The port of the HTTPS server
125 */
126 HelloHTTPS(const char * domain, const uint16_t port) :
127 _stream(SOCKET_STACK_LWIP_IPV4), _domain(domain), _port(port)
128 {
129
130 _error = false;
131 _gothello = false;
132 _got200 = false;
133 _bpos = 0;
134 _request_sent = 0;
135 _stream.open(SOCKET_AF_INET4);
136
137 mbedtls_entropy_init(&_entropy);
138 mbedtls_ctr_drbg_init(&_ctr_drbg);
139 mbedtls_x509_crt_init(&_cacert);
140 mbedtls_ssl_init(&_ssl);
141 mbedtls_ssl_config_init(&_ssl_conf);
142 }
143 /**
Manuel Pégourié-Gonnardfe626132015-08-20 12:00:58 +0200144 * HelloHTTPS Desctructor
145 */
146 ~HelloHTTPS() {
147 mbedtls_entropy_free(&_entropy);
148 mbedtls_ctr_drbg_free(&_ctr_drbg);
149 mbedtls_x509_crt_free(&_cacert);
150 mbedtls_ssl_free(&_ssl);
151 mbedtls_ssl_config_free(&_ssl_conf);
152 }
153 /**
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200154 * Initiate the test.
155 *
156 * Starts by clearing test flags, then resolves the address with DNS.
157 *
158 * @param[in] path The path of the file to fetch from the HTTPS server
159 * @return SOCKET_ERROR_NONE on success, or an error code on failure
160 */
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200161 void startTest(const char *path) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200162 /* Initialize the flags */
163 _got200 = false;
164 _gothello = false;
165 _error = false;
166 _disconnected = false;
167 _request_sent = false;
168 /* Fill the request buffer */
169 _bpos = snprintf(_buffer, sizeof(_buffer) - 1, "GET %s HTTP/1.1\nHost: %s\n\n", path, HTTPS_SERVER_NAME);
170
171 /*
172 * Initialize TLS-related stuf.
173 */
174 int ret;
175 if ((ret = mbedtls_ctr_drbg_seed(&_ctr_drbg, mbedtls_entropy_func, &_entropy,
176 (const unsigned char *) DRBG_PERS,
177 sizeof (DRBG_PERS))) != 0) {
178 print_mbedtls_error("mbedtls_crt_drbg_init", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200179 _error = true;
180 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200181 }
182
183 if ((ret = mbedtls_x509_crt_parse(&_cacert, (const unsigned char *) SSL_CA_PEM,
184 sizeof (SSL_CA_PEM))) != 0) {
185 print_mbedtls_error("mbedtls_x509_crt_parse", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200186 _error = true;
187 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200188 }
189
190 if ((ret = mbedtls_ssl_config_defaults(&_ssl_conf,
191 MBEDTLS_SSL_IS_CLIENT,
192 MBEDTLS_SSL_TRANSPORT_STREAM,
193 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
194 print_mbedtls_error("mbedtls_ssl_config_defaults", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200195 _error = true;
196 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200197 }
198
199 mbedtls_ssl_conf_ca_chain(&_ssl_conf, &_cacert, NULL);
200 mbedtls_ssl_conf_rng(&_ssl_conf, mbedtls_ctr_drbg_random, &_ctr_drbg);
201
202#if UNSAFE
203 mbedtls_ssl_conf_authmode(&_ssl_conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
204#endif
205
206#if DEBUG_LEVEL > 0
207 mbedtls_ssl_conf_verify(&_ssl_conf, my_verify, NULL);
208 mbedtls_ssl_conf_dbg(&_ssl_conf, my_debug, NULL);
209 mbedtls_debug_set_threshold(DEBUG_LEVEL);
210#endif
211
212 if ((ret = mbedtls_ssl_setup(&_ssl, &_ssl_conf)) != 0) {
213 print_mbedtls_error("mbedtls_ssl_setup", ret);
Manuel Pégourié-Gonnard8356fc22015-08-07 14:35:12 +0200214 _error = true;
215 return;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200216 }
217
218 mbedtls_ssl_set_hostname(&_ssl, HTTPS_SERVER_NAME);
219
220 mbedtls_ssl_set_bio(&_ssl, static_cast<void *>(&_stream),
221 ssl_send, ssl_recv, NULL );
222
223
224 /* Connect to the server */
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200225 printf("Starting DNS lookup for %s\r\n", _domain);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200226 /* Resolve the domain name: */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200227 socket_error_t err = _stream.resolve(_domain, TCPStream::DNSHandler_t(this, &HelloHTTPS::onDNS));
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200228 _stream.error_check(err);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200229 }
230 /**
231 * Check if the test has completed.
232 * @return Returns true if done, false otherwise.
233 */
234 bool done() {
235 return _error || (_got200 && _gothello);
236 }
237 /**
238 * Check if there was an error
239 * @return Returns true if there was an error, false otherwise.
240 */
241 bool error() {
242 return _error;
243 }
244 /**
245 * Closes the TCP socket
246 */
247 void close() {
248 _stream.close();
249 while (!_disconnected)
250 __WFI();
251 }
252protected:
253 /**
254 * Helper for pretty-printing mbed TLS error codes
255 */
256 static void print_mbedtls_error(const char *name, int err) {
257 char buf[128];
258 mbedtls_strerror(err, buf, sizeof (buf));
259 printf("%s() failed: -0x%04x (%d): %s\r\n", name, -err, err, buf);
260 }
261
262#if DEBUG_LEVEL > 0
263 /**
264 * Debug callback for mbed TLS
265 * Just prints on the USB serial port
266 */
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200267 static void my_debug(void *ctx, int level, const char *file, int line,
268 const char *str)
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200269 {
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200270 const char *p, *basename;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200271 (void) ctx;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200272
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200273 /* Extract basename from file */
274 for(p = basename = file; *p != '\0'; p++) {
275 if(*p == '/' || *p == '\\') {
276 basename = p + 1;
277 }
278 }
279
280 printf("%s:%04d: |%d| %s", basename, line, level, str);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200281 }
282
283 /**
284 * Certificate verification callback for mbed TLS
285 * Here we only use it to display information on each cert in the chain
286 */
Manuel Pégourié-Gonnarde50f67c2015-08-18 20:52:18 +0200287 static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200288 {
289 char buf[1024];
290 (void) data;
291
292 printf("\nVerifying certificate at depth %d:\n", depth);
293 mbedtls_x509_crt_info(buf, sizeof (buf) - 1, " ", crt);
294 printf("%s", buf);
295
296 if (*flags == 0)
297 printf("No verification issue for this certificate\n");
298 else
299 {
300 mbedtls_x509_crt_verify_info(buf, sizeof (buf), " ! ", *flags);
301 printf("%s\n", buf);
302 }
303
304 return 0;
305 }
306#endif
307
308 /**
309 * Receive callback for mbed TLS
310 */
311 static int ssl_recv(void *ctx, unsigned char *buf, size_t len) {
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200312 TCPStream *stream = static_cast<TCPStream *>(ctx);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200313 socket_error_t err = stream->recv(buf, &len);
314
315 if (err == SOCKET_ERROR_NONE) {
316 return static_cast<int>(len);
317 } else if (err == SOCKET_ERROR_WOULD_BLOCK) {
318 return MBEDTLS_ERR_SSL_WANT_READ;
319 } else {
320 return -1;
321 }
322 }
323
324 /**
325 * Send callback for mbed TLS
326 */
327 static int ssl_send(void *ctx, const unsigned char *buf, size_t len) {
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200328 TCPStream *stream = static_cast<TCPStream *>(ctx);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200329
330 socket_error_t err = stream->send(buf, len);
331
332 if (err == SOCKET_ERROR_NONE) {
333 return static_cast<int>(len);
334 } else if (err == SOCKET_ERROR_WOULD_BLOCK) {
335 return MBEDTLS_ERR_SSL_WANT_WRITE;
336 } else {
337 return -1;
338 }
339 }
340
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200341 void onError(Socket *s, socket_error_t err) {
342 (void) s;
343 printf("MBED: Socket Error: %s (%d)\r\n", socket_strerror(err), err);
344 _stream.close();
345 _error = true;
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200346 MBED_HOSTTEST_RESULT(false);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200347 }
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200348 /**
349 * On Connect handler
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200350 * Starts the TLS handshake
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200351 */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200352 void onConnect(TCPStream *s) {
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200353 char buf[16];
354 _remoteAddr.fmtIPv4(buf,sizeof(buf));
355 printf("Connected to %s:%d\r\n", buf, _port);
356
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200357 s->setOnReadable(TCPStream::ReadableHandler_t(this, &HelloHTTPS::onReceive));
358 s->setOnDisconnect(TCPStream::DisconnectHandler_t(this, &HelloHTTPS::onDisconnect));
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200359
360 /* Start the handshake, the rest will be done in onReceive() */
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200361 printf("Starting the TLS handshake...\r\n");
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200362 int ret = mbedtls_ssl_handshake(&_ssl);
363 if (ret < 0) {
364 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
365 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
366 print_mbedtls_error("mbedtls_ssl_handshake", ret);
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200367 onError(s, SOCKET_ERROR_UNKNOWN);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200368 }
369 return;
370 }
371 }
372 /**
373 * On Receive handler
374 * Parses the response from the server, to check for the HTTPS 200 status code and the expected response ("Hello World!")
375 */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200376 void onReceive(Socket *s) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200377 /* Send request if not done yet */
378 if (!_request_sent) {
379 int ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos);
380 if (ret < 0) {
381 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
382 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
383 print_mbedtls_error("mbedtls_ssl_write", ret);
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200384 onError(s, SOCKET_ERROR_UNKNOWN);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200385 }
386 return;
387 }
388
389 /* If we get here, the request was sent */
390 _request_sent = 1;
391
392 /* It also means the handshake is done, time to print info */
393 printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME);
394 {
395 char buf[1024];
396 mbedtls_x509_crt_info(buf, sizeof(buf), "\r ",
397 mbedtls_ssl_get_peer_cert(&_ssl));
398 printf("Server certificate:\r\n%s\r", buf);
399
400#if defined(UNSAFE)
401 uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl);
402 if( flags != 0 )
403 {
404 mbedtls_x509_crt_verify_info(buf, sizeof (buf), "\r ! ", flags);
405 printf("Certificate verification failed:\r\n%s\r\r\n", buf);
406 }
407 else
408#endif
409 printf("Certificate verification passed\r\n\r\n");
410 }
411 }
412
413 /* Read data out of the socket */
414 int ret = mbedtls_ssl_read(&_ssl, (unsigned char *) _buffer, sizeof(_buffer));
415 if (ret < 0) {
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200416 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
417 print_mbedtls_error("mbedtls_ssl_read", ret);
418 onError(s, SOCKET_ERROR_UNKNOWN);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200419 }
420 return;
421 }
422 _bpos = static_cast<size_t>(ret);
423
424 _buffer[_bpos] = 0;
425
426 /* Check each of the flags */
427 _got200 = _got200 || strstr(_buffer, HTTPS_OK_STR) != NULL;
428 _gothello = _gothello || strstr(_buffer, HTTPS_HELLO_STR) != NULL;
429
430 /* Print status messages */
431 printf("HTTPS: Received %d chars from server\r\n", _bpos);
432 printf("HTTPS: Received 200 OK status ... %s\r\n", _got200 ? "[OK]" : "[FAIL]");
433 printf("HTTPS: Received '%s' status ... %s\r\n", HTTPS_HELLO_STR, _gothello ? "[OK]" : "[FAIL]");
434 printf("HTTPS: Received message:\r\n\r\n");
435 printf("%s", _buffer);
436 _error = !(_got200 && _gothello);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200437
438 s->close();
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200439 }
440 /**
441 * On DNS Handler
442 * Reads the address returned by DNS, then starts the connect process.
443 */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200444 void onDNS(Socket *s, struct socket_addr addr, const char *domain) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200445 /* Check that the result is a valid DNS response */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200446 if (socket_addr_is_any(&addr)) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200447 /* Could not find DNS entry */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200448 printf("Could not find DNS entry for %s", HTTPS_SERVER_NAME);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200449 onError(s, SOCKET_ERROR_DNS_FAILED);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200450 } else {
451 /* Start connecting to the remote host */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200452 char buf[16];
453 _remoteAddr.setAddr(&addr);
454 _remoteAddr.fmtIPv4(buf,sizeof(buf));
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200455 printf("DNS Response Received:\r\n%s: %s\r\n", domain, buf);
456 printf("Connecting to %s:%d\r\n", buf, _port);
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200457 socket_error_t err = _stream.connect(_remoteAddr, _port, TCPStream::ConnectHandler_t(this, &HelloHTTPS::onConnect));
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200458
459 if (err != SOCKET_ERROR_NONE) {
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200460 onError(s, err);
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200461 }
462 }
463 }
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200464 void onDisconnect(TCPStream *s) {
465 s->close();
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200466 MBED_HOSTTEST_RESULT(!error());
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200467 }
468
469protected:
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200470 TCPStream _stream; /**< The TCP Socket */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200471 const char *_domain; /**< The domain name of the HTTPS server */
472 const uint16_t _port; /**< The HTTPS server port */
473 char _buffer[RECV_BUFFER_SIZE]; /**< The response buffer */
474 size_t _bpos; /**< The current offset in the response buffer */
Manuel Pégourié-Gonnardae5398a2015-08-04 14:10:28 +0200475 SocketAddr _remoteAddr; /**< The remote address */
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200476 volatile bool _got200; /**< Status flag for HTTPS 200 */
477 volatile bool _gothello; /**< Status flag for finding the test string */
478 volatile bool _error; /**< Status flag for an error */
479 volatile bool _disconnected;
480 volatile bool _request_sent;
481
482 mbedtls_entropy_context _entropy;
483 mbedtls_ctr_drbg_context _ctr_drbg;
484 mbedtls_x509_crt _cacert;
485 mbedtls_ssl_context _ssl;
486 mbedtls_ssl_config _ssl_conf;
487};
488
489/**
490 * The main loop of the HTTPS Hello World test
491 */
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200492EthernetInterface eth;
493HelloHTTPS *hello;
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200494
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200495void app_start(int, char*[]) {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200496 /* The default 9600 bps is too slow to print full TLS debug info and could
497 * cause the other party to time out. Select a higher baud rate for
498 * printf(), regardless of debug level for the sake of uniformity. */
499 Serial pc(USBTX, USBRX);
500 pc.baud(115200);
501
502 MBED_HOSTTEST_TIMEOUT(120);
503 MBED_HOSTTEST_SELECT(default);
504 MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client);
505 MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT");
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200506
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200507 /* Initialise with DHCP, connect, and start up the stack */
508 eth.init();
509 eth.connect();
510 lwipv4_socket_init();
511
512 hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
513
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200514 printf("Client IP Address is %s\r\n", eth.getIPAddress());
515
Manuel Pégourié-Gonnardca4fb712015-09-18 14:36:57 +0200516 mbed::util::FunctionPointer1<void, const char*> fp(hello, &HelloHTTPS::startTest);
Manuel Pégourié-Gonnardbd6d0ab2015-08-18 18:18:16 +0200517 minar::Scheduler::postCallback(fp.bind(HTTPS_PATH));
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200518}
519
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200520#endif /* TARGET_LIKE_MBED */