2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include "internal/cryptlib.h"
16 #ifndef NO_SYS_TYPES_H
17 # include <sys/types.h>
19 #ifndef OPENSSL_NO_POSIX_IO
20 # include <sys/stat.h>
24 #include <openssl/lhash.h>
25 #include <openssl/x509.h>
26 #include "internal/x509_int.h"
29 struct lookup_dir_hashes_st
{
34 struct lookup_dir_entry_st
{
37 STACK_OF(BY_DIR_HASH
) *hashes
;
40 typedef struct lookup_dir_st
{
42 STACK_OF(BY_DIR_ENTRY
) *dirs
;
46 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
48 static int new_dir(X509_LOOKUP
*lu
);
49 static void free_dir(X509_LOOKUP
*lu
);
50 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
);
51 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
52 X509_NAME
*name
, X509_OBJECT
*ret
);
53 static X509_LOOKUP_METHOD x509_dir_lookup
= {
54 "Load certs from files in a directory",
60 get_cert_by_subject
, /* get_by_subject */
61 NULL
, /* get_by_issuer_serial */
62 NULL
, /* get_by_fingerprint */
63 NULL
, /* get_by_alias */
66 X509_LOOKUP_METHOD
*X509_LOOKUP_hash_dir(void)
68 return (&x509_dir_lookup
);
71 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
78 ld
= (BY_DIR
*)ctx
->method_data
;
82 if (argl
== X509_FILETYPE_DEFAULT
) {
83 dir
= (char *)getenv(X509_get_default_cert_dir_env());
85 ret
= add_cert_dir(ld
, dir
, X509_FILETYPE_PEM
);
87 ret
= add_cert_dir(ld
, X509_get_default_cert_dir(),
90 X509err(X509_F_DIR_CTRL
, X509_R_LOADING_CERT_DIR
);
93 ret
= add_cert_dir(ld
, argp
, (int)argl
);
99 static int new_dir(X509_LOOKUP
*lu
)
103 if ((a
= OPENSSL_malloc(sizeof(*a
))) == NULL
)
105 if ((a
->buffer
= BUF_MEM_new()) == NULL
) {
110 a
->lock
= CRYPTO_THREAD_lock_new();
111 if (a
->lock
== NULL
) {
112 BUF_MEM_free(a
->buffer
);
116 lu
->method_data
= (char *)a
;
120 static void by_dir_hash_free(BY_DIR_HASH
*hash
)
125 static int by_dir_hash_cmp(const BY_DIR_HASH
*const *a
,
126 const BY_DIR_HASH
*const *b
)
128 if ((*a
)->hash
> (*b
)->hash
)
130 if ((*a
)->hash
< (*b
)->hash
)
135 static void by_dir_entry_free(BY_DIR_ENTRY
*ent
)
137 OPENSSL_free(ent
->dir
);
138 sk_BY_DIR_HASH_pop_free(ent
->hashes
, by_dir_hash_free
);
142 static void free_dir(X509_LOOKUP
*lu
)
146 a
= (BY_DIR
*)lu
->method_data
;
147 sk_BY_DIR_ENTRY_pop_free(a
->dirs
, by_dir_entry_free
);
148 BUF_MEM_free(a
->buffer
);
149 CRYPTO_THREAD_lock_free(a
->lock
);
153 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
)
157 if (dir
== NULL
|| !*dir
) {
158 X509err(X509_F_ADD_CERT_DIR
, X509_R_INVALID_DIRECTORY
);
165 if ((*p
== LIST_SEPARATOR_CHAR
) || (*p
== '\0')) {
174 for (j
= 0; j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); j
++) {
175 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, j
);
176 if (strlen(ent
->dir
) == len
&&
177 strncmp(ent
->dir
, ss
, len
) == 0)
180 if (j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
))
182 if (ctx
->dirs
== NULL
) {
183 ctx
->dirs
= sk_BY_DIR_ENTRY_new_null();
185 X509err(X509_F_ADD_CERT_DIR
, ERR_R_MALLOC_FAILURE
);
189 ent
= OPENSSL_malloc(sizeof(*ent
));
192 ent
->dir_type
= type
;
193 ent
->hashes
= sk_BY_DIR_HASH_new(by_dir_hash_cmp
);
194 ent
->dir
= OPENSSL_strndup(ss
, len
);
195 if (ent
->dir
== NULL
|| ent
->hashes
== NULL
) {
196 by_dir_entry_free(ent
);
199 if (!sk_BY_DIR_ENTRY_push(ctx
->dirs
, ent
)) {
200 by_dir_entry_free(ent
);
204 } while (*p
++ != '\0');
208 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
209 X509_NAME
*name
, X509_OBJECT
*ret
)
220 X509_OBJECT stmp
, *tmp
;
221 const char *postfix
= "";
227 if (type
== X509_LU_X509
) {
228 data
.st_x509
.cert_info
.subject
= name
;
229 stmp
.data
.x509
= &data
.st_x509
;
231 } else if (type
== X509_LU_CRL
) {
232 data
.crl
.crl
.issuer
= name
;
233 stmp
.data
.crl
= &data
.crl
;
236 X509err(X509_F_GET_CERT_BY_SUBJECT
, X509_R_WRONG_LOOKUP_TYPE
);
240 if ((b
= BUF_MEM_new()) == NULL
) {
241 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_BUF_LIB
);
245 ctx
= (BY_DIR
*)xl
->method_data
;
247 h
= X509_NAME_hash(name
);
248 for (i
= 0; i
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); i
++) {
251 BY_DIR_HASH htmp
, *hent
;
252 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, i
);
253 j
= strlen(ent
->dir
) + 1 + 8 + 6 + 1 + 1;
254 if (!BUF_MEM_grow(b
, j
)) {
255 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
258 if (type
== X509_LU_CRL
&& ent
->hashes
) {
260 CRYPTO_THREAD_read_lock(ctx
->lock
);
261 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
263 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
269 CRYPTO_THREAD_unlock(ctx
->lock
);
276 #ifdef OPENSSL_SYS_VMS
277 c
= ent
->dir
[strlen(ent
->dir
) - 1];
278 if (c
!= ':' && c
!= '>' && c
!= ']') {
280 * If no separator is present, we assume the directory
281 * specifier is a logical name, and add a colon. We really
282 * should use better VMS routines for merging things like
283 * this, but this will do for now... -- Richard Levitte
292 * This is special. When c == '\0', no directory separator
295 BIO_snprintf(b
->data
, b
->max
,
296 "%s%08lx.%s%d", ent
->dir
, h
, postfix
, k
);
298 BIO_snprintf(b
->data
, b
->max
,
299 "%s%c%08lx.%s%d", ent
->dir
, c
, h
, postfix
, k
);
301 #ifndef OPENSSL_NO_POSIX_IO
307 if (stat(b
->data
, &st
) < 0)
312 if (type
== X509_LU_X509
) {
313 if ((X509_load_cert_file(xl
, b
->data
, ent
->dir_type
)) == 0)
315 } else if (type
== X509_LU_CRL
) {
316 if ((X509_load_crl_file(xl
, b
->data
, ent
->dir_type
)) == 0)
319 /* else case will caught higher up */
324 * we have added it to the cache so now pull it out again
326 CRYPTO_THREAD_write_lock(ctx
->lock
);
327 j
= sk_X509_OBJECT_find(xl
->store_ctx
->objs
, &stmp
);
329 tmp
= sk_X509_OBJECT_value(xl
->store_ctx
->objs
, j
);
332 CRYPTO_THREAD_unlock(ctx
->lock
);
334 /* If a CRL, update the last file suffix added for this */
336 if (type
== X509_LU_CRL
) {
337 CRYPTO_THREAD_write_lock(ctx
->lock
);
339 * Look for entry again in case another thread added an entry
344 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
346 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
349 hent
= OPENSSL_malloc(sizeof(*hent
));
351 CRYPTO_THREAD_unlock(ctx
->lock
);
352 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
358 if (!sk_BY_DIR_HASH_push(ent
->hashes
, hent
)) {
359 CRYPTO_THREAD_unlock(ctx
->lock
);
364 } else if (hent
->suffix
< k
) {
368 CRYPTO_THREAD_unlock(ctx
->lock
);
374 ret
->type
= tmp
->type
;
375 memcpy(&ret
->data
, &tmp
->data
, sizeof(ret
->data
));
377 * If we were going to up the reference count, we would need to
378 * do it on a perl 'type' basis
380 /*- CRYPTO_add(&tmp->data.x509->references,1,