ssh-keysign.c 6.3 KB
Newer Older
1
/* $OpenBSD: ssh-keysign.c,v 1.39 2013/12/06 13:39:49 markus Exp $ */
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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.
 */
25

26
#include "includes.h"
27

28
#include <fcntl.h>
29
#ifdef HAVE_PATHS_H
30
#include <paths.h>
31
#endif
32
#include <pwd.h>
33
#include <stdarg.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <unistd.h>
37 38

#include <openssl/evp.h>
39 40
#include <openssl/rand.h>
#include <openssl/rsa.h>
41

42
#include "xmalloc.h"
43 44
#include "log.h"
#include "key.h"
45
#include "ssh.h"
46 47 48 49 50 51 52
#include "ssh2.h"
#include "misc.h"
#include "buffer.h"
#include "authfile.h"
#include "msg.h"
#include "canohost.h"
#include "pathnames.h"
53
#include "readconf.h"
54
#include "uidswap.h"
55

56 57
/* XXX readconf.c needs these */
uid_t original_real_uid;
58

59 60
extern char *__progname;

61 62 63 64 65
static int
valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
    u_int datalen)
{
	Buffer b;
66
	Key *key = NULL;
67 68 69 70 71 72 73 74 75
	u_char *pkblob;
	u_int blen, len;
	char *pkalg, *p;
	int pktype, fail;

	fail = 0;

	buffer_init(&b);
	buffer_append(&b, data, datalen);
76

77
	/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
78
	p = buffer_get_string(&b, &len);
79
	if (len != 20 && len != 32)
80
		fail++;
81
	free(p);
82

83 84 85 86 87 88 89 90 91 92
	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++;
93
	free(p);
94 95 96 97 98

	/* method */
	p = buffer_get_string(&b, NULL);
	if (strcmp("hostbased", p) != 0)
		fail++;
99
	free(p);
100 101 102 103 104 105 106 107 108 109 110 111

	/* 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++;
112 113
	free(pkalg);
	free(pkblob);
114 115 116 117 118 119 120

	/* 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] != '.')
121
		fail++;
122
	else if (strncasecmp(host, p, len - 1) != 0)
123
		fail++;
124
	free(p);
125 126 127 128 129 130

	/* local user */
	p = buffer_get_string(&b, NULL);

	if (strcmp(pw->pw_name, p) != 0)
		fail++;
131
	free(p);
132 133 134 135

	/* end of message */
	if (buffer_len(&b) != 0)
		fail++;
136
	buffer_free(&b);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

	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;
152
	Options options;
153
#define NUM_KEYTYPES 4
154
	Key *keys[NUM_KEYTYPES], *key = NULL;
155
	struct passwd *pw;
156
	int key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
157 158 159
	u_char *signature, *data;
	char *host;
	u_int slen, dlen;
160
	u_int32_t rnd[256];
161

162 163 164 165 166 167 168
	/* 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);

169 170 171
	i = 0;
	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
172
	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
173
	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
174

175 176
	original_real_uid = getuid();	/* XXX readconf.c needs this */
	if ((pw = getpwuid(original_real_uid)) == NULL)
177 178 179 180
		fatal("getpwuid failed");
	pw = pwcopy(pw);

	permanently_set_uid(pw);
181

Ben Lindstrom's avatar
Ben Lindstrom committed
182
	seed_rng();
183

184 185
#ifdef DEBUG_SSH_KEYSIGN
	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
186
#endif
187

188 189
	/* verify that ssh-keysign is enabled by the admin */
	initialize_options(&options);
190
	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", &options, 0);
191
	fill_default_options(&options);
192 193
	if (options.enable_ssh_keysign != 1)
		fatal("ssh-keysign not enabled in %s",
194 195
		    _PATH_HOST_CONFIG_FILE);

196 197 198 199 200
	for (i = found = 0; i < NUM_KEYTYPES; i++) {
		if (key_fd[i] != -1)
			found = 1;
	}
	if (found == 0)
201 202
		fatal("could not open any host key");

203
	OpenSSL_add_all_algorithms();
204 205 206
	for (i = 0; i < 256; i++)
		rnd[i] = arc4random();
	RAND_seed(rnd, sizeof(rnd));
207 208

	found = 0;
209
	for (i = 0; i < NUM_KEYTYPES; i++) {
210 211 212 213 214 215 216 217 218 219 220 221 222
		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);
223 224
	if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
		fatal("ssh_msg_recv failed");
225 226 227 228 229 230
	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)
231
		fatal("cannot get local name for fd");
232

233 234 235
	data = buffer_get_string(&b, &dlen);
	if (valid_request(pw, host, &key, data, dlen) < 0)
		fatal("not a valid request");
236
	free(host);
237 238

	found = 0;
239
	for (i = 0; i < NUM_KEYTYPES; i++) {
240
		if (keys[i] != NULL &&
241
		    key_equal_public(key, keys[i])) {
242 243 244 245 246 247 248 249 250
			found = 1;
			break;
		}
	}
	if (!found)
		fatal("no matching hostkey found");

	if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
		fatal("key_sign failed");
251
	free(data);
252

253 254 255
	/* send reply */
	buffer_clear(&b);
	buffer_put_string(&b, signature, slen);
256 257
	if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
		fatal("ssh_msg_send failed");
258 259 260

	return (0);
}