]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rehash.c
417b91ed087508a937af244fba272482ba3da961
2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * C implementation based on the original Perl and shell versions
13 * Copyright (c) 2013-2014 Timo Teräs <timo.teras@iki.fi>
18 #if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) || \
19 (defined(__VMS) && defined(__DECC) && __CTRL_VER >= 80300000)
26 # include <sys/stat.h>
28 # include "internal/o_dir.h"
29 # include <openssl/evp.h>
30 # include <openssl/pem.h>
31 # include <openssl/x509.h>
35 # define PATH_MAX 4096
40 # define MAX_COLLISIONS 256
42 typedef struct hentry_st
{
43 struct hentry_st
*next
;
45 unsigned short old_id
;
46 unsigned char need_symlink
;
47 unsigned char digest
[EVP_MAX_MD_SIZE
];
50 typedef struct bucket_st
{
51 struct bucket_st
*next
;
52 HENTRY
*first_entry
, *last_entry
;
55 unsigned short num_needed
;
59 /* Keep in sync with |suffixes|, below. */
60 TYPE_CERT
=0, TYPE_CRL
=1
64 HASH_OLD
, HASH_NEW
, HASH_BOTH
69 static const EVP_MD
*evpmd
;
70 static int remove_links
= 1;
71 static int verbose
= 0;
72 static BUCKET
*hash_table
[257];
74 static const char *suffixes
[] = { "", "r" };
75 static const char *extensions
[] = { "pem", "crt", "cer", "crl" };
78 static void bit_set(unsigned char *set
, unsigned int bit
)
80 set
[bit
>> 3] |= 1 << (bit
& 0x7);
83 static int bit_isset(unsigned char *set
, unsigned int bit
)
85 return set
[bit
>> 3] & (1 << (bit
& 0x7));
90 * Process an entry; return number of errors.
92 static int add_entry(enum Type type
, unsigned int hash
, const char *filename
,
93 const unsigned char *digest
, int need_symlink
,
94 unsigned short old_id
)
96 static BUCKET nilbucket
;
97 static HENTRY nilhentry
;
99 HENTRY
*ep
, *found
= NULL
;
100 unsigned int ndx
= (type
+ hash
) % OSSL_NELEM(hash_table
);
102 for (bp
= hash_table
[ndx
]; bp
; bp
= bp
->next
)
103 if (bp
->type
== type
&& bp
->hash
== hash
)
106 bp
= app_malloc(sizeof(*bp
), "hash bucket");
108 bp
->next
= hash_table
[ndx
];
111 hash_table
[ndx
] = bp
;
114 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
) {
115 if (digest
&& memcmp(digest
, ep
->digest
, evpmdsize
) == 0) {
117 "%s: skipping duplicate certificate in %s\n",
118 opt_getprog(), filename
);
121 if (strcmp(filename
, ep
->filename
) == 0) {
129 if (bp
->num_needed
>= MAX_COLLISIONS
) {
131 "%s: hash table overflow for %s\n",
132 opt_getprog(), filename
);
135 ep
= app_malloc(sizeof(*ep
), "collision bucket");
138 ep
->filename
= OPENSSL_strdup(filename
);
140 bp
->last_entry
->next
= ep
;
141 if (bp
->first_entry
== NULL
)
142 bp
->first_entry
= ep
;
146 if (old_id
< ep
->old_id
)
148 if (need_symlink
&& !ep
->need_symlink
) {
149 ep
->need_symlink
= 1;
151 memcpy(ep
->digest
, digest
, evpmdsize
);
157 * Check if a symlink goes to the right spot; return 0 if okay.
158 * This can be -1 if bad filename, or an error count.
160 static int handle_symlink(const char *filename
, const char *fullpath
)
162 unsigned int hash
= 0;
165 char linktarget
[PATH_MAX
], *endptr
;
168 for (i
= 0; i
< 8; i
++) {
173 hash
+= OPENSSL_hexchar2int(ch
);
175 if (filename
[i
++] != '.')
177 for (type
= OSSL_NELEM(suffixes
) - 1; type
> 0; type
--)
178 if (strcasecmp(suffixes
[type
], &filename
[i
]) == 0)
180 i
+= strlen(suffixes
[type
]);
182 id
= strtoul(&filename
[i
], &endptr
, 10);
186 n
= readlink(fullpath
, linktarget
, sizeof(linktarget
));
187 if (n
< 0 || n
>= (int)sizeof(linktarget
))
191 return add_entry(type
, hash
, linktarget
, NULL
, 0, id
);
195 * process a file, return number of errors.
197 static int do_file(const char *filename
, const char *fullpath
, enum Hash h
)
199 STACK_OF (X509_INFO
) *inf
= NULL
;
201 X509_NAME
*name
= NULL
;
204 unsigned char digest
[EVP_MAX_MD_SIZE
];
208 /* Does it end with a recognized extension? */
209 if ((ext
= strrchr(filename
, '.')) == NULL
)
211 for (i
= 0; i
< OSSL_NELEM(extensions
); i
++) {
212 if (strcasecmp(extensions
[i
], ext
+ 1) == 0)
215 if (i
>= OSSL_NELEM(extensions
))
218 /* Does it have X.509 data in it? */
219 if ((b
= BIO_new_file(fullpath
, "r")) == NULL
) {
220 BIO_printf(bio_err
, "%s: skipping %s, cannot open file\n",
221 opt_getprog(), filename
);
225 inf
= PEM_X509_INFO_read_bio(b
, NULL
, NULL
, NULL
);
230 if (sk_X509_INFO_num(inf
) != 1) {
233 "it does not contain exactly one certificate or CRL\n",
234 opt_getprog(), filename
);
235 /* This is not an error. */
238 x
= sk_X509_INFO_value(inf
, 0);
241 name
= X509_get_subject_name(x
->x509
);
242 X509_digest(x
->x509
, evpmd
, digest
, NULL
);
245 name
= X509_CRL_get_issuer(x
->crl
);
246 X509_CRL_digest(x
->crl
, evpmd
, digest
, NULL
);
252 if ((h
== HASH_NEW
) || (h
== HASH_BOTH
))
253 errs
+= add_entry(type
, X509_NAME_hash(name
), filename
, digest
, 1, ~0);
254 if ((h
== HASH_OLD
) || (h
== HASH_BOTH
))
255 errs
+= add_entry(type
, X509_NAME_hash_old(name
), filename
, digest
, 1, ~0);
259 sk_X509_INFO_pop_free(inf
, X509_INFO_free
);
264 * Process a directory; return number of errors found.
266 static int do_dir(const char *dirname
, enum Hash h
)
270 OPENSSL_DIR_CTX
*d
= NULL
;
272 unsigned char idmask
[MAX_COLLISIONS
/ 8];
273 int n
, nextid
, buflen
, errs
= 0;
276 const char *filename
;
279 if (app_access(dirname
, W_OK
) < 0) {
280 BIO_printf(bio_err
, "Skipping %s, can't write\n", dirname
);
283 buflen
= strlen(dirname
);
284 pathsep
= (buflen
&& dirname
[buflen
- 1] == '/') ? "" : "/";
285 buflen
+= NAME_MAX
+ 1 + 1;
286 buf
= app_malloc(buflen
, "filename buffer");
289 BIO_printf(bio_out
, "Doing %s\n", dirname
);
291 while ((filename
= OPENSSL_DIR_read(&d
, dirname
)) != NULL
) {
292 if (snprintf(buf
, buflen
, "%s%s%s",
293 dirname
, pathsep
, filename
) >= buflen
)
295 if (lstat(buf
, &st
) < 0)
297 if (S_ISLNK(st
.st_mode
) && handle_symlink(filename
, buf
) == 0)
299 errs
+= do_file(filename
, buf
, h
);
303 for (i
= 0; i
< OSSL_NELEM(hash_table
); i
++) {
304 for (bp
= hash_table
[i
]; bp
; bp
= nextbp
) {
307 memset(idmask
, 0, (bp
->num_needed
+ 7) / 8);
308 for (ep
= bp
->first_entry
; ep
; ep
= ep
->next
)
309 if (ep
->old_id
< bp
->num_needed
)
310 bit_set(idmask
, ep
->old_id
);
312 for (ep
= bp
->first_entry
; ep
; ep
= nextep
) {
314 if (ep
->old_id
< bp
->num_needed
) {
315 /* Link exists, and is used as-is */
316 snprintf(buf
, buflen
, "%08x.%s%d", bp
->hash
,
317 suffixes
[bp
->type
], ep
->old_id
);
319 BIO_printf(bio_out
, "link %s -> %s\n",
321 } else if (ep
->need_symlink
) {
322 /* New link needed (it may replace something) */
323 while (bit_isset(idmask
, nextid
))
326 snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
327 dirname
, pathsep
, &n
, bp
->hash
,
328 suffixes
[bp
->type
], nextid
);
330 BIO_printf(bio_out
, "link %s -> %s\n",
331 ep
->filename
, &buf
[n
]);
332 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
334 "%s: Can't unlink %s, %s\n",
335 opt_getprog(), buf
, strerror(errno
));
338 if (symlink(ep
->filename
, buf
) < 0) {
340 "%s: Can't symlink %s, %s\n",
341 opt_getprog(), ep
->filename
,
345 } else if (remove_links
) {
346 /* Link to be deleted */
347 snprintf(buf
, buflen
, "%s%s%n%08x.%s%d",
348 dirname
, pathsep
, &n
, bp
->hash
,
349 suffixes
[bp
->type
], ep
->old_id
);
351 BIO_printf(bio_out
, "unlink %s\n",
353 if (unlink(buf
) < 0 && errno
!= ENOENT
) {
355 "%s: Can't unlink %s, %s\n",
356 opt_getprog(), buf
, strerror(errno
));
360 OPENSSL_free(ep
->filename
);
365 hash_table
[i
] = NULL
;
372 typedef enum OPTION_choice
{
373 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
374 OPT_COMPAT
, OPT_OLD
, OPT_N
, OPT_VERBOSE
377 OPTIONS rehash_options
[] = {
378 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [cert-directory...]\n"},
379 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
380 {"help", OPT_HELP
, '-', "Display this summary"},
381 {"compat", OPT_COMPAT
, '-', "Create both new- and old-style hash links"},
382 {"old", OPT_OLD
, '-', "Use old-style hash to generate links"},
383 {"n", OPT_N
, '-', "Do not remove existing links"},
384 {"v", OPT_VERBOSE
, '-', "Verbose output"},
389 int rehash_main(int argc
, char **argv
)
391 const char *env
, *prog
;
395 enum Hash h
= HASH_NEW
;
397 prog
= opt_init(argc
, argv
, rehash_options
);
398 while ((o
= opt_next()) != OPT_EOF
) {
402 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
405 opt_help(rehash_options
);
421 argc
= opt_num_rest();
425 evpmdsize
= EVP_MD_size(evpmd
);
429 errs
+= do_dir(*argv
++, h
);
430 } else if ((env
= getenv("SSL_CERT_DIR")) != NULL
) {
431 m
= OPENSSL_strdup(env
);
432 for (e
= strtok(m
, ":"); e
!= NULL
; e
= strtok(NULL
, ":"))
433 errs
+= do_dir(e
, h
);
436 errs
+= do_dir("/etc/ssl/certs", h
);
444 OPTIONS rehash_options
[] = {
448 int rehash_main(int argc
, char **argv
)
450 BIO_printf(bio_err
, "Not available; use c_rehash script\n");
454 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */