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
72typedef struct lookup_dir_hashes_st {
73 unsigned long hash;
74 int suffix;
75} BY_DIR_HASH;
76
77typedef struct lookup_dir_entry_st {
78 char *dir;
79 int dir_type;
80 STACK_OF(BY_DIR_HASH) *hashes;
81} BY_DIR_ENTRY;
82
83typedef struct lookup_dir_st {
84 BUF_MEM *buffer;
85 STACK_OF(BY_DIR_ENTRY) *dirs;
86} BY_DIR;
87
88DEFINE_STACK_OF(BY_DIR_HASH)
89DEFINE_STACK_OF(BY_DIR_ENTRY)
90
91static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
92 char **ret);
93static int new_dir(X509_LOOKUP *lu);
94static void free_dir(X509_LOOKUP *lu);
95static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
96static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
97 X509_OBJECT *ret);
98static X509_LOOKUP_METHOD x509_dir_lookup = {
99 "Load certs from files in a directory",
100 new_dir, /* new */
101 free_dir, /* free */
102 NULL, /* init */
103 NULL, /* shutdown */
104 dir_ctrl, /* ctrl */
105 get_cert_by_subject, /* get_by_subject */
106 NULL, /* get_by_issuer_serial */
107 NULL, /* get_by_fingerprint */
108 NULL, /* get_by_alias */
109};
110
111X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
112{
113 return (&x509_dir_lookup);
114}
115
116static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
117 char **retp)
118{
119 int ret = 0;
120 BY_DIR *ld;
121 char *dir = NULL;
122
123 ld = (BY_DIR *)ctx->method_data;
124
125 switch (cmd) {
126 case X509_L_ADD_DIR:
127 if (argl == X509_FILETYPE_DEFAULT) {
128 dir = (char *)getenv(X509_get_default_cert_dir_env());
129 if (dir)
130 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
131 else
132 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
133 X509_FILETYPE_PEM);
134 if (!ret) {
135 OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR);
136 }
137 } else
138 ret = add_cert_dir(ld, argp, (int)argl);
139 break;
140 }
141 return (ret);
142}
143
144static int new_dir(X509_LOOKUP *lu)
145{
146 BY_DIR *a;
147
148 if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
149 return (0);
150 if ((a->buffer = BUF_MEM_new()) == NULL) {
151 OPENSSL_free(a);
152 return (0);
153 }
154 a->dirs = NULL;
155 lu->method_data = (char *)a;
156 return (1);
157}
158
159static void by_dir_hash_free(BY_DIR_HASH *hash)
160{
161 OPENSSL_free(hash);
162}
163
164static int by_dir_hash_cmp(const BY_DIR_HASH **a, const BY_DIR_HASH **b)
165{
166 if ((*a)->hash > (*b)->hash)
167 return 1;
168 if ((*a)->hash < (*b)->hash)
169 return -1;
170 return 0;
171}
172
173static void by_dir_entry_free(BY_DIR_ENTRY *ent)
174{
175 if (ent->dir)
176 OPENSSL_free(ent->dir);
177 if (ent->hashes)
178 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
179 OPENSSL_free(ent);
180}
181
182static void free_dir(X509_LOOKUP *lu)
183{
184 BY_DIR *a;
185
186 a = (BY_DIR *)lu->method_data;
187 if (a->dirs != NULL)
188 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
189 if (a->buffer != NULL)
190 BUF_MEM_free(a->buffer);
191 OPENSSL_free(a);
192}
193
194static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
195{
196 size_t j, len;
197 const char *s, *ss, *p;
198
199 if (dir == NULL || !*dir) {
200 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY);
201 return 0;
202 }
203
204 s = dir;
205 p = s;
206 do {
207 if ((*p == ':') || (*p == '\0')) {
208 BY_DIR_ENTRY *ent;
209 ss = s;
210 s = p + 1;
211 len = p - ss;
212 if (len == 0)
213 continue;
214 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
215 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
216 if (strlen(ent->dir) == len &&
217 strncmp(ent->dir, ss, len) == 0)
218 break;
219 }
220 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
221 continue;
222 if (ctx->dirs == NULL) {
223 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
224 if (!ctx->dirs) {
225 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
226 return 0;
227 }
228 }
229 ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY));
230 if (!ent)
231 return 0;
232 ent->dir_type = type;
233 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
234 ent->dir = OPENSSL_malloc(len + 1);
235 if (!ent->dir || !ent->hashes) {
236 by_dir_entry_free(ent);
237 return 0;
238 }
239 BUF_strlcpy(ent->dir, ss, len + 1);
240 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
241 by_dir_entry_free(ent);
242 return 0;
243 }
244 }
245 } while (*p++ != '\0');
246 return 1;
247}
248
249/*
250 * g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY|
251 * objects.
252 */
253static struct CRYPTO_STATIC_MUTEX g_ent_hashes_lock =
254 CRYPTO_STATIC_MUTEX_INIT;
255
256static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
257 X509_OBJECT *ret)
258{
259 BY_DIR *ctx;
260 union {
261 struct {
262 X509 st_x509;
263 X509_CINF st_x509_cinf;
264 } x509;
265 struct {
266 X509_CRL st_crl;
267 X509_CRL_INFO st_crl_info;
268 } crl;
269 } data;
270 int ok = 0;
271 size_t i;
272 int j, k;
273 unsigned long h;
274 unsigned long hash_array[2];
275 int hash_index;
276 BUF_MEM *b = NULL;
277 X509_OBJECT stmp, *tmp;
278 const char *postfix = "";
279
280 if (name == NULL)
281 return (0);
282
283 stmp.type = type;
284 if (type == X509_LU_X509) {
285 data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
286 data.x509.st_x509_cinf.subject = name;
287 stmp.data.x509 = &data.x509.st_x509;
288 postfix = "";
289 } else if (type == X509_LU_CRL) {
290 data.crl.st_crl.crl = &data.crl.st_crl_info;
291 data.crl.st_crl_info.issuer = name;
292 stmp.data.crl = &data.crl.st_crl;
293 postfix = "r";
294 } else {
295 OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE);
296 goto finish;
297 }
298
299 if ((b = BUF_MEM_new()) == NULL) {
300 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
301 goto finish;
302 }
303
304 ctx = (BY_DIR *)xl->method_data;
305
306 hash_array[0] = X509_NAME_hash(name);
307 hash_array[1] = X509_NAME_hash_old(name);
308 for (hash_index = 0; hash_index < 2; ++hash_index) {
309 h = hash_array[hash_index];
310 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
311 BY_DIR_ENTRY *ent;
312 size_t idx;
313 BY_DIR_HASH htmp, *hent;
314 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
315 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
316 if (!BUF_MEM_grow(b, j)) {
317 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
318 goto finish;
319 }
320 if (type == X509_LU_CRL && ent->hashes) {
321 htmp.hash = h;
322 CRYPTO_STATIC_MUTEX_lock_read(&g_ent_hashes_lock);
323 if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) {
324 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
325 k = hent->suffix;
326 } else {
327 hent = NULL;
328 k = 0;
329 }
330 CRYPTO_STATIC_MUTEX_unlock_read(&g_ent_hashes_lock);
331 } else {
332 k = 0;
333 hent = NULL;
334 }
335 for (;;) {
336 char c = '/';
337#ifdef OPENSSL_SYS_VMS
338 c = ent->dir[strlen(ent->dir) - 1];
339 if (c != ':' && c != '>' && c != ']') {
340 /*
341 * If no separator is present, we assume the directory
342 * specifier is a logical name, and add a colon. We
343 * really should use better VMS routines for merging
344 * things like this, but this will do for now... --
345 * Richard Levitte
346 */
347 c = ':';
348 } else {
349 c = '\0';
350 }
351#endif
352 if (c == '\0') {
353 /*
354 * This is special. When c == '\0', no directory
355 * separator should be added.
356 */
357 BIO_snprintf(b->data, b->max,
358 "%s%08lx.%s%d", ent->dir, h, postfix, k);
359 } else {
360 BIO_snprintf(b->data, b->max,
361 "%s%c%08lx.%s%d", ent->dir, c, h,
362 postfix, k);
363 }
364#ifndef OPENSSL_NO_POSIX_IO
365# if defined(_WIN32) && !defined(stat)
366# define stat _stat
367# endif
368 {
369 struct stat st;
370 if (stat(b->data, &st) < 0)
371 break;
372 }
373#endif
374 /* found one. */
375 if (type == X509_LU_X509) {
376 if ((X509_load_cert_file(xl, b->data,
377 ent->dir_type)) == 0)
378 break;
379 } else if (type == X509_LU_CRL) {
380 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
381 break;
382 }
383 /* else case will caught higher up */
384 k++;
385 }
386
387 /*
388 * we have added it to the cache so now pull it out again
389 */
390 CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock);
391 tmp = NULL;
392 sk_X509_OBJECT_sort(xl->store_ctx->objs);
393 if (sk_X509_OBJECT_find(xl->store_ctx->objs, &idx, &stmp)) {
394 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, idx);
395 }
396 CRYPTO_MUTEX_unlock_write(&xl->store_ctx->objs_lock);
397
398 /*
399 * If a CRL, update the last file suffix added for this
400 */
401
402 if (type == X509_LU_CRL) {
403 CRYPTO_STATIC_MUTEX_lock_write(&g_ent_hashes_lock);
404 /*
405 * Look for entry again in case another thread added an entry
406 * first.
407 */
408 if (!hent) {
409 htmp.hash = h;
410 sk_BY_DIR_HASH_sort(ent->hashes);
411 if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp))
412 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
413 }
414 if (!hent) {
415 hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
416 if (hent == NULL) {
417 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
418 ok = 0;
419 goto finish;
420 }
421 hent->hash = h;
422 hent->suffix = k;
423 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
424 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
425 OPENSSL_free(hent);
426 ok = 0;
427 goto finish;
428 }
429 sk_BY_DIR_HASH_sort(ent->hashes);
430 } else if (hent->suffix < k)
431 hent->suffix = k;
432
433 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
434 }
435
436 if (tmp != NULL) {
437 ok = 1;
438 ret->type = tmp->type;
439 OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data));
440 /*
441 * If we were going to up the reference count, we would need
442 * to do it on a perl 'type' basis
443 */
444 /*
445 * CRYPTO_add(&tmp->data.x509->references,1,
446 * CRYPTO_LOCK_X509);
447 */
448 goto finish;
449 }
450 }
451 }
452 finish:
453 if (b != NULL)
454 BUF_MEM_free(b);
455 return (ok);
456}
457
458#endif // OPENSSL_TRUSTY
459