Skip to content

Commit 1a73980

Browse files
committed
added support to default ssl certificate automatically uploads and custom certificate management
Former-commit-id: 2f82498
1 parent aab6e08 commit 1a73980

File tree

7 files changed

+183
-50
lines changed

7 files changed

+183
-50
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
TLS WiFi Web client
3+
4+
Remeber to update the CA certificates using CertificateUploader sketch
5+
before using this sketch.
6+
7+
*/
8+
9+
#include "WiFiS3.h"
10+
#include "WiFiSSLClient.h"
11+
#include "IPAddress.h"
12+
13+
#include "arduino_secrets.h"
14+
15+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
16+
char ssid[] = SECRET_SSID; // your network SSID (name)
17+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
18+
19+
int status = WL_IDLE_STATUS;
20+
// if you don't want to use DNS (and reduce your sketch size)
21+
// use the numeric IP instead of the name for the server:
22+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
23+
char server[] = "www.google.com"; // name address for Google (using DNS)
24+
25+
// Initialize the Ethernet client library
26+
// with the IP address and port of the server
27+
// that you want to connect to (port 80 is default for HTTP):
28+
WiFiSSLClient client;
29+
30+
/* -------------------------------------------------------------------------- */
31+
void setup() {
32+
/* -------------------------------------------------------------------------- */
33+
//Initialize serial and wait for port to open:
34+
Serial.begin(115200);
35+
while (!Serial) {
36+
; // wait for serial port to connect. Needed for native USB port only
37+
}
38+
39+
// check for the WiFi module:
40+
if (WiFi.status() == WL_NO_MODULE) {
41+
Serial.println("Communication with WiFi module failed!");
42+
// don't continue
43+
while (true);
44+
}
45+
46+
String fv = WiFi.firmwareVersion();
47+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
48+
Serial.println("Please upgrade the firmware");
49+
}
50+
51+
// attempt to connect to WiFi network:
52+
while (status != WL_CONNECTED) {
53+
Serial.print("Attempting to connect to SSID: ");
54+
Serial.println(ssid);
55+
// Connect to WPA/WPA2 network.
56+
status = WiFi.begin(ssid, pass);
57+
58+
// wait 10 seconds for connection:
59+
delay(10000);
60+
}
61+
62+
printWifiStatus();
63+
64+
Serial.println("\nStarting connection to server...");
65+
// if you get a connection, report back via serial:
66+
67+
if (client.connect(server, 443)) {
68+
Serial.println("connected to server");
69+
// Make a HTTP request:
70+
client.println("GET / HTTP/1.1");
71+
client.println("Host: www.google.com");
72+
client.println("Connection: close");
73+
client.println();
74+
}
75+
}
76+
77+
/* just wrap the received data up to 80 columns in the serial print*/
78+
/* -------------------------------------------------------------------------- */
79+
void read_response() {
80+
/* -------------------------------------------------------------------------- */
81+
uint32_t received_data_num = 0;
82+
while (client.available()) {
83+
/* actual data reception */
84+
char c = client.read();
85+
/* print data to serial port */
86+
Serial.print(c);
87+
/* wrap data to 80 columns*/
88+
received_data_num++;
89+
if(received_data_num % 80 == 0) {
90+
Serial.println();
91+
}
92+
}
93+
}
94+
95+
/* -------------------------------------------------------------------------- */
96+
void loop() {
97+
/* -------------------------------------------------------------------------- */
98+
read_response();
99+
100+
// if the server's disconnected, stop the client:
101+
if (!client.connected()) {
102+
Serial.println();
103+
Serial.println("disconnecting from server.");
104+
client.stop();
105+
106+
// do nothing forevermore:
107+
while (true);
108+
}
109+
}
110+
111+
/* -------------------------------------------------------------------------- */
112+
void printWifiStatus() {
113+
/* -------------------------------------------------------------------------- */
114+
// print the SSID of the network you're attached to:
115+
Serial.print("SSID: ");
116+
Serial.println(WiFi.SSID());
117+
118+
// print your board's IP address:
119+
IPAddress ip = WiFi.localIP();
120+
Serial.print("IP Address: ");
121+
Serial.println(ip);
122+
123+
// print the received signal strength:
124+
long rssi = WiFi.RSSI();
125+
Serial.print("signal strength (RSSI):");
126+
Serial.print(rssi);
127+
Serial.println(" dBm");
128+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

libraries/WiFiS3/src/WiFi.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPA
149149
void CWifi::config(IPAddress local_ip, IPAddress dns_server) {
150150
/* -------------------------------------------------------------------------- */
151151
IPAddress _gw(local_ip[0],local_ip[1], local_ip[2], 1);
152-
Serial.println(_gw);
153152
IPAddress _sm(255,255,255,0);
154153
IPAddress dns(0,0,0,0);
155154
return _config(local_ip, _gw, _sm,dns_server,dns);

libraries/WiFiS3/src/WiFiClient.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size){
7474
}
7575

7676
/* -------------------------------------------------------------------------- */
77-
int WiFiClient::available(){
77+
int WiFiClient::available() {
7878
/* -------------------------------------------------------------------------- */
7979
int rv = 0;
8080
if(_sock >= 0) {
@@ -86,7 +86,10 @@ int WiFiClient::available(){
8686
modem.begin();
8787
if(modem.write(string(PROMPT(_AVAILABLE)),res, "%s%d\r\n" , CMD_WRITE(_AVAILABLE), _sock)) {
8888
rv = atoi(res.c_str());
89-
}
89+
if (rv < 0) {
90+
return 0;
91+
}
92+
}
9093
}
9194
}
9295
return rv;

libraries/WiFiS3/src/WiFiClient.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ class WiFiClient : public Client {
5858

5959
using Print::write;
6060

61-
protected:
61+
private:
6262
int _sock;
6363
void getSocket();
6464
FifoBuffer<uint8_t,RX_BUFFER_DIM> rx_buffer;
6565
int _read();
6666
bool read_needed(size_t s);
6767

6868

69-
7069
};
7170

7271
#endif

libraries/WiFiS3/src/WiFiSSLClient.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#include "WiFiSSLClient.h"
22

33
/* -------------------------------------------------------------------------- */
4-
WiFiSSLClient::WiFiSSLClient() : _sock(-1){
4+
WiFiSSLClient::WiFiSSLClient() : _sock(-1), rx_buffer(nullptr){
5+
/* -------------------------------------------------------------------------- */
6+
rx_buffer = shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
7+
}
8+
9+
/* -------------------------------------------------------------------------- */
10+
WiFiSSLClient::~WiFiSSLClient() {
511
/* -------------------------------------------------------------------------- */
612

713
}
@@ -22,6 +28,7 @@ void WiFiSSLClient::getSocket() {
2228
int WiFiSSLClient::connect(IPAddress ip, uint16_t port) {
2329
/* -------------------------------------------------------------------------- */
2430
getSocket();
31+
2532
string res = "";
2633
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTIP)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTIP), _sock, ip.toString(), port)) {
2734
return 1;
@@ -33,6 +40,9 @@ int WiFiSSLClient::connect(IPAddress ip, uint16_t port) {
3340
int WiFiSSLClient::connect(const char* host, uint16_t port) {
3441
/* -------------------------------------------------------------------------- */
3542
getSocket();
43+
if (!_custom_root) {
44+
setCACert();
45+
}
3646
string res = "";
3747
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTNAME), _sock, host, port)) {
3848
return 1;
@@ -45,27 +55,16 @@ void WiFiSSLClient::setCACert(const char* root_ca, size_t size) {
4555
/* -------------------------------------------------------------------------- */
4656
getSocket();
4757
string res = "";
48-
Serial.print("size ");
49-
Serial.println(size);
5058
if(size > 0) {
5159
modem.write_nowait(string(PROMPT(_SETCAROOT)),res, "%s%d,%d\r\n" , CMD_WRITE(_SETCAROOT), _sock, size);
5260
if(modem.passthrough((uint8_t *)root_ca, size)) {
53-
return;
61+
_custom_root = true;
5462
}
5563
} else {
5664
modem.write(string(PROMPT(_SETCAROOT)),res, "%s%d\r\n" , CMD_WRITE(_SETCAROOT), _sock);
5765
}
5866
}
5967

60-
/* -------------------------------------------------------------------------- */
61-
void WiFiSSLClient::setInsecure() {
62-
/* -------------------------------------------------------------------------- */
63-
getSocket();
64-
string res = "";
65-
modem.write(string(PROMPT(_SSLSETINSERCURE)),res, "%s%d\r\n" , CMD_WRITE(_SSLSETINSERCURE), _sock);
66-
67-
}
68-
6968
/* -------------------------------------------------------------------------- */
7069
size_t WiFiSSLClient::write(uint8_t b){
7170
/* -------------------------------------------------------------------------- */
@@ -92,15 +91,18 @@ size_t WiFiSSLClient::write(const uint8_t *buf, size_t size){
9291
int WiFiSSLClient::available(){
9392
/* -------------------------------------------------------------------------- */
9493
int rv = 0;
95-
if(_sock >= 0) {
96-
if(rx_buffer.available() > 0) {
97-
return rx_buffer.available();
94+
if(_sock >= 0 && rx_buffer != nullptr) {
95+
if(rx_buffer->available() > 0) {
96+
return rx_buffer->available();
9897
}
9998
else {
10099
string res = "";
101100
modem.begin();
102101
if(modem.write(string(PROMPT(_SSLAVAILABLE)),res, "%s%d\r\n" , CMD_WRITE(_SSLAVAILABLE), _sock)) {
103102
rv = atoi(res.c_str());
103+
if (rv < 0) {
104+
return 0;
105+
}
104106
}
105107
}
106108
}
@@ -113,15 +115,15 @@ int WiFiSSLClient::_read() {
113115
int rv = -1;
114116
if(_sock >= 0) {
115117
string res = "";
116-
uint32_t size = rx_buffer.freePositions() - 1;
118+
uint32_t size = rx_buffer->freePositions() - 1;
117119
modem.begin();
118120

119121
/* important - it works one shot */
120122
modem.avoid_trim_results();
121123
modem.read_using_size();
122124
if(modem.write(string(PROMPT(_SSLCLIENTRECEIVE)),res, "%s%d,%d\r\n" , CMD_WRITE(_SSLCLIENTRECEIVE), _sock, size)) {
123125
for(int i = 0, rv = 0; i < size && i < res.size(); i++) {
124-
rx_buffer.store((uint8_t)res[i]);
126+
rx_buffer->store((uint8_t)res[i]);
125127
rv++;
126128
}
127129
}
@@ -132,7 +134,7 @@ int WiFiSSLClient::_read() {
132134
/* -------------------------------------------------------------------------- */
133135
bool WiFiSSLClient::read_needed(size_t s) {
134136
/* -------------------------------------------------------------------------- */
135-
if((size_t)rx_buffer.available() < s) {
137+
if((size_t)rx_buffer->available() < s) {
136138
_read();
137139
}
138140
}
@@ -155,7 +157,7 @@ int WiFiSSLClient::read(uint8_t *buf, size_t size) {
155157
bool go_on = true;
156158
for(int i = 0; i < size && go_on; i++) {
157159
bool is_read = false;
158-
*(buf+i) = rx_buffer.read(&is_read);
160+
*(buf+i) = rx_buffer->read(&is_read);
159161
if(is_read) {
160162
rv++;
161163
}

libraries/WiFiS3/src/WiFiSSLClient.h

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,37 @@ class WiFiSSLClient : public WiFiClient {
3030

3131
public:
3232
WiFiSSLClient();
33-
33+
~WiFiSSLClient();
3434
virtual int connect(IPAddress ip, uint16_t port);
3535
virtual int connect(const char* host, uint16_t port);
3636
void setCACert(const char* root_ca = NULL, size_t size = 0);
37-
void setInsecure();
38-
virtual size_t write(uint8_t);
39-
virtual size_t write(const uint8_t *buf, size_t size);
40-
virtual int available();
41-
virtual int read();
42-
virtual int read(uint8_t *buf, size_t size);
43-
virtual int peek();
44-
virtual void flush();
45-
virtual void stop();
46-
virtual uint8_t connected();
47-
virtual operator bool() {
48-
return _sock != -1;
49-
}
37+
virtual size_t write(uint8_t);
38+
virtual size_t write(const uint8_t *buf, size_t size);
39+
virtual int available();
40+
virtual int read();
41+
virtual int read(uint8_t *buf, size_t size);
42+
virtual int peek();
43+
virtual void flush();
44+
virtual void stop();
45+
virtual uint8_t connected();
46+
virtual operator bool() {
47+
return _sock != -1;
48+
}
49+
50+
virtual IPAddress remoteIP();
51+
virtual uint16_t remotePort();
5052

51-
virtual IPAddress remoteIP();
52-
virtual uint16_t remotePort();
53+
friend class WiFiServer;
5354

54-
friend class WiFiServer;
55-
56-
using Print::write;
55+
using Print::write;
5756

58-
protected:
59-
int _sock;
60-
void getSocket();
61-
FifoBuffer<uint8_t,RX_BUFFER_DIM> rx_buffer;
62-
int _read();
63-
bool read_needed(size_t s);
57+
private:
58+
int _sock;
59+
bool _custom_root = false;
60+
void getSocket();
61+
shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>> rx_buffer;
62+
int _read();
63+
bool read_needed(size_t s);
6464

6565
private:
6666
void upload_default_Cert();

0 commit comments

Comments
 (0)