1*ba1276acSMatthew Dillon /* $OpenBSD: auth2-hostbased.c,v 1.53 2024/05/17 00:30:23 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos * Copyright (c) 2000 Markus Friedl. All rights reserved.
418de8d7fSPeter Avalos *
518de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without
618de8d7fSPeter Avalos * modification, are permitted provided that the following conditions
718de8d7fSPeter Avalos * are met:
818de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright
918de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer.
1018de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1118de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the
1218de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution.
1318de8d7fSPeter Avalos *
1418de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1518de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1618de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1718de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1818de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1918de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2018de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2118de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2218de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2318de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2418de8d7fSPeter Avalos */
2518de8d7fSPeter Avalos
2618de8d7fSPeter Avalos #include "includes.h"
2718de8d7fSPeter Avalos
2818de8d7fSPeter Avalos #include <sys/types.h>
2918de8d7fSPeter Avalos
300cbfa66cSDaniel Fojt #include <stdlib.h>
3118de8d7fSPeter Avalos #include <pwd.h>
3218de8d7fSPeter Avalos #include <string.h>
3318de8d7fSPeter Avalos #include <stdarg.h>
3418de8d7fSPeter Avalos
3518de8d7fSPeter Avalos #include "xmalloc.h"
3618de8d7fSPeter Avalos #include "ssh2.h"
3718de8d7fSPeter Avalos #include "packet.h"
3850a69bb5SSascha Wildner #include "kex.h"
39664f4763Szrj #include "sshbuf.h"
4018de8d7fSPeter Avalos #include "log.h"
4136e94dc5SPeter Avalos #include "misc.h"
4218de8d7fSPeter Avalos #include "servconf.h"
43ce74bacaSMatthew Dillon #include "sshkey.h"
4418de8d7fSPeter Avalos #include "hostfile.h"
4518de8d7fSPeter Avalos #include "auth.h"
4618de8d7fSPeter Avalos #include "canohost.h"
4718de8d7fSPeter Avalos #ifdef GSSAPI
4818de8d7fSPeter Avalos #include "ssh-gss.h"
4918de8d7fSPeter Avalos #endif
5018de8d7fSPeter Avalos #include "monitor_wrap.h"
5118de8d7fSPeter Avalos #include "pathnames.h"
52ce74bacaSMatthew Dillon #include "ssherr.h"
53e9778795SPeter Avalos #include "match.h"
5418de8d7fSPeter Avalos
5518de8d7fSPeter Avalos /* import */
5618de8d7fSPeter Avalos extern ServerOptions options;
57*ba1276acSMatthew Dillon extern struct authmethod_cfg methodcfg_hostbased;
5818de8d7fSPeter Avalos
5918de8d7fSPeter Avalos static int
userauth_hostbased(struct ssh * ssh,const char * method)60ee116499SAntonio Huete Jimenez userauth_hostbased(struct ssh *ssh, const char *method)
6118de8d7fSPeter Avalos {
62ce74bacaSMatthew Dillon Authctxt *authctxt = ssh->authctxt;
63ce74bacaSMatthew Dillon struct sshbuf *b;
64ce74bacaSMatthew Dillon struct sshkey *key = NULL;
65664f4763Szrj char *pkalg, *cuser, *chost;
6618de8d7fSPeter Avalos u_char *pkblob, *sig;
67ce74bacaSMatthew Dillon size_t alen, blen, slen;
68ce74bacaSMatthew Dillon int r, pktype, authenticated = 0;
6918de8d7fSPeter Avalos
70ce74bacaSMatthew Dillon /* XXX use sshkey_froms() */
71ce74bacaSMatthew Dillon if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
72ce74bacaSMatthew Dillon (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
73ce74bacaSMatthew Dillon (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
74ce74bacaSMatthew Dillon (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
75ce74bacaSMatthew Dillon (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
7650a69bb5SSascha Wildner fatal_fr(r, "parse packet");
7718de8d7fSPeter Avalos
7850a69bb5SSascha Wildner debug_f("cuser %s chost %s pkalg %s slen %zu",
7918de8d7fSPeter Avalos cuser, chost, pkalg, slen);
8018de8d7fSPeter Avalos #ifdef DEBUG_PK
8118de8d7fSPeter Avalos debug("signature:");
82664f4763Szrj sshbuf_dump_data(sig, slen, stderr);
8318de8d7fSPeter Avalos #endif
84ce74bacaSMatthew Dillon pktype = sshkey_type_from_name(pkalg);
8518de8d7fSPeter Avalos if (pktype == KEY_UNSPEC) {
8618de8d7fSPeter Avalos /* this is perfectly legal */
8750a69bb5SSascha Wildner logit_f("unsupported public key algorithm: %s",
8850a69bb5SSascha Wildner pkalg);
8918de8d7fSPeter Avalos goto done;
9018de8d7fSPeter Avalos }
91ce74bacaSMatthew Dillon if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
9250a69bb5SSascha Wildner error_fr(r, "key_from_blob");
93ce74bacaSMatthew Dillon goto done;
94ce74bacaSMatthew Dillon }
9518de8d7fSPeter Avalos if (key == NULL) {
9650a69bb5SSascha Wildner error_f("cannot decode key: %s", pkalg);
9718de8d7fSPeter Avalos goto done;
9818de8d7fSPeter Avalos }
9918de8d7fSPeter Avalos if (key->type != pktype) {
10050a69bb5SSascha Wildner error_f("type mismatch for decoded key "
10150a69bb5SSascha Wildner "(received %d, expected %d)", key->type, pktype);
10218de8d7fSPeter Avalos goto done;
10318de8d7fSPeter Avalos }
10450a69bb5SSascha Wildner if (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
105ee116499SAntonio Huete Jimenez logit_f("signature algorithm %s not in "
106ee116499SAntonio Huete Jimenez "HostbasedAcceptedAlgorithms", pkalg);
107e9778795SPeter Avalos goto done;
108e9778795SPeter Avalos }
109664f4763Szrj if ((r = sshkey_check_cert_sigtype(key,
110664f4763Szrj options.ca_sign_algorithms)) != 0) {
11150a69bb5SSascha Wildner logit_fr(r, "certificate signature algorithm %s",
112664f4763Szrj (key->cert == NULL || key->cert->signature_type == NULL) ?
11350a69bb5SSascha Wildner "(null)" : key->cert->signature_type);
114664f4763Szrj goto done;
115664f4763Szrj }
116ee116499SAntonio Huete Jimenez if ((r = sshkey_check_rsa_length(key,
117ee116499SAntonio Huete Jimenez options.required_rsa_size)) != 0) {
118ee116499SAntonio Huete Jimenez logit_r(r, "refusing %s key", sshkey_type(key));
119ee116499SAntonio Huete Jimenez goto done;
120ee116499SAntonio Huete Jimenez }
121e9778795SPeter Avalos
122664f4763Szrj if (!authctxt->valid || authctxt->user == NULL) {
12350a69bb5SSascha Wildner debug2_f("disabled because of invalid user");
124664f4763Szrj goto done;
125664f4763Szrj }
126664f4763Szrj
127ce74bacaSMatthew Dillon if ((b = sshbuf_new()) == NULL)
12850a69bb5SSascha Wildner fatal_f("sshbuf_new failed");
12918de8d7fSPeter Avalos /* reconstruct packet */
13050a69bb5SSascha Wildner if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
131ce74bacaSMatthew Dillon (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
132ce74bacaSMatthew Dillon (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
133664f4763Szrj (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
134ee116499SAntonio Huete Jimenez (r = sshbuf_put_cstring(b, method)) != 0 ||
135ce74bacaSMatthew Dillon (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
136ce74bacaSMatthew Dillon (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
137ce74bacaSMatthew Dillon (r = sshbuf_put_cstring(b, chost)) != 0 ||
138ce74bacaSMatthew Dillon (r = sshbuf_put_cstring(b, cuser)) != 0)
13950a69bb5SSascha Wildner fatal_fr(r, "reconstruct packet");
14018de8d7fSPeter Avalos #ifdef DEBUG_PK
141ce74bacaSMatthew Dillon sshbuf_dump(b, stderr);
14218de8d7fSPeter Avalos #endif
14336e94dc5SPeter Avalos
144ce74bacaSMatthew Dillon auth2_record_info(authctxt,
14536e94dc5SPeter Avalos "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
14636e94dc5SPeter Avalos
14718de8d7fSPeter Avalos /* test for allowed key and correct signature */
14818de8d7fSPeter Avalos authenticated = 0;
149*ba1276acSMatthew Dillon if (mm_hostbased_key_allowed(ssh, authctxt->pw, cuser,
150*ba1276acSMatthew Dillon chost, key) &&
151*ba1276acSMatthew Dillon mm_sshkey_verify(key, sig, slen,
152*ba1276acSMatthew Dillon sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL) == 0)
15318de8d7fSPeter Avalos authenticated = 1;
15418de8d7fSPeter Avalos
155ce74bacaSMatthew Dillon auth2_record_key(authctxt, authenticated, key);
156ce74bacaSMatthew Dillon sshbuf_free(b);
15718de8d7fSPeter Avalos done:
15850a69bb5SSascha Wildner debug2_f("authenticated %d", authenticated);
159ce74bacaSMatthew Dillon sshkey_free(key);
16036e94dc5SPeter Avalos free(pkalg);
16136e94dc5SPeter Avalos free(pkblob);
16236e94dc5SPeter Avalos free(cuser);
16336e94dc5SPeter Avalos free(chost);
16436e94dc5SPeter Avalos free(sig);
16518de8d7fSPeter Avalos return authenticated;
16618de8d7fSPeter Avalos }
16718de8d7fSPeter Avalos
16818de8d7fSPeter Avalos /* return 1 if given hostkey is allowed */
16918de8d7fSPeter Avalos int
hostbased_key_allowed(struct ssh * ssh,struct passwd * pw,const char * cuser,char * chost,struct sshkey * key)170664f4763Szrj hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
171664f4763Szrj const char *cuser, char *chost, struct sshkey *key)
17218de8d7fSPeter Avalos {
173856ea928SPeter Avalos const char *resolvedname, *ipaddr, *lookup, *reason;
17418de8d7fSPeter Avalos HostStatus host_status;
17518de8d7fSPeter Avalos int len;
176856ea928SPeter Avalos char *fp;
177856ea928SPeter Avalos
178856ea928SPeter Avalos if (auth_key_is_revoked(key))
179856ea928SPeter Avalos return 0;
18018de8d7fSPeter Avalos
181e9778795SPeter Avalos resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
182e9778795SPeter Avalos ipaddr = ssh_remote_ipaddr(ssh);
18318de8d7fSPeter Avalos
18450a69bb5SSascha Wildner debug2_f("chost %s resolvedname %s ipaddr %s",
18518de8d7fSPeter Avalos chost, resolvedname, ipaddr);
18618de8d7fSPeter Avalos
18718de8d7fSPeter Avalos if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
18818de8d7fSPeter Avalos debug2("stripping trailing dot from chost %s", chost);
18918de8d7fSPeter Avalos chost[len - 1] = '\0';
19018de8d7fSPeter Avalos }
19118de8d7fSPeter Avalos
19218de8d7fSPeter Avalos if (options.hostbased_uses_name_from_packet_only) {
193e9778795SPeter Avalos if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
19450a69bb5SSascha Wildner debug2_f("auth_rhosts2 refused user \"%.100s\" "
19550a69bb5SSascha Wildner "host \"%.100s\" (from packet)", cuser, chost);
19618de8d7fSPeter Avalos return 0;
197e9778795SPeter Avalos }
19818de8d7fSPeter Avalos lookup = chost;
19918de8d7fSPeter Avalos } else {
20018de8d7fSPeter Avalos if (strcasecmp(resolvedname, chost) != 0)
20118de8d7fSPeter Avalos logit("userauth_hostbased mismatch: "
20218de8d7fSPeter Avalos "client sends %s, but we resolve %s to %s",
20318de8d7fSPeter Avalos chost, ipaddr, resolvedname);
204e9778795SPeter Avalos if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
20550a69bb5SSascha Wildner debug2_f("auth_rhosts2 refused "
206e9778795SPeter Avalos "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
20750a69bb5SSascha Wildner cuser, resolvedname, ipaddr);
20818de8d7fSPeter Avalos return 0;
209e9778795SPeter Avalos }
21018de8d7fSPeter Avalos lookup = resolvedname;
21118de8d7fSPeter Avalos }
21250a69bb5SSascha Wildner debug2_f("access allowed by auth_rhosts2");
21318de8d7fSPeter Avalos
214ce74bacaSMatthew Dillon if (sshkey_is_cert(key) &&
21550a69bb5SSascha Wildner sshkey_cert_check_authority_now(key, 1, 0, 0, lookup, &reason)) {
216856ea928SPeter Avalos error("%s", reason);
217856ea928SPeter Avalos auth_debug_add("%s", reason);
218856ea928SPeter Avalos return 0;
219856ea928SPeter Avalos }
220856ea928SPeter Avalos
22118de8d7fSPeter Avalos host_status = check_key_in_hostfiles(pw, key, lookup,
22218de8d7fSPeter Avalos _PATH_SSH_SYSTEM_HOSTFILE,
22318de8d7fSPeter Avalos options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
22418de8d7fSPeter Avalos
22518de8d7fSPeter Avalos /* backward compat if no key has been found. */
226856ea928SPeter Avalos if (host_status == HOST_NEW) {
22718de8d7fSPeter Avalos host_status = check_key_in_hostfiles(pw, key, lookup,
22818de8d7fSPeter Avalos _PATH_SSH_SYSTEM_HOSTFILE2,
22918de8d7fSPeter Avalos options.ignore_user_known_hosts ? NULL :
23018de8d7fSPeter Avalos _PATH_SSH_USER_HOSTFILE2);
231856ea928SPeter Avalos }
232856ea928SPeter Avalos
233856ea928SPeter Avalos if (host_status == HOST_OK) {
234ce74bacaSMatthew Dillon if (sshkey_is_cert(key)) {
235e9778795SPeter Avalos if ((fp = sshkey_fingerprint(key->cert->signature_key,
236e9778795SPeter Avalos options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
23750a69bb5SSascha Wildner fatal_f("sshkey_fingerprint fail");
238856ea928SPeter Avalos verbose("Accepted certificate ID \"%s\" signed by "
239856ea928SPeter Avalos "%s CA %s from %s@%s", key->cert->key_id,
240ce74bacaSMatthew Dillon sshkey_type(key->cert->signature_key), fp,
241856ea928SPeter Avalos cuser, lookup);
242856ea928SPeter Avalos } else {
243e9778795SPeter Avalos if ((fp = sshkey_fingerprint(key,
244e9778795SPeter Avalos options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
24550a69bb5SSascha Wildner fatal_f("sshkey_fingerprint fail");
246856ea928SPeter Avalos verbose("Accepted %s public key %s from %s@%s",
247ce74bacaSMatthew Dillon sshkey_type(key), fp, cuser, lookup);
248856ea928SPeter Avalos }
24936e94dc5SPeter Avalos free(fp);
250856ea928SPeter Avalos }
25118de8d7fSPeter Avalos
25218de8d7fSPeter Avalos return (host_status == HOST_OK);
25318de8d7fSPeter Avalos }
25418de8d7fSPeter Avalos
25518de8d7fSPeter Avalos Authmethod method_hostbased = {
256*ba1276acSMatthew Dillon &methodcfg_hostbased,
25718de8d7fSPeter Avalos userauth_hostbased,
25818de8d7fSPeter Avalos };
259