]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/rehash.c
Fix uninit warning. Remove unnecessary casts. Nothing to add is an error.
[thirdparty/openssl.git] / apps / rehash.c
CommitLineData
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
5278dec3 62#if defined(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
77# define MAX_COLLISIONS 256
78
79typedef struct hentry_st {
80 struct hentry_st *next;
81 char *filename;
82 unsigned short old_id;
83 unsigned char need_symlink;
84 unsigned char digest[EVP_MAX_MD_SIZE];
85} HENTRY;
86
87typedef struct bucket_st {
88 struct bucket_st *next;
89 HENTRY *first_entry, *last_entry;
90 unsigned int hash;
91 unsigned short type;
92 unsigned short num_needed;
93} BUCKET;
94
95enum Type {
96 /* Keep in sync with |suffixes|, below. */
97 TYPE_CERT=0, TYPE_CRL=1
98};
99
100enum Hash {
101 HASH_OLD, HASH_NEW, HASH_BOTH
102};
103
104
105static int evpmdsize;
106static const EVP_MD *evpmd;
107static int remove_links = 1;
108static int verbose = 0;
109static BUCKET *hash_table[257];
110
111static const char *suffixes[] = { "", "r" };
112static const char *extensions[] = { "pem", "crt", "cer", "crl" };
113
114
115static void bit_set(unsigned char *set, unsigned int bit)
116{
117 set[bit >> 3] |= 1 << (bit & 0x7);
118}
119
120static int bit_isset(unsigned char *set, unsigned int bit)
121{
122 return set[bit >> 3] & (1 << (bit & 0x7));
123}
124
125
ce249fac
RS
126/*
127 * Process an entry; return number of errors.
128 */
129static int add_entry(enum Type type, unsigned int hash, const char *filename,
8f6f1441
TT
130 const unsigned char *digest, int need_symlink,
131 unsigned short old_id)
132{
133 static BUCKET nilbucket;
134 static HENTRY nilhentry;
135 BUCKET *bp;
136 HENTRY *ep, *found = NULL;
137 unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table);
138
139 for (bp = hash_table[ndx]; bp; bp = bp->next)
140 if (bp->type == type && bp->hash == hash)
141 break;
142 if (bp == NULL) {
143 bp = app_malloc(sizeof(*bp), "hash bucket");
144 *bp = nilbucket;
145 bp->next = hash_table[ndx];
146 bp->type = type;
147 bp->hash = hash;
148 hash_table[ndx] = bp;
149 }
150
151 for (ep = bp->first_entry; ep; ep = ep->next) {
152 if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) {
153 BIO_printf(bio_err,
154 "%s: skipping duplicate certificate in %s\n",
155 opt_getprog(), filename);
ce249fac 156 return 1;
8f6f1441
TT
157 }
158 if (strcmp(filename, ep->filename) == 0) {
159 found = ep;
160 if (digest == NULL)
161 break;
162 }
163 }
164 ep = found;
165 if (ep == NULL) {
ce249fac
RS
166 if (bp->num_needed >= MAX_COLLISIONS) {
167 BIO_printf(bio_err,
168 "%s: hash table overflow for %s\n",
169 opt_getprog(), filename);
170 return 1;
171 }
8f6f1441
TT
172 ep = app_malloc(sizeof(*ep), "collision bucket");
173 *ep = nilhentry;
174 ep->old_id = ~0;
175 ep->filename = BUF_strdup(filename);
176 if (bp->last_entry)
177 bp->last_entry->next = ep;
178 if (bp->first_entry == NULL)
179 bp->first_entry = ep;
180 bp->last_entry = ep;
181 }
182
183 if (old_id < ep->old_id)
184 ep->old_id = old_id;
185 if (need_symlink && !ep->need_symlink) {
186 ep->need_symlink = 1;
187 bp->num_needed++;
188 memcpy(ep->digest, digest, evpmdsize);
189 }
ce249fac 190 return 0;
8f6f1441
TT
191}
192
ce249fac
RS
193/*
194 * Check if a symlink goes to the right spot; return 0 if okay.
195 * This can be -1 if bad filename, or an error count.
196 */
8f6f1441
TT
197static int handle_symlink(const char *filename, const char *fullpath)
198{
199 unsigned int hash = 0;
200 int i, type, id;
201 unsigned char ch;
202 char linktarget[NAME_MAX], *endptr;
203 ssize_t n;
204
205 for (i = 0; i < 8; i++) {
206 ch = filename[i];
207 if (!isxdigit(ch))
208 return -1;
209 hash <<= 4;
210 hash += app_hex(ch);
211 }
212 if (filename[i++] != '.')
213 return -1;
214 for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--)
215 if (strcasecmp(suffixes[type], &filename[i]) == 0)
216 break;
217 i += strlen(suffixes[type]);
218
219 id = strtoul(&filename[i], &endptr, 10);
220 if (*endptr != '\0')
221 return -1;
222
223 n = readlink(fullpath, linktarget, sizeof(linktarget));
224 if (n < 0 || n >= (int)sizeof(linktarget))
225 return -1;
226 linktarget[n] = 0;
227
ce249fac 228 return add_entry(type, hash, linktarget, NULL, 0, id);
8f6f1441
TT
229}
230
ce249fac
RS
231/*
232 * process a file, return number of errors.
233 */
8f6f1441
TT
234static int do_file(const char *filename, const char *fullpath, enum Hash h)
235{
ce249fac 236 STACK_OF (X509_INFO) *inf = NULL;
8f6f1441
TT
237 X509_INFO *x;
238 X509_NAME *name = NULL;
239 BIO *b;
240 const char *ext;
241 unsigned char digest[EVP_MAX_MD_SIZE];
8c82de99
BL
242 int type, errs = 0;
243 size_t i;
8f6f1441 244
ce249fac 245 /* Does it end with a recognized extension? */
8f6f1441 246 if ((ext = strrchr(filename, '.')) == NULL)
ce249fac 247 goto end;
8c82de99 248 for (i = 0; i < OSSL_NELEM(extensions); i++) {
8f6f1441
TT
249 if (strcasecmp(extensions[i], ext + 1) == 0)
250 break;
251 }
8c82de99 252 if (i >= OSSL_NELEM(extensions))
ce249fac 253 goto end;
8f6f1441 254
ce249fac
RS
255 /* Does it have X.509 data in it? */
256 if ((b = BIO_new_file(fullpath, "r")) == NULL) {
257 BIO_printf(bio_err, "%s: skipping %s, cannot open file\n",
258 opt_getprog(), filename);
259 errs++;
260 goto end;
261 }
8f6f1441
TT
262 inf = PEM_X509_INFO_read_bio(b, NULL, NULL, NULL);
263 BIO_free(b);
264 if (inf == NULL)
ce249fac 265 goto end;
8f6f1441
TT
266
267 if (sk_X509_INFO_num(inf) != 1) {
268 BIO_printf(bio_err,
269 "%s: skipping %s,"
270 "it does not contain exactly one certificate or CRL\n",
271 opt_getprog(), filename);
ce249fac 272 /* This is not an error. */
8f6f1441
TT
273 goto end;
274 }
275 x = sk_X509_INFO_value(inf, 0);
276 if (x->x509) {
277 type = TYPE_CERT;
278 name = X509_get_subject_name(x->x509);
279 X509_digest(x->x509, evpmd, digest, NULL);
280 } else if (x->crl) {
281 type = TYPE_CRL;
282 name = X509_CRL_get_issuer(x->crl);
283 X509_CRL_digest(x->crl, evpmd, digest, NULL);
8c82de99
BL
284 } else {
285 ++errs;
286 goto end;
8f6f1441
TT
287 }
288 if (name) {
289 if ((h == HASH_NEW) || (h == HASH_BOTH))
ce249fac 290 errs += add_entry(type, X509_NAME_hash(name), filename, digest, 1, ~0);
8f6f1441 291 if ((h == HASH_OLD) || (h == HASH_BOTH))
ce249fac 292 errs += add_entry(type, X509_NAME_hash_old(name), filename, digest, 1, ~0);
8f6f1441
TT
293 }
294
295end:
296 sk_X509_INFO_pop_free(inf, X509_INFO_free);
ce249fac 297 return errs;
8f6f1441
TT
298}
299
ce249fac
RS
300/*
301 * Process a directory; return number of errors found.
302 */
8f6f1441
TT
303static int do_dir(const char *dirname, enum Hash h)
304{
305 BUCKET *bp, *nextbp;
306 HENTRY *ep, *nextep;
307 OPENSSL_DIR_CTX *d = NULL;
308 struct stat st;
309 unsigned char idmask[MAX_COLLISIONS / 8];
8c82de99
BL
310 int n, nextid, buflen, errs = 0;
311 size_t i;
8f6f1441
TT
312 const char *pathsep;
313 const char *filename;
314 char *buf;
315
316 buflen = strlen(dirname);
317 pathsep = (buflen && dirname[buflen - 1] == '/') ? "" : "/";
318 buflen += NAME_MAX + 2;
319 buf = app_malloc(buflen, "filename buffer");
320
321 if (verbose)
322 BIO_printf(bio_out, "Doing %s\n", dirname);
323
324 while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) {
325 if (snprintf(buf, buflen, "%s%s%s",
326 dirname, pathsep, filename) >= buflen)
327 continue;
328 if (lstat(buf, &st) < 0)
329 continue;
330 if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0)
331 continue;
ce249fac 332 errs += do_file(filename, buf, h);
8f6f1441
TT
333 }
334 OPENSSL_DIR_end(&d);
335
8c82de99 336 for (i = 0; i < OSSL_NELEM(hash_table); i++) {
8f6f1441
TT
337 for (bp = hash_table[i]; bp; bp = nextbp) {
338 nextbp = bp->next;
339 nextid = 0;
340 memset(idmask, 0, (bp->num_needed + 7) / 8);
341 for (ep = bp->first_entry; ep; ep = ep->next)
342 if (ep->old_id < bp->num_needed)
343 bit_set(idmask, ep->old_id);
344
345 for (ep = bp->first_entry; ep; ep = nextep) {
346 nextep = ep->next;
347 if (ep->old_id < bp->num_needed) {
348 /* Link exists, and is used as-is */
349 snprintf(buf, buflen, "%08x.%s%d", bp->hash,
350 suffixes[bp->type], ep->old_id);
351 if (verbose)
352 BIO_printf(bio_out, "link %s -> %s\n",
353 ep->filename, buf);
354 } else if (ep->need_symlink) {
355 /* New link needed (it may replace something) */
356 while (bit_isset(idmask, nextid))
357 nextid++;
358
359 snprintf(buf, buflen, "%s%s%n%08x.%s%d",
360 dirname, pathsep, &n, bp->hash,
361 suffixes[bp->type], nextid);
362 if (verbose)
363 BIO_printf(bio_out, "link %s -> %s\n",
364 ep->filename, &buf[n]);
ce249fac 365 if (unlink(buf) < 0 && errno != ENOENT) {
8f6f1441
TT
366 BIO_printf(bio_err,
367 "%s: Can't unlink %s, %s\n",
368 opt_getprog(), buf, strerror(errno));
ce249fac
RS
369 errs++;
370 }
371 if (symlink(ep->filename, buf) < 0) {
8f6f1441
TT
372 BIO_printf(bio_err,
373 "%s: Can't symlink %s, %s\n",
374 opt_getprog(), ep->filename,
375 strerror(errno));
ce249fac
RS
376 errs++;
377 }
8f6f1441
TT
378 } else if (remove_links) {
379 /* Link to be deleted */
380 snprintf(buf, buflen, "%s%s%n%08x.%s%d",
381 dirname, pathsep, &n, bp->hash,
382 suffixes[bp->type], ep->old_id);
383 if (verbose)
384 BIO_printf(bio_out, "unlink %s\n",
385 &buf[n]);
ce249fac 386 if (unlink(buf) < 0 && errno != ENOENT) {
8f6f1441
TT
387 BIO_printf(bio_err,
388 "%s: Can't unlink %s, %s\n",
389 opt_getprog(), buf, strerror(errno));
ce249fac
RS
390 errs++;
391 }
8f6f1441
TT
392 }
393 OPENSSL_free(ep->filename);
394 OPENSSL_free(ep);
395 }
396 OPENSSL_free(bp);
397 }
398 hash_table[i] = NULL;
399 }
8f6f1441
TT
400
401 OPENSSL_free(buf);
ce249fac 402 return errs;
8f6f1441
TT
403}
404
405typedef enum OPTION_choice {
406 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
407 OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE
408} OPTION_CHOICE;
409
410OPTIONS rehash_options[] = {
411 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert-directory...]\n"},
412 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
413 {"help", OPT_HELP, '-', "Display this summary"},
414 {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"},
415 {"old", OPT_OLD, '-', "Use old-style hash to generate links"},
416 {"n", OPT_N, '-', "Do not remove existing links"},
417 {"v", OPT_VERBOSE, '-', "Verbose output"},
418 {NULL}
419};
420
421
422int rehash_main(int argc, char **argv)
423{
424 const char *env, *prog;
425 char *e, *m;
ce249fac 426 int errs = 0;
8f6f1441
TT
427 OPTION_CHOICE o;
428 enum Hash h = HASH_NEW;
429
430 prog = opt_init(argc, argv, rehash_options);
431 while ((o = opt_next()) != OPT_EOF) {
432 switch (o) {
433 case OPT_EOF:
434 case OPT_ERR:
435 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
436 goto end;
437 case OPT_HELP:
438 opt_help(rehash_options);
439 goto end;
440 case OPT_COMPAT:
441 h = HASH_BOTH;
442 break;
443 case OPT_OLD:
444 h = HASH_OLD;
445 break;
446 case OPT_N:
447 remove_links = 0;
448 break;
449 case OPT_VERBOSE:
450 verbose = 1;
451 break;
452 }
453 }
454 argc = opt_num_rest();
455 argv = opt_rest();
456
457 evpmd = EVP_sha1();
458 evpmdsize = EVP_MD_size(evpmd);
459
460 if (*argv) {
461 while (*argv)
ce249fac 462 errs += do_dir(*argv++, h);
8f6f1441
TT
463 } else if ((env = getenv("SSL_CERT_DIR")) != NULL) {
464 m = BUF_strdup(env);
465 for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":"))
ce249fac 466 errs += do_dir(e, h);
8f6f1441
TT
467 OPENSSL_free(m);
468 } else {
ce249fac 469 errs += do_dir("/etc/ssl/certs", h);
8f6f1441
TT
470 }
471
472 end:
ce249fac 473 return errs;
8f6f1441
TT
474}
475
476#else
62fdf4ee
RS
477OPTIONS rehash_options[] = {
478 {NULL}
479};
8f6f1441
TT
480
481int rehash_main(int argc, char **argv)
482{
9e0da060 483 BIO_printf(bio_err, "Not available; use c_rehash script\n");
8f6f1441
TT
484 return (1);
485}
486
5278dec3 487#endif /* defined(unix) || defined(__APPLE__) */