1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <openssl/store.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include "crypto/x509.h"
13e1051a39Sopenharmony_ci#include "x509_local.h"
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ci/* Generic object loader, given expected type and criterion */
16e1051a39Sopenharmony_cistatic int cache_objects(X509_LOOKUP *lctx, const char *uri,
17e1051a39Sopenharmony_ci                         const OSSL_STORE_SEARCH *criterion,
18e1051a39Sopenharmony_ci                         int depth, OSSL_LIB_CTX *libctx, const char *propq)
19e1051a39Sopenharmony_ci{
20e1051a39Sopenharmony_ci    int ok = 0;
21e1051a39Sopenharmony_ci    OSSL_STORE_CTX *ctx = NULL;
22e1051a39Sopenharmony_ci    X509_STORE *xstore = X509_LOOKUP_get_store(lctx);
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci    if ((ctx = OSSL_STORE_open_ex(uri, libctx, propq, NULL, NULL, NULL,
25e1051a39Sopenharmony_ci                                  NULL, NULL)) == NULL)
26e1051a39Sopenharmony_ci        return 0;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci    /*
29e1051a39Sopenharmony_ci     * We try to set the criterion, but don't care if it was valid or not.
30e1051a39Sopenharmony_ci     * For a OSSL_STORE, it merely serves as an optimization, the expectation
31e1051a39Sopenharmony_ci     * being that if the criterion couldn't be used, we will get *everything*
32e1051a39Sopenharmony_ci     * from the container that the URI represents rather than the subset that
33e1051a39Sopenharmony_ci     * the criterion indicates, so the biggest harm is that we cache more
34e1051a39Sopenharmony_ci     * objects certs and CRLs than we may expect, but that's ok.
35e1051a39Sopenharmony_ci     *
36e1051a39Sopenharmony_ci     * Specifically for OpenSSL's own file: scheme, the only workable
37e1051a39Sopenharmony_ci     * criterion is the BY_NAME one, which it can only apply on directories,
38e1051a39Sopenharmony_ci     * but it's possible that the URI is a single file rather than a directory,
39e1051a39Sopenharmony_ci     * and in that case, the BY_NAME criterion is pointless.
40e1051a39Sopenharmony_ci     *
41e1051a39Sopenharmony_ci     * We could very simply not apply any criterion at all here, and just let
42e1051a39Sopenharmony_ci     * the code that selects certs and CRLs from the cached objects do its job,
43e1051a39Sopenharmony_ci     * but it's a nice optimization when it can be applied (such as on an
44e1051a39Sopenharmony_ci     * actual directory with a thousand CA certs).
45e1051a39Sopenharmony_ci     */
46e1051a39Sopenharmony_ci    if (criterion != NULL)
47e1051a39Sopenharmony_ci        OSSL_STORE_find(ctx, criterion);
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci    for (;;) {
50e1051a39Sopenharmony_ci        OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
51e1051a39Sopenharmony_ci        int infotype;
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci        /* NULL means error or "end of file".  Either way, we break. */
54e1051a39Sopenharmony_ci        if (info == NULL)
55e1051a39Sopenharmony_ci            break;
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci        infotype = OSSL_STORE_INFO_get_type(info);
58e1051a39Sopenharmony_ci        ok = 0;
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci        if (infotype == OSSL_STORE_INFO_NAME) {
61e1051a39Sopenharmony_ci            /*
62e1051a39Sopenharmony_ci             * This is an entry in the "directory" represented by the current
63e1051a39Sopenharmony_ci             * uri.  if |depth| allows, dive into it.
64e1051a39Sopenharmony_ci             */
65e1051a39Sopenharmony_ci            if (depth > 0)
66e1051a39Sopenharmony_ci                ok = cache_objects(lctx, OSSL_STORE_INFO_get0_NAME(info),
67e1051a39Sopenharmony_ci                                   criterion, depth - 1, libctx, propq);
68e1051a39Sopenharmony_ci        } else {
69e1051a39Sopenharmony_ci            /*
70e1051a39Sopenharmony_ci             * We know that X509_STORE_add_{cert|crl} increments the object's
71e1051a39Sopenharmony_ci             * refcount, so we can safely use OSSL_STORE_INFO_get0_{cert,crl}
72e1051a39Sopenharmony_ci             * to get them.
73e1051a39Sopenharmony_ci             */
74e1051a39Sopenharmony_ci            switch (infotype) {
75e1051a39Sopenharmony_ci            case OSSL_STORE_INFO_CERT:
76e1051a39Sopenharmony_ci                ok = X509_STORE_add_cert(xstore,
77e1051a39Sopenharmony_ci                                         OSSL_STORE_INFO_get0_CERT(info));
78e1051a39Sopenharmony_ci                break;
79e1051a39Sopenharmony_ci            case OSSL_STORE_INFO_CRL:
80e1051a39Sopenharmony_ci                ok = X509_STORE_add_crl(xstore,
81e1051a39Sopenharmony_ci                                        OSSL_STORE_INFO_get0_CRL(info));
82e1051a39Sopenharmony_ci                break;
83e1051a39Sopenharmony_ci            }
84e1051a39Sopenharmony_ci        }
85e1051a39Sopenharmony_ci
86e1051a39Sopenharmony_ci        OSSL_STORE_INFO_free(info);
87e1051a39Sopenharmony_ci        if (!ok)
88e1051a39Sopenharmony_ci            break;
89e1051a39Sopenharmony_ci    }
90e1051a39Sopenharmony_ci    OSSL_STORE_close(ctx);
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    return ok;
93e1051a39Sopenharmony_ci}
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ci/* Because OPENSSL_free is a macro and for C type match */
97e1051a39Sopenharmony_cistatic void free_uri(OPENSSL_STRING data)
98e1051a39Sopenharmony_ci{
99e1051a39Sopenharmony_ci    OPENSSL_free(data);
100e1051a39Sopenharmony_ci}
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_cistatic void by_store_free(X509_LOOKUP *ctx)
103e1051a39Sopenharmony_ci{
104e1051a39Sopenharmony_ci    STACK_OF(OPENSSL_STRING) *uris = X509_LOOKUP_get_method_data(ctx);
105e1051a39Sopenharmony_ci    sk_OPENSSL_STRING_pop_free(uris, free_uri);
106e1051a39Sopenharmony_ci}
107e1051a39Sopenharmony_ci
108e1051a39Sopenharmony_cistatic int by_store_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,
109e1051a39Sopenharmony_ci                            long argl, char **retp, OSSL_LIB_CTX *libctx,
110e1051a39Sopenharmony_ci                            const char *propq)
111e1051a39Sopenharmony_ci{
112e1051a39Sopenharmony_ci    switch (cmd) {
113e1051a39Sopenharmony_ci    case X509_L_ADD_STORE:
114e1051a39Sopenharmony_ci        /* If no URI is given, use the default cert dir as default URI */
115e1051a39Sopenharmony_ci        if (argp == NULL)
116e1051a39Sopenharmony_ci            argp = ossl_safe_getenv(X509_get_default_cert_dir_env());
117e1051a39Sopenharmony_ci        if (argp == NULL)
118e1051a39Sopenharmony_ci            argp = X509_get_default_cert_dir();
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci        {
121e1051a39Sopenharmony_ci            STACK_OF(OPENSSL_STRING) *uris = X509_LOOKUP_get_method_data(ctx);
122e1051a39Sopenharmony_ci            char *data = OPENSSL_strdup(argp);
123e1051a39Sopenharmony_ci
124e1051a39Sopenharmony_ci            if (data == NULL) {
125e1051a39Sopenharmony_ci                return 0;
126e1051a39Sopenharmony_ci            }
127e1051a39Sopenharmony_ci            if (uris == NULL) {
128e1051a39Sopenharmony_ci                uris = sk_OPENSSL_STRING_new_null();
129e1051a39Sopenharmony_ci                X509_LOOKUP_set_method_data(ctx, uris);
130e1051a39Sopenharmony_ci            }
131e1051a39Sopenharmony_ci            return sk_OPENSSL_STRING_push(uris, data) > 0;
132e1051a39Sopenharmony_ci        }
133e1051a39Sopenharmony_ci    case X509_L_LOAD_STORE:
134e1051a39Sopenharmony_ci        /* This is a shortcut for quick loading of specific containers */
135e1051a39Sopenharmony_ci        return cache_objects(ctx, argp, NULL, 0, libctx, propq);
136e1051a39Sopenharmony_ci    }
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_ci    return 0;
139e1051a39Sopenharmony_ci}
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_cistatic int by_store_ctrl(X509_LOOKUP *ctx, int cmd,
142e1051a39Sopenharmony_ci                         const char *argp, long argl, char **retp)
143e1051a39Sopenharmony_ci{
144e1051a39Sopenharmony_ci    return by_store_ctrl_ex(ctx, cmd, argp, argl, retp, NULL, NULL);
145e1051a39Sopenharmony_ci}
146e1051a39Sopenharmony_ci
147e1051a39Sopenharmony_cistatic int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
148e1051a39Sopenharmony_ci                    const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret,
149e1051a39Sopenharmony_ci                    OSSL_LIB_CTX *libctx, const char *propq)
150e1051a39Sopenharmony_ci{
151e1051a39Sopenharmony_ci    STACK_OF(OPENSSL_STRING) *uris = X509_LOOKUP_get_method_data(ctx);
152e1051a39Sopenharmony_ci    int i;
153e1051a39Sopenharmony_ci    int ok = 0;
154e1051a39Sopenharmony_ci
155e1051a39Sopenharmony_ci    for (i = 0; i < sk_OPENSSL_STRING_num(uris); i++) {
156e1051a39Sopenharmony_ci        ok = cache_objects(ctx, sk_OPENSSL_STRING_value(uris, i), criterion,
157e1051a39Sopenharmony_ci                           1 /* depth */, libctx, propq);
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_ci        if (ok)
160e1051a39Sopenharmony_ci            break;
161e1051a39Sopenharmony_ci    }
162e1051a39Sopenharmony_ci    return ok;
163e1051a39Sopenharmony_ci}
164e1051a39Sopenharmony_ci
165e1051a39Sopenharmony_cistatic int by_store_subject_ex(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
166e1051a39Sopenharmony_ci                               const X509_NAME *name, X509_OBJECT *ret,
167e1051a39Sopenharmony_ci                               OSSL_LIB_CTX *libctx, const char *propq)
168e1051a39Sopenharmony_ci{
169e1051a39Sopenharmony_ci    OSSL_STORE_SEARCH *criterion =
170e1051a39Sopenharmony_ci        OSSL_STORE_SEARCH_by_name((X509_NAME *)name); /* won't modify it */
171e1051a39Sopenharmony_ci    int ok = by_store(ctx, type, criterion, ret, libctx, propq);
172e1051a39Sopenharmony_ci    STACK_OF(X509_OBJECT) *store_objects =
173e1051a39Sopenharmony_ci        X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx));
174e1051a39Sopenharmony_ci    X509_OBJECT *tmp = NULL;
175e1051a39Sopenharmony_ci
176e1051a39Sopenharmony_ci    OSSL_STORE_SEARCH_free(criterion);
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_ci    if (ok)
179e1051a39Sopenharmony_ci        tmp = X509_OBJECT_retrieve_by_subject(store_objects, type, name);
180e1051a39Sopenharmony_ci
181e1051a39Sopenharmony_ci    ok = 0;
182e1051a39Sopenharmony_ci    if (tmp != NULL) {
183e1051a39Sopenharmony_ci        /*
184e1051a39Sopenharmony_ci         * This could also be done like this:
185e1051a39Sopenharmony_ci         *
186e1051a39Sopenharmony_ci         *     if (tmp != NULL) {
187e1051a39Sopenharmony_ci         *         *ret = *tmp;
188e1051a39Sopenharmony_ci         *         ok = 1;
189e1051a39Sopenharmony_ci         *     }
190e1051a39Sopenharmony_ci         *
191e1051a39Sopenharmony_ci         * However, we want to exercise the documented API to the max, so
192e1051a39Sopenharmony_ci         * we do it the hard way.
193e1051a39Sopenharmony_ci         *
194e1051a39Sopenharmony_ci         * To be noted is that X509_OBJECT_set1_* increment the refcount,
195e1051a39Sopenharmony_ci         * but so does X509_STORE_CTX_get_by_subject upon return of this
196e1051a39Sopenharmony_ci         * function, so we must ensure the refcount is decremented
197e1051a39Sopenharmony_ci         * before we return, or we will get a refcount leak.  We cannot do
198e1051a39Sopenharmony_ci         * this with X509_OBJECT_free(), though, as that will free a bit
199e1051a39Sopenharmony_ci         * too much.
200e1051a39Sopenharmony_ci         */
201e1051a39Sopenharmony_ci        switch (type) {
202e1051a39Sopenharmony_ci        case X509_LU_X509:
203e1051a39Sopenharmony_ci            ok = X509_OBJECT_set1_X509(ret, tmp->data.x509);
204e1051a39Sopenharmony_ci            if (ok)
205e1051a39Sopenharmony_ci                X509_free(tmp->data.x509);
206e1051a39Sopenharmony_ci            break;
207e1051a39Sopenharmony_ci        case X509_LU_CRL:
208e1051a39Sopenharmony_ci            ok = X509_OBJECT_set1_X509_CRL(ret, tmp->data.crl);
209e1051a39Sopenharmony_ci            if (ok)
210e1051a39Sopenharmony_ci                X509_CRL_free(tmp->data.crl);
211e1051a39Sopenharmony_ci            break;
212e1051a39Sopenharmony_ci        case X509_LU_NONE:
213e1051a39Sopenharmony_ci            break;
214e1051a39Sopenharmony_ci        }
215e1051a39Sopenharmony_ci    }
216e1051a39Sopenharmony_ci    return ok;
217e1051a39Sopenharmony_ci}
218e1051a39Sopenharmony_ci
219e1051a39Sopenharmony_cistatic int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,
220e1051a39Sopenharmony_ci                            const X509_NAME *name, X509_OBJECT *ret)
221e1051a39Sopenharmony_ci{
222e1051a39Sopenharmony_ci    return by_store_subject_ex(ctx, type, name, ret, NULL, NULL);
223e1051a39Sopenharmony_ci}
224e1051a39Sopenharmony_ci
225e1051a39Sopenharmony_ci/*
226e1051a39Sopenharmony_ci * We lack the implementations for get_by_issuer_serial, get_by_fingerprint
227e1051a39Sopenharmony_ci * and get_by_alias.  There's simply not enough support in the X509_LOOKUP
228e1051a39Sopenharmony_ci * or X509_STORE APIs.
229e1051a39Sopenharmony_ci */
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_cistatic X509_LOOKUP_METHOD x509_store_lookup = {
232e1051a39Sopenharmony_ci    "Load certs from STORE URIs",
233e1051a39Sopenharmony_ci    NULL,                        /* new_item */
234e1051a39Sopenharmony_ci    by_store_free,               /* free */
235e1051a39Sopenharmony_ci    NULL,                        /* init */
236e1051a39Sopenharmony_ci    NULL,                        /* shutdown */
237e1051a39Sopenharmony_ci    by_store_ctrl,               /* ctrl */
238e1051a39Sopenharmony_ci    by_store_subject,            /* get_by_subject */
239e1051a39Sopenharmony_ci    NULL,                        /* get_by_issuer_serial */
240e1051a39Sopenharmony_ci    NULL,                        /* get_by_fingerprint */
241e1051a39Sopenharmony_ci    NULL,                        /* get_by_alias */
242e1051a39Sopenharmony_ci    by_store_subject_ex,
243e1051a39Sopenharmony_ci    by_store_ctrl_ex
244e1051a39Sopenharmony_ci};
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ciX509_LOOKUP_METHOD *X509_LOOKUP_store(void)
247e1051a39Sopenharmony_ci{
248e1051a39Sopenharmony_ci    return &x509_store_lookup;
249e1051a39Sopenharmony_ci}
250