Debian Codegraph @

contrib/mod_sftp/crypto.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp OpenSSL interface
3 * Copyright (c) 2008-2023 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 "crypto.h"
27 #include "provider.h"
28 #include "umac.h"
29
30 /* In OpenSSL 0.9.7, all des_ functions were renamed to DES_ to avoid
31 * clashes with older versions of libdes.
32 */
33 #if OPENSSL_VERSION_NUMBER < 0x000907000L
34 # define DES_key_schedule des_key_schedule
35 # define DES_cblock des_cblock
36 # define DES_encrypt3 des_encrypt3
37 # define DES_set_key_unchecked des_set_key_unchecked
38 #endif
39
40 #if OPENSSL_VERSION_NUMBER > 0x000907000L && \
41 defined(PR_USE_OPENSSL_ENGINE)
42 static const char *crypto_engine = NULL;
43 #endif /* PR_USE_OPENSSL_ENGINE */
44
45 struct sftp_cipher {
46 const char *name;
47 const char *openssl_name;
48
49 /* Used mostly for AEAD algorithms like GCM. */
50 size_t auth_len;
51
52 /* Used mostly for the RC4/ArcFour algorithms, for mitigating attacks
53 * based on the first N bytes of the keystream.
54 */
55 size_t discard_len;
56
57 #if OPENSSL_VERSION_NUMBER > 0x000907000L
58 const EVP_CIPHER *(*get_type)(void);
59 #else
60 EVP_CIPHER *(*get_type)(void);
61 #endif
62
63 /* Is this cipher enabled by default? If FALSE, then this cipher must
64 * be explicitly requested via SFTPCiphers.
65 */
66 int enabled;
67
68 /* Is this cipher usable when FIPS is enabled? If FALSE, then this
69 * cipher must NOT be advertised to clients in the KEXINIT.
70 */
71 int fips_allowed;
72 };
73
74 /* Currently, OpenSSL does NOT support AES CTR modes (not sure why).
75 * Until then, we have to provide our own CTR code, for some of the ciphers
76 * recommended by RFC4344.
77 *
78 * And according to:
79 *
80 * http://www.cpni.gov.uk/Docs/Vulnerability_Advisory_SSH.txt
81 *
82 * it is highly recommended to use CTR mode ciphers, rather than CBC mode,
83 * in order to avoid leaking plaintext.
84 */
85
86 static struct sftp_cipher ciphers[] = {
87 /* The handling of NULL openssl_name and get_type fields is done in
88 * sftp_crypto_get_cipher(), as special cases.
89 */
90
91 #if defined(HAVE_EVP_CHACHA20_OPENSSL) && \
92 !defined(HAVE_BROKEN_CHACHA20)
93 { "[email protected]", "chacha20", 16, 0, EVP_chacha20, TRUE, TRUE },
94 #endif /* HAVE_EVP_CHACHA20_OPENSSL and !HAVE_BROKEN_CHACHA20 */
95
96 #if OPENSSL_VERSION_NUMBER > 0x000907000L
97 { "aes256-ctr", NULL, 0, 0, NULL, TRUE, TRUE },
98 { "aes192-ctr", NULL, 0, 0, NULL, TRUE, TRUE },
99 { "aes128-ctr", NULL, 0, 0, NULL, TRUE, TRUE },
100
101 # if defined(HAVE_EVP_AES_256_GCM_OPENSSL)
102 { "[email protected]", "aes-256-gcm", 16, 0, EVP_aes_256_gcm, TRUE, TRUE },
103 { "[email protected]", "aes-128-gcm", 16, 0, EVP_aes_128_gcm, TRUE, TRUE },
104 # endif
105
106 # if !defined(HAVE_AES_CRIPPLED_OPENSSL)
107 { "aes256-cbc", "aes-256-cbc", 0, 0, EVP_aes_256_cbc, TRUE, TRUE },
108 { "aes192-cbc", "aes-192-cbc", 0, 0, EVP_aes_192_cbc, TRUE, TRUE },
109 # endif /* !HAVE_AES_CRIPPLED_OPENSSL */
110
111 { "aes128-cbc", "aes-128-cbc", 0, 0, EVP_aes_128_cbc, TRUE, TRUE },
112 #endif
113
114 #if !defined(OPENSSL_NO_BF) && \
115 (!defined(HAVE_LIBRESSL) || \
116 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L))
117 # if OPENSSL_VERSION_NUMBER < 0x30000000L
118 { "blowfish-ctr", NULL, 0, 0, NULL, FALSE, FALSE },
119 # endif /* Prior to OpenSSL 3.x/LibreSSL-3.5.0 */
120 { "blowfish-cbc", "bf-cbc", 0, 0, EVP_bf_cbc, FALSE, FALSE },
121 #endif /* !OPENSSL_NO_BF */
122
123 #if !defined(OPENSSL_NO_CAST)
124 { "cast128-cbc", "cast5-cbc", 0, 0, EVP_cast5_cbc, TRUE, FALSE },
125 #endif /* !OPENSSL_NO_CAST */
126
127 #if !defined(OPENSSL_NO_RC4)
128 { "arcfour256", "rc4", 0, 1536, EVP_rc4, FALSE, FALSE },
129 { "arcfour128", "rc4", 0, 1536, EVP_rc4, FALSE, FALSE },
130 #endif /* !OPENSSL_NO_RC4 */
131
132 #if 0
133 /* This cipher is explicitly NOT supported because it does not discard
134 * the first N bytes of the keystream, unlike the other RC4 ciphers.
135 *
136 * If there is a hue and cry, I might add this to the code BUT it would
137 * require explicit configuration via SFTPCiphers, and would generate
138 * warnings about its unsafe use.
139 *
140 * Even then, RFC 8758 explicitly deprecates/removes RC4 (all variants)
141 * from SSH.
142 */
143 { "arcfour", "rc4", 0, 0, EVP_rc4, FALSE, FALSE },
144 #endif
145
146 #if !defined(OPENSSL_NO_DES) && \
147 (!defined(HAVE_LIBRESSL) || \
148 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L))
149 # if OPENSSL_VERSION_NUMBER < 0x30000000L
150 { "3des-ctr", NULL, 0, 0, NULL, TRUE, TRUE },
151 # endif /* Prior to OpenSSL 3.x/LibreSSL-3.5.0 */
152 { "3des-cbc", "des-ede3-cbc", 0, 0, EVP_des_ede3_cbc, TRUE, TRUE },
153 #endif /* !OPENSSL_NO_DES */
154
155 { "none", "null", 0, 0, EVP_enc_null, FALSE, TRUE },
156 { NULL, NULL, 0, 0, NULL, FALSE, FALSE }
157 };
158
159 struct sftp_digest {
160 const char *name;
161 const char *openssl_name;
162
163 #if OPENSSL_VERSION_NUMBER > 0x000907000L
164 const EVP_MD *(*get_type)(void);
165 #else
166 EVP_MD *(*get_type)(void);
167 #endif
168
169 uint32_t mac_len;
170
171 /* Is this MAC enabled by default? If FALSE, then this MAC must be
172 * explicitly requested via SFTPDigests.
173 */
174 int enabled;
175
176 /* Is this MAC usable when FIPS is enabled? If FALSE, then this digest must
177 * NOT be advertised to clients in the KEXINIT.
178 */
179 int fips_allowed;
180 };
181
182 static struct sftp_digest digests[] = {
183 /* The handling of NULL openssl_name and get_type fields is done in
184 * sftp_crypto_get_digest(), as special cases.
185 */
186 #ifdef HAVE_SHA256_OPENSSL
187 { "hmac-sha2-256", "sha256", EVP_sha256, 0, TRUE, TRUE },
188 { "[email protected]", "sha256", EVP_sha256, 0, TRUE, TRUE },
189 #endif /* SHA256 support in OpenSSL */
190 #ifdef HAVE_SHA512_OPENSSL
191 { "hmac-sha2-512", "sha512", EVP_sha512, 0, TRUE, TRUE },
192 { "[email protected]", "sha512", EVP_sha512, 0, TRUE, TRUE },
193 #endif /* SHA512 support in OpenSSL */
194 { "hmac-sha1", "sha1", EVP_sha1, 0, TRUE, TRUE },
195 { "[email protected]", "sha1",EVP_sha1, 0, TRUE, TRUE },
196 { "hmac-sha1-96", "sha1", EVP_sha1, 12, TRUE, TRUE },
197 { "[email protected]", "sha1", EVP_sha1, 12, TRUE, TRUE },
198 { "hmac-md5", "md5", EVP_md5, 0, FALSE, FALSE },
199 { "[email protected]", "md5", EVP_md5, 0, FALSE, TRUE },
200 { "hmac-md5-96", "md5", EVP_md5, 12, FALSE, FALSE },
201 { "[email protected]", "md5", EVP_md5, 12, FALSE, TRUE },
202 #if !defined(OPENSSL_NO_RIPEMD)
203 { "hmac-ripemd160", "rmd160", EVP_ripemd160, 0, FALSE, FALSE },
204 #endif /* !OPENSSL_NO_RIPEMD */
205 #if OPENSSL_VERSION_NUMBER > 0x000907000L
206 { "[email protected]", NULL, NULL, 8, TRUE, FALSE },
207 { "[email protected]", NULL, NULL, 8, TRUE, FALSE },
208 { "[email protected]", NULL, NULL, 16, TRUE, FALSE },
209 { "[email protected]", NULL, NULL, 16, TRUE, FALSE },
210 #endif /* OpenSSL-0.9.7 or later */
211 { "none", "null", EVP_md_null, 0, FALSE, TRUE },
212 { NULL, NULL, NULL, 0, FALSE, FALSE }
213 };
214
215 static const char *hostkey_algos[] = {
216 #if defined(PR_USE_SODIUM)
217 "ssh-ed25519",
218 #endif /* PR_USE_SODIUM */
219 #if defined(HAVE_X448_OPENSSL)
220 "ssh-ed448",
221 #endif /* HAVE_X448_OPENSSL */
222 #if defined(PR_USE_OPENSSL_ECC)
223 "ecdsa-sha2-nistp256",
224 "ecdsa-sha2-nistp384",
225 "ecdsa-sha2-nistp521",
226 #endif /* PR_USE_OPENSSL_ECC */
227 #if defined(HAVE_SHA512_OPENSSL)
228 "rsa-sha2-512",
229 #endif /* HAVE_SHA512_OPENSSL */
230 #if defined(HAVE_SHA256_OPENSSL)
231 "rsa-sha2-256",
232 #endif /* HAVE_SHA256_OPENSSL */
233 "ssh-rsa",
234 "ssh-dss",
235 NULL
236 };
237
238 static const char *key_exchanges[] = {
239 "diffie-hellman-group1-sha1",
240 "diffie-hellman-group14-sha1",
241 #if (OPENSSL_VERSION_NUMBER > 0x000907000L && defined(OPENSSL_FIPS)) || \
242 (OPENSSL_VERSION_NUMBER > 0x000908000L)
243 "diffie-hellman-group14-sha256",
244 "diffie-hellman-group16-sha512",
245 "diffie-hellman-group18-sha512",
246 "diffie-hellman-group-exchange-sha256",
247 #endif
248 "diffie-hellman-group-exchange-sha1",
249 #if defined(PR_USE_OPENSSL_ECC)
250 "ecdh-sha2-nistp256",
251 "ecdh-sha2-nistp384",
252 "ecdh-sha2-nistp521",
253 #endif /* PR_USE_OPENSSL_ECC */
254 #if defined(HAVE_SODIUM_H) && defined(HAVE_SHA256_OPENSSL)
255 "curve25519-sha256",
256 "[email protected]",
257 #endif /* HAVE_SODIUM_H and HAVE_SHA256_OPENSSL */
258 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
259 "curve448-sha512",
260 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
261 "rsa1024-sha1",
262 NULL
263 };
264
265 static const char *trace_channel = "ssh2";
266
267 #if OPENSSL_VERSION_NUMBER < 0x30000000L && \
268 (!defined(HAVE_LIBRESSL) || \
269 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L))
270 static void ctr_incr(unsigned char *ctr, size_t len) {
271 register int i;
272
273 if (len == 0) {
274 return;
275 }
276
277 for (i = len - 1; i >= 0; i--) {
278 /* If we haven't overflowed, we're done. */
279 if (++ctr[i]) {
280 return;
281 }
282 }
283 }
284 #endif /* Prior to OpenSSL 3.x */
285
286 #if !defined(OPENSSL_NO_BF) && \
287 (!defined(HAVE_LIBRESSL) || \
288 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)) && \
289 OPENSSL_VERSION_NUMBER < 0x30000000L
290 /* Blowfish CTR mode implementation */
291
292 struct bf_ctr_ex {
293 BF_KEY key;
294 unsigned char counter[BF_BLOCK];
295 };
296
297 static int init_bf_ctr(EVP_CIPHER_CTX *ctx, const unsigned char *key,
298 const unsigned char *iv, int enc) {
299 struct bf_ctr_ex *bce;
300
301 bce = EVP_CIPHER_CTX_get_app_data(ctx);
302 if (bce == NULL) {
303
304 /* Allocate our data structure. */
305 bce = calloc(1, sizeof(struct bf_ctr_ex));
306 if (bce == NULL) {
307 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
308 _exit(1);
309 }
310
311 EVP_CIPHER_CTX_set_app_data(ctx, bce);
312 }
313
314 if (key != NULL) {
315 int key_len;
316
317 # if OPENSSL_VERSION_NUMBER == 0x0090805fL
318 /* OpenSSL 0.9.8e had a bug where EVP_CIPHER_CTX_key_length() returned
319 * the cipher key length rather than the context key length.
320 */
321 key_len = ctx->key_len;
322 # else
323 key_len = EVP_CIPHER_CTX_key_length(ctx);
324 # endif
325
326 BF_set_key(&(bce->key), key_len, key);
327 }
328
329 if (iv != NULL) {
330 memcpy(bce->counter, iv, BF_BLOCK);
331 }
332
333 return 1;
334 }
335
336 static int cleanup_bf_ctr(EVP_CIPHER_CTX *ctx) {
337 struct bf_ctr_ex *bce;
338
339 bce = EVP_CIPHER_CTX_get_app_data(ctx);
340 if (bce != NULL) {
341 pr_memscrub(bce, sizeof(struct bf_ctr_ex));
342 free(bce);
343 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
344 }
345
346 return 1;
347 }
348
349 static int do_bf_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dst,
350 const unsigned char *src, size_t len) {
351 struct bf_ctr_ex *bce;
352 unsigned int n;
353 unsigned char buf[BF_BLOCK];
354
355 if (len == 0) {
356 return 1;
357 }
358
359 bce = EVP_CIPHER_CTX_get_app_data(ctx);
360 if (bce == NULL) {
361 return 0;
362 }
363
364 n = 0;
365
366 while ((len--) > 0) {
367 pr_signals_handle();
368
369 if (n == 0) {
370 BF_LONG ctr[2];
371
372 /* Ideally, we would not be using htonl/ntohl here, and the following
373 * code would be as simple as:
374 *
375 * memcpy(buf, bce->counter, BF_BLOCK);
376 * BF_encrypt((BF_LONG *) buf, &(bce->key));
377 *
378 * However, the above is susceptible to endianness issues. The only
379 * client that I could find which implements the blowfish-ctr cipher,
380 * PuTTy, uses its own big-endian Blowfish implementation. So the
381 * above code will work with PuTTy, but only on big-endian machines.
382 * For little-endian machines, we need to handle the endianness
383 * ourselves. Whee.
384 */
385
386 memcpy(&(ctr[0]), bce->counter, sizeof(BF_LONG));
387 memcpy(&(ctr[1]), bce->counter + sizeof(BF_LONG), sizeof(BF_LONG));
388
389 /* Convert to big-endian values before encrypting the counter... */
390 ctr[0] = htonl(ctr[0]);
391 ctr[1] = htonl(ctr[1]);
392
393 BF_encrypt(ctr, &(bce->key));
394
395 /* ...and convert back to little-endian before XOR'ing the counter in. */
396 ctr[0] = ntohl(ctr[0]);
397 ctr[1] = ntohl(ctr[1]);
398
399 memcpy(buf, ctr, BF_BLOCK);
400
401 ctr_incr(bce->counter, BF_BLOCK);
402 }
403
404 *(dst++) = *(src++) ^ buf[n];
405 n = (n + 1) % BF_BLOCK;
406 }
407
408 return 1;
409 }
410
411 static const EVP_CIPHER *get_bf_ctr_cipher(void) {
412 EVP_CIPHER *cipher;
413
414 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
415 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
416 /* XXX TODO: At some point, we also need to call EVP_CIPHER_meth_free() on
417 * this, to avoid a resource leak.
418 */
419 cipher = EVP_CIPHER_meth_new(NID_bf_cbc, BF_BLOCK, 32);
420 EVP_CIPHER_meth_set_iv_length(cipher, BF_BLOCK);
421 EVP_CIPHER_meth_set_init(cipher, init_bf_ctr);
422 EVP_CIPHER_meth_set_cleanup(cipher, cleanup_bf_ctr);
423 EVP_CIPHER_meth_set_do_cipher(cipher, do_bf_ctr);
424 EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV);
425 #else
426 static EVP_CIPHER bf_ctr_cipher;
427
428 memset(&bf_ctr_cipher, 0, sizeof(EVP_CIPHER));
429
430 bf_ctr_cipher.nid = NID_bf_cbc;
431 bf_ctr_cipher.block_size = BF_BLOCK;
432 bf_ctr_cipher.iv_len = BF_BLOCK;
433 bf_ctr_cipher.key_len = 32;
434 bf_ctr_cipher.init = init_bf_ctr;
435 bf_ctr_cipher.cleanup = cleanup_bf_ctr;
436 bf_ctr_cipher.do_cipher = do_bf_ctr;
437
438 bf_ctr_cipher.flags = EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV;
439
440 cipher = &bf_ctr_cipher;
441 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
442
443 return cipher;
444 }
445 #endif /* !OPENSSL_NO_BF */
446
447 #if OPENSSL_VERSION_NUMBER > 0x000907000L
448
449 # if !defined(OPENSSL_NO_DES) && \
450 (!defined(HAVE_LIBRESSL) || \
451 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)) && \
452 OPENSSL_VERSION_NUMBER < 0x30000000L
453 /* 3DES CTR mode implementation */
454
455 struct des3_ctr_ex {
456 DES_key_schedule sched[3];
457 unsigned char counter[8];
458 int big_endian;
459 };
460
461 static uint32_t byteswap32(uint32_t in) {
462 uint32_t out;
463
464 out = (((in & 0x000000ff) << 24) |
465 ((in & 0x0000ff00) << 8) |
466 ((in & 0x00ff0000) >> 8) |
467 ((in & 0xff000000) >> 24));
468
469 return out;
470 }
471
472 static int init_des3_ctr(EVP_CIPHER_CTX *ctx, const unsigned char *key,
473 const unsigned char *iv, int enc) {
474 struct des3_ctr_ex *dce;
475
476 dce = EVP_CIPHER_CTX_get_app_data(ctx);
477 if (dce == NULL) {
478
479 /* Allocate our data structure. */
480 dce = calloc(1, sizeof(struct des3_ctr_ex));
481 if (dce == NULL) {
482 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
483 _exit(1);
484 }
485
486 /* Simple test to see if we're on a big- or little-endian machine:
487 * on big-endian machines, the ntohl() et al will be no-ops.
488 */
489 dce->big_endian = (ntohl(1234) == 1234);
490
491 EVP_CIPHER_CTX_set_app_data(ctx, dce);
492 }
493
494 if (key != NULL) {
495 register unsigned int i;
496 unsigned char *ptr;
497
498 ptr = (unsigned char *) key;
499
500 for (i = 0; i < 3; i++) {
501 DES_cblock material[8];
502 memcpy(material, ptr, 8);
503 ptr += 8;
504
505 DES_set_key_unchecked(material, &(dce->sched[i]));
506 }
507 }
508
509 if (iv != NULL) {
510 memcpy(dce->counter, iv, 8);
511 }
512
513 return 1;
514 }
515
516 static int cleanup_des3_ctr(EVP_CIPHER_CTX *ctx) {
517 struct des3_ctr_ex *dce;
518
519 dce = EVP_CIPHER_CTX_get_app_data(ctx);
520 if (dce != NULL) {
521 pr_memscrub(dce, sizeof(struct des3_ctr_ex));
522 free(dce);
523 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
524 }
525
526 return 1;
527 }
528
529 static int do_des3_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dst,
530 const unsigned char *src, size_t len) {
531 struct des3_ctr_ex *dce;
532 unsigned int n;
533 unsigned char buf[8];
534
535 if (len == 0) {
536 return 1;
537 }
538
539 dce = EVP_CIPHER_CTX_get_app_data(ctx);
540 if (dce == NULL) {
541 return 0;
542 }
543
544 n = 0;
545
546 while ((len--) > 0) {
547 pr_signals_handle();
548
549 if (n == 0) {
550 DES_LONG ctr[2];
551
552 memcpy(&(ctr[0]), dce->counter, sizeof(DES_LONG));
553 memcpy(&(ctr[1]), dce->counter + sizeof(DES_LONG), sizeof(DES_LONG));
554
555 if (dce->big_endian) {
556 /* If we are on a big-endian machine, we need to initialize the counter
557 * using little-endian values, since that is what OpenSSL's
558 * DES_encryptX() functions expect.
559 */
560
561 ctr[0] = byteswap32(ctr[0]);
562 ctr[1] = byteswap32(ctr[1]);
563 }
564
565 DES_encrypt3(ctr, &(dce->sched[0]), &(dce->sched[1]), &(dce->sched[2]));
566
567 if (dce->big_endian) {
568 ctr[0] = byteswap32(ctr[0]);
569 ctr[1] = byteswap32(ctr[1]);
570 }
571
572 memcpy(buf, ctr, 8);
573
574 ctr_incr(dce->counter, 8);
575 }
576
577 *(dst++) = *(src++) ^ buf[n];
578 n = (n + 1) % 8;
579 }
580
581 return 1;
582 }
583
584 static const EVP_CIPHER *get_des3_ctr_cipher(void) {
585 EVP_CIPHER *cipher;
586
587 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
588 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
589 unsigned long flags;
590
591 /* XXX TODO: At some point, we also need to call EVP_CIPHER_meth_free() on
592 * this, to avoid a resource leak.
593 */
594 cipher = EVP_CIPHER_meth_new(NID_des_ede3_ecb, 8, 24);
595 EVP_CIPHER_meth_set_iv_length(cipher, 8);
596 EVP_CIPHER_meth_set_init(cipher, init_des3_ctr);
597 EVP_CIPHER_meth_set_cleanup(cipher, cleanup_des3_ctr);
598 EVP_CIPHER_meth_set_do_cipher(cipher, do_des3_ctr);
599
600 flags = EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV;
601 # ifdef OPENSSL_FIPS
602 flags |= EVP_CIPH_FLAG_FIPS;
603 # endif /* OPENSSL_FIPS */
604
605 EVP_CIPHER_meth_set_flags(cipher, flags);
606
607 # else
608 static EVP_CIPHER des3_ctr_cipher;
609
610 memset(&des3_ctr_cipher, 0, sizeof(EVP_CIPHER));
611
612 des3_ctr_cipher.nid = NID_des_ede3_ecb;
613 des3_ctr_cipher.block_size = 8;
614 des3_ctr_cipher.iv_len = 8;
615 des3_ctr_cipher.key_len = 24;
616 des3_ctr_cipher.init = init_des3_ctr;
617 des3_ctr_cipher.cleanup = cleanup_des3_ctr;
618 des3_ctr_cipher.do_cipher = do_des3_ctr;
619
620 des3_ctr_cipher.flags = EVP_CIPH_CBC_MODE|EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV;
621 # ifdef OPENSSL_FIPS
622 des3_ctr_cipher.flags |= EVP_CIPH_FLAG_FIPS;
623 # endif /* OPENSSL_FIPS */
624
625 cipher = &des3_ctr_cipher;
626 # endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
627
628 return cipher;
629 }
630 # endif /* !OPENSSL_NO_DES and prior to OpenSSL 3.x */
631
632 #if !defined(HAVE_EVP_AES_256_CTR_OPENSSL) && \
633 !defined(HAVE_EVP_AES_192_CTR_OPENSSL) && \
634 !defined(HAVE_EVP_AES_128_CTR_OPENSSL) && \
635 (!defined(HAVE_LIBRESSL) || \
636 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L))
637
638 /* AES CTR mode implementation */
639 struct aes_ctr_ex {
640 AES_KEY key;
641 unsigned char counter[AES_BLOCK_SIZE];
642 unsigned char enc_counter[AES_BLOCK_SIZE];
643 unsigned int num;
644 };
645
646 static int init_aes_ctr(EVP_CIPHER_CTX *ctx, const unsigned char *key,
647 const unsigned char *iv, int enc) {
648 struct aes_ctr_ex *ace;
649
650 ace = EVP_CIPHER_CTX_get_app_data(ctx);
651 if (ace == NULL) {
652
653 /* Allocate our data structure. */
654 ace = calloc(1, sizeof(struct aes_ctr_ex));
655 if (ace == NULL) {
656 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
657 _exit(1);
658 }
659
660 EVP_CIPHER_CTX_set_app_data(ctx, ace);
661 }
662
663 if (key != NULL) {
664 int nbits;
665
666 # if OPENSSL_VERSION_NUMBER == 0x0090805fL
667 /* OpenSSL 0.9.8e had a bug where EVP_CIPHER_CTX_key_length() returned
668 * the cipher key length rather than the context key length.
669 */
670 nbits = ctx->key_len * 8;
671 # else
672 nbits = EVP_CIPHER_CTX_key_length(ctx) * 8;
673 # endif
674
675 if (AES_set_encrypt_key(key, nbits, &(ace->key)) != 0) {
676 pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
677 "error setting AES CTR mode encryption key");
678 }
679 }
680
681 if (iv != NULL) {
682 memcpy(ace->counter, iv, AES_BLOCK_SIZE);
683 }
684
685 return 1;
686 }
687
688 static int cleanup_aes_ctr(EVP_CIPHER_CTX *ctx) {
689 struct aes_ctr_ex *ace;
690
691 ace = EVP_CIPHER_CTX_get_app_data(ctx);
692 if (ace != NULL) {
693 pr_memscrub(ace, sizeof(struct aes_ctr_ex));
694 free(ace);
695 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
696 }
697
698 return 1;
699 }
700
701 static int do_aes_ctr(EVP_CIPHER_CTX *ctx, unsigned char *dst,
702 const unsigned char *src, size_t len) {
703 struct aes_ctr_ex *ace;
704 # if OPENSSL_VERSION_NUMBER <= 0x0090704fL || \
705 OPENSSL_VERSION_NUMBER >= 0x10100000L
706 unsigned int n;
707 unsigned char buf[AES_BLOCK_SIZE];
708 # endif
709
710 if (len == 0) {
711 return 1;
712 }
713
714 ace = EVP_CIPHER_CTX_get_app_data(ctx);
715 if (ace == NULL) {
716 return 0;
717 }
718
719 # if OPENSSL_VERSION_NUMBER <= 0x0090704fL || \
720 OPENSSL_VERSION_NUMBER >= 0x10100000L
721 /* In OpenSSL-0.9.7d and earlier, the AES CTR code did not properly handle
722 * the IV as big-endian; this would cause the dreaded "Incorrect MAC
723 * received on packet" error when using clients e.g. PuTTy. To see
724 * the difference in OpenSSL, you have do manually do:
725 *
726 * diff -u openssl-0.9.7d/crypto/aes/aes_ctr.c \
727 * openssl-0.9.7e/crypto/aes/aes_ctr.c
728 *
729 * This change is not documented in OpenSSL's CHANGES file. Sigh.
730 *
731 * And in OpenSSL-1.1.0 and later, the AES CTR code was removed entirely.
732 *
733 * Thus for these versions, we have to use our own AES CTR code.
734 */
735
736 n = 0;
737
738 while ((len--) > 0) {
739 pr_signals_handle();
740
741 if (n == 0) {
742 AES_encrypt(ace->counter, buf, &(ace->key));
743 ctr_incr(ace->counter, AES_BLOCK_SIZE);
744 }
745
746 *(dst++) = *(src++) ^ buf[n];
747 n = (n + 1) % AES_BLOCK_SIZE;
748 }
749
750 return 1;
751 # else
752 /* Thin wrapper around AES_ctr128_encrypt(). */
753 AES_ctr128_encrypt(src, dst, len, &(ace->key), ace->counter, ace->enc_counter,
754 &(ace->num));
755 # endif
756
757 return 1;
758 }
759
760 static int get_aes_ctr_cipher_nid(int key_len) {
761 int nid;
762
763 # ifdef OPENSSL_FIPS
764 /* Set the NID depending on the key len. */
765 switch (key_len) {
766 case 16:
767 nid = NID_aes_128_cbc;
768 break;
769
770 case 24:
771 nid = NID_aes_192_cbc;
772 break;
773
774 case 32:
775 nid = NID_aes_256_cbc;
776 break;
777
778 default:
779 nid = NID_undef;
780 break;
781 }
782
783 # else
784 /* Setting this nid member to something other than NID_undef causes
785 * interesting problems on an OpenSolaris system, using the provided
786 * OpenSSL installation's pkcs11 engine via:
787 *
788 * SFTPCryptoDevice pkcs11
789 *
790 * for the mod_sftp config. I'm not sure why; I need to look into this
791 * issue more.
792 *
793 * For posterity, the issues seen when using the above config are
794 * described below. After sending the NEWKEYS request, mod_sftp
795 * would log the following, upon receiving the next message from sftp(1):
796 *
797 * <ssh2:20>: SSH2 packet len = 1500737511 bytes
798 * <ssh2:20>: SSH2 packet padding len = 95 bytes
799 * <ssh2:20>: SSH2 packet payload len = 1500737415 bytes
800 * <ssh2:20>: payload len (1500737415 bytes) exceeds max payload len (262144), ignoring payload
801 * client sent buggy/malicious packet payload length, ignoring
802 *
803 * and sftp(1), for its side, would report:
804 *
805 * debug1: send SSH2_MSG_SERVICE_REQUEST
806 * Disconnecting: Bad packet length.
807 * debug1: Calling cleanup 0x807cc14(0x0)
808 * Couldn't read packet: Error 0
809 */
810 nid = NID_undef;
811 # endif /* OPENSSL_FIPS */
812
813 return nid;
814 }
815
816 static const EVP_CIPHER *get_aes_ctr_cipher(int key_len) {
817 EVP_CIPHER *cipher;
818
819 # if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
820 !defined(HAVE_LIBRESSL)
821 unsigned long flags;
822
823 /* XXX TODO: At some point, we also need to call EVP_CIPHER_meth_free() on
824 * this, to avoid a resource leak.
825 */
826 cipher = EVP_CIPHER_meth_new(get_aes_ctr_cipher_nid(key_len), AES_BLOCK_SIZE,
827 key_len);
828 EVP_CIPHER_meth_set_iv_length(cipher, AES_BLOCK_SIZE);
829 EVP_CIPHER_meth_set_init(cipher, init_aes_ctr);
830 EVP_CIPHER_meth_set_cleanup(cipher, cleanup_aes_ctr);
831 EVP_CIPHER_meth_set_do_cipher(cipher, do_aes_ctr);
832
833 flags = EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV;
834
835 # if defined(EVP_CIPH_CTR_MODE)
836 flags |= EVP_CIPH_CTR_MODE;
837 # else
838 flags |= EVP_CIPH_CBC_MODE;
839 # endif /* EVP_CIPH_CTR_MODE */
840
841 # if defined(EVP_CIPH_FLAG_CUSTOM_CIPHER)
842 flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
843 # endif /* EVP_CIPH_FLAG_CUSTOM_CIPHER */
844
845 # if defined(OPENSSL_FIPS)
846 flags |= EVP_CIPH_FLAG_FIPS;
847 # endif /* OPENSSL_FIPS */
848
849 EVP_CIPHER_meth_set_flags(cipher, flags);
850
851 # else
852 static EVP_CIPHER aes_ctr_cipher;
853
854 memset(&aes_ctr_cipher, 0, sizeof(EVP_CIPHER));
855
856 aes_ctr_cipher.nid = get_aes_ctr_cipher_nid(key_len);
857 aes_ctr_cipher.block_size = AES_BLOCK_SIZE;
858 aes_ctr_cipher.iv_len = AES_BLOCK_SIZE;
859 aes_ctr_cipher.key_len = key_len;
860 aes_ctr_cipher.init = init_aes_ctr;
861 aes_ctr_cipher.cleanup = cleanup_aes_ctr;
862 aes_ctr_cipher.do_cipher = do_aes_ctr;
863 aes_ctr_cipher.flags = EVP_CIPH_VARIABLE_LENGTH|EVP_CIPH_ALWAYS_CALL_INIT|EVP_CIPH_CUSTOM_IV;
864
865 # if defined(EVP_CIPH_CTR_MODE)
866 aes_ctr_cipher.flags |= EVP_CIPH_CTR_MODE;
867 # else
868 aes_ctr_cipher.flags |= EVP_CIPH_CBC_MODE;
869 # endif /* EVP_CIPH_CTR_MODE */
870
871 # if defined(EVP_CIPH_FLAG_CUSTOM_CIPHER)
872 aes_ctr_cipher.flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
873 # endif /* EVP_CIPH_FLAG_CUSTOM_CIPHER */
874
875 # ifdef OPENSSL_FIPS
876 aes_ctr_cipher.flags |= EVP_CIPH_FLAG_FIPS;
877 # endif /* OPENSSL_FIPS */
878
879 cipher = &aes_ctr_cipher;
880 # endif /* prior to OpenSSL-1.1.0 */
881
882 return cipher;
883 }
884 #endif /* OpenSSL implements AES CTR modes */
885
886 #if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL)
887 /* We'll use the Provider interface for UMAC digests in this case. */
888 #else
889 static int update_umac64(EVP_MD_CTX *ctx, const void *data, size_t len) {
890 int res;
891 void *md_data;
892
893 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
894 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
895 md_data = EVP_MD_CTX_md_data(ctx);
896 #else
897 md_data = ctx->md_data;
898 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
899 if (md_data == NULL) {
900 struct umac_ctx *umac;
901 void **ptr;
902
903 umac = umac_new((unsigned char *) data);
904 if (umac == NULL) {
905 return 0;
906 }
907
908 ptr = &md_data;
909 *ptr = umac;
910 return 1;
911 }
912
913 res = umac_update(md_data, (unsigned char *) data, (long) len);
914 return res;
915 }
916
917 static int update_umac128(EVP_MD_CTX *ctx, const void *data, size_t len) {
918 int res;
919 void *md_data;
920
921 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
922 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
923 md_data = EVP_MD_CTX_md_data(ctx);
924 #else
925 md_data = ctx->md_data;
926 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
927
928 if (md_data == NULL) {
929 struct umac_ctx *umac;
930 void **ptr;
931
932 umac = umac128_new((unsigned char *) data);
933 if (umac == NULL) {
934 return 0;
935 }
936
937 ptr = &md_data;
938 *ptr = umac;
939 return 1;
940 }
941
942 res = umac128_update(md_data, (unsigned char *) data, (long) len);
943 return res;
944 }
945
946 static int final_umac64(EVP_MD_CTX *ctx, unsigned char *md) {
947 unsigned char nonce[8];
948 int res;
949 void *md_data;
950
951 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
952 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
953 md_data = EVP_MD_CTX_md_data(ctx);
954 #else
955 md_data = ctx->md_data;
956 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
957
958 res = umac_final(md_data, md, nonce);
959 return res;
960 }
961
962 static int final_umac128(EVP_MD_CTX *ctx, unsigned char *md) {
963 unsigned char nonce[8];
964 int res;
965 void *md_data;
966
967 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
968 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
969 md_data = EVP_MD_CTX_md_data(ctx);
970 #else
971 md_data = ctx->md_data;
972 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
973
974 res = umac128_final(md_data, md, nonce);
975 return res;
976 }
977
978 static int delete_umac64(EVP_MD_CTX *ctx) {
979 struct umac_ctx *umac;
980 void *md_data, **ptr;
981
982 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
983 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
984 md_data = EVP_MD_CTX_md_data(ctx);
985 #else
986 md_data = ctx->md_data;
987 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
988
989 umac = md_data;
990 umac_delete(umac);
991
992 ptr = &md_data;
993 *ptr = NULL;
994
995 return 1;
996 }
997
998 static int delete_umac128(EVP_MD_CTX *ctx) {
999 struct umac_ctx *umac;
1000 void *md_data, **ptr;
1001
1002 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1003 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1004 md_data = EVP_MD_CTX_md_data(ctx);
1005 #else
1006 md_data = ctx->md_data;
1007 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1008
1009 umac = md_data;
1010 umac128_delete(umac);
1011
1012 ptr = &md_data;
1013 *ptr = NULL;
1014
1015 return 1;
1016 }
1017 #endif /* OpenSSL before 4.x */
1018
1019 static const EVP_MD *get_umac64_digest(int *free_md) {
1020 EVP_MD *md = NULL;
1021
1022 *free_md = FALSE;
1023
1024 #if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL)
1025 md = EVP_MD_fetch(NULL, "umac64", NULL);
1026 if (md == NULL) {
1027 pr_trace_msg(trace_channel, 4, "error fetching 'umac64' EVP_MD: %s",
1028 sftp_crypto_get_errors());
1029
1030 } else {
1031 *free_md = TRUE;
1032 }
1033
1034 #else
1035 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1036 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1037 /* XXX TODO: At some point, we also need to call EVP_MD_meth_free() on
1038 * this, to avoid a resource leak.
1039 */
1040 md = EVP_MD_meth_new(NID_undef, NID_undef);
1041 EVP_MD_meth_set_input_blocksize(md, 32);
1042 EVP_MD_meth_set_result_size(md, 8);
1043 EVP_MD_meth_set_flags(md, 0UL);
1044 EVP_MD_meth_set_update(md, update_umac64);
1045 EVP_MD_meth_set_final(md, final_umac64);
1046 EVP_MD_meth_set_cleanup(md, delete_umac64);
1047 # else
1048 static EVP_MD umac64_digest;
1049
1050 memset(&umac64_digest, 0, sizeof(EVP_MD));
1051
1052 umac64_digest.type = NID_undef;
1053 umac64_digest.pkey_type = NID_undef;
1054 umac64_digest.md_size = 8;
1055 umac64_digest.flags = 0UL;
1056 umac64_digest.update = update_umac64;
1057 umac64_digest.final = final_umac64;
1058 umac64_digest.cleanup = delete_umac64;
1059 umac64_digest.block_size = 32;
1060
1061 md = &umac64_digest;
1062 # endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1063 #endif /* OpenSSL before 4.x */
1064
1065 return md;
1066 }
1067
1068 static const EVP_MD *get_umac128_digest(int *free_md) {
1069 EVP_MD *md = NULL;
1070
1071 *free_md = FALSE;
1072
1073 #if OPENSSL_VERSION_NUMBER >= 0x40000000L && !defined(HAVE_LIBRESSL)
1074 md = EVP_MD_fetch(NULL, "umac128", NULL);
1075 if (md == NULL) {
1076 pr_trace_msg(trace_channel, 4, "error fetching 'umac128' EVP_MD: %s",
1077 sftp_crypto_get_errors());
1078
1079 } else {
1080 *free_md = TRUE;
1081 }
1082
1083 #else
1084 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1085 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1086 /* XXX TODO: At some point, we also need to call EVP_MD_meth_free() on
1087 * this, to avoid a resource leak.
1088 */
1089 md = EVP_MD_meth_new(NID_undef, NID_undef);
1090 EVP_MD_meth_set_input_blocksize(md, 64);
1091 EVP_MD_meth_set_result_size(md, 16);
1092 EVP_MD_meth_set_flags(md, 0UL);
1093 EVP_MD_meth_set_update(md, update_umac128);
1094 EVP_MD_meth_set_final(md, final_umac128);
1095 EVP_MD_meth_set_cleanup(md, delete_umac128);
1096 # else
1097 static EVP_MD umac128_digest;
1098
1099 memset(&umac128_digest, 0, sizeof(EVP_MD));
1100
1101 umac128_digest.type = NID_undef;
1102 umac128_digest.pkey_type = NID_undef;
1103 umac128_digest.md_size = 16;
1104 umac128_digest.flags = 0UL;
1105 umac128_digest.update = update_umac128;
1106 umac128_digest.final = final_umac128;
1107 umac128_digest.cleanup = delete_umac128;
1108 umac128_digest.block_size = 64;
1109
1110 md = &umac128_digest;
1111 # endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1112 #endif /* OpenSSL before 4.x */
1113
1114 return md;
1115 }
1116 #endif /* OpenSSL older than 0.9.7 */
1117
1118 const EVP_CIPHER *sftp_crypto_get_cipher(const char *name, size_t *key_len,
1119 size_t *auth_len, size_t *discard_len) {
1120 register unsigned int i;
1121
1122 if (name == NULL) {
1123 errno = EINVAL;
1124 return NULL;
1125 }
1126
1127 for (i = 0; ciphers[i].name; i++) {
1128 if (strcmp(ciphers[i].name, name) == 0) {
1129 const EVP_CIPHER *cipher;
1130
1131 if (strcmp(name, "blowfish-ctr") == 0) {
1132 #if !defined(OPENSSL_NO_BF) && \
1133 (!defined(HAVE_LIBRESSL) || \
1134 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)) && \
1135 OPENSSL_VERSION_NUMBER < 0x30000000L
1136 cipher = get_bf_ctr_cipher();
1137 #else
1138 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1139 "'%s' cipher unsupported", name);
1140 errno = ENOENT;
1141 return NULL;
1142 #endif /* !OPENSSL_NO_BF */
1143
1144 #if OPENSSL_VERSION_NUMBER > 0x000907000L
1145 } else if (strcmp(name, "3des-ctr") == 0) {
1146 # if !defined(OPENSSL_NO_DES) && \
1147 (!defined(HAVE_LIBRESSL) || \
1148 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)) && \
1149 OPENSSL_VERSION_NUMBER < 0x30000000L
1150 cipher = get_des3_ctr_cipher();
1151 # else
1152 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1153 "'%s' cipher unsupported", name);
1154 errno = ENOENT;
1155 return NULL;
1156 # endif /* !OPENSSL_NO_DES */
1157
1158 } else if (strcmp(name, "aes256-ctr") == 0) {
1159 # if defined(HAVE_EVP_AES_256_CTR_OPENSSL)
1160 cipher = EVP_aes_256_ctr();
1161 # else
1162 cipher = get_aes_ctr_cipher(32);
1163 # endif /* HAVE_EVP_AES_256_CTR_OPENSSL */
1164
1165 } else if (strcmp(name, "aes192-ctr") == 0) {
1166 # if defined(HAVE_EVP_AES_192_CTR_OPENSSL)
1167 cipher = EVP_aes_192_ctr();
1168 # else
1169 cipher = get_aes_ctr_cipher(24);
1170 # endif /* HAVE_EVP_AES_192_CTR_OPENSSL */
1171
1172 } else if (strcmp(name, "aes128-ctr") == 0) {
1173 # if defined(HAVE_EVP_AES_128_CTR_OPENSSL)
1174 cipher = EVP_aes_128_ctr();
1175 # else
1176 cipher = get_aes_ctr_cipher(16);
1177 # endif /* HAVE_EVP_AES_128_CTR_OPENSSL */
1178 #endif /* OpenSSL older than 0.9.7 */
1179
1180 } else {
1181 cipher = ciphers[i].get_type();
1182 }
1183
1184 if (key_len != NULL) {
1185 if (strcmp(name, "arcfour256") == 0) {
1186 /* The arcfour256 cipher is special-cased here in order to use
1187 * a longer key (32 bytes), rather than the normal 16 bytes for the
1188 * RC4 cipher.
1189 */
1190
1191 *key_len = 32;
1192
1193 } else if (strcmp(name, "[email protected]") == 0) {
1194 *key_len = 64;
1195
1196 } else {
1197 *key_len = 0;
1198 }
1199 }
1200
1201 if (auth_len != NULL) {
1202 *auth_len = ciphers[i].auth_len;
1203 }
1204
1205 if (discard_len != NULL) {
1206 *discard_len = ciphers[i].discard_len;
1207 }
1208
1209 return cipher;
1210 }
1211 }
1212
1213 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1214 "no cipher matching '%s' found", name);
1215 errno = ENOENT;
1216 return NULL;
1217 }
1218
1219 void sftp_crypto_free_digest(const EVP_MD *md) {
1220 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(HAVE_LIBRESSL)) || \
1221 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3080000L)
1222 EVP_MD_free((EVP_MD *) md);
1223 #else
1224 /* Avoid compiler warnings. */
1225 (void) md;
1226 #endif /* OpenSSL-3.x/LibreSSL-3.8.x and later */
1227 }
1228
1229 const EVP_MD *sftp_crypto_get_digest(const char *name, uint32_t *mac_len,
1230 int *free_md) {
1231 register unsigned int i;
1232
1233 if (name == NULL) {
1234 errno = EINVAL;
1235 return NULL;
1236 }
1237
1238 *free_md = FALSE;
1239
1240 for (i = 0; digests[i].name; i++) {
1241 if (strcmp(digests[i].name, name) == 0) {
1242 const EVP_MD *digest = NULL;
1243
1244 #if OPENSSL_VERSION_NUMBER > 0x000907000L
1245 if (strcmp(name, "[email protected]") == 0 ||
1246 strcmp(name, "[email protected]") == 0) {
1247 digest = get_umac64_digest(free_md);
1248
1249 } else if (strcmp(name, "[email protected]") == 0 ||
1250 strcmp(name, "[email protected]") == 0) {
1251 digest = get_umac128_digest(free_md);
1252 #else
1253 if (FALSE) {
1254 #endif /* OpenSSL older than 0.9.7 */
1255
1256 } else {
1257 digest = digests[i].get_type();
1258 }
1259
1260 if (mac_len) {
1261 *mac_len = digests[i].mac_len;
1262 }
1263
1264 return digest;
1265 }
1266 }
1267
1268 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1269 "no digest matching '%s' found", name);
1270 return NULL;
1271 }
1272
1273 int sftp_crypto_is_hostkey(const char *name) {
1274 register unsigned int i;
1275
1276 if (name == NULL) {
1277 errno = EINVAL;
1278 return -1;
1279 }
1280
1281 for (i = 0; hostkey_algos[i]; i++) {
1282 if (strcmp(hostkey_algos[i], name) == 0) {
1283 return TRUE;
1284 }
1285 }
1286
1287 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1288 "no hostkey matching '%s' found", name);
1289 errno = ENOENT;
1290 return -1;
1291 }
1292
1293 int sftp_crypto_is_key_exchange(const char *name) {
1294 register unsigned int i;
1295
1296 if (name == NULL) {
1297 errno = EINVAL;
1298 return -1;
1299 }
1300
1301 for (i = 0; key_exchanges[i]; i++) {
1302 if (strcmp(key_exchanges[i], name) == 0) {
1303 return TRUE;
1304 }
1305 }
1306
1307 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1308 "no key exchange matching '%s' found", name);
1309 errno = ENOENT;
1310 return -1;
1311 }
1312
1313 const char *sftp_crypto_get_kexinit_cipher_list(pool *p) {
1314 char *res = "";
1315 config_rec *c;
1316
1317 /* Make sure that OpenSSL can use these ciphers. For example, in FIPS mode,
1318 * some ciphers cannot be used. So we should not advertise ciphers that we
1319 * know we cannot use.
1320 */
1321
1322 c = find_config(main_server->conf, CONF_PARAM, "SFTPCiphers", FALSE);
1323 if (c != NULL) {
1324 register unsigned int i;
1325
1326 for (i = 0; i < c->argc; i++) {
1327 register unsigned int j;
1328
1329 for (j = 0; ciphers[j].name; j++) {
1330 if (strcmp(c->argv[i], ciphers[j].name) == 0) {
1331 #if defined(OPENSSL_FIPS)
1332 if (FIPS_mode()) {
1333 /* If FIPS mode is enabled, check whether the cipher is allowed
1334 * for use.
1335 */
1336 if (ciphers[j].fips_allowed == FALSE) {
1337 pr_trace_msg(trace_channel, 5,
1338 "cipher '%s' is disabled in FIPS mode, skipping",
1339 ciphers[j].name);
1340 continue;
1341 }
1342 }
1343 #endif /* OPENSSL_FIPS */
1344 if (strcmp(c->argv[i], "none") != 0) {
1345 if (EVP_get_cipherbyname(ciphers[j].openssl_name) != NULL) {
1346 res = pstrcat(p, res, *res ? "," : "",
1347 pstrdup(p, ciphers[j].name), NULL);
1348
1349 } else {
1350 /* The CTR and GCM modes are special cases. */
1351
1352 if (strcmp(ciphers[j].name, "blowfish-ctr") == 0 ||
1353 strcmp(ciphers[j].name, "3des-ctr") == 0
1354 #if OPENSSL_VERSION_NUMBER > 0x000907000L
1355 || strcmp(ciphers[j].name, "aes256-ctr") == 0 ||
1356 strcmp(ciphers[j].name, "aes192-ctr") == 0 ||
1357 strcmp(ciphers[j].name, "aes128-ctr") == 0
1358 #endif
1359 #if defined(HAVE_EVP_AES_256_GCM_OPENSSL)
1360 || strcmp(ciphers[j].name, "[email protected]") == 0 ||
1361 strcmp(ciphers[j].name, "[email protected]") == 0
1362 #endif
1363 ) {
1364 res = pstrcat(p, res, *res ? "," : "",
1365 pstrdup(p, ciphers[j].name), NULL);
1366
1367 } else {
1368 pr_trace_msg(trace_channel, 3,
1369 "unable to use '%s' cipher: Unsupported by OpenSSL",
1370 ciphers[j].name);
1371 }
1372 }
1373
1374 } else {
1375 res = pstrcat(p, res, *res ? "," : "",
1376 pstrdup(p, ciphers[j].name), NULL);
1377 }
1378 }
1379 }
1380 }
1381
1382 } else {
1383 register unsigned int i;
1384
1385 for (i = 0; ciphers[i].name; i++) {
1386 if (ciphers[i].enabled == TRUE) {
1387 #if defined(OPENSSL_FIPS)
1388 if (FIPS_mode()) {
1389 /* If FIPS mode is enabled, check whether the cipher is allowed
1390 * for use.
1391 */
1392 if (ciphers[i].fips_allowed == FALSE) {
1393 pr_trace_msg(trace_channel, 5,
1394 "cipher '%s' is disabled in FIPS mode, skipping",
1395 ciphers[i].name);
1396 continue;
1397 }
1398 }
1399 #endif /* OPENSSL_FIPS */
1400
1401 if (strcmp(ciphers[i].name, "none") != 0) {
1402 if (EVP_get_cipherbyname(ciphers[i].openssl_name) != NULL) {
1403 res = pstrcat(p, res, *res ? "," : "",
1404 pstrdup(p, ciphers[i].name), NULL);
1405
1406 } else {
1407 /* The CTR and GCM modes are special cases. */
1408
1409 if (strcmp(ciphers[i].name, "blowfish-ctr") == 0 ||
1410 strcmp(ciphers[i].name, "3des-ctr") == 0
1411 #if OPENSSL_VERSION_NUMBER > 0x000907000L
1412 || strcmp(ciphers[i].name, "aes256-ctr") == 0 ||
1413 strcmp(ciphers[i].name, "aes192-ctr") == 0 ||
1414 strcmp(ciphers[i].name, "aes128-ctr") == 0
1415 #endif
1416 #if defined(HAVE_EVP_AES_256_GCM_OPENSSL)
1417 || strcmp(ciphers[i].name, "[email protected]") == 0 ||
1418 strcmp(ciphers[i].name, "[email protected]") == 0
1419 #endif
1420 ) {
1421 res = pstrcat(p, res, *res ? "," : "",
1422 pstrdup(p, ciphers[i].name), NULL);
1423
1424 } else {
1425 pr_trace_msg(trace_channel, 3,
1426 "unable to use '%s' cipher: Unsupported by OpenSSL",
1427 ciphers[i].name);
1428 }
1429 }
1430
1431 } else {
1432 res = pstrcat(p, res, *res ? "," : "",
1433 pstrdup(p, ciphers[i].name), NULL);
1434 }
1435
1436 } else {
1437 pr_trace_msg(trace_channel, 3, "unable to use '%s' cipher: "
1438 "Must be explicitly requested via SFTPCiphers", ciphers[i].name);
1439 }
1440 }
1441 }
1442
1443 return res;
1444 }
1445
1446 const char *sftp_crypto_get_kexinit_digest_list(pool *p) {
1447 char *res = "";
1448 config_rec *c;
1449
1450 /* Make sure that OpenSSL can use these digests. For example, in FIPS
1451 * mode, some digests cannot be used. So we should not advertise digests
1452 * that we know we cannot use.
1453 */
1454
1455 c = find_config(main_server->conf, CONF_PARAM, "SFTPDigests", FALSE);
1456 if (c != NULL) {
1457 register unsigned int i;
1458
1459 for (i = 0; i < c->argc; i++) {
1460 register unsigned int j;
1461
1462 for (j = 0; digests[j].name; j++) {
1463 if (strcmp(c->argv[i], digests[j].name) == 0) {
1464 #ifdef OPENSSL_FIPS
1465 if (FIPS_mode()) {
1466 /* If FIPS mode is enabled, check whether the MAC is allowed
1467 * for use.
1468 */
1469 if (digests[j].fips_allowed == FALSE) {
1470 pr_trace_msg(trace_channel, 5,
1471 "digest '%s' is disabled in FIPS mode, skipping",
1472 digests[j].name);
1473 continue;
1474 }
1475 }
1476 #endif /* OPENSSL_FIPS */
1477
1478 if (strcmp(c->argv[i], "none") != 0) {
1479 if (digests[j].openssl_name != NULL &&
1480 EVP_get_digestbyname(digests[j].openssl_name) != NULL) {
1481 res = pstrcat(p, res, *res ? "," : "",
1482 pstrdup(p, digests[j].name), NULL);
1483
1484 } else {
1485 /* The umac-64/umac-128 digests are special cases. */
1486 if (strcmp(digests[j].name, "[email protected]") == 0 ||
1487 strcmp(digests[j].name, "[email protected]") == 0 ||
1488 strcmp(digests[j].name, "[email protected]") == 0 ||
1489 strcmp(digests[j].name, "[email protected]") == 0) {
1490 res = pstrcat(p, res, *res ? "," : "",
1491 pstrdup(p, digests[j].name), NULL);
1492
1493 } else {
1494 pr_trace_msg(trace_channel, 3,
1495 "unable to use '%s' digest: Unsupported by OpenSSL",
1496 digests[j].name);
1497 }
1498 }
1499
1500 } else {
1501 res = pstrcat(p, res, *res ? "," : "",
1502 pstrdup(p, digests[j].name), NULL);
1503 }
1504 }
1505 }
1506 }
1507
1508 } else {
1509 register unsigned int i;
1510
1511 for (i = 0; digests[i].name; i++) {
1512 if (digests[i].enabled) {
1513 #ifdef OPENSSL_FIPS
1514 if (FIPS_mode()) {
1515 /* If FIPS mode is enabled, check whether the digest is allowed
1516 * for use.
1517 */
1518 if (digests[i].fips_allowed == FALSE) {
1519 pr_trace_msg(trace_channel, 5,
1520 "digest '%s' is disabled in FIPS mode, skipping",
1521 digests[i].name);
1522 continue;
1523 }
1524 }
1525 #endif /* OPENSSL_FIPS */
1526
1527 if (strcmp(digests[i].name, "none") != 0) {
1528 if (digests[i].openssl_name != NULL &&
1529 EVP_get_digestbyname(digests[i].openssl_name) != NULL) {
1530 res = pstrcat(p, res, *res ? "," : "",
1531 pstrdup(p, digests[i].name), NULL);
1532
1533 } else {
1534 /* The umac-64/umac-128 digests are special cases. */
1535 if (strcmp(digests[i].name, "[email protected]") == 0 ||
1536 strcmp(digests[i].name, "[email protected]") == 0 ||
1537 strcmp(digests[i].name, "[email protected]") == 0 ||
1538 strcmp(digests[i].name, "[email protected]") == 0) {
1539 res = pstrcat(p, res, *res ? "," : "",
1540 pstrdup(p, digests[i].name), NULL);
1541
1542 } else {
1543 pr_trace_msg(trace_channel, 3,
1544 "unable to use '%s' digest: Unsupported by OpenSSL",
1545 digests[i].name);
1546 }
1547 }
1548
1549 } else {
1550 res = pstrcat(p, res, *res ? "," : "",
1551 pstrdup(p, digests[i].name), NULL);
1552 }
1553
1554 } else {
1555 pr_trace_msg(trace_channel, 3, "unable to use '%s' digest: "
1556 "Must be explicitly requested via SFTPDigests", digests[i].name);
1557 }
1558 }
1559 }
1560
1561 return res;
1562 }
1563
1564 const char *sftp_crypto_get_errors(void) {
1565 unsigned int count = 0;
1566 unsigned long error_code;
1567 BIO *bio = NULL;
1568 char *data = NULL;
1569 long datalen;
1570 const char *error_data = NULL, *str = "(unknown)";
1571 int error_flags = 0;
1572
1573 /* Use ERR_print_errors() and a memory BIO to build up a string with
1574 * all of the error messages from the error queue.
1575 */
1576
1577 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
1578 if (error_code) {
1579 bio = BIO_new(BIO_s_mem());
1580 }
1581
1582 while (error_code) {
1583 if (error_flags & ERR_TXT_STRING) {
1584 BIO_printf(bio, "\n (%u) %s [%s]", ++count,
1585 ERR_error_string(error_code, NULL), error_data);
1586
1587 } else {
1588 BIO_printf(bio, "\n (%u) %s", ++count,
1589 ERR_error_string(error_code, NULL));
1590 }
1591
1592 error_data = NULL;
1593 error_flags = 0;
1594 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
1595 }
1596
1597 datalen = BIO_get_mem_data(bio, &data);
1598 if (data != NULL) {
1599 data[datalen] = '\0';
1600 str = pstrdup(sftp_pool, data);
1601 }
1602
1603 if (bio) {
1604 BIO_free(bio);
1605 }
1606
1607 return str;
1608 }
1609
1610 /* Try to find the best multiple/block size which accommodates the two given
1611 * sizes by rounding up.
1612 */
1613 size_t sftp_crypto_get_size(size_t first, size_t second) {
1614 #ifdef roundup
1615 return roundup(first, second);
1616 #else
1617 return (((first + (second - 1)) / second) * second);
1618 #endif /* !roundup */
1619 }
1620
1621 int sftp_crypto_init(void) {
1622 if (sftp_provider_init() < 0) {
1623 return -1;
1624 }
1625
1626 return 0;
1627 }
1628
1629 void sftp_crypto_free(int flags) {
1630 sftp_provider_free();
1631
1632 /* Only call EVP_cleanup() et al if other OpenSSL-using modules are not
1633 * present. If we called EVP_cleanup() here during a restart,
1634 * and other modules want to use OpenSSL, we may be depriving those modules
1635 * of OpenSSL functionality.
1636 *
1637 * At the moment, the modules known to use OpenSSL are mod_ldap, mod_radius,
1638 * mod_sftp, mod_sql, and mod_sql_passwd, and mod_tls.
1639 */
1640 if (pr_module_get("mod_auth_otp.c") == NULL &&
1641 pr_module_get("mod_digest.c") == NULL &&
1642 pr_module_get("mod_ldap.c") == NULL &&
1643 pr_module_get("mod_radius.c") == NULL &&
1644 pr_module_get("mod_sql.c") == NULL &&
1645 pr_module_get("mod_sql_passwd.c") == NULL &&
1646 pr_module_get("mod_tls.c") == NULL) {
1647
1648 #if OPENSSL_VERSION_NUMBER > 0x000907000L && \
1649 OPENSSL_VERSION_NUMBER < 0x10100000L
1650 # if defined(PR_USE_OPENSSL_ENGINE)
1651 if (crypto_engine != NULL) {
1652 ENGINE_cleanup();
1653 crypto_engine = NULL;
1654 }
1655 # endif /* PR_USE_OPENSSL_ENGINE */
1656 #endif
1657
1658 #if OPENSSL_VERSION_NUMBER >= 0x10000001L
1659 /* The ERR_remove_state(0) usage is deprecated due to thread ID
1660 * differences among platforms; see the OpenSSL-1.0.0 CHANGES file
1661 * for details. So for new enough OpenSSL installations, use the
1662 * proper way to clear the error queue state.
1663 */
1664 # if OPENSSL_VERSION_NUMBER < 0x10100000L
1665 ERR_remove_thread_state(NULL);
1666 # endif /* prior to OpenSSL-1.1.x */
1667 #else
1668 ERR_remove_state(0);
1669 #endif /* OpenSSL prior to 1.0.0-beta1 */
1670
1671 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1672 ERR_free_strings();
1673 EVP_cleanup();
1674 RAND_cleanup();
1675 #endif /* prior to OpenSSL-1.1.x */
1676 }
1677 }
1678
1679 int sftp_crypto_set_driver(const char *driver) {
1680 #if OPENSSL_VERSION_NUMBER > 0x000907000L && \
1681 defined(PR_USE_OPENSSL_ENGINE)
1682 if (driver == NULL) {
1683 errno = EINVAL;
1684 return -1;
1685 }
1686
1687 crypto_engine = driver;
1688
1689 if (strcasecmp(driver, "ALL") == 0) {
1690 /* Load all ENGINE implementations bundled with OpenSSL. */
1691 ENGINE_load_builtin_engines();
1692 ENGINE_register_all_complete();
1693
1694 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1695 "enabled all builtin crypto devices");
1696
1697 } else {
1698 ENGINE *e;
1699
1700 /* Load all ENGINE implementations bundled with OpenSSL. */
1701 ENGINE_load_builtin_engines();
1702
1703 e = ENGINE_by_id(driver);
1704 if (e != NULL) {
1705 if (ENGINE_init(e)) {
1706 if (ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
1707 ENGINE_finish(e);
1708 ENGINE_free(e);
1709
1710 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1711 "using SFTPCryptoDevice '%s'", driver);
1712
1713 } else {
1714 /* The requested driver could not be used as the default for
1715 * some odd reason.
1716 */
1717 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1718 "unable to register SFTPCryptoDevice '%s' as the default: %s",
1719 driver, sftp_crypto_get_errors());
1720
1721 ENGINE_finish(e);
1722 ENGINE_free(e);
1723 e = NULL;
1724 crypto_engine = NULL;
1725
1726 errno = EPERM;
1727 return -1;
1728 }
1729
1730 } else {
1731 /* The requested driver could not be initialized. */
1732 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1733 "unable to initialize SFTPCryptoDevice '%s': %s", driver,
1734 sftp_crypto_get_errors());
1735
1736 ENGINE_free(e);
1737 e = NULL;
1738 crypto_engine = NULL;
1739
1740 errno = EPERM;
1741 return -1;
1742 }
1743
1744 } else {
1745 /* The requested driver is not available. */
1746 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1747 "SFTPCryptoDevice '%s' is not available", driver);
1748
1749 crypto_engine = NULL;
1750
1751 errno = EPERM;
1752 return -1;
1753 }
1754 }
1755
1756 return 0;
1757 #else
1758 errno = ENOSYS;
1759 return -1;
1760 #endif
1761 }
1762