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