Skip to content

Commit e8b7cec

Browse files
authored
Merge pull request arduino#70 from bcmi-labs/fs_cert
Allow certificate loading from FS
2 parents 023b287 + c67dc3a commit e8b7cec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+5958
-48
lines changed

extras/tls/mbedtls_alt/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.a
3+
*.d
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
TLS Ethernet Web client
3+
4+
Remeber to update the CA certificates using CertificateUploader sketch
5+
before using this sketch.
6+
7+
*/
8+
9+
#include <Ethernet.h>
10+
#include <EthernetSSLClient.h>
11+
12+
// if you don't want to use DNS (and reduce your sketch size)
13+
// use the numeric IP instead of the name for the server:
14+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
15+
char server[] = "www.google.com"; // name address for Google (using DNS)
16+
17+
// Set the static IP address to use if the DHCP fails to assign
18+
IPAddress ip(192, 168, 0, 177);
19+
20+
// Initialize the Ethernet client library
21+
// with the IP address and port of the server
22+
// that you want to connect to (port 80 is default for HTTP):
23+
EthernetSSLClient client;
24+
25+
void setup() {
26+
// Open serial communications and wait for port to open:
27+
Serial.begin(9600);
28+
29+
while (!Serial) {
30+
; // wait for serial port to connect. Needed for native USB port only
31+
}
32+
33+
// start the Ethernet connection:
34+
Serial.println("Starting Ethernet");
35+
if (Ethernet.begin() == 0) {
36+
Serial.println("Failed to configure Ethernet using DHCP");
37+
// try to configure using IP address instead of DHCP:
38+
Ethernet.begin(ip);
39+
}
40+
// give the Ethernet shield a second to initialize:
41+
delay(1000);
42+
Serial.println("Connecting...");
43+
44+
// if you get a connection, report back via serial:
45+
if (client.connect(server, 443)) {
46+
Serial.println("connected");
47+
// Make a HTTP request:
48+
client.println("GET / HTTP/1.1");
49+
client.println("Host: www.google.com");
50+
client.println("Connection: close");
51+
client.println();
52+
} else {
53+
// if you didn't get a connection to the server:
54+
Serial.println("connection failed");
55+
}
56+
}
57+
58+
/* just wrap the received data up to 80 columns in the serial print*/
59+
void read_request() {
60+
uint32_t received_data_num = 0;
61+
while (client.available()) {
62+
/* actual data reception */
63+
char c = client.read();
64+
/* print data to serial port */
65+
Serial.print(c);
66+
/* wrap data to 80 columns*/
67+
received_data_num++;
68+
if(received_data_num % 80 == 0) {
69+
Serial.println();
70+
}
71+
}
72+
}
73+
74+
void loop() {
75+
76+
read_request();
77+
78+
// if the server's disconnected, stop the client:
79+
if (!client.connected()) {
80+
Serial.println();
81+
Serial.println("disconnecting.");
82+
client.stop();
83+
84+
// do nothing forevermore:
85+
while (true);
86+
}
87+
}

libraries/Ethernet/library.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ category=Communication
88
url=https://github.com/arduino/ArduinoCore-renesas/tree/master/libraries/Ethernet
99
architectures=renesas
1010
includes=Ethernet.h
11-
precompiled=true
1211

libraries/Ethernet/src/CEthernet.cpp renamed to libraries/Ethernet/src/Ethernet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int CEthernet::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway, IP
6565
}
6666

6767
/* -------------------------------------------------------------------------- */
68-
void setDNS(IPAddress dns_server) {
68+
void CEthernet::setDNS(IPAddress dns_server) {
6969
/* -------------------------------------------------------------------------- */
7070
CLwipIf::getInstance().addDns(dns_server);
7171
}

libraries/Ethernet/src/ethernetDriver.cpp renamed to libraries/Ethernet/src/EthernetDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "ethernetDriver.h"
1+
#include "EthernetDriver.h"
22
#include "IRQManager.h"
33

44
/* IMPORTANT NOTE: this driver is configured to use ZERO COPY

libraries/Ethernet/src/EthernetSSLClient.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ EthernetSSLClient::EthernetSSLClient()
66

77
sslclient = new sslclient_context;
88
_client = new EthernetClient;
9-
ssl_init(sslclient, _client);
9+
1010
_timeout = 1000;
1111
_CA_cert = NULL;
12+
_CA_path = "/mbedtls";
1213
_cert = NULL;
1314
_private_key = NULL;
1415
_pskIdent = NULL;
1516
_psKey = NULL;
1617

18+
ssl_init(sslclient, _client, _CA_path);
19+
1720
sslclient->handshake_timeout = 5000;
1821
}
1922

libraries/Ethernet/src/EthernetSSLClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef ethernetsslclient_h
2-
#define ethernetsslclient_h
1+
#ifndef ARDUINO_LWIP_ETHERNET_SSL_CLIENT_H
2+
#define ARDUINO_LWIP_ETHERNET_SSL_CLIENT_H
33

44
#include "EthernetClient.h"
55
#include "SSLClient.h"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "QSPIFlashBlockDevice.h"
2+
#include "FATFileSystem.h"
3+
#include "certificates.h"
4+
5+
QSPIFlashBlockDevice root(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
6+
FATFileSystem root_fs("wlan");
7+
8+
long getFileSize(FILE *fp) {
9+
fseek(fp, 0, SEEK_END);
10+
int size = ftell(fp);
11+
fseek(fp, 0, SEEK_SET);
12+
13+
return size;
14+
}
15+
16+
void printProgress(uint32_t offset, uint32_t size, uint32_t threshold, bool reset) {
17+
static int percent_done = 0;
18+
if (reset == true) {
19+
percent_done = 0;
20+
Serial.println("Flashed " + String(percent_done) + "%");
21+
} else {
22+
uint32_t percent_done_new = offset * 100 / size;
23+
if (percent_done_new >= percent_done + threshold) {
24+
percent_done = percent_done_new;
25+
Serial.println("Flashed " + String(percent_done) + "%");
26+
}
27+
}
28+
}
29+
30+
void setup() {
31+
32+
Serial.begin(115200);
33+
while (!Serial);
34+
35+
int err = root_fs.mount(&root);
36+
if (err) {
37+
// Reformat if we can't mount the filesystem
38+
// this should only happen on the first boot
39+
Serial.println("No filesystem containing the WiFi firmware was found.");
40+
Serial.println("Usually that means that the WiFi firmware has not been installed yet"
41+
" or was overwritten with another firmware.\n");
42+
Serial.println("Formatting the filsystem to install the firmware and certificates...\n");
43+
err = root_fs.reformat(&root);
44+
}
45+
46+
DIR *dir;
47+
struct dirent *ent;
48+
49+
if ((dir = opendir("/wlan")) != NULL) {
50+
/* print all the files and directories within directory */
51+
while ((ent = readdir (dir)) != NULL) {
52+
Serial.println("Searching for WiFi firmware file " + String(ent->d_name) + " ...");
53+
String fullname = "/wlan/" + String(ent->d_name);
54+
if (fullname == "/wlan/cacert.pem") {
55+
Serial.println("A WiFi firmware is already installed. "
56+
"Do you want to install the firmware anyway? Y/[n]");
57+
while (1) {
58+
if (Serial.available()) {
59+
int c = Serial.read();
60+
if (c == 'Y' || c == 'y') {
61+
root_fs.reformat(&root);
62+
break;
63+
}
64+
if (c == 'N' || c == 'n') {
65+
Serial.println("It's now safe to reboot or disconnect your board.");
66+
return;
67+
}
68+
}
69+
}
70+
}
71+
}
72+
closedir (dir);
73+
}
74+
75+
76+
77+
int chunck_size = 128;
78+
int byte_count = 0;
79+
FILE* fp = fopen("/wlan/cacert.pem", "wb");
80+
81+
Serial.println("Flashing certificates");
82+
printProgress(byte_count, cacert_pem_len, 10, true);
83+
while (byte_count < cacert_pem_len) {
84+
if(byte_count + chunck_size > cacert_pem_len)
85+
chunck_size = cacert_pem_len - byte_count;
86+
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
87+
if (ret != 1) {
88+
Serial.println("Error writing certificates");
89+
break;
90+
}
91+
byte_count += chunck_size;
92+
printProgress(byte_count, cacert_pem_len, 10, false);
93+
}
94+
fclose(fp);
95+
96+
fp = fopen("/wlan/cacert.pem", "rb");
97+
char buffer[128];
98+
int ret = fread(buffer, 1, 128, fp);
99+
Serial.write(buffer, ret);
100+
while (ret == 128) {
101+
ret = fread(buffer, 1, 128, fp);
102+
Serial.write(buffer, ret);
103+
}
104+
fclose(fp);
105+
106+
Serial.println("\nFirmware and certificates updated!");
107+
Serial.println("It's now safe to reboot or disconnect your board.");
108+
}
109+
110+
void loop() {
111+
112+
}

0 commit comments

Comments
 (0)