blob: 5b4a2cc0222aa15b22da5f0dfe125c1f5f31f3f7 [file] [log] [blame]
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001/* $OpenBSD: auth2-pubkey.c,v 1.98 2020/01/23 07:10:22 dtucker Exp $ */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00002/*
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"
Damien Millerf17883e2006-03-15 11:45:54 +110027
28#include <sys/types.h>
29#include <sys/stat.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000030
djm@openbsd.orgbe02d7c2019-09-06 04:53:27 +000031#include <stdlib.h>
Damien Miller09d3e122012-10-31 08:58:58 +110032#include <errno.h>
Darren Tucker06db5842008-06-13 14:51:28 +100033#include <fcntl.h>
Darren Tucker737f7af2012-11-05 17:07:43 +110034#ifdef HAVE_PATHS_H
35# include <paths.h>
36#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100037#include <pwd.h>
Damien Miller09d3e122012-10-31 08:58:58 +110038#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110041#include <string.h>
42#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100043#include <unistd.h>
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +000044#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100045
Damien Millerd7834352006-08-05 12:39:39 +100046#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110047#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000048#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000049#include "packet.h"
markus@openbsd.orgc7d39ac2018-07-09 21:35:50 +000050#include "sshbuf.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000051#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100052#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000053#include "servconf.h"
54#include "compat.h"
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000055#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "hostfile.h"
57#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000058#include "pathnames.h"
59#include "uidswap.h"
60#include "auth-options.h"
61#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100062#ifdef GSSAPI
63#include "ssh-gss.h"
64#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000065#include "monitor_wrap.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110066#include "authfile.h"
Damien Miller30da3442010-05-10 11:58:03 +100067#include "match.h"
djm@openbsd.org24232a32015-05-21 06:38:35 +000068#include "ssherr.h"
69#include "channels.h" /* XXX for session.h */
70#include "session.h" /* XXX for child_set_env(); refactor? */
djm@openbsd.org0fddf292019-11-25 00:52:46 +000071#include "sk-api.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000072
73/* import */
74extern ServerOptions options;
75extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100076extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000077
djm@openbsd.org27885632017-12-19 00:24:34 +000078static char *
79format_key(const struct sshkey *key)
80{
81 char *ret, *fp = sshkey_fingerprint(key,
82 options.fingerprint_hash, SSH_FP_DEFAULT);
83
84 xasprintf(&ret, "%s %s", sshkey_type(key), fp);
85 free(fp);
86 return ret;
87}
88
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000089static int
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000090userauth_pubkey(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000091{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000092 Authctxt *authctxt = ssh->authctxt;
djm@openbsd.org7c856852018-03-03 03:15:51 +000093 struct passwd *pw = authctxt->pw;
djm@openbsd.org74287f52018-07-31 03:10:27 +000094 struct sshbuf *b = NULL;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000095 struct sshkey *key = NULL;
djm@openbsd.org74287f52018-07-31 03:10:27 +000096 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
97 u_char *pkblob = NULL, *sig = NULL, have_sig;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000098 size_t blen, slen;
99 int r, pktype;
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000100 int req_presence = 0, authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000101 struct sshauthopt *authopts = NULL;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000102 struct sshkey_sig_details *sig_details = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000103
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000104 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
105 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
106 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
107 fatal("%s: parse request failed: %s", __func__, ssh_err(r));
djm@openbsd.orgff5d2cf2019-01-22 11:26:16 +0000108
109 if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
110 char *keystring;
111 struct sshbuf *pkbuf;
112
113 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
114 fatal("%s: sshbuf_from failed", __func__);
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000115 if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
djm@openbsd.orgff5d2cf2019-01-22 11:26:16 +0000116 fatal("%s: sshbuf_dtob64 failed", __func__);
117 debug2("%s: %s user %s %s public key %s %s", __func__,
118 authctxt->valid ? "valid" : "invalid", authctxt->user,
119 have_sig ? "attempting" : "querying", pkalg, keystring);
120 sshbuf_free(pkbuf);
121 free(keystring);
122 }
123
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000124 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000125 if (pktype == KEY_UNSPEC) {
126 /* this is perfectly legal */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000127 verbose("%s: unsupported public key algorithm: %s",
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000128 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000129 goto done;
130 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000131 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
132 error("%s: could not parse key: %s", __func__, ssh_err(r));
133 goto done;
134 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000135 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000136 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000137 goto done;
138 }
139 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000140 error("%s: type mismatch for decoded key "
141 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000142 goto done;
143 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000144 if (sshkey_type_plain(key->type) == KEY_RSA &&
145 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100146 logit("Refusing RSA key because client uses unsafe "
147 "signature scheme");
148 goto done;
149 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000150 if (auth2_key_already_used(authctxt, key)) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000151 logit("refusing previously-used %s key", sshkey_type(key));
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000152 goto done;
153 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000154 if (match_pattern_list(pkalg, options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000155 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
156 __func__, sshkey_ssh_name(key));
157 goto done;
158 }
djm@openbsd.org86e57372018-09-20 03:28:06 +0000159 if ((r = sshkey_check_cert_sigtype(key,
160 options.ca_sign_algorithms)) != 0) {
161 logit("%s: certificate signature algorithm %s: %s", __func__,
162 (key->cert == NULL || key->cert->signature_type == NULL) ?
163 "(null)" : key->cert->signature_type, ssh_err(r));
164 goto done;
165 }
djm@openbsd.org27885632017-12-19 00:24:34 +0000166 key_s = format_key(key);
167 if (sshkey_is_cert(key))
168 ca_s = format_key(key->cert->signature_key);
169
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000170 if (have_sig) {
djm@openbsd.org27885632017-12-19 00:24:34 +0000171 debug3("%s: have %s signature for %s%s%s",
172 __func__, pkalg, key_s,
173 ca_s == NULL ? "" : " CA ",
174 ca_s == NULL ? "" : ca_s);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000175 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
176 (r = sshpkt_get_end(ssh)) != 0)
177 fatal("%s: %s", __func__, ssh_err(r));
178 if ((b = sshbuf_new()) == NULL)
179 fatal("%s: sshbuf_new failed", __func__);
180 if (ssh->compat & SSH_OLD_SESSIONID) {
181 if ((r = sshbuf_put(b, session_id2,
182 session_id2_len)) != 0)
183 fatal("%s: sshbuf_put session id: %s",
184 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000185 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000186 if ((r = sshbuf_put_string(b, session_id2,
187 session_id2_len)) != 0)
188 fatal("%s: sshbuf_put_string session id: %s",
189 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000190 }
djm@openbsd.org74287f52018-07-31 03:10:27 +0000191 if (!authctxt->valid || authctxt->user == NULL) {
192 debug2("%s: disabled because of invalid user",
193 __func__);
194 goto done;
195 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000196 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000197 xasprintf(&userstyle, "%s%s%s", authctxt->user,
198 authctxt->style ? ":" : "",
199 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000200 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
201 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000202 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
203 (r = sshbuf_put_cstring(b, "publickey")) != 0 ||
204 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
mestre@openbsd.orgdb8bb802018-08-28 12:25:53 +0000205 (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000206 (r = sshbuf_put_string(b, pkblob, blen)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000207 fatal("%s: build packet failed: %s",
208 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000209#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000210 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000211#endif
212 /* test for correct signature */
213 authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000214 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000215 PRIVSEP(sshkey_verify(key, sig, slen,
216 sshbuf_ptr(b), sshbuf_len(b),
217 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000218 ssh->compat, &sig_details)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000219 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000220 }
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000221 if (authenticated == 1 && sig_details != NULL) {
222 auth2_record_info(authctxt, "signature count = %u",
223 sig_details->sk_counter);
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000224 debug("%s: sk_counter = %u, sk_flags = 0x%02x",
225 __func__, sig_details->sk_counter,
226 sig_details->sk_flags);
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000227 req_presence = (options.pubkey_auth_options &
djm@openbsd.org2e712632019-11-25 00:54:23 +0000228 PUBKEYAUTH_TOUCH_REQUIRED) ||
229 !authopts->no_require_user_presence;
djm@openbsd.org0fddf292019-11-25 00:52:46 +0000230 if (req_presence && (sig_details->sk_flags &
231 SSH_SK_USER_PRESENCE_REQD) == 0) {
232 error("public key %s signature for %s%s from "
233 "%.128s port %d rejected: user presence "
234 "(key touch) requirement not met ", key_s,
235 authctxt->valid ? "" : "invalid user ",
236 authctxt->user, ssh_remote_ipaddr(ssh),
237 ssh_remote_port(ssh));
238 authenticated = 0;
239 goto done;
240 }
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000241 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000242 auth2_record_key(authctxt, authenticated, key);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000243 } else {
djm@openbsd.org27885632017-12-19 00:24:34 +0000244 debug("%s: test pkalg %s pkblob %s%s%s",
245 __func__, pkalg, key_s,
246 ca_s == NULL ? "" : " CA ",
247 ca_s == NULL ? "" : ca_s);
248
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000249 if ((r = sshpkt_get_end(ssh)) != 0)
250 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000251
djm@openbsd.org74287f52018-07-31 03:10:27 +0000252 if (!authctxt->valid || authctxt->user == NULL) {
253 debug2("%s: disabled because of invalid user",
254 __func__);
255 goto done;
256 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000257 /* XXX fake reply and always send PK_OK ? */
258 /*
259 * XXX this allows testing whether a user is allowed
260 * to login: if you happen to have a valid pubkey this
261 * message is sent. the message is NEVER sent at all
262 * if a user is not allowed to login. is this an
263 * issue? -markus
264 */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000265 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000266 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
267 != 0 ||
268 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
269 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000270 (r = sshpkt_send(ssh)) != 0 ||
271 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000272 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000273 authctxt->postponed = 1;
274 }
275 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000276done:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000277 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
278 debug("%s: key options inconsistent with existing", __func__);
279 authenticated = 0;
280 }
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000281 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000282
djm@openbsd.org7fef1732018-08-23 03:01:08 +0000283 sshbuf_free(b);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000284 sshauthopt_free(authopts);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000285 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000286 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000287 free(pkalg);
288 free(pkblob);
djm@openbsd.org27885632017-12-19 00:24:34 +0000289 free(key_s);
290 free(ca_s);
djm@openbsd.org74287f52018-07-31 03:10:27 +0000291 free(sig);
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000292 sshkey_sig_details_free(sig_details);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000293 return authenticated;
294}
295
Damien Miller30da3442010-05-10 11:58:03 +1000296static int
Damien Miller86687062014-07-02 15:28:02 +1000297match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000298{
299 char *result;
300 u_int i;
301
302 /* XXX percent_expand() sequences for authorized_principals? */
303
304 for (i = 0; i < cert->nprincipals; i++) {
305 if ((result = match_list(cert->principals[i],
306 principal_list, NULL)) != NULL) {
307 debug3("matched principal from key options \"%.100s\"",
308 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000309 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000310 return 1;
311 }
312 }
313 return 0;
314}
315
djm@openbsd.org7c856852018-03-03 03:15:51 +0000316/*
317 * Process a single authorized_principals format line. Returns 0 and sets
318 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
319 * log preamble for file/line information.
320 */
Damien Miller30da3442010-05-10 11:58:03 +1000321static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000322check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
323 const char *loc, struct sshauthopt **authoptsp)
Damien Miller30da3442010-05-10 11:58:03 +1000324{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000325 u_int i, found = 0;
326 char *ep, *line_opts;
327 const char *reason = NULL;
328 struct sshauthopt *opts = NULL;
329
330 if (authoptsp != NULL)
331 *authoptsp = NULL;
332
333 /* Trim trailing whitespace. */
334 ep = cp + strlen(cp) - 1;
335 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
336 *ep-- = '\0';
337
338 /*
339 * If the line has internal whitespace then assume it has
340 * key options.
341 */
342 line_opts = NULL;
343 if ((ep = strrchr(cp, ' ')) != NULL ||
344 (ep = strrchr(cp, '\t')) != NULL) {
345 for (; *ep == ' ' || *ep == '\t'; ep++)
346 ;
347 line_opts = cp;
348 cp = ep;
349 }
350 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
351 debug("%s: bad principals options: %s", loc, reason);
352 auth_debug_add("%s: bad principals options: %s", loc, reason);
353 return -1;
354 }
355 /* Check principals in cert against those on line */
356 for (i = 0; i < cert->nprincipals; i++) {
357 if (strcmp(cp, cert->principals[i]) != 0)
358 continue;
359 debug3("%s: matched principal \"%.100s\"",
360 loc, cert->principals[i]);
361 found = 1;
362 }
363 if (found && authoptsp != NULL) {
364 *authoptsp = opts;
365 opts = NULL;
366 }
367 sshauthopt_free(opts);
368 return found ? 0 : -1;
369}
370
371static int
372process_principals(struct ssh *ssh, FILE *f, const char *file,
373 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
374{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000375 char loc[256], *line = NULL, *cp, *ep;
376 size_t linesize = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000377 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000378 u_int found_principal = 0;
379
380 if (authoptsp != NULL)
381 *authoptsp = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000382
markus@openbsd.org7f906352018-06-06 18:29:18 +0000383 while (getline(&line, &linesize, f) != -1) {
384 linenum++;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000385 /* Always consume entire input */
386 if (found_principal)
387 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000388
Damien Miller6018a362010-07-02 13:35:19 +1000389 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000390 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
391 ;
Damien Miller6018a362010-07-02 13:35:19 +1000392 /* Skip blank and comment lines. */
393 if ((ep = strchr(cp, '#')) != NULL)
394 *ep = '\0';
395 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000396 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000397
398 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
399 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
400 found_principal = 1;
Damien Miller30da3442010-05-10 11:58:03 +1000401 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000402 free(line);
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000403 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100404}
Damien Miller30da3442010-05-10 11:58:03 +1000405
djm@openbsd.org7c856852018-03-03 03:15:51 +0000406/* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
407
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000408static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000409match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
410 struct sshkey_cert *cert, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000411{
412 FILE *f;
413 int success;
414
djm@openbsd.org7c856852018-03-03 03:15:51 +0000415 if (authoptsp != NULL)
416 *authoptsp = NULL;
417
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000418 temporarily_use_uid(pw);
419 debug("trying authorized principals file %s", file);
420 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
421 restore_uid();
422 return 0;
423 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000424 success = process_principals(ssh, f, file, cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000425 fclose(f);
426 restore_uid();
427 return success;
428}
429
430/*
431 * Checks whether principal is allowed in output of command.
432 * returns 1 if the principal is allowed or 0 otherwise.
433 */
434static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000435match_principals_command(struct ssh *ssh, struct passwd *user_pw,
436 const struct sshkey *key, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000437{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000438 struct passwd *runas_pw = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000439 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000440 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000441 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000442 int i, ac = 0, uid_swapped = 0;
443 pid_t pid;
444 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000445 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.org30615292019-05-20 00:25:55 +0000446 char serial_s[32], uidstr[32];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000447 void (*osigchld)(int);
448
djm@openbsd.org7c856852018-03-03 03:15:51 +0000449 if (authoptsp != NULL)
450 *authoptsp = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000451 if (options.authorized_principals_command == NULL)
452 return 0;
453 if (options.authorized_principals_command_user == NULL) {
454 error("No user for AuthorizedPrincipalsCommand specified, "
455 "skipping");
456 return 0;
457 }
458
459 /*
460 * NB. all returns later this function should go via "out" to
461 * ensure the original SIGCHLD handler is restored properly.
462 */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000463 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000464
465 /* Prepare and verify the user for the command */
466 username = percent_expand(options.authorized_principals_command_user,
467 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000468 runas_pw = getpwnam(username);
469 if (runas_pw == NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000470 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
471 username, strerror(errno));
472 goto out;
473 }
474
475 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000476 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000477 error("AuthorizedPrincipalsCommand \"%s\" contains "
djm@openbsd.org4cd6b122019-06-21 03:19:59 +0000478 "invalid quotes", options.authorized_principals_command);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000479 goto out;
480 }
481 if (ac == 0) {
482 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
djm@openbsd.org4cd6b122019-06-21 03:19:59 +0000483 options.authorized_principals_command);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000484 goto out;
485 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000486 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
487 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
488 error("%s: sshkey_fingerprint failed", __func__);
489 goto out;
490 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000491 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000492 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
493 error("%s: sshkey_fingerprint failed", __func__);
494 goto out;
495 }
496 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
497 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
498 goto out;
499 }
500 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
501 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
502 goto out;
503 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000504 snprintf(serial_s, sizeof(serial_s), "%llu",
505 (unsigned long long)cert->serial);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000506 snprintf(uidstr, sizeof(uidstr), "%llu",
507 (unsigned long long)user_pw->pw_uid);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000508 for (i = 1; i < ac; i++) {
509 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000510 "U", uidstr,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000511 "u", user_pw->pw_name,
512 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000513 "t", sshkey_ssh_name(key),
514 "T", sshkey_ssh_name(cert->signature_key),
515 "f", key_fp,
516 "F", ca_fp,
517 "k", keytext,
518 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000519 "i", cert->key_id,
520 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000521 (char *)NULL);
522 if (tmp == NULL)
523 fatal("%s: percent_expand failed", __func__);
524 free(av[i]);
525 av[i] = tmp;
526 }
527 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000528 command = argv_assemble(ac, av);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000529
djm@openbsd.org7c856852018-03-03 03:15:51 +0000530 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000531 ac, av, &f,
532 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000533 goto out;
534
535 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000536 temporarily_use_uid(runas_pw);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000537
djm@openbsd.org7c856852018-03-03 03:15:51 +0000538 ok = process_principals(ssh, f, "(command)", cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000539
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000540 fclose(f);
541 f = NULL;
542
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000543 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000544 goto out;
545
546 /* Read completed successfully */
547 found_principal = ok;
548 out:
549 if (f != NULL)
550 fclose(f);
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000551 ssh_signal(SIGCHLD, osigchld);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000552 for (i = 0; i < ac; i++)
553 free(av[i]);
554 free(av);
555 if (uid_swapped)
556 restore_uid();
557 free(command);
558 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000559 free(ca_fp);
560 free(key_fp);
561 free(catext);
562 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000563 return found_principal;
564}
djm@openbsd.org7c856852018-03-03 03:15:51 +0000565
djm@openbsd.org7c856852018-03-03 03:15:51 +0000566/*
djm@openbsd.org7c856852018-03-03 03:15:51 +0000567 * Check a single line of an authorized_keys-format file. Returns 0 if key
568 * matches, -1 otherwise. Will return key/cert options via *authoptsp
569 * on success. "loc" is used as file/line location in log messages.
570 */
571static int
572check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
573 char *cp, const char *loc, struct sshauthopt **authoptsp)
574{
575 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
576 struct sshkey *found = NULL;
577 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
578 char *key_options = NULL, *fp = NULL;
579 const char *reason = NULL;
580 int ret = -1;
581
582 if (authoptsp != NULL)
583 *authoptsp = NULL;
584
585 if ((found = sshkey_new(want_keytype)) == NULL) {
586 debug3("%s: keytype %d failed", __func__, want_keytype);
587 goto out;
588 }
589
590 /* XXX djm: peek at key type in line and skip if unwanted */
591
592 if (sshkey_read(found, &cp) != 0) {
593 /* no key? check for options */
594 debug2("%s: check options: '%s'", loc, cp);
595 key_options = cp;
djm@openbsd.orgdd8002f2019-09-03 08:30:47 +0000596 if (sshkey_advance_past_options(&cp) != 0) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000597 reason = "invalid key option string";
598 goto fail_reason;
599 }
600 skip_space(&cp);
601 if (sshkey_read(found, &cp) != 0) {
602 /* still no key? advance to next line*/
603 debug2("%s: advance: '%s'", loc, cp);
604 goto out;
605 }
606 }
607 /* Parse key options now; we need to know if this is a CA key */
608 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
609 debug("%s: bad key options: %s", loc, reason);
610 auth_debug_add("%s: bad key options: %s", loc, reason);
611 goto out;
612 }
613 /* Ignore keys that don't match or incorrectly marked as CAs */
614 if (sshkey_is_cert(key)) {
615 /* Certificate; check signature key against CA */
616 if (!sshkey_equal(found, key->cert->signature_key) ||
617 !keyopts->cert_authority)
618 goto out;
619 } else {
620 /* Plain key: check it against key found in file */
621 if (!sshkey_equal(found, key) || keyopts->cert_authority)
622 goto out;
623 }
624
625 /* We have a candidate key, perform authorisation checks */
626 if ((fp = sshkey_fingerprint(found,
627 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
628 fatal("%s: fingerprint failed", __func__);
629
630 debug("%s: matching %s found: %s %s", loc,
631 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
632
633 if (auth_authorise_keyopts(ssh, pw, keyopts,
634 sshkey_is_cert(key), loc) != 0) {
635 reason = "Refused by key options";
636 goto fail_reason;
637 }
638 /* That's all we need for plain keys. */
639 if (!sshkey_is_cert(key)) {
640 verbose("Accepted key %s %s found at %s",
641 sshkey_type(found), fp, loc);
642 finalopts = keyopts;
643 keyopts = NULL;
644 goto success;
645 }
646
647 /*
648 * Additional authorisation for certificates.
649 */
650
651 /* Parse and check options present in certificate */
652 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
653 reason = "Invalid certificate options";
654 goto fail_reason;
655 }
656 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
657 reason = "Refused by certificate options";
658 goto fail_reason;
659 }
660 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
661 goto fail_reason;
662
663 /*
664 * If the user has specified a list of principals as
665 * a key option, then prefer that list to matching
666 * their username in the certificate principals list.
667 */
668 if (keyopts->cert_principals != NULL &&
669 !match_principals_option(keyopts->cert_principals, key->cert)) {
670 reason = "Certificate does not contain an authorized principal";
671 goto fail_reason;
672 }
673 if (sshkey_cert_check_authority(key, 0, 0,
674 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0)
675 goto fail_reason;
676
677 verbose("Accepted certificate ID \"%s\" (serial %llu) "
678 "signed by CA %s %s found at %s",
679 key->cert->key_id,
680 (unsigned long long)key->cert->serial,
681 sshkey_type(found), fp, loc);
682
683 success:
684 if (finalopts == NULL)
685 fatal("%s: internal error: missing options", __func__);
686 if (authoptsp != NULL) {
687 *authoptsp = finalopts;
688 finalopts = NULL;
689 }
690 /* success */
691 ret = 0;
692 goto out;
693
694 fail_reason:
695 error("%s", reason);
696 auth_debug_add("%s", reason);
697 out:
698 free(fp);
699 sshauthopt_free(keyopts);
700 sshauthopt_free(certopts);
701 sshauthopt_free(finalopts);
702 sshkey_free(found);
703 return ret;
704}
705
Damien Miller09d3e122012-10-31 08:58:58 +1100706/*
707 * Checks whether key is allowed in authorized_keys-format file,
708 * returns 1 if the key is allowed or 0 otherwise.
709 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000710static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000711check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
712 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000713{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000714 char *cp, *line = NULL, loc[256];
715 size_t linesize = 0;
Darren Tucker33c787f2008-07-02 22:37:30 +1000716 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000717 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000718
719 if (authoptsp != NULL)
720 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000721
markus@openbsd.org7f906352018-06-06 18:29:18 +0000722 while (getline(&line, &linesize, f) != -1) {
723 linenum++;
djm@openbsd.orgabd59662017-09-07 23:48:09 +0000724 /* Always consume entire file */
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000725 if (found_key)
726 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100727
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000728 /* Skip leading whitespace, empty and comment lines. */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000729 cp = line;
730 skip_space(&cp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000731 if (!*cp || *cp == '\n' || *cp == '#')
732 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000733 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
734 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
Damien Miller0a80ca12010-02-27 07:55:05 +1100735 found_key = 1;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000736 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000737 free(line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000738 return found_key;
739}
740
Damien Miller1aed65e2010-03-04 21:53:35 +1100741/* Authenticate a certificate key against TrustedUserCAKeys */
742static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000743user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
744 struct sshauthopt **authoptsp)
Damien Miller1aed65e2010-03-04 21:53:35 +1100745{
Damien Miller30da3442010-05-10 11:58:03 +1000746 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100747 const char *reason;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000748 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
749 struct sshauthopt *final_opts = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000750 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100751
djm@openbsd.org7c856852018-03-03 03:15:51 +0000752 if (authoptsp != NULL)
753 *authoptsp = NULL;
754
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000755 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100756 return 0;
757
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000758 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
759 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
760 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100761
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000762 if ((r = sshkey_in_file(key->cert->signature_key,
763 options.trusted_user_ca_keys, 1, 0)) != 0) {
764 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
765 sshkey_type(key->cert->signature_key), ca_fp,
766 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100767 goto out;
768 }
Damien Miller30da3442010-05-10 11:58:03 +1000769 /*
770 * If AuthorizedPrincipals is in use, then compare the certificate
771 * principals against the names in that file rather than matching
772 * against the username.
773 */
774 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000775 if (match_principals_file(ssh, pw, principals_file,
776 key->cert, &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000777 found_principal = 1;
778 }
779 /* Try querying command if specified */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000780 if (!found_principal && match_principals_command(ssh, pw, key,
781 &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000782 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000783 /* If principals file or command is specified, then require a match */
784 use_authorized_principals = principals_file != NULL ||
785 options.authorized_principals_command != NULL;
786 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000787 reason = "Certificate does not contain an authorized principal";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000788 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100789 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000790 if (use_authorized_principals && principals_opts == NULL)
791 fatal("%s: internal error: missing principals_opts", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000792 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000793 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000794 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100795
djm@openbsd.org7c856852018-03-03 03:15:51 +0000796 /* Check authority from options in key and from principals file/cmd */
797 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
798 reason = "Invalid certificate options";
799 goto fail_reason;
800 }
801 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
802 reason = "Refused by certificate options";
803 goto fail_reason;
804 }
805 if (principals_opts == NULL) {
806 final_opts = cert_opts;
807 cert_opts = NULL;
808 } else {
809 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
810 "principals") != 0) {
811 reason = "Refused by certificate principals options";
812 goto fail_reason;
813 }
814 if ((final_opts = sshauthopt_merge(principals_opts,
815 cert_opts, &reason)) == NULL) {
816 fail_reason:
817 error("%s", reason);
818 auth_debug_add("%s", reason);
819 goto out;
820 }
821 }
822
823 /* Success */
djm@openbsd.org63d18812015-10-27 01:44:45 +0000824 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
825 "%s CA %s via %s", key->cert->key_id,
826 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000827 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100828 options.trusted_user_ca_keys);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000829 if (authoptsp != NULL) {
830 *authoptsp = final_opts;
831 final_opts = NULL;
832 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100833 ret = 1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100834 out:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000835 sshauthopt_free(principals_opts);
836 sshauthopt_free(cert_opts);
837 sshauthopt_free(final_opts);
Darren Tuckera627d422013-06-02 07:31:17 +1000838 free(principals_file);
839 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100840 return ret;
841}
842
Damien Miller09d3e122012-10-31 08:58:58 +1100843/*
844 * Checks whether key is allowed in file.
845 * returns 1 if the key is allowed or 0 otherwise.
846 */
847static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000848user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
849 char *file, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100850{
851 FILE *f;
852 int found_key = 0;
853
djm@openbsd.org7c856852018-03-03 03:15:51 +0000854 if (authoptsp != NULL)
855 *authoptsp = NULL;
856
Damien Miller09d3e122012-10-31 08:58:58 +1100857 /* Temporarily use the user's uid. */
858 temporarily_use_uid(pw);
859
860 debug("trying public key file %s", file);
861 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000862 found_key = check_authkeys_file(ssh, pw, f, file,
863 key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100864 fclose(f);
865 }
866
867 restore_uid();
868 return found_key;
869}
870
871/*
872 * Checks whether key is allowed in output of command.
873 * returns 1 if the key is allowed or 0 otherwise.
874 */
875static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000876user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
877 struct sshkey *key, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100878{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000879 struct passwd *runas_pw = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000880 FILE *f = NULL;
881 int r, ok, found_key = 0;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000882 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100883 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000884 char *username = NULL, *key_fp = NULL, *keytext = NULL;
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000885 char uidstr[32], *tmp, *command = NULL, **av = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000886 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100887
djm@openbsd.org7c856852018-03-03 03:15:51 +0000888 if (authoptsp != NULL)
889 *authoptsp = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000890 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100891 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100892 if (options.authorized_keys_command_user == NULL) {
893 error("No user for AuthorizedKeysCommand specified, skipping");
894 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100895 }
896
djm@openbsd.org24232a32015-05-21 06:38:35 +0000897 /*
898 * NB. all returns later this function should go via "out" to
899 * ensure the original SIGCHLD handler is restored properly.
900 */
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000901 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
djm@openbsd.org24232a32015-05-21 06:38:35 +0000902
903 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100904 username = percent_expand(options.authorized_keys_command_user,
905 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000906 runas_pw = getpwnam(username);
907 if (runas_pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100908 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
909 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100910 goto out;
911 }
912
djm@openbsd.org24232a32015-05-21 06:38:35 +0000913 /* Prepare AuthorizedKeysCommand */
914 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
915 SSH_FP_DEFAULT)) == NULL) {
916 error("%s: sshkey_fingerprint failed", __func__);
917 goto out;
918 }
919 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
920 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100921 goto out;
922 }
923
djm@openbsd.org24232a32015-05-21 06:38:35 +0000924 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000925 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
djm@openbsd.org24232a32015-05-21 06:38:35 +0000926 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
927 command);
928 goto out;
929 }
930 if (ac == 0) {
931 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
932 command);
933 goto out;
934 }
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000935 snprintf(uidstr, sizeof(uidstr), "%llu",
936 (unsigned long long)user_pw->pw_uid);
djm@openbsd.org24232a32015-05-21 06:38:35 +0000937 for (i = 1; i < ac; i++) {
938 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000939 "U", uidstr,
djm@openbsd.org24232a32015-05-21 06:38:35 +0000940 "u", user_pw->pw_name,
941 "h", user_pw->pw_dir,
942 "t", sshkey_ssh_name(key),
943 "f", key_fp,
944 "k", keytext,
945 (char *)NULL);
946 if (tmp == NULL)
947 fatal("%s: percent_expand failed", __func__);
948 free(av[i]);
949 av[i] = tmp;
950 }
951 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000952 command = argv_assemble(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100953
954 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000955 * If AuthorizedKeysCommand was run without arguments
956 * then fall back to the old behaviour of passing the
957 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100958 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000959 if (ac == 1) {
960 av = xreallocarray(av, ac + 2, sizeof(*av));
961 av[1] = xstrdup(user_pw->pw_name);
962 av[2] = NULL;
963 /* Fix up command too, since it is used in log messages */
964 free(command);
965 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +1100966 }
967
djm@openbsd.org7c856852018-03-03 03:15:51 +0000968 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000969 ac, av, &f,
970 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.org24232a32015-05-21 06:38:35 +0000971 goto out;
972
973 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000974 temporarily_use_uid(runas_pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100975
djm@openbsd.org7c856852018-03-03 03:15:51 +0000976 ok = check_authkeys_file(ssh, user_pw, f,
977 options.authorized_keys_command, key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100978
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000979 fclose(f);
980 f = NULL;
981
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000982 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +1100983 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000984
985 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +1100986 found_key = ok;
987 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +0000988 if (f != NULL)
989 fclose(f);
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +0000990 ssh_signal(SIGCHLD, osigchld);
djm@openbsd.org24232a32015-05-21 06:38:35 +0000991 for (i = 0; i < ac; i++)
992 free(av[i]);
993 free(av);
994 if (uid_swapped)
995 restore_uid();
996 free(command);
997 free(username);
998 free(key_fp);
999 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +11001000 return found_key;
1001}
1002
1003/*
1004 * Check whether key authenticates and authorises the user.
1005 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001006int
djm@openbsd.org7c856852018-03-03 03:15:51 +00001007user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
1008 int auth_attempt, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001009{
djm@openbsd.orgc95b90d2019-06-14 03:39:59 +00001010 u_int success = 0, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001011 char *file;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001012 struct sshauthopt *opts = NULL;
djm@openbsd.orgc95b90d2019-06-14 03:39:59 +00001013
djm@openbsd.org7c856852018-03-03 03:15:51 +00001014 if (authoptsp != NULL)
1015 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001016
Damien Miller1aed65e2010-03-04 21:53:35 +11001017 if (auth_key_is_revoked(key))
1018 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001019 if (sshkey_is_cert(key) &&
1020 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +11001021 return 0;
1022
djm@openbsd.orgc95b90d2019-06-14 03:39:59 +00001023 for (i = 0; !success && i < options.num_authkeys_files; i++) {
1024 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1025 continue;
1026 file = expand_authorized_keys(
1027 options.authorized_keys_files[i], pw);
1028 success = user_key_allowed2(ssh, pw, key, file, &opts);
1029 free(file);
1030 if (!success) {
1031 sshauthopt_free(opts);
1032 opts = NULL;
1033 }
1034 }
1035 if (success)
1036 goto out;
1037
djm@openbsd.org7c856852018-03-03 03:15:51 +00001038 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
1039 goto out;
1040 sshauthopt_free(opts);
1041 opts = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +11001042
djm@openbsd.org7c856852018-03-03 03:15:51 +00001043 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
1044 goto out;
1045 sshauthopt_free(opts);
1046 opts = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +11001047
djm@openbsd.org7c856852018-03-03 03:15:51 +00001048 out:
1049 if (success && authoptsp != NULL) {
1050 *authoptsp = opts;
1051 opts = NULL;
1052 }
1053 sshauthopt_free(opts);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001054 return success;
1055}
1056
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001057Authmethod method_pubkey = {
1058 "publickey",
1059 userauth_pubkey,
1060 &options.pubkey_authentication
1061};