1*1c7715ddSchristos /* $NetBSD: ssh-keysign.c,v 1.26 2024/07/08 22:33:44 christos Exp $ */
2*1c7715ddSchristos /* $OpenBSD: ssh-keysign.c,v 1.74 2024/04/30 05:53:03 djm Exp $ */
3e160b4e8Schristos
4ca32bd8dSchristos /*
5ca32bd8dSchristos * Copyright (c) 2002 Markus Friedl. All rights reserved.
6ca32bd8dSchristos *
7ca32bd8dSchristos * Redistribution and use in source and binary forms, with or without
8ca32bd8dSchristos * modification, are permitted provided that the following conditions
9ca32bd8dSchristos * are met:
10ca32bd8dSchristos * 1. Redistributions of source code must retain the above copyright
11ca32bd8dSchristos * notice, this list of conditions and the following disclaimer.
12ca32bd8dSchristos * 2. Redistributions in binary form must reproduce the above copyright
13ca32bd8dSchristos * notice, this list of conditions and the following disclaimer in the
14ca32bd8dSchristos * documentation and/or other materials provided with the distribution.
15ca32bd8dSchristos *
16ca32bd8dSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ca32bd8dSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ca32bd8dSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ca32bd8dSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ca32bd8dSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ca32bd8dSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ca32bd8dSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ca32bd8dSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ca32bd8dSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ca32bd8dSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ca32bd8dSchristos */
27ca32bd8dSchristos
28313c6c94Schristos #include "includes.h"
29*1c7715ddSchristos __RCSID("$NetBSD: ssh-keysign.c,v 1.26 2024/07/08 22:33:44 christos Exp $");
30ca32bd8dSchristos #include <sys/types.h>
31ca32bd8dSchristos
32cd4ada6aSchristos #ifdef WITH_OPENSSL
33ca32bd8dSchristos #include <openssl/evp.h>
34cd4ada6aSchristos #endif
35ca32bd8dSchristos
36ca32bd8dSchristos #include <fcntl.h>
37ca32bd8dSchristos #include <paths.h>
38ca32bd8dSchristos #include <pwd.h>
39ed75d7a8Schristos #include <stdarg.h>
40b592f463Schristos #include <stdio.h>
41b592f463Schristos #include <stdlib.h>
42b592f463Schristos #include <string.h>
43ca32bd8dSchristos #include <unistd.h>
4479976551Schristos #include <errno.h>
45ca32bd8dSchristos
46ca32bd8dSchristos #include "xmalloc.h"
47ca32bd8dSchristos #include "log.h"
48e4d43b82Schristos #include "sshkey.h"
49ca32bd8dSchristos #include "ssh.h"
50ca32bd8dSchristos #include "ssh2.h"
51ca32bd8dSchristos #include "misc.h"
52e4d43b82Schristos #include "sshbuf.h"
53ca32bd8dSchristos #include "authfile.h"
54ca32bd8dSchristos #include "msg.h"
55ca32bd8dSchristos #include "canohost.h"
56ca32bd8dSchristos #include "pathnames.h"
57ca32bd8dSchristos #include "readconf.h"
58ca32bd8dSchristos #include "uidswap.h"
59e4d43b82Schristos #include "ssherr.h"
60ca32bd8dSchristos
6179976551Schristos extern char *__progname;
6279976551Schristos
63ca32bd8dSchristos static int
valid_request(struct passwd * pw,char * host,struct sshkey ** ret,char ** pkalgp,u_char * data,size_t datalen)64a03ec00cSchristos valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
65e4d43b82Schristos u_char *data, size_t datalen)
66ca32bd8dSchristos {
67e4d43b82Schristos struct sshbuf *b;
68e4d43b82Schristos struct sshkey *key = NULL;
69e4d43b82Schristos u_char type, *pkblob;
70e4d43b82Schristos char *p;
71e4d43b82Schristos size_t blen, len;
72e4d43b82Schristos char *pkalg, *luser;
73e4d43b82Schristos int r, pktype, fail;
74ca32bd8dSchristos
75e4d43b82Schristos if (ret != NULL)
76e4d43b82Schristos *ret = NULL;
77a03ec00cSchristos if (pkalgp != NULL)
78a03ec00cSchristos *pkalgp = NULL;
79ca32bd8dSchristos fail = 0;
80ca32bd8dSchristos
81e4d43b82Schristos if ((b = sshbuf_from(data, datalen)) == NULL)
8217418e98Schristos fatal_f("sshbuf_from failed");
83ca32bd8dSchristos
84a03ec00cSchristos /* session id */
85e4d43b82Schristos if ((r = sshbuf_get_string(b, NULL, &len)) != 0)
8617418e98Schristos fatal_fr(r, "parse session ID");
87a03ec00cSchristos if (len != 20 && /* SHA1 */
88a03ec00cSchristos len != 32 && /* SHA256 */
89a03ec00cSchristos len != 48 && /* SHA384 */
90a03ec00cSchristos len != 64) /* SHA512 */
91ca32bd8dSchristos fail++;
92ca32bd8dSchristos
93e4d43b82Schristos if ((r = sshbuf_get_u8(b, &type)) != 0)
9417418e98Schristos fatal_fr(r, "parse type");
95e4d43b82Schristos if (type != SSH2_MSG_USERAUTH_REQUEST)
96ca32bd8dSchristos fail++;
97ca32bd8dSchristos
98ca32bd8dSchristos /* server user */
99e4d43b82Schristos if ((r = sshbuf_skip_string(b)) != 0)
10017418e98Schristos fatal_fr(r, "parse user");
101ca32bd8dSchristos
102ca32bd8dSchristos /* service */
103e4d43b82Schristos if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
10417418e98Schristos fatal_fr(r, "parse service");
105ca32bd8dSchristos if (strcmp("ssh-connection", p) != 0)
106ca32bd8dSchristos fail++;
10700a838c4Schristos free(p);
108ca32bd8dSchristos
109ca32bd8dSchristos /* method */
110e4d43b82Schristos if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0)
11117418e98Schristos fatal_fr(r, "parse method");
112ca32bd8dSchristos if (strcmp("hostbased", p) != 0)
113ca32bd8dSchristos fail++;
11400a838c4Schristos free(p);
115ca32bd8dSchristos
116ca32bd8dSchristos /* pubkey */
117e4d43b82Schristos if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 ||
118e4d43b82Schristos (r = sshbuf_get_string(b, &pkblob, &blen)) != 0)
11917418e98Schristos fatal_fr(r, "parse pk");
120ca32bd8dSchristos
121e4d43b82Schristos pktype = sshkey_type_from_name(pkalg);
122ca32bd8dSchristos if (pktype == KEY_UNSPEC)
123ca32bd8dSchristos fail++;
124e4d43b82Schristos else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
12517418e98Schristos error_fr(r, "decode key");
126ca32bd8dSchristos fail++;
127e4d43b82Schristos } else if (key->type != pktype)
128ca32bd8dSchristos fail++;
129ca32bd8dSchristos
130ca32bd8dSchristos /* client host name, handle trailing dot */
131e4d43b82Schristos if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
13217418e98Schristos fatal_fr(r, "parse hostname");
13317418e98Schristos debug2_f("check expect chost %s got %s", host, p);
134ca32bd8dSchristos if (strlen(host) != len - 1)
135ca32bd8dSchristos fail++;
136ca32bd8dSchristos else if (p[len - 1] != '.')
137ca32bd8dSchristos fail++;
138ca32bd8dSchristos else if (strncasecmp(host, p, len - 1) != 0)
139ca32bd8dSchristos fail++;
14000a838c4Schristos free(p);
141ca32bd8dSchristos
142ca32bd8dSchristos /* local user */
143e4d43b82Schristos if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0)
14417418e98Schristos fatal_fr(r, "parse luser");
145ca32bd8dSchristos
146e4d43b82Schristos if (strcmp(pw->pw_name, luser) != 0)
147ca32bd8dSchristos fail++;
148e4d43b82Schristos free(luser);
149ca32bd8dSchristos
150ca32bd8dSchristos /* end of message */
151e4d43b82Schristos if (sshbuf_len(b) != 0)
152ca32bd8dSchristos fail++;
153e4d43b82Schristos sshbuf_free(b);
154ca32bd8dSchristos
15517418e98Schristos debug3_f("fail %d", fail);
156ca32bd8dSchristos
157e160b4e8Schristos if (!fail) {
158a03ec00cSchristos if (ret != NULL) {
159ca32bd8dSchristos *ret = key;
160a03ec00cSchristos key = NULL;
161a03ec00cSchristos }
162a03ec00cSchristos if (pkalgp != NULL) {
163a03ec00cSchristos *pkalgp = pkalg;
164a03ec00cSchristos pkalg = NULL;
165a03ec00cSchristos }
166a03ec00cSchristos }
167a03ec00cSchristos sshkey_free(key);
168a03ec00cSchristos free(pkalg);
169a03ec00cSchristos free(pkblob);
170ca32bd8dSchristos
171ca32bd8dSchristos return (fail ? -1 : 0);
172ca32bd8dSchristos }
173ca32bd8dSchristos
174ca32bd8dSchristos int
main(int argc,char ** argv)175ca32bd8dSchristos main(int argc, char **argv)
176ca32bd8dSchristos {
177e4d43b82Schristos struct sshbuf *b;
178ca32bd8dSchristos Options options;
179ffae97bbSchristos #define NUM_KEYTYPES 5
180e4d43b82Schristos struct sshkey *keys[NUM_KEYTYPES], *key = NULL;
181ca32bd8dSchristos struct passwd *pw;
182e4d43b82Schristos int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
183e4d43b82Schristos u_char *signature, *data, rver;
184a03ec00cSchristos char *host, *fp, *pkalg;
185e4d43b82Schristos size_t slen, dlen;
186ca32bd8dSchristos
18779976551Schristos #ifdef __OpenBSD__
18879976551Schristos if (pledge("stdio rpath getpw dns id", NULL) != 0)
18979976551Schristos fatal("%s: pledge: %s", __progname, strerror(errno));
19079976551Schristos #endif
191313c6c94Schristos
192ca32bd8dSchristos /* Ensure that stdin and stdout are connected */
193ca32bd8dSchristos if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
194ca32bd8dSchristos exit(1);
195ca32bd8dSchristos /* Leave /dev/null fd iff it is attached to stderr */
196ca32bd8dSchristos if (fd > 2)
197ca32bd8dSchristos close(fd);
198ca32bd8dSchristos
199c5555919Schristos for (i = 0; i < NUM_KEYTYPES; i++)
200c5555919Schristos key_fd[i] = -1;
201c5555919Schristos
2026f47b660Schristos i = 0;
2038395c133Schristos /* XXX This really needs to read sshd_config for the paths */
204c5555919Schristos #ifdef WITH_DSA
2056f47b660Schristos key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
206c5555919Schristos #endif
2076f47b660Schristos key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY);
2088a4530f9Schristos key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY);
209ffae97bbSchristos key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY);
2106f47b660Schristos key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
211ca32bd8dSchristos
21255a4608bSchristos if ((pw = getpwuid(getuid())) == NULL)
213ca32bd8dSchristos fatal("getpwuid failed");
214ca32bd8dSchristos pw = pwcopy(pw);
215ca32bd8dSchristos
216ca32bd8dSchristos permanently_set_uid(pw);
217ca32bd8dSchristos
218ca32bd8dSchristos #ifdef DEBUG_SSH_KEYSIGN
219ca32bd8dSchristos log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
220ca32bd8dSchristos #endif
221ca32bd8dSchristos
222ca32bd8dSchristos /* verify that ssh-keysign is enabled by the admin */
223ca32bd8dSchristos initialize_options(&options);
224aa36fcacSchristos (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "",
225aa36fcacSchristos &options, 0, NULL);
22617418e98Schristos (void)fill_default_options(&options);
227ca32bd8dSchristos if (options.enable_ssh_keysign != 1)
228ca32bd8dSchristos fatal("ssh-keysign not enabled in %s",
229ca32bd8dSchristos _PATH_HOST_CONFIG_FILE);
230ca32bd8dSchristos
231a03ec00cSchristos #ifdef __OpenBSD__
232a03ec00cSchristos if (pledge("stdio dns", NULL) != 0)
233a03ec00cSchristos fatal("%s: pledge: %s", __progname, strerror(errno));
234a03ec00cSchristos #endif
235a03ec00cSchristos
2366f47b660Schristos for (i = found = 0; i < NUM_KEYTYPES; i++) {
2376f47b660Schristos if (key_fd[i] != -1)
2386f47b660Schristos found = 1;
2396f47b660Schristos }
2406f47b660Schristos if (found == 0)
241ca32bd8dSchristos fatal("could not open any host key");
242ca32bd8dSchristos
243cd4ada6aSchristos #ifdef WITH_OPENSSL
244185c8f97Schristos OpenSSL_add_all_algorithms();
245cd4ada6aSchristos #endif
246a03ec00cSchristos
247ca32bd8dSchristos found = 0;
2486f47b660Schristos for (i = 0; i < NUM_KEYTYPES; i++) {
249ca32bd8dSchristos keys[i] = NULL;
250ca32bd8dSchristos if (key_fd[i] == -1)
251ca32bd8dSchristos continue;
252e4d43b82Schristos r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC,
253e4d43b82Schristos NULL, &key, NULL);
254ca32bd8dSchristos close(key_fd[i]);
255e4d43b82Schristos if (r != 0)
25617418e98Schristos debug_r(r, "parse key %d", i);
257e4d43b82Schristos else if (key != NULL) {
258e4d43b82Schristos keys[i] = key;
259ca32bd8dSchristos found = 1;
260ca32bd8dSchristos }
261e4d43b82Schristos }
262ca32bd8dSchristos if (!found)
263ca32bd8dSchristos fatal("no hostkey found");
264ca32bd8dSchristos
265e4d43b82Schristos if ((b = sshbuf_new()) == NULL)
26679976551Schristos fatal("%s: sshbuf_new failed", __progname);
267e4d43b82Schristos if (ssh_msg_recv(STDIN_FILENO, b) < 0)
26817418e98Schristos fatal("%s: ssh_msg_recv failed", __progname);
269e4d43b82Schristos if ((r = sshbuf_get_u8(b, &rver)) != 0)
27017418e98Schristos fatal_r(r, "%s: buffer error", __progname);
271e4d43b82Schristos if (rver != version)
27217418e98Schristos fatal("%s: bad version: received %d, expected %d",
27317418e98Schristos __progname, rver, version);
274e4d43b82Schristos if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0)
27517418e98Schristos fatal_r(r, "%s: buffer error", __progname);
276*1c7715ddSchristos if (fd <= STDERR_FILENO)
27717418e98Schristos fatal("%s: bad fd = %d", __progname, fd);
278ca32bd8dSchristos if ((host = get_local_name(fd)) == NULL)
27917418e98Schristos fatal("%s: cannot get local name for fd", __progname);
280ca32bd8dSchristos
281e4d43b82Schristos if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
28217418e98Schristos fatal_r(r, "%s: buffer error", __progname);
283a03ec00cSchristos if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
28417418e98Schristos fatal("%s: not a valid request", __progname);
28500a838c4Schristos free(host);
286ca32bd8dSchristos
287ca32bd8dSchristos found = 0;
2886f47b660Schristos for (i = 0; i < NUM_KEYTYPES; i++) {
289ca32bd8dSchristos if (keys[i] != NULL &&
290e4d43b82Schristos sshkey_equal_public(key, keys[i])) {
291ca32bd8dSchristos found = 1;
292ca32bd8dSchristos break;
293ca32bd8dSchristos }
294ca32bd8dSchristos }
2958a4530f9Schristos if (!found) {
296e4d43b82Schristos if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
297e4d43b82Schristos SSH_FP_DEFAULT)) == NULL)
29879976551Schristos fatal("%s: sshkey_fingerprint failed", __progname);
29917418e98Schristos fatal("%s: no matching hostkey found for key %s %s", __progname,
300e4d43b82Schristos sshkey_type(key), fp ? fp : "");
3018a4530f9Schristos }
302ca32bd8dSchristos
303ed75d7a8Schristos if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
304a03ec00cSchristos pkalg, NULL, NULL, 0)) != 0)
30517418e98Schristos fatal_r(r, "%s: sshkey_sign failed", __progname);
30600a838c4Schristos free(data);
307ca32bd8dSchristos
308ca32bd8dSchristos /* send reply */
309e4d43b82Schristos sshbuf_reset(b);
310e4d43b82Schristos if ((r = sshbuf_put_string(b, signature, slen)) != 0)
31117418e98Schristos fatal_r(r, "%s: buffer error", __progname);
312e4d43b82Schristos if (ssh_msg_send(STDOUT_FILENO, version, b) == -1)
31317418e98Schristos fatal("%s: ssh_msg_send failed", __progname);
314ca32bd8dSchristos
315ca32bd8dSchristos return (0);
316ca32bd8dSchristos }
317