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