6cf9b8
diff -up openssh-7.4p1/authfd.c.openssl openssh-7.4p1/authfd.c
6cf9b8
--- openssh-7.4p1/authfd.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/authfd.c	2016-12-23 17:47:36.429817751 +0100
aacf0d
@@ -207,15 +207,22 @@ deserialise_identity1(struct sshbuf *ids
aacf0d
 	int r, keybits;
aacf0d
 	u_int32_t bits;
aacf0d
 	char *comment = NULL;
aacf0d
+	BIGNUM *e = NULL, *n = NULL;
aacf0d
 
aacf0d
 	if ((key = sshkey_new(KEY_RSA1)) == NULL)
aacf0d
 		return SSH_ERR_ALLOC_FAIL;
aacf0d
-	if ((r = sshbuf_get_u32(ids, &bits)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(ids, key->rsa->e)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(ids, key->rsa->n)) != 0 ||
aacf0d
-	    (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
aacf0d
+	if ((e = BN_new()) == NULL ||
aacf0d
+	    (n = BN_new()) == NULL ||
aacf0d
+	    (r = sshbuf_get_u32(ids, &bits)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(ids, e)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(ids, n)) != 0 ||
aacf0d
+	    (RSA_set0_key(key->rsa, n, e, NULL) == 0) ||
aacf0d
+	    (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0) {
aacf0d
+		BN_free(n);
aacf0d
+		BN_free(e);
aacf0d
 		goto out;
aacf0d
-	keybits = BN_num_bits(key->rsa->n);
aacf0d
+	}
aacf0d
+	keybits = BN_num_bits(n);
aacf0d
 	/* XXX previously we just warned here. I think we should be strict */
aacf0d
 	if (keybits < 0 || bits != (u_int)keybits) {
aacf0d
 		r = SSH_ERR_KEY_BITS_MISMATCH;
aacf0d
@@ -393,15 +400,17 @@ ssh_decrypt_challenge(int sock, struct s
aacf0d
 	struct sshbuf *msg;
aacf0d
 	int r;
aacf0d
 	u_char type;
aacf0d
+	const BIGNUM *e, *n;
aacf0d
 
aacf0d
 	if (key->type != KEY_RSA1)
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	if ((msg = sshbuf_new()) == NULL)
aacf0d
 		return SSH_ERR_ALLOC_FAIL;
aacf0d
+	RSA_get0_key(key->rsa, &n, &e, NULL);
aacf0d
 	if ((r = sshbuf_put_u8(msg, SSH_AGENTC_RSA_CHALLENGE)) != 0 ||
aacf0d
-	    (r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0 ||
aacf0d
+	    (r = sshbuf_put_u32(msg, BN_num_bits(n))) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(msg, e)) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(msg, n)) != 0 ||
aacf0d
 	    (r = sshbuf_put_bignum1(msg, challenge)) != 0 ||
aacf0d
 	    (r = sshbuf_put(msg, session_id, 16)) != 0 ||
aacf0d
 	    (r = sshbuf_put_u32(msg, 1)) != 0) /* Response type for proto 1.1 */
aacf0d
@@ -499,15 +508,19 @@ static int
aacf0d
 ssh_encode_identity_rsa1(struct sshbuf *b, RSA *key, const char *comment)
aacf0d
 {
aacf0d
 	int r;
aacf0d
+	const BIGNUM *n, *e, *d, *q, *p, *iqmp;
aacf0d
 
aacf0d
+	RSA_get0_key(key, &n, &e, &d);
aacf0d
+	RSA_get0_factors(key, &p, &q);
aacf0d
+	RSA_get0_crt_params(key, NULL, NULL, &iqmp);
aacf0d
 	/* To keep within the protocol: p < q for ssh. in SSL p > q */
aacf0d
-	if ((r = sshbuf_put_u32(b, BN_num_bits(key->n))) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(b, key->n)) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(b, key->e)) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(b, key->d)) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(b, key->iqmp)) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(b, key->q)) != 0 ||
aacf0d
-	    (r = sshbuf_put_bignum1(b, key->p)) != 0 ||
aacf0d
+	if ((r = sshbuf_put_u32(b, BN_num_bits(n))) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(b, n)) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(b, e)) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(b, d)) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(b, iqmp)) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(b, q)) != 0 ||
aacf0d
+	    (r = sshbuf_put_bignum1(b, p)) != 0 ||
aacf0d
 	    (r = sshbuf_put_cstring(b, comment)) != 0)
aacf0d
 		return r;
aacf0d
 	return 0;
aacf0d
@@ -622,11 +635,13 @@ ssh_remove_identity(int sock, struct ssh
aacf0d
 
aacf0d
 #ifdef WITH_SSH1
aacf0d
 	if (key->type == KEY_RSA1) {
aacf0d
+		const BIGNUM *e, *n;
aacf0d
+		RSA_get0_key(key->rsa, &n, &e, NULL);
aacf0d
 		if ((r = sshbuf_put_u8(msg,
aacf0d
 		    SSH_AGENTC_REMOVE_RSA_IDENTITY)) != 0 ||
aacf0d
-		    (r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
aacf0d
-		    (r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0)
aacf0d
+		    (r = sshbuf_put_u32(msg, BN_num_bits(n))) != 0 ||
aacf0d
+		    (r = sshbuf_put_bignum1(msg, e)) != 0 ||
aacf0d
+		    (r = sshbuf_put_bignum1(msg, n)) != 0)
aacf0d
 			goto out;
aacf0d
 	} else
aacf0d
 #endif
6cf9b8
diff -up openssh-7.4p1/auth-pam.c.openssl openssh-7.4p1/auth-pam.c
6cf9b8
--- openssh-7.4p1/auth-pam.c.openssl	2016-12-23 17:47:36.400817739 +0100
6cf9b8
+++ openssh-7.4p1/auth-pam.c	2016-12-23 17:47:36.430817752 +0100
6cf9b8
@@ -129,6 +129,10 @@ extern u_int utmp_len;
aacf0d
 typedef pthread_t sp_pthread_t;
aacf0d
 #else
aacf0d
 typedef pid_t sp_pthread_t;
aacf0d
+# define pthread_create(a, b, c, d)	_ssh_compat_pthread_create(a, b, c, d)
aacf0d
+# define pthread_exit(a)		_ssh_compat_pthread_exit(a)
aacf0d
+# define pthread_cancel(a)		_ssh_compat_pthread_cancel(a)
aacf0d
+# define pthread_join(a, b)		_ssh_compat_pthread_join(a, b)
aacf0d
 #endif
aacf0d
 
aacf0d
 struct pam_ctxt {
6cf9b8
diff -up openssh-7.4p1/cipher-3des1.c.openssl openssh-7.4p1/cipher-3des1.c
6cf9b8
--- openssh-7.4p1/cipher-3des1.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/cipher-3des1.c	2016-12-23 17:47:36.430817752 +0100
6cf9b8
@@ -44,7 +44,7 @@
aacf0d
  */
aacf0d
 struct ssh1_3des_ctx
aacf0d
 {
aacf0d
-	EVP_CIPHER_CTX	k1, k2, k3;
aacf0d
+	EVP_CIPHER_CTX	*k1, *k2, *k3;
aacf0d
 };
aacf0d
 
aacf0d
 const EVP_CIPHER * evp_ssh1_3des(void);
6cf9b8
@@ -65,7 +65,7 @@ ssh1_3des_init(EVP_CIPHER_CTX *ctx, cons
aacf0d
 	if (key == NULL)
aacf0d
 		return 1;
aacf0d
 	if (enc == -1)
aacf0d
-		enc = ctx->encrypt;
aacf0d
+		enc = EVP_CIPHER_CTX_encrypting(ctx);
aacf0d
 	k1 = k2 = k3 = (u_char *) key;
aacf0d
 	k2 += 8;
aacf0d
 	if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
6cf9b8
@@ -74,12 +74,19 @@ ssh1_3des_init(EVP_CIPHER_CTX *ctx, cons
aacf0d
 		else
aacf0d
 			k1 += 16;
aacf0d
 	}
aacf0d
-	EVP_CIPHER_CTX_init(&c->k1);
aacf0d
-	EVP_CIPHER_CTX_init(&c->k2);
aacf0d
-	EVP_CIPHER_CTX_init(&c->k3);
aacf0d
-	if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
aacf0d
-	    EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
aacf0d
-	    EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
aacf0d
+	c->k1 = EVP_CIPHER_CTX_new();
aacf0d
+	c->k2 = EVP_CIPHER_CTX_new();
aacf0d
+	c->k3 = EVP_CIPHER_CTX_new();
aacf0d
+	if (c->k1 == NULL || c->k2 == NULL || c->k3 == NULL) {
aacf0d
+		EVP_CIPHER_CTX_free(c->k1);
aacf0d
+		EVP_CIPHER_CTX_free(c->k2);
aacf0d
+		EVP_CIPHER_CTX_free(c->k3);
aacf0d
+		free(c);
aacf0d
+		return 0;
aacf0d
+	}
aacf0d
+	if (EVP_CipherInit(c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
aacf0d
+	    EVP_CipherInit(c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
aacf0d
+	    EVP_CipherInit(c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
aacf0d
 		explicit_bzero(c, sizeof(*c));
aacf0d
 		free(c);
aacf0d
 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
6cf9b8
@@ -95,9 +102,9 @@ ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_cha
aacf0d
 
aacf0d
 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
aacf0d
 		return 0;
aacf0d
-	if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
aacf0d
-	    EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
aacf0d
-	    EVP_Cipher(&c->k3, dest, dest, len) == 0)
aacf0d
+	if (EVP_Cipher(c->k1, dest, (u_char *)src, len) == 0 ||
aacf0d
+	    EVP_Cipher(c->k2, dest, dest, len) == 0 ||
aacf0d
+	    EVP_Cipher(c->k3, dest, dest, len) == 0)
aacf0d
 		return 0;
aacf0d
 	return 1;
aacf0d
 }
6cf9b8
@@ -108,9 +115,9 @@ ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
aacf0d
 	struct ssh1_3des_ctx *c;
aacf0d
 
aacf0d
 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
aacf0d
-		EVP_CIPHER_CTX_cleanup(&c->k1);
aacf0d
-		EVP_CIPHER_CTX_cleanup(&c->k2);
aacf0d
-		EVP_CIPHER_CTX_cleanup(&c->k3);
aacf0d
+		EVP_CIPHER_CTX_free(c->k1);
aacf0d
+		EVP_CIPHER_CTX_free(c->k2);
aacf0d
+		EVP_CIPHER_CTX_free(c->k3);
aacf0d
 		explicit_bzero(c, sizeof(*c));
aacf0d
 		free(c);
aacf0d
 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
6cf9b8
@@ -128,13 +135,13 @@ ssh1_3des_iv(EVP_CIPHER_CTX *evp, int do
aacf0d
 	if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
aacf0d
 		return SSH_ERR_INTERNAL_ERROR;
aacf0d
 	if (doset) {
aacf0d
-		memcpy(c->k1.iv, iv, 8);
aacf0d
-		memcpy(c->k2.iv, iv + 8, 8);
aacf0d
-		memcpy(c->k3.iv, iv + 16, 8);
aacf0d
+		memcpy(EVP_CIPHER_CTX_iv_noconst(c->k1), iv, 8);
aacf0d
+		memcpy(EVP_CIPHER_CTX_iv_noconst(c->k2), iv + 8, 8);
aacf0d
+		memcpy(EVP_CIPHER_CTX_iv_noconst(c->k3), iv + 16, 8);
aacf0d
 	} else {
aacf0d
-		memcpy(iv, c->k1.iv, 8);
aacf0d
-		memcpy(iv + 8, c->k2.iv, 8);
aacf0d
-		memcpy(iv + 16, c->k3.iv, 8);
aacf0d
+		memcpy(iv, EVP_CIPHER_CTX_iv(c->k1), 8);
aacf0d
+		memcpy(iv + 8, EVP_CIPHER_CTX_iv(c->k2), 8);
aacf0d
+		memcpy(iv + 16, EVP_CIPHER_CTX_iv(c->k3), 8);
aacf0d
 	}
aacf0d
 	return 0;
aacf0d
 }
6cf9b8
@@ -142,17 +149,14 @@ ssh1_3des_iv(EVP_CIPHER_CTX *evp, int do
aacf0d
 const EVP_CIPHER *
aacf0d
 evp_ssh1_3des(void)
aacf0d
 {
aacf0d
-	static EVP_CIPHER ssh1_3des;
aacf0d
+	EVP_CIPHER *ssh1_3des;
aacf0d
 
aacf0d
-	memset(&ssh1_3des, 0, sizeof(ssh1_3des));
aacf0d
-	ssh1_3des.nid = NID_undef;
aacf0d
-	ssh1_3des.block_size = 8;
aacf0d
-	ssh1_3des.iv_len = 0;
aacf0d
-	ssh1_3des.key_len = 16;
aacf0d
-	ssh1_3des.init = ssh1_3des_init;
aacf0d
-	ssh1_3des.cleanup = ssh1_3des_cleanup;
aacf0d
-	ssh1_3des.do_cipher = ssh1_3des_cbc;
aacf0d
-	ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
aacf0d
-	return &ssh1_3des;
aacf0d
+	ssh1_3des = EVP_CIPHER_meth_new(NID_undef, 8, 16);
aacf0d
+	EVP_CIPHER_meth_set_iv_length(ssh1_3des, 0);
aacf0d
+	EVP_CIPHER_meth_set_init(ssh1_3des, ssh1_3des_init);
aacf0d
+	EVP_CIPHER_meth_set_cleanup(ssh1_3des, ssh1_3des_cleanup);
aacf0d
+	EVP_CIPHER_meth_set_do_cipher(ssh1_3des, ssh1_3des_cbc);
aacf0d
+	EVP_CIPHER_meth_set_flags(ssh1_3des, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH);
aacf0d
+	return ssh1_3des;
aacf0d
 }
6cf9b8
 #endif /* WITH_SSH1 */
6cf9b8
diff -up openssh-7.4p1/cipher-bf1.c.openssl openssh-7.4p1/cipher-bf1.c
6cf9b8
--- openssh-7.4p1/cipher-bf1.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/cipher-bf1.c	2016-12-23 17:47:36.430817752 +0100
6cf9b8
@@ -89,17 +89,28 @@ bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_ch
aacf0d
 const EVP_CIPHER *
aacf0d
 evp_ssh1_bf(void)
aacf0d
 {
aacf0d
-	static EVP_CIPHER ssh1_bf;
aacf0d
+	EVP_CIPHER *ssh1_bf;
aacf0d
 
aacf0d
-	memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
aacf0d
-	orig_bf = ssh1_bf.do_cipher;
aacf0d
-	ssh1_bf.nid = NID_undef;
aacf0d
+	orig_bf = EVP_CIPHER_meth_get_do_cipher(EVP_bf_cbc());
aacf0d
+	/* block_size, length, flags from openssl/crypto/engine/eng_cryptodev.c:638 */
aacf0d
+	ssh1_bf = EVP_CIPHER_meth_new(NID_undef, 8, 32);
aacf0d
+	EVP_CIPHER_meth_set_iv_length(ssh1_bf, 8);
aacf0d
+	EVP_CIPHER_meth_set_flags(ssh1_bf, EVP_CIPH_CBC_MODE);
aacf0d
 #ifdef SSH_OLD_EVP
aacf0d
-	ssh1_bf.init = bf_ssh1_init;
aacf0d
+	EVP_CIPHER_meth_set_init(ssh1_bf, ssh1_bf_init);
aacf0d
+#else
aacf0d
+	EVP_CIPHER_meth_set_init(ssh1_bf,
aacf0d
+	    EVP_CIPHER_meth_get_init(EVP_bf_cbc()));
aacf0d
 #endif
aacf0d
-	ssh1_bf.do_cipher = bf_ssh1_cipher;
aacf0d
-	ssh1_bf.key_len = 32;
aacf0d
-	return (&ssh1_bf);
aacf0d
+	/* copy methods and parameters from old EVP_BF_cbc()
aacf0d
+	 * meth_dup does not allow to change type and key_len */
aacf0d
+	EVP_CIPHER_meth_set_cleanup(ssh1_bf,
aacf0d
+	    EVP_CIPHER_meth_get_cleanup(EVP_bf_cbc()));
aacf0d
+	EVP_CIPHER_meth_set_ctrl(ssh1_bf,
aacf0d
+	    EVP_CIPHER_meth_get_ctrl(EVP_bf_cbc()));
aacf0d
+	/* ASN1 params??? */
aacf0d
+	EVP_CIPHER_meth_set_do_cipher(ssh1_bf, bf_ssh1_cipher);
aacf0d
+	return ssh1_bf;
aacf0d
 }
aacf0d
 #endif /* defined(WITH_OPENSSL) && !defined(OPENSSL_NO_BF) */
6cf9b8
 
6cf9b8
diff -up openssh-7.4p1/cipher.c.openssl openssh-7.4p1/cipher.c
6cf9b8
--- openssh-7.4p1/cipher.c.openssl	2016-12-23 17:47:36.418817747 +0100
6cf9b8
+++ openssh-7.4p1/cipher.c	2016-12-23 17:47:36.430817752 +0100
6cf9b8
@@ -368,7 +368,7 @@ cipher_init(struct sshcipher_ctx **ccp,
6cf9b8
 		ret = SSH_ERR_ALLOC_FAIL;
6cf9b8
 		goto out;
6cf9b8
 	}
6cf9b8
-	if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
aacf0d
+	if (EVP_CipherInit(cc->evp, type, (u_char *)key, (u_char *)iv,
aacf0d
 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
aacf0d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
6cf9b8
 		goto out;
6cf9b8
@@ -386,10 +386,6 @@ cipher_init(struct sshcipher_ctx **ccp,
6cf9b8
 			goto out;
aacf0d
 		}
aacf0d
 	}
6cf9b8
-	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
aacf0d
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
6cf9b8
-		goto out;
aacf0d
-	}
aacf0d
 
aacf0d
 	if (cipher->discard_len > 0) {
aacf0d
 		if ((junk = malloc(cipher->discard_len)) == NULL ||
6cf9b8
@@ -621,7 +617,7 @@ cipher_get_keyiv(struct sshcipher_ctx *c
aacf0d
 			   len, iv))
aacf0d
 			       return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 		} else
6cf9b8
-			memcpy(iv, cc->evp->iv, len);
aacf0d
+			memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
aacf0d
 		break;
aacf0d
 #endif
aacf0d
 #ifdef WITH_SSH1
6cf9b8
@@ -667,7 +663,7 @@ cipher_set_keyiv(struct sshcipher_ctx *c
aacf0d
 			    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
aacf0d
 				return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 		} else
6cf9b8
-			memcpy(cc->evp->iv, iv, evplen);
aacf0d
+			memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen);
aacf0d
 		break;
aacf0d
 #endif
aacf0d
 #ifdef WITH_SSH1
6cf9b8
@@ -681,8 +677,8 @@ cipher_set_keyiv(struct sshcipher_ctx *c
aacf0d
 }
aacf0d
 
aacf0d
 #ifdef WITH_OPENSSL
6cf9b8
-#define EVP_X_STATE(evp)	(evp)->cipher_data
6cf9b8
-#define EVP_X_STATE_LEN(evp)	(evp)->cipher->ctx_size
aacf0d
+#define EVP_X_STATE(evp)	EVP_CIPHER_CTX_get_cipher_data(evp)
aacf0d
+#define EVP_X_STATE_LEN(evp)	EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
aacf0d
 #endif
aacf0d
 
aacf0d
 int
6cf9b8
diff -up openssh-7.4p1/ctr-cavstest.c.openssl openssh-7.4p1/ctr-cavstest.c
6cf9b8
--- openssh-7.4p1/ctr-cavstest.c.openssl	2016-12-23 17:47:36.344817716 +0100
6cf9b8
+++ openssh-7.4p1/ctr-cavstest.c	2016-12-23 17:47:36.430817752 +0100
aacf0d
@@ -144,7 +144,7 @@ int main (int argc, char *argv[])
aacf0d
                 usage();
aacf0d
         }
aacf0d
 
aacf0d
-	SSLeay_add_all_algorithms();
aacf0d
+	OpenSSL_add_all_algorithms();
aacf0d
 
aacf0d
 	c = cipher_by_name(algo);
aacf0d
 	if (c == NULL) {
6cf9b8
diff -up openssh-7.4p1/dh.c.openssl openssh-7.4p1/dh.c
6cf9b8
--- openssh-7.4p1/dh.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/dh.c	2016-12-23 17:47:36.430817752 +0100
6cf9b8
@@ -212,14 +212,15 @@ choose_dh(int min, int wantbits, int max
aacf0d
 /* diffie-hellman-groupN-sha1 */
aacf0d
 
aacf0d
 int
aacf0d
-dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
aacf0d
+dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
aacf0d
 {
aacf0d
 	int i;
aacf0d
 	int n = BN_num_bits(dh_pub);
aacf0d
 	int bits_set = 0;
aacf0d
 	BIGNUM *tmp;
aacf0d
+	const BIGNUM *p;
aacf0d
 
aacf0d
-	if (dh_pub->neg) {
aacf0d
+	if (BN_is_negative(dh_pub)) {
aacf0d
 		logit("invalid public DH value: negative");
aacf0d
 		return 0;
aacf0d
 	}
6cf9b8
@@ -232,7 +233,8 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
aacf0d
 		error("%s: BN_new failed", __func__);
aacf0d
 		return 0;
aacf0d
 	}
aacf0d
-	if (!BN_sub(tmp, dh->p, BN_value_one()) ||
aacf0d
+	DH_get0_pqg(dh, &p, NULL, NULL);
aacf0d
+	if (!BN_sub(tmp, p, BN_value_one()) ||
aacf0d
 	    BN_cmp(dh_pub, tmp) != -1) {		/* pub_exp > p-2 */
aacf0d
 		BN_clear_free(tmp);
aacf0d
 		logit("invalid public DH value: >= p-1");
6cf9b8
@@ -243,14 +245,14 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
aacf0d
 	for (i = 0; i <= n; i++)
aacf0d
 		if (BN_is_bit_set(dh_pub, i))
aacf0d
 			bits_set++;
aacf0d
-	debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
aacf0d
+	debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
aacf0d
 
aacf0d
 	/*
aacf0d
 	 * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
aacf0d
 	 */
aacf0d
 	if (bits_set < 4) {
aacf0d
 		logit("invalid public DH value (%d/%d)",
aacf0d
-		   bits_set, BN_num_bits(dh->p));
aacf0d
+		   bits_set, BN_num_bits(p));
aacf0d
 		return 0;
aacf0d
 	}
aacf0d
 	return 1;
6cf9b8
@@ -260,9 +262,11 @@ int
aacf0d
 dh_gen_key(DH *dh, int need)
aacf0d
 {
aacf0d
 	int pbits;
aacf0d
+	const BIGNUM *p, *pub_key;
aacf0d
 
aacf0d
-	if (need < 0 || dh->p == NULL ||
aacf0d
-	    (pbits = BN_num_bits(dh->p)) <= 0 ||
aacf0d
+	DH_get0_pqg(dh, &p, NULL, NULL);
aacf0d
+	if (need < 0 || p == NULL ||
aacf0d
+	    (pbits = BN_num_bits(p)) <= 0 ||
aacf0d
 	    need > INT_MAX / 2 || 2 * need > pbits)
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	if (need < 256)
6cf9b8
@@ -271,10 +275,11 @@ dh_gen_key(DH *dh, int need)
aacf0d
 	 * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
aacf0d
 	 * so double requested need here.
aacf0d
 	 */
6cf9b8
-	dh->length = MINIMUM(need * 2, pbits - 1);
aacf0d
-	if (DH_generate_key(dh) == 0 ||
aacf0d
-	    !dh_pub_is_valid(dh, dh->pub_key)) {
aacf0d
-		BN_clear_free(dh->priv_key);
aacf0d
+	DH_set_length(dh, MINIMUM(need * 2, pbits - 1));
aacf0d
+	if (DH_generate_key(dh) == 0)
aacf0d
+		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
6cf9b8
+	if (!dh_pub_is_valid(dh, pub_key)) {
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
6cf9b8
 	}
aacf0d
 	return 0;
6cf9b8
@@ -284,15 +290,22 @@ DH *
aacf0d
 dh_new_group_asc(const char *gen, const char *modulus)
aacf0d
 {
aacf0d
 	DH *dh;
aacf0d
+	BIGNUM *p, *g;
aacf0d
 
aacf0d
-	if ((dh = DH_new()) == NULL)
aacf0d
-		return NULL;
aacf0d
-	if (BN_hex2bn(&dh->p, modulus) == 0 ||
aacf0d
-	    BN_hex2bn(&dh->g, gen) == 0) {
aacf0d
-		DH_free(dh);
aacf0d
-		return NULL;
aacf0d
-	}
aacf0d
+	if ((dh = DH_new()) == NULL ||
aacf0d
+	    (p = BN_new()) == NULL ||
aacf0d
+	    (g = BN_new()) == NULL)
aacf0d
+		goto err;
aacf0d
+	if (BN_hex2bn(&p, modulus) == 0 ||
aacf0d
+	    BN_hex2bn(&g, gen) == 0 ||
aacf0d
+	    DH_set0_pqg(dh, p, NULL, g) == 0)
aacf0d
+		goto err;
aacf0d
 	return (dh);
aacf0d
+err:
aacf0d
+	DH_free(dh);
aacf0d
+	BN_free(p);
aacf0d
+	BN_free(g);
aacf0d
+	return NULL;
aacf0d
 }
aacf0d
 
aacf0d
 /*
6cf9b8
@@ -307,8 +320,7 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulu
aacf0d
 
aacf0d
 	if ((dh = DH_new()) == NULL)
aacf0d
 		return NULL;
aacf0d
-	dh->p = modulus;
aacf0d
-	dh->g = gen;
aacf0d
+	DH_set0_pqg(dh, modulus, NULL, gen);
aacf0d
 
aacf0d
 	return (dh);
aacf0d
 }
6cf9b8
diff -up openssh-7.4p1/dh.h.openssl openssh-7.4p1/dh.h
6cf9b8
--- openssh-7.4p1/dh.h.openssl	2016-12-23 17:47:36.418817747 +0100
6cf9b8
+++ openssh-7.4p1/dh.h	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -42,7 +42,7 @@ DH	*dh_new_group18(void);
aacf0d
 DH	*dh_new_group_fallback(int);
aacf0d
 
aacf0d
 int	 dh_gen_key(DH *, int);
aacf0d
-int	 dh_pub_is_valid(DH *, BIGNUM *);
aacf0d
+int	 dh_pub_is_valid(const DH *, const BIGNUM *);
aacf0d
 
aacf0d
 u_int	 dh_estimate(int);
aacf0d
 
6cf9b8
diff -up openssh-7.4p1/digest-openssl.c.openssl openssh-7.4p1/digest-openssl.c
6cf9b8
--- openssh-7.4p1/digest-openssl.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/digest-openssl.c	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -43,7 +43,7 @@
aacf0d
 
aacf0d
 struct ssh_digest_ctx {
aacf0d
 	int alg;
aacf0d
-	EVP_MD_CTX mdctx;
aacf0d
+	EVP_MD_CTX *mdctx;
aacf0d
 };
aacf0d
 
aacf0d
 struct ssh_digest {
aacf0d
@@ -107,7 +107,7 @@ ssh_digest_bytes(int alg)
aacf0d
 size_t
aacf0d
 ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
aacf0d
 {
aacf0d
-	return EVP_MD_CTX_block_size(&ctx->mdctx);
aacf0d
+	return EVP_MD_CTX_block_size(ctx->mdctx);
aacf0d
 }
aacf0d
 
aacf0d
 struct ssh_digest_ctx *
aacf0d
@@ -119,8 +119,9 @@ ssh_digest_start(int alg)
aacf0d
 	if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
aacf0d
 		return NULL;
aacf0d
 	ret->alg = alg;
aacf0d
-	EVP_MD_CTX_init(&ret->mdctx);
aacf0d
-	if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
aacf0d
+	ret->mdctx = EVP_MD_CTX_new();
aacf0d
+	if (ret->mdctx == NULL ||
aacf0d
+	    EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
aacf0d
 		free(ret);
aacf0d
 		return NULL;
aacf0d
 	}
aacf0d
@@ -133,7 +134,7 @@ ssh_digest_copy_state(struct ssh_digest_
aacf0d
 	if (from->alg != to->alg)
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	/* we have bcopy-style order while openssl has memcpy-style */
aacf0d
-	if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
aacf0d
+	if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 	return 0;
aacf0d
 }
aacf0d
@@ -141,7 +142,7 @@ ssh_digest_copy_state(struct ssh_digest_
aacf0d
 int
aacf0d
 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
aacf0d
 {
aacf0d
-	if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
aacf0d
+	if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 	return 0;
aacf0d
 }
aacf0d
@@ -162,7 +163,7 @@ ssh_digest_final(struct ssh_digest_ctx *
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 	if (dlen < digest->digest_len) /* No truncation allowed */
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
-	if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
aacf0d
+	if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 	if (l != digest->digest_len) /* sanity */
aacf0d
 		return SSH_ERR_INTERNAL_ERROR;
aacf0d
@@ -173,7 +174,7 @@ void
aacf0d
 ssh_digest_free(struct ssh_digest_ctx *ctx)
aacf0d
 {
aacf0d
 	if (ctx != NULL) {
aacf0d
-		EVP_MD_CTX_cleanup(&ctx->mdctx);
aacf0d
+		EVP_MD_CTX_free(ctx->mdctx);
aacf0d
 		explicit_bzero(ctx, sizeof(*ctx));
aacf0d
 		free(ctx);
aacf0d
 	}
6cf9b8
diff -up openssh-7.4p1/entropy.c.openssl openssh-7.4p1/entropy.c
6cf9b8
--- openssh-7.4p1/entropy.c.openssl	2016-12-23 17:47:36.419817747 +0100
6cf9b8
+++ openssh-7.4p1/entropy.c	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -218,7 +218,9 @@ seed_rng(void)
aacf0d
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
aacf0d
 
aacf0d
 	/* clean the PRNG status when exiting the program */
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
 	atexit(RAND_cleanup);
aacf0d
+#endif
aacf0d
 
aacf0d
 #ifndef OPENSSL_PRNG_ONLY
aacf0d
 	if (RAND_status() == 1) {
6cf9b8
diff -up openssh-7.4p1/gss-genr.c.openssl openssh-7.4p1/gss-genr.c
6cf9b8
--- openssh-7.4p1/gss-genr.c.openssl	2016-12-23 17:47:36.392817736 +0100
6cf9b8
+++ openssh-7.4p1/gss-genr.c	2016-12-23 17:47:36.431817752 +0100
6cf9b8
@@ -99,7 +99,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 	u_char digest[EVP_MAX_MD_SIZE];
aacf0d
 	char deroid[2];
aacf0d
 	const EVP_MD *evp_md = EVP_md5();
aacf0d
-	EVP_MD_CTX md;
aacf0d
+	EVP_MD_CTX *md;
aacf0d
 	char *s, *cp, *p;
aacf0d
 
aacf0d
 	if (gss_enc2oid != NULL) {
6cf9b8
@@ -113,6 +113,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 
aacf0d
 	buffer_init(&buf;;
aacf0d
 
aacf0d
+	md = EVP_MD_CTX_new();
aacf0d
 	oidpos = 0;
aacf0d
 	s = cp = xstrdup(kex);
aacf0d
 	for (i = 0; i < gss_supported->count; i++) {
6cf9b8
@@ -122,12 +123,13 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 			deroid[0] = SSH_GSS_OIDTYPE;
aacf0d
 			deroid[1] = gss_supported->elements[i].length;
aacf0d
 
aacf0d
-			EVP_DigestInit(&md, evp_md);
aacf0d
-			EVP_DigestUpdate(&md, deroid, 2);
aacf0d
-			EVP_DigestUpdate(&md,
aacf0d
+			EVP_MD_CTX_reset(md);
aacf0d
+			EVP_DigestInit(md, evp_md);
aacf0d
+			EVP_DigestUpdate(md, deroid, 2);
aacf0d
+			EVP_DigestUpdate(md,
aacf0d
 			    gss_supported->elements[i].elements,
aacf0d
 			    gss_supported->elements[i].length);
aacf0d
-			EVP_DigestFinal(&md, digest, NULL);
aacf0d
+			EVP_DigestFinal(md, digest, NULL);
aacf0d
 
aacf0d
 			encoded = xmalloc(EVP_MD_size(evp_md) * 2);
aacf0d
 			enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
6cf9b8
@@ -149,6 +151,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
aacf0d
 		}
aacf0d
 	}
aacf0d
 	free(s);
aacf0d
+	EVP_MD_CTX_free(md);
aacf0d
 	gss_enc2oid[oidpos].oid = NULL;
aacf0d
 	gss_enc2oid[oidpos].encoded = NULL;
aacf0d
 
6cf9b8
diff -up openssh-7.4p1/includes.h.openssl openssh-7.4p1/includes.h
6cf9b8
--- openssh-7.4p1/includes.h.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/includes.h	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -163,6 +163,7 @@
aacf0d
 
aacf0d
 #ifdef WITH_OPENSSL
aacf0d
 #include <openssl/opensslv.h> /* For OPENSSL_VERSION_NUMBER */
aacf0d
+#include "libcrypto-compat.h"
aacf0d
 #endif
aacf0d
 
aacf0d
 #include "defines.h"
6cf9b8
diff -up openssh-7.4p1/kexdhc.c.openssl openssh-7.4p1/kexdhc.c
6cf9b8
--- openssh-7.4p1/kexdhc.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/kexdhc.c	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -56,6 +56,7 @@ kexdh_client(struct ssh *ssh)
aacf0d
 {
aacf0d
 	struct kex *kex = ssh->kex;
aacf0d
 	int r;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	/* generate and send 'e', client DH public key */
aacf0d
 	switch (kex->kex_type) {
aacf0d
@@ -81,21 +82,27 @@ kexdh_client(struct ssh *ssh)
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 	debug("sending SSH2_MSG_KEXDH_INIT");
aacf0d
-	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
aacf0d
-	    (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
aacf0d
+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
aacf0d
+		goto out;
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
aacf0d
 #ifdef DEBUG_KEXDH
aacf0d
 	DHparams_print_fp(stderr, kex->dh);
aacf0d
 	fprintf(stderr, "pub= ");
aacf0d
-	BN_print_fp(stderr, kex->dh->pub_key);
aacf0d
+	BN_print_fp(stderr, pub_key);
aacf0d
 	fprintf(stderr, "\n");
aacf0d
 #endif
aacf0d
 	debug("expecting SSH2_MSG_KEXDH_REPLY");
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
aacf0d
@@ -110,6 +117,7 @@ input_kex_dh(int type, u_int32_t seq, vo
aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
aacf0d
 	size_t klen = 0, slen, sbloblen, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	if (kex->verify_host_key == NULL) {
aacf0d
 		r = SSH_ERR_INVALID_ARGUMENT;
aacf0d
@@ -169,6 +177,7 @@ input_kex_dh(int type, u_int32_t seq, vo
aacf0d
 #endif
aacf0d
 
aacf0d
 	/* calc and verify H */
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	hashlen = sizeof(hash);
aacf0d
 	if ((r = kex_dh_hash(
aacf0d
 	    kex->hash_alg,
aacf0d
@@ -177,7 +186,7 @@ input_kex_dh(int type, u_int32_t seq, vo
aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
aacf0d
 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    pub_key,
aacf0d
 	    dh_server_pub,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
6cf9b8
diff -up openssh-7.4p1/kexdhs.c.openssl openssh-7.4p1/kexdhs.c
6cf9b8
--- openssh-7.4p1/kexdhs.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/kexdhs.c	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -87,6 +87,10 @@ kexdh_server(struct ssh *ssh)
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
aacf0d
@@ -102,6 +106,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 	size_t sbloblen, slen;
aacf0d
 	size_t klen = 0, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	if (kex->load_host_public_key == NULL ||
aacf0d
 	    kex->load_host_private_key == NULL) {
aacf0d
@@ -164,6 +169,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 		goto out;
aacf0d
 	/* calc H */
aacf0d
 	hashlen = sizeof(hash);
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	if ((r = kex_dh_hash(
aacf0d
 	    kex->hash_alg,
aacf0d
 	    kex->client_version_string,
aacf0d
@@ -172,7 +178,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
 	    dh_client_pub,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    pub_key,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
aacf0d
 		goto out;
aacf0d
@@ -198,7 +204,7 @@ input_kex_dh_init(int type, u_int32_t se
aacf0d
 	/* send server hostkey, DH pubkey 'f' and singed H */
aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
aacf0d
 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||	/* f */
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||	/* f */
aacf0d
 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
6cf9b8
diff -up openssh-7.4p1/kexgexc.c.openssl openssh-7.4p1/kexgexc.c
6cf9b8
--- openssh-7.4p1/kexgexc.c.openssl	2016-12-23 17:47:36.419817747 +0100
6cf9b8
+++ openssh-7.4p1/kexgexc.c	2016-12-23 17:47:36.431817752 +0100
aacf0d
@@ -96,6 +96,7 @@ input_kex_dh_gex_group(int type, u_int32
aacf0d
 	struct kex *kex = ssh->kex;
aacf0d
 	BIGNUM *p = NULL, *g = NULL;
aacf0d
 	int r, bits;
aacf0d
+	const BIGNUM *pub_key;
aacf0d
 
aacf0d
 	debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
aacf0d
 
aacf0d
@@ -120,26 +121,30 @@ input_kex_dh_gex_group(int type, u_int32
aacf0d
 	p = g = NULL; /* belong to kex->dh now */
aacf0d
 
aacf0d
 	/* generate and send 'e', client DH public key */
aacf0d
-	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
aacf0d
-	    (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
aacf0d
+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
aacf0d
+		goto out;
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
aacf0d
 #ifdef DEBUG_KEXDH
aacf0d
 	DHparams_print_fp(stderr, kex->dh);
aacf0d
 	fprintf(stderr, "pub= ");
aacf0d
-	BN_print_fp(stderr, kex->dh->pub_key);
aacf0d
+	BN_print_fp(stderr, pub_key);
aacf0d
 	fprintf(stderr, "\n");
aacf0d
 #endif
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
aacf0d
 	r = 0;
aacf0d
 out:
aacf0d
-	if (p)
aacf0d
-		BN_clear_free(p);
aacf0d
-	if (g)
aacf0d
-		BN_clear_free(g);
aacf0d
+	BN_clear_free(p);
aacf0d
+	BN_clear_free(g);
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
aacf0d
@@ -154,6 +159,7 @@ input_kex_dh_gex_reply(int type, u_int32
aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
aacf0d
 	size_t klen = 0, slen, sbloblen, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *p, *g, *pub_key;
aacf0d
 
aacf0d
 	debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
aacf0d
 	if (kex->verify_host_key == NULL) {
aacf0d
@@ -220,6 +226,8 @@ input_kex_dh_gex_reply(int type, u_int32
aacf0d
 		kex->min = kex->max = -1;
aacf0d
 
aacf0d
 	/* calc and verify H */
aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	hashlen = sizeof(hash);
aacf0d
 	if ((r = kexgex_hash(
aacf0d
 	    kex->hash_alg,
aacf0d
@@ -229,8 +237,8 @@ input_kex_dh_gex_reply(int type, u_int32
aacf0d
 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
 	    kex->min, kex->nbits, kex->max,
aacf0d
-	    kex->dh->p, kex->dh->g,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    p, g,
aacf0d
+	    pub_key,
aacf0d
 	    dh_server_pub,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
6cf9b8
diff -up openssh-7.4p1/kexgexs.c.openssl openssh-7.4p1/kexgexs.c
6cf9b8
--- openssh-7.4p1/kexgexs.c.openssl	2016-12-23 17:47:36.419817747 +0100
6cf9b8
+++ openssh-7.4p1/kexgexs.c	2016-12-23 17:47:36.432817753 +0100
aacf0d
@@ -73,6 +73,7 @@ input_kex_dh_gex_request(int type, u_int
aacf0d
 	struct kex *kex = ssh->kex;
aacf0d
 	int r;
aacf0d
 	u_int min = 0, max = 0, nbits = 0;
aacf0d
+	const BIGNUM *p, *g;
aacf0d
 
aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
aacf0d
 	if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
aacf0d
@@ -102,9 +103,10 @@ input_kex_dh_gex_request(int type, u_int
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
aacf0d
 
aacf0d
@@ -116,6 +118,10 @@ input_kex_dh_gex_request(int type, u_int
aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
+	if (r != 0) {
aacf0d
+		DH_free(kex->dh);
aacf0d
+		kex->dh = NULL;
aacf0d
+	}
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
aacf0d
@@ -131,6 +137,7 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 	size_t sbloblen, slen;
aacf0d
 	size_t klen = 0, hashlen;
aacf0d
 	int kout, r;
aacf0d
+	const BIGNUM *p, *g, *pub_key;
aacf0d
 
aacf0d
 	if (kex->load_host_public_key == NULL ||
aacf0d
 	    kex->load_host_private_key == NULL) {
aacf0d
@@ -193,6 +200,8 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 		goto out;
aacf0d
 	/* calc H */
aacf0d
 	hashlen = sizeof(hash);
aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
aacf0d
 	if ((r = kexgex_hash(
aacf0d
 	    kex->hash_alg,
aacf0d
 	    kex->client_version_string,
aacf0d
@@ -201,9 +210,9 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
aacf0d
 	    server_host_key_blob, sbloblen,
aacf0d
 	    kex->min, kex->nbits, kex->max,
aacf0d
-	    kex->dh->p, kex->dh->g,
aacf0d
+	    p, g,
aacf0d
 	    dh_client_pub,
aacf0d
-	    kex->dh->pub_key,
aacf0d
+	    pub_key,
aacf0d
 	    shared_secret,
aacf0d
 	    hash, &hashlen)) != 0)
aacf0d
 		goto out;
aacf0d
@@ -229,7 +238,7 @@ input_kex_dh_gex_init(int type, u_int32_
aacf0d
 	/* send server hostkey, DH pubkey 'f' and singed H */
aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
aacf0d
 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
aacf0d
 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
aacf0d
 		goto out;
6cf9b8
diff -up openssh-7.4p1/kexgssc.c.openssl openssh-7.4p1/kexgssc.c
6cf9b8
--- openssh-7.4p1/kexgssc.c.openssl	2016-12-23 17:47:36.349817718 +0100
6cf9b8
+++ openssh-7.4p1/kexgssc.c	2016-12-23 17:47:36.432817753 +0100
aacf0d
@@ -58,6 +58,7 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 	BIGNUM *shared_secret = NULL;
aacf0d
 	BIGNUM *p = NULL;
aacf0d
 	BIGNUM *g = NULL;	
aacf0d
+	const BIGNUM *pub_key, *p1, *g1;
aacf0d
 	u_char *kbuf;
aacf0d
 	u_char *serverhostkey = NULL;
aacf0d
 	u_char *empty = "";
aacf0d
@@ -121,6 +122,7 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 	
aacf0d
 	/* Step 1 - e is dh->pub_key */
aacf0d
 	dh_gen_key(dh, ssh->kex->we_need * 8);
aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
aacf0d
 
aacf0d
 	/* This is f, we initialise it now to make life easier */
aacf0d
 	dh_server_pub = BN_new();
aacf0d
@@ -168,7 +170,7 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 				packet_start(SSH2_MSG_KEXGSS_INIT);
aacf0d
 				packet_put_string(send_tok.value,
aacf0d
 				    send_tok.length);
aacf0d
-				packet_put_bignum2(dh->pub_key);
aacf0d
+				packet_put_bignum2((BIGNUM *)pub_key);
aacf0d
 				first = 0;
aacf0d
 			} else {
aacf0d
 				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
aacf0d
@@ -275,13 +277,14 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
aacf0d
 		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
aacf0d
 		    (serverhostkey ? serverhostkey : empty), slen,
aacf0d
-		    dh->pub_key,	/* e */
aacf0d
+		    pub_key,		/* e */
aacf0d
 		    dh_server_pub,	/* f */
aacf0d
 		    shared_secret,	/* K */
aacf0d
 		    hash, &hashlen
aacf0d
 		);
aacf0d
 		break;
aacf0d
 	case KEX_GSS_GEX_SHA1:
aacf0d
+		DH_get0_pqg(dh, &p1, NULL, &g1;;
aacf0d
 		kexgex_hash(
aacf0d
 		    ssh->kex->hash_alg,
aacf0d
 		    ssh->kex->client_version_string,
aacf0d
@@ -290,8 +293,8 @@ kexgss_client(struct ssh *ssh) {
aacf0d
 		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
aacf0d
 		    (serverhostkey ? serverhostkey : empty), slen,
aacf0d
  		    min, nbits, max,
aacf0d
-		    dh->p, dh->g,
aacf0d
-		    dh->pub_key,
aacf0d
+		    p1, g1,
aacf0d
+		    pub_key,
aacf0d
 		    dh_server_pub,
aacf0d
 		    shared_secret,
aacf0d
 		    hash, &hashlen
6cf9b8
diff -up openssh-7.4p1/kexgsss.c.openssl openssh-7.4p1/kexgsss.c
6cf9b8
--- openssh-7.4p1/kexgsss.c.openssl	2016-12-23 17:47:36.349817718 +0100
6cf9b8
+++ openssh-7.4p1/kexgsss.c	2016-12-23 17:47:36.432817753 +0100
aacf0d
@@ -77,6 +77,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 	char *mechs;
aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
aacf0d
 	size_t hashlen;
aacf0d
+	const BIGNUM *p, *g, *pub_key;
aacf0d
 
aacf0d
 	/* Initialise GSSAPI */
aacf0d
 
aacf0d
@@ -122,9 +123,10 @@ kexgss_server(struct ssh *ssh)
aacf0d
 		if (dh == NULL)
aacf0d
 			packet_disconnect("Protocol error: no matching group found");
aacf0d
 
aacf0d
+		DH_get0_pqg(dh, &p, NULL, &g);
aacf0d
 		packet_start(SSH2_MSG_KEXGSS_GROUP);
aacf0d
-		packet_put_bignum2(dh->p);
aacf0d
-		packet_put_bignum2(dh->g);
aacf0d
+		packet_put_bignum2((BIGNUM *)p);
aacf0d
+		packet_put_bignum2((BIGNUM *)g);
aacf0d
 		packet_send();
aacf0d
 
aacf0d
 		packet_write_wait();
aacf0d
@@ -216,6 +218,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 	memset(kbuf, 0, klen);
aacf0d
 	free(kbuf);
aacf0d
 
aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
aacf0d
 	hashlen = sizeof(hash);
aacf0d
 	switch (ssh->kex->kex_type) {
aacf0d
 	case KEX_GSS_GRP1_SHA1:
aacf0d
@@ -225,7 +228,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
aacf0d
 		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
aacf0d
 		    NULL, 0, /* Change this if we start sending host keys */
aacf0d
-		    dh_client_pub, dh->pub_key, shared_secret,
aacf0d
+		    dh_client_pub, pub_key, shared_secret,
aacf0d
 		    hash, &hashlen
aacf0d
 		);
aacf0d
 		break;
aacf0d
@@ -237,9 +240,9 @@ kexgss_server(struct ssh *ssh)
aacf0d
 		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
aacf0d
 		    NULL, 0,
aacf0d
 		    cmin, nbits, cmax,
aacf0d
-		    dh->p, dh->g,
aacf0d
+		    p, g,
aacf0d
 		    dh_client_pub,
aacf0d
-		    dh->pub_key,
aacf0d
+		    pub_key,
aacf0d
 		    shared_secret,
aacf0d
 		    hash, &hashlen
aacf0d
 		);
aacf0d
@@ -263,7 +266,7 @@ kexgss_server(struct ssh *ssh)
aacf0d
 		fatal("Couldn't get MIC");
aacf0d
 
aacf0d
 	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
aacf0d
-	packet_put_bignum2(dh->pub_key);
aacf0d
+	packet_put_bignum2((BIGNUM *)pub_key);
aacf0d
 	packet_put_string(msg_tok.value,msg_tok.length);
aacf0d
 
aacf0d
 	if (send_tok.length != 0) {
6cf9b8
diff -up openssh-7.4p1/libcrypto-compat.c.openssl openssh-7.4p1/libcrypto-compat.c
6cf9b8
--- openssh-7.4p1/libcrypto-compat.c.openssl	2016-12-23 17:47:36.432817753 +0100
6cf9b8
+++ openssh-7.4p1/libcrypto-compat.c	2016-12-23 17:47:36.432817753 +0100
aacf0d
@@ -0,0 +1,546 @@
aacf0d
+/*
aacf0d
+ * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
aacf0d
+ *
aacf0d
+ * Licensed under the OpenSSL license (the "License").  You may not use
aacf0d
+ * this file except in compliance with the License.  You can obtain a copy
aacf0d
+ * in the file LICENSE in the source distribution or at
aacf0d
+ * https://www.openssl.org/source/license.html
aacf0d
+ */
aacf0d
+
aacf0d
+#include "includes.h"
aacf0d
+
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
+
aacf0d
+#include <string.h>
aacf0d
+#include <openssl/engine.h>
aacf0d
+
aacf0d
+static void *OPENSSL_zalloc(size_t num)
aacf0d
+{
aacf0d
+    void *ret = OPENSSL_malloc(num);
aacf0d
+
aacf0d
+    if (ret != NULL)
aacf0d
+        memset(ret, 0, num);
aacf0d
+    return ret;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
aacf0d
+{
aacf0d
+    /* If the fields n and e in r are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL for n and e.  d may be
aacf0d
+     * left NULL (in case only the public key is used).
aacf0d
+     */
aacf0d
+    if ((r->n == NULL && n == NULL)
aacf0d
+        || (r->e == NULL && e == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (n != NULL) {
aacf0d
+        BN_free(r->n);
aacf0d
+        r->n = n;
aacf0d
+    }
aacf0d
+    if (e != NULL) {
aacf0d
+        BN_free(r->e);
aacf0d
+        r->e = e;
aacf0d
+    }
aacf0d
+    if (d != NULL) {
aacf0d
+        BN_free(r->d);
aacf0d
+        r->d = d;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
aacf0d
+{
aacf0d
+    /* If the fields p and q in r are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.
aacf0d
+     */
aacf0d
+    if ((r->p == NULL && p == NULL)
aacf0d
+        || (r->q == NULL && q == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (p != NULL) {
aacf0d
+        BN_free(r->p);
aacf0d
+        r->p = p;
aacf0d
+    }
aacf0d
+    if (q != NULL) {
aacf0d
+        BN_free(r->q);
aacf0d
+        r->q = q;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
aacf0d
+{
aacf0d
+    /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.
aacf0d
+     */
aacf0d
+    if ((r->dmp1 == NULL && dmp1 == NULL)
aacf0d
+        || (r->dmq1 == NULL && dmq1 == NULL)
aacf0d
+        || (r->iqmp == NULL && iqmp == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (dmp1 != NULL) {
aacf0d
+        BN_free(r->dmp1);
aacf0d
+        r->dmp1 = dmp1;
aacf0d
+    }
aacf0d
+    if (dmq1 != NULL) {
aacf0d
+        BN_free(r->dmq1);
aacf0d
+        r->dmq1 = dmq1;
aacf0d
+    }
aacf0d
+    if (iqmp != NULL) {
aacf0d
+        BN_free(r->iqmp);
aacf0d
+        r->iqmp = iqmp;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_get0_key(const RSA *r,
aacf0d
+                  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
aacf0d
+{
aacf0d
+    if (n != NULL)
aacf0d
+        *n = r->n;
aacf0d
+    if (e != NULL)
aacf0d
+        *e = r->e;
aacf0d
+    if (d != NULL)
aacf0d
+        *d = r->d;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
aacf0d
+{
aacf0d
+    if (p != NULL)
aacf0d
+        *p = r->p;
aacf0d
+    if (q != NULL)
aacf0d
+        *q = r->q;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_get0_crt_params(const RSA *r,
aacf0d
+                         const BIGNUM **dmp1, const BIGNUM **dmq1,
aacf0d
+                         const BIGNUM **iqmp)
aacf0d
+{
aacf0d
+    if (dmp1 != NULL)
aacf0d
+        *dmp1 = r->dmp1;
aacf0d
+    if (dmq1 != NULL)
aacf0d
+        *dmq1 = r->dmq1;
aacf0d
+    if (iqmp != NULL)
aacf0d
+        *iqmp = r->iqmp;
aacf0d
+}
aacf0d
+
aacf0d
+void DSA_get0_pqg(const DSA *d,
aacf0d
+                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
aacf0d
+{
aacf0d
+    if (p != NULL)
aacf0d
+        *p = d->p;
aacf0d
+    if (q != NULL)
aacf0d
+        *q = d->q;
aacf0d
+    if (g != NULL)
aacf0d
+        *g = d->g;
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
aacf0d
+{
aacf0d
+    /* If the fields p, q and g in d are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.
aacf0d
+     */
aacf0d
+    if ((d->p == NULL && p == NULL)
aacf0d
+        || (d->q == NULL && q == NULL)
aacf0d
+        || (d->g == NULL && g == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (p != NULL) {
aacf0d
+        BN_free(d->p);
aacf0d
+        d->p = p;
aacf0d
+    }
aacf0d
+    if (q != NULL) {
aacf0d
+        BN_free(d->q);
aacf0d
+        d->q = q;
aacf0d
+    }
aacf0d
+    if (g != NULL) {
aacf0d
+        BN_free(d->g);
aacf0d
+        d->g = g;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DSA_get0_key(const DSA *d,
aacf0d
+                  const BIGNUM **pub_key, const BIGNUM **priv_key)
aacf0d
+{
aacf0d
+    if (pub_key != NULL)
aacf0d
+        *pub_key = d->pub_key;
aacf0d
+    if (priv_key != NULL)
aacf0d
+        *priv_key = d->priv_key;
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
aacf0d
+{
aacf0d
+    /* If the field pub_key in d is NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.  The priv_key field may
aacf0d
+     * be left NULL.
aacf0d
+     */
aacf0d
+    if (d->pub_key == NULL && pub_key == NULL)
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (pub_key != NULL) {
aacf0d
+        BN_free(d->pub_key);
aacf0d
+        d->pub_key = pub_key;
aacf0d
+    }
aacf0d
+    if (priv_key != NULL) {
aacf0d
+        BN_free(d->priv_key);
aacf0d
+        d->priv_key = priv_key;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
aacf0d
+{
aacf0d
+    if (pr != NULL)
aacf0d
+        *pr = sig->r;
aacf0d
+    if (ps != NULL)
aacf0d
+        *ps = sig->s;
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
aacf0d
+{
aacf0d
+    if (r == NULL || s == NULL)
aacf0d
+        return 0;
aacf0d
+    BN_clear_free(sig->r);
aacf0d
+    BN_clear_free(sig->s);
aacf0d
+    sig->r = r;
aacf0d
+    sig->s = s;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
aacf0d
+{
aacf0d
+    if (pr != NULL)
aacf0d
+        *pr = sig->r;
aacf0d
+    if (ps != NULL)
aacf0d
+        *ps = sig->s;
aacf0d
+}
aacf0d
+
aacf0d
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
aacf0d
+{
aacf0d
+    if (r == NULL || s == NULL)
aacf0d
+        return 0;
aacf0d
+    BN_clear_free(sig->r);
aacf0d
+    BN_clear_free(sig->s);
aacf0d
+    sig->r = r;
aacf0d
+    sig->s = s;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DH_get0_pqg(const DH *dh,
aacf0d
+                 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
aacf0d
+{
aacf0d
+    if (p != NULL)
aacf0d
+        *p = dh->p;
aacf0d
+    if (q != NULL)
aacf0d
+        *q = dh->q;
aacf0d
+    if (g != NULL)
aacf0d
+        *g = dh->g;
aacf0d
+}
aacf0d
+
aacf0d
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
aacf0d
+{
aacf0d
+    /* If the fields p and g in d are NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.  q may remain NULL.
aacf0d
+     */
aacf0d
+    if ((dh->p == NULL && p == NULL)
aacf0d
+        || (dh->g == NULL && g == NULL))
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (p != NULL) {
aacf0d
+        BN_free(dh->p);
aacf0d
+        dh->p = p;
aacf0d
+    }
aacf0d
+    if (q != NULL) {
aacf0d
+        BN_free(dh->q);
aacf0d
+        dh->q = q;
aacf0d
+    }
aacf0d
+    if (g != NULL) {
aacf0d
+        BN_free(dh->g);
aacf0d
+        dh->g = g;
aacf0d
+    }
aacf0d
+
aacf0d
+    if (q != NULL) {
aacf0d
+        dh->length = BN_num_bits(q);
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
aacf0d
+{
aacf0d
+    if (pub_key != NULL)
aacf0d
+        *pub_key = dh->pub_key;
aacf0d
+    if (priv_key != NULL)
aacf0d
+        *priv_key = dh->priv_key;
aacf0d
+}
aacf0d
+
aacf0d
+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
aacf0d
+{
aacf0d
+    /* If the field pub_key in dh is NULL, the corresponding input
aacf0d
+     * parameters MUST be non-NULL.  The priv_key field may
aacf0d
+     * be left NULL.
aacf0d
+     */
aacf0d
+    if (dh->pub_key == NULL && pub_key == NULL)
aacf0d
+        return 0;
aacf0d
+
aacf0d
+    if (pub_key != NULL) {
aacf0d
+        BN_free(dh->pub_key);
aacf0d
+        dh->pub_key = pub_key;
aacf0d
+    }
aacf0d
+    if (priv_key != NULL) {
aacf0d
+        BN_free(dh->priv_key);
aacf0d
+        dh->priv_key = priv_key;
aacf0d
+    }
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int DH_set_length(DH *dh, long length)
aacf0d
+{
aacf0d
+    dh->length = length;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
aacf0d
+{
aacf0d
+    return ctx->iv;
aacf0d
+}
aacf0d
+
aacf0d
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
aacf0d
+{
aacf0d
+    return ctx->iv;
aacf0d
+}
aacf0d
+
aacf0d
+EVP_MD_CTX *EVP_MD_CTX_new(void)
aacf0d
+{
aacf0d
+    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
aacf0d
+}
aacf0d
+
aacf0d
+static void OPENSSL_clear_free(void *str, size_t num)
aacf0d
+{
aacf0d
+    if (str == NULL)
aacf0d
+        return;
aacf0d
+    if (num)
aacf0d
+        OPENSSL_cleanse(str, num);
aacf0d
+    OPENSSL_free(str);
aacf0d
+}
aacf0d
+
aacf0d
+/* This call frees resources associated with the context */
aacf0d
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
aacf0d
+{
aacf0d
+    if (ctx == NULL)
aacf0d
+        return 1;
aacf0d
+
aacf0d
+    /*
aacf0d
+     * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
aacf0d
+     * sometimes only copies of the context are ever finalised.
aacf0d
+     */
aacf0d
+    if (ctx->digest && ctx->digest->cleanup
aacf0d
+        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
aacf0d
+        ctx->digest->cleanup(ctx);
aacf0d
+    if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
aacf0d
+        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
aacf0d
+        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
aacf0d
+    }
aacf0d
+    EVP_PKEY_CTX_free(ctx->pctx);
aacf0d
+#ifndef OPENSSL_NO_ENGINE
aacf0d
+    ENGINE_finish(ctx->engine);
aacf0d
+#endif
aacf0d
+    OPENSSL_cleanse(ctx, sizeof(*ctx));
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
aacf0d
+{
aacf0d
+    EVP_MD_CTX_reset(ctx);
aacf0d
+    OPENSSL_free(ctx);
aacf0d
+}
aacf0d
+
aacf0d
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
aacf0d
+{
aacf0d
+    RSA_METHOD *ret;
aacf0d
+
aacf0d
+    ret = OPENSSL_malloc(sizeof(RSA_METHOD));
aacf0d
+
aacf0d
+    if (ret != NULL) {
aacf0d
+        memcpy(ret, meth, sizeof(*meth));
aacf0d
+        ret->name = OPENSSL_strdup(meth->name);
aacf0d
+        if (ret->name == NULL) {
aacf0d
+            OPENSSL_free(ret);
aacf0d
+            return NULL;
aacf0d
+        }
aacf0d
+    }
aacf0d
+
aacf0d
+    return ret;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
aacf0d
+{
aacf0d
+    char *tmpname;
aacf0d
+
aacf0d
+    tmpname = OPENSSL_strdup(name);
aacf0d
+    if (tmpname == NULL) {
aacf0d
+        return 0;
aacf0d
+    }
aacf0d
+
aacf0d
+    OPENSSL_free((char *)meth->name);
aacf0d
+    meth->name = tmpname;
aacf0d
+
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set_priv_enc(RSA_METHOD *meth,
aacf0d
+                          int (*priv_enc) (int flen, const unsigned char *from,
aacf0d
+                                           unsigned char *to, RSA *rsa,
aacf0d
+                                           int padding))
aacf0d
+{
aacf0d
+    meth->rsa_priv_enc = priv_enc;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set_priv_dec(RSA_METHOD *meth,
aacf0d
+                          int (*priv_dec) (int flen, const unsigned char *from,
aacf0d
+                                           unsigned char *to, RSA *rsa,
aacf0d
+                                           int padding))
aacf0d
+{
aacf0d
+    meth->rsa_priv_dec = priv_dec;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
aacf0d
+{
aacf0d
+    meth->finish = finish;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+void RSA_meth_free(RSA_METHOD *meth)
aacf0d
+{
aacf0d
+    if (meth != NULL) {
aacf0d
+        OPENSSL_free((char *)meth->name);
aacf0d
+        OPENSSL_free(meth);
aacf0d
+    }
aacf0d
+}
aacf0d
+
aacf0d
+int RSA_bits(const RSA *r)
aacf0d
+{
aacf0d
+    return (BN_num_bits(r->n));
aacf0d
+}
aacf0d
+
aacf0d
+int DSA_bits(const DSA *dsa)
aacf0d
+{
aacf0d
+    return BN_num_bits(dsa->p);
aacf0d
+}
aacf0d
+
aacf0d
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
aacf0d
+{
aacf0d
+    if (pkey->type != EVP_PKEY_RSA) {
aacf0d
+        return NULL;
aacf0d
+    }
aacf0d
+    return pkey->pkey.rsa;
aacf0d
+}
aacf0d
+
aacf0d
+EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
aacf0d
+{
aacf0d
+    EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
aacf0d
+
aacf0d
+    if (cipher != NULL) {
aacf0d
+        cipher->nid = cipher_type;
aacf0d
+        cipher->block_size = block_size;
aacf0d
+        cipher->key_len = key_len;
aacf0d
+    }
aacf0d
+    return cipher;
aacf0d
+}
aacf0d
+
aacf0d
+void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
aacf0d
+{
aacf0d
+    OPENSSL_free(cipher);
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
aacf0d
+{
aacf0d
+    cipher->iv_len = iv_len;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
aacf0d
+{
aacf0d
+    cipher->flags = flags;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
aacf0d
+                             int (*init) (EVP_CIPHER_CTX *ctx,
aacf0d
+                                          const unsigned char *key,
aacf0d
+                                          const unsigned char *iv,
aacf0d
+                                          int enc))
aacf0d
+{
aacf0d
+    cipher->init = init;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
aacf0d
+                                  int (*do_cipher) (EVP_CIPHER_CTX *ctx,
aacf0d
+                                                    unsigned char *out,
aacf0d
+                                                    const unsigned char *in,
aacf0d
+                                                    size_t inl))
aacf0d
+{
aacf0d
+    cipher->do_cipher = do_cipher;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
aacf0d
+                                int (*cleanup) (EVP_CIPHER_CTX *))
aacf0d
+{
aacf0d
+    cipher->cleanup = cleanup;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
aacf0d
+                             int (*ctrl) (EVP_CIPHER_CTX *, int type,
aacf0d
+                                          int arg, void *ptr))
aacf0d
+{
aacf0d
+    cipher->ctrl = ctrl;
aacf0d
+    return 1;
aacf0d
+}
aacf0d
+
aacf0d
+int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
aacf0d
+                                                          const unsigned char *key,
aacf0d
+                                                          const unsigned char *iv,
aacf0d
+                                                          int enc)
aacf0d
+{
aacf0d
+    return cipher->init;
aacf0d
+}
aacf0d
+
aacf0d
+int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
aacf0d
+                                                               unsigned char *out,
aacf0d
+                                                               const unsigned char *in,
aacf0d
+                                                               size_t inl)
aacf0d
+{
aacf0d
+    return cipher->do_cipher;
aacf0d
+}
aacf0d
+
aacf0d
+int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
aacf0d
+{
aacf0d
+    return cipher->cleanup;
aacf0d
+}
aacf0d
+
aacf0d
+int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
aacf0d
+                                                          int type, int arg,
aacf0d
+                                                          void *ptr)
aacf0d
+{
aacf0d
+    return cipher->ctrl;
aacf0d
+}
aacf0d
+
aacf0d
+int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
aacf0d
+{
aacf0d
+    return ctx->encrypt;
aacf0d
+}
aacf0d
+
aacf0d
+#endif /* OPENSSL_VERSION_NUMBER */
6cf9b8
diff -up openssh-7.4p1/libcrypto-compat.h.openssl openssh-7.4p1/libcrypto-compat.h
6cf9b8
--- openssh-7.4p1/libcrypto-compat.h.openssl	2016-12-23 17:47:36.432817753 +0100
6cf9b8
+++ openssh-7.4p1/libcrypto-compat.h	2016-12-23 17:47:36.432817753 +0100
aacf0d
@@ -0,0 +1,98 @@
aacf0d
+#ifndef LIBCRYPTO_COMPAT_H
aacf0d
+#define LIBCRYPTO_COMPAT_H
aacf0d
+
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
+
aacf0d
+#include <openssl/rsa.h>
aacf0d
+#include <openssl/dsa.h>
aacf0d
+#include <openssl/ecdsa.h>
aacf0d
+#include <openssl/dh.h>
aacf0d
+#include <openssl/evp.h>
aacf0d
+
aacf0d
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
aacf0d
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
aacf0d
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
aacf0d
+void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
aacf0d
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
aacf0d
+void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp);
aacf0d
+
aacf0d
+void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
aacf0d
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
aacf0d
+void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key);
aacf0d
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
aacf0d
+
aacf0d
+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
aacf0d
+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
aacf0d
+
aacf0d
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
aacf0d
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
aacf0d
+
aacf0d
+void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
aacf0d
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
aacf0d
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
aacf0d
+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
aacf0d
+int DH_set_length(DH *dh, long length);
aacf0d
+
aacf0d
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx);
aacf0d
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx);
aacf0d
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
aacf0d
+EVP_MD_CTX *EVP_MD_CTX_new(void);
aacf0d
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
aacf0d
+#define EVP_CIPHER_impl_ctx_size(e) e->ctx_size
aacf0d
+#define EVP_CIPHER_CTX_get_cipher_data(ctx) ctx->cipher_data
aacf0d
+
aacf0d
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
aacf0d
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
aacf0d
+#define RSA_meth_get_finish(meth) meth->finish
aacf0d
+int RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
aacf0d
+int RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec) (int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding));
aacf0d
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa));
aacf0d
+void RSA_meth_free(RSA_METHOD *meth);
aacf0d
+
aacf0d
+int RSA_bits(const RSA *r);
aacf0d
+int DSA_bits(const DSA *d);
aacf0d
+
aacf0d
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
aacf0d
+
aacf0d
+EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len);
aacf0d
+void EVP_CIPHER_meth_free(EVP_CIPHER *cipher);
aacf0d
+
aacf0d
+int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len);
aacf0d
+int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags);
aacf0d
+int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
aacf0d
+                             int (*init) (EVP_CIPHER_CTX *ctx,
aacf0d
+                                          const unsigned char *key,
aacf0d
+                                          const unsigned char *iv,
aacf0d
+                                          int enc));
aacf0d
+int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
aacf0d
+                                  int (*do_cipher) (EVP_CIPHER_CTX *ctx,
aacf0d
+                                                    unsigned char *out,
aacf0d
+                                                    const unsigned char *in,
aacf0d
+                                                    size_t inl));
aacf0d
+int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
aacf0d
+                                int (*cleanup) (EVP_CIPHER_CTX *));
aacf0d
+int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
aacf0d
+                             int (*ctrl) (EVP_CIPHER_CTX *, int type,
aacf0d
+                                          int arg, void *ptr));
aacf0d
+
aacf0d
+int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
aacf0d
+                                                          const unsigned char *key,
aacf0d
+                                                          const unsigned char *iv,
aacf0d
+                                                          int enc);
aacf0d
+int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
aacf0d
+                                                               unsigned char *out,
aacf0d
+                                                               const unsigned char *in,
aacf0d
+                                                               size_t inl);
aacf0d
+int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *);
aacf0d
+int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
aacf0d
+                                                          int type, int arg,
aacf0d
+                                                          void *ptr);
aacf0d
+
aacf0d
+#define EVP_CIPHER_CTX_reset(c)      EVP_CIPHER_CTX_init(c)
aacf0d
+
aacf0d
+int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx);
aacf0d
+
aacf0d
+#endif /* OPENSSL_VERSION_NUMBER */
aacf0d
+
aacf0d
+#endif /* LIBCRYPTO_COMPAT_H */
aacf0d
+
6cf9b8
diff -up openssh-7.4p1/Makefile.in.openssl openssh-7.4p1/Makefile.in
6cf9b8
--- openssh-7.4p1/Makefile.in.openssl	2016-12-23 17:47:36.420817748 +0100
6cf9b8
+++ openssh-7.4p1/Makefile.in	2016-12-23 17:47:36.432817753 +0100
aacf0d
@@ -100,7 +100,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
aacf0d
 	kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
aacf0d
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
aacf0d
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
aacf0d
-	platform-pledge.o platform-tracing.o auditstub.o
aacf0d
+	platform-pledge.o platform-tracing.o auditstub.o libcrypto-compat.o
aacf0d
 
aacf0d
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
aacf0d
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o
6cf9b8
diff -up openssh-7.4p1/monitor.c.openssl openssh-7.4p1/monitor.c
6cf9b8
--- openssh-7.4p1/monitor.c.openssl	2016-12-23 17:47:36.426817750 +0100
6cf9b8
+++ openssh-7.4p1/monitor.c	2016-12-23 17:47:36.433817753 +0100
6cf9b8
@@ -636,9 +636,12 @@ mm_answer_moduli(int sock, Buffer *m)
aacf0d
 		return (0);
aacf0d
 	} else {
aacf0d
 		/* Send first bignum */
aacf0d
+		const BIGNUM *p, *g;
aacf0d
+
aacf0d
+		DH_get0_pqg(dh, &p, NULL, &g);
aacf0d
 		buffer_put_char(m, 1);
aacf0d
-		buffer_put_bignum2(m, dh->p);
aacf0d
-		buffer_put_bignum2(m, dh->g);
aacf0d
+		buffer_put_bignum2(m, p);
aacf0d
+		buffer_put_bignum2(m, g);
aacf0d
 
aacf0d
 		DH_free(dh);
aacf0d
 	}
6cf9b8
diff -up openssh-7.4p1/openbsd-compat/openssl-compat.c.openssl openssh-7.4p1/openbsd-compat/openssl-compat.c
6cf9b8
--- openssh-7.4p1/openbsd-compat/openssl-compat.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/openbsd-compat/openssl-compat.c	2016-12-23 17:47:36.433817753 +0100
6cf9b8
@@ -70,12 +70,19 @@ ssh_compatible_openssl(long headerver, l
aacf0d
 void
aacf0d
 ssh_OpenSSL_add_all_algorithms(void)
aacf0d
 {
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
aacf0d
 	OpenSSL_add_all_algorithms();
aacf0d
 
aacf0d
 	/* Enable use of crypto hardware */
aacf0d
 	ENGINE_load_builtin_engines();
aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10001000L
aacf0d
 	ENGINE_register_all_complete();
aacf0d
+#endif
aacf0d
 	OPENSSL_config(NULL);
aacf0d
+#else
aacf0d
+	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS |
aacf0d
+	    OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, NULL);
aacf0d
+#endif
aacf0d
 }
aacf0d
 #endif
aacf0d
 
bdb932
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/configure.ac.openssl openssh-7.4p1/pam_ssh_agent_auth-0.10.3/configure.ac
bdb932
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/configure.ac.openssl	2014-03-31 19:35:17.000000000 +0200
bdb932
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/configure.ac	2016-12-23 17:47:36.433817753 +0100
aacf0d
@@ -1829,6 +1829,7 @@ AC_RUN_IFELSE(
aacf0d
 	[AC_LANG_SOURCE([[
aacf0d
 #include <string.h>
aacf0d
 #include <openssl/opensslv.h>
aacf0d
+#include <openssl/crypto.h>
aacf0d
 int main(void) { exit(SSLeay() == OPENSSL_VERSION_NUMBER ? 0 : 1); }
aacf0d
 	]])],
aacf0d
 	[
6cf9b8
diff -up openssh-7.4p1/regress/unittests/sshkey/test_file.c.openssl openssh-7.4p1/regress/unittests/sshkey/test_file.c
6cf9b8
--- openssh-7.4p1/regress/unittests/sshkey/test_file.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/regress/unittests/sshkey/test_file.c	2016-12-23 17:47:36.433817753 +0100
aacf0d
@@ -46,6 +46,7 @@ sshkey_file_tests(void)
aacf0d
 	struct sshbuf *buf, *pw;
aacf0d
 	BIGNUM *a, *b, *c;
aacf0d
 	char *cp;
aacf0d
+	const BIGNUM *n, *p, *q, *g, *pub_key, *priv_key;
aacf0d
 
aacf0d
 	TEST_START("load passphrase");
aacf0d
 	pw = load_text_file("pw");
aacf0d
@@ -58,7 +59,8 @@ sshkey_file_tests(void)
aacf0d
 	sshbuf_free(buf);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	a = load_bignum("rsa1_1.param.n");
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->n, a);
aacf0d
+	RSA_get0_key(k1->rsa, &n, NULL, NULL);
aacf0d
+	ASSERT_BIGNUM_EQ(n, a);
aacf0d
 	BN_free(a);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
@@ -109,9 +111,11 @@ sshkey_file_tests(void)
aacf0d
 	a = load_bignum("rsa_1.param.n");
aacf0d
 	b = load_bignum("rsa_1.param.p");
aacf0d
 	c = load_bignum("rsa_1.param.q");
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->n, a);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->p, b);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->q, c);
aacf0d
+	RSA_get0_key(k1->rsa, &n, NULL, NULL);
aacf0d
+	RSA_get0_factors(k1->rsa, &p, &q);
aacf0d
+	ASSERT_BIGNUM_EQ(n, a);
aacf0d
+	ASSERT_BIGNUM_EQ(p, b);
aacf0d
+	ASSERT_BIGNUM_EQ(q, c);
aacf0d
 	BN_free(a);
aacf0d
 	BN_free(b);
aacf0d
 	BN_free(c);
aacf0d
@@ -200,9 +204,11 @@ sshkey_file_tests(void)
aacf0d
 	a = load_bignum("dsa_1.param.g");
aacf0d
 	b = load_bignum("dsa_1.param.priv");
aacf0d
 	c = load_bignum("dsa_1.param.pub");
aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->g, a);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
aacf0d
+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
aacf0d
+	DSA_get0_key(k1->dsa, &pub_key, &priv_key);
aacf0d
+	ASSERT_BIGNUM_EQ(g, a);
aacf0d
+	ASSERT_BIGNUM_EQ(priv_key, b);
aacf0d
+	ASSERT_BIGNUM_EQ(pub_key, c);
aacf0d
 	BN_free(a);
aacf0d
 	BN_free(b);
aacf0d
 	BN_free(c);
6cf9b8
diff -up openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c.openssl openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c
6cf9b8
--- openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c	2016-12-23 17:47:36.433817753 +0100
aacf0d
@@ -197,9 +197,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new(KEY_RSA1);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
@@ -207,9 +204,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new(KEY_RSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
@@ -217,8 +211,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new(KEY_DSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
@@ -244,9 +236,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new_private(KEY_RSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->p, NULL);
aacf0d
 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
@@ -255,8 +244,6 @@ sshkey_tests(void)
aacf0d
 	k1 = sshkey_new_private(KEY_DSA);
aacf0d
 	ASSERT_PTR_NE(k1, NULL);
aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
aacf0d
 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
aacf0d
 	sshkey_free(k1);
aacf0d
 	TEST_DONE();
aacf0d
@@ -295,18 +282,13 @@ sshkey_tests(void)
aacf0d
 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
aacf0d
 	ASSERT_PTR_NE(kr, NULL);
aacf0d
 	ASSERT_PTR_NE(kr->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(kr->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(kr->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_NE(kr->rsa->p, NULL);
aacf0d
-	ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
aacf0d
+	ASSERT_INT_EQ(RSA_bits(kr->rsa), 1024);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 	TEST_START("generate KEY_DSA");
aacf0d
 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
aacf0d
 	ASSERT_PTR_NE(kd, NULL);
aacf0d
 	ASSERT_PTR_NE(kd->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(kd->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 #ifdef OPENSSL_HAS_ECC
aacf0d
@@ -333,9 +315,6 @@ sshkey_tests(void)
aacf0d
 	ASSERT_PTR_NE(kr, k1);
aacf0d
 	ASSERT_INT_EQ(k1->type, KEY_RSA);
aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 	TEST_START("equal KEY_RSA/demoted KEY_RSA");
aacf0d
@@ -349,8 +328,6 @@ sshkey_tests(void)
aacf0d
 	ASSERT_PTR_NE(kd, k1);
aacf0d
 	ASSERT_INT_EQ(k1->type, KEY_DSA);
aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
aacf0d
-	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
aacf0d
 	TEST_DONE();
aacf0d
 
aacf0d
 	TEST_START("equal KEY_DSA/demoted KEY_DSA");
6cf9b8
diff -up openssh-7.4p1/rsa.c.openssl openssh-7.4p1/rsa.c
6cf9b8
--- openssh-7.4p1/rsa.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/rsa.c	2016-12-23 17:47:36.434817754 +0100
aacf0d
@@ -76,11 +76,14 @@ rsa_public_encrypt(BIGNUM *out, BIGNUM *
aacf0d
 {
aacf0d
 	u_char *inbuf = NULL, *outbuf = NULL;
aacf0d
 	int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR;
aacf0d
+	const BIGNUM *e, *n;
aacf0d
 
aacf0d
-	if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
aacf0d
+	RSA_get0_key(key, &n, &e, NULL);
aacf0d
+
aacf0d
+	if (BN_num_bits(e) < 2 || !BN_is_odd(e))
aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
aacf0d
 
aacf0d
-	olen = BN_num_bytes(key->n);
aacf0d
+	olen = BN_num_bytes(n);
aacf0d
 	if ((outbuf = malloc(olen)) == NULL) {
aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
aacf0d
 		goto out;
aacf0d
@@ -122,8 +125,11 @@ rsa_private_decrypt(BIGNUM *out, BIGNUM
aacf0d
 {
aacf0d
 	u_char *inbuf = NULL, *outbuf = NULL;
aacf0d
 	int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR;
aacf0d
+	const BIGNUM *n;
aacf0d
+
aacf0d
+	RSA_get0_key(key, &n, NULL, NULL);
aacf0d
 
aacf0d
-	olen = BN_num_bytes(key->n);
aacf0d
+	olen = BN_num_bytes(n);
aacf0d
 	if ((outbuf = malloc(olen)) == NULL) {
aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
aacf0d
 		goto out;
aacf0d
@@ -157,31 +163,42 @@ rsa_private_decrypt(BIGNUM *out, BIGNUM
aacf0d
 	return r;
aacf0d
 }
aacf0d
 
aacf0d
-/* calculate p-1 and q-1 */
aacf0d
+/* calculate d mod p-1 and d mod q-1 */
aacf0d
 int
aacf0d
-rsa_generate_additional_parameters(RSA *rsa)
aacf0d
+rsa_generate_additional_parameters(RSA *rsa, BIGNUM *iqmp)
aacf0d
 {
aacf0d
 	BIGNUM *aux = NULL;
aacf0d
 	BN_CTX *ctx = NULL;
aacf0d
 	int r;
aacf0d
+	const BIGNUM *p, *q, *d;
aacf0d
+	BIGNUM *dmp1 = NULL, *dmq1 = NULL;
aacf0d
+
aacf0d
+	RSA_get0_factors(rsa, &p, &q);
aacf0d
+	RSA_get0_key(rsa, NULL, NULL, &d);
aacf0d
 
aacf0d
-	if ((ctx = BN_CTX_new()) == NULL)
aacf0d
-		return SSH_ERR_ALLOC_FAIL;
aacf0d
-	if ((aux = BN_new()) == NULL) {
aacf0d
+	if ((ctx = BN_CTX_new()) == NULL ||
aacf0d
+	    (aux = BN_new()) == NULL ||
aacf0d
+	    (dmp1 = BN_new()) == NULL ||
aacf0d
+	    (dmq1 = BN_new()) == NULL) {
aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
 
aacf0d
-	if ((BN_sub(aux, rsa->q, BN_value_one()) == 0) ||
aacf0d
-	    (BN_mod(rsa->dmq1, rsa->d, aux, ctx) == 0) ||
aacf0d
-	    (BN_sub(aux, rsa->p, BN_value_one()) == 0) ||
aacf0d
-	    (BN_mod(rsa->dmp1, rsa->d, aux, ctx) == 0)) {
aacf0d
+	if ((BN_sub(aux, q, BN_value_one()) == 0) ||
aacf0d
+	    (BN_mod(dmq1, d, aux, ctx) == 0) ||
aacf0d
+	    (BN_sub(aux, p, BN_value_one()) == 0) ||
aacf0d
+	    (BN_mod(dmp1, d, aux, ctx) == 0) ||
aacf0d
+	    (RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp) == 0)) {
aacf0d
 		r = SSH_ERR_LIBCRYPTO_ERROR;
aacf0d
 		goto out;
aacf0d
 	}
aacf0d
+	dmp1 = NULL;
aacf0d
+	dmq1 = NULL;
aacf0d
 	r = 0;
aacf0d
  out:
aacf0d
 	BN_clear_free(aux);
aacf0d
+	BN_clear_free(dmp1);
aacf0d
+	BN_clear_free(dmq1);
aacf0d
 	BN_CTX_free(ctx);
aacf0d
 	return r;
aacf0d
 }
6cf9b8
diff -up openssh-7.4p1/rsa.h.openssl openssh-7.4p1/rsa.h
6cf9b8
--- openssh-7.4p1/rsa.h.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/rsa.h	2016-12-23 17:47:36.434817754 +0100
aacf0d
@@ -21,6 +21,6 @@
aacf0d
 
aacf0d
 int	 rsa_public_encrypt(BIGNUM *, BIGNUM *, RSA *);
aacf0d
 int	 rsa_private_decrypt(BIGNUM *, BIGNUM *, RSA *);
aacf0d
-int	 rsa_generate_additional_parameters(RSA *);
aacf0d
+int	 rsa_generate_additional_parameters(RSA *, BIGNUM *);
aacf0d
 
aacf0d
 #endif				/* RSA_H */
6cf9b8
diff -up openssh-7.4p1/ssh-agent.c.openssl openssh-7.4p1/ssh-agent.c
6cf9b8
--- openssh-7.4p1/ssh-agent.c.openssl	2016-12-23 17:47:36.428817751 +0100
6cf9b8
+++ openssh-7.4p1/ssh-agent.c	2016-12-23 17:47:36.434817754 +0100
6cf9b8
@@ -258,12 +258,12 @@ process_request_identities(SocketEntry *
aacf0d
 	TAILQ_FOREACH(id, &tab->idlist, next) {
aacf0d
 		if (id->key->type == KEY_RSA1) {
aacf0d
 #ifdef WITH_SSH1
aacf0d
+			const BIGNUM *r_n, *r_e;
aacf0d
+			RSA_get0_key(id->key->rsa, &r_n, &r_e, NULL);
aacf0d
 			if ((r = sshbuf_put_u32(msg,
aacf0d
-			    BN_num_bits(id->key->rsa->n))) != 0 ||
aacf0d
-			    (r = sshbuf_put_bignum1(msg,
aacf0d
-			    id->key->rsa->e)) != 0 ||
aacf0d
-			    (r = sshbuf_put_bignum1(msg,
aacf0d
-			    id->key->rsa->n)) != 0)
aacf0d
+			    BN_num_bits(r_n))) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum1(msg, r_e)) != 0 ||
aacf0d
+			    (r = sshbuf_put_bignum1(msg, r_n)) != 0)
aacf0d
 				fatal("%s: buffer error: %s",
aacf0d
 				    __func__, ssh_err(r));
aacf0d
 #endif
6cf9b8
@@ -302,6 +302,7 @@ process_authentication_challenge1(Socket
aacf0d
 	struct sshbuf *msg;
aacf0d
 	struct ssh_digest_ctx *md;
aacf0d
 	struct sshkey *key;
aacf0d
+	BIGNUM *r_n = NULL, *r_e = NULL;
aacf0d
 
aacf0d
 	if ((msg = sshbuf_new()) == NULL)
aacf0d
 		fatal("%s: sshbuf_new failed", __func__);
6cf9b8
@@ -310,11 +311,16 @@ process_authentication_challenge1(Socket
aacf0d
 	if ((challenge = BN_new()) == NULL)
aacf0d
 		fatal("%s: BN_new failed", __func__);
aacf0d
 
aacf0d
-	if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
aacf0d
-	    (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(e->request, challenge)))
aacf0d
+	if ((r_n = BN_new()) == NULL || (r_e = BN_new()) == NULL ||
aacf0d
+	    (r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
aacf0d
+	    (r = sshbuf_get_bignum1(e->request, r_e)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(e->request, r_n)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(e->request, challenge)) ||
aacf0d
+	    RSA_set0_key(key->rsa, r_n, r_e, NULL) == 0) {
aacf0d
+		BN_free(r_n);
aacf0d
+		BN_free(r_e);
aacf0d
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
aacf0d
+	}
aacf0d
 
aacf0d
 	/* Only protocol 1.1 is supported */
aacf0d
 	if (sshbuf_len(e->request) == 0)
6cf9b8
@@ -450,6 +456,7 @@ process_remove_identity(SocketEntry *e,
aacf0d
 	u_char *blob;
aacf0d
 #ifdef WITH_SSH1
aacf0d
 	u_int bits;
aacf0d
+	BIGNUM *r_n = NULL, *r_e = NULL;
aacf0d
 #endif /* WITH_SSH1 */
aacf0d
 
aacf0d
 	switch (version) {
6cf9b8
@@ -459,10 +466,15 @@ process_remove_identity(SocketEntry *e,
aacf0d
 			error("%s: sshkey_new failed", __func__);
aacf0d
 			return;
aacf0d
 		}
aacf0d
-		if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
aacf0d
-		    (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
aacf0d
-		    (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
aacf0d
+		if ((r_n = BN_new()) == NULL || (r_e = BN_new()) == NULL ||
aacf0d
+		    (r = sshbuf_get_u32(e->request, &bits)) != 0 ||
aacf0d
+		    (r = sshbuf_get_bignum1(e->request, r_e)) != 0 ||
aacf0d
+		    (r = sshbuf_get_bignum1(e->request, r_n)) != 0 ||
aacf0d
+		    RSA_set0_key(key->rsa, r_n, r_e, NULL) == 0) {
aacf0d
+			BN_free(r_n);
aacf0d
+			BN_free(r_e);
aacf0d
 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
aacf0d
+		}
aacf0d
 
aacf0d
 		if (bits != sshkey_size(key))
aacf0d
 			logit("Warning: identity keysize mismatch: "
6cf9b8
@@ -565,23 +577,38 @@ agent_decode_rsa1(struct sshbuf *m, stru
aacf0d
 {
aacf0d
 	struct sshkey *k = NULL;
aacf0d
 	int r = SSH_ERR_INTERNAL_ERROR;
aacf0d
+	BIGNUM *n = NULL, *e = NULL, *d = NULL,
aacf0d
+	    *iqmp = NULL, *q = NULL, *p = NULL;
aacf0d
 
aacf0d
 	*kp = NULL;
aacf0d
 	if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
aacf0d
 		return SSH_ERR_ALLOC_FAIL;
aacf0d
 
aacf0d
-	if ((r = sshbuf_get_u32(m, NULL)) != 0 ||		/* ignored */
aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
aacf0d
+	if ((n = BN_new()) == NULL || (e = BN_new()) == NULL ||
aacf0d
+	    (d = BN_new()) == NULL || (iqmp = BN_new()) == NULL ||
aacf0d
+	    (q = BN_new()) == NULL || (p = BN_new()) == NULL ||
aacf0d
+	    (r = sshbuf_get_u32(m, NULL)) != 0 ||		/* ignored */
aacf0d
+	    (r = sshbuf_get_bignum1(m, n)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(m, e)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(m, d)) != 0 ||
aacf0d
+	    (r = sshbuf_get_bignum1(m, iqmp)) != 0 ||
aacf0d
 	    /* SSH1 and SSL have p and q swapped */
aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 ||	/* p */
aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) 	/* q */
aacf0d
+	    (r = sshbuf_get_bignum1(m, q)) != 0 ||	/* p */
aacf0d
+	    (r = sshbuf_get_bignum1(m, p)) != 0 || 	/* q */
aacf0d
+	    RSA_set0_key(k->rsa, n, e, d) == 0 ||
aacf0d
+	    RSA_set0_factors(k->rsa, p, q) == 0 ||
aacf0d
+	    RSA_set0_crt_params(k->rsa, NULL, NULL, iqmp) == 0) {
aacf0d
+		BN_free(n);
aacf0d
+		BN_free(e);
aacf0d
+		BN_free(d);
aacf0d
+		BN_free(p);
aacf0d
+		BN_free(q);
aacf0d
+		BN_free(iqmp);
aacf0d
 		goto out;
aacf0d
+	}
aacf0d
 
aacf0d
 	/* Generate additional parameters */
aacf0d
-	if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
aacf0d
+	if ((r = rsa_generate_additional_parameters(k->rsa, NULL)) != 0)
aacf0d
 		goto out;
aacf0d
 	/* enable blinding */
aacf0d
 	if (RSA_blinding_on(k->rsa, NULL) != 1) {
6cf9b8
diff -up openssh-7.4p1/sshconnect1.c.openssl openssh-7.4p1/sshconnect1.c
6cf9b8
--- openssh-7.4p1/sshconnect1.c.openssl	2016-12-19 05:59:41.000000000 +0100
6cf9b8
+++ openssh-7.4p1/sshconnect1.c	2016-12-23 17:47:36.434817754 +0100
aacf0d
@@ -70,6 +70,7 @@ try_agent_authentication(void)
aacf0d
 	u_char response[16];
aacf0d
 	size_t i;
aacf0d
 	BIGNUM *challenge;
aacf0d
+	const BIGNUM *n;
aacf0d
 	struct ssh_identitylist *idlist = NULL;
aacf0d
 
aacf0d
 	/* Get connection to the agent. */
aacf0d
@@ -96,8 +97,9 @@ try_agent_authentication(void)
aacf0d
 		    idlist->comments[i]);
aacf0d
 
aacf0d
 		/* Tell the server that we are willing to authenticate using this key. */
aacf0d
+		RSA_get0_key(idlist->keys[i]->rsa, &n, NULL, NULL);