5b55d0
diff -up openssh/auth-pam.c.openssl openssh/auth-pam.c
5b55d0
--- openssh/auth-pam.c.openssl	2017-09-26 13:19:31.662248869 +0200
5b55d0
+++ openssh/auth-pam.c	2017-09-26 13:19:31.793249672 +0200
5b55d0
@@ -128,6 +128,10 @@ extern u_int utmp_len;
aacf0d
 typedef pthread_t sp_pthread_t;
aacf0d
 #else
aacf0d
 typedef pid_t sp_pthread_t;
aacf0d
+# define pthread_create(a, b, c, d)	_ssh_compat_pthread_create(a, b, c, d)
aacf0d
+# define pthread_exit(a)		_ssh_compat_pthread_exit(a)
aacf0d
+# define pthread_cancel(a)		_ssh_compat_pthread_cancel(a)
aacf0d
+# define pthread_join(a, b)		_ssh_compat_pthread_join(a, b)
aacf0d
 #endif
aacf0d
 
aacf0d
 struct pam_ctxt {
5b55d0
diff -up openssh/cipher.c.openssl openssh/cipher.c
5b55d0
--- openssh/cipher.c.openssl	2017-09-26 13:19:31.782249605 +0200
5b55d0
+++ openssh/cipher.c	2017-09-26 13:27:37.424040367 +0200
5b55d0
@@ -283,7 +283,7 @@ cipher_init(struct sshcipher_ctx **ccp,
6cf9b8
 		ret = SSH_ERR_ALLOC_FAIL;
6cf9b8
 		goto out;
6cf9b8
 	}
6cf9b8
-	if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
aacf0d
+	if (EVP_CipherInit(cc->evp, type, (u_char *)key, (u_char *)iv,
aacf0d
 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
aacf0d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
6cf9b8
 		goto out;
5b55d0
@@ -301,10 +301,6 @@ cipher_init(struct sshcipher_ctx **ccp,
6cf9b8
 			goto out;
aacf0d
 		}
aacf0d
 	}
6cf9b8
-	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
aacf0d
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
6cf9b8
-		goto out;
aacf0d
-	}
5b55d0
 	ret = 0;
5b55d0
 #endif /* WITH_OPENSSL */
5b55d0
  out:
5b55d0
@@ -490,7 +486,7 @@ cipher_get_keyiv(struct sshcipher_ctx *c
5b55d0
 		   len, iv))
5b55d0
 		       return SSH_ERR_LIBCRYPTO_ERROR;
5b55d0
 	} else
5b55d0
-		memcpy(iv, cc->evp->iv, len);
5b55d0
+		memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
aacf0d
 #endif
5b55d0
 	return 0;
5b55d0
 }
5b55d0
@@ -524,14 +520,14 @@ cipher_set_keyiv(struct sshcipher_ctx *c
5b55d0
 		    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
5b55d0
 			return SSH_ERR_LIBCRYPTO_ERROR;
5b55d0
 	} else
5b55d0
-		memcpy(cc->evp->iv, iv, evplen);
5b55d0
+		memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen);
aacf0d
 #endif
5b55d0
 	return 0;
aacf0d
 }
aacf0d
 
aacf0d
 #ifdef WITH_OPENSSL
6cf9b8
-#define EVP_X_STATE(evp)	(evp)->cipher_data
6cf9b8
-#define EVP_X_STATE_LEN(evp)	(evp)->cipher->ctx_size
aacf0d
+#define EVP_X_STATE(evp)	EVP_CIPHER_CTX_get_cipher_data(evp)
aacf0d
+#define EVP_X_STATE_LEN(evp)	EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
aacf0d
 #endif
aacf0d
 
aacf0d
 int
5b55d0
diff -up openssh/ctr-cavstest.c.openssl openssh/ctr-cavstest.c
5b55d0
--- openssh/ctr-cavstest.c.openssl	2017-09-26 13:19:31.707249145 +0200
5b55d0
+++ openssh/ctr-cavstest.c	2017-09-26 13:19:31.794249679 +0200
aacf0d
@@ -144,7 +144,7 @@ int main (int argc, char *argv[])
aacf0d
                 usage();
aacf0d
         }
aacf0d
 
aacf0d
-	SSLeay_add_all_algorithms();
aacf0d
+	OpenSSL_add_all_algorithms();
aacf0d
 
aacf0d
 	c = cipher_by_name(algo);
aacf0d
 	if (c == NULL) {
5b55d0
diff -up openssh/dh.c.openssl openssh/dh.c
5b55d0
--- openssh/dh.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/dh.c	2017-09-26 13:19:31.794249679 +0200
6cf9b8
@@ -212,14 +212,15 @@ choose_dh(int min, int wantbits, int max
aacf0d
 /* diffie-hellman-groupN-sha1 */
aacf0d
 
aacf0d
 int
aacf0d
-dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
aacf0d
+dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
aacf0d
 {
aacf0d
 	int i;
aacf0d
 	int n = BN_num_bits(dh_pub);
aacf0d
 	int bits_set = 0;
aacf0d
 	BIGNUM *tmp;
aacf0d
+	const BIGNUM *p;
aacf0d
 
aacf0d
-	if (dh_pub->neg) {
aacf0d
+	if (BN_is_negative(dh_pub)) {
aacf0d
 		logit("invalid public DH value: negative");
aacf0d
 		return 0;
aacf0d
 	}
6cf9b8
@@ -232,7 +233,8 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
aacf0d
 		error("%s: BN_new failed", __func__);
aacf0d
 		return 0;
aacf0d
 	}
aacf0d
-	if (!BN_sub(tmp, dh->p, BN_value_one()) ||
aacf0d
+	DH_get0_pqg(dh, &p, NULL, NULL);
aacf0d
+	if (!BN_sub(tmp, p, BN_value_one()) ||
aacf0d
 	    BN_cmp(dh_pub, tmp) != -1) {		/* pub_exp > p-2 */
aacf0d
 		BN_clear_free(tmp);
aacf0d
 		logit("invalid public DH value: >= p-1");
6cf9b8
@@ -243,14 +245,14 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
aacf0d
 	for (i = 0; i <= n; i++)
aacf0d
 		if (BN_is_bit_set(dh_pub, i))
aacf0d
 			bits_set++;
aacf0d
-	debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
aacf0d
+	debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
aacf0d
 
aacf0d
 	/*
aacf0d
 	 * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
aacf0d
 	 */
aacf0d
 	if (bits_set < 4) {
aacf0d
 		logit("invalid public DH value (%d/%d)",
aacf0d
-		   bits_set, BN_num_bits(dh->p));
aacf0d
+		   bits_set, BN_num_bits(p));
aacf0d
 		return 0;
aacf0d
 	}
aacf0d
 	return 1;
6cf9b8
@@ -260,9 +262,11 @@ int
aacf0d
 dh_gen_key(DH *dh, int need)
aacf0d
 {
aacf0d
 	int pbits;
aacf0d
+	const BIGNUM *p, *pub_key;
aacf0d
 
aacf0d
-	if (need < 0 || dh->p == NULL ||
aacf0d
-	    (pbits = BN_num_bits(dh->p)) <= 0 ||
aacf0d
+	DH_get0_pqg(dh, &p, NULL, NULL);
aacf0d
+	if (need < 0 || p == NULL ||
aacf0d
+	    (pbits = BN_num_bits(p)) <= 0 ||
aacf0d
 	    need > INT_MAX / 2 || 2 * need > pbits)
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	if (need < 256)
bbf61d
@@ -271,11 +275,11 @@ dh_gen_key(DH *dh, int need)
aacf0d
 	 * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
aacf0d
 	 * so double requested need here.
aacf0d
 	 */
6cf9b8
-	dh->length = MINIMUM(need * 2, pbits - 1);
aacf0d
-	if (DH_generate_key(dh) == 0 ||
aacf0d
-	    !dh_pub_is_valid(dh, dh->pub_key)) {
aacf0d
-		BN_clear_free(dh->priv_key);
bbf61d
-		dh->priv_key = NULL;
aacf0d
+	DH_set_length(dh, MINIMUM(need * 2, pbits - 1));
aacf0d
+	if (DH_generate_key(dh) == 0)
aacf0d
+		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
6cf9b8
+	if (!dh_pub_is_valid(dh, pub_key)) {
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
6cf9b8
 	}
aacf0d
 	return 0;
5b55d0
@@ -284,15 +289,22 @@ DH *
aacf0d
 dh_new_group_asc(const char *gen, const char *modulus)
aacf0d
 {
aacf0d
 	DH *dh;
e0e7ed
+	BIGNUM *p = NULL, *g = NULL;
aacf0d
 
aacf0d
-	if ((dh = DH_new()) == NULL)
aacf0d
-		return NULL;
aacf0d
-	if (BN_hex2bn(&dh->p, modulus) == 0 ||
aacf0d
-	    BN_hex2bn(&dh->g, gen) == 0) {
aacf0d
-		DH_free(dh);
aacf0d
-		return NULL;
aacf0d
-	}
aacf0d
+	if ((dh = DH_new()) == NULL ||
aacf0d
+	    (p = BN_new()) == NULL ||
aacf0d
+	    (g = BN_new()) == NULL)
aacf0d
+		goto err;
aacf0d
+	if (BN_hex2bn(&p, modulus) == 0 ||
aacf0d
+	    BN_hex2bn(&g, gen) == 0 ||
aacf0d
+	    DH_set0_pqg(dh, p, NULL, g) == 0)
aacf0d
+		goto err;
aacf0d
 	return (dh);
aacf0d
+err:
aacf0d
+	DH_free(dh);
aacf0d
+	BN_free(p);
aacf0d
+	BN_free(g);
aacf0d
+	return NULL;
aacf0d
 }
aacf0d
 
aacf0d
 /*
5b55d0
@@ -307,8 +319,7 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulu
aacf0d
 
aacf0d
 	if ((dh = DH_new()) == NULL)
aacf0d
 		return NULL;
aacf0d
-	dh->p = modulus;
aacf0d
-	dh->g = gen;
aacf0d
+	DH_set0_pqg(dh, modulus, NULL, gen);
aacf0d
 
aacf0d
 	return (dh);
aacf0d
 }
5b55d0
diff -up openssh/dh.h.openssl openssh/dh.h
5b55d0
--- openssh/dh.h.openssl	2017-09-26 13:19:31.783249611 +0200
5b55d0
+++ openssh/dh.h	2017-09-26 13:19:31.794249679 +0200
aacf0d
@@ -42,7 +42,7 @@ DH	*dh_new_group18(void);
aacf0d
 DH	*dh_new_group_fallback(int);
aacf0d
 
aacf0d
 int	 dh_gen_key(DH *, int);
aacf0d
-int	 dh_pub_is_valid(DH *, BIGNUM *);
aacf0d
+int	 dh_pub_is_valid(const DH *, const BIGNUM *);
aacf0d
 
aacf0d
 u_int	 dh_estimate(int);
aacf0d
 
5b55d0
diff -up openssh/digest-openssl.c.openssl openssh/digest-openssl.c
5b55d0
--- openssh/digest-openssl.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/digest-openssl.c	2017-09-26 13:19:31.795249685 +0200
aacf0d
@@ -43,7 +43,7 @@
aacf0d
 
aacf0d
 struct ssh_digest_ctx {
aacf0d
 	int alg;
aacf0d
-	EVP_MD_CTX mdctx;
aacf0d
+	EVP_MD_CTX *mdctx;
aacf0d
 };
aacf0d
 
aacf0d
 struct ssh_digest {
5b55d0
@@ -106,7 +106,7 @@ ssh_digest_bytes(int alg)
aacf0d
 size_t
aacf0d
 ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
aacf0d
 {
aacf0d
-	return EVP_MD_CTX_block_size(&ctx->mdctx);
aacf0d
+	return EVP_MD_CTX_block_size(ctx->mdctx);
aacf0d
 }
aacf0d
 
aacf0d
 struct ssh_digest_ctx *
e0e7ed
@@ -118,8 +118,10 @@ ssh_digest_start(int alg)
aacf0d
 	if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
aacf0d
 		return NULL;
aacf0d
 	ret->alg = alg;
aacf0d
-	EVP_MD_CTX_init(&ret->mdctx);
aacf0d
-	if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
aacf0d
+	ret->mdctx = EVP_MD_CTX_new();
aacf0d
+	if (ret->mdctx == NULL ||
aacf0d
+	    EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
e0e7ed
+		EVP_MD_CTX_free(ret->mdctx);
aacf0d
 		free(ret);
aacf0d
 		return NULL;
aacf0d
 	}
5b55d0
@@ -132,7 +133,7 @@ ssh_digest_copy_state(struct ssh_digest_
aacf0d
 	if (from->alg != to->alg)
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	/* we have bcopy-style order while openssl has memcpy-style */
aacf0d
-	if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
aacf0d
+	if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 	return 0;
aacf0d
 }
5b55d0
@@ -140,7 +141,7 @@ ssh_digest_copy_state(struct ssh_digest_
aacf0d
 int
aacf0d
 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
aacf0d
 {
aacf0d
-	if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
aacf0d
+	if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 	return 0;
aacf0d
 }
5b55d0
@@ -161,7 +162,7 @@ ssh_digest_final(struct ssh_digest_ctx *
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	if (dlen < digest->digest_len) /* No truncation allowed */
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
-	if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
aacf0d
+	if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 	if (l != digest->digest_len) /* sanity */
aacf0d
 		return SSH_ERR_INTERNAL_ERROR;
5b55d0
@@ -172,7 +173,7 @@ void
aacf0d
 ssh_digest_free(struct ssh_digest_ctx *ctx)
aacf0d
 {
aacf0d
 	if (ctx != NULL) {
aacf0d
-		EVP_MD_CTX_cleanup(&ctx->mdctx);
aacf0d
+		EVP_MD_CTX_free(ctx->mdctx);
aacf0d
 		explicit_bzero(ctx, sizeof(*ctx));
aacf0d
 		free(ctx);
aacf0d
 	}
5b55d0
diff -up openssh/entropy.c.openssl openssh/entropy.c
5b55d0
--- openssh/entropy.c.openssl	2017-09-26 13:19:31.783249611 +0200
5b55d0
+++ openssh/entropy.c	2017-09-26 13:19:31.795249685 +0200
aacf0d
@@ -218,7 +218,9 @@ seed_rng(void)
aacf0d
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
aacf0d
 
aacf0d
 	/* clean the PRNG status when exiting the program */
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
 	atexit(RAND_cleanup);
aacf0d
+#endif
aacf0d
 
aacf0d
 #ifndef OPENSSL_PRNG_ONLY
aacf0d
 	if (RAND_status() == 1) {
5b55d0
diff -up openssh/gss-genr.c.openssl openssh/gss-genr.c
5b55d0
--- openssh/gss-genr.c.openssl	2017-09-26 13:19:31.773249550 +0200
5b55d0
+++ openssh/gss-genr.c	2017-09-26 13:19:31.796249691 +0200
6cf9b8
@@ -99,7 +99,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 	u_char digest[EVP_MAX_MD_SIZE];
aacf0d
 	char deroid[2];
aacf0d
 	const EVP_MD *evp_md = EVP_md5();
aacf0d
-	EVP_MD_CTX md;
aacf0d
+	EVP_MD_CTX *md;
aacf0d
 	char *s, *cp, *p;
aacf0d
 
aacf0d
 	if (gss_enc2oid != NULL) {
6cf9b8
@@ -113,6 +113,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
bbf61d
 	if ((buf = sshbuf_new()) == NULL)
bbf61d
 		fatal("%s: sshbuf_new failed", __func__);
aacf0d
 
aacf0d
+	md = EVP_MD_CTX_new();
aacf0d
 	oidpos = 0;
aacf0d
 	s = cp = xstrdup(kex);
aacf0d
 	for (i = 0; i < gss_supported->count; i++) {
6cf9b8
@@ -122,12 +123,13 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 			deroid[0] = SSH_GSS_OIDTYPE;
aacf0d
 			deroid[1] = gss_supported->elements[i].length;
aacf0d
 
aacf0d
-			EVP_DigestInit(&md, evp_md);
aacf0d
-			EVP_DigestUpdate(&md, deroid, 2);
aacf0d
-			EVP_DigestUpdate(&md,
aacf0d
+			EVP_MD_CTX_reset(md);
aacf0d
+			EVP_DigestInit(md, evp_md);
aacf0d
+			EVP_DigestUpdate(md, deroid, 2);
aacf0d
+			EVP_DigestUpdate(md,
aacf0d
 			    gss_supported->elements[i].elements,
aacf0d
 			    gss_supported->elements[i].length);
aacf0d
-			EVP_DigestFinal(&md, digest, NULL);
aacf0d
+			EVP_DigestFinal(md, digest, NULL);
aacf0d
 
aacf0d
 			encoded = xmalloc(EVP_MD_size(evp_md) * 2);
aacf0d
 			enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
6cf9b8
@@ -149,6 +151,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 		}
aacf0d
 	}
aacf0d
 	free(s);
aacf0d
+	EVP_MD_CTX_free(md);
aacf0d
 	gss_enc2oid[oidpos].oid = NULL;
aacf0d
 	gss_enc2oid[oidpos].encoded = NULL;
aacf0d
 
5b55d0
diff -up openssh/includes.h.openssl openssh/includes.h
5b55d0
--- openssh/includes.h.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/includes.h	2017-09-26 13:19:31.796249691 +0200
5b55d0
@@ -166,6 +166,7 @@
aacf0d
 
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
 #include <openssl/opensslv.h> /* For OPENSSL_VERSION_NUMBER */
aacf0d
+#include "libcrypto-compat.h"
aacf0d
 #endif
aacf0d
 
aacf0d
 #include "defines.h"
5b55d0
diff -up openssh/kexdhc.c.openssl openssh/kexdhc.c
5b55d0
--- openssh/kexdhc.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/kexdhc.c	2017-09-26 13:19:31.797249697 +0200
aacf0d
@@ -56,6 +56,7 @@ kexdh_client(struct ssh *ssh)
aacf0d
 {
aacf0d
 	struct kex *kex = ssh->kex;
aacf0d
 	int r;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	/* generate and send 'e', client DH public key */
aacf0d
 	switch (kex->kex_type) {
aacf0d
@@ -81,21 +82,27 @@ kexdh_client(struct ssh *ssh)
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 	debug("sending SSH2_MSG_KEXDH_INIT");
aacf0d
-	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
aacf0d
-	    (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
aacf0d
+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
aacf0d
+		goto out;
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
aacf0d
 #ifdef DEBUG_KEXDH
aacf0d
 	DHparams_print_fp(stderr, kex->dh);
aacf0d
 	fprintf(stderr, "pub= ");
aacf0d
-	BN_print_fp(stderr, kex->dh->pub_key);
aacf0d
+	BN_print_fp(stderr, pub_key);
aacf0d
 	fprintf(stderr, "\n");
aacf0d
 #endif
aacf0d
 	debug("expecting SSH2_MSG_KEXDH_REPLY");
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
5b55d0
@@ -109,6 +116,7 @@ input_kex_dh(int type, u_int32_t seq, st
aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
aacf0d
 	size_t klen = 0, slen, sbloblen, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	if (kex->verify_host_key == NULL) {
aacf0d
 		r = SSH_ERR_INVALID_ARGUMENT;
5b55d0
@@ -168,6 +176,7 @@ input_kex_dh(int type, u_int32_t seq, st
aacf0d
 #endif
aacf0d
 
aacf0d
 	/* calc and verify H */
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	hashlen = sizeof(hash);
aacf0d
 	if ((r = kex_dh_hash(
aacf0d
 	    kex->hash_alg,
5b55d0
@@ -176,7 +185,7 @@ input_kex_dh(int type, u_int32_t seq, st
aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
aacf0d
 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    pub_key,
aacf0d
 	    dh_server_pub,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
5b55d0
diff -up openssh/kexdhs.c.openssl openssh/kexdhs.c
5b55d0
--- openssh/kexdhs.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/kexdhs.c	2017-09-26 13:19:31.797249697 +0200
aacf0d
@@ -87,6 +87,10 @@ kexdh_server(struct ssh *ssh)
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
5b55d0
@@ -101,6 +105,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 	size_t sbloblen, slen;
aacf0d
 	size_t klen = 0, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	if (kex->load_host_public_key == NULL ||
aacf0d
 	    kex->load_host_private_key == NULL) {
5b55d0
@@ -163,6 +168,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 		goto out;
aacf0d
 	/* calc H */
aacf0d
 	hashlen = sizeof(hash);
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	if ((r = kex_dh_hash(
aacf0d
 	    kex->hash_alg,
aacf0d
 	    kex->client_version_string,
5b55d0
@@ -171,7 +177,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
 	    dh_client_pub,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    pub_key,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
aacf0d
 		goto out;
5b55d0
@@ -197,7 +203,7 @@ input_kex_dh_init(int type, u_int32_t se
bbf61d
 	/* send server hostkey, DH pubkey 'f' and signed H */
aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
aacf0d
 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||	/* f */
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||	/* f */
aacf0d
 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
5b55d0
diff -up openssh/kexgexc.c.openssl openssh/kexgexc.c
5b55d0
--- openssh/kexgexc.c.openssl	2017-09-26 13:19:31.783249611 +0200
5b55d0
+++ openssh/kexgexc.c	2017-09-26 13:19:31.797249697 +0200
5b55d0
@@ -95,6 +95,7 @@ input_kex_dh_gex_group(int type, u_int32
aacf0d
 	struct kex *kex = ssh->kex;
aacf0d
 	BIGNUM *p = NULL, *g = NULL;
aacf0d
 	int r, bits;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
aacf0d
 
3cd489
@@ -119,24 +120,30 @@ input_kex_dh_gex_group(int type, u_int32
aacf0d
 	p = g = NULL; /* belong to kex->dh now */
aacf0d
 
aacf0d
 	/* generate and send 'e', client DH public key */
aacf0d
-	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
aacf0d
-	    (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
aacf0d
+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
aacf0d
+		goto out;
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
aacf0d
 #ifdef DEBUG_KEXDH
aacf0d
 	DHparams_print_fp(stderr, kex->dh);
aacf0d
 	fprintf(stderr, "pub= ");
aacf0d
-	BN_print_fp(stderr, kex->dh->pub_key);
aacf0d
+	BN_print_fp(stderr, pub_key);
aacf0d
 	fprintf(stderr, "\n");
aacf0d
 #endif
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
aacf0d
 	r = 0;
aacf0d
 out:
3cd489
 	BN_clear_free(p);
3cd489
 	BN_clear_free(g);
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
5b55d0
@@ -152,6 +157,7 @@ input_kex_dh_gex_reply(int type, u_int32
aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
aacf0d
 	size_t klen = 0, slen, sbloblen, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *p, *g, *pub_key;
aacf0d
 
aacf0d
 	debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
aacf0d
 	if (kex->verify_host_key == NULL) {
5b55d0
@@ -214,6 +220,8 @@ input_kex_dh_gex_reply(int type, u_int32
aacf0d
 		kex->min = kex->max = -1;
aacf0d
 
aacf0d
 	/* calc and verify H */
aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	hashlen = sizeof(hash);
aacf0d
 	if ((r = kexgex_hash(
aacf0d
 	    kex->hash_alg,
5b55d0
@@ -223,8 +231,8 @@ input_kex_dh_gex_reply(int type, u_int32
aacf0d
 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
 	    kex->min, kex->nbits, kex->max,
aacf0d
-	    kex->dh->p, kex->dh->g,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    p, g,
aacf0d
+	    pub_key,
aacf0d
 	    dh_server_pub,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
5b55d0
diff -up openssh/kexgexs.c.openssl openssh/kexgexs.c
5b55d0
--- openssh/kexgexs.c.openssl	2017-09-26 13:19:31.783249611 +0200
5b55d0
+++ openssh/kexgexs.c	2017-09-26 13:19:31.797249697 +0200
5b55d0
@@ -72,6 +72,7 @@ input_kex_dh_gex_request(int type, u_int
aacf0d
 	struct kex *kex = ssh->kex;
aacf0d
 	int r;
aacf0d
 	u_int min = 0, max = 0, nbits = 0;
aacf0d
+	const BIGNUM *p, *g;
aacf0d
 
aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
aacf0d
 	if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
5b55d0
@@ -101,9 +102,10 @@ input_kex_dh_gex_request(int type, u_int
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
aacf0d
 
5b55d0
@@ -115,6 +117,10 @@ input_kex_dh_gex_request(int type, u_int
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
5b55d0
@@ -129,6 +135,7 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 	size_t sbloblen, slen;
aacf0d
 	size_t klen = 0, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *p, *g, *pub_key;
aacf0d
 
aacf0d
 	if (kex->load_host_public_key == NULL ||
aacf0d
 	    kex->load_host_private_key == NULL) {
5b55d0
@@ -191,6 +198,8 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 		goto out;
aacf0d
 	/* calc H */
aacf0d
 	hashlen = sizeof(hash);
aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	if ((r = kexgex_hash(
aacf0d
 	    kex->hash_alg,
aacf0d
 	    kex->client_version_string,
5b55d0
@@ -199,9 +208,9 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
 	    kex->min, kex->nbits, kex->max,
aacf0d
-	    kex->dh->p, kex->dh->g,
aacf0d
+	    p, g,
aacf0d
 	    dh_client_pub,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    pub_key,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
aacf0d
 		goto out;
5b55d0
@@ -227,7 +236,7 @@ input_kex_dh_gex_init(int type, u_int32_
bbf61d
 	/* send server hostkey, DH pubkey 'f' and signed H */
aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
aacf0d
 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
aacf0d
 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
5b55d0
diff -up openssh/kexgssc.c.openssl openssh/kexgssc.c
5b55d0
--- openssh/kexgssc.c.openssl	2017-09-26 13:19:31.771249537 +0200
5b55d0
+++ openssh/kexgssc.c	2017-09-26 13:19:31.797249697 +0200
5b55d0
@@ -59,6 +59,7 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 	BIGNUM *shared_secret = NULL;
aacf0d
 	BIGNUM *p = NULL;
aacf0d
 	BIGNUM *g = NULL;	
aacf0d
+	const BIGNUM *pub_key, *p1, *g1;
aacf0d
 	u_char *kbuf;
aacf0d
 	u_char *serverhostkey = NULL;
aacf0d
 	u_char *empty = "";
5b55d0
@@ -126,6 +127,7 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 	
aacf0d
 	/* Step 1 - e is dh->pub_key */
aacf0d
 	dh_gen_key(dh, ssh->kex->we_need * 8);
aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
aacf0d
 
aacf0d
 	/* This is f, we initialise it now to make life easier */
aacf0d
 	dh_server_pub = BN_new();
5b55d0
@@ -173,7 +175,7 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 				packet_start(SSH2_MSG_KEXGSS_INIT);
aacf0d
 				packet_put_string(send_tok.value,
aacf0d
 				    send_tok.length);
aacf0d
-				packet_put_bignum2(dh->pub_key);
aacf0d
+				packet_put_bignum2((BIGNUM *)pub_key);
aacf0d
 				first = 0;
aacf0d
 			} else {
aacf0d
 				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
5b55d0
@@ -282,13 +284,14 @@ kexgss_client(struct ssh *ssh) {
bbf61d
 		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
bbf61d
 		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
aacf0d
 		    (serverhostkey ? serverhostkey : empty), slen,
aacf0d
-		    dh->pub_key,	/* e */
aacf0d
+		    pub_key,		/* e */
aacf0d
 		    dh_server_pub,	/* f */
aacf0d
 		    shared_secret,	/* K */
aacf0d
 		    hash, &hashlen
aacf0d
 		);
aacf0d
 		break;
aacf0d
 	case KEX_GSS_GEX_SHA1:
aacf0d
+		DH_get0_pqg(dh, &p1, NULL, &g1;;
aacf0d
 		kexgex_hash(
aacf0d
 		    ssh->kex->hash_alg,
aacf0d
 		    ssh->kex->client_version_string,
5b55d0
@@ -297,8 +300,8 @@ kexgss_client(struct ssh *ssh) {
bbf61d
 		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
aacf0d
 		    (serverhostkey ? serverhostkey : empty), slen,
aacf0d
  		    min, nbits, max,
aacf0d
-		    dh->p, dh->g,
aacf0d
-		    dh->pub_key,
aacf0d
+		    p1, g1,
aacf0d
+		    pub_key,
aacf0d
 		    dh_server_pub,
aacf0d
 		    shared_secret,
aacf0d
 		    hash, &hashlen
5b55d0
diff -up openssh/kexgsss.c.openssl openssh/kexgsss.c
5b55d0
--- openssh/kexgsss.c.openssl	2017-09-26 13:19:31.771249537 +0200
5b55d0
+++ openssh/kexgsss.c	2017-09-26 13:19:31.798249703 +0200
5b55d0
@@ -78,6 +78,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 	char *mechs;
aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
aacf0d
 	size_t hashlen;
aacf0d
+	const BIGNUM *p, *g, *pub_key;
aacf0d
 
aacf0d
 	/* Initialise GSSAPI */
aacf0d
 
5b55d0
@@ -127,9 +128,10 @@ kexgss_server(struct ssh *ssh)
aacf0d
 		if (dh == NULL)
aacf0d
 			packet_disconnect("Protocol error: no matching group found");
aacf0d
 
aacf0d
+		DH_get0_pqg(dh, &p, NULL, &g);
aacf0d
 		packet_start(SSH2_MSG_KEXGSS_GROUP);
aacf0d
-		packet_put_bignum2(dh->p);
aacf0d
-		packet_put_bignum2(dh->g);
aacf0d
+		packet_put_bignum2((BIGNUM *)p);
aacf0d
+		packet_put_bignum2((BIGNUM *)g);
aacf0d
 		packet_send();
aacf0d
 
aacf0d
 		packet_write_wait();
5b55d0
@@ -221,6 +223,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 	memset(kbuf, 0, klen);
aacf0d
 	free(kbuf);
aacf0d
 
aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
aacf0d
 	hashlen = sizeof(hash);
aacf0d
 	switch (ssh->kex->kex_type) {
aacf0d
 	case KEX_GSS_GRP1_SHA1:
5b55d0
@@ -232,7 +235,7 @@ kexgss_server(struct ssh *ssh)
bbf61d
 		    sshbuf_ptr(ssh->kex->peer), sshbuf_len(ssh->kex->peer),
bbf61d
 		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
aacf0d
 		    NULL, 0, /* Change this if we start sending host keys */
aacf0d
-		    dh_client_pub, dh->pub_key, shared_secret,
aacf0d
+		    dh_client_pub, pub_key, shared_secret,
aacf0d
 		    hash, &hashlen
aacf0d
 		);
aacf0d
 		break;
5b55d0
@@ -244,9 +247,9 @@ kexgss_server(struct ssh *ssh)
bbf61d
 		    sshbuf_ptr(ssh->kex->my), sshbuf_len(ssh->kex->my),
aacf0d
 		    NULL, 0,
aacf0d
 		    cmin, nbits, cmax,
aacf0d
-		    dh->p, dh->g,
aacf0d
+		    p, g,
aacf0d
 		    dh_client_pub,
aacf0d
-		    dh->pub_key,
aacf0d
+		    pub_key,
aacf0d
 		    shared_secret,
aacf0d
 		    hash, &hashlen
aacf0d
 		);
5b55d0
@@ -270,7 +273,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 		fatal("Couldn't get MIC");
aacf0d
 
aacf0d
 	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
aacf0d
-	packet_put_bignum2(dh->pub_key);
aacf0d
+	packet_put_bignum2((BIGNUM *)pub_key);
aacf0d
 	packet_put_string(msg_tok.value,msg_tok.length);
aacf0d
 
aacf0d
 	if (send_tok.length != 0) {
5b55d0
diff -up openssh/libcrypto-compat.c.openssl openssh/libcrypto-compat.c
5b55d0
--- openssh/libcrypto-compat.c.openssl	2017-09-26 13:19:31.798249703 +0200
5b55d0
+++ openssh/libcrypto-compat.c	2017-09-26 13:19:31.798249703 +0200
e0e7ed
@@ -0,0 +1,428 @@
aacf0d
+/*
aacf0d
+ * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
aacf0d
+ *
aacf0d
+ * Licensed under the OpenSSL license (the "License").  You may not use
aacf0d
+ * this file except in compliance with the License.  You can obtain a copy
aacf0d
+ * in the file LICENSE in the source distribution or at
aacf0d
+ * https://www.openssl.org/source/license.html
aacf0d
+ */
aacf0d
+
aacf0d
+#include "includes.h"
aacf0d
+
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
+
aacf0d
+#include <string.h>
aacf0d
+#include <openssl/engine.h>
aacf0d
+
aacf0d
+static void *OPENSSL_zalloc(size_t num)
aacf0d
+{
aacf0d
+    void *ret = OPENSSL_malloc(num);
aacf0d
+
aacf0d
+    if (ret != NULL)
aacf0d
+        memset(ret, 0, num);
aacf0d
+    return ret;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
aacf0d
+{
aacf0d
+    /* If the fields n and e in r are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL for n and e.  d may be
aacf0d
+     * left NULL (in case only the public key is used).
aacf0d
+     */
aacf0d
+    if ((r->n == NULL && n == NULL)
aacf0d
+        || (r->e == NULL && e == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (n != NULL) {
aacf0d
+        BN_free(r->n);
aacf0d
+        r->n = n;
aacf0d
+    }
aacf0d
+    if (e != NULL) {
aacf0d
+        BN_free(r->e);
aacf0d
+        r->e = e;
aacf0d
+    }
aacf0d
+    if (d != NULL) {
aacf0d
+        BN_free(r->d);
aacf0d
+        r->d = d;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
aacf0d
+{
aacf0d
+    /* If the fields p and q in r are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.
aacf0d
+     */
aacf0d
+    if ((r->p == NULL && p == NULL)
aacf0d
+        || (r->q == NULL && q == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (p != NULL) {
aacf0d
+        BN_free(r->p);
aacf0d
+        r->p = p;
aacf0d
+    }
aacf0d
+    if (q != NULL) {
aacf0d
+        BN_free(r->q);
aacf0d
+        r->q = q;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
aacf0d
+{
aacf0d
+    /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.
aacf0d
+     */
aacf0d
+    if ((r->dmp1 == NULL && dmp1 == NULL)
aacf0d
+        || (r->dmq1 == NULL && dmq1 == NULL)
aacf0d
+        || (r->iqmp == NULL && iqmp == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (dmp1 != NULL) {
aacf0d
+        BN_free(r->dmp1);
aacf0d
+        r->dmp1 = dmp1;
aacf0d
+    }
aacf0d
+    if (dmq1 != NULL) {
aacf0d
+        BN_free(r->dmq1);
aacf0d
+        r->dmq1 = dmq1;
aacf0d
+    }
aacf0d
+    if (iqmp != NULL) {
aacf0d
+        BN_free(r->iqmp);
aacf0d
+        r->iqmp = iqmp;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_get0_key(const RSA *r,
aacf0d
+                  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
aacf0d
+{
aacf0d
+    if (n != NULL)
aacf0d
+        *n = r->n;
aacf0d
+    if (e != NULL)
aacf0d
+        *e = r->e;
aacf0d
+    if (d != NULL)
aacf0d
+        *d = r->d;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
aacf0d
+{
aacf0d
+    if (p != NULL)
aacf0d
+        *p = r->p;
aacf0d
+    if (q != NULL)
aacf0d
+        *q = r->q;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_get0_crt_params(const RSA *r,
aacf0d
+                         const BIGNUM **dmp1, const BIGNUM **dmq1,
aacf0d
+                         const BIGNUM **iqmp)
aacf0d
+{
aacf0d
+    if (dmp1 != NULL)
aacf0d
+        *dmp1 = r->dmp1;
aacf0d
+    if (dmq1 != NULL)
aacf0d
+        *dmq1 = r->dmq1;
aacf0d
+    if (iqmp != NULL)
aacf0d
+        *iqmp = r->iqmp;
aacf0d
+}
aacf0d
+
aacf0d
+void DSA_get0_pqg(const DSA *d,
aacf0d
+                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
aacf0d
+{
aacf0d
+    if (p != NULL)
aacf0d
+        *p = d->p;
aacf0d
+    if (q != NULL)
aacf0d
+        *q = d->q;
aacf0d
+    if (g != NULL)
aacf0d
+        *g = d->g;
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
aacf0d
+{
aacf0d
+    /* If the fields p, q and g in d are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.
aacf0d
+     */
aacf0d
+    if ((d->p == NULL && p == NULL)
aacf0d
+        || (d->q == NULL && q == NULL)
aacf0d
+        || (d->g == NULL && g == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (p != NULL) {
aacf0d
+        BN_free(d->p);
aacf0d
+        d->p = p;
aacf0d
+    }
aacf0d
+    if (q != NULL) {
aacf0d
+        BN_free(d->q);
aacf0d
+        d->q = q;
aacf0d
+    }
aacf0d
+    if (g != NULL) {
aacf0d
+        BN_free(d->g);
aacf0d
+        d->g = g;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DSA_get0_key(const DSA *d,
aacf0d
+                  const BIGNUM **pub_key, const BIGNUM **priv_key)
aacf0d
+{
aacf0d
+    if (pub_key != NULL)
aacf0d
+        *pub_key = d->pub_key;
aacf0d
+    if (priv_key != NULL)
aacf0d
+        *priv_key = d->priv_key;
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
aacf0d
+{
aacf0d
+    /* If the field pub_key in d is NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.  The priv_key field may
aacf0d
+     * be left NULL.
aacf0d
+     */
aacf0d
+    if (d->pub_key == NULL && pub_key == NULL)
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (pub_key != NULL) {
aacf0d
+        BN_free(d->pub_key);
aacf0d
+        d->pub_key = pub_key;
aacf0d
+    }
aacf0d
+    if (priv_key != NULL) {
aacf0d
+        BN_free(d->priv_key);
aacf0d
+        d->priv_key = priv_key;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
aacf0d
+{
aacf0d
+    if (pr != NULL)
aacf0d
+        *pr = sig->r;
aacf0d
+    if (ps != NULL)
aacf0d
+        *ps = sig->s;
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
aacf0d
+{
aacf0d
+    if (r == NULL || s == NULL)
aacf0d
+        return 0;
aacf0d
+    BN_clear_free(sig->r);
aacf0d
+    BN_clear_free(sig->s);
aacf0d
+    sig->r = r;
aacf0d
+    sig->s = s;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
aacf0d
+{
aacf0d
+    if (pr != NULL)
aacf0d
+        *pr = sig->r;
aacf0d
+    if (ps != NULL)
aacf0d
+        *ps = sig->s;
aacf0d
+}
aacf0d
+
aacf0d
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
aacf0d
+{
aacf0d
+    if (r == NULL || s == NULL)
aacf0d
+        return 0;
aacf0d
+    BN_clear_free(sig->r);
aacf0d
+    BN_clear_free(sig->s);
aacf0d
+    sig->r = r;
aacf0d
+    sig->s = s;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DH_get0_pqg(const DH *dh,
aacf0d
+                 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
aacf0d
+{
aacf0d
+    if (p != NULL)
aacf0d
+        *p = dh->p;
aacf0d
+    if (q != NULL)
aacf0d
+        *q = dh->q;
aacf0d
+    if (g != NULL)
aacf0d
+        *g = dh->g;
aacf0d
+}
aacf0d
+
aacf0d
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
aacf0d
+{
aacf0d
+    /* If the fields p and g in d are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.  q may remain NULL.
aacf0d
+     */
aacf0d
+    if ((dh->p == NULL && p == NULL)
aacf0d
+        || (dh->g == NULL && g == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (p != NULL) {
aacf0d
+        BN_free(dh->p);
aacf0d
+        dh->p = p;
aacf0d
+    }
aacf0d
+    if (q != NULL) {
aacf0d
+        BN_free(dh->q);
aacf0d
+        dh->q = q;
aacf0d
+    }
aacf0d
+    if (g != NULL) {
aacf0d
+        BN_free(dh->g);
aacf0d
+        dh->g = g;
aacf0d
+    }
aacf0d
+
aacf0d
+    if (q != NULL) {
aacf0d
+        dh->length = BN_num_bits(q);
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
aacf0d
+{
aacf0d
+    if (pub_key != NULL)
aacf0d
+        *pub_key = dh->pub_key;
aacf0d
+    if (priv_key != NULL)
aacf0d
+        *priv_key = dh->priv_key;
aacf0d
+}
aacf0d
+
aacf0d
+int DH_set_length(DH *dh, long length)
aacf0d
+{
aacf0d
+    dh->length = length;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
aacf0d
+{
aacf0d
+    return ctx->iv;
aacf0d
+}
aacf0d
+
aacf0d
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
aacf0d
+{
aacf0d
+    return ctx->iv;
aacf0d
+}
aacf0d
+
aacf0d
+EVP_MD_CTX *EVP_MD_CTX_new(void)
aacf0d
+{
aacf0d
+    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
aacf0d
+}
aacf0d
+
aacf0d
+static void OPENSSL_clear_free(void *str, size_t num)
aacf0d
+{
aacf0d
+    if (str == NULL)
aacf0d
+        return;
aacf0d
+    if (num)
aacf0d
+        OPENSSL_cleanse(str, num);
aacf0d
+    OPENSSL_free(str);
aacf0d
+}
aacf0d
+
aacf0d
+/* This call frees resources associated with the context */
aacf0d
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
aacf0d
+{
aacf0d
+    if (ctx == NULL)
aacf0d
+        return 1;
aacf0d
+
aacf0d
+    /*
aacf0d
+     * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
aacf0d
+     * sometimes only copies of the context are ever finalised.
aacf0d
+     */
aacf0d
+    if (ctx->digest && ctx->digest->cleanup
aacf0d
+        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
aacf0d
+        ctx->digest->cleanup(ctx);
aacf0d
+    if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
aacf0d
+        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
aacf0d
+        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
aacf0d
+    }
aacf0d
+    EVP_PKEY_CTX_free(ctx->pctx);
aacf0d
+#ifndef OPENSSL_NO_ENGINE
aacf0d
+    ENGINE_finish(ctx->engine);
aacf0d
+#endif
aacf0d
+    OPENSSL_cleanse(ctx, sizeof(*ctx));
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
aacf0d
+{
aacf0d
+    EVP_MD_CTX_reset(ctx);
aacf0d
+    OPENSSL_free(ctx);
aacf0d
+}
aacf0d
+
aacf0d
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
aacf0d
+{
aacf0d
+    RSA_METHOD *ret;
aacf0d
+
aacf0d
+    ret = OPENSSL_malloc(sizeof(RSA_METHOD));
aacf0d
+
aacf0d
+    if (ret != NULL) {
aacf0d
+        memcpy(ret, meth, sizeof(*meth));
aacf0d
+        ret->name = OPENSSL_strdup(meth->name);
aacf0d
+        if (ret->name == NULL) {
aacf0d
+            OPENSSL_free(ret);
aacf0d
+            return NULL;
aacf0d
+        }
aacf0d
+    }
aacf0d
+
aacf0d
+    return ret;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
aacf0d
+{
aacf0d
+    char *tmpname;
aacf0d
+
aacf0d
+    tmpname = OPENSSL_strdup(name);
aacf0d
+    if (tmpname == NULL) {
aacf0d
+        return 0;
aacf0d
+    }
aacf0d
+
aacf0d
+    OPENSSL_free((char *)meth->name);
aacf0d
+    meth->name = tmpname;
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set_priv_enc(RSA_METHOD *meth,
aacf0d
+                          int (*priv_enc) (int flen, const unsigned char *from,
aacf0d
+                                           unsigned char *to, RSA *rsa,
aacf0d
+                                           int padding))
aacf0d
+{
aacf0d
+    meth->rsa_priv_enc = priv_enc;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set_priv_dec(RSA_METHOD *meth,
aacf0d
+                          int (*priv_dec) (int flen, const unsigned char *from,
aacf0d
+                                           unsigned char *to, RSA *rsa,
aacf0d
+                                           int padding))
aacf0d
+{
aacf0d
+    meth->rsa_priv_dec = priv_dec;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
aacf0d
+{
aacf0d
+    meth->finish = finish;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_meth_free(RSA_METHOD *meth)
aacf0d
+{
aacf0d
+    if (meth != NULL) {
aacf0d
+        OPENSSL_free((char *)meth->name);
aacf0d
+        OPENSSL_free(meth);
aacf0d
+    }
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_bits(const RSA *r)
aacf0d
+{
aacf0d
+    return (BN_num_bits(r->n));
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_bits(const DSA *dsa)
aacf0d
+{
aacf0d
+    return BN_num_bits(dsa->p);
aacf0d
+}
aacf0d
+
aacf0d
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
aacf0d
+{
aacf0d
+    if (pkey->type != EVP_PKEY_RSA) {
aacf0d
+        return NULL;
aacf0d
+    }
aacf0d
+    return pkey->pkey.rsa;
aacf0d
+}
aacf0d
+
aacf0d
+#endif /* OPENSSL_VERSION_NUMBER */
5b55d0
diff -up openssh/libcrypto-compat.h.openssl openssh/libcrypto-compat.h
5b55d0
--- openssh/libcrypto-compat.h.openssl	2017-09-26 13:19:31.798249703 +0200
5b55d0
+++ openssh/libcrypto-compat.h	2017-09-26 13:19:31.798249703 +0200
e0e7ed
@@ -0,0 +1,59 @@
aacf0d
+#ifndef LIBCRYPTO_COMPAT_H
aacf0d
+#define LIBCRYPTO_COMPAT_H
aacf0d
+
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
+
aacf0d
+#include <openssl/rsa.h>
aacf0d
+#include <openssl/dsa.h>
aacf0d
+#include <openssl/ecdsa.h>
aacf0d
+#include <openssl/dh.h>
aacf0d
+#include <openssl/evp.h>
aacf0d
+
aacf0d
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
aacf0d
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
aacf0d
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
aacf0d
+void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
aacf0d
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
aacf0d
+void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp);
aacf0d
+
aacf0d
+void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
aacf0d
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
aacf0d
+void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key);
aacf0d
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
aacf0d
+
aacf0d
+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
aacf0d
+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
aacf0d
+
aacf0d
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
aacf0d
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
aacf0d
+
aacf0d
+void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
aacf0d
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
aacf0d
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
aacf0d
+int DH_set_length(DH *dh, long length);
aacf0d
+
aacf0d
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx);
aacf0d
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx);
aacf0d
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
aacf0d
+EVP_MD_CTX *EVP_MD_CTX_new(void);
aacf0d
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
aacf0d
+#define EVP_CIPHER_impl_ctx_size(e) e->ctx_size
aacf0d
+#define EVP_CIPHER_CTX_get_cipher_data(ctx) ctx->cipher_data
aacf0d
+
aacf0d
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
aacf0d
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
aacf0d
+#define RSA_meth_get_finish(meth) meth->finish
aacf0d
+int RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
aacf0d
+int RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
aacf0d
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa));
aacf0d
+void RSA_meth_free(RSA_METHOD *meth);
aacf0d
+
aacf0d
+int RSA_bits(const RSA *r);
aacf0d
+int DSA_bits(const DSA *d);
aacf0d
+
aacf0d
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
aacf0d
+
aacf0d
+#endif /* OPENSSL_VERSION_NUMBER */
aacf0d
+
aacf0d
+#endif /* LIBCRYPTO_COMPAT_H */
aacf0d
+
5b55d0
diff -up openssh/Makefile.in.openssl openssh/Makefile.in
5b55d0
--- openssh/Makefile.in.openssl	2017-09-26 13:19:31.784249617 +0200
5b55d0
+++ openssh/Makefile.in	2017-09-26 13:19:31.798249703 +0200
5b55d0
@@ -101,7 +101,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
aacf0d
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
aacf0d
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
5b55d0
 	platform-pledge.o platform-tracing.o platform-misc.o \
5b55d0
-	auditstub.o
5b55d0
+	auditstub.o libcrypto-compat.o
aacf0d
 
aacf0d
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
5b55d0
 	sshconnect.o sshconnect2.o mux.o
5b55d0
diff -up openssh/monitor.c.openssl openssh/monitor.c
5b55d0
--- openssh/monitor.c.openssl	2017-09-26 13:19:31.789249648 +0200
5b55d0
+++ openssh/monitor.c	2017-09-26 13:19:31.799249709 +0200
5b55d0
@@ -631,9 +631,12 @@ mm_answer_moduli(int sock, Buffer *m)
aacf0d
 		return (0);
aacf0d
 	} else {
aacf0d
 		/* Send first bignum */
aacf0d
+		const BIGNUM *p, *g;
aacf0d
+
aacf0d
+		DH_get0_pqg(dh, &p, NULL, &g);
bbf61d
 		if ((r = sshbuf_put_u8(m, 1)) != 0 ||
bbf61d
-		    (r = sshbuf_put_bignum2(m, dh->p)) != 0 ||
bbf61d
-		    (r = sshbuf_put_bignum2(m, dh->g)) != 0)
bbf61d
+		    (r = sshbuf_put_bignum2(m, p)) != 0 ||
bbf61d
+		    (r = sshbuf_put_bignum2(m, g)) != 0)
bbf61d
			fatal("%s: buffer error: %s", __func__, ssh_err(r));
aacf0d
 
aacf0d
 		DH_free(dh);
5b55d0
diff -up openssh/openbsd-compat/openssl-compat.c.openssl openssh/openbsd-compat/openssl-compat.c
5b55d0
--- openssh/openbsd-compat/openssl-compat.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/openbsd-compat/openssl-compat.c	2017-09-26 13:19:31.799249709 +0200
6cf9b8
@@ -70,12 +70,19 @@ ssh_compatible_openssl(long headerver, l
aacf0d
 void
aacf0d
 ssh_OpenSSL_add_all_algorithms(void)
aacf0d
 {
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
 	OpenSSL_add_all_algorithms();
aacf0d
 
aacf0d
 	/* Enable use of crypto hardware */
aacf0d
 	ENGINE_load_builtin_engines();
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10001000L
aacf0d
 	ENGINE_register_all_complete();
aacf0d
+#endif
aacf0d
 	OPENSSL_config(NULL);
aacf0d
+#else
aacf0d
+	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS |
aacf0d
+	    OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, NULL);
aacf0d
+#endif
aacf0d
 }
aacf0d
 #endif
aacf0d
 
5b55d0
diff -up openssh/pam_ssh_agent_auth-0.10.3/configure.ac.openssl openssh/pam_ssh_agent_auth-0.10.3/configure.ac
5b55d0
--- openssh/pam_ssh_agent_auth-0.10.3/configure.ac.openssl	2016-11-13 04:24:32.000000000 +0100
5b55d0
+++ openssh/pam_ssh_agent_auth-0.10.3/configure.ac	2017-09-26 13:19:31.799249709 +0200
aacf0d
@@ -1829,6 +1829,7 @@ AC_RUN_IFELSE(
aacf0d
 	[AC_LANG_SOURCE([[
aacf0d
 #include <string.h>
aacf0d
 #include <openssl/opensslv.h>
aacf0d
+#include <openssl/crypto.h>
aacf0d
 int main(void) { exit(SSLeay() == OPENSSL_VERSION_NUMBER ? 0 : 1); }
aacf0d
 	]])],
aacf0d
 	[
5b55d0
diff -up openssh/regress/unittests/sshkey/test_file.c.openssl openssh/regress/unittests/sshkey/test_file.c
5b55d0
--- openssh/regress/unittests/sshkey/test_file.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/regress/unittests/sshkey/test_file.c	2017-09-26 13:19:31.799249709 +0200
aacf0d
@@ -46,6 +46,7 @@ sshkey_file_tests(void)
aacf0d
 	struct sshbuf *buf, *pw;
aacf0d
 	BIGNUM *a, *b, *c;
aacf0d
 	char *cp;
aacf0d
+	const BIGNUM *n, *p, *q, *g, *pub_key, *priv_key;
aacf0d
 
aacf0d
 	TEST_START("load passphrase");
aacf0d
 	pw = load_text_file("pw");
5b55d0
@@ -60,9 +61,11 @@ sshkey_file_tests(void)
aacf0d
 	a = load_bignum("rsa_1.param.n");
aacf0d
 	b = load_bignum("rsa_1.param.p");
aacf0d
 	c = load_bignum("rsa_1.param.q");
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->n, a);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->p, b);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->q, c);
aacf0d
+	RSA_get0_key(k1->rsa, &n, NULL, NULL);
aacf0d
+	RSA_get0_factors(k1->rsa, &p, &q);
aacf0d
+	ASSERT_BIGNUM_EQ(n, a);
aacf0d
+	ASSERT_BIGNUM_EQ(p, b);
aacf0d
+	ASSERT_BIGNUM_EQ(q, c);
aacf0d
 	BN_free(a);
aacf0d
 	BN_free(b);
aacf0d
 	BN_free(c);
5b55d0
@@ -151,9 +154,11 @@ sshkey_file_tests(void)
aacf0d
 	a = load_bignum("dsa_1.param.g");
aacf0d
 	b = load_bignum("dsa_1.param.priv");
aacf0d
 	c = load_bignum("dsa_1.param.pub");
aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->g, a);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
aacf0d
+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
aacf0d
+	DSA_get0_key(k1->dsa, &pub_key, &priv_key);
aacf0d
+	ASSERT_BIGNUM_EQ(g, a);
aacf0d
+	ASSERT_BIGNUM_EQ(priv_key, b);
aacf0d
+	ASSERT_BIGNUM_EQ(pub_key, c);
aacf0d
 	BN_free(a);
aacf0d
 	BN_free(b);
aacf0d
 	BN_free(c);
5b55d0
diff -up openssh/regress/unittests/sshkey/test_sshkey.c.openssl openssh/regress/unittests/sshkey/test_sshkey.c
5b55d0
--- openssh/regress/unittests/sshkey/test_sshkey.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/regress/unittests/sshkey/test_sshkey.c	2017-09-26 13:19:31.800249715 +0200
aacf0d
@@ -197,9 +197,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new(KEY_RSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
 
5b55d0
@@ -207,8 +204,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new(KEY_DSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
 
5b55d0
@@ -234,9 +229,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new_private(KEY_RSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->p, NULL);
aacf0d
 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
5b55d0
@@ -245,8 +237,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new_private(KEY_DSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
aacf0d
 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
5b55d0
@@ -285,18 +275,13 @@ sshkey_tests(void)
aacf0d
 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
aacf0d
 	ASSERT_PTR_NE(kr, NULL);
aacf0d
 	ASSERT_PTR_NE(kr->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(kr->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(kr->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_NE(kr->rsa->p, NULL);
aacf0d
-	ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
aacf0d
+	ASSERT_INT_EQ(RSA_bits(kr->rsa), 1024);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 	TEST_START("generate KEY_DSA");
aacf0d
 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
aacf0d
 	ASSERT_PTR_NE(kd, NULL);
aacf0d
 	ASSERT_PTR_NE(kd->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(kd->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 #ifdef OPENSSL_HAS_ECC
5b55d0
@@ -323,9 +308,6 @@ sshkey_tests(void)
aacf0d
 	ASSERT_PTR_NE(kr, k1);
aacf0d
 	ASSERT_INT_EQ(k1->type, KEY_RSA);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 	TEST_START("equal KEY_RSA/demoted KEY_RSA");
5b55d0
@@ -339,8 +321,6 @@ sshkey_tests(void)
aacf0d
 	ASSERT_PTR_NE(kd, k1);
aacf0d
 	ASSERT_INT_EQ(k1->type, KEY_DSA);
aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 	TEST_START("equal KEY_DSA/demoted KEY_DSA");
5b55d0
diff -up openssh/ssh.c.openssl openssh/ssh.c
5b55d0
--- openssh/ssh.c.openssl	2017-09-26 13:19:31.786249629 +0200
5b55d0
+++ openssh/ssh.c	2017-09-26 13:19:31.800249715 +0200
5b55d0
@@ -530,7 +530,9 @@ main(int ac, char **av)
aacf0d
 	sanitise_stdfd();
aacf0d
 
aacf0d
 	__progname = ssh_get_progname(av[0]);
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
         SSLeay_add_all_algorithms();
aacf0d
+#endif
aacf0d
 	if (access("/etc/system-fips", F_OK) == 0)
aacf0d
 		if (! FIPSCHECK_verify(NULL, NULL)){
aacf0d
 			if (FIPS_mode())
5b55d0
diff -up openssh/sshd.c.openssl openssh/sshd.c
5b55d0
--- openssh/sshd.c.openssl	2017-09-26 13:19:31.792249666 +0200
5b55d0
+++ openssh/sshd.c	2017-09-26 13:19:31.801249721 +0200
5b55d0
@@ -1485,7 +1485,7 @@ main(int ac, char **av)
aacf0d
 #endif
aacf0d
 	__progname = ssh_get_progname(av[0]);
aacf0d
 
aacf0d
-        SSLeay_add_all_algorithms();
aacf0d
+	OpenSSL_add_all_algorithms();
aacf0d
 	if (access("/etc/system-fips", F_OK) == 0)
aacf0d
 		if (! FIPSCHECK_verify(NULL, NULL)) {
aacf0d
 			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
5b55d0
diff -up openssh/ssh-dss.c.openssl openssh/ssh-dss.c
5b55d0
--- openssh/ssh-dss.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/ssh-dss.c	2017-09-26 13:19:31.801249721 +0200
aacf0d
@@ -55,6 +55,7 @@ ssh_dss_sign(const struct sshkey *key, u
aacf0d
 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
aacf0d
 	struct sshbuf *b = NULL;
aacf0d
 	int ret = SSH_ERR_INVALID_ARGUMENT;
aacf0d
+	const BIGNUM *r, *s;
aacf0d
 
aacf0d
 	if (lenp != NULL)
aacf0d
 		*lenp = 0;
aacf0d
@@ -76,15 +77,16 @@ ssh_dss_sign(const struct sshkey *key, u
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 
aacf0d
-	rlen = BN_num_bytes(sig->r);
aacf0d
-	slen = BN_num_bytes(sig->s);
aacf0d
+	DSA_SIG_get0(sig, &r, &s);
aacf0d
+	rlen = BN_num_bytes(r);
aacf0d
+	slen = BN_num_bytes(s);
aacf0d
 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
aacf0d
 		ret = SSH_ERR_INTERNAL_ERROR;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 	explicit_bzero(sigblob, SIGBLOB_LEN);
aacf0d
-	BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
aacf0d
-	BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
aacf0d
+	BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
aacf0d
+	BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
aacf0d
 
3cd489
 	if ((b = sshbuf_new()) == NULL) {
3cd489
 		ret = SSH_ERR_ALLOC_FAIL;
aacf0d
@@ -137,6 +139,7 @@ ssh_dss_verify(const struct sshkey *key,
aacf0d
 	int ret = SSH_ERR_INTERNAL_ERROR;
aacf0d
 	struct sshbuf *b = NULL;
aacf0d
 	char *ktype = NULL;
aacf0d
+	BIGNUM *r = NULL, *s = NULL;
aacf0d
 
aacf0d
 	if (key == NULL || key->dsa == NULL ||
aacf0d
 	    sshkey_type_plain(key->type) != KEY_DSA ||
aacf0d
@@ -177,16 +180,19 @@ ssh_dss_verify(const struct sshkey *key,
aacf0d
 
aacf0d
 	/* parse signature */
aacf0d
 	if ((sig = DSA_SIG_new()) == NULL ||
aacf0d
-	    (sig->r = BN_new()) == NULL ||
aacf0d
-	    (sig->s = BN_new()) == NULL) {
aacf0d
+	    (r = BN_new()) == NULL ||
aacf0d
+	    (s = BN_new()) == NULL) {
aacf0d
 		ret = SSH_ERR_ALLOC_FAIL;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
-	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
aacf0d
-	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
aacf0d
+	if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
aacf0d
+	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL) ||
aacf0d
+	    (DSA_SIG_set0(sig, r, s) == 0)) {
aacf0d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
+	r = NULL;
aacf0d
+	s = NULL;
aacf0d
 
aacf0d
 	/* sha1 the data */
aacf0d
 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
3cd489
@@ -207,7 +213,9 @@ ssh_dss_verify(const struct sshkey *key,
aacf0d
 
aacf0d
  out:
aacf0d
 	explicit_bzero(digest, sizeof(digest));
aacf0d
+	BN_free(r);
aacf0d
+	BN_free(s);
3cd489
 	DSA_SIG_free(sig);
aacf0d
 	sshbuf_free(b);
aacf0d
 	free(ktype);
aacf0d
 	if (sigblob != NULL) {
5b55d0
diff -up openssh/ssh-ecdsa.c.openssl openssh/ssh-ecdsa.c
5b55d0
--- openssh/ssh-ecdsa.c.openssl	2017-09-19 06:26:43.000000000 +0200
5b55d0
+++ openssh/ssh-ecdsa.c	2017-09-26 13:19:31.801249721 +0200
aacf0d
@@ -54,6 +54,7 @@ ssh_ecdsa_sign(const struct sshkey *key,
aacf0d
 	size_t len, dlen;
aacf0d
 	struct sshbuf *b = NULL, *bb = NULL;
aacf0d
 	int ret = SSH_ERR_INTERNAL_ERROR;
aacf0d
+	const BIGNUM *r, *s;
aacf0d
 
aacf0d
 	if (lenp != NULL)
aacf0d
 		*lenp = 0;
aacf0d
@@ -80,8 +81,9 @@ ssh_ecdsa_sign(const struct sshkey *key,
aacf0d
 		ret = SSH_ERR_ALLOC_FAIL;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
-	if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
aacf0d
-	    (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
aacf0d
+	ECDSA_SIG_get0(sig, &r, &s);
aacf0d
+	if ((ret = sshbuf_put_bignum2(bb, r)) != 0 ||
aacf0d
+	    (ret = sshbuf_put_bignum2(bb, s)) != 0)
aacf0d
 		goto out;
aacf0d
 	if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
aacf0d
 	    (ret = sshbuf_put_stringb(b, bb)) != 0)
aacf0d
@@ -119,6 +121,7 @@ ssh_ecdsa_verify(const struct sshkey *ke
aacf0d
 	int ret = SSH_ERR_INTERNAL_ERROR;
aacf0d
 	struct sshbuf *b = NULL, *sigbuf = NULL;
aacf0d
 	char *ktype = NULL;
aacf0d
+	BIGNUM *r = NULL, *s = NULL;
aacf0d
 
aacf0d
 	if (key == NULL || key->ecdsa == NULL ||
aacf0d
 	    sshkey_type_plain(key->type) != KEY_ECDSA ||
aacf0d
@@ -147,15 +150,23 @@ ssh_ecdsa_verify(const struct sshkey *ke
aacf0d
 	}
aacf0d
 
aacf0d
 	/* parse signature */
aacf0d
-	if ((sig = ECDSA_SIG_new()) == NULL) {
aacf0d
+	if ((sig = ECDSA_SIG_new()) == NULL ||
aacf0d
+	    (r = BN_new()) == NULL ||
aacf0d
+	    (s = BN_new()) == NULL) {
aacf0d
 		ret = SSH_ERR_ALLOC_FAIL;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
-	if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
aacf0d
-	    sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
aacf0d
+	if (sshbuf_get_bignum2(sigbuf, r) != 0 ||
aacf0d
+	    sshbuf_get_bignum2(sigbuf, s) != 0) {
aacf0d
 		ret = SSH_ERR_INVALID_FORMAT;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
+	if (ECDSA_SIG_set0(sig, r, s) == 0) {
aacf0d
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
+		goto out;
aacf0d
+	}
aacf0d
+	r = NULL;
aacf0d
+	s = NULL;
aacf0d
 	if (sshbuf_len(sigbuf) != 0) {
aacf0d
 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
aacf0d
 		goto out;
3cd489
@@ -180,7 +191,9 @@ ssh_ecdsa_verify(const struct sshkey *ke
aacf0d
 	explicit_bzero(digest, sizeof(digest));
aacf0d
 	sshbuf_free(sigbuf);
aacf0d
 	sshbuf_free(b);
aacf0d
+	BN_free(r);
aacf0d
+	BN_free(s);
3cd489
	ECDSA_SIG_free(sig);
aacf0d
 	free(ktype);
aacf0d
 	return ret;
aacf0d
 }
5b55d0
diff -up openssh/sshkey.c.openssl openssh/sshkey.c
5b55d0
--- openssh/sshkey.c.openssl	2017-09-26 13:19:31.786249629 +0200
5b55d0
+++ openssh/sshkey.c	2017-09-26 13:19:31.802249728 +0200
5b55d0
@@ -267,10 +267,10 @@ sshkey_size(const struct sshkey *k)
5b55d0
 #ifdef WITH_OPENSSL
aacf0d
 	case KEY_RSA:
aacf0d
 	case KEY_RSA_CERT:
aacf0d
-		return BN_num_bits(k->rsa->n);
aacf0d
+		return RSA_bits(k->rsa);
aacf0d
 	case KEY_DSA:
aacf0d
 	case KEY_DSA_CERT:
aacf0d
-		return BN_num_bits(k->dsa->p);
aacf0d
+		return DSA_bits(k->dsa);
aacf0d
 	case KEY_ECDSA:
aacf0d
 	case KEY_ECDSA_CERT:
aacf0d
 		return sshkey_curve_nid_to_bits(k->ecdsa_nid);
5b55d0
@@ -302,11 +302,17 @@ sshkey_is_private(const struct sshkey *k
5b55d0
       switch (k->type) {
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
       case KEY_RSA_CERT:
aacf0d
-      case KEY_RSA:
aacf0d
-              return k->rsa->d != NULL;
aacf0d
+      case KEY_RSA: {
aacf0d
+              const BIGNUM *d;
aacf0d
+              RSA_get0_key(k->rsa, NULL, NULL, &d);
aacf0d
+              return d != NULL;
aacf0d
+          }
aacf0d
       case KEY_DSA_CERT:
aacf0d
-      case KEY_DSA:
aacf0d
-              return k->dsa->priv_key != NULL;
aacf0d
+      case KEY_DSA: {
aacf0d
+              const BIGNUM *priv_key;
aacf0d
+              DSA_get0_key(k->dsa, NULL, &priv_key);
aacf0d
+              return priv_key != NULL;
aacf0d
+          }
aacf0d
 #ifdef OPENSSL_HAS_ECC
aacf0d
       case KEY_ECDSA_CERT:
aacf0d
       case KEY_ECDSA:
3cd489
@@ -496,10 +501,7 @@ sshkey_new(int type)
5b55d0
 #ifdef WITH_OPENSSL
aacf0d
 	case KEY_RSA:
aacf0d
 	case KEY_RSA_CERT:
aacf0d
-		if ((rsa = RSA_new()) == NULL ||
aacf0d
-		    (rsa->n = BN_new()) == NULL ||
aacf0d
-		    (rsa->e = BN_new()) == NULL) {
3cd489
-			RSA_free(rsa);
aacf0d
+		if ((rsa = RSA_new()) == NULL) {
aacf0d
 			free(k);
aacf0d
 			return NULL;
aacf0d
 		}
3cd489
@@ -508,12 +509,7 @@ sshkey_new(int type)
aacf0d
 		break;
aacf0d
 	case KEY_DSA:
aacf0d
 	case KEY_DSA_CERT:
aacf0d
-		if ((dsa = DSA_new()) == NULL ||
aacf0d
-		    (dsa->p = BN_new()) == NULL ||
aacf0d
-		    (dsa->q = BN_new()) == NULL ||
aacf0d
-		    (dsa->g = BN_new()) == NULL ||
aacf0d
-		    (dsa->pub_key = BN_new()) == NULL) {
3cd489
-			DSA_free(dsa);
aacf0d
+		if ((dsa = DSA_new()) == NULL) {
aacf0d
 			free(k);
aacf0d
 			return NULL;
aacf0d
 		}
5b55d0
@@ -553,21 +548,10 @@ sshkey_add_private(struct sshkey *k)
5b55d0
 #ifdef WITH_OPENSSL
aacf0d
 	case KEY_RSA:
aacf0d
 	case KEY_RSA_CERT:
aacf0d
-#define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
aacf0d
-		if (bn_maybe_alloc_failed(k->rsa->d) ||
aacf0d
-		    bn_maybe_alloc_failed(k->rsa->iqmp) ||
aacf0d
-		    bn_maybe_alloc_failed(k->rsa->q) ||
aacf0d
-		    bn_maybe_alloc_failed(k->rsa->p) ||
aacf0d
-		    bn_maybe_alloc_failed(k->rsa->dmq1) ||
aacf0d
-		    bn_maybe_alloc_failed(k->rsa->dmp1))
aacf0d
-			return SSH_ERR_ALLOC_FAIL;
aacf0d
 		break;
aacf0d
 	case KEY_DSA:
aacf0d
 	case KEY_DSA_CERT:
aacf0d
-		if (bn_maybe_alloc_failed(k->dsa->priv_key))
aacf0d
-			return SSH_ERR_ALLOC_FAIL;
aacf0d
 		break;
aacf0d
-#undef bn_maybe_alloc_failed
aacf0d
 	case KEY_ECDSA:
aacf0d
 	case KEY_ECDSA_CERT:
aacf0d
 		/* Cannot do anything until we know the group */
5b55d0
@@ -684,17 +668,31 @@ sshkey_equal_public(const struct sshkey
5b55d0
 	switch (a->type) {
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
 	case KEY_RSA_CERT:
aacf0d
-	case KEY_RSA:
aacf0d
-		return a->rsa != NULL && b->rsa != NULL &&
aacf0d
-		    BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
aacf0d
-		    BN_cmp(a->rsa->n, b->rsa->n) == 0;
aacf0d
+	case KEY_RSA: {
aacf0d
+			const BIGNUM *a_e, *a_n, *b_e, *b_n;
aacf0d
+
aacf0d
+			if (a->rsa == NULL || b->rsa == NULL)
aacf0d
+				return 0;
aacf0d
+			RSA_get0_key(a->rsa, &a_n, &a_e, NULL);
aacf0d
+			RSA_get0_key(b->rsa, &b_n, &b_e, NULL);
aacf0d
+			return BN_cmp(a_e, b_e) == 0 && BN_cmp(a_n, b_n) == 0;
aacf0d
+		}
aacf0d
 	case KEY_DSA_CERT:
aacf0d
-	case KEY_DSA:
aacf0d
-		return a->dsa != NULL && b->dsa != NULL &&
aacf0d
-		    BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
aacf0d
-		    BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
aacf0d
-		    BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
aacf0d
-		    BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
aacf0d
+	case KEY_DSA: {
aacf0d
+			const BIGNUM *a_p, *a_q, *a_g, *a_pub_key;
aacf0d
+			const BIGNUM *b_p, *b_q, *b_g, *b_pub_key;
aacf0d
+
aacf0d
+			if (a->dsa == NULL || b->dsa == NULL)
aacf0d
+				return 0;
aacf0d
+			DSA_get0_pqg(a->dsa, &a_p, &a_q, &a_g);
aacf0d
+			DSA_get0_key(a->dsa, &a_pub_key, NULL);
aacf0d
+			DSA_get0_pqg(b->dsa, &b_p, &b_q, &b_g);
aacf0d
+			DSA_get0_key(b->dsa, &b_pub_key, NULL);
aacf0d
+			return BN_cmp(a_p, b_p) == 0 &&
aacf0d
+			    BN_cmp(a_q, b_q) == 0 &&
aacf0d
+			    BN_cmp(a_g, b_g) == 0 &&
aacf0d
+			    BN_cmp(a_pub_key, b_pub_key) == 0;
aacf0d
+		}
aacf0d
 # ifdef OPENSSL_HAS_ECC
aacf0d
 	case KEY_ECDSA_CERT:
aacf0d
 	case KEY_ECDSA:
5b55d0
@@ -769,15 +767,21 @@ to_blob_buf(const struct sshkey *key, st
aacf0d
 			return ret;
aacf0d
 		break;
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
-	case KEY_DSA:
aacf0d
-		if (key->dsa == NULL)
aacf0d
-			return SSH_ERR_INVALID_ARGUMENT;
aacf0d
-		if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
aacf0d
-			return ret;
aacf0d
+	case KEY_DSA: {
aacf0d
+			const BIGNUM *p, *q, *g, *pub_key;
aacf0d
+
aacf0d
+			if (key->dsa == NULL)
aacf0d
+				return SSH_ERR_INVALID_ARGUMENT;
aacf0d
+
aacf0d
+			DSA_get0_pqg(key->dsa, &p, &q, &g);
aacf0d
+			DSA_get0_key(key->dsa, &pub_key, NULL);
aacf0d
+			if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(b, p)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(b, q)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(b, g)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(b, pub_key)) != 0)
aacf0d
+				return ret;
aacf0d
+		}
aacf0d
 		break;
aacf0d
 # ifdef OPENSSL_HAS_ECC
aacf0d
 	case KEY_ECDSA:
5b55d0
@@ -790,13 +794,18 @@ to_blob_buf(const struct sshkey *key, st
aacf0d
 			return ret;
aacf0d
 		break;
aacf0d
 # endif
aacf0d
-	case KEY_RSA:
aacf0d
-		if (key->rsa == NULL)
aacf0d
-			return SSH_ERR_INVALID_ARGUMENT;
aacf0d
-		if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
aacf0d
-			return ret;
aacf0d
+	case KEY_RSA: {
aacf0d
+			const BIGNUM *e, *n;
aacf0d
+
aacf0d
+			if (key->rsa == NULL)
aacf0d
+				return SSH_ERR_INVALID_ARGUMENT;
aacf0d
+
aacf0d
+			RSA_get0_key(key->rsa, &n, &e, NULL);
aacf0d
+			if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(b, e)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(b, n)) != 0)
aacf0d
+				return ret;
aacf0d
+		}
aacf0d
 		break;
aacf0d
 #endif /* WITH_OPENSSL */
aacf0d
 	case KEY_ED25519:
5b55d0
@@ -1672,15 +1681,32 @@ sshkey_from_private(const struct sshkey
aacf0d
 	switch (k->type) {
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
 	case KEY_DSA:
aacf0d
-	case KEY_DSA_CERT:
aacf0d
-		if ((n = sshkey_new(k->type)) == NULL)
aacf0d
-			return SSH_ERR_ALLOC_FAIL;
aacf0d
-		if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
aacf0d
-		    (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
aacf0d
-		    (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
aacf0d
-		    (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
aacf0d
-			sshkey_free(n);
aacf0d
-			return SSH_ERR_ALLOC_FAIL;
aacf0d
+	case KEY_DSA_CERT: {
aacf0d
+			const BIGNUM *k_p, *k_q, *k_g, *k_pub_key;
aacf0d
+			BIGNUM *n_p = NULL, *n_q = NULL, *n_g = NULL, *n_pub_key = NULL;
aacf0d
+
aacf0d
+			if ((n = sshkey_new(k->type)) == NULL)
aacf0d
+				return SSH_ERR_ALLOC_FAIL;
aacf0d
+
aacf0d
+			DSA_get0_pqg(k->dsa, &k_p, &k_q, &k_g);
aacf0d
+			DSA_get0_key(k->dsa, &k_pub_key, NULL);
aacf0d
+
aacf0d
+			if (((n_p = BN_dup(k_p)) == NULL) ||
aacf0d
+			    ((n_q = BN_dup(k_q)) == NULL) ||
aacf0d
+			    ((n_g = BN_dup(k_g)) == NULL) ||
aacf0d
+			    (DSA_set0_pqg(n->dsa, n_p, n_q, n_g) == 0)) {
aacf0d
+				sshkey_free(n);
aacf0d
+				BN_free(n_p);
aacf0d
+				BN_free(n_q);
aacf0d
+				BN_free(n_g);
aacf0d
+				return SSH_ERR_ALLOC_FAIL;
aacf0d
+			}
aacf0d
+			if (((n_pub_key = BN_dup(k_pub_key)) == NULL) ||
aacf0d
+			    (DSA_set0_key(n->dsa, n_pub_key, NULL) == 0)) {
aacf0d
+				sshkey_free(n);
aacf0d
+				BN_free(n_pub_key);
aacf0d
+				return SSH_ERR_ALLOC_FAIL;
aacf0d
+			}
aacf0d
 		}
aacf0d
 		break;
aacf0d
 # ifdef OPENSSL_HAS_ECC
5b55d0
@@ -1702,13 +1728,22 @@ sshkey_from_private(const struct sshkey
5b55d0
 		break;
aacf0d
 # endif /* OPENSSL_HAS_ECC */
aacf0d
 	case KEY_RSA:
aacf0d
-	case KEY_RSA_CERT:
aacf0d
-		if ((n = sshkey_new(k->type)) == NULL)
aacf0d
-			return SSH_ERR_ALLOC_FAIL;
aacf0d
-		if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
aacf0d
-		    (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
aacf0d
-			sshkey_free(n);
aacf0d
-			return SSH_ERR_ALLOC_FAIL;
aacf0d
+	case KEY_RSA_CERT: {
aacf0d
+			const BIGNUM *k_n, *k_e;
aacf0d
+			BIGNUM *n_n = NULL, *n_e = NULL;
aacf0d
+
aacf0d
+			if ((n = sshkey_new(k->type)) == NULL)
aacf0d
+				return SSH_ERR_ALLOC_FAIL;
aacf0d
+
aacf0d
+			RSA_get0_key(k->rsa, &k_n, &k_e, NULL);
aacf0d
+			if (((n_n = BN_dup(k_n)) == NULL) ||
aacf0d
+			    ((n_e = BN_dup(k_e)) == NULL) ||
aacf0d
+			    RSA_set0_key(n->rsa, n_n, n_e, NULL) == 0) {
aacf0d
+				sshkey_free(n);
aacf0d
+				BN_free(n_n);
aacf0d
+				BN_free(n_e);
aacf0d
+				return SSH_ERR_ALLOC_FAIL;
aacf0d
+			}
aacf0d
 		}
aacf0d
 		break;
aacf0d
 #endif /* WITH_OPENSSL */
5b55d0
@@ -1907,12 +1942,22 @@ sshkey_from_blob_internal(struct sshbuf
aacf0d
 			ret = SSH_ERR_ALLOC_FAIL;
aacf0d
 			goto out;
aacf0d
 		}
aacf0d
-		if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
aacf0d
-		    sshbuf_get_bignum2(b, key->rsa->n) != 0) {
aacf0d
-			ret = SSH_ERR_INVALID_FORMAT;
aacf0d
-			goto out;
aacf0d
+		{
aacf0d
+			BIGNUM *e, *n;
aacf0d
+
aacf0d
+			e = BN_new();
aacf0d
+			n = BN_new();
aacf0d
+			if (e == NULL || n == NULL ||
aacf0d
+			    sshbuf_get_bignum2(b, e) != 0 ||
aacf0d
+			    sshbuf_get_bignum2(b, n) != 0 ||
aacf0d
+			    RSA_set0_key(key->rsa, n, e, NULL) == 0) {
aacf0d
+				BN_free(e);
aacf0d
+				BN_free(n);
aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
aacf0d
+				goto out;
aacf0d
+			}
aacf0d
 		}
5b55d0
-		if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
5b55d0
+		if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
5b55d0
 			ret = SSH_ERR_KEY_LENGTH;
5b55d0
 			goto out;
5b55d0
 		}
5b55d0
@@ -1932,12 +1977,34 @@ sshkey_from_blob_internal(struct sshbuf
aacf0d
 			ret = SSH_ERR_ALLOC_FAIL;
aacf0d
 			goto out;
aacf0d
 		}
aacf0d
-		if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
aacf0d
-		    sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
aacf0d
-		    sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
aacf0d
-		    sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
aacf0d
-			ret = SSH_ERR_INVALID_FORMAT;
aacf0d
-			goto out;
aacf0d
+		{
aacf0d
+			BIGNUM *p, *q, *g, *pub_key;
aacf0d
+
aacf0d
+			p = BN_new();
aacf0d
+			q = BN_new();
aacf0d
+			g = BN_new();
aacf0d
+			pub_key = BN_new();
aacf0d
+
aacf0d
+			if (p == NULL || q == NULL || g == NULL ||
aacf0d
+			    pub_key == NULL ||
aacf0d
+			    sshbuf_get_bignum2(b, p) != 0 ||
aacf0d
+			    sshbuf_get_bignum2(b, q) != 0 ||
aacf0d
+			    sshbuf_get_bignum2(b, g) != 0 ||
aacf0d
+			    sshbuf_get_bignum2(b, pub_key) != 0 ||
aacf0d
+			    DSA_set0_pqg(key->dsa, p, q, g) == 0) {
aacf0d
+				BN_free(p);
aacf0d
+				BN_free(q);
aacf0d
+				BN_free(g);
aacf0d
+				BN_free(pub_key);
aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
aacf0d
+				goto out;
aacf0d
+			}
aacf0d
+
aacf0d
+			if (DSA_set0_key(key->dsa, pub_key, NULL) == 0) {
aacf0d
+				BN_free(pub_key);
aacf0d
+				ret = SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
+				goto out;
aacf0d
+			}
aacf0d
 		}
aacf0d
 #ifdef DEBUG_PK
aacf0d
 		DSA_print_fp(stderr, key->dsa, 8);
5b55d0
@@ -2171,26 +2238,53 @@ sshkey_demote(const struct sshkey *k, st
5b55d0
 		if ((ret = sshkey_cert_copy(k, pk)) != 0)
aacf0d
 			goto fail;
aacf0d
 		/* FALLTHROUGH */
aacf0d
-	case KEY_RSA:
aacf0d
-		if ((pk->rsa = RSA_new()) == NULL ||
aacf0d
-		    (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
aacf0d
-		    (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
aacf0d
-			ret = SSH_ERR_ALLOC_FAIL;
aacf0d
-			goto fail;
aacf0d
+	case KEY_RSA: {
aacf0d
+			const BIGNUM *k_e, *k_n;
aacf0d
+			BIGNUM *pk_e = NULL, *pk_n = NULL;
aacf0d
+
aacf0d
+			RSA_get0_key(k->rsa, &k_n, &k_e, NULL);
aacf0d
+			if ((pk->rsa = RSA_new()) == NULL ||
aacf0d
+			    (pk_e = BN_dup(k_e)) == NULL ||
aacf0d
+			    (pk_n = BN_dup(k_n)) == NULL ||
aacf0d
+			    RSA_set0_key(pk->rsa, pk_n, pk_e, NULL) == 0) {
aacf0d
+				BN_free(pk_e);
aacf0d
+				BN_free(pk_n);
aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
aacf0d
+				goto fail;
aacf0d
 			}
aacf0d
+		}
aacf0d
 		break;
aacf0d
 	case KEY_DSA_CERT:
aacf0d
 		if ((ret = sshkey_cert_copy(k, pk)) != 0)
aacf0d
 			goto fail;
aacf0d
 		/* FALLTHROUGH */
aacf0d
-	case KEY_DSA:
aacf0d
-		if ((pk->dsa = DSA_new()) == NULL ||
aacf0d
-		    (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
aacf0d
-		    (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
aacf0d
-		    (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
aacf0d
-		    (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
aacf0d
-			ret = SSH_ERR_ALLOC_FAIL;
aacf0d
-			goto fail;
aacf0d
+	case KEY_DSA: {
aacf0d
+			const BIGNUM *k_p, *k_q, *k_g, *k_pub_key;
aacf0d
+			BIGNUM *pk_p = NULL, *pk_q = NULL, *pk_g = NULL;
aacf0d
+			BIGNUM *pk_pub_key = NULL;
aacf0d
+
aacf0d
+			DSA_get0_pqg(k->dsa, &k_p, &k_q, &k_g);
aacf0d
+			DSA_get0_key(k->dsa, &k_pub_key, NULL);
aacf0d
+
aacf0d
+			if ((pk->dsa = DSA_new()) == NULL ||
aacf0d
+			    (pk_p = BN_dup(k_p)) == NULL ||
aacf0d
+			    (pk_q = BN_dup(k_q)) == NULL ||
aacf0d
+			    (pk_g = BN_dup(k_g)) == NULL ||
aacf0d
+			    (pk_pub_key = BN_dup(k_pub_key)) == NULL ||
aacf0d
+			    DSA_set0_pqg(pk->dsa, pk_p, pk_q, pk_g) == 0) {
aacf0d
+				BN_free(pk_p);
aacf0d
+				BN_free(pk_q);
aacf0d
+				BN_free(pk_g);
aacf0d
+				BN_free(pk_pub_key);
aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
aacf0d
+				goto fail;
aacf0d
+			}
aacf0d
+
aacf0d
+			if (DSA_set0_key(pk->dsa, pk_pub_key, NULL) == 0) {
aacf0d
+				BN_free(pk_pub_key);
aacf0d
+				ret = SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
+				goto fail;
aacf0d
+			}
aacf0d
 		}
aacf0d
 		break;
aacf0d
 	case KEY_ECDSA_CERT:
5b55d0
@@ -2312,12 +2406,17 @@ sshkey_certify_custom(struct sshkey *k,
aacf0d
 	/* XXX this substantially duplicates to_blob(); refactor */
aacf0d
 	switch (k->type) {
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
-	case KEY_DSA_CERT:
aacf0d
-		if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
aacf0d
-			goto out;
aacf0d
+	case KEY_DSA_CERT: {
aacf0d
+			const BIGNUM *p, *q, *g, *pub_key;
aacf0d
+
aacf0d
+			DSA_get0_pqg(k->dsa, &p, &q, &g);
aacf0d
+			DSA_get0_key(k->dsa, &pub_key, NULL);
aacf0d
+			if ((ret = sshbuf_put_bignum2(cert, p)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(cert, pub_key)) != 0)
aacf0d
+				goto out;
aacf0d
+		}
aacf0d
 		break;
aacf0d
 # ifdef OPENSSL_HAS_ECC
aacf0d
 	case KEY_ECDSA_CERT:
5b55d0
@@ -2329,10 +2428,15 @@ sshkey_certify_custom(struct sshkey *k,
aacf0d
 			goto out;
aacf0d
 		break;
aacf0d
 # endif /* OPENSSL_HAS_ECC */
aacf0d
-	case KEY_RSA_CERT:
aacf0d
-		if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
aacf0d
-			goto out;
aacf0d
+	case KEY_RSA_CERT: {
aacf0d
+			const BIGNUM *e, *n;
aacf0d
+
aacf0d
+			RSA_get0_key(k->rsa, &n, &e, NULL);
aacf0d
+			if (e == NULL || n == NULL ||
aacf0d
+			    (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
aacf0d
+			    (ret = sshbuf_put_bignum2(cert, n)) != 0)
aacf0d
+				goto out;
aacf0d
+		}
aacf0d
 		break;
aacf0d
 #endif /* WITH_OPENSSL */
aacf0d
 	case KEY_ED25519_CERT:
5b55d0
@@ -2505,43 +2609,65 @@ sshkey_private_serialize(const struct ss
aacf0d
 		goto out;
aacf0d
 	switch (key->type) {
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
-	case KEY_RSA:
aacf0d
-		if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
aacf0d
-			goto out;
aacf0d
+	case KEY_RSA: {
aacf0d
+			const BIGNUM *n, *e, *d, *iqmp, *p, *q;
aacf0d
+			RSA_get0_key(key->rsa, &n, &e, &d);
aacf0d
+			RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
aacf0d
+			RSA_get0_factors(key->rsa, &p, &q);
aacf0d
+			if ((r = sshbuf_put_bignum2(b, n)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, e)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, d)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, p)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, q)) != 0)
aacf0d
+				goto out;
aacf0d
+		}
aacf0d
 		break;
aacf0d
 	case KEY_RSA_CERT:
aacf0d
 		if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
aacf0d
 			r = SSH_ERR_INVALID_ARGUMENT;
aacf0d
 			goto out;
aacf0d
 		}
aacf0d
-		if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
aacf0d
-			goto out;
aacf0d
+		{
aacf0d
+			const BIGNUM *d, *iqmp, *p, *q;
aacf0d
+
aacf0d
+			RSA_get0_key(key->rsa, NULL, NULL, &d);
aacf0d
+			RSA_get0_factors(key->rsa, &p, &q);
aacf0d
+			RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
aacf0d
+			if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, d)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, p)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, q)) != 0)
aacf0d
+				goto out;
aacf0d
+		}
aacf0d
 		break;
aacf0d
-	case KEY_DSA:
aacf0d
-		if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
aacf0d
-			goto out;
aacf0d
+	case KEY_DSA: {
aacf0d
+			const BIGNUM *p, *q, *g, *pub_key, *priv_key;
aacf0d
+
aacf0d
+			DSA_get0_pqg(key->dsa, &p, &q, &g);
aacf0d
+			DSA_get0_key(key->dsa, &pub_key, &priv_key);
aacf0d
+			if ((r = sshbuf_put_bignum2(b, p)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, q)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, g)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, pub_key)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum2(b, priv_key)) != 0)
aacf0d
+				goto out;
aacf0d
+		}
aacf0d
 		break;
aacf0d
 	case KEY_DSA_CERT:
aacf0d
 		if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {