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