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