2 * C implementation based on the original Perl and shell versions
4 * Copyright (c) 2013-2014 Timo Teräs <timo.teras@iki.fi>
7 /* ====================================================================
8 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * licensing@OpenSSL.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
70 # include <sys/stat.h>
72 # include "internal/o_dir.h"
73 # include <openssl/evp.h>
74 # include <openssl/pem.h>
75 # include <openssl/x509.h>
78 # define MAX_COLLISIONS 256
80 typedef struct hentry_st {
81 struct hentry_st *next;
83 unsigned short old_id;
84 unsigned char need_symlink;
85 unsigned char digest[EVP_MAX_MD_SIZE];
88 typedef struct bucket_st {
89 struct bucket_st *next;
90 HENTRY *first_entry, *last_entry;
93 unsigned short num_needed;
97 /* Keep in sync with |suffixes|, below. */
98 TYPE_CERT=0, TYPE_CRL=1
102 HASH_OLD, HASH_NEW, HASH_BOTH
106 static int evpmdsize;
107 static const EVP_MD *evpmd;
108 static int remove_links = 1;
109 static int verbose = 0;
110 static BUCKET *hash_table[257];
112 static const char *suffixes[] = { "", "r" };
113 static const char *extensions[] = { "pem", "crt", "cer", "crl" };
116 static void bit_set(unsigned char *set, unsigned int bit)
118 set[bit >> 3] |= 1 << (bit & 0x7);
121 static int bit_isset(unsigned char *set, unsigned int bit)
123 return set[bit >> 3] & (1 << (bit & 0x7));
127 static void add_entry(enum Type type, unsigned int hash, const char *filename,
128 const unsigned char *digest, int need_symlink,
129 unsigned short old_id)
131 static BUCKET nilbucket;
132 static HENTRY nilhentry;
134 HENTRY *ep, *found = NULL;
135 unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table);
137 for (bp = hash_table[ndx]; bp; bp = bp->next)
138 if (bp->type == type && bp->hash == hash)
141 bp = app_malloc(sizeof(*bp), "hash bucket");
143 bp->next = hash_table[ndx];
146 hash_table[ndx] = bp;
149 for (ep = bp->first_entry; ep; ep = ep->next) {
150 if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) {
152 "%s: skipping duplicate certificate in %s\n",
153 opt_getprog(), filename);
156 if (strcmp(filename, ep->filename) == 0) {
164 if (bp->num_needed >= MAX_COLLISIONS)
166 ep = app_malloc(sizeof(*ep), "collision bucket");
169 ep->filename = BUF_strdup(filename);
171 bp->last_entry->next = ep;
172 if (bp->first_entry == NULL)
173 bp->first_entry = ep;
177 if (old_id < ep->old_id)
179 if (need_symlink && !ep->need_symlink) {
180 ep->need_symlink = 1;
182 memcpy(ep->digest, digest, evpmdsize);
186 static int handle_symlink(const char *filename, const char *fullpath)
188 unsigned int hash = 0;
191 char linktarget[NAME_MAX], *endptr;
194 for (i = 0; i < 8; i++) {
201 if (filename[i++] != '.')
203 for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--)
204 if (strcasecmp(suffixes[type], &filename[i]) == 0)
206 i += strlen(suffixes[type]);
208 id = strtoul(&filename[i], &endptr, 10);
212 n = readlink(fullpath, linktarget, sizeof(linktarget));
213 if (n < 0 || n >= (int)sizeof(linktarget))
217 add_entry(type, hash, linktarget, NULL, 0, id);
221 static int do_file(const char *filename, const char *fullpath, enum Hash h)
223 STACK_OF (X509_INFO) *inf;
225 X509_NAME *name = NULL;
228 unsigned char digest[EVP_MAX_MD_SIZE];
229 int i, type, ret = -1;
231 if ((ext = strrchr(filename, '.')) == NULL)
233 for (i = 0; i < (int)OSSL_NELEM(extensions); i++) {
234 if (strcasecmp(extensions[i], ext + 1) == 0)
237 if (i >= (int)OSSL_NELEM(extensions))
240 if ((b = BIO_new_file(fullpath, "r")) == NULL)
242 inf = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL);
247 if (sk_X509_INFO_num(inf) != 1) {
250 "it does not contain exactly one certificate or CRL\n",
251 opt_getprog(), filename);
254 x = sk_X509_INFO_value(inf, 0);
257 name = X509_get_subject_name(x->x509);
258 X509_digest(x->x509, evpmd, digest, NULL);
261 name = X509_CRL_get_issuer(x->crl);
262 X509_CRL_digest(x->crl, evpmd, digest, NULL);
265 if ((h == HASH_NEW) || (h == HASH_BOTH))
266 add_entry(type, X509_NAME_hash(name), filename, digest, 1, ~0);
267 if ((h == HASH_OLD) || (h == HASH_BOTH))
268 add_entry(type, X509_NAME_hash_old(name), filename, digest, 1, ~0);
272 sk_X509_INFO_pop_free(inf, X509_INFO_free);
276 static int do_dir(const char *dirname, enum Hash h)
280 OPENSSL_DIR_CTX *d = NULL;
282 unsigned char idmask[MAX_COLLISIONS / 8];
283 int i, n, nextid, buflen, ret = -1;
285 const char *filename;
288 buflen = strlen(dirname);
289 pathsep = (buflen && dirname[buflen - 1] == '/') ? "" : "/";
290 buflen += NAME_MAX + 2;
291 buf = app_malloc(buflen, "filename buffer");
294 BIO_printf(bio_out, "Doing %s\n", dirname);
296 while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) {
297 if (snprintf(buf, buflen, "%s%s%s",
298 dirname, pathsep, filename) >= buflen)
300 if (lstat(buf, &st) < 0)
302 if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0)
304 do_file(filename, buf, h);
308 for (i = 0; i < (int)OSSL_NELEM(hash_table); i++) {
309 for (bp = hash_table[i]; bp; bp = nextbp) {
312 memset(idmask, 0, (bp->num_needed + 7) / 8);
313 for (ep = bp->first_entry; ep; ep = ep->next)
314 if (ep->old_id < bp->num_needed)
315 bit_set(idmask, ep->old_id);
317 for (ep = bp->first_entry; ep; ep = nextep) {
319 if (ep->old_id < bp->num_needed) {
320 /* Link exists, and is used as-is */
321 snprintf(buf, buflen, "%08x.%s%d", bp->hash,
322 suffixes[bp->type], ep->old_id);
324 BIO_printf(bio_out, "link %s -> %s\n",
326 } else if (ep->need_symlink) {
327 /* New link needed (it may replace something) */
328 while (bit_isset(idmask, nextid))
331 snprintf(buf, buflen, "%s%s%n%08x.%s%d",
332 dirname, pathsep, &n, bp->hash,
333 suffixes[bp->type], nextid);
335 BIO_printf(bio_out, "link %s -> %s\n",
336 ep->filename, &buf[n]);
337 if (unlink(buf) < 0 && errno != ENOENT)
339 "%s: Can't unlink %s, %s\n",
340 opt_getprog(), buf, strerror(errno));
341 if (symlink(ep->filename, buf) < 0)
343 "%s: Can't symlink %s, %s\n",
344 opt_getprog(), ep->filename,
346 } else if (remove_links) {
347 /* Link to be deleted */
348 snprintf(buf, buflen, "%s%s%n%08x.%s%d",
349 dirname, pathsep, &n, bp->hash,
350 suffixes[bp->type], ep->old_id);
352 BIO_printf(bio_out, "unlink %s\n",
354 if (unlink(buf) < 0 && errno != ENOENT)
356 "%s: Can't unlink %s, %s\n",
357 opt_getprog(), buf, strerror(errno));
359 OPENSSL_free(ep->filename);
364 hash_table[i] = NULL;
372 typedef enum OPTION_choice {
373 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
374 OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE
377 OPTIONS rehash_options[] = {
378 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert-directory...]\n"},
379 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
380 {"help", OPT_HELP, '-', "Display this summary"},
381 {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"},
382 {"old", OPT_OLD, '-', "Use old-style hash to generate links"},
383 {"n", OPT_N, '-', "Do not remove existing links"},
384 {"v", OPT_VERBOSE, '-', "Verbose output"},
389 int rehash_main(int argc, char **argv)
391 const char *env, *prog;
395 enum Hash h = HASH_NEW;
397 prog = opt_init(argc, argv, rehash_options);
398 while ((o = opt_next()) != OPT_EOF) {
402 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
405 opt_help(rehash_options);
421 argc = opt_num_rest();
425 evpmdsize = EVP_MD_size(evpmd);
429 ret |= do_dir(*argv++, h);
430 } else if ((env = getenv("SSL_CERT_DIR")) != NULL) {
432 for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":"))
436 ret |= do_dir("/etc/ssl/certs", h);
444 OPTIONS rehash_options[] = {
448 int rehash_main(int argc, char **argv)
450 BIO_printf(bio_err, "Not available; use c_rehash script\n");