Skip to content

Commit

Permalink
Implement text encoder for Ed25519 keys
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Jelen <[email protected]>
  • Loading branch information
Jakuje committed Apr 16, 2024
1 parent 5fb38be commit 1eb745c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,75 @@ p11prov_common_encoder_priv_key_info_pem_does_selection(void *inctx,
}
return RET_OSSL_ERR;
}

DISPATCH_TEXT_ENCODER_FN(ec_edwards, encode);

static int p11prov_ec_edwards_encoder_encode_text(
void *inctx, OSSL_CORE_BIO *cbio, const void *inkey,
const OSSL_PARAM key_abstract[], int selection,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
struct p11prov_encoder_ctx *ctx = (struct p11prov_encoder_ctx *)inctx;
P11PROV_OBJ *key = (P11PROV_OBJ *)inkey;
CK_KEY_TYPE type;
CK_ULONG keysize;
const char *type_name = "ED25519";
char *uri = NULL;
BIO *out;
int ret;

P11PROV_debug("EdDSA Text Encoder");

type = p11prov_obj_get_key_type(key);
if (type != CKK_EC_EDWARDS) {
P11PROV_raise(ctx->provctx, CKR_GENERAL_ERROR, "Invalid Key Type");
return RET_OSSL_ERR;
}

out = BIO_new_from_core_bio(p11prov_ctx_get_libctx(ctx->provctx), cbio);
if (!out) {
P11PROV_raise(ctx->provctx, CKR_GENERAL_ERROR, "Failed to init BIO");
return RET_OSSL_ERR;
}

keysize = p11prov_obj_get_key_bit_size(key);
if (keysize == 448) {
type_name = "ED448";
}
if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
CK_OBJECT_CLASS class = p11prov_obj_get_class(key);
if (class != CKO_PRIVATE_KEY) {
return RET_OSSL_ERR;
}
BIO_printf(out, "PKCS11 %s Private Key (%lu bits)\n", type_name,
keysize);
BIO_printf(out, "[Can't export and print private key data]\n");
}

if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
BIO_printf(out, "PKCS11 %s Public Key (%lu bits)\n", type_name,
keysize);
ret = p11prov_obj_export_public_key(key, CKK_EC_EDWARDS, true,
p11prov_ec_print_public_key, out);
/* FIXME if we want print in different format */
if (ret != RET_OSSL_OK) {
BIO_printf(out, "[Error: Failed to decode public key data]\n");
}
}

uri = p11prov_key_to_uri(ctx->provctx, key);
if (uri) {
BIO_printf(out, "URI %s\n", uri);
}

OPENSSL_free(uri);
BIO_free(out);
return RET_OSSL_OK;
}

const OSSL_DISPATCH p11prov_ec_edwards_encoder_text_functions[] = {
DISPATCH_BASE_ENCODER_ELEM(NEWCTX, newctx),
DISPATCH_BASE_ENCODER_ELEM(FREECTX, freectx),
DISPATCH_TEXT_ENCODER_ELEM(ENCODE, ec_edwards, encode_text),
{ 0, NULL },
};
1 change: 1 addition & 0 deletions src/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ extern const OSSL_DISPATCH p11prov_ec_encoder_spki_der_functions[];
extern const OSSL_DISPATCH p11prov_ec_encoder_priv_key_info_pem_functions[];
extern const OSSL_DISPATCH
p11prov_ec_edwards_encoder_priv_key_info_pem_functions[];
extern const OSSL_DISPATCH p11prov_ec_edwards_encoder_text_functions[];

#endif /* _ENCODER_H */
4 changes: 4 additions & 0 deletions src/provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,10 @@ static CK_RV operations_init(P11PROV_CTX *ctx)
ADD_ALGO_EXT(EC, encoder,
"provider=pkcs11,output=der,structure=SubjectPublicKeyInfo",
p11prov_ec_encoder_spki_der_functions);
ADD_ALGO_EXT(ED25519, encoder, "provider=pkcs11,output=text",
p11prov_ec_edwards_encoder_text_functions);
ADD_ALGO_EXT(ED448, encoder, "provider=pkcs11,output=text",
p11prov_ec_edwards_encoder_text_functions);
if (ctx->encode_pkey_as_pk11_uri) {
ADD_ALGO_EXT(RSA, encoder,
"provider=pkcs11,output=pem,structure=PrivateKeyInfo",
Expand Down
2 changes: 1 addition & 1 deletion tests/tedwards
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ title LINE "Print ED25519 Public key from private"
ossl 'pkey -in $EDPRIURI -pubout -text' $helper_emit
output="$helper_output"
FAIL=0
echo "$output" | grep "ED25519 Public-Key" > /dev/null 2>&1 || FAIL=1
echo "$output" | grep "ED25519 Public Key" > /dev/null 2>&1 || FAIL=1
if [ $FAIL -eq 1 ]; then
echo "Could not extract public key from private"
echo
Expand Down

0 comments on commit 1eb745c

Please sign in to comment.