diff options
author | Richard Levitte <levitte@openssl.org> | 2024-07-25 13:30:28 +0200 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2024-08-27 13:56:28 +0200 |
commit | 033dcce2bae2d1d261f2460f5b9217682e03a7cf (patch) | |
tree | 2a6475af0545bb6f11c971143db2a484f84000f5 | |
parent | feat: Implement EVP_CIPHER_CTX_{set,get}_algor_params() and EVP_CIPHER_CTX_ge... (diff) | |
download | openssl-033dcce2bae2d1d261f2460f5b9217682e03a7cf.tar.xz openssl-033dcce2bae2d1d261f2460f5b9217682e03a7cf.zip |
feat: Implement EVP_PKEY_CTX_{set,get}_algor_params() and EVP_PKEY_CTX_get_algor()
This should be sufficient to cover the intent with the following legacy ctrls:
- EVP_PKEY_CTRL_PKCS7_ENCRYPT (through EVP_ASYM_CIPHER implementations)
- EVP_PKEY_CTRL_PKCS7_DECRYPT (through EVP_ASYM_CIPHER implementations)
- EVP_PKEY_CTRL_PKCS7_SIGN (through EVP_SIGNATURE implementations)
- EVP_PKEY_CTRL_CMS_ENCRYPT (through EVP_ASYM_CIPHER implementations)
- EVP_PKEY_CTRL_CMS_DECRYPT (through EVP_ASYM_CIPHER implementations)
- EVP_PKEY_CTRL_CMS_SIGN (through EVP_SIGNATURE implementations)
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/25000)
-rw-r--r-- | crypto/evp/evp_lib.c | 116 | ||||
-rw-r--r-- | include/openssl/evp.h | 5 | ||||
-rw-r--r-- | util/libcrypto.num | 3 |
3 files changed, 124 insertions, 0 deletions
diff --git a/crypto/evp/evp_lib.c b/crypto/evp/evp_lib.c index 9ae4afd42a..3c670b8396 100644 --- a/crypto/evp/evp_lib.c +++ b/crypto/evp/evp_lib.c @@ -1344,4 +1344,120 @@ int EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX *ctx, X509_ALGOR **alg) return ret; } +int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg) +{ + int ret = -1; /* Assume the worst */ + unsigned char *der = NULL; + int derl = -1; + + if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) { + const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS; + OSSL_PARAM params[2]; + + /* + * Passing the same data with both the old (deprecated) and the + * new AlgID parameters OSSL_PARAM key. + */ + params[0] = OSSL_PARAM_construct_octet_string(k, der, (size_t)derl); + params[1] = OSSL_PARAM_construct_end(); + ret = EVP_PKEY_CTX_set_params(ctx, params); + } + OPENSSL_free(der); + return ret; +} + +int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg) +{ + int ret = -1; /* Assume the worst */ + OSSL_PARAM params[2]; + unsigned char *der = NULL; + size_t derl; + ASN1_TYPE *type = NULL; + const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS; + + /* + * We make two passes, the first to get the appropriate buffer size, + * and the second to get the actual value. + * Also, using both the old (deprecated) and the new AlgID parameters + * OSSL_PARAM key, and using whichever the provider responds to. + * Should the provider respond on both, the new key takes priority. + */ + params[0] = OSSL_PARAM_construct_octet_string(k, NULL, 0); + params[1] = OSSL_PARAM_construct_end(); + + if (!EVP_PKEY_CTX_get_params(ctx, params)) + goto err; + + /* + * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE() + * below. If it is NULL, the d2i_ASN1_TYPE() call will allocate new + * space for it. Either way, alg->parameter can be safely assigned + * with type after the d2i_ASN1_TYPE() call, with the safety that it + * will be ok. + */ + type = alg->parameter; + + derl = params[0].return_size; + if (OSSL_PARAM_modified(¶ms[0]) + /* ... but, we should get a return size too! */ + && derl != 0 + && (der = OPENSSL_malloc(derl)) != NULL) { + unsigned char *derp = der; + + params[0] = OSSL_PARAM_construct_octet_string(k, der, derl); + if (EVP_PKEY_CTX_get_params(ctx, params) + && OSSL_PARAM_modified(¶ms[0]) + && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp, + derl) != NULL) { + /* + * Don't free alg->parameter, see comment further up. + * Worst case, alg->parameter gets assigned its own value. + */ + alg->parameter = type; + ret = 1; + } + } + err: + OPENSSL_free(der); + return ret; +} + +int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg) +{ + int ret = -1; /* Assume the worst */ + OSSL_PARAM params[2]; + size_t aid_len = 0; + const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID; + + params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0); + params[1] = OSSL_PARAM_construct_end(); + + if (EVP_PKEY_CTX_get_params(ctx, params) <= 0) + goto err; + + if (OSSL_PARAM_modified(¶ms[0])) + aid_len = params[0].return_size; + if (aid_len == 0) { + ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED); + ret = -2; + goto err; + } + if (alg != NULL) { + unsigned char *aid = NULL; + const unsigned char *pp = NULL; + + if ((aid = OPENSSL_malloc(aid_len)) != NULL) { + params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len); + pp = aid; + if (EVP_PKEY_CTX_get_params(ctx, params) + && OSSL_PARAM_modified(¶ms[0]) + && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL) + ret = 1; + } + OPENSSL_free(aid); + } + err: + return ret; +} + #endif /* !defined(FIPS_MODULE) */ diff --git a/include/openssl/evp.h b/include/openssl/evp.h index 5ca10346b2..ae76d472a7 100644 --- a/include/openssl/evp.h +++ b/include/openssl/evp.h @@ -1815,6 +1815,11 @@ int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params); const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx); int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx); + +int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg); +int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg); +int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg); + int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, int p1, void *p2); int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, diff --git a/util/libcrypto.num b/util/libcrypto.num index 351df4de98..2c485fb153 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -5729,3 +5729,6 @@ EVP_PKEY_verify_recover_init_ex2 ? 3_4_0 EXIST::FUNCTION: EVP_CIPHER_CTX_set_algor_params ? 3_4_0 EXIST::FUNCTION: EVP_CIPHER_CTX_get_algor_params ? 3_4_0 EXIST::FUNCTION: EVP_CIPHER_CTX_get_algor ? 3_4_0 EXIST::FUNCTION: +EVP_PKEY_CTX_set_algor_params ? 3_4_0 EXIST::FUNCTION: +EVP_PKEY_CTX_get_algor_params ? 3_4_0 EXIST::FUNCTION: +EVP_PKEY_CTX_get_algor ? 3_4_0 EXIST::FUNCTION: |