1 /* $OpenBSD: auth2-pubkey.c,v 1.113 2022/02/27 01:33:59 naddy Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
59 #include "pathnames.h"
61 #include "auth-options.h"
66 #include "monitor_wrap.h"
71 #include "channels.h" /* XXX for session.h */
72 #include "session.h" /* XXX for child_set_env(); refactor? */
76 extern ServerOptions options;
79 format_key(const struct sshkey *key)
81 char *ret, *fp = sshkey_fingerprint(key,
82 options.fingerprint_hash, SSH_FP_DEFAULT);
84 xasprintf(&ret, "%s %s", sshkey_type(key), fp);
90 userauth_pubkey(struct ssh *ssh, const char *method)
92 Authctxt *authctxt = ssh->authctxt;
93 struct passwd *pw = authctxt->pw;
94 struct sshbuf *b = NULL;
95 struct sshkey *key = NULL, *hostkey = NULL;
96 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
97 u_char *pkblob = NULL, *sig = NULL, have_sig;
99 int hostbound, r, pktype;
100 int req_presence = 0, req_verify = 0, authenticated = 0;
101 struct sshauthopt *authopts = NULL;
102 struct sshkey_sig_details *sig_details = NULL;
104 hostbound = strcmp(method, "publickey-hostbound-v00@openssh.com") == 0;
106 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
107 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
108 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
109 fatal_fr(r, "parse %s packet", method);
111 /* hostbound auth includes the hostkey offered at initial KEX */
113 if ((r = sshpkt_getb_froms(ssh, &b)) != 0 ||
114 (r = sshkey_fromb(b, &hostkey)) != 0)
115 fatal_fr(r, "parse %s hostkey", method);
116 if (ssh->kex->initial_hostkey == NULL)
117 fatal_f("internal error: initial hostkey not recorded");
118 if (!sshkey_equal(hostkey, ssh->kex->initial_hostkey))
119 fatal_f("%s packet contained wrong host key", method);
124 if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
126 struct sshbuf *pkbuf;
128 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
129 fatal_f("sshbuf_from failed");
130 if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
131 fatal_f("sshbuf_dtob64 failed");
132 debug2_f("%s user %s %s public key %s %s",
133 authctxt->valid ? "valid" : "invalid", authctxt->user,
134 have_sig ? "attempting" : "querying", pkalg, keystring);
139 pktype = sshkey_type_from_name(pkalg);
140 if (pktype == KEY_UNSPEC) {
141 /* this is perfectly legal */
142 verbose_f("unsupported public key algorithm: %s", pkalg);
145 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
146 error_fr(r, "parse key");
150 error_f("cannot decode key: %s", pkalg);
153 if (key->type != pktype) {
154 error_f("type mismatch for decoded key "
155 "(received %d, expected %d)", key->type, pktype);
158 if (sshkey_type_plain(key->type) == KEY_RSA &&
159 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
160 logit("Refusing RSA key because client uses unsafe "
164 if (auth2_key_already_used(authctxt, key)) {
165 logit("refusing previously-used %s key", sshkey_type(key));
168 if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) {
169 logit_f("signature algorithm %s not in "
170 "PubkeyAcceptedAlgorithms", pkalg);
173 if ((r = sshkey_check_cert_sigtype(key,
174 options.ca_sign_algorithms)) != 0) {
175 logit_fr(r, "certificate signature algorithm %s",
176 (key->cert == NULL || key->cert->signature_type == NULL) ?
177 "(null)" : key->cert->signature_type);
180 key_s = format_key(key);
181 if (sshkey_is_cert(key))
182 ca_s = format_key(key->cert->signature_key);
185 debug3_f("%s have %s signature for %s%s%s",
186 method, pkalg, key_s,
187 ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
188 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
189 (r = sshpkt_get_end(ssh)) != 0)
190 fatal_fr(r, "parse signature packet");
191 if ((b = sshbuf_new()) == NULL)
192 fatal_f("sshbuf_new failed");
193 if (ssh->compat & SSH_OLD_SESSIONID) {
194 if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
195 fatal_fr(r, "put old session id");
197 if ((r = sshbuf_put_stringb(b,
198 ssh->kex->session_id)) != 0)
199 fatal_fr(r, "put session id");
201 if (!authctxt->valid || authctxt->user == NULL) {
202 debug2_f("disabled because of invalid user");
205 /* reconstruct packet */
206 xasprintf(&userstyle, "%s%s%s", authctxt->user,
207 authctxt->style ? ":" : "",
208 authctxt->style ? authctxt->style : "");
209 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
210 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
211 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
212 (r = sshbuf_put_cstring(b, method)) != 0 ||
213 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
214 (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
215 (r = sshbuf_put_string(b, pkblob, blen)) != 0)
216 fatal_fr(r, "reconstruct %s packet", method);
218 (r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
219 fatal_fr(r, "reconstruct %s packet", method);
221 sshbuf_dump(b, stderr);
223 /* test for correct signature */
225 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
226 PRIVSEP(sshkey_verify(key, sig, slen,
227 sshbuf_ptr(b), sshbuf_len(b),
228 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
229 ssh->compat, &sig_details)) == 0) {
232 if (authenticated == 1 && sig_details != NULL) {
233 auth2_record_info(authctxt, "signature count = %u",
234 sig_details->sk_counter);
235 debug_f("sk_counter = %u, sk_flags = 0x%02x",
236 sig_details->sk_counter, sig_details->sk_flags);
237 req_presence = (options.pubkey_auth_options &
238 PUBKEYAUTH_TOUCH_REQUIRED) ||
239 !authopts->no_require_user_presence;
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 "
244 "(authenticator touch) requirement "
246 authctxt->valid ? "" : "invalid user ",
247 authctxt->user, ssh_remote_ipaddr(ssh),
248 ssh_remote_port(ssh));
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,
260 authctxt->valid ? "" : "invalid user ",
261 authctxt->user, ssh_remote_ipaddr(ssh),
262 ssh_remote_port(ssh));
267 auth2_record_key(authctxt, authenticated, key);
269 debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s,
270 ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
272 if ((r = sshpkt_get_end(ssh)) != 0)
273 fatal_fr(r, "parse packet");
275 if (!authctxt->valid || authctxt->user == NULL) {
276 debug2_f("disabled because of invalid user");
279 /* XXX fake reply and always send PK_OK ? */
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
287 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
288 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
290 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
291 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
292 (r = sshpkt_send(ssh)) != 0 ||
293 (r = ssh_packet_write_wait(ssh)) != 0)
294 fatal_fr(r, "send packet");
295 authctxt->postponed = 1;
299 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
300 debug_f("key options inconsistent with existing");
303 debug2_f("authenticated %d pkalg %s", authenticated, pkalg);
306 sshauthopt_free(authopts);
308 sshkey_free(hostkey);
315 sshkey_sig_details_free(sig_details);
316 return authenticated;
320 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
325 /* XXX percent_expand() sequences for authorized_principals? */
327 for (i = 0; i < cert->nprincipals; i++) {
328 if ((result = match_list(cert->principals[i],
329 principal_list, NULL)) != NULL) {
330 debug3("matched principal from key options \"%.100s\"",
340 * Process a single authorized_principals format line. Returns 0 and sets
341 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
342 * log preamble for file/line information.
345 check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
346 const char *loc, struct sshauthopt **authoptsp)
349 char *ep, *line_opts;
350 const char *reason = NULL;
351 struct sshauthopt *opts = NULL;
353 if (authoptsp != NULL)
356 /* Trim trailing whitespace. */
357 ep = cp + strlen(cp) - 1;
358 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
362 * If the line has internal whitespace then assume it has
366 if ((ep = strrchr(cp, ' ')) != NULL ||
367 (ep = strrchr(cp, '\t')) != NULL) {
368 for (; *ep == ' ' || *ep == '\t'; ep++)
373 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
374 debug("%s: bad principals options: %s", loc, reason);
375 auth_debug_add("%s: bad principals options: %s", loc, reason);
378 /* Check principals in cert against those on line */
379 for (i = 0; i < cert->nprincipals; i++) {
380 if (strcmp(cp, cert->principals[i]) != 0)
382 debug3("%s: matched principal \"%.100s\"",
383 loc, cert->principals[i]);
386 if (found && authoptsp != NULL) {
390 sshauthopt_free(opts);
391 return found ? 0 : -1;
395 process_principals(struct ssh *ssh, FILE *f, const char *file,
396 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
398 char loc[256], *line = NULL, *cp, *ep;
400 u_long linenum = 0, nonblank = 0;
401 u_int found_principal = 0;
403 if (authoptsp != NULL)
406 while (getline(&line, &linesize, f) != -1) {
408 /* Always consume entire input */
412 /* Skip leading whitespace. */
413 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
415 /* Skip blank and comment lines. */
416 if ((ep = strchr(cp, '#')) != NULL)
418 if (!*cp || *cp == '\n')
422 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
423 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
426 debug2_f("%s: processed %lu/%lu lines", file, nonblank, linenum);
428 return found_principal;
431 /* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
434 match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
435 struct sshkey_cert *cert, struct sshauthopt **authoptsp)
440 if (authoptsp != NULL)
443 temporarily_use_uid(pw);
444 debug("trying authorized principals file %s", file);
445 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
449 success = process_principals(ssh, f, file, cert, authoptsp);
456 * Checks whether principal is allowed in output of command.
457 * returns 1 if the principal is allowed or 0 otherwise.
460 match_principals_command(struct ssh *ssh, struct passwd *user_pw,
461 const struct sshkey *key, struct sshauthopt **authoptsp)
463 struct passwd *runas_pw = NULL;
464 const struct sshkey_cert *cert = key->cert;
466 int r, ok, found_principal = 0;
467 int i, ac = 0, uid_swapped = 0;
469 char *tmp, *username = NULL, *command = NULL, **av = NULL;
470 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
471 char serial_s[32], uidstr[32];
472 void (*osigchld)(int);
474 if (authoptsp != NULL)
476 if (options.authorized_principals_command == NULL)
478 if (options.authorized_principals_command_user == NULL) {
479 error("No user for AuthorizedPrincipalsCommand specified, "
485 * NB. all returns later this function should go via "out" to
486 * ensure the original SIGCHLD handler is restored properly.
488 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
490 /* Prepare and verify the user for the command */
491 username = percent_expand(options.authorized_principals_command_user,
492 "u", user_pw->pw_name, (char *)NULL);
493 runas_pw = getpwnam(username);
494 if (runas_pw == NULL) {
495 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
496 username, strerror(errno));
500 /* Turn the command into an argument vector */
501 if (argv_split(options.authorized_principals_command,
503 error("AuthorizedPrincipalsCommand \"%s\" contains "
504 "invalid quotes", options.authorized_principals_command);
508 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
509 options.authorized_principals_command);
512 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
513 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
514 error_f("sshkey_fingerprint failed");
517 if ((key_fp = sshkey_fingerprint(key,
518 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
519 error_f("sshkey_fingerprint failed");
522 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
523 error_fr(r, "sshkey_to_base64 failed");
526 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
527 error_fr(r, "sshkey_to_base64 failed");
530 snprintf(serial_s, sizeof(serial_s), "%llu",
531 (unsigned long long)cert->serial);
532 snprintf(uidstr, sizeof(uidstr), "%llu",
533 (unsigned long long)user_pw->pw_uid);
534 for (i = 1; i < ac; i++) {
535 tmp = percent_expand(av[i],
537 "u", user_pw->pw_name,
538 "h", user_pw->pw_dir,
539 "t", sshkey_ssh_name(key),
540 "T", sshkey_ssh_name(cert->signature_key),
549 fatal_f("percent_expand failed");
553 /* Prepare a printable command for logs, etc. */
554 command = argv_assemble(ac, av);
556 if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
558 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
559 runas_pw, temporarily_use_uid, restore_uid)) == 0)
563 temporarily_use_uid(runas_pw);
565 ok = process_principals(ssh, f, "(command)", cert, authoptsp);
570 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
573 /* Read completed successfully */
574 found_principal = ok;
578 ssh_signal(SIGCHLD, osigchld);
579 for (i = 0; i < ac; i++)
590 return found_principal;
594 * Check a single line of an authorized_keys-format file. Returns 0 if key
595 * matches, -1 otherwise. Will return key/cert options via *authoptsp
596 * on success. "loc" is used as file/line location in log messages.
599 check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
600 char *cp, const char *loc, struct sshauthopt **authoptsp)
602 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
603 struct sshkey *found = NULL;
604 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
605 char *key_options = NULL, *fp = NULL;
606 const char *reason = NULL;
609 if (authoptsp != NULL)
612 if ((found = sshkey_new(want_keytype)) == NULL) {
613 debug3_f("keytype %d failed", want_keytype);
617 /* XXX djm: peek at key type in line and skip if unwanted */
619 if (sshkey_read(found, &cp) != 0) {
620 /* no key? check for options */
621 debug2("%s: check options: '%s'", loc, cp);
623 if (sshkey_advance_past_options(&cp) != 0) {
624 reason = "invalid key option string";
628 if (sshkey_read(found, &cp) != 0) {
629 /* still no key? advance to next line*/
630 debug2("%s: advance: '%s'", loc, cp);
634 /* Parse key options now; we need to know if this is a CA key */
635 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
636 debug("%s: bad key options: %s", loc, reason);
637 auth_debug_add("%s: bad key options: %s", loc, reason);
640 /* Ignore keys that don't match or incorrectly marked as CAs */
641 if (sshkey_is_cert(key)) {
642 /* Certificate; check signature key against CA */
643 if (!sshkey_equal(found, key->cert->signature_key) ||
644 !keyopts->cert_authority)
647 /* Plain key: check it against key found in file */
648 if (!sshkey_equal(found, key) || keyopts->cert_authority)
652 /* We have a candidate key, perform authorisation checks */
653 if ((fp = sshkey_fingerprint(found,
654 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
655 fatal_f("fingerprint failed");
657 debug("%s: matching %s found: %s %s", loc,
658 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
660 if (auth_authorise_keyopts(ssh, pw, keyopts,
661 sshkey_is_cert(key), loc) != 0) {
662 reason = "Refused by key options";
665 /* That's all we need for plain keys. */
666 if (!sshkey_is_cert(key)) {
667 verbose("Accepted key %s %s found at %s",
668 sshkey_type(found), fp, loc);
675 * Additional authorisation for certificates.
678 /* Parse and check options present in certificate */
679 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
680 reason = "Invalid certificate options";
683 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
684 reason = "Refused by certificate options";
687 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
691 * If the user has specified a list of principals as
692 * a key option, then prefer that list to matching
693 * their username in the certificate principals list.
695 if (keyopts->cert_principals != NULL &&
696 !match_principals_option(keyopts->cert_principals, key->cert)) {
697 reason = "Certificate does not contain an authorized principal";
700 if (sshkey_cert_check_authority_now(key, 0, 0, 0,
701 keyopts->cert_principals == NULL ? pw->pw_name : NULL,
705 verbose("Accepted certificate ID \"%s\" (serial %llu) "
706 "signed by CA %s %s found at %s",
708 (unsigned long long)key->cert->serial,
709 sshkey_type(found), fp, loc);
712 if (finalopts == NULL)
713 fatal_f("internal error: missing options");
714 if (authoptsp != NULL) {
715 *authoptsp = finalopts;
724 auth_debug_add("%s", reason);
727 sshauthopt_free(keyopts);
728 sshauthopt_free(certopts);
729 sshauthopt_free(finalopts);
735 * Checks whether key is allowed in authorized_keys-format file,
736 * returns 1 if the key is allowed or 0 otherwise.
739 check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
740 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
742 char *cp, *line = NULL, loc[256];
745 u_long linenum = 0, nonblank = 0;
747 if (authoptsp != NULL)
750 while (getline(&line, &linesize, f) != -1) {
752 /* Always consume entire file */
756 /* Skip leading whitespace, empty and comment lines. */
759 if (!*cp || *cp == '\n' || *cp == '#')
763 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
764 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
768 debug2_f("%s: processed %lu/%lu lines", file, nonblank, linenum);
772 /* Authenticate a certificate key against TrustedUserCAKeys */
774 user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
775 struct sshauthopt **authoptsp)
777 char *ca_fp, *principals_file = NULL;
779 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
780 struct sshauthopt *final_opts = NULL;
781 int r, ret = 0, found_principal = 0, use_authorized_principals;
783 if (authoptsp != NULL)
786 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
789 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
790 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
793 if ((r = sshkey_in_file(key->cert->signature_key,
794 options.trusted_user_ca_keys, 1, 0)) != 0) {
795 debug2_fr(r, "CA %s %s is not listed in %s",
796 sshkey_type(key->cert->signature_key), ca_fp,
797 options.trusted_user_ca_keys);
801 * If AuthorizedPrincipals is in use, then compare the certificate
802 * principals against the names in that file rather than matching
803 * against the username.
805 if ((principals_file = authorized_principals_file(pw)) != NULL) {
806 if (match_principals_file(ssh, pw, principals_file,
807 key->cert, &principals_opts))
810 /* Try querying command if specified */
811 if (!found_principal && match_principals_command(ssh, pw, key,
814 /* If principals file or command is specified, then require a match */
815 use_authorized_principals = principals_file != NULL ||
816 options.authorized_principals_command != NULL;
817 if (!found_principal && use_authorized_principals) {
818 reason = "Certificate does not contain an authorized principal";
821 if (use_authorized_principals && principals_opts == NULL)
822 fatal_f("internal error: missing principals_opts");
823 if (sshkey_cert_check_authority_now(key, 0, 1, 0,
824 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
827 /* Check authority from options in key and from principals file/cmd */
828 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
829 reason = "Invalid certificate options";
832 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
833 reason = "Refused by certificate options";
836 if (principals_opts == NULL) {
837 final_opts = cert_opts;
840 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
841 "principals") != 0) {
842 reason = "Refused by certificate principals options";
845 if ((final_opts = sshauthopt_merge(principals_opts,
846 cert_opts, &reason)) == NULL) {
849 auth_debug_add("%s", reason);
855 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
856 "%s CA %s via %s", key->cert->key_id,
857 (unsigned long long)key->cert->serial,
858 sshkey_type(key->cert->signature_key), ca_fp,
859 options.trusted_user_ca_keys);
860 if (authoptsp != NULL) {
861 *authoptsp = final_opts;
866 sshauthopt_free(principals_opts);
867 sshauthopt_free(cert_opts);
868 sshauthopt_free(final_opts);
869 free(principals_file);
875 * Checks whether key is allowed in file.
876 * returns 1 if the key is allowed or 0 otherwise.
879 user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
880 char *file, struct sshauthopt **authoptsp)
885 if (authoptsp != NULL)
888 /* Temporarily use the user's uid. */
889 temporarily_use_uid(pw);
891 debug("trying public key file %s", file);
892 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
893 found_key = check_authkeys_file(ssh, pw, f, file,
903 * Checks whether key is allowed in output of command.
904 * returns 1 if the key is allowed or 0 otherwise.
907 user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
908 struct sshkey *key, struct sshauthopt **authoptsp)
910 struct passwd *runas_pw = NULL;
912 int r, ok, found_key = 0;
913 int i, uid_swapped = 0, ac = 0;
915 char *username = NULL, *key_fp = NULL, *keytext = NULL;
916 char uidstr[32], *tmp, *command = NULL, **av = NULL;
917 void (*osigchld)(int);
919 if (authoptsp != NULL)
921 if (options.authorized_keys_command == NULL)
923 if (options.authorized_keys_command_user == NULL) {
924 error("No user for AuthorizedKeysCommand specified, skipping");
929 * NB. all returns later this function should go via "out" to
930 * ensure the original SIGCHLD handler is restored properly.
932 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
934 /* Prepare and verify the user for the command */
935 username = percent_expand(options.authorized_keys_command_user,
936 "u", user_pw->pw_name, (char *)NULL);
937 runas_pw = getpwnam(username);
938 if (runas_pw == NULL) {
939 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
940 username, strerror(errno));
944 /* Prepare AuthorizedKeysCommand */
945 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
946 SSH_FP_DEFAULT)) == NULL) {
947 error_f("sshkey_fingerprint failed");
950 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
951 error_fr(r, "sshkey_to_base64 failed");
955 /* Turn the command into an argument vector */
956 if (argv_split(options.authorized_keys_command, &ac, &av, 0) != 0) {
957 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
958 options.authorized_keys_command);
962 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
963 options.authorized_keys_command);
966 snprintf(uidstr, sizeof(uidstr), "%llu",
967 (unsigned long long)user_pw->pw_uid);
968 for (i = 1; i < ac; i++) {
969 tmp = percent_expand(av[i],
971 "u", user_pw->pw_name,
972 "h", user_pw->pw_dir,
973 "t", sshkey_ssh_name(key),
978 fatal_f("percent_expand failed");
982 /* Prepare a printable command for logs, etc. */
983 command = argv_assemble(ac, av);
986 * If AuthorizedKeysCommand was run without arguments
987 * then fall back to the old behaviour of passing the
988 * target username as a single argument.
991 av = xreallocarray(av, ac + 2, sizeof(*av));
992 av[1] = xstrdup(user_pw->pw_name);
994 /* Fix up command too, since it is used in log messages */
996 xasprintf(&command, "%s %s", av[0], av[1]);
999 if ((pid = subprocess("AuthorizedKeysCommand", command,
1001 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
1002 runas_pw, temporarily_use_uid, restore_uid)) == 0)
1006 temporarily_use_uid(runas_pw);
1008 ok = check_authkeys_file(ssh, user_pw, f,
1009 options.authorized_keys_command, key, authoptsp);
1014 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
1017 /* Read completed successfully */
1022 ssh_signal(SIGCHLD, osigchld);
1023 for (i = 0; i < ac; i++)
1036 * Check whether key authenticates and authorises the user.
1039 user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
1040 int auth_attempt, struct sshauthopt **authoptsp)
1042 u_int success = 0, i;
1044 struct sshauthopt *opts = NULL;
1046 if (authoptsp != NULL)
1049 if (auth_key_is_revoked(key))
1051 if (sshkey_is_cert(key) &&
1052 auth_key_is_revoked(key->cert->signature_key))
1055 for (i = 0; !success && i < options.num_authkeys_files; i++) {
1056 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1058 file = expand_authorized_keys(
1059 options.authorized_keys_files[i], pw);
1060 success = user_key_allowed2(ssh, pw, key, file, &opts);
1063 sshauthopt_free(opts);
1070 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
1072 sshauthopt_free(opts);
1075 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
1077 sshauthopt_free(opts);
1081 if (success && authoptsp != NULL) {
1085 sshauthopt_free(opts);
1089 Authmethod method_pubkey = {
1091 "publickey-hostbound-v00@openssh.com",
1093 &options.pubkey_authentication