![]() |
Morse Micro IoT SDK
2.9.7
|
SSL Client example to demonstrate connecting to a HTTPS server, TLS handshake and retrieve data.
sslclient.c is an example application that demonstrates how to use TLS to connect to a HTTPS server. In this example we attempt to connect to https://www.google.com/ on the standard HTTPS port 443 and download the content (In this case the Google Search page).
In the function app_init we first initialize the Wi-Fi subsystem and wait for a connection to be established.
Once the connection is established we initialize the various TLS modules as follows:
Once initialized we generate entropy with a call to:
We then load and parse the CA Root certificate, Client certificate and client keys. These certificates and keys are stored in the file default_certs.h. Test certificates have been used in this example, but you may generate and use your own certificates and keys.
We now initialize the SSL context:
mbedtls_ssl_set_bio() passes the network read/write functions to mbedtls to use for reading and writing from the network. We use the blocking version of recv().
Now that the SSL setup is done, we open a TCP/IP connection to the server:
The above call does a DNS lookup and then attempts to connect to the resolved IP's by one till it succeeds. For this to work we need a DNS server, which is why we need DHCP to be enabled so we can setup the DNS server using DHCP.
Once a connection is setup we perform the TLS handshake to start secure communications:
It is possible to communicate in the clear before initiating a TLS handshake. This is commonly done in email clients using the STARTTLS protocol.
Once a handshake is completed, we can verify the server certificates using:
If the above call fails, it means the authenticity of the server could not be verified. However, TLS communications are still possible.
We can now write and read data securely using:
Once done, we can tear down the connection and free memory using:
This example application contains embedded certificates and keys for ease of testing. However, you should generate and load your own certificates and keys for real world applications using the steps below.
This application needs a root CA certificate that you can download from a certificate authority such as Verisign or Thawte. Ensure you download the root certificate in PEM file format from the certifying authority and also ensure the encryption protocols required by the certificate are enabled in the mbedtls_config.h file for this application. Out of the box we support RSA x509 root certificates in PEM format.
Use the steps below to generate a set of client keys and a self signed client certificate.
sslclient.rootca in the config.hjson file in this example folder.sslclient.key file above under the key sslclient.clientkeys in the config.hjson file in this example folder.sslclient.crt file above under the key sslclient.clientcert in the config.hjson file in this example folder.config.hjson file to the device.See Application helper routines for Wireless LAN interface for details of WLAN and IP stack configuration. Additional configuration options for this application can be found in the config.hjson file.
Definition in file sslclient.c.
#include <string.h>#include "mmosal.h"#include "mmwlan.h"#include "mmconfig.h"#include "mmipal.h"#include "mbedtls/build_info.h"#include "mbedtls/platform.h"#include "mbedtls/net.h"#include "mbedtls/ssl.h"#include "mbedtls/entropy.h"#include "mbedtls/ctr_drbg.h"#include "mbedtls/debug.h"#include "mm_app_common.h"#include "default_certs.h"
Go to the source code of this file.
Macros | |
| #define | DEFAULT_PORT "443" |
| HTTPS port number to connect to. More... | |
| #define | DEFAULT_SERVER "www.google.com" |
| HTTPS server to connect to. More... | |
| #define | GET_REQUEST "GET / HTTP/1.0\r\n\r\n" |
| HTTPS get request string. More... | |
Functions | |
| void | my_debug (void *ctx, int level, const char *file, int line, const char *str) |
| Optional mbedtls debug callback handler. More... | |
| void | app_init (void) |
| Main entry point to the application. More... | |
Variables | |
| char | buf [1408] |
| Statically allocated buffer for HTTP GET request, just under 1 packet size. More... | |
| #define DEFAULT_PORT "443" |
HTTPS port number to connect to.
Definition at line 170 of file sslclient.c.
| #define DEFAULT_SERVER "www.google.com" |
HTTPS server to connect to.
Definition at line 172 of file sslclient.c.
| #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n" |
HTTPS get request string.
Definition at line 174 of file sslclient.c.
| void app_init | ( | void | ) |
Main entry point to the application.
This will be invoked in a thread once operating system and hardware initialization has completed. It may return, but it does not have to.
Definition at line 201 of file sslclient.c.
| void my_debug | ( | void * | ctx, |
| int | level, | ||
| const char * | file, | ||
| int | line, | ||
| const char * | str | ||
| ) |
Optional mbedtls debug callback handler.
| ctx | |
| level | |
| file | |
| line | |
| str |
Definition at line 188 of file sslclient.c.
| char buf[1408] |
Statically allocated buffer for HTTP GET request, just under 1 packet size.
Definition at line 177 of file sslclient.c.