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