]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rehash.c
2 * Copyright 2015-2018 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>
47 # define PATH_MAX 4096
52 # define MAX_COLLISIONS 256
54 typedef struct hentry_st
{
55 struct hentry_st
*next
;
57 unsigned short old_id
;
58 unsigned char need_symlink
;
59 unsigned char digest
[EVP_MAX_MD_SIZE
];
62 typedef struct bucket_st
{
63 struct bucket_st
*next
;
64 HENTRY
*first_entry
, *last_entry
;
67 unsigned short num_needed
;
71 /* Keep in sync with |suffixes|, below. */
72 TYPE_CERT
=0, TYPE_CRL
=1
76 HASH_OLD
, HASH_NEW
, HASH_BOTH
81 static const EVP_MD
*evpmd
;
82 static int remove_links
= 1;
83 static int verbose
= 0;
84 static BUCKET
*hash_table
[257];
86 static const char *suffixes
[] = { "", "r" };
87 static const char *extensions
[] = { "pem", "crt", "cer", "crl" };
90 static void bit_set(unsigned char *set
, unsigned int bit
)
92 set
[bit
>> 3] |= 1 << (bit
& 0x7);
95 static int bit_isset(unsigned char *set
, unsigned int bit
)
97 return set
[bit
>> 3] & (1 << (bit
& 0x7));
102 * Process an entry; return number of errors.
104 static int add_entry(enum Type type
, unsigned int hash
, const char *filename
,
105 const unsigned char *digest
, int need_symlink
,
106 unsigned short old_id
)
108 static BUCKET nilbucket
;
109 static HENTRY nilhentry
;
111 HENTRY
*ep
, *found
= NULL
;
112 unsigned int ndx
= (type
+ hash
) % OSSL_NELEM(hash_table
);
114 for (bp
= hash_table
[ndx
]; bp
; bp
= bp
->next
)
115 if (bp
->type
== type
&& bp
->hash
== hash
)
118 bp
= app_malloc(sizeof(*bp
), "hash bucket");
120 bp
->next
= hash_table
[ndx
];
123 hash_table
[ndx
] = bp
;
126 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
) {
127 if (digest
&& memcmp(digest
, ep
->digest
, evpmdsize
) == 0) {
129 "%s: warning: skipping duplicate %s in %s\n",
131 type
== TYPE_CERT
? "certificate" : "CRL", filename
);
134 if (strcmp(filename
, ep
->filename
) == 0) {
142 if (bp
->num_needed
>= MAX_COLLISIONS
) {
144 "%s: error: hash table overflow for %s\n",
145 opt_getprog(), filename
);
148 ep
= app_malloc(sizeof(*ep
), "collision bucket");
151 ep
->filename
= OPENSSL_strdup(filename
);
153 bp
->last_entry
->next
= ep
;
154 if (bp
->first_entry
== NULL
)
155 bp
->first_entry
= ep
;
159 if (old_id
< ep
->old_id
)
161 if (need_symlink
&& !ep
->need_symlink
) {
162 ep
->need_symlink
= 1;
164 memcpy(ep
->digest
, digest
, evpmdsize
);
170 * Check if a symlink goes to the right spot; return 0 if okay.
171 * This can be -1 if bad filename, or an error count.
173 static int handle_symlink(const char *filename
, const char *fullpath
)
175 unsigned int hash
= 0;
178 char linktarget
[PATH_MAX
], *endptr
;
181 for (i
= 0; i
< 8; i
++) {
186 hash
+= OPENSSL_hexchar2int(ch
);
188 if (filename
[i
++] != '.')
190 for (type
= OSSL_NELEM(suffixes
) - 1; type
> 0; type
--) {
191 const char *suffix
= suffixes
[type
];
192 if (strncasecmp(suffix
, &filename
[i
], strlen(suffix
)) == 0)
195 i
+= strlen(suffixes
[type
]);
197 id
= strtoul(&filename
[i
], &endptr
, 10);
201 n
= readlink(fullpath
, linktarget
, sizeof(linktarget
));
202 if (n
< 0 || n
>= (int)sizeof(linktarget
))
206 return add_entry(type
, hash
, linktarget
, NULL
, 0, id
);
210 * process a file, return number of errors.
212 static int do_file(const char *filename
, const char *fullpath
, enum Hash h
)
214 STACK_OF (X509_INFO
) *inf
= NULL
;
216 X509_NAME
*name
= NULL
;
219 unsigned char digest
[EVP_MAX_MD_SIZE
];
223 /* Does it end with a recognized extension? */
224 if ((ext
= strrchr(filename
, '.')) == NULL
)
226 for (i
= 0; i
< OSSL_NELEM(extensions
); i
++) {
227 if (strcasecmp(extensions
[i
], ext
+ 1) == 0)
230 if (i
>= OSSL_NELEM(extensions
))
233 /* Does it have X.509 data in it? */
234 if ((b
= BIO_new_file(fullpath
, "r")) == NULL
) {
235 BIO_printf(bio_err
, "%s: error: skipping %s, cannot open file\n",
236 opt_getprog(), filename
);
240 inf
= PEM_X509_INFO_read_bio(b
, NULL
, NULL
, NULL
);
245 if (sk_X509_INFO_num(inf
) != 1) {
247 "%s: warning: skipping %s,"
248 "it does not contain exactly one certificate or CRL\n",
249 opt_getprog(), filename
);
250 /* This is not an error. */
253 x
= sk_X509_INFO_value(inf
, 0);
254 if (x
->x509
!= NULL
) {
256 name
= X509_get_subject_name(x
->x509
);
257 X509_digest(x
->x509
, evpmd
, digest
, NULL
);
258 } else if (x
->crl
!= NULL
) {
260 name
= X509_CRL_get_issuer(x
->crl
);
261 X509_CRL_digest(x
->crl
, evpmd
, digest
, NULL
);
267 if ((h
== HASH_NEW
) || (h
== HASH_BOTH
))
268 errs
+= add_entry(type
, X509_NAME_hash(name
), filename
, digest
, 1, ~0);
269 if ((h
== HASH_OLD
) || (h
== HASH_BOTH
))
270 errs
+= add_entry(type
, X509_NAME_hash_old(name
), filename
, digest
, 1, ~0);
274 sk_X509_INFO_pop_free(inf
, X509_INFO_free
);
278 static void str_free(char *s
)
283 static int ends_with_dirsep(const char *path
)
286 path
+= strlen(path
) - 1;
288 if (*path
== ']' || *path
== '>' || *path
== ':')
290 # elif defined _WIN32
298 * Process a directory; return number of errors found.
300 static int do_dir(const char *dirname
, enum Hash h
)
304 OPENSSL_DIR_CTX
*d
= NULL
;
306 unsigned char idmask
[MAX_COLLISIONS
/ 8];
307 int n
, numfiles
, nextid
, buflen
, errs
= 0;
310 const char *filename
;
311 char *buf
, *copy
= NULL
;
312 STACK_OF(OPENSSL_STRING
) *files
= NULL
;
314 if (app_access(dirname
, W_OK
) < 0) {
315 BIO_printf(bio_err
, "Skipping %s, can't write\n", dirname
);
318 buflen
= strlen(dirname
);
319 pathsep
= (buflen
&& !ends_with_dirsep(dirname
)) ? "/": "";
320 buflen
+= NAME_MAX
+ 1 + 1;
321 buf
= app_malloc(buflen
, "filename buffer");
324 BIO_printf(bio_out
, "Doing %s\n", dirname
);
326 if ((files
= sk_OPENSSL_STRING_new_null()) == NULL
) {
327 BIO_printf(bio_err
, "Skipping %s, out of memory\n", dirname
);
331 while ((filename
= OPENSSL_DIR_read(&d
, dirname
)) != NULL
) {
332 if ((copy
= OPENSSL_strdup(filename
)) == NULL
333 || sk_OPENSSL_STRING_push(files
, copy
) == 0) {
335 BIO_puts(bio_err
, "out of memory\n");
341 sk_OPENSSL_STRING_sort(files
);
343 numfiles
= sk_OPENSSL_STRING_num(files
);
344 for (n
= 0; n
< numfiles
; ++n
) {
345 filename
= sk_OPENSSL_STRING_value(files
, n
);
346 if (BIO_snprintf(buf
, buflen
, "%s%s%s",
347 dirname
, pathsep
, filename
) >= buflen
)
349 if (lstat(buf
, &st
) < 0)
351 if (S_ISLNK(st
.st_mode
) && handle_symlink(filename
, buf
) == 0)
353 errs
+= do_file(filename
, buf
, h
);
356 for (i
= 0; i
< OSSL_NELEM(hash_table
); i
++) {
357 for (bp
= hash_table
[i
]; bp
; bp
= nextbp
) {
360 memset(idmask
, 0, (bp
->num_needed
+ 7) / 8);
361 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
)
362 if (ep
->old_id
< bp
->num_needed
)
363 bit_set(idmask
, ep
->old_id
);
365 for (ep
= bp
->first_entry
; ep
; ep
= nextep
) {
367 if (ep
->old_id
< bp
->num_needed
) {
368 /* Link exists, and is used as-is */
369 BIO_snprintf(buf
, buflen
, "%08x.%s%d", bp
->hash
,
370 suffixes
[bp
->type
], ep
->old_id
);
372 BIO_printf(bio_out
, "link %s -> %s\n",
374 } else if (ep
->need_symlink
) {
375 /* New link needed (it may replace something) */
376 while (bit_isset(idmask
, nextid
))
379 BIO_snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
380 dirname
, pathsep
, &n
, bp
->hash
,
381 suffixes
[bp
->type
], nextid
);
383 BIO_printf(bio_out
, "link %s -> %s\n",
384 ep
->filename
, &buf
[n
]);
385 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
387 "%s: Can't unlink %s, %s\n",
388 opt_getprog(), buf
, strerror(errno
));
391 if (symlink(ep
->filename
, buf
) < 0) {
393 "%s: Can't symlink %s, %s\n",
394 opt_getprog(), ep
->filename
,
398 bit_set(idmask
, nextid
);
399 } else if (remove_links
) {
400 /* Link to be deleted */
401 BIO_snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
402 dirname
, pathsep
, &n
, bp
->hash
,
403 suffixes
[bp
->type
], ep
->old_id
);
405 BIO_printf(bio_out
, "unlink %s\n",
407 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
409 "%s: Can't unlink %s, %s\n",
410 opt_getprog(), buf
, strerror(errno
));
414 OPENSSL_free(ep
->filename
);
419 hash_table
[i
] = NULL
;
423 sk_OPENSSL_STRING_pop_free(files
, str_free
);
428 typedef enum OPTION_choice
{
429 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
430 OPT_COMPAT
, OPT_OLD
, OPT_N
, OPT_VERBOSE
433 const OPTIONS rehash_options
[] = {
434 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [cert-directory...]\n"},
435 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
436 {"help", OPT_HELP
, '-', "Display this summary"},
437 {"h", OPT_HELP
, '-', "Display this summary"},
438 {"compat", OPT_COMPAT
, '-', "Create both new- and old-style hash links"},
439 {"old", OPT_OLD
, '-', "Use old-style hash to generate links"},
440 {"n", OPT_N
, '-', "Do not remove existing links"},
441 {"v", OPT_VERBOSE
, '-', "Verbose output"},
446 int rehash_main(int argc
, char **argv
)
448 const char *env
, *prog
;
452 enum Hash h
= HASH_NEW
;
454 prog
= opt_init(argc
, argv
, rehash_options
);
455 while ((o
= opt_next()) != OPT_EOF
) {
459 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
462 opt_help(rehash_options
);
478 argc
= opt_num_rest();
482 evpmdsize
= EVP_MD_size(evpmd
);
485 while (*argv
!= NULL
)
486 errs
+= do_dir(*argv
++, h
);
487 } else if ((env
= getenv(X509_get_default_cert_dir_env())) != NULL
) {
488 char lsc
[2] = { LIST_SEPARATOR_CHAR
, '\0' };
489 m
= OPENSSL_strdup(env
);
490 for (e
= strtok(m
, lsc
); e
!= NULL
; e
= strtok(NULL
, lsc
))
491 errs
+= do_dir(e
, h
);
494 errs
+= do_dir(X509_get_default_cert_dir(), h
);
502 const OPTIONS rehash_options
[] = {
506 int rehash_main(int argc
, char **argv
)
508 BIO_printf(bio_err
, "Not available; use c_rehash script\n");
512 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */