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

1.21    ! djm         1: /* $OpenBSD: auth2-pubkey.c,v 1.20 2010/02/26 20:29:54 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:                        debug("matching CA found: file %s, line %lu",
                    243:                            file, linenum);
                    244:                        fp = key_fingerprint(found, SSH_FP_MD5,
                    245:                            SSH_FP_HEX);
                    246:                        verbose("Found matching %s CA: %s",
                    247:                            key_type(found), fp);
                    248:                        xfree(fp);
                    249:                        if (key_cert_check_authority(key, 0, 0, pw->pw_name,
                    250:                            &reason) != 0) {
                    251:                                error("%s", reason);
                    252:                                auth_debug_add("%s", reason);
                    253:                                continue;
                    254:                        }
                    255:                        if (auth_cert_constraints(&key->cert->constraints,
                    256:                            pw) != 0)
                    257:                                continue;
                    258:                        found_key = 1;
                    259:                        break;
                    260:                } else if (!key_is_cert_authority && key_equal(found, key)) {
1.1       markus    261:                        found_key = 1;
                    262:                        debug("matching key found: file %s, line %lu",
                    263:                            file, linenum);
                    264:                        fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
                    265:                        verbose("Found matching %s key: %s",
                    266:                            key_type(found), fp);
                    267:                        xfree(fp);
                    268:                        break;
                    269:                }
                    270:        }
                    271:        restore_uid();
                    272:        fclose(f);
                    273:        key_free(found);
                    274:        if (!found_key)
                    275:                debug2("key not found");
                    276:        return found_key;
                    277: }
                    278:
1.21    ! djm       279: /* Authenticate a certificate key against TrustedUserCAKeys */
        !           280: static int
        !           281: user_cert_trusted_ca(struct passwd *pw, Key *key)
        !           282: {
        !           283:        char *key_fp, *ca_fp;
        !           284:        const char *reason;
        !           285:        int ret = 0;
        !           286:
        !           287:        if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
        !           288:                return 0;
        !           289:
        !           290:        key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
        !           291:        ca_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
        !           292:
        !           293:        if (key_in_file(key->cert->signature_key,
        !           294:            options.trusted_user_ca_keys, 1) != 1) {
        !           295:                debug2("%s: CA %s %s is not listed in %s", __func__,
        !           296:                    key_type(key->cert->signature_key), ca_fp,
        !           297:                    options.trusted_user_ca_keys);
        !           298:                goto out;
        !           299:        }
        !           300:        if (key_cert_check_authority(key, 0, 1, pw->pw_name, &reason) != 0) {
        !           301:                error("%s", reason);
        !           302:                auth_debug_add("%s", reason);
        !           303:                goto out;
        !           304:        }
        !           305:        if (auth_cert_constraints(&key->cert->constraints, pw) != 0)
        !           306:                goto out;
        !           307:
        !           308:        verbose("%s certificate %s allowed by trusted %s key %s",
        !           309:            key_type(key), key_fp, key_type(key->cert->signature_key), ca_fp);
        !           310:        ret = 1;
        !           311:
        !           312:  out:
        !           313:        if (key_fp != NULL)
        !           314:                xfree(key_fp);
        !           315:        if (ca_fp != NULL)
        !           316:                xfree(ca_fp);
        !           317:        return ret;
        !           318: }
        !           319:
1.1       markus    320: /* check whether given key is in .ssh/authorized_keys* */
                    321: int
                    322: user_key_allowed(struct passwd *pw, Key *key)
                    323: {
                    324:        int success;
                    325:        char *file;
1.21    ! djm       326:
        !           327:        if (auth_key_is_revoked(key))
        !           328:                return 0;
        !           329:        if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
        !           330:                return 0;
        !           331:
        !           332:        success = user_cert_trusted_ca(pw, key);
        !           333:        if (success)
        !           334:                return success;
1.1       markus    335:
                    336:        file = authorized_keys_file(pw);
                    337:        success = user_key_allowed2(pw, key, file);
                    338:        xfree(file);
                    339:        if (success)
                    340:                return success;
                    341:
                    342:        /* try suffix "2" for backward compat, too */
                    343:        file = authorized_keys_file2(pw);
                    344:        success = user_key_allowed2(pw, key, file);
                    345:        xfree(file);
                    346:        return success;
                    347: }
1.2       markus    348:
                    349: Authmethod method_pubkey = {
                    350:        "publickey",
                    351:        userauth_pubkey,
                    352:        &options.pubkey_authentication
                    353: };