jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

Blame SOURCES/openssh-7.3p1-openssl-1.1.0.patch

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