blob: 39452a58fd085673101216bc9995c04f4238631d [file] [log] [blame] [view]
Irit Arkin9cdd0d52015-08-28 09:27:57 +01001# HTTPS File Download Example for TLS Client
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +02002
Simon Butcherdf819242015-08-19 19:33:27 +01003This application downloads a file from an HTTPS server (developer.mbed.org) and looks for a specific string in that file.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +02004
5This example is implemented as a logic class (HelloHTTPS) wrapping a TCP socket and a TLS context. The logic class handles all events, leaving the main loop to just check if the process has finished.
6
7## Pre-requisites
8
Irit Arkin9cdd0d52015-08-28 09:27:57 +01009To build and run this example you must have:
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020010
11* A computer with the following software installed:
12 * [CMake](http://www.cmake.org/download/).
13 * [yotta](https://github.com/ARMmbed/yotta). Please note that **yotta has its own set of dependencies**, listed in the [installation instructions](http://armmbed.github.io/yotta/#installing-on-windows).
14 * [Python](https://www.python.org/downloads/).
Irit Arkin9cdd0d52015-08-28 09:27:57 +010015 * [The ARM GCC toolchain](https://launchpad.net/gcc-arm-embedded).
16 * A serial terminal emulator (Like screen, pySerial and cu).
17* An [FRDM-K64F](http://developer.mbed.org/platforms/FRDM-K64F/) development board, or another board supported by mbed OS (in which case you'll have to substitute frdm-k64f-gcc with the appropriate target in the instructions below).
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020018* A micro-USB cable.
Irit Arkin9cdd0d52015-08-28 09:27:57 +010019* An Ethernet connection to the internet.
20* An Ethernet cable.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020021* If your OS is Windows, please follow the installation instructions [for the serial port driver](https://developer.mbed.org/handbook/Windows-serial-configuration).
22
23## Getting started
24
Irit Arkin9cdd0d52015-08-28 09:27:57 +0100251. Connect the FRDM-K64F to the internet using the Ethernet cable.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020026
Irit Arkin9cdd0d52015-08-28 09:27:57 +0100272. Connect the FRDM-K64F to the computer with the micro-USB cable, being careful to use the "OpenSDA" connector on the target board.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020028
293. Navigate to the mbedtls directory supplied with your release and open a terminal.
30
314. Set the yotta target:
32
33 ```
34 yotta target frdm-k64f-gcc
35 ```
36
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200375. Build mbedtls and the examples. This will take a long time if it is the first time:
38
39 ```
Simon Butcherdf819242015-08-19 19:33:27 +010040 $ yotta build
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020041 ```
42
Brian Daniels3c405ae2015-08-18 10:52:57 -0700436. Copy `build/frdm-k64f-gcc/test/mbedtls-test-example-tls-client.bin` to your mbed board and wait until the LED next to the USB port stops blinking.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020044
Irit Arkin9cdd0d52015-08-28 09:27:57 +0100457. Start the serial terminal emulator and connect to the virtual serial port presented by FRDM-K64F.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020046
Irit Arkin9cdd0d52015-08-28 09:27:57 +010047 Use the following settings:
48
49 * 115200 baud (not 9600).
50 * 8N1.
51 * No flow control.
52
538. Press the Reset button on the board.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020054
Simon Butchera1500502015-08-19 20:29:31 +0100559. The output in the terminal window should look similar to this:
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020056
57 ```
Manuel Pégourié-Gonnard1a18aae2015-08-18 20:38:22 +020058 {{timeout;120}}
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020059 {{host_test_name;default}}
60 {{description;mbed TLS example HTTPS client}}
61 {{test_id;MBEDTLS_EX_HTTPS_CLIENT}}
62 {{start}}
63
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020064 Client IP Address is 192.168.0.2
Manuel Pégourié-Gonnard1a18aae2015-08-18 20:38:22 +020065 Starting DNS lookup for developer.mbed.org
66 DNS Response Received:
67 developer.mbed.org: 217.140.101.30
68 Connecting to 217.140.101.30:443
69 Connected to 217.140.101.30:443
70 Starting the TLS handshake...
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020071 TLS connection to developer.mbed.org established
72 Server certificate:
73 cert. version : 3
74 serial number : 11:21:4E:4B:13:27:F0:89:21:FB:70:EC:3B:B5:73:5C:FF:B9
75 issuer name : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2
76 subject name : C=GB, ST=Cambridgeshire, L=Cambridge, O=ARM Ltd, CN=*.mbed.com
77 issued on : 2015-03-05 10:31:02
78 expires on : 2016-03-05 10:31:02
79 signed using : RSA with SHA-256
80 RSA key size : 2048 bits
81 basic constraints : CA=false
82 subject alt name : *.mbed.com, *.mbed.org, mbed.org, mbed.com
83 key usage : Digital Signature, Key Encipherment
84 ext key usage : TLS Web Server Authentication, TLS Web Client Authentication
85 Certificate verification passed
86
Manuel Pégourié-Gonnard1a18aae2015-08-18 20:38:22 +020087 HTTPS: Received 473 chars from server
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020088 HTTPS: Received 200 OK status ... [OK]
89 HTTPS: Received 'Hello world!' status ... [OK]
90 HTTPS: Received message:
91
92 HTTP/1.1 200 OK
93 Server: nginx/1.7.10
Manuel Pégourié-Gonnard1a18aae2015-08-18 20:38:22 +020094 Date: Tue, 18 Aug 2015 18:34:04 GMT
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020095 Content-Type: text/plain
96 Content-Length: 14
97 Connection: keep-alive
98 Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
99 Accept-Ranges: bytes
100 Cache-Control: max-age=36000
Manuel Pégourié-Gonnard1a18aae2015-08-18 20:38:22 +0200101 Expires: Wed, 19 Aug 2015 04:34:04 GMT
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200102 X-Upstream-L3: 172.17.42.1:8080
Manuel Pégourié-Gonnard1a18aae2015-08-18 20:38:22 +0200103 X-Upstream-L2: developer-sjc-indigo-2-nginx
104 X-Upstream-L1-next-hop: 217.140.101.86:8001
105 X-Upstream-L1: developer-sjc-indigo-border-nginx
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200106
107 Hello world!
108 {{success}}
109 {{end}}
110 ```
111
112## Debugging the TLS connection
113
Manuel Pégourié-Gonnard6a6619b2015-08-31 09:29:08 +0200114If you are experiencing problems with this example, you should first rule out network issues by making sure the [simple HTTP file downloader example](https://github.com/ARMmbed/mbed-example-network-private/tree/master/test/helloworld-tcpclient) for the TCP module works as expected. If not, please follow the debug instructions for the HTTP file example before proceeding with the instructions below.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200115
Irit Arkin9cdd0d52015-08-28 09:27:57 +0100116To print out more debug information about the TLS connection, edit the file `source/main.cpp` and change the definition of `DEBUG_LEVEL` (near the top of the file) from 0 to a positive number:
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200117
118* Level 1 only prints non-zero return codes from SSL functions and information about the full certificate chain being verified.
119
120* Level 2 prints more information about internal state updates.
121
122* Level 3 is intermediate.
123
124* Level 4 (the maximum) includes full binary dumps of the packets.
125
Simon Butcherdf819242015-08-19 19:33:27 +0100126
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200127If the TLS connection is failing with an error similar to:
128
129 ```
130 mbedtls_ssl_write() failed: -0x2700 (-9984): X509 - Certificate verification failed, e.g. CRL, CA or signature check failed
131 Failed to fetch /media/uploads/mbed_official/hello.txt from developer.mbed.org:443
132 ```
133
Irit Arkin9cdd0d52015-08-28 09:27:57 +0100134it 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).
135
Manuel Pégourié-Gonnard6a6619b2015-08-31 09:29:08 +0200136Another reason for this error may be 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, therefore use at your own risk, or for debugging only!