5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Thu Jun 29 07:10:56 1995 ylo
12 * Functions for manipulating the known hosts files.
23 * Reads a multiple-precision integer in hex from the buffer, and advances
24 * the pointer. The integer must already be initialized. This function is
25 * permitted to modify the buffer. This leaves *cpp to point just beyond the
26 * last processed (and maybe modified) character. Note that this may modify
27 * the buffer containing the number.
31 auth_rsa_read_bignum(char **cpp, BIGNUM * value)
36 /* Skip any leading whitespace. */
37 for (; *cp == ' ' || *cp == '\t'; cp++)
40 /* Check that it begins with a hex digit. */
41 if (*cp < '0' || *cp > '9')
44 /* Save starting position. */
47 /* Move forward until all hex digits skipped. */
48 for (; *cp >= '0' && *cp <= '9'; cp++)
51 /* Compute the length of the hex number. */
54 /* Save the old terminating character, and replace it by \0. */
58 /* Parse the number. */
59 if (BN_dec2bn(&value, *cpp) == 0)
62 /* Restore old terminating character. */
65 /* Move beyond the number and return success. */
71 * Parses an RSA key (number of bits, e, n) from a string. Moves the pointer
72 * over the key. Skips any whitespace at the beginning and at end.
76 auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n)
81 /* Skip leading whitespace. */
82 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
85 /* Get number of bits. */
86 if (*cp < '0' || *cp > '9')
87 return 0; /* Bad bit count... */
88 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
89 bits = 10 * bits + *cp - '0';
91 /* Get public exponent. */
92 if (!auth_rsa_read_bignum(&cp, e))
95 /* Get public modulus. */
96 if (!auth_rsa_read_bignum(&cp, n))
99 /* Skip trailing whitespace. */
100 for (; *cp == ' ' || *cp == '\t'; cp++)
103 /* Return results. */
110 * Tries to match the host name (which must be in all lowercase) against the
111 * comma-separated sequence of subpatterns (each possibly preceded by ! to
112 * indicate negation). Returns true if there is a positive match; zero
117 match_hostname(const char *host, const char *pattern, unsigned int len)
122 unsigned int i, subi;
125 for (i = 0; i < len;) {
126 /* Check if the subpattern is negated. */
127 if (pattern[i] == '!') {
134 * Extract the subpattern up to a comma or end. Convert the
135 * subpattern to lowercase.
138 i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
140 sub[subi] = isupper(pattern[i]) ? tolower(pattern[i]) : pattern[i];
141 /* If subpattern too long, return failure (no match). */
142 if (subi >= sizeof(sub) - 1)
145 /* If the subpattern was terminated by a comma, skip the comma. */
146 if (i < len && pattern[i] == ',')
149 /* Null-terminate the subpattern. */
152 /* Try to match the subpattern against the host name. */
153 if (match_pattern(host, sub)) {
162 * Return success if got a positive match. If there was a negative
163 * match, we have already returned zero and never get here.
169 * Checks whether the given host (which must be in all lowercase) is already
170 * in the list of our known hosts. Returns HOST_OK if the host is known and
171 * has the specified key, HOST_NEW if the host is not known, and HOST_CHANGED
172 * if the host is known but used to have a different host key.
176 check_host_in_hostfile(const char *filename, const char *host,
177 BIGNUM * e, BIGNUM * n, BIGNUM * ke, BIGNUM * kn)
182 unsigned int bits, kbits, hostlen;
184 HostStatus end_return;
186 /* Open the file containing the list of known hosts. */
187 f = fopen(filename, "r");
191 /* Cache the length of the host name. */
192 hostlen = strlen(host);
195 * Return value when the loop terminates. This is set to
196 * HOST_CHANGED if we have seen a different key for the host and have
197 * not found the proper one.
199 end_return = HOST_NEW;
201 /* size of modulus 'n' */
202 bits = BN_num_bits(n);
204 /* Go trough the file. */
205 while (fgets(line, sizeof(line), f)) {
209 /* Skip any leading whitespace, comments and empty lines. */
210 for (; *cp == ' ' || *cp == '\t'; cp++)
212 if (!*cp || *cp == '#' || *cp == '\n')
215 /* Find the end of the host name portion. */
216 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
219 /* Check if the host name matches. */
220 if (!match_hostname(host, cp, (unsigned int) (cp2 - cp)))
223 /* Got a match. Skip host name. */
227 * Extract the key from the line. This will skip any leading
228 * whitespace. Ignore badly formatted lines.
230 if (!auth_rsa_read_key(&cp, &kbits, ke, kn))
233 if (kbits != BN_num_bits(kn)) {
234 error("Warning: error in %s, line %d: keysize mismatch for host %s: "
235 "actual size %d vs. announced %d.",
236 filename, linenum, host, BN_num_bits(kn), kbits);
237 error("Warning: replace %d with %d in %s, line %d.",
238 kbits, BN_num_bits(kn), filename, linenum);
240 /* Check if the current key is the same as the given key. */
241 if (BN_cmp(ke, e) == 0 && BN_cmp(kn, n) == 0) {
242 /* Ok, they match. */
247 * They do not match. We will continue to go through the
248 * file; however, we note that we will not return that it is
251 end_return = HOST_CHANGED;
253 /* Clear variables and close the file. */
257 * Return either HOST_NEW or HOST_CHANGED, depending on whether we
258 * saw a different key for the host.
264 * Appends an entry to the host file. Returns false if the entry could not
269 add_host_to_hostfile(const char *filename, const char *host,
270 BIGNUM * e, BIGNUM * n)
276 /* Open the file for appending. */
277 f = fopen(filename, "a");
281 /* size of modulus 'n' */
282 bits = BN_num_bits(n);
284 /* Print the host name and key to the file. */
285 fprintf(f, "%s %u ", host, bits);
288 error("add_host_to_hostfile: BN_bn2dec(e) failed");
292 fprintf(f, "%s ", buf);
296 error("add_host_to_hostfile: BN_bn2dec(n) failed");
300 fprintf(f, "%s\n", buf);
303 /* Close the file. */