Debian Codegraph @

contrib/mod_sftp/mac.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp MACs
3 * Copyright (c) 2008-2024 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_sftp.h"
26 #include "ssh2.h"
27 #include "msg.h"
28 #include "packet.h"
29 #include "crypto.h"
30 #include "cipher.h"
31 #include "mac.h"
32 #include "session.h"
33 #include "disconnect.h"
34 #include "interop.h"
35 #include "umac.h"
36
37 struct sftp_mac {
38 pool *pool;
39 const char *algo;
40 unsigned int algo_type;
41 int is_etm;
42 int free_digest;
43
44 const EVP_MD *digest;
45
46 unsigned char *key;
47
48 /* The keysz and key_len are usually the same; they can differ if, for
49 * example, the client always truncates the MAC key len to 16 bits.
50 */
51 size_t keysz;
52 uint32_t key_len;
53
54 uint32_t mac_len;
55 };
56
57 #define SFTP_MAC_ALGO_TYPE_NONE 1
58 #define SFTP_MAC_ALGO_TYPE_HMAC 2
59 #define SFTP_MAC_ALGO_TYPE_UMAC64 3
60 #define SFTP_MAC_ALGO_TYPE_UMAC128 4
61
62 #define SFTP_MAC_FL_READ_MAC 1
63 #define SFTP_MAC_FL_WRITE_MAC 2
64
65 /* We need to keep the old MACs around, so that we can handle N arbitrary
66 * packets to/from the client using the old keys, as during rekeying.
67 * Thus we have two read MAC contexts, two write MAC contexts.
68 * The cipher idx variable indicates which of the MACs is currently in use.
69 */
70
71 static struct sftp_mac read_macs[] = {
72 { NULL, NULL, 0, FALSE, FALSE, NULL, NULL, 0, 0, 0 },
73 { NULL, NULL, 0, FALSE, FALSE, NULL, NULL, 0, 0, 0 }
74 };
75 static HMAC_CTX *hmac_read_ctxs[2];
76 static struct umac_ctx *umac_read_ctxs[2];
77
78 static struct sftp_mac write_macs[] = {
79 { NULL, NULL, 0, FALSE, FALSE, NULL, NULL, 0, 0, 0 },
80 { NULL, NULL, 0, FALSE, FALSE, NULL, NULL, 0, 0, 0 }
81 };
82 static HMAC_CTX *hmac_write_ctxs[2];
83 static struct umac_ctx *umac_write_ctxs[2];
84
85 static size_t mac_blockszs[2] = { 0, 0 };
86
87 static unsigned int read_mac_idx = 0;
88 static unsigned int write_mac_idx = 0;
89
90 static void clear_mac(struct sftp_mac *);
91
92 static unsigned int get_next_read_index(void) {
93 if (read_mac_idx == 1) {
94 return 0;
95 }
96
97 return 1;
98 }
99
100 static unsigned int get_next_write_index(void) {
101 if (write_mac_idx == 1) {
102 return 0;
103 }
104
105 return 1;
106 }
107
108 static void switch_read_mac(void) {
109 /* First we can clear the read MAC, kept from rekeying. */
110 if (read_macs[read_mac_idx].key != NULL) {
111 clear_mac(&(read_macs[read_mac_idx]));
112 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
113 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
114 HMAC_CTX_reset(hmac_read_ctxs[read_mac_idx]);
115 #elif OPENSSL_VERSION_NUMBER > 0x000907000L
116 HMAC_CTX_cleanup(hmac_read_ctxs[read_mac_idx]);
117 #else
118 HMAC_cleanup(hmac_read_ctxs[read_mac_idx]);
119 #endif
120 if (read_macs[read_mac_idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC64) {
121 umac_reset(umac_read_ctxs[read_mac_idx]);
122
123 } else if (read_macs[read_mac_idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
124 umac128_reset(umac_read_ctxs[read_mac_idx]);
125 }
126
127 mac_blockszs[read_mac_idx] = 0;
128
129 /* Now we can switch the index. */
130 if (read_mac_idx == 1) {
131 read_mac_idx = 0;
132 return;
133 }
134
135 read_mac_idx = 1;
136 }
137 }
138
139 static void switch_write_mac(void) {
140 /* First we can clear the write MAC, kept from rekeying. */
141 if (write_macs[write_mac_idx].key != NULL) {
142 clear_mac(&(write_macs[write_mac_idx]));
143 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
144 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
145 HMAC_CTX_reset(hmac_write_ctxs[write_mac_idx]);
146 #elif OPENSSL_VERSION_NUMBER > 0x000907000L
147 HMAC_CTX_cleanup(hmac_write_ctxs[write_mac_idx]);
148 #else
149 HMAC_cleanup(hmac_write_ctxs[write_mac_idx]);
150 #endif
151 if (write_macs[write_mac_idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC64) {
152 umac_reset(umac_write_ctxs[write_mac_idx]);
153
154 } else if (write_macs[write_mac_idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
155 umac128_reset(umac_write_ctxs[write_mac_idx]);
156 }
157
158 /* Now we can switch the index. */
159 if (write_mac_idx == 1) {
160 write_mac_idx = 0;
161 return;
162 }
163
164 write_mac_idx = 1;
165 }
166 }
167
168 static void clear_mac(struct sftp_mac *mac) {
169 if (mac->key) {
170 pr_memscrub(mac->key, mac->keysz);
171 free(mac->key);
172 mac->key = NULL;
173 mac->keysz = 0;
174 mac->key_len = 0;
175 }
176
177 mac->digest = NULL;
178 mac->algo = NULL;
179 }
180
181 static int init_mac(pool *p, struct sftp_mac *mac, HMAC_CTX *hmac_ctx,
182 struct umac_ctx *umac_ctx) {
183 if (mac->algo_type == SFTP_MAC_ALGO_TYPE_NONE) {
184 return 0;
185 }
186
187 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
188 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
189 HMAC_CTX_reset(hmac_ctx);
190 #elif OPENSSL_VERSION_NUMBER > 0x000907000L
191 HMAC_CTX_init(hmac_ctx);
192 #else
193 /* Reset the HMAC context. */
194 HMAC_Init(hmac_ctx, NULL, 0, NULL);
195 #endif
196
197 if (mac->algo_type == SFTP_MAC_ALGO_TYPE_HMAC) {
198 #if OPENSSL_VERSION_NUMBER > 0x000907000L
199 # if OPENSSL_VERSION_NUMBER >= 0x10000001L
200 if (HMAC_Init_ex(hmac_ctx, mac->key, mac->key_len, mac->digest,
201 NULL) != 1) {
202 pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
203 "error initializing HMAC: %s", sftp_crypto_get_errors());
204 errno = EPERM;
205 return -1;
206 }
207
208 # else
209 HMAC_Init_ex(hmac_ctx, mac->key, mac->key_len, mac->digest, NULL);
210 # endif /* OpenSSL-1.0.0 and later */
211
212 #else
213 HMAC_Init(hmac_ctx, mac->key, mac->key_len, mac->digest);
214 #endif
215
216 } else if (mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC64) {
217 umac_reset(umac_ctx);
218 umac_init(umac_ctx, mac->key);
219
220 } else if (mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
221 umac128_reset(umac_ctx);
222 umac128_init(umac_ctx, mac->key);
223 }
224
225 return 0;
226 }
227
228 static unsigned int get_algo_type(const char *algo) {
229 unsigned int algo_type = 0;
230
231 if (strcmp(algo, "none") == 0) {
232 algo_type = SFTP_MAC_ALGO_TYPE_NONE;
233
234 } else if (strcmp(algo, "[email protected]") == 0 ||
235 strcmp(algo, "[email protected]") == 0) {
236 algo_type = SFTP_MAC_ALGO_TYPE_UMAC64;
237
238 } else if (strcmp(algo, "[email protected]") == 0 ||
239 strcmp(algo, "[email protected]") == 0) {
240 algo_type = SFTP_MAC_ALGO_TYPE_UMAC128;
241
242 } else {
243 algo_type = SFTP_MAC_ALGO_TYPE_HMAC;
244 }
245
246 return algo_type;
247 }
248
249 static int get_mac(struct ssh2_packet *pkt, struct sftp_mac *mac,
250 HMAC_CTX *hmac_ctx, struct umac_ctx *umac_ctx, int etm_mac, int flags) {
251 unsigned char *mac_data;
252 unsigned char *buf, *ptr;
253 uint32_t buflen, bufsz = 0, mac_len = 0;
254
255 if (mac->algo_type == SFTP_MAC_ALGO_TYPE_HMAC) {
256 /* Always leave a little extra room in the buffer. */
257 bufsz = (sizeof(uint32_t) * 2) + pkt->packet_len + 64;
258 mac_data = palloc(pkt->pool, EVP_MAX_MD_SIZE);
259
260 if (etm_mac == TRUE) {
261 bufsz += sftp_mac_get_block_size();
262 }
263
264 buflen = bufsz;
265 ptr = buf = palloc(pkt->pool, bufsz);
266
267 sftp_msg_write_int(&buf, &buflen, pkt->seqno);
268 sftp_msg_write_int(&buf, &buflen, pkt->packet_len);
269
270 if (etm_mac == FALSE) {
271 /* For Encrypt-Then-Mac modes, padding and its length will be part of
272 * the encrypted payload.
273 */
274 sftp_msg_write_byte(&buf, &buflen, pkt->padding_len);
275 }
276
277 sftp_msg_write_data(&buf, &buflen, pkt->payload, pkt->payload_len, FALSE);
278
279 if (etm_mac == FALSE) {
280 sftp_msg_write_data(&buf, &buflen, pkt->padding, pkt->padding_len, FALSE);
281 }
282
283 #if OPENSSL_VERSION_NUMBER > 0x000907000L
284 # if OPENSSL_VERSION_NUMBER >= 0x10000001L
285 if (HMAC_Init_ex(hmac_ctx, NULL, 0, NULL, NULL) != 1) {
286 pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
287 "error resetting HMAC context: %s", sftp_crypto_get_errors());
288 errno = EPERM;
289 return -1;
290 }
291 # else
292 HMAC_Init_ex(hmac_ctx, NULL, 0, NULL, NULL);
293 # endif /* OpenSSL-1.0.0 and later */
294
295 #else
296 HMAC_Init(hmac_ctx, NULL, 0, NULL);
297 #endif /* OpenSSL-0.9.7 and later */
298
299 #if OPENSSL_VERSION_NUMBER >= 0x10000001L
300 if (HMAC_Update(hmac_ctx, ptr, (bufsz - buflen)) != 1) {
301 pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
302 "error adding %lu bytes of data to HMAC context: %s",
303 (unsigned long) (bufsz - buflen), sftp_crypto_get_errors());
304 errno = EPERM;
305 return -1;
306 }
307
308 if (HMAC_Final(hmac_ctx, mac_data, &mac_len) != 1) {
309 pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
310 "error finalizing HMAC context: %s", sftp_crypto_get_errors());
311 errno = EPERM;
312 return -1;
313 }
314 #else
315 HMAC_Update(hmac_ctx, ptr, (bufsz - buflen));
316 HMAC_Final(hmac_ctx, mac_data, &mac_len);
317 #endif /* OpenSSL-1.0.0 and later */
318
319 } else if (mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC64 ||
320 mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
321 unsigned char nonce[8], *nonce_ptr;
322 uint32_t nonce_len = 0;
323
324 /* Always leave a little extra room in the buffer. */
325 bufsz = sizeof(uint32_t) + pkt->packet_len + 64;
326 mac_data = palloc(pkt->pool, EVP_MAX_MD_SIZE);
327
328 if (etm_mac == TRUE) {
329 bufsz += sftp_mac_get_block_size();
330 }
331
332 buflen = bufsz;
333 ptr = buf = palloc(pkt->pool, bufsz);
334
335 sftp_msg_write_int(&buf, &buflen, pkt->packet_len);
336
337 if (etm_mac == FALSE) {
338 /* For Encrypt-Then-Mac modes, padding and its length will be part of
339 * the encrypted payload.
340 */
341 sftp_msg_write_byte(&buf, &buflen, pkt->padding_len);
342 }
343
344 sftp_msg_write_data(&buf, &buflen, pkt->payload, pkt->payload_len, FALSE);
345
346 if (etm_mac == FALSE) {
347 sftp_msg_write_data(&buf, &buflen, pkt->padding, pkt->padding_len, FALSE);
348 }
349
350 nonce_ptr = nonce;
351 nonce_len = sizeof(nonce);
352 sftp_msg_write_long(&nonce_ptr, &nonce_len, pkt->seqno);
353
354 if (mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC64) {
355 umac_reset(umac_ctx);
356 umac_update(umac_ctx, ptr, (bufsz - buflen));
357 umac_final(umac_ctx, mac_data, nonce);
358 mac_len = 8;
359
360 } else if (mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
361 umac128_reset(umac_ctx);
362 umac128_update(umac_ctx, ptr, (bufsz - buflen));
363 umac128_final(umac_ctx, mac_data, nonce);
364 mac_len = 16;
365 }
366 }
367
368 if (mac_len == 0) {
369 pkt->mac = NULL;
370 pkt->mac_len = 0;
371
372 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
373 "error computing MAC using %s: %s", mac->algo,
374 sftp_crypto_get_errors());
375
376 errno = EIO;
377 return -1;
378 }
379
380 if (mac->mac_len != 0) {
381 mac_len = mac->mac_len;
382 }
383
384 if (flags & SFTP_MAC_FL_READ_MAC) {
385 if (memcmp(mac_data, pkt->mac, mac_len) != 0) {
386 unsigned int i = 0;
387
388 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
389 "MAC from client differs from expected MAC using %s", mac->algo);
390
391 #ifdef SFTP_DEBUG_PACKET
392 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
393 "client MAC (len %lu):", (unsigned long) pkt->mac_len);
394 for (i = 0; i < mac_len;) {
395 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
396 " %02x%02x %02x%02x %02x%02x %02x%02x",
397 ((unsigned char *) pkt->mac)[i], ((unsigned char *) pkt->mac)[i+1],
398 ((unsigned char *) pkt->mac)[i+2], ((unsigned char *) pkt->mac)[i+3],
399 ((unsigned char *) pkt->mac)[i+4], ((unsigned char *) pkt->mac)[i+5],
400 ((unsigned char *) pkt->mac)[i+6], ((unsigned char *) pkt->mac)[i+7]);
401 i += 8;
402 }
403
404 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
405 "server MAC (len %lu):", (unsigned long) mac_len);
406 for (i = 0; i < mac_len;) {
407 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
408 " %02x%02x %02x%02x %02x%02x %02x%02x",
409 ((unsigned char *) mac)[i], ((unsigned char *) mac)[i+1],
410 ((unsigned char *) mac)[i+2], ((unsigned char *) mac)[i+3],
411 ((unsigned char *) mac)[i+4], ((unsigned char *) mac)[i+5],
412 ((unsigned char *) mac)[i+6], ((unsigned char *) mac)[i+7]);
413 i += 8;
414 }
415 #else
416 /* Avoid compiler warning. */
417 (void) i;
418 #endif
419
420 errno = EINVAL;
421 return -1;
422 }
423
424 } else if (flags & SFTP_MAC_FL_WRITE_MAC) {
425 /* Debugging. */
426 #ifdef SFTP_DEBUG_PACKET
427 if (pkt->mac_len > 0) {
428 unsigned int i = 0;
429
430 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
431 "server MAC (len %lu, seqno %lu):",
432 (unsigned long) pkt->mac_len, (unsigned long) pkt->seqno);
433 for (i = 0; i < mac_len;) {
434 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
435 " %02x%02x %02x%02x %02x%02x %02x%02x",
436 ((unsigned char *) pkt->mac)[i], ((unsigned char *) pkt->mac)[i+1],
437 ((unsigned char *) pkt->mac)[i+2], ((unsigned char *) pkt->mac)[i+3],
438 ((unsigned char *) pkt->mac)[i+4], ((unsigned char *) pkt->mac)[i+5],
439 ((unsigned char *) pkt->mac)[i+6], ((unsigned char *) pkt->mac)[i+7]);
440 i += 8;
441 }
442 }
443 #endif
444 }
445
446 pkt->mac_len = mac_len;
447 pkt->mac = pcalloc(pkt->pool, pkt->mac_len);
448 memcpy(pkt->mac, mac_data, mac_len);
449
450 return 0;
451 }
452
453 static int set_mac_key(struct sftp_mac *mac, const EVP_MD *hash,
454 const unsigned char *k, uint32_t klen, const char *h, uint32_t hlen,
455 char letter, const unsigned char *id, uint32_t id_len) {
456 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
457 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
458 EVP_MD_CTX ctx;
459 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
460 EVP_MD_CTX *pctx;
461 unsigned char *key = NULL;
462 size_t key_sz;
463 uint32_t key_len = 0;
464
465 key_sz = sftp_crypto_get_size(EVP_MD_block_size(mac->digest),
466 EVP_MD_size(hash));
467 if (key_sz == 0) {
468 if (mac->algo_type == SFTP_MAC_ALGO_TYPE_NONE) {
469 return 0;
470 }
471
472 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
473 "unable to determine key length for MAC '%s'", mac->algo);
474 errno = EINVAL;
475 return -1;
476 }
477
478 key = malloc(key_sz);
479 if (key == NULL) {
480 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
481 _exit(1);
482 }
483
484 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
485 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
486 pctx = &ctx;
487 #else
488 pctx = EVP_MD_CTX_new();
489 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
490
491 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
492 * int. Without these ugly OpenSSL version preprocessor checks, the
493 * compiler will error out with "void value not ignored as it ought to be".
494 */
495
496 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
497 if (EVP_DigestInit(pctx, hash) != 1) {
498 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
499 "error initializing message digest: %s", sftp_crypto_get_errors());
500 free(key);
501 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
502 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
503 EVP_MD_CTX_free(pctx);
504 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
505 return -1;
506 }
507 #else
508 EVP_DigestInit(pctx, hash);
509 #endif
510
511 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
512 if (EVP_DigestUpdate(pctx, k, klen) != 1) {
513 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
514 "error updating message digest with K: %s", sftp_crypto_get_errors());
515 free(key);
516 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
517 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
518 EVP_MD_CTX_free(pctx);
519 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
520 return -1;
521 }
522 #else
523 EVP_DigestUpdate(pctx, k, klen);
524 #endif
525
526 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
527 if (EVP_DigestUpdate(pctx, h, hlen) != 1) {
528 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
529 "error updating message digest with H: %s", sftp_crypto_get_errors());
530 free(key);
531 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
532 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
533 EVP_MD_CTX_free(pctx);
534 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
535 return -1;
536 }
537 #else
538 EVP_DigestUpdate(pctx, h, hlen);
539 #endif
540
541 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
542 if (EVP_DigestUpdate(pctx, &letter, sizeof(letter)) != 1) {
543 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
544 "error updating message digest with '%c': %s", letter,
545 sftp_crypto_get_errors());
546 free(key);
547 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
548 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
549 EVP_MD_CTX_free(pctx);
550 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
551 return -1;
552 }
553 #else
554 EVP_DigestUpdate(pctx, &letter, sizeof(letter));
555 #endif
556
557 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
558 if (EVP_DigestUpdate(pctx, (char *) id, id_len) != 1) {
559 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
560 "error updating message digest with ID: %s", sftp_crypto_get_errors());
561 free(key);
562 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
563 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
564 EVP_MD_CTX_free(pctx);
565 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
566 return -1;
567 }
568 #else
569 EVP_DigestUpdate(pctx, (char *) id, id_len);
570 #endif
571
572 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
573 if (EVP_DigestFinal(pctx, key, &key_len) != 1) {
574 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
575 "error finalizing message digest: %s", sftp_crypto_get_errors());
576 pr_memscrub(key, key_sz);
577 free(key);
578 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
579 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
580 EVP_MD_CTX_free(pctx);
581 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
582 return -1;
583 }
584 #else
585 EVP_DigestFinal(pctx, key, &key_len);
586 #endif
587
588 /* If we need more, keep hashing, as per RFC, until we have enough
589 * material.
590 */
591
592 while (key_sz > key_len) {
593 uint32_t len = key_len;
594
595 pr_signals_handle();
596
597 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
598 if (EVP_DigestInit(pctx, hash) != 1) {
599 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
600 "error initializing message digest: %s", sftp_crypto_get_errors());
601 pr_memscrub(key, key_sz);
602 free(key);
603 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
604 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
605 EVP_MD_CTX_free(pctx);
606 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
607 return -1;
608 }
609 #else
610 EVP_DigestInit(pctx, hash);
611 #endif
612
613 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
614 if (EVP_DigestUpdate(pctx, k, klen) != 1) {
615 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
616 "error updating message digest with K: %s", sftp_crypto_get_errors());
617 pr_memscrub(key, key_sz);
618 free(key);
619 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
620 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
621 EVP_MD_CTX_free(pctx);
622 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
623 return -1;
624 }
625 #else
626 EVP_DigestUpdate(pctx, k, klen);
627 #endif
628
629 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
630 if (EVP_DigestUpdate(pctx, h, hlen) != 1) {
631 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
632 "error updating message digest with H: %s", sftp_crypto_get_errors());
633 pr_memscrub(key, key_sz);
634 free(key);
635 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
636 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
637 EVP_MD_CTX_free(pctx);
638 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
639 return -1;
640 }
641 #else
642 EVP_DigestUpdate(pctx, h, hlen);
643 #endif
644
645 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
646 if (EVP_DigestUpdate(pctx, key, len) != 1) {
647 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
648 "error updating message digest with data: %s",
649 sftp_crypto_get_errors());
650 pr_memscrub(key, key_sz);
651 free(key);
652 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
653 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
654 EVP_MD_CTX_free(pctx);
655 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
656 return -1;
657 }
658 #else
659 EVP_DigestUpdate(pctx, key, len);
660 #endif
661
662 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
663 if (EVP_DigestFinal(pctx, key + len, &len) != 1) {
664 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
665 "error finalizing message digest: %s", sftp_crypto_get_errors());
666 pr_memscrub(key, key_sz);
667 free(key);
668 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
669 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
670 EVP_MD_CTX_free(pctx);
671 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
672 return -1;
673 }
674 #else
675 EVP_DigestFinal(pctx, key + len, &len);
676 #endif
677
678 key_len += len;
679 }
680
681 mac->key = key;
682 mac->keysz = key_sz;
683
684 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
685 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
686 EVP_MD_CTX_free(pctx);
687 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
688
689 if (mac->algo_type == SFTP_MAC_ALGO_TYPE_HMAC) {
690 mac->key_len = EVP_MD_size(mac->digest);
691
692 } else if (mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC64 ||
693 mac->algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
694 mac->key_len = EVP_MD_block_size(mac->digest);
695 }
696
697 if (!sftp_interop_supports_feature(SFTP_SSH2_FEAT_MAC_LEN)) {
698 mac->key_len = 16;
699 }
700
701 return 0;
702 }
703
704 size_t sftp_mac_get_block_size(void) {
705 return mac_blockszs[read_mac_idx];
706 }
707
708 void sftp_mac_set_block_size(size_t blocksz) {
709 if (blocksz > mac_blockszs[read_mac_idx]) {
710 mac_blockszs[read_mac_idx] = blocksz;
711 }
712 }
713
714 const char *sftp_mac_get_read_algo(void) {
715 if (read_macs[read_mac_idx].key != NULL ||
716 (read_macs[read_mac_idx].algo != NULL &&
717 read_macs[read_mac_idx].algo_type == SFTP_MAC_ALGO_TYPE_NONE)) {
718 return read_macs[read_mac_idx].algo;
719 }
720
721 /* It is possible for there to be no MAC, as for some ciphers such as
722 * AES-GCM. Rather than returning NULL here, we indicate this by returning
723 * a string (see Issue #1411).
724 */
725 return "implicit";
726 }
727
728 int sftp_mac_is_read_etm(void) {
729 if (read_macs[read_mac_idx].key != NULL) {
730 return read_macs[read_mac_idx].is_etm;
731 }
732
733 return FALSE;
734 }
735
736 int sftp_mac_set_read_algo(const char *algo) {
737 const char *etm_suffix;
738 size_t algo_len, etm_len;
739 uint32_t mac_len;
740 unsigned int idx = read_mac_idx;
741
742 /* For authenticated encryption ciphers, there is no separate MAC. */
743 if (sftp_cipher_get_read_auth_size() > 0) {
744 return 0;
745 }
746
747 if (read_macs[idx].key != NULL) {
748 /* If we have an existing key, it means that we are currently rekeying. */
749 idx = get_next_read_index();
750 }
751
752 /* Clear any potential UMAC contexts at this index. */
753 if (umac_read_ctxs[idx] != NULL) {
754 switch (read_macs[idx].algo_type) {
755 case SFTP_MAC_ALGO_TYPE_UMAC64:
756 umac_delete(umac_read_ctxs[idx]);
757 umac_read_ctxs[idx] = NULL;
758 if (read_macs[idx].free_digest == TRUE) {
759 sftp_crypto_free_digest(read_macs[idx].digest);
760 read_macs[idx].digest = NULL;
761 }
762 break;
763
764 case SFTP_MAC_ALGO_TYPE_UMAC128:
765 umac128_delete(umac_read_ctxs[idx]);
766 umac_read_ctxs[idx] = NULL;
767 if (read_macs[idx].free_digest == TRUE) {
768 sftp_crypto_free_digest(read_macs[idx].digest);
769 read_macs[idx].digest = NULL;
770 }
771 break;
772 }
773 }
774
775 read_macs[idx].digest = sftp_crypto_get_digest(algo, &mac_len,
776 &(read_macs[idx].free_digest));
777 if (read_macs[idx].digest == NULL) {
778 return -1;
779 }
780
781 /* Note that we use a new pool, each time the algorithm is set (which
782 * happens during key exchange) to prevent undue memory growth for
783 * long-lived sessions with many rekeys.
784 */
785 if (read_macs[idx].pool != NULL) {
786 destroy_pool(read_macs[idx].pool);
787 }
788
789 read_macs[idx].pool = make_sub_pool(sftp_pool);
790 pr_pool_tag(read_macs[idx].pool, "SFTP MAC read pool");
791 read_macs[idx].algo = pstrdup(read_macs[idx].pool, algo);
792 read_macs[idx].algo_type = get_algo_type(algo);
793
794 if (read_macs[idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC64) {
795 umac_read_ctxs[idx] = umac_alloc();
796
797 } else if (read_macs[idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
798 umac_read_ctxs[idx] = umac128_alloc();
799 }
800
801 read_macs[idx].mac_len = mac_len;
802
803 algo_len = strlen(algo);
804 etm_suffix = "[email protected]";
805 etm_len = strlen(etm_suffix);
806
807 if (pr_strnrstr(algo, algo_len, etm_suffix, etm_len, 0) == TRUE) {
808 read_macs[idx].is_etm = TRUE;
809 }
810
811 return 0;
812 }
813
814 int sftp_mac_set_read_key(pool *p, const EVP_MD *hash, const unsigned char *k,
815 uint32_t klen, const char *h, uint32_t hlen, int role) {
816 const unsigned char *id = NULL;
817 uint32_t id_len;
818 char letter;
819 size_t blocksz;
820 struct sftp_mac *mac;
821 HMAC_CTX *hmac_ctx;
822 struct umac_ctx *umac_ctx;
823
824 /* For authenticated encryption ciphers, there is no separate MAC. */
825 if (sftp_cipher_get_read_auth_size() > 0) {
826 return 0;
827 }
828
829 switch_read_mac();
830
831 mac = &(read_macs[read_mac_idx]);
832 hmac_ctx = hmac_read_ctxs[read_mac_idx];
833 umac_ctx = umac_read_ctxs[read_mac_idx];
834
835 id_len = sftp_session_get_id(&id);
836
837 /* The letters used depend on the role; see:
838 * https://tools.ietf.org/html/rfc4253#section-7.2
839 *
840 * If we are the SERVER, then we use the letters for the "client to server"
841 * flows, since we are READING from the client.
842 */
843
844 /* client-to-server HASH(K || H || "E" || session_id)
845 * server-to-client HASH(K || H || "F" || session_id)
846 */
847 letter = (role == SFTP_ROLE_SERVER ? 'E' : 'F');
848 set_mac_key(mac, hash, k, klen, h, hlen, letter, id, id_len);
849
850 if (init_mac(p, mac, hmac_ctx, umac_ctx) < 0) {
851 return -1;
852 }
853
854 if (mac->mac_len == 0) {
855 blocksz = EVP_MD_size(mac->digest);
856
857 } else {
858 blocksz = mac->mac_len;
859 }
860
861 sftp_mac_set_block_size(blocksz);
862 return 0;
863 }
864
865 int sftp_mac_read_data(struct ssh2_packet *pkt) {
866 struct sftp_mac *mac;
867 HMAC_CTX *hmac_ctx;
868 struct umac_ctx *umac_ctx;
869 int res, etm_mac = FALSE;
870
871 /* For authenticated encryption ciphers, there is no separate MAC. */
872 if (sftp_cipher_get_read_auth_size() > 0) {
873 return 0;
874 }
875
876 etm_mac = sftp_mac_is_read_etm();
877
878 mac = &(read_macs[read_mac_idx]);
879 hmac_ctx = hmac_read_ctxs[read_mac_idx];
880 umac_ctx = umac_read_ctxs[read_mac_idx];
881
882 if (mac->key == NULL) {
883 pkt->mac = NULL;
884 pkt->mac_len = 0;
885
886 return 0;
887 }
888
889 res = get_mac(pkt, mac, hmac_ctx, umac_ctx, etm_mac, SFTP_MAC_FL_READ_MAC);
890 if (res < 0) {
891 return -1;
892 }
893
894 return 0;
895 }
896
897 const char *sftp_mac_get_write_algo(void) {
898 if (write_macs[write_mac_idx].key != NULL ||
899 (write_macs[write_mac_idx].algo != NULL &&
900 write_macs[write_mac_idx].algo_type == SFTP_MAC_ALGO_TYPE_NONE)) {
901 return write_macs[write_mac_idx].algo;
902 }
903
904 /* It is possible for there to be no MAC, as for some ciphers such as
905 * AES-GCM. Rather than returning NULL here, we indicate this by returning
906 * a string (see Issue #1411).
907 */
908 return "implicit";
909 }
910
911 int sftp_mac_is_write_etm(void) {
912 if (write_macs[write_mac_idx].key != NULL) {
913 return write_macs[write_mac_idx].is_etm;
914 }
915
916 return FALSE;
917 }
918
919 int sftp_mac_set_write_algo(const char *algo) {
920 const char *etm_suffix;
921 size_t algo_len, etm_len;
922 uint32_t mac_len;
923 unsigned int idx = write_mac_idx;
924
925 /* For authenticated encryption ciphers, there is no separate MAC. */
926 if (sftp_cipher_get_write_auth_size() > 0) {
927 return 0;
928 }
929
930 if (write_macs[idx].key != NULL) {
931 /* If we have an existing key, it means that we are currently rekeying. */
932 idx = get_next_write_index();
933 }
934
935 /* Clear any potential UMAC contexts at this index. */
936 if (umac_write_ctxs[idx] != NULL) {
937 switch (write_macs[idx].algo_type) {
938 case SFTP_MAC_ALGO_TYPE_UMAC64:
939 umac_delete(umac_write_ctxs[idx]);
940 umac_write_ctxs[idx] = NULL;
941 if (write_macs[idx].free_digest == TRUE) {
942 sftp_crypto_free_digest(write_macs[idx].digest);
943 write_macs[idx].digest = NULL;
944 }
945 break;
946
947 case SFTP_MAC_ALGO_TYPE_UMAC128:
948 umac128_delete(umac_write_ctxs[idx]);
949 umac_write_ctxs[idx] = NULL;
950 if (write_macs[idx].free_digest == TRUE) {
951 sftp_crypto_free_digest(write_macs[idx].digest);
952 write_macs[idx].digest = NULL;
953 }
954 break;
955 }
956 }
957
958 write_macs[idx].digest = sftp_crypto_get_digest(algo, &mac_len,
959 &(write_macs[idx].free_digest));
960 if (write_macs[idx].digest == NULL) {
961 return -1;
962 }
963
964 /* Note that we use a new pool, each time the algorithm is set (which
965 * happens during key exchange) to prevent undue memory growth for
966 * long-lived sessions with many rekeys.
967 */
968 if (write_macs[idx].pool != NULL) {
969 destroy_pool(write_macs[idx].pool);
970 }
971
972 write_macs[idx].pool = make_sub_pool(sftp_pool);
973 pr_pool_tag(write_macs[idx].pool, "SFTP MAC write pool");
974 write_macs[idx].algo = pstrdup(write_macs[idx].pool, algo);
975 write_macs[idx].algo_type = get_algo_type(algo);
976
977 if (write_macs[idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC64) {
978 umac_write_ctxs[idx] = umac_alloc();
979
980 } else if (write_macs[idx].algo_type == SFTP_MAC_ALGO_TYPE_UMAC128) {
981 umac_write_ctxs[idx] = umac128_alloc();
982 }
983
984 write_macs[idx].mac_len = mac_len;
985
986 algo_len = strlen(algo);
987 etm_suffix = "[email protected]";
988 etm_len = strlen(etm_suffix);
989
990 if (pr_strnrstr(algo, algo_len, etm_suffix, etm_len, 0) == TRUE) {
991 write_macs[idx].is_etm = TRUE;
992 }
993
994 return 0;
995 }
996
997 int sftp_mac_set_write_key(pool *p, const EVP_MD *hash, const unsigned char *k,
998 uint32_t klen, const char *h, uint32_t hlen, int role) {
999 const unsigned char *id = NULL;
1000 uint32_t id_len;
1001 char letter;
1002 struct sftp_mac *mac;
1003 HMAC_CTX *hmac_ctx;
1004 struct umac_ctx *umac_ctx;
1005
1006 /* For authenticated encryption ciphers, there is no separate MAC. */
1007 if (sftp_cipher_get_write_auth_size() > 0) {
1008 return 0;
1009 }
1010
1011 switch_write_mac();
1012
1013 mac = &(write_macs[write_mac_idx]);
1014 hmac_ctx = hmac_write_ctxs[write_mac_idx];
1015 umac_ctx = umac_write_ctxs[write_mac_idx];
1016
1017 id_len = sftp_session_get_id(&id);
1018
1019 /* The letters used depend on the role; see:
1020 * https://tools.ietf.org/html/rfc4253#section-7.2
1021 *
1022 * If we are the SERVER, then we use the letters for the "server to client"
1023 * flows, since we are WRITING to the client.
1024 */
1025
1026 /* client-to-server HASH(K || H || "E" || session_id)
1027 * server-to-client HASH(K || H || "F" || session_id)
1028 */
1029 letter = (role == SFTP_ROLE_SERVER ? 'F' : 'E');
1030 set_mac_key(mac, hash, k, klen, h, hlen, letter, id, id_len);
1031
1032 if (init_mac(p, mac, hmac_ctx, umac_ctx) < 0) {
1033 return -1;
1034 }
1035
1036 return 0;
1037 }
1038
1039 int sftp_mac_write_data(struct ssh2_packet *pkt) {
1040 struct sftp_mac *mac;
1041 HMAC_CTX *hmac_ctx;
1042 struct umac_ctx *umac_ctx;
1043 int res, etm_mac = FALSE;
1044
1045 /* For authenticated encryption ciphers, there is no separate MAC. */
1046 if (sftp_cipher_get_write_auth_size() > 0) {
1047 return 0;
1048 }
1049
1050 etm_mac = sftp_mac_is_write_etm();
1051
1052 mac = &(write_macs[write_mac_idx]);
1053 hmac_ctx = hmac_write_ctxs[write_mac_idx];
1054 umac_ctx = umac_write_ctxs[write_mac_idx];
1055
1056 if (mac->key == NULL) {
1057 pkt->mac = NULL;
1058 pkt->mac_len = 0;
1059
1060 return 0;
1061 }
1062
1063 res = get_mac(pkt, mac, hmac_ctx, umac_ctx, etm_mac, SFTP_MAC_FL_WRITE_MAC);
1064 if (res < 0) {
1065 return -1;
1066 }
1067
1068 return 0;
1069 }
1070
1071 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1072 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
1073 /* In older versions of OpenSSL/LibreSSL, there was not a way to dynamically
1074 * allocate an HMAC_CTX object. Thus we have these static objects for those
1075 * older versions.
1076 */
1077 static HMAC_CTX read_ctx1, read_ctx2;
1078 static HMAC_CTX write_ctx1, write_ctx2;
1079 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1080
1081 int sftp_mac_init(void) {
1082 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
1083 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
1084 hmac_read_ctxs[0] = &read_ctx1;
1085 hmac_read_ctxs[1] = &read_ctx2;
1086 hmac_write_ctxs[0] = &write_ctx1;
1087 hmac_write_ctxs[1] = &write_ctx2;
1088 #else
1089 hmac_read_ctxs[0] = HMAC_CTX_new();
1090 hmac_read_ctxs[1] = HMAC_CTX_new();
1091 hmac_write_ctxs[0] = HMAC_CTX_new();
1092 hmac_write_ctxs[1] = HMAC_CTX_new();
1093 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
1094
1095 umac_read_ctxs[0] = NULL;
1096 umac_read_ctxs[1] = NULL;
1097 umac_write_ctxs[0] = NULL;
1098 umac_write_ctxs[1] = NULL;
1099
1100 return 0;
1101 }
1102
1103 int sftp_mac_free(void) {
1104 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1105 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1106 if (hmac_read_ctxs[0] != NULL) {
1107 HMAC_CTX_free(hmac_read_ctxs[0]);
1108 hmac_read_ctxs[0] = NULL;
1109 }
1110
1111 if (hmac_read_ctxs[1] != NULL) {
1112 HMAC_CTX_free(hmac_read_ctxs[1]);
1113 hmac_read_ctxs[1] = NULL;
1114 }
1115
1116 if (hmac_write_ctxs[0] != NULL) {
1117 HMAC_CTX_free(hmac_write_ctxs[0]);
1118 hmac_write_ctxs[0] = NULL;
1119 }
1120
1121 if (hmac_write_ctxs[1] != NULL) {
1122 HMAC_CTX_free(hmac_write_ctxs[1]);
1123 hmac_write_ctxs[1] = NULL;
1124 }
1125 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
1126 return 0;
1127 }