vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/authfd.c.openssl openssh-7.4p1/authfd.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/authfd.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/authfd.c	2016-12-23 17:47:36.429817751 +0100
Jakub Jelen aacf0d
@@ -207,15 +207,22 @@ deserialise_identity1(struct sshbuf *ids
Jakub Jelen aacf0d
 	int r, keybits;
Jakub Jelen aacf0d
 	u_int32_t bits;
Jakub Jelen aacf0d
 	char *comment = NULL;
Jakub Jelen aacf0d
+	BIGNUM *e = NULL, *n = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if ((key = sshkey_new(KEY_RSA1)) == NULL)
Jakub Jelen aacf0d
 		return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
-	if ((r = sshbuf_get_u32(ids, &bits)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(ids, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(ids, key->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
Jakub Jelen aacf0d
+	if ((e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (n = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_u32(ids, &bits)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(ids, e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(ids, n)) != 0 ||
Jakub Jelen aacf0d
+	    (RSA_set0_key(key->rsa, n, e, NULL) == 0) ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0) {
Jakub Jelen aacf0d
+		BN_free(n);
Jakub Jelen aacf0d
+		BN_free(e);
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
-	keybits = BN_num_bits(key->rsa->n);
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
+	keybits = BN_num_bits(n);
Jakub Jelen aacf0d
 	/* XXX previously we just warned here. I think we should be strict */
Jakub Jelen aacf0d
 	if (keybits < 0 || bits != (u_int)keybits) {
Jakub Jelen aacf0d
 		r = SSH_ERR_KEY_BITS_MISMATCH;
Jakub Jelen aacf0d
@@ -393,15 +400,17 @@ ssh_decrypt_challenge(int sock, struct s
Jakub Jelen aacf0d
 	struct sshbuf *msg;
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
 	u_char type;
Jakub Jelen aacf0d
+	const BIGNUM *e, *n;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (key->type != KEY_RSA1)
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 	if ((msg = sshbuf_new()) == NULL)
Jakub Jelen aacf0d
 		return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+	RSA_get0_key(key->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
 	if ((r = sshbuf_put_u8(msg, SSH_AGENTC_RSA_CHALLENGE)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_u32(msg, BN_num_bits(n))) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(msg, e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(msg, n)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_put_bignum1(msg, challenge)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_put(msg, session_id, 16)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_put_u32(msg, 1)) != 0) /* Response type for proto 1.1 */
Jakub Jelen aacf0d
@@ -499,15 +508,19 @@ static int
Jakub Jelen aacf0d
 ssh_encode_identity_rsa1(struct sshbuf *b, RSA *key, const char *comment)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
+	const BIGNUM *n, *e, *d, *q, *p, *iqmp;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
+	RSA_get0_key(key, &n, &e, &d);
Jakub Jelen aacf0d
+	RSA_get0_factors(key, &p, &q);
Jakub Jelen aacf0d
+	RSA_get0_crt_params(key, NULL, NULL, &iqmp);
Jakub Jelen aacf0d
 	/* To keep within the protocol: p < q for ssh. in SSL p > q */
Jakub Jelen aacf0d
-	if ((r = sshbuf_put_u32(b, BN_num_bits(key->n))) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(b, key->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(b, key->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(b, key->d)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(b, key->iqmp)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(b, key->q)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(b, key->p)) != 0 ||
Jakub Jelen aacf0d
+	if ((r = sshbuf_put_u32(b, BN_num_bits(n))) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(b, n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(b, e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(b, d)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(b, iqmp)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(b, q)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(b, p)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_put_cstring(b, comment)) != 0)
Jakub Jelen aacf0d
 		return r;
Jakub Jelen aacf0d
 	return 0;
Jakub Jelen aacf0d
@@ -622,11 +635,13 @@ ssh_remove_identity(int sock, struct ssh
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen aacf0d
 	if (key->type == KEY_RSA1) {
Jakub Jelen aacf0d
+		const BIGNUM *e, *n;
Jakub Jelen aacf0d
+		RSA_get0_key(key->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
 		if ((r = sshbuf_put_u8(msg,
Jakub Jelen aacf0d
 		    SSH_AGENTC_REMOVE_RSA_IDENTITY)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0)
Jakub Jelen aacf0d
+		    (r = sshbuf_put_u32(msg, BN_num_bits(n))) != 0 ||
Jakub Jelen aacf0d
+		    (r = sshbuf_put_bignum1(msg, e)) != 0 ||
Jakub Jelen aacf0d
+		    (r = sshbuf_put_bignum1(msg, n)) != 0)
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 	} else
Jakub Jelen aacf0d
 #endif
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/auth-pam.c.openssl openssh-7.4p1/auth-pam.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/auth-pam.c.openssl	2016-12-23 17:47:36.400817739 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/auth-pam.c	2016-12-23 17:47:36.430817752 +0100
Jakub Jelen 6cf9b8
@@ -129,6 +129,10 @@ extern u_int utmp_len;
Jakub Jelen aacf0d
 typedef pthread_t sp_pthread_t;
Jakub Jelen aacf0d
 #else
Jakub Jelen aacf0d
 typedef pid_t sp_pthread_t;
Jakub Jelen aacf0d
+# define pthread_create(a, b, c, d)	_ssh_compat_pthread_create(a, b, c, d)
Jakub Jelen aacf0d
+# define pthread_exit(a)		_ssh_compat_pthread_exit(a)
Jakub Jelen aacf0d
+# define pthread_cancel(a)		_ssh_compat_pthread_cancel(a)
Jakub Jelen aacf0d
+# define pthread_join(a, b)		_ssh_compat_pthread_join(a, b)
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 struct pam_ctxt {
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/cipher-3des1.c.openssl openssh-7.4p1/cipher-3des1.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/cipher-3des1.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/cipher-3des1.c	2016-12-23 17:47:36.430817752 +0100
Jakub Jelen 6cf9b8
@@ -44,7 +44,7 @@
Jakub Jelen aacf0d
  */
Jakub Jelen aacf0d
 struct ssh1_3des_ctx
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
-	EVP_CIPHER_CTX	k1, k2, k3;
Jakub Jelen aacf0d
+	EVP_CIPHER_CTX	*k1, *k2, *k3;
Jakub Jelen aacf0d
 };
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 const EVP_CIPHER * evp_ssh1_3des(void);
Jakub Jelen 6cf9b8
@@ -65,7 +65,7 @@ ssh1_3des_init(EVP_CIPHER_CTX *ctx, cons
Jakub Jelen aacf0d
 	if (key == NULL)
Jakub Jelen aacf0d
 		return 1;
Jakub Jelen aacf0d
 	if (enc == -1)
Jakub Jelen aacf0d
-		enc = ctx->encrypt;
Jakub Jelen aacf0d
+		enc = EVP_CIPHER_CTX_encrypting(ctx);
Jakub Jelen aacf0d
 	k1 = k2 = k3 = (u_char *) key;
Jakub Jelen aacf0d
 	k2 += 8;
Jakub Jelen aacf0d
 	if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
Jakub Jelen 6cf9b8
@@ -74,12 +74,19 @@ ssh1_3des_init(EVP_CIPHER_CTX *ctx, cons
Jakub Jelen aacf0d
 		else
Jakub Jelen aacf0d
 			k1 += 16;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	EVP_CIPHER_CTX_init(&c->k1);
Jakub Jelen aacf0d
-	EVP_CIPHER_CTX_init(&c->k2);
Jakub Jelen aacf0d
-	EVP_CIPHER_CTX_init(&c->k3);
Jakub Jelen aacf0d
-	if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
Jakub Jelen aacf0d
-	    EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
Jakub Jelen aacf0d
-	    EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
Jakub Jelen aacf0d
+	c->k1 = EVP_CIPHER_CTX_new();
Jakub Jelen aacf0d
+	c->k2 = EVP_CIPHER_CTX_new();
Jakub Jelen aacf0d
+	c->k3 = EVP_CIPHER_CTX_new();
Jakub Jelen aacf0d
+	if (c->k1 == NULL || c->k2 == NULL || c->k3 == NULL) {
Jakub Jelen aacf0d
+		EVP_CIPHER_CTX_free(c->k1);
Jakub Jelen aacf0d
+		EVP_CIPHER_CTX_free(c->k2);
Jakub Jelen aacf0d
+		EVP_CIPHER_CTX_free(c->k3);
Jakub Jelen aacf0d
+		free(c);
Jakub Jelen aacf0d
+		return 0;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
+	if (EVP_CipherInit(c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
Jakub Jelen aacf0d
+	    EVP_CipherInit(c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
Jakub Jelen aacf0d
+	    EVP_CipherInit(c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
Jakub Jelen aacf0d
 		explicit_bzero(c, sizeof(*c));
Jakub Jelen aacf0d
 		free(c);
Jakub Jelen aacf0d
 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
Jakub Jelen 6cf9b8
@@ -95,9 +102,9 @@ ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_cha
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
Jakub Jelen aacf0d
 		return 0;
Jakub Jelen aacf0d
-	if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
Jakub Jelen aacf0d
-	    EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
Jakub Jelen aacf0d
-	    EVP_Cipher(&c->k3, dest, dest, len) == 0)
Jakub Jelen aacf0d
+	if (EVP_Cipher(c->k1, dest, (u_char *)src, len) == 0 ||
Jakub Jelen aacf0d
+	    EVP_Cipher(c->k2, dest, dest, len) == 0 ||
Jakub Jelen aacf0d
+	    EVP_Cipher(c->k3, dest, dest, len) == 0)
Jakub Jelen aacf0d
 		return 0;
Jakub Jelen aacf0d
 	return 1;
Jakub Jelen aacf0d
 }
Jakub Jelen 6cf9b8
@@ -108,9 +115,9 @@ ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
Jakub Jelen aacf0d
 	struct ssh1_3des_ctx *c;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
Jakub Jelen aacf0d
-		EVP_CIPHER_CTX_cleanup(&c->k1);
Jakub Jelen aacf0d
-		EVP_CIPHER_CTX_cleanup(&c->k2);
Jakub Jelen aacf0d
-		EVP_CIPHER_CTX_cleanup(&c->k3);
Jakub Jelen aacf0d
+		EVP_CIPHER_CTX_free(c->k1);
Jakub Jelen aacf0d
+		EVP_CIPHER_CTX_free(c->k2);
Jakub Jelen aacf0d
+		EVP_CIPHER_CTX_free(c->k3);
Jakub Jelen aacf0d
 		explicit_bzero(c, sizeof(*c));
Jakub Jelen aacf0d
 		free(c);
Jakub Jelen aacf0d
 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
Jakub Jelen 6cf9b8
@@ -128,13 +135,13 @@ ssh1_3des_iv(EVP_CIPHER_CTX *evp, int do
Jakub Jelen aacf0d
 	if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
Jakub Jelen aacf0d
 		return SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
 	if (doset) {
Jakub Jelen aacf0d
-		memcpy(c->k1.iv, iv, 8);
Jakub Jelen aacf0d
-		memcpy(c->k2.iv, iv + 8, 8);
Jakub Jelen aacf0d
-		memcpy(c->k3.iv, iv + 16, 8);
Jakub Jelen aacf0d
+		memcpy(EVP_CIPHER_CTX_iv_noconst(c->k1), iv, 8);
Jakub Jelen aacf0d
+		memcpy(EVP_CIPHER_CTX_iv_noconst(c->k2), iv + 8, 8);
Jakub Jelen aacf0d
+		memcpy(EVP_CIPHER_CTX_iv_noconst(c->k3), iv + 16, 8);
Jakub Jelen aacf0d
 	} else {
Jakub Jelen aacf0d
-		memcpy(iv, c->k1.iv, 8);
Jakub Jelen aacf0d
-		memcpy(iv + 8, c->k2.iv, 8);
Jakub Jelen aacf0d
-		memcpy(iv + 16, c->k3.iv, 8);
Jakub Jelen aacf0d
+		memcpy(iv, EVP_CIPHER_CTX_iv(c->k1), 8);
Jakub Jelen aacf0d
+		memcpy(iv + 8, EVP_CIPHER_CTX_iv(c->k2), 8);
Jakub Jelen aacf0d
+		memcpy(iv + 16, EVP_CIPHER_CTX_iv(c->k3), 8);
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	return 0;
Jakub Jelen aacf0d
 }
Jakub Jelen 6cf9b8
@@ -142,17 +149,14 @@ ssh1_3des_iv(EVP_CIPHER_CTX *evp, int do
Jakub Jelen aacf0d
 const EVP_CIPHER *
Jakub Jelen aacf0d
 evp_ssh1_3des(void)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
-	static EVP_CIPHER ssh1_3des;
Jakub Jelen aacf0d
+	EVP_CIPHER *ssh1_3des;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	memset(&ssh1_3des, 0, sizeof(ssh1_3des));
Jakub Jelen aacf0d
-	ssh1_3des.nid = NID_undef;
Jakub Jelen aacf0d
-	ssh1_3des.block_size = 8;
Jakub Jelen aacf0d
-	ssh1_3des.iv_len = 0;
Jakub Jelen aacf0d
-	ssh1_3des.key_len = 16;
Jakub Jelen aacf0d
-	ssh1_3des.init = ssh1_3des_init;
Jakub Jelen aacf0d
-	ssh1_3des.cleanup = ssh1_3des_cleanup;
Jakub Jelen aacf0d
-	ssh1_3des.do_cipher = ssh1_3des_cbc;
Jakub Jelen aacf0d
-	ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
Jakub Jelen aacf0d
-	return &ssh1_3des;
Jakub Jelen aacf0d
+	ssh1_3des = EVP_CIPHER_meth_new(NID_undef, 8, 16);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_iv_length(ssh1_3des, 0);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_init(ssh1_3des, ssh1_3des_init);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_cleanup(ssh1_3des, ssh1_3des_cleanup);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_do_cipher(ssh1_3des, ssh1_3des_cbc);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_flags(ssh1_3des, EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH);
Jakub Jelen aacf0d
+	return ssh1_3des;
Jakub Jelen aacf0d
 }
Jakub Jelen 6cf9b8
 #endif /* WITH_SSH1 */
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/cipher-bf1.c.openssl openssh-7.4p1/cipher-bf1.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/cipher-bf1.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/cipher-bf1.c	2016-12-23 17:47:36.430817752 +0100
Jakub Jelen 6cf9b8
@@ -89,17 +89,28 @@ bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_ch
Jakub Jelen aacf0d
 const EVP_CIPHER *
Jakub Jelen aacf0d
 evp_ssh1_bf(void)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
-	static EVP_CIPHER ssh1_bf;
Jakub Jelen aacf0d
+	EVP_CIPHER *ssh1_bf;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
Jakub Jelen aacf0d
-	orig_bf = ssh1_bf.do_cipher;
Jakub Jelen aacf0d
-	ssh1_bf.nid = NID_undef;
Jakub Jelen aacf0d
+	orig_bf = EVP_CIPHER_meth_get_do_cipher(EVP_bf_cbc());
Jakub Jelen aacf0d
+	/* block_size, length, flags from openssl/crypto/engine/eng_cryptodev.c:638 */
Jakub Jelen aacf0d
+	ssh1_bf = EVP_CIPHER_meth_new(NID_undef, 8, 32);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_iv_length(ssh1_bf, 8);
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_flags(ssh1_bf, EVP_CIPH_CBC_MODE);
Jakub Jelen aacf0d
 #ifdef SSH_OLD_EVP
Jakub Jelen aacf0d
-	ssh1_bf.init = bf_ssh1_init;
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_init(ssh1_bf, ssh1_bf_init);
Jakub Jelen aacf0d
+#else
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_init(ssh1_bf,
Jakub Jelen aacf0d
+	    EVP_CIPHER_meth_get_init(EVP_bf_cbc()));
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
-	ssh1_bf.do_cipher = bf_ssh1_cipher;
Jakub Jelen aacf0d
-	ssh1_bf.key_len = 32;
Jakub Jelen aacf0d
-	return (&ssh1_bf);
Jakub Jelen aacf0d
+	/* copy methods and parameters from old EVP_BF_cbc()
Jakub Jelen aacf0d
+	 * meth_dup does not allow to change type and key_len */
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_cleanup(ssh1_bf,
Jakub Jelen aacf0d
+	    EVP_CIPHER_meth_get_cleanup(EVP_bf_cbc()));
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_ctrl(ssh1_bf,
Jakub Jelen aacf0d
+	    EVP_CIPHER_meth_get_ctrl(EVP_bf_cbc()));
Jakub Jelen aacf0d
+	/* ASN1 params??? */
Jakub Jelen aacf0d
+	EVP_CIPHER_meth_set_do_cipher(ssh1_bf, bf_ssh1_cipher);
Jakub Jelen aacf0d
+	return ssh1_bf;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 #endif /* defined(WITH_OPENSSL) && !defined(OPENSSL_NO_BF) */
Jakub Jelen 6cf9b8
 
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/cipher.c.openssl openssh-7.4p1/cipher.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/cipher.c.openssl	2016-12-23 17:47:36.418817747 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/cipher.c	2016-12-23 17:47:36.430817752 +0100
Jakub Jelen 6cf9b8
@@ -368,7 +368,7 @@ cipher_init(struct sshcipher_ctx **ccp,
Jakub Jelen 6cf9b8
 		ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen 6cf9b8
 		goto out;
Jakub Jelen 6cf9b8
 	}
Jakub Jelen 6cf9b8
-	if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
Jakub Jelen aacf0d
+	if (EVP_CipherInit(cc->evp, type, (u_char *)key, (u_char *)iv,
Jakub Jelen aacf0d
 	    (do_encrypt == CIPHER_ENCRYPT)) == 0) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen 6cf9b8
 		goto out;
Jakub Jelen 6cf9b8
@@ -386,10 +386,6 @@ cipher_init(struct sshcipher_ctx **ccp,
Jakub Jelen 6cf9b8
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 	}
Jakub Jelen 6cf9b8
-	if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
Jakub Jelen aacf0d
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen 6cf9b8
-		goto out;
Jakub Jelen aacf0d
-	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (cipher->discard_len > 0) {
Jakub Jelen aacf0d
 		if ((junk = malloc(cipher->discard_len)) == NULL ||
Jakub Jelen 6cf9b8
@@ -621,7 +617,7 @@ cipher_get_keyiv(struct sshcipher_ctx *c
Jakub Jelen aacf0d
 			   len, iv))
Jakub Jelen aacf0d
 			       return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 		} else
Jakub Jelen 6cf9b8
-			memcpy(iv, cc->evp->iv, len);
Jakub Jelen aacf0d
+			memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen 6cf9b8
@@ -667,7 +663,7 @@ cipher_set_keyiv(struct sshcipher_ctx *c
Jakub Jelen aacf0d
 			    EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
Jakub Jelen aacf0d
 				return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 		} else
Jakub Jelen 6cf9b8
-			memcpy(cc->evp->iv, iv, evplen);
Jakub Jelen aacf0d
+			memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen);
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen 6cf9b8
@@ -681,8 +677,8 @@ cipher_set_keyiv(struct sshcipher_ctx *c
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen 6cf9b8
-#define EVP_X_STATE(evp)	(evp)->cipher_data
Jakub Jelen 6cf9b8
-#define EVP_X_STATE_LEN(evp)	(evp)->cipher->ctx_size
Jakub Jelen aacf0d
+#define EVP_X_STATE(evp)	EVP_CIPHER_CTX_get_cipher_data(evp)
Jakub Jelen aacf0d
+#define EVP_X_STATE_LEN(evp)	EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 int
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ctr-cavstest.c.openssl openssh-7.4p1/ctr-cavstest.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ctr-cavstest.c.openssl	2016-12-23 17:47:36.344817716 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ctr-cavstest.c	2016-12-23 17:47:36.430817752 +0100
Jakub Jelen aacf0d
@@ -144,7 +144,7 @@ int main (int argc, char *argv[])
Jakub Jelen aacf0d
                 usage();
Jakub Jelen aacf0d
         }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	SSLeay_add_all_algorithms();
Jakub Jelen aacf0d
+	OpenSSL_add_all_algorithms();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	c = cipher_by_name(algo);
Jakub Jelen aacf0d
 	if (c == NULL) {
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/dh.c.openssl openssh-7.4p1/dh.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/dh.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/dh.c	2016-12-23 17:47:36.430817752 +0100
Jakub Jelen 6cf9b8
@@ -212,14 +212,15 @@ choose_dh(int min, int wantbits, int max
Jakub Jelen aacf0d
 /* diffie-hellman-groupN-sha1 */
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 int
Jakub Jelen aacf0d
-dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
Jakub Jelen aacf0d
+dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	int i;
Jakub Jelen aacf0d
 	int n = BN_num_bits(dh_pub);
Jakub Jelen aacf0d
 	int bits_set = 0;
Jakub Jelen aacf0d
 	BIGNUM *tmp;
Jakub Jelen aacf0d
+	const BIGNUM *p;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if (dh_pub->neg) {
Jakub Jelen aacf0d
+	if (BN_is_negative(dh_pub)) {
Jakub Jelen aacf0d
 		logit("invalid public DH value: negative");
Jakub Jelen aacf0d
 		return 0;
Jakub Jelen aacf0d
 	}
Jakub Jelen 6cf9b8
@@ -232,7 +233,8 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
Jakub Jelen aacf0d
 		error("%s: BN_new failed", __func__);
Jakub Jelen aacf0d
 		return 0;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if (!BN_sub(tmp, dh->p, BN_value_one()) ||
Jakub Jelen aacf0d
+	DH_get0_pqg(dh, &p, NULL, NULL);
Jakub Jelen aacf0d
+	if (!BN_sub(tmp, p, BN_value_one()) ||
Jakub Jelen aacf0d
 	    BN_cmp(dh_pub, tmp) != -1) {		/* pub_exp > p-2 */
Jakub Jelen aacf0d
 		BN_clear_free(tmp);
Jakub Jelen aacf0d
 		logit("invalid public DH value: >= p-1");
Jakub Jelen 6cf9b8
@@ -243,14 +245,14 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
Jakub Jelen aacf0d
 	for (i = 0; i <= n; i++)
Jakub Jelen aacf0d
 		if (BN_is_bit_set(dh_pub, i))
Jakub Jelen aacf0d
 			bits_set++;
Jakub Jelen aacf0d
-	debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
Jakub Jelen aacf0d
+	debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/*
Jakub Jelen aacf0d
 	 * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
Jakub Jelen aacf0d
 	 */
Jakub Jelen aacf0d
 	if (bits_set < 4) {
Jakub Jelen aacf0d
 		logit("invalid public DH value (%d/%d)",
Jakub Jelen aacf0d
-		   bits_set, BN_num_bits(dh->p));
Jakub Jelen aacf0d
+		   bits_set, BN_num_bits(p));
Jakub Jelen aacf0d
 		return 0;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	return 1;
Jakub Jelen 6cf9b8
@@ -260,9 +262,11 @@ int
Jakub Jelen aacf0d
 dh_gen_key(DH *dh, int need)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	int pbits;
Jakub Jelen aacf0d
+	const BIGNUM *p, *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if (need < 0 || dh->p == NULL ||
Jakub Jelen aacf0d
-	    (pbits = BN_num_bits(dh->p)) <= 0 ||
Jakub Jelen aacf0d
+	DH_get0_pqg(dh, &p, NULL, NULL);
Jakub Jelen aacf0d
+	if (need < 0 || p == NULL ||
Jakub Jelen aacf0d
+	    (pbits = BN_num_bits(p)) <= 0 ||
Jakub Jelen aacf0d
 	    need > INT_MAX / 2 || 2 * need > pbits)
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 	if (need < 256)
Jakub Jelen 6cf9b8
@@ -271,10 +275,11 @@ dh_gen_key(DH *dh, int need)
Jakub Jelen aacf0d
 	 * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
Jakub Jelen aacf0d
 	 * so double requested need here.
Jakub Jelen aacf0d
 	 */
Jakub Jelen 6cf9b8
-	dh->length = MINIMUM(need * 2, pbits - 1);
Jakub Jelen aacf0d
-	if (DH_generate_key(dh) == 0 ||
Jakub Jelen aacf0d
-	    !dh_pub_is_valid(dh, dh->pub_key)) {
Jakub Jelen aacf0d
-		BN_clear_free(dh->priv_key);
Jakub Jelen aacf0d
+	DH_set_length(dh, MINIMUM(need * 2, pbits - 1));
Jakub Jelen aacf0d
+	if (DH_generate_key(dh) == 0)
Jakub Jelen aacf0d
+		return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
Jakub Jelen 6cf9b8
+	if (!dh_pub_is_valid(dh, pub_key)) {
Jakub Jelen aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen 6cf9b8
 	}
Jakub Jelen aacf0d
 	return 0;
Jakub Jelen 6cf9b8
@@ -284,15 +290,22 @@ DH *
Jakub Jelen aacf0d
 dh_new_group_asc(const char *gen, const char *modulus)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	DH *dh;
Jakub Jelen aacf0d
+	BIGNUM *p, *g;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if ((dh = DH_new()) == NULL)
Jakub Jelen aacf0d
-		return NULL;
Jakub Jelen aacf0d
-	if (BN_hex2bn(&dh->p, modulus) == 0 ||
Jakub Jelen aacf0d
-	    BN_hex2bn(&dh->g, gen) == 0) {
Jakub Jelen aacf0d
-		DH_free(dh);
Jakub Jelen aacf0d
-		return NULL;
Jakub Jelen aacf0d
-	}
Jakub Jelen aacf0d
+	if ((dh = DH_new()) == NULL ||
Jakub Jelen aacf0d
+	    (p = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (g = BN_new()) == NULL)
Jakub Jelen aacf0d
+		goto err;
Jakub Jelen aacf0d
+	if (BN_hex2bn(&p, modulus) == 0 ||
Jakub Jelen aacf0d
+	    BN_hex2bn(&g, gen) == 0 ||
Jakub Jelen aacf0d
+	    DH_set0_pqg(dh, p, NULL, g) == 0)
Jakub Jelen aacf0d
+		goto err;
Jakub Jelen aacf0d
 	return (dh);
Jakub Jelen aacf0d
+err:
Jakub Jelen aacf0d
+	DH_free(dh);
Jakub Jelen aacf0d
+	BN_free(p);
Jakub Jelen aacf0d
+	BN_free(g);
Jakub Jelen aacf0d
+	return NULL;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 /*
Jakub Jelen 6cf9b8
@@ -307,8 +320,7 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulu
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if ((dh = DH_new()) == NULL)
Jakub Jelen aacf0d
 		return NULL;
Jakub Jelen aacf0d
-	dh->p = modulus;
Jakub Jelen aacf0d
-	dh->g = gen;
Jakub Jelen aacf0d
+	DH_set0_pqg(dh, modulus, NULL, gen);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	return (dh);
Jakub Jelen aacf0d
 }
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/dh.h.openssl openssh-7.4p1/dh.h
Jakub Jelen 6cf9b8
--- openssh-7.4p1/dh.h.openssl	2016-12-23 17:47:36.418817747 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/dh.h	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -42,7 +42,7 @@ DH	*dh_new_group18(void);
Jakub Jelen aacf0d
 DH	*dh_new_group_fallback(int);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 int	 dh_gen_key(DH *, int);
Jakub Jelen aacf0d
-int	 dh_pub_is_valid(DH *, BIGNUM *);
Jakub Jelen aacf0d
+int	 dh_pub_is_valid(const DH *, const BIGNUM *);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 u_int	 dh_estimate(int);
Jakub Jelen aacf0d
 
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/digest-openssl.c.openssl openssh-7.4p1/digest-openssl.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/digest-openssl.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/digest-openssl.c	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -43,7 +43,7 @@
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 struct ssh_digest_ctx {
Jakub Jelen aacf0d
 	int alg;
Jakub Jelen aacf0d
-	EVP_MD_CTX mdctx;
Jakub Jelen aacf0d
+	EVP_MD_CTX *mdctx;
Jakub Jelen aacf0d
 };
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 struct ssh_digest {
Jakub Jelen aacf0d
@@ -107,7 +107,7 @@ ssh_digest_bytes(int alg)
Jakub Jelen aacf0d
 size_t
Jakub Jelen aacf0d
 ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
-	return EVP_MD_CTX_block_size(&ctx->mdctx);
Jakub Jelen aacf0d
+	return EVP_MD_CTX_block_size(ctx->mdctx);
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 struct ssh_digest_ctx *
Jakub Jelen aacf0d
@@ -119,8 +119,9 @@ ssh_digest_start(int alg)
Jakub Jelen aacf0d
 	if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
Jakub Jelen aacf0d
 		return NULL;
Jakub Jelen aacf0d
 	ret->alg = alg;
Jakub Jelen aacf0d
-	EVP_MD_CTX_init(&ret->mdctx);
Jakub Jelen aacf0d
-	if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
Jakub Jelen aacf0d
+	ret->mdctx = EVP_MD_CTX_new();
Jakub Jelen aacf0d
+	if (ret->mdctx == NULL ||
Jakub Jelen aacf0d
+	    EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
Jakub Jelen aacf0d
 		free(ret);
Jakub Jelen aacf0d
 		return NULL;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
@@ -133,7 +134,7 @@ ssh_digest_copy_state(struct ssh_digest_
Jakub Jelen aacf0d
 	if (from->alg != to->alg)
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 	/* we have bcopy-style order while openssl has memcpy-style */
Jakub Jelen aacf0d
-	if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
Jakub Jelen aacf0d
+	if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
Jakub Jelen aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 	return 0;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
@@ -141,7 +142,7 @@ ssh_digest_copy_state(struct ssh_digest_
Jakub Jelen aacf0d
 int
Jakub Jelen aacf0d
 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
-	if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
Jakub Jelen aacf0d
+	if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
Jakub Jelen aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 	return 0;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
@@ -162,7 +163,7 @@ ssh_digest_final(struct ssh_digest_ctx *
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 	if (dlen < digest->digest_len) /* No truncation allowed */
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
-	if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
Jakub Jelen aacf0d
+	if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
Jakub Jelen aacf0d
 		return SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 	if (l != digest->digest_len) /* sanity */
Jakub Jelen aacf0d
 		return SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
@@ -173,7 +174,7 @@ void
Jakub Jelen aacf0d
 ssh_digest_free(struct ssh_digest_ctx *ctx)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	if (ctx != NULL) {
Jakub Jelen aacf0d
-		EVP_MD_CTX_cleanup(&ctx->mdctx);
Jakub Jelen aacf0d
+		EVP_MD_CTX_free(ctx->mdctx);
Jakub Jelen aacf0d
 		explicit_bzero(ctx, sizeof(*ctx));
Jakub Jelen aacf0d
 		free(ctx);
Jakub Jelen aacf0d
 	}
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/entropy.c.openssl openssh-7.4p1/entropy.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/entropy.c.openssl	2016-12-23 17:47:36.419817747 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/entropy.c	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -218,7 +218,9 @@ seed_rng(void)
Jakub Jelen aacf0d
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* clean the PRNG status when exiting the program */
Jakub Jelen aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
Jakub Jelen aacf0d
 	atexit(RAND_cleanup);
Jakub Jelen aacf0d
+#endif
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #ifndef OPENSSL_PRNG_ONLY
Jakub Jelen aacf0d
 	if (RAND_status() == 1) {
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/gss-genr.c.openssl openssh-7.4p1/gss-genr.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/gss-genr.c.openssl	2016-12-23 17:47:36.392817736 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/gss-genr.c	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen 6cf9b8
@@ -99,7 +99,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
Jakub Jelen aacf0d
 	u_char digest[EVP_MAX_MD_SIZE];
Jakub Jelen aacf0d
 	char deroid[2];
Jakub Jelen aacf0d
 	const EVP_MD *evp_md = EVP_md5();
Jakub Jelen aacf0d
-	EVP_MD_CTX md;
Jakub Jelen aacf0d
+	EVP_MD_CTX *md;
Jakub Jelen aacf0d
 	char *s, *cp, *p;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (gss_enc2oid != NULL) {
Jakub Jelen 6cf9b8
@@ -113,6 +113,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	buffer_init(&buf;;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
+	md = EVP_MD_CTX_new();
Jakub Jelen aacf0d
 	oidpos = 0;
Jakub Jelen aacf0d
 	s = cp = xstrdup(kex);
Jakub Jelen aacf0d
 	for (i = 0; i < gss_supported->count; i++) {
Jakub Jelen 6cf9b8
@@ -122,12 +123,13 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
Jakub Jelen aacf0d
 			deroid[0] = SSH_GSS_OIDTYPE;
Jakub Jelen aacf0d
 			deroid[1] = gss_supported->elements[i].length;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-			EVP_DigestInit(&md, evp_md);
Jakub Jelen aacf0d
-			EVP_DigestUpdate(&md, deroid, 2);
Jakub Jelen aacf0d
-			EVP_DigestUpdate(&md,
Jakub Jelen aacf0d
+			EVP_MD_CTX_reset(md);
Jakub Jelen aacf0d
+			EVP_DigestInit(md, evp_md);
Jakub Jelen aacf0d
+			EVP_DigestUpdate(md, deroid, 2);
Jakub Jelen aacf0d
+			EVP_DigestUpdate(md,
Jakub Jelen aacf0d
 			    gss_supported->elements[i].elements,
Jakub Jelen aacf0d
 			    gss_supported->elements[i].length);
Jakub Jelen aacf0d
-			EVP_DigestFinal(&md, digest, NULL);
Jakub Jelen aacf0d
+			EVP_DigestFinal(md, digest, NULL);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 			encoded = xmalloc(EVP_MD_size(evp_md) * 2);
Jakub Jelen aacf0d
 			enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
Jakub Jelen 6cf9b8
@@ -149,6 +151,7 @@ ssh_gssapi_kex_mechs(gss_OID_set gss_sup
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	free(s);
Jakub Jelen aacf0d
+	EVP_MD_CTX_free(md);
Jakub Jelen aacf0d
 	gss_enc2oid[oidpos].oid = NULL;
Jakub Jelen aacf0d
 	gss_enc2oid[oidpos].encoded = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/includes.h.openssl openssh-7.4p1/includes.h
Jakub Jelen 6cf9b8
--- openssh-7.4p1/includes.h.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/includes.h	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -163,6 +163,7 @@
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
 #include <openssl/opensslv.h> /* For OPENSSL_VERSION_NUMBER */
Jakub Jelen aacf0d
+#include "libcrypto-compat.h"
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #include "defines.h"
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/kexdhc.c.openssl openssh-7.4p1/kexdhc.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/kexdhc.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/kexdhc.c	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -56,6 +56,7 @@ kexdh_client(struct ssh *ssh)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	struct kex *kex = ssh->kex;
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
+	const BIGNUM *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* generate and send 'e', client DH public key */
Jakub Jelen aacf0d
 	switch (kex->kex_type) {
Jakub Jelen aacf0d
@@ -81,21 +82,27 @@ kexdh_client(struct ssh *ssh)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	debug("sending SSH2_MSG_KEXDH_INIT");
Jakub Jelen aacf0d
-	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
Jakub Jelen aacf0d
+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
Jakub Jelen aacf0d
+		goto out;
Jakub Jelen aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
Jakub Jelen aacf0d
+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 #ifdef DEBUG_KEXDH
Jakub Jelen aacf0d
 	DHparams_print_fp(stderr, kex->dh);
Jakub Jelen aacf0d
 	fprintf(stderr, "pub= ");
Jakub Jelen aacf0d
-	BN_print_fp(stderr, kex->dh->pub_key);
Jakub Jelen aacf0d
+	BN_print_fp(stderr, pub_key);
Jakub Jelen aacf0d
 	fprintf(stderr, "\n");
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 	debug("expecting SSH2_MSG_KEXDH_REPLY");
Jakub Jelen aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
Jakub Jelen aacf0d
 	r = 0;
Jakub Jelen aacf0d
  out:
Jakub Jelen aacf0d
+	if (r != 0) {
Jakub Jelen aacf0d
+		DH_free(kex->dh);
Jakub Jelen aacf0d
+		kex->dh = NULL;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 	return r;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -110,6 +117,7 @@ input_kex_dh(int type, u_int32_t seq, vo
Jakub Jelen aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
Jakub Jelen aacf0d
 	size_t klen = 0, slen, sbloblen, hashlen;
Jakub Jelen aacf0d
 	int kout, r;
Jakub Jelen aacf0d
+	const BIGNUM *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (kex->verify_host_key == NULL) {
Jakub Jelen aacf0d
 		r = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
@@ -169,6 +177,7 @@ input_kex_dh(int type, u_int32_t seq, vo
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* calc and verify H */
Jakub Jelen aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
Jakub Jelen aacf0d
 	hashlen = sizeof(hash);
Jakub Jelen aacf0d
 	if ((r = kex_dh_hash(
Jakub Jelen aacf0d
 	    kex->hash_alg,
Jakub Jelen aacf0d
@@ -177,7 +186,7 @@ input_kex_dh(int type, u_int32_t seq, vo
Jakub Jelen aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Jakub Jelen aacf0d
 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Jakub Jelen aacf0d
 	    server_host_key_blob, sbloblen,
Jakub Jelen aacf0d
-	    kex->dh->pub_key,
Jakub Jelen aacf0d
+	    pub_key,
Jakub Jelen aacf0d
 	    dh_server_pub,
Jakub Jelen aacf0d
 	    shared_secret,
Jakub Jelen aacf0d
 	    hash, &hashlen)) != 0)
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/kexdhs.c.openssl openssh-7.4p1/kexdhs.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/kexdhs.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/kexdhs.c	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -87,6 +87,10 @@ kexdh_server(struct ssh *ssh)
Jakub Jelen aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
Jakub Jelen aacf0d
 	r = 0;
Jakub Jelen aacf0d
  out:
Jakub Jelen aacf0d
+	if (r != 0) {
Jakub Jelen aacf0d
+		DH_free(kex->dh);
Jakub Jelen aacf0d
+		kex->dh = NULL;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 	return r;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -102,6 +106,7 @@ input_kex_dh_init(int type, u_int32_t se
Jakub Jelen aacf0d
 	size_t sbloblen, slen;
Jakub Jelen aacf0d
 	size_t klen = 0, hashlen;
Jakub Jelen aacf0d
 	int kout, r;
Jakub Jelen aacf0d
+	const BIGNUM *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (kex->load_host_public_key == NULL ||
Jakub Jelen aacf0d
 	    kex->load_host_private_key == NULL) {
Jakub Jelen aacf0d
@@ -164,6 +169,7 @@ input_kex_dh_init(int type, u_int32_t se
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	/* calc H */
Jakub Jelen aacf0d
 	hashlen = sizeof(hash);
Jakub Jelen aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
Jakub Jelen aacf0d
 	if ((r = kex_dh_hash(
Jakub Jelen aacf0d
 	    kex->hash_alg,
Jakub Jelen aacf0d
 	    kex->client_version_string,
Jakub Jelen aacf0d
@@ -172,7 +178,7 @@ input_kex_dh_init(int type, u_int32_t se
Jakub Jelen aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Jakub Jelen aacf0d
 	    server_host_key_blob, sbloblen,
Jakub Jelen aacf0d
 	    dh_client_pub,
Jakub Jelen aacf0d
-	    kex->dh->pub_key,
Jakub Jelen aacf0d
+	    pub_key,
Jakub Jelen aacf0d
 	    shared_secret,
Jakub Jelen aacf0d
 	    hash, &hashlen)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
@@ -198,7 +204,7 @@ input_kex_dh_init(int type, u_int32_t se
Jakub Jelen aacf0d
 	/* send server hostkey, DH pubkey 'f' and singed H */
Jakub Jelen aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||	/* f */
Jakub Jelen aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||	/* f */
Jakub Jelen aacf0d
 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/kexgexc.c.openssl openssh-7.4p1/kexgexc.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/kexgexc.c.openssl	2016-12-23 17:47:36.419817747 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/kexgexc.c	2016-12-23 17:47:36.431817752 +0100
Jakub Jelen aacf0d
@@ -96,6 +96,7 @@ input_kex_dh_gex_group(int type, u_int32
Jakub Jelen aacf0d
 	struct kex *kex = ssh->kex;
Jakub Jelen aacf0d
 	BIGNUM *p = NULL, *g = NULL;
Jakub Jelen aacf0d
 	int r, bits;
Jakub Jelen aacf0d
+	const BIGNUM *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -120,26 +121,30 @@ input_kex_dh_gex_group(int type, u_int32
Jakub Jelen aacf0d
 	p = g = NULL; /* belong to kex->dh now */
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* generate and send 'e', client DH public key */
Jakub Jelen aacf0d
-	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
Jakub Jelen aacf0d
+	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
Jakub Jelen aacf0d
+		goto out;
Jakub Jelen aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
Jakub Jelen aacf0d
+	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
Jakub Jelen aacf0d
 #ifdef DEBUG_KEXDH
Jakub Jelen aacf0d
 	DHparams_print_fp(stderr, kex->dh);
Jakub Jelen aacf0d
 	fprintf(stderr, "pub= ");
Jakub Jelen aacf0d
-	BN_print_fp(stderr, kex->dh->pub_key);
Jakub Jelen aacf0d
+	BN_print_fp(stderr, pub_key);
Jakub Jelen aacf0d
 	fprintf(stderr, "\n");
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
Jakub Jelen aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
Jakub Jelen aacf0d
 	r = 0;
Jakub Jelen aacf0d
 out:
Jakub Jelen aacf0d
-	if (p)
Jakub Jelen aacf0d
-		BN_clear_free(p);
Jakub Jelen aacf0d
-	if (g)
Jakub Jelen aacf0d
-		BN_clear_free(g);
Jakub Jelen aacf0d
+	BN_clear_free(p);
Jakub Jelen aacf0d
+	BN_clear_free(g);
Jakub Jelen aacf0d
+	if (r != 0) {
Jakub Jelen aacf0d
+		DH_free(kex->dh);
Jakub Jelen aacf0d
+		kex->dh = NULL;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 	return r;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -154,6 +159,7 @@ input_kex_dh_gex_reply(int type, u_int32
Jakub Jelen aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
Jakub Jelen aacf0d
 	size_t klen = 0, slen, sbloblen, hashlen;
Jakub Jelen aacf0d
 	int kout, r;
Jakub Jelen aacf0d
+	const BIGNUM *p, *g, *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
Jakub Jelen aacf0d
 	if (kex->verify_host_key == NULL) {
Jakub Jelen aacf0d
@@ -220,6 +226,8 @@ input_kex_dh_gex_reply(int type, u_int32
Jakub Jelen aacf0d
 		kex->min = kex->max = -1;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* calc and verify H */
Jakub Jelen aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
Jakub Jelen aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
Jakub Jelen aacf0d
 	hashlen = sizeof(hash);
Jakub Jelen aacf0d
 	if ((r = kexgex_hash(
Jakub Jelen aacf0d
 	    kex->hash_alg,
Jakub Jelen aacf0d
@@ -229,8 +237,8 @@ input_kex_dh_gex_reply(int type, u_int32
Jakub Jelen aacf0d
 	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Jakub Jelen aacf0d
 	    server_host_key_blob, sbloblen,
Jakub Jelen aacf0d
 	    kex->min, kex->nbits, kex->max,
Jakub Jelen aacf0d
-	    kex->dh->p, kex->dh->g,
Jakub Jelen aacf0d
-	    kex->dh->pub_key,
Jakub Jelen aacf0d
+	    p, g,
Jakub Jelen aacf0d
+	    pub_key,
Jakub Jelen aacf0d
 	    dh_server_pub,
Jakub Jelen aacf0d
 	    shared_secret,
Jakub Jelen aacf0d
 	    hash, &hashlen)) != 0)
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/kexgexs.c.openssl openssh-7.4p1/kexgexs.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/kexgexs.c.openssl	2016-12-23 17:47:36.419817747 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/kexgexs.c	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen aacf0d
@@ -73,6 +73,7 @@ input_kex_dh_gex_request(int type, u_int
Jakub Jelen aacf0d
 	struct kex *kex = ssh->kex;
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
 	u_int min = 0, max = 0, nbits = 0;
Jakub Jelen aacf0d
+	const BIGNUM *p, *g;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
Jakub Jelen aacf0d
 	if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
Jakub Jelen aacf0d
@@ -102,9 +103,10 @@ input_kex_dh_gex_request(int type, u_int
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
Jakub Jelen aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
Jakub Jelen aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -116,6 +118,10 @@ input_kex_dh_gex_request(int type, u_int
Jakub Jelen aacf0d
 	ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
Jakub Jelen aacf0d
 	r = 0;
Jakub Jelen aacf0d
  out:
Jakub Jelen aacf0d
+	if (r != 0) {
Jakub Jelen aacf0d
+		DH_free(kex->dh);
Jakub Jelen aacf0d
+		kex->dh = NULL;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 	return r;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -131,6 +137,7 @@ input_kex_dh_gex_init(int type, u_int32_
Jakub Jelen aacf0d
 	size_t sbloblen, slen;
Jakub Jelen aacf0d
 	size_t klen = 0, hashlen;
Jakub Jelen aacf0d
 	int kout, r;
Jakub Jelen aacf0d
+	const BIGNUM *p, *g, *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (kex->load_host_public_key == NULL ||
Jakub Jelen aacf0d
 	    kex->load_host_private_key == NULL) {
Jakub Jelen aacf0d
@@ -193,6 +200,8 @@ input_kex_dh_gex_init(int type, u_int32_
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	/* calc H */
Jakub Jelen aacf0d
 	hashlen = sizeof(hash);
Jakub Jelen aacf0d
+	DH_get0_pqg(kex->dh, &p, NULL, &g);
Jakub Jelen aacf0d
+	DH_get0_key(kex->dh, &pub_key, NULL);
Jakub Jelen aacf0d
 	if ((r = kexgex_hash(
Jakub Jelen aacf0d
 	    kex->hash_alg,
Jakub Jelen aacf0d
 	    kex->client_version_string,
Jakub Jelen aacf0d
@@ -201,9 +210,9 @@ input_kex_dh_gex_init(int type, u_int32_
Jakub Jelen aacf0d
 	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Jakub Jelen aacf0d
 	    server_host_key_blob, sbloblen,
Jakub Jelen aacf0d
 	    kex->min, kex->nbits, kex->max,
Jakub Jelen aacf0d
-	    kex->dh->p, kex->dh->g,
Jakub Jelen aacf0d
+	    p, g,
Jakub Jelen aacf0d
 	    dh_client_pub,
Jakub Jelen aacf0d
-	    kex->dh->pub_key,
Jakub Jelen aacf0d
+	    pub_key,
Jakub Jelen aacf0d
 	    shared_secret,
Jakub Jelen aacf0d
 	    hash, &hashlen)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
@@ -229,7 +238,7 @@ input_kex_dh_gex_init(int type, u_int32_
Jakub Jelen aacf0d
 	/* send server hostkey, DH pubkey 'f' and singed H */
Jakub Jelen aacf0d
 	if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
Jakub Jelen aacf0d
+	    (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
Jakub Jelen aacf0d
 	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshpkt_send(ssh)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/kexgssc.c.openssl openssh-7.4p1/kexgssc.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/kexgssc.c.openssl	2016-12-23 17:47:36.349817718 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/kexgssc.c	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen aacf0d
@@ -58,6 +58,7 @@ kexgss_client(struct ssh *ssh) {
Jakub Jelen aacf0d
 	BIGNUM *shared_secret = NULL;
Jakub Jelen aacf0d
 	BIGNUM *p = NULL;
Jakub Jelen aacf0d
 	BIGNUM *g = NULL;	
Jakub Jelen aacf0d
+	const BIGNUM *pub_key, *p1, *g1;
Jakub Jelen aacf0d
 	u_char *kbuf;
Jakub Jelen aacf0d
 	u_char *serverhostkey = NULL;
Jakub Jelen aacf0d
 	u_char *empty = "";
Jakub Jelen aacf0d
@@ -121,6 +122,7 @@ kexgss_client(struct ssh *ssh) {
Jakub Jelen aacf0d
 	
Jakub Jelen aacf0d
 	/* Step 1 - e is dh->pub_key */
Jakub Jelen aacf0d
 	dh_gen_key(dh, ssh->kex->we_need * 8);
Jakub Jelen aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* This is f, we initialise it now to make life easier */
Jakub Jelen aacf0d
 	dh_server_pub = BN_new();
Jakub Jelen aacf0d
@@ -168,7 +170,7 @@ kexgss_client(struct ssh *ssh) {
Jakub Jelen aacf0d
 				packet_start(SSH2_MSG_KEXGSS_INIT);
Jakub Jelen aacf0d
 				packet_put_string(send_tok.value,
Jakub Jelen aacf0d
 				    send_tok.length);
Jakub Jelen aacf0d
-				packet_put_bignum2(dh->pub_key);
Jakub Jelen aacf0d
+				packet_put_bignum2((BIGNUM *)pub_key);
Jakub Jelen aacf0d
 				first = 0;
Jakub Jelen aacf0d
 			} else {
Jakub Jelen aacf0d
 				packet_start(SSH2_MSG_KEXGSS_CONTINUE);
Jakub Jelen aacf0d
@@ -275,13 +277,14 @@ kexgss_client(struct ssh *ssh) {
Jakub Jelen aacf0d
 		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
Jakub Jelen aacf0d
 		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
Jakub Jelen aacf0d
 		    (serverhostkey ? serverhostkey : empty), slen,
Jakub Jelen aacf0d
-		    dh->pub_key,	/* e */
Jakub Jelen aacf0d
+		    pub_key,		/* e */
Jakub Jelen aacf0d
 		    dh_server_pub,	/* f */
Jakub Jelen aacf0d
 		    shared_secret,	/* K */
Jakub Jelen aacf0d
 		    hash, &hashlen
Jakub Jelen aacf0d
 		);
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEX_GSS_GEX_SHA1:
Jakub Jelen aacf0d
+		DH_get0_pqg(dh, &p1, NULL, &g1;;
Jakub Jelen aacf0d
 		kexgex_hash(
Jakub Jelen aacf0d
 		    ssh->kex->hash_alg,
Jakub Jelen aacf0d
 		    ssh->kex->client_version_string,
Jakub Jelen aacf0d
@@ -290,8 +293,8 @@ kexgss_client(struct ssh *ssh) {
Jakub Jelen aacf0d
 		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
Jakub Jelen aacf0d
 		    (serverhostkey ? serverhostkey : empty), slen,
Jakub Jelen aacf0d
  		    min, nbits, max,
Jakub Jelen aacf0d
-		    dh->p, dh->g,
Jakub Jelen aacf0d
-		    dh->pub_key,
Jakub Jelen aacf0d
+		    p1, g1,
Jakub Jelen aacf0d
+		    pub_key,
Jakub Jelen aacf0d
 		    dh_server_pub,
Jakub Jelen aacf0d
 		    shared_secret,
Jakub Jelen aacf0d
 		    hash, &hashlen
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/kexgsss.c.openssl openssh-7.4p1/kexgsss.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/kexgsss.c.openssl	2016-12-23 17:47:36.349817718 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/kexgsss.c	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen aacf0d
@@ -77,6 +77,7 @@ kexgss_server(struct ssh *ssh)
Jakub Jelen aacf0d
 	char *mechs;
Jakub Jelen aacf0d
 	u_char hash[SSH_DIGEST_MAX_LENGTH];
Jakub Jelen aacf0d
 	size_t hashlen;
Jakub Jelen aacf0d
+	const BIGNUM *p, *g, *pub_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Initialise GSSAPI */
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -122,9 +123,10 @@ kexgss_server(struct ssh *ssh)
Jakub Jelen aacf0d
 		if (dh == NULL)
Jakub Jelen aacf0d
 			packet_disconnect("Protocol error: no matching group found");
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
+		DH_get0_pqg(dh, &p, NULL, &g);
Jakub Jelen aacf0d
 		packet_start(SSH2_MSG_KEXGSS_GROUP);
Jakub Jelen aacf0d
-		packet_put_bignum2(dh->p);
Jakub Jelen aacf0d
-		packet_put_bignum2(dh->g);
Jakub Jelen aacf0d
+		packet_put_bignum2((BIGNUM *)p);
Jakub Jelen aacf0d
+		packet_put_bignum2((BIGNUM *)g);
Jakub Jelen aacf0d
 		packet_send();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 		packet_write_wait();
Jakub Jelen aacf0d
@@ -216,6 +218,7 @@ kexgss_server(struct ssh *ssh)
Jakub Jelen aacf0d
 	memset(kbuf, 0, klen);
Jakub Jelen aacf0d
 	free(kbuf);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
+	DH_get0_key(dh, &pub_key, NULL);
Jakub Jelen aacf0d
 	hashlen = sizeof(hash);
Jakub Jelen aacf0d
 	switch (ssh->kex->kex_type) {
Jakub Jelen aacf0d
 	case KEX_GSS_GRP1_SHA1:
Jakub Jelen aacf0d
@@ -225,7 +228,7 @@ kexgss_server(struct ssh *ssh)
Jakub Jelen aacf0d
 		    buffer_ptr(ssh->kex->peer), buffer_len(ssh->kex->peer),
Jakub Jelen aacf0d
 		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
Jakub Jelen aacf0d
 		    NULL, 0, /* Change this if we start sending host keys */
Jakub Jelen aacf0d
-		    dh_client_pub, dh->pub_key, shared_secret,
Jakub Jelen aacf0d
+		    dh_client_pub, pub_key, shared_secret,
Jakub Jelen aacf0d
 		    hash, &hashlen
Jakub Jelen aacf0d
 		);
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
@@ -237,9 +240,9 @@ kexgss_server(struct ssh *ssh)
Jakub Jelen aacf0d
 		    buffer_ptr(ssh->kex->my), buffer_len(ssh->kex->my),
Jakub Jelen aacf0d
 		    NULL, 0,
Jakub Jelen aacf0d
 		    cmin, nbits, cmax,
Jakub Jelen aacf0d
-		    dh->p, dh->g,
Jakub Jelen aacf0d
+		    p, g,
Jakub Jelen aacf0d
 		    dh_client_pub,
Jakub Jelen aacf0d
-		    dh->pub_key,
Jakub Jelen aacf0d
+		    pub_key,
Jakub Jelen aacf0d
 		    shared_secret,
Jakub Jelen aacf0d
 		    hash, &hashlen
Jakub Jelen aacf0d
 		);
Jakub Jelen aacf0d
@@ -263,7 +266,7 @@ kexgss_server(struct ssh *ssh)
Jakub Jelen aacf0d
 		fatal("Couldn't get MIC");
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	packet_start(SSH2_MSG_KEXGSS_COMPLETE);
Jakub Jelen aacf0d
-	packet_put_bignum2(dh->pub_key);
Jakub Jelen aacf0d
+	packet_put_bignum2((BIGNUM *)pub_key);
Jakub Jelen aacf0d
 	packet_put_string(msg_tok.value,msg_tok.length);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (send_tok.length != 0) {
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/libcrypto-compat.c.openssl openssh-7.4p1/libcrypto-compat.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/libcrypto-compat.c.openssl	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/libcrypto-compat.c	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen aacf0d
@@ -0,0 +1,546 @@
Jakub Jelen aacf0d
+/*
Jakub Jelen aacf0d
+ * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
Jakub Jelen aacf0d
+ *
Jakub Jelen aacf0d
+ * Licensed under the OpenSSL license (the "License").  You may not use
Jakub Jelen aacf0d
+ * this file except in compliance with the License.  You can obtain a copy
Jakub Jelen aacf0d
+ * in the file LICENSE in the source distribution or at
Jakub Jelen aacf0d
+ * https://www.openssl.org/source/license.html
Jakub Jelen aacf0d
+ */
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#include "includes.h"
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#include <string.h>
Jakub Jelen aacf0d
+#include <openssl/engine.h>
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+static void *OPENSSL_zalloc(size_t num)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    void *ret = OPENSSL_malloc(num);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (ret != NULL)
Jakub Jelen aacf0d
+        memset(ret, 0, num);
Jakub Jelen aacf0d
+    return ret;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the fields n and e in r are NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL for n and e.  d may be
Jakub Jelen aacf0d
+     * left NULL (in case only the public key is used).
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if ((r->n == NULL && n == NULL)
Jakub Jelen aacf0d
+        || (r->e == NULL && e == NULL))
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (n != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->n);
Jakub Jelen aacf0d
+        r->n = n;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (e != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->e);
Jakub Jelen aacf0d
+        r->e = e;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (d != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->d);
Jakub Jelen aacf0d
+        r->d = d;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the fields p and q in r are NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if ((r->p == NULL && p == NULL)
Jakub Jelen aacf0d
+        || (r->q == NULL && q == NULL))
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (p != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->p);
Jakub Jelen aacf0d
+        r->p = p;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (q != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->q);
Jakub Jelen aacf0d
+        r->q = q;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if ((r->dmp1 == NULL && dmp1 == NULL)
Jakub Jelen aacf0d
+        || (r->dmq1 == NULL && dmq1 == NULL)
Jakub Jelen aacf0d
+        || (r->iqmp == NULL && iqmp == NULL))
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (dmp1 != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->dmp1);
Jakub Jelen aacf0d
+        r->dmp1 = dmp1;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (dmq1 != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->dmq1);
Jakub Jelen aacf0d
+        r->dmq1 = dmq1;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (iqmp != NULL) {
Jakub Jelen aacf0d
+        BN_free(r->iqmp);
Jakub Jelen aacf0d
+        r->iqmp = iqmp;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void RSA_get0_key(const RSA *r,
Jakub Jelen aacf0d
+                  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (n != NULL)
Jakub Jelen aacf0d
+        *n = r->n;
Jakub Jelen aacf0d
+    if (e != NULL)
Jakub Jelen aacf0d
+        *e = r->e;
Jakub Jelen aacf0d
+    if (d != NULL)
Jakub Jelen aacf0d
+        *d = r->d;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (p != NULL)
Jakub Jelen aacf0d
+        *p = r->p;
Jakub Jelen aacf0d
+    if (q != NULL)
Jakub Jelen aacf0d
+        *q = r->q;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void RSA_get0_crt_params(const RSA *r,
Jakub Jelen aacf0d
+                         const BIGNUM **dmp1, const BIGNUM **dmq1,
Jakub Jelen aacf0d
+                         const BIGNUM **iqmp)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (dmp1 != NULL)
Jakub Jelen aacf0d
+        *dmp1 = r->dmp1;
Jakub Jelen aacf0d
+    if (dmq1 != NULL)
Jakub Jelen aacf0d
+        *dmq1 = r->dmq1;
Jakub Jelen aacf0d
+    if (iqmp != NULL)
Jakub Jelen aacf0d
+        *iqmp = r->iqmp;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DSA_get0_pqg(const DSA *d,
Jakub Jelen aacf0d
+                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (p != NULL)
Jakub Jelen aacf0d
+        *p = d->p;
Jakub Jelen aacf0d
+    if (q != NULL)
Jakub Jelen aacf0d
+        *q = d->q;
Jakub Jelen aacf0d
+    if (g != NULL)
Jakub Jelen aacf0d
+        *g = d->g;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the fields p, q and g in d are NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if ((d->p == NULL && p == NULL)
Jakub Jelen aacf0d
+        || (d->q == NULL && q == NULL)
Jakub Jelen aacf0d
+        || (d->g == NULL && g == NULL))
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (p != NULL) {
Jakub Jelen aacf0d
+        BN_free(d->p);
Jakub Jelen aacf0d
+        d->p = p;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (q != NULL) {
Jakub Jelen aacf0d
+        BN_free(d->q);
Jakub Jelen aacf0d
+        d->q = q;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (g != NULL) {
Jakub Jelen aacf0d
+        BN_free(d->g);
Jakub Jelen aacf0d
+        d->g = g;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DSA_get0_key(const DSA *d,
Jakub Jelen aacf0d
+                  const BIGNUM **pub_key, const BIGNUM **priv_key)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (pub_key != NULL)
Jakub Jelen aacf0d
+        *pub_key = d->pub_key;
Jakub Jelen aacf0d
+    if (priv_key != NULL)
Jakub Jelen aacf0d
+        *priv_key = d->priv_key;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the field pub_key in d is NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL.  The priv_key field may
Jakub Jelen aacf0d
+     * be left NULL.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if (d->pub_key == NULL && pub_key == NULL)
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (pub_key != NULL) {
Jakub Jelen aacf0d
+        BN_free(d->pub_key);
Jakub Jelen aacf0d
+        d->pub_key = pub_key;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (priv_key != NULL) {
Jakub Jelen aacf0d
+        BN_free(d->priv_key);
Jakub Jelen aacf0d
+        d->priv_key = priv_key;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (pr != NULL)
Jakub Jelen aacf0d
+        *pr = sig->r;
Jakub Jelen aacf0d
+    if (ps != NULL)
Jakub Jelen aacf0d
+        *ps = sig->s;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (r == NULL || s == NULL)
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+    BN_clear_free(sig->r);
Jakub Jelen aacf0d
+    BN_clear_free(sig->s);
Jakub Jelen aacf0d
+    sig->r = r;
Jakub Jelen aacf0d
+    sig->s = s;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (pr != NULL)
Jakub Jelen aacf0d
+        *pr = sig->r;
Jakub Jelen aacf0d
+    if (ps != NULL)
Jakub Jelen aacf0d
+        *ps = sig->s;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (r == NULL || s == NULL)
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+    BN_clear_free(sig->r);
Jakub Jelen aacf0d
+    BN_clear_free(sig->s);
Jakub Jelen aacf0d
+    sig->r = r;
Jakub Jelen aacf0d
+    sig->s = s;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DH_get0_pqg(const DH *dh,
Jakub Jelen aacf0d
+                 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (p != NULL)
Jakub Jelen aacf0d
+        *p = dh->p;
Jakub Jelen aacf0d
+    if (q != NULL)
Jakub Jelen aacf0d
+        *q = dh->q;
Jakub Jelen aacf0d
+    if (g != NULL)
Jakub Jelen aacf0d
+        *g = dh->g;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the fields p and g in d are NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL.  q may remain NULL.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if ((dh->p == NULL && p == NULL)
Jakub Jelen aacf0d
+        || (dh->g == NULL && g == NULL))
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (p != NULL) {
Jakub Jelen aacf0d
+        BN_free(dh->p);
Jakub Jelen aacf0d
+        dh->p = p;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (q != NULL) {
Jakub Jelen aacf0d
+        BN_free(dh->q);
Jakub Jelen aacf0d
+        dh->q = q;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (g != NULL) {
Jakub Jelen aacf0d
+        BN_free(dh->g);
Jakub Jelen aacf0d
+        dh->g = g;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (q != NULL) {
Jakub Jelen aacf0d
+        dh->length = BN_num_bits(q);
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (pub_key != NULL)
Jakub Jelen aacf0d
+        *pub_key = dh->pub_key;
Jakub Jelen aacf0d
+    if (priv_key != NULL)
Jakub Jelen aacf0d
+        *priv_key = dh->priv_key;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    /* If the field pub_key in dh is NULL, the corresponding input
Jakub Jelen aacf0d
+     * parameters MUST be non-NULL.  The priv_key field may
Jakub Jelen aacf0d
+     * be left NULL.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if (dh->pub_key == NULL && pub_key == NULL)
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (pub_key != NULL) {
Jakub Jelen aacf0d
+        BN_free(dh->pub_key);
Jakub Jelen aacf0d
+        dh->pub_key = pub_key;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    if (priv_key != NULL) {
Jakub Jelen aacf0d
+        BN_free(dh->priv_key);
Jakub Jelen aacf0d
+        dh->priv_key = priv_key;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DH_set_length(DH *dh, long length)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    dh->length = length;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return ctx->iv;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return ctx->iv;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+EVP_MD_CTX *EVP_MD_CTX_new(void)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+static void OPENSSL_clear_free(void *str, size_t num)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (str == NULL)
Jakub Jelen aacf0d
+        return;
Jakub Jelen aacf0d
+    if (num)
Jakub Jelen aacf0d
+        OPENSSL_cleanse(str, num);
Jakub Jelen aacf0d
+    OPENSSL_free(str);
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+/* This call frees resources associated with the context */
Jakub Jelen aacf0d
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (ctx == NULL)
Jakub Jelen aacf0d
+        return 1;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    /*
Jakub Jelen aacf0d
+     * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
Jakub Jelen aacf0d
+     * sometimes only copies of the context are ever finalised.
Jakub Jelen aacf0d
+     */
Jakub Jelen aacf0d
+    if (ctx->digest && ctx->digest->cleanup
Jakub Jelen aacf0d
+        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
Jakub Jelen aacf0d
+        ctx->digest->cleanup(ctx);
Jakub Jelen aacf0d
+    if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
Jakub Jelen aacf0d
+        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
Jakub Jelen aacf0d
+        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    EVP_PKEY_CTX_free(ctx->pctx);
Jakub Jelen aacf0d
+#ifndef OPENSSL_NO_ENGINE
Jakub Jelen aacf0d
+    ENGINE_finish(ctx->engine);
Jakub Jelen aacf0d
+#endif
Jakub Jelen aacf0d
+    OPENSSL_cleanse(ctx, sizeof(*ctx));
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    EVP_MD_CTX_reset(ctx);
Jakub Jelen aacf0d
+    OPENSSL_free(ctx);
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    RSA_METHOD *ret;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    ret = OPENSSL_malloc(sizeof(RSA_METHOD));
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (ret != NULL) {
Jakub Jelen aacf0d
+        memcpy(ret, meth, sizeof(*meth));
Jakub Jelen aacf0d
+        ret->name = OPENSSL_strdup(meth->name);
Jakub Jelen aacf0d
+        if (ret->name == NULL) {
Jakub Jelen aacf0d
+            OPENSSL_free(ret);
Jakub Jelen aacf0d
+            return NULL;
Jakub Jelen aacf0d
+        }
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return ret;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    char *tmpname;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    tmpname = OPENSSL_strdup(name);
Jakub Jelen aacf0d
+    if (tmpname == NULL) {
Jakub Jelen aacf0d
+        return 0;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    OPENSSL_free((char *)meth->name);
Jakub Jelen aacf0d
+    meth->name = tmpname;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_meth_set_priv_enc(RSA_METHOD *meth,
Jakub Jelen aacf0d
+                          int (*priv_enc) (int flen, const unsigned char *from,
Jakub Jelen aacf0d
+                                           unsigned char *to, RSA *rsa,
Jakub Jelen aacf0d
+                                           int padding))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    meth->rsa_priv_enc = priv_enc;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_meth_set_priv_dec(RSA_METHOD *meth,
Jakub Jelen aacf0d
+                          int (*priv_dec) (int flen, const unsigned char *from,
Jakub Jelen aacf0d
+                                           unsigned char *to, RSA *rsa,
Jakub Jelen aacf0d
+                                           int padding))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    meth->rsa_priv_dec = priv_dec;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    meth->finish = finish;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void RSA_meth_free(RSA_METHOD *meth)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (meth != NULL) {
Jakub Jelen aacf0d
+        OPENSSL_free((char *)meth->name);
Jakub Jelen aacf0d
+        OPENSSL_free(meth);
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_bits(const RSA *r)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return (BN_num_bits(r->n));
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int DSA_bits(const DSA *dsa)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return BN_num_bits(dsa->p);
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    if (pkey->type != EVP_PKEY_RSA) {
Jakub Jelen aacf0d
+        return NULL;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    return pkey->pkey.rsa;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+    if (cipher != NULL) {
Jakub Jelen aacf0d
+        cipher->nid = cipher_type;
Jakub Jelen aacf0d
+        cipher->block_size = block_size;
Jakub Jelen aacf0d
+        cipher->key_len = key_len;
Jakub Jelen aacf0d
+    }
Jakub Jelen aacf0d
+    return cipher;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    OPENSSL_free(cipher);
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    cipher->iv_len = iv_len;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    cipher->flags = flags;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                             int (*init) (EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                          const unsigned char *key,
Jakub Jelen aacf0d
+                                          const unsigned char *iv,
Jakub Jelen aacf0d
+                                          int enc))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    cipher->init = init;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                                  int (*do_cipher) (EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                                    unsigned char *out,
Jakub Jelen aacf0d
+                                                    const unsigned char *in,
Jakub Jelen aacf0d
+                                                    size_t inl))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    cipher->do_cipher = do_cipher;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                                int (*cleanup) (EVP_CIPHER_CTX *))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    cipher->cleanup = cleanup;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                             int (*ctrl) (EVP_CIPHER_CTX *, int type,
Jakub Jelen aacf0d
+                                          int arg, void *ptr))
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    cipher->ctrl = ctrl;
Jakub Jelen aacf0d
+    return 1;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                                          const unsigned char *key,
Jakub Jelen aacf0d
+                                                          const unsigned char *iv,
Jakub Jelen aacf0d
+                                                          int enc)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return cipher->init;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                                               unsigned char *out,
Jakub Jelen aacf0d
+                                                               const unsigned char *in,
Jakub Jelen aacf0d
+                                                               size_t inl)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return cipher->do_cipher;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return cipher->cleanup;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
Jakub Jelen aacf0d
+                                                          int type, int arg,
Jakub Jelen aacf0d
+                                                          void *ptr)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return cipher->ctrl;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
Jakub Jelen aacf0d
+{
Jakub Jelen aacf0d
+    return ctx->encrypt;
Jakub Jelen aacf0d
+}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#endif /* OPENSSL_VERSION_NUMBER */
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/libcrypto-compat.h.openssl openssh-7.4p1/libcrypto-compat.h
Jakub Jelen 6cf9b8
--- openssh-7.4p1/libcrypto-compat.h.openssl	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/libcrypto-compat.h	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen aacf0d
@@ -0,0 +1,98 @@
Jakub Jelen aacf0d
+#ifndef LIBCRYPTO_COMPAT_H
Jakub Jelen aacf0d
+#define LIBCRYPTO_COMPAT_H
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#include <openssl/rsa.h>
Jakub Jelen aacf0d
+#include <openssl/dsa.h>
Jakub Jelen aacf0d
+#include <openssl/ecdsa.h>
Jakub Jelen aacf0d
+#include <openssl/dh.h>
Jakub Jelen aacf0d
+#include <openssl/evp.h>
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
Jakub Jelen aacf0d
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
Jakub Jelen aacf0d
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
Jakub Jelen aacf0d
+void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
Jakub Jelen aacf0d
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
Jakub Jelen aacf0d
+void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
Jakub Jelen aacf0d
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
Jakub Jelen aacf0d
+void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key);
Jakub Jelen aacf0d
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
Jakub Jelen aacf0d
+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
Jakub Jelen aacf0d
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
Jakub Jelen aacf0d
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
Jakub Jelen aacf0d
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
Jakub Jelen aacf0d
+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
Jakub Jelen aacf0d
+int DH_set_length(DH *dh, long length);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx);
Jakub Jelen aacf0d
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx);
Jakub Jelen aacf0d
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
Jakub Jelen aacf0d
+EVP_MD_CTX *EVP_MD_CTX_new(void);
Jakub Jelen aacf0d
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
Jakub Jelen aacf0d
+#define EVP_CIPHER_impl_ctx_size(e) e->ctx_size
Jakub Jelen aacf0d
+#define EVP_CIPHER_CTX_get_cipher_data(ctx) ctx->cipher_data
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
Jakub Jelen aacf0d
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
Jakub Jelen aacf0d
+#define RSA_meth_get_finish(meth) meth->finish
Jakub Jelen 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));
Jakub Jelen 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));
Jakub Jelen aacf0d
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa));
Jakub Jelen aacf0d
+void RSA_meth_free(RSA_METHOD *meth);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int RSA_bits(const RSA *r);
Jakub Jelen aacf0d
+int DSA_bits(const DSA *d);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len);
Jakub Jelen aacf0d
+void EVP_CIPHER_meth_free(EVP_CIPHER *cipher);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len);
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags);
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                             int (*init) (EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                          const unsigned char *key,
Jakub Jelen aacf0d
+                                          const unsigned char *iv,
Jakub Jelen aacf0d
+                                          int enc));
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                                  int (*do_cipher) (EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                                    unsigned char *out,
Jakub Jelen aacf0d
+                                                    const unsigned char *in,
Jakub Jelen aacf0d
+                                                    size_t inl));
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                                int (*cleanup) (EVP_CIPHER_CTX *));
Jakub Jelen aacf0d
+int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
Jakub Jelen aacf0d
+                             int (*ctrl) (EVP_CIPHER_CTX *, int type,
Jakub Jelen aacf0d
+                                          int arg, void *ptr));
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                                          const unsigned char *key,
Jakub Jelen aacf0d
+                                                          const unsigned char *iv,
Jakub Jelen aacf0d
+                                                          int enc);
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
Jakub Jelen aacf0d
+                                                               unsigned char *out,
Jakub Jelen aacf0d
+                                                               const unsigned char *in,
Jakub Jelen aacf0d
+                                                               size_t inl);
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *);
Jakub Jelen aacf0d
+int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
Jakub Jelen aacf0d
+                                                          int type, int arg,
Jakub Jelen aacf0d
+                                                          void *ptr);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#define EVP_CIPHER_CTX_reset(c)      EVP_CIPHER_CTX_init(c)
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#endif /* OPENSSL_VERSION_NUMBER */
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+#endif /* LIBCRYPTO_COMPAT_H */
Jakub Jelen aacf0d
+
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/Makefile.in.openssl openssh-7.4p1/Makefile.in
Jakub Jelen 6cf9b8
--- openssh-7.4p1/Makefile.in.openssl	2016-12-23 17:47:36.420817748 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/Makefile.in	2016-12-23 17:47:36.432817753 +0100
Jakub Jelen aacf0d
@@ -100,7 +100,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
Jakub Jelen aacf0d
 	kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
Jakub Jelen aacf0d
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
Jakub Jelen aacf0d
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
Jakub Jelen aacf0d
-	platform-pledge.o platform-tracing.o auditstub.o
Jakub Jelen aacf0d
+	platform-pledge.o platform-tracing.o auditstub.o libcrypto-compat.o
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
Jakub Jelen aacf0d
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/monitor.c.openssl openssh-7.4p1/monitor.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/monitor.c.openssl	2016-12-23 17:47:36.426817750 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/monitor.c	2016-12-23 17:47:36.433817753 +0100
Jakub Jelen 6cf9b8
@@ -636,9 +636,12 @@ mm_answer_moduli(int sock, Buffer *m)
Jakub Jelen aacf0d
 		return (0);
Jakub Jelen aacf0d
 	} else {
Jakub Jelen aacf0d
 		/* Send first bignum */
Jakub Jelen aacf0d
+		const BIGNUM *p, *g;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+		DH_get0_pqg(dh, &p, NULL, &g);
Jakub Jelen aacf0d
 		buffer_put_char(m, 1);
Jakub Jelen aacf0d
-		buffer_put_bignum2(m, dh->p);
Jakub Jelen aacf0d
-		buffer_put_bignum2(m, dh->g);
Jakub Jelen aacf0d
+		buffer_put_bignum2(m, p);
Jakub Jelen aacf0d
+		buffer_put_bignum2(m, g);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 		DH_free(dh);
Jakub Jelen aacf0d
 	}
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/openbsd-compat/openssl-compat.c.openssl openssh-7.4p1/openbsd-compat/openssl-compat.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/openbsd-compat/openssl-compat.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/openbsd-compat/openssl-compat.c	2016-12-23 17:47:36.433817753 +0100
Jakub Jelen 6cf9b8
@@ -70,12 +70,19 @@ ssh_compatible_openssl(long headerver, l
Jakub Jelen aacf0d
 void
Jakub Jelen aacf0d
 ssh_OpenSSL_add_all_algorithms(void)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
Jakub Jelen aacf0d
 	OpenSSL_add_all_algorithms();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Enable use of crypto hardware */
Jakub Jelen aacf0d
 	ENGINE_load_builtin_engines();
Jakub Jelen aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10001000L
Jakub Jelen aacf0d
 	ENGINE_register_all_complete();
Jakub Jelen aacf0d
+#endif
Jakub Jelen aacf0d
 	OPENSSL_config(NULL);
Jakub Jelen aacf0d
+#else
Jakub Jelen aacf0d
+	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS |
Jakub Jelen aacf0d
+	    OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, NULL);
Jakub Jelen aacf0d
+#endif
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 
Jakub Jelen 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
Jakub Jelen bdb932
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/configure.ac.openssl	2014-03-31 19:35:17.000000000 +0200
Jakub Jelen bdb932
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/configure.ac	2016-12-23 17:47:36.433817753 +0100
Jakub Jelen aacf0d
@@ -1829,6 +1829,7 @@ AC_RUN_IFELSE(
Jakub Jelen aacf0d
 	[AC_LANG_SOURCE([[
Jakub Jelen aacf0d
 #include <string.h>
Jakub Jelen aacf0d
 #include <openssl/opensslv.h>
Jakub Jelen aacf0d
+#include <openssl/crypto.h>
Jakub Jelen aacf0d
 int main(void) { exit(SSLeay() == OPENSSL_VERSION_NUMBER ? 0 : 1); }
Jakub Jelen aacf0d
 	]])],
Jakub Jelen aacf0d
 	[
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/regress/unittests/sshkey/test_file.c.openssl openssh-7.4p1/regress/unittests/sshkey/test_file.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/regress/unittests/sshkey/test_file.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/regress/unittests/sshkey/test_file.c	2016-12-23 17:47:36.433817753 +0100
Jakub Jelen aacf0d
@@ -46,6 +46,7 @@ sshkey_file_tests(void)
Jakub Jelen aacf0d
 	struct sshbuf *buf, *pw;
Jakub Jelen aacf0d
 	BIGNUM *a, *b, *c;
Jakub Jelen aacf0d
 	char *cp;
Jakub Jelen aacf0d
+	const BIGNUM *n, *p, *q, *g, *pub_key, *priv_key;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	TEST_START("load passphrase");
Jakub Jelen aacf0d
 	pw = load_text_file("pw");
Jakub Jelen aacf0d
@@ -58,7 +59,8 @@ sshkey_file_tests(void)
Jakub Jelen aacf0d
 	sshbuf_free(buf);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1, NULL);
Jakub Jelen aacf0d
 	a = load_bignum("rsa1_1.param.n");
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->n, a);
Jakub Jelen aacf0d
+	RSA_get0_key(k1->rsa, &n, NULL, NULL);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(n, a);
Jakub Jelen aacf0d
 	BN_free(a);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -109,9 +111,11 @@ sshkey_file_tests(void)
Jakub Jelen aacf0d
 	a = load_bignum("rsa_1.param.n");
Jakub Jelen aacf0d
 	b = load_bignum("rsa_1.param.p");
Jakub Jelen aacf0d
 	c = load_bignum("rsa_1.param.q");
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->n, a);
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->p, b);
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->rsa->q, c);
Jakub Jelen aacf0d
+	RSA_get0_key(k1->rsa, &n, NULL, NULL);
Jakub Jelen aacf0d
+	RSA_get0_factors(k1->rsa, &p, &q);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(n, a);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(p, b);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(q, c);
Jakub Jelen aacf0d
 	BN_free(a);
Jakub Jelen aacf0d
 	BN_free(b);
Jakub Jelen aacf0d
 	BN_free(c);
Jakub Jelen aacf0d
@@ -200,9 +204,11 @@ sshkey_file_tests(void)
Jakub Jelen aacf0d
 	a = load_bignum("dsa_1.param.g");
Jakub Jelen aacf0d
 	b = load_bignum("dsa_1.param.priv");
Jakub Jelen aacf0d
 	c = load_bignum("dsa_1.param.pub");
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->g, a);
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
Jakub Jelen aacf0d
-	ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
Jakub Jelen aacf0d
+	DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
Jakub Jelen aacf0d
+	DSA_get0_key(k1->dsa, &pub_key, &priv_key);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(g, a);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(priv_key, b);
Jakub Jelen aacf0d
+	ASSERT_BIGNUM_EQ(pub_key, c);
Jakub Jelen aacf0d
 	BN_free(a);
Jakub Jelen aacf0d
 	BN_free(b);
Jakub Jelen aacf0d
 	BN_free(c);
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c.openssl openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/regress/unittests/sshkey/test_sshkey.c	2016-12-23 17:47:36.433817753 +0100
Jakub Jelen aacf0d
@@ -197,9 +197,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	k1 = sshkey_new(KEY_RSA1);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
Jakub Jelen aacf0d
 	sshkey_free(k1);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -207,9 +204,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	k1 = sshkey_new(KEY_RSA);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
Jakub Jelen aacf0d
 	sshkey_free(k1);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -217,8 +211,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	k1 = sshkey_new(KEY_DSA);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
Jakub Jelen aacf0d
 	sshkey_free(k1);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -244,9 +236,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	k1 = sshkey_new_private(KEY_RSA);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->p, NULL);
Jakub Jelen aacf0d
 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
Jakub Jelen aacf0d
 	sshkey_free(k1);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
@@ -255,8 +244,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	k1 = sshkey_new_private(KEY_DSA);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
Jakub Jelen aacf0d
 	ASSERT_INT_EQ(sshkey_add_private(k1), 0);
Jakub Jelen aacf0d
 	sshkey_free(k1);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
@@ -295,18 +282,13 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(kr, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(kr->rsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(kr->rsa->n, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(kr->rsa->e, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(kr->rsa->p, NULL);
Jakub Jelen aacf0d
-	ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
Jakub Jelen aacf0d
+	ASSERT_INT_EQ(RSA_bits(kr->rsa), 1024);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	TEST_START("generate KEY_DSA");
Jakub Jelen aacf0d
 	ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(kd, NULL);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(kd->dsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(kd->dsa->g, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
@@ -333,9 +315,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(kr, k1);
Jakub Jelen aacf0d
 	ASSERT_INT_EQ(k1->type, KEY_RSA);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->rsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->n, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->rsa->e, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_EQ(k1->rsa->p, NULL);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	TEST_START("equal KEY_RSA/demoted KEY_RSA");
Jakub Jelen aacf0d
@@ -349,8 +328,6 @@ sshkey_tests(void)
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(kd, k1);
Jakub Jelen aacf0d
 	ASSERT_INT_EQ(k1->type, KEY_DSA);
Jakub Jelen aacf0d
 	ASSERT_PTR_NE(k1->dsa, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_NE(k1->dsa->g, NULL);
Jakub Jelen aacf0d
-	ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
Jakub Jelen aacf0d
 	TEST_DONE();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	TEST_START("equal KEY_DSA/demoted KEY_DSA");
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/rsa.c.openssl openssh-7.4p1/rsa.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/rsa.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/rsa.c	2016-12-23 17:47:36.434817754 +0100
Jakub Jelen aacf0d
@@ -76,11 +76,14 @@ rsa_public_encrypt(BIGNUM *out, BIGNUM *
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	u_char *inbuf = NULL, *outbuf = NULL;
Jakub Jelen aacf0d
 	int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
+	const BIGNUM *e, *n;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
Jakub Jelen aacf0d
+	RSA_get0_key(key, &n, &e, NULL);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+	if (BN_num_bits(e) < 2 || !BN_is_odd(e))
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	olen = BN_num_bytes(key->n);
Jakub Jelen aacf0d
+	olen = BN_num_bytes(n);
Jakub Jelen aacf0d
 	if ((outbuf = malloc(olen)) == NULL) {
Jakub Jelen aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
@@ -122,8 +125,11 @@ rsa_private_decrypt(BIGNUM *out, BIGNUM
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	u_char *inbuf = NULL, *outbuf = NULL;
Jakub Jelen aacf0d
 	int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
+	const BIGNUM *n;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+	RSA_get0_key(key, &n, NULL, NULL);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	olen = BN_num_bytes(key->n);
Jakub Jelen aacf0d
+	olen = BN_num_bytes(n);
Jakub Jelen aacf0d
 	if ((outbuf = malloc(olen)) == NULL) {
Jakub Jelen aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
@@ -157,31 +163,42 @@ rsa_private_decrypt(BIGNUM *out, BIGNUM
Jakub Jelen aacf0d
 	return r;
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-/* calculate p-1 and q-1 */
Jakub Jelen aacf0d
+/* calculate d mod p-1 and d mod q-1 */
Jakub Jelen aacf0d
 int
Jakub Jelen aacf0d
-rsa_generate_additional_parameters(RSA *rsa)
Jakub Jelen aacf0d
+rsa_generate_additional_parameters(RSA *rsa, BIGNUM *iqmp)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	BIGNUM *aux = NULL;
Jakub Jelen aacf0d
 	BN_CTX *ctx = NULL;
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
+	const BIGNUM *p, *q, *d;
Jakub Jelen aacf0d
+	BIGNUM *dmp1 = NULL, *dmq1 = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+	RSA_get0_factors(rsa, &p, &q);
Jakub Jelen aacf0d
+	RSA_get0_key(rsa, NULL, NULL, &d);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if ((ctx = BN_CTX_new()) == NULL)
Jakub Jelen aacf0d
-		return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
-	if ((aux = BN_new()) == NULL) {
Jakub Jelen aacf0d
+	if ((ctx = BN_CTX_new()) == NULL ||
Jakub Jelen aacf0d
+	    (aux = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (dmp1 = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (dmq1 = BN_new()) == NULL) {
Jakub Jelen aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if ((BN_sub(aux, rsa->q, BN_value_one()) == 0) ||
Jakub Jelen aacf0d
-	    (BN_mod(rsa->dmq1, rsa->d, aux, ctx) == 0) ||
Jakub Jelen aacf0d
-	    (BN_sub(aux, rsa->p, BN_value_one()) == 0) ||
Jakub Jelen aacf0d
-	    (BN_mod(rsa->dmp1, rsa->d, aux, ctx) == 0)) {
Jakub Jelen aacf0d
+	if ((BN_sub(aux, q, BN_value_one()) == 0) ||
Jakub Jelen aacf0d
+	    (BN_mod(dmq1, d, aux, ctx) == 0) ||
Jakub Jelen aacf0d
+	    (BN_sub(aux, p, BN_value_one()) == 0) ||
Jakub Jelen aacf0d
+	    (BN_mod(dmp1, d, aux, ctx) == 0) ||
Jakub Jelen aacf0d
+	    (RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp) == 0)) {
Jakub Jelen aacf0d
 		r = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
+	dmp1 = NULL;
Jakub Jelen aacf0d
+	dmq1 = NULL;
Jakub Jelen aacf0d
 	r = 0;
Jakub Jelen aacf0d
  out:
Jakub Jelen aacf0d
 	BN_clear_free(aux);
Jakub Jelen aacf0d
+	BN_clear_free(dmp1);
Jakub Jelen aacf0d
+	BN_clear_free(dmq1);
Jakub Jelen aacf0d
 	BN_CTX_free(ctx);
Jakub Jelen aacf0d
 	return r;
Jakub Jelen aacf0d
 }
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/rsa.h.openssl openssh-7.4p1/rsa.h
Jakub Jelen 6cf9b8
--- openssh-7.4p1/rsa.h.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/rsa.h	2016-12-23 17:47:36.434817754 +0100
Jakub Jelen aacf0d
@@ -21,6 +21,6 @@
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 int	 rsa_public_encrypt(BIGNUM *, BIGNUM *, RSA *);
Jakub Jelen aacf0d
 int	 rsa_private_decrypt(BIGNUM *, BIGNUM *, RSA *);
Jakub Jelen aacf0d
-int	 rsa_generate_additional_parameters(RSA *);
Jakub Jelen aacf0d
+int	 rsa_generate_additional_parameters(RSA *, BIGNUM *);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 #endif				/* RSA_H */
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-agent.c.openssl openssh-7.4p1/ssh-agent.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-agent.c.openssl	2016-12-23 17:47:36.428817751 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-agent.c	2016-12-23 17:47:36.434817754 +0100
Jakub Jelen 6cf9b8
@@ -258,12 +258,12 @@ process_request_identities(SocketEntry *
Jakub Jelen aacf0d
 	TAILQ_FOREACH(id, &tab->idlist, next) {
Jakub Jelen aacf0d
 		if (id->key->type == KEY_RSA1) {
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen aacf0d
+			const BIGNUM *r_n, *r_e;
Jakub Jelen aacf0d
+			RSA_get0_key(id->key->rsa, &r_n, &r_e, NULL);
Jakub Jelen aacf0d
 			if ((r = sshbuf_put_u32(msg,
Jakub Jelen aacf0d
-			    BN_num_bits(id->key->rsa->n))) != 0 ||
Jakub Jelen aacf0d
-			    (r = sshbuf_put_bignum1(msg,
Jakub Jelen aacf0d
-			    id->key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-			    (r = sshbuf_put_bignum1(msg,
Jakub Jelen aacf0d
-			    id->key->rsa->n)) != 0)
Jakub Jelen aacf0d
+			    BN_num_bits(r_n))) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum1(msg, r_e)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum1(msg, r_n)) != 0)
Jakub Jelen aacf0d
 				fatal("%s: buffer error: %s",
Jakub Jelen aacf0d
 				    __func__, ssh_err(r));
Jakub Jelen aacf0d
 #endif
Jakub Jelen 6cf9b8
@@ -302,6 +302,7 @@ process_authentication_challenge1(Socket
Jakub Jelen aacf0d
 	struct sshbuf *msg;
Jakub Jelen aacf0d
 	struct ssh_digest_ctx *md;
Jakub Jelen aacf0d
 	struct sshkey *key;
Jakub Jelen aacf0d
+	BIGNUM *r_n = NULL, *r_e = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if ((msg = sshbuf_new()) == NULL)
Jakub Jelen aacf0d
 		fatal("%s: sshbuf_new failed", __func__);
Jakub Jelen 6cf9b8
@@ -310,11 +311,16 @@ process_authentication_challenge1(Socket
Jakub Jelen aacf0d
 	if ((challenge = BN_new()) == NULL)
Jakub Jelen aacf0d
 		fatal("%s: BN_new failed", __func__);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(e->request, challenge)))
Jakub Jelen aacf0d
+	if ((r_n = BN_new()) == NULL || (r_e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(e->request, r_e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(e->request, r_n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(e->request, challenge)) ||
Jakub Jelen aacf0d
+	    RSA_set0_key(key->rsa, r_n, r_e, NULL) == 0) {
Jakub Jelen aacf0d
+		BN_free(r_n);
Jakub Jelen aacf0d
+		BN_free(r_e);
Jakub Jelen aacf0d
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Only protocol 1.1 is supported */
Jakub Jelen aacf0d
 	if (sshbuf_len(e->request) == 0)
Jakub Jelen 6cf9b8
@@ -450,6 +456,7 @@ process_remove_identity(SocketEntry *e,
Jakub Jelen aacf0d
 	u_char *blob;
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen aacf0d
 	u_int bits;
Jakub Jelen aacf0d
+	BIGNUM *r_n = NULL, *r_e = NULL;
Jakub Jelen aacf0d
 #endif /* WITH_SSH1 */
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	switch (version) {
Jakub Jelen 6cf9b8
@@ -459,10 +466,15 @@ process_remove_identity(SocketEntry *e,
Jakub Jelen aacf0d
 			error("%s: sshkey_new failed", __func__);
Jakub Jelen aacf0d
 			return;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
Jakub Jelen aacf0d
+		if ((r_n = BN_new()) == NULL || (r_e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+		    (r = sshbuf_get_u32(e->request, &bits)) != 0 ||
Jakub Jelen aacf0d
+		    (r = sshbuf_get_bignum1(e->request, r_e)) != 0 ||
Jakub Jelen aacf0d
+		    (r = sshbuf_get_bignum1(e->request, r_n)) != 0 ||
Jakub Jelen aacf0d
+		    RSA_set0_key(key->rsa, r_n, r_e, NULL) == 0) {
Jakub Jelen aacf0d
+			BN_free(r_n);
Jakub Jelen aacf0d
+			BN_free(r_e);
Jakub Jelen aacf0d
 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 		if (bits != sshkey_size(key))
Jakub Jelen aacf0d
 			logit("Warning: identity keysize mismatch: "
Jakub Jelen 6cf9b8
@@ -565,23 +577,38 @@ agent_decode_rsa1(struct sshbuf *m, stru
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	struct sshkey *k = NULL;
Jakub Jelen aacf0d
 	int r = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
+	BIGNUM *n = NULL, *e = NULL, *d = NULL,
Jakub Jelen aacf0d
+	    *iqmp = NULL, *q = NULL, *p = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	*kp = NULL;
Jakub Jelen aacf0d
 	if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
Jakub Jelen aacf0d
 		return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if ((r = sshbuf_get_u32(m, NULL)) != 0 ||		/* ignored */
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
+	if ((n = BN_new()) == NULL || (e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (d = BN_new()) == NULL || (iqmp = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (q = BN_new()) == NULL || (p = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_u32(m, NULL)) != 0 ||		/* ignored */
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(m, n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(m, e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(m, d)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(m, iqmp)) != 0 ||
Jakub Jelen aacf0d
 	    /* SSH1 and SSL have p and q swapped */
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 ||	/* p */
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) 	/* q */
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(m, q)) != 0 ||	/* p */
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(m, p)) != 0 || 	/* q */
Jakub Jelen aacf0d
+	    RSA_set0_key(k->rsa, n, e, d) == 0 ||
Jakub Jelen aacf0d
+	    RSA_set0_factors(k->rsa, p, q) == 0 ||
Jakub Jelen aacf0d
+	    RSA_set0_crt_params(k->rsa, NULL, NULL, iqmp) == 0) {
Jakub Jelen aacf0d
+		BN_free(n);
Jakub Jelen aacf0d
+		BN_free(e);
Jakub Jelen aacf0d
+		BN_free(d);
Jakub Jelen aacf0d
+		BN_free(p);
Jakub Jelen aacf0d
+		BN_free(q);
Jakub Jelen aacf0d
+		BN_free(iqmp);
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Generate additional parameters */
Jakub Jelen aacf0d
-	if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
Jakub Jelen aacf0d
+	if ((r = rsa_generate_additional_parameters(k->rsa, NULL)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	/* enable blinding */
Jakub Jelen aacf0d
 	if (RSA_blinding_on(k->rsa, NULL) != 1) {
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/sshconnect1.c.openssl openssh-7.4p1/sshconnect1.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/sshconnect1.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/sshconnect1.c	2016-12-23 17:47:36.434817754 +0100
Jakub Jelen aacf0d
@@ -70,6 +70,7 @@ try_agent_authentication(void)
Jakub Jelen aacf0d
 	u_char response[16];
Jakub Jelen aacf0d
 	size_t i;
Jakub Jelen aacf0d
 	BIGNUM *challenge;
Jakub Jelen aacf0d
+	const BIGNUM *n;
Jakub Jelen aacf0d
 	struct ssh_identitylist *idlist = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Get connection to the agent. */
Jakub Jelen aacf0d
@@ -96,8 +97,9 @@ try_agent_authentication(void)
Jakub Jelen aacf0d
 		    idlist->comments[i]);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 		/* Tell the server that we are willing to authenticate using this key. */
Jakub Jelen aacf0d
+		RSA_get0_key(idlist->keys[i]->rsa, &n, NULL, NULL);
Jakub Jelen aacf0d
 		packet_start(SSH_CMSG_AUTH_RSA);
Jakub Jelen aacf0d
-		packet_put_bignum(idlist->keys[i]->rsa->n);
Jakub Jelen aacf0d
+		packet_put_bignum((BIGNUM *)n);
Jakub Jelen aacf0d
 		packet_send();
Jakub Jelen aacf0d
 		packet_write_wait();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -220,6 +222,7 @@ static int
Jakub Jelen aacf0d
 try_rsa_authentication(int idx)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	BIGNUM *challenge;
Jakub Jelen aacf0d
+	const BIGNUM *n;
Jakub Jelen aacf0d
 	Key *public, *private;
Jakub Jelen aacf0d
 	char buf[300], *passphrase = NULL, *comment, *authfile;
Jakub Jelen aacf0d
 	int i, perm_ok = 1, type, quit;
Jakub Jelen aacf0d
@@ -231,8 +234,9 @@ try_rsa_authentication(int idx)
Jakub Jelen aacf0d
 	debug("Trying RSA authentication with key '%.100s'", comment);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Tell the server that we are willing to authenticate using this key. */
Jakub Jelen aacf0d
+	RSA_get0_key(public->rsa, &n, NULL, NULL);
Jakub Jelen aacf0d
 	packet_start(SSH_CMSG_AUTH_RSA);
Jakub Jelen aacf0d
-	packet_put_bignum(public->rsa->n);
Jakub Jelen aacf0d
+	packet_put_bignum((BIGNUM *)n);
Jakub Jelen aacf0d
 	packet_send();
Jakub Jelen aacf0d
 	packet_write_wait();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -348,15 +352,17 @@ try_rhosts_rsa_authentication(const char
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	int type;
Jakub Jelen aacf0d
 	BIGNUM *challenge;
Jakub Jelen aacf0d
+	const BIGNUM *n, *e;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Tell the server that we are willing to authenticate using this key. */
Jakub Jelen aacf0d
+	RSA_get0_key(host_key->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
 	packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Jakub Jelen aacf0d
 	packet_put_cstring(local_user);
Jakub Jelen aacf0d
-	packet_put_int(BN_num_bits(host_key->rsa->n));
Jakub Jelen aacf0d
-	packet_put_bignum(host_key->rsa->e);
Jakub Jelen aacf0d
-	packet_put_bignum(host_key->rsa->n);
Jakub Jelen aacf0d
+	packet_put_int(BN_num_bits(n));
Jakub Jelen aacf0d
+	packet_put_bignum((BIGNUM *)e);
Jakub Jelen aacf0d
+	packet_put_bignum((BIGNUM *)n);
Jakub Jelen aacf0d
 	packet_send();
Jakub Jelen aacf0d
 	packet_write_wait();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
@@ -502,6 +508,8 @@ ssh_kex(char *host, struct sockaddr *hos
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
 	int i;
Jakub Jelen aacf0d
 	BIGNUM *key;
Jakub Jelen aacf0d
+	BIGNUM *server_n = NULL, *server_e = NULL,
Jakub Jelen aacf0d
+	    *host_n = NULL, *host_e = NULL;
Jakub Jelen aacf0d
 	Key *host_key, *server_key;
Jakub Jelen aacf0d
 	int bits, rbits;
Jakub Jelen aacf0d
 	int ssh_cipher_default = SSH_CIPHER_3DES;
Jakub Jelen 6cf9b8
@@ -522,10 +530,14 @@ ssh_kex(char *host, struct sockaddr *hos
Jakub Jelen aacf0d
 	/* Get the public key. */
Jakub Jelen aacf0d
 	server_key = key_new(KEY_RSA1);
Jakub Jelen aacf0d
 	bits = packet_get_int();
Jakub Jelen aacf0d
-	packet_get_bignum(server_key->rsa->e);
Jakub Jelen aacf0d
-	packet_get_bignum(server_key->rsa->n);
Jakub Jelen aacf0d
+	if ((server_e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (server_n = BN_new()) == NULL)
Jakub Jelen aacf0d
+		fatal("BN_new() failed");
Jakub Jelen aacf0d
+	packet_get_bignum(server_e);
Jakub Jelen aacf0d
+	packet_get_bignum(server_n);
Jakub Jelen aacf0d
+	RSA_set0_key(server_key->rsa, server_n, server_e, NULL);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	rbits = BN_num_bits(server_key->rsa->n);
Jakub Jelen aacf0d
+	rbits = BN_num_bits(server_n);
Jakub Jelen aacf0d
 	if (bits != rbits) {
Jakub Jelen aacf0d
 		logit("Warning: Server lies about size of server public key: "
Jakub Jelen aacf0d
 		    "actual size is %d bits vs. announced %d.", rbits, bits);
Jakub Jelen 6cf9b8
@@ -534,10 +546,14 @@ ssh_kex(char *host, struct sockaddr *hos
Jakub Jelen aacf0d
 	/* Get the host key. */
Jakub Jelen aacf0d
 	host_key = key_new(KEY_RSA1);
Jakub Jelen aacf0d
 	bits = packet_get_int();
Jakub Jelen aacf0d
-	packet_get_bignum(host_key->rsa->e);
Jakub Jelen aacf0d
-	packet_get_bignum(host_key->rsa->n);
Jakub Jelen aacf0d
+	if ((host_e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (host_n = BN_new()) == NULL)
Jakub Jelen aacf0d
+		fatal("BN_new() failed");
Jakub Jelen aacf0d
+	packet_get_bignum(host_e);
Jakub Jelen aacf0d
+	packet_get_bignum(host_n);
Jakub Jelen aacf0d
+	RSA_set0_key(host_key->rsa, host_n, host_e, NULL);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	rbits = BN_num_bits(host_key->rsa->n);
Jakub Jelen aacf0d
+	rbits = BN_num_bits(host_n);
Jakub Jelen aacf0d
 	if (bits != rbits) {
Jakub Jelen aacf0d
 		logit("Warning: Server lies about size of server host key: "
Jakub Jelen aacf0d
 		    "actual size is %d bits vs. announced %d.", rbits, bits);
Jakub Jelen 6cf9b8
@@ -553,14 +569,14 @@ ssh_kex(char *host, struct sockaddr *hos
Jakub Jelen aacf0d
 	packet_check_eom();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	debug("Received server public key (%d bits) and host key (%d bits).",
Jakub Jelen aacf0d
-	    BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Jakub Jelen aacf0d
+	    BN_num_bits(server_n), BN_num_bits(host_n));
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (verify_host_key(host, hostaddr, host_key) == -1)
Jakub Jelen aacf0d
 		fatal("Host key verification failed.");
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Jakub Jelen aacf0d
+	derive_ssh1_session_id(host_n, server_n, cookie, session_id);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/*
Jakub Jelen aacf0d
 	 * Generate an encryption key for the session.   The key is a 256 bit
Jakub Jelen 6cf9b8
@@ -595,14 +611,14 @@ ssh_kex(char *host, struct sockaddr *hos
Jakub Jelen aacf0d
 	 * Encrypt the integer using the public key and host key of the
Jakub Jelen aacf0d
 	 * server (key with smaller modulus first).
Jakub Jelen aacf0d
 	 */
Jakub Jelen aacf0d
-	if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Jakub Jelen aacf0d
+	if (BN_cmp(server_n, host_n) < 0) {
Jakub Jelen aacf0d
 		/* Public key has smaller modulus. */
Jakub Jelen aacf0d
-		if (BN_num_bits(host_key->rsa->n) <
Jakub Jelen aacf0d
-		    BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
Jakub Jelen aacf0d
+		if (BN_num_bits(host_n) <
Jakub Jelen aacf0d
+		    BN_num_bits(server_n) + SSH_KEY_BITS_RESERVED) {
Jakub Jelen aacf0d
 			fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Jakub Jelen aacf0d
 			    "SSH_KEY_BITS_RESERVED %d",
Jakub Jelen aacf0d
-			    BN_num_bits(host_key->rsa->n),
Jakub Jelen aacf0d
-			    BN_num_bits(server_key->rsa->n),
Jakub Jelen aacf0d
+			    BN_num_bits(host_n),
Jakub Jelen aacf0d
+			    BN_num_bits(server_n),
Jakub Jelen aacf0d
 			    SSH_KEY_BITS_RESERVED);
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 		if (rsa_public_encrypt(key, key, server_key->rsa) != 0 ||
Jakub Jelen 6cf9b8
@@ -610,12 +626,12 @@ ssh_kex(char *host, struct sockaddr *hos
Jakub Jelen aacf0d
 			fatal("%s: rsa_public_encrypt failed", __func__);
Jakub Jelen aacf0d
 	} else {
Jakub Jelen aacf0d
 		/* Host key has smaller modulus (or they are equal). */
Jakub Jelen aacf0d
-		if (BN_num_bits(server_key->rsa->n) <
Jakub Jelen aacf0d
-		    BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
Jakub Jelen aacf0d
+		if (BN_num_bits(server_n) <
Jakub Jelen aacf0d
+		    BN_num_bits(host_n) + SSH_KEY_BITS_RESERVED) {
Jakub Jelen aacf0d
 			fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Jakub Jelen aacf0d
 			    "SSH_KEY_BITS_RESERVED %d",
Jakub Jelen aacf0d
-			    BN_num_bits(server_key->rsa->n),
Jakub Jelen aacf0d
-			    BN_num_bits(host_key->rsa->n),
Jakub Jelen aacf0d
+			    BN_num_bits(server_n),
Jakub Jelen aacf0d
+			    BN_num_bits(host_n),
Jakub Jelen aacf0d
 			    SSH_KEY_BITS_RESERVED);
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 		if (rsa_public_encrypt(key, key, host_key->rsa) != 0 ||
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/sshconnect2.c.openssl openssh-7.4p1/sshconnect2.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/sshconnect2.c.openssl	2016-12-23 17:47:36.423817749 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/sshconnect2.c	2016-12-23 17:47:36.434817754 +0100
Jakub Jelen aacf0d
@@ -299,6 +299,7 @@ ssh_kex2(char *host, struct sockaddr *ho
Jakub Jelen aacf0d
 	packet_send();
Jakub Jelen aacf0d
 	packet_write_wait();
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
+	/* XXX free myproposal ?? */
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 /*
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/sshconnect.c.openssl openssh-7.4p1/sshconnect.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/sshconnect.c.openssl	2016-12-23 17:47:36.397817738 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/sshconnect.c	2016-12-23 17:47:36.435817754 +0100
Jakub Jelen 6cf9b8
@@ -1369,6 +1369,7 @@ ssh_login(Sensitive *sensitive, const ch
Jakub Jelen aacf0d
 	char *server_user, *local_user;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	local_user = xstrdup(pw->pw_name);
Jakub Jelen aacf0d
+	free(pw);
Jakub Jelen aacf0d
 	server_user = options.user ? options.user : local_user;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Convert the user-supplied hostname into all lowercase. */
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh.c.openssl openssh-7.4p1/ssh.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh.c.openssl	2016-12-23 17:47:36.422817748 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh.c	2016-12-23 17:47:36.435817754 +0100
Jakub Jelen 6cf9b8
@@ -532,7 +532,9 @@ main(int ac, char **av)
Jakub Jelen aacf0d
 	sanitise_stdfd();
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	__progname = ssh_get_progname(av[0]);
Jakub Jelen aacf0d
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
Jakub Jelen aacf0d
         SSLeay_add_all_algorithms();
Jakub Jelen aacf0d
+#endif
Jakub Jelen aacf0d
 	if (access("/etc/system-fips", F_OK) == 0)
Jakub Jelen aacf0d
 		if (! FIPSCHECK_verify(NULL, NULL)){
Jakub Jelen aacf0d
 			if (FIPS_mode())
Jakub Jelen aacf0d
@@ -1247,6 +1249,7 @@ main(int ac, char **av)
Jakub Jelen aacf0d
 		free(cp);
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	free(conn_hash_hex);
Jakub Jelen aacf0d
+	free(host_arg);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (config_test) {
Jakub Jelen aacf0d
 		dump_client_config(&options, host);
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/sshd.c.openssl openssh-7.4p1/sshd.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/sshd.c.openssl	2016-12-23 17:47:36.428817751 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/sshd.c	2016-12-23 17:47:36.435817754 +0100
Jakub Jelen 6cf9b8
@@ -1483,7 +1483,7 @@ main(int ac, char **av)
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 	__progname = ssh_get_progname(av[0]);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-        SSLeay_add_all_algorithms();
Jakub Jelen aacf0d
+	OpenSSL_add_all_algorithms();
Jakub Jelen aacf0d
 	if (access("/etc/system-fips", F_OK) == 0)
Jakub Jelen aacf0d
 		if (! FIPSCHECK_verify(NULL, NULL)) {
Jakub Jelen aacf0d
 			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-dss.c.openssl openssh-7.4p1/ssh-dss.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-dss.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-dss.c	2016-12-23 17:47:36.435817754 +0100
Jakub Jelen aacf0d
@@ -55,6 +55,7 @@ ssh_dss_sign(const struct sshkey *key, u
Jakub Jelen aacf0d
 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
Jakub Jelen aacf0d
 	struct sshbuf *b = NULL;
Jakub Jelen aacf0d
 	int ret = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
+	const BIGNUM *r, *s;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (lenp != NULL)
Jakub Jelen aacf0d
 		*lenp = 0;
Jakub Jelen aacf0d
@@ -76,15 +77,16 @@ ssh_dss_sign(const struct sshkey *key, u
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	rlen = BN_num_bytes(sig->r);
Jakub Jelen aacf0d
-	slen = BN_num_bytes(sig->s);
Jakub Jelen aacf0d
+	DSA_SIG_get0(sig, &r, &s);
Jakub Jelen aacf0d
+	rlen = BN_num_bytes(r);
Jakub Jelen aacf0d
+	slen = BN_num_bytes(s);
Jakub Jelen aacf0d
 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	explicit_bzero(sigblob, SIGBLOB_LEN);
Jakub Jelen aacf0d
-	BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
Jakub Jelen aacf0d
-	BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
Jakub Jelen aacf0d
+	BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
Jakub Jelen aacf0d
+	BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (compat & SSH_BUG_SIGBLOB) {
Jakub Jelen aacf0d
 		if (sigp != NULL) {
Jakub Jelen aacf0d
@@ -137,6 +139,7 @@ ssh_dss_verify(const struct sshkey *key,
Jakub Jelen aacf0d
 	int ret = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
 	struct sshbuf *b = NULL;
Jakub Jelen aacf0d
 	char *ktype = NULL;
Jakub Jelen aacf0d
+	BIGNUM *r = NULL, *s = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (key == NULL || key->dsa == NULL ||
Jakub Jelen aacf0d
 	    sshkey_type_plain(key->type) != KEY_DSA ||
Jakub Jelen aacf0d
@@ -177,16 +180,19 @@ ssh_dss_verify(const struct sshkey *key,
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* parse signature */
Jakub Jelen aacf0d
 	if ((sig = DSA_SIG_new()) == NULL ||
Jakub Jelen aacf0d
-	    (sig->r = BN_new()) == NULL ||
Jakub Jelen aacf0d
-	    (sig->s = BN_new()) == NULL) {
Jakub Jelen aacf0d
+	    (r = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (s = BN_new()) == NULL) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
Jakub Jelen aacf0d
-	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
Jakub Jelen aacf0d
+	if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
Jakub Jelen aacf0d
+	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL) ||
Jakub Jelen aacf0d
+	    (DSA_SIG_set0(sig, r, s) == 0)) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
+	r = NULL;
Jakub Jelen aacf0d
+	s = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* sha1 the data */
Jakub Jelen aacf0d
 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
Jakub Jelen aacf0d
@@ -207,8 +213,9 @@ ssh_dss_verify(const struct sshkey *key,
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
  out:
Jakub Jelen aacf0d
 	explicit_bzero(digest, sizeof(digest));
Jakub Jelen aacf0d
-	if (sig != NULL)
Jakub Jelen aacf0d
-		DSA_SIG_free(sig);
Jakub Jelen aacf0d
+	BN_free(r);
Jakub Jelen aacf0d
+	BN_free(s);
Jakub Jelen aacf0d
+	DSA_SIG_free(sig);
Jakub Jelen aacf0d
 	sshbuf_free(b);
Jakub Jelen aacf0d
 	free(ktype);
Jakub Jelen aacf0d
 	if (sigblob != NULL) {
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-ecdsa.c.openssl openssh-7.4p1/ssh-ecdsa.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-ecdsa.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-ecdsa.c	2016-12-23 17:47:36.436817754 +0100
Jakub Jelen aacf0d
@@ -54,6 +54,7 @@ ssh_ecdsa_sign(const struct sshkey *key,
Jakub Jelen aacf0d
 	size_t len, dlen;
Jakub Jelen aacf0d
 	struct sshbuf *b = NULL, *bb = NULL;
Jakub Jelen aacf0d
 	int ret = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
+	const BIGNUM *r, *s;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (lenp != NULL)
Jakub Jelen aacf0d
 		*lenp = 0;
Jakub Jelen aacf0d
@@ -80,8 +81,9 @@ ssh_ecdsa_sign(const struct sshkey *key,
Jakub Jelen aacf0d
 		ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
Jakub Jelen aacf0d
-	    (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
Jakub Jelen aacf0d
+	ECDSA_SIG_get0(sig, &r, &s);
Jakub Jelen aacf0d
+	if ((ret = sshbuf_put_bignum2(bb, r)) != 0 ||
Jakub Jelen aacf0d
+	    (ret = sshbuf_put_bignum2(bb, s)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
Jakub Jelen aacf0d
 	    (ret = sshbuf_put_stringb(b, bb)) != 0)
Jakub Jelen aacf0d
@@ -119,6 +121,7 @@ ssh_ecdsa_verify(const struct sshkey *ke
Jakub Jelen aacf0d
 	int ret = SSH_ERR_INTERNAL_ERROR;
Jakub Jelen aacf0d
 	struct sshbuf *b = NULL, *sigbuf = NULL;
Jakub Jelen aacf0d
 	char *ktype = NULL;
Jakub Jelen aacf0d
+	BIGNUM *r = NULL, *s = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (key == NULL || key->ecdsa == NULL ||
Jakub Jelen aacf0d
 	    sshkey_type_plain(key->type) != KEY_ECDSA ||
Jakub Jelen aacf0d
@@ -147,15 +150,23 @@ ssh_ecdsa_verify(const struct sshkey *ke
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* parse signature */
Jakub Jelen aacf0d
-	if ((sig = ECDSA_SIG_new()) == NULL) {
Jakub Jelen aacf0d
+	if ((sig = ECDSA_SIG_new()) == NULL ||
Jakub Jelen aacf0d
+	    (r = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (s = BN_new()) == NULL) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
Jakub Jelen aacf0d
-	    sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
Jakub Jelen aacf0d
+	if (sshbuf_get_bignum2(sigbuf, r) != 0 ||
Jakub Jelen aacf0d
+	    sshbuf_get_bignum2(sigbuf, s) != 0) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_INVALID_FORMAT;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
+	if (ECDSA_SIG_set0(sig, r, s) == 0) {
Jakub Jelen aacf0d
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
+		goto out;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
+	r = NULL;
Jakub Jelen aacf0d
+	s = NULL;
Jakub Jelen aacf0d
 	if (sshbuf_len(sigbuf) != 0) {
Jakub Jelen aacf0d
 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
@@ -180,8 +191,9 @@ ssh_ecdsa_verify(const struct sshkey *ke
Jakub Jelen aacf0d
 	explicit_bzero(digest, sizeof(digest));
Jakub Jelen aacf0d
 	sshbuf_free(sigbuf);
Jakub Jelen aacf0d
 	sshbuf_free(b);
Jakub Jelen aacf0d
-	if (sig != NULL)
Jakub Jelen aacf0d
-		ECDSA_SIG_free(sig);
Jakub Jelen aacf0d
+	BN_free(r);
Jakub Jelen aacf0d
+	BN_free(s);
Jakub Jelen aacf0d
+	ECDSA_SIG_free(sig);
Jakub Jelen aacf0d
 	free(ktype);
Jakub Jelen aacf0d
 	return ret;
Jakub Jelen aacf0d
 }
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/sshkey.c.openssl openssh-7.4p1/sshkey.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/sshkey.c.openssl	2016-12-23 17:47:36.424817749 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/sshkey.c	2016-12-23 17:47:56.652826110 +0100
Jakub Jelen 6cf9b8
@@ -275,10 +275,10 @@ sshkey_size(const struct sshkey *k)
Jakub Jelen aacf0d
 	case KEY_RSA1:
Jakub Jelen aacf0d
 	case KEY_RSA:
Jakub Jelen aacf0d
 	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-		return BN_num_bits(k->rsa->n);
Jakub Jelen aacf0d
+		return RSA_bits(k->rsa);
Jakub Jelen aacf0d
 	case KEY_DSA:
Jakub Jelen aacf0d
 	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-		return BN_num_bits(k->dsa->p);
Jakub Jelen aacf0d
+		return DSA_bits(k->dsa);
Jakub Jelen aacf0d
 	case KEY_ECDSA:
Jakub Jelen aacf0d
 	case KEY_ECDSA_CERT:
Jakub Jelen aacf0d
 		return sshkey_curve_nid_to_bits(k->ecdsa_nid);
Jakub Jelen 6cf9b8
@@ -311,11 +311,17 @@ sshkey_is_private(const struct sshkey *k
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
       case KEY_RSA_CERT:
Jakub Jelen aacf0d
       case KEY_RSA1:
Jakub Jelen aacf0d
-      case KEY_RSA:
Jakub Jelen aacf0d
-              return k->rsa->d != NULL;
Jakub Jelen aacf0d
+      case KEY_RSA: {
Jakub Jelen aacf0d
+              const BIGNUM *d;
Jakub Jelen aacf0d
+              RSA_get0_key(k->rsa, NULL, NULL, &d);
Jakub Jelen aacf0d
+              return d != NULL;
Jakub Jelen aacf0d
+          }
Jakub Jelen aacf0d
       case KEY_DSA_CERT:
Jakub Jelen aacf0d
-      case KEY_DSA:
Jakub Jelen aacf0d
-              return k->dsa->priv_key != NULL;
Jakub Jelen aacf0d
+      case KEY_DSA: {
Jakub Jelen aacf0d
+              const BIGNUM *priv_key;
Jakub Jelen aacf0d
+              DSA_get0_key(k->dsa, NULL, &priv_key);
Jakub Jelen aacf0d
+              return priv_key != NULL;
Jakub Jelen aacf0d
+          }
Jakub Jelen aacf0d
 #ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
       case KEY_ECDSA_CERT:
Jakub Jelen aacf0d
       case KEY_ECDSA:
Jakub Jelen 6cf9b8
@@ -505,11 +511,7 @@ sshkey_new(int type)
Jakub Jelen aacf0d
 	case KEY_RSA1:
Jakub Jelen aacf0d
 	case KEY_RSA:
Jakub Jelen aacf0d
 	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-		if ((rsa = RSA_new()) == NULL ||
Jakub Jelen aacf0d
-		    (rsa->n = BN_new()) == NULL ||
Jakub Jelen aacf0d
-		    (rsa->e = BN_new()) == NULL) {
Jakub Jelen aacf0d
-			if (rsa != NULL)
Jakub Jelen aacf0d
-				RSA_free(rsa);
Jakub Jelen aacf0d
+		if ((rsa = RSA_new()) == NULL) {
Jakub Jelen aacf0d
 			free(k);
Jakub Jelen aacf0d
 			return NULL;
Jakub Jelen aacf0d
 		}
Jakub Jelen 6cf9b8
@@ -517,13 +519,7 @@ sshkey_new(int type)
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEY_DSA:
Jakub Jelen aacf0d
 	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-		if ((dsa = DSA_new()) == NULL ||
Jakub Jelen aacf0d
-		    (dsa->p = BN_new()) == NULL ||
Jakub Jelen aacf0d
-		    (dsa->q = BN_new()) == NULL ||
Jakub Jelen aacf0d
-		    (dsa->g = BN_new()) == NULL ||
Jakub Jelen aacf0d
-		    (dsa->pub_key = BN_new()) == NULL) {
Jakub Jelen aacf0d
-			if (dsa != NULL)
Jakub Jelen aacf0d
-				DSA_free(dsa);
Jakub Jelen aacf0d
+		if ((dsa = DSA_new()) == NULL) {
Jakub Jelen aacf0d
 			free(k);
Jakub Jelen aacf0d
 			return NULL;
Jakub Jelen aacf0d
 		}
Jakub Jelen 6cf9b8
@@ -563,21 +559,10 @@ sshkey_add_private(struct sshkey *k)
Jakub Jelen aacf0d
 	case KEY_RSA1:
Jakub Jelen aacf0d
 	case KEY_RSA:
Jakub Jelen aacf0d
 	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-#define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
Jakub Jelen aacf0d
-		if (bn_maybe_alloc_failed(k->rsa->d) ||
Jakub Jelen aacf0d
-		    bn_maybe_alloc_failed(k->rsa->iqmp) ||
Jakub Jelen aacf0d
-		    bn_maybe_alloc_failed(k->rsa->q) ||
Jakub Jelen aacf0d
-		    bn_maybe_alloc_failed(k->rsa->p) ||
Jakub Jelen aacf0d
-		    bn_maybe_alloc_failed(k->rsa->dmq1) ||
Jakub Jelen aacf0d
-		    bn_maybe_alloc_failed(k->rsa->dmp1))
Jakub Jelen aacf0d
-			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEY_DSA:
Jakub Jelen aacf0d
 	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-		if (bn_maybe_alloc_failed(k->dsa->priv_key))
Jakub Jelen aacf0d
-			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
-#undef bn_maybe_alloc_failed
Jakub Jelen aacf0d
 	case KEY_ECDSA:
Jakub Jelen aacf0d
 	case KEY_ECDSA_CERT:
Jakub Jelen aacf0d
 		/* Cannot do anything until we know the group */
Jakub Jelen 6cf9b8
@@ -696,17 +681,31 @@ sshkey_equal_public(const struct sshkey
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
 	case KEY_RSA1:
Jakub Jelen aacf0d
 	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-	case KEY_RSA:
Jakub Jelen aacf0d
-		return a->rsa != NULL && b->rsa != NULL &&
Jakub Jelen aacf0d
-		    BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
Jakub Jelen aacf0d
-		    BN_cmp(a->rsa->n, b->rsa->n) == 0;
Jakub Jelen aacf0d
+	case KEY_RSA: {
Jakub Jelen aacf0d
+			const BIGNUM *a_e, *a_n, *b_e, *b_n;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (a->rsa == NULL || b->rsa == NULL)
Jakub Jelen aacf0d
+				return 0;
Jakub Jelen aacf0d
+			RSA_get0_key(a->rsa, &a_n, &a_e, NULL);
Jakub Jelen aacf0d
+			RSA_get0_key(b->rsa, &b_n, &b_e, NULL);
Jakub Jelen aacf0d
+			return BN_cmp(a_e, b_e) == 0 && BN_cmp(a_n, b_n) == 0;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-	case KEY_DSA:
Jakub Jelen aacf0d
-		return a->dsa != NULL && b->dsa != NULL &&
Jakub Jelen aacf0d
-		    BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
Jakub Jelen aacf0d
-		    BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
Jakub Jelen aacf0d
-		    BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
Jakub Jelen aacf0d
-		    BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Jakub Jelen aacf0d
+	case KEY_DSA: {
Jakub Jelen aacf0d
+			const BIGNUM *a_p, *a_q, *a_g, *a_pub_key;
Jakub Jelen aacf0d
+			const BIGNUM *b_p, *b_q, *b_g, *b_pub_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (a->dsa == NULL || b->dsa == NULL)
Jakub Jelen aacf0d
+				return 0;
Jakub Jelen aacf0d
+			DSA_get0_pqg(a->dsa, &a_p, &a_q, &a_g);
Jakub Jelen aacf0d
+			DSA_get0_key(a->dsa, &a_pub_key, NULL);
Jakub Jelen aacf0d
+			DSA_get0_pqg(b->dsa, &b_p, &b_q, &b_g);
Jakub Jelen aacf0d
+			DSA_get0_key(b->dsa, &b_pub_key, NULL);
Jakub Jelen aacf0d
+			return BN_cmp(a_p, b_p) == 0 &&
Jakub Jelen aacf0d
+			    BN_cmp(a_q, b_q) == 0 &&
Jakub Jelen aacf0d
+			    BN_cmp(a_g, b_g) == 0 &&
Jakub Jelen aacf0d
+			    BN_cmp(a_pub_key, b_pub_key) == 0;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 # ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
 	case KEY_ECDSA_CERT:
Jakub Jelen aacf0d
 	case KEY_ECDSA:
Jakub Jelen 6cf9b8
@@ -781,15 +780,21 @@ to_blob_buf(const struct sshkey *key, st
Jakub Jelen aacf0d
 			return ret;
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
-	case KEY_DSA:
Jakub Jelen aacf0d
-		if (key->dsa == NULL)
Jakub Jelen aacf0d
-			return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
-		if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
Jakub Jelen aacf0d
-			return ret;
Jakub Jelen aacf0d
+	case KEY_DSA: {
Jakub Jelen aacf0d
+			const BIGNUM *p, *q, *g, *pub_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (key->dsa == NULL)
Jakub Jelen aacf0d
+				return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			DSA_get0_pqg(key->dsa, &p, &q, &g);
Jakub Jelen aacf0d
+			DSA_get0_key(key->dsa, &pub_key, NULL);
Jakub Jelen aacf0d
+			if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(b, p)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(b, q)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(b, g)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(b, pub_key)) != 0)
Jakub Jelen aacf0d
+				return ret;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
 	case KEY_ECDSA:
Jakub Jelen 6cf9b8
@@ -802,13 +807,18 @@ to_blob_buf(const struct sshkey *key, st
Jakub Jelen aacf0d
 			return ret;
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # endif
Jakub Jelen aacf0d
-	case KEY_RSA:
Jakub Jelen aacf0d
-		if (key->rsa == NULL)
Jakub Jelen aacf0d
-			return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
-		if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
Jakub Jelen aacf0d
-			return ret;
Jakub Jelen aacf0d
+	case KEY_RSA: {
Jakub Jelen aacf0d
+			const BIGNUM *e, *n;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (key->rsa == NULL)
Jakub Jelen aacf0d
+				return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			RSA_get0_key(key->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
+			if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(b, e)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(b, n)) != 0)
Jakub Jelen aacf0d
+				return ret;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #endif /* WITH_OPENSSL */
Jakub Jelen aacf0d
 	case KEY_ED25519:
Jakub Jelen 6cf9b8
@@ -914,8 +924,13 @@ sshkey_fingerprint_raw(const struct sshk
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (k->type == KEY_RSA1) {
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
-		int nlen = BN_num_bytes(k->rsa->n);
Jakub Jelen aacf0d
-		int elen = BN_num_bytes(k->rsa->e);
Jakub Jelen aacf0d
+		const BIGNUM *n, *e;
Jakub Jelen aacf0d
+		int nlen, elen;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+		RSA_get0_key(k->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+		nlen = BN_num_bytes(n);
Jakub Jelen aacf0d
+		elen = BN_num_bytes(e);
Jakub Jelen aacf0d
 
Jakub Jelen 6cf9b8
 		if (nlen < 0 || elen < 0 || nlen >= INT_MAX - elen) {
Jakub Jelen 6cf9b8
 			r = SSH_ERR_INVALID_FORMAT;
Jakub Jelen 6cf9b8
@@ -926,8 +941,8 @@ sshkey_fingerprint_raw(const struct sshk
Jakub Jelen aacf0d
 			r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		BN_bn2bin(k->rsa->n, blob);
Jakub Jelen aacf0d
-		BN_bn2bin(k->rsa->e, blob + nlen);
Jakub Jelen aacf0d
+		BN_bn2bin(n, blob);
Jakub Jelen aacf0d
+		BN_bn2bin(e, blob + nlen);
Jakub Jelen aacf0d
 #endif /* WITH_OPENSSL */
Jakub Jelen aacf0d
 	} else if ((r = to_blob(k, &blob, &blob_len, 1)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen 6cf9b8
@@ -1290,6 +1305,7 @@ sshkey_read(struct sshkey *ret, char **c
Jakub Jelen aacf0d
 	struct sshbuf *blob;
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen aacf0d
 	u_long bits;
Jakub Jelen aacf0d
+	BIGNUM *e = NULL, *n = NULL;
Jakub Jelen aacf0d
 #endif /* WITH_SSH1 */
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	cp = *cpp;
Jakub Jelen 6cf9b8
@@ -1303,12 +1319,21 @@ sshkey_read(struct sshkey *ret, char **c
Jakub Jelen aacf0d
 		    bits == 0 || bits > SSHBUF_MAX_BIGNUM * 8)
Jakub Jelen aacf0d
 			return SSH_ERR_INVALID_FORMAT;	/* Bad bit count... */
Jakub Jelen aacf0d
 		/* Get public exponent, public modulus. */
Jakub Jelen aacf0d
-		if ((r = read_decimal_bignum(&ep, ret->rsa->e)) < 0)
Jakub Jelen aacf0d
+		if ((e = BN_new()) == NULL || (n = BN_new()) == NULL) {
Jakub Jelen aacf0d
+			BN_free(e);
Jakub Jelen aacf0d
+			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
+		if ((r = read_decimal_bignum(&ep, e)) < 0)
Jakub Jelen aacf0d
 			return r;
Jakub Jelen aacf0d
-		if ((r = read_decimal_bignum(&ep, ret->rsa->n)) < 0)
Jakub Jelen aacf0d
+		if ((r = read_decimal_bignum(&ep, n)) < 0)
Jakub Jelen aacf0d
 			return r;
Jakub Jelen aacf0d
+		if (RSA_set0_key(ret->rsa, n, e, NULL) == 0) {
Jakub Jelen aacf0d
+			BN_free(e);
Jakub Jelen aacf0d
+			BN_free(n);
Jakub Jelen aacf0d
+			return -1;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		/* validate the claimed number of bits */
Jakub Jelen aacf0d
-		if (BN_num_bits(ret->rsa->n) != (int)bits)
Jakub Jelen aacf0d
+		if (BN_num_bits(n) != (int)bits)
Jakub Jelen aacf0d
 			return SSH_ERR_KEY_BITS_MISMATCH;
Jakub Jelen aacf0d
 		*cpp = ep;
Jakub Jelen aacf0d
 		retval = 0;
Jakub Jelen 6cf9b8
@@ -1473,19 +1498,20 @@ sshkey_format_rsa1(const struct sshkey *
Jakub Jelen aacf0d
 #ifdef WITH_SSH1
Jakub Jelen aacf0d
 	u_int bits = 0;
Jakub Jelen aacf0d
 	char *dec_e = NULL, *dec_n = NULL;
Jakub Jelen aacf0d
+	const BIGNUM *e, *n;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	if (key->rsa == NULL || key->rsa->e == NULL ||
Jakub Jelen aacf0d
-	    key->rsa->n == NULL) {
Jakub Jelen aacf0d
+	RSA_get0_key(key->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
+	if (key->rsa == NULL || e == NULL || n == NULL) {
Jakub Jelen aacf0d
 		r = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if ((dec_e = BN_bn2dec(key->rsa->e)) == NULL ||
Jakub Jelen aacf0d
-	    (dec_n = BN_bn2dec(key->rsa->n)) == NULL) {
Jakub Jelen aacf0d
+	if ((dec_e = BN_bn2dec(e)) == NULL ||
Jakub Jelen aacf0d
+	    (dec_n = BN_bn2dec(n)) == NULL) {
Jakub Jelen aacf0d
 		r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	/* size of modulus 'n' */
Jakub Jelen aacf0d
-	if ((bits = BN_num_bits(key->rsa->n)) <= 0) {
Jakub Jelen aacf0d
+	if ((bits = BN_num_bits(n)) <= 0) {
Jakub Jelen aacf0d
 		r = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen 6cf9b8
@@ -1819,15 +1845,32 @@ sshkey_from_private(const struct sshkey
Jakub Jelen aacf0d
 	switch (k->type) {
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
 	case KEY_DSA:
Jakub Jelen aacf0d
-	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-		if ((n = sshkey_new(k->type)) == NULL)
Jakub Jelen aacf0d
-			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
-		if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
Jakub Jelen aacf0d
-		    (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
Jakub Jelen aacf0d
-		    (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
Jakub Jelen aacf0d
-		    (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
Jakub Jelen aacf0d
-			sshkey_free(n);
Jakub Jelen aacf0d
-			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+	case KEY_DSA_CERT: {
Jakub Jelen aacf0d
+			const BIGNUM *k_p, *k_q, *k_g, *k_pub_key;
Jakub Jelen aacf0d
+			BIGNUM *n_p = NULL, *n_q = NULL, *n_g = NULL, *n_pub_key = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if ((n = sshkey_new(k->type)) == NULL)
Jakub Jelen aacf0d
+				return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			DSA_get0_pqg(k->dsa, &k_p, &k_q, &k_g);
Jakub Jelen aacf0d
+			DSA_get0_key(k->dsa, &k_pub_key, NULL);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (((n_p = BN_dup(k_p)) == NULL) ||
Jakub Jelen aacf0d
+			    ((n_q = BN_dup(k_q)) == NULL) ||
Jakub Jelen aacf0d
+			    ((n_g = BN_dup(k_g)) == NULL) ||
Jakub Jelen aacf0d
+			    (DSA_set0_pqg(n->dsa, n_p, n_q, n_g) == 0)) {
Jakub Jelen aacf0d
+				sshkey_free(n);
Jakub Jelen aacf0d
+				BN_free(n_p);
Jakub Jelen aacf0d
+				BN_free(n_q);
Jakub Jelen aacf0d
+				BN_free(n_g);
Jakub Jelen aacf0d
+				return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+			if (((n_pub_key = BN_dup(k_pub_key)) == NULL) ||
Jakub Jelen aacf0d
+			    (DSA_set0_key(n->dsa, n_pub_key, NULL) == 0)) {
Jakub Jelen aacf0d
+				sshkey_free(n);
Jakub Jelen aacf0d
+				BN_free(n_pub_key);
Jakub Jelen aacf0d
+				return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # ifdef OPENSSL_HAS_ECC
Jakub Jelen 6cf9b8
@@ -1850,13 +1893,22 @@ sshkey_from_private(const struct sshkey
Jakub Jelen aacf0d
 # endif /* OPENSSL_HAS_ECC */
Jakub Jelen aacf0d
 	case KEY_RSA:
Jakub Jelen aacf0d
 	case KEY_RSA1:
Jakub Jelen aacf0d
-	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-		if ((n = sshkey_new(k->type)) == NULL)
Jakub Jelen aacf0d
-			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
-		if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
Jakub Jelen aacf0d
-		    (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
Jakub Jelen aacf0d
-			sshkey_free(n);
Jakub Jelen aacf0d
-			return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+	case KEY_RSA_CERT: {
Jakub Jelen aacf0d
+			const BIGNUM *k_n, *k_e;
Jakub Jelen aacf0d
+			BIGNUM *n_n = NULL, *n_e = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if ((n = sshkey_new(k->type)) == NULL)
Jakub Jelen aacf0d
+				return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			RSA_get0_key(k->rsa, &k_n, &k_e, NULL);
Jakub Jelen aacf0d
+			if (((n_n = BN_dup(k_n)) == NULL) ||
Jakub Jelen aacf0d
+			    ((n_e = BN_dup(k_e)) == NULL) ||
Jakub Jelen aacf0d
+			    RSA_set0_key(n->rsa, n_n, n_e, NULL) == 0) {
Jakub Jelen aacf0d
+				sshkey_free(n);
Jakub Jelen aacf0d
+				BN_free(n_n);
Jakub Jelen aacf0d
+				BN_free(n_e);
Jakub Jelen aacf0d
+				return SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #endif /* WITH_OPENSSL */
Jakub Jelen 6cf9b8
@@ -2054,10 +2106,20 @@ sshkey_from_blob_internal(struct sshbuf
Jakub Jelen aacf0d
 			ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
Jakub Jelen aacf0d
-		    sshbuf_get_bignum2(b, key->rsa->n) != 0) {
Jakub Jelen aacf0d
-			ret = SSH_ERR_INVALID_FORMAT;
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+		{
Jakub Jelen aacf0d
+			BIGNUM *e, *n;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			e = BN_new();
Jakub Jelen aacf0d
+			n = BN_new();
Jakub Jelen aacf0d
+			if (e == NULL || n == NULL ||
Jakub Jelen aacf0d
+			    sshbuf_get_bignum2(b, e) != 0 ||
Jakub Jelen aacf0d
+			    sshbuf_get_bignum2(b, n) != 0 ||
Jakub Jelen aacf0d
+			    RSA_set0_key(key->rsa, n, e, NULL) == 0) {
Jakub Jelen aacf0d
+				BN_free(e);
Jakub Jelen aacf0d
+				BN_free(n);
Jakub Jelen aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 #ifdef DEBUG_PK
Jakub Jelen aacf0d
 		RSA_print_fp(stderr, key->rsa, 8);
Jakub Jelen 6cf9b8
@@ -2075,12 +2137,34 @@ sshkey_from_blob_internal(struct sshbuf
Jakub Jelen aacf0d
 			ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
Jakub Jelen aacf0d
-		    sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
Jakub Jelen aacf0d
-		    sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
Jakub Jelen aacf0d
-		    sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
Jakub Jelen aacf0d
-			ret = SSH_ERR_INVALID_FORMAT;
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+		{
Jakub Jelen aacf0d
+			BIGNUM *p, *q, *g, *pub_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			p = BN_new();
Jakub Jelen aacf0d
+			q = BN_new();
Jakub Jelen aacf0d
+			g = BN_new();
Jakub Jelen aacf0d
+			pub_key = BN_new();
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (p == NULL || q == NULL || g == NULL ||
Jakub Jelen aacf0d
+			    pub_key == NULL ||
Jakub Jelen aacf0d
+			    sshbuf_get_bignum2(b, p) != 0 ||
Jakub Jelen aacf0d
+			    sshbuf_get_bignum2(b, q) != 0 ||
Jakub Jelen aacf0d
+			    sshbuf_get_bignum2(b, g) != 0 ||
Jakub Jelen aacf0d
+			    sshbuf_get_bignum2(b, pub_key) != 0 ||
Jakub Jelen aacf0d
+			    DSA_set0_pqg(key->dsa, p, q, g) == 0) {
Jakub Jelen aacf0d
+				BN_free(p);
Jakub Jelen aacf0d
+				BN_free(q);
Jakub Jelen aacf0d
+				BN_free(g);
Jakub Jelen aacf0d
+				BN_free(pub_key);
Jakub Jelen aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (DSA_set0_key(key->dsa, pub_key, NULL) == 0) {
Jakub Jelen aacf0d
+				BN_free(pub_key);
Jakub Jelen aacf0d
+				ret = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 #ifdef DEBUG_PK
Jakub Jelen aacf0d
 		DSA_print_fp(stderr, key->dsa, 8);
Jakub Jelen 6cf9b8
@@ -2320,26 +2404,53 @@ sshkey_demote(const struct sshkey *k, st
Jakub Jelen aacf0d
 			goto fail;
Jakub Jelen aacf0d
 		/* FALLTHROUGH */
Jakub Jelen aacf0d
 	case KEY_RSA1:
Jakub Jelen aacf0d
-	case KEY_RSA:
Jakub Jelen aacf0d
-		if ((pk->rsa = RSA_new()) == NULL ||
Jakub Jelen aacf0d
-		    (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
Jakub Jelen aacf0d
-		    (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
Jakub Jelen aacf0d
-			ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
-			goto fail;
Jakub Jelen aacf0d
+	case KEY_RSA: {
Jakub Jelen aacf0d
+			const BIGNUM *k_e, *k_n;
Jakub Jelen aacf0d
+			BIGNUM *pk_e = NULL, *pk_n = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			RSA_get0_key(k->rsa, &k_n, &k_e, NULL);
Jakub Jelen aacf0d
+			if ((pk->rsa = RSA_new()) == NULL ||
Jakub Jelen aacf0d
+			    (pk_e = BN_dup(k_e)) == NULL ||
Jakub Jelen aacf0d
+			    (pk_n = BN_dup(k_n)) == NULL ||
Jakub Jelen aacf0d
+			    RSA_set0_key(pk->rsa, pk_n, pk_e, NULL) == 0) {
Jakub Jelen aacf0d
+				BN_free(pk_e);
Jakub Jelen aacf0d
+				BN_free(pk_n);
Jakub Jelen aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+				goto fail;
Jakub Jelen aacf0d
 			}
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEY_DSA_CERT:
Jakub Jelen aacf0d
 		if ((ret = sshkey_cert_copy(k, pk)) != 0)
Jakub Jelen aacf0d
 			goto fail;
Jakub Jelen aacf0d
 		/* FALLTHROUGH */
Jakub Jelen aacf0d
-	case KEY_DSA:
Jakub Jelen aacf0d
-		if ((pk->dsa = DSA_new()) == NULL ||
Jakub Jelen aacf0d
-		    (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
Jakub Jelen aacf0d
-		    (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
Jakub Jelen aacf0d
-		    (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
Jakub Jelen aacf0d
-		    (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
Jakub Jelen aacf0d
-			ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
-			goto fail;
Jakub Jelen aacf0d
+	case KEY_DSA: {
Jakub Jelen aacf0d
+			const BIGNUM *k_p, *k_q, *k_g, *k_pub_key;
Jakub Jelen aacf0d
+			BIGNUM *pk_p = NULL, *pk_q = NULL, *pk_g = NULL;
Jakub Jelen aacf0d
+			BIGNUM *pk_pub_key = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			DSA_get0_pqg(k->dsa, &k_p, &k_q, &k_g);
Jakub Jelen aacf0d
+			DSA_get0_key(k->dsa, &k_pub_key, NULL);
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if ((pk->dsa = DSA_new()) == NULL ||
Jakub Jelen aacf0d
+			    (pk_p = BN_dup(k_p)) == NULL ||
Jakub Jelen aacf0d
+			    (pk_q = BN_dup(k_q)) == NULL ||
Jakub Jelen aacf0d
+			    (pk_g = BN_dup(k_g)) == NULL ||
Jakub Jelen aacf0d
+			    (pk_pub_key = BN_dup(k_pub_key)) == NULL ||
Jakub Jelen aacf0d
+			    DSA_set0_pqg(pk->dsa, pk_p, pk_q, pk_g) == 0) {
Jakub Jelen aacf0d
+				BN_free(pk_p);
Jakub Jelen aacf0d
+				BN_free(pk_q);
Jakub Jelen aacf0d
+				BN_free(pk_g);
Jakub Jelen aacf0d
+				BN_free(pk_pub_key);
Jakub Jelen aacf0d
+				ret = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
+				goto fail;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (DSA_set0_key(pk->dsa, pk_pub_key, NULL) == 0) {
Jakub Jelen aacf0d
+				BN_free(pk_pub_key);
Jakub Jelen aacf0d
+				ret = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
+				goto fail;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEY_ECDSA_CERT:
Jakub Jelen 6cf9b8
@@ -2460,12 +2571,17 @@ sshkey_certify(struct sshkey *k, struct
Jakub Jelen aacf0d
 	/* XXX this substantially duplicates to_blob(); refactor */
Jakub Jelen aacf0d
 	switch (k->type) {
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
-	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-		if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+	case KEY_DSA_CERT: {
Jakub Jelen aacf0d
+			const BIGNUM *p, *q, *g, *pub_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			DSA_get0_pqg(k->dsa, &p, &q, &g);
Jakub Jelen aacf0d
+			DSA_get0_key(k->dsa, &pub_key, NULL);
Jakub Jelen aacf0d
+			if ((ret = sshbuf_put_bignum2(cert, p)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(cert, pub_key)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
 	case KEY_ECDSA_CERT:
Jakub Jelen 6cf9b8
@@ -2477,10 +2593,15 @@ sshkey_certify(struct sshkey *k, struct
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # endif /* OPENSSL_HAS_ECC */
Jakub Jelen aacf0d
-	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-		if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-		    (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+	case KEY_RSA_CERT: {
Jakub Jelen aacf0d
+			const BIGNUM *e, *n;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			RSA_get0_key(k->rsa, &n, &e, NULL);
Jakub Jelen aacf0d
+			if (e == NULL || n == NULL ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
Jakub Jelen aacf0d
+			    (ret = sshbuf_put_bignum2(cert, n)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #endif /* WITH_OPENSSL */
Jakub Jelen aacf0d
 	case KEY_ED25519_CERT:
Jakub Jelen 6cf9b8
@@ -2637,43 +2758,65 @@ sshkey_private_serialize(const struct ss
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	switch (key->type) {
Jakub Jelen aacf0d
 #ifdef WITH_OPENSSL
Jakub Jelen aacf0d
-	case KEY_RSA:
Jakub Jelen aacf0d
-		if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+	case KEY_RSA: {
Jakub Jelen aacf0d
+			const BIGNUM *n, *e, *d, *iqmp, *p, *q;
Jakub Jelen aacf0d
+			RSA_get0_key(key->rsa, &n, &e, &d);
Jakub Jelen aacf0d
+			RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
Jakub Jelen aacf0d
+			RSA_get0_factors(key->rsa, &p, &q);
Jakub Jelen aacf0d
+			if ((r = sshbuf_put_bignum2(b, n)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, e)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, d)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, p)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, q)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEY_RSA_CERT:
Jakub Jelen aacf0d
 		if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
Jakub Jelen aacf0d
 			r = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+		{
Jakub Jelen aacf0d
+			const BIGNUM *d, *iqmp, *p, *q;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			RSA_get0_key(key->rsa, NULL, NULL, &d);
Jakub Jelen aacf0d
+			RSA_get0_factors(key->rsa, &p, &q);
Jakub Jelen aacf0d
+			RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
Jakub Jelen aacf0d
+			if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, d)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, p)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, q)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
-	case KEY_DSA:
Jakub Jelen aacf0d
-		if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+	case KEY_DSA: {
Jakub Jelen aacf0d
+			const BIGNUM *p, *q, *g, *pub_key, *priv_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			DSA_get0_pqg(key->dsa, &p, &q, &g);
Jakub Jelen aacf0d
+			DSA_get0_key(key->dsa, &pub_key, &priv_key);
Jakub Jelen aacf0d
+			if ((r = sshbuf_put_bignum2(b, p)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, q)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, g)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, pub_key)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, priv_key)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	case KEY_DSA_CERT:
Jakub Jelen aacf0d
 		if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
Jakub Jelen aacf0d
 			r = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+		{
Jakub Jelen aacf0d
+			const BIGNUM *priv_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			DSA_get0_key(key->dsa, NULL, &priv_key);
Jakub Jelen aacf0d
+			if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_put_bignum2(b, priv_key)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
 	case KEY_ECDSA:
Jakub Jelen 6cf9b8
@@ -2749,18 +2892,51 @@ sshkey_private_deserialize(struct sshbuf
Jakub Jelen aacf0d
 			r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+		{
Jakub Jelen aacf0d
+			BIGNUM *p, *q, *g, *pub_key, *priv_key;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			p = BN_new();
Jakub Jelen aacf0d
+			q = BN_new();
Jakub Jelen aacf0d
+			g = BN_new();
Jakub Jelen aacf0d
+			pub_key = BN_new();
Jakub Jelen aacf0d
+			priv_key = BN_new();
Jakub Jelen aacf0d
+			if (p == NULL || q == NULL || g == NULL ||
Jakub Jelen aacf0d
+			    pub_key == NULL || priv_key == NULL ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, p)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, q)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, g)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, pub_key)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, priv_key)) != 0 ||
Jakub Jelen aacf0d
+			    (r = ((DSA_set0_pqg(k->dsa, p, q, g) == 0)
Jakub Jelen aacf0d
+			    ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
Jakub Jelen aacf0d
+				BN_free(p);
Jakub Jelen aacf0d
+				BN_free(q);
Jakub Jelen aacf0d
+				BN_free(g);
Jakub Jelen aacf0d
+				BN_free(pub_key);
Jakub Jelen aacf0d
+				BN_free(priv_key);
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+			if (DSA_set0_key(k->dsa, pub_key, priv_key) == 0) {
Jakub Jelen aacf0d
+				r = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
+				BN_free(pub_key);
Jakub Jelen aacf0d
+				BN_free(priv_key);
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
-	case KEY_DSA_CERT:
Jakub Jelen aacf0d
-		if ((r = sshkey_froms(buf, &k)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshkey_add_private(k)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+	case KEY_DSA_CERT: {
Jakub Jelen aacf0d
+			BIGNUM *priv_key = BN_new();
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (priv_key == NULL ||
Jakub Jelen aacf0d
+			    (r = sshkey_froms(buf, &k)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshkey_add_private(k)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, priv_key)) != 0 ||
Jakub Jelen aacf0d
+			    (r = ((DSA_set0_key(k->dsa, NULL, priv_key) == 0)
Jakub Jelen aacf0d
+			    ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
Jakub Jelen aacf0d
+				BN_free(priv_key);
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 # ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
 	case KEY_ECDSA:
Jakub Jelen 4a6ef4
@@ -2819,24 +2995,71 @@ sshkey_private_deserialize(struct sshbuf
Jakub Jelen aacf0d
 			r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
Jakub Jelen aacf0d
-		    (r = rsa_generate_additional_parameters(k->rsa)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+		{
Jakub Jelen aacf0d
+			BIGNUM *n, *e, *d, *iqmp, *p, *q;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			n = BN_new();
Jakub Jelen aacf0d
+			e = BN_new();
Jakub Jelen aacf0d
+			d = BN_new();
Jakub Jelen aacf0d
+			iqmp = BN_new();
Jakub Jelen aacf0d
+			p = BN_new();
Jakub Jelen aacf0d
+			q = BN_new();
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if (n == NULL || e == NULL || d == NULL ||
Jakub Jelen aacf0d
+			    iqmp == NULL || p == NULL || q == NULL ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, n)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, e)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, d)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, p)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, q)) != 0 ||
Jakub Jelen aacf0d
+			    (r = ((RSA_set0_key(k->rsa, n, e, d) == 0)
Jakub Jelen aacf0d
+			    ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
Jakub Jelen aacf0d
+				BN_free(n);
Jakub Jelen aacf0d
+				BN_free(e);
Jakub Jelen aacf0d
+				BN_free(d);
Jakub Jelen aacf0d
+				BN_free(iqmp);
Jakub Jelen aacf0d
+				BN_free(p);
Jakub Jelen aacf0d
+				BN_free(q);
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+			if (RSA_set0_factors(k->rsa, p, q) == 0) {
Jakub Jelen aacf0d
+				r = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
+				BN_free(iqmp);
Jakub Jelen aacf0d
+				BN_free(p);
Jakub Jelen aacf0d
+				BN_free(q);
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+			if ((r = rsa_generate_additional_parameters(k->rsa, iqmp)) != 0) {
Jakub Jelen aacf0d
+				BN_free(iqmp);
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
-	case KEY_RSA_CERT:
Jakub Jelen aacf0d
-		if ((r = sshkey_froms(buf, &k)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshkey_add_private(k)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
Jakub Jelen aacf0d
-		    (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
Jakub Jelen aacf0d
-		    (r = rsa_generate_additional_parameters(k->rsa)) != 0)
Jakub Jelen aacf0d
-			goto out;
Jakub Jelen aacf0d
+	case KEY_RSA_CERT: {
Jakub Jelen 4a6ef4
+			BIGNUM *d, *iqmp, *p, *q;
Jakub Jelen aacf0d
+
Jakub Jelen 4a6ef4
+			/* N and E are already set so make sure we will not overwrite them */
Jakub Jelen aacf0d
+			d = BN_new();
Jakub Jelen aacf0d
+			iqmp = BN_new();
Jakub Jelen aacf0d
+			p = BN_new();
Jakub Jelen aacf0d
+			q = BN_new();
Jakub Jelen aacf0d
+
Jakub Jelen 4a6ef4
+			if (d == NULL || iqmp == NULL || p == NULL ||
Jakub Jelen 4a6ef4
+			    q == NULL ||
Jakub Jelen aacf0d
+			    (r = sshkey_froms(buf, &k)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshkey_add_private(k)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, d)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, p)) != 0 ||
Jakub Jelen aacf0d
+			    (r = sshbuf_get_bignum2(buf, q)) != 0 ||
Jakub Jelen 4a6ef4
+			    (r = ((RSA_set0_key(k->rsa, NULL, NULL, d) == 0)
Jakub Jelen aacf0d
+			        ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0 ||
Jakub Jelen aacf0d
+			    (r = ((RSA_set0_factors(k->rsa, p, q) == 0)
Jakub Jelen aacf0d
+			        ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0 ||
Jakub Jelen 4a6ef4
+			    (r = rsa_generate_additional_parameters(k->rsa, iqmp)) != 0)
Jakub Jelen aacf0d
+				goto out;
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 #endif /* WITH_OPENSSL */
Jakub Jelen aacf0d
 	case KEY_ED25519:
Jakub Jelen 6cf9b8
@@ -3471,6 +3704,7 @@ sshkey_private_rsa1_to_blob(struct sshke
Jakub Jelen 6cf9b8
 	struct sshcipher_ctx *ciphercontext = NULL;
Jakub Jelen aacf0d
 	const struct sshcipher *cipher;
Jakub Jelen aacf0d
 	u_char *cp;
Jakub Jelen aacf0d
+	const BIGNUM *n, *e, *d, *q, *p, *iqmp;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/*
Jakub Jelen aacf0d
 	 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
Jakub Jelen 6cf9b8
@@ -3497,10 +3731,13 @@ sshkey_private_rsa1_to_blob(struct sshke
Jakub Jelen aacf0d
 	 * format would just give known plaintext).
Jakub Jelen aacf0d
 	 * Note: q and p are stored in reverse order to SSL.
Jakub Jelen aacf0d
 	 */
Jakub Jelen aacf0d
-	if ((r = sshbuf_put_bignum1(buffer, key->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(buffer, key->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(buffer, key->rsa->q)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(buffer, key->rsa->p)) != 0)
Jakub Jelen aacf0d
+	RSA_get0_key(key->rsa, &n, &e, &d);
Jakub Jelen aacf0d
+	RSA_get0_factors(key->rsa, &p, &q);
Jakub Jelen aacf0d
+	RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
Jakub Jelen aacf0d
+	if ((r = sshbuf_put_bignum1(buffer, d)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(buffer, iqmp)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(buffer, q)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(buffer, p)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Pad the part to be encrypted to a size that is a multiple of 8. */
Jakub Jelen 6cf9b8
@@ -3525,9 +3762,9 @@ sshkey_private_rsa1_to_blob(struct sshke
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Store public key.  This will be in plain text. */
Jakub Jelen aacf0d
-	if ((r = sshbuf_put_u32(encrypted, BN_num_bits(key->rsa->n))) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(encrypted, key->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_put_bignum1(encrypted, key->rsa->e)) != 0 ||
Jakub Jelen aacf0d
+	if ((r = sshbuf_put_u32(encrypted, BN_num_bits(n))) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(encrypted, n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_put_bignum1(encrypted, e)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_put_cstring(encrypted, comment)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen 6cf9b8
@@ -3654,6 +3891,7 @@ sshkey_parse_public_rsa1_fileblob(struct
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
 	struct sshkey *pub = NULL;
Jakub Jelen aacf0d
 	struct sshbuf *copy = NULL;
Jakub Jelen aacf0d
+	BIGNUM *n = NULL, *e = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (keyp != NULL)
Jakub Jelen aacf0d
 		*keyp = NULL;
Jakub Jelen 6cf9b8
@@ -3683,10 +3921,16 @@ sshkey_parse_public_rsa1_fileblob(struct
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Read the public key from the buffer. */
Jakub Jelen aacf0d
-	if ((pub = sshkey_new(KEY_RSA1)) == NULL ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(copy, pub->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(copy, pub->rsa->e)) != 0)
Jakub Jelen aacf0d
+	if ((n = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (pub = sshkey_new(KEY_RSA1)) == NULL ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(copy, n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(copy, e)) != 0 ||
Jakub Jelen aacf0d
+	    RSA_set0_key(pub->rsa, n, e, NULL) == 0) {
Jakub Jelen aacf0d
+		BN_free(n);
Jakub Jelen aacf0d
+		BN_free(e);
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Finally, the comment */
Jakub Jelen aacf0d
 	if ((r = sshbuf_get_string(copy, (u_char**)commentp, NULL)) != 0)
Jakub Jelen 6cf9b8
@@ -3718,6 +3962,8 @@ sshkey_parse_private_rsa1(struct sshbuf
Jakub Jelen 6cf9b8
 	struct sshcipher_ctx *ciphercontext = NULL;
Jakub Jelen aacf0d
 	const struct sshcipher *cipher;
Jakub Jelen aacf0d
 	struct sshkey *prv = NULL;
Jakub Jelen aacf0d
+	BIGNUM *n = NULL, *e = NULL, *d = NULL, *q = NULL, *p = NULL,
Jakub Jelen aacf0d
+	    *iqmp = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (keyp != NULL)
Jakub Jelen aacf0d
 		*keyp = NULL;
Jakub Jelen 6cf9b8
@@ -3753,11 +3999,17 @@ sshkey_parse_private_rsa1(struct sshbuf
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Read the public key and comment from the buffer. */
Jakub Jelen aacf0d
-	if ((r = sshbuf_get_u32(copy, NULL)) != 0 ||	/* key bits */
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(copy, prv->rsa->n)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(copy, prv->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_cstring(copy, &comment, NULL)) != 0)
Jakub Jelen aacf0d
+	if ((n = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_u32(copy, NULL)) != 0 ||	/* key bits */
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(copy, n)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(copy, e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_cstring(copy, &comment, NULL)) != 0 ||
Jakub Jelen aacf0d
+	    RSA_set0_key(prv->rsa, n, e, NULL) == 0) {
Jakub Jelen aacf0d
+		BN_free(n);
Jakub Jelen aacf0d
+		BN_free(e);
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Check that it is a supported cipher. */
Jakub Jelen aacf0d
 	cipher = cipher_by_number(cipher_type);
Jakub Jelen 6cf9b8
@@ -3786,14 +4038,25 @@ sshkey_parse_private_rsa1(struct sshbuf
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* Read the rest of the private key. */
Jakub Jelen aacf0d
-	if ((r = sshbuf_get_bignum1(decrypted, prv->rsa->d)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(decrypted, prv->rsa->iqmp)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(decrypted, prv->rsa->q)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(decrypted, prv->rsa->p)) != 0)
Jakub Jelen aacf0d
+	if ((d = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (p = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (q = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (iqmp = BN_new()) == NULL ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(decrypted, d)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(decrypted, iqmp)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(decrypted, q)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(decrypted, p)) != 0 ||
Jakub Jelen aacf0d
+	    (RSA_set0_key(prv->rsa, NULL, NULL, d) == 0) ||
Jakub Jelen aacf0d
+	    (RSA_set0_factors(prv->rsa, p, q) == 0)) {
Jakub Jelen aacf0d
+		BN_free(d);
Jakub Jelen aacf0d
+		BN_free(p);
Jakub Jelen aacf0d
+		BN_free(q);
Jakub Jelen aacf0d
+		BN_free(iqmp);
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* calculate p-1 and q-1 */
Jakub Jelen aacf0d
-	if ((r = rsa_generate_additional_parameters(prv->rsa)) != 0)
Jakub Jelen aacf0d
+	if ((r = rsa_generate_additional_parameters(prv->rsa, iqmp)) != 0)
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	/* enable blinding */
Jakub Jelen 6cf9b8
@@ -3846,7 +4109,7 @@ sshkey_parse_private_pem_fileblob(struct
Jakub Jelen aacf0d
 		r = SSH_ERR_KEY_WRONG_PASSPHRASE;
Jakub Jelen aacf0d
 		goto out;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if (pk->type == EVP_PKEY_RSA &&
Jakub Jelen aacf0d
+	if (EVP_PKEY_id(pk) == EVP_PKEY_RSA &&
Jakub Jelen aacf0d
 	    (type == KEY_UNSPEC || type == KEY_RSA)) {
Jakub Jelen aacf0d
 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
Jakub Jelen aacf0d
 			r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen 6cf9b8
@@ -3861,7 +4124,7 @@ sshkey_parse_private_pem_fileblob(struct
Jakub Jelen aacf0d
 			r = SSH_ERR_LIBCRYPTO_ERROR;
Jakub Jelen aacf0d
 			goto out;
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-	} else if (pk->type == EVP_PKEY_DSA &&
Jakub Jelen aacf0d
+	} else if (EVP_PKEY_id(pk) == EVP_PKEY_DSA &&
Jakub Jelen aacf0d
 	    (type == KEY_UNSPEC || type == KEY_DSA)) {
Jakub Jelen aacf0d
 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
Jakub Jelen aacf0d
 			r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen 6cf9b8
@@ -3873,7 +4136,7 @@ sshkey_parse_private_pem_fileblob(struct
Jakub Jelen aacf0d
 		DSA_print_fp(stderr, prv->dsa, 8);
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 #ifdef OPENSSL_HAS_ECC
Jakub Jelen aacf0d
-	} else if (pk->type == EVP_PKEY_EC &&
Jakub Jelen aacf0d
+	} else if (EVP_PKEY_id(pk) == EVP_PKEY_EC &&
Jakub Jelen aacf0d
 	    (type == KEY_UNSPEC || type == KEY_ECDSA)) {
Jakub Jelen aacf0d
 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
Jakub Jelen aacf0d
 			r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-keygen.c.openssl openssh-7.4p1/ssh-keygen.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-keygen.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-keygen.c	2016-12-23 17:47:36.437817755 +0100
Jakub Jelen aacf0d
@@ -480,40 +480,67 @@ do_convert_private_ssh2_from_blob(u_char
Jakub Jelen aacf0d
 	free(type);
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	switch (key->type) {
Jakub Jelen aacf0d
-	case KEY_DSA:
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->dsa->p);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->dsa->g);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->dsa->q);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->dsa->pub_key);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->dsa->priv_key);
Jakub Jelen aacf0d
+	case KEY_DSA: {
Jakub Jelen aacf0d
+			BIGNUM *p = NULL, *g = NULL, *q = NULL, *pub_key = NULL, *priv_key = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if ((p = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (g = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (q = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (pub_key = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (priv_key = BN_new()) == NULL)
Jakub Jelen aacf0d
+				fatal("BN_new() failed");
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, p);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, g);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, q);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, pub_key);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, priv_key);
Jakub Jelen aacf0d
+			if (DSA_set0_pqg(key->dsa, p, q, g) == 0 ||
Jakub Jelen aacf0d
+			    DSA_set0_key(key->dsa, pub_key, priv_key) == 0) {
Jakub Jelen aacf0d
+				fatal("failed to set DSA key");
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+		}
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
-	case KEY_RSA:
Jakub Jelen aacf0d
-		if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
Jakub Jelen aacf0d
-		    (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
Jakub Jelen aacf0d
-		    (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
Jakub Jelen aacf0d
-			fatal("%s: buffer error: %s", __func__, ssh_err(r));
Jakub Jelen aacf0d
-		e = e1;
Jakub Jelen aacf0d
-		debug("e %lx", e);
Jakub Jelen aacf0d
-		if (e < 30) {
Jakub Jelen aacf0d
-			e <<= 8;
Jakub Jelen aacf0d
-			e += e2;
Jakub Jelen aacf0d
-			debug("e %lx", e);
Jakub Jelen aacf0d
-			e <<= 8;
Jakub Jelen aacf0d
-			e += e3;
Jakub Jelen aacf0d
+	case KEY_RSA: {
Jakub Jelen aacf0d
+			BIGNUM *bn_e = NULL, *bn_d = NULL, *bn_n = NULL, *bn_iqmp = NULL, *bn_p = NULL, *bn_q = NULL;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if ((bn_e = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (bn_d = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (bn_n = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (bn_iqmp = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (bn_p = BN_new()) == NULL ||
Jakub Jelen aacf0d
+			    (bn_q = BN_new()) == NULL)
Jakub Jelen aacf0d
+				fatal("BN_new() failed");
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+			if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
Jakub Jelen aacf0d
+			    (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
Jakub Jelen aacf0d
+			    (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
Jakub Jelen aacf0d
+				fatal("%s: buffer error: %s", __func__, ssh_err(r));
Jakub Jelen aacf0d
+			e = e1;
Jakub Jelen aacf0d
 			debug("e %lx", e);
Jakub Jelen aacf0d
+			if (e < 30) {
Jakub Jelen aacf0d
+				e <<= 8;
Jakub Jelen aacf0d
+				e += e2;
Jakub Jelen aacf0d
+				debug("e %lx", e);
Jakub Jelen aacf0d
+				e <<= 8;
Jakub Jelen aacf0d
+				e += e3;
Jakub Jelen aacf0d
+				debug("e %lx", e);
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+			if (!BN_set_word(bn_e, e)) {
Jakub Jelen aacf0d
+				sshbuf_free(b);
Jakub Jelen aacf0d
+				sshkey_free(key);
Jakub Jelen aacf0d
+				return NULL;
Jakub Jelen aacf0d
+			}
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, bn_d);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, bn_n);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, bn_iqmp);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, bn_q);
Jakub Jelen aacf0d
+			buffer_get_bignum_bits(b, bn_p);
Jakub Jelen aacf0d
+			if (RSA_set0_key(key->rsa, bn_n, bn_e, bn_d) == 0 ||
Jakub Jelen aacf0d
+			    RSA_set0_factors(key->rsa, bn_p, bn_q) == 0)
Jakub Jelen aacf0d
+				fatal("Failed to set RSA parameters");
Jakub Jelen aacf0d
+			if ((r = rsa_generate_additional_parameters(key->rsa, bn_iqmp)) != 0)
Jakub Jelen aacf0d
+				fatal("generate RSA parameters failed: %s", ssh_err(r));
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if (!BN_set_word(key->rsa->e, e)) {
Jakub Jelen aacf0d
-			sshbuf_free(b);
Jakub Jelen aacf0d
-			sshkey_free(key);
Jakub Jelen aacf0d
-			return NULL;
Jakub Jelen aacf0d
-		}
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->rsa->d);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->rsa->n);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->rsa->iqmp);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->rsa->q);
Jakub Jelen aacf0d
-		buffer_get_bignum_bits(b, key->rsa->p);
Jakub Jelen aacf0d
-		if ((r = rsa_generate_additional_parameters(key->rsa)) != 0)
Jakub Jelen aacf0d
-			fatal("generate RSA parameters failed: %s", ssh_err(r));
Jakub Jelen aacf0d
 		break;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	rlen = sshbuf_len(b);
Jakub Jelen aacf0d
@@ -621,7 +648,7 @@ do_convert_from_pkcs8(struct sshkey **k,
Jakub Jelen aacf0d
 		    identity_file);
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	fclose(fp);
Jakub Jelen aacf0d
-	switch (EVP_PKEY_type(pubkey->type)) {
Jakub Jelen aacf0d
+	switch (EVP_PKEY_base_id(pubkey)) {
Jakub Jelen aacf0d
 	case EVP_PKEY_RSA:
Jakub Jelen aacf0d
 		if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
Jakub Jelen aacf0d
 			fatal("sshkey_new failed");
Jakub Jelen aacf0d
@@ -645,7 +672,7 @@ do_convert_from_pkcs8(struct sshkey **k,
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
 	default:
Jakub Jelen aacf0d
 		fatal("%s: unsupported pubkey type %d", __func__,
Jakub Jelen aacf0d
-		    EVP_PKEY_type(pubkey->type));
Jakub Jelen aacf0d
+		    EVP_PKEY_base_id(pubkey));
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
 	EVP_PKEY_free(pubkey);
Jakub Jelen aacf0d
 	return;
Jakub Jelen aacf0d
@@ -1683,6 +1710,7 @@ do_ca_sign(struct passwd *pw, int argc,
Jakub Jelen aacf0d
 #ifdef ENABLE_PKCS11
Jakub Jelen aacf0d
 	pkcs11_terminate();
Jakub Jelen aacf0d
 #endif
Jakub Jelen aacf0d
+	free(ca);
Jakub Jelen aacf0d
 	exit(0);
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-keyscan.c.openssl openssh-7.4p1/ssh-keyscan.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-keyscan.c.openssl	2016-12-23 17:47:36.325817708 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-keyscan.c	2016-12-23 17:47:36.437817755 +0100
Jakub Jelen aacf0d
@@ -195,6 +195,7 @@ keygrab_ssh1(con *c)
Jakub Jelen aacf0d
 	static struct sshbuf *msg;
Jakub Jelen aacf0d
 	int r;
Jakub Jelen aacf0d
 	u_char type;
Jakub Jelen aacf0d
+	BIGNUM *n = NULL, *e = NULL;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (rsa == NULL) {
Jakub Jelen aacf0d
 		if ((rsa = sshkey_new(KEY_RSA1)) == NULL) {
Jakub Jelen aacf0d
@@ -213,16 +214,20 @@ keygrab_ssh1(con *c)
Jakub Jelen aacf0d
 		sshbuf_reset(msg);
Jakub Jelen aacf0d
 		return NULL;
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	if ((r = sshbuf_consume(msg, 8)) != 0 || /* cookie */
Jakub Jelen aacf0d
+	if ((n = BN_new()) == NULL || (e = BN_new()) == NULL || 
Jakub Jelen aacf0d
+	    (r = sshbuf_consume(msg, 8)) != 0 || /* cookie */
Jakub Jelen aacf0d
 	    /* server key */
Jakub Jelen aacf0d
 	    (r = sshbuf_get_u32(msg, NULL)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_get_bignum1(msg, NULL)) != 0 ||
Jakub Jelen aacf0d
 	    (r = sshbuf_get_bignum1(msg, NULL)) != 0 ||
Jakub Jelen aacf0d
 	    /* host key */
Jakub Jelen aacf0d
 	    (r = sshbuf_get_u32(msg, NULL)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(msg, rsa->rsa->e)) != 0 ||
Jakub Jelen aacf0d
-	    (r = sshbuf_get_bignum1(msg, rsa->rsa->n)) != 0) {
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(msg, e)) != 0 ||
Jakub Jelen aacf0d
+	    (r = sshbuf_get_bignum1(msg, n)) != 0 ||
Jakub Jelen aacf0d
+	    RSA_set0_key(rsa->rsa, n, e, NULL) == 0) {
Jakub Jelen aacf0d
  buf_err:
Jakub Jelen aacf0d
+		BN_free(n);
Jakub Jelen aacf0d
+		BN_free(e);
Jakub Jelen aacf0d
 		error("%s: buffer error: %s", __func__, ssh_err(r));
Jakub Jelen aacf0d
 		sshbuf_reset(msg);
Jakub Jelen aacf0d
 		return NULL;
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-pkcs11-client.c.openssl openssh-7.4p1/ssh-pkcs11-client.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-pkcs11-client.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-pkcs11-client.c	2016-12-23 17:47:36.437817755 +0100
Jakub Jelen aacf0d
@@ -143,12 +143,14 @@ pkcs11_rsa_private_encrypt(int flen, con
Jakub Jelen aacf0d
 static int
Jakub Jelen aacf0d
 wrap_key(RSA *rsa)
Jakub Jelen aacf0d
 {
Jakub Jelen aacf0d
-	static RSA_METHOD helper_rsa;
Jakub Jelen aacf0d
+	static RSA_METHOD *helper_rsa;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
-	memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
Jakub Jelen aacf0d
-	helper_rsa.name = "ssh-pkcs11-helper";
Jakub Jelen aacf0d
-	helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
Jakub Jelen aacf0d
-	RSA_set_method(rsa, &helper_rsa);
Jakub Jelen aacf0d
+	if (helper_rsa == NULL) {
Jakub Jelen aacf0d
+		helper_rsa = RSA_meth_dup(RSA_get_default_method());
Jakub Jelen aacf0d
+		RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
Jakub Jelen aacf0d
+		RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
+	RSA_set_method(rsa, helper_rsa);
Jakub Jelen aacf0d
 	return (0);
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
 
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-pkcs11.c.openssl openssh-7.4p1/ssh-pkcs11.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-pkcs11.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-pkcs11.c	2016-12-23 17:47:36.437817755 +0100
Jakub Jelen aacf0d
@@ -67,7 +67,7 @@ struct pkcs11_key {
Jakub Jelen aacf0d
 	struct pkcs11_provider	*provider;
Jakub Jelen aacf0d
 	CK_ULONG		slotidx;
Jakub Jelen aacf0d
 	int			(*orig_finish)(RSA *rsa);
Jakub Jelen aacf0d
-	RSA_METHOD		rsa_method;
Jakub Jelen aacf0d
+	RSA_METHOD		*rsa_method;
Jakub Jelen aacf0d
 	char			*keyid;
Jakub Jelen aacf0d
 	int			keyid_len;
Jakub Jelen aacf0d
 };
Jakub Jelen aacf0d
@@ -326,13 +326,21 @@ pkcs11_rsa_wrap(struct pkcs11_provider *
Jakub Jelen aacf0d
 		k11->keyid = xmalloc(k11->keyid_len);
Jakub Jelen aacf0d
 		memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
Jakub Jelen aacf0d
 	}
Jakub Jelen aacf0d
-	k11->orig_finish = def->finish;
Jakub Jelen aacf0d
-	memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
Jakub Jelen aacf0d
-	k11->rsa_method.name = "pkcs11";
Jakub Jelen aacf0d
-	k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
Jakub Jelen aacf0d
-	k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
Jakub Jelen aacf0d
-	k11->rsa_method.finish = pkcs11_rsa_finish;
Jakub Jelen aacf0d
-	RSA_set_method(rsa, &k11->rsa_method);
Jakub Jelen aacf0d
+	k11->orig_finish = RSA_meth_get_finish(def);
Jakub Jelen aacf0d
+	if ((k11->rsa_method = RSA_meth_dup(def)) == NULL ||
Jakub Jelen aacf0d
+	    RSA_meth_set1_name(k11->rsa_method, "pkcs11") == 0 ||
Jakub Jelen aacf0d
+	    RSA_meth_set_priv_enc(k11->rsa_method, pkcs11_rsa_private_encrypt) == 0 ||
Jakub Jelen aacf0d
+	    RSA_meth_set_priv_dec(k11->rsa_method, pkcs11_rsa_private_decrypt) == 0 ||
Jakub Jelen aacf0d
+	    RSA_meth_set_finish(k11->rsa_method, pkcs11_rsa_finish) == 0) {
Jakub Jelen aacf0d
+		RSA_meth_free(k11->rsa_method);
Jakub Jelen aacf0d
+		k11->rsa_method = NULL;
Jakub Jelen aacf0d
+		pkcs11_provider_unref(k11->provider);
Jakub Jelen aacf0d
+		free(k11->keyid);
Jakub Jelen aacf0d
+		free(k11);
Jakub Jelen aacf0d
+		return (-1);
Jakub Jelen aacf0d
+	}
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+	RSA_set_method(rsa, k11->rsa_method);
Jakub Jelen aacf0d
 	RSA_set_app_data(rsa, k11);
Jakub Jelen aacf0d
 	return (0);
Jakub Jelen aacf0d
 }
Jakub Jelen aacf0d
@@ -460,6 +468,7 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
Jakub Jelen aacf0d
 	CK_ULONG		nfound;
Jakub Jelen aacf0d
 	CK_SESSION_HANDLE	session;
Jakub Jelen aacf0d
 	CK_FUNCTION_LIST	*f;
Jakub Jelen aacf0d
+	const BIGNUM		*n, *e;
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	f = p->function_list;
Jakub Jelen aacf0d
 	session = p->slotinfo[slotidx].session;
Jakub Jelen aacf0d
@@ -512,10 +521,14 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
Jakub Jelen aacf0d
 			if ((rsa = RSA_new()) == NULL) {
Jakub Jelen aacf0d
 				error("RSA_new failed");
Jakub Jelen aacf0d
 			} else {
Jakub Jelen aacf0d
-				rsa->n = BN_bin2bn(attribs[1].pValue,
Jakub Jelen aacf0d
+				BIGNUM *rsa_n, *rsa_e;
Jakub Jelen aacf0d
+
Jakub Jelen aacf0d
+				rsa_n = BN_bin2bn(attribs[1].pValue,
Jakub Jelen aacf0d
 				    attribs[1].ulValueLen, NULL);
Jakub Jelen aacf0d
-				rsa->e = BN_bin2bn(attribs[2].pValue,
Jakub Jelen aacf0d
+				rsa_e = BN_bin2bn(attribs[2].pValue,
Jakub Jelen aacf0d
 				    attribs[2].ulValueLen, NULL);
Jakub Jelen aacf0d
+				if (RSA_set0_key(rsa, rsa_n, rsa_e, NULL) == 0)
Jakub Jelen aacf0d
+					error("RSA_set0_key failed");
Jakub Jelen aacf0d
 			}
Jakub Jelen aacf0d
 		} else {
Jakub Jelen aacf0d
 			cp = attribs[2].pValue;
Jakub Jelen aacf0d
@@ -525,17 +538,18 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
Jakub Jelen aacf0d
 			    == NULL) {
Jakub Jelen aacf0d
 				error("d2i_X509 failed");
Jakub Jelen aacf0d
 			} else if ((evp = X509_get_pubkey(x509)) == NULL ||
Jakub Jelen aacf0d
-			    evp->type != EVP_PKEY_RSA ||
Jakub Jelen aacf0d
-			    evp->pkey.rsa == NULL) {
Jakub Jelen aacf0d
+			    EVP_PKEY_id(evp) != EVP_PKEY_RSA ||
Jakub Jelen aacf0d
+			    EVP_PKEY_get0_RSA(evp) == NULL) {
Jakub Jelen aacf0d
 				debug("X509_get_pubkey failed or no rsa");
Jakub Jelen aacf0d
-			} else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
Jakub Jelen aacf0d
+			} else if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp)))
Jakub Jelen aacf0d
 			    == NULL) {
Jakub Jelen aacf0d
 				error("RSAPublicKey_dup");
Jakub Jelen aacf0d
 			}
Jakub Jelen aacf0d
 			if (x509)
Jakub Jelen aacf0d
 				X509_free(x509);
Jakub Jelen aacf0d
 		}
Jakub Jelen aacf0d
-		if (rsa && rsa->n && rsa->e &&
Jakub Jelen aacf0d
+		RSA_get0_key(rsa, &n, &e, NULL);
Jakub Jelen aacf0d
+		if (rsa && n && e &&
Jakub Jelen aacf0d
 		    pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
Jakub Jelen aacf0d
 			key = sshkey_new(KEY_UNSPEC);
Jakub Jelen aacf0d
 			key->rsa = rsa;
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/ssh-rsa.c.openssl openssh-7.4p1/ssh-rsa.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/ssh-rsa.c.openssl	2016-12-19 05:59:41.000000000 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/ssh-rsa.c	2016-12-23 17:47:36.437817755 +0100
Jakub Jelen aacf0d
@@ -100,7 +100,7 @@ ssh_rsa_sign(const struct sshkey *key, u
Jakub Jelen aacf0d
 		hash_alg = rsa_hash_alg_from_ident(alg_ident);
Jakub Jelen aacf0d
 	if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
Jakub Jelen aacf0d
 	    sshkey_type_plain(key->type) != KEY_RSA ||
Jakub Jelen aacf0d
-	    BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Jakub Jelen aacf0d
+	    RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d
 	slen = RSA_size(key->rsa);
Jakub Jelen aacf0d
 	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
Jakub Jelen aacf0d
@@ -172,7 +172,7 @@ ssh_rsa_verify(const struct sshkey *key,
Jakub Jelen aacf0d
 
Jakub Jelen aacf0d
 	if (key == NULL || key->rsa == NULL ||
Jakub Jelen aacf0d
 	    sshkey_type_plain(key->type) != KEY_RSA ||
Jakub Jelen aacf0d
-	    BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE ||
Jakub Jelen aacf0d
+	    RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE ||
Jakub Jelen aacf0d
 	    sig == NULL || siglen == 0)
Jakub Jelen aacf0d
 		return SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen aacf0d