Remove parentheses of return.
[openssl.git] / crypto / x509 / by_dir.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include "e_os.h"
11 #include "internal/cryptlib.h"
12 #include <stdio.h>
13 #include <time.h>
14 #include <errno.h>
15 #include <sys/types.h>
16
17 #ifndef OPENSSL_NO_POSIX_IO
18 # include <sys/stat.h>
19 #endif
20
21 #include <openssl/x509.h>
22 #include "internal/x509_int.h"
23 #include "x509_lcl.h"
24
25 struct lookup_dir_hashes_st {
26     unsigned long hash;
27     int suffix;
28 };
29
30 struct lookup_dir_entry_st {
31     char *dir;
32     int dir_type;
33     STACK_OF(BY_DIR_HASH) *hashes;
34 };
35
36 typedef struct lookup_dir_st {
37     BUF_MEM *buffer;
38     STACK_OF(BY_DIR_ENTRY) *dirs;
39     CRYPTO_RWLOCK *lock;
40 } BY_DIR;
41
42 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
43                     char **ret);
44 static int new_dir(X509_LOOKUP *lu);
45 static void free_dir(X509_LOOKUP *lu);
46 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
47 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
48                                X509_NAME *name, X509_OBJECT *ret);
49 static X509_LOOKUP_METHOD x509_dir_lookup = {
50     "Load certs from files in a directory",
51     new_dir,                    /* new */
52     free_dir,                   /* free */
53     NULL,                       /* init */
54     NULL,                       /* shutdown */
55     dir_ctrl,                   /* ctrl */
56     get_cert_by_subject,        /* get_by_subject */
57     NULL,                       /* get_by_issuer_serial */
58     NULL,                       /* get_by_fingerprint */
59     NULL,                       /* get_by_alias */
60 };
61
62 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
63 {
64     return &x509_dir_lookup;
65 }
66
67 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
68                     char **retp)
69 {
70     int ret = 0;
71     BY_DIR *ld;
72     char *dir = NULL;
73
74     ld = (BY_DIR *)ctx->method_data;
75
76     switch (cmd) {
77     case X509_L_ADD_DIR:
78         if (argl == X509_FILETYPE_DEFAULT) {
79             dir = (char *)getenv(X509_get_default_cert_dir_env());
80             if (dir)
81                 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
82             else
83                 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
84                                    X509_FILETYPE_PEM);
85             if (!ret) {
86                 X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
87             }
88         } else
89             ret = add_cert_dir(ld, argp, (int)argl);
90         break;
91     }
92     return ret;
93 }
94
95 static int new_dir(X509_LOOKUP *lu)
96 {
97     BY_DIR *a;
98
99     if ((a = OPENSSL_malloc(sizeof(*a))) == NULL)
100         return 0;
101     if ((a->buffer = BUF_MEM_new()) == NULL) {
102         OPENSSL_free(a);
103         return 0;
104     }
105     a->dirs = NULL;
106     a->lock = CRYPTO_THREAD_lock_new();
107     if (a->lock == NULL) {
108         BUF_MEM_free(a->buffer);
109         OPENSSL_free(a);
110         return 0;
111     }
112     lu->method_data = (char *)a;
113     return 1;
114 }
115
116 static void by_dir_hash_free(BY_DIR_HASH *hash)
117 {
118     OPENSSL_free(hash);
119 }
120
121 static int by_dir_hash_cmp(const BY_DIR_HASH *const *a,
122                            const BY_DIR_HASH *const *b)
123 {
124     if ((*a)->hash > (*b)->hash)
125         return 1;
126     if ((*a)->hash < (*b)->hash)
127         return -1;
128     return 0;
129 }
130
131 static void by_dir_entry_free(BY_DIR_ENTRY *ent)
132 {
133     OPENSSL_free(ent->dir);
134     sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
135     OPENSSL_free(ent);
136 }
137
138 static void free_dir(X509_LOOKUP *lu)
139 {
140     BY_DIR *a;
141
142     a = (BY_DIR *)lu->method_data;
143     sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
144     BUF_MEM_free(a->buffer);
145     CRYPTO_THREAD_lock_free(a->lock);
146     OPENSSL_free(a);
147 }
148
149 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
150 {
151     int j;
152     size_t len;
153     const char *s, *ss, *p;
154
155     if (dir == NULL || !*dir) {
156         X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
157         return 0;
158     }
159
160     s = dir;
161     p = s;
162     do {
163         if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
164             BY_DIR_ENTRY *ent;
165             ss = s;
166             s = p + 1;
167             len = p - ss;
168             if (len == 0)
169                 continue;
170             for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
171                 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
172                 if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0)
173                     break;
174             }
175             if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
176                 continue;
177             if (ctx->dirs == NULL) {
178                 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
179                 if (!ctx->dirs) {
180                     X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
181                     return 0;
182                 }
183             }
184             ent = OPENSSL_malloc(sizeof(*ent));
185             if (ent == NULL)
186                 return 0;
187             ent->dir_type = type;
188             ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
189             ent->dir = OPENSSL_strndup(ss, len);
190             if (ent->dir == NULL || ent->hashes == NULL) {
191                 by_dir_entry_free(ent);
192                 return 0;
193             }
194             if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
195                 by_dir_entry_free(ent);
196                 return 0;
197             }
198         }
199     } while (*p++ != '\0');
200     return 1;
201 }
202
203 static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type,
204                                X509_NAME *name, X509_OBJECT *ret)
205 {
206     BY_DIR *ctx;
207     union {
208         X509 st_x509;
209         X509_CRL crl;
210     } data;
211     int ok = 0;
212     int i, j, k;
213     unsigned long h;
214     BUF_MEM *b = NULL;
215     X509_OBJECT stmp, *tmp;
216     const char *postfix = "";
217
218     if (name == NULL)
219         return 0;
220
221     stmp.type = type;
222     if (type == X509_LU_X509) {
223         data.st_x509.cert_info.subject = name;
224         stmp.data.x509 = &data.st_x509;
225         postfix = "";
226     } else if (type == X509_LU_CRL) {
227         data.crl.crl.issuer = name;
228         stmp.data.crl = &data.crl;
229         postfix = "r";
230     } else {
231         X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE);
232         goto finish;
233     }
234
235     if ((b = BUF_MEM_new()) == NULL) {
236         X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB);
237         goto finish;
238     }
239
240     ctx = (BY_DIR *)xl->method_data;
241
242     h = X509_NAME_hash(name);
243     for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
244         BY_DIR_ENTRY *ent;
245         int idx;
246         BY_DIR_HASH htmp, *hent;
247         ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
248         j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
249         if (!BUF_MEM_grow(b, j)) {
250             X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
251             goto finish;
252         }
253         if (type == X509_LU_CRL && ent->hashes) {
254             htmp.hash = h;
255             CRYPTO_THREAD_read_lock(ctx->lock);
256             idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
257             if (idx >= 0) {
258                 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
259                 k = hent->suffix;
260             } else {
261                 hent = NULL;
262                 k = 0;
263             }
264             CRYPTO_THREAD_unlock(ctx->lock);
265         } else {
266             k = 0;
267             hent = NULL;
268         }
269         for (;;) {
270             char c = '/';
271 #ifdef OPENSSL_SYS_VMS
272             c = ent->dir[strlen(ent->dir) - 1];
273             if (c != ':' && c != '>' && c != ']') {
274                 /*
275                  * If no separator is present, we assume the directory
276                  * specifier is a logical name, and add a colon.  We really
277                  * should use better VMS routines for merging things like
278                  * this, but this will do for now... -- Richard Levitte
279                  */
280                 c = ':';
281             } else {
282                 c = '\0';
283             }
284 #endif
285             if (c == '\0') {
286                 /*
287                  * This is special.  When c == '\0', no directory separator
288                  * should be added.
289                  */
290                 BIO_snprintf(b->data, b->max,
291                              "%s%08lx.%s%d", ent->dir, h, postfix, k);
292             } else {
293                 BIO_snprintf(b->data, b->max,
294                              "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k);
295             }
296 #ifndef OPENSSL_NO_POSIX_IO
297 # ifdef _WIN32
298 #  define stat _stat
299 # endif
300             {
301                 struct stat st;
302                 if (stat(b->data, &st) < 0)
303                     break;
304             }
305 #endif
306             /* found one. */
307             if (type == X509_LU_X509) {
308                 if ((X509_load_cert_file(xl, b->data, ent->dir_type)) == 0)
309                     break;
310             } else if (type == X509_LU_CRL) {
311                 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
312                     break;
313             }
314             /* else case will caught higher up */
315             k++;
316         }
317
318         /*
319          * we have added it to the cache so now pull it out again
320          */
321         CRYPTO_THREAD_write_lock(ctx->lock);
322         j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
323         if (j != -1)
324             tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
325         else
326             tmp = NULL;
327         CRYPTO_THREAD_unlock(ctx->lock);
328
329         /* If a CRL, update the last file suffix added for this */
330
331         if (type == X509_LU_CRL) {
332             CRYPTO_THREAD_write_lock(ctx->lock);
333             /*
334              * Look for entry again in case another thread added an entry
335              * first.
336              */
337             if (!hent) {
338                 htmp.hash = h;
339                 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
340                 if (idx >= 0)
341                     hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
342             }
343             if (!hent) {
344                 hent = OPENSSL_malloc(sizeof(*hent));
345                 if (hent == NULL) {
346                     CRYPTO_THREAD_unlock(ctx->lock);
347                     X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
348                     ok = 0;
349                     goto finish;
350                 }
351                 hent->hash = h;
352                 hent->suffix = k;
353                 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
354                     CRYPTO_THREAD_unlock(ctx->lock);
355                     OPENSSL_free(hent);
356                     ok = 0;
357                     goto finish;
358                 }
359             } else if (hent->suffix < k) {
360                 hent->suffix = k;
361             }
362
363             CRYPTO_THREAD_unlock(ctx->lock);
364
365         }
366
367         if (tmp != NULL) {
368             ok = 1;
369             ret->type = tmp->type;
370             memcpy(&ret->data, &tmp->data, sizeof(ret->data));
371
372             /*
373              * Clear any errors that might have been raised processing empty
374              * or malformed files.
375              */
376             ERR_clear_error();
377
378             /*
379              * If we were going to up the reference count, we would need to
380              * do it on a perl 'type' basis
381              */
382         /*- CRYPTO_add(&tmp->data.x509->references,1,
383                     CRYPTO_LOCK_X509);*/
384             goto finish;
385         }
386     }
387  finish:
388     BUF_MEM_free(b);
389     return ok;
390 }