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