xref: /freebsd/crypto/openssh/ssh-keysign.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /* $OpenBSD: ssh-keysign.c,v 1.55 2018/07/27 05:34:42 dtucker 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 #include <errno.h>
38 
39 #ifdef WITH_OPENSSL
40 #include <openssl/evp.h>
41 #include <openssl/rand.h>
42 #include <openssl/rsa.h>
43 #endif
44 
45 #include "xmalloc.h"
46 #include "log.h"
47 #include "sshkey.h"
48 #include "ssh.h"
49 #include "ssh2.h"
50 #include "misc.h"
51 #include "sshbuf.h"
52 #include "authfile.h"
53 #include "msg.h"
54 #include "canohost.h"
55 #include "pathnames.h"
56 #include "readconf.h"
57 #include "uidswap.h"
58 #include "sshkey.h"
59 #include "ssherr.h"
60 
61 struct ssh *active_state = NULL; /* XXX needed for linking */
62 
63 extern char *__progname;
64 
65 static int
66 valid_request(struct passwd *pw, char *host, struct sshkey **ret,
67     u_char *data, size_t datalen)
68 {
69 	struct sshbuf *b;
70 	struct sshkey *key = NULL;
71 	u_char type, *pkblob;
72 	char *p;
73 	size_t blen, len;
74 	char *pkalg, *luser;
75 	int r, pktype, fail;
76 
77 	if (ret != NULL)
78 		*ret = NULL;
79 	fail = 0;
80 
81 	if ((b = sshbuf_from(data, datalen)) == NULL)
82 		fatal("%s: sshbuf_from failed", __func__);
83 
84 	/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
85 	if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
86 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
87 	if (len != 20 && len != 32)
88 		fail++;
89 
90 	if ((r = sshbuf_get_u8(b, &type)) != 0)
91 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
92 	if (type != SSH2_MSG_USERAUTH_REQUEST)
93 		fail++;
94 
95 	/* server user */
96 	if ((r = sshbuf_skip_string(b)) != 0)
97 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
98 
99 	/* service */
100 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
101 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
102 	if (strcmp("ssh-connection", p) != 0)
103 		fail++;
104 	free(p);
105 
106 	/* method */
107 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
108 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
109 	if (strcmp("hostbased", p) != 0)
110 		fail++;
111 	free(p);
112 
113 	/* pubkey */
114 	if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
115 	    (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
116 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
117 
118 	pktype = sshkey_type_from_name(pkalg);
119 	if (pktype == KEY_UNSPEC)
120 		fail++;
121 	else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
122 		error("%s: bad key blob: %s", __func__, ssh_err(r));
123 		fail++;
124 	} else if (key->type != pktype)
125 		fail++;
126 	free(pkalg);
127 	free(pkblob);
128 
129 	/* client host name, handle trailing dot */
130 	if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
131 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
132 	debug2("%s: check expect chost %s got %s", __func__, host, p);
133 	if (strlen(host) != len - 1)
134 		fail++;
135 	else if (p[len - 1] != '.')
136 		fail++;
137 	else if (strncasecmp(host, p, len - 1) != 0)
138 		fail++;
139 	free(p);
140 
141 	/* local user */
142 	if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
143 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
144 
145 	if (strcmp(pw->pw_name, luser) != 0)
146 		fail++;
147 	free(luser);
148 
149 	/* end of message */
150 	if (sshbuf_len(b) != 0)
151 		fail++;
152 	sshbuf_free(b);
153 
154 	debug3("%s: fail %d", __func__, fail);
155 
156 	if (fail)
157 		sshkey_free(key);
158 	else if (ret != NULL)
159 		*ret = key;
160 
161 	return (fail ? -1 : 0);
162 }
163 
164 int
165 main(int argc, char **argv)
166 {
167 	struct sshbuf *b;
168 	Options options;
169 #define NUM_KEYTYPES 5
170 	struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
171 	struct passwd *pw;
172 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
173 	u_char *signature, *data, rver;
174 	char *host, *fp;
175 	size_t slen, dlen;
176 #ifdef WITH_OPENSSL
177 	u_int32_t rnd[256];
178 #endif
179 
180 	ssh_malloc_init();	/* must be called before any mallocs */
181 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
182 		fatal("%s: pledge: %s", __progname, strerror(errno));
183 
184 	/* Ensure that stdin and stdout are connected */
185 	if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
186 		exit(1);
187 	/* Leave /dev/null fd iff it is attached to stderr */
188 	if (fd > 2)
189 		close(fd);
190 
191 	i = 0;
192 	/* XXX This really needs to read sshd_config for the paths */
193 	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
194 	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
195 	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
196 	key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
197 	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
198 
199 	if ((pw = getpwuid(getuid())) == NULL)
200 		fatal("getpwuid failed");
201 	pw = pwcopy(pw);
202 
203 	permanently_set_uid(pw);
204 
205 	seed_rng();
206 
207 #ifdef DEBUG_SSH_KEYSIGN
208 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
209 #endif
210 
211 	/* verify that ssh-keysign is enabled by the admin */
212 	initialize_options(&options);
213 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", &options, 0);
214 	fill_default_options(&options);
215 	if (options.enable_ssh_keysign != 1)
216 		fatal("ssh-keysign not enabled in %s",
217 		    _PATH_HOST_CONFIG_FILE);
218 
219 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
220 		if (key_fd[i] != -1)
221 			found = 1;
222 	}
223 	if (found == 0)
224 		fatal("could not open any host key");
225 
226 #ifdef WITH_OPENSSL
227 	OpenSSL_add_all_algorithms();
228 	arc4random_buf(rnd, sizeof(rnd));
229 	RAND_seed(rnd, sizeof(rnd));
230 #endif
231 
232 	found = 0;
233 	for (i = 0; i < NUM_KEYTYPES; i++) {
234 		keys[i] = NULL;
235 		if (key_fd[i] == -1)
236 			continue;
237 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
238 		    NULL, &key, NULL);
239 		close(key_fd[i]);
240 		if (r != 0)
241 			debug("parse key %d: %s", i, ssh_err(r));
242 		else if (key != NULL) {
243 			keys[i] = key;
244 			found = 1;
245 		}
246 	}
247 	if (!found)
248 		fatal("no hostkey found");
249 
250 	if (pledge("stdio dns", NULL) != 0)
251 		fatal("%s: pledge: %s", __progname, strerror(errno));
252 
253 	if ((b = sshbuf_new()) == NULL)
254 		fatal("%s: sshbuf_new failed", __progname);
255 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
256 		fatal("ssh_msg_recv failed");
257 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
258 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
259 	if (rver != version)
260 		fatal("bad version: received %d, expected %d", rver, version);
261 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
262 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
263 	if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
264 		fatal("bad fd");
265 	if ((host = get_local_name(fd)) == NULL)
266 		fatal("cannot get local name for fd");
267 
268 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
269 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
270 	if (valid_request(pw, host, &key, data, dlen) < 0)
271 		fatal("not a valid request");
272 	free(host);
273 
274 	found = 0;
275 	for (i = 0; i < NUM_KEYTYPES; i++) {
276 		if (keys[i] != NULL &&
277 		    sshkey_equal_public(key, keys[i])) {
278 			found = 1;
279 			break;
280 		}
281 	}
282 	if (!found) {
283 		if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
284 		    SSH_FP_DEFAULT)) == NULL)
285 			fatal("%s: sshkey_fingerprint failed", __progname);
286 		fatal("no matching hostkey found for key %s %s",
287 		    sshkey_type(key), fp ? fp : "");
288 	}
289 
290 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, NULL, 0))
291 	    != 0)
292 		fatal("sshkey_sign failed: %s", ssh_err(r));
293 	free(data);
294 
295 	/* send reply */
296 	sshbuf_reset(b);
297 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
298 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
299 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
300 		fatal("ssh_msg_send failed");
301 
302 	return (0);
303 }
304