2011-02-17 22:18:45 +00:00
|
|
|
/* $OpenBSD: ssh-keysign.c,v 1.36 2011/02/16 00:31:14 djm 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>
|
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>
|
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>
|
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"
|
|
|
|
#include "key.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"
|
|
|
|
#include "buffer.h"
|
|
|
|
#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"
|
2002-07-04 00:19:40 +00:00
|
|
|
|
2003-05-18 10:50:30 +00:00
|
|
|
/* XXX readconf.c needs these */
|
|
|
|
uid_t original_real_uid;
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2002-06-07 14:37:00 +00:00
|
|
|
extern char *__progname;
|
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
static int
|
|
|
|
valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
|
|
|
|
u_int datalen)
|
|
|
|
{
|
|
|
|
Buffer b;
|
2003-04-09 10:50:26 +00:00
|
|
|
Key *key = NULL;
|
2002-06-06 19:57:33 +00:00
|
|
|
u_char *pkblob;
|
|
|
|
u_int blen, len;
|
|
|
|
char *pkalg, *p;
|
|
|
|
int pktype, fail;
|
|
|
|
|
|
|
|
fail = 0;
|
|
|
|
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_append(&b, data, datalen);
|
2002-06-21 00:41:51 +00:00
|
|
|
|
2006-04-23 02:05:32 +00:00
|
|
|
/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
|
2002-06-09 20:01:48 +00:00
|
|
|
p = buffer_get_string(&b, &len);
|
2006-04-23 02:05:32 +00:00
|
|
|
if (len != 20 && len != 32)
|
2002-06-09 20:01:48 +00:00
|
|
|
fail++;
|
|
|
|
xfree(p);
|
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
|
|
|
|
fail++;
|
|
|
|
|
|
|
|
/* server user */
|
|
|
|
buffer_skip_string(&b);
|
|
|
|
|
|
|
|
/* service */
|
|
|
|
p = buffer_get_string(&b, NULL);
|
|
|
|
if (strcmp("ssh-connection", p) != 0)
|
|
|
|
fail++;
|
|
|
|
xfree(p);
|
|
|
|
|
|
|
|
/* method */
|
|
|
|
p = buffer_get_string(&b, NULL);
|
|
|
|
if (strcmp("hostbased", p) != 0)
|
|
|
|
fail++;
|
|
|
|
xfree(p);
|
|
|
|
|
|
|
|
/* pubkey */
|
|
|
|
pkalg = buffer_get_string(&b, NULL);
|
|
|
|
pkblob = buffer_get_string(&b, &blen);
|
|
|
|
|
|
|
|
pktype = key_type_from_name(pkalg);
|
|
|
|
if (pktype == KEY_UNSPEC)
|
|
|
|
fail++;
|
|
|
|
else if ((key = key_from_blob(pkblob, blen)) == NULL)
|
|
|
|
fail++;
|
|
|
|
else if (key->type != pktype)
|
|
|
|
fail++;
|
|
|
|
xfree(pkalg);
|
|
|
|
xfree(pkblob);
|
|
|
|
|
|
|
|
/* client host name, handle trailing dot */
|
|
|
|
p = buffer_get_string(&b, &len);
|
|
|
|
debug2("valid_request: check expect chost %s got %s", host, p);
|
|
|
|
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++;
|
2002-06-06 19:57:33 +00:00
|
|
|
xfree(p);
|
|
|
|
|
|
|
|
/* local user */
|
|
|
|
p = buffer_get_string(&b, NULL);
|
|
|
|
|
|
|
|
if (strcmp(pw->pw_name, p) != 0)
|
|
|
|
fail++;
|
|
|
|
xfree(p);
|
|
|
|
|
|
|
|
/* end of message */
|
|
|
|
if (buffer_len(&b) != 0)
|
|
|
|
fail++;
|
2004-01-21 00:02:50 +00:00
|
|
|
buffer_free(&b);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
debug3("valid_request: fail %d", fail);
|
|
|
|
|
|
|
|
if (fail && key != NULL)
|
|
|
|
key_free(key);
|
|
|
|
else
|
|
|
|
*ret = key;
|
|
|
|
|
|
|
|
return (fail ? -1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Buffer b;
|
2002-07-04 00:19:40 +00:00
|
|
|
Options options;
|
2011-02-17 22:18:45 +00:00
|
|
|
#define NUM_KEYTYPES 3
|
|
|
|
Key *keys[NUM_KEYTYPES], *key = NULL;
|
2002-06-06 19:57:33 +00:00
|
|
|
struct passwd *pw;
|
2011-02-17 22:18:45 +00:00
|
|
|
int key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
|
2002-06-06 19:57:33 +00:00
|
|
|
u_char *signature, *data;
|
|
|
|
char *host;
|
|
|
|
u_int slen, dlen;
|
2002-07-04 00:17:33 +00:00
|
|
|
u_int32_t rnd[256];
|
2002-06-06 19:57:33 +00:00
|
|
|
|
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;
|
|
|
|
key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
|
|
|
|
key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
|
|
|
|
key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
|
2002-06-06 19:57:33 +00:00
|
|
|
|
2004-08-29 06:32:59 +00:00
|
|
|
original_real_uid = getuid(); /* XXX readconf.c needs this */
|
|
|
|
if ((pw = getpwuid(original_real_uid)) == 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();
|
|
|
|
arc4random_stir();
|
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);
|
2004-04-20 10:11:57 +00:00
|
|
|
(void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options, 0);
|
2002-07-04 00:19:40 +00:00
|
|
|
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);
|
|
|
|
|
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");
|
|
|
|
|
2010-09-10 01:12:09 +00:00
|
|
|
OpenSSL_add_all_algorithms();
|
2002-07-04 00:17:33 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
rnd[i] = arc4random();
|
|
|
|
RAND_seed(rnd, sizeof(rnd));
|
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
|
|
|
keys[i] = NULL;
|
|
|
|
if (key_fd[i] == -1)
|
|
|
|
continue;
|
|
|
|
keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
|
|
|
|
NULL, NULL);
|
|
|
|
close(key_fd[i]);
|
|
|
|
if (keys[i] != NULL)
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
fatal("no hostkey found");
|
|
|
|
|
|
|
|
buffer_init(&b);
|
2002-10-04 01:10:04 +00:00
|
|
|
if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
|
|
|
|
fatal("ssh_msg_recv failed");
|
2002-06-06 19:57:33 +00:00
|
|
|
if (buffer_get_char(&b) != version)
|
|
|
|
fatal("bad version");
|
|
|
|
fd = buffer_get_int(&b);
|
|
|
|
if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
|
|
|
|
fatal("bad fd");
|
|
|
|
if ((host = get_local_name(fd)) == NULL)
|
2010-01-13 11:43:33 +00:00
|
|
|
fatal("cannot get local name for fd");
|
2002-06-21 00:41:51 +00:00
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
data = buffer_get_string(&b, &dlen);
|
|
|
|
if (valid_request(pw, host, &key, data, dlen) < 0)
|
|
|
|
fatal("not a valid request");
|
|
|
|
xfree(host);
|
|
|
|
|
|
|
|
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 &&
|
2010-08-05 03:04:50 +00:00
|
|
|
key_equal_public(key, keys[i])) {
|
2002-06-06 19:57:33 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
fatal("no matching hostkey found");
|
|
|
|
|
|
|
|
if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
|
|
|
|
fatal("key_sign failed");
|
2002-06-27 00:25:07 +00:00
|
|
|
xfree(data);
|
2002-06-21 00:41:51 +00:00
|
|
|
|
2002-06-06 19:57:33 +00:00
|
|
|
/* send reply */
|
|
|
|
buffer_clear(&b);
|
|
|
|
buffer_put_string(&b, signature, slen);
|
2003-11-17 10:20:47 +00:00
|
|
|
if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
|
|
|
|
fatal("ssh_msg_send failed");
|
2002-06-06 19:57:33 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|