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 double free on decrypt ek rsa padding failure #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions src/jwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,15 @@ static bool _cjose_jwe_decrypt_ek_rsa_padding(
return false;
}

// jwk must have the necessary private parts set
BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
_cjose_jwk_rsa_get((RSA *)jwk->keydata, &rsa_n, &rsa_e, &rsa_d);
if (NULL == rsa_e || NULL == rsa_n || NULL == rsa_d)
{
CJOSE_ERROR(err, CJOSE_ERR_INVALID_ARG);
return false;
}

// we don't know the size of the key to expect, but must be < RSA_size
_cjose_release_cek(&jwe->cek, jwe->cek_len);
size_t buflen = RSA_size((RSA *)jwk->keydata);
Expand All @@ -659,13 +668,15 @@ static bool _cjose_jwe_decrypt_ek_rsa_padding(
}

// decrypt the CEK using RSA v1.5 or OAEP padding
jwe->cek_len = RSA_private_decrypt(recipient->enc_key.raw_len, recipient->enc_key.raw, jwe->cek, (RSA *)jwk->keydata, padding);
if (-1 == jwe->cek_len)
int len = RSA_private_decrypt(recipient->enc_key.raw_len, recipient->enc_key.raw, jwe->cek, (RSA *)jwk->keydata, padding);
if (-1 == len)
{
CJOSE_ERROR(err, CJOSE_ERR_CRYPTO);
return false;
}

jwe->cek_len = len;

return true;
}

Expand Down