From 20e91a426a5e306f6c9706a56c0bda718ff2047d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AD=D0=B4=D1=83=D0=B0=D1=80=D0=B4=20=D0=A1=D0=B0=D0=B1?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2?= Date: Mon, 23 Dec 2024 12:53:07 +0300 Subject: [PATCH] Fix serialization pub key Currently, EVP_PKEY_print_public specifies an incorrect key class for the generated EVP_PKEY. Now selection is also used to get the key class. Signed-off-by: Eduard Sabirov --- src/encoder.c | 8 ++++---- src/util.c | 8 ++++++-- src/util.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/encoder.c b/src/encoder.c index 66d75f5c..f5cae6cb 100644 --- a/src/encoder.c +++ b/src/encoder.c @@ -140,7 +140,7 @@ static int p11prov_rsa_encoder_encode_text(void *inctx, OSSL_CORE_BIO *cbio, } } - uri = p11prov_key_to_uri(ctx->provctx, key); + uri = p11prov_key_to_uri(ctx->provctx, key, selection); if (uri) { BIO_printf(out, "URI %s\n", uri); free(uri); @@ -474,7 +474,7 @@ static P11PROV_PK11_URI *p11prov_encoder_private_key_to_asn1(P11PROV_CTX *pctx, size_t uri_len; int ret = RET_OSSL_ERR; - uri = p11prov_key_to_uri(pctx, key); + uri = p11prov_key_to_uri(pctx, key, OSSL_KEYMGMT_SELECT_PRIVATE_KEY); if (!uri) { goto done; } @@ -896,7 +896,7 @@ static int p11prov_ec_encoder_encode_text(void *inctx, OSSL_CORE_BIO *cbio, } } - uri = p11prov_key_to_uri(ctx->provctx, key); + uri = p11prov_key_to_uri(ctx->provctx, key, selection); if (uri) { BIO_printf(out, "URI %s\n", uri); } @@ -1014,7 +1014,7 @@ static int p11prov_ec_edwards_encoder_encode_text( } } - uri = p11prov_key_to_uri(ctx->provctx, key); + uri = p11prov_key_to_uri(ctx->provctx, key, selection); if (uri) { BIO_printf(out, "URI %s\n", uri); } diff --git a/src/util.c b/src/util.c index 66a3bd0f..13c5f68c 100644 --- a/src/util.c +++ b/src/util.c @@ -670,7 +670,7 @@ static char *uri_component(const char *name, const char *val, size_t vlen, return c; } -char *p11prov_key_to_uri(P11PROV_CTX *ctx, P11PROV_OBJ *key) +char *p11prov_key_to_uri(P11PROV_CTX *ctx, P11PROV_OBJ *key, int selection) { P11PROV_SLOTS_CTX *slots; P11PROV_SLOT *slot; @@ -691,7 +691,11 @@ char *p11prov_key_to_uri(P11PROV_CTX *ctx, P11PROV_OBJ *key) size_t size_hint = 0; CK_RV ret; - class = p11prov_obj_get_class(key); + if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) { + class = CKO_PUBLIC_KEY; + } else { + class = p11prov_obj_get_class(key); + } slot_id = p11prov_obj_get_slotid(key); cka_id = p11prov_obj_get_attr(key, CKA_ID); cka_label = p11prov_obj_get_attr(key, CKA_LABEL); diff --git a/src/util.h b/src/util.h index a96eec72..7c5d9d9d 100644 --- a/src/util.h +++ b/src/util.h @@ -54,7 +54,7 @@ void p11prov_fetch_attrs_free(struct fetch_attrs *attrs, int num); #define MAX_PIN_LENGTH 32 int parse_ulong(P11PROV_CTX *ctx, const char *str, size_t len, void **output); P11PROV_URI *p11prov_parse_uri(P11PROV_CTX *ctx, const char *uri); -char *p11prov_key_to_uri(P11PROV_CTX *ctx, P11PROV_OBJ *key); +char *p11prov_key_to_uri(P11PROV_CTX *ctx, P11PROV_OBJ *key, int selection); void p11prov_uri_free(P11PROV_URI *parsed_uri); CK_OBJECT_CLASS p11prov_uri_get_class(P11PROV_URI *uri); void p11prov_uri_set_class(P11PROV_URI *uri, CK_OBJECT_CLASS class);