Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame^] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.21 2010/03/04 10:36:03 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" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 60 | |
| 61 | /* import */ |
| 62 | extern ServerOptions options; |
| 63 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 64 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 65 | |
| 66 | static int |
| 67 | userauth_pubkey(Authctxt *authctxt) |
| 68 | { |
| 69 | Buffer b; |
| 70 | Key *key = NULL; |
| 71 | char *pkalg; |
| 72 | u_char *pkblob, *sig; |
| 73 | u_int alen, blen, slen; |
| 74 | int have_sig, pktype; |
| 75 | int authenticated = 0; |
| 76 | |
| 77 | if (!authctxt->valid) { |
| 78 | debug2("userauth_pubkey: disabled because of invalid user"); |
| 79 | return 0; |
| 80 | } |
| 81 | have_sig = packet_get_char(); |
| 82 | if (datafellows & SSH_BUG_PKAUTH) { |
| 83 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 84 | /* no explicit pkalg given */ |
| 85 | pkblob = packet_get_string(&blen); |
| 86 | buffer_init(&b); |
| 87 | buffer_append(&b, pkblob, blen); |
| 88 | /* so we have to extract the pkalg from the pkblob */ |
| 89 | pkalg = buffer_get_string(&b, &alen); |
| 90 | buffer_free(&b); |
| 91 | } else { |
| 92 | pkalg = packet_get_string(&alen); |
| 93 | pkblob = packet_get_string(&blen); |
| 94 | } |
| 95 | pktype = key_type_from_name(pkalg); |
| 96 | if (pktype == KEY_UNSPEC) { |
| 97 | /* this is perfectly legal */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 98 | logit("userauth_pubkey: unsupported public key algorithm: %s", |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 99 | pkalg); |
| 100 | goto done; |
| 101 | } |
| 102 | key = key_from_blob(pkblob, blen); |
| 103 | if (key == NULL) { |
| 104 | error("userauth_pubkey: cannot decode key: %s", pkalg); |
| 105 | goto done; |
| 106 | } |
| 107 | if (key->type != pktype) { |
| 108 | error("userauth_pubkey: type mismatch for decoded key " |
| 109 | "(received %d, expected %d)", key->type, pktype); |
| 110 | goto done; |
| 111 | } |
| 112 | if (have_sig) { |
| 113 | sig = packet_get_string(&slen); |
| 114 | packet_check_eom(); |
| 115 | buffer_init(&b); |
| 116 | if (datafellows & SSH_OLD_SESSIONID) { |
| 117 | buffer_append(&b, session_id2, session_id2_len); |
| 118 | } else { |
| 119 | buffer_put_string(&b, session_id2, session_id2_len); |
| 120 | } |
| 121 | /* reconstruct packet */ |
| 122 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 123 | buffer_put_cstring(&b, authctxt->user); |
| 124 | buffer_put_cstring(&b, |
| 125 | datafellows & SSH_BUG_PKSERVICE ? |
| 126 | "ssh-userauth" : |
| 127 | authctxt->service); |
| 128 | if (datafellows & SSH_BUG_PKAUTH) { |
| 129 | buffer_put_char(&b, have_sig); |
| 130 | } else { |
| 131 | buffer_put_cstring(&b, "publickey"); |
| 132 | buffer_put_char(&b, have_sig); |
| 133 | buffer_put_cstring(&b, pkalg); |
| 134 | } |
| 135 | buffer_put_string(&b, pkblob, blen); |
| 136 | #ifdef DEBUG_PK |
| 137 | buffer_dump(&b); |
| 138 | #endif |
| 139 | /* test for correct signature */ |
| 140 | authenticated = 0; |
| 141 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && |
| 142 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 143 | buffer_len(&b))) == 1) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 144 | authenticated = 1; |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 145 | buffer_free(&b); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 146 | xfree(sig); |
| 147 | } else { |
| 148 | debug("test whether pkalg/pkblob are acceptable"); |
| 149 | packet_check_eom(); |
| 150 | |
| 151 | /* XXX fake reply and always send PK_OK ? */ |
| 152 | /* |
| 153 | * XXX this allows testing whether a user is allowed |
| 154 | * to login: if you happen to have a valid pubkey this |
| 155 | * message is sent. the message is NEVER sent at all |
| 156 | * if a user is not allowed to login. is this an |
| 157 | * issue? -markus |
| 158 | */ |
| 159 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
| 160 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 161 | packet_put_string(pkalg, alen); |
| 162 | packet_put_string(pkblob, blen); |
| 163 | packet_send(); |
| 164 | packet_write_wait(); |
| 165 | authctxt->postponed = 1; |
| 166 | } |
| 167 | } |
| 168 | if (authenticated != 1) |
| 169 | auth_clear_options(); |
| 170 | done: |
| 171 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
| 172 | if (key != NULL) |
| 173 | key_free(key); |
| 174 | xfree(pkalg); |
| 175 | xfree(pkblob); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 176 | return authenticated; |
| 177 | } |
| 178 | |
| 179 | /* return 1 if user allows given key */ |
| 180 | static int |
| 181 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 182 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 183 | char line[SSH_MAX_PUBKEY_BYTES]; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 184 | const char *reason; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 185 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 186 | FILE *f; |
| 187 | u_long linenum = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 188 | Key *found; |
| 189 | char *fp; |
| 190 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 191 | /* Temporarily use the user's uid. */ |
| 192 | temporarily_use_uid(pw); |
| 193 | |
| 194 | debug("trying public key file %s", file); |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 195 | f = auth_openkeyfile(file, pw, options.strict_modes); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 196 | |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 197 | if (!f) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 198 | restore_uid(); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | found_key = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 203 | found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 204 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 205 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 206 | char *cp, *key_options = NULL; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 207 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 208 | auth_clear_options(); |
| 209 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 210 | /* Skip leading whitespace, empty and comment lines. */ |
| 211 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 212 | ; |
| 213 | if (!*cp || *cp == '\n' || *cp == '#') |
| 214 | continue; |
| 215 | |
| 216 | if (key_read(found, &cp) != 1) { |
| 217 | /* no key? check if there are options for this key */ |
| 218 | int quoted = 0; |
| 219 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 220 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 221 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 222 | if (*cp == '\\' && cp[1] == '"') |
| 223 | cp++; /* Skip both */ |
| 224 | else if (*cp == '"') |
| 225 | quoted = !quoted; |
| 226 | } |
| 227 | /* Skip remaining whitespace. */ |
| 228 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 229 | ; |
| 230 | if (key_read(found, &cp) != 1) { |
| 231 | debug2("user_key_allowed: advance: '%s'", cp); |
| 232 | /* still no key? advance to next line*/ |
| 233 | continue; |
| 234 | } |
| 235 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 236 | if (auth_parse_options(pw, key_options, file, linenum) != 1) |
| 237 | continue; |
| 238 | if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) { |
| 239 | if (!key_is_cert_authority) |
| 240 | continue; |
| 241 | if (!key_equal(found, key->cert->signature_key)) |
| 242 | continue; |
| 243 | debug("matching CA found: file %s, line %lu", |
| 244 | file, linenum); |
| 245 | fp = key_fingerprint(found, SSH_FP_MD5, |
| 246 | SSH_FP_HEX); |
| 247 | verbose("Found matching %s CA: %s", |
| 248 | key_type(found), fp); |
| 249 | xfree(fp); |
| 250 | if (key_cert_check_authority(key, 0, 0, pw->pw_name, |
| 251 | &reason) != 0) { |
| 252 | error("%s", reason); |
| 253 | auth_debug_add("%s", reason); |
| 254 | continue; |
| 255 | } |
| 256 | if (auth_cert_constraints(&key->cert->constraints, |
| 257 | pw) != 0) |
| 258 | continue; |
| 259 | found_key = 1; |
| 260 | break; |
| 261 | } else if (!key_is_cert_authority && key_equal(found, key)) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 262 | found_key = 1; |
| 263 | debug("matching key found: file %s, line %lu", |
| 264 | file, linenum); |
| 265 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
| 266 | verbose("Found matching %s key: %s", |
| 267 | key_type(found), fp); |
| 268 | xfree(fp); |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | restore_uid(); |
| 273 | fclose(f); |
| 274 | key_free(found); |
| 275 | if (!found_key) |
| 276 | debug2("key not found"); |
| 277 | return found_key; |
| 278 | } |
| 279 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame^] | 280 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 281 | static int |
| 282 | user_cert_trusted_ca(struct passwd *pw, Key *key) |
| 283 | { |
| 284 | char *key_fp, *ca_fp; |
| 285 | const char *reason; |
| 286 | int ret = 0; |
| 287 | |
| 288 | if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) |
| 289 | return 0; |
| 290 | |
| 291 | key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 292 | ca_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 293 | |
| 294 | if (key_in_file(key->cert->signature_key, |
| 295 | options.trusted_user_ca_keys, 1) != 1) { |
| 296 | debug2("%s: CA %s %s is not listed in %s", __func__, |
| 297 | key_type(key->cert->signature_key), ca_fp, |
| 298 | options.trusted_user_ca_keys); |
| 299 | goto out; |
| 300 | } |
| 301 | if (key_cert_check_authority(key, 0, 1, pw->pw_name, &reason) != 0) { |
| 302 | error("%s", reason); |
| 303 | auth_debug_add("%s", reason); |
| 304 | goto out; |
| 305 | } |
| 306 | if (auth_cert_constraints(&key->cert->constraints, pw) != 0) |
| 307 | goto out; |
| 308 | |
| 309 | verbose("%s certificate %s allowed by trusted %s key %s", |
| 310 | key_type(key), key_fp, key_type(key->cert->signature_key), ca_fp); |
| 311 | ret = 1; |
| 312 | |
| 313 | out: |
| 314 | if (key_fp != NULL) |
| 315 | xfree(key_fp); |
| 316 | if (ca_fp != NULL) |
| 317 | xfree(ca_fp); |
| 318 | return ret; |
| 319 | } |
| 320 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 321 | /* check whether given key is in .ssh/authorized_keys* */ |
| 322 | int |
| 323 | user_key_allowed(struct passwd *pw, Key *key) |
| 324 | { |
| 325 | int success; |
| 326 | char *file; |
| 327 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame^] | 328 | if (auth_key_is_revoked(key)) |
| 329 | return 0; |
| 330 | if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |
| 331 | return 0; |
| 332 | |
| 333 | success = user_cert_trusted_ca(pw, key); |
| 334 | if (success) |
| 335 | return success; |
| 336 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 337 | file = authorized_keys_file(pw); |
| 338 | success = user_key_allowed2(pw, key, file); |
| 339 | xfree(file); |
| 340 | if (success) |
| 341 | return success; |
| 342 | |
| 343 | /* try suffix "2" for backward compat, too */ |
| 344 | file = authorized_keys_file2(pw); |
| 345 | success = user_key_allowed2(pw, key, file); |
| 346 | xfree(file); |
| 347 | return success; |
| 348 | } |
| 349 | |
| 350 | Authmethod method_pubkey = { |
| 351 | "publickey", |
| 352 | userauth_pubkey, |
| 353 | &options.pubkey_authentication |
| 354 | }; |