The EVP_PKEY_ASN1_METHOD function export_to() must check that the key
we're trying to export has a known libcrypto method, i.e. is a built
in RSA_METHOD, DSA_METHOD, etc. Otherwise, the method may be defined
by the calling application, by an engine, by another library, and we
simply cannot know all the quirks hidden behind that method, if we
have access to the key data, or much anything.
Such keys are simply deemed impossible to export to provider keys,
i.e. have export_to() return 0. This cascades back to functions like
evp_pkey_export_to_provider() and evp_pkey_upgrade_to_provider() and
their callers. In most cases, this is fine, but if these get mixed in
with provider side keys in any function, that function will fail.
Fixes #11179
Fixes #9915
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/11193)
#include "dh_local.h"
#include <openssl/bn.h>
#include "crypto/asn1.h"
+#include "crypto/dh.h"
#include "crypto/evp.h"
#include <openssl/cms.h>
#include <openssl/core_names.h>
OSSL_PARAM *params;
int rv;
+ /*
+ * If the DH method is foreign, then we can't be sure of anything, and
+ * can therefore not export or pretend to export.
+ */
+ if (dh_get_method(dh) != DH_OpenSSL())
+ return 0;
+
if (p == NULL || g == NULL)
return 0;
OSSL_PARAM *params;
int rv;
+ /*
+ * If the DSA method is foreign, then we can't be sure of anything, and
+ * can therefore not export or pretend to export.
+ */
+ if (DSA_get_method(dsa) != DSA_OpenSSL())
+ return 0;
+
if (p == NULL || q == NULL || g == NULL)
return 0;
|| (ecg = EC_KEY_get0_group(eckey)) == NULL)
return 0;
+ /*
+ * If the EC_KEY method is foreign, then we can't be sure of anything,
+ * and can therefore not export or pretend to export.
+ */
+ if (EC_KEY_get_method(eckey) != EC_KEY_OpenSSL())
+ return 0;
+
ossl_param_bld_init(&tmpl);
/* export the domain parameters */
*/
ERR_set_mark();
- if (locpctx->keytype == NULL)
+ if (locpctx->engine != NULL || locpctx->keytype == NULL)
goto legacy;
/*
*/
ERR_set_mark();
- if (ctx->keytype == NULL || ctx->engine != NULL)
+ if (ctx->engine != NULL || ctx->keytype == NULL)
goto legacy;
/*
*/
ERR_set_mark();
- if (ctx->keytype == NULL)
+ if (ctx->engine != NULL || ctx->keytype == NULL)
goto legacy;
/*
OSSL_PARAM *params = NULL;
int rv = 0;
+ /*
+ * If the RSA method is foreign, then we can't be sure of anything, and
+ * can therefore not export or pretend to export.
+ */
+ if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
+ return 0;
+
/* Public parameters must always be present */
if (n == NULL || e == NULL)
goto err;