Implement ED448 and ED25519 support in xkey_provider
PATCH 1/1 Implement ED448 and ED25519 support in xkey_provider
OpenSSL's implementation of ED448 and ED25519 has a few idiosyncrasies. Instead of belonging to the eliptic curve type or to a common Edwards curve type, ED448 and ED25519 have each their own type.
Also, OpenSSL excepts singatures using these curves to be done with the EVP_DigestSign API instead of the EVP_Sign API but using md=NULL.
This has been tested using a "fake" external key that used a normal software key instead of a hardware implementation but that makes no difference from the perspective of xkey_provider/managment interface.
--- src/openvpn/xkey_helper.c | 4 + src/openvpn/xkey_provider.c | 121 +++++++++++++++++++++-- tests/unit_tests/openvpn/test_provider.c | 21 ++-- 3 files changed, 134 insertions(+), 12 deletions(-)
src/openvpn/xkey_helper.c +4 -0
@@ -206,6 +206,10 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,openvpn_snprintf (alg_str,sizeof (alg_str),"ECDSA,hashalg=%s" , alg.mdname); } } +else if (!strcmp (alg.keytype,"ED448" ) || !strcmp (alg.keytype,"ED25519" )) + { +strncpynt (alg_str, alg.keytype,sizeof (alg_str)); + }/* else assume RSA key */ else if (!strcmp (alg.padmode,"pkcs1" ) && (flags &MF_EXTERNAL_KEY_PKCS1PAD )) {
src/openvpn/xkey_provider.c +115 -6
182 lines not on this page · raw message
tests/unit_tests/openvpn/test_provider.c +15 -6
@@ -66,7 +66,11 @@ static const char *const pubkey2 = "-----BEGIN PUBLIC KEY-----\n""u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n" "-----END PUBLIC KEY-----\n" ; -static const char *pubkeys[] = {pubkey1, pubkey2}; +static const char *const pubkey3 ="-----BEGIN PUBLIC KEY-----\n" +"MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n" +"-----END PUBLIC KEY-----" ; + +static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};static const char *prov_name ="ovpn.xkey" ;
@@ -158,12 +162,17 @@ management_query_pk_sig(struct management *man, const char *b64_data,if (strstr (algorithm,"data=message" )) { expected_tbs = test_msg_b64; -assert_non_null (strstr (algorithm,"hashalg=SHA256" )); +/* ED25519 does not have a hash algorithm even though it goes via +* the DigestSign path (data=message) */ +if (!strstr (algorithm,"ED25519" )) + { +assert_non_null (strstr (algorithm,"hashalg=SHA256" )); + } }assert_string_equal (b64_data, expected_tbs); -/* We test using ECDSA or PSS with saltlen = digest */ -if (!strstr (algorithm,"ECDSA" )) +/* We test using ED25519, ECDSA or PSS with saltlen = digest */ +if (!strstr (algorithm,"ECDSA" ) && !strstr (algorithm,"ED25519" )) {assert_non_null (strstr (algorithm,"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest" )); }
@@ -328,13 +337,13 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen,assert_memory_equal (tbs, test_digest,sizeof (test_digest)); } -/* For the test use sha256 and PSS padding for RSA */ +/* For the test use sha256 and PSS padding for RSA and none for EDDSA */ assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256);if (!strcmp (s.keytype,"RSA" )) {assert_string_equal (s.padmode,"pss" );/* we use PSS for the test */ } -else if (strcmp (s.keytype,"EC" )) +else if (strcmp (s.keytype,"EC" ) &&strcmp (s.keytype,"ED25519" )) {fail_msg ("Unknown keytype: %s" , s.keytype); }
Signature · 1 line
2.32.0 (Apple Git-132)
Hi,
Quoted 6 lines · Arne Schwabe
OpenSSL's implementation of ED448 and ED25519 has a few idiosyncrasies. Instead of belonging to the eliptic curve type or to a common Edwards curve type, ED448 and ED25519 have each their own type.
Also, OpenSSL excepts singatures using these curves to be done with the
expects
signatures
(good to know I'm not the only one afflicted by transposition errors)
Quoted 1 line
EVP_DigestSignAPI instead of theEVP_SignAPI but using md=NULL.
This has been tested using a "fake" external key that used a normal
Quoted 3 lines
software key instead of a hardware implementation but that makes no difference from the perspective of xkey_provider/managment interface.
management
Quoted 44 lines
--- src/openvpn/xkey_helper.c | 4 + src/openvpn/xkey_provider.c | 121 +++++++++++++++++++++-- tests/unit_tests/openvpn/test_provider.c | 21 ++-- 3 files changed, 134 insertions(+), 12 deletions(-)src/openvpn/xkey_helper.c +4
-0@@ -206,6 +206,10 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,openvpn_snprintf (alg_str,sizeof (alg_str),"ECDSA,hashalg=%s" , alg.mdname); } } +else if (!strcmp (alg.keytype,"ED448" ) || !strcmp (alg.keytype,"ED25519" )) + { +strncpynt (alg_str, alg.keytype,sizeof (alg_str)); + }/* else assume RSA key */ else if (!strcmp(alg.padmode, "pkcs1") && (flags & MF_EXTERNAL_KEY_PKCS1PAD)) {src/openvpn/xkey_provider.c +7
-3@@ -99,12 +99,28 @@ typedef structint refcount;/**< reference count */ }XKEY_KEYDATA ; -static int -KEYTYPE (const XKEY_KEYDATA *key) +static inline const char * +get_keytype (const XKEY_KEYDATA *key) { -return key->pubkey ?EVP_PKEY_get_id (key->pubkey) :0 ; +int keytype = key->pubkey ?EVP_PKEY_get_id (key->pubkey) :0 ; + +switch (keytype) + { +case EVP_PKEY_RSA :
src/openvpn/xkey_provider.c typedef struct · line 119
Quoted 10 lines
src/openvpn/xkey_provider.c +10
-0+return "RSA" ; + +case EVP_PKEY_ED448 : +return "ED448" ; + +case EVP_PKEY_ED25519 : +return "ED25519" ; + +default : +return "EC" ;
+ }
Quoted 49 lines
src/openvpn/xkey_provider.c +17
-0} +static int KEYSIZE (const XKEY_KEYDATA *key) {@@ -310,6 +326,22 @@ ec_keymgmt_import(void *keydata, int selection, const OSSL_PARAM params[])return keymgmt_import (keydata, selection, params,"EC" ); } +static int +ed448_keymgmt_import (void *keydata,int selection,const OSSL_PARAM params[]) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +return keymgmt_import (keydata, selection, params,"ED448" ); +} + +static int +ed25519_keymgmt_import (void *keydata,int selection,const OSSL_PARAM params[]) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +return keymgmt_import (keydata, selection, params,"ED25519" ); +} +/* This function has to exist for key import to work
* though we do not support import of individual params
* like n or e. We simply return an empty list here forsrc/openvpn/xkey_provider.c +1
-1@@ -449,7 +481,7 @@ keymgmt_import_helper(XKEY_KEYDATA *key, const OSSL_PARAM *params)ASSERT (pkey );int id =EVP_PKEY_get_id (pkey); -if (id !=EVP_PKEY_RSA && id !=EVP_PKEY_EC ) +if (id !=EVP_PKEY_RSA && id !=EVP_PKEY_EC && id !=EVP_PKEY_ED25519 && id !=EVP_PKEY_ED448 ) {msg(M_WARN, "Error: xkey
keymgmt_import: unknown key type(%d)", id); return 0;src/openvpn/xkey_provider.c +1
-0@@ -556,6 +588,40 @@ ec_keymgmt_name(int id)return "EC" ; } +static const char *
src/openvpn/xkey_provider.c ec_keymgmt_name(int id) · line 601
Quoted 10 lines
src/openvpn/xkey_provider.c +10
-0+ed448_keymgmt_name(int id) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +/* though we do not implement keyexch we could be queried for +* keyexch mechanism supported by EC keys +*/ +if (id ==OSSL_OP_SIGNATURE || id ==OSSL_OP_KEYEXCH ) + { +return "ED448" ;
+ }
src/openvpn/xkey_provider.c ec_keymgmt_name(int id) · line 606
Quoted 5 lines
src/openvpn/xkey_provider.c +3
-0+ + msg(D_XKEY, "xkey ed448_keymgmt_name called with op_id != SIGNATURE or KEYEXCH id=%d", id); + return "EC";
I think this should be either "ED448" or NULL. The default provider does not define the query_operation function for this key type in the dispatch table --- likely because the name will then default to the keymgmt name (ED448). Also, I just realized that in the default provider, the query_operation_name (when defined) returns NULL for ids not recognized. That leads to triggering an error message in the library which is good.
We could either return NULL here or "ED448" and then convert the above debug msg() to a warning. Or leave out this function altogether and remove it from the dispatch table.
+}
src/openvpn/xkey_provider.c ec_keymgmt_name(int id) · line 619
Quoted 13 lines
src/openvpn/xkey_provider.c +13
-0+ +static const char * +ed25519_keymgmt_name (int id) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +/* though we do not implement keyexch we could be queried for +* keyexch mechanism supported by EC keys +*/ +if (id ==OSSL_OP_SIGNATURE || id ==OSSL_OP_KEYEXCH ) + { +return "ED25519" ; + }
+
src/openvpn/xkey_provider.c ec_keymgmt_name(int id) · line 622
Quoted 4 lines
src/openvpn/xkey_provider.c +2
-0+ msg(D_XKEY, "xkey ed448_keymgmt_name called with op_id != SIGNATURE or KEYEXCH id=%d", id); + return "EC";
same comment as above with ED448 --> ED25519.
Quoted 23 lines
src/openvpn/xkey_provider.c +14
-0+} +static const OSSL_DISPATCH rsa_keymgmt_functions[] = { {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free},@@ -588,10 +654,45 @@ static const OSSL_DISPATCH ec_keymgmt_functions[] = { {0, NULL } }; +static const OSSL_DISPATCH ed448_keymgmt_functions[] = { + {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, + {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free}, + {OSSL_FUNC_KEYMGMT_LOAD , (void (*)(void ))keymgmt_load}, + {OSSL_FUNC_KEYMGMT_HAS , (void (*)(void ))keymgmt_has}, + {OSSL_FUNC_KEYMGMT_MATCH , (void (*)(void ))keymgmt_match}, + {OSSL_FUNC_KEYMGMT_IMPORT , (void (*)(void ))ed448_keymgmt_import}, + {OSSL_FUNC_KEYMGMT_IMPORT_TYPES , (void (*)(void ))keymgmt_import_types}, + {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params}, + {OSSL_FUNC_KEYMGMT_GET_PARAMS , (void (*)(void ))keymgmt_get_params}, + {OSSL_FUNC_KEYMGMT_SET_PARAMS , (void (*)(void ))keymgmt_set_params}, + {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void
Quoted 4 lines
(*)(void))keymgmt_gettable_params}, /* same as gettable */
+ {OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void
(*)(void))ed448_keymgmt_name},
see above
Quoted 71 lines
src/openvpn/xkey_provider.c +11
-0+ {0, NULL } +}; + +static const OSSL_DISPATCH ed25519_keymgmt_functions[] = { + {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, + {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free}, + {OSSL_FUNC_KEYMGMT_LOAD , (void (*)(void ))keymgmt_load}, + {OSSL_FUNC_KEYMGMT_HAS , (void (*)(void ))keymgmt_has}, + {OSSL_FUNC_KEYMGMT_MATCH , (void (*)(void ))keymgmt_match}, + {OSSL_FUNC_KEYMGMT_IMPORT , (void (*)(void ))ed25519_keymgmt_import}, + {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void(*)(void))keymgmt_import_types},
+ {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void
(*)(void))keymgmt_gettable_params},src/openvpn/xkey_provider.c +3
-0+ {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))keymgmt_get_params}, + {OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))keymgmt_set_params}, + {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void(*)(void))keymgmt_gettable_params}, /* same as gettable */
+ {OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void
(*)(void))ed25519_keymgmt_name},src/openvpn/xkey_provider.c +4
-0+ {0, NULL } +}; + +const OSSL_ALGORITHM keymgmts[] = { {"RSA:rsaEncryption" ,XKEY_PROV_PROPS , rsa_keymgmt_functions,"OpenVPN xkey RSA Key Manager"},
{"RSA-PSS:RSASSA-PSS",XKEY_PROV_PROPS,rsa_keymgmt_functions,
"OpenVPN xkey RSA-PSS Key Manager"},
{"EC:id-ecPublicKey",XKEY_PROV_PROPS,ec_keymgmt_functions, "OpenVPN
xkey EC Key Manager"},
+ {"ED448",XKEY_PROV_PROPS,ed448_keymgmt_functions, "OpenVPN xkey
ED448 Key Manager"},
+ {"ED25519",XKEY_PROV_PROPS,ed25519_keymgmt_functions, "OpenVPN xkey
ED25519 Key Manager"},{NULL, NULL, NULL, NULL}};
src/openvpn/xkey_provider.c +1
-1@@ -835,7 +936,7 @@ signature_sign_init(void *ctx, void *provkey, const OSSL_PARAM params[]) } sctx->keydata = provkey; sctx->keydata->refcount++;/* we are keeping a copy */ - sctx->sigalg.keytype =KEYTYPE (sctx->keydata) ==EVP_PKEY_RSA ?"RSA" : "EC"; + sctx->sigalg.keytype =get_keytype (sctx->keydata);signature_set_ctx_params(sctx, params);src/openvpn/xkey_provider.c +1
-1@@ -929,13 +1030,19 @@ signature_digest_sign_init(void *ctx, const char *mdname, } sctx->keydata = provkey;/* used by digest_sign */ sctx->keydata->refcount++; - sctx->sigalg.keytype =KEYTYPE (sctx->keydata) ==EVP_PKEY_RSA ?"RSA" : "EC"; + sctx->sigalg.keytype =get_keytype (sctx->keydata);signature_set_ctx_params (ctx, params);if (mdname) { sctx->sigalg.mdname =xkey_mdname (mdname);/* get a string literal pointer */
}+ else if (!strcmp(sctx->sigalg.keytype, "ED448") ||
!strcmp(sctx->sigalg.keytype, "ED25519"))
src/openvpn/xkey_provider.c
Quoted 5 lines
src/openvpn/xkey_provider.c +5
-0+ { +/* EdDSA requires NULL as digest for the DigestSign API instead +* of using the normal Sign API */ + sctx->sigalg.mdname ="none" ; + }
Instead of this order, we have to assert that mdname must be NULL if keytype is ED25519 or ED448. OpenSSL does not impose this when the key is in provider, but docs for EVP_DigestSignInit() says mdname should be NULL for such keys. So I think we should return an error if mdname is not NULL.
In fact we do call EVP_DigestSignInit() in our unit test with mdname=SHA256 and Ed25519 key (after this patch) which exposes this issue (see below).
Quoted 38 lines
else { msg(M_WARN, "xkey digest_sign_init: mdname is NULL.");src/openvpn/xkey_provider.c +2
-0@@ -1073,6 +1180,8 @@ static const OSSL_DISPATCH signature_functions[] = { const OSSL_ALGORITHM signatures[] = { {"RSA:rsaEncryption", XKEY_PROV_PROPS, signature_functions, "OpenVPN xkey RSA Signature"}, {"ECDSA", XKEY_PROV_PROPS, signature_functions, "OpenVPN xkey ECDSA Signature"}, + {"ED448", XKEY_PROV_PROPS, signature_functions, "OpenVPN xkey Ed448 Signature"}, + {"ED25519", XKEY_PROV_PROPS, signature_functions, "OpenVPN xkeyEd25519 Signature"},
{NULL, NULL, NULL, NULL}};
tests/unit_tests/openvpn/test_provider.c +0
-0@@ -66,7 +66,11 @@ static const char *const pubkey2 = "-----BEGIN PUBLICKEY-----\n"
"u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n"
"-----END PUBLIC KEY-----\n";tests/unit_tests/openvpn/test_provider.c +5
-1-static const char *pubkeys[] = {pubkey1, pubkey2}; +static const char *const pubkey3 ="-----BEGIN PUBLIC KEY-----\n" +"MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n" +"-----END PUBLIC KEY-----" ; + +static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};static const char *prov_name ="ovpn.xkey" ;
tests/unit_tests/openvpn/test_provider.c management_query_pk_sig(struct management *man, · line 166
Quoted 6 lines
tests/unit_tests/openvpn/test_provider.c +1
-1@@ -158,12 +162,17 @@ management_query_pk_sig(struct management *man,const char *b64_data, if (strstr(algorithm, "data=message")) { expected_tbs = test_msg_b64; -assert_non_null (strstr (algorithm,"hashalg=SHA256" )); +/* ED25519 does not have a hash algorithm even though it goes via
+ * the DigestSign path (data=message) */
tests/unit_tests/openvpn/test_provider.c management_query_pk_sig(struct management *man, · line 170
Quoted 4 lines
tests/unit_tests/openvpn/test_provider.c +4
-0+if (!strstr (algorithm,"ED25519" )) + { +assert_non_null (strstr (algorithm,"hashalg=SHA256" )); + }
}
Quoted 12 lines
tests/unit_tests/openvpn/test_provider.c +2
-2assert_string_equal(b64_data, expected_tbs); Though it still uses digestSign() -/* We test using ECDSA or PSS with saltlen = digest */ - if (!strstr(algorithm, "ECDSA")) + /* We test using ED25519, ECDSA or PSS with saltlen = digest */ + if (!strstr(algorithm, "ECDSA") && !strstr(algorithm, "ED25519")) { assert_non_null(strstr(algorithm,"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest")); }
tests/unit_tests/openvpn/test_provider.c xkey_sign(void *handle, unsigned char *sig, size_t · line 344
Quoted 9 lines
tests/unit_tests/openvpn/test_provider.c +1
-1@@ -328,13 +337,13 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen,assert_memory_equal (tbs, test_digest,sizeof (test_digest)); } -/* For the test use sha256 and PSS padding for RSA */ +/* For the test use sha256 and PSS padding for RSA and none for EDDSA */ assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256);
This works now because we wrongly call Ed25519 signature with mdname=sha256 in the test. I think we want this to be something like
if (!strcmp(s.keytype, "ED25519"))
{
assert_string_equal(s.mdname, "none");
}
else
{
assert_int_equal(OBJ_sn2nid(s.mdname), NID_sha256);
}And, in digest_sign() we want to set mdname=NULL for Ed25519 key.. Like:
(quoted diff) +0 -0
b/tests/unit_tests/openvpn/test_provider.c
index e2925283..aa2ac7b9 100644
tests/unit_tests/openvpn/test_provider.c +0 -0
@@ -237,6 +237,10 @@ digest_sign(EVP_PKEY *pkey) params[3] =
OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, (char
*)saltlen, 0);
params[4] = OSSL_PARAM_construct_end();
}+ else if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED25519)
+ {
+ mdname = NULL;
+ }
if (!strcmp(s.keytype, "RSA"))
Quoted 10 lines
tests/unit_tests/openvpn/test_provider.c +1
-1{assert_string_equal (s.padmode,"pss" );/* we use PSS for the test */ } - else if (strcmp(s.keytype, "EC")) + else if (strcmp(s.keytype, "EC") && strcmp(s.keytype,"ED25519")){ fail_msg("Unknown keytype: %s", s.keytype); }
Finally, xkey_management_sign() in xkey_helper.c should not call xkey_digest() if mdname is "none". So line 182 in xkey_helper.c will have to become something like:
if (!strcmp(alg.op, "DigestSign") && !(flags & MF_EXTERNAL_KEY_DIGEST) && strcmp(alg.mdname,"none"))
All look good otherwise.
Regards,
Selva
PATCH v2 1/1 Implement ED448 and ED25519 support in xkey_provider
In reply to Arne SchwabeOpenSSL's implementation of ED448 and ED25519 has a few idiosyncrasies. Instead of belonging to the eliptic curve type or to a common Edwards curve type, ED448 and ED25519 have each their own type.
Also, OpenSSL expects signatures using these curves to be done with the EVP_DigestSign API instead of the EVP_Sign API but using md=NULL.
This has been tested using a "fake" external key that used a normal software key instead of a hardware implementation but that makes no difference from the perspective of xkey_provider/management interface.
Patch v2: remove name functions from ed448/ed25519, ensure md is NULL
for ed448/ed25519 and handle NULL/none better in general.
--- src/openvpn/xkey_common.h | 2 +- src/openvpn/xkey_helper.c | 7 +- src/openvpn/xkey_provider.c | 97 ++++++++++++++++++++++-- tests/unit_tests/openvpn/test_provider.c | 37 +++++++-- 4 files changed, 127 insertions(+), 16 deletions(-)
src/openvpn/xkey_common.h +1 -1
@@ -43,7 +43,7 @@ OSSL_provider_init_fn xkey_provider_init;#define XKEY_PROV_PROPS "provider=ovpn.xkey"/** -* Stuct to encapsulate signature algorithm parameters to pass +* Struct to encapsulate signature algorithm parameters to pass * to sign operation. */ typedef struct {
src/openvpn/xkey_helper.c +6 -1
@@ -179,7 +179,8 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,bool is_message = !strcmp (alg.op,"DigestSign" );/* tbs is message, not digest */ /* if management client cannot do digest -- we do it here */ -if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST )) +if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST ) + &&strcmp (alg.mdname,"none" ) !=0 ) {dmsg (D_XKEY ,"xkey_management_sign: computing digest" );if (xkey_digest (tbs, tbslen, buf, &buflen, alg.mdname))
@@ -206,6 +207,10 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,openvpn_snprintf (alg_str,sizeof (alg_str),"ECDSA,hashalg=%s" , alg.mdname); } } +else if (!strcmp (alg.keytype,"ED448" ) || !strcmp (alg.keytype,"ED25519" )) + { +strncpynt (alg_str, alg.keytype,sizeof (alg_str)); + }/* else assume RSA key */ else if (!strcmp (alg.padmode,"pkcs1" ) && (flags &MF_EXTERNAL_KEY_PKCS1PAD )) {
src/openvpn/xkey_provider.c +90 -7
154 lines not on this page · raw message
tests/unit_tests/openvpn/test_provider.c +30 -7
@@ -66,7 +66,11 @@ static const char *const pubkey2 = "-----BEGIN PUBLIC KEY-----\n""u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n" "-----END PUBLIC KEY-----\n" ; -static const char *pubkeys[] = {pubkey1, pubkey2}; +static const char *const pubkey3 ="-----BEGIN PUBLIC KEY-----\n" +"MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n" +"-----END PUBLIC KEY-----" ; + +static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};static const char *prov_name ="ovpn.xkey" ;
@@ -158,12 +162,17 @@ management_query_pk_sig(struct management *man, const char *b64_data,if (strstr (algorithm,"data=message" )) { expected_tbs = test_msg_b64; -assert_non_null (strstr (algorithm,"hashalg=SHA256" )); +/* ED25519 does not have a hash algorithm even though it goes via +* the DigestSign path (data=message) */ +if (!strstr (algorithm,"ED25519" )) + { +assert_non_null (strstr (algorithm,"hashalg=SHA256" )); + } }assert_string_equal (b64_data, expected_tbs); -/* We test using ECDSA or PSS with saltlen = digest */ -if (!strstr (algorithm,"ECDSA" )) +/* We test using ED25519, ECDSA or PSS with saltlen = digest */ +if (!strstr (algorithm,"ECDSA" ) && !strstr (algorithm,"ED25519" )) {assert_non_null (strstr (algorithm,"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest" )); }
@@ -228,6 +237,13 @@ digest_sign(EVP_PKEY *pkey) params[3 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_MGF1_DIGEST , (char *)saltlen,0 ); params[4 ] =OSSL_PARAM_construct_end (); } +else if (EVP_PKEY_get_id (pkey) ==EVP_PKEY_ED25519 ) + { + mdname =NULL ; + params[0 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_DIGEST , (char *)mdname,0 ); + params[1 ] =OSSL_PARAM_construct_end (); + } +EVP_PKEY_CTX *pctx =NULL ;EVP_MD_CTX *mctx =EVP_MD_CTX_new ();
@@ -328,13 +344,20 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen,assert_memory_equal (tbs, test_digest,sizeof (test_digest)); } -/* For the test use sha256 and PSS padding for RSA */ -assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); +/* For the test use sha256 and PSS padding for RSA and none for EDDSA */ +if (!strcmp (s.keytype,"ED25519" )) + { +assert_string_equal (s.mdname,"none" ); + } +else + { +assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); + }if (!strcmp (s.keytype,"RSA" )) {assert_string_equal (s.padmode,"pss" );/* we use PSS for the test */ } -else if (strcmp (s.keytype,"EC" )) +else if (strcmp (s.keytype,"EC" ) &&strcmp (s.keytype,"ED25519" )) {fail_msg ("Unknown keytype: %s" , s.keytype); }
Signature · 1 line
2.32.0 (Apple Git-132)
Hi,
Thanks for the v2. I'm ready to ack this but for one issue (NULL passed to OSSL_PARAM_construct_utf8_string).
Quoted 3 lines · Arne Schwabe
OpenSSL's implementation of ED448 and ED25519 has a few idiosyncrasies. Instead of belonging to the eliptic curve type or to a common Edwards
nit: elliptic (sorry I missed this earlier).
Quoted 36 lines
curve type, ED448 and ED25519 have each their own type.
Also, OpenSSL expects signatures using these curves to be done with the
EVP_DigestSignAPI instead of theEVP_SignAPI but using md=NULL.This has been tested using a "fake" external key that used a normal software key instead of a hardware implementation but that makes no difference from the perspective of xkey_provider/management interface.
Patch v2: remove name functions from ed448/ed25519, ensure md is NULL
for ed448/ed25519 and handle NULL/none better in general.--- src/openvpn/xkey_common.h | 2 +- src/openvpn/xkey_helper.c | 7 +- src/openvpn/xkey_provider.c | 97 ++++++++++++++++++++++-- tests/unit_tests/openvpn/test_provider.c | 37 +++++++-- 4 files changed, 127 insertions(+), 16 deletions(-)src/openvpn/xkey_common.h +1
-1@@ -43,7 +43,7 @@ OSSL_provider_init_fn xkey_provider_init;#define XKEY_PROV_PROPS "provider=ovpn.xkey"/** -* Stuct to encapsulate signature algorithm parameters to pass +* Struct to encapsulate signature algorithm parameters to pass * to sign operation. */ typedef struct {src/openvpn/xkey_helper.c +0
-0
src/openvpn/xkey_helper.c xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen, · line 183
Quoted 6 lines
src/openvpn/xkey_helper.c +2
-1@@ -179,7 +179,8 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,bool is_message = !strcmp (alg.op,"DigestSign" );/* tbs is message, not digest */ /* if management client cannot do digest -- we do it here */ -if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST )) +if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST ) + &&strcmp (alg.mdname,"none" ) !=0 )
nit: Why not strcmp(alg.mdname, "none")? That's how we use it elsewhere in the patch and this file. Unless our coding style now requires this.
Quoted 219 lines
src/openvpn/xkey_helper.c +4
-0{dmsg (D_XKEY ,"xkey_management_sign: computing digest" );if (xkey_digest (tbs, tbslen, buf, &buflen, alg.mdname))@@ -206,6 +207,10 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,openvpn_snprintf (alg_str,sizeof (alg_str),"ECDSA,hashalg=%s" , alg.mdname); } } +else if (!strcmp (alg.keytype,"ED448" ) || !strcmp (alg.keytype,"ED25519" )) + { +strncpynt (alg_str, alg.keytype,sizeof (alg_str)); + }/* else assume RSA key */ else if (!strcmp (alg.padmode,"pkcs1" ) && (flags &MF_EXTERNAL_KEY_PKCS1PAD )) {src/openvpn/xkey_provider.c +90
-7@@ -99,12 +99,28 @@ typedef structint refcount;/**< reference count */ }XKEY_KEYDATA ; -static int -KEYTYPE (const XKEY_KEYDATA *key) +static inline const char * +get_keytype (const XKEY_KEYDATA *key) { -return key->pubkey ?EVP_PKEY_get_id (key->pubkey) :0 ; +int keytype = key->pubkey ?EVP_PKEY_get_id (key->pubkey) :0 ; + +switch (keytype) + { +case EVP_PKEY_RSA : +return "RSA" ; + +case EVP_PKEY_ED448 : +return "ED448" ; + +case EVP_PKEY_ED25519 : +return "ED25519" ; + +default : +return "EC" ; + } } +static int KEYSIZE (const XKEY_KEYDATA *key) {@@ -310,6 +326,22 @@ ec_keymgmt_import(void *keydata, int selection, const OSSL_PARAM params[])return keymgmt_import (keydata, selection, params,"EC" ); } +static int +ed448_keymgmt_import (void *keydata,int selection,const OSSL_PARAM params[]) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +return keymgmt_import (keydata, selection, params,"ED448" ); +} + +static int +ed25519_keymgmt_import (void *keydata,int selection,const OSSL_PARAM params[]) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +return keymgmt_import (keydata, selection, params,"ED25519" ); +} +/* This function has to exist for key import to work * though we do not support import of individual params * like n or e. We simply return an empty list here for @@ -449,7 +481,7 @@ keymgmt_import_helper(XKEY_KEYDATA *key, const OSSL_PARAM *params)ASSERT (pkey);int id =EVP_PKEY_get_id (pkey); -if (id !=EVP_PKEY_RSA && id !=EVP_PKEY_EC ) +if (id !=EVP_PKEY_RSA && id !=EVP_PKEY_EC && id !=EVP_PKEY_ED25519 && id !=EVP_PKEY_ED448 ) {msg (M_WARN ,"Error: xkey keymgmt_import: unknown key type (%d)" , id);return 0 ;@@ -588,10 +620,43 @@ static const OSSL_DISPATCH ec_keymgmt_functions[] = { {0, NULL } }; +static const OSSL_DISPATCH ed448_keymgmt_functions[] = { + {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, + {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free}, + {OSSL_FUNC_KEYMGMT_LOAD , (void (*)(void ))keymgmt_load}, + {OSSL_FUNC_KEYMGMT_HAS , (void (*)(void ))keymgmt_has}, + {OSSL_FUNC_KEYMGMT_MATCH , (void (*)(void ))keymgmt_match}, + {OSSL_FUNC_KEYMGMT_IMPORT , (void (*)(void ))ed448_keymgmt_import}, + {OSSL_FUNC_KEYMGMT_IMPORT_TYPES , (void (*)(void ))keymgmt_import_types}, + {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params}, + {OSSL_FUNC_KEYMGMT_GET_PARAMS , (void (*)(void ))keymgmt_get_params}, + {OSSL_FUNC_KEYMGMT_SET_PARAMS , (void (*)(void ))keymgmt_set_params}, + {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params},/* same as gettable */ + {0 ,NULL } +}; + +static const OSSL_DISPATCH ed25519_keymgmt_functions[] = { + {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, + {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free}, + {OSSL_FUNC_KEYMGMT_LOAD , (void (*)(void ))keymgmt_load}, + {OSSL_FUNC_KEYMGMT_HAS , (void (*)(void ))keymgmt_has}, + {OSSL_FUNC_KEYMGMT_MATCH , (void (*)(void ))keymgmt_match}, + {OSSL_FUNC_KEYMGMT_IMPORT , (void (*)(void ))ed25519_keymgmt_import}, + {OSSL_FUNC_KEYMGMT_IMPORT_TYPES , (void (*)(void ))keymgmt_import_types}, + {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params}, + {OSSL_FUNC_KEYMGMT_GET_PARAMS , (void (*)(void ))keymgmt_get_params}, + {OSSL_FUNC_KEYMGMT_SET_PARAMS , (void (*)(void ))keymgmt_set_params}, + {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params},/* same as gettable */ + {0 ,NULL } +}; + +const OSSL_ALGORITHM keymgmts[] = { {"RSA:rsaEncryption" ,XKEY_PROV_PROPS , rsa_keymgmt_functions,"OpenVPN xkey RSA Key Manager" }, {"RSA-PSS:RSASSA-PSS" ,XKEY_PROV_PROPS , rsa_keymgmt_functions,"OpenVPN xkey RSA-PSS Key Manager" }, {"EC:id-ecPublicKey" ,XKEY_PROV_PROPS , ec_keymgmt_functions,"OpenVPN xkey EC Key Manager" }, + {"ED448" ,XKEY_PROV_PROPS , ed448_keymgmt_functions,"OpenVPN xkey ED448 Key Manager" }, + {"ED25519" ,XKEY_PROV_PROPS , ed25519_keymgmt_functions,"OpenVPN xkey ED25519 Key Manager" }, {NULL ,NULL ,NULL ,NULL } };@@ -649,6 +714,11 @@ static const char *saltlen_names[] = {"digest", "max", "auto", NULL};static const char *xkey_mdname (const char *name) { +if (name ==NULL ) + { +return "none" ; + } +int i =0 ;int nid =EVP_MD_get_type (EVP_get_digestbyname (name));@@ -835,7 +905,7 @@ signature_sign_init(void *ctx, void *provkey, const OSSL_PARAM params[]) } sctx->keydata = provkey; sctx->keydata->refcount++;/* we are keeping a copy */ - sctx->sigalg.keytype =KEYTYPE (sctx->keydata) ==EVP_PKEY_RSA ?"RSA" :"EC" ; + sctx->sigalg.keytype =get_keytype (sctx->keydata);signature_set_ctx_params (sctx, params);@@ -929,10 +999,21 @@ signature_digest_sign_init(void *ctx, const char *mdname, } sctx->keydata = provkey;/* used by digest_sign */ sctx->keydata->refcount++; - sctx->sigalg.keytype =KEYTYPE (sctx->keydata) ==EVP_PKEY_RSA ?"RSA" :"EC" ; + sctx->sigalg.keytype =get_keytype (sctx->keydata);signature_set_ctx_params (ctx, params); -if (mdname) +if (!strcmp (sctx->sigalg.keytype,"ED448" ) || !strcmp (sctx->sigalg.keytype,"ED25519" )) + { +/* EdDSA requires NULL as digest for the DigestSign API instead +* of using the normal Sign API. Ensure it is actually NULL too */ +if (mdname !=NULL ) + { +msg (M_WARN ,"xkey digest_sign_init: mdname must be NULL for ED448/ED25519." ); +return 0 ; + } + sctx->sigalg.mdname ="none" ; + } +else if (mdname) { sctx->sigalg.mdname =xkey_mdname (mdname);/* get a string literal pointer */ }@@ -1073,6 +1154,8 @@ static const OSSL_DISPATCH signature_functions[] = {const OSSL_ALGORITHM signatures[] = { {"RSA:rsaEncryption" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey RSA Signature" }, {"ECDSA" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey ECDSA Signature" }, + {"ED448" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey Ed448 Signature" }, + {"ED25519" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey Ed25519 Signature" }, {NULL ,NULL ,NULL ,NULL } };tests/unit_tests/openvpn/test_provider.c +13
-4@@ -66,7 +66,11 @@ static const char *const pubkey2 = "-----BEGIN PUBLIC KEY-----\n""u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n" "-----END PUBLIC KEY-----\n" ; -static const char *pubkeys[] = {pubkey1, pubkey2}; +static const char *const pubkey3 ="-----BEGIN PUBLIC KEY-----\n" +"MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n" +"-----END PUBLIC KEY-----" ; + +static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};static const char *prov_name ="ovpn.xkey" ;@@ -158,12 +162,17 @@ management_query_pk_sig(struct management *man, const char *b64_data,if (strstr (algorithm,"data=message" )) { expected_tbs = test_msg_b64; -assert_non_null (strstr (algorithm,"hashalg=SHA256" )); +/* ED25519 does not have a hash algorithm even though it goes via +* the DigestSign path (data=message) */ +if (!strstr (algorithm,"ED25519" )) + { +assert_non_null (strstr (algorithm,"hashalg=SHA256" )); + } }assert_string_equal (b64_data, expected_tbs); -/* We test using ECDSA or PSS with saltlen = digest */ -if (!strstr (algorithm,"ECDSA" )) +/* We test using ED25519, ECDSA or PSS with saltlen = digest */ +if (!strstr (algorithm,"ECDSA" ) && !strstr (algorithm,"ED25519" )) {assert_non_null (strstr (algorithm,"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest" )); }
tests/unit_tests/openvpn/test_provider.c digest_sign(EVP_PKEY *pkey) · line 243
Quoted 7 lines
tests/unit_tests/openvpn/test_provider.c +4
-0@@ -228,6 +237,13 @@ digest_sign(EVP_PKEY *pkey) params[3 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_MGF1_DIGEST , (char *)saltlen,0 ); params[4 ] =OSSL_PARAM_construct_end (); } +else if (EVP_PKEY_get_id (pkey) ==EVP_PKEY_ED25519 ) + { + mdname =NULL ; + params[0 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_DIGEST , (char *)mdname,0 );
I think it's wrong to pass NULL here as the last 0 in OSSL_PARAM_construct() means what is passed is a null-terminated string. We cope with this in xkey_provider for this particular case by checking for NULL, but generally we would dereference strings in OSSL_PARAM without checking. OpenSSL also may.
In any case, mdname = NULL is all that is required for this key type and no need to set any params (same as with EC).
Quoted 39 lines
tests/unit_tests/openvpn/test_provider.c +13
-4+ params[1 ] =OSSL_PARAM_construct_end (); + } +EVP_PKEY_CTX *pctx =NULL ;EVP_MD_CTX *mctx =EVP_MD_CTX_new ();@@ -328,13 +344,20 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen,assert_memory_equal (tbs, test_digest,sizeof (test_digest)); } -/* For the test use sha256 and PSS padding for RSA */ -assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); +/* For the test use sha256 and PSS padding for RSA and none for EDDSA */ +if (!strcmp (s.keytype,"ED25519" )) + { +assert_string_equal (s.mdname,"none" ); + } +else + { +assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); + }if (!strcmp (s.keytype,"RSA" )) {assert_string_equal (s.padmode,"pss" );/* we use PSS for the test */ } -else if (strcmp (s.keytype,"EC" )) +else if (strcmp (s.keytype,"EC" ) &&strcmp (s.keytype,"ED25519" )) {fail_msg ("Unknown keytype: %s" , s.keytype); } --2.32.0 (Apple Git-132)
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Quoted 9 lines
src/openvpn/xkey_helper.c xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen, · line 183
Quoted 5 lines
/* if management client cannot do digest -- we do it here */src/openvpn/xkey_helper.c +2
-1-if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST )) +if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST ) + &&strcmp (alg.mdname,"none" ) !=0 )nit: Why not
strcmp(alg.mdname, "none")? That's how we use it elsewhere in the patch and this file. Unless our coding style now requires this.
clang-tidy complained but I have no strong preference, I will remove the != 0 in v3 of the patch.
Quoted 6 lines
tests/unit_tests/openvpn/test_provider.c digest_sign(EVP_PKEY *pkey) · line 243
Quoted 5 lines
}tests/unit_tests/openvpn/test_provider.c +4
-0+else if (EVP_PKEY_get_id (pkey) ==EVP_PKEY_ED25519 ) + { + mdname =NULL ; + params[0 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_DIGEST , (char *)mdname,0 );
Quoted 8 lines
I think it's wrong to pass NULL here as the last 0 in
OSSL_PARAM_construct()means what is passed is a null-terminated string. We cope with this inxkey_providerfor this particular case by checking for NULL, but generally we would dereference strings inOSSL_PARAMwithout checking. OpenSSL also may.In any case, mdname = NULL is all that is required for this key type and no need to set any params (same as with EC).
Good point. I still think the OpenSSL API is kind of broken here as it basically forces you do pass NULL in EVP_DigestSignInit/EVP_DigestSignInit_ex but you cannot pass the same with params but that is not the place to fix it.
Arne
PATCH v3 1/1 Implement ED448 and ED25519 support in xkey_provider
In reply to Arne SchwabeOpenSSL's implementation of ED448 and ED25519 has a few idiosyncrasies. Instead of belonging to the elliptic curve type or to a common Edwards curve type, ED448 and ED25519 have each their own type.
Also, OpenSSL expects signatures using these curves to be done with the EVP_DigestSign API instead of the EVP_Sign API but using md=NULL.
This has been tested using a "fake" external key that used a normal software key instead of a hardware implementation but that makes no difference from the perspective of xkey_provider/management interface.
Patch v2: remove name functions from ed448/ed25519, ensure md is NULL
for ed448/ed25519 and handle NULL/none better in general.
Patch v3: do not pass NULL as string for the OSSL params.
--- src/openvpn/xkey_common.h | 2 +- src/openvpn/xkey_helper.c | 7 +- src/openvpn/xkey_provider.c | 97 ++++++++++++++++++++++-- tests/unit_tests/openvpn/test_provider.c | 36 +++++++-- 4 files changed, 126 insertions(+), 16 deletions(-)
src/openvpn/xkey_common.h +1 -1
@@ -43,7 +43,7 @@ OSSL_provider_init_fn xkey_provider_init;#define XKEY_PROV_PROPS "provider=ovpn.xkey"/** -* Stuct to encapsulate signature algorithm parameters to pass +* Struct to encapsulate signature algorithm parameters to pass * to sign operation. */ typedef struct {
src/openvpn/xkey_helper.c +6 -1
@@ -179,7 +179,8 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,bool is_message = !strcmp (alg.op,"DigestSign" );/* tbs is message, not digest */ /* if management client cannot do digest -- we do it here */ -if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST )) +if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST ) + &&strcmp (alg.mdname,"none" )) {dmsg (D_XKEY ,"xkey_management_sign: computing digest" );if (xkey_digest (tbs, tbslen, buf, &buflen, alg.mdname))
@@ -206,6 +207,10 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,openvpn_snprintf (alg_str,sizeof (alg_str),"ECDSA,hashalg=%s" , alg.mdname); } } +else if (!strcmp (alg.keytype,"ED448" ) || !strcmp (alg.keytype,"ED25519" )) + { +strncpynt (alg_str, alg.keytype,sizeof (alg_str)); + }/* else assume RSA key */ else if (!strcmp (alg.padmode,"pkcs1" ) && (flags &MF_EXTERNAL_KEY_PKCS1PAD )) {
src/openvpn/xkey_provider.c +90 -7
154 lines not on this page · raw message
tests/unit_tests/openvpn/test_provider.c +29 -7
@@ -66,7 +66,11 @@ static const char *const pubkey2 = "-----BEGIN PUBLIC KEY-----\n""u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n" "-----END PUBLIC KEY-----\n" ; -static const char *pubkeys[] = {pubkey1, pubkey2}; +static const char *const pubkey3 ="-----BEGIN PUBLIC KEY-----\n" +"MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n" +"-----END PUBLIC KEY-----" ; + +static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};static const char *prov_name ="ovpn.xkey" ;
@@ -158,12 +162,17 @@ management_query_pk_sig(struct management *man, const char *b64_data,if (strstr (algorithm,"data=message" )) { expected_tbs = test_msg_b64; -assert_non_null (strstr (algorithm,"hashalg=SHA256" )); +/* ED25519 does not have a hash algorithm even though it goes via +* the DigestSign path (data=message) */ +if (!strstr (algorithm,"ED25519" )) + { +assert_non_null (strstr (algorithm,"hashalg=SHA256" )); + } }assert_string_equal (b64_data, expected_tbs); -/* We test using ECDSA or PSS with saltlen = digest */ -if (!strstr (algorithm,"ECDSA" )) +/* We test using ED25519, ECDSA or PSS with saltlen = digest */ +if (!strstr (algorithm,"ECDSA" ) && !strstr (algorithm,"ED25519" )) {assert_non_null (strstr (algorithm,"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest" )); }
@@ -228,6 +237,12 @@ digest_sign(EVP_PKEY *pkey) params[3 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_MGF1_DIGEST , (char *)saltlen,0 ); params[4 ] =OSSL_PARAM_construct_end (); } +else if (EVP_PKEY_get_id (pkey) ==EVP_PKEY_ED25519 ) + { + mdname =NULL ; + params[0 ] =OSSL_PARAM_construct_end (); + } +EVP_PKEY_CTX *pctx =NULL ;EVP_MD_CTX *mctx =EVP_MD_CTX_new ();
@@ -328,13 +343,20 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen,assert_memory_equal (tbs, test_digest,sizeof (test_digest)); } -/* For the test use sha256 and PSS padding for RSA */ -assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); +/* For the test use sha256 and PSS padding for RSA and none for EDDSA */ +if (!strcmp (s.keytype,"ED25519" )) + { +assert_string_equal (s.mdname,"none" ); + } +else + { +assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); + }if (!strcmp (s.keytype,"RSA" )) {assert_string_equal (s.padmode,"pss" );/* we use PSS for the test */ } -else if (strcmp (s.keytype,"EC" )) +else if (strcmp (s.keytype,"EC" ) &&strcmp (s.keytype,"ED25519" )) {fail_msg ("Unknown keytype: %s" , s.keytype); }
Signature · 1 line
2.32.0 (Apple Git-132)
Hi,
Thanks for the new version. Looks good (only compile tested).
Selva
Quoted 311 lines · Arne Schwabe
OpenSSL's implementation of ED448 and ED25519 has a few idiosyncrasies. Instead of belonging to the elliptic curve type or to a common Edwards curve type, ED448 and ED25519 have each their own type.
Also, OpenSSL expects signatures using these curves to be done with the
EVP_DigestSignAPI instead of theEVP_SignAPI but using md=NULL.This has been tested using a "fake" external key that used a normal software key instead of a hardware implementation but that makes no difference from the perspective of xkey_provider/management interface.
Patch v2: remove name functions from ed448/ed25519, ensure md is NULL
for ed448/ed25519 and handle NULL/none better in general.Patch v3: do not pass NULL as string for the OSSL params.
--- src/openvpn/xkey_common.h | 2 +- src/openvpn/xkey_helper.c | 7 +- src/openvpn/xkey_provider.c | 97 ++++++++++++++++++++++-- tests/unit_tests/openvpn/test_provider.c | 36 +++++++-- 4 files changed, 126 insertions(+), 16 deletions(-)src/openvpn/xkey_common.h +1
-1@@ -43,7 +43,7 @@ OSSL_provider_init_fn xkey_provider_init;#define XKEY_PROV_PROPS "provider=ovpn.xkey"/** -* Stuct to encapsulate signature algorithm parameters to pass +* Struct to encapsulate signature algorithm parameters to pass * to sign operation. */ typedef struct {src/openvpn/xkey_helper.c +6
-1@@ -179,7 +179,8 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,bool is_message = !strcmp (alg.op,"DigestSign" );/* tbs is message, not digest */ /* if management client cannot do digest -- we do it here */ -if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST )) +if (!strcmp (alg.op,"DigestSign" ) && !(flags &MF_EXTERNAL_KEY_DIGEST ) + &&strcmp (alg.mdname,"none" )) {dmsg (D_XKEY ,"xkey_management_sign: computing digest" );if (xkey_digest (tbs, tbslen, buf, &buflen, alg.mdname))@@ -206,6 +207,10 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,openvpn_snprintf (alg_str,sizeof (alg_str),"ECDSA,hashalg=%s" , alg.mdname); } } +else if (!strcmp (alg.keytype,"ED448" ) || !strcmp (alg.keytype,"ED25519" )) + { +strncpynt (alg_str, alg.keytype,sizeof (alg_str)); + }/* else assume RSA key */ else if (!strcmp (alg.padmode,"pkcs1" ) && (flags &MF_EXTERNAL_KEY_PKCS1PAD )) {src/openvpn/xkey_provider.c +90
-7@@ -99,12 +99,28 @@ typedef structint refcount;/**< reference count */ }XKEY_KEYDATA ; -static int -KEYTYPE (const XKEY_KEYDATA *key) +static inline const char * +get_keytype (const XKEY_KEYDATA *key) { -return key->pubkey ?EVP_PKEY_get_id (key->pubkey) :0 ; +int keytype = key->pubkey ?EVP_PKEY_get_id (key->pubkey) :0 ; + +switch (keytype) + { +case EVP_PKEY_RSA : +return "RSA" ; + +case EVP_PKEY_ED448 : +return "ED448" ; + +case EVP_PKEY_ED25519 : +return "ED25519" ; + +default : +return "EC" ; + } } +static int KEYSIZE (const XKEY_KEYDATA *key) {@@ -310,6 +326,22 @@ ec_keymgmt_import(void *keydata, int selection, const OSSL_PARAM params[])return keymgmt_import (keydata, selection, params,"EC" ); } +static int +ed448_keymgmt_import (void *keydata,int selection,const OSSL_PARAM params[]) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +return keymgmt_import (keydata, selection, params,"ED448" ); +} + +static int +ed25519_keymgmt_import (void *keydata,int selection,const OSSL_PARAM params[]) +{ +xkey_dmsg (D_XKEY ,"entry" ); + +return keymgmt_import (keydata, selection, params,"ED25519" ); +} +/* This function has to exist for key import to work * though we do not support import of individual params * like n or e. We simply return an empty list here for @@ -449,7 +481,7 @@ keymgmt_import_helper(XKEY_KEYDATA *key, const OSSL_PARAM *params)ASSERT (pkey);int id =EVP_PKEY_get_id (pkey); -if (id !=EVP_PKEY_RSA && id !=EVP_PKEY_EC ) +if (id !=EVP_PKEY_RSA && id !=EVP_PKEY_EC && id !=EVP_PKEY_ED25519 && id !=EVP_PKEY_ED448 ) {msg (M_WARN ,"Error: xkey keymgmt_import: unknown key type (%d)" , id);return 0 ;@@ -588,10 +620,43 @@ static const OSSL_DISPATCH ec_keymgmt_functions[] = { {0, NULL } }; +static const OSSL_DISPATCH ed448_keymgmt_functions[] = { + {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, + {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free}, + {OSSL_FUNC_KEYMGMT_LOAD , (void (*)(void ))keymgmt_load}, + {OSSL_FUNC_KEYMGMT_HAS , (void (*)(void ))keymgmt_has}, + {OSSL_FUNC_KEYMGMT_MATCH , (void (*)(void ))keymgmt_match}, + {OSSL_FUNC_KEYMGMT_IMPORT , (void (*)(void ))ed448_keymgmt_import}, + {OSSL_FUNC_KEYMGMT_IMPORT_TYPES , (void (*)(void ))keymgmt_import_types}, + {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params}, + {OSSL_FUNC_KEYMGMT_GET_PARAMS , (void (*)(void ))keymgmt_get_params}, + {OSSL_FUNC_KEYMGMT_SET_PARAMS , (void (*)(void ))keymgmt_set_params}, + {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params},/* same as gettable */ + {0 ,NULL } +}; + +static const OSSL_DISPATCH ed25519_keymgmt_functions[] = { + {OSSL_FUNC_KEYMGMT_NEW , (void (*)(void ))keymgmt_new}, + {OSSL_FUNC_KEYMGMT_FREE , (void (*)(void ))keymgmt_free}, + {OSSL_FUNC_KEYMGMT_LOAD , (void (*)(void ))keymgmt_load}, + {OSSL_FUNC_KEYMGMT_HAS , (void (*)(void ))keymgmt_has}, + {OSSL_FUNC_KEYMGMT_MATCH , (void (*)(void ))keymgmt_match}, + {OSSL_FUNC_KEYMGMT_IMPORT , (void (*)(void ))ed25519_keymgmt_import}, + {OSSL_FUNC_KEYMGMT_IMPORT_TYPES , (void (*)(void ))keymgmt_import_types}, + {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params}, + {OSSL_FUNC_KEYMGMT_GET_PARAMS , (void (*)(void ))keymgmt_get_params}, + {OSSL_FUNC_KEYMGMT_SET_PARAMS , (void (*)(void ))keymgmt_set_params}, + {OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS , (void (*)(void ))keymgmt_gettable_params},/* same as gettable */ + {0 ,NULL } +}; + +const OSSL_ALGORITHM keymgmts[] = { {"RSA:rsaEncryption" ,XKEY_PROV_PROPS , rsa_keymgmt_functions,"OpenVPN xkey RSA Key Manager" }, {"RSA-PSS:RSASSA-PSS" ,XKEY_PROV_PROPS , rsa_keymgmt_functions,"OpenVPN xkey RSA-PSS Key Manager" }, {"EC:id-ecPublicKey" ,XKEY_PROV_PROPS , ec_keymgmt_functions,"OpenVPN xkey EC Key Manager" }, + {"ED448" ,XKEY_PROV_PROPS , ed448_keymgmt_functions,"OpenVPN xkey ED448 Key Manager" }, + {"ED25519" ,XKEY_PROV_PROPS , ed25519_keymgmt_functions,"OpenVPN xkey ED25519 Key Manager" }, {NULL ,NULL ,NULL ,NULL } };@@ -649,6 +714,11 @@ static const char *saltlen_names[] = {"digest", "max", "auto", NULL};static const char *xkey_mdname (const char *name) { +if (name ==NULL ) + { +return "none" ; + } +int i =0 ;int nid =EVP_MD_get_type (EVP_get_digestbyname (name));@@ -835,7 +905,7 @@ signature_sign_init(void *ctx, void *provkey, const OSSL_PARAM params[]) } sctx->keydata = provkey; sctx->keydata->refcount++;/* we are keeping a copy */ - sctx->sigalg.keytype =KEYTYPE (sctx->keydata) ==EVP_PKEY_RSA ?"RSA" :"EC" ; + sctx->sigalg.keytype =get_keytype (sctx->keydata);signature_set_ctx_params (sctx, params);@@ -929,10 +999,21 @@ signature_digest_sign_init(void *ctx, const char *mdname, } sctx->keydata = provkey;/* used by digest_sign */ sctx->keydata->refcount++; - sctx->sigalg.keytype =KEYTYPE (sctx->keydata) ==EVP_PKEY_RSA ?"RSA" :"EC" ; + sctx->sigalg.keytype =get_keytype (sctx->keydata);signature_set_ctx_params (ctx, params); -if (mdname) +if (!strcmp (sctx->sigalg.keytype,"ED448" ) || !strcmp (sctx->sigalg.keytype,"ED25519" )) + { +/* EdDSA requires NULL as digest for the DigestSign API instead +* of using the normal Sign API. Ensure it is actually NULL too */ +if (mdname !=NULL ) + { +msg (M_WARN ,"xkey digest_sign_init: mdname must be NULL for ED448/ED25519." ); +return 0 ; + } + sctx->sigalg.mdname ="none" ; + } +else if (mdname) { sctx->sigalg.mdname =xkey_mdname (mdname);/* get a string literal pointer */ }@@ -1073,6 +1154,8 @@ static const OSSL_DISPATCH signature_functions[] = {const OSSL_ALGORITHM signatures[] = { {"RSA:rsaEncryption" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey RSA Signature" }, {"ECDSA" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey ECDSA Signature" }, + {"ED448" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey Ed448 Signature" }, + {"ED25519" ,XKEY_PROV_PROPS , signature_functions,"OpenVPN xkey Ed25519 Signature" }, {NULL ,NULL ,NULL ,NULL } };tests/unit_tests/openvpn/test_provider.c +29
-8@@ -66,7 +66,11 @@ static const char *const pubkey2 = "-----BEGIN PUBLIC KEY-----\n""u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n" "-----END PUBLIC KEY-----\n" ; -static const char *pubkeys[] = {pubkey1, pubkey2}; +static const char *const pubkey3 ="-----BEGIN PUBLIC KEY-----\n" +"MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n" +"-----END PUBLIC KEY-----" ; + +static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};static const char *prov_name ="ovpn.xkey" ;@@ -158,12 +162,17 @@ management_query_pk_sig(struct management *man, const char *b64_data,if (strstr (algorithm,"data=message" )) { expected_tbs = test_msg_b64; -assert_non_null (strstr (algorithm,"hashalg=SHA256" )); +/* ED25519 does not have a hash algorithm even though it goes via +* the DigestSign path (data=message) */ +if (!strstr (algorithm,"ED25519" )) + { +assert_non_null (strstr (algorithm,"hashalg=SHA256" )); + } }assert_string_equal (b64_data, expected_tbs); -/* We test using ECDSA or PSS with saltlen = digest */ -if (!strstr (algorithm,"ECDSA" )) +/* We test using ED25519, ECDSA or PSS with saltlen = digest */ +if (!strstr (algorithm,"ECDSA" ) && !strstr (algorithm,"ED25519" )) {assert_non_null (strstr (algorithm,"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest" )); }@@ -228,6 +237,12 @@ digest_sign(EVP_PKEY *pkey) params[3 ] =OSSL_PARAM_construct_utf8_string (OSSL_SIGNATURE_PARAM_MGF1_DIGEST , (char *)saltlen,0 ); params[4 ] =OSSL_PARAM_construct_end (); } +else if (EVP_PKEY_get_id (pkey) ==EVP_PKEY_ED25519 ) + { + mdname =NULL ; + params[0 ] =OSSL_PARAM_construct_end (); + } +EVP_PKEY_CTX *pctx =NULL ;EVP_MD_CTX *mctx =EVP_MD_CTX_new ();@@ -328,13 +343,20 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen,assert_memory_equal (tbs, test_digest,sizeof (test_digest)); } -/* For the test use sha256 and PSS padding for RSA */ -assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); +/* For the test use sha256 and PSS padding for RSA and none for EDDSA */ +if (!strcmp (s.keytype,"ED25519" )) + { +assert_string_equal (s.mdname,"none" ); + } +else + { +assert_int_equal (OBJ_sn2nid (s.mdname), NID_sha256); + }if (!strcmp (s.keytype,"RSA" )) {assert_string_equal (s.padmode,"pss" );/* we use PSS for the test */ } -else if (strcmp (s.keytype,"EC" )) +else if (strcmp (s.keytype,"EC" ) &&strcmp (s.keytype,"ED25519" )) {fail_msg ("Unknown keytype: %s" , s.keytype); } --2.32.0 (Apple Git-132)
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
I have only stared a bit at the code, and then run "make check" on a FreeBSD / OpenSSL 3.0.3 - which passes, including test_provider (the t_client check succeeds, but does not excercise this code at all - so I'm not really testing it).
Uncrustify / pre-applypatch complained about the new ",<blank>" rules in test_provider.c, so I fixed them :-) - thanks, Frank!
Your patch has been applied to the master branch.
commit c0323aa63d638ee72eb73fcf2a6983b568f952f9
Author: Arne Schwabe Date: Mon May 16 12:48:07 2022 +0200
Implement ED448 and ED25519 support in xkey_provider
Acked-by: Selva Nair <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg24363.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering