Darren Tucker | af1a60e | 2011-10-02 18:59:59 +1100 | [diff] [blame] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.30 2011/09/25 05:44:47 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 | |
Darren Tucker | 06db584 | 2008-06-13 14:51:28 +1000 | [diff] [blame] | 31 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 32 | #include <pwd.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 33 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 34 | #include <stdarg.h> |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <time.h> |
Darren Tucker | d9526a5 | 2008-06-14 09:01:24 +1000 | [diff] [blame] | 37 | #include <unistd.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 38 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 40 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 41 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 42 | #include "packet.h" |
| 43 | #include "buffer.h" |
| 44 | #include "log.h" |
| 45 | #include "servconf.h" |
| 46 | #include "compat.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 47 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 48 | #include "hostfile.h" |
| 49 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 50 | #include "pathnames.h" |
| 51 | #include "uidswap.h" |
| 52 | #include "auth-options.h" |
| 53 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 54 | #ifdef GSSAPI |
| 55 | #include "ssh-gss.h" |
| 56 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 57 | #include "monitor_wrap.h" |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 58 | #include "misc.h" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 59 | #include "authfile.h" |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 60 | #include "match.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 61 | |
| 62 | /* import */ |
| 63 | extern ServerOptions options; |
| 64 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 65 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 66 | |
| 67 | static int |
| 68 | userauth_pubkey(Authctxt *authctxt) |
| 69 | { |
| 70 | Buffer b; |
| 71 | Key *key = NULL; |
| 72 | char *pkalg; |
| 73 | u_char *pkblob, *sig; |
| 74 | u_int alen, blen, slen; |
| 75 | int have_sig, pktype; |
| 76 | int authenticated = 0; |
| 77 | |
| 78 | if (!authctxt->valid) { |
| 79 | debug2("userauth_pubkey: disabled because of invalid user"); |
| 80 | return 0; |
| 81 | } |
| 82 | have_sig = packet_get_char(); |
| 83 | if (datafellows & SSH_BUG_PKAUTH) { |
| 84 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 85 | /* no explicit pkalg given */ |
| 86 | pkblob = packet_get_string(&blen); |
| 87 | buffer_init(&b); |
| 88 | buffer_append(&b, pkblob, blen); |
| 89 | /* so we have to extract the pkalg from the pkblob */ |
| 90 | pkalg = buffer_get_string(&b, &alen); |
| 91 | buffer_free(&b); |
| 92 | } else { |
| 93 | pkalg = packet_get_string(&alen); |
| 94 | pkblob = packet_get_string(&blen); |
| 95 | } |
| 96 | pktype = key_type_from_name(pkalg); |
| 97 | if (pktype == KEY_UNSPEC) { |
| 98 | /* this is perfectly legal */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 99 | logit("userauth_pubkey: unsupported public key algorithm: %s", |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 100 | pkalg); |
| 101 | goto done; |
| 102 | } |
| 103 | key = key_from_blob(pkblob, blen); |
| 104 | if (key == NULL) { |
| 105 | error("userauth_pubkey: cannot decode key: %s", pkalg); |
| 106 | goto done; |
| 107 | } |
| 108 | if (key->type != pktype) { |
| 109 | error("userauth_pubkey: type mismatch for decoded key " |
| 110 | "(received %d, expected %d)", key->type, pktype); |
| 111 | goto done; |
| 112 | } |
| 113 | if (have_sig) { |
| 114 | sig = packet_get_string(&slen); |
| 115 | packet_check_eom(); |
| 116 | buffer_init(&b); |
| 117 | if (datafellows & SSH_OLD_SESSIONID) { |
| 118 | buffer_append(&b, session_id2, session_id2_len); |
| 119 | } else { |
| 120 | buffer_put_string(&b, session_id2, session_id2_len); |
| 121 | } |
| 122 | /* reconstruct packet */ |
| 123 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 124 | buffer_put_cstring(&b, authctxt->user); |
| 125 | buffer_put_cstring(&b, |
| 126 | datafellows & SSH_BUG_PKSERVICE ? |
| 127 | "ssh-userauth" : |
| 128 | authctxt->service); |
| 129 | if (datafellows & SSH_BUG_PKAUTH) { |
| 130 | buffer_put_char(&b, have_sig); |
| 131 | } else { |
| 132 | buffer_put_cstring(&b, "publickey"); |
| 133 | buffer_put_char(&b, have_sig); |
| 134 | buffer_put_cstring(&b, pkalg); |
| 135 | } |
| 136 | buffer_put_string(&b, pkblob, blen); |
| 137 | #ifdef DEBUG_PK |
| 138 | buffer_dump(&b); |
| 139 | #endif |
| 140 | /* test for correct signature */ |
| 141 | authenticated = 0; |
| 142 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && |
| 143 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 144 | buffer_len(&b))) == 1) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 145 | authenticated = 1; |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 146 | buffer_free(&b); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 147 | xfree(sig); |
| 148 | } else { |
| 149 | debug("test whether pkalg/pkblob are acceptable"); |
| 150 | packet_check_eom(); |
| 151 | |
| 152 | /* XXX fake reply and always send PK_OK ? */ |
| 153 | /* |
| 154 | * XXX this allows testing whether a user is allowed |
| 155 | * to login: if you happen to have a valid pubkey this |
| 156 | * message is sent. the message is NEVER sent at all |
| 157 | * if a user is not allowed to login. is this an |
| 158 | * issue? -markus |
| 159 | */ |
| 160 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
| 161 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 162 | packet_put_string(pkalg, alen); |
| 163 | packet_put_string(pkblob, blen); |
| 164 | packet_send(); |
| 165 | packet_write_wait(); |
| 166 | authctxt->postponed = 1; |
| 167 | } |
| 168 | } |
| 169 | if (authenticated != 1) |
| 170 | auth_clear_options(); |
| 171 | done: |
| 172 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
| 173 | if (key != NULL) |
| 174 | key_free(key); |
| 175 | xfree(pkalg); |
| 176 | xfree(pkblob); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 177 | return authenticated; |
| 178 | } |
| 179 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 180 | static int |
| 181 | match_principals_option(const char *principal_list, struct KeyCert *cert) |
| 182 | { |
| 183 | char *result; |
| 184 | u_int i; |
| 185 | |
| 186 | /* XXX percent_expand() sequences for authorized_principals? */ |
| 187 | |
| 188 | for (i = 0; i < cert->nprincipals; i++) { |
| 189 | if ((result = match_list(cert->principals[i], |
| 190 | principal_list, NULL)) != NULL) { |
| 191 | debug3("matched principal from key options \"%.100s\"", |
| 192 | result); |
| 193 | xfree(result); |
| 194 | return 1; |
| 195 | } |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 201 | match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 202 | { |
| 203 | FILE *f; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 204 | char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 205 | u_long linenum = 0; |
| 206 | u_int i; |
| 207 | |
| 208 | temporarily_use_uid(pw); |
| 209 | debug("trying authorized principals file %s", file); |
| 210 | if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 211 | restore_uid(); |
| 212 | return 0; |
| 213 | } |
| 214 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 215 | /* Skip leading whitespace. */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 216 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 217 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 218 | /* Skip blank and comment lines. */ |
| 219 | if ((ep = strchr(cp, '#')) != NULL) |
| 220 | *ep = '\0'; |
| 221 | if (!*cp || *cp == '\n') |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 222 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 223 | /* Trim trailing whitespace. */ |
| 224 | ep = cp + strlen(cp) - 1; |
| 225 | while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) |
| 226 | *ep-- = '\0'; |
| 227 | /* |
| 228 | * If the line has internal whitespace then assume it has |
| 229 | * key options. |
| 230 | */ |
| 231 | line_opts = NULL; |
| 232 | if ((ep = strrchr(cp, ' ')) != NULL || |
| 233 | (ep = strrchr(cp, '\t')) != NULL) { |
| 234 | for (; *ep == ' ' || *ep == '\t'; ep++) |
Damien Miller | 188ea81 | 2010-12-01 11:50:14 +1100 | [diff] [blame] | 235 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 236 | line_opts = cp; |
| 237 | cp = ep; |
| 238 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 239 | for (i = 0; i < cert->nprincipals; i++) { |
| 240 | if (strcmp(cp, cert->principals[i]) == 0) { |
Darren Tucker | af1a60e | 2011-10-02 18:59:59 +1100 | [diff] [blame] | 241 | debug3("matched principal \"%.100s\" " |
| 242 | "from file \"%s\" on line %lu", |
| 243 | cert->principals[i], file, linenum); |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 244 | if (auth_parse_options(pw, line_opts, |
| 245 | file, linenum) != 1) |
| 246 | continue; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 247 | fclose(f); |
| 248 | restore_uid(); |
| 249 | return 1; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | fclose(f); |
| 254 | restore_uid(); |
| 255 | return 0; |
| 256 | } |
| 257 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 258 | /* return 1 if user allows given key */ |
| 259 | static int |
| 260 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 261 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 262 | char line[SSH_MAX_PUBKEY_BYTES]; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 263 | const char *reason; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 264 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 265 | FILE *f; |
| 266 | u_long linenum = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 267 | Key *found; |
| 268 | char *fp; |
| 269 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 270 | /* Temporarily use the user's uid. */ |
| 271 | temporarily_use_uid(pw); |
| 272 | |
| 273 | debug("trying public key file %s", file); |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 274 | f = auth_openkeyfile(file, pw, options.strict_modes); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 275 | |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 276 | if (!f) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 277 | restore_uid(); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | found_key = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 282 | found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 283 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 284 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 285 | char *cp, *key_options = NULL; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 286 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 287 | auth_clear_options(); |
| 288 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 289 | /* Skip leading whitespace, empty and comment lines. */ |
| 290 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 291 | ; |
| 292 | if (!*cp || *cp == '\n' || *cp == '#') |
| 293 | continue; |
| 294 | |
| 295 | if (key_read(found, &cp) != 1) { |
| 296 | /* no key? check if there are options for this key */ |
| 297 | int quoted = 0; |
| 298 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 299 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 300 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 301 | if (*cp == '\\' && cp[1] == '"') |
| 302 | cp++; /* Skip both */ |
| 303 | else if (*cp == '"') |
| 304 | quoted = !quoted; |
| 305 | } |
| 306 | /* Skip remaining whitespace. */ |
| 307 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 308 | ; |
| 309 | if (key_read(found, &cp) != 1) { |
| 310 | debug2("user_key_allowed: advance: '%s'", cp); |
| 311 | /* still no key? advance to next line*/ |
| 312 | continue; |
| 313 | } |
| 314 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 315 | if (key_is_cert(key)) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 316 | if (!key_equal(found, key->cert->signature_key)) |
| 317 | continue; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 318 | if (auth_parse_options(pw, key_options, file, |
| 319 | linenum) != 1) |
| 320 | continue; |
| 321 | if (!key_is_cert_authority) |
| 322 | continue; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 323 | fp = key_fingerprint(found, SSH_FP_MD5, |
| 324 | SSH_FP_HEX); |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 325 | debug("matching CA found: file %s, line %lu, %s %s", |
| 326 | file, linenum, key_type(found), fp); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 327 | /* |
| 328 | * If the user has specified a list of principals as |
| 329 | * a key option, then prefer that list to matching |
| 330 | * their username in the certificate principals list. |
| 331 | */ |
| 332 | if (authorized_principals != NULL && |
| 333 | !match_principals_option(authorized_principals, |
| 334 | key->cert)) { |
| 335 | reason = "Certificate does not contain an " |
| 336 | "authorized principal"; |
| 337 | fail_reason: |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 338 | xfree(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 339 | error("%s", reason); |
| 340 | auth_debug_add("%s", reason); |
| 341 | continue; |
| 342 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 343 | if (key_cert_check_authority(key, 0, 0, |
| 344 | authorized_principals == NULL ? pw->pw_name : NULL, |
| 345 | &reason) != 0) |
| 346 | goto fail_reason; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 347 | if (auth_cert_options(key, pw) != 0) { |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 348 | xfree(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 349 | continue; |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 350 | } |
| 351 | verbose("Accepted certificate ID \"%s\" " |
| 352 | "signed by %s CA %s via %s", key->cert->key_id, |
| 353 | key_type(found), fp, file); |
| 354 | xfree(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 355 | found_key = 1; |
| 356 | break; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 357 | } else if (key_equal(found, key)) { |
| 358 | if (auth_parse_options(pw, key_options, file, |
| 359 | linenum) != 1) |
| 360 | continue; |
| 361 | if (key_is_cert_authority) |
| 362 | continue; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 363 | found_key = 1; |
| 364 | debug("matching key found: file %s, line %lu", |
| 365 | file, linenum); |
| 366 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
| 367 | verbose("Found matching %s key: %s", |
| 368 | key_type(found), fp); |
| 369 | xfree(fp); |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | restore_uid(); |
| 374 | fclose(f); |
| 375 | key_free(found); |
| 376 | if (!found_key) |
| 377 | debug2("key not found"); |
| 378 | return found_key; |
| 379 | } |
| 380 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 381 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 382 | static int |
| 383 | user_cert_trusted_ca(struct passwd *pw, Key *key) |
| 384 | { |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 385 | char *ca_fp, *principals_file = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 386 | const char *reason; |
| 387 | int ret = 0; |
| 388 | |
| 389 | if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) |
| 390 | return 0; |
| 391 | |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 392 | ca_fp = key_fingerprint(key->cert->signature_key, |
| 393 | SSH_FP_MD5, SSH_FP_HEX); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 394 | |
| 395 | if (key_in_file(key->cert->signature_key, |
| 396 | options.trusted_user_ca_keys, 1) != 1) { |
| 397 | debug2("%s: CA %s %s is not listed in %s", __func__, |
| 398 | key_type(key->cert->signature_key), ca_fp, |
| 399 | options.trusted_user_ca_keys); |
| 400 | goto out; |
| 401 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 402 | /* |
| 403 | * If AuthorizedPrincipals is in use, then compare the certificate |
| 404 | * principals against the names in that file rather than matching |
| 405 | * against the username. |
| 406 | */ |
| 407 | if ((principals_file = authorized_principals_file(pw)) != NULL) { |
| 408 | if (!match_principals_file(principals_file, pw, key->cert)) { |
| 409 | reason = "Certificate does not contain an " |
| 410 | "authorized principal"; |
| 411 | fail_reason: |
| 412 | error("%s", reason); |
| 413 | auth_debug_add("%s", reason); |
| 414 | goto out; |
| 415 | } |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 416 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 417 | if (key_cert_check_authority(key, 0, 1, |
| 418 | principals_file == NULL ? pw->pw_name : NULL, &reason) != 0) |
| 419 | goto fail_reason; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 420 | if (auth_cert_options(key, pw) != 0) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 421 | goto out; |
| 422 | |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 423 | verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s", |
| 424 | key->cert->key_id, key_type(key->cert->signature_key), ca_fp, |
| 425 | options.trusted_user_ca_keys); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 426 | ret = 1; |
| 427 | |
| 428 | out: |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 429 | if (principals_file != NULL) |
| 430 | xfree(principals_file); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 431 | if (ca_fp != NULL) |
| 432 | xfree(ca_fp); |
| 433 | return ret; |
| 434 | } |
| 435 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 436 | /* check whether given key is in .ssh/authorized_keys* */ |
| 437 | int |
| 438 | user_key_allowed(struct passwd *pw, Key *key) |
| 439 | { |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 440 | u_int success, i; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 441 | char *file; |
| 442 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 443 | if (auth_key_is_revoked(key)) |
| 444 | return 0; |
| 445 | if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |
| 446 | return 0; |
| 447 | |
| 448 | success = user_cert_trusted_ca(pw, key); |
| 449 | if (success) |
| 450 | return success; |
| 451 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 452 | for (i = 0; !success && i < options.num_authkeys_files; i++) { |
| 453 | file = expand_authorized_keys( |
| 454 | options.authorized_keys_files[i], pw); |
| 455 | success = user_key_allowed2(pw, key, file); |
| 456 | xfree(file); |
| 457 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 458 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 459 | return success; |
| 460 | } |
| 461 | |
| 462 | Authmethod method_pubkey = { |
| 463 | "publickey", |
| 464 | userauth_pubkey, |
| 465 | &options.pubkey_authentication |
| 466 | }; |