Skip to content

Commit

Permalink
Move length check to more internal function
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel40791765 committed Jan 31, 2025
1 parent ba4163c commit 398a123
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
14 changes: 6 additions & 8 deletions crypto/evp_extra/p_pqdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,9 @@ EVP_PKEY *EVP_PKEY_pqdsa_new_raw_public_key(int nid, const uint8_t *in, size_t l
goto err;
}

const PQDSA *pqdsa = PQDSA_KEY_get0_dsa(ret->pkey.pqdsa_key);
if (pqdsa->public_key_len != len) {
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
goto err;
}

if (!PQDSA_KEY_set_raw_public_key(ret->pkey.pqdsa_key, in)) {
CBS cbs;
CBS_init(&cbs, in, len);
if (!PQDSA_KEY_set_raw_public_key(ret->pkey.pqdsa_key, &cbs)) {
// PQDSA_KEY_set_raw_public_key sets the appropriate error.
goto err;
}
Expand Down Expand Up @@ -316,7 +312,9 @@ EVP_PKEY *EVP_PKEY_pqdsa_new_raw_private_key(int nid, const uint8_t *in, size_t
goto err;
}

if (!PQDSA_KEY_set_raw_private_key(ret->pkey.pqdsa_key, in)) {
CBS cbs;
CBS_init(&cbs, in, len);
if (!PQDSA_KEY_set_raw_private_key(ret->pkey.pqdsa_key, &cbs)) {
// PQDSA_KEY_set_raw_private_key sets the appropriate error.
goto err;
}
Expand Down
7 changes: 3 additions & 4 deletions crypto/evp_extra/p_pqdsa_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ static int pqdsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
}
// Set the pqdsa params on |out| and check if the parsed length corresponds
// with the nid's specified length.
if (!EVP_PKEY_pqdsa_set_params(out, OBJ_cbs2nid(params)) ||
CBS_len(key) != out->pkey.pqdsa_key->pqdsa->public_key_len) {
if (!EVP_PKEY_pqdsa_set_params(out, OBJ_cbs2nid(params))) {
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
return 0;
}
return PQDSA_KEY_set_raw_public_key(out->pkey.pqdsa_key, CBS_data(key));
return PQDSA_KEY_set_raw_public_key(out->pkey.pqdsa_key, key);
}

static int pqdsa_pub_encode(CBB *out, const EVP_PKEY *pkey) {
Expand Down Expand Up @@ -158,7 +157,7 @@ static int pqdsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key, CBS *pubkey)
}

// Set the private key
if (!PQDSA_KEY_set_raw_private_key(out->pkey.pqdsa_key, CBS_data(key))) {
if (!PQDSA_KEY_set_raw_private_key(out->pkey.pqdsa_key, key)) {
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/pqdsa/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ PQDSA_KEY *PQDSA_KEY_new(void);
void PQDSA_KEY_free(PQDSA_KEY *key);
int EVP_PKEY_pqdsa_set_params(EVP_PKEY *pkey, int nid);

int PQDSA_KEY_set_raw_public_key(PQDSA_KEY *key, const uint8_t *in);
int PQDSA_KEY_set_raw_private_key(PQDSA_KEY *key, const uint8_t *in);
int PQDSA_KEY_set_raw_public_key(PQDSA_KEY *key, CBS *in);
int PQDSA_KEY_set_raw_private_key(PQDSA_KEY *key, CBS *in);
#if defined(__cplusplus)
} // extern C
#endif
Expand Down
18 changes: 14 additions & 4 deletions crypto/pqdsa/pqdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,27 @@ const PQDSA *PQDSA_KEY_get0_dsa(PQDSA_KEY* key) {
return key->pqdsa;
}

int PQDSA_KEY_set_raw_public_key(PQDSA_KEY *key, const uint8_t *in) {
key->public_key = OPENSSL_memdup(in, key->pqdsa->public_key_len);
int PQDSA_KEY_set_raw_public_key(PQDSA_KEY *key, CBS *in) {
if (CBS_len(in) != key->pqdsa->public_key_len) {
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
return 0;
}

key->public_key = OPENSSL_memdup(CBS_data(in), key->pqdsa->public_key_len);
if (key->public_key == NULL) {
return 0;
}

return 1;
}

int PQDSA_KEY_set_raw_private_key(PQDSA_KEY *key, const uint8_t *in) {
key->private_key = OPENSSL_memdup(in, key->pqdsa->private_key_len);
int PQDSA_KEY_set_raw_private_key(PQDSA_KEY *key, CBS *in) {
if (CBS_len(in) != key->pqdsa->private_key_len) {
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
return 0;
}

key->private_key = OPENSSL_memdup(CBS_data(in), key->pqdsa->private_key_len);
if (key->private_key == NULL) {
return 0;
}
Expand Down

0 comments on commit 398a123

Please sign in to comment.