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

Annotation of src/usr.bin/ssh/dns.c, Revision 1.30

1.30    ! logan       1: /* $OpenBSD: dns.c,v 1.29 2013/05/17 00:13:13 djm Exp $ */
1.1       jakob       2:
                      3: /*
                      4:  * Copyright (c) 2003 Wesley Griffin. All rights reserved.
                      5:  * Copyright (c) 2003 Jakob Schlyter. All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
1.20      stevesk    27:
                     28: #include <sys/types.h>
                     29: #include <sys/socket.h>
1.1       jakob      30:
                     31: #include <netdb.h>
1.22      stevesk    32: #include <stdio.h>
1.21      stevesk    33: #include <string.h>
1.1       jakob      34:
                     35: #include "xmalloc.h"
                     36: #include "key.h"
                     37: #include "dns.h"
                     38: #include "log.h"
                     39:
                     40: static const char *errset_text[] = {
                     41:        "success",              /* 0 ERRSET_SUCCESS */
                     42:        "out of memory",        /* 1 ERRSET_NOMEMORY */
                     43:        "general failure",      /* 2 ERRSET_FAIL */
                     44:        "invalid parameter",    /* 3 ERRSET_INVAL */
                     45:        "name does not exist",  /* 4 ERRSET_NONAME */
                     46:        "data does not exist",  /* 5 ERRSET_NODATA */
                     47: };
                     48:
                     49: static const char *
1.10      avsm       50: dns_result_totext(unsigned int res)
1.1       jakob      51: {
1.10      avsm       52:        switch (res) {
1.1       jakob      53:        case ERRSET_SUCCESS:
                     54:                return errset_text[ERRSET_SUCCESS];
                     55:        case ERRSET_NOMEMORY:
                     56:                return errset_text[ERRSET_NOMEMORY];
                     57:        case ERRSET_FAIL:
                     58:                return errset_text[ERRSET_FAIL];
                     59:        case ERRSET_INVAL:
                     60:                return errset_text[ERRSET_INVAL];
                     61:        case ERRSET_NONAME:
                     62:                return errset_text[ERRSET_NONAME];
                     63:        case ERRSET_NODATA:
                     64:                return errset_text[ERRSET_NODATA];
                     65:        default:
                     66:                return "unknown error";
                     67:        }
                     68: }
                     69:
                     70: /*
                     71:  * Read SSHFP parameters from key buffer.
                     72:  */
                     73: static int
                     74: dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
1.26      djm        75:     u_char **digest, u_int *digest_len, Key *key)
1.1       jakob      76: {
                     77:        int success = 0;
1.28      djm        78:        enum fp_type fp_type = 0;
1.1       jakob      79:
                     80:        switch (key->type) {
                     81:        case KEY_RSA:
1.3       jakob      82:                *algorithm = SSHFP_KEY_RSA;
1.28      djm        83:                if (!*digest_type)
                     84:                        *digest_type = SSHFP_HASH_SHA1;
1.1       jakob      85:                break;
                     86:        case KEY_DSA:
1.3       jakob      87:                *algorithm = SSHFP_KEY_DSA;
1.28      djm        88:                if (!*digest_type)
                     89:                        *digest_type = SSHFP_HASH_SHA1;
                     90:                break;
                     91:        case KEY_ECDSA:
                     92:                *algorithm = SSHFP_KEY_ECDSA;
1.30    ! logan      93:                if (!*digest_type)
        !            94:                        *digest_type = SSHFP_HASH_SHA256;
        !            95:                break;
        !            96:        case KEY_ED25519:
        !            97:                *algorithm = SSHFP_KEY_ED25519;
1.28      djm        98:                if (!*digest_type)
                     99:                        *digest_type = SSHFP_HASH_SHA256;
1.1       jakob     100:                break;
                    101:        default:
1.14      stevesk   102:                *algorithm = SSHFP_KEY_RESERVED; /* 0 */
1.28      djm       103:                *digest_type = SSHFP_HASH_RESERVED; /* 0 */
                    104:        }
                    105:
                    106:        switch (*digest_type) {
                    107:        case SSHFP_HASH_SHA1:
                    108:                fp_type = SSH_FP_SHA1;
                    109:                break;
                    110:        case SSHFP_HASH_SHA256:
                    111:                fp_type = SSH_FP_SHA256;
                    112:                break;
                    113:        default:
                    114:                *digest_type = SSHFP_HASH_RESERVED; /* 0 */
1.1       jakob     115:        }
                    116:
1.28      djm       117:        if (*algorithm && *digest_type) {
                    118:                *digest = key_fingerprint_raw(key, fp_type, digest_len);
1.14      stevesk   119:                if (*digest == NULL)
                    120:                        fatal("dns_read_key: null from key_fingerprint_raw()");
1.1       jakob     121:                success = 1;
                    122:        } else {
                    123:                *digest = NULL;
                    124:                *digest_len = 0;
                    125:                success = 0;
                    126:        }
                    127:
                    128:        return success;
                    129: }
                    130:
                    131: /*
                    132:  * Read SSHFP parameters from rdata buffer.
                    133:  */
                    134: static int
                    135: dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type,
                    136:     u_char **digest, u_int *digest_len, u_char *rdata, int rdata_len)
                    137: {
                    138:        int success = 0;
                    139:
1.3       jakob     140:        *algorithm = SSHFP_KEY_RESERVED;
                    141:        *digest_type = SSHFP_HASH_RESERVED;
1.1       jakob     142:
                    143:        if (rdata_len >= 2) {
                    144:                *algorithm = rdata[0];
                    145:                *digest_type = rdata[1];
                    146:                *digest_len = rdata_len - 2;
                    147:
                    148:                if (*digest_len > 0) {
                    149:                        *digest = (u_char *) xmalloc(*digest_len);
                    150:                        memcpy(*digest, rdata + 2, *digest_len);
                    151:                } else {
1.18      deraadt   152:                        *digest = (u_char *)xstrdup("");
1.1       jakob     153:                }
                    154:
                    155:                success = 1;
                    156:        }
                    157:
                    158:        return success;
                    159: }
                    160:
1.11      jakob     161: /*
                    162:  * Check if hostname is numerical.
                    163:  * Returns -1 if hostname is numeric, 0 otherwise
                    164:  */
                    165: static int
                    166: is_numeric_hostname(const char *hostname)
                    167: {
                    168:        struct addrinfo hints, *ai;
                    169:
1.25      dtucker   170:        /*
                    171:         * We shouldn't ever get a null host but if we do then log an error
                    172:         * and return -1 which stops DNS key fingerprint processing.
                    173:         */
                    174:        if (hostname == NULL) {
                    175:                error("is_numeric_hostname called with NULL hostname");
                    176:                return -1;
                    177:        }
                    178:
1.11      jakob     179:        memset(&hints, 0, sizeof(hints));
                    180:        hints.ai_socktype = SOCK_DGRAM;
                    181:        hints.ai_flags = AI_NUMERICHOST;
                    182:
1.25      dtucker   183:        if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) {
1.11      jakob     184:                freeaddrinfo(ai);
                    185:                return -1;
                    186:        }
                    187:
                    188:        return 0;
                    189: }
1.1       jakob     190:
                    191: /*
                    192:  * Verify the given hostname, address and host key using DNS.
1.9       djm       193:  * Returns 0 if lookup succeeds, -1 otherwise
1.1       jakob     194:  */
                    195: int
                    196: verify_host_key_dns(const char *hostname, struct sockaddr *address,
1.26      djm       197:     Key *hostkey, int *flags)
1.1       jakob     198: {
1.12      djm       199:        u_int counter;
1.1       jakob     200:        int result;
1.4       jakob     201:        struct rrsetinfo *fingerprints = NULL;
1.1       jakob     202:
                    203:        u_int8_t hostkey_algorithm;
1.28      djm       204:        u_int8_t hostkey_digest_type = SSHFP_HASH_RESERVED;
1.1       jakob     205:        u_char *hostkey_digest;
                    206:        u_int hostkey_digest_len;
                    207:
                    208:        u_int8_t dnskey_algorithm;
                    209:        u_int8_t dnskey_digest_type;
                    210:        u_char *dnskey_digest;
                    211:        u_int dnskey_digest_len;
                    212:
1.8       jakob     213:        *flags = 0;
1.1       jakob     214:
1.16      stevesk   215:        debug3("verify_host_key_dns");
1.1       jakob     216:        if (hostkey == NULL)
                    217:                fatal("No key to look up!");
1.11      jakob     218:
                    219:        if (is_numeric_hostname(hostname)) {
                    220:                debug("skipped DNS lookup for numerical hostname");
                    221:                return -1;
                    222:        }
1.1       jakob     223:
                    224:        result = getrrsetbyname(hostname, DNS_RDATACLASS_IN,
1.4       jakob     225:            DNS_RDATATYPE_SSHFP, 0, &fingerprints);
1.1       jakob     226:        if (result) {
                    227:                verbose("DNS lookup error: %s", dns_result_totext(result));
1.8       jakob     228:                return -1;
1.1       jakob     229:        }
                    230:
1.8       jakob     231:        if (fingerprints->rri_flags & RRSET_VALIDATED) {
                    232:                *flags |= DNS_VERIFY_SECURE;
                    233:                debug("found %d secure fingerprints in DNS",
                    234:                    fingerprints->rri_nrdatas);
                    235:        } else {
                    236:                debug("found %d insecure fingerprints in DNS",
                    237:                    fingerprints->rri_nrdatas);
1.1       jakob     238:        }
                    239:
1.28      djm       240:        /* Initialize default host key parameters */
1.1       jakob     241:        if (!dns_read_key(&hostkey_algorithm, &hostkey_digest_type,
                    242:            &hostkey_digest, &hostkey_digest_len, hostkey)) {
                    243:                error("Error calculating host key fingerprint.");
1.5       jakob     244:                freerrset(fingerprints);
1.8       jakob     245:                return -1;
1.1       jakob     246:        }
                    247:
1.8       jakob     248:        if (fingerprints->rri_nrdatas)
                    249:                *flags |= DNS_VERIFY_FOUND;
                    250:
1.24      stevesk   251:        for (counter = 0; counter < fingerprints->rri_nrdatas; counter++) {
1.1       jakob     252:                /*
                    253:                 * Extract the key from the answer. Ignore any badly
1.4       jakob     254:                 * formatted fingerprints.
1.1       jakob     255:                 */
                    256:                if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type,
                    257:                    &dnskey_digest, &dnskey_digest_len,
1.4       jakob     258:                    fingerprints->rri_rdatas[counter].rdi_data,
                    259:                    fingerprints->rri_rdatas[counter].rdi_length)) {
1.1       jakob     260:                        verbose("Error parsing fingerprint from DNS.");
                    261:                        continue;
                    262:                }
                    263:
1.28      djm       264:                if (hostkey_digest_type != dnskey_digest_type) {
                    265:                        hostkey_digest_type = dnskey_digest_type;
1.29      djm       266:                        free(hostkey_digest);
1.28      djm       267:
                    268:                        /* Initialize host key parameters */
                    269:                        if (!dns_read_key(&hostkey_algorithm,
                    270:                            &hostkey_digest_type, &hostkey_digest,
                    271:                            &hostkey_digest_len, hostkey)) {
                    272:                                error("Error calculating key fingerprint.");
                    273:                                freerrset(fingerprints);
                    274:                                return -1;
                    275:                        }
                    276:                }
                    277:
1.1       jakob     278:                /* Check if the current key is the same as the given key */
                    279:                if (hostkey_algorithm == dnskey_algorithm &&
                    280:                    hostkey_digest_type == dnskey_digest_type) {
                    281:                        if (hostkey_digest_len == dnskey_digest_len &&
1.28      djm       282:                            timingsafe_bcmp(hostkey_digest, dnskey_digest,
                    283:                            hostkey_digest_len) == 0)
1.8       jakob     284:                                *flags |= DNS_VERIFY_MATCH;
1.1       jakob     285:                }
1.29      djm       286:                free(dnskey_digest);
1.1       jakob     287:        }
                    288:
1.29      djm       289:        free(hostkey_digest); /* from key_fingerprint_raw() */
1.4       jakob     290:        freerrset(fingerprints);
1.1       jakob     291:
1.8       jakob     292:        if (*flags & DNS_VERIFY_FOUND)
                    293:                if (*flags & DNS_VERIFY_MATCH)
                    294:                        debug("matching host key fingerprint found in DNS");
                    295:                else
                    296:                        debug("mismatching host key fingerprint found in DNS");
                    297:        else
                    298:                debug("no host key fingerprint found in DNS");
1.1       jakob     299:
1.8       jakob     300:        return 0;
1.1       jakob     301: }
                    302:
                    303: /*
                    304:  * Export the fingerprint of a key as a DNS resource record
                    305:  */
                    306: int
1.26      djm       307: export_dns_rr(const char *hostname, Key *key, FILE *f, int generic)
1.1       jakob     308: {
                    309:        u_int8_t rdata_pubkey_algorithm = 0;
1.28      djm       310:        u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED;
                    311:        u_int8_t dtype;
1.1       jakob     312:        u_char *rdata_digest;
1.28      djm       313:        u_int i, rdata_digest_len;
1.1       jakob     314:        int success = 0;
                    315:
1.28      djm       316:        for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) {
                    317:                rdata_digest_type = dtype;
                    318:                if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
                    319:                    &rdata_digest, &rdata_digest_len, key)) {
                    320:                        if (generic) {
                    321:                                fprintf(f, "%s IN TYPE%d \\# %d %02x %02x ",
                    322:                                    hostname, DNS_RDATATYPE_SSHFP,
                    323:                                    2 + rdata_digest_len,
                    324:                                    rdata_pubkey_algorithm, rdata_digest_type);
                    325:                        } else {
                    326:                                fprintf(f, "%s IN SSHFP %d %d ", hostname,
                    327:                                    rdata_pubkey_algorithm, rdata_digest_type);
                    328:                        }
                    329:                        for (i = 0; i < rdata_digest_len; i++)
                    330:                                fprintf(f, "%02x", rdata_digest[i]);
                    331:                        fprintf(f, "\n");
1.29      djm       332:                        free(rdata_digest); /* from key_fingerprint_raw() */
1.28      djm       333:                        success = 1;
                    334:                }
                    335:        }
1.1       jakob     336:
1.28      djm       337:        /* No SSHFP record was generated at all */
                    338:        if (success == 0) {
                    339:                error("%s: unsupported algorithm and/or digest_type", __func__);
1.1       jakob     340:        }
                    341:
                    342:        return success;
                    343: }