/*
- * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
return CRYPTO_THREAD_write_lock(xs->lock);
}
+static int x509_store_read_lock(X509_STORE *xs)
+{
+ return CRYPTO_THREAD_read_lock(xs->lock);
+}
+
int X509_STORE_unlock(X509_STORE *xs)
{
return CRYPTO_THREAD_unlock(xs->lock);
ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
goto err;
}
- ret->references = 1;
+
+ if (!CRYPTO_NEW_REF(&ret->references, 1))
+ goto err;
return ret;
err:
X509_VERIFY_PARAM_free(ret->param);
sk_X509_OBJECT_free(ret->objs);
sk_X509_LOOKUP_free(ret->get_cert_methods);
+ CRYPTO_THREAD_lock_free(ret->lock);
OPENSSL_free(ret);
return NULL;
}
if (xs == NULL)
return;
- CRYPTO_DOWN_REF(&xs->references, &i, xs->lock);
+ CRYPTO_DOWN_REF(&xs->references, &i);
REF_PRINT_COUNT("X509_STORE", xs);
if (i > 0)
return;
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE, xs, &xs->ex_data);
X509_VERIFY_PARAM_free(xs->param);
CRYPTO_THREAD_lock_free(xs->lock);
+ CRYPTO_FREE_REF(&xs->references);
OPENSSL_free(xs);
}
{
int i;
- if (CRYPTO_UP_REF(&xs->references, &i, xs->lock) <= 0)
+ if (CRYPTO_UP_REF(&xs->references, &i) <= 0)
return 0;
REF_PRINT_COUNT("X509_STORE", xs);
stmp.type = X509_LU_NONE;
stmp.data.ptr = NULL;
- if (!X509_STORE_lock(store))
+ if (!x509_store_read_lock(store))
return 0;
- sk_X509_OBJECT_sort(store->objs);
+ /* Should already be sorted...but just in case */
+ if (!sk_X509_OBJECT_is_sorted(store->objs)) {
+ X509_STORE_unlock(store);
+ /* Take a write lock instead of a read lock */
+ if (!X509_STORE_lock(store))
+ return 0;
+ /*
+ * Another thread might have sorted it in the meantime. But if so,
+ * sk_X509_OBJECT_sort() exits early.
+ */
+ sk_X509_OBJECT_sort(store->objs);
+ }
tmp = X509_OBJECT_retrieve_by_subject(store->objs, type, name);
X509_STORE_unlock(store);
return -1;
}
+ /* Assumes h is locked for read if applicable */
return sk_X509_OBJECT_find_all(h, &stmp, pnmatch);
}
+/* Assumes h is locked for read if applicable */
int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type,
const X509_NAME *name)
{
return x509_object_idx_cnt(h, type, name, NULL);
}
+/* Assumes h is locked for read if applicable */
X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
X509_LOOKUP_TYPE type,
const X509_NAME *name)