summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509/by_dir.c
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
committercvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
commitdecf84ba5550c1656a7fdb51b5b81969590c3f03 (patch)
tree44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/x509/by_dir.c
parent7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff)
downloadopenbsd-libressl-v2.1.5.tar.gz
openbsd-libressl-v2.1.5.tar.bz2
openbsd-libressl-v2.1.5.zip
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libcrypto/x509/by_dir.c')
-rw-r--r--src/lib/libcrypto/x509/by_dir.c428
1 files changed, 0 insertions, 428 deletions
diff --git a/src/lib/libcrypto/x509/by_dir.c b/src/lib/libcrypto/x509/by_dir.c
deleted file mode 100644
index 032210424d..0000000000
--- a/src/lib/libcrypto/x509/by_dir.c
+++ /dev/null
@@ -1,428 +0,0 @@
1/* $OpenBSD: by_dir.c,v 1.36 2015/02/12 03:54:07 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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
59#include <sys/types.h>
60
61#include <errno.h>
62#include <stdio.h>
63#include <string.h>
64#include <time.h>
65#include <unistd.h>
66
67#include <openssl/opensslconf.h>
68
69#include <openssl/err.h>
70#include <openssl/lhash.h>
71#include <openssl/x509.h>
72
73# include <sys/stat.h>
74
75typedef struct lookup_dir_hashes_st {
76 unsigned long hash;
77 int suffix;
78} BY_DIR_HASH;
79
80typedef struct lookup_dir_entry_st {
81 char *dir;
82 int dir_type;
83 STACK_OF(BY_DIR_HASH) *hashes;
84} BY_DIR_ENTRY;
85
86typedef struct lookup_dir_st {
87 BUF_MEM *buffer;
88 STACK_OF(BY_DIR_ENTRY) *dirs;
89} BY_DIR;
90
91DECLARE_STACK_OF(BY_DIR_HASH)
92DECLARE_STACK_OF(BY_DIR_ENTRY)
93
94static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
95 char **ret);
96static int new_dir(X509_LOOKUP *lu);
97static void free_dir(X509_LOOKUP *lu);
98static int add_cert_dir(BY_DIR *ctx, const char *dir, int type);
99static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
100 X509_OBJECT *ret);
101
102static X509_LOOKUP_METHOD x509_dir_lookup = {
103 .name = "Load certs from files in a directory",
104 .new_item = new_dir,
105 .free = free_dir,
106 .init = NULL,
107 .shutdown = NULL,
108 .ctrl = dir_ctrl,
109 .get_by_subject = get_cert_by_subject,
110 .get_by_issuer_serial = NULL,
111 .get_by_fingerprint = NULL,
112 .get_by_alias = NULL,
113};
114
115X509_LOOKUP_METHOD *
116X509_LOOKUP_hash_dir(void)
117{
118 return (&x509_dir_lookup);
119}
120
121static int
122dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
123 char **retp)
124{
125 int ret = 0;
126 BY_DIR *ld;
127 char *dir = NULL;
128
129 ld = (BY_DIR *)ctx->method_data;
130
131 switch (cmd) {
132 case X509_L_ADD_DIR:
133 if (argl == X509_FILETYPE_DEFAULT) {
134 if (issetugid() == 0)
135 dir = getenv(X509_get_default_cert_dir_env());
136 if (dir)
137 ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM);
138 else
139 ret = add_cert_dir(ld, X509_get_default_cert_dir(),
140 X509_FILETYPE_PEM);
141 if (!ret) {
142 X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR);
143 }
144 } else
145 ret = add_cert_dir(ld, argp, (int)argl);
146 break;
147 }
148 return (ret);
149}
150
151static int
152new_dir(X509_LOOKUP *lu)
153{
154 BY_DIR *a;
155
156 if ((a = malloc(sizeof(BY_DIR))) == NULL)
157 return (0);
158 if ((a->buffer = BUF_MEM_new()) == NULL) {
159 free(a);
160 return (0);
161 }
162 a->dirs = NULL;
163 lu->method_data = (char *)a;
164 return (1);
165}
166
167static void
168by_dir_hash_free(BY_DIR_HASH *hash)
169{
170 free(hash);
171}
172
173static int
174by_dir_hash_cmp(const BY_DIR_HASH * const *a,
175 const BY_DIR_HASH * const *b)
176{
177 if ((*a)->hash > (*b)->hash)
178 return 1;
179 if ((*a)->hash < (*b)->hash)
180 return -1;
181 return 0;
182}
183
184static void
185by_dir_entry_free(BY_DIR_ENTRY *ent)
186{
187 free(ent->dir);
188 if (ent->hashes)
189 sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free);
190 free(ent);
191}
192
193static void
194free_dir(X509_LOOKUP *lu)
195{
196 BY_DIR *a;
197
198 a = (BY_DIR *)lu->method_data;
199 if (a->dirs != NULL)
200 sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free);
201 if (a->buffer != NULL)
202 BUF_MEM_free(a->buffer);
203 free(a);
204}
205
206static int
207add_cert_dir(BY_DIR *ctx, const char *dir, int type)
208{
209 int j;
210 const char *s, *ss, *p;
211 ptrdiff_t len;
212
213 if (dir == NULL || !*dir) {
214 X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY);
215 return 0;
216 }
217
218 s = dir;
219 p = s;
220 do {
221 if ((*p == ':') || (*p == '\0')) {
222 BY_DIR_ENTRY *ent;
223 ss = s;
224 s = p + 1;
225 len = p - ss;
226 if (len == 0)
227 continue;
228 for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) {
229 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
230 if (strlen(ent->dir) == (size_t)len &&
231 strncmp(ent->dir, ss, (size_t)len) == 0)
232 break;
233 }
234 if (j < sk_BY_DIR_ENTRY_num(ctx->dirs))
235 continue;
236 if (ctx->dirs == NULL) {
237 ctx->dirs = sk_BY_DIR_ENTRY_new_null();
238 if (!ctx->dirs) {
239 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
240 return 0;
241 }
242 }
243 ent = malloc(sizeof(BY_DIR_ENTRY));
244 if (!ent) {
245 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
246 return 0;
247 }
248 ent->dir_type = type;
249 ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp);
250 ent->dir = strndup(ss, (size_t)len);
251 if (!ent->dir || !ent->hashes) {
252 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
253 by_dir_entry_free(ent);
254 return 0;
255 }
256 if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) {
257 X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE);
258 by_dir_entry_free(ent);
259 return 0;
260 }
261 }
262 } while (*p++ != '\0');
263 return 1;
264}
265
266static int
267get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
268 X509_OBJECT *ret)
269{
270 BY_DIR *ctx;
271 union {
272 struct {
273 X509 st_x509;
274 X509_CINF st_x509_cinf;
275 } x509;
276 struct {
277 X509_CRL st_crl;
278 X509_CRL_INFO st_crl_info;
279 } crl;
280 } data;
281 int ok = 0;
282 int i, j, k;
283 unsigned long h;
284 BUF_MEM *b = NULL;
285 X509_OBJECT stmp, *tmp;
286 const char *postfix="";
287
288 if (name == NULL)
289 return (0);
290
291 stmp.type = type;
292 if (type == X509_LU_X509) {
293 data.x509.st_x509.cert_info = &data.x509.st_x509_cinf;
294 data.x509.st_x509_cinf.subject = name;
295 stmp.data.x509 = &data.x509.st_x509;
296 postfix="";
297 } else if (type == X509_LU_CRL) {
298 data.crl.st_crl.crl = &data.crl.st_crl_info;
299 data.crl.st_crl_info.issuer = name;
300 stmp.data.crl = &data.crl.st_crl;
301 postfix="r";
302 } else {
303 X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE);
304 goto finish;
305 }
306
307 if ((b = BUF_MEM_new()) == NULL) {
308 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB);
309 goto finish;
310 }
311
312 ctx = (BY_DIR *)xl->method_data;
313
314 h = X509_NAME_hash(name);
315 for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) {
316 BY_DIR_ENTRY *ent;
317 int idx;
318 BY_DIR_HASH htmp, *hent;
319 ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i);
320 j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1;
321 if (!BUF_MEM_grow(b, j)) {
322 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
323 goto finish;
324 }
325 if (type == X509_LU_CRL && ent->hashes) {
326 htmp.hash = h;
327 CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE);
328 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
329 if (idx >= 0) {
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_r_unlock(CRYPTO_LOCK_X509_STORE);
337 } else {
338 k = 0;
339 hent = NULL;
340 }
341 for (;;) {
342 (void) snprintf(b->data, b->max, "%s/%08lx.%s%d",
343 ent->dir, h, postfix, k);
344
345 {
346 struct stat st;
347 if (stat(b->data, &st) < 0)
348 break;
349 }
350 /* found one. */
351 if (type == X509_LU_X509) {
352 if ((X509_load_cert_file(xl, b->data,
353 ent->dir_type)) == 0)
354 break;
355 } else if (type == X509_LU_CRL) {
356 if ((X509_load_crl_file(xl, b->data,
357 ent->dir_type)) == 0)
358 break;
359 }
360 /* else case will caught higher up */
361 k++;
362 }
363
364 /* we have added it to the cache so now pull it out again */
365 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
366 j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp);
367 if (j != -1)
368 tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j);
369 else
370 tmp = NULL;
371 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
372
373 /* If a CRL, update the last file suffix added for this */
374 if (type == X509_LU_CRL) {
375 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
376 /*
377 * Look for entry again in case another thread added
378 * an entry first.
379 */
380 if (!hent) {
381 htmp.hash = h;
382 idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp);
383 if (idx >= 0)
384 hent = sk_BY_DIR_HASH_value(
385 ent->hashes, idx);
386 }
387 if (!hent) {
388 hent = malloc(sizeof(BY_DIR_HASH));
389 if (!hent) {
390 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
391 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
392 ok = 0;
393 goto finish;
394 }
395 hent->hash = h;
396 hent->suffix = k;
397 if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) {
398 X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE);
399 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
400 free(hent);
401 ok = 0;
402 goto finish;
403 }
404 } else if (hent->suffix < k)
405 hent->suffix = k;
406
407 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
408
409 }
410
411 if (tmp != NULL) {
412 ok = 1;
413 ret->type = tmp->type;
414 memcpy(&ret->data, &tmp->data, sizeof(ret->data));
415 /*
416 * If we were going to up the reference count,
417 * we would need to do it on a perl 'type' basis
418 */
419 /* CRYPTO_add(&tmp->data.x509->references,1,
420 CRYPTO_LOCK_X509);*/
421 goto finish;
422 }
423 }
424finish:
425 if (b != NULL)
426 BUF_MEM_free(b);
427 return (ok);
428}