]>
Commit | Line | Data |
---|---|---|
8f6f1441 TT |
1 | /* |
2 | * C implementation based on the original Perl and shell versions | |
3 | * | |
4 | * Copyright (c) 2013-2014 Timo Teräs <timo.teras@iki.fi> | |
8f6f1441 TT |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 2015 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * | |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include "apps.h" | |
61 | ||
568b8020 | 62 | #if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) |
8f6f1441 TT |
63 | # include <unistd.h> |
64 | # include <stdio.h> | |
65 | # include <limits.h> | |
66 | # include <errno.h> | |
67 | # include <string.h> | |
68 | # include <ctype.h> | |
69 | # include <sys/stat.h> | |
70 | ||
71 | # include "internal/o_dir.h" | |
72 | # include <openssl/evp.h> | |
73 | # include <openssl/pem.h> | |
74 | # include <openssl/x509.h> | |
75 | ||
76 | ||
c9c84a13 AG |
77 | # ifndef NAME_MAX |
78 | # define NAME_MAX 255 | |
79 | # endif | |
8f6f1441 TT |
80 | # define MAX_COLLISIONS 256 |
81 | ||
82 | typedef struct hentry_st { | |
83 | struct hentry_st *next; | |
84 | char *filename; | |
85 | unsigned short old_id; | |
86 | unsigned char need_symlink; | |
87 | unsigned char digest[EVP_MAX_MD_SIZE]; | |
88 | } HENTRY; | |
89 | ||
90 | typedef struct bucket_st { | |
91 | struct bucket_st *next; | |
92 | HENTRY *first_entry, *last_entry; | |
93 | unsigned int hash; | |
94 | unsigned short type; | |
95 | unsigned short num_needed; | |
96 | } BUCKET; | |
97 | ||
98 | enum Type { | |
99 | /* Keep in sync with |suffixes|, below. */ | |
100 | TYPE_CERT=0, TYPE_CRL=1 | |
101 | }; | |
102 | ||
103 | enum Hash { | |
104 | HASH_OLD, HASH_NEW, HASH_BOTH | |
105 | }; | |
106 | ||
107 | ||
108 | static int evpmdsize; | |
109 | static const EVP_MD *evpmd; | |
110 | static int remove_links = 1; | |
111 | static int verbose = 0; | |
112 | static BUCKET *hash_table[257]; | |
113 | ||
114 | static const char *suffixes[] = { "", "r" }; | |
115 | static const char *extensions[] = { "pem", "crt", "cer", "crl" }; | |
116 | ||
117 | ||
118 | static void bit_set(unsigned char *set, unsigned int bit) | |
119 | { | |
120 | set[bit >> 3] |= 1 << (bit & 0x7); | |
121 | } | |
122 | ||
123 | static int bit_isset(unsigned char *set, unsigned int bit) | |
124 | { | |
125 | return set[bit >> 3] & (1 << (bit & 0x7)); | |
126 | } | |
127 | ||
128 | ||
ce249fac RS |
129 | /* |
130 | * Process an entry; return number of errors. | |
131 | */ | |
132 | static int add_entry(enum Type type, unsigned int hash, const char *filename, | |
8f6f1441 TT |
133 | const unsigned char *digest, int need_symlink, |
134 | unsigned short old_id) | |
135 | { | |
136 | static BUCKET nilbucket; | |
137 | static HENTRY nilhentry; | |
138 | BUCKET *bp; | |
139 | HENTRY *ep, *found = NULL; | |
140 | unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table); | |
141 | ||
142 | for (bp = hash_table[ndx]; bp; bp = bp->next) | |
143 | if (bp->type == type && bp->hash == hash) | |
144 | break; | |
145 | if (bp == NULL) { | |
146 | bp = app_malloc(sizeof(*bp), "hash bucket"); | |
147 | *bp = nilbucket; | |
148 | bp->next = hash_table[ndx]; | |
149 | bp->type = type; | |
150 | bp->hash = hash; | |
151 | hash_table[ndx] = bp; | |
152 | } | |
153 | ||
154 | for (ep = bp->first_entry; ep; ep = ep->next) { | |
155 | if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) { | |
156 | BIO_printf(bio_err, | |
157 | "%s: skipping duplicate certificate in %s\n", | |
158 | opt_getprog(), filename); | |
ce249fac | 159 | return 1; |
8f6f1441 TT |
160 | } |
161 | if (strcmp(filename, ep->filename) == 0) { | |
162 | found = ep; | |
163 | if (digest == NULL) | |
164 | break; | |
165 | } | |
166 | } | |
167 | ep = found; | |
168 | if (ep == NULL) { | |
ce249fac RS |
169 | if (bp->num_needed >= MAX_COLLISIONS) { |
170 | BIO_printf(bio_err, | |
171 | "%s: hash table overflow for %s\n", | |
172 | opt_getprog(), filename); | |
173 | return 1; | |
174 | } | |
8f6f1441 TT |
175 | ep = app_malloc(sizeof(*ep), "collision bucket"); |
176 | *ep = nilhentry; | |
177 | ep->old_id = ~0; | |
7644a9ae | 178 | ep->filename = OPENSSL_strdup(filename); |
8f6f1441 TT |
179 | if (bp->last_entry) |
180 | bp->last_entry->next = ep; | |
181 | if (bp->first_entry == NULL) | |
182 | bp->first_entry = ep; | |
183 | bp->last_entry = ep; | |
184 | } | |
185 | ||
186 | if (old_id < ep->old_id) | |
187 | ep->old_id = old_id; | |
188 | if (need_symlink && !ep->need_symlink) { | |
189 | ep->need_symlink = 1; | |
190 | bp->num_needed++; | |
191 | memcpy(ep->digest, digest, evpmdsize); | |
192 | } | |
ce249fac | 193 | return 0; |
8f6f1441 TT |
194 | } |
195 | ||
ce249fac RS |
196 | /* |
197 | * Check if a symlink goes to the right spot; return 0 if okay. | |
198 | * This can be -1 if bad filename, or an error count. | |
199 | */ | |
8f6f1441 TT |
200 | static int handle_symlink(const char *filename, const char *fullpath) |
201 | { | |
202 | unsigned int hash = 0; | |
203 | int i, type, id; | |
204 | unsigned char ch; | |
e7a68985 | 205 | char linktarget[PATH_MAX], *endptr; |
8f6f1441 TT |
206 | ssize_t n; |
207 | ||
208 | for (i = 0; i < 8; i++) { | |
209 | ch = filename[i]; | |
210 | if (!isxdigit(ch)) | |
211 | return -1; | |
212 | hash <<= 4; | |
213 | hash += app_hex(ch); | |
214 | } | |
215 | if (filename[i++] != '.') | |
216 | return -1; | |
217 | for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--) | |
218 | if (strcasecmp(suffixes[type], &filename[i]) == 0) | |
219 | break; | |
220 | i += strlen(suffixes[type]); | |
221 | ||
222 | id = strtoul(&filename[i], &endptr, 10); | |
223 | if (*endptr != '\0') | |
224 | return -1; | |
225 | ||
226 | n = readlink(fullpath, linktarget, sizeof(linktarget)); | |
227 | if (n < 0 || n >= (int)sizeof(linktarget)) | |
228 | return -1; | |
229 | linktarget[n] = 0; | |
230 | ||
ce249fac | 231 | return add_entry(type, hash, linktarget, NULL, 0, id); |
8f6f1441 TT |
232 | } |
233 | ||
ce249fac RS |
234 | /* |
235 | * process a file, return number of errors. | |
236 | */ | |
8f6f1441 TT |
237 | static int do_file(const char *filename, const char *fullpath, enum Hash h) |
238 | { | |
ce249fac | 239 | STACK_OF (X509_INFO) *inf = NULL; |
8f6f1441 TT |
240 | X509_INFO *x; |
241 | X509_NAME *name = NULL; | |
242 | BIO *b; | |
243 | const char *ext; | |
244 | unsigned char digest[EVP_MAX_MD_SIZE]; | |
8c82de99 BL |
245 | int type, errs = 0; |
246 | size_t i; | |
8f6f1441 | 247 | |
ce249fac | 248 | /* Does it end with a recognized extension? */ |
8f6f1441 | 249 | if ((ext = strrchr(filename, '.')) == NULL) |
ce249fac | 250 | goto end; |
8c82de99 | 251 | for (i = 0; i < OSSL_NELEM(extensions); i++) { |
8f6f1441 TT |
252 | if (strcasecmp(extensions[i], ext + 1) == 0) |
253 | break; | |
254 | } | |
8c82de99 | 255 | if (i >= OSSL_NELEM(extensions)) |
ce249fac | 256 | goto end; |
8f6f1441 | 257 | |
ce249fac RS |
258 | /* Does it have X.509 data in it? */ |
259 | if ((b = BIO_new_file(fullpath, "r")) == NULL) { | |
260 | BIO_printf(bio_err, "%s: skipping %s, cannot open file\n", | |
261 | opt_getprog(), filename); | |
262 | errs++; | |
263 | goto end; | |
264 | } | |
8f6f1441 TT |
265 | inf = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL); |
266 | BIO_free(b); | |
267 | if (inf == NULL) | |
ce249fac | 268 | goto end; |
8f6f1441 TT |
269 | |
270 | if (sk_X509_INFO_num(inf) != 1) { | |
271 | BIO_printf(bio_err, | |
272 | "%s: skipping %s," | |
273 | "it does not contain exactly one certificate or CRL\n", | |
274 | opt_getprog(), filename); | |
ce249fac | 275 | /* This is not an error. */ |
8f6f1441 TT |
276 | goto end; |
277 | } | |
278 | x = sk_X509_INFO_value(inf, 0); | |
279 | if (x->x509) { | |
280 | type = TYPE_CERT; | |
281 | name = X509_get_subject_name(x->x509); | |
282 | X509_digest(x->x509, evpmd, digest, NULL); | |
283 | } else if (x->crl) { | |
284 | type = TYPE_CRL; | |
285 | name = X509_CRL_get_issuer(x->crl); | |
286 | X509_CRL_digest(x->crl, evpmd, digest, NULL); | |
8c82de99 BL |
287 | } else { |
288 | ++errs; | |
289 | goto end; | |
8f6f1441 TT |
290 | } |
291 | if (name) { | |
292 | if ((h == HASH_NEW) || (h == HASH_BOTH)) | |
ce249fac | 293 | errs += add_entry(type, X509_NAME_hash(name), filename, digest, 1, ~0); |
8f6f1441 | 294 | if ((h == HASH_OLD) || (h == HASH_BOTH)) |
ce249fac | 295 | errs += add_entry(type, X509_NAME_hash_old(name), filename, digest, 1, ~0); |
8f6f1441 TT |
296 | } |
297 | ||
298 | end: | |
299 | sk_X509_INFO_pop_free(inf, X509_INFO_free); | |
ce249fac | 300 | return errs; |
8f6f1441 TT |
301 | } |
302 | ||
ce249fac RS |
303 | /* |
304 | * Process a directory; return number of errors found. | |
305 | */ | |
8f6f1441 TT |
306 | static int do_dir(const char *dirname, enum Hash h) |
307 | { | |
308 | BUCKET *bp, *nextbp; | |
309 | HENTRY *ep, *nextep; | |
310 | OPENSSL_DIR_CTX *d = NULL; | |
311 | struct stat st; | |
312 | unsigned char idmask[MAX_COLLISIONS / 8]; | |
8c82de99 BL |
313 | int n, nextid, buflen, errs = 0; |
314 | size_t i; | |
8f6f1441 TT |
315 | const char *pathsep; |
316 | const char *filename; | |
317 | char *buf; | |
318 | ||
ff2f6bb0 RS |
319 | if (app_access(dirname, W_OK) < 0) { |
320 | BIO_printf(bio_err, "Skipping %s, can't write\n", dirname); | |
4c7103a5 | 321 | return 1; |
ff2f6bb0 | 322 | } |
8f6f1441 TT |
323 | buflen = strlen(dirname); |
324 | pathsep = (buflen && dirname[buflen - 1] == '/') ? "" : "/"; | |
c9c84a13 AG |
325 | buflen += NAME_MAX + 1 + 1; |
326 | buf = app_malloc(buflen, "filename buffer"); | |
8f6f1441 TT |
327 | |
328 | if (verbose) | |
329 | BIO_printf(bio_out, "Doing %s\n", dirname); | |
330 | ||
331 | while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) { | |
332 | if (snprintf(buf, buflen, "%s%s%s", | |
333 | dirname, pathsep, filename) >= buflen) | |
334 | continue; | |
335 | if (lstat(buf, &st) < 0) | |
336 | continue; | |
337 | if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0) | |
338 | continue; | |
ce249fac | 339 | errs += do_file(filename, buf, h); |
8f6f1441 TT |
340 | } |
341 | OPENSSL_DIR_end(&d); | |
342 | ||
8c82de99 | 343 | for (i = 0; i < OSSL_NELEM(hash_table); i++) { |
8f6f1441 TT |
344 | for (bp = hash_table[i]; bp; bp = nextbp) { |
345 | nextbp = bp->next; | |
346 | nextid = 0; | |
347 | memset(idmask, 0, (bp->num_needed + 7) / 8); | |
348 | for (ep = bp->first_entry; ep; ep = ep->next) | |
349 | if (ep->old_id < bp->num_needed) | |
350 | bit_set(idmask, ep->old_id); | |
351 | ||
352 | for (ep = bp->first_entry; ep; ep = nextep) { | |
353 | nextep = ep->next; | |
354 | if (ep->old_id < bp->num_needed) { | |
355 | /* Link exists, and is used as-is */ | |
356 | snprintf(buf, buflen, "%08x.%s%d", bp->hash, | |
357 | suffixes[bp->type], ep->old_id); | |
358 | if (verbose) | |
359 | BIO_printf(bio_out, "link %s -> %s\n", | |
360 | ep->filename, buf); | |
361 | } else if (ep->need_symlink) { | |
362 | /* New link needed (it may replace something) */ | |
363 | while (bit_isset(idmask, nextid)) | |
364 | nextid++; | |
365 | ||
366 | snprintf(buf, buflen, "%s%s%n%08x.%s%d", | |
367 | dirname, pathsep, &n, bp->hash, | |
368 | suffixes[bp->type], nextid); | |
369 | if (verbose) | |
370 | BIO_printf(bio_out, "link %s -> %s\n", | |
371 | ep->filename, &buf[n]); | |
ce249fac | 372 | if (unlink(buf) < 0 && errno != ENOENT) { |
8f6f1441 TT |
373 | BIO_printf(bio_err, |
374 | "%s: Can't unlink %s, %s\n", | |
375 | opt_getprog(), buf, strerror(errno)); | |
ce249fac RS |
376 | errs++; |
377 | } | |
378 | if (symlink(ep->filename, buf) < 0) { | |
8f6f1441 TT |
379 | BIO_printf(bio_err, |
380 | "%s: Can't symlink %s, %s\n", | |
381 | opt_getprog(), ep->filename, | |
382 | strerror(errno)); | |
ce249fac RS |
383 | errs++; |
384 | } | |
8f6f1441 TT |
385 | } else if (remove_links) { |
386 | /* Link to be deleted */ | |
387 | snprintf(buf, buflen, "%s%s%n%08x.%s%d", | |
388 | dirname, pathsep, &n, bp->hash, | |
389 | suffixes[bp->type], ep->old_id); | |
390 | if (verbose) | |
391 | BIO_printf(bio_out, "unlink %s\n", | |
392 | &buf[n]); | |
ce249fac | 393 | if (unlink(buf) < 0 && errno != ENOENT) { |
8f6f1441 TT |
394 | BIO_printf(bio_err, |
395 | "%s: Can't unlink %s, %s\n", | |
396 | opt_getprog(), buf, strerror(errno)); | |
ce249fac RS |
397 | errs++; |
398 | } | |
8f6f1441 TT |
399 | } |
400 | OPENSSL_free(ep->filename); | |
401 | OPENSSL_free(ep); | |
402 | } | |
403 | OPENSSL_free(bp); | |
404 | } | |
405 | hash_table[i] = NULL; | |
406 | } | |
8f6f1441 TT |
407 | |
408 | OPENSSL_free(buf); | |
ce249fac | 409 | return errs; |
8f6f1441 TT |
410 | } |
411 | ||
412 | typedef enum OPTION_choice { | |
413 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
414 | OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE | |
415 | } OPTION_CHOICE; | |
416 | ||
417 | OPTIONS rehash_options[] = { | |
418 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert-directory...]\n"}, | |
419 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
420 | {"help", OPT_HELP, '-', "Display this summary"}, | |
421 | {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"}, | |
422 | {"old", OPT_OLD, '-', "Use old-style hash to generate links"}, | |
423 | {"n", OPT_N, '-', "Do not remove existing links"}, | |
424 | {"v", OPT_VERBOSE, '-', "Verbose output"}, | |
425 | {NULL} | |
426 | }; | |
427 | ||
428 | ||
429 | int rehash_main(int argc, char **argv) | |
430 | { | |
431 | const char *env, *prog; | |
432 | char *e, *m; | |
ce249fac | 433 | int errs = 0; |
8f6f1441 TT |
434 | OPTION_CHOICE o; |
435 | enum Hash h = HASH_NEW; | |
436 | ||
437 | prog = opt_init(argc, argv, rehash_options); | |
438 | while ((o = opt_next()) != OPT_EOF) { | |
439 | switch (o) { | |
440 | case OPT_EOF: | |
441 | case OPT_ERR: | |
442 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
443 | goto end; | |
444 | case OPT_HELP: | |
445 | opt_help(rehash_options); | |
446 | goto end; | |
447 | case OPT_COMPAT: | |
448 | h = HASH_BOTH; | |
449 | break; | |
450 | case OPT_OLD: | |
451 | h = HASH_OLD; | |
452 | break; | |
453 | case OPT_N: | |
454 | remove_links = 0; | |
455 | break; | |
456 | case OPT_VERBOSE: | |
457 | verbose = 1; | |
458 | break; | |
459 | } | |
460 | } | |
461 | argc = opt_num_rest(); | |
462 | argv = opt_rest(); | |
463 | ||
464 | evpmd = EVP_sha1(); | |
465 | evpmdsize = EVP_MD_size(evpmd); | |
466 | ||
467 | if (*argv) { | |
468 | while (*argv) | |
ce249fac | 469 | errs += do_dir(*argv++, h); |
8f6f1441 | 470 | } else if ((env = getenv("SSL_CERT_DIR")) != NULL) { |
7644a9ae | 471 | m = OPENSSL_strdup(env); |
8f6f1441 | 472 | for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":")) |
ce249fac | 473 | errs += do_dir(e, h); |
8f6f1441 TT |
474 | OPENSSL_free(m); |
475 | } else { | |
ce249fac | 476 | errs += do_dir("/etc/ssl/certs", h); |
8f6f1441 TT |
477 | } |
478 | ||
479 | end: | |
ce249fac | 480 | return errs; |
8f6f1441 TT |
481 | } |
482 | ||
483 | #else | |
62fdf4ee RS |
484 | OPTIONS rehash_options[] = { |
485 | {NULL} | |
486 | }; | |
8f6f1441 TT |
487 | |
488 | int rehash_main(int argc, char **argv) | |
489 | { | |
9e0da060 | 490 | BIO_printf(bio_err, "Not available; use c_rehash script\n"); |
8f6f1441 TT |
491 | return (1); |
492 | } | |
493 | ||
568b8020 | 494 | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */ |