2021-11-13 17:26:13 +00:00
|
|
|
/* $OpenBSD: ssh-keysign.c,v 1.69 2021/11/13 17:26:13 deraadt Exp $ */
|
2002-06-06 19:57:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2006-08-05 02:39:39 +00:00
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
#include "includes.h"
|
2006-03-15 00:16:59 +00:00
|
|
|
|
2006-07-10 11:13:46 +00:00
|
|
|
#include <fcntl.h>
|
2006-03-15 03:42:54 +00:00
|
|
|
#ifdef HAVE_PATHS_H
|
2006-03-15 00:16:59 +00:00
|
|
|
#include <paths.h>
|
2006-03-15 03:42:54 +00:00
|
|
|
#endif
|
2006-07-10 10:53:08 +00:00
|
|
|
#include <pwd.h>
|
2006-09-01 05:38:36 +00:00
|
|
|
#include <stdarg.h>
|
2019-07-23 12:25:44 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2006-07-24 04:01:23 +00:00
|
|
|
#include <unistd.h>
|
2015-11-29 22:18:37 +00:00
|
|
|
#include <errno.h>
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2015-01-14 15:21:31 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
2002-06-06 19:57:33 +00:00
|
|
|
#include <openssl/evp.h>
|
2002-07-04 00:17:33 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/rsa.h>
|
2018-10-22 09:05:18 +00:00
|
|
|
#include "openbsd-compat/openssl-compat.h"
|
2015-01-14 15:21:31 +00:00
|
|
|
#endif
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "xmalloc.h"
|
2002-06-06 19:57:33 +00:00
|
|
|
#include "log.h"
|
2015-01-15 09:40:00 +00:00
|
|
|
#include "sshkey.h"
|
2002-07-04 00:19:40 +00:00
|
|
|
#include "ssh.h"
|
2002-06-06 19:57:33 +00:00
|
|
|
#include "ssh2.h"
|
|
|
|
#include "misc.h"
|
2015-01-15 09:40:00 +00:00
|
|
|
#include "sshbuf.h"
|
2002-06-06 19:57:33 +00:00
|
|
|
#include "authfile.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "canohost.h"
|
|
|
|
#include "pathnames.h"
|
2002-07-04 00:19:40 +00:00
|
|
|
#include "readconf.h"
|
2004-08-15 07:23:34 +00:00
|
|
|
#include "uidswap.h"
|
2015-01-08 10:14:08 +00:00
|
|
|
#include "ssherr.h"
|
2002-07-04 00:19:40 +00:00
|
|
|
|
2015-11-29 22:18:37 +00:00
|
|
|
extern char *__progname;
|
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
static int
|
2015-01-15 09:40:00 +00:00
|
|
|
valid_request(struct passwd *pw, char *host, struct sshkey **ret,
|
|
|
|
u_char *data, size_t datalen)
|
2002-06-06 19:57:33 +00:00
|
|
|
{
|
2015-01-15 09:40:00 +00:00
|
|
|
struct sshbuf *b;
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
u_char type, *pkblob;
|
|
|
|
char *p;
|
|
|
|
size_t blen, len;
|
|
|
|
char *pkalg, *luser;
|
|
|
|
int r, pktype, fail;
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2015-01-08 10:14:08 +00:00
|
|
|
if (ret != NULL)
|
|
|
|
*ret = NULL;
|
2002-06-06 19:57:33 +00:00
|
|
|
fail = 0;
|
|
|
|
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((b = sshbuf_from(data, datalen)) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshbuf_from failed");
|
2002-06-21 00:41:51 +00:00
|
|
|
|
2021-11-10 06:25:08 +00:00
|
|
|
/* session id */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse session ID");
|
2021-11-10 06:25:08 +00:00
|
|
|
if (len != 20 && /* SHA1 */
|
|
|
|
len != 32 && /* SHA256 */
|
|
|
|
len != 48 && /* SHA384 */
|
|
|
|
len != 64) /* SHA512 */
|
2002-06-09 20:01:48 +00:00
|
|
|
fail++;
|
|
|
|
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_u8(b, &type)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse type");
|
2015-01-15 09:40:00 +00:00
|
|
|
if (type != SSH2_MSG_USERAUTH_REQUEST)
|
2002-06-06 19:57:33 +00:00
|
|
|
fail++;
|
|
|
|
|
|
|
|
/* server user */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_skip_string(b)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse user");
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
/* service */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse service");
|
2002-06-06 19:57:33 +00:00
|
|
|
if (strcmp("ssh-connection", p) != 0)
|
|
|
|
fail++;
|
2013-06-01 21:31:17 +00:00
|
|
|
free(p);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
/* method */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse method");
|
2002-06-06 19:57:33 +00:00
|
|
|
if (strcmp("hostbased", p) != 0)
|
|
|
|
fail++;
|
2013-06-01 21:31:17 +00:00
|
|
|
free(p);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
/* pubkey */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse pk");
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2015-01-15 09:40:00 +00:00
|
|
|
pktype = sshkey_type_from_name(pkalg);
|
2002-06-06 19:57:33 +00:00
|
|
|
if (pktype == KEY_UNSPEC)
|
|
|
|
fail++;
|
2015-01-15 09:40:00 +00:00
|
|
|
else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "decode key");
|
2002-06-06 19:57:33 +00:00
|
|
|
fail++;
|
2015-01-15 09:40:00 +00:00
|
|
|
} else if (key->type != pktype)
|
2002-06-06 19:57:33 +00:00
|
|
|
fail++;
|
2013-06-01 21:31:17 +00:00
|
|
|
free(pkalg);
|
|
|
|
free(pkblob);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
/* client host name, handle trailing dot */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse hostname");
|
|
|
|
debug2_f("check expect chost %s got %s", host, p);
|
2002-06-06 19:57:33 +00:00
|
|
|
if (strlen(host) != len - 1)
|
|
|
|
fail++;
|
|
|
|
else if (p[len - 1] != '.')
|
2002-06-21 00:41:51 +00:00
|
|
|
fail++;
|
2002-06-06 19:57:33 +00:00
|
|
|
else if (strncasecmp(host, p, len - 1) != 0)
|
2002-06-21 00:41:51 +00:00
|
|
|
fail++;
|
2013-06-01 21:31:17 +00:00
|
|
|
free(p);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
/* local user */
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse luser");
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2015-01-15 09:40:00 +00:00
|
|
|
if (strcmp(pw->pw_name, luser) != 0)
|
2002-06-06 19:57:33 +00:00
|
|
|
fail++;
|
2015-01-15 09:40:00 +00:00
|
|
|
free(luser);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
/* end of message */
|
2015-01-15 09:40:00 +00:00
|
|
|
if (sshbuf_len(b) != 0)
|
2002-06-06 19:57:33 +00:00
|
|
|
fail++;
|
2015-01-15 09:40:00 +00:00
|
|
|
sshbuf_free(b);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2020-10-18 11:32:01 +00:00
|
|
|
debug3_f("fail %d", fail);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2018-02-07 22:52:45 +00:00
|
|
|
if (fail)
|
2015-01-15 09:40:00 +00:00
|
|
|
sshkey_free(key);
|
2015-03-24 20:09:11 +00:00
|
|
|
else if (ret != NULL)
|
2002-06-06 19:57:33 +00:00
|
|
|
*ret = key;
|
|
|
|
|
|
|
|
return (fail ? -1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2015-01-15 09:40:00 +00:00
|
|
|
struct sshbuf *b;
|
2002-07-04 00:19:40 +00:00
|
|
|
Options options;
|
2018-02-23 15:58:37 +00:00
|
|
|
#define NUM_KEYTYPES 5
|
2015-01-15 09:40:00 +00:00
|
|
|
struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
|
2002-06-06 19:57:33 +00:00
|
|
|
struct passwd *pw;
|
2015-01-08 10:14:08 +00:00
|
|
|
int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
|
2015-01-15 09:40:00 +00:00
|
|
|
u_char *signature, *data, rver;
|
2014-04-20 03:23:21 +00:00
|
|
|
char *host, *fp;
|
2015-01-15 09:40:00 +00:00
|
|
|
size_t slen, dlen;
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2015-11-29 22:18:37 +00:00
|
|
|
if (pledge("stdio rpath getpw dns id", NULL) != 0)
|
|
|
|
fatal("%s: pledge: %s", __progname, strerror(errno));
|
|
|
|
|
2005-10-03 08:11:24 +00:00
|
|
|
/* Ensure that stdin and stdout are connected */
|
|
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
|
|
|
|
exit(1);
|
|
|
|
/* Leave /dev/null fd iff it is attached to stderr */
|
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
|
|
|
|
2011-02-17 22:18:45 +00:00
|
|
|
i = 0;
|
2015-07-03 03:56:25 +00:00
|
|
|
/* XXX This really needs to read sshd_config for the paths */
|
2011-02-17 22:18:45 +00:00
|
|
|
key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
|
|
|
|
key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
|
2013-12-07 00:24:01 +00:00
|
|
|
key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
|
2018-02-23 15:58:37 +00:00
|
|
|
key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
|
2011-02-17 22:18:45 +00:00
|
|
|
key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2018-07-27 05:34:42 +00:00
|
|
|
if ((pw = getpwuid(getuid())) == NULL)
|
2004-08-15 07:23:34 +00:00
|
|
|
fatal("getpwuid failed");
|
|
|
|
pw = pwcopy(pw);
|
|
|
|
|
|
|
|
permanently_set_uid(pw);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2002-06-09 19:41:48 +00:00
|
|
|
seed_rng();
|
2002-06-07 03:11:38 +00:00
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
#ifdef DEBUG_SSH_KEYSIGN
|
|
|
|
log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
|
2002-06-21 00:41:51 +00:00
|
|
|
#endif
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2002-07-04 00:19:40 +00:00
|
|
|
/* verify that ssh-keysign is enabled by the admin */
|
|
|
|
initialize_options(&options);
|
2018-11-23 05:08:07 +00:00
|
|
|
(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
|
|
|
|
&options, 0, NULL);
|
2020-12-17 23:10:27 +00:00
|
|
|
(void)fill_default_options(&options);
|
2002-11-09 15:52:31 +00:00
|
|
|
if (options.enable_ssh_keysign != 1)
|
|
|
|
fatal("ssh-keysign not enabled in %s",
|
2002-07-04 00:19:40 +00:00
|
|
|
_PATH_HOST_CONFIG_FILE);
|
|
|
|
|
2021-11-13 17:26:13 +00:00
|
|
|
if (pledge("stdio dns", NULL) != 0)
|
|
|
|
fatal("%s: pledge: %s", __progname, strerror(errno));
|
|
|
|
|
2011-02-17 22:18:45 +00:00
|
|
|
for (i = found = 0; i < NUM_KEYTYPES; i++) {
|
|
|
|
if (key_fd[i] != -1)
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
if (found == 0)
|
2002-06-06 19:57:33 +00:00
|
|
|
fatal("could not open any host key");
|
|
|
|
|
|
|
|
found = 0;
|
2011-02-17 22:18:45 +00:00
|
|
|
for (i = 0; i < NUM_KEYTYPES; i++) {
|
2002-06-06 19:57:33 +00:00
|
|
|
keys[i] = NULL;
|
|
|
|
if (key_fd[i] == -1)
|
|
|
|
continue;
|
2015-01-08 10:14:08 +00:00
|
|
|
r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
|
|
|
|
NULL, &key, NULL);
|
2002-06-06 19:57:33 +00:00
|
|
|
close(key_fd[i]);
|
2015-01-08 10:14:08 +00:00
|
|
|
if (r != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_r(r, "parse key %d", i);
|
2015-01-08 10:14:08 +00:00
|
|
|
else if (key != NULL) {
|
|
|
|
keys[i] = key;
|
2002-06-06 19:57:33 +00:00
|
|
|
found = 1;
|
2015-01-08 10:14:08 +00:00
|
|
|
}
|
2002-06-06 19:57:33 +00:00
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
fatal("no hostkey found");
|
|
|
|
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((b = sshbuf_new()) == NULL)
|
2015-11-29 22:18:37 +00:00
|
|
|
fatal("%s: sshbuf_new failed", __progname);
|
2015-01-15 09:40:00 +00:00
|
|
|
if (ssh_msg_recv(STDIN_FILENO, b) < 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: ssh_msg_recv failed", __progname);
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_u8(b, &rver)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_r(r, "%s: buffer error", __progname);
|
2015-01-15 09:40:00 +00:00
|
|
|
if (rver != version)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: bad version: received %d, expected %d",
|
|
|
|
__progname, rver, version);
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_r(r, "%s: buffer error", __progname);
|
2015-01-15 09:40:00 +00:00
|
|
|
if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: bad fd = %d", __progname, fd);
|
2002-06-06 19:57:33 +00:00
|
|
|
if ((host = get_local_name(fd)) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: cannot get local name for fd", __progname);
|
2002-06-21 00:41:51 +00:00
|
|
|
|
2015-01-15 09:40:00 +00:00
|
|
|
if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_r(r, "%s: buffer error", __progname);
|
2002-06-06 19:57:33 +00:00
|
|
|
if (valid_request(pw, host, &key, data, dlen) < 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: not a valid request", __progname);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(host);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
found = 0;
|
2011-02-17 22:18:45 +00:00
|
|
|
for (i = 0; i < NUM_KEYTYPES; i++) {
|
2002-06-06 19:57:33 +00:00
|
|
|
if (keys[i] != NULL &&
|
2015-01-15 09:40:00 +00:00
|
|
|
sshkey_equal_public(key, keys[i])) {
|
2002-06-06 19:57:33 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 03:23:21 +00:00
|
|
|
if (!found) {
|
2015-01-28 22:36:00 +00:00
|
|
|
if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
|
|
|
|
SSH_FP_DEFAULT)) == NULL)
|
2015-11-29 22:18:37 +00:00
|
|
|
fatal("%s: sshkey_fingerprint failed", __progname);
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: no matching hostkey found for key %s %s", __progname,
|
2015-01-15 09:40:00 +00:00
|
|
|
sshkey_type(key), fp ? fp : "");
|
2014-04-20 03:23:21 +00:00
|
|
|
}
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2019-10-31 21:23:19 +00:00
|
|
|
if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
|
2020-08-27 01:06:18 +00:00
|
|
|
NULL, NULL, NULL, 0)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_r(r, "%s: sshkey_sign failed", __progname);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(data);
|
2002-06-21 00:41:51 +00:00
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
/* send reply */
|
2015-01-15 09:40:00 +00:00
|
|
|
sshbuf_reset(b);
|
|
|
|
if ((r = sshbuf_put_string(b, signature, slen)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_r(r, "%s: buffer error", __progname);
|
2015-01-15 09:40:00 +00:00
|
|
|
if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal("%s: ssh_msg_send failed", __progname);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|