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