Annotation of src/usr.bin/ssh/hostfile.c, Revision 1.56
1.56 ! markus 1: /* $OpenBSD: hostfile.c,v 1.55 2014/01/31 16:39:19 tedu 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:
43: #include <resolv.h>
1.44 stevesk 44: #include <stdio.h>
1.43 stevesk 45: #include <stdlib.h>
1.42 stevesk 46: #include <string.h>
1.1 deraadt 47:
1.45 deraadt 48: #include "xmalloc.h"
1.14 markus 49: #include "match.h"
50: #include "key.h"
51: #include "hostfile.h"
1.24 markus 52: #include "log.h"
1.49 djm 53: #include "misc.h"
1.53 djm 54: #include "digest.h"
1.54 markus 55: #include "hmac.h"
1.49 djm 56:
57: struct hostkeys {
58: struct hostkey_entry *entries;
59: u_int num_entries;
60: };
1.33 djm 61:
62: static int
1.52 djm 63: extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
1.33 djm 64: {
65: char *p, *b64salt;
66: u_int b64len;
67: int ret;
68:
69: if (l < sizeof(HASH_MAGIC) - 1) {
70: debug2("extract_salt: string too short");
71: return (-1);
72: }
73: if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
74: debug2("extract_salt: invalid magic identifier");
75: return (-1);
76: }
77: s += sizeof(HASH_MAGIC) - 1;
78: l -= sizeof(HASH_MAGIC) - 1;
79: if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
80: debug2("extract_salt: missing salt termination character");
81: return (-1);
82: }
83:
84: b64len = p - s;
85: /* Sanity check */
86: if (b64len == 0 || b64len > 1024) {
87: debug2("extract_salt: bad encoded salt length %u", b64len);
88: return (-1);
89: }
90: b64salt = xmalloc(1 + b64len);
91: memcpy(b64salt, s, b64len);
92: b64salt[b64len] = '\0';
93:
94: ret = __b64_pton(b64salt, salt, salt_len);
1.51 djm 95: free(b64salt);
1.33 djm 96: if (ret == -1) {
97: debug2("extract_salt: salt decode error");
98: return (-1);
99: }
1.54 markus 100: if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
101: debug2("extract_salt: expected salt len %zd, got %d",
102: ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
1.33 djm 103: return (-1);
104: }
1.34 deraadt 105:
1.33 djm 106: return (0);
107: }
108:
109: char *
110: host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
111: {
1.54 markus 112: struct ssh_hmac_ctx *ctx;
1.52 djm 113: u_char salt[256], result[256];
114: char uu_salt[512], uu_result[512];
1.33 djm 115: static char encoded[1024];
116: u_int i, len;
117:
1.54 markus 118: len = ssh_digest_bytes(SSH_DIGEST_SHA1);
1.33 djm 119:
120: if (name_from_hostfile == NULL) {
121: /* Create new salt */
122: for (i = 0; i < len; i++)
123: salt[i] = arc4random();
124: } else {
125: /* Extract salt from known host entry */
126: if (extract_salt(name_from_hostfile, src_len, salt,
127: sizeof(salt)) == -1)
128: return (NULL);
129: }
130:
1.54 markus 131: if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL ||
132: ssh_hmac_init(ctx, salt, len) < 0 ||
133: ssh_hmac_update(ctx, host, strlen(host)) < 0 ||
134: ssh_hmac_final(ctx, result, sizeof(result)))
135: fatal("%s: ssh_hmac failed", __func__);
136: ssh_hmac_free(ctx);
1.33 djm 137:
1.34 deraadt 138: if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
1.33 djm 139: __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
1.54 markus 140: fatal("%s: __b64_ntop failed", __func__);
1.33 djm 141:
142: snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
143: HASH_DELIM, uu_result);
144:
145: return (encoded);
146: }
1.1 deraadt 147:
1.9 markus 148: /*
1.14 markus 149: * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
150: * pointer over the key. Skips any whitespace at the beginning and at end.
1.9 markus 151: */
1.1 deraadt 152:
1.29 jakob 153: int
1.52 djm 154: hostfile_read_key(char **cpp, int *bitsp, Key *ret)
1.1 deraadt 155: {
1.7 markus 156: char *cp;
157:
158: /* Skip leading whitespace. */
1.9 markus 159: for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
160: ;
1.1 deraadt 161:
1.21 markus 162: if (key_read(ret, &cp) != 1)
1.7 markus 163: return 0;
164:
165: /* Skip trailing whitespace. */
1.9 markus 166: for (; *cp == ' ' || *cp == '\t'; cp++)
167: ;
1.7 markus 168:
169: /* Return results. */
170: *cpp = cp;
1.52 djm 171: if (bitsp != NULL) {
172: if ((*bitsp = key_size(ret)) <= 0)
173: return 0;
174: }
1.7 markus 175: return 1;
1.14 markus 176: }
1.1 deraadt 177:
1.27 itojun 178: static int
1.49 djm 179: hostfile_check_key(int bits, const Key *key, const char *host,
180: const char *filename, u_long linenum)
1.1 deraadt 181: {
1.56 ! markus 182: #ifdef WITH_SSH1
1.21 markus 183: if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
1.14 markus 184: return 1;
185: if (bits != BN_num_bits(key->rsa->n)) {
1.49 djm 186: logit("Warning: %s, line %lu: keysize mismatch for host %s: "
1.14 markus 187: "actual %d vs. announced %d.",
188: filename, linenum, host, BN_num_bits(key->rsa->n), bits);
1.49 djm 189: logit("Warning: replace %d with %d in %s, line %lu.",
1.14 markus 190: bits, BN_num_bits(key->rsa->n), filename, linenum);
1.1 deraadt 191: }
1.56 ! markus 192: #endif
1.14 markus 193: return 1;
1.1 deraadt 194: }
195:
1.49 djm 196: static HostkeyMarker
1.48 djm 197: check_markers(char **cpp)
198: {
199: char marker[32], *sp, *cp = *cpp;
200: int ret = MRK_NONE;
201:
202: while (*cp == '@') {
203: /* Only one marker is allowed */
204: if (ret != MRK_NONE)
205: return MRK_ERROR;
206: /* Markers are terminated by whitespace */
207: if ((sp = strchr(cp, ' ')) == NULL &&
208: (sp = strchr(cp, '\t')) == NULL)
209: return MRK_ERROR;
210: /* Extract marker for comparison */
211: if (sp <= cp + 1 || sp >= cp + sizeof(marker))
212: return MRK_ERROR;
213: memcpy(marker, cp, sp - cp);
214: marker[sp - cp] = '\0';
215: if (strcmp(marker, CA_MARKER) == 0)
216: ret = MRK_CA;
217: else if (strcmp(marker, REVOKE_MARKER) == 0)
218: ret = MRK_REVOKE;
219: else
220: return MRK_ERROR;
221:
222: /* Skip past marker and any whitespace that follows it */
223: cp = sp;
224: for (; *cp == ' ' || *cp == '\t'; cp++)
225: ;
226: }
227: *cpp = cp;
228: return ret;
229: }
230:
1.49 djm 231: struct hostkeys *
232: init_hostkeys(void)
233: {
234: struct hostkeys *ret = xcalloc(1, sizeof(*ret));
235:
236: ret->entries = NULL;
237: return ret;
238: }
1.1 deraadt 239:
1.49 djm 240: void
241: load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
1.1 deraadt 242: {
1.7 markus 243: FILE *f;
244: char line[8192];
1.49 djm 245: u_long linenum = 0, num_loaded = 0;
1.33 djm 246: char *cp, *cp2, *hashed_host;
1.49 djm 247: HostkeyMarker marker;
248: Key *key;
249: int kbits;
250:
251: if ((f = fopen(path, "r")) == NULL)
252: return;
253: debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
254: __func__, host, path);
255: while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
1.7 markus 256: cp = line;
257:
1.9 markus 258: /* Skip any leading whitespace, comments and empty lines. */
259: for (; *cp == ' ' || *cp == '\t'; cp++)
260: ;
1.7 markus 261: if (!*cp || *cp == '#' || *cp == '\n')
262: continue;
263:
1.49 djm 264: if ((marker = check_markers(&cp)) == MRK_ERROR) {
265: verbose("%s: invalid marker at %s:%lu",
266: __func__, path, linenum);
1.47 djm 267: continue;
1.49 djm 268: }
1.47 djm 269:
1.7 markus 270: /* Find the end of the host name portion. */
1.9 markus 271: for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
272: ;
1.7 markus 273:
274: /* Check if the host name matches. */
1.33 djm 275: if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
276: if (*cp != HASH_DELIM)
277: continue;
278: hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
279: if (hashed_host == NULL) {
1.49 djm 280: debug("Invalid hashed host line %lu of %s",
281: linenum, path);
1.33 djm 282: continue;
283: }
284: if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
285: continue;
286: }
1.7 markus 287:
288: /* Got a match. Skip host name. */
289: cp = cp2;
290:
1.9 markus 291: /*
292: * Extract the key from the line. This will skip any leading
293: * whitespace. Ignore badly formatted lines.
294: */
1.49 djm 295: key = key_new(KEY_UNSPEC);
296: if (!hostfile_read_key(&cp, &kbits, key)) {
297: key_free(key);
1.56 ! markus 298: #ifdef WITH_SSH1
1.49 djm 299: key = key_new(KEY_RSA1);
300: if (!hostfile_read_key(&cp, &kbits, key)) {
301: key_free(key);
302: continue;
303: }
1.56 ! markus 304: #else
! 305: continue;
! 306: #endif
1.49 djm 307: }
308: if (!hostfile_check_key(kbits, key, host, path, linenum))
1.14 markus 309: continue;
1.23 markus 310:
1.49 djm 311: debug3("%s: found %skey type %s in file %s:%lu", __func__,
312: marker == MRK_NONE ? "" :
313: (marker == MRK_CA ? "ca " : "revoked "),
314: key_type(key), path, linenum);
315: hostkeys->entries = xrealloc(hostkeys->entries,
316: hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
317: hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
318: hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
319: hostkeys->entries[hostkeys->num_entries].line = linenum;
320: hostkeys->entries[hostkeys->num_entries].key = key;
321: hostkeys->entries[hostkeys->num_entries].marker = marker;
322: hostkeys->num_entries++;
323: num_loaded++;
324: }
325: debug3("%s: loaded %lu keys", __func__, num_loaded);
1.50 djm 326: fclose(f);
1.49 djm 327: return;
328: }
329:
330: void
331: free_hostkeys(struct hostkeys *hostkeys)
332: {
333: u_int i;
334:
335: for (i = 0; i < hostkeys->num_entries; i++) {
1.51 djm 336: free(hostkeys->entries[i].host);
337: free(hostkeys->entries[i].file);
1.49 djm 338: key_free(hostkeys->entries[i].key);
1.55 tedu 339: explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
1.49 djm 340: }
1.51 djm 341: free(hostkeys->entries);
1.55 tedu 342: explicit_bzero(hostkeys, sizeof(*hostkeys));
1.51 djm 343: free(hostkeys);
1.49 djm 344: }
345:
346: static int
347: check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
348: {
349: int is_cert = key_is_cert(k);
350: u_int i;
1.7 markus 351:
1.49 djm 352: for (i = 0; i < hostkeys->num_entries; i++) {
353: if (hostkeys->entries[i].marker != MRK_REVOKE)
1.30 markus 354: continue;
1.49 djm 355: if (key_equal_public(k, hostkeys->entries[i].key))
356: return -1;
357: if (is_cert &&
358: key_equal_public(k->cert->signature_key,
359: hostkeys->entries[i].key))
360: return -1;
361: }
362: return 0;
363: }
364:
365: /*
366: * Match keys against a specified key, or look one up by key type.
367: *
368: * If looking for a keytype (key == NULL) and one is found then return
369: * HOST_FOUND, otherwise HOST_NEW.
370: *
371: * If looking for a key (key != NULL):
372: * 1. If the key is a cert and a matching CA is found, return HOST_OK
373: * 2. If the key is not a cert and a matching key is found, return HOST_OK
374: * 3. If no key matches but a key with a different type is found, then
375: * return HOST_CHANGED
376: * 4. If no matching keys are found, then return HOST_NEW.
377: *
378: * Finally, check any found key is not revoked.
379: */
380: static HostStatus
381: check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
382: Key *k, int keytype, const struct hostkey_entry **found)
383: {
384: u_int i;
385: HostStatus end_return = HOST_NEW;
386: int want_cert = key_is_cert(k);
387: HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
388: int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
389:
390: if (found != NULL)
391: *found = NULL;
1.30 markus 392:
1.49 djm 393: for (i = 0; i < hostkeys->num_entries; i++) {
394: if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
395: continue;
396: if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
1.30 markus 397: continue;
1.49 djm 398: if (hostkeys->entries[i].marker != want_marker)
399: continue;
400: if (k == NULL) {
401: if (hostkeys->entries[i].key->type != keytype)
402: continue;
403: end_return = HOST_FOUND;
404: if (found != NULL)
405: *found = hostkeys->entries + i;
406: k = hostkeys->entries[i].key;
407: break;
408: }
409: if (want_cert) {
410: if (key_equal_public(k->cert->signature_key,
411: hostkeys->entries[i].key)) {
412: /* A matching CA exists */
413: end_return = HOST_OK;
414: if (found != NULL)
415: *found = hostkeys->entries + i;
416: break;
1.48 djm 417: }
1.49 djm 418: } else {
419: if (key_equal(k, hostkeys->entries[i].key)) {
420: end_return = HOST_OK;
421: if (found != NULL)
422: *found = hostkeys->entries + i;
423: break;
1.48 djm 424: }
1.49 djm 425: /* A non-maching key exists */
426: end_return = HOST_CHANGED;
427: if (found != NULL)
428: *found = hostkeys->entries + i;
1.48 djm 429: }
1.1 deraadt 430: }
1.49 djm 431: if (check_key_not_revoked(hostkeys, k) != 0) {
432: end_return = HOST_REVOKED;
433: if (found != NULL)
434: *found = NULL;
435: }
1.7 markus 436: return end_return;
1.30 markus 437: }
1.49 djm 438:
1.30 markus 439: HostStatus
1.49 djm 440: check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
441: const struct hostkey_entry **found)
1.30 markus 442: {
443: if (key == NULL)
444: fatal("no key to look up");
1.49 djm 445: return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
1.30 markus 446: }
447:
448: int
1.49 djm 449: lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
450: const struct hostkey_entry **found)
1.30 markus 451: {
1.49 djm 452: return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
453: found) == HOST_FOUND);
1.1 deraadt 454: }
455:
1.9 markus 456: /*
457: * Appends an entry to the host file. Returns false if the entry could not
458: * be appended.
459: */
1.1 deraadt 460:
1.2 provos 461: int
1.34 deraadt 462: add_host_to_hostfile(const char *filename, const char *host, const Key *key,
1.33 djm 463: int store_hash)
1.1 deraadt 464: {
1.7 markus 465: FILE *f;
1.14 markus 466: int success = 0;
1.35 dtucker 467: char *hashed_host = NULL;
1.33 djm 468:
1.14 markus 469: if (key == NULL)
1.17 markus 470: return 1; /* XXX ? */
1.7 markus 471: f = fopen(filename, "a");
472: if (!f)
473: return 0;
1.33 djm 474:
475: if (store_hash) {
476: if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
477: error("add_host_to_hostfile: host_hash failed");
478: fclose(f);
479: return 0;
480: }
481: }
482: fprintf(f, "%s ", store_hash ? hashed_host : host);
483:
1.14 markus 484: if (key_write(key, f)) {
485: success = 1;
486: } else {
1.17 markus 487: error("add_host_to_hostfile: saving key in %s failed", filename);
1.7 markus 488: }
1.17 markus 489: fprintf(f, "\n");
1.7 markus 490: fclose(f);
1.14 markus 491: return success;
1.1 deraadt 492: }