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