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