xref: /openbsd/usr.bin/ssh/ssh-keysign.c (revision 4cfece93)
1 /* $OpenBSD: ssh-keysign.c,v 1.63 2019/11/18 16:10:05 naddy 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 <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdarg.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,
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 	fail = 0;
74 
75 	if ((b = sshbuf_from(data, datalen)) == NULL)
76 		fatal("%s: sshbuf_from failed", __func__);
77 
78 	/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
79 	if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
80 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
81 	if (len != 20 && len != 32)
82 		fail++;
83 
84 	if ((r = sshbuf_get_u8(b, &type)) != 0)
85 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
86 	if (type != SSH2_MSG_USERAUTH_REQUEST)
87 		fail++;
88 
89 	/* server user */
90 	if ((r = sshbuf_skip_string(b)) != 0)
91 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
92 
93 	/* service */
94 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
95 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
96 	if (strcmp("ssh-connection", p) != 0)
97 		fail++;
98 	free(p);
99 
100 	/* method */
101 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
102 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
103 	if (strcmp("hostbased", p) != 0)
104 		fail++;
105 	free(p);
106 
107 	/* pubkey */
108 	if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
109 	    (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
110 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
111 
112 	pktype = sshkey_type_from_name(pkalg);
113 	if (pktype == KEY_UNSPEC)
114 		fail++;
115 	else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
116 		error("%s: bad key blob: %s", __func__, ssh_err(r));
117 		fail++;
118 	} else if (key->type != pktype)
119 		fail++;
120 	free(pkalg);
121 	free(pkblob);
122 
123 	/* client host name, handle trailing dot */
124 	if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
125 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
126 	debug2("%s: check expect chost %s got %s", __func__, host, p);
127 	if (strlen(host) != len - 1)
128 		fail++;
129 	else if (p[len - 1] != '.')
130 		fail++;
131 	else if (strncasecmp(host, p, len - 1) != 0)
132 		fail++;
133 	free(p);
134 
135 	/* local user */
136 	if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
137 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
138 
139 	if (strcmp(pw->pw_name, luser) != 0)
140 		fail++;
141 	free(luser);
142 
143 	/* end of message */
144 	if (sshbuf_len(b) != 0)
145 		fail++;
146 	sshbuf_free(b);
147 
148 	debug3("%s: fail %d", __func__, fail);
149 
150 	if (fail)
151 		sshkey_free(key);
152 	else if (ret != NULL)
153 		*ret = key;
154 
155 	return (fail ? -1 : 0);
156 }
157 
158 int
159 main(int argc, char **argv)
160 {
161 	struct sshbuf *b;
162 	Options options;
163 #define NUM_KEYTYPES 5
164 	struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
165 	struct passwd *pw;
166 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
167 	u_char *signature, *data, rver;
168 	char *host, *fp;
169 	size_t slen, dlen;
170 
171 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
172 		fatal("%s: pledge: %s", __progname, strerror(errno));
173 
174 	/* Ensure that stdin and stdout are connected */
175 	if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
176 		exit(1);
177 	/* Leave /dev/null fd iff it is attached to stderr */
178 	if (fd > 2)
179 		close(fd);
180 
181 	i = 0;
182 	/* XXX This really needs to read sshd_config for the paths */
183 	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
184 	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
185 	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
186 	key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
187 	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
188 
189 	if ((pw = getpwuid(getuid())) == NULL)
190 		fatal("getpwuid failed");
191 	pw = pwcopy(pw);
192 
193 	permanently_set_uid(pw);
194 
195 #ifdef DEBUG_SSH_KEYSIGN
196 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
197 #endif
198 
199 	/* verify that ssh-keysign is enabled by the admin */
200 	initialize_options(&options);
201 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
202 	    &options, 0, NULL);
203 	fill_default_options(&options);
204 	if (options.enable_ssh_keysign != 1)
205 		fatal("ssh-keysign not enabled in %s",
206 		    _PATH_HOST_CONFIG_FILE);
207 
208 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
209 		if (key_fd[i] != -1)
210 			found = 1;
211 	}
212 	if (found == 0)
213 		fatal("could not open any host key");
214 
215 #ifdef WITH_OPENSSL
216 	OpenSSL_add_all_algorithms();
217 #endif
218 	found = 0;
219 	for (i = 0; i < NUM_KEYTYPES; i++) {
220 		keys[i] = NULL;
221 		if (key_fd[i] == -1)
222 			continue;
223 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
224 		    NULL, &key, NULL);
225 		close(key_fd[i]);
226 		if (r != 0)
227 			debug("parse key %d: %s", i, ssh_err(r));
228 		else if (key != NULL) {
229 			keys[i] = key;
230 			found = 1;
231 		}
232 	}
233 	if (!found)
234 		fatal("no hostkey found");
235 
236 	if (pledge("stdio dns", NULL) != 0)
237 		fatal("%s: pledge: %s", __progname, strerror(errno));
238 
239 	if ((b = sshbuf_new()) == NULL)
240 		fatal("%s: sshbuf_new failed", __progname);
241 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
242 		fatal("ssh_msg_recv failed");
243 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
244 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
245 	if (rver != version)
246 		fatal("bad version: received %d, expected %d", rver, version);
247 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
248 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
249 	if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
250 		fatal("bad fd = %d", fd);
251 	if ((host = get_local_name(fd)) == NULL)
252 		fatal("cannot get local name for fd");
253 
254 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
255 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
256 	if (valid_request(pw, host, &key, data, dlen) < 0)
257 		fatal("not a valid request");
258 	free(host);
259 
260 	found = 0;
261 	for (i = 0; i < NUM_KEYTYPES; i++) {
262 		if (keys[i] != NULL &&
263 		    sshkey_equal_public(key, keys[i])) {
264 			found = 1;
265 			break;
266 		}
267 	}
268 	if (!found) {
269 		if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
270 		    SSH_FP_DEFAULT)) == NULL)
271 			fatal("%s: sshkey_fingerprint failed", __progname);
272 		fatal("no matching hostkey found for key %s %s",
273 		    sshkey_type(key), fp ? fp : "");
274 	}
275 
276 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
277 	    NULL, NULL, 0)) != 0)
278 		fatal("sshkey_sign failed: %s", ssh_err(r));
279 	free(data);
280 
281 	/* send reply */
282 	sshbuf_reset(b);
283 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
284 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
285 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
286 		fatal("ssh_msg_send failed");
287 
288 	return (0);
289 }
290