Debian Codegraph @

contrib/mod_sftp/rfc4716.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp RFC4716 keystore
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 "keys.h"
27 #include "keystore.h"
28 #include "crypto.h"
29 #include "rfc4716.h"
30
31 /* File-based keystore implementation */
32
33 struct filestore_key {
34 const char *subject;
35
36 /* Optional headers */
37 pr_table_t *headers;
38
39 /* Key data */
40 unsigned char *key_data;
41 uint32_t key_datalen;
42 };
43
44 struct filestore_data {
45 pr_fh_t *fh;
46 const char *path;
47 unsigned int lineno;
48 };
49
50 static const char *trace_channel = "ssh2";
51
52 /* This getline() function is quite similar to pr_fsio_getline(), except
53 * that it a) enforces the 72-byte max line length from RFC4716, and b)
54 * properly handles lines ending with CR, LF, or CRLF.
55 *
56 * Technically it allows one more byte than necessary, since the worst case
57 * is 74 bytes (72 + CRLF); this also means 73 + CR or 73 + LF. The extra
58 * byte is for the terminating NUL.
59 */
60 static char *filestore_getline(sftp_keystore_t *store, pool *p) {
61 char linebuf[75], *line = "", *res;
62 struct filestore_data *store_data = store->keystore_data;
63
64 while (TRUE) {
65 size_t linelen;
66
67 pr_signals_handle();
68
69 memset(&linebuf, '\0', sizeof(linebuf));
70 res = pr_fsio_gets(linebuf, sizeof(linebuf) - 1, store_data->fh);
71
72 if (res == NULL) {
73 if (errno == EINTR) {
74 continue;
75 }
76
77 pr_trace_msg(trace_channel, 10, "reached end of '%s', no matching "
78 "key found", store_data->path);
79 errno = EOF;
80 return NULL;
81 }
82
83 linelen = strlen(linebuf);
84 if (linelen >= 1) {
85
86 /* Check for the CRLF case first. If those are not present, then we
87 * check for either CR or LF. Doing the "CR or LF" check first prevents
88 * us from properly handling CRLFs, and thus lead to Issue #1904.
89 */
90
91 if (linelen >= 2 &&
92 linebuf[linelen - 2] == '\r' &&
93 linebuf[linelen - 1] == '\n') {
94 char *tmp;
95 unsigned int header_taglen, header_valuelen;
96 int have_line_continuation = FALSE;
97
98 store_data->lineno++;
99
100 linebuf[linelen - 2] = '\0';
101 linebuf[linelen - 1] = '\0';
102 line = pstrcat(p, line, linebuf, NULL);
103
104 if (line[strlen(line) - 1] == '\\') {
105 have_line_continuation = TRUE;
106 line[strlen(line) - 1] = '\0';
107 }
108
109 tmp = strchr(line, ':');
110 if (tmp == NULL) {
111 return line;
112 }
113
114 /* We have a header. Make sure the header tag is not longer than
115 * the specified length of 64 bytes, and that the header value is
116 * not longer than 1024 bytes.
117 */
118 header_taglen = tmp - line;
119 if (header_taglen > 64) {
120 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
121 "header tag too long (%u) on line %u of '%s'", header_taglen,
122 store_data->lineno, store_data->path);
123 errno = EINVAL;
124 return NULL;
125 }
126
127 /* Header value starts at 2 after the ':' (one for the mandatory
128 * space character.
129 */
130 header_valuelen = strlen(line) - (header_taglen + 2);
131 if (header_valuelen > 1024) {
132 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
133 "header value too long (%u) on line %u of '%s'", header_valuelen,
134 store_data->lineno, store_data->path);
135 errno = EINVAL;
136 return NULL;
137 }
138
139 if (!have_line_continuation) {
140 return line;
141 }
142
143 continue;
144
145 } else if (linebuf[linelen - 1] == '\r' ||
146 linebuf[linelen - 1] == '\n') {
147 char *tmp;
148 unsigned int header_taglen, header_valuelen;
149 int have_line_continuation = FALSE;
150
151 store_data->lineno++;
152
153 linebuf[linelen - 1] = '\0';
154 line = pstrcat(p, line, linebuf, NULL);
155
156 if (line[strlen(line) - 1] == '\\') {
157 have_line_continuation = TRUE;
158 line[strlen(line) - 1] = '\0';
159 }
160
161 tmp = strchr(line, ':');
162 if (tmp == NULL) {
163 return line;
164 }
165
166 /* We have a header. Make sure the header tag is not longer than
167 * the specified length of 64 bytes, and that the header value is
168 * not longer than 1024 bytes.
169 */
170 header_taglen = tmp - line;
171 if (header_taglen > 64) {
172 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
173 "header tag too long (%u) on line %u of '%s'", header_taglen,
174 store_data->lineno, store_data->path);
175 errno = EINVAL;
176 return NULL;
177 }
178
179 /* Header value starts at 2 after the ':' (one for the mandatory
180 * space character.
181 */
182 header_valuelen = strlen(line) - (header_taglen + 2);
183 if (header_valuelen > 1024) {
184 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
185 "header value too long (%u) on line %u of '%s'", header_valuelen,
186 store_data->lineno, store_data->path);
187 errno = EINVAL;
188 return NULL;
189 }
190
191 if (!have_line_continuation) {
192 return line;
193 }
194
195 continue;
196
197 } else if (linelen < sizeof(linebuf)) {
198 /* No CR or LF terminator; maybe a badly formatted file? Try to
199 * work with the data, if we can.
200 */
201 line = pstrcat(p, line, linebuf, NULL);
202 return line;
203
204 } else {
205 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
206 "line too long (%lu) on line %u of '%s'", (unsigned long) linelen,
207 store_data->lineno, store_data->path);
208 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
209 "Make sure that '%s' is a RFC4716 formatted key", store_data->path);
210 errno = EINVAL;
211 break;
212 }
213 }
214 }
215
216 return NULL;
217 }
218
219 static struct filestore_key *filestore_alloc_key(pool *p) {
220 struct filestore_key *key = NULL;
221
222 key = pcalloc(p, sizeof(struct filestore_key));
223 key->headers = pr_table_nalloc(p, 0, 1);
224
225 return key;
226 }
227
228 static struct filestore_key *filestore_get_key(sftp_keystore_t *store,
229 pool *p) {
230 char *line;
231 BIO *bio = NULL;
232 struct filestore_key *key = NULL;
233 struct filestore_data *store_data = store->keystore_data;
234 size_t begin_markerlen = 0, end_markerlen = 0;
235
236 line = filestore_getline(store, p);
237 while (line == NULL &&
238 errno == EINVAL) {
239 pr_signals_handle();
240
241 line = filestore_getline(store, p);
242 }
243
244 if (line == NULL) {
245 return NULL;
246 }
247
248 begin_markerlen = strlen(SFTP_SSH2_PUBKEY_BEGIN_MARKER);
249 end_markerlen = strlen(SFTP_SSH2_PUBKEY_END_MARKER);
250
251 while (line != NULL) {
252 pr_signals_handle();
253
254 if (key == NULL &&
255 strncmp(line, SFTP_SSH2_PUBKEY_BEGIN_MARKER,
256 begin_markerlen + 1) == 0) {
257 key = filestore_alloc_key(p);
258 bio = BIO_new(BIO_s_mem());
259
260 } else if (key != NULL &&
261 strncmp(line, SFTP_SSH2_PUBKEY_END_MARKER,
262 end_markerlen + 1) == 0) {
263 if (bio != NULL) {
264 BIO *b64 = NULL, *bmem = NULL;
265 char chunk[1024], *data = NULL;
266 int chunklen;
267 long datalen = 0;
268
269 /* Add a base64 filter BIO, and read the data out, thus base64-decoding
270 * the key. Write the decoded data into another memory BIO.
271 */
272 b64 = BIO_new(BIO_f_base64());
273 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
274 bio = BIO_push(b64, bio);
275
276 bmem = BIO_new(BIO_s_mem());
277
278 memset(chunk, '\0', sizeof(chunk));
279 chunklen = BIO_read(bio, chunk, sizeof(chunk));
280
281 if (chunklen < 0 &&
282 !BIO_should_retry(bio)) {
283 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
284 "unable to base64-decode data in '%s': %s",
285 store_data->path, sftp_crypto_get_errors());
286 BIO_free_all(bio);
287 BIO_free_all(bmem);
288
289 errno = EPERM;
290 return NULL;
291 }
292
293 while (chunklen > 0) {
294 pr_signals_handle();
295
296 if (BIO_write(bmem, chunk, chunklen) < 0) {
297 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
298 "error writing to memory BIO: %s", sftp_crypto_get_errors());
299 BIO_free_all(bio);
300 BIO_free_all(bmem);
301
302 errno = EPERM;
303 return NULL;
304 }
305
306 memset(chunk, '\0', sizeof(chunk));
307 chunklen = BIO_read(bio, chunk, sizeof(chunk));
308 }
309
310 datalen = BIO_get_mem_data(bmem, &data);
311
312 if (data != NULL &&
313 datalen > 0) {
314 key->key_data = palloc(p, datalen);
315 key->key_datalen = datalen;
316 memcpy(key->key_data, data, datalen);
317
318 } else {
319 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
320 "error base64-decoding key data in '%s'", store_data->path);
321 }
322
323 BIO_free_all(bio);
324 bio = NULL;
325
326 BIO_free_all(bmem);
327 }
328
329 break;
330
331 } else {
332 if (key != NULL) {
333 char *ptr;
334
335 ptr = strstr(line, ": ");
336 if (ptr != NULL) {
337 /* We have a header. */
338 char *tag, *text;
339
340 tag = pstrndup(p, line, ptr - line);
341 text = pstrdup(p, ptr + 2);
342 if (pr_table_add(key->headers, tag, text, 0) < 0) {
343 pr_trace_msg(trace_channel, 4,
344 "failed to add header '%s' to notes: %s", tag, strerror(errno));
345
346 } else {
347 pr_trace_msg(trace_channel, 22, "added header '%s: %s' to notes",
348 tag, text);
349 }
350
351 if (strcasecmp(tag, SFTP_KEYSTORE_HEADER_SUBJECT) == 0) {
352 key->subject = text;
353 }
354
355 } else {
356 if (BIO_write(bio, line, strlen(line)) < 0) {
357 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
358 "error buffering base64 data");
359 }
360 }
361 }
362 }
363
364 line = filestore_getline(store, p);
365 while (line == NULL &&
366 errno == EINVAL) {
367 pr_signals_handle();
368
369 line = filestore_getline(store, p);
370 }
371 }
372
373 return key;
374 }
375
376 static int filestore_verify_host_key(sftp_keystore_t *store, pool *p,
377 const char *user, const char *host_fqdn, const char *host_user,
378 unsigned char *key_data, uint32_t key_len, pr_table_t *headers) {
379 struct filestore_key *key = NULL;
380 struct filestore_data *store_data = store->keystore_data;
381 int res = -1;
382
383 if (store_data->path == NULL) {
384 errno = EPERM;
385 return -1;
386 }
387
388 /* XXX Note that this will scan the file from the beginning, each time.
389 * There's room for improvement; perhaps mmap() the file into memory?
390 */
391
392 key = filestore_get_key(store, p);
393 while (key != NULL) {
394 int ok;
395
396 pr_signals_handle();
397
398 ok = sftp_keys_compare_keys(p, key_data, key_len, key->key_data,
399 key->key_datalen);
400 if (ok != TRUE) {
401 if (ok == -1) {
402 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
403 "error comparing keys from '%s': %s", store_data->path,
404 strerror(errno));
405 }
406
407 } else {
408
409 /* XXX Verify that the user and the host_user match?? */
410
411 res = 0;
412 break;
413 }
414
415 key = filestore_get_key(store, p);
416 }
417
418 if (res == 0) {
419 pr_trace_msg(trace_channel, 10, "found matching public key for host '%s' "
420 "in '%s'", host_fqdn, store_data->path);
421 if (pr_table_copy(headers, key->headers, 0) < 0) {
422 pr_trace_msg(trace_channel, 19, "error copying verify notes: %s",
423 strerror(errno));
424 }
425 }
426
427 if (pr_fsio_lseek(store_data->fh, 0, SEEK_SET) < 0) {
428 int xerrno = errno;
429
430 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
431 "error seeking to start of '%s': %s", store_data->path, strerror(xerrno));
432
433 errno = xerrno;
434 return -1;
435 }
436
437 store_data->lineno = 0;
438 return res;
439 }
440
441 static int filestore_verify_user_key(sftp_keystore_t *store, pool *p,
442 const char *user, unsigned char *key_data, uint32_t key_len,
443 pr_table_t *headers) {
444 struct filestore_key *key = NULL;
445 struct filestore_data *store_data = store->keystore_data;
446 unsigned int count = 0;
447 int res = -1;
448
449 if (store_data->path == NULL) {
450 errno = EPERM;
451 return -1;
452 }
453
454 /* XXX Note that this will scan the file from the beginning, each time.
455 * There's room for improvement; perhaps mmap() the file into memory?
456 */
457
458 key = filestore_get_key(store, p);
459 while (key != NULL) {
460 int ok;
461
462 pr_signals_handle();
463 count++;
464
465 ok = sftp_keys_compare_keys(p, key_data, key_len, key->key_data,
466 key->key_datalen);
467 if (ok != TRUE) {
468 if (ok == -1) {
469 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
470 "error comparing keys from '%s': %s", store_data->path,
471 strerror(errno));
472
473 } else {
474 pr_trace_msg(trace_channel, 10,
475 "failed to match key #%u from file '%s'", count, store_data->path);
476 }
477
478 } else {
479 /* If we are configured to check for Subject headers, and if the file key
480 * has a Subject header, and that header value does not match the
481 * logging in user, then continue looking.
482 */
483 if ((sftp_opts & SFTP_OPT_MATCH_KEY_SUBJECT) &&
484 key->subject != NULL) {
485 if (strcmp(key->subject, user) != 0) {
486 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
487 "found matching key for user '%s' in '%s', but Subject "
488 "header ('%s') does not match, skipping key", user,
489 store_data->path, key->subject);
490
491 } else {
492 res = 0;
493 break;
494 }
495
496 } else {
497 res = 0;
498 break;
499 }
500 }
501
502 key = filestore_get_key(store, p);
503 }
504
505 if (res == 0) {
506 pr_trace_msg(trace_channel, 10, "found matching public key for user '%s' "
507 "in '%s'", user, store_data->path);
508 if (pr_table_copy(headers, key->headers, 0) < 0) {
509 pr_trace_msg(trace_channel, 19, "error copying verify notes: %s",
510 strerror(errno));
511 }
512 }
513
514 if (pr_fsio_lseek(store_data->fh, 0, SEEK_SET) < 0) {
515 int xerrno = errno;
516
517 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
518 "error seeking to start of '%s': %s", store_data->path, strerror(xerrno));
519
520 xerrno = errno;
521 return -1;
522 }
523
524 store_data->lineno = 0;
525 return res;
526 }
527
528 static int filestore_close(sftp_keystore_t *store) {
529 struct filestore_data *store_data;
530
531 store_data = store->keystore_data;
532 (void) pr_fsio_close(store_data->fh);
533 return 0;
534 }
535
536 static sftp_keystore_t *filestore_open(pool *parent_pool,
537 int requested_key_type, const char *store_info, const char *user) {
538 int xerrno;
539 sftp_keystore_t *store;
540 pool *filestore_pool;
541 struct filestore_data *store_data;
542 pr_fh_t *fh;
543 char buf[PR_TUNABLE_PATH_MAX+1], *path;
544 struct stat st;
545
546 filestore_pool = make_sub_pool(parent_pool);
547 pr_pool_tag(filestore_pool, "SFTP File-based Keystore Pool");
548
549 store = pcalloc(filestore_pool, sizeof(sftp_keystore_t));
550 store->keystore_pool = filestore_pool;
551
552 /* Open the file. The given path (store_info) may need to be
553 * interpolated.
554 */
555 session.user = (char *) user;
556
557 memset(buf, '\0', sizeof(buf));
558 switch (pr_fs_interpolate(store_info, buf, sizeof(buf)-1)) {
559 case 1:
560 /* Interpolate occurred; make a copy of the interpolated path. */
561 path = pstrdup(filestore_pool, buf);
562 break;
563
564 default:
565 /* Otherwise, use the path as is. */
566 path = pstrdup(filestore_pool, store_info);
567 break;
568 }
569
570 session.user = NULL;
571
572 PRIVS_ROOT
573 fh = pr_fsio_open(path, O_RDONLY|O_NONBLOCK);
574 xerrno = errno;
575 PRIVS_RELINQUISH
576
577 if (fh == NULL) {
578 destroy_pool(filestore_pool);
579 errno = xerrno;
580 return NULL;
581 }
582
583 if (pr_fsio_set_block(fh) < 0) {
584 xerrno = errno;
585
586 destroy_pool(filestore_pool);
587 (void) pr_fsio_close(fh);
588
589 errno = xerrno;
590 return NULL;
591 }
592
593 /* Stat the opened file to determine the optimal buffer size for IO. */
594 memset(&st, 0, sizeof(st));
595 if (pr_fsio_fstat(fh, &st) < 0) {
596 xerrno = errno;
597
598 destroy_pool(filestore_pool);
599 (void) pr_fsio_close(fh);
600
601 errno = xerrno;
602 return NULL;
603 }
604
605 if (S_ISDIR(st.st_mode)) {
606 destroy_pool(filestore_pool);
607 (void) pr_fsio_close(fh);
608
609 errno = EISDIR;
610 return NULL;
611 }
612
613 fh->fh_iosz = st.st_blksize;
614
615 store_data = pcalloc(filestore_pool, sizeof(struct filestore_data));
616 store->keystore_data = store_data;
617
618 store_data->path = path;
619 store_data->fh = fh;
620 store_data->lineno = 0;
621
622 store->store_ktypes = requested_key_type;
623
624 switch (requested_key_type) {
625 case SFTP_SSH2_HOST_KEY_STORE:
626 store->verify_host_key = filestore_verify_host_key;
627 break;
628
629 case SFTP_SSH2_USER_KEY_STORE:
630 store->verify_user_key = filestore_verify_user_key;
631 break;
632 }
633
634 store->store_close = filestore_close;
635 return store;
636 }
637
638 int sftp_rfc4716_init(void) {
639 sftp_keystore_register_store("file", filestore_open,
640 SFTP_SSH2_HOST_KEY_STORE|SFTP_SSH2_USER_KEY_STORE);
641
642 return 0;
643 }
644
645 int sftp_rfc4716_free(void) {
646 sftp_keystore_unregister_store("file",
647 SFTP_SSH2_HOST_KEY_STORE|SFTP_SSH2_USER_KEY_STORE);
648
649 return 0;
650 }