jcs's openbsd hax
openbsd
1/* $OpenBSD: auth2-hostbased.c,v 1.56 2025/12/22 01:49:03 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 "sshkey.h"
43#include "hostfile.h"
44#include "auth.h"
45#include "canohost.h"
46#ifdef GSSAPI
47#include "ssh-gss.h"
48#endif
49#include "monitor_wrap.h"
50#include "pathnames.h"
51#include "ssherr.h"
52#include "match.h"
53
54/* import */
55extern ServerOptions options;
56extern struct authmethod_cfg methodcfg_hostbased;
57
58static int
59userauth_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 (match_pattern_list(pkalg, options.hostbased_accepted_algos, 0) != 1) {
104 logit_f("signature algorithm %s not in "
105 "HostbasedAcceptedAlgorithms", pkalg);
106 goto done;
107 }
108 if ((r = sshkey_check_cert_sigtype(key,
109 options.ca_sign_algorithms)) != 0) {
110 logit_fr(r, "certificate signature algorithm %s",
111 (key->cert == NULL || key->cert->signature_type == NULL) ?
112 "(null)" : key->cert->signature_type);
113 goto done;
114 }
115 if ((r = sshkey_check_rsa_length(key,
116 options.required_rsa_size)) != 0) {
117 logit_r(r, "refusing %s key", sshkey_type(key));
118 goto done;
119 }
120
121 if (!authctxt->valid || authctxt->user == NULL) {
122 debug2_f("disabled because of invalid user");
123 goto done;
124 }
125
126 if ((b = sshbuf_new()) == NULL)
127 fatal_f("sshbuf_new failed");
128 /* reconstruct packet */
129 if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
130 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
131 (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
132 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
133 (r = sshbuf_put_cstring(b, method)) != 0 ||
134 (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
135 (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
136 (r = sshbuf_put_cstring(b, chost)) != 0 ||
137 (r = sshbuf_put_cstring(b, cuser)) != 0)
138 fatal_fr(r, "reconstruct packet");
139#ifdef DEBUG_PK
140 sshbuf_dump(b, stderr);
141#endif
142
143 auth2_record_info(authctxt,
144 "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
145
146 /* test for allowed key and correct signature */
147 authenticated = 0;
148 if (mm_hostbased_key_allowed(ssh, authctxt->pw, cuser,
149 chost, key) &&
150 mm_sshkey_verify(key, sig, slen,
151 sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL) == 0)
152 authenticated = 1;
153
154 auth2_record_key(authctxt, authenticated, key);
155 sshbuf_free(b);
156done:
157 debug2_f("authenticated %d", authenticated);
158 sshkey_free(key);
159 free(pkalg);
160 free(pkblob);
161 free(cuser);
162 free(chost);
163 free(sig);
164 return authenticated;
165}
166
167/* return 1 if given hostkey is allowed */
168int
169hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
170 const char *cuser, char *chost, struct sshkey *key)
171{
172 const char *resolvedname, *ipaddr, *lookup, *reason;
173 HostStatus host_status;
174 int len;
175 char *fp;
176
177 if (auth_key_is_revoked(key))
178 return 0;
179
180 resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
181 ipaddr = ssh_remote_ipaddr(ssh);
182
183 debug2_f("chost %s resolvedname %s ipaddr %s",
184 chost, resolvedname, ipaddr);
185
186 if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
187 debug2("stripping trailing dot from chost %s", chost);
188 chost[len - 1] = '\0';
189 }
190
191 if (options.hostbased_uses_name_from_packet_only) {
192 if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
193 debug2_f("auth_rhosts2 refused user \"%.100s\" "
194 "host \"%.100s\" (from packet)", cuser, chost);
195 return 0;
196 }
197 lookup = chost;
198 } else {
199 if (strcasecmp(resolvedname, chost) != 0)
200 logit("userauth_hostbased mismatch: "
201 "client sends %s, but we resolve %s to %s",
202 chost, ipaddr, resolvedname);
203 if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
204 debug2_f("auth_rhosts2 refused "
205 "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
206 cuser, resolvedname, ipaddr);
207 return 0;
208 }
209 lookup = resolvedname;
210 }
211 debug2_f("access allowed by auth_rhosts2");
212
213 if (sshkey_is_cert(key) && sshkey_cert_check_host(key, lookup,
214 options.ca_sign_algorithms, &reason) != 0) {
215 if ((fp = sshkey_fingerprint(key->cert->signature_key,
216 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
217 fatal_f("sshkey_fingerprint fail");
218 error("Refusing certificate ID \"%s\" serial=%llu signed by "
219 "%s CA %s: %s", key->cert->key_id,
220 (unsigned long long)key->cert->serial,
221 sshkey_type(key->cert->signature_key), fp, reason);
222 auth_debug_add("Refused Certificate ID \"%s\" serial=%llu: %s",
223 key->cert->key_id, (unsigned long long)key->cert->serial,
224 reason);
225 free(fp);
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
263Authmethod method_hostbased = {
264 &methodcfg_hostbased,
265 userauth_hostbased,
266};