Skip to content

Commit

Permalink
Check max message size against firmware version
Browse files Browse the repository at this point in the history
  • Loading branch information
aveenismail committed Aug 29, 2024
1 parent f2aa63e commit eaf9ebd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct yh_connector {
char address[32];
uint32_t port;
uint32_t pid;
uint8_t fw_version_major;
uint8_t fw_version_minor;
uint8_t fw_version_patch;

};

#ifndef __WIN32
Expand Down
18 changes: 9 additions & 9 deletions lib/tests/test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ static void test_status(void) {
yh_connector c;
} tests[] = {
{"status=OK\nversion=1.2.3\n",
{NULL, NULL, NULL, {0}, {0}, true, 1, 2, 3, "", 0, 0}},
{"", {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0}},
{"foobar", {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0}},
{"\n\n\n\n\n\n", {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0}},
{NULL, NULL, NULL, {0}, {0}, true, 1, 2, 3, "", 0, 0, 0, 0, 0}},
{"", {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0, 0, 0, 0}},
{"foobar", {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0, 0, 0, 0}},
{"\n\n\n\n\n\n", {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0, 0, 0, 0}},
{"status=NO_DEVICE\nserial=*\nversion=1.0.2\npid=412\naddress=\nport=12345",
{NULL, NULL, NULL, {0}, {0}, false, 1, 0, 2, "", 12345, 412}},
{"version=1.2", {NULL, NULL, NULL, {0}, {0}, false, 1, 2, 0, "", 0, 0}},
{NULL, NULL, NULL, {0}, {0}, false, 1, 0, 2, "", 12345, 412, 0, 0, 0}},
{"version=1.2", {NULL, NULL, NULL, {0}, {0}, false, 1, 2, 0, "", 0, 0, 0, 0, 0}},
{"version=foobar",
{NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0}},
{NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0, 0, 0, 0}},
{"version=2..\nstatus=OK",
{NULL, NULL, NULL, {0}, {0}, true, 2, 0, 0, "", 0, 0}},
{NULL, NULL, NULL, {0}, {0}, true, 2, 0, 0, "", 0, 0, 0, 0, 0}},
};

for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
yh_connector c = {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0};
yh_connector c = {NULL, NULL, NULL, {0}, {0}, false, 0, 0, 0, "", 0, 0, 0, 0, 0};
char *data = strdup(tests[i].data);

parse_status_data(data, &c);
Expand Down
12 changes: 11 additions & 1 deletion lib/yubihsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,13 @@ static yh_rc send_encrypted_msg(Scp_ctx *session, yh_cmd cmd,
}

// Outer command { cmd | cmd_len | sid | encrypted payload | mac }
if (3 + 1 + len + SCP_MAC_LEN > SCP_MSG_BUF_SIZE) {
int max_message_size = SCP_MSG_BUF_SIZE;
if(session->parent->fw_version_major < 2 ||
(session->parent->fw_version_major == 2 && session->parent->fw_version_minor < 4)) {
max_message_size = 2048;
}

if (3 + 1 + len + SCP_MAC_LEN > max_message_size) {
DBG_ERR("%s: %u", yh_strerror(YHR_BUFFER_TOO_SMALL), 3 + 1 + len + SCP_MAC_LEN);
return YHR_BUFFER_TOO_SMALL;
}
Expand Down Expand Up @@ -4815,6 +4821,10 @@ yh_rc yh_connect(yh_connector *connector, int timeout) {
DBG_ERR("Failed when connecting: %s", yh_strerror(rc));
}

yh_util_get_device_info(connector, &connector->fw_version_major,
&connector->fw_version_minor, &connector->fw_version_patch,
NULL, NULL, NULL, NULL, NULL);

return rc;
}

Expand Down

0 comments on commit eaf9ebd

Please sign in to comment.