Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/x509/by_store.c
Line
Count
Source
1
/*
2
 * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <openssl/safestack.h>
11
#include <openssl/store.h>
12
#include "internal/cryptlib.h"
13
#include "crypto/x509.h"
14
#include "x509_local.h"
15
16
typedef struct cached_store_st {
17
    char *uri;
18
    OSSL_LIB_CTX *libctx;
19
    char *propq;
20
} CACHED_STORE;
21
22
DEFINE_STACK_OF(CACHED_STORE)
23
24
/* Generic object loader, given expected type and criterion */
25
static int cache_objects(X509_LOOKUP *lctx, CACHED_STORE *store,
26
    const OSSL_STORE_SEARCH *criterion, int depth)
27
0
{
28
0
    int ok = 0;
29
0
    OSSL_STORE_CTX *ctx;
30
0
    X509_STORE *xstore = X509_LOOKUP_get_store(lctx);
31
32
0
    if ((ctx = OSSL_STORE_open_ex(store->uri, store->libctx, store->propq,
33
0
             NULL, NULL, NULL, NULL, NULL))
34
0
        == NULL)
35
0
        return 0;
36
37
    /*
38
     * We try to set the criterion, but don't care if it was valid or not.
39
     * For a OSSL_STORE, it merely serves as an optimization, the expectation
40
     * being that if the criterion couldn't be used, we will get *everything*
41
     * from the container that the URI represents rather than the subset that
42
     * the criterion indicates, so the biggest harm is that we cache more
43
     * objects certs and CRLs than we may expect, but that's ok.
44
     *
45
     * Specifically for OpenSSL's own file: scheme, the only workable
46
     * criterion is the BY_NAME one, which it can only apply on directories,
47
     * but it's possible that the URI is a single file rather than a directory,
48
     * and in that case, the BY_NAME criterion is pointless.
49
     *
50
     * We could very simply not apply any criterion at all here, and just let
51
     * the code that selects certs and CRLs from the cached objects do its job,
52
     * but it's a nice optimization when it can be applied (such as on an
53
     * actual directory with a thousand CA certs).
54
     */
55
0
    if (criterion != NULL)
56
0
        OSSL_STORE_find(ctx, criterion);
57
58
0
    for (;;) {
59
0
        OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
60
0
        int infotype;
61
62
        /* NULL means error or "end of file".  Either way, we break. */
63
0
        if (info == NULL)
64
0
            break;
65
66
0
        infotype = OSSL_STORE_INFO_get_type(info);
67
0
        ok = 0;
68
69
0
        if (infotype == OSSL_STORE_INFO_NAME) {
70
            /*
71
             * This is an entry in the "directory" represented by the current
72
             * uri.  if |depth| allows, dive into it.
73
             */
74
0
            if (depth > 0) {
75
0
                CACHED_STORE substore;
76
77
0
                substore.uri = (char *)OSSL_STORE_INFO_get0_NAME(info);
78
0
                substore.libctx = store->libctx;
79
0
                substore.propq = store->propq;
80
0
                ok = cache_objects(lctx, &substore, criterion, depth - 1);
81
0
            }
82
0
        } else {
83
            /*
84
             * We know that X509_STORE_add_{cert|crl} increments the object's
85
             * refcount, so we can safely use OSSL_STORE_INFO_get0_{cert,crl}
86
             * to get them.
87
             */
88
0
            switch (infotype) {
89
0
            case OSSL_STORE_INFO_CERT:
90
0
                ok = X509_STORE_add_cert(xstore,
91
0
                    OSSL_STORE_INFO_get0_CERT(info));
92
0
                break;
93
0
            case OSSL_STORE_INFO_CRL:
94
0
                ok = X509_STORE_add_crl(xstore,
95
0
                    OSSL_STORE_INFO_get0_CRL(info));
96
0
                break;
97
0
            }
98
0
        }
99
100
0
        OSSL_STORE_INFO_free(info);
101
0
        if (!ok)
102
0
            break;
103
0
    }
104
0
    OSSL_STORE_close(ctx);
105
106
0
    return ok;
107
0
}
108
109
static void free_store(CACHED_STORE *store)
110
0
{
111
0
    if (store != NULL) {
112
0
        OPENSSL_free(store->uri);
113
0
        OPENSSL_free(store->propq);
114
0
        OPENSSL_free(store);
115
0
    }
116
0
}
117
118
static void by_store_free(X509_LOOKUP *ctx)
119
0
{
120
0
    STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
121
0
    sk_CACHED_STORE_pop_free(stores, free_store);
122
0
}
123
124
static int by_store_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
125
    long argl, char **retp, OSSL_LIB_CTX *libctx,
126
    const char *propq)
127
0
{
128
    /*
129
     * In some cases below, failing to use the defaults shouldn't result in
130
     * an error.  |use_default| is used as the return code in those cases.
131
     */
132
0
    int use_default = argp == NULL;
133
134
0
    switch (cmd) {
135
0
    case X509_L_ADD_STORE:
136
        /* If no URI is given, use the default cert dir as default URI */
137
0
        if (argp == NULL)
138
0
            argp = ossl_safe_getenv(X509_get_default_cert_dir_env());
139
0
        if (argp == NULL)
140
0
            argp = X509_get_default_cert_dir();
141
142
0
        {
143
0
            STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
144
0
            CACHED_STORE *store = OPENSSL_zalloc(sizeof(*store));
145
0
            OSSL_STORE_CTX *sctx;
146
147
0
            if (store == NULL) {
148
0
                return 0;
149
0
            }
150
151
0
            store->uri = OPENSSL_strdup(argp);
152
0
            store->libctx = libctx;
153
0
            if (propq != NULL)
154
0
                store->propq = OPENSSL_strdup(propq);
155
            /*
156
             * We open this to check for errors now - so we can report those
157
             * errors early.
158
             */
159
0
            sctx = OSSL_STORE_open_ex(argp, libctx, propq, NULL, NULL,
160
0
                NULL, NULL, NULL);
161
0
            if (sctx == NULL
162
0
                || (propq != NULL && store->propq == NULL)
163
0
                || store->uri == NULL) {
164
0
                OSSL_STORE_close(sctx);
165
0
                free_store(store);
166
0
                return use_default;
167
0
            }
168
0
            OSSL_STORE_close(sctx);
169
170
0
            if (stores == NULL) {
171
0
                stores = sk_CACHED_STORE_new_null();
172
0
                if (stores != NULL)
173
0
                    X509_LOOKUP_set_method_data(ctx, stores);
174
0
            }
175
0
            if (stores == NULL || sk_CACHED_STORE_push(stores, store) <= 0) {
176
0
                free_store(store);
177
0
                return 0;
178
0
            }
179
0
            return 1;
180
0
        }
181
0
    case X509_L_LOAD_STORE: {
182
        /* This is a shortcut for quick loading of specific containers */
183
0
        CACHED_STORE store;
184
185
0
        store.uri = (char *)argp;
186
0
        store.libctx = libctx;
187
0
        store.propq = (char *)propq;
188
0
        return cache_objects(ctx, &store, NULL, 0);
189
0
    }
190
0
    default:
191
        /* Unsupported command */
192
0
        return 0;
193
0
    }
194
195
0
    return 0;
196
0
}
197
198
static int by_store_ctrl(X509_LOOKUP *ctx, int cmd,
199
    const char *argp, long argl, char **retp)
200
0
{
201
0
    return by_store_ctrl_ex(ctx, cmd, argp, argl, retp, NULL, NULL);
202
0
}
203
204
static int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
205
    const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret)
206
0
{
207
0
    STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);
208
0
    int i;
209
0
    int ok = 0;
210
211
0
    for (i = 0; i < sk_CACHED_STORE_num(stores); i++) {
212
0
        ok = cache_objects(ctx, sk_CACHED_STORE_value(stores, i), criterion,
213
0
            1 /* depth */);
214
215
0
        if (ok)
216
0
            break;
217
0
    }
218
0
    return ok;
219
0
}
220
221
static int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
222
    const X509_NAME *name, X509_OBJECT *ret)
223
0
{
224
0
    OSSL_STORE_SEARCH *criterion = OSSL_STORE_SEARCH_by_name((X509_NAME *)name); /* won't modify it */
225
0
    int ok = by_store(ctx, type, criterion, ret);
226
0
    STACK_OF(X509_OBJECT) *store_objects = X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx));
227
0
    X509_OBJECT *tmp = NULL;
228
229
0
    OSSL_STORE_SEARCH_free(criterion);
230
231
0
    if (ok) {
232
0
        X509_STORE *store = X509_LOOKUP_get_store(ctx);
233
234
0
        if (!X509_STORE_lock(store))
235
0
            return 0;
236
0
        tmp = X509_OBJECT_retrieve_by_subject(store_objects, type, name);
237
0
        X509_STORE_unlock(store);
238
0
    }
239
240
0
    ok = 0;
241
0
    if (tmp != NULL) {
242
        /*
243
         * This could also be done like this:
244
         *
245
         *     if (tmp != NULL) {
246
         *         *ret = *tmp;
247
         *         ok = 1;
248
         *     }
249
         *
250
         * However, we want to exercise the documented API to the max, so
251
         * we do it the hard way.
252
         *
253
         * To be noted is that X509_OBJECT_set1_* increment the refcount,
254
         * but so does X509_STORE_CTX_get_by_subject upon return of this
255
         * function, so we must ensure the refcount is decremented
256
         * before we return, or we will get a refcount leak.  We cannot do
257
         * this with X509_OBJECT_free(), though, as that will free a bit
258
         * too much.
259
         */
260
0
        switch (type) {
261
0
        case X509_LU_X509:
262
0
            ok = X509_OBJECT_set1_X509(ret, tmp->data.x509);
263
0
            if (ok)
264
0
                X509_free(tmp->data.x509);
265
0
            break;
266
0
        case X509_LU_CRL:
267
0
            ok = X509_OBJECT_set1_X509_CRL(ret, tmp->data.crl);
268
0
            if (ok)
269
0
                X509_CRL_free(tmp->data.crl);
270
0
            break;
271
0
        case X509_LU_NONE:
272
0
            break;
273
0
        }
274
0
    }
275
0
    return ok;
276
0
}
277
278
/*
279
 * We lack the implementations for get_by_issuer_serial, get_by_fingerprint
280
 * and get_by_alias.  There's simply not enough support in the X509_LOOKUP
281
 * or X509_STORE APIs.
282
 */
283
284
static X509_LOOKUP_METHOD x509_store_lookup = {
285
    "Load certs from STORE URIs",
286
    NULL, /* new_item */
287
    by_store_free, /* free */
288
    NULL, /* init */
289
    NULL, /* shutdown */
290
    by_store_ctrl, /* ctrl */
291
    by_store_subject, /* get_by_subject */
292
    NULL, /* get_by_issuer_serial */
293
    NULL, /* get_by_fingerprint */
294
    NULL, /* get_by_alias */
295
    NULL, /* get_by_subject_ex */
296
    by_store_ctrl_ex
297
};
298
299
X509_LOOKUP_METHOD *X509_LOOKUP_store(void)
300
0
{
301
0
    return &x509_store_lookup;
302
0
}