1
1
/*
2
2
* Hello world example of a TLS client: fetch an HTTPS page
3
3
*
4
- * Copyright (C) 2006-2015 , ARM Limited, All Rights Reserved
4
+ * Copyright (C) 2006-2016 , ARM Limited, All Rights Reserved
5
5
* SPDX-License-Identifier: Apache-2.0
6
6
*
7
7
* Licensed under the Apache License, Version 2.0 (the "License"); you may
19
19
* This file is part of mbed TLS (https://tls.mbed.org)
20
20
*/
21
21
22
- #if !defined(TARGET_LIKE_MBED)
23
-
24
- #include < stdio.h>
25
-
26
- int main () {
27
- printf (" This program only works on mbed OS.\n " );
28
- return 0 ;
29
- }
30
-
31
- #else
32
-
33
22
/* * \file main.cpp
34
23
* \brief An example TLS Client application
35
24
* This application sends an HTTPS request to developer.mbed.org and searches for a string in
@@ -48,12 +37,9 @@ int main() {
48
37
49
38
#include " mbed.h"
50
39
#include " NetworkStack.h"
51
- #include " LWIPInterface.h"
52
40
53
- // #include "EthernetInterface.h"
41
+ #include " EthernetInterface.h"
54
42
#include " TCPSocket.h"
55
- #include " test_env.h"
56
- // #include "lwipv4_init.h"
57
43
58
44
#include " mbedtls/platform.h"
59
45
#include " mbedtls/ssl.h"
@@ -66,9 +52,6 @@ int main() {
66
52
67
53
namespace {
68
54
69
- Serial output (USBTX, USBRX);
70
- NetworkStack *network_stack = NULL ;
71
-
72
55
const char *HTTPS_SERVER_NAME = " developer.mbed.org" ;
73
56
const int HTTPS_SERVER_PORT = 443 ;
74
57
const int RECV_BUFFER_SIZE = 600 ;
@@ -144,8 +127,6 @@ const char SSL_CA_PEM[] =
144
127
#endif
145
128
}
146
129
147
- // using namespace mbed::Sockets::v0;
148
-
149
130
/* *
150
131
* \brief HelloHTTPS implements the logic for fetching a file from a webserver
151
132
* using a TCP socket and parsing the result.
@@ -159,7 +140,7 @@ class HelloHTTPS {
159
140
* @param[in] domain The domain name to fetch from
160
141
* @param[in] port The port of the HTTPS server
161
142
*/
162
- HelloHTTPS (const char * domain, const uint16_t port) :
143
+ HelloHTTPS (const char * domain, const uint16_t port, NetworkInterface *net_iface ) :
163
144
_domain (domain), _port(port)
164
145
{
165
146
@@ -168,7 +149,7 @@ class HelloHTTPS {
168
149
_got200 = false ;
169
150
_bpos = 0 ;
170
151
_request_sent = 0 ;
171
- _tcpsocket = new TCPSocket (network_stack );
152
+ _tcpsocket = new TCPSocket (net_iface );
172
153
173
154
mbedtls_entropy_init (&_entropy);
174
155
mbedtls_ctr_drbg_init (&_ctr_drbg);
@@ -258,11 +239,11 @@ class HelloHTTPS {
258
239
259
240
260
241
/* Connect to the server */
261
- output. printf (" Connecting with %s\r\n " , _domain);
242
+ mbedtls_printf (" Connecting with %s\r\n " , _domain);
262
243
_tcpsocket->connect ( _domain, _port );
263
244
264
245
/* Start the handshake, the rest will be done in onReceive() */
265
- output. printf (" Starting the TLS handshake...\r\n " );
246
+ mbedtls_printf (" Starting the TLS handshake...\r\n " );
266
247
ret = mbedtls_ssl_handshake (&_ssl);
267
248
if (ret < 0 ) {
268
249
if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
@@ -289,7 +270,7 @@ class HelloHTTPS {
289
270
char buf[1024 ];
290
271
mbedtls_x509_crt_info (buf, sizeof (buf), " \r " ,
291
272
mbedtls_ssl_get_peer_cert (&_ssl));
292
- output. printf (" Server certificate:\r\n %s\r " , buf);
273
+ mbedtls_printf (" Server certificate:\r\n %s\r " , buf);
293
274
294
275
#if defined(UNSAFE)
295
276
uint32_t flags = mbedtls_ssl_get_verify_result (&_ssl);
@@ -321,15 +302,14 @@ class HelloHTTPS {
321
302
_gothello = _gothello || strstr (_buffer, HTTPS_HELLO_STR) != NULL ;
322
303
323
304
/* Print status messages */
324
- output. printf (" HTTPS: Received %d chars from server\r\n " , _bpos);
325
- output. printf (" HTTPS: Received 200 OK status ... %s\r\n " , _got200 ? " [OK]" : " [FAIL]" );
326
- output. printf (" HTTPS: Received '%s' status ... %s\r\n " , HTTPS_HELLO_STR, _gothello ? " [OK]" : " [FAIL]" );
327
- output. printf (" HTTPS: Received message:\r\n\r\n " );
328
- output. printf (" %s" , _buffer);
305
+ mbedtls_printf (" HTTPS: Received %d chars from server\r\n " , _bpos);
306
+ mbedtls_printf (" HTTPS: Received 200 OK status ... %s\r\n " , _got200 ? " [OK]" : " [FAIL]" );
307
+ mbedtls_printf (" HTTPS: Received '%s' status ... %s\r\n " , HTTPS_HELLO_STR, _gothello ? " [OK]" : " [FAIL]" );
308
+ mbedtls_printf (" HTTPS: Received message:\r\n\r\n " );
309
+ mbedtls_printf (" %s" , _buffer);
329
310
_error = !(_got200 && _gothello);
330
311
331
312
_tcpsocket->close ();
332
- // MBED_HOSTTEST_RESULT(!error());
333
313
}
334
314
/* *
335
315
* Check if the test has completed.
@@ -360,7 +340,7 @@ class HelloHTTPS {
360
340
static void print_mbedtls_error (const char *name, int err) {
361
341
char buf[128 ];
362
342
mbedtls_strerror (err, buf, sizeof (buf));
363
- output. printf (" %s() failed: -0x%04x (%d): %s\r\n " , name, -err, err, buf);
343
+ mbedtls_printf (" %s() failed: -0x%04x (%d): %s\r\n " , name, -err, err, buf);
364
344
}
365
345
366
346
#if DEBUG_LEVEL > 0
@@ -381,7 +361,7 @@ class HelloHTTPS {
381
361
}
382
362
}
383
363
384
- output. printf (" %s:%04d: |%d| %s" , basename, line, level, str);
364
+ mbedtls_printf (" %s:%04d: |%d| %s" , basename, line, level, str);
385
365
}
386
366
387
367
/* *
@@ -393,16 +373,16 @@ class HelloHTTPS {
393
373
char buf[1024 ];
394
374
(void ) data;
395
375
396
- output. printf (" \n Verifying certificate at depth %d:\n " , depth);
376
+ mbedtls_printf (" \n Verifying certificate at depth %d:\n " , depth);
397
377
mbedtls_x509_crt_info (buf, sizeof (buf) - 1 , " " , crt);
398
- output. printf (" %s" , buf);
378
+ mbedtls_printf (" %s" , buf);
399
379
400
380
if (*flags == 0 )
401
- output. printf (" No verification issue for this certificate\n " );
381
+ mbedtls_printf (" No verification issue for this certificate\n " );
402
382
else
403
383
{
404
384
mbedtls_x509_crt_verify_info (buf, sizeof (buf), " ! " , *flags);
405
- output. printf (" %s\n " , buf);
385
+ mbedtls_printf (" %s\n " , buf);
406
386
}
407
387
408
388
return 0 ;
@@ -447,17 +427,8 @@ class HelloHTTPS {
447
427
printf (" MBED: Socket Error: %d\r\n " , error);
448
428
s->close ();
449
429
_error = true ;
450
- // MBED_HOSTTEST_RESULT(false);
451
- }
452
-
453
- #if 0
454
- void onDisconnect(TCPStream *s) {
455
- s->close();
456
- MBED_HOSTTEST_RESULT(!error());
457
430
}
458
431
459
- #endif
460
-
461
432
protected:
462
433
TCPSocket* _tcpsocket;
463
434
@@ -483,34 +454,19 @@ class HelloHTTPS {
483
454
*/
484
455
int main () {
485
456
/* The default 9600 bps is too slow to print full TLS debug info and could
486
- * cause the other party to time out. Select a higher baud rate for
487
- * printf(), regardless of debug level for the sake of uniformity. */
488
-
489
- // Sets the console baud-rate
490
- output.baud (115200 );
491
-
492
- // MBED_HOSTTEST_TIMEOUT(120);
493
- // MBED_HOSTTEST_SELECT(_default);
494
- // MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client);
495
- // MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT");
457
+ * cause the other party to time out. */
496
458
497
- /* Initialise with DHCP, connect, and start up the stack */
498
- LWIPInterface lwip;
499
-
500
- lwip.connect ();
501
- output.printf (" Using Ethernet LWIP\r\n " );
502
- network_stack = &lwip;
503
-
504
- const char *ip_addr = network_stack->get_ip_address ();
459
+ /* Inititalise with DHCP, connect, and start up the stack */
460
+ EthernetInterface eth_iface;
461
+ eth_iface.connect ();
462
+ mbedtls_printf (" Using Ethernet LWIP\r\n " );
463
+ const char *ip_addr = eth_iface.get_ip_address ();
505
464
if (ip_addr) {
506
- output. printf (" Client IP Address is %s\r\n " ,ip_addr);
465
+ mbedtls_printf (" Client IP Address is %s\r\n " , ip_addr);
507
466
} else {
508
- output. printf (" No Client IP Address\r\n " );
467
+ mbedtls_printf (" No Client IP Address\r\n " );
509
468
}
510
469
511
-
512
- HelloHTTPS hello (HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
470
+ HelloHTTPS hello (HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, ð_iface);
513
471
hello.startTest (HTTPS_PATH);
514
472
}
515
-
516
- #endif /* TARGET_LIKE_MBED */
0 commit comments