Skip to content

Commit

Permalink
Implement #8: Support SSL/TLS connection by adding TLSClient
Browse files Browse the repository at this point in the history
We add a new `TLSClient` class as part of the LWiFi library, it

* Wraps mbedTLS calls
* Provide an example that connects to http://howsmyssl.com

Some implementation details:

 * mbedTLS is already in the liblinkit.a
* Switch the default mbedTLS config file to config-mtk-websocket.h which have a larger TLS content buffer.
 * Increase FreeRTOS heap size to 120KB because increasing the TLS content buffer results in larger heap memory usage.
 * Also fixes #12 by correct use `const char*` in ssid parameter in `WiFi` APIs.
  • Loading branch information
pablosun committed May 19, 2017
1 parent c136ba7 commit 8db6ec2
Show file tree
Hide file tree
Showing 12 changed files with 663 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Web client
This sketch connects to a website (http://download.labs.mediatek.com)
using LinkIt 7697
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* LinkIt 7697
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified Jan 2017
by MediaTek Labs
*/

#include <LWiFi.h>

char ssid[] = "your_ap_ssid"; // your network SSID (name)
char pass[] = "your_ap_password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
char server[] = "www.howsmyssl.com"; // This website checks TLS/SSL capabilities

// This is the root certificate for our host.
// Different host server may have different root CA.
static const char rootCA[] = "-----BEGIN CERTIFICATE-----\r\n"
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\r\n"
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\r\n"
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\r\n"
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\r\n"
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\r\n"
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\r\n"
"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\r\n"
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\r\n"
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\r\n"
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\r\n"
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\r\n"
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\r\n"
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\r\n"
"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\r\n"
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\r\n"
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\r\n"
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\r\n"
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\r\n"
"-----END CERTIFICATE-----\r\n";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
TLSClient client;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 2 seconds for connection:
delay(2000);
}
Serial.println("Connected to wifi");
printWifiStatus();

Serial.println("\nStarting connection to server...");
// We must set root CA before connecting to host
// Note that the lenght includes the terminating NULL,
// so use sizeof() instead of strlen().
client.setRootCA(rootCA, sizeof(rootCA));
if (client.connect(server, 443)) {
Serial.println("connected to server (GET)");
// Make a HTTP request over SSL (HTTPS)
client.println("GET https://www.howsmyssl.com/a/check HTTP/1.1");
client.println("Host: www.howsmyssl.com");
client.println("Accept: */*");
client.println("Connection: close");
client.println();
delay(300);
}
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();

// do nothing forevermore:
while (true);
}
}


void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
#include "IPAddress.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "TLSClient.h"

class WiFiClass
{
Expand Down Expand Up @@ -56,7 +57,7 @@ class WiFiClass
*
* param ssid: Pointer to the SSID string.
*/
int begin(char* ssid);
int begin(const char* ssid);

/* Start Wifi connection with WEP encryption.
* Configure a key into the device. The key type (WEP-40, WEP-104)
Expand All @@ -66,7 +67,7 @@ class WiFiClass
* param key_idx: The key index to set. Valid values are 0-3.
* param key: Key input buffer.
*/
int begin(char* ssid, uint8_t key_idx, const char* key);
int begin(const char* ssid, uint8_t key_idx, const char* key);

/* Start Wifi connection with passphrase
* the most secure supported mode will be automatically selected
Expand All @@ -75,7 +76,7 @@ class WiFiClass
* param passphrase: Passphrase. Valid characters in a passphrase
* must be between ASCII 32-126 (decimal).
*/
int begin(char* ssid, const char *passphrase);
int begin(const char* ssid, const char *passphrase);

/* Change Ip configuration settings disabling the dhcp client
*
Expand Down

0 comments on commit 8db6ec2

Please sign in to comment.