xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-keysign.c (revision 2718af68c3efc72c9769069b5c7f9ed36f6b9def)
1 /*	$NetBSD: ssh-keysign.c,v 1.23 2022/02/23 19:07:20 christos Exp $	*/
2 /* $OpenBSD: ssh-keysign.c,v 1.70 2022/01/06 22:00:18 djm Exp $ */
3 /*
4  * Copyright (c) 2002 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 __RCSID("$NetBSD: ssh-keysign.c,v 1.23 2022/02/23 19:07:20 christos Exp $");
29 #include <sys/types.h>
30 
31 #ifdef WITH_OPENSSL
32 #include <openssl/evp.h>
33 #endif
34 
35 #include <fcntl.h>
36 #include <paths.h>
37 #include <pwd.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
44 
45 #include "xmalloc.h"
46 #include "log.h"
47 #include "sshkey.h"
48 #include "ssh.h"
49 #include "ssh2.h"
50 #include "misc.h"
51 #include "sshbuf.h"
52 #include "authfile.h"
53 #include "msg.h"
54 #include "canohost.h"
55 #include "pathnames.h"
56 #include "readconf.h"
57 #include "uidswap.h"
58 #include "ssherr.h"
59 
60 extern char *__progname;
61 
62 static int
63 valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
64     u_char *data, size_t datalen)
65 {
66 	struct sshbuf *b;
67 	struct sshkey *key = NULL;
68 	u_char type, *pkblob;
69 	char *p;
70 	size_t blen, len;
71 	char *pkalg, *luser;
72 	int r, pktype, fail;
73 
74 	if (ret != NULL)
75 		*ret = NULL;
76 	if (pkalgp != NULL)
77 		*pkalgp = NULL;
78 	fail = 0;
79 
80 	if ((b = sshbuf_from(data, datalen)) == NULL)
81 		fatal_f("sshbuf_from failed");
82 
83 	/* session id */
84 	if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
85 		fatal_fr(r, "parse session ID");
86 	if (len != 20 && /* SHA1 */
87 	    len != 32 && /* SHA256 */
88 	    len != 48 && /* SHA384 */
89 	    len != 64)   /* SHA512 */
90 		fail++;
91 
92 	if ((r = sshbuf_get_u8(b, &type)) != 0)
93 		fatal_fr(r, "parse type");
94 	if (type != SSH2_MSG_USERAUTH_REQUEST)
95 		fail++;
96 
97 	/* server user */
98 	if ((r = sshbuf_skip_string(b)) != 0)
99 		fatal_fr(r, "parse user");
100 
101 	/* service */
102 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
103 		fatal_fr(r, "parse service");
104 	if (strcmp("ssh-connection", p) != 0)
105 		fail++;
106 	free(p);
107 
108 	/* method */
109 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
110 		fatal_fr(r, "parse method");
111 	if (strcmp("hostbased", p) != 0)
112 		fail++;
113 	free(p);
114 
115 	/* pubkey */
116 	if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
117 	    (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
118 		fatal_fr(r, "parse pk");
119 
120 	pktype = sshkey_type_from_name(pkalg);
121 	if (pktype == KEY_UNSPEC)
122 		fail++;
123 	else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
124 		error_fr(r, "decode key");
125 		fail++;
126 	} else if (key->type != pktype)
127 		fail++;
128 
129 	/* client host name, handle trailing dot */
130 	if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
131 		fatal_fr(r, "parse hostname");
132 	debug2_f("check expect chost %s got %s", host, p);
133 	if (strlen(host) != len - 1)
134 		fail++;
135 	else if (p[len - 1] != '.')
136 		fail++;
137 	else if (strncasecmp(host, p, len - 1) != 0)
138 		fail++;
139 	free(p);
140 
141 	/* local user */
142 	if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
143 		fatal_fr(r, "parse luser");
144 
145 	if (strcmp(pw->pw_name, luser) != 0)
146 		fail++;
147 	free(luser);
148 
149 	/* end of message */
150 	if (sshbuf_len(b) != 0)
151 		fail++;
152 	sshbuf_free(b);
153 
154 	debug3_f("fail %d", fail);
155 
156 	if (fail)
157 		sshkey_free(key);
158 	else {
159 		if (ret != NULL) {
160 			*ret = key;
161 			key = NULL;
162 		}
163 		if (pkalgp != NULL) {
164 			*pkalgp = pkalg;
165 			pkalg = NULL;
166 		}
167 	}
168 	sshkey_free(key);
169 	free(pkalg);
170 	free(pkblob);
171 
172 	return (fail ? -1 : 0);
173 }
174 
175 int
176 main(int argc, char **argv)
177 {
178 	struct sshbuf *b;
179 	Options options;
180 #define NUM_KEYTYPES 5
181 	struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
182 	struct passwd *pw;
183 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
184 	u_char *signature, *data, rver;
185 	char *host, *fp, *pkalg;
186 	size_t slen, dlen;
187 
188 #ifdef __OpenBSD__
189 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
190 		fatal("%s: pledge: %s", __progname, strerror(errno));
191 #endif
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 #ifdef DEBUG_SSH_KEYSIGN
215 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
216 #endif
217 
218 	/* verify that ssh-keysign is enabled by the admin */
219 	initialize_options(&options);
220 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
221 	    &options, 0, NULL);
222 	(void)fill_default_options(&options);
223 	if (options.enable_ssh_keysign != 1)
224 		fatal("ssh-keysign not enabled in %s",
225 		    _PATH_HOST_CONFIG_FILE);
226 
227 #ifdef __OpenBSD__
228 	if (pledge("stdio dns", NULL) != 0)
229 		fatal("%s: pledge: %s", __progname, strerror(errno));
230 #endif
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 #ifdef WITH_OPENSSL
240 	OpenSSL_add_all_algorithms();
241 #endif
242 
243 	found = 0;
244 	for (i = 0; i < NUM_KEYTYPES; i++) {
245 		keys[i] = NULL;
246 		if (key_fd[i] == -1)
247 			continue;
248 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
249 		    NULL, &key, NULL);
250 		close(key_fd[i]);
251 		if (r != 0)
252 			debug_r(r, "parse key %d", i);
253 		else if (key != NULL) {
254 			keys[i] = key;
255 			found = 1;
256 		}
257 	}
258 	if (!found)
259 		fatal("no hostkey found");
260 
261 	if ((b = sshbuf_new()) == NULL)
262 		fatal("%s: sshbuf_new failed", __progname);
263 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
264 		fatal("%s: ssh_msg_recv failed", __progname);
265 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
266 		fatal_r(r, "%s: buffer error", __progname);
267 	if (rver != version)
268 		fatal("%s: bad version: received %d, expected %d",
269 		    __progname, rver, version);
270 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
271 		fatal_r(r, "%s: buffer error", __progname);
272 	if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
273 		fatal("%s: bad fd = %d", __progname, fd);
274 	if ((host = get_local_name(fd)) == NULL)
275 		fatal("%s: cannot get local name for fd", __progname);
276 
277 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
278 		fatal_r(r, "%s: buffer error", __progname);
279 	if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
280 		fatal("%s: not a valid request", __progname);
281 	free(host);
282 
283 	found = 0;
284 	for (i = 0; i < NUM_KEYTYPES; i++) {
285 		if (keys[i] != NULL &&
286 		    sshkey_equal_public(key, keys[i])) {
287 			found = 1;
288 			break;
289 		}
290 	}
291 	if (!found) {
292 		if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
293 		    SSH_FP_DEFAULT)) == NULL)
294 			fatal("%s: sshkey_fingerprint failed", __progname);
295 		fatal("%s: no matching hostkey found for key %s %s", __progname,
296 		    sshkey_type(key), fp ? fp : "");
297 	}
298 
299 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
300 	    pkalg, NULL, NULL, 0)) != 0)
301 		fatal_r(r, "%s: sshkey_sign failed", __progname);
302 	free(data);
303 
304 	/* send reply */
305 	sshbuf_reset(b);
306 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
307 		fatal_r(r, "%s: buffer error", __progname);
308 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
309 		fatal("%s: ssh_msg_send failed", __progname);
310 
311 	return (0);
312 }
313