1 /* $OpenBSD: ssh-keysign.c,v 1.71 2022/08/01 11:09:26 djm Exp $ */
3 * Copyright (c) 2002 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
41 #include <openssl/evp.h>
42 #include <openssl/rand.h>
43 #include <openssl/rsa.h>
44 #include "openbsd-compat/openssl-compat.h"
57 #include "pathnames.h"
62 extern char *__progname;
65 valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
66 u_char *data, size_t datalen)
69 struct sshkey *key = NULL;
82 if ((b = sshbuf_from(data, datalen)) == NULL)
83 fatal_f("sshbuf_from failed");
86 if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
87 fatal_fr(r, "parse session ID");
88 if (len != 20 && /* SHA1 */
89 len != 32 && /* SHA256 */
90 len != 48 && /* SHA384 */
91 len != 64) /* SHA512 */
94 if ((r = sshbuf_get_u8(b, &type)) != 0)
95 fatal_fr(r, "parse type");
96 if (type != SSH2_MSG_USERAUTH_REQUEST)
100 if ((r = sshbuf_skip_string(b)) != 0)
101 fatal_fr(r, "parse user");
104 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
105 fatal_fr(r, "parse service");
106 if (strcmp("ssh-connection", p) != 0)
111 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
112 fatal_fr(r, "parse method");
113 if (strcmp("hostbased", p) != 0)
118 if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
119 (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
120 fatal_fr(r, "parse pk");
122 pktype = sshkey_type_from_name(pkalg);
123 if (pktype == KEY_UNSPEC)
125 else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
126 error_fr(r, "decode key");
128 } else if (key->type != pktype)
131 /* client host name, handle trailing dot */
132 if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
133 fatal_fr(r, "parse hostname");
134 debug2_f("check expect chost %s got %s", host, p);
135 if (strlen(host) != len - 1)
137 else if (p[len - 1] != '.')
139 else if (strncasecmp(host, p, len - 1) != 0)
144 if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
145 fatal_fr(r, "parse luser");
147 if (strcmp(pw->pw_name, luser) != 0)
152 if (sshbuf_len(b) != 0)
156 debug3_f("fail %d", fail);
163 if (pkalgp != NULL) {
172 return (fail ? -1 : 0);
176 main(int argc, char **argv)
180 #define NUM_KEYTYPES 5
181 struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
183 int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
184 u_char *signature, *data, rver;
185 char *host, *fp, *pkalg;
188 if (pledge("stdio rpath getpw dns id", NULL) != 0)
189 fatal("%s: pledge: %s", __progname, strerror(errno));
191 /* Ensure that stdin and stdout are connected */
192 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
194 /* Leave /dev/null fd iff it is attached to stderr */
199 /* XXX This really needs to read sshd_config for the paths */
200 key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
201 key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
202 key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
203 key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
204 key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
206 if ((pw = getpwuid(getuid())) == NULL)
207 fatal("getpwuid failed");
210 permanently_set_uid(pw);
214 #ifdef DEBUG_SSH_KEYSIGN
215 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
218 /* verify that ssh-keysign is enabled by the admin */
219 initialize_options(&options);
220 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
222 (void)fill_default_options(&options);
223 if (options.enable_ssh_keysign != 1)
224 fatal("ssh-keysign not enabled in %s",
225 _PATH_HOST_CONFIG_FILE);
227 if (pledge("stdio dns", NULL) != 0)
228 fatal("%s: pledge: %s", __progname, strerror(errno));
230 for (i = found = 0; i < NUM_KEYTYPES; i++) {
235 fatal("could not open any host key");
238 for (i = 0; i < NUM_KEYTYPES; i++) {
242 r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
246 debug_r(r, "parse key %d", i);
247 else if (key != NULL) {
253 fatal("no hostkey found");
255 if ((b = sshbuf_new()) == NULL)
256 fatal("%s: sshbuf_new failed", __progname);
257 if (ssh_msg_recv(STDIN_FILENO, b) < 0)
258 fatal("%s: ssh_msg_recv failed", __progname);
259 if ((r = sshbuf_get_u8(b, &rver)) != 0)
260 fatal_r(r, "%s: buffer error", __progname);
262 fatal("%s: bad version: received %d, expected %d",
263 __progname, rver, version);
264 if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
265 fatal_r(r, "%s: buffer error", __progname);
266 if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO)
267 fatal("%s: bad fd = %d", __progname, fd);
268 if ((host = get_local_name(fd)) == NULL)
269 fatal("%s: cannot get local name for fd", __progname);
271 if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
272 fatal_r(r, "%s: buffer error", __progname);
273 if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
274 fatal("%s: not a valid request", __progname);
278 for (i = 0; i < NUM_KEYTYPES; i++) {
279 if (keys[i] != NULL &&
280 sshkey_equal_public(key, keys[i])) {
286 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
287 SSH_FP_DEFAULT)) == NULL)
288 fatal("%s: sshkey_fingerprint failed", __progname);
289 fatal("%s: no matching hostkey found for key %s %s", __progname,
290 sshkey_type(key), fp ? fp : "");
293 if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
294 pkalg, NULL, NULL, 0)) != 0)
295 fatal_r(r, "%s: sshkey_sign failed", __progname);
300 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
301 fatal_r(r, "%s: buffer error", __progname);
302 if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
303 fatal("%s: ssh_msg_send failed", __progname);