Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential address misalignment issue #445

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all 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
54 changes: 38 additions & 16 deletions lib/yubihsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2750,26 +2750,33 @@ yh_rc yh_util_import_wrapped(yh_session *session, uint16_t wrapping_key_id,
};
uint8_t buf[1];
} data = {0};
union {
struct {
uint8_t key_type;
uint16_t key_id;
};
uint8_t buf[1];
} response = {0};
#pragma pack(pop)
size_t data_len = 2 + in_len;

uint8_t response[YH_MSG_BUF_SIZE] = {0};
size_t response_len = sizeof(response);
yh_cmd response_cmd = 0;

size_t data_len = 2 + in_len;

data.key_id = htons(wrapping_key_id);
memcpy(data.bytes, in, in_len);

yh_rc yrc =
yh_send_secure_msg(session, YHC_IMPORT_WRAPPED, data.buf, data_len,
&response_cmd, response, &response_len);
&response_cmd, response.buf, &response_len);
insecure_memzero(data.buf, data_len);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to send IMPORT WRAPPED command: %s", yh_strerror(yrc));
return yrc;
}

*target_type = response[0];
*target_id = ntohs(*((uint16_t *) (response + 1)));
*target_type = response.key_type;
*target_id = ntohs(response.key_id);

return YHR_SUCCESS;
}
Expand Down Expand Up @@ -2808,27 +2815,35 @@ yh_rc yh_util_import_rsa_wrapped(yh_session *session, uint16_t wrapping_key_id,
{ 0 }
}
};
union {
struct {
uint8_t key_type;
uint16_t key_id;
};
uint8_t buf[1];
} response = {0};
#pragma pack(pop)
size_t data_len = 4;
uint8_t response[YH_MSG_BUF_SIZE] = {0};
size_t response_len = sizeof(response);
yh_cmd response_cmd = 0;

size_t data_len = 4;

memcpy(data.bytes, in, in_len);
data_len += in_len;
memcpy(data.bytes + in_len, label, label_len);
data_len += label_len;

yh_rc yrc =
yh_send_secure_msg(session, YHC_IMPORT_RSA_WRAPPED, data.buf, data_len,
&response_cmd, response, &response_len);
&response_cmd, response.buf, &response_len);
insecure_memzero(data.buf, data_len);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to send IMPORT RSA WRAPPED command: %s", yh_strerror(yrc));
return yrc;
}

*target_type = response[0];
*target_id = ntohs(*((uint16_t *) (response + 1)));
*target_type = response.key_type;
*target_id = ntohs(response.key_id);

return YHR_SUCCESS;
}
Expand Down Expand Up @@ -2878,6 +2893,13 @@ yh_rc yh_util_put_rsa_wrapped_key(yh_session *session, uint16_t wrapping_key_id,
{ 0 }
}
};
union {
struct {
uint8_t key_type;
uint16_t key_id;
};
uint8_t buf[1];
} response = {0};
#pragma pack(pop)

// 2 bytes wrap key ID +
Expand All @@ -2889,7 +2911,6 @@ yh_rc yh_util_put_rsa_wrapped_key(yh_session *session, uint16_t wrapping_key_id,
// 1 byte MGF1 algorithm
// = 10 bytes
size_t data_len = 10 + YH_OBJ_LABEL_LEN + YH_CAPABILITIES_LEN;
uint8_t response[YH_MSG_BUF_SIZE] = {0};
size_t response_len = sizeof(response);
yh_cmd response_cmd = 0;

Expand All @@ -2902,17 +2923,18 @@ yh_rc yh_util_put_rsa_wrapped_key(yh_session *session, uint16_t wrapping_key_id,

yh_rc yrc =
yh_send_secure_msg(session, YHC_PUT_RSA_WRAPPED_KEY, data.buf, data_len,
&response_cmd, response, &response_len);
&response_cmd, response.buf, &response_len);
insecure_memzero(data.buf, data_len);
if (yrc != YHR_SUCCESS) {
DBG_ERR("Failed to send IMPORT WRAPPED command: %s", yh_strerror(yrc));
return yrc;
}

*target_id = ntohs(*((uint16_t *) (response + 1)));
*target_id = ntohs(response.key_id);

if (response[0] != type) {
if (response.key_type != type) {
DBG_ERR("Imported key type does not match stated key. Removing key.");
yh_util_delete_object(session, *target_id, response[0]);
yh_util_delete_object(session, *target_id, response.key_type);
*target_id = 0;
return YHR_INVALID_PARAMETERS;
}
Expand Down
Loading