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