Debian Codegraph @

contrib/mod_sftp/cipher.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp ciphers
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
27 #include "packet.h"
28 #include "msg.h"
29 #include "crypto.h"
30 #include "cipher.h"
31 #include "session.h"
32 #include "interop.h"
33 #include "poly1305.h"
34
35 struct sftp_cipher {
36 pool *pool;
37 const char *algo;
38 unsigned int algo_type;
39 const EVP_CIPHER *cipher;
40
41 unsigned char *iv;
42 uint32_t iv_len;
43
44 unsigned char *key;
45 uint32_t key_len;
46
47 uint32_t auth_len;
48 size_t discard_len;
49 };
50
51 #define SFTP_CIPHER_ALGO_NONE 1
52 #define SFTP_CIPHER_ALGO_GCM 2
53 #define SFTP_CIPHER_ALGO_CHACHA 3
54
55 /* We need to keep the old ciphers around, so that we can handle N
56 * arbitrary packets to/from the client using the old keys, as during rekeying.
57 * Thus we have two read cipher contexts, two write cipher contexts.
58 * The cipher idx variable indicates which of the ciphers is currently in use.
59 */
60
61 static struct sftp_cipher read_ciphers[2] = {
62 { NULL, NULL, 0, NULL, NULL, 0, NULL, 0, 0, 0 },
63 { NULL, NULL, 0, NULL, NULL, 0, NULL, 0, 0, 0 }
64 };
65 static EVP_CIPHER_CTX *read_ctxs[2];
66 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
67 !defined(HAVE_BROKEN_CHACHA20)
68 static EVP_CIPHER_CTX *read_header_ctxs[2] = { NULL, NULL };
69 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
70
71 static struct sftp_cipher write_ciphers[2] = {
72 { NULL, NULL, 0, NULL, NULL, 0, NULL, 0, 0, 0 },
73 { NULL, NULL, 0, NULL, NULL, 0, NULL, 0, 0, 0 }
74 };
75 static EVP_CIPHER_CTX *write_ctxs[2];
76 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
77 !defined(HAVE_BROKEN_CHACHA20)
78 static EVP_CIPHER_CTX *write_header_ctxs[2] = { NULL, NULL };
79 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
80
81 #define SFTP_CIPHER_DEFAULT_BLOCK_SZ 8
82
83 static size_t read_blockszs[2] = {
84 SFTP_CIPHER_DEFAULT_BLOCK_SZ,
85 SFTP_CIPHER_DEFAULT_BLOCK_SZ
86 };
87
88 static size_t write_blockszs[2] = {
89 SFTP_CIPHER_DEFAULT_BLOCK_SZ,
90 SFTP_CIPHER_DEFAULT_BLOCK_SZ
91 };
92
93 static unsigned int read_cipher_idx = 0;
94 static unsigned int write_cipher_idx = 0;
95
96 static const char *trace_channel = "ssh2";
97
98 static void clear_cipher(struct sftp_cipher *);
99
100 static unsigned int get_next_read_index(void) {
101 if (read_cipher_idx == 1) {
102 return 0;
103 }
104
105 return 1;
106 }
107
108 static unsigned int get_next_write_index(void) {
109 if (write_cipher_idx == 1) {
110 return 0;
111 }
112
113 return 1;
114 }
115
116 static void switch_read_cipher(void) {
117 /* First, clear the context of the existing read cipher, if any. */
118 if (read_ciphers[read_cipher_idx].key != NULL) {
119 clear_cipher(&(read_ciphers[read_cipher_idx]));
120
121 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
122 defined(HAVE_LIBRESSL)
123 if (EVP_CIPHER_CTX_cleanup(read_ctxs[read_cipher_idx]) != 1) {
124 #else
125 if (EVP_CIPHER_CTX_reset(read_ctxs[read_cipher_idx]) != 1) {
126 #endif /* OpenSSL-1.1.x and later */
127 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
128 "error clearing cipher context: %s", sftp_crypto_get_errors());
129 }
130
131 read_blockszs[read_cipher_idx] = SFTP_CIPHER_DEFAULT_BLOCK_SZ;
132
133 /* Now we can switch the index. */
134 if (read_cipher_idx == 1) {
135 read_cipher_idx = 0;
136 return;
137 }
138
139 read_cipher_idx = 1;
140 }
141 }
142
143 static void switch_write_cipher(void) {
144 /* First, clear the context of the existing read cipher, if any. */
145 if (write_ciphers[write_cipher_idx].key != NULL) {
146 clear_cipher(&(write_ciphers[write_cipher_idx]));
147
148 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
149 defined(HAVE_LIBRESSL)
150 if (EVP_CIPHER_CTX_cleanup(write_ctxs[write_cipher_idx]) != 1) {
151 #else
152 if (EVP_CIPHER_CTX_reset(write_ctxs[write_cipher_idx]) != 1) {
153 #endif /* OpenSSL-1.1.x and later */
154 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
155 "error clearing cipher context: %s", sftp_crypto_get_errors());
156 }
157
158 write_blockszs[write_cipher_idx] = SFTP_CIPHER_DEFAULT_BLOCK_SZ;
159
160 /* Now we can switch the index. */
161 if (write_cipher_idx == 1) {
162 write_cipher_idx = 0;
163 return;
164 }
165
166 write_cipher_idx = 1;
167 }
168 }
169
170 static void clear_cipher(struct sftp_cipher *cipher) {
171 if (cipher->iv != NULL) {
172 pr_memscrub(cipher->iv, cipher->iv_len);
173 free(cipher->iv);
174 cipher->iv = NULL;
175 cipher->iv_len = 0;
176 }
177
178 if (cipher->key != NULL) {
179 pr_memscrub(cipher->key, cipher->key_len);
180 free(cipher->key);
181 cipher->key = NULL;
182 cipher->key_len = 0;
183 }
184
185 cipher->cipher = NULL;
186 cipher->algo = NULL;
187 }
188
189 static unsigned int get_algo_type(const char *algo) {
190 unsigned int algo_type = 0;
191 const char *gcm_suffix = "[email protected]";
192
193 if (strcmp(algo, "none") == 0) {
194 algo_type = SFTP_CIPHER_ALGO_NONE;
195
196 } else if (pr_strnrstr(algo, strlen(algo), gcm_suffix,
197 strlen(gcm_suffix), 0) == TRUE) {
198 algo_type = SFTP_CIPHER_ALGO_GCM;
199
200 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
201 !defined(HAVE_BROKEN_CHACHA20)
202 } else if (strcmp(algo, "[email protected]") == 0) {
203 algo_type = SFTP_CIPHER_ALGO_CHACHA;
204 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
205 }
206
207 return algo_type;
208 }
209
210 static int set_cipher_iv(struct sftp_cipher *cipher, const EVP_MD *hash,
211 const unsigned char *k, uint32_t klen, const char *h, uint32_t hlen,
212 char *letter, const unsigned char *id, uint32_t id_len) {
213 EVP_MD_CTX *ctx;
214 unsigned char *iv = NULL;
215 size_t cipher_iv_len = 0, iv_sz = 0;
216 uint32_t iv_len = 0;
217
218 if (cipher->algo_type == SFTP_CIPHER_ALGO_NONE) {
219 cipher->iv = iv;
220 cipher->iv_len = iv_len;
221
222 return 0;
223 }
224
225 /* Some ciphers do not use IVs; handle this case. */
226 cipher_iv_len = EVP_CIPHER_iv_length(cipher->cipher);
227 if (cipher_iv_len != 0) {
228 iv_sz = sftp_crypto_get_size(cipher_iv_len, EVP_MD_size(hash));
229
230 } else {
231 iv_sz = EVP_MD_size(hash);
232 }
233
234 if (iv_sz == 0) {
235 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
236 "unable to determine IV length for cipher '%s'", cipher->algo);
237 errno = EINVAL;
238 return -1;
239 }
240
241 iv = malloc(iv_sz);
242 if (iv == NULL) {
243 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
244 _exit(1);
245 }
246
247 ctx = EVP_MD_CTX_create();
248 if (EVP_DigestInit(ctx, hash) != 1) {
249 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
250 "unable to initialize MD context for '%s': %s", EVP_MD_name(hash),
251 sftp_crypto_get_errors());
252 free(iv);
253 errno = EINVAL;
254 return -1;
255 }
256
257 if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_CIPHER_USE_K)) {
258 EVP_DigestUpdate(ctx, k, klen);
259 }
260
261 if (EVP_DigestUpdate(ctx, h, hlen) != 1) {
262 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
263 "unable to update MD context for '%s': %s", EVP_MD_name(hash),
264 sftp_crypto_get_errors());
265 free(iv);
266 errno = EINVAL;
267 return -1;
268 }
269
270 EVP_DigestUpdate(ctx, letter, sizeof(char));
271 EVP_DigestUpdate(ctx, (char *) id, id_len);
272
273 if (EVP_DigestFinal(ctx, iv, &iv_len) != 1) {
274 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
275 "unable to finish MD context for '%s': %s", EVP_MD_name(hash),
276 sftp_crypto_get_errors());
277 free(iv);
278 errno = EINVAL;
279 return -1;
280 }
281
282 EVP_MD_CTX_destroy(ctx);
283
284 /* If we need more, keep hashing, as per RFC, until we have enough
285 * material.
286 */
287 while (iv_sz > iv_len) {
288 uint32_t len = iv_len;
289
290 pr_signals_handle();
291
292 ctx = EVP_MD_CTX_create();
293 EVP_DigestInit(ctx, hash);
294 if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_CIPHER_USE_K)) {
295 EVP_DigestUpdate(ctx, k, klen);
296 }
297 EVP_DigestUpdate(ctx, h, hlen);
298 EVP_DigestUpdate(ctx, iv, len);
299 EVP_DigestFinal(ctx, iv + len, &len);
300 EVP_MD_CTX_destroy(ctx);
301
302 iv_len += len;
303 }
304
305 cipher->iv = iv;
306 cipher->iv_len = iv_len;
307
308 return 0;
309 }
310
311 static int set_cipher_key(struct sftp_cipher *cipher, const EVP_MD *hash,
312 const unsigned char *k, uint32_t klen, const char *h, uint32_t hlen,
313 char letter, const unsigned char *id, uint32_t id_len) {
314 EVP_MD_CTX *ctx;
315 unsigned char *key = NULL;
316 size_t key_sz = 0;
317 uint32_t key_len = 0;
318
319 if (cipher->algo_type == SFTP_CIPHER_ALGO_NONE) {
320 cipher->key = key;
321 cipher->key_len = key_len;
322
323 return 0;
324 }
325
326 key_sz = sftp_crypto_get_size(cipher->key_len > 0 ?
327 cipher->key_len : (size_t) EVP_CIPHER_key_length(cipher->cipher),
328 EVP_MD_size(hash));
329 if (key_sz == 0) {
330 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
331 "unable to determine key length for cipher '%s'", cipher->algo);
332 errno = EINVAL;
333 return -1;
334 }
335
336 pr_trace_msg(trace_channel, 19, "setting key (%lu bytes) for cipher %s",
337 (unsigned long) key_sz, cipher->algo);
338
339 key = malloc(key_sz);
340 if (key == NULL) {
341 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
342 _exit(1);
343 }
344
345 ctx = EVP_MD_CTX_create();
346 if (EVP_DigestInit(ctx, hash) != 1) {
347 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
348 "unable to initialize MD context for '%s': %s", EVP_MD_name(hash),
349 sftp_crypto_get_errors());
350 free(key);
351 errno = EINVAL;
352 return -1;
353 }
354
355 if (EVP_DigestUpdate(ctx, k, klen) != 1) {
356 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
357 "unable to update MD context for '%s': %s", EVP_MD_name(hash),
358 sftp_crypto_get_errors());
359 free(key);
360 errno = EINVAL;
361 return -1;
362 }
363
364 EVP_DigestUpdate(ctx, h, hlen);
365 EVP_DigestUpdate(ctx, &letter, sizeof(letter));
366 EVP_DigestUpdate(ctx, (char *) id, id_len);
367
368 if (EVP_DigestFinal(ctx, key, &key_len) != 1) {
369 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
370 "unable to finish MD context for '%s': %s", EVP_MD_name(hash),
371 sftp_crypto_get_errors());
372 free(key);
373 errno = EINVAL;
374 return -1;
375 }
376
377 EVP_MD_CTX_destroy(ctx);
378
379 pr_trace_msg(trace_channel, 19,
380 "hashed data to produce key (%lu of %lu bytes)", (unsigned long) key_len,
381 (unsigned long) key_sz);
382
383 /* If we need more, keep hashing, as per RFC, until we have enough
384 * material.
385 */
386 while (key_sz > key_len) {
387 uint32_t len = key_len;
388
389 pr_signals_handle();
390
391 ctx = EVP_MD_CTX_create();
392 EVP_DigestInit(ctx, hash);
393 EVP_DigestUpdate(ctx, k, klen);
394 EVP_DigestUpdate(ctx, h, hlen);
395 EVP_DigestUpdate(ctx, key, len);
396 EVP_DigestFinal(ctx, key + len, &len);
397 EVP_MD_CTX_destroy(ctx);
398
399 key_len += len;
400 }
401
402 cipher->key = key;
403
404 return 0;
405 }
406
407 /* If the chosen cipher requires that we discard some of the initial bytes of
408 * the cipher stream, then do so. (This is mostly for any RC4 ciphers.)
409 */
410 static int set_cipher_discarded(struct sftp_cipher *cipher,
411 EVP_CIPHER_CTX *pctx) {
412 unsigned char *garbage_in, *garbage_out;
413
414 if (cipher->discard_len == 0) {
415 return 0;
416 }
417
418 garbage_in = malloc(cipher->discard_len);
419 if (garbage_in == NULL) {
420 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
421 _exit(1);
422 }
423
424 garbage_out = malloc(cipher->discard_len);
425 if (garbage_out == NULL) {
426 free(garbage_in);
427 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
428 _exit(1);
429 }
430
431 if (EVP_Cipher(pctx, garbage_out, garbage_in,
432 cipher->discard_len) != 1) {
433 free(garbage_in);
434 pr_memscrub(garbage_out, cipher->discard_len);
435 free(garbage_out);
436 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
437 "error ciphering discard data: %s", sftp_crypto_get_errors());
438
439 return -1;
440 }
441
442 pr_trace_msg(trace_channel, 19, "discarded %lu bytes of cipher data",
443 (unsigned long) cipher->discard_len);
444 free(garbage_in);
445 pr_memscrub(garbage_out, cipher->discard_len);
446 free(garbage_out);
447
448 return 0;
449 }
450
451 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
452 !defined(HAVE_BROKEN_CHACHA20)
453 /* Note that the given poly_key buffer MUST be POLY1305_KEYLEN in size. */
454 static int compute_chachapoly_key(struct ssh2_packet *pkt,
455 EVP_CIPHER_CTX *pctx, unsigned char *poly_key) {
456 unsigned char seqnobuf[16], *ptr;
457 uint32_t len;
458
459 /* Initialize our IV for the ChaCha cipher. */
460 memset(seqnobuf, 0, sizeof(seqnobuf));
461 ptr = seqnobuf + 8;
462 len = 8;
463 sftp_msg_write_long(&ptr, &len, pkt->seqno);
464
465 if (EVP_CipherInit(pctx, NULL, NULL, seqnobuf, 1) != 1) {
466 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
467 "error initializing ChaChaPoly cipher for encryption: %s",
468 sftp_crypto_get_errors());
469 return -1;
470 }
471
472 memset(poly_key, 0, POLY1305_KEYLEN);
473 if (EVP_Cipher(pctx, poly_key, poly_key, POLY1305_KEYLEN) < 0) {
474 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
475 "error computing ChaChaPoly packet key: %s", sftp_crypto_get_errors());
476 return -1;
477 }
478
479 return 0;
480 }
481 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
482
483 #if !defined(HAVE_TIMINGSAFE_BCMP) && \
484 defined(HAVE_EVP_CHACHA20_OPENSSL) && \
485 !defined(HAVE_BROKEN_CHACHA20)
486 static int timingsafe_bcmp(const void *b1, const void *b2, size_t n) {
487 const unsigned char *p1 = b1, *p2 = b2;
488 int ret = 0;
489
490 for (; n > 0; n--) {
491 ret |= *p1++ ^ *p2++;
492 }
493
494 return (ret != 0);
495 }
496 #endif /* HAVE_TIMINGSAFE_BCMP and HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
497
498 /* These accessors to get the authenticated data length for the read, write
499 * ciphers are used during packet IO, and thus do not return the AAD lengths
500 * until those ciphers are keyed.
501 *
502 * However, during KEX, there are times when we want to know the ADD lengths
503 * after the algorithms are selected, but before they are keyed. Thus for
504 * those cases, we have the accessor variants.
505 */
506
507 size_t sftp_cipher_get_read_auth_size2(void) {
508 return read_ciphers[read_cipher_idx].auth_len;
509 }
510
511 size_t sftp_cipher_get_read_auth_size(void) {
512 /* Do not indicate the read cipher authentication tag size until the
513 * cipher has been keyed.
514 */
515 if (read_ciphers[read_cipher_idx].key != NULL) {
516 return sftp_cipher_get_read_auth_size2();
517 }
518
519 return 0;
520 }
521
522 size_t sftp_cipher_get_write_auth_size2(void) {
523 return write_ciphers[write_cipher_idx].auth_len;
524 }
525
526 size_t sftp_cipher_get_write_auth_size(void) {
527 /* Do not indicate the write cipher authentication tag size until the
528 * cipher has been keyed.
529 */
530 if (write_ciphers[write_cipher_idx].key != NULL) {
531 return sftp_cipher_get_write_auth_size2();
532 }
533
534 return 0;
535 }
536
537 size_t sftp_cipher_get_read_block_size(void) {
538 return read_blockszs[read_cipher_idx];
539 }
540
541 size_t sftp_cipher_get_write_block_size(void) {
542 return write_blockszs[write_cipher_idx];
543 }
544
545 void sftp_cipher_set_read_block_size(size_t blocksz) {
546 if (blocksz > read_blockszs[read_cipher_idx]) {
547 read_blockszs[read_cipher_idx] = blocksz;
548 }
549 }
550
551 void sftp_cipher_set_write_block_size(size_t blocksz) {
552 if (blocksz > write_blockszs[write_cipher_idx]) {
553 write_blockszs[write_cipher_idx] = blocksz;
554 }
555 }
556
557 int sftp_cipher_is_read_chachapoly(void) {
558 if (read_ciphers[read_cipher_idx].key != NULL &&
559 read_ciphers[read_cipher_idx].algo_type == SFTP_CIPHER_ALGO_CHACHA) {
560 return TRUE;
561 }
562
563 return FALSE;
564 }
565
566 const char *sftp_cipher_get_read_algo(void) {
567 if (read_ciphers[read_cipher_idx].key != NULL ||
568 read_ciphers[read_cipher_idx].algo_type == SFTP_CIPHER_ALGO_NONE) {
569 return read_ciphers[read_cipher_idx].algo;
570 }
571
572 return NULL;
573 }
574
575 int sftp_cipher_set_read_algo(const char *algo) {
576 unsigned int idx = read_cipher_idx;
577 size_t key_len = 0, auth_len = 0, discard_len = 0;
578
579 if (read_ciphers[idx].key != NULL) {
580 /* If we have an existing key, it means that we are currently rekeying. */
581 idx = get_next_read_index();
582 }
583
584 read_ciphers[idx].cipher = sftp_crypto_get_cipher(algo, &key_len, &auth_len,
585 &discard_len);
586 if (read_ciphers[idx].cipher == NULL) {
587 return -1;
588 }
589
590 if (key_len > 0) {
591 pr_trace_msg(trace_channel, 19,
592 "setting read key for cipher %s: key len = %lu", algo,
593 (unsigned long) key_len);
594 }
595
596 if (auth_len > 0) {
597 pr_trace_msg(trace_channel, 19,
598 "setting read key for cipher %s: auth len = %lu", algo,
599 (unsigned long) auth_len);
600 }
601
602 if (discard_len > 0) {
603 pr_trace_msg(trace_channel, 19,
604 "setting read key for cipher %s: discard len = %lu", algo,
605 (unsigned long) discard_len);
606 }
607
608 /* Note that we use a new pool, each time the algorithm is set (which
609 * happens during key exchange) to prevent undue memory growth for
610 * long-lived sessions with many rekeys.
611 */
612 if (read_ciphers[idx].pool != NULL) {
613 destroy_pool(read_ciphers[idx].pool);
614 }
615
616 read_ciphers[idx].pool = make_sub_pool(sftp_pool);
617 pr_pool_tag(read_ciphers[idx].pool, "SFTP cipher read pool");
618 read_ciphers[idx].algo = pstrdup(read_ciphers[idx].pool, algo);
619 read_ciphers[idx].algo_type = get_algo_type(algo);
620
621 read_ciphers[idx].key_len = (uint32_t) key_len;
622 read_ciphers[idx].auth_len = (uint32_t) auth_len;
623 read_ciphers[idx].discard_len = discard_len;
624
625 return 0;
626 }
627
628 int sftp_cipher_set_read_key(pool *p, const EVP_MD *hash,
629 const unsigned char *k, uint32_t klen, const char *h, uint32_t hlen,
630 int role) {
631 const unsigned char *id = NULL;
632 char letter;
633 uint32_t id_len;
634 int key_len, auth_len;
635 struct sftp_cipher *cipher;
636 EVP_CIPHER_CTX *pctx, *hpctx = NULL;
637
638 switch_read_cipher();
639
640 cipher = &(read_ciphers[read_cipher_idx]);
641 pctx = read_ctxs[read_cipher_idx];
642 if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
643 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
644 !defined(HAVE_BROKEN_CHACHA20)
645 hpctx = read_header_ctxs[read_cipher_idx];
646 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
647 }
648
649 id_len = sftp_session_get_id(&id);
650
651 /* The letters used depend on the role; see:
652 * https://tools.ietf.org/html/rfc4253#section-7.2
653 *
654 * If we are the SERVER, then we use the letters for the "client to server"
655 * flows, since we are READING from the client.
656 */
657
658 /* client-to-server IV: HASH(K || H || "A" || session_id)
659 * server-to-client IV: HASH(K || H || "B" || session_id)
660 */
661 letter = (role == SFTP_ROLE_SERVER ? 'A' : 'B');
662 if (set_cipher_iv(cipher, hash, k, klen, h, hlen, &letter, id, id_len) < 0) {
663 return -1;
664 }
665
666 /* client-to-server key: HASH(K || H || "C" || session_id)
667 * server-to-client key: HASH(K || H || "D" || session_id)
668 */
669 letter = (role == SFTP_ROLE_SERVER ? 'C' : 'D');
670 if (set_cipher_key(cipher, hash, k, klen, h, hlen, letter, id, id_len) < 0) {
671 return -1;
672 }
673
674 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
675 defined(HAVE_LIBRESSL)
676 EVP_CIPHER_CTX_init(pctx);
677 if (hpctx != NULL) {
678 EVP_CIPHER_CTX_init(hpctx);
679 }
680 #else
681 EVP_CIPHER_CTX_reset(pctx);
682 if (hpctx != NULL) {
683 EVP_CIPHER_CTX_reset(hpctx);
684 }
685 #endif /* prior to OpenSSL-1.1.0 */
686
687 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
688 if (EVP_CipherInit_ex(pctx, cipher->cipher, NULL, NULL,
689 cipher->iv, 0) != 1) {
690 #else
691 if (EVP_CipherInit(pctx, cipher->cipher, NULL, cipher->iv, 0) != 1) {
692 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
693 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
694 "error initializing %s cipher for decryption: %s", cipher->algo,
695 sftp_crypto_get_errors());
696 return -1;
697 }
698
699 if (hpctx != NULL) {
700 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
701 if (EVP_CipherInit_ex(hpctx, cipher->cipher, NULL, NULL, NULL, 0) != 1) {
702 #else
703 if (EVP_CipherInit(hpctx, cipher->cipher, NULL, NULL, 0) != 1) {
704 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
705 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
706 "error initializing %s cipher for header decryption: %s", cipher->algo,
707 sftp_crypto_get_errors());
708 return -1;
709 }
710 }
711
712 auth_len = (int) cipher->auth_len;
713 if (auth_len > 0) {
714 if (cipher->algo_type == SFTP_CIPHER_ALGO_GCM) {
715 #if defined(EVP_CTRL_GCM_SET_IV_FIXED)
716 if (EVP_CIPHER_CTX_ctrl(pctx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
717 cipher->iv) != 1) {
718 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
719 "error configuring %s cipher for decryption: %s", cipher->algo,
720 sftp_crypto_get_errors());
721 return -1;
722 }
723 #endif /* EVP_CTRL_GCM_SET_IV_FIXED */
724 pr_trace_msg(trace_channel, 19,
725 "set auth length (%d) for %s cipher for decryption", auth_len,
726 cipher->algo);
727 }
728 }
729
730 /* Next, set the key length. */
731 key_len = (int) cipher->key_len;
732 if (key_len > 0 &&
733 cipher->algo_type != SFTP_CIPHER_ALGO_CHACHA) {
734
735 /* Skip setting our custom key length for ChaCha20, since the custom
736 * key length is used for two different ChaCha20 cipher instances.
737 */
738 if (EVP_CIPHER_CTX_set_key_length(pctx, key_len) != 1) {
739 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
740 "error setting key length (%d bytes) for %s cipher for decryption: %s",
741 key_len, cipher->algo, sftp_crypto_get_errors());
742 return -1;
743 }
744
745 pr_trace_msg(trace_channel, 19,
746 "set key length (%d) for %s cipher for decryption", key_len,
747 cipher->algo);
748 }
749
750 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
751 if (EVP_CipherInit_ex(pctx, NULL, NULL, cipher->key, NULL, -1) != 1) {
752 #else
753 if (EVP_CipherInit(pctx, NULL, cipher->key, NULL, -1) != 1) {
754 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
755 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
756 "error re-initializing %s cipher for decryption: %s", cipher->algo,
757 sftp_crypto_get_errors());
758 return -1;
759 }
760
761 if (hpctx != NULL) {
762 /* The ChaChaPoly header instance uses the "second half" of the computed
763 * session key, per OpenSSH spec.
764 */
765 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
766 if (EVP_CipherInit_ex(hpctx, NULL, NULL, cipher->key + 32, NULL, -1) != 1) {
767 #else
768 if (EVP_CipherInit(hpctx, NULL, cipher->key + 32, NULL, -1) != 1) {
769 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
770 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
771 "error re-initializing %s cipher for header decryption: %s",
772 cipher->algo, sftp_crypto_get_errors());
773 return -1;
774 }
775 }
776
777 if (set_cipher_discarded(cipher, pctx) < 0) {
778 return -1;
779 }
780
781 if (strcmp(cipher->algo, "aes128-ctr") == 0 ||
782 strcmp(cipher->algo, "[email protected]") == 0 ||
783 strcmp(cipher->algo, "aes192-ctr") == 0 ||
784 strcmp(cipher->algo, "aes256-ctr") == 0 ||
785 strcmp(cipher->algo, "[email protected]") == 0) {
786 /* For some reason, OpenSSL returns 8 for the AES CTR/GCM block size (even
787 * though the AES block size is 16, per RFC 5647), but OpenSSH wants 16.
788 */
789 sftp_cipher_set_read_block_size(16);
790
791 } else {
792 sftp_cipher_set_read_block_size(EVP_CIPHER_block_size(cipher->cipher));
793 }
794
795 pr_trace_msg(trace_channel, 19,
796 "set block size (%d) for %s cipher for decryption",
797 (int) sftp_cipher_get_read_block_size(), cipher->algo);
798
799 return 0;
800 }
801
802 int sftp_cipher_read_data(struct ssh2_packet *pkt, unsigned char *data,
803 uint32_t data_len, unsigned char **buf, uint32_t *buflen) {
804 int res;
805 struct sftp_cipher *cipher;
806 EVP_CIPHER_CTX *pctx;
807 size_t auth_len = 0, read_blocksz;
808 uint32_t output_buflen;
809 unsigned char *ptr = NULL, *buf2 = NULL;
810 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
811 !defined(HAVE_BROKEN_CHACHA20)
812 unsigned char chachapoly_key[POLY1305_KEYLEN];
813 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
814
815 cipher = &(read_ciphers[read_cipher_idx]);
816
817 if (cipher->key == NULL) {
818 /* We haven't finished NEWKEYS yet, so our cipher isn't keyed. */
819
820 *buf = data;
821 *buflen = data_len;
822 return 0;
823 }
824
825 pctx = read_ctxs[read_cipher_idx];
826 read_blocksz = read_blockszs[read_cipher_idx];
827 auth_len = sftp_cipher_get_read_auth_size();
828 output_buflen = *buflen;
829
830 if (*buf == NULL) {
831 size_t bufsz;
832
833 /* Allocate a buffer that's large enough. */
834 bufsz = (data_len + read_blocksz - 1);
835 ptr = buf2 = palloc(pkt->pool, bufsz);
836
837 } else {
838 ptr = buf2 = *buf;
839 }
840
841 if (pkt->packet_len == 0) {
842 if (auth_len > 0) {
843 if (cipher->algo_type == SFTP_CIPHER_ALGO_GCM) {
844 #if defined(EVP_CTRL_GCM_IV_GEN)
845 unsigned char prev_iv[1];
846
847 /* Increment the IV. */
848 if (EVP_CIPHER_CTX_ctrl(pctx, EVP_CTRL_GCM_IV_GEN, 1, prev_iv) != 1) {
849 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
850 "error incrementing %s IV data for client: %s", cipher->algo,
851 sftp_crypto_get_errors());
852 errno = EIO;
853 return -1;
854 }
855 #endif
856 }
857 }
858
859 if (pkt->aad_len > 0 &&
860 pkt->aad == NULL) {
861 pkt->aad = palloc(pkt->pool, pkt->aad_len);
862 memcpy(pkt->aad, data, pkt->aad_len);
863 memcpy(ptr, data, pkt->aad_len);
864
865 /* Save room at the start of the output buffer `ptr` for the AAD
866 * bytes.
867 */
868 buf2 += pkt->aad_len;
869 data += pkt->aad_len;
870 data_len -= pkt->aad_len;
871 output_buflen -= pkt->aad_len;
872
873 if (auth_len > 0) {
874 if (cipher->algo_type != SFTP_CIPHER_ALGO_CHACHA) {
875 if (EVP_Cipher(pctx, NULL, pkt->aad, pkt->aad_len) < 0) {
876 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
877 "error setting %s AAD data for client: %s", cipher->algo,
878 sftp_crypto_get_errors());
879 errno = EIO;
880 return -1;
881 }
882 }
883 }
884 }
885 }
886
887 if (output_buflen % read_blocksz != 0) {
888 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
889 "bad input length for decryption (%u bytes, %lu AAD bytes, "
890 "%u block size)", output_buflen, (unsigned long) pkt->aad_len,
891 (unsigned int) read_blocksz);
892 return -1;
893 }
894
895 if (pkt->packet_len > 0 &&
896 auth_len > 0) {
897 unsigned char *tag_data = NULL;
898 uint32_t tag_datalen = auth_len;
899
900 /* The authentication tag appears after the unencrypted AAD bytes, and
901 * the encrypted payload bytes.
902 */
903 tag_data = data + (data_len - auth_len);
904
905 if (cipher->algo_type == SFTP_CIPHER_ALGO_GCM) {
906 #if defined(EVP_CTRL_GCM_GET_TAG)
907 if (EVP_CIPHER_CTX_ctrl(pctx, EVP_CTRL_GCM_SET_TAG, tag_datalen,
908 tag_data) != 1) {
909 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
910 "error setting %s authentication tag for client: %s", cipher->algo,
911 sftp_crypto_get_errors());
912 errno = EIO;
913 return -1;
914 }
915 #endif
916 data_len -= auth_len;
917
918 } else if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
919 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
920 !defined(HAVE_BROKEN_CHACHA20)
921 unsigned char chachapoly_tag[POLY1305_TAGLEN];
922 pool *tag_pool;
923 unsigned char *tag_buf;
924 size_t tag_bufsz;
925
926 if (compute_chachapoly_key(pkt, pctx, chachapoly_key) < 0) {
927 return -1;
928 }
929
930 /* Here we want to compute our Poly1305 tag over the combination
931 * of the encrypted packet length (pkt->aad) and the encrypted
932 * payload (buf).
933 *
934 * Thus we need to assemble that here.
935 */
936
937 tag_pool = make_sub_pool(pkt->pool);
938 tag_bufsz = pkt->aad_len + data_len;
939 tag_buf = palloc(tag_pool, tag_bufsz);
940
941 memcpy(tag_buf, pkt->aad, pkt->aad_len);
942 memcpy(tag_buf + pkt->aad_len, data, data_len);
943
944 poly1305_auth(chachapoly_tag, tag_buf, tag_bufsz, chachapoly_key);
945 destroy_pool(tag_pool);
946
947 /* Our ChaChaPoly tag is stored as the MAC, NOT in the given network
948 * data (which is just the payload).
949 */
950 if (timingsafe_bcmp(chachapoly_tag, pkt->mac,
951 sizeof(chachapoly_tag)) != 0) {
952 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
953 "error verifying %s authentication tag from client: "
954 "Mismatched tags", cipher->algo);
955 errno = EIO;
956 return -1;
957 }
958 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
959 }
960 }
961
962 if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
963 unsigned char seqnobuf[16], *ptr;
964 uint32_t len;
965
966 memset(seqnobuf, 0, sizeof(seqnobuf));
967 seqnobuf[0] = 1;
968
969 ptr = seqnobuf + 8;
970 len = 8;
971 sftp_msg_write_long(&ptr, &len, pkt->seqno);
972
973 if (EVP_CipherInit(pctx, NULL, NULL, seqnobuf, 1) != 1) {
974 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
975 "error initializing %s cipher for encryption: %s", cipher->algo,
976 sftp_crypto_get_errors());
977 return -1;
978 }
979 }
980
981 res = EVP_Cipher(pctx, buf2, data, data_len);
982 if (res < 0) {
983 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
984 "error decrypting %s data from client: %s", cipher->algo,
985 sftp_crypto_get_errors());
986 return -1;
987 }
988
989 if (pkt->packet_len > 0) {
990 *buflen = data_len;
991
992 } else {
993 /* If we don't know the packet length yet, it means we need to allow for
994 * the processing of the AAD bytes.
995 */
996 *buflen = pkt->aad_len + data_len;
997 }
998
999 *buf = ptr;
1000
1001 if (pkt->packet_len > 0 &&
1002 auth_len > 0) {
1003 /* Verify the authentication tag, but only if we have the full packet. */
1004 if (cipher->algo_type != SFTP_CIPHER_ALGO_CHACHA) {
1005 if (EVP_Cipher(pctx, NULL, NULL, 0) < 0) {
1006 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1007 "error verifying %s authentication tag for client: %s",
1008 cipher->algo, sftp_crypto_get_errors());
1009 errno = EIO;
1010 return -1;
1011 }
1012 }
1013 }
1014
1015 return 0;
1016 }
1017
1018 int sftp_cipher_read_packet_len(struct ssh2_packet *pkt, unsigned char *data,
1019 uint32_t data_len, unsigned char **buf, uint32_t *buflen,
1020 uint32_t *packet_len) {
1021 int res;
1022 struct sftp_cipher *cipher;
1023 uint32_t pkt_len = 0;
1024
1025 cipher = &(read_ciphers[read_cipher_idx]);
1026
1027 if (cipher->key == NULL) {
1028 /* We haven't finished NEWKEYS setup yet, so packet length is in
1029 * plaintext.
1030 */
1031
1032 *buf = data;
1033 *buflen = data_len;
1034
1035 memmove(&pkt_len, *buf, sizeof(uint32_t));
1036 *packet_len = ntohl(pkt_len);
1037
1038 *buf += sizeof(uint32_t);
1039 *buflen -= sizeof(uint32_t);
1040
1041 return 0;
1042 }
1043
1044 if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
1045 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1046 !defined(HAVE_BROKEN_CHACHA20)
1047 unsigned char seqnobuf[16], *ptr;
1048 uint32_t len;
1049 EVP_CIPHER_CTX *hpctx;
1050
1051 hpctx = read_header_ctxs[read_cipher_idx];
1052
1053 /* Initialize our IV for the ChaChaPoly header. Note that the packet
1054 * sequence number must be encoded according to the SSH spec.
1055 */
1056 memset(seqnobuf, 0, sizeof(seqnobuf));
1057 ptr = seqnobuf + 8;
1058 len = 8;
1059 sftp_msg_write_long(&ptr, &len, pkt->seqno);
1060
1061 if (EVP_CipherInit(hpctx, NULL, NULL, seqnobuf, 0) != 1) {
1062 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1063 "error initializing %s cipher for packet length decryption: %s",
1064 cipher->algo, sftp_crypto_get_errors());
1065 return -1;
1066 }
1067
1068 if (EVP_Cipher(hpctx, (unsigned char *) &pkt_len, data,
1069 sizeof(pkt_len)) < 0) {
1070 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1071 "error decrypting %s packet length from client: %s", cipher->algo,
1072 sftp_crypto_get_errors());
1073 return -1;
1074 }
1075
1076 /* We need to save these encrypted header bytes for later. */
1077 pkt->aad = palloc(pkt->pool, pkt->aad_len);
1078 memcpy(pkt->aad, data, pkt->aad_len);
1079
1080 *packet_len = ntohl(pkt_len);
1081
1082 /* No leftover network bytes for later processing; we used them all. */
1083 *buf = NULL;
1084 *buflen = 0;
1085
1086 return 0;
1087 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1088 }
1089
1090 res = sftp_cipher_read_data(pkt, data, data_len, buf, buflen);
1091 if (res < 0) {
1092 return -1;
1093 }
1094
1095 memmove(&pkt_len, *buf, sizeof(uint32_t));
1096 *packet_len = ntohl(pkt_len);
1097
1098 *buf += sizeof(uint32_t);
1099 *buflen -= sizeof(uint32_t);
1100 return 0;
1101 }
1102
1103 const char *sftp_cipher_get_write_algo(void) {
1104 if (write_ciphers[write_cipher_idx].key != NULL ||
1105 write_ciphers[write_cipher_idx].algo_type == SFTP_CIPHER_ALGO_NONE) {
1106 return write_ciphers[write_cipher_idx].algo;
1107 }
1108
1109 return NULL;
1110 }
1111
1112 int sftp_cipher_set_write_algo(const char *algo) {
1113 unsigned int idx = write_cipher_idx;
1114 size_t key_len = 0, auth_len = 0, discard_len = 0;
1115
1116 if (write_ciphers[idx].key != NULL) {
1117 /* If we have an existing key, it means that we are currently rekeying. */
1118 idx = get_next_write_index();
1119 }
1120
1121 write_ciphers[idx].cipher = sftp_crypto_get_cipher(algo, &key_len, &auth_len,
1122 &discard_len);
1123 if (write_ciphers[idx].cipher == NULL) {
1124 return -1;
1125 }
1126
1127 if (key_len > 0) {
1128 pr_trace_msg(trace_channel, 19,
1129 "setting write key for cipher %s: key len = %lu", algo,
1130 (unsigned long) key_len);
1131 }
1132
1133 if (auth_len > 0) {
1134 pr_trace_msg(trace_channel, 19,
1135 "setting write key for cipher %s: auth len = %lu", algo,
1136 (unsigned long) auth_len);
1137 }
1138
1139 if (discard_len > 0) {
1140 pr_trace_msg(trace_channel, 19,
1141 "setting write key for cipher %s: discard len = %lu", algo,
1142 (unsigned long) discard_len);
1143 }
1144
1145 /* Note that we use a new pool, each time the algorithm is set (which
1146 * happens during key exchange) to prevent undue memory growth for
1147 * long-lived sessions with many rekeys.
1148 */
1149 if (write_ciphers[idx].pool != NULL) {
1150 destroy_pool(write_ciphers[idx].pool);
1151 }
1152
1153 write_ciphers[idx].pool = make_sub_pool(sftp_pool);
1154 pr_pool_tag(write_ciphers[idx].pool, "SFTP cipher write pool");
1155 write_ciphers[idx].algo = pstrdup(write_ciphers[idx].pool, algo);
1156 write_ciphers[idx].algo_type = get_algo_type(algo);
1157
1158 write_ciphers[idx].key_len = (uint32_t) key_len;
1159 write_ciphers[idx].auth_len = (uint32_t) auth_len;
1160 write_ciphers[idx].discard_len = discard_len;
1161
1162 return 0;
1163 }
1164
1165 int sftp_cipher_set_write_key(pool *p, const EVP_MD *hash,
1166 const unsigned char *k, uint32_t klen, const char *h, uint32_t hlen,
1167 int role) {
1168 const unsigned char *id = NULL;
1169 char letter;
1170 uint32_t id_len;
1171 int key_len, auth_len;
1172 struct sftp_cipher *cipher;
1173 EVP_CIPHER_CTX *pctx, *hpctx = NULL;
1174
1175 switch_write_cipher();
1176
1177 cipher = &(write_ciphers[write_cipher_idx]);
1178 pctx = write_ctxs[write_cipher_idx];
1179 if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
1180 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1181 !defined(HAVE_BROKEN_CHACHA20)
1182 hpctx = write_header_ctxs[write_cipher_idx];
1183 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1184 }
1185
1186 id_len = sftp_session_get_id(&id);
1187
1188 /* The letters used depend on the role; see:
1189 * https://tools.ietf.org/html/rfc4253#section-7.2
1190 *
1191 * If we are the SERVER, then we use the letters for the "server to client"
1192 * flows, since we are WRITING to the client.
1193 */
1194
1195 /* client-to-server IV: HASH(K || H || "A" || session_id)
1196 * server-to-client IV: HASH(K || H || "B" || session_id)
1197 */
1198 letter = (role == SFTP_ROLE_SERVER ? 'B' : 'A');
1199 if (set_cipher_iv(cipher, hash, k, klen, h, hlen, &letter, id, id_len) < 0) {
1200 return -1;
1201 }
1202
1203 /* client-to-server key: HASH(K || H || "C" || session_id)
1204 * server-to-client key: HASH(K || H || "D" || session_id)
1205 */
1206 letter = (role == SFTP_ROLE_SERVER ? 'D' : 'C');
1207 if (set_cipher_key(cipher, hash, k, klen, h, hlen, letter, id, id_len) < 0) {
1208 return -1;
1209 }
1210
1211 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1212 defined(HAVE_LIBRESSL)
1213 EVP_CIPHER_CTX_init(pctx);
1214 if (hpctx != NULL) {
1215 EVP_CIPHER_CTX_init(hpctx);
1216 }
1217 #else
1218 EVP_CIPHER_CTX_reset(pctx);
1219 if (hpctx != NULL) {
1220 EVP_CIPHER_CTX_reset(hpctx);
1221 }
1222 #endif
1223
1224 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
1225 if (EVP_CipherInit_ex(pctx, cipher->cipher, NULL, NULL,
1226 cipher->iv, 1) != 1) {
1227 #else
1228 if (EVP_CipherInit(pctx, cipher->cipher, NULL, cipher->iv, 1) != 1) {
1229 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
1230 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1231 "error initializing %s cipher for encryption: %s", cipher->algo,
1232 sftp_crypto_get_errors());
1233 return -1;
1234 }
1235
1236 if (hpctx != NULL) {
1237 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
1238 if (EVP_CipherInit_ex(hpctx, cipher->cipher, NULL, NULL, NULL, 1) != 1) {
1239 #else
1240 if (EVP_CipherInit(hpctx, cipher->cipher, NULL, NULL, 1) != 1) {
1241 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
1242 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1243 "error initializing %s cipher for header encryption: %s", cipher->algo,
1244 sftp_crypto_get_errors());
1245 return -1;
1246 }
1247 }
1248
1249 auth_len = (int) cipher->auth_len;
1250 if (auth_len > 0) {
1251 if (cipher->algo_type == SFTP_CIPHER_ALGO_GCM) {
1252 #if defined(EVP_CTRL_GCM_SET_IV_FIXED)
1253 if (EVP_CIPHER_CTX_ctrl(pctx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
1254 cipher->iv) != 1) {
1255 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1256 "error configuring %s cipher for encryption: %s", cipher->algo,
1257 sftp_crypto_get_errors());
1258 return -1;
1259 }
1260 #endif /* EVP_CTRL_GCM_SET_IV_FIXED */
1261
1262 pr_trace_msg(trace_channel, 19,
1263 "set auth length (%d) for %s cipher for encryption", auth_len,
1264 cipher->algo);
1265 }
1266 }
1267
1268 /* Next, set the key length. */
1269 key_len = (int) cipher->key_len;
1270 if (key_len > 0 &&
1271 cipher->algo_type != SFTP_CIPHER_ALGO_CHACHA) {
1272
1273 /* Skip setting our custom key length for ChaCha20, since the custom
1274 * key length is used for two different ChaCha20 cipher instances.
1275 */
1276
1277 if (EVP_CIPHER_CTX_set_key_length(pctx, key_len) != 1) {
1278 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1279 "error setting key length (%d bytes) for %s cipher for decryption: %s",
1280 key_len, cipher->algo, sftp_crypto_get_errors());
1281 return -1;
1282 }
1283
1284 pr_trace_msg(trace_channel, 19,
1285 "set key length (%d) for %s cipher for encryption", key_len,
1286 cipher->algo);
1287 }
1288
1289 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
1290 if (EVP_CipherInit_ex(pctx, NULL, NULL, cipher->key, NULL, -1) != 1) {
1291 #else
1292 if (EVP_CipherInit(pctx, NULL, cipher->key, NULL, -1) != 1) {
1293 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
1294 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1295 "error re-initializing %s cipher for encryption: %s", cipher->algo,
1296 sftp_crypto_get_errors());
1297 return -1;
1298 }
1299
1300 if (hpctx != NULL) {
1301 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
1302 if (EVP_CipherInit_ex(hpctx, NULL, NULL, cipher->key + 32, NULL, -1) != 1) {
1303 #else
1304 if (EVP_CipherInit(hpctx, NULL, cipher->key + 32, NULL, -1) != 1) {
1305 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
1306 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1307 "error re-initializing %s cipher for header encryption: %s",
1308 cipher->algo, sftp_crypto_get_errors());
1309 return -1;
1310 }
1311 }
1312
1313 if (set_cipher_discarded(cipher, pctx) < 0) {
1314 return -1;
1315 }
1316
1317 if (strcmp(cipher->algo, "aes128-ctr") == 0 ||
1318 strcmp(cipher->algo, "[email protected]") == 0 ||
1319 strcmp(cipher->algo, "aes192-ctr") == 0 ||
1320 strcmp(cipher->algo, "aes256-ctr") == 0 ||
1321 strcmp(cipher->algo, "[email protected]") == 0) {
1322 /* For some reason, OpenSSL returns 8 for the AES CTR/GCM block size (even
1323 * though the AES block size is 16, per RFC 5647), but OpenSSH wants 16.
1324 */
1325 sftp_cipher_set_write_block_size(16);
1326
1327 } else {
1328 sftp_cipher_set_write_block_size(EVP_CIPHER_block_size(cipher->cipher));
1329 }
1330
1331 pr_trace_msg(trace_channel, 19,
1332 "set block size (%d) for %s cipher for encryption",
1333 (int) sftp_cipher_get_write_block_size(), cipher->algo);
1334
1335 return 0;
1336 }
1337
1338 int sftp_cipher_write_data(struct ssh2_packet *pkt, unsigned char *buf,
1339 size_t *buflen) {
1340 int res;
1341 struct sftp_cipher *cipher;
1342 EVP_CIPHER_CTX *pctx;
1343 size_t auth_len = 0;
1344 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1345 !defined(HAVE_BROKEN_CHACHA20)
1346 unsigned char chachapoly_key[POLY1305_KEYLEN];
1347 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1348 unsigned char *data, *ptr;
1349 uint32_t datalen, datasz;
1350
1351 cipher = &(write_ciphers[write_cipher_idx]);
1352 pctx = write_ctxs[write_cipher_idx];
1353 auth_len = sftp_cipher_get_write_auth_size();
1354
1355 if (cipher->key == NULL) {
1356 *buflen = 0;
1357 return 0;
1358 }
1359
1360 /* Always leave a little extra room in the buffer. */
1361 datasz = sizeof(uint32_t) + pkt->packet_len + 64;
1362
1363 if (pkt->aad_len > 0) {
1364 /* Packet length is not encrypted for authentication encryption, or
1365 * Encrypt-Then-MAC modes. However, it IS encrypted for ChaChaPoly.
1366 */
1367 if (cipher->algo_type != SFTP_CIPHER_ALGO_CHACHA) {
1368 datasz -= pkt->aad_len;
1369 }
1370
1371 /* And, for ETM modes, we may need a little more space. */
1372 datasz += sftp_cipher_get_write_block_size();
1373 }
1374
1375 datalen = datasz;
1376 ptr = data = palloc(pkt->pool, datasz);
1377
1378 if (auth_len > 0) {
1379 if (cipher->algo_type == SFTP_CIPHER_ALGO_GCM) {
1380 #if defined(EVP_CTRL_GCM_IV_GEN)
1381 unsigned char prev_iv[1];
1382
1383 /* Increment the IV. */
1384 if (EVP_CIPHER_CTX_ctrl(pctx, EVP_CTRL_GCM_IV_GEN, 1, prev_iv) != 1) {
1385 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1386 "error incrementing %s IV data for client: %s", cipher->algo,
1387 sftp_crypto_get_errors());
1388 errno = EIO;
1389 return -1;
1390 }
1391 #endif
1392 } else if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
1393 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1394 !defined(HAVE_BROKEN_CHACHA20)
1395 if (compute_chachapoly_key(pkt, pctx, chachapoly_key) < 0) {
1396 return -1;
1397 }
1398 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1399 }
1400 }
1401
1402 if (pkt->aad_len > 0 &&
1403 pkt->aad == NULL) {
1404 uint32_t packet_len;
1405
1406 packet_len = htonl(pkt->packet_len);
1407 pkt->aad = palloc(pkt->pool, pkt->aad_len);
1408 memcpy(pkt->aad, &packet_len, pkt->aad_len);
1409
1410 if (auth_len > 0) {
1411 if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
1412 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1413 !defined(HAVE_BROKEN_CHACHA20)
1414 unsigned char seqnobuf[16], *ptr;
1415 uint32_t len;
1416 EVP_CIPHER_CTX *hpctx;
1417
1418 hpctx = write_header_ctxs[write_cipher_idx];
1419
1420 memset(seqnobuf, 0, sizeof(seqnobuf));
1421 ptr = seqnobuf + 8;
1422 len = 8;
1423 sftp_msg_write_long(&ptr, &len, pkt->seqno);
1424
1425 if (EVP_CipherInit(hpctx, NULL, NULL, seqnobuf, 1) != 1) {
1426 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1427 "error initializing %s cipher for packet length encryption: %s",
1428 cipher->algo, sftp_crypto_get_errors());
1429 return -1;
1430 }
1431
1432 if (EVP_Cipher(hpctx, pkt->aad, (unsigned char *) &packet_len,
1433 pkt->aad_len) < 0) {
1434 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1435 "error encrypting %s packet length for client: %s", cipher->algo,
1436 sftp_crypto_get_errors());
1437 return -1;
1438 }
1439 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1440
1441 } else {
1442 if (EVP_Cipher(pctx, NULL, pkt->aad, pkt->aad_len) < 0) {
1443 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1444 "error setting %s AAD (%lu bytes) for client: %s", cipher->algo,
1445 (unsigned long) pkt->aad_len, sftp_crypto_get_errors());
1446 errno = EIO;
1447 return -1;
1448 }
1449 }
1450 }
1451
1452 } else {
1453 sftp_msg_write_int(&data, &datalen, pkt->packet_len);
1454 }
1455
1456 sftp_msg_write_byte(&data, &datalen, pkt->padding_len);
1457 sftp_msg_write_data(&data, &datalen, pkt->payload, pkt->payload_len, FALSE);
1458 sftp_msg_write_data(&data, &datalen, pkt->padding, pkt->padding_len, FALSE);
1459
1460 if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
1461 unsigned char seqnobuf[16], *ptr;
1462 uint32_t len;
1463
1464 memset(seqnobuf, 0, sizeof(seqnobuf));
1465 seqnobuf[0] = 1;
1466
1467 ptr = seqnobuf + 8;
1468 len = 8;
1469 sftp_msg_write_long(&ptr, &len, pkt->seqno);
1470
1471 if (EVP_CipherInit(pctx, NULL, NULL, seqnobuf, 1) != 1) {
1472 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1473 "error initializing %s cipher for encryption: %s", cipher->algo,
1474 sftp_crypto_get_errors());
1475 return -1;
1476 }
1477 }
1478
1479 res = EVP_Cipher(pctx, buf, ptr, (datasz - datalen));
1480 if (res < 0) {
1481 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1482 "error encrypting %s data for client: %s", cipher->algo,
1483 sftp_crypto_get_errors());
1484 errno = EIO;
1485 return -1;
1486 }
1487
1488 *buflen = (datasz - datalen);
1489
1490 #ifdef SFTP_DEBUG_PACKET
1491 {
1492 unsigned int i;
1493
1494 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1495 "encrypted packet data (len %lu):", (unsigned long) *buflen);
1496 for (i = 0; i < *buflen;) {
1497 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1498 " %02x%02x %02x%02x %02x%02x %02x%02x",
1499 ((unsigned char *) buf)[i], ((unsigned char *) buf)[i+1],
1500 ((unsigned char *) buf)[i+2], ((unsigned char *) buf)[i+3],
1501 ((unsigned char *) buf)[i+4], ((unsigned char *) buf)[i+5],
1502 ((unsigned char *) buf)[i+6], ((unsigned char *) buf)[i+7]);
1503 i += 8;
1504 }
1505 }
1506 #endif
1507
1508 if (auth_len > 0) {
1509 unsigned char *tag_data = NULL;
1510 uint32_t tag_datalen = 0;
1511
1512 if (cipher->algo_type != SFTP_CIPHER_ALGO_CHACHA) {
1513 if (EVP_Cipher(pctx, NULL, NULL, 0) < 0) {
1514 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1515 "error generating %s authentication tag for client: %s",
1516 cipher->algo, sftp_crypto_get_errors());
1517 errno = EIO;
1518 return -1;
1519 }
1520 }
1521
1522 tag_datalen = auth_len;
1523 tag_data = palloc(pkt->pool, tag_datalen);
1524
1525 if (cipher->algo_type == SFTP_CIPHER_ALGO_GCM) {
1526 #if defined(EVP_CTRL_GCM_GET_TAG)
1527 if (EVP_CIPHER_CTX_ctrl(pctx, EVP_CTRL_GCM_GET_TAG, tag_datalen,
1528 tag_data) != 1) {
1529 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1530 "error getting %s authentication tag for client: %s", cipher->algo,
1531 sftp_crypto_get_errors());
1532 errno = EIO;
1533 return -1;
1534 }
1535 #endif
1536 } else if (cipher->algo_type == SFTP_CIPHER_ALGO_CHACHA) {
1537 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1538 !defined(HAVE_BROKEN_CHACHA20)
1539 pool *tag_pool;
1540 unsigned char *tag_buf;
1541 size_t tag_bufsz;
1542
1543 /* Here we want to compute our Poly1305 tag over the combination
1544 * of the encrypted packet length (pkt->aad) and the encrypted
1545 * payload (buf).
1546 *
1547 * Thus we need to assemble that here.
1548 */
1549
1550 tag_pool = make_sub_pool(pkt->pool);
1551 tag_bufsz = pkt->aad_len + *buflen;
1552 tag_buf = palloc(tag_pool, tag_bufsz);
1553
1554 memcpy(tag_buf, pkt->aad, pkt->aad_len);
1555 memcpy(tag_buf + pkt->aad_len, buf, *buflen);
1556
1557 poly1305_auth(tag_data, tag_buf, tag_bufsz, chachapoly_key);
1558 destroy_pool(tag_pool);
1559 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1560 }
1561
1562 pkt->mac_len = tag_datalen;
1563 pkt->mac = tag_data;
1564 }
1565
1566 return 0;
1567 }
1568
1569 #if OPENSSL_VERSION_NUMBER < 0x1000000fL
1570 /* In older versions of OpenSSL, there was not a way to dynamically allocate
1571 * an EVP_CIPHER_CTX object. Thus we have these static objects for those
1572 * older versions.
1573 */
1574 static EVP_CIPHER_CTX read_ctx1, read_ctx2;
1575 static EVP_CIPHER_CTX write_ctx1, write_ctx2;
1576 #endif /* prior to OpenSSL-1.0.0 */
1577
1578 int sftp_cipher_init(void) {
1579 #if OPENSSL_VERSION_NUMBER < 0x1000000fL
1580 read_ctxs[0] = &read_ctx1;
1581 read_ctxs[1] = &read_ctx2;
1582 write_ctxs[0] = &write_ctx1;
1583 write_ctxs[1] = &write_ctx2;
1584 #else
1585 read_ctxs[0] = EVP_CIPHER_CTX_new();
1586 read_ctxs[1] = EVP_CIPHER_CTX_new();
1587 write_ctxs[0] = EVP_CIPHER_CTX_new();
1588 write_ctxs[1] = EVP_CIPHER_CTX_new();
1589 # if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1590 !defined(HAVE_BROKEN_CHACHA20)
1591 read_header_ctxs[0] = EVP_CIPHER_CTX_new();
1592 read_header_ctxs[1] = EVP_CIPHER_CTX_new();
1593 write_header_ctxs[0] = EVP_CIPHER_CTX_new();
1594 write_header_ctxs[1] = EVP_CIPHER_CTX_new();
1595 # endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1596 #endif /* OpenSSL-1.0.0 and later */
1597 return 0;
1598 }
1599
1600 int sftp_cipher_free(void) {
1601 #if OPENSSL_VERSION_NUMBER >= 0x1000000fL
1602 if (read_ctxs[0] != NULL) {
1603 EVP_CIPHER_CTX_free(read_ctxs[0]);
1604 read_ctxs[0] = NULL;
1605 }
1606
1607 if (read_ctxs[1] != NULL) {
1608 EVP_CIPHER_CTX_free(read_ctxs[1]);
1609 read_ctxs[1] = NULL;
1610 }
1611
1612 if (write_ctxs[0] != NULL) {
1613 EVP_CIPHER_CTX_free(write_ctxs[0]);
1614 write_ctxs[0] = NULL;
1615 }
1616
1617 if (write_ctxs[1] != NULL) {
1618 EVP_CIPHER_CTX_free(write_ctxs[1]);
1619 write_ctxs[1] = NULL;
1620 }
1621
1622 # if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
1623 !defined(HAVE_BROKEN_CHACHA20)
1624 if (read_header_ctxs[0] != NULL) {
1625 EVP_CIPHER_CTX_free(read_header_ctxs[0]);
1626 read_header_ctxs[0] = NULL;
1627 }
1628
1629 if (read_header_ctxs[1] != NULL) {
1630 EVP_CIPHER_CTX_free(read_header_ctxs[1]);
1631 read_header_ctxs[1] = NULL;
1632 }
1633
1634 if (write_header_ctxs[0] != NULL) {
1635 EVP_CIPHER_CTX_free(write_header_ctxs[0]);
1636 write_header_ctxs[0] = NULL;
1637 }
1638
1639 if (write_header_ctxs[1] != NULL) {
1640 EVP_CIPHER_CTX_free(write_header_ctxs[1]);
1641 write_header_ctxs[1] = NULL;
1642 }
1643 # endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
1644 #endif /* OpenSSL-1.0.0 and later */
1645 return 0;
1646 }