From ee58915cfd9d0ad67f52d43cc1a2ce549049d248 Mon Sep 17 00:00:00 2001 From: Michael Baentsch <57787676+baentsch@users.noreply.github.com> Date: Mon, 26 Sep 2022 17:32:05 +0200 Subject: [PATCH] first cut at sigalg loading Reviewed-by: Matt Caswell Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/19312) --- CHANGES.md | 9 + NEWS.md | 3 + apps/lib/s_cb.c | 3 +- crypto/x509/x509_set.c | 23 +- doc/man7/provider-base.pod | 123 ++ include/openssl/core_names.h | 15 + ssl/s3_lib.c | 8 +- ssl/ssl_cert.c | 80 +- ssl/ssl_cert_table.h | 2 +- ssl/ssl_ciph.c | 17 +- ssl/ssl_lib.c | 46 +- ssl/ssl_local.h | 67 +- ssl/ssl_rsa.c | 22 +- ssl/statem/statem_clnt.c | 18 +- ssl/statem/statem_lib.c | 7 +- ssl/t1_lib.c | 428 +++++- test/sslapitest.c | 104 ++ test/tls-provider.c | 2660 ++++++++++++++++++++++++++++++++-- 18 files changed, 3397 insertions(+), 238 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f83d9fa445..d02feefa6b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -30,6 +30,15 @@ OpenSSL 3.2 *Oliver Mihatsch* + * Added support for pluggable (provider-based) TLS signature algorithms. + This enables TLS 1.3 authentication operations with algorithms embedded + in providers not included by default in OpenSSL. In combination with + the already available pluggable KEM and X.509 support, this enables + for example suitable providers to deliver post-quantum or quantum-safe + cryptography to OpenSSL users. + + *Michael Baentsch* + * Added support for Hybrid Public Key Encryption (HPKE) as defined in RFC9180. HPKE is required for TLS Encrypted ClientHello (ECH), Message Layer Security (MLS) and other IETF specifications. diff --git a/NEWS.md b/NEWS.md index 69010f9e8f..fca7c57e5d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -28,6 +28,9 @@ OpenSSL 3.2 by default. * TCP Fast Open (RFC7413) support is available on Linux, macOS, and FreeBSD where enabled and supported. + * Full support for provider-based/pluggable signature algorithms in TLS 1.3 + operations as well as X.509 data structure support. With a suitable provider + this fully enables use of post-quantum/quantum-safe cryptography. OpenSSL 3.1 ----------- diff --git a/apps/lib/s_cb.c b/apps/lib/s_cb.c index 8325e7371f..04464bec03 100644 --- a/apps/lib/s_cb.c +++ b/apps/lib/s_cb.c @@ -258,7 +258,8 @@ static const char *get_sigtype(int nid) return "gost2012_512"; default: - return NULL; + /* Try to output provider-registered sig alg name */ + return OBJ_nid2sn(nid); } } diff --git a/crypto/x509/x509_set.c b/crypto/x509/x509_set.c index e804380a64..1d400a6b90 100644 --- a/crypto/x509/x509_set.c +++ b/crypto/x509/x509_set.c @@ -210,7 +210,7 @@ int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits, /* Modify *siginf according to alg and sig. Return 1 on success, else 0. */ static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg, - const ASN1_STRING *sig) + const ASN1_STRING *sig, const EVP_PKEY *pubkey) { int pknid, mdnid; const EVP_MD *md; @@ -232,12 +232,20 @@ static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg, case NID_undef: /* If we have one, use a custom handler for this algorithm */ ameth = EVP_PKEY_asn1_find(NULL, pknid); - if (ameth == NULL || ameth->siginf_set == NULL - || !ameth->siginf_set(siginf, alg, sig)) { - ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET); - return 0; + if (ameth != NULL && ameth->siginf_set != NULL + && ameth->siginf_set(siginf, alg, sig)) + break; + if (pubkey != NULL) { + int secbits; + + secbits = EVP_PKEY_get_security_bits(pubkey); + if (secbits != 0) { + siginf->secbits = secbits; + break; + } } - break; + ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET); + return 0; /* * SHA1 and MD5 are known to be broken. Reduce security bits so that * they're no longer accepted at security level 1. @@ -288,5 +296,6 @@ static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg, /* Returns 1 on success, 0 on failure */ int ossl_x509_init_sig_info(X509 *x) { - return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature); + return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature, + X509_PUBKEY_get0(x->cert_info.key)); } diff --git a/doc/man7/provider-base.pod b/doc/man7/provider-base.pod index 30b460cb29..c7e15254cd 100644 --- a/doc/man7/provider-base.pod +++ b/doc/man7/provider-base.pod @@ -607,6 +607,129 @@ or maximum. A -1 indicates that the group should not be used in that protocol. =back +=head3 "TLS-SIGALG" Capability + +The "TLS-SIGALG" capability can be queried by libssl to discover the list of +TLS signature algorithms that a provider can support. Each signature supported +can be used for client- or server-authentication in addition to the built-in +signature algorithms. +TLS1.3 clients can advertise the list of TLS signature algorithms they support +in the signature_algorithms extension, and TLS servers can select an algorithm +from the offered list that they also support. In this way a provider can add +to the list of signature algorithms that libssl already supports with +additional ones. + +Each TLS signature algorithm that a provider supports should be described via +the callback passed in through the provider_get_capabilities function. Each +algorithm can have the following details supplied: + +=over 4 + +=item "iana-name" (B) + +The name of the signature algorithm as given in the IANA TLS Signature Scheme +registry as "Description": +L. +This value must be supplied. + +=item "iana-code-point" (B) + +The TLS algorithm ID value as given in the IANA TLS SignatureScheme registry. +This value must be supplied. + +=item "sigalg-name" (B) + +A name for the full (possibly composite hash-and-signature) signature +algorithm. +The provider may, but is not obligated to, provide a signature implementation +with this name; if it doesn't, this is assumed to be a composite of a pure +signature algorithm and a hash algorithm, which must be given with the +parameters "sig-name" and "hash-name". +This value must be supplied. + +=item "sigalg-oid" (B) + +The OID of the "sigalg-name" algorithm in canonical numeric text form. If +this parameter is given, OBJ_create() will be used to create an OBJ and +a NID for this OID, using the "sigalg-name" parameter for its (short) name. +Otherwise, it's assumed to already exist in the object database, possibly +done by the provider with the core_obj_create() upcall. +This value is optional. + +=item "sig-name" (B) + +The name of the pure signature algorithm that is part of a composite +"sigalg-name". If "sigalg-name" is implemented by the provider, this +parameter is redundant and must not be given. +This value is optional. + +=item "sig-oid" (B) + +The OID of the "sig-name" algorithm in canonical numeric text form. If +this parameter is given, OBJ_create() will be used to create an OBJ and +a NID for this OID, using the "sig-name" parameter for its (short) name. +Otherwise, it is assumed to already exist in the object database. This +can be done by the provider using the core_obj_create() upcall. +This value is optional. + +=item "hash-name" (B) + +The name of the hash algorithm that is part of a composite "sigalg-name". +If "sigalg-name" is implemented by the provider, this parameter is redundant +and must not be given. +This value is optional. + +=item "hash-oid" (B) + +The OID of the "hash-name" algorithm in canonical numeric text form. If +this parameter is given, OBJ_create() will be used to create an OBJ and +a NID for this OID, using the "hash-name" parameter for its (short) name. +Otherwise, it's assumed to already exist in the object database, possibly +done by the provider with the core_obj_create() upcall. +This value is optional. + +=item "key-type" (B) + +The key type of the public key of applicable certificates. If this parameter +isn't present, it's assumed to be the same as "sig-name" if that's present, +otherwise "sigalg-name". +This value is optional. + +=item "key-type-oid" (B) + +The OID of the "key-type" in canonical numeric text form. If +this parameter is given, OBJ_create() will be used to create an OBJ and +a NID for this OID, using the "key-type" parameter for its (short) name. +Otherwise, it's assumed to already exist in the object database, possibly +done by the provider with the core_obj_create() upcall. +This value is optional. + +=item "sec-bits" (B) + +The number of bits of security offered by keys of this algorithm. The number +of bits should be comparable with the ones given in table 2 and 3 of the NIST +SP800-57 document. This number is used to determine the security strength of +the algorithm if no digest algorithm has been registered that otherwise +defines the security strength. If the signature algorithm implements its own +digest internally, this value needs to be set to properly reflect the overall +security strength. +This value must be supplied. + +=item "tls-min-tls" (B) + +=item "tls-max-tls" (B) + +These parameters can be used to describe the minimum and maximum TLS +versions supported by the signature algorithm. The values equate to the +on-the-wire encoding of the various TLS versions. For example TLSv1.3 is +0x0304 (772 decimal), and TLSv1.2 is 0x0303 (771 decimal). A 0 indicates that +there is no defined minimum or maximum. A -1 indicates that the signature +algorithm should not be used in that protocol. +Presently values representing anything other than TLS1.3 mean that the +complete algorithm is ignored. + +=back + =head1 NOTES The core_obj_create() and core_obj_add_sigid() functions were not thread safe diff --git a/include/openssl/core_names.h b/include/openssl/core_names.h index 2cde6cdb94..173a81d28b 100644 --- a/include/openssl/core_names.h +++ b/include/openssl/core_names.h @@ -540,6 +540,21 @@ extern "C" { #define OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS "tls-min-dtls" #define OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS "tls-max-dtls" +/* TLS-SIGALG Capability */ +#define OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME "tls-sigalg-iana-name" +#define OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT "tls-sigalg-code-point" +#define OSSL_CAPABILITY_TLS_SIGALG_NAME "tls-sigalg-name" +#define OSSL_CAPABILITY_TLS_SIGALG_OID "tls-sigalg-oid" +#define OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME "tls-sigalg-sig-name" +#define OSSL_CAPABILITY_TLS_SIGALG_SIG_OID "tls-sigalg-sig-oid" +#define OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME "tls-sigalg-hash-name" +#define OSSL_CAPABILITY_TLS_SIGALG_HASH_OID "tls-sigalg-hash-oid" +#define OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE "tls-sigalg-keytype" +#define OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID "tls-sigalg-keytype-oid" +#define OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS "tls-sigalg-sec-bits" +#define OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS "tls-min-tls" +#define OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS "tls-max-tls" + /*- * storemgmt parameters */ diff --git a/ssl/s3_lib.c b/ssl/s3_lib.c index e7078efa6c..17e318b857 100644 --- a/ssl/s3_lib.c +++ b/ssl/s3_lib.c @@ -3366,6 +3366,7 @@ void ssl3_free(SSL *s) OPENSSL_clear_free(sc->s3.tmp.pms, sc->s3.tmp.pmslen); OPENSSL_free(sc->s3.tmp.peer_sigalgs); OPENSSL_free(sc->s3.tmp.peer_cert_sigalgs); + OPENSSL_free(sc->s3.tmp.valid_flags); ssl3_free_digest_list(sc); OPENSSL_free(sc->s3.alpn_selected); OPENSSL_free(sc->s3.alpn_proposed); @@ -3390,6 +3391,7 @@ int ssl3_clear(SSL *s) OPENSSL_clear_free(sc->s3.tmp.pms, sc->s3.tmp.pmslen); OPENSSL_free(sc->s3.tmp.peer_sigalgs); OPENSSL_free(sc->s3.tmp.peer_cert_sigalgs); + OPENSSL_free(sc->s3.tmp.valid_flags); EVP_PKEY_free(sc->s3.tmp.pkey); EVP_PKEY_free(sc->s3.peer_tmp); @@ -4244,7 +4246,7 @@ const SSL_CIPHER *ssl3_choose_cipher(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *cl if (SSL_CONNECTION_IS_TLS13(s)) { #ifndef OPENSSL_NO_PSK - int j; + size_t j; /* * If we allow "old" style PSK callbacks, and we have no certificate (so @@ -4254,8 +4256,8 @@ const SSL_CIPHER *ssl3_choose_cipher(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *cl * that. */ if (s->psk_server_callback != NULL) { - for (j = 0; j < SSL_PKEY_NUM && !ssl_has_cert(s, j); j++); - if (j == SSL_PKEY_NUM) { + for (j = 0; j < s->ssl_pkey_num && !ssl_has_cert(s, j); j++); + if (j == s->ssl_pkey_num) { /* There are no certificates */ prefer_sha256 = 1; } diff --git a/ssl/ssl_cert.c b/ssl/ssl_cert.c index 320f6d322e..1c4f452997 100644 --- a/ssl/ssl_cert.c +++ b/ssl/ssl_cert.c @@ -58,13 +58,25 @@ int SSL_get_ex_data_X509_STORE_CTX_idx(void) return ssl_x509_store_ctx_idx; } -CERT *ssl_cert_new(void) +CERT *ssl_cert_new(size_t ssl_pkey_num) { - CERT *ret = OPENSSL_zalloc(sizeof(*ret)); + CERT *ret = NULL; + + /* Should never happen */ + if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM)) + return NULL; + ret = OPENSSL_zalloc(sizeof(*ret)); if (ret == NULL) return NULL; + ret->ssl_pkey_num = ssl_pkey_num; + ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY)); + if (ret->pkeys == NULL) { + OPENSSL_free(ret); + return NULL; + } + ret->key = &(ret->pkeys[SSL_PKEY_RSA]); ret->references = 1; ret->sec_cb = ssl_security_default_callback; @@ -73,6 +85,7 @@ CERT *ssl_cert_new(void) ret->lock = CRYPTO_THREAD_lock_new(); if (ret->lock == NULL) { ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); + OPENSSL_free(ret->pkeys); OPENSSL_free(ret); return NULL; } @@ -83,7 +96,7 @@ CERT *ssl_cert_new(void) CERT *ssl_cert_dup(CERT *cert) { CERT *ret = OPENSSL_zalloc(sizeof(*ret)); - int i; + size_t i; #ifndef OPENSSL_NO_COMP_ALG int j; #endif @@ -91,6 +104,11 @@ CERT *ssl_cert_dup(CERT *cert) if (ret == NULL) return NULL; + ret->ssl_pkey_num = cert->ssl_pkey_num; + ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY)); + if (ret->pkeys == NULL) + return NULL; + ret->references = 1; ret->key = &ret->pkeys[cert->key - cert->pkeys]; ret->lock = CRYPTO_THREAD_lock_new(); @@ -108,7 +126,7 @@ CERT *ssl_cert_dup(CERT *cert) ret->dh_tmp_cb = cert->dh_tmp_cb; ret->dh_tmp_auto = cert->dh_tmp_auto; - for (i = 0; i < SSL_PKEY_NUM; i++) { + for (i = 0; i < ret->ssl_pkey_num; i++) { CERT_PKEY *cpk = cert->pkeys + i; CERT_PKEY *rpk = ret->pkeys + i; @@ -217,14 +235,14 @@ CERT *ssl_cert_dup(CERT *cert) void ssl_cert_clear_certs(CERT *c) { - int i; + size_t i; #ifndef OPENSSL_NO_COMP_ALG int j; #endif if (c == NULL) return; - for (i = 0; i < SSL_PKEY_NUM; i++) { + for (i = 0; i < c->ssl_pkey_num; i++) { CERT_PKEY *cpk = c->pkeys + i; X509_free(cpk->x509); cpk->x509 = NULL; @@ -269,6 +287,7 @@ void ssl_cert_free(CERT *c) #ifndef OPENSSL_NO_PSK OPENSSL_free(c->psk_identity_hint); #endif + OPENSSL_free(c->pkeys); CRYPTO_THREAD_lock_free(c->lock); OPENSSL_free(c); } @@ -297,6 +316,7 @@ int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain) int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain) { STACK_OF(X509) *dchain; + if (!chain) return ssl_cert_set0_chain(s, ctx, NULL); dchain = X509_chain_up_ref(chain); @@ -313,6 +333,7 @@ int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x) { int r; CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; + if (!cpk) return 0; r = ssl_security_cert(s, ctx, x, 0, 0); @@ -337,10 +358,11 @@ int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x) int ssl_cert_select_current(CERT *c, X509 *x) { - int i; + size_t i; + if (x == NULL) return 0; - for (i = 0; i < SSL_PKEY_NUM; i++) { + for (i = 0; i < c->ssl_pkey_num; i++) { CERT_PKEY *cpk = c->pkeys + i; if (cpk->x509 == x && cpk->privatekey) { c->key = cpk; @@ -348,7 +370,7 @@ int ssl_cert_select_current(CERT *c, X509 *x) } } - for (i = 0; i < SSL_PKEY_NUM; i++) { + for (i = 0; i < c->ssl_pkey_num; i++) { CERT_PKEY *cpk = c->pkeys + i; if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { c->key = cpk; @@ -360,18 +382,19 @@ int ssl_cert_select_current(CERT *c, X509 *x) int ssl_cert_set_current(CERT *c, long op) { - int i, idx; + size_t i, idx; + if (!c) return 0; if (op == SSL_CERT_SET_FIRST) idx = 0; else if (op == SSL_CERT_SET_NEXT) { - idx = (int)(c->key - c->pkeys + 1); - if (idx >= SSL_PKEY_NUM) + idx = (size_t)(c->key - c->pkeys + 1); + if (idx >= c->ssl_pkey_num) return 0; } else return 0; - for (i = idx; i < SSL_PKEY_NUM; i++) { + for (i = idx; i < c->ssl_pkey_num; i++) { CERT_PKEY *cpk = c->pkeys + i; if (cpk->x509 && cpk->privatekey) { c->key = cpk; @@ -1156,7 +1179,7 @@ int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other) ctx->cert->sec_ex); } -int ssl_cert_lookup_by_nid(int nid, size_t *pidx) +int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx) { size_t i; @@ -1166,16 +1189,22 @@ int ssl_cert_lookup_by_nid(int nid, size_t *pidx) return 1; } } - + for (i = 0; i < ctx->sigalg_list_len; i++) { + if (ctx->ssl_cert_info[i].nid == nid) { + *pidx = SSL_PKEY_NUM + i; + return 1; + } + } return 0; } -const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx) +SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx) { size_t i; + /* check classic pk types */ for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) { - const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i]; + SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i]; if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid)) || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) { @@ -1184,13 +1213,26 @@ const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx) return tmp_lu; } } + /* check provider-loaded pk types */ + for (i = 0; ctx->sigalg_list_len; i++) { + SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]); + + if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid)) + || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) { + if (pidx != NULL) + *pidx = SSL_PKEY_NUM + i; + return &ctx->ssl_cert_info[i]; + } + } return NULL; } -const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx) +SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx) { - if (idx >= OSSL_NELEM(ssl_cert_info)) + if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len)) return NULL; + else if (idx >= (OSSL_NELEM(ssl_cert_info))) + return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]); return &ssl_cert_info[idx]; } diff --git a/ssl/ssl_cert_table.h b/ssl/ssl_cert_table.h index f66c5fe390..e70d9d624e 100644 --- a/ssl/ssl_cert_table.h +++ b/ssl/ssl_cert_table.h @@ -10,7 +10,7 @@ /* * Certificate table information. NB: table entries must match SSL_PKEY indices */ -static const SSL_CERT_LOOKUP ssl_cert_info [] = { +static SSL_CERT_LOOKUP ssl_cert_info [] = { {EVP_PKEY_RSA, SSL_aRSA}, /* SSL_PKEY_RSA */ {EVP_PKEY_RSA_PSS, SSL_aRSA}, /* SSL_PKEY_RSA_PSS_SIGN */ {EVP_PKEY_DSA, SSL_aDSS}, /* SSL_PKEY_DSA_SIGN */ diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c index 0ea998d383..a2b12a8811 100644 --- a/ssl/ssl_ciph.c +++ b/ssl/ssl_ciph.c @@ -2155,6 +2155,16 @@ int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c) return ssl_cipher_table_auth[i].nid; } +int ssl_get_md_idx(int md_nid) { + int i; + + for(i = 0; i < SSL_MD_NUM_IDX; i++) { + if (md_nid == ssl_cipher_table_mac[i].nid) + return i; + } + return -1; +} + const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c) { int idx = c->algorithm2 & SSL_HANDSHAKE_MAC_MASK; @@ -2223,8 +2233,13 @@ int ssl_cipher_get_overhead(const SSL_CIPHER *c, size_t *mac_overhead, int ssl_cert_is_disabled(SSL_CTX *ctx, size_t idx) { - const SSL_CERT_LOOKUP *cl = ssl_cert_lookup_by_idx(idx); + SSL_CERT_LOOKUP *cl; + + /* A provider-loaded key type is always enabled */ + if (idx >= SSL_PKEY_NUM) + return 0; + cl = ssl_cert_lookup_by_idx(idx, ctx); if (cl == NULL || (cl->amask & ctx->disabled_auth_mask) != 0) return 1; return 0; diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c index a2e2666962..1b2c527eb0 100644 --- a/ssl/ssl_lib.c +++ b/ssl/ssl_lib.c @@ -890,6 +890,7 @@ SSL *ossl_ssl_connection_new_int(SSL_CTX *ctx, const SSL_METHOD *method) goto sslerr; #endif + s->ssl_pkey_num = SSL_PKEY_NUM + ctx->sigalg_list_len; return ssl; cerr: ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); @@ -1432,6 +1433,7 @@ void ossl_ssl_connection_free(SSL *ssl) s->wbio = NULL; BIO_free_all(s->rbio); s->rbio = NULL; + OPENSSL_free(s->s3.tmp.valid_flags); } void SSL_set0_rbio(SSL *s, BIO *rbio) @@ -3778,10 +3780,6 @@ SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq, ret->session_timeout = meth->get_timeout(); ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; ret->verify_mode = SSL_VERIFY_NONE; - if ((ret->cert = ssl_cert_new()) == NULL) { - ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); - goto err; - } ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); if (ret->sessions == NULL) { @@ -3802,20 +3800,38 @@ SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq, #endif /* initialize cipher/digest methods table */ - if (!ssl_load_ciphers(ret)) + if (!ssl_load_ciphers(ret)) { + ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); goto err; - /* initialise sig algs */ - if (!ssl_setup_sig_algs(ret)) + } + + if (!ssl_load_groups(ret)) { + ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); + goto err; + } + + /* load provider sigalgs */ + if (!ssl_load_sigalgs(ret)) { + ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); goto err; + } - if (!ssl_load_groups(ret)) + /* initialise sig algs */ + if (!ssl_setup_sigalgs(ret)) { + ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); goto err; + } if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites())) { ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); goto err; } + if ((ret->cert = ssl_cert_new(SSL_PKEY_NUM + ret->sigalg_list_len)) == NULL) { + ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB); + goto err; + } + if (!ssl_create_cipher_list(ret, ret->tls13_ciphersuites, &ret->cipher_list, &ret->cipher_list_by_id, @@ -4057,8 +4073,22 @@ void SSL_CTX_free(SSL_CTX *a) OPENSSL_free(a->group_list[j].algorithm); } OPENSSL_free(a->group_list); + for (j = 0; j < a->sigalg_list_len; j++) { + OPENSSL_free(a->sigalg_list[j].name); + OPENSSL_free(a->sigalg_list[j].sigalg_name); + OPENSSL_free(a->sigalg_list[j].sigalg_oid); + OPENSSL_free(a->sigalg_list[j].sig_name); + OPENSSL_free(a->sigalg_list[j].sig_oid); + OPENSSL_free(a->sigalg_list[j].hash_name); + OPENSSL_free(a->sigalg_list[j].hash_oid); + OPENSSL_free(a->sigalg_list[j].keytype); + OPENSSL_free(a->sigalg_list[j].keytype_oid); + } + OPENSSL_free(a->sigalg_list); + OPENSSL_free(a->ssl_cert_info); OPENSSL_free(a->sigalg_lookup_cache); + OPENSSL_free(a->tls12_sigalgs); CRYPTO_THREAD_lock_free(a->lock); #ifdef TSAN_REQUIRES_LOCKING diff --git a/ssl/ssl_local.h b/ssl/ssl_local.h index e82564daf6..87fb1fd7cf 100644 --- a/ssl/ssl_local.h +++ b/ssl/ssl_local.h @@ -755,6 +755,31 @@ typedef struct tls_group_info_st { char is_kem; /* Mode for this Group: 0 is KEX, 1 is KEM */ } TLS_GROUP_INFO; +typedef struct tls_sigalg_info_st { + char *name; /* name as in IANA TLS specs */ + uint16_t code_point; /* IANA-specified code point of sigalg-name */ + char *sigalg_name; /* (combined) sigalg name */ + char *sigalg_oid; /* (combined) sigalg OID */ + char *sig_name; /* pure signature algorithm name */ + char *sig_oid; /* pure signature algorithm OID */ + char *hash_name; /* hash algorithm name */ + char *hash_oid; /* hash algorithm OID */ + char *keytype; /* keytype name */ + char *keytype_oid; /* keytype OID */ + unsigned int secbits; /* Bits of security (from SP800-57) */ + int mintls; /* Minimum TLS version, -1 unsupported */ + int maxtls; /* Maximum TLS version (or 0 for undefined) */ +} TLS_SIGALG_INFO; + +/* + * Structure containing table entry of certificate info corresponding to + * CERT_PKEY entries + */ +typedef struct { + int nid; /* NID of public key algorithm */ + uint32_t amask; /* authmask corresponding to key type */ +} SSL_CERT_LOOKUP; + /* flags values */ # define TLS_GROUP_TYPE 0x0000000FU /* Mask for group type */ # define TLS_GROUP_CURVE_PRIME 0x00000001U @@ -901,6 +926,7 @@ struct ssl_ctx_st { size_t max_cert_list; struct cert_st /* CERT */ *cert; + SSL_CERT_LOOKUP *ssl_cert_info; int read_ahead; /* callback that allows applications to peek at protocol messages */ @@ -1120,13 +1146,20 @@ struct ssl_ctx_st { const EVP_MD *ssl_digest_methods[SSL_MD_NUM_IDX]; size_t ssl_mac_secret_size[SSL_MD_NUM_IDX]; + size_t tls12_sigalgs_len; /* Cache of all sigalgs we know and whether they are available or not */ struct sigalg_lookup_st *sigalg_lookup_cache; + /* List of all sigalgs (code points) available, incl. from providers */ + uint16_t *tls12_sigalgs; TLS_GROUP_INFO *group_list; size_t group_list_len; size_t group_list_max_len; + TLS_SIGALG_INFO *sigalg_list; + size_t sigalg_list_len; + size_t sigalg_list_max_len; + /* masks of disabled algorithms */ uint32_t disabled_enc_mask; uint32_t disabled_mac_mask; @@ -1210,6 +1243,8 @@ struct ssl_connection_st { size_t init_num; /* amount read/written */ size_t init_off; /* amount read/written */ + size_t ssl_pkey_num; + struct { long flags; unsigned char server_random[SSL3_RANDOM_SIZE]; @@ -1244,6 +1279,7 @@ struct ssl_connection_st { int total_renegotiations; int num_renegotiations; int in_read_app_data; + struct { /* actually only need to be 16+20 for SSLv3 and 12 for TLS */ unsigned char finish_md[EVP_MAX_MD_SIZE * 2]; @@ -1307,7 +1343,7 @@ struct ssl_connection_st { * SSL session: e.g. appropriate curve, signature algorithms etc. * If zero it can't be used at all. */ - uint32_t valid_flags[SSL_PKEY_NUM]; + uint32_t *valid_flags; /* * For servers the following masks are for the key and auth algorithms * that are supported by the certs below. For clients they are masks of @@ -1794,15 +1830,6 @@ typedef struct sigalg_lookup_st { int enabled; } SIGALG_LOOKUP; -/* - * Structure containing table entry of certificate info corresponding to - * CERT_PKEY entries - */ -typedef struct { - int nid; /* NID of public key algorithm */ - uint32_t amask; /* authmask corresponding to key type */ -} SSL_CERT_LOOKUP; - /* DTLS structures */ # ifndef OPENSSL_NO_SCTP @@ -2000,7 +2027,8 @@ typedef struct cert_st { int dh_tmp_auto; /* Flags related to certificates */ uint32_t cert_flags; - CERT_PKEY pkeys[SSL_PKEY_NUM]; + CERT_PKEY *pkeys; + size_t ssl_pkey_num; /* Custom certificate types sent in certificate request message. */ uint8_t *ctype; size_t ctype_len; @@ -2355,7 +2383,7 @@ const char *ssl_protocol_to_string(int version); /* Returns true if certificate and private key for 'idx' are present */ static ossl_inline int ssl_has_cert(const SSL_CONNECTION *s, int idx) { - if (idx < 0 || idx >= SSL_PKEY_NUM) + if (idx < 0 || idx >= (int)s->ssl_pkey_num) return 0; return s->cert->pkeys[idx].x509 != NULL && s->cert->pkeys[idx].privatekey != NULL; @@ -2381,7 +2409,7 @@ __owur int ossl_ssl_connection_reset(SSL *ssl); __owur int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes); __owur int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written); int ssl_clear_bad_session(SSL_CONNECTION *s); -__owur CERT *ssl_cert_new(void); +__owur CERT *ssl_cert_new(size_t ssl_pkey_num); __owur CERT *ssl_cert_dup(CERT *cert); void ssl_cert_clear_certs(CERT *c); void ssl_cert_free(CERT *c); @@ -2444,10 +2472,11 @@ __owur int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other); int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp); -__owur int ssl_cert_lookup_by_nid(int nid, size_t *pidx); -__owur const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, - size_t *pidx); -__owur const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx); +__owur int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx); +__owur SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, + size_t *pidx, + SSL_CTX *ctx); +__owur SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx); int ssl_undefined_function(SSL *s); __owur int ssl_undefined_void_function(void); @@ -2460,8 +2489,9 @@ __owur STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *sc); __owur int ssl_x509err2alert(int type); void ssl_sort_cipher_list(void); int ssl_load_ciphers(SSL_CTX *ctx); -__owur int ssl_setup_sig_algs(SSL_CTX *ctx); +__owur int ssl_setup_sigalgs(SSL_CTX *ctx); int ssl_load_groups(SSL_CTX *ctx); +int ssl_load_sigalgs(SSL_CTX *ctx); __owur int ssl_fill_hello_random(SSL_CONNECTION *s, int server, unsigned char *field, size_t len, DOWNGRADE dgrd); @@ -2750,6 +2780,7 @@ __owur int ssl_handshake_hash(SSL_CONNECTION *s, unsigned char *out, size_t outlen, size_t *hashlen); __owur const EVP_MD *ssl_md(SSL_CTX *ctx, int idx); +int ssl_get_md_idx(int md_nid); __owur const EVP_MD *ssl_handshake_md(SSL_CONNECTION *s); __owur const EVP_MD *ssl_prf_md(SSL_CONNECTION *s); diff --git a/ssl/ssl_rsa.c b/ssl/ssl_rsa.c index 76466b55b9..3d4ad85a62 100644 --- a/ssl/ssl_rsa.c +++ b/ssl/ssl_rsa.c @@ -17,8 +17,8 @@ #include #include -static int ssl_set_cert(CERT *c, X509 *x509); -static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey); +static int ssl_set_cert(CERT *c, X509 *x509, SSL_CTX *ctx); +static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey, SSL_CTX *ctx); #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \ | SSL_EXT_CLIENT_HELLO \ @@ -47,7 +47,7 @@ int SSL_use_certificate(SSL *ssl, X509 *x) return 0; } - return ssl_set_cert(sc->cert, x); + return ssl_set_cert(sc->cert, x, SSL_CONNECTION_GET_CTX(sc)); } int SSL_use_certificate_file(SSL *ssl, const char *file, int type) @@ -128,11 +128,11 @@ int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len) return ret; } -static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) +static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey, SSL_CTX *ctx) { size_t i; - if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) { + if (ssl_cert_lookup_by_pkey(pkey, &i, ctx) == NULL) { ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); return 0; } @@ -160,7 +160,7 @@ int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); return 0; } - ret = ssl_set_pkey(sc->cert, pkey); + ret = ssl_set_pkey(sc->cert, pkey, SSL_CONNECTION_GET_CTX(sc)); return ret; } @@ -243,10 +243,10 @@ int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) ERR_raise(ERR_LIB_SSL, rv); return 0; } - return ssl_set_cert(ctx->cert, x); + return ssl_set_cert(ctx->cert, x, ctx); } -static int ssl_set_cert(CERT *c, X509 *x) +static int ssl_set_cert(CERT *c, X509 *x, SSL_CTX *ctx) { EVP_PKEY *pkey; size_t i; @@ -257,7 +257,7 @@ static int ssl_set_cert(CERT *c, X509 *x) return 0; } - if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) { + if (ssl_cert_lookup_by_pkey(pkey, &i, ctx) == NULL) { ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); return 0; } @@ -371,7 +371,7 @@ int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); return 0; } - return ssl_set_pkey(ctx->cert, pkey); + return ssl_set_pkey(ctx->cert, pkey, ctx); } int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) @@ -1010,7 +1010,7 @@ static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *pr goto out; } } - if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) { + if (ssl_cert_lookup_by_pkey(pubkey, &i, ctx) == NULL) { ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); goto out; } diff --git a/ssl/statem/statem_clnt.c b/ssl/statem/statem_clnt.c index f1ed43abd3..e5b036390a 100644 --- a/ssl/statem/statem_clnt.c +++ b/ssl/statem/statem_clnt.c @@ -1988,7 +1988,8 @@ WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s, return WORK_ERROR; } - if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx)) == NULL) { + if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx, + SSL_CONNECTION_GET_CTX(s))) == NULL) { SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE); return WORK_ERROR; } @@ -2434,11 +2435,15 @@ MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt) MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s, PACKET *pkt) { - size_t i; - /* Clear certificate validity flags */ - for (i = 0; i < SSL_PKEY_NUM; i++) - s->s3.tmp.valid_flags[i] = 0; + if (s->s3.tmp.valid_flags != NULL) + memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); + else + s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t)); + + /* Give up for good if allocation didn't work */ + if (s->s3.tmp.valid_flags == NULL) + return 0; if (SSL_CONNECTION_IS_TLS13(s)) { PACKET reqctx, extensions; @@ -3768,7 +3773,8 @@ int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s) return 1; /* This is the passed certificate */ - clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx); + clu = ssl_cert_lookup_by_pkey(X509_get0_pubkey(s->session->peer), &idx, + SSL_CONNECTION_GET_CTX(s)); /* Check certificate is recognised and suitable for cipher */ if (clu == NULL || (alg_a & clu->amask) == 0) { diff --git a/ssl/statem/statem_lib.c b/ssl/statem/statem_lib.c index ebedbeefbb..1bc01e1d25 100644 --- a/ssl/statem/statem_lib.c +++ b/ssl/statem/statem_lib.c @@ -468,7 +468,7 @@ MSG_PROCESS_RETURN tls_process_cert_verify(SSL_CONNECTION *s, PACKET *pkt) goto err; } - if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) { + if (ssl_cert_lookup_by_pkey(pkey, NULL, sctx) == NULL) { SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); goto err; @@ -1602,7 +1602,7 @@ static int ssl_method_error(const SSL_CONNECTION *s, const SSL_METHOD *method) */ static int is_tls13_capable(const SSL_CONNECTION *s) { - int i; + size_t i; int curve; SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); @@ -1625,7 +1625,8 @@ static int is_tls13_capable(const SSL_CONNECTION *s) if (s->psk_find_session_cb != NULL || s->cert->cert_cb != NULL) return 1; - for (i = 0; i < SSL_PKEY_NUM; i++) { + /* All provider-based sig algs are required to support at least TLS1.3 */ + for (i = 0; i < s->ssl_pkey_num; i++) { /* Skip over certs disallowed for TLSv1.3 */ switch (i) { case SSL_PKEY_DSA_SIGN: diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c index 25aaa9b56d..5622532314 100644 --- a/ssl/t1_lib.c +++ b/ssl/t1_lib.c @@ -222,7 +222,7 @@ static const uint16_t suiteb_curves[] = { OSSL_TLS_GROUP_ID_secp384r1, }; -struct provider_group_data_st { +struct provider_ctx_data_st { SSL_CTX *ctx; OSSL_PROVIDER *provider; }; @@ -231,7 +231,7 @@ struct provider_group_data_st { static OSSL_CALLBACK add_provider_groups; static int add_provider_groups(const OSSL_PARAM params[], void *data) { - struct provider_group_data_st *pgd = data; + struct provider_ctx_data_st *pgd = data; SSL_CTX *ctx = pgd->ctx; OSSL_PROVIDER *provider = pgd->provider; const OSSL_PARAM *p; @@ -375,7 +375,7 @@ static int add_provider_groups(const OSSL_PARAM params[], void *data) static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx) { - struct provider_group_data_st pgd; + struct provider_ctx_data_st pgd; pgd.ctx = vctx; pgd.provider = provider; @@ -417,6 +417,319 @@ int ssl_load_groups(SSL_CTX *ctx) return 1; } +#define TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE 10 +static OSSL_CALLBACK add_provider_sigalgs; +static int add_provider_sigalgs(const OSSL_PARAM params[], void *data) +{ + struct provider_ctx_data_st *pgd = data; + SSL_CTX *ctx = pgd->ctx; + OSSL_PROVIDER *provider = pgd->provider; + const OSSL_PARAM *p; + TLS_SIGALG_INFO *sinf = NULL; + EVP_KEYMGMT *keymgmt; + const char *keytype; + unsigned int code_point = 0; + int ret = 0; + + if (ctx->sigalg_list_max_len == ctx->sigalg_list_len) { + TLS_SIGALG_INFO *tmp = NULL; + + if (ctx->sigalg_list_max_len == 0) + tmp = OPENSSL_malloc(sizeof(TLS_SIGALG_INFO) + * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE); + else + tmp = OPENSSL_realloc(ctx->sigalg_list, + (ctx->sigalg_list_max_len + + TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE) + * sizeof(TLS_SIGALG_INFO)); + if (tmp == NULL) + return 0; + ctx->sigalg_list = tmp; + memset(tmp + ctx->sigalg_list_max_len, 0, + sizeof(TLS_SIGALG_INFO) * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE); + ctx->sigalg_list_max_len += TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE; + } + + sinf = &ctx->sigalg_list[ctx->sigalg_list_len]; + + /* First, mandatory parameters */ + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_NAME); + if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + OPENSSL_free(sinf->sigalg_name); + sinf->sigalg_name = OPENSSL_strdup(p->data); + if (sinf->sigalg_name == NULL) + goto err; + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME); + if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + OPENSSL_free(sinf->name); + sinf->name = OPENSSL_strdup(p->data); + if (sinf->name == NULL) + goto err; + + p = OSSL_PARAM_locate_const(params, + OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT); + if (p == NULL + || !OSSL_PARAM_get_uint(p, &code_point) + || code_point > UINT16_MAX) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + sinf->code_point = (uint16_t)code_point; + + p = OSSL_PARAM_locate_const(params, + OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS); + if (p == NULL || !OSSL_PARAM_get_uint(p, &sinf->secbits)) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + + /* Now, optional parameters */ + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_OID); + if (p == NULL) { + sinf->sigalg_oid = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->sigalg_oid); + sinf->sigalg_oid = OPENSSL_strdup(p->data); + if (sinf->sigalg_oid == NULL) + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME); + if (p == NULL) { + sinf->sig_name = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->sig_name); + sinf->sig_name = OPENSSL_strdup(p->data); + if (sinf->sig_name == NULL) + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_OID); + if (p == NULL) { + sinf->sig_oid = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->sig_oid); + sinf->sig_oid = OPENSSL_strdup(p->data); + if (sinf->sig_oid == NULL) + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME); + if (p == NULL) { + sinf->hash_name = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->hash_name); + sinf->hash_name = OPENSSL_strdup(p->data); + if (sinf->hash_name == NULL) + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_OID); + if (p == NULL) { + sinf->hash_oid = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->hash_oid); + sinf->hash_oid = OPENSSL_strdup(p->data); + if (sinf->hash_oid == NULL) + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE); + if (p == NULL) { + sinf->keytype = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->keytype); + sinf->keytype = OPENSSL_strdup(p->data); + if (sinf->keytype == NULL) + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID); + if (p == NULL) { + sinf->keytype_oid = NULL; + } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { + goto err; + } else { + OPENSSL_free(sinf->keytype_oid); + sinf->keytype_oid = OPENSSL_strdup(p->data); + if (sinf->keytype_oid == NULL) + goto err; + } + + /* The remaining parameters below are mandatory again */ + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS); + if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->mintls)) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + if ((sinf->mintls != 0) && (sinf->mintls != -1) && + ((sinf->mintls < TLS1_3_VERSION))) { + /* ignore this sigalg as this OpenSSL doesn't know how to handle it */ + ret = 1; + goto err; + } + + p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS); + if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->maxtls)) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && + ((sinf->maxtls < sinf->mintls))) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && + ((sinf->maxtls < TLS1_3_VERSION))) { + /* ignore this sigalg as this OpenSSL doesn't know how to handle it */ + ret = 1; + goto err; + } + + /* + * Now check that the algorithm is actually usable for our property query + * string. Regardless of the result we still return success because we have + * successfully processed this signature, even though we may decide not to + * use it. + */ + ret = 1; + ERR_set_mark(); + keytype = (sinf->keytype != NULL + ? sinf->keytype + : (sinf->sig_name != NULL + ? sinf->sig_name + : sinf->sigalg_name)); + keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, keytype, ctx->propq); + if (keymgmt != NULL) { + /* + * We have successfully fetched the algorithm - however if the provider + * doesn't match this one then we ignore it. + * + * Note: We're cheating a little here. Technically if the same algorithm + * is available from more than one provider then it is undefined which + * implementation you will get back. Theoretically this could be + * different every time...we assume here that you'll always get the + * same one back if you repeat the exact same fetch. Is this a reasonable + * assumption to make (in which case perhaps we should document this + * behaviour)? + */ + if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) { + /* + * We have a match - so we could use this signature; + * Check proper object registration first, though. + * Don't care about return value as this may have been + * done within providers or previous calls to + * add_provider_sigalgs. + */ + OBJ_create(sinf->sigalg_oid, sinf->sigalg_name, NULL); + /* sanity check: Without successful registration don't use alg */ + if ((OBJ_txt2nid(sinf->sigalg_name) == NID_undef) || + (OBJ_nid2obj(OBJ_txt2nid(sinf->sigalg_name)) == NULL)) { + ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + if (sinf->sig_name != NULL) + OBJ_create(sinf->sig_oid, sinf->sig_name, NULL); + if (sinf->keytype != NULL) + OBJ_create(sinf->keytype_oid, sinf->keytype, NULL); + if (sinf->hash_name != NULL) + OBJ_create(sinf->hash_oid, sinf->hash_name, NULL); + OBJ_add_sigid(OBJ_txt2nid(sinf->sigalg_name), + (sinf->hash_name != NULL + ? OBJ_txt2nid(sinf->hash_name) + : NID_undef), + OBJ_txt2nid(keytype)); + ctx->sigalg_list_len++; + sinf = NULL; + } + EVP_KEYMGMT_free(keymgmt); + } + ERR_pop_to_mark(); + err: + if (sinf != NULL) { + OPENSSL_free(sinf->name); + sinf->name = NULL; + OPENSSL_free(sinf->sigalg_name); + sinf->sigalg_name = NULL; + OPENSSL_free(sinf->sigalg_oid); + sinf->sigalg_oid = NULL; + OPENSSL_free(sinf->sig_name); + sinf->sig_name = NULL; + OPENSSL_free(sinf->sig_oid); + sinf->sig_oid = NULL; + OPENSSL_free(sinf->hash_name); + sinf->hash_name = NULL; + OPENSSL_free(sinf->hash_oid); + sinf->hash_oid = NULL; + OPENSSL_free(sinf->keytype); + sinf->keytype = NULL; + OPENSSL_free(sinf->keytype_oid); + sinf->keytype_oid = NULL; + } + return ret; +} + +static int discover_provider_sigalgs(OSSL_PROVIDER *provider, void *vctx) +{ + struct provider_ctx_data_st pgd; + + pgd.ctx = vctx; + pgd.provider = provider; + OSSL_PROVIDER_get_capabilities(provider, "TLS-SIGALG", + add_provider_sigalgs, &pgd); + /* + * Always OK, even if provider doesn't support the capability: + * Reconsider testing retval when legacy sigalgs are also loaded this way. + */ + return 1; +} + +int ssl_load_sigalgs(SSL_CTX *ctx) +{ + size_t i; + SSL_CERT_LOOKUP lu; + + if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_sigalgs, ctx)) + return 0; + + /* now populate ctx->ssl_cert_info */ + if (ctx->sigalg_list_len > 0) { + ctx->ssl_cert_info = OPENSSL_zalloc(sizeof(lu) * ctx->sigalg_list_len); + if (ctx->ssl_cert_info == NULL) + return 0; + for(i = 0; i < ctx->sigalg_list_len; i++) { + ctx->ssl_cert_info[i].nid = OBJ_txt2nid(ctx->sigalg_list[i].sigalg_name); + ctx->ssl_cert_info[i].amask = SSL_aANY; + } + } + + /* + * For now, leave it at this: legacy sigalgs stay in their own + * data structures until "legacy cleanup" occurs. + */ + + return 1; +} + static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name) { size_t i; @@ -1153,24 +1466,36 @@ static const uint16_t tls_default_sigalg[] = { 0, /* SSL_PKEY_ED448 */ }; -int ssl_setup_sig_algs(SSL_CTX *ctx) +int ssl_setup_sigalgs(SSL_CTX *ctx) { - size_t i; + size_t i, cache_idx, sigalgs_len; const SIGALG_LOOKUP *lu; - SIGALG_LOOKUP *cache - = OPENSSL_malloc(sizeof(*lu) * OSSL_NELEM(sigalg_lookup_tbl)); + SIGALG_LOOKUP *cache = NULL; + uint16_t *tls12_sigalgs_list = NULL; EVP_PKEY *tmpkey = EVP_PKEY_new(); int ret = 0; + if (ctx == NULL) + goto err; + + sigalgs_len = OSSL_NELEM(sigalg_lookup_tbl) + ctx->sigalg_list_len; + + cache = OPENSSL_malloc(sizeof(const SIGALG_LOOKUP) * sigalgs_len); if (cache == NULL || tmpkey == NULL) goto err; + tls12_sigalgs_list = OPENSSL_malloc(sizeof(uint16_t) * sigalgs_len); + if (tls12_sigalgs_list == NULL) + goto err; + ERR_set_mark(); + /* First fill cache and tls12_sigalgs list from legacy algorithm list */ for (i = 0, lu = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { EVP_PKEY_CTX *pctx; cache[i] = *lu; + tls12_sigalgs_list[i] = tls12_sigalgs[i]; /* * Check hash is available. @@ -1196,13 +1521,35 @@ int ssl_setup_sig_algs(SSL_CTX *ctx) cache[i].enabled = 0; EVP_PKEY_CTX_free(pctx); } + + /* Now complete cache and tls12_sigalgs list with provider sig information */ + cache_idx = OSSL_NELEM(sigalg_lookup_tbl); + for (i = 0; i < ctx->sigalg_list_len; i++) { + TLS_SIGALG_INFO si = ctx->sigalg_list[i]; + cache[cache_idx].name = si.name; + cache[cache_idx].sigalg = si.code_point; + tls12_sigalgs_list[cache_idx] = si.code_point; + cache[cache_idx].hash = si.hash_name?OBJ_txt2nid(si.hash_name):NID_undef; + cache[cache_idx].hash_idx = ssl_get_md_idx(cache[cache_idx].hash); + cache[cache_idx].sig = OBJ_txt2nid(si.sigalg_name); + cache[cache_idx].sig_idx = i + SSL_PKEY_NUM; + cache[cache_idx].sigandhash = OBJ_txt2nid(si.sigalg_name); + cache[cache_idx].curve = NID_undef; + /* all provided sigalgs are enabled by load */ + cache[cache_idx].enabled = 1; + cache_idx++; + } ERR_pop_to_mark(); ctx->sigalg_lookup_cache = cache; + ctx->tls12_sigalgs = tls12_sigalgs_list; + ctx->tls12_sigalgs_len = sigalgs_len; cache = NULL; + tls12_sigalgs_list = NULL; ret = 1; err: OPENSSL_free(cache); + OPENSSL_free(tls12_sigalgs_list); EVP_PKEY_free(tmpkey); return ret; } @@ -1215,8 +1562,7 @@ static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL_CONNECTION *s, const SIGALG_LOOKUP *lu; for (i = 0, lu = SSL_CONNECTION_GET_CTX(s)->sigalg_lookup_cache; - /* cache should have the same number of elements as sigalg_lookup_tbl */ - i < OSSL_NELEM(sigalg_lookup_tbl); + i < SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; lu++, i++) { if (lu->sigalg == sigalg) { if (!lu->enabled) @@ -1283,8 +1629,9 @@ static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s, size_t i; /* Work out index corresponding to ciphersuite */ - for (i = 0; i < SSL_PKEY_NUM; i++) { - const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i); + for (i = 0; i < s->ssl_pkey_num; i++) { + const SSL_CERT_LOOKUP *clu + = ssl_cert_lookup_by_idx(i, SSL_CONNECTION_GET_CTX(s)); if (clu == NULL) continue; @@ -1329,6 +1676,7 @@ static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s, } if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) return NULL; + if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, tls_default_sigalg[idx]); @@ -1350,7 +1698,7 @@ int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey) size_t idx; const SIGALG_LOOKUP *lu; - if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) + if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) return 0; lu = tls1_get_legacy_sigalg(s, idx); if (lu == NULL) @@ -1390,8 +1738,8 @@ size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent, const uint16_t **psigs) *psigs = s->cert->conf_sigalgs; return s->cert->conf_sigalgslen; } else { - *psigs = tls12_sigalgs; - return OSSL_NELEM(tls12_sigalgs); + *psigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; + return SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; } } @@ -1408,8 +1756,8 @@ int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve) sigs = s->cert->conf_sigalgs; siglen = s->cert->conf_sigalgslen; } else { - sigs = tls12_sigalgs; - siglen = OSSL_NELEM(tls12_sigalgs); + sigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; + siglen = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; } for (i = 0; i < siglen; i++) { @@ -1466,6 +1814,14 @@ static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu) else if (lu->sigalg == TLSEXT_SIGALG_ed448) secbits = 224; } + /* + * For provider-based sigalgs we have secbits information available + * in the (provider-loaded) sigalg_list structure + */ + if ((secbits == 0) && (lu->sig_idx >= SSL_PKEY_NUM) + && ((lu->sig_idx - SSL_PKEY_NUM) < (int)ctx->sigalg_list_len)) { + secbits = ctx->sigalg_list[lu->sig_idx - SSL_PKEY_NUM].secbits; + } return secbits; } @@ -1485,9 +1841,7 @@ int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) int secbits = 0; pkeyid = EVP_PKEY_get_id(pkey); - /* Should never happen */ - if (pkeyid == -1) - return -1; + if (SSL_CONNECTION_IS_TLS13(s)) { /* Disallow DSA for TLS 1.3 */ if (pkeyid == EVP_PKEY_DSA) { @@ -1499,6 +1853,14 @@ int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) pkeyid = EVP_PKEY_RSA_PSS; } lu = tls1_lookup_sigalg(s, sig); + /* if this sigalg is loaded, set so far unknown pkeyid to its sig NID */ + if ((pkeyid == -1) && (lu != NULL)) + pkeyid = lu->sig; + + /* Should never happen */ + if (pkeyid == -1) + return -1; + /* * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type * is consistent with signature: RSA keys can be used for RSA-PSS @@ -1512,7 +1874,9 @@ int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) return 0; } /* Check the sigalg is consistent with the key OID */ - if (!ssl_cert_lookup_by_nid(EVP_PKEY_get_id(pkey), &cidx) + if (!ssl_cert_lookup_by_nid( + (pkeyid == EVP_PKEY_RSA_PSS) ? EVP_PKEY_get_id(pkey) : pkeyid, + &cidx, SSL_CONNECTION_GET_CTX(s)) || lu->sig_idx != (int)cidx) { SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); return 0; @@ -1706,9 +2070,14 @@ int tls1_set_server_sigalgs(SSL_CONNECTION *s) OPENSSL_free(s->shared_sigalgs); s->shared_sigalgs = NULL; s->shared_sigalgslen = 0; + /* Clear certificate validity flags */ - for (i = 0; i < SSL_PKEY_NUM; i++) - s->s3.tmp.valid_flags[i] = 0; + if (s->s3.tmp.valid_flags) + memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); + else + s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t)); + if (s->s3.tmp.valid_flags == NULL) + return 0; /* * If peer sent no signature algorithms check to see if we support * the default algorithm for each certificate type @@ -1718,7 +2087,7 @@ int tls1_set_server_sigalgs(SSL_CONNECTION *s) const uint16_t *sent_sigs; size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); - for (i = 0; i < SSL_PKEY_NUM; i++) { + for (i = 0; i < s->ssl_pkey_num; i++) { const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); size_t j; @@ -2172,7 +2541,8 @@ void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op) if (lu == NULL) continue; - clu = ssl_cert_lookup_by_idx(lu->sig_idx); + clu = ssl_cert_lookup_by_idx(lu->sig_idx, + SSL_CONNECTION_GET_CTX(s)); if (clu == NULL) continue; @@ -2345,7 +2715,7 @@ int tls1_process_sigalgs(SSL_CONNECTION *s) if (!tls1_set_shared_sigalgs(s)) return 0; - for (i = 0; i < SSL_PKEY_NUM; i++) + for (i = 0; i < s->ssl_pkey_num; i++) pvalid[i] = 0; for (i = 0; i < s->shared_sigalgslen; i++) { @@ -2699,7 +3069,8 @@ int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk, if (!x || !pk) return 0; - if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL) + if (ssl_cert_lookup_by_pkey(pk, &certidx, + SSL_CONNECTION_GET_CTX(s)) == NULL) return 0; idx = certidx; pvalid = s->s3.tmp.valid_flags + idx; @@ -3105,7 +3476,8 @@ static int tls12_get_cert_sigalg_idx(const SSL_CONNECTION *s, const SIGALG_LOOKUP *lu) { int sig_idx = lu->sig_idx; - const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx); + const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx, + SSL_CONNECTION_GET_CTX(s)); /* If not recognised or not supported by cipher mask it is not suitable */ if (clu == NULL @@ -3203,7 +3575,7 @@ static int is_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, X509 *x, { size_t idx; - if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) + if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) return 0; /* Check the key is consistent with the sig alg */ 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 #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include "internal/asn1.h" /* For TLS1_3_VERSION */ #include #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; - } - - /* Register our 2 groups */ - ret = cb(xor_group_params, arg); - ret &= cb(xor_kemgroup_params, arg); + if (strcmp(capability, "TLS-GROUP") == 0) { + /* 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) +{ + 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_freekey(void *keydata) { - return OPENSSL_zalloc(sizeof(XORKEY)); + 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 void xor_freedata(void *keydata) +static int xor_key_up_ref(XORKEY *key) { - OPENSSL_free(keydata); + 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,101 +1093,2160 @@ static const OSSL_DISPATCH xor_keymgmt_functions[] = { { 0, NULL } }; -static const OSSL_ALGORITHM tls_prov_keymgmt[] = { - /* - * Obviously this is not FIPS approved, but in order to test in conjunction - * with the FIPS provider we pretend that it is. - */ - { "XOR", "provider=tls-provider,fips=yes", xor_keymgmt_functions }, - { NULL, NULL, NULL } -}; - -static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id, - int *no_cache) +/* We're re-using most XOR keymgmt functions also for signature operations: */ +static void *xor_xorhmacsig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { - *no_cache = 0; - switch (operation_id) { - case OSSL_OP_KEYMGMT: - return tls_prov_keymgmt; - case OSSL_OP_KEYEXCH: - return tls_prov_keyexch; - case OSSL_OP_KEM: - return tls_prov_kem; + 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 NULL; + return k; } -static void tls_prov_teardown(void *provctx) +static void *xor_xorhmacsha2sig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { - int i; + XORKEY* k = xor_gen(genctx, osslcb, cbarg); - OSSL_LIB_CTX_free(provctx); - - for (i = 0; i < NUM_DUMMY_GROUPS; i++) { - OPENSSL_free(dummy_group_names[i]); - dummy_group_names[i] = NULL; + 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; } -/* Functions we provide to the core */ -static const OSSL_DISPATCH tls_prov_dispatch_table[] = { - { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))tls_prov_teardown }, - { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query }, - { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities }, + +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 -unsigned int randomize_tls_group_id(OSSL_LIB_CTX *libctx) +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) { - /* - * Randomise the group_id we're going to use to ensure we don't interoperate - * with anything but ourselves. - */ - unsigned int group_id; - static unsigned int mem[10] = { 0 }; - static int in_mem = 0; - int i; + XORKEY *key = NULL; + int nid = NID_undef; - retry: - if (RAND_bytes_ex(libctx, (unsigned char *)&group_id, sizeof(group_id), 0) <= 0) + 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; - /* - * Ensure group_id is within the IANA Reserved for private use range - * (65024-65279) - */ - group_id %= 65279 - 65024; - group_id += 65024; + } - /* Ensure we did not already issue this group_id */ - for (i = 0; i < in_mem; i++) - if (mem[i] == group_id) - goto retry; + 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) { + memcpy(key->pubkey, p, plen); + key->haspubkey = 1; + } else { + memcpy(key->privkey, p, plen); + key->hasprivkey = 1; + } - /* Add this group_id to the list of ids issued by this function */ - mem[in_mem++] = group_id; + key->tls_name = OPENSSL_strdup(OBJ_nid2sn(nid)); + if (key->tls_name == NULL) + goto err; + return key; - return group_id; + err: + xor_freekey(key); + return NULL; } -int tls_provider_init(const OSSL_CORE_HANDLE *handle, - const OSSL_DISPATCH *in, - const OSSL_DISPATCH **out, - void **provctx) +static XORKEY *xor_key_from_x509pubkey(const X509_PUBKEY *xpk, + OSSL_LIB_CTX *libctx, const char *propq) +{ + const unsigned char *p; + int plen; + X509_ALGOR *palg; + + if (!xpk || (!X509_PUBKEY_get0_param(NULL, &p, &plen, &palg, xpk))) { + return NULL; + } + return xor_key_op(palg, p, plen, KEY_OP_PUBLIC, libctx, propq); +} + +static XORKEY *xor_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, + OSSL_LIB_CTX *libctx, const char *propq) { - OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); + XORKEY *xork = NULL; + const unsigned char *p; + int plen; + ASN1_OCTET_STRING *oct = NULL; + const X509_ALGOR *palg; - if (libctx == NULL) + if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf)) return 0; - *provctx = libctx; + oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen); + if (oct == NULL) { + p = NULL; + plen = 0; + } else { + p = ASN1_STRING_get0_data(oct); + plen = ASN1_STRING_length(oct); + } + + xork = xor_key_op(palg, p, plen, KEY_OP_PRIVATE, + libctx, propq); + ASN1_OCTET_STRING_free(oct); + return xork; +} +static const OSSL_ALGORITHM tls_prov_keymgmt[] = { /* - * Randomise the group_id we're going to use to ensure we don't interoperate - * with anything but ourselves. + * Obviously this is not FIPS approved, but in order to test in conjunction + * with the FIPS provider we pretend that it is. */ - xor_group.group_id = randomize_tls_group_id(libctx); - xor_kemgroup.group_id = randomize_tls_group_id(libctx); + { "XOR", "provider=tls-provider,fips=yes", + xor_keymgmt_functions }, + { XORSIGALG_NAME, "provider=tls-provider,fips=yes", + xor_xorhmacsig_keymgmt_functions }, + { XORSIGALG_HASH_NAME, + "provider=tls-provider,fips=yes", + xor_xorhmacsha2sig_keymgmt_functions }, + { NULL, NULL, NULL } +}; + +struct key2any_ctx_st { + PROV_XOR_CTX *provctx; + + /* Set to 0 if parameters should not be saved (dsa only) */ + int save_parameters; + + /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */ + int cipher_intent; + + EVP_CIPHER *cipher; + + OSSL_PASSPHRASE_CALLBACK *pwcb; + void *pwcbarg; +}; + +typedef int check_key_type_fn(const void *key, int nid); +typedef int key_to_paramstring_fn(const void *key, int nid, int save, + void **str, int *strtype); +typedef int key_to_der_fn(BIO *out, const void *key, + int key_nid, const char *pemname, + key_to_paramstring_fn *p2s, i2d_of_void *k2d, + struct key2any_ctx_st *ctx); +typedef int write_bio_of_void_fn(BIO *bp, const void *x); + + +/* Free the blob allocated during key_to_paramstring_fn */ +static void free_asn1_data(int type, void *data) +{ + switch(type) { + case V_ASN1_OBJECT: + ASN1_OBJECT_free(data); + break; + case V_ASN1_SEQUENCE: + ASN1_STRING_free(data); + break; + } +} + +static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid, + void *params, int params_type, + i2d_of_void *k2d) +{ + /* der, derlen store the key DER output and its length */ + unsigned char *der = NULL; + int derlen; + /* The final PKCS#8 info */ + PKCS8_PRIV_KEY_INFO *p8info = NULL; + + if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL + || (derlen = k2d(key, &der)) <= 0 + || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0, + V_ASN1_UNDEF, NULL, + der, derlen)) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + PKCS8_PRIV_KEY_INFO_free(p8info); + OPENSSL_free(der); + p8info = NULL; + } + + return p8info; +} + +static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info, + struct key2any_ctx_st *ctx) +{ + X509_SIG *p8 = NULL; + char kstr[PEM_BUFSIZE]; + size_t klen = 0; + OSSL_LIB_CTX *libctx = PROV_XOR_LIBCTX_OF(ctx->provctx); + + if (ctx->cipher == NULL || ctx->pwcb == NULL) + return NULL; + + if (!ctx->pwcb(kstr, PEM_BUFSIZE, &klen, NULL, ctx->pwcbarg)) { + ERR_raise(ERR_LIB_USER, PROV_R_UNABLE_TO_GET_PASSPHRASE); + return NULL; + } + /* First argument == -1 means "standard" */ + p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info, libctx, NULL); + OPENSSL_cleanse(kstr, klen); + return p8; +} + +static X509_SIG *key_to_encp8(const void *key, int key_nid, + void *params, int params_type, + i2d_of_void *k2d, struct key2any_ctx_st *ctx) +{ + PKCS8_PRIV_KEY_INFO *p8info = + key_to_p8info(key, key_nid, params, params_type, k2d); + X509_SIG *p8 = NULL; + + if (p8info == NULL) { + free_asn1_data(params_type, params); + } else { + p8 = p8info_to_encp8(p8info, ctx); + PKCS8_PRIV_KEY_INFO_free(p8info); + } + return p8; +} + +static X509_PUBKEY *xorx_key_to_pubkey(const void *key, int key_nid, + void *params, int params_type, + i2d_of_void k2d) +{ + /* der, derlen store the key DER output and its length */ + unsigned char *der = NULL; + int derlen; + /* The final X509_PUBKEY */ + X509_PUBKEY *xpk = NULL; + + if ((xpk = X509_PUBKEY_new()) == NULL + || (derlen = k2d(key, &der)) <= 0 + || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid), + V_ASN1_UNDEF, NULL, + der, derlen)) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + X509_PUBKEY_free(xpk); + OPENSSL_free(der); + xpk = NULL; + } + + return xpk; +} + +/* + * key_to_epki_* produce encoded output with the private key data in a + * EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require + * that there's an intent to encrypt, anything else is an error. + * + * key_to_pki_* primarly produce encoded output with the private key data + * in a PrivateKeyInfo structure (also defined by PKCS#8). However, if + * there is an intent to encrypt the data, the corresponding key_to_epki_* + * function is used instead. + * + * key_to_spki_* produce encoded output with the public key data in an + * X.509 SubjectPublicKeyInfo. + * + * Key parameters don't have any defined envelopment of this kind, but are + * included in some manner in the output from the functions described above, + * either in the AlgorithmIdentifier's parameter field, or as part of the + * key data itself. + */ + +static int key_to_epki_der_priv_bio(BIO *out, const void *key, + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) +{ + int ret = 0; + void *str = NULL; + int strtype = V_ASN1_UNDEF; + X509_SIG *p8; + + if (!ctx->cipher_intent) + return 0; + + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, + &str, &strtype)) + return 0; + + p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); + if (p8 != NULL) + ret = i2d_PKCS8_bio(out, p8); + + X509_SIG_free(p8); + + return ret; +} + +static int key_to_epki_pem_priv_bio(BIO *out, const void *key, + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) +{ + int ret = 0; + void *str = NULL; + int strtype = V_ASN1_UNDEF; + X509_SIG *p8; + + if (!ctx->cipher_intent) + return 0; + + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, + &str, &strtype)) + return 0; + + p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); + if (p8 != NULL) + ret = PEM_write_bio_PKCS8(out, p8); + + X509_SIG_free(p8); + + return ret; +} + +static int key_to_pki_der_priv_bio(BIO *out, const void *key, + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) +{ + int ret = 0; + void *str = NULL; + int strtype = V_ASN1_UNDEF; + PKCS8_PRIV_KEY_INFO *p8info; + + if (ctx->cipher_intent) + return key_to_epki_der_priv_bio(out, key, key_nid, pemname, + p2s, k2d, ctx); + + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, + &str, &strtype)) + return 0; + + p8info = key_to_p8info(key, key_nid, str, strtype, k2d); + + if (p8info != NULL) + ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info); + else + free_asn1_data(strtype, str); + + PKCS8_PRIV_KEY_INFO_free(p8info); + + return ret; +} + +static int key_to_pki_pem_priv_bio(BIO *out, const void *key, + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) +{ + int ret = 0; + void *str = NULL; + int strtype = V_ASN1_UNDEF; + PKCS8_PRIV_KEY_INFO *p8info; + + if (ctx->cipher_intent) + return key_to_epki_pem_priv_bio(out, key, key_nid, pemname, + p2s, k2d, ctx); + + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, + &str, &strtype)) + return 0; + + p8info = key_to_p8info(key, key_nid, str, strtype, k2d); + + if (p8info != NULL) + ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info); + else + free_asn1_data(strtype, str); + + PKCS8_PRIV_KEY_INFO_free(p8info); + + return ret; +} + +static int key_to_spki_der_pub_bio(BIO *out, const void *key, + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) +{ + int ret = 0; + X509_PUBKEY *xpk = NULL; + void *str = NULL; + int strtype = V_ASN1_UNDEF; + + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, + &str, &strtype)) + return 0; + + xpk = xorx_key_to_pubkey(key, key_nid, str, strtype, k2d); + + if (xpk != NULL) + ret = i2d_X509_PUBKEY_bio(out, xpk); + + X509_PUBKEY_free(xpk); + return ret; +} + +static int key_to_spki_pem_pub_bio(BIO *out, const void *key, + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) +{ + int ret = 0; + X509_PUBKEY *xpk = NULL; + void *str = NULL; + int strtype = V_ASN1_UNDEF; + + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, + &str, &strtype)) + return 0; + + xpk = xorx_key_to_pubkey(key, key_nid, str, strtype, k2d); + + if (xpk != NULL) + ret = PEM_write_bio_X509_PUBKEY(out, xpk); + else + free_asn1_data(strtype, str); + + /* Also frees |str| */ + X509_PUBKEY_free(xpk); + return ret; +} + +/* ---------------------------------------------------------------------- */ + +static int prepare_xorx_params(const void *xorxkey, int nid, int save, + void **pstr, int *pstrtype) +{ + ASN1_OBJECT *params = NULL; + XORKEY *k = (XORKEY*)xorxkey; + + if (k->tls_name && OBJ_sn2nid(k->tls_name) != nid) { + ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_KEY); + return 0; + } + + if (nid == NID_undef) { + ERR_raise(ERR_LIB_USER, XORPROV_R_MISSING_OID); + return 0; + } + + params = OBJ_nid2obj(nid); + + if (params == NULL || OBJ_length(params) == 0) { + /* unexpected error */ + ERR_raise(ERR_LIB_USER, XORPROV_R_MISSING_OID); + ASN1_OBJECT_free(params); + return 0; + } + *pstr = params; + *pstrtype = V_ASN1_OBJECT; + return 1; +} + +static int xorx_spki_pub_to_der(const void *vecxkey, unsigned char **pder) +{ + const XORKEY *xorxkey = vecxkey; + unsigned char *keyblob; + int retlen; + + if (xorxkey == NULL) { + ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + keyblob = OPENSSL_memdup(xorxkey->pubkey, retlen = XOR_KEY_SIZE); + if (keyblob == NULL) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + return 0; + } + + *pder = keyblob; + return retlen; +} + +static int xorx_pki_priv_to_der(const void *vecxkey, unsigned char **pder) +{ + XORKEY *xorxkey = (XORKEY *)vecxkey; + unsigned char* buf = NULL; + ASN1_OCTET_STRING oct; + int keybloblen; + + if (xorxkey == NULL) { + ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + buf = OPENSSL_secure_malloc(XOR_KEY_SIZE); + memcpy(buf, xorxkey->privkey, XOR_KEY_SIZE); + + oct.data = buf; + oct.length = XOR_KEY_SIZE; + oct.flags = 0; + + keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder); + if (keybloblen < 0) { + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + keybloblen = 0; + } + + OPENSSL_secure_clear_free(buf, XOR_KEY_SIZE); + return keybloblen; +} + +# define xorx_epki_priv_to_der xorx_pki_priv_to_der + +/* + * XORX only has PKCS#8 / SubjectPublicKeyInfo + * representation, so we don't define xorx_type_specific_[priv,pub,params]_to_der. + */ + +# define xorx_check_key_type NULL + +# define xorhmacsig_evp_type 0 +# define xorhmacsig_input_type XORSIGALG_NAME +# define xorhmacsig_pem_type XORSIGALG_NAME +# define xorhmacsha2sig_evp_type 0 +# define xorhmacsha2sig_input_type XORSIGALG_HASH_NAME +# define xorhmacsha2sig_pem_type XORSIGALG_HASH_NAME + +/* ---------------------------------------------------------------------- */ + +static OSSL_FUNC_decoder_newctx_fn key2any_newctx; +static OSSL_FUNC_decoder_freectx_fn key2any_freectx; + +static void *key2any_newctx(void *provctx) +{ + struct key2any_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); + + if (ctx != NULL) { + ctx->provctx = provctx; + ctx->save_parameters = 1; + } + + return ctx; +} + +static void key2any_freectx(void *vctx) +{ + struct key2any_ctx_st *ctx = vctx; + + EVP_CIPHER_free(ctx->cipher); + OPENSSL_free(ctx); +} + +static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx) +{ + static const OSSL_PARAM settables[] = { + OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0), + OSSL_PARAM_END, + }; + + return settables; +} + +static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[]) +{ + struct key2any_ctx_st *ctx = vctx; + OSSL_LIB_CTX *libctx = PROV_XOR_LIBCTX_OF(ctx->provctx); + const OSSL_PARAM *cipherp = + OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER); + const OSSL_PARAM *propsp = + OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES); + const OSSL_PARAM *save_paramsp = + OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS); + + if (cipherp != NULL) { + const char *ciphername = NULL; + const char *props = NULL; + + if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername)) + return 0; + if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props)) + return 0; + + EVP_CIPHER_free(ctx->cipher); + ctx->cipher = NULL; + ctx->cipher_intent = ciphername != NULL; + if (ciphername != NULL + && ((ctx->cipher = + EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL)) { + return 0; + } + } + + if (save_paramsp != NULL) { + if (!OSSL_PARAM_get_int(save_paramsp, &ctx->save_parameters)) { + return 0; + } + } + return 1; +} + +static int key2any_check_selection(int selection, int selection_mask) +{ + /* + * The selections are kinda sorta "levels", i.e. each selection given + * here is assumed to include those following. + */ + int checks[] = { + OSSL_KEYMGMT_SELECT_PRIVATE_KEY, + OSSL_KEYMGMT_SELECT_PUBLIC_KEY, + OSSL_KEYMGMT_SELECT_ALL_PARAMETERS + }; + size_t i; + + /* The decoder implementations made here support guessing */ + if (selection == 0) + return 1; + + for (i = 0; i < OSSL_NELEM(checks); i++) { + int check1 = (selection & checks[i]) != 0; + int check2 = (selection_mask & checks[i]) != 0; + + /* + * If the caller asked for the currently checked bit(s), return + * whether the decoder description says it's supported. + */ + if (check1) + return check2; + } + + /* This should be dead code, but just to be safe... */ + return 0; +} + +static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, + const void *key, const char* typestr, const char *pemname, + key_to_der_fn *writer, + OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg, + key_to_paramstring_fn *key2paramstring, + i2d_of_void *key2der) +{ + int ret = 0; + int type = OBJ_sn2nid(typestr); + + if (key == NULL || type <= 0) { + ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER); + } else if (writer != NULL) { + BIO *out = BIO_new_from_core_bio(ctx->provctx->libctx, cout); + + if (out != NULL) { + ctx->pwcb = pwcb; + ctx->pwcbarg = pwcbarg; + + ret = writer(out, key, type, pemname, key2paramstring, key2der, ctx); + } + + BIO_free(out); + } else { + ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); + } + return ret; +} + +#define DO_ENC_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY +#define DO_ENC_PRIVATE_KEY(impl, type, kind, output) \ + if ((selection & DO_ENC_PRIVATE_KEY_selection_mask) != 0) \ + return key2any_encode(ctx, cout, key, impl##_pem_type, \ + impl##_pem_type " PRIVATE KEY", \ + key_to_##kind##_##output##_priv_bio, \ + cb, cbarg, prepare_##type##_params, \ + type##_##kind##_priv_to_der); + +#define DO_ENC_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY +#define DO_ENC_PUBLIC_KEY(impl, type, kind, output) \ + if ((selection & DO_ENC_PUBLIC_KEY_selection_mask) != 0) \ + return key2any_encode(ctx, cout, key, impl##_pem_type, \ + impl##_pem_type " PUBLIC KEY", \ + key_to_##kind##_##output##_pub_bio, \ + cb, cbarg, prepare_##type##_params, \ + type##_##kind##_pub_to_der); + +#define DO_ENC_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS +#define DO_ENC_PARAMETERS(impl, type, kind, output) \ + if ((selection & DO_ENC_PARAMETERS_selection_mask) != 0) \ + return key2any_encode(ctx, cout, key, impl##_pem_type, \ + impl##_pem_type " PARAMETERS", \ + key_to_##kind##_##output##_param_bio, \ + NULL, NULL, NULL, \ + type##_##kind##_params_to_der); + +/*- + * Implement the kinds of output structure that can be produced. They are + * referred to by name, and for each name, the following macros are defined + * (braces not included): + * + * DO_{kind}_selection_mask + * + * A mask of selection bits that must not be zero. This is used as a + * selection criterion for each implementation. + * This mask must never be zero. + * + * DO_{kind} + * + * The performing macro. It must use the DO_ macros defined above, + * always in this order: + * + * - DO_PRIVATE_KEY + * - DO_PUBLIC_KEY + * - DO_PARAMETERS + * + * Any of those may be omitted, but the relative order must still be + * the same. + */ + +/* + * PKCS#8 defines two structures for private keys only: + * - PrivateKeyInfo (raw unencrypted form) + * - EncryptedPrivateKeyInfo (encrypted wrapping) + * + * To allow a certain amount of flexibility, we allow the routines + * for PrivateKeyInfo to also produce EncryptedPrivateKeyInfo if a + * passphrase callback has been passed to them. + */ +#define DO_ENC_PrivateKeyInfo_selection_mask DO_ENC_PRIVATE_KEY_selection_mask +#define DO_ENC_PrivateKeyInfo(impl, type, output) \ + DO_ENC_PRIVATE_KEY(impl, type, pki, output) + +#define DO_ENC_EncryptedPrivateKeyInfo_selection_mask DO_ENC_PRIVATE_KEY_selection_mask +#define DO_ENC_EncryptedPrivateKeyInfo(impl, type, output) \ + DO_ENC_PRIVATE_KEY(impl, type, epki, output) + +/* SubjectPublicKeyInfo is a structure for public keys only */ +#define DO_ENC_SubjectPublicKeyInfo_selection_mask DO_ENC_PUBLIC_KEY_selection_mask +#define DO_ENC_SubjectPublicKeyInfo(impl, type, output) \ + DO_ENC_PUBLIC_KEY(impl, type, spki, output) + +/* + * MAKE_ENCODER is the single driver for creating OSSL_DISPATCH tables. + * It takes the following arguments: + * + * impl This is the key type name that's being implemented. + * type This is the type name for the set of functions that implement + * the key type. For example, ed25519, ed448, x25519 and x448 + * are all implemented with the exact same set of functions. + * kind What kind of support to implement. These translate into + * the DO_##kind macros above. + * output The output type to implement. may be der or pem. + * + * The resulting OSSL_DISPATCH array gets the following name (expressed in + * C preprocessor terms) from those arguments: + * + * xor_##impl##_to_##kind##_##output##_encoder_functions + */ +#define MAKE_ENCODER(impl, type, kind, output) \ + static OSSL_FUNC_encoder_import_object_fn \ + impl##_to_##kind##_##output##_import_object; \ + static OSSL_FUNC_encoder_free_object_fn \ + impl##_to_##kind##_##output##_free_object; \ + static OSSL_FUNC_encoder_encode_fn \ + impl##_to_##kind##_##output##_encode; \ + \ + static void * \ + impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \ + const OSSL_PARAM params[]) \ + { \ + struct key2any_ctx_st *ctx = vctx; \ + \ + return xor_prov_import_key(xor_##impl##_keymgmt_functions, \ + ctx->provctx, selection, params); \ + } \ + static void impl##_to_##kind##_##output##_free_object(void *key) \ + { \ + xor_prov_free_key(xor_##impl##_keymgmt_functions, key); \ + } \ + static int impl##_to_##kind##_##output##_does_selection(void *ctx, \ + int selection) \ + { \ + return key2any_check_selection(selection, \ + DO_ENC_##kind##_selection_mask); \ + } \ + static int \ + impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \ + const void *key, \ + const OSSL_PARAM key_abstract[], \ + int selection, \ + OSSL_PASSPHRASE_CALLBACK *cb, \ + void *cbarg) \ + { \ + /* We don't deal with abstract objects */ \ + if (key_abstract != NULL) { \ + ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); \ + return 0; \ + } \ + DO_ENC_##kind(impl, type, output) \ + \ + ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); \ + return 0; \ + } \ + static const OSSL_DISPATCH \ + xor_##impl##_to_##kind##_##output##_encoder_functions[] = { \ + { OSSL_FUNC_ENCODER_NEWCTX, \ + (void (*)(void))key2any_newctx }, \ + { OSSL_FUNC_ENCODER_FREECTX, \ + (void (*)(void))key2any_freectx }, \ + { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ + (void (*)(void))key2any_settable_ctx_params }, \ + { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \ + (void (*)(void))key2any_set_ctx_params }, \ + { OSSL_FUNC_ENCODER_DOES_SELECTION, \ + (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \ + { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ + (void (*)(void))impl##_to_##kind##_##output##_import_object }, \ + { OSSL_FUNC_ENCODER_FREE_OBJECT, \ + (void (*)(void))impl##_to_##kind##_##output##_free_object }, \ + { OSSL_FUNC_ENCODER_ENCODE, \ + (void (*)(void))impl##_to_##kind##_##output##_encode }, \ + { 0, NULL } \ + } + +/* + * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey, + * i2d_{TYPE}params, as they exist. + */ + +/* + * PKCS#8 and SubjectPublicKeyInfo support. This may duplicate some of the + * implementations specified above, but are more specific. + * The SubjectPublicKeyInfo implementations also replace the + * PEM_write_bio_{TYPE}_PUBKEY functions. + * For PEM, these are expected to be used by PEM_write_bio_PrivateKey(), + * PEM_write_bio_PUBKEY() and PEM_write_bio_Parameters(). + */ + +MAKE_ENCODER(xorhmacsig, xorx, EncryptedPrivateKeyInfo, der); +MAKE_ENCODER(xorhmacsig, xorx, EncryptedPrivateKeyInfo, pem); +MAKE_ENCODER(xorhmacsig, xorx, PrivateKeyInfo, der); +MAKE_ENCODER(xorhmacsig, xorx, PrivateKeyInfo, pem); +MAKE_ENCODER(xorhmacsig, xorx, SubjectPublicKeyInfo, der); +MAKE_ENCODER(xorhmacsig, xorx, SubjectPublicKeyInfo, pem); +MAKE_ENCODER(xorhmacsha2sig, xorx, EncryptedPrivateKeyInfo, der); +MAKE_ENCODER(xorhmacsha2sig, xorx, EncryptedPrivateKeyInfo, pem); +MAKE_ENCODER(xorhmacsha2sig, xorx, PrivateKeyInfo, der); +MAKE_ENCODER(xorhmacsha2sig, xorx, PrivateKeyInfo, pem); +MAKE_ENCODER(xorhmacsha2sig, xorx, SubjectPublicKeyInfo, der); +MAKE_ENCODER(xorhmacsha2sig, xorx, SubjectPublicKeyInfo, pem); + +static const OSSL_ALGORITHM tls_prov_encoder[] = { +#define ENCODER_PROVIDER "tls-provider" +#ifndef ENCODER_PROVIDER +# error Macro ENCODER_PROVIDER undefined +#endif + +#define ENCODER_STRUCTURE_PKCS8 "pkcs8" +#define ENCODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo" +#define ENCODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo" +#define ENCODER_STRUCTURE_EncryptedPrivateKeyInfo "EncryptedPrivateKeyInfo" +#define ENCODER_STRUCTURE_PKCS1 "pkcs1" +#define ENCODER_STRUCTURE_PKCS3 "pkcs3" + +/* Arguments are prefixed with '_' to avoid build breaks on certain platforms */ +/* + * Obviously this is not FIPS approved, but in order to test in conjunction + * with the FIPS provider we pretend that it is. + */ +#define ENCODER_TEXT(_name, _sym) \ + { _name, \ + "provider=" ENCODER_PROVIDER ",fips=yes,output=text", \ + (xor_##_sym##_to_text_encoder_functions) } +#define ENCODER(_name, _sym, _fips, _output) \ + { _name, \ + "provider=" ENCODER_PROVIDER ",fips=yes,output=" #_output, \ + (xor_##_sym##_to_##_output##_encoder_functions) } + +#define ENCODER_w_structure(_name, _sym, _output, _structure) \ + { _name, \ + "provider=" ENCODER_PROVIDER ",fips=yes,output=" #_output \ + ",structure=" ENCODER_STRUCTURE_##_structure, \ + (xor_##_sym##_to_##_structure##_##_output##_encoder_functions) } + +/* + * Entries for human text "encoders" + */ + +/* + * Entries for PKCS#8 and SubjectPublicKeyInfo. + * The "der" ones are added convenience for any user that wants to use + * OSSL_ENCODER directly. + * The "pem" ones also support PEM_write_bio_PrivateKey() and + * PEM_write_bio_PUBKEY(). + */ + +ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, PrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, PrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, EncryptedPrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, EncryptedPrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, SubjectPublicKeyInfo), +ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, SubjectPublicKeyInfo), +ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, + der, PrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, + pem, PrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, + der, EncryptedPrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, + pem, EncryptedPrivateKeyInfo), +ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, + der, SubjectPublicKeyInfo), +ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig, + pem, SubjectPublicKeyInfo), +#undef ENCODER_PROVIDER + { NULL, NULL, NULL } +}; + +struct der2key_ctx_st; /* Forward declaration */ +typedef int check_key_fn(void *, struct der2key_ctx_st *ctx); +typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx); +typedef void free_key_fn(void *); +typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long, + struct der2key_ctx_st *); +struct keytype_desc_st { + const char *keytype_name; + const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */ + + /* The input structure name */ + const char *structure_name; + + /* + * The EVP_PKEY_xxx type macro. Should be zero for type specific + * structures, non-zero when the outermost structure is PKCS#8 or + * SubjectPublicKeyInfo. This determines which of the function + * pointers below will be used. + */ + int evp_type; + + /* The selection mask for OSSL_FUNC_decoder_does_selection() */ + int selection_mask; + + /* For type specific decoders, we use the corresponding d2i */ + d2i_of_void *d2i_private_key; /* From type-specific DER */ + d2i_of_void *d2i_public_key; /* From type-specific DER */ + d2i_of_void *d2i_key_params; /* From type-specific DER */ + d2i_PKCS8_fn *d2i_PKCS8; /* Wrapped in a PrivateKeyInfo */ + d2i_of_void *d2i_PUBKEY; /* Wrapped in a SubjectPublicKeyInfo */ + + /* + * For any key, we may need to check that the key meets expectations. + * This is useful when the same functions can decode several variants + * of a key. + */ + check_key_fn *check_key; + + /* + * For any key, we may need to make provider specific adjustments, such + * as ensure the key carries the correct library context. + */ + adjust_key_fn *adjust_key; + /* {type}_free() */ + free_key_fn *free_key; +}; + +/* + * Start blatant code steal. Alternative: Open up d2i_X509_PUBKEY_INTERNAL + * as per https://github.com/openssl/openssl/issues/16697 (TBD) + * Code from from openssl/crypto/x509/x_pubkey.c as + * ossl_d2i_X509_PUBKEY_INTERNAL is presently not public + */ +struct X509_pubkey_st { + X509_ALGOR *algor; + ASN1_BIT_STRING *public_key; + + EVP_PKEY *pkey; + + /* extra data for the callback, used by d2i_PUBKEY_ex */ + OSSL_LIB_CTX *libctx; + char *propq; +}; + +ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = { + ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR), + ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING) +} static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL) + +static X509_PUBKEY *xorx_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp, + long len, OSSL_LIB_CTX *libctx) +{ + X509_PUBKEY *xpub = OPENSSL_zalloc(sizeof(*xpub)); + + if (xpub == NULL) + return NULL; + return (X509_PUBKEY *)ASN1_item_d2i_ex((ASN1_VALUE **)&xpub, pp, len, + ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), + libctx, NULL); +} +/* end steal https://github.com/openssl/openssl/issues/16697 */ + +/* + * Context used for DER to key decoding. + */ +struct der2key_ctx_st { + PROV_XOR_CTX *provctx; + struct keytype_desc_st *desc; + /* The selection that is passed to xor_der2key_decode() */ + int selection; + /* Flag used to signal that a failure is fatal */ + unsigned int flag_fatal : 1; +}; + +static int xor_read_der(PROV_XOR_CTX *provctx, OSSL_CORE_BIO *cin, + unsigned char **data, long *len) +{ + BUF_MEM *mem = NULL; + BIO *in = BIO_new_from_core_bio(provctx->libctx, cin); + int ok = (asn1_d2i_read_bio(in, &mem) >= 0); + + if (ok) { + *data = (unsigned char *)mem->data; + *len = (long)mem->length; + OPENSSL_free(mem); + } + BIO_free(in); + return ok; +} + +typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf, + OSSL_LIB_CTX *libctx, const char *propq); +static void *xor_der2key_decode_p8(const unsigned char **input_der, + long input_der_len, struct der2key_ctx_st *ctx, + key_from_pkcs8_t *key_from_pkcs8) +{ + PKCS8_PRIV_KEY_INFO *p8inf = NULL; + const X509_ALGOR *alg = NULL; + void *key = NULL; + + if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL + && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf) + && OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type) + key = key_from_pkcs8(p8inf, PROV_XOR_LIBCTX_OF(ctx->provctx), NULL); + PKCS8_PRIV_KEY_INFO_free(p8inf); + + return key; +} + +static XORKEY *xor_d2i_PUBKEY(XORKEY **a, + const unsigned char **pp, long length) +{ + XORKEY *key = NULL; + X509_PUBKEY *xpk; + + xpk = xorx_d2i_X509_PUBKEY_INTERNAL(pp, length, NULL); + + key = xor_key_from_x509pubkey(xpk, NULL, NULL); + + if (key == NULL) + goto err_exit; + + if (a != NULL) { + xor_freekey(*a); + *a = key; + } + + err_exit: + X509_PUBKEY_free(xpk); + return key; +} + + +/* ---------------------------------------------------------------------- */ + +static OSSL_FUNC_decoder_freectx_fn der2key_freectx; +static OSSL_FUNC_decoder_decode_fn xor_der2key_decode; +static OSSL_FUNC_decoder_export_object_fn der2key_export_object; + +static struct der2key_ctx_st * +der2key_newctx(void *provctx, struct keytype_desc_st *desc, const char* tls_name) +{ + struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); + + if (ctx != NULL) { + ctx->provctx = provctx; + ctx->desc = desc; + if (desc->evp_type == 0) { + ctx->desc->evp_type = OBJ_sn2nid(tls_name); + } + } + return ctx; +} + +static void der2key_freectx(void *vctx) +{ + struct der2key_ctx_st *ctx = vctx; + + OPENSSL_free(ctx); +} + +static int der2key_check_selection(int selection, + const struct keytype_desc_st *desc) +{ + /* + * The selections are kinda sorta "levels", i.e. each selection given + * here is assumed to include those following. + */ + int checks[] = { + OSSL_KEYMGMT_SELECT_PRIVATE_KEY, + OSSL_KEYMGMT_SELECT_PUBLIC_KEY, + OSSL_KEYMGMT_SELECT_ALL_PARAMETERS + }; + size_t i; + + /* The decoder implementations made here support guessing */ + if (selection == 0) + return 1; + + for (i = 0; i < OSSL_NELEM(checks); i++) { + int check1 = (selection & checks[i]) != 0; + int check2 = (desc->selection_mask & checks[i]) != 0; + + /* + * If the caller asked for the currently checked bit(s), return + * whether the decoder description says it's supported. + */ + if (check1) + return check2; + } + + /* This should be dead code, but just to be safe... */ + return 0; +} + +static int xor_der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) +{ + struct der2key_ctx_st *ctx = vctx; + unsigned char *der = NULL; + const unsigned char *derp; + long der_len = 0; + void *key = NULL; + int ok = 0; + + ctx->selection = selection; + /* + * The caller is allowed to specify 0 as a selection mark, to have the + * structure and key type guessed. For type-specific structures, this + * is not recommended, as some structures are very similar. + * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter + * signifies a private key structure, where everything else is assumed + * to be present as well. + */ + if (selection == 0) + selection = ctx->desc->selection_mask; + if ((selection & ctx->desc->selection_mask) == 0) { + ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); + return 0; + } + + ok = xor_read_der(ctx->provctx, cin, &der, &der_len); + if (!ok) + goto next; + + ok = 0; /* Assume that we fail */ + + if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { + derp = der; + if (ctx->desc->d2i_PKCS8 != NULL) { + key = ctx->desc->d2i_PKCS8(NULL, &derp, der_len, ctx); + if (ctx->flag_fatal) + goto end; + } else if (ctx->desc->d2i_private_key != NULL) { + key = ctx->desc->d2i_private_key(NULL, &derp, der_len); + } + if (key == NULL && ctx->selection != 0) + goto next; + } + if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { + derp = der; + if (ctx->desc->d2i_PUBKEY != NULL) + key = ctx->desc->d2i_PUBKEY(NULL, &derp, der_len); + else + key = ctx->desc->d2i_public_key(NULL, &derp, der_len); + if (key == NULL && ctx->selection != 0) + goto next; + } + if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) { + derp = der; + if (ctx->desc->d2i_key_params != NULL) + key = ctx->desc->d2i_key_params(NULL, &derp, der_len); + if (key == NULL && ctx->selection != 0) + goto next; + } + + /* + * Last minute check to see if this was the correct type of key. This + * should never lead to a fatal error, i.e. the decoding itself was + * correct, it was just an unexpected key type. This is generally for + * classes of key types that have subtle variants, like RSA-PSS keys as + * opposed to plain RSA keys. + */ + if (key != NULL + && ctx->desc->check_key != NULL + && !ctx->desc->check_key(key, ctx)) { + ctx->desc->free_key(key); + key = NULL; + } + + if (key != NULL && ctx->desc->adjust_key != NULL) + ctx->desc->adjust_key(key, ctx); + + next: + /* + * Indicated that we successfully decoded something, or not at all. + * Ending up "empty handed" is not an error. + */ + ok = 1; + + /* + * We free memory here so it's not held up during the callback, because + * we know the process is recursive and the allocated chunks of memory + * add up. + */ + OPENSSL_free(der); + der = NULL; + + if (key != NULL) { + OSSL_PARAM params[4]; + int object_type = OSSL_OBJECT_PKEY; + + params[0] = + OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); + params[1] = + OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + (char *)ctx->desc->keytype_name, + 0); + /* The address of the key becomes the octet string */ + params[2] = + OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, + &key, sizeof(key)); + params[3] = OSSL_PARAM_construct_end(); + + ok = data_cb(params, data_cbarg); + } + + end: + ctx->desc->free_key(key); + OPENSSL_free(der); + + return ok; +} + +static int der2key_export_object(void *vctx, + const void *reference, size_t reference_sz, + OSSL_CALLBACK *export_cb, void *export_cbarg) +{ + struct der2key_ctx_st *ctx = vctx; + OSSL_FUNC_keymgmt_export_fn *export = + xor_prov_get_keymgmt_export(ctx->desc->fns); + void *keydata; + + if (reference_sz == sizeof(keydata) && export != NULL) { + /* The contents of the reference is the address to our object */ + keydata = *(void **)reference; + + return export(keydata, ctx->selection, export_cb, export_cbarg); + } + return 0; +} + +/* ---------------------------------------------------------------------- */ + +static void *xorx_d2i_PKCS8(void **key, const unsigned char **der, long der_len, + struct der2key_ctx_st *ctx) +{ + return xor_der2key_decode_p8(der, der_len, ctx, + (key_from_pkcs8_t *)xor_key_from_pkcs8); +} + +static void xorx_key_adjust(void *key, struct der2key_ctx_st *ctx) +{ +} + +/* ---------------------------------------------------------------------- */ + +#define DO_PrivateKeyInfo(keytype) \ + "PrivateKeyInfo", 0, \ + ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \ + NULL, \ + NULL, \ + NULL, \ + xorx_d2i_PKCS8, \ + NULL, \ + NULL, \ + xorx_key_adjust, \ + (free_key_fn *)xor_freekey + +#define DO_SubjectPublicKeyInfo(keytype) \ + "SubjectPublicKeyInfo", 0, \ + ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + (d2i_of_void *)xor_d2i_PUBKEY, \ + NULL, \ + xorx_key_adjust, \ + (free_key_fn *)xor_freekey + +/* + * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables. + * It takes the following arguments: + * + * keytype_name The implementation key type as a string. + * keytype The implementation key type. This must correspond exactly + * to our existing keymgmt keytype names... in other words, + * there must exist an ossl_##keytype##_keymgmt_functions. + * type The type name for the set of functions that implement the + * decoder for the key type. This isn't necessarily the same + * as keytype. For example, the key types ed25519, ed448, + * x25519 and x448 are all handled by the same functions with + * the common type name ecx. + * kind The kind of support to implement. This translates into + * the DO_##kind macros above, to populate the keytype_desc_st + * structure. + */ +#define MAKE_DECODER(keytype_name, keytype, type, kind) \ + static struct keytype_desc_st kind##_##keytype##_desc = \ + { keytype_name, xor_##keytype##_keymgmt_functions, \ + DO_##kind(keytype) }; \ + \ + static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \ + \ + static void *kind##_der2##keytype##_newctx(void *provctx) \ + { \ + return der2key_newctx(provctx, &kind##_##keytype##_desc, keytype_name );\ + } \ + static int kind##_der2##keytype##_does_selection(void *provctx, \ + int selection) \ + { \ + return der2key_check_selection(selection, \ + &kind##_##keytype##_desc); \ + } \ + static const OSSL_DISPATCH \ + xor_##kind##_der_to_##keytype##_decoder_functions[] = { \ + { OSSL_FUNC_DECODER_NEWCTX, \ + (void (*)(void))kind##_der2##keytype##_newctx }, \ + { OSSL_FUNC_DECODER_FREECTX, \ + (void (*)(void))der2key_freectx }, \ + { OSSL_FUNC_DECODER_DOES_SELECTION, \ + (void (*)(void))kind##_der2##keytype##_does_selection }, \ + { OSSL_FUNC_DECODER_DECODE, \ + (void (*)(void))xor_der2key_decode }, \ + { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ + (void (*)(void))der2key_export_object }, \ + { 0, NULL } \ + } + +MAKE_DECODER(XORSIGALG_NAME, xorhmacsig, xor, PrivateKeyInfo); +MAKE_DECODER(XORSIGALG_NAME, xorhmacsig, xor, SubjectPublicKeyInfo); +MAKE_DECODER(XORSIGALG_HASH_NAME, xorhmacsha2sig, xor, PrivateKeyInfo); +MAKE_DECODER(XORSIGALG_HASH_NAME, xorhmacsha2sig, xor, SubjectPublicKeyInfo); + +static const OSSL_ALGORITHM tls_prov_decoder[] = { +#define DECODER_PROVIDER "tls-provider" +#define DECODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo" +#define DECODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo" + +/* Arguments are prefixed with '_' to avoid build breaks on certain platforms */ +/* + * Obviously this is not FIPS approved, but in order to test in conjunction + * with the FIPS provider we pretend that it is. + */ + +#define DECODER(_name, _input, _output) \ + { _name, \ + "provider=" DECODER_PROVIDER ",fips=yes,input=" #_input, \ + (xor_##_input##_to_##_output##_decoder_functions) } +#define DECODER_w_structure(_name, _input, _structure, _output) \ + { _name, \ + "provider=" DECODER_PROVIDER ",fips=yes,input=" #_input \ + ",structure=" DECODER_STRUCTURE_##_structure, \ + (xor_##_structure##_##_input##_to_##_output##_decoder_functions) } + +DECODER_w_structure(XORSIGALG_NAME, der, PrivateKeyInfo, xorhmacsig), +DECODER_w_structure(XORSIGALG_NAME, der, SubjectPublicKeyInfo, xorhmacsig), +DECODER_w_structure(XORSIGALG_HASH_NAME, der, PrivateKeyInfo, xorhmacsha2sig), +DECODER_w_structure(XORSIGALG_HASH_NAME, der, SubjectPublicKeyInfo, xorhmacsha2sig), +#undef DECODER_PROVIDER + { NULL, NULL, NULL } +}; + +#define OSSL_MAX_NAME_SIZE 50 +#define OSSL_MAX_PROPQUERY_SIZE 256 /* Property query strings */ + +static OSSL_FUNC_signature_newctx_fn xor_sig_newctx; +static OSSL_FUNC_signature_sign_init_fn xor_sig_sign_init; +static OSSL_FUNC_signature_verify_init_fn xor_sig_verify_init; +static OSSL_FUNC_signature_sign_fn xor_sig_sign; +static OSSL_FUNC_signature_verify_fn xor_sig_verify; +static OSSL_FUNC_signature_digest_sign_init_fn xor_sig_digest_sign_init; +static OSSL_FUNC_signature_digest_sign_update_fn xor_sig_digest_signverify_update; +static OSSL_FUNC_signature_digest_sign_final_fn xor_sig_digest_sign_final; +static OSSL_FUNC_signature_digest_verify_init_fn xor_sig_digest_verify_init; +static OSSL_FUNC_signature_digest_verify_update_fn xor_sig_digest_signverify_update; +static OSSL_FUNC_signature_digest_verify_final_fn xor_sig_digest_verify_final; +static OSSL_FUNC_signature_freectx_fn xor_sig_freectx; +static OSSL_FUNC_signature_dupctx_fn xor_sig_dupctx; +static OSSL_FUNC_signature_get_ctx_params_fn xor_sig_get_ctx_params; +static OSSL_FUNC_signature_gettable_ctx_params_fn xor_sig_gettable_ctx_params; +static OSSL_FUNC_signature_set_ctx_params_fn xor_sig_set_ctx_params; +static OSSL_FUNC_signature_settable_ctx_params_fn xor_sig_settable_ctx_params; +static OSSL_FUNC_signature_get_ctx_md_params_fn xor_sig_get_ctx_md_params; +static OSSL_FUNC_signature_gettable_ctx_md_params_fn xor_sig_gettable_ctx_md_params; +static OSSL_FUNC_signature_set_ctx_md_params_fn xor_sig_set_ctx_md_params; +static OSSL_FUNC_signature_settable_ctx_md_params_fn xor_sig_settable_ctx_md_params; + +static int xor_get_aid(unsigned char** oidbuf, const char *tls_name) { + X509_ALGOR *algor = X509_ALGOR_new(); + int aidlen = 0; + + X509_ALGOR_set0(algor, OBJ_txt2obj(tls_name, 0), V_ASN1_UNDEF, NULL); + + aidlen = i2d_X509_ALGOR(algor, oidbuf); + X509_ALGOR_free(algor); + return(aidlen); +} + +/* + * What's passed as an actual key is defined by the KEYMGMT interface. + */ +typedef struct { + OSSL_LIB_CTX *libctx; + char *propq; + XORKEY *sig; + + /* + * Flag to determine if the hash function can be changed (1) or not (0) + * Because it's dangerous to change during a DigestSign or DigestVerify + * operation, this flag is cleared by their Init function, and set again + * by their Final function. + */ + unsigned int flag_allow_md : 1; + + char mdname[OSSL_MAX_NAME_SIZE]; + + /* The Algorithm Identifier of the combined signature algorithm */ + unsigned char *aid; + size_t aid_len; + + /* main digest */ + EVP_MD *md; + EVP_MD_CTX *mdctx; + int operation; +} PROV_XORSIG_CTX; + +static void *xor_sig_newctx(void *provctx, const char *propq) +{ + PROV_XORSIG_CTX *pxor_sigctx; + + pxor_sigctx = OPENSSL_zalloc(sizeof(PROV_XORSIG_CTX)); + if (pxor_sigctx == NULL) + return NULL; + + pxor_sigctx->libctx = ((PROV_XOR_CTX*)provctx)->libctx; + pxor_sigctx->flag_allow_md = 0; + if (propq != NULL && (pxor_sigctx->propq = OPENSSL_strdup(propq)) == NULL) { + OPENSSL_free(pxor_sigctx); + pxor_sigctx = NULL; + ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE); + } + return pxor_sigctx; +} + +static int xor_sig_setup_md(PROV_XORSIG_CTX *ctx, + const char *mdname, const char *mdprops) +{ + EVP_MD *md; + + if (mdprops == NULL) + mdprops = ctx->propq; + + md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); + + if ((md == NULL) || (EVP_MD_nid(md)==NID_undef)) { + if (md == NULL) + ERR_raise_data(ERR_LIB_USER, XORPROV_R_INVALID_DIGEST, + "%s could not be fetched", mdname); + EVP_MD_free(md); + return 0; + } + + EVP_MD_CTX_free(ctx->mdctx); + ctx->mdctx = NULL; + EVP_MD_free(ctx->md); + ctx->md = NULL; + + OPENSSL_free(ctx->aid); + ctx->aid = NULL; + ctx->aid_len = xor_get_aid(&(ctx->aid), ctx->sig->tls_name); + + ctx->mdctx = NULL; + ctx->md = md; + OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); + return 1; +} + +static int xor_sig_signverify_init(void *vpxor_sigctx, void *vxorsig, + int operation) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + if (pxor_sigctx == NULL || vxorsig == NULL) + return 0; + xor_freekey(pxor_sigctx->sig); + if (!xor_key_up_ref(vxorsig)) + return 0; + pxor_sigctx->sig = vxorsig; + pxor_sigctx->operation = operation; + if ((operation==EVP_PKEY_OP_SIGN && pxor_sigctx->sig == NULL) + || (operation==EVP_PKEY_OP_VERIFY && pxor_sigctx->sig == NULL)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_KEY); + return 0; + } + return 1; +} + +static int xor_sig_sign_init(void *vpxor_sigctx, void *vxorsig, + const OSSL_PARAM params[]) +{ + return xor_sig_signverify_init(vpxor_sigctx, vxorsig, EVP_PKEY_OP_SIGN); +} + +static int xor_sig_verify_init(void *vpxor_sigctx, void *vxorsig, + const OSSL_PARAM params[]) +{ + return xor_sig_signverify_init(vpxor_sigctx, vxorsig, EVP_PKEY_OP_VERIFY); +} + +static int xor_sig_sign(void *vpxor_sigctx, unsigned char *sig, size_t *siglen, + size_t sigsize, const unsigned char *tbs, size_t tbslen) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + XORKEY *xorkey = pxor_sigctx->sig; + + size_t max_sig_len = EVP_MAX_MD_SIZE; + size_t xor_sig_len = 0; + int rv = 0; + + if (xorkey == NULL || !xorkey->hasprivkey) { + ERR_raise(ERR_LIB_USER, XORPROV_R_NO_PRIVATE_KEY); + return rv; + } + + if (sig == NULL) { + *siglen = max_sig_len; + return 1; + } + if (*siglen < max_sig_len) { + ERR_raise(ERR_LIB_USER, XORPROV_R_BUFFER_LENGTH_WRONG); + return rv; + } + + /* + * create HMAC using XORKEY as key and hash as data: + * No real crypto, just for test, don't do this at home! + */ + if (!EVP_Q_mac(pxor_sigctx->libctx, "HMAC", NULL, "sha1", NULL, + xorkey->privkey, XOR_KEY_SIZE, tbs, tbslen, + &sig[0], EVP_MAX_MD_SIZE, &xor_sig_len)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_SIGNING_FAILED); + goto endsign; + } + + *siglen = xor_sig_len; + rv = 1; /* success */ + + endsign: + return rv; +} + +static int xor_sig_verify(void *vpxor_sigctx, + const unsigned char *sig, size_t siglen, + const unsigned char *tbs, size_t tbslen) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + XORKEY *xorkey = pxor_sigctx->sig; + unsigned char resignature[EVP_MAX_MD_SIZE]; + size_t resiglen; + int i; + + if (xorkey == NULL || sig == NULL || tbs == NULL) { + ERR_raise(ERR_LIB_USER, XORPROV_R_WRONG_PARAMETERS); + return 0; + } + + /* + * This is no real verify: just re-sign and compare: + * Don't do this at home! Not fit for real use! + */ + /* First re-create private key from public key: */ + for (i = 0; i < XOR_KEY_SIZE; i++) + xorkey->privkey[i] = xorkey->pubkey[i] ^ private_constant[i]; + + /* Now re-create signature */ + if (!EVP_Q_mac(pxor_sigctx->libctx, "HMAC", NULL, "sha1", NULL, + xorkey->privkey, XOR_KEY_SIZE, tbs, tbslen, + &resignature[0], EVP_MAX_MD_SIZE, &resiglen)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_VERIFY_ERROR); + return 0; + } + + /* Now compare with signature passed */ + if (siglen != resiglen || memcmp(resignature, sig, siglen) != 0) { + ERR_raise(ERR_LIB_USER, XORPROV_R_VERIFY_ERROR); + return 0; + } + return 1; +} + +static int xor_sig_digest_signverify_init(void *vpxor_sigctx, const char *mdname, + void *vxorsig, int operation) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + char *rmdname = (char *)mdname; + + if (rmdname == NULL) + rmdname = "sha256"; + + pxor_sigctx->flag_allow_md = 0; + if (!xor_sig_signverify_init(vpxor_sigctx, vxorsig, operation)) + return 0; + + if (!xor_sig_setup_md(pxor_sigctx, rmdname, NULL)) + return 0; + + pxor_sigctx->mdctx = EVP_MD_CTX_new(); + if (pxor_sigctx->mdctx == NULL) + goto error; + + if (!EVP_DigestInit_ex(pxor_sigctx->mdctx, pxor_sigctx->md, NULL)) + goto error; + + return 1; + + error: + EVP_MD_CTX_free(pxor_sigctx->mdctx); + EVP_MD_free(pxor_sigctx->md); + pxor_sigctx->mdctx = NULL; + pxor_sigctx->md = NULL; + return 0; +} + +static int xor_sig_digest_sign_init(void *vpxor_sigctx, const char *mdname, + void *vxorsig, const OSSL_PARAM params[]) +{ + return xor_sig_digest_signverify_init(vpxor_sigctx, mdname, vxorsig, + EVP_PKEY_OP_SIGN); +} + +static int xor_sig_digest_verify_init(void *vpxor_sigctx, const char *mdname, void *vxorsig, const OSSL_PARAM params[]) +{ + return xor_sig_digest_signverify_init(vpxor_sigctx, mdname, + vxorsig, EVP_PKEY_OP_VERIFY); +} + +int xor_sig_digest_signverify_update(void *vpxor_sigctx, + const unsigned char *data, + size_t datalen) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL) + return 0; + + return EVP_DigestUpdate(pxor_sigctx->mdctx, data, datalen); +} + +int xor_sig_digest_sign_final(void *vpxor_sigctx, + unsigned char *sig, size_t *siglen, + size_t sigsize) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + unsigned char digest[EVP_MAX_MD_SIZE]; + unsigned int dlen = 0; + + if (sig != NULL) { + if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL) + return 0; + + if (!EVP_DigestFinal_ex(pxor_sigctx->mdctx, digest, &dlen)) + return 0; + + pxor_sigctx->flag_allow_md = 1; + } + + return xor_sig_sign(vpxor_sigctx, sig, siglen, sigsize, digest, (size_t)dlen); + +} + +int xor_sig_digest_verify_final(void *vpxor_sigctx, const unsigned char *sig, + size_t siglen) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + unsigned char digest[EVP_MAX_MD_SIZE]; + unsigned int dlen = 0; + + if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL) + return 0; + + if (!EVP_DigestFinal_ex(pxor_sigctx->mdctx, digest, &dlen)) + return 0; + + pxor_sigctx->flag_allow_md = 1; + + return xor_sig_verify(vpxor_sigctx, sig, siglen, digest, (size_t)dlen); +} + +static void xor_sig_freectx(void *vpxor_sigctx) +{ + PROV_XORSIG_CTX *ctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + OPENSSL_free(ctx->propq); + EVP_MD_CTX_free(ctx->mdctx); + EVP_MD_free(ctx->md); + ctx->propq = NULL; + ctx->mdctx = NULL; + ctx->md = NULL; + xor_freekey(ctx->sig); + ctx->sig = NULL; + OPENSSL_free(ctx->aid); + OPENSSL_free(ctx); +} + +static void *xor_sig_dupctx(void *vpxor_sigctx) +{ + PROV_XORSIG_CTX *srcctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + PROV_XORSIG_CTX *dstctx; + + dstctx = OPENSSL_zalloc(sizeof(*srcctx)); + if (dstctx == NULL) + return NULL; + + *dstctx = *srcctx; + dstctx->sig = NULL; + dstctx->md = NULL; + dstctx->mdctx = NULL; + dstctx->aid = NULL; + + if ((srcctx->sig != NULL) && !xor_key_up_ref(srcctx->sig)) + goto err; + dstctx->sig = srcctx->sig; + + if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) + goto err; + dstctx->md = srcctx->md; + + if (srcctx->mdctx != NULL) { + dstctx->mdctx = EVP_MD_CTX_new(); + if (dstctx->mdctx == NULL + || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) + goto err; + } + + return dstctx; + err: + xor_sig_freectx(dstctx); + return NULL; +} + +static int xor_sig_get_ctx_params(void *vpxor_sigctx, OSSL_PARAM *params) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + OSSL_PARAM *p; + + if (pxor_sigctx == NULL || params == NULL) + return 0; + + p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); + + if (pxor_sigctx->aid == NULL) + pxor_sigctx->aid_len = xor_get_aid(&(pxor_sigctx->aid), pxor_sigctx->sig->tls_name); + + if (p != NULL + && !OSSL_PARAM_set_octet_string(p, pxor_sigctx->aid, pxor_sigctx->aid_len)) + return 0; + + p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); + if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pxor_sigctx->mdname)) + return 0; + + return 1; +} + +static const OSSL_PARAM known_gettable_ctx_params[] = { + OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), + OSSL_PARAM_END +}; + +static const OSSL_PARAM *xor_sig_gettable_ctx_params(ossl_unused void *vpxor_sigctx, ossl_unused void *vctx) +{ + return known_gettable_ctx_params; +} + +static int xor_sig_set_ctx_params(void *vpxor_sigctx, const OSSL_PARAM params[]) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + const OSSL_PARAM *p; + + if (pxor_sigctx == NULL || params == NULL) + return 0; + + p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); + /* Not allowed during certain operations */ + if (p != NULL && !pxor_sigctx->flag_allow_md) + return 0; + if (p != NULL) { + char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; + char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; + const OSSL_PARAM *propsp = + OSSL_PARAM_locate_const(params, + OSSL_SIGNATURE_PARAM_PROPERTIES); + + if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) + return 0; + if (propsp != NULL + && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) + return 0; + if (!xor_sig_setup_md(pxor_sigctx, mdname, mdprops)) + return 0; + } + + return 1; +} + +static const OSSL_PARAM known_settable_ctx_params[] = { + OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), + OSSL_PARAM_END +}; + +static const OSSL_PARAM *xor_sig_settable_ctx_params(ossl_unused void *vpsm2ctx, + ossl_unused void *provctx) +{ + return known_settable_ctx_params; +} + +static int xor_sig_get_ctx_md_params(void *vpxor_sigctx, OSSL_PARAM *params) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + if (pxor_sigctx->mdctx == NULL) + return 0; + + return EVP_MD_CTX_get_params(pxor_sigctx->mdctx, params); +} + +static const OSSL_PARAM *xor_sig_gettable_ctx_md_params(void *vpxor_sigctx) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + if (pxor_sigctx->md == NULL) + return 0; + + return EVP_MD_gettable_ctx_params(pxor_sigctx->md); +} + +static int xor_sig_set_ctx_md_params(void *vpxor_sigctx, const OSSL_PARAM params[]) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + if (pxor_sigctx->mdctx == NULL) + return 0; + + return EVP_MD_CTX_set_params(pxor_sigctx->mdctx, params); +} + +static const OSSL_PARAM *xor_sig_settable_ctx_md_params(void *vpxor_sigctx) +{ + PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx; + + if (pxor_sigctx->md == NULL) + return 0; + + return EVP_MD_settable_ctx_params(pxor_sigctx->md); +} + +static const OSSL_DISPATCH xor_signature_functions[] = { + { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))xor_sig_newctx }, + { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))xor_sig_sign_init }, + { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))xor_sig_sign }, + { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))xor_sig_verify_init }, + { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))xor_sig_verify }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, + (void (*)(void))xor_sig_digest_sign_init }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, + (void (*)(void))xor_sig_digest_signverify_update }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, + (void (*)(void))xor_sig_digest_sign_final }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, + (void (*)(void))xor_sig_digest_verify_init }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, + (void (*)(void))xor_sig_digest_signverify_update }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, + (void (*)(void))xor_sig_digest_verify_final }, + { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))xor_sig_freectx }, + { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))xor_sig_dupctx }, + { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))xor_sig_get_ctx_params }, + { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, + (void (*)(void))xor_sig_gettable_ctx_params }, + { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))xor_sig_set_ctx_params }, + { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, + (void (*)(void))xor_sig_settable_ctx_params }, + { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, + (void (*)(void))xor_sig_get_ctx_md_params }, + { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, + (void (*)(void))xor_sig_gettable_ctx_md_params }, + { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, + (void (*)(void))xor_sig_set_ctx_md_params }, + { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, + (void (*)(void))xor_sig_settable_ctx_md_params }, + { 0, NULL } +}; + +static const OSSL_ALGORITHM tls_prov_signature[] = { + /* + * Obviously this is not FIPS approved, but in order to test in conjunction + * with the FIPS provider we pretend that it is. + */ + { XORSIGALG_NAME, "provider=tls-provider,fips=yes", + xor_signature_functions }, + { XORSIGALG_HASH_NAME, "provider=tls-provider,fips=yes", + xor_signature_functions }, + { XORSIGALG12_NAME, "provider=tls-provider,fips=yes", + xor_signature_functions }, + { NULL, NULL, NULL } +}; + + +static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id, + int *no_cache) +{ + *no_cache = 0; + switch (operation_id) { + case OSSL_OP_KEYMGMT: + return tls_prov_keymgmt; + case OSSL_OP_KEYEXCH: + return tls_prov_keyexch; + case OSSL_OP_KEM: + return tls_prov_kem; + case OSSL_OP_ENCODER: + return tls_prov_encoder; + case OSSL_OP_DECODER: + return tls_prov_decoder; + case OSSL_OP_SIGNATURE: + return tls_prov_signature; + } + return NULL; +} + +static void tls_prov_teardown(void *provctx) +{ + int i; + PROV_XOR_CTX *pctx = (PROV_XOR_CTX*)provctx; + + OSSL_LIB_CTX_free(pctx->libctx); + + for (i = 0; i < NUM_DUMMY_GROUPS; i++) { + OPENSSL_free(dummy_group_names[i]); + dummy_group_names[i] = NULL; + } + OPENSSL_free(pctx); +} + +/* Functions we provide to the core */ +static const OSSL_DISPATCH tls_prov_dispatch_table[] = { + { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))tls_prov_teardown }, + { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query }, + { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities }, + { 0, NULL } +}; + +static +unsigned int randomize_tls_alg_id(OSSL_LIB_CTX *libctx) +{ + /* + * Randomise the id we're going to use to ensure we don't interoperate + * with anything but ourselves. + */ + unsigned int id; + static unsigned int mem[10] = { 0 }; + static int in_mem = 0; + int i; + + retry: + if (RAND_bytes_ex(libctx, (unsigned char *)&id, sizeof(id), 0) <= 0) + return 0; + /* + * Ensure id is within the IANA Reserved for private use range + * (65024-65279) + */ + id %= 65279 - 65024; + id += 65024; + + /* Ensure we did not already issue this id */ + for (i = 0; i < in_mem; i++) + if (mem[i] == id) + goto retry; + + /* Add this id to the list of ids issued by this function */ + mem[in_mem++] = id; + + return id; +} + +int tls_provider_init(const OSSL_CORE_HANDLE *handle, + const OSSL_DISPATCH *in, + const OSSL_DISPATCH **out, + void **provctx) +{ + OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(handle, in); + OSSL_FUNC_core_obj_create_fn *c_obj_create= NULL; + OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid= NULL; + PROV_XOR_CTX *prov_ctx = xor_newprovctx(libctx); + + if (libctx == NULL || prov_ctx == NULL) + return 0; + + *provctx = prov_ctx; + + /* + * Randomise the group_id and code_points we're going to use to ensure we + * don't interoperate with anything but ourselves. + */ + xor_group.group_id = randomize_tls_alg_id(libctx); + xor_kemgroup.group_id = randomize_tls_alg_id(libctx); + xor_sigalg.code_point = randomize_tls_alg_id(libctx); + xor_sigalg_hash.code_point = randomize_tls_alg_id(libctx); + + /* Retrieve registration functions */ + for (; in->function_id != 0; in++) { + switch (in->function_id) { + case OSSL_FUNC_CORE_OBJ_CREATE: + c_obj_create = OSSL_FUNC_core_obj_create(in); + break; + case OSSL_FUNC_CORE_OBJ_ADD_SIGID: + c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in); + break; + /* Just ignore anything we don't understand */ + default: + break; + } + } + + /* + * Register algorithms manually as add_provider_sigalgs is + * only called during session establishment -- too late for + * key & cert generation... + */ + if (!c_obj_create(handle, XORSIGALG_OID, XORSIGALG_NAME, XORSIGALG_NAME)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); + return 0; + } + + if (!c_obj_add_sigid(handle, XORSIGALG_OID, "", XORSIGALG_OID)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); + return 0; + } + if (!c_obj_create(handle, XORSIGALG_HASH_OID, XORSIGALG_HASH_NAME, NULL)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); + return 0; + } + + if (!c_obj_add_sigid(handle, XORSIGALG_HASH_OID, XORSIGALG_HASH, XORSIGALG_HASH_OID)) { + ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR); + return 0; + } *out = tls_prov_dispatch_table; return 1; -- 2.39.2