Skip to content

Commit 2a0c202

Browse files
committed
MCU8MASS-348 MCU8MASS-942 MCU8MASS-976 Add security profile module for checking if provisioning is done
1 parent 422ebfb commit 2a0c202

File tree

4 files changed

+117
-53
lines changed

4 files changed

+117
-53
lines changed

src/http_client.cpp

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "http_client.h"
22
#include "log.h"
3+
#include "security_profile.h"
34
#include "sequans_controller.h"
45

56
#include <Arduino.h>
@@ -20,10 +21,7 @@
2021
// This results in 36 + 127 + 5 + 1 + 1 = 170
2122
#define HTTP_CONFIGURE_SIZE 170
2223

23-
#define QUERY_SECURITY_PROFILE "AT+SQNSPCFG"
24-
25-
#define SECURITY_PROFILE_PREFIX_LENGTH 11
26-
#define HTTPS_SECURITY_PROFILE_NUMBER '3'
24+
#define HTTPS_SECURITY_PROFILE_NUMBER 3
2725

2826
#define HTTP_SEND "AT+SQNHTTPSND=0,%u,\"%s\",%lu,\"%s\",\"%s\""
2927
#define HTTP_RECEIVE "AT+SQNHTTPRCV=0,%lu"
@@ -246,42 +244,9 @@ bool HttpClientClass::configure(const char* host,
246244
const bool enable_tls) {
247245

248246
if (enable_tls) {
249-
250-
char response[256] = "";
251-
ResponseResult result = SequansController.writeCommand(
252-
QUERY_SECURITY_PROFILE,
253-
response,
254-
sizeof(response));
255-
256-
if (result != ResponseResult::OK) {
257-
Log.error("Failed to query HTTPS security profile");
258-
return false;
259-
}
260-
261-
// Split by line feed and carriage return to retrieve each entry
262-
char* ptr = strtok(response, "\r\n");
263-
bool security_profile_found = false;
264-
265-
while (ptr != NULL) {
266-
267-
// Skip the prefix of '+SQNSPCFG: '
268-
ptr += SECURITY_PROFILE_PREFIX_LENGTH;
269-
270-
// Now we check if the entry has the third security profile
271-
if (*ptr == HTTPS_SECURITY_PROFILE_NUMBER) {
272-
security_profile_found = true;
273-
break;
274-
}
275-
276-
ptr = strtok(NULL, "\r\n");
277-
}
278-
279-
if (!security_profile_found) {
280-
Log.error(
281-
"Security profile not set up for HTTPS. Run the "
282-
"'https_configure_ca' Arduino sketch example to set this up."
283-
"More information here: "
284-
"https://iot.microchip.com/docs/arduino/userguide/http");
247+
if (!SecurityProfile.profileExists(HTTPS_SECURITY_PROFILE_NUMBER)) {
248+
Log.error("Security profile not set up for HTTPS. Run the "
249+
"'provision' Arduino sketch example to set this up.");
285250

286251
return false;
287252
}

src/mqtt_client.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
#include "mqtt_client.h"
12
#include "ecc608.h"
23
#include "led_ctrl.h"
34
#include "log.h"
45
#include "lte.h"
5-
#include "mqtt_client.h"
6+
#include "security_profile.h"
67
#include "sequans_controller.h"
78

89
#include <cryptoauthlib.h>
@@ -289,6 +290,7 @@ static bool generateSigningCommand(char* data, char* command_buffer) {
289290
uint8_t message_to_sign[HCESIGN_DIGEST_LENGTH / 2];
290291
char* position = digest;
291292

293+
// Convert hex representation in string to numerical hex values
292294
for (uint8_t i = 0; i < sizeof(message_to_sign); i++) {
293295
sscanf(position, "%2hhx", &message_to_sign[i]);
294296
position += 2;
@@ -298,7 +300,7 @@ static bool generateSigningCommand(char* data, char* command_buffer) {
298300
ATCA_STATUS result = atcab_sign(0, message_to_sign, (uint8_t*)digest);
299301

300302
if (result != ATCA_SUCCESS) {
301-
Log.error("ECC signing failed");
303+
Log.errorf("ECC signing failed, status code: %x\r\n", result);
302304
return false;
303305
}
304306

@@ -323,31 +325,30 @@ static bool generateSigningCommand(char* data, char* command_buffer) {
323325
}
324326

325327
bool MqttClientClass::beginAWS() {
326-
// Get the endoint and thing name
327-
// -- Initialize the ECC
328-
uint8_t err = ECC608.begin();
329-
if (err != ATCA_SUCCESS) {
328+
329+
// Initialize the ECC
330+
uint8_t status = ECC608.begin();
331+
if (status != ECC608.ERR_OK) {
330332
Log.error("Could not initialize ECC hardware");
331333
return false;
332334
}
333335

334-
// -- Allocate the buffers
335336
uint8_t thingName[128];
336337
uint8_t thingNameLen = sizeof(thingName);
337338
uint8_t endpoint[128];
338339
uint8_t endpointLen = sizeof(endpoint);
339340

340-
// -- Get the thingname
341-
err = ECC608.getThingName(thingName, &thingNameLen);
342-
if (err != ECC608.ERR_OK) {
341+
// Get the thingname
342+
status = ECC608.getThingName(thingName, &thingNameLen);
343+
if (status != ECC608.ERR_OK) {
343344
Log.error("Could not retrieve thing name from the ECC");
344345
return false;
345346
}
346347

347-
// -- Get the endpoint
348-
err = ECC608.getEndpoint(endpoint, &endpointLen);
348+
// Get the endpoint
349+
status = ECC608.getEndpoint(endpoint, &endpointLen);
349350

350-
if (err != ECC608.ERR_OK) {
351+
if (status != ECC608.ERR_OK) {
351352
Log.error("Could not retrieve endpoint from the ECC");
352353
return false;
353354
}
@@ -394,6 +395,21 @@ bool MqttClientClass::begin(const char* client_id,
394395
username_length + password_length] = "";
395396

396397
if (use_ecc) {
398+
399+
if (!SecurityProfile.profileExists(
400+
MQTT_TLS_ECC_SECURITY_PROFILE_ID)) {
401+
Log.error("Security profile not set up for MQTT TLS with ECC. "
402+
"Run the 'provision' example Arduino sketch to set "
403+
"this up.");
404+
return false;
405+
}
406+
407+
uint8_t status = ECC608.begin();
408+
if (status != ATCA_SUCCESS) {
409+
Log.error("Could not initialize ECC hardware");
410+
return false;
411+
}
412+
397413
sprintf(command,
398414
MQTT_CONFIGURE_TLS,
399415
client_id,
@@ -402,6 +418,12 @@ bool MqttClientClass::begin(const char* client_id,
402418
MQTT_TLS_ECC_SECURITY_PROFILE_ID);
403419

404420
} else {
421+
if (!SecurityProfile.profileExists(MQTT_TLS_SECURITY_PROFILE_ID)) {
422+
Log.error("Security profile not set up for MQTT TLS. Run the "
423+
"'provision' example Arduino sketch to set this up.");
424+
return false;
425+
}
426+
405427
sprintf(command,
406428
MQTT_CONFIGURE_TLS,
407429
client_id,

src/security_profile.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "security_profile.h"
2+
3+
#include "sequans_controller.h"
4+
5+
#include "log.h"
6+
7+
#define QUERY_SECURITY_PROFILE "AT+SQNSPCFG"
8+
9+
#define SECURITY_PROFILE_PREFIX_LENGTH 11
10+
11+
SecurityProfileClass SecurityProfile = SecurityProfileClass::instance();
12+
13+
bool SecurityProfileClass::profileExists(const uint8_t id) {
14+
15+
char response[256] = "";
16+
ResponseResult result = SequansController.writeCommand(
17+
QUERY_SECURITY_PROFILE,
18+
response,
19+
sizeof(response));
20+
21+
if (result != ResponseResult::OK) {
22+
Log.error("Failed to query security profile");
23+
return false;
24+
}
25+
26+
// Split by line feed and carriage return to retrieve each entry
27+
char* ptr = strtok(response, "\r\n");
28+
29+
while (ptr != NULL) {
30+
31+
// Skip the prefix of '+SQNSPCFG: '
32+
ptr += SECURITY_PROFILE_PREFIX_LENGTH;
33+
34+
int security_profile_id;
35+
sscanf(ptr, "%d", &security_profile_id);
36+
37+
if (security_profile_id == id) {
38+
return true;
39+
}
40+
41+
ptr = strtok(NULL, "\r\n");
42+
}
43+
44+
return false;
45+
}

src/security_profile.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef SECURITY_PROFILE
2+
#define SECURITY_PROFILE
3+
4+
#include <stdint.h>
5+
6+
class SecurityProfileClass {
7+
8+
private:
9+
SecurityProfileClass(){};
10+
11+
public:
12+
/**
13+
* @brief Singleton instance.
14+
*/
15+
static SecurityProfileClass& instance(void) {
16+
static SecurityProfileClass instance;
17+
return instance;
18+
}
19+
20+
/**
21+
* @brief Probes the modem for whether a certain security profile exists.
22+
*
23+
* @param id The security profile identifier.
24+
*
25+
* @return true if it exists.
26+
*/
27+
bool profileExists(const uint8_t id);
28+
};
29+
30+
extern SecurityProfileClass SecurityProfile;
31+
32+
#endif

0 commit comments

Comments
 (0)