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