jcs's openbsd hax
openbsd
1/* $OpenBSD: ssh-keysign.c,v 1.79 2025/11/13 10:35:14 dtucker 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 <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.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
57extern char *__progname;
58
59static int
60valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
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 if (pkalgp != NULL)
74 *pkalgp = NULL;
75 fail = 0;
76
77 if ((b = sshbuf_from(data, datalen)) == NULL)
78 fatal_f("sshbuf_from failed");
79
80 /* session id */
81 if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
82 fatal_fr(r, "parse session ID");
83 if (len != 20 && /* SHA1 */
84 len != 32 && /* SHA256 */
85 len != 48 && /* SHA384 */
86 len != 64) /* SHA512 */
87 fail++;
88
89 if ((r = sshbuf_get_u8(b, &type)) != 0)
90 fatal_fr(r, "parse type");
91 if (type != SSH2_MSG_USERAUTH_REQUEST)
92 fail++;
93
94 /* server user */
95 if ((r = sshbuf_skip_string(b)) != 0)
96 fatal_fr(r, "parse user");
97
98 /* service */
99 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
100 fatal_fr(r, "parse service");
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_fr(r, "parse method");
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_fr(r, "parse pk");
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_fr(r, "decode key");
122 fail++;
123 } else if (key->type != pktype)
124 fail++;
125
126 /* client host name, handle trailing dot */
127 if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
128 fatal_fr(r, "parse hostname");
129 debug2_f("check expect chost \"%s\" got \"%s\"", host, p);
130 if (len == 0)
131 fail++;
132 else 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_fr(r, "parse luser");
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_f("fail %d", fail);
154
155 if (!fail) {
156 if (ret != NULL) {
157 *ret = key;
158 key = NULL;
159 }
160 if (pkalgp != NULL) {
161 *pkalgp = pkalg;
162 pkalg = NULL;
163 }
164 }
165 sshkey_free(key);
166 free(pkalg);
167 free(pkblob);
168
169 return (fail ? -1 : 0);
170}
171
172int
173main(int argc, char **argv)
174{
175 struct sshbuf *b;
176 Options options;
177#define NUM_KEYTYPES 5
178 struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
179 struct passwd *pw;
180 int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
181 u_char *signature, *data, rver;
182 char *host, *fp, *pkalg;
183 size_t slen, dlen;
184
185 if (pledge("stdio rpath getpw dns id", NULL) != 0)
186 fatal("%s: pledge: %s", __progname, strerror(errno));
187
188 /* Ensure that stdin and stdout are connected */
189 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
190 exit(1);
191 /* Leave /dev/null fd iff it is attached to stderr */
192 if (fd > 2)
193 close(fd);
194
195 for (i = 0; i < NUM_KEYTYPES; i++)
196 key_fd[i] = -1;
197
198 i = 0;
199 /* XXX This really needs to read sshd_config for the paths */
200 key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
201 key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
202 key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
203
204 if ((pw = getpwuid(getuid())) == NULL)
205 fatal("getpwuid failed");
206 pw = pwcopy(pw);
207
208 permanently_set_uid(pw);
209
210#ifdef DEBUG_SSH_KEYSIGN
211 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
212#endif
213
214 /* verify that ssh-keysign is enabled by the admin */
215 initialize_options(&options);
216 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", "",
217 &options, 0, NULL);
218 (void)fill_default_options(&options);
219 if (options.enable_ssh_keysign != 1)
220 fatal("ssh-keysign not enabled in %s",
221 _PATH_HOST_CONFIG_FILE);
222
223 if (pledge("stdio dns", NULL) != 0)
224 fatal("%s: pledge: %s", __progname, strerror(errno));
225
226 for (i = found = 0; i < NUM_KEYTYPES; i++) {
227 if (key_fd[i] != -1)
228 found = 1;
229 }
230 if (found == 0)
231 fatal("could not open any host key");
232
233 found = 0;
234 for (i = 0; i < NUM_KEYTYPES; i++) {
235 keys[i] = NULL;
236 if (key_fd[i] == -1)
237 continue;
238 r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
239 NULL, &key, NULL);
240 close(key_fd[i]);
241 if (r != 0)
242 debug_r(r, "parse key %d", i);
243 else if (key != NULL) {
244 keys[i] = key;
245 found = 1;
246 }
247 }
248 if (!found)
249 fatal("no hostkey found");
250
251 if ((b = sshbuf_new()) == NULL)
252 fatal("%s: sshbuf_new failed", __progname);
253 if (ssh_msg_recv(STDIN_FILENO, b) < 0)
254 fatal("%s: ssh_msg_recv failed", __progname);
255 if ((r = sshbuf_get_u8(b, &rver)) != 0)
256 fatal_r(r, "%s: buffer error", __progname);
257 if (rver != version)
258 fatal("%s: bad version: received %d, expected %d",
259 __progname, rver, version);
260 if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
261 fatal_r(r, "%s: buffer error", __progname);
262 if (fd <= STDERR_FILENO)
263 fatal("%s: bad fd = %d", __progname, fd);
264 if ((host = get_local_name(fd)) == NULL)
265 fatal("%s: cannot get local name for fd", __progname);
266
267 if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
268 fatal_r(r, "%s: buffer error", __progname);
269 if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
270 fatal("%s: not a valid request", __progname);
271 free(host);
272
273 found = 0;
274 for (i = 0; i < NUM_KEYTYPES; i++) {
275 if (keys[i] != NULL &&
276 sshkey_equal_public(key, keys[i])) {
277 found = 1;
278 break;
279 }
280 }
281 if (!found) {
282 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
283 SSH_FP_DEFAULT)) == NULL)
284 fatal("%s: sshkey_fingerprint failed", __progname);
285 fatal("%s: no matching hostkey found for key %s %s", __progname,
286 sshkey_type(key), fp ? fp : "");
287 }
288
289 if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
290 pkalg, NULL, NULL, 0)) != 0)
291 fatal_r(r, "%s: sshkey_sign failed", __progname);
292 free(data);
293
294 /* send reply */
295 sshbuf_reset(b);
296 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
297 fatal_r(r, "%s: buffer error", __progname);
298 if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
299 fatal("%s: ssh_msg_send failed", __progname);
300
301 return (0);
302}