#include <openssl/safestack.h>
#include <openssl/store.h>
#include "internal/cryptlib.h"
#include "crypto/x509.h"
#include "x509_local.h"
typedef struct cached_store_st {
char *uri;
OSSL_LIB_CTX *libctx;
char *propq;
} CACHED_STORE;
DEFINE_STACK_OF(CACHED_STORE)
static int cache_objects(X509_LOOKUP *lctx, CACHED_STORE *store,
const OSSL_STORE_SEARCH *criterion, int depth)
{
int ok = 0;
OSSL_STORE_CTX *ctx;
X509_STORE *xstore = X509_LOOKUP_get_store(lctx);
if ((ctx = OSSL_STORE_open_ex(store->uri, store->libctx, store->propq,
NULL, NULL, NULL, NULL, NULL))
== NULL)
return 0;
if (criterion != NULL)
OSSL_STORE_find(ctx, criterion);
for (;;) {
OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
int infotype;
if (info == NULL)
break;
infotype = OSSL_STORE_INFO_get_type(info);
ok = 0;
if (infotype == OSSL_STORE_INFO_NAME) {
if (depth > 0) {
CACHED_STORE substore;
substore.uri = (char *)OSSL_STORE_INFO_get0_NAME(info);
substore.libctx = store->libctx;
substore.propq = store->propq;
ok = cache_objects(lctx, &substore, criterion, depth - 1);
}
} else {
switch (infotype) {
case OSSL_STORE_INFO_CERT:
ok = X509_STORE_add_cert(xstore,
OSSL_STORE_INFO_get0_CERT(info));
break;
case OSSL_STORE_INFO_CRL:
ok = X509_STORE_add_crl(xstore,
OSSL_STORE_INFO_get0_CRL(info));
break;
}
}
OSSL_STORE_INFO_free(info);
if (!ok)
break;
}
OSSL_STORE_close(ctx);
return ok;
}
static void free_store(CACHED_STORE *store)
{
if (store != NULL) {
OPENSSL_free(store->uri);
OPENSSL_free(store->propq);
OPENSSL_free(store);
}
}
static void by_store_free(X509_LOOKUP *ctx)
{
STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
sk_CACHED_STORE_pop_free(stores, free_store);
}
static int by_store_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
long argl, char **retp, OSSL_LIB_CTX *libctx,
const char *propq)
{
switch (cmd) {
case X509_L_ADD_STORE:
if (argp != NULL) {
STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
CACHED_STORE *store = OPENSSL_zalloc(sizeof(*store));
OSSL_STORE_CTX *sctx;
if (store == NULL) {
return 0;
}
store->uri = OPENSSL_strdup(argp);
store->libctx = libctx;
if (propq != NULL)
store->propq = OPENSSL_strdup(propq);
sctx = OSSL_STORE_open_ex(argp, libctx, propq, NULL, NULL,
NULL, NULL, NULL);
if (sctx == NULL
|| (propq != NULL && store->propq == NULL)
|| store->uri == NULL) {
OSSL_STORE_close(sctx);
free_store(store);
return 0;
}
OSSL_STORE_close(sctx);
if (stores == NULL) {
stores = sk_CACHED_STORE_new_null();
if (stores != NULL)
X509_LOOKUP_set_method_data(ctx, stores);
}
if (stores == NULL || sk_CACHED_STORE_push(stores, store) <= 0) {
free_store(store);
return 0;
}
return 1;
}
return 1;
case X509_L_LOAD_STORE: {
CACHED_STORE store;
store.uri = (char *)argp;
store.libctx = libctx;
store.propq = (char *)propq;
return cache_objects(ctx, &store, NULL, 0);
}
default:
return 0;
}
}
static int by_store_ctrl(X509_LOOKUP *ctx, int cmd,
const char *argp, long argl, char **retp)
{
return by_store_ctrl_ex(ctx, cmd, argp, argl, retp, NULL, NULL);
}
static int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret)
{
STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
int i;
int ok = 0;
for (i = 0; i < sk_CACHED_STORE_num(stores); i++) {
ok = cache_objects(ctx, sk_CACHED_STORE_value(stores, i), criterion,
1 );
if (ok)
break;
}
return ok;
}
static int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
const X509_NAME *name, X509_OBJECT *ret)
{
OSSL_STORE_SEARCH *criterion = OSSL_STORE_SEARCH_by_name((X509_NAME *)name);
int ok = by_store(ctx, type, criterion, ret);
STACK_OF(X509_OBJECT) *store_objects = X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx));
X509_OBJECT *tmp = NULL;
OSSL_STORE_SEARCH_free(criterion);
if (ok) {
X509_STORE *store = X509_LOOKUP_get_store(ctx);
if (!ossl_x509_store_read_lock(store))
return 0;
tmp = X509_OBJECT_retrieve_by_subject(store_objects, type, name);
X509_STORE_unlock(store);
}
ok = 0;
if (tmp != NULL) {
switch (type) {
case X509_LU_X509:
ok = X509_OBJECT_set1_X509(ret, tmp->data.x509);
if (ok)
X509_free(tmp->data.x509);
break;
case X509_LU_CRL:
ok = X509_OBJECT_set1_X509_CRL(ret, tmp->data.crl);
if (ok)
X509_CRL_free(tmp->data.crl);
break;
case X509_LU_NONE:
break;
}
}
return ok;
}
static X509_LOOKUP_METHOD x509_store_lookup = {
"Load certs from STORE URIs",
NULL,
by_store_free,
NULL,
NULL,
by_store_ctrl,
by_store_subject,
NULL,
NULL,
NULL,
NULL,
by_store_ctrl_ex
};
X509_LOOKUP_METHOD *X509_LOOKUP_store(void)
{
return &x509_store_lookup;
}