1 /* crypto/x509/by_dir.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] */
57
58 #include <string.h>
59 #include <sys/stat.h>
60 #include <sys/types.h>
61
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 #include <openssl/thread.h>
66 #include <openssl/x509.h>
67
68 #if !defined(OPENSSL_TRUSTY)
69
70 #include "../internal.h"
71 #include "internal.h"
72
73 typedef struct lookup_dir_hashes_st {
74 unsigned long hash;
75 int suffix;
76 } BY_DIR_HASH;
77
78 typedef struct lookup_dir_entry_st {
79 char *dir;
80 int dir_type;
81 STACK_OF(BY_DIR_HASH) *hashes;
82 } BY_DIR_ENTRY;
83
84 typedef struct lookup_dir_st {
85 BUF_MEM *buffer;
86 STACK_OF(BY_DIR_ENTRY) *dirs;
87 } BY_DIR;
88
89 DEFINE_STACK_OF(BY_DIR_HASH)
90 DEFINE_STACK_OF(BY_DIR_ENTRY)
91
92 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
93 char **ret);
94 static int new_dir(X509_LOOKUP *lu);
95 static void free_dir(X509_LOOKUP *lu);
96 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
97 static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
98 X509_OBJECT *ret);
99 static X509_LOOKUP_METHOD x509_dir_lookup = {
100 "Load certs from files in a directory",
101 new_dir, /* new */
102 free_dir, /* free */
103 NULL, /* init */
104 NULL, /* shutdown */
105 dir_ctrl, /* ctrl */
106 get_cert_by_subject, /* get_by_subject */
107 NULL, /* get_by_issuer_serial */
108 NULL, /* get_by_fingerprint */
109 NULL, /* get_by_alias */
110 };
111
X509_LOOKUP_hash_dir(void)112 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
113 {
114 return (&x509_dir_lookup);
115 }
116
dir_ctrl(X509_LOOKUP * ctx,int cmd,const char * argp,long argl,char ** retp)117 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
118 char **retp)
119 {
120 int ret = 0;
121 BY_DIR *ld;
122 char *dir = NULL;
123
124 ld = (BY_DIR *)ctx->method_data;
125
126 switch (cmd) {
127 case X509_L_ADD_DIR:
128 if (argl == X509_FILETYPE_DEFAULT) {
129 dir = (char *)getenv(X509_get_default_cert_dir_env());
130 if (dir)
131 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
132 else
133 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
134 X509_FILETYPE_PEM);
135 if (!ret) {
136 OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR);
137 }
138 } else
139 ret = add_cert_dir(ld, argp, (int)argl);
140 break;
141 }
142 return (ret);
143 }
144
new_dir(X509_LOOKUP * lu)145 static int new_dir(X509_LOOKUP *lu)
146 {
147 BY_DIR *a;
148
149 if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
150 return (0);
151 if ((a->buffer = BUF_MEM_new()) == NULL) {
152 OPENSSL_free(a);
153 return (0);
154 }
155 a->dirs = NULL;
156 lu->method_data = (char *)a;
157 return (1);
158 }
159
by_dir_hash_free(BY_DIR_HASH * hash)160 static void by_dir_hash_free(BY_DIR_HASH *hash)
161 {
162 OPENSSL_free(hash);
163 }
164
by_dir_hash_cmp(const BY_DIR_HASH ** a,const BY_DIR_HASH ** b)165 static int by_dir_hash_cmp(const BY_DIR_HASH **a, const BY_DIR_HASH **b)
166 {
167 if ((*a)->hash > (*b)->hash)
168 return 1;
169 if ((*a)->hash < (*b)->hash)
170 return -1;
171 return 0;
172 }
173
by_dir_entry_free(BY_DIR_ENTRY * ent)174 static void by_dir_entry_free(BY_DIR_ENTRY *ent)
175 {
176 if (ent->dir)
177 OPENSSL_free(ent->dir);
178 if (ent->hashes)
179 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
180 OPENSSL_free(ent);
181 }
182
free_dir(X509_LOOKUP * lu)183 static void free_dir(X509_LOOKUP *lu)
184 {
185 BY_DIR *a;
186
187 a = (BY_DIR *)lu->method_data;
188 if (a->dirs != NULL)
189 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
190 if (a->buffer != NULL)
191 BUF_MEM_free(a->buffer);
192 OPENSSL_free(a);
193 }
194
add_cert_dir(BY_DIR * ctx,const char * dir,int type)195 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
196 {
197 size_t j, len;
198 const char *s, *ss, *p;
199
200 if (dir == NULL || !*dir) {
201 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY);
202 return 0;
203 }
204
205 s = dir;
206 p = s;
207 do {
208 if ((*p == ':') || (*p == '\0')) {
209 BY_DIR_ENTRY *ent;
210 ss = s;
211 s = p + 1;
212 len = p - ss;
213 if (len == 0)
214 continue;
215 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
216 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
217 if (strlen(ent->dir) == len &&
218 strncmp(ent->dir, ss, len) == 0)
219 break;
220 }
221 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
222 continue;
223 if (ctx->dirs == NULL) {
224 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
225 if (!ctx->dirs) {
226 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
227 return 0;
228 }
229 }
230 ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY));
231 if (!ent)
232 return 0;
233 ent->dir_type = type;
234 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
235 ent->dir = OPENSSL_malloc(len + 1);
236 if (!ent->dir || !ent->hashes) {
237 by_dir_entry_free(ent);
238 return 0;
239 }
240 OPENSSL_strlcpy(ent->dir, ss, len + 1);
241 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
242 by_dir_entry_free(ent);
243 return 0;
244 }
245 }
246 } while (*p++ != '\0');
247 return 1;
248 }
249
250 /*
251 * g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY|
252 * objects.
253 */
254 static struct CRYPTO_STATIC_MUTEX g_ent_hashes_lock =
255 CRYPTO_STATIC_MUTEX_INIT;
256
get_cert_by_subject(X509_LOOKUP * xl,int type,X509_NAME * name,X509_OBJECT * ret)257 static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
258 X509_OBJECT *ret)
259 {
260 BY_DIR *ctx;
261 union {
262 struct {
263 X509 st_x509;
264 X509_CINF st_x509_cinf;
265 } x509;
266 struct {
267 X509_CRL st_crl;
268 X509_CRL_INFO st_crl_info;
269 } crl;
270 } data;
271 int ok = 0;
272 size_t i;
273 int j, k;
274 unsigned long h;
275 unsigned long hash_array[2];
276 int hash_index;
277 BUF_MEM *b = NULL;
278 X509_OBJECT stmp, *tmp;
279 const char *postfix = "";
280
281 if (name == NULL)
282 return (0);
283
284 stmp.type = type;
285 if (type == X509_LU_X509) {
286 data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
287 data.x509.st_x509_cinf.subject = name;
288 stmp.data.x509 = &data.x509.st_x509;
289 postfix = "";
290 } else if (type == X509_LU_CRL) {
291 data.crl.st_crl.crl = &data.crl.st_crl_info;
292 data.crl.st_crl_info.issuer = name;
293 stmp.data.crl = &data.crl.st_crl;
294 postfix = "r";
295 } else {
296 OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE);
297 goto finish;
298 }
299
300 if ((b = BUF_MEM_new()) == NULL) {
301 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
302 goto finish;
303 }
304
305 ctx = (BY_DIR *)xl->method_data;
306
307 hash_array[0] = X509_NAME_hash(name);
308 hash_array[1] = X509_NAME_hash_old(name);
309 for (hash_index = 0; hash_index < 2; ++hash_index) {
310 h = hash_array[hash_index];
311 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
312 BY_DIR_ENTRY *ent;
313 size_t idx;
314 BY_DIR_HASH htmp, *hent;
315 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
316 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
317 if (!BUF_MEM_grow(b, j)) {
318 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
319 goto finish;
320 }
321 if (type == X509_LU_CRL && ent->hashes) {
322 htmp.hash = h;
323 CRYPTO_STATIC_MUTEX_lock_read(&g_ent_hashes_lock);
324 if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) {
325 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
326 k = hent->suffix;
327 } else {
328 hent = NULL;
329 k = 0;
330 }
331 CRYPTO_STATIC_MUTEX_unlock_read(&g_ent_hashes_lock);
332 } else {
333 k = 0;
334 hent = NULL;
335 }
336 for (;;) {
337 char c = '/';
338 #ifdef OPENSSL_SYS_VMS
339 c = ent->dir[strlen(ent->dir) - 1];
340 if (c != ':' && c != '>' && c != ']') {
341 /*
342 * If no separator is present, we assume the directory
343 * specifier is a logical name, and add a colon. We
344 * really should use better VMS routines for merging
345 * things like this, but this will do for now... --
346 * Richard Levitte
347 */
348 c = ':';
349 } else {
350 c = '\0';
351 }
352 #endif
353 if (c == '\0') {
354 /*
355 * This is special. When c == '\0', no directory
356 * separator should be added.
357 */
358 BIO_snprintf(b->data, b->max,
359 "%s%08lx.%s%d", ent->dir, h, postfix, k);
360 } else {
361 BIO_snprintf(b->data, b->max,
362 "%s%c%08lx.%s%d", ent->dir, c, h,
363 postfix, k);
364 }
365 #ifndef OPENSSL_NO_POSIX_IO
366 # if defined(_WIN32) && !defined(stat)
367 # define stat _stat
368 # endif
369 {
370 struct stat st;
371 if (stat(b->data, &st) < 0)
372 break;
373 }
374 #endif
375 /* found one. */
376 if (type == X509_LU_X509) {
377 if ((X509_load_cert_file(xl, b->data,
378 ent->dir_type)) == 0)
379 break;
380 } else if (type == X509_LU_CRL) {
381 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
382 break;
383 }
384 /* else case will caught higher up */
385 k++;
386 }
387
388 /*
389 * we have added it to the cache so now pull it out again
390 */
391 CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock);
392 tmp = NULL;
393 sk_X509_OBJECT_sort(xl->store_ctx->objs);
394 if (sk_X509_OBJECT_find(xl->store_ctx->objs, &idx, &stmp)) {
395 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, idx);
396 }
397 CRYPTO_MUTEX_unlock_write(&xl->store_ctx->objs_lock);
398
399 /*
400 * If a CRL, update the last file suffix added for this
401 */
402
403 if (type == X509_LU_CRL) {
404 CRYPTO_STATIC_MUTEX_lock_write(&g_ent_hashes_lock);
405 /*
406 * Look for entry again in case another thread added an entry
407 * first.
408 */
409 if (!hent) {
410 htmp.hash = h;
411 sk_BY_DIR_HASH_sort(ent->hashes);
412 if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp))
413 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
414 }
415 if (!hent) {
416 hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
417 if (hent == NULL) {
418 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
419 ok = 0;
420 goto finish;
421 }
422 hent->hash = h;
423 hent->suffix = k;
424 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
425 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
426 OPENSSL_free(hent);
427 ok = 0;
428 goto finish;
429 }
430 sk_BY_DIR_HASH_sort(ent->hashes);
431 } else if (hent->suffix < k)
432 hent->suffix = k;
433
434 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
435 }
436
437 if (tmp != NULL) {
438 ok = 1;
439 ret->type = tmp->type;
440 OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data));
441 /*
442 * If we were going to up the reference count, we would need
443 * to do it on a perl 'type' basis
444 */
445 /*
446 * CRYPTO_add(&tmp->data.x509->references,1,
447 * CRYPTO_LOCK_X509);
448 */
449 goto finish;
450 }
451 }
452 }
453 finish:
454 if (b != NULL)
455 BUF_MEM_free(b);
456 return (ok);
457 }
458
459 #endif // OPENSSL_TRUSTY
460