34 #ifdef HAVE_XKEY_PROVIDER
36 #include <openssl/provider.h>
37 #include <openssl/params.h>
38 #include <openssl/core_dispatch.h>
39 #include <openssl/core_object.h>
40 #include <openssl/core_names.h>
41 #include <openssl/store.h>
42 #include <openssl/evp.h>
43 #include <openssl/err.h>
46 static const char *provname =
"OpenVPN External Key Provider";
54 #define xkey_dmsg(f, ...) \
56 dmsg(f|M_NOLF, "xkey_provider: In %s: ", __func__); \
57 dmsg(f|M_NOPREFIX, __VA_ARGS__); \
93 XKEY_EXTERNAL_SIGN_fn *sign;
95 XKEY_PRIVKEY_FREE_fn *free;
96 XKEY_PROVIDER_CTX *prov;
100 static inline const char *
101 get_keytype(
const XKEY_KEYDATA *
key)
103 int keytype =
key->pubkey ? EVP_PKEY_get_id(
key->pubkey) : 0;
113 case EVP_PKEY_ED25519:
123 KEYSIZE(
const XKEY_KEYDATA *
key)
125 return key->pubkey ? EVP_PKEY_get_size(
key->pubkey) : 0;
133 xkey_native_sign(XKEY_KEYDATA *
key,
unsigned char *sig,
size_t *siglen,
134 const unsigned char *tbs,
size_t tbslen, XKEY_SIGALG sigalg);
140 static OSSL_FUNC_keymgmt_new_fn keymgmt_new;
141 static OSSL_FUNC_keymgmt_free_fn keymgmt_free;
142 static OSSL_FUNC_keymgmt_load_fn keymgmt_load;
143 static OSSL_FUNC_keymgmt_has_fn keymgmt_has;
144 static OSSL_FUNC_keymgmt_match_fn keymgmt_match;
145 static OSSL_FUNC_keymgmt_import_fn rsa_keymgmt_import;
146 static OSSL_FUNC_keymgmt_import_fn ec_keymgmt_import;
147 static OSSL_FUNC_keymgmt_import_types_fn keymgmt_import_types;
148 static OSSL_FUNC_keymgmt_get_params_fn keymgmt_get_params;
149 static OSSL_FUNC_keymgmt_gettable_params_fn keymgmt_gettable_params;
150 static OSSL_FUNC_keymgmt_set_params_fn keymgmt_set_params;
151 static OSSL_FUNC_keymgmt_query_operation_name_fn rsa_keymgmt_name;
152 static OSSL_FUNC_keymgmt_query_operation_name_fn ec_keymgmt_name;
155 keymgmt_import_helper(XKEY_KEYDATA *
key,
const OSSL_PARAM params[]);
157 static XKEY_KEYDATA *
160 xkey_dmsg(
D_XKEY,
"entry");
162 XKEY_KEYDATA *
key = OPENSSL_zalloc(
sizeof(*
key));
172 keydata_free(XKEY_KEYDATA *
key)
174 xkey_dmsg(
D_XKEY,
"entry");
176 if (!
key ||
key->refcount-- > 0)
180 if (
key->free &&
key->handle)
187 EVP_PKEY_free(
key->pubkey);
193 keymgmt_new(
void *provctx)
195 xkey_dmsg(
D_XKEY,
"entry");
197 XKEY_KEYDATA *
key = keydata_new();
207 keymgmt_load(
const void *reference,
size_t reference_sz)
209 xkey_dmsg(
D_XKEY,
"entry");
247 keymgmt_import(
void *keydata,
int selection,
const OSSL_PARAM params[],
const char *name)
249 xkey_dmsg(
D_XKEY,
"entry");
251 XKEY_KEYDATA *
key = keydata;
255 if (
key->handle ||
key->pubkey)
257 msg(
M_WARN,
"Error: keymgmt_import: keydata not empty -- our keys are immutable");
262 const OSSL_PARAM *p = OSSL_PARAM_locate_const(params,
"xkey-origin");
263 if (p && p->data_type == OSSL_PARAM_UTF8_STRING)
265 key->origin = EXTERNAL_KEY;
266 xkey_dmsg(
D_XKEY,
"importing external key");
267 return keymgmt_import_helper(
key, params);
270 xkey_dmsg(
D_XKEY,
"importing native key");
273 EVP_PKEY *pkey = NULL;
274 int selection_pub = selection & ~OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
276 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(
key->prov->libctx, name, NULL);
278 || (EVP_PKEY_fromdata_init(ctx) != 1)
279 || (EVP_PKEY_fromdata(ctx, &pkey, selection_pub, (OSSL_PARAM *) params) !=1))
281 msg(
M_WARN,
"Error: keymgmt_import failed for key type <%s>", name);
288 EVP_PKEY_CTX_free(ctx);
294 key->origin = OPENSSL_NATIVE;
295 if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
299 if (EVP_PKEY_fromdata(ctx, &pkey, selection, (OSSL_PARAM *) params) == 1)
302 key->free = (XKEY_PRIVKEY_FREE_fn *) EVP_PKEY_free;
305 EVP_PKEY_CTX_free(ctx);
307 xkey_dmsg(
D_XKEY,
"imported native %s key", EVP_PKEY_get0_type_name(pkey));
312 rsa_keymgmt_import(
void *keydata,
int selection,
const OSSL_PARAM params[])
314 xkey_dmsg(
D_XKEY,
"entry");
316 return keymgmt_import(keydata, selection, params,
"RSA");
320 ec_keymgmt_import(
void *keydata,
int selection,
const OSSL_PARAM params[])
322 xkey_dmsg(
D_XKEY,
"entry");
324 return keymgmt_import(keydata, selection, params,
"EC");
328 ed448_keymgmt_import(
void *keydata,
int selection,
const OSSL_PARAM params[])
330 xkey_dmsg(
D_XKEY,
"entry");
332 return keymgmt_import(keydata, selection, params,
"ED448");
336 ed25519_keymgmt_import(
void *keydata,
int selection,
const OSSL_PARAM params[])
338 xkey_dmsg(
D_XKEY,
"entry");
340 return keymgmt_import(keydata, selection, params,
"ED25519");
348 static const OSSL_PARAM *
349 keymgmt_import_types(
int selection)
351 xkey_dmsg(
D_XKEY,
"entry");
353 static const OSSL_PARAM key_types[] = { OSSL_PARAM_END };
355 if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
363 keymgmt_free(
void *keydata)
365 xkey_dmsg(
D_XKEY,
"entry");
367 keydata_free(keydata);
371 keymgmt_has(
const void *keydata,
int selection)
373 xkey_dmsg(
D_XKEY,
"selection = %d", selection);
375 const XKEY_KEYDATA *
key = keydata;
376 int ok = (
key != NULL);
378 if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
380 ok = ok &&
key->pubkey;
382 if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
384 ok = ok &&
key->handle;
391 keymgmt_match(
const void *keydata1,
const void *keydata2,
int selection)
393 const XKEY_KEYDATA *
key1 = keydata1;
394 const XKEY_KEYDATA *
key2 = keydata2;
396 xkey_dmsg(
D_XKEY,
"entry");
402 if (selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
404 ret = ret && EVP_PKEY_eq(
key1->pubkey,
key2->pubkey);
405 xkey_dmsg(
D_XKEY,
"checking key pair match: res = %d", ret);
408 if (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
410 ret = ret && EVP_PKEY_parameters_eq(
key1->pubkey,
key2->pubkey);
411 xkey_dmsg(
D_XKEY,
"checking parameter match: res = %d", ret);
418 static const OSSL_PARAM *
419 keymgmt_gettable_params(
void *provctx)
421 xkey_dmsg(
D_XKEY,
"entry");
423 static OSSL_PARAM gettable[] = {
424 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
425 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
426 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
433 keymgmt_get_params(
void *keydata, OSSL_PARAM *params)
435 xkey_dmsg(
D_XKEY,
"entry");
437 XKEY_KEYDATA *
key = keydata;
443 return EVP_PKEY_get_params(
key->pubkey, params);
450 keymgmt_import_helper(XKEY_KEYDATA *
key,
const OSSL_PARAM *params)
452 xkey_dmsg(
D_XKEY,
"entry");
455 EVP_PKEY *pkey = NULL;
474 p = OSSL_PARAM_locate_const(params,
"pubkey");
475 if (p && p->data_type == OSSL_PARAM_OCTET_STRING
476 && p->data_size ==
sizeof(pkey))
478 pkey = *(EVP_PKEY **)p->data;
481 int id = EVP_PKEY_get_id(pkey);
482 if (
id != EVP_PKEY_RSA &&
id != EVP_PKEY_EC &&
id != EVP_PKEY_ED25519 &&
id != EVP_PKEY_ED448)
484 msg(
M_WARN,
"Error: xkey keymgmt_import: unknown key type (%d)",
id);
488 key->pubkey = EVP_PKEY_dup(pkey);
489 if (
key->pubkey == NULL)
491 msg(
M_NONFATAL,
"Error: xkey keymgmt_import: duplicating pubkey failed.");
496 p = OSSL_PARAM_locate_const(params,
"handle");
497 if (p && p->data_type == OSSL_PARAM_OCTET_PTR
498 && p->data_size ==
sizeof(
key->handle))
500 key->handle = *(
void **)p->data;
505 p = OSSL_PARAM_locate_const(params,
"sign_op");
506 if (p && p->data_type == OSSL_PARAM_OCTET_PTR
507 && p->data_size ==
sizeof(
key->sign))
509 key->sign = *(
void **)p->data;
514 p = OSSL_PARAM_locate_const(params,
"free_op");
515 if (p && p->data_type == OSSL_PARAM_OCTET_PTR
516 && p->data_size ==
sizeof(
key->free))
518 key->free = *(
void **)p->data;
520 xkey_dmsg(
D_XKEY,
"imported external %s key", EVP_PKEY_get0_type_name(
key->pubkey));
537 keymgmt_set_params(
void *keydata,
const OSSL_PARAM *params)
539 XKEY_KEYDATA *
key = keydata;
542 xkey_dmsg(
D_XKEY,
"entry");
544 if (
key->origin != OPENSSL_NATIVE)
546 return keymgmt_import_helper(
key, params);
548 else if (
key->handle == NULL)
551 return EVP_PKEY_set_params(
key->pubkey, (OSSL_PARAM *)params);
555 msg(
M_WARN,
"xkey keymgmt_set_params: key is immutable");
561 rsa_keymgmt_name(
int id)
563 xkey_dmsg(
D_XKEY,
"entry");
569 ec_keymgmt_name(
int id)
571 xkey_dmsg(
D_XKEY,
"entry");
573 if (
id == OSSL_OP_SIGNATURE)
580 else if (
id == OSSL_OP_KEYEXCH)
585 msg(
D_XKEY,
"xkey ec_keymgmt_name called with op_id != SIGNATURE or KEYEXCH id=%d",
id);
589 static const OSSL_DISPATCH rsa_keymgmt_functions[] = {
590 {OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))keymgmt_new},
591 {OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))keymgmt_free},
592 {OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))keymgmt_load},
593 {OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))keymgmt_has},
594 {OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))keymgmt_match},
595 {OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_keymgmt_import},
596 {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))keymgmt_import_types},
597 {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
598 {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))keymgmt_get_params},
599 {OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))keymgmt_set_params},
600 {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
601 {OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void (*)(void))rsa_keymgmt_name},
605 static const OSSL_DISPATCH ec_keymgmt_functions[] = {
606 {OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))keymgmt_new},
607 {OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))keymgmt_free},
608 {OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))keymgmt_load},
609 {OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))keymgmt_has},
610 {OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))keymgmt_match},
611 {OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_keymgmt_import},
612 {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))keymgmt_import_types},
613 {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
614 {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))keymgmt_get_params},
615 {OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))keymgmt_set_params},
616 {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
617 {OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void (*)(void))ec_keymgmt_name},
621 static const OSSL_DISPATCH ed448_keymgmt_functions[] = {
622 {OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))keymgmt_new},
623 {OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))keymgmt_free},
624 {OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))keymgmt_load},
625 {OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))keymgmt_has},
626 {OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))keymgmt_match},
627 {OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ed448_keymgmt_import},
628 {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))keymgmt_import_types},
629 {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
630 {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))keymgmt_get_params},
631 {OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))keymgmt_set_params},
632 {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
636 static const OSSL_DISPATCH ed25519_keymgmt_functions[] = {
637 {OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))keymgmt_new},
638 {OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))keymgmt_free},
639 {OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))keymgmt_load},
640 {OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))keymgmt_has},
641 {OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))keymgmt_match},
642 {OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ed25519_keymgmt_import},
643 {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))keymgmt_import_types},
644 {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
645 {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))keymgmt_get_params},
646 {OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))keymgmt_set_params},
647 {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))keymgmt_gettable_params},
652 const OSSL_ALGORITHM keymgmts[] = {
653 {
"RSA:rsaEncryption", XKEY_PROV_PROPS, rsa_keymgmt_functions,
"OpenVPN xkey RSA Key Manager"},
654 {
"RSA-PSS:RSASSA-PSS", XKEY_PROV_PROPS, rsa_keymgmt_functions,
"OpenVPN xkey RSA-PSS Key Manager"},
655 {
"EC:id-ecPublicKey", XKEY_PROV_PROPS, ec_keymgmt_functions,
"OpenVPN xkey EC Key Manager"},
656 {
"ED448", XKEY_PROV_PROPS, ed448_keymgmt_functions,
"OpenVPN xkey ED448 Key Manager"},
657 {
"ED25519", XKEY_PROV_PROPS, ed25519_keymgmt_functions,
"OpenVPN xkey ED25519 Key Manager"},
658 {NULL, NULL, NULL, NULL}
665 static OSSL_FUNC_signature_newctx_fn signature_newctx;
666 static OSSL_FUNC_signature_freectx_fn signature_freectx;
667 static OSSL_FUNC_signature_sign_init_fn signature_sign_init;
668 static OSSL_FUNC_signature_sign_fn signature_sign;
669 static OSSL_FUNC_signature_digest_verify_init_fn signature_digest_verify_init;
670 static OSSL_FUNC_signature_digest_verify_fn signature_digest_verify;
671 static OSSL_FUNC_signature_digest_sign_init_fn signature_digest_sign_init;
672 static OSSL_FUNC_signature_digest_sign_fn signature_digest_sign;
673 static OSSL_FUNC_signature_set_ctx_params_fn signature_set_ctx_params;
674 static OSSL_FUNC_signature_settable_ctx_params_fn signature_settable_ctx_params;
675 static OSSL_FUNC_signature_get_ctx_params_fn signature_get_ctx_params;
676 static OSSL_FUNC_signature_gettable_ctx_params_fn signature_gettable_ctx_params;
680 XKEY_PROVIDER_CTX *prov;
681 XKEY_KEYDATA *keydata;
683 } XKEY_SIGNATURE_CTX;
685 static const XKEY_SIGALG default_sigalg = { .mdname =
"MD5-SHA1", .saltlen =
"digest",
686 .padmode =
"pkcs1", .keytype =
"RSA"};
691 } digest_names[] = {{NID_md5_sha1,
"MD5-SHA1"}, {NID_sha1,
"SHA1"},
692 {NID_sha224,
"SHA224", }, {NID_sha256,
"SHA256"}, {NID_sha384,
"SHA384"},
693 {NID_sha512,
"SHA512"}, {0, NULL}};
702 } padmode_names[] = {{RSA_PKCS1_PADDING,
"pkcs1"},
703 {RSA_PKCS1_PSS_PADDING,
"pss"},
704 {RSA_NO_PADDING,
"none"},
707 static const char *saltlen_names[] = {
"digest",
"max",
"auto", NULL};
713 xkey_mdname(
const char *name)
722 int nid = EVP_MD_get_type(EVP_get_digestbyname(name));
724 while (digest_names[i].name && nid != digest_names[i].nid)
728 return digest_names[i].name ? digest_names[i].name :
"MD5-SHA1";
732 signature_newctx(
void *provctx,
const char *propq)
734 xkey_dmsg(
D_XKEY,
"entry");
738 XKEY_SIGNATURE_CTX *sctx = OPENSSL_zalloc(
sizeof(*sctx));
745 sctx->prov = provctx;
746 sctx->sigalg = default_sigalg;
752 signature_freectx(
void *ctx)
754 xkey_dmsg(
D_XKEY,
"entry");
756 XKEY_SIGNATURE_CTX *sctx = ctx;
758 keydata_free(sctx->keydata);
763 static const OSSL_PARAM *
764 signature_settable_ctx_params(
void *ctx,
void *provctx)
766 xkey_dmsg(
D_XKEY,
"entry");
768 static OSSL_PARAM settable[] = {
769 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
770 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
771 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
779 signature_set_ctx_params(
void *ctx,
const OSSL_PARAM params[])
781 xkey_dmsg(
D_XKEY,
"entry");
783 XKEY_SIGNATURE_CTX *sctx = ctx;
790 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
791 if (p && p->data_type == OSSL_PARAM_UTF8_STRING)
793 sctx->sigalg.padmode = NULL;
794 for (
int i = 0; padmode_names[i].id != 0; i++)
796 if (!strcmp(p->data, padmode_names[i].name))
798 sctx->sigalg.padmode = padmode_names[i].name;
802 if (sctx->sigalg.padmode == NULL)
804 msg(
M_WARN,
"xkey signature_ctx: padmode <%s>, treating as <none>",
806 sctx->sigalg.padmode =
"none";
808 xkey_dmsg(
D_XKEY,
"setting padmode as %s", sctx->sigalg.padmode);
810 else if (p && p->data_type == OSSL_PARAM_INTEGER)
812 sctx->sigalg.padmode = NULL;
814 if (OSSL_PARAM_get_int(p, &padmode))
816 for (
int i = 0; padmode_names[i].id != 0; i++)
818 if (padmode == padmode_names[i].
id)
820 sctx->sigalg.padmode = padmode_names[i].name;
825 if (padmode == 0 || sctx->sigalg.padmode == NULL)
827 msg(
M_WARN,
"xkey signature_ctx: padmode <%d>, treating as <none>", padmode);
828 sctx->sigalg.padmode =
"none";
830 xkey_dmsg(
D_XKEY,
"setting padmode <%s>", sctx->sigalg.padmode);
834 msg(
M_WARN,
"xkey_signature_params: unknown padmode ignored");
837 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
838 if (p && p->data_type == OSSL_PARAM_UTF8_STRING)
840 sctx->sigalg.mdname = xkey_mdname(p->data);
841 xkey_dmsg(
D_XKEY,
"setting hashalg as %s", sctx->sigalg.mdname);
845 msg(
M_WARN,
"xkey_signature_params: unknown digest type ignored");
848 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
849 if (p && p->data_type == OSSL_PARAM_UTF8_STRING)
851 sctx->sigalg.saltlen = NULL;
852 for (
int i = 0; saltlen_names[i] != NULL; i++)
854 if (!strcmp(p->data, saltlen_names[i]))
856 sctx->sigalg.saltlen = saltlen_names[i];
860 if (sctx->sigalg.saltlen == NULL)
862 msg(
M_WARN,
"xkey_signature_params: unknown saltlen <%s>",
864 sctx->sigalg.saltlen =
"digest";
866 xkey_dmsg(
D_XKEY,
"setting saltlen to %s", sctx->sigalg.saltlen);
870 msg(
M_WARN,
"xkey_signature_params: unknown saltlen ignored");
876 static const OSSL_PARAM *
877 signature_gettable_ctx_params(
void *ctx,
void *provctx)
879 xkey_dmsg(
D_XKEY,
"entry");
881 static OSSL_PARAM gettable[] = { OSSL_PARAM_END };
887 signature_get_ctx_params(
void *ctx, OSSL_PARAM params[])
889 xkey_dmsg(
D_XKEY,
"not implemented");
894 signature_sign_init(
void *ctx,
void *provkey,
const OSSL_PARAM params[])
896 xkey_dmsg(
D_XKEY,
"entry");
898 XKEY_SIGNATURE_CTX *sctx = ctx;
902 keydata_free(sctx->keydata);
904 sctx->keydata = provkey;
905 sctx->keydata->refcount++;
906 sctx->sigalg.keytype = get_keytype(sctx->keydata);
908 signature_set_ctx_params(sctx, params);
915 xkey_sign_dispatch(XKEY_SIGNATURE_CTX *sctx,
unsigned char *sig,
size_t *siglen,
916 const unsigned char *tbs,
size_t tbslen)
918 XKEY_EXTERNAL_SIGN_fn *sign = sctx->keydata->sign;
921 if (sctx->keydata->origin == OPENSSL_NATIVE)
923 ret = xkey_native_sign(sctx->keydata, sig, siglen, tbs, tbslen, sctx->sigalg);
927 ret = sign(sctx->keydata->handle, sig, siglen, tbs, tbslen, sctx->sigalg);
928 xkey_dmsg(
D_XKEY,
"xkey_provider: external sign op returned ret = %d siglen = %d", ret, (
int) *siglen);
932 msg(
M_NONFATAL,
"xkey_provider: Internal error: No sign callback for external key.");
939 signature_sign(
void *ctx,
unsigned char *sig,
size_t *siglen,
size_t sigsize,
940 const unsigned char *tbs,
size_t tbslen)
942 xkey_dmsg(
D_XKEY,
"entry with siglen = %zu\n", *siglen);
944 XKEY_SIGNATURE_CTX *sctx = ctx;
950 *siglen = KEYSIZE(sctx->keydata);
954 sctx->sigalg.op =
"Sign";
955 return xkey_sign_dispatch(sctx, sig, siglen, tbs, tbslen);
959 signature_digest_verify_init(
void *ctx,
const char *mdname,
void *provkey,
960 const OSSL_PARAM params[])
962 xkey_dmsg(
D_XKEY,
"mdname <%s>", mdname);
964 msg(
M_WARN,
"xkey_provider: DigestVerifyInit is not implemented");
973 signature_digest_verify(
void *ctx,
const unsigned char *sig,
size_t siglen,
974 const unsigned char *tbs,
size_t tbslen)
976 xkey_dmsg(
D_XKEY,
"entry");
978 msg(
M_WARN,
"xkey_provider: DigestVerify is not implemented");
983 signature_digest_sign_init(
void *ctx,
const char *mdname,
984 void *provkey,
const OSSL_PARAM params[])
986 xkey_dmsg(
D_XKEY,
"mdname = <%s>", mdname);
988 XKEY_SIGNATURE_CTX *sctx = ctx;
996 keydata_free(sctx->keydata);
998 sctx->keydata = provkey;
999 sctx->keydata->refcount++;
1000 sctx->sigalg.keytype = get_keytype(sctx->keydata);
1002 signature_set_ctx_params(ctx, params);
1003 if (!strcmp(sctx->sigalg.keytype,
"ED448") || !strcmp(sctx->sigalg.keytype,
"ED25519"))
1009 msg(
M_WARN,
"xkey digest_sign_init: mdname must be NULL for ED448/ED25519.");
1012 sctx->sigalg.mdname =
"none";
1016 sctx->sigalg.mdname = xkey_mdname(mdname);
1020 msg(
M_WARN,
"xkey digest_sign_init: mdname is NULL.");
1026 signature_digest_sign(
void *ctx,
unsigned char *sig,
size_t *siglen,
1027 size_t sigsize,
const unsigned char *tbs,
size_t tbslen)
1029 xkey_dmsg(
D_XKEY,
"entry");
1031 XKEY_SIGNATURE_CTX *sctx = ctx;
1038 *siglen = KEYSIZE(sctx->keydata);
1042 if (sctx->keydata->origin != OPENSSL_NATIVE)
1045 sctx->sigalg.op =
"DigestSign";
1046 return xkey_sign_dispatch(ctx, sig, siglen, tbs, tbslen);
1051 const char *mdname = sctx->sigalg.mdname;
1052 EVP_MD *md =
EVP_MD_fetch(sctx->prov->libctx, mdname, NULL);
1055 msg(
M_WARN,
"WARN: xkey digest_sign_init: MD_fetch failed for <%s>", mdname);
1060 unsigned char buf[EVP_MAX_MD_SIZE];
1062 if (EVP_Digest(tbs, tbslen, buf, &sz, md, NULL) != 1)
1064 msg(
M_WARN,
"WARN: xkey digest_sign: EVP_Digest failed");
1070 return signature_sign(ctx, sig, siglen, sigsize, buf, sz);
1076 xkey_native_sign(XKEY_KEYDATA *
key,
unsigned char *sig,
size_t *siglen,
1077 const unsigned char *tbs,
size_t tbslen, XKEY_SIGALG sigalg)
1079 xkey_dmsg(
D_XKEY,
"entry");
1083 EVP_PKEY *pkey =
key->handle;
1090 msg(
M_NONFATAL,
"Error: xkey provider: signature request with empty private key");
1094 const char *saltlen = sigalg.saltlen;
1095 const char *mdname = sigalg.mdname;
1096 const char *padmode = sigalg.padmode;
1098 xkey_dmsg(
D_XKEY,
"digest=<%s>, padmode=<%s>, saltlen=<%s>", mdname, padmode, saltlen);
1101 OSSL_PARAM params[6];
1102 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_RSA)
1104 params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, (
char *)mdname, 0);
1105 params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, (
char *)padmode, 0);
1106 if (!strcmp(sigalg.padmode,
"pss"))
1108 params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, (
char *) saltlen, 0);
1110 params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, (
char *) mdname, 0);
1113 params[i++] = OSSL_PARAM_construct_end();
1115 EVP_PKEY_CTX *ectx = EVP_PKEY_CTX_new_from_pkey(
key->prov->libctx, pkey, NULL);
1119 msg(
M_WARN,
"WARN: xkey test_sign: call to EVP_PKEY_CTX_new...failed");
1123 if (EVP_PKEY_sign_init_ex(ectx, NULL) != 1)
1125 msg(
M_WARN,
"WARN: xkey test_sign: call to EVP_PKEY_sign_init failed");
1128 EVP_PKEY_CTX_set_params(ectx, params);
1130 ret = EVP_PKEY_sign(ectx, sig, siglen, tbs, tbslen);
1131 EVP_PKEY_CTX_free(ectx);
1136 static const OSSL_DISPATCH signature_functions[] = {
1137 {OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))signature_newctx},
1138 {OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))signature_freectx},
1139 {OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))signature_sign_init},
1140 {OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))signature_sign},
1141 {OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void (*)(void))signature_digest_verify_init},
1142 {OSSL_FUNC_SIGNATURE_DIGEST_VERIFY, (void (*)(void))signature_digest_verify},
1143 {OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void (*)(void))signature_digest_sign_init},
1144 {OSSL_FUNC_SIGNATURE_DIGEST_SIGN, (void (*)(void))signature_digest_sign},
1145 {OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))signature_set_ctx_params},
1146 {OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, (void (*)(void))signature_settable_ctx_params},
1147 {OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))signature_get_ctx_params},
1148 {OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, (void (*)(void))signature_gettable_ctx_params},
1152 const OSSL_ALGORITHM signatures[] = {
1153 {
"RSA:rsaEncryption", XKEY_PROV_PROPS, signature_functions,
"OpenVPN xkey RSA Signature"},
1154 {
"ECDSA", XKEY_PROV_PROPS, signature_functions,
"OpenVPN xkey ECDSA Signature"},
1155 {
"ED448", XKEY_PROV_PROPS, signature_functions,
"OpenVPN xkey Ed448 Signature"},
1156 {
"ED25519", XKEY_PROV_PROPS, signature_functions,
"OpenVPN xkey Ed25519 Signature"},
1157 {NULL, NULL, NULL, NULL}
1163 static OSSL_FUNC_provider_query_operation_fn query_operation;
1164 static OSSL_FUNC_provider_gettable_params_fn gettable_params;
1165 static OSSL_FUNC_provider_get_params_fn get_params;
1166 static OSSL_FUNC_provider_teardown_fn
teardown;
1168 static const OSSL_ALGORITHM *
1169 query_operation(
void *provctx,
int op,
int *no_store)
1171 xkey_dmsg(
D_XKEY,
"op = %d", op);
1177 case OSSL_OP_SIGNATURE:
1180 case OSSL_OP_KEYMGMT:
1184 xkey_dmsg(
D_XKEY,
"op not supported");
1190 static const OSSL_PARAM *
1191 gettable_params(
void *provctx)
1193 xkey_dmsg(
D_XKEY,
"entry");
1195 static const OSSL_PARAM param_types[] = {
1196 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
1203 get_params(
void *provctx, OSSL_PARAM params[])
1207 xkey_dmsg(
D_XKEY,
"entry");
1209 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
1212 return (OSSL_PARAM_set_utf8_ptr(p, provname) != 0);
1221 xkey_dmsg(
D_XKEY,
"entry");
1223 XKEY_PROVIDER_CTX *prov = provctx;
1224 if (prov && prov->libctx)
1226 OSSL_LIB_CTX_free(prov->libctx);
1231 static const OSSL_DISPATCH dispatch_table[] = {
1232 {OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))gettable_params},
1233 {OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))get_params},
1234 {OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))query_operation},
1235 {OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))
teardown},
1240 xkey_provider_init(
const OSSL_CORE_HANDLE *handle,
const OSSL_DISPATCH *in,
1241 const OSSL_DISPATCH **out,
void **provctx)
1243 XKEY_PROVIDER_CTX *prov;
1245 xkey_dmsg(
D_XKEY,
"entry");
1247 prov = OPENSSL_zalloc(
sizeof(*prov));
1257 prov->libctx = OSSL_LIB_CTX_new_child(handle, in);
1259 EVP_set_default_properties(prov->libctx,
"provider!=ovpn.xkey");
1261 *out = dispatch_table;