Skip to content

Commit 975ec02

Browse files
committed
Fix RSA get params to set params instead of reading from empty params
1 parent 5eeba61 commit 975ec02

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

src/wp_rsa_kmgmt.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,11 @@ static int wp_rsa_get_params_key_data(wp_Rsa* rsa, OSSL_PARAM params[])
747747
size_t oLen;
748748
mp_int* mp = (mp_int*)(((byte*)&rsa->key) + wp_rsa_offset[i]);
749749
oLen = mp_unsigned_bin_size(mp);
750-
if ((p->data != NULL) && (!wp_mp_read_unsigned_bin_le(mp, p->data,
751-
p->data_size))) {
750+
if (oLen > p->data_size) {
751+
ok = 0;
752+
}
753+
if (ok && (p->data != NULL) &&
754+
(!wp_mp_to_unsigned_bin_le(mp, p->data, oLen))) {
752755
ok = 0;
753756
}
754757
p->return_size = oLen;

test/test_rsa.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,83 @@ int test_rsa_pkey_keygen(void *data)
707707
return err;
708708
}
709709

710+
int test_rsa_get_params(void *data)
711+
{
712+
int err;
713+
EVP_PKEY *pkey = NULL;
714+
unsigned char n[2048 / 8];
715+
unsigned char e[2048 / 8];
716+
OSSL_PARAM params[3];
717+
EVP_PKEY_CTX *ctx = NULL;
718+
BIGNUM *eCmd = NULL;
719+
BIGNUM *eRet = NULL;
720+
const int newKeySize = 2048;
721+
(void)data;
722+
723+
err = (ctx = EVP_PKEY_CTX_new_from_name(wpLibCtx, "RSA", NULL)) == NULL;
724+
if (err == 0) {
725+
err = EVP_PKEY_keygen_init(ctx) != 1;
726+
}
727+
if (err == 0) {
728+
PRINT_MSG("Change the key size w/ ctrl command");
729+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
730+
EVP_PKEY_CTRL_RSA_KEYGEN_BITS, newKeySize,
731+
NULL) <= 0;
732+
}
733+
if (err == 0) {
734+
err = (eCmd = BN_new()) == NULL;
735+
}
736+
if (err == 0) {
737+
err = BN_set_word(eCmd, 3) != 1;
738+
}
739+
if (err == 0) {
740+
PRINT_MSG("Change the public exponent w/ ctrl command");
741+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
742+
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, eCmd) <= 0;
743+
}
744+
if (err == 0) {
745+
PRINT_MSG("Generate RSA key w/ new parameters");
746+
err = EVP_PKEY_keygen(ctx, &pkey) != 1;
747+
}
748+
if (pkey == NULL) {
749+
err = 1;
750+
}
751+
if (err == 0) {
752+
memset(e, 0, sizeof(e));
753+
memset(n, 0, sizeof(n));
754+
params[0] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_N, n, sizeof(n));
755+
params[1] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_E, e, sizeof(e));
756+
params[2] = OSSL_PARAM_construct_end();
757+
758+
PRINT_MSG("Getting RSA params");
759+
760+
if (EVP_PKEY_get_params(pkey, params) != 1) {
761+
err = 1;
762+
}
763+
}
764+
/* Check return sizes, then verify e matches the one we set */
765+
if (err == 0) {
766+
if ((params[0].return_size != (size_t)(newKeySize / 8)) ||
767+
(params[1].return_size != 1)) {
768+
err = 1;
769+
}
770+
}
771+
if (err == 0) {
772+
eRet = BN_bin2bn(e, params[1].return_size, NULL);
773+
if (eRet == NULL) {
774+
err = 1;
775+
}
776+
}
777+
if (err == 0) {
778+
err = BN_cmp((const BIGNUM *)eCmd, (const BIGNUM *)eRet);
779+
}
780+
781+
BN_free(eCmd);
782+
BN_free(eRet);
783+
EVP_PKEY_free(pkey);
784+
return err;
785+
}
786+
710787
int test_rsa_pkey_invalid_key_size(void *data) {
711788
int err;
712789
EVP_PKEY *pkey = NULL;

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ TEST_CASE test_case[] = {
160160
TEST_DECL(test_rsa_enc_dec_oaep, NULL),
161161
TEST_DECL(test_rsa_pkey_keygen, NULL),
162162
TEST_DECL(test_rsa_pkey_invalid_key_size, NULL),
163+
TEST_DECL(test_rsa_get_params, NULL),
163164
TEST_DECL(test_rsa_load_key, NULL),
164165
TEST_DECL(test_rsa_load_cert, NULL),
165166
#endif /* WP_HAVE_RSA */

test/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ int test_rsa_enc_dec_pkcs1(void *data);
236236
int test_rsa_enc_dec_oaep(void *data);
237237
int test_rsa_pkey_keygen(void *data);
238238
int test_rsa_pkey_invalid_key_size(void *data);
239+
int test_rsa_get_params(void *data);
239240

240241
int test_rsa_load_key(void* data);
241242
int test_rsa_load_cert(void* data);

0 commit comments

Comments
 (0)