Skip to content

Commit 65a1660

Browse files
Andres Amaya Garciasimonbutcher
Andres Amaya Garcia
authored andcommitted
Fix TLS client sample (#5)
* Update deprecated API calls in tls-client example * Remove serial object from tls-client sample * Update sample output in tls-client/README.md * Fix default baud rate in README.md
1 parent d482afc commit 65a1660

File tree

4 files changed

+69
-125
lines changed

4 files changed

+69
-125
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Please browse the subdirectories for specific documentation.
4040
4141
The application prints debug messages over the serial port, so you can monitor its activity with a serial terminal emulator. Start the [serial terminal emulator](https://developer.mbed.org/handbook/Terminals) and connect to the [virtual serial port](https://developer.mbed.org/handbook/SerialPC#host-interface-and-terminal-applications) presented by FRDM-K64F. Use the following settings:
4242
43-
* 115200 baud (not 9600).
43+
* 9600 baud.
4444
* 8N1.
4545
* No flow control.
4646

tls-client/README.md

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,46 @@ __NOTE:__ Make sure that the Ethernet cable is plugged in correctly before runni
1717
The output in the terminal window should be similar to this:
1818

1919
```
20-
{{timeout;120}}
21-
{{host_test_name;default}}
22-
{{description;mbed TLS example HTTPS client}}
23-
{{test_id;MBEDTLS_EX_HTTPS_CLIENT}}
24-
{{start}}
25-
26-
Client IP Address is 192.168.0.2
27-
Starting DNS lookup for developer.mbed.org
28-
DNS Response Received:
29-
developer.mbed.org: 217.140.101.30
30-
Connecting to 217.140.101.30:443
31-
Connected to 217.140.101.30:443
32-
Starting the TLS handshake...
33-
TLS connection to developer.mbed.org established
34-
Server certificate:
35-
cert. version : 3
36-
serial number : 11:21:4E:4B:13:27:F0:89:21:FB:70:EC:3B:B5:73:5C:FF:B9
37-
issuer name : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2
38-
subject name : C=GB, ST=Cambridgeshire, L=Cambridge, O=ARM Ltd, CN=*.mbed.com
39-
issued on : 2015-03-05 10:31:02
40-
expires on : 2016-03-05 10:31:02
41-
signed using : RSA with SHA-256
42-
RSA key size : 2048 bits
43-
basic constraints : CA=false
44-
subject alt name : *.mbed.com, *.mbed.org, mbed.org, mbed.com
45-
key usage : Digital Signature, Key Encipherment
46-
ext key usage : TLS Web Server Authentication, TLS Web Client Authentication
47-
Certificate verification passed
48-
49-
HTTPS: Received 473 chars from server
50-
HTTPS: Received 200 OK status ... [OK]
51-
HTTPS: Received 'Hello world!' status ... [OK]
52-
HTTPS: Received message:
53-
54-
HTTP/1.1 200 OK
55-
Server: nginx/1.7.10
56-
Date: Tue, 18 Aug 2015 18:34:04 GMT
57-
Content-Type: text/plain
58-
Content-Length: 14
59-
Connection: keep-alive
60-
Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
61-
Accept-Ranges: bytes
62-
Cache-Control: max-age=36000
63-
Expires: Wed, 19 Aug 2015 04:34:04 GMT
64-
X-Upstream-L3: 172.17.42.1:8080
65-
X-Upstream-L2: developer-sjc-indigo-2-nginx
66-
X-Upstream-L1-next-hop: 217.140.101.86:8001
67-
X-Upstream-L1: developer-sjc-indigo-border-nginx
68-
69-
Hello world!
70-
{{success}}
71-
{{end}}
20+
Using Ethernet LWIP
21+
Client IP Address is 10.2.203.43
22+
Connecting with developer.mbed.org
23+
Starting the TLS handshake...
24+
TLS connection to developer.mbed.org established
25+
Server certificate:
26+
cert. version : 3
27+
serial number : 11:21:B8:47:9B:21:6C:B1:C6:AF:BC:5D:0C:19:52:DC:D7:C3
28+
issuer name : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2
29+
subject name : C=GB, ST=Cambridgeshire, L=Cambridge, O=ARM Ltd, CN=*.mbed.com
30+
issued on : 2016-03-03 12:26:08
31+
expires on : 2017-04-05 10:31:02
32+
signed using : RSA with SHA-256
33+
RSA key size : 2048 bits
34+
basic constraints : CA=false
35+
subject alt name : *.mbed.com, mbed.org, *.mbed.org, mbed.com
36+
key usage : Digital Signature, Key Encipherment
37+
ext key usage : TLS Web Server Authentication, TLS Web Client Authentication
38+
Certificate verification passed
39+
40+
HTTPS: Received 439 chars from server
41+
HTTPS: Received 200 OK status ... [OK]
42+
HTTPS: Received 'Hello world!' status ... [OK]
43+
HTTPS: Received message:
44+
45+
HTTP/1.1 200 OK
46+
Server: nginx/1.7.10
47+
Date: Wed, 20 Jul 2016 10:00:35 GMT
48+
Content-Type: text/plain
49+
Content-Length: 14
50+
Connection: keep-alive
51+
Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
52+
Accept-Ranges: bytes
53+
Cache-Control: max-age=36000
54+
Expires: Wed, 20 Jul 2016 20:00:35 GMT
55+
X-Upstream-L3: 172.17.0.3:80
56+
X-Upstream-L2: developer-sjc-indigo-1-nginx
57+
Strict-Transport-Security: max-age=31536000; includeSubdomains
58+
59+
Hello world!
7260
```
7361

7462
## Debugging the TLS connection

tls-client/main.cpp

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Hello world example of a TLS client: fetch an HTTPS page
33
*
4-
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
4+
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
55
* SPDX-License-Identifier: Apache-2.0
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -19,17 +19,6 @@
1919
* This file is part of mbed TLS (https://tls.mbed.org)
2020
*/
2121

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-
3322
/** \file main.cpp
3423
* \brief An example TLS Client application
3524
* This application sends an HTTPS request to developer.mbed.org and searches for a string in
@@ -48,12 +37,9 @@ int main() {
4837

4938
#include "mbed.h"
5039
#include "NetworkStack.h"
51-
#include "LWIPInterface.h"
5240

53-
//#include "EthernetInterface.h"
41+
#include "EthernetInterface.h"
5442
#include "TCPSocket.h"
55-
#include "test_env.h"
56-
//#include "lwipv4_init.h"
5743

5844
#include "mbedtls/platform.h"
5945
#include "mbedtls/ssl.h"
@@ -66,9 +52,6 @@ int main() {
6652

6753
namespace {
6854

69-
Serial output(USBTX, USBRX);
70-
NetworkStack *network_stack = NULL;
71-
7255
const char *HTTPS_SERVER_NAME = "developer.mbed.org";
7356
const int HTTPS_SERVER_PORT = 443;
7457
const int RECV_BUFFER_SIZE = 600;
@@ -144,8 +127,6 @@ const char SSL_CA_PEM[] =
144127
#endif
145128
}
146129

147-
//using namespace mbed::Sockets::v0;
148-
149130
/**
150131
* \brief HelloHTTPS implements the logic for fetching a file from a webserver
151132
* using a TCP socket and parsing the result.
@@ -159,7 +140,7 @@ class HelloHTTPS {
159140
* @param[in] domain The domain name to fetch from
160141
* @param[in] port The port of the HTTPS server
161142
*/
162-
HelloHTTPS(const char * domain, const uint16_t port) :
143+
HelloHTTPS(const char * domain, const uint16_t port, NetworkInterface *net_iface) :
163144
_domain(domain), _port(port)
164145
{
165146

@@ -168,7 +149,7 @@ class HelloHTTPS {
168149
_got200 = false;
169150
_bpos = 0;
170151
_request_sent = 0;
171-
_tcpsocket = new TCPSocket(network_stack);
152+
_tcpsocket = new TCPSocket(net_iface);
172153

173154
mbedtls_entropy_init(&_entropy);
174155
mbedtls_ctr_drbg_init(&_ctr_drbg);
@@ -258,11 +239,11 @@ class HelloHTTPS {
258239

259240

260241
/* Connect to the server */
261-
output.printf("Connecting with %s\r\n", _domain);
242+
mbedtls_printf("Connecting with %s\r\n", _domain);
262243
_tcpsocket->connect( _domain, _port );
263244

264245
/* 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");
266247
ret = mbedtls_ssl_handshake(&_ssl);
267248
if (ret < 0) {
268249
if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
@@ -289,7 +270,7 @@ class HelloHTTPS {
289270
char buf[1024];
290271
mbedtls_x509_crt_info(buf, sizeof(buf), "\r ",
291272
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);
293274

294275
#if defined(UNSAFE)
295276
uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl);
@@ -321,15 +302,14 @@ class HelloHTTPS {
321302
_gothello = _gothello || strstr(_buffer, HTTPS_HELLO_STR) != NULL;
322303

323304
/* 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);
329310
_error = !(_got200 && _gothello);
330311

331312
_tcpsocket->close();
332-
// MBED_HOSTTEST_RESULT(!error());
333313
}
334314
/**
335315
* Check if the test has completed.
@@ -360,7 +340,7 @@ class HelloHTTPS {
360340
static void print_mbedtls_error(const char *name, int err) {
361341
char buf[128];
362342
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);
364344
}
365345

366346
#if DEBUG_LEVEL > 0
@@ -381,7 +361,7 @@ class HelloHTTPS {
381361
}
382362
}
383363

384-
output.printf("%s:%04d: |%d| %s", basename, line, level, str);
364+
mbedtls_printf("%s:%04d: |%d| %s", basename, line, level, str);
385365
}
386366

387367
/**
@@ -393,16 +373,16 @@ class HelloHTTPS {
393373
char buf[1024];
394374
(void) data;
395375

396-
output.printf("\nVerifying certificate at depth %d:\n", depth);
376+
mbedtls_printf("\nVerifying certificate at depth %d:\n", depth);
397377
mbedtls_x509_crt_info(buf, sizeof (buf) - 1, " ", crt);
398-
output.printf("%s", buf);
378+
mbedtls_printf("%s", buf);
399379

400380
if (*flags == 0)
401-
output.printf("No verification issue for this certificate\n");
381+
mbedtls_printf("No verification issue for this certificate\n");
402382
else
403383
{
404384
mbedtls_x509_crt_verify_info(buf, sizeof (buf), " ! ", *flags);
405-
output.printf("%s\n", buf);
385+
mbedtls_printf("%s\n", buf);
406386
}
407387

408388
return 0;
@@ -447,17 +427,8 @@ class HelloHTTPS {
447427
printf("MBED: Socket Error: %d\r\n", error);
448428
s->close();
449429
_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());
457430
}
458431

459-
#endif
460-
461432
protected:
462433
TCPSocket* _tcpsocket;
463434

@@ -483,34 +454,19 @@ class HelloHTTPS {
483454
*/
484455
int main() {
485456
/* 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. */
496458

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();
505464
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);
507466
} else {
508-
output.printf("No Client IP Address\r\n");
467+
mbedtls_printf("No Client IP Address\r\n");
509468
}
510469

511-
512-
HelloHTTPS hello(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
470+
HelloHTTPS hello(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, &eth_iface);
513471
hello.startTest(HTTPS_PATH);
514472
}
515-
516-
#endif /* TARGET_LIKE_MBED */

tls-client/mbed-os.lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/ARMmbed/mbed-os/#c0c5a70b1907d701f69f37b8812b5fe2ce048ff0
1+
https://github.com/ARMmbed/mbed-os/#b7b6dd2c8769251c66d68911f116ec899c7054f7

0 commit comments

Comments
 (0)