Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.120
1.120 ! djm 1: /* $OpenBSD: auth2-pubkey.c,v 1.119 2023/07/27 22:25:17 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.115 djm 4: * Copyright (c) 2010 Damien Miller. All rights reserved.
1.1 markus 5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.10 stevesk 27:
28: #include <sys/types.h>
1.13 stevesk 29:
1.94 djm 30: #include <stdlib.h>
1.31 djm 31: #include <errno.h>
32: #include <paths.h>
1.13 stevesk 33: #include <pwd.h>
1.31 djm 34: #include <signal.h>
1.14 stevesk 35: #include <stdio.h>
1.15 deraadt 36: #include <stdarg.h>
1.20 djm 37: #include <string.h>
38: #include <time.h>
1.17 dtucker 39: #include <unistd.h>
1.44 djm 40: #include <limits.h>
1.1 markus 41:
1.15 deraadt 42: #include "xmalloc.h"
1.8 dtucker 43: #include "ssh.h"
1.1 markus 44: #include "ssh2.h"
45: #include "packet.h"
1.106 djm 46: #include "kex.h"
1.81 markus 47: #include "sshbuf.h"
1.1 markus 48: #include "log.h"
1.41 millert 49: #include "misc.h"
1.1 markus 50: #include "servconf.h"
51: #include "compat.h"
1.64 markus 52: #include "sshkey.h"
1.15 deraadt 53: #include "hostfile.h"
1.1 markus 54: #include "auth.h"
55: #include "pathnames.h"
56: #include "uidswap.h"
57: #include "auth-options.h"
58: #include "canohost.h"
1.15 deraadt 59: #ifdef GSSAPI
60: #include "ssh-gss.h"
61: #endif
1.1 markus 62: #include "monitor_wrap.h"
1.21 djm 63: #include "authfile.h"
1.24 djm 64: #include "match.h"
1.50 djm 65: #include "ssherr.h"
66: #include "channels.h" /* XXX for session.h */
67: #include "session.h" /* XXX for child_set_env(); refactor? */
1.96 djm 68: #include "sk-api.h"
1.1 markus 69:
70: /* import */
71: extern ServerOptions options;
1.120 ! djm 72: extern struct authmethod_cfg methodcfg_pubkey;
1.1 markus 73:
1.73 djm 74: static char *
75: format_key(const struct sshkey *key)
76: {
77: char *ret, *fp = sshkey_fingerprint(key,
78: options.fingerprint_hash, SSH_FP_DEFAULT);
79:
80: xasprintf(&ret, "%s %s", sshkey_type(key), fp);
81: free(fp);
82: return ret;
83: }
84:
1.2 markus 85: static int
1.111 djm 86: userauth_pubkey(struct ssh *ssh, const char *method)
1.1 markus 87: {
1.65 markus 88: Authctxt *authctxt = ssh->authctxt;
1.77 djm 89: struct passwd *pw = authctxt->pw;
1.83 djm 90: struct sshbuf *b = NULL;
1.112 djm 91: struct sshkey *key = NULL, *hostkey = NULL;
1.83 djm 92: char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
93: u_char *pkblob = NULL, *sig = NULL, have_sig;
1.64 markus 94: size_t blen, slen;
1.112 djm 95: int hostbound, r, pktype;
1.100 djm 96: int req_presence = 0, req_verify = 0, authenticated = 0;
1.77 djm 97: struct sshauthopt *authopts = NULL;
1.95 djm 98: struct sshkey_sig_details *sig_details = NULL;
1.1 markus 99:
1.112 djm 100: hostbound = strcmp(method, "publickey-hostbound-v00@openssh.com") == 0;
101:
1.75 djm 102: if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
103: (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
104: (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
1.112 djm 105: fatal_fr(r, "parse %s packet", method);
106:
107: /* hostbound auth includes the hostkey offered at initial KEX */
108: if (hostbound) {
109: if ((r = sshpkt_getb_froms(ssh, &b)) != 0 ||
110: (r = sshkey_fromb(b, &hostkey)) != 0)
111: fatal_fr(r, "parse %s hostkey", method);
112: if (ssh->kex->initial_hostkey == NULL)
113: fatal_f("internal error: initial hostkey not recorded");
114: if (!sshkey_equal(hostkey, ssh->kex->initial_hostkey))
115: fatal_f("%s packet contained wrong host key", method);
116: sshbuf_free(b);
117: b = NULL;
118: }
1.87 djm 119:
120: if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
121: char *keystring;
122: struct sshbuf *pkbuf;
123:
124: if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
1.101 djm 125: fatal_f("sshbuf_from failed");
1.91 djm 126: if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
1.101 djm 127: fatal_f("sshbuf_dtob64 failed");
128: debug2_f("%s user %s %s public key %s %s",
1.87 djm 129: authctxt->valid ? "valid" : "invalid", authctxt->user,
130: have_sig ? "attempting" : "querying", pkalg, keystring);
131: sshbuf_free(pkbuf);
132: free(keystring);
133: }
134:
1.64 markus 135: pktype = sshkey_type_from_name(pkalg);
1.1 markus 136: if (pktype == KEY_UNSPEC) {
137: /* this is perfectly legal */
1.101 djm 138: verbose_f("unsupported public key algorithm: %s", pkalg);
1.1 markus 139: goto done;
140: }
1.64 markus 141: if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
1.101 djm 142: error_fr(r, "parse key");
1.64 markus 143: goto done;
144: }
1.1 markus 145: if (key == NULL) {
1.101 djm 146: error_f("cannot decode key: %s", pkalg);
1.1 markus 147: goto done;
148: }
149: if (key->type != pktype) {
1.101 djm 150: error_f("type mismatch for decoded key "
151: "(received %d, expected %d)", key->type, pktype);
1.1 markus 152: goto done;
153: }
1.68 djm 154: if (auth2_key_already_used(authctxt, key)) {
1.64 markus 155: logit("refusing previously-used %s key", sshkey_type(key));
1.44 djm 156: goto done;
157: }
1.104 dtucker 158: if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) {
1.113 naddy 159: logit_f("signature algorithm %s not in "
160: "PubkeyAcceptedAlgorithms", pkalg);
1.45 djm 161: goto done;
162: }
1.86 djm 163: if ((r = sshkey_check_cert_sigtype(key,
164: options.ca_sign_algorithms)) != 0) {
1.101 djm 165: logit_fr(r, "certificate signature algorithm %s",
1.86 djm 166: (key->cert == NULL || key->cert->signature_type == NULL) ?
1.101 djm 167: "(null)" : key->cert->signature_type);
1.117 djm 168: goto done;
169: }
170: if ((r = sshkey_check_rsa_length(key,
171: options.required_rsa_size)) != 0) {
172: logit_r(r, "refusing %s key", sshkey_type(key));
1.86 djm 173: goto done;
174: }
1.73 djm 175: key_s = format_key(key);
176: if (sshkey_is_cert(key))
177: ca_s = format_key(key->cert->signature_key);
178:
1.1 markus 179: if (have_sig) {
1.112 djm 180: debug3_f("%s have %s signature for %s%s%s",
181: method, pkalg, key_s,
1.101 djm 182: ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
1.64 markus 183: if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
184: (r = sshpkt_get_end(ssh)) != 0)
1.101 djm 185: fatal_fr(r, "parse signature packet");
1.64 markus 186: if ((b = sshbuf_new()) == NULL)
1.101 djm 187: fatal_f("sshbuf_new failed");
1.64 markus 188: if (ssh->compat & SSH_OLD_SESSIONID) {
1.106 djm 189: if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
1.101 djm 190: fatal_fr(r, "put old session id");
1.1 markus 191: } else {
1.106 djm 192: if ((r = sshbuf_put_stringb(b,
193: ssh->kex->session_id)) != 0)
1.101 djm 194: fatal_fr(r, "put session id");
1.1 markus 195: }
1.83 djm 196: if (!authctxt->valid || authctxt->user == NULL) {
1.101 djm 197: debug2_f("disabled because of invalid user");
1.83 djm 198: goto done;
199: }
1.1 markus 200: /* reconstruct packet */
1.35 djm 201: xasprintf(&userstyle, "%s%s%s", authctxt->user,
202: authctxt->style ? ":" : "",
203: authctxt->style ? authctxt->style : "");
1.64 markus 204: if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
205: (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
1.75 djm 206: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.111 djm 207: (r = sshbuf_put_cstring(b, method)) != 0 ||
1.75 djm 208: (r = sshbuf_put_u8(b, have_sig)) != 0 ||
1.85 mestre 209: (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
1.75 djm 210: (r = sshbuf_put_string(b, pkblob, blen)) != 0)
1.112 djm 211: fatal_fr(r, "reconstruct %s packet", method);
212: if (hostbound &&
213: (r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
214: fatal_fr(r, "reconstruct %s packet", method);
1.1 markus 215: #ifdef DEBUG_PK
1.64 markus 216: sshbuf_dump(b, stderr);
1.1 markus 217: #endif
218: /* test for correct signature */
219: authenticated = 0;
1.120 ! djm 220: if (mm_user_key_allowed(ssh, pw, key, 1, &authopts) &&
! 221: mm_sshkey_verify(key, sig, slen,
1.80 djm 222: sshbuf_ptr(b), sshbuf_len(b),
223: (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
1.120 ! djm 224: ssh->compat, &sig_details) == 0) {
1.1 markus 225: authenticated = 1;
1.44 djm 226: }
1.96 djm 227: if (authenticated == 1 && sig_details != NULL) {
228: auth2_record_info(authctxt, "signature count = %u",
229: sig_details->sk_counter);
1.101 djm 230: debug_f("sk_counter = %u, sk_flags = 0x%02x",
231: sig_details->sk_counter, sig_details->sk_flags);
1.96 djm 232: req_presence = (options.pubkey_auth_options &
1.97 djm 233: PUBKEYAUTH_TOUCH_REQUIRED) ||
234: !authopts->no_require_user_presence;
1.96 djm 235: if (req_presence && (sig_details->sk_flags &
236: SSH_SK_USER_PRESENCE_REQD) == 0) {
237: error("public key %s signature for %s%s from "
238: "%.128s port %d rejected: user presence "
1.99 naddy 239: "(authenticator touch) requirement "
240: "not met ", key_s,
1.100 djm 241: authctxt->valid ? "" : "invalid user ",
242: authctxt->user, ssh_remote_ipaddr(ssh),
243: ssh_remote_port(ssh));
244: authenticated = 0;
245: goto done;
246: }
247: req_verify = (options.pubkey_auth_options &
248: PUBKEYAUTH_VERIFY_REQUIRED) ||
249: authopts->require_verify;
250: if (req_verify && (sig_details->sk_flags &
251: SSH_SK_USER_VERIFICATION_REQD) == 0) {
252: error("public key %s signature for %s%s from "
253: "%.128s port %d rejected: user "
254: "verification requirement not met ", key_s,
1.96 djm 255: authctxt->valid ? "" : "invalid user ",
256: authctxt->user, ssh_remote_ipaddr(ssh),
257: ssh_remote_port(ssh));
258: authenticated = 0;
259: goto done;
260: }
1.95 djm 261: }
1.68 djm 262: auth2_record_key(authctxt, authenticated, key);
1.1 markus 263: } else {
1.112 djm 264: debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s,
1.101 djm 265: ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
1.73 djm 266:
1.64 markus 267: if ((r = sshpkt_get_end(ssh)) != 0)
1.101 djm 268: fatal_fr(r, "parse packet");
1.1 markus 269:
1.83 djm 270: if (!authctxt->valid || authctxt->user == NULL) {
1.101 djm 271: debug2_f("disabled because of invalid user");
1.83 djm 272: goto done;
273: }
1.1 markus 274: /* XXX fake reply and always send PK_OK ? */
275: /*
276: * XXX this allows testing whether a user is allowed
277: * to login: if you happen to have a valid pubkey this
278: * message is sent. the message is NEVER sent at all
279: * if a user is not allowed to login. is this an
280: * issue? -markus
281: */
1.120 ! djm 282: if (mm_user_key_allowed(ssh, pw, key, 0, NULL)) {
1.64 markus 283: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
284: != 0 ||
285: (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
286: (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
1.82 markus 287: (r = sshpkt_send(ssh)) != 0 ||
288: (r = ssh_packet_write_wait(ssh)) != 0)
1.101 djm 289: fatal_fr(r, "send packet");
1.1 markus 290: authctxt->postponed = 1;
291: }
292: }
293: done:
1.77 djm 294: if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
1.101 djm 295: debug_f("key options inconsistent with existing");
1.77 djm 296: authenticated = 0;
297: }
1.101 djm 298: debug2_f("authenticated %d pkalg %s", authenticated, pkalg);
1.77 djm 299:
1.84 djm 300: sshbuf_free(b);
1.77 djm 301: sshauthopt_free(authopts);
1.68 djm 302: sshkey_free(key);
1.112 djm 303: sshkey_free(hostkey);
1.64 markus 304: free(userstyle);
1.36 djm 305: free(pkalg);
306: free(pkblob);
1.73 djm 307: free(key_s);
308: free(ca_s);
1.83 djm 309: free(sig);
1.95 djm 310: sshkey_sig_details_free(sig_details);
1.1 markus 311: return authenticated;
312: }
313:
1.24 djm 314: static int
1.114 djm 315: match_principals_file(struct passwd *pw, char *file,
1.77 djm 316: struct sshkey_cert *cert, struct sshauthopt **authoptsp)
1.51 djm 317: {
318: FILE *f;
319: int success;
320:
1.77 djm 321: if (authoptsp != NULL)
322: *authoptsp = NULL;
323:
1.51 djm 324: temporarily_use_uid(pw);
325: debug("trying authorized principals file %s", file);
326: if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
327: restore_uid();
328: return 0;
329: }
1.115 djm 330: success = auth_process_principals(f, file, cert, authoptsp);
1.24 djm 331: fclose(f);
332: restore_uid();
1.51 djm 333: return success;
1.31 djm 334: }
1.24 djm 335:
1.31 djm 336: /*
1.51 djm 337: * Checks whether principal is allowed in output of command.
338: * returns 1 if the principal is allowed or 0 otherwise.
339: */
340: static int
1.119 djm 341: match_principals_command(struct passwd *user_pw, const struct sshkey *key,
342: const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
1.51 djm 343: {
1.77 djm 344: struct passwd *runas_pw = NULL;
1.56 djm 345: const struct sshkey_cert *cert = key->cert;
1.51 djm 346: FILE *f = NULL;
1.56 djm 347: int r, ok, found_principal = 0;
1.51 djm 348: int i, ac = 0, uid_swapped = 0;
349: pid_t pid;
350: char *tmp, *username = NULL, *command = NULL, **av = NULL;
1.56 djm 351: char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
1.88 djm 352: char serial_s[32], uidstr[32];
1.51 djm 353: void (*osigchld)(int);
354:
1.77 djm 355: if (authoptsp != NULL)
356: *authoptsp = NULL;
1.51 djm 357: if (options.authorized_principals_command == NULL)
358: return 0;
359: if (options.authorized_principals_command_user == NULL) {
360: error("No user for AuthorizedPrincipalsCommand specified, "
361: "skipping");
362: return 0;
363: }
364:
365: /*
366: * NB. all returns later this function should go via "out" to
367: * ensure the original SIGCHLD handler is restored properly.
368: */
1.98 dtucker 369: osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.51 djm 370:
371: /* Prepare and verify the user for the command */
372: username = percent_expand(options.authorized_principals_command_user,
373: "u", user_pw->pw_name, (char *)NULL);
1.77 djm 374: runas_pw = getpwnam(username);
375: if (runas_pw == NULL) {
1.51 djm 376: error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
377: username, strerror(errno));
378: goto out;
379: }
380:
381: /* Turn the command into an argument vector */
1.108 djm 382: if (argv_split(options.authorized_principals_command,
383: &ac, &av, 0) != 0) {
1.51 djm 384: error("AuthorizedPrincipalsCommand \"%s\" contains "
1.90 djm 385: "invalid quotes", options.authorized_principals_command);
1.51 djm 386: goto out;
387: }
388: if (ac == 0) {
389: error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
1.90 djm 390: options.authorized_principals_command);
1.51 djm 391: goto out;
392: }
1.56 djm 393: if ((ca_fp = sshkey_fingerprint(cert->signature_key,
394: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1.101 djm 395: error_f("sshkey_fingerprint failed");
1.56 djm 396: goto out;
397: }
1.57 djm 398: if ((key_fp = sshkey_fingerprint(key,
1.56 djm 399: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1.101 djm 400: error_f("sshkey_fingerprint failed");
1.56 djm 401: goto out;
402: }
403: if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
1.101 djm 404: error_fr(r, "sshkey_to_base64 failed");
1.56 djm 405: goto out;
406: }
407: if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1.101 djm 408: error_fr(r, "sshkey_to_base64 failed");
1.56 djm 409: goto out;
410: }
1.59 djm 411: snprintf(serial_s, sizeof(serial_s), "%llu",
412: (unsigned long long)cert->serial);
1.78 djm 413: snprintf(uidstr, sizeof(uidstr), "%llu",
414: (unsigned long long)user_pw->pw_uid);
1.51 djm 415: for (i = 1; i < ac; i++) {
416: tmp = percent_expand(av[i],
1.119 djm 417: "C", conn_id,
418: "D", rdomain,
1.78 djm 419: "U", uidstr,
1.51 djm 420: "u", user_pw->pw_name,
421: "h", user_pw->pw_dir,
1.56 djm 422: "t", sshkey_ssh_name(key),
423: "T", sshkey_ssh_name(cert->signature_key),
424: "f", key_fp,
425: "F", ca_fp,
426: "k", keytext,
427: "K", catext,
1.58 djm 428: "i", cert->key_id,
429: "s", serial_s,
1.51 djm 430: (char *)NULL);
431: if (tmp == NULL)
1.101 djm 432: fatal_f("percent_expand failed");
1.51 djm 433: free(av[i]);
434: av[i] = tmp;
435: }
436: /* Prepare a printable command for logs, etc. */
1.69 djm 437: command = argv_assemble(ac, av);
1.51 djm 438:
1.103 djm 439: if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
1.69 djm 440: ac, av, &f,
1.103 djm 441: SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
442: runas_pw, temporarily_use_uid, restore_uid)) == 0)
1.51 djm 443: goto out;
444:
445: uid_swapped = 1;
1.77 djm 446: temporarily_use_uid(runas_pw);
1.51 djm 447:
1.115 djm 448: ok = auth_process_principals(f, "(command)", cert, authoptsp);
1.51 djm 449:
1.61 djm 450: fclose(f);
451: f = NULL;
452:
1.70 djm 453: if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
1.51 djm 454: goto out;
455:
456: /* Read completed successfully */
457: found_principal = ok;
458: out:
459: if (f != NULL)
460: fclose(f);
1.98 dtucker 461: ssh_signal(SIGCHLD, osigchld);
1.51 djm 462: for (i = 0; i < ac; i++)
463: free(av[i]);
464: free(av);
465: if (uid_swapped)
466: restore_uid();
467: free(command);
468: free(username);
1.56 djm 469: free(ca_fp);
470: free(key_fp);
471: free(catext);
472: free(keytext);
1.51 djm 473: return found_principal;
1.77 djm 474: }
475:
1.21 djm 476: /* Authenticate a certificate key against TrustedUserCAKeys */
477: static int
1.114 djm 478: user_cert_trusted_ca(struct passwd *pw, struct sshkey *key,
479: const char *remote_ip, const char *remote_host,
1.119 djm 480: const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
1.21 djm 481: {
1.24 djm 482: char *ca_fp, *principals_file = NULL;
1.21 djm 483: const char *reason;
1.77 djm 484: struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
485: struct sshauthopt *final_opts = NULL;
1.64 markus 486: int r, ret = 0, found_principal = 0, use_authorized_principals;
1.21 djm 487:
1.77 djm 488: if (authoptsp != NULL)
489: *authoptsp = NULL;
490:
1.64 markus 491: if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
1.21 djm 492: return 0;
493:
1.46 djm 494: if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
495: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
496: return 0;
1.21 djm 497:
1.64 markus 498: if ((r = sshkey_in_file(key->cert->signature_key,
499: options.trusted_user_ca_keys, 1, 0)) != 0) {
1.101 djm 500: debug2_fr(r, "CA %s %s is not listed in %s",
1.64 markus 501: sshkey_type(key->cert->signature_key), ca_fp,
1.101 djm 502: options.trusted_user_ca_keys);
1.21 djm 503: goto out;
504: }
1.24 djm 505: /*
506: * If AuthorizedPrincipals is in use, then compare the certificate
507: * principals against the names in that file rather than matching
508: * against the username.
509: */
510: if ((principals_file = authorized_principals_file(pw)) != NULL) {
1.114 djm 511: if (match_principals_file(pw, principals_file,
1.77 djm 512: key->cert, &principals_opts))
1.51 djm 513: found_principal = 1;
514: }
515: /* Try querying command if specified */
1.114 djm 516: if (!found_principal && match_principals_command(pw, key,
1.119 djm 517: conn_id, rdomain, &principals_opts))
1.51 djm 518: found_principal = 1;
1.53 jsing 519: /* If principals file or command is specified, then require a match */
520: use_authorized_principals = principals_file != NULL ||
1.107 djm 521: options.authorized_principals_command != NULL;
1.53 jsing 522: if (!found_principal && use_authorized_principals) {
1.51 djm 523: reason = "Certificate does not contain an authorized principal";
1.77 djm 524: goto fail_reason;
1.21 djm 525: }
1.77 djm 526: if (use_authorized_principals && principals_opts == NULL)
1.101 djm 527: fatal_f("internal error: missing principals_opts");
1.109 djm 528: if (sshkey_cert_check_authority_now(key, 0, 1, 0,
1.53 jsing 529: use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
1.24 djm 530: goto fail_reason;
1.77 djm 531:
532: /* Check authority from options in key and from principals file/cmd */
533: if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
534: reason = "Invalid certificate options";
535: goto fail_reason;
536: }
1.114 djm 537: if (auth_authorise_keyopts(pw, cert_opts, 0,
538: remote_ip, remote_host, "cert") != 0) {
1.77 djm 539: reason = "Refused by certificate options";
1.60 djm 540: goto fail_reason;
1.77 djm 541: }
542: if (principals_opts == NULL) {
543: final_opts = cert_opts;
544: cert_opts = NULL;
545: } else {
1.114 djm 546: if (auth_authorise_keyopts(pw, principals_opts, 0,
547: remote_ip, remote_host, "principals") != 0) {
1.77 djm 548: reason = "Refused by certificate principals options";
549: goto fail_reason;
550: }
551: if ((final_opts = sshauthopt_merge(principals_opts,
552: cert_opts, &reason)) == NULL) {
553: fail_reason:
554: error("%s", reason);
555: auth_debug_add("%s", reason);
556: goto out;
557: }
558: }
1.21 djm 559:
1.77 djm 560: /* Success */
1.54 djm 561: verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
562: "%s CA %s via %s", key->cert->key_id,
563: (unsigned long long)key->cert->serial,
1.64 markus 564: sshkey_type(key->cert->signature_key), ca_fp,
1.22 djm 565: options.trusted_user_ca_keys);
1.77 djm 566: if (authoptsp != NULL) {
567: *authoptsp = final_opts;
568: final_opts = NULL;
569: }
1.21 djm 570: ret = 1;
571: out:
1.77 djm 572: sshauthopt_free(principals_opts);
573: sshauthopt_free(cert_opts);
574: sshauthopt_free(final_opts);
1.36 djm 575: free(principals_file);
576: free(ca_fp);
1.21 djm 577: return ret;
578: }
579:
1.31 djm 580: /*
581: * Checks whether key is allowed in file.
582: * returns 1 if the key is allowed or 0 otherwise.
583: */
584: static int
1.114 djm 585: user_key_allowed2(struct passwd *pw, struct sshkey *key,
586: char *file, const char *remote_ip, const char *remote_host,
587: struct sshauthopt **authoptsp)
1.31 djm 588: {
589: FILE *f;
590: int found_key = 0;
591:
1.77 djm 592: if (authoptsp != NULL)
593: *authoptsp = NULL;
594:
1.31 djm 595: /* Temporarily use the user's uid. */
596: temporarily_use_uid(pw);
597:
598: debug("trying public key file %s", file);
599: if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
1.115 djm 600: found_key = auth_check_authkeys_file(pw, f, file,
1.114 djm 601: key, remote_ip, remote_host, authoptsp);
1.31 djm 602: fclose(f);
603: }
604:
605: restore_uid();
606: return found_key;
607: }
608:
609: /*
610: * Checks whether key is allowed in output of command.
611: * returns 1 if the key is allowed or 0 otherwise.
612: */
613: static int
1.114 djm 614: user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key,
615: const char *remote_ip, const char *remote_host,
1.119 djm 616: const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
1.31 djm 617: {
1.77 djm 618: struct passwd *runas_pw = NULL;
1.50 djm 619: FILE *f = NULL;
620: int r, ok, found_key = 0;
621: int i, uid_swapped = 0, ac = 0;
1.31 djm 622: pid_t pid;
1.50 djm 623: char *username = NULL, *key_fp = NULL, *keytext = NULL;
1.78 djm 624: char uidstr[32], *tmp, *command = NULL, **av = NULL;
1.50 djm 625: void (*osigchld)(int);
1.31 djm 626:
1.77 djm 627: if (authoptsp != NULL)
628: *authoptsp = NULL;
1.50 djm 629: if (options.authorized_keys_command == NULL)
1.31 djm 630: return 0;
1.32 djm 631: if (options.authorized_keys_command_user == NULL) {
632: error("No user for AuthorizedKeysCommand specified, skipping");
633: return 0;
634: }
635:
1.50 djm 636: /*
637: * NB. all returns later this function should go via "out" to
638: * ensure the original SIGCHLD handler is restored properly.
639: */
1.98 dtucker 640: osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.50 djm 641:
642: /* Prepare and verify the user for the command */
1.32 djm 643: username = percent_expand(options.authorized_keys_command_user,
644: "u", user_pw->pw_name, (char *)NULL);
1.77 djm 645: runas_pw = getpwnam(username);
646: if (runas_pw == NULL) {
1.34 djm 647: error("AuthorizedKeysCommandUser \"%s\" not found: %s",
648: username, strerror(errno));
1.50 djm 649: goto out;
1.31 djm 650: }
651:
1.50 djm 652: /* Prepare AuthorizedKeysCommand */
653: if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
654: SSH_FP_DEFAULT)) == NULL) {
1.101 djm 655: error_f("sshkey_fingerprint failed");
1.31 djm 656: goto out;
657: }
1.50 djm 658: if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1.101 djm 659: error_fr(r, "sshkey_to_base64 failed");
1.31 djm 660: goto out;
661: }
662:
1.50 djm 663: /* Turn the command into an argument vector */
1.108 djm 664: if (argv_split(options.authorized_keys_command, &ac, &av, 0) != 0) {
1.50 djm 665: error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1.102 djm 666: options.authorized_keys_command);
1.50 djm 667: goto out;
668: }
669: if (ac == 0) {
670: error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1.102 djm 671: options.authorized_keys_command);
1.31 djm 672: goto out;
673: }
1.78 djm 674: snprintf(uidstr, sizeof(uidstr), "%llu",
675: (unsigned long long)user_pw->pw_uid);
1.50 djm 676: for (i = 1; i < ac; i++) {
677: tmp = percent_expand(av[i],
1.119 djm 678: "C", conn_id,
679: "D", rdomain,
1.78 djm 680: "U", uidstr,
1.50 djm 681: "u", user_pw->pw_name,
682: "h", user_pw->pw_dir,
683: "t", sshkey_ssh_name(key),
684: "f", key_fp,
685: "k", keytext,
686: (char *)NULL);
687: if (tmp == NULL)
1.101 djm 688: fatal_f("percent_expand failed");
1.50 djm 689: free(av[i]);
690: av[i] = tmp;
691: }
692: /* Prepare a printable command for logs, etc. */
1.69 djm 693: command = argv_assemble(ac, av);
1.31 djm 694:
695: /*
1.50 djm 696: * If AuthorizedKeysCommand was run without arguments
697: * then fall back to the old behaviour of passing the
698: * target username as a single argument.
1.31 djm 699: */
1.50 djm 700: if (ac == 1) {
701: av = xreallocarray(av, ac + 2, sizeof(*av));
702: av[1] = xstrdup(user_pw->pw_name);
703: av[2] = NULL;
704: /* Fix up command too, since it is used in log messages */
705: free(command);
706: xasprintf(&command, "%s %s", av[0], av[1]);
707: }
1.31 djm 708:
1.103 djm 709: if ((pid = subprocess("AuthorizedKeysCommand", command,
1.69 djm 710: ac, av, &f,
1.103 djm 711: SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
712: runas_pw, temporarily_use_uid, restore_uid)) == 0)
1.50 djm 713: goto out;
1.31 djm 714:
1.50 djm 715: uid_swapped = 1;
1.77 djm 716: temporarily_use_uid(runas_pw);
1.31 djm 717:
1.115 djm 718: ok = auth_check_authkeys_file(user_pw, f,
1.114 djm 719: options.authorized_keys_command, key, remote_ip,
720: remote_host, authoptsp);
1.61 djm 721:
722: fclose(f);
723: f = NULL;
1.31 djm 724:
1.70 djm 725: if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
1.31 djm 726: goto out;
1.50 djm 727:
728: /* Read completed successfully */
1.31 djm 729: found_key = ok;
730: out:
1.50 djm 731: if (f != NULL)
732: fclose(f);
1.98 dtucker 733: ssh_signal(SIGCHLD, osigchld);
1.50 djm 734: for (i = 0; i < ac; i++)
735: free(av[i]);
736: free(av);
737: if (uid_swapped)
738: restore_uid();
739: free(command);
740: free(username);
741: free(key_fp);
742: free(keytext);
1.31 djm 743: return found_key;
744: }
745:
746: /*
747: * Check whether key authenticates and authorises the user.
748: */
1.1 markus 749: int
1.116 djm 750: user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
751: int auth_attempt, struct sshauthopt **authoptsp)
1.1 markus 752: {
1.89 djm 753: u_int success = 0, i;
1.119 djm 754: char *file, *conn_id;
1.77 djm 755: struct sshauthopt *opts = NULL;
1.119 djm 756: const char *rdomain, *remote_ip, *remote_host;
1.89 djm 757:
1.77 djm 758: if (authoptsp != NULL)
759: *authoptsp = NULL;
1.21 djm 760:
761: if (auth_key_is_revoked(key))
762: return 0;
1.64 markus 763: if (sshkey_is_cert(key) &&
764: auth_key_is_revoked(key->cert->signature_key))
1.21 djm 765: return 0;
766:
1.119 djm 767: if ((rdomain = ssh_packet_rdomain_in(ssh)) == NULL)
768: rdomain = "";
769: remote_ip = ssh_remote_ipaddr(ssh);
770: remote_host = auth_get_canonical_hostname(ssh, options.use_dns);
771: xasprintf(&conn_id, "%s %d %s %d",
772: ssh_local_ipaddr(ssh), ssh_local_port(ssh),
773: remote_ip, ssh_remote_port(ssh));
774:
1.89 djm 775: for (i = 0; !success && i < options.num_authkeys_files; i++) {
776: if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
777: continue;
778: file = expand_authorized_keys(
779: options.authorized_keys_files[i], pw);
1.114 djm 780: success = user_key_allowed2(pw, key, file,
781: remote_ip, remote_host, &opts);
1.89 djm 782: free(file);
783: if (!success) {
784: sshauthopt_free(opts);
785: opts = NULL;
786: }
787: }
788: if (success)
789: goto out;
790:
1.114 djm 791: if ((success = user_cert_trusted_ca(pw, key, remote_ip, remote_host,
1.119 djm 792: conn_id, rdomain, &opts)) != 0)
1.77 djm 793: goto out;
794: sshauthopt_free(opts);
795: opts = NULL;
796:
1.114 djm 797: if ((success = user_key_command_allowed2(pw, key, remote_ip,
1.119 djm 798: remote_host, conn_id, rdomain, &opts)) != 0)
1.77 djm 799: goto out;
800: sshauthopt_free(opts);
801: opts = NULL;
1.1 markus 802:
1.77 djm 803: out:
1.119 djm 804: free(conn_id);
1.77 djm 805: if (success && authoptsp != NULL) {
806: *authoptsp = opts;
807: opts = NULL;
808: }
809: sshauthopt_free(opts);
1.1 markus 810: return success;
811: }
1.2 markus 812:
813: Authmethod method_pubkey = {
1.120 ! djm 814: &methodcfg_pubkey,
1.2 markus 815: userauth_pubkey,
816: };