Hello world example of a TLS client: fetch an HTTPS page. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-tls

HTTPS File Download Example for TLS Client on mbed OS

This application downloads a file from an HTTPS server (developer.mbed.org) and looks for a specific string in that file.

Getting started

Building with mbed CLI

If you'd like to use mbed CLI to build this, then you should set up your environment if you have not done so already. For instructions, refer to the main readme. The instructions here relate to using the developer.mbed.org Online Compiler

Import the program in to the Online Compiler, select your board from the drop down in the top right hand corner and then compile the application. Once it has built, you can drag and drop the binary onto your device.

Required hardware

This example also requires an Ethernet cable an connection to the internet additional to the hardware requirements in the main readme.

Monitoring the application

NOTE: Make sure that the Ethernet cable is plugged in correctly before running the application.

The output in the terminal window should be similar to this:

terminal output

Using Ethernet LWIP
Client IP Address is 10.2.203.43
Connecting with developer.mbed.org
Starting the TLS handshake...
TLS connection to developer.mbed.org established
Server certificate:
    cert. version     : 3
    serial number     : 11:21:B8:47:9B:21:6C:B1:C6:AF:BC:5D:0C:19:52:DC:D7:C3
    issuer name       : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2
    subject name      : C=GB, ST=Cambridgeshire, L=Cambridge, O=ARM Ltd, CN=*.mbed.com
    issued  on        : 2016-03-03 12:26:08
    expires on        : 2017-04-05 10:31:02
    signed using      : RSA with SHA-256
    RSA key size      : 2048 bits
    basic constraints : CA=false
    subject alt name  : *.mbed.com, mbed.org, *.mbed.org, mbed.com
    key usage         : Digital Signature, Key Encipherment
    ext key usage     : TLS Web Server Authentication, TLS Web Client Authentication
Certificate verification passed

HTTPS: Received 439 chars from server
HTTPS: Received 200 OK status ... [OK]
HTTPS: Received 'Hello world!' status ... [OK]
HTTPS: Received message:

HTTP/1.1 200 OK
Server: nginx/1.7.10
Date: Wed, 20 Jul 2016 10:00:35 GMT
Content-Type: text/plain
Content-Length: 14
Connection: keep-alive
Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
Accept-Ranges: bytes
Cache-Control: max-age=36000
Expires: Wed, 20 Jul 2016 20:00:35 GMT
X-Upstream-L3: 172.17.0.3:80
X-Upstream-L2: developer-sjc-indigo-1-nginx
Strict-Transport-Security: max-age=31536000; includeSubdomains

Hello world!

Debugging the TLS connection

To print out more debug information about the TLS connection, edit the file `main.cpp` and change the definition of `DEBUG_LEVEL` (near the top of the file) from 0 to a positive number:

  • Level 1 only prints non-zero return codes from SSL functions and information about the full certificate chain being verified.
  • Level 2 prints more information about internal state updates.
  • Level 3 is intermediate.
  • Level 4 (the maximum) includes full binary dumps of the packets.

The TLS connection can fail with an error similar to:

error message

    mbedtls_ssl_write() failed: -0x2700 (-9984): X509 - Certificate verification failed, e.g. CRL, CA or signature check failed
    Failed to fetch /media/uploads/mbed_official/hello.txt from developer.mbed.org:443

This probably means you need to update the contents of the SSL_CA_PEM constant (this can happen if you modify HTTPS_SERVER_NAME, or when developer.mbed.org switches to a new CA when updating its certificate).

Another possible reason for this error is a proxy providing a different certificate. Proxies can be used in some network configurations or for performing man-in-the-middle attacks. If you choose to ignore this error and proceed with the connection anyway, you can change the definition of UNSAFE near the top of the file from 0 to 1.

Warning: this removes all security against a possible active attacker, so use at your own risk or for debugging only!

Changes

RevisionDateWhoCommit message
98:54b91f4b0c49 2019-11-18 mbed_official Merge pull request #264 from dgreen-arm/point-master-at-mbed-os-master default tip
97:55368c4a101f 2019-02-25 mbed_official Merge pull request #235 from adbridge/master
96:424d22ca2cce 2019-02-13 mbed_official Merge pull request #234 from adbridge/master
95:d282bc7f32e4 2019-01-28 mbed_official Merge pull request #232 from ARMmbed/tls-client_psa
94:23cb70e63007 2019-01-17 mbed_official Merge pull request #231 from cmonr/master
93:eeb015d879ec 2019-01-08 mbed_official Merge pull request #220 from RonEld/undefine_mpi_max_size
92:0c5a8e282a2a 2019-01-07 mbed_official Merge pull request #222 from dgreen-arm/tls-client-ublox-fix
91:0bb80113f9c2 2019-01-04 mbed_official Merge pull request #225 from adbridge/master
90:4aaeb25cfdeb 2018-12-28 mbed_official Merge pull request #224 from ARMmbed/mbed-os-5.11.0-oob
89:75bc6e025eaa 2018-11-21 mbed_official Merge pull request #215 from cmonr/master
88:9a774920414b 2018-11-20 mbed_official Merge pull request #217 from k-stachowiak/update-tls-client-ca
87:750530169f06 2018-11-06 mbed_official Merge pull request #212 from adbridge/master
86:e1ceb1075f1a 2018-10-31 mbed_official Merge pull request #199 from RonEld/set_NULL_as_platform_context
85:7a91af2ccf6e 2018-10-23 mbed_official Merge pull request #207 from cmonr/master
84:c71ee4f9d7cc 2018-10-15 mbed_official Merge pull request #204 from cmonr/master
83:6dbd4cae4527 2018-10-08 mbed_official Merge pull request #126 from andresag01/line-endings
82:1c4623fef8ed 2018-10-08 mbed_official Update to Mbed OS 5.10.0 (#202)
81:1b9acc423bfc 2018-09-27 mbed_official Don't use easy-connet, use NetworkInterface::get_default_instance() (#198)
80:896adcac0aee 2018-09-10 mbed_official Merge pull request #200 from adbridge/master
79:52fbb784177a 2018-08-27 mbed_official Merge pull request #197 from 0xc0170/master
78:1cef64db75dd 2018-08-15 mbed_official Merge pull request #195 from cmonr/master
77:6a727b3c3ef1 2018-07-31 mbed_official Merge pull request #188 from cmonr/master
76:b8c07be20122 2018-07-19 mbed_official Merge pull request #174 from RonEld/seperate_server_name
75:98d7217a443d 2018-07-16 mbed_official Merge pull request #182 from cmonr/master
74:012fb2ed0bca 2018-07-02 mbed_official Merge pull request #180 from adbridge/master
73:ddf8f2dca939 2018-06-19 mbed_official Merge pull request #177 from adbridge/master
72:a8326c014358 2018-06-15 mbed_official Merge pull request #176 from k-stachowiak/update-mbed-os-5.9.0
71:44e2a19a02ff 2018-06-07 mbed_official Merge pull request #175 from adbridge/master
70:6a43581f1280 2018-05-21 mbed_official Merge pull request #169 from adbridge/master
69:ffefb2e2d149 2018-05-10 mbed_official Merge pull request #165 from ARMmbed/feature-platform-init
68:6d74abebd544 2018-05-04 mbed_official Merge pull request #151 from JanneKiiskila/patch-1
67:3bf1bf2323fa 2018-05-04 mbed_official Merge pull request #145 from JanneKiiskila/upd-easy-4-feb
66:ce8709d9912c 2018-05-04 mbed_official Merge pull request #87 from andresag01/iotssl-1247-tls-client-refactoring
65:a358e0ad3471 2018-05-02 mbed_official Merge pull request #166 from adbridge/master
64:963d9a67d3c3 2018-04-11 mbed_official Merge pull request #164 from adbridge/master
63:66b2078f0e26 2018-03-28 mbed_official Merge pull request #162 from adbridge/master
62:a0b3ed2d1227 2018-03-16 mbed_official Merge pull request #156 from cmonr/master
61:d4c9261e053d 2018-02-26 mbed_official Merge pull request #149 from adbridge/master
60:3cdcad04ff96 2018-02-14 mbed_official Merge pull request #147 from adbridge/master
59:209e439ae9dd 2018-01-31 mbed_official Merge pull request #144 from cmonr/master
58:9a88026ca63f 2018-01-16 mbed_official Merge pull request #143 from adbridge/master
57:d099f06e5c8c 2018-01-03 mbed_official Merge pull request #141 from adbridge/master
56:0a668029cb26 2018-01-02 mbed_official Merge pull request #140 from adbridge/master
55:33095a875ab0 2018-01-02 mbed_official Merge pull request #139 from ARMmbed/mbed-os-5.7.0-oob
54:042e70e75517 2017-12-14 mbed_official Update README.md with network troubleshooting
53:f8f870f572b0 2017-12-04 mbed_official Merge pull request #132 from adbridge/master
52:b22178490e3e 2017-11-22 mbed_official Merge pull request #131 from adbridge/master
51:bef91d415b35 2017-11-21 mbed_official Merge pull request #130 from adbridge/master
50:b6870173bcac 2017-11-09 mbed_official Change Arm trademarks in the examples' source files
49:eefae2a6ace5 2017-11-07 mbed_official Update branding from mbed to Mbed in tls-client/README.md
48:bc15de2d00db 2017-11-07 mbed_official Merge pull request #127 from adbridge/master
47:c84bc63913c6 2017-11-02 mbed_official Merge pull request #101 from JanneKiiskila/easy-connect
46:3091c2387c57 2017-10-24 mbed_official Merge pull request #124 from 0xc0170/master
45:42fad9edd56c 2017-10-09 mbed_official Merge pull request #121 from adbridge/master
44:a1dff81a34dc 2017-10-02 mbed_official Merge pull request #120 from adbridge/master
43:3c67414f345e 2017-09-29 mbed_official Merge pull request #119 from andresag01/master
42:5236ebc3d12a 2017-09-20 mbed_official Merge pull request #117 from andresag01/change-mbed-server-address
41:6fdcfbaef261 2017-08-30 mbed_official Merge pull request #113 from adbridge/master
40:173959655eff 2017-08-17 mbed_official Merge pull request #112 from andresag01/fix-hw-accel-failure
39:33ff7af3cd4f 2017-08-03 mbed_official Merge pull request #110 from 0xc0170/master