Debian Codegraph @

contrib/mod_sftp/kex.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp key exchange (kex)
3 * Copyright (c) 2008-2024 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_sftp.h"
26 #include "ssh2.h"
27 #include "msg.h"
28 #include "packet.h"
29 #include "session.h"
30 #include "cipher.h"
31 #include "mac.h"
32 #include "compress.h"
33 #include "kex.h"
34 #include "keys.h"
35 #include "crypto.h"
36 #include "disconnect.h"
37 #include "interop.h"
38 #include "tap.h"
39 #include "misc.h"
40
41 #if defined(PR_USE_SODIUM)
42 # include <sodium.h>
43 # define CURVE25519_SIZE 32
44 #endif /* PR_USE_SODIUM */
45
46 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
47 # define CURVE448_SIZE 56
48 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
49
50 /* Define the minimum DH group length we allow ourselves to use/prefer (unless
51 * the AllowWeakDH SFTPOption is used).
52 */
53 #define SFTP_DH_PREF_MIN_LEN 2048
54
55 /* Define the size of the buffer we use to compute H. Note that this buffer
56 * needs to be large enough to contain the KEXINIT messages from both client
57 * and server; see Issue#1694.
58 */
59 #define SFTP_KEX_H_BUFLEN 8192
60
61 extern pr_response_t *resp_list, *resp_err_list;
62 extern module sftp_module;
63
64 /* For managing the kexinit process */
65 static pool *kex_pool = NULL;
66
67 static int kex_rekey_interval = 0;
68 static int kex_rekey_timeout = 0;
69 static int kex_rekey_timerno = -1;
70 static int kex_rekey_timeout_timerno = -1;
71
72 struct sftp_kex_names {
73 const char *kex_algo;
74 const char *server_hostkey_algo;
75 const char *c2s_encrypt_algo;
76 const char *s2c_encrypt_algo;
77 const char *c2s_mac_algo;
78 const char *s2c_mac_algo;
79 const char *c2s_comp_algo;
80 const char *s2c_comp_algo;
81 const char *c2s_lang;
82 const char *s2c_lang;
83 };
84
85 struct sftp_kex {
86 pool *pool;
87
88 /* Versions */
89 const char *client_version;
90 const char *server_version;
91
92 /* KEXINIT lists from client */
93 struct sftp_kex_names *client_names;
94
95 /* KEXINIT lists from server. */
96 struct sftp_kex_names *server_names;
97
98 /* Session algorithms */
99 struct sftp_kex_names *session_names;
100
101 /* For constructing the session ID/hash */
102 unsigned char *client_kexinit_payload;
103 size_t client_kexinit_payload_len;
104
105 unsigned char *server_kexinit_payload;
106 size_t server_kexinit_payload_len;
107
108 int first_kex_follows;
109
110 /* Client-preferred hostkey type, based on algorithm:
111 *
112 * "ssh-dss" --> SFTP_KEY_DSA
113 * "ssh-rsa" --> SFTP_KEY_RSA
114 * "ecdsa-sha2-*" --> SFTP_KEY_ECDSA_*
115 * "ssh-ed25519" --> SFTP_KEY_ED25519
116 * "ssh-ed448" --> SFTP_KEY_ED448
117 * "rsa-sha2-256" --> SFTP_KEY_RSA_SHA256
118 * "rsa-sha2-512" --> SFTP_KEY_RSA_SHA512
119 */
120 enum sftp_key_type_e use_hostkey_type;
121
122 /* Using DH group-exchange? */
123 int use_gex;
124
125 /* Using RSA key exchange? */
126 int use_kexrsa;
127
128 /* Using ECDH? */
129 int use_ecdh;
130
131 /* Using Curve25519? */
132 int use_curve25519;
133
134 /* Using Curve448? */
135 int use_curve448;
136
137 /* Using extension negotiations? */
138 int use_ext_info;
139
140 /* For generating the session ID */
141 DH *dh;
142 const BIGNUM *e;
143 const EVP_MD *hash;
144
145 const BIGNUM *k;
146 const char *h;
147 uint32_t hlen;
148
149 RSA *rsa;
150 unsigned char *rsa_encrypted;
151 uint32_t rsa_encrypted_len;
152
153 #if defined(PR_USE_OPENSSL_ECC)
154 EC_KEY *ec;
155 EC_POINT *client_point;
156 #endif /* PR_USE_OPENSSL_ECC */
157 #if defined(PR_USE_SODIUM) && defined(HAVE_SHA256_OPENSSL)
158 unsigned char *client_curve25519;
159 #endif /* PR_USE_SODIUM and HAVE_SHA256_OPENSSL */
160 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
161 unsigned char *client_curve448;
162 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
163 };
164
165 static struct sftp_kex *kex_first_kex = NULL;
166 static struct sftp_kex *kex_rekey_kex = NULL;
167 static int kex_sent_kexinit = FALSE;
168
169 /* Using strict kex? Note that we maintain this value here, rather than
170 * in the sftp_kex struct, so that any "use strict KEX" flag set via the
171 * first KEXINIT is used through any subsequent KEXINITs.
172 */
173 static int use_strict_kex = FALSE;
174 static int kex_done_first_kex = FALSE;
175
176 /* Diffie-Hellman group moduli */
177
178 static const char *dh_group1_str =
179 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74"
180 "020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437"
181 "4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
182 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF";
183
184 static const char *dh_group14_str =
185 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74"
186 "020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437"
187 "4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
188 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05"
189 "98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB"
190 "9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
191 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
192 "3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF";
193
194 static const char *dh_group16_str =
195 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
196 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
197 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
198 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
199 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
200 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
201 "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
202 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
203 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
204 "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
205 "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
206 "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
207 "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
208 "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
209 "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
210 "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
211 "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
212 "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
213 "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
214 "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
215 "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
216 "FFFFFFFFFFFFFFFF";
217
218 static const char *dh_group18_str =
219 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
220 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
221 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
222 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
223 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
224 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
225 "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
226 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
227 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
228 "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
229 "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
230 "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
231 "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
232 "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
233 "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
234 "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
235 "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
236 "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
237 "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
238 "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
239 "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
240 "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD"
241 "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831"
242 "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
243 "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF"
244 "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6"
245 "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3"
246 "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
247 "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328"
248 "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C"
249 "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE"
250 "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4"
251 "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300"
252 "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568"
253 "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
254 "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B"
255 "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A"
256 "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36"
257 "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1"
258 "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92"
259 "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47"
260 "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
261 "60C980DD98EDD3DFFFFFFFFFFFFFFFFF";
262
263
264 #define SFTP_DH_GROUP1_SHA1 1
265 #define SFTP_DH_GROUP14_SHA1 2
266 #define SFTP_DH_GEX_SHA1 3
267 #define SFTP_DH_GEX_SHA256 4
268 #define SFTP_KEXRSA_SHA1 5
269 #define SFTP_KEXRSA_SHA256 6
270 #define SFTP_ECDH_SHA256 7
271 #define SFTP_ECDH_SHA384 8
272 #define SFTP_ECDH_SHA512 9
273 #define SFTP_DH_GROUP14_SHA256 10
274 #define SFTP_DH_GROUP16_SHA512 11
275 #define SFTP_DH_GROUP18_SHA512 12
276
277 #define SFTP_KEXRSA_SHA1_SIZE 2048
278 #define SFTP_KEXRSA_SHA256_SIZE 3072
279
280 static const char *kex_client_version = NULL;
281 static const char *kex_server_version = NULL;
282 static unsigned char kex_digest_buf[EVP_MAX_MD_SIZE];
283
284 static enum sftp_key_type_e kex_used_hostkey_type = SFTP_KEY_UNKNOWN;
285
286 /* Used for access to a SFTPDHParamsFile during rekeys, even if the process
287 * has chrooted itself.
288 */
289 static FILE *kex_dhparams_fp = NULL;
290
291 /* Necessary prototypes. */
292 static struct ssh2_packet *read_kex_packet(pool *, struct sftp_kex *, int,
293 char *, unsigned int, ...);
294
295 static const char *trace_channel = "ssh2";
296
297 static int kex_rekey_timeout_cb(CALLBACK_FRAME) {
298 pr_trace_msg(trace_channel, 5,
299 "Failed to rekey before %d %s timeout, disconnecting client",
300 kex_rekey_timeout, kex_rekey_timeout != 1 ? "seconds" : "second");
301 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
302 "Failed to rekey before %d %s timeout, disconnecting client",
303 kex_rekey_timeout, kex_rekey_timeout != 1 ? "seconds" : "second");
304 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
305 return 0;
306 }
307
308 static int kex_rekey_timer_cb(CALLBACK_FRAME) {
309 pr_trace_msg(trace_channel, 17,
310 "SFTPRekey timer (%d %s) expired, requesting rekey", kex_rekey_interval,
311 kex_rekey_interval != 1 ? "secs" : "sec");
312 sftp_kex_rekey();
313 return 0;
314 }
315
316 static const unsigned char *calculate_h(struct sftp_kex *kex,
317 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
318 const BIGNUM *k, uint32_t *hlen) {
319 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
320 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
321 EVP_MD_CTX ctx;
322 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
323 EVP_MD_CTX *pctx;
324 const BIGNUM *dh_pub_key = NULL;
325 unsigned char *buf, *ptr;
326 uint32_t buflen, bufsz;
327
328 bufsz = buflen = SFTP_KEX_H_BUFLEN;
329
330 /* XXX Is this buffer large enough? Too large? */
331 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
332
333 /* Write all of the data into the buffer in the SSH2 format, and hash it. */
334
335 /* First, the version strings */
336 sftp_msg_write_string(&buf, &buflen, kex->client_version);
337 sftp_msg_write_string(&buf, &buflen, kex->server_version);
338
339 /* Client's KEXINIT */
340 sftp_msg_write_int(&buf, &buflen, kex->client_kexinit_payload_len + 1);
341 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
342 sftp_msg_write_data(&buf, &buflen, kex->client_kexinit_payload,
343 kex->client_kexinit_payload_len, FALSE);
344
345 /* Server's KEXINIT */
346 sftp_msg_write_int(&buf, &buflen, kex->server_kexinit_payload_len + 1);
347 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
348 sftp_msg_write_data(&buf, &buflen, kex->server_kexinit_payload,
349 kex->server_kexinit_payload_len, FALSE);
350
351 /* Hostkey data */
352 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
353
354 /* Client's key */
355 sftp_msg_write_mpint(&buf, &buflen, kex->e);
356
357 /* Server's key */
358 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
359 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
360 DH_get0_key(kex->dh, &dh_pub_key, NULL);
361 #else
362 dh_pub_key = kex->dh->pub_key;
363 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
364 sftp_msg_write_mpint(&buf, &buflen, dh_pub_key);
365
366 /* Shared secret */
367 sftp_msg_write_mpint(&buf, &buflen, k);
368
369 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
370 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
371 pctx = EVP_MD_CTX_new();
372 #else
373 pctx = &ctx;
374 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
375
376 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
377 * int. Without these ugly OpenSSL version preprocessor checks, the
378 * compiler will error out with "void value not ignored as it ought to be".
379 */
380
381 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
382 if (EVP_DigestInit(pctx, kex->hash) != 1) {
383 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
384 "error initializing message digest: %s", sftp_crypto_get_errors());
385 BN_clear_free((BIGNUM *) kex->e);
386 kex->e = NULL;
387 pr_memscrub(ptr, bufsz);
388 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
389 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
390 EVP_MD_CTX_free(pctx);
391 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
392 return NULL;
393 }
394 #else
395 EVP_DigestInit(pctx, kex->hash);
396 #endif
397
398 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
399 if (EVP_DigestUpdate(pctx, ptr, (bufsz - buflen)) != 1) {
400 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
401 "error updating message digest: %s", sftp_crypto_get_errors());
402 BN_clear_free((BIGNUM *) kex->e);
403 kex->e = NULL;
404 pr_memscrub(ptr, bufsz);
405 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
406 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
407 EVP_MD_CTX_free(pctx);
408 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
409 return NULL;
410 }
411 #else
412 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
413 #endif
414
415 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
416 if (EVP_DigestFinal(pctx, kex_digest_buf, hlen) != 1) {
417 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
418 "error finalizing message digest: %s", sftp_crypto_get_errors());
419 BN_clear_free((BIGNUM *) kex->e);
420 kex->e = NULL;
421 pr_memscrub(ptr, bufsz);
422 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
423 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
424 EVP_MD_CTX_free(pctx);
425 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
426 return NULL;
427 }
428 #else
429 EVP_DigestFinal(pctx, kex_digest_buf, hlen);
430 #endif
431
432 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
433 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
434 EVP_MD_CTX_free(pctx);
435 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
436
437 BN_clear_free((BIGNUM *) kex->e);
438 kex->e = NULL;
439
440 pr_memscrub(ptr, bufsz);
441 return kex_digest_buf;
442 }
443
444 static const unsigned char *calculate_gex_h(struct sftp_kex *kex,
445 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
446 const BIGNUM *k, uint32_t min, uint32_t pref, uint32_t max,
447 uint32_t *hlen) {
448 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
449 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
450 EVP_MD_CTX ctx;
451 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
452 EVP_MD_CTX *pctx;
453 const BIGNUM *dh_p = NULL, *dh_g = NULL, *dh_pub_key = NULL;
454 unsigned char *buf, *ptr;
455 uint32_t buflen, bufsz;
456
457 bufsz = buflen = SFTP_KEX_H_BUFLEN;
458
459 /* XXX Is this buffer large enough? Too large? */
460 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
461
462 /* Write all of the data into the buffer in the SSH2 format, and hash it.
463 * The ordering of these fields is described in RFC4419.
464 */
465
466 /* First, the version strings */
467 sftp_msg_write_string(&buf, &buflen, kex->client_version);
468 sftp_msg_write_string(&buf, &buflen, kex->server_version);
469
470 /* Client's KEXINIT */
471 sftp_msg_write_int(&buf, &buflen, kex->client_kexinit_payload_len + 1);
472 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
473 sftp_msg_write_data(&buf, &buflen, kex->client_kexinit_payload,
474 kex->client_kexinit_payload_len, FALSE);
475
476 /* Server's KEXINIT */
477 sftp_msg_write_int(&buf, &buflen, kex->server_kexinit_payload_len + 1);
478 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
479 sftp_msg_write_data(&buf, &buflen, kex->server_kexinit_payload,
480 kex->server_kexinit_payload_len, FALSE);
481
482 /* Hostkey data */
483 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
484
485 if (min == 0 ||
486 max == 0) {
487 sftp_msg_write_int(&buf, &buflen, pref);
488
489 } else {
490 sftp_msg_write_int(&buf, &buflen, min);
491 sftp_msg_write_int(&buf, &buflen, pref);
492 sftp_msg_write_int(&buf, &buflen, max);
493 }
494
495 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
496 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
497 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
498 #else
499 dh_p = kex->dh->p;
500 dh_g = kex->dh->g;
501 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
502 sftp_msg_write_mpint(&buf, &buflen, dh_p);
503 sftp_msg_write_mpint(&buf, &buflen, dh_g);
504
505 /* Client's key */
506 sftp_msg_write_mpint(&buf, &buflen, kex->e);
507
508 /* Server's key */
509 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
510 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
511 DH_get0_key(kex->dh, &dh_pub_key, NULL);
512 #else
513 dh_pub_key = kex->dh->pub_key;
514 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
515 sftp_msg_write_mpint(&buf, &buflen, dh_pub_key);
516
517 /* Shared secret */
518 sftp_msg_write_mpint(&buf, &buflen, k);
519
520 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
521 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
522 pctx = EVP_MD_CTX_new();
523 #else
524 pctx = &ctx;
525 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
526
527 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
528 * int. Without these ugly OpenSSL version preprocessor checks, the
529 * compiler will error out with "void value not ignored as it ought to be".
530 */
531
532 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
533 if (EVP_DigestInit(pctx, kex->hash) != 1) {
534 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
535 "error initializing message digest: %s", sftp_crypto_get_errors());
536 BN_clear_free((BIGNUM *) kex->e);
537 kex->e = NULL;
538 pr_memscrub(ptr, bufsz);
539 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
540 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
541 EVP_MD_CTX_free(pctx);
542 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
543 return NULL;
544 }
545 #else
546 EVP_DigestInit(pctx, kex->hash);
547 #endif
548
549 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
550 if (EVP_DigestUpdate(pctx, ptr, (bufsz - buflen)) != 1) {
551 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
552 "error updating message digest: %s", sftp_crypto_get_errors());
553 BN_clear_free((BIGNUM *) kex->e);
554 kex->e = NULL;
555 pr_memscrub(ptr, bufsz);
556 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
557 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
558 EVP_MD_CTX_free(pctx);
559 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
560 return NULL;
561 }
562 #else
563 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
564 #endif
565
566 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
567 if (EVP_DigestFinal(pctx, kex_digest_buf, hlen) != 1) {
568 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
569 "error finalizing message digest: %s", sftp_crypto_get_errors());
570 BN_clear_free((BIGNUM *) kex->e);
571 kex->e = NULL;
572 pr_memscrub(ptr, bufsz);
573 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
574 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
575 EVP_MD_CTX_free(pctx);
576 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
577 return NULL;
578 }
579 #else
580 EVP_DigestFinal(pctx, kex_digest_buf, hlen);
581 #endif
582
583 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
584 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
585 EVP_MD_CTX_free(pctx);
586 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
587 BN_clear_free((BIGNUM *) kex->e);
588 kex->e = NULL;
589 pr_memscrub(ptr, bufsz);
590
591 return kex_digest_buf;
592 }
593
594 static const unsigned char *calculate_kexrsa_h(struct sftp_kex *kex,
595 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
596 const BIGNUM *k, unsigned char *rsa_key, uint32_t rsa_keylen,
597 uint32_t *hlen) {
598 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
599 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
600 EVP_MD_CTX ctx;
601 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
602 EVP_MD_CTX *pctx;
603 unsigned char *buf, *ptr;
604 uint32_t buflen, bufsz;
605
606 bufsz = buflen = SFTP_KEX_H_BUFLEN;
607
608 /* XXX Is this buffer large enough? Too large? */
609 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
610
611 /* Write all of the data into the buffer in the SSH2 format, and hash it. */
612
613 /* First, the version strings */
614 sftp_msg_write_string(&buf, &buflen, kex->client_version);
615 sftp_msg_write_string(&buf, &buflen, kex->server_version);
616
617 /* Client's KEXINIT */
618 sftp_msg_write_int(&buf, &buflen, kex->client_kexinit_payload_len + 1);
619 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
620 sftp_msg_write_data(&buf, &buflen, kex->client_kexinit_payload,
621 kex->client_kexinit_payload_len, FALSE);
622
623 /* Server's KEXINIT */
624 sftp_msg_write_int(&buf, &buflen, kex->server_kexinit_payload_len + 1);
625 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
626 sftp_msg_write_data(&buf, &buflen, kex->server_kexinit_payload,
627 kex->server_kexinit_payload_len, FALSE);
628
629 /* Hostkey data */
630 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
631
632 /* Transient RSA public key */
633 sftp_msg_write_data(&buf, &buflen, rsa_key, rsa_keylen, TRUE);
634
635 /* RSA-encrypted secret */
636 sftp_msg_write_data(&buf, &buflen, kex->rsa_encrypted, kex->rsa_encrypted_len,
637 TRUE);
638
639 /* Shared secret. */
640 sftp_msg_write_mpint(&buf, &buflen, k);
641
642 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
643 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
644 pctx = EVP_MD_CTX_new();
645 #else
646 pctx = &ctx;
647 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
648
649 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
650 * int. Without these ugly OpenSSL version preprocessor checks, the
651 * compiler will error out with "void value not ignored as it ought to be".
652 */
653
654 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
655 if (EVP_DigestInit(pctx, kex->hash) != 1) {
656 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
657 "error initializing message digest: %s", sftp_crypto_get_errors());
658 pr_memscrub(ptr, bufsz);
659 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
660 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
661 EVP_MD_CTX_free(pctx);
662 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
663 return NULL;
664 }
665 #else
666 EVP_DigestInit(pctx, kex->hash);
667 #endif
668
669 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
670 if (EVP_DigestUpdate(pctx, ptr, (bufsz - buflen)) != 1) {
671 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
672 "error updating message digest: %s", sftp_crypto_get_errors());
673 pr_memscrub(ptr, bufsz);
674 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
675 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
676 EVP_MD_CTX_free(pctx);
677 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
678 return NULL;
679 }
680 #else
681 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
682 #endif
683
684 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
685 if (EVP_DigestFinal(pctx, kex_digest_buf, hlen) != 1) {
686 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
687 "error finalizing message digest: %s", sftp_crypto_get_errors());
688 pr_memscrub(ptr, bufsz);
689 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
690 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
691 EVP_MD_CTX_free(pctx);
692 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
693 return NULL;
694 }
695 #else
696 EVP_DigestFinal(pctx, kex_digest_buf, hlen);
697 #endif
698
699 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
700 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
701 EVP_MD_CTX_free(pctx);
702 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
703 pr_memscrub(ptr, bufsz);
704
705 return kex_digest_buf;
706 }
707
708 #if defined(PR_USE_OPENSSL_ECC)
709 static const unsigned char *calculate_ecdh_h(struct sftp_kex *kex,
710 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
711 const BIGNUM *k, uint32_t *hlen) {
712 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
713 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
714 EVP_MD_CTX ctx;
715 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
716 EVP_MD_CTX *pctx;
717 unsigned char *buf, *ptr;
718 uint32_t buflen, bufsz;
719
720 bufsz = buflen = SFTP_KEX_H_BUFLEN;
721
722 /* XXX Is this buffer large enough? Too large? */
723 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
724
725 /* Write all of the data into the buffer in the SSH2 format, and hash it.
726 * The ordering of these fields is described in RFC5656.
727 */
728
729 /* First, the version strings */
730 sftp_msg_write_string(&buf, &buflen, kex->client_version);
731 sftp_msg_write_string(&buf, &buflen, kex->server_version);
732
733 /* Client's KEXINIT */
734 sftp_msg_write_int(&buf, &buflen, kex->client_kexinit_payload_len + 1);
735 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
736 sftp_msg_write_data(&buf, &buflen, kex->client_kexinit_payload,
737 kex->client_kexinit_payload_len, FALSE);
738
739 /* Server's KEXINIT */
740 sftp_msg_write_int(&buf, &buflen, kex->server_kexinit_payload_len + 1);
741 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
742 sftp_msg_write_data(&buf, &buflen, kex->server_kexinit_payload,
743 kex->server_kexinit_payload_len, FALSE);
744
745 /* Hostkey data */
746 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
747
748 /* Client's key */
749 sftp_msg_write_ecpoint(&buf, &buflen, EC_KEY_get0_group(kex->ec),
750 kex->client_point);
751
752 /* Server's key */
753 sftp_msg_write_ecpoint(&buf, &buflen, EC_KEY_get0_group(kex->ec),
754 EC_KEY_get0_public_key(kex->ec));
755
756 /* Shared secret */
757 sftp_msg_write_mpint(&buf, &buflen, k);
758
759 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
760 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
761 pctx = EVP_MD_CTX_new();
762 #else
763 pctx = &ctx;
764 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
765
766 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
767 * int. Without these ugly OpenSSL version preprocessor checks, the
768 * compiler will error out with "void value not ignored as it ought to be".
769 */
770
771 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
772 if (EVP_DigestInit(pctx, kex->hash) != 1) {
773 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
774 "error initializing message digest: %s", sftp_crypto_get_errors());
775 BN_clear_free((BIGNUM *) kex->e);
776 kex->e = NULL;
777 pr_memscrub(ptr, bufsz);
778 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
779 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
780 EVP_MD_CTX_free(pctx);
781 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
782 return NULL;
783 }
784 #else
785 EVP_DigestInit(pctx, kex->hash);
786 #endif
787
788 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
789 if (EVP_DigestUpdate(pctx, ptr, (bufsz - buflen)) != 1) {
790 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
791 "error updating message digest: %s", sftp_crypto_get_errors());
792 BN_clear_free((BIGNUM *) kex->e);
793 kex->e = NULL;
794 pr_memscrub(ptr, bufsz);
795 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
796 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
797 EVP_MD_CTX_free(pctx);
798 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
799 return NULL;
800 }
801 #else
802 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
803 #endif
804
805 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
806 if (EVP_DigestFinal(pctx, kex_digest_buf, hlen) != 1) {
807 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
808 "error finalizing message digest: %s", sftp_crypto_get_errors());
809 BN_clear_free((BIGNUM *) kex->e);
810 kex->e = NULL;
811 pr_memscrub(ptr, bufsz);
812 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
813 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
814 EVP_MD_CTX_free(pctx);
815 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
816 return NULL;
817 }
818 #else
819 EVP_DigestFinal(pctx, kex_digest_buf, hlen);
820 #endif
821
822 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
823 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
824 EVP_MD_CTX_free(pctx);
825 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
826 BN_clear_free((BIGNUM *) kex->e);
827 kex->e = NULL;
828 pr_memscrub(ptr, bufsz);
829
830 return kex_digest_buf;
831 }
832 #endif /* PR_USE_OPENSSL_ECC */
833
834 /* Make sure that the DH key we're generating is good enough. */
835 static int have_good_dh(DH *dh, const BIGNUM *pub_key) {
836 register int i;
837 unsigned int nbits = 0;
838 const BIGNUM *dh_p = NULL;
839 BIGNUM *tmp;
840
841 #if OPENSSL_VERSION_NUMBER >= 0x0090801fL
842 if (BN_is_negative(pub_key)) {
843 pr_trace_msg(trace_channel, 10,
844 "DH public keys cannot have negative numbers");
845 errno = EINVAL;
846 return -1;
847 }
848 #endif /* OpenSSL-0.9.8a or later */
849
850 if (BN_cmp(pub_key, BN_value_one()) != 1) {
851 pr_trace_msg(trace_channel, 10, "bad DH public key exponent (<= 1)");
852 errno = EINVAL;
853 return -1;
854 }
855
856 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
857 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
858 DH_get0_pqg(dh, &dh_p, NULL, NULL);
859 #else
860 dh_p = dh->p;
861 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
862
863 tmp = BN_new();
864 if (!BN_sub(tmp, dh_p, BN_value_one()) ||
865 BN_cmp(pub_key, tmp) != -1) {
866 BN_clear_free(tmp);
867 pr_trace_msg(trace_channel, 10, "bad DH public key (>= p-1)");
868 errno = EINVAL;
869 return -1;
870 }
871
872 BN_clear_free(tmp);
873
874 for (i = 0; i <= BN_num_bits(pub_key); i++) {
875 if (BN_is_bit_set(pub_key, i)) {
876 nbits++;
877 }
878 }
879
880 /* The number of bits set in the public key must be greater than one.
881 * Otherwise, the public key will not hold up under scrutiny, not for
882 * our needs. (The OpenSSH client is picky about the DH public keys it
883 * will accept as well, so this is necessary to pass OpenSSH's requirements).
884 */
885 if (nbits <= 1) {
886 errno = EINVAL;
887 return -1;
888 }
889
890 pr_trace_msg(trace_channel, 10, "good DH public key: %u bits set", nbits);
891 return 0;
892 }
893
894 static int get_dh_nbits(struct sftp_kex *kex) {
895 int dh_nbits = 0, dh_size = 0, free_digest = FALSE;
896 const char *algo;
897 const EVP_CIPHER *cipher;
898 const EVP_MD *digest;
899
900 algo = kex->session_names->c2s_encrypt_algo;
901 cipher = sftp_crypto_get_cipher(algo, NULL, NULL, NULL);
902 if (cipher != NULL) {
903 int block_size, key_len;
904
905 key_len = EVP_CIPHER_key_length(cipher);
906 if (strcmp(algo, "none") == 0 &&
907 key_len < 32) {
908 key_len = 32;
909 }
910
911 if (dh_size < key_len) {
912 dh_size = key_len;
913 pr_trace_msg(trace_channel, 19,
914 "set DH size to %d bytes, matching client-to-server '%s' cipher "
915 "key length", dh_size, algo);
916 }
917
918 block_size = EVP_CIPHER_block_size(cipher);
919 if (dh_size < block_size) {
920 dh_size = block_size;
921 pr_trace_msg(trace_channel, 19,
922 "set DH size to %d bytes, matching client-to-server '%s' cipher "
923 "block size", dh_size, algo);
924 }
925 }
926
927 algo = kex->session_names->s2c_encrypt_algo;
928 cipher = sftp_crypto_get_cipher(algo, NULL, NULL, NULL);
929 if (cipher != NULL) {
930 int block_size, key_len;
931
932 key_len = EVP_CIPHER_key_length(cipher);
933 if (strcmp(algo, "none") == 0 &&
934 key_len < 32) {
935 key_len = 32;
936 }
937
938 if (dh_size < key_len) {
939 dh_size = key_len;
940 pr_trace_msg(trace_channel, 19,
941 "set DH size to %d bytes, matching server-to-client '%s' cipher "
942 "key length", dh_size, algo);
943 }
944
945 block_size = EVP_CIPHER_block_size(cipher);
946 if (dh_size < block_size) {
947 dh_size = block_size;
948 pr_trace_msg(trace_channel, 19,
949 "set DH size to %d bytes, matching server-to-client '%s' cipher "
950 "block size", dh_size, algo);
951 }
952 }
953
954 algo = kex->session_names->c2s_mac_algo;
955 digest = sftp_crypto_get_digest(algo, NULL, &free_digest);
956 if (digest != NULL) {
957 int mac_len;
958
959 mac_len = EVP_MD_size(digest);
960 if (dh_size < mac_len) {
961 dh_size = mac_len;
962 pr_trace_msg(trace_channel, 19,
963 "set DH size to %d bytes, matching client-to-server '%s' digest size",
964 dh_size, algo);
965 }
966
967 if (free_digest == TRUE) {
968 sftp_crypto_free_digest(digest);
969 }
970 }
971
972 algo = kex->session_names->s2c_mac_algo;
973 digest = sftp_crypto_get_digest(algo, NULL, &free_digest);
974 if (digest != NULL) {
975 int mac_len;
976
977 mac_len = EVP_MD_size(digest);
978 if (dh_size < mac_len) {
979 dh_size = mac_len;
980 pr_trace_msg(trace_channel, 19,
981 "set DH size to %d bytes, matching server-to-client '%s' digest size",
982 dh_size, algo);
983 }
984
985 if (free_digest == TRUE) {
986 sftp_crypto_free_digest(digest);
987 }
988 }
989
990 /* We want to return bits, not bytes. */
991 dh_nbits = dh_size * 8;
992
993 pr_trace_msg(trace_channel, 8, "requesting DH size of %d bits", dh_nbits);
994 return dh_nbits;
995 }
996
997 static int create_dh(struct sftp_kex *kex, int type) {
998 unsigned int attempts = 0;
999 int dh_nbits;
1000 DH *dh;
1001
1002 if (type != SFTP_DH_GROUP1_SHA1 &&
1003 type != SFTP_DH_GROUP14_SHA1 &&
1004 type != SFTP_DH_GROUP14_SHA256 &&
1005 type != SFTP_DH_GROUP16_SHA512 &&
1006 type != SFTP_DH_GROUP18_SHA512) {
1007 errno = EINVAL;
1008 return -1;
1009 }
1010
1011 if (kex->dh) {
1012 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1013 if (kex->dh->p) {
1014 BN_clear_free(kex->dh->p);
1015 kex->dh->p = NULL;
1016 }
1017
1018 if (kex->dh->g) {
1019 BN_clear_free(kex->dh->g);
1020 kex->dh->g = NULL;
1021 }
1022
1023 if (kex->dh->priv_key) {
1024 BN_clear_free(kex->dh->priv_key);
1025 kex->dh->priv_key = NULL;
1026 }
1027
1028 if (kex->dh->pub_key) {
1029 BN_clear_free(kex->dh->pub_key);
1030 kex->dh->pub_key = NULL;
1031 }
1032 #endif /* prior to OpenSSL-1.1.0 */
1033
1034 DH_free(kex->dh);
1035 kex->dh = NULL;
1036 }
1037
1038 dh_nbits = get_dh_nbits(kex);
1039
1040 /* We have 10 attempts to make a DH key which passes muster. */
1041 while (attempts <= 10) {
1042 const BIGNUM *dh_p, *dh_g, *dh_pub_key = NULL, *dh_priv_key = NULL;
1043
1044 pr_signals_handle();
1045
1046 attempts++;
1047 pr_trace_msg(trace_channel, 9, "attempt #%u to create a good DH key",
1048 attempts);
1049
1050 dh = DH_new();
1051 if (dh == NULL) {
1052 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1053 "error creating DH: %s", sftp_crypto_get_errors());
1054 return -1;
1055 }
1056
1057 dh_p = BN_new();
1058
1059 switch (type) {
1060 case SFTP_DH_GROUP18_SHA512:
1061 if (BN_hex2bn((BIGNUM **) &dh_p, dh_group18_str) == 0) {
1062 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1063 "error setting DH (group18) P: %s", sftp_crypto_get_errors());
1064 BN_clear_free((BIGNUM *) dh_p);
1065 DH_free(dh);
1066 return -1;
1067 }
1068 break;
1069
1070 case SFTP_DH_GROUP16_SHA512:
1071 if (BN_hex2bn((BIGNUM **) &dh_p, dh_group16_str) == 0) {
1072 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1073 "error setting DH (group16) P: %s", sftp_crypto_get_errors());
1074 BN_clear_free((BIGNUM *) dh_p);
1075 DH_free(dh);
1076 return -1;
1077 }
1078 break;
1079
1080 case SFTP_DH_GROUP14_SHA1:
1081 case SFTP_DH_GROUP14_SHA256:
1082 if (BN_hex2bn((BIGNUM **) &dh_p, dh_group14_str) == 0) {
1083 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1084 "error setting DH (group14) P: %s", sftp_crypto_get_errors());
1085 BN_clear_free((BIGNUM *) dh_p);
1086 DH_free(dh);
1087 return -1;
1088 }
1089 break;
1090
1091 default:
1092 if (BN_hex2bn((BIGNUM **) &dh_p, dh_group1_str) == 0) {
1093 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1094 "error setting DH (group1) P: %s", sftp_crypto_get_errors());
1095 BN_clear_free((BIGNUM *) dh_p);
1096 DH_free(dh);
1097 return -1;
1098 }
1099 break;
1100 }
1101
1102 dh_g = BN_new();
1103
1104 if (BN_hex2bn((BIGNUM **) &dh_g, "2") == 0) {
1105 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1106 "error setting DH G: %s", sftp_crypto_get_errors());
1107 BN_clear_free((BIGNUM *) dh_p);
1108 BN_clear_free((BIGNUM *) dh_g);
1109 DH_free(dh);
1110 return -1;
1111 }
1112
1113 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1114 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1115 DH_set0_pqg(dh, (BIGNUM *) dh_p, NULL, (BIGNUM *) dh_g);
1116 #else
1117 dh->p = dh_p;
1118 dh->g = dh_g;
1119 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1120
1121 dh_priv_key = BN_new();
1122
1123 /* Generate a random private exponent of the desired size, in bits. */
1124 if (!BN_rand((BIGNUM *) dh_priv_key, dh_nbits, 0, 0)) {
1125 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1126 "error generating DH random key (%d bits): %s", dh_nbits,
1127 sftp_crypto_get_errors());
1128 BN_clear_free((BIGNUM *) dh_priv_key);
1129 DH_free(dh);
1130 return -1;
1131 }
1132
1133 dh_pub_key = BN_new();
1134 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1135 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1136 DH_set0_key(dh, (BIGNUM *) dh_pub_key, (BIGNUM *) dh_priv_key);
1137 #else
1138 dh->pub_key = dh_pub_key;
1139 dh->priv_key = dh_priv_key;
1140 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1141
1142 pr_trace_msg(trace_channel, 12, "generating DH key");
1143 if (DH_generate_key(dh) != 1) {
1144 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1145 "error generating DH key: %s", sftp_crypto_get_errors());
1146 DH_free(dh);
1147 return -1;
1148 }
1149
1150 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1151 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1152 DH_get0_key(dh, &dh_pub_key, NULL);
1153 #else
1154 dh_pub_key = dh->pub_key;
1155 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1156
1157 if (have_good_dh(dh, dh_pub_key) < 0) {
1158 DH_free(dh);
1159 continue;
1160 }
1161
1162 kex->dh = dh;
1163
1164 switch (type) {
1165 #if defined(HAVE_SHA512_OPENSSL)
1166 case SFTP_DH_GROUP16_SHA512:
1167 case SFTP_DH_GROUP18_SHA512:
1168 kex->hash = EVP_sha512();
1169 break;
1170 #endif /* HAVE_SHA512_OPENSSL */
1171
1172 #if defined(HAVE_SHA256_OPENSSL)
1173 case SFTP_DH_GROUP14_SHA256:
1174 kex->hash = EVP_sha256();
1175 break;
1176 #endif /* HAVE_SHA256_OPENSSL */
1177
1178 default:
1179 kex->hash = EVP_sha1();
1180 }
1181
1182 return 0;
1183 }
1184
1185 errno = EPERM;
1186 return -1;
1187 }
1188
1189 static int prepare_dh(struct sftp_kex *kex, int type) {
1190 DH *dh;
1191
1192 if (type != SFTP_DH_GEX_SHA1 &&
1193 type != SFTP_DH_GEX_SHA256) {
1194 errno = EINVAL;
1195 return -1;
1196 }
1197
1198 if (kex->dh) {
1199 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1200 if (kex->dh->p) {
1201 BN_clear_free(kex->dh->p);
1202 kex->dh->p = NULL;
1203 }
1204
1205 if (kex->dh->g) {
1206 BN_clear_free(kex->dh->g);
1207 kex->dh->g = NULL;
1208 }
1209
1210 if (kex->dh->priv_key) {
1211 BN_clear_free(kex->dh->priv_key);
1212 kex->dh->priv_key = NULL;
1213 }
1214
1215 if (kex->dh->pub_key) {
1216 BN_clear_free(kex->dh->pub_key);
1217 kex->dh->pub_key = NULL;
1218 }
1219 #endif /* prior to OpenSSL-1.1.0 */
1220
1221 DH_free(kex->dh);
1222 kex->dh = NULL;
1223 }
1224
1225 dh = DH_new();
1226 if (!dh) {
1227 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1228 "error creating DH: %s", sftp_crypto_get_errors());
1229 return -1;
1230 }
1231
1232 kex->dh = dh;
1233
1234 if (type == SFTP_DH_GEX_SHA1) {
1235 kex->hash = EVP_sha1();
1236
1237 #if ((OPENSSL_VERSION_NUMBER > 0x000907000L && defined(OPENSSL_FIPS)) || \
1238 (OPENSSL_VERSION_NUMBER > 0x000908000L)) && \
1239 defined(HAVE_SHA256_OPENSSL)
1240 } else if (type == SFTP_DH_GEX_SHA256) {
1241 kex->hash = EVP_sha256();
1242 #endif
1243 }
1244
1245 return 0;
1246 }
1247
1248 static int finish_dh(struct sftp_kex *kex) {
1249 unsigned int attempts = 0;
1250 int dh_nbits;
1251 BIGNUM *dh_pub_key, *dh_priv_key;
1252
1253 dh_nbits = get_dh_nbits(kex);
1254
1255 /* We have 10 attempts to make a DH key which passes muster. */
1256 while (attempts <= 10) {
1257 pr_signals_handle();
1258
1259 attempts++;
1260 pr_trace_msg(trace_channel, 9, "attempt #%u to create a good DH key",
1261 attempts);
1262
1263 dh_priv_key = BN_new();
1264
1265 /* Generate a random private exponent of the desired size, in bits. */
1266 if (!BN_rand(dh_priv_key, dh_nbits, 0, 0)) {
1267 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1268 "error generating DH random key (%d bits): %s", dh_nbits,
1269 sftp_crypto_get_errors());
1270 BN_clear_free(dh_priv_key);
1271 return -1;
1272 }
1273
1274 dh_pub_key = BN_new();
1275
1276 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1277 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1278 DH_set0_key(kex->dh, dh_pub_key, dh_priv_key);
1279 #else
1280 kex->dh->pub_key = dh_pub_key;
1281 kex->dh->priv_key = dh_priv_key;
1282 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1283
1284 pr_trace_msg(trace_channel, 12, "generating DH key");
1285 if (DH_generate_key(kex->dh) != 1) {
1286 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1287 "error generating DH key: %s", sftp_crypto_get_errors());
1288 return -1;
1289 }
1290
1291 if (have_good_dh(kex->dh, kex->e) < 0) {
1292 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1293 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
1294 if (kex->dh->priv_key != NULL) {
1295 BN_clear_free(kex->dh->priv_key);
1296 kex->dh->priv_key = NULL;
1297 }
1298
1299 if (kex->dh->pub_key != NULL) {
1300 BN_clear_free(kex->dh->pub_key);
1301 kex->dh->pub_key = NULL;
1302 }
1303 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1304
1305 continue;
1306 }
1307
1308 return 0;
1309 }
1310
1311 errno = EPERM;
1312 return -1;
1313 }
1314
1315 static int create_kexrsa(struct sftp_kex *kex, int type) {
1316 RSA *rsa = NULL;
1317
1318 if (type != SFTP_KEXRSA_SHA1 &&
1319 type != SFTP_KEXRSA_SHA256) {
1320 errno = EINVAL;
1321 return -1;
1322 }
1323
1324 if (kex->rsa != NULL) {
1325 RSA_free(kex->rsa);
1326 kex->rsa = NULL;
1327 }
1328
1329 if (kex->rsa_encrypted != NULL) {
1330 pr_memscrub(kex->rsa_encrypted, kex->rsa_encrypted_len);
1331 kex->rsa_encrypted = NULL;
1332 kex->rsa_encrypted_len = 0;
1333 }
1334
1335 if (type == SFTP_KEXRSA_SHA1) {
1336 BIGNUM *e = NULL;
1337
1338 #if OPENSSL_VERSION_NUMBER > 0x000908000L
1339 e = BN_new();
1340 if (e == NULL) {
1341 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1342 "error allocated BIGNUM: %s", sftp_crypto_get_errors());
1343 return -1;
1344 }
1345
1346 if (BN_set_word(e, 17) != 1) {
1347 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1348 "error setting BIGNUM word: %s", sftp_crypto_get_errors());
1349 BN_free(e);
1350 return -1;
1351 }
1352
1353 rsa = RSA_new();
1354 if (RSA_generate_key_ex(rsa, SFTP_KEXRSA_SHA1_SIZE, e, NULL) != 1) {
1355 #else
1356 rsa = RSA_generate_key(SFTP_KEXRSA_SHA1_SIZE, 17, NULL, NULL);
1357 if (rsa == NULL) {
1358 #endif /* OpenSSL version 0.9.8 and later */
1359 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1360 "error generating %u-bit RSA key: %s", SFTP_KEXRSA_SHA1_SIZE,
1361 sftp_crypto_get_errors());
1362
1363 if (e != NULL) {
1364 BN_free(e);
1365 }
1366
1367 return -1;
1368 }
1369
1370 kex->hash = EVP_sha1();
1371
1372 #if ((OPENSSL_VERSION_NUMBER > 0x000907000L && defined(OPENSSL_FIPS)) || \
1373 (OPENSSL_VERSION_NUMBER > 0x000908000L)) && \
1374 defined(HAVE_SHA256_OPENSSL)
1375 } else if (type == SFTP_KEXRSA_SHA256) {
1376 BIGNUM *e = NULL;
1377
1378 # if OPENSSL_VERSION_NUMBER > 0x000908000L
1379 e = BN_new();
1380 if (e == NULL) {
1381 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1382 "error allocated BIGNUM: %s", sftp_crypto_get_errors());
1383 return -1;
1384 }
1385
1386 if (BN_set_word(e, 65537) != 1) {
1387 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1388 "error setting BIGNUM word: %s", sftp_crypto_get_errors());
1389 BN_free(e);
1390 return -1;
1391 }
1392
1393 rsa = RSA_new();
1394 if (RSA_generate_key_ex(rsa, SFTP_KEXRSA_SHA256_SIZE, e, NULL) != 1) {
1395 # else
1396 rsa = RSA_generate_key(SFTP_KEXRSA_SHA256_SIZE, 65537, NULL, NULL);
1397 if (rsa == NULL) {
1398 # endif /* OpenSSL version 0.9.8 and later */
1399 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1400 "error generating %u-bit RSA key: %s", SFTP_KEXRSA_SHA256_SIZE,
1401 sftp_crypto_get_errors());
1402
1403 if (e != NULL) {
1404 BN_free(e);
1405 }
1406
1407 return -1;
1408 }
1409
1410 kex->hash = EVP_sha256();
1411 #endif
1412 }
1413
1414 #if OPENSSL_VERSION_NUMBER < 0x0090702fL
1415 /* In OpenSSL-0.9.7a and later, RSA blinding is turned on by default. Thus
1416 * if our OpenSSL is older than that, manually enable RSA blinding.
1417 */
1418 if (RSA_blinding_on(rsa, NULL) != 1) {
1419 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1420 "error enabling RSA blinding for generated key: %s",
1421 sftp_crypto_get_errors());
1422
1423 } else {
1424 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1425 "RSA blinding enabled for generated key");
1426 }
1427 #endif
1428
1429 kex->rsa = rsa;
1430 return 0;
1431 }
1432
1433 #if defined(PR_USE_OPENSSL_ECC)
1434 static int create_ecdh(struct sftp_kex *kex, int type) {
1435 EC_KEY *ec;
1436 int curve_nid = -1;
1437 char *curve_name = NULL;
1438
1439 switch (type) {
1440 case SFTP_ECDH_SHA256:
1441 curve_name = "NID_X9_62_prime256v1";
1442 # if defined(HAVE_SHA256_OPENSSL)
1443 curve_nid = NID_X9_62_prime256v1;
1444 kex->hash = EVP_sha256();
1445 # else
1446 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1447 "unable to generate EC key using '%s': OpenSSL lacks SHA256 support",
1448 curve_name);
1449 errno = ENOSYS;
1450 return -1;
1451 # endif /* HAVE_SHA256_OPENSSL */
1452 break;
1453
1454 case SFTP_ECDH_SHA384:
1455 curve_name = "NID_secp384r1";
1456 # if defined(HAVE_SHA256_OPENSSL)
1457 curve_nid = NID_secp384r1;
1458 kex->hash = EVP_sha384();
1459 # else
1460 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1461 "unable to generate EC key using '%s': OpenSSL lacks SHA256 support",
1462 curve_name);
1463 errno = ENOSYS;
1464 return -1;
1465 # endif /* HAVE_SHA256_OPENSSL */
1466 break;
1467
1468 case SFTP_ECDH_SHA512:
1469 curve_name = "NID_secp521r1";
1470 # if defined(HAVE_SHA512_OPENSSL)
1471 curve_nid = NID_secp521r1;
1472 kex->hash = EVP_sha512();
1473 # else
1474 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1475 "unable to generate EC key using '%s': OpenSSL lacks SHA512 support",
1476 curve_name);
1477 errno = ENOSYS;
1478 return -1;
1479 # endif /* HAVE_SHA512_OPENSSL */
1480 break;
1481
1482 default:
1483 errno = EINVAL;
1484 return -1;
1485 }
1486
1487 ec = EC_KEY_new_by_curve_name(curve_nid);
1488 if (ec == NULL) {
1489 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1490 "error generating new EC key using '%s': %s", curve_name,
1491 sftp_crypto_get_errors());
1492 return -1;
1493 }
1494
1495 if (EC_KEY_generate_key(ec) != 1) {
1496 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1497 "error generating new EC key: %s", sftp_crypto_get_errors());
1498 EC_KEY_free(ec);
1499 return -1;
1500 }
1501
1502 kex->ec = ec;
1503 return 0;
1504 }
1505
1506 static int finish_ecdh(struct sftp_kex *kex) {
1507 if (kex->ec != NULL) {
1508 EC_KEY_free(kex->ec);
1509 kex->ec = NULL;
1510 }
1511
1512 if (kex->client_point != NULL) {
1513 EC_POINT_clear_free(kex->client_point);
1514 kex->client_point = NULL;
1515 }
1516
1517 return 0;
1518 }
1519 #endif /* PR_USE_OPENSSL_ECC */
1520
1521 /* Given a name-list, return the first (i.e. preferred) name in the list. */
1522 static const char *get_preferred_name(pool *p, const char *names) {
1523 register unsigned int i;
1524
1525 /* Advance to the first comma, or NUL. */
1526 for (i = 0; names[i] && names[i] != ','; i++);
1527
1528 if (names[i] == ',' ||
1529 names[i] == '\0') {
1530 char *pref;
1531
1532 pref = pcalloc(p, i + 1);
1533 memcpy(pref, names, i);
1534
1535 return pref;
1536 }
1537
1538 /* This should never happen. */
1539 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1540 "unable to find preferred name in '%s'", names);
1541 return NULL;
1542 }
1543
1544 /* Note that in this default list of key exchange algorithms, one of the
1545 * REQUIRED algorithms is conspicuously absent:
1546 *
1547 * diffie-hellman-group1-sha1
1548 *
1549 * This exchange has a weak hardcoded DH group, and will thus only be used
1550 * if explicitly requested via SFTPKeyExchanges, or if the AllowWeakDH
1551 * SFTPOption is used.
1552 */
1553 static const char *kex_exchanges[] = {
1554 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
1555 "curve448-sha512",
1556 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
1557 #if defined(PR_USE_SODIUM) && defined(HAVE_SHA256_OPENSSL)
1558 "curve25519-sha256",
1559 "[email protected]",
1560 #endif /* PR_USE_SODIUM and HAVE_SHA256_OPENSSL */
1561 #if defined(PR_USE_OPENSSL_ECC)
1562 "ecdh-sha2-nistp521",
1563 "ecdh-sha2-nistp384",
1564 "ecdh-sha2-nistp256",
1565 #endif /* PR_USE_OPENSSL_ECC */
1566
1567 #if (OPENSSL_VERSION_NUMBER > 0x000907000L && defined(OPENSSL_FIPS)) || \
1568 (OPENSSL_VERSION_NUMBER > 0x000908000L)
1569 # if defined(HAVE_SHA512_OPENSSL)
1570 "diffie-hellman-group18-sha512",
1571 "diffie-hellman-group16-sha512",
1572 # endif /* HAVE_SHA512_OPENSSL */
1573 # if defined(HAVE_SHA256_OPENSSL)
1574 "diffie-hellman-group14-sha256",
1575 "diffie-hellman-group-exchange-sha256",
1576 # endif /* HAVE_SHA256_OPENSSL */
1577 #endif
1578 "diffie-hellman-group-exchange-sha1",
1579 "diffie-hellman-group14-sha1",
1580
1581 #if 0
1582 /* We cannot currently support rsa2048-sha256, since it requires support
1583 * for PKCS#1 v2.1 (RFC3447). OpenSSL only supports PKCS#1 v2.0 (RFC2437)
1584 * at present, which only allows EME-OAEP using SHA1. v2.1 allows for
1585 * using other message digests, e.g. SHA256, for EME-OAEP.
1586 */
1587 #if ((OPENSSL_VERSION_NUMBER > 0x000907000L && defined(OPENSSL_FIPS)) || \
1588 (OPENSSL_VERSION_NUMBER > 0x000908000L)) && \
1589 defined(HAVE_SHA256_OPENSSL)
1590 "rsa2048-sha256",
1591 #endif
1592 #endif
1593
1594 "rsa1024-sha1",
1595 NULL,
1596 };
1597
1598 static const char *get_kexinit_exchange_list(pool *p) {
1599 char *res = "";
1600 config_rec *c;
1601
1602 c = find_config(main_server->conf, CONF_PARAM, "SFTPKeyExchanges", FALSE);
1603 if (c != NULL) {
1604 res = pstrdup(p, c->argv[0]);
1605
1606 } else {
1607 register unsigned int i;
1608
1609 for (i = 0; kex_exchanges[i]; i++) {
1610 res = pstrcat(p, res, *res ? "," : "", pstrdup(p, kex_exchanges[i]),
1611 NULL);
1612 }
1613
1614 if (sftp_opts & SFTP_OPT_ALLOW_WEAK_DH) {
1615 /* The hardcoded group for this exchange is rather weak in the face of
1616 * the "Logjam" vulnerability (see https://weakdh.org). Thus it is
1617 * only appended to the end of the default exchanges if the AllowWeakDH
1618 * SFTPOption is in effect.
1619 */
1620 res = pstrcat(p, res, ",", pstrdup(p, "diffie-hellman-group1-sha1"),
1621 NULL);
1622 }
1623 }
1624
1625 if (!(sftp_opts & SFTP_OPT_NO_EXT_INFO)) {
1626 /* Indicate support for RFC 8308's extension negotiation mechanism. */
1627 res = pstrcat(p, res, *res ? "," : "", pstrdup(p, "ext-info-s"), NULL);
1628 }
1629
1630 if (!(sftp_opts & SFTP_OPT_NO_STRICT_KEX)) {
1631 /* Indicate support for OpenSSH's custom "strict KEX" mode extension,
1632 * but only if we have not done/completed our first KEX.
1633 */
1634 if (kex_done_first_kex == FALSE) {
1635 res = pstrcat(p, res, *res ? "," : "",
1636 pstrdup(p, "[email protected]"), NULL);
1637 }
1638 }
1639
1640 return res;
1641 }
1642
1643 static const char *get_kexinit_hostkey_algo_list(pool *p) {
1644 register unsigned int i;
1645 config_rec *c;
1646 array_header *hostkey_algos;
1647 char *list = "";
1648
1649 /* Our list of supported hostkey algorithms depends on the hostkeys
1650 * that have been configured. Show a preference for RSA over DSA,
1651 * and ECDSA over both RSA and DSA, and ED25519/ED448 over all.
1652 */
1653 hostkey_algos = make_array(p, 1, sizeof(char *));
1654
1655 c = find_config(main_server->conf, CONF_PARAM, "SFTPHostKeys", FALSE);
1656 if (c != NULL) {
1657 for (i = 0; i < c->argc; i++) {
1658 *((char **) push_array(hostkey_algos)) = pstrdup(p, c->argv[i]);
1659 }
1660
1661 } else {
1662 /* Create our default list of host key algorithms, in preference order. */
1663 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ssh-ed448");
1664 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ssh-ed25519");
1665 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ecdsa-sha2-nistp256");
1666 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ecdsa-sha2-nistp384");
1667 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ecdsa-sha2-nistp521");
1668 *((char **) push_array(hostkey_algos)) = pstrdup(p, "rsa-sha2-512");
1669 *((char **) push_array(hostkey_algos)) = pstrdup(p, "rsa-sha2-256");
1670 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ssh-rsa");
1671 *((char **) push_array(hostkey_algos)) = pstrdup(p, "ssh-dss");
1672 }
1673
1674 for (i = 0; i < hostkey_algos->nelts; i++) {
1675 const char *algo;
1676 int have_key = FALSE, supported_algo = FALSE;
1677
1678 pr_signals_handle();
1679
1680 algo = ((char **) hostkey_algos->elts)[i];
1681
1682 if (strcmp(algo, "ssh-ed25519") == 0) {
1683 #if defined(PR_USE_SODIUM)
1684 supported_algo = TRUE;
1685 #endif /* PR_USE_SODIUM */
1686
1687 if (sftp_keys_have_ed25519_hostkey() == 0) {
1688 have_key = TRUE;
1689 }
1690
1691 } else if (strcmp(algo, "ssh-ed448") == 0) {
1692 #if defined(HAVE_X448_OPENSSL)
1693 supported_algo = TRUE;
1694 #endif /* HAVE_X448_OPENSSL */
1695
1696 if (sftp_keys_have_ed448_hostkey() == 0) {
1697 have_key = TRUE;
1698 }
1699
1700 #if defined(PR_USE_OPENSSL_ECC)
1701 } else if (strcmp(algo, "ecdsa-sha2-nistp256") == 0) {
1702 int *nids = NULL, res;
1703
1704 supported_algo = TRUE;
1705 res = sftp_keys_have_ecdsa_hostkey(p, &nids);
1706 if (res > 0) {
1707 register int j;
1708
1709 for (j = 0; j < res; j++) {
1710 if (nids[j] == NID_X9_62_prime256v1) {
1711 have_key = TRUE;
1712 break;
1713 }
1714 }
1715 }
1716
1717 } else if (strcmp(algo, "ecdsa-sha2-nistp384") == 0) {
1718 int *nids = NULL, res;
1719
1720 supported_algo = TRUE;
1721 res = sftp_keys_have_ecdsa_hostkey(p, &nids);
1722 if (res > 0) {
1723 register int j;
1724
1725 for (j = 0; j < res; j++) {
1726 if (nids[j] == NID_secp384r1) {
1727 have_key = TRUE;
1728 break;
1729 }
1730 }
1731 }
1732
1733 } else if (strcmp(algo, "ecdsa-sha2-nistp521") == 0) {
1734 int *nids = NULL, res;
1735
1736 supported_algo = TRUE;
1737 res = sftp_keys_have_ecdsa_hostkey(p, &nids);
1738 if (res > 0) {
1739 register int j;
1740
1741 for (j = 0; j < res; j++) {
1742 if (nids[j] == NID_secp521r1) {
1743 have_key = TRUE;
1744 break;
1745 }
1746 }
1747 }
1748 #endif /* PR_USE_OPENSSL_ECC */
1749
1750 #if defined(HAVE_SHA512_OPENSSL)
1751 } else if (strcmp(algo, "rsa-sha2-512") == 0) {
1752 supported_algo = TRUE;
1753
1754 if (sftp_keys_have_rsa_hostkey() == 0) {
1755 have_key = TRUE;
1756 }
1757 #endif /* HAVE_SHA512_OPENSSL */
1758
1759 #if defined(HAVE_SHA256_OPENSSL)
1760 } else if (strcmp(algo, "rsa-sha2-256") == 0) {
1761 supported_algo = TRUE;
1762
1763 if (sftp_keys_have_rsa_hostkey() == 0) {
1764 have_key = TRUE;
1765 }
1766 #endif /* HAVE_SHA256_OPENSSL */
1767
1768 } else if (strcmp(algo, "ssh-rsa") == 0) {
1769 supported_algo = TRUE;
1770
1771 if (sftp_keys_have_rsa_hostkey() == 0) {
1772 have_key = TRUE;
1773 }
1774
1775 } else if (strcmp(algo, "ssh-dss") == 0) {
1776 supported_algo = TRUE;
1777
1778 if (sftp_keys_have_dsa_hostkey() == 0) {
1779 have_key = TRUE;
1780 }
1781 }
1782
1783 if (supported_algo == TRUE &&
1784 have_key == TRUE) {
1785 list = pstrcat(p, list, *list ? "," : "", algo, NULL);
1786
1787 } else {
1788 if (supported_algo == FALSE) {
1789 pr_trace_msg(trace_channel, 19,
1790 "omitting host key algorithm '%s' due to lack of support", algo);
1791
1792 } else {
1793 pr_trace_msg(trace_channel, 19,
1794 "omitting host key algorithm '%s' due to lack of key", algo);
1795 }
1796 }
1797 }
1798
1799 return list;
1800 }
1801
1802 static struct sftp_kex *create_kex(pool *p) {
1803 struct sftp_kex *kex;
1804 const char *list;
1805 config_rec *c;
1806 pool *tmp_pool;
1807
1808 tmp_pool = make_sub_pool(p);
1809 pr_pool_tag(tmp_pool, "Kex KEXINIT Pool");
1810
1811 kex = pcalloc(tmp_pool, sizeof(struct sftp_kex));
1812 kex->pool = tmp_pool;
1813 kex->client_version = kex_client_version;
1814 kex->server_version = kex_server_version;
1815 kex->client_names = pcalloc(kex->pool, sizeof(struct sftp_kex_names));
1816 kex->server_names = pcalloc(kex->pool, sizeof(struct sftp_kex_names));
1817 kex->session_names = pcalloc(kex->pool, sizeof(struct sftp_kex_names));
1818 kex->use_hostkey_type = SFTP_KEY_UNKNOWN;
1819 kex->dh = NULL;
1820 kex->e = NULL;
1821 kex->hash = NULL;
1822 kex->k = NULL;
1823 kex->h = NULL;
1824 kex->hlen = 0;
1825 kex->rsa = NULL;
1826 kex->rsa_encrypted = NULL;
1827 kex->rsa_encrypted_len = 0;
1828
1829 list = get_kexinit_exchange_list(kex->pool);
1830 kex->server_names->kex_algo = list;
1831
1832 list = get_kexinit_hostkey_algo_list(kex->pool);
1833 kex->server_names->server_hostkey_algo = list;
1834
1835 list = sftp_crypto_get_kexinit_cipher_list(kex->pool);
1836 kex->server_names->c2s_encrypt_algo = list;
1837 kex->server_names->s2c_encrypt_algo = list;
1838
1839 list = sftp_crypto_get_kexinit_digest_list(kex->pool);
1840 kex->server_names->c2s_mac_algo = list;
1841 kex->server_names->s2c_mac_algo = list;
1842
1843 c = find_config(main_server->conf, CONF_PARAM, "SFTPCompression", FALSE);
1844 if (c) {
1845 int comp_mode;
1846
1847 comp_mode = *((int *) c->argv[0]);
1848
1849 switch (comp_mode) {
1850 case 2:
1851 /* Advertise that we support OpenSSH's "delayed" compression mode. */
1852 kex->server_names->c2s_comp_algo = "[email protected],zlib,none";
1853 kex->server_names->s2c_comp_algo = "[email protected],zlib,none";
1854 break;
1855
1856 case 1:
1857 kex->server_names->c2s_comp_algo = "zlib,none";
1858 kex->server_names->s2c_comp_algo = "zlib,none";
1859 break;
1860
1861 default:
1862 kex->server_names->c2s_comp_algo = "none";
1863 kex->server_names->s2c_comp_algo = "none";
1864 break;
1865 }
1866
1867 } else {
1868 kex->server_names->c2s_comp_algo = "none";
1869 kex->server_names->s2c_comp_algo = "none";
1870 }
1871
1872 #ifdef PR_USE_NLS
1873 c = find_config(main_server->conf, CONF_PARAM, "SFTPLanguages", FALSE);
1874 if (c) {
1875 /* XXX Need to implement functionality here. */
1876
1877 } else {
1878 kex->server_names->c2s_lang = "en_US";
1879 kex->server_names->s2c_lang = "en_US";
1880 }
1881 #else
1882 kex->server_names->c2s_lang = "";
1883 kex->server_names->s2c_lang = "";
1884 #endif /* !PR_USE_NLS */
1885
1886 return kex;
1887 }
1888
1889 static void destroy_kex(struct sftp_kex *kex) {
1890 if (kex != NULL) {
1891 if (kex->dh != NULL) {
1892 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1893 if (kex->dh->p != NULL) {
1894 BN_clear_free(kex->dh->p);
1895 kex->dh->p = NULL;
1896 }
1897
1898 if (kex->dh->g != NULL) {
1899 BN_clear_free(kex->dh->g);
1900 kex->dh->g = NULL;
1901 }
1902 #endif /* prior to OpenSSL-1.1.0 */
1903
1904 DH_free(kex->dh);
1905 kex->dh = NULL;
1906 }
1907
1908 if (kex->rsa != NULL) {
1909 RSA_free(kex->rsa);
1910 kex->rsa = NULL;
1911 }
1912
1913 if (kex->rsa_encrypted != NULL) {
1914 pr_memscrub(kex->rsa_encrypted, kex->rsa_encrypted_len);
1915 kex->rsa_encrypted = NULL;
1916 kex->rsa_encrypted_len = 0;
1917 }
1918
1919 if (kex->e != NULL) {
1920 BN_clear_free((BIGNUM *) kex->e);
1921 kex->e = NULL;
1922 }
1923
1924 if (kex->k != NULL) {
1925 BN_clear_free((BIGNUM *) kex->k);
1926 kex->k = NULL;
1927 }
1928
1929 if (kex->hlen > 0) {
1930 pr_memscrub((char *) kex->h, kex->hlen);
1931 kex->hlen = 0;
1932 }
1933
1934 #if defined(PR_USE_OPENSSL_ECC)
1935 if (kex->ec != NULL) {
1936 EC_KEY_free(kex->ec);
1937 kex->ec = NULL;
1938 }
1939
1940 if (kex->client_point != NULL) {
1941 EC_POINT_free(kex->client_point);
1942 kex->client_point = NULL;
1943 }
1944 #endif /* PR_USE_OPENSSL_ECC */
1945
1946 #if defined(PR_USE_SODIUM) && defined(HAVE_SHA256_OPENSSL)
1947 if (kex->client_curve25519 != NULL) {
1948 pr_memscrub(kex->client_curve25519, CURVE25519_SIZE);
1949 kex->client_curve25519 = NULL;
1950 }
1951 #endif /* PR_USE_SODIUM and HAVE_SHA256_OPENSSL */
1952
1953 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
1954 if (kex->client_curve448 != NULL) {
1955 pr_memscrub(kex->client_curve448, CURVE448_SIZE);
1956 kex->client_curve448 = NULL;
1957 }
1958 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
1959
1960 if (kex->pool != NULL) {
1961 destroy_pool(kex->pool);
1962 }
1963 }
1964
1965 kex_first_kex = kex_rekey_kex = NULL;
1966 }
1967
1968 static int setup_kex_algo(struct sftp_kex *kex, const char *algo) {
1969 if (strcmp(algo, "diffie-hellman-group1-sha1") == 0) {
1970 if (create_dh(kex, SFTP_DH_GROUP1_SHA1) < 0) {
1971 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1972 "error using '%s' as the key exchange algorithm: %s", algo,
1973 strerror(errno));
1974 return -1;
1975 }
1976
1977 kex->session_names->kex_algo = algo;
1978 return 0;
1979 }
1980
1981 if (strcmp(algo, "diffie-hellman-group14-sha1") == 0) {
1982 if (create_dh(kex, SFTP_DH_GROUP14_SHA1) < 0) {
1983 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1984 "error using '%s' as the key exchange algorithm: %s", algo,
1985 strerror(errno));
1986 return -1;
1987 }
1988
1989 kex->session_names->kex_algo = algo;
1990 return 0;
1991 }
1992
1993 if (strcmp(algo, "diffie-hellman-group14-sha256") == 0) {
1994 if (create_dh(kex, SFTP_DH_GROUP14_SHA256) < 0) {
1995 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1996 "error using '%s' as the key exchange algorithm: %s", algo,
1997 strerror(errno));
1998 return -1;
1999 }
2000
2001 kex->session_names->kex_algo = algo;
2002 return 0;
2003 }
2004
2005 if (strcmp(algo, "diffie-hellman-group16-sha512") == 0) {
2006 if (create_dh(kex, SFTP_DH_GROUP16_SHA512) < 0) {
2007 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2008 "error using '%s' as the key exchange algorithm: %s", algo,
2009 strerror(errno));
2010 return -1;
2011 }
2012
2013 kex->session_names->kex_algo = algo;
2014 return 0;
2015 }
2016
2017 if (strcmp(algo, "diffie-hellman-group18-sha512") == 0) {
2018 if (create_dh(kex, SFTP_DH_GROUP18_SHA512) < 0) {
2019 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2020 "error using '%s' as the key exchange algorithm: %s", algo,
2021 strerror(errno));
2022 return -1;
2023 }
2024
2025 kex->session_names->kex_algo = algo;
2026 return 0;
2027 }
2028
2029 if (strcmp(algo, "diffie-hellman-group-exchange-sha1") == 0) {
2030 if (prepare_dh(kex, SFTP_DH_GEX_SHA1) < 0) {
2031 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2032 "error using '%s' as the key exchange algorithm: %s", algo,
2033 strerror(errno));
2034 return -1;
2035 }
2036
2037 kex->session_names->kex_algo = algo;
2038 kex->use_gex = TRUE;
2039 return 0;
2040 }
2041
2042 if (strcmp(algo, "rsa1024-sha1") == 0) {
2043 if (create_kexrsa(kex, SFTP_KEXRSA_SHA1) < 0) {
2044 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2045 "error using '%s' as the key exchange algorithm: %s", algo,
2046 strerror(errno));
2047 return -1;
2048 }
2049
2050 kex->session_names->kex_algo = algo;
2051 kex->use_kexrsa = TRUE;
2052 return 0;
2053 }
2054
2055 #if ((OPENSSL_VERSION_NUMBER > 0x000907000L && defined(OPENSSL_FIPS)) || \
2056 (OPENSSL_VERSION_NUMBER > 0x000908000L)) && \
2057 defined(HAVE_SHA256_OPENSSL)
2058 if (strcmp(algo, "diffie-hellman-group-exchange-sha256") == 0) {
2059 if (prepare_dh(kex, SFTP_DH_GEX_SHA256) < 0) {
2060 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2061 "error using '%s' as the key exchange algorithm: %s", algo,
2062 strerror(errno));
2063 return -1;
2064 }
2065
2066 kex->session_names->kex_algo = algo;
2067 kex->use_gex = TRUE;
2068 return 0;
2069 }
2070
2071 if (strcmp(algo, "rsa2048-sha256") == 0) {
2072 if (create_kexrsa(kex, SFTP_KEXRSA_SHA256) < 0) {
2073 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2074 "error using '%s' as the key exchange algorithm: %s", algo,
2075 strerror(errno));
2076 return -1;
2077 }
2078
2079 kex->session_names->kex_algo = algo;
2080 kex->use_kexrsa = TRUE;
2081 return 0;
2082 }
2083 #endif
2084
2085 #if defined(PR_USE_OPENSSL_ECC)
2086 if (strcmp(algo, "ecdh-sha2-nistp256") == 0) {
2087 if (create_ecdh(kex, SFTP_ECDH_SHA256) < 0) {
2088 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2089 "error using '%s' as the key exchange algorithm: %s", algo,
2090 strerror(errno));
2091 return -1;
2092 }
2093
2094 kex->session_names->kex_algo = algo;
2095 kex->use_ecdh = TRUE;
2096 return 0;
2097 }
2098
2099 if (strcmp(algo, "ecdh-sha2-nistp384") == 0) {
2100 if (create_ecdh(kex, SFTP_ECDH_SHA384) < 0) {
2101 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2102 "error using '%s' as the key exchange algorithm: %s", algo,
2103 strerror(errno));
2104 return -1;
2105 }
2106
2107 kex->session_names->kex_algo = algo;
2108 kex->use_ecdh = TRUE;
2109 return 0;
2110 }
2111
2112 if (strcmp(algo, "ecdh-sha2-nistp521") == 0) {
2113 if (create_ecdh(kex, SFTP_ECDH_SHA512) < 0) {
2114 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2115 "error using '%s' as the key exchange algorithm: %s", algo,
2116 strerror(errno));
2117 return -1;
2118 }
2119
2120 kex->session_names->kex_algo = algo;
2121 kex->use_ecdh = TRUE;
2122 return 0;
2123 }
2124 #endif /* PR_USE_OPENSSL_ECC */
2125
2126 #if defined(PR_USE_SODIUM) && defined(HAVE_SHA256_OPENSSL)
2127 if (strcmp(algo, "curve25519-sha256") == 0 ||
2128 strcmp(algo, "[email protected]") == 0) {
2129 kex->hash = EVP_sha256();
2130 kex->session_names->kex_algo = algo;
2131 kex->use_curve25519 = TRUE;
2132 return 0;
2133 }
2134 #endif /* PR_USE_SODIUM and HAVE_SHA256_OPENSSL */
2135
2136 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
2137 if (strcmp(algo, "curve448-sha512") == 0) {
2138 kex->hash = EVP_sha512();
2139 kex->session_names->kex_algo = algo;
2140 kex->use_curve448 = TRUE;
2141 return 0;
2142 }
2143 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
2144
2145 if (strcmp(algo, "ext-info-c") == 0 ||
2146 strcmp(algo, "ext-info-s") == 0) {
2147 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2148 "unable to use extension negotiation algorithm '%s' for key exchange",
2149 algo);
2150 errno = EINVAL;
2151 return -1;
2152 }
2153
2154 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2155 "unsupported key exchange algorithm '%s'", algo);
2156 errno = EINVAL;
2157 return -1;
2158 }
2159
2160 static int setup_hostkey_algo(struct sftp_kex *kex, const char *algo) {
2161 int res = -1;
2162
2163 kex->session_names->server_hostkey_algo = (char *) algo;
2164
2165 if (strcmp(algo, "ssh-dss") == 0) {
2166 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_DSA;
2167 res = 0;
2168
2169 } else if (strcmp(algo, "ssh-rsa") == 0) {
2170 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_RSA;
2171 res = 0;
2172
2173 #if defined(HAVE_SHA256_OPENSSL)
2174 } else if (strcmp(algo, "rsa-sha2-256") == 0) {
2175 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_RSA_SHA256;
2176 res = 0;
2177 #endif /* HAVE_SHA256_OPENSSL */
2178
2179 #if defined(HAVE_SHA512_OPENSSL)
2180 } else if (strcmp(algo, "rsa-sha2-512") == 0) {
2181 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_RSA_SHA512;
2182 res = 0;
2183 #endif /* HAVE_SHA512_OPENSSL */
2184
2185 #if defined(PR_USE_OPENSSL_ECC)
2186 } else if (strcmp(algo, "ecdsa-sha2-nistp256") == 0) {
2187 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_ECDSA_256;
2188 res = 0;
2189
2190 } else if (strcmp(algo, "ecdsa-sha2-nistp384") == 0) {
2191 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_ECDSA_384;
2192 res = 0;
2193
2194 } else if (strcmp(algo, "ecdsa-sha2-nistp521") == 0) {
2195 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_ECDSA_521;
2196 res = 0;
2197 #endif /* PR_USE_OPENSSL_ECC */
2198
2199 #if defined(PR_USE_SODIUM)
2200 } else if (strcmp(algo, "ssh-ed25519") == 0) {
2201 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_ED25519;
2202 res = 0;
2203 #endif /* PR_USE_SODIUM */
2204
2205 #if defined(HAVE_X448_OPENSSL)
2206 } else if (strcmp(algo, "ssh-ed448") == 0) {
2207 kex->use_hostkey_type = kex_used_hostkey_type = SFTP_KEY_ED448;
2208 res = 0;
2209 #endif /* HAVE_X448_OPENSSL */
2210 }
2211
2212 /* XXX Need to handle "x509v3-ssh-dss", "x509v3-ssh-rsa", "x509v3-sign"
2213 * algorithms here.
2214 */
2215
2216 if (res < 0) {
2217 errno = EINVAL;
2218 return -1;
2219 }
2220
2221 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2222 " + Session host key algorithm: %s", algo);
2223 pr_trace_msg(trace_channel, 20, "session host key algorithm: %s", algo);
2224 return 0;
2225 }
2226
2227 static int setup_c2s_encrypt_algo(struct sftp_kex *kex, const char *algo) {
2228 if (sftp_cipher_set_read_algo(algo) < 0) {
2229 return -1;
2230 }
2231
2232 kex->session_names->c2s_encrypt_algo = algo;
2233 return 0;
2234 }
2235
2236 static int setup_s2c_encrypt_algo(struct sftp_kex *kex, const char *algo) {
2237 if (sftp_cipher_set_write_algo(algo) < 0) {
2238 return -1;
2239 }
2240
2241 kex->session_names->s2c_encrypt_algo = algo;
2242 return 0;
2243 }
2244
2245 static int setup_c2s_mac_algo(struct sftp_kex *kex, const char *algo) {
2246 if (sftp_mac_set_read_algo(algo) < 0) {
2247 return -1;
2248 }
2249
2250 kex->session_names->c2s_mac_algo = algo;
2251 return 0;
2252 }
2253
2254 static int setup_s2c_mac_algo(struct sftp_kex *kex, const char *algo) {
2255 if (sftp_mac_set_write_algo(algo) < 0) {
2256 return -1;
2257 }
2258
2259 kex->session_names->s2c_mac_algo = algo;
2260 return 0;
2261 }
2262
2263 static int setup_c2s_comp_algo(struct sftp_kex *kex, const char *algo) {
2264 if (sftp_compress_set_read_algo(algo) < 0) {
2265 return -1;
2266 }
2267
2268 kex->session_names->c2s_comp_algo = algo;
2269 return 0;
2270 }
2271
2272 static int setup_s2c_comp_algo(struct sftp_kex *kex, const char *algo) {
2273 if (sftp_compress_set_write_algo(algo) < 0) {
2274 return -1;
2275 }
2276
2277 kex->session_names->s2c_comp_algo = algo;
2278 return 0;
2279 }
2280
2281 static int setup_c2s_lang(struct sftp_kex *kex, const char *lang) {
2282 /* XXX Need to implement the functionality here. */
2283 kex->session_names->c2s_lang = lang;
2284 return 0;
2285 }
2286
2287 static int setup_s2c_lang(struct sftp_kex *kex, const char *lang) {
2288 /* XXX Need to implement the functionality here. */
2289 kex->session_names->s2c_lang = lang;
2290 return 0;
2291 }
2292
2293 static int get_session_names(struct sftp_kex *kex, int *correct_guess) {
2294 const char *kex_algo, *shared, *client_list, *server_list;
2295 const char *client_pref, *server_pref;
2296 pool *tmp_pool;
2297
2298 tmp_pool = make_sub_pool(kex->pool);
2299 pr_pool_tag(tmp_pool, "SSH2 session shared name pool");
2300
2301 client_list = kex->client_names->kex_algo;
2302 server_list = kex->server_names->kex_algo;
2303
2304 pr_trace_msg(trace_channel, 8, "client-sent key exchange algorithms: %s",
2305 client_list);
2306 pr_trace_msg(trace_channel, 8, "server-sent key exchange algorithms: %s",
2307 server_list);
2308
2309 client_pref = get_preferred_name(tmp_pool, client_list);
2310 server_pref = get_preferred_name(tmp_pool, server_list);
2311
2312 /* Did the client correctly guess at the key exchange algorithm that
2313 * we would list first in our server list, if it says it sent
2314 * a guess KEX packet?
2315 */
2316
2317 if (kex->first_kex_follows == TRUE &&
2318 *correct_guess == TRUE &&
2319 client_pref != NULL &&
2320 server_pref != NULL) {
2321
2322 if (strcmp(client_pref, server_pref) != 0) {
2323 *correct_guess = FALSE;
2324
2325 pr_trace_msg(trace_channel, 7,
2326 "client incorrectly guessed key exchange algorithm '%s'", client_pref);
2327
2328 } else {
2329 pr_trace_msg(trace_channel, 7,
2330 "client correctly guessed key exchange algorithm '%s'", server_pref);
2331 }
2332 }
2333
2334 kex_algo = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2335 if (kex_algo != NULL) {
2336 /* Unlike the following algorithms, we wait to setup the chosen kex algo
2337 * until the end. Why? The kex algo setup may require knowledge of the
2338 * ciphers chosen for encryption, MAC, etc (Bug#4097).
2339 */
2340 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2341 " + Session key exchange: %s", kex_algo);
2342 pr_trace_msg(trace_channel, 20, "session key exchange algorithm: %s",
2343 kex_algo);
2344
2345 /* Did the client indicate EXT_INFO support (Issue #984)? */
2346 kex->use_ext_info = sftp_misc_namelist_contains(kex->pool, client_list,
2347 "ext-info-c");
2348 pr_trace_msg(trace_channel, 20, "client %s EXT_INFO support",
2349 kex->use_ext_info ? "signaled" : "did not signal" );
2350
2351 if (!(sftp_opts & SFTP_OPT_NO_STRICT_KEX)) {
2352 /* Did the client indicate "strict kex" support (Issue 1760)?
2353 *
2354 * Note that we only check for this if it is our first KEXINIT.
2355 * The "strict kex" extension is ignored in any subsequent KEXINITs, as
2356 * for rekeys.
2357 */
2358 if (kex_done_first_kex == FALSE) {
2359 use_strict_kex = sftp_misc_namelist_contains(kex->pool,
2360 client_list, "[email protected]");
2361 pr_trace_msg(trace_channel, 20, "client %s strict KEX support",
2362 use_strict_kex ? "signaled" : "did not signal" );
2363 }
2364 }
2365
2366 } else {
2367 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2368 "no shared key exchange algorithm found (client sent '%s', server sent "
2369 "'%s')", client_list, server_list);
2370 destroy_pool(tmp_pool);
2371 return -1;
2372 }
2373
2374 client_list = kex->client_names->server_hostkey_algo;
2375 server_list = kex->server_names->server_hostkey_algo;
2376
2377 pr_trace_msg(trace_channel, 8,
2378 "client-sent host key algorithms: %s", client_list);
2379 pr_trace_msg(trace_channel, 8,
2380 "server-sent host key algorithms: %s", server_list);
2381
2382 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2383 if (shared != NULL) {
2384 if (setup_hostkey_algo(kex, shared) < 0) {
2385 destroy_pool(tmp_pool);
2386 return -1;
2387 }
2388
2389 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2390 " + Session server hostkey: %s", shared);
2391 pr_trace_msg(trace_channel, 20, "session server hostkey algorithm: %s",
2392 shared);
2393
2394 } else {
2395 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2396 "no shared server hostkey algorithm found (client sent '%s', server sent "
2397 "'%s'", client_list, server_list);
2398 destroy_pool(tmp_pool);
2399 return -1;
2400 }
2401
2402 client_list = kex->client_names->c2s_encrypt_algo;
2403 server_list = kex->server_names->c2s_encrypt_algo;
2404
2405 pr_trace_msg(trace_channel, 8, "client-sent client encryption algorithms: %s",
2406 client_list);
2407 pr_trace_msg(trace_channel, 8, "server-sent client encryption algorithms: %s",
2408 server_list);
2409
2410 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2411 if (shared != NULL) {
2412 if (setup_c2s_encrypt_algo(kex, shared) < 0) {
2413 destroy_pool(tmp_pool);
2414 return -1;
2415 }
2416
2417 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2418 " + Session client-to-server encryption: %s", shared);
2419 pr_trace_msg(trace_channel, 20,
2420 "session client-to-server encryption algorithm: %s", shared);
2421
2422 } else {
2423 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2424 "no shared client-to-server encryption algorithm found (client sent '%s',"
2425 " server sent '%s')", client_list, server_list);
2426 destroy_pool(tmp_pool);
2427 return -1;
2428 }
2429
2430 client_list = kex->client_names->s2c_encrypt_algo;
2431 server_list = kex->server_names->s2c_encrypt_algo;
2432
2433 pr_trace_msg(trace_channel, 8, "client-sent server encryption algorithms: %s",
2434 client_list);
2435 pr_trace_msg(trace_channel, 8, "server-sent server encryption algorithms: %s",
2436 server_list);
2437
2438 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2439 if (shared != NULL) {
2440 if (setup_s2c_encrypt_algo(kex, shared) < 0) {
2441 destroy_pool(tmp_pool);
2442 return -1;
2443 }
2444
2445 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2446 " + Session server-to-client encryption: %s", shared);
2447 pr_trace_msg(trace_channel, 20,
2448 "session server-to-client encryption algorithm: %s", shared);
2449
2450 } else {
2451 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2452 "no shared server-to-client encryption algorithm found (client sent '%s',"
2453 " server sent '%s')", client_list, server_list);
2454 destroy_pool(tmp_pool);
2455 return -1;
2456 }
2457
2458 client_list = kex->client_names->c2s_mac_algo;
2459 server_list = kex->server_names->c2s_mac_algo;
2460
2461 pr_trace_msg(trace_channel, 8, "client-sent client MAC algorithms: %s",
2462 client_list);
2463 pr_trace_msg(trace_channel, 8, "server-sent client MAC algorithms: %s",
2464 server_list);
2465
2466 /* Ignore MAC/digests when authenticated encryption algorithms are used. */
2467 if (sftp_cipher_get_read_auth_size2() == 0) {
2468 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2469 if (shared != NULL) {
2470 if (setup_c2s_mac_algo(kex, shared) < 0) {
2471 destroy_pool(tmp_pool);
2472 return -1;
2473 }
2474
2475 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2476 " + Session client-to-server MAC: %s", shared);
2477 pr_trace_msg(trace_channel, 20,
2478 "session client-to-server MAC algorithm: %s", shared);
2479
2480 } else {
2481 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2482 "no shared client-to-server MAC algorithm found (client sent '%s', "
2483 "server sent '%s')", client_list, server_list);
2484 destroy_pool(tmp_pool);
2485 return -1;
2486 }
2487
2488 } else {
2489 pr_trace_msg(trace_channel, 8, "ignoring MAC algorithms due to use of "
2490 "client-to-server authenticated cipher algorithm '%s'",
2491 kex->session_names->c2s_encrypt_algo);
2492 pr_trace_msg(trace_channel, 20,
2493 "session client-to-server MAC algorithm: <implicit>");
2494 }
2495
2496 client_list = kex->client_names->s2c_mac_algo;
2497 server_list = kex->server_names->s2c_mac_algo;
2498
2499 pr_trace_msg(trace_channel, 8, "client-sent server MAC algorithms: %s",
2500 client_list);
2501 pr_trace_msg(trace_channel, 8, "server-sent server MAC algorithms: %s",
2502 server_list);
2503
2504 /* Ignore MAC/digests when authenticated encryption algorithms are used. */
2505 if (sftp_cipher_get_write_auth_size2() == 0) {
2506 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2507 if (shared != NULL) {
2508 if (setup_s2c_mac_algo(kex, shared) < 0) {
2509 destroy_pool(tmp_pool);
2510 return -1;
2511 }
2512
2513 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2514 " + Session server-to-client MAC: %s", shared);
2515 pr_trace_msg(trace_channel, 20,
2516 "session server-to-client MAC algorithm: %s", shared);
2517
2518 } else {
2519 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2520 "no shared server-to-client MAC algorithm found (client sent '%s', "
2521 "server sent '%s')", client_list, server_list);
2522 destroy_pool(tmp_pool);
2523 return -1;
2524 }
2525
2526 } else {
2527 pr_trace_msg(trace_channel, 8, "ignoring MAC algorithms due to use of "
2528 "server-to-client authenticated cipher algorithm '%s'",
2529 kex->session_names->s2c_encrypt_algo);
2530 pr_trace_msg(trace_channel, 20,
2531 "session server-to-client MAC algorithm: <implicit>");
2532 }
2533
2534 client_list = kex->client_names->c2s_comp_algo;
2535 server_list = kex->server_names->c2s_comp_algo;
2536
2537 pr_trace_msg(trace_channel, 8,
2538 "client-sent client compression algorithms: %s", client_list);
2539 pr_trace_msg(trace_channel, 8,
2540 "server-sent client compression algorithms: %s", server_list);
2541
2542 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2543 if (shared != NULL) {
2544 if (setup_c2s_comp_algo(kex, shared) < 0) {
2545 destroy_pool(tmp_pool);
2546 return -1;
2547 }
2548
2549 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2550 " + Session client-to-server compression: %s", shared);
2551 pr_trace_msg(trace_channel, 20,
2552 "session client-to-server compression algorithm: %s", shared);
2553
2554 } else {
2555 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2556 "no shared client-to-server compression algorithm found (client sent "
2557 "'%s', server sent '%s'", client_list, server_list);
2558 destroy_pool(tmp_pool);
2559 return -1;
2560 }
2561
2562 client_list = kex->client_names->s2c_comp_algo;
2563 server_list = kex->server_names->s2c_comp_algo;
2564
2565 pr_trace_msg(trace_channel, 8,
2566 "client-sent server compression algorithms: %s", client_list);
2567 pr_trace_msg(trace_channel, 8,
2568 "server-sent server compression algorithms: %s", server_list);
2569
2570 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2571 if (shared != NULL) {
2572 if (setup_s2c_comp_algo(kex, shared) < 0) {
2573 destroy_pool(tmp_pool);
2574 return -1;
2575 }
2576
2577 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2578 " + Session server-to-client compression: %s", shared);
2579 pr_trace_msg(trace_channel, 20,
2580 "session server-to-client compression algorithm: %s", shared);
2581
2582 } else {
2583 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2584 "no shared server-to-client compression algorithm found (client sent "
2585 "'%s', server sent '%s'", client_list, server_list);
2586 destroy_pool(tmp_pool);
2587 return -1;
2588 }
2589
2590 client_list = kex->client_names->c2s_lang;
2591 server_list = kex->server_names->c2s_lang;
2592
2593 pr_trace_msg(trace_channel, 8,
2594 "client-sent client languages: %s", client_list);
2595 pr_trace_msg(trace_channel, 8,
2596 "server-sent client languages: %s", client_list);
2597
2598 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2599 if (shared != NULL) {
2600 if (setup_c2s_lang(kex, shared) < 0) {
2601 destroy_pool(tmp_pool);
2602 return -1;
2603 }
2604
2605 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2606 " + Session client-to-server language: %s", shared);
2607 pr_trace_msg(trace_channel, 20,
2608 "session client-to-server language: %s", shared);
2609
2610 /* XXX Do not error out if there are no shared languages yet. */
2611 #if 0
2612 } else {
2613 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2614 "no shared client-to-server language found (client sent '%s', server "
2615 "sent '%s'", client_list, server_list);
2616 destroy_pool(tmp_pool);
2617 return -1;
2618 #endif
2619 }
2620
2621 client_list = kex->client_names->s2c_lang;
2622 server_list = kex->server_names->s2c_lang;
2623
2624 pr_trace_msg(trace_channel, 8,
2625 "client-sent server languages: %s", client_list);
2626 pr_trace_msg(trace_channel, 8,
2627 "server-sent server languages: %s", client_list);
2628
2629 shared = sftp_misc_namelist_shared(kex->pool, client_list, server_list);
2630 if (shared != NULL) {
2631 if (setup_s2c_lang(kex, shared) < 0) {
2632 destroy_pool(tmp_pool);
2633 return -1;
2634 }
2635
2636 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2637 " + Session server-to-client language: %s", shared);
2638 pr_trace_msg(trace_channel, 20,
2639 "session server-to-client language: %s", shared);
2640
2641 /* XXX Do not error out if there are no shared languages yet. */
2642 #if 0
2643 } else {
2644 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2645 "no shared server-to-client language found (client sent '%s', server "
2646 "sent '%s'", client_list, server_list);
2647 destroy_pool(tmp_pool);
2648 return -1;
2649 #endif
2650 }
2651
2652 /* Now that we've finished setting up the other bits, we can set up the
2653 * kex algo.
2654 */
2655 if (setup_kex_algo(kex, kex_algo) < 0) {
2656 destroy_pool(tmp_pool);
2657 return -1;
2658 }
2659
2660 destroy_pool(tmp_pool);
2661 return 0;
2662 }
2663
2664 static int read_kexinit(struct ssh2_packet *pkt, struct sftp_kex *kex) {
2665 unsigned char *buf;
2666 char *list;
2667 uint32_t buflen;
2668
2669 buf = pkt->payload;
2670 buflen = pkt->payload_len;
2671
2672 /* Make a copy of the payload for later. */
2673 kex->client_kexinit_payload = palloc(kex->pool, pkt->payload_len);
2674 kex->client_kexinit_payload_len = pkt->payload_len;
2675 memcpy(kex->client_kexinit_payload, pkt->payload, pkt->payload_len);
2676
2677 /* Read the cookie, which is a mandated length of 16 bytes. */
2678 (void) sftp_msg_read_data(pkt->pool, &buf, &buflen, 16);
2679
2680 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2681 kex->client_names->kex_algo = list;
2682
2683 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2684 kex->client_names->server_hostkey_algo = list;
2685
2686 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2687 kex->client_names->c2s_encrypt_algo = list;
2688
2689 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2690 kex->client_names->s2c_encrypt_algo = list;
2691
2692 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2693 kex->client_names->c2s_mac_algo = list;
2694
2695 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2696 kex->client_names->s2c_mac_algo = list;
2697
2698 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2699 kex->client_names->c2s_comp_algo = list;
2700
2701 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2702 kex->client_names->s2c_comp_algo = list;
2703
2704 /* Client-to-server languages */
2705 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2706 kex->client_names->c2s_lang = list;
2707
2708 /* Server-to-client languages */
2709 list = sftp_msg_read_string(kex->pool, &buf, &buflen);
2710 kex->client_names->s2c_lang = list;
2711
2712 /* Read the "first kex packet follows" byte */
2713 kex->first_kex_follows = sftp_msg_read_bool(pkt->pool, &buf, &buflen);
2714
2715 pr_trace_msg(trace_channel, 3, "first kex packet follows = %s",
2716 kex->first_kex_follows ? "true" : "false");
2717
2718 /* Reserved flags */
2719 (void) sftp_msg_read_int(pkt->pool, &buf, &buflen);
2720
2721 return 0;
2722 }
2723
2724 static int write_kexinit(struct ssh2_packet *pkt, struct sftp_kex *kex) {
2725 unsigned char cookie[16];
2726 unsigned char *buf, *ptr;
2727 const char *list;
2728 uint32_t bufsz, buflen;
2729
2730 /* XXX Always have empty language lists; we really don't care. */
2731 const char *langs = "";
2732
2733 bufsz = buflen = sizeof(char) +
2734 sizeof(cookie) +
2735 sizeof(uint32_t) + strlen(kex->server_names->kex_algo) +
2736 sizeof(uint32_t) + strlen(kex->server_names->server_hostkey_algo) +
2737 sizeof(uint32_t) + strlen(kex->server_names->c2s_encrypt_algo) +
2738 sizeof(uint32_t) + strlen(kex->server_names->s2c_encrypt_algo) +
2739 sizeof(uint32_t) + strlen(kex->server_names->c2s_mac_algo) +
2740 sizeof(uint32_t) + strlen(kex->server_names->s2c_mac_algo) +
2741 sizeof(uint32_t) + strlen(kex->server_names->c2s_comp_algo) +
2742 sizeof(uint32_t) + strlen(kex->server_names->s2c_comp_algo) +
2743 sizeof(uint32_t) + strlen(langs) +
2744 sizeof(uint32_t) + strlen(langs) +
2745 sizeof(char) +
2746 sizeof(uint32_t);
2747
2748 ptr = buf = pcalloc(pkt->pool, bufsz);
2749
2750 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
2751
2752 /* Try first to use cryptographically secure bytes for the cookie.
2753 * If that fails (e.g. if the PRNG hasn't been seeded well), use
2754 * pseudo-cryptographically secure bytes.
2755 */
2756 memset(cookie, 0, sizeof(cookie));
2757 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
2758 RAND_bytes(cookie, sizeof(cookie));
2759 #else
2760 if (RAND_bytes(cookie, sizeof(cookie)) != 1) {
2761 RAND_pseudo_bytes(cookie, sizeof(cookie));
2762 }
2763 #endif /* prior to OpenSSL-1.1.0 */
2764
2765 sftp_msg_write_data(&buf, &buflen, cookie, sizeof(cookie), FALSE);
2766
2767 list = kex->server_names->kex_algo;
2768 sftp_msg_write_string(&buf, &buflen, list);
2769
2770 list = kex->server_names->server_hostkey_algo;
2771 sftp_msg_write_string(&buf, &buflen, list);
2772
2773 list = kex->server_names->c2s_encrypt_algo;
2774 sftp_msg_write_string(&buf, &buflen, list);
2775
2776 list = kex->server_names->s2c_encrypt_algo;
2777 sftp_msg_write_string(&buf, &buflen, list);
2778
2779 list = kex->server_names->c2s_mac_algo;
2780 sftp_msg_write_string(&buf, &buflen, list);
2781
2782 list = kex->server_names->s2c_mac_algo;
2783 sftp_msg_write_string(&buf, &buflen, list);
2784
2785 list = kex->server_names->c2s_comp_algo;
2786 sftp_msg_write_string(&buf, &buflen, list);
2787
2788 list = kex->server_names->s2c_comp_algo;
2789 sftp_msg_write_string(&buf, &buflen, list);
2790
2791 /* XXX Need to support langs here. */
2792 sftp_msg_write_string(&buf, &buflen, langs);
2793 sftp_msg_write_string(&buf, &buflen, langs);
2794
2795 /* We don't try to optimistically guess what algorithms the client would
2796 * use and send a preemptive kex packet.
2797 */
2798 sftp_msg_write_bool(&buf, &buflen, FALSE);
2799 sftp_msg_write_int(&buf, &buflen, 0);
2800
2801 pkt->payload = ptr;
2802 pkt->payload_len = (bufsz - buflen);
2803
2804 /* Make a copy of the payload for later. Skip past the first byte, which
2805 * is the KEXINIT identifier.
2806 */
2807 kex->server_kexinit_payload_len = pkt->payload_len - 1;
2808 kex->server_kexinit_payload = palloc(kex->pool, pkt->payload_len - 1);
2809 memcpy(kex->server_kexinit_payload, pkt->payload + 1, pkt->payload_len - 1);
2810
2811 return 0;
2812 }
2813
2814 static int read_dh_init(struct ssh2_packet *pkt, struct sftp_kex *kex) {
2815 unsigned char *buf;
2816 uint32_t buflen;
2817
2818 buf = pkt->payload;
2819 buflen = pkt->payload_len;
2820
2821 /* Read in 'e' */
2822 kex->e = sftp_msg_read_mpint(pkt->pool, &buf, &buflen);
2823 if (kex->e == NULL) {
2824 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2825 "error reading DH_INIT: %s", strerror(errno));
2826 return -1;
2827 }
2828
2829 return 0;
2830 }
2831
2832 /* Only set the given note (and environment) variable/value IFF it is not
2833 * already present.
2834 */
2835 static void set_note_var(pool *p, const char *k, const char *v) {
2836 const char *val;
2837 int have_val = FALSE;
2838
2839 val = pr_env_get(p, k);
2840 if (val != NULL) {
2841 if (strcmp(val, v) == 0) {
2842 have_val = TRUE;
2843 }
2844 }
2845
2846 if (have_val == FALSE) {
2847 k = pstrdup(p, k);
2848 v = pstrdup(p, v);
2849 pr_env_unset(p, k);
2850 pr_env_set(p, k, v);
2851 }
2852
2853 (void) pr_table_add_dup(session.notes, k, v, 0);
2854 }
2855
2856 static int set_session_keys(struct sftp_kex *kex) {
2857 unsigned char *buf, *ptr;
2858 uint32_t buflen, bufsz, klen;
2859 int comp_read_flags, comp_write_flags;
2860
2861 /* To date, the kex algo that has generated the largest K that I have
2862 * seen so far is "diffie-hellman-group18-sha512".
2863 */
2864 bufsz = buflen = 2048;
2865 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
2866
2867 /* Need to use SSH2-style format of K for the key. */
2868 klen = sftp_msg_write_mpint(&buf, &buflen, kex->k);
2869
2870 if (sftp_cipher_set_read_key(kex_pool, kex->hash, ptr, klen, kex->h,
2871 kex->hlen, SFTP_ROLE_SERVER) < 0) {
2872 pr_memscrub(ptr, bufsz);
2873 return -1;
2874 }
2875
2876 if (sftp_cipher_set_write_key(kex_pool, kex->hash, ptr, klen, kex->h,
2877 kex->hlen, SFTP_ROLE_SERVER) < 0) {
2878 pr_memscrub(ptr, bufsz);
2879 return -1;
2880 }
2881
2882 if (sftp_mac_set_read_key(kex_pool, kex->hash, ptr, klen, kex->h,
2883 kex->hlen, SFTP_ROLE_SERVER) < 0) {
2884 pr_memscrub(ptr, bufsz);
2885 return -1;
2886 }
2887
2888 if (sftp_mac_set_write_key(kex_pool, kex->hash, ptr, klen, kex->h,
2889 kex->hlen, SFTP_ROLE_SERVER) < 0) {
2890 pr_memscrub(ptr, bufsz);
2891 return -1;
2892 }
2893
2894 pr_memscrub(ptr, bufsz);
2895
2896 comp_read_flags = comp_write_flags = SFTP_COMPRESS_FL_NEW_KEY;
2897
2898 /* If we are rekeying, AND the existing compression is "delayed", then
2899 * we need to use slightly different compression flags.
2900 */
2901 if (kex_rekey_kex != NULL) {
2902 const char *algo;
2903
2904 algo = sftp_compress_get_read_algo();
2905 if (strcmp(algo, "[email protected]") == 0) {
2906 comp_read_flags = SFTP_COMPRESS_FL_AUTHENTICATED;
2907 }
2908
2909 algo = sftp_compress_get_write_algo();
2910 if (strcmp(algo, "[email protected]") == 0) {
2911 comp_write_flags = SFTP_COMPRESS_FL_AUTHENTICATED;
2912 }
2913 }
2914
2915 if (sftp_compress_init_read(comp_read_flags) < 0) {
2916 return -1;
2917 }
2918
2919 if (sftp_compress_init_write(comp_write_flags) < 0) {
2920 return -1;
2921 }
2922
2923 set_note_var(session.pool, "SFTP_CLIENT_CIPHER_ALGO",
2924 sftp_cipher_get_read_algo());
2925 set_note_var(session.pool, "SFTP_SERVER_CIPHER_ALGO",
2926 sftp_cipher_get_write_algo());
2927
2928 if (sftp_cipher_get_read_auth_size2() == 0) {
2929 set_note_var(session.pool, "SFTP_CLIENT_MAC_ALGO",
2930 sftp_mac_get_read_algo());
2931
2932 } else {
2933 set_note_var(session.pool, "SFTP_CLIENT_MAC_ALGO", "implicit");
2934 }
2935
2936 if (sftp_cipher_get_write_auth_size2() == 0) {
2937 set_note_var(session.pool, "SFTP_SERVER_MAC_ALGO",
2938 sftp_mac_get_write_algo());
2939
2940 } else {
2941 set_note_var(session.pool, "SFTP_SERVER_MAC_ALGO", "implicit");
2942 }
2943
2944 set_note_var(session.pool, "SFTP_CLIENT_COMPRESSION_ALGO",
2945 sftp_compress_get_read_algo());
2946 set_note_var(session.pool, "SFTP_SERVER_COMPRESSION_ALGO",
2947 sftp_compress_get_write_algo());
2948 set_note_var(session.pool, "SFTP_KEX_ALGO",
2949 kex->session_names->kex_algo);
2950 set_note_var(session.pool, "SFTP_HOST_KEY_ALGO",
2951 kex->session_names->server_hostkey_algo);
2952
2953 if (kex_rekey_interval > 0 &&
2954 kex_rekey_timerno == -1) {
2955 /* Register the rekey timer. */
2956 kex_rekey_timerno = pr_timer_add(kex_rekey_interval, -1,
2957 &sftp_module, kex_rekey_timer_cb, "SFTP KEX Rekey timer");
2958 }
2959
2960 if (kex_rekey_timeout > 0 &&
2961 kex_rekey_timeout_timerno > 0) {
2962 pr_timer_remove(kex_rekey_timeout_timerno, &sftp_module);
2963 kex_rekey_timeout_timerno = -1;
2964 }
2965
2966 if (kex_rekey_kex != NULL) {
2967 pr_trace_msg("ssh2", 3, "rekey KEX completed");
2968 }
2969
2970 sftp_ssh2_packet_rekey_reset();
2971 kex_rekey_kex = NULL;
2972
2973 return 0;
2974 }
2975
2976 static int write_dh_reply(struct ssh2_packet *pkt, struct sftp_kex *kex) {
2977 const unsigned char *h;
2978 const unsigned char *hostkey_data, *hsig;
2979 unsigned char *buf, *ptr;
2980 uint32_t bufsz, buflen, hlen = 0, hostkey_datalen = 0;
2981 size_t dhlen, hsiglen;
2982 const BIGNUM *k = NULL, *dh_pub_key = NULL;
2983 int res;
2984
2985 /* Compute the shared secret */
2986 dhlen = DH_size(kex->dh);
2987 buf = palloc(pkt->pool, dhlen);
2988
2989 pr_trace_msg(trace_channel, 12, "computing DH key");
2990 res = DH_compute_key((unsigned char *) buf, kex->e, kex->dh);
2991 if (res < 0) {
2992 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2993 "error computing DH shared secret: %s", sftp_crypto_get_errors());
2994 return -1;
2995 }
2996
2997 k = BN_new();
2998 if (BN_bin2bn((unsigned char *) buf, res, (BIGNUM *) k) == NULL) {
2999 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3000 "error converting DH shared secret to BN: %s", sftp_crypto_get_errors());
3001 pr_memscrub(buf, res);
3002 return -1;
3003 }
3004
3005 pr_memscrub(buf, res);
3006 kex->k = k;
3007
3008 /* Get the hostkey data; it will be part of the data we hash in order
3009 * to create the session key.
3010 */
3011 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
3012 &hostkey_datalen);
3013 if (hostkey_data == NULL) {
3014 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3015 "error converting hostkey for signing: %s", strerror(errno));
3016
3017 BN_clear_free((BIGNUM *) kex->k);
3018 kex->k = NULL;
3019 return -1;
3020 }
3021
3022 /* Calculate H */
3023 h = calculate_h(kex, hostkey_data, hostkey_datalen, k, &hlen);
3024 if (h == NULL) {
3025 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3026 BN_clear_free((BIGNUM *) kex->k);
3027 kex->k = NULL;
3028 return -1;
3029 }
3030
3031 kex->h = palloc(kex->pool, hlen);
3032 kex->hlen = hlen;
3033 memcpy((char *) kex->h, h, kex->hlen);
3034
3035 /* Save H as the session ID */
3036 sftp_session_set_id(h, hlen);
3037
3038 /* Sign H with our hostkey */
3039 hsig = sftp_keys_sign_data(pkt->pool, kex->use_hostkey_type, h, hlen,
3040 &hsiglen);
3041 if (hsig == NULL) {
3042 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "error signing H");
3043 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3044 BN_clear_free((BIGNUM *) kex->k);
3045 kex->k = NULL;
3046 return -1;
3047 }
3048
3049 /* XXX Is this large enough? Too large? */
3050 buflen = bufsz = 8192;
3051 ptr = buf = palloc(pkt->pool, bufsz);
3052
3053 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEX_DH_REPLY);
3054 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
3055
3056 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3057 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3058 DH_get0_key(kex->dh, &dh_pub_key, NULL);
3059 #else
3060 dh_pub_key = kex->dh->pub_key;
3061 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3062 sftp_msg_write_mpint(&buf, &buflen, dh_pub_key);
3063
3064 sftp_msg_write_data(&buf, &buflen, hsig, hsiglen, TRUE);
3065
3066 /* Scrub any sensitive data when done */
3067 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3068 pr_memscrub((char *) hsig, hsiglen);
3069
3070 pkt->payload = ptr;
3071 pkt->payload_len = (bufsz - buflen);
3072
3073 return 0;
3074 }
3075
3076 static int write_newkeys_reply(struct ssh2_packet *pkt) {
3077 unsigned char *buf, *ptr;
3078 uint32_t bufsz, buflen;
3079
3080 /* Write out the NEWKEYS message. */
3081 bufsz = buflen = 1;
3082 ptr = buf = palloc(pkt->pool, bufsz);
3083
3084 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_NEWKEYS);
3085
3086 pkt->payload = ptr;
3087 pkt->payload_len = (bufsz - buflen);
3088
3089 return 0;
3090 }
3091
3092 static int write_ext_info_server_sig_algs(struct ssh2_packet *pkt,
3093 unsigned char **buf, uint32_t *buflen) {
3094 char *sig_algs = "";
3095
3096 #if defined(HAVE_X448_OPENSSL)
3097 sig_algs = pstrcat(pkt->pool, sig_algs, *sig_algs ? "," : "", "ssh-ed448",
3098 NULL);
3099 #endif /* HAVE_X448_OPENSSL */
3100
3101 #if defined(PR_USE_SODIUM)
3102 sig_algs = pstrcat(pkt->pool, sig_algs, *sig_algs ? "," : "", "ssh-ed25519",
3103 NULL);
3104 #endif /* PR_USE_SODIUM */
3105
3106 #if defined(HAVE_SHA256_OPENSSL)
3107 sig_algs = pstrcat(pkt->pool, sig_algs, *sig_algs ? "," : "", "rsa-sha2-256",
3108 NULL);
3109 #endif /* HAVE_SHA256_OPENSSL */
3110
3111 #if defined(HAVE_SHA512_OPENSSL)
3112 sig_algs = pstrcat(pkt->pool, sig_algs, *sig_algs ? "," : "", "rsa-sha2-512",
3113 NULL);
3114 #endif /* HAVE_SHA512_OPENSSL */
3115
3116 #if defined(PR_USE_OPENSSL_ECC)
3117 sig_algs = pstrcat(pkt->pool, sig_algs, *sig_algs ? "," : "",
3118 "ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521", NULL);
3119 #endif /* PR_USE_OPENSSL_ECC */
3120
3121 sig_algs = pstrcat(pkt->pool, sig_algs, *sig_algs ? "," : "", "ssh-rsa",
3122 NULL);
3123 #if !defined(OPENSSL_NO_DSA)
3124 sig_algs = pstrcat(pkt->pool, sig_algs, ",", "ssh-dss", NULL);
3125 #endif /* OPENSSL_NO_DSA */
3126
3127 pr_trace_msg(trace_channel, 11,
3128 "writing 'server-sig-algs' EXT_INFO extension: %s", sig_algs);
3129
3130 sftp_msg_write_string(buf, buflen, "server-sig-algs");
3131 sftp_msg_write_string(buf, buflen, sig_algs);
3132
3133 return 0;
3134 }
3135
3136 static int write_ext_info(struct ssh2_packet *pkt) {
3137 unsigned char *buf, *ptr;
3138 uint32_t bufsz, buflen, ext_count = 1;
3139
3140 /* XXX Is this large enough? Too large? */
3141 buflen = bufsz = 8192;
3142 ptr = buf = palloc(pkt->pool, bufsz);
3143
3144 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_EXT_INFO);
3145
3146 /* TODO: Consider supporting the "no-flow-control" extension. */
3147
3148 ext_count = 1;
3149 sftp_msg_write_int(&buf, &buflen, ext_count);
3150 write_ext_info_server_sig_algs(pkt, &buf, &buflen);
3151
3152 pkt->payload = ptr;
3153 pkt->payload_len = (bufsz - buflen);
3154
3155 return 0;
3156 }
3157
3158 static int handle_kex_dh(struct ssh2_packet *pkt, struct sftp_kex *kex) {
3159 int res;
3160 cmd_rec *cmd;
3161
3162 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "DH_INIT"));
3163 cmd->arg = "(data)";
3164 cmd->cmd_class = CL_SEC|CL_SSH;
3165 cmd->cmd_id = SFTP_CMD_ID;
3166
3167 pr_trace_msg(trace_channel, 9, "reading DH_INIT message from client");
3168
3169 res = read_dh_init(pkt, kex);
3170 if (res < 0) {
3171 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
3172
3173 destroy_pool(pkt->pool);
3174 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3175 }
3176
3177 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
3178 destroy_pool(pkt->pool);
3179
3180 pr_trace_msg(trace_channel, 9, "writing DH_INIT message to client");
3181
3182 /* Send our key exchange reply. */
3183 pkt = sftp_ssh2_packet_create(kex_pool);
3184 res = write_dh_reply(pkt, kex);
3185 if (res < 0) {
3186 destroy_pool(pkt->pool);
3187 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3188 }
3189
3190 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
3191 if (res < 0) {
3192 destroy_pool(pkt->pool);
3193 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3194 }
3195
3196 destroy_pool(pkt->pool);
3197 return 0;
3198 }
3199
3200 static int read_dh_gex(struct ssh2_packet *pkt, uint32_t *min, uint32_t *pref,
3201 uint32_t *max, int old_request) {
3202 unsigned char *buf;
3203 uint32_t buflen;
3204
3205 buf = pkt->payload;
3206 buflen = pkt->payload_len;
3207
3208 if (!old_request) {
3209 *min = sftp_msg_read_int(pkt->pool, &buf, &buflen);
3210 if (*min < SFTP_KEX_DH_GROUP_MIN) {
3211 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3212 "DH_GEX_REQUEST min value (%lu) too small (< %lu)",
3213 (unsigned long) *min, (unsigned long) SFTP_KEX_DH_GROUP_MIN);
3214 return -1;
3215 }
3216
3217 *pref = sftp_msg_read_int(pkt->pool, &buf, &buflen);
3218
3219 *max = sftp_msg_read_int(pkt->pool, &buf, &buflen);
3220 if (*max > SFTP_KEX_DH_GROUP_MAX) {
3221 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3222 "DH_GEX_REQUEST max value (%lu) too large (> %lu)",
3223 (unsigned long) *max, (unsigned long) SFTP_KEX_DH_GROUP_MAX);
3224 return -1;
3225 }
3226
3227 } else {
3228 *min = SFTP_KEX_DH_GROUP_MIN;
3229 *pref = sftp_msg_read_int(pkt->pool, &buf, &buflen);
3230 *max = SFTP_KEX_DH_GROUP_MAX;
3231 }
3232
3233 if (*max < *min ||
3234 *pref < *min ||
3235 *pref > *max) {
3236 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3237 "bad DH_GEX_REQUEST parameters: min = %lu, pref = %lu, max = %lu",
3238 (unsigned long) *min, (unsigned long) *pref, (unsigned long) *max);
3239 return -1;
3240 }
3241
3242 return 0;
3243 }
3244
3245 static int get_dh_gex_group(struct sftp_kex *kex, uint32_t min,
3246 uint32_t pref, uint32_t max) {
3247 const char *dhparam_path;
3248 config_rec *c;
3249 int use_fixed_modulus = FALSE;
3250
3251 dhparam_path = PR_CONFIG_DIR "/dhparams.pem";
3252 c = find_config(main_server->conf, CONF_PARAM, "SFTPDHParamFile", FALSE);
3253 if (c != NULL) {
3254 dhparam_path = c->argv[0];
3255 }
3256
3257 /* If the preferred DH is less than SFTP_DH_PREF_MIN_LEN, AND the AllowWeakDH
3258 * SFTPOption is not used, then use a pref of SFTP_DH_PREF_MIN_LEN (Bug#4184).
3259 */
3260 if (pref < SFTP_DH_PREF_MIN_LEN) {
3261 if (sftp_opts & SFTP_OPT_ALLOW_WEAK_DH) {
3262 pr_trace_msg(trace_channel, 14,
3263 "client prefers relatively weak DH group size (%lu) but AllowWeakDH "
3264 "SFTPOption in effect", (unsigned long) pref);
3265
3266 } else {
3267 pref = SFTP_DH_PREF_MIN_LEN;
3268 /* Make sure we don't exceed the client's max supported size. */
3269 if (max <= SFTP_DH_PREF_MIN_LEN) {
3270 pr_trace_msg(trace_channel, 14,
3271 "client prefers relatively weak DH group size (%lu), adjusting "
3272 "preferred size to our preferred size (%lu)", (unsigned long) pref,
3273 (unsigned long) SFTP_DH_PREF_MIN_LEN);
3274 pref = SFTP_DH_PREF_MIN_LEN;
3275
3276 } else {
3277 pr_trace_msg(trace_channel, 14,
3278 "client prefers relatively weak DH group size (%lu), and does not "
3279 "handle our preferred size (%lu)", (unsigned long) pref,
3280 (unsigned long) SFTP_DH_PREF_MIN_LEN);
3281 }
3282 }
3283 }
3284
3285 if (dhparam_path != NULL) {
3286 if (kex_dhparams_fp != NULL) {
3287 /* Rewind to the start of the file. */
3288 fseek(kex_dhparams_fp, 0, SEEK_SET);
3289
3290 } else {
3291 kex_dhparams_fp = fopen(dhparam_path, "r");
3292 }
3293
3294 if (kex_dhparams_fp) {
3295 register unsigned int i;
3296 pool *tmp_pool;
3297 array_header *smaller_dhs, *pref_dhs, *larger_dhs;
3298 DH *chosen_dh, **dhs;
3299 uint32_t smaller_dh_nbits = 0, larger_dh_nbits = 0;
3300
3301 pr_trace_msg(trace_channel, 15,
3302 "using DH parameters from SFTPDHParamFile '%s' for group exchange",
3303 dhparam_path);
3304
3305 tmp_pool = make_sub_pool(kex->pool);
3306 pr_pool_tag(tmp_pool, "Kex DHparams selection pool");
3307
3308 smaller_dhs = make_array(tmp_pool, 1, sizeof(DH *));
3309 pref_dhs = make_array(tmp_pool, 1, sizeof(DH *));
3310 larger_dhs = make_array(tmp_pool, 1, sizeof(DH *));
3311
3312 /* From Section 3 of RFC4419:
3313 *
3314 * "The server should return the smallest group it knows that is larger
3315 * than the size the client requested. If the server does not know a
3316 * group that is larger than the client request, then it SHOULD return
3317 * the largest group it knows. In all cases, the size of the returned
3318 * group SHOULD be at least 1024 bits."
3319 *
3320 * Make lists of DHs in the param file whose size falls within the
3321 * bit lengths requested by the client. Note that DH_size() returns
3322 * the sizes _in bytes_, not bits. We have three lists: one for DHs
3323 * which match the client-requested preferred size, one for DHs which
3324 * are smaller than the preferred size, and one for DHs which are larger
3325 * than the preferred size. DHs in these last two lists will be of the
3326 * same size. Once the lists are populated, we will randomly choose one
3327 * from the preferred DH list (if available), else one from the larger
3328 * DH list (if available), else one from the smaller DH list.
3329 */
3330
3331 while (TRUE) {
3332 uint32_t nbits;
3333
3334 pr_signals_handle();
3335
3336 chosen_dh = PEM_read_DHparams(kex_dhparams_fp, NULL, NULL, NULL);
3337 if (chosen_dh == NULL) {
3338 if (!feof(kex_dhparams_fp)) {
3339 pr_trace_msg(trace_channel, 5, "error reading DH params from "
3340 "SFTPDHParamFile '%s': %s", dhparam_path,
3341 sftp_crypto_get_errors());
3342 }
3343
3344 break;
3345 }
3346
3347 nbits = DH_size(chosen_dh) * 8;
3348
3349 if (nbits < min ||
3350 nbits > max) {
3351 pr_trace_msg(trace_channel, 17,
3352 "skipping %lu-bit DH from %s (exceeds min %lu, max %lu bits)",
3353 (unsigned long) nbits, dhparam_path, (unsigned long) min,
3354 (unsigned long) max);
3355 DH_free(chosen_dh);
3356 continue;
3357 }
3358
3359 if (nbits == pref) {
3360 *((DH **) push_array(pref_dhs)) = chosen_dh;
3361
3362 } else if (nbits < pref) {
3363 if (nbits > smaller_dh_nbits) {
3364 if (smaller_dhs->nelts > 0) {
3365 dhs = smaller_dhs->elts;
3366 for (i = 0; i < smaller_dhs->nelts; i++) {
3367 DH_free(dhs[i]);
3368 }
3369
3370 clear_array(smaller_dhs);
3371 }
3372
3373 smaller_dh_nbits = nbits;
3374 *((DH **) push_array(smaller_dhs)) = chosen_dh;
3375
3376 } else if (nbits == smaller_dh_nbits) {
3377 *((DH **) push_array(smaller_dhs)) = chosen_dh;
3378
3379 } else {
3380 DH_free(chosen_dh);
3381 }
3382
3383 } else {
3384 /* By process of elimination, nbits here MUST be > pref. */
3385
3386 if (nbits < larger_dh_nbits) {
3387 if (larger_dhs->nelts > 0) {
3388 dhs = larger_dhs->elts;
3389 for (i = 0; i < larger_dhs->nelts; i++) {
3390 DH_free(dhs[i]);
3391 }
3392
3393 clear_array(larger_dhs);
3394 }
3395
3396 larger_dh_nbits = nbits;
3397 *((DH **) push_array(larger_dhs)) = chosen_dh;
3398
3399 } else if (nbits == larger_dh_nbits) {
3400 *((DH **) push_array(larger_dhs)) = chosen_dh;
3401
3402 } else {
3403 DH_free(chosen_dh);
3404 }
3405 }
3406 }
3407
3408 chosen_dh = NULL;
3409
3410 /* The use of rand(3) below is NOT intended to be perfect, or even
3411 * uniformly distributed. It simply needs to be good enough to pick
3412 * a single item from a small list, where all items are equally
3413 * usable and valid.
3414 *
3415 * Ideally we want to find a preferred DH first. Failing that, a larger
3416 * DH is better; if none found there, then we settle for a smaller DH.
3417 */
3418
3419 if (pref_dhs->nelts > 0) {
3420 int r = (int) (rand() / (RAND_MAX / pref_dhs->nelts + 1));
3421
3422 pr_trace_msg(trace_channel, 17,
3423 "%s DH selection: preferred DHs (count %u, idx %d)", dhparam_path,
3424 pref_dhs->nelts, r);
3425 dhs = pref_dhs->elts;
3426 chosen_dh = dhs[r];
3427
3428 } else if (larger_dhs->nelts > 0) {
3429 int r = (int) (rand() / (RAND_MAX / larger_dhs->nelts + 1));
3430
3431 pr_trace_msg(trace_channel, 17,
3432 "%s DH selection: larger DHs (count %u, idx %d)", dhparam_path,
3433 larger_dhs->nelts, r);
3434 dhs = larger_dhs->elts;
3435 chosen_dh = dhs[r];
3436
3437 } else if (smaller_dhs->nelts > 0) {
3438 int r = (int) (rand() / (RAND_MAX / smaller_dhs->nelts + 1));
3439
3440 pr_trace_msg(trace_channel, 17,
3441 "%s DH selection: smaller DHs (count %u, idx %d)", dhparam_path,
3442 smaller_dhs->nelts, r);
3443 dhs = smaller_dhs->elts;
3444 chosen_dh = dhs[r];
3445
3446 } else {
3447 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3448 "unable to find suitable DH in SFTPDHParamFile '%s' for %lu-%lu "
3449 "bit sizes", dhparam_path, (unsigned long) min, (unsigned long) max);
3450 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3451 "WARNING: using fixed modulus for DH group exchange");
3452 use_fixed_modulus = TRUE;
3453 }
3454
3455 if (chosen_dh != NULL) {
3456 const BIGNUM *dh_p = NULL, *dh_g = NULL, *dup_p, *dup_g;
3457
3458 pr_trace_msg(trace_channel, 20, "client requested min %lu, pref %lu, "
3459 "max %lu sizes for DH group exchange, selected DH of %lu bits",
3460 (unsigned long) min, (unsigned long) pref, (unsigned long) max,
3461 (unsigned long) DH_size(chosen_dh) * 8);
3462
3463 /* Get the P, G parameters of the chosen DH group, and make copies
3464 * of them for our KEX DH.
3465 */
3466
3467 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3468 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3469 DH_get0_pqg(chosen_dh, &dh_p, NULL, &dh_g);
3470 #else
3471 dh_p = chosen_dh->p;
3472 dh_g = chosen_dh->g;
3473 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3474
3475 dup_p = BN_dup(dh_p);
3476 if (dup_p == NULL) {
3477 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3478 "error copying selected DH P: %s", sftp_crypto_get_errors());
3479 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3480 "WARNING: using fixed modulus for DH group exchange");
3481 use_fixed_modulus = TRUE;
3482
3483 } else {
3484 dup_g = BN_dup(dh_g);
3485 if (dup_g == NULL) {
3486 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3487 "error copying selected DH G: %s", sftp_crypto_get_errors());
3488 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3489 "WARNING: using fixed modulus for DH group exchange");
3490 BN_clear_free((BIGNUM *) dup_p);
3491 use_fixed_modulus = TRUE;
3492
3493 } else {
3494 /* Now set those P, G copies into our KEX DH. */
3495 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3496 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3497 DH_set0_pqg(kex->dh, (BIGNUM *) dup_p, NULL, (BIGNUM *) dup_g);
3498 #else
3499 kex->dh->p = dup_p;
3500 kex->dh->g = dup_g;
3501 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3502 }
3503 }
3504 }
3505
3506 /* Don't forget to clean up all of the allocated DHs. */
3507 dhs = (DH **) smaller_dhs->elts;
3508 for (i = 0; i < smaller_dhs->nelts; i++) {
3509 pr_signals_handle();
3510 DH_free(dhs[i]);
3511 }
3512
3513 dhs = (DH **) pref_dhs->elts;
3514 for (i = 0; i < pref_dhs->nelts; i++) {
3515 pr_signals_handle();
3516 DH_free(dhs[i]);
3517 }
3518
3519 dhs = (DH **) larger_dhs->elts;
3520 for (i = 0; i < larger_dhs->nelts; i++) {
3521 pr_signals_handle();
3522 DH_free(dhs[i]);
3523 }
3524
3525 destroy_pool(tmp_pool);
3526
3527 } else {
3528 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3529 "WARNING: unable to read SFTPDHParamFile '%s': %s", dhparam_path,
3530 strerror(errno));
3531 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3532 "WARNING: using fixed modulus for DH group exchange");
3533 use_fixed_modulus = TRUE;
3534 }
3535 }
3536
3537 if (use_fixed_modulus) {
3538 BIGNUM *dh_p, *dh_g;
3539
3540 dh_p = BN_new();
3541
3542 /* Note: Consider using a stronger fixed DH group here! */
3543 if (BN_hex2bn(&dh_p, dh_group14_str) == 0) {
3544 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3545 "error setting DH P: %s", sftp_crypto_get_errors());
3546 BN_clear_free(dh_p);
3547 errno = EACCES;
3548 return -1;
3549 }
3550
3551 dh_g = BN_new();
3552 if (BN_hex2bn(&dh_g, "2") == 0) {
3553 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3554 "error setting DH G: %s", sftp_crypto_get_errors());
3555 BN_clear_free(dh_p);
3556 BN_clear_free(dh_g);
3557 errno = EACCES;
3558 return -1;
3559 }
3560
3561 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3562 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3563 DH_set0_pqg(kex->dh, dh_p, NULL, dh_g);
3564 #else
3565 kex->dh->p = dh_p;
3566 kex->dh->g = dh_g;
3567 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3568 }
3569
3570 return 0;
3571 }
3572
3573 static int write_dh_gex_group(struct ssh2_packet *pkt, struct sftp_kex *kex,
3574 uint32_t min, uint32_t pref, uint32_t max) {
3575 const BIGNUM *dh_p = NULL, *dh_g = NULL;
3576 unsigned char *buf, *ptr;
3577 uint32_t buflen, bufsz;
3578
3579 if (get_dh_gex_group(kex, min, pref, max) < 0) {
3580 return -1;
3581 }
3582
3583 /* XXX Is this large enough? Too large? */
3584 buflen = bufsz = 4096;
3585 ptr = buf = palloc(pkt->pool, bufsz);
3586
3587 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEX_DH_GEX_GROUP);
3588
3589 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3590 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3591 DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
3592 #else
3593 dh_p = kex->dh->p;
3594 dh_g = kex->dh->g;
3595 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3596 sftp_msg_write_mpint(&buf, &buflen, dh_p);
3597 sftp_msg_write_mpint(&buf, &buflen, dh_g);
3598
3599 pkt->payload = ptr;
3600 pkt->payload_len = (bufsz - buflen);
3601
3602 return 0;
3603 }
3604
3605 static int read_dh_gex_init(struct ssh2_packet *pkt, struct sftp_kex *kex) {
3606 unsigned char *buf;
3607 uint32_t buflen;
3608
3609 buf = pkt->payload;
3610 buflen = pkt->payload_len;
3611
3612 /* Read in 'e' */
3613 kex->e = sftp_msg_read_mpint(pkt->pool, &buf, &buflen);
3614 if (kex->e == NULL) {
3615 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3616 "error reading DH_GEX_INIT: %s", strerror(errno));
3617 return -1;
3618 }
3619
3620 return 0;
3621 }
3622
3623 static int write_dh_gex_reply(struct ssh2_packet *pkt, struct sftp_kex *kex,
3624 uint32_t min, uint32_t pref, uint32_t max, int old_request) {
3625 const unsigned char *h, *hostkey_data, *hsig;
3626 unsigned char *buf, *ptr;
3627 uint32_t bufsz, buflen, hlen = 0, hostkey_datalen = 0;
3628 size_t dhlen, hsiglen = 0;
3629 const BIGNUM *k = NULL, *dh_pub_key = NULL;
3630 int res;
3631
3632 /* Compute the shared secret. */
3633 dhlen = DH_size(kex->dh);
3634 buf = palloc(kex_pool, dhlen);
3635
3636 pr_trace_msg(trace_channel, 12, "computing DH key");
3637 res = DH_compute_key(buf, kex->e, kex->dh);
3638 if (res < 0) {
3639 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3640 "error computing DH shared secret: %s", sftp_crypto_get_errors());
3641 return -1;
3642 }
3643
3644 k = BN_new();
3645 if (BN_bin2bn(buf, res, (BIGNUM *) k) == NULL) {
3646 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3647 "error converting DH shared secret to BN: %s", sftp_crypto_get_errors());
3648 pr_memscrub(buf, res);
3649 BN_clear_free((BIGNUM *) k);
3650 return -1;
3651 }
3652
3653 pr_memscrub(buf, res);
3654 kex->k = k;
3655
3656 /* Get the hostkey data; it will be part of the data we hash in order
3657 * to create the session key.
3658 */
3659 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
3660 &hostkey_datalen);
3661 if (hostkey_data == NULL) {
3662 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3663 "error converting hostkey for signing: %s", strerror(errno));
3664 BN_clear_free((BIGNUM *) kex->k);
3665 kex->k = NULL;
3666 return -1;
3667 }
3668
3669 if (old_request) {
3670 max = min = 0;
3671 }
3672
3673 /* Calculate H */
3674 h = calculate_gex_h(kex, hostkey_data, hostkey_datalen, k, min, pref, max,
3675 &hlen);
3676 if (h == NULL) {
3677 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3678 BN_clear_free((BIGNUM *) kex->k);
3679 kex->k = NULL;
3680 return -1;
3681 }
3682
3683 kex->h = palloc(kex->pool, hlen);
3684 kex->hlen = hlen;
3685 memcpy((char *) kex->h, h, kex->hlen);
3686
3687 /* Save H as the session ID */
3688 sftp_session_set_id(h, hlen);
3689
3690 /* Sign H with our hostkey */
3691 hsig = sftp_keys_sign_data(pkt->pool, kex->use_hostkey_type, h, hlen,
3692 &hsiglen);
3693 if (hsig == NULL) {
3694 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "error signing H");
3695 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3696 BN_clear_free((BIGNUM *) kex->k);
3697 kex->k = NULL;
3698 return -1;
3699 }
3700
3701 /* XXX Is this large enough? Too large? */
3702 buflen = bufsz = 8192;
3703 ptr = buf = palloc(pkt->pool, bufsz);
3704
3705 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEX_DH_GEX_REPLY);
3706 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
3707
3708 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3709 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3710 DH_get0_key(kex->dh, &dh_pub_key, NULL);
3711 #else
3712 dh_pub_key = kex->dh->pub_key;
3713 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3714 sftp_msg_write_mpint(&buf, &buflen, dh_pub_key);
3715
3716 sftp_msg_write_data(&buf, &buflen, hsig, hsiglen, TRUE);
3717
3718 /* Scrub any sensitive data when done */
3719 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3720 pr_memscrub((char *) hsig, hsiglen);
3721
3722 pkt->payload = ptr;
3723 pkt->payload_len = (bufsz - buflen);
3724
3725 return 0;
3726 }
3727
3728 static int handle_kex_dh_gex(struct ssh2_packet *pkt, struct sftp_kex *kex,
3729 int old_request) {
3730 int res;
3731 uint32_t min = 0, pref = 0, max = 0;
3732 cmd_rec *cmd;
3733
3734 if (!old_request) {
3735 pr_trace_msg(trace_channel, 9,
3736 "reading DH_GEX_REQUEST message from client");
3737
3738 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "DH_GEX_REQUEST"));
3739 cmd->arg = "(data)";
3740 cmd->cmd_class = CL_SEC|CL_SSH;
3741 cmd->cmd_id = SFTP_CMD_ID;
3742
3743 } else {
3744 pr_trace_msg(trace_channel, 9,
3745 "reading DH_GEX_REQUEST_OLD message from client");
3746
3747 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "DH_GEX_REQUEST_OLD"));
3748 cmd->arg = "(data)";
3749 cmd->cmd_class = CL_SEC|CL_SSH;
3750 cmd->cmd_id = SFTP_CMD_ID;
3751 }
3752
3753 res = read_dh_gex(pkt, &min, &pref, &max, old_request);
3754 if (res < 0) {
3755 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
3756
3757 destroy_pool(pkt->pool);
3758 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3759 }
3760
3761 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
3762 destroy_pool(pkt->pool);
3763
3764 pr_trace_msg(trace_channel, 9,
3765 "read DH_GEX_GROUP message from client: DH group size min = %lu, "
3766 "preferred = %lu, max = %lu bits", (unsigned long) min,
3767 (unsigned long) pref, (unsigned long) max);
3768
3769 pkt = sftp_ssh2_packet_create(kex_pool);
3770 res = write_dh_gex_group(pkt, kex, min, pref, max);
3771 if (res < 0) {
3772 destroy_pool(pkt->pool);
3773 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3774 }
3775
3776 pr_trace_msg(trace_channel, 9, "writing DH_GEX_GROUP message to client");
3777
3778 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
3779 if (res < 0) {
3780 destroy_pool(pkt->pool);
3781 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3782 }
3783
3784 destroy_pool(pkt->pool);
3785
3786 pkt = read_kex_packet(kex_pool, kex, SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED,
3787 NULL, 1, SFTP_SSH2_MSG_KEX_DH_GEX_INIT);
3788
3789 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "DH_GEX_INIT"));
3790 cmd->arg = "(data)";
3791 cmd->cmd_class = CL_SEC|CL_SSH;
3792 cmd->cmd_id = SFTP_CMD_ID;
3793
3794 pr_trace_msg(trace_channel, 9, "reading DH_GEX_INIT message from client");
3795
3796 res = read_dh_gex_init(pkt, kex);
3797 if (res < 0) {
3798 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
3799
3800 destroy_pool(pkt->pool);
3801 return -1;
3802 }
3803
3804 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
3805 destroy_pool(pkt->pool);
3806
3807 if (finish_dh(kex) < 0) {
3808 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3809 "error finishing DH key for group exchange: %s", strerror(errno));
3810 return -1;
3811 }
3812
3813 pkt = sftp_ssh2_packet_create(kex_pool);
3814 res = write_dh_gex_reply(pkt, kex, min, pref, max, old_request);
3815 if (res < 0) {
3816 destroy_pool(pkt->pool);
3817 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3818 }
3819
3820 pr_trace_msg(trace_channel, 9, "writing DH_GEX_REPLY message to client");
3821
3822 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
3823 if (res < 0) {
3824 destroy_pool(pkt->pool);
3825 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3826 }
3827
3828 destroy_pool(pkt->pool);
3829 return 0;
3830 }
3831
3832 static int read_kexrsa_secret(struct ssh2_packet *pkt, struct sftp_kex *kex) {
3833 unsigned char *buf, *encrypted, *decrypted;
3834 uint32_t buflen, encrypted_len;
3835 BIGNUM *k = NULL;
3836 int res;
3837
3838 buf = pkt->payload;
3839 buflen = pkt->payload_len;
3840
3841 encrypted_len = sftp_msg_read_int(pkt->pool, &buf, &buflen);
3842 encrypted = (unsigned char *) sftp_msg_read_data(pkt->pool, &buf, &buflen,
3843 encrypted_len);
3844
3845 /* Save the encrypted secret for calculating H. */
3846 kex->rsa_encrypted_len = encrypted_len;
3847 kex->rsa_encrypted = palloc(kex_pool, encrypted_len);
3848 memcpy(kex->rsa_encrypted, encrypted, encrypted_len);
3849
3850 pr_trace_msg(trace_channel, 12, "decrypting RSA shared secret");
3851
3852 decrypted = palloc(pkt->pool, RSA_size(kex->rsa));
3853
3854 res = RSA_private_decrypt((int) encrypted_len, encrypted, decrypted,
3855 kex->rsa, RSA_PKCS1_OAEP_PADDING);
3856 if (res == -1) {
3857 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3858 "error decrypting RSA shared secret: %s", sftp_crypto_get_errors());
3859 RSA_free(kex->rsa);
3860 kex->rsa = NULL;
3861 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3862 }
3863
3864 /* When converting the decrypted secret into an mpint, watch out for any
3865 * leading padding (as required per SSH RFCs).
3866 */
3867
3868 k = BN_new();
3869 if (BN_mpi2bn(decrypted, res, k) == NULL) {
3870 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3871 "error converting RSA shared secret to BN: %s", sftp_crypto_get_errors());
3872
3873 pr_memscrub(decrypted, res);
3874 RSA_free(kex->rsa);
3875 kex->rsa = NULL;
3876 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
3877 }
3878
3879 pr_memscrub(decrypted, res);
3880
3881 kex->k = k;
3882 return 0;
3883 }
3884
3885 static int write_kexrsa_pubkey(struct ssh2_packet *pkt, struct sftp_kex *kex) {
3886 const BIGNUM *rsa_n = NULL, *rsa_e = NULL;
3887 unsigned char *buf, *ptr, *buf2, *ptr2;
3888 const unsigned char *hostkey_data;
3889 uint32_t buflen, bufsz, buflen2, bufsz2, hostkey_datalen;
3890
3891 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
3892 &hostkey_datalen);
3893 if (hostkey_data == NULL) {
3894 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3895 "error obtaining hostkey for KEXRSA key exchange: %s", strerror(errno));
3896 RSA_free(kex->rsa);
3897 kex->rsa = NULL;
3898 return -1;
3899 }
3900
3901 /* XXX Is this buffer large enough? Too large? */
3902 bufsz = buflen = 2048;
3903 ptr = buf = palloc(kex_pool, bufsz);
3904
3905 /* Write the transient RSA public key into its own buffer, to then be
3906 * written in its entirety as an SSH2 string.
3907 */
3908 sftp_msg_write_string(&buf, &buflen, "ssh-rsa");
3909
3910 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3911 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3912 RSA_get0_key(kex->rsa, &rsa_n, &rsa_e, NULL);
3913 #else
3914 rsa_e = kex->rsa->e;
3915 rsa_n = kex->rsa->n;
3916 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3917 sftp_msg_write_mpint(&buf, &buflen, rsa_e);
3918 sftp_msg_write_mpint(&buf, &buflen, rsa_n);
3919
3920 /* XXX Is this buffer large enough? Too large? */
3921 bufsz2 = buflen2 = 4096;
3922 ptr2 = buf2 = palloc(pkt->pool, bufsz2);
3923
3924 sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_KEXRSA_PUBKEY);
3925 sftp_msg_write_data(&buf2, &buflen2, hostkey_data, hostkey_datalen, TRUE);
3926 sftp_msg_write_data(&buf2, &buflen2, ptr, (bufsz - buflen), TRUE);
3927
3928 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3929
3930 pkt->payload = ptr2;
3931 pkt->payload_len = (bufsz2 - buflen2);
3932
3933 return 0;
3934 }
3935
3936 static int write_kexrsa_done(struct ssh2_packet *pkt, struct sftp_kex *kex) {
3937 const BIGNUM *rsa_e = NULL, *rsa_n = NULL;
3938 unsigned char *buf, *ptr, *buf2, *ptr2;
3939 const unsigned char *h, *hostkey_data, *hsig;
3940 uint32_t buflen, bufsz, buflen2, bufsz2, hlen, hostkey_datalen = 0;
3941 size_t hsiglen;
3942
3943 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
3944 &hostkey_datalen);
3945 if (hostkey_data == NULL) {
3946 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3947 "error obtaining hostkey for KEXRSA key exchange: %s", strerror(errno));
3948
3949 RSA_free(kex->rsa);
3950 kex->rsa = NULL;
3951 BN_clear_free((BIGNUM *) kex->k);
3952 kex->k = NULL;
3953 pr_memscrub(kex->rsa_encrypted, kex->rsa_encrypted_len);
3954 kex->rsa_encrypted = NULL;
3955 kex->rsa_encrypted_len = 0;
3956
3957 return -1;
3958 }
3959
3960 /* XXX Is this buffer large enough? Too large? */
3961 bufsz2 = buflen2 = 4096;
3962 ptr2 = buf2 = palloc(kex_pool, bufsz2);
3963
3964 /* Write the transient RSA public key into its own buffer, to then be
3965 * written in its entirety as an SSH2 string.
3966 */
3967 sftp_msg_write_string(&buf2, &buflen2, "ssh-rsa");
3968
3969 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
3970 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
3971 RSA_get0_key(kex->rsa, &rsa_n, &rsa_e, NULL);
3972 #else
3973 rsa_e = kex->rsa->e;
3974 rsa_n = kex->rsa->n;
3975 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
3976 sftp_msg_write_mpint(&buf2, &buflen2, rsa_e);
3977 sftp_msg_write_mpint(&buf2, &buflen2, rsa_n);
3978
3979 /* Calculate H */
3980 h = calculate_kexrsa_h(kex, hostkey_data, hostkey_datalen, kex->k,
3981 ptr2, (bufsz2 - buflen2), &hlen);
3982 if (h == NULL) {
3983 pr_memscrub((char *) hostkey_data, hostkey_datalen);
3984 RSA_free(kex->rsa);
3985 kex->rsa = NULL;
3986 BN_clear_free((BIGNUM *) kex->k);
3987 kex->k = NULL;
3988 pr_memscrub(kex->rsa_encrypted, kex->rsa_encrypted_len);
3989 kex->rsa_encrypted = NULL;
3990 kex->rsa_encrypted_len = 0;
3991
3992 return -1;
3993 }
3994
3995 kex->h = palloc(kex->pool, hlen);
3996 kex->hlen = hlen;
3997 memcpy((char *) kex->h, h, kex->hlen);
3998
3999 /* Save H as the session ID */
4000 sftp_session_set_id(h, hlen);
4001
4002 /* Sign H with our host key */
4003 hsig = sftp_keys_sign_data(pkt->pool, kex->use_hostkey_type, h, hlen,
4004 &hsiglen);
4005 if (hsig == NULL) {
4006 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "error signing H");
4007
4008 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4009 RSA_free(kex->rsa);
4010 kex->rsa = NULL;
4011 BN_clear_free((BIGNUM *) kex->k);
4012 kex->k = NULL;
4013 pr_memscrub(kex->rsa_encrypted, kex->rsa_encrypted_len);
4014 kex->rsa_encrypted = NULL;
4015 kex->rsa_encrypted_len = 0;
4016
4017 return -1;
4018 }
4019
4020 /* XXX Is this buffer large enough? Too large? */
4021 bufsz = buflen = 4096;
4022 ptr = buf = palloc(pkt->pool, bufsz);
4023
4024 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXRSA_DONE);
4025 sftp_msg_write_data(&buf, &buflen, hsig, hsiglen, TRUE);
4026
4027 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4028 pr_memscrub((char *) h, hlen);
4029 pr_memscrub((char *) hsig, hsiglen);
4030
4031 pr_memscrub(kex->rsa_encrypted, kex->rsa_encrypted_len);
4032 kex->rsa_encrypted = NULL;
4033 kex->rsa_encrypted_len = 0;
4034
4035 pkt->payload = ptr;
4036 pkt->payload_len = (bufsz - buflen);
4037
4038 return 0;
4039 }
4040
4041 static int handle_kex_rsa(struct sftp_kex *kex) {
4042 struct ssh2_packet *pkt;
4043 int res;
4044 cmd_rec *cmd;
4045
4046 pkt = sftp_ssh2_packet_create(kex_pool);
4047 res = write_kexrsa_pubkey(pkt, kex);
4048 if (res < 0) {
4049 destroy_pool(pkt->pool);
4050 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4051 }
4052
4053 pr_trace_msg(trace_channel, 9, "writing KEXRSA_PUBKEY message to client");
4054
4055 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
4056 if (res < 0) {
4057 destroy_pool(pkt->pool);
4058 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4059 }
4060
4061 destroy_pool(pkt->pool);
4062
4063 pkt = read_kex_packet(kex_pool, kex, SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED,
4064 NULL, 1, SFTP_SSH2_MSG_KEXRSA_SECRET);
4065
4066 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "KEXRSA_SECRET"));
4067 cmd->arg = "(data)";
4068 cmd->cmd_class = CL_SEC|CL_SSH;
4069 cmd->cmd_id = SFTP_CMD_ID;
4070
4071 pr_trace_msg(trace_channel, 9, "reading KEXRSA_SECRET message from client");
4072
4073 res = read_kexrsa_secret(pkt, kex);
4074 if (res < 0) {
4075 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
4076
4077 destroy_pool(pkt->pool);
4078 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4079 }
4080
4081 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
4082 destroy_pool(pkt->pool);
4083
4084 pkt = sftp_ssh2_packet_create(kex_pool);
4085 res = write_kexrsa_done(pkt, kex);
4086 if (res < 0) {
4087 destroy_pool(pkt->pool);
4088 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4089 }
4090
4091 pr_trace_msg(trace_channel, 9, "writing KEXRSA_DONE message to client");
4092
4093 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
4094 if (res < 0) {
4095 destroy_pool(pkt->pool);
4096 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4097 }
4098
4099 destroy_pool(pkt->pool);
4100 return 0;
4101 }
4102
4103 #if defined(PR_USE_SODIUM) && defined(HAVE_SHA256_OPENSSL)
4104 static int generate_curve25519_keys(unsigned char *priv_key,
4105 unsigned char *pub_key) {
4106 static const unsigned char basepoint[CURVE25519_SIZE] = {9};
4107 unsigned char zero_curve25519[CURVE25519_SIZE];
4108 int res;
4109
4110 randombytes_buf(priv_key, CURVE25519_SIZE);
4111 res = crypto_scalarmult_curve25519(pub_key, priv_key, basepoint);
4112 if (res < 0) {
4113 pr_trace_msg(trace_channel, 3,
4114 "error performing Curve25519 scalar multiplication");
4115 errno = EINVAL;
4116 return -1;
4117 }
4118
4119 /* Check for all-zero public keys. */
4120 sodium_memzero(zero_curve25519, CURVE25519_SIZE);
4121 if (sodium_memcmp(pub_key, zero_curve25519, CURVE25519_SIZE) == 0) {
4122 pr_trace_msg(trace_channel, 12,
4123 "generated all-zero Curve25519 public key, trying again");
4124 return generate_curve25519_keys(priv_key, pub_key);
4125 }
4126
4127 return 0;
4128 }
4129
4130 static int read_curve25519_init(struct ssh2_packet *pkt, struct sftp_kex *kex) {
4131 unsigned char zero_curve25519[CURVE25519_SIZE];
4132 unsigned char *client_curve25519;
4133 unsigned char *buf;
4134 uint32_t buflen, data_len;
4135 char *data;
4136
4137 buf = pkt->payload;
4138 buflen = data_len = pkt->payload_len;
4139
4140 data = sftp_msg_read_string(pkt->pool, &buf, &buflen);
4141
4142 /* The "string" we read MIGHT contain NULs, thus using strlen(3) to determine
4143 * the length of data is a Bad Idea (Issue #556). Thus instead, we track
4144 * the packet payload length remaining after the read; the data length is
4145 * the difference, including the length value prefix of 4 bytes.
4146 */
4147 data_len -= (buflen + sizeof(uint32_t));
4148
4149 if (data_len != CURVE25519_SIZE) {
4150 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4151 "rejecting invalid length (%lu %s, wanted %d) client Curve25519 key",
4152 (unsigned long) data_len, data_len != 1 ? "bytes" : "byte",
4153 CURVE25519_SIZE);
4154 errno = EINVAL;
4155 return -1;
4156 }
4157
4158 client_curve25519 = (unsigned char *) data;
4159
4160 /* Watch for all-zero public keys, and reject them. */
4161 sodium_memzero(zero_curve25519, CURVE25519_SIZE);
4162 if (sodium_memcmp(client_curve25519, zero_curve25519, CURVE25519_SIZE) == 0) {
4163 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4164 "rejecting invalid (all-zero) client Curve25519 key");
4165 errno = EINVAL;
4166 return -1;
4167 }
4168
4169 kex->client_curve25519 = palloc(kex_pool, CURVE25519_SIZE);
4170 memcpy(kex->client_curve25519, client_curve25519, CURVE25519_SIZE);
4171 return 0;
4172 }
4173
4174 static int get_curve25519_shared_key(unsigned char *shared_key,
4175 unsigned char *client_curve25519, unsigned char *server_key) {
4176 int res;
4177
4178 res = crypto_scalarmult_curve25519(shared_key, server_key, client_curve25519);
4179 if (res < 0) {
4180 pr_trace_msg(trace_channel, 3,
4181 "error performing Curve25519 scalar multiplication");
4182 errno = EINVAL;
4183 return -1;
4184 }
4185
4186 return CURVE25519_SIZE;
4187 }
4188
4189 static const unsigned char *calculate_curve25519_h(struct sftp_kex *kex,
4190 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
4191 const BIGNUM *k, unsigned char *client_curve25519,
4192 unsigned char *server_curve25519, uint32_t *hlen) {
4193 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4194 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4195 EVP_MD_CTX ctx;
4196 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4197 EVP_MD_CTX *pctx;
4198 unsigned char *buf, *ptr;
4199 uint32_t buflen, bufsz;
4200
4201 bufsz = buflen = SFTP_KEX_H_BUFLEN;
4202
4203 /* XXX Is this buffer large enough? Too large? */
4204 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
4205
4206 /* Write all of the data into the buffer in the SSH2 format, and hash it.
4207 * The ordering of these fields is described in RFC5656.
4208 */
4209
4210 /* First, the version strings */
4211 sftp_msg_write_string(&buf, &buflen, kex->client_version);
4212 sftp_msg_write_string(&buf, &buflen, kex->server_version);
4213
4214 /* Client's KEXINIT */
4215 sftp_msg_write_int(&buf, &buflen, kex->client_kexinit_payload_len + 1);
4216 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
4217 sftp_msg_write_data(&buf, &buflen, kex->client_kexinit_payload,
4218 kex->client_kexinit_payload_len, FALSE);
4219
4220 /* Server's KEXINIT */
4221 sftp_msg_write_int(&buf, &buflen, kex->server_kexinit_payload_len + 1);
4222 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
4223 sftp_msg_write_data(&buf, &buflen, kex->server_kexinit_payload,
4224 kex->server_kexinit_payload_len, FALSE);
4225
4226 /* Hostkey data */
4227 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
4228
4229 /* Client's key */
4230 sftp_msg_write_data(&buf, &buflen, client_curve25519, CURVE25519_SIZE, TRUE);
4231
4232 /* Server's key */
4233 sftp_msg_write_data(&buf, &buflen, server_curve25519, CURVE25519_SIZE, TRUE);
4234
4235 /* Shared secret */
4236 sftp_msg_write_mpint(&buf, &buflen, k);
4237
4238 #if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4239 pctx = EVP_MD_CTX_new();
4240 #else
4241 pctx = &ctx;
4242 #endif /* OpenSSL-1.1.0 and later */
4243
4244 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
4245 * int. Without these ugly OpenSSL version preprocessor checks, the
4246 * compiler will error out with "void value not ignored as it ought to be".
4247 */
4248
4249 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
4250 if (EVP_DigestInit(pctx, kex->hash) != 1) {
4251 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4252 "error initializing message digest: %s", sftp_crypto_get_errors());
4253 BN_clear_free((BIGNUM *) kex->e);
4254 kex->e = NULL;
4255 pr_memscrub(ptr, bufsz);
4256 # if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4257 EVP_MD_CTX_free(pctx);
4258 # endif /* OpenSSL-1.1.0 and later */
4259 return NULL;
4260 }
4261 #else
4262 EVP_DigestInit(pctx, kex->hash);
4263 #endif
4264
4265 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
4266 if (EVP_DigestUpdate(pctx, ptr, (bufsz - buflen)) != 1) {
4267 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4268 "error updating message digest: %s", sftp_crypto_get_errors());
4269 BN_clear_free((BIGNUM *) kex->e);
4270 kex->e = NULL;
4271 pr_memscrub(ptr, bufsz);
4272 # if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4273 EVP_MD_CTX_free(pctx);
4274 # endif /* OpenSSL-1.1.0 and later */
4275 return NULL;
4276 }
4277 #else
4278 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
4279 #endif
4280
4281 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
4282 if (EVP_DigestFinal(pctx, kex_digest_buf, hlen) != 1) {
4283 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4284 "error finalizing message digest: %s", sftp_crypto_get_errors());
4285 BN_clear_free((BIGNUM *) kex->e);
4286 kex->e = NULL;
4287 pr_memscrub(ptr, bufsz);
4288 # if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4289 EVP_MD_CTX_free(pctx);
4290 # endif /* OpenSSL-1.1.0 and later */
4291 return NULL;
4292 }
4293 #else
4294 EVP_DigestFinal(pctx, kex_digest_buf, hlen);
4295 #endif
4296
4297 #if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4298 EVP_MD_CTX_free(pctx);
4299 #endif /* OpenSSL-1.1.0 and later */
4300 BN_clear_free((BIGNUM *) kex->e);
4301 kex->e = NULL;
4302 pr_memscrub(ptr, bufsz);
4303
4304 return kex_digest_buf;
4305 }
4306
4307 static int write_curve25519_reply(struct ssh2_packet *pkt,
4308 struct sftp_kex *kex) {
4309 const unsigned char *h, *hostkey_data, *hsig;
4310 unsigned char *buf, *ptr;
4311 unsigned char server_curve25519[CURVE25519_SIZE];
4312 unsigned char server_key[CURVE25519_SIZE];
4313 uint32_t bufsz, buflen, hlen = 0, hostkey_datalen = 0;
4314 size_t hsiglen;
4315 BIGNUM *k = NULL;
4316 int res;
4317
4318 if (generate_curve25519_keys(server_key, server_curve25519) < 0) {
4319 return -1;
4320 }
4321
4322 /* Compute the shared secret. */
4323 buf = palloc(kex_pool, CURVE25519_SIZE);
4324
4325 pr_trace_msg(trace_channel, 12, "computing Curve25519 key");
4326 res = get_curve25519_shared_key((unsigned char *) buf, kex->client_curve25519,
4327 server_key);
4328 if (res < 0) {
4329 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4330 "error computing Curve25519 shared secret: %s", strerror(errno));
4331 return -1;
4332 }
4333
4334 k = BN_new();
4335 if (k == NULL) {
4336 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4337 "error allocating new BIGNUM: %s", sftp_crypto_get_errors());
4338 pr_memscrub(buf, res);
4339 return -1;
4340 }
4341
4342 if (BN_bin2bn((unsigned char *) buf, res, k) == NULL) {
4343 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4344 "error converting Curve25519 shared secret to BN: %s",
4345 sftp_crypto_get_errors());
4346 pr_memscrub(buf, res);
4347 return -1;
4348 }
4349
4350 pr_memscrub(buf, res);
4351 kex->k = k;
4352
4353 /* Get the hostkey data; it will be part of the data we hash in order
4354 * to create the session key.
4355 */
4356 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
4357 &hostkey_datalen);
4358 if (hostkey_data == NULL) {
4359 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4360 "error converting hostkey for signing: %s", strerror(errno));
4361
4362 BN_clear_free((BIGNUM *) kex->k);
4363 kex->k = NULL;
4364 return -1;
4365 }
4366
4367 /* Calculate H */
4368 h = calculate_curve25519_h(kex, hostkey_data, hostkey_datalen, k,
4369 kex->client_curve25519, server_curve25519, &hlen);
4370 if (h == NULL) {
4371 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4372 BN_clear_free((BIGNUM *) kex->k);
4373 kex->k = NULL;
4374 return -1;
4375 }
4376
4377 kex->h = palloc(kex->pool, hlen);
4378 kex->hlen = hlen;
4379 memcpy((char *) kex->h, h, kex->hlen);
4380
4381 /* Save H as the session ID */
4382 sftp_session_set_id(h, hlen);
4383
4384 /* Sign H with our hostkey */
4385 hsig = sftp_keys_sign_data(pkt->pool, kex->use_hostkey_type, h, hlen,
4386 &hsiglen);
4387 if (hsig == NULL) {
4388 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "error signing H");
4389 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4390 BN_clear_free((BIGNUM *) kex->k);
4391 kex->k = NULL;
4392 return -1;
4393 }
4394
4395 /* XXX Is this large enough? Too large? */
4396 buflen = bufsz = 4096;
4397 ptr = buf = palloc(pkt->pool, bufsz);
4398
4399 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEX_ECDH_REPLY);
4400 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
4401 sftp_msg_write_data(&buf, &buflen, server_curve25519, CURVE25519_SIZE, TRUE);
4402 sftp_msg_write_data(&buf, &buflen, hsig, hsiglen, TRUE);
4403
4404 /* Scrub any sensitive data when done */
4405 pr_memscrub((char *) server_key, CURVE25519_SIZE);
4406 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4407 pr_memscrub((char *) hsig, hsiglen);
4408
4409 pkt->payload = ptr;
4410 pkt->payload_len = (bufsz - buflen);
4411
4412 return 0;
4413 }
4414
4415 static int handle_kex_curve25519(struct ssh2_packet *pkt,
4416 struct sftp_kex *kex) {
4417 int res;
4418 cmd_rec *cmd;
4419 const char *req;
4420
4421 req = "ECDH_INIT";
4422 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, req));
4423 cmd->arg = "(data)";
4424 cmd->cmd_class = CL_SEC|CL_SSH;
4425 cmd->cmd_id = SFTP_CMD_ID;
4426
4427 pr_trace_msg(trace_channel, 9, "reading %s message from client", req);
4428
4429 res = read_curve25519_init(pkt, kex);
4430 if (res < 0) {
4431 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
4432
4433 destroy_pool(pkt->pool);
4434 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4435 }
4436
4437 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
4438 destroy_pool(pkt->pool);
4439
4440 /* Send our key exchange reply. */
4441 pkt = sftp_ssh2_packet_create(kex_pool);
4442 res = write_curve25519_reply(pkt, kex);
4443 if (res < 0) {
4444 destroy_pool(pkt->pool);
4445 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4446 }
4447
4448 pr_trace_msg(trace_channel, 9, "writing %s message to client", req);
4449
4450 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
4451 if (res < 0) {
4452 destroy_pool(pkt->pool);
4453 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4454 }
4455
4456 destroy_pool(pkt->pool);
4457 return 0;
4458 }
4459 #endif /* PR_USE_SODIUM and HAVE_SHA256_OPENSSL */
4460
4461 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
4462 static int generate_curve448_keys(unsigned char *priv_key,
4463 unsigned char *pub_key) {
4464 EVP_PKEY_CTX *pctx = NULL;
4465 EVP_PKEY *pkey = NULL;
4466 size_t key_len = 0;
4467
4468 pctx = EVP_PKEY_CTX_new_id(NID_X448, NULL);
4469 if (pctx == NULL) {
4470 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4471 "error initializing context for Curve448 key: %s",
4472 sftp_crypto_get_errors());
4473 return -1;
4474 }
4475
4476 if (EVP_PKEY_keygen_init(pctx) != 1) {
4477 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4478 "error preparing to generate Curve448 key: %s", sftp_crypto_get_errors());
4479 EVP_PKEY_CTX_free(pctx);
4480 return -1;
4481 }
4482
4483 if (EVP_PKEY_keygen(pctx, &pkey) != 1) {
4484 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4485 "error generating Curve448 shared key: %s", sftp_crypto_get_errors());
4486 EVP_PKEY_CTX_free(pctx);
4487 return -1;
4488 }
4489
4490 key_len = CURVE448_SIZE;
4491 if (EVP_PKEY_get_raw_private_key(pkey, priv_key, &key_len) != 1) {
4492 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4493 "error obtaining Curve448 private key: %s", sftp_crypto_get_errors());
4494 EVP_PKEY_CTX_free(pctx);
4495 EVP_PKEY_free(pkey);
4496 return -1;
4497 }
4498
4499 key_len = CURVE448_SIZE;
4500 if (EVP_PKEY_get_raw_public_key(pkey, pub_key, &key_len) != 1) {
4501 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4502 "error obtaining Curve448 public key: %s", sftp_crypto_get_errors());
4503 EVP_PKEY_CTX_free(pctx);
4504 EVP_PKEY_free(pkey);
4505 return -1;
4506 }
4507
4508 EVP_PKEY_CTX_free(pctx);
4509 EVP_PKEY_free(pkey);
4510 return 0;
4511 }
4512
4513 static int read_curve448_init(struct ssh2_packet *pkt, struct sftp_kex *kex) {
4514 unsigned char zero_curve448[CURVE448_SIZE];
4515 unsigned char *client_curve448;
4516 unsigned char *buf;
4517 uint32_t buflen, data_len;
4518 char *data;
4519
4520 buf = pkt->payload;
4521 buflen = data_len = pkt->payload_len;
4522
4523 data = sftp_msg_read_string(pkt->pool, &buf, &buflen);
4524
4525 /* The "string" we read MIGHT contain NULs, thus using strlen(3) to determine
4526 * the length of data is a Bad Idea (Issue #556). Thus instead, we track
4527 * the packet payload length remaining after the read; the data length is
4528 * the difference, including the length value prefix of 4 bytes.
4529 */
4530 data_len -= (buflen + sizeof(uint32_t));
4531
4532 if (data_len != CURVE448_SIZE) {
4533 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4534 "rejecting invalid length (%lu %s, wanted %d) client Curve448 key",
4535 (unsigned long) data_len, data_len != 1 ? "bytes" : "byte",
4536 CURVE448_SIZE);
4537 errno = EINVAL;
4538 return -1;
4539 }
4540
4541 client_curve448 = (unsigned char *) data;
4542
4543 /* Watch for all-zero public keys, and reject them. */
4544 memset(zero_curve448, '\0', sizeof(zero_curve448));
4545 if (memcmp(client_curve448, zero_curve448, CURVE448_SIZE) == 0) {
4546 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4547 "rejecting invalid (all-zero) client Curve448 key");
4548 errno = EINVAL;
4549 return -1;
4550 }
4551
4552 kex->client_curve448 = palloc(kex_pool, CURVE448_SIZE);
4553 memcpy(kex->client_curve448, client_curve448, CURVE448_SIZE);
4554 return 0;
4555 }
4556
4557 static int get_curve448_shared_key(unsigned char *shared_key,
4558 unsigned char *client_curve448, unsigned char *server_key) {
4559 EVP_PKEY_CTX *pctx = NULL;
4560 EVP_PKEY *client_pkey = NULL, *server_pkey = NULL;
4561 size_t shared_keylen = 0;
4562
4563 server_pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_X448, NULL, server_key,
4564 CURVE448_SIZE);
4565 if (server_pkey == NULL) {
4566 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4567 "error initializing Curve448 server key: %s", sftp_crypto_get_errors());
4568 return -1;
4569 }
4570
4571 client_pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_X448, NULL,
4572 client_curve448, CURVE448_SIZE);
4573 if (client_pkey == NULL) {
4574 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4575 "error initializing Curve448 client key: %s", sftp_crypto_get_errors());
4576 EVP_PKEY_free(server_pkey);
4577 return -1;
4578 }
4579
4580 pctx = EVP_PKEY_CTX_new(server_pkey, NULL);
4581 if (pctx == NULL) {
4582 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4583 "error initializing context for Curve448 shared key: %s",
4584 sftp_crypto_get_errors());
4585 EVP_PKEY_free(server_pkey);
4586 EVP_PKEY_free(client_pkey);
4587 return -1;
4588 }
4589
4590 if (EVP_PKEY_derive_init(pctx) != 1) {
4591 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4592 "error preparing for Curve448 shared key: %s", sftp_crypto_get_errors());
4593 EVP_PKEY_CTX_free(pctx);
4594 EVP_PKEY_free(server_pkey);
4595 EVP_PKEY_free(client_pkey);
4596 return -1;
4597 }
4598
4599 if (EVP_PKEY_derive_set_peer(pctx, client_pkey) != 1) {
4600 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4601 "error setting peer for Curve448 shared key: %s",
4602 sftp_crypto_get_errors());
4603 EVP_PKEY_CTX_free(pctx);
4604 EVP_PKEY_free(server_pkey);
4605 EVP_PKEY_free(client_pkey);
4606 return -1;
4607 }
4608
4609 shared_keylen = CURVE448_SIZE;
4610 if (EVP_PKEY_derive(pctx, shared_key, &shared_keylen) != 1) {
4611 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4612 "error generating Curve448 shared key: %s", sftp_crypto_get_errors());
4613 EVP_PKEY_CTX_free(pctx);
4614 EVP_PKEY_free(server_pkey);
4615 EVP_PKEY_free(client_pkey);
4616 return -1;
4617 }
4618
4619 if (shared_keylen != CURVE448_SIZE) {
4620 pr_trace_msg(trace_channel, 1,
4621 "generated Curve448 shared key length (%lu bytes) is not as expected "
4622 "(%lu bytes)", (unsigned long) shared_keylen,
4623 (unsigned long) CURVE448_SIZE);
4624 }
4625
4626 EVP_PKEY_CTX_free(pctx);
4627 EVP_PKEY_free(server_pkey);
4628 EVP_PKEY_free(client_pkey);
4629
4630 return CURVE448_SIZE;
4631 }
4632
4633 static const unsigned char *calculate_curve448_h(struct sftp_kex *kex,
4634 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
4635 const BIGNUM *k, unsigned char *client_curve448,
4636 unsigned char *server_curve448, uint32_t *hlen) {
4637 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4638 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4639 EVP_MD_CTX ctx;
4640 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4641 EVP_MD_CTX *pctx;
4642 unsigned char *buf, *ptr;
4643 uint32_t buflen, bufsz;
4644
4645 bufsz = buflen = SFTP_KEX_H_BUFLEN;
4646
4647 /* XXX Is this buffer large enough? Too large? */
4648 ptr = buf = sftp_msg_getbuf(kex_pool, bufsz);
4649
4650 /* Write all of the data into the buffer in the SSH2 format, and hash it.
4651 * The ordering of these fields is described in RFC5656.
4652 */
4653
4654 /* First, the version strings */
4655 sftp_msg_write_string(&buf, &buflen, kex->client_version);
4656 sftp_msg_write_string(&buf, &buflen, kex->server_version);
4657
4658 /* Client's KEXINIT */
4659 sftp_msg_write_int(&buf, &buflen, kex->client_kexinit_payload_len + 1);
4660 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
4661 sftp_msg_write_data(&buf, &buflen, kex->client_kexinit_payload,
4662 kex->client_kexinit_payload_len, FALSE);
4663
4664 /* Server's KEXINIT */
4665 sftp_msg_write_int(&buf, &buflen, kex->server_kexinit_payload_len + 1);
4666 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEXINIT);
4667 sftp_msg_write_data(&buf, &buflen, kex->server_kexinit_payload,
4668 kex->server_kexinit_payload_len, FALSE);
4669
4670 /* Hostkey data */
4671 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
4672
4673 /* Client's key */
4674 sftp_msg_write_data(&buf, &buflen, client_curve448, CURVE448_SIZE, TRUE);
4675
4676 /* Server's key */
4677 sftp_msg_write_data(&buf, &buflen, server_curve448, CURVE448_SIZE, TRUE);
4678
4679 /* Shared secret */
4680 sftp_msg_write_mpint(&buf, &buflen, k);
4681
4682 #if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4683 pctx = EVP_MD_CTX_new();
4684 #else
4685 pctx = &ctx;
4686 #endif /* OpenSSL-1.1.0 and later */
4687
4688 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
4689 * int. Without these ugly OpenSSL version preprocessor checks, the
4690 * compiler will error out with "void value not ignored as it ought to be".
4691 */
4692
4693 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
4694 if (EVP_DigestInit(pctx, kex->hash) != 1) {
4695 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4696 "error initializing message digest: %s", sftp_crypto_get_errors());
4697 BN_clear_free((BIGNUM *) kex->e);
4698 kex->e = NULL;
4699 pr_memscrub(ptr, bufsz);
4700 # if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4701 EVP_MD_CTX_free(pctx);
4702 # endif /* OpenSSL-1.1.0 and later */
4703 return NULL;
4704 }
4705 #else
4706 EVP_DigestInit(pctx, kex->hash);
4707 #endif
4708
4709 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
4710 if (EVP_DigestUpdate(pctx, ptr, (bufsz - buflen)) != 1) {
4711 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4712 "error updating message digest: %s", sftp_crypto_get_errors());
4713 BN_clear_free((BIGNUM *) kex->e);
4714 kex->e = NULL;
4715 pr_memscrub(ptr, bufsz);
4716 # if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4717 EVP_MD_CTX_free(pctx);
4718 # endif /* OpenSSL-1.1.0 and later */
4719 return NULL;
4720 }
4721 #else
4722 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
4723 #endif
4724
4725 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
4726 if (EVP_DigestFinal(pctx, kex_digest_buf, hlen) != 1) {
4727 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4728 "error finalizing message digest: %s", sftp_crypto_get_errors());
4729 BN_clear_free((BIGNUM *) kex->e);
4730 kex->e = NULL;
4731 pr_memscrub(ptr, bufsz);
4732 # if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4733 EVP_MD_CTX_free(pctx);
4734 # endif /* OpenSSL-1.1.0 and later */
4735 return NULL;
4736 }
4737 #else
4738 EVP_DigestFinal(pctx, kex_digest_buf, hlen);
4739 #endif
4740
4741 #if OPENSSL_VERSION_NUMBER >= 0x10100000LL
4742 EVP_MD_CTX_free(pctx);
4743 #endif /* OpenSSL-1.1.0 and later */
4744 BN_clear_free((BIGNUM *) kex->e);
4745 kex->e = NULL;
4746 pr_memscrub(ptr, bufsz);
4747
4748 return kex_digest_buf;
4749 }
4750
4751 static int write_curve448_reply(struct ssh2_packet *pkt, struct sftp_kex *kex) {
4752 const unsigned char *h, *hostkey_data, *hsig;
4753 unsigned char *buf, *ptr;
4754 unsigned char server_curve448[CURVE448_SIZE];
4755 unsigned char server_key[CURVE448_SIZE];
4756 uint32_t bufsz, buflen, hlen = 0, hostkey_datalen = 0;
4757 size_t hsiglen;
4758 BIGNUM *k = NULL;
4759 int res;
4760
4761 if (generate_curve448_keys(server_key, server_curve448) < 0) {
4762 return -1;
4763 }
4764
4765 /* Compute the shared secret. */
4766 buf = palloc(kex_pool, CURVE448_SIZE);
4767
4768 pr_trace_msg(trace_channel, 12, "computing Curve448 key");
4769 res = get_curve448_shared_key((unsigned char *) buf, kex->client_curve448,
4770 server_key);
4771 if (res < 0) {
4772 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4773 "error computing Curve448 shared secret: %s", strerror(errno));
4774 return -1;
4775 }
4776
4777 k = BN_new();
4778 if (k == NULL) {
4779 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4780 "error allocating new BIGNUM: %s", sftp_crypto_get_errors());
4781 pr_memscrub(buf, res);
4782 return -1;
4783 }
4784
4785 if (BN_bin2bn((unsigned char *) buf, res, k) == NULL) {
4786 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4787 "error converting Curve448 shared secret to BN: %s",
4788 sftp_crypto_get_errors());
4789 pr_memscrub(buf, res);
4790 return -1;
4791 }
4792
4793 pr_memscrub(buf, res);
4794 kex->k = k;
4795
4796 /* Get the hostkey data; it will be part of the data we hash in order
4797 * to create the session key.
4798 */
4799 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
4800 &hostkey_datalen);
4801 if (hostkey_data == NULL) {
4802 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4803 "error converting hostkey for signing: %s", strerror(errno));
4804
4805 BN_clear_free((BIGNUM *) kex->k);
4806 kex->k = NULL;
4807 return -1;
4808 }
4809
4810 /* Calculate H */
4811 h = calculate_curve448_h(kex, hostkey_data, hostkey_datalen, k,
4812 kex->client_curve448, server_curve448, &hlen);
4813 if (h == NULL) {
4814 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4815 BN_clear_free((BIGNUM *) kex->k);
4816 kex->k = NULL;
4817 return -1;
4818 }
4819
4820 kex->h = palloc(kex->pool, hlen);
4821 kex->hlen = hlen;
4822 memcpy((char *) kex->h, h, kex->hlen);
4823
4824 /* Save H as the session ID */
4825 sftp_session_set_id(h, hlen);
4826
4827 /* Sign H with our hostkey */
4828 hsig = sftp_keys_sign_data(pkt->pool, kex->use_hostkey_type, h, hlen,
4829 &hsiglen);
4830 if (hsig == NULL) {
4831 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "error signing H");
4832 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4833 BN_clear_free((BIGNUM *) kex->k);
4834 kex->k = NULL;
4835 return -1;
4836 }
4837
4838 /* XXX Is this large enough? Too large? */
4839 buflen = bufsz = 4096;
4840 ptr = buf = palloc(pkt->pool, bufsz);
4841
4842 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEX_ECDH_REPLY);
4843 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
4844 sftp_msg_write_data(&buf, &buflen, server_curve448, CURVE448_SIZE, TRUE);
4845 sftp_msg_write_data(&buf, &buflen, hsig, hsiglen, TRUE);
4846
4847 /* Scrub any sensitive data when done */
4848 pr_memscrub((char *) server_key, CURVE448_SIZE);
4849 pr_memscrub((char *) hostkey_data, hostkey_datalen);
4850 pr_memscrub((char *) hsig, hsiglen);
4851
4852 pkt->payload = ptr;
4853 pkt->payload_len = (bufsz - buflen);
4854
4855 return 0;
4856 }
4857
4858 static int handle_kex_curve448(struct ssh2_packet *pkt, struct sftp_kex *kex) {
4859 int res;
4860 cmd_rec *cmd;
4861 const char *req;
4862
4863 req = "ECDH_INIT";
4864 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, req));
4865 cmd->arg = "(data)";
4866 cmd->cmd_class = CL_SEC|CL_SSH;
4867 cmd->cmd_id = SFTP_CMD_ID;
4868
4869 pr_trace_msg(trace_channel, 9, "reading %s message from client", req);
4870
4871 res = read_curve448_init(pkt, kex);
4872 if (res < 0) {
4873 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
4874
4875 destroy_pool(pkt->pool);
4876 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4877 }
4878
4879 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
4880 destroy_pool(pkt->pool);
4881
4882 /* Send our key exchange reply. */
4883 pkt = sftp_ssh2_packet_create(kex_pool);
4884 res = write_curve448_reply(pkt, kex);
4885 if (res < 0) {
4886 destroy_pool(pkt->pool);
4887 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4888 }
4889
4890 pr_trace_msg(trace_channel, 9, "writing %s message to client", req);
4891
4892 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
4893 if (res < 0) {
4894 destroy_pool(pkt->pool);
4895 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
4896 }
4897
4898 destroy_pool(pkt->pool);
4899 return 0;
4900 }
4901 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
4902
4903 #if defined(PR_USE_OPENSSL_ECC)
4904 static int read_ecdh_init(struct ssh2_packet *pkt, struct sftp_kex *kex) {
4905 unsigned char *buf;
4906 uint32_t buflen;
4907 const EC_GROUP *curve;
4908 EC_POINT *point;
4909
4910 buf = pkt->payload;
4911 buflen = pkt->payload_len;
4912
4913 curve = EC_KEY_get0_group(kex->ec);
4914
4915 point = EC_POINT_new(curve);
4916 if (point == NULL) {
4917 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4918 "error allocating EC_POINT: %s", sftp_crypto_get_errors());
4919 return -1;
4920 }
4921
4922 /* Read in the client's EC point (i.e. their ECC "public key"). */
4923 kex->client_point = sftp_msg_read_ecpoint(pkt->pool, &buf, &buflen, curve,
4924 point);
4925 if (kex->client_point == NULL) {
4926 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4927 "error reading ECDH_INIT: %s", strerror(errno));
4928 EC_POINT_clear_free(point);
4929 kex->client_point = NULL;
4930 return -1;
4931 }
4932
4933 if (sftp_keys_validate_ecdsa_params(curve, kex->client_point) < 0) {
4934 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4935 "invalid client ECDH public key (EC point): %s", strerror(errno));
4936 EC_POINT_clear_free(kex->client_point);
4937 kex->client_point = NULL;
4938 return -1;
4939 }
4940
4941 return 0;
4942 }
4943
4944 static int write_ecdh_reply(struct ssh2_packet *pkt, struct sftp_kex *kex) {
4945 const unsigned char *h;
4946 const unsigned char *hostkey_data, *hsig;
4947 unsigned char *buf, *ptr;
4948 uint32_t bufsz, buflen, hlen = 0, hostkey_datalen = 0;
4949 size_t ecdhlen, hsiglen;
4950 const BIGNUM *k = NULL;
4951 int res;
4952
4953 /* Compute the shared secret */
4954 ecdhlen = ((EC_GROUP_get_degree(EC_KEY_get0_group(kex->ec)) + 7) / 8);
4955 buf = palloc(kex_pool, ecdhlen);
4956
4957 pr_trace_msg(trace_channel, 12, "computing ECDH key");
4958 res = ECDH_compute_key((unsigned char *) buf, ecdhlen, kex->client_point,
4959 kex->ec, NULL);
4960 if (res <= 0) {
4961 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4962 "error computing ECDH shared secret: %s", sftp_crypto_get_errors());
4963 return -1;
4964 }
4965
4966 if ((size_t) res != ecdhlen) {
4967 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4968 "computed ECDH shared secret length (%d) does not match needed length "
4969 "(%lu), rejecting", res, (unsigned long) ecdhlen);
4970 return -1;
4971 }
4972
4973 k = BN_new();
4974 if (k == NULL) {
4975 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4976 "error allocating new BIGNUM: %s", sftp_crypto_get_errors());
4977 pr_memscrub(buf, res);
4978 return -1;
4979 }
4980
4981 if (BN_bin2bn((unsigned char *) buf, res, (BIGNUM *) k) == NULL) {
4982 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4983 "error converting ECDH shared secret to BN: %s",
4984 sftp_crypto_get_errors());
4985 pr_memscrub(buf, res);
4986 return -1;
4987 }
4988
4989 pr_memscrub(buf, res);
4990 kex->k = k;
4991
4992 /* Get the hostkey data; it will be part of the data we hash in order
4993 * to create the session key.
4994 */
4995 hostkey_data = sftp_keys_get_hostkey_data(pkt->pool, kex->use_hostkey_type,
4996 &hostkey_datalen);
4997 if (hostkey_data == NULL) {
4998 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4999 "error converting hostkey for signing: %s", strerror(errno));
5000 BN_clear_free((BIGNUM *) kex->k);
5001 kex->k = NULL;
5002 return -1;
5003 }
5004
5005 /* Calculate H */
5006 h = calculate_ecdh_h(kex, hostkey_data, hostkey_datalen, k, &hlen);
5007 if (h == NULL) {
5008 pr_memscrub((char *) hostkey_data, hostkey_datalen);
5009 BN_clear_free((BIGNUM *) kex->k);
5010 kex->k = NULL;
5011 return -1;
5012 }
5013
5014 kex->h = palloc(kex->pool, hlen);
5015 kex->hlen = hlen;
5016 memcpy((char *) kex->h, h, kex->hlen);
5017
5018 /* Save H as the session ID */
5019 sftp_session_set_id(h, hlen);
5020
5021 /* Sign H with our hostkey */
5022 hsig = sftp_keys_sign_data(pkt->pool, kex->use_hostkey_type, h, hlen,
5023 &hsiglen);
5024 if (hsig == NULL) {
5025 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "error signing H");
5026 pr_memscrub((char *) hostkey_data, hostkey_datalen);
5027 BN_clear_free((BIGNUM *) kex->k);
5028 kex->k = NULL;
5029 return -1;
5030 }
5031
5032 /* XXX Is this large enough? Too large? */
5033 buflen = bufsz = 4096;
5034 ptr = buf = palloc(pkt->pool, bufsz);
5035
5036 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_KEX_ECDH_REPLY);
5037 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
5038 sftp_msg_write_ecpoint(&buf, &buflen, EC_KEY_get0_group(kex->ec),
5039 EC_KEY_get0_public_key(kex->ec));
5040 sftp_msg_write_data(&buf, &buflen, hsig, hsiglen, TRUE);
5041
5042 /* Scrub any sensitive data when done */
5043 pr_memscrub((char *) hostkey_data, hostkey_datalen);
5044 pr_memscrub((char *) hsig, hsiglen);
5045
5046 pkt->payload = ptr;
5047 pkt->payload_len = (bufsz - buflen);
5048
5049 return 0;
5050 }
5051
5052 static int handle_kex_ecdh(struct ssh2_packet *pkt, struct sftp_kex *kex) {
5053 int res;
5054 cmd_rec *cmd;
5055 const char *req;
5056
5057 req = "ECDH_INIT";
5058 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, req));
5059 cmd->arg = "(data)";
5060 cmd->cmd_class = CL_SEC|CL_SSH;
5061 cmd->cmd_id = SFTP_CMD_ID;
5062
5063 pr_trace_msg(trace_channel, 9, "reading %s message from client", req);
5064
5065 res = read_ecdh_init(pkt, kex);
5066 if (res < 0) {
5067 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
5068
5069 destroy_pool(pkt->pool);
5070 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
5071 }
5072
5073 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
5074 destroy_pool(pkt->pool);
5075
5076 /* Send our key exchange reply. */
5077 pkt = sftp_ssh2_packet_create(kex_pool);
5078 res = write_ecdh_reply(pkt, kex);
5079 if (res < 0) {
5080 destroy_pool(pkt->pool);
5081 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
5082 }
5083
5084 /* Don't clean up the EC key in the kex struct until after we've
5085 * written out a reply.
5086 */
5087 if (finish_ecdh(kex) < 0) {
5088 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5089 "error finishing ECDH key: %s", strerror(errno));
5090 destroy_pool(pkt->pool);
5091 return -1;
5092 }
5093
5094 pr_trace_msg(trace_channel, 9, "writing %s message to client", req);
5095
5096 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5097 if (res < 0) {
5098 destroy_pool(pkt->pool);
5099 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
5100 }
5101
5102 destroy_pool(pkt->pool);
5103 return 0;
5104 }
5105 #endif /* PR_USE_OPENSSL_ECC */
5106
5107 static struct ssh2_packet *read_kex_packet(pool *p, struct sftp_kex *kex,
5108 int disconn_code, char *found_msg_type, unsigned int ntypes, ...) {
5109 register unsigned int i;
5110 va_list ap;
5111 struct ssh2_packet *pkt = NULL;
5112 array_header *allowed_types;
5113
5114 pr_trace_msg(trace_channel, 9, "waiting for a message of %d %s from client",
5115 ntypes, ntypes != 1 ? "types" : "type");
5116
5117 allowed_types = make_array(p, 1, sizeof(char));
5118
5119 va_start(ap, ntypes);
5120
5121 while (ntypes-- > 0) {
5122 *((char *) push_array(allowed_types)) = va_arg(ap, int);
5123 }
5124
5125 va_end(ap);
5126
5127 /* Keep looping until we get the desired message, or we time out (hopefully
5128 * via TimeoutLogin or somesuch).
5129 */
5130 while (pkt == NULL) {
5131 int found = FALSE, res;
5132 char msg_type;
5133
5134 pr_signals_handle();
5135
5136 pkt = sftp_ssh2_packet_create(p);
5137 res = sftp_ssh2_packet_read(sftp_conn->rfd, pkt);
5138 if (res < 0) {
5139 int xerrno = errno;
5140
5141 destroy_kex(kex);
5142 destroy_pool(pkt->pool);
5143
5144 errno = xerrno;
5145 return NULL;
5146 }
5147
5148 pr_response_clear(&resp_list);
5149 pr_response_clear(&resp_err_list);
5150 pr_response_set_pool(pkt->pool);
5151
5152 /* Per RFC 4253, Section 11, DEBUG, DISCONNECT, IGNORE, and UNIMPLEMENTED
5153 * messages can occur at any time, even during KEX. We have to be prepared
5154 * for this, and Do The Right Thing(tm).
5155 *
5156 * However, due to the Terrapin attack, if we are using a "strict KEX"
5157 * mode, then only DISCONNECT messages can occur during KEX; DEBUG,
5158 * IGNORE, and UNIMPLEMENTED messages will lead to disconnecting.
5159 */
5160
5161 msg_type = sftp_ssh2_packet_get_msg_type(pkt);
5162
5163 for (i = 0; i < allowed_types->nelts; i++) {
5164 if (msg_type == ((unsigned char *) allowed_types->elts)[i]) {
5165 /* Exactly what we were looking for. Excellent. */
5166 pr_trace_msg(trace_channel, 13,
5167 "received expected %s message",
5168 sftp_ssh2_packet_get_msg_type_desc(msg_type));
5169
5170 if (found_msg_type != NULL) {
5171 /* The caller wants to know the type of message we're returning;
5172 * packet_get_msg_type() performs a destructive read.
5173 */
5174 *found_msg_type = msg_type;
5175 }
5176
5177 found = TRUE;
5178 break;
5179 }
5180 }
5181
5182 if (found == TRUE) {
5183 break;
5184 }
5185
5186 switch (msg_type) {
5187 /* DISCONNECT messages are always allowed. */
5188 case SFTP_SSH2_MSG_DISCONNECT:
5189 sftp_ssh2_packet_handle_disconnect(pkt);
5190 pr_response_set_pool(NULL);
5191 pkt = NULL;
5192 break;
5193
5194 case SFTP_SSH2_MSG_DEBUG:
5195 if (use_strict_kex == FALSE) {
5196 sftp_ssh2_packet_handle_debug(pkt);
5197 pr_response_set_pool(NULL);
5198 pkt = NULL;
5199 break;
5200 }
5201
5202 case SFTP_SSH2_MSG_IGNORE:
5203 if (use_strict_kex == FALSE) {
5204 sftp_ssh2_packet_handle_ignore(pkt);
5205 pr_response_set_pool(NULL);
5206 pkt = NULL;
5207 break;
5208 }
5209
5210 case SFTP_SSH2_MSG_UNIMPLEMENTED:
5211 if (use_strict_kex == FALSE) {
5212 sftp_ssh2_packet_handle_unimplemented(pkt);
5213 pr_response_set_pool(NULL);
5214 pkt = NULL;
5215 break;
5216 }
5217
5218 default:
5219 /* For any other message type, it's considered a protocol error. */
5220 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5221 "received %s (%d) unexpectedly%s, disconnecting",
5222 sftp_ssh2_packet_get_msg_type_desc(msg_type), msg_type,
5223 use_strict_kex ? " during strict KEX" : "");
5224 pr_response_set_pool(NULL);
5225 destroy_kex(kex);
5226 destroy_pool(pkt->pool);
5227 SFTP_DISCONNECT_CONN(disconn_code, NULL);
5228 }
5229 }
5230
5231 return pkt;
5232 }
5233
5234 int sftp_kex_handle(struct ssh2_packet *pkt) {
5235 int correct_guess = TRUE, res, sent_newkeys = FALSE;
5236 char msg_type;
5237 struct sftp_kex *kex;
5238 cmd_rec *cmd;
5239 const char *write_algo;
5240
5241 /* We may already have a kex structure, either from the client
5242 * initial connect (kex_first_kex not null), or because we
5243 * are in a server-initiated rekeying (kex_rekey_kex not null).
5244 */
5245 if (kex_first_kex != NULL) {
5246 kex = kex_first_kex;
5247
5248 /* We need to assign the client/server versions, which this struct
5249 * will not have.
5250 */
5251 kex->client_version = kex_client_version;
5252 kex->server_version = kex_server_version;
5253
5254 } else if (kex_rekey_kex != NULL) {
5255 kex = kex_rekey_kex;
5256
5257 } else {
5258 kex = create_kex(kex_pool);
5259 }
5260
5261 /* The packet we are given is guaranteed to be a KEXINIT packet. */
5262
5263 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "KEXINIT"));
5264 cmd->arg = "(data)";
5265 cmd->cmd_class = CL_SEC|CL_SSH;
5266 cmd->cmd_id = SFTP_CMD_ID;
5267
5268 pr_trace_msg(trace_channel, 9, "reading KEXINIT message from client");
5269
5270 res = read_kexinit(pkt, kex);
5271 if (res < 0) {
5272 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
5273
5274 destroy_kex(kex);
5275 destroy_pool(pkt->pool);
5276 return -1;
5277 }
5278
5279 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
5280 destroy_pool(pkt->pool);
5281 cmd = NULL;
5282
5283 pr_trace_msg(trace_channel, 9,
5284 "determining shared algorithms for SSH session");
5285
5286 if (get_session_names(kex, &correct_guess) < 0) {
5287 destroy_kex(kex);
5288 return -1;
5289 }
5290
5291 if (use_strict_kex == TRUE &&
5292 kex_done_first_kex == FALSE) {
5293 uint32_t client_seqno;
5294
5295 client_seqno = sftp_ssh2_packet_get_client_seqno();
5296 if (client_seqno != 1) {
5297 /* Receiving any messages other than a KEXINIT as the first client
5298 * message indicates the possibility of the Terrapin attack being
5299 * conducted (Issue 1760). Thus we disconnect the client in such
5300 * cases.
5301 */
5302 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5303 "'strict KEX' violation, as KEXINIT was not the first message; disconnecting");
5304 destroy_kex(kex);
5305 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
5306 }
5307 }
5308
5309 /* Once we have received the client KEXINIT message, we can compare what we
5310 * want to send against what we already received from the client.
5311 *
5312 * If the client said that it was going to send a "guess" KEX packet,
5313 * and we determine that its key exchange guess matches what we would have
5314 * sent in our KEXINIT, then we proceed on with reading and handling that
5315 * guess packet. If not, we ignore that packet, and proceed.
5316 */
5317
5318 if (!kex->first_kex_follows) {
5319 /* No guess packet sent; send our KEXINIT as normal (as long as we are
5320 * not in a server-initiated rekeying).
5321 */
5322
5323 if (!kex_sent_kexinit) {
5324 pkt = sftp_ssh2_packet_create(kex_pool);
5325 res = write_kexinit(pkt, kex);
5326 if (res < 0) {
5327 destroy_kex(kex);
5328 destroy_pool(pkt->pool);
5329 return -1;
5330 }
5331
5332 pr_trace_msg(trace_channel, 9, "sending KEXINIT message to client");
5333
5334 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5335 if (res < 0) {
5336 destroy_kex(kex);
5337 destroy_pool(pkt->pool);
5338 return res;
5339 }
5340
5341 kex_sent_kexinit = TRUE;
5342 destroy_pool(pkt->pool);
5343 }
5344
5345 } else {
5346
5347 /* If the client sent a guess kex packet, but that guess was incorrect,
5348 * then we need to consume and silently ignore that packet, and proceed
5349 * as normal.
5350 */
5351 if (correct_guess == FALSE) {
5352 pr_trace_msg(trace_channel, 3, "client sent incorrect key exchange "
5353 "guess, ignoring guess packet");
5354
5355 pkt = read_kex_packet(kex_pool, kex,
5356 SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, &msg_type, 3,
5357 SFTP_SSH2_MSG_KEX_DH_INIT,
5358 SFTP_SSH2_MSG_KEX_DH_GEX_INIT,
5359 SFTP_SSH2_MSG_KEX_ECDH_INIT);
5360
5361 pr_trace_msg(trace_channel, 3,
5362 "ignored %s (%d) guess message sent by client",
5363 sftp_ssh2_packet_get_msg_type_desc(msg_type), msg_type);
5364
5365 destroy_pool(pkt->pool);
5366
5367 if (kex_sent_kexinit == FALSE) {
5368 pkt = sftp_ssh2_packet_create(kex_pool);
5369 res = write_kexinit(pkt, kex);
5370 if (res < 0) {
5371 destroy_kex(kex);
5372 destroy_pool(pkt->pool);
5373 return -1;
5374 }
5375
5376 pr_trace_msg(trace_channel, 9, "sending KEXINIT message to client");
5377
5378 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5379 if (res < 0) {
5380 destroy_kex(kex);
5381 destroy_pool(pkt->pool);
5382 return res;
5383 }
5384
5385 kex_sent_kexinit = TRUE;
5386 destroy_pool(pkt->pool);
5387 }
5388 }
5389
5390 if (kex_sent_kexinit == FALSE) {
5391 pkt = sftp_ssh2_packet_create(kex_pool);
5392 res = write_kexinit(pkt, kex);
5393 if (res < 0) {
5394 destroy_kex(kex);
5395 destroy_pool(pkt->pool);
5396 return -1;
5397 }
5398
5399 pr_trace_msg(trace_channel, 9, "sending KEXINIT message to client");
5400
5401 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5402 if (res < 0) {
5403 destroy_kex(kex);
5404 destroy_pool(pkt->pool);
5405 return res;
5406 }
5407
5408 kex_sent_kexinit = TRUE;
5409 destroy_pool(pkt->pool);
5410 }
5411 }
5412
5413 if (!kex->use_kexrsa) {
5414 /* Read the client key exchange message. */
5415 pkt = read_kex_packet(kex_pool, kex,
5416 SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, &msg_type, 3,
5417 SFTP_SSH2_MSG_KEX_DH_INIT,
5418 SFTP_SSH2_MSG_KEX_DH_GEX_REQUEST,
5419 SFTP_SSH2_MSG_KEX_ECDH_INIT);
5420
5421 switch (msg_type) {
5422 case SFTP_SSH2_MSG_KEX_DH_INIT:
5423 /* This handles the case of SFTP_SSH2_MSG_KEX_DH_GEX_REQUEST_OLD as
5424 * well; that ID has the same value as the KEX_DH_INIT ID.
5425 */
5426 #if defined(PR_USE_SODIUM) && defined(HAVE_SHA256_OPENSSL)
5427 if (kex->use_curve25519) {
5428 res = handle_kex_curve25519(pkt, kex);
5429
5430 } else
5431 #endif /* PR_USE_SODIUM and HAVE_SHA256_OPENSSL */
5432 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
5433 if (kex->use_curve448) {
5434 res = handle_kex_curve448(pkt, kex);
5435
5436 } else
5437 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
5438 #if defined(PR_USE_OPENSSL_ECC)
5439 if (kex->use_ecdh) {
5440 res = handle_kex_ecdh(pkt, kex);
5441
5442 } else
5443 #endif /* PR_USE_OPENSSL_ECC */
5444
5445 if (kex->use_gex) {
5446 res = handle_kex_dh_gex(pkt, kex, TRUE);
5447
5448 } else {
5449 res = handle_kex_dh(pkt, kex);
5450 }
5451
5452 if (res < 0) {
5453 destroy_kex(kex);
5454 destroy_pool(pkt->pool);
5455 return -1;
5456 }
5457 break;
5458
5459 case SFTP_SSH2_MSG_KEX_DH_GEX_REQUEST:
5460 res = handle_kex_dh_gex(pkt, kex, FALSE);
5461 if (res < 0) {
5462 destroy_kex(kex);
5463 destroy_pool(pkt->pool);
5464 return -1;
5465 }
5466 break;
5467
5468 default:
5469 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5470 #if defined(PR_USE_OPENSSL_ECC)
5471 "expecting KEX_DH_INIT, KEX_ECDH_INIT or KEX_DH_GEX_GROUP message, "
5472 #else
5473 "expecting KEX_DH_INIT or KEX_DH_GEX_GROUP message, "
5474 #endif /* PR_USE_OPENSSL_ECC */
5475 "received %s (%d), disconnecting",
5476 sftp_ssh2_packet_get_msg_type_desc(msg_type), msg_type);
5477 destroy_kex(kex);
5478 destroy_pool(pkt->pool);
5479 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_ERROR, NULL);
5480 }
5481
5482 /* Note: All of the above handle_kex_*() functions are REQUIRED to have
5483 * destroyed the pkt->pool themselves, thus we do NOT need to do it here.
5484 */
5485
5486 } else {
5487 res = handle_kex_rsa(kex);
5488 if (res < 0) {
5489 destroy_kex(kex);
5490 return -1;
5491 }
5492 }
5493
5494 if (!sftp_interop_supports_feature(SFTP_SSH2_FEAT_PESSIMISTIC_NEWKEYS)) {
5495 pr_trace_msg(trace_channel, 9, "sending NEWKEYS message to client");
5496
5497 /* Send our NEWKEYS reply. */
5498 pkt = sftp_ssh2_packet_create(kex_pool);
5499 res = write_newkeys_reply(pkt);
5500 if (res < 0) {
5501 destroy_kex(kex);
5502 destroy_pool(pkt->pool);
5503 return -1;
5504 }
5505
5506 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5507 if (res < 0) {
5508 destroy_kex(kex);
5509 destroy_pool(pkt->pool);
5510 return -1;
5511 }
5512
5513 destroy_pool(pkt->pool);
5514 sent_newkeys = TRUE;
5515 }
5516
5517 pkt = read_kex_packet(kex_pool, kex, SFTP_SSH2_DISCONNECT_PROTOCOL_ERROR,
5518 NULL, 1, SFTP_SSH2_MSG_NEWKEYS);
5519
5520 /* If we didn't send our NEWKEYS message earlier, do it now. */
5521 if (sent_newkeys == FALSE) {
5522 struct ssh2_packet *pkt2;
5523
5524 pr_trace_msg(trace_channel, 9, "sending NEWKEYS message to client");
5525
5526 /* Send our NEWKEYS reply. */
5527 pkt2 = sftp_ssh2_packet_create(kex_pool);
5528 res = write_newkeys_reply(pkt2);
5529 if (res < 0) {
5530 destroy_kex(kex);
5531 destroy_pool(pkt2->pool);
5532 return -1;
5533 }
5534
5535 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt2);
5536 if (res < 0) {
5537 destroy_kex(kex);
5538 destroy_pool(pkt2->pool);
5539 return -1;
5540 }
5541
5542 destroy_pool(pkt2->pool);
5543 }
5544
5545 if (use_strict_kex == TRUE) {
5546 sftp_ssh2_packet_reset_client_seqno();
5547 sftp_ssh2_packet_reset_server_seqno();
5548 }
5549
5550 /* Last but certainly not least, set up the keys for encryption and
5551 * authentication, based on H and K.
5552 */
5553 pr_trace_msg(trace_channel, 9, "setting session keys");
5554 if (set_session_keys(kex) < 0) {
5555 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5556 "error setting session keys, disconnecting");
5557 destroy_kex(kex);
5558 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
5559 }
5560
5561 cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "NEWKEYS"));
5562 cmd->arg = "";
5563 cmd->cmd_class = CL_SEC|CL_SSH;
5564 cmd->cmd_id = SFTP_CMD_ID;
5565
5566 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
5567 destroy_pool(pkt->pool);
5568 cmd = NULL;
5569
5570 /* We've now completed our KEX, possibly our first. */
5571 kex_done_first_kex = TRUE;
5572
5573 /* If extension negotiation has not been disabled, AND if we have not
5574 * received a service request, AND if the client sent "ext-info-c", THEN
5575 * send our EXT_INFO. We do not want send this during rekeys.
5576 */
5577 if (!(sftp_opts & SFTP_OPT_NO_EXT_INFO) &&
5578 kex->use_ext_info == TRUE &&
5579 !(sftp_sess_state & SFTP_SESS_STATE_HAVE_SERVICE)) {
5580 struct ssh2_packet *pkt2;
5581
5582 pr_trace_msg(trace_channel, 9, "sending EXT_INFO message to client");
5583
5584 pkt2 = sftp_ssh2_packet_create(kex_pool);
5585 res = write_ext_info(pkt2);
5586 if (res < 0) {
5587 destroy_kex(kex);
5588 destroy_pool(pkt2->pool);
5589 return -1;
5590 }
5591
5592 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt2);
5593 if (res < 0) {
5594 destroy_kex(kex);
5595 destroy_pool(pkt2->pool);
5596 return -1;
5597 }
5598
5599 cmd = pr_cmd_alloc(pkt2->pool, 1, pstrdup(pkt2->pool, "EXT_INFO"));
5600 cmd->arg = "";
5601 cmd->cmd_class = CL_INFO|CL_SSH;
5602 cmd->cmd_id = SFTP_CMD_ID;
5603
5604 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
5605
5606 destroy_pool(pkt2->pool);
5607 cmd = NULL;
5608 }
5609
5610 /* If any CBC mode ciphers have been negotiated for the server-to-client
5611 * stream, then we need to use the 'rogaway' TAP policy.
5612 */
5613 write_algo = sftp_cipher_get_write_algo();
5614 if (strncmp(write_algo + strlen(write_algo) - 4, "-cbc", 4) == 0) {
5615 const char *policy = "rogaway";
5616
5617 pr_trace_msg("ssh2", 4, "CBC mode cipher chosen for server-to-client "
5618 "messages, automatically enabling '%s' TAP policy", policy);
5619
5620 if (sftp_tap_set_policy(policy) < 0) {
5621 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5622 "error setting TrafficPolicy '%s': %s", policy, strerror(errno));
5623 }
5624 }
5625
5626 /* Only start the TAP timer after we have completed our first KEX.
5627 * Otherwise, we risk sending "illegal" packets prior to, or during,
5628 * a "strict KEX" session (Issue 1760).
5629 */
5630 sftp_tap_start_policy();
5631
5632 /* Reset this flag for the next time through. */
5633 kex_sent_kexinit = FALSE;
5634
5635 destroy_kex(kex);
5636 return 0;
5637 }
5638
5639 int sftp_kex_free(void) {
5640 struct sftp_kex *first_kex, *rekey_kex;
5641
5642 if (kex_dhparams_fp != NULL) {
5643 (void) fclose(kex_dhparams_fp);
5644 kex_dhparams_fp = NULL;
5645 }
5646
5647 /* destroy_kex() will set the kex_first_kex AND kex_rekey_kex pointers to
5648 * null, so we need to keep our own copies of those pointers here.
5649 */
5650 first_kex = kex_first_kex;
5651 rekey_kex = kex_rekey_kex;
5652
5653 if (first_kex != NULL) {
5654 destroy_kex(first_kex);
5655 }
5656
5657 if (rekey_kex != NULL) {
5658 destroy_kex(rekey_kex);
5659 }
5660
5661 if (kex_pool != NULL) {
5662 destroy_pool(kex_pool);
5663 kex_pool = NULL;
5664 }
5665
5666 return 0;
5667 }
5668
5669 int sftp_kex_init(const char *client_version, const char *server_version) {
5670 /* If we are called with client_version and server_version both NULL,
5671 * then we're setting up for a rekey. We can destroy/create the Kex
5672 * pool in that case. But not otherwise.
5673 */
5674 if (client_version == NULL &&
5675 server_version == NULL) {
5676 if (kex_pool) {
5677 destroy_pool(kex_pool);
5678 kex_pool = NULL;
5679 }
5680 }
5681
5682 if (kex_pool == NULL) {
5683 kex_pool = make_sub_pool(sftp_pool);
5684 pr_pool_tag(kex_pool, "Kex Pool");
5685 }
5686
5687 /* Save the client and server versions, the first time through. They
5688 * will be used for any future rekey KEXINIT exchanges.
5689 */
5690
5691 if (client_version != NULL &&
5692 kex_client_version == NULL) {
5693 kex_client_version = pstrdup(sftp_pool, client_version);
5694 }
5695
5696 if (server_version != NULL &&
5697 kex_server_version == NULL) {
5698 kex_server_version = pstrdup(sftp_pool, server_version);
5699 }
5700
5701 return 0;
5702 }
5703
5704 int sftp_kex_rekey(void) {
5705 int res;
5706 struct ssh2_packet *pkt;
5707
5708 /* We cannot perform a rekey if we have not even finished the first kex. */
5709 if (!(sftp_sess_state & SFTP_SESS_STATE_HAVE_KEX)) {
5710 pr_trace_msg(trace_channel, 3,
5711 "unable to request rekey: KEX not completed");
5712
5713 /* If this was triggered by a rekey timer, register a new timer and
5714 * try the rekey request in another 5 seconds.
5715 */
5716 if (kex_rekey_interval > 0 &&
5717 kex_rekey_timerno == -1) {
5718 pr_trace_msg(trace_channel, 3,
5719 "trying rekey request in another 5 seconds");
5720 kex_rekey_timerno = pr_timer_add(5, -1, &sftp_module, kex_rekey_timer_cb,
5721 "SFTP KEX Rekey timer");
5722 }
5723
5724 return 0;
5725 }
5726
5727 if (!sftp_interop_supports_feature(SFTP_SSH2_FEAT_REKEYING)) {
5728 pr_trace_msg(trace_channel, 3,
5729 "unable to request rekeying: Not supported by client");
5730 sftp_ssh2_packet_rekey_reset();
5731 return 0;
5732 }
5733
5734 /* If already rekeying, return now. */
5735 if (sftp_sess_state & SFTP_SESS_STATE_REKEYING) {
5736 pr_trace_msg(trace_channel, 17,
5737 "rekeying already in effect, ignoring rekey request");
5738 return 0;
5739 }
5740
5741 /* If the client has NOT authenticated by this point in time, try again
5742 * later. Some clients do not deal well with having their authentication
5743 * processes interrupted by a rekey (Bug#4254).
5744 */
5745 if (!(sftp_sess_state & SFTP_SESS_STATE_HAVE_AUTH)) {
5746 pr_trace_msg(trace_channel, 17,
5747 "authentication not completed, delaying rekey request");
5748
5749 /* If the rekey interval is so short as to interfere the client before
5750 * it has authenticated, assume the rekey interval is short enough to
5751 * fire again soon, such that we do not need to change its schedule.
5752 */
5753 return 1;
5754 }
5755
5756 /* Make sure that any rekey timer will try not to interfere while the
5757 * rekeying is happening.
5758 */
5759 if (kex_rekey_timerno != -1) {
5760 pr_timer_remove(kex_rekey_timerno, &sftp_module);
5761 kex_rekey_timerno = -1;
5762 }
5763
5764 pr_trace_msg(trace_channel, 17, "sending rekey KEXINIT");
5765
5766 /* Some SSH2 clients are very particular about rekeying, and do NOT want
5767 * other data while they are rekeying. Other clients are more forgiving.
5768 * For the strict clients, we set the REKEYING flag here, such that the
5769 * Channel API will buffer up its responses until the rekeying completes.
5770 */
5771 if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_NO_DATA_WHILE_REKEYING)) {
5772 sftp_sess_state |= SFTP_SESS_STATE_REKEYING;
5773 }
5774
5775 sftp_kex_init(NULL, NULL);
5776
5777 kex_rekey_kex = create_kex(kex_pool);
5778
5779 pr_trace_msg(trace_channel, 9, "writing KEXINIT message to client");
5780
5781 /* Send our KEXINIT message. */
5782 pkt = sftp_ssh2_packet_create(kex_pool);
5783 res = write_kexinit(pkt, kex_rekey_kex);
5784 if (res < 0) {
5785 destroy_pool(pkt->pool);
5786 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
5787 }
5788
5789 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5790 if (res < 0) {
5791 destroy_pool(pkt->pool);
5792 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, NULL);
5793 }
5794
5795 destroy_pool(pkt->pool);
5796
5797 kex_sent_kexinit = TRUE;
5798
5799 if (kex_rekey_timeout > 0) {
5800 pr_trace_msg(trace_channel, 17, "client has %d %s to rekey",
5801 kex_rekey_timeout, kex_rekey_timeout != 1 ? "secs" : "sec");
5802 kex_rekey_timeout_timerno = pr_timer_add(kex_rekey_timeout, -1,
5803 &sftp_module, kex_rekey_timeout_cb, "SFTP KEX Rekey Timeout timer");
5804 }
5805
5806 return 0;
5807 }
5808
5809 enum sftp_key_type_e sftp_kex_get_hostkey_type(void) {
5810 return kex_used_hostkey_type;
5811 }
5812
5813 int sftp_kex_rekey_set_interval(int rekey_interval) {
5814 if (rekey_interval < 0) {
5815 errno = EINVAL;
5816 return -1;
5817 }
5818
5819 kex_rekey_interval = rekey_interval;
5820 return 0;
5821 }
5822
5823 int sftp_kex_rekey_set_timeout(int timeout) {
5824 if (timeout < 0) {
5825 errno = EINVAL;
5826 return -1;
5827 }
5828
5829 kex_rekey_timeout = timeout;
5830 return 0;
5831 }
5832
5833 int sftp_kex_send_first_kexinit(void) {
5834 struct ssh2_packet *pkt;
5835 int res;
5836
5837 if (kex_pool == NULL) {
5838 kex_pool = make_sub_pool(sftp_pool);
5839 pr_pool_tag(kex_pool, "Kex Pool");
5840 }
5841
5842 /* The client has just connected to us. We want to send our version
5843 * ID string _and_ the KEXINIT in the same TCP packet, and save a
5844 * TCP round trip (one TCP ACK for both messages, rather than one ACK
5845 * per message). The packet API will automatically send the version
5846 * ID string along with the first packet we send; we just have to
5847 * send a packet, and the KEXINIT is the first one in the protocol.
5848 */
5849 kex_first_kex = create_kex(kex_pool);
5850
5851 pkt = sftp_ssh2_packet_create(kex_pool);
5852 res = write_kexinit(pkt, kex_first_kex);
5853 if (res < 0) {
5854 destroy_kex(kex_first_kex);
5855 destroy_pool(pkt->pool);
5856 return -1;
5857 }
5858
5859 pr_trace_msg(trace_channel, 9, "sending KEXINIT message to client");
5860
5861 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
5862 if (res < 0) {
5863 destroy_kex(kex_first_kex);
5864 destroy_pool(pkt->pool);
5865 return -1;
5866 }
5867 kex_sent_kexinit = TRUE;
5868
5869 destroy_pool(pkt->pool);
5870 return 0;
5871 }