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