xref: /freebsd/crypto/openssh/ssh-keysign.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
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 "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 <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39 
40 #ifdef WITH_OPENSSL
41 #include <openssl/evp.h>
42 #include <openssl/rand.h>
43 #include <openssl/rsa.h>
44 #include "openbsd-compat/openssl-compat.h"
45 #endif
46 
47 #include "xmalloc.h"
48 #include "log.h"
49 #include "sshkey.h"
50 #include "ssh.h"
51 #include "ssh2.h"
52 #include "misc.h"
53 #include "sshbuf.h"
54 #include "authfile.h"
55 #include "msg.h"
56 #include "canohost.h"
57 #include "pathnames.h"
58 #include "readconf.h"
59 #include "uidswap.h"
60 #include "ssherr.h"
61 
62 extern char *__progname;
63 
64 static int
65 valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
66     u_char *data, size_t datalen)
67 {
68 	struct sshbuf *b;
69 	struct sshkey *key = NULL;
70 	u_char type, *pkblob;
71 	char *p;
72 	size_t blen, len;
73 	char *pkalg, *luser;
74 	int r, pktype, fail;
75 
76 	if (ret != NULL)
77 		*ret = NULL;
78 	if (pkalgp != NULL)
79 		*pkalgp = NULL;
80 	fail = 0;
81 
82 	if ((b = sshbuf_from(data, datalen)) == NULL)
83 		fatal_f("sshbuf_from failed");
84 
85 	/* session id */
86 	if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
87 		fatal_fr(r, "parse session ID");
88 	if (len != 20 && /* SHA1 */
89 	    len != 32 && /* SHA256 */
90 	    len != 48 && /* SHA384 */
91 	    len != 64)   /* SHA512 */
92 		fail++;
93 
94 	if ((r = sshbuf_get_u8(b, &type)) != 0)
95 		fatal_fr(r, "parse type");
96 	if (type != SSH2_MSG_USERAUTH_REQUEST)
97 		fail++;
98 
99 	/* server user */
100 	if ((r = sshbuf_skip_string(b)) != 0)
101 		fatal_fr(r, "parse user");
102 
103 	/* service */
104 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
105 		fatal_fr(r, "parse service");
106 	if (strcmp("ssh-connection", p) != 0)
107 		fail++;
108 	free(p);
109 
110 	/* method */
111 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
112 		fatal_fr(r, "parse method");
113 	if (strcmp("hostbased", p) != 0)
114 		fail++;
115 	free(p);
116 
117 	/* pubkey */
118 	if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
119 	    (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
120 		fatal_fr(r, "parse pk");
121 
122 	pktype = sshkey_type_from_name(pkalg);
123 	if (pktype == KEY_UNSPEC)
124 		fail++;
125 	else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
126 		error_fr(r, "decode key");
127 		fail++;
128 	} else if (key->type != pktype)
129 		fail++;
130 
131 	/* client host name, handle trailing dot */
132 	if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
133 		fatal_fr(r, "parse hostname");
134 	debug2_f("check expect chost %s got %s", host, p);
135 	if (strlen(host) != len - 1)
136 		fail++;
137 	else if (p[len - 1] != '.')
138 		fail++;
139 	else if (strncasecmp(host, p, len - 1) != 0)
140 		fail++;
141 	free(p);
142 
143 	/* local user */
144 	if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
145 		fatal_fr(r, "parse luser");
146 
147 	if (strcmp(pw->pw_name, luser) != 0)
148 		fail++;
149 	free(luser);
150 
151 	/* end of message */
152 	if (sshbuf_len(b) != 0)
153 		fail++;
154 	sshbuf_free(b);
155 
156 	debug3_f("fail %d", fail);
157 
158 	if (fail)
159 		sshkey_free(key);
160 	else {
161 		if (ret != NULL) {
162 			*ret = key;
163 			key = NULL;
164 		}
165 		if (pkalgp != NULL) {
166 			*pkalgp = pkalg;
167 			pkalg = NULL;
168 		}
169 	}
170 	sshkey_free(key);
171 	free(pkalg);
172 	free(pkblob);
173 
174 	return (fail ? -1 : 0);
175 }
176 
177 int
178 main(int argc, char **argv)
179 {
180 	struct sshbuf *b;
181 	Options options;
182 #define NUM_KEYTYPES 5
183 	struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
184 	struct passwd *pw;
185 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
186 	u_char *signature, *data, rver;
187 	char *host, *fp, *pkalg;
188 	size_t slen, dlen;
189 
190 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
191 		fatal("%s: pledge: %s", __progname, strerror(errno));
192 
193 	/* Ensure that stdin and stdout are connected */
194 	if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
195 		exit(1);
196 	/* Leave /dev/null fd iff it is attached to stderr */
197 	if (fd > 2)
198 		close(fd);
199 
200 	i = 0;
201 	/* XXX This really needs to read sshd_config for the paths */
202 	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
203 	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
204 	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
205 	key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
206 	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
207 
208 	if ((pw = getpwuid(getuid())) == NULL)
209 		fatal("getpwuid failed");
210 	pw = pwcopy(pw);
211 
212 	permanently_set_uid(pw);
213 
214 	seed_rng();
215 
216 #ifdef DEBUG_SSH_KEYSIGN
217 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
218 #endif
219 
220 	/* verify that ssh-keysign is enabled by the admin */
221 	initialize_options(&options);
222 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
223 	    &options, 0, NULL);
224 	(void)fill_default_options(&options);
225 	if (options.enable_ssh_keysign != 1)
226 		fatal("ssh-keysign not enabled in %s",
227 		    _PATH_HOST_CONFIG_FILE);
228 
229 	if (pledge("stdio dns", NULL) != 0)
230 		fatal("%s: pledge: %s", __progname, strerror(errno));
231 
232 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
233 		if (key_fd[i] != -1)
234 			found = 1;
235 	}
236 	if (found == 0)
237 		fatal("could not open any host key");
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