07d1ba
diff -up openssh-8.7p1/sshkey.c.evpgenrsa openssh-8.7p1/sshkey.c
07d1ba
--- openssh-8.7p1/sshkey.c.evpgenrsa	2022-06-30 15:14:58.200518353 +0200
07d1ba
+++ openssh-8.7p1/sshkey.c	2022-06-30 15:24:31.499641196 +0200
07d1ba
@@ -1657,7 +1657,8 @@ sshkey_cert_type(const struct sshkey *k)
07d1ba
 static int
07d1ba
 rsa_generate_private_key(u_int bits, RSA **rsap)
07d1ba
 {
07d1ba
-	RSA *private = NULL;
07d1ba
+	EVP_PKEY_CTX *ctx = NULL;
07d1ba
+	EVP_PKEY *res = NULL;
07d1ba
 	BIGNUM *f4 = NULL;
07d1ba
 	int ret = SSH_ERR_INTERNAL_ERROR;
07d1ba
 
07d1ba
@@ -1667,20 +1668,42 @@ rsa_generate_private_key(u_int bits, RSA
07d1ba
 	    bits > SSHBUF_MAX_BIGNUM * 8)
07d1ba
 		return SSH_ERR_KEY_LENGTH;
07d1ba
 	*rsap = NULL;
07d1ba
-	if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) {
07d1ba
+
07d1ba
+	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL
07d1ba
+		|| (f4 = BN_new()) == NULL || !BN_set_word(f4, RSA_F4)) {
07d1ba
 		ret = SSH_ERR_ALLOC_FAIL;
07d1ba
 		goto out;
07d1ba
 	}
07d1ba
-	if (!BN_set_word(f4, RSA_F4) ||
07d1ba
-	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
07d1ba
+
07d1ba
+	if (EVP_PKEY_keygen_init(ctx) <= 0) {
07d1ba
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
07d1ba
+		goto out;
07d1ba
+	}
07d1ba
+
07d1ba
+	if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0) {
07d1ba
+		ret = SSH_ERR_KEY_LENGTH;
07d1ba
+		goto out;
07d1ba
+	}
07d1ba
+
07d1ba
+	if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, f4) <= 0)
07d1ba
+		goto out;
07d1ba
+
07d1ba
+	if (EVP_PKEY_keygen(ctx, &res) <= 0) {
07d1ba
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
07d1ba
+		goto out;
07d1ba
+	}
07d1ba
+
07d1ba
+	/* This function is deprecated in OpenSSL 3.0 but OpenSSH doesn't worry about it*/
07d1ba
+	*rsap = EVP_PKEY_get1_RSA(res);
07d1ba
+	if (*rsap) {
07d1ba
+		ret = 0;
07d1ba
+	} else {
07d1ba
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
07d1ba
 		goto out;
07d1ba
 	}
07d1ba
-	*rsap = private;
07d1ba
-	private = NULL;
07d1ba
-	ret = 0;
07d1ba
  out:
07d1ba
-	RSA_free(private);
07d1ba
+	EVP_PKEY_CTX_free(ctx);
07d1ba
+	EVP_PKEY_free(res);
07d1ba
 	BN_free(f4);
07d1ba
 	return ret;
07d1ba
 }
07d1ba
@@ -1820,7 +1820,8 @@ sshkey_ecdsa_key_to_nid(EC_KEY *k)
07d1ba
 static int
07d1ba
 ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap)
07d1ba
 {
07d1ba
-	EC_KEY *private;
07d1ba
+	EVP_PKEY_CTX *ctx = NULL;
07d1ba
+	EVP_PKEY *res = NULL;
07d1ba
 	int ret = SSH_ERR_INTERNAL_ERROR;
07d1ba
 
07d1ba
 	if (nid == NULL || ecdsap == NULL)
07d1ba
@@ -1828,20 +1829,29 @@ ecdsa_generate_private_key(u_int bits, i
07d1ba
 	if ((*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
07d1ba
 		return SSH_ERR_KEY_LENGTH;
07d1ba
 	*ecdsap = NULL;
07d1ba
-	if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) {
07d1ba
+
07d1ba
+	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL) {
07d1ba
 		ret = SSH_ERR_ALLOC_FAIL;
07d1ba
 		goto out;
07d1ba
 	}
07d1ba
-	if (EC_KEY_generate_key(private) != 1) {
07d1ba
+
07d1ba
+	if (EVP_PKEY_keygen_init(ctx) <= 0 || EVP_PKEY_CTX_set_group_name(ctx, OBJ_nid2sn(*nid)) <= 0
07d1ba
+	   || EVP_PKEY_keygen(ctx, &res) <= 0) {
07d1ba
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
07d1ba
+		goto out;
07d1ba
+	}
07d1ba
+	/* This function is deprecated in OpenSSL 3.0 but OpenSSH doesn't worry about it*/
07d1ba
+	*ecdsap = EVP_PKEY_get1_EC_KEY(res);
07d1ba
+	if (*ecdsap) {
07d1ba
+		EC_KEY_set_asn1_flag(*ecdsap, OPENSSL_EC_NAMED_CURVE);
07d1ba
+		ret = 0;
07d1ba
+	} else {
07d1ba
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
07d1ba
 		goto out;
07d1ba
 	}
07d1ba
-	EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
07d1ba
-	*ecdsap = private;
07d1ba
-	private = NULL;
07d1ba
-	ret = 0;
07d1ba
  out:
07d1ba
-	EC_KEY_free(private);
07d1ba
+	EVP_PKEY_CTX_free(ctx);
07d1ba
+	EVP_PKEY_free(res);
07d1ba
 	return ret;
07d1ba
 }
07d1ba
 # endif /* OPENSSL_HAS_ECC */