]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/rehash.c
Copyright year updates
[thirdparty/openssl.git] / apps / rehash.c
CommitLineData
8f6f1441 1/*
b6461792 2 * Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
4602cc85 3 * Copyright (c) 2013-2014 Timo Teräs <timo.teras@gmail.com>
8f6f1441 4 *
dffa7520 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
8f6f1441 9 */
846e33c7 10
8f6f1441 11#include "apps.h"
dab2cd68 12#include "progs.h"
8f6f1441 13
6616429d 14#if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) || \
5c80e2af 15 (defined(__VMS) && defined(__DECC) && __CRTL_VER >= 80300000)
8f6f1441
TT
16# include <unistd.h>
17# include <stdio.h>
18# include <limits.h>
19# include <errno.h>
20# include <string.h>
21# include <ctype.h>
22# include <sys/stat.h>
23
2ac915f1
RL
24/*
25 * Make sure that the processing of symbol names is treated the same as when
26 * libcrypto is built. This is done automatically for public headers (see
27 * include/openssl/__DECC_INCLUDE_PROLOGUE.H and __DECC_INCLUDE_EPILOGUE.H),
28 * but not for internal headers.
29 */
30# ifdef __VMS
31# pragma names save
32# pragma names as_is,shortened
33# endif
34
8f6f1441 35# include "internal/o_dir.h"
2ac915f1
RL
36
37# ifdef __VMS
38# pragma names restore
39# endif
40
8f6f1441
TT
41# include <openssl/evp.h>
42# include <openssl/pem.h>
43# include <openssl/x509.h>
44
6616429d
RL
45# ifndef PATH_MAX
46# define PATH_MAX 4096
47# endif
8f6f1441
TT
48# define MAX_COLLISIONS 256
49
5c8b7b4c
KT
50# if defined(OPENSSL_SYS_VXWORKS)
51/*
52 * VxWorks has no symbolic links
53 */
54
55# define lstat(path, buf) stat(path, buf)
56
57int symlink(const char *target, const char *linkpath)
58{
59 errno = ENOSYS;
60 return -1;
61}
62
63ssize_t readlink(const char *pathname, char *buf, size_t bufsiz)
64{
65 errno = ENOSYS;
66 return -1;
67}
68# endif
69
8f6f1441
TT
70typedef struct hentry_st {
71 struct hentry_st *next;
72 char *filename;
73 unsigned short old_id;
74 unsigned char need_symlink;
75 unsigned char digest[EVP_MAX_MD_SIZE];
76} HENTRY;
77
78typedef struct bucket_st {
79 struct bucket_st *next;
80 HENTRY *first_entry, *last_entry;
81 unsigned int hash;
82 unsigned short type;
83 unsigned short num_needed;
84} BUCKET;
85
86enum Type {
87 /* Keep in sync with |suffixes|, below. */
88 TYPE_CERT=0, TYPE_CRL=1
89};
90
91enum Hash {
92 HASH_OLD, HASH_NEW, HASH_BOTH
93};
94
95
96static int evpmdsize;
97static const EVP_MD *evpmd;
98static int remove_links = 1;
99static int verbose = 0;
100static BUCKET *hash_table[257];
101
102static const char *suffixes[] = { "", "r" };
103static const char *extensions[] = { "pem", "crt", "cer", "crl" };
104
105
106static void bit_set(unsigned char *set, unsigned int bit)
107{
108 set[bit >> 3] |= 1 << (bit & 0x7);
109}
110
111static int bit_isset(unsigned char *set, unsigned int bit)
112{
113 return set[bit >> 3] & (1 << (bit & 0x7));
114}
115
116
ce249fac
RS
117/*
118 * Process an entry; return number of errors.
119 */
120static int add_entry(enum Type type, unsigned int hash, const char *filename,
8f6f1441
TT
121 const unsigned char *digest, int need_symlink,
122 unsigned short old_id)
123{
124 static BUCKET nilbucket;
125 static HENTRY nilhentry;
126 BUCKET *bp;
127 HENTRY *ep, *found = NULL;
128 unsigned int ndx = (type + hash) % OSSL_NELEM(hash_table);
129
130 for (bp = hash_table[ndx]; bp; bp = bp->next)
131 if (bp->type == type && bp->hash == hash)
132 break;
133 if (bp == NULL) {
134 bp = app_malloc(sizeof(*bp), "hash bucket");
135 *bp = nilbucket;
136 bp->next = hash_table[ndx];
137 bp->type = type;
138 bp->hash = hash;
139 hash_table[ndx] = bp;
140 }
141
142 for (ep = bp->first_entry; ep; ep = ep->next) {
143 if (digest && memcmp(digest, ep->digest, evpmdsize) == 0) {
144 BIO_printf(bio_err,
e6a833cb
RL
145 "%s: warning: skipping duplicate %s in %s\n",
146 opt_getprog(),
f22ff0eb 147 type == TYPE_CERT ? "certificate" : "CRL", filename);
e6a833cb 148 return 0;
8f6f1441
TT
149 }
150 if (strcmp(filename, ep->filename) == 0) {
151 found = ep;
152 if (digest == NULL)
153 break;
154 }
155 }
156 ep = found;
157 if (ep == NULL) {
ce249fac
RS
158 if (bp->num_needed >= MAX_COLLISIONS) {
159 BIO_printf(bio_err,
e6a833cb 160 "%s: error: hash table overflow for %s\n",
ce249fac
RS
161 opt_getprog(), filename);
162 return 1;
163 }
8f6f1441
TT
164 ep = app_malloc(sizeof(*ep), "collision bucket");
165 *ep = nilhentry;
166 ep->old_id = ~0;
7644a9ae 167 ep->filename = OPENSSL_strdup(filename);
79cda38c
JJ
168 if (ep->filename == NULL) {
169 OPENSSL_free(ep);
170 ep = NULL;
171 BIO_printf(bio_err, "out of memory\n");
172 return 1;
173 }
8f6f1441
TT
174 if (bp->last_entry)
175 bp->last_entry->next = ep;
176 if (bp->first_entry == NULL)
177 bp->first_entry = ep;
178 bp->last_entry = ep;
179 }
180
181 if (old_id < ep->old_id)
182 ep->old_id = old_id;
183 if (need_symlink && !ep->need_symlink) {
184 ep->need_symlink = 1;
185 bp->num_needed++;
186 memcpy(ep->digest, digest, evpmdsize);
187 }
ce249fac 188 return 0;
8f6f1441
TT
189}
190
ce249fac
RS
191/*
192 * Check if a symlink goes to the right spot; return 0 if okay.
193 * This can be -1 if bad filename, or an error count.
194 */
8f6f1441
TT
195static int handle_symlink(const char *filename, const char *fullpath)
196{
197 unsigned int hash = 0;
198 int i, type, id;
199 unsigned char ch;
e7a68985 200 char linktarget[PATH_MAX], *endptr;
6616429d 201 ossl_ssize_t n;
8f6f1441
TT
202
203 for (i = 0; i < 8; i++) {
204 ch = filename[i];
205 if (!isxdigit(ch))
206 return -1;
207 hash <<= 4;
14f051a0 208 hash += OPENSSL_hexchar2int(ch);
8f6f1441
TT
209 }
210 if (filename[i++] != '.')
211 return -1;
747adb6a 212 for (type = OSSL_NELEM(suffixes) - 1; type > 0; type--)
fba140c7
DB
213 if (OPENSSL_strncasecmp(&filename[i],
214 suffixes[type], strlen(suffixes[type])) == 0)
8f6f1441 215 break;
fba140c7 216
8f6f1441
TT
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 237 X509_INFO *x;
8cc86b81 238 const X509_NAME *name = NULL;
8f6f1441
TT
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++) {
fba140c7 249 if (OPENSSL_strcasecmp(extensions[i], ext + 1) == 0)
8f6f1441
TT
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) {
e6a833cb 257 BIO_printf(bio_err, "%s: error: skipping %s, cannot open file\n",
ce249fac
RS
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,
0c2333d3 269 "%s: warning: skipping %s, "
8f6f1441
TT
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);
2234212c 276 if (x->x509 != NULL) {
8f6f1441
TT
277 type = TYPE_CERT;
278 name = X509_get_subject_name(x->x509);
7e06a675
BE
279 if (!X509_digest(x->x509, evpmd, digest, NULL)) {
280 BIO_printf(bio_err, "out of memory\n");
281 ++errs;
282 goto end;
283 }
2234212c 284 } else if (x->crl != NULL) {
8f6f1441
TT
285 type = TYPE_CRL;
286 name = X509_CRL_get_issuer(x->crl);
7e06a675
BE
287 if (!X509_CRL_digest(x->crl, evpmd, digest, NULL)) {
288 BIO_printf(bio_err, "out of memory\n");
289 ++errs;
290 goto end;
291 }
8c82de99
BL
292 } else {
293 ++errs;
294 goto end;
8f6f1441 295 }
2234212c 296 if (name != NULL) {
bf973d06
DDO
297 if (h == HASH_NEW || h == HASH_BOTH) {
298 int ok;
299 unsigned long hash_value =
300 X509_NAME_hash_ex(name,
301 app_get0_libctx(), app_get0_propq(), &ok);
302
303 if (ok) {
304 errs += add_entry(type, hash_value, filename, digest, 1, ~0);
305 } else {
306 BIO_printf(bio_err, "%s: error calculating SHA1 hash value\n",
307 opt_getprog());
308 errs++;
309 }
310 }
8f6f1441 311 if ((h == HASH_OLD) || (h == HASH_BOTH))
bf973d06
DDO
312 errs += add_entry(type, X509_NAME_hash_old(name),
313 filename, digest, 1, ~0);
8f6f1441
TT
314 }
315
316end:
317 sk_X509_INFO_pop_free(inf, X509_INFO_free);
ce249fac 318 return errs;
8f6f1441
TT
319}
320
b1ffe8db
RS
321static void str_free(char *s)
322{
323 OPENSSL_free(s);
324}
325
5c80e2af
RL
326static int ends_with_dirsep(const char *path)
327{
328 if (*path != '\0')
329 path += strlen(path) - 1;
46958a04 330# if defined __VMS
5c80e2af
RL
331 if (*path == ']' || *path == '>' || *path == ':')
332 return 1;
46958a04 333# elif defined _WIN32
5c80e2af
RL
334 if (*path == '\\')
335 return 1;
336# endif
337 return *path == '/';
338}
339
31c94b5e 340static int sk_strcmp(const char * const *a, const char * const *b)
341{
342 return strcmp(*a, *b);
343}
344
ce249fac
RS
345/*
346 * Process a directory; return number of errors found.
347 */
8f6f1441
TT
348static int do_dir(const char *dirname, enum Hash h)
349{
350 BUCKET *bp, *nextbp;
351 HENTRY *ep, *nextep;
352 OPENSSL_DIR_CTX *d = NULL;
353 struct stat st;
354 unsigned char idmask[MAX_COLLISIONS / 8];
ec0d22fe 355 int n, numfiles, nextid, dirlen, buflen, errs = 0;
de8e0851 356 size_t i, fname_max_len = 20; /* maximum length of "%08x.r%d" */
ec0d22fe 357 const char *pathsep = "";
8f6f1441 358 const char *filename;
de8e0851 359 char *buf = NULL, *copy = NULL;
b1ffe8db 360 STACK_OF(OPENSSL_STRING) *files = NULL;
8f6f1441 361
ff2f6bb0
RS
362 if (app_access(dirname, W_OK) < 0) {
363 BIO_printf(bio_err, "Skipping %s, can't write\n", dirname);
4c7103a5 364 return 1;
ff2f6bb0 365 }
ec0d22fe
MSP
366 dirlen = strlen(dirname);
367 if (dirlen != 0 && !ends_with_dirsep(dirname)) {
368 pathsep = "/";
369 dirlen++;
370 }
8f6f1441
TT
371
372 if (verbose)
373 BIO_printf(bio_out, "Doing %s\n", dirname);
374
31c94b5e 375 if ((files = sk_OPENSSL_STRING_new(sk_strcmp)) == NULL) {
b1ffe8db 376 BIO_printf(bio_err, "Skipping %s, out of memory\n", dirname);
02a7e0a9
TS
377 errs = 1;
378 goto err;
b1ffe8db 379 }
8f6f1441 380 while ((filename = OPENSSL_DIR_read(&d, dirname)) != NULL) {
de8e0851
TM
381 size_t fname_len = strlen(filename);
382
02a7e0a9 383 if ((copy = OPENSSL_strdup(filename)) == NULL
b1ffe8db 384 || sk_OPENSSL_STRING_push(files, copy) == 0) {
02a7e0a9 385 OPENSSL_free(copy);
01709fcb 386 OPENSSL_DIR_end(&d);
b1ffe8db 387 BIO_puts(bio_err, "out of memory\n");
02a7e0a9
TS
388 errs = 1;
389 goto err;
b1ffe8db 390 }
de8e0851
TM
391 if (fname_len > fname_max_len)
392 fname_max_len = fname_len;
b1ffe8db
RS
393 }
394 OPENSSL_DIR_end(&d);
395 sk_OPENSSL_STRING_sort(files);
396
de8e0851
TM
397 buflen = dirlen + fname_max_len + 1;
398 buf = app_malloc(buflen, "filename buffer");
399
b1ffe8db
RS
400 numfiles = sk_OPENSSL_STRING_num(files);
401 for (n = 0; n < numfiles; ++n) {
402 filename = sk_OPENSSL_STRING_value(files, n);
c41048ff
RS
403 if (BIO_snprintf(buf, buflen, "%s%s%s",
404 dirname, pathsep, filename) >= buflen)
8f6f1441
TT
405 continue;
406 if (lstat(buf, &st) < 0)
407 continue;
408 if (S_ISLNK(st.st_mode) && handle_symlink(filename, buf) == 0)
409 continue;
ce249fac 410 errs += do_file(filename, buf, h);
8f6f1441 411 }
8f6f1441 412
8c82de99 413 for (i = 0; i < OSSL_NELEM(hash_table); i++) {
8f6f1441
TT
414 for (bp = hash_table[i]; bp; bp = nextbp) {
415 nextbp = bp->next;
416 nextid = 0;
417 memset(idmask, 0, (bp->num_needed + 7) / 8);
418 for (ep = bp->first_entry; ep; ep = ep->next)
419 if (ep->old_id < bp->num_needed)
420 bit_set(idmask, ep->old_id);
421
422 for (ep = bp->first_entry; ep; ep = nextep) {
423 nextep = ep->next;
424 if (ep->old_id < bp->num_needed) {
425 /* Link exists, and is used as-is */
c41048ff
RS
426 BIO_snprintf(buf, buflen, "%08x.%s%d", bp->hash,
427 suffixes[bp->type], ep->old_id);
8f6f1441
TT
428 if (verbose)
429 BIO_printf(bio_out, "link %s -> %s\n",
430 ep->filename, buf);
431 } else if (ep->need_symlink) {
432 /* New link needed (it may replace something) */
433 while (bit_isset(idmask, nextid))
434 nextid++;
435
ec0d22fe
MSP
436 BIO_snprintf(buf, buflen, "%s%s%08x.%s%d",
437 dirname, pathsep, bp->hash,
c41048ff 438 suffixes[bp->type], nextid);
8f6f1441
TT
439 if (verbose)
440 BIO_printf(bio_out, "link %s -> %s\n",
ec0d22fe 441 ep->filename, &buf[dirlen]);
ce249fac 442 if (unlink(buf) < 0 && errno != ENOENT) {
8f6f1441
TT
443 BIO_printf(bio_err,
444 "%s: Can't unlink %s, %s\n",
445 opt_getprog(), buf, strerror(errno));
ce249fac
RS
446 errs++;
447 }
448 if (symlink(ep->filename, buf) < 0) {
8f6f1441
TT
449 BIO_printf(bio_err,
450 "%s: Can't symlink %s, %s\n",
451 opt_getprog(), ep->filename,
452 strerror(errno));
ce249fac
RS
453 errs++;
454 }
3770b877 455 bit_set(idmask, nextid);
8f6f1441
TT
456 } else if (remove_links) {
457 /* Link to be deleted */
ec0d22fe
MSP
458 BIO_snprintf(buf, buflen, "%s%s%08x.%s%d",
459 dirname, pathsep, bp->hash,
c41048ff 460 suffixes[bp->type], ep->old_id);
8f6f1441
TT
461 if (verbose)
462 BIO_printf(bio_out, "unlink %s\n",
ec0d22fe 463 &buf[dirlen]);
ce249fac 464 if (unlink(buf) < 0 && errno != ENOENT) {
8f6f1441
TT
465 BIO_printf(bio_err,
466 "%s: Can't unlink %s, %s\n",
467 opt_getprog(), buf, strerror(errno));
ce249fac
RS
468 errs++;
469 }
8f6f1441
TT
470 }
471 OPENSSL_free(ep->filename);
472 OPENSSL_free(ep);
473 }
474 OPENSSL_free(bp);
475 }
476 hash_table[i] = NULL;
477 }
8f6f1441 478
02a7e0a9
TS
479 err:
480 sk_OPENSSL_STRING_pop_free(files, str_free);
8f6f1441 481 OPENSSL_free(buf);
ce249fac 482 return errs;
8f6f1441
TT
483}
484
485typedef enum OPTION_choice {
b0f96018 486 OPT_COMMON,
6bd4e3f2
P
487 OPT_COMPAT, OPT_OLD, OPT_N, OPT_VERBOSE,
488 OPT_PROV_ENUM
8f6f1441
TT
489} OPTION_CHOICE;
490
44c83ebd 491const OPTIONS rehash_options[] = {
92de469f 492 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [directory...]\n"},
5388f986
RS
493
494 OPT_SECTION("General"),
8f6f1441 495 {"help", OPT_HELP, '-', "Display this summary"},
7d959c35 496 {"h", OPT_HELP, '-', "Display this summary"},
8f6f1441
TT
497 {"compat", OPT_COMPAT, '-', "Create both new- and old-style hash links"},
498 {"old", OPT_OLD, '-', "Use old-style hash to generate links"},
499 {"n", OPT_N, '-', "Do not remove existing links"},
5388f986
RS
500
501 OPT_SECTION("Output"),
8f6f1441 502 {"v", OPT_VERBOSE, '-', "Verbose output"},
92de469f 503
6bd4e3f2
P
504 OPT_PROV_OPTIONS,
505
92de469f
RS
506 OPT_PARAMETERS(),
507 {"directory", 0, 0, "One or more directories to process (optional)"},
8f6f1441
TT
508 {NULL}
509};
510
511
512int rehash_main(int argc, char **argv)
513{
514 const char *env, *prog;
515 char *e, *m;
ce249fac 516 int errs = 0;
8f6f1441
TT
517 OPTION_CHOICE o;
518 enum Hash h = HASH_NEW;
519
520 prog = opt_init(argc, argv, rehash_options);
521 while ((o = opt_next()) != OPT_EOF) {
522 switch (o) {
523 case OPT_EOF:
524 case OPT_ERR:
525 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
526 goto end;
527 case OPT_HELP:
528 opt_help(rehash_options);
529 goto end;
530 case OPT_COMPAT:
531 h = HASH_BOTH;
532 break;
533 case OPT_OLD:
534 h = HASH_OLD;
535 break;
536 case OPT_N:
537 remove_links = 0;
538 break;
539 case OPT_VERBOSE:
540 verbose = 1;
541 break;
6bd4e3f2
P
542 case OPT_PROV_CASES:
543 if (!opt_provider(o))
544 goto end;
545 break;
8f6f1441
TT
546 }
547 }
021410ea
RS
548
549 /* Optional arguments are directories to scan. */
8f6f1441
TT
550 argc = opt_num_rest();
551 argv = opt_rest();
552
553 evpmd = EVP_sha1();
ed576acd 554 evpmdsize = EVP_MD_get_size(evpmd);
8f6f1441 555
2234212c
PY
556 if (*argv != NULL) {
557 while (*argv != NULL)
ce249fac 558 errs += do_dir(*argv++, h);
362ff3c3
RL
559 } else if ((env = getenv(X509_get_default_cert_dir_env())) != NULL) {
560 char lsc[2] = { LIST_SEPARATOR_CHAR, '\0' };
7644a9ae 561 m = OPENSSL_strdup(env);
362ff3c3 562 for (e = strtok(m, lsc); e != NULL; e = strtok(NULL, lsc))
ce249fac 563 errs += do_dir(e, h);
8f6f1441
TT
564 OPENSSL_free(m);
565 } else {
362ff3c3 566 errs += do_dir(X509_get_default_cert_dir(), h);
8f6f1441
TT
567 }
568
569 end:
ce249fac 570 return errs;
8f6f1441
TT
571}
572
573#else
44c83ebd 574const OPTIONS rehash_options[] = {
62fdf4ee
RS
575 {NULL}
576};
8f6f1441
TT
577
578int rehash_main(int argc, char **argv)
579{
9e0da060 580 BIO_printf(bio_err, "Not available; use c_rehash script\n");
208fb891 581 return 1;
8f6f1441
TT
582}
583
568b8020 584#endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */