]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rehash.c
2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2013-2014 Timo Teräs <timo.teras@gmail.com>
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
14 #if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) || \
15 (defined(__VMS) && defined(__DECC) && __CRTL_VER >= 80300000)
22 # include <sys/stat.h>
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.
32 # pragma names as_is,shortened
35 # include "internal/o_dir.h"
38 # pragma names restore
41 # include <openssl/evp.h>
42 # include <openssl/pem.h>
43 # include <openssl/x509.h>
46 # define PATH_MAX 4096
51 # define MAX_COLLISIONS 256
53 # if defined(OPENSSL_SYS_VXWORKS)
55 * VxWorks has no symbolic links
58 # define lstat(path, buf) stat(path, buf)
60 int symlink(const char *target
, const char *linkpath
)
66 ssize_t
readlink(const char *pathname
, char *buf
, size_t bufsiz
)
73 typedef struct hentry_st
{
74 struct hentry_st
*next
;
76 unsigned short old_id
;
77 unsigned char need_symlink
;
78 unsigned char digest
[EVP_MAX_MD_SIZE
];
81 typedef struct bucket_st
{
82 struct bucket_st
*next
;
83 HENTRY
*first_entry
, *last_entry
;
86 unsigned short num_needed
;
90 /* Keep in sync with |suffixes|, below. */
91 TYPE_CERT
=0, TYPE_CRL
=1
95 HASH_OLD
, HASH_NEW
, HASH_BOTH
100 static const EVP_MD
*evpmd
;
101 static int remove_links
= 1;
102 static int verbose
= 0;
103 static BUCKET
*hash_table
[257];
105 static const char *suffixes
[] = { "", "r" };
106 static const char *extensions
[] = { "pem", "crt", "cer", "crl" };
109 static void bit_set(unsigned char *set
, unsigned int bit
)
111 set
[bit
>> 3] |= 1 << (bit
& 0x7);
114 static int bit_isset(unsigned char *set
, unsigned int bit
)
116 return set
[bit
>> 3] & (1 << (bit
& 0x7));
121 * Process an entry; return number of errors.
123 static int add_entry(enum Type type
, unsigned int hash
, const char *filename
,
124 const unsigned char *digest
, int need_symlink
,
125 unsigned short old_id
)
127 static BUCKET nilbucket
;
128 static HENTRY nilhentry
;
130 HENTRY
*ep
, *found
= NULL
;
131 unsigned int ndx
= (type
+ hash
) % OSSL_NELEM(hash_table
);
133 for (bp
= hash_table
[ndx
]; bp
; bp
= bp
->next
)
134 if (bp
->type
== type
&& bp
->hash
== hash
)
137 bp
= app_malloc(sizeof(*bp
), "hash bucket");
139 bp
->next
= hash_table
[ndx
];
142 hash_table
[ndx
] = bp
;
145 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
) {
146 if (digest
&& memcmp(digest
, ep
->digest
, evpmdsize
) == 0) {
148 "%s: warning: skipping duplicate %s in %s\n",
150 type
== TYPE_CERT
? "certificate" : "CRL", filename
);
153 if (strcmp(filename
, ep
->filename
) == 0) {
161 if (bp
->num_needed
>= MAX_COLLISIONS
) {
163 "%s: error: hash table overflow for %s\n",
164 opt_getprog(), filename
);
167 ep
= app_malloc(sizeof(*ep
), "collision bucket");
170 ep
->filename
= OPENSSL_strdup(filename
);
172 bp
->last_entry
->next
= ep
;
173 if (bp
->first_entry
== NULL
)
174 bp
->first_entry
= ep
;
178 if (old_id
< ep
->old_id
)
180 if (need_symlink
&& !ep
->need_symlink
) {
181 ep
->need_symlink
= 1;
183 memcpy(ep
->digest
, digest
, evpmdsize
);
189 * Check if a symlink goes to the right spot; return 0 if okay.
190 * This can be -1 if bad filename, or an error count.
192 static int handle_symlink(const char *filename
, const char *fullpath
)
194 unsigned int hash
= 0;
197 char linktarget
[PATH_MAX
], *endptr
;
200 for (i
= 0; i
< 8; i
++) {
205 hash
+= OPENSSL_hexchar2int(ch
);
207 if (filename
[i
++] != '.')
209 for (type
= OSSL_NELEM(suffixes
) - 1; type
> 0; type
--) {
210 const char *suffix
= suffixes
[type
];
211 if (strncasecmp(suffix
, &filename
[i
], strlen(suffix
)) == 0)
214 i
+= strlen(suffixes
[type
]);
216 id
= strtoul(&filename
[i
], &endptr
, 10);
220 n
= readlink(fullpath
, linktarget
, sizeof(linktarget
));
221 if (n
< 0 || n
>= (int)sizeof(linktarget
))
225 return add_entry(type
, hash
, linktarget
, NULL
, 0, id
);
229 * process a file, return number of errors.
231 static int do_file(const char *filename
, const char *fullpath
, enum Hash h
)
233 STACK_OF (X509_INFO
) *inf
= NULL
;
235 const X509_NAME
*name
= NULL
;
238 unsigned char digest
[EVP_MAX_MD_SIZE
];
242 /* Does it end with a recognized extension? */
243 if ((ext
= strrchr(filename
, '.')) == NULL
)
245 for (i
= 0; i
< OSSL_NELEM(extensions
); i
++) {
246 if (strcasecmp(extensions
[i
], ext
+ 1) == 0)
249 if (i
>= OSSL_NELEM(extensions
))
252 /* Does it have X.509 data in it? */
253 if ((b
= BIO_new_file(fullpath
, "r")) == NULL
) {
254 BIO_printf(bio_err
, "%s: error: skipping %s, cannot open file\n",
255 opt_getprog(), filename
);
259 inf
= PEM_X509_INFO_read_bio(b
, NULL
, NULL
, NULL
);
264 if (sk_X509_INFO_num(inf
) != 1) {
266 "%s: warning: skipping %s,"
267 "it does not contain exactly one certificate or CRL\n",
268 opt_getprog(), filename
);
269 /* This is not an error. */
272 x
= sk_X509_INFO_value(inf
, 0);
273 if (x
->x509
!= NULL
) {
275 name
= X509_get_subject_name(x
->x509
);
276 if (!X509_digest(x
->x509
, evpmd
, digest
, NULL
)) {
277 BIO_printf(bio_err
, "out of memory\n");
281 } else if (x
->crl
!= NULL
) {
283 name
= X509_CRL_get_issuer(x
->crl
);
284 if (!X509_CRL_digest(x
->crl
, evpmd
, digest
, NULL
)) {
285 BIO_printf(bio_err
, "out of memory\n");
294 if (h
== HASH_NEW
|| h
== HASH_BOTH
) {
296 unsigned long hash_value
=
297 X509_NAME_hash_ex(name
,
298 app_get0_libctx(), app_get0_propq(), &ok
);
301 errs
+= add_entry(type
, hash_value
, filename
, digest
, 1, ~0);
303 BIO_printf(bio_err
, "%s: error calculating SHA1 hash value\n",
308 if ((h
== HASH_OLD
) || (h
== HASH_BOTH
))
309 errs
+= add_entry(type
, X509_NAME_hash_old(name
),
310 filename
, digest
, 1, ~0);
314 sk_X509_INFO_pop_free(inf
, X509_INFO_free
);
318 static void str_free(char *s
)
323 static int ends_with_dirsep(const char *path
)
326 path
+= strlen(path
) - 1;
328 if (*path
== ']' || *path
== '>' || *path
== ':')
330 # elif defined _WIN32
338 * Process a directory; return number of errors found.
340 static int do_dir(const char *dirname
, enum Hash h
)
344 OPENSSL_DIR_CTX
*d
= NULL
;
346 unsigned char idmask
[MAX_COLLISIONS
/ 8];
347 int n
, numfiles
, nextid
, buflen
, errs
= 0;
350 const char *filename
;
351 char *buf
, *copy
= NULL
;
352 STACK_OF(OPENSSL_STRING
) *files
= NULL
;
354 if (app_access(dirname
, W_OK
) < 0) {
355 BIO_printf(bio_err
, "Skipping %s, can't write\n", dirname
);
358 buflen
= strlen(dirname
);
359 pathsep
= (buflen
&& !ends_with_dirsep(dirname
)) ? "/": "";
360 buflen
+= NAME_MAX
+ 1 + 1;
361 buf
= app_malloc(buflen
, "filename buffer");
364 BIO_printf(bio_out
, "Doing %s\n", dirname
);
366 if ((files
= sk_OPENSSL_STRING_new_null()) == NULL
) {
367 BIO_printf(bio_err
, "Skipping %s, out of memory\n", dirname
);
371 while ((filename
= OPENSSL_DIR_read(&d
, dirname
)) != NULL
) {
372 if ((copy
= OPENSSL_strdup(filename
)) == NULL
373 || sk_OPENSSL_STRING_push(files
, copy
) == 0) {
375 BIO_puts(bio_err
, "out of memory\n");
381 sk_OPENSSL_STRING_sort(files
);
383 numfiles
= sk_OPENSSL_STRING_num(files
);
384 for (n
= 0; n
< numfiles
; ++n
) {
385 filename
= sk_OPENSSL_STRING_value(files
, n
);
386 if (BIO_snprintf(buf
, buflen
, "%s%s%s",
387 dirname
, pathsep
, filename
) >= buflen
)
389 if (lstat(buf
, &st
) < 0)
391 if (S_ISLNK(st
.st_mode
) && handle_symlink(filename
, buf
) == 0)
393 errs
+= do_file(filename
, buf
, h
);
396 for (i
= 0; i
< OSSL_NELEM(hash_table
); i
++) {
397 for (bp
= hash_table
[i
]; bp
; bp
= nextbp
) {
400 memset(idmask
, 0, (bp
->num_needed
+ 7) / 8);
401 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
)
402 if (ep
->old_id
< bp
->num_needed
)
403 bit_set(idmask
, ep
->old_id
);
405 for (ep
= bp
->first_entry
; ep
; ep
= nextep
) {
407 if (ep
->old_id
< bp
->num_needed
) {
408 /* Link exists, and is used as-is */
409 BIO_snprintf(buf
, buflen
, "%08x.%s%d", bp
->hash
,
410 suffixes
[bp
->type
], ep
->old_id
);
412 BIO_printf(bio_out
, "link %s -> %s\n",
414 } else if (ep
->need_symlink
) {
415 /* New link needed (it may replace something) */
416 while (bit_isset(idmask
, nextid
))
419 BIO_snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
420 dirname
, pathsep
, &n
, bp
->hash
,
421 suffixes
[bp
->type
], nextid
);
423 BIO_printf(bio_out
, "link %s -> %s\n",
424 ep
->filename
, &buf
[n
]);
425 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
427 "%s: Can't unlink %s, %s\n",
428 opt_getprog(), buf
, strerror(errno
));
431 if (symlink(ep
->filename
, buf
) < 0) {
433 "%s: Can't symlink %s, %s\n",
434 opt_getprog(), ep
->filename
,
438 bit_set(idmask
, nextid
);
439 } else if (remove_links
) {
440 /* Link to be deleted */
441 BIO_snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
442 dirname
, pathsep
, &n
, bp
->hash
,
443 suffixes
[bp
->type
], ep
->old_id
);
445 BIO_printf(bio_out
, "unlink %s\n",
447 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
449 "%s: Can't unlink %s, %s\n",
450 opt_getprog(), buf
, strerror(errno
));
454 OPENSSL_free(ep
->filename
);
459 hash_table
[i
] = NULL
;
463 sk_OPENSSL_STRING_pop_free(files
, str_free
);
468 typedef enum OPTION_choice
{
470 OPT_COMPAT
, OPT_OLD
, OPT_N
, OPT_VERBOSE
,
474 const OPTIONS rehash_options
[] = {
475 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [directory...]\n"},
477 OPT_SECTION("General"),
478 {"help", OPT_HELP
, '-', "Display this summary"},
479 {"h", OPT_HELP
, '-', "Display this summary"},
480 {"compat", OPT_COMPAT
, '-', "Create both new- and old-style hash links"},
481 {"old", OPT_OLD
, '-', "Use old-style hash to generate links"},
482 {"n", OPT_N
, '-', "Do not remove existing links"},
484 OPT_SECTION("Output"),
485 {"v", OPT_VERBOSE
, '-', "Verbose output"},
490 {"directory", 0, 0, "One or more directories to process (optional)"},
495 int rehash_main(int argc
, char **argv
)
497 const char *env
, *prog
;
501 enum Hash h
= HASH_NEW
;
503 prog
= opt_init(argc
, argv
, rehash_options
);
504 while ((o
= opt_next()) != OPT_EOF
) {
508 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
511 opt_help(rehash_options
);
526 if (!opt_provider(o
))
532 /* Optional arguments are directories to scan. */
533 argc
= opt_num_rest();
537 evpmdsize
= EVP_MD_size(evpmd
);
540 while (*argv
!= NULL
)
541 errs
+= do_dir(*argv
++, h
);
542 } else if ((env
= getenv(X509_get_default_cert_dir_env())) != NULL
) {
543 char lsc
[2] = { LIST_SEPARATOR_CHAR
, '\0' };
544 m
= OPENSSL_strdup(env
);
545 for (e
= strtok(m
, lsc
); e
!= NULL
; e
= strtok(NULL
, lsc
))
546 errs
+= do_dir(e
, h
);
549 errs
+= do_dir(X509_get_default_cert_dir(), h
);
557 const OPTIONS rehash_options
[] = {
561 int rehash_main(int argc
, char **argv
)
563 BIO_printf(bio_err
, "Not available; use c_rehash script\n");
567 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */