1 /* $OpenBSD: hostfile.c,v 1.53 2014/01/09 23:20:00 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Functions for manipulating the known hosts files.
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".
15 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
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.
41 #include <sys/types.h>
43 #include <netinet/in.h>
45 #include <openssl/hmac.h>
46 #include <openssl/sha.h>
63 struct hostkey_entry *entries;
68 extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
74 if (l < sizeof(HASH_MAGIC) - 1) {
75 debug2("extract_salt: string too short");
78 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
79 debug2("extract_salt: invalid magic identifier");
82 s += sizeof(HASH_MAGIC) - 1;
83 l -= sizeof(HASH_MAGIC) - 1;
84 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
85 debug2("extract_salt: missing salt termination character");
91 if (b64len == 0 || b64len > 1024) {
92 debug2("extract_salt: bad encoded salt length %u", b64len);
95 b64salt = xmalloc(1 + b64len);
96 memcpy(b64salt, s, b64len);
97 b64salt[b64len] = '\0';
99 ret = __b64_pton(b64salt, salt, salt_len);
102 debug2("extract_salt: salt decode error");
105 if (ret != SHA_DIGEST_LENGTH) {
106 debug2("extract_salt: expected salt len %d, got %d",
107 SHA_DIGEST_LENGTH, ret);
115 host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
117 const EVP_MD *md = EVP_sha1();
119 u_char salt[256], result[256];
120 char uu_salt[512], uu_result[512];
121 static char encoded[1024];
124 len = EVP_MD_size(md);
126 if (name_from_hostfile == NULL) {
127 /* Create new salt */
128 for (i = 0; i < len; i++)
129 salt[i] = arc4random();
131 /* Extract salt from known host entry */
132 if (extract_salt(name_from_hostfile, src_len, salt,
137 HMAC_Init(&mac_ctx, salt, len, md);
138 HMAC_Update(&mac_ctx, (u_char *)host, strlen(host));
139 HMAC_Final(&mac_ctx, result, NULL);
140 HMAC_cleanup(&mac_ctx);
142 if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
143 __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
144 fatal("host_hash: __b64_ntop failed");
146 snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
147 HASH_DELIM, uu_result);
153 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
154 * pointer over the key. Skips any whitespace at the beginning and at end.
158 hostfile_read_key(char **cpp, int *bitsp, Key *ret)
162 /* Skip leading whitespace. */
163 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
166 if (key_read(ret, &cp) != 1)
169 /* Skip trailing whitespace. */
170 for (; *cp == ' ' || *cp == '\t'; cp++)
173 /* Return results. */
176 if ((*bitsp = key_size(ret)) <= 0)
183 hostfile_check_key(int bits, const Key *key, const char *host,
184 const char *filename, u_long linenum)
186 if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
188 if (bits != BN_num_bits(key->rsa->n)) {
189 logit("Warning: %s, line %lu: keysize mismatch for host %s: "
190 "actual %d vs. announced %d.",
191 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
192 logit("Warning: replace %d with %d in %s, line %lu.",
193 bits, BN_num_bits(key->rsa->n), filename, linenum);
199 check_markers(char **cpp)
201 char marker[32], *sp, *cp = *cpp;
205 /* Only one marker is allowed */
208 /* Markers are terminated by whitespace */
209 if ((sp = strchr(cp, ' ')) == NULL &&
210 (sp = strchr(cp, '\t')) == NULL)
212 /* Extract marker for comparison */
213 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
215 memcpy(marker, cp, sp - cp);
216 marker[sp - cp] = '\0';
217 if (strcmp(marker, CA_MARKER) == 0)
219 else if (strcmp(marker, REVOKE_MARKER) == 0)
224 /* Skip past marker and any whitespace that follows it */
226 for (; *cp == ' ' || *cp == '\t'; cp++)
236 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
243 load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
247 u_long linenum = 0, num_loaded = 0;
248 char *cp, *cp2, *hashed_host;
249 HostkeyMarker marker;
253 if ((f = fopen(path, "r")) == NULL)
255 debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
256 __func__, host, path);
257 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
260 /* Skip any leading whitespace, comments and empty lines. */
261 for (; *cp == ' ' || *cp == '\t'; cp++)
263 if (!*cp || *cp == '#' || *cp == '\n')
266 if ((marker = check_markers(&cp)) == MRK_ERROR) {
267 verbose("%s: invalid marker at %s:%lu",
268 __func__, path, linenum);
272 /* Find the end of the host name portion. */
273 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
276 /* Check if the host name matches. */
277 if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
278 if (*cp != HASH_DELIM)
280 hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
281 if (hashed_host == NULL) {
282 debug("Invalid hashed host line %lu of %s",
286 if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
290 /* Got a match. Skip host name. */
294 * Extract the key from the line. This will skip any leading
295 * whitespace. Ignore badly formatted lines.
297 key = key_new(KEY_UNSPEC);
298 if (!hostfile_read_key(&cp, &kbits, key)) {
300 key = key_new(KEY_RSA1);
301 if (!hostfile_read_key(&cp, &kbits, key)) {
306 if (!hostfile_check_key(kbits, key, host, path, linenum))
309 debug3("%s: found %skey type %s in file %s:%lu", __func__,
310 marker == MRK_NONE ? "" :
311 (marker == MRK_CA ? "ca " : "revoked "),
312 key_type(key), path, linenum);
313 hostkeys->entries = xrealloc(hostkeys->entries,
314 hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
315 hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
316 hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
317 hostkeys->entries[hostkeys->num_entries].line = linenum;
318 hostkeys->entries[hostkeys->num_entries].key = key;
319 hostkeys->entries[hostkeys->num_entries].marker = marker;
320 hostkeys->num_entries++;
323 debug3("%s: loaded %lu keys", __func__, num_loaded);
329 free_hostkeys(struct hostkeys *hostkeys)
333 for (i = 0; i < hostkeys->num_entries; i++) {
334 free(hostkeys->entries[i].host);
335 free(hostkeys->entries[i].file);
336 key_free(hostkeys->entries[i].key);
337 bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
339 free(hostkeys->entries);
340 bzero(hostkeys, sizeof(*hostkeys));
345 check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
347 int is_cert = key_is_cert(k);
350 for (i = 0; i < hostkeys->num_entries; i++) {
351 if (hostkeys->entries[i].marker != MRK_REVOKE)
353 if (key_equal_public(k, hostkeys->entries[i].key))
356 key_equal_public(k->cert->signature_key,
357 hostkeys->entries[i].key))
364 * Match keys against a specified key, or look one up by key type.
366 * If looking for a keytype (key == NULL) and one is found then return
367 * HOST_FOUND, otherwise HOST_NEW.
369 * If looking for a key (key != NULL):
370 * 1. If the key is a cert and a matching CA is found, return HOST_OK
371 * 2. If the key is not a cert and a matching key is found, return HOST_OK
372 * 3. If no key matches but a key with a different type is found, then
373 * return HOST_CHANGED
374 * 4. If no matching keys are found, then return HOST_NEW.
376 * Finally, check any found key is not revoked.
379 check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
380 Key *k, int keytype, const struct hostkey_entry **found)
383 HostStatus end_return = HOST_NEW;
384 int want_cert = key_is_cert(k);
385 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
386 int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
391 for (i = 0; i < hostkeys->num_entries; i++) {
392 if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
394 if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
396 if (hostkeys->entries[i].marker != want_marker)
399 if (hostkeys->entries[i].key->type != keytype)
401 end_return = HOST_FOUND;
403 *found = hostkeys->entries + i;
404 k = hostkeys->entries[i].key;
408 if (key_equal_public(k->cert->signature_key,
409 hostkeys->entries[i].key)) {
410 /* A matching CA exists */
411 end_return = HOST_OK;
413 *found = hostkeys->entries + i;
417 if (key_equal(k, hostkeys->entries[i].key)) {
418 end_return = HOST_OK;
420 *found = hostkeys->entries + i;
423 /* A non-maching key exists */
424 end_return = HOST_CHANGED;
426 *found = hostkeys->entries + i;
429 if (check_key_not_revoked(hostkeys, k) != 0) {
430 end_return = HOST_REVOKED;
438 check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
439 const struct hostkey_entry **found)
442 fatal("no key to look up");
443 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
447 lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
448 const struct hostkey_entry **found)
450 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
451 found) == HOST_FOUND);
455 * Appends an entry to the host file. Returns false if the entry could not
460 add_host_to_hostfile(const char *filename, const char *host, const Key *key,
465 char *hashed_host = NULL;
468 return 1; /* XXX ? */
469 f = fopen(filename, "a");
474 if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
475 error("add_host_to_hostfile: host_hash failed");
480 fprintf(f, "%s ", store_hash ? hashed_host : host);
482 if (key_write(key, f)) {
485 error("add_host_to_hostfile: saving key in %s failed", filename);