1/* $OpenBSD: ssh-keysign.c,v 1.36 2011/02/16 00:31:14 djm Exp $ */
2/*
3 * Copyright (c) 2002 Markus Friedl.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
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#ifdef __APPLE_CRYPTO__
39#include "ossl-evp.h"
40#include "ossl-rand.h"
41#include "ossl-rsa.h"
42#else
43#include <openssl/evp.h>
44#include <openssl/rand.h>
45#include <openssl/rsa.h>
46#endif
47
48#include "xmalloc.h"
49#include "log.h"
50#include "key.h"
51#include "ssh.h"
52#include "ssh2.h"
53#include "misc.h"
54#include "buffer.h"
55#include "authfile.h"
56#include "msg.h"
57#include "canohost.h"
58#include "pathnames.h"
59#include "readconf.h"
60#include "uidswap.h"
61
62/* XXX readconf.c needs these */
63uid_t original_real_uid;
64
65extern char *__progname;
66
67static int
68valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
69    u_int datalen)
70{
71	Buffer b;
72	Key *key = NULL;
73	u_char *pkblob;
74	u_int blen, len;
75	char *pkalg, *p;
76	int pktype, fail;
77
78	fail = 0;
79
80	buffer_init(&b);
81	buffer_append(&b, data, datalen);
82
83	/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
84	p = buffer_get_string(&b, &len);
85	if (len != 20 && len != 32)
86		fail++;
87	xfree(p);
88
89	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
90		fail++;
91
92	/* server user */
93	buffer_skip_string(&b);
94
95	/* service */
96	p = buffer_get_string(&b, NULL);
97	if (strcmp("ssh-connection", p) != 0)
98		fail++;
99	xfree(p);
100
101	/* method */
102	p = buffer_get_string(&b, NULL);
103	if (strcmp("hostbased", p) != 0)
104		fail++;
105	xfree(p);
106
107	/* pubkey */
108	pkalg = buffer_get_string(&b, NULL);
109	pkblob = buffer_get_string(&b, &blen);
110
111	pktype = key_type_from_name(pkalg);
112	if (pktype == KEY_UNSPEC)
113		fail++;
114	else if ((key = key_from_blob(pkblob, blen)) == NULL)
115		fail++;
116	else if (key->type != pktype)
117		fail++;
118	xfree(pkalg);
119	xfree(pkblob);
120
121	/* client host name, handle trailing dot */
122	p = buffer_get_string(&b, &len);
123	debug2("valid_request: check expect chost %s got %s", host, p);
124	if (strlen(host) != len - 1)
125		fail++;
126	else if (p[len - 1] != '.')
127		fail++;
128	else if (strncasecmp(host, p, len - 1) != 0)
129		fail++;
130	xfree(p);
131
132	/* local user */
133	p = buffer_get_string(&b, NULL);
134
135	if (strcmp(pw->pw_name, p) != 0)
136		fail++;
137	xfree(p);
138
139	/* end of message */
140	if (buffer_len(&b) != 0)
141		fail++;
142	buffer_free(&b);
143
144	debug3("valid_request: fail %d", fail);
145
146	if (fail && key != NULL)
147		key_free(key);
148	else
149		*ret = key;
150
151	return (fail ? -1 : 0);
152}
153
154int
155main(int argc, char **argv)
156{
157	Buffer b;
158	Options options;
159#define NUM_KEYTYPES 3
160	Key *keys[NUM_KEYTYPES], *key = NULL;
161	struct passwd *pw;
162	int key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
163	u_char *signature, *data;
164	char *host;
165	u_int slen, dlen;
166	u_int32_t rnd[256];
167
168	/* Ensure that stdin and stdout are connected */
169	if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
170		exit(1);
171	/* Leave /dev/null fd iff it is attached to stderr */
172	if (fd > 2)
173		close(fd);
174
175	i = 0;
176	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
177	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
178	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
179
180	original_real_uid = getuid();	/* XXX readconf.c needs this */
181	if ((pw = getpwuid(original_real_uid)) == NULL)
182		fatal("getpwuid failed");
183	pw = pwcopy(pw);
184
185	permanently_set_uid(pw);
186
187	seed_rng();
188	arc4random_stir();
189
190#ifdef DEBUG_SSH_KEYSIGN
191	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
192#endif
193
194	/* verify that ssh-keysign is enabled by the admin */
195	initialize_options(&options);
196	(void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options, 0);
197	fill_default_options(&options);
198	if (options.enable_ssh_keysign != 1)
199		fatal("ssh-keysign not enabled in %s",
200		    _PATH_HOST_CONFIG_FILE);
201
202	for (i = found = 0; i < NUM_KEYTYPES; i++) {
203		if (key_fd[i] != -1)
204			found = 1;
205	}
206	if (found == 0)
207		fatal("could not open any host key");
208
209	OpenSSL_add_all_algorithms();
210	for (i = 0; i < 256; i++)
211		rnd[i] = arc4random();
212	RAND_seed(rnd, sizeof(rnd));
213
214	found = 0;
215	for (i = 0; i < NUM_KEYTYPES; i++) {
216		keys[i] = NULL;
217		if (key_fd[i] == -1)
218			continue;
219		keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
220		    NULL, NULL);
221		close(key_fd[i]);
222		if (keys[i] != NULL)
223			found = 1;
224	}
225	if (!found)
226		fatal("no hostkey found");
227
228	buffer_init(&b);
229	if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
230		fatal("ssh_msg_recv failed");
231	if (buffer_get_char(&b) != version)
232		fatal("bad version");
233	fd = buffer_get_int(&b);
234	if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
235		fatal("bad fd");
236	if ((host = get_local_name(fd)) == NULL)
237		fatal("cannot get local name for fd");
238
239	data = buffer_get_string(&b, &dlen);
240	if (valid_request(pw, host, &key, data, dlen) < 0)
241		fatal("not a valid request");
242	xfree(host);
243
244	found = 0;
245	for (i = 0; i < NUM_KEYTYPES; i++) {
246		if (keys[i] != NULL &&
247		    key_equal_public(key, keys[i])) {
248			found = 1;
249			break;
250		}
251	}
252	if (!found)
253		fatal("no matching hostkey found");
254
255	if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
256		fatal("key_sign failed");
257	xfree(data);
258
259	/* send reply */
260	buffer_clear(&b);
261	buffer_put_string(&b, signature, slen);
262	if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
263		fatal("ssh_msg_send failed");
264
265	return (0);
266}
267