Skip to content

Commit b615cb3

Browse files
sandeepmistryRocketct
authored andcommitted
Bundle root certs and enable SSL cert validation
1 parent f66103f commit b615cb3

File tree

7 files changed

+1265
-3
lines changed

7 files changed

+1265
-3
lines changed

src/GSMClient.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ enum {
2929
CLIENT_STATE_WAIT_CREATE_SOCKET_RESPONSE,
3030
CLIENT_STATE_ENABLE_SSL,
3131
CLIENT_STATE_WAIT_ENABLE_SSL_RESPONSE,
32+
CLIENT_STATE_MANAGE_SSL_PROFILE,
33+
CLIENT_STATE_WAIT_MANAGE_SSL_PROFILE_RESPONSE,
3234
CLIENT_STATE_CONNECT,
3335
CLIENT_STATE_WAIT_CONNECT_RESPONSE,
3436
CLIENT_STATE_CLOSE_SOCKET,
@@ -97,7 +99,7 @@ int GSMClient::ready()
9799
}
98100

99101
case CLIENT_STATE_ENABLE_SSL: {
100-
MODEM.sendf("AT+USOSEC=%d,1", _socket);
102+
MODEM.sendf("AT+USOSEC=%d,1,0", _socket);
101103

102104
_state = CLIENT_STATE_WAIT_ENABLE_SSL_RESPONSE;
103105
ready = 0;
@@ -108,12 +110,35 @@ int GSMClient::ready()
108110
if (ready > 1) {
109111
_state = CLIENT_STATE_CLOSE_SOCKET;
110112
} else {
111-
_state = CLIENT_STATE_CONNECT;
113+
_state = CLIENT_STATE_MANAGE_SSL_PROFILE;
114+
}
115+
116+
ready = 0;
117+
break;
118+
}
119+
120+
case CLIENT_STATE_MANAGE_SSL_PROFILE: {
121+
if (_host != NULL) {
122+
MODEM.sendf("AT+USECPRF=0,0,1,4,\"%s\"", _host);
123+
} else {
124+
MODEM.send("AT+USECPRF=0,0,1");
112125
}
113126

127+
_state = CLIENT_STATE_WAIT_MANAGE_SSL_PROFILE_RESPONSE;
114128
ready = 0;
115129
break;
116130
}
131+
132+
case CLIENT_STATE_WAIT_MANAGE_SSL_PROFILE_RESPONSE: {
133+
if (ready > 1) {
134+
_state = CLIENT_STATE_CLOSE_SOCKET;
135+
} else {
136+
_state = CLIENT_STATE_CONNECT;
137+
}
138+
139+
ready = 0;
140+
break;
141+
}
117142

118143
case CLIENT_STATE_CONNECT: {
119144
if (_host != NULL) {

src/GSMClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class GSMClient : public Client {
4242
/** Get last command status
4343
@return returns 0 if last command is still executing, 1 success, >1 error
4444
*/
45-
int ready();
45+
virtual int ready();
4646

4747
/** Connect to server by IP address
4848
@param (IPAddress)

src/GSMSSLClient.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@
1717
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20+
#include "utility/GSMRootCerts.h"
21+
22+
#include "Modem.h"
23+
2024
#include "GSMSSLClient.h"
2125

26+
enum {
27+
SSL_CLIENT_STATE_LOAD_ROOT_CERT,
28+
SSL_CLIENT_STATE_WAIT_LOAD_ROOT_CERT_RESPONSE
29+
};
30+
31+
bool GSMSSLClient::_rootCertsLoaded = false;
32+
2233
GSMSSLClient::GSMSSLClient(bool synch) :
2334
GSMClient(synch)
2435
{
@@ -28,12 +39,72 @@ GSMSSLClient::~GSMSSLClient()
2839
{
2940
}
3041

42+
int GSMSSLClient::ready()
43+
{
44+
if (_rootCertsLoaded) {
45+
// root certs loaded already, continue to regular GSMClient
46+
return GSMClient::ready();
47+
}
48+
49+
int ready = MODEM.ready();
50+
51+
if (ready == 0) {
52+
// a command is still running
53+
return 0;
54+
}
55+
56+
switch (_state) {
57+
case SSL_CLIENT_STATE_LOAD_ROOT_CERT: {
58+
// load the next root cert
59+
MODEM.sendf("AT+USECMNG=0,0,\"%s\",%d", GSM_ROOT_CERTS[_certIndex].name, GSM_ROOT_CERTS[_certIndex].size);
60+
if (MODEM.waitForPrompt() != 1) {
61+
// failure
62+
ready = -1;
63+
} else {
64+
// send the cert contents
65+
MODEM.write(GSM_ROOT_CERTS[_certIndex].data, GSM_ROOT_CERTS[_certIndex].size);
66+
67+
_state = SSL_CLIENT_STATE_WAIT_LOAD_ROOT_CERT_RESPONSE;
68+
ready = 0;
69+
}
70+
break;
71+
}
72+
73+
case SSL_CLIENT_STATE_WAIT_LOAD_ROOT_CERT_RESPONSE: {
74+
if (ready > 1) {
75+
// error
76+
} else {
77+
_certIndex++;
78+
79+
if (_certIndex == GSM_NUM_ROOT_CERTS) {
80+
// all certs loaded
81+
_rootCertsLoaded = true;
82+
} else {
83+
// load next
84+
_state = SSL_CLIENT_STATE_LOAD_ROOT_CERT;
85+
}
86+
87+
ready = 0;
88+
}
89+
break;
90+
}
91+
}
92+
93+
return ready;
94+
}
95+
3196
int GSMSSLClient::connect(IPAddress ip, uint16_t port)
3297
{
98+
_certIndex = 0;
99+
_state = SSL_CLIENT_STATE_LOAD_ROOT_CERT;
100+
33101
return connectSSL(ip, port);
34102
}
35103

36104
int GSMSSLClient::connect(const char* host, uint16_t port)
37105
{
106+
_certIndex = 0;
107+
_state = SSL_CLIENT_STATE_LOAD_ROOT_CERT;
108+
38109
return connectSSL(host, port);
39110
}

src/GSMSSLClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ class GSMSSLClient : public GSMClient {
2828
GSMSSLClient(bool synch = true);
2929
virtual ~GSMSSLClient();
3030

31+
virtual int ready();
32+
3133
virtual int connect(IPAddress ip, uint16_t port);
3234
virtual int connect(const char* host, uint16_t port);
35+
36+
private:
37+
static bool _rootCertsLoaded;
38+
int _certIndex;
39+
int _state;
3340
};
3441

3542
#endif

src/Modem.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ size_t ModemClass::write(uint8_t c)
167167
return _uart->write(c);
168168
}
169169

170+
size_t ModemClass::write(const uint8_t* buf, size_t size)
171+
{
172+
return _uart->write(buf, size);
173+
}
174+
170175
void ModemClass::send(const char* command)
171176
{
172177
if (_lowPowerMode) {
@@ -216,6 +221,19 @@ int ModemClass::waitForResponse(unsigned long timeout, String* responseDataStora
216221
return -1;
217222
}
218223

224+
int ModemClass::waitForPrompt(unsigned long timeout)
225+
{
226+
for (unsigned long start = millis(); (millis() - start) < timeout;) {
227+
ready();
228+
229+
if (_buffer.endsWith(">")) {
230+
return 1;
231+
}
232+
}
233+
234+
return -1;
235+
}
236+
219237
int ModemClass::ready()
220238
{
221239
poll();

src/Modem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ class ModemClass {
4949
int noLowPowerMode();
5050

5151
size_t write(uint8_t c);
52+
size_t write(const uint8_t*, size_t);
5253

5354
void send(const char* command);
5455
void send(const String& command) { send(command.c_str()); }
5556
void sendf(const char *fmt, ...);
5657

5758
int waitForResponse(unsigned long timeout = 100, String* responseDataStorage = NULL);
59+
int waitForPrompt(unsigned long timeout = 500);
5860
int ready();
5961
void poll();
6062
void setResponseDataStorage(String* responseDataStorage);

0 commit comments

Comments
 (0)