xref: /openbsd/usr.bin/ssh/ssh-keysign.c (revision 3bef86f7)
1 /* $OpenBSD: ssh-keysign.c,v 1.73 2024/01/11 01:51:16 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 	for (i = 0; i < NUM_KEYTYPES; i++)
194 		key_fd[i] = -1;
195 
196 	i = 0;
197 	/* XXX This really needs to read sshd_config for the paths */
198 #ifdef WITH_DSA
199 	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
200 #endif
201 	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
202 	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
203 	key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
204 	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
205 
206 	if ((pw = getpwuid(getuid())) == NULL)
207 		fatal("getpwuid failed");
208 	pw = pwcopy(pw);
209 
210 	permanently_set_uid(pw);
211 
212 #ifdef DEBUG_SSH_KEYSIGN
213 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
214 #endif
215 
216 	/* verify that ssh-keysign is enabled by the admin */
217 	initialize_options(&options);
218 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
219 	    &options, 0, NULL);
220 	(void)fill_default_options(&options);
221 	if (options.enable_ssh_keysign != 1)
222 		fatal("ssh-keysign not enabled in %s",
223 		    _PATH_HOST_CONFIG_FILE);
224 
225 	if (pledge("stdio dns", NULL) != 0)
226 		fatal("%s: pledge: %s", __progname, strerror(errno));
227 
228 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
229 		if (key_fd[i] != -1)
230 			found = 1;
231 	}
232 	if (found == 0)
233 		fatal("could not open any host key");
234 
235 #ifdef WITH_OPENSSL
236 	OpenSSL_add_all_algorithms();
237 #endif
238 
239 	found = 0;
240 	for (i = 0; i < NUM_KEYTYPES; i++) {
241 		keys[i] = NULL;
242 		if (key_fd[i] == -1)
243 			continue;
244 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
245 		    NULL, &key, NULL);
246 		close(key_fd[i]);
247 		if (r != 0)
248 			debug_r(r, "parse key %d", i);
249 		else if (key != NULL) {
250 			keys[i] = key;
251 			found = 1;
252 		}
253 	}
254 	if (!found)
255 		fatal("no hostkey found");
256 
257 	if ((b = sshbuf_new()) == NULL)
258 		fatal("%s: sshbuf_new failed", __progname);
259 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
260 		fatal("%s: ssh_msg_recv failed", __progname);
261 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
262 		fatal_r(r, "%s: buffer error", __progname);
263 	if (rver != version)
264 		fatal("%s: bad version: received %d, expected %d",
265 		    __progname, rver, version);
266 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
267 		fatal_r(r, "%s: buffer error", __progname);
268 	if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
269 		fatal("%s: bad fd = %d", __progname, fd);
270 	if ((host = get_local_name(fd)) == NULL)
271 		fatal("%s: cannot get local name for fd", __progname);
272 
273 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
274 		fatal_r(r, "%s: buffer error", __progname);
275 	if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
276 		fatal("%s: not a valid request", __progname);
277 	free(host);
278 
279 	found = 0;
280 	for (i = 0; i < NUM_KEYTYPES; i++) {
281 		if (keys[i] != NULL &&
282 		    sshkey_equal_public(key, keys[i])) {
283 			found = 1;
284 			break;
285 		}
286 	}
287 	if (!found) {
288 		if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
289 		    SSH_FP_DEFAULT)) == NULL)
290 			fatal("%s: sshkey_fingerprint failed", __progname);
291 		fatal("%s: no matching hostkey found for key %s %s", __progname,
292 		    sshkey_type(key), fp ? fp : "");
293 	}
294 
295 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
296 	    pkalg, NULL, NULL, 0)) != 0)
297 		fatal_r(r, "%s: sshkey_sign failed", __progname);
298 	free(data);
299 
300 	/* send reply */
301 	sshbuf_reset(b);
302 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
303 		fatal_r(r, "%s: buffer error", __progname);
304 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
305 		fatal("%s: ssh_msg_send failed", __progname);
306 
307 	return (0);
308 }
309