From c627cd1d68fdc382db3839d429327709aed5528d Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Sun, 4 Oct 2020 19:09:40 +0000
Subject: openssh: update to 8.4p1

---
 abs/core/openssh/PKGBUILD            |   48 +-
 abs/core/openssh/glibc-2.31.patch    |  100 ++
 abs/core/openssh/install             |   32 +
 abs/core/openssh/openssl-1.1.0.patch | 1982 ----------------------------------
 abs/core/openssh/sshd.service        |    3 -
 abs/core/openssh/sshd.socket         |   10 -
 abs/core/openssh/sshd@.service       |    9 -
 7 files changed, 157 insertions(+), 2027 deletions(-)
 create mode 100644 abs/core/openssh/glibc-2.31.patch
 create mode 100644 abs/core/openssh/install
 delete mode 100644 abs/core/openssh/openssl-1.1.0.patch
 delete mode 100644 abs/core/openssh/sshd.socket
 delete mode 100644 abs/core/openssh/sshd@.service

diff --git a/abs/core/openssh/PKGBUILD b/abs/core/openssh/PKGBUILD
index b180b75..a7c3f0d 100644
--- a/abs/core/openssh/PKGBUILD
+++ b/abs/core/openssh/PKGBUILD
@@ -1,44 +1,46 @@
-# $Id$
-# Maintainer: Gaetan Bisson <bisson@archlinux.org>
+# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
+# Contributor: Gaetan Bisson <bisson@archlinux.org>
 # Contributor: Aaron Griffin <aaron@archlinux.org>
 # Contributor: judd <jvinet@zeroflux.org>
 
 pkgname=openssh
-pkgver=7.6p1
-pkgrel=2
-pkgdesc='Free version of the SSH connectivity tools'
+pkgver=8.4p1
+pkgrel=1
+pkgdesc='Premier connectivity tool for remote login with the SSH protocol'
 url='https://www.openssh.com/portable.html'
 license=('custom:BSD')
 arch=('x86_64')
-makedepends=('linux-headers')
-depends=('krb5' 'openssl' 'libedit' 'ldns')
+depends=('glibc' 'krb5' 'openssl' 'libedit' 'ldns' 'libxcrypt' 'libcrypt.so' 'zlib' 'pam')
+makedepends=('linux-headers' 'libfido2')
+checkdepends=('inetutils')
 optdepends=('xorg-xauth: X11 forwarding'
-            'x11-ssh-askpass: input passphrase in X')
+            'x11-ssh-askpass: input passphrase in X'
+            'libfido2: FIDO/U2F support')
 validpgpkeys=('59C2118ED206D927E667EBE3D3E5F56B6D920D30')
+#source=("git://anongit.mindrot.org/openssh.git?signed#tag=V_8_2_P1"
 source=("https://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/${pkgname}-${pkgver}.tar.gz"{,.asc}
-        'openssl-1.1.0.patch'
         'sshdgenkeys.service'
-        'sshd@.service'
         'sshd.service'
-        'sshd.socket'
         'sshd.conf'
-        'sshd.pam')
-sha256sums=('a323caeeddfe145baaa0db16e98d784b1fbc7dd436a6bf1f479dfd5cd1d21723'
+        'sshd.pam'
+        'glibc-2.31.patch')
+sha256sums=('5a01d22e407eb1c05ba8a8f7c654d388a13e9f226e4ed33bd38748dafa1d2b24'
             'SKIP'
-            '1fcae9fc461026d96d08b38457e7ffe281b4319caaada1508f9eb74c2566ba5d'
             '4031577db6416fcbaacf8a26a024ecd3939e5c10fe6a86ee3f0eea5093d533b7'
-            '3a0845737207f4eda221c9c9fb64e766ade9684562d8ba4f705f7ae6826886e5'
-            'c5ed9fa629f8f8dbf3bae4edbad4441c36df535088553fe82695c52d7bde30aa'
-            'de14363e9d4ed92848e524036d9e6b57b2d35cc77d377b7247c38111d2a3defd'
+            'e40f8b7c8e5e2ecf3084b3511a6c36d5b5c9f9e61f2bb13e3726c71dc7d4fbc7'
             '4effac1186cc62617f44385415103021f72f674f8b8e26447fc1139c670090f6'
-            '64576021515c0a98b0aaf0a0ae02e0f5ebe8ee525b1e647ab68f369f81ecd846')
+            '64576021515c0a98b0aaf0a0ae02e0f5ebe8ee525b1e647ab68f369f81ecd846'
+            '25b4a4d9e2d9d3289ef30636a30e85fa1c71dd930d5efd712cca1a01a5019f93')
 
 backup=('etc/ssh/ssh_config' 'etc/ssh/sshd_config' 'etc/pam.d/sshd')
 
+install=install
+
 prepare() {
-	cd $pkgname-$pkgver
-	# OpenSSL 1.1.0 patch from http://vega.pgw.jp/~kabe/vsd/patch/openssh-7.4p1-openssl-1.1.0c.patch.html
-	patch -Np1 -i ../openssl-1.1.0.patch
+	cd "${srcdir}/${pkgname}-${pkgver}"
+
+	patch -p1 -i ../glibc-2.31.patch
+	autoreconf
 }
 
 build() {
@@ -49,8 +51,10 @@ build() {
 		--sbindir=/usr/bin \
 		--libexecdir=/usr/lib/ssh \
 		--sysconfdir=/etc/ssh \
+		--disable-strip \
 		--with-ldns \
 		--with-libedit \
+		--with-security-key-builtin \
 		--with-ssl-engine \
 		--with-pam \
 		--with-privsep-user=nobody \
@@ -84,9 +88,7 @@ package() {
 	install -Dm644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENCE"
 
 	install -Dm644 ../sshdgenkeys.service "${pkgdir}"/usr/lib/systemd/system/sshdgenkeys.service
-	install -Dm644 ../sshd@.service "${pkgdir}"/usr/lib/systemd/system/sshd@.service
 	install -Dm644 ../sshd.service "${pkgdir}"/usr/lib/systemd/system/sshd.service
-	install -Dm644 ../sshd.socket "${pkgdir}"/usr/lib/systemd/system/sshd.socket
 	install -Dm644 ../sshd.conf "${pkgdir}"/usr/lib/tmpfiles.d/sshd.conf
 	install -Dm644 ../sshd.pam "${pkgdir}"/etc/pam.d/sshd
 
diff --git a/abs/core/openssh/glibc-2.31.patch b/abs/core/openssh/glibc-2.31.patch
new file mode 100644
index 0000000..1870428
--- /dev/null
+++ b/abs/core/openssh/glibc-2.31.patch
@@ -0,0 +1,100 @@
+From beee0ef61866cb567b9abc23bd850f922e59e3f0 Mon Sep 17 00:00:00 2001
+From: Darren Tucker <dtucker@dtucker.net>
+Date: Wed, 13 Nov 2019 23:19:35 +1100
+Subject: [PATCH] seccomp: Allow clock_nanosleep() in sandbox.
+
+seccomp: Allow clock_nanosleep() to make OpenSSH working with latest
+glibc.  Patch from Jakub Jelen <jjelen@redhat.com> via bz #3093.
+---
+ sandbox-seccomp-filter.c | 6 ++++++
+ 1 file changed, 6 insertions(+)
+
+diff --git a/sandbox-seccomp-filter.c b/sandbox-seccomp-filter.c
+index b5cda70bb..96ab141f7 100644
+--- a/sandbox-seccomp-filter.c
++++ b/sandbox-seccomp-filter.c
+@@ -242,6 +242,12 @@ static const struct sock_filter preauth_insns[] = {
+ #ifdef __NR_nanosleep
+ 	SC_ALLOW(__NR_nanosleep),
+ #endif
++#ifdef __NR_clock_nanosleep
++	SC_ALLOW(__NR_clock_nanosleep),
++#endif
++#ifdef __NR_clock_nanosleep
++	SC_ALLOW(__NR_clock_nanosleep),
++#endif
+ #ifdef __NR__newselect
+ 	SC_ALLOW(__NR__newselect),
+ #endif
+From 69298ebfc2c066acee5d187eac8ce9f38c796630 Mon Sep 17 00:00:00 2001
+From: Darren Tucker <dtucker@dtucker.net>
+Date: Wed, 13 Nov 2019 23:27:31 +1100
+Subject: [PATCH] Remove duplicate __NR_clock_nanosleep
+
+---
+ sandbox-seccomp-filter.c | 3 ---
+ 1 file changed, 3 deletions(-)
+
+diff --git a/sandbox-seccomp-filter.c b/sandbox-seccomp-filter.c
+index 96ab141f7..be2397671 100644
+--- a/sandbox-seccomp-filter.c
++++ b/sandbox-seccomp-filter.c
+@@ -245,9 +245,6 @@ static const struct sock_filter preauth_insns[] = {
+ #ifdef __NR_clock_nanosleep
+ 	SC_ALLOW(__NR_clock_nanosleep),
+ #endif
+-#ifdef __NR_clock_nanosleep
+-	SC_ALLOW(__NR_clock_nanosleep),
+-#endif
+ #ifdef __NR__newselect
+ 	SC_ALLOW(__NR__newselect),
+ #endif
+From 030b4c2b8029563bc8a9fd764288fde08fa2347c Mon Sep 17 00:00:00 2001
+From: Darren Tucker <dtucker@dtucker.net>
+Date: Mon, 16 Dec 2019 13:55:56 +1100
+Subject: [PATCH] Allow clock_nanosleep_time64 in seccomp sandbox.
+
+Needed on Linux ARM.  bz#3100, patch from jjelen@redhat.com.
+---
+ sandbox-seccomp-filter.c | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/sandbox-seccomp-filter.c b/sandbox-seccomp-filter.c
+index be2397671..3ef30c9d5 100644
+--- a/sandbox-seccomp-filter.c
++++ b/sandbox-seccomp-filter.c
+@@ -245,6 +245,9 @@ static const struct sock_filter preauth_insns[] = {
+ #ifdef __NR_clock_nanosleep
+ 	SC_ALLOW(__NR_clock_nanosleep),
+ #endif
++#ifdef __NR_clock_nanosleep_time64
++	SC_ALLOW(__NR_clock_nanosleep_time64),
++#endif
+ #ifdef __NR__newselect
+ 	SC_ALLOW(__NR__newselect),
+ #endif
+From a991cc5ed5a7c455fefe909a30cf082011ef5dff Mon Sep 17 00:00:00 2001
+From: Khem Raj <raj.khem@gmail.com>
+Date: Tue, 7 Jan 2020 16:26:45 -0800
+Subject: [PATCH] seccomp: Allow clock_gettime64() in sandbox.
+
+This helps sshd accept connections on mips platforms with
+upcoming glibc ( 2.31 )
+---
+ sandbox-seccomp-filter.c | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/sandbox-seccomp-filter.c b/sandbox-seccomp-filter.c
+index 3ef30c9d5..999c46c9f 100644
+--- a/sandbox-seccomp-filter.c
++++ b/sandbox-seccomp-filter.c
+@@ -248,6 +248,9 @@ static const struct sock_filter preauth_insns[] = {
+ #ifdef __NR_clock_nanosleep_time64
+ 	SC_ALLOW(__NR_clock_nanosleep_time64),
+ #endif
++#ifdef __NR_clock_gettime64
++	SC_ALLOW(__NR_clock_gettime64),
++#endif
+ #ifdef __NR__newselect
+ 	SC_ALLOW(__NR__newselect),
+ #endif
diff --git a/abs/core/openssh/install b/abs/core/openssh/install
new file mode 100644
index 0000000..47c1e77
--- /dev/null
+++ b/abs/core/openssh/install
@@ -0,0 +1,32 @@
+pre_upgrade() {
+	# Remove socket activation. See: https://bugs.archlinux.org/task/62248
+	if (( $(vercmp $2 8.0p1-3) < 0 )); then
+	if systemctl is-enabled -q sshd.socket; then
+		cat <<EOF
+==> This package no longer provides sshd.socket and sshd@.service;
+==> copies of those files will be placed under /etc/systemd/system
+==> but please migrate to sshd.service whenever possible.
+EOF
+		src=/usr/lib/systemd/system
+		dst=/etc/systemd/system
+		for i in sshd.socket sshd\@.service; do
+			if [[ ! -e "$dst/$i" ]]; then
+				cp -v "$src/$i" "$dst/$i"
+			fi
+		done
+		systemctl reenable sshd.socket
+	fi
+	fi
+}
+
+post_upgrade() {
+	if (( $(vercmp $2 8.2p1-3) < 0 )); then
+		if systemctl is-active sshd.service >/dev/null; then
+			cat <<EOF
+==> After this upgrade, your existing SSH daemon may be unable to accept
+==> new connections. To fix this, your SSH daemon will now be restarted.
+EOF
+			systemctl restart sshd.service
+		fi
+	fi
+}
diff --git a/abs/core/openssh/openssl-1.1.0.patch b/abs/core/openssh/openssl-1.1.0.patch
deleted file mode 100644
index f5454ac..0000000
--- a/abs/core/openssh/openssl-1.1.0.patch
+++ /dev/null
@@ -1,1982 +0,0 @@
-diff -Naur old/auth-pam.c new/auth-pam.c
---- old/auth-pam.c	2017-10-03 21:49:05.363829772 -1000
-+++ new/auth-pam.c	2017-10-03 21:55:50.869718862 -1000
-@@ -128,6 +128,10 @@
- typedef pthread_t sp_pthread_t;
- #else
- typedef pid_t sp_pthread_t;
-+# define pthread_create(a, b, c, d)    _ssh_compat_pthread_create(a, b, c, d)
-+# define pthread_exit(a)               _ssh_compat_pthread_exit(a)
-+# define pthread_cancel(a)             _ssh_compat_pthread_cancel(a)
-+# define pthread_join(a, b)            _ssh_compat_pthread_join(a, b)
- #endif
- 
- struct pam_ctxt {
-diff -Naur old/cipher.c new/cipher.c
---- old/cipher.c	2017-10-03 21:49:05.367162904 -1000
-+++ new/cipher.c	2017-10-03 21:55:50.869718862 -1000
-@@ -297,7 +297,10 @@
- 			goto out;
- 		}
- 	}
--	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
-+	/* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups;
-+	   use EVP_CipherInit_ex for augmenting */
-+	if (EVP_CipherInit_ex(cc->evp, NULL, NULL, (u_char *)key, NULL, -1) == 0)
-+	{
- 		ret = SSH_ERR_LIBCRYPTO_ERROR;
- 		goto out;
- 	}
-@@ -486,7 +489,7 @@
- 		   len, iv))
- 		       return SSH_ERR_LIBCRYPTO_ERROR;
- 	} else
--		memcpy(iv, cc->evp->iv, len);
-+		memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
- #endif
- 	return 0;
- }
-@@ -520,14 +523,19 @@
- 		    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
- 			return SSH_ERR_LIBCRYPTO_ERROR;
- 	} else
--		memcpy(cc->evp->iv, iv, evplen);
-+		memcpy(EVP_CIPHER_CTX_iv(cc->evp), iv, evplen);
- #endif
- 	return 0;
- }
- 
- #ifdef WITH_OPENSSL
--#define EVP_X_STATE(evp)	(evp)->cipher_data
--#define EVP_X_STATE_LEN(evp)	(evp)->cipher->ctx_size
-+# if OPENSSL_VERSION_NUMBER >= 0x10100000UL
-+#define EVP_X_STATE(evp)	EVP_CIPHER_CTX_get_cipher_data(evp)
-+#define EVP_X_STATE_LEN(evp)	EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
-+# else
-+#define EVP_X_STATE(evp)	(evp).cipher_data
-+#define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
-+# endif
- #endif
- 
- int
-diff -Naur old/cipher.h new/cipher.h
---- old/cipher.h	2017-10-03 21:49:05.367162904 -1000
-+++ new/cipher.h	2017-10-03 21:55:50.869718862 -1000
-@@ -46,7 +46,18 @@
- #define CIPHER_DECRYPT		0
- 
- struct sshcipher;
-+#if 0
-+struct sshcipher_ctx {
-+	int	plaintext;
-+	int	encrypt;
-+	EVP_CIPHER_CTX *evp;
-+	struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
-+	struct aesctr_ctx ac_ctx; /* XXX union with evp? */
-+	const struct sshcipher *cipher;
-+};
-+#else
- struct sshcipher_ctx;
-+#endif
- 
- const struct sshcipher *cipher_by_name(const char *);
- const char *cipher_warning_message(const struct sshcipher_ctx *);
-diff -Naur old/configure new/configure
---- old/configure	2017-10-03 21:49:05.410493626 -1000
-+++ new/configure	2017-10-03 22:01:49.159050540 -1000
-@@ -12688,7 +12688,6 @@
- 				100*)   ;; # 1.0.x
- 				200*)   ;; # LibreSSL
- 			        *)
--					as_fn_error $? "OpenSSL >= 1.1.0 is not yet supported (have \"$ssl_library_ver\")" "$LINENO" 5
- 			                ;;
- 			esac
- 			{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ssl_library_ver" >&5
-diff -Naur old/dh.c new/dh.c
---- old/dh.c	2017-10-03 21:49:05.370496037 -1000
-+++ new/dh.c	2017-10-03 21:55:50.869718862 -1000
-@@ -212,14 +212,15 @@
- /* diffie-hellman-groupN-sha1 */
- 
- int
--dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
-+dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
- {
- 	int i;
- 	int n = BN_num_bits(dh_pub);
- 	int bits_set = 0;
- 	BIGNUM *tmp;
-+	const BIGNUM *p;
- 
--	if (dh_pub->neg) {
-+	if (BN_is_negative(dh_pub)) {
- 		logit("invalid public DH value: negative");
- 		return 0;
- 	}
-@@ -232,7 +233,8 @@
- 		error("%s: BN_new failed", __func__);
- 		return 0;
- 	}
--	if (!BN_sub(tmp, dh->p, BN_value_one()) ||
-+	DH_get0_pqg(dh, &p, NULL, NULL);
-+	if (!BN_sub(tmp, p, BN_value_one()) ||
- 	    BN_cmp(dh_pub, tmp) != -1) {		/* pub_exp > p-2 */
- 		BN_clear_free(tmp);
- 		logit("invalid public DH value: >= p-1");
-@@ -243,14 +245,14 @@
- 	for (i = 0; i <= n; i++)
- 		if (BN_is_bit_set(dh_pub, i))
- 			bits_set++;
--	debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
-+	debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
- 
- 	/*
- 	 * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
- 	 */
- 	if (bits_set < 4) {
- 		logit("invalid public DH value (%d/%d)",
--		   bits_set, BN_num_bits(dh->p));
-+		   bits_set, BN_num_bits(p));
- 		return 0;
- 	}
- 	return 1;
-@@ -260,9 +262,13 @@
- dh_gen_key(DH *dh, int need)
- {
- 	int pbits;
-+	const BIGNUM *p, *pub_key;
-+	BIGNUM *priv_key;
- 
--	if (need < 0 || dh->p == NULL ||
--	    (pbits = BN_num_bits(dh->p)) <= 0 ||
-+	DH_get0_pqg(dh, &p, NULL, NULL);
-+
-+	if (need < 0 || p == NULL ||
-+	    (pbits = BN_num_bits(p)) <= 0 ||
- 	    need > INT_MAX / 2 || 2 * need > pbits)
- 		return SSH_ERR_INVALID_ARGUMENT;
- 	if (need < 256)
-@@ -271,10 +277,13 @@
- 	 * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
- 	 * so double requested need here.
- 	 */
--	dh->length = MINIMUM(need * 2, pbits - 1);
--	if (DH_generate_key(dh) == 0 ||
--	    !dh_pub_is_valid(dh, dh->pub_key)) {
--		BN_clear_free(dh->priv_key);
-+	DH_set_length(dh, MIN(need * 2, pbits - 1));
-+	if (DH_generate_key(dh) == 0) {
-+		return SSH_ERR_LIBCRYPTO_ERROR;
-+	}
-+	DH_get0_key(dh, &pub_key, &priv_key);
-+	if (!dh_pub_is_valid(dh, pub_key)) {
-+		BN_clear(priv_key);
- 		return SSH_ERR_LIBCRYPTO_ERROR;
- 	}
- 	return 0;
-@@ -283,16 +292,27 @@
- DH *
- dh_new_group_asc(const char *gen, const char *modulus)
- {
--	DH *dh;
-+	DH *dh = NULL;
-+	BIGNUM *p=NULL, *g=NULL;
- 
--	if ((dh = DH_new()) == NULL)
--		return NULL;
--	if (BN_hex2bn(&dh->p, modulus) == 0 ||
--	    BN_hex2bn(&dh->g, gen) == 0) {
--		DH_free(dh);
--		return NULL;
-+	if ((dh = DH_new()) == NULL ||
-+	    (p = BN_new()) == NULL ||
-+	    (g = BN_new()) == NULL)
-+		goto null;
-+	if (BN_hex2bn(&p, modulus) == 0 ||
-+	    BN_hex2bn(&g, gen) == 0) {
-+		goto null;
- 	}
-+	if (DH_set0_pqg(dh, p, NULL, g) == 0) {
-+		goto null;
-+	}
-+	p = g = NULL;
- 	return (dh);
-+null:
-+	BN_free(p);
-+	BN_free(g);
-+	DH_free(dh);
-+	return NULL;
- }
- 
- /*
-@@ -307,8 +327,8 @@
- 
- 	if ((dh = DH_new()) == NULL)
- 		return NULL;
--	dh->p = modulus;
--	dh->g = gen;
-+	if (DH_set0_pqg(dh, modulus, NULL, gen) == 0)
-+		return NULL;
- 
- 	return (dh);
- }
-diff -Naur old/dh.h new/dh.h
---- old/dh.h	2017-10-03 21:49:05.370496037 -1000
-+++ new/dh.h	2017-10-03 21:55:50.869718862 -1000
-@@ -42,7 +42,7 @@
- DH	*dh_new_group_fallback(int);
- 
- int	 dh_gen_key(DH *, int);
--int	 dh_pub_is_valid(DH *, BIGNUM *);
-+int	 dh_pub_is_valid(const DH *, const BIGNUM *);
- 
- u_int	 dh_estimate(int);
- 
-diff -Naur old/digest-openssl.c new/digest-openssl.c
---- old/digest-openssl.c	2017-10-03 21:49:05.370496037 -1000
-+++ new/digest-openssl.c	2017-10-03 21:55:50.869718862 -1000
-@@ -43,7 +43,7 @@
- 
- struct ssh_digest_ctx {
- 	int alg;
--	EVP_MD_CTX mdctx;
-+	EVP_MD_CTX *mdctx;
- };
- 
- struct ssh_digest {
-@@ -106,20 +106,21 @@
- size_t
- ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
- {
--	return EVP_MD_CTX_block_size(&ctx->mdctx);
-+	return EVP_MD_CTX_block_size(ctx->mdctx);
- }
- 
- struct ssh_digest_ctx *
- ssh_digest_start(int alg)
- {
- 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
--	struct ssh_digest_ctx *ret;
-+	struct ssh_digest_ctx *ret = NULL;
- 
- 	if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
- 		return NULL;
- 	ret->alg = alg;
--	EVP_MD_CTX_init(&ret->mdctx);
--	if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
-+	if ((ret->mdctx = EVP_MD_CTX_new()) == NULL ||
-+	    EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
-+		EVP_MD_CTX_free(ret->mdctx);
- 		free(ret);
- 		return NULL;
- 	}
-@@ -132,7 +133,7 @@
- 	if (from->alg != to->alg)
- 		return SSH_ERR_INVALID_ARGUMENT;
- 	/* we have bcopy-style order while openssl has memcpy-style */
--	if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
-+	if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
- 		return SSH_ERR_LIBCRYPTO_ERROR;
- 	return 0;
- }
-@@ -140,7 +141,7 @@
- int
- ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
- {
--	if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
-+	if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
- 		return SSH_ERR_LIBCRYPTO_ERROR;
- 	return 0;
- }
-@@ -161,7 +162,7 @@
- 		return SSH_ERR_INVALID_ARGUMENT;
- 	if (dlen < digest->digest_len) /* No truncation allowed */
- 		return SSH_ERR_INVALID_ARGUMENT;
--	if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
-+	if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
- 		return SSH_ERR_LIBCRYPTO_ERROR;
- 	if (l != digest->digest_len) /* sanity */
- 		return SSH_ERR_INTERNAL_ERROR;
-@@ -172,7 +173,7 @@
- ssh_digest_free(struct ssh_digest_ctx *ctx)
- {
- 	if (ctx != NULL) {
--		EVP_MD_CTX_cleanup(&ctx->mdctx);
-+		EVP_MD_CTX_free(ctx->mdctx);
- 		explicit_bzero(ctx, sizeof(*ctx));
- 		free(ctx);
- 	}
-diff -Naur old/kexdhc.c new/kexdhc.c
---- old/kexdhc.c	2017-10-03 21:49:05.373829169 -1000
-+++ new/kexdhc.c	2017-10-03 21:55:50.869718862 -1000
-@@ -81,11 +81,16 @@
- 		goto out;
- 	}
- 	debug("sending SSH2_MSG_KEXDH_INIT");
--	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
--	    (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
--	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
-+	{
-+	const BIGNUM *pub_key;
-+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
-+		goto out;
-+	DH_get0_key(kex->dh, &pub_key, NULL);
-+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
-+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
- 	    (r = sshpkt_send(ssh)) != 0)
- 		goto out;
-+	}
- #ifdef DEBUG_KEXDH
- 	DHparams_print_fp(stderr, kex->dh);
- 	fprintf(stderr, "pub= ");
-@@ -169,6 +174,9 @@
- 
- 	/* calc and verify H */
- 	hashlen = sizeof(hash);
-+	{
-+	const BIGNUM *pub_key;
-+	DH_get0_key(kex->dh, &pub_key, NULL);
- 	if ((r = kex_dh_hash(
- 	    kex->hash_alg,
- 	    kex->client_version_string,
-@@ -176,11 +184,13 @@
- 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
- 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
- 	    server_host_key_blob, sbloblen,
--	    kex->dh->pub_key,
-+	    pub_key,
- 	    dh_server_pub,
- 	    shared_secret,
--	    hash, &hashlen)) != 0)
-+	    hash, &hashlen)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
- 	    ssh->compat)) != 0)
-diff -Naur old/kexdhs.c new/kexdhs.c
---- old/kexdhs.c	2017-10-03 21:49:05.373829169 -1000
-+++ new/kexdhs.c	2017-10-03 21:55:50.869718862 -1000
-@@ -87,6 +87,10 @@
- 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
- 	r = 0;
-  out:
-+	if (r != 0) {
-+		if (kex->dh) DH_free(kex->dh);
-+		kex->dh = NULL;
-+	}
- 	return r;
- }
- 
-@@ -163,6 +167,9 @@
- 		goto out;
- 	/* calc H */
- 	hashlen = sizeof(hash);
-+	{
-+	const BIGNUM *pub_key;
-+	DH_get0_key(kex->dh, &pub_key, NULL);
- 	if ((r = kex_dh_hash(
- 	    kex->hash_alg,
- 	    kex->client_version_string,
-@@ -171,10 +178,12 @@
- 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
- 	    server_host_key_blob, sbloblen,
- 	    dh_client_pub,
--	    kex->dh->pub_key,
-+	    pub_key,
- 	    shared_secret,
--	    hash, &hashlen)) != 0)
-+	    hash, &hashlen)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	/* save session id := H */
- 	if (kex->session_id == NULL) {
-@@ -195,12 +204,17 @@
- 	/* destroy_sensitive_data(); */
- 
- 	/* send server hostkey, DH pubkey 'f' and singed H */
-+	{
-+	const BIGNUM *pub_key;
-+	DH_get0_key(kex->dh, &pub_key, NULL);
- 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
- 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
--	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||	/* f */
-+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||	/* f */
- 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
--	    (r = sshpkt_send(ssh)) != 0)
-+	    (r = sshpkt_send(ssh)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
- 		r = kex_send_newkeys(ssh);
-diff -Naur old/kexgexc.c new/kexgexc.c
---- old/kexgexc.c	2017-10-03 21:49:05.373829169 -1000
-+++ new/kexgexc.c	2017-10-03 21:55:50.869718862 -1000
-@@ -118,11 +118,17 @@
- 	p = g = NULL; /* belong to kex->dh now */
- 
- 	/* generate and send 'e', client DH public key */
--	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
--	    (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
--	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
--	    (r = sshpkt_send(ssh)) != 0)
-+	{
-+	const BIGNUM *pub_key;
-+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
-+		goto out;
-+	DH_get0_key(kex->dh, &pub_key, NULL);
-+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
-+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
-+	    (r = sshpkt_send(ssh)) != 0) {
- 		goto out;
-+	}
-+	}
- 	debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
- #ifdef DEBUG_KEXDH
- 	DHparams_print_fp(stderr, kex->dh);
-@@ -134,10 +140,12 @@
- 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
- 	r = 0;
- out:
--	if (p)
-+	if (r != 0) {
- 		BN_clear_free(p);
--	if (g)
- 		BN_clear_free(g);
-+		DH_free(kex->dh);
-+		kex->dh = NULL;
-+	}
- 	return r;
- }
- 
-@@ -214,6 +222,10 @@
- 
- 	/* calc and verify H */
- 	hashlen = sizeof(hash);
-+	{
-+	const BIGNUM *p, *g, *pub_key;
-+	DH_get0_pqg(kex->dh, &p, NULL, &g);
-+	DH_get0_key(kex->dh, &pub_key, NULL);
- 	if ((r = kexgex_hash(
- 	    kex->hash_alg,
- 	    kex->client_version_string,
-@@ -222,12 +234,14 @@
- 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
- 	    server_host_key_blob, sbloblen,
- 	    kex->min, kex->nbits, kex->max,
--	    kex->dh->p, kex->dh->g,
--	    kex->dh->pub_key,
-+	    p, g,
-+	    pub_key,
- 	    dh_server_pub,
- 	    shared_secret,
--	    hash, &hashlen)) != 0)
-+	    hash, &hashlen)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	if ((r = sshkey_verify(server_host_key, signature, slen, hash,
- 	    hashlen, ssh->compat)) != 0)
-diff -Naur old/kexgexs.c new/kexgexs.c
---- old/kexgexs.c	2017-10-03 21:49:05.373829169 -1000
-+++ new/kexgexs.c	2017-10-03 21:55:50.869718862 -1000
-@@ -101,11 +101,16 @@
- 		goto out;
- 	}
- 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
-+	{
-+	const BIGNUM *p, *g;
-+	DH_get0_pqg(kex->dh, &p, NULL, &g);
- 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
--	    (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
--	    (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
--	    (r = sshpkt_send(ssh)) != 0)
-+	    (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
-+	    (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
-+	    (r = sshpkt_send(ssh)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	/* Compute our exchange value in parallel with the client */
- 	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
-@@ -115,6 +120,10 @@
- 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
- 	r = 0;
-  out:
-+	if (r != 0) {
-+		DH_free(kex->dh);
-+		kex->dh = NULL;
-+	}
- 	return r;
- }
- 
-@@ -191,6 +200,10 @@
- 		goto out;
- 	/* calc H */
- 	hashlen = sizeof(hash);
-+	{
-+	const BIGNUM *p, *g, *pub_key;
-+	DH_get0_pqg(kex->dh, &p, NULL, &g);
-+	DH_get0_key(kex->dh, &pub_key, NULL);
- 	if ((r = kexgex_hash(
- 	    kex->hash_alg,
- 	    kex->client_version_string,
-@@ -199,12 +212,14 @@
- 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
- 	    server_host_key_blob, sbloblen,
- 	    kex->min, kex->nbits, kex->max,
--	    kex->dh->p, kex->dh->g,
-+	    p, g,
- 	    dh_client_pub,
--	    kex->dh->pub_key,
-+	    pub_key,
- 	    shared_secret,
--	    hash, &hashlen)) != 0)
-+	    hash, &hashlen)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	/* save session id := H */
- 	if (kex->session_id == NULL) {
-@@ -225,12 +240,17 @@
- 	/* destroy_sensitive_data(); */
- 
- 	/* send server hostkey, DH pubkey 'f' and singed H */
-+	{
-+	const BIGNUM *pub_key;
-+	DH_get0_key(kex->dh, &pub_key, NULL);
- 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
- 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
--	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
-+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
- 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
--	    (r = sshpkt_send(ssh)) != 0)
-+	    (r = sshpkt_send(ssh)) != 0) {
- 		goto out;
-+	}
-+	}
- 
- 	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
- 		r = kex_send_newkeys(ssh);
-diff -Naur old/monitor.c new/monitor.c
---- old/monitor.c	2017-10-03 21:49:05.377162302 -1000
-+++ new/monitor.c	2017-10-03 21:55:50.869718862 -1000
-@@ -586,10 +586,12 @@
- 		buffer_put_char(m, 0);
- 		return (0);
- 	} else {
-+		const BIGNUM *p, *g;
-+		DH_get0_pqg(dh, &p, NULL, &g);
- 		/* Send first bignum */
- 		buffer_put_char(m, 1);
--		buffer_put_bignum2(m, dh->p);
--		buffer_put_bignum2(m, dh->g);
-+		buffer_put_bignum2(m, p);
-+		buffer_put_bignum2(m, g);
- 
- 		DH_free(dh);
- 	}
-diff -Naur old/openbsd-compat/openssl-compat.c new/openbsd-compat/openssl-compat.c
---- old/openbsd-compat/openssl-compat.c	2017-10-03 21:49:05.397161097 -1000
-+++ new/openbsd-compat/openssl-compat.c	2017-10-03 21:55:50.886387486 -1000
-@@ -75,7 +75,6 @@
- 	/* Enable use of crypto hardware */
- 	ENGINE_load_builtin_engines();
- 	ENGINE_register_all_complete();
--	OPENSSL_config(NULL);
- }
- #endif
- 
-diff -Naur old/regress/unittests/sshkey/test_file.c new/regress/unittests/sshkey/test_file.c
---- old/regress/unittests/sshkey/test_file.c	2017-10-03 21:49:05.387161699 -1000
-+++ new/regress/unittests/sshkey/test_file.c	2017-10-03 21:55:50.883053761 -1000
-@@ -60,9 +60,14 @@
- 	a = load_bignum("rsa_1.param.n");
- 	b = load_bignum("rsa_1.param.p");
- 	c = load_bignum("rsa_1.param.q");
--	ASSERT_BIGNUM_EQ(k1->rsa->n, a);
--	ASSERT_BIGNUM_EQ(k1->rsa->p, b);
--	ASSERT_BIGNUM_EQ(k1->rsa->q, c);
-+	{
-+	const BIGNUM *n, *p, *q;
-+	RSA_get0_key(k1->rsa, &n, NULL, NULL);
-+	RSA_get0_factors(k1->rsa, &p, &q);
-+	ASSERT_BIGNUM_EQ(n, a);
-+	ASSERT_BIGNUM_EQ(p, b);
-+	ASSERT_BIGNUM_EQ(q, c);
-+	}
- 	BN_free(a);
- 	BN_free(b);
- 	BN_free(c);
-@@ -151,9 +156,14 @@
- 	a = load_bignum("dsa_1.param.g");
- 	b = load_bignum("dsa_1.param.priv");
- 	c = load_bignum("dsa_1.param.pub");
--	ASSERT_BIGNUM_EQ(k1->dsa->g, a);
--	ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
--	ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
-+	{
-+	const BIGNUM *g, *priv_key, *pub_key;
-+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
-+	DSA_get0_key(k1->dsa, &pub_key, &priv_key);
-+	ASSERT_BIGNUM_EQ(g, a);
-+	ASSERT_BIGNUM_EQ(priv_key, b);
-+	ASSERT_BIGNUM_EQ(pub_key, c);
-+	}
- 	BN_free(a);
- 	BN_free(b);
- 	BN_free(c);
-diff -Naur old/regress/unittests/sshkey/test_sshkey.c new/regress/unittests/sshkey/test_sshkey.c
---- old/regress/unittests/sshkey/test_sshkey.c	2017-10-03 21:49:05.387161699 -1000
-+++ new/regress/unittests/sshkey/test_sshkey.c	2017-10-03 21:55:50.883053761 -1000
-@@ -197,9 +197,14 @@
- 	k1 = sshkey_new(KEY_RSA);
- 	ASSERT_PTR_NE(k1, NULL);
- 	ASSERT_PTR_NE(k1->rsa, NULL);
--	ASSERT_PTR_NE(k1->rsa->n, NULL);
--	ASSERT_PTR_NE(k1->rsa->e, NULL);
--	ASSERT_PTR_EQ(k1->rsa->p, NULL);
-+	{
-+	const BIGNUM *n, *e, *p;
-+	RSA_get0_key(k1->rsa, &n, &e, NULL);
-+	RSA_get0_factors(k1->rsa, &p, NULL);
-+	ASSERT_PTR_NE(n, NULL);
-+	ASSERT_PTR_NE(e, NULL);
-+	ASSERT_PTR_EQ(p, NULL);
-+	}
- 	sshkey_free(k1);
- 	TEST_DONE();
- 
-@@ -207,8 +212,13 @@
- 	k1 = sshkey_new(KEY_DSA);
- 	ASSERT_PTR_NE(k1, NULL);
- 	ASSERT_PTR_NE(k1->dsa, NULL);
--	ASSERT_PTR_NE(k1->dsa->g, NULL);
--	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
-+	{
-+	const BIGNUM *g, *priv_key;
-+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
-+	DSA_get0_key(k1->dsa, NULL, &priv_key);
-+	ASSERT_PTR_NE(g, NULL);
-+	ASSERT_PTR_EQ(priv_key, NULL);
-+	}
- 	sshkey_free(k1);
- 	TEST_DONE();
- 
-@@ -234,9 +244,14 @@
- 	k1 = sshkey_new_private(KEY_RSA);
- 	ASSERT_PTR_NE(k1, NULL);
- 	ASSERT_PTR_NE(k1->rsa, NULL);
--	ASSERT_PTR_NE(k1->rsa->n, NULL);
--	ASSERT_PTR_NE(k1->rsa->e, NULL);
--	ASSERT_PTR_NE(k1->rsa->p, NULL);
-+	{
-+	const BIGNUM *n, *e, *p;
-+	RSA_get0_key(k1->rsa, &n, &e, NULL);
-+	RSA_get0_factors(k1->rsa, &p, NULL);
-+	ASSERT_PTR_NE(n, NULL);
-+	ASSERT_PTR_NE(e, NULL);
-+	ASSERT_PTR_NE(p, NULL);
-+	}
- 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
- 	sshkey_free(k1);
- 	TEST_DONE();
-@@ -245,8 +260,13 @@
- 	k1 = sshkey_new_private(KEY_DSA);
- 	ASSERT_PTR_NE(k1, NULL);
- 	ASSERT_PTR_NE(k1->dsa, NULL);
--	ASSERT_PTR_NE(k1->dsa->g, NULL);
--	ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
-+	{
-+	const BIGNUM *g, *priv_key;
-+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
-+	DSA_get0_key(k1->dsa, NULL, &priv_key);
-+	ASSERT_PTR_NE(g, NULL);
-+	ASSERT_PTR_NE(priv_key, NULL);
-+	}
- 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
- 	sshkey_free(k1);
- 	TEST_DONE();
-@@ -285,18 +305,28 @@
- 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
- 	ASSERT_PTR_NE(kr, NULL);
- 	ASSERT_PTR_NE(kr->rsa, NULL);
--	ASSERT_PTR_NE(kr->rsa->n, NULL);
--	ASSERT_PTR_NE(kr->rsa->e, NULL);
--	ASSERT_PTR_NE(kr->rsa->p, NULL);
--	ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
-+	{
-+	const BIGNUM *n, *e, *p;
-+	RSA_get0_key(kr->rsa, &n, &e, NULL);
-+	RSA_get0_factors(kr->rsa, &p, NULL);
-+	ASSERT_PTR_NE(n, NULL);
-+	ASSERT_PTR_NE(e, NULL);
-+	ASSERT_PTR_NE(p, NULL);
-+	ASSERT_INT_EQ(BN_num_bits(n), 1024);
-+	}
- 	TEST_DONE();
- 
- 	TEST_START("generate KEY_DSA");
- 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
- 	ASSERT_PTR_NE(kd, NULL);
- 	ASSERT_PTR_NE(kd->dsa, NULL);
--	ASSERT_PTR_NE(kd->dsa->g, NULL);
--	ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
-+	{
-+	const BIGNUM *g, *priv_key;
-+	DSA_get0_pqg(kd->dsa, NULL, NULL, &g);
-+	DSA_get0_key(kd->dsa, NULL, &priv_key);
-+	ASSERT_PTR_NE(g, NULL);
-+	ASSERT_PTR_NE(priv_key, NULL);
-+	}
- 	TEST_DONE();
- 
- #ifdef OPENSSL_HAS_ECC
-@@ -323,9 +353,14 @@
- 	ASSERT_PTR_NE(kr, k1);
- 	ASSERT_INT_EQ(k1->type, KEY_RSA);
- 	ASSERT_PTR_NE(k1->rsa, NULL);
--	ASSERT_PTR_NE(k1->rsa->n, NULL);
--	ASSERT_PTR_NE(k1->rsa->e, NULL);
--	ASSERT_PTR_EQ(k1->rsa->p, NULL);
-+	{
-+	const BIGNUM *n, *e, *p;
-+	RSA_get0_key(k1->rsa, &n, &e, NULL);
-+	RSA_get0_factors(k1->rsa, &p, NULL);
-+	ASSERT_PTR_NE(n, NULL);
-+	ASSERT_PTR_NE(e, NULL);
-+	ASSERT_PTR_EQ(p, NULL);
-+	}
- 	TEST_DONE();
- 
- 	TEST_START("equal KEY_RSA/demoted KEY_RSA");
-@@ -339,8 +374,13 @@
- 	ASSERT_PTR_NE(kd, k1);
- 	ASSERT_INT_EQ(k1->type, KEY_DSA);
- 	ASSERT_PTR_NE(k1->dsa, NULL);
--	ASSERT_PTR_NE(k1->dsa->g, NULL);
--	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
-+	{
-+	const BIGNUM *g, *priv_key;
-+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
-+	DSA_get0_key(k1->dsa, NULL, &priv_key);
-+	ASSERT_PTR_NE(g, NULL);
-+	ASSERT_PTR_EQ(priv_key, NULL);
-+	}
- 	TEST_DONE();
- 
- 	TEST_START("equal KEY_DSA/demoted KEY_DSA");
-diff -Naur old/ssh-dss.c new/ssh-dss.c
---- old/ssh-dss.c	2017-10-03 21:49:05.403827361 -1000
-+++ new/ssh-dss.c	2017-10-03 21:55:50.869718862 -1000
-@@ -53,6 +53,7 @@
- 	DSA_SIG *sig = NULL;
- 	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
- 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
-+	const BIGNUM *r, *s;
- 	struct sshbuf *b = NULL;
- 	int ret = SSH_ERR_INVALID_ARGUMENT;
- 
-@@ -76,15 +77,16 @@
- 		goto out;
- 	}
- 
--	rlen = BN_num_bytes(sig->r);
--	slen = BN_num_bytes(sig->s);
-+	DSA_SIG_get0(sig, &r, &s);
-+	rlen = BN_num_bytes(r);
-+	slen = BN_num_bytes(s);
- 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
- 		ret = SSH_ERR_INTERNAL_ERROR;
- 		goto out;
- 	}
- 	explicit_bzero(sigblob, SIGBLOB_LEN);
--	BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
--	BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
-+	BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
-+	BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
- 
- 	if (compat & SSH_BUG_SIGBLOB) {
- 		if (sigp != NULL) {
-@@ -176,17 +178,26 @@
- 	}
- 
- 	/* parse signature */
-+	{
-+	BIGNUM *r=NULL, *s=NULL;
- 	if ((sig = DSA_SIG_new()) == NULL ||
--	    (sig->r = BN_new()) == NULL ||
--	    (sig->s = BN_new()) == NULL) {
-+	    (r = BN_new()) == NULL ||
-+	    (s = BN_new()) == NULL) {
- 		ret = SSH_ERR_ALLOC_FAIL;
-+		BN_free(r);
-+		BN_free(s);
- 		goto out;
- 	}
--	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
--	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
-+	if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
-+	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL)) {
- 		ret = SSH_ERR_LIBCRYPTO_ERROR;
-+		BN_free(r);
-+		BN_free(s);
- 		goto out;
- 	}
-+	DSA_SIG_set0(sig, r, s);
-+	r = s = NULL;
-+	}
- 
- 	/* sha1 the data */
- 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
-diff -Naur old/ssh-ecdsa.c new/ssh-ecdsa.c
---- old/ssh-ecdsa.c	2017-10-03 21:49:05.403827361 -1000
-+++ new/ssh-ecdsa.c	2017-10-03 21:55:50.869718862 -1000
-@@ -80,9 +80,14 @@
- 		ret = SSH_ERR_ALLOC_FAIL;
- 		goto out;
- 	}
--	if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
--	    (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
-+	{
-+	const BIGNUM *r, *s;
-+	ECDSA_SIG_get0(sig, &r, &s);
-+	if ((ret = sshbuf_put_bignum2(bb, r)) != 0 ||
-+	    (ret = sshbuf_put_bignum2(bb, s)) != 0) {
- 		goto out;
-+	}
-+	}
- 	if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
- 	    (ret = sshbuf_put_stringb(b, bb)) != 0)
- 		goto out;
-@@ -151,11 +156,27 @@
- 		ret = SSH_ERR_ALLOC_FAIL;
- 		goto out;
- 	}
--	if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
--	    sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
-+	{
-+	BIGNUM *r=NULL, *s=NULL;
-+	if ((r = BN_new()) == NULL ||
-+	    (s = BN_new()) == NULL) {
-+		ret = SSH_ERR_ALLOC_FAIL;
-+		goto out_rs;
-+	}
-+	if (sshbuf_get_bignum2(sigbuf, r) != 0 ||
-+	    sshbuf_get_bignum2(sigbuf, s) != 0) {
- 		ret = SSH_ERR_INVALID_FORMAT;
-+		goto out_rs;
-+	}
-+	if (ECDSA_SIG_set0(sig, r, s) == 0) {
-+		ret = SSH_ERR_LIBCRYPTO_ERROR;
-+out_rs:
-+		BN_free(r);
-+		BN_free(s);
- 		goto out;
- 	}
-+	r = s = NULL;
-+	}
- 	if (sshbuf_len(sigbuf) != 0) {
- 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
- 		goto out;
-diff -Naur old/ssh-keygen.c new/ssh-keygen.c
---- old/ssh-keygen.c	2017-10-03 21:49:05.403827361 -1000
-+++ new/ssh-keygen.c	2017-10-03 21:55:50.869718862 -1000
-@@ -496,11 +496,33 @@
- 
- 	switch (key->type) {
- 	case KEY_DSA:
--		buffer_get_bignum_bits(b, key->dsa->p);
--		buffer_get_bignum_bits(b, key->dsa->g);
--		buffer_get_bignum_bits(b, key->dsa->q);
--		buffer_get_bignum_bits(b, key->dsa->pub_key);
--		buffer_get_bignum_bits(b, key->dsa->priv_key);
-+		{
-+		BIGNUM *p=NULL, *g=NULL, *q=NULL, *pub_key=NULL, *priv_key=NULL;
-+		if ((p=BN_new()) == NULL ||
-+		    (g=BN_new()) == NULL ||
-+		    (q=BN_new()) == NULL ||
-+		    (pub_key=BN_new()) == NULL ||
-+		    (priv_key=BN_new()) == NULL) {
-+			BN_free(p);
-+			BN_free(g);
-+			BN_free(q);
-+			BN_free(pub_key);
-+			BN_free(priv_key);
-+			return NULL;
-+		}
-+		buffer_get_bignum_bits(b, p);
-+		buffer_get_bignum_bits(b, g);
-+		buffer_get_bignum_bits(b, q);
-+		buffer_get_bignum_bits(b, pub_key);
-+		buffer_get_bignum_bits(b, priv_key);
-+		if (DSA_set0_pqg(key->dsa, p, q, g) == 0 ||
-+		    DSA_set0_key(key->dsa, pub_key, priv_key) == 0) {
-+			fatal("failed to set DSA key");
-+			BN_free(p); BN_free(g); BN_free(q);
-+			BN_free(pub_key); BN_free(priv_key);
-+			return NULL;
-+		}
-+		}
- 		break;
- 	case KEY_RSA:
- 		if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
-@@ -517,16 +539,52 @@
- 			e += e3;
- 			debug("e %lx", e);
- 		}
--		if (!BN_set_word(key->rsa->e, e)) {
-+		{
-+		BIGNUM *rsa_e = NULL;
-+		BIGNUM *d=NULL, *n=NULL, *iqmp=NULL, *q=NULL, *p=NULL;
-+		BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy input to set in RSA_set0_crt_params */
-+		rsa_e = BN_new();
-+		if (!rsa_e || !BN_set_word(rsa_e, e)) {
-+			if (rsa_e) BN_free(rsa_e);
- 			sshbuf_free(b);
- 			sshkey_free(key);
- 			return NULL;
- 		}
--		buffer_get_bignum_bits(b, key->rsa->d);
--		buffer_get_bignum_bits(b, key->rsa->n);
--		buffer_get_bignum_bits(b, key->rsa->iqmp);
--		buffer_get_bignum_bits(b, key->rsa->q);
--		buffer_get_bignum_bits(b, key->rsa->p);
-+		if ((d=BN_new()) == NULL ||
-+		    (n=BN_new()) == NULL ||
-+		    (iqmp=BN_new()) == NULL ||
-+		    (q=BN_new()) == NULL ||
-+		    (p=BN_new()) == NULL ||
-+		    (dmp1=BN_new()) == NULL ||
-+		    (dmq1=BN_new()) == NULL) {
-+			BN_free(d); BN_free(n); BN_free(iqmp);
-+			BN_free(q); BN_free(p);
-+			BN_free(dmp1); BN_free(dmq1);
-+			return NULL;
-+		}
-+		BN_clear(dmp1); BN_clear(dmq1);
-+		buffer_get_bignum_bits(b, d);
-+		buffer_get_bignum_bits(b, n);
-+		buffer_get_bignum_bits(b, iqmp);
-+		buffer_get_bignum_bits(b, q);
-+		buffer_get_bignum_bits(b, p);
-+		if (RSA_set0_key(key->rsa, n, rsa_e, d) == 0)
-+			goto null;
-+		n = d = NULL;
-+		if (RSA_set0_factors(key->rsa, p, q) == 0)
-+			goto null;
-+		p = q = NULL;
-+		/* dmp1, dmq1 should not be NULL for initial set0 */
-+		if (RSA_set0_crt_params(key->rsa, dmp1, dmq1, iqmp) == 0) {
-+ null:
-+			fatal("Failed to set RSA parameters");
-+			BN_free(d); BN_free(n); BN_free(iqmp);
-+			BN_free(q); BN_free(p);
-+			BN_free(dmp1); BN_free(dmq1);
-+			return NULL;
-+		}
-+		dmp1 = dmq1 = iqmp = NULL;
-+		}
- 		if ((r = ssh_rsa_generate_additional_parameters(key)) != 0)
- 			fatal("generate RSA parameters failed: %s", ssh_err(r));
- 		break;
-@@ -636,7 +694,7 @@
- 		    identity_file);
- 	}
- 	fclose(fp);
--	switch (EVP_PKEY_type(pubkey->type)) {
-+	switch (EVP_PKEY_type(EVP_PKEY_id(pubkey))) {
- 	case EVP_PKEY_RSA:
- 		if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
- 			fatal("sshkey_new failed");
-@@ -660,7 +718,7 @@
- #endif
- 	default:
- 		fatal("%s: unsupported pubkey type %d", __func__,
--		    EVP_PKEY_type(pubkey->type));
-+		    EVP_PKEY_type(EVP_PKEY_id(pubkey)));
- 	}
- 	EVP_PKEY_free(pubkey);
- 	return;
-diff -Naur old/ssh-pkcs11-client.c new/ssh-pkcs11-client.c
---- old/ssh-pkcs11-client.c	2017-10-03 21:49:05.403827361 -1000
-+++ new/ssh-pkcs11-client.c	2017-10-03 21:55:50.869718862 -1000
-@@ -143,12 +143,13 @@
- static int
- wrap_key(RSA *rsa)
- {
--	static RSA_METHOD helper_rsa;
-+	static RSA_METHOD *helper_rsa;
- 
--	memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
--	helper_rsa.name = "ssh-pkcs11-helper";
--	helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
--	RSA_set_method(rsa, &helper_rsa);
-+	if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
-+		return (-1); /* XXX but caller isn't checking */
-+	RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
-+	RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
-+	RSA_set_method(rsa, helper_rsa);
- 	return (0);
- }
- 
-diff -Naur old/ssh-pkcs11.c new/ssh-pkcs11.c
---- old/ssh-pkcs11.c	2017-10-03 21:49:05.403827361 -1000
-+++ new/ssh-pkcs11.c	2017-10-03 21:55:50.869718862 -1000
-@@ -67,7 +67,7 @@
- 	struct pkcs11_provider	*provider;
- 	CK_ULONG		slotidx;
- 	int			(*orig_finish)(RSA *rsa);
--	RSA_METHOD		rsa_method;
-+	RSA_METHOD		*rsa_method;
- 	char			*keyid;
- 	int			keyid_len;
- };
-@@ -326,13 +326,15 @@
- 		k11->keyid = xmalloc(k11->keyid_len);
- 		memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
- 	}
--	k11->orig_finish = def->finish;
--	memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
--	k11->rsa_method.name = "pkcs11";
--	k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
--	k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
--	k11->rsa_method.finish = pkcs11_rsa_finish;
--	RSA_set_method(rsa, &k11->rsa_method);
-+	k11->orig_finish = RSA_meth_get_finish(def);
-+
-+	if ((k11->rsa_method = RSA_meth_new("pkcs11", RSA_meth_get_flags(def))) == NULL)
-+		return -1;
-+	RSA_meth_set_priv_enc(k11->rsa_method, pkcs11_rsa_private_encrypt);
-+	RSA_meth_set_priv_dec(k11->rsa_method, pkcs11_rsa_private_decrypt);
-+	RSA_meth_set_finish(k11->rsa_method, pkcs11_rsa_finish);
-+
-+	RSA_set_method(rsa, k11->rsa_method);
- 	RSA_set_app_data(rsa, k11);
- 	return (0);
- }
-@@ -512,10 +514,19 @@
- 			if ((rsa = RSA_new()) == NULL) {
- 				error("RSA_new failed");
- 			} else {
--				rsa->n = BN_bin2bn(attribs[1].pValue,
--				    attribs[1].ulValueLen, NULL);
--				rsa->e = BN_bin2bn(attribs[2].pValue,
--				    attribs[2].ulValueLen, NULL);
-+				BIGNUM *n=NULL, *e=NULL;
-+				n = BN_new();
-+				e = BN_new();
-+				if (n == NULL || e == NULL)
-+					error("BN_new alloc failed");
-+				if (BN_bin2bn(attribs[1].pValue,
-+				      attribs[1].ulValueLen, n) == NULL ||
-+				    BN_bin2bn(attribs[2].pValue,
-+				      attribs[2].ulValueLen, e) == NULL)
-+					error("BN_bin2bn failed");
-+				if (RSA_set0_key(rsa, n, e, NULL) == 0)
-+					error("RSA_set0_key failed");
-+				n = e = NULL;
- 			}
- 		} else {
- 			cp = attribs[2].pValue;
-@@ -525,17 +536,20 @@
- 			    == NULL) {
- 				error("d2i_X509 failed");
- 			} else if ((evp = X509_get_pubkey(x509)) == NULL ||
--			    evp->type != EVP_PKEY_RSA ||
--			    evp->pkey.rsa == NULL) {
-+			    EVP_PKEY_id(evp) != EVP_PKEY_RSA ||
-+			    EVP_PKEY_get0_RSA(evp) == NULL) {
- 				debug("X509_get_pubkey failed or no rsa");
--			} else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
-+			} else if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp)))
- 			    == NULL) {
- 				error("RSAPublicKey_dup");
- 			}
- 			if (x509)
- 				X509_free(x509);
- 		}
--		if (rsa && rsa->n && rsa->e &&
-+		{
-+		const BIGNUM *n, *e;
-+		RSA_get0_key(rsa, &n, &e, NULL);
-+		if (rsa && n && e &&
- 		    pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
- 			if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
- 				fatal("sshkey_new failed");
-@@ -555,6 +569,7 @@
- 		} else if (rsa) {
- 			RSA_free(rsa);
- 		}
-+		}
- 		for (i = 0; i < 3; i++)
- 			free(attribs[i].pValue);
- 	}
-diff -Naur old/ssh-rsa.c new/ssh-rsa.c
---- old/ssh-rsa.c	2017-10-03 21:49:05.403827361 -1000
-+++ new/ssh-rsa.c	2017-10-03 22:06:32.005937158 -1000
-@@ -99,13 +99,27 @@
- 	}
- 	rsa = key->rsa;
- 
--	if ((BN_sub(aux, rsa->q, BN_value_one()) == 0) ||
--	    (BN_mod(rsa->dmq1, rsa->d, aux, ctx) == 0) ||
--	    (BN_sub(aux, rsa->p, BN_value_one()) == 0) ||
--	    (BN_mod(rsa->dmp1, rsa->d, aux, ctx) == 0)) {
-+	{
-+	const BIGNUM *q, *d, *p;
-+	BIGNUM *dmq1=NULL, *dmp1=NULL;
-+	if ((dmq1 = BN_new()) == NULL ||
-+	    (dmp1 = BN_new()) == NULL ) {
-+		r = SSH_ERR_ALLOC_FAIL;
-+		goto out;
-+	}
-+	RSA_get0_key(rsa, NULL, NULL, &d);
-+	RSA_get0_factors(rsa, &p, &q);
-+	if ((BN_sub(aux, q, BN_value_one()) == 0) ||
-+	    (BN_mod(dmq1, d, aux, ctx) == 0) ||
-+	    (BN_sub(aux, p, BN_value_one()) == 0) ||
-+	    (BN_mod(dmp1, d, aux, ctx) == 0) ||
-+	     RSA_set0_crt_params(rsa, dmp1, dmq1, NULL) == 0) {
- 		r = SSH_ERR_LIBCRYPTO_ERROR;
-+		BN_clear_free(dmp1);
-+		BN_clear_free(dmq1);
- 		goto out;
- 	}
-+	}
- 	r = 0;
-  out:
- 	BN_clear_free(aux);
-@@ -136,7 +150,7 @@
- 	if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
- 	    sshkey_type_plain(key->type) != KEY_RSA)
- 		return SSH_ERR_INVALID_ARGUMENT;
--	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
-+	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
- 		return SSH_ERR_KEY_LENGTH;
- 	slen = RSA_size(key->rsa);
- 	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
-@@ -210,7 +224,7 @@
- 	    sshkey_type_plain(key->type) != KEY_RSA ||
- 	    sig == NULL || siglen == 0)
- 		return SSH_ERR_INVALID_ARGUMENT;
--	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
-+	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
- 		return SSH_ERR_KEY_LENGTH;
- 
- 	if ((b = sshbuf_from(sig, siglen)) == NULL)
-diff -Naur old/sshkey.c new/sshkey.c
---- old/sshkey.c	2017-10-03 21:49:05.407160494 -1000
-+++ new/sshkey.c	2017-10-03 22:16:31.124964276 -1000
-@@ -264,10 +264,18 @@
- #ifdef WITH_OPENSSL
- 	case KEY_RSA:
- 	case KEY_RSA_CERT:
--		return BN_num_bits(k->rsa->n);
-+#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
-+		return RSA_bits(k->rsa);
-+#else
-+		return RSA_bits(key->rsa);
-+#endif
- 	case KEY_DSA:
- 	case KEY_DSA_CERT:
-+#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
-+		return DSA_bits(k->dsa);
-+#else
- 		return BN_num_bits(k->dsa->p);
-+#endif
- 	case KEY_ECDSA:
- 	case KEY_ECDSA_CERT:
- 		return sshkey_curve_nid_to_bits(k->ecdsa_nid);
-@@ -466,28 +474,55 @@
- #ifdef WITH_OPENSSL
- 	case KEY_RSA:
- 	case KEY_RSA_CERT:
-+		{
-+		BIGNUM *n=NULL, *e=NULL; /* just allocate */
- 		if ((rsa = RSA_new()) == NULL ||
--		    (rsa->n = BN_new()) == NULL ||
--		    (rsa->e = BN_new()) == NULL) {
-+		    (n = BN_new()) == NULL ||
-+		    (e = BN_new()) == NULL) {
-+			BN_free(n);
-+			BN_free(e);
- 			if (rsa != NULL)
- 				RSA_free(rsa);
- 			free(k);
- 			return NULL;
- 		}
-+		BN_clear(n); BN_clear(e);
-+		if (RSA_set0_key(rsa, n, e, NULL) == 0)
-+			return NULL;
-+		n = e = NULL;
-+		}
- 		k->rsa = rsa;
- 		break;
- 	case KEY_DSA:
- 	case KEY_DSA_CERT:
-+		{
-+		BIGNUM *p=NULL, *q=NULL, *g=NULL, *pubkey=NULL; /* just allocate */
- 		if ((dsa = DSA_new()) == NULL ||
--		    (dsa->p = BN_new()) == NULL ||
--		    (dsa->q = BN_new()) == NULL ||
--		    (dsa->g = BN_new()) == NULL ||
--		    (dsa->pub_key = BN_new()) == NULL) {
-+		    (p = BN_new()) == NULL ||
-+		    (q = BN_new()) == NULL ||
-+		    (g = BN_new()) == NULL ||
-+		    (pubkey = BN_new()) == NULL) {
-+			BN_free(p);
-+			BN_free(q);
-+			BN_free(g);
-+			BN_free(pubkey);
- 			if (dsa != NULL)
- 				DSA_free(dsa);
- 			free(k);
- 			return NULL;
- 		}
-+		if (DSA_set0_pqg(dsa, p, q, g) == 0) {
-+			BN_free(p); BN_free(q); BN_free(g);
-+			BN_free(pubkey);
-+			return NULL;
-+		}
-+		p = q = g = NULL;
-+		if (DSA_set0_key(dsa, pubkey, NULL) == 0) {
-+			BN_free(pubkey);
-+			return NULL;
-+		}
-+		pubkey = NULL;
-+		}
- 		k->dsa = dsa;
- 		break;
- 	case KEY_ECDSA:
-@@ -523,6 +558,51 @@
- #ifdef WITH_OPENSSL
- 	case KEY_RSA:
- 	case KEY_RSA_CERT:
-+#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
-+		/* Allocate BIGNUM. This is a mess.
-+		   For OpenSSL 1.1.x API these shouldn't be mandatory,
-+		   but some regression tests for non-NULL pointer of
-+		   the data. */
-+#define new_or_dup(bn, nbn) \
-+		if (bn == NULL) { \
-+			if ((nbn = BN_new()) == NULL) \
-+				return SSH_ERR_ALLOC_FAIL; \
-+		} else { \
-+			/* otherwise use-after-free will occur */ \
-+			if ((nbn = BN_dup(bn)) == NULL) \
-+				return SSH_ERR_ALLOC_FAIL; \
-+		}
-+		{
-+		const BIGNUM *d, *iqmp, *q, *p, *dmq1, *dmp1; /* allocate if NULL */
-+		BIGNUM *nd, *niqmp, *nq, *np, *ndmq1, *ndmp1;
-+
-+		RSA_get0_key(k->rsa, NULL, NULL, &d);
-+		RSA_get0_factors(k->rsa, &p, &q);
-+		RSA_get0_crt_params(k->rsa, &dmp1, &dmq1, &iqmp);
-+
-+		new_or_dup(d, nd);
-+		new_or_dup(iqmp, niqmp);
-+		new_or_dup(q, nq);
-+		new_or_dup(p, np);
-+		new_or_dup(dmq1, ndmq1);
-+		new_or_dup(dmp1, ndmp1);
-+
-+		if (RSA_set0_key(k->rsa, NULL, NULL, nd) == 0)
-+			goto error1;
-+		nd = NULL;
-+		if (RSA_set0_factors(k->rsa, np, nq) == 0)
-+			goto error1;
-+		np = nq = NULL;
-+		if (RSA_set0_crt_params(k->rsa, ndmp1, ndmq1, niqmp) == 0) {
-+error1:
-+			BN_free(nd);
-+			BN_free(np); BN_free(nq);
-+			BN_free(ndmp1); BN_free(ndmq1); BN_free(niqmp);
-+			return SSH_ERR_LIBCRYPTO_ERROR;
-+		}
-+		ndmp1 = ndmq1 = niqmp = NULL;
-+		}
-+#else
- #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
- 		if (bn_maybe_alloc_failed(k->rsa->d) ||
- 		    bn_maybe_alloc_failed(k->rsa->iqmp) ||
-@@ -531,13 +611,28 @@
- 		    bn_maybe_alloc_failed(k->rsa->dmq1) ||
- 		    bn_maybe_alloc_failed(k->rsa->dmp1))
- 			return SSH_ERR_ALLOC_FAIL;
-+#endif
- 		break;
- 	case KEY_DSA:
- 	case KEY_DSA_CERT:
-+#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
-+		{
-+		const BIGNUM *priv_key;
-+		BIGNUM *npriv_key;
-+		DSA_get0_key(k->dsa, NULL, &priv_key);
-+		new_or_dup(priv_key, npriv_key);
-+		if (DSA_set0_key(k->dsa, NULL, npriv_key) == 0) {
-+			BN_free(npriv_key);
-+			return SSH_ERR_LIBCRYPTO_ERROR;
-+		}
-+		}
-+#else
- 		if (bn_maybe_alloc_failed(k->dsa->priv_key))
- 			return SSH_ERR_ALLOC_FAIL;
-+#endif
- 		break;
- #undef bn_maybe_alloc_failed
-+#undef new_or_dup
- 	case KEY_ECDSA:
- 	case KEY_ECDSA_CERT:
- 		/* Cannot do anything until we know the group */
-@@ -655,16 +750,34 @@
- #ifdef WITH_OPENSSL
- 	case KEY_RSA_CERT:
- 	case KEY_RSA:
--		return a->rsa != NULL && b->rsa != NULL &&
--		    BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
--		    BN_cmp(a->rsa->n, b->rsa->n) == 0;
-+		{
-+		const BIGNUM *a_e, *b_e, *a_n, *b_n;
-+		const BIGNUM *a_d, *b_d;
-+		if (a->rsa == NULL) return 0;
-+		if (b->rsa == NULL) return 0;
-+		RSA_get0_key(a->rsa, &a_n, &a_e, &a_d);
-+		RSA_get0_key(b->rsa, &b_n, &b_e, &b_d);
-+		return 
-+		    BN_cmp(a_e, b_e) == 0 &&
-+		    BN_cmp(a_n, b_n) == 0;
-+		}
- 	case KEY_DSA_CERT:
- 	case KEY_DSA:
--		return a->dsa != NULL && b->dsa != NULL &&
--		    BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
--		    BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
--		    BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
--		    BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
-+		{
-+		const BIGNUM *a_p, *a_q, *a_g, *a_pub_key;
-+		const BIGNUM *b_p, *b_q, *b_g, *b_pub_key;
-+		if (a->dsa == NULL) return 0;
-+		if (b->dsa == NULL) return 0;
-+		DSA_get0_pqg(a->dsa, &a_p, &a_q, &a_g);
-+		DSA_get0_pqg(b->dsa, &b_p, &b_q, &b_g);
-+		DSA_get0_key(a->dsa, &a_pub_key, NULL);
-+		DSA_get0_key(b->dsa, &b_pub_key, NULL);
-+		return 
-+		    BN_cmp(a_p, b_p) == 0 &&
-+		    BN_cmp(a_q, b_q) == 0 &&
-+		    BN_cmp(a_g, b_g) == 0 &&
-+		    BN_cmp(a_pub_key, b_pub_key) == 0;
-+		}
- # ifdef OPENSSL_HAS_ECC
- 	case KEY_ECDSA_CERT:
- 	case KEY_ECDSA:
-@@ -742,12 +855,17 @@
- 	case KEY_DSA:
- 		if (key->dsa == NULL)
- 			return SSH_ERR_INVALID_ARGUMENT;
-+		{
-+		const BIGNUM *p, *q, *g, *pub_key;
-+		DSA_get0_pqg(key->dsa, &p, &q, &g);
-+		DSA_get0_key(key->dsa, &pub_key, NULL);
- 		if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
--		    (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
--		    (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
--		    (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
--		    (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
-+		    (ret = sshbuf_put_bignum2(b, p)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(b, q)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(b, g)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(b, pub_key)) != 0)
- 			return ret;
-+		}
- 		break;
- # ifdef OPENSSL_HAS_ECC
- 	case KEY_ECDSA:
-@@ -763,10 +881,14 @@
- 	case KEY_RSA:
- 		if (key->rsa == NULL)
- 			return SSH_ERR_INVALID_ARGUMENT;
-+		{
-+		const BIGNUM *e, *n;
-+		RSA_get0_key(key->rsa, &n, &e, NULL);
- 		if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
--		    (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
--		    (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
-+		    (ret = sshbuf_put_bignum2(b, e)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(b, n)) != 0)
- 			return ret;
-+		}
- 		break;
- #endif /* WITH_OPENSSL */
- 	case KEY_ED25519:
-@@ -1643,13 +1765,32 @@
- 	case KEY_DSA_CERT:
- 		if ((n = sshkey_new(k->type)) == NULL)
- 			return SSH_ERR_ALLOC_FAIL;
--		if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
--		    (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
--		    (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
--		    (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
-+		{
-+		const BIGNUM *p, *q, *g, *pub_key, *priv_key;
-+		BIGNUM *cp=NULL, *cq=NULL, *cg=NULL, *cpub_key=NULL;
-+		DSA_get0_pqg(k->dsa, &p, &q, &g);
-+		DSA_get0_key(k->dsa, &pub_key, &priv_key);
-+		if ((cp = BN_dup(p)) == NULL ||
-+		    (cq = BN_dup(q)) == NULL ||
-+		    (cg = BN_dup(g)) == NULL ||
-+		    (cpub_key = BN_dup(pub_key)) == NULL) {
-+			BN_free(cp); BN_free(cq); BN_free(cg);
-+			BN_free(cpub_key);
- 			sshkey_free(n);
- 			return SSH_ERR_ALLOC_FAIL;
- 		}
-+		if (DSA_set0_pqg(n->dsa, cp, cq, cg) == 0)
-+			goto error1;
-+		cp = cq = cg = NULL;
-+		if (DSA_set0_key(n->dsa, cpub_key, NULL) == 0) {
-+error1:
-+			BN_free(cp); BN_free(cq); BN_free(cg);
-+			BN_free(cpub_key);
-+			sshkey_free(n);
-+			return SSH_ERR_LIBCRYPTO_ERROR;
-+		}
-+		cpub_key = NULL;
-+		}
- 		break;
- # ifdef OPENSSL_HAS_ECC
- 	case KEY_ECDSA:
-@@ -1673,11 +1814,23 @@
- 	case KEY_RSA_CERT:
- 		if ((n = sshkey_new(k->type)) == NULL)
- 			return SSH_ERR_ALLOC_FAIL;
--		if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
--		    (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
-+		{
-+		const BIGNUM *nn, *e, *d;
-+		BIGNUM *cn=NULL, *ce=NULL;
-+		RSA_get0_key(k->rsa, &nn, &e, &d);
-+		if ((cn = BN_dup(nn)) == NULL ||
-+		    (ce = BN_dup(e)) == NULL ) {
-+			BN_free(cn); BN_free(ce);
- 			sshkey_free(n);
- 			return SSH_ERR_ALLOC_FAIL;
- 		}
-+		if (RSA_set0_key(n->rsa, cn, ce, NULL) == 0) {
-+			BN_free(cn); BN_free(ce);
-+			sshkey_free(n);
-+			return SSH_ERR_LIBCRYPTO_ERROR;
-+		}
-+		cn = ce = NULL;
-+		}
- 		break;
- #endif /* WITH_OPENSSL */
- 	case KEY_ED25519:
-@@ -1875,12 +2028,27 @@
- 			ret = SSH_ERR_ALLOC_FAIL;
- 			goto out;
- 		}
--		if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
--		    sshbuf_get_bignum2(b, key->rsa->n) != 0) {
-+		{
-+		BIGNUM *e=NULL, *n=NULL;
-+		if ((e = BN_new()) == NULL ||
-+		    (n = BN_new()) == NULL ) {
-+			ret = SSH_ERR_ALLOC_FAIL;
-+			BN_free(e); BN_free(n);
-+			goto out;
-+		}
-+		if (sshbuf_get_bignum2(b, e) != 0 ||
-+		    sshbuf_get_bignum2(b, n) != 0) {
- 			ret = SSH_ERR_INVALID_FORMAT;
-+			BN_free(e); BN_free(n);
- 			goto out;
- 		}
--		if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
-+		if (RSA_set0_key(key->rsa, n, e, NULL) == 0) {
-+			BN_free(e); BN_free(n);
-+			return SSH_ERR_LIBCRYPTO_ERROR;
-+		}
-+		n = e = NULL;
-+		}
-+		if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
- 			ret = SSH_ERR_KEY_LENGTH;
- 			goto out;
- 		}
-@@ -1900,13 +2068,36 @@
- 			ret = SSH_ERR_ALLOC_FAIL;
- 			goto out;
- 		}
--		if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
--		    sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
--		    sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
--		    sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
-+		{
-+		BIGNUM *p=NULL, *q=NULL, *g=NULL, *pub_key=NULL;
-+		if ((p = BN_new()) == NULL ||
-+		    (q = BN_new()) == NULL ||
-+		    (g = BN_new()) == NULL ||
-+		    (pub_key = BN_new()) == NULL) {
-+			ret = SSH_ERR_ALLOC_FAIL;
-+			goto error1;
-+		}
-+		if (sshbuf_get_bignum2(b, p) != 0 ||
-+		    sshbuf_get_bignum2(b, q) != 0 ||
-+		    sshbuf_get_bignum2(b, g) != 0 ||
-+		    sshbuf_get_bignum2(b, pub_key) != 0) {
- 			ret = SSH_ERR_INVALID_FORMAT;
-+			goto error1;
-+		}
-+		if (DSA_set0_pqg(key->dsa, p, q, g) == 0) {
-+			ret = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error1;
-+		}
-+		p = q = g = NULL;
-+		if (DSA_set0_key(key->dsa, pub_key, NULL) == 0) {
-+			ret = SSH_ERR_LIBCRYPTO_ERROR;
-+error1:
-+			BN_free(p); BN_free(q); BN_free(g);
-+			BN_free(pub_key);
- 			goto out;
- 		}
-+		pub_key = NULL;
-+		}
- #ifdef DEBUG_PK
- 		DSA_print_fp(stderr, key->dsa, 8);
- #endif
-@@ -2140,26 +2331,63 @@
- 			goto fail;
- 		/* FALLTHROUGH */
- 	case KEY_RSA:
--		if ((pk->rsa = RSA_new()) == NULL ||
--		    (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
--		    (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
-+		if ((pk->rsa = RSA_new()) == NULL ){
- 			ret = SSH_ERR_ALLOC_FAIL;
- 			goto fail;
- 			}
-+		{
-+		const BIGNUM *ke, *kn;
-+		BIGNUM *pke=NULL, *pkn=NULL;
-+		RSA_get0_key(k->rsa, &kn, &ke, NULL);
-+		 if ((pke = BN_dup(ke)) == NULL ||
-+		     (pkn = BN_dup(kn)) == NULL) {
-+			ret = SSH_ERR_ALLOC_FAIL;
-+			BN_free(pke); BN_free(pkn);
-+			goto fail;
-+			}
-+		if (RSA_set0_key(pk->rsa, pkn, pke, NULL) == 0) {
-+			ret = SSH_ERR_LIBCRYPTO_ERROR;
-+			BN_free(pke); BN_free(pkn);
-+			goto fail;
-+		}
-+		pkn = pke = NULL;
-+		}
- 		break;
- 	case KEY_DSA_CERT:
- 		if ((ret = sshkey_cert_copy(k, pk)) != 0)
- 			goto fail;
- 		/* FALLTHROUGH */
- 	case KEY_DSA:
--		if ((pk->dsa = DSA_new()) == NULL ||
--		    (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
--		    (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
--		    (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
--		    (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
-+		if ((pk->dsa = DSA_new()) == NULL ) {
- 			ret = SSH_ERR_ALLOC_FAIL;
- 			goto fail;
- 		}
-+		{
-+		const BIGNUM *kp, *kq, *kg, *kpub_key;
-+		BIGNUM *pkp=NULL, *pkq=NULL, *pkg=NULL, *pkpub_key=NULL;
-+		DSA_get0_pqg(k->dsa, &kp, &kq, &kg);
-+		DSA_get0_key(k->dsa, &kpub_key, NULL);
-+		if ((pkp = BN_dup(kp)) == NULL ||
-+		    (pkq = BN_dup(kq)) == NULL ||
-+		    (pkg = BN_dup(kg)) == NULL ||
-+		    (pkpub_key = BN_dup(kpub_key)) == NULL) {
-+			ret = SSH_ERR_ALLOC_FAIL;
-+			goto error1;
-+		}
-+		if (DSA_set0_pqg(pk->dsa, pkp, pkq, pkg) == 0) {
-+			ret = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error1;
-+		}
-+		pkp = pkq = pkg = NULL;
-+		if (DSA_set0_key(pk->dsa, pkpub_key, NULL) == 0) {
-+			ret = SSH_ERR_LIBCRYPTO_ERROR;
-+error1:
-+			BN_free(pkp); BN_free(pkq); BN_free(pkg);
-+			BN_free(pkpub_key);
-+			goto fail;
-+		}
-+		pkpub_key = NULL;
-+		}
- 		break;
- 	case KEY_ECDSA_CERT:
- 		if ((ret = sshkey_cert_copy(k, pk)) != 0)
-@@ -2281,11 +2509,17 @@
- 	switch (k->type) {
- #ifdef WITH_OPENSSL
- 	case KEY_DSA_CERT:
--		if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
--		    (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
--		    (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
--		    (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
-+		{
-+		const BIGNUM *p, *q, *g, *pub_key;
-+		DSA_get0_pqg(k->dsa, &p, &q, &g);
-+		DSA_get0_key(k->dsa, &pub_key, NULL);
-+		if ((ret = sshbuf_put_bignum2(cert, p)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(cert, pub_key)) != 0) {
- 			goto out;
-+		}
-+		}
- 		break;
- # ifdef OPENSSL_HAS_ECC
- 	case KEY_ECDSA_CERT:
-@@ -2298,9 +2532,15 @@
- 		break;
- # endif /* OPENSSL_HAS_ECC */
- 	case KEY_RSA_CERT:
--		if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
--		    (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
-+		{
-+		const BIGNUM *e, *n;
-+		RSA_get0_key(k->rsa, &n, &e, NULL);
-+		if (n == NULL || e == NULL ||
-+		    (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
-+		    (ret = sshbuf_put_bignum2(cert, n)) != 0) {
- 			goto out;
-+		}
-+		}
- 		break;
- #endif /* WITH_OPENSSL */
- 	case KEY_ED25519_CERT:
-@@ -2474,42 +2714,67 @@
- 	switch (key->type) {
- #ifdef WITH_OPENSSL
- 	case KEY_RSA:
--		if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
-+		{
-+		const BIGNUM *n, *e, *d, *iqmp, *p, *q;
-+		RSA_get0_key(key->rsa, &n, &e, &d);
-+		RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
-+		RSA_get0_factors(key->rsa, &p, &q);
-+		if ((r = sshbuf_put_bignum2(b, n)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, e)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, d)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, p)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, q)) != 0) {
- 			goto out;
-+		}
-+		}
- 		break;
- 	case KEY_RSA_CERT:
- 		if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- 			r = SSH_ERR_INVALID_ARGUMENT;
- 			goto out;
- 		}
-+		{
-+		const BIGNUM *d, *iqmp, *p, *q;
-+		RSA_get0_key(key->rsa, NULL, NULL, &d);
-+		RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
-+		RSA_get0_factors(key->rsa, &p, &q);
- 		if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
-+		    (r = sshbuf_put_bignum2(b, d)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, p)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, q)) != 0) {
- 			goto out;
-+		}
-+		}
- 		break;
- 	case KEY_DSA:
--		if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
-+		{
-+		const BIGNUM *p, *q, *g, *pub_key, *priv_key;
-+		DSA_get0_pqg(key->dsa, &p, &q, &g);
-+		DSA_get0_key(key->dsa, &pub_key, &priv_key);
-+		if ((r = sshbuf_put_bignum2(b, p)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, q)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, g)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, pub_key)) != 0 ||
-+		    (r = sshbuf_put_bignum2(b, priv_key)) != 0) {
- 			goto out;
-+		}
-+		}
- 		break;
- 	case KEY_DSA_CERT:
- 		if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- 			r = SSH_ERR_INVALID_ARGUMENT;
- 			goto out;
- 		}
-+		{
-+		const BIGNUM *priv_key;
-+		DSA_get0_key(key->dsa, NULL, &priv_key);
- 		if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
--		    (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
-+		    (r = sshbuf_put_bignum2(b, priv_key)) != 0) {
- 			goto out;
-+		}
-+		}
- 		break;
- # ifdef OPENSSL_HAS_ECC
- 	case KEY_ECDSA:
-@@ -2585,18 +2850,61 @@
- 			r = SSH_ERR_ALLOC_FAIL;
- 			goto out;
- 		}
--		if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
-+		{
-+		BIGNUM *p=NULL, *q=NULL, *g=NULL, *pub_key=NULL, *priv_key=NULL;
-+		if ((p = BN_new()) == NULL ||
-+		    (q = BN_new()) == NULL ||
-+		    (g = BN_new()) == NULL ||
-+		    (pub_key = BN_new()) == NULL ||
-+		    (priv_key = BN_new()) == NULL) {
-+			r = SSH_ERR_ALLOC_FAIL;
-+			goto error1;
-+		}
-+		if (p == NULL || q == NULL || g == NULL ||
-+		    pub_key == NULL || priv_key == NULL ||
-+		    (r = sshbuf_get_bignum2(buf, p)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, q)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, g)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, pub_key)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, priv_key)) != 0) {
-+			goto error1;
-+		}
-+		if (DSA_set0_pqg(k->dsa, p, q, g) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error1;
-+		}
-+		p = q = g = NULL;
-+		if (DSA_set0_key(k->dsa, pub_key, priv_key) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+error1:
-+			BN_free(p); BN_free(q); BN_free(g);
-+			BN_free(pub_key); BN_free(priv_key);
- 			goto out;
-+		}
-+		pub_key = priv_key = NULL;
-+		}
- 		break;
- 	case KEY_DSA_CERT:
--		if ((r = sshkey_froms(buf, &k)) != 0 ||
-+		{
-+		BIGNUM *priv_key=NULL;
-+		if ((priv_key = BN_new()) == NULL) {
-+			r = SSH_ERR_ALLOC_FAIL;
-+			goto out;
-+		}
-+		if (priv_key == NULL ||
-+		    (r = sshkey_froms(buf, &k)) != 0 ||
- 		    (r = sshkey_add_private(k)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
-+		    (r = sshbuf_get_bignum2(buf, priv_key)) != 0) {
-+			BN_free(priv_key);
-+			goto out;
-+		}
-+		if (DSA_set0_key(k->dsa, NULL, priv_key) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+			BN_free(priv_key);
- 			goto out;
-+		}
-+		priv_key = NULL;
-+		}
- 		break;
- # ifdef OPENSSL_HAS_ECC
- 	case KEY_ECDSA:
-@@ -2655,29 +2963,104 @@
- 			r = SSH_ERR_ALLOC_FAIL;
- 			goto out;
- 		}
--		if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
--		    (r = ssh_rsa_generate_additional_parameters(k)) != 0)
-+		{
-+		BIGNUM *n=NULL, *e=NULL, *d=NULL, *iqmp=NULL, *p=NULL, *q=NULL;
-+		BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy for RSA_set0_crt_params */
-+		if ((n = BN_new()) == NULL ||
-+		    (e = BN_new()) == NULL ||
-+		    (d = BN_new()) == NULL ||
-+		    (iqmp = BN_new()) == NULL ||
-+		    (p = BN_new()) == NULL ||
-+		    (q = BN_new()) == NULL ||
-+		    (dmp1 = BN_new()) == NULL ||
-+		    (dmq1 = BN_new()) == NULL) {
-+			r = SSH_ERR_ALLOC_FAIL;
-+			goto error2;
-+		}
-+		BN_clear(dmp1); BN_clear(dmq1);
-+		if ((r = sshbuf_get_bignum2(buf, n)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, e)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, d)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, p)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, q)) != 0) {
-+			goto error2;
-+		}
-+		if (RSA_set0_key(k->rsa, n, e, d) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error2;
-+		}
-+		n = e = d = NULL;
-+		/* dmp1,dmpq1 should be non NULL to set iqmp value */
-+		if (RSA_set0_crt_params(k->rsa, dmp1, dmq1, iqmp) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error2;
-+		}
-+		dmp1 = dmq1 = iqmp = NULL;
-+		if (RSA_set0_factors(k->rsa, p, q) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+ error2:
-+			BN_free(n); BN_free(e); BN_free(d);
-+			BN_free(iqmp);
-+			BN_free(p); BN_free(q);
-+			BN_free(dmp1); BN_free(dmq1);
-+			goto out;
-+		}
-+		p = q = NULL;
-+		if ((r = ssh_rsa_generate_additional_parameters(k)) != 0) {
- 			goto out;
--		if (BN_num_bits(k->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
-+		}
-+		}
-+		if (RSA_bits(k->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
- 			r = SSH_ERR_KEY_LENGTH;
- 			goto out;
- 		}
- 		break;
- 	case KEY_RSA_CERT:
-+		{
-+		BIGNUM *d=NULL, *iqmp=NULL, *p=NULL, *q=NULL;
-+		BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy for RSA_set0_crt_params */
-+		if ((d = BN_new()) == NULL ||
-+		    (iqmp = BN_new()) == NULL ||
-+		    (p = BN_new()) == NULL ||
-+		    (q = BN_new()) == NULL ||
-+		    (dmp1 = BN_new()) == NULL ||
-+		    (dmq1 = BN_new()) == NULL) {
-+			r = SSH_ERR_ALLOC_FAIL;
-+			goto error3;
-+		}
-+		BN_clear(dmp1); BN_clear(dmq1);
- 		if ((r = sshkey_froms(buf, &k)) != 0 ||
- 		    (r = sshkey_add_private(k)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
--		    (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
--		    (r = ssh_rsa_generate_additional_parameters(k)) != 0)
-+		    (r = sshbuf_get_bignum2(buf, d)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, p)) != 0 ||
-+		    (r = sshbuf_get_bignum2(buf, q)) != 0) {
-+			goto error3;
-+		}
-+		if (RSA_set0_key(k->rsa, NULL, NULL, d) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error3;
-+		}
-+		/* dmp1,dmpq1 should be non NULL to set value */
-+		if (RSA_set0_crt_params(k->rsa, dmp1, dmq1, iqmp) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+			goto error3;
-+		}
-+		dmp1 = dmq1 = iqmp = NULL;
-+		if (RSA_set0_factors(k->rsa, p, q) == 0) {
-+			r = SSH_ERR_LIBCRYPTO_ERROR;
-+ error3:
-+			BN_free(d); BN_free(iqmp);
-+			BN_free(p); BN_free(q);
-+			BN_free(dmp1); BN_free(dmq1);
- 			goto out;
--		if (BN_num_bits(k->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
-+		}
-+		p = q = NULL;
-+		if ((r = ssh_rsa_generate_additional_parameters(k)) != 0)
-+			goto out;
-+		}
-+		if (RSA_bits(k->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
- 			r = SSH_ERR_KEY_LENGTH;
- 			goto out;
- 		}
-@@ -3395,7 +3778,6 @@
- 		switch (pem_reason) {
- 		case EVP_R_BAD_DECRYPT:
- 			return SSH_ERR_KEY_WRONG_PASSPHRASE;
--		case EVP_R_BN_DECODE_ERROR:
- 		case EVP_R_DECODE_ERROR:
- #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
- 		case EVP_R_PRIVATE_KEY_DECODE_ERROR:
-@@ -3460,7 +3842,7 @@
- 		r = convert_libcrypto_error();
- 		goto out;
- 	}
--	if (pk->type == EVP_PKEY_RSA &&
-+	if (EVP_PKEY_id(pk) == EVP_PKEY_RSA &&
- 	    (type == KEY_UNSPEC || type == KEY_RSA)) {
- 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- 			r = SSH_ERR_ALLOC_FAIL;
-@@ -3475,11 +3857,11 @@
- 			r = SSH_ERR_LIBCRYPTO_ERROR;
- 			goto out;
- 		}
--		if (BN_num_bits(prv->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
-+		if (RSA_bits(prv->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
- 			r = SSH_ERR_KEY_LENGTH;
- 			goto out;
- 		}
--	} else if (pk->type == EVP_PKEY_DSA &&
-+	} else if (EVP_PKEY_id(pk) == EVP_PKEY_DSA &&
- 	    (type == KEY_UNSPEC || type == KEY_DSA)) {
- 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- 			r = SSH_ERR_ALLOC_FAIL;
-@@ -3491,7 +3873,7 @@
- 		DSA_print_fp(stderr, prv->dsa, 8);
- #endif
- #ifdef OPENSSL_HAS_ECC
--	} else if (pk->type == EVP_PKEY_EC &&
-+	} else if (EVP_PKEY_id(pk) == EVP_PKEY_EC &&
- 	    (type == KEY_UNSPEC || type == KEY_ECDSA)) {
- 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- 			r = SSH_ERR_ALLOC_FAIL;
diff --git a/abs/core/openssh/sshd.service b/abs/core/openssh/sshd.service
index 55ed953..a893724 100644
--- a/abs/core/openssh/sshd.service
+++ b/abs/core/openssh/sshd.service
@@ -12,6 +12,3 @@ Restart=always
 
 [Install]
 WantedBy=multi-user.target
-
-# This service file runs an SSH daemon that forks for each incoming connection.
-# If you prefer to spawn on-demand daemons, use sshd.socket and sshd@.service.
diff --git a/abs/core/openssh/sshd.socket b/abs/core/openssh/sshd.socket
deleted file mode 100644
index e09e328..0000000
--- a/abs/core/openssh/sshd.socket
+++ /dev/null
@@ -1,10 +0,0 @@
-[Unit]
-Conflicts=sshd.service
-Wants=sshdgenkeys.service
-
-[Socket]
-ListenStream=22
-Accept=yes
-
-[Install]
-WantedBy=sockets.target
diff --git a/abs/core/openssh/sshd@.service b/abs/core/openssh/sshd@.service
deleted file mode 100644
index 0201a9d..0000000
--- a/abs/core/openssh/sshd@.service
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=OpenSSH Per-Connection Daemon
-After=sshdgenkeys.service
-
-[Service]
-ExecStart=-/usr/bin/sshd -i
-StandardInput=socket
-StandardError=syslog
-KillMode=process
-- 
cgit v0.12

