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

Annotation of src/usr.bin/ssh/hostfile.c, Revision 1.50

1.50    ! djm         1: /* $OpenBSD: hostfile.c,v 1.49 2010/11/29 23:45:51 djm Exp $ */
1.1       deraadt     2: /*
1.8       deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Functions for manipulating the known hosts files.
1.16      markus      7:  *
1.20      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
                     13:  *
                     14:  *
1.28      markus     15:  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
1.20      deraadt    16:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
                     17:  *
                     18:  * Redistribution and use in source and binary forms, with or without
                     19:  * modification, are permitted provided that the following conditions
                     20:  * are met:
                     21:  * 1. Redistributions of source code must retain the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer.
                     23:  * 2. Redistributions in binary form must reproduce the above copyright
                     24:  *    notice, this list of conditions and the following disclaimer in the
                     25:  *    documentation and/or other materials provided with the distribution.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     28:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     29:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     30:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     31:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     32:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     33:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     34:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     35:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     36:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.8       deraadt    37:  */
1.1       deraadt    38:
1.41      stevesk    39: #include <sys/types.h>
                     40:
                     41: #include <netinet/in.h>
1.33      djm        42:
1.43      stevesk    43: #include <openssl/hmac.h>
                     44: #include <openssl/sha.h>
                     45:
1.33      djm        46: #include <resolv.h>
1.44      stevesk    47: #include <stdio.h>
1.43      stevesk    48: #include <stdlib.h>
1.42      stevesk    49: #include <string.h>
1.1       deraadt    50:
1.45      deraadt    51: #include "xmalloc.h"
1.14      markus     52: #include "match.h"
                     53: #include "key.h"
                     54: #include "hostfile.h"
1.24      markus     55: #include "log.h"
1.49      djm        56: #include "misc.h"
                     57:
                     58: struct hostkeys {
                     59:        struct hostkey_entry *entries;
                     60:        u_int num_entries;
                     61: };
1.33      djm        62:
                     63: static int
                     64: extract_salt(const char *s, u_int l, char *salt, size_t salt_len)
                     65: {
                     66:        char *p, *b64salt;
                     67:        u_int b64len;
                     68:        int ret;
                     69:
                     70:        if (l < sizeof(HASH_MAGIC) - 1) {
                     71:                debug2("extract_salt: string too short");
                     72:                return (-1);
                     73:        }
                     74:        if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
                     75:                debug2("extract_salt: invalid magic identifier");
                     76:                return (-1);
                     77:        }
                     78:        s += sizeof(HASH_MAGIC) - 1;
                     79:        l -= sizeof(HASH_MAGIC) - 1;
                     80:        if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
                     81:                debug2("extract_salt: missing salt termination character");
                     82:                return (-1);
                     83:        }
                     84:
                     85:        b64len = p - s;
                     86:        /* Sanity check */
                     87:        if (b64len == 0 || b64len > 1024) {
                     88:                debug2("extract_salt: bad encoded salt length %u", b64len);
                     89:                return (-1);
                     90:        }
                     91:        b64salt = xmalloc(1 + b64len);
                     92:        memcpy(b64salt, s, b64len);
                     93:        b64salt[b64len] = '\0';
                     94:
                     95:        ret = __b64_pton(b64salt, salt, salt_len);
                     96:        xfree(b64salt);
                     97:        if (ret == -1) {
                     98:                debug2("extract_salt: salt decode error");
                     99:                return (-1);
                    100:        }
                    101:        if (ret != SHA_DIGEST_LENGTH) {
1.36      dtucker   102:                debug2("extract_salt: expected salt len %d, got %d",
                    103:                    SHA_DIGEST_LENGTH, ret);
1.33      djm       104:                return (-1);
                    105:        }
1.34      deraadt   106:
1.33      djm       107:        return (0);
                    108: }
                    109:
                    110: char *
                    111: host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
                    112: {
                    113:        const EVP_MD *md = EVP_sha1();
                    114:        HMAC_CTX mac_ctx;
                    115:        char salt[256], result[256], uu_salt[512], uu_result[512];
                    116:        static char encoded[1024];
                    117:        u_int i, len;
                    118:
                    119:        len = EVP_MD_size(md);
                    120:
                    121:        if (name_from_hostfile == NULL) {
                    122:                /* Create new salt */
                    123:                for (i = 0; i < len; i++)
                    124:                        salt[i] = arc4random();
                    125:        } else {
                    126:                /* Extract salt from known host entry */
                    127:                if (extract_salt(name_from_hostfile, src_len, salt,
                    128:                    sizeof(salt)) == -1)
                    129:                        return (NULL);
                    130:        }
                    131:
                    132:        HMAC_Init(&mac_ctx, salt, len, md);
                    133:        HMAC_Update(&mac_ctx, host, strlen(host));
                    134:        HMAC_Final(&mac_ctx, result, NULL);
                    135:        HMAC_cleanup(&mac_ctx);
                    136:
1.34      deraadt   137:        if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
1.33      djm       138:            __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
                    139:                fatal("host_hash: __b64_ntop failed");
                    140:
                    141:        snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
                    142:            HASH_DELIM, uu_result);
                    143:
                    144:        return (encoded);
                    145: }
1.1       deraadt   146:
1.9       markus    147: /*
1.14      markus    148:  * Parses an RSA (number of bits, e, n) or DSA key from a string.  Moves the
                    149:  * pointer over the key.  Skips any whitespace at the beginning and at end.
1.9       markus    150:  */
1.1       deraadt   151:
1.29      jakob     152: int
1.22      markus    153: hostfile_read_key(char **cpp, u_int *bitsp, Key *ret)
1.1       deraadt   154: {
1.7       markus    155:        char *cp;
                    156:
                    157:        /* Skip leading whitespace. */
1.9       markus    158:        for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
                    159:                ;
1.1       deraadt   160:
1.21      markus    161:        if (key_read(ret, &cp) != 1)
1.7       markus    162:                return 0;
                    163:
                    164:        /* Skip trailing whitespace. */
1.9       markus    165:        for (; *cp == ' ' || *cp == '\t'; cp++)
                    166:                ;
1.7       markus    167:
                    168:        /* Return results. */
                    169:        *cpp = cp;
1.49      djm       170:        if (bitsp != NULL)
                    171:                *bitsp = key_size(ret);
1.7       markus    172:        return 1;
1.14      markus    173: }
1.1       deraadt   174:
1.27      itojun    175: static int
1.49      djm       176: hostfile_check_key(int bits, const Key *key, const char *host,
                    177:     const char *filename, u_long linenum)
1.1       deraadt   178: {
1.21      markus    179:        if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
1.14      markus    180:                return 1;
                    181:        if (bits != BN_num_bits(key->rsa->n)) {
1.49      djm       182:                logit("Warning: %s, line %lu: keysize mismatch for host %s: "
1.14      markus    183:                    "actual %d vs. announced %d.",
                    184:                    filename, linenum, host, BN_num_bits(key->rsa->n), bits);
1.49      djm       185:                logit("Warning: replace %d with %d in %s, line %lu.",
1.14      markus    186:                    bits, BN_num_bits(key->rsa->n), filename, linenum);
1.1       deraadt   187:        }
1.14      markus    188:        return 1;
1.1       deraadt   189: }
                    190:
1.49      djm       191: static HostkeyMarker
1.48      djm       192: check_markers(char **cpp)
                    193: {
                    194:        char marker[32], *sp, *cp = *cpp;
                    195:        int ret = MRK_NONE;
                    196:
                    197:        while (*cp == '@') {
                    198:                /* Only one marker is allowed */
                    199:                if (ret != MRK_NONE)
                    200:                        return MRK_ERROR;
                    201:                /* Markers are terminated by whitespace */
                    202:                if ((sp = strchr(cp, ' ')) == NULL &&
                    203:                    (sp = strchr(cp, '\t')) == NULL)
                    204:                        return MRK_ERROR;
                    205:                /* Extract marker for comparison */
                    206:                if (sp <= cp + 1 || sp >= cp + sizeof(marker))
                    207:                        return MRK_ERROR;
                    208:                memcpy(marker, cp, sp - cp);
                    209:                marker[sp - cp] = '\0';
                    210:                if (strcmp(marker, CA_MARKER) == 0)
                    211:                        ret = MRK_CA;
                    212:                else if (strcmp(marker, REVOKE_MARKER) == 0)
                    213:                        ret = MRK_REVOKE;
                    214:                else
                    215:                        return MRK_ERROR;
                    216:
                    217:                /* Skip past marker and any whitespace that follows it */
                    218:                cp = sp;
                    219:                for (; *cp == ' ' || *cp == '\t'; cp++)
                    220:                        ;
                    221:        }
                    222:        *cpp = cp;
                    223:        return ret;
                    224: }
                    225:
1.49      djm       226: struct hostkeys *
                    227: init_hostkeys(void)
                    228: {
                    229:        struct hostkeys *ret = xcalloc(1, sizeof(*ret));
                    230:
                    231:        ret->entries = NULL;
                    232:        return ret;
                    233: }
1.1       deraadt   234:
1.49      djm       235: void
                    236: load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
1.1       deraadt   237: {
1.7       markus    238:        FILE *f;
                    239:        char line[8192];
1.49      djm       240:        u_long linenum = 0, num_loaded = 0;
1.33      djm       241:        char *cp, *cp2, *hashed_host;
1.49      djm       242:        HostkeyMarker marker;
                    243:        Key *key;
                    244:        int kbits;
                    245:
                    246:        if ((f = fopen(path, "r")) == NULL)
                    247:                return;
                    248:        debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
                    249:            __func__, host, path);
                    250:        while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
1.7       markus    251:                cp = line;
                    252:
1.9       markus    253:                /* Skip any leading whitespace, comments and empty lines. */
                    254:                for (; *cp == ' ' || *cp == '\t'; cp++)
                    255:                        ;
1.7       markus    256:                if (!*cp || *cp == '#' || *cp == '\n')
                    257:                        continue;
                    258:
1.49      djm       259:                if ((marker = check_markers(&cp)) == MRK_ERROR) {
                    260:                        verbose("%s: invalid marker at %s:%lu",
                    261:                            __func__, path, linenum);
1.47      djm       262:                        continue;
1.49      djm       263:                }
1.47      djm       264:
1.7       markus    265:                /* Find the end of the host name portion. */
1.9       markus    266:                for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
                    267:                        ;
1.7       markus    268:
                    269:                /* Check if the host name matches. */
1.33      djm       270:                if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
                    271:                        if (*cp != HASH_DELIM)
                    272:                                continue;
                    273:                        hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
                    274:                        if (hashed_host == NULL) {
1.49      djm       275:                                debug("Invalid hashed host line %lu of %s",
                    276:                                    linenum, path);
1.33      djm       277:                                continue;
                    278:                        }
                    279:                        if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
                    280:                                continue;
                    281:                }
1.7       markus    282:
                    283:                /* Got a match.  Skip host name. */
                    284:                cp = cp2;
                    285:
1.9       markus    286:                /*
                    287:                 * Extract the key from the line.  This will skip any leading
                    288:                 * whitespace.  Ignore badly formatted lines.
                    289:                 */
1.49      djm       290:                key = key_new(KEY_UNSPEC);
                    291:                if (!hostfile_read_key(&cp, &kbits, key)) {
                    292:                        key_free(key);
                    293:                        key = key_new(KEY_RSA1);
                    294:                        if (!hostfile_read_key(&cp, &kbits, key)) {
                    295:                                key_free(key);
                    296:                                continue;
                    297:                        }
                    298:                }
                    299:                if (!hostfile_check_key(kbits, key, host, path, linenum))
1.14      markus    300:                        continue;
1.23      markus    301:
1.49      djm       302:                debug3("%s: found %skey type %s in file %s:%lu", __func__,
                    303:                    marker == MRK_NONE ? "" :
                    304:                    (marker == MRK_CA ? "ca " : "revoked "),
                    305:                    key_type(key), path, linenum);
                    306:                hostkeys->entries = xrealloc(hostkeys->entries,
                    307:                    hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
                    308:                hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
                    309:                hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
                    310:                hostkeys->entries[hostkeys->num_entries].line = linenum;
                    311:                hostkeys->entries[hostkeys->num_entries].key = key;
                    312:                hostkeys->entries[hostkeys->num_entries].marker = marker;
                    313:                hostkeys->num_entries++;
                    314:                num_loaded++;
                    315:        }
                    316:        debug3("%s: loaded %lu keys", __func__, num_loaded);
1.50    ! djm       317:        fclose(f);
1.49      djm       318:        return;
                    319: }
                    320:
                    321: void
                    322: free_hostkeys(struct hostkeys *hostkeys)
                    323: {
                    324:        u_int i;
                    325:
                    326:        for (i = 0; i < hostkeys->num_entries; i++) {
                    327:                xfree(hostkeys->entries[i].host);
                    328:                xfree(hostkeys->entries[i].file);
                    329:                key_free(hostkeys->entries[i].key);
                    330:                bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
                    331:        }
                    332:        if (hostkeys->entries != NULL)
                    333:                xfree(hostkeys->entries);
                    334:        hostkeys->entries = NULL;
                    335:        hostkeys->num_entries = 0;
                    336:        xfree(hostkeys);
                    337: }
                    338:
                    339: static int
                    340: check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
                    341: {
                    342:        int is_cert = key_is_cert(k);
                    343:        u_int i;
1.7       markus    344:
1.49      djm       345:        for (i = 0; i < hostkeys->num_entries; i++) {
                    346:                if (hostkeys->entries[i].marker != MRK_REVOKE)
1.30      markus    347:                        continue;
1.49      djm       348:                if (key_equal_public(k, hostkeys->entries[i].key))
                    349:                        return -1;
                    350:                if (is_cert &&
                    351:                    key_equal_public(k->cert->signature_key,
                    352:                    hostkeys->entries[i].key))
                    353:                        return -1;
                    354:        }
                    355:        return 0;
                    356: }
                    357:
                    358: /*
                    359:  * Match keys against a specified key, or look one up by key type.
                    360:  *
                    361:  * If looking for a keytype (key == NULL) and one is found then return
                    362:  * HOST_FOUND, otherwise HOST_NEW.
                    363:  *
                    364:  * If looking for a key (key != NULL):
                    365:  *  1. If the key is a cert and a matching CA is found, return HOST_OK
                    366:  *  2. If the key is not a cert and a matching key is found, return HOST_OK
                    367:  *  3. If no key matches but a key with a different type is found, then
                    368:  *     return HOST_CHANGED
                    369:  *  4. If no matching keys are found, then return HOST_NEW.
                    370:  *
                    371:  * Finally, check any found key is not revoked.
                    372:  */
                    373: static HostStatus
                    374: check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
                    375:     Key *k, int keytype, const struct hostkey_entry **found)
                    376: {
                    377:        u_int i;
                    378:        HostStatus end_return = HOST_NEW;
                    379:        int want_cert = key_is_cert(k);
                    380:        HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
                    381:        int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
                    382:
                    383:        if (found != NULL)
                    384:                *found = NULL;
1.30      markus    385:
1.49      djm       386:        for (i = 0; i < hostkeys->num_entries; i++) {
                    387:                if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
                    388:                        continue;
                    389:                if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
1.30      markus    390:                        continue;
1.49      djm       391:                if (hostkeys->entries[i].marker != want_marker)
                    392:                        continue;
                    393:                if (k == NULL) {
                    394:                        if (hostkeys->entries[i].key->type != keytype)
                    395:                                continue;
                    396:                        end_return = HOST_FOUND;
                    397:                        if (found != NULL)
                    398:                                *found = hostkeys->entries + i;
                    399:                        k = hostkeys->entries[i].key;
                    400:                        break;
                    401:                }
                    402:                if (want_cert) {
                    403:                        if (key_equal_public(k->cert->signature_key,
                    404:                            hostkeys->entries[i].key)) {
                    405:                                /* A matching CA exists */
                    406:                                end_return = HOST_OK;
                    407:                                if (found != NULL)
                    408:                                        *found = hostkeys->entries + i;
                    409:                                break;
1.48      djm       410:                        }
1.49      djm       411:                } else {
                    412:                        if (key_equal(k, hostkeys->entries[i].key)) {
                    413:                                end_return = HOST_OK;
                    414:                                if (found != NULL)
                    415:                                        *found = hostkeys->entries + i;
                    416:                                break;
1.48      djm       417:                        }
1.49      djm       418:                        /* A non-maching key exists */
                    419:                        end_return = HOST_CHANGED;
                    420:                        if (found != NULL)
                    421:                                *found = hostkeys->entries + i;
1.48      djm       422:                }
1.1       deraadt   423:        }
1.49      djm       424:        if (check_key_not_revoked(hostkeys, k) != 0) {
                    425:                end_return = HOST_REVOKED;
                    426:                if (found != NULL)
                    427:                        *found = NULL;
                    428:        }
1.7       markus    429:        return end_return;
1.30      markus    430: }
1.49      djm       431:
1.30      markus    432: HostStatus
1.49      djm       433: check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
                    434:     const struct hostkey_entry **found)
1.30      markus    435: {
                    436:        if (key == NULL)
                    437:                fatal("no key to look up");
1.49      djm       438:        return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
1.30      markus    439: }
                    440:
                    441: int
1.49      djm       442: lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
                    443:     const struct hostkey_entry **found)
1.30      markus    444: {
1.49      djm       445:        return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
                    446:            found) == HOST_FOUND);
1.1       deraadt   447: }
                    448:
1.9       markus    449: /*
                    450:  * Appends an entry to the host file.  Returns false if the entry could not
                    451:  * be appended.
                    452:  */
1.1       deraadt   453:
1.2       provos    454: int
1.34      deraadt   455: add_host_to_hostfile(const char *filename, const char *host, const Key *key,
1.33      djm       456:     int store_hash)
1.1       deraadt   457: {
1.7       markus    458:        FILE *f;
1.14      markus    459:        int success = 0;
1.35      dtucker   460:        char *hashed_host = NULL;
1.33      djm       461:
1.14      markus    462:        if (key == NULL)
1.17      markus    463:                return 1;       /* XXX ? */
1.7       markus    464:        f = fopen(filename, "a");
                    465:        if (!f)
                    466:                return 0;
1.33      djm       467:
                    468:        if (store_hash) {
                    469:                if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
                    470:                        error("add_host_to_hostfile: host_hash failed");
                    471:                        fclose(f);
                    472:                        return 0;
                    473:                }
                    474:        }
                    475:        fprintf(f, "%s ", store_hash ? hashed_host : host);
                    476:
1.14      markus    477:        if (key_write(key, f)) {
                    478:                success = 1;
                    479:        } else {
1.17      markus    480:                error("add_host_to_hostfile: saving key in %s failed", filename);
1.7       markus    481:        }
1.17      markus    482:        fprintf(f, "\n");
1.7       markus    483:        fclose(f);
1.14      markus    484:        return success;
1.1       deraadt   485: }