diff options
author | Michael Baentsch <57787676+baentsch@users.noreply.github.com> | 2022-09-26 17:32:05 +0200 |
---|---|---|
committer | Pauli <pauli@openssl.org> | 2023-02-24 11:02:48 +1100 |
commit | ee58915cfd9d0ad67f52d43cc1a2ce549049d248 (patch) | |
tree | e892900c53900bd693498bdc9ff2152ae14bcbe6 /test | |
parent | 1817dcaf556df559a32eed14d0947ff961be7b4f (diff) |
first cut at sigalg loading
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19312)
Diffstat (limited to 'test')
-rw-r--r-- | test/sslapitest.c | 104 | ||||
-rw-r--r-- | test/tls-provider.c | 2566 |
2 files changed, 2585 insertions, 85 deletions
diff --git a/test/sslapitest.c b/test/sslapitest.c index 17f3d07280..3a8242d2d8 100644 --- a/test/sslapitest.c +++ b/test/sslapitest.c @@ -9390,6 +9390,109 @@ static int test_pluggable_group(int idx) return testresult; } + +/* + * This function triggers encode, decode and sign functions + * of the artificial "xorhmacsig" algorithm implemented in tls-provider + * creating private key and certificate files for use in TLS testing. + */ +static int create_cert_key(int idx, char *certfilename, char *privkeyfilename) +{ + EVP_PKEY_CTX * evpctx = EVP_PKEY_CTX_new_from_name(libctx, + (idx == 0) ? "xorhmacsig" : "xorhmacsha2sig", NULL); + EVP_PKEY *pkey = NULL; + X509 *x509 = X509_new(); + X509_NAME *name = NULL; + BIO *keybio = NULL, *certbio = NULL; + int ret = 1; + + if (!TEST_ptr(evpctx) + || !TEST_true(EVP_PKEY_keygen_init(evpctx)) + || !TEST_true(EVP_PKEY_generate(evpctx, &pkey)) + || !TEST_ptr(pkey) + || !TEST_ptr(x509) + || !TEST_true(ASN1_INTEGER_set(X509_get_serialNumber(x509), 1)) + || !TEST_true(X509_gmtime_adj(X509_getm_notBefore(x509), 0)) + || !TEST_true(X509_gmtime_adj(X509_getm_notAfter(x509), 31536000L)) + || !TEST_true(X509_set_pubkey(x509, pkey)) + || !TEST_ptr(name = X509_get_subject_name(x509)) + || !TEST_true(X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, + (unsigned char *)"CH", -1, -1, 0)) + || !TEST_true(X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, + (unsigned char *)"test.org", -1, -1, 0)) + || !TEST_true(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, + (unsigned char *)"localhost", -1, -1, 0)) + || !TEST_true(X509_set_issuer_name(x509, name)) + || !TEST_true(X509_sign(x509, pkey, EVP_sha1())) + || !TEST_ptr(keybio = BIO_new_file(privkeyfilename, "wb")) + || !TEST_true(PEM_write_bio_PrivateKey(keybio, pkey, NULL, NULL, 0, NULL, NULL)) + || !TEST_ptr(certbio = BIO_new_file(certfilename, "wb")) + || !TEST_true(PEM_write_bio_X509(certbio, x509))) + ret = 0; + + EVP_PKEY_free(pkey); + X509_free(x509); + EVP_PKEY_CTX_free(evpctx); + BIO_free(keybio); + BIO_free(certbio); + return ret; +} + +/* + * Test that signature algorithms loaded via the provider interface can + * correctly establish a TLS (1.3) connection. + * Test 0: Signature algorithm with built-in hashing functionality: "xorhmacsig" + * Test 1: Signature algorithm using external SHA2 hashing: "xorhmacsha2sig" + */ +static int test_pluggable_signature(int idx) +{ + SSL_CTX *cctx = NULL, *sctx = NULL; + SSL *clientssl = NULL, *serverssl = NULL; + int testresult = 0; + OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider"); + OSSL_PROVIDER *defaultprov = OSSL_PROVIDER_load(libctx, "default"); + char *certfilename = "tls-prov-cert.pem"; + char *privkeyfilename = "tls-prov-key.pem"; + + /* create key and certificate for the different algorithm types */ + if (!TEST_ptr(tlsprov) + || !TEST_true(create_cert_key(idx, certfilename, privkeyfilename))) + goto end; + + if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), + TLS_client_method(), + TLS1_3_VERSION, + TLS1_3_VERSION, + &sctx, &cctx, certfilename, privkeyfilename)) + || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, + NULL, NULL))) + goto end; + + /* This is necessary to pass minimal setup w/o other groups configured */ + if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup")) + || !TEST_true(SSL_set1_groups_list(clientssl, "xorgroup"))) + goto end; + + /* + * If this connection gets established, it must have been completed + * via the tls-provider-implemented "hmacsig" algorithm, testing + * both sign and verify functions during handshake. + */ + if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) + goto end; + + testresult = 1; + + end: + SSL_free(serverssl); + SSL_free(clientssl); + SSL_CTX_free(sctx); + SSL_CTX_free(cctx); + OSSL_PROVIDER_unload(tlsprov); + OSSL_PROVIDER_unload(defaultprov); + + return testresult; +} #endif #ifndef OPENSSL_NO_TLS1_2 @@ -10844,6 +10947,7 @@ int setup_tests(void) #endif #ifndef OPENSSL_NO_TLS1_3 ADD_ALL_TESTS(test_pluggable_group, 2); + ADD_ALL_TESTS(test_pluggable_signature, 2); #endif #ifndef OPENSSL_NO_TLS1_2 ADD_TEST(test_ssl_dup); diff --git a/test/tls-provider.c b/test/tls-provider.c index adbe88da52..c2cf583d35 100644 --- a/test/tls-provider.c +++ b/test/tls-provider.c @@ -12,9 +12,38 @@ #include <openssl/core_dispatch.h> #include <openssl/rand.h> #include <openssl/params.h> +#include <openssl/err.h> +#include <openssl/proverr.h> +#include <openssl/pkcs12.h> +#include <openssl/provider.h> +#include <assert.h> +#include <openssl/asn1.h> +#include <openssl/asn1t.h> +#include <openssl/core_object.h> +#include "internal/asn1.h" /* For TLS1_3_VERSION */ #include <openssl/ssl.h> #include "internal/nelem.h" +#include "internal/refcount.h" + +/* error codes */ + +/* xorprovider error codes */ +#define XORPROV_R_INVALID_DIGEST 1 +#define XORPROV_R_INVALID_SIZE 2 +#define XORPROV_R_INVALID_KEY 3 +#define XORPROV_R_UNSUPPORTED 4 +#define XORPROV_R_MISSING_OID 5 +#define XORPROV_R_OBJ_CREATE_ERR 6 +#define XORPROV_R_INVALID_ENCODING 7 +#define XORPROV_R_SIGN_ERROR 8 +#define XORPROV_R_LIB_CREATE_ERR 9 +#define XORPROV_R_NO_PRIVATE_KEY 10 +#define XORPROV_R_BUFFER_LENGTH_WRONG 11 +#define XORPROV_R_SIGNING_FAILED 12 +#define XORPROV_R_WRONG_PARAMETERS 13 +#define XORPROV_R_VERIFY_ERROR 14 +#define XORPROV_R_EVPINFO_MISSING 15 static OSSL_FUNC_keymgmt_import_fn xor_import; static OSSL_FUNC_keymgmt_import_types_fn xor_import_types; @@ -45,13 +74,15 @@ typedef struct xorkey_st { unsigned char pubkey[XOR_KEY_SIZE]; int hasprivkey; int haspubkey; + char *tls_name; + CRYPTO_REF_COUNT references; + CRYPTO_RWLOCK *lock; } XORKEY; +/* Key Management for the dummy XOR KEX, KEM and signature algorithms */ -/* Key Management for the dummy XOR KEX and KEM algorithms */ - -static OSSL_FUNC_keymgmt_new_fn xor_newdata; -static OSSL_FUNC_keymgmt_free_fn xor_freedata; +static OSSL_FUNC_keymgmt_new_fn xor_newkey; +static OSSL_FUNC_keymgmt_free_fn xor_freekey; static OSSL_FUNC_keymgmt_has_fn xor_has; static OSSL_FUNC_keymgmt_dup_fn xor_dup; static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init; @@ -59,6 +90,7 @@ static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params; static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params; static OSSL_FUNC_keymgmt_gen_fn xor_gen; static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup; +static OSSL_FUNC_keymgmt_load_fn xor_load; static OSSL_FUNC_keymgmt_get_params_fn xor_get_params; static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params; static OSSL_FUNC_keymgmt_set_params_fn xor_set_params; @@ -69,7 +101,7 @@ static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params; * together. Don't use this! */ -static OSSL_FUNC_keyexch_newctx_fn xor_newctx; +static OSSL_FUNC_keyexch_newctx_fn xor_newkemkexctx; static OSSL_FUNC_keyexch_init_fn xor_init; static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer; static OSSL_FUNC_keyexch_derive_fn xor_derive; @@ -81,7 +113,7 @@ static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx; * Don't use this! */ -static OSSL_FUNC_kem_newctx_fn xor_newctx; +static OSSL_FUNC_kem_newctx_fn xor_newkemkexctx; static OSSL_FUNC_kem_freectx_fn xor_freectx; static OSSL_FUNC_kem_dupctx_fn xor_dupctx; static OSSL_FUNC_kem_encapsulate_init_fn xor_init; @@ -89,6 +121,79 @@ static OSSL_FUNC_kem_encapsulate_fn xor_encapsulate; static OSSL_FUNC_kem_decapsulate_init_fn xor_init; static OSSL_FUNC_kem_decapsulate_fn xor_decapsulate; +/* + * Common key management table access functions + */ +static OSSL_FUNC_keymgmt_new_fn * +xor_prov_get_keymgmt_new(const OSSL_DISPATCH *fns) +{ + /* Pilfer the keymgmt dispatch table */ + for (; fns->function_id != 0; fns++) + if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW) + return OSSL_FUNC_keymgmt_new(fns); + + return NULL; +} + +static OSSL_FUNC_keymgmt_free_fn * +xor_prov_get_keymgmt_free(const OSSL_DISPATCH *fns) +{ + /* Pilfer the keymgmt dispatch table */ + for (; fns->function_id != 0; fns++) + if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE) + return OSSL_FUNC_keymgmt_free(fns); + + return NULL; +} + +static OSSL_FUNC_keymgmt_import_fn * +xor_prov_get_keymgmt_import(const OSSL_DISPATCH *fns) +{ + /* Pilfer the keymgmt dispatch table */ + for (; fns->function_id != 0; fns++) + if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT) + return OSSL_FUNC_keymgmt_import(fns); + + return NULL; +} + +static OSSL_FUNC_keymgmt_export_fn * +xor_prov_get_keymgmt_export(const OSSL_DISPATCH *fns) +{ + /* Pilfer the keymgmt dispatch table */ + for (; fns->function_id != 0; fns++) + if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT) + return OSSL_FUNC_keymgmt_export(fns); + + return NULL; +} + +static void *xor_prov_import_key(const OSSL_DISPATCH *fns, void *provctx, + int selection, const OSSL_PARAM params[]) +{ + OSSL_FUNC_keymgmt_new_fn *kmgmt_new = xor_prov_get_keymgmt_new(fns); + OSSL_FUNC_keymgmt_free_fn *kmgmt_free = xor_prov_get_keymgmt_free(fns); + OSSL_FUNC_keymgmt_import_fn *kmgmt_import = + xor_prov_get_keymgmt_import(fns); + void *key = NULL; + + if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) { + if ((key = kmgmt_new(provctx)) == NULL + || !kmgmt_import(key, selection, params)) { + kmgmt_free(key); + key = NULL; + } + } + return key; +} + +static void xor_prov_free_key(const OSSL_DISPATCH *fns, void *key) +{ + OSSL_FUNC_keymgmt_free_fn *kmgmt_free = xor_prov_get_keymgmt_free(fns); + + if (kmgmt_free != NULL) + kmgmt_free(key); +} /* * We define 2 dummy TLS groups called "xorgroup" and "xorkemgroup" for test @@ -107,7 +212,7 @@ struct tls_group_st { #define XORGROUP_NAME "xorgroup" #define XORGROUP_NAME_INTERNAL "xorgroup-int" static struct tls_group_st xor_group = { - 0, /* group_id, set by randomize_tls_group_id() */ + 0, /* group_id, set by randomize_tls_alg_id() */ 128, /* secbits */ TLS1_3_VERSION, /* mintls */ 0, /* maxtls */ @@ -119,7 +224,7 @@ static struct tls_group_st xor_group = { #define XORKEMGROUP_NAME "xorkemgroup" #define XORKEMGROUP_NAME_INTERNAL "xorkemgroup-int" static struct tls_group_st xor_kemgroup = { - 0, /* group_id, set by randomize_tls_group_id() */ + 0, /* group_id, set by randomize_tls_alg_id() */ 128, /* secbits */ TLS1_3_VERSION, /* mintls */ 0, /* maxtls */ @@ -171,65 +276,188 @@ static const OSSL_PARAM xor_kemgroup_params[] = { #define NUM_DUMMY_GROUPS 50 static char *dummy_group_names[NUM_DUMMY_GROUPS]; +/* + * We define a dummy TLS sigalg called for test purposes + */ +struct tls_sigalg_st { + unsigned int code_point; /* for "tls-sigalg-alg", see provider-base(7) */ + unsigned int secbits; + unsigned int mintls; + unsigned int maxtls; +}; + +#define XORSIGALG_NAME "xorhmacsig" +#define XORSIGALG_OID "1.3.6.1.4.1.16604.998888.1" +#define XORSIGALG_HASH_NAME "xorhmacsha2sig" +#define XORSIGALG_HASH "SHA256" +#define XORSIGALG_HASH_OID "1.3.6.1.4.1.16604.998888.2" +#define XORSIGALG12_NAME "xorhmacsig12" +#define XORSIGALG12_OID "1.3.6.1.4.1.16604.998888.3" + +static struct tls_sigalg_st xor_sigalg = { + 0, /* alg id, set by randomize_tls_alg_id() */ + 128, /* secbits */ + TLS1_3_VERSION, /* mintls */ + 0, /* maxtls */ +}; + +static struct tls_sigalg_st xor_sigalg_hash = { + 0, /* alg id, set by randomize_tls_alg_id() */ + 128, /* secbits */ + TLS1_3_VERSION, /* mintls */ + 0, /* maxtls */ +}; + +static struct tls_sigalg_st xor_sigalg12 = { + 0, /* alg id, set by randomize_tls_alg_id() */ + 128, /* secbits */ + TLS1_2_VERSION, /* mintls */ + TLS1_2_VERSION, /* maxtls */ +}; + +static const OSSL_PARAM xor_sig_nohash_params[] = { + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME, + XORSIGALG_NAME, sizeof(XORSIGALG_NAME)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME, + XORSIGALG_NAME, + sizeof(XORSIGALG_NAME)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID, + XORSIGALG_OID, sizeof(XORSIGALG_OID)), + OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT, + &xor_sigalg.code_point), + OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS, + &xor_sigalg.secbits), + OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS, + &xor_sigalg.mintls), + OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS, + &xor_sigalg.maxtls), + OSSL_PARAM_END +}; + +static const OSSL_PARAM xor_sig_hash_params[] = { + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME, + XORSIGALG_HASH_NAME, sizeof(XORSIGALG_HASH_NAME)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME, + XORSIGALG_HASH_NAME, + sizeof(XORSIGALG_HASH_NAME)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME, + XORSIGALG_HASH, sizeof(XORSIGALG_HASH)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID, + XORSIGALG_HASH_OID, sizeof(XORSIGALG_HASH_OID)), + OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT, + &xor_sigalg_hash.code_point), + OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS, + &xor_sigalg_hash.secbits), + OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS, + &xor_sigalg_hash.mintls), + OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS, + &xor_sigalg_hash.maxtls), + OSSL_PARAM_END +}; + +static const OSSL_PARAM xor_sig_12_params[] = { + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME, + XORSIGALG12_NAME, sizeof(XORSIGALG12_NAME)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME, + XORSIGALG12_NAME, + sizeof(XORSIGALG12_NAME)), + OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID, + XORSIGALG12_OID, sizeof(XORSIGALG12_OID)), + OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT, + &xor_sigalg12.code_point), + OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS, + &xor_sigalg12.secbits), + OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS, + &xor_sigalg12.mintls), + OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS, + &xor_sigalg12.maxtls), + OSSL_PARAM_END +}; + static int tls_prov_get_capabilities(void *provctx, const char *capability, OSSL_CALLBACK *cb, void *arg) { - int ret; + int ret = 0; int i; const char *dummy_base = "dummy"; const size_t dummy_name_max_size = strlen(dummy_base) + 3; - if (strcmp(capability, "TLS-GROUP") != 0) { - /* We don't support this capability */ - return 0; - } + if (strcmp(capability, "TLS-GROUP") == 0) { + /* Register our 2 groups */ + ret = cb(xor_group_params, arg); + ret &= cb(xor_kemgroup_params, arg); - /* Register our 2 groups */ - ret = cb(xor_group_params, arg); - ret &= cb(xor_kemgroup_params, arg); + /* + * Now register some dummy groups > GROUPLIST_INCREMENT (== 40) as defined + * in ssl/t1_lib.c, to make sure we exercise the code paths for registering + * large numbers of groups. + */ - /* - * Now register some dummy groups > GROUPLIST_INCREMENT (== 40) as defined - * in ssl/t1_lib.c, to make sure we exercise the code paths for registering - * large numbers of groups. - */ + for (i = 0; i < NUM_DUMMY_GROUPS; i++) { + OSSL_PARAM dummygroup[OSSL_NELEM(xor_group_params)]; - for (i = 0; i < NUM_DUMMY_GROUPS; i++) { - OSSL_PARAM dummygroup[OSSL_NELEM(xor_group_params)]; + memcpy(dummygroup, xor_group_params, sizeof(xor_group_params)); - memcpy(dummygroup, xor_group_params, sizeof(xor_group_params)); - - /* Give the dummy group a unique name */ - if (dummy_group_names[i] == NULL) { - dummy_group_names[i] = OPENSSL_zalloc(dummy_name_max_size); - if (dummy_group_names[i] == NULL) - return 0; - BIO_snprintf(dummy_group_names[i], + /* Give the dummy group a unique name */ + if (dummy_group_names[i] == NULL) { + dummy_group_names[i] = OPENSSL_zalloc(dummy_name_max_size); + if (dummy_group_names[i] == NULL) + return 0; + BIO_snprintf(dummy_group_names[i], dummy_name_max_size, "%s%d", dummy_base, i); + } + dummygroup[0].data = dummy_group_names[i]; + dummygroup[0].data_size = strlen(dummy_group_names[i]) + 1; + ret &= cb(dummygroup, arg); } - dummygroup[0].data = dummy_group_names[i]; - dummygroup[0].data_size = strlen(dummy_group_names[i]) + 1; - ret &= cb(dummygroup, arg); } + if (strcmp(capability, "TLS-SIGALG") == 0) { + ret = cb(xor_sig_nohash_params, arg); + ret &= cb(xor_sig_hash_params, arg); + ret &= cb(xor_sig_12_params, arg); + } return ret; } +typedef struct { + OSSL_LIB_CTX *libctx; +} PROV_XOR_CTX; + +static PROV_XOR_CTX *xor_newprovctx(OSSL_LIB_CTX *libctx) +{ + PROV_XOR_CTX* prov_ctx = OPENSSL_malloc(sizeof(PROV_XOR_CTX)); + + if (prov_ctx == NULL) + return NULL; + + if (libctx == NULL) { + OPENSSL_free(prov_ctx); + return NULL; + } + prov_ctx->libctx = libctx; + return prov_ctx; +} + + + +#define PROV_XOR_LIBCTX_OF(provctx) (((PROV_XOR_CTX *)provctx)->libctx) + /* - * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys - * together. Don't use this! + * Dummy "XOR" Key Exchange and signature algorithm. We just xor the + * private and public keys together. Don't use this! */ typedef struct { XORKEY *key; XORKEY *peerkey; void *provctx; -} PROV_XOR_CTX; +} PROV_XORKEMKEX_CTX; -static void *xor_newctx(void *provctx) +static void *xor_newkemkexctx(void *provctx) { - PROV_XOR_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XOR_CTX)); + PROV_XORKEMKEX_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XORKEMKEX_CTX)); if (pxorctx == NULL) return NULL; @@ -242,7 +470,7 @@ static void *xor_newctx(void *provctx) static int xor_init(void *vpxorctx, void *vkey, ossl_unused const OSSL_PARAM params[]) { - PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx; + PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx; if (pxorctx == NULL || vkey == NULL) return 0; @@ -252,7 +480,7 @@ static int xor_init(void *vpxorctx, void *vkey, static int xor_set_peer(void *vpxorctx, void *vpeerkey) { - PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx; + PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx; if (pxorctx == NULL || vpeerkey == NULL) return 0; @@ -263,7 +491,7 @@ static int xor_set_peer(void *vpxorctx, void *vpeerkey) static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen, size_t outlen) { - PROV_XOR_CTX *pxorctx = (PROV_XOR_CTX *)vpxorctx; + PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx; int i; if (pxorctx->key == NULL || pxorctx->peerkey == NULL) @@ -289,8 +517,8 @@ static void xor_freectx(void *pxorctx) static void *xor_dupctx(void *vpxorctx) { - PROV_XOR_CTX *srcctx = (PROV_XOR_CTX *)vpxorctx; - PROV_XOR_CTX *dstctx; + PROV_XORKEMKEX_CTX *srcctx = (PROV_XORKEMKEX_CTX *)vpxorctx; + PROV_XORKEMKEX_CTX *dstctx; dstctx = OPENSSL_zalloc(sizeof(*srcctx)); if (dstctx == NULL) @@ -302,7 +530,7 @@ static void *xor_dupctx(void *vpxorctx) } static const OSSL_DISPATCH xor_keyexch_functions[] = { - { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newctx }, + { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newkemkexctx }, { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init }, { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive }, { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer }, @@ -340,7 +568,7 @@ static int xor_encapsulate(void *vpxorctx, int rv = 0; void *genctx = NULL, *derivectx = NULL; XORKEY *ourkey = NULL; - PROV_XOR_CTX *pxorctx = vpxorctx; + PROV_XORKEMKEX_CTX *pxorctx = vpxorctx; if (ct == NULL || ss == NULL) { /* Just return sizes */ @@ -367,7 +595,7 @@ static int xor_encapsulate(void *vpxorctx, *ctlen = XOR_KEY_SIZE; /* 3. Derive ss via KEX */ - derivectx = xor_newctx(pxorctx->provctx); + derivectx = xor_newkemkexctx(pxorctx->provctx); if (derivectx == NULL || !xor_init(derivectx, ourkey, NULL) || !xor_set_peer(derivectx, pxorctx->key) @@ -378,7 +606,7 @@ static int xor_encapsulate(void *vpxorctx, end: xor_gen_cleanup(genctx); - xor_freedata(ourkey); + xor_freekey(ourkey); xor_freectx(derivectx); return rv; } @@ -396,7 +624,7 @@ static int xor_decapsulate(void *vpxorctx, int rv = 0; void *derivectx = NULL; XORKEY *peerkey = NULL; - PROV_XOR_CTX *pxorctx = vpxorctx; + PROV_XORKEMKEX_CTX *pxorctx = vpxorctx; if (ss == NULL) { /* Just return size */ @@ -408,13 +636,13 @@ static int xor_decapsulate(void *vpxorctx, if (ctlen != XOR_KEY_SIZE) return 0; - peerkey = xor_newdata(pxorctx->provctx); + peerkey = xor_newkey(pxorctx->provctx); if (peerkey == NULL) goto end; memcpy(peerkey->pubkey, ct, XOR_KEY_SIZE); /* Derive ss via KEX */ - derivectx = xor_newctx(pxorctx->provctx); + derivectx = xor_newkemkexctx(pxorctx->provctx); if (derivectx == NULL || !xor_init(derivectx, pxorctx->key, NULL) || !xor_set_peer(derivectx, peerkey) @@ -424,13 +652,13 @@ static int xor_decapsulate(void *vpxorctx, rv = 1; end: - xor_freedata(peerkey); + xor_freekey(peerkey); xor_freectx(derivectx); return rv; } static const OSSL_DISPATCH xor_kem_functions[] = { - { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))xor_newctx }, + { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))xor_newkemkexctx }, { OSSL_FUNC_KEM_FREECTX, (void (*)(void))xor_freectx }, { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))xor_dupctx }, { OSSL_FUNC_KEM_ENCAPSULATE_INIT, (void (*)(void))xor_init }, @@ -451,14 +679,56 @@ static const OSSL_ALGORITHM tls_prov_kem[] = { /* Key Management for the dummy XOR key exchange algorithm */ -static void *xor_newdata(void *provctx) +static void *xor_newkey(void *provctx) { - return OPENSSL_zalloc(sizeof(XORKEY)); + XORKEY *ret = OPENSSL_zalloc(sizeof(XORKEY)); + + if (ret == NULL) + return NULL; + + ret->references = 1; + ret->lock = CRYPTO_THREAD_lock_new(); + if (ret->lock == NULL) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + OPENSSL_free(ret); + return NULL; + } + + return ret; } -static void xor_freedata(void *keydata) +static void xor_freekey(void *keydata) { - OPENSSL_free(keydata); + XORKEY* key = (XORKEY *)keydata; + int refcnt; + + if (key == NULL) + return; + + if (CRYPTO_DOWN_REF(&key->references, &refcnt, key->lock) <= 0) + return; + + if (refcnt > 0) + return; + assert(refcnt == 0); + + if (key != NULL) { + OPENSSL_free(key->tls_name); + key->tls_name = NULL; + } + CRYPTO_THREAD_lock_free(key->lock); + OPENSSL_free(key); +} + +static int xor_key_up_ref(XORKEY *key) +{ + int refcnt; + + if (CRYPTO_UP_REF(&key->references, &refcnt, key->lock) <= 0) + return 0; + + assert(refcnt > 1); + return (refcnt > 1); } static int xor_has(const void *vkey, int selection) @@ -479,7 +749,7 @@ static int xor_has(const void *vkey, int selection) static void *xor_dup(const void *vfromkey, int selection) { - XORKEY *tokey = xor_newdata(NULL); + XORKEY *tokey = xor_newkey(NULL); const XORKEY *fromkey = vfromkey; int ok = 0; @@ -502,9 +772,11 @@ static void *xor_dup(const void *vfromkey, int selection) tokey->hasprivkey = 0; } } + if (fromkey->tls_name != NULL) + tokey->tls_name = OPENSSL_strdup(fromkey->tls_name); } if (!ok) { - xor_freedata(tokey); + xor_freekey(tokey); tokey = NULL; } return tokey; @@ -569,6 +841,72 @@ static const OSSL_PARAM xor_known_settable_params[] = { OSSL_PARAM_END }; +static void *xor_load(const void *reference, size_t reference_sz) +{ + XORKEY *key = NULL; + + if (reference_sz == sizeof(key)) { + /* The contents of the reference is the address to our object */ + key = *(XORKEY **)reference; + /* We grabbed, so we detach it */ + *(XORKEY **)reference = NULL; + return key; + } + return NULL; +} + +/* check one key is the "XOR complement" of the other */ +static int xor_recreate(const unsigned char *kd1, const unsigned char *kd2) { + int i; + + for (i = 0; i < XOR_KEY_SIZE; i++) { + if ((kd1[i] & 0xff) != ((kd2[i] ^ private_constant[i]) & 0xff)) + return 0; + } + return 1; +} + +static int xor_match(const void *keydata1, const void *keydata2, int selection) +{ + const XORKEY *key1 = keydata1; + const XORKEY *key2 = keydata2; + int ok = 1; + + if (key1->tls_name != NULL && key2->tls_name != NULL) + ok = ok & (strcmp(key1->tls_name, key2->tls_name) == 0); + + if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { + if (key1->hasprivkey) { + if (key2->hasprivkey) + ok = ok & (CRYPTO_memcmp(key1->privkey, key2->privkey, + XOR_KEY_SIZE) == 0); + else + ok = ok & xor_recreate(key1->privkey, key2->pubkey); + } else { + if (key2->hasprivkey) + ok = ok & xor_recreate(key2->privkey, key1->pubkey); + else + ok = 0; + } + } + + if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { + if (key1->haspubkey) { + if (key2->haspubkey) + ok = ok & (CRYPTO_memcmp(key1->pubkey, key2->pubkey, XOR_KEY_SIZE) == 0); + else + ok = ok & xor_recreate(key1->pubkey, key2->privkey); + } else { + if (key2->haspubkey) + ok = ok & xor_recreate(key2->pubkey, key1->privkey); + else + ok = 0; + } + } + + return ok; +} + static const OSSL_PARAM *xor_settable_params(void *provctx) { return xor_known_settable_params; @@ -591,8 +929,7 @@ static void *xor_gen_init(void *provctx, int selection, if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) gctx->selection = selection; - /* Our provctx is really just an OSSL_LIB_CTX */ - gctx->libctx = (OSSL_LIB_CTX *)provctx; + gctx->libctx = PROV_XOR_LIBCTX_OF(provctx); if (!xor_gen_set_params(gctx, params)) { OPENSSL_free(gctx); @@ -633,7 +970,7 @@ static const OSSL_PARAM *xor_gen_settable_params(ossl_unused void *genctx, static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { struct xor_gen_ctx *gctx = genctx; - XORKEY *key = OPENSSL_zalloc(sizeof(*key)); + XORKEY *key = xor_newkey(NULL); size_t i; if (key == NULL) @@ -735,7 +1072,7 @@ static void xor_gen_cleanup(void *genctx) } static const OSSL_DISPATCH xor_keymgmt_functions[] = { - { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newdata }, + { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey }, { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init }, { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params }, { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, @@ -748,7 +1085,7 @@ static const OSSL_DISPATCH xor_keymgmt_functions[] = { { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params }, { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has }, { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup }, - { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freedata }, + { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey }, { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import }, { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types }, { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export }, @@ -756,15 +1093,2022 @@ static const OSSL_DISPATCH xor_keymgmt_functions[] = { { 0, NULL } }; +/* We're re-using most XOR keymgmt functions also for signature operations: */ +static void *xor_xorhmacsig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) +{ + XORKEY *k = xor_gen(genctx, osslcb, cbarg); + + if (k == NULL) + return NULL; + k->tls_name = OPENSSL_strdup(XORSIGALG_NAME); + if (k->tls_name == NULL) { + xor_freekey(k); + return NULL; + } + return k; +} + +static void *xor_xorhmacsha2sig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) +{ + XORKEY* k = xor_gen(genctx, osslcb, cbarg); + + if (k == NULL) + return NULL; + k->tls_name = OPENSSL_strdup(XORSIGALG_HASH_NAME); + if (k->tls_name == NULL) { + xor_freekey(k); + return NULL; + } + return k; +} + + +static const OSSL_DISPATCH xor_xorhmacsig_keymgmt_functions[] = { + { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey }, + { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init }, + { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params }, + { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, + (void (*)(void))xor_gen_settable_params }, + { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_xorhmacsig_gen }, + { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup }, + { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params }, + { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params }, + { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params }, + { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params }, + { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has }, + { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup }, + { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey }, + { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import }, + { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types }, + { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export }, + { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types }, + { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))xor_load }, + { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))xor_match }, + { 0, NULL } +}; + +static const OSSL_DISPATCH xor_xorhmacsha2sig_keymgmt_functions[] = { + { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey }, + { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init }, + { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params }, + { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, + (void (*)(void))xor_gen_settable_params }, + { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_xorhmacsha2sig_gen }, + { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup }, + { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params }, + { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params }, + { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params }, + { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params }, + { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has }, + { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup }, + { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey }, + { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import }, + { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types }, + { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export }, + { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types }, + { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))xor_load }, + { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))xor_match }, + { 0, NULL } +}; + +typedef enum { + KEY_OP_PUBLIC, + KEY_OP_PRIVATE, + KEY_OP_KEYGEN +} xor_key_op_t; + +/* Re-create XORKEY from encoding(s): Same end-state as after key-gen */ +static XORKEY *xor_key_op(const X509_ALGOR *palg, + const unsigned char *p, int plen, + xor_key_op_t op, + OSSL_LIB_CTX *libctx, const char *propq) +{ + XORKEY *key = NULL; + int nid = NID_undef; + + if (palg != NULL) { + int ptype; + + /* Algorithm parameters must be absent */ + X509_ALGOR_get0(NULL, &ptype, NULL, palg); + if (ptype != V_ASN1_UNDEF || palg->algorithm == NULL) { + ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING); + return 0; + } + nid = OBJ_obj2nid(palg->algorithm); + } + + if (p == NULL || nid == EVP_PKEY_NONE || nid == NID_undef) { + ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING); + return 0; + } + + key = xor_newkey(NULL); + if (key == NULL) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + return 0; + } + + if (XOR_KEY_SIZE != plen) { + ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING); + goto err; + } + + if (op == KEY_OP_PUBLIC) { + |