/src/openssl/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 = 1;  | 
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)) == NULL)  | 
34  | 0  |         return 0;  | 
35  |  |  | 
36  |  |     /*  | 
37  |  |      * We try to set the criterion, but don't care if it was valid or not.  | 
38  |  |      * For an OSSL_STORE, it merely serves as an optimization, the expectation  | 
39  |  |      * being that if the criterion couldn't be used, we will get *everything*  | 
40  |  |      * from the container that the URI represents rather than the subset that  | 
41  |  |      * the criterion indicates, so the biggest harm is that we cache more  | 
42  |  |      * objects certs and CRLs than we may expect, but that's ok.  | 
43  |  |      *  | 
44  |  |      * Specifically for OpenSSL's own file: scheme, the only workable  | 
45  |  |      * criterion is the BY_NAME one, which it can only apply on directories,  | 
46  |  |      * but it's possible that the URI is a single file rather than a directory,  | 
47  |  |      * and in that case, the BY_NAME criterion is pointless.  | 
48  |  |      *  | 
49  |  |      * We could very simply not apply any criterion at all here, and just let  | 
50  |  |      * the code that selects certs and CRLs from the cached objects do its job,  | 
51  |  |      * but it's a nice optimization when it can be applied (such as on an  | 
52  |  |      * actual directory with a thousand CA certs).  | 
53  |  |      */  | 
54  | 0  |     if (criterion != NULL) { | 
55  | 0  |         ERR_set_mark();  | 
56  | 0  |         OSSL_STORE_find(ctx, criterion);  | 
57  | 0  |         ERR_pop_to_mark();  | 
58  | 0  |     }  | 
59  |  | 
  | 
60  | 0  |     for (;;) { | 
61  | 0  |         OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);  | 
62  | 0  |         int infotype;  | 
63  |  |  | 
64  |  |         /* NULL means error or "end of file".  Either way, we break. */  | 
65  | 0  |         if (info == NULL)  | 
66  |  |             /*  | 
67  |  |              * Cannot rely on OSSL_STORE_error() here:  | 
68  |  |              * file_load() incorrectly reports an error at EOF  | 
69  |  |              */  | 
70  | 0  |             break;  | 
71  |  |  | 
72  | 0  |         infotype = OSSL_STORE_INFO_get_type(info);  | 
73  |  | 
  | 
74  | 0  |         switch (infotype) { | 
75  | 0  |         case OSSL_STORE_INFO_NAME:  | 
76  |  |             /*  | 
77  |  |              * This is an entry in the "directory" represented by the current  | 
78  |  |              * uri.  if |depth| allows, dive into it.  | 
79  |  |              */  | 
80  | 0  |             if (depth > 0) { | 
81  | 0  |                 CACHED_STORE substore;  | 
82  |  | 
  | 
83  | 0  |                 substore.uri = (char *)OSSL_STORE_INFO_get0_NAME(info);  | 
84  | 0  |                 substore.libctx = store->libctx;  | 
85  | 0  |                 substore.propq = store->propq;  | 
86  | 0  |                 ok = cache_objects(lctx, &substore, criterion, depth - 1);  | 
87  | 0  |             }  | 
88  | 0  |             break;  | 
89  |  |         /*  | 
90  |  |          * We know that X509_STORE_add_{cert|crl} increments the object's | 
91  |  |          * refcount, so we can safely use OSSL_STORE_INFO_get0_{cert,crl} | 
92  |  |          * to get them.  | 
93  |  |          */  | 
94  | 0  |         case OSSL_STORE_INFO_CERT:  | 
95  | 0  |             ok = X509_STORE_add_cert(xstore, OSSL_STORE_INFO_get0_CERT(info));  | 
96  | 0  |             break;  | 
97  | 0  |         case OSSL_STORE_INFO_CRL:  | 
98  | 0  |             ok = X509_STORE_add_crl(xstore, OSSL_STORE_INFO_get0_CRL(info));  | 
99  | 0  |             break;  | 
100  | 0  |         default:  | 
101  |  |             /* Ignore all other types (PKEY, PUBKEY, PARAMS) */  | 
102  | 0  |             break;  | 
103  | 0  |         }  | 
104  |  |  | 
105  | 0  |         OSSL_STORE_INFO_free(info);  | 
106  | 0  |         if (!ok)  | 
107  | 0  |             break; /* stop on first failure */  | 
108  | 0  |     }  | 
109  | 0  |     OSSL_STORE_close(ctx);  | 
110  |  | 
  | 
111  | 0  |     return ok;  | 
112  | 0  | }  | 
113  |  |  | 
114  |  |  | 
115  |  | static void free_store(CACHED_STORE *store)  | 
116  | 0  | { | 
117  | 0  |     if (store != NULL) { | 
118  | 0  |         OPENSSL_free(store->uri);  | 
119  | 0  |         OPENSSL_free(store->propq);  | 
120  | 0  |         OPENSSL_free(store);  | 
121  | 0  |     }  | 
122  | 0  | }  | 
123  |  |  | 
124  |  | static void by_store_free(X509_LOOKUP *ctx)  | 
125  | 0  | { | 
126  | 0  |     STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);  | 
127  | 0  |     sk_CACHED_STORE_pop_free(stores, free_store);  | 
128  | 0  | }  | 
129  |  |  | 
130  |  | static int by_store_ctrl_ex(X509_LOOKUP *ctx, int cmd, const char *argp,  | 
131  |  |                             long argl, char **retp, OSSL_LIB_CTX *libctx,  | 
132  |  |                             const char *propq)  | 
133  | 0  | { | 
134  | 0  |     switch (cmd) { | 
135  | 0  |     case X509_L_ADD_STORE:  | 
136  | 0  |         if (argp != NULL) { | 
137  | 0  |             STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);  | 
138  | 0  |             CACHED_STORE *store = OPENSSL_zalloc(sizeof(*store));  | 
139  | 0  |             OSSL_STORE_CTX *sctx;  | 
140  |  | 
  | 
141  | 0  |             if (store == NULL) { | 
142  | 0  |                 return 0;  | 
143  | 0  |             }  | 
144  |  |  | 
145  | 0  |             store->uri = OPENSSL_strdup(argp);  | 
146  | 0  |             store->libctx = libctx;  | 
147  | 0  |             if (propq != NULL)  | 
148  | 0  |                 store->propq = OPENSSL_strdup(propq);  | 
149  |  |             /*  | 
150  |  |              * We open this to check for errors now - so we can report those  | 
151  |  |              * errors early.  | 
152  |  |              */  | 
153  | 0  |             sctx = OSSL_STORE_open_ex(argp, libctx, propq, NULL, NULL,  | 
154  | 0  |                                       NULL, NULL, NULL);  | 
155  | 0  |             if (sctx == NULL  | 
156  | 0  |                 || (propq != NULL && store->propq == NULL)  | 
157  | 0  |                 || store->uri == NULL) { | 
158  | 0  |                 OSSL_STORE_close(sctx);  | 
159  | 0  |                 free_store(store);  | 
160  | 0  |                 return 0;  | 
161  | 0  |             }  | 
162  | 0  |             OSSL_STORE_close(sctx);  | 
163  |  | 
  | 
164  | 0  |             if (stores == NULL) { | 
165  | 0  |                 stores = sk_CACHED_STORE_new_null();  | 
166  | 0  |                 if (stores != NULL)  | 
167  | 0  |                     X509_LOOKUP_set_method_data(ctx, stores);  | 
168  | 0  |             }  | 
169  | 0  |             if (stores == NULL || sk_CACHED_STORE_push(stores, store) <= 0) { | 
170  | 0  |                 free_store(store);  | 
171  | 0  |                 return 0;  | 
172  | 0  |             }  | 
173  | 0  |             return 1;  | 
174  | 0  |         }  | 
175  |  |         /* NOP if no URI is given. */  | 
176  | 0  |         return 1;  | 
177  | 0  |     case X509_L_LOAD_STORE: { | 
178  |  |         /* This is a shortcut for quick loading of specific containers */  | 
179  | 0  |         CACHED_STORE store;  | 
180  |  | 
  | 
181  | 0  |         store.uri = (char *)argp;  | 
182  | 0  |         store.libctx = libctx;  | 
183  | 0  |         store.propq = (char *)propq;  | 
184  | 0  |         return cache_objects(ctx, &store, NULL, 0);  | 
185  | 0  |     }  | 
186  | 0  |     default:  | 
187  |  |         /* Unsupported command */  | 
188  | 0  |         return 0;  | 
189  | 0  |     }  | 
190  | 0  | }  | 
191  |  |  | 
192  |  | static int by_store_ctrl(X509_LOOKUP *ctx, int cmd,  | 
193  |  |                          const char *argp, long argl, char **retp)  | 
194  | 0  | { | 
195  | 0  |     return by_store_ctrl_ex(ctx, cmd, argp, argl, retp, NULL, NULL);  | 
196  | 0  | }  | 
197  |  |  | 
198  |  | static int by_store(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,  | 
199  |  |                     const OSSL_STORE_SEARCH *criterion, X509_OBJECT *ret)  | 
200  | 0  | { | 
201  | 0  |     STACK_OF(CACHED_STORE) *stores = X509_LOOKUP_get_method_data(ctx);  | 
202  | 0  |     int i;  | 
203  | 0  |     int ok = 0;  | 
204  |  | 
  | 
205  | 0  |     for (i = 0; i < sk_CACHED_STORE_num(stores); i++) { | 
206  | 0  |         ok = cache_objects(ctx, sk_CACHED_STORE_value(stores, i), criterion,  | 
207  | 0  |                            1 /* depth */);  | 
208  |  | 
  | 
209  | 0  |         if (ok)  | 
210  | 0  |             break;  | 
211  | 0  |     }  | 
212  | 0  |     return ok;  | 
213  | 0  | }  | 
214  |  |  | 
215  |  | static int by_store_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type,  | 
216  |  |                             const X509_NAME *name, X509_OBJECT *ret)  | 
217  | 0  | { | 
218  | 0  |     OSSL_STORE_SEARCH *criterion =  | 
219  | 0  |         OSSL_STORE_SEARCH_by_name((X509_NAME *)name); /* won't modify it */  | 
220  | 0  |     int ok = by_store(ctx, type, criterion, ret);  | 
221  | 0  |     X509_OBJECT *tmp = NULL;  | 
222  |  | 
  | 
223  | 0  |     OSSL_STORE_SEARCH_free(criterion);  | 
224  |  | 
  | 
225  | 0  |     if (ok) { | 
226  | 0  |         STACK_OF(X509_OBJECT) *store_objects;  | 
227  | 0  |         X509_STORE *store = X509_LOOKUP_get_store(ctx);  | 
228  |  | 
  | 
229  | 0  |         if (!ossl_x509_store_read_lock(store))  | 
230  | 0  |             return 0;  | 
231  | 0  |         if (store->objs_ht != NULL)  | 
232  | 0  |             store_objects = ossl_x509_store_ht_get_by_name(store, name);  | 
233  | 0  |         else  | 
234  | 0  |             store_objects = store->objs;  | 
235  | 0  |         if (store_objects != NULL)  | 
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 certificates and CRLs from OSSL_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  | }  |