[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.22

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