2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #if defined (__TANDEM) && defined (_SPT_MODEL_)
12 * These definitions have to come first in SPT due to scoping of the
13 * declarations in c99 associated with SPT use of stat.
15 # include <sys/types.h>
16 # include <sys/stat.h>
19 #include "internal/e_os.h"
20 #include "internal/cryptlib.h"
24 #include <sys/types.h>
26 #ifndef OPENSSL_NO_POSIX_IO
27 # include <sys/stat.h>
30 #include <openssl/x509.h>
31 #include "crypto/x509.h"
32 #include "x509_local.h"
34 struct lookup_dir_hashes_st
{
39 struct lookup_dir_entry_st
{
42 STACK_OF(BY_DIR_HASH
) *hashes
;
45 typedef struct lookup_dir_st
{
47 STACK_OF(BY_DIR_ENTRY
) *dirs
;
51 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
54 static int new_dir(X509_LOOKUP
*lu
);
55 static void free_dir(X509_LOOKUP
*lu
);
56 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
);
57 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
58 const X509_NAME
*name
, X509_OBJECT
*ret
);
59 static int get_cert_by_subject_ex(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
60 const X509_NAME
*name
, X509_OBJECT
*ret
,
61 OSSL_LIB_CTX
*libctx
, const char *propq
);
62 static X509_LOOKUP_METHOD x509_dir_lookup
= {
63 "Load certs from files in a directory",
64 new_dir
, /* new_item */
69 get_cert_by_subject
, /* get_by_subject */
70 NULL
, /* get_by_issuer_serial */
71 NULL
, /* get_by_fingerprint */
72 NULL
, /* get_by_alias */
73 get_cert_by_subject_ex
, /* get_by_subject_ex */
77 X509_LOOKUP_METHOD
*X509_LOOKUP_hash_dir(void)
79 return &x509_dir_lookup
;
82 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
86 BY_DIR
*ld
= (BY_DIR
*)ctx
->method_data
;
90 if (argl
== X509_FILETYPE_DEFAULT
) {
91 const char *dir
= ossl_safe_getenv(X509_get_default_cert_dir_env());
94 ret
= add_cert_dir(ld
, dir
, X509_FILETYPE_PEM
);
96 ret
= add_cert_dir(ld
, X509_get_default_cert_dir(),
99 ERR_raise(ERR_LIB_X509
, X509_R_LOADING_CERT_DIR
);
102 ret
= add_cert_dir(ld
, argp
, (int)argl
);
108 static int new_dir(X509_LOOKUP
*lu
)
110 BY_DIR
*a
= OPENSSL_malloc(sizeof(*a
));
115 if ((a
->buffer
= BUF_MEM_new()) == NULL
) {
116 ERR_raise(ERR_LIB_X509
, ERR_R_BN_LIB
);
120 a
->lock
= CRYPTO_THREAD_lock_new();
121 if (a
->lock
== NULL
) {
122 BUF_MEM_free(a
->buffer
);
123 ERR_raise(ERR_LIB_X509
, ERR_R_CRYPTO_LIB
);
134 static void by_dir_hash_free(BY_DIR_HASH
*hash
)
139 static int by_dir_hash_cmp(const BY_DIR_HASH
*const *a
,
140 const BY_DIR_HASH
*const *b
)
142 if ((*a
)->hash
> (*b
)->hash
)
144 if ((*a
)->hash
< (*b
)->hash
)
149 static void by_dir_entry_free(BY_DIR_ENTRY
*ent
)
151 OPENSSL_free(ent
->dir
);
152 sk_BY_DIR_HASH_pop_free(ent
->hashes
, by_dir_hash_free
);
156 static void free_dir(X509_LOOKUP
*lu
)
158 BY_DIR
*a
= (BY_DIR
*)lu
->method_data
;
160 sk_BY_DIR_ENTRY_pop_free(a
->dirs
, by_dir_entry_free
);
161 BUF_MEM_free(a
->buffer
);
162 CRYPTO_THREAD_lock_free(a
->lock
);
166 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
)
170 const char *s
, *ss
, *p
;
172 if (dir
== NULL
|| *dir
== '\0') {
173 ERR_raise(ERR_LIB_X509
, X509_R_INVALID_DIRECTORY
);
180 if ((*p
== LIST_SEPARATOR_CHAR
) || (*p
== '\0')) {
188 for (j
= 0; j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); j
++) {
189 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, j
);
190 if (strlen(ent
->dir
) == len
&& strncmp(ent
->dir
, ss
, len
) == 0)
193 if (j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
))
195 if (ctx
->dirs
== NULL
) {
196 ctx
->dirs
= sk_BY_DIR_ENTRY_new_null();
198 ERR_raise(ERR_LIB_X509
, ERR_R_CRYPTO_LIB
);
202 ent
= OPENSSL_malloc(sizeof(*ent
));
205 ent
->dir_type
= type
;
206 ent
->hashes
= sk_BY_DIR_HASH_new(by_dir_hash_cmp
);
207 ent
->dir
= OPENSSL_strndup(ss
, len
);
208 if (ent
->dir
== NULL
|| ent
->hashes
== NULL
) {
209 by_dir_entry_free(ent
);
212 if (!sk_BY_DIR_ENTRY_push(ctx
->dirs
, ent
)) {
213 by_dir_entry_free(ent
);
214 ERR_raise(ERR_LIB_X509
, ERR_R_CRYPTO_LIB
);
218 } while (*p
++ != '\0');
222 static int get_cert_by_subject_ex(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
223 const X509_NAME
*name
, X509_OBJECT
*ret
,
224 OSSL_LIB_CTX
*libctx
, const char *propq
)
235 X509_OBJECT stmp
, *tmp
;
236 const char *postfix
= "";
242 if (type
== X509_LU_X509
) {
243 data
.st_x509
.cert_info
.subject
= (X509_NAME
*)name
; /* won't modify it */
244 stmp
.data
.x509
= &data
.st_x509
;
245 } else if (type
== X509_LU_CRL
) {
246 data
.crl
.crl
.issuer
= (X509_NAME
*)name
; /* won't modify it */
247 stmp
.data
.crl
= &data
.crl
;
250 ERR_raise(ERR_LIB_X509
, X509_R_WRONG_LOOKUP_TYPE
);
254 if ((b
= BUF_MEM_new()) == NULL
) {
255 ERR_raise(ERR_LIB_X509
, ERR_R_BUF_LIB
);
259 ctx
= (BY_DIR
*)xl
->method_data
;
260 h
= X509_NAME_hash_ex(name
, libctx
, propq
, &i
);
263 for (i
= 0; i
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); i
++) {
266 BY_DIR_HASH htmp
, *hent
;
268 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, i
);
269 j
= strlen(ent
->dir
) + 1 + 8 + 6 + 1 + 1;
270 if (!BUF_MEM_grow(b
, j
)) {
271 ERR_raise(ERR_LIB_X509
, ERR_R_BUF_LIB
);
274 if (type
== X509_LU_CRL
&& ent
->hashes
) {
276 if (!CRYPTO_THREAD_read_lock(ctx
->lock
))
278 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
280 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
286 CRYPTO_THREAD_unlock(ctx
->lock
);
294 #ifdef OPENSSL_SYS_VMS
295 c
= ent
->dir
[strlen(ent
->dir
) - 1];
296 if (c
!= ':' && c
!= '>' && c
!= ']') {
298 * If no separator is present, we assume the directory
299 * specifier is a logical name, and add a colon. We really
300 * should use better VMS routines for merging things like
301 * this, but this will do for now... -- Richard Levitte
310 * This is special. When c == '\0', no directory separator
313 BIO_snprintf(b
->data
, b
->max
,
314 "%s%08lx.%s%d", ent
->dir
, h
, postfix
, k
);
318 BIO_snprintf(b
->data
, b
->max
,
319 "%s%c%08lx.%s%d", ent
->dir
, c
, h
, postfix
, k
);
321 #ifndef OPENSSL_NO_POSIX_IO
327 if (stat(b
->data
, &st
) < 0)
332 if (type
== X509_LU_X509
) {
333 if ((X509_load_cert_file_ex(xl
, b
->data
, ent
->dir_type
, libctx
,
336 } else if (type
== X509_LU_CRL
) {
337 if ((X509_load_crl_file(xl
, b
->data
, ent
->dir_type
)) == 0)
340 /* else case will caught higher up */
345 * we have added it to the cache so now pull it out again
347 * Note: quadratic time find here since the objects won't generally be
348 * sorted and sorting the would result in O(n^2 log n) complexity.
351 if (!X509_STORE_lock(xl
->store_ctx
))
353 j
= sk_X509_OBJECT_find(xl
->store_ctx
->objs
, &stmp
);
354 tmp
= sk_X509_OBJECT_value(xl
->store_ctx
->objs
, j
);
355 X509_STORE_unlock(xl
->store_ctx
);
360 * If a CRL, update the last file suffix added for this.
361 * We don't need to add an entry if k is 0 as this is the initial value.
362 * This avoids the need for a write lock and sort operation in the
363 * simple case where no CRL is present for a hash.
365 if (type
== X509_LU_CRL
&& k
> 0) {
366 if (!CRYPTO_THREAD_write_lock(ctx
->lock
))
369 * Look for entry again in case another thread added an entry
374 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
375 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
378 hent
= OPENSSL_malloc(sizeof(*hent
));
380 CRYPTO_THREAD_unlock(ctx
->lock
);
386 if (!sk_BY_DIR_HASH_push(ent
->hashes
, hent
)) {
387 CRYPTO_THREAD_unlock(ctx
->lock
);
389 ERR_raise(ERR_LIB_X509
, ERR_R_CRYPTO_LIB
);
395 * Ensure stack is sorted so that subsequent sk_BY_DIR_HASH_find
396 * will not mutate the stack and therefore require a write lock.
398 sk_BY_DIR_HASH_sort(ent
->hashes
);
399 } else if (hent
->suffix
< k
) {
403 CRYPTO_THREAD_unlock(ctx
->lock
);
409 ret
->type
= tmp
->type
;
410 memcpy(&ret
->data
, &tmp
->data
, sizeof(ret
->data
));
413 * Clear any errors that might have been raised processing empty
414 * or malformed files.
422 /* If we changed anything, resort the objects for faster lookup */
423 if (X509_STORE_lock(xl
->store_ctx
)) {
424 if (!sk_X509_OBJECT_is_sorted(xl
->store_ctx
->objs
)) {
425 sk_X509_OBJECT_sort(xl
->store_ctx
->objs
);
427 X509_STORE_unlock(xl
->store_ctx
);
434 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
435 const X509_NAME
*name
, X509_OBJECT
*ret
)
437 return get_cert_by_subject_ex(xl
, type
, name
, ret
, NULL
, NULL
);