]>
Commit | Line | Data |
---|---|---|
8f6f1441 | 1 | /* |
846e33c7 | 2 | * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
8f6f1441 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8f6f1441 | 8 | */ |
846e33c7 RS |
9 | |
10 | /* | |
11 | * C implementation based on the original Perl and shell versions | |
8f6f1441 | 12 | * |
846e33c7 | 13 | * Copyright (c) 2013-2014 Timo Teräs <timo.teras@iki.fi> |
8f6f1441 TT |
14 | */ |
15 | ||
16 | #include "apps.h" | |
17 | ||
6616429d | 18 | #if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) || \ |
5c80e2af | 19 | (defined(__VMS) && defined(__DECC) && __CRTL_VER >= 80300000) |
8f6f1441 TT |
20 | # include <unistd.h> |
21 | # include <stdio.h> | |
22 | # include <limits.h> | |
23 | # include <errno.h> | |
24 | # include <string.h> | |
25 | # include <ctype.h> | |
26 | # include <sys/stat.h> | |
27 | ||
28 | # include "internal/o_dir.h" | |
29 | # include <openssl/evp.h> | |
30 | # include <openssl/pem.h> | |
31 | # include <openssl/x509.h> | |
32 | ||
33 | ||
6616429d RL |
34 | # ifndef PATH_MAX |
35 | # define PATH_MAX 4096 | |
36 | # endif | |
c9c84a13 AG |
37 | # ifndef NAME_MAX |
38 | # define NAME_MAX 255 | |
39 | # endif | |
8f6f1441 TT |
40 | # define MAX_COLLISIONS 256 |
41 | ||
42 | typedef struct hentry_st { | |
43 | struct hentry_st *next; | |
44 | char *filename; | |
45 | unsigned short old_id; | |
46 | unsigned char need_symlink; | |
47 | unsigned char digest[EVP_MAX_MD_SIZE]; | |
48 | } HENTRY; | |
49 | ||
50 | typedef struct bucket_st { | |
51 | struct bucket_st *next; | |
52 | HENTRY *first_entry, *last_entry; | |
53 | unsigned int hash; | |
54 | unsigned short type; | |
55 | unsigned short num_needed; | |
56 | } BUCKET; | |
57 | ||
58 | enum Type { | |
59 | /* Keep in sync with |suffixes|, below. */ | |
60 | TYPE_CERT=0, TYPE_CRL=1 | |
61 | }; | |
62 | ||
63 | enum Hash { | |
64 | HASH_OLD, HASH_NEW, HASH_BOTH | |
65 | }; | |
66 | ||
67 | ||
68 | static int evpmdsize; | |
69 | static const EVP_MD *evpmd; | |
70 | static int remove_links = 1; | |
71 | static int verbose = 0; | |
72 | static BUCKET *hash_table[257]; | |
73 | ||
74 | static const char *suffixes[] = { "", "r" }; | |
75 | static const char *extensions[] = { "pem", "crt", "cer", "crl" }; | |
76 | ||
77 | ||
78 | static void bit_set(unsigned char *set, unsigned int bit) | |
79 | { | |
80 | set[bit >> 3] |= 1 << (bit & 0x7); | |
81 | } | |
82 | ||
83 | static int bit_isset(unsigned char *set, unsigned int bit) | |
84 | { | |
85 | return set[bit >> 3] & (1 << (bit & 0x7)); | |
86 | } | |
87 | ||
88 | ||
ce249fac RS |
89 | /* |
90 | * Process an entry; return number of errors. | |
91 | */ | |
92 | static int add_entry(enum Type type, unsigned int hash, const char *filename, | |
8f6f1441 TT |
93 | const unsigned char *digest, int need_symlink, |
94 | unsigned short old_id) | |
95 | { | |
96 | static BUCKET nilbucket; | |
97 | static HENTRY nilhentry; | |
98 | BUCKET *bp; | |
99 | HENTRY *ep, *found = NULL; | |
100 | unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table); | |
101 | ||
102 | for (bp = hash_table[ndx]; bp; bp = bp->next) | |
103 | if (bp->type == type && bp->hash == hash) | |
104 | break; | |
105 | if (bp == NULL) { | |
106 | bp = app_malloc(sizeof(*bp), "hash bucket"); | |
107 | *bp = nilbucket; | |
108 | bp->next = hash_table[ndx]; | |
109 | bp->type = type; | |
110 | bp->hash = hash; | |
111 | hash_table[ndx] = bp; | |
112 | } | |
113 | ||
114 | for (ep = bp->first_entry; ep; ep = ep->next) { | |
115 | if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) { | |
116 | BIO_printf(bio_err, | |
f22ff0eb DSH |
117 | "%s: skipping duplicate %s in %s\n", opt_getprog(), |
118 | type == TYPE_CERT ? "certificate" : "CRL", filename); | |
ce249fac | 119 | return 1; |
8f6f1441 TT |
120 | } |
121 | if (strcmp(filename, ep->filename) == 0) { | |
122 | found = ep; | |
123 | if (digest == NULL) | |
124 | break; | |
125 | } | |
126 | } | |
127 | ep = found; | |
128 | if (ep == NULL) { | |
ce249fac RS |
129 | if (bp->num_needed >= MAX_COLLISIONS) { |
130 | BIO_printf(bio_err, | |
131 | "%s: hash table overflow for %s\n", | |
132 | opt_getprog(), filename); | |
133 | return 1; | |
134 | } | |
8f6f1441 TT |
135 | ep = app_malloc(sizeof(*ep), "collision bucket"); |
136 | *ep = nilhentry; | |
137 | ep->old_id = ~0; | |
7644a9ae | 138 | ep->filename = OPENSSL_strdup(filename); |
8f6f1441 TT |
139 | if (bp->last_entry) |
140 | bp->last_entry->next = ep; | |
141 | if (bp->first_entry == NULL) | |
142 | bp->first_entry = ep; | |
143 | bp->last_entry = ep; | |
144 | } | |
145 | ||
146 | if (old_id < ep->old_id) | |
147 | ep->old_id = old_id; | |
148 | if (need_symlink && !ep->need_symlink) { | |
149 | ep->need_symlink = 1; | |
150 | bp->num_needed++; | |
151 | memcpy(ep->digest, digest, evpmdsize); | |
152 | } | |
ce249fac | 153 | return 0; |
8f6f1441 TT |
154 | } |
155 | ||
ce249fac RS |
156 | /* |
157 | * Check if a symlink goes to the right spot; return 0 if okay. | |
158 | * This can be -1 if bad filename, or an error count. | |
159 | */ | |
8f6f1441 TT |
160 | static int handle_symlink(const char *filename, const char *fullpath) |
161 | { | |
162 | unsigned int hash = 0; | |
163 | int i, type, id; | |
164 | unsigned char ch; | |
e7a68985 | 165 | char linktarget[PATH_MAX], *endptr; |
6616429d | 166 | ossl_ssize_t n; |
8f6f1441 TT |
167 | |
168 | for (i = 0; i < 8; i++) { | |
169 | ch = filename[i]; | |
170 | if (!isxdigit(ch)) | |
171 | return -1; | |
172 | hash <<= 4; | |
14f051a0 | 173 | hash += OPENSSL_hexchar2int(ch); |
8f6f1441 TT |
174 | } |
175 | if (filename[i++] != '.') | |
176 | return -1; | |
ee8f7858 DSH |
177 | for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--) { |
178 | const char *suffix = suffixes[type]; | |
179 | if (strncasecmp(suffix, &filename[i], strlen(suffix)) == 0) | |
8f6f1441 | 180 | break; |
ee8f7858 | 181 | } |
8f6f1441 TT |
182 | i += strlen(suffixes[type]); |
183 | ||
184 | id = strtoul(&filename[i], &endptr, 10); | |
185 | if (*endptr != '\0') | |
186 | return -1; | |
187 | ||
188 | n = readlink(fullpath, linktarget, sizeof(linktarget)); | |
189 | if (n < 0 || n >= (int)sizeof(linktarget)) | |
190 | return -1; | |
191 | linktarget[n] = 0; | |
192 | ||
ce249fac | 193 | return add_entry(type, hash, linktarget, NULL, 0, id); |
8f6f1441 TT |
194 | } |
195 | ||
ce249fac RS |
196 | /* |
197 | * process a file, return number of errors. | |
198 | */ | |
8f6f1441 TT |
199 | static int do_file(const char *filename, const char *fullpath, enum Hash h) |
200 | { | |
ce249fac | 201 | STACK_OF (X509_INFO) *inf = NULL; |
8f6f1441 TT |
202 | X509_INFO *x; |
203 | X509_NAME *name = NULL; | |
204 | BIO *b; | |
205 | const char *ext; | |
206 | unsigned char digest[EVP_MAX_MD_SIZE]; | |
8c82de99 BL |
207 | int type, errs = 0; |
208 | size_t i; | |
8f6f1441 | 209 | |
ce249fac | 210 | /* Does it end with a recognized extension? */ |
8f6f1441 | 211 | if ((ext = strrchr(filename, '.')) == NULL) |
ce249fac | 212 | goto end; |
8c82de99 | 213 | for (i = 0; i < OSSL_NELEM(extensions); i++) { |
8f6f1441 TT |
214 | if (strcasecmp(extensions[i], ext + 1) == 0) |
215 | break; | |
216 | } | |
8c82de99 | 217 | if (i >= OSSL_NELEM(extensions)) |
ce249fac | 218 | goto end; |
8f6f1441 | 219 | |
ce249fac RS |
220 | /* Does it have X.509 data in it? */ |
221 | if ((b = BIO_new_file(fullpath, "r")) == NULL) { | |
222 | BIO_printf(bio_err, "%s: skipping %s, cannot open file\n", | |
223 | opt_getprog(), filename); | |
224 | errs++; | |
225 | goto end; | |
226 | } | |
8f6f1441 TT |
227 | inf = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL); |
228 | BIO_free(b); | |
229 | if (inf == NULL) | |
ce249fac | 230 | goto end; |
8f6f1441 TT |
231 | |
232 | if (sk_X509_INFO_num(inf) != 1) { | |
233 | BIO_printf(bio_err, | |
234 | "%s: skipping %s," | |
235 | "it does not contain exactly one certificate or CRL\n", | |
236 | opt_getprog(), filename); | |
ce249fac | 237 | /* This is not an error. */ |
8f6f1441 TT |
238 | goto end; |
239 | } | |
240 | x = sk_X509_INFO_value(inf, 0); | |
241 | if (x->x509) { | |
242 | type = TYPE_CERT; | |
243 | name = X509_get_subject_name(x->x509); | |
244 | X509_digest(x->x509, evpmd, digest, NULL); | |
245 | } else if (x->crl) { | |
246 | type = TYPE_CRL; | |
247 | name = X509_CRL_get_issuer(x->crl); | |
248 | X509_CRL_digest(x->crl, evpmd, digest, NULL); | |
8c82de99 BL |
249 | } else { |
250 | ++errs; | |
251 | goto end; | |
8f6f1441 TT |
252 | } |
253 | if (name) { | |
254 | if ((h == HASH_NEW) || (h == HASH_BOTH)) | |
ce249fac | 255 | errs += add_entry(type, X509_NAME_hash(name), filename, digest, 1, ~0); |
8f6f1441 | 256 | if ((h == HASH_OLD) || (h == HASH_BOTH)) |
ce249fac | 257 | errs += add_entry(type, X509_NAME_hash_old(name), filename, digest, 1, ~0); |
8f6f1441 TT |
258 | } |
259 | ||
260 | end: | |
261 | sk_X509_INFO_pop_free(inf, X509_INFO_free); | |
ce249fac | 262 | return errs; |
8f6f1441 TT |
263 | } |
264 | ||
b1ffe8db RS |
265 | static void str_free(char *s) |
266 | { | |
267 | OPENSSL_free(s); | |
268 | } | |
269 | ||
5c80e2af RL |
270 | static int ends_with_dirsep(const char *path) |
271 | { | |
272 | if (*path != '\0') | |
273 | path += strlen(path) - 1; | |
274 | # ifdef __VMS | |
275 | if (*path == ']' || *path == '>' || *path == ':') | |
276 | return 1; | |
277 | # elif _WIN32 | |
278 | if (*path == '\\') | |
279 | return 1; | |
280 | # endif | |
281 | return *path == '/'; | |
282 | } | |
283 | ||
39aceac3 RL |
284 | static int massage_filename(char *name) |
285 | { | |
286 | # ifdef __VMS | |
287 | char *p = strchr(name, ';'); | |
288 | char *q = p; | |
289 | ||
290 | if (q != NULL) { | |
291 | for (q++; *q != '\0'; q++) { | |
292 | if (!isdigit(*q)) | |
293 | return 1; | |
294 | } | |
295 | } | |
296 | ||
297 | *p = '\0'; | |
298 | # endif | |
299 | return 1; | |
300 | } | |
301 | ||
ce249fac RS |
302 | /* |
303 | * Process a directory; return number of errors found. | |
304 | */ | |
8f6f1441 TT |
305 | static int do_dir(const char *dirname, enum Hash h) |
306 | { | |
307 | BUCKET *bp, *nextbp; | |
308 | HENTRY *ep, *nextep; | |
309 | OPENSSL_DIR_CTX *d = NULL; | |
310 | struct stat st; | |
311 | unsigned char idmask[MAX_COLLISIONS / 8]; | |
b1ffe8db | 312 | int n, numfiles, nextid, buflen, errs = 0; |
8c82de99 | 313 | size_t i; |
8f6f1441 TT |
314 | const char *pathsep; |
315 | const char *filename; | |
b1ffe8db RS |
316 | char *buf, *copy; |
317 | STACK_OF(OPENSSL_STRING) *files = NULL; | |
8f6f1441 | 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 | 323 | buflen = strlen(dirname); |
5c80e2af | 324 | pathsep = (buflen && !ends_with_dirsep(dirname)) ? "/": ""; |
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 | ||
b1ffe8db RS |
331 | if ((files = sk_OPENSSL_STRING_new_null()) == NULL) { |
332 | BIO_printf(bio_err, "Skipping %s, out of memory\n", dirname); | |
333 | exit(1); | |
334 | } | |
8f6f1441 | 335 | while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) { |
b1ffe8db | 336 | if ((copy = strdup(filename)) == NULL |
39aceac3 | 337 | || !massage_filename(copy) |
b1ffe8db RS |
338 | || sk_OPENSSL_STRING_push(files, copy) == 0) { |
339 | BIO_puts(bio_err, "out of memory\n"); | |
340 | exit(1); | |
341 | } | |
342 | } | |
343 | OPENSSL_DIR_end(&d); | |
344 | sk_OPENSSL_STRING_sort(files); | |
345 | ||
346 | numfiles = sk_OPENSSL_STRING_num(files); | |
347 | for (n = 0; n < numfiles; ++n) { | |
348 | filename = sk_OPENSSL_STRING_value(files, n); | |
8f6f1441 TT |
349 | if (snprintf(buf, buflen, "%s%s%s", |
350 | dirname, pathsep, filename) >= buflen) | |
351 | continue; | |
352 | if (lstat(buf, &st) < 0) | |
353 | continue; | |
354 | if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0) | |
355 | continue; | |
ce249fac | 356 | errs += do_file(filename, buf, h); |
8f6f1441 | 357 | } |
b1ffe8db | 358 | sk_OPENSSL_STRING_pop_free(files, str_free); |
8f6f1441 | 359 | |
8c82de99 | 360 | for (i = 0; i < OSSL_NELEM(hash_table); i++) { |
8f6f1441 TT |
361 | for (bp = hash_table[i]; bp; bp = nextbp) { |
362 | nextbp = bp->next; | |
363 | nextid = 0; | |
364 | memset(idmask, 0, (bp->num_needed + 7) / 8); | |
365 | for (ep = bp->first_entry; ep; ep = ep->next) | |
366 | if (ep->old_id < bp->num_needed) | |
367 | bit_set(idmask, ep->old_id); | |
368 | ||
369 | for (ep = bp->first_entry; ep; ep = nextep) { | |
370 | nextep = ep->next; | |
371 | if (ep->old_id < bp->num_needed) { | |
372 | /* Link exists, and is used as-is */ | |
373 | snprintf(buf, buflen, "%08x.%s%d", bp->hash, | |
374 | suffixes[bp->type], ep->old_id); | |
375 | if (verbose) | |
376 | BIO_printf(bio_out, "link %s -> %s\n", | |
377 | ep->filename, buf); | |
378 | } else if (ep->need_symlink) { | |
379 | /* New link needed (it may replace something) */ | |
380 | while (bit_isset(idmask, nextid)) | |
381 | nextid++; | |
382 | ||
383 | snprintf(buf, buflen, "%s%s%n%08x.%s%d", | |
384 | dirname, pathsep, &n, bp->hash, | |
385 | suffixes[bp->type], nextid); | |
386 | if (verbose) | |
387 | BIO_printf(bio_out, "link %s -> %s\n", | |
388 | ep->filename, &buf[n]); | |
ce249fac | 389 | if (unlink(buf) < 0 && errno != ENOENT) { |
8f6f1441 TT |
390 | BIO_printf(bio_err, |
391 | "%s: Can't unlink %s, %s\n", | |
392 | opt_getprog(), buf, strerror(errno)); | |
ce249fac RS |
393 | errs++; |
394 | } | |
395 | if (symlink(ep->filename, buf) < 0) { | |
8f6f1441 TT |
396 | BIO_printf(bio_err, |
397 | "%s: Can't symlink %s, %s\n", | |
398 | opt_getprog(), ep->filename, | |
399 | strerror(errno)); | |
ce249fac RS |
400 | errs++; |
401 | } | |
3770b877 | 402 | bit_set(idmask, nextid); |
8f6f1441 TT |
403 | } else if (remove_links) { |
404 | /* Link to be deleted */ | |
405 | snprintf(buf, buflen, "%s%s%n%08x.%s%d", | |
406 | dirname, pathsep, &n, bp->hash, | |
407 | suffixes[bp->type], ep->old_id); | |
408 | if (verbose) | |
409 | BIO_printf(bio_out, "unlink %s\n", | |
410 | &buf[n]); | |
ce249fac | 411 | if (unlink(buf) < 0 && errno != ENOENT) { |
8f6f1441 TT |
412 | BIO_printf(bio_err, |
413 | "%s: Can't unlink %s, %s\n", | |
414 | opt_getprog(), buf, strerror(errno)); | |
ce249fac RS |
415 | errs++; |
416 | } | |
8f6f1441 TT |
417 | } |
418 | OPENSSL_free(ep->filename); | |
419 | OPENSSL_free(ep); | |
420 | } | |
421 | OPENSSL_free(bp); | |
422 | } | |
423 | hash_table[i] = NULL; | |
424 | } | |
8f6f1441 TT |
425 | |
426 | OPENSSL_free(buf); | |
ce249fac | 427 | return errs; |
8f6f1441 TT |
428 | } |
429 | ||
430 | typedef enum OPTION_choice { | |
431 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
432 | OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE | |
433 | } OPTION_CHOICE; | |
434 | ||
44c83ebd | 435 | const OPTIONS rehash_options[] = { |
8f6f1441 TT |
436 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert-directory...]\n"}, |
437 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
438 | {"help", OPT_HELP, '-', "Display this summary"}, | |
7d959c35 | 439 | {"h", OPT_HELP, '-', "Display this summary"}, |
8f6f1441 TT |
440 | {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"}, |
441 | {"old", OPT_OLD, '-', "Use old-style hash to generate links"}, | |
442 | {"n", OPT_N, '-', "Do not remove existing links"}, | |
443 | {"v", OPT_VERBOSE, '-', "Verbose output"}, | |
444 | {NULL} | |
445 | }; | |
446 | ||
447 | ||
448 | int rehash_main(int argc, char **argv) | |
449 | { | |
450 | const char *env, *prog; | |
451 | char *e, *m; | |
ce249fac | 452 | int errs = 0; |
8f6f1441 TT |
453 | OPTION_CHOICE o; |
454 | enum Hash h = HASH_NEW; | |
455 | ||
456 | prog = opt_init(argc, argv, rehash_options); | |
457 | while ((o = opt_next()) != OPT_EOF) { | |
458 | switch (o) { | |
459 | case OPT_EOF: | |
460 | case OPT_ERR: | |
461 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
462 | goto end; | |
463 | case OPT_HELP: | |
464 | opt_help(rehash_options); | |
465 | goto end; | |
466 | case OPT_COMPAT: | |
467 | h = HASH_BOTH; | |
468 | break; | |
469 | case OPT_OLD: | |
470 | h = HASH_OLD; | |
471 | break; | |
472 | case OPT_N: | |
473 | remove_links = 0; | |
474 | break; | |
475 | case OPT_VERBOSE: | |
476 | verbose = 1; | |
477 | break; | |
478 | } | |
479 | } | |
480 | argc = opt_num_rest(); | |
481 | argv = opt_rest(); | |
482 | ||
483 | evpmd = EVP_sha1(); | |
484 | evpmdsize = EVP_MD_size(evpmd); | |
485 | ||
486 | if (*argv) { | |
487 | while (*argv) | |
ce249fac | 488 | errs += do_dir(*argv++, h); |
8f6f1441 | 489 | } else if ((env = getenv("SSL_CERT_DIR")) != NULL) { |
7644a9ae | 490 | m = OPENSSL_strdup(env); |
8f6f1441 | 491 | for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":")) |
ce249fac | 492 | errs += do_dir(e, h); |
8f6f1441 TT |
493 | OPENSSL_free(m); |
494 | } else { | |
ce249fac | 495 | errs += do_dir("/etc/ssl/certs", h); |
8f6f1441 TT |
496 | } |
497 | ||
498 | end: | |
ce249fac | 499 | return errs; |
8f6f1441 TT |
500 | } |
501 | ||
502 | #else | |
44c83ebd | 503 | const OPTIONS rehash_options[] = { |
62fdf4ee RS |
504 | {NULL} |
505 | }; | |
8f6f1441 TT |
506 | |
507 | int rehash_main(int argc, char **argv) | |
508 | { | |
9e0da060 | 509 | BIO_printf(bio_err, "Not available; use c_rehash script\n"); |
8f6f1441 TT |
510 | return (1); |
511 | } | |
512 | ||
568b8020 | 513 | #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */ |