Skip to content

Commit f385bf2

Browse files
committed
Refactor key matching to support ML-DSA
The function for matching a private key with its corresponding certificate has been refactored to be more generic. Instead of separate, hardcoded logic for each key type (RSA, EC), a general loop now compares the relevant attributes. This change was made to add support for matching ML-DSA keys. For ML-DSA, the public key is represented by the CKA_VALUE attribute, which is now correctly extracted from the certificate and compared against the public key object. Signed-off-by: Simo Sorce <simo@redhat.com>
1 parent 5d3db47 commit f385bf2

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/obj/export.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ CK_RV get_attrs_from_cert(P11PROV_OBJ *crt, CK_ATTRIBUTE *attrs, int num)
9090
break;
9191
}
9292
}
93+
} else if (EVP_PKEY_is_a(pkey, MLDSA_44) || EVP_PKEY_is_a(pkey, MLDSA_65)
94+
|| EVP_PKEY_is_a(pkey, MLDSA_87)) {
95+
for (int i = 0; i < num; i++) {
96+
switch (attrs[i].type) {
97+
case CKA_VALUE:
98+
types[attrnum] = CKA_VALUE;
99+
params[attrnum] = OSSL_PARAM_construct_octet_string(
100+
OSSL_PKEY_PARAM_PUB_KEY, NULL, 0);
101+
attrnum++;
102+
break;
103+
}
104+
}
93105
} else {
94106
rv = CKR_OBJECT_HANDLE_INVALID;
95107
goto done;

src/obj/keymgmt.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,14 @@ static int match_key_with_cert(P11PROV_OBJ *priv_key, P11PROV_OBJ *pub_key)
623623
attrs[0].type = CKA_P11PROV_PUB_KEY;
624624
num = 1;
625625
break;
626+
case CKK_ML_DSA:
627+
attrs[0].type = CKA_VALUE;
628+
num = 1;
629+
break;
630+
default:
631+
P11PROV_raise(priv_key->ctx, CKR_GENERAL_ERROR,
632+
"Unknown public key type");
633+
return RET_OSSL_ERR;
626634
}
627635

628636
ret = get_attrs_from_cert(cert, attrs, num);
@@ -633,37 +641,17 @@ static int match_key_with_cert(P11PROV_OBJ *priv_key, P11PROV_OBJ *pub_key)
633641
goto done;
634642
}
635643

636-
switch (pub_key->data.key.type) {
637-
case CKK_RSA:
638-
x = p11prov_obj_get_attr(pub_key, CKA_MODULUS);
639-
if (!x || x->ulValueLen != attrs[0].ulValueLen
640-
|| memcmp(x->pValue, attrs[0].pValue, x->ulValueLen) != 0) {
641-
ret = RET_OSSL_ERR;
642-
goto done;
643-
}
644-
645-
x = p11prov_obj_get_attr(pub_key, CKA_PUBLIC_EXPONENT);
646-
if (!x || x->ulValueLen != attrs[1].ulValueLen
647-
|| memcmp(x->pValue, attrs[1].pValue, x->ulValueLen) != 0) {
648-
ret = RET_OSSL_ERR;
649-
goto done;
650-
}
651-
652-
ret = RET_OSSL_OK;
653-
break;
654-
case CKK_EC:
655-
case CKK_EC_EDWARDS:
656-
x = p11prov_obj_get_attr(pub_key, CKA_P11PROV_PUB_KEY);
657-
if (!x || x->ulValueLen != attrs[0].ulValueLen
658-
|| memcmp(x->pValue, attrs[0].pValue, x->ulValueLen) != 0) {
644+
for (int i = 0; i < num; i++) {
645+
x = p11prov_obj_get_attr(pub_key, attrs[i].type);
646+
if (!x || x->ulValueLen != attrs[i].ulValueLen
647+
|| memcmp(x->pValue, attrs[i].pValue, x->ulValueLen) != 0) {
659648
ret = RET_OSSL_ERR;
660649
goto done;
661650
}
662-
663-
ret = RET_OSSL_OK;
664-
break;
665651
}
666652

653+
ret = RET_OSSL_OK;
654+
667655
done:
668656
for (int i = 0; i < num; i++) {
669657
OPENSSL_free(attrs[i].pValue);

0 commit comments

Comments
 (0)