Skip to content

Commit

Permalink
Calculate lengths instead of using magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
aveenismail committed Sep 4, 2024
1 parent e23eaa7 commit c9bae58
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions common/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ bool read_ed25519_key(uint8_t *in, size_t in_len, uint8_t *out,
uint8_t decoded[128];
size_t decoded_len = sizeof(decoded);

if(in_len < (sizeof(PEM_private_header) + sizeof(PEM_private_trailer)-3)) {
if (in_len < (sizeof(PEM_private_header) + sizeof(PEM_private_trailer) -
3)) { // -3 to account for null bytes in PEM_private_header and
// PEM_private_trailer
return false;
}
if (memcmp(in, PEM_private_header, sizeof(PEM_private_header)-1) != 0 ||
memcmp(in + in_len - sizeof(PEM_private_header), PEM_private_trailer,
sizeof(PEM_private_header) - 2) != 0) {
if (memcmp(in, PEM_private_header, sizeof(PEM_private_header) - 1) != 0 ||
memcmp(in + in_len - (sizeof(PEM_private_trailer) - 1),
PEM_private_trailer, sizeof(PEM_private_trailer) - 2) != 0) {
return false;
}

Expand Down Expand Up @@ -107,7 +109,7 @@ bool read_ed25519_key(uint8_t *in, size_t in_len, uint8_t *out,

BIO_free_all(b64);

if (ret <= 0 || ret != 48) {
if (ret != 48) {
return false;
}

Expand Down

0 comments on commit c9bae58

Please sign in to comment.