xref: /netbsd-src/crypto/external/bsd/openssh/dist/auth2-hostbased.c (revision 1c7715dda22cf2bd169e2f84953c050393e8fe9c)
1*1c7715ddSchristos /*	$NetBSD: auth2-hostbased.c,v 1.24 2024/07/08 22:33:43 christos Exp $	*/
2*1c7715ddSchristos /* $OpenBSD: auth2-hostbased.c,v 1.53 2024/05/17 00:30:23 djm Exp $ */
3*1c7715ddSchristos 
4ca32bd8dSchristos /*
5ca32bd8dSchristos  * Copyright (c) 2000 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: auth2-hostbased.c,v 1.24 2024/07/08 22:33:43 christos Exp $");
30ca32bd8dSchristos #include <sys/types.h>
31ca32bd8dSchristos 
32cd4ada6aSchristos #include <stdlib.h>
33ca32bd8dSchristos #include <pwd.h>
34ca32bd8dSchristos #include <string.h>
35ca32bd8dSchristos #include <stdarg.h>
36ca32bd8dSchristos 
37ca32bd8dSchristos #include "xmalloc.h"
38ca32bd8dSchristos #include "ssh2.h"
39ca32bd8dSchristos #include "packet.h"
4017418e98Schristos #include "kex.h"
4155a4608bSchristos #include "sshbuf.h"
42ca32bd8dSchristos #include "log.h"
438a4530f9Schristos #include "misc.h"
44ca32bd8dSchristos #include "servconf.h"
457a183406Schristos #include "sshkey.h"
46ca32bd8dSchristos #include "hostfile.h"
47ca32bd8dSchristos #include "auth.h"
48ca32bd8dSchristos #include "canohost.h"
49ca32bd8dSchristos #ifdef GSSAPI
50ca32bd8dSchristos #include "ssh-gss.h"
51ca32bd8dSchristos #endif
52ca32bd8dSchristos #include "monitor_wrap.h"
53ca32bd8dSchristos #include "pathnames.h"
547a183406Schristos #include "ssherr.h"
55e4d43b82Schristos #include "match.h"
56ca32bd8dSchristos 
57ca32bd8dSchristos /* import */
58ca32bd8dSchristos extern ServerOptions options;
59*1c7715ddSchristos extern struct authmethod_cfg methodcfg_hostbased;
60ca32bd8dSchristos 
61ca32bd8dSchristos static int
userauth_hostbased(struct ssh * ssh,const char * method)62a03ec00cSchristos userauth_hostbased(struct ssh *ssh, const char *method)
63ca32bd8dSchristos {
647a183406Schristos 	Authctxt *authctxt = ssh->authctxt;
657a183406Schristos 	struct sshbuf *b;
667a183406Schristos 	struct sshkey *key = NULL;
67ffae97bbSchristos 	char *pkalg, *cuser, *chost;
68ca32bd8dSchristos 	u_char *pkblob, *sig;
697a183406Schristos 	size_t alen, blen, slen;
707a183406Schristos 	int r, pktype, authenticated = 0;
71ca32bd8dSchristos 
727a183406Schristos 	/* XXX use sshkey_froms() */
737a183406Schristos 	if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
747a183406Schristos 	    (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
757a183406Schristos 	    (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
767a183406Schristos 	    (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
777a183406Schristos 	    (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
7817418e98Schristos 		fatal_fr(r, "parse packet");
79ca32bd8dSchristos 
8017418e98Schristos 	debug_f("cuser %s chost %s pkalg %s slen %zu",
81ca32bd8dSchristos 	    cuser, chost, pkalg, slen);
82ca32bd8dSchristos #ifdef DEBUG_PK
83ca32bd8dSchristos 	debug("signature:");
84aa36fcacSchristos 	sshbuf_dump_data(sig, slen, stderr);
85ca32bd8dSchristos #endif
867a183406Schristos 	pktype = sshkey_type_from_name(pkalg);
87ca32bd8dSchristos 	if (pktype == KEY_UNSPEC) {
88ca32bd8dSchristos 		/* this is perfectly legal */
8917418e98Schristos 		logit_f("unsupported public key algorithm: %s",
9017418e98Schristos 		    pkalg);
91ca32bd8dSchristos 		goto done;
92ca32bd8dSchristos 	}
937a183406Schristos 	if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
9417418e98Schristos 		error_fr(r, "key_from_blob");
957a183406Schristos 		goto done;
967a183406Schristos 	}
97ca32bd8dSchristos 	if (key == NULL) {
9817418e98Schristos 		error_f("cannot decode key: %s", pkalg);
99ca32bd8dSchristos 		goto done;
100ca32bd8dSchristos 	}
101ca32bd8dSchristos 	if (key->type != pktype) {
10217418e98Schristos 		error_f("type mismatch for decoded key "
10317418e98Schristos 		    "(received %d, expected %d)", key->type, pktype);
104ca32bd8dSchristos 		goto done;
105ca32bd8dSchristos 	}
10617418e98Schristos 	if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
107a03ec00cSchristos 		logit_f("signature algorithm %s not in "
108a03ec00cSchristos 		    "HostbasedAcceptedAlgorithms", pkalg);
109e4d43b82Schristos 		goto done;
110e4d43b82Schristos 	}
111aa36fcacSchristos 	if ((r = sshkey_check_cert_sigtype(key,
112aa36fcacSchristos 	    options.ca_sign_algorithms)) != 0) {
11317418e98Schristos 		logit_fr(r, "certificate signature algorithm %s",
114aa36fcacSchristos 		    (key->cert == NULL || key->cert->signature_type == NULL) ?
11517418e98Schristos 		    "(null)" : key->cert->signature_type);
116aa36fcacSchristos 		goto done;
117aa36fcacSchristos 	}
118e160b4e8Schristos 	if ((r = sshkey_check_rsa_length(key,
119e160b4e8Schristos 	    options.required_rsa_size)) != 0) {
120e160b4e8Schristos 		logit_r(r, "refusing %s key", sshkey_type(key));
121e160b4e8Schristos 		goto done;
122e160b4e8Schristos 	}
123e4d43b82Schristos 
12455a4608bSchristos 	if (!authctxt->valid || authctxt->user == NULL) {
12517418e98Schristos 		debug2_f("disabled because of invalid user");
12655a4608bSchristos 		goto done;
12755a4608bSchristos 	}
12855a4608bSchristos 
1297a183406Schristos 	if ((b = sshbuf_new()) == NULL)
13017418e98Schristos 		fatal_f("sshbuf_new failed");
131ca32bd8dSchristos 	/* reconstruct packet */
13217418e98Schristos 	if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
1337a183406Schristos 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1347a183406Schristos 	    (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
135ffae97bbSchristos 	    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
136a03ec00cSchristos 	    (r = sshbuf_put_cstring(b, method)) != 0 ||
1377a183406Schristos 	    (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
1387a183406Schristos 	    (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
1397a183406Schristos 	    (r = sshbuf_put_cstring(b, chost)) != 0 ||
1407a183406Schristos 	    (r = sshbuf_put_cstring(b, cuser)) != 0)
14117418e98Schristos 		fatal_fr(r, "reconstruct packet");
142ca32bd8dSchristos #ifdef DEBUG_PK
1437a183406Schristos 	sshbuf_dump(b, stderr);
144ca32bd8dSchristos #endif
14500a838c4Schristos 
1467a183406Schristos 	auth2_record_info(authctxt,
14700a838c4Schristos 	    "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
14800a838c4Schristos 
149ca32bd8dSchristos 	/* test for allowed key and correct signature */
150ca32bd8dSchristos 	authenticated = 0;
151*1c7715ddSchristos 	if (mm_hostbased_key_allowed(ssh, authctxt->pw, cuser,
152*1c7715ddSchristos 	    chost, key) &&
153*1c7715ddSchristos 	    mm_sshkey_verify(key, sig, slen,
154*1c7715ddSchristos 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL) == 0)
155ca32bd8dSchristos 		authenticated = 1;
156ca32bd8dSchristos 
1577a183406Schristos 	auth2_record_key(authctxt, authenticated, key);
1587a183406Schristos 	sshbuf_free(b);
159ca32bd8dSchristos done:
16017418e98Schristos 	debug2_f("authenticated %d", authenticated);
1617a183406Schristos 	sshkey_free(key);
16200a838c4Schristos 	free(pkalg);
16300a838c4Schristos 	free(pkblob);
16400a838c4Schristos 	free(cuser);
16500a838c4Schristos 	free(chost);
16600a838c4Schristos 	free(sig);
167ca32bd8dSchristos 	return authenticated;
168ca32bd8dSchristos }
169ca32bd8dSchristos 
170ca32bd8dSchristos /* return 1 if given hostkey is allowed */
171ca32bd8dSchristos int
hostbased_key_allowed(struct ssh * ssh,struct passwd * pw,const char * cuser,char * chost,struct sshkey * key)172aa36fcacSchristos hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
173aa36fcacSchristos     const char *cuser, char *chost, struct sshkey *key)
174ca32bd8dSchristos {
17534b27b53Sadam 	const char *resolvedname, *ipaddr, *lookup, *reason;
176ca32bd8dSchristos 	HostStatus host_status;
177ca32bd8dSchristos 	int len;
17834b27b53Sadam 	char *fp;
17934b27b53Sadam 
18034b27b53Sadam 	if (auth_key_is_revoked(key))
18134b27b53Sadam 		return 0;
182ca32bd8dSchristos 
1835101d403Schristos 	resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
1845101d403Schristos 	ipaddr = ssh_remote_ipaddr(ssh);
185ca32bd8dSchristos 
18617418e98Schristos 	debug2_f("chost %s resolvedname %s ipaddr %s",
187ca32bd8dSchristos 	    chost, resolvedname, ipaddr);
188ca32bd8dSchristos 
189ca32bd8dSchristos 	if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
190ca32bd8dSchristos 		debug2("stripping trailing dot from chost %s", chost);
191ca32bd8dSchristos 		chost[len - 1] = '\0';
192ca32bd8dSchristos 	}
193ca32bd8dSchristos 
194ca32bd8dSchristos 	if (options.hostbased_uses_name_from_packet_only) {
195e4d43b82Schristos 		if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
19617418e98Schristos 			debug2_f("auth_rhosts2 refused user \"%.100s\" "
19717418e98Schristos 			    "host \"%.100s\" (from packet)", cuser, chost);
198ca32bd8dSchristos 			return 0;
199e4d43b82Schristos 		}
200ca32bd8dSchristos 		lookup = chost;
201ca32bd8dSchristos 	} else {
202ca32bd8dSchristos 		if (strcasecmp(resolvedname, chost) != 0)
203ca32bd8dSchristos 			logit("userauth_hostbased mismatch: "
204ca32bd8dSchristos 			    "client sends %s, but we resolve %s to %s",
205ca32bd8dSchristos 			    chost, ipaddr, resolvedname);
206e4d43b82Schristos 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
20717418e98Schristos 			debug2_f("auth_rhosts2 refused "
208e4d43b82Schristos 			    "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
20917418e98Schristos 			    cuser, resolvedname, ipaddr);
210ca32bd8dSchristos 			return 0;
211e4d43b82Schristos 		}
212ca32bd8dSchristos 		lookup = resolvedname;
213ca32bd8dSchristos 	}
21417418e98Schristos 	debug2_f("access allowed by auth_rhosts2");
215ca32bd8dSchristos 
2167a183406Schristos 	if (sshkey_is_cert(key) &&
217b592f463Schristos 	    sshkey_cert_check_authority_now(key, 1, 0, 0, lookup, &reason)) {
21834b27b53Sadam 		error("%s", reason);
21934b27b53Sadam 		auth_debug_add("%s", reason);
22034b27b53Sadam 		return 0;
22134b27b53Sadam 	}
22234b27b53Sadam 
223ca32bd8dSchristos 	host_status = check_key_in_hostfiles(pw, key, lookup,
224ca32bd8dSchristos 	    _PATH_SSH_SYSTEM_HOSTFILE,
225ca32bd8dSchristos 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
226ca32bd8dSchristos 
227ca32bd8dSchristos 	/* backward compat if no key has been found. */
22834b27b53Sadam 	if (host_status == HOST_NEW) {
229ca32bd8dSchristos 		host_status = check_key_in_hostfiles(pw, key, lookup,
230ca32bd8dSchristos 		    _PATH_SSH_SYSTEM_HOSTFILE2,
231ca32bd8dSchristos 		    options.ignore_user_known_hosts ? NULL :
232ca32bd8dSchristos 		    _PATH_SSH_USER_HOSTFILE2);
23334b27b53Sadam 	}
23434b27b53Sadam 
23534b27b53Sadam 	if (host_status == HOST_OK) {
2367a183406Schristos 		if (sshkey_is_cert(key)) {
237e4d43b82Schristos 			if ((fp = sshkey_fingerprint(key->cert->signature_key,
238e4d43b82Schristos 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
23917418e98Schristos 				fatal_f("sshkey_fingerprint fail");
24034b27b53Sadam 			verbose("Accepted certificate ID \"%s\" signed by "
24134b27b53Sadam 			    "%s CA %s from %s@%s", key->cert->key_id,
2427a183406Schristos 			    sshkey_type(key->cert->signature_key), fp,
24334b27b53Sadam 			    cuser, lookup);
24434b27b53Sadam 		} else {
245e4d43b82Schristos 			if ((fp = sshkey_fingerprint(key,
246e4d43b82Schristos 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
24717418e98Schristos 				fatal_f("sshkey_fingerprint fail");
24834b27b53Sadam 			verbose("Accepted %s public key %s from %s@%s",
2497a183406Schristos 			    sshkey_type(key), fp, cuser, lookup);
25034b27b53Sadam 		}
25100a838c4Schristos 		free(fp);
25234b27b53Sadam 	}
253ca32bd8dSchristos 
254ca32bd8dSchristos 	return (host_status == HOST_OK);
255ca32bd8dSchristos }
256ca32bd8dSchristos 
257ca32bd8dSchristos Authmethod method_hostbased = {
258*1c7715ddSchristos 	&methodcfg_hostbased,
259ca32bd8dSchristos 	userauth_hostbased,
260ca32bd8dSchristos };
261