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