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.
22 /* Reads a multiple-precision integer in hex from the buffer, and advances the
23 pointer. The integer must already be initialized. This function is
24 permitted to modify the buffer. This leaves *cpp to point just beyond
25 the last processed (and maybe modified) character. Note that this may
26 modify the buffer containing the number. */
29 auth_rsa_read_bignum(char **cpp, BIGNUM * value)
34 /* Skip any leading whitespace. */
35 for (; *cp == ' ' || *cp == '\t'; cp++);
37 /* Check that it begins with a hex digit. */
38 if (*cp < '0' || *cp > '9')
41 /* Save starting position. */
44 /* Move forward until all hex digits skipped. */
45 for (; *cp >= '0' && *cp <= '9'; cp++);
47 /* Compute the length of the hex number. */
50 /* Save the old terminating character, and replace it by \0. */
55 /* Parse the number. */
56 if (BN_dec2bn(&value, *cpp) == 0)
59 /* Restore old terminating character. */
62 /* Move beyond the number and return success. */
67 /* Parses an RSA key (number of bits, e, n) from a string. Moves the pointer
68 over the key. Skips any whitespace at the beginning and at end. */
71 auth_rsa_read_key(char **cpp, unsigned int *bitsp, BIGNUM * e, BIGNUM * n)
76 /* Skip leading whitespace. */
77 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++);
79 /* Get number of bits. */
80 if (*cp < '0' || *cp > '9')
81 return 0; /* Bad bit count... */
82 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
83 bits = 10 * bits + *cp - '0';
85 /* Get public exponent. */
86 if (!auth_rsa_read_bignum(&cp, e))
89 /* Get public modulus. */
90 if (!auth_rsa_read_bignum(&cp, n))
93 /* Skip trailing whitespace. */
94 for (; *cp == ' ' || *cp == '\t'; cp++);
102 /* Tries to match the host name (which must be in all lowercase) against the
103 comma-separated sequence of subpatterns (each possibly preceded by ! to
104 indicate negation). Returns true if there is a positive match; zero
108 match_hostname(const char *host, const char *pattern, unsigned int len)
113 unsigned int i, subi;
116 for (i = 0; i < len;) {
117 /* Check if the subpattern is negated. */
118 if (pattern[i] == '!') {
124 /* Extract the subpattern up to a comma or end. Convert
125 the subpattern to lowercase. */
127 i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
129 sub[subi] = isupper(pattern[i]) ? tolower(pattern[i]) : pattern[i];
130 /* If subpattern too long, return failure (no match). */
131 if (subi >= sizeof(sub) - 1)
134 /* If the subpattern was terminated by a comma, skip the
136 if (i < len && pattern[i] == ',')
139 /* Null-terminate the subpattern. */
142 /* Try to match the subpattern against the host name. */
143 if (match_pattern(host, sub)) {
145 return 0; /* Fail if host matches
146 any negated subpattern. */
152 /* Return success if got a positive match. If there was a
153 negative match, we have already returned zero and never get
158 /* Checks whether the given host (which must be in all lowercase) is
159 already in the list of our known hosts.
160 Returns HOST_OK if the host is known and has the specified key,
161 HOST_NEW if the host is not known, and HOST_CHANGED if the host is known
162 but used to have a different host key. */
165 check_host_in_hostfile(const char *filename, const char *host,
166 BIGNUM * e, BIGNUM * n, BIGNUM * ke, BIGNUM * kn)
171 unsigned int bits, kbits, hostlen;
173 HostStatus end_return;
175 /* Open the file containing the list of known hosts. */
176 f = fopen(filename, "r");
180 /* Cache the length of the host name. */
181 hostlen = strlen(host);
183 /* Return value when the loop terminates. This is set to
184 HOST_CHANGED if we have seen a different key for the host and
185 have not found the proper one. */
186 end_return = HOST_NEW;
188 /* size of modulus 'n' */
189 bits = BN_num_bits(n);
191 /* Go trough the file. */
192 while (fgets(line, sizeof(line), f)) {
196 /* Skip any leading whitespace. */
197 for (; *cp == ' ' || *cp == '\t'; cp++);
199 /* Ignore comment lines and empty lines. */
200 if (!*cp || *cp == '#' || *cp == '\n')
203 /* Find the end of the host name portion. */
204 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++);
206 /* Check if the host name matches. */
207 if (!match_hostname(host, cp, (unsigned int) (cp2 - cp)))
210 /* Got a match. Skip host name. */
213 /* Extract the key from the line. This will skip any
214 leading whitespace. Ignore badly formatted lines. */
215 if (!auth_rsa_read_key(&cp, &kbits, ke, kn))
218 if (kbits != BN_num_bits(kn)) {
219 error("Warning: error in %s, line %d: keysize mismatch for host %s: "
220 "actual size %d vs. announced %d.",
221 filename, linenum, host, BN_num_bits(kn), kbits);
222 error("Warning: replace %d with %d in %s, line %d.",
223 kbits, BN_num_bits(kn), filename, linenum);
225 /* Check if the current key is the same as the given key. */
226 if (BN_cmp(ke, e) == 0 && BN_cmp(kn, n) == 0) {
227 /* Ok, they match. */
231 /* They do not match. We will continue to go through the
232 file; however, we note that we will not return that it
234 end_return = HOST_CHANGED;
236 /* Clear variables and close the file. */
239 /* Return either HOST_NEW or HOST_CHANGED, depending on whether we
240 saw a different key for the host. */
244 /* Appends an entry to the host file. Returns false if the entry
245 could not be appended. */
248 add_host_to_hostfile(const char *filename, const char *host,
249 BIGNUM * e, BIGNUM * n)
255 /* Open the file for appending. */
256 f = fopen(filename, "a");
260 /* size of modulus 'n' */
261 bits = BN_num_bits(n);
263 /* Print the host name and key to the file. */
264 fprintf(f, "%s %u ", host, bits);
267 error("add_host_to_hostfile: BN_bn2dec(e) failed");
271 fprintf(f, "%s ", buf);
275 error("add_host_to_hostfile: BN_bn2dec(n) failed");
279 fprintf(f, "%s\n", buf);
282 /* Close the file. */