xref: /openbsd-src/usr.bin/ssh/ssh-keysign.c (revision 5c389b79544373bccfce668b646e62e7ba9802a3)
1 /* $OpenBSD: ssh-keysign.c,v 1.71 2022/08/01 11:09:26 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 		if (ret != NULL) {
155 			*ret = key;
156 			key = NULL;
157 		}
158 		if (pkalgp != NULL) {
159 			*pkalgp = pkalg;
160 			pkalg = NULL;
161 		}
162 	}
163 	sshkey_free(key);
164 	free(pkalg);
165 	free(pkblob);
166 
167 	return (fail ? -1 : 0);
168 }
169 
170 int
171 main(int argc, char **argv)
172 {
173 	struct sshbuf *b;
174 	Options options;
175 #define NUM_KEYTYPES 5
176 	struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
177 	struct passwd *pw;
178 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
179 	u_char *signature, *data, rver;
180 	char *host, *fp, *pkalg;
181 	size_t slen, dlen;
182 
183 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
184 		fatal("%s: pledge: %s", __progname, strerror(errno));
185 
186 	/* Ensure that stdin and stdout are connected */
187 	if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
188 		exit(1);
189 	/* Leave /dev/null fd iff it is attached to stderr */
190 	if (fd > 2)
191 		close(fd);
192 
193 	i = 0;
194 	/* XXX This really needs to read sshd_config for the paths */
195 	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
196 	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
197 	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
198 	key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
199 	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
200 
201 	if ((pw = getpwuid(getuid())) == NULL)
202 		fatal("getpwuid failed");
203 	pw = pwcopy(pw);
204 
205 	permanently_set_uid(pw);
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, "", "",
214 	    &options, 0, NULL);
215 	(void)fill_default_options(&options);
216 	if (options.enable_ssh_keysign != 1)
217 		fatal("ssh-keysign not enabled in %s",
218 		    _PATH_HOST_CONFIG_FILE);
219 
220 	if (pledge("stdio dns", NULL) != 0)
221 		fatal("%s: pledge: %s", __progname, strerror(errno));
222 
223 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
224 		if (key_fd[i] != -1)
225 			found = 1;
226 	}
227 	if (found == 0)
228 		fatal("could not open any host key");
229 
230 #ifdef WITH_OPENSSL
231 	OpenSSL_add_all_algorithms();
232 #endif
233 
234 	found = 0;
235 	for (i = 0; i < NUM_KEYTYPES; i++) {
236 		keys[i] = NULL;
237 		if (key_fd[i] == -1)
238 			continue;
239 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
240 		    NULL, &key, NULL);
241 		close(key_fd[i]);
242 		if (r != 0)
243 			debug_r(r, "parse key %d", i);
244 		else if (key != NULL) {
245 			keys[i] = key;
246 			found = 1;
247 		}
248 	}
249 	if (!found)
250 		fatal("no hostkey found");
251 
252 	if ((b = sshbuf_new()) == NULL)
253 		fatal("%s: sshbuf_new failed", __progname);
254 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
255 		fatal("%s: ssh_msg_recv failed", __progname);
256 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
257 		fatal_r(r, "%s: buffer error", __progname);
258 	if (rver != version)
259 		fatal("%s: bad version: received %d, expected %d",
260 		    __progname, rver, version);
261 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
262 		fatal_r(r, "%s: buffer error", __progname);
263 	if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
264 		fatal("%s: bad fd = %d", __progname, fd);
265 	if ((host = get_local_name(fd)) == NULL)
266 		fatal("%s: cannot get local name for fd", __progname);
267 
268 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
269 		fatal_r(r, "%s: buffer error", __progname);
270 	if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
271 		fatal("%s: not a valid request", __progname);
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("%s: no matching hostkey found for key %s %s", __progname,
287 		    sshkey_type(key), fp ? fp : "");
288 	}
289 
290 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
291 	    pkalg, NULL, NULL, 0)) != 0)
292 		fatal_r(r, "%s: sshkey_sign failed", __progname);
293 	free(data);
294 
295 	/* send reply */
296 	sshbuf_reset(b);
297 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
298 		fatal_r(r, "%s: buffer error", __progname);
299 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
300 		fatal("%s: ssh_msg_send failed", __progname);
301 
302 	return (0);
303 }
304