]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rehash.c
6f2b5da4ad463e9c600df773bf42d28cbb9fbc31
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 OpenSSL license (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: skipping duplicate %s in %s\n", opt_getprog(),
130 type
== TYPE_CERT
? "certificate" : "CRL", filename
);
133 if (strcmp(filename
, ep
->filename
) == 0) {
141 if (bp
->num_needed
>= MAX_COLLISIONS
) {
143 "%s: hash table overflow for %s\n",
144 opt_getprog(), filename
);
147 ep
= app_malloc(sizeof(*ep
), "collision bucket");
150 ep
->filename
= OPENSSL_strdup(filename
);
152 bp
->last_entry
->next
= ep
;
153 if (bp
->first_entry
== NULL
)
154 bp
->first_entry
= ep
;
158 if (old_id
< ep
->old_id
)
160 if (need_symlink
&& !ep
->need_symlink
) {
161 ep
->need_symlink
= 1;
163 memcpy(ep
->digest
, digest
, evpmdsize
);
169 * Check if a symlink goes to the right spot; return 0 if okay.
170 * This can be -1 if bad filename, or an error count.
172 static int handle_symlink(const char *filename
, const char *fullpath
)
174 unsigned int hash
= 0;
177 char linktarget
[PATH_MAX
], *endptr
;
180 for (i
= 0; i
< 8; i
++) {
185 hash
+= OPENSSL_hexchar2int(ch
);
187 if (filename
[i
++] != '.')
189 for (type
= OSSL_NELEM(suffixes
) - 1; type
> 0; type
--) {
190 const char *suffix
= suffixes
[type
];
191 if (strncasecmp(suffix
, &filename
[i
], strlen(suffix
)) == 0)
194 i
+= strlen(suffixes
[type
]);
196 id
= strtoul(&filename
[i
], &endptr
, 10);
200 n
= readlink(fullpath
, linktarget
, sizeof(linktarget
));
201 if (n
< 0 || n
>= (int)sizeof(linktarget
))
205 return add_entry(type
, hash
, linktarget
, NULL
, 0, id
);
209 * process a file, return number of errors.
211 static int do_file(const char *filename
, const char *fullpath
, enum Hash h
)
213 STACK_OF (X509_INFO
) *inf
= NULL
;
215 X509_NAME
*name
= NULL
;
218 unsigned char digest
[EVP_MAX_MD_SIZE
];
222 /* Does it end with a recognized extension? */
223 if ((ext
= strrchr(filename
, '.')) == NULL
)
225 for (i
= 0; i
< OSSL_NELEM(extensions
); i
++) {
226 if (strcasecmp(extensions
[i
], ext
+ 1) == 0)
229 if (i
>= OSSL_NELEM(extensions
))
232 /* Does it have X.509 data in it? */
233 if ((b
= BIO_new_file(fullpath
, "r")) == NULL
) {
234 BIO_printf(bio_err
, "%s: skipping %s, cannot open file\n",
235 opt_getprog(), filename
);
239 inf
= PEM_X509_INFO_read_bio(b
, NULL
, NULL
, NULL
);
244 if (sk_X509_INFO_num(inf
) != 1) {
247 "it does not contain exactly one certificate or CRL\n",
248 opt_getprog(), filename
);
249 /* This is not an error. */
252 x
= sk_X509_INFO_value(inf
, 0);
253 if (x
->x509
!= NULL
) {
255 name
= X509_get_subject_name(x
->x509
);
256 X509_digest(x
->x509
, evpmd
, digest
, NULL
);
257 } else if (x
->crl
!= NULL
) {
259 name
= X509_CRL_get_issuer(x
->crl
);
260 X509_CRL_digest(x
->crl
, evpmd
, digest
, NULL
);
266 if ((h
== HASH_NEW
) || (h
== HASH_BOTH
))
267 errs
+= add_entry(type
, X509_NAME_hash(name
), filename
, digest
, 1, ~0);
268 if ((h
== HASH_OLD
) || (h
== HASH_BOTH
))
269 errs
+= add_entry(type
, X509_NAME_hash_old(name
), filename
, digest
, 1, ~0);
273 sk_X509_INFO_pop_free(inf
, X509_INFO_free
);
277 static void str_free(char *s
)
282 static int ends_with_dirsep(const char *path
)
285 path
+= strlen(path
) - 1;
287 if (*path
== ']' || *path
== '>' || *path
== ':')
289 # elif defined _WIN32
297 * Process a directory; return number of errors found.
299 static int do_dir(const char *dirname
, enum Hash h
)
303 OPENSSL_DIR_CTX
*d
= NULL
;
305 unsigned char idmask
[MAX_COLLISIONS
/ 8];
306 int n
, numfiles
, nextid
, buflen
, errs
= 0;
309 const char *filename
;
311 STACK_OF(OPENSSL_STRING
) *files
= NULL
;
313 if (app_access(dirname
, W_OK
) < 0) {
314 BIO_printf(bio_err
, "Skipping %s, can't write\n", dirname
);
317 buflen
= strlen(dirname
);
318 pathsep
= (buflen
&& !ends_with_dirsep(dirname
)) ? "/": "";
319 buflen
+= NAME_MAX
+ 1 + 1;
320 buf
= app_malloc(buflen
, "filename buffer");
323 BIO_printf(bio_out
, "Doing %s\n", dirname
);
325 if ((files
= sk_OPENSSL_STRING_new_null()) == NULL
) {
326 BIO_printf(bio_err
, "Skipping %s, out of memory\n", dirname
);
329 while ((filename
= OPENSSL_DIR_read(&d
, dirname
)) != NULL
) {
330 if ((copy
= strdup(filename
)) == NULL
331 || sk_OPENSSL_STRING_push(files
, copy
) == 0) {
332 BIO_puts(bio_err
, "out of memory\n");
337 sk_OPENSSL_STRING_sort(files
);
339 numfiles
= sk_OPENSSL_STRING_num(files
);
340 for (n
= 0; n
< numfiles
; ++n
) {
341 filename
= sk_OPENSSL_STRING_value(files
, n
);
342 if (BIO_snprintf(buf
, buflen
, "%s%s%s",
343 dirname
, pathsep
, filename
) >= buflen
)
345 if (lstat(buf
, &st
) < 0)
347 if (S_ISLNK(st
.st_mode
) && handle_symlink(filename
, buf
) == 0)
349 errs
+= do_file(filename
, buf
, h
);
351 sk_OPENSSL_STRING_pop_free(files
, str_free
);
353 for (i
= 0; i
< OSSL_NELEM(hash_table
); i
++) {
354 for (bp
= hash_table
[i
]; bp
; bp
= nextbp
) {
357 memset(idmask
, 0, (bp
->num_needed
+ 7) / 8);
358 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
)
359 if (ep
->old_id
< bp
->num_needed
)
360 bit_set(idmask
, ep
->old_id
);
362 for (ep
= bp
->first_entry
; ep
; ep
= nextep
) {
364 if (ep
->old_id
< bp
->num_needed
) {
365 /* Link exists, and is used as-is */
366 BIO_snprintf(buf
, buflen
, "%08x.%s%d", bp
->hash
,
367 suffixes
[bp
->type
], ep
->old_id
);
369 BIO_printf(bio_out
, "link %s -> %s\n",
371 } else if (ep
->need_symlink
) {
372 /* New link needed (it may replace something) */
373 while (bit_isset(idmask
, nextid
))
376 BIO_snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
377 dirname
, pathsep
, &n
, bp
->hash
,
378 suffixes
[bp
->type
], nextid
);
380 BIO_printf(bio_out
, "link %s -> %s\n",
381 ep
->filename
, &buf
[n
]);
382 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
384 "%s: Can't unlink %s, %s\n",
385 opt_getprog(), buf
, strerror(errno
));
388 if (symlink(ep
->filename
, buf
) < 0) {
390 "%s: Can't symlink %s, %s\n",
391 opt_getprog(), ep
->filename
,
395 bit_set(idmask
, nextid
);
396 } else if (remove_links
) {
397 /* Link to be deleted */
398 BIO_snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
399 dirname
, pathsep
, &n
, bp
->hash
,
400 suffixes
[bp
->type
], ep
->old_id
);
402 BIO_printf(bio_out
, "unlink %s\n",
404 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
406 "%s: Can't unlink %s, %s\n",
407 opt_getprog(), buf
, strerror(errno
));
411 OPENSSL_free(ep
->filename
);
416 hash_table
[i
] = NULL
;
423 typedef enum OPTION_choice
{
424 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
425 OPT_COMPAT
, OPT_OLD
, OPT_N
, OPT_VERBOSE
428 const OPTIONS rehash_options
[] = {
429 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [cert-directory...]\n"},
430 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
431 {"help", OPT_HELP
, '-', "Display this summary"},
432 {"h", OPT_HELP
, '-', "Display this summary"},
433 {"compat", OPT_COMPAT
, '-', "Create both new- and old-style hash links"},
434 {"old", OPT_OLD
, '-', "Use old-style hash to generate links"},
435 {"n", OPT_N
, '-', "Do not remove existing links"},
436 {"v", OPT_VERBOSE
, '-', "Verbose output"},
441 int rehash_main(int argc
, char **argv
)
443 const char *env
, *prog
;
447 enum Hash h
= HASH_NEW
;
449 prog
= opt_init(argc
, argv
, rehash_options
);
450 while ((o
= opt_next()) != OPT_EOF
) {
454 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
457 opt_help(rehash_options
);
473 argc
= opt_num_rest();
477 evpmdsize
= EVP_MD_size(evpmd
);
480 while (*argv
!= NULL
)
481 errs
+= do_dir(*argv
++, h
);
482 } else if ((env
= getenv("SSL_CERT_DIR")) != NULL
) {
483 m
= OPENSSL_strdup(env
);
484 for (e
= strtok(m
, ":"); e
!= NULL
; e
= strtok(NULL
, ":"))
485 errs
+= do_dir(e
, h
);
488 errs
+= do_dir("/etc/ssl/certs", h
);
496 const OPTIONS rehash_options
[] = {
500 int rehash_main(int argc
, char **argv
)
502 BIO_printf(bio_err
, "Not available; use c_rehash script\n");
506 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */