$NetBSD$
--- sendmail/tls.c.orig	2020-06-02 11:41:43.000000000 +0200
+++ sendmail/tls.c	2021-03-18 15:55:56.977879368 +0100
@@ -75,8 +75,40 @@
 {
 	0x02
 };
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000
+#define MTA_HAVE_DH_set0_pqg
+static inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
+{
+	/* If the fields p and g in d are NULL, the corresponding input
+	 * parameters MUST be non-NULL.  q may remain NULL.
+	 */
+	if ((dh->p == NULL && p == NULL)
+	    || (dh->g == NULL && g == NULL))
+		return 0;
+
+	if (p != NULL) {
+		BN_free(dh->p);
+		dh->p = p;
+	}
+	if (q != NULL) {
+		BN_free(dh->q);
+		dh->q = q;
+	}
+	if (g != NULL) {
+		BN_free(dh->g);
+		dh->g = g;
+	}
+
+	if (q != NULL) {
+		dh->length = BN_num_bits(q);
+	}
+
+	return 1;
+}
+#endif
+
 static DH *
 get_dh512()
 {
 	DH *dh = NULL;
@@ -1179,28 +1211,44 @@
 	**  It is a time-consuming operation and it is not always necessary.
 	**  maybe we should do it only on demand...
 	*/
 
-	if (bitset(TLS_I_RSA_TMP, req)
 #  if SM_CONF_SHM
-	    && ShmId != SM_SHM_NO_ID &&
-	    (rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL,
-					NULL)) == NULL
-#  else /* SM_CONF_SHM */
-	    && 0	/* no shared memory: no need to generate key now */
-#  endif /* SM_CONF_SHM */
-	   )
+	if (bitset(TLS_I_RSA_TMP, req)
+	    && ShmId != SM_SHM_NO_ID)
 	{
-		if (LogLevel > 7)
+		BIGNUM *bn;
+
+		bn = BN_new();
+		rsa_tmp = RSA_new();
+		if (!bn || !rsa_tmp || !BN_set_word(bn, RSA_F4)) {
+			RSA_free(rsa_tmp);
+			rsa_tmp = NULL;
+		}
+		if (rsa_tmp)
 		{
-			sm_syslog(LOG_WARNING, NOQID,
-				  "STARTTLS=%s, error: RSA_generate_key failed",
-				  who);
-			tlslogerr(LOG_WARNING, 9, who);
+			if (!RSA_generate_key_ex(rsa_tmp, RSA_KEYLENGTH, bn, NULL))
+			{
+				RSA_free(rsa_tmp);
+				rsa_tmp = NULL;
+			}
+		}
+		BN_free(bn);
+		if (!rsa_tmp)
+		{
+			if (LogLevel > 7)
+			{
+				sm_syslog(LOG_WARNING, NOQID,
+					  "STARTTLS=%s, error: RSA_generate_key failed",
+					  who);
+				if (LogLevel > 9)
+					tlslogerr(LOG_WARNING, who);
+			}
+			return false;
 		}
-		return false;
 	}
-# endif /* !TLS_NO_RSA && MTA_RSA_TMP_CB */
+# endif /* SM_CONF_SHM */
+# endif /* !TLS_NO_RSA */
 
 	/*
 	**  load private key
 	**  XXX change this for DSA-only version
@@ -2078,12 +2126,24 @@
 	    ++(*PRSATmpCnt) < MAX_RSA_TMP_CNT)
 		return rsa_tmp;
 #  endif /* SM_CONF_SHM */
 
-	if (rsa_tmp != NULL)
-		RSA_free(rsa_tmp);
-	rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL, NULL);
-	if (rsa_tmp == NULL)
+	if (rsa_tmp == NULL) {
+		rsa_tmp = RSA_new();
+		if (!rsa_tmp)
+			return NULL;
+	}
+
+	bn = BN_new();
+	if (!bn)
+		return NULL;
+	if (!BN_set_word(bn, RSA_F4)) {
+		BN_free(bn);
+		return NULL;
+	}
+	ret = RSA_generate_key_ex(rsa_tmp, RSA_KEYLENGTH, bn, NULL);
+	BN_free(bn);
+	if (!ret)
 	{
 		if (LogLevel > 0)
 			sm_syslog(LOG_ERR, NOQID,
 				  "STARTTLS=server, tmp_rsa_key: RSA_generate_key failed!");
