xref: /openbsd-src/usr.bin/ssh/ssh-keysign.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /* $OpenBSD: ssh-keysign.c,v 1.43 2014/10/08 22:20:25 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 <sys/types.h>
27 
28 #include <openssl/evp.h>
29 #include <openssl/rsa.h>
30 
31 #include <fcntl.h>
32 #include <paths.h>
33 #include <pwd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include "xmalloc.h"
39 #include "log.h"
40 #include "key.h"
41 #include "ssh.h"
42 #include "ssh2.h"
43 #include "misc.h"
44 #include "buffer.h"
45 #include "authfile.h"
46 #include "msg.h"
47 #include "canohost.h"
48 #include "pathnames.h"
49 #include "readconf.h"
50 #include "uidswap.h"
51 
52 /* XXX readconf.c needs these */
53 uid_t original_real_uid;
54 
55 static int
56 valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
57     u_int datalen)
58 {
59 	Buffer b;
60 	Key *key = NULL;
61 	u_char *pkblob;
62 	u_int blen, len;
63 	char *pkalg, *p;
64 	int pktype, fail;
65 
66 	fail = 0;
67 
68 	buffer_init(&b);
69 	buffer_append(&b, data, datalen);
70 
71 	/* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */
72 	p = buffer_get_string(&b, &len);
73 	if (len != 20 && len != 32)
74 		fail++;
75 	free(p);
76 
77 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
78 		fail++;
79 
80 	/* server user */
81 	buffer_skip_string(&b);
82 
83 	/* service */
84 	p = buffer_get_string(&b, NULL);
85 	if (strcmp("ssh-connection", p) != 0)
86 		fail++;
87 	free(p);
88 
89 	/* method */
90 	p = buffer_get_string(&b, NULL);
91 	if (strcmp("hostbased", p) != 0)
92 		fail++;
93 	free(p);
94 
95 	/* pubkey */
96 	pkalg = buffer_get_string(&b, NULL);
97 	pkblob = buffer_get_string(&b, &blen);
98 
99 	pktype = key_type_from_name(pkalg);
100 	if (pktype == KEY_UNSPEC)
101 		fail++;
102 	else if ((key = key_from_blob(pkblob, blen)) == NULL)
103 		fail++;
104 	else if (key->type != pktype)
105 		fail++;
106 	free(pkalg);
107 	free(pkblob);
108 
109 	/* client host name, handle trailing dot */
110 	p = buffer_get_string(&b, &len);
111 	debug2("valid_request: check expect chost %s got %s", host, p);
112 	if (strlen(host) != len - 1)
113 		fail++;
114 	else if (p[len - 1] != '.')
115 		fail++;
116 	else if (strncasecmp(host, p, len - 1) != 0)
117 		fail++;
118 	free(p);
119 
120 	/* local user */
121 	p = buffer_get_string(&b, NULL);
122 
123 	if (strcmp(pw->pw_name, p) != 0)
124 		fail++;
125 	free(p);
126 
127 	/* end of message */
128 	if (buffer_len(&b) != 0)
129 		fail++;
130 	buffer_free(&b);
131 
132 	debug3("valid_request: fail %d", fail);
133 
134 	if (fail && key != NULL)
135 		key_free(key);
136 	else
137 		*ret = key;
138 
139 	return (fail ? -1 : 0);
140 }
141 
142 int
143 main(int argc, char **argv)
144 {
145 	Buffer b;
146 	Options options;
147 #define NUM_KEYTYPES 4
148 	Key *keys[NUM_KEYTYPES], *key = NULL;
149 	struct passwd *pw;
150 	int key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
151 	u_char *signature, *data;
152 	char *host, *fp;
153 	u_int slen, dlen;
154 
155 	/* Ensure that stdin and stdout are connected */
156 	if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
157 		exit(1);
158 	/* Leave /dev/null fd iff it is attached to stderr */
159 	if (fd > 2)
160 		close(fd);
161 
162 	i = 0;
163 	key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
164 	key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
165 	key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
166 	key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
167 
168 	original_real_uid = getuid();	/* XXX readconf.c needs this */
169 	if ((pw = getpwuid(original_real_uid)) == NULL)
170 		fatal("getpwuid failed");
171 	pw = pwcopy(pw);
172 
173 	permanently_set_uid(pw);
174 
175 #ifdef DEBUG_SSH_KEYSIGN
176 	log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
177 #endif
178 
179 	/* verify that ssh-keysign is enabled by the admin */
180 	initialize_options(&options);
181 	(void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", &options, 0);
182 	fill_default_options(&options);
183 	if (options.enable_ssh_keysign != 1)
184 		fatal("ssh-keysign not enabled in %s",
185 		    _PATH_HOST_CONFIG_FILE);
186 
187 	for (i = found = 0; i < NUM_KEYTYPES; i++) {
188 		if (key_fd[i] != -1)
189 			found = 1;
190 	}
191 	if (found == 0)
192 		fatal("could not open any host key");
193 
194 	OpenSSL_add_all_algorithms();
195 
196 	found = 0;
197 	for (i = 0; i < NUM_KEYTYPES; i++) {
198 		keys[i] = NULL;
199 		if (key_fd[i] == -1)
200 			continue;
201 #ifdef WITH_OPENSSL
202 /* XXX wrong api */
203 		keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
204 		    NULL, NULL);
205 #endif
206 		close(key_fd[i]);
207 		if (keys[i] != NULL)
208 			found = 1;
209 	}
210 	if (!found)
211 		fatal("no hostkey found");
212 
213 	buffer_init(&b);
214 	if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
215 		fatal("ssh_msg_recv failed");
216 	if (buffer_get_char(&b) != version)
217 		fatal("bad version");
218 	fd = buffer_get_int(&b);
219 	if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
220 		fatal("bad fd");
221 	if ((host = get_local_name(fd)) == NULL)
222 		fatal("cannot get local name for fd");
223 
224 	data = buffer_get_string(&b, &dlen);
225 	if (valid_request(pw, host, &key, data, dlen) < 0)
226 		fatal("not a valid request");
227 	free(host);
228 
229 	found = 0;
230 	for (i = 0; i < NUM_KEYTYPES; i++) {
231 		if (keys[i] != NULL &&
232 		    key_equal_public(key, keys[i])) {
233 			found = 1;
234 			break;
235 		}
236 	}
237 	if (!found) {
238 		fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
239 		fatal("no matching hostkey found for key %s %s",
240 		    key_type(key), fp);
241 	}
242 
243 	if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
244 		fatal("key_sign failed");
245 	free(data);
246 
247 	/* send reply */
248 	buffer_clear(&b);
249 	buffer_put_string(&b, signature, slen);
250 	if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
251 		fatal("ssh_msg_send failed");
252 
253 	return (0);
254 }
255