Skip to content

Commit

Permalink
Set the raw point for ECDH public data params
Browse files Browse the repository at this point in the history
Signed-off-by: Simo Sorce <simo@redhat.com>
  • Loading branch information
simo5 committed Jul 22, 2024
1 parent fe03d43 commit c8ebeb2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/exchange.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static int p11prov_ecdh_derive(void *ctx, unsigned char *secret,
}
}

ec_point = p11prov_obj_get_attr(ecdhctx->peer_key, CKA_EC_POINT);
ec_point = p11prov_obj_get_ec_public_raw(ecdhctx->peer_key);
if (ec_point == NULL) {
return RET_OSSL_ERR;
}
Expand Down
59 changes: 59 additions & 0 deletions src/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,65 @@ int p11prov_obj_get_ec_public_x_y(P11PROV_OBJ *obj, CK_ATTRIBUTE **pub_x,
return ret;
}

CK_ATTRIBUTE *p11prov_obj_get_ec_public_raw(P11PROV_OBJ *key)
{
CK_ATTRIBUTE *pub_key;

if (!key) {
return RET_OSSL_ERR;
}

if (key->data.key.type != CKK_EC) {
P11PROV_raise(key->ctx, CKR_GENERAL_ERROR, "Unsupported key type");
return RET_OSSL_ERR;
}

if (key->class != CKO_PRIVATE_KEY && key->class != CKO_PUBLIC_KEY) {
P11PROV_raise(key->ctx, CKR_GENERAL_ERROR, "Invalid Object Class");
return RET_OSSL_ERR;
}

pub_key = p11prov_obj_get_attr(key, CKA_P11PROV_PUB_KEY);
if (!pub_key) {
CK_ATTRIBUTE *ec_point;

ec_point = p11prov_obj_get_attr(key, CKA_EC_POINT);
if (ec_point) {
const unsigned char *val;
ASN1_OCTET_STRING *octet;
void *tmp_ptr;

val = ec_point->pValue;
octet = d2i_ASN1_OCTET_STRING(NULL, (const unsigned char **)&val,
ec_point->ulValueLen);
if (!octet) {
P11PROV_raise(key->ctx, CKR_KEY_INDIGESTIBLE,
"Failed to decode CKA_EC_POINT");
return NULL;
}
tmp_ptr = OPENSSL_realloc(key->attrs, sizeof(CK_ATTRIBUTE)
* (key->numattrs + 1));
if (!tmp_ptr) {
P11PROV_raise(key->ctx, CKR_HOST_MEMORY,
"Failed to allocate memory key attributes");
return NULL;
}
key->attrs = tmp_ptr;

CKATTR_ASSIGN(key->attrs[key->numattrs], CKA_P11PROV_PUB_KEY,
octet->data, octet->length);
key->numattrs++;

pub_key = &key->attrs[key->numattrs - 1];
}
}

if (!pub_key) {
P11PROV_debug("ECC Public Point not found");
}
return pub_key;
}

static int cmp_attr(P11PROV_OBJ *key1, P11PROV_OBJ *key2,
CK_ATTRIBUTE_TYPE attr)
{
Expand Down
1 change: 1 addition & 0 deletions src/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int p11prov_obj_export_public_key(P11PROV_OBJ *obj, CK_KEY_TYPE key_type,
int p11prov_obj_get_ec_public_x_y(P11PROV_OBJ *obj, CK_ATTRIBUTE **pub_x,
CK_ATTRIBUTE **pub_y);
int p11prov_obj_get_ed_pub_key(P11PROV_OBJ *obj, CK_ATTRIBUTE **pub);
CK_ATTRIBUTE *p11prov_obj_get_ec_public_raw(P11PROV_OBJ *key);

#define OBJ_CMP_KEY_TYPE 0x00
#define OBJ_CMP_KEY_PUBLIC 0x01
Expand Down
2 changes: 1 addition & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ tests = {
'certs': {'suites': ['softokn', 'softhsm', 'kryoptic']},
'ecc': {'suites': ['softokn', 'softhsm', 'kryoptic']},
'edwards': {'suites': ['softhsm']},
'ecdh': {'suites': ['softokn']},
'ecdh': {'suites': ['softokn', 'kryoptic']},
'democa': {'suites': ['softokn', 'softhsm', 'kryoptic'], 'is_parallel': false},
'digest': {'suites': ['softokn', 'softhsm', 'kryoptic']},
'fork': {'suites': ['softokn', 'softhsm', 'kryoptic']},
Expand Down

0 comments on commit c8ebeb2

Please sign in to comment.