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