Skip to content

Commit 5519222

Browse files
authored
Merge pull request #547 from fabik111/migration
Changes for migration from Provisioning 1.0 to Provisioning 2.0
2 parents b81778f + dda3cba commit 5519222

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

examples/utility/Provisioning_2.0/ClaimingHandler.cpp

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "utility/HCI.h"
1414
#include <Arduino_HEX.h>
1515

16+
#define SLOT_BOARD_PRIVATE_KEY 1
17+
1618
extern const char *SKETCH_VERSION;
1719

1820
ClaimingHandlerClass::ClaimingHandlerClass():
@@ -92,30 +94,47 @@ void ClaimingHandlerClass::poll() {
9294
}
9395

9496
void ClaimingHandlerClass::getIdReqHandler() {
95-
if (_ts != 0) {
96-
byte _uhwidBytes[32];
97-
hex::decode(_uhwid->c_str(), _uhwidBytes, _uhwid->length());
98-
//Send UHWID
99-
ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID};
100-
idMsg.m.uhwid = _uhwidBytes;
101-
_agentManager.sendMsg(idMsg);
102-
103-
String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, 1);
104-
if (token == "") {
105-
DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__);
106-
sendStatus(StatusMessage::ERROR);
107-
return;
108-
}
109-
110-
//Send JWT
111-
ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT};
112-
jwtMsg.m.jwt = token.c_str();
113-
_agentManager.sendMsg(jwtMsg);
114-
_ts = 0;
115-
} else {
97+
if (_ts == 0) {
11698
DEBUG_ERROR("CH::%s Error: timestamp not provided" , __FUNCTION__);
11799
sendStatus(StatusMessage::PARAMS_NOT_FOUND);
100+
return;
118101
}
102+
103+
byte _uhwidBytes[32];
104+
hex::decode(_uhwid->c_str(), _uhwidBytes, _uhwid->length());
105+
106+
String token = generateToken();
107+
if (token == "") {
108+
DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__);
109+
sendStatus(StatusMessage::ERROR);
110+
return;
111+
}
112+
113+
SElementJWS sejws;
114+
String publicKey = sejws.publicKey(*_secureElement, SLOT_BOARD_PRIVATE_KEY, false);
115+
if (publicKey == "") {
116+
DEBUG_ERROR("CH::%s Error: public key not created", __FUNCTION__);
117+
sendStatus(StatusMessage::ERROR);
118+
return;
119+
}
120+
121+
//Send public key
122+
ProvisioningOutputMessage publicKeyMsg = {MessageOutputType::PROV_PUBLIC_KEY};
123+
publicKeyMsg.m.provPublicKey = publicKey.c_str();
124+
_agentManager.sendMsg(publicKeyMsg);
125+
126+
127+
//Send UHWID
128+
ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID};
129+
idMsg.m.uhwid = _uhwidBytes;
130+
_agentManager.sendMsg(idMsg);
131+
132+
//Send JWT
133+
ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT};
134+
jwtMsg.m.jwt = token.c_str();
135+
_agentManager.sendMsg(jwtMsg);
136+
_ts = 0;
137+
119138
}
120139

121140
void ClaimingHandlerClass::resetStoredCredReqHandler() {
@@ -186,7 +205,22 @@ void ClaimingHandlerClass::getProvSketchVersionRequestCb() {
186205
_receivedEvent = ClaimingReqEvents::GET_PROV_SKETCH_VERSION;
187206
}
188207

208+
String ClaimingHandlerClass::generateToken() {
209+
String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, SLOT_BOARD_PRIVATE_KEY);
210+
if(token == "") {
211+
byte publicKey[64];
212+
DEBUG_INFO("Generating private key");
213+
if(!_secureElement->generatePrivateKey(SLOT_BOARD_PRIVATE_KEY, publicKey)){
214+
DEBUG_ERROR("CH::%s Error: private key generation failed", __FUNCTION__);
215+
return "";
216+
}
217+
token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, SLOT_BOARD_PRIVATE_KEY);
218+
}
219+
220+
return token;
221+
}
222+
189223
bool ClaimingHandlerClass::sendStatus(StatusMessage msg) {
190-
ProvisioningOutputMessage statusMsg = { MessageOutputType::STATUS, { msg } };
191-
return _agentManager.sendMsg(statusMsg);
224+
ProvisioningOutputMessage statusMsg = {MessageOutputType::STATUS, {msg}};
225+
return _agentManager.sendMsg(statusMsg);
192226
}

examples/utility/Provisioning_2.0/ClaimingHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ClaimingHandlerClass {
3636
LEDFeedbackClass &_ledFeedback;
3737
static inline uint64_t _ts;
3838
SecureElement *_secureElement;
39+
String generateToken();
3940

4041
bool sendStatus(StatusMessage msg);
4142
/* Commands handlers */

examples/utility/Provisioning_2.0/Provisioning_2.0.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <utility/SElementArduinoCloudCertificate.h>
1616
#include "utility/LEDFeedback.h"
1717

18-
const char *SKETCH_VERSION = "0.1.0";
18+
const char *SKETCH_VERSION = "0.3.0";
1919

2020
enum class DeviceState {
2121
HARDWARE_CHECK,
@@ -59,7 +59,7 @@ void setup() {
5959
initProperties();
6060
AgentsManagerClass::getInstance().begin();
6161
LEDFeedbackClass::getInstance().begin();
62-
DEBUG_INFO("Starting Provisioning");
62+
DEBUG_INFO("Starting Provisioning version %s", SKETCH_VERSION);
6363
}
6464

6565
void sendStatus(StatusMessage msg) {

0 commit comments

Comments
 (0)