openssh: Update to 7.7p1
[people/pmueller/ipfire-2.x.git] / src / patches / openssh-7.7p1-openssl-1.1.0-1.patch
1 Submitted by:            Bruce Dubbs (bdubbs@linuxfromscratch.org)
2 Date:                    2018-04-07
3 Initial Package Version: 7.7p1
4 Upstream Status:         Pending (Still)
5 Origin:                  https://git.archlinux.org/svntogit/packages.git/plain/trunk/openssl-1.1.0.patch?h=packages/openssh
6 Description:             Fixes build issues with OpenSSL-1.1.0.
7
8 diff -aurp old/auth-pam.c new/auth-pam.c
9 --- old/auth-pam.c      2018-03-22 16:21:14.000000000 -1000
10 +++ new/auth-pam.c      2018-03-23 10:05:03.886621278 -1000
11 @@ -128,6 +128,10 @@ extern u_int utmp_len;
12  typedef pthread_t sp_pthread_t;
13  #else
14  typedef pid_t sp_pthread_t;
15 +# define pthread_create(a, b, c, d)    _ssh_compat_pthread_create(a, b, c, d)
16 +# define pthread_exit(a)               _ssh_compat_pthread_exit(a)
17 +# define pthread_cancel(a)             _ssh_compat_pthread_cancel(a)
18 +# define pthread_join(a, b)            _ssh_compat_pthread_join(a, b)
19  #endif
20  
21  struct pam_ctxt {
22 diff -aurp old/cipher.c new/cipher.c
23 --- old/cipher.c        2018-03-22 16:21:14.000000000 -1000
24 +++ new/cipher.c        2018-03-23 10:05:03.886621278 -1000
25 @@ -297,7 +297,10 @@ cipher_init(struct sshcipher_ctx **ccp,
26                         goto out;
27                 }
28         }
29 -       if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
30 +       /* in OpenSSL 1.1.0, EVP_CipherInit clears all previous setups;
31 +          use EVP_CipherInit_ex for augmenting */
32 +       if (EVP_CipherInit_ex(cc->evp, NULL, NULL, (u_char *)key, NULL, -1) == 0)
33 +       {
34                 ret = SSH_ERR_LIBCRYPTO_ERROR;
35                 goto out;
36         }
37 @@ -483,7 +486,7 @@ cipher_get_keyiv(struct sshcipher_ctx *c
38                    len, iv))
39                        return SSH_ERR_LIBCRYPTO_ERROR;
40         } else
41 -               memcpy(iv, cc->evp->iv, len);
42 +               memcpy(iv, EVP_CIPHER_CTX_iv(cc->evp), len);
43  #endif
44         return 0;
45  }
46 @@ -517,14 +520,19 @@ cipher_set_keyiv(struct sshcipher_ctx *c
47                     EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
48                         return SSH_ERR_LIBCRYPTO_ERROR;
49         } else
50 -               memcpy(cc->evp->iv, iv, evplen);
51 +               memcpy(EVP_CIPHER_CTX_iv(cc->evp), iv, evplen);
52  #endif
53         return 0;
54  }
55  
56  #ifdef WITH_OPENSSL
57 -#define EVP_X_STATE(evp)       (evp)->cipher_data
58 -#define EVP_X_STATE_LEN(evp)   (evp)->cipher->ctx_size
59 +# if OPENSSL_VERSION_NUMBER >= 0x10100000UL
60 +#define EVP_X_STATE(evp)       EVP_CIPHER_CTX_get_cipher_data(evp)
61 +#define EVP_X_STATE_LEN(evp)   EVP_CIPHER_impl_ctx_size(EVP_CIPHER_CTX_cipher(evp))
62 +# else
63 +#define EVP_X_STATE(evp)       (evp).cipher_data
64 +#define EVP_X_STATE_LEN(evp)   (evp).cipher->ctx_size
65 +# endif
66  #endif
67  
68  int
69 diff -aurp old/cipher.h new/cipher.h
70 --- old/cipher.h        2018-03-22 16:21:14.000000000 -1000
71 +++ new/cipher.h        2018-03-23 10:05:03.886621278 -1000
72 @@ -46,7 +46,18 @@
73  #define CIPHER_DECRYPT         0
74  
75  struct sshcipher;
76 +#if 0
77 +struct sshcipher_ctx {
78 +       int     plaintext;
79 +       int     encrypt;
80 +       EVP_CIPHER_CTX *evp;
81 +       struct chachapoly_ctx cp_ctx; /* XXX union with evp? */
82 +       struct aesctr_ctx ac_ctx; /* XXX union with evp? */
83 +       const struct sshcipher *cipher;
84 +};
85 +#else
86  struct sshcipher_ctx;
87 +#endif
88  
89  const struct sshcipher *cipher_by_name(const char *);
90  const char *cipher_warning_message(const struct sshcipher_ctx *);
91 diff -aurp old/configure new/configure
92 --- old/configure       2018-03-23 03:30:17.000000000 -1000
93 +++ new/configure       2018-03-23 10:05:03.888621444 -1000
94 @@ -13076,7 +13076,6 @@ if ac_fn_c_try_run "$LINENO"; then :
95                                 100*)   ;; # 1.0.x
96                                 200*)   ;; # LibreSSL
97                                 *)
98 -                                       as_fn_error $? "OpenSSL >= 1.1.0 is not yet supported (have \"$ssl_library_ver\")" "$LINENO" 5
99                                         ;;
100                         esac
101                         { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ssl_library_ver" >&5
102 diff -aurp old/dh.c new/dh.c
103 --- old/dh.c    2018-03-22 16:21:14.000000000 -1000
104 +++ new/dh.c    2018-03-23 10:05:03.888621444 -1000
105 @@ -211,14 +211,15 @@ choose_dh(int min, int wantbits, int max
106  /* diffie-hellman-groupN-sha1 */
107  
108  int
109 -dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
110 +dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
111  {
112         int i;
113         int n = BN_num_bits(dh_pub);
114         int bits_set = 0;
115         BIGNUM *tmp;
116 +       const BIGNUM *p;
117  
118 -       if (dh_pub->neg) {
119 +       if (BN_is_negative(dh_pub)) {
120                 logit("invalid public DH value: negative");
121                 return 0;
122         }
123 @@ -231,7 +232,8 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
124                 error("%s: BN_new failed", __func__);
125                 return 0;
126         }
127 -       if (!BN_sub(tmp, dh->p, BN_value_one()) ||
128 +       DH_get0_pqg(dh, &p, NULL, NULL);
129 +       if (!BN_sub(tmp, p, BN_value_one()) ||
130             BN_cmp(dh_pub, tmp) != -1) {                /* pub_exp > p-2 */
131                 BN_clear_free(tmp);
132                 logit("invalid public DH value: >= p-1");
133 @@ -242,14 +244,14 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
134         for (i = 0; i <= n; i++)
135                 if (BN_is_bit_set(dh_pub, i))
136                         bits_set++;
137 -       debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
138 +       debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
139  
140         /*
141          * if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
142          */
143         if (bits_set < 4) {
144                 logit("invalid public DH value (%d/%d)",
145 -                  bits_set, BN_num_bits(dh->p));
146 +                  bits_set, BN_num_bits(p));
147                 return 0;
148         }
149         return 1;
150 @@ -259,9 +261,13 @@ int
151  dh_gen_key(DH *dh, int need)
152  {
153         int pbits;
154 +       const BIGNUM *p, *pub_key;
155 +       BIGNUM *priv_key;
156  
157 -       if (need < 0 || dh->p == NULL ||
158 -           (pbits = BN_num_bits(dh->p)) <= 0 ||
159 +       DH_get0_pqg(dh, &p, NULL, NULL);
160 +
161 +       if (need < 0 || p == NULL ||
162 +           (pbits = BN_num_bits(p)) <= 0 ||
163             need > INT_MAX / 2 || 2 * need > pbits)
164                 return SSH_ERR_INVALID_ARGUMENT;
165         if (need < 256)
166 @@ -270,10 +276,13 @@ dh_gen_key(DH *dh, int need)
167          * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
168          * so double requested need here.
169          */
170 -       dh->length = MINIMUM(need * 2, pbits - 1);
171 -       if (DH_generate_key(dh) == 0 ||
172 -           !dh_pub_is_valid(dh, dh->pub_key)) {
173 -               BN_clear_free(dh->priv_key);
174 +       DH_set_length(dh, MIN(need * 2, pbits - 1));
175 +       if (DH_generate_key(dh) == 0) {
176 +               return SSH_ERR_LIBCRYPTO_ERROR;
177 +       }
178 +       DH_get0_key(dh, &pub_key, &priv_key);
179 +       if (!dh_pub_is_valid(dh, pub_key)) {
180 +               BN_clear(priv_key);
181                 return SSH_ERR_LIBCRYPTO_ERROR;
182         }
183         return 0;
184 @@ -282,16 +291,27 @@ dh_gen_key(DH *dh, int need)
185  DH *
186  dh_new_group_asc(const char *gen, const char *modulus)
187  {
188 -       DH *dh;
189 +       DH *dh = NULL;
190 +       BIGNUM *p=NULL, *g=NULL;
191  
192 -       if ((dh = DH_new()) == NULL)
193 -               return NULL;
194 -       if (BN_hex2bn(&dh->p, modulus) == 0 ||
195 -           BN_hex2bn(&dh->g, gen) == 0) {
196 -               DH_free(dh);
197 -               return NULL;
198 +       if ((dh = DH_new()) == NULL ||
199 +           (p = BN_new()) == NULL ||
200 +           (g = BN_new()) == NULL)
201 +               goto null;
202 +       if (BN_hex2bn(&p, modulus) == 0 ||
203 +           BN_hex2bn(&g, gen) == 0) {
204 +               goto null;
205         }
206 +       if (DH_set0_pqg(dh, p, NULL, g) == 0) {
207 +               goto null;
208 +       }
209 +       p = g = NULL;
210         return (dh);
211 +null:
212 +       BN_free(p);
213 +       BN_free(g);
214 +       DH_free(dh);
215 +       return NULL;
216  }
217  
218  /*
219 @@ -306,8 +326,8 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulu
220  
221         if ((dh = DH_new()) == NULL)
222                 return NULL;
223 -       dh->p = modulus;
224 -       dh->g = gen;
225 +       if (DH_set0_pqg(dh, modulus, NULL, gen) == 0)
226 +               return NULL;
227  
228         return (dh);
229  }
230 diff -aurp old/dh.h new/dh.h
231 --- old/dh.h    2018-03-22 16:21:14.000000000 -1000
232 +++ new/dh.h    2018-03-23 10:05:03.889621527 -1000
233 @@ -42,7 +42,7 @@ DH    *dh_new_group18(void);
234  DH     *dh_new_group_fallback(int);
235  
236  int     dh_gen_key(DH *, int);
237 -int     dh_pub_is_valid(DH *, BIGNUM *);
238 +int     dh_pub_is_valid(const DH *, const BIGNUM *);
239  
240  u_int   dh_estimate(int);
241  
242 diff -aurp old/digest-openssl.c new/digest-openssl.c
243 --- old/digest-openssl.c        2018-03-22 16:21:14.000000000 -1000
244 +++ new/digest-openssl.c        2018-03-23 10:05:03.889621527 -1000
245 @@ -43,7 +43,7 @@
246  
247  struct ssh_digest_ctx {
248         int alg;
249 -       EVP_MD_CTX mdctx;
250 +       EVP_MD_CTX *mdctx;
251  };
252  
253  struct ssh_digest {
254 @@ -106,20 +106,21 @@ ssh_digest_bytes(int alg)
255  size_t
256  ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
257  {
258 -       return EVP_MD_CTX_block_size(&ctx->mdctx);
259 +       return EVP_MD_CTX_block_size(ctx->mdctx);
260  }
261  
262  struct ssh_digest_ctx *
263  ssh_digest_start(int alg)
264  {
265         const struct ssh_digest *digest = ssh_digest_by_alg(alg);
266 -       struct ssh_digest_ctx *ret;
267 +       struct ssh_digest_ctx *ret = NULL;
268  
269         if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
270                 return NULL;
271         ret->alg = alg;
272 -       EVP_MD_CTX_init(&ret->mdctx);
273 -       if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
274 +       if ((ret->mdctx = EVP_MD_CTX_new()) == NULL ||
275 +           EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
276 +               EVP_MD_CTX_free(ret->mdctx);
277                 free(ret);
278                 return NULL;
279         }
280 @@ -132,7 +133,7 @@ ssh_digest_copy_state(struct ssh_digest_
281         if (from->alg != to->alg)
282                 return SSH_ERR_INVALID_ARGUMENT;
283         /* we have bcopy-style order while openssl has memcpy-style */
284 -       if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
285 +       if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
286                 return SSH_ERR_LIBCRYPTO_ERROR;
287         return 0;
288  }
289 @@ -140,7 +141,7 @@ ssh_digest_copy_state(struct ssh_digest_
290  int
291  ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
292  {
293 -       if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
294 +       if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
295                 return SSH_ERR_LIBCRYPTO_ERROR;
296         return 0;
297  }
298 @@ -161,7 +162,7 @@ ssh_digest_final(struct ssh_digest_ctx *
299                 return SSH_ERR_INVALID_ARGUMENT;
300         if (dlen < digest->digest_len) /* No truncation allowed */
301                 return SSH_ERR_INVALID_ARGUMENT;
302 -       if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
303 +       if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
304                 return SSH_ERR_LIBCRYPTO_ERROR;
305         if (l != digest->digest_len) /* sanity */
306                 return SSH_ERR_INTERNAL_ERROR;
307 @@ -172,7 +173,7 @@ void
308  ssh_digest_free(struct ssh_digest_ctx *ctx)
309  {
310         if (ctx != NULL) {
311 -               EVP_MD_CTX_cleanup(&ctx->mdctx);
312 +               EVP_MD_CTX_free(ctx->mdctx);
313                 explicit_bzero(ctx, sizeof(*ctx));
314                 free(ctx);
315         }
316 diff -aurp old/kexdhc.c new/kexdhc.c
317 --- old/kexdhc.c        2018-03-22 16:21:14.000000000 -1000
318 +++ new/kexdhc.c        2018-03-23 10:05:03.889621527 -1000
319 @@ -81,11 +81,16 @@ kexdh_client(struct ssh *ssh)
320                 goto out;
321         }
322         debug("sending SSH2_MSG_KEXDH_INIT");
323 -       if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
324 -           (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
325 -           (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
326 +       {
327 +       const BIGNUM *pub_key;
328 +       if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
329 +               goto out;
330 +       DH_get0_key(kex->dh, &pub_key, NULL);
331 +       if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
332 +           (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
333             (r = sshpkt_send(ssh)) != 0)
334                 goto out;
335 +       }
336  #ifdef DEBUG_KEXDH
337         DHparams_print_fp(stderr, kex->dh);
338         fprintf(stderr, "pub= ");
339 @@ -169,6 +174,9 @@ input_kex_dh(int type, u_int32_t seq, st
340  
341         /* calc and verify H */
342         hashlen = sizeof(hash);
343 +       {
344 +       const BIGNUM *pub_key;
345 +       DH_get0_key(kex->dh, &pub_key, NULL);
346         if ((r = kex_dh_hash(
347             kex->hash_alg,
348             kex->client_version_string,
349 @@ -176,11 +184,13 @@ input_kex_dh(int type, u_int32_t seq, st
350             sshbuf_ptr(kex->my), sshbuf_len(kex->my),
351             sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
352             server_host_key_blob, sbloblen,
353 -           kex->dh->pub_key,
354 +           pub_key,
355             dh_server_pub,
356             shared_secret,
357 -           hash, &hashlen)) != 0)
358 +           hash, &hashlen)) != 0) {
359                 goto out;
360 +       }
361 +       }
362  
363         if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
364             kex->hostkey_alg, ssh->compat)) != 0)
365 diff -aurp old/kexdhs.c new/kexdhs.c
366 --- old/kexdhs.c        2018-03-22 16:21:14.000000000 -1000
367 +++ new/kexdhs.c        2018-03-23 10:58:58.126733207 -1000
368 @@ -163,6 +163,9 @@ input_kex_dh_init(int type, u_int32_t se
369                 goto out;
370         /* calc H */
371         hashlen = sizeof(hash);
372 +       {
373 +       const BIGNUM *pub_key;
374 +       DH_get0_key(kex->dh, &pub_key, NULL);
375         if ((r = kex_dh_hash(
376             kex->hash_alg,
377             kex->client_version_string,
378 @@ -171,10 +174,12 @@ input_kex_dh_init(int type, u_int32_t se
379             sshbuf_ptr(kex->my), sshbuf_len(kex->my),
380             server_host_key_blob, sbloblen,
381             dh_client_pub,
382 -           kex->dh->pub_key,
383 +           pub_key,
384             shared_secret,
385 -           hash, &hashlen)) != 0)
386 +           hash, &hashlen)) != 0) {
387                 goto out;
388 +       }
389 +       }
390  
391         /* save session id := H */
392         if (kex->session_id == NULL) {
393 @@ -195,12 +200,17 @@ input_kex_dh_init(int type, u_int32_t se
394         /* destroy_sensitive_data(); */
395  
396         /* send server hostkey, DH pubkey 'f' and singed H */
397 +       {
398 +       const BIGNUM *pub_key;
399 +       DH_get0_key(kex->dh, &pub_key, NULL);
400         if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
401             (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
402 -           (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
403 +           (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||      /* f */
404             (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
405 -           (r = sshpkt_send(ssh)) != 0)
406 +           (r = sshpkt_send(ssh)) != 0) {
407                 goto out;
408 +       }
409 +       }
410  
411         if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
412                 r = kex_send_newkeys(ssh);
413 diff -aurp old/kexgexc.c new/kexgexc.c
414 --- old/kexgexc.c       2018-03-22 16:21:14.000000000 -1000
415 +++ new/kexgexc.c       2018-03-23 11:00:00.132866201 -1000
416 @@ -118,11 +118,17 @@ input_kex_dh_gex_group(int type, u_int32
417         p = g = NULL; /* belong to kex->dh now */
418  
419         /* generate and send 'e', client DH public key */
420 -       if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
421 -           (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
422 -           (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
423 -           (r = sshpkt_send(ssh)) != 0)
424 +       {
425 +       const BIGNUM *pub_key;
426 +       if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
427 +               goto out;
428 +       DH_get0_key(kex->dh, &pub_key, NULL);
429 +       if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
430 +           (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
431 +           (r = sshpkt_send(ssh)) != 0) {
432                 goto out;
433 +       }
434 +       }
435         debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
436  #ifdef DEBUG_KEXDH
437         DHparams_print_fp(stderr, kex->dh);
438 @@ -212,6 +218,10 @@ input_kex_dh_gex_reply(int type, u_int32
439  
440         /* calc and verify H */
441         hashlen = sizeof(hash);
442 +       {
443 +       const BIGNUM *p, *g, *pub_key;
444 +       DH_get0_pqg(kex->dh, &p, NULL, &g);
445 +       DH_get0_key(kex->dh, &pub_key, NULL);
446         if ((r = kexgex_hash(
447             kex->hash_alg,
448             kex->client_version_string,
449 @@ -220,12 +230,14 @@ input_kex_dh_gex_reply(int type, u_int32
450             sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
451             server_host_key_blob, sbloblen,
452             kex->min, kex->nbits, kex->max,
453 -           kex->dh->p, kex->dh->g,
454 -           kex->dh->pub_key,
455 +           p, g,
456 +           pub_key,
457             dh_server_pub,
458             shared_secret,
459 -           hash, &hashlen)) != 0)
460 +           hash, &hashlen)) != 0) {
461                 goto out;
462 +       }
463 +       }
464  
465         if ((r = sshkey_verify(server_host_key, signature, slen, hash,
466             hashlen, kex->hostkey_alg, ssh->compat)) != 0)
467 diff -aurp old/kexgexs.c new/kexgexs.c
468 --- old/kexgexs.c       2018-03-22 16:21:14.000000000 -1000
469 +++ new/kexgexs.c       2018-03-23 11:03:06.045049721 -1000
470 @@ -101,11 +101,16 @@ input_kex_dh_gex_request(int type, u_int
471                 goto out;
472         }
473         debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
474 +       {
475 +       const BIGNUM *p, *g;
476 +       DH_get0_pqg(kex->dh, &p, NULL, &g);
477         if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
478 -           (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
479 -           (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
480 -           (r = sshpkt_send(ssh)) != 0)
481 +           (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
482 +           (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
483 +           (r = sshpkt_send(ssh)) != 0) {
484                 goto out;
485 +       }
486 +       }
487  
488         /* Compute our exchange value in parallel with the client */
489         if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
490 @@ -191,6 +196,10 @@ input_kex_dh_gex_init(int type, u_int32_
491                 goto out;
492         /* calc H */
493         hashlen = sizeof(hash);
494 +       {
495 +       const BIGNUM *p, *g, *pub_key;
496 +       DH_get0_pqg(kex->dh, &p, NULL, &g);
497 +       DH_get0_key(kex->dh, &pub_key, NULL);
498         if ((r = kexgex_hash(
499             kex->hash_alg,
500             kex->client_version_string,
501 @@ -199,12 +208,14 @@ input_kex_dh_gex_init(int type, u_int32_
502             sshbuf_ptr(kex->my), sshbuf_len(kex->my),
503             server_host_key_blob, sbloblen,
504             kex->min, kex->nbits, kex->max,
505 -           kex->dh->p, kex->dh->g,
506 +           p, g,
507             dh_client_pub,
508 -           kex->dh->pub_key,
509 +           pub_key,
510             shared_secret,
511 -           hash, &hashlen)) != 0)
512 +           hash, &hashlen)) != 0) {
513                 goto out;
514 +       }
515 +       }
516  
517         /* save session id := H */
518         if (kex->session_id == NULL) {
519 @@ -225,12 +236,17 @@ input_kex_dh_gex_init(int type, u_int32_
520         /* destroy_sensitive_data(); */
521  
522         /* send server hostkey, DH pubkey 'f' and singed H */
523 +       {
524 +       const BIGNUM *pub_key;
525 +       DH_get0_key(kex->dh, &pub_key, NULL);
526         if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
527             (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
528 -           (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
529 +           (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
530             (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
531 -           (r = sshpkt_send(ssh)) != 0)
532 +           (r = sshpkt_send(ssh)) != 0) {
533                 goto out;
534 +       }
535 +       }
536  
537         if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
538                 r = kex_send_newkeys(ssh);
539 diff -aurp old/monitor.c new/monitor.c
540 --- old/monitor.c       2018-03-22 16:21:14.000000000 -1000
541 +++ new/monitor.c       2018-03-23 10:05:03.890621610 -1000
542 @@ -595,10 +595,12 @@ mm_answer_moduli(int sock, Buffer *m)
543                 buffer_put_char(m, 0);
544                 return (0);
545         } else {
546 +               const BIGNUM *p, *g;
547 +               DH_get0_pqg(dh, &p, NULL, &g);
548                 /* Send first bignum */
549                 buffer_put_char(m, 1);
550 -               buffer_put_bignum2(m, dh->p);
551 -               buffer_put_bignum2(m, dh->g);
552 +               buffer_put_bignum2(m, p);
553 +               buffer_put_bignum2(m, g);
554  
555                 DH_free(dh);
556         }
557 diff -aurp old/openbsd-compat/openssl-compat.c new/openbsd-compat/openssl-compat.c
558 --- old/openbsd-compat/openssl-compat.c 2018-03-22 16:21:14.000000000 -1000
559 +++ new/openbsd-compat/openssl-compat.c 2018-03-23 10:05:03.890621610 -1000
560 @@ -75,7 +75,6 @@ ssh_OpenSSL_add_all_algorithms(void)
561         /* Enable use of crypto hardware */
562         ENGINE_load_builtin_engines();
563         ENGINE_register_all_complete();
564 -       OPENSSL_config(NULL);
565  }
566  #endif
567  
568 diff -aurp old/regress/unittests/sshkey/test_file.c new/regress/unittests/sshkey/test_file.c
569 --- old/regress/unittests/sshkey/test_file.c    2018-03-22 16:21:14.000000000 -1000
570 +++ new/regress/unittests/sshkey/test_file.c    2018-03-23 10:05:03.890621610 -1000
571 @@ -60,9 +60,14 @@ sshkey_file_tests(void)
572         a = load_bignum("rsa_1.param.n");
573         b = load_bignum("rsa_1.param.p");
574         c = load_bignum("rsa_1.param.q");
575 -       ASSERT_BIGNUM_EQ(k1->rsa->n, a);
576 -       ASSERT_BIGNUM_EQ(k1->rsa->p, b);
577 -       ASSERT_BIGNUM_EQ(k1->rsa->q, c);
578 +       {
579 +       const BIGNUM *n, *p, *q;
580 +       RSA_get0_key(k1->rsa, &n, NULL, NULL);
581 +       RSA_get0_factors(k1->rsa, &p, &q);
582 +       ASSERT_BIGNUM_EQ(n, a);
583 +       ASSERT_BIGNUM_EQ(p, b);
584 +       ASSERT_BIGNUM_EQ(q, c);
585 +       }
586         BN_free(a);
587         BN_free(b);
588         BN_free(c);
589 @@ -151,9 +156,14 @@ sshkey_file_tests(void)
590         a = load_bignum("dsa_1.param.g");
591         b = load_bignum("dsa_1.param.priv");
592         c = load_bignum("dsa_1.param.pub");
593 -       ASSERT_BIGNUM_EQ(k1->dsa->g, a);
594 -       ASSERT_BIGNUM_EQ(k1->dsa->priv_key, b);
595 -       ASSERT_BIGNUM_EQ(k1->dsa->pub_key, c);
596 +       {
597 +       const BIGNUM *g, *priv_key, *pub_key;
598 +       DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
599 +       DSA_get0_key(k1->dsa, &pub_key, &priv_key);
600 +       ASSERT_BIGNUM_EQ(g, a);
601 +       ASSERT_BIGNUM_EQ(priv_key, b);
602 +       ASSERT_BIGNUM_EQ(pub_key, c);
603 +       }
604         BN_free(a);
605         BN_free(b);
606         BN_free(c);
607 diff -aurp old/regress/unittests/sshkey/test_sshkey.c new/regress/unittests/sshkey/test_sshkey.c
608 --- old/regress/unittests/sshkey/test_sshkey.c  2018-03-22 16:21:14.000000000 -1000
609 +++ new/regress/unittests/sshkey/test_sshkey.c  2018-03-23 10:05:03.890621610 -1000
610 @@ -197,9 +197,14 @@ sshkey_tests(void)
611         k1 = sshkey_new(KEY_RSA);
612         ASSERT_PTR_NE(k1, NULL);
613         ASSERT_PTR_NE(k1->rsa, NULL);
614 -       ASSERT_PTR_NE(k1->rsa->n, NULL);
615 -       ASSERT_PTR_NE(k1->rsa->e, NULL);
616 -       ASSERT_PTR_EQ(k1->rsa->p, NULL);
617 +       {
618 +       const BIGNUM *n, *e, *p;
619 +       RSA_get0_key(k1->rsa, &n, &e, NULL);
620 +       RSA_get0_factors(k1->rsa, &p, NULL);
621 +       ASSERT_PTR_NE(n, NULL);
622 +       ASSERT_PTR_NE(e, NULL);
623 +       ASSERT_PTR_EQ(p, NULL);
624 +       }
625         sshkey_free(k1);
626         TEST_DONE();
627  
628 @@ -207,8 +212,13 @@ sshkey_tests(void)
629         k1 = sshkey_new(KEY_DSA);
630         ASSERT_PTR_NE(k1, NULL);
631         ASSERT_PTR_NE(k1->dsa, NULL);
632 -       ASSERT_PTR_NE(k1->dsa->g, NULL);
633 -       ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
634 +       {
635 +       const BIGNUM *g, *priv_key;
636 +       DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
637 +       DSA_get0_key(k1->dsa, NULL, &priv_key);
638 +       ASSERT_PTR_NE(g, NULL);
639 +       ASSERT_PTR_EQ(priv_key, NULL);
640 +       }
641         sshkey_free(k1);
642         TEST_DONE();
643  
644 @@ -234,9 +244,14 @@ sshkey_tests(void)
645         k1 = sshkey_new_private(KEY_RSA);
646         ASSERT_PTR_NE(k1, NULL);
647         ASSERT_PTR_NE(k1->rsa, NULL);
648 -       ASSERT_PTR_NE(k1->rsa->n, NULL);
649 -       ASSERT_PTR_NE(k1->rsa->e, NULL);
650 -       ASSERT_PTR_NE(k1->rsa->p, NULL);
651 +       {
652 +       const BIGNUM *n, *e, *p;
653 +       RSA_get0_key(k1->rsa, &n, &e, NULL);
654 +       RSA_get0_factors(k1->rsa, &p, NULL);
655 +       ASSERT_PTR_NE(n, NULL);
656 +       ASSERT_PTR_NE(e, NULL);
657 +       ASSERT_PTR_NE(p, NULL);
658 +       }
659         ASSERT_INT_EQ(sshkey_add_private(k1), 0);
660         sshkey_free(k1);
661         TEST_DONE();
662 @@ -245,8 +260,13 @@ sshkey_tests(void)
663         k1 = sshkey_new_private(KEY_DSA);
664         ASSERT_PTR_NE(k1, NULL);
665         ASSERT_PTR_NE(k1->dsa, NULL);
666 -       ASSERT_PTR_NE(k1->dsa->g, NULL);
667 -       ASSERT_PTR_NE(k1->dsa->priv_key, NULL);
668 +       {
669 +       const BIGNUM *g, *priv_key;
670 +       DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
671 +       DSA_get0_key(k1->dsa, NULL, &priv_key);
672 +       ASSERT_PTR_NE(g, NULL);
673 +       ASSERT_PTR_NE(priv_key, NULL);
674 +       }
675         ASSERT_INT_EQ(sshkey_add_private(k1), 0);
676         sshkey_free(k1);
677         TEST_DONE();
678 @@ -285,18 +305,28 @@ sshkey_tests(void)
679         ASSERT_INT_EQ(sshkey_generate(KEY_RSA, 1024, &kr), 0);
680         ASSERT_PTR_NE(kr, NULL);
681         ASSERT_PTR_NE(kr->rsa, NULL);
682 -       ASSERT_PTR_NE(kr->rsa->n, NULL);
683 -       ASSERT_PTR_NE(kr->rsa->e, NULL);
684 -       ASSERT_PTR_NE(kr->rsa->p, NULL);
685 -       ASSERT_INT_EQ(BN_num_bits(kr->rsa->n), 1024);
686 +       {
687 +       const BIGNUM *n, *e, *p;
688 +       RSA_get0_key(kr->rsa, &n, &e, NULL);
689 +       RSA_get0_factors(kr->rsa, &p, NULL);
690 +       ASSERT_PTR_NE(n, NULL);
691 +       ASSERT_PTR_NE(e, NULL);
692 +       ASSERT_PTR_NE(p, NULL);
693 +       ASSERT_INT_EQ(BN_num_bits(n), 1024);
694 +       }
695         TEST_DONE();
696  
697         TEST_START("generate KEY_DSA");
698         ASSERT_INT_EQ(sshkey_generate(KEY_DSA, 1024, &kd), 0);
699         ASSERT_PTR_NE(kd, NULL);
700         ASSERT_PTR_NE(kd->dsa, NULL);
701 -       ASSERT_PTR_NE(kd->dsa->g, NULL);
702 -       ASSERT_PTR_NE(kd->dsa->priv_key, NULL);
703 +       {
704 +       const BIGNUM *g, *priv_key;
705 +       DSA_get0_pqg(kd->dsa, NULL, NULL, &g);
706 +       DSA_get0_key(kd->dsa, NULL, &priv_key);
707 +       ASSERT_PTR_NE(g, NULL);
708 +       ASSERT_PTR_NE(priv_key, NULL);
709 +       }
710         TEST_DONE();
711  
712  #ifdef OPENSSL_HAS_ECC
713 @@ -323,9 +353,14 @@ sshkey_tests(void)
714         ASSERT_PTR_NE(kr, k1);
715         ASSERT_INT_EQ(k1->type, KEY_RSA);
716         ASSERT_PTR_NE(k1->rsa, NULL);
717 -       ASSERT_PTR_NE(k1->rsa->n, NULL);
718 -       ASSERT_PTR_NE(k1->rsa->e, NULL);
719 -       ASSERT_PTR_EQ(k1->rsa->p, NULL);
720 +       {
721 +       const BIGNUM *n, *e, *p;
722 +       RSA_get0_key(k1->rsa, &n, &e, NULL);
723 +       RSA_get0_factors(k1->rsa, &p, NULL);
724 +       ASSERT_PTR_NE(n, NULL);
725 +       ASSERT_PTR_NE(e, NULL);
726 +       ASSERT_PTR_EQ(p, NULL);
727 +       }
728         TEST_DONE();
729  
730         TEST_START("equal KEY_RSA/demoted KEY_RSA");
731 @@ -339,8 +374,13 @@ sshkey_tests(void)
732         ASSERT_PTR_NE(kd, k1);
733         ASSERT_INT_EQ(k1->type, KEY_DSA);
734         ASSERT_PTR_NE(k1->dsa, NULL);
735 -       ASSERT_PTR_NE(k1->dsa->g, NULL);
736 -       ASSERT_PTR_EQ(k1->dsa->priv_key, NULL);
737 +       {
738 +       const BIGNUM *g, *priv_key;
739 +       DSA_get0_pqg(k1->dsa, NULL, NULL, &g);
740 +       DSA_get0_key(k1->dsa, NULL, &priv_key);
741 +       ASSERT_PTR_NE(g, NULL);
742 +       ASSERT_PTR_EQ(priv_key, NULL);
743 +       }
744         TEST_DONE();
745  
746         TEST_START("equal KEY_DSA/demoted KEY_DSA");
747 diff -aurp old/ssh-dss.c new/ssh-dss.c
748 --- old/ssh-dss.c       2018-03-22 16:21:14.000000000 -1000
749 +++ new/ssh-dss.c       2018-03-23 10:05:03.891621693 -1000
750 @@ -53,6 +53,7 @@ ssh_dss_sign(const struct sshkey *key, u
751         DSA_SIG *sig = NULL;
752         u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
753         size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
754 +       const BIGNUM *r, *s;
755         struct sshbuf *b = NULL;
756         int ret = SSH_ERR_INVALID_ARGUMENT;
757  
758 @@ -76,15 +77,16 @@ ssh_dss_sign(const struct sshkey *key, u
759                 goto out;
760         }
761  
762 -       rlen = BN_num_bytes(sig->r);
763 -       slen = BN_num_bytes(sig->s);
764 +       DSA_SIG_get0(sig, &r, &s);
765 +       rlen = BN_num_bytes(r);
766 +       slen = BN_num_bytes(s);
767         if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
768                 ret = SSH_ERR_INTERNAL_ERROR;
769                 goto out;
770         }
771         explicit_bzero(sigblob, SIGBLOB_LEN);
772 -       BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
773 -       BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
774 +       BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
775 +       BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
776  
777         if ((b = sshbuf_new()) == NULL) {
778                 ret = SSH_ERR_ALLOC_FAIL;
779 @@ -154,17 +156,26 @@ ssh_dss_verify(const struct sshkey *key,
780         }
781  
782         /* parse signature */
783 +       {
784 +       BIGNUM *r=NULL, *s=NULL;
785         if ((sig = DSA_SIG_new()) == NULL ||
786 -           (sig->r = BN_new()) == NULL ||
787 -           (sig->s = BN_new()) == NULL) {
788 +           (r = BN_new()) == NULL ||
789 +           (s = BN_new()) == NULL) {
790                 ret = SSH_ERR_ALLOC_FAIL;
791 +               BN_free(r);
792 +               BN_free(s);
793                 goto out;
794         }
795 -       if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
796 -           (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
797 +       if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
798 +           (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL)) {
799                 ret = SSH_ERR_LIBCRYPTO_ERROR;
800 +               BN_free(r);
801 +               BN_free(s);
802                 goto out;
803         }
804 +       DSA_SIG_set0(sig, r, s);
805 +       r = s = NULL;
806 +       }
807  
808         /* sha1 the data */
809         if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
810 diff -aurp old/ssh-ecdsa.c new/ssh-ecdsa.c
811 --- old/ssh-ecdsa.c     2018-03-22 16:21:14.000000000 -1000
812 +++ new/ssh-ecdsa.c     2018-03-23 10:05:03.891621693 -1000
813 @@ -80,9 +80,14 @@ ssh_ecdsa_sign(const struct sshkey *key,
814                 ret = SSH_ERR_ALLOC_FAIL;
815                 goto out;
816         }
817 -       if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
818 -           (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
819 +       {
820 +       const BIGNUM *r, *s;
821 +       ECDSA_SIG_get0(sig, &r, &s);
822 +       if ((ret = sshbuf_put_bignum2(bb, r)) != 0 ||
823 +           (ret = sshbuf_put_bignum2(bb, s)) != 0) {
824                 goto out;
825 +       }
826 +       }
827         if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
828             (ret = sshbuf_put_stringb(b, bb)) != 0)
829                 goto out;
830 @@ -150,11 +155,27 @@ ssh_ecdsa_verify(const struct sshkey *ke
831                 ret = SSH_ERR_ALLOC_FAIL;
832                 goto out;
833         }
834 -       if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
835 -           sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
836 +       {
837 +       BIGNUM *r=NULL, *s=NULL;
838 +       if ((r = BN_new()) == NULL ||
839 +           (s = BN_new()) == NULL) {
840 +               ret = SSH_ERR_ALLOC_FAIL;
841 +               goto out_rs;
842 +       }
843 +       if (sshbuf_get_bignum2(sigbuf, r) != 0 ||
844 +           sshbuf_get_bignum2(sigbuf, s) != 0) {
845                 ret = SSH_ERR_INVALID_FORMAT;
846 +               goto out_rs;
847 +       }
848 +       if (ECDSA_SIG_set0(sig, r, s) == 0) {
849 +               ret = SSH_ERR_LIBCRYPTO_ERROR;
850 +out_rs:
851 +               BN_free(r);
852 +               BN_free(s);
853                 goto out;
854         }
855 +       r = s = NULL;
856 +       }
857         if (sshbuf_len(sigbuf) != 0) {
858                 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
859                 goto out;
860 diff -aurp old/ssh-keygen.c new/ssh-keygen.c
861 --- old/ssh-keygen.c    2018-03-22 16:21:14.000000000 -1000
862 +++ new/ssh-keygen.c    2018-03-23 10:05:03.891621693 -1000
863 @@ -493,11 +493,33 @@ do_convert_private_ssh2_from_blob(u_char
864  
865         switch (key->type) {
866         case KEY_DSA:
867 -               buffer_get_bignum_bits(b, key->dsa->p);
868 -               buffer_get_bignum_bits(b, key->dsa->g);
869 -               buffer_get_bignum_bits(b, key->dsa->q);
870 -               buffer_get_bignum_bits(b, key->dsa->pub_key);
871 -               buffer_get_bignum_bits(b, key->dsa->priv_key);
872 +               {
873 +               BIGNUM *p=NULL, *g=NULL, *q=NULL, *pub_key=NULL, *priv_key=NULL;
874 +               if ((p=BN_new()) == NULL ||
875 +                   (g=BN_new()) == NULL ||
876 +                   (q=BN_new()) == NULL ||
877 +                   (pub_key=BN_new()) == NULL ||
878 +                   (priv_key=BN_new()) == NULL) {
879 +                       BN_free(p);
880 +                       BN_free(g);
881 +                       BN_free(q);
882 +                       BN_free(pub_key);
883 +                       BN_free(priv_key);
884 +                       return NULL;
885 +               }
886 +               buffer_get_bignum_bits(b, p);
887 +               buffer_get_bignum_bits(b, g);
888 +               buffer_get_bignum_bits(b, q);
889 +               buffer_get_bignum_bits(b, pub_key);
890 +               buffer_get_bignum_bits(b, priv_key);
891 +               if (DSA_set0_pqg(key->dsa, p, q, g) == 0 ||
892 +                   DSA_set0_key(key->dsa, pub_key, priv_key) == 0) {
893 +                       fatal("failed to set DSA key");
894 +                       BN_free(p); BN_free(g); BN_free(q);
895 +                       BN_free(pub_key); BN_free(priv_key);
896 +                       return NULL;
897 +               }
898 +               }
899                 break;
900         case KEY_RSA:
901                 if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
902 @@ -514,16 +536,52 @@ do_convert_private_ssh2_from_blob(u_char
903                         e += e3;
904                         debug("e %lx", e);
905                 }
906 -               if (!BN_set_word(key->rsa->e, e)) {
907 +               {
908 +               BIGNUM *rsa_e = NULL;
909 +               BIGNUM *d=NULL, *n=NULL, *iqmp=NULL, *q=NULL, *p=NULL;
910 +               BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy input to set in RSA_set0_crt_params */
911 +               rsa_e = BN_new();
912 +               if (!rsa_e || !BN_set_word(rsa_e, e)) {
913 +                       if (rsa_e) BN_free(rsa_e);
914                         sshbuf_free(b);
915                         sshkey_free(key);
916                         return NULL;
917                 }
918 -               buffer_get_bignum_bits(b, key->rsa->d);
919 -               buffer_get_bignum_bits(b, key->rsa->n);
920 -               buffer_get_bignum_bits(b, key->rsa->iqmp);
921 -               buffer_get_bignum_bits(b, key->rsa->q);
922 -               buffer_get_bignum_bits(b, key->rsa->p);
923 +               if ((d=BN_new()) == NULL ||
924 +                   (n=BN_new()) == NULL ||
925 +                   (iqmp=BN_new()) == NULL ||
926 +                   (q=BN_new()) == NULL ||
927 +                   (p=BN_new()) == NULL ||
928 +                   (dmp1=BN_new()) == NULL ||
929 +                   (dmq1=BN_new()) == NULL) {
930 +                       BN_free(d); BN_free(n); BN_free(iqmp);
931 +                       BN_free(q); BN_free(p);
932 +                       BN_free(dmp1); BN_free(dmq1);
933 +                       return NULL;
934 +               }
935 +               BN_clear(dmp1); BN_clear(dmq1);
936 +               buffer_get_bignum_bits(b, d);
937 +               buffer_get_bignum_bits(b, n);
938 +               buffer_get_bignum_bits(b, iqmp);
939 +               buffer_get_bignum_bits(b, q);
940 +               buffer_get_bignum_bits(b, p);
941 +               if (RSA_set0_key(key->rsa, n, rsa_e, d) == 0)
942 +                       goto null;
943 +               n = d = NULL;
944 +               if (RSA_set0_factors(key->rsa, p, q) == 0)
945 +                       goto null;
946 +               p = q = NULL;
947 +               /* dmp1, dmq1 should not be NULL for initial set0 */
948 +               if (RSA_set0_crt_params(key->rsa, dmp1, dmq1, iqmp) == 0) {
949 + null:
950 +                       fatal("Failed to set RSA parameters");
951 +                       BN_free(d); BN_free(n); BN_free(iqmp);
952 +                       BN_free(q); BN_free(p);
953 +                       BN_free(dmp1); BN_free(dmq1);
954 +                       return NULL;
955 +               }
956 +               dmp1 = dmq1 = iqmp = NULL;
957 +               }
958                 if ((r = ssh_rsa_generate_additional_parameters(key)) != 0)
959                         fatal("generate RSA parameters failed: %s", ssh_err(r));
960                 break;
961 @@ -633,7 +691,7 @@ do_convert_from_pkcs8(struct sshkey **k,
962                     identity_file);
963         }
964         fclose(fp);
965 -       switch (EVP_PKEY_type(pubkey->type)) {
966 +       switch (EVP_PKEY_type(EVP_PKEY_id(pubkey))) {
967         case EVP_PKEY_RSA:
968                 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
969                         fatal("sshkey_new failed");
970 @@ -657,7 +715,7 @@ do_convert_from_pkcs8(struct sshkey **k,
971  #endif
972         default:
973                 fatal("%s: unsupported pubkey type %d", __func__,
974 -                   EVP_PKEY_type(pubkey->type));
975 +                   EVP_PKEY_type(EVP_PKEY_id(pubkey)));
976         }
977         EVP_PKEY_free(pubkey);
978         return;
979 diff -aurp old/ssh-pkcs11-client.c new/ssh-pkcs11-client.c
980 --- old/ssh-pkcs11-client.c     2018-03-22 16:21:14.000000000 -1000
981 +++ new/ssh-pkcs11-client.c     2018-03-23 10:05:03.892621777 -1000
982 @@ -144,12 +144,13 @@ pkcs11_rsa_private_encrypt(int flen, con
983  static int
984  wrap_key(RSA *rsa)
985  {
986 -       static RSA_METHOD helper_rsa;
987 +       static RSA_METHOD *helper_rsa;
988  
989 -       memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
990 -       helper_rsa.name = "ssh-pkcs11-helper";
991 -       helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
992 -       RSA_set_method(rsa, &helper_rsa);
993 +       if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
994 +               return (-1); /* XXX but caller isn't checking */
995 +       RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
996 +       RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
997 +       RSA_set_method(rsa, helper_rsa);
998         return (0);
999  }
1000  
1001 diff -aurp old/ssh-pkcs11.c new/ssh-pkcs11.c
1002 --- old/ssh-pkcs11.c    2018-03-22 16:21:14.000000000 -1000
1003 +++ new/ssh-pkcs11.c    2018-03-23 10:05:03.892621777 -1000
1004 @@ -67,7 +67,7 @@ struct pkcs11_key {
1005         struct pkcs11_provider  *provider;
1006         CK_ULONG                slotidx;
1007         int                     (*orig_finish)(RSA *rsa);
1008 -       RSA_METHOD              rsa_method;
1009 +       RSA_METHOD              *rsa_method;
1010         char                    *keyid;
1011         int                     keyid_len;
1012  };
1013 @@ -326,13 +326,15 @@ pkcs11_rsa_wrap(struct pkcs11_provider *
1014                 k11->keyid = xmalloc(k11->keyid_len);
1015                 memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
1016         }
1017 -       k11->orig_finish = def->finish;
1018 -       memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
1019 -       k11->rsa_method.name = "pkcs11";
1020 -       k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
1021 -       k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
1022 -       k11->rsa_method.finish = pkcs11_rsa_finish;
1023 -       RSA_set_method(rsa, &k11->rsa_method);
1024 +       k11->orig_finish = RSA_meth_get_finish(def);
1025 +
1026 +       if ((k11->rsa_method = RSA_meth_new("pkcs11", RSA_meth_get_flags(def))) == NULL)
1027 +               return -1;
1028 +       RSA_meth_set_priv_enc(k11->rsa_method, pkcs11_rsa_private_encrypt);
1029 +       RSA_meth_set_priv_dec(k11->rsa_method, pkcs11_rsa_private_decrypt);
1030 +       RSA_meth_set_finish(k11->rsa_method, pkcs11_rsa_finish);
1031 +
1032 +       RSA_set_method(rsa, k11->rsa_method);
1033         RSA_set_app_data(rsa, k11);
1034         return (0);
1035  }
1036 @@ -512,10 +514,19 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
1037                         if ((rsa = RSA_new()) == NULL) {
1038                                 error("RSA_new failed");
1039                         } else {
1040 -                               rsa->n = BN_bin2bn(attribs[1].pValue,
1041 -                                   attribs[1].ulValueLen, NULL);
1042 -                               rsa->e = BN_bin2bn(attribs[2].pValue,
1043 -                                   attribs[2].ulValueLen, NULL);
1044 +                               BIGNUM *n=NULL, *e=NULL;
1045 +                               n = BN_new();
1046 +                               e = BN_new();
1047 +                               if (n == NULL || e == NULL)
1048 +                                       error("BN_new alloc failed");
1049 +                               if (BN_bin2bn(attribs[1].pValue,
1050 +                                     attribs[1].ulValueLen, n) == NULL ||
1051 +                                   BN_bin2bn(attribs[2].pValue,
1052 +                                     attribs[2].ulValueLen, e) == NULL)
1053 +                                       error("BN_bin2bn failed");
1054 +                               if (RSA_set0_key(rsa, n, e, NULL) == 0)
1055 +                                       error("RSA_set0_key failed");
1056 +                               n = e = NULL;
1057                         }
1058                 } else {
1059                         cp = attribs[2].pValue;
1060 @@ -525,16 +536,19 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
1061                             == NULL) {
1062                                 error("d2i_X509 failed");
1063                         } else if ((evp = X509_get_pubkey(x509)) == NULL ||
1064 -                           evp->type != EVP_PKEY_RSA ||
1065 -                           evp->pkey.rsa == NULL) {
1066 +                           EVP_PKEY_id(evp) != EVP_PKEY_RSA ||
1067 +                           EVP_PKEY_get0_RSA(evp) == NULL) {
1068                                 debug("X509_get_pubkey failed or no rsa");
1069 -                       } else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
1070 +                       } else if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp)))
1071                             == NULL) {
1072                                 error("RSAPublicKey_dup");
1073                         }
1074                         X509_free(x509);
1075                 }
1076 -               if (rsa && rsa->n && rsa->e &&
1077 +               {
1078 +               const BIGNUM *n, *e;
1079 +               RSA_get0_key(rsa, &n, &e, NULL);
1080 +               if (rsa && n && e &&
1081                     pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
1082                         if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
1083                                 fatal("sshkey_new failed");
1084 @@ -554,6 +568,7 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
1085                 } else if (rsa) {
1086                         RSA_free(rsa);
1087                 }
1088 +               }
1089                 for (i = 0; i < 3; i++)
1090                         free(attribs[i].pValue);
1091         }
1092 diff -aurp old/ssh-rsa.c new/ssh-rsa.c
1093 --- old/ssh-rsa.c       2018-03-22 16:21:14.000000000 -1000
1094 +++ new/ssh-rsa.c       2018-03-23 10:05:03.892621777 -1000
1095 @@ -84,7 +84,6 @@ ssh_rsa_generate_additional_parameters(s
1096  {
1097         BIGNUM *aux = NULL;
1098         BN_CTX *ctx = NULL;
1099 -       BIGNUM d;
1100         int r;
1101  
1102         if (key == NULL || key->rsa == NULL ||
1103 @@ -99,16 +98,27 @@ ssh_rsa_generate_additional_parameters(s
1104         }
1105         BN_set_flags(aux, BN_FLG_CONSTTIME);
1106  
1107 -       BN_init(&d);
1108 -       BN_with_flags(&d, key->rsa->d, BN_FLG_CONSTTIME);
1109 -
1110 -       if ((BN_sub(aux, key->rsa->q, BN_value_one()) == 0) ||
1111 -           (BN_mod(key->rsa->dmq1, &d, aux, ctx) == 0) ||
1112 -           (BN_sub(aux, key->rsa->p, BN_value_one()) == 0) ||
1113 -           (BN_mod(key->rsa->dmp1, &d, aux, ctx) == 0)) {
1114 +       {
1115 +       const BIGNUM *q, *d, *p;
1116 +       BIGNUM *dmq1=NULL, *dmp1=NULL;
1117 +       if ((dmq1 = BN_new()) == NULL ||
1118 +           (dmp1 = BN_new()) == NULL ) {
1119 +               r = SSH_ERR_ALLOC_FAIL;
1120 +               goto out;
1121 +       }
1122 +       RSA_get0_key(key->rsa, NULL, NULL, &d);
1123 +       RSA_get0_factors(key->rsa, &p, &q);
1124 +       if ((BN_sub(aux, q, BN_value_one()) == 0) ||
1125 +           (BN_mod(dmq1, d, aux, ctx) == 0) ||
1126 +           (BN_sub(aux, p, BN_value_one()) == 0) ||
1127 +           (BN_mod(dmp1, d, aux, ctx) == 0) ||
1128 +           RSA_set0_crt_params(key->rsa, dmp1, dmq1, NULL) == 0) {
1129                 r = SSH_ERR_LIBCRYPTO_ERROR;
1130 +               BN_clear_free(dmp1);
1131 +               BN_clear_free(dmq1);
1132                 goto out;
1133         }
1134 +       }
1135         r = 0;
1136   out:
1137         BN_clear_free(aux);
1138 @@ -139,7 +149,7 @@ ssh_rsa_sign(const struct sshkey *key, u
1139         if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
1140             sshkey_type_plain(key->type) != KEY_RSA)
1141                 return SSH_ERR_INVALID_ARGUMENT;
1142 -       if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
1143 +       if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
1144                 return SSH_ERR_KEY_LENGTH;
1145         slen = RSA_size(key->rsa);
1146         if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
1147 @@ -211,7 +221,7 @@ ssh_rsa_verify(const struct sshkey *key,
1148             sshkey_type_plain(key->type) != KEY_RSA ||
1149             sig == NULL || siglen == 0)
1150                 return SSH_ERR_INVALID_ARGUMENT;
1151 -       if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
1152 +       if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
1153                 return SSH_ERR_KEY_LENGTH;
1154  
1155         if ((b = sshbuf_from(sig, siglen)) == NULL)
1156 diff -aurp old/sshkey.c new/sshkey.c
1157 --- old/sshkey.c        2018-03-22 16:21:14.000000000 -1000
1158 +++ new/sshkey.c        2018-03-23 10:05:03.893621860 -1000
1159 @@ -274,10 +274,18 @@ sshkey_size(const struct sshkey *k)
1160  #ifdef WITH_OPENSSL
1161         case KEY_RSA:
1162         case KEY_RSA_CERT:
1163 -               return BN_num_bits(k->rsa->n);
1164 +#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
1165 +               return RSA_bits(k->rsa);
1166 +#else
1167 +               return RSA_bits(key->rsa);
1168 +#endif
1169         case KEY_DSA:
1170         case KEY_DSA_CERT:
1171 +#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
1172 +               return DSA_bits(k->dsa);
1173 +#else
1174                 return BN_num_bits(k->dsa->p);
1175 +#endif
1176         case KEY_ECDSA:
1177         case KEY_ECDSA_CERT:
1178                 return sshkey_curve_nid_to_bits(k->ecdsa_nid);
1179 @@ -482,26 +490,53 @@ sshkey_new(int type)
1180  #ifdef WITH_OPENSSL
1181         case KEY_RSA:
1182         case KEY_RSA_CERT:
1183 +               {
1184 +               BIGNUM *n=NULL, *e=NULL; /* just allocate */
1185                 if ((rsa = RSA_new()) == NULL ||
1186 -                   (rsa->n = BN_new()) == NULL ||
1187 -                   (rsa->e = BN_new()) == NULL) {
1188 +                   (n = BN_new()) == NULL ||
1189 +                   (e = BN_new()) == NULL) {
1190 +                       BN_free(n);
1191 +                       BN_free(e);
1192                         RSA_free(rsa);
1193                         free(k);
1194                         return NULL;
1195                 }
1196 +               BN_clear(n); BN_clear(e);
1197 +               if (RSA_set0_key(rsa, n, e, NULL) == 0)
1198 +                       return NULL;
1199 +               n = e = NULL;
1200 +               }
1201                 k->rsa = rsa;
1202                 break;
1203         case KEY_DSA:
1204         case KEY_DSA_CERT:
1205 +               {
1206 +               BIGNUM *p=NULL, *q=NULL, *g=NULL, *pubkey=NULL; /* just allocate */
1207                 if ((dsa = DSA_new()) == NULL ||
1208 -                   (dsa->p = BN_new()) == NULL ||
1209 -                   (dsa->q = BN_new()) == NULL ||
1210 -                   (dsa->g = BN_new()) == NULL ||
1211 -                   (dsa->pub_key = BN_new()) == NULL) {
1212 +                   (p = BN_new()) == NULL ||
1213 +                   (q = BN_new()) == NULL ||
1214 +                   (g = BN_new()) == NULL ||
1215 +                   (pubkey = BN_new()) == NULL) {
1216 +                       BN_free(p);
1217 +                       BN_free(q);
1218 +                       BN_free(g);
1219 +                       BN_free(pubkey);
1220                         DSA_free(dsa);
1221                         free(k);
1222                         return NULL;
1223                 }
1224 +               if (DSA_set0_pqg(dsa, p, q, g) == 0) {
1225 +                       BN_free(p); BN_free(q); BN_free(g);
1226 +                       BN_free(pubkey);
1227 +                       return NULL;
1228 +               }
1229 +               p = q = g = NULL;
1230 +               if (DSA_set0_key(dsa, pubkey, NULL) == 0) {
1231 +                       BN_free(pubkey);
1232 +                       return NULL;
1233 +               }
1234 +               pubkey = NULL;
1235 +               }
1236                 k->dsa = dsa;
1237                 break;
1238         case KEY_ECDSA:
1239 @@ -539,6 +574,51 @@ sshkey_add_private(struct sshkey *k)
1240  #ifdef WITH_OPENSSL
1241         case KEY_RSA:
1242         case KEY_RSA_CERT:
1243 +#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
1244 +               /* Allocate BIGNUM. This is a mess.
1245 +                  For OpenSSL 1.1.x API these shouldn't be mandatory,
1246 +                  but some regression tests for non-NULL pointer of
1247 +                  the data. */
1248 +#define new_or_dup(bn, nbn) \
1249 +               if (bn == NULL) { \
1250 +                       if ((nbn = BN_new()) == NULL) \
1251 +                               return SSH_ERR_ALLOC_FAIL; \
1252 +               } else { \
1253 +                       /* otherwise use-after-free will occur */ \
1254 +                       if ((nbn = BN_dup(bn)) == NULL) \
1255 +                               return SSH_ERR_ALLOC_FAIL; \
1256 +               }
1257 +               {
1258 +               const BIGNUM *d, *iqmp, *q, *p, *dmq1, *dmp1; /* allocate if NULL */
1259 +               BIGNUM *nd, *niqmp, *nq, *np, *ndmq1, *ndmp1;
1260 +
1261 +               RSA_get0_key(k->rsa, NULL, NULL, &d);
1262 +               RSA_get0_factors(k->rsa, &p, &q);
1263 +               RSA_get0_crt_params(k->rsa, &dmp1, &dmq1, &iqmp);
1264 +
1265 +               new_or_dup(d, nd);
1266 +               new_or_dup(iqmp, niqmp);
1267 +               new_or_dup(q, nq);
1268 +               new_or_dup(p, np);
1269 +               new_or_dup(dmq1, ndmq1);
1270 +               new_or_dup(dmp1, ndmp1);
1271 +
1272 +               if (RSA_set0_key(k->rsa, NULL, NULL, nd) == 0)
1273 +                       goto error1;
1274 +               nd = NULL;
1275 +               if (RSA_set0_factors(k->rsa, np, nq) == 0)
1276 +                       goto error1;
1277 +               np = nq = NULL;
1278 +               if (RSA_set0_crt_params(k->rsa, ndmp1, ndmq1, niqmp) == 0) {
1279 +error1:
1280 +                       BN_free(nd);
1281 +                       BN_free(np); BN_free(nq);
1282 +                       BN_free(ndmp1); BN_free(ndmq1); BN_free(niqmp);
1283 +                       return SSH_ERR_LIBCRYPTO_ERROR;
1284 +               }
1285 +               ndmp1 = ndmq1 = niqmp = NULL;
1286 +               }
1287 +#else
1288  #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
1289                 if (bn_maybe_alloc_failed(k->rsa->d) ||
1290                     bn_maybe_alloc_failed(k->rsa->iqmp) ||
1291 @@ -547,13 +627,28 @@ sshkey_add_private(struct sshkey *k)
1292                     bn_maybe_alloc_failed(k->rsa->dmq1) ||
1293                     bn_maybe_alloc_failed(k->rsa->dmp1))
1294                         return SSH_ERR_ALLOC_FAIL;
1295 +#endif
1296                 break;
1297         case KEY_DSA:
1298         case KEY_DSA_CERT:
1299 +#if OPENSSL_VERSION_NUMBER >= 0x10100000UL
1300 +               {
1301 +               const BIGNUM *priv_key;
1302 +               BIGNUM *npriv_key;
1303 +               DSA_get0_key(k->dsa, NULL, &priv_key);
1304 +               new_or_dup(priv_key, npriv_key);
1305 +               if (DSA_set0_key(k->dsa, NULL, npriv_key) == 0) {
1306 +                       BN_free(npriv_key);
1307 +                       return SSH_ERR_LIBCRYPTO_ERROR;
1308 +               }
1309 +               }
1310 +#else
1311                 if (bn_maybe_alloc_failed(k->dsa->priv_key))
1312                         return SSH_ERR_ALLOC_FAIL;
1313 +#endif
1314                 break;
1315  #undef bn_maybe_alloc_failed
1316 +#undef new_or_dup
1317         case KEY_ECDSA:
1318         case KEY_ECDSA_CERT:
1319                 /* Cannot do anything until we know the group */
1320 @@ -677,16 +772,34 @@ sshkey_equal_public(const struct sshkey
1321  #ifdef WITH_OPENSSL
1322         case KEY_RSA_CERT:
1323         case KEY_RSA:
1324 -               return a->rsa != NULL && b->rsa != NULL &&
1325 -                   BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
1326 -                   BN_cmp(a->rsa->n, b->rsa->n) == 0;
1327 +               {
1328 +               const BIGNUM *a_e, *b_e, *a_n, *b_n;
1329 +               const BIGNUM *a_d, *b_d;
1330 +               if (a->rsa == NULL) return 0;
1331 +               if (b->rsa == NULL) return 0;
1332 +               RSA_get0_key(a->rsa, &a_n, &a_e, &a_d);
1333 +               RSA_get0_key(b->rsa, &b_n, &b_e, &b_d);
1334 +               return 
1335 +                   BN_cmp(a_e, b_e) == 0 &&
1336 +                   BN_cmp(a_n, b_n) == 0;
1337 +               }
1338         case KEY_DSA_CERT:
1339         case KEY_DSA:
1340 -               return a->dsa != NULL && b->dsa != NULL &&
1341 -                   BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
1342 -                   BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
1343 -                   BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
1344 -                   BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
1345 +               {
1346 +               const BIGNUM *a_p, *a_q, *a_g, *a_pub_key;
1347 +               const BIGNUM *b_p, *b_q, *b_g, *b_pub_key;
1348 +               if (a->dsa == NULL) return 0;
1349 +               if (b->dsa == NULL) return 0;
1350 +               DSA_get0_pqg(a->dsa, &a_p, &a_q, &a_g);
1351 +               DSA_get0_pqg(b->dsa, &b_p, &b_q, &b_g);
1352 +               DSA_get0_key(a->dsa, &a_pub_key, NULL);
1353 +               DSA_get0_key(b->dsa, &b_pub_key, NULL);
1354 +               return 
1355 +                   BN_cmp(a_p, b_p) == 0 &&
1356 +                   BN_cmp(a_q, b_q) == 0 &&
1357 +                   BN_cmp(a_g, b_g) == 0 &&
1358 +                   BN_cmp(a_pub_key, b_pub_key) == 0;
1359 +               }
1360  # ifdef OPENSSL_HAS_ECC
1361         case KEY_ECDSA_CERT:
1362         case KEY_ECDSA:
1363 @@ -775,12 +888,17 @@ to_blob_buf(const struct sshkey *key, st
1364         case KEY_DSA:
1365                 if (key->dsa == NULL)
1366                         return SSH_ERR_INVALID_ARGUMENT;
1367 +               {
1368 +               const BIGNUM *p, *q, *g, *pub_key;
1369 +               DSA_get0_pqg(key->dsa, &p, &q, &g);
1370 +               DSA_get0_key(key->dsa, &pub_key, NULL);
1371                 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
1372 -                   (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
1373 -                   (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
1374 -                   (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
1375 -                   (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
1376 +                   (ret = sshbuf_put_bignum2(b, p)) != 0 ||
1377 +                   (ret = sshbuf_put_bignum2(b, q)) != 0 ||
1378 +                   (ret = sshbuf_put_bignum2(b, g)) != 0 ||
1379 +                   (ret = sshbuf_put_bignum2(b, pub_key)) != 0)
1380                         return ret;
1381 +               }
1382                 break;
1383  # ifdef OPENSSL_HAS_ECC
1384         case KEY_ECDSA:
1385 @@ -796,10 +914,14 @@ to_blob_buf(const struct sshkey *key, st
1386         case KEY_RSA:
1387                 if (key->rsa == NULL)
1388                         return SSH_ERR_INVALID_ARGUMENT;
1389 +               {
1390 +               const BIGNUM *e, *n;
1391 +               RSA_get0_key(key->rsa, &n, &e, NULL);
1392                 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
1393 -                   (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
1394 -                   (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
1395 +                   (ret = sshbuf_put_bignum2(b, e)) != 0 ||
1396 +                   (ret = sshbuf_put_bignum2(b, n)) != 0)
1397                         return ret;
1398 +               }
1399                 break;
1400  #endif /* WITH_OPENSSL */
1401         case KEY_ED25519:
1402 @@ -1740,13 +1862,32 @@ sshkey_from_private(const struct sshkey
1403         case KEY_DSA_CERT:
1404                 if ((n = sshkey_new(k->type)) == NULL)
1405                         return SSH_ERR_ALLOC_FAIL;
1406 -               if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
1407 -                   (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
1408 -                   (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
1409 -                   (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
1410 +               {
1411 +               const BIGNUM *p, *q, *g, *pub_key, *priv_key;
1412 +               BIGNUM *cp=NULL, *cq=NULL, *cg=NULL, *cpub_key=NULL;
1413 +               DSA_get0_pqg(k->dsa, &p, &q, &g);
1414 +               DSA_get0_key(k->dsa, &pub_key, &priv_key);
1415 +               if ((cp = BN_dup(p)) == NULL ||
1416 +                   (cq = BN_dup(q)) == NULL ||
1417 +                   (cg = BN_dup(g)) == NULL ||
1418 +                   (cpub_key = BN_dup(pub_key)) == NULL) {
1419 +                       BN_free(cp); BN_free(cq); BN_free(cg);
1420 +                       BN_free(cpub_key);
1421                         sshkey_free(n);
1422                         return SSH_ERR_ALLOC_FAIL;
1423                 }
1424 +               if (DSA_set0_pqg(n->dsa, cp, cq, cg) == 0)
1425 +                       goto error1;
1426 +               cp = cq = cg = NULL;
1427 +               if (DSA_set0_key(n->dsa, cpub_key, NULL) == 0) {
1428 +error1:
1429 +                       BN_free(cp); BN_free(cq); BN_free(cg);
1430 +                       BN_free(cpub_key);
1431 +                       sshkey_free(n);
1432 +                       return SSH_ERR_LIBCRYPTO_ERROR;
1433 +               }
1434 +               cpub_key = NULL;
1435 +               }
1436                 break;
1437  # ifdef OPENSSL_HAS_ECC
1438         case KEY_ECDSA:
1439 @@ -1770,11 +1911,23 @@ sshkey_from_private(const struct sshkey
1440         case KEY_RSA_CERT:
1441                 if ((n = sshkey_new(k->type)) == NULL)
1442                         return SSH_ERR_ALLOC_FAIL;
1443 -               if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
1444 -                   (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
1445 +               {
1446 +               const BIGNUM *nn, *e, *d;
1447 +               BIGNUM *cn=NULL, *ce=NULL;
1448 +               RSA_get0_key(k->rsa, &nn, &e, &d);
1449 +               if ((cn = BN_dup(nn)) == NULL ||
1450 +                   (ce = BN_dup(e)) == NULL ) {
1451 +                       BN_free(cn); BN_free(ce);
1452                         sshkey_free(n);
1453                         return SSH_ERR_ALLOC_FAIL;
1454                 }
1455 +               if (RSA_set0_key(n->rsa, cn, ce, NULL) == 0) {
1456 +                       BN_free(cn); BN_free(ce);
1457 +                       sshkey_free(n);
1458 +                       return SSH_ERR_LIBCRYPTO_ERROR;
1459 +               }
1460 +               cn = ce = NULL;
1461 +               }
1462                 break;
1463  #endif /* WITH_OPENSSL */
1464         case KEY_ED25519:
1465 @@ -1995,12 +2148,27 @@ sshkey_from_blob_internal(struct sshbuf
1466                         ret = SSH_ERR_ALLOC_FAIL;
1467                         goto out;
1468                 }
1469 -               if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
1470 -                   sshbuf_get_bignum2(b, key->rsa->n) != 0) {
1471 +               {
1472 +               BIGNUM *e=NULL, *n=NULL;
1473 +               if ((e = BN_new()) == NULL ||
1474 +                   (n = BN_new()) == NULL ) {
1475 +                       ret = SSH_ERR_ALLOC_FAIL;
1476 +                       BN_free(e); BN_free(n);
1477 +                       goto out;
1478 +               }
1479 +               if (sshbuf_get_bignum2(b, e) != 0 ||
1480 +                   sshbuf_get_bignum2(b, n) != 0) {
1481                         ret = SSH_ERR_INVALID_FORMAT;
1482 +                       BN_free(e); BN_free(n);
1483                         goto out;
1484                 }
1485 -               if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1486 +               if (RSA_set0_key(key->rsa, n, e, NULL) == 0) {
1487 +                       BN_free(e); BN_free(n);
1488 +                       return SSH_ERR_LIBCRYPTO_ERROR;
1489 +               }
1490 +               n = e = NULL;
1491 +               }
1492 +               if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1493                         ret = SSH_ERR_KEY_LENGTH;
1494                         goto out;
1495                 }
1496 @@ -2020,13 +2188,36 @@ sshkey_from_blob_internal(struct sshbuf
1497                         ret = SSH_ERR_ALLOC_FAIL;
1498                         goto out;
1499                 }
1500 -               if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
1501 -                   sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
1502 -                   sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
1503 -                   sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
1504 +               {
1505 +               BIGNUM *p=NULL, *q=NULL, *g=NULL, *pub_key=NULL;
1506 +               if ((p = BN_new()) == NULL ||
1507 +                   (q = BN_new()) == NULL ||
1508 +                   (g = BN_new()) == NULL ||
1509 +                   (pub_key = BN_new()) == NULL) {
1510 +                       ret = SSH_ERR_ALLOC_FAIL;
1511 +                       goto error1;
1512 +               }
1513 +               if (sshbuf_get_bignum2(b, p) != 0 ||
1514 +                   sshbuf_get_bignum2(b, q) != 0 ||
1515 +                   sshbuf_get_bignum2(b, g) != 0 ||
1516 +                   sshbuf_get_bignum2(b, pub_key) != 0) {
1517                         ret = SSH_ERR_INVALID_FORMAT;
1518 +                       goto error1;
1519 +               }
1520 +               if (DSA_set0_pqg(key->dsa, p, q, g) == 0) {
1521 +                       ret = SSH_ERR_LIBCRYPTO_ERROR;
1522 +                       goto error1;
1523 +               }
1524 +               p = q = g = NULL;
1525 +               if (DSA_set0_key(key->dsa, pub_key, NULL) == 0) {
1526 +                       ret = SSH_ERR_LIBCRYPTO_ERROR;
1527 +error1:
1528 +                       BN_free(p); BN_free(q); BN_free(g);
1529 +                       BN_free(pub_key);
1530                         goto out;
1531                 }
1532 +               pub_key = NULL;
1533 +               }
1534  #ifdef DEBUG_PK
1535                 DSA_print_fp(stderr, key->dsa, 8);
1536  #endif
1537 @@ -2327,26 +2518,63 @@ sshkey_demote(const struct sshkey *k, st
1538                         goto fail;
1539                 /* FALLTHROUGH */
1540         case KEY_RSA:
1541 -               if ((pk->rsa = RSA_new()) == NULL ||
1542 -                   (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
1543 -                   (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
1544 +               if ((pk->rsa = RSA_new()) == NULL ){
1545                         ret = SSH_ERR_ALLOC_FAIL;
1546                         goto fail;
1547                         }
1548 +               {
1549 +               const BIGNUM *ke, *kn;
1550 +               BIGNUM *pke=NULL, *pkn=NULL;
1551 +               RSA_get0_key(k->rsa, &kn, &ke, NULL);
1552 +                if ((pke = BN_dup(ke)) == NULL ||
1553 +                    (pkn = BN_dup(kn)) == NULL) {
1554 +                       ret = SSH_ERR_ALLOC_FAIL;
1555 +                       BN_free(pke); BN_free(pkn);
1556 +                       goto fail;
1557 +                       }
1558 +               if (RSA_set0_key(pk->rsa, pkn, pke, NULL) == 0) {
1559 +                       ret = SSH_ERR_LIBCRYPTO_ERROR;
1560 +                       BN_free(pke); BN_free(pkn);
1561 +                       goto fail;
1562 +               }
1563 +               pkn = pke = NULL;
1564 +               }
1565                 break;
1566         case KEY_DSA_CERT:
1567                 if ((ret = sshkey_cert_copy(k, pk)) != 0)
1568                         goto fail;
1569                 /* FALLTHROUGH */
1570         case KEY_DSA:
1571 -               if ((pk->dsa = DSA_new()) == NULL ||
1572 -                   (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
1573 -                   (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
1574 -                   (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
1575 -                   (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
1576 +               if ((pk->dsa = DSA_new()) == NULL ) {
1577                         ret = SSH_ERR_ALLOC_FAIL;
1578                         goto fail;
1579                 }
1580 +               {
1581 +               const BIGNUM *kp, *kq, *kg, *kpub_key;
1582 +               BIGNUM *pkp=NULL, *pkq=NULL, *pkg=NULL, *pkpub_key=NULL;
1583 +               DSA_get0_pqg(k->dsa, &kp, &kq, &kg);
1584 +               DSA_get0_key(k->dsa, &kpub_key, NULL);
1585 +               if ((pkp = BN_dup(kp)) == NULL ||
1586 +                   (pkq = BN_dup(kq)) == NULL ||
1587 +                   (pkg = BN_dup(kg)) == NULL ||
1588 +                   (pkpub_key = BN_dup(kpub_key)) == NULL) {
1589 +                       ret = SSH_ERR_ALLOC_FAIL;
1590 +                       goto error1;
1591 +               }
1592 +               if (DSA_set0_pqg(pk->dsa, pkp, pkq, pkg) == 0) {
1593 +                       ret = SSH_ERR_LIBCRYPTO_ERROR;
1594 +                       goto error1;
1595 +               }
1596 +               pkp = pkq = pkg = NULL;
1597 +               if (DSA_set0_key(pk->dsa, pkpub_key, NULL) == 0) {
1598 +                       ret = SSH_ERR_LIBCRYPTO_ERROR;
1599 +error1:
1600 +                       BN_free(pkp); BN_free(pkq); BN_free(pkg);
1601 +                       BN_free(pkpub_key);
1602 +                       goto fail;
1603 +               }
1604 +               pkpub_key = NULL;
1605 +               }
1606                 break;
1607         case KEY_ECDSA_CERT:
1608                 if ((ret = sshkey_cert_copy(k, pk)) != 0)
1609 @@ -2496,11 +2724,17 @@ sshkey_certify_custom(struct sshkey *k,
1610         switch (k->type) {
1611  #ifdef WITH_OPENSSL
1612         case KEY_DSA_CERT:
1613 -               if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
1614 -                   (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
1615 -                   (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
1616 -                   (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
1617 +               {
1618 +               const BIGNUM *p, *q, *g, *pub_key;
1619 +               DSA_get0_pqg(k->dsa, &p, &q, &g);
1620 +               DSA_get0_key(k->dsa, &pub_key, NULL);
1621 +               if ((ret = sshbuf_put_bignum2(cert, p)) != 0 ||
1622 +                   (ret = sshbuf_put_bignum2(cert, q)) != 0 ||
1623 +                   (ret = sshbuf_put_bignum2(cert, g)) != 0 ||
1624 +                   (ret = sshbuf_put_bignum2(cert, pub_key)) != 0) {
1625                         goto out;
1626 +               }
1627 +               }
1628                 break;
1629  # ifdef OPENSSL_HAS_ECC
1630         case KEY_ECDSA_CERT:
1631 @@ -2513,9 +2747,15 @@ sshkey_certify_custom(struct sshkey *k,
1632                 break;
1633  # endif /* OPENSSL_HAS_ECC */
1634         case KEY_RSA_CERT:
1635 -               if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
1636 -                   (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
1637 +               {
1638 +               const BIGNUM *e, *n;
1639 +               RSA_get0_key(k->rsa, &n, &e, NULL);
1640 +               if (n == NULL || e == NULL ||
1641 +                   (ret = sshbuf_put_bignum2(cert, e)) != 0 ||
1642 +                   (ret = sshbuf_put_bignum2(cert, n)) != 0) {
1643                         goto out;
1644 +               }
1645 +               }
1646                 break;
1647  #endif /* WITH_OPENSSL */
1648         case KEY_ED25519_CERT:
1649 @@ -2702,42 +2942,67 @@ sshkey_private_serialize_opt(const struc
1650         switch (key->type) {
1651  #ifdef WITH_OPENSSL
1652         case KEY_RSA:
1653 -               if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
1654 -                   (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
1655 -                   (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
1656 -                   (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
1657 -                   (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
1658 -                   (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
1659 +               {
1660 +               const BIGNUM *n, *e, *d, *iqmp, *p, *q;
1661 +               RSA_get0_key(key->rsa, &n, &e, &d);
1662 +               RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
1663 +               RSA_get0_factors(key->rsa, &p, &q);
1664 +               if ((r = sshbuf_put_bignum2(b, n)) != 0 ||
1665 +                   (r = sshbuf_put_bignum2(b, e)) != 0 ||
1666 +                   (r = sshbuf_put_bignum2(b, d)) != 0 ||
1667 +                   (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
1668 +                   (r = sshbuf_put_bignum2(b, p)) != 0 ||
1669 +                   (r = sshbuf_put_bignum2(b, q)) != 0) {
1670                         goto out;
1671 +               }
1672 +               }
1673                 break;
1674         case KEY_RSA_CERT:
1675                 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
1676                         r = SSH_ERR_INVALID_ARGUMENT;
1677                         goto out;
1678                 }
1679 +               {
1680 +               const BIGNUM *d, *iqmp, *p, *q;
1681 +               RSA_get0_key(key->rsa, NULL, NULL, &d);
1682 +               RSA_get0_crt_params(key->rsa, NULL, NULL, &iqmp);
1683 +               RSA_get0_factors(key->rsa, &p, &q);
1684                 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
1685 -                   (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
1686 -                   (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
1687 -                   (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
1688 -                   (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
1689 +                   (r = sshbuf_put_bignum2(b, d)) != 0 ||
1690 +                   (r = sshbuf_put_bignum2(b, iqmp)) != 0 ||
1691 +                   (r = sshbuf_put_bignum2(b, p)) != 0 ||
1692 +                   (r = sshbuf_put_bignum2(b, q)) != 0) {
1693                         goto out;
1694 +               }
1695 +               }
1696                 break;
1697         case KEY_DSA:
1698 -               if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
1699 -                   (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
1700 -                   (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
1701 -                   (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
1702 -                   (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
1703 +               {
1704 +               const BIGNUM *p, *q, *g, *pub_key, *priv_key;
1705 +               DSA_get0_pqg(key->dsa, &p, &q, &g);
1706 +               DSA_get0_key(key->dsa, &pub_key, &priv_key);
1707 +               if ((r = sshbuf_put_bignum2(b, p)) != 0 ||
1708 +                   (r = sshbuf_put_bignum2(b, q)) != 0 ||
1709 +                   (r = sshbuf_put_bignum2(b, g)) != 0 ||
1710 +                   (r = sshbuf_put_bignum2(b, pub_key)) != 0 ||
1711 +                   (r = sshbuf_put_bignum2(b, priv_key)) != 0) {
1712                         goto out;
1713 +               }
1714 +               }
1715                 break;
1716         case KEY_DSA_CERT:
1717                 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
1718                         r = SSH_ERR_INVALID_ARGUMENT;
1719                         goto out;
1720                 }
1721 +               {
1722 +               const BIGNUM *priv_key;
1723 +               DSA_get0_key(key->dsa, NULL, &priv_key);
1724                 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
1725 -                   (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
1726 +                   (r = sshbuf_put_bignum2(b, priv_key)) != 0) {
1727                         goto out;
1728 +               }
1729 +               }
1730                 break;
1731  # ifdef OPENSSL_HAS_ECC
1732         case KEY_ECDSA:
1733 @@ -2851,18 +3116,61 @@ sshkey_private_deserialize(struct sshbuf
1734                         r = SSH_ERR_ALLOC_FAIL;
1735                         goto out;
1736                 }
1737 -               if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
1738 -                   (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
1739 -                   (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
1740 -                   (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
1741 -                   (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
1742 +               {
1743 +               BIGNUM *p=NULL, *q=NULL, *g=NULL, *pub_key=NULL, *priv_key=NULL;
1744 +               if ((p = BN_new()) == NULL ||
1745 +                   (q = BN_new()) == NULL ||
1746 +                   (g = BN_new()) == NULL ||
1747 +                   (pub_key = BN_new()) == NULL ||
1748 +                   (priv_key = BN_new()) == NULL) {
1749 +                       r = SSH_ERR_ALLOC_FAIL;
1750 +                       goto error1;
1751 +               }
1752 +               if (p == NULL || q == NULL || g == NULL ||
1753 +                   pub_key == NULL || priv_key == NULL ||
1754 +                   (r = sshbuf_get_bignum2(buf, p)) != 0 ||
1755 +                   (r = sshbuf_get_bignum2(buf, q)) != 0 ||
1756 +                   (r = sshbuf_get_bignum2(buf, g)) != 0 ||
1757 +                   (r = sshbuf_get_bignum2(buf, pub_key)) != 0 ||
1758 +                   (r = sshbuf_get_bignum2(buf, priv_key)) != 0) {
1759 +                       goto error1;
1760 +               }
1761 +               if (DSA_set0_pqg(k->dsa, p, q, g) == 0) {
1762 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1763 +                       goto error1;
1764 +               }
1765 +               p = q = g = NULL;
1766 +               if (DSA_set0_key(k->dsa, pub_key, priv_key) == 0) {
1767 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1768 +error1:
1769 +                       BN_free(p); BN_free(q); BN_free(g);
1770 +                       BN_free(pub_key); BN_free(priv_key);
1771                         goto out;
1772 +               }
1773 +               pub_key = priv_key = NULL;
1774 +               }
1775                 break;
1776         case KEY_DSA_CERT:
1777 -               if ((r = sshkey_froms(buf, &k)) != 0 ||
1778 +               {
1779 +               BIGNUM *priv_key=NULL;
1780 +               if ((priv_key = BN_new()) == NULL) {
1781 +                       r = SSH_ERR_ALLOC_FAIL;
1782 +                       goto out;
1783 +               }
1784 +               if (priv_key == NULL ||
1785 +                   (r = sshkey_froms(buf, &k)) != 0 ||
1786                     (r = sshkey_add_private(k)) != 0 ||
1787 -                   (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
1788 +                   (r = sshbuf_get_bignum2(buf, priv_key)) != 0) {
1789 +                       BN_free(priv_key);
1790 +                       goto out;
1791 +               }
1792 +               if (DSA_set0_key(k->dsa, NULL, priv_key) == 0) {
1793 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1794 +                       BN_free(priv_key);
1795                         goto out;
1796 +               }
1797 +               priv_key = NULL;
1798 +               }
1799                 break;
1800  # ifdef OPENSSL_HAS_ECC
1801         case KEY_ECDSA:
1802 @@ -2921,29 +3229,104 @@ sshkey_private_deserialize(struct sshbuf
1803                         r = SSH_ERR_ALLOC_FAIL;
1804                         goto out;
1805                 }
1806 -               if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
1807 -                   (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
1808 -                   (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
1809 -                   (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
1810 -                   (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
1811 -                   (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
1812 -                   (r = ssh_rsa_generate_additional_parameters(k)) != 0)
1813 +               {
1814 +               BIGNUM *n=NULL, *e=NULL, *d=NULL, *iqmp=NULL, *p=NULL, *q=NULL;
1815 +               BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy for RSA_set0_crt_params */
1816 +               if ((n = BN_new()) == NULL ||
1817 +                   (e = BN_new()) == NULL ||
1818 +                   (d = BN_new()) == NULL ||
1819 +                   (iqmp = BN_new()) == NULL ||
1820 +                   (p = BN_new()) == NULL ||
1821 +                   (q = BN_new()) == NULL ||
1822 +                   (dmp1 = BN_new()) == NULL ||
1823 +                   (dmq1 = BN_new()) == NULL) {
1824 +                       r = SSH_ERR_ALLOC_FAIL;
1825 +                       goto error2;
1826 +               }
1827 +               BN_clear(dmp1); BN_clear(dmq1);
1828 +               if ((r = sshbuf_get_bignum2(buf, n)) != 0 ||
1829 +                   (r = sshbuf_get_bignum2(buf, e)) != 0 ||
1830 +                   (r = sshbuf_get_bignum2(buf, d)) != 0 ||
1831 +                   (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
1832 +                   (r = sshbuf_get_bignum2(buf, p)) != 0 ||
1833 +                   (r = sshbuf_get_bignum2(buf, q)) != 0) {
1834 +                       goto error2;
1835 +               }
1836 +               if (RSA_set0_key(k->rsa, n, e, d) == 0) {
1837 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1838 +                       goto error2;
1839 +               }
1840 +               n = e = d = NULL;
1841 +               /* dmp1,dmpq1 should be non NULL to set iqmp value */
1842 +               if (RSA_set0_crt_params(k->rsa, dmp1, dmq1, iqmp) == 0) {
1843 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1844 +                       goto error2;
1845 +               }
1846 +               dmp1 = dmq1 = iqmp = NULL;
1847 +               if (RSA_set0_factors(k->rsa, p, q) == 0) {
1848 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1849 + error2:
1850 +                       BN_free(n); BN_free(e); BN_free(d);
1851 +                       BN_free(iqmp);
1852 +                       BN_free(p); BN_free(q);
1853 +                       BN_free(dmp1); BN_free(dmq1);
1854 +                       goto out;
1855 +               }
1856 +               p = q = NULL;
1857 +               if ((r = ssh_rsa_generate_additional_parameters(k)) != 0) {
1858                         goto out;
1859 -               if (BN_num_bits(k->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1860 +               }
1861 +               }
1862 +               if (RSA_bits(k->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1863                         r = SSH_ERR_KEY_LENGTH;
1864                         goto out;
1865                 }
1866                 break;
1867         case KEY_RSA_CERT:
1868 +               {
1869 +               BIGNUM *d=NULL, *iqmp=NULL, *p=NULL, *q=NULL;
1870 +               BIGNUM *dmp1=NULL, *dmq1=NULL; /* dummy for RSA_set0_crt_params */
1871 +               if ((d = BN_new()) == NULL ||
1872 +                   (iqmp = BN_new()) == NULL ||
1873 +                   (p = BN_new()) == NULL ||
1874 +                   (q = BN_new()) == NULL ||
1875 +                   (dmp1 = BN_new()) == NULL ||
1876 +                   (dmq1 = BN_new()) == NULL) {
1877 +                       r = SSH_ERR_ALLOC_FAIL;
1878 +                       goto error3;
1879 +               }
1880 +               BN_clear(dmp1); BN_clear(dmq1);
1881                 if ((r = sshkey_froms(buf, &k)) != 0 ||
1882                     (r = sshkey_add_private(k)) != 0 ||
1883 -                   (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
1884 -                   (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
1885 -                   (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
1886 -                   (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
1887 -                   (r = ssh_rsa_generate_additional_parameters(k)) != 0)
1888 +                   (r = sshbuf_get_bignum2(buf, d)) != 0 ||
1889 +                   (r = sshbuf_get_bignum2(buf, iqmp)) != 0 ||
1890 +                   (r = sshbuf_get_bignum2(buf, p)) != 0 ||
1891 +                   (r = sshbuf_get_bignum2(buf, q)) != 0) {
1892 +                       goto error3;
1893 +               }
1894 +               if (RSA_set0_key(k->rsa, NULL, NULL, d) == 0) {
1895 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1896 +                       goto error3;
1897 +               }
1898 +               /* dmp1,dmpq1 should be non NULL to set value */
1899 +               if (RSA_set0_crt_params(k->rsa, dmp1, dmq1, iqmp) == 0) {
1900 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1901 +                       goto error3;
1902 +               }
1903 +               dmp1 = dmq1 = iqmp = NULL;
1904 +               if (RSA_set0_factors(k->rsa, p, q) == 0) {
1905 +                       r = SSH_ERR_LIBCRYPTO_ERROR;
1906 + error3:
1907 +                       BN_free(d); BN_free(iqmp);
1908 +                       BN_free(p); BN_free(q);
1909 +                       BN_free(dmp1); BN_free(dmq1);
1910                         goto out;
1911 -               if (BN_num_bits(k->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1912 +               }
1913 +               p = q = NULL;
1914 +               if ((r = ssh_rsa_generate_additional_parameters(k)) != 0)
1915 +                       goto out;
1916 +               }
1917 +               if (RSA_bits(k->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1918                         r = SSH_ERR_KEY_LENGTH;
1919                         goto out;
1920                 }
1921 @@ -3707,7 +4090,6 @@ translate_libcrypto_error(unsigned long
1922                 switch (pem_reason) {
1923                 case EVP_R_BAD_DECRYPT:
1924                         return SSH_ERR_KEY_WRONG_PASSPHRASE;
1925 -               case EVP_R_BN_DECODE_ERROR:
1926                 case EVP_R_DECODE_ERROR:
1927  #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
1928                 case EVP_R_PRIVATE_KEY_DECODE_ERROR:
1929 @@ -3772,7 +4154,7 @@ sshkey_parse_private_pem_fileblob(struct
1930                 r = convert_libcrypto_error();
1931                 goto out;
1932         }
1933 -       if (pk->type == EVP_PKEY_RSA &&
1934 +       if (EVP_PKEY_id(pk) == EVP_PKEY_RSA &&
1935             (type == KEY_UNSPEC || type == KEY_RSA)) {
1936                 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
1937                         r = SSH_ERR_ALLOC_FAIL;
1938 @@ -3787,11 +4169,11 @@ sshkey_parse_private_pem_fileblob(struct
1939                         r = SSH_ERR_LIBCRYPTO_ERROR;
1940                         goto out;
1941                 }
1942 -               if (BN_num_bits(prv->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1943 +               if (RSA_bits(prv->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1944                         r = SSH_ERR_KEY_LENGTH;
1945                         goto out;
1946                 }
1947 -       } else if (pk->type == EVP_PKEY_DSA &&
1948 +       } else if (EVP_PKEY_id(pk) == EVP_PKEY_DSA &&
1949             (type == KEY_UNSPEC || type == KEY_DSA)) {
1950                 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
1951                         r = SSH_ERR_ALLOC_FAIL;
1952 @@ -3803,7 +4185,7 @@ sshkey_parse_private_pem_fileblob(struct
1953                 DSA_print_fp(stderr, prv->dsa, 8);
1954  #endif
1955  #ifdef OPENSSL_HAS_ECC
1956 -       } else if (pk->type == EVP_PKEY_EC &&
1957 +       } else if (EVP_PKEY_id(pk) == EVP_PKEY_EC &&
1958             (type == KEY_UNSPEC || type == KEY_ECDSA)) {
1959                 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
1960                         r = SSH_ERR_ALLOC_FAIL;