1 /* $NetBSD: ssh-keysign.c,v 1.26 2024/07/08 22:33:44 christos Exp $ */
2 /* $OpenBSD: ssh-keysign.c,v 1.74 2024/04/30 05:53:03 djm Exp $ */
3
4 /*
5 * Copyright (c) 2002 Markus Friedl. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "includes.h"
29 __RCSID("$NetBSD: ssh-keysign.c,v 1.26 2024/07/08 22:33:44 christos Exp $");
30 #include <sys/types.h>
31
32 #ifdef WITH_OPENSSL
33 #include <openssl/evp.h>
34 #endif
35
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <pwd.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <errno.h>
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 "ssherr.h"
60
61 extern char *__progname;
62
63 static int
valid_request(struct passwd * pw,char * host,struct sshkey ** ret,char ** pkalgp,u_char * data,size_t datalen)64 valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
65 u_char *data, size_t datalen)
66 {
67 struct sshbuf *b;
68 struct sshkey *key = NULL;
69 u_char type, *pkblob;
70 char *p;
71 size_t blen, len;
72 char *pkalg, *luser;
73 int r, pktype, fail;
74
75 if (ret != NULL)
76 *ret = NULL;
77 if (pkalgp != NULL)
78 *pkalgp = NULL;
79 fail = 0;
80
81 if ((b = sshbuf_from(data, datalen)) == NULL)
82 fatal_f("sshbuf_from failed");
83
84 /* session id */
85 if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
86 fatal_fr(r, "parse session ID");
87 if (len != 20 && /* SHA1 */
88 len != 32 && /* SHA256 */
89 len != 48 && /* SHA384 */
90 len != 64) /* SHA512 */
91 fail++;
92
93 if ((r = sshbuf_get_u8(b, &type)) != 0)
94 fatal_fr(r, "parse type");
95 if (type != SSH2_MSG_USERAUTH_REQUEST)
96 fail++;
97
98 /* server user */
99 if ((r = sshbuf_skip_string(b)) != 0)
100 fatal_fr(r, "parse user");
101
102 /* service */
103 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
104 fatal_fr(r, "parse service");
105 if (strcmp("ssh-connection", p) != 0)
106 fail++;
107 free(p);
108
109 /* method */
110 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
111 fatal_fr(r, "parse method");
112 if (strcmp("hostbased", p) != 0)
113 fail++;
114 free(p);
115
116 /* pubkey */
117 if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
118 (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
119 fatal_fr(r, "parse pk");
120
121 pktype = sshkey_type_from_name(pkalg);
122 if (pktype == KEY_UNSPEC)
123 fail++;
124 else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
125 error_fr(r, "decode key");
126 fail++;
127 } else if (key->type != pktype)
128 fail++;
129
130 /* client host name, handle trailing dot */
131 if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
132 fatal_fr(r, "parse hostname");
133 debug2_f("check expect chost %s got %s", host, p);
134 if (strlen(host) != len - 1)
135 fail++;
136 else if (p[len - 1] != '.')
137 fail++;
138 else if (strncasecmp(host, p, len - 1) != 0)
139 fail++;
140 free(p);
141
142 /* local user */
143 if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
144 fatal_fr(r, "parse luser");
145
146 if (strcmp(pw->pw_name, luser) != 0)
147 fail++;
148 free(luser);
149
150 /* end of message */
151 if (sshbuf_len(b) != 0)
152 fail++;
153 sshbuf_free(b);
154
155 debug3_f("fail %d", fail);
156
157 if (!fail) {
158 if (ret != NULL) {
159 *ret = key;
160 key = NULL;
161 }
162 if (pkalgp != NULL) {
163 *pkalgp = pkalg;
164 pkalg = NULL;
165 }
166 }
167 sshkey_free(key);
168 free(pkalg);
169 free(pkblob);
170
171 return (fail ? -1 : 0);
172 }
173
174 int
main(int argc,char ** argv)175 main(int argc, char **argv)
176 {
177 struct sshbuf *b;
178 Options options;
179 #define NUM_KEYTYPES 5
180 struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
181 struct passwd *pw;
182 int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
183 u_char *signature, *data, rver;
184 char *host, *fp, *pkalg;
185 size_t slen, dlen;
186
187 #ifdef __OpenBSD__
188 if (pledge("stdio rpath getpw dns id", NULL) != 0)
189 fatal("%s: pledge: %s", __progname, strerror(errno));
190 #endif
191
192 /* Ensure that stdin and stdout are connected */
193 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
194 exit(1);
195 /* Leave /dev/null fd iff it is attached to stderr */
196 if (fd > 2)
197 close(fd);
198
199 for (i = 0; i < NUM_KEYTYPES; i++)
200 key_fd[i] = -1;
201
202 i = 0;
203 /* XXX This really needs to read sshd_config for the paths */
204 #ifdef WITH_DSA
205 key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
206 #endif
207 key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
208 key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
209 key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
210 key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
211
212 if ((pw = getpwuid(getuid())) == NULL)
213 fatal("getpwuid failed");
214 pw = pwcopy(pw);
215
216 permanently_set_uid(pw);
217
218 #ifdef DEBUG_SSH_KEYSIGN
219 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
220 #endif
221
222 /* verify that ssh-keysign is enabled by the admin */
223 initialize_options(&options);
224 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
225 &options, 0, NULL);
226 (void)fill_default_options(&options);
227 if (options.enable_ssh_keysign != 1)
228 fatal("ssh-keysign not enabled in %s",
229 _PATH_HOST_CONFIG_FILE);
230
231 #ifdef __OpenBSD__
232 if (pledge("stdio dns", NULL) != 0)
233 fatal("%s: pledge: %s", __progname, strerror(errno));
234 #endif
235
236 for (i = found = 0; i < NUM_KEYTYPES; i++) {
237 if (key_fd[i] != -1)
238 found = 1;
239 }
240 if (found == 0)
241 fatal("could not open any host key");
242
243 #ifdef WITH_OPENSSL
244 OpenSSL_add_all_algorithms();
245 #endif
246
247 found = 0;
248 for (i = 0; i < NUM_KEYTYPES; i++) {
249 keys[i] = NULL;
250 if (key_fd[i] == -1)
251 continue;
252 r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
253 NULL, &key, NULL);
254 close(key_fd[i]);
255 if (r != 0)
256 debug_r(r, "parse key %d", i);
257 else if (key != NULL) {
258 keys[i] = key;
259 found = 1;
260 }
261 }
262 if (!found)
263 fatal("no hostkey found");
264
265 if ((b = sshbuf_new()) == NULL)
266 fatal("%s: sshbuf_new failed", __progname);
267 if (ssh_msg_recv(STDIN_FILENO, b) < 0)
268 fatal("%s: ssh_msg_recv failed", __progname);
269 if ((r = sshbuf_get_u8(b, &rver)) != 0)
270 fatal_r(r, "%s: buffer error", __progname);
271 if (rver != version)
272 fatal("%s: bad version: received %d, expected %d",
273 __progname, rver, version);
274 if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
275 fatal_r(r, "%s: buffer error", __progname);
276 if (fd <= STDERR_FILENO)
277 fatal("%s: bad fd = %d", __progname, fd);
278 if ((host = get_local_name(fd)) == NULL)
279 fatal("%s: cannot get local name for fd", __progname);
280
281 if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
282 fatal_r(r, "%s: buffer error", __progname);
283 if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
284 fatal("%s: not a valid request", __progname);
285 free(host);
286
287 found = 0;
288 for (i = 0; i < NUM_KEYTYPES; i++) {
289 if (keys[i] != NULL &&
290 sshkey_equal_public(key, keys[i])) {
291 found = 1;
292 break;
293 }
294 }
295 if (!found) {
296 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
297 SSH_FP_DEFAULT)) == NULL)
298 fatal("%s: sshkey_fingerprint failed", __progname);
299 fatal("%s: no matching hostkey found for key %s %s", __progname,
300 sshkey_type(key), fp ? fp : "");
301 }
302
303 if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
304 pkalg, NULL, NULL, 0)) != 0)
305 fatal_r(r, "%s: sshkey_sign failed", __progname);
306 free(data);
307
308 /* send reply */
309 sshbuf_reset(b);
310 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
311 fatal_r(r, "%s: buffer error", __progname);
312 if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
313 fatal("%s: ssh_msg_send failed", __progname);
314
315 return (0);
316 }
317