|
13 | 13 | #include "utility/HCI.h" |
14 | 14 | #include <Arduino_HEX.h> |
15 | 15 |
|
| 16 | +#define SLOT_BOARD_PRIVATE_KEY 1 |
| 17 | + |
16 | 18 | extern const char *SKETCH_VERSION; |
17 | 19 |
|
18 | 20 | ClaimingHandlerClass::ClaimingHandlerClass(): |
@@ -92,30 +94,47 @@ void ClaimingHandlerClass::poll() { |
92 | 94 | } |
93 | 95 |
|
94 | 96 | 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) { |
116 | 98 | DEBUG_ERROR("CH::%s Error: timestamp not provided" , __FUNCTION__); |
117 | 99 | sendStatus(StatusMessage::PARAMS_NOT_FOUND); |
| 100 | + return; |
118 | 101 | } |
| 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 | + |
119 | 138 | } |
120 | 139 |
|
121 | 140 | void ClaimingHandlerClass::resetStoredCredReqHandler() { |
@@ -186,7 +205,22 @@ void ClaimingHandlerClass::getProvSketchVersionRequestCb() { |
186 | 205 | _receivedEvent = ClaimingReqEvents::GET_PROV_SKETCH_VERSION; |
187 | 206 | } |
188 | 207 |
|
| 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 | + |
189 | 223 | 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); |
192 | 226 | } |
0 commit comments