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!

Committer:
mbed_official
Date:
Mon Nov 18 14:00:49 2019 +0000
Revision:
98:54b91f4b0c49
Parent:
66:ce8709d9912c
Merge pull request #264 from dgreen-arm/point-master-at-mbed-os-master

Point master branch at Mbed OS master
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-tls

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 49:eefae2a6ace5 1 # HTTPS File Download Example for TLS Client on Mbed OS
mbed_official 2:270602af41c9 2
mbed_official 42:5236ebc3d12a 3 This application downloads a file from an HTTPS server (os.mbed.com) and looks for a specific string in that file.
mbed_official 2:270602af41c9 4
mbed_official 5:8275e4cee0d5 5 ## Getting started
mbed_official 2:270602af41c9 6
mbed_official 2:270602af41c9 7 Set up your environment if you have not done so already. For instructions, refer to the [main readme](../README.md).
mbed_official 2:270602af41c9 8
mbed_official 49:eefae2a6ace5 9 You can also compile this example with the [Mbed Online Compiler](https://os.mbed.com/compiler/) by using [this project](https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-tls-tls-client).
mbed_official 22:3fbb4afb6a43 10
mbed_official 54:042e70e75517 11 ## Requirements
mbed_official 2:270602af41c9 12
mbed_official 54:042e70e75517 13 This example uses by default an Ethernet connection to the internet.
mbed_official 54:042e70e75517 14 It's possible to switch to another network interface by using [Easy Connect](https://github.com/ARMmbed/easy-connect/blob/master/README.md). The [Mbed OS Example Client](https://github.com/ARMmbed/mbed-os-example-client#application-setup) application shows an example of how to configure other network interfaces.
mbed_official 12:1ae41c231014 15
mbed_official 49:eefae2a6ace5 16 The networking stack used in this example requires TLS functionality to be enabled on Mbed TLS. On devices where hardware entropy is not present, TLS is disabled by default. This would result in compile time or linking failures.
mbed_official 12:1ae41c231014 17
mbed_official 42:5236ebc3d12a 18 To learn why entropy is required, read the [TLS Porting guide](https://docs.mbed.com/docs/mbed-os-handbook/en/latest/advanced/tls_porting/).
mbed_official 2:270602af41c9 19
mbed_official 2:270602af41c9 20 ## Monitoring the application
mbed_official 2:270602af41c9 21
mbed_official 47:c84bc63913c6 22 __NOTE:__ Make sure that the network is functional before running the application.
mbed_official 2:270602af41c9 23
mbed_official 2:270602af41c9 24 The output in the terminal window should be similar to this:
mbed_official 2:270602af41c9 25
mbed_official 2:270602af41c9 26 ```
mbed_official 47:c84bc63913c6 27 Starting mbed-os-example-tls/tls-client
mbed_official 66:ce8709d9912c 28 Using Mbed OS 5.6.3
mbed_official 66:ce8709d9912c 29 [EasyConnect] IPv4 mode
mbed_official 66:ce8709d9912c 30 Successfully connected to os.mbed.com at port 443
mbed_official 2:270602af41c9 31 Starting the TLS handshake...
mbed_official 66:ce8709d9912c 32 Successfully completed the TLS handshake
mbed_official 2:270602af41c9 33 Server certificate:
mbed_official 66:ce8709d9912c 34 cert. version : 3
mbed_official 66:ce8709d9912c 35 serial number : 65:7B:6D:8D:15:A5:B6:86:87:6B:5E:BC
mbed_official 66:ce8709d9912c 36 issuer name : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2
mbed_official 66:ce8709d9912c 37 subject name : C=GB, ST=Cambridgeshire, L=Cambridge, O=ARM Ltd, CN=*.mbed.com
mbed_official 66:ce8709d9912c 38 issued on : 2017-04-03 13:54:02
mbed_official 66:ce8709d9912c 39 expires on : 2018-05-06 10:31:02
mbed_official 66:ce8709d9912c 40 signed using : RSA with SHA-256
mbed_official 66:ce8709d9912c 41 RSA key size : 2048 bits
mbed_official 66:ce8709d9912c 42 basic constraints : CA=false
mbed_official 66:ce8709d9912c 43 subject alt name : *.mbed.com, mbed.org, *.mbed.org, mbed.com
mbed_official 66:ce8709d9912c 44 key usage : Digital Signature, Key Encipherment
mbed_official 66:ce8709d9912c 45 ext key usage : TLS Web Server Authentication, TLS Web Client Authentication
mbed_official 66:ce8709d9912c 46
mbed_official 2:270602af41c9 47 Certificate verification passed
mbed_official 66:ce8709d9912c 48 Established TLS connection to os.mbed.com
mbed_official 66:ce8709d9912c 49 HTTP: Received 365 chars from server
mbed_official 66:ce8709d9912c 50 HTTP: Received '200 OK' status ... OK
mbed_official 66:ce8709d9912c 51 HTTP: Received message:
mbed_official 2:270602af41c9 52 HTTP/1.1 200 OK
mbed_official 66:ce8709d9912c 53 Server: nginx/1.11.10
mbed_official 66:ce8709d9912c 54 Date: Wed, 08 Nov 2017 09:07:59 GMT
mbed_official 2:270602af41c9 55 Content-Type: text/plain
mbed_official 2:270602af41c9 56 Content-Length: 14
mbed_official 2:270602af41c9 57 Connection: keep-alive
mbed_official 2:270602af41c9 58 Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
mbed_official 2:270602af41c9 59 Accept-Ranges: bytes
mbed_official 2:270602af41c9 60 Cache-Control: max-age=36000
mbed_official 66:ce8709d9912c 61 Expires: Wed, 08 Nov 2017 19:07:59 GMT
mbed_official 2:270602af41c9 62 Strict-Transport-Security: max-age=31536000; includeSubdomains
mbed_official 2:270602af41c9 63
mbed_official 2:270602af41c9 64 Hello world!
mbed_official 66:ce8709d9912c 65
mbed_official 66:ce8709d9912c 66
mbed_official 66:ce8709d9912c 67 DONE
mbed_official 2:270602af41c9 68 ```
mbed_official 2:270602af41c9 69
mbed_official 2:270602af41c9 70 ## Debugging the TLS connection
mbed_official 2:270602af41c9 71
mbed_official 2:270602af41c9 72 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:
mbed_official 2:270602af41c9 73
mbed_official 2:270602af41c9 74 * Level 1 only prints non-zero return codes from SSL functions and information about the full certificate chain being verified.
mbed_official 2:270602af41c9 75
mbed_official 2:270602af41c9 76 * Level 2 prints more information about internal state updates.
mbed_official 2:270602af41c9 77
mbed_official 2:270602af41c9 78 * Level 3 is intermediate.
mbed_official 2:270602af41c9 79
mbed_official 2:270602af41c9 80 * Level 4 (the maximum) includes full binary dumps of the packets.
mbed_official 2:270602af41c9 81
mbed_official 2:270602af41c9 82
mbed_official 2:270602af41c9 83 The TLS connection can fail with an error similar to:
mbed_official 2:270602af41c9 84
mbed_official 2:270602af41c9 85 mbedtls_ssl_write() failed: -0x2700 (-9984): X509 - Certificate verification failed, e.g. CRL, CA or signature check failed
mbed_official 42:5236ebc3d12a 86 Failed to fetch /media/uploads/mbed_official/hello.txt from os.mbed.com:443
mbed_official 2:270602af41c9 87
mbed_official 42:5236ebc3d12a 88 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 `os.mbed.com` switches to a new CA when updating its certificate).
mbed_official 2:270602af41c9 89
mbed_official 2:270602af41c9 90 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.
mbed_official 2:270602af41c9 91
mbed_official 2:270602af41c9 92 **Warning:** this removes all security against a possible active attacker, so use at your own risk or for debugging only!
mbed_official 2:270602af41c9 93
mbed_official 54:042e70e75517 94 ## Troubleshooting
mbed_official 54:042e70e75517 95
mbed_official 54:042e70e75517 96 If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.