Skip to content

Commit 051a027

Browse files
authored
Merge pull request #90 from ColtonWilley/wp_ecc_encoding_fix2
Fix param handling for ec encoding for openssl genpkey
2 parents eb8a716 + cc2cd06 commit 051a027

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

src/wp_ecc_kmgmt.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,15 @@ static int wp_ecc_gen_set_params(wp_EccGenCtx* ctx, const OSSL_PARAM params[]);
116116
/** Mapping of OpenSSL curve name to wolfSSL elliptic curve information. */
117117
static const wp_EccGroupMap wp_ecc_group_map[] = {
118118
{ SN_X9_62_prime192v1, ECC_SECP192R1, 192 },
119+
{ "P-192" , ECC_SECP192R1, 192 },
119120
{ SN_secp224r1 , ECC_SECP224R1, 224 },
121+
{ "P-224" , ECC_SECP224R1, 224 },
120122
{ SN_X9_62_prime256v1, ECC_SECP256R1, 256 },
123+
{ "P-256" , ECC_SECP256R1, 256 },
121124
{ SN_secp384r1 , ECC_SECP384R1, 384 },
125+
{ "P-384" , ECC_SECP384R1, 384 },
122126
{ SN_secp521r1 , ECC_SECP521R1, 521 },
127+
{ "P-521" , ECC_SECP521R1, 521 },
123128
};
124129

125130
/** Number of entries in elliptic curve mapping. */
@@ -1569,6 +1574,9 @@ static int wp_ecc_gen_set_template(wp_EccGenCtx* ctx, wp_Ecc* ecc)
15691574
return ok;
15701575
}
15711576

1577+
#define WP_EC_ENCODING_NAMED_CURVE_STR "named_curve"
1578+
#define WP_EC_ENCODING_NAMED_CURVE_STR_LEN 11
1579+
15721580
/**
15731581
* Sets the parameters into the ECC generation context object.
15741582
*
@@ -1590,6 +1598,25 @@ static int wp_ecc_gen_set_params(wp_EccGenCtx* ctx, const OSSL_PARAM params[])
15901598
ctx->curveName, sizeof(ctx->curveName)))) {
15911599
ok = 0;
15921600
}
1601+
if (ok) {
1602+
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
1603+
if (p != NULL) {
1604+
if (p->data_type != OSSL_PARAM_UTF8_STRING) {
1605+
ok = 0;
1606+
}
1607+
else if (p->data_size != WP_EC_ENCODING_NAMED_CURVE_STR_LEN) {
1608+
ok = 0;
1609+
}
1610+
else if (XMEMCMP(p->data, WP_EC_ENCODING_NAMED_CURVE_STR,
1611+
p->data_size) != 0) {
1612+
ok = 0;
1613+
}
1614+
if (!ok) {
1615+
WOLFPROV_ERROR_MSG(WP_LOG_PK,
1616+
"only named curve encoding supported");
1617+
}
1618+
}
1619+
}
15931620

15941621
WOLFPROV_LEAVE(WP_LOG_PK, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
15951622
return ok;
@@ -1685,6 +1712,7 @@ static const OSSL_PARAM* wp_ecc_gen_settable_params(wp_EccGenCtx* ctx,
16851712
static OSSL_PARAM wp_ecc_gen_supported_settable_params[] = {
16861713
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
16871714
OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
1715+
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
16881716
OSSL_PARAM_END
16891717
};
16901718
(void)ctx;

test/test_ecc.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,115 @@ static const unsigned char ecc_derived_521[] = {
268268

269269
#ifdef WP_HAVE_ECKEYGEN
270270

271+
static int test_eckeygen_name_ex(const char *name, int setEncoding, int expectFail) {
272+
int err;
273+
EVP_PKEY_CTX *ctx = NULL;
274+
EVP_PKEY *key = NULL;
275+
(void)expectFail;
276+
277+
PRINT_MSG("Create public key context");
278+
err = (ctx = EVP_PKEY_CTX_new_from_name(wpLibCtx, "EC", NULL)) == NULL;
279+
if (err == 0) {
280+
PRINT_MSG("Initialize key generation");
281+
err = EVP_PKEY_keygen_init(ctx) != 1;
282+
}
283+
if (err == 0) {
284+
PRINT_MSG("Set named curve");
285+
err = EVP_PKEY_CTX_ctrl_str(ctx, "ec_paramgen_curve", name) != 1;
286+
}
287+
if (err == 0 && setEncoding) {
288+
/* For now only testing explictly setting named curve encoding */
289+
err = EVP_PKEY_CTX_ctrl_str(ctx, "ec_param_enc",
290+
OSSL_PKEY_EC_ENCODING_GROUP) != 1;
291+
}
292+
if (err == 0) {
293+
PRINT_MSG("Generate key");
294+
err = EVP_PKEY_keygen(ctx, &key) != 1;
295+
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
296+
if (expectFail) {
297+
err = err != 1;
298+
if (err == 0) {
299+
PRINT_MSG("Key gen failed, expected"
300+
"(P-192 not allowed w/ FIPS)");
301+
}
302+
else {
303+
PRINT_MSG("Key gen succeeded, unexpected"
304+
"(P-192 not allowed w/FIPS)");
305+
}
306+
}
307+
#endif /* HAVE_FIPS || HAVE_FIPS_VERSION */
308+
}
309+
310+
EVP_PKEY_free(key);
311+
EVP_PKEY_CTX_free(ctx);
312+
313+
return err;
314+
}
315+
316+
int test_eckeygen_name(void *data) {
317+
int err = 0;
318+
(void)data;
319+
#ifdef WP_HAVE_EC_P192
320+
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
321+
err = test_eckeygen_name_ex("P-192", 0, 1);
322+
#else
323+
err = test_eckeygen_name_ex("P-192", 0, 0);
324+
if (err == 0) {
325+
err = test_eckeygen_name_ex("P-192", 1, 0);
326+
}
327+
if (err == 0) {
328+
err = test_eckeygen_name_ex(SN_X9_62_prime192v1, 1, 0);
329+
}
330+
#endif
331+
#endif
332+
#ifdef WP_HAVE_EC_P224
333+
if (err == 0) {
334+
err = test_eckeygen_name_ex("P-192", 0, 0);
335+
}
336+
if (err == 0) {
337+
err = test_eckeygen_name_ex("P-192", 1, 0);
338+
}
339+
if (err == 0) {
340+
err = test_eckeygen_name_ex(SN_secp224r1, 1, 0);
341+
}
342+
#endif
343+
#ifdef WP_HAVE_EC_P256
344+
if (err == 0) {
345+
err = test_eckeygen_name_ex("P-256", 0, 0);
346+
}
347+
if (err == 0) {
348+
err = test_eckeygen_name_ex("P-256", 1, 0);
349+
}
350+
if (err == 0) {
351+
err = test_eckeygen_name_ex(SN_X9_62_prime256v1, 1, 0);
352+
}
353+
#endif
354+
#ifdef WP_HAVE_EC_P384
355+
if (err == 0) {
356+
err = test_eckeygen_name_ex("P-384", 0, 0);
357+
}
358+
if (err == 0) {
359+
err = test_eckeygen_name_ex("P-384", 1, 0);
360+
}
361+
if (err == 0) {
362+
err = test_eckeygen_name_ex(SN_secp384r1, 1, 0);
363+
}
364+
#endif
365+
#ifdef WP_HAVE_EC_P521
366+
if (err == 0) {
367+
err = test_eckeygen_name_ex("P-521", 0, 0);
368+
}
369+
if (err == 0) {
370+
err = test_eckeygen_name_ex("P-521", 1, 0);
371+
}
372+
if (err == 0) {
373+
err = test_eckeygen_name_ex(SN_secp521r1, 1, 0);
374+
}
375+
#endif
376+
377+
return err;
378+
}
379+
271380
#ifdef WP_HAVE_EC_P192
272381
int test_eckeygen_p192(void *data)
273382
{

test/unit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ TEST_CASE test_case[] = {
260260
#endif
261261
#endif
262262
#endif
263+
#ifdef WP_HAVE_ECKEYGEN
264+
TEST_DECL(test_eckeygen_name, NULL),
265+
#endif
266+
263267
#ifdef WP_HAVE_ECDSA
264268
TEST_DECL(test_ec_load_key, NULL),
265269
TEST_DECL(test_ec_load_cert, NULL),

test/unit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ int test_dh_pkey(void *data);
254254

255255
#ifdef WP_HAVE_ECKEYGEN
256256

257+
int test_eckeygen_name(void *data);
258+
257259
#ifdef WP_HAVE_EC_P192
258260
int test_eckeygen_p192(void *data);
259261
#endif /* WP_HAVE_EC_P192 */

0 commit comments

Comments
 (0)