Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 58 additions & 24 deletions examples/utility/Provisioning_2.0/ClaimingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,47 @@ void ClaimingHandlerClass::poll() {
}

void ClaimingHandlerClass::getIdReqHandler() {
if (_ts != 0) {
byte _uhwidBytes[32];
hex::decode(_uhwid->c_str(), _uhwidBytes, _uhwid->length());
//Send UHWID
ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID};
idMsg.m.uhwid = _uhwidBytes;
_agentManager.sendMsg(idMsg);

String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, 1);
if (token == "") {
DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__);
sendStatus(StatusMessage::ERROR);
return;
}

//Send JWT
ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT};
jwtMsg.m.jwt = token.c_str();
_agentManager.sendMsg(jwtMsg);
_ts = 0;
} else {
if (_ts == 0) {
DEBUG_ERROR("CH::%s Error: timestamp not provided" , __FUNCTION__);
sendStatus(StatusMessage::PARAMS_NOT_FOUND);
return;
}

byte _uhwidBytes[32];
hex::decode(_uhwid->c_str(), _uhwidBytes, _uhwid->length());

String token = generateToken();
if (token == "") {
DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__);
sendStatus(StatusMessage::ERROR);
return;
}

SElementJWS sejws;
String publicKey = sejws.publicKey(*_secureElement, 1, false);
if (publicKey == "") {
DEBUG_ERROR("CH::%s Error: public key not created", __FUNCTION__);
sendStatus(StatusMessage::ERROR);
return;
}

//Send public key
ProvisioningOutputMessage publicKeyMsg = {MessageOutputType::PROV_PUBLIC_KEY};
publicKeyMsg.m.provPublicKey = publicKey.c_str();
_agentManager.sendMsg(publicKeyMsg);


//Send UHWID
ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID};
idMsg.m.uhwid = _uhwidBytes;
_agentManager.sendMsg(idMsg);

//Send JWT
ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT};
jwtMsg.m.jwt = token.c_str();
_agentManager.sendMsg(jwtMsg);
_ts = 0;

}

void ClaimingHandlerClass::resetStoredCredReqHandler() {
Expand Down Expand Up @@ -186,7 +203,24 @@ void ClaimingHandlerClass::getProvSketchVersionRequestCb() {
_receivedEvent = ClaimingReqEvents::GET_PROV_SKETCH_VERSION;
}

bool ClaimingHandlerClass::sendStatus(StatusMessage msg) {
ProvisioningOutputMessage statusMsg = { MessageOutputType::STATUS, { msg } };
return _agentManager.sendMsg(statusMsg);
String ClaimingHandlerClass::generateToken()
{
String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, 1);
if(token == "") {
byte publicKey[64];
DEBUG_INFO("Generating private key");
if(!_secureElement->generatePrivateKey(1, publicKey)){
DEBUG_ERROR("CH::%s Error: private key generation failed", __FUNCTION__);
return "";
}
token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, 1);
}

return token;
}

bool ClaimingHandlerClass::sendStatus(StatusMessage msg)
{
ProvisioningOutputMessage statusMsg = {MessageOutputType::STATUS, {msg}};
return _agentManager.sendMsg(statusMsg);
}
1 change: 1 addition & 0 deletions examples/utility/Provisioning_2.0/ClaimingHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ClaimingHandlerClass {
LEDFeedbackClass &_ledFeedback;
static inline uint64_t _ts;
SecureElement *_secureElement;
String generateToken();

bool sendStatus(StatusMessage msg);
/* Commands handlers */
Expand Down
2 changes: 1 addition & 1 deletion examples/utility/Provisioning_2.0/Provisioning_2.0.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void setup() {
initProperties();
AgentsManagerClass::getInstance().begin();
LEDFeedbackClass::getInstance().begin();
DEBUG_INFO("Starting Provisioning");
DEBUG_INFO("Starting Provisioning version %s", SKETCH_VERSION);
}

void sendStatus(StatusMessage msg) {
Expand Down