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