[BACK]Return to auth2-pubkey.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.24

1.24    ! djm         1: /* $OpenBSD: auth2-pubkey.c,v 1.23 2010/04/16 01:47:26 djm Exp $ */
1.1       markus      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:
1.10      stevesk    26:
                     27: #include <sys/types.h>
                     28: #include <sys/stat.h>
1.13      stevesk    29:
1.16      djm        30: #include <fcntl.h>
1.13      stevesk    31: #include <pwd.h>
1.14      stevesk    32: #include <stdio.h>
1.15      deraadt    33: #include <stdarg.h>
1.20      djm        34: #include <string.h>
                     35: #include <time.h>
1.17      dtucker    36: #include <unistd.h>
1.1       markus     37:
1.15      deraadt    38: #include "xmalloc.h"
1.8       dtucker    39: #include "ssh.h"
1.1       markus     40: #include "ssh2.h"
                     41: #include "packet.h"
                     42: #include "buffer.h"
                     43: #include "log.h"
                     44: #include "servconf.h"
                     45: #include "compat.h"
1.15      deraadt    46: #include "key.h"
                     47: #include "hostfile.h"
1.1       markus     48: #include "auth.h"
                     49: #include "pathnames.h"
                     50: #include "uidswap.h"
                     51: #include "auth-options.h"
                     52: #include "canohost.h"
1.15      deraadt    53: #ifdef GSSAPI
                     54: #include "ssh-gss.h"
                     55: #endif
1.1       markus     56: #include "monitor_wrap.h"
1.9       dtucker    57: #include "misc.h"
1.21      djm        58: #include "authfile.h"
1.24    ! djm        59: #include "match.h"
1.1       markus     60:
                     61: /* import */
                     62: extern ServerOptions options;
                     63: extern u_char *session_id2;
1.4       markus     64: extern u_int session_id2_len;
1.1       markus     65:
1.2       markus     66: static int
1.1       markus     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 */
1.3       itojun     98:                logit("userauth_pubkey: unsupported public key algorithm: %s",
1.1       markus     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),
1.6       markus    143:                    buffer_len(&b))) == 1)
1.1       markus    144:                        authenticated = 1;
1.6       markus    145:                buffer_free(&b);
1.1       markus    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);
                    176:        return authenticated;
                    177: }
                    178:
1.24    ! djm       179: static int
        !           180: match_principals_option(const char *principal_list, struct KeyCert *cert)
        !           181: {
        !           182:        char *result;
        !           183:        u_int i;
        !           184:
        !           185:        /* XXX percent_expand() sequences for authorized_principals? */
        !           186:
        !           187:        for (i = 0; i < cert->nprincipals; i++) {
        !           188:                if ((result = match_list(cert->principals[i],
        !           189:                    principal_list, NULL)) != NULL) {
        !           190:                        debug3("matched principal from key options \"%.100s\"",
        !           191:                            result);
        !           192:                        xfree(result);
        !           193:                        return 1;
        !           194:                }
        !           195:        }
        !           196:        return 0;
        !           197: }
        !           198:
        !           199: static int
        !           200: match_principals_file(const char *file, struct passwd *pw, struct KeyCert *cert)
        !           201: {
        !           202:        FILE *f;
        !           203:        char line[SSH_MAX_PUBKEY_BYTES], *cp;
        !           204:        u_long linenum = 0;
        !           205:        u_int i;
        !           206:
        !           207:        temporarily_use_uid(pw);
        !           208:        debug("trying authorized principals file %s", file);
        !           209:        if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
        !           210:                restore_uid();
        !           211:                return 0;
        !           212:        }
        !           213:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
        !           214:                /* Skip leading whitespace, empty and comment lines. */
        !           215:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
        !           216:                        ;
        !           217:                if (!*cp || *cp == '\n' || *cp == '#')
        !           218:                        continue;
        !           219:                line[strcspn(line, "\n")] = '\0';
        !           220:
        !           221:                for (i = 0; i < cert->nprincipals; i++) {
        !           222:                        if (strcmp(cp, cert->principals[i]) == 0) {
        !           223:                                debug3("matched principal from file \"%.100s\"",
        !           224:                                    cert->principals[i]);
        !           225:                                fclose(f);
        !           226:                                restore_uid();
        !           227:                                return 1;
        !           228:                        }
        !           229:                }
        !           230:        }
        !           231:        fclose(f);
        !           232:        restore_uid();
        !           233:        return 0;
        !           234: }
        !           235:
1.1       markus    236: /* return 1 if user allows given key */
                    237: static int
                    238: user_key_allowed2(struct passwd *pw, Key *key, char *file)
                    239: {
1.8       dtucker   240:        char line[SSH_MAX_PUBKEY_BYTES];
1.20      djm       241:        const char *reason;
1.18      dtucker   242:        int found_key = 0;
1.1       markus    243:        FILE *f;
                    244:        u_long linenum = 0;
                    245:        Key *found;
                    246:        char *fp;
                    247:
                    248:        /* Temporarily use the user's uid. */
                    249:        temporarily_use_uid(pw);
                    250:
                    251:        debug("trying public key file %s", file);
1.18      dtucker   252:        f = auth_openkeyfile(file, pw, options.strict_modes);
1.1       markus    253:
1.18      dtucker   254:        if (!f) {
1.1       markus    255:                restore_uid();
                    256:                return 0;
                    257:        }
                    258:
                    259:        found_key = 0;
1.20      djm       260:        found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
1.1       markus    261:
1.8       dtucker   262:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.7       avsm      263:                char *cp, *key_options = NULL;
1.8       dtucker   264:
1.20      djm       265:                auth_clear_options();
                    266:
1.1       markus    267:                /* Skip leading whitespace, empty and comment lines. */
                    268:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    269:                        ;
                    270:                if (!*cp || *cp == '\n' || *cp == '#')
                    271:                        continue;
                    272:
                    273:                if (key_read(found, &cp) != 1) {
                    274:                        /* no key?  check if there are options for this key */
                    275:                        int quoted = 0;
                    276:                        debug2("user_key_allowed: check options: '%s'", cp);
1.7       avsm      277:                        key_options = cp;
1.1       markus    278:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    279:                                if (*cp == '\\' && cp[1] == '"')
                    280:                                        cp++;   /* Skip both */
                    281:                                else if (*cp == '"')
                    282:                                        quoted = !quoted;
                    283:                        }
                    284:                        /* Skip remaining whitespace. */
                    285:                        for (; *cp == ' ' || *cp == '\t'; cp++)
                    286:                                ;
                    287:                        if (key_read(found, &cp) != 1) {
                    288:                                debug2("user_key_allowed: advance: '%s'", cp);
                    289:                                /* still no key?  advance to next line*/
                    290:                                continue;
                    291:                        }
                    292:                }
1.20      djm       293:                if (auth_parse_options(pw, key_options, file, linenum) != 1)
                    294:                        continue;
1.23      djm       295:                if (key_is_cert(key)) {
1.20      djm       296:                        if (!key_is_cert_authority)
                    297:                                continue;
                    298:                        if (!key_equal(found, key->cert->signature_key))
                    299:                                continue;
                    300:                        fp = key_fingerprint(found, SSH_FP_MD5,
                    301:                            SSH_FP_HEX);
1.22      djm       302:                        debug("matching CA found: file %s, line %lu, %s %s",
                    303:                            file, linenum, key_type(found), fp);
1.24    ! djm       304:                        /*
        !           305:                         * If the user has specified a list of principals as
        !           306:                         * a key option, then prefer that list to matching
        !           307:                         * their username in the certificate principals list.
        !           308:                         */
        !           309:                        if (authorized_principals != NULL &&
        !           310:                            !match_principals_option(authorized_principals,
        !           311:                            key->cert)) {
        !           312:                                reason = "Certificate does not contain an "
        !           313:                                    "authorized principal";
        !           314:  fail_reason:
1.22      djm       315:                                xfree(fp);
1.20      djm       316:                                error("%s", reason);
                    317:                                auth_debug_add("%s", reason);
                    318:                                continue;
                    319:                        }
1.24    ! djm       320:                        if (key_cert_check_authority(key, 0, 0,
        !           321:                            authorized_principals == NULL ? pw->pw_name : NULL,
        !           322:                            &reason) != 0)
        !           323:                                goto fail_reason;
1.23      djm       324:                        if (auth_cert_options(key, pw) != 0) {
1.22      djm       325:                                xfree(fp);
1.20      djm       326:                                continue;
1.22      djm       327:                        }
                    328:                        verbose("Accepted certificate ID \"%s\" "
                    329:                            "signed by %s CA %s via %s", key->cert->key_id,
                    330:                            key_type(found), fp, file);
                    331:                        xfree(fp);
1.20      djm       332:                        found_key = 1;
                    333:                        break;
                    334:                } else if (!key_is_cert_authority && key_equal(found, key)) {
1.1       markus    335:                        found_key = 1;
                    336:                        debug("matching key found: file %s, line %lu",
                    337:                            file, linenum);
                    338:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
                    339:                        verbose("Found matching %s key: %s",
                    340:                            key_type(found), fp);
                    341:                        xfree(fp);
                    342:                        break;
                    343:                }
                    344:        }
                    345:        restore_uid();
                    346:        fclose(f);
                    347:        key_free(found);
                    348:        if (!found_key)
                    349:                debug2("key not found");
                    350:        return found_key;
                    351: }
                    352:
1.21      djm       353: /* Authenticate a certificate key against TrustedUserCAKeys */
                    354: static int
                    355: user_cert_trusted_ca(struct passwd *pw, Key *key)
                    356: {
1.24    ! djm       357:        char *ca_fp, *principals_file = NULL;
1.21      djm       358:        const char *reason;
                    359:        int ret = 0;
                    360:
                    361:        if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
                    362:                return 0;
                    363:
1.22      djm       364:        ca_fp = key_fingerprint(key->cert->signature_key,
                    365:            SSH_FP_MD5, SSH_FP_HEX);
1.21      djm       366:
                    367:        if (key_in_file(key->cert->signature_key,
                    368:            options.trusted_user_ca_keys, 1) != 1) {
                    369:                debug2("%s: CA %s %s is not listed in %s", __func__,
                    370:                    key_type(key->cert->signature_key), ca_fp,
                    371:                    options.trusted_user_ca_keys);
                    372:                goto out;
                    373:        }
1.24    ! djm       374:        /*
        !           375:         * If AuthorizedPrincipals is in use, then compare the certificate
        !           376:         * principals against the names in that file rather than matching
        !           377:         * against the username.
        !           378:         */
        !           379:        if ((principals_file = authorized_principals_file(pw)) != NULL) {
        !           380:                if (!match_principals_file(principals_file, pw, key->cert)) {
        !           381:                        reason = "Certificate does not contain an "
        !           382:                            "authorized principal";
        !           383:  fail_reason:
        !           384:                        error("%s", reason);
        !           385:                        auth_debug_add("%s", reason);
        !           386:                        goto out;
        !           387:                }
1.21      djm       388:        }
1.24    ! djm       389:        if (key_cert_check_authority(key, 0, 1,
        !           390:            principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
        !           391:                goto fail_reason;
1.23      djm       392:        if (auth_cert_options(key, pw) != 0)
1.21      djm       393:                goto out;
                    394:
1.22      djm       395:        verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
                    396:            key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
                    397:            options.trusted_user_ca_keys);
1.21      djm       398:        ret = 1;
                    399:
                    400:  out:
1.24    ! djm       401:        if (principals_file != NULL)
        !           402:                xfree(principals_file);
1.21      djm       403:        if (ca_fp != NULL)
                    404:                xfree(ca_fp);
                    405:        return ret;
                    406: }
                    407:
1.1       markus    408: /* check whether given key is in .ssh/authorized_keys* */
                    409: int
                    410: user_key_allowed(struct passwd *pw, Key *key)
                    411: {
                    412:        int success;
                    413:        char *file;
1.21      djm       414:
                    415:        if (auth_key_is_revoked(key))
                    416:                return 0;
                    417:        if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
                    418:                return 0;
                    419:
                    420:        success = user_cert_trusted_ca(pw, key);
                    421:        if (success)
                    422:                return success;
1.1       markus    423:
                    424:        file = authorized_keys_file(pw);
                    425:        success = user_key_allowed2(pw, key, file);
                    426:        xfree(file);
                    427:        if (success)
                    428:                return success;
                    429:
                    430:        /* try suffix "2" for backward compat, too */
                    431:        file = authorized_keys_file2(pw);
                    432:        success = user_key_allowed2(pw, key, file);
                    433:        xfree(file);
                    434:        return success;
                    435: }
1.2       markus    436:
                    437: Authmethod method_pubkey = {
                    438:        "publickey",
                    439:        userauth_pubkey,
                    440:        &options.pubkey_authentication
                    441: };