Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 1 | # HTTPS File Download Example/ TLS Client |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 2 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 3 | This application downloads a file from an HTTPS server (developer.mbed.org) and looks for a specific string in that file. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 4 | |
| 5 | This 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 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 9 | To build and run this example the following requirements are necessary: |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 10 | |
| 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/). |
| 15 | * [ARM GCC toolchain](https://launchpad.net/gcc-arm-embedded). |
| 16 | * A serial terminal emulator (e.g. screen, pySerial, cu). |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 17 | * An [FRDM-K64F](http://developer.mbed.org/platforms/FRDM-K64F/) development board, or another board that has an ethernet port and is supported by mbed OS (in which case you'll have to substitute frdm-k64f-gcc with the appropriate target in the instructions below). |
| 18 | * An ethernet connection to the internet. |
| 19 | * An ethernet cable. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 20 | * A micro-USB cable. |
| 21 | * 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 | |
| 25 | 1. Connect the FRDM-K64F to the internet using the ethernet cable. |
| 26 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 27 | 2. 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é-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 28 | |
| 29 | 3. Navigate to the mbedtls directory supplied with your release and open a terminal. |
| 30 | |
| 31 | 4. Set the yotta target: |
| 32 | |
| 33 | ``` |
| 34 | yotta target frdm-k64f-gcc |
| 35 | ``` |
| 36 | |
| 37 | 5. Check that there are no missing dependencies: |
| 38 | |
| 39 | ``` |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 40 | $ yotta ls |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 41 | ``` |
| 42 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 43 | If there are missing dependencies, yotta will list them in the terminal. Please install these before proceeding. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 44 | |
| 45 | 5. Build mbedtls and the examples. This will take a long time if it is the first time: |
| 46 | |
| 47 | ``` |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 48 | $ yotta build |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 49 | ``` |
| 50 | |
| 51 | 7. 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. |
| 52 | |
| 53 | 8. Start the serial terminal emulator and connect to the virtual serial port presented by FRDM-K64F. For settings, use 115200 baud, 8N1, no flow control. **Warning:** for this example, the baud rate is not the default 9600, it is 115200. |
| 54 | |
| 55 | 9. Press the reset button on the board. |
| 56 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 57 | 10. The output in the terminal window should look similar to this: |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 58 | |
| 59 | ``` |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 60 | {timeout;120}} |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 61 | {{host_test_name;default}} |
| 62 | {{description;mbed TLS example HTTPS client}} |
| 63 | {{test_id;MBEDTLS_EX_HTTPS_CLIENT}} |
| 64 | {{start}} |
| 65 | |
| 66 | |
| 67 | Client IP Address is 192.168.0.2 |
| 68 | Connecting to developer.mbed.org:443 |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 69 | developer.mbed.org address: 217.140.101.20 |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 70 | TLS connection to developer.mbed.org established |
| 71 | Server certificate: |
| 72 | cert. version : 3 |
| 73 | serial number : 11:21:4E:4B:13:27:F0:89:21:FB:70:EC:3B:B5:73:5C:FF:B9 |
| 74 | issuer name : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2 |
| 75 | subject name : C=GB, ST=Cambridgeshire, L=Cambridge, O=ARM Ltd, CN=*.mbed.com |
| 76 | issued on : 2015-03-05 10:31:02 |
| 77 | expires on : 2016-03-05 10:31:02 |
| 78 | signed using : RSA with SHA-256 |
| 79 | RSA key size : 2048 bits |
| 80 | basic constraints : CA=false |
| 81 | subject alt name : *.mbed.com, *.mbed.org, mbed.org, mbed.com |
| 82 | key usage : Digital Signature, Key Encipherment |
| 83 | ext key usage : TLS Web Server Authentication, TLS Web Client Authentication |
| 84 | Certificate verification passed |
| 85 | |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 86 | HTTPS: Received 469 chars from server |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 87 | HTTPS: Received 200 OK status ... [OK] |
| 88 | HTTPS: Received 'Hello world!' status ... [OK] |
| 89 | HTTPS: Received message: |
| 90 | |
| 91 | HTTP/1.1 200 OK |
| 92 | Server: nginx/1.7.10 |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 93 | Date: Mon, 17 Aug 2015 11:46:19 GMT |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 94 | Content-Type: text/plain |
| 95 | Content-Length: 14 |
| 96 | Connection: keep-alive |
| 97 | Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT |
| 98 | Accept-Ranges: bytes |
| 99 | Cache-Control: max-age=36000 |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 100 | Expires: Mon, 17 Aug 2015 21:46:19 GMT |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 101 | X-Upstream-L3: 172.17.42.1:8080 |
Manuel Pégourié-Gonnard | 42618b8 | 2015-08-17 14:19:38 +0200 | [diff] [blame] | 102 | X-Upstream-L2: developer-sjc-cyan-1-nginx |
| 103 | X-Upstream-L1-next-hop: 217.140.101.22:8001 |
| 104 | X-Upstream-L1: developer-sjc-cyan-border-nginx |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 105 | |
| 106 | Hello world! |
| 107 | {{success}} |
| 108 | {{end}} |
| 109 | ``` |
| 110 | |
| 111 | ## Debugging the TLS connection |
| 112 | |
| 113 | If 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/maste r/test/helloworld-tcpclient) for the TCP module works as expected. If not, please follow the debug instructions for this example. |
| 114 | |
| 115 | To 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: |
| 116 | |
| 117 | * Level 1 only prints non-zero return codes from SSL functions and information about the full certificate chain being verified. |
| 118 | |
| 119 | * Level 2 prints more information about internal state updates. |
| 120 | |
| 121 | * Level 3 is intermediate. |
| 122 | |
| 123 | * Level 4 (the maximum) includes full binary dumps of the packets. |
| 124 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 125 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 126 | If the TLS connection is failing with an error similar to: |
| 127 | |
| 128 | ``` |
| 129 | mbedtls_ssl_write() failed: -0x2700 (-9984): X509 - Certificate verification failed, e.g. CRL, CA or signature check failed |
| 130 | Failed to fetch /media/uploads/mbed_official/hello.txt from developer.mbed.org:443 |
| 131 | ``` |
| 132 | |
Simon Butcher | df81924 | 2015-08-19 19:33:27 +0100 | [diff] [blame^] | 133 | it 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 reason for this error may be a proxy providing a different certificate. Proxys 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 attacker, therefore use at your own risk, or for debugging only! |