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