Debian Codegraph @

external/boringssl/crypto/x509/by_dir.c

raw
1 /* crypto/x509/by_dir.c */
2 /* Copyright (C) 1995-1998 Eric Young ([email protected])
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young ([email protected]).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson ([email protected]).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young ([email protected])"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson ([email protected])"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] */
57
58 #include <string.h>
59 #include <sys/stat.h>
60 #include <sys/types.h>
61
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/lhash.h>
65 #include <openssl/mem.h>
66 #include <openssl/thread.h>
67 #include <openssl/x509.h>
68
69 #include "../internal.h"
70
71 #ifdef OPENSSL_WINDOWS
72 #define LIST_SEPARATOR_CHAR ';'
73 #else
74 #define LIST_SEPARATOR_CHAR ':'
75 #endif
76
77 typedef struct lookup_dir_hashes_st {
78 unsigned long hash;
79 int suffix;
80 } BY_DIR_HASH;
81
82 typedef struct lookup_dir_entry_st {
83 char *dir;
84 int dir_type;
85 STACK_OF(BY_DIR_HASH) *hashes;
86 } BY_DIR_ENTRY;
87
88 typedef struct lookup_dir_st {
89 BUF_MEM *buffer;
90 STACK_OF(BY_DIR_ENTRY) *dirs;
91 } BY_DIR;
92
93 DECLARE_STACK_OF(BY_DIR_HASH)
94 DECLARE_STACK_OF(BY_DIR_ENTRY)
95
96 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
97 char **ret);
98 static int new_dir(X509_LOOKUP *lu);
99 static void free_dir(X509_LOOKUP *lu);
100 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
101 static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
102 X509_OBJECT *ret);
103 static X509_LOOKUP_METHOD x509_dir_lookup = {
104 "Load certs from files in a directory",
105 new_dir, /* new */
106 free_dir, /* free */
107 NULL, /* init */
108 NULL, /* shutdown */
109 dir_ctrl, /* ctrl */
110 get_cert_by_subject, /* get_by_subject */
111 NULL, /* get_by_issuer_serial */
112 NULL, /* get_by_fingerprint */
113 NULL, /* get_by_alias */
114 };
115
116 X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
117 {
118 return (&x509_dir_lookup);
119 }
120
121 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
122 char **retp)
123 {
124 int ret = 0;
125 BY_DIR *ld;
126 char *dir = NULL;
127
128 ld = (BY_DIR *)ctx->method_data;
129
130 switch (cmd) {
131 case X509_L_ADD_DIR:
132 if (argl == X509_FILETYPE_DEFAULT) {
133 dir = (char *)getenv(X509_get_default_cert_dir_env());
134 if (dir)
135 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
136 else
137 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
138 X509_FILETYPE_PEM);
139 if (!ret) {
140 OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR);
141 }
142 } else
143 ret = add_cert_dir(ld, argp, (int)argl);
144 break;
145 }
146 return (ret);
147 }
148
149 static int new_dir(X509_LOOKUP *lu)
150 {
151 BY_DIR *a;
152
153 if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
154 return (0);
155 if ((a->buffer = BUF_MEM_new()) == NULL) {
156 OPENSSL_free(a);
157 return (0);
158 }
159 a->dirs = NULL;
160 lu->method_data = (char *)a;
161 return (1);
162 }
163
164 static void by_dir_hash_free(BY_DIR_HASH *hash)
165 {
166 OPENSSL_free(hash);
167 }
168
169 static int by_dir_hash_cmp(const BY_DIR_HASH **a, const BY_DIR_HASH **b)
170 {
171 if ((*a)->hash > (*b)->hash)
172 return 1;
173 if ((*a)->hash < (*b)->hash)
174 return -1;
175 return 0;
176 }
177
178 static void by_dir_entry_free(BY_DIR_ENTRY *ent)
179 {
180 if (ent->dir)
181 OPENSSL_free(ent->dir);
182 if (ent->hashes)
183 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
184 OPENSSL_free(ent);
185 }
186
187 static void free_dir(X509_LOOKUP *lu)
188 {
189 BY_DIR *a;
190
191 a = (BY_DIR *)lu->method_data;
192 if (a->dirs != NULL)
193 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
194 if (a->buffer != NULL)
195 BUF_MEM_free(a->buffer);
196 OPENSSL_free(a);
197 }
198
199 static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
200 {
201 size_t j, len;
202 const char *s, *ss, *p;
203
204 if (dir == NULL || !*dir) {
205 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY);
206 return 0;
207 }
208
209 s = dir;
210 p = s;
211 do {
212 if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) {
213 BY_DIR_ENTRY *ent;
214 ss = s;
215 s = p + 1;
216 len = p - ss;
217 if (len == 0)
218 continue;
219 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
220 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
221 if (strlen(ent->dir) == len &&
222 strncmp(ent->dir, ss, len) == 0)
223 break;
224 }
225 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
226 continue;
227 if (ctx->dirs == NULL) {
228 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
229 if (!ctx->dirs) {
230 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
231 return 0;
232 }
233 }
234 ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY));
235 if (!ent)
236 return 0;
237 ent->dir_type = type;
238 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
239 ent->dir = OPENSSL_malloc(len + 1);
240 if (!ent->dir || !ent->hashes) {
241 by_dir_entry_free(ent);
242 return 0;
243 }
244 strncpy(ent->dir, ss, len);
245 ent->dir[len] = '\0';
246 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
247 by_dir_entry_free(ent);
248 return 0;
249 }
250 }
251 } while (*p++ != '\0');
252 return 1;
253 }
254
255 /*
256 * g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY|
257 * objects.
258 */
259 static struct CRYPTO_STATIC_MUTEX g_ent_hashes_lock =
260 CRYPTO_STATIC_MUTEX_INIT;
261
262 static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
263 X509_OBJECT *ret)
264 {
265 BY_DIR *ctx;
266 union {
267 struct {
268 X509 st_x509;
269 X509_CINF st_x509_cinf;
270 } x509;
271 struct {
272 X509_CRL st_crl;
273 X509_CRL_INFO st_crl_info;
274 } crl;
275 } data;
276 int ok = 0;
277 size_t i;
278 int j, k;
279 unsigned long h;
280 unsigned long hash_array[2];
281 int hash_index;
282 BUF_MEM *b = NULL;
283 X509_OBJECT stmp, *tmp;
284 const char *postfix = "";
285
286 if (name == NULL)
287 return (0);
288
289 stmp.type = type;
290 if (type == X509_LU_X509) {
291 data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
292 data.x509.st_x509_cinf.subject = name;
293 stmp.data.x509 = &data.x509.st_x509;
294 postfix = "";
295 } else if (type == X509_LU_CRL) {
296 data.crl.st_crl.crl = &data.crl.st_crl_info;
297 data.crl.st_crl_info.issuer = name;
298 stmp.data.crl = &data.crl.st_crl;
299 postfix = "r";
300 } else {
301 OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE);
302 goto finish;
303 }
304
305 if ((b = BUF_MEM_new()) == NULL) {
306 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
307 goto finish;
308 }
309
310 ctx = (BY_DIR *)xl->method_data;
311
312 hash_array[0] = X509_NAME_hash(name);
313 hash_array[1] = X509_NAME_hash_old(name);
314 for (hash_index = 0; hash_index < 2; ++hash_index) {
315 h = hash_array[hash_index];
316 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
317 BY_DIR_ENTRY *ent;
318 size_t idx;
319 BY_DIR_HASH htmp, *hent;
320 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
321 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
322 if (!BUF_MEM_grow(b, j)) {
323 OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
324 goto finish;
325 }
326 if (type == X509_LU_CRL && ent->hashes) {
327 htmp.hash = h;
328 CRYPTO_STATIC_MUTEX_lock_read(&g_ent_hashes_lock);
329 if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) {
330 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
331 k = hent->suffix;
332 } else {
333 hent = NULL;
334 k = 0;
335 }
336 CRYPTO_STATIC_MUTEX_unlock_read(&g_ent_hashes_lock);
337 } else {
338 k = 0;
339 hent = NULL;
340 }
341 for (;;) {
342 char c = '/';
343 #ifdef OPENSSL_SYS_VMS
344 c = ent->dir[strlen(ent->dir) - 1];
345 if (c != ':' && c != '>' && c != ']') {
346 /*
347 * If no separator is present, we assume the directory
348 * specifier is a logical name, and add a colon. We
349 * really should use better VMS routines for merging
350 * things like this, but this will do for now... --
351 * Richard Levitte
352 */
353 c = ':';
354 } else {
355 c = '\0';
356 }
357 #endif
358 if (c == '\0') {
359 /*
360 * This is special. When c == '\0', no directory
361 * separator should be added.
362 */
363 BIO_snprintf(b->data, b->max,
364 "%s%08lx.%s%d", ent->dir, h, postfix, k);
365 } else {
366 BIO_snprintf(b->data, b->max,
367 "%s%c%08lx.%s%d", ent->dir, c, h,
368 postfix, k);
369 }
370 #ifndef OPENSSL_NO_POSIX_IO
371 # ifdef _WIN32
372 # define stat _stat
373 # endif
374 {
375 struct stat st;
376 if (stat(b->data, &st) < 0)
377 break;
378 }
379 #endif
380 /* found one. */
381 if (type == X509_LU_X509) {
382 if ((X509_load_cert_file(xl, b->data,
383 ent->dir_type)) == 0)
384 break;
385 } else if (type == X509_LU_CRL) {
386 if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0)
387 break;
388 }
389 /* else case will caught higher up */
390 k++;
391 }
392
393 /*
394 * we have added it to the cache so now pull it out again
395 */
396 CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock);
397 tmp = NULL;
398 if (sk_X509_OBJECT_find(xl->store_ctx->objs, &idx, &stmp)) {
399 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, idx);
400 }
401 CRYPTO_MUTEX_unlock_write(&xl->store_ctx->objs_lock);
402
403 /*
404 * If a CRL, update the last file suffix added for this
405 */
406
407 if (type == X509_LU_CRL) {
408 CRYPTO_STATIC_MUTEX_lock_write(&g_ent_hashes_lock);
409 /*
410 * Look for entry again in case another thread added an entry
411 * first.
412 */
413 if (!hent) {
414 htmp.hash = h;
415 if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp))
416 hent = sk_BY_DIR_HASH_value(ent->hashes, idx);
417 }
418 if (!hent) {
419 hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
420 if (hent == NULL) {
421 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
422 ok = 0;
423 goto finish;
424 }
425 hent->hash = h;
426 hent->suffix = k;
427 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
428 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
429 OPENSSL_free(hent);
430 ok = 0;
431 goto finish;
432 }
433 } else if (hent->suffix < k)
434 hent->suffix = k;
435
436 CRYPTO_STATIC_MUTEX_unlock_write(&g_ent_hashes_lock);
437 }
438
439 if (tmp != NULL) {
440 ok = 1;
441 ret->type = tmp->type;
442 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
443 /*
444 * If we were going to up the reference count, we would need
445 * to do it on a perl 'type' basis
446 */
447 /*
448 * CRYPTO_add(&tmp->data.x509->references,1,
449 * CRYPTO_LOCK_X509);
450 */
451 goto finish;
452 }
453 }
454 }
455 finish:
456 if (b != NULL)
457 BUF_MEM_free(b);
458 return (ok);
459 }