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

Make parse_cred safe #169

Merged
merged 1 commit into from
Dec 8, 2023
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
34 changes: 24 additions & 10 deletions consts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,30 @@ mod helpers {
{
Err(EDHOCError::ParsingError)
} else {
let subject_len = (cred[2] - CBOR_MAJOR_TEXT_STRING) as usize;
let id_cred_offset: usize = CCS_PREFIX_LEN + subject_len + CNF_AND_COSE_KEY_PREFIX_LEN;
let g_a_x_offset: usize = id_cred_offset + COSE_KEY_FIRST_ITEMS_LEN;

Ok((
cred[g_a_x_offset..g_a_x_offset + P256_ELEM_LEN]
.try_into()
.expect("Wrong key length"),
cred[id_cred_offset],
))
let subject_len = CBORDecoder::info_of(cred[2]) as usize;

let id_cred_offset: usize = CCS_PREFIX_LEN
.checked_add(subject_len)
.and_then(|x| x.checked_add(CNF_AND_COSE_KEY_PREFIX_LEN))
.ok_or(EDHOCError::ParsingError)?;

let g_a_x_offset: usize = id_cred_offset
.checked_add(COSE_KEY_FIRST_ITEMS_LEN)
.ok_or(EDHOCError::ParsingError)?;

if g_a_x_offset
.checked_add(P256_ELEM_LEN)
.map_or(false, |end| end <= cred.len())
{
Ok((
cred[g_a_x_offset..g_a_x_offset + P256_ELEM_LEN]
.try_into()
.expect("Wrong key length"),
cred[id_cred_offset],
))
} else {
Err(EDHOCError::ParsingError)
}
}
}

Expand Down