djm@openbsd.org | d45d69f | 2017-12-21 00:00:28 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.74 2017/12/21 00:00:28 djm Exp $ */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 30 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 31 | #include <errno.h> |
Darren Tucker | 06db584 | 2008-06-13 14:51:28 +1000 | [diff] [blame] | 32 | #include <fcntl.h> |
Darren Tucker | 737f7af | 2012-11-05 17:07:43 +1100 | [diff] [blame] | 33 | #ifdef HAVE_PATHS_H |
| 34 | # include <paths.h> |
| 35 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 36 | #include <pwd.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 37 | #include <signal.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 38 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | #include <stdarg.h> |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 40 | #include <string.h> |
| 41 | #include <time.h> |
Darren Tucker | d9526a5 | 2008-06-14 09:01:24 +1000 | [diff] [blame] | 42 | #include <unistd.h> |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 43 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 44 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 45 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 46 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 47 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 48 | #include "packet.h" |
| 49 | #include "buffer.h" |
| 50 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 51 | #include "misc.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 52 | #include "servconf.h" |
| 53 | #include "compat.h" |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 54 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 55 | #include "hostfile.h" |
| 56 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 57 | #include "pathnames.h" |
| 58 | #include "uidswap.h" |
| 59 | #include "auth-options.h" |
| 60 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 61 | #ifdef GSSAPI |
| 62 | #include "ssh-gss.h" |
| 63 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 64 | #include "monitor_wrap.h" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 65 | #include "authfile.h" |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 66 | #include "match.h" |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 67 | #include "ssherr.h" |
| 68 | #include "channels.h" /* XXX for session.h */ |
| 69 | #include "session.h" /* XXX for child_set_env(); refactor? */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 70 | |
| 71 | /* import */ |
| 72 | extern ServerOptions options; |
| 73 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 74 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 75 | |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 76 | static char * |
| 77 | format_key(const struct sshkey *key) |
| 78 | { |
| 79 | char *ret, *fp = sshkey_fingerprint(key, |
| 80 | options.fingerprint_hash, SSH_FP_DEFAULT); |
| 81 | |
| 82 | xasprintf(&ret, "%s %s", sshkey_type(key), fp); |
| 83 | free(fp); |
| 84 | return ret; |
| 85 | } |
| 86 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 87 | static int |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 88 | userauth_pubkey(struct ssh *ssh) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 89 | { |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 90 | Authctxt *authctxt = ssh->authctxt; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 91 | struct sshbuf *b; |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 92 | struct sshkey *key = NULL; |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 93 | char *pkalg, *userstyle = NULL, *key_s = NULL, *ca_s = NULL; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 94 | u_char *pkblob, *sig, have_sig; |
| 95 | size_t blen, slen; |
| 96 | int r, pktype; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 97 | int authenticated = 0; |
| 98 | |
| 99 | if (!authctxt->valid) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 100 | debug2("%s: disabled because of invalid user", __func__); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 101 | return 0; |
| 102 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 103 | if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0) |
| 104 | fatal("%s: sshpkt_get_u8 failed: %s", __func__, ssh_err(r)); |
| 105 | if (ssh->compat & SSH_BUG_PKAUTH) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 106 | debug2("%s: SSH_BUG_PKAUTH", __func__); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 107 | if ((b = sshbuf_new()) == NULL) |
| 108 | fatal("%s: sshbuf_new failed", __func__); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 109 | /* no explicit pkalg given */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 110 | /* so we have to extract the pkalg from the pkblob */ |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 111 | /* XXX use sshbuf_from() */ |
| 112 | if ((r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 || |
| 113 | (r = sshbuf_put(b, pkblob, blen)) != 0 || |
| 114 | (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0) |
| 115 | fatal("%s: failed: %s", __func__, ssh_err(r)); |
| 116 | sshbuf_free(b); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 117 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 118 | if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || |
| 119 | (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) |
| 120 | fatal("%s: sshpkt_get_cstring failed: %s", |
| 121 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 122 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 123 | pktype = sshkey_type_from_name(pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 124 | if (pktype == KEY_UNSPEC) { |
| 125 | /* this is perfectly legal */ |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 126 | logit("%s: unsupported public key algorithm: %s", |
| 127 | __func__, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 128 | goto done; |
| 129 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 130 | if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { |
| 131 | error("%s: could not parse key: %s", __func__, ssh_err(r)); |
| 132 | goto done; |
| 133 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 134 | if (key == NULL) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 135 | error("%s: cannot decode key: %s", __func__, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 136 | goto done; |
| 137 | } |
| 138 | if (key->type != pktype) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 139 | error("%s: type mismatch for decoded key " |
| 140 | "(received %d, expected %d)", __func__, key->type, pktype); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 141 | goto done; |
| 142 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 143 | if (sshkey_type_plain(key->type) == KEY_RSA && |
| 144 | (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 145 | logit("Refusing RSA key because client uses unsafe " |
| 146 | "signature scheme"); |
| 147 | goto done; |
| 148 | } |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 149 | if (auth2_key_already_used(authctxt, key)) { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 150 | logit("refusing previously-used %s key", sshkey_type(key)); |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 151 | goto done; |
| 152 | } |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 153 | if (match_pattern_list(sshkey_ssh_name(key), |
| 154 | options.pubkey_key_types, 0) != 1) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 155 | logit("%s: key type %s not in PubkeyAcceptedKeyTypes", |
| 156 | __func__, sshkey_ssh_name(key)); |
| 157 | goto done; |
| 158 | } |
| 159 | |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 160 | key_s = format_key(key); |
| 161 | if (sshkey_is_cert(key)) |
| 162 | ca_s = format_key(key->cert->signature_key); |
| 163 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 164 | if (have_sig) { |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 165 | debug3("%s: have %s signature for %s%s%s", |
| 166 | __func__, pkalg, key_s, |
| 167 | ca_s == NULL ? "" : " CA ", |
| 168 | ca_s == NULL ? "" : ca_s); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 169 | if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || |
| 170 | (r = sshpkt_get_end(ssh)) != 0) |
| 171 | fatal("%s: %s", __func__, ssh_err(r)); |
| 172 | if ((b = sshbuf_new()) == NULL) |
| 173 | fatal("%s: sshbuf_new failed", __func__); |
| 174 | if (ssh->compat & SSH_OLD_SESSIONID) { |
| 175 | if ((r = sshbuf_put(b, session_id2, |
| 176 | session_id2_len)) != 0) |
| 177 | fatal("%s: sshbuf_put session id: %s", |
| 178 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 179 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 180 | if ((r = sshbuf_put_string(b, session_id2, |
| 181 | session_id2_len)) != 0) |
| 182 | fatal("%s: sshbuf_put_string session id: %s", |
| 183 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 184 | } |
| 185 | /* reconstruct packet */ |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 186 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 187 | authctxt->style ? ":" : "", |
| 188 | authctxt->style ? authctxt->style : ""); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 189 | if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 190 | (r = sshbuf_put_cstring(b, userstyle)) != 0 || |
| 191 | (r = sshbuf_put_cstring(b, ssh->compat & SSH_BUG_PKSERVICE ? |
| 192 | "ssh-userauth" : authctxt->service)) != 0) |
| 193 | fatal("%s: build packet failed: %s", |
| 194 | __func__, ssh_err(r)); |
| 195 | if (ssh->compat & SSH_BUG_PKAUTH) { |
| 196 | if ((r = sshbuf_put_u8(b, have_sig)) != 0) |
| 197 | fatal("%s: build packet failed: %s", |
| 198 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 199 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 200 | if ((r = sshbuf_put_cstring(b, "publickey")) != 0 || |
| 201 | (r = sshbuf_put_u8(b, have_sig)) != 0 || |
| 202 | (r = sshbuf_put_cstring(b, pkalg) != 0)) |
| 203 | fatal("%s: build packet failed: %s", |
| 204 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 205 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 206 | if ((r = sshbuf_put_string(b, pkblob, blen)) != 0) |
| 207 | fatal("%s: build packet failed: %s", |
| 208 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 209 | #ifdef DEBUG_PK |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 210 | sshbuf_dump(b, stderr); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 211 | #endif |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 212 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 213 | /* test for correct signature */ |
| 214 | authenticated = 0; |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 215 | if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) && |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 216 | PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b), |
djm@openbsd.org | d45d69f | 2017-12-21 00:00:28 +0000 | [diff] [blame^] | 217 | sshbuf_len(b), NULL, ssh->compat)) == 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 218 | authenticated = 1; |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 219 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 220 | sshbuf_free(b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 221 | free(sig); |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 222 | auth2_record_key(authctxt, authenticated, key); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 223 | } else { |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 224 | debug("%s: test pkalg %s pkblob %s%s%s", |
| 225 | __func__, pkalg, key_s, |
| 226 | ca_s == NULL ? "" : " CA ", |
| 227 | ca_s == NULL ? "" : ca_s); |
| 228 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 229 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 230 | fatal("%s: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 231 | |
| 232 | /* XXX fake reply and always send PK_OK ? */ |
| 233 | /* |
| 234 | * XXX this allows testing whether a user is allowed |
| 235 | * to login: if you happen to have a valid pubkey this |
| 236 | * message is sent. the message is NEVER sent at all |
| 237 | * if a user is not allowed to login. is this an |
| 238 | * issue? -markus |
| 239 | */ |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 240 | if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 241 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) |
| 242 | != 0 || |
| 243 | (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || |
| 244 | (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || |
| 245 | (r = sshpkt_send(ssh)) != 0) |
| 246 | fatal("%s: %s", __func__, ssh_err(r)); |
| 247 | ssh_packet_write_wait(ssh); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 248 | authctxt->postponed = 1; |
| 249 | } |
| 250 | } |
| 251 | if (authenticated != 1) |
| 252 | auth_clear_options(); |
| 253 | done: |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 254 | debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg); |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 255 | sshkey_free(key); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 256 | free(userstyle); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 257 | free(pkalg); |
| 258 | free(pkblob); |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 259 | free(key_s); |
| 260 | free(ca_s); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 261 | return authenticated; |
| 262 | } |
| 263 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 264 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 265 | match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 266 | { |
| 267 | char *result; |
| 268 | u_int i; |
| 269 | |
| 270 | /* XXX percent_expand() sequences for authorized_principals? */ |
| 271 | |
| 272 | for (i = 0; i < cert->nprincipals; i++) { |
| 273 | if ((result = match_list(cert->principals[i], |
| 274 | principal_list, NULL)) != NULL) { |
| 275 | debug3("matched principal from key options \"%.100s\"", |
| 276 | result); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 277 | free(result); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 278 | return 1; |
| 279 | } |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static int |
markus@openbsd.org | 75b8af8 | 2017-05-31 10:54:00 +0000 | [diff] [blame] | 285 | process_principals(FILE *f, const char *file, struct passwd *pw, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 286 | const struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 287 | { |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 288 | char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 289 | u_long linenum = 0; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 290 | u_int i, found_principal = 0; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 291 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 292 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 293 | /* Always consume entire input */ |
| 294 | if (found_principal) |
| 295 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 296 | /* Skip leading whitespace. */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 297 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 298 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 299 | /* Skip blank and comment lines. */ |
| 300 | if ((ep = strchr(cp, '#')) != NULL) |
| 301 | *ep = '\0'; |
| 302 | if (!*cp || *cp == '\n') |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 303 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 304 | /* Trim trailing whitespace. */ |
| 305 | ep = cp + strlen(cp) - 1; |
| 306 | while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) |
| 307 | *ep-- = '\0'; |
| 308 | /* |
| 309 | * If the line has internal whitespace then assume it has |
| 310 | * key options. |
| 311 | */ |
| 312 | line_opts = NULL; |
| 313 | if ((ep = strrchr(cp, ' ')) != NULL || |
| 314 | (ep = strrchr(cp, '\t')) != NULL) { |
| 315 | for (; *ep == ' ' || *ep == '\t'; ep++) |
Damien Miller | 188ea81 | 2010-12-01 11:50:14 +1100 | [diff] [blame] | 316 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 317 | line_opts = cp; |
| 318 | cp = ep; |
| 319 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 320 | for (i = 0; i < cert->nprincipals; i++) { |
| 321 | if (strcmp(cp, cert->principals[i]) == 0) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 322 | debug3("%s:%lu: matched principal \"%.100s\"", |
markus@openbsd.org | 75b8af8 | 2017-05-31 10:54:00 +0000 | [diff] [blame] | 323 | file, linenum, cert->principals[i]); |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 324 | if (auth_parse_options(pw, line_opts, |
| 325 | file, linenum) != 1) |
| 326 | continue; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 327 | found_principal = 1; |
| 328 | continue; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 332 | return found_principal; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 333 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 334 | |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 335 | static int |
| 336 | match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) |
| 337 | { |
| 338 | FILE *f; |
| 339 | int success; |
| 340 | |
| 341 | temporarily_use_uid(pw); |
| 342 | debug("trying authorized principals file %s", file); |
| 343 | if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 344 | restore_uid(); |
| 345 | return 0; |
| 346 | } |
| 347 | success = process_principals(f, file, pw, cert); |
| 348 | fclose(f); |
| 349 | restore_uid(); |
| 350 | return success; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Checks whether principal is allowed in output of command. |
| 355 | * returns 1 if the principal is allowed or 0 otherwise. |
| 356 | */ |
| 357 | static int |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 358 | match_principals_command(struct passwd *user_pw, const struct sshkey *key) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 359 | { |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 360 | const struct sshkey_cert *cert = key->cert; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 361 | FILE *f = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 362 | int r, ok, found_principal = 0; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 363 | struct passwd *pw; |
| 364 | int i, ac = 0, uid_swapped = 0; |
| 365 | pid_t pid; |
| 366 | char *tmp, *username = NULL, *command = NULL, **av = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 367 | char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; |
djm@openbsd.org | bfa9d96 | 2016-09-21 01:34:45 +0000 | [diff] [blame] | 368 | char serial_s[16]; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 369 | void (*osigchld)(int); |
| 370 | |
| 371 | if (options.authorized_principals_command == NULL) |
| 372 | return 0; |
| 373 | if (options.authorized_principals_command_user == NULL) { |
| 374 | error("No user for AuthorizedPrincipalsCommand specified, " |
| 375 | "skipping"); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * NB. all returns later this function should go via "out" to |
| 381 | * ensure the original SIGCHLD handler is restored properly. |
| 382 | */ |
| 383 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 384 | |
| 385 | /* Prepare and verify the user for the command */ |
| 386 | username = percent_expand(options.authorized_principals_command_user, |
| 387 | "u", user_pw->pw_name, (char *)NULL); |
| 388 | pw = getpwnam(username); |
| 389 | if (pw == NULL) { |
| 390 | error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", |
| 391 | username, strerror(errno)); |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | /* Turn the command into an argument vector */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 396 | if (argv_split(options.authorized_principals_command, &ac, &av) != 0) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 397 | error("AuthorizedPrincipalsCommand \"%s\" contains " |
| 398 | "invalid quotes", command); |
| 399 | goto out; |
| 400 | } |
| 401 | if (ac == 0) { |
| 402 | error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", |
| 403 | command); |
| 404 | goto out; |
| 405 | } |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 406 | if ((ca_fp = sshkey_fingerprint(cert->signature_key, |
| 407 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 408 | error("%s: sshkey_fingerprint failed", __func__); |
| 409 | goto out; |
| 410 | } |
djm@openbsd.org | 00df97f | 2016-09-14 20:11:26 +0000 | [diff] [blame] | 411 | if ((key_fp = sshkey_fingerprint(key, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 412 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 413 | error("%s: sshkey_fingerprint failed", __func__); |
| 414 | goto out; |
| 415 | } |
| 416 | if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { |
| 417 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 418 | goto out; |
| 419 | } |
| 420 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 421 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 422 | goto out; |
| 423 | } |
djm@openbsd.org | f83a0cf | 2016-09-21 17:44:20 +0000 | [diff] [blame] | 424 | snprintf(serial_s, sizeof(serial_s), "%llu", |
| 425 | (unsigned long long)cert->serial); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 426 | for (i = 1; i < ac; i++) { |
| 427 | tmp = percent_expand(av[i], |
| 428 | "u", user_pw->pw_name, |
| 429 | "h", user_pw->pw_dir, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 430 | "t", sshkey_ssh_name(key), |
| 431 | "T", sshkey_ssh_name(cert->signature_key), |
| 432 | "f", key_fp, |
| 433 | "F", ca_fp, |
| 434 | "k", keytext, |
| 435 | "K", catext, |
djm@openbsd.org | bfa9d96 | 2016-09-21 01:34:45 +0000 | [diff] [blame] | 436 | "i", cert->key_id, |
| 437 | "s", serial_s, |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 438 | (char *)NULL); |
| 439 | if (tmp == NULL) |
| 440 | fatal("%s: percent_expand failed", __func__); |
| 441 | free(av[i]); |
| 442 | av[i] = tmp; |
| 443 | } |
| 444 | /* Prepare a printable command for logs, etc. */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 445 | command = argv_assemble(ac, av); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 446 | |
| 447 | if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command, |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 448 | ac, av, &f, |
| 449 | SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 450 | goto out; |
| 451 | |
| 452 | uid_swapped = 1; |
| 453 | temporarily_use_uid(pw); |
| 454 | |
markus@openbsd.org | 75b8af8 | 2017-05-31 10:54:00 +0000 | [diff] [blame] | 455 | ok = process_principals(f, "(command)", pw, cert); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 456 | |
djm@openbsd.org | ddd3d34 | 2016-12-30 22:08:02 +0000 | [diff] [blame] | 457 | fclose(f); |
| 458 | f = NULL; |
| 459 | |
djm@openbsd.org | b074c3c | 2017-08-18 05:48:04 +0000 | [diff] [blame] | 460 | if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 461 | goto out; |
| 462 | |
| 463 | /* Read completed successfully */ |
| 464 | found_principal = ok; |
| 465 | out: |
| 466 | if (f != NULL) |
| 467 | fclose(f); |
| 468 | signal(SIGCHLD, osigchld); |
| 469 | for (i = 0; i < ac; i++) |
| 470 | free(av[i]); |
| 471 | free(av); |
| 472 | if (uid_swapped) |
| 473 | restore_uid(); |
| 474 | free(command); |
| 475 | free(username); |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 476 | free(ca_fp); |
| 477 | free(key_fp); |
| 478 | free(catext); |
| 479 | free(keytext); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 480 | return found_principal; |
| 481 | } |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 482 | /* |
| 483 | * Checks whether key is allowed in authorized_keys-format file, |
| 484 | * returns 1 if the key is allowed or 0 otherwise. |
| 485 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 486 | static int |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 487 | check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 488 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 489 | char line[SSH_MAX_PUBKEY_BYTES]; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 490 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 491 | u_long linenum = 0; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 492 | struct sshkey *found = NULL; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 493 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 494 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 495 | char *cp, *key_options = NULL, *fp = NULL; |
| 496 | const char *reason = NULL; |
| 497 | |
djm@openbsd.org | abd5966 | 2017-09-07 23:48:09 +0000 | [diff] [blame] | 498 | /* Always consume entire file */ |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 499 | if (found_key) |
| 500 | continue; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 501 | if (found != NULL) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 502 | sshkey_free(found); |
| 503 | found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type); |
| 504 | if (found == NULL) |
| 505 | goto done; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 506 | auth_clear_options(); |
| 507 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 508 | /* Skip leading whitespace, empty and comment lines. */ |
| 509 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 510 | ; |
| 511 | if (!*cp || *cp == '\n' || *cp == '#') |
| 512 | continue; |
| 513 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 514 | if (sshkey_read(found, &cp) != 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 515 | /* no key? check if there are options for this key */ |
| 516 | int quoted = 0; |
| 517 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 518 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 519 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 520 | if (*cp == '\\' && cp[1] == '"') |
| 521 | cp++; /* Skip both */ |
| 522 | else if (*cp == '"') |
| 523 | quoted = !quoted; |
| 524 | } |
| 525 | /* Skip remaining whitespace. */ |
| 526 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 527 | ; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 528 | if (sshkey_read(found, &cp) != 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 529 | debug2("user_key_allowed: advance: '%s'", cp); |
| 530 | /* still no key? advance to next line*/ |
| 531 | continue; |
| 532 | } |
| 533 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 534 | if (sshkey_is_cert(key)) { |
| 535 | if (!sshkey_equal(found, key->cert->signature_key)) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 536 | continue; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 537 | if (auth_parse_options(pw, key_options, file, |
| 538 | linenum) != 1) |
| 539 | continue; |
| 540 | if (!key_is_cert_authority) |
| 541 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 542 | if ((fp = sshkey_fingerprint(found, |
| 543 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 544 | continue; |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 545 | debug("matching CA found: file %s, line %lu, %s %s", |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 546 | file, linenum, sshkey_type(found), fp); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 547 | /* |
| 548 | * If the user has specified a list of principals as |
| 549 | * a key option, then prefer that list to matching |
| 550 | * their username in the certificate principals list. |
| 551 | */ |
| 552 | if (authorized_principals != NULL && |
| 553 | !match_principals_option(authorized_principals, |
| 554 | key->cert)) { |
| 555 | reason = "Certificate does not contain an " |
| 556 | "authorized principal"; |
| 557 | fail_reason: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 558 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 559 | error("%s", reason); |
| 560 | auth_debug_add("%s", reason); |
| 561 | continue; |
| 562 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 563 | if (sshkey_cert_check_authority(key, 0, 0, |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 564 | authorized_principals == NULL ? pw->pw_name : NULL, |
| 565 | &reason) != 0) |
| 566 | goto fail_reason; |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 567 | if (auth_cert_options(key, pw, &reason) != 0) |
| 568 | goto fail_reason; |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 569 | verbose("Accepted certificate ID \"%s\" (serial %llu) " |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 570 | "signed by %s CA %s via %s", key->cert->key_id, |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 571 | (unsigned long long)key->cert->serial, |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 572 | sshkey_type(found), fp, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 573 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 574 | found_key = 1; |
| 575 | break; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 576 | } else if (sshkey_equal(found, key)) { |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 577 | if (auth_parse_options(pw, key_options, file, |
| 578 | linenum) != 1) |
| 579 | continue; |
| 580 | if (key_is_cert_authority) |
| 581 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 582 | if ((fp = sshkey_fingerprint(found, |
| 583 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 584 | continue; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 585 | debug("matching key found: file %s, line %lu %s %s", |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 586 | file, linenum, sshkey_type(found), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 587 | free(fp); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 588 | found_key = 1; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 589 | continue; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 592 | done: |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 593 | if (found != NULL) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 594 | sshkey_free(found); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 595 | if (!found_key) |
| 596 | debug2("key not found"); |
| 597 | return found_key; |
| 598 | } |
| 599 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 600 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 601 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 602 | user_cert_trusted_ca(struct passwd *pw, struct sshkey *key) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 603 | { |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 604 | char *ca_fp, *principals_file = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 605 | const char *reason; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 606 | int r, ret = 0, found_principal = 0, use_authorized_principals; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 607 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 608 | if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 609 | return 0; |
| 610 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 611 | if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, |
| 612 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 613 | return 0; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 614 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 615 | if ((r = sshkey_in_file(key->cert->signature_key, |
| 616 | options.trusted_user_ca_keys, 1, 0)) != 0) { |
| 617 | debug2("%s: CA %s %s is not listed in %s: %s", __func__, |
| 618 | sshkey_type(key->cert->signature_key), ca_fp, |
| 619 | options.trusted_user_ca_keys, ssh_err(r)); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 620 | goto out; |
| 621 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 622 | /* |
| 623 | * If AuthorizedPrincipals is in use, then compare the certificate |
| 624 | * principals against the names in that file rather than matching |
| 625 | * against the username. |
| 626 | */ |
| 627 | if ((principals_file = authorized_principals_file(pw)) != NULL) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 628 | if (match_principals_file(principals_file, pw, key->cert)) |
| 629 | found_principal = 1; |
| 630 | } |
| 631 | /* Try querying command if specified */ |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 632 | if (!found_principal && match_principals_command(pw, key)) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 633 | found_principal = 1; |
jsing@openbsd.org | 596dbca | 2015-06-15 18:44:22 +0000 | [diff] [blame] | 634 | /* If principals file or command is specified, then require a match */ |
| 635 | use_authorized_principals = principals_file != NULL || |
| 636 | options.authorized_principals_command != NULL; |
| 637 | if (!found_principal && use_authorized_principals) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 638 | reason = "Certificate does not contain an authorized principal"; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 639 | fail_reason: |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 640 | error("%s", reason); |
| 641 | auth_debug_add("%s", reason); |
| 642 | goto out; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 643 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 644 | if (sshkey_cert_check_authority(key, 0, 1, |
jsing@openbsd.org | 596dbca | 2015-06-15 18:44:22 +0000 | [diff] [blame] | 645 | use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 646 | goto fail_reason; |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 647 | if (auth_cert_options(key, pw, &reason) != 0) |
| 648 | goto fail_reason; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 649 | |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 650 | verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " |
| 651 | "%s CA %s via %s", key->cert->key_id, |
| 652 | (unsigned long long)key->cert->serial, |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 653 | sshkey_type(key->cert->signature_key), ca_fp, |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 654 | options.trusted_user_ca_keys); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 655 | ret = 1; |
| 656 | |
| 657 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 658 | free(principals_file); |
| 659 | free(ca_fp); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 660 | return ret; |
| 661 | } |
| 662 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 663 | /* |
| 664 | * Checks whether key is allowed in file. |
| 665 | * returns 1 if the key is allowed or 0 otherwise. |
| 666 | */ |
| 667 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 668 | user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 669 | { |
| 670 | FILE *f; |
| 671 | int found_key = 0; |
| 672 | |
| 673 | /* Temporarily use the user's uid. */ |
| 674 | temporarily_use_uid(pw); |
| 675 | |
| 676 | debug("trying public key file %s", file); |
| 677 | if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { |
| 678 | found_key = check_authkeys_file(f, file, key, pw); |
| 679 | fclose(f); |
| 680 | } |
| 681 | |
| 682 | restore_uid(); |
| 683 | return found_key; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Checks whether key is allowed in output of command. |
| 688 | * returns 1 if the key is allowed or 0 otherwise. |
| 689 | */ |
| 690 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 691 | user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 692 | { |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 693 | FILE *f = NULL; |
| 694 | int r, ok, found_key = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 695 | struct passwd *pw; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 696 | int i, uid_swapped = 0, ac = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 697 | pid_t pid; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 698 | char *username = NULL, *key_fp = NULL, *keytext = NULL; |
| 699 | char *tmp, *command = NULL, **av = NULL; |
| 700 | void (*osigchld)(int); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 701 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 702 | if (options.authorized_keys_command == NULL) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 703 | return 0; |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 704 | if (options.authorized_keys_command_user == NULL) { |
| 705 | error("No user for AuthorizedKeysCommand specified, skipping"); |
| 706 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 707 | } |
| 708 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 709 | /* |
| 710 | * NB. all returns later this function should go via "out" to |
| 711 | * ensure the original SIGCHLD handler is restored properly. |
| 712 | */ |
| 713 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 714 | |
| 715 | /* Prepare and verify the user for the command */ |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 716 | username = percent_expand(options.authorized_keys_command_user, |
| 717 | "u", user_pw->pw_name, (char *)NULL); |
| 718 | pw = getpwnam(username); |
| 719 | if (pw == NULL) { |
Damien Miller | 4018dc0 | 2013-02-15 10:28:55 +1100 | [diff] [blame] | 720 | error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 721 | username, strerror(errno)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 722 | goto out; |
| 723 | } |
| 724 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 725 | /* Prepare AuthorizedKeysCommand */ |
| 726 | if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 727 | SSH_FP_DEFAULT)) == NULL) { |
| 728 | error("%s: sshkey_fingerprint failed", __func__); |
| 729 | goto out; |
| 730 | } |
| 731 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 732 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 733 | goto out; |
| 734 | } |
| 735 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 736 | /* Turn the command into an argument vector */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 737 | if (argv_split(options.authorized_keys_command, &ac, &av) != 0) { |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 738 | error("AuthorizedKeysCommand \"%s\" contains invalid quotes", |
| 739 | command); |
| 740 | goto out; |
| 741 | } |
| 742 | if (ac == 0) { |
| 743 | error("AuthorizedKeysCommand \"%s\" yielded no arguments", |
| 744 | command); |
| 745 | goto out; |
| 746 | } |
| 747 | for (i = 1; i < ac; i++) { |
| 748 | tmp = percent_expand(av[i], |
| 749 | "u", user_pw->pw_name, |
| 750 | "h", user_pw->pw_dir, |
| 751 | "t", sshkey_ssh_name(key), |
| 752 | "f", key_fp, |
| 753 | "k", keytext, |
| 754 | (char *)NULL); |
| 755 | if (tmp == NULL) |
| 756 | fatal("%s: percent_expand failed", __func__); |
| 757 | free(av[i]); |
| 758 | av[i] = tmp; |
| 759 | } |
| 760 | /* Prepare a printable command for logs, etc. */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 761 | command = argv_assemble(ac, av); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 762 | |
| 763 | /* |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 764 | * If AuthorizedKeysCommand was run without arguments |
| 765 | * then fall back to the old behaviour of passing the |
| 766 | * target username as a single argument. |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 767 | */ |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 768 | if (ac == 1) { |
| 769 | av = xreallocarray(av, ac + 2, sizeof(*av)); |
| 770 | av[1] = xstrdup(user_pw->pw_name); |
| 771 | av[2] = NULL; |
| 772 | /* Fix up command too, since it is used in log messages */ |
| 773 | free(command); |
| 774 | xasprintf(&command, "%s %s", av[0], av[1]); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 775 | } |
| 776 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 777 | if ((pid = subprocess("AuthorizedKeysCommand", pw, command, |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 778 | ac, av, &f, |
| 779 | SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 780 | goto out; |
| 781 | |
| 782 | uid_swapped = 1; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 783 | temporarily_use_uid(pw); |
| 784 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 785 | ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 786 | |
djm@openbsd.org | ddd3d34 | 2016-12-30 22:08:02 +0000 | [diff] [blame] | 787 | fclose(f); |
| 788 | f = NULL; |
| 789 | |
djm@openbsd.org | b074c3c | 2017-08-18 05:48:04 +0000 | [diff] [blame] | 790 | if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 791 | goto out; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 792 | |
| 793 | /* Read completed successfully */ |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 794 | found_key = ok; |
| 795 | out: |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 796 | if (f != NULL) |
| 797 | fclose(f); |
| 798 | signal(SIGCHLD, osigchld); |
| 799 | for (i = 0; i < ac; i++) |
| 800 | free(av[i]); |
| 801 | free(av); |
| 802 | if (uid_swapped) |
| 803 | restore_uid(); |
| 804 | free(command); |
| 805 | free(username); |
| 806 | free(key_fp); |
| 807 | free(keytext); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 808 | return found_key; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Check whether key authenticates and authorises the user. |
| 813 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 814 | int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 815 | user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 816 | { |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 817 | u_int success, i; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 818 | char *file; |
| 819 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 820 | if (auth_key_is_revoked(key)) |
| 821 | return 0; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 822 | if (sshkey_is_cert(key) && |
| 823 | auth_key_is_revoked(key->cert->signature_key)) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 824 | return 0; |
| 825 | |
| 826 | success = user_cert_trusted_ca(pw, key); |
| 827 | if (success) |
| 828 | return success; |
| 829 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 830 | success = user_key_command_allowed2(pw, key); |
| 831 | if (success > 0) |
| 832 | return success; |
| 833 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 834 | for (i = 0; !success && i < options.num_authkeys_files; i++) { |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 835 | |
| 836 | if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 837 | continue; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 838 | file = expand_authorized_keys( |
| 839 | options.authorized_keys_files[i], pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 840 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 841 | success = user_key_allowed2(pw, key, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 842 | free(file); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 843 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 844 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 845 | return success; |
| 846 | } |
| 847 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 848 | Authmethod method_pubkey = { |
| 849 | "publickey", |
| 850 | userauth_pubkey, |
| 851 | &options.pubkey_authentication |
| 852 | }; |