Skip to content

Commit e852e3f

Browse files
committed
Add checking in SHE response handlers
1 parent df426ad commit e852e3f

1 file changed

Lines changed: 169 additions & 55 deletions

File tree

src/wh_client_she.c

Lines changed: 169 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ int wh_Client_SheSetUidResponse(whClientContext* c)
9696

9797
resp = (whMessageShe_SetUidResponse*)wh_CommClient_GetDataPtr(c->comm);
9898
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
99+
if (ret == WH_ERROR_OK) {
100+
if (group != WH_MESSAGE_GROUP_SHE ||
101+
action != WH_SHE_SET_UID ||
102+
dataSz < sizeof(*resp)) {
103+
ret = WH_ERROR_ABORTED;
104+
}
105+
}
99106
if (ret == WH_ERROR_OK) {
100107
ret = resp->rc;
101108
}
@@ -150,6 +157,13 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader,
150157
initResp = (whMessageShe_SecureBootInitResponse*)respBuf;
151158
} while (ret == WH_ERROR_NOTREADY);
152159
}
160+
if (ret == 0) {
161+
if (group != WH_MESSAGE_GROUP_SHE ||
162+
action != WH_SHE_SECURE_BOOT_INIT ||
163+
dataSz < sizeof(*initResp)) {
164+
ret = WH_ERROR_ABORTED;
165+
}
166+
}
153167

154168
/* send update sub command until we've sent the entire bootloader */
155169
while (ret == 0 && bootloaderSent < bootloaderLen) {
@@ -186,6 +200,15 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader,
186200
respBuf);
187201
} while (ret == WH_ERROR_NOTREADY);
188202
}
203+
if (ret == 0) {
204+
whMessageShe_SecureBootUpdateResponse* updateResp =
205+
(whMessageShe_SecureBootUpdateResponse*)respBuf;
206+
if (group != WH_MESSAGE_GROUP_SHE ||
207+
action != WH_SHE_SECURE_BOOT_UPDATE ||
208+
dataSz < sizeof(*updateResp)) {
209+
ret = WH_ERROR_ABORTED;
210+
}
211+
}
189212

190213
/* increment sent */
191214
if (ret == 0) {
@@ -205,6 +228,13 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader,
205228
finishResp = (whMessageShe_SecureBootFinishResponse*)respBuf;
206229
} while (ret == WH_ERROR_NOTREADY);
207230
}
231+
if (ret == 0) {
232+
if (group != WH_MESSAGE_GROUP_SHE ||
233+
action != WH_SHE_SECURE_BOOT_FINISH ||
234+
dataSz < sizeof(*finishResp)) {
235+
ret = WH_ERROR_ABORTED;
236+
}
237+
}
208238

209239
if (ret == 0) {
210240
ret = finishResp->rc;
@@ -242,13 +272,18 @@ int wh_Client_SheGetStatusResponse(whClientContext* c, uint8_t* sreg)
242272
resp = (whMessageShe_GetStatusResponse*)wh_CommClient_GetDataPtr(c->comm);
243273

244274
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
245-
246-
/* return error or set sreg */
247275
if (ret == 0) {
248-
if (resp->rc != WH_SHE_ERC_NO_ERROR)
249-
ret = resp->rc;
250-
else
251-
*sreg = resp->sreg;
276+
if (group != WH_MESSAGE_GROUP_SHE ||
277+
action != WH_SHE_GET_STATUS ||
278+
dataSz < sizeof(*resp)) {
279+
ret = WH_ERROR_ABORTED;
280+
}
281+
}
282+
if (ret == 0) {
283+
ret = resp->rc;
284+
}
285+
if (ret == 0) {
286+
*sreg = resp->sreg;
252287
}
253288
return ret;
254289
}
@@ -307,15 +342,20 @@ int wh_Client_SheLoadKeyResponse(whClientContext* c, uint8_t* messageFour,
307342

308343
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
309344
if (ret == 0) {
310-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
311-
ret = resp->rc;
312-
}
313-
else {
314-
/* copy out message 4 and 5 */
315-
memcpy(messageFour, resp->messageFour, sizeof(resp->messageFour));
316-
memcpy(messageFive, resp->messageFive, sizeof(resp->messageFive));
345+
if (group != WH_MESSAGE_GROUP_SHE ||
346+
action != WH_SHE_LOAD_KEY ||
347+
dataSz < sizeof(*resp)) {
348+
ret = WH_ERROR_ABORTED;
317349
}
318350
}
351+
if (ret == 0) {
352+
ret = resp->rc;
353+
}
354+
if (ret == 0) {
355+
/* copy out message 4 and 5 */
356+
memcpy(messageFour, resp->messageFour, sizeof(resp->messageFour));
357+
memcpy(messageFive, resp->messageFive, sizeof(resp->messageFive));
358+
}
319359
return ret;
320360
}
321361

@@ -366,6 +406,13 @@ int wh_Client_SheLoadPlainKeyResponse(whClientContext* c)
366406
(whMessageShe_LoadPlainKeyResponse*)wh_CommClient_GetDataPtr(c->comm);
367407

368408
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
409+
if (ret == 0) {
410+
if (group != WH_MESSAGE_GROUP_SHE ||
411+
action != WH_SHE_LOAD_PLAIN_KEY ||
412+
dataSz < sizeof(*resp)) {
413+
ret = WH_ERROR_ABORTED;
414+
}
415+
}
369416
if (ret == 0) {
370417
ret = resp->rc;
371418
}
@@ -417,18 +464,23 @@ int wh_Client_SheExportRamKeyResponse(whClientContext* c, uint8_t* messageOne,
417464

418465
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
419466
if (ret == 0) {
420-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
421-
ret = resp->rc;
422-
}
423-
else {
424-
memcpy(messageOne, resp->messageOne, sizeof(resp->messageOne));
425-
memcpy(messageTwo, resp->messageTwo, sizeof(resp->messageTwo));
426-
memcpy(messageThree, resp->messageThree,
427-
sizeof(resp->messageThree));
428-
memcpy(messageFour, resp->messageFour, sizeof(resp->messageFour));
429-
memcpy(messageFive, resp->messageFive, sizeof(resp->messageFive));
467+
if (group != WH_MESSAGE_GROUP_SHE ||
468+
action != WH_SHE_EXPORT_RAM_KEY ||
469+
dataSz < sizeof(*resp)) {
470+
ret = WH_ERROR_ABORTED;
430471
}
431472
}
473+
if (ret == 0) {
474+
ret = resp->rc;
475+
}
476+
if (ret == 0) {
477+
memcpy(messageOne, resp->messageOne, sizeof(resp->messageOne));
478+
memcpy(messageTwo, resp->messageTwo, sizeof(resp->messageTwo));
479+
memcpy(messageThree, resp->messageThree,
480+
sizeof(resp->messageThree));
481+
memcpy(messageFour, resp->messageFour, sizeof(resp->messageFour));
482+
memcpy(messageFive, resp->messageFive, sizeof(resp->messageFive));
483+
}
432484

433485
return ret;
434486
}
@@ -473,6 +525,13 @@ int wh_Client_SheInitRndResponse(whClientContext* c)
473525

474526
resp = (whMessageShe_InitRngResponse*)wh_CommClient_GetDataPtr(c->comm);
475527
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
528+
if (ret == 0) {
529+
if (group != WH_MESSAGE_GROUP_SHE ||
530+
action != WH_SHE_INIT_RND ||
531+
dataSz < sizeof(*resp)) {
532+
ret = WH_ERROR_ABORTED;
533+
}
534+
}
476535
if (ret == 0) {
477536
ret = resp->rc;
478537
}
@@ -516,15 +575,21 @@ int wh_Client_SheRndResponse(whClientContext* c, uint8_t* out, uint32_t* outSz)
516575
resp = (whMessageShe_RndResponse*)wh_CommClient_GetDataPtr(c->comm);
517576

518577
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
519-
520578
if (ret == 0) {
521-
if (resp->rc != WH_SHE_ERC_NO_ERROR)
522-
ret = resp->rc;
523-
else {
524-
memcpy(out, resp->rnd, sizeof(resp->rnd));
525-
*outSz = sizeof(resp->rnd);
579+
if (group != WH_MESSAGE_GROUP_SHE ||
580+
action != WH_SHE_RND ||
581+
dataSz < sizeof(*resp)) {
582+
ret = WH_ERROR_ABORTED;
526583
}
527584
}
585+
if (ret == 0) {
586+
ret = resp->rc;
587+
}
588+
if (ret == 0) {
589+
memcpy(out, resp->rnd, sizeof(resp->rnd));
590+
*outSz = sizeof(resp->rnd);
591+
}
592+
528593
return ret;
529594
}
530595

@@ -577,6 +642,13 @@ int wh_Client_SheExtendSeedResponse(whClientContext* c)
577642
resp = (whMessageShe_ExtendSeedResponse*)wh_CommClient_GetDataPtr(c->comm);
578643
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
579644

645+
if (ret == 0) {
646+
if (group != WH_MESSAGE_GROUP_SHE ||
647+
action != WH_SHE_EXTEND_SEED ||
648+
dataSz < sizeof(*resp)) {
649+
ret = WH_ERROR_ABORTED;
650+
}
651+
}
580652
if (ret == 0) {
581653
ret = resp->rc;
582654
}
@@ -640,11 +712,19 @@ int wh_Client_SheEncEcbResponse(whClientContext* c, uint8_t* out, uint32_t sz)
640712

641713
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
642714
if (ret == 0) {
643-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
644-
ret = resp->rc;
715+
if (group != WH_MESSAGE_GROUP_SHE ||
716+
action != WH_SHE_ENC_ECB ||
717+
dataSz < sizeof(*resp) ||
718+
dataSz < sizeof(*resp) + resp->sz) {
719+
ret = WH_ERROR_ABORTED;
645720
}
646-
else if (sz < resp->sz) {
647-
ret = WH_ERROR_BADARGS;
721+
}
722+
if (ret == 0) {
723+
ret = resp->rc;
724+
}
725+
if (ret == 0) {
726+
if (sz < resp->sz) {
727+
ret = WH_ERROR_BUFFER_SIZE;
648728
}
649729
else {
650730
memcpy(out, packOut, resp->sz);
@@ -712,11 +792,19 @@ int wh_Client_SheEncCbcResponse(whClientContext* c, uint8_t* out, uint32_t sz)
712792

713793
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
714794
if (ret == 0) {
715-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
716-
ret = resp->rc;
795+
if (group != WH_MESSAGE_GROUP_SHE ||
796+
action != WH_SHE_ENC_CBC ||
797+
dataSz < sizeof(*resp) ||
798+
dataSz < sizeof(*resp) + resp->sz) {
799+
ret = WH_ERROR_ABORTED;
717800
}
718-
else if (sz < resp->sz) {
719-
ret = WH_ERROR_BADARGS;
801+
}
802+
if (ret == 0) {
803+
ret = resp->rc;
804+
}
805+
if (ret == 0) {
806+
if (sz < resp->sz) {
807+
ret = WH_ERROR_BUFFER_SIZE;
720808
}
721809
else {
722810
memcpy(out, packOut, resp->sz);
@@ -780,11 +868,19 @@ int wh_Client_SheDecEcbResponse(whClientContext* c, uint8_t* out, uint32_t sz)
780868

781869
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
782870
if (ret == 0) {
783-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
784-
ret = resp->rc;
871+
if (group != WH_MESSAGE_GROUP_SHE ||
872+
action != WH_SHE_DEC_ECB ||
873+
dataSz < sizeof(*resp) ||
874+
dataSz < sizeof(*resp) + resp->sz) {
875+
ret = WH_ERROR_ABORTED;
785876
}
786-
else if (sz < resp->sz) {
787-
ret = WH_ERROR_BADARGS;
877+
}
878+
if (ret == 0) {
879+
ret = resp->rc;
880+
}
881+
if (ret == 0) {
882+
if (sz < resp->sz) {
883+
ret = WH_ERROR_BUFFER_SIZE;
788884
}
789885
else {
790886
memcpy(out, packOut, resp->sz);
@@ -852,11 +948,19 @@ int wh_Client_SheDecCbcResponse(whClientContext* c, uint8_t* out, uint32_t sz)
852948

853949
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
854950
if (ret == 0) {
855-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
856-
ret = resp->rc;
951+
if (group != WH_MESSAGE_GROUP_SHE ||
952+
action != WH_SHE_DEC_CBC ||
953+
dataSz < sizeof(*resp) ||
954+
dataSz < sizeof(*resp) + resp->sz) {
955+
ret = WH_ERROR_ABORTED;
857956
}
858-
else if (sz < resp->sz) {
859-
ret = WH_ERROR_BADARGS;
957+
}
958+
if (ret == 0) {
959+
ret = resp->rc;
960+
}
961+
if (ret == 0) {
962+
if (sz < resp->sz) {
963+
ret = WH_ERROR_BUFFER_SIZE;
860964
}
861965
else {
862966
memcpy(out, packOut, resp->sz);
@@ -919,13 +1023,18 @@ int wh_Client_SheGenerateMacResponse(whClientContext* c, uint8_t* out,
9191023

9201024
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
9211025
if (ret == 0) {
922-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
923-
ret = resp->rc;
924-
}
925-
else {
926-
memcpy(out, resp->mac, WH_SHE_KEY_SZ);
1026+
if (group != WH_MESSAGE_GROUP_SHE ||
1027+
action != WH_SHE_GEN_MAC ||
1028+
dataSz < sizeof(*resp)) {
1029+
ret = WH_ERROR_ABORTED;
9271030
}
9281031
}
1032+
if (ret == 0) {
1033+
ret = resp->rc;
1034+
}
1035+
if (ret == 0) {
1036+
memcpy(out, resp->mac, WH_SHE_KEY_SZ);
1037+
}
9291038
return ret;
9301039
}
9311040

@@ -991,13 +1100,18 @@ int wh_Client_SheVerifyMacResponse(whClientContext* c, uint8_t* outStatus)
9911100
resp = (whMessageShe_VerifyMacResponse*)wh_CommClient_GetDataPtr(c->comm);
9921101
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp);
9931102
if (ret == 0) {
994-
if (resp->rc != WH_SHE_ERC_NO_ERROR) {
995-
ret = resp->rc;
996-
}
997-
else {
998-
*outStatus = resp->status;
1103+
if (group != WH_MESSAGE_GROUP_SHE ||
1104+
action != WH_SHE_VERIFY_MAC ||
1105+
dataSz < sizeof(*resp)) {
1106+
ret = WH_ERROR_ABORTED;
9991107
}
10001108
}
1109+
if (ret == 0) {
1110+
ret = resp->rc;
1111+
}
1112+
if (ret == 0) {
1113+
*outStatus = resp->status;
1114+
}
10011115
return ret;
10021116
}
10031117

0 commit comments

Comments
 (0)