2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 * C implementation based on the original Perl and shell versions
13 * Copyright (c) 2013-2014 Timo Teräs <timo.teras@iki.fi>
18 #if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__)
25 # include <sys/stat.h>
27 # include "internal/o_dir.h"
28 # include <openssl/evp.h>
29 # include <openssl/pem.h>
30 # include <openssl/x509.h>
36 # define MAX_COLLISIONS 256
38 typedef struct hentry_st {
39 struct hentry_st *next;
41 unsigned short old_id;
42 unsigned char need_symlink;
43 unsigned char digest[EVP_MAX_MD_SIZE];
46 typedef struct bucket_st {
47 struct bucket_st *next;
48 HENTRY *first_entry, *last_entry;
51 unsigned short num_needed;
55 /* Keep in sync with |suffixes|, below. */
56 TYPE_CERT=0, TYPE_CRL=1
60 HASH_OLD, HASH_NEW, HASH_BOTH
65 static const EVP_MD *evpmd;
66 static int remove_links = 1;
67 static int verbose = 0;
68 static BUCKET *hash_table[257];
70 static const char *suffixes[] = { "", "r" };
71 static const char *extensions[] = { "pem", "crt", "cer", "crl" };
74 static void bit_set(unsigned char *set, unsigned int bit)
76 set[bit >> 3] |= 1 << (bit & 0x7);
79 static int bit_isset(unsigned char *set, unsigned int bit)
81 return set[bit >> 3] & (1 << (bit & 0x7));
86 * Process an entry; return number of errors.
88 static int add_entry(enum Type type, unsigned int hash, const char *filename,
89 const unsigned char *digest, int need_symlink,
90 unsigned short old_id)
92 static BUCKET nilbucket;
93 static HENTRY nilhentry;
95 HENTRY *ep, *found = NULL;
96 unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table);
98 for (bp = hash_table[ndx]; bp; bp = bp->next)
99 if (bp->type == type && bp->hash == hash)
102 bp = app_malloc(sizeof(*bp), "hash bucket");
104 bp->next = hash_table[ndx];
107 hash_table[ndx] = bp;
110 for (ep = bp->first_entry; ep; ep = ep->next) {
111 if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) {
113 "%s: skipping duplicate certificate in %s\n",
114 opt_getprog(), filename);
117 if (strcmp(filename, ep->filename) == 0) {
125 if (bp->num_needed >= MAX_COLLISIONS) {
127 "%s: hash table overflow for %s\n",
128 opt_getprog(), filename);
131 ep = app_malloc(sizeof(*ep), "collision bucket");
134 ep->filename = OPENSSL_strdup(filename);
136 bp->last_entry->next = ep;
137 if (bp->first_entry == NULL)
138 bp->first_entry = ep;
142 if (old_id < ep->old_id)
144 if (need_symlink && !ep->need_symlink) {
145 ep->need_symlink = 1;
147 memcpy(ep->digest, digest, evpmdsize);
153 * Check if a symlink goes to the right spot; return 0 if okay.
154 * This can be -1 if bad filename, or an error count.
156 static int handle_symlink(const char *filename, const char *fullpath)
158 unsigned int hash = 0;
161 char linktarget[PATH_MAX], *endptr;
164 for (i = 0; i < 8; i++) {
169 hash += OPENSSL_hexchar2int(ch);
171 if (filename[i++] != '.')
173 for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--)
174 if (strcasecmp(suffixes[type], &filename[i]) == 0)
176 i += strlen(suffixes[type]);
178 id = strtoul(&filename[i], &endptr, 10);
182 n = readlink(fullpath, linktarget, sizeof(linktarget));
183 if (n < 0 || n >= (int)sizeof(linktarget))
187 return add_entry(type, hash, linktarget, NULL, 0, id);
191 * process a file, return number of errors.
193 static int do_file(const char *filename, const char *fullpath, enum Hash h)
195 STACK_OF (X509_INFO) *inf = NULL;
197 X509_NAME *name = NULL;
200 unsigned char digest[EVP_MAX_MD_SIZE];
204 /* Does it end with a recognized extension? */
205 if ((ext = strrchr(filename, '.')) == NULL)
207 for (i = 0; i < OSSL_NELEM(extensions); i++) {
208 if (strcasecmp(extensions[i], ext + 1) == 0)
211 if (i >= OSSL_NELEM(extensions))
214 /* Does it have X.509 data in it? */
215 if ((b = BIO_new_file(fullpath, "r")) == NULL) {
216 BIO_printf(bio_err, "%s: skipping %s, cannot open file\n",
217 opt_getprog(), filename);
221 inf = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL);
226 if (sk_X509_INFO_num(inf) != 1) {
229 "it does not contain exactly one certificate or CRL\n",
230 opt_getprog(), filename);
231 /* This is not an error. */
234 x = sk_X509_INFO_value(inf, 0);
237 name = X509_get_subject_name(x->x509);
238 X509_digest(x->x509, evpmd, digest, NULL);
241 name = X509_CRL_get_issuer(x->crl);
242 X509_CRL_digest(x->crl, evpmd, digest, NULL);
248 if ((h == HASH_NEW) || (h == HASH_BOTH))
249 errs += add_entry(type, X509_NAME_hash(name), filename, digest, 1, ~0);
250 if ((h == HASH_OLD) || (h == HASH_BOTH))
251 errs += add_entry(type, X509_NAME_hash_old(name), filename, digest, 1, ~0);
255 sk_X509_INFO_pop_free(inf, X509_INFO_free);
260 * Process a directory; return number of errors found.
262 static int do_dir(const char *dirname, enum Hash h)
266 OPENSSL_DIR_CTX *d = NULL;
268 unsigned char idmask[MAX_COLLISIONS / 8];
269 int n, nextid, buflen, errs = 0;
272 const char *filename;
275 if (app_access(dirname, W_OK) < 0) {
276 BIO_printf(bio_err, "Skipping %s, can't write\n", dirname);
279 buflen = strlen(dirname);
280 pathsep = (buflen && dirname[buflen - 1] == '/') ? "" : "/";
281 buflen += NAME_MAX + 1 + 1;
282 buf = app_malloc(buflen, "filename buffer");
285 BIO_printf(bio_out, "Doing %s\n", dirname);
287 while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) {
288 if (snprintf(buf, buflen, "%s%s%s",
289 dirname, pathsep, filename) >= buflen)
291 if (lstat(buf, &st) < 0)
293 if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0)
295 errs += do_file(filename, buf, h);
299 for (i = 0; i < OSSL_NELEM(hash_table); i++) {
300 for (bp = hash_table[i]; bp; bp = nextbp) {
303 memset(idmask, 0, (bp->num_needed + 7) / 8);
304 for (ep = bp->first_entry; ep; ep = ep->next)
305 if (ep->old_id < bp->num_needed)
306 bit_set(idmask, ep->old_id);
308 for (ep = bp->first_entry; ep; ep = nextep) {
310 if (ep->old_id < bp->num_needed) {
311 /* Link exists, and is used as-is */
312 snprintf(buf, buflen, "%08x.%s%d", bp->hash,
313 suffixes[bp->type], ep->old_id);
315 BIO_printf(bio_out, "link %s -> %s\n",
317 } else if (ep->need_symlink) {
318 /* New link needed (it may replace something) */
319 while (bit_isset(idmask, nextid))
322 snprintf(buf, buflen, "%s%s%n%08x.%s%d",
323 dirname, pathsep, &n, bp->hash,
324 suffixes[bp->type], nextid);
326 BIO_printf(bio_out, "link %s -> %s\n",
327 ep->filename, &buf[n]);
328 if (unlink(buf) < 0 && errno != ENOENT) {
330 "%s: Can't unlink %s, %s\n",
331 opt_getprog(), buf, strerror(errno));
334 if (symlink(ep->filename, buf) < 0) {
336 "%s: Can't symlink %s, %s\n",
337 opt_getprog(), ep->filename,
341 } else if (remove_links) {
342 /* Link to be deleted */
343 snprintf(buf, buflen, "%s%s%n%08x.%s%d",
344 dirname, pathsep, &n, bp->hash,
345 suffixes[bp->type], ep->old_id);
347 BIO_printf(bio_out, "unlink %s\n",
349 if (unlink(buf) < 0 && errno != ENOENT) {
351 "%s: Can't unlink %s, %s\n",
352 opt_getprog(), buf, strerror(errno));
356 OPENSSL_free(ep->filename);
361 hash_table[i] = NULL;
368 typedef enum OPTION_choice {
369 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
370 OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE
373 OPTIONS rehash_options[] = {
374 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert-directory...]\n"},
375 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
376 {"help", OPT_HELP, '-', "Display this summary"},
377 {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"},
378 {"old", OPT_OLD, '-', "Use old-style hash to generate links"},
379 {"n", OPT_N, '-', "Do not remove existing links"},
380 {"v", OPT_VERBOSE, '-', "Verbose output"},
385 int rehash_main(int argc, char **argv)
387 const char *env, *prog;
391 enum Hash h = HASH_NEW;
393 prog = opt_init(argc, argv, rehash_options);
394 while ((o = opt_next()) != OPT_EOF) {
398 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
401 opt_help(rehash_options);
417 argc = opt_num_rest();
421 evpmdsize = EVP_MD_size(evpmd);
425 errs += do_dir(*argv++, h);
426 } else if ((env = getenv("SSL_CERT_DIR")) != NULL) {
427 m = OPENSSL_strdup(env);
428 for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":"))
429 errs += do_dir(e, h);
432 errs += do_dir("/etc/ssl/certs", h);
440 OPTIONS rehash_options[] = {
444 int rehash_main(int argc, char **argv)
446 BIO_printf(bio_err, "Not available; use c_rehash script\n");
450 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */