Skip to content

Commit 18eb99d

Browse files
authored
Merge pull request #196 from haydenroche5/ruby_fixes
Fix some issues discovered when running ruby OpenSSL gem tests.
2 parents 5abd355 + 55399ee commit 18eb99d

File tree

6 files changed

+155
-43
lines changed

6 files changed

+155
-43
lines changed

include/wolfengine/we_openssl_bc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
200200
#define EVP_PKEY_ECDH_KDF_X9_63 EVP_PKEY_ECDH_KDF_X9_62
201201
#endif
202202

203+
#ifndef EVP_PKEY_CTRL_DH_PAD
204+
/* First defined in OPENSSL_VERSION_NUMBER == 0x10101001L. */
205+
#define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
206+
#endif
207+
203208
WOLFENGINE_LOCAL const BIGNUM *DH_get0_p(const DH *dh);
204209
WOLFENGINE_LOCAL const BIGNUM *DH_get0_g(const DH *dh);
205210
WOLFENGINE_LOCAL const BIGNUM *DH_get0_q(const DH *dh);

src/we_dh.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
* local one.
3232
*/
3333

34-
#ifndef EVP_PKEY_CTRL_DH_PAD
35-
#define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
36-
#endif
37-
3834
#define DEFAULT_PRIME_LEN 1024
3935

4036
/**
@@ -567,6 +563,7 @@ static int we_dh_compute_key_int(we_Dh *engineDh, unsigned char *secret,
567563
unsigned char *privBuf = NULL;
568564
int privLen = 0;
569565
unsigned int secLen = 0;
566+
const BIGNUM* privBn;
570567

571568
WOLFENGINE_ENTER(WE_LOG_KE, "we_dh_compute_key_int");
572569
WOLFENGINE_MSG_VERBOSE(WE_LOG_KE, "ARGS [engineDh = %p, secret = %p, "
@@ -600,7 +597,15 @@ static int we_dh_compute_key_int(we_Dh *engineDh, unsigned char *secret,
600597

601598
if (ret == 1) {
602599
/* Convert our private key to a byte array. */
603-
ret = we_dh_bignum_to_bin(DH_get0_priv_key(dh), &privBuf, &privLen);
600+
privBn = DH_get0_priv_key(dh);
601+
if (privBn == NULL) {
602+
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Private key is NULL. Can't create "
603+
"DH shared secret.");
604+
ret = 0;
605+
}
606+
else {
607+
ret = we_dh_bignum_to_bin(DH_get0_priv_key(dh), &privBuf, &privLen);
608+
}
604609
}
605610

606611
if (ret == 1) {
@@ -1057,6 +1062,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
10571062
"setting the generator when generating DH params");
10581063
/* wolfCrypt doesn't allow setting the generator when generating
10591064
* DH params. */
1065+
ret = 0;
10601066
break;
10611067
case EVP_PKEY_CTRL_DH_PAD:
10621068
dh->pad = num;
@@ -1086,7 +1092,8 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
10861092
* Extra operations for working with DH.
10871093
* Supported operations include:
10881094
* - "dh_param": set the named parameters.
1089-
* - "pad": pad out secret to input length.
1095+
* - "dh_pad": pad out secret to input length.
1096+
* - "dh_paramgen_prime_len": set the length of the prime, "p."
10901097
*
10911098
* @param ctx [in] Public key context of operation.
10921099
* @param type [in] Type of operation to perform.
@@ -1113,7 +1120,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11131120

11141121
if (ret == 1) {
11151122
/* Set named DH parameters. */
1116-
if (XSTRNCMP(type, "dh_param", 9) == 0) {
1123+
if (XSTRCMP(type, "dh_param") == 0) {
11171124
#ifndef HAVE_WC_DHSETNAMEDKEY
11181125
const DhParams *params = NULL;
11191126
#else
@@ -1122,7 +1129,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11221129

11231130
#ifdef HAVE_PUBLIC_FFDHE
11241131
#ifdef HAVE_FFDHE_2048
1125-
if (XSTRNCMP(value, "ffdhe2048", 10) == 0) {
1132+
if (XSTRCMP(value, "ffdhe2048") == 0) {
11261133
WOLFENGINE_MSG(WE_LOG_KE,
11271134
"Setting named parameters: ffdhe2048");
11281135
#ifndef HAVE_WC_DHSETNAMEDKEY
@@ -1134,7 +1141,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11341141
else
11351142
#endif
11361143
#ifdef HAVE_FFDHE_3072
1137-
if (XSTRNCMP(value, "ffdhe3072", 10) == 0) {
1144+
if (XSTRCMP(value, "ffdhe3072") == 0) {
11381145
WOLFENGINE_MSG(WE_LOG_KE,
11391146
"Setting named parameters: ffdhe3072");
11401147
#ifndef HAVE_WC_DHSETNAMEDKEY
@@ -1146,7 +1153,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11461153
else
11471154
#endif
11481155
#ifdef HAVE_FFDHE_4096
1149-
if (XSTRNCMP(value, "ffdhe4096", 10) == 0) {
1156+
if (XSTRCMP(value, "ffdhe4096") == 0) {
11501157
WOLFENGINE_MSG(WE_LOG_KE,
11511158
"Setting named parameters: ffdhe4096");
11521159
#ifndef HAVE_WC_DHSETNAMEDKEY
@@ -1180,7 +1187,8 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11801187
rc = wc_DhSetNamedKey(&dh->key, params);
11811188
#endif
11821189
if (rc != 0) {
1183-
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Failed set parameters");
1190+
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Failed to set "
1191+
"parameters.");
11841192
ret = 0;
11851193
}
11861194
}
@@ -1189,9 +1197,13 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
11891197
}
11901198
}
11911199
/* Set padding requirement for secret output. */
1192-
else if (XSTRNCMP(type, "dh_pad", 7) == 0) {
1200+
else if (XSTRCMP(type, "dh_pad") == 0) {
11931201
dh->pad = XATOI(value);
11941202
}
1203+
else if (XSTRCMP(type, "dh_paramgen_prime_len") == 0) {
1204+
ret = we_dh_pkey_ctrl(ctx, EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN,
1205+
XATOI(value), NULL);
1206+
}
11951207
else {
11961208
/* Unsupported control type. */
11971209
XSNPRINTF(errBuff, sizeof(errBuff), "Unsupported ctrl string %s",

src/we_random.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -358,43 +358,45 @@ static int we_rand_bytes(unsigned char *buf, int num)
358358
WOLFENGINE_MSG_VERBOSE(WE_LOG_RNG, "ARGS [buf = %p, num = %d]",
359359
buf, num);
360360

361-
#ifndef WE_SINGLE_THREADED
362-
rc = wc_LockMutex(we_rng_mutex);
363-
if (rc != 0) {
364-
WOLFENGINE_ERROR_FUNC(WE_LOG_RNG, "wc_LockMutex", rc);
365-
ret = 0;
366-
}
367-
else
368-
#endif
369-
{
370-
/* Use global random to generator pseudo-random data. */
371-
rc = wc_RNG_GenerateBlock(we_rng, buf, num);
361+
if (num > 0) {
362+
#ifndef WE_SINGLE_THREADED
363+
rc = wc_LockMutex(we_rng_mutex);
372364
if (rc != 0) {
373-
WOLFENGINE_ERROR_FUNC(WE_LOG_RNG, "wc_RNG_GenerateBlock", rc);
365+
WOLFENGINE_ERROR_FUNC(WE_LOG_RNG, "wc_LockMutex", rc);
374366
ret = 0;
375367
}
376-
#ifndef WE_STATIC_WOLFSSL
377-
/* Mix global seed if RAND_add() or RAND_seed() has been called. */
378-
if (ret == 1 && haveSeed) {
379-
ret = we_rand_mix_seed(buf, num, we_seed, sizeof(we_seed));
380-
if (ret != 1) {
381-
WOLFENGINE_ERROR_MSG(WE_LOG_RNG, "we_rand_mix_seed with global "
382-
"seed failed");
368+
else
369+
#endif
370+
{
371+
/* Use global random to generator pseudo-random data. */
372+
rc = wc_RNG_GenerateBlock(we_rng, buf, num);
373+
if (rc != 0) {
374+
WOLFENGINE_ERROR_FUNC(WE_LOG_RNG, "wc_RNG_GenerateBlock", rc);
375+
ret = 0;
383376
}
384-
}
385-
/* Mix in weak entropy. */
386-
if (ret == 1) {
387-
ret = we_rand_add_weak_entropy(buf, num);
388-
if (ret != 1) {
389-
WOLFENGINE_ERROR_MSG(WE_LOG_RNG, "we_rand_mix_seed with "
390-
"weak entropy failed");
377+
#ifndef WE_STATIC_WOLFSSL
378+
/* Mix global seed if RAND_add() or RAND_seed() has been called. */
379+
if (ret == 1 && haveSeed) {
380+
ret = we_rand_mix_seed(buf, num, we_seed, sizeof(we_seed));
381+
if (ret != 1) {
382+
WOLFENGINE_ERROR_MSG(WE_LOG_RNG, "we_rand_mix_seed with "
383+
"global seed failed");
384+
}
391385
}
392-
}
393-
#endif /* !WE_STATIC_WOLFSSL */
386+
/* Mix in weak entropy. */
387+
if (ret == 1) {
388+
ret = we_rand_add_weak_entropy(buf, num);
389+
if (ret != 1) {
390+
WOLFENGINE_ERROR_MSG(WE_LOG_RNG, "we_rand_mix_seed with "
391+
"weak entropy failed");
392+
}
393+
}
394+
#endif /* !WE_STATIC_WOLFSSL */
394395

395-
#ifndef WE_SINGLE_THREADED
396-
wc_UnLockMutex(we_rng_mutex);
397-
#endif
396+
#ifndef WE_SINGLE_THREADED
397+
wc_UnLockMutex(we_rng_mutex);
398+
#endif
399+
}
398400
}
399401

400402
WOLFENGINE_LEAVE(WE_LOG_RNG, "we_rand_bytes", ret);

test/test_dh.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,97 @@ int test_dh_pkey(ENGINE* e, void* data)
426426
return err;
427427
}
428428

429+
int test_dh_ctrl(ENGINE* e, void* data)
430+
{
431+
int err;
432+
EVP_PKEY_CTX* ctx = NULL;
433+
int primeLengths[] = {1024, 2048, 3072};
434+
word32 i;
435+
436+
(void)data;
437+
438+
err = (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, e)) == NULL;
439+
if (err == 0){
440+
err = EVP_PKEY_paramgen_init(ctx) <= 0;
441+
}
442+
443+
for (i = 0; err == 0 && i < sizeof(primeLengths)/sizeof(*primeLengths);
444+
++i) {
445+
/* Set valid prime lengths. */
446+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
447+
EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, primeLengths[i],
448+
NULL) <= 0;
449+
}
450+
if (err == 0) {
451+
/* Set an invalid prime length. */
452+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
453+
EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, 512, NULL) > 0;
454+
}
455+
if (err == 0) {
456+
/* Set a valid prime length via control string. */
457+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_paramgen_prime_len", "2048") <= 0;
458+
}
459+
460+
if (err == 0) {
461+
/* Set the generator (not supported by wolfCrypt). */
462+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
463+
EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, 2, NULL) > 0;
464+
}
465+
466+
if (err == 0) {
467+
/* Zero pad secret. */
468+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
469+
EVP_PKEY_CTRL_DH_PAD, 1, NULL) <= 0;
470+
}
471+
if (err == 0) {
472+
/* Same as above but using a control string. */
473+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_pad", "1") <= 0;
474+
}
475+
476+
if (err == 0) {
477+
/* Set peer key for shared secret. No-op internally. */
478+
err = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
479+
EVP_PKEY_CTRL_PEER_KEY, 0, NULL) <= 0;
480+
}
481+
482+
#ifdef HAVE_PUBLIC_FFDHE
483+
/* Set parameters to different named parameter sets. */
484+
#ifdef HAVE_FFDHE_2048
485+
if (err == 0) {
486+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe2048") <= 0;
487+
}
488+
#else
489+
if (err == 0) {
490+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe2048") > 0;
491+
}
492+
#endif /* HAVE_FFDHE_2048 */
493+
494+
#ifdef HAVE_FFDHE_3072
495+
if (err == 0) {
496+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe3072") <= 0;
497+
}
498+
#else
499+
if (err == 0) {
500+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe3072") > 0;
501+
}
502+
#endif /* HAVE_FFDHE_3072 */
503+
504+
#ifdef HAVE_FFDHE_4096
505+
if (err == 0) {
506+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe4096") <= 0;
507+
}
508+
#else
509+
if (err == 0) {
510+
err = EVP_PKEY_CTX_ctrl_str(ctx, "dh_param", "ffdhe4096") > 0;
511+
}
512+
#endif /* HAVE_FFDHE_4096 */
513+
#endif /* HAVE_PUBLIC_FFDHE */
514+
515+
EVP_PKEY_CTX_free(ctx);
516+
517+
return err;
518+
}
519+
429520
#endif /* WE_HAVE_EVP_PKEY */
430521

431522
#endif /* WE_HAVE_DH */

test/unit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ TEST_CASE test_case[] = {
153153
#ifdef WE_HAVE_EVP_PKEY
154154
TEST_DECL(test_dh_pgen_pkey, NULL),
155155
TEST_DECL(test_dh_pkey, NULL),
156+
TEST_DECL(test_dh_ctrl, NULL),
156157
#if !defined(WE_SINGLE_THREADED) && defined(_WIN32)
157158
TEST_DECL(test_dh_key_gen_multithreaded, NULL),
158159
#endif /* !WE_SINGLE_THREADED && _WIN32 */

test/unit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ int test_dh(ENGINE *e, void *data);
249249
#ifdef WE_HAVE_EVP_PKEY
250250
int test_dh_pgen_pkey(ENGINE *e, void *data);
251251
int test_dh_pkey(ENGINE *e, void *data);
252+
int test_dh_ctrl(ENGINE *e, void *data);
252253
#if !defined(WE_SINGLE_THREADED) && defined(_WIN32)
253254
int test_dh_key_gen_multithreaded(ENGINE *e, void *data);
254255
#endif /* !WE_SINGLE_THREADED && _WIN32 */

0 commit comments

Comments
 (0)