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