2 * Copyright 1995-2018 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 struct lookup_dir_hashes_st
{
30 struct lookup_dir_entry_st
{
33 STACK_OF(BY_DIR_HASH
) *hashes
;
36 typedef struct lookup_dir_st
{
38 STACK_OF(BY_DIR_ENTRY
) *dirs
;
42 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
44 static int new_dir(X509_LOOKUP
*lu
);
45 static void free_dir(X509_LOOKUP
*lu
);
46 static int add_cert_dir(BY_DIR
*ctx
, const char *dir
, int type
);
47 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
48 X509_NAME
*name
, X509_OBJECT
*ret
);
49 static X509_LOOKUP_METHOD x509_dir_lookup
= {
50 "Load certs from files in a directory",
51 new_dir
, /* new_item */
56 get_cert_by_subject
, /* get_by_subject */
57 NULL
, /* get_by_issuer_serial */
58 NULL
, /* get_by_fingerprint */
59 NULL
, /* get_by_alias */
62 X509_LOOKUP_METHOD
*X509_LOOKUP_hash_dir(void)
64 return &x509_dir_lookup
;
67 static int dir_ctrl(X509_LOOKUP
*ctx
, int cmd
, const char *argp
, long argl
,
71 BY_DIR
*ld
= (BY_DIR
*)ctx
->method_data
;
75 if (argl
== X509_FILETYPE_DEFAULT
) {
76 const char *dir
= ossl_safe_getenv(X509_get_default_cert_dir_env());
79 ret
= add_cert_dir(ld
, dir
, X509_FILETYPE_PEM
);
81 ret
= add_cert_dir(ld
, X509_get_default_cert_dir(),
84 X509err(X509_F_DIR_CTRL
, X509_R_LOADING_CERT_DIR
);
87 ret
= add_cert_dir(ld
, argp
, (int)argl
);
93 static int new_dir(X509_LOOKUP
*lu
)
95 BY_DIR
*a
= OPENSSL_malloc(sizeof(*a
));
98 X509err(X509_F_NEW_DIR
, ERR_R_MALLOC_FAILURE
);
102 if ((a
->buffer
= BUF_MEM_new()) == NULL
) {
103 X509err(X509_F_NEW_DIR
, ERR_R_MALLOC_FAILURE
);
107 a
->lock
= CRYPTO_THREAD_lock_new();
108 if (a
->lock
== NULL
) {
109 BUF_MEM_free(a
->buffer
);
110 X509err(X509_F_NEW_DIR
, ERR_R_MALLOC_FAILURE
);
121 static void by_dir_hash_free(BY_DIR_HASH
*hash
)
126 static int by_dir_hash_cmp(const BY_DIR_HASH
*const *a
,
127 const BY_DIR_HASH
*const *b
)
129 if ((*a
)->hash
> (*b
)->hash
)
131 if ((*a
)->hash
< (*b
)->hash
)
136 static void by_dir_entry_free(BY_DIR_ENTRY
*ent
)
138 OPENSSL_free(ent
->dir
);
139 sk_BY_DIR_HASH_pop_free(ent
->hashes
, by_dir_hash_free
);
143 static void free_dir(X509_LOOKUP
*lu
)
145 BY_DIR
*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 const char *s
, *ss
, *p
;
159 if (dir
== NULL
|| !*dir
) {
160 X509err(X509_F_ADD_CERT_DIR
, X509_R_INVALID_DIRECTORY
);
167 if ((*p
== LIST_SEPARATOR_CHAR
) || (*p
== '\0')) {
175 for (j
= 0; j
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); j
++) {
176 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, j
);
177 if (strlen(ent
->dir
) == len
&& 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
));
191 X509err(X509_F_ADD_CERT_DIR
, ERR_R_MALLOC_FAILURE
);
194 ent
->dir_type
= type
;
195 ent
->hashes
= sk_BY_DIR_HASH_new(by_dir_hash_cmp
);
196 ent
->dir
= OPENSSL_strndup(ss
, len
);
197 if (ent
->dir
== NULL
|| ent
->hashes
== NULL
) {
198 by_dir_entry_free(ent
);
201 if (!sk_BY_DIR_ENTRY_push(ctx
->dirs
, ent
)) {
202 by_dir_entry_free(ent
);
203 X509err(X509_F_ADD_CERT_DIR
, ERR_R_MALLOC_FAILURE
);
207 } while (*p
++ != '\0');
211 static int get_cert_by_subject(X509_LOOKUP
*xl
, X509_LOOKUP_TYPE type
,
212 X509_NAME
*name
, X509_OBJECT
*ret
)
223 X509_OBJECT stmp
, *tmp
;
224 const char *postfix
= "";
230 if (type
== X509_LU_X509
) {
231 data
.st_x509
.cert_info
.subject
= name
;
232 stmp
.data
.x509
= &data
.st_x509
;
234 } else if (type
== X509_LU_CRL
) {
235 data
.crl
.crl
.issuer
= name
;
236 stmp
.data
.crl
= &data
.crl
;
239 X509err(X509_F_GET_CERT_BY_SUBJECT
, X509_R_WRONG_LOOKUP_TYPE
);
243 if ((b
= BUF_MEM_new()) == NULL
) {
244 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_BUF_LIB
);
248 ctx
= (BY_DIR
*)xl
->method_data
;
250 h
= X509_NAME_hash(name
);
251 for (i
= 0; i
< sk_BY_DIR_ENTRY_num(ctx
->dirs
); i
++) {
254 BY_DIR_HASH htmp
, *hent
;
256 ent
= sk_BY_DIR_ENTRY_value(ctx
->dirs
, i
);
257 j
= strlen(ent
->dir
) + 1 + 8 + 6 + 1 + 1;
258 if (!BUF_MEM_grow(b
, j
)) {
259 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
262 if (type
== X509_LU_CRL
&& ent
->hashes
) {
264 CRYPTO_THREAD_read_lock(ctx
->lock
);
265 idx
= sk_BY_DIR_HASH_find(ent
->hashes
, &htmp
);
267 hent
= sk_BY_DIR_HASH_value(ent
->hashes
, idx
);
273 CRYPTO_THREAD_unlock(ctx
->lock
);
280 #ifdef OPENSSL_SYS_VMS
281 c
= ent
->dir
[strlen(ent
->dir
) - 1];
282 if (c
!= ':' && c
!= '>' && c
!= ']') {
284 * If no separator is present, we assume the directory
285 * specifier is a logical name, and add a colon. We really
286 * should use better VMS routines for merging things like
287 * this, but this will do for now... -- Richard Levitte
296 * This is special. When c == '\0', no directory separator
299 BIO_snprintf(b
->data
, b
->max
,
300 "%s%08lx.%s%d", ent
->dir
, h
, postfix
, k
);
302 BIO_snprintf(b
->data
, b
->max
,
303 "%s%c%08lx.%s%d", ent
->dir
, c
, h
, postfix
, k
);
305 #ifndef OPENSSL_NO_POSIX_IO
311 if (stat(b
->data
, &st
) < 0)
316 if (type
== X509_LU_X509
) {
317 if ((X509_load_cert_file(xl
, b
->data
, ent
->dir_type
)) == 0)
319 } else if (type
== X509_LU_CRL
) {
320 if ((X509_load_crl_file(xl
, b
->data
, ent
->dir_type
)) == 0)
323 /* else case will caught higher up */
328 * we have added it to the cache so now pull it out again
330 X509_STORE_lock(xl
->store_ctx
);
331 j
= sk_X509_OBJECT_find(xl
->store_ctx
->objs
, &stmp
);
332 tmp
= sk_X509_OBJECT_value(xl
->store_ctx
->objs
, j
);
333 X509_STORE_unlock(xl
->store_ctx
);
335 /* If a CRL, update the last file suffix added for this */
337 if (type
== X509_LU_CRL
) {
338 CRYPTO_THREAD_write_lock(ctx
->lock
);
340 * Look for entry again in case another thread added an entry
345 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
);
361 X509err(X509_F_GET_CERT_BY_SUBJECT
, ERR_R_MALLOC_FAILURE
);
365 } else if (hent
->suffix
< k
) {
369 CRYPTO_THREAD_unlock(ctx
->lock
);
375 ret
->type
= tmp
->type
;
376 memcpy(&ret
->data
, &tmp
->data
, sizeof(ret
->data
));
379 * Clear any errors that might have been raised processing empty
380 * or malformed files.