Skip to content

Commit 81dba61

Browse files
committed
generate private key if fails in generating the token
1 parent b81778f commit 81dba61

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

examples/utility/Provisioning_2.0/ClaimingHandler.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void ClaimingHandlerClass::getIdReqHandler() {
100100
idMsg.m.uhwid = _uhwidBytes;
101101
_agentManager.sendMsg(idMsg);
102102

103-
String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, 1);
103+
String token = generateToken();
104104
if (token == "") {
105105
DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__);
106106
sendStatus(StatusMessage::ERROR);
@@ -112,6 +112,19 @@ void ClaimingHandlerClass::getIdReqHandler() {
112112
jwtMsg.m.jwt = token.c_str();
113113
_agentManager.sendMsg(jwtMsg);
114114
_ts = 0;
115+
116+
SElementJWS sejws;
117+
String publicKey = sejws.publicKey(*_secureElement, 1, false);
118+
if (publicKey == "") {
119+
DEBUG_ERROR("CH::%s Error: public key not created", __FUNCTION__);
120+
sendStatus(StatusMessage::ERROR);
121+
return;
122+
}
123+
124+
//Send public key
125+
ProvisioningOutputMessage publicKeyMsg = {MessageOutputType::PROV_PUBLIC_KEY};
126+
publicKeyMsg.m.provPublicKey = publicKey.c_str();
127+
_agentManager.sendMsg(publicKeyMsg);
115128
} else {
116129
DEBUG_ERROR("CH::%s Error: timestamp not provided" , __FUNCTION__);
117130
sendStatus(StatusMessage::PARAMS_NOT_FOUND);
@@ -186,7 +199,24 @@ void ClaimingHandlerClass::getProvSketchVersionRequestCb() {
186199
_receivedEvent = ClaimingReqEvents::GET_PROV_SKETCH_VERSION;
187200
}
188201

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

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 */

0 commit comments

Comments
 (0)