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