xref: /netbsd-src/crypto/external/bsd/openssh/dist/auth2-hostbased.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: auth2-hostbased.c,v 1.22 2022/10/05 22:39:36 christos Exp $	*/
2 /* $OpenBSD: auth2-hostbased.c,v 1.50 2022/09/17 10:34:29 djm Exp $ */
3 
4 /*
5  * Copyright (c) 2000 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: auth2-hostbased.c,v 1.22 2022/10/05 22:39:36 christos Exp $");
30 #include <sys/types.h>
31 
32 #include <stdlib.h>
33 #include <pwd.h>
34 #include <string.h>
35 #include <stdarg.h>
36 
37 #include "xmalloc.h"
38 #include "ssh2.h"
39 #include "packet.h"
40 #include "kex.h"
41 #include "sshbuf.h"
42 #include "log.h"
43 #include "misc.h"
44 #include "servconf.h"
45 #include "compat.h"
46 #include "sshkey.h"
47 #include "hostfile.h"
48 #include "auth.h"
49 #include "canohost.h"
50 #ifdef GSSAPI
51 #include "ssh-gss.h"
52 #endif
53 #include "monitor_wrap.h"
54 #include "pathnames.h"
55 #include "ssherr.h"
56 #include "match.h"
57 
58 /* import */
59 extern ServerOptions options;
60 
61 static int
62 userauth_hostbased(struct ssh *ssh, const char *method)
63 {
64 	Authctxt *authctxt = ssh->authctxt;
65 	struct sshbuf *b;
66 	struct sshkey *key = NULL;
67 	char *pkalg, *cuser, *chost;
68 	u_char *pkblob, *sig;
69 	size_t alen, blen, slen;
70 	int r, pktype, authenticated = 0;
71 
72 	/* XXX use sshkey_froms() */
73 	if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
74 	    (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
75 	    (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
76 	    (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
77 	    (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
78 		fatal_fr(r, "parse packet");
79 
80 	debug_f("cuser %s chost %s pkalg %s slen %zu",
81 	    cuser, chost, pkalg, slen);
82 #ifdef DEBUG_PK
83 	debug("signature:");
84 	sshbuf_dump_data(sig, slen, stderr);
85 #endif
86 	pktype = sshkey_type_from_name(pkalg);
87 	if (pktype == KEY_UNSPEC) {
88 		/* this is perfectly legal */
89 		logit_f("unsupported public key algorithm: %s",
90 		    pkalg);
91 		goto done;
92 	}
93 	if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
94 		error_fr(r, "key_from_blob");
95 		goto done;
96 	}
97 	if (key == NULL) {
98 		error_f("cannot decode key: %s", pkalg);
99 		goto done;
100 	}
101 	if (key->type != pktype) {
102 		error_f("type mismatch for decoded key "
103 		    "(received %d, expected %d)", key->type, pktype);
104 		goto done;
105 	}
106 	if (sshkey_type_plain(key->type) == KEY_RSA &&
107 	    (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
108 		error("Refusing RSA key because peer uses unsafe "
109 		    "signature format");
110 		goto done;
111 	}
112 	if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
113 		logit_f("signature algorithm %s not in "
114 		    "HostbasedAcceptedAlgorithms", pkalg);
115 		goto done;
116 	}
117 	if ((r = sshkey_check_cert_sigtype(key,
118 	    options.ca_sign_algorithms)) != 0) {
119 		logit_fr(r, "certificate signature algorithm %s",
120 		    (key->cert == NULL || key->cert->signature_type == NULL) ?
121 		    "(null)" : key->cert->signature_type);
122 		goto done;
123 	}
124 	if ((r = sshkey_check_rsa_length(key,
125 	    options.required_rsa_size)) != 0) {
126 		logit_r(r, "refusing %s key", sshkey_type(key));
127 		goto done;
128 	}
129 
130 	if (!authctxt->valid || authctxt->user == NULL) {
131 		debug2_f("disabled because of invalid user");
132 		goto done;
133 	}
134 
135 	if ((b = sshbuf_new()) == NULL)
136 		fatal_f("sshbuf_new failed");
137 	/* reconstruct packet */
138 	if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
139 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
140 	    (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
141 	    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
142 	    (r = sshbuf_put_cstring(b, method)) != 0 ||
143 	    (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
144 	    (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
145 	    (r = sshbuf_put_cstring(b, chost)) != 0 ||
146 	    (r = sshbuf_put_cstring(b, cuser)) != 0)
147 		fatal_fr(r, "reconstruct packet");
148 #ifdef DEBUG_PK
149 	sshbuf_dump(b, stderr);
150 #endif
151 
152 	auth2_record_info(authctxt,
153 	    "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
154 
155 	/* test for allowed key and correct signature */
156 	authenticated = 0;
157 	if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
158 	    chost, key)) &&
159 	    PRIVSEP(sshkey_verify(key, sig, slen,
160 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
161 		authenticated = 1;
162 
163 	auth2_record_key(authctxt, authenticated, key);
164 	sshbuf_free(b);
165 done:
166 	debug2_f("authenticated %d", authenticated);
167 	sshkey_free(key);
168 	free(pkalg);
169 	free(pkblob);
170 	free(cuser);
171 	free(chost);
172 	free(sig);
173 	return authenticated;
174 }
175 
176 /* return 1 if given hostkey is allowed */
177 int
178 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
179     const char *cuser, char *chost, struct sshkey *key)
180 {
181 	const char *resolvedname, *ipaddr, *lookup, *reason;
182 	HostStatus host_status;
183 	int len;
184 	char *fp;
185 
186 	if (auth_key_is_revoked(key))
187 		return 0;
188 
189 	resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
190 	ipaddr = ssh_remote_ipaddr(ssh);
191 
192 	debug2_f("chost %s resolvedname %s ipaddr %s",
193 	    chost, resolvedname, ipaddr);
194 
195 	if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
196 		debug2("stripping trailing dot from chost %s", chost);
197 		chost[len - 1] = '\0';
198 	}
199 
200 	if (options.hostbased_uses_name_from_packet_only) {
201 		if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
202 			debug2_f("auth_rhosts2 refused user \"%.100s\" "
203 			    "host \"%.100s\" (from packet)", cuser, chost);
204 			return 0;
205 		}
206 		lookup = chost;
207 	} else {
208 		if (strcasecmp(resolvedname, chost) != 0)
209 			logit("userauth_hostbased mismatch: "
210 			    "client sends %s, but we resolve %s to %s",
211 			    chost, ipaddr, resolvedname);
212 		if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
213 			debug2_f("auth_rhosts2 refused "
214 			    "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
215 			    cuser, resolvedname, ipaddr);
216 			return 0;
217 		}
218 		lookup = resolvedname;
219 	}
220 	debug2_f("access allowed by auth_rhosts2");
221 
222 	if (sshkey_is_cert(key) &&
223 	    sshkey_cert_check_authority_now(key, 1, 0, 0, lookup, &reason)) {
224 		error("%s", reason);
225 		auth_debug_add("%s", reason);
226 		return 0;
227 	}
228 
229 	host_status = check_key_in_hostfiles(pw, key, lookup,
230 	    _PATH_SSH_SYSTEM_HOSTFILE,
231 	    options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
232 
233 	/* backward compat if no key has been found. */
234 	if (host_status == HOST_NEW) {
235 		host_status = check_key_in_hostfiles(pw, key, lookup,
236 		    _PATH_SSH_SYSTEM_HOSTFILE2,
237 		    options.ignore_user_known_hosts ? NULL :
238 		    _PATH_SSH_USER_HOSTFILE2);
239 	}
240 
241 	if (host_status == HOST_OK) {
242 		if (sshkey_is_cert(key)) {
243 			if ((fp = sshkey_fingerprint(key->cert->signature_key,
244 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
245 				fatal_f("sshkey_fingerprint fail");
246 			verbose("Accepted certificate ID \"%s\" signed by "
247 			    "%s CA %s from %s@%s", key->cert->key_id,
248 			    sshkey_type(key->cert->signature_key), fp,
249 			    cuser, lookup);
250 		} else {
251 			if ((fp = sshkey_fingerprint(key,
252 			    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
253 				fatal_f("sshkey_fingerprint fail");
254 			verbose("Accepted %s public key %s from %s@%s",
255 			    sshkey_type(key), fp, cuser, lookup);
256 		}
257 		free(fp);
258 	}
259 
260 	return (host_status == HOST_OK);
261 }
262 
263 Authmethod method_hostbased = {
264 	"hostbased",
265 	NULL,
266 	userauth_hostbased,
267 	&options.hostbased_authentication
268 };
269