Debian Codegraph @

contrib/mod_sftp/keys.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp key mgmt (keys)
3 * Copyright (c) 2008-2024 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_sftp.h"
26
27 #include "ssh2.h"
28 #include "msg.h"
29 #include "packet.h"
30 #include "crypto.h"
31 #include "kex.h"
32 #include "keys.h"
33 #include "agent.h"
34 #include "interop.h"
35 #include "session.h"
36 #include "bcrypt.h"
37 #if defined(PR_USE_SODIUM)
38 # include <sodium.h>
39 #endif /* PR_USE_SODIUM */
40
41 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
42 # define CURVE448_SIZE 56
43 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
44
45 extern xaset_t *server_list;
46 extern module sftp_module;
47
48 /* Note: Should this size be made bigger, in light of larger hostkeys? */
49 #define SFTP_DEFAULT_HOSTKEY_SZ 4096
50 #define SFTP_MAX_SIG_SZ 4096
51
52 struct sftp_hostkey {
53 enum sftp_key_type_e key_type;
54 EVP_PKEY *pkey;
55
56 /* Non-OpenSSL keys */
57 unsigned char *ed25519_public_key;
58 unsigned long long ed25519_public_keylen;
59 unsigned char *ed25519_secret_key;
60 unsigned long long ed25519_secret_keylen;
61
62 unsigned char *ed448_public_key;
63 unsigned long long ed448_public_keylen;
64 unsigned char *ed448_secret_key;
65 unsigned long long ed448_secret_keylen;
66
67 const unsigned char *key_data;
68 uint32_t key_datalen;
69
70 /* This will usually not be null; if the key was obtained from a local
71 * file, this will point to that file.
72 */
73 const char *file_path;
74
75 /* This will usually be null; if the key was obtained from an agent,
76 * this point will point to the Unix domain socket to use for talking
77 * to that agent, e.g. for data signing requests.
78 */
79 const char *agent_path;
80 };
81
82 static struct sftp_hostkey *sftp_dsa_hostkey = NULL;
83 static struct sftp_hostkey *sftp_ed25519_hostkey = NULL;
84 static struct sftp_hostkey *sftp_ed448_hostkey = NULL;
85 static struct sftp_hostkey *sftp_rsa_hostkey = NULL;
86
87 #if defined(PR_USE_OPENSSL_ECC)
88 static struct sftp_hostkey *sftp_ecdsa256_hostkey = NULL;
89 static struct sftp_hostkey *sftp_ecdsa384_hostkey = NULL;
90 static struct sftp_hostkey *sftp_ecdsa521_hostkey = NULL;
91 #endif /* PR_USE_OPENSSL_ECC */
92
93 static const char *passphrase_provider = NULL;
94
95 struct sftp_pkey {
96 struct sftp_pkey *next;
97 size_t pkeysz;
98
99 char *host_pkey;
100 void *host_pkey_ptr;
101 server_rec *server;
102 };
103
104 #define SFTP_PASSPHRASE_TIMEOUT 10
105
106 static struct sftp_pkey *sftp_pkey_list = NULL;
107 static unsigned int sftp_npkeys = 0;
108 static struct sftp_pkey *server_pkey = NULL;
109
110 struct sftp_pkey_data {
111 server_rec *s;
112 const char *path;
113 char *buf;
114 size_t buflen, bufsz;
115 const char *prompt;
116 };
117
118 /* Default minimum key sizes, in BITS. The RSA minimum of 768 bits comes from
119 * the OpenSSH-7.2 implementation. And the others follow from that, based on
120 * the assumptions described here:
121 * https://en.wikipedia.org/wiki/Key_size#Asymmetric_algorithm_key_lengths
122 * http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/key-size.htm
123 *
124 * Note that the RSA size refers to the size of the modulus. The DSA size
125 * refers to the size of the modulus. The EC size refers to the minimum
126 * order of the base point on the elliptic curve.
127 */
128 static int keys_rsa_min_nbits = 768;
129 static int keys_dsa_min_nbits = 384;
130 static int keys_ec_min_nbits = 160;
131
132 /* Public key files start with "BEGIN ... PUBLIC KEY" and "END ... PUBLIC KEY"
133 * lines. Note that the "..." can be multiple different values ("RSA", "SSH2",
134 * etc).
135 */
136 #define SFTP_PUBLICKEY_BEGIN "BEGIN PUBLIC KEY"
137 #define SFTP_PUBLICKEY_BEGIN_LEN (sizeof(SFTP_PUBLICKEY_BEGIN) - 1)
138 #define SFTP_PUBLICKEY_END "END PUBLIC KEY"
139 #define SFTP_PUBLICKEY_END_LEN (sizeof(SFTP_PUBLICKEY_BEGIN) - 1)
140
141 /* OpenSSH's homegrown private key file format.
142 *
143 * See the PROTOCOL.key file in the OpenSSH source distribution for details
144 * on their homegrown private key format. See also the implementations in
145 * sskey.c#sshkey_private_to_blob2 (for writing private keys) and
146 * sshkey.c#sshkey_parse_private2 (for reading private keys). The values
147 * for different encryption ciphers are in the `ciphers[]` table in cipher.c.
148 */
149
150 #define SFTP_OPENSSH_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
151 #define SFTP_OPENSSH_END "-----END OPENSSH PRIVATE KEY-----\n"
152 #define SFTP_OPENSSH_BEGIN_LEN (sizeof(SFTP_OPENSSH_BEGIN) - 1)
153 #define SFTP_OPENSSH_END_LEN (sizeof(SFTP_OPENSSH_END) - 1)
154 #define SFTP_OPENSSH_KDFNAME "bcrypt"
155 #define SFTP_OPENSSH_MAGIC "openssh-key-v1"
156
157 /* Encryption cipher info. */
158 struct openssh_cipher {
159 const char *algo;
160 uint32_t blocksz;
161 uint32_t key_len;
162 uint32_t iv_len;
163 uint32_t auth_len;
164
165 const EVP_CIPHER *cipher;
166 const EVP_CIPHER *(*get_cipher)(void);
167 };
168
169 static struct openssh_cipher ciphers[] = {
170 { "none", 8, 0, 0, 0, NULL, EVP_enc_null },
171 { "aes256-cbc", 16, 32, 16, 0, NULL, EVP_aes_256_cbc },
172 #if defined(HAVE_EVP_AES_256_CTR_OPENSSL)
173 { "aes256-ctr", 16, 32, 16, 0, NULL, EVP_aes_256_ctr },
174 #else
175 { "aes256-ctr", 16, 32, 16, 0, NULL, NULL },
176 #endif /* HAVE_EVP_AES_256_CTR_OPENSSL */
177
178 { NULL, 0, 0, 0, 0, NULL, NULL }
179 };
180
181 static void free_hostkey_bio(BIO *);
182 static BIO *load_file_hostkey_bio(pool *p, int fd);
183 #if defined(HAVE_X448_OPENSSL)
184 static int handle_ed448_hostkey(pool *p, const unsigned char *key_data,
185 uint32_t key_datalen, const char *file_path);
186 #endif /* HAVE_X448_OPENSSL */
187 static int read_openssh_private_key(pool *p, const char *path, int fd,
188 const char *passphrase, enum sftp_key_type_e *key_type, EVP_PKEY **pkey,
189 unsigned char **key, uint32_t *keylen);
190
191 static const char *trace_channel = "ssh2";
192
193 static void prepare_provider_fds(int stdout_fd, int stderr_fd) {
194 long nfiles = 0;
195 register unsigned int i = 0;
196 struct rlimit rlim;
197
198 if (stdout_fd != STDOUT_FILENO) {
199 if (dup2(stdout_fd, STDOUT_FILENO) < 0) {
200 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
201 ": error duping fd %d to stdout: %s", stdout_fd, strerror(errno));
202 }
203
204 (void) close(stdout_fd);
205 }
206
207 if (stderr_fd != STDERR_FILENO) {
208 if (dup2(stderr_fd, STDERR_FILENO) < 0) {
209 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
210 ": error duping fd %d to stderr: %s", stderr_fd, strerror(errno));
211 }
212
213 (void) close(stderr_fd);
214 }
215
216 /* Make sure not to pass on open file descriptors. For stdout and stderr,
217 * we dup some pipes, so that we can capture what the command may write
218 * to stdout or stderr. The stderr output will be logged to the SFTPLog.
219 *
220 * First, use getrlimit() to obtain the maximum number of open files
221 * for this process -- then close that number.
222 */
223 #if defined(RLIMIT_NOFILE) || defined(RLIMIT_OFILE)
224 # if defined(RLIMIT_NOFILE)
225 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
226 # elif defined(RLIMIT_OFILE)
227 if (getrlimit(RLIMIT_OFILE, &rlim) < 0) {
228 # endif
229 /* Ignore ENOSYS (and EPERM, since some libc's use this as ENOSYS). */
230 if (errno != ENOSYS &&
231 errno != EPERM) {
232 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": getrlimit error: %s",
233 strerror(errno));
234 }
235
236 /* Pick some arbitrary high number. */
237 nfiles = 255;
238
239 } else {
240 nfiles = (unsigned long) rlim.rlim_max;
241 }
242
243 #else /* no RLIMIT_NOFILE or RLIMIT_OFILE */
244 nfiles = 255;
245 #endif
246
247 /* Appears that on some platforms (e.g. Solaris, Mac OSX), having too
248 * high of an fd value can lead to undesirable behavior for some reason.
249 * Need to track down why; the behavior I saw was the inability of
250 * select() to work properly on the stdout/stderr fds attached to the
251 * exec'd script.
252 */
253 if (nfiles > 255) {
254 nfiles = 255;
255 }
256
257 if (nfiles < 0) {
258 /* Yes, using a long for the nfiles variable is not quite kosher; it should
259 * be an unsigned type, otherwise a large limit (say, RLIMIT_INFINITY)
260 * might overflow the data type. In that case, though, we want to know
261 * about it -- and using a signed type, we will know if the overflowed
262 * value is a negative number. Chances are we do NOT want to be closing
263 * fds whose value is as high as they can possibly get; that's too many
264 * fds to iterate over. Long story short, using a long int is just fine.
265 */
266 nfiles = 255;
267 }
268
269 /* Close the "non-standard" file descriptors. */
270 for (i = 3; i < nfiles; i++) {
271 pr_signals_handle();
272 (void) close(i);
273 }
274
275 return;
276 }
277
278 static void prepare_provider_pipes(int *stdout_pipe, int *stderr_pipe) {
279 if (pipe(stdout_pipe) < 0) {
280 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": error opening stdout pipe: %s",
281 strerror(errno));
282 stdout_pipe[0] = -1;
283 stdout_pipe[1] = STDOUT_FILENO;
284
285 } else {
286 if (fcntl(stdout_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
287 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
288 ": error setting close-on-exec flag on stdout pipe read fd: %s",
289 strerror(errno));
290 }
291
292 if (fcntl(stdout_pipe[1], F_SETFD, 0) < 0) {
293 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
294 ": error setting close-on-exec flag on stdout pipe write fd: %s",
295 strerror(errno));
296 }
297 }
298
299 if (pipe(stderr_pipe) < 0) {
300 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": error opening stderr pipe: %s",
301 strerror(errno));
302 stderr_pipe[0] = -1;
303 stderr_pipe[1] = STDERR_FILENO;
304
305 } else {
306 if (fcntl(stderr_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
307 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
308 ": error setting close-on-exec flag on stderr pipe read fd: %s",
309 strerror(errno));
310 }
311
312 if (fcntl(stderr_pipe[1], F_SETFD, 0) < 0) {
313 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
314 ": error setting close-on-exec flag on stderr pipe write fd: %s",
315 strerror(errno));
316 }
317 }
318 }
319
320 static int exec_passphrase_provider(server_rec *s, char *buf, int buflen,
321 const char *path) {
322 pid_t pid;
323 int status;
324 int stdout_pipe[2], stderr_pipe[2];
325
326 struct sigaction sa_ignore, sa_intr, sa_quit;
327 sigset_t set_chldmask, set_save;
328
329 /* Prepare signal dispositions. */
330 sa_ignore.sa_handler = SIG_IGN;
331 sigemptyset(&sa_ignore.sa_mask);
332 sa_ignore.sa_flags = 0;
333
334 if (sigaction(SIGINT, &sa_ignore, &sa_intr) < 0) {
335 return -1;
336 }
337
338 if (sigaction(SIGQUIT, &sa_ignore, &sa_quit) < 0) {
339 return -1;
340 }
341
342 sigemptyset(&set_chldmask);
343 sigaddset(&set_chldmask, SIGCHLD);
344
345 if (sigprocmask(SIG_BLOCK, &set_chldmask, &set_save) < 0) {
346 return -1;
347 }
348
349 prepare_provider_pipes(stdout_pipe, stderr_pipe);
350
351 pid = fork();
352 if (pid < 0) {
353 int xerrno = errno;
354
355 pr_log_pri(PR_LOG_ALERT,
356 MOD_SFTP_VERSION ": error: unable to fork: %s", strerror(xerrno));
357
358 errno = xerrno;
359 status = -1;
360
361 } else if (pid == 0) {
362 char nbuf[32];
363 pool *tmp_pool;
364 char *stdin_argv[4];
365
366 /* Child process */
367 session.pid = getpid();
368
369 /* Note: there is no need to clean up this temporary pool, as we've
370 * forked. If the exec call succeeds, this child process will exit
371 * normally, and its process space recovered by the OS. If the exec
372 * call fails, we still exit, and the process space is recovered by
373 * the OS. Either way, the memory will be cleaned up without need for
374 * us to do it explicitly (unless one wanted to be pedantic about it,
375 * of course).
376 */
377 tmp_pool = make_sub_pool(s->pool);
378
379 /* Restore previous signal actions. */
380 sigaction(SIGINT, &sa_intr, NULL);
381 sigaction(SIGQUIT, &sa_quit, NULL);
382 sigprocmask(SIG_SETMASK, &set_save, NULL);
383
384 stdin_argv[0] = pstrdup(tmp_pool, passphrase_provider);
385
386 memset(nbuf, '\0', sizeof(nbuf));
387 pr_snprintf(nbuf, sizeof(nbuf)-1, "%u", (unsigned int) s->ServerPort);
388 nbuf[sizeof(nbuf)-1] = '\0';
389 stdin_argv[1] = pstrcat(tmp_pool, s->ServerName, ":", nbuf, NULL);
390 stdin_argv[2] = pstrdup(tmp_pool, path);
391 stdin_argv[3] = NULL;
392
393 PRIVS_ROOT
394
395 pr_log_debug(DEBUG6, MOD_SFTP_VERSION
396 ": executing '%s' with uid %lu (euid %lu), gid %lu (egid %lu)",
397 passphrase_provider,
398 (unsigned long) getuid(), (unsigned long) geteuid(),
399 (unsigned long) getgid(), (unsigned long) getegid());
400
401 /* Prepare the file descriptors that the process will inherit. */
402 prepare_provider_fds(stdout_pipe[1], stderr_pipe[1]);
403
404 errno = 0;
405 execv(passphrase_provider, stdin_argv);
406
407 /* Since all previous file descriptors (including those for log files)
408 * have been closed, and root privs have been revoked, there's little
409 * chance of directing a message of execv() failure to proftpd's log
410 * files. execv() only returns if there's an error; the only way we
411 * can signal this to the waiting parent process is to exit with a
412 * non-zero value (the value of errno will do nicely).
413 */
414
415 exit(errno);
416
417 } else {
418 int res;
419 int maxfd = -1, fds, send_sigterm = 1;
420 fd_set readfds;
421 time_t start_time = time(NULL);
422 struct timeval tv;
423
424 /* Parent process */
425
426 close(stdout_pipe[1]);
427 stdout_pipe[1] = -1;
428
429 close(stderr_pipe[1]);
430 stderr_pipe[1] = -1;
431
432 if (stdout_pipe[0] > maxfd) {
433 maxfd = stdout_pipe[0];
434 }
435
436 if (stderr_pipe[0] > maxfd) {
437 maxfd = stderr_pipe[0];
438 }
439
440 res = waitpid(pid, &status, WNOHANG);
441 while (res <= 0) {
442 if (res < 0) {
443 if (errno != EINTR) {
444 pr_log_debug(DEBUG2, MOD_SFTP_VERSION
445 ": passphrase provider error: unable to wait for pid %u: %s",
446 (unsigned int) pid, strerror(errno));
447 status = -1;
448 break;
449
450 } else {
451 pr_signals_handle();
452 continue;
453 }
454 }
455
456 /* Check the time elapsed since we started. */
457 if ((time(NULL) - start_time) > SFTP_PASSPHRASE_TIMEOUT) {
458
459 /* Send TERM, the first time, to be polite. */
460 if (send_sigterm) {
461 send_sigterm = 0;
462 pr_log_debug(DEBUG6, MOD_SFTP_VERSION
463 ": '%s' has exceeded the timeout (%lu seconds), sending "
464 "SIGTERM (signal %d)", passphrase_provider,
465 (unsigned long) SFTP_PASSPHRASE_TIMEOUT, SIGTERM);
466 kill(pid, SIGTERM);
467
468 } else {
469 /* The child is still around? Terminate with extreme prejudice. */
470 pr_log_debug(DEBUG6, MOD_SFTP_VERSION
471 ": '%s' has exceeded the timeout (%lu seconds), sending "
472 "SIGKILL (signal %d)", passphrase_provider,
473 (unsigned long) SFTP_PASSPHRASE_TIMEOUT, SIGKILL);
474 kill(pid, SIGKILL);
475 }
476 }
477
478 /* Select on the pipe read fds, to see if the child has anything
479 * to tell us.
480 */
481 FD_ZERO(&readfds);
482
483 FD_SET(stdout_pipe[0], &readfds);
484 FD_SET(stderr_pipe[0], &readfds);
485
486 /* Note: this delay should be configurable somehow. */
487 tv.tv_sec = 2L;
488 tv.tv_usec = 0L;
489
490 fds = select(maxfd + 1, &readfds, NULL, NULL, &tv);
491
492 if (fds == -1 &&
493 errno == EINTR) {
494 pr_signals_handle();
495 }
496
497 if (fds > 0) {
498 /* The child sent us something. How thoughtful. */
499
500 if (FD_ISSET(stdout_pipe[0], &readfds)) {
501 res = read(stdout_pipe[0], buf, buflen);
502 if (res > 0) {
503 buf[buflen-1] = '\0';
504
505 while (res &&
506 (buf[res-1] == '\r' ||
507 buf[res-1] == '\n')) {
508 pr_signals_handle();
509 res--;
510 }
511 buf[res] = '\0';
512
513 } else if (res < 0) {
514 pr_log_debug(DEBUG2, MOD_SFTP_VERSION
515 ": error reading stdout from '%s': %s",
516 passphrase_provider, strerror(errno));
517 }
518 }
519
520 if (FD_ISSET(stderr_pipe[0], &readfds)) {
521 long stderrlen, stderrsz;
522 char *stderrbuf;
523 pool *tmp_pool = make_sub_pool(s->pool);
524
525 stderrbuf = pr_fsio_getpipebuf(tmp_pool, stderr_pipe[0], &stderrsz);
526 memset(stderrbuf, '\0', stderrsz);
527
528 stderrlen = read(stderr_pipe[0], stderrbuf, stderrsz-1);
529 if (stderrlen > 0) {
530 while (stderrlen &&
531 (stderrbuf[stderrlen-1] == '\r' ||
532 stderrbuf[stderrlen-1] == '\n')) {
533 stderrlen--;
534 }
535 stderrbuf[stderrlen] = '\0';
536
537 pr_log_debug(DEBUG5, MOD_SFTP_VERSION
538 ": stderr from '%s': %s", passphrase_provider, stderrbuf);
539
540 } else if (res < 0) {
541 pr_log_debug(DEBUG2, MOD_SFTP_VERSION
542 ": error reading stderr from '%s': %s",
543 passphrase_provider, strerror(errno));
544 }
545
546 destroy_pool(tmp_pool);
547 tmp_pool = NULL;
548 }
549 }
550
551 res = waitpid(pid, &status, WNOHANG);
552 }
553 }
554
555 /* Restore the previous signal actions. */
556 if (sigaction(SIGINT, &sa_intr, NULL) < 0) {
557 return -1;
558 }
559
560 if (sigaction(SIGQUIT, &sa_quit, NULL) < 0) {
561 return -1;
562 }
563
564 if (sigprocmask(SIG_SETMASK, &set_save, NULL) < 0) {
565 return -1;
566 }
567
568 if (WIFSIGNALED(status)) {
569 pr_log_debug(DEBUG2, MOD_SFTP_VERSION ": '%s' died from signal %d",
570 passphrase_provider, WTERMSIG(status));
571 errno = EPERM;
572 return -1;
573 }
574
575 return 0;
576 }
577
578 /* Return the size of a page on this architecture. */
579 static size_t get_pagesz(void) {
580 long pagesz;
581
582 #if defined(_SC_PAGESIZE)
583 pagesz = sysconf(_SC_PAGESIZE);
584 #elif defined(_SC_PAGE_SIZE)
585 pagesz = sysconf(_SC_PAGE_SIZE);
586 #else
587 /* Default to using OpenSSL's defined buffer size for PEM files. */
588 pagesz = PEM_BUFSIZE;
589 #endif /* !_SC_PAGESIZE and !_SC_PAGE_SIZE */
590
591 return pagesz;
592 }
593
594 /* Return a page-aligned pointer to memory of at least the given size. */
595 static char *get_page(size_t sz, void **ptr) {
596 void *d;
597 long pagesz = get_pagesz(), p;
598
599 d = calloc(1, sz + (pagesz-1));
600 if (d == NULL) {
601 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
602 exit(1);
603 }
604
605 *ptr = d;
606
607 p = ((long) d + (pagesz-1)) &~ (pagesz-1);
608
609 return ((char *) p);
610 }
611
612 static unsigned char *decode_base64(pool *p, unsigned char *text,
613 size_t text_len, size_t *data_len) {
614 unsigned char *data = NULL;
615 int have_padding = FALSE, res;
616
617 /* Due to Base64's padding, we need to detect if the last block was padded
618 * with zeros; we do this by looking for '=' characters at the end of the
619 * text being decoded. If we see these characters, then we will "trim" off
620 * any trailing zero values in the decoded data, on the ASSUMPTION that they
621 * are the auto-added padding bytes.
622 */
623 if (text[text_len-1] == '=') {
624 have_padding = TRUE;
625 }
626
627 data = pcalloc(p, text_len);
628 res = EVP_DecodeBlock((unsigned char *) data, (unsigned char *) text,
629 (int) text_len);
630 if (res <= 0) {
631 /* Base64-decoding error. */
632 errno = EINVAL;
633 return NULL;
634 }
635
636 if (have_padding == TRUE) {
637 /* Assume that only one or two zero bytes of padding were added. */
638 if (data[res-1] == '\0') {
639 res -= 1;
640
641 if (data[res-1] == '\0') {
642 res -= 1;
643 }
644 }
645 }
646
647 *data_len = (size_t) res;
648 return data;
649 }
650
651 static int is_public_key(int fd) {
652 struct stat st;
653 char begin_buf[SFTP_PUBLICKEY_BEGIN_LEN+20];
654 ssize_t len;
655 off_t minsz;
656
657 if (fstat(fd, &st) < 0) {
658 return -1;
659 }
660
661 minsz = SFTP_PUBLICKEY_BEGIN_LEN + SFTP_PUBLICKEY_END_LEN;
662 if (st.st_size < minsz) {
663 return FALSE;
664 }
665
666 len = pread(fd, begin_buf, sizeof(begin_buf), 0);
667 if (len != sizeof(begin_buf)) {
668 return FALSE;
669 }
670
671 begin_buf[len-1] = '\0';
672
673 if (strstr(begin_buf, "PUBLIC KEY") == NULL) {
674 return FALSE;
675 }
676
677 if (strstr(begin_buf, "BEGIN") == NULL) {
678 return FALSE;
679 }
680
681 return TRUE;
682 }
683
684 static int is_openssh_private_key(int fd) {
685 struct stat st;
686 char begin_buf[SFTP_OPENSSH_BEGIN_LEN], end_buf[SFTP_OPENSSH_END_LEN];
687 ssize_t len;
688 off_t minsz;
689
690 if (fstat(fd, &st) < 0) {
691 return -1;
692 }
693
694 minsz = SFTP_OPENSSH_BEGIN_LEN + SFTP_OPENSSH_END_LEN;
695 if (st.st_size < minsz) {
696 return FALSE;
697 }
698
699 len = pread(fd, begin_buf, sizeof(begin_buf), 0);
700 if (len != sizeof(begin_buf)) {
701 return FALSE;
702 }
703
704 if (memcmp(begin_buf, SFTP_OPENSSH_BEGIN, SFTP_OPENSSH_BEGIN_LEN) != 0) {
705 return FALSE;
706 }
707
708 len = pread(fd, end_buf, sizeof(end_buf), st.st_size - SFTP_OPENSSH_END_LEN);
709 if (len != sizeof(end_buf)) {
710 return FALSE;
711 }
712
713 if (memcmp(end_buf, SFTP_OPENSSH_END, SFTP_OPENSSH_END_LEN) != 0) {
714 return FALSE;
715 }
716
717 return TRUE;
718 }
719
720 static int get_passphrase_cb(char *buf, int buflen, int rwflag, void *d) {
721 static int need_banner = TRUE;
722 struct sftp_pkey_data *pdata = d;
723
724 if (passphrase_provider == NULL) {
725 register unsigned int attempt;
726 size_t pwlen = 0;
727
728 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": requesting passphrase from admin");
729
730 if (need_banner) {
731 fprintf(stderr, "\nPlease provide passphrase for the encrypted host key:\n");
732 need_banner = FALSE;
733 }
734
735 /* You get three attempts at entering the passphrase correctly. */
736 for (attempt = 0; attempt < 3; attempt++) {
737 int res;
738
739 /* Always handle signals in a loop. */
740 pr_signals_handle();
741
742 res = EVP_read_pw_string(buf, buflen, pdata->prompt, TRUE);
743
744 /* A return value of zero from EVP_read_pw_string() means success; -1
745 * means a system error occurred, and 1 means user interaction problems.
746 */
747 if (res != 0) {
748 fprintf(stderr, "\nPassphrases do not match. Please try again.\n");
749 continue;
750 }
751
752 /* Ensure that the buffer is NUL-terminated. */
753 buf[buflen-1] = '\0';
754 pwlen = strlen(buf);
755 if (pwlen < 1) {
756 fprintf(stderr, "Error: passphrase must be at least one character\n");
757
758 } else {
759 sstrncpy(pdata->buf, buf, pdata->bufsz);
760 pdata->buflen = pwlen;
761
762 return pwlen;
763 }
764 }
765
766 } else {
767 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": requesting passphrase from '%s'",
768 passphrase_provider);
769
770 if (exec_passphrase_provider(pdata->s, buf, buflen, pdata->path) < 0) {
771 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
772 ": error obtaining passphrase from '%s': %s",
773 passphrase_provider, strerror(errno));
774
775 } else {
776 size_t pwlen;
777 /* Ensure that the buffer is NUL-terminated. */
778 buf[buflen-1] = '\0';
779
780 pwlen = strlen(buf);
781
782 sstrncpy(pdata->buf, buf, pdata->bufsz);
783 pdata->buflen = pwlen;
784
785 return pwlen;
786 }
787 }
788
789 #if OPENSSL_VERSION_NUMBER < 0x00908001
790 PEMerr(PEM_F_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
791 #else
792 PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD);
793 #endif
794
795 pr_memscrub(buf, buflen);
796 return -1;
797 }
798
799 static int get_passphrase(struct sftp_pkey *k, const char *path) {
800 pool *tmp_pool = NULL;
801 char prompt[256];
802 BIO *bio = NULL;
803 EVP_PKEY *pkey = NULL;
804 unsigned char *key_data = NULL;
805 uint32_t key_datalen = 0;
806 int fd, prompt_fd = -1, res, xerrno, openssh_format = FALSE,
807 public_key_format = FALSE;
808 struct sftp_pkey_data pdata;
809 register unsigned int attempt;
810
811 memset(prompt, '\0', sizeof(prompt));
812 res = pr_snprintf(prompt, sizeof(prompt)-1,
813 "Host key for the %s#%d (%s) server: ",
814 pr_netaddr_get_ipstr(k->server->addr), k->server->ServerPort,
815 k->server->ServerName);
816 prompt[res] = '\0';
817 prompt[sizeof(prompt)-1] = '\0';
818
819 PRIVS_ROOT
820 fd = open(path, O_RDONLY);
821 xerrno = errno;
822 PRIVS_RELINQUISH
823
824 if (fd < 0) {
825 SYSerr(SYS_F_FOPEN, xerrno);
826 errno = xerrno;
827 return -1;
828 }
829
830 /* Make sure the fd isn't one of the big three. */
831 if (fd <= STDERR_FILENO) {
832 res = pr_fs_get_usable_fd(fd);
833 if (res >= 0) {
834 (void) close(fd);
835 fd = res;
836 }
837 }
838
839 public_key_format = is_public_key(fd);
840 if (public_key_format == TRUE) {
841 pr_trace_msg(trace_channel, 3, "hostkey file '%s' uses a public key format",
842 path);
843 (void) pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
844 ": unable to use public key '%s' for SFTPHostKey", path);
845 (void) close(fd);
846 errno = EINVAL;
847 return -1;
848 }
849
850 tmp_pool = make_sub_pool(sftp_pool);
851 pr_pool_tag(tmp_pool, "SFTP Passphrase pool");
852
853 openssh_format = is_openssh_private_key(fd);
854 if (openssh_format != TRUE) {
855 /* Rather than using OpenSSL's PEM_read_PrivateKey and the underlying C
856 * library's FILE routines, dealing with unbuffered file handles and
857 * byte-by-byte reads from OpenSSL, we instead provision the file data
858 * into a memory BIO, and let OpenSSL read from that.
859 *
860 * This allows OpenSSL to maintain its byte-by-byte reads, while we read
861 * the file data using filesystem block-sized reads.
862 */
863
864 bio = load_file_hostkey_bio(tmp_pool, fd);
865 if (bio == NULL) {
866 xerrno = errno;
867
868 (void) close(fd);
869 destroy_pool(tmp_pool);
870 SYSerr(SYS_F_FOPEN, xerrno);
871
872 errno = xerrno;
873 return -1;
874 }
875
876 } else {
877 pr_trace_msg(trace_channel, 9,
878 "handling host key '%s' as an OpenSSH-formatted private key", path);
879 }
880
881 k->host_pkey = get_page(PEM_BUFSIZE, &k->host_pkey_ptr);
882 if (k->host_pkey == NULL) {
883 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!");
884 exit(1);
885 }
886
887 pdata.s = k->server;
888 pdata.buf = k->host_pkey;
889 pdata.buflen = 0;
890 pdata.bufsz = k->pkeysz;
891 pdata.path = path;
892 pdata.prompt = prompt;
893
894 /* Reconnect stderr to the term because proftpd connects stderr, earlier,
895 * to the general stderr logfile.
896 */
897 prompt_fd = open("/dev/null", O_WRONLY);
898 if (prompt_fd == -1) {
899 /* This is an arbitrary, meaningless placeholder number. */
900 prompt_fd = 76;
901 }
902
903 dup2(STDERR_FILENO, prompt_fd);
904 dup2(STDOUT_FILENO, STDERR_FILENO);
905
906 /* The user gets three tries to enter the correct passphrase. */
907 for (attempt = 0; attempt < 3; attempt++) {
908
909 /* Always handle signals in a loop. */
910 pr_signals_handle();
911
912 if (openssh_format == FALSE) {
913 pkey = PEM_read_bio_PrivateKey(bio, NULL, get_passphrase_cb, &pdata);
914 if (pkey != NULL) {
915 break;
916 }
917
918 if (BIO_reset(bio) < 0) {
919 pr_trace_msg(trace_channel, 3,
920 "error resetting BIO for '%s': %s", path, strerror(errno));
921 }
922
923 } else {
924 char buf[PEM_BUFSIZE];
925 const char *passphrase;
926 enum sftp_key_type_e key_type = SFTP_KEY_UNKNOWN;
927
928 /* First we try with no passphrase. Failing that, we have to invoke the
929 * get_passphase_cb() callback ourselves for OpenSSH keys.
930 */
931 if (attempt == 0) {
932 passphrase = pstrdup(tmp_pool, "");
933 res = read_openssh_private_key(tmp_pool, path, fd, passphrase,
934 &key_type, &pkey, &key_data, &key_datalen);
935
936 if (lseek(fd, 0, SEEK_SET) < 0) {
937 pr_trace_msg(trace_channel, 3, "error rewinding fd %d for '%s': %s",
938 fd, path, strerror(errno));
939 }
940 if (res == 0) {
941 break;
942 }
943 }
944
945 res = get_passphrase_cb(buf, PEM_BUFSIZE, 0, &pdata);
946 if (res > 0) {
947 passphrase = pdata.buf;
948
949 res = read_openssh_private_key(tmp_pool, path, fd, passphrase,
950 &key_type, &pkey, &key_data, &key_datalen);
951 if (res == 0) {
952 break;
953 }
954
955 if (lseek(fd, 0, SEEK_SET) < 0) {
956 pr_trace_msg(trace_channel, 3, "error rewinding fd %d for '%s': %s",
957 fd, path, strerror(errno));
958 }
959
960 } else {
961 pr_trace_msg(trace_channel, 2,
962 "error reading passphrase for OpenSSH key: %s",
963 sftp_crypto_get_errors());
964 }
965 }
966
967 ERR_clear_error();
968 fprintf(stderr, "\nWrong passphrase for this key. Please try again.\n");
969 }
970
971 if (bio != NULL) {
972 free_hostkey_bio(bio);
973 }
974
975 /* Done with our fd. */
976 (void) close(fd);
977
978 /* Restore the normal stderr logging. */
979 (void) dup2(prompt_fd, STDERR_FILENO);
980 (void) close(prompt_fd);
981
982 if (pkey == NULL &&
983 key_data == NULL) {
984 return -1;
985 }
986
987 if (pkey != NULL) {
988 EVP_PKEY_free(pkey);
989 }
990
991 if (key_data != NULL) {
992 pr_memscrub(key_data, key_datalen);
993 }
994
995 destroy_pool(tmp_pool);
996
997 if (pdata.buflen > 0) {
998 #if OPENSSL_VERSION_NUMBER >= 0x000905000L
999 /* Use the obtained passphrase as additional entropy, ostensibly
1000 * unknown to attackers who may be watching the network, for
1001 * OpenSSL's PRNG.
1002 *
1003 * Human language gives about 2-3 bits of entropy per byte (RFC1750).
1004 */
1005 RAND_add(pdata.buf, pdata.buflen, pdata.buflen * 0.25);
1006 #endif
1007
1008 #ifdef HAVE_MLOCK
1009 PRIVS_ROOT
1010 if (mlock(k->host_pkey, k->pkeysz) < 0) {
1011 pr_log_debug(DEBUG1, MOD_SFTP_VERSION
1012 ": error locking passphrase into memory: %s", strerror(errno));
1013
1014 } else {
1015 pr_log_debug(DEBUG1, MOD_SFTP_VERSION ": passphrase locked into memory");
1016 }
1017 PRIVS_RELINQUISH
1018 #endif
1019 }
1020
1021 return 0;
1022 }
1023
1024 static struct sftp_pkey *lookup_pkey(void) {
1025 struct sftp_pkey *k, *pkey = NULL;
1026
1027 for (k = sftp_pkey_list; k; k = k->next) {
1028
1029 /* If this pkey matches the current server_rec, mark it and move on. */
1030 if (k->server == main_server) {
1031
1032 #ifdef HAVE_MLOCK
1033 /* mlock() the passphrase memory areas again; page locks are not
1034 * inherited across forks.
1035 */
1036 PRIVS_ROOT
1037 if (k->host_pkey) {
1038 if (mlock(k->host_pkey, k->pkeysz) < 0) {
1039 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1040 "error locking passphrase into memory: %s", strerror(errno));
1041 }
1042 }
1043 PRIVS_RELINQUISH
1044 #endif /* HAVE_MLOCK */
1045
1046 pkey = k;
1047 continue;
1048 }
1049
1050 /* Otherwise, scrub the passphrase's memory areas. */
1051 if (k->host_pkey) {
1052 pr_memscrub(k->host_pkey, k->pkeysz);
1053 free(k->host_pkey_ptr);
1054 k->host_pkey = k->host_pkey_ptr = NULL;
1055 }
1056 }
1057
1058 return pkey;
1059 }
1060
1061 static void scrub_pkeys(void) {
1062 struct sftp_pkey *k;
1063
1064 if (sftp_pkey_list == NULL) {
1065 return;
1066 }
1067
1068 /* Scrub and free all passphrases in memory. */
1069 pr_log_debug(DEBUG5, MOD_SFTP_VERSION
1070 ": scrubbing %u %s from memory",
1071 sftp_npkeys, sftp_npkeys != 1 ? "passphrases" : "passphrase");
1072
1073 for (k = sftp_pkey_list; k; k = k->next) {
1074 if (k->host_pkey != NULL) {
1075 pr_memscrub(k->host_pkey, k->pkeysz);
1076 free(k->host_pkey_ptr);
1077 k->host_pkey = k->host_pkey_ptr = NULL;
1078 }
1079 }
1080
1081 sftp_pkey_list = NULL;
1082 sftp_npkeys = 0;
1083 }
1084
1085 static int pkey_cb(char *buf, int buflen, int rwflag, void *d) {
1086 struct sftp_pkey *k;
1087
1088 if (d == NULL)
1089 return 0;
1090
1091 k = (struct sftp_pkey *) d;
1092
1093 if (k->host_pkey) {
1094 sstrncpy(buf, k->host_pkey, buflen);
1095 buf[buflen - 1] = '\0';
1096 return strlen(buf);
1097 }
1098
1099 return 0;
1100 }
1101
1102 static int has_req_perms(int fd, const char *path) {
1103 struct stat st;
1104
1105 if (fstat(fd, &st) < 0) {
1106 return -1;
1107 }
1108
1109 if (st.st_mode & (S_IRWXG|S_IRWXO)) {
1110 if (!(sftp_opts & SFTP_OPT_INSECURE_HOSTKEY_PERMS)) {
1111 errno = EACCES;
1112 return -1;
1113 }
1114
1115 pr_log_pri(PR_LOG_INFO, MOD_SFTP_VERSION
1116 "notice: the permissions on SFTPHostKey '%s' (%04o) allow "
1117 "group-readable and/or world-readable access, increasing chances of "
1118 "system users reading the private key", path, st.st_mode);
1119 }
1120
1121 return 0;
1122 }
1123
1124 static uint32_t read_pkey_from_data(pool *p, unsigned char *pkey_data,
1125 uint32_t pkey_datalen, EVP_PKEY **pkey, enum sftp_key_type_e *key_type,
1126 struct sftp_verify_details *details, int openssh_format) {
1127 char *pkey_type = NULL;
1128 uint32_t res, len = 0;
1129
1130 res = sftp_msg_read_string2(p, &pkey_data, &pkey_datalen, &pkey_type);
1131 if (res == 0) {
1132 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1133 "error reading key: invalid/unsupported key format");
1134 return 0;
1135 }
1136 len += res;
1137
1138 if (strcmp(pkey_type, "ssh-rsa") == 0) {
1139 RSA *rsa;
1140 const BIGNUM *rsa_e = NULL, *rsa_n = NULL, *rsa_d = NULL;
1141
1142 *pkey = EVP_PKEY_new();
1143 if (*pkey == NULL) {
1144 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1145 "error allocating EVP_PKEY: %s", sftp_crypto_get_errors());
1146 return 0;
1147 }
1148
1149 rsa = RSA_new();
1150 if (rsa == NULL) {
1151 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1152 "error allocating RSA: %s", sftp_crypto_get_errors());
1153 EVP_PKEY_free(*pkey);
1154 *pkey = NULL;
1155 return 0;
1156 }
1157
1158 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &rsa_e);
1159 if (res == 0) {
1160 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1161 "error reading %s key: invalid/unsupported key format", pkey_type);
1162 RSA_free(rsa);
1163 EVP_PKEY_free(*pkey);
1164 *pkey = NULL;
1165 return 0;
1166 }
1167 len += res;
1168
1169 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &rsa_n);
1170 if (res == 0) {
1171 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1172 "error reading %s key: invalid/unsupported key format", pkey_type);
1173 RSA_free(rsa);
1174 EVP_PKEY_free(*pkey);
1175 *pkey = NULL;
1176 return 0;
1177 }
1178 len += res;
1179
1180 if (openssh_format == TRUE) {
1181 const BIGNUM *rsa_p, *rsa_q, *rsa_iqmp;
1182
1183 /* The OpenSSH private key format encodes more factors. */
1184
1185 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &rsa_d);
1186 if (res == 0) {
1187 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1188 "error reading %s key: invalid/unsupported key format", pkey_type);
1189 RSA_free(rsa);
1190 EVP_PKEY_free(*pkey);
1191 *pkey = NULL;
1192 return 0;
1193 }
1194 len += res;
1195
1196 /* RSA_get0_crt_params */
1197 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &rsa_iqmp);
1198 if (res == 0) {
1199 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1200 "error reading %s key: invalid/unsupported key format", pkey_type);
1201 RSA_free(rsa);
1202 EVP_PKEY_free(*pkey);
1203 *pkey = NULL;
1204 return 0;
1205 }
1206 len += res;
1207
1208 /* RSA_get0_factors */
1209 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &rsa_p);
1210 if (res == 0) {
1211 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1212 "error reading %s key: invalid/unsupported key format", pkey_type);
1213 RSA_free(rsa);
1214 EVP_PKEY_free(*pkey);
1215 *pkey = NULL;
1216 return 0;
1217 }
1218 len += res;
1219
1220 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &rsa_q);
1221 if (res == 0) {
1222 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1223 "error reading %s key: invalid/unsupported key format", pkey_type);
1224 RSA_free(rsa);
1225 EVP_PKEY_free(*pkey);
1226 *pkey = NULL;
1227 return 0;
1228 }
1229 len += res;
1230
1231 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1232 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1233 RSA_set0_crt_params(rsa, NULL, NULL, (BIGNUM *) rsa_iqmp);
1234 RSA_set0_factors(rsa, (BIGNUM *) rsa_p, (BIGNUM *) rsa_q);
1235 #else
1236 rsa->iqmp = rsa_iqmp;
1237 rsa->p = rsa_p;
1238 rsa->q = rsa_q;
1239 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1240
1241 /* Turns out that for OpenSSH formatted RSA keys, the 'e' and 'n' values
1242 * are in the opposite order than the normal PEM format. Typical.
1243 */
1244 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1245 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1246 RSA_set0_key(rsa, (BIGNUM *) rsa_e, (BIGNUM *) rsa_n, (BIGNUM *) rsa_d);
1247 #else
1248 rsa->e = rsa_n;
1249 rsa->n = rsa_e;
1250 rsa->d = rsa_d;
1251 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1252 } else {
1253 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1254 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1255 RSA_set0_key(rsa, (BIGNUM *) rsa_n, (BIGNUM *) rsa_e, (BIGNUM *) rsa_d);
1256 #else
1257 rsa->e = rsa_e;
1258 rsa->n = rsa_n;
1259 rsa->d = rsa_d;
1260 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1261 }
1262
1263 if (EVP_PKEY_assign_RSA(*pkey, rsa) != 1) {
1264 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1265 "error assigning RSA to EVP_PKEY: %s", sftp_crypto_get_errors());
1266 RSA_free(rsa);
1267 EVP_PKEY_free(*pkey);
1268 *pkey = NULL;
1269 return 0;
1270 }
1271
1272 if (key_type != NULL) {
1273 *key_type = SFTP_KEY_RSA;
1274 }
1275
1276 } else if (strcmp(pkey_type, "ssh-dss") == 0) {
1277 #if !defined(OPENSSL_NO_DSA)
1278 DSA *dsa;
1279 const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key, *dsa_priv_key = NULL;
1280
1281 *pkey = EVP_PKEY_new();
1282 if (*pkey == NULL) {
1283 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1284 "error allocating EVP_PKEY: %s", sftp_crypto_get_errors());
1285 return 0;
1286 }
1287
1288 dsa = DSA_new();
1289 if (dsa == NULL) {
1290 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1291 "error allocating DSA: %s", sftp_crypto_get_errors());
1292 EVP_PKEY_free(*pkey);
1293 *pkey = NULL;
1294 return 0;
1295 }
1296
1297 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &dsa_p);
1298 if (res == 0) {
1299 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1300 "error reading %s key: invalid/unsupported key format", pkey_type);
1301 DSA_free(dsa);
1302 EVP_PKEY_free(*pkey);
1303 *pkey = NULL;
1304 return 0;
1305 }
1306 len += res;
1307
1308 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &dsa_q);
1309 if (res == 0) {
1310 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1311 "error reading %s key: invalid/unsupported key format", pkey_type);
1312 DSA_free(dsa);
1313 EVP_PKEY_free(*pkey);
1314 *pkey = NULL;
1315 return 0;
1316 }
1317 len += res;
1318
1319 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &dsa_g);
1320 if (res == 0) {
1321 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1322 "error reading %s key: invalid/unsupported key format", pkey_type);
1323 DSA_free(dsa);
1324 EVP_PKEY_free(*pkey);
1325 *pkey = NULL;
1326 return 0;
1327 }
1328 len += res;
1329
1330 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &dsa_pub_key);
1331 if (res == 0) {
1332 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1333 "error reading %s key: invalid/unsupported key format", pkey_type);
1334 DSA_free(dsa);
1335 EVP_PKEY_free(*pkey);
1336 *pkey = NULL;
1337 return 0;
1338 }
1339 len += res;
1340
1341 if (openssh_format == TRUE) {
1342 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &dsa_priv_key);
1343 if (res == 0) {
1344 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1345 "error reading %s key: invalid/unsupported key format", pkey_type);
1346 DSA_free(dsa);
1347 EVP_PKEY_free(*pkey);
1348 *pkey = NULL;
1349 return 0;
1350 }
1351 len += res;
1352 }
1353
1354 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
1355 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
1356 DSA_set0_pqg(dsa, (BIGNUM *) dsa_p, (BIGNUM *) dsa_q, (BIGNUM *) dsa_g);
1357 DSA_set0_key(dsa, (BIGNUM *) dsa_pub_key, (BIGNUM *) dsa_priv_key);
1358 #else
1359 dsa->p = dsa_p;
1360 dsa->q = dsa_q;
1361 dsa->g = dsa_g;
1362 dsa->pub_key = dsa_pub_key;
1363 dsa->priv_key = dsa_priv_key;
1364 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
1365
1366 if (EVP_PKEY_assign_DSA(*pkey, dsa) != 1) {
1367 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1368 "error assigning RSA to EVP_PKEY: %s", sftp_crypto_get_errors());
1369 DSA_free(dsa);
1370 EVP_PKEY_free(*pkey);
1371 *pkey = NULL;
1372 return 0;
1373 }
1374
1375 if (key_type != NULL) {
1376 *key_type = SFTP_KEY_DSA;
1377 }
1378 #else
1379 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1380 "unsupported public key algorithm '%s'", pkey_type);
1381 errno = EINVAL;
1382 return 0;
1383 #endif /* !OPENSSL_NO_DSA */
1384
1385 #if defined(PR_USE_OPENSSL_ECC)
1386 } else if (strcmp(pkey_type, "ecdsa-sha2-nistp256") == 0 ||
1387 strcmp(pkey_type, "ecdsa-sha2-nistp384") == 0 ||
1388 strcmp(pkey_type, "ecdsa-sha2-nistp521") == 0) {
1389 EC_KEY *ec;
1390 const char *curve_name;
1391 const EC_GROUP *curve;
1392 EC_POINT *point;
1393 int ec_nid;
1394 char *ptr = NULL;
1395
1396 res = sftp_msg_read_string2(p, &pkey_data, &pkey_datalen, &ptr);
1397 if (res == 0) {
1398 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1399 "error reading %s key: invalid/unsupported key format", pkey_type);
1400 return 0;
1401 }
1402 len += res;
1403
1404 curve_name = (const char *) ptr;
1405
1406 /* If the curve name does not match the last 8 characters of the
1407 * public key type (which, in the case of ECDSA keys, contains the
1408 * curve name), then it's definitely a mismatch.
1409 */
1410 if (strncmp(pkey_type + 11, curve_name, 9) != 0) {
1411 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1412 "EC public key curve name '%s' does not match public key "
1413 "algorithm '%s'", curve_name, pkey_type);
1414 return 0;
1415 }
1416
1417 if (strncmp(curve_name, "nistp256", 9) == 0) {
1418 ec_nid = NID_X9_62_prime256v1;
1419
1420 if (key_type != NULL) {
1421 *key_type = SFTP_KEY_ECDSA_256;
1422 }
1423
1424 } else if (strncmp(curve_name, "nistp384", 9) == 0) {
1425 ec_nid = NID_secp384r1;
1426
1427 if (key_type != NULL) {
1428 *key_type = SFTP_KEY_ECDSA_384;
1429 }
1430
1431 } else if (strncmp(curve_name, "nistp521", 9) == 0) {
1432 ec_nid = NID_secp521r1;
1433
1434 if (key_type != NULL) {
1435 *key_type = SFTP_KEY_ECDSA_521;
1436 }
1437
1438 } else {
1439 ec_nid = -1;
1440 }
1441
1442 ec = EC_KEY_new_by_curve_name(ec_nid);
1443 if (ec == NULL) {
1444 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1445 "error allocating EC_KEY for %s: %s", pkey_type,
1446 sftp_crypto_get_errors());
1447 return 0;
1448 }
1449
1450 curve = EC_KEY_get0_group(ec);
1451
1452 point = EC_POINT_new(curve);
1453 if (point == NULL) {
1454 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1455 "error allocating EC_POINT for %s: %s", pkey_type,
1456 sftp_crypto_get_errors());
1457 EC_KEY_free(ec);
1458 return 0;
1459 }
1460
1461 res = sftp_msg_read_ecpoint2(p, &pkey_data, &pkey_datalen, curve, &point);
1462 if (res == 0) {
1463 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1464 "error reading %s key: invalid/unsupported key format", pkey_type);
1465 EC_KEY_free(ec);
1466 return 0;
1467 }
1468 len += res;
1469
1470 if (point == NULL) {
1471 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1472 "error reading EC_POINT from public key data: %s", strerror(errno));
1473 EC_POINT_free(point);
1474 EC_KEY_free(ec);
1475 return 0;
1476 }
1477
1478 if (sftp_keys_validate_ecdsa_params(curve, point) < 0) {
1479 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1480 "error validating EC public key: %s", strerror(errno));
1481 EC_POINT_free(point);
1482 EC_KEY_free(ec);
1483 return 0;
1484 }
1485
1486 if (EC_KEY_set_public_key(ec, point) != 1) {
1487 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1488 "error setting public key on EC_KEY: %s", sftp_crypto_get_errors());
1489 EC_POINT_free(point);
1490 EC_KEY_free(ec);
1491 return 0;
1492 }
1493
1494 if (openssh_format == TRUE) {
1495 const BIGNUM *ec_priv_key = NULL;
1496
1497 res = sftp_msg_read_mpint2(p, &pkey_data, &pkey_datalen, &ec_priv_key);
1498 if (res == 0) {
1499 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1500 "error reading %s key: invalid/unsupported key format", pkey_type);
1501 EC_POINT_free(point);
1502 EC_KEY_free(ec);
1503 *pkey = NULL;
1504 return 0;
1505 }
1506 len += res;
1507
1508 if (EC_KEY_set_private_key(ec, ec_priv_key) != 1) {
1509 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1510 "error setting private key on EC_KEY: %s", sftp_crypto_get_errors());
1511 EC_POINT_free(point);
1512 EC_KEY_free(ec);
1513 return 0;
1514 }
1515 }
1516
1517 *pkey = EVP_PKEY_new();
1518 if (*pkey == NULL) {
1519 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1520 "error allocating EVP_PKEY: %s", sftp_crypto_get_errors());
1521 EC_POINT_free(point);
1522 EC_KEY_free(ec);
1523 return 0;
1524 }
1525
1526 if (EVP_PKEY_assign_EC_KEY(*pkey, ec) != 1) {
1527 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1528 "error assigning ECDSA-256 to EVP_PKEY: %s", sftp_crypto_get_errors());
1529 EC_POINT_free(point);
1530 EC_KEY_free(ec);
1531 EVP_PKEY_free(*pkey);
1532 *pkey = NULL;
1533 return 0;
1534 }
1535
1536 } else if (strcmp(pkey_type, "[email protected]") == 0) {
1537 EC_KEY *ec;
1538 const char *curve_name;
1539 const EC_GROUP *curve;
1540 EC_POINT *point;
1541 int ec_nid;
1542 char *ptr = NULL;
1543
1544 res = sftp_msg_read_string2(p, &pkey_data, &pkey_datalen, &ptr);
1545 if (res == 0) {
1546 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1547 "error reading %s key: invalid/unsupported key format", pkey_type);
1548 return 0;
1549 }
1550 len += res;
1551
1552 curve_name = (const char *) ptr;
1553
1554 /* If the curve name does not match the last 8 characters of the
1555 * public key type (which, in the case of ECDSA keys, contains the
1556 * curve name), then it's definitely a mismatch.
1557 */
1558 if (strncmp(pkey_type + 14, curve_name, 8) != 0) {
1559 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1560 "EC public key curve name '%s' does not match public key "
1561 "algorithm '%s'", curve_name, pkey_type);
1562 return 0;
1563 }
1564
1565 if (strncmp(curve_name, "nistp256", 8) == 0) {
1566 ec_nid = NID_X9_62_prime256v1;
1567
1568 if (key_type != NULL) {
1569 *key_type = SFTP_KEY_ECDSA_256_SK;
1570 }
1571
1572 } else {
1573 ec_nid = -1;
1574 }
1575
1576 ec = EC_KEY_new_by_curve_name(ec_nid);
1577 if (ec == NULL) {
1578 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1579 "error allocating EC_KEY for %s: %s", pkey_type,
1580 sftp_crypto_get_errors());
1581 return 0;
1582 }
1583
1584 curve = EC_KEY_get0_group(ec);
1585
1586 point = EC_POINT_new(curve);
1587 if (point == NULL) {
1588 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1589 "error allocating EC_POINT for %s: %s", pkey_type,
1590 sftp_crypto_get_errors());
1591 EC_KEY_free(ec);
1592 return 0;
1593 }
1594
1595 res = sftp_msg_read_ecpoint2(p, &pkey_data, &pkey_datalen, curve, &point);
1596 if (res == 0) {
1597 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1598 "error reading %s key: invalid/unsupported key format", pkey_type);
1599 EC_KEY_free(ec);
1600 return 0;
1601 }
1602 len += res;
1603
1604 if (point == NULL) {
1605 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1606 "error reading EC_POINT from public key data: %s", strerror(errno));
1607 EC_POINT_free(point);
1608 EC_KEY_free(ec);
1609 return 0;
1610 }
1611
1612 if (sftp_keys_validate_ecdsa_params(curve, point) < 0) {
1613 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1614 "error validating EC public key: %s", strerror(errno));
1615 EC_POINT_free(point);
1616 EC_KEY_free(ec);
1617 return 0;
1618 }
1619
1620 if (EC_KEY_set_public_key(ec, point) != 1) {
1621 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1622 "error setting public key on EC_KEY: %s", sftp_crypto_get_errors());
1623 EC_POINT_free(point);
1624 EC_KEY_free(ec);
1625 return 0;
1626 }
1627
1628 res = sftp_msg_read_string2(p, &pkey_data, &pkey_datalen, &ptr);
1629 if (res == 0) {
1630 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1631 "error reading %s key: invalid/unsupported key format", pkey_type);
1632 return 0;
1633 }
1634 len += res;
1635
1636 if (details != NULL) {
1637 details->sk_application = ptr;
1638 }
1639
1640 *pkey = EVP_PKEY_new();
1641 if (*pkey == NULL) {
1642 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1643 "error allocating EVP_PKEY: %s", sftp_crypto_get_errors());
1644 EC_POINT_free(point);
1645 EC_KEY_free(ec);
1646 return 0;
1647 }
1648
1649 if (EVP_PKEY_assign_EC_KEY(*pkey, ec) != 1) {
1650 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1651 "error assigning ECDSA-256 to EVP_PKEY: %s", sftp_crypto_get_errors());
1652 EC_POINT_free(point);
1653 EC_KEY_free(ec);
1654 EVP_PKEY_free(*pkey);
1655 *pkey = NULL;
1656 return 0;
1657 }
1658 #endif /* PR_USE_OPENSSL_ECC */
1659
1660 #if defined(PR_USE_SODIUM)
1661 } else if (strcmp(pkey_type, "ssh-ed25519") == 0) {
1662 if (key_type != NULL) {
1663 *key_type = SFTP_KEY_ED25519;
1664 }
1665
1666 } else if (strcmp(pkey_type, "[email protected]") == 0) {
1667 char *ptr = NULL;
1668 unsigned char *public_key;
1669 uint32_t public_keylen;
1670
1671 if (key_type != NULL) {
1672 *key_type = SFTP_KEY_ED25519_SK;
1673 }
1674
1675 res = sftp_msg_read_int2(p, &pkey_data, &pkey_datalen, &public_keylen);
1676 if (res == 0) {
1677 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1678 "error reading %s key: invalid/unsupported key format", pkey_type);
1679 return 0;
1680 }
1681 len += res;
1682
1683 res = sftp_msg_read_data2(p, &pkey_data, &pkey_datalen, public_keylen,
1684 &public_key);
1685 if (res == 0) {
1686 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1687 "error reading %s key: invalid/unsupported key format", pkey_type);
1688 return 0;
1689 }
1690 len += res;
1691
1692 res = sftp_msg_read_string2(p, &pkey_data, &pkey_datalen, &ptr);
1693 if (res == 0) {
1694 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1695 "error reading %s key: invalid/unsupported key format", pkey_type);
1696 return 0;
1697 }
1698 len += res;
1699
1700 if (details != NULL) {
1701 details->sk_application = ptr;
1702 }
1703 #endif /* PR_USE_SODIUM */
1704
1705 #if defined(HAVE_X448_OPENSSL)
1706 } else if (strcmp(pkey_type, "ssh-ed448") == 0) {
1707 if (key_type != NULL) {
1708 *key_type = SFTP_KEY_ED448;
1709 }
1710 #endif /* HAVE_X448_OPENSSL */
1711
1712 } else {
1713 pr_trace_msg(trace_channel, 3, "unsupported public key algorithm '%s'",
1714 pkey_type);
1715 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1716 "unsupported public key algorithm '%s'", pkey_type);
1717 errno = EINVAL;
1718 return 0;
1719 }
1720
1721 return len;
1722 }
1723
1724 static const char *get_pkey_type_desc(int pkey_type) {
1725 const char *key_desc = NULL;
1726
1727 switch (pkey_type) {
1728 #ifdef EVP_PKEY_NONE
1729 case EVP_PKEY_NONE:
1730 key_desc = "undefined";
1731 break;
1732 #endif
1733
1734 #ifdef EVP_PKEY_RSA
1735 case EVP_PKEY_RSA:
1736 key_desc = "RSA";
1737 break;
1738 #endif
1739
1740 #ifdef EVP_PKEY_DSA
1741 case EVP_PKEY_DSA:
1742 key_desc = "DSA";
1743 break;
1744 #endif
1745
1746 #ifdef EVP_PKEY_DH
1747 case EVP_PKEY_DH:
1748 key_desc = "DH";
1749 break;
1750 #endif
1751
1752 #ifdef EVP_PKEY_EC
1753 case EVP_PKEY_EC:
1754 key_desc = "ECC";
1755 break;
1756 #endif
1757
1758 default:
1759 key_desc = "unknown";
1760 }
1761
1762 return key_desc;
1763 }
1764
1765 static const char *get_key_type_desc(enum sftp_key_type_e key_type) {
1766 const char *key_desc = NULL;
1767
1768 switch (key_type) {
1769 case SFTP_KEY_UNKNOWN:
1770 key_desc = "unknown";
1771 break;
1772
1773 case SFTP_KEY_DSA:
1774 key_desc = "DSA";
1775 break;
1776
1777 case SFTP_KEY_RSA:
1778 key_desc = "RSA";
1779 break;
1780
1781 case SFTP_KEY_ECDSA_256:
1782 key_desc = "ECDSA256";
1783 break;
1784
1785 case SFTP_KEY_ECDSA_384:
1786 key_desc = "ECDSA384";
1787 break;
1788
1789 case SFTP_KEY_ECDSA_521:
1790 key_desc = "ECDSA521";
1791 break;
1792
1793 case SFTP_KEY_ED25519:
1794 key_desc = "ED25519";
1795 break;
1796
1797 case SFTP_KEY_ED448:
1798 key_desc = "ED448";
1799 break;
1800
1801 case SFTP_KEY_ECDSA_256_SK:
1802 key_desc = "ECDSA256-SK";
1803 break;
1804
1805 case SFTP_KEY_ED25519_SK:
1806 key_desc = "ED25519-SK";
1807 break;
1808
1809 default:
1810 key_desc = "undefined";
1811 break;
1812 }
1813
1814 return key_desc;
1815 }
1816
1817 #if defined(PR_USE_OPENSSL_ECC)
1818 /* Make sure the given ECDSA private key is suitable for use. */
1819 static int validate_ecdsa_private_key(const EC_KEY *ec) {
1820 BN_CTX *bn_ctx;
1821 BIGNUM *ec_order, *bn_tmp;
1822 int ec_order_nbits, priv_key_nbits;
1823
1824 /* A BN_CTX is like our pools; we allocate one, use it to get any
1825 * number of BIGNUM variables, and only have free up the BN_CTX when
1826 * we're done, rather than all of the individual BIGNUMs.
1827 */
1828
1829 bn_ctx = BN_CTX_new();
1830 if (bn_ctx == NULL) {
1831 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1832 "error allocating BN_CTX: %s", sftp_crypto_get_errors());
1833 return -1;
1834 }
1835
1836 BN_CTX_start(bn_ctx);
1837
1838 ec_order = BN_CTX_get(bn_ctx);
1839 if (ec_order == NULL) {
1840 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1841 "error getting new BIGNUM from BN_CTX: %s", sftp_crypto_get_errors());
1842 BN_CTX_free(bn_ctx);
1843 errno = EPERM;
1844 return -1;
1845 }
1846
1847 bn_tmp = BN_CTX_get(bn_ctx);
1848 if (bn_tmp == NULL) {
1849 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1850 "error getting new BIGNUM from BN_CTX: %s", sftp_crypto_get_errors());
1851 BN_CTX_free(bn_ctx);
1852 errno = EPERM;
1853 return -1;
1854 }
1855
1856 /* Make sure that log2(private key) is greater than log2(EC order)/2. */
1857
1858 if (EC_GROUP_get_order(EC_KEY_get0_group(ec), ec_order, bn_ctx) != 1) {
1859 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1860 "error getting the EC group order: %s", sftp_crypto_get_errors());
1861 BN_CTX_free(bn_ctx);
1862 errno = EPERM;
1863 return -1;
1864 }
1865
1866 priv_key_nbits = BN_num_bits(EC_KEY_get0_private_key(ec));
1867 ec_order_nbits = BN_num_bits(ec_order);
1868
1869 if (priv_key_nbits <= (ec_order_nbits / 2)) {
1870 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1871 "ECDSA private key (%d bits) is too small, must be at "
1872 "least %d bits", priv_key_nbits, ec_order_nbits);
1873 BN_CTX_free(bn_ctx);
1874 errno = EACCES;
1875 return -1;
1876 }
1877
1878 /* Ensure that the private key < (EC order - 1). */
1879
1880 if (BN_sub(bn_tmp, ec_order, BN_value_one()) == 0) {
1881 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1882 "error subtracting one from EC group order: %s",
1883 sftp_crypto_get_errors());
1884 BN_CTX_free(bn_ctx);
1885 errno = EPERM;
1886 return -1;
1887 }
1888
1889 if (BN_cmp(EC_KEY_get0_private_key(ec), bn_tmp) >= 0) {
1890 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1891 "ECDSA private key is greater than or equal to EC group order, "
1892 "rejecting");
1893 BN_CTX_free(bn_ctx);
1894 errno = EACCES;
1895 return -1;
1896 }
1897
1898 BN_CTX_free(bn_ctx);
1899 return 0;
1900 }
1901
1902 /* This is used to validate the ECDSA parameters we might receive e.g. from
1903 * a client. These checks come from Section 3.2.2.1 of 'Standards for
1904 * Efficient Cryptography Group, "Elliptic Curve Cryptography", SEC 1,
1905 * May 2009:
1906 *
1907 * http://www.secg.org/download/aid-780/sec1-v2.pdf
1908 *
1909 * as per RFC 5656 recommendation.
1910 */
1911 int sftp_keys_validate_ecdsa_params(const EC_GROUP *group,
1912 const EC_POINT *point) {
1913 BN_CTX *bn_ctx;
1914 BIGNUM *ec_order, *x_coord, *y_coord, *bn_tmp;
1915 int coord_nbits, ec_order_nbits;
1916 EC_POINT *subgroup_order = NULL;
1917
1918 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != NID_X9_62_prime_field) {
1919 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1920 "ECDSA group is not a prime field, rejecting");
1921 errno = EACCES;
1922 return -1;
1923 }
1924
1925 /* A Q of infinity is unacceptable. */
1926 if (EC_POINT_is_at_infinity(group, point) != 0) {
1927 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1928 "ECDSA EC point has infinite value, rejecting");
1929 errno = EACCES;
1930 return -1;
1931 }
1932
1933 /* A BN_CTX is like our pools; we allocate one, use it to get any
1934 * number of BIGNUM variables, and only have free up the BN_CTX when
1935 * we're done, rather than all of the individual BIGNUMs.
1936 */
1937
1938 bn_ctx = BN_CTX_new();
1939 if (bn_ctx == NULL) {
1940 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1941 "error allocating BN_CTX: %s", sftp_crypto_get_errors());
1942 return -1;
1943 }
1944
1945 BN_CTX_start(bn_ctx);
1946
1947 ec_order = BN_CTX_get(bn_ctx);
1948 if (ec_order == NULL) {
1949 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1950 "error getting new BIGNUM from BN_CTX: %s", sftp_crypto_get_errors());
1951 BN_CTX_free(bn_ctx);
1952 errno = EPERM;
1953 return -1;
1954 }
1955
1956 if (EC_GROUP_get_order(group, ec_order, bn_ctx) != 1) {
1957 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1958 "error getting EC group order: %s", sftp_crypto_get_errors());
1959 BN_CTX_free(bn_ctx);
1960 errno = EPERM;
1961 return -1;
1962 }
1963
1964 x_coord = BN_CTX_get(bn_ctx);
1965 if (x_coord == NULL) {
1966 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1967 "error getting new BIGNUM from BN_CTX: %s", sftp_crypto_get_errors());
1968 BN_CTX_free(bn_ctx);
1969 errno = EPERM;
1970 return -1;
1971 }
1972
1973 y_coord = BN_CTX_get(bn_ctx);
1974 if (y_coord == NULL) {
1975 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1976 "error getting new BIGNUM from BN_CTX: %s", sftp_crypto_get_errors());
1977 BN_CTX_free(bn_ctx);
1978 errno = EPERM;
1979 return -1;
1980 }
1981
1982 if (EC_POINT_get_affine_coordinates_GFp(group, point, x_coord, y_coord,
1983 bn_ctx) != 1) {
1984 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1985 "error getting EC point affine coordinates: %s",
1986 sftp_crypto_get_errors());
1987 BN_CTX_free(bn_ctx);
1988 errno = EPERM;
1989 return -1;
1990 }
1991
1992 /* Ensure that the following are both true:
1993 *
1994 * log2(X coord) > log2(EC order)/2
1995 * log2(Y coord) > log2(EC order)/2
1996 */
1997
1998 coord_nbits = BN_num_bits(x_coord);
1999 ec_order_nbits = BN_num_bits(ec_order);
2000 if (coord_nbits <= (ec_order_nbits / 2)) {
2001 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2002 "EC public key X coordinate (%d bits) too small (<= %d bits), rejecting",
2003 coord_nbits, (ec_order_nbits / 2));
2004 BN_CTX_free(bn_ctx);
2005 errno = EACCES;
2006 return -1;
2007 }
2008
2009 coord_nbits = BN_num_bits(y_coord);
2010 if (coord_nbits <= (ec_order_nbits / 2)) {
2011 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2012 "EC public key Y coordinate (%d bits) too small (<= %d bits), rejecting",
2013 coord_nbits, (ec_order_nbits / 2));
2014 BN_CTX_free(bn_ctx);
2015 errno = EACCES;
2016 return -1;
2017 }
2018
2019 /* Ensure that the following is true:
2020 *
2021 * subgroup order == infinity
2022 */
2023
2024 subgroup_order = EC_POINT_new(group);
2025 if (subgroup_order == NULL) {
2026 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2027 "error allocating new EC_POINT: %s", sftp_crypto_get_errors());
2028 BN_CTX_free(bn_ctx);
2029 errno = EPERM;
2030 return -1;
2031 }
2032
2033 if (EC_POINT_mul(group, subgroup_order, NULL, point, ec_order, bn_ctx) != 1) {
2034 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2035 "error doing EC point multiplication: %s", sftp_crypto_get_errors());
2036 EC_POINT_free(subgroup_order);
2037 BN_CTX_free(bn_ctx);
2038 errno = EPERM;
2039 return -1;
2040 }
2041
2042 if (EC_POINT_is_at_infinity(group, subgroup_order) != 1) {
2043 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2044 "EC public key has finite subgroup order, rejecting");
2045 EC_POINT_free(subgroup_order);
2046 BN_CTX_free(bn_ctx);
2047 errno = EACCES;
2048 return -1;
2049 }
2050
2051 EC_POINT_free(subgroup_order);
2052
2053 /* Ensure that the following are both true:
2054 *
2055 * X < order - 1
2056 * Y < order - 1
2057 */
2058
2059 bn_tmp = BN_CTX_get(bn_ctx);
2060 if (bn_tmp == NULL) {
2061 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2062 "error getting new BIGNUM from BN_CTX: %s", sftp_crypto_get_errors());
2063 BN_CTX_free(bn_ctx);
2064 errno = EPERM;
2065 return -1;
2066 }
2067
2068 if (BN_sub(bn_tmp, ec_order, BN_value_one()) == 0) {
2069 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2070 "error subtracting one from EC group order: %s",
2071 sftp_crypto_get_errors());
2072 BN_CTX_free(bn_ctx);
2073 errno = EPERM;
2074 return -1;
2075 }
2076
2077 if (BN_cmp(x_coord, bn_tmp) >= 0) {
2078 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2079 "EC public key X coordinate too large (>= EC group order - 1), "
2080 "rejecting");
2081 BN_CTX_free(bn_ctx);
2082 errno = EACCES;
2083 return -1;
2084 }
2085
2086 if (BN_cmp(y_coord, bn_tmp) >= 0) {
2087 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2088 "EC public key Y coordinate too large (>= EC group order - 1), "
2089 "rejecting");
2090 BN_CTX_free(bn_ctx);
2091 errno = EACCES;
2092 return -1;
2093 }
2094
2095 BN_CTX_free(bn_ctx);
2096 return 0;
2097 }
2098 #endif /* PR_USE_OPENSSL_ECC */
2099
2100 #ifdef SFTP_DEBUG_KEYS
2101 static void debug_rsa_key(pool *p, const char *label, RSA *rsa) {
2102 BIO *bio = NULL;
2103 char *data;
2104 long datalen;
2105
2106 bio = BIO_new(BIO_s_mem());
2107 RSA_print(bio, rsa, 0);
2108 BIO_flush(bio);
2109 datalen = BIO_get_mem_data(bio, &data);
2110 if (data != NULL &&
2111 datalen > 0) {
2112 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "%s",label);
2113 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "%.*s",
2114 (int) datalen, data);
2115 }
2116
2117 BIO_free(bio);
2118 }
2119 #endif
2120
2121 static int get_pkey_type(EVP_PKEY *pkey) {
2122 int pkey_type;
2123
2124 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
2125 !defined(HAVE_LIBRESS)
2126 pkey_type = EVP_PKEY_base_id(pkey);
2127 #else
2128 pkey_type = EVP_PKEY_type(pkey->type);
2129 #endif /* OpenSSL 1.1.x and later */
2130
2131 return pkey_type;
2132 }
2133
2134 static int rsa_compare_keys(pool *p, EVP_PKEY *remote_pkey,
2135 EVP_PKEY *local_pkey) {
2136 RSA *remote_rsa = NULL, *local_rsa = NULL;
2137 const BIGNUM *remote_rsa_e = NULL, *local_rsa_e = NULL;
2138 const BIGNUM *remote_rsa_n = NULL, *local_rsa_n = NULL;
2139 int res = 0;
2140
2141 local_rsa = EVP_PKEY_get1_RSA(local_pkey);
2142 if (keys_rsa_min_nbits > 0) {
2143 int rsa_nbits;
2144
2145 rsa_nbits = RSA_size(local_rsa) * 8;
2146 if (rsa_nbits < keys_rsa_min_nbits) {
2147 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2148 "local RSA key size (%d bits) less than required minimum (%d bits)",
2149 rsa_nbits, keys_rsa_min_nbits);
2150 RSA_free(local_rsa);
2151 errno = EPERM;
2152 return -1;
2153 }
2154
2155 pr_trace_msg(trace_channel, 19,
2156 "comparing RSA keys using local RSA key (%d bits, min %d)", rsa_nbits,
2157 keys_rsa_min_nbits);
2158 }
2159
2160 remote_rsa = EVP_PKEY_get1_RSA(remote_pkey);
2161
2162 #ifdef SFTP_DEBUG_KEYS
2163 debug_rsa_key(p, "remote RSA key:", remote_rsa);
2164 debug_rsa_key(p, "local RSA key:", local_rsa);
2165 #endif
2166
2167 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2168 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2169 RSA_get0_key(remote_rsa, &remote_rsa_n, &remote_rsa_e, NULL);
2170 RSA_get0_key(local_rsa, &local_rsa_n, &local_rsa_e, NULL);
2171 #else
2172 remote_rsa_e = remote_rsa->e;
2173 local_rsa_e = local_rsa->e;
2174 remote_rsa_n = remote_rsa->n;
2175 local_rsa_n = local_rsa->n;
2176 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
2177
2178 if (BN_cmp(remote_rsa_e, local_rsa_e) != 0) {
2179 pr_trace_msg(trace_channel, 17, "%s",
2180 "RSA key mismatch: client-sent RSA key component 'e' does not match "
2181 "local RSA key component 'e'");
2182 res = -1;
2183 }
2184
2185 if (res == 0) {
2186 if (BN_cmp(remote_rsa_n, local_rsa_n) != 0) {
2187 pr_trace_msg(trace_channel, 17, "%s",
2188 "RSA key mismatch: client-sent RSA key component 'n' does not match "
2189 "local RSA key component 'n'");
2190 res = -1;
2191 }
2192 }
2193
2194 RSA_free(remote_rsa);
2195 RSA_free(local_rsa);
2196 return res;
2197 }
2198
2199 #if !defined(OPENSSL_NO_DSA)
2200 static int dsa_compare_keys(pool *p, EVP_PKEY *remote_pkey,
2201 EVP_PKEY *local_pkey) {
2202 DSA *remote_dsa = NULL, *local_dsa = NULL;
2203 const BIGNUM *remote_dsa_p, *remote_dsa_q, *remote_dsa_g;
2204 const BIGNUM *local_dsa_p, *local_dsa_q, *local_dsa_g;
2205 const BIGNUM *remote_dsa_pub_key, *local_dsa_pub_key;
2206 int res = 0;
2207
2208 local_dsa = EVP_PKEY_get1_DSA(local_pkey);
2209 if (keys_dsa_min_nbits > 0) {
2210 int dsa_nbits;
2211
2212 dsa_nbits = DSA_size(local_dsa) * 8;
2213 if (dsa_nbits < keys_dsa_min_nbits) {
2214 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2215 "local DSA key size (%d bits) less than required minimum (%d bits)",
2216 dsa_nbits, keys_dsa_min_nbits);
2217 DSA_free(local_dsa);
2218 errno = EPERM;
2219 return -1;
2220 }
2221
2222 pr_trace_msg(trace_channel, 19,
2223 "comparing DSA keys using local DSA key (%d bits)", dsa_nbits);
2224 }
2225
2226 remote_dsa = EVP_PKEY_get1_DSA(remote_pkey);
2227
2228 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2229 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2230 DSA_get0_pqg(remote_dsa, &remote_dsa_p, &remote_dsa_q, &remote_dsa_g);
2231 DSA_get0_pqg(local_dsa, &local_dsa_p, &local_dsa_q, &local_dsa_g);
2232 DSA_get0_key(remote_dsa, &remote_dsa_pub_key, NULL);
2233 DSA_get0_key(local_dsa, &local_dsa_pub_key, NULL);
2234 #else
2235 remote_dsa_p = remote_dsa->p;
2236 remote_dsa_q = remote_dsa->q;
2237 remote_dsa_g = remote_dsa->g;
2238 remote_dsa_pub_key = remote_dsa->pub_key;
2239 local_dsa_p = local_dsa->p;
2240 local_dsa_q = local_dsa->q;
2241 local_dsa_g = local_dsa->g;
2242 local_dsa_pub_key = local_dsa->pub_key;
2243 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
2244
2245 if (BN_cmp(remote_dsa_p, local_dsa_p) != 0) {
2246 pr_trace_msg(trace_channel, 17, "%s",
2247 "DSA key mismatch: client-sent DSA key parameter 'p' does not match "
2248 "local DSA key parameter 'p'");
2249 res = -1;
2250 }
2251
2252 if (res == 0) {
2253 if (BN_cmp(remote_dsa_q, local_dsa_q) != 0) {
2254 pr_trace_msg(trace_channel, 17, "%s",
2255 "DSA key mismatch: client-sent DSA key parameter 'q' does not match "
2256 "local DSA key parameter 'q'");
2257 res = -1;
2258 }
2259 }
2260
2261 if (res == 0) {
2262 if (BN_cmp(remote_dsa_g, local_dsa_g) != 0) {
2263 pr_trace_msg(trace_channel, 17, "%s",
2264 "DSA key mismatch: client-sent DSA key parameter 'g' does not match "
2265 "local DSA key parameter 'g'");
2266 res = -1;
2267 }
2268 }
2269
2270 if (res == 0) {
2271 if (BN_cmp(remote_dsa_pub_key, local_dsa_pub_key) != 0) {
2272 pr_trace_msg(trace_channel, 17, "%s",
2273 "DSA key mismatch: client-sent DSA key parameter 'pub_key' does not "
2274 "match local DSA key parameter 'pub_key'");
2275 res = -1;
2276 }
2277 }
2278
2279 DSA_free(remote_dsa);
2280 DSA_free(local_dsa);
2281 return res;
2282 }
2283 #endif /* OPENSSL_NO_DSA */
2284
2285 #if defined(PR_USE_OPENSSL_ECC)
2286 static int ecdsa_compare_keys(pool *p, EVP_PKEY *remote_pkey,
2287 EVP_PKEY *local_pkey) {
2288 EC_KEY *remote_ec, *local_ec;
2289 int res = 0;
2290
2291 local_ec = EVP_PKEY_get1_EC_KEY(local_pkey);
2292 if (keys_ec_min_nbits > 0) {
2293 int ec_nbits;
2294
2295 ec_nbits = EVP_PKEY_bits(local_pkey) * 8;
2296 if (ec_nbits < keys_ec_min_nbits) {
2297 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2298 "local EC key size (%d bits) less than required minimum (%d bits)",
2299 ec_nbits, keys_ec_min_nbits);
2300 EC_KEY_free(local_ec);
2301 errno = EPERM;
2302 return -1;
2303 }
2304
2305 pr_trace_msg(trace_channel, 19,
2306 "comparing EC keys using local EC key (%d bits)", ec_nbits);
2307 }
2308
2309 remote_ec = EVP_PKEY_get1_EC_KEY(remote_pkey);
2310
2311 if (EC_GROUP_cmp(EC_KEY_get0_group(local_ec),
2312 EC_KEY_get0_group(remote_ec), NULL) != 0) {
2313 pr_trace_msg(trace_channel, 17, "%s",
2314 "ECC key mismatch: client-sent curve does not match local ECC curve");
2315 res = -1;
2316 }
2317
2318 if (res == 0) {
2319 if (EC_POINT_cmp(EC_KEY_get0_group(local_ec),
2320 EC_KEY_get0_public_key(local_ec),
2321 EC_KEY_get0_public_key(remote_ec), NULL) != 0) {
2322 pr_trace_msg(trace_channel, 17, "%s",
2323 "ECC key mismatch: client-sent public key 'Q' does not match "
2324 "local ECC public key 'Q'");
2325 res = -1;
2326 }
2327 }
2328
2329 EC_KEY_free(remote_ec);
2330 EC_KEY_free(local_ec);
2331 return res;
2332 }
2333 #endif /* PR_USE_OPENSSL_ECC */
2334
2335 #if defined(PR_USE_SODIUM)
2336 static int ed25519_compare_keys(pool *p,
2337 unsigned char *remote_pubkey_data, uint32_t remote_pubkey_datalen,
2338 unsigned char *local_pubkey_data, uint32_t local_pubkey_datalen) {
2339 int res = 0;
2340
2341 if (remote_pubkey_datalen != local_pubkey_datalen) {
2342 return -1;
2343 }
2344
2345 if (memcmp(remote_pubkey_data, local_pubkey_data, remote_pubkey_datalen) != 0) {
2346 res = -1;
2347 }
2348
2349 return res;
2350 }
2351 #endif /* PR_USE_SODIUM */
2352
2353 #if defined(HAVE_X448_OPENSSL)
2354 static int ed448_compare_keys(pool *p,
2355 unsigned char *remote_pubkey_data, uint32_t remote_pubkey_datalen,
2356 unsigned char *local_pubkey_data, uint32_t local_pubkey_datalen) {
2357 int res = 0;
2358
2359 if (remote_pubkey_datalen != local_pubkey_datalen) {
2360 return -1;
2361 }
2362
2363 if (memcmp(remote_pubkey_data, local_pubkey_data, remote_pubkey_datalen) != 0) {
2364 res = -1;
2365 }
2366
2367 return res;
2368 }
2369 #endif /* HAVE_X448_OPENSSL */
2370
2371 /* Compare a "blob" of pubkey data sent by the client for authentication
2372 * with a local file pubkey (from an RFC4716 formatted file). Returns -1 if
2373 * there was an error, TRUE if the keys are equals, and FALSE if not.
2374 */
2375 int sftp_keys_compare_keys(pool *p,
2376 unsigned char *remote_pubkey_data, uint32_t remote_pubkey_datalen,
2377 unsigned char *local_pubkey_data, uint32_t local_pubkey_datalen) {
2378 enum sftp_key_type_e remote_key_type, local_key_type;
2379 EVP_PKEY *remote_pkey = NULL, *local_pkey = NULL;
2380 int res = -1;
2381 uint32_t len = 0;
2382
2383 if (remote_pubkey_data == NULL ||
2384 local_pubkey_data == NULL) {
2385 errno = EINVAL;
2386 return -1;
2387 }
2388
2389 remote_key_type = local_key_type = SFTP_KEY_UNKNOWN;
2390
2391 len = read_pkey_from_data(p, remote_pubkey_data, remote_pubkey_datalen,
2392 &remote_pkey, &remote_key_type, NULL, FALSE);
2393 if (len == 0) {
2394 return -1;
2395 }
2396
2397 len = read_pkey_from_data(p, local_pubkey_data, local_pubkey_datalen,
2398 &local_pkey, &local_key_type, NULL, FALSE);
2399 if (len == 0) {
2400 int xerrno = errno;
2401
2402 if (remote_pkey != NULL) {
2403 EVP_PKEY_free(remote_pkey);
2404 }
2405
2406 errno = xerrno;
2407 return -1;
2408 }
2409
2410 if (remote_pkey != NULL &&
2411 local_pkey != NULL &&
2412 remote_key_type == local_key_type) {
2413 switch (get_pkey_type(remote_pkey)) {
2414 case EVP_PKEY_RSA: {
2415 if (rsa_compare_keys(p, remote_pkey, local_pkey) == 0) {
2416 res = TRUE;
2417
2418 } else {
2419 res = FALSE;
2420 }
2421
2422 break;
2423 }
2424
2425 #if !defined(OPENSSL_NO_DSA)
2426 case EVP_PKEY_DSA: {
2427 if (dsa_compare_keys(p, remote_pkey, local_pkey) == 0) {
2428 res = TRUE;
2429
2430 } else {
2431 res = FALSE;
2432 }
2433
2434 break;
2435 }
2436 #endif /* !OPENSSL_NO_DSA */
2437
2438 #if defined(PR_USE_OPENSSL_ECC)
2439 case EVP_PKEY_EC: {
2440 if (ecdsa_compare_keys(p, remote_pkey, local_pkey) == 0) {
2441 res = TRUE;
2442
2443 } else {
2444 res = FALSE;
2445 }
2446
2447 break;
2448 }
2449 #endif /* PR_USE_OPENSSL_ECC */
2450
2451 default:
2452 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2453 "unable to compare %s keys: unsupported key type",
2454 get_pkey_type_desc(get_pkey_type(remote_pkey)));
2455 errno = ENOSYS;
2456 break;
2457 }
2458
2459 } else if ((remote_key_type == SFTP_KEY_ED25519 ||
2460 remote_key_type == SFTP_KEY_ED25519_SK) &&
2461 remote_key_type == local_key_type) {
2462 #if defined(PR_USE_SODIUM)
2463 if (ed25519_compare_keys(p, remote_pubkey_data, remote_pubkey_datalen,
2464 local_pubkey_data, local_pubkey_datalen) == 0) {
2465 res = TRUE;
2466
2467 } else {
2468 res = FALSE;
2469 }
2470 #endif /* PR_USE_SODIUM */
2471
2472 } else if (remote_key_type == SFTP_KEY_ED448 &&
2473 remote_key_type == local_key_type) {
2474 #if defined(HAVE_X448_OPENSSL)
2475 if (ed448_compare_keys(p, remote_pubkey_data, remote_pubkey_datalen,
2476 local_pubkey_data, local_pubkey_datalen) == 0) {
2477 res = TRUE;
2478
2479 } else {
2480 res = FALSE;
2481 }
2482 #endif /* HAVE_X448_OPENSSL */
2483
2484 } else {
2485 if (pr_trace_get_level(trace_channel) >= 17) {
2486 const char *remote_key_desc, *local_key_desc;
2487
2488 remote_key_desc = get_key_type_desc(remote_key_type);
2489 local_key_desc = get_key_type_desc(local_key_type);
2490
2491 pr_trace_msg(trace_channel, 17, "key mismatch: cannot compare %s key "
2492 "(client-sent) with %s key (local)", remote_key_desc, local_key_desc);
2493 }
2494
2495 res = FALSE;
2496 }
2497
2498 if (remote_pkey != NULL) {
2499 EVP_PKEY_free(remote_pkey);
2500 }
2501
2502 if (local_pkey != NULL) {
2503 EVP_PKEY_free(local_pkey);
2504 }
2505
2506 return res;
2507 }
2508
2509 const char *sftp_keys_get_fingerprint(pool *p, unsigned char *key_data,
2510 uint32_t key_datalen, int digest_algo) {
2511 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
2512 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
2513 EVP_MD_CTX ctx;
2514 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
2515 EVP_MD_CTX *pctx;
2516 const EVP_MD *digest;
2517 char *digest_name = "none", *fp;
2518 unsigned char *fp_data;
2519 unsigned int fp_datalen = 0;
2520 register unsigned int i;
2521
2522 switch (digest_algo) {
2523 case SFTP_KEYS_FP_DIGEST_MD5:
2524 digest = EVP_md5();
2525 digest_name = "md5";
2526 break;
2527
2528 case SFTP_KEYS_FP_DIGEST_SHA1:
2529 digest = EVP_sha1();
2530 digest_name = "sha1";
2531 break;
2532
2533 #if defined(HAVE_SHA256_OPENSSL)
2534 case SFTP_KEYS_FP_DIGEST_SHA256:
2535 digest = EVP_sha256();
2536 digest_name = "sha256";
2537 break;
2538 #endif /* HAVE_SHA256_OPENSSL */
2539
2540 default:
2541 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2542 "unsupported key fingerprint digest algorithm (%d)", digest_algo);
2543 errno = EACCES;
2544 return NULL;
2545 }
2546
2547 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2548 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2549 pctx = EVP_MD_CTX_new();
2550 #else
2551 pctx = &ctx;
2552 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
2553
2554 /* In OpenSSL 0.9.6, many of the EVP_Digest* functions returned void, not
2555 * int. Without these ugly OpenSSL version preprocessor checks, the
2556 * compiler will error out with "void value not ignored as it ought to be".
2557 */
2558
2559 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
2560 if (EVP_DigestInit(pctx, digest) != 1) {
2561 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2562 "error initializing %s digest: %s", digest_name,
2563 sftp_crypto_get_errors());
2564 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2565 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2566 EVP_MD_CTX_free(pctx);
2567 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
2568 errno = EPERM;
2569 return NULL;
2570 }
2571 #else
2572 EVP_DigestInit(pctx, digest);
2573 #endif
2574
2575 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
2576 if (EVP_DigestUpdate(pctx, key_data, key_datalen) != 1) {
2577 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2578 "error updating %s digest: %s", digest_name, sftp_crypto_get_errors());
2579 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2580 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2581 EVP_MD_CTX_free(pctx);
2582 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
2583 errno = EPERM;
2584 return NULL;
2585 }
2586 #else
2587 EVP_DigestUpdate(pctx, key_data, key_datalen);
2588 #endif
2589
2590 fp_data = palloc(p, EVP_MAX_MD_SIZE);
2591
2592 #if OPENSSL_VERSION_NUMBER >= 0x000907000L
2593 if (EVP_DigestFinal(pctx, fp_data, &fp_datalen) != 1) {
2594 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2595 "error finishing %s digest: %s", digest_name, sftp_crypto_get_errors());
2596 # if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2597 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2598 EVP_MD_CTX_free(pctx);
2599 # endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
2600 errno = EPERM;
2601 return NULL;
2602 }
2603 #else
2604 EVP_DigestFinal(pctx, fp_data, &fp_datalen);
2605 #endif
2606
2607 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
2608 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
2609 EVP_MD_CTX_free(pctx);
2610 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
2611
2612 /* Now encode that digest in fp_data as hex characters. */
2613 fp = "";
2614
2615 for (i = 0; i < fp_datalen; i++) {
2616 char c[4];
2617
2618 memset(c, '\0', sizeof(c));
2619 pr_snprintf(c, sizeof(c), "%02x:", fp_data[i]);
2620 fp = pstrcat(p, fp, &c, NULL);
2621 }
2622 fp[strlen(fp)-1] = '\0';
2623
2624 return fp;
2625 }
2626
2627 #if defined(PR_USE_OPENSSL_ECC)
2628 /* Returns the NID for the configured EVP_PKEY_EC key. */
2629 static int get_ecdsa_nid(EC_KEY *ec) {
2630 register unsigned int i;
2631 const EC_GROUP *key_group;
2632 EC_GROUP *new_group = NULL;
2633 BN_CTX *bn_ctx = NULL;
2634 int supported_ecdsa_nids[] = {
2635 NID_X9_62_prime256v1,
2636 NID_secp384r1,
2637 NID_secp521r1,
2638 -1
2639 };
2640 int nid;
2641
2642 if (ec == NULL) {
2643 errno = EINVAL;
2644 return -1;
2645 }
2646
2647 /* Since the EC group might be encoded in different ways, we need to try
2648 * different lookups to find the NID.
2649 *
2650 * First, we see if the EC group is encoded as a "named group" in the
2651 * private key.
2652 */
2653 key_group = EC_KEY_get0_group(ec);
2654 nid = EC_GROUP_get_curve_name(key_group);
2655 if (nid > 0) {
2656 return nid;
2657 }
2658
2659 /* Otherwise, we check to see if the group is encoded via explicit group
2660 * parameters in the private key.
2661 */
2662
2663 bn_ctx = BN_CTX_new();
2664 if (bn_ctx == NULL) {
2665 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2666 "error allocated BN_CTX: %s", sftp_crypto_get_errors());
2667 return -1;
2668 }
2669
2670 for (i = 0; supported_ecdsa_nids[i] != -1; i++) {
2671 new_group = EC_GROUP_new_by_curve_name(supported_ecdsa_nids[i]);
2672 if (new_group == NULL) {
2673 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2674 "error creating new EC_GROUP by curve name %d: %s",
2675 supported_ecdsa_nids[i], sftp_crypto_get_errors());
2676 BN_CTX_free(bn_ctx);
2677 return -1;
2678 }
2679
2680 if (EC_GROUP_cmp(key_group, new_group, bn_ctx) == 0) {
2681 /* We have a match. */
2682 break;
2683 }
2684
2685 EC_GROUP_free(new_group);
2686 new_group = NULL;
2687 }
2688
2689 BN_CTX_free(bn_ctx);
2690
2691 if (supported_ecdsa_nids[i] != -1) {
2692 EC_GROUP_set_asn1_flag(new_group, OPENSSL_EC_NAMED_CURVE);
2693 if (EC_KEY_set_group(ec, new_group) != 1) {
2694 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2695 "error setting EC group on key: %s", sftp_crypto_get_errors());
2696 EC_GROUP_free(new_group);
2697 return -1;
2698 }
2699
2700 EC_GROUP_free(new_group);
2701 }
2702
2703 return supported_ecdsa_nids[i];
2704 }
2705 #endif /* PR_USE_OPENSSL_ECC */
2706
2707 static int handle_hostkey(pool *p, EVP_PKEY *pkey,
2708 const unsigned char *key_data, uint32_t key_datalen,
2709 const char *file_path, const char *agent_path) {
2710
2711 switch (get_pkey_type(pkey)) {
2712 case EVP_PKEY_RSA: {
2713 #if OPENSSL_VERSION_NUMBER < 0x0090702fL
2714 /* In OpenSSL-0.9.7a and later, RSA blinding is turned on by default.
2715 * Thus if our OpenSSL is older than that, manually enable RSA
2716 * blinding.
2717 */
2718 RSA *rsa;
2719
2720 rsa = EVP_PKEY_get1_RSA(pkey);
2721 if (rsa) {
2722 if (RSA_blinding_on(rsa, NULL) != 1) {
2723 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2724 "error enabling RSA blinding for key '%s': %s",
2725 file_path ? file_path : agent_path,
2726 sftp_crypto_get_errors());
2727
2728 } else {
2729 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2730 "RSA blinding enabled for key '%s'",
2731 file_path ? file_path : agent_path);
2732 }
2733
2734 RSA_free(rsa);
2735 }
2736 #endif
2737
2738 if (sftp_rsa_hostkey != NULL) {
2739 /* If we have an existing RSA hostkey, free it up. */
2740 EVP_PKEY_free(sftp_rsa_hostkey->pkey);
2741 sftp_rsa_hostkey->pkey = NULL;
2742 sftp_rsa_hostkey->key_data = NULL;
2743 sftp_rsa_hostkey->key_datalen = 0;
2744 sftp_rsa_hostkey->file_path = NULL;
2745 sftp_rsa_hostkey->agent_path = NULL;
2746
2747 } else {
2748 sftp_rsa_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
2749 }
2750
2751 sftp_rsa_hostkey->key_type = SFTP_KEY_RSA;
2752 sftp_rsa_hostkey->pkey = pkey;
2753 sftp_rsa_hostkey->key_data = key_data;
2754 sftp_rsa_hostkey->key_datalen = key_datalen;
2755 sftp_rsa_hostkey->file_path = file_path;
2756 sftp_rsa_hostkey->agent_path = agent_path;
2757
2758 if (file_path != NULL) {
2759 pr_trace_msg(trace_channel, 4, "using '%s' as RSA hostkey (%d bits)",
2760 file_path, EVP_PKEY_bits(pkey));
2761
2762 } else if (agent_path != NULL) {
2763 pr_trace_msg(trace_channel, 4,
2764 "using RSA hostkey (%d bits) from SSH agent at '%s'",
2765 EVP_PKEY_bits(pkey), agent_path);
2766 }
2767
2768 break;
2769 }
2770
2771 case EVP_PKEY_DSA: {
2772 if (sftp_dsa_hostkey != NULL) {
2773 /* If we have an existing DSA hostkey, free it up. */
2774 EVP_PKEY_free(sftp_dsa_hostkey->pkey);
2775 sftp_dsa_hostkey->pkey = NULL;
2776 sftp_dsa_hostkey->key_data = NULL;
2777 sftp_dsa_hostkey->key_datalen = 0;
2778 sftp_dsa_hostkey->file_path = NULL;
2779 sftp_dsa_hostkey->agent_path = NULL;
2780
2781 } else {
2782 sftp_dsa_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
2783 }
2784
2785 sftp_dsa_hostkey->key_type = SFTP_KEY_DSA;
2786 sftp_dsa_hostkey->pkey = pkey;
2787 sftp_dsa_hostkey->key_data = key_data;
2788 sftp_dsa_hostkey->key_datalen = key_datalen;
2789 sftp_dsa_hostkey->file_path = file_path;
2790 sftp_dsa_hostkey->agent_path = agent_path;
2791
2792 if (file_path != NULL) {
2793 pr_trace_msg(trace_channel, 4, "using '%s' as DSA hostkey (%d bits)",
2794 file_path, EVP_PKEY_bits(pkey));
2795
2796 } else if (agent_path != NULL) {
2797 pr_trace_msg(trace_channel, 4,
2798 "using DSA hostkey (%d bits) from SSH agent at '%s'",
2799 EVP_PKEY_bits(pkey), agent_path);
2800 }
2801
2802 break;
2803 }
2804
2805 #if defined(PR_USE_OPENSSL_ECC)
2806 case EVP_PKEY_EC: {
2807 EC_KEY *ec;
2808 int ec_nid;
2809
2810 ec = EVP_PKEY_get1_EC_KEY(pkey);
2811 ec_nid = get_ecdsa_nid(ec);
2812 if (ec_nid < 0) {
2813 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2814 "unsupported NID in EC key, ignoring");
2815 EC_KEY_free(ec);
2816 EVP_PKEY_free(pkey);
2817 return -1;
2818 }
2819
2820 if (sftp_keys_validate_ecdsa_params(EC_KEY_get0_group(ec),
2821 EC_KEY_get0_public_key(ec)) < 0) {
2822 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2823 "error validating EC public key: %s", strerror(errno));
2824 EC_KEY_free(ec);
2825 EVP_PKEY_free(pkey);
2826 return -1;
2827 }
2828
2829 if (validate_ecdsa_private_key(ec)) {
2830 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2831 "error validating EC private key: %s", strerror(errno));
2832 EC_KEY_free(ec);
2833 EVP_PKEY_free(pkey);
2834 return -1;
2835 }
2836
2837 EC_KEY_free(ec);
2838
2839 switch (ec_nid) {
2840 case NID_X9_62_prime256v1:
2841 if (sftp_ecdsa256_hostkey != NULL) {
2842 /* If we have an existing 256-bit ECDSA hostkey, free it up. */
2843 EVP_PKEY_free(sftp_ecdsa256_hostkey->pkey);
2844 sftp_ecdsa256_hostkey->pkey = NULL;
2845 sftp_ecdsa256_hostkey->key_data = NULL;
2846 sftp_ecdsa256_hostkey->key_datalen = 0;
2847 sftp_ecdsa256_hostkey->file_path = NULL;
2848 sftp_ecdsa256_hostkey->agent_path = NULL;
2849
2850 } else {
2851 sftp_ecdsa256_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
2852 }
2853
2854 sftp_ecdsa256_hostkey->key_type = SFTP_KEY_ECDSA_256;
2855 sftp_ecdsa256_hostkey->pkey = pkey;
2856 sftp_ecdsa256_hostkey->key_data = key_data;
2857 sftp_ecdsa256_hostkey->key_datalen = key_datalen;
2858 sftp_ecdsa256_hostkey->file_path = file_path;
2859 sftp_ecdsa256_hostkey->agent_path = agent_path;
2860
2861 if (file_path != NULL) {
2862 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2863 "using '%s' as 256-bit ECDSA hostkey (%d bits)", file_path,
2864 EVP_PKEY_bits(pkey));
2865
2866 } else if (agent_path != NULL) {
2867 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2868 "using 256-bit ECDSA hostkey (%d bits) from SSH agent at '%s'",
2869 EVP_PKEY_bits(pkey), agent_path);
2870 }
2871
2872 break;
2873
2874 case NID_secp384r1:
2875 if (sftp_ecdsa384_hostkey != NULL) {
2876 /* If we have an existing 384-bit ECDSA hostkey, free it up. */
2877 EVP_PKEY_free(sftp_ecdsa384_hostkey->pkey);
2878 sftp_ecdsa384_hostkey->pkey = NULL;
2879 sftp_ecdsa384_hostkey->key_data = NULL;
2880 sftp_ecdsa384_hostkey->key_datalen = 0;
2881 sftp_ecdsa384_hostkey->file_path = NULL;
2882 sftp_ecdsa384_hostkey->agent_path = NULL;
2883
2884 } else {
2885 sftp_ecdsa384_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
2886 }
2887
2888 sftp_ecdsa384_hostkey->key_type = SFTP_KEY_ECDSA_384;
2889 sftp_ecdsa384_hostkey->pkey = pkey;
2890 sftp_ecdsa384_hostkey->key_data = key_data;
2891 sftp_ecdsa384_hostkey->key_datalen = key_datalen;
2892 sftp_ecdsa384_hostkey->file_path = file_path;
2893 sftp_ecdsa384_hostkey->agent_path = agent_path;
2894
2895 if (file_path != NULL) {
2896 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2897 "using '%s' as 384-bit ECDSA hostkey (%d bits)", file_path,
2898 EVP_PKEY_bits(pkey));
2899
2900 } else if (agent_path != NULL) {
2901 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2902 "using 384-bit ECDSA hostkey (%d bits) from SSH agent at '%s'",
2903 EVP_PKEY_bits(pkey), agent_path);
2904 }
2905
2906 break;
2907
2908 case NID_secp521r1:
2909 if (sftp_ecdsa521_hostkey != NULL) {
2910 /* If we have an existing 521-bit ECDSA hostkey, free it up. */
2911 EVP_PKEY_free(sftp_ecdsa521_hostkey->pkey);
2912 sftp_ecdsa521_hostkey->pkey = NULL;
2913 sftp_ecdsa521_hostkey->key_data = NULL;
2914 sftp_ecdsa521_hostkey->key_datalen = 0;
2915 sftp_ecdsa521_hostkey->file_path = NULL;
2916 sftp_ecdsa521_hostkey->agent_path = NULL;
2917
2918 } else {
2919 sftp_ecdsa521_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
2920 }
2921
2922 sftp_ecdsa521_hostkey->key_type = SFTP_KEY_ECDSA_521;
2923 sftp_ecdsa521_hostkey->pkey = pkey;
2924 sftp_ecdsa521_hostkey->key_data = key_data;
2925 sftp_ecdsa521_hostkey->key_datalen = key_datalen;
2926 sftp_ecdsa521_hostkey->file_path = file_path;
2927 sftp_ecdsa521_hostkey->agent_path = agent_path;
2928
2929 if (file_path != NULL) {
2930 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2931 "using '%s' as 521-bit ECDSA hostkey (%d bits)", file_path,
2932 EVP_PKEY_bits(pkey));
2933
2934 } else if (agent_path != NULL) {
2935 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2936 "using 521-bit hostkey (%d bits) from SSH agent at '%s'",
2937 EVP_PKEY_bits(pkey), agent_path);
2938 }
2939
2940 break;
2941 }
2942
2943 break;
2944 }
2945 #endif /* PR_USE_OPENSSL_ECC */
2946
2947 #if defined(HAVE_X448_OPENSSL)
2948 case EVP_PKEY_ED448: {
2949 unsigned char *privkey_data;
2950 size_t privkey_datalen;
2951
2952 privkey_datalen = (CURVE448_SIZE * 2);
2953 privkey_data = palloc(p, privkey_datalen);
2954 if (EVP_PKEY_get_raw_private_key(pkey, privkey_data,
2955 &privkey_datalen) != 1) {
2956 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2957 "error reading ED448 private key from '%s': %s", file_path,
2958 sftp_crypto_get_errors());
2959 EVP_PKEY_free(pkey);
2960 return -1;
2961 }
2962
2963 if (handle_ed448_hostkey(p, privkey_data, privkey_datalen,
2964 file_path) < 0) {
2965 EVP_PKEY_free(pkey);
2966 return -1;
2967 }
2968 break;
2969 }
2970 #endif /* HAVE_X448_OPENSSL */
2971
2972 default:
2973 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2974 "unknown private key type (%d), ignoring", get_pkey_type(pkey));
2975 EVP_PKEY_free(pkey);
2976 return -1;
2977 }
2978
2979 return 0;
2980 }
2981
2982 static int load_agent_hostkeys(pool *p, const char *path) {
2983 register unsigned int i;
2984 int accepted_nkeys = 0, res;
2985 array_header *key_list;
2986
2987 key_list = make_array(p, 0, sizeof(struct agent_key *));
2988
2989 res = sftp_agent_get_keys(p, path, key_list);
2990 if (res < 0) {
2991 int xerrno = errno;
2992
2993 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2994 "error loading hostkeys from SSH agent at '%s': %s", path,
2995 strerror(xerrno));
2996
2997 errno = xerrno;
2998 return -1;
2999 }
3000
3001 if (key_list->nelts == 0) {
3002 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3003 "SSH agent at '%s' returned no keys", path);
3004 errno = ENOENT;
3005 return -1;
3006 }
3007
3008 pr_trace_msg(trace_channel, 9, "processing %d keys from SSH agent at '%s'",
3009 key_list->nelts, path);
3010
3011 for (i = 0; i < key_list->nelts; i++) {
3012 EVP_PKEY *pkey;
3013 uint32_t len;
3014 struct agent_key *agent_key;
3015
3016 agent_key = ((struct agent_key **) key_list->elts)[i];
3017
3018 len = read_pkey_from_data(p, agent_key->key_data, agent_key->key_datalen,
3019 &pkey, NULL, NULL, FALSE);
3020 if (len == 0) {
3021 continue;
3022 }
3023
3024 if (handle_hostkey(p, pkey, agent_key->key_data, agent_key->key_datalen,
3025 NULL, path) == 0) {
3026 accepted_nkeys++;
3027 }
3028 }
3029
3030 if (accepted_nkeys == 0) {
3031 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3032 "none of the keys provided by the SSH agent at '%s' were acceptable",
3033 path);
3034 errno = EINVAL;
3035 return -1;
3036 }
3037
3038 pr_trace_msg(trace_channel, 9, "loaded %d keys from SSH agent at '%s'",
3039 accepted_nkeys, path);
3040
3041 /* Return the number of keys we successfully accept from the agent. */
3042 return accepted_nkeys;
3043 }
3044
3045 static struct openssh_cipher *get_openssh_cipher(const char *name) {
3046 register unsigned int i;
3047 struct openssh_cipher *cipher = NULL;
3048
3049 for (i = 0; ciphers[i].algo != NULL; i++) {
3050 if (strcmp(ciphers[i].algo, name) == 0) {
3051 cipher = &ciphers[i];
3052 break;
3053 }
3054 }
3055
3056 if (cipher == NULL) {
3057 errno = ENOENT;
3058 return NULL;
3059 }
3060
3061 if (cipher->get_cipher != NULL) {
3062 cipher->cipher = (cipher->get_cipher)();
3063 if (cipher->cipher != NULL) {
3064 return cipher;
3065 }
3066 }
3067
3068 /* The CTR algorithms may require our own implementation, not the OpenSSL
3069 * implementation.
3070 */
3071
3072 cipher->cipher = sftp_crypto_get_cipher(name, NULL, NULL, NULL);
3073 if (cipher->cipher == NULL) {
3074 errno = ENOSYS;
3075 return NULL;
3076 }
3077
3078 return cipher;
3079 }
3080
3081 static int decrypt_openssh_data(pool *p, const char *path,
3082 unsigned char *encrypted_data, uint32_t encrypted_len,
3083 const char *passphrase, struct openssh_cipher *cipher,
3084 const char *kdf_name, unsigned char *kdf_data, uint32_t kdf_len,
3085 unsigned char **decrypted_data, uint32_t *decrypted_len) {
3086 EVP_CIPHER_CTX *cipher_ctx = NULL;
3087 unsigned char *buf, *key, *iv, *salt_data;
3088 uint32_t buflen, key_len, rounds, salt_len;
3089 size_t passphrase_len;
3090
3091 if (strcmp(kdf_name, "none") == 0) {
3092 *decrypted_data = encrypted_data;
3093 *decrypted_len = encrypted_len;
3094
3095 return 0;
3096 }
3097
3098 if (strcmp(kdf_name, "bcrypt") != 0) {
3099 pr_trace_msg(trace_channel, 3,
3100 "'%s' key uses unsupported %s KDF", path, kdf_name);
3101 errno = ENOSYS;
3102 return -1;
3103 }
3104
3105 salt_len = sftp_msg_read_int(p, &kdf_data, &kdf_len);
3106 salt_data = sftp_msg_read_data(p, &kdf_data, &kdf_len, salt_len);
3107 rounds = sftp_msg_read_int(p, &kdf_data, &kdf_len);
3108
3109 pr_trace_msg(trace_channel, 9,
3110 "'%s' key %s KDF using %lu bytes of salt, %lu rounds", path,
3111 kdf_name, (unsigned long) salt_len, (unsigned long) rounds);
3112
3113 /* Compute the decryption key using the KDF and the passphrase. Note that
3114 * we derive the key AND the IV using this approach at the same time.
3115 */
3116 passphrase_len = strlen(passphrase);
3117 key_len = cipher->key_len + cipher->iv_len;
3118
3119 pr_trace_msg(trace_channel, 13,
3120 "generating %s decryption key using %s KDF (key len = %lu, IV len = %lu)",
3121 cipher->algo, kdf_name, (unsigned long) cipher->key_len,
3122 (unsigned long) cipher->iv_len);
3123 key = pcalloc(p, key_len);
3124 if (sftp_bcrypt_pbkdf2(p, passphrase, passphrase_len, salt_data, salt_len,
3125 rounds, key, key_len) < 0) {
3126 pr_trace_msg(trace_channel, 3,
3127 "error computing key using %s KDF: %s", kdf_name, strerror(errno));
3128 errno = EPERM;
3129 return -1;
3130 }
3131
3132 if (cipher->iv_len > 0) {
3133 iv = key + cipher->key_len;
3134
3135 } else {
3136 iv = NULL;
3137 }
3138
3139 cipher_ctx = EVP_CIPHER_CTX_new();
3140 if (cipher_ctx == NULL) {
3141 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3142 "error allocating cipher context: %s", sftp_crypto_get_errors());
3143 errno = EPERM;
3144 return -1;
3145 }
3146
3147 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
3148 defined(HAVE_LIBRESSL)
3149 EVP_CIPHER_CTX_init(cipher_ctx);
3150 #endif
3151
3152 #if defined(PR_USE_OPENSSL_EVP_CIPHERINIT_EX)
3153 if (EVP_CipherInit_ex(cipher_ctx, cipher->cipher, NULL, key, iv, 0) != 1) {
3154 #else
3155 if (EVP_CipherInit(cipher_ctx, cipher->cipher, key, iv, 0) != 1) {
3156 #endif /* PR_USE_OPENSSL_EVP_CIPHERINIT_EX */
3157 pr_trace_msg(trace_channel, 3,
3158 "error initializing %s cipher for decryption: %s", cipher->algo,
3159 sftp_crypto_get_errors());
3160 EVP_CIPHER_CTX_free(cipher_ctx);
3161 pr_memscrub(key, key_len);
3162 errno = EPERM;
3163 return -1;
3164 }
3165
3166 if (cipher->key_len > 0) {
3167 if (EVP_CIPHER_CTX_set_key_length(cipher_ctx, cipher->key_len) != 1) {
3168 pr_trace_msg(trace_channel, 3,
3169 "error setting key length (%lu bytes) for %s cipher for decryption: %s",
3170 (unsigned long) cipher->key_len, cipher->algo,
3171 sftp_crypto_get_errors());
3172 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3173 EVP_CIPHER_CTX_cleanup(cipher_ctx);
3174 #endif /* prior to OpenSSL-1.1.0 */
3175 EVP_CIPHER_CTX_free(cipher_ctx);
3176 pr_memscrub(key, key_len);
3177 errno = EPERM;
3178 return -1;
3179 }
3180 }
3181
3182 buflen = encrypted_len;
3183 buf = pcalloc(p, buflen);
3184
3185 /* TODO: this currently works because our data does NOT contain any extra
3186 * trailing AEAD bytes. Need to fix that in the future.
3187 */
3188
3189 if (EVP_Cipher(cipher_ctx, buf, encrypted_data, encrypted_len) < 0) {
3190 /* This might happen due to a wrong/bad passphrase. */
3191 pr_trace_msg(trace_channel, 3,
3192 "error decrypting %s data for key: %s", cipher->algo,
3193 sftp_crypto_get_errors());
3194 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3195 EVP_CIPHER_CTX_cleanup(cipher_ctx);
3196 #endif /* prior to OpenSSL-1.1.0 */
3197 EVP_CIPHER_CTX_free(cipher_ctx);
3198 pr_memscrub(key, key_len);
3199 pr_memscrub(buf, buflen);
3200 errno = EPERM;
3201 return -1;
3202 }
3203
3204 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3205 EVP_CIPHER_CTX_cleanup(cipher_ctx);
3206 #endif /* prior to OpenSSL-1.1.0 */
3207 EVP_CIPHER_CTX_free(cipher_ctx);
3208 pr_memscrub(key, key_len);
3209
3210 *decrypted_data = buf;
3211 *decrypted_len = buflen;
3212 return 0;
3213 }
3214
3215 /* See openssh-7.9p1/sshkey.c#sshkey_from_blob_internal(). */
3216 static int deserialize_openssh_private_key(pool *p, const char *path,
3217 unsigned char **data, uint32_t *data_len, enum sftp_key_type_e *key_type,
3218 EVP_PKEY **pkey, unsigned char **key, uint32_t *keylen) {
3219 uint32_t len = 0;
3220 const char *pkey_type;
3221 uint32_t public_keylen = 0, secret_keylen = 0;
3222 unsigned char *public_key = NULL, *secret_key = NULL;
3223 int have_extra_public_key = FALSE;
3224
3225 len = read_pkey_from_data(p, *data, *data_len, pkey, key_type, NULL, TRUE);
3226 if (len == 0) {
3227 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3228 "unsupported key type %d found in '%s'", *key_type, path);
3229 errno = EPERM;
3230 return -1;
3231 }
3232
3233 /* Advance our pointers for all of the data read from them. */
3234 (*data) += len;
3235 (*data_len) -= len;
3236
3237 switch (*key_type) {
3238 case SFTP_KEY_DSA:
3239 case SFTP_KEY_RSA:
3240 case SFTP_KEY_ECDSA_256:
3241 case SFTP_KEY_ECDSA_384:
3242 case SFTP_KEY_ECDSA_521:
3243 case SFTP_KEY_RSA_SHA256:
3244 case SFTP_KEY_RSA_SHA512:
3245 return 0;
3246
3247 case SFTP_KEY_ED25519:
3248 pkey_type = "ssh-ed25519";
3249 break;
3250
3251 case SFTP_KEY_ED448:
3252 pkey_type = "ssh-ed448";
3253 break;
3254
3255 case SFTP_KEY_UNKNOWN:
3256 default:
3257 *key = NULL;
3258 *keylen = 0;
3259 return 0;
3260 }
3261
3262 public_keylen = sftp_msg_read_int(p, data, data_len);
3263 public_key = sftp_msg_read_data(p, data, data_len, public_keylen);
3264 if (public_key == NULL) {
3265 pr_trace_msg(trace_channel, 2,
3266 "error reading %s key: invalid/supported key format", pkey_type);
3267 errno = EINVAL;
3268 return -1;
3269 }
3270
3271 secret_keylen = sftp_msg_read_int(p, data, data_len);
3272
3273 /* NOTE: PuTTY's puttygen adds the public key _again_, in the second half
3274 * of the secret key data, per commments in its
3275 * `sshecc.c#eddsa_new_priv_openssh` function. Thus if this secret key
3276 * length is larger than expected for Ed448 keys, only use the first half of
3277 * it. Ugh. This "divide in half" hack only works for these keys where the
3278 * private and public key sizes are the same.
3279 */
3280 switch (*key_type) {
3281 case SFTP_KEY_ED448:
3282 #if defined(HAVE_X448_OPENSSL) && defined(HAVE_SHA512_OPENSSL)
3283 if (secret_keylen > (CURVE448_SIZE + 1)) {
3284 have_extra_public_key = TRUE;
3285 secret_keylen /= 2;
3286 }
3287 #endif /* HAVE_X448_OPENSSL and HAVE_SHA512_OPENSSL */
3288 break;
3289
3290 default:
3291 break;
3292 }
3293
3294 secret_key = sftp_msg_read_data(p, data, data_len, secret_keylen);
3295 if (secret_key == NULL) {
3296 pr_trace_msg(trace_channel, 2,
3297 "error reading %s key: invalid/supported key format", pkey_type);
3298 errno = EINVAL;
3299 return -1;
3300 }
3301
3302 if (have_extra_public_key == TRUE) {
3303 /* Read (and ignore) the rest of the secret data */
3304 (void) sftp_msg_read_data(p, data, data_len, secret_keylen);
3305 }
3306
3307 /* The secret key is what we need to extract. */
3308 *key = secret_key;
3309 *keylen = secret_keylen;
3310
3311 return 0;
3312 }
3313
3314 static int decrypt_openssh_private_key(pool *p, const char *path,
3315 unsigned char *encrypted_data, uint32_t encrypted_len,
3316 const char *passphrase, struct openssh_cipher *cipher,
3317 const char *kdf_name, unsigned char *kdf_data, uint32_t kdf_len,
3318 enum sftp_key_type_e *key_type, EVP_PKEY **pkey, unsigned char **key,
3319 uint32_t *keylen) {
3320 unsigned char *decrypted_data = NULL, *decrypted_ptr = NULL;
3321 uint32_t check_bytes[2], decrypted_len = 0, decrypted_sz = 0;
3322 char *comment = NULL;
3323 int res;
3324 unsigned int i = 0;
3325
3326 res = decrypt_openssh_data(p, path, encrypted_data, encrypted_len, passphrase,
3327 cipher, kdf_name, kdf_data, kdf_len, &decrypted_data, &decrypted_len);
3328 if (res < 0) {
3329 pr_trace_msg(trace_channel, 6,
3330 "failed to decrypt '%s' using %s cipher: %s", path, cipher->algo,
3331 strerror(errno));
3332 errno = EINVAL;
3333 return -1;
3334 }
3335
3336 pr_trace_msg(trace_channel, 14,
3337 "decrypted %lu bytes into %lu bytes", (unsigned long) encrypted_len,
3338 (unsigned long) decrypted_len);
3339
3340 decrypted_ptr = decrypted_data;
3341 decrypted_sz = decrypted_len;
3342
3343 check_bytes[0] = sftp_msg_read_int(p, &decrypted_data, &decrypted_len);
3344 check_bytes[1] = sftp_msg_read_int(p, &decrypted_data, &decrypted_len);
3345
3346 if (check_bytes[0] != check_bytes[1]) {
3347 pr_trace_msg(trace_channel, 6,
3348 "'%s' has mismatched check bytes (%lu != %lu); wrong passphrase", path,
3349 (unsigned long) check_bytes[0], (unsigned long) check_bytes[1]);
3350 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3351 "unable to read hostkey '%s': wrong passphrase", path);
3352
3353 pr_memscrub(decrypted_ptr, decrypted_sz);
3354 errno = EINVAL;
3355 return -1;
3356 }
3357
3358 res = deserialize_openssh_private_key(p, path, &decrypted_data,
3359 &decrypted_len, key_type, pkey, key, keylen);
3360 if (res < 0) {
3361 int xerrno = errno;
3362
3363 pr_memscrub(decrypted_ptr, decrypted_sz);
3364 errno = xerrno;
3365 return -1;
3366 }
3367
3368 comment = sftp_msg_read_string(p, &decrypted_data, &decrypted_len);
3369 if (comment != NULL) {
3370 pr_trace_msg(trace_channel, 9,
3371 "'%s' comment: '%s'", path, comment);
3372 }
3373
3374 /* Verify the expected remaining padding. */
3375 for (i = 1; decrypted_len > 0; i++) {
3376 char padding;
3377
3378 pr_signals_handle();
3379
3380 padding = sftp_msg_read_byte(p, &decrypted_data, &decrypted_len);
3381 if (((unsigned int) padding) != (i & 0xFF)) {
3382 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3383 "'%s' key has invalid padding", path);
3384 pr_memscrub(decrypted_ptr, decrypted_sz);
3385 errno = EINVAL;
3386 return -1;
3387 }
3388 }
3389
3390 pr_memscrub(decrypted_ptr, decrypted_sz);
3391 return 0;
3392 }
3393
3394 static int unwrap_openssh_private_key(pool *p, const char *path,
3395 unsigned char *text, size_t text_len, const char *passphrase,
3396 enum sftp_key_type_e *key_type, EVP_PKEY **pkey, unsigned char **key,
3397 uint32_t *keylen) {
3398 char *cipher_name, *kdf_name;
3399 unsigned char *buf, *data = NULL, *kdf_data, *encrypted_data;
3400 size_t data_len = 0, magicsz;
3401 uint32_t buflen, kdf_len = 0, key_count = 0, encrypted_len = 0;
3402 struct openssh_cipher *cipher = NULL;
3403 int xerrno = 0;
3404
3405 data = decode_base64(p, text, text_len, &data_len);
3406 xerrno = errno;
3407
3408 if (data == NULL) {
3409 pr_trace_msg(trace_channel, 6,
3410 "error base64-decoding key '%s': %s", path, strerror(xerrno));
3411 errno = xerrno;
3412 return -1;
3413 }
3414
3415 magicsz = sizeof(SFTP_OPENSSH_MAGIC);
3416 if (data_len < magicsz) {
3417 pr_trace_msg(trace_channel, 6,
3418 "'%s' key base64-decoded data too short (%lu bytes < %lu minimum "
3419 "required)", path, (unsigned long) data_len, (unsigned long) magicsz);
3420 errno = EINVAL;
3421 return -1;
3422 }
3423
3424 if (memcmp(data, SFTP_OPENSSH_MAGIC, magicsz) != 0) {
3425 pr_trace_msg(trace_channel, 6,
3426 "'%s' key base64-decoded contains invalid magic value", path);
3427 errno = EINVAL;
3428 return -1;
3429 }
3430
3431 data += magicsz;
3432 data_len -= magicsz;
3433
3434 buf = data;
3435 buflen = data_len;
3436
3437 cipher_name = sftp_msg_read_string(p, &buf, &buflen);
3438 kdf_name = sftp_msg_read_string(p, &buf, &buflen);
3439
3440 kdf_len = sftp_msg_read_int(p, &buf, &buflen);
3441 kdf_data = sftp_msg_read_data(p, &buf, &buflen, kdf_len);
3442 key_count = sftp_msg_read_int(p, &buf, &buflen);
3443
3444 /* Ignore the public key */
3445 (void) sftp_msg_read_string(p, &buf, &buflen);
3446
3447 encrypted_len = sftp_msg_read_int(p, &buf, &buflen);
3448
3449 pr_trace_msg(trace_channel, 9,
3450 "'%s' key cipher = '%s', KDF = '%s' (%lu bytes KDF data), "
3451 "key count = %lu, (%lu bytes encrypted data)", path, cipher_name, kdf_name,
3452 (unsigned long) kdf_len, (unsigned long) key_count,
3453 (unsigned long) encrypted_len);
3454
3455 cipher = get_openssh_cipher(cipher_name);
3456 if (cipher == NULL) {
3457 pr_trace_msg(trace_channel, 6,
3458 "'%s' key uses unexpected/unsupported cipher (%s)", path, cipher_name);
3459 errno = EPERM;
3460 return -1;
3461 }
3462
3463 if ((passphrase == NULL ||
3464 strlen(passphrase) == 0) &&
3465 strcmp(cipher_name, "none") != 0) {
3466 pr_trace_msg(trace_channel, 6,
3467 "'%s' key requires passphrase for cipher (%s)", path, cipher_name);
3468 errno = EPERM;
3469 return -1;
3470 }
3471
3472 /* We only support the "none" and "bcrypt" KDFs at present. */
3473 if (strcmp(kdf_name, "bcrypt") != 0 &&
3474 strcmp(kdf_name, "none") != 0) {
3475 pr_trace_msg(trace_channel, 6,
3476 "'%s' key encrypted using unsupported KDF '%s'", path, kdf_name);
3477 errno = EPERM;
3478 return -1;
3479 }
3480
3481 /* If our KDF is "none" and our cipher is NOT "none", we have a problem. */
3482 if (strcmp(kdf_name, "none") == 0 &&
3483 strcmp(cipher_name, "none") != 0) {
3484 pr_trace_msg(trace_channel, 6,
3485 "'%s' key encrypted using mismatched KDF and cipher algorithms: "
3486 "KDF '%s', cipher '%s'", path, kdf_name, cipher_name);
3487 errno = EPERM;
3488 return -1;
3489 }
3490
3491 /* OpenSSH only supports one key at present. Huh. */
3492 if (key_count != 1) {
3493 pr_trace_msg(trace_channel, 6,
3494 "'%s' key includes unexpected/unsupported key count (%lu)",
3495 path, (unsigned long) key_count);
3496 errno = EPERM;
3497 return -1;
3498 }
3499
3500 /* XXX Should we enforce that the KDF data be empty for the "none" KDF? */
3501 if (strcmp(kdf_name, "none") == 0 &&
3502 kdf_len > 0) {
3503 pr_trace_msg(trace_channel, 6,
3504 "'%s' key uses KDF 'none', but contains unexpected %lu bytes "
3505 "of KDF options", path, (unsigned long) kdf_len);
3506 }
3507
3508 if (buflen < encrypted_len) {
3509 pr_trace_msg(trace_channel, 6,
3510 "'%s' key declares %lu bytes of encrypted data, but has "
3511 "only %lu bytes remaining", path, (unsigned long) encrypted_len,
3512 (unsigned long) buflen);
3513 errno = EPERM;
3514 return -1;
3515 }
3516
3517 if (encrypted_len < cipher->blocksz ||
3518 (encrypted_len % cipher->blocksz) != 0) {
3519 pr_trace_msg(trace_channel, 6,
3520 "'%s' key declares %lu bytes of encrypted data, which is invalid for "
3521 "the %s cipher block size (%lu bytes)", path,
3522 (unsigned long) encrypted_len, cipher_name,
3523 (unsigned long) cipher->blocksz);
3524 errno = EPERM;
3525 return -1;
3526 }
3527
3528 if (buflen < (encrypted_len + cipher->auth_len)) {
3529 pr_trace_msg(trace_channel, 6,
3530 "'%s' key declares %lu bytes of encrypted data and %lu bytes of auth "
3531 "data, but has only %lu bytes remaining", path,
3532 (unsigned long) encrypted_len, (unsigned long) cipher->auth_len,
3533 (unsigned long) buflen);
3534 errno = EPERM;
3535 return -1;
3536 }
3537
3538 encrypted_data = sftp_msg_read_data(p, &buf, &buflen, encrypted_len);
3539
3540 #if 0
3541 if (cipher->auth_len > 0) {
3542 /* Read (and ignore) any auth data (for AEAD ciphers). */
3543 (void) sftp_msg_read_data(p, &encrypted_data, &encrypted_len,
3544 cipher->auth_len);
3545 }
3546
3547 /* We should have used all of the encrypted data, with none left over. */
3548 if (encrypted_len != 0) {
3549 pr_trace_msg(trace_channel, 3,
3550 "'%s' key provided too much data (%lu bytes remaining unexpectedly)",
3551 path, (unsigned long) encrypted_len);
3552 pr_memscrub(buf, buflen);
3553 errno = EINVAL;
3554 return -1;
3555 }
3556 #endif
3557
3558 return decrypt_openssh_private_key(p, path, encrypted_data, encrypted_len,
3559 passphrase, cipher, kdf_name, kdf_data, kdf_len, key_type, pkey, key,
3560 keylen);
3561 }
3562
3563 static int read_openssh_private_key(pool *p, const char *path, int fd,
3564 const char *passphrase, enum sftp_key_type_e *key_type, EVP_PKEY **pkey,
3565 unsigned char **key, uint32_t *keylen) {
3566 struct stat st;
3567 pool *tmp_pool;
3568 unsigned char *decoded_buf, *decoded_ptr, *input_buf, *input_ptr;
3569 unsigned char *tmp_key = NULL;
3570 int res, xerrno = 0;
3571 size_t decoded_len, input_len;
3572 off_t input_sz;
3573
3574 if (p == NULL ||
3575 path == NULL ||
3576 fd < 0 ||
3577 key == NULL ||
3578 keylen == NULL) {
3579 errno = EINVAL;
3580 return -1;
3581 }
3582
3583 if (fstat(fd, &st) < 0) {
3584 return -1;
3585 }
3586
3587 tmp_pool = make_sub_pool(p);
3588
3589 /* Read the entire file into memory. */
3590 /* TODO: Impose maximum size limit for this treatment? */
3591 input_sz = st.st_size;
3592 input_ptr = input_buf = palloc(tmp_pool, input_sz);
3593 input_len = 0;
3594
3595 res = read(fd, input_buf, input_sz);
3596 xerrno = errno;
3597
3598 while (res != 0) {
3599 pr_signals_handle();
3600
3601 if (res < 0) {
3602 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": error reading '%s': %s",
3603 path, strerror(xerrno));
3604 pr_memscrub(input_ptr, st.st_size);
3605 destroy_pool(tmp_pool);
3606 errno = xerrno;
3607 return -1;
3608 }
3609
3610 input_buf += res;
3611 input_len += res;
3612 input_sz -= res;
3613
3614 res = read(fd, input_buf, input_sz);
3615 xerrno = errno;
3616 }
3617
3618 input_buf = input_ptr;
3619
3620 /* Now we read from the input buffer into a buffer for decoding, for use
3621 * in the unwrapping process.
3622 */
3623 decoded_ptr = decoded_buf = palloc(tmp_pool, input_len);
3624 decoded_len = 0;
3625
3626 /* We know (due to the OpenSSH private key check) that the first bytes
3627 * match the expected start, and that the last bytes match the expected end.
3628 * So skip past them.
3629 */
3630 input_buf += SFTP_OPENSSH_BEGIN_LEN;
3631 input_len -= (SFTP_OPENSSH_BEGIN_LEN + SFTP_OPENSSH_END_LEN);
3632
3633 while (input_len > 0) {
3634 char ch;
3635
3636 pr_signals_handle();
3637
3638 ch = *input_buf;
3639
3640 /* Skip whitespace */
3641 if (ch != '\r' &&
3642 ch != '\n') {
3643 *decoded_buf++ = ch;
3644 decoded_len++;
3645 }
3646
3647 input_buf++;
3648 input_len--;
3649 }
3650
3651 res = unwrap_openssh_private_key(tmp_pool, path, decoded_ptr, decoded_len,
3652 passphrase, key_type, pkey, &tmp_key, keylen);
3653 xerrno = errno;
3654
3655 if (res < 0) {
3656 pr_memscrub(input_ptr, st.st_size);
3657 destroy_pool(tmp_pool);
3658 errno = xerrno;
3659 return -1;
3660 }
3661
3662 /* At this point, our unwrapped key is allocated out of the temporary pool.
3663 * We need to copy it to memory out of the longer-lived pool given to us.
3664 */
3665 if (*keylen > 0) {
3666 *key = palloc(p, *keylen);
3667 memcpy(*key, tmp_key, *keylen);
3668
3669 pr_memscrub(tmp_key, *keylen);
3670 }
3671
3672 destroy_pool(tmp_pool);
3673 return 0;
3674 }
3675
3676 #if defined(PR_USE_SODIUM)
3677 static int handle_ed25519_hostkey(pool *p, const unsigned char *key_data,
3678 uint32_t key_datalen, const char *file_path) {
3679 unsigned char *public_key;
3680
3681 if (sftp_ed25519_hostkey != NULL) {
3682 /* If we have an existing ED25519 hostkey, free it up. */
3683 pr_memscrub(sftp_ed25519_hostkey->ed25519_secret_key,
3684 sftp_ed25519_hostkey->ed25519_secret_keylen);
3685 sftp_ed25519_hostkey->ed25519_secret_key = NULL;
3686 sftp_ed25519_hostkey->ed25519_secret_keylen = 0;
3687
3688 pr_memscrub(sftp_ed25519_hostkey->ed25519_public_key,
3689 sftp_ed25519_hostkey->ed25519_public_keylen);
3690 sftp_ed25519_hostkey->ed25519_public_key = NULL;
3691 sftp_ed25519_hostkey->ed25519_public_keylen = 0;
3692
3693 sftp_ed25519_hostkey->file_path = NULL;
3694 sftp_ed25519_hostkey->agent_path = NULL;
3695
3696 } else {
3697 sftp_ed25519_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
3698 }
3699
3700 sftp_ed25519_hostkey->key_type = SFTP_KEY_ED25519;
3701 sftp_ed25519_hostkey->ed25519_secret_key = (unsigned char *) key_data;
3702 sftp_ed25519_hostkey->ed25519_secret_keylen = key_datalen;
3703
3704 /* Use the secret key to get the public key. */
3705 public_key = palloc(p, crypto_sign_ed25519_PUBLICKEYBYTES);
3706 if (crypto_sign_ed25519_sk_to_pk(public_key, key_data) != 0) {
3707 return -1;
3708 }
3709
3710 sftp_ed25519_hostkey->ed25519_public_key = public_key;
3711 sftp_ed25519_hostkey->ed25519_public_keylen = crypto_sign_ed25519_PUBLICKEYBYTES;
3712
3713 sftp_ed25519_hostkey->file_path = file_path;
3714 pr_trace_msg(trace_channel, 4, "using '%s' as Ed25519 hostkey", file_path);
3715
3716 return 0;
3717 }
3718 #endif /* PR_USE_SODIUM */
3719
3720 #if defined(HAVE_X448_OPENSSL)
3721 static int handle_ed448_hostkey(pool *p, const unsigned char *key_data,
3722 uint32_t key_datalen, const char *file_path) {
3723 unsigned char *public_key;
3724 EVP_PKEY *pkey = NULL;
3725 size_t public_keylen = 0;
3726
3727 if (sftp_ed448_hostkey != NULL) {
3728 /* If we have an existing ED448 hostkey, free it up. */
3729 pr_memscrub(sftp_ed448_hostkey->ed448_secret_key,
3730 sftp_ed448_hostkey->ed448_secret_keylen);
3731 sftp_ed448_hostkey->ed448_secret_key = NULL;
3732 sftp_ed448_hostkey->ed448_secret_keylen = 0;
3733
3734 pr_memscrub(sftp_ed448_hostkey->ed448_public_key,
3735 sftp_ed448_hostkey->ed448_public_keylen);
3736 sftp_ed448_hostkey->ed448_public_key = NULL;
3737 sftp_ed448_hostkey->ed448_public_keylen = 0;
3738
3739 sftp_ed448_hostkey->file_path = NULL;
3740 sftp_ed448_hostkey->agent_path = NULL;
3741
3742 } else {
3743 sftp_ed448_hostkey = pcalloc(p, sizeof(struct sftp_hostkey));
3744 }
3745
3746 sftp_ed448_hostkey->key_type = SFTP_KEY_ED448;
3747 sftp_ed448_hostkey->ed448_secret_key = (unsigned char *) key_data;
3748 sftp_ed448_hostkey->ed448_secret_keylen = key_datalen;
3749
3750 pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED448, NULL,
3751 sftp_ed448_hostkey->ed448_secret_key,
3752 sftp_ed448_hostkey->ed448_secret_keylen);
3753 if (pkey == NULL) {
3754 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3755 "error initializing Ed448 private key: %s", sftp_crypto_get_errors());
3756 return -1;
3757 }
3758
3759 /* Use the secret key to get the public key. */
3760 public_keylen = (CURVE448_SIZE * 2);
3761 public_key = palloc(p, public_keylen);
3762 if (EVP_PKEY_get_raw_public_key(pkey, public_key, &public_keylen) != 1) {
3763 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3764 "error obtaining Ed448 public key: %s", sftp_crypto_get_errors());
3765 EVP_PKEY_free(pkey);
3766 return -1;
3767 }
3768
3769 EVP_PKEY_free(pkey);
3770
3771 sftp_ed448_hostkey->ed448_public_key = public_key;
3772 sftp_ed448_hostkey->ed448_public_keylen = public_keylen;
3773
3774 sftp_ed448_hostkey->file_path = file_path;
3775 pr_trace_msg(trace_channel, 4, "using '%s' as Ed448 hostkey", file_path);
3776
3777 return 0;
3778 }
3779 #endif /* HAVE_X448_OPENSSL */
3780
3781 static int load_openssh_hostkey(pool *p, const char *path, int fd) {
3782 const char *passphrase = NULL;
3783 enum sftp_key_type_e key_type = SFTP_KEY_UNKNOWN;
3784 EVP_PKEY *pkey = NULL;
3785 unsigned char *key = NULL;
3786 uint32_t keylen = 0;
3787 int res;
3788
3789 if (server_pkey != NULL) {
3790 passphrase = server_pkey->host_pkey;
3791 }
3792
3793 res = read_openssh_private_key(p, path, fd, passphrase, &key_type, &pkey,
3794 &key, &keylen);
3795 if (res < 0) {
3796 return -1;
3797 }
3798
3799 switch (key_type) {
3800 #if defined(PR_USE_SODIUM)
3801 case SFTP_KEY_ED25519:
3802 res = handle_ed25519_hostkey(p, key, keylen, path);
3803 break;
3804 #endif /* PR_USE_SODIUM */
3805
3806 #if defined(HAVE_X448_OPENSSL)
3807 case SFTP_KEY_ED448:
3808 res = handle_ed448_hostkey(p, key, keylen, path);
3809 break;
3810 #endif /* HAVE_X448_OPENSSL */
3811
3812 default:
3813 res = handle_hostkey(p, pkey, NULL, 0, path, NULL);
3814 break;
3815 }
3816
3817 return res;
3818 }
3819
3820 static void free_hostkey_bio(BIO *bio) {
3821 char *data = NULL;
3822 long datalen = 0;
3823
3824 /* "Rewind" the pointer to the start of the buffer, for scrubbing. */
3825 BIO_reset(bio);
3826
3827 datalen = BIO_get_mem_data(bio, &data);
3828 if (data != NULL &&
3829 datalen > 0) {
3830 pr_memscrub(data, datalen);
3831 }
3832
3833 BIO_free(bio);
3834 }
3835
3836 static BIO *load_file_hostkey_bio(pool *p, int fd) {
3837 int res, xerrno;
3838 BIO *bio = NULL, *readonly_bio = NULL;
3839 struct stat st;
3840 unsigned char *buf = NULL;
3841 size_t bufsz;
3842 char *data = NULL, *ptr = NULL;
3843 long datalen = 0;
3844
3845 memset(&st, 0, sizeof(st));
3846 res = fstat(fd, &st);
3847 if (res < 0) {
3848 return NULL;
3849 }
3850
3851 bufsz = st.st_blksize;
3852 buf = palloc(p, bufsz);
3853 #if defined(PR_USE_OPENSSL_BIO_SECMEM)
3854 bio = BIO_new(BIO_s_secmem());
3855 #else
3856 bio = BIO_new(BIO_s_mem());
3857 #endif /* PR_USE_OPENSSL_BIO_SECMEM */
3858
3859 res = read(fd, buf, bufsz);
3860 xerrno = errno;
3861
3862 if (res < 0) {
3863 BIO_free(bio);
3864
3865 errno = xerrno;
3866 return NULL;
3867 }
3868
3869 while (res > 0) {
3870 pr_signals_handle();
3871
3872 BIO_write(bio, buf, res);
3873 pr_memscrub(buf, res);
3874
3875 res = read(fd, buf, bufsz);
3876 xerrno = errno;
3877
3878 if (res < 0) {
3879 BIO_free(bio);
3880
3881 errno = xerrno;
3882 return NULL;
3883 }
3884 }
3885
3886 /* Now we create a read-only memory BIO for this hostkey data. This is
3887 * specifically for the on-startup use case, where the admin might mistype
3888 * the passphrase for a passphrase-protected hostkey, and need to retry
3889 * the decryption process. The data in a normal read/write memory BIO is
3890 * consumed from the BIO on read, meaning that that BIO would not be usable
3891 * for a subsequent decryption attempt.
3892 */
3893 datalen = BIO_get_mem_data(bio, &ptr);
3894 if (ptr == NULL ||
3895 datalen == 0) {
3896 BIO_free(bio);
3897
3898 errno = EIO;
3899 return NULL;
3900 }
3901
3902 /* Make a copy of the data, so that we can destroy the original BIO without
3903 * losing this data.
3904 */
3905 data = palloc(p, datalen);
3906 memcpy(data, ptr, datalen);
3907 BIO_free(bio);
3908
3909 readonly_bio = BIO_new_mem_buf(data, datalen);
3910 return readonly_bio;
3911 }
3912
3913 static int load_file_hostkey(pool *p, const char *path) {
3914 int fd, xerrno = 0, openssh_format = FALSE, public_key_format = FALSE;
3915 pool *tmp_pool = NULL;
3916 BIO *bio;
3917 EVP_PKEY *pkey;
3918
3919 pr_signals_block();
3920 PRIVS_ROOT
3921
3922 /* XXX Would we ever want to allow host keys to be read from FIFOs? If
3923 * so, we would need to include the O_NONBLOCK flag here.
3924 */
3925 fd = open(path, O_RDONLY, 0);
3926 xerrno = errno;
3927 PRIVS_RELINQUISH
3928 pr_signals_unblock();
3929
3930 if (fd < 0) {
3931 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3932 "error reading '%s': %s", path, strerror(xerrno));
3933 errno = xerrno;
3934 return -1;
3935 }
3936
3937 if (has_req_perms(fd, path) < 0) {
3938 xerrno = errno;
3939
3940 if (xerrno == EACCES) {
3941 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3942 "'%s' is accessible by group or world, which is not allowed", path);
3943
3944 } else {
3945 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3946 "error checking '%s' perms: %s", path, strerror(xerrno));
3947 }
3948
3949 (void) close(fd);
3950 errno = xerrno;
3951 return -1;
3952 }
3953
3954 if (server_pkey == NULL) {
3955 server_pkey = lookup_pkey();
3956 }
3957
3958 /* Make sure this is not a public key inadvertently configured as a hostkey.
3959 */
3960 public_key_format = is_public_key(fd);
3961 if (public_key_format == TRUE) {
3962 pr_trace_msg(trace_channel, 3, "hostkey file '%s' uses a public key format",
3963 path);
3964 (void) pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
3965 ": unable to use public key '%s' for SFTPHostKey", path);
3966 (void) close(fd);
3967 errno = EINVAL;
3968 return -1;
3969 }
3970
3971 /* If this happens to be in the OpenSSH private key format, handle it
3972 * separately.
3973 */
3974 openssh_format = is_openssh_private_key(fd);
3975 if (openssh_format == TRUE) {
3976 int res;
3977
3978 pr_trace_msg(trace_channel, 9, "hostkey file '%s' uses OpenSSH key format",
3979 path);
3980
3981 res = load_openssh_hostkey(p, path, fd);
3982 xerrno = errno;
3983
3984 (void) close(fd);
3985 errno = xerrno;
3986 return res;
3987 }
3988
3989 /* Rather than using OpenSSL's PEM_read_PrivateKey and the underlying C
3990 * library's FILE routines, dealing with unbuffered file handles and
3991 * byte-by-byte reads from OpenSSL, we instead provision the file data
3992 * into a memory BIO, and let OpenSSL read from that.
3993 *
3994 * This allows OpenSSL to maintain its byte-by-byte reads, while we read
3995 * the file data using filesystem block-sized reads.
3996 */
3997 tmp_pool = make_sub_pool(p);
3998 pr_pool_tag(tmp_pool, "SFTP hostkey BIO pool");
3999
4000 bio = load_file_hostkey_bio(tmp_pool, fd);
4001 if (bio == NULL) {
4002 xerrno = errno;
4003
4004 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4005 "error reading data from fd %d: %s", fd, strerror(xerrno));
4006 (void) close(fd);
4007 destroy_pool(tmp_pool);
4008
4009 errno = xerrno;
4010 return -1;
4011 }
4012
4013 if (server_pkey != NULL) {
4014 pkey = PEM_read_bio_PrivateKey(bio, NULL, pkey_cb, (void *) server_pkey);
4015
4016 } else {
4017 /* Assume that the key is not passphrase-protected. */
4018 pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, "");
4019 }
4020
4021 free_hostkey_bio(bio);
4022 destroy_pool(tmp_pool);
4023
4024 if (pkey == NULL) {
4025 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4026 "error reading private key from '%s': %s", path,
4027 sftp_crypto_get_errors());
4028 return -1;
4029 }
4030
4031 return handle_hostkey(p, pkey, NULL, 0, path, NULL);
4032 }
4033
4034 int sftp_keys_get_hostkey(pool *p, const char *path) {
4035 int res;
4036
4037 /* Check whether we are to load keys from a file on disk, or from an
4038 * SSH agent.
4039 */
4040 if (strncmp(path, "agent:", 6) != 0) {
4041 pr_trace_msg(trace_channel, 9, "loading host key from file '%s'", path);
4042 res = load_file_hostkey(p, path);
4043
4044 } else {
4045 const char *agent_path;
4046
4047 /* Skip past the "agent:" prefix. */
4048 agent_path = (path + 6);
4049
4050 pr_trace_msg(trace_channel, 9, "loading host keys from SSH agent at '%s'",
4051 agent_path);
4052 res = load_agent_hostkeys(p, agent_path);
4053 }
4054
4055 return res;
4056 }
4057
4058 static int get_rsa_hostkey_data(pool *p, unsigned char **buf,
4059 unsigned char **ptr, uint32_t *buflen) {
4060 RSA *rsa;
4061 const BIGNUM *rsa_n = NULL, *rsa_e = NULL;
4062
4063 rsa = EVP_PKEY_get1_RSA(sftp_rsa_hostkey->pkey);
4064 if (rsa == NULL) {
4065 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4066 "error using RSA hostkey: %s", sftp_crypto_get_errors());
4067 return -1;
4068 }
4069
4070 /* XXX Is this buffer large enough? Too large? */
4071 *ptr = *buf = palloc(p, *buflen);
4072 sftp_msg_write_string(buf, buflen, "ssh-rsa");
4073
4074 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4075 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4076 RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
4077 #else
4078 rsa_e = rsa->e;
4079 rsa_n = rsa->n;
4080 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4081 sftp_msg_write_mpint(buf, buflen, rsa_e);
4082 sftp_msg_write_mpint(buf, buflen, rsa_n);
4083
4084 RSA_free(rsa);
4085 return 0;
4086 }
4087
4088 #if !defined(OPENSSL_NO_DSA)
4089 static int get_dsa_hostkey_data(pool *p, unsigned char **buf,
4090 unsigned char **ptr, uint32_t *buflen) {
4091 DSA *dsa;
4092 const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL, *dsa_pub_key = NULL;
4093
4094 dsa = EVP_PKEY_get1_DSA(sftp_dsa_hostkey->pkey);
4095 if (dsa == NULL) {
4096 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4097 "error using DSA hostkey: %s", sftp_crypto_get_errors());
4098 return -1;
4099 }
4100
4101 /* XXX Is this buffer large enough? Too large? */
4102 *ptr = *buf = palloc(p, *buflen);
4103 sftp_msg_write_string(buf, buflen, "ssh-dss");
4104
4105 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4106 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4107 DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
4108 DSA_get0_key(dsa, &dsa_pub_key, NULL);
4109 #else
4110 dsa_p = dsa->p;
4111 dsa_q = dsa->q;
4112 dsa_g = dsa->g;
4113 dsa_pub_key = dsa->pub_key;;
4114 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4115 sftp_msg_write_mpint(buf, buflen, dsa_p);
4116 sftp_msg_write_mpint(buf, buflen, dsa_q);
4117 sftp_msg_write_mpint(buf, buflen, dsa_g);
4118 sftp_msg_write_mpint(buf, buflen, dsa_pub_key);
4119
4120 DSA_free(dsa);
4121 return 0;
4122 }
4123 #endif /* !OPENSSL_NO_DSA */
4124
4125 #if defined(PR_USE_OPENSSL_ECC)
4126 static int get_ecdsa_hostkey_data(pool *p, struct sftp_hostkey *hostkey,
4127 const char *algo, const char *curve, unsigned char **buf,
4128 unsigned char **ptr, uint32_t *buflen) {
4129 EC_KEY *ec;
4130
4131 ec = EVP_PKEY_get1_EC_KEY(hostkey->pkey);
4132 if (ec == NULL) {
4133 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4134 "error using %s hostkey: %s", algo, sftp_crypto_get_errors());
4135 return -1;
4136 }
4137
4138 /* XXX Is this buffer large enough? Too large? */
4139 *ptr = *buf = palloc(p, *buflen);
4140 sftp_msg_write_string(buf, buflen, algo);
4141 sftp_msg_write_string(buf, buflen, curve);
4142 sftp_msg_write_ecpoint(buf, buflen, EC_KEY_get0_group(ec),
4143 EC_KEY_get0_public_key(ec));
4144
4145 EC_KEY_free(ec);
4146 return 0;
4147 }
4148 #endif /* PR_USE_OPENSSL_ECC */
4149
4150 #if defined(PR_USE_SODIUM)
4151 static int get_ed25519_hostkey_data(pool *p, unsigned char **buf,
4152 unsigned char **ptr, uint32_t *buflen) {
4153
4154 /* XXX Is this buffer large enough? Too large? */
4155 *ptr = *buf = palloc(p, *buflen);
4156 sftp_msg_write_string(buf, buflen, "ssh-ed25519");
4157 sftp_msg_write_data(buf, buflen, sftp_ed25519_hostkey->ed25519_public_key,
4158 sftp_ed25519_hostkey->ed25519_public_keylen, TRUE);
4159
4160 return 0;
4161 }
4162 #endif /* PR_USE_SODIUM */
4163
4164 #if defined(HAVE_X448_OPENSSL)
4165 static int get_ed448_hostkey_data(pool *p, unsigned char **buf,
4166 unsigned char **ptr, uint32_t *buflen) {
4167
4168 /* XXX Is this buffer large enough? Too large? */
4169 *ptr = *buf = palloc(p, *buflen);
4170 sftp_msg_write_string(buf, buflen, "ssh-ed448");
4171 sftp_msg_write_data(buf, buflen, sftp_ed448_hostkey->ed448_public_key,
4172 sftp_ed448_hostkey->ed448_public_keylen, TRUE);
4173
4174 return 0;
4175 }
4176 #endif /* HAVE_X448_OPENSSL */
4177
4178 const unsigned char *sftp_keys_get_hostkey_data(pool *p,
4179 enum sftp_key_type_e key_type, uint32_t *datalen) {
4180 unsigned char *buf = NULL, *ptr = NULL;
4181 uint32_t buflen = SFTP_DEFAULT_HOSTKEY_SZ;
4182 int res;
4183
4184 switch (key_type) {
4185 case SFTP_KEY_RSA:
4186 case SFTP_KEY_RSA_SHA256:
4187 case SFTP_KEY_RSA_SHA512: {
4188 res = get_rsa_hostkey_data(p, &buf, &ptr, &buflen);
4189 if (res < 0) {
4190 return NULL;
4191 }
4192
4193 break;
4194 }
4195
4196 #if !defined(OPENSSL_NO_DSA)
4197 case SFTP_KEY_DSA: {
4198 res = get_dsa_hostkey_data(p, &buf, &ptr, &buflen);
4199 if (res < 0) {
4200 return NULL;
4201 }
4202
4203 break;
4204 }
4205 #endif /* !OPENSSL_NO_DSA */
4206
4207 #if defined(PR_USE_OPENSSL_ECC)
4208 case SFTP_KEY_ECDSA_256: {
4209 res = get_ecdsa_hostkey_data(p, sftp_ecdsa256_hostkey,
4210 "ecdsa-sha2-nistp256", "nistp256", &buf, &ptr, &buflen);
4211 if (res < 0) {
4212 return NULL;
4213 }
4214
4215 break;
4216 }
4217
4218 case SFTP_KEY_ECDSA_384: {
4219 res = get_ecdsa_hostkey_data(p, sftp_ecdsa384_hostkey,
4220 "ecdsa-sha2-nistp384", "nistp384", &buf, &ptr, &buflen);
4221 if (res < 0) {
4222 return NULL;
4223 }
4224
4225 break;
4226 }
4227
4228 case SFTP_KEY_ECDSA_521: {
4229 res = get_ecdsa_hostkey_data(p, sftp_ecdsa521_hostkey,
4230 "ecdsa-sha2-nistp521", "nistp521", &buf, &ptr, &buflen);
4231 if (res < 0) {
4232 return NULL;
4233 }
4234
4235 break;
4236 }
4237 #endif /* PR_USE_OPENSSL_ECC */
4238
4239 #if defined(PR_USE_SODIUM)
4240 case SFTP_KEY_ED25519: {
4241 res = get_ed25519_hostkey_data(p, &buf, &ptr, &buflen);
4242 if (res < 0) {
4243 return NULL;
4244 }
4245
4246 break;
4247 }
4248 #endif /* PR_USE_SODIUM */
4249
4250 #if defined(HAVE_X448_OPENSSL)
4251 case SFTP_KEY_ED448: {
4252 res = get_ed448_hostkey_data(p, &buf, &ptr, &buflen);
4253 if (res < 0) {
4254 return NULL;
4255 }
4256
4257 break;
4258 }
4259 #endif /* HAVE_X448_OPENSSL */
4260
4261 default:
4262 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4263 "unknown/unsupported key type (%d) requested, ignoring", key_type);
4264 return NULL;
4265 }
4266
4267 *datalen = SFTP_DEFAULT_HOSTKEY_SZ - buflen;
4268
4269 /* If the caller provided a pool, make a copy of the data from the
4270 * given pool, and return the copy. Make sure to scrub the original
4271 * after making the copy.
4272 *
4273 * Note that we do this copy, even though we use the given pool, since
4274 * we only know the actual size of the data after the fact. And we need
4275 * to provide the size of the data to the caller, NOT the optimistic size
4276 * we allocate out of the pool for writing the data in the first place.
4277 * Hence the copy.
4278 */
4279 buf = palloc(p, *datalen);
4280 memcpy(buf, ptr, *datalen);
4281
4282 pr_memscrub(ptr, *datalen);
4283 return buf;
4284 }
4285
4286 int sftp_keys_clear_dsa_hostkey(void) {
4287 if (sftp_dsa_hostkey == NULL) {
4288 errno = ENOENT;
4289 return -1;
4290 }
4291
4292 if (sftp_dsa_hostkey->pkey != NULL) {
4293 EVP_PKEY_free(sftp_dsa_hostkey->pkey);
4294 }
4295
4296 sftp_dsa_hostkey = NULL;
4297 return 0;
4298 }
4299
4300 int sftp_keys_clear_ecdsa_hostkey(void) {
4301 #if defined(PR_USE_OPENSSL_ECC)
4302 int count = 0;
4303
4304 if (sftp_ecdsa256_hostkey != NULL) {
4305 if (sftp_ecdsa256_hostkey->pkey != NULL) {
4306 EVP_PKEY_free(sftp_ecdsa256_hostkey->pkey);
4307 }
4308
4309 sftp_ecdsa256_hostkey = NULL;
4310 count++;
4311 }
4312
4313 if (sftp_ecdsa384_hostkey != NULL) {
4314 if (sftp_ecdsa384_hostkey->pkey != NULL) {
4315 EVP_PKEY_free(sftp_ecdsa384_hostkey->pkey);
4316 }
4317
4318 sftp_ecdsa384_hostkey = NULL;
4319 count++;
4320 }
4321
4322 if (sftp_ecdsa521_hostkey != NULL) {
4323 if (sftp_ecdsa521_hostkey->pkey != NULL) {
4324 EVP_PKEY_free(sftp_ecdsa521_hostkey->pkey);
4325 }
4326
4327 sftp_ecdsa521_hostkey = NULL;
4328 count++;
4329 }
4330
4331 if (count > 0) {
4332 return 0;
4333 }
4334
4335 #endif /* PR_USE_OPENSSL_ECC */
4336 errno = ENOENT;
4337 return -1;
4338 }
4339
4340 int sftp_keys_clear_ed25519_hostkey(void) {
4341 if (sftp_ed25519_hostkey == NULL) {
4342 errno = ENOENT;
4343 return -1;
4344 }
4345
4346 if (sftp_ed25519_hostkey->ed25519_secret_key != NULL) {
4347 pr_memscrub(sftp_ed25519_hostkey->ed25519_secret_key,
4348 sftp_ed25519_hostkey->ed25519_secret_keylen);
4349 sftp_ed25519_hostkey->ed25519_secret_key = NULL;
4350 sftp_ed25519_hostkey->ed25519_secret_keylen = 0;
4351 }
4352
4353 if (sftp_ed25519_hostkey->ed25519_public_key != NULL) {
4354 pr_memscrub(sftp_ed25519_hostkey->ed25519_public_key,
4355 sftp_ed25519_hostkey->ed25519_public_keylen);
4356 sftp_ed25519_hostkey->ed25519_public_key = NULL;
4357 sftp_ed25519_hostkey->ed25519_public_keylen = 0;
4358 }
4359
4360 return 0;
4361 }
4362
4363 int sftp_keys_clear_ed448_hostkey(void) {
4364 if (sftp_ed448_hostkey == NULL) {
4365 errno = ENOENT;
4366 return -1;
4367 }
4368
4369 if (sftp_ed448_hostkey->ed448_secret_key != NULL) {
4370 pr_memscrub(sftp_ed448_hostkey->ed448_secret_key,
4371 sftp_ed448_hostkey->ed448_secret_keylen);
4372 sftp_ed448_hostkey->ed448_secret_key = NULL;
4373 sftp_ed448_hostkey->ed448_secret_keylen = 0;
4374 }
4375
4376 if (sftp_ed448_hostkey->ed448_public_key != NULL) {
4377 pr_memscrub(sftp_ed448_hostkey->ed448_public_key,
4378 sftp_ed448_hostkey->ed448_public_keylen);
4379 sftp_ed448_hostkey->ed448_public_key = NULL;
4380 sftp_ed448_hostkey->ed448_public_keylen = 0;
4381 }
4382
4383 return 0;
4384 }
4385
4386 int sftp_keys_clear_rsa_hostkey(void) {
4387 if (sftp_rsa_hostkey == NULL) {
4388 errno = ENOENT;
4389 return -1;
4390 }
4391
4392 if (sftp_rsa_hostkey->pkey != NULL) {
4393 EVP_PKEY_free(sftp_rsa_hostkey->pkey);
4394 }
4395
4396 sftp_rsa_hostkey = NULL;
4397 return 0;
4398 }
4399
4400 int sftp_keys_have_dsa_hostkey(void) {
4401 if (sftp_dsa_hostkey == NULL) {
4402 errno = ENOENT;
4403 return -1;
4404 }
4405
4406 return 0;
4407 }
4408
4409 /* Returns the count of returned NIDs for the configured ECDSA hostkeys,
4410 * if any.
4411 */
4412 int sftp_keys_have_ecdsa_hostkey(pool *p, int **nids) {
4413 #if defined(PR_USE_OPENSSL_ECC)
4414 int count = 0;
4415
4416 if (nids != NULL) {
4417 *nids = palloc(p, sizeof(int) * 3);
4418 }
4419
4420 if (sftp_ecdsa256_hostkey != NULL) {
4421 EC_KEY *ec;
4422
4423 ec = EVP_PKEY_get1_EC_KEY(sftp_ecdsa256_hostkey->pkey);
4424 if (nids != NULL) {
4425 (*nids)[count] = get_ecdsa_nid(ec);
4426 }
4427 count++;
4428 EC_KEY_free(ec);
4429 }
4430
4431 if (sftp_ecdsa384_hostkey != NULL) {
4432 EC_KEY *ec;
4433
4434 ec = EVP_PKEY_get1_EC_KEY(sftp_ecdsa384_hostkey->pkey);
4435 if (nids != NULL) {
4436 (*nids)[count] = get_ecdsa_nid(ec);
4437 }
4438 count++;
4439 EC_KEY_free(ec);
4440 }
4441
4442 if (sftp_ecdsa521_hostkey != NULL) {
4443 EC_KEY *ec;
4444
4445 ec = EVP_PKEY_get1_EC_KEY(sftp_ecdsa521_hostkey->pkey);
4446 if (nids != NULL) {
4447 (*nids)[count] = get_ecdsa_nid(ec);
4448 }
4449 count++;
4450 EC_KEY_free(ec);
4451 }
4452
4453 if (count > 0) {
4454 return count;
4455 }
4456 #endif /* PR_USE_OPENSSL_ECC */
4457
4458 errno = ENOENT;
4459 return -1;
4460 }
4461
4462 int sftp_keys_have_ed25519_hostkey(void) {
4463 if (sftp_ed25519_hostkey == NULL) {
4464 errno = ENOENT;
4465 return -1;
4466 }
4467
4468 return 0;
4469 }
4470
4471 int sftp_keys_have_ed448_hostkey(void) {
4472 if (sftp_ed448_hostkey == NULL) {
4473 errno = ENOENT;
4474 return -1;
4475 }
4476
4477 return 0;
4478 }
4479
4480 int sftp_keys_have_rsa_hostkey(void) {
4481 if (sftp_rsa_hostkey == NULL) {
4482 errno = ENOENT;
4483 return -1;
4484 }
4485
4486 return 0;
4487 }
4488
4489 static const unsigned char *agent_sign_data(pool *p, const char *agent_path,
4490 const unsigned char *key_data, uint32_t key_datalen,
4491 const unsigned char *data, size_t datalen, size_t *siglen, int flags) {
4492 unsigned char *sig_data;
4493 uint32_t sig_datalen = 0;
4494
4495 pr_trace_msg(trace_channel, 15,
4496 "asking SSH agent at '%s' to sign data", agent_path);
4497
4498 /* Ask the agent to sign the data for this hostkey for us. */
4499 sig_data = (unsigned char *) sftp_agent_sign_data(p, agent_path,
4500 key_data, key_datalen, data, datalen, &sig_datalen, flags);
4501
4502 if (sig_data == NULL) {
4503 int xerrno = errno;
4504
4505 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4506 "SSH agent at '%s' could not sign data: %s", agent_path,
4507 strerror(xerrno));
4508
4509 errno = xerrno;
4510 return NULL;
4511 }
4512
4513 /* The SSH agent already provides the signed data in the correct
4514 * SSH2-style.
4515 */
4516
4517 *siglen = sig_datalen;
4518 return sig_data;
4519 }
4520
4521 static const unsigned char *get_rsa_signed_data(pool *p,
4522 const unsigned char *data, size_t datalen, size_t *siglen,
4523 const char *sig_name, const EVP_MD *md) {
4524 RSA *rsa;
4525 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4526 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4527 EVP_MD_CTX ctx;
4528 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4529 EVP_MD_CTX *pctx;
4530 unsigned char dgst[EVP_MAX_MD_SIZE], *sig_data;
4531 unsigned char *buf, *ptr;
4532 size_t bufsz;
4533 uint32_t buflen, dgstlen = 0, sig_datalen = 0, sig_rsalen = 0;
4534 int res;
4535
4536 rsa = EVP_PKEY_get1_RSA(sftp_rsa_hostkey->pkey);
4537 if (rsa == NULL) {
4538 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4539 "error using RSA hostkey: %s", sftp_crypto_get_errors());
4540 return NULL;
4541 }
4542
4543 if (keys_rsa_min_nbits > 0) {
4544 int rsa_nbits;
4545
4546 rsa_nbits = RSA_size(rsa) * 8;
4547 if (rsa_nbits < keys_rsa_min_nbits) {
4548 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4549 "RSA hostkey size (%d bits) less than required minimum (%d bits)",
4550 rsa_nbits, keys_rsa_min_nbits);
4551 RSA_free(rsa);
4552
4553 errno = EINVAL;
4554 return NULL;
4555 }
4556 }
4557
4558 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4559 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4560 pctx = EVP_MD_CTX_new();
4561 #else
4562 pctx = &ctx;
4563 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4564
4565 EVP_DigestInit(pctx, md);
4566 EVP_DigestUpdate(pctx, data, datalen);
4567 EVP_DigestFinal(pctx, dgst, &dgstlen);
4568
4569 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
4570 !defined(HAVE_LIBRESSL)
4571 EVP_MD_CTX_free(pctx);
4572 #endif /* OpenSSL-1.1.0 and later */
4573
4574 sig_rsalen = RSA_size(rsa);
4575 sig_data = pcalloc(p, sig_rsalen);
4576 res = RSA_sign(EVP_MD_type(md), dgst, dgstlen, sig_data, &sig_datalen, rsa);
4577
4578 /* Regardless of whether the RSA signing succeeds or fails, we are done
4579 * with the digest buffer.
4580 */
4581 pr_memscrub(dgst, dgstlen);
4582
4583 if (res != 1) {
4584 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4585 "error signing data using RSA: %s", sftp_crypto_get_errors());
4586 RSA_free(rsa);
4587 return NULL;
4588 }
4589
4590 /* XXX Is this buffer large enough? Too large? */
4591 buflen = bufsz = SFTP_MAX_SIG_SZ;
4592 ptr = buf = palloc(p, bufsz);
4593
4594 /* Now build up the signature, SSH2-style */
4595 sftp_msg_write_string(&buf, &buflen, sig_name);
4596 sftp_msg_write_data(&buf, &buflen, sig_data, sig_datalen, TRUE);
4597
4598 pr_memscrub(sig_data, sig_datalen);
4599 RSA_free(rsa);
4600
4601 /* At this point, buflen is the amount remaining in the allocated buffer.
4602 * So the total length of the signed data is the buffer size, minus those
4603 * remaining unused bytes.
4604 */
4605 *siglen = (bufsz - buflen);
4606 return ptr;
4607 }
4608
4609 static const unsigned char *rsa_sign_data(pool *p, const unsigned char *data,
4610 size_t datalen, size_t *siglen) {
4611 if (sftp_rsa_hostkey->agent_path != NULL) {
4612 return agent_sign_data(p, sftp_rsa_hostkey->agent_path,
4613 sftp_rsa_hostkey->key_data, sftp_rsa_hostkey->key_datalen, data, datalen,
4614 siglen, 0);
4615 }
4616
4617 return get_rsa_signed_data(p, data, datalen, siglen, "ssh-rsa", EVP_sha1());
4618 }
4619
4620 #if defined(HAVE_SHA256_OPENSSL)
4621 static const unsigned char *rsa_sha256_sign_data(pool *p,
4622 const unsigned char *data, size_t datalen, size_t *siglen) {
4623 if (sftp_rsa_hostkey->agent_path != NULL) {
4624 return agent_sign_data(p, sftp_rsa_hostkey->agent_path,
4625 sftp_rsa_hostkey->key_data, sftp_rsa_hostkey->key_datalen, data, datalen,
4626 siglen, SFTP_AGENT_SIGN_FL_USE_RSA_SHA256);
4627 }
4628
4629 return get_rsa_signed_data(p, data, datalen, siglen, "rsa-sha2-256",
4630 EVP_sha256());
4631 }
4632 #endif /* HAVE_SHA256_OPENSSL */
4633
4634 #if defined(HAVE_SHA512_OPENSSL)
4635 static const unsigned char *rsa_sha512_sign_data(pool *p,
4636 const unsigned char *data, size_t datalen, size_t *siglen) {
4637 if (sftp_rsa_hostkey->agent_path != NULL) {
4638 return agent_sign_data(p, sftp_rsa_hostkey->agent_path,
4639 sftp_rsa_hostkey->key_data, sftp_rsa_hostkey->key_datalen, data, datalen,
4640 siglen, SFTP_AGENT_SIGN_FL_USE_RSA_SHA512);
4641 }
4642
4643 return get_rsa_signed_data(p, data, datalen, siglen, "rsa-sha2-512",
4644 EVP_sha512());
4645 }
4646 #endif /* HAVE_SHA256_OPENSSL */
4647
4648 /* RFC 4253, Section 6.6, is quite specific about the length of a DSA
4649 * ("ssh-dss") signature blob. It is comprised of two integers R and S,
4650 * each 160 bits (20 bytes), so that the total signature blob is 40 bytes
4651 * long.
4652 */
4653 #define SFTP_DSA_INTEGER_LEN 20
4654 #define SFTP_DSA_SIGNATURE_LEN (SFTP_DSA_INTEGER_LEN * 2)
4655
4656 #if !defined(OPENSSL_NO_DSA)
4657 static const unsigned char *dsa_sign_data(pool *p, const unsigned char *data,
4658 size_t datalen, size_t *siglen) {
4659 DSA *dsa;
4660 DSA_SIG *sig;
4661 const BIGNUM *sig_r = NULL, *sig_s = NULL;
4662 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4663 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4664 EVP_MD_CTX ctx;
4665 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4666 EVP_MD_CTX *pctx;
4667 const EVP_MD *sha1 = EVP_sha1();
4668 unsigned char dgst[EVP_MAX_MD_SIZE], *sig_data;
4669 unsigned char *buf, *ptr;
4670 size_t bufsz;
4671 uint32_t buflen, dgstlen = 0;
4672 unsigned int rlen = 0, slen = 0;
4673
4674 if (sftp_dsa_hostkey->agent_path != NULL) {
4675 return agent_sign_data(p, sftp_dsa_hostkey->agent_path,
4676 sftp_dsa_hostkey->key_data, sftp_dsa_hostkey->key_datalen, data, datalen,
4677 siglen, 0);
4678 }
4679
4680 dsa = EVP_PKEY_get1_DSA(sftp_dsa_hostkey->pkey);
4681 if (dsa == NULL) {
4682 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4683 "error using DSA hostkey: %s", sftp_crypto_get_errors());
4684 return NULL;
4685 }
4686
4687 if (keys_dsa_min_nbits > 0) {
4688 int dsa_nbits;
4689
4690 dsa_nbits = DSA_size(dsa) * 8;
4691 if (dsa_nbits < keys_dsa_min_nbits) {
4692 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4693 "DSA hostkey size (%d bits) less than required minimum (%d bits)",
4694 dsa_nbits, keys_dsa_min_nbits);
4695 DSA_free(dsa);
4696
4697 errno = EINVAL;
4698 return NULL;
4699 }
4700 }
4701
4702 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4703 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4704 pctx = EVP_MD_CTX_new();
4705 #else
4706 pctx = &ctx;
4707 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4708
4709 EVP_DigestInit(pctx, sha1);
4710 EVP_DigestUpdate(pctx, data, datalen);
4711 EVP_DigestFinal(pctx, dgst, &dgstlen);
4712
4713 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
4714 !defined(HAVE_LIBRESSL)
4715 EVP_MD_CTX_free(pctx);
4716 #endif /* OpenSSL-1.1.0 and later */
4717
4718 sig = DSA_do_sign(dgst, dgstlen, dsa);
4719 if (sig == NULL) {
4720 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4721 "error obtaining DSA signature: %s", sftp_crypto_get_errors());
4722 pr_memscrub(dgst, dgstlen);
4723 DSA_free(dsa);
4724 return NULL;
4725 }
4726
4727 /* Got the signature, no need for the digest memory. */
4728 pr_memscrub(dgst, dgstlen);
4729
4730 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4731 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4732 DSA_SIG_get0(sig, &sig_r, &sig_s);
4733 #else
4734 sig_r = sig->r;
4735 sig_s = sig->s;
4736 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4737
4738 rlen = BN_num_bytes(sig_r);
4739 slen = BN_num_bytes(sig_s);
4740
4741 /* Make sure the values of R and S are big enough. */
4742 if (rlen > SFTP_DSA_INTEGER_LEN ||
4743 slen > SFTP_DSA_INTEGER_LEN) {
4744 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4745 "bad DSA signature size (%u, %u)", rlen, slen);
4746 DSA_SIG_free(sig);
4747 DSA_free(dsa);
4748 return NULL;
4749 }
4750
4751 sig_data = pcalloc(p, SFTP_MAX_SIG_SZ);
4752
4753 /* These may look strange, but the pointer arithmetic is necessary to
4754 * ensure the correct placement of the R and S values in the signature,
4755 * per RFC 4253 Section 6.6 requirements.
4756 */
4757 BN_bn2bin(sig_r,
4758 sig_data + SFTP_DSA_SIGNATURE_LEN - SFTP_DSA_INTEGER_LEN - rlen);
4759 BN_bn2bin(sig_s, sig_data + SFTP_DSA_SIGNATURE_LEN - slen);
4760
4761 /* Done with the signature. */
4762 DSA_SIG_free(sig);
4763 DSA_free(dsa);
4764
4765 /* XXX Is this buffer large enough? Too large? */
4766 buflen = bufsz = SFTP_MAX_SIG_SZ;
4767 ptr = buf = palloc(p, bufsz);
4768
4769 /* Now build up the signature, SSH2-style */
4770 sftp_msg_write_string(&buf, &buflen, "ssh-dss");
4771 sftp_msg_write_data(&buf, &buflen, sig_data, SFTP_DSA_SIGNATURE_LEN, TRUE);
4772
4773 /* At this point, buflen is the amount remaining in the allocated buffer.
4774 * So the total length of the signed data is the buffer size, minus those
4775 * remaining unused bytes.
4776 */
4777 *siglen = (bufsz - buflen);
4778 return ptr;
4779 }
4780 #endif /* !OPENSSL_NO_DSA */
4781
4782 #if defined(PR_USE_OPENSSL_ECC)
4783 static const unsigned char *ecdsa_sign_data(pool *p, const unsigned char *data,
4784 size_t datalen, size_t *siglen, int nid) {
4785 EVP_PKEY *pkey = NULL;
4786 EC_KEY *ec = NULL;
4787 ECDSA_SIG *sig;
4788 const BIGNUM *sig_r = NULL, *sig_s = NULL;
4789 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4790 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4791 EVP_MD_CTX ctx;
4792 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4793 EVP_MD_CTX *pctx;
4794 const EVP_MD *md;
4795 unsigned char dgst[EVP_MAX_MD_SIZE];
4796 unsigned char *buf, *ptr, *sig_buf, *sig_ptr;
4797 uint32_t bufsz, buflen, dgstlen = 0, sig_buflen, sig_bufsz;
4798
4799 switch (nid) {
4800 case NID_X9_62_prime256v1:
4801 if (sftp_ecdsa256_hostkey->agent_path != NULL) {
4802 return agent_sign_data(p, sftp_ecdsa256_hostkey->agent_path,
4803 sftp_ecdsa256_hostkey->key_data, sftp_ecdsa256_hostkey->key_datalen,
4804 data, datalen, siglen, 0);
4805 }
4806
4807 ec = EVP_PKEY_get1_EC_KEY(sftp_ecdsa256_hostkey->pkey);
4808 if (ec == NULL) {
4809 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4810 "error using ECDSA-256 hostkey: %s", sftp_crypto_get_errors());
4811 return NULL;
4812 }
4813
4814 pkey = sftp_ecdsa256_hostkey->pkey;
4815 md = EVP_sha256();
4816 break;
4817
4818 case NID_secp384r1:
4819 if (sftp_ecdsa384_hostkey->agent_path != NULL) {
4820 return agent_sign_data(p, sftp_ecdsa384_hostkey->agent_path,
4821 sftp_ecdsa384_hostkey->key_data, sftp_ecdsa384_hostkey->key_datalen,
4822 data, datalen, siglen, 0);
4823 }
4824
4825 ec = EVP_PKEY_get1_EC_KEY(sftp_ecdsa384_hostkey->pkey);
4826 if (ec == NULL) {
4827 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4828 "error using ECDSA-384 hostkey: %s", sftp_crypto_get_errors());
4829 return NULL;
4830 }
4831
4832 pkey = sftp_ecdsa384_hostkey->pkey;
4833 md = EVP_sha384();
4834 break;
4835
4836 case NID_secp521r1:
4837 if (sftp_ecdsa521_hostkey->agent_path != NULL) {
4838 return agent_sign_data(p, sftp_ecdsa521_hostkey->agent_path,
4839 sftp_ecdsa521_hostkey->key_data, sftp_ecdsa521_hostkey->key_datalen,
4840 data, datalen, siglen, 0);
4841 }
4842
4843 ec = EVP_PKEY_get1_EC_KEY(sftp_ecdsa521_hostkey->pkey);
4844 if (ec == NULL) {
4845 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4846 "error using ECDSA-521 hostkey: %s", sftp_crypto_get_errors());
4847 return NULL;
4848 }
4849
4850 pkey = sftp_ecdsa521_hostkey->pkey;
4851 md = EVP_sha512();
4852 break;
4853
4854 default:
4855 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4856 "unknown/unsupported ECDSA NID (%d) requested", nid);
4857 return NULL;
4858 }
4859
4860 if (keys_ec_min_nbits > 0) {
4861 int ec_nbits;
4862
4863 ec_nbits = EVP_PKEY_bits(pkey) * 8;
4864 if (ec_nbits < keys_ec_min_nbits) {
4865 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4866 "EC hostkey size (%d bits) less than required minimum (%d bits)",
4867 ec_nbits, keys_ec_min_nbits);
4868 EC_KEY_free(ec);
4869
4870 errno = EINVAL;
4871 return NULL;
4872 }
4873 }
4874
4875 buflen = bufsz = SFTP_MAX_SIG_SZ;
4876 ptr = buf = palloc(p, bufsz);
4877
4878 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4879 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4880 pctx = EVP_MD_CTX_new();
4881 #else
4882 pctx = &ctx;
4883 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4884
4885 EVP_DigestInit(pctx, md);
4886 EVP_DigestUpdate(pctx, data, datalen);
4887 EVP_DigestFinal(pctx, dgst, &dgstlen);
4888
4889 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4890 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4891 EVP_MD_CTX_free(pctx);
4892 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
4893
4894 sig = ECDSA_do_sign(dgst, dgstlen, ec);
4895 if (sig == NULL) {
4896 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4897 "error obtaining ECDSA signature: %s", sftp_crypto_get_errors());
4898 pr_memscrub(dgst, dgstlen);
4899 EC_KEY_free(ec);
4900 return NULL;
4901 }
4902
4903 /* Got the signature, no need for the digest memory. */
4904 pr_memscrub(dgst, dgstlen);
4905
4906 /* Unlike DSA, the R and S lengths for ECDSA are dependent on the curve
4907 * selected, so we do no sanity checking of their lengths.
4908 */
4909
4910 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
4911 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
4912 ECDSA_SIG_get0(sig, &sig_r, &sig_s);
4913 #else
4914 sig_r = sig->r;
4915 sig_s = sig->s;
4916 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4917
4918 /* XXX Is this buffer large enough? Too large? */
4919 sig_buflen = sig_bufsz = 256;
4920 sig_ptr = sig_buf = palloc(p, sig_bufsz);
4921
4922 sftp_msg_write_mpint(&sig_buf, &sig_buflen, sig_r);
4923 sftp_msg_write_mpint(&sig_buf, &sig_buflen, sig_s);
4924
4925 /* Done with the signature. */
4926 ECDSA_SIG_free(sig);
4927 EC_KEY_free(ec);
4928
4929 /* XXX Is this buffer large enough? Too large? */
4930 buflen = bufsz = SFTP_MAX_SIG_SZ;
4931 ptr = buf = palloc(p, bufsz);
4932
4933 /* Now build up the signature, SSH2-style */
4934 switch (nid) {
4935 case NID_X9_62_prime256v1:
4936 sftp_msg_write_string(&buf, &buflen, "ecdsa-sha2-nistp256");
4937 break;
4938
4939 case NID_secp384r1:
4940 sftp_msg_write_string(&buf, &buflen, "ecdsa-sha2-nistp384");
4941 break;
4942
4943 case NID_secp521r1:
4944 sftp_msg_write_string(&buf, &buflen, "ecdsa-sha2-nistp521");
4945 break;
4946 }
4947
4948 sftp_msg_write_data(&buf, &buflen, sig_ptr, (sig_bufsz - sig_buflen), TRUE);
4949 pr_memscrub(sig_ptr, sig_bufsz);
4950
4951 /* At this point, buflen is the amount remaining in the allocated buffer.
4952 * So the total length of the signed data is the buffer size, minus those
4953 * remaining unused bytes.
4954 */
4955 *siglen = (bufsz - buflen);
4956 return ptr;
4957 }
4958 #endif /* PR_USE_OPENSSL_ECC */
4959
4960 #if defined(PR_USE_SODIUM)
4961 static const unsigned char *ed25519_sign_data(pool *p,
4962 const unsigned char *data, size_t datalen, size_t *siglen) {
4963 unsigned char *buf, *ptr, *sig_buf, *sig_ptr;
4964 uint32_t bufsz, buflen, sig_buflen, sig_bufsz;
4965 unsigned long long slen;
4966 int res;
4967
4968 /* XXX TODO ED25519: Test this! */
4969 if (sftp_ed25519_hostkey->agent_path != NULL) {
4970 return agent_sign_data(p, sftp_ed25519_hostkey->agent_path,
4971 sftp_ed25519_hostkey->ed25519_public_key,
4972 sftp_ed25519_hostkey->ed25519_public_keylen,
4973 data, datalen, siglen, 0);
4974 }
4975
4976 sig_buflen = sig_bufsz = slen = datalen + crypto_sign_ed25519_BYTES;
4977 sig_ptr = sig_buf = palloc(p, sig_bufsz);
4978
4979 res = crypto_sign_ed25519(sig_buf, &slen, data, datalen,
4980 sftp_ed25519_hostkey->ed25519_secret_key);
4981 if (res != 0) {
4982 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4983 "failed to sign data using Ed25519 (%d)", res);
4984 pr_memscrub(sig_ptr, sig_bufsz);
4985 return NULL;
4986 }
4987
4988 sig_buflen = slen;
4989 if (sig_buflen <= datalen) {
4990 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4991 "invalid Ed25519 signature (%lu bytes) generated, expected more than "
4992 "%lu bytes", (unsigned long) sig_buflen, (unsigned long) datalen);
4993 pr_memscrub(sig_ptr, sig_bufsz);
4994 return NULL;
4995 }
4996
4997 /* XXX Is this buffer large enough? Too large? */
4998 buflen = bufsz = SFTP_MAX_SIG_SZ;
4999 ptr = buf = palloc(p, bufsz);
5000
5001 /* Now build up the signature, SSH2-style */
5002 sftp_msg_write_string(&buf, &buflen, "ssh-ed25519");
5003 sftp_msg_write_data(&buf, &buflen, sig_ptr, sig_buflen - datalen, TRUE);
5004 pr_memscrub(sig_ptr, sig_bufsz);
5005
5006 /* At this point, buflen is the amount remaining in the allocated buffer.
5007 * So the total length of the signed data is the buffer size, minus those
5008 * remaining unused bytes.
5009 */
5010 *siglen = (bufsz - buflen);
5011 return ptr;
5012 }
5013 #endif /* PR_USE_SODIUM */
5014
5015 #if defined(HAVE_X448_OPENSSL)
5016 static const unsigned char *ed448_sign_data(pool *p,
5017 const unsigned char *data, size_t datalen, size_t *siglen) {
5018 unsigned char *buf, *ptr, *sig_buf;
5019 uint32_t bufsz, buflen, sig_buflen, sig_bufsz;
5020 EVP_MD_CTX *md_ctx;
5021 EVP_PKEY *pkey;
5022
5023 /* XXX TODO ED448: Test this! */
5024 if (sftp_ed448_hostkey->agent_path != NULL) {
5025 return agent_sign_data(p, sftp_ed448_hostkey->agent_path,
5026 sftp_ed448_hostkey->ed448_public_key,
5027 sftp_ed448_hostkey->ed448_public_keylen,
5028 data, datalen, siglen, 0);
5029 }
5030
5031 sig_buflen = sig_bufsz = datalen + 256;
5032 sig_buf = palloc(p, sig_bufsz);
5033
5034 md_ctx = EVP_MD_CTX_new();
5035 pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED448, NULL,
5036 sftp_ed448_hostkey->ed448_secret_key,
5037 sftp_ed448_hostkey->ed448_secret_keylen);
5038 if (pkey == NULL) {
5039 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5040 "error initializing Ed448 private key: %s", sftp_crypto_get_errors());
5041 EVP_MD_CTX_free(md_ctx);
5042 return NULL;
5043 }
5044
5045 if (EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, pkey) != 1) {
5046 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5047 "error initializing Ed448 signature: %s", sftp_crypto_get_errors());
5048 EVP_PKEY_free(pkey);
5049 EVP_MD_CTX_free(md_ctx);
5050 return NULL;
5051 }
5052
5053 if (EVP_DigestSign(md_ctx, sig_buf, (size_t *) &sig_buflen, data,
5054 datalen) != 1) {
5055 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5056 "failed to sign data using Ed448: %s", sftp_crypto_get_errors());
5057 EVP_PKEY_free(pkey);
5058 EVP_MD_CTX_free(md_ctx);
5059 return NULL;
5060 }
5061
5062 EVP_PKEY_free(pkey);
5063 EVP_MD_CTX_free(md_ctx);
5064
5065 /* XXX Is this buffer large enough? Too large? */
5066 buflen = bufsz = SFTP_MAX_SIG_SZ;
5067 ptr = buf = palloc(p, bufsz);
5068
5069 /* Now build up the signature, SSH2-style */
5070 sftp_msg_write_string(&buf, &buflen, "ssh-ed448");
5071 sftp_msg_write_data(&buf, &buflen, sig_buf, sig_buflen, TRUE);
5072 pr_memscrub(sig_buf, sig_bufsz);
5073
5074 /* At this point, buflen is the amount remaining in the allocated buffer.
5075 * So the total length of the signed data is the buffer size, minus those
5076 * remaining unused bytes.
5077 */
5078 *siglen = (bufsz - buflen);
5079 return ptr;
5080 }
5081 #endif /* HAVE_X448_OPENSSL */
5082
5083 const unsigned char *sftp_keys_sign_data(pool *p,
5084 enum sftp_key_type_e key_type, const unsigned char *data,
5085 size_t datalen, size_t *siglen) {
5086 const unsigned char *res;
5087
5088 switch (key_type) {
5089 case SFTP_KEY_RSA:
5090 res = rsa_sign_data(p, data, datalen, siglen);
5091 break;
5092
5093 #if defined(HAVE_SHA256_OPENSSL)
5094 case SFTP_KEY_RSA_SHA256:
5095 res = rsa_sha256_sign_data(p, data, datalen, siglen);
5096 break;
5097 #endif /* HAVE_SHA256_OPENSSL */
5098
5099 #if defined(HAVE_SHA512_OPENSSL)
5100 case SFTP_KEY_RSA_SHA512:
5101 res = rsa_sha512_sign_data(p, data, datalen, siglen);
5102 break;
5103 #endif /* HAVE_SHA512_OPENSSL */
5104
5105 #if !defined(OPENSSL_NO_DSA)
5106 case SFTP_KEY_DSA:
5107 res = dsa_sign_data(p, data, datalen, siglen);
5108 break;
5109 #endif /* !OPENSSL_NO_DSA */
5110
5111 #if defined(PR_USE_OPENSSL_ECC)
5112 case SFTP_KEY_ECDSA_256:
5113 res = ecdsa_sign_data(p, data, datalen, siglen, NID_X9_62_prime256v1);
5114 break;
5115
5116 case SFTP_KEY_ECDSA_384:
5117 res = ecdsa_sign_data(p, data, datalen, siglen, NID_secp384r1);
5118 break;
5119
5120 case SFTP_KEY_ECDSA_521:
5121 res = ecdsa_sign_data(p, data, datalen, siglen, NID_secp521r1);
5122 break;
5123 #endif /* PR_USE_OPENSSL_ECC */
5124
5125 #if defined(PR_USE_SODIUM)
5126 case SFTP_KEY_ED25519:
5127 res = ed25519_sign_data(p, data, datalen, siglen);
5128 break;
5129 #endif /* PR_USE_SODIUM */
5130
5131 #if defined(HAVE_X448_OPENSSL)
5132 case SFTP_KEY_ED448:
5133 res = ed448_sign_data(p, data, datalen, siglen);
5134 break;
5135 #endif /* HAVE_X448_OPENSSL */
5136
5137 default:
5138 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5139 "unknown key type (%d) requested for signing, ignoring", key_type);
5140 return NULL;
5141 }
5142
5143 if (res != NULL &&
5144 p != NULL) {
5145 unsigned char *buf;
5146
5147 buf = palloc(p, *siglen);
5148 memcpy(buf, res, *siglen);
5149
5150 pr_memscrub((char *) res, *siglen);
5151 return buf;
5152 }
5153
5154 return res;
5155 }
5156
5157 int sftp_keys_verify_pubkey_type(pool *p, unsigned char *pubkey_data,
5158 uint32_t pubkey_len, enum sftp_key_type_e pubkey_type) {
5159 EVP_PKEY *pkey = NULL;
5160 int res = FALSE;
5161 uint32_t len;
5162
5163 if (pubkey_data == NULL ||
5164 pubkey_len == 0) {
5165 errno = EINVAL;
5166 return -1;
5167 }
5168
5169 len = read_pkey_from_data(p, pubkey_data, pubkey_len, &pkey, NULL, NULL,
5170 FALSE);
5171 if (len == 0) {
5172 return -1;
5173 }
5174
5175 switch (pubkey_type) {
5176 case SFTP_KEY_RSA:
5177 case SFTP_KEY_RSA_SHA256:
5178 case SFTP_KEY_RSA_SHA512:
5179 res = (get_pkey_type(pkey) == EVP_PKEY_RSA);
5180 break;
5181
5182 case SFTP_KEY_DSA:
5183 res = (get_pkey_type(pkey) == EVP_PKEY_DSA);
5184 break;
5185
5186 #if defined(PR_USE_OPENSSL_ECC)
5187 case SFTP_KEY_ECDSA_256:
5188 case SFTP_KEY_ECDSA_384:
5189 case SFTP_KEY_ECDSA_521:
5190 if (get_pkey_type(pkey) == EVP_PKEY_EC) {
5191 EC_KEY *ec;
5192 int ec_nid;
5193
5194 ec = EVP_PKEY_get1_EC_KEY(pkey);
5195 ec_nid = get_ecdsa_nid(ec);
5196 EC_KEY_free(ec);
5197
5198 switch (ec_nid) {
5199 case NID_X9_62_prime256v1:
5200 res = (pubkey_type == SFTP_KEY_ECDSA_256);
5201 break;
5202
5203 case NID_secp384r1:
5204 res = (pubkey_type == SFTP_KEY_ECDSA_384);
5205 break;
5206
5207 case NID_secp521r1:
5208 res = (pubkey_type == SFTP_KEY_ECDSA_521);
5209 break;
5210 }
5211 }
5212 break;
5213
5214 case SFTP_KEY_ECDSA_256_SK:
5215 if (get_pkey_type(pkey) == EVP_PKEY_EC) {
5216 EC_KEY *ec;
5217 int ec_nid;
5218
5219 ec = EVP_PKEY_get1_EC_KEY(pkey);
5220 ec_nid = get_ecdsa_nid(ec);
5221 EC_KEY_free(ec);
5222
5223 if (ec_nid == NID_X9_62_prime256v1) {
5224 res = TRUE;
5225 break;
5226 }
5227 }
5228 break;
5229 #endif /* PR_USE_OPENSSL_ECC */
5230
5231 #if defined(PR_USE_SODIUM)
5232 case SFTP_KEY_ED25519:
5233 case SFTP_KEY_ED25519_SK: {
5234 char *pkey_type;
5235
5236 pkey_type = sftp_msg_read_string(p, &pubkey_data, &pubkey_len);
5237 if (strcmp(pkey_type, "ssh-ed25519") == 0) {
5238 if (pubkey_type == SFTP_KEY_ED25519) {
5239 res = TRUE;
5240
5241 } else {
5242 pr_trace_msg(trace_channel, 8,
5243 "invalid public key algorithm '%s' for expected %s key", pkey_type,
5244 get_key_type_desc(pubkey_type));
5245 res = FALSE;
5246 }
5247
5248 } else if (strcmp(pkey_type, "[email protected]") == 0) {
5249 if (pubkey_type == SFTP_KEY_ED25519_SK) {
5250 res = TRUE;
5251
5252 } else {
5253 pr_trace_msg(trace_channel, 8,
5254 "invalid public key algorithm '%s' for expected %s key", pkey_type,
5255 get_key_type_desc(pubkey_type));
5256 res = FALSE;
5257 }
5258
5259 } else {
5260 pr_trace_msg(trace_channel, 8,
5261 "invalid public key algorithm '%s' for Ed25519 key", pkey_type);
5262 res = FALSE;
5263 }
5264
5265 if (res == TRUE) {
5266 uint32_t pklen;
5267
5268 pklen = sftp_msg_read_int(p, &pubkey_data, &pubkey_len);
5269
5270 res = (pklen == (uint32_t) crypto_sign_ed25519_PUBLICKEYBYTES);
5271 if (res == FALSE) {
5272 pr_trace_msg(trace_channel, 8,
5273 "%s public key length (%lu bytes) does not match expected "
5274 "length (%lu bytes)", get_key_type_desc(pubkey_type),
5275 (unsigned long) pklen,
5276 (unsigned long) crypto_sign_ed25519_PUBLICKEYBYTES);
5277 }
5278 }
5279 break;
5280 }
5281 #endif /* PR_USE_SODIUM */
5282
5283 #if defined(HAVE_X448_OPENSSL)
5284 case SFTP_KEY_ED448: {
5285 char *pkey_type;
5286
5287 pkey_type = sftp_msg_read_string(p, &pubkey_data, &pubkey_len);
5288 if (strcmp(pkey_type, "ssh-ed448") != 0) {
5289 pr_trace_msg(trace_channel, 8,
5290 "invalid public key type '%s' for Ed448 key", pkey_type);
5291 res = FALSE;
5292
5293 } else {
5294 uint32_t pklen;
5295
5296 pklen = sftp_msg_read_int(p, &pubkey_data, &pubkey_len);
5297
5298 res = FALSE;
5299 if (pklen == (uint32_t) CURVE448_SIZE ||
5300 pklen == ((uint32_t) CURVE448_SIZE + 1)) {
5301 res = TRUE;
5302 }
5303
5304 if (res == FALSE) {
5305 pr_trace_msg(trace_channel, 8,
5306 "Ed448 public key length (%lu bytes) does not match expected "
5307 "length (%lu bytes)", (unsigned long) pklen,
5308 (unsigned long) CURVE448_SIZE);
5309 }
5310 }
5311 break;
5312 }
5313 #endif /* HAVE_X448_OPENSSL */
5314
5315 default:
5316 /* No matching public key type/algorithm. */
5317 errno = ENOENT;
5318 res = FALSE;
5319 break;
5320 }
5321
5322 if (pkey != NULL) {
5323 EVP_PKEY_free(pkey);
5324 }
5325
5326 return res;
5327 }
5328
5329 static int verify_rsa_signed_data(pool *p, EVP_PKEY *pkey,
5330 unsigned char *signature, uint32_t signature_len,
5331 unsigned char *sig_data, size_t sig_datalen, const EVP_MD *md) {
5332 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
5333 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
5334 EVP_MD_CTX ctx;
5335 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5336 EVP_MD_CTX *pctx;
5337 RSA *rsa;
5338 uint32_t len, sig_len;
5339 unsigned char digest[EVP_MAX_MD_SIZE], *sig;
5340 unsigned int digest_len = 0, modulus_len = 0;
5341 int ok = FALSE, res = 0;
5342
5343 len = sftp_msg_read_int2(p, &signature, &signature_len, &sig_len);
5344 if (len == 0) {
5345 errno = EINVAL;
5346 return -1;
5347 }
5348
5349 len = sftp_msg_read_data2(p, &signature, &signature_len, sig_len, &sig);
5350 if (len == 0) {
5351 errno = EINVAL;
5352 return -1;
5353 }
5354
5355 if (sig == NULL) {
5356 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5357 "error verifying RSA signature: missing signature data");
5358 errno = EINVAL;
5359 return -1;
5360 }
5361
5362 rsa = EVP_PKEY_get1_RSA(pkey);
5363 if (rsa == NULL) {
5364 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5365 "error obtaining RSA key: %s", sftp_crypto_get_errors());
5366 errno = EINVAL;
5367 return -1;
5368 }
5369
5370 if (keys_rsa_min_nbits > 0) {
5371 int rsa_nbits;
5372
5373 rsa_nbits = RSA_size(rsa) * 8;
5374 if (rsa_nbits < keys_rsa_min_nbits) {
5375 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5376 "RSA key size (%d bits) less than required minimum (%d bits)",
5377 rsa_nbits, keys_rsa_min_nbits);
5378 RSA_free(rsa);
5379
5380 errno = EINVAL;
5381 return -1;
5382 }
5383 }
5384
5385 modulus_len = RSA_size(rsa);
5386
5387 /* If the signature provided by the client is more than the expected
5388 * key length, the verification will fail.
5389 */
5390 if (sig_len > modulus_len) {
5391 RSA_free(rsa);
5392
5393 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5394 "error verifying RSA signature: signature len (%lu) > RSA modulus "
5395 "len (%u)", (unsigned long) sig_len, modulus_len);
5396 errno = EINVAL;
5397 return -1;
5398 }
5399
5400 /* If the signature provided by the client is less than the expected
5401 * key length, the verification will fail. In such cases, we need to
5402 * pad the provided signature with leading zeros (Bug#3992).
5403 */
5404 if (sig_len < modulus_len) {
5405 unsigned int padding_len;
5406 unsigned char *padded_sig;
5407
5408 padding_len = modulus_len - sig_len;
5409 padded_sig = pcalloc(p, modulus_len);
5410
5411 pr_trace_msg(trace_channel, 12, "padding client-sent RSA signature "
5412 "(%lu) bytes with %u bytes of zeroed data", (unsigned long) sig_len,
5413 padding_len);
5414 memmove(padded_sig + padding_len, sig, sig_len);
5415
5416 sig = padded_sig;
5417 sig_len = (uint32_t) modulus_len;
5418 }
5419
5420 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5421 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5422 pctx = EVP_MD_CTX_new();
5423 #else
5424 pctx = &ctx;
5425 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5426
5427 EVP_DigestInit(pctx, md);
5428 EVP_DigestUpdate(pctx, sig_data, sig_datalen);
5429 EVP_DigestFinal(pctx, digest, &digest_len);
5430
5431 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5432 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5433 EVP_MD_CTX_free(pctx);
5434 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
5435
5436 ok = RSA_verify(EVP_MD_type(md), digest, digest_len, sig, sig_len, rsa);
5437 if (ok == 1) {
5438 res = 0;
5439
5440 } else {
5441 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5442 "error verifying RSA signature: %s", sftp_crypto_get_errors());
5443 res = -1;
5444 }
5445
5446 pr_memscrub(digest, digest_len);
5447 RSA_free(rsa);
5448 return res;
5449 }
5450
5451 static int rsa_verify_signed_data(pool *p, EVP_PKEY *pkey,
5452 unsigned char *signature, uint32_t signature_len,
5453 unsigned char *sig_data, size_t sig_datalen) {
5454 return verify_rsa_signed_data(p, pkey, signature, signature_len,
5455 sig_data, sig_datalen, EVP_sha1());
5456 }
5457
5458 #if defined(HAVE_SHA256_OPENSSL)
5459 static int rsa_sha256_verify_signed_data(pool *p, EVP_PKEY *pkey,
5460 unsigned char *signature, uint32_t signature_len,
5461 unsigned char *sig_data, size_t sig_datalen) {
5462 return verify_rsa_signed_data(p, pkey, signature, signature_len,
5463 sig_data, sig_datalen, EVP_sha256());
5464 }
5465 #endif /* HAVE_SHA256_OPENSSL */
5466
5467 #if defined(HAVE_SHA512_OPENSSL)
5468 static int rsa_sha512_verify_signed_data(pool *p, EVP_PKEY *pkey,
5469 unsigned char *signature, uint32_t signature_len,
5470 unsigned char *sig_data, size_t sig_datalen) {
5471 return verify_rsa_signed_data(p, pkey, signature, signature_len,
5472 sig_data, sig_datalen, EVP_sha512());
5473 }
5474 #endif /* HAVE_SHA512_OPENSSL */
5475
5476 #if !defined(OPENSSL_NO_DSA)
5477 static int dsa_verify_signed_data(pool *p, EVP_PKEY *pkey,
5478 unsigned char *signature, uint32_t signature_len,
5479 unsigned char *sig_data, size_t sig_datalen) {
5480 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
5481 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
5482 EVP_MD_CTX ctx;
5483 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5484 EVP_MD_CTX *pctx;
5485 DSA *dsa;
5486 DSA_SIG *dsa_sig;
5487 const BIGNUM *sig_r, *sig_s;
5488 uint32_t len, sig_len;
5489 unsigned char digest[EVP_MAX_MD_SIZE], *sig;
5490 unsigned int digest_len = 0;
5491 int ok = FALSE, res = 0;
5492
5493 len = sftp_msg_read_int2(p, &signature, &signature_len, &sig_len);
5494 if (len == 0) {
5495 errno = EINVAL;
5496 return -1;
5497 }
5498
5499 /* A DSA signature string is composed of 2 20 character parts. */
5500 if (sig_len != 40) {
5501 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5502 "bad DSA signature len (%lu)", (unsigned long) sig_len);
5503 errno = EINVAL;
5504 return -1;
5505 }
5506
5507 len = sftp_msg_read_data2(p, &signature, &signature_len, sig_len, &sig);
5508 if (len == 0) {
5509 errno = EINVAL;
5510 return -1;
5511 }
5512
5513 if (sig == NULL) {
5514 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5515 "error verifying DSA signature: missing signature data");
5516 errno = EINVAL;
5517 return -1;
5518 }
5519
5520 dsa = EVP_PKEY_get1_DSA(pkey);
5521 if (dsa == NULL) {
5522 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5523 "error obtaining DSA key: %s", sftp_crypto_get_errors());
5524 errno = EINVAL;
5525 return -1;
5526 }
5527
5528 if (keys_dsa_min_nbits > 0) {
5529 int dsa_nbits;
5530
5531 dsa_nbits = DSA_size(dsa) * 8;
5532 if (dsa_nbits < keys_dsa_min_nbits) {
5533 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5534 "DSA key size (%d bits) less than required minimum (%d bits)",
5535 dsa_nbits, keys_dsa_min_nbits);
5536 DSA_free(dsa);
5537
5538 errno = EINVAL;
5539 return -1;
5540 }
5541 }
5542
5543 dsa_sig = DSA_SIG_new();
5544 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5545 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5546 DSA_SIG_get0(dsa_sig, &sig_r, &sig_s);
5547 #else
5548 sig_r = dsa_sig->r;
5549 sig_s = dsa_sig->s;
5550 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5551
5552 sig_r = BN_bin2bn(sig, 20, (BIGNUM *) sig_r);
5553 if (sig_r == NULL) {
5554 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5555 "error obtaining 'r' DSA signature component: %s",
5556 sftp_crypto_get_errors());
5557 DSA_free(dsa);
5558 DSA_SIG_free(dsa_sig);
5559 return -1;
5560 }
5561
5562 sig_s = BN_bin2bn(sig + 20, 20, (BIGNUM *) sig_s);
5563 if (sig_s == NULL) {
5564 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5565 "error obtaining 's' DSA signature component: %s",
5566 sftp_crypto_get_errors());
5567 BN_clear_free((BIGNUM *) sig_r);
5568 DSA_free(dsa);
5569 DSA_SIG_free(dsa_sig);
5570 return -1;
5571 }
5572
5573 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5574 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5575 pctx = EVP_MD_CTX_new();
5576 #else
5577 pctx = &ctx;
5578 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5579
5580 EVP_DigestInit(pctx, EVP_sha1());
5581 EVP_DigestUpdate(pctx, sig_data, sig_datalen);
5582 EVP_DigestFinal(pctx, digest, &digest_len);
5583
5584 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5585 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5586 EVP_MD_CTX_free(pctx);
5587 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
5588
5589 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5590 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5591 # if OPENSSL_VERSION_NUMBER >= 0x10100006L
5592 DSA_SIG_set0(dsa_sig, (BIGNUM *) sig_r, (BIGNUM *) sig_s);
5593 # else
5594 /* XXX What to do here? */
5595 # endif /* prior to OpenSSL-1.1.0-pre6 */
5596 #else
5597 dsa_sig->r = sig_r;
5598 dsa_sig->s = sig_s;
5599 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5600
5601 ok = DSA_do_verify(digest, digest_len, dsa_sig, dsa);
5602 if (ok == 1) {
5603 res = 0;
5604
5605 } else {
5606 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5607 "error verifying DSA signature: %s", sftp_crypto_get_errors());
5608 res = -1;
5609 }
5610
5611 pr_memscrub(digest, digest_len);
5612 DSA_free(dsa);
5613 DSA_SIG_free(dsa_sig);
5614 return res;
5615 }
5616 #endif /* !OPENSSL_NO_DSA */
5617
5618 #if defined(PR_USE_OPENSSL_ECC)
5619 static int ecdsa_verify_signed_data(pool *p, EVP_PKEY *pkey,
5620 struct sftp_verify_details *details,
5621 unsigned char *signature, uint32_t signature_len,
5622 unsigned char *sig_data, size_t sig_datalen, char *sig_type) {
5623 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
5624 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
5625 EVP_MD_CTX ctx;
5626 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5627 EVP_MD_CTX *pctx;
5628 const EVP_MD *md = NULL;
5629 EC_KEY *ec;
5630 ECDSA_SIG *ecdsa_sig;
5631 const BIGNUM *sig_r, *sig_s;
5632 uint32_t len, sig_len;
5633 unsigned char digest[EVP_MAX_MD_SIZE], *sig;
5634 unsigned int digest_len = 0;
5635 int ok = FALSE, is_security_key_sig = FALSE, res = 0;
5636
5637 if (keys_ec_min_nbits > 0) {
5638 int ec_nbits;
5639
5640 ec_nbits = EVP_PKEY_bits(pkey) * 8;
5641 if (ec_nbits < keys_ec_min_nbits) {
5642 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5643 "EC key size (%d bits) less than required minimum (%d bits)",
5644 ec_nbits, keys_ec_min_nbits);
5645 errno = EINVAL;
5646 return -1;
5647 }
5648 }
5649
5650 len = sftp_msg_read_int2(p, &signature, &signature_len, &sig_len);
5651 if (len == 0) {
5652 errno = EINVAL;
5653 return -1;
5654 }
5655
5656 len = sftp_msg_read_data2(p, &signature, &signature_len, sig_len, &sig);
5657 if (len == 0) {
5658 errno = EINVAL;
5659 return -1;
5660 }
5661
5662 if (sig == NULL) {
5663 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5664 "error verifying ECDSA signature: missing signature data");
5665 errno = EINVAL;
5666 return -1;
5667 }
5668
5669 ecdsa_sig = ECDSA_SIG_new();
5670 if (ecdsa_sig == NULL) {
5671 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5672 "error allocating new ECDSA_SIG: %s", sftp_crypto_get_errors());
5673 return -1;
5674 }
5675
5676 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5677 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5678 ECDSA_SIG_get0(ecdsa_sig, &sig_r, &sig_s);
5679 #else
5680 sig_r = ecdsa_sig->r;
5681 sig_s = ecdsa_sig->s;
5682 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5683
5684 len = sftp_msg_read_mpint2(p, &sig, &sig_len, &sig_r);
5685 if (len == 0) {
5686 ECDSA_SIG_free(ecdsa_sig);
5687 errno = EINVAL;
5688 return -1;
5689 }
5690
5691 if (sig_r == NULL) {
5692 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5693 "error reading 'r' ECDSA signature component: %s",
5694 sftp_crypto_get_errors());
5695 ECDSA_SIG_free(ecdsa_sig);
5696 return -1;
5697 }
5698
5699 len = sftp_msg_read_mpint2(p, &sig, &sig_len, &sig_s);
5700 if (len == 0) {
5701 ECDSA_SIG_free(ecdsa_sig);
5702 errno = EINVAL;
5703 return -1;
5704 }
5705
5706 if (sig_s == NULL) {
5707 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5708 "error reading 's' ECDSA signature component: %s",
5709 sftp_crypto_get_errors());
5710 ECDSA_SIG_free(ecdsa_sig);
5711 return -1;
5712 }
5713
5714 if (strncmp(sig_type, "sk-", 3) == 0) {
5715 details->is_security_key = is_security_key_sig = TRUE;
5716 }
5717
5718 if (is_security_key_sig == FALSE) {
5719 /* Skip past the common leading prefix "ecdsa-sha2-" to compare just
5720 * last 9 characters.
5721 */
5722
5723 if (strncmp(sig_type + 11, "nistp256", 9) == 0) {
5724 md = EVP_sha256();
5725
5726 } else if (strncmp(sig_type + 11, "nistp384", 9) == 0) {
5727 md = EVP_sha384();
5728
5729 } else if (strncmp(sig_type + 11, "nistp521", 9) == 0) {
5730 md = EVP_sha512();
5731 }
5732
5733 } else {
5734 uint32_t sk_counter = 0;
5735 unsigned char sk_flags = 0;
5736
5737 /* ASSUME that we are dealing with an `[email protected]`
5738 * signature type.
5739 */
5740 md = EVP_sha256();
5741
5742 len = sftp_msg_read_byte2(p, &signature, &signature_len, &sk_flags);
5743 if (len == 0) {
5744 ECDSA_SIG_free(ecdsa_sig);
5745 errno = EINVAL;
5746 return -1;
5747 }
5748
5749 len = sftp_msg_read_int2(p, &signature, &signature_len, &sk_counter);
5750 if (len == 0) {
5751 ECDSA_SIG_free(ecdsa_sig);
5752 errno = EINVAL;
5753 return -1;
5754 }
5755
5756 details->sk_flags = sk_flags;
5757 details->sk_counter = sk_counter;
5758 }
5759
5760 ec = EVP_PKEY_get1_EC_KEY(pkey);
5761
5762 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5763 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5764 # if OPENSSL_VERSION_NUMBER >= 0x10100006L
5765 ECDSA_SIG_set0(ecdsa_sig, (BIGNUM *) sig_r, (BIGNUM *) sig_s);
5766 # else
5767 /* XXX What to do here? */
5768 # endif /* prior to OpenSSL-1.1.0-pre6 */
5769 #else
5770 ecdsa_sig->r = sig_r;
5771 ecdsa_sig->s = sig_s;
5772 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5773
5774 if (is_security_key_sig == FALSE) {
5775 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5776 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5777 pctx = EVP_MD_CTX_new();
5778 #else
5779 pctx = &ctx;
5780 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5781
5782 EVP_DigestInit(pctx, md);
5783 EVP_DigestUpdate(pctx, sig_data, sig_datalen);
5784 EVP_DigestFinal(pctx, digest, &digest_len);
5785
5786 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5787 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5788 EVP_MD_CTX_free(pctx);
5789 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
5790
5791 } else {
5792 unsigned char sk_digest[EVP_MAX_MD_SIZE], *buf, *ptr;
5793 unsigned int sk_digestlen = 0;
5794 uint32_t buflen, bufsz;
5795
5796 /* Needs to be large enough for the entired SK signed message, including
5797 * app name, flags, counter, extensions, and message.
5798 */
5799 bufsz = buflen = 256;
5800 ptr = buf = palloc(p, buflen);
5801
5802 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5803 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5804 pctx = EVP_MD_CTX_new();
5805 #else
5806 pctx = &ctx;
5807 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5808
5809 EVP_DigestInit(pctx, md);
5810 EVP_DigestUpdate(pctx, details->sk_application,
5811 strlen(details->sk_application));
5812 EVP_DigestFinal(pctx, sk_digest, &sk_digestlen);
5813
5814 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5815 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5816 EVP_MD_CTX_reset(pctx);
5817 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
5818
5819 sftp_msg_write_data(&buf, &buflen, sk_digest, sk_digestlen, FALSE);
5820
5821 sftp_msg_write_byte(&buf, &buflen, details->sk_flags);
5822 sftp_msg_write_int(&buf, &buflen, details->sk_counter);
5823
5824 /* XXX Currently there are no SK extensions defined/implemented. */
5825
5826 EVP_DigestInit(pctx, md);
5827 EVP_DigestUpdate(pctx, sig_data, sig_datalen);
5828 EVP_DigestFinal(pctx, sk_digest, &sk_digestlen);
5829
5830 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5831 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5832 EVP_MD_CTX_reset(pctx);
5833 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
5834
5835 sftp_msg_write_data(&buf, &buflen, sk_digest, sk_digestlen, FALSE);
5836 pr_memscrub(sk_digest, sk_digestlen);
5837
5838 EVP_DigestInit(pctx, md);
5839 EVP_DigestUpdate(pctx, ptr, (bufsz - buflen));
5840 EVP_DigestFinal(pctx, digest, &digest_len);
5841
5842 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5843 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5844 EVP_MD_CTX_free(pctx);
5845 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
5846 }
5847
5848 ok = ECDSA_do_verify(digest, digest_len, ecdsa_sig, ec);
5849 if (ok == 1) {
5850 res = 0;
5851
5852 } else {
5853 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5854 "error verifying %s ECDSA signature: %s", sig_type,
5855 sftp_crypto_get_errors());
5856 res = -1;
5857 }
5858
5859 pr_memscrub(digest, digest_len);
5860 EC_KEY_free(ec);
5861 ECDSA_SIG_free(ecdsa_sig);
5862 return res;
5863 }
5864 #endif /* PR_USE_OPENSSL_ECC */
5865
5866 #if defined(PR_USE_SODIUM)
5867 static int ed25519_verify_signed_data(pool *p,
5868 unsigned char *pubkey_data, uint32_t pubkey_datalen,
5869 struct sftp_verify_details *details,
5870 unsigned char *signature, uint32_t signature_len,
5871 unsigned char *sig_data, size_t sig_datalen) {
5872 char *pkey_type;
5873 uint32_t len, public_keylen, sig_len;
5874 unsigned char *msg, *public_key, *signed_msg, *sig;
5875 unsigned long long msg_len, signed_msglen;
5876 int res, is_security_key_sig = FALSE;
5877
5878 len = sftp_msg_read_string2(p, &pubkey_data, &pubkey_datalen, &pkey_type);
5879 if (len == 0) {
5880 errno = EINVAL;
5881 return -1;
5882 }
5883
5884 if (strcmp(pkey_type, "ssh-ed25519") != 0 &&
5885 strcmp(pkey_type, "[email protected]") != 0) {
5886 pr_trace_msg(trace_channel, 17,
5887 "public key type '%s' does not match expected key types "
5888 "'ssh-ed25519', '[email protected]'", pkey_type);
5889 errno = EINVAL;
5890 return -1;
5891 }
5892
5893 if (strcmp(pkey_type, "[email protected]") == 0) {
5894 details->is_security_key = is_security_key_sig = TRUE;
5895 }
5896
5897 len = sftp_msg_read_int2(p, &pubkey_data, &pubkey_datalen, &public_keylen);
5898 if (len == 0) {
5899 errno = EINVAL;
5900 return -1;
5901 }
5902
5903 if (public_keylen != crypto_sign_ed25519_PUBLICKEYBYTES) {
5904 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5905 "invalid %s public key length (%lu bytes), expected %lu bytes", pkey_type,
5906 (unsigned long) public_keylen,
5907 (unsigned long) crypto_sign_ed25519_PUBLICKEYBYTES);
5908 errno = EINVAL;
5909 return -1;
5910 }
5911
5912 len = sftp_msg_read_data2(p, &pubkey_data, &pubkey_datalen, public_keylen,
5913 &public_key);
5914 if (len == 0) {
5915 errno = EINVAL;
5916 return -1;
5917 }
5918
5919 len = sftp_msg_read_int2(p, &signature, &signature_len, &sig_len);
5920 if (len == 0) {
5921 errno = EINVAL;
5922 return -1;
5923 }
5924
5925 len = sftp_msg_read_data2(p, &signature, &signature_len, sig_len, &sig);
5926 if (len == 0) {
5927 errno = EINVAL;
5928 return -1;
5929 }
5930
5931 if (sig == NULL) {
5932 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5933 "error verifying Ed25519 signature: missing signature data");
5934 errno = EINVAL;
5935 return -1;
5936 }
5937
5938 if (sig_len > crypto_sign_ed25519_BYTES) {
5939 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5940 "Ed25519 signature length (%lu bytes) exceeds valid length (%lu bytes)",
5941 (unsigned long) sig_len, (unsigned long) crypto_sign_ed25519_BYTES);
5942 errno = EINVAL;
5943 return -1;
5944 }
5945
5946 if (is_security_key_sig == FALSE) {
5947 signed_msglen = sig_len + sig_datalen;
5948 signed_msg = palloc(p, signed_msglen);
5949 memcpy(signed_msg, sig, sig_len);
5950 memcpy(signed_msg + sig_len, sig_data, sig_datalen);
5951
5952 } else {
5953 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
5954 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
5955 EVP_MD_CTX ctx;
5956 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5957 EVP_MD_CTX *pctx;
5958 const EVP_MD *md = NULL;
5959 uint32_t sk_counter = 0, bufsz, buflen;
5960 unsigned char sk_digest[EVP_MAX_MD_SIZE], sk_flags = 0, *buf, *ptr;
5961 unsigned int sk_digestlen = 0;
5962
5963 md = EVP_sha256();
5964
5965 len = sftp_msg_read_byte2(p, &signature, &signature_len, &sk_flags);
5966 if (len == 0) {
5967 errno = EINVAL;
5968 return -1;
5969 }
5970
5971 len = sftp_msg_read_int2(p, &signature, &signature_len, &sk_counter);
5972 if (len == 0) {
5973 errno = EINVAL;
5974 return -1;
5975 }
5976
5977 details->sk_flags = sk_flags;
5978 details->sk_counter = sk_counter;
5979
5980 /* Needs to be large enough for the entired SK signed message, including
5981 * app name, flags, counter, extensions, and message.
5982 */
5983 bufsz = buflen = 256;
5984 ptr = buf = palloc(p, buflen);
5985
5986 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5987 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
5988 pctx = EVP_MD_CTX_new();
5989 #else
5990 pctx = &ctx;
5991 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
5992
5993 EVP_DigestInit(pctx, md);
5994 EVP_DigestUpdate(pctx, details->sk_application,
5995 strlen(details->sk_application));
5996 EVP_DigestFinal(pctx, sk_digest, &sk_digestlen);
5997
5998 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
5999 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
6000 EVP_MD_CTX_reset(pctx);
6001 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
6002
6003 sftp_msg_write_data(&buf, &buflen, sk_digest, sk_digestlen, FALSE);
6004
6005 sftp_msg_write_byte(&buf, &buflen, details->sk_flags);
6006 sftp_msg_write_int(&buf, &buflen, details->sk_counter);
6007
6008 /* XXX Currently there are no SK extensions defined/implemented. */
6009
6010 EVP_DigestInit(pctx, md);
6011 EVP_DigestUpdate(pctx, sig_data, sig_datalen);
6012 EVP_DigestFinal(pctx, sk_digest, &sk_digestlen);
6013
6014 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(HAVE_LIBRESSL)) || \
6015 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER >= 0x3050000L)
6016 EVP_MD_CTX_free(pctx);
6017 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
6018
6019 sftp_msg_write_data(&buf, &buflen, sk_digest, sk_digestlen, FALSE);
6020 pr_memscrub(sk_digest, sk_digestlen);
6021
6022 signed_msglen = sig_len + (bufsz - buflen);
6023 signed_msg = palloc(p, signed_msglen);
6024 memcpy(signed_msg, sig, sig_len);
6025 memcpy(signed_msg + sig_len, ptr, (bufsz - buflen));
6026 }
6027
6028 msg_len = signed_msglen;
6029 msg = palloc(p, msg_len);
6030
6031 res = crypto_sign_ed25519_open(msg, &msg_len, signed_msg, signed_msglen,
6032 public_key);
6033 if (res != 0) {
6034 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6035 "failed Ed25519 signature verification (%d)", res);
6036 res = -1;
6037 }
6038
6039 if (res == 0) {
6040 if (is_security_key_sig == FALSE &&
6041 msg_len != sig_datalen) {
6042 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6043 "invalid Ed25519 signature length (%lu bytes), expected %lu bytes",
6044 (unsigned long) sig_datalen, (unsigned long) msg_len);
6045 errno = EINVAL;
6046 res = -1;
6047 }
6048 }
6049
6050 if (res == 0) {
6051 if (is_security_key_sig == FALSE &&
6052 sodium_memcmp(msg, sig_data, msg_len) != 0) {
6053 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6054 "invalid Ed25519 signature (mismatched data)");
6055 errno = EINVAL;
6056 res = -1;
6057 }
6058 }
6059
6060 pr_memscrub(signed_msg, signed_msglen);
6061 pr_memscrub(msg, msg_len);
6062 return res;
6063 }
6064 #endif /* PR_USE_SODIUM */
6065
6066 #if defined(HAVE_X448_OPENSSL)
6067 static int ed448_verify_signed_data(pool *p,
6068 unsigned char *pubkey_data, uint32_t pubkey_datalen,
6069 unsigned char *signature, uint32_t signature_len,
6070 unsigned char *sig_data, size_t sig_datalen) {
6071 char *pkey_type;
6072 uint32_t len, public_keylen, sig_len;
6073 unsigned char *public_key, *sig;
6074 EVP_MD_CTX *md_ctx = NULL;
6075 EVP_PKEY *pkey = NULL;
6076
6077 len = sftp_msg_read_string2(p, &pubkey_data, &pubkey_datalen, &pkey_type);
6078 if (len == 0) {
6079 errno = EINVAL;
6080 return -1;
6081 }
6082
6083 if (strcmp(pkey_type, "ssh-ed448") != 0) {
6084 pr_trace_msg(trace_channel, 17,
6085 "public key type '%s' does not match expected key type 'ssh-ed448'",
6086 pkey_type);
6087 errno = EINVAL;
6088 return -1;
6089 }
6090
6091 len = sftp_msg_read_int2(p, &pubkey_data, &pubkey_datalen, &public_keylen);
6092 if (len == 0) {
6093 errno = EINVAL;
6094 return -1;
6095 }
6096
6097 if (public_keylen != CURVE448_SIZE &&
6098 public_keylen != (CURVE448_SIZE + 1)) {
6099 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6100 "invalid Ed448 public key length (%lu bytes), expected %lu bytes",
6101 (unsigned long) public_keylen, (unsigned long) CURVE448_SIZE);
6102 errno = EINVAL;
6103 return -1;
6104 }
6105
6106 len = sftp_msg_read_data2(p, &pubkey_data, &pubkey_datalen, public_keylen,
6107 &public_key);
6108 if (len == 0) {
6109 errno = EINVAL;
6110 return -1;
6111 }
6112
6113 len = sftp_msg_read_int2(p, &signature, &signature_len, &sig_len);
6114 if (len == 0) {
6115 errno = EINVAL;
6116 return -1;
6117 }
6118
6119 len = sftp_msg_read_data2(p, &signature, &signature_len, sig_len, &sig);
6120 if (len == 0) {
6121 errno = EINVAL;
6122 return -1;
6123 }
6124
6125 if (sig == NULL) {
6126 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6127 "error verifying Ed448 signature: missing signature data");
6128 errno = EINVAL;
6129 return -1;
6130 }
6131
6132 pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED448, NULL, public_key,
6133 public_keylen);
6134 if (pkey == NULL) {
6135 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6136 "error initializing Ed448 public key: %s", sftp_crypto_get_errors());
6137 return -1;
6138 }
6139
6140 md_ctx = EVP_MD_CTX_new();
6141 if (EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, pkey) != 1) {
6142 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6143 "error initializing Ed448 verification: %s", sftp_crypto_get_errors());
6144 EVP_MD_CTX_free(md_ctx);
6145 EVP_PKEY_free(pkey);
6146 return -1;
6147 }
6148
6149 if (EVP_DigestVerify(md_ctx, sig, sig_len, sig_data, sig_datalen) != 1) {
6150 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6151 "invalid Ed448 signature (mismatched data)");
6152 EVP_MD_CTX_free(md_ctx);
6153 EVP_PKEY_free(pkey);
6154 errno = EINVAL;
6155 return -1;
6156 }
6157
6158 return 0;
6159 }
6160 #endif /* HAVE_X448_OPENSSL */
6161
6162 int sftp_keys_verify_signed_data(pool *p, const char *pubkey_algo,
6163 unsigned char *pubkey_data, uint32_t pubkey_datalen,
6164 unsigned char *signature, uint32_t signature_len,
6165 unsigned char *sig_data, size_t sig_datalen,
6166 struct sftp_verify_details *details) {
6167 EVP_PKEY *pkey = NULL;
6168 char *sig_type;
6169 uint32_t len;
6170 int res = 0;
6171
6172 if (pubkey_algo == NULL ||
6173 pubkey_data == NULL ||
6174 signature == NULL ||
6175 sig_data == NULL ||
6176 sig_datalen == 0) {
6177 errno = EINVAL;
6178 return -1;
6179 }
6180
6181 len = read_pkey_from_data(p, pubkey_data, pubkey_datalen, &pkey, NULL,
6182 details, FALSE);
6183 if (len == 0) {
6184 return -1;
6185 }
6186
6187 if (strcmp(pubkey_algo, "ssh-dss") == 0) {
6188 if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO_IN_DSA_SIG)) {
6189 len = sftp_msg_read_string2(p, &signature, &signature_len, &sig_type);
6190 if (len == 0) {
6191 errno = EINVAL;
6192 return -1;
6193 }
6194
6195 } else {
6196 /* The client did not prepend the public key algorithm name to their
6197 * signature data, so there is no need to extract that string.
6198 * We will ASSUME that the public key algorithm provided elsewhere
6199 * in the 'publickey' USERAUTH_REQUEST is accurate.
6200 */
6201 pr_trace_msg(trace_channel, 9, "assuming client did not prepend public "
6202 "key algorithm name to DSA signature");
6203 sig_type = "ssh-dss";
6204 }
6205
6206 } else {
6207 len = sftp_msg_read_string2(p, &signature, &signature_len, &sig_type);
6208 if (len == 0) {
6209 errno = EINVAL;
6210 return -1;
6211 }
6212 }
6213
6214 if (strcmp(sig_type, "ssh-rsa") == 0) {
6215 if (strcmp(pubkey_algo, sig_type) != 0) {
6216 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6217 "unable to verify signed data: signature type '%s' does not match "
6218 "publickey algorithm '%s'", sig_type, pubkey_algo);
6219 errno = EINVAL;
6220 return -1;
6221 }
6222
6223 res = rsa_verify_signed_data(p, pkey, signature, signature_len, sig_data,
6224 sig_datalen);
6225
6226 #if defined(HAVE_SHA256_OPENSSL)
6227 } else if (strcmp(sig_type, "rsa-sha2-256") == 0) {
6228 if (strcmp(pubkey_algo, sig_type) != 0 &&
6229 strcmp(pubkey_algo, "ssh-rsa") != 0) {
6230 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6231 "unable to verify signed data: signature type '%s' does not match "
6232 "publickey algorithm '%s'", sig_type, pubkey_algo);
6233 errno = EINVAL;
6234 return -1;
6235 }
6236
6237 res = rsa_sha256_verify_signed_data(p, pkey, signature, signature_len,
6238 sig_data, sig_datalen);
6239 #endif /* HAVE_SHA256_OPENSSL */
6240
6241 #if defined(HAVE_SHA512_OPENSSL)
6242 } else if (strcmp(sig_type, "rsa-sha2-512") == 0) {
6243 if (strcmp(pubkey_algo, sig_type) != 0 &&
6244 strcmp(pubkey_algo, "ssh-rsa") != 0) {
6245 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6246 "unable to verify signed data: signature type '%s' does not match "
6247 "publickey algorithm '%s'", sig_type, pubkey_algo);
6248 errno = EINVAL;
6249 return -1;
6250 }
6251
6252 res = rsa_sha512_verify_signed_data(p, pkey, signature, signature_len,
6253 sig_data, sig_datalen);
6254 #endif /* HAVE_SHA512_OPENSSL */
6255
6256 #if !defined(OPENSSL_NO_DSA)
6257 } else if (strcmp(sig_type, "ssh-dss") == 0) {
6258 if (strcmp(pubkey_algo, sig_type) != 0) {
6259 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6260 "unable to verify signed data: signature type '%s' does not match "
6261 "publickey algorithm '%s'", sig_type, pubkey_algo);
6262 errno = EINVAL;
6263 return -1;
6264 }
6265
6266 res = dsa_verify_signed_data(p, pkey, signature, signature_len, sig_data,
6267 sig_datalen);
6268 #endif /* !OPENSSL_NO_DSA */
6269
6270 #if defined(PR_USE_OPENSSL_ECC)
6271 } else if (strcmp(sig_type, "ecdsa-sha2-nistp256") == 0 ||
6272 strcmp(sig_type, "ecdsa-sha2-nistp384") == 0 ||
6273 strcmp(sig_type, "ecdsa-sha2-nistp521") == 0 ||
6274 strcmp(sig_type, "[email protected]") == 0) {
6275
6276 if (strcmp(pubkey_algo, sig_type) != 0) {
6277 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6278 "unable to verify signed data: public key algorithm '%s' does not "
6279 "match signature algorithm '%s'", pubkey_algo, sig_type);
6280 return -1;
6281 }
6282
6283 res = ecdsa_verify_signed_data(p, pkey, details, signature,
6284 signature_len, sig_data, sig_datalen, sig_type);
6285 #endif /* PR_USE_OPENSSL_ECC */
6286
6287 #if defined(PR_USE_SODIUM)
6288 } else if (strcmp(sig_type, "ssh-ed25519") == 0 ||
6289 strcmp(sig_type, "[email protected]") == 0) {
6290 res = ed25519_verify_signed_data(p, pubkey_data, pubkey_datalen,
6291 details, signature, signature_len, sig_data, sig_datalen);
6292 #endif /* PR_USE_SODIUM */
6293
6294 #if defined(HAVE_X448_OPENSSL)
6295 } else if (strcmp(sig_type, "ssh-ed448") == 0) {
6296 res = ed448_verify_signed_data(p, pubkey_data, pubkey_datalen, signature,
6297 signature_len, sig_data, sig_datalen);
6298 #endif /* HAVE_X448_OPENSSL */
6299
6300 } else {
6301 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6302 "unable to verify signed data: unsupported signature algorithm '%s'",
6303 sig_type);
6304 errno = EINVAL;
6305 return -1;
6306 }
6307
6308 if (pkey != NULL) {
6309 EVP_PKEY_free(pkey);
6310 }
6311
6312 return res;
6313 }
6314
6315 /* This is where we check for, enforce, any FIDO/SK policies. */
6316 int sftp_keys_permit_key(pool *p, const char *pubkey_algo, const char *username,
6317 struct sftp_verify_details *details, pr_table_t *notes) {
6318 int fido_touch_required, fido_verify_required;
6319 const void *val;
6320 size_t valsz = 0;
6321
6322 if (details->is_security_key == FALSE) {
6323 return 0;
6324 }
6325
6326 pr_trace_msg(trace_channel, 19, "checking security key policy: "
6327 "application = '%s', flags = %d, counter = %lu", details->sk_application,
6328 details->sk_flags, (unsigned long) details->sk_counter);
6329
6330 /* Is FIDO touch required? */
6331 fido_touch_required = TRUE;
6332
6333 if (!(sftp_opts & SFTP_OPT_FIDO_TOUCH_REQUIRED)) {
6334 val = pr_table_get(notes, SFTP_KEYSTORE_HEADER_FIDO_TOUCH_REQUIRED, &valsz);
6335 if (val != NULL) {
6336 const char *text;
6337
6338 text = val;
6339 pr_trace_msg(trace_channel, 19,
6340 "found %s verification note for key: '%s'",
6341 SFTP_KEYSTORE_HEADER_FIDO_TOUCH_REQUIRED, text);
6342
6343 fido_touch_required = pr_str_is_boolean(text);
6344 }
6345 }
6346
6347 if (fido_touch_required == TRUE &&
6348 !(details->sk_flags & SFTP_KEYS_SK_USER_PRESENCE_REQUIRED)) {
6349 const pr_netaddr_t *remote_addr;
6350
6351 remote_addr = pr_netaddr_get_sess_remote_addr();
6352
6353 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6354 "public key '%s' signature for %s from %.128s:%u rejected: "
6355 "user presence (authenticator touch) requirement not met",
6356 pubkey_algo, username, pr_netaddr_get_ipstr(remote_addr),
6357 ntohs(pr_netaddr_get_port(remote_addr)));
6358 errno = EACCES;
6359 return -1;
6360 }
6361
6362 /* Is FIDO verify required? */
6363 fido_verify_required = FALSE;
6364
6365 if (!(sftp_opts & SFTP_OPT_FIDO_VERIFY_REQUIRED)) {
6366 val = pr_table_get(notes, SFTP_KEYSTORE_HEADER_FIDO_VERIFY_REQUIRED,
6367 &valsz);
6368 if (val != NULL) {
6369 const char *text;
6370
6371 text = val;
6372 pr_trace_msg(trace_channel, 19,
6373 "found %s verification note for key: '%s'",
6374 SFTP_KEYSTORE_HEADER_FIDO_VERIFY_REQUIRED, text);
6375
6376 fido_verify_required = pr_str_is_boolean(text);
6377 }
6378
6379 } else {
6380 fido_verify_required = TRUE;
6381 }
6382
6383 if (fido_verify_required == TRUE &&
6384 !(details->sk_flags & SFTP_KEYS_SK_USER_VERIFICATION_REQUIRED)) {
6385 const pr_netaddr_t *remote_addr;
6386
6387 remote_addr = pr_netaddr_get_sess_remote_addr();
6388
6389 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6390 "public key '%s' signature for %s from %.128s:%u rejected: "
6391 "user verification requirement not met",
6392 pubkey_algo, username, pr_netaddr_get_ipstr(remote_addr),
6393 ntohs(pr_netaddr_get_port(remote_addr)));
6394 errno = EACCES;
6395 return -1;
6396 }
6397
6398 return 0;
6399 }
6400
6401 int sftp_keys_set_key_limits(int rsa_min, int dsa_min, int ec_min) {
6402 /* Ignore any negative values. */
6403
6404 if (rsa_min >= 0) {
6405 keys_rsa_min_nbits = (unsigned int) rsa_min;
6406 }
6407
6408 if (dsa_min >= 0) {
6409 keys_dsa_min_nbits = (unsigned int) dsa_min;
6410 }
6411
6412 if (ec_min >= 0) {
6413 keys_ec_min_nbits = (unsigned int) ec_min;
6414 }
6415
6416 return 0;
6417 }
6418
6419 /* Send "[email protected]" GLOBAL_REQUEST with our hostkeys. */
6420 int sftp_keys_send_hostkeys(pool *p) {
6421 int res, *nids = NULL;
6422 pool *tmp_pool;
6423 struct ssh2_packet *pkt;
6424 unsigned char *buf, *ptr;
6425 const unsigned char *hostkey_data = NULL;
6426 uint32_t buflen, bufsz, hostkey_datalen = 0;
6427
6428 if (!sftp_interop_supports_feature(SFTP_SSH2_FEAT_HOSTKEYS)) {
6429 return 0;
6430 }
6431
6432 if (sftp_opts & SFTP_OPT_NO_HOSTKEY_ROTATION) {
6433 return 0;
6434 }
6435
6436 tmp_pool = make_sub_pool(p);
6437 pr_pool_tag(tmp_pool, "[email protected] pool");
6438 pkt = sftp_ssh2_packet_create(tmp_pool);
6439
6440 /* Allocate 32K, for all of the possible hostkeys. */
6441 buflen = bufsz = 32768;
6442 ptr = buf = palloc(pkt->pool, bufsz);
6443
6444 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_GLOBAL_REQUEST);
6445 sftp_msg_write_string(&buf, &buflen, "[email protected]");
6446 sftp_msg_write_bool(&buf, &buflen, FALSE);
6447
6448 res = sftp_keys_have_rsa_hostkey();
6449 if (res == 0) {
6450 hostkey_data = sftp_keys_get_hostkey_data(tmp_pool, SFTP_KEY_RSA,
6451 &hostkey_datalen);
6452 if (hostkey_data != NULL) {
6453 pr_trace_msg(trace_channel, 17, "adding RSA hostkey to hostkeys message");
6454 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
6455 }
6456 }
6457
6458 res = sftp_keys_have_dsa_hostkey();
6459 if (res == 0) {
6460 hostkey_data = sftp_keys_get_hostkey_data(tmp_pool, SFTP_KEY_DSA,
6461 &hostkey_datalen);
6462 if (hostkey_data != NULL) {
6463 pr_trace_msg(trace_channel, 17, "adding DSA hostkey to hostkeys message");
6464 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
6465 }
6466 }
6467
6468 res = sftp_keys_have_ed25519_hostkey();
6469 if (res == 0) {
6470 hostkey_data = sftp_keys_get_hostkey_data(tmp_pool, SFTP_KEY_ED25519,
6471 &hostkey_datalen);
6472 if (hostkey_data != NULL) {
6473 pr_trace_msg(trace_channel, 17,
6474 "adding Ed25519 hostkey to hostkeys message");
6475 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
6476 }
6477 }
6478
6479 res = sftp_keys_have_ed448_hostkey();
6480 if (res == 0) {
6481 hostkey_data = sftp_keys_get_hostkey_data(tmp_pool, SFTP_KEY_ED448,
6482 &hostkey_datalen);
6483 if (hostkey_data != NULL) {
6484 pr_trace_msg(trace_channel, 17,
6485 "adding Ed448 hostkey to hostkeys message");
6486 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
6487 }
6488 }
6489
6490 res = sftp_keys_have_ecdsa_hostkey(tmp_pool, &nids);
6491 #if defined(PR_USE_OPENSSL_ECC)
6492 if (res > 0) {
6493 register int i;
6494
6495 for (i = 0; i < res; i++) {
6496 enum sftp_key_type_e key_type;
6497 const char *key_desc;
6498
6499 switch (nids[i]) {
6500 case NID_X9_62_prime256v1:
6501 key_type = SFTP_KEY_ECDSA_256;
6502 key_desc = "ECDSA256";
6503 break;
6504
6505 case NID_secp384r1:
6506 key_type = SFTP_KEY_ECDSA_384;
6507 key_desc = "ECDSA384";
6508 break;
6509
6510 case NID_secp521r1:
6511 key_type = SFTP_KEY_ECDSA_521;
6512 key_desc = "ECDSA521";
6513 break;
6514
6515 default:
6516 continue;
6517 }
6518
6519 hostkey_data = sftp_keys_get_hostkey_data(tmp_pool, key_type,
6520 &hostkey_datalen);
6521 if (hostkey_data != NULL) {
6522 pr_trace_msg(trace_channel, 17,
6523 "adding %s hostkey to hostkeys message", key_desc);
6524 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
6525 }
6526 }
6527 }
6528 #endif /* PR_USE_OPENSSL_ECC */
6529
6530 pkt->payload = ptr;
6531 pkt->payload_len = (bufsz - buflen);
6532
6533 pr_trace_msg(trace_channel, 17,
6534 "sending '[email protected]' GLOBAL_REQUEST (%lu bytes)",
6535 (unsigned long) pkt->payload_len);
6536 (void) sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
6537 destroy_pool(tmp_pool);
6538
6539 return 0;
6540 }
6541
6542 static enum sftp_key_type_e get_hostkey_type(pool *p,
6543 const unsigned char *hostkey_data, uint32_t hostkey_datalen) {
6544 unsigned char *buf;
6545 uint32_t buflen;
6546 char *key_desc;
6547 enum sftp_key_type_e key_type = SFTP_KEY_UNKNOWN;
6548
6549 buf = (unsigned char *) hostkey_data;
6550 buflen = hostkey_datalen;
6551 key_desc = sftp_msg_read_string(p, &buf, &buflen);
6552
6553 if (strcmp(key_desc, "ssh-rsa") == 0) {
6554 key_type = SFTP_KEY_RSA;
6555
6556 } else if (strcmp(key_desc, "ssh-dss") == 0) {
6557 key_type = SFTP_KEY_DSA;
6558
6559 } else if (strcmp(key_desc, "ssh-ed25519") == 0) {
6560 key_type = SFTP_KEY_ED25519;
6561
6562 } else if (strcmp(key_desc, "ssh-ed448") == 0) {
6563 key_type = SFTP_KEY_ED448;
6564
6565 } else if (strcmp(key_desc, "ecdsa-sha2-nistp256") == 0) {
6566 key_type = SFTP_KEY_ECDSA_256;
6567
6568 } else if (strcmp(key_desc, "ecdsa-sha2-nistp384") == 0) {
6569 key_type = SFTP_KEY_ECDSA_384;
6570
6571 } else if (strcmp(key_desc, "ecdsa-sha2-nistp521") == 0) {
6572 key_type = SFTP_KEY_ECDSA_521;
6573 }
6574
6575 return key_type;
6576 }
6577
6578 static int have_hostkey(pool *p, enum sftp_key_type_e hostkey_type,
6579 const unsigned char *hostkey_data, uint32_t hostkey_datalen) {
6580 const unsigned char *local_hostkey_data = NULL;
6581 uint32_t local_hostkey_datalen = 0;
6582
6583 switch (hostkey_type) {
6584 case SFTP_KEY_RSA:
6585 case SFTP_KEY_RSA_SHA256:
6586 case SFTP_KEY_RSA_SHA512:
6587 if (sftp_keys_have_rsa_hostkey() == 0) {
6588 local_hostkey_data = sftp_keys_get_hostkey_data(p, hostkey_type,
6589 &local_hostkey_datalen);
6590 }
6591 break;
6592
6593 case SFTP_KEY_DSA:
6594 if (sftp_keys_have_dsa_hostkey() == 0) {
6595 local_hostkey_data = sftp_keys_get_hostkey_data(p, hostkey_type,
6596 &local_hostkey_datalen);
6597 }
6598 break;
6599
6600 case SFTP_KEY_ED25519:
6601 if (sftp_keys_have_ed25519_hostkey() == 0) {
6602 local_hostkey_data = sftp_keys_get_hostkey_data(p, hostkey_type,
6603 &local_hostkey_datalen);
6604 }
6605 break;
6606
6607 case SFTP_KEY_ED448:
6608 if (sftp_keys_have_ed448_hostkey() == 0) {
6609 local_hostkey_data = sftp_keys_get_hostkey_data(p, hostkey_type,
6610 &local_hostkey_datalen);
6611 }
6612 break;
6613
6614 case SFTP_KEY_ECDSA_256:
6615 case SFTP_KEY_ECDSA_384:
6616 case SFTP_KEY_ECDSA_521:
6617 if (sftp_keys_have_ecdsa_hostkey(p, NULL) > 0) {
6618 local_hostkey_data = sftp_keys_get_hostkey_data(p, hostkey_type,
6619 &local_hostkey_datalen);
6620 }
6621 break;
6622
6623 default:
6624 /* Unknown. */
6625 break;
6626 }
6627
6628 if (local_hostkey_data == NULL) {
6629 return -1;
6630 }
6631
6632 if (hostkey_datalen != local_hostkey_datalen) {
6633 return -1;
6634 }
6635
6636 if (memcmp(hostkey_data, local_hostkey_data, hostkey_datalen) != 0) {
6637 return -1;
6638 }
6639
6640 return 0;
6641 }
6642
6643 static const unsigned char *prove_hostkey(pool *p,
6644 const unsigned char *hostkey_data, uint32_t hostkey_datalen,
6645 size_t *hsiglen) {
6646 pool *tmp_pool;
6647 enum sftp_key_type_e hostkey_type;
6648 const unsigned char *session_id, *hsig;
6649 unsigned char *buf, *ptr;
6650 uint32_t buflen, bufsz, session_idsz;
6651
6652 tmp_pool = make_sub_pool(p);
6653 pr_pool_tag(tmp_pool, "[email protected] pool");
6654
6655 buflen = bufsz = 8192;
6656 ptr = buf = palloc(tmp_pool, bufsz);
6657
6658 sftp_msg_write_string(&buf, &buflen, "[email protected]");
6659 session_idsz = sftp_session_get_id(&session_id);
6660 sftp_msg_write_data(&buf, &buflen, session_id, session_idsz, TRUE);
6661 sftp_msg_write_data(&buf, &buflen, hostkey_data, hostkey_datalen, TRUE);
6662
6663 hostkey_type = get_hostkey_type(tmp_pool, hostkey_data, hostkey_datalen);
6664
6665 pr_trace_msg(trace_channel, 17, "proving possession of %s hostkey",
6666 get_key_type_desc(hostkey_type));
6667
6668 /* Is this even one of our hostkeys? Use the detected hostkey type to
6669 * make for a faster search.
6670 */
6671 if (have_hostkey(tmp_pool, hostkey_type, hostkey_data, hostkey_datalen) < 0) {
6672 pr_trace_msg(trace_channel, 7, "unknown %s hostkey proof requested",
6673 get_key_type_desc(hostkey_type));
6674 destroy_pool(tmp_pool);
6675 return NULL;
6676 }
6677
6678 /* Note that the hostkey type used for signing, for RSA keys, should take
6679 * into account the KEX hostkey sig type, such as for SHA256/SHA512 signatures
6680 * for RSA keys.
6681 */
6682 if (hostkey_type == SFTP_KEY_RSA) {
6683 enum sftp_key_type_e kex_hostkey_type;
6684
6685 kex_hostkey_type = sftp_kex_get_hostkey_type();
6686 switch (kex_hostkey_type) {
6687 case SFTP_KEY_RSA:
6688 case SFTP_KEY_RSA_SHA256:
6689 case SFTP_KEY_RSA_SHA512:
6690 hostkey_type = kex_hostkey_type;
6691 break;
6692
6693 default:
6694 /* Ignore */
6695 break;
6696 }
6697 }
6698
6699 hsig = sftp_keys_sign_data(p, hostkey_type, ptr, (bufsz - buflen), hsiglen);
6700 if (hsig == NULL) {
6701 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6702 "error proving hostkey");
6703 }
6704
6705 destroy_pool(tmp_pool);
6706 return hsig;
6707 }
6708
6709 static int prove_hostkeys_failed(pool *p) {
6710 unsigned char *buf, *ptr;
6711 uint32_t buflen, bufsz;
6712 struct ssh2_packet *pkt;
6713
6714 pkt = sftp_ssh2_packet_create(p);
6715 buflen = bufsz = 256;
6716 ptr = buf = palloc(pkt->pool, bufsz);
6717
6718 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_REQUEST_FAILURE);
6719
6720 pkt->payload = ptr;
6721 pkt->payload_len = (bufsz - buflen);
6722
6723 return sftp_ssh2_packet_write(sftp_conn->wfd, pkt);
6724 }
6725
6726 /* Handle "[email protected]" GLOBAL_REQUEST from client. */
6727 int sftp_keys_prove_hostkeys(pool *p, int want_reply, unsigned char *buf,
6728 uint32_t buflen) {
6729 int res;
6730 unsigned char *buf2, *ptr2;
6731 uint32_t buflen2, bufsz2;
6732 struct ssh2_packet *pkt2;
6733
6734 /* No point in doing the work, if the client does not care about a reply. */
6735 if (want_reply == FALSE) {
6736 return 0;
6737 }
6738
6739 if (sftp_opts & SFTP_OPT_NO_HOSTKEY_ROTATION) {
6740 return prove_hostkeys_failed(p);
6741 }
6742
6743 pr_trace_msg(trace_channel, 16,
6744 "handling '[email protected]' request (%lu bytes)",
6745 (unsigned long) buflen);
6746
6747 pkt2 = sftp_ssh2_packet_create(p);
6748 buflen2 = bufsz2 = 32768;
6749 ptr2 = buf2 = palloc(pkt2->pool, bufsz2);
6750
6751 sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_REQUEST_SUCCESS);
6752
6753 while (buflen != 0) {
6754 const unsigned char *hsig = NULL;
6755 unsigned char *hostkey_data = NULL;
6756 uint32_t hostkey_datalen = 0;
6757 size_t hsiglen = 0;
6758
6759 pr_signals_handle();
6760
6761 hostkey_datalen = sftp_msg_read_int(p, &buf, &buflen);
6762 hostkey_data = sftp_msg_read_data(p, &buf, &buflen, hostkey_datalen);
6763
6764 hsig = prove_hostkey(p, hostkey_data, hostkey_datalen, &hsiglen);
6765 if (hsig == NULL) {
6766 return prove_hostkeys_failed(p);
6767 }
6768
6769 sftp_msg_write_data(&buf2, &buflen2, hsig, hsiglen, TRUE);
6770 }
6771
6772 pkt2->payload = ptr2;
6773 pkt2->payload_len = (bufsz2 - buflen2);
6774
6775 res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt2);
6776 if (res < 0) {
6777 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6778 "error writing '[email protected]' response: %s",
6779 strerror(errno));
6780 }
6781
6782 return 0;
6783 }
6784
6785 int sftp_keys_set_passphrase_provider(const char *provider) {
6786 if (provider == NULL) {
6787 errno = EINVAL;
6788 return -1;
6789 }
6790
6791 passphrase_provider = provider;
6792 return 0;
6793 }
6794
6795 void sftp_keys_get_passphrases(void) {
6796 server_rec *s = NULL;
6797
6798 for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
6799 config_rec *c;
6800 struct sftp_pkey *k;
6801
6802 c = find_config(s->conf, CONF_PARAM, "SFTPHostKey", FALSE);
6803 while (c != NULL) {
6804 int flags;
6805
6806 pr_signals_handle();
6807
6808 flags = *((int *) c->argv[1]);
6809
6810 /* Skip any agent-provided SFTPHostKey directives, as well as any
6811 * "disabling key" directives.
6812 */
6813 if (flags != 0 ||
6814 strncmp(c->argv[0], "agent:", 6) == 0) {
6815 c = find_config_next(c, c->next, CONF_PARAM, "SFTPHostKey", FALSE);
6816 continue;
6817 }
6818
6819 k = pcalloc(s->pool, sizeof(struct sftp_pkey));
6820 k->pkeysz = PEM_BUFSIZE-1;
6821 k->server = s;
6822
6823 if (get_passphrase(k, c->argv[0]) < 0) {
6824 int xerrno = errno;
6825 const char *errstr;
6826
6827 errstr = sftp_crypto_get_errors();
6828
6829 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
6830 ": error reading passphrase for SFTPHostKey '%s': %s",
6831 (const char *) c->argv[0], errstr ? errstr : strerror(xerrno));
6832
6833 pr_log_pri(PR_LOG_ERR, MOD_SFTP_VERSION
6834 ": unable to use key in SFTPHostKey '%s', exiting",
6835 (const char *) c->argv[0]);
6836 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BAD_CONFIG,
6837 NULL);
6838 }
6839
6840 k->next = sftp_pkey_list;
6841 sftp_pkey_list = k;
6842 sftp_npkeys++;
6843
6844 c = find_config_next(c, c->next, CONF_PARAM, "SFTPHostKey", FALSE);
6845 }
6846 }
6847 }
6848
6849 /* Make sure that no valuable information can be inadvertently written
6850 * out to swap.
6851 */
6852 void sftp_keys_free(void) {
6853 scrub_pkeys();
6854
6855 sftp_keys_clear_dsa_hostkey();
6856 sftp_keys_clear_ecdsa_hostkey();
6857 sftp_keys_clear_ed25519_hostkey();
6858 sftp_keys_clear_ed448_hostkey();
6859 sftp_keys_clear_rsa_hostkey();
6860 }