$ git diff --patch-with-stat --summary dd0d0d9e45a5f23b07a6106e7a6194b442693ce5..be36b9ae81d0604a3289f01c5e88e09094cd0dbf
.abf.yml | 4 +-
48.patch | 2639 ------------------------
openssh-01-fix-pam-uclibc-pthreads-clash.patch | 44 -
openssh-7.4p1.tar.gz.asc | 13 -
openssh-7.7p1-openssl-1.1.0-1.patch | 1971 ++++++++++++++++++
openssh-omdv_conf.patch | 29 +-
openssh.spec | 34 +-
7 files changed, 2001 insertions(+), 2733 deletions(-)
delete mode 100644 48.patch
delete mode 100644 openssh-01-fix-pam-uclibc-pthreads-clash.patch
delete mode 100644 openssh-7.4p1.tar.gz.asc
create mode 100644 openssh-7.7p1-openssl-1.1.0-1.patch
diff --git a/.abf.yml b/.abf.yml
index 1d67b63..0b495de 100644
--- a/.abf.yml
+++ b/.abf.yml
@@ -1,4 +1,4 @@
sources:
openssh-4.4p1-watchdog.patch.tgz: e4a13018c494faf7ed06fd74dad720a8a8ac9089
- openssh-7.5p1.tar.gz: 5e8f185d00afb4f4f89801e9b0f8b9cee9d87ebd
- openssh-7.5p1.tar.gz.asc: 4238263abdd51d6d068e94ad868047e41b71a7f1
+ openssh-7.7p1.tar.gz: 446fe9ed171f289f0d62197dffdbfdaaf21c49f2
+
diff --git a/48.patch b/48.patch
deleted file mode 100644
index d76ade2..0000000
--- a/48.patch
+++ /dev/null
@@ -1,2639 +0,0 @@
-From 86be7a0227caf1ce94ce16596593381f621ec458 Mon Sep 17 00:00:00 2001
-From: Kurt Roeckx <kurt@roeckx.be>
-Date: Sat, 17 Sep 2016 19:08:27 +0200
-Subject: [PATCH 1/3] Make it build using OpenSSL 1.1.0
-
----
- Makefile.in | 2 +-
- cipher-3des1.c | 4 +
- cipher-bf1.c | 4 +-
- cipher.c | 14 +-
- dh.c | 54 ++--
- dh.h | 2 +-
- digest-openssl.c | 17 +-
- includes.h | 1 +
- kexdhc.c | 19 +-
- kexdhs.c | 10 +-
- kexgexc.c | 28 +-
- kexgexs.c | 19 +-
- libcrypto-compat.c | 445 ++++++++++++++++++++++++++++
- libcrypto-compat.h | 58 ++++
- monitor.c | 7 +-
- regress/unittests/sshkey/test_file.c | 17 +-
- regress/unittests/sshkey/test_sshkey.c | 25 +-
- rsa.c | 41 ++-
- rsa.h | 2 +-
- ssh-dss.c | 27 +-
- ssh-ecdsa.c | 26 +-
- ssh-keygen.c | 93 +++---
- ssh-pkcs11-client.c | 12 +-
- ssh-pkcs11.c | 42 ++-
- ssh-rsa.c | 12 +-
- sshkey.c | 524 +++++++++++++++++++++++----------
- 26 files changed, 1172 insertions(+), 333 deletions(-)
- create mode 100644 libcrypto-compat.c
- create mode 100644 libcrypto-compat.h
-
-diff --git a/Makefile.in b/Makefile.in
-index d6df2ff..68d9c46 100644
---- a/Makefile.in
-+++ b/Makefile.in
-@@ -92,7 +92,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
- kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
- kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
- kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
-- platform-pledge.o platform-tracing.o
-+ platform-pledge.o platform-tracing.o libcrypto-compat.o
-
- SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
- sshconnect.o sshconnect1.o sshconnect2.o mux.o
-diff --git a/cipher.c b/cipher.c
-index 747b59b..66c451b 100644
---- a/cipher.c
-+++ b/cipher.c
-@@ -372,7 +372,7 @@ cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
-+ if (EVP_CipherInit(cc->evp, type, (u_char *)key, (u_char *)iv,
- (do_encrypt == CIPHER_ENCRYPT)) == 0) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
-@@ -390,10 +390,6 @@ cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
- goto out;
- }
- }
-- if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
-- ret = SSH_ERR_LIBCRYPTO_ERROR;
-- goto out;
-- }
-
- if (cipher->discard_len > 0) {
- if ((junk = malloc(cipher->discard_len)) == NULL ||
-@@ -625,7 +621,7 @@ cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
- len, iv))
- return SSH_ERR_LIBCRYPTO_ERROR;
- } else
-- memcpy(iv, cc->evp->iv, len);
-+ memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
- break;
- #endif
- #ifdef WITH_SSH1
-@@ -665,7 +661,7 @@ cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
- EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
- return SSH_ERR_LIBCRYPTO_ERROR;
- } else
-- memcpy(cc->evp->iv, iv, evplen);
-+ memcpy(EVP_CIPHER_CTX_iv_noconst(cc->evp), iv, evplen);
- break;
- #endif
- #ifdef WITH_SSH1
-@@ -679,8 +675,8 @@ cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
- }
-
- #ifdef WITH_OPENSSL
--#define EVP_X_STATE(evp) (evp)->cipher_data
--#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
-+#define EVP_X_STATE(evp) EVP_CIPHER_CTX_get_cipher_data(evp)
-+#define EVP_X_STATE_LEN(evp) EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
- #endif
-
- int
-diff --git a/dh.c b/dh.c
-index 194f29b..10a5eb9 100644
---- a/dh.c
-+++ b/dh.c
-@@ -212,14 +212,15 @@ choose_dh(int min, int wantbits, int max)
- /* diffie-hellman-groupN-sha1 */
-
- int
--dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
-+dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
- {
- int i;
- int n = BN_num_bits(dh_pub);
- int bits_set = 0;
- BIGNUM *tmp;
-+ const BIGNUM *p;
-
-- if (dh_pub->neg) {
-+ if (BN_is_negative(dh_pub)) {
- logit("invalid public DH value: negative");
- return 0;
- }
-@@ -232,7 +233,8 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
- error("%s: BN_new failed", __func__);
- return 0;
- }
-- if (!BN_sub(tmp, dh->p, BN_value_one()) ||
-+ DH_get0_pqg(dh, &p, NULL, NULL);
-+ if (!BN_sub(tmp, p, BN_value_one()) ||
- BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */
- BN_clear_free(tmp);
- logit("invalid public DH value: >= p-1");
-@@ -243,14 +245,14 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
- for (i = 0; i <= n; i++)
- if (BN_is_bit_set(dh_pub, i))
- bits_set++;
-- debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
-+ debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
-
- /*
- * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
- */
- if (bits_set < 4) {
- logit("invalid public DH value (%d/%d)",
-- bits_set, BN_num_bits(dh->p));
-+ bits_set, BN_num_bits(p));
- return 0;
- }
- return 1;
-@@ -260,9 +262,11 @@ int
- dh_gen_key(DH *dh, int need)
- {
- int pbits;
-+ const BIGNUM *p, *pub_key;
-
-- if (need < 0 || dh->p == NULL ||
-- (pbits = BN_num_bits(dh->p)) <= 0 ||
-+ DH_get0_pqg(dh, &p, NULL, NULL);
-+ if (need < 0 || p == NULL ||
-+ (pbits = BN_num_bits(p)) <= 0 ||
- need > INT_MAX / 2 || 2 * need > pbits)
- return SSH_ERR_INVALID_ARGUMENT;
- if (need < 256)
-@@ -271,12 +275,12 @@ dh_gen_key(DH *dh, int need)
- * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
- * so double requested need here.
- */
-- dh->length = MINIMUM(need * 2, pbits - 1);
-- if (DH_generate_key(dh) == 0 ||
-- !dh_pub_is_valid(dh, dh->pub_key)) {
-- BN_clear_free(dh->priv_key);
-+ DH_set_length(dh, MINIMUM(need * 2, pbits - 1));
-+ if (DH_generate_key(dh) == 0)
-+ return SSH_ERR_LIBCRYPTO_ERROR;
-+ DH_get0_key(dh, &pub_key, NULL);
-+ if (!dh_pub_is_valid(dh, pub_key))
- return SSH_ERR_LIBCRYPTO_ERROR;
-- }
- return 0;
- }
-
-@@ -284,15 +288,22 @@ DH *
- dh_new_group_asc(const char *gen, const char *modulus)
- {
- DH *dh;
--
-- if ((dh = DH_new()) == NULL)
-- return NULL;
-- if (BN_hex2bn(&dh->p, modulus) == 0 ||
-- BN_hex2bn(&dh->g, gen) == 0) {
-- DH_free(dh);
-- return NULL;
-- }
-+ BIGNUM *p, *g;
-+
-+ if ((dh = DH_new()) == NULL ||
-+ (p = BN_new()) == NULL ||
-+ (g = BN_new()) == NULL)
-+ goto err;
-+ if (BN_hex2bn(&p, modulus) == 0 ||
-+ BN_hex2bn(&g, gen) == 0 ||
-+ DH_set0_pqg(dh, p, NULL, g) == 0)
-+ goto err;
- return (dh);
-+err:
-+ DH_free(dh);
-+ BN_free(p);
-+ BN_free(g);
-+ return NULL;
- }
-
- /*
-@@ -307,8 +318,7 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulus)
-
- if ((dh = DH_new()) == NULL)
- return NULL;
-- dh->p = modulus;
-- dh->g = gen;
-+ DH_set0_pqg(dh, modulus, NULL, gen);
-
- return (dh);
- }
-diff --git a/dh.h b/dh.h
-index bcd485c..344b29e 100644
---- a/dh.h
-+++ b/dh.h
-@@ -42,7 +42,7 @@ DH *dh_new_group18(void);
- DH *dh_new_group_fallback(int);
-
- int dh_gen_key(DH *, int);
--int dh_pub_is_valid(DH *, BIGNUM *);
-+int dh_pub_is_valid(const DH *, const BIGNUM *);
-
- u_int dh_estimate(int);
-
-diff --git a/digest-openssl.c b/digest-openssl.c
-index 13b63c2..b0b95a4 100644
---- a/digest-openssl.c
-+++ b/digest-openssl.c
-@@ -43,7 +43,7 @@
-
- struct ssh_digest_ctx {
- int alg;
-- EVP_MD_CTX mdctx;
-+ EVP_MD_CTX *mdctx;
- };
-
- struct ssh_digest {
-@@ -107,7 +107,7 @@ ssh_digest_bytes(int alg)
- size_t
- ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
- {
-- return EVP_MD_CTX_block_size(&ctx->mdctx);
-+ return EVP_MD_CTX_block_size(ctx->mdctx);
- }
-
- struct ssh_digest_ctx *
-@@ -119,8 +119,9 @@ ssh_digest_start(int alg)
- if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
- return NULL;
- ret->alg = alg;
-- EVP_MD_CTX_init(&ret->mdctx);
-- if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
-+ ret->mdctx = EVP_MD_CTX_new();
-+ if (ret->mdctx == NULL ||
-+ EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
- free(ret);
- return NULL;
- }
-@@ -133,7 +134,7 @@ ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
- if (from->alg != to->alg)
- return SSH_ERR_INVALID_ARGUMENT;
- /* we have bcopy-style order while openssl has memcpy-style */
-- if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
-+ if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
- return SSH_ERR_LIBCRYPTO_ERROR;
- return 0;
- }
-@@ -141,7 +142,7 @@ ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
- int
- ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
- {
-- if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
-+ if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
- return SSH_ERR_LIBCRYPTO_ERROR;
- return 0;
- }
-@@ -162,7 +163,7 @@ ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
- return SSH_ERR_INVALID_ARGUMENT;
- if (dlen < digest->digest_len) /* No truncation allowed */
- return SSH_ERR_INVALID_ARGUMENT;
-- if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
-+ if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
- return SSH_ERR_LIBCRYPTO_ERROR;
- if (l != digest->digest_len) /* sanity */
- return SSH_ERR_INTERNAL_ERROR;
-@@ -173,7 +174,7 @@ void
- ssh_digest_free(struct ssh_digest_ctx *ctx)
- {
- if (ctx != NULL) {
-- EVP_MD_CTX_cleanup(&ctx->mdctx);
-+ EVP_MD_CTX_free(ctx->mdctx);
- explicit_bzero(ctx, sizeof(*ctx));
- free(ctx);
- }
-diff --git a/includes.h b/includes.h
-index 497a038..7e602f5 100644
---- a/includes.h
-+++ b/includes.h
-@@ -163,6 +163,7 @@
-
- #ifdef WITH_OPENSSL
- #include <openssl/opensslv.h> /* For OPENSSL_VERSION_NUMBER */
-+#include "libcrypto-compat.h"
- #endif
-
- #include "defines.h"
-diff --git a/kexdhc.c b/kexdhc.c
-index ad3975f..0a55092 100644
---- a/kexdhc.c
-+++ b/kexdhc.c
-@@ -56,6 +56,7 @@ kexdh_client(struct ssh *ssh)
- {
- struct kex *kex = ssh->kex;
- int r;
-+ const BIGNUM *pub_key;
-
- /* generate and send 'e', client DH public key */
- switch (kex->kex_type) {
-@@ -81,21 +82,27 @@ kexdh_client(struct ssh *ssh)
- goto out;
- }
- debug("sending SSH2_MSG_KEXDH_INIT");
-- if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
-- (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
-- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
-+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
-+ goto out;
-+ DH_get0_key(kex->dh, &pub_key, NULL);
-+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
-+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
- (r = sshpkt_send(ssh)) != 0)
- goto out;
- #ifdef DEBUG_KEXDH
- DHparams_print_fp(stderr, kex->dh);
- fprintf(stderr, "pub= ");
-- BN_print_fp(stderr, kex->dh->pub_key);
-+ BN_print_fp(stderr, pub_key);
- fprintf(stderr, "\n");
- #endif
- debug("expecting SSH2_MSG_KEXDH_REPLY");
- ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
- r = 0;
- out:
-+ if (r != 0) {
-+ DH_free(kex->dh);
-+ kex->dh = NULL;
-+ }
- return r;
- }
-
-@@ -110,6 +117,7 @@ input_kex_dh(int type, u_int32_t seq, void *ctxt)
- u_char hash[SSH_DIGEST_MAX_LENGTH];
- size_t klen = 0, slen, sbloblen, hashlen;
- int kout, r;
-+ const BIGNUM *pub_key;
-
- if (kex->verify_host_key == NULL) {
- r = SSH_ERR_INVALID_ARGUMENT;
-@@ -169,6 +177,7 @@ input_kex_dh(int type, u_int32_t seq, void *ctxt)
- #endif
-
- /* calc and verify H */
-+ DH_get0_key(kex->dh, &pub_key, NULL);
- hashlen = sizeof(hash);
- if ((r = kex_dh_hash(
- kex->hash_alg,
-@@ -177,7 +186,7 @@ input_kex_dh(int type, u_int32_t seq, void *ctxt)
- sshbuf_ptr(kex->my), sshbuf_len(kex->my),
- sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
- server_host_key_blob, sbloblen,
-- kex->dh->pub_key,
-+ pub_key,
- dh_server_pub,
- shared_secret,
- hash, &hashlen)) != 0)
-diff --git a/kexdhs.c b/kexdhs.c
-index 108f664..8d0906e 100644
---- a/kexdhs.c
-+++ b/kexdhs.c
-@@ -87,6 +87,10 @@ kexdh_server(struct ssh *ssh)
- ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
- r = 0;
- out:
-+ if (r != 0) {
-+ DH_free(kex->dh);
-+ kex->dh = NULL;
-+ }
- return r;
- }
-
-@@ -102,6 +106,7 @@ input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
- size_t sbloblen, slen;
- size_t klen = 0, hashlen;
- int kout, r;
-+ const BIGNUM *pub_key;
-
- if (kex->load_host_public_key == NULL ||
- kex->load_host_private_key == NULL) {
-@@ -164,6 +169,7 @@ input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
- goto out;
- /* calc H */
- hashlen = sizeof(hash);
-+ DH_get0_key(kex->dh, &pub_key, NULL);
- if ((r = kex_dh_hash(
- kex->hash_alg,
- kex->client_version_string,
-@@ -172,7 +178,7 @@ input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
- sshbuf_ptr(kex->my), sshbuf_len(kex->my),
- server_host_key_blob, sbloblen,
- dh_client_pub,
-- kex->dh->pub_key,
-+ pub_key,
- shared_secret,
- hash, &hashlen)) != 0)
- goto out;
-@@ -198,7 +204,7 @@ input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
- /* send server hostkey, DH pubkey 'f' and singed H */
- if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
- (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
-- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
-+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
- (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
- (r = sshpkt_send(ssh)) != 0)
- goto out;
-diff --git a/kexgexc.c b/kexgexc.c
-index ad0d1c8..da17c49 100644
---- a/kexgexc.c
-+++ b/kexgexc.c
-@@ -95,6 +95,7 @@ input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt)
- struct kex *kex = ssh->kex;
- BIGNUM *p = NULL, *g = NULL;
- int r, bits;
-+ const BIGNUM *pub_key;
-
- debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
-
-@@ -119,26 +120,30 @@ input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt)
- p = g = NULL; /* belong to kex->dh now */
-
- /* generate and send 'e', client DH public key */
-- if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
-- (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
-- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
-+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
-+ goto out;
-+ DH_get0_key(kex->dh, &pub_key, NULL);
-+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
-+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
- (r = sshpkt_send(ssh)) != 0)
- goto out;
- debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
- #ifdef DEBUG_KEXDH
- DHparams_print_fp(stderr, kex->dh);
- fprintf(stderr, "pub= ");
-- BN_print_fp(stderr, kex->dh->pub_key);
-+ BN_print_fp(stderr, pub_key);
- fprintf(stderr, "\n");
- #endif
- ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
- ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
- r = 0;
- out:
-- if (p)
-- BN_clear_free(p);
-- if (g)
-- BN_clear_free(g);
-+ BN_clear_free(p);
-+ BN_clear_free(g);
-+ if (r != 0) {
-+ DH_free(kex->dh);
-+ kex->dh = NULL;
-+ }
- return r;
- }
-
-@@ -153,6 +158,7 @@ input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt)
- u_char hash[SSH_DIGEST_MAX_LENGTH];
- size_t klen = 0, slen, sbloblen, hashlen;
- int kout, r;
-+ const BIGNUM *p, *g, *pub_key;
-
- debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
- if (kex->verify_host_key == NULL) {
-@@ -219,6 +225,8 @@ input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt)
- kex->min = kex->max = -1;
-
- /* calc and verify H */
-+ DH_get0_pqg(kex->dh, &p, NULL, &g);
-+ DH_get0_key(kex->dh, &pub_key, NULL);
- hashlen = sizeof(hash);
- if ((r = kexgex_hash(
- kex->hash_alg,
-@@ -228,8 +236,8 @@ input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt)
- sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
- server_host_key_blob, sbloblen,
- kex->min, kex->nbits, kex->max,
-- kex->dh->p, kex->dh->g,
-- kex->dh->pub_key,
-+ p, g,
-+ pub_key,
- dh_server_pub,
- shared_secret,
- hash, &hashlen)) != 0)
-diff --git a/kexgexs.c b/kexgexs.c
-index 4496035..7cd8c7e 100644
---- a/kexgexs.c
-+++ b/kexgexs.c
-@@ -73,6 +73,7 @@ input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
- struct kex *kex = ssh->kex;
- int r;
- u_int min = 0, max = 0, nbits = 0;
-+ const BIGNUM *p, *g;
-
- debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
- if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
-@@ -102,9 +103,10 @@ input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
- goto out;
- }
- debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
-+ DH_get0_pqg(kex->dh, &p, NULL, &g);
- if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
-- (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
-- (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
-+ (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
-+ (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
- (r = sshpkt_send(ssh)) != 0)
- goto out;
-
-@@ -116,6 +118,10 @@ input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
- ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
- r = 0;
- out:
-+ if (r != 0) {
-+ DH_free(kex->dh);
-+ kex->dh = NULL;
-+ }
- return r;
- }
-
-@@ -131,6 +137,7 @@ input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
- size_t sbloblen, slen;
- size_t klen = 0, hashlen;
- int kout, r;
-+ const BIGNUM *p, *g, *pub_key;
-
- if (kex->load_host_public_key == NULL ||
- kex->load_host_private_key == NULL) {
-@@ -193,6 +200,8 @@ input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
- goto out;
- /* calc H */
- hashlen = sizeof(hash);
-+ DH_get0_pqg(kex->dh, &p, NULL, &g);
-+ DH_get0_key(kex->dh, &pub_key, NULL);
- if ((r = kexgex_hash(
- kex->hash_alg,
- kex->client_version_string,
-@@ -201,9 +210,9 @@ input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
- sshbuf_ptr(kex->my), sshbuf_len(kex->my),
- server_host_key_blob, sbloblen,
- kex->min, kex->nbits, kex->max,
-- kex->dh->p, kex->dh->g,
-+ p, g,
- dh_client_pub,
-- kex->dh->pub_key,
-+ pub_key,
- shared_secret,
- hash, &hashlen)) != 0)
- goto out;
-@@ -229,7 +238,7 @@ input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
- /* send server hostkey, DH pubkey 'f' and singed H */
- if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
- (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
-- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
-+ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
- (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
- (r = sshpkt_send(ssh)) != 0)
- goto out;
-diff --git a/libcrypto-compat.c b/libcrypto-compat.c
-new file mode 100644
-index 0000000..d92c8d9
---- /dev/null
-+++ b/libcrypto-compat.c
-@@ -0,0 +1,445 @@
-+/*
-+ * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
-+ *
-+ * Licensed under the OpenSSL license (the "License"). You may not use
-+ * this file except in compliance with the License. You can obtain a copy
-+ * in the file LICENSE in the source distribution or at
-+ * https://www.openssl.org/source/license.html
-+ */
-+
-+#include "includes.h"
-+
-+#if OPENSSL_VERSION_NUMBER < 0x10100000L
-+
-+#include <string.h>
-+#include <openssl/engine.h>
-+
-+static void *OPENSSL_zalloc(size_t num)
-+{
-+ void *ret = OPENSSL_malloc(num);
-+
-+ if (ret != NULL)
-+ memset(ret, 0, num);
-+ return ret;
-+}
-+
-+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
-+{
-+ /* If the fields n and e in r are NULL, the corresponding input
-+ * parameters MUST be non-NULL for n and e. d may be
-+ * left NULL (in case only the public key is used).
-+ */
-+ if ((r->n == NULL && n == NULL)
-+ || (r->e == NULL && e == NULL))
-+ return 0;
-+
-+ if (n != NULL) {
-+ BN_free(r->n);
-+ r->n = n;
-+ }
-+ if (e != NULL) {
-+ BN_free(r->e);
-+ r->e = e;
-+ }
-+ if (d != NULL) {
-+ BN_free(r->d);
-+ r->d = d;
-+ }
-+
-+ return 1;
-+}
-+
-+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
-+{
-+ /* If the fields p and q in r are NULL, the corresponding input
-+ * parameters MUST be non-NULL.
-+ */
-+ if ((r->p == NULL && p == NULL)
-+ || (r->q == NULL && q == NULL))
-+ return 0;
-+
-+ if (p != NULL) {
-+ BN_free(r->p);
-+ r->p = p;
-+ }
-+ if (q != NULL) {
-+ BN_free(r->q);
-+ r->q = q;
-+ }
-+
-+ return 1;
-+}
-+
-+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
-+{
-+ /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
-+ * parameters MUST be non-NULL.
-+ */
-+ if ((r->dmp1 == NULL && dmp1 == NULL)
-+ || (r->dmq1 == NULL && dmq1 == NULL)
-+ || (r->iqmp == NULL && iqmp == NULL))
-+ return 0;
-+
-+ if (dmp1 != NULL) {
-+ BN_free(r->dmp1);
-+ r->dmp1 = dmp1;
-+ }
-+ if (dmq1 != NULL) {
-+ BN_free(r->dmq1);
-+ r->dmq1 = dmq1;
-+ }
-+ if (iqmp != NULL) {
-+ BN_free(r->iqmp);
-+ r->iqmp = iqmp;
-+ }
-+
-+ return 1;
-+}
-+
-+void RSA_get0_key(const RSA *r,
-+ const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
-+{
-+ if (n != NULL)
-+ *n = r->n;
-+ if (e != NULL)
-+ *e = r->e;
-+ if (d != NULL)
-+ *d = r->d;
-+}
-+
-+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
-+{
-+ if (p != NULL)
-+ *p = r->p;
-+ if (q != NULL)
-+ *q = r->q;
-+}
-+
-+void RSA_get0_crt_params(const RSA *r,
-+ const BIGNUM **dmp1, const BIGNUM **dmq1,
-+ const BIGNUM **iqmp)
-+{
-+ if (dmp1 != NULL)
-+ *dmp1 = r->dmp1;
-+ if (dmq1 != NULL)
-+ *dmq1 = r->dmq1;
-+ if (iqmp != NULL)
-+ *iqmp = r->iqmp;
-+}
-+
-+void DSA_get0_pqg(const DSA *d,
-+ const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
-+{
-+ if (p != NULL)
-+ *p = d->p;
-+ if (q != NULL)
-+ *q = d->q;
-+ if (g != NULL)
-+ *g = d->g;
-+}
-+
-+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
-+{
-+ /* If the fields p, q and g in d are NULL, the corresponding input
-+ * parameters MUST be non-NULL.
-+ */
-+ if ((d->p == NULL && p == NULL)
-+ || (d->q == NULL && q == NULL)
-+ || (d->g == NULL && g == NULL))
-+ return 0;
-+
-+ if (p != NULL) {
-+ BN_free(d->p);
-+ d->p = p;
-+ }
-+ if (q != NULL) {
-+ BN_free(d->q);
-+ d->q = q;
-+ }
-+ if (g != NULL) {
-+ BN_free(d->g);
-+ d->g = g;
-+ }
-+
-+ return 1;
-+}
-+
-+void DSA_get0_key(const DSA *d,
-+ const BIGNUM **pub_key, const BIGNUM **priv_key)
-+{
-+ if (pub_key != NULL)
-+ *pub_key = d->pub_key;
-+ if (priv_key != NULL)
-+ *priv_key = d->priv_key;
-+}
-+
-+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
-+{
-+ /* If the field pub_key in d is NULL, the corresponding input
-+ * parameters MUST be non-NULL. The priv_key field may
-+ * be left NULL.
-+ */
-+ if (d->pub_key == NULL && pub_key == NULL)
-+ return 0;
-+
-+ if (pub_key != NULL) {
-+ BN_free(d->pub_key);
-+ d->pub_key = pub_key;
-+ }
-+ if (priv_key != NULL) {
-+ BN_free(d->priv_key);
-+ d->priv_key = priv_key;
-+ }
-+
-+ return 1;
-+}
-+
-+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
-+{
-+ if (pr != NULL)
-+ *pr = sig->r;
-+ if (ps != NULL)
-+ *ps = sig->s;
-+}
-+
-+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
-+{
-+ if (r == NULL || s == NULL)
-+ return 0;
-+ BN_clear_free(sig->r);
-+ BN_clear_free(sig->s);
-+ sig->r = r;
-+ sig->s = s;
-+ return 1;
-+}
-+
-+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
-+{
-+ if (pr != NULL)
-+ *pr = sig->r;
-+ if (ps != NULL)
-+ *ps = sig->s;
-+}
-+
-+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
-+{
-+ if (r == NULL || s == NULL)
-+ return 0;
-+ BN_clear_free(sig->r);
-+ BN_clear_free(sig->s);
-+ sig->r = r;
-+ sig->s = s;
-+ return 1;
-+}
-+
-+void DH_get0_pqg(const DH *dh,
-+ const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
-+{
-+ if (p != NULL)
-+ *p = dh->p;
-+ if (q != NULL)
-+ *q = dh->q;
-+ if (g != NULL)
-+ *g = dh->g;
-+}
-+
-+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
-+{
-+ /* If the fields p and g in d are NULL, the corresponding input
-+ * parameters MUST be non-NULL. q may remain NULL.
-+ */
-+ if ((dh->p == NULL && p == NULL)
-+ || (dh->g == NULL && g == NULL))
-+ return 0;
-+
-+ if (p != NULL) {
-+ BN_free(dh->p);
-+ dh->p = p;
-+ }
-+ if (q != NULL) {
-+ BN_free(dh->q);
-+ dh->q = q;
-+ }
-+ if (g != NULL) {
-+ BN_free(dh->g);
-+ dh->g = g;
-+ }
-+
-+ if (q != NULL) {
-+ dh->length = BN_num_bits(q);
-+ }
-+
-+ return 1;
-+}
-+
-+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
-+{
-+ if (pub_key != NULL)
-+ *pub_key = dh->pub_key;
-+ if (priv_key != NULL)
-+ *priv_key = dh->priv_key;
-+}
-+
-+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
-+{
-+ /* If the field pub_key in dh is NULL, the corresponding input
-+ * parameters MUST be non-NULL. The priv_key field may
-+ * be left NULL.
-+ */
-+ if (dh->pub_key == NULL && pub_key == NULL)
-+ return 0;
-+
-+ if (pub_key != NULL) {
-+ BN_free(dh->pub_key);
-+ dh->pub_key = pub_key;
-+ }
-+ if (priv_key != NULL) {
-+ BN_free(dh->priv_key);
-+ dh->priv_key = priv_key;
-+ }
-+
-+ return 1;
-+}
-+
-+int DH_set_length(DH *dh, long length)
-+{
-+ dh->length = length;
-+ return 1;
-+}
-+
-+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
-+{
-+ return ctx->iv;
-+}
-+
-+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
-+{
-+ return ctx->iv;
-+}
-+
-+EVP_MD_CTX *EVP_MD_CTX_new(void)
-+{
-+ return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
-+}
-+
-+static void OPENSSL_clear_free(void *str, size_t num)
-+{
-+ if (str == NULL)
-+ return;
-+ if (num)
-+ OPENSSL_cleanse(str, num);
-+ OPENSSL_free(str);
-+}
-+
-+/* This call frees resources associated with the context */
-+static int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
-+{
-+ if (ctx == NULL)
-+ return 1;
-+
-+ /*
-+ * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
-+ * sometimes only copies of the context are ever finalised.
-+ */
-+ if (ctx->digest && ctx->digest->cleanup
-+ && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
-+ ctx->digest->cleanup(ctx);
-+ if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
-+ && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
-+ OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
-+ }
-+ EVP_PKEY_CTX_free(ctx->pctx);
-+#ifndef OPENSSL_NO_ENGINE
-+ ENGINE_finish(ctx->engine);
-+#endif
-+ OPENSSL_cleanse(ctx, sizeof(*ctx));
-+
-+ return 1;
-+}
-+
-+void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
-+{
-+ EVP_MD_CTX_reset(ctx);
-+ OPENSSL_free(ctx);
-+}
-+
-+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
-+{
-+ RSA_METHOD *ret;
-+
-+ ret = OPENSSL_malloc(sizeof(RSA_METHOD));
-+
-+ if (ret != NULL) {
-+ memcpy(ret, meth, sizeof(*meth));
-+ ret->name = OPENSSL_strdup(meth->name);
-+ if (ret->name == NULL) {
-+ OPENSSL_free(ret);
-+ return NULL;
-+ }
-+ }
-+
-+ return ret;
-+}
-+
-+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
-+{
-+ char *tmpname;
-+
-+ tmpname = OPENSSL_strdup(name);
-+ if (tmpname == NULL) {
-+ return 0;
-+ }
-+
-+ OPENSSL_free((char *)meth->name);
-+ meth->name = tmpname;
-+
-+ return 1;
-+}
-+
-+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))
-+{
-+ meth->rsa_priv_enc = priv_enc;
-+ return 1;
-+}
-+
-+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))
-+{
-+ meth->rsa_priv_dec = priv_dec;
-+ return 1;
-+}
-+
-+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
-+{
-+ meth->finish = finish;
-+ return 1;
-+}
-+
-+void RSA_meth_free(RSA_METHOD *meth)
-+{
-+ if (meth != NULL) {
-+ OPENSSL_free((char *)meth->name);
-+ OPENSSL_free(meth);
-+ }
-+}
-+
-+int RSA_bits(const RSA *r)
-+{
-+ return (BN_num_bits(r->n));
-+}
-+
-+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
-+{
-+ if (pkey->type != EVP_PKEY_RSA) {
-+ return NULL;
-+ }
-+ return pkey->pkey.rsa;
-+}
-+
-+
-+#endif /* OPENSSL_VERSION_NUMBER */
-diff --git a/libcrypto-compat.h b/libcrypto-compat.h
-new file mode 100644
-index 0000000..e0599b4
---- /dev/null
-+++ b/libcrypto-compat.h
-@@ -0,0 +1,58 @@
-+#ifndef LIBCRYPTO_COMPAT_H
-+#define LIBCRYPTO_COMPAT_H
-+
-+#if OPENSSL_VERSION_NUMBER < 0x10100000L
-+
-+#include <openssl/rsa.h>
-+#include <openssl/dsa.h>
-+#include <openssl/ecdsa.h>
-+#include <openssl/dh.h>
-+#include <openssl/evp.h>
-+
-+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
-+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
-+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
-+void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
-+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
-+void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp);
-+
-+void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
-+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
-+void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key);
-+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
-+
-+void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
-+int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
-+
-+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
-+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
-+
-+void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
-+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
-+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
-+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
-+int DH_set_length(DH *dh, long length);
-+
-+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx);
-+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx);
-+EVP_MD_CTX *EVP_MD_CTX_new(void);
-+void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
-+#define EVP_CIPHER_impl_ctx_size(e) e->ctx_size
-+#define EVP_CIPHER_CTX_get_cipher_data(ctx) ctx->cipher_data
-+
-+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth);
-+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name);
-+#define RSA_meth_get_finish(meth) meth->finish
-+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));
-+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));
-+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa));
-+void RSA_meth_free(RSA_METHOD *meth);
-+
-+int RSA_bits(const RSA *r);
-+
-+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey);
-+
-+#endif /* OPENSSL_VERSION_NUMBER */
-+
-+#endif /* LIBCRYPTO_COMPAT_H */
-+
-diff --git a/monitor.c b/monitor.c
-index bea8d8b..af7e49e 100644
---- a/monitor.c
-+++ b/monitor.c
-@@ -605,9 +605,12 @@ mm_answer_moduli(int sock, Buffer *m)
- return (0);
- } else {
- /* Send first bignum */
-+ const BIGNUM *p, *g;
-+
-+ DH_get0_pqg(dh, &p, NULL, &g);
- buffer_put_char(m, 1);
-- buffer_put_bignum2(m, dh->p);
-- buffer_put_bignum2(m, dh->g);
-+ buffer_put_bignum2(m, p);
-+ buffer_put_bignum2(m, g);
-
- DH_free(dh);
- }
-diff --git a/regress/unittests/sshkey/test_file.c b/regress/unittests/sshkey/test_file.c
-index 906491f..7235a25 100644
---- a/regress/unittests/sshkey/test_file.c
-+++ b/regress/unittests/sshkey/test_file.c
-@@ -46,6 +46,7 @@ sshkey_file_tests(void)
- struct sshbuf *buf, *pw;
- BIGNUM *a, *b, *c;
- char *cp;
-+ const BIGNUM *n, *p, *q, *g, *pub_key, *priv_key;
-
- TEST_START("load passphrase");
- pw = load_text_file("pw");
-@@ -109,9 +110,11 @@ sshkey_file_tests(void)
- a = load_bignum("rsa_1.param.n");
- b = load_bignum("rsa_1.param.p");
- c = load_bignum("rsa_1.param.q");
-- ASSERT_BIGNUM_EQ(k1->rsa->n, a);
-- ASSERT_BIGNUM_EQ(k1->rsa->p, b);
-- ASSERT_BIGNUM_EQ(k1->rsa->q, c);
-+ RSA_get0_key(k1->rsa, &n, NULL, NULL);
-+ RSA_get0_factors(k1->rsa, &p, &q);
-+ ASSERT_BIGNUM_EQ(n, a);
-+ ASSERT_BIGNUM_EQ(p, b);
-+ ASSERT_BIGNUM_EQ(q, c);
- BN_free(a);
- BN_free(b);
- BN_free(c);
-@@ -200,9 +203,11 @@ sshkey_file_tests(void)
- a = load_bignum("dsa_1.param.g");
- b = load_bignum("dsa_1.param.priv");
- c = load_bignum("dsa_1.param.pub");
-- ASSERT_BIGNUM_EQ(k1->dsa->g, a);
-- ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
-- ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
-+ DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
-+ DSA_get0_key(k1->dsa, &pub_key, &priv_key);
-+ ASSERT_BIGNUM_EQ(g, a);
-+ ASSERT_BIGNUM_EQ(priv_key, b);
-+ ASSERT_BIGNUM_EQ(pub_key, c);
- BN_free(a);
- BN_free(b);
- BN_free(c);
-diff --git a/regress/unittests/sshkey/test_sshkey.c b/regress/unittests/sshkey/test_sshkey.c
-index 1476dc2..c0f94ee 100644
---- a/regress/unittests/sshkey/test_sshkey.c
-+++ b/regress/unittests/sshkey/test_sshkey.c
-@@ -197,9 +197,6 @@ sshkey_tests(void)
- k1 = sshkey_new(KEY_RSA1);
- ASSERT_PTR_NE(k1, NULL);
- ASSERT_PTR_NE(k1->rsa, NULL);
-- ASSERT_PTR_NE(k1->rsa->n, NULL);
-- ASSERT_PTR_NE(k1->rsa->e, NULL);
-- ASSERT_PTR_EQ(k1->rsa->p, NULL);
- sshkey_free(k1);
- TEST_DONE();
-
-@@ -207,9 +204,6 @@ sshkey_tests(void)
- k1 = sshkey_new(KEY_RSA);
- ASSERT_PTR_NE(k1, NULL);
- ASSERT_PTR_NE(k1->rsa, NULL);
-- ASSERT_PTR_NE(k1->rsa->n, NULL);
-- ASSERT_PTR_NE(k1->rsa->e, NULL);
-- ASSERT_PTR_EQ(k1->rsa->p, NULL);
- sshkey_free(k1);
- TEST_DONE();
-
-@@ -217,8 +211,6 @@ sshkey_tests(void)
- k1 = sshkey_new(KEY_DSA);
- ASSERT_PTR_NE(k1, NULL);
- ASSERT_PTR_NE(k1->dsa, NULL);
-- ASSERT_PTR_NE(k1->dsa->g, NULL);
-- ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
- sshkey_free(k1);
- TEST_DONE();
-
-@@ -244,9 +236,6 @@ sshkey_tests(void)
- k1 = sshkey_new_private(KEY_RSA);
- ASSERT_PTR_NE(k1, NULL);
- ASSERT_PTR_NE(k1->rsa, NULL);
-- ASSERT_PTR_NE(k1->rsa->n, NULL);
-- ASSERT_PTR_NE(k1->rsa->e, NULL);
-- ASSERT_PTR_NE(k1->rsa->p, NULL);
- ASSERT_INT_EQ(sshkey_add_private(k1), 0);
- sshkey_free(k1);
- TEST_DONE();
-@@ -255,8 +244,6 @@ sshkey_tests(void)
- k1 = sshkey_new_private(KEY_DSA);
- ASSERT_PTR_NE(k1, NULL);
- ASSERT_PTR_NE(k1->dsa, NULL);
-- ASSERT_PTR_NE(k1->dsa->g, NULL);
-- ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
- ASSERT_INT_EQ(sshkey_add_private(k1), 0);
- sshkey_free(k1);
- TEST_DONE();
-@@ -295,18 +282,13 @@ sshkey_tests(void)
- ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
- ASSERT_PTR_NE(kr, NULL);
- ASSERT_PTR_NE(kr->rsa, NULL);
-- ASSERT_PTR_NE(kr->rsa->n, NULL);
-- ASSERT_PTR_NE(kr->rsa->e, NULL);
-- ASSERT_PTR_NE(kr->rsa->p, NULL);
-- ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
-+ ASSERT_INT_EQ(RSA_bits(kr->rsa), 1024);
- TEST_DONE();
-
- TEST_START("generate KEY_DSA");
- ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
- ASSERT_PTR_NE(kd, NULL);
- ASSERT_PTR_NE(kd->dsa, NULL);
-- ASSERT_PTR_NE(kd->dsa->g, NULL);
-- ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
- TEST_DONE();
-
- #ifdef OPENSSL_HAS_ECC
-@@ -333,9 +315,6 @@ sshkey_tests(void)
- ASSERT_PTR_NE(kr, k1);
- ASSERT_INT_EQ(k1->type, KEY_RSA);
- ASSERT_PTR_NE(k1->rsa, NULL);
-- ASSERT_PTR_NE(k1->rsa->n, NULL);
-- ASSERT_PTR_NE(k1->rsa->e, NULL);
-- ASSERT_PTR_EQ(k1->rsa->p, NULL);
- TEST_DONE();
-
- TEST_START("equal KEY_RSA/demoted KEY_RSA");
-@@ -349,8 +328,6 @@ sshkey_tests(void)
- ASSERT_PTR_NE(kd, k1);
- ASSERT_INT_EQ(k1->type, KEY_DSA);
- ASSERT_PTR_NE(k1->dsa, NULL);
-- ASSERT_PTR_NE(k1->dsa->g, NULL);
-- ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
- TEST_DONE();
-
- TEST_START("equal KEY_DSA/demoted KEY_DSA");
-diff --git a/rsa.c b/rsa.c
-index 5ecacef..6ff9947 100644
---- a/rsa.c
-+++ b/rsa.c
-@@ -76,11 +76,14 @@ rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
- {
- u_char *inbuf = NULL, *outbuf = NULL;
- int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR;
-+ const BIGNUM *e, *n;
-
-- if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
-+ RSA_get0_key(key, &n, &e, NULL);
-+
-+ if (BN_num_bits(e) < 2 || !BN_is_odd(e))
- return SSH_ERR_INVALID_ARGUMENT;
-
-- olen = BN_num_bytes(key->n);
-+ olen = BN_num_bytes(n);
- if ((outbuf = malloc(olen)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
-@@ -122,8 +125,11 @@ rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
- {
- u_char *inbuf = NULL, *outbuf = NULL;
- int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR;
-+ const BIGNUM *n;
-+
-+ RSA_get0_key(key, &n, NULL, NULL);
-
-- olen = BN_num_bytes(key->n);
-+ olen = BN_num_bytes(n);
- if ((outbuf = malloc(olen)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
-@@ -157,31 +163,42 @@ rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
- return r;
- }
-
--/* calculate p-1 and q-1 */
-+/* calculate d mod p-1 and d mod q-1 */
- int
--rsa_generate_additional_parameters(RSA *rsa)
-+rsa_generate_additional_parameters(RSA *rsa, BIGNUM *iqmp)
- {
- BIGNUM *aux = NULL;
- BN_CTX *ctx = NULL;
- int r;
-+ const BIGNUM *p, *q, *d;
-+ BIGNUM *dmp1 = NULL, *dmq1 = NULL;
-+
-+ RSA_get0_factors(rsa, &p, &q);
-+ RSA_get0_key(rsa, NULL, NULL, &d);
-
-- if ((ctx = BN_CTX_new()) == NULL)
-- return SSH_ERR_ALLOC_FAIL;
-- if ((aux = BN_new()) == NULL) {
-+ if ((ctx = BN_CTX_new()) == NULL ||
-+ (aux = BN_new()) == NULL ||
-+ (dmp1 = BN_new()) == NULL ||
-+ (dmq1 = BN_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-
-- if ((BN_sub(aux, rsa->q, BN_value_one()) == 0) ||
-- (BN_mod(rsa->dmq1, rsa->d, aux, ctx) == 0) ||
-- (BN_sub(aux, rsa->p, BN_value_one()) == 0) ||
-- (BN_mod(rsa->dmp1, rsa->d, aux, ctx) == 0)) {
-+ if ((BN_sub(aux, q, BN_value_one()) == 0) ||
-+ (BN_mod(dmq1, d, aux, ctx) == 0) ||
-+ (BN_sub(aux, p, BN_value_one()) == 0) ||
-+ (BN_mod(dmp1, d, aux, ctx) == 0) ||
-+ (RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp) == 0)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
-+ dmp1 = NULL;
-+ dmq1 = NULL;
- r = 0;
- out:
- BN_clear_free(aux);
-+ BN_clear_free(dmp1);
-+ BN_clear_free(dmq1);
- BN_CTX_free(ctx);
- return r;
- }
-diff --git a/rsa.h b/rsa.h
-index c476707..4da6936 100644
---- a/rsa.h
-+++ b/rsa.h
-@@ -21,6 +21,6 @@
-
- int rsa_public_encrypt(BIGNUM *, BIGNUM *, RSA *);
- int rsa_private_decrypt(BIGNUM *, BIGNUM *, RSA *);
--int rsa_generate_additional_parameters(RSA *);
-+int rsa_generate_additional_parameters(RSA *, BIGNUM *);
-
- #endif /* RSA_H */
-diff --git a/ssh-dss.c b/ssh-dss.c
-index 7af59fa..e36751e 100644
---- a/ssh-dss.c
-+++ b/ssh-dss.c
-@@ -55,6 +55,7 @@ ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
- size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
- struct sshbuf *b = NULL;
- int ret = SSH_ERR_INVALID_ARGUMENT;
-+ const BIGNUM *r, *s;
-
- if (lenp != NULL)
- *lenp = 0;
-@@ -76,15 +77,16 @@ ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
- goto out;
- }
-
-- rlen = BN_num_bytes(sig->r);
-- slen = BN_num_bytes(sig->s);
-+ DSA_SIG_get0(sig, &r, &s);
-+ rlen = BN_num_bytes(r);
-+ slen = BN_num_bytes(s);
- if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
- ret = SSH_ERR_INTERNAL_ERROR;
- goto out;
- }
- explicit_bzero(sigblob, SIGBLOB_LEN);
-- BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
-- BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
-+ BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
-+ BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
-
- if (compat & SSH_BUG_SIGBLOB) {
- if (sigp != NULL) {
-@@ -137,6 +139,7 @@ ssh_dss_verify(const struct sshkey *key,
- int ret = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *b = NULL;
- char *ktype = NULL;
-+ BIGNUM *r = NULL, *s = NULL;
-
- if (key == NULL || key->dsa == NULL ||
- sshkey_type_plain(key->type) != KEY_DSA ||
-@@ -177,16 +180,19 @@ ssh_dss_verify(const struct sshkey *key,
-
- /* parse signature */
- if ((sig = DSA_SIG_new()) == NULL ||
-- (sig->r = BN_new()) == NULL ||
-- (sig->s = BN_new()) == NULL) {
-+ (r = BN_new()) == NULL ||
-+ (s = BN_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
-- (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
-+ if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
-+ (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL) ||
-+ (DSA_SIG_set0(sig, r, s) == 0)) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
-+ r = NULL;
-+ s = NULL;
-
- /* sha1 the data */
- if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
-@@ -207,8 +213,9 @@ ssh_dss_verify(const struct sshkey *key,
-
- out:
- explicit_bzero(digest, sizeof(digest));
-- if (sig != NULL)
-- DSA_SIG_free(sig);
-+ BN_free(r);
-+ BN_free(s);
-+ DSA_SIG_free(sig);
- sshbuf_free(b);
- free(ktype);
- if (sigblob != NULL) {
-diff --git a/ssh-ecdsa.c b/ssh-ecdsa.c
-index d7bf3c6..985b7e5 100644
---- a/ssh-ecdsa.c
-+++ b/ssh-ecdsa.c
-@@ -54,6 +54,7 @@ ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
- size_t len, dlen;
- struct sshbuf *b = NULL, *bb = NULL;
- int ret = SSH_ERR_INTERNAL_ERROR;
-+ const BIGNUM *r, *s;
-
- if (lenp != NULL)
- *lenp = 0;
-@@ -80,8 +81,9 @@ ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
-- (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
-+ ECDSA_SIG_get0(sig, &r, &s);
-+ if ((ret = sshbuf_put_bignum2(bb, r)) != 0 ||
-+ (ret = sshbuf_put_bignum2(bb, s)) != 0)
- goto out;
- if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
- (ret = sshbuf_put_stringb(b, bb)) != 0)
-@@ -119,6 +121,7 @@ ssh_ecdsa_verify(const struct sshkey *key,
- int ret = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *b = NULL, *sigbuf = NULL;
- char *ktype = NULL;
-+ BIGNUM *r = NULL, *s = NULL;
-
- if (key == NULL || key->ecdsa == NULL ||
- sshkey_type_plain(key->type) != KEY_ECDSA ||
-@@ -147,15 +150,23 @@ ssh_ecdsa_verify(const struct sshkey *key,
- }
-
- /* parse signature */
-- if ((sig = ECDSA_SIG_new()) == NULL) {
-+ if ((sig = ECDSA_SIG_new()) == NULL ||
-+ (r = BN_new()) == NULL ||
-+ (s = BN_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
-- sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
-+ if (sshbuf_get_bignum2(sigbuf, r) != 0 ||
-+ sshbuf_get_bignum2(sigbuf, s) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
-+ if (ECDSA_SIG_set0(sig, r, s) == 0) {
-+ ret = SSH_ERR_LIBCRYPTO_ERROR;
-+ goto out;
-+ }
-+ r = NULL;
-+ s = NULL;
- if (sshbuf_len(sigbuf) != 0) {
- ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
- goto out;
-@@ -180,8 +191,9 @@ ssh_ecdsa_verify(const struct sshkey *key,
- explicit_bzero(digest, sizeof(digest));
- sshbuf_free(sigbuf);
- sshbuf_free(b);
-- if (sig != NULL)
-- ECDSA_SIG_free(sig);
-+ BN_free(r);
-+ BN_free(s);
-+ ECDSA_SIG_free(sig);
- free(ktype);
- return ret;
- }
-diff --git a/ssh-keygen.c b/ssh-keygen.c
-index 2a7939b..95cfed8 100644
---- a/ssh-keygen.c
-+++ b/ssh-keygen.c
-@@ -480,40 +480,67 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
- free(type);
-
- switch (key->type) {
-- case KEY_DSA:
-- buffer_get_bignum_bits(b, key->dsa->p);
-- buffer_get_bignum_bits(b, key->dsa->g);
-- buffer_get_bignum_bits(b, key->dsa->q);
-- buffer_get_bignum_bits(b, key->dsa->pub_key);
-- buffer_get_bignum_bits(b, key->dsa->priv_key);
-+ case KEY_DSA: {
-+ BIGNUM *p = NULL, *g = NULL, *q = NULL, *pub_key = NULL, *priv_key = NULL;
-+
-+ if ((p = BN_new()) == NULL ||
-+ (g = BN_new()) == NULL ||
-+ (q = BN_new()) == NULL ||
-+ (pub_key = BN_new()) == NULL ||
-+ (priv_key = BN_new()) == NULL)
-+ fatal("BN_new() failed");
-+ buffer_get_bignum_bits(b, p);
-+ buffer_get_bignum_bits(b, g);
-+ buffer_get_bignum_bits(b, q);
-+ buffer_get_bignum_bits(b, pub_key);
-+ buffer_get_bignum_bits(b, priv_key);
-+ if (DSA_set0_pqg(key->dsa, p, q, g) == 0 ||
-+ DSA_set0_key(key->dsa, pub_key, priv_key) == 0) {
-+ fatal("failed to set DSA key");
-+ }
-+ }
- break;
-- case KEY_RSA:
-- if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
-- (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
-- (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
-- fatal("%s: buffer error: %s", __func__, ssh_err(r));
-- e = e1;
-- debug("e %lx", e);
-- if (e < 30) {
-- e <<= 8;
-- e += e2;
-+ case KEY_RSA: {
-+ BIGNUM *bn_e = NULL, *bn_d = NULL, *bn_n = NULL, *bn_iqmp = NULL, *bn_p = NULL, *bn_q = NULL;
-+
-+ if ((bn_e = BN_new()) == NULL ||
-+ (bn_d = BN_new()) == NULL ||
-+ (bn_n = BN_new()) == NULL ||
-+ (bn_iqmp = BN_new()) == NULL ||
-+ (bn_p = BN_new()) == NULL ||
-+ (bn_q = BN_new()) == NULL)
-+ fatal("BN_new() failed");
-+
-+ if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
-+ (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
-+ (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
-+ fatal("%s: buffer error: %s", __func__, ssh_err(r));
-+ e = e1;
- debug("e %lx", e);
-- e <<= 8;
-- e += e3;
-- debug("e %lx", e);
-- }
-- if (!BN_set_word(key->rsa->e, e)) {
-- sshbuf_free(b);
-- sshkey_free(key);
-- return NULL;
-+ if (e < 30) {
-+ e <<= 8;
-+ e += e2;
-+ debug("e %lx", e);
-+ e <<= 8;
-+ e += e3;
-+ debug("e %lx", e);
-+ }
-+ if (!BN_set_word(bn_e, e)) {
-+ sshbuf_free(b);
-+ sshkey_free(key);
-+ return NULL;
-+ }
-+ buffer_get_bignum_bits(b, bn_d);
-+ buffer_get_bignum_bits(b, bn_n);
-+ buffer_get_bignum_bits(b, bn_iqmp);
-+ buffer_get_bignum_bits(b, bn_q);
-+ buffer_get_bignum_bits(b, bn_p);
-+ if (RSA_set0_key(key->rsa, bn_n, bn_e, bn_d) == 0 ||
-+ RSA_set0_factors(key->rsa, bn_p, bn_q) == 0)
-+ fatal("Failed to set RSA parameters");
-+ if ((r = rsa_generate_additional_parameters(key->rsa, bn_iqmp)) != 0)
-+ fatal("generate RSA parameters failed: %s", ssh_err(r));
- }
-- buffer_get_bignum_bits(b, key->rsa->d);
-- buffer_get_bignum_bits(b, key->rsa->n);
-- buffer_get_bignum_bits(b, key->rsa->iqmp);
-- buffer_get_bignum_bits(b, key->rsa->q);
-- buffer_get_bignum_bits(b, key->rsa->p);
-- if ((r = rsa_generate_additional_parameters(key->rsa)) != 0)
-- fatal("generate RSA parameters failed: %s", ssh_err(r));
- break;
- }
- rlen = sshbuf_len(b);
-@@ -621,7 +648,7 @@ do_convert_from_pkcs8(struct sshkey **k, int *private)
- identity_file);
- }
- fclose(fp);
-- switch (EVP_PKEY_type(pubkey->type)) {
-+ switch (EVP_PKEY_base_id(pubkey)) {
- case EVP_PKEY_RSA:
- if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
- fatal("sshkey_new failed");
-@@ -645,7 +672,7 @@ do_convert_from_pkcs8(struct sshkey **k, int *private)
- #endif
- default:
- fatal("%s: unsupported pubkey type %d", __func__,
-- EVP_PKEY_type(pubkey->type));
-+ EVP_PKEY_base_id(pubkey));
- }
- EVP_PKEY_free(pubkey);
- return;
-diff --git a/ssh-pkcs11-client.c b/ssh-pkcs11-client.c
-index fac0167..b82deec 100644
---- a/ssh-pkcs11-client.c
-+++ b/ssh-pkcs11-client.c
-@@ -143,12 +143,14 @@ pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
- static int
- wrap_key(RSA *rsa)
- {
-- static RSA_METHOD helper_rsa;
-+ static RSA_METHOD *helper_rsa;
-
-- memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
-- helper_rsa.name = "ssh-pkcs11-helper";
-- helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
-- RSA_set_method(rsa, &helper_rsa);
-+ if (helper_rsa == NULL) {
-+ helper_rsa = RSA_meth_dup(RSA_get_default_method());
-+ RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
-+ RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
-+ }
-+ RSA_set_method(rsa, helper_rsa);
- return (0);
- }
-
-diff --git a/ssh-pkcs11.c b/ssh-pkcs11.c
-index d1f750d..18ce16e 100644
---- a/ssh-pkcs11.c
-+++ b/ssh-pkcs11.c
-@@ -67,7 +67,7 @@ struct pkcs11_key {
- struct pkcs11_provider *provider;
- CK_ULONG slotidx;
- int (*orig_finish)(RSA *rsa);
-- RSA_METHOD rsa_method;
-+ RSA_METHOD *rsa_method;
- char *keyid;
- int keyid_len;
- };
-@@ -326,13 +326,21 @@ pkcs11_rsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
- k11->keyid = xmalloc(k11->keyid_len);
- memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
- }
-- k11->orig_finish = def->finish;
-- memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
-- k11->rsa_method.name = "pkcs11";
-- k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
-- k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
-- k11->rsa_method.finish = pkcs11_rsa_finish;
-- RSA_set_method(rsa, &k11->rsa_method);
-+ k11->orig_finish = RSA_meth_get_finish(def);
-+ if ((k11->rsa_method = RSA_meth_dup(def)) == NULL ||
-+ RSA_meth_set1_name(k11->rsa_method, "pkcs11") == 0 ||
-+ RSA_meth_set_priv_enc(k11->rsa_method, pkcs11_rsa_private_encrypt) == 0 ||
-+ RSA_meth_set_priv_dec(k11->rsa_method, pkcs11_rsa_private_decrypt) == 0 ||
-+ RSA_meth_set_finish(k11->rsa_method, pkcs11_rsa_finish) == 0) {
-+ RSA_meth_free(k11->rsa_method);
-+ k11->rsa_method = NULL;
-+ pkcs11_provider_unref(k11->provider);
-+ free(k11->keyid);
-+ free(k11);
-+ return (-1);
-+ }
-+
-+ RSA_set_method(rsa, k11->rsa_method);
- RSA_set_app_data(rsa, k11);
- return (0);
- }
-@@ -460,6 +468,7 @@ pkcs11_fetch_keys_filter(struct pkcs11_provider *p, CK_ULONG slotidx,
- CK_ULONG nfound;
- CK_SESSION_HANDLE session;
- CK_FUNCTION_LIST *f;
-+ const BIGNUM *n, *e;
-
- f = p->function_list;
- session = p->slotinfo[slotidx].session;
-@@ -512,10 +521,14 @@ pkcs11_fetch_keys_filter(struct pkcs11_provider *p, CK_ULONG slotidx,
- if ((rsa = RSA_new()) == NULL) {
- error("RSA_new failed");
- } else {
-- rsa->n = BN_bin2bn(attribs[1].pValue,
-+ BIGNUM *rsa_n, *rsa_e;
-+
-+ rsa_n = BN_bin2bn(attribs[1].pValue,
- attribs[1].ulValueLen, NULL);
-- rsa->e = BN_bin2bn(attribs[2].pValue,
-+ rsa_e = BN_bin2bn(attribs[2].pValue,
- attribs[2].ulValueLen, NULL);
-+ if (RSA_set0_key(rsa, rsa_n, rsa_e, NULL) == 0)
-+ error("RSA_set0_key failed");
- }
- } else {
- cp = attribs[2].pValue;
-@@ -525,17 +538,18 @@ pkcs11_fetch_keys_filter(struct pkcs11_provider *p, CK_ULONG slotidx,
- == NULL) {
- error("d2i_X509 failed");
- } else if ((evp = X509_get_pubkey(x509)) == NULL ||
-- evp->type != EVP_PKEY_RSA ||
-- evp->pkey.rsa == NULL) {
-+ EVP_PKEY_id(evp) != EVP_PKEY_RSA ||
-+ EVP_PKEY_get0_RSA(evp) == NULL) {
- debug("X509_get_pubkey failed or no rsa");
-- } else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
-+ } else if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp)))
- == NULL) {
- error("RSAPublicKey_dup");
- }
- if (x509)
- X509_free(x509);
- }
-- if (rsa && rsa->n && rsa->e &&
-+ RSA_get0_key(rsa, &n, &e, NULL);
-+ if (rsa && n && e &&
- pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
- key = sshkey_new(KEY_UNSPEC);
- key->rsa = rsa;
-diff --git a/ssh-rsa.c b/ssh-rsa.c
-index cde05df..8197949 100644
---- a/ssh-rsa.c
-+++ b/ssh-rsa.c
-@@ -88,6 +88,7 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
- u_int dlen, len;
- int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *b = NULL;
-+ const BIGNUM *n;
-
- if (lenp != NULL)
- *lenp = 0;
-@@ -99,8 +100,10 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
- else
- hash_alg = rsa_hash_alg_from_ident(alg_ident);
- if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
-- sshkey_type_plain(key->type) != KEY_RSA ||
-- BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
-+ sshkey_type_plain(key->type) != KEY_RSA)
-+ return SSH_ERR_INVALID_ARGUMENT;
-+ RSA_get0_key(key->rsa, &n, NULL, NULL);
-+ if (BN_num_bits(n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
- return SSH_ERR_INVALID_ARGUMENT;
- slen = RSA_size(key->rsa);
- if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
-@@ -169,12 +172,15 @@ ssh_rsa_verify(const struct sshkey *key,
- size_t len, diff, modlen, dlen;
- struct sshbuf *b = NULL;
- u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
-+ const BIGNUM *n;
-
- if (key == NULL || key->rsa == NULL ||
- sshkey_type_plain(key->type) != KEY_RSA ||
-- BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE ||
- sig == NULL || siglen == 0)
- return SSH_ERR_INVALID_ARGUMENT;
-+ RSA_get0_key(key->rsa, &n, NULL, NULL);
-+ if (BN_num_bits(n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
-+ return SSH_ERR_INVALID_ARGUMENT;
-
- if ((b = sshbuf_from(sig, siglen)) == NULL)
- return SSH_ERR_ALLOC_FAIL;
-diff --git a/sshkey.c b/sshkey.c
-index e6df94a..6f06790 100644
---- a/sshkey.c
-+++ b/sshkey.c
-@@ -266,15 +266,18 @@ sshkey_names_valid2(const char *names, int allow_wildcard)
- u_int
- sshkey_size(const struct sshkey *k)
- {
-+ const BIGNUM *bn;
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA1:
- case KEY_RSA:
- case KEY_RSA_CERT:
-- return BN_num_bits(k->rsa->n);
-+ RSA_get0_key(k->rsa, &bn, NULL, NULL);
-+ return BN_num_bits(bn);
- case KEY_DSA:
- case KEY_DSA_CERT:
-- return BN_num_bits(k->dsa->p);
-+ DSA_get0_pqg(k->dsa, &bn, NULL, NULL);
-+ return BN_num_bits(bn);
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- return sshkey_curve_nid_to_bits(k->ecdsa_nid);
-@@ -474,11 +477,7 @@ sshkey_new(int type)
- case KEY_RSA1:
- case KEY_RSA:
- case KEY_RSA_CERT:
-- if ((rsa = RSA_new()) == NULL ||
-- (rsa->n = BN_new()) == NULL ||
-- (rsa->e = BN_new()) == NULL) {
-- if (rsa != NULL)
-- RSA_free(rsa);
-+ if ((rsa = RSA_new()) == NULL) {
- free(k);
- return NULL;
- }
-@@ -486,13 +485,7 @@ sshkey_new(int type)
- break;
- case KEY_DSA:
- case KEY_DSA_CERT:
-- if ((dsa = DSA_new()) == NULL ||
-- (dsa->p = BN_new()) == NULL ||
-- (dsa->q = BN_new()) == NULL ||
-- (dsa->g = BN_new()) == NULL ||
-- (dsa->pub_key = BN_new()) == NULL) {
-- if (dsa != NULL)
-- DSA_free(dsa);
-+ if ((dsa = DSA_new()) == NULL) {
- free(k);
- return NULL;
- }
-@@ -533,21 +526,10 @@ sshkey_add_private(struct sshkey *k)
- case KEY_RSA1:
- case KEY_RSA:
- case KEY_RSA_CERT:
--#define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
-- if (bn_maybe_alloc_failed(k->rsa->d) ||
-- bn_maybe_alloc_failed(k->rsa->iqmp) ||
-- bn_maybe_alloc_failed(k->rsa->q) ||
-- bn_maybe_alloc_failed(k->rsa->p) ||
-- bn_maybe_alloc_failed(k->rsa->dmq1) ||
-- bn_maybe_alloc_failed(k->rsa->dmp1))
-- return SSH_ERR_ALLOC_FAIL;
- break;
- case KEY_DSA:
- case KEY_DSA_CERT:
-- if (bn_maybe_alloc_failed(k->dsa->priv_key))
-- return SSH_ERR_ALLOC_FAIL;
- break;
--#undef bn_maybe_alloc_failed
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- /* Cannot do anything until we know the group */
-@@ -666,17 +648,31 @@ sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
- #ifdef WITH_OPENSSL
- case KEY_RSA1:
- case KEY_RSA_CERT:
-- case KEY_RSA:
-- return a->rsa != NULL && b->rsa != NULL &&
-- BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
-- BN_cmp(a->rsa->n, b->rsa->n) == 0;
-+ case KEY_RSA: {
-+ const BIGNUM *a_e, *a_n, *b_e, *b_n;
-+
-+ if (a->rsa == NULL || b->rsa == NULL)
-+ return 0;
-+ RSA_get0_key(a->rsa, &a_n, &a_e, NULL);
-+ RSA_get0_key(b->rsa, &b_n, &b_e, NULL);
-+ return BN_cmp(a_e, b_e) == 0 && BN_cmp(a_n, b_n) == 0;
-+ }
- case KEY_DSA_CERT:
-- case KEY_DSA:
-- return a->dsa != NULL && b->dsa != NULL &&
-- BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
-- BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
-- BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
-- BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
-+ case KEY_DSA: {
-+ const BIGNUM *a_p, *a_q, *a_g, *a_pub_key;
-+ const BIGNUM *b_p, *b_q, *b_g, *b_pub_key;
-+
-+ if (a->dsa == NULL || b->dsa == NULL)
-+ return 0;
-+ DSA_get0_pqg(a->dsa, &a_p, &a_q, &a_g);
-+ DSA_get0_key(a->dsa, &a_pub_key, NULL);
-+ DSA_get0_pqg(b->dsa, &b_p, &b_q, &b_g);
-+ DSA_get0_key(b->dsa, &b_pub_key, NULL);
-+ return BN_cmp(a_p, b_p) == 0 &&
-+ BN_cmp(a_q, b_q) == 0 &&
-+ BN_cmp(a_g, b_g) == 0 &&
-+ BN_cmp(a_pub_key, b_pub_key) == 0;
-+ }
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
- case KEY_ECDSA:
-@@ -751,15 +747,21 @@ to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain)
- return ret;
- break;
- #ifdef WITH_OPENSSL
-- case KEY_DSA:
-- if (key->dsa == NULL)
-- return SSH_ERR_INVALID_ARGUMENT;
-- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
-- (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
-- (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
-- (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
-- (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
-- return ret;
-+ case KEY_DSA: {
-+ const BIGNUM *p, *q, *g, *pub_key;
-+
-+ if (key->dsa == NULL)
-+ return SSH_ERR_INVALID_ARGUMENT;
-+
-+ DSA_get0_pqg(key->dsa, &p, &q, &g);
-+ DSA_get0_key(key->dsa, &pub_key, NULL);
-+ if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
-+ (ret = sshbuf_put_bignum2(b, p)) != 0 ||
-+ (ret = sshbuf_put_bignum2(b, q)) != 0 ||
-+ (ret = sshbuf_put_bignum2(b, g)) != 0 ||
-+ (ret = sshbuf_put_bignum2(b, pub_key)) != 0)
-+ return ret;
-+ }
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
-@@ -772,13 +774,18 @@ to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain)
- return ret;
- break;
- # endif
-- case KEY_RSA:
-- if (key->rsa == NULL)
-- return SSH_ERR_INVALID_ARGUMENT;
-- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
-- (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
-- (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
-- return ret;
-+ case KEY_RSA: {
-+ const BIGNUM *e, *n;
-+
-+ if (key->rsa == NULL)
-+ return SSH_ERR_INVALID_ARGUMENT;
-+
-+ RSA_get0_key(key->rsa, &n, &e, NULL);
-+ if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
-+ (ret = sshbuf_put_bignum2(b, e)) != 0 ||
-+ (ret = sshbuf_put_bignum2(b, n)) != 0)
-+ return ret;
-+ }
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
-@@ -884,8 +891,13 @@ sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
-
- if (k->type == KEY_RSA1) {
- #ifdef WITH_OPENSSL
-- int nlen = BN_num_bytes(k->rsa->n);
-- int elen = BN_num_bytes(k->rsa->e);
-+ const BIGNUM *n, *e;
-+ int nlen, elen;
-+
-+ RSA_get0_key(k->rsa, &n, &e, NULL);
-+
-+ nlen = BN_num_bytes(n);
-+ elen = BN_num_bytes(e);
-
- blob_len = nlen + elen;
- if (nlen >= INT_MAX - elen ||
-@@ -893,8 +905,8 @@ sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- BN_bn2bin(k->rsa->n, blob);
-- BN_bn2bin(k->rsa->e, blob + nlen);
-+ BN_bn2bin(n, blob);
-+ BN_bn2bin(e, blob + nlen);
- #endif /* WITH_OPENSSL */
- } else if ((r = to_blob(k, &blob, &blob_len, 1)) != 0)
- goto out;
-@@ -1760,15 +1772,32 @@ sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA:
-- case KEY_DSA_CERT:
-- if ((n = sshkey_new(k->type)) == NULL)
-- return SSH_ERR_ALLOC_FAIL;
-- if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
-- (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
-- (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
-- (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
-- sshkey_free(n);
-- return SSH_ERR_ALLOC_FAIL;
-+ case KEY_DSA_CERT: {
-+ const BIGNUM *k_p, *k_q, *k_g, *k_pub_key;
-+ BIGNUM *n_p = NULL, *n_q = NULL, *n_g = NULL, *n_pub_key = NULL;
-+
-+ if ((n = sshkey_new(k->type)) == NULL)
-+ return SSH_ERR_ALLOC_FAIL;
-+
-+ DSA_get0_pqg(k->dsa, &k_p, &k_q, &k_g);
-+ DSA_get0_key(k->dsa, &k_pub_key, NULL);
-+
-+ if (((n_p = BN_dup(k_p)) == NULL) ||
-+ ((n_q = BN_dup(k_q)) == NULL) ||
-+ ((n_g = BN_dup(k_g)) == NULL) ||
-+ (DSA_set0_pqg(n->dsa, n_p, n_q, n_g) == 0)) {
-+ sshkey_free(n);
-+ BN_free(n_p);
-+ BN_free(n_q);
-+ BN_free(n_g);
-+ return SSH_ERR_ALLOC_FAIL;
-+ }
-+ if (((n_pub_key = BN_dup(k_pub_key)) == NULL) ||
-+ (DSA_set0_key(n->dsa, n_pub_key, NULL) == 0)) {
-+ sshkey_free(n);
-+ BN_free(n_pub_key);
-+ return SSH_ERR_ALLOC_FAIL;
-+ }
- }
- break;
- # ifdef OPENSSL_HAS_ECC
-@@ -1791,13 +1820,22 @@ sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA:
- case KEY_RSA1:
-- case KEY_RSA_CERT:
-- if ((n = sshkey_new(k->type)) == NULL)
-- return SSH_ERR_ALLOC_FAIL;
-- if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
-- (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
-- sshkey_free(n);
-- return SSH_ERR_ALLOC_FAIL;
-+ case KEY_RSA_CERT: {
-+ const BIGNUM *k_n, *k_e;
-+ BIGNUM *n_n = NULL, *n_e = NULL;
-+
-+ if ((n = sshkey_new(k->type)) == NULL)
-+ return SSH_ERR_ALLOC_FAIL;
-+
-+ RSA_get0_key(k->rsa, &k_n, &k_e, NULL);
-+ if (((n_n = BN_dup(k_n)) == NULL) ||
-+ ((n_e = BN_dup(k_e)) == NULL) ||
-+ (RSA_set0_key(n->rsa, n_n, n_e, NULL) == 0)) {
-+ sshkey_free(n);
-+ BN_free(n_n);
-+ BN_free(n_e);
-+ return SSH_ERR_ALLOC_FAIL;
-+ }
- }
- break;
- #endif /* WITH_OPENSSL */
-@@ -1995,10 +2033,20 @@ sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
-- sshbuf_get_bignum2(b, key->rsa->n) != 0) {
-- ret = SSH_ERR_INVALID_FORMAT;
-- goto out;
-+ {
-+ BIGNUM *e, *n;
-+
-+ e = BN_new();
-+ n = BN_new();
-+ if (e == NULL || n == NULL ||
-+ sshbuf_get_bignum2(b, e) != 0 ||
-+ sshbuf_get_bignum2(b, n) != 0 ||
-+ RSA_set0_key(key->rsa, n, e, NULL) == 0) {
-+ BN_free(e);
-+ BN_free(n);
-+ ret = SSH_ERR_ALLOC_FAIL;
-+ goto out;
-+ }
- }
- #ifdef DEBUG_PK
- RSA_print_fp(stderr, key->rsa, 8);
-@@ -2016,12 +2064,34 @@ sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
-- sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
-- sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
-- sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
-- ret = SSH_ERR_INVALID_FORMAT;
-- goto out;
-+ {
-+ BIGNUM *p, *q, *g, *pub_key;
-+
-+ p = BN_new();
-+ q = BN_new();
-+ g = BN_new();
-+ pub_key = BN_new();
-+
-+ if (p == NULL || q == NULL || g == NULL ||
-+ pub_key == NULL ||
-+ sshbuf_get_bignum2(b, p) != 0 ||
-+ sshbuf_get_bignum2(b, q) != 0 ||
-+ sshbuf_get_bignum2(b, g) != 0 ||
-+ sshbuf_get_bignum2(b, pub_key) != 0 ||
-+ DSA_set0_pqg(key->dsa, p, q, g) == 0) {
-+ BN_free(p);
-+ BN_free(q);
-+ BN_free(g);
-+ BN_free(pub_key);
-+ ret = SSH_ERR_ALLOC_FAIL;
-+ goto out;
-+ }
-+
-+ if (DSA_set0_key(key->dsa, pub_key, NULL) == 0) {
-+ BN_free(pub_key);
-+ ret = SSH_ERR_LIBCRYPTO_ERROR;
-+ goto out;
-+ }
- }
- #ifdef DEBUG_PK
- DSA_print_fp(stderr, key->dsa, 8);
-@@ -2261,26 +2331,53 @@ sshkey_demote(const struct sshkey *k, struct sshkey **dkp)
- goto fail;
- /* FALLTHROUGH */
- case KEY_RSA1:
-- case KEY_RSA:
-- if ((pk->rsa = RSA_new()) == NULL ||
-- (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
-- (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
-- ret = SSH_ERR_ALLOC_FAIL;
-- goto fail;
-+ case KEY_RSA: {
-+ const BIGNUM *k_e, *k_n;
-+ BIGNUM *pk_e = NULL, *pk_n = NULL;
-+
-+ RSA_get0_key(k->rsa, &k_n, &k_e, NULL);
-+ if ((pk->rsa = RSA_new()) == NULL ||
-+ (pk_e = BN_dup(k_e)) == NULL ||
-+ (pk_n = BN_dup(k_n)) == NULL ||
-+ RSA_set0_key(pk->rsa, pk_n, pk_e, NULL) == 0) {
-+ BN_free(pk_e);
-+ BN_free(pk_n);
-+ ret = SSH_ERR_ALLOC_FAIL;
-+ goto fail;
- }
-+ }
- break;
- case KEY_DSA_CERT:
- if ((ret = sshkey_cert_copy(k, pk)) != 0)
- goto fail;
- /* FALLTHROUGH */
-- case KEY_DSA:
-- if ((pk->dsa = DSA_new()) == NULL ||
-- (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
-- (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
-- (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
-- (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
-- ret = SSH_ERR_ALLOC_FAIL;
-- goto fail;
-+ case KEY_DSA: {
-+ const BIGNUM *k_p, *k_q, *k_g, *k_pub_key;
-+ BIGNUM *pk_p = NULL, *pk_q = NULL, *pk_g = NULL;
-+ BIGNUM *pk_pub_key = NULL;
-+
-+ DSA_get0_pqg(k->dsa, &k_p, &k_q, &k_g);
-+ DSA_get0_key(k->dsa, &k_pub_key, NULL);
-+
-+ if ((pk->dsa = DSA_new()) == NULL ||
-+ (pk_p = BN_dup(k_p)) == NULL ||
-+ (pk_q = BN_dup(k_q)) == NULL ||
-+ (pk_g = BN_dup(k_g)) == NULL ||
-+ (pk_pub_key = BN_dup(k_pub_key)) == NULL ||
-+ DSA_set0_pqg(pk->dsa, pk_p, pk_q, pk_g) == 0) {
-+ BN_free(pk_p);
-+ BN_free(pk_q);
-+ BN_free(pk_g);
-+ BN_free(pk_pub_key);
-+ ret = SSH_ERR_ALLOC_FAIL;
-+ goto fail;
-+ }
-+
-+ if (DSA_set0_key(pk->dsa, pk_pub_key, NULL) == 0) {
-+ BN_free(pk_pub_key);
-+ ret = SSH_ERR_LIBCRYPTO_ERROR;
-+ goto fail;
-+ }
- }
- break;
- case KEY_ECDSA_CERT:
-@@ -2401,12 +2498,34 @@ sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg)
- /* XXX this substantially duplicates to_blob(); refactor */
- switch (k->type) {
- #ifdef WITH_OPENSSL
-- case KEY_DSA_CERT:
-- if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
-- (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
-- (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
-- (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
-- goto out;
-+ case KEY_DSA_CERT: {
-+ BIGNUM *p, *q, *g, *pub_key;
-+
-+ p = BN_new();
-+ q = BN_new();
-+ g = BN_new();
-+ pub_key = BN_new();
-+
-+ if (p == NULL || q == NULL || g == NULL ||
-+ pub_key == NULL ||
-+ (ret = sshbuf_put_bignum2(cert, p)) != 0 ||
-+ (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
-+ (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
-+ (ret = sshbuf_put_bignum2(cert, pub_key)) != 0 ||
-+ (ret = ((DSA_set0_pqg(k->dsa, p, q, g) == 0)
-+ ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-+ BN_free(p);
-+ BN_free(q);
-+ BN_free(g);
-+ BN_free(pub_key);
-+ goto out;
-+ }
-+ if (DSA_set0_key(k->dsa, pub_key, NULL) == 0) {
-+ ret = SSH_ERR_LIBCRYPTO_ERROR;
-+ BN_free(pub_key);
-+ goto out;
-+ }
-+ }
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
-@@ -2418,10 +2537,21 @@ sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg)
- goto out;
- break;
- # endif /* OPENSSL_HAS_ECC */
-- case KEY_RSA_CERT:
-- if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
-- (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
-- goto out;
-+ case KEY_RSA_CERT: {
-+ BIGNUM *e, *n;
-+
-+ e = BN_new();
-+ n = BN_new();
-+ if (e == NULL || n == NULL ||
-+ (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
-+ (ret = sshbuf_put_bignum2(cert, n)) != 0 ||
-+ (ret = ((RSA_set0_key(k->rsa, n, e, NULL) == 0)
-+ ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-+ BN_free(e);
-+ BN_free(n);
-+ goto out;
-+ }
-+ }
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_CERT:
-@@ -2578,43 +2708,65 @@ sshkey_private_serialize(const struct sshkey *key, struct sshbuf *b)
- goto out;
- switch (key->type) {
- #ifdef WITH_OPENSSL
-- case KEY_RSA:
-- if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
-- goto out;
-+ case KEY_RSA: {
-+ const BIGNUM *n, *e, *d, *iqmp, *p, *q;
-+ RSA_get0_key(key->rsa, &n, &e, &d);
-+ RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
-+ RSA_get0_factors(key->rsa, &p, &q);
-+ if ((r = sshbuf_put_bignum2(b, n)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, e)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, d)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, p)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, q)) != 0)
-+ goto out;
-+ }
- break;
- case KEY_RSA_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
-- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
-- goto out;
-+ {
-+ const BIGNUM *d, *iqmp, *p, *q;
-+
-+ RSA_get0_key(key->rsa, NULL, NULL, &d);
-+ RSA_get0_factors(key->rsa, &p, &q);
-+ RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
-+ if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, d)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, p)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, q)) != 0)
-+ goto out;
-+ }
- break;
-- case KEY_DSA:
-- if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
-- goto out;
-+ case KEY_DSA: {
-+ const BIGNUM *p, *q, *g, *pub_key, *priv_key;
-+
-+ DSA_get0_pqg(key->dsa, &p, &q, &g);
-+ DSA_get0_key(key->dsa, &pub_key, &priv_key);
-+ if ((r = sshbuf_put_bignum2(b, p)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, q)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, g)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, pub_key)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, priv_key)) != 0)
-+ goto out;
-+ }
- break;
- case KEY_DSA_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
-- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
-- (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
-- goto out;
-+ {
-+ const BIGNUM *priv_key;
-+
-+ DSA_get0_key(key->dsa, NULL, &priv_key);
-+ if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
-+ (r = sshbuf_put_bignum2(b, priv_key)) != 0)
-+ goto out;
-+ }
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
-@@ -2690,18 +2842,51 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
-- goto out;
-+ {
-+ BIGNUM *p, *q, *g, *pub_key, *priv_key;
-+
-+ p = BN_new();
-+ q = BN_new();
-+ g = BN_new();
-+ pub_key = BN_new();
-+ priv_key = BN_new();
-+ if (p == NULL || q == NULL || g == NULL ||
-+ pub_key == NULL || priv_key == NULL ||
-+ (r = sshbuf_get_bignum2(buf, p)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, q)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, g)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, pub_key)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, priv_key)) != 0 ||
-+ (r = ((DSA_set0_pqg(k->dsa, p, q, g) == 0)
-+ ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-+ BN_free(p);
-+ BN_free(q);
-+ BN_free(g);
-+ BN_free(pub_key);
-+ BN_free(priv_key);
-+ goto out;
-+ }
-+ if (DSA_set0_key(k->dsa, pub_key, priv_key) == 0) {
-+ r = SSH_ERR_LIBCRYPTO_ERROR;
-+ BN_free(pub_key);
-+ BN_free(priv_key);
-+ goto out;
-+ }
-+ }
- break;
-- case KEY_DSA_CERT:
-- if ((r = sshkey_froms(buf, &k)) != 0 ||
-- (r = sshkey_add_private(k)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
-- goto out;
-+ case KEY_DSA_CERT: {
-+ BIGNUM *priv_key = BN_new();
-+
-+ if (priv_key == NULL ||
-+ (r = sshkey_froms(buf, &k)) != 0 ||
-+ (r = sshkey_add_private(k)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, priv_key)) != 0 ||
-+ (r = ((DSA_set0_key(k->dsa, NULL, priv_key) == 0)
-+ ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-+ BN_free(priv_key);
-+ goto out;
-+ }
-+ }
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
-@@ -2760,16 +2945,49 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
-- if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
-- (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
-- (r = rsa_generate_additional_parameters(k->rsa)) != 0)
-- goto out;
-+ {
-+ BIGNUM *n, *e, *d, *iqmp, *p, *q;
-+
-+ n = BN_new();
-+ e = BN_new();
-+ d = BN_new();
-+ iqmp = BN_new();
-+ p = BN_new();
-+ q = BN_new();
-+
-+ if (n == NULL || e == NULL || d == NULL ||
-+ iqmp == NULL || p == NULL || q == NULL ||
-+ (r = sshbuf_get_bignum2(buf, n)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, e)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, d)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, p)) != 0 ||
-+ (r = sshbuf_get_bignum2(buf, q)) != 0 ||
-+ (r = ((RSA_set0_key(k->rsa, n, e, d) == 0)
-+ ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-+ BN_free(n);
-+ BN_free(e);
-+ BN_free(d);
-+ BN_free(iqmp);
-+ BN_free(p);
-+ BN_free(q);
-+ goto out;
-+ }
-+ if (RSA_set0_factors(k->rsa, p, q) == 0) {
-+ r = SSH_ERR_LIBCRYPTO_ERROR;
-+ BN_free(iqmp);
-+ BN_free(p);
-+ BN_free(q);
-+ goto out;
-+ }
-+ if ((r = rsa_generate_additional_parameters(k->rsa, iqmp)) != 0) {
-+ BN_free(iqmp);
-+ goto out;
-+ }
-+ }
- break;
- case KEY_RSA_CERT:
-+#if 0
- if ((r = sshkey_froms(buf, &k)) != 0 ||
- (r = sshkey_add_private(k)) != 0 ||
- (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
-@@ -2778,6 +2996,10 @@ sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
- (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
- (r = rsa_generate_additional_parameters(k->rsa)) != 0)
- goto out;
-+#endif
-+ /* XXX: public key is missing */
-+ r = SSH_ERR_INTERNAL_ERROR;
-+ goto out;
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
-@@ -3779,7 +4001,7 @@ sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
- r = SSH_ERR_KEY_WRONG_PASSPHRASE;
- goto out;
- }
-- if (pk->type == EVP_PKEY_RSA &&
-+ if (EVP_PKEY_id(pk) == EVP_PKEY_RSA &&
- (type == KEY_UNSPEC || type == KEY_RSA)) {
- if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
-@@ -3794,7 +4016,7 @@ sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
-- } else if (pk->type == EVP_PKEY_DSA &&
-+ } else if (EVP_PKEY_id(pk) == EVP_PKEY_DSA &&
- (type == KEY_UNSPEC || type == KEY_DSA)) {
- if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
-@@ -3806,7 +4028,7 @@ sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
- DSA_print_fp(stderr, prv->dsa, 8);
- #endif
- #ifdef OPENSSL_HAS_ECC
-- } else if (pk->type == EVP_PKEY_EC &&
-+ } else if (EVP_PKEY_id(pk) == EVP_PKEY_EC &&
- (type == KEY_UNSPEC || type == KEY_ECDSA)) {
- if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
-
-From 34938abb73f260cf58b9e5c3120098f9e1450aec Mon Sep 17 00:00:00 2001
-From: Kurt Roeckx <kurt@roeckx.be>
-Date: Thu, 27 Oct 2016 16:51:31 +0200
-Subject: [PATCH 2/3] Fix sshkey_certify(), it was the wrong way around.
-
----
- sshkey.c | 41 +++++++++--------------------------------
- 1 file changed, 9 insertions(+), 32 deletions(-)
-
-diff --git a/sshkey.c b/sshkey.c
-index 6f06790..4519e01 100644
---- a/sshkey.c
-+++ b/sshkey.c
-@@ -2499,32 +2499,16 @@ sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg)
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA_CERT: {
-- BIGNUM *p, *q, *g, *pub_key;
-+ const BIGNUM *p, *q, *g, *pub_key;
-
-- p = BN_new();
-- q = BN_new();
-- g = BN_new();
-- pub_key = BN_new();
-+ DSA_get0_pqg(k->dsa, &p, &q, &g);
-+ DSA_get0_key(k->dsa, &pub_key, NULL);
-
-- if (p == NULL || q == NULL || g == NULL ||
-- pub_key == NULL ||
-- (ret = sshbuf_put_bignum2(cert, p)) != 0 ||
-+ if ((ret = sshbuf_put_bignum2(cert, p)) != 0 ||
- (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
- (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
-- (ret = sshbuf_put_bignum2(cert, pub_key)) != 0 ||
-- (ret = ((DSA_set0_pqg(k->dsa, p, q, g) == 0)
-- ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-- BN_free(p);
-- BN_free(q);
-- BN_free(g);
-- BN_free(pub_key);
-+ (ret = sshbuf_put_bignum2(cert, pub_key)) != 0)
- goto out;
-- }
-- if (DSA_set0_key(k->dsa, pub_key, NULL) == 0) {
-- ret = SSH_ERR_LIBCRYPTO_ERROR;
-- BN_free(pub_key);
-- goto out;
-- }
- }
- break;
- # ifdef OPENSSL_HAS_ECC
-@@ -2538,19 +2522,12 @@ sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg)
- break;
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA_CERT: {
-- BIGNUM *e, *n;
-+ const BIGNUM *e, *n;
-
-- e = BN_new();
-- n = BN_new();
-- if (e == NULL || n == NULL ||
-- (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
-- (ret = sshbuf_put_bignum2(cert, n)) != 0 ||
-- (ret = ((RSA_set0_key(k->rsa, n, e, NULL) == 0)
-- ? SSH_ERR_LIBCRYPTO_ERROR : 0)) != 0) {
-- BN_free(e);
-- BN_free(n);
-+ RSA_get0_key(k->rsa, &n, &e, NULL);
-+ if ((ret = sshbuf_put_bignum2(cert, e)) != 0 ||
-+ (ret = sshbuf_put_bignum2(cert, n)) != 0)
- goto out;
-- }
- }
- break;
- #endif /* WITH_OPENSSL */
-
-From 34b85955cc0fdb197d86c6bc4cb9168f8e87b527 Mon Sep 17 00:00:00 2001
-From: Kurt Roeckx <kurt@roeckx.be>
-Date: Thu, 27 Oct 2016 17:03:12 +0200
-Subject: [PATCH 3/3] Simplify compat layer.
-
----
- libcrypto-compat.c | 37 +------------------------------------
- 1 file changed, 1 insertion(+), 36 deletions(-)
-
-diff --git a/libcrypto-compat.c b/libcrypto-compat.c
-index d92c8d9..fafccd0 100644
---- a/libcrypto-compat.c
-+++ b/libcrypto-compat.c
-@@ -322,44 +322,9 @@ EVP_MD_CTX *EVP_MD_CTX_new(void)
- return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
- }
-
--static void OPENSSL_clear_free(void *str, size_t num)
--{
-- if (str == NULL)
-- return;
-- if (num)
-- OPENSSL_cleanse(str, num);
-- OPENSSL_free(str);
--}
--
--/* This call frees resources associated with the context */
--static int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
--{
-- if (ctx == NULL)
-- return 1;
--
-- /*
-- * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
-- * sometimes only copies of the context are ever finalised.
-- */
-- if (ctx->digest && ctx->digest->cleanup
-- && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
-- ctx->digest->cleanup(ctx);
-- if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
-- && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
-- OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
-- }
-- EVP_PKEY_CTX_free(ctx->pctx);
--#ifndef OPENSSL_NO_ENGINE
-- ENGINE_finish(ctx->engine);
--#endif
-- OPENSSL_cleanse(ctx, sizeof(*ctx));
--
-- return 1;
--}
--
- void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
- {
-- EVP_MD_CTX_reset(ctx);
-+ EVP_MD_CTX_cleanup(ctx);
- OPENSSL_free(ctx);
- }
-
diff --git a/openssh-01-fix-pam-uclibc-pthreads-clash.patch b/openssh-01-fix-pam-uclibc-pthreads-clash.patch
deleted file mode 100644
index d9bc6e5..0000000
--- a/openssh-01-fix-pam-uclibc-pthreads-clash.patch
+++ /dev/null
@@ -1,44 +0,0 @@
-When PAM is enabled, openssh makes its own static versions of pthreads
-functions. But when built with a uclibc toolchain, pthreads.h gets
-indirectly included. The clashing exported and static definitions of
-the pthreads functions then cause a compile error. This patch fixes
-the problem by changing the static pthread function names with macros
-when the static functions are defined.
-
-Signed-off-by: Danomi Manchego <danomimanchego123@gmail.com>
-
-diff -urN openssh-6.1p1.orig/auth-pam.c openssh-6.1p1/auth-pam.c
---- openssh-6.1p1.orig/auth-pam.c 2009-07-12 08:07:21.000000000 -0400
-+++ openssh-6.1p1/auth-pam.c 2012-09-15 19:49:47.677288199 -0400
-@@ -166,6 +166,7 @@
- sigdie("PAM: authentication thread exited uncleanly");
- }
-
-+#define pthread_exit pthread_exit_AVOID_UCLIBC_PTHREAD_CLASH
- /* ARGSUSED */
- static void
- pthread_exit(void *value)
-@@ -173,6 +174,7 @@
- _exit(0);
- }
-
-+#define pthread_create pthread_create_AVOID_UCLIBC_PTHREAD_CLASH
- /* ARGSUSED */
- static int
- pthread_create(sp_pthread_t *thread, const void *attr,
-@@ -200,6 +202,7 @@
- }
- }
-
-+#define pthread_cancel pthread_cancel_AVOID_UCLIBC_PTHREAD_CLASH
- static int
- pthread_cancel(sp_pthread_t thread)
- {
-@@ -207,6 +210,7 @@
- return (kill(thread, SIGTERM));
- }
-
-+#define pthread_join pthread_join_AVOID_UCLIBC_PTHREAD_CLASH
- /* ARGSUSED */
- static int
- pthread_join(sp_pthread_t thread, void **value)
diff --git a/openssh-7.4p1.tar.gz.asc b/openssh-7.4p1.tar.gz.asc
deleted file mode 100644
index cf0a02d..0000000
--- a/openssh-7.4p1.tar.gz.asc
+++ /dev/null
@@ -1,13 +0,0 @@
------BEGIN PGP SIGNATURE-----
-
-iQGsBAABCgAGBQJYV25gAAoJENPl9Wttkg0wtXoMfjsJmSHFi5YYX6QpvNt+HxN6
-8yqoBUo0+oy8og/7pdhevL3wKAxBy45xiQz3H1kVKEPVcMvygJXUjhHrx12jgSE8
-Er+jQtxXCtqoh8OQRqZXDLj7iFkFKcpf0W9PsJikKDnt/jA4aKu3Tfxc5vy32jrp
-f7eQOjC/HiWE5gdM6XI+HgMJleHOkrQaniOmkbEk5mi+vc68suXBTql7eo5gPp2g
-QPISlWIbxZXJ16Ht+MUA6nSnkNHdPR96rn6pO8SCqErcn2uPJZYvE2FXA14UE12n
-fp7pqBj4HdNAFCeCS8QaQM8ACTQrueLQdDb+qVr0vgadsqn2AYiBb6w1uGo89pqI
-vnkBgW3H3im358vgTtMa6RyFJUOYxc8LCUiO+G+t0nD+G8wlVssnz1DHUTYK89lU
-eznMBUZdJt1ssqnzaX8bOozRQA5sGtOnAD/V2akzn7PmKwgjOERJdh+9LjM+SQcp
-NJUEQYcarxgmbJ6sW8LCjTHbNoON3ezhiMB2Fxa1JUIsB4x/mIDLy6iGmuaAAKU=
-=PV2g
------END PGP SIGNATURE-----
diff --git a/openssh-7.7p1-openssl-1.1.0-1.patch b/openssh-7.7p1-openssl-1.1.0-1.patch
new file mode 100644
index 0000000..a181fe0
--- /dev/null
+++ b/openssh-7.7p1-openssl-1.1.0-1.patch
@@ -0,0 +1,1971 @@
+Submitted by: Bruce Dubbs (bdubbs@linuxfromscratch.org)
+Date: 2018-04-07
+Initial Package Version: 7.7p1
+Upstream Status: Pending (Still)
+Origin: https://git.archlinux.org/svntogit/packages.git/plain/trunk/openssl-1.1.0.patch?h=packages/openssh
+Description: Fixes build issues with OpenSSL-1.1.0.
+
+diff -aurp old/auth-pam.c new/auth-pam.c
+--- old/auth-pam.c 2018-03-22 16:21:14.000000000 -1000
++++ new/auth-pam.c 2018-03-23 10:05:03.886621278 -1000
+@@ -128,6 +128,10 @@ extern u_int utmp_len;
+ typedef pthread_t sp_pthread_t;
+ #else
+ typedef pid_t sp_pthread_t;
++# define pthread_create(a, b, c, d) _ssh_compat_pthread_create(a, b, c, d)
++# define pthread_exit(a) _ssh_compat_pthread_exit(a)
++# define pthread_cancel(a) _ssh_compat_pthread_cancel(a)
++# define pthread_join(a, b) _ssh_compat_pthread_join(a, b)
+ #endif
+
+ struct pam_ctxt {
+diff -aurp old/cipher.c new/cipher.c
+--- old/cipher.c 2018-03-22 16:21:14.000000000 -1000
++++ new/cipher.c 2018-03-23 10:05:03.886621278 -1000
+@@ -297,7 +297,10 @@ cipher_init(struct sshcipher_ctx **ccp,
+ goto out;
+ }
+ }
+- if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
++ /* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups;
++ use EVP_CipherInit_ex for augmenting */
++ if (EVP_CipherInit_ex(cc->evp, NULL, NULL, (u_char *)key, NULL, -1) == 0)
++ {
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
+ goto out;
+ }
+@@ -483,7 +486,7 @@ cipher_get_keyiv(struct sshcipher_ctx *c
+ len, iv))
+ return SSH_ERR_LIBCRYPTO_ERROR;
+ } else
+- memcpy(iv, cc->evp->iv, len);
++ memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
+ #endif
+ return 0;
+ }
+@@ -517,14 +520,19 @@ cipher_set_keyiv(struct sshcipher_ctx *c
+ EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
+ return SSH_ERR_LIBCRYPTO_ERROR;
+ } else
+- memcpy(cc->evp->iv, iv, evplen);
++ memcpy(EVP_CIPHER_CTX_iv(cc->evp), iv, evplen);
+ #endif
+ return 0;
+ }
+
+ #ifdef WITH_OPENSSL
+-#define EVP_X_STATE(evp) (evp)->cipher_data
+-#define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size
++# if OPENSSL_VERSION_NUMBER >= 0x10100000UL
++#define EVP_X_STATE(evp) EVP_CIPHER_CTX_get_cipher_data(evp)
++#define EVP_X_STATE_LEN(evp) EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
++# else
++#define EVP_X_STATE(evp) (evp).cipher_data
++#define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
++# endif
+ #endif
+
+ int
+diff -aurp old/cipher.h new/cipher.h
+--- old/cipher.h 2018-03-22 16:21:14.000000000 -1000
++++ new/cipher.h 2018-03-23 10:05:03.886621278 -1000
+@@ -46,7 +46,18 @@
+ #define CIPHER_DECRYPT 0
+
+ struct sshcipher;
++#if 0
++struct sshcipher_ctx {
++ int plaintext;
++ int encrypt;
++ EVP_CIPHER_CTX *evp;
++ struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
++ struct aesctr_ctx ac_ctx; /* XXX union with evp? */
++ const struct sshcipher *cipher;
++};
++#else
+ struct sshcipher_ctx;
++#endif
+
+ const struct sshcipher *cipher_by_name(const char *);
+ const char *cipher_warning_message(const struct sshcipher_ctx *);
+diff -aurp old/configure new/configure
+--- old/configure 2018-03-23 03:30:17.000000000 -1000
++++ new/configure 2018-03-23 10:05:03.888621444 -1000
+@@ -13076,7 +13076,6 @@ if ac_fn_c_try_run "$LINENO"; then :
+ 100*) ;; # 1.0.x
+ 200*) ;; # LibreSSL
+ *)
+- as_fn_error $? "OpenSSL >= 1.1.0 is not yet supported (have \"$ssl_library_ver\")" "$LINENO" 5
+ ;;
+ esac
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ssl_library_ver" >&5
+diff -aurp old/dh.c new/dh.c
+--- old/dh.c 2018-03-22 16:21:14.000000000 -1000
++++ new/dh.c 2018-03-23 10:05:03.888621444 -1000
+@@ -211,14 +211,15 @@ choose_dh(int min, int wantbits, int max
+ /* diffie-hellman-groupN-sha1 */
+
+ int
+-dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
++dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
+ {
+ int i;
+ int n = BN_num_bits(dh_pub);
+ int bits_set = 0;
+ BIGNUM *tmp;
++ const BIGNUM *p;
+
+- if (dh_pub->neg) {
++ if (BN_is_negative(dh_pub)) {
+ logit("invalid public DH value: negative");
+ return 0;
+ }
+@@ -231,7 +232,8 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
+ error("%s: BN_new failed", __func__);
+ return 0;
+ }
+- if (!BN_sub(tmp, dh->p, BN_value_one()) ||
++ DH_get0_pqg(dh, &p, NULL, NULL);
++ if (!BN_sub(tmp, p, BN_value_one()) ||
+ BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */
+ BN_clear_free(tmp);
+ logit("invalid public DH value: >= p-1");
+@@ -242,14 +244,14 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
+ for (i = 0; i <= n; i++)
+ if (BN_is_bit_set(dh_pub, i))
+ bits_set++;
+- debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
++ debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
+
+ /*
+ * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
+ */
+ if (bits_set < 4) {
+ logit("invalid public DH value (%d/%d)",
+- bits_set, BN_num_bits(dh->p));
++ bits_set, BN_num_bits(p));
+ return 0;
+ }
+ return 1;
+@@ -259,9 +261,13 @@ int
+ dh_gen_key(DH *dh, int need)
+ {
+ int pbits;
++ const BIGNUM *p, *pub_key;
++ BIGNUM *priv_key;
+
+- if (need < 0 || dh->p == NULL ||
+- (pbits = BN_num_bits(dh->p)) <= 0 ||
++ DH_get0_pqg(dh, &p, NULL, NULL);
++
++ if (need < 0 || p == NULL ||
++ (pbits = BN_num_bits(p)) <= 0 ||
+ need > INT_MAX / 2 || 2 * need > pbits)
+ return SSH_ERR_INVALID_ARGUMENT;
+ if (need < 256)
+@@ -270,10 +276,13 @@ dh_gen_key(DH *dh, int need)
+ * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
+ * so double requested need here.
+ */
+- dh->length = MINIMUM(need * 2, pbits - 1);
+- if (DH_generate_key(dh) == 0 ||
+- !dh_pub_is_valid(dh, dh->pub_key)) {
+- BN_clear_free(dh->priv_key);
++ DH_set_length(dh, MIN(need * 2, pbits - 1));
++ if (DH_generate_key(dh) == 0) {
++ return SSH_ERR_LIBCRYPTO_ERROR;
++ }
++ DH_get0_key(dh, &pub_key, &priv_key);
++ if (!dh_pub_is_valid(dh, pub_key)) {
++ BN_clear(priv_key);
+ return SSH_ERR_LIBCRYPTO_ERROR;
+ }
+ return 0;
+@@ -282,16 +291,27 @@ dh_gen_key(DH *dh, int need)
+ DH *
+ dh_new_group_asc(const char *gen, const char *modulus)
+ {
+- DH *dh;
++ DH *dh = NULL;
++ BIGNUM *p=NULL, *g=NULL;
+
+- if ((dh = DH_new()) == NULL)
+- return NULL;
+- if (BN_hex2bn(&dh->p, modulus) == 0 ||
+- BN_hex2bn(&dh->g, gen) == 0) {
+- DH_free(dh);
+- return NULL;
++ if ((dh = DH_new()) == NULL ||
++ (p = BN_new()) == NULL ||
++ (g = BN_new()) == NULL)
++ goto null;
++ if (BN_hex2bn(&p, modulus) == 0 ||
++ BN_hex2bn(&g, gen) == 0) {
++ goto null;
+ }
++ if (DH_set0_pqg(dh, p, NULL, g) == 0) {
++ goto null;
++ }
++ p = g = NULL;
+ return (dh);
++null:
++ BN_free(p);
++ BN_free(g);
++ DH_free(dh);
++ return NULL;
+ }
+
+ /*
+@@ -306,8 +326,8 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulu
+
+ if ((dh = DH_new()) == NULL)
+ return NULL;
+- dh->p = modulus;
+- dh->g = gen;
++ if (DH_set0_pqg(dh, modulus, NULL, gen) == 0)
++ return NULL;
+
+ return (dh);
+ }
+diff -aurp old/dh.h new/dh.h
+--- old/dh.h 2018-03-22 16:21:14.000000000 -1000
++++ new/dh.h 2018-03-23 10:05:03.889621527 -1000
+@@ -42,7 +42,7 @@ DH *dh_new_group18(void);
+ DH *dh_new_group_fallback(int);
+
+ int dh_gen_key(DH *, int);
+-int dh_pub_is_valid(DH *, BIGNUM *);
++int dh_pub_is_valid(const DH *, const BIGNUM *);
+
+ u_int dh_estimate(int);
+
+diff -aurp old/digest-openssl.c new/digest-openssl.c
+--- old/digest-openssl.c 2018-03-22 16:21:14.000000000 -1000
++++ new/digest-openssl.c 2018-03-23 10:05:03.889621527 -1000
+@@ -43,7 +43,7 @@
+
+ struct ssh_digest_ctx {
+ int alg;
+- EVP_MD_CTX mdctx;
++ EVP_MD_CTX *mdctx;
+ };
+
+ struct ssh_digest {
+@@ -106,20 +106,21 @@ ssh_digest_bytes(int alg)
+ size_t
+ ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
+ {
+- return EVP_MD_CTX_block_size(&ctx->mdctx);
++ return EVP_MD_CTX_block_size(ctx->mdctx);
+ }
+
+ struct ssh_digest_ctx *
+ ssh_digest_start(int alg)
+ {
+ const struct ssh_digest *digest = ssh_digest_by_alg(alg);
+- struct ssh_digest_ctx *ret;
++ struct ssh_digest_ctx *ret = NULL;
+
+ if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
+ return NULL;
+ ret->alg = alg;
+- EVP_MD_CTX_init(&ret->mdctx);
+- if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
++ if ((ret->mdctx = EVP_MD_CTX_new()) == NULL ||
++ EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
++ EVP_MD_CTX_free(ret->mdctx);
+ free(ret);
+ return NULL;
+ }
+@@ -132,7 +133,7 @@ ssh_digest_copy_state(struct ssh_digest_
+ if (from->alg != to->alg)
+ return SSH_ERR_INVALID_ARGUMENT;
+ /* we have bcopy-style order while openssl has memcpy-style */
+- if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
++ if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
+ return SSH_ERR_LIBCRYPTO_ERROR;
+ return 0;
+ }
+@@ -140,7 +141,7 @@ ssh_digest_copy_state(struct ssh_digest_
+ int
+ ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
+ {
+- if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
++ if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
+ return SSH_ERR_LIBCRYPTO_ERROR;
+ return 0;
+ }
+@@ -161,7 +162,7 @@ ssh_digest_final(struct ssh_digest_ctx *
+ return SSH_ERR_INVALID_ARGUMENT;
+ if (dlen < digest->digest_len) /* No truncation allowed */
+ return SSH_ERR_INVALID_ARGUMENT;
+- if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
++ if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
+ return SSH_ERR_LIBCRYPTO_ERROR;
+ if (l != digest->digest_len) /* sanity */
+ return SSH_ERR_INTERNAL_ERROR;
+@@ -172,7 +173,7 @@ void
+ ssh_digest_free(struct ssh_digest_ctx *ctx)
+ {
+ if (ctx != NULL) {
+- EVP_MD_CTX_cleanup(&ctx->mdctx);
++ EVP_MD_CTX_free(ctx->mdctx);
+ explicit_bzero(ctx, sizeof(*ctx));
+ free(ctx);
+ }
+diff -aurp old/kexdhc.c new/kexdhc.c
+--- old/kexdhc.c 2018-03-22 16:21:14.000000000 -1000
++++ new/kexdhc.c 2018-03-23 10:05:03.889621527 -1000
+@@ -81,11 +81,16 @@ kexdh_client(struct ssh *ssh)
+ goto out;
+ }
+ debug("sending SSH2_MSG_KEXDH_INIT");
+- if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
+- (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
+- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
++ {
++ const BIGNUM *pub_key;
++ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
++ goto out;
++ DH_get0_key(kex->dh, &pub_key, NULL);
++ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
++ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
+ (r = sshpkt_send(ssh)) != 0)
+ goto out;
++ }
+ #ifdef DEBUG_KEXDH
+ DHparams_print_fp(stderr, kex->dh);
+ fprintf(stderr, "pub= ");
+@@ -169,6 +174,9 @@ input_kex_dh(int type, u_int32_t seq, st
+
+ /* calc and verify H */
+ hashlen = sizeof(hash);
++ {
++ const BIGNUM *pub_key;
++ DH_get0_key(kex->dh, &pub_key, NULL);
+ if ((r = kex_dh_hash(
+ kex->hash_alg,
+ kex->client_version_string,
+@@ -176,11 +184,13 @@ input_kex_dh(int type, u_int32_t seq, st
+ sshbuf_ptr(kex->my), sshbuf_len(kex->my),
+ sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
+ server_host_key_blob, sbloblen,
+- kex->dh->pub_key,
++ pub_key,
+ dh_server_pub,
+ shared_secret,
+- hash, &hashlen)) != 0)
++ hash, &hashlen)) != 0) {
+ goto out;
++ }
++ }
+
+ if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
+ kex->hostkey_alg, ssh->compat)) != 0)
+diff -aurp old/kexdhs.c new/kexdhs.c
+--- old/kexdhs.c 2018-03-22 16:21:14.000000000 -1000
++++ new/kexdhs.c 2018-03-23 10:58:58.126733207 -1000
+@@ -163,6 +163,9 @@ input_kex_dh_init(int type, u_int32_t se
+ goto out;
+ /* calc H */
+ hashlen = sizeof(hash);
++ {
++ const BIGNUM *pub_key;
++ DH_get0_key(kex->dh, &pub_key, NULL);
+ if ((r = kex_dh_hash(
+ kex->hash_alg,
+ kex->client_version_string,
+@@ -171,10 +174,12 @@ input_kex_dh_init(int type, u_int32_t se
+ sshbuf_ptr(kex->my), sshbuf_len(kex->my),
+ server_host_key_blob, sbloblen,
+ dh_client_pub,
+- kex->dh->pub_key,
++ pub_key,
+ shared_secret,
+- hash, &hashlen)) != 0)
++ hash, &hashlen)) != 0) {
+ goto out;
++ }
++ }
+
+ /* save session id := H */
+ if (kex->session_id == NULL) {
+@@ -195,12 +200,17 @@ input_kex_dh_init(int type, u_int32_t se
+ /* destroy_sensitive_data(); */
+
+ /* send server hostkey, DH pubkey 'f' and singed H */
++ {
++ const BIGNUM *pub_key;
++ DH_get0_key(kex->dh, &pub_key, NULL);
+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
+ (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
+- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
++ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
+ (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
+- (r = sshpkt_send(ssh)) != 0)
++ (r = sshpkt_send(ssh)) != 0) {
+ goto out;
++ }
++ }
+
+ if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
+ r = kex_send_newkeys(ssh);
+diff -aurp old/kexgexc.c new/kexgexc.c
+--- old/kexgexc.c 2018-03-22 16:21:14.000000000 -1000
++++ new/kexgexc.c 2018-03-23 11:00:00.132866201 -1000
+@@ -118,11 +118,17 @@ input_kex_dh_gex_group(int type, u_int32
+ p = g = NULL; /* belong to kex->dh now */
+
+ /* generate and send 'e', client DH public key */
+- if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
+- (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
+- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
+- (r = sshpkt_send(ssh)) != 0)
++ {
++ const BIGNUM *pub_key;
++ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
++ goto out;
++ DH_get0_key(kex->dh, &pub_key, NULL);
++ if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
++ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
++ (r = sshpkt_send(ssh)) != 0) {
+ goto out;
++ }
++ }
+ debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
+ #ifdef DEBUG_KEXDH
+ DHparams_print_fp(stderr, kex->dh);
+@@ -212,6 +218,10 @@ input_kex_dh_gex_reply(int type, u_int32
+
+ /* calc and verify H */
+ hashlen = sizeof(hash);
++ {
++ const BIGNUM *p, *g, *pub_key;
++ DH_get0_pqg(kex->dh, &p, NULL, &g);
++ DH_get0_key(kex->dh, &pub_key, NULL);
+ if ((r = kexgex_hash(
+ kex->hash_alg,
+ kex->client_version_string,
+@@ -220,12 +230,14 @@ input_kex_dh_gex_reply(int type, u_int32
+ sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
+ server_host_key_blob, sbloblen,
+ kex->min, kex->nbits, kex->max,
+- kex->dh->p, kex->dh->g,
+- kex->dh->pub_key,
++ p, g,
++ pub_key,
+ dh_server_pub,
+ shared_secret,
+- hash, &hashlen)) != 0)
++ hash, &hashlen)) != 0) {
+ goto out;
++ }
++ }
+
+ if ((r = sshkey_verify(server_host_key, signature, slen, hash,
+ hashlen, kex->hostkey_alg, ssh->compat)) != 0)
+diff -aurp old/kexgexs.c new/kexgexs.c
+--- old/kexgexs.c 2018-03-22 16:21:14.000000000 -1000
++++ new/kexgexs.c 2018-03-23 11:03:06.045049721 -1000
+@@ -101,11 +101,16 @@ input_kex_dh_gex_request(int type, u_int
+ goto out;
+ }
+ debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
++ {
++ const BIGNUM *p, *g;
++ DH_get0_pqg(kex->dh, &p, NULL, &g);
+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
+- (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
+- (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
+- (r = sshpkt_send(ssh)) != 0)
++ (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
++ (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
++ (r = sshpkt_send(ssh)) != 0) {
+ goto out;
++ }
++ }
+
+ /* Compute our exchange value in parallel with the client */
+ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
+@@ -191,6 +196,10 @@ input_kex_dh_gex_init(int type, u_int32_
+ goto out;
+ /* calc H */
+ hashlen = sizeof(hash);
++ {
++ const BIGNUM *p, *g, *pub_key;
++ DH_get0_pqg(kex->dh, &p, NULL, &g);
++ DH_get0_key(kex->dh, &pub_key, NULL);
+ if ((r = kexgex_hash(
+ kex->hash_alg,
+ kex->client_version_string,
+@@ -199,12 +208,14 @@ input_kex_dh_gex_init(int type, u_int32_
+ sshbuf_ptr(kex->my), sshbuf_len(kex->my),
+ server_host_key_blob, sbloblen,
+ kex->min, kex->nbits, kex->max,
+- kex->dh->p, kex->dh->g,
++ p, g,
+ dh_client_pub,
+- kex->dh->pub_key,
++ pub_key,
+ shared_secret,
+- hash, &hashlen)) != 0)
++ hash, &hashlen)) != 0) {
+ goto out;
++ }
++ }
+
+ /* save session id := H */
+ if (kex->session_id == NULL) {
+@@ -225,12 +236,17 @@ input_kex_dh_gex_init(int type, u_int32_
+ /* destroy_sensitive_data(); */
+
+ /* send server hostkey, DH pubkey 'f' and singed H */
++ {
++ const BIGNUM *pub_key;
++ DH_get0_key(kex->dh, &pub_key, NULL);
+ if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
+ (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
+- (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
++ (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
+ (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
+- (r = sshpkt_send(ssh)) != 0)
++ (r = sshpkt_send(ssh)) != 0) {
+ goto out;
++ }
++ }
+
+ if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
+ r = kex_send_newkeys(ssh);
+diff -aurp old/monitor.c new/monitor.c
+--- old/monitor.c 2018-03-22 16:21:14.000000000 -1000
++++ new/monitor.c 2018-03-23 10:05:03.890621610 -1000
+@@ -595,10 +595,12 @@ mm_answer_moduli(int sock, Buffer *m)
+ buffer_put_char(m, 0);
+ return (0);
+ } else {
++ const BIGNUM *p, *g;
++ DH_get0_pqg(dh, &p, NULL, &g);
+ /* Send first bignum */
+ buffer_put_char(m, 1);
+- buffer_put_bignum2(m, dh->p);
+- buffer_put_bignum2(m, dh->g);
++ buffer_put_bignum2(m, p);
++ buffer_put_bignum2(m, g);
+
+ DH_free(dh);
+ }
+diff -aurp old/openbsd-compat/openssl-compat.c new/openbsd-compat/openssl-compat.c
+--- old/openbsd-compat/openssl-compat.c 2018-03-22 16:21:14.000000000 -1000
++++ new/openbsd-compat/openssl-compat.c 2018-03-23 10:05:03.890621610 -1000
+@@ -75,7 +75,6 @@ ssh_OpenSSL_add_all_algorithms(void)
+ /* Enable use of crypto hardware */
+ ENGINE_load_builtin_engines();
+ ENGINE_register_all_complete();
+- OPENSSL_config(NULL);
+ }
+ #endif
+
+diff -aurp old/regress/unittests/sshkey/test_file.c new/regress/unittests/sshkey/test_file.c
+--- old/regress/unittests/sshkey/test_file.c 2018-03-22 16:21:14.000000000 -1000
++++ new/regress/unittests/sshkey/test_file.c 2018-03-23 10:05:03.890621610 -1000
+@@ -60,9 +60,14 @@ sshkey_file_tests(void)
+ a = load_bignum("rsa_1.param.n");
+ b = load_bignum("rsa_1.param.p");
+ c = load_bignum("rsa_1.param.q");
+- ASSERT_BIGNUM_EQ(k1->rsa->n, a);
+- ASSERT_BIGNUM_EQ(k1->rsa->p, b);
+- ASSERT_BIGNUM_EQ(k1->rsa->q, c);
++ {
++ const BIGNUM *n, *p, *q;
++ RSA_get0_key(k1->rsa, &n, NULL, NULL);
++ RSA_get0_factors(k1->rsa, &p, &q);
++ ASSERT_BIGNUM_EQ(n, a);
++ ASSERT_BIGNUM_EQ(p, b);
++ ASSERT_BIGNUM_EQ(q, c);
++ }
+ BN_free(a);
+ BN_free(b);
+ BN_free(c);
+@@ -151,9 +156,14 @@ sshkey_file_tests(void)
+ a = load_bignum("dsa_1.param.g");
+ b = load_bignum("dsa_1.param.priv");
+ c = load_bignum("dsa_1.param.pub");
+- ASSERT_BIGNUM_EQ(k1->dsa->g, a);
+- ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
+- ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
++ {
++ const BIGNUM *g, *priv_key, *pub_key;
++ DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
++ DSA_get0_key(k1->dsa, &pub_key, &priv_key);
++ ASSERT_BIGNUM_EQ(g, a);
++ ASSERT_BIGNUM_EQ(priv_key, b);
++ ASSERT_BIGNUM_EQ(pub_key, c);
++ }
+ BN_free(a);
+ BN_free(b);
+ BN_free(c);
+diff -aurp old/regress/unittests/sshkey/test_sshkey.c new/regress/unittests/sshkey/test_sshkey.c
+--- old/regress/unittests/sshkey/test_sshkey.c 2018-03-22 16:21:14.000000000 -1000
++++ new/regress/unittests/sshkey/test_sshkey.c 2018-03-23 10:05:03.890621610 -1000
+@@ -197,9 +197,14 @@ sshkey_tests(void)
+ k1 = sshkey_new(KEY_RSA);
+ ASSERT_PTR_NE(k1, NULL);
+ ASSERT_PTR_NE(k1->rsa, NULL);
+- ASSERT_PTR_NE(k1->rsa->n, NULL);
+- ASSERT_PTR_NE(k1->rsa->e, NULL);
+- ASSERT_PTR_EQ(k1->rsa->p, NULL);
++ {
++ const BIGNUM *n, *e, *p;
++ RSA_get0_key(k1->rsa, &n, &e, NULL);
++ RSA_get0_factors(k1->rsa, &p, NULL);
++ ASSERT_PTR_NE(n, NULL);
++ ASSERT_PTR_NE(e, NULL);
++ ASSERT_PTR_EQ(p, NULL);
++ }
+ sshkey_free(k1);
+ TEST_DONE();
+
+@@ -207,8 +212,13 @@ sshkey_tests(void)
+ k1 = sshkey_new(KEY_DSA);
+ ASSERT_PTR_NE(k1, NULL);
+ ASSERT_PTR_NE(k1->dsa, NULL);
+- ASSERT_PTR_NE(k1->dsa->g, NULL);
+- ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
++ {
++ const BIGNUM *g, *priv_key;
++ DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
++ DSA_get0_key(k1->dsa, NULL, &priv_key);
++ ASSERT_PTR_NE(g, NULL);
++ ASSERT_PTR_EQ(priv_key, NULL);
++ }
+ sshkey_free(k1);
+ TEST_DONE();
+
+@@ -234,9 +244,14 @@ sshkey_tests(void)
+ k1 = sshkey_new_private(KEY_RSA);
+ ASSERT_PTR_NE(k1, NULL);
+ ASSERT_PTR_NE(k1->rsa, NULL);
+- ASSERT_PTR_NE(k1->rsa->n, NULL);
+- ASSERT_PTR_NE(k1->rsa->e, NULL);
+- ASSERT_PTR_NE(k1->rsa->p, NULL);
++ {
++ const BIGNUM *n, *e, *p;
++ RSA_get0_key(k1->rsa, &n, &e, NULL);
++ RSA_get0_factors(k1->rsa, &p, NULL);
++ ASSERT_PTR_NE(n, NULL);
++ ASSERT_PTR_NE(e, NULL);
++ ASSERT_PTR_NE(p, NULL);
++ }
+ ASSERT_INT_EQ(sshkey_add_private(k1), 0);
+ sshkey_free(k1);
+ TEST_DONE();
+@@ -245,8 +260,13 @@ sshkey_tests(void)
+ k1 = sshkey_new_private(KEY_DSA);
+ ASSERT_PTR_NE(k1, NULL);
+ ASSERT_PTR_NE(k1->dsa, NULL);
+- ASSERT_PTR_NE(k1->dsa->g, NULL);
+- ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
++ {
++ const BIGNUM *g, *priv_key;
++ DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
++ DSA_get0_key(k1->dsa, NULL, &priv_key);
++ ASSERT_PTR_NE(g, NULL);
++ ASSERT_PTR_NE(priv_key, NULL);
++ }
+ ASSERT_INT_EQ(sshkey_add_private(k1), 0);
+ sshkey_free(k1);
+ TEST_DONE();
+@@ -285,18 +305,28 @@ sshkey_tests(void)
+ ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
+ ASSERT_PTR_NE(kr, NULL);
+ ASSERT_PTR_NE(kr->rsa, NULL);
+- ASSERT_PTR_NE(kr->rsa->n, NULL);
+- ASSERT_PTR_NE(kr->rsa->e, NULL);
+- ASSERT_PTR_NE(kr->rsa->p, NULL);
+- ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
++ {
++ const BIGNUM *n, *e, *p;
++ RSA_get0_key(kr->rsa, &n, &e, NULL);
++ RSA_get0_factors(kr->rsa, &p, NULL);
++ ASSERT_PTR_NE(n, NULL);
++ ASSERT_PTR_NE(e, NULL);
++ ASSERT_PTR_NE(p, NULL);
++ ASSERT_INT_EQ(BN_num_bits(n), 1024);
++ }
+ TEST_DONE();
+
+ TEST_START("generate KEY_DSA");
+ ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
+ ASSERT_PTR_NE(kd, NULL);
+ ASSERT_PTR_NE(kd->dsa, NULL);
+- ASSERT_PTR_NE(kd->dsa->g, NULL);
+- ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
++ {
++ const BIGNUM *g, *priv_key;
++ DSA_get0_pqg(kd->dsa, NULL, NULL, &g);
++ DSA_get0_key(kd->dsa, NULL, &priv_key);
++ ASSERT_PTR_NE(g, NULL);
++ ASSERT_PTR_NE(priv_key, NULL);
++ }
+ TEST_DONE();
+
+ #ifdef OPENSSL_HAS_ECC
+@@ -323,9 +353,14 @@ sshkey_tests(void)
+ ASSERT_PTR_NE(kr, k1);
+ ASSERT_INT_EQ(k1->type, KEY_RSA);
+ ASSERT_PTR_NE(k1->rsa, NULL);
+- ASSERT_PTR_NE(k1->rsa->n, NULL);
+- ASSERT_PTR_NE(k1->rsa->e, NULL);
+- ASSERT_PTR_EQ(k1->rsa->p, NULL);
++ {
++ const BIGNUM *n, *e, *p;
++ RSA_get0_key(k1->rsa, &n, &e, NULL);
++ RSA_get0_factors(k1->rsa, &p, NULL);
++ ASSERT_PTR_NE(n, NULL);
++ ASSERT_PTR_NE(e, NULL);
++ ASSERT_PTR_EQ(p, NULL);
++ }
+ TEST_DONE();
+
+ TEST_START("equal KEY_RSA/demoted KEY_RSA");
+@@ -339,8 +374,13 @@ sshkey_tests(void)
+ ASSERT_PTR_NE(kd, k1);
+ ASSERT_INT_EQ(k1->type, KEY_DSA);
+ ASSERT_PTR_NE(k1->dsa, NULL);
+- ASSERT_PTR_NE(k1->dsa->g, NULL);
+- ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
++ {
++ const BIGNUM *g, *priv_key;
++ DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
++ DSA_get0_key(k1->dsa, NULL, &priv_key);
++ ASSERT_PTR_NE(g, NULL);
++ ASSERT_PTR_EQ(priv_key, NULL);
++ }
+ TEST_DONE();
+
+ TEST_START("equal KEY_DSA/demoted KEY_DSA");
+diff -aurp old/ssh-dss.c new/ssh-dss.c
+--- old/ssh-dss.c 2018-03-22 16:21:14.000000000 -1000
++++ new/ssh-dss.c 2018-03-23 10:05:03.891621693 -1000
+@@ -53,6 +53,7 @@ ssh_dss_sign(const struct sshkey *key, u
+ DSA_SIG *sig = NULL;
+ u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
+ size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
++ const BIGNUM *r, *s;
+ struct sshbuf *b = NULL;
+ int ret = SSH_ERR_INVALID_ARGUMENT;
+
+@@ -76,15 +77,16 @@ ssh_dss_sign(const struct sshkey *key, u
+ goto out;
+ }
+
+- rlen = BN_num_bytes(sig->r);
+- slen = BN_num_bytes(sig->s);
++ DSA_SIG_get0(sig, &r, &s);
++ rlen = BN_num_bytes(r);
++ slen = BN_num_bytes(s);
+ if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
+ ret = SSH_ERR_INTERNAL_ERROR;
+ goto out;
+ }
+ explicit_bzero(sigblob, SIGBLOB_LEN);
+- BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
+- BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
++ BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
++ BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
+
+ if ((b = sshbuf_new()) == NULL) {
+ ret = SSH_ERR_ALLOC_FAIL;
+@@ -154,17 +156,26 @@ ssh_dss_verify(const struct sshkey *key,
+ }
+
+ /* parse signature */
++ {
++ BIGNUM *r=NULL, *s=NULL;
+ if ((sig = DSA_SIG_new()) == NULL ||
+- (sig->r = BN_new()) == NULL ||
+- (sig->s = BN_new()) == NULL) {
++ (r = BN_new()) == NULL ||
++ (s = BN_new()) == NULL) {
+ ret = SSH_ERR_ALLOC_FAIL;
++ BN_free(r);
++ BN_free(s);
+ goto out;
+ }
+- if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
+- (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
++ if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
++ (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL)) {
+ ret = SSH_ERR_LIBCRYPTO_ERROR;
++ BN_free(r);
++ BN_free(s);
+ goto out;
+ }
++ DSA_SIG_set0(sig, r, s);
++ r = s = NULL;
++ }
+
+ /* sha1 the data */
+ if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
+diff -aurp old/ssh-ecdsa.c new/ssh-ecdsa.c
+--- old/ssh-ecdsa.c 2018-03-22 16:21:14.000000000 -1000
++++ new/ssh-ecdsa.c 2018-03-23 10:05:03.891621693 -1000
+@@ -80,9 +80,14 @@ ssh_ecdsa_sign(const struct sshkey *key,
+ ret = SSH_ERR_ALLOC_FAIL;
+ goto out;
+ }
+- if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
+- (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
++ {
++ const BIGNUM *r, *s;
++ ECDSA_SIG_get0(sig, &r, &s);
++ if ((ret = sshbuf_put_bignum2(bb, r)) != 0 ||
++ (ret = sshbuf_put_bignum2(bb, s)) != 0) {
+ goto out;
++ }
++ }
+ if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
+ (ret = sshbuf_put_stringb(b, bb)) != 0)
+ goto out;
+@@ -150,11 +155,27 @@ ssh_ecdsa_verify(const struct sshkey *ke
+ ret = SSH_ERR_ALLOC_FAIL;
+ goto out;
+ }
+- if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
+- sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
++ {
++ BIGNUM *r=NULL, *s=NULL;
++ if ((r = BN_new()) == NULL ||
++ (s = BN_new()) == NULL) {
++ ret = SSH_ERR_ALLOC_FAIL;
++ goto out_rs;
++ }
++ if (sshbuf_get_bignum2(sigbuf, r) != 0 ||
++ sshbuf_get_bignum2(sigbuf, s) != 0) {
+ ret = SSH_ERR_INVALID_FORMAT;
++ goto out_rs;
++ }
++ if (ECDSA_SIG_set0(sig, r, s) == 0) {
++ ret = SSH_ERR_LIBCRYPTO_ERROR;
++out_rs:
++ BN_free(r);
++ BN_free(s);
+ goto out;
+ }
++ r = s = NULL;
++ }
+ if (sshbuf_len(sigbuf) != 0) {
+ ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
+ goto out;
+diff -aurp old/ssh-keygen.c new/ssh-keygen.c
+--- old/ssh-keygen.c 2018-03-22 16:21:14.000000000 -1000
++++ new/ssh-keygen.c 2018-03-23 10:05:03.891621693 -1000
+@@ -493,11 +493,33 @@ do_convert_private_ssh2_from_blob(u_char
+
+ switch (key->type) {
+ case KEY_DSA:
+- buffer_get_bignum_bits(b, key->dsa->p);
+- buffer_get_bignum_bits(b, key->dsa->g);
+- buffer_get_bignum_bits(b, key->dsa->q);
+- buffer_get_bignum_bits(b, key->dsa->pub_key);
+- buffer_get_bignum_bits(b, key->dsa->priv_key);
++ {
++ BIGNUM *p=NULL, *g=NULL, *q=NULL, *pub_key=NULL, *priv_key=NULL;
++ if ((p=BN_new()) == NULL ||
++ (g=BN_new()) == NULL ||
++ (q=BN_new()) == NULL ||
++ (pub_key=BN_new()) == NULL ||
++ (priv_key=BN_new()) == NULL) {
++ BN_free(p);
++ BN_free(g);
++ BN_free(q);
++ BN_free(pub_key);
++ BN_free(priv_key);
++ return NULL;
++ }
++ buffer_get_bignum_bits(b, p);
++ buffer_get_bignum_bits(b, g);
++ buffer_get_bignum_bits(b, q);
++ buffer_get_bignum_bits(b, pub_key);
++ buffer_get_bignum_bits(b, priv_key);
++ if (DSA_set0_pqg(key->dsa, p, q, g) == 0 ||
++ DSA_set0_key(key->dsa, pub_key, priv_key) == 0) {
++ fatal("failed to set DSA key");
++ BN_free(p); BN_free(g); BN_free(q);
++ BN_free(pub_key); BN_free(priv_key);
++ return NULL;
++ }
++ }
+ break;
+ case KEY_RSA:
+ if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
+@@ -514,16 +536,52 @@ do_convert_private_ssh2_from_blob(u_char
+ e += e3;
+ debug("e %lx", e);
+ }
+- if (!BN_set_word(key->rsa->e, e)) {
++ {
++ BIGNUM *rsa_e = NULL;
++ BIGNUM *d=NULL, *n=NULL, *iqmp=NULL, *q=NULL, *p=NULL;
++ BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy input to set in RSA_set0_crt_params */
++ rsa_e = BN_new();
++ if (!rsa_e || !BN_set_word(rsa_e, e)) {
++ if (rsa_e) BN_free(rsa_e);
+ sshbuf_free(b);
+ sshkey_free(key);
+ return NULL;
+ }
+- buffer_get_bignum_bits(b, key->rsa->d);
+- buffer_get_bignum_bits(b, key->rsa->n);
+- buffer_get_bignum_bits(b, key->rsa->iqmp);
+- buffer_get_bignum_bits(b, key->rsa->q);
+- buffer_get_bignum_bits(b, key->rsa->p);
++ if ((d=BN_new()) == NULL ||
++ (n=BN_new()) == NULL ||
++ (iqmp=BN_new()) == NULL ||
++ (q=BN_new()) == NULL ||
++ (p=BN_new()) == NULL ||
++ (dmp1=BN_new()) == NULL ||
++ (dmq1=BN_new()) == NULL) {
++ BN_free(d); BN_free(n); BN_free(iqmp);
++ BN_free(q); BN_free(p);
++ BN_free(dmp1); BN_free(dmq1);
++ return NULL;
++ }
++ BN_clear(dmp1); BN_clear(dmq1);
++ buffer_get_bignum_bits(b, d);
++ buffer_get_bignum_bits(b, n);
++ buffer_get_bignum_bits(b, iqmp);
++ buffer_get_bignum_bits(b, q);
++ buffer_get_bignum_bits(b, p);
++ if (RSA_set0_key(key->rsa, n, rsa_e, d) == 0)
++ goto null;
++ n = d = NULL;
++ if (RSA_set0_factors(key->rsa, p, q) == 0)
++ goto null;
++ p = q = NULL;
++ /* dmp1, dmq1 should not be NULL for initial set0 */
++ if (RSA_set0_crt_params(key->rsa, dmp1, dmq1, iqmp) == 0) {
++ null:
++ fatal("Failed to set RSA parameters");
++ BN_free(d); BN_free(n); BN_free(iqmp);
++ BN_free(q); BN_free(p);
++ BN_free(dmp1); BN_free(dmq1);
++ return NULL;
++ }
++ dmp1 = dmq1 = iqmp = NULL;
++ }
+ if ((r = ssh_rsa_generate_additional_parameters(key)) != 0)
+ fatal("generate RSA parameters failed: %s", ssh_err(r));
+ break;
+@@ -633,7 +691,7 @@ do_convert_from_pkcs8(struct sshkey **k,
+ identity_file);
+ }
+ fclose(fp);
+- switch (EVP_PKEY_type(pubkey->type)) {
++ switch (EVP_PKEY_type(EVP_PKEY_id(pubkey))) {
+ case EVP_PKEY_RSA:
+ if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
+ fatal("sshkey_new failed");
+@@ -657,7 +715,7 @@ do_convert_from_pkcs8(struct sshkey **k,
+ #endif
+ default:
+ fatal("%s: unsupported pubkey type %d", __func__,
+- EVP_PKEY_type(pubkey->type));
++ EVP_PKEY_type(EVP_PKEY_id(pubkey)));
+ }
+ EVP_PKEY_free(pubkey);
+ return;
+diff -aurp old/ssh-pkcs11-client.c new/ssh-pkcs11-client.c
+--- old/ssh-pkcs11-client.c 2018-03-22 16:21:14.000000000 -1000
++++ new/ssh-pkcs11-client.c 2018-03-23 10:05:03.892621777 -1000
+@@ -144,12 +144,13 @@ pkcs11_rsa_private_encrypt(int flen, con
+ static int
+ wrap_key(RSA *rsa)
+ {
+- static RSA_METHOD helper_rsa;
++ static RSA_METHOD *helper_rsa;
+
+- memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
+- helper_rsa.name = "ssh-pkcs11-helper";
+- helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
+- RSA_set_method(rsa, &helper_rsa);
++ if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
++ return (-1); /* XXX but caller isn't checking */
++ RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
++ RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
++ RSA_set_method(rsa, helper_rsa);
+ return (0);
+ }
+
+diff -aurp old/ssh-pkcs11.c new/ssh-pkcs11.c
+--- old/ssh-pkcs11.c 2018-03-22 16:21:14.000000000 -1000
++++ new/ssh-pkcs11.c 2018-03-23 10:05:03.892621777 -1000
+@@ -67,7 +67,7 @@ struct pkcs11_key {
+ struct pkcs11_provider *provider;
+ CK_ULONG slotidx;
+ int (*orig_finish)(RSA *rsa);
+- RSA_METHOD rsa_method;
++ RSA_METHOD *rsa_method;
+ char *keyid;
+ int keyid_len;
+ };
+@@ -326,13 +326,15 @@ pkcs11_rsa_wrap(struct pkcs11_provider *
+ k11->keyid = xmalloc(k11->keyid_len);
+ memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
+ }
+- k11->orig_finish = def->finish;
+- memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
+- k11->rsa_method.name = "pkcs11";
+- k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
+- k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
+- k11->rsa_method.finish = pkcs11_rsa_finish;
+- RSA_set_method(rsa, &k11->rsa_method);
++ k11->orig_finish = RSA_meth_get_finish(def);
++
++ if ((k11->rsa_method = RSA_meth_new("pkcs11", RSA_meth_get_flags(def))) == NULL)
++ return -1;
++ RSA_meth_set_priv_enc(k11->rsa_method, pkcs11_rsa_private_encrypt);
++ RSA_meth_set_priv_dec(k11->rsa_method, pkcs11_rsa_private_decrypt);
++ RSA_meth_set_finish(k11->rsa_method, pkcs11_rsa_finish);
++
++ RSA_set_method(rsa, k11->rsa_method);
+ RSA_set_app_data(rsa, k11);
+ return (0);
+ }
+@@ -512,10 +514,19 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
+ if ((rsa = RSA_new()) == NULL) {
+ error("RSA_new failed");
+ } else {
+- rsa->n = BN_bin2bn(attribs[1].pValue,
+- attribs[1].ulValueLen, NULL);
+- rsa->e = BN_bin2bn(attribs[2].pValue,
+- attribs[2].ulValueLen, NULL);
++ BIGNUM *n=NULL, *e=NULL;
++ n = BN_new();
++ e = BN_new();
++ if (n == NULL || e == NULL)
++ error("BN_new alloc failed");
++ if (BN_bin2bn(attribs[1].pValue,
++ attribs[1].ulValueLen, n) == NULL ||
++ BN_bin2bn(attribs[2].pValue,
++ attribs[2].ulValueLen, e) == NULL)
++ error("BN_bin2bn failed");
++ if (RSA_set0_key(rsa, n, e, NULL) == 0)
++ error("RSA_set0_key failed");
++ n = e = NULL;
+ }
+ } else {
+ cp = attribs[2].pValue;
+@@ -525,16 +536,19 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
+ == NULL) {
+ error("d2i_X509 failed");
+ } else if ((evp = X509_get_pubkey(x509)) == NULL ||
+- evp->type != EVP_PKEY_RSA ||
+- evp->pkey.rsa == NULL) {
++ EVP_PKEY_id(evp) != EVP_PKEY_RSA ||
++ EVP_PKEY_get0_RSA(evp) == NULL) {
+ debug("X509_get_pubkey failed or no rsa");
+- } else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
++ } else if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp)))
+ == NULL) {
+ error("RSAPublicKey_dup");
+ }
+ X509_free(x509);
+ }
+- if (rsa && rsa->n && rsa->e &&
++ {
++ const BIGNUM *n, *e;
++ RSA_get0_key(rsa, &n, &e, NULL);
++ if (rsa && n && e &&
+ pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
+ if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
+ fatal("sshkey_new failed");
+@@ -554,6 +568,7 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
+ } else if (rsa) {
+ RSA_free(rsa);
+ }
++ }
+ for (i = 0; i < 3; i++)
+ free(attribs[i].pValue);
+ }
+diff -aurp old/ssh-rsa.c new/ssh-rsa.c
+--- old/ssh-rsa.c 2018-03-22 16:21:14.000000000 -1000
++++ new/ssh-rsa.c 2018-03-23 10:05:03.892621777 -1000
+@@ -84,7 +84,6 @@ ssh_rsa_generate_additional_parameters(s
+ {
+ BIGNUM *aux = NULL;
+ BN_CTX *ctx = NULL;
+- BIGNUM d;
+ int r;
+
+ if (key == NULL || key->rsa == NULL ||
+@@ -99,16 +98,27 @@ ssh_rsa_generate_additional_parameters(s
+ }
+ BN_set_flags(aux, BN_FLG_CONSTTIME);
+
+- BN_init(&d);
+- BN_with_flags(&d, key->rsa->d, BN_FLG_CONSTTIME);
+-
+- if ((BN_sub(aux, key->rsa->q, BN_value_one()) == 0) ||
+- (BN_mod(key->rsa->dmq1, &d, aux, ctx) == 0) ||
+- (BN_sub(aux, key->rsa->p, BN_value_one()) == 0) ||
+- (BN_mod(key->rsa->dmp1, &d, aux, ctx) == 0)) {
++ {
++ const BIGNUM *q, *d, *p;
++ BIGNUM *dmq1=NULL, *dmp1=NULL;
++ if ((dmq1 = BN_new()) == NULL ||
++ (dmp1 = BN_new()) == NULL ) {
++ r = SSH_ERR_ALLOC_FAIL;
++ goto out;
++ }
++ RSA_get0_key(key->rsa, NULL, NULL, &d);
++ RSA_get0_factors(key->rsa, &p, &q);
++ if ((BN_sub(aux, q, BN_value_one()) == 0) ||
++ (BN_mod(dmq1, d, aux, ctx) == 0) ||
++ (BN_sub(aux, p, BN_value_one()) == 0) ||
++ (BN_mod(dmp1, d, aux, ctx) == 0) ||
++ RSA_set0_crt_params(key->rsa, dmp1, dmq1, NULL) == 0) {
+ r = SSH_ERR_LIBCRYPTO_ERROR;
++ BN_clear_free(dmp1);
++ BN_clear_free(dmq1);
+ goto out;
+ }
++ }
+ r = 0;
+ out:
+ BN_clear_free(aux);
+@@ -139,7 +149,7 @@ ssh_rsa_sign(const struct sshkey *key, u
+ if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
+ sshkey_type_plain(key->type) != KEY_RSA)
+ return SSH_ERR_INVALID_ARGUMENT;
+- if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
++ if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
+ return SSH_ERR_KEY_LENGTH;
+ slen = RSA_size(key->rsa);
+ if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
+@@ -211,7 +221,7 @@ ssh_rsa_verify(const struct sshkey *key,
+ sshkey_type_plain(key->type) != KEY_RSA ||
+ sig == NULL || siglen == 0)
+ return SSH_ERR_INVALID_ARGUMENT;
+- if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
++ if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
+ return SSH_ERR_KEY_LENGTH;
+
+ if ((b = sshbuf_from(sig, siglen)) == NULL)
+diff -aurp old/sshkey.c new/sshkey.c
+--- old/sshkey.c 2018-03-22 16:21:14.000000000 -1000
++++ new/sshkey.c 2018-03-23 10:05:03.893621860 -1000
+@@ -274,10 +274,18 @@ sshkey_size(const struct sshkey *k)
+ #ifdef WITH_OPENSSL
+ case KEY_RSA:
+ case KEY_RSA_CERT:
+- return BN_num_bits(k->rsa->n);
++#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
++ return RSA_bits(k->rsa);
++#else
++ return RSA_bits(key->rsa);
++#endif
+ case KEY_DSA:
+ case KEY_DSA_CERT:
++#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
++ return DSA_bits(k->dsa);
++#else
+ return BN_num_bits(k->dsa->p);
++#endif
+ case KEY_ECDSA:
+ case KEY_ECDSA_CERT:
+ return sshkey_curve_nid_to_bits(k->ecdsa_nid);
+@@ -482,26 +490,53 @@ sshkey_new(int type)
+ #ifdef WITH_OPENSSL
+ case KEY_RSA:
+ case KEY_RSA_CERT:
++ {
++ BIGNUM *n=NULL, *e=NULL; /* just allocate */
+ if ((rsa = RSA_new()) == NULL ||
+- (rsa->n = BN_new()) == NULL ||
+- (rsa->e = BN_new()) == NULL) {
++ (n = BN_new()) == NULL ||
++ (e = BN_new()) == NULL) {
++ BN_free(n);
++ BN_free(e);
+ RSA_free(rsa);
+ free(k);
+ return NULL;
+ }
++ BN_clear(n); BN_clear(e);
++ if (RSA_set0_key(rsa, n, e, NULL) == 0)
++ return NULL;
++ n = e = NULL;
++ }
+ k->rsa = rsa;
+ break;
+ case KEY_DSA:
+ case KEY_DSA_CERT:
++ {
++ BIGNUM *p=NULL, *q=NULL, *g=NULL, *pubkey=NULL; /* just allocate */
+ if ((dsa = DSA_new()) == NULL ||
+- (dsa->p = BN_new()) == NULL ||
+- (dsa->q = BN_new()) == NULL ||
+- (dsa->g = BN_new()) == NULL ||
+- (dsa->pub_key = BN_new()) == NULL) {
++ (p = BN_new()) == NULL ||
++ (q = BN_new()) == NULL ||
++ (g = BN_new()) == NULL ||
++ (pubkey = BN_new()) == NULL) {
++ BN_free(p);
++ BN_free(q);
++ BN_free(g);
++ BN_free(pubkey);
+ DSA_free(dsa);
+ free(k);
+ return NULL;
+ }
++ if (DSA_set0_pqg(dsa, p, q, g) == 0) {
++ BN_free(p); BN_free(q); BN_free(g);
++ BN_free(pubkey);
++ return NULL;
++ }
++ p = q = g = NULL;
++ if (DSA_set0_key(dsa, pubkey, NULL) == 0) {
++ BN_free(pubkey);
++ return NULL;
++ }
++ pubkey = NULL;
++ }
+ k->dsa = dsa;
+ break;
+ case KEY_ECDSA:
+@@ -539,6 +574,51 @@ sshkey_add_private(struct sshkey *k)
+ #ifdef WITH_OPENSSL
+ case KEY_RSA:
+ case KEY_RSA_CERT:
++#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
++ /* Allocate BIGNUM. This is a mess.
++ For OpenSSL 1.1.x API these shouldn't be mandatory,
++ but some regression tests for non-NULL pointer of
++ the data. */
++#define new_or_dup(bn, nbn) \
++ if (bn == NULL) { \
++ if ((nbn = BN_new()) == NULL) \
++ return SSH_ERR_ALLOC_FAIL; \
++ } else { \
++ /* otherwise use-after-free will occur */ \
++ if ((nbn = BN_dup(bn)) == NULL) \
++ return SSH_ERR_ALLOC_FAIL; \
++ }
++ {
++ const BIGNUM *d, *iqmp, *q, *p, *dmq1, *dmp1; /* allocate if NULL */
++ BIGNUM *nd, *niqmp, *nq, *np, *ndmq1, *ndmp1;
++
++ RSA_get0_key(k->rsa, NULL, NULL, &d);
++ RSA_get0_factors(k->rsa, &p, &q);
++ RSA_get0_crt_params(k->rsa, &dmp1, &dmq1, &iqmp);
++
++ new_or_dup(d, nd);
++ new_or_dup(iqmp, niqmp);
++ new_or_dup(q, nq);
++ new_or_dup(p, np);
++ new_or_dup(dmq1, ndmq1);
++ new_or_dup(dmp1, ndmp1);
++
++ if (RSA_set0_key(k->rsa, NULL, NULL, nd) == 0)
++ goto error1;
++ nd = NULL;
++ if (RSA_set0_factors(k->rsa, np, nq) == 0)
++ goto error1;
++ np = nq = NULL;
++ if (RSA_set0_crt_params(k->rsa, ndmp1, ndmq1, niqmp) == 0) {
++error1:
++ BN_free(nd);
++ BN_free(np); BN_free(nq);
++ BN_free(ndmp1); BN_free(ndmq1); BN_free(niqmp);
++ return SSH_ERR_LIBCRYPTO_ERROR;
++ }
++ ndmp1 = ndmq1 = niqmp = NULL;
++ }
++#else
+ #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
+ if (bn_maybe_alloc_failed(k->rsa->d) ||
+ bn_maybe_alloc_failed(k->rsa->iqmp) ||
+@@ -547,13 +627,28 @@ sshkey_add_private(struct sshkey *k)
+ bn_maybe_alloc_failed(k->rsa->dmq1) ||
+ bn_maybe_alloc_failed(k->rsa->dmp1))
+ return SSH_ERR_ALLOC_FAIL;
++#endif
+ break;
+ case KEY_DSA:
+ case KEY_DSA_CERT:
++#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
++ {
++ const BIGNUM *priv_key;
++ BIGNUM *npriv_key;
++ DSA_get0_key(k->dsa, NULL, &priv_key);
++ new_or_dup(priv_key, npriv_key);
++ if (DSA_set0_key(k->dsa, NULL, npriv_key) == 0) {
++ BN_free(npriv_key);
++ return SSH_ERR_LIBCRYPTO_ERROR;
++ }
++ }
++#else
+ if (bn_maybe_alloc_failed(k->dsa->priv_key))
+ return SSH_ERR_ALLOC_FAIL;
++#endif
+ break;
+ #undef bn_maybe_alloc_failed
++#undef new_or_dup
+ case KEY_ECDSA:
+ case KEY_ECDSA_CERT:
+ /* Cannot do anything until we know the group */
+@@ -677,16 +772,34 @@ sshkey_equal_public(const struct sshkey
+ #ifdef WITH_OPENSSL
+ case KEY_RSA_CERT:
+ case KEY_RSA:
+- return a->rsa != NULL && b->rsa != NULL &&
+- BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
+- BN_cmp(a->rsa->n, b->rsa->n) == 0;
++ {
++ const BIGNUM *a_e, *b_e, *a_n, *b_n;
++ const BIGNUM *a_d, *b_d;
++ if (a->rsa == NULL) return 0;
++ if (b->rsa == NULL) return 0;
++ RSA_get0_key(a->rsa, &a_n, &a_e, &a_d);
++ RSA_get0_key(b->rsa, &b_n, &b_e, &b_d);
++ return
++ BN_cmp(a_e, b_e) == 0 &&
++ BN_cmp(a_n, b_n) == 0;
++ }
+ case KEY_DSA_CERT:
+ case KEY_DSA:
+- return a->dsa != NULL && b->dsa != NULL &&
+- BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
+- BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
+- BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
+- BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
++ {
++ const BIGNUM *a_p, *a_q, *a_g, *a_pub_key;
++ const BIGNUM *b_p, *b_q, *b_g, *b_pub_key;
++ if (a->dsa == NULL) return 0;
++ if (b->dsa == NULL) return 0;
++ DSA_get0_pqg(a->dsa, &a_p, &a_q, &a_g);
++ DSA_get0_pqg(b->dsa, &b_p, &b_q, &b_g);
++ DSA_get0_key(a->dsa, &a_pub_key, NULL);
++ DSA_get0_key(b->dsa, &b_pub_key, NULL);
++ return
++ BN_cmp(a_p, b_p) == 0 &&
++ BN_cmp(a_q, b_q) == 0 &&
++ BN_cmp(a_g, b_g) == 0 &&
++ BN_cmp(a_pub_key, b_pub_key) == 0;
++ }
+ # ifdef OPENSSL_HAS_ECC
+ case KEY_ECDSA_CERT:
+ case KEY_ECDSA:
+@@ -775,12 +888,17 @@ to_blob_buf(const struct sshkey *key, st
+ case KEY_DSA:
+ if (key->dsa == NULL)
+ return SSH_ERR_INVALID_ARGUMENT;
++ {
++ const BIGNUM *p, *q, *g, *pub_key;
++ DSA_get0_pqg(key->dsa, &p, &q, &g);
++ DSA_get0_key(key->dsa, &pub_key, NULL);
+ if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
+- (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
+- (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
+- (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
+- (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
++ (ret = sshbuf_put_bignum2(b, p)) != 0 ||
++ (ret = sshbuf_put_bignum2(b, q)) != 0 ||
++ (ret = sshbuf_put_bignum2(b, g)) != 0 ||
++ (ret = sshbuf_put_bignum2(b, pub_key)) != 0)
+ return ret;
++ }
+ break;
+ # ifdef OPENSSL_HAS_ECC
+ case KEY_ECDSA:
+@@ -796,10 +914,14 @@ to_blob_buf(const struct sshkey *key, st
+ case KEY_RSA:
+ if (key->rsa == NULL)
+ return SSH_ERR_INVALID_ARGUMENT;
++ {
++ const BIGNUM *e, *n;
++ RSA_get0_key(key->rsa, &n, &e, NULL);
+ if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
+- (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
+- (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
++ (ret = sshbuf_put_bignum2(b, e)) != 0 ||
++ (ret = sshbuf_put_bignum2(b, n)) != 0)
+ return ret;
++ }
+ break;
+ #endif /* WITH_OPENSSL */
+ case KEY_ED25519:
+@@ -1740,13 +1862,32 @@ sshkey_from_private(const struct sshkey
+ case KEY_DSA_CERT:
+ if ((n = sshkey_new(k->type)) == NULL)
+ return SSH_ERR_ALLOC_FAIL;
+- if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
+- (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
+- (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
+- (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
++ {
++ const BIGNUM *p, *q, *g, *pub_key, *priv_key;
++ BIGNUM *cp=NULL, *cq=NULL, *cg=NULL, *cpub_key=NULL;
++ DSA_get0_pqg(k->dsa, &p, &q, &g);
++ DSA_get0_key(k->dsa, &pub_key, &priv_key);
++ if ((cp = BN_dup(p)) == NULL ||
++ (cq = BN_dup(q)) == NULL ||
++ (cg = BN_dup(g)) == NULL ||
++ (cpub_key = BN_dup(pub_key)) == NULL) {
++ BN_free(cp); BN_free(cq); BN_free(cg);
++ BN_free(cpub_key);
+ sshkey_free(n);
+ return SSH_ERR_ALLOC_FAIL;
+ }
++ if (DSA_set0_pqg(n->dsa, cp, cq, cg) == 0)
++ goto error1;
++ cp = cq = cg = NULL;
++ if (DSA_set0_key(n->dsa, cpub_key, NULL) == 0) {
++error1:
++ BN_free(cp); BN_free(cq); BN_free(cg);
++ BN_free(cpub_key);
++ sshkey_free(n);
++ return SSH_ERR_LIBCRYPTO_ERROR;
++ }
++ cpub_key = NULL;
++ }
+ break;
+ # ifdef OPENSSL_HAS_ECC
+ case KEY_ECDSA:
+@@ -1770,11 +1911,23 @@ sshkey_from_private(const struct sshkey
+ case KEY_RSA_CERT:
+ if ((n = sshkey_new(k->type)) == NULL)
+ return SSH_ERR_ALLOC_FAIL;
+- if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
+- (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
++ {
++ const BIGNUM *nn, *e, *d;
++ BIGNUM *cn=NULL, *ce=NULL;
++ RSA_get0_key(k->rsa, &nn, &e, &d);
++ if ((cn = BN_dup(nn)) == NULL ||
++ (ce = BN_dup(e)) == NULL ) {
++ BN_free(cn); BN_free(ce);
+ sshkey_free(n);
+ return SSH_ERR_ALLOC_FAIL;
+ }
++ if (RSA_set0_key(n->rsa, cn, ce, NULL) == 0) {
++ BN_free(cn); BN_free(ce);
++ sshkey_free(n);
++ return SSH_ERR_LIBCRYPTO_ERROR;
++ }
++ cn = ce = NULL;
++ }
+ break;
+ #endif /* WITH_OPENSSL */
+ case KEY_ED25519:
+@@ -1995,12 +2148,27 @@ sshkey_from_blob_internal(struct sshbuf
+ ret = SSH_ERR_ALLOC_FAIL;
+ goto out;
+ }
+- if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
+- sshbuf_get_bignum2(b, key->rsa->n) != 0) {
++ {
++ BIGNUM *e=NULL, *n=NULL;
++ if ((e = BN_new()) == NULL ||
++ (n = BN_new()) == NULL ) {
++ ret = SSH_ERR_ALLOC_FAIL;
++ BN_free(e); BN_free(n);
++ goto out;
++ }
++ if (sshbuf_get_bignum2(b, e) != 0 ||
++ sshbuf_get_bignum2(b, n) != 0) {
+ ret = SSH_ERR_INVALID_FORMAT;
++ BN_free(e); BN_free(n);
+ goto out;
+ }
+- if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
++ if (RSA_set0_key(key->rsa, n, e, NULL) == 0) {
++ BN_free(e); BN_free(n);
++ return SSH_ERR_LIBCRYPTO_ERROR;
++ }
++ n = e = NULL;
++ }
++ if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
+ ret = SSH_ERR_KEY_LENGTH;
+ goto out;
+ }
+@@ -2020,13 +2188,36 @@ sshkey_from_blob_internal(struct sshbuf
+ ret = SSH_ERR_ALLOC_FAIL;
+ goto out;
+ }
+- if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
+- sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
+- sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
+- sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
++ {
++ BIGNUM *p=NULL, *q=NULL, *g=NULL, *pub_key=NULL;
++ if ((p = BN_new()) == NULL ||
++ (q = BN_new()) == NULL ||
++ (g = BN_new()) == NULL ||
++ (pub_key = BN_new()) == NULL) {
++ ret = SSH_ERR_ALLOC_FAIL;
++ goto error1;
++ }
++ if (sshbuf_get_bignum2(b, p) != 0 ||
++ sshbuf_get_bignum2(b, q) != 0 ||
++ sshbuf_get_bignum2(b, g) != 0 ||
++ sshbuf_get_bignum2(b, pub_key) != 0) {
+ ret = SSH_ERR_INVALID_FORMAT;
++ goto error1;
++ }
++ if (DSA_set0_pqg(key->dsa, p, q, g) == 0) {
++ ret = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error1;
++ }
++ p = q = g = NULL;
++ if (DSA_set0_key(key->dsa, pub_key, NULL) == 0) {
++ ret = SSH_ERR_LIBCRYPTO_ERROR;
++error1:
++ BN_free(p); BN_free(q); BN_free(g);
++ BN_free(pub_key);
+ goto out;
+ }
++ pub_key = NULL;
++ }
+ #ifdef DEBUG_PK
+ DSA_print_fp(stderr, key->dsa, 8);
+ #endif
+@@ -2327,26 +2518,63 @@ sshkey_demote(const struct sshkey *k, st
+ goto fail;
+ /* FALLTHROUGH */
+ case KEY_RSA:
+- if ((pk->rsa = RSA_new()) == NULL ||
+- (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
+- (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
++ if ((pk->rsa = RSA_new()) == NULL ){
+ ret = SSH_ERR_ALLOC_FAIL;
+ goto fail;
+ }
++ {
++ const BIGNUM *ke, *kn;
++ BIGNUM *pke=NULL, *pkn=NULL;
++ RSA_get0_key(k->rsa, &kn, &ke, NULL);
++ if ((pke = BN_dup(ke)) == NULL ||
++ (pkn = BN_dup(kn)) == NULL) {
++ ret = SSH_ERR_ALLOC_FAIL;
++ BN_free(pke); BN_free(pkn);
++ goto fail;
++ }
++ if (RSA_set0_key(pk->rsa, pkn, pke, NULL) == 0) {
++ ret = SSH_ERR_LIBCRYPTO_ERROR;
++ BN_free(pke); BN_free(pkn);
++ goto fail;
++ }
++ pkn = pke = NULL;
++ }
+ break;
+ case KEY_DSA_CERT:
+ if ((ret = sshkey_cert_copy(k, pk)) != 0)
+ goto fail;
+ /* FALLTHROUGH */
+ case KEY_DSA:
+- if ((pk->dsa = DSA_new()) == NULL ||
+- (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
+- (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
+- (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
+- (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
++ if ((pk->dsa = DSA_new()) == NULL ) {
+ ret = SSH_ERR_ALLOC_FAIL;
+ goto fail;
+ }
++ {
++ const BIGNUM *kp, *kq, *kg, *kpub_key;
++ BIGNUM *pkp=NULL, *pkq=NULL, *pkg=NULL, *pkpub_key=NULL;
++ DSA_get0_pqg(k->dsa, &kp, &kq, &kg);
++ DSA_get0_key(k->dsa, &kpub_key, NULL);
++ if ((pkp = BN_dup(kp)) == NULL ||
++ (pkq = BN_dup(kq)) == NULL ||
++ (pkg = BN_dup(kg)) == NULL ||
++ (pkpub_key = BN_dup(kpub_key)) == NULL) {
++ ret = SSH_ERR_ALLOC_FAIL;
++ goto error1;
++ }
++ if (DSA_set0_pqg(pk->dsa, pkp, pkq, pkg) == 0) {
++ ret = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error1;
++ }
++ pkp = pkq = pkg = NULL;
++ if (DSA_set0_key(pk->dsa, pkpub_key, NULL) == 0) {
++ ret = SSH_ERR_LIBCRYPTO_ERROR;
++error1:
++ BN_free(pkp); BN_free(pkq); BN_free(pkg);
++ BN_free(pkpub_key);
++ goto fail;
++ }
++ pkpub_key = NULL;
++ }
+ break;
+ case KEY_ECDSA_CERT:
+ if ((ret = sshkey_cert_copy(k, pk)) != 0)
+@@ -2496,11 +2724,17 @@ sshkey_certify_custom(struct sshkey *k,
+ switch (k->type) {
+ #ifdef WITH_OPENSSL
+ case KEY_DSA_CERT:
+- if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
+- (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
+- (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
+- (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
++ {
++ const BIGNUM *p, *q, *g, *pub_key;
++ DSA_get0_pqg(k->dsa, &p, &q, &g);
++ DSA_get0_key(k->dsa, &pub_key, NULL);
++ if ((ret = sshbuf_put_bignum2(cert, p)) != 0 ||
++ (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
++ (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
++ (ret = sshbuf_put_bignum2(cert, pub_key)) != 0) {
+ goto out;
++ }
++ }
+ break;
+ # ifdef OPENSSL_HAS_ECC
+ case KEY_ECDSA_CERT:
+@@ -2513,9 +2747,15 @@ sshkey_certify_custom(struct sshkey *k,
+ break;
+ # endif /* OPENSSL_HAS_ECC */
+ case KEY_RSA_CERT:
+- if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
+- (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
++ {
++ const BIGNUM *e, *n;
++ RSA_get0_key(k->rsa, &n, &e, NULL);
++ if (n == NULL || e == NULL ||
++ (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
++ (ret = sshbuf_put_bignum2(cert, n)) != 0) {
+ goto out;
++ }
++ }
+ break;
+ #endif /* WITH_OPENSSL */
+ case KEY_ED25519_CERT:
+@@ -2702,42 +2942,67 @@ sshkey_private_serialize_opt(const struc
+ switch (key->type) {
+ #ifdef WITH_OPENSSL
+ case KEY_RSA:
+- if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
++ {
++ const BIGNUM *n, *e, *d, *iqmp, *p, *q;
++ RSA_get0_key(key->rsa, &n, &e, &d);
++ RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
++ RSA_get0_factors(key->rsa, &p, &q);
++ if ((r = sshbuf_put_bignum2(b, n)) != 0 ||
++ (r = sshbuf_put_bignum2(b, e)) != 0 ||
++ (r = sshbuf_put_bignum2(b, d)) != 0 ||
++ (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
++ (r = sshbuf_put_bignum2(b, p)) != 0 ||
++ (r = sshbuf_put_bignum2(b, q)) != 0) {
+ goto out;
++ }
++ }
+ break;
+ case KEY_RSA_CERT:
+ if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
+ r = SSH_ERR_INVALID_ARGUMENT;
+ goto out;
+ }
++ {
++ const BIGNUM *d, *iqmp, *p, *q;
++ RSA_get0_key(key->rsa, NULL, NULL, &d);
++ RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
++ RSA_get0_factors(key->rsa, &p, &q);
+ if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
++ (r = sshbuf_put_bignum2(b, d)) != 0 ||
++ (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
++ (r = sshbuf_put_bignum2(b, p)) != 0 ||
++ (r = sshbuf_put_bignum2(b, q)) != 0) {
+ goto out;
++ }
++ }
+ break;
+ case KEY_DSA:
+- if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
++ {
++ const BIGNUM *p, *q, *g, *pub_key, *priv_key;
++ DSA_get0_pqg(key->dsa, &p, &q, &g);
++ DSA_get0_key(key->dsa, &pub_key, &priv_key);
++ if ((r = sshbuf_put_bignum2(b, p)) != 0 ||
++ (r = sshbuf_put_bignum2(b, q)) != 0 ||
++ (r = sshbuf_put_bignum2(b, g)) != 0 ||
++ (r = sshbuf_put_bignum2(b, pub_key)) != 0 ||
++ (r = sshbuf_put_bignum2(b, priv_key)) != 0) {
+ goto out;
++ }
++ }
+ break;
+ case KEY_DSA_CERT:
+ if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
+ r = SSH_ERR_INVALID_ARGUMENT;
+ goto out;
+ }
++ {
++ const BIGNUM *priv_key;
++ DSA_get0_key(key->dsa, NULL, &priv_key);
+ if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
+- (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
++ (r = sshbuf_put_bignum2(b, priv_key)) != 0) {
+ goto out;
++ }
++ }
+ break;
+ # ifdef OPENSSL_HAS_ECC
+ case KEY_ECDSA:
+@@ -2851,18 +3116,61 @@ sshkey_private_deserialize(struct sshbuf
+ r = SSH_ERR_ALLOC_FAIL;
+ goto out;
+ }
+- if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
++ {
++ BIGNUM *p=NULL, *q=NULL, *g=NULL, *pub_key=NULL, *priv_key=NULL;
++ if ((p = BN_new()) == NULL ||
++ (q = BN_new()) == NULL ||
++ (g = BN_new()) == NULL ||
++ (pub_key = BN_new()) == NULL ||
++ (priv_key = BN_new()) == NULL) {
++ r = SSH_ERR_ALLOC_FAIL;
++ goto error1;
++ }
++ if (p == NULL || q == NULL || g == NULL ||
++ pub_key == NULL || priv_key == NULL ||
++ (r = sshbuf_get_bignum2(buf, p)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, q)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, g)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, pub_key)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, priv_key)) != 0) {
++ goto error1;
++ }
++ if (DSA_set0_pqg(k->dsa, p, q, g) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error1;
++ }
++ p = q = g = NULL;
++ if (DSA_set0_key(k->dsa, pub_key, priv_key) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++error1:
++ BN_free(p); BN_free(q); BN_free(g);
++ BN_free(pub_key); BN_free(priv_key);
+ goto out;
++ }
++ pub_key = priv_key = NULL;
++ }
+ break;
+ case KEY_DSA_CERT:
+- if ((r = sshkey_froms(buf, &k)) != 0 ||
++ {
++ BIGNUM *priv_key=NULL;
++ if ((priv_key = BN_new()) == NULL) {
++ r = SSH_ERR_ALLOC_FAIL;
++ goto out;
++ }
++ if (priv_key == NULL ||
++ (r = sshkey_froms(buf, &k)) != 0 ||
+ (r = sshkey_add_private(k)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
++ (r = sshbuf_get_bignum2(buf, priv_key)) != 0) {
++ BN_free(priv_key);
++ goto out;
++ }
++ if (DSA_set0_key(k->dsa, NULL, priv_key) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ BN_free(priv_key);
+ goto out;
++ }
++ priv_key = NULL;
++ }
+ break;
+ # ifdef OPENSSL_HAS_ECC
+ case KEY_ECDSA:
+@@ -2921,29 +3229,104 @@ sshkey_private_deserialize(struct sshbuf
+ r = SSH_ERR_ALLOC_FAIL;
+ goto out;
+ }
+- if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
+- (r = ssh_rsa_generate_additional_parameters(k)) != 0)
++ {
++ BIGNUM *n=NULL, *e=NULL, *d=NULL, *iqmp=NULL, *p=NULL, *q=NULL;
++ BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy for RSA_set0_crt_params */
++ if ((n = BN_new()) == NULL ||
++ (e = BN_new()) == NULL ||
++ (d = BN_new()) == NULL ||
++ (iqmp = BN_new()) == NULL ||
++ (p = BN_new()) == NULL ||
++ (q = BN_new()) == NULL ||
++ (dmp1 = BN_new()) == NULL ||
++ (dmq1 = BN_new()) == NULL) {
++ r = SSH_ERR_ALLOC_FAIL;
++ goto error2;
++ }
++ BN_clear(dmp1); BN_clear(dmq1);
++ if ((r = sshbuf_get_bignum2(buf, n)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, e)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, d)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, p)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, q)) != 0) {
++ goto error2;
++ }
++ if (RSA_set0_key(k->rsa, n, e, d) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error2;
++ }
++ n = e = d = NULL;
++ /* dmp1,dmpq1 should be non NULL to set iqmp value */
++ if (RSA_set0_crt_params(k->rsa, dmp1, dmq1, iqmp) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error2;
++ }
++ dmp1 = dmq1 = iqmp = NULL;
++ if (RSA_set0_factors(k->rsa, p, q) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ error2:
++ BN_free(n); BN_free(e); BN_free(d);
++ BN_free(iqmp);
++ BN_free(p); BN_free(q);
++ BN_free(dmp1); BN_free(dmq1);
++ goto out;
++ }
++ p = q = NULL;
++ if ((r = ssh_rsa_generate_additional_parameters(k)) != 0) {
+ goto out;
+- if (BN_num_bits(k->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
++ }
++ }
++ if (RSA_bits(k->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
+ r = SSH_ERR_KEY_LENGTH;
+ goto out;
+ }
+ break;
+ case KEY_RSA_CERT:
++ {
++ BIGNUM *d=NULL, *iqmp=NULL, *p=NULL, *q=NULL;
++ BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy for RSA_set0_crt_params */
++ if ((d = BN_new()) == NULL ||
++ (iqmp = BN_new()) == NULL ||
++ (p = BN_new()) == NULL ||
++ (q = BN_new()) == NULL ||
++ (dmp1 = BN_new()) == NULL ||
++ (dmq1 = BN_new()) == NULL) {
++ r = SSH_ERR_ALLOC_FAIL;
++ goto error3;
++ }
++ BN_clear(dmp1); BN_clear(dmq1);
+ if ((r = sshkey_froms(buf, &k)) != 0 ||
+ (r = sshkey_add_private(k)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
+- (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
+- (r = ssh_rsa_generate_additional_parameters(k)) != 0)
++ (r = sshbuf_get_bignum2(buf, d)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, p)) != 0 ||
++ (r = sshbuf_get_bignum2(buf, q)) != 0) {
++ goto error3;
++ }
++ if (RSA_set0_key(k->rsa, NULL, NULL, d) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error3;
++ }
++ /* dmp1,dmpq1 should be non NULL to set value */
++ if (RSA_set0_crt_params(k->rsa, dmp1, dmq1, iqmp) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ goto error3;
++ }
++ dmp1 = dmq1 = iqmp = NULL;
++ if (RSA_set0_factors(k->rsa, p, q) == 0) {
++ r = SSH_ERR_LIBCRYPTO_ERROR;
++ error3:
++ BN_free(d); BN_free(iqmp);
++ BN_free(p); BN_free(q);
++ BN_free(dmp1); BN_free(dmq1);
+ goto out;
+- if (BN_num_bits(k->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
++ }
++ p = q = NULL;
++ if ((r = ssh_rsa_generate_additional_parameters(k)) != 0)
++ goto out;
++ }
++ if (RSA_bits(k->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
+ r = SSH_ERR_KEY_LENGTH;
+ goto out;
+ }
+@@ -3707,7 +4090,6 @@ translate_libcrypto_error(unsigned long
+ switch (pem_reason) {
+ case EVP_R_BAD_DECRYPT:
+ return SSH_ERR_KEY_WRONG_PASSPHRASE;
+- case EVP_R_BN_DECODE_ERROR:
+ case EVP_R_DECODE_ERROR:
+ #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
+ case EVP_R_PRIVATE_KEY_DECODE_ERROR:
+@@ -3772,7 +4154,7 @@ sshkey_parse_private_pem_fileblob(struct
+ r = convert_libcrypto_error();
+ goto out;
+ }
+- if (pk->type == EVP_PKEY_RSA &&
++ if (EVP_PKEY_id(pk) == EVP_PKEY_RSA &&
+ (type == KEY_UNSPEC || type == KEY_RSA)) {
+ if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
+ r = SSH_ERR_ALLOC_FAIL;
+@@ -3787,11 +4169,11 @@ sshkey_parse_private_pem_fileblob(struct
+ r = SSH_ERR_LIBCRYPTO_ERROR;
+ goto out;
+ }
+- if (BN_num_bits(prv->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
++ if (RSA_bits(prv->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
+ r = SSH_ERR_KEY_LENGTH;
+ goto out;
+ }
+- } else if (pk->type == EVP_PKEY_DSA &&
++ } else if (EVP_PKEY_id(pk) == EVP_PKEY_DSA &&
+ (type == KEY_UNSPEC || type == KEY_DSA)) {
+ if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
+ r = SSH_ERR_ALLOC_FAIL;
+@@ -3803,7 +4185,7 @@ sshkey_parse_private_pem_fileblob(struct
+ DSA_print_fp(stderr, prv->dsa, 8);
+ #endif
+ #ifdef OPENSSL_HAS_ECC
+- } else if (pk->type == EVP_PKEY_EC &&
++ } else if (EVP_PKEY_id(pk) == EVP_PKEY_EC &&
+ (type == KEY_UNSPEC || type == KEY_ECDSA)) {
+ if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
+ r = SSH_ERR_ALLOC_FAIL;
+diff -Naur openssh-7.7p1/configure.ac openssh-7.7p1.tpg/configure.ac
+--- openssh-7.7p1/configure.ac 2018-04-02 05:38:28.000000000 +0000
++++ openssh-7.7p1.tpg/configure.ac 2018-04-12 13:00:12.179948497 +0000
+@@ -2642,7 +2642,6 @@
+ 100*) ;; # 1.0.x
+ 200*) ;; # LibreSSL
+ *)
+- AC_MSG_ERROR([OpenSSL >= 1.1.0 is not yet supported (have "$ssl_library_ver")])
+ ;;
+ esac
+ AC_MSG_RESULT([$ssl_library_ver])
diff --git a/openssh-omdv_conf.patch b/openssh-omdv_conf.patch
index b6ca3d2..a58a1a5 100644
--- a/openssh-omdv_conf.patch
+++ b/openssh-omdv_conf.patch
@@ -1,8 +1,7 @@
-diff --git a/ssh_config b/ssh_config
-index 90fb63f..460ca1f 100644
---- a/ssh_config
-+++ b/ssh_config
-@@ -48,3 +48,17 @@
+diff -Naur openssh-7.7p1/ssh_config openssh-7.7p1.tpg/ssh_config
+--- openssh-7.7p1/ssh_config 2018-04-02 07:38:28.000000000 +0200
++++ openssh-7.7p1.tpg/ssh_config 2018-04-11 22:00:54.270647404 +0200
+@@ -44,3 +44,17 @@
# VisualHostKey no
# ProxyCommand ssh -q -W %h:%p gateway.example.com
# RekeyLimit 1G 1h
@@ -20,10 +19,9 @@ index 90fb63f..460ca1f 100644
+ #SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
+ #SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
+ #SendEnv LC_IDENTIFICATION LC_ALL
-diff --git a/sshd_config b/sshd_config
-index 4eb2e02..3356ece 100644
---- a/sshd_config
-+++ b/sshd_config
+diff -Naur openssh-7.7p1/sshd_config openssh-7.7p1.tpg/sshd_config
+--- openssh-7.7p1/sshd_config 2018-04-11 21:57:09.072276000 +0200
++++ openssh-7.7p1.tpg/sshd_config 2018-04-11 22:04:34.936016889 +0200
@@ -3,7 +3,7 @@
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
@@ -33,15 +31,14 @@ index 4eb2e02..3356ece 100644
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
-@@ -15,26 +15,32 @@
+@@ -15,25 +15,31 @@
#ListenAddress 0.0.0.0
#ListenAddress ::
-#HostKey /etc/ssh/ssh_host_rsa_key
-+HostKey /etc/ssh/ssh_host_rsa_key
- #HostKey /etc/ssh/ssh_host_dsa_key
-#HostKey /etc/ssh/ssh_host_ecdsa_key
-#HostKey /etc/ssh/ssh_host_ed25519_key
++HostKey /etc/ssh/ssh_host_rsa_key
+HostKey /etc/ssh/ssh_host_ecdsa_key
+HostKey /etc/ssh/ssh_host_ed25519_key
@@ -70,7 +67,7 @@ index 4eb2e02..3356ece 100644
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
-@@ -55,10 +61,16 @@ AuthorizedKeysFile .ssh/authorized_keys
+@@ -54,10 +60,16 @@
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
@@ -87,7 +84,7 @@ index 4eb2e02..3356ece 100644
#ChallengeResponseAuthentication yes
# Kerberos options
-@@ -80,17 +92,22 @@ AuthorizedKeysFile .ssh/authorized_keys
+@@ -79,17 +91,22 @@
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
@@ -114,12 +111,12 @@ index 4eb2e02..3356ece 100644
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
-@@ -105,7 +122,7 @@ AuthorizedKeysFile .ssh/authorized_keys
+@@ -104,7 +121,7 @@
#VersionAddendum none
# no default banner path
-#Banner none
-+# Banner /etc/issue.net
++#Banner /etc/issue.net
# override default of no subsystems
Subsystem sftp /usr/libexec/sftp-server
diff --git a/openssh.spec b/openssh.spec
index b73b243..38614d8 100644
--- a/openssh.spec
+++ b/openssh.spec
@@ -25,13 +25,12 @@
Summary: OpenSSH free Secure Shell (SSH) implementation
Name: openssh
-Version: 7.5p1
+Version: 7.7p1
Release: 1
License: BSD
Group: Networking/Remote access
Url: http://www.openssh.com/
Source0: http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/%{name}-%{version}.tar.gz
-Source1: http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/%{name}-%{version}.tar.gz.asc
# ssh-copy-id taken from debian, with "usage" added
Source3: ssh-copy-id
Source4: sshd.tmpfiles
@@ -76,16 +75,17 @@ Patch14: openssh-4.7p1-audit.patch
Patch17: openssh-5.1p1-askpass-progress.patch
Patch18: openssh-4.3p2-askpass-grab-info.patch
Patch19: openssh-4.0p1-exit-deadlock.patch
-# openssl 1.1.0 patch
-# https://github.com/openssh/openssh-portable/pull/48
-Patch20: 48.patch
-# fix clash with pthreads
-Patch21: openssh-01-fix-pam-uclibc-pthreads-clash.patch
+# (tpg) http://vega.pgw.jp/~kabe/vsd/patch/openssh-7.7p1-openssl-1.1.0.patch.html
+# http://www.linuxfromscratch.org/patches/downloads/openssh/openssh-7.7p1-openssl-1.1.0-1.patch
+Patch20: openssh-7.7p1-openssl-1.1.0-1.patch
+
BuildRequires: groff-base
BuildRequires: pam-devel
-BuildRequires: tcp_wrappers-devel
BuildRequires: pkgconfig(openssl)
BuildRequires: pkgconfig(zlib)
+BuildRequires: pkgconfig(com_err)
+BuildRequires: pkgconfig(libnsl)
+BuildRequires: pkgconfig(p11-kit-1)
%if %{with skey}
BuildRequires: skey-devel
%endif
@@ -96,7 +96,7 @@ BuildRequires: krb5-devel
BuildRequires: pkgconfig(gtk+-3.0)
%endif
%if %{with ldap}
-BuildRequires: openldap-devel >= 2.0
+BuildRequires: openldap-devel >= 2.0
%endif
%if %{with audit}
BuildRequires: audit-devel
@@ -106,11 +106,11 @@ BuildRequires: pkgconfig(libedit)
BuildRequires: pkgconfig(ncurses)
%endif
BuildConflicts: libgssapi-devel
-BuildRequires: systemd-units
+BuildRequires: pkgconfig(systemd)
Requires(pre,post,preun,postun): rpm-helper > 0.24
-Requires: tcp_wrappers
Obsoletes: ssh < 7.1
Provides: ssh = 7.1
+Recommends: p11-kit
%description
Ssh (Secure Shell) is a program for logging into a remote machine and for
@@ -184,7 +184,7 @@ Summary: OpenSSH GNOME passphrase dialog
Group: Networking/Remote access
Requires: %{name} = %{EVRD}
Requires: %{name}-askpass-common
-Requires(pre): update-alternatives
+Requires(pre): chkconfig
Provides: %{name}-askpass
Provides: ssh-askpass
Provides: ssh-extras
@@ -228,10 +228,9 @@ install %{SOURCE21} .
#patch17 -p1 -b .progress
%patch18 -p1 -b .grab-info
%patch19 -p1 -b .exit-deadlock
-%if %mdvver > 201500
+%if %mdvver > 3000000
%patch20 -p1 -b .openssl110
%endif
-%patch21 -p1 -b .pth
install %{SOURCE12} %{SOURCE19} %{SOURCE20} .
@@ -266,11 +265,8 @@ autoreconf -fi
--without-zlib-version-check \
--with-maildir=/var/spool/mail \
--with-sandbox=rlimit \
-%if %mdvver > 201500
--without-ssh1 \
-%else
- --with-ssh1 \
-%endif
+ --with-default-pkcs11-provider=yes \
%if %{with krb5}
--with-kerberos5=%{_prefix} \
%endif
@@ -329,7 +325,7 @@ else
fi
echo "root" > %{buildroot}%{_sysconfdir}/ssh/denyusers
-if [[ -f ssh_config.out ]]; then
+if [ -f ssh_config.out ]; then
install -m644 ssh_config.out %{buildroot}%{_sysconfdir}/ssh/ssh_config
else
install -m644 ssh_config %{buildroot}%{_sysconfdir}/ssh/ssh_config