xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-keysign.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: ssh-keysign.c,v 1.10 2015/08/13 10:33:21 christos Exp $	*/
2 /* $OpenBSD: ssh-keysign.c,v 1.49 2015/07/03 03:56:25 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.10 2015/08/13 10:33:21 christos Exp $");
29 #include <sys/types.h>
30 
31 #include <openssl/evp.h>
32 #include <openssl/rsa.h>
33 
34 #include <fcntl.h>
35 #include <paths.h>
36 #include <pwd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "xmalloc.h"
42 #include "log.h"
43 #include "sshkey.h"
44 #include "ssh.h"
45 #include "ssh2.h"
46 #include "misc.h"
47 #include "sshbuf.h"
48 #include "authfile.h"
49 #include "msg.h"
50 #include "canohost.h"
51 #include "pathnames.h"
52 #include "readconf.h"
53 #include "uidswap.h"
54 #include "sshkey.h"
55 #include "ssherr.h"
56 
57 /* XXX readconf.c needs these */
58 uid_t original_real_uid;
59 
60 static int
61 valid_request(struct passwd *pw, char *host, struct sshkey **ret,
62     u_char *data, size_t datalen)
63 {
64 	struct sshbuf *b;
65 	struct sshkey *key = NULL;
66 	u_char type, *pkblob;
67 	char *p;
68 	size_t blen, len;
69 	char *pkalg, *luser;
70 	int r, pktype, fail;
71 
72 	if (ret != NULL)
73 		*ret = NULL;
74 	fail = 0;
75 
76 	if ((b = sshbuf_from(data, datalen)) == NULL)
77 		fatal("%s: sshbuf_from failed", __func__);
78 
79 	/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
80 	if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
81 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
82 	if (len != 20 && len != 32)
83 		fail++;
84 
85 	if ((r = sshbuf_get_u8(b, &type)) != 0)
86 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
87 	if (type != SSH2_MSG_USERAUTH_REQUEST)
88 		fail++;
89 
90 	/* server user */
91 	if ((r = sshbuf_skip_string(b)) != 0)
92 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
93 
94 	/* service */
95 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
96 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
97 	if (strcmp("ssh-connection", p) != 0)
98 		fail++;
99 	free(p);
100 
101 	/* method */
102 	if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
103 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
104 	if (strcmp("hostbased", p) != 0)
105 		fail++;
106 	free(p);
107 
108 	/* pubkey */
109 	if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
110 	    (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
111 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
112 
113 	pktype = sshkey_type_from_name(pkalg);
114 	if (pktype == KEY_UNSPEC)
115 		fail++;
116 	else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
117 		error("%s: bad key blob: %s", __func__, ssh_err(r));
118 		fail++;
119 	} else if (key->type != pktype)
120 		fail++;
121 	free(pkalg);
122 	free(pkblob);
123 
124 	/* client host name, handle trailing dot */
125 	if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
126 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
127 	debug2("%s: check expect chost %s got %s", __func__, host, p);
128 	if (strlen(host) != len - 1)
129 		fail++;
130 	else if (p[len - 1] != '.')
131 		fail++;
132 	else if (strncasecmp(host, p, len - 1) != 0)
133 		fail++;
134 	free(p);
135 
136 	/* local user */
137 	if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
138 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
139 
140 	if (strcmp(pw->pw_name, luser) != 0)
141 		fail++;
142 	free(luser);
143 
144 	/* end of message */
145 	if (sshbuf_len(b) != 0)
146 		fail++;
147 	sshbuf_free(b);
148 
149 	debug3("%s: fail %d", __func__, fail);
150 
151 	if (fail && key != NULL)
152 		sshkey_free(key);
153 	else if (ret != NULL)
154 		*ret = key;
155 
156 	return (fail ? -1 : 0);
157 }
158 
159 int
160 main(int argc, char **argv)
161 {
162 	struct sshbuf *b;
163 	Options options;
164 #define NUM_KEYTYPES 4
165 	struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
166 	struct passwd *pw;
167 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
168 	u_char *signature, *data, rver;
169 	char *host, *fp;
170 	size_t slen, dlen;
171 
172 	key = NULL;	/* XXX gcc */
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_RSA_KEY_FILE, O_RDONLY);
187 
188 	original_real_uid = getuid();	/* XXX readconf.c needs this */
189 	if ((pw = getpwuid(original_real_uid)) == 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, "", "", &options, 0);
202 	fill_default_options(&options);
203 	if (options.enable_ssh_keysign != 1)
204 		fatal("ssh-keysign not enabled in %s",
205 		    _PATH_HOST_CONFIG_FILE);
206 
207 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
208 		if (key_fd[i] != -1)
209 			found = 1;
210 	}
211 	if (found == 0)
212 		fatal("could not open any host key");
213 
214 	OpenSSL_add_all_algorithms();
215 
216 	found = 0;
217 	for (i = 0; i < NUM_KEYTYPES; i++) {
218 		keys[i] = NULL;
219 		if (key_fd[i] == -1)
220 			continue;
221 		r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
222 		    NULL, &key, NULL);
223 		close(key_fd[i]);
224 		if (r != 0)
225 			debug("parse key %d: %s", i, ssh_err(r));
226 		else if (key != NULL) {
227 			keys[i] = key;
228 			found = 1;
229 		}
230 	}
231 	if (!found)
232 		fatal("no hostkey found");
233 
234 	if ((b = sshbuf_new()) == NULL)
235 		fatal("%s: sshbuf_new failed", __func__);
236 	if (ssh_msg_recv(STDIN_FILENO, b) < 0)
237 		fatal("ssh_msg_recv failed");
238 	if ((r = sshbuf_get_u8(b, &rver)) != 0)
239 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
240 	if (rver != version)
241 		fatal("bad version: received %d, expected %d", rver, version);
242 	if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
243 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
244 	if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
245 		fatal("bad fd");
246 	if ((host = get_local_name(fd)) == NULL)
247 		fatal("cannot get local name for fd");
248 
249 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
250 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
251 	if (valid_request(pw, host, &key, data, dlen) < 0)
252 		fatal("not a valid request");
253 	free(host);
254 
255 	found = 0;
256 	for (i = 0; i < NUM_KEYTYPES; i++) {
257 		if (keys[i] != NULL &&
258 		    sshkey_equal_public(key, keys[i])) {
259 			found = 1;
260 			break;
261 		}
262 	}
263 	if (!found) {
264 		if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
265 		    SSH_FP_DEFAULT)) == NULL)
266 			fatal("%s: sshkey_fingerprint failed", __func__);
267 		fatal("no matching hostkey found for key %s %s",
268 		    sshkey_type(key), fp ? fp : "");
269 	}
270 
271 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, 0)) != 0)
272 		fatal("sshkey_sign failed: %s", ssh_err(r));
273 	free(data);
274 
275 	/* send reply */
276 	sshbuf_reset(b);
277 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
278 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
279 	if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
280 		fatal("ssh_msg_send failed");
281 
282 	return (0);
283 }
284