2 * Copyright 1995-2020 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
11 #include "internal/cryptlib.h"
15 #include <sys/types.h>
17 #ifndef OPENSSL_NO_POSIX_IO
18 # include <sys/stat.h>
21 #include <openssl/x509.h>
22 #include "crypto/x509.h"
23 #include "x509_local.h"
25 DEFINE_STACK_OF(X509_OBJECT
)
27 struct lookup_dir_hashes_st
{
32 struct lookup_dir_entry_st
{
35 STACK_OF(BY_DIR_HASH
) *hashes
;
38 typedef struct lookup_dir_st
{
40 STACK_OF(BY_DIR_ENTRY
) *dirs
;
44 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
46 static int new_dir(X509_LOOKUP
*lu
);
47 static void free_dir(X509_LOOKUP
*lu
);
48 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
);
49 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
50 const X509_NAME
*name
, X509_OBJECT
*ret
);
51 static X509_LOOKUP_METHOD x509_dir_lookup
= {
52 "Load certs from files in a directory",
53 new_dir
, /* new_item */
58 get_cert_by_subject
, /* get_by_subject */
59 NULL
, /* get_by_issuer_serial */
60 NULL
, /* get_by_fingerprint */
61 NULL
, /* get_by_alias */
64 X509_LOOKUP_METHOD
*X509_LOOKUP_hash_dir(void)
66 return &x509_dir_lookup
;
69 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
73 BY_DIR
*ld
= (BY_DIR
*)ctx
->method_data
;
77 if (argl
== X509_FILETYPE_DEFAULT
) {
78 const char *dir
= ossl_safe_getenv(X509_get_default_cert_dir_env());
81 ret
= add_cert_dir(ld
, dir
, X509_FILETYPE_PEM
);
83 ret
= add_cert_dir(ld
, X509_get_default_cert_dir(),
86 X509err(X509_F_DIR_CTRL
, X509_R_LOADING_CERT_DIR
);
89 ret
= add_cert_dir(ld
, argp
, (int)argl
);
95 static int new_dir(X509_LOOKUP
*lu
)
97 BY_DIR
*a
= OPENSSL_malloc(sizeof(*a
));
100 X509err(X509_F_NEW_DIR
, ERR_R_MALLOC_FAILURE
);
104 if ((a
->buffer
= BUF_MEM_new()) == NULL
) {
105 X509err(X509_F_NEW_DIR
, ERR_R_MALLOC_FAILURE
);
109 a
->lock
= CRYPTO_THREAD_lock_new();
110 if (a
->lock
== NULL
) {
111 BUF_MEM_free(a
->buffer
);
112 X509err(X509_F_NEW_DIR
, ERR_R_MALLOC_FAILURE
);
123 static void by_dir_hash_free(BY_DIR_HASH
*hash
)
128 static int by_dir_hash_cmp(const BY_DIR_HASH
*const *a
,
129 const BY_DIR_HASH
*const *b
)
131 if ((*a
)->hash
> (*b
)->hash
)
133 if ((*a
)->hash
< (*b
)->hash
)
138 static void by_dir_entry_free(BY_DIR_ENTRY
*ent
)
140 OPENSSL_free(ent
->dir
);
141 sk_BY_DIR_HASH_pop_free(ent
->hashes
, by_dir_hash_free
);
145 static void free_dir(X509_LOOKUP
*lu
)
147 BY_DIR
*a
= (BY_DIR
*)lu
->method_data
;
149 sk_BY_DIR_ENTRY_pop_free(a
->dirs
, by_dir_entry_free
);
150 BUF_MEM_free(a
->buffer
);
151 CRYPTO_THREAD_lock_free(a
->lock
);
155 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
)
159 const char *s
, *ss
, *p
;
161 if (dir
== NULL
|| *dir
== '\0') {
162 X509err(X509_F_ADD_CERT_DIR
, X509_R_INVALID_DIRECTORY
);
169 if ((*p
== LIST_SEPARATOR_CHAR
) || (*p
== '\0')) {
177 for (j
= 0; j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); j
++) {
178 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, j
);
179 if (strlen(ent
->dir
) == len
&& strncmp(ent
->dir
, ss
, len
) == 0)
182 if (j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
))
184 if (ctx
->dirs
== NULL
) {
185 ctx
->dirs
= sk_BY_DIR_ENTRY_new_null();
187 X509err(X509_F_ADD_CERT_DIR
, ERR_R_MALLOC_FAILURE
);
191 ent
= OPENSSL_malloc(sizeof(*ent
));
193 X509err(X509_F_ADD_CERT_DIR
, ERR_R_MALLOC_FAILURE
);
196 ent
->dir_type
= type
;
197 ent
->hashes
= sk_BY_DIR_HASH_new(by_dir_hash_cmp
);
198 ent
->dir
= OPENSSL_strndup(ss
, len
);
199 if (ent
->dir
== NULL
|| ent
->hashes
== NULL
) {
200 by_dir_entry_free(ent
);
203 if (!sk_BY_DIR_ENTRY_push(ctx
->dirs
, ent
)) {
204 by_dir_entry_free(ent
);
205 X509err(X509_F_ADD_CERT_DIR
, ERR_R_MALLOC_FAILURE
);
209 } while (*p
++ != '\0');
213 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
214 const X509_NAME
*name
, X509_OBJECT
*ret
)
225 X509_OBJECT stmp
, *tmp
;
226 const char *postfix
= "";
232 if (type
== X509_LU_X509
) {
233 data
.st_x509
.cert_info
.subject
= (X509_NAME
*)name
; /* won't modify it */
234 stmp
.data
.x509
= &data
.st_x509
;
236 } else if (type
== X509_LU_CRL
) {
237 data
.crl
.crl
.issuer
= (X509_NAME
*)name
; /* won't modify it */
238 stmp
.data
.crl
= &data
.crl
;
241 X509err(X509_F_GET_CERT_BY_SUBJECT
, X509_R_WRONG_LOOKUP_TYPE
);
245 if ((b
= BUF_MEM_new()) == NULL
) {
246 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_BUF_LIB
);
250 ctx
= (BY_DIR
*)xl
->method_data
;
252 h
= X509_NAME_hash(name
);
253 for (i
= 0; i
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); i
++) {
256 BY_DIR_HASH htmp
, *hent
;
258 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, i
);
259 j
= strlen(ent
->dir
) + 1 + 8 + 6 + 1 + 1;
260 if (!BUF_MEM_grow(b
, j
)) {
261 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
264 if (type
== X509_LU_CRL
&& ent
->hashes
) {
266 CRYPTO_THREAD_read_lock(ctx
->lock
);
267 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
269 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
275 CRYPTO_THREAD_unlock(ctx
->lock
);
282 #ifdef OPENSSL_SYS_VMS
283 c
= ent
->dir
[strlen(ent
->dir
) - 1];
284 if (c
!= ':' && c
!= '>' && c
!= ']') {
286 * If no separator is present, we assume the directory
287 * specifier is a logical name, and add a colon. We really
288 * should use better VMS routines for merging things like
289 * this, but this will do for now... -- Richard Levitte
298 * This is special. When c == '\0', no directory separator
301 BIO_snprintf(b
->data
, b
->max
,
302 "%s%08lx.%s%d", ent
->dir
, h
, postfix
, k
);
304 BIO_snprintf(b
->data
, b
->max
,
305 "%s%c%08lx.%s%d", ent
->dir
, c
, h
, postfix
, k
);
307 #ifndef OPENSSL_NO_POSIX_IO
313 if (stat(b
->data
, &st
) < 0)
318 if (type
== X509_LU_X509
) {
319 if ((X509_load_cert_file(xl
, b
->data
, ent
->dir_type
)) == 0)
321 } else if (type
== X509_LU_CRL
) {
322 if ((X509_load_crl_file(xl
, b
->data
, ent
->dir_type
)) == 0)
325 /* else case will caught higher up */
330 * we have added it to the cache so now pull it out again
332 X509_STORE_lock(xl
->store_ctx
);
333 j
= sk_X509_OBJECT_find(xl
->store_ctx
->objs
, &stmp
);
334 tmp
= sk_X509_OBJECT_value(xl
->store_ctx
->objs
, j
);
335 X509_STORE_unlock(xl
->store_ctx
);
337 /* If a CRL, update the last file suffix added for this */
339 if (type
== X509_LU_CRL
) {
340 CRYPTO_THREAD_write_lock(ctx
->lock
);
342 * Look for entry again in case another thread added an entry
347 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
348 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
351 hent
= OPENSSL_malloc(sizeof(*hent
));
353 CRYPTO_THREAD_unlock(ctx
->lock
);
354 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
360 if (!sk_BY_DIR_HASH_push(ent
->hashes
, hent
)) {
361 CRYPTO_THREAD_unlock(ctx
->lock
);
363 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
367 } else if (hent
->suffix
< k
) {
371 CRYPTO_THREAD_unlock(ctx
->lock
);
377 ret
->type
= tmp
->type
;
378 memcpy(&ret
->data
, &tmp
->data
, sizeof(ret
->data
));
381 * Clear any errors that might have been raised processing empty
382 * or malformed files.