Skip to content

Commit 9b53f9a

Browse files
authored
Merge pull request #134 from ColtonWilley/wp_ecc_get_priv_bn_fix
Fix wolfprovider ECC get priv key handling
2 parents 86b3227 + a7f4c76 commit 9b53f9a

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

src/wp_ecc_kmgmt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ static int wp_ecc_get_params(wp_Ecc* ecc, OSSL_PARAM params[])
818818
#else
819819
&(ecc->key.k),
820820
#endif
821-
1))) {
821+
ecc->hasPriv))) {
822822
ok = 0;
823823
}
824824
/* Private key. */

test/test_ecc.c

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ static const unsigned char ecc_key_der_256[] = {
8989

9090
/* Raw P256 group and priv key for EVP_PKEY_fromdata() */
9191
static const char *ecc_p256_group_str = "prime256v1";
92+
static const unsigned char ecc_p256_pub[] = {
93+
0x04, 0x29, 0x48, 0x87, 0x1D, 0x51, 0xEB, 0xC1, 0x20, 0x99,
94+
0x6B, 0x2C, 0x2E, 0xAA, 0xCE, 0x2F, 0x8D, 0x28, 0x52, 0x57,
95+
0xA6, 0x5D, 0x78, 0xD1, 0x90, 0xAB, 0xDB, 0xF8, 0xA3, 0x7D,
96+
0xF9, 0x77, 0x4F, 0x1F, 0x30, 0xE5, 0x67, 0xCF, 0x3F, 0x52,
97+
0xA0, 0x6F, 0x98, 0xDE, 0x98, 0x63, 0x88, 0xB2, 0xF1, 0x3F,
98+
0x1E, 0x41, 0xB9, 0x03, 0xCB, 0xB0, 0x83, 0x1C, 0x54, 0xA9,
99+
0xC4, 0xCA, 0x45, 0xA4, 0x9A
100+
};
92101
static const unsigned char ecc_p256_priv[] = {
93102
0x89, 0x93, 0x7A, 0x74, 0x00, 0x48, 0x17, 0x0D, 0x3D, 0x8E,
94103
0x23, 0x74, 0xAE, 0x7F, 0xD5, 0x31, 0x99, 0x0A, 0x32, 0x34,
@@ -1814,7 +1823,7 @@ int test_ec_decode(void* data)
18141823
return err;
18151824
}
18161825

1817-
int test_ec_import(void* data)
1826+
static int test_ec_import_priv(void)
18181827
{
18191828
int err = 0;
18201829
int len = 0;
@@ -1830,8 +1839,6 @@ int test_ec_import(void* data)
18301839
OSSL_PARAM_BLD *bld = NULL;
18311840
BIGNUM* priv = NULL;
18321841

1833-
(void)data;
1834-
18351842
/* Hand construct ECC private only key simulating bind9 flow */
18361843
err = (bld = OSSL_PARAM_BLD_new()) == NULL;
18371844
if (err == 0) {
@@ -1919,5 +1926,84 @@ int test_ec_import(void* data)
19191926
return err;
19201927
}
19211928

1929+
static int test_ec_import_pub(void)
1930+
{
1931+
int err = 0;
1932+
EVP_PKEY_CTX *ctx1 = NULL;
1933+
EVP_PKEY_CTX *ctx2 = NULL;
1934+
EVP_PKEY* pkey1 = NULL;
1935+
EVP_PKEY* pkey2 = NULL;
1936+
OSSL_PARAM *params = NULL;
1937+
OSSL_PARAM_BLD *bld = NULL;
1938+
BIGNUM* priv = NULL;
1939+
1940+
/* Hand construct ECC public only key simulating bind9 flow */
1941+
err = (bld = OSSL_PARAM_BLD_new()) == NULL;
1942+
if (err == 0) {
1943+
err = OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1944+
ecc_p256_group_str, 0) != 1;
1945+
}
1946+
if (err == 0) {
1947+
err = OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
1948+
ecc_p256_pub, sizeof(ecc_p256_pub)) != 1;
1949+
}
1950+
if (err == 0) {
1951+
err = (params = OSSL_PARAM_BLD_to_param(bld)) == NULL;
1952+
}
1953+
/* Create openssl and wolfprovider backed pkey */
1954+
if (err == 0) {
1955+
err = (ctx1 = EVP_PKEY_CTX_new_from_name(osslLibCtx, "EC", NULL)) == NULL;
1956+
}
1957+
if (err == 0) {
1958+
err = EVP_PKEY_fromdata_init(ctx1) != 1;
1959+
}
1960+
if (err == 0) {
1961+
err = EVP_PKEY_fromdata(ctx1, &pkey1, EVP_PKEY_KEYPAIR, params) != 1;
1962+
}
1963+
if (err == 0) {
1964+
err = (ctx2 = EVP_PKEY_CTX_new_from_name(wpLibCtx, "EC", NULL)) == NULL;
1965+
}
1966+
if (err == 0) {
1967+
err = EVP_PKEY_fromdata_init(ctx2) != 1;
1968+
}
1969+
if (err == 0) {
1970+
err = EVP_PKEY_fromdata(ctx2, &pkey2, EVP_PKEY_KEYPAIR, params) != 1;
1971+
}
1972+
1973+
/* Perform standard validation of public key data */
1974+
err = test_ec_pubkey_match(pkey1, pkey2);
1975+
1976+
/* Ensure that attempts to get the private key fail appropriately */
1977+
if (err == 0) {
1978+
err = EVP_PKEY_get_bn_param(pkey1, OSSL_PKEY_PARAM_PRIV_KEY, &priv) != 0;
1979+
}
1980+
if (err == 0) {
1981+
err = EVP_PKEY_get_bn_param(pkey2, OSSL_PKEY_PARAM_PRIV_KEY, &priv) != 0;
1982+
}
1983+
1984+
EVP_PKEY_free(pkey1);
1985+
EVP_PKEY_free(pkey2);
1986+
EVP_PKEY_CTX_free(ctx1);
1987+
EVP_PKEY_CTX_free(ctx2);
1988+
OSSL_PARAM_free(params);
1989+
OSSL_PARAM_BLD_free(bld);
1990+
BN_clear_free(priv);
1991+
1992+
return err;
1993+
}
1994+
1995+
int test_ec_import(void* data)
1996+
{
1997+
int err = 0;
1998+
(void)data;
1999+
2000+
err = test_ec_import_priv();
2001+
if (err == 0) {
2002+
err = test_ec_import_pub();
2003+
}
2004+
2005+
return err;
2006+
}
2007+
19222008

19232009
#endif /* WP_HAVE_ECC */

0 commit comments

Comments
 (0)