Skip to content

Commit 9218354

Browse files
committed
Make improvements to RSA code.
- Add support for rsa_keygen_pubexp control string. - Add an RSA control string test case. - Check that RSA PSS salt length isn't negative in control string handler.
1 parent 4cfaadf commit 9218354

File tree

4 files changed

+153
-23
lines changed

4 files changed

+153
-23
lines changed

src/we_rsa.c

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,15 +2266,16 @@ static int we_rsa_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
22662266
/**
22672267
* Extra operations for working with RSA.
22682268
* Supported operations include:
2269-
* - "rsa_padding_mode": set the padding mode
2270-
* - "rsa_pss_saltlen": set RSA-PSS salt length to use
2271-
* - "rsa_keygen_bits": set size of RSA keys to generate in bits
2272-
* - "rsa_mgf1_md": set the RSA-PSS MGF1 hash to use
2269+
* - rsa_padding_mode: set the padding mode.
2270+
* - rsa_pss_saltlen: set RSA-PSS salt length to use.
2271+
* - rsa_keygen_bits: set size of RSA keys to generate in bits.
2272+
* - rsa_mgf1_md: set the RSA-PSS MGF1 hash to use.
2273+
* - rsa_oaep_md: set the digest to use with OAEP padding.
2274+
* - rsa_keygen_pubexp: set public exponent to use when making a key.
22732275
*
2274-
* @param ctx [in] Public key context of operation.
2275-
* @param type [in] Type of operation to perform.
2276-
* @param num [in] Integer parameter.
2277-
* @param ptr [in] Pointer parameter.
2276+
* @param ctx [in] Public key context of operation.
2277+
* @param type [in] Type of operation to perform.
2278+
* @param value [in] Control string dependent value.
22782279
* @returns 1 on success and 0 on failure.
22792280
*/
22802281
static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
@@ -2296,22 +2297,22 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
22962297
ret = 0;
22972298
}
22982299

2299-
if ((ret == 1) && (XSTRNCMP(type, "rsa_padding_mode", 17) == 0)) {
2300+
if ((ret == 1) && (XSTRCMP(type, "rsa_padding_mode") == 0)) {
23002301
/* Padding mode. */
2301-
if (XSTRNCMP(value, "none", 5) == 0) {
2302+
if (XSTRCMP(value, "none") == 0) {
23022303
rsa->padMode = RSA_NO_PADDING;
23032304
}
2304-
else if (XSTRNCMP(value, "pkcs1", 6) == 0) {
2305+
else if (XSTRCMP(value, "pkcs1") == 0) {
23052306
rsa->padMode = RSA_PKCS1_PADDING;
23062307
}
2307-
else if (XSTRNCMP(value, "oaep", 5) == 0) {
2308+
else if (XSTRCMP(value, "oaep") == 0) {
23082309
rsa->padMode = RSA_PKCS1_OAEP_PADDING;
23092310
}
2310-
else if (XSTRNCMP(value, "pss", 4) == 0) {
2311+
else if (XSTRCMP(value, "pss") == 0) {
23112312
rsa->padMode = RSA_PKCS1_PSS_PADDING;
23122313
}
23132314
#ifdef WE_HAVE_RSA_X931
2314-
else if (XSTRNCMP(value, "x931", 5) == 0) {
2315+
else if (XSTRCMP(value, "x931") == 0) {
23152316
rsa->padMode = RSA_X931_PADDING;
23162317
}
23172318
#endif
@@ -2326,19 +2327,19 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23262327
rsa->md = EVP_sha1();
23272328
}
23282329
}
2329-
else if ((ret == 1) && (XSTRNCMP(type, "rsa_pss_saltlen", 16) == 0)) {
2330+
else if ((ret == 1) && (XSTRCMP(type, "rsa_pss_saltlen") == 0)) {
23302331
/* RSA-PSS salt length. */
23312332
if (rsa->padMode != RSA_PKCS1_PSS_PADDING) {
23322333
ret = 0;
23332334
}
23342335
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
2335-
else if (XSTRNCMP(value, "digest", 7) == 0) {
2336+
else if (XSTRCMP(value, "digest") == 0) {
23362337
rsa->saltLen = RSA_PSS_SALTLEN_DIGEST;
23372338
}
2338-
else if (XSTRNCMP(value, "max", 4) == 0) {
2339+
else if (XSTRCMP(value, "max") == 0) {
23392340
rsa->saltLen = RSA_PSS_SALTLEN_MAX;
23402341
}
2341-
else if (XSTRNCMP(value, "auto", 5) == 0) {
2342+
else if (XSTRCMP(value, "auto") == 0) {
23422343
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
23432344
rsa->saltLen = RSA_PSS_SALTLEN_AUTO;
23442345
#else
@@ -2347,10 +2348,20 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23472348
}
23482349
#endif
23492350
else {
2350-
rsa->saltLen = XATOI(value);
2351+
int len;
2352+
2353+
len = XATOI(value);
2354+
WOLFENGINE_MSG(WE_LOG_PK, "Setting PSS salt length to %d", len);
2355+
if (len < 0) {
2356+
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Negative PSS salt length.");
2357+
ret = 0;
2358+
}
2359+
else {
2360+
rsa->saltLen = len;
2361+
}
23512362
}
23522363
}
2353-
else if ((ret == 1) && (XSTRNCMP(type, "rsa_keygen_bits", 16) == 0)) {
2364+
else if ((ret == 1) && (XSTRCMP(type, "rsa_keygen_bits") == 0)) {
23542365
/* Size, in bits, of RSA key to generate. */
23552366
bits = XATOI(value);
23562367
ret = we_check_rsa_key_size(bits, 0);
@@ -2362,7 +2373,7 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23622373
rsa->bits = bits;
23632374
}
23642375
}
2365-
else if ((ret == 1) && (XSTRNCMP(type, "rsa_mgf1_md", 12) == 0)) {
2376+
else if ((ret == 1) && (XSTRCMP(type, "rsa_mgf1_md") == 0)) {
23662377
if ((rsa->padMode != RSA_PKCS1_OAEP_PADDING) &&
23672378
(rsa->padMode != RSA_PKCS1_PSS_PADDING)) {
23682379
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Setting MGF1 and not PSS or OAEP");
@@ -2376,7 +2387,7 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23762387
}
23772388
}
23782389
}
2379-
else if ((ret == 1) && (XSTRNCMP(type, "rsa_oaep_md", 12) == 0)) {
2390+
else if ((ret == 1) && (XSTRCMP(type, "rsa_oaep_md") == 0)) {
23802391
if (rsa->padMode != RSA_PKCS1_OAEP_PADDING) {
23812392
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Setting MD and not OAEP");
23822393
ret = -2;
@@ -2392,6 +2403,23 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23922403
}
23932404
}
23942405
}
2406+
else if ((ret == 1) && (XSTRCMP(type, "rsa_keygen_pubexp") == 0)) {
2407+
int e;
2408+
2409+
e = XATOI(value);
2410+
WOLFENGINE_MSG(WE_LOG_PK, "Setting public exponent (e) to %d", e);
2411+
if (e < 0) {
2412+
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Negative public exponent.");
2413+
ret = 0;
2414+
}
2415+
else if (e == 0) {
2416+
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Zero public exponent.");
2417+
ret = 0;
2418+
}
2419+
else {
2420+
rsa->pubExp = e;
2421+
}
2422+
}
23952423
else {
23962424
/* Unsupported string. */
23972425
XSNPRINTF(errBuff, sizeof(errBuff), "Unsupported ctrl string: %s",

test/test_rsa.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,106 @@ static int test_rsa_direct(ENGINE *e, const unsigned char *der, size_t derLen,
571571
return err;
572572
}
573573

574+
int test_rsa_ctrl_str(ENGINE* e, void* data)
575+
{
576+
int err;
577+
EVP_PKEY_CTX* ctx = NULL;
578+
579+
(void)data;
580+
581+
err = (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, e)) == NULL;
582+
583+
/* rsa_padding_mode */
584+
if (err == 0) {
585+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "none") <= 0;
586+
}
587+
if (err == 0) {
588+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "pkcs1") <= 0;
589+
}
590+
if (err == 0) {
591+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "oaep") <= 0;
592+
}
593+
if (err == 0) {
594+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "pss") <= 0;
595+
}
596+
#ifdef WE_HAVE_RSA_X931
597+
if (err == 0) {
598+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "x931") <= 0;
599+
}
600+
#endif
601+
if (err == 0) {
602+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "garbage") > 0;
603+
}
604+
605+
/* rsa_keygen_bits */
606+
if (err == 0) {
607+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_keygen_bits", "2048") <= 0;
608+
}
609+
if (err == 0) {
610+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_keygen_bits", "-1") > 0;
611+
}
612+
613+
/* rsa_pss_saltlen */
614+
if (err == 0) {
615+
err = EVP_PKEY_sign_init(ctx) <= 0;
616+
}
617+
if (err == 0) {
618+
err = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) <= 0;
619+
}
620+
if (err == 0) {
621+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pss_saltlen", "digest") <= 0;
622+
}
623+
if (err == 0) {
624+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pss_saltlen", "max") <= 0;
625+
}
626+
if (err == 0) {
627+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pss_saltlen", "auto") <= 0;
628+
}
629+
if (err == 0) {
630+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pss_saltlen", "4") <= 0;
631+
}
632+
if (err == 0) {
633+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pss_saltlen", "0") <= 0;
634+
}
635+
if (err == 0) {
636+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_pss_saltlen", "-1") > 0;
637+
}
638+
639+
/* rsa_mgf1_md */
640+
if (err == 0) {
641+
err = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0;
642+
}
643+
if (err == 0) {
644+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_mgf1_md", "SHA256") <= 0;
645+
}
646+
if (err == 0) {
647+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_mgf1_md", "garbage") > 0;
648+
}
649+
650+
/* rsa_oaep_md */
651+
if (err == 0) {
652+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_oaep_md", "SHA256") <= 0;
653+
}
654+
if (err == 0) {
655+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_oaep_md", "garbage") > 0;
656+
}
657+
658+
/* rsa_keygen_pubexp */
659+
if (err == 0) {
660+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_keygen_pubexp", "65537") <= 0;
661+
}
662+
if (err == 0) {
663+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_keygen_pubexp", "0") > 0;
664+
}
665+
if (err == 0) {
666+
err = EVP_PKEY_CTX_ctrl_str(ctx, "rsa_keygen_pubexp", "-1") > 0;
667+
}
668+
669+
EVP_PKEY_CTX_free(ctx);
670+
671+
return err;
672+
}
673+
574674
int test_rsa_direct_key_gen(ENGINE *e, void *data)
575675
{
576676
int err = 0;
@@ -1150,7 +1250,7 @@ int test_rsa_pkey_keygen(ENGINE *e, void *data)
11501250
err = BN_set_word(eCmd, 3) != 1;
11511251
}
11521252
if (err == 0) {
1153-
PRINT_MSG("Change the public exponent w/ ctrl command");
1253+
PRINT_MSG("Change the public exponent w/ ctrl command");
11541254
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
11551255
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, eCmd) <= 0;
11561256
}

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ TEST_CASE test_case[] = {
140140
TEST_DECL(test_random, NULL),
141141
#endif
142142
#ifdef WE_HAVE_RSA
143+
TEST_DECL(test_rsa_ctrl_str, NULL),
143144
TEST_DECL(test_rsa_direct_key_gen, NULL),
144145
TEST_DECL(test_rsa_direct_priv_enc, NULL),
145146
TEST_DECL(test_rsa_direct_priv_dec, NULL),

test/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ int test_pkey_dec(EVP_PKEY *pkey, ENGINE *e, unsigned char *msg, size_t msgLen,
214214
#endif /* WE_HAVE_EVP_PKEY */
215215

216216
#ifdef WE_HAVE_RSA
217+
int test_rsa_ctrl_str(ENGINE* e, void* data);
217218
int test_rsa_direct_key_gen(ENGINE *e, void *data);
218219
int test_rsa_direct_priv_enc(ENGINE *e, void *data);
219220
int test_rsa_direct_priv_dec(ENGINE *e, void *data);

0 commit comments

Comments
 (0)