xref: /freebsd/crypto/openssh/ssh-keysign.c (revision 2574974648c68c738aec3ff96644d888d7913a37)
1 /* $OpenBSD: ssh-keysign.c,v 1.80 2026/03/19 02:36:28 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 #include <paths.h>
30 #include <pwd.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <errno.h>
37 
38 #ifdef WITH_OPENSSL
39 #include <openssl/evp.h>
40 #include <openssl/rand.h>
41 #include <openssl/rsa.h>
42 #include "openbsd-compat/openssl-compat.h"
43 #endif
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
valid_request(struct passwd * pw,char * host,struct sshkey ** ret,char ** pkalgp,u_char * data,size_t datalen)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 (len == 0)
134 		fail++;
135 	else 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 		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
main(int argc,char ** argv)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 	/* 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 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
196 		fatal("%s: pledge: %s", __progname, strerror(errno));
197 
198 	for (i = 0; i < NUM_KEYTYPES; i++)
199 		key_fd[i] = -1;
200 
201 	i = 0;
202 	/* XXX This really needs to read sshd_config for the paths */
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_RSA_KEY_FILE, O_RDONLY);
206 
207 	if ((pw = getpwuid(getuid())) == NULL)
208 		fatal("getpwuid failed");
209 	pw = pwcopy(pw);
210 
211 	permanently_set_uid(pw);
212 
213 	seed_rng();
214 
215 #ifdef DEBUG_SSH_KEYSIGN
216 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
217 #endif
218 
219 	/* verify that ssh-keysign is enabled by the admin */
220 	initialize_options(&options);
221 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", "",
222 	    &options, 0, NULL);
223 	(void)fill_default_options(&options);
224 	if (options.enable_ssh_keysign != 1)
225 		fatal("ssh-keysign not enabled in %s",
226 		    _PATH_HOST_CONFIG_FILE);
227 
228 	if (pledge("stdio dns", NULL) != 0)
229 		fatal("%s: pledge: %s", __progname, strerror(errno));
230 
231 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
232 		if (key_fd[i] != -1)
233 			found = 1;
234 	}
235 	if (found == 0)
236 		fatal("could not open any host key");
237 
238 	found = 0;
239 	for (i = 0; i < NUM_KEYTYPES; i++) {
240 		keys[i] = NULL;
241 		if (key_fd[i] == -1)
242 			continue;
243 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
244 		    NULL, &key, NULL);
245 		close(key_fd[i]);
246 		if (r != 0)
247 			debug_r(r, "parse key %d", i);
248 		else if (key != NULL) {
249 			keys[i] = key;
250 			found = 1;
251 		}
252 	}
253 	if (!found)
254 		fatal("no hostkey found");
255 
256 	if ((b = sshbuf_new()) == NULL)
257 		fatal("%s: sshbuf_new failed", __progname);
258 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
259 		fatal("%s: ssh_msg_recv failed", __progname);
260 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
261 		fatal_r(r, "%s: buffer error", __progname);
262 	if (rver != version)
263 		fatal("%s: bad version: received %d, expected %d",
264 		    __progname, rver, version);
265 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
266 		fatal_r(r, "%s: buffer error", __progname);
267 	if (fd <= STDERR_FILENO)
268 		fatal("%s: bad fd = %d", __progname, fd);
269 	if ((host = get_local_name(fd)) == NULL)
270 		fatal("%s: cannot get local name for fd", __progname);
271 
272 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
273 		fatal_r(r, "%s: buffer error", __progname);
274 	if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
275 		fatal("%s: not a valid request", __progname);
276 	free(host);
277 
278 	found = 0;
279 	for (i = 0; i < NUM_KEYTYPES; i++) {
280 		if (keys[i] != NULL &&
281 		    sshkey_equal_public(key, keys[i])) {
282 			found = 1;
283 			break;
284 		}
285 	}
286 	if (!found) {
287 		if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
288 		    SSH_FP_DEFAULT)) == NULL)
289 			fatal("%s: sshkey_fingerprint failed", __progname);
290 		fatal("%s: no matching hostkey found for key %s %s", __progname,
291 		    sshkey_type(key), fp ? fp : "");
292 	}
293 
294 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
295 	    pkalg, NULL, NULL, 0)) != 0)
296 		fatal_r(r, "%s: sshkey_sign failed", __progname);
297 	free(data);
298 
299 	/* send reply */
300 	sshbuf_reset(b);
301 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
302 		fatal_r(r, "%s: buffer error", __progname);
303 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
304 		fatal("%s: ssh_msg_send failed", __progname);
305 
306 	return (0);
307 }
308