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