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