/src/openssl/crypto/x509/by_dir.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "e_os.h" |
11 | | #include "internal/cryptlib.h" |
12 | | #include <stdio.h> |
13 | | #include <time.h> |
14 | | #include <errno.h> |
15 | | #include <sys/types.h> |
16 | | |
17 | | #ifndef OPENSSL_NO_POSIX_IO |
18 | | # include <sys/stat.h> |
19 | | #endif |
20 | | |
21 | | #include <openssl/x509.h> |
22 | | #include "internal/x509_int.h" |
23 | | #include "x509_lcl.h" |
24 | | |
25 | | struct lookup_dir_hashes_st { |
26 | | unsigned long hash; |
27 | | int suffix; |
28 | | }; |
29 | | |
30 | | struct lookup_dir_entry_st { |
31 | | char *dir; |
32 | | int dir_type; |
33 | | STACK_OF(BY_DIR_HASH) *hashes; |
34 | | }; |
35 | | |
36 | | typedef struct lookup_dir_st { |
37 | | BUF_MEM *buffer; |
38 | | STACK_OF(BY_DIR_ENTRY) *dirs; |
39 | | CRYPTO_RWLOCK *lock; |
40 | | } BY_DIR; |
41 | | |
42 | | static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, |
43 | | char **ret); |
44 | | static int new_dir(X509_LOOKUP *lu); |
45 | | static void free_dir(X509_LOOKUP *lu); |
46 | | static int add_cert_dir(BY_DIR *ctx, const char *dir, int type); |
47 | | static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type, |
48 | | X509_NAME *name, X509_OBJECT *ret); |
49 | | static X509_LOOKUP_METHOD x509_dir_lookup = { |
50 | | "Load certs from files in a directory", |
51 | | new_dir, /* new_item */ |
52 | | free_dir, /* free */ |
53 | | NULL, /* init */ |
54 | | NULL, /* shutdown */ |
55 | | dir_ctrl, /* ctrl */ |
56 | | get_cert_by_subject, /* get_by_subject */ |
57 | | NULL, /* get_by_issuer_serial */ |
58 | | NULL, /* get_by_fingerprint */ |
59 | | NULL, /* get_by_alias */ |
60 | | }; |
61 | | |
62 | | X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void) |
63 | 0 | { |
64 | 0 | return &x509_dir_lookup; |
65 | 0 | } |
66 | | |
67 | | static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, |
68 | | char **retp) |
69 | 0 | { |
70 | 0 | int ret = 0; |
71 | 0 | BY_DIR *ld = (BY_DIR *)ctx->method_data; |
72 | 0 |
|
73 | 0 | switch (cmd) { |
74 | 0 | case X509_L_ADD_DIR: |
75 | 0 | if (argl == X509_FILETYPE_DEFAULT) { |
76 | 0 | const char *dir = getenv(X509_get_default_cert_dir_env()); |
77 | 0 |
|
78 | 0 | if (dir) |
79 | 0 | ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM); |
80 | 0 | else |
81 | 0 | ret = add_cert_dir(ld, X509_get_default_cert_dir(), |
82 | 0 | X509_FILETYPE_PEM); |
83 | 0 | if (!ret) { |
84 | 0 | X509err(X509_F_DIR_CTRL, X509_R_LOADING_CERT_DIR); |
85 | 0 | } |
86 | 0 | } else |
87 | 0 | ret = add_cert_dir(ld, argp, (int)argl); |
88 | 0 | break; |
89 | 0 | } |
90 | 0 | return ret; |
91 | 0 | } |
92 | | |
93 | | static int new_dir(X509_LOOKUP *lu) |
94 | 0 | { |
95 | 0 | BY_DIR *a = OPENSSL_malloc(sizeof(*a)); |
96 | 0 |
|
97 | 0 | if (a == NULL) { |
98 | 0 | X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE); |
99 | 0 | return 0; |
100 | 0 | } |
101 | 0 |
|
102 | 0 | if ((a->buffer = BUF_MEM_new()) == NULL) { |
103 | 0 | X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE); |
104 | 0 | goto err; |
105 | 0 | } |
106 | 0 | a->dirs = NULL; |
107 | 0 | a->lock = CRYPTO_THREAD_lock_new(); |
108 | 0 | if (a->lock == NULL) { |
109 | 0 | BUF_MEM_free(a->buffer); |
110 | 0 | X509err(X509_F_NEW_DIR, ERR_R_MALLOC_FAILURE); |
111 | 0 | goto err; |
112 | 0 | } |
113 | 0 | lu->method_data = a; |
114 | 0 | return 1; |
115 | 0 | |
116 | 0 | err: |
117 | 0 | OPENSSL_free(a); |
118 | 0 | return 0; |
119 | 0 | } |
120 | | |
121 | | static void by_dir_hash_free(BY_DIR_HASH *hash) |
122 | 0 | { |
123 | 0 | OPENSSL_free(hash); |
124 | 0 | } |
125 | | |
126 | | static int by_dir_hash_cmp(const BY_DIR_HASH *const *a, |
127 | | const BY_DIR_HASH *const *b) |
128 | 0 | { |
129 | 0 | if ((*a)->hash > (*b)->hash) |
130 | 0 | return 1; |
131 | 0 | if ((*a)->hash < (*b)->hash) |
132 | 0 | return -1; |
133 | 0 | return 0; |
134 | 0 | } |
135 | | |
136 | | static void by_dir_entry_free(BY_DIR_ENTRY *ent) |
137 | 0 | { |
138 | 0 | OPENSSL_free(ent->dir); |
139 | 0 | sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free); |
140 | 0 | OPENSSL_free(ent); |
141 | 0 | } |
142 | | |
143 | | static void free_dir(X509_LOOKUP *lu) |
144 | 0 | { |
145 | 0 | BY_DIR *a = (BY_DIR *)lu->method_data; |
146 | 0 |
|
147 | 0 | sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free); |
148 | 0 | BUF_MEM_free(a->buffer); |
149 | 0 | CRYPTO_THREAD_lock_free(a->lock); |
150 | 0 | OPENSSL_free(a); |
151 | 0 | } |
152 | | |
153 | | static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) |
154 | 0 | { |
155 | 0 | int j; |
156 | 0 | size_t len; |
157 | 0 | const char *s, *ss, *p; |
158 | 0 |
|
159 | 0 | if (dir == NULL || !*dir) { |
160 | 0 | X509err(X509_F_ADD_CERT_DIR, X509_R_INVALID_DIRECTORY); |
161 | 0 | return 0; |
162 | 0 | } |
163 | 0 |
|
164 | 0 | s = dir; |
165 | 0 | p = s; |
166 | 0 | do { |
167 | 0 | if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) { |
168 | 0 | BY_DIR_ENTRY *ent; |
169 | 0 |
|
170 | 0 | ss = s; |
171 | 0 | s = p + 1; |
172 | 0 | len = p - ss; |
173 | 0 | if (len == 0) |
174 | 0 | continue; |
175 | 0 | for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) { |
176 | 0 | ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j); |
177 | 0 | if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0) |
178 | 0 | break; |
179 | 0 | } |
180 | 0 | if (j < sk_BY_DIR_ENTRY_num(ctx->dirs)) |
181 | 0 | continue; |
182 | 0 | if (ctx->dirs == NULL) { |
183 | 0 | ctx->dirs = sk_BY_DIR_ENTRY_new_null(); |
184 | 0 | if (!ctx->dirs) { |
185 | 0 | X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | } |
189 | 0 | ent = OPENSSL_malloc(sizeof(*ent)); |
190 | 0 | if (ent == NULL) { |
191 | 0 | X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 | ent->dir_type = type; |
195 | 0 | ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); |
196 | 0 | ent->dir = OPENSSL_strndup(ss, len); |
197 | 0 | if (ent->dir == NULL || ent->hashes == NULL) { |
198 | 0 | by_dir_entry_free(ent); |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { |
202 | 0 | by_dir_entry_free(ent); |
203 | 0 | X509err(X509_F_ADD_CERT_DIR, ERR_R_MALLOC_FAILURE); |
204 | 0 | return 0; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } while (*p++ != '\0'); |
208 | 0 | return 1; |
209 | 0 | } |
210 | | |
211 | | static int get_cert_by_subject(X509_LOOKUP *xl, X509_LOOKUP_TYPE type, |
212 | | X509_NAME *name, X509_OBJECT *ret) |
213 | 0 | { |
214 | 0 | BY_DIR *ctx; |
215 | 0 | union { |
216 | 0 | X509 st_x509; |
217 | 0 | X509_CRL crl; |
218 | 0 | } data; |
219 | 0 | int ok = 0; |
220 | 0 | int i, j, k; |
221 | 0 | unsigned long h; |
222 | 0 | BUF_MEM *b = NULL; |
223 | 0 | X509_OBJECT stmp, *tmp; |
224 | 0 | const char *postfix = ""; |
225 | 0 |
|
226 | 0 | if (name == NULL) |
227 | 0 | return 0; |
228 | 0 | |
229 | 0 | stmp.type = type; |
230 | 0 | if (type == X509_LU_X509) { |
231 | 0 | data.st_x509.cert_info.subject = name; |
232 | 0 | stmp.data.x509 = &data.st_x509; |
233 | 0 | postfix = ""; |
234 | 0 | } else if (type == X509_LU_CRL) { |
235 | 0 | data.crl.crl.issuer = name; |
236 | 0 | stmp.data.crl = &data.crl; |
237 | 0 | postfix = "r"; |
238 | 0 | } else { |
239 | 0 | X509err(X509_F_GET_CERT_BY_SUBJECT, X509_R_WRONG_LOOKUP_TYPE); |
240 | 0 | goto finish; |
241 | 0 | } |
242 | 0 |
|
243 | 0 | if ((b = BUF_MEM_new()) == NULL) { |
244 | 0 | X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_BUF_LIB); |
245 | 0 | goto finish; |
246 | 0 | } |
247 | 0 |
|
248 | 0 | ctx = (BY_DIR *)xl->method_data; |
249 | 0 |
|
250 | 0 | h = X509_NAME_hash(name); |
251 | 0 | for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) { |
252 | 0 | BY_DIR_ENTRY *ent; |
253 | 0 | int idx; |
254 | 0 | BY_DIR_HASH htmp, *hent; |
255 | 0 |
|
256 | 0 | ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i); |
257 | 0 | j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1; |
258 | 0 | if (!BUF_MEM_grow(b, j)) { |
259 | 0 | X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE); |
260 | 0 | goto finish; |
261 | 0 | } |
262 | 0 | if (type == X509_LU_CRL && ent->hashes) { |
263 | 0 | htmp.hash = h; |
264 | 0 | CRYPTO_THREAD_read_lock(ctx->lock); |
265 | 0 | idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp); |
266 | 0 | if (idx >= 0) { |
267 | 0 | hent = sk_BY_DIR_HASH_value(ent->hashes, idx); |
268 | 0 | k = hent->suffix; |
269 | 0 | } else { |
270 | 0 | hent = NULL; |
271 | 0 | k = 0; |
272 | 0 | } |
273 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
274 | 0 | } else { |
275 | 0 | k = 0; |
276 | 0 | hent = NULL; |
277 | 0 | } |
278 | 0 | for (;;) { |
279 | 0 | char c = '/'; |
280 | | #ifdef OPENSSL_SYS_VMS |
281 | | c = ent->dir[strlen(ent->dir) - 1]; |
282 | | if (c != ':' && c != '>' && c != ']') { |
283 | | /* |
284 | | * If no separator is present, we assume the directory |
285 | | * specifier is a logical name, and add a colon. We really |
286 | | * should use better VMS routines for merging things like |
287 | | * this, but this will do for now... -- Richard Levitte |
288 | | */ |
289 | | c = ':'; |
290 | | } else { |
291 | | c = '\0'; |
292 | | } |
293 | | #endif |
294 | 0 | if (c == '\0') { |
295 | 0 | /* |
296 | 0 | * This is special. When c == '\0', no directory separator |
297 | 0 | * should be added. |
298 | 0 | */ |
299 | 0 | BIO_snprintf(b->data, b->max, |
300 | 0 | "%s%08lx.%s%d", ent->dir, h, postfix, k); |
301 | 0 | } else { |
302 | 0 | BIO_snprintf(b->data, b->max, |
303 | 0 | "%s%c%08lx.%s%d", ent->dir, c, h, postfix, k); |
304 | 0 | } |
305 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
306 | | # ifdef _WIN32 |
307 | | # define stat _stat |
308 | | # endif |
309 | | { |
310 | 0 | struct stat st; |
311 | 0 | if (stat(b->data, &st) < 0) |
312 | 0 | break; |
313 | 0 | } |
314 | 0 | #endif |
315 | 0 | /* found one. */ |
316 | 0 | if (type == X509_LU_X509) { |
317 | 0 | if ((X509_load_cert_file(xl, b->data, ent->dir_type)) == 0) |
318 | 0 | break; |
319 | 0 | } else if (type == X509_LU_CRL) { |
320 | 0 | if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0) |
321 | 0 | break; |
322 | 0 | } |
323 | 0 | /* else case will caught higher up */ |
324 | 0 | k++; |
325 | 0 | } |
326 | 0 |
|
327 | 0 | /* |
328 | 0 | * we have added it to the cache so now pull it out again |
329 | 0 | */ |
330 | 0 | CRYPTO_THREAD_write_lock(ctx->lock); |
331 | 0 | j = sk_X509_OBJECT_find(xl->store_ctx->objs, &stmp); |
332 | 0 | tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, j); |
333 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
334 | 0 |
|
335 | 0 | /* If a CRL, update the last file suffix added for this */ |
336 | 0 |
|
337 | 0 | if (type == X509_LU_CRL) { |
338 | 0 | CRYPTO_THREAD_write_lock(ctx->lock); |
339 | 0 | /* |
340 | 0 | * Look for entry again in case another thread added an entry |
341 | 0 | * first. |
342 | 0 | */ |
343 | 0 | if (hent == NULL) { |
344 | 0 | htmp.hash = h; |
345 | 0 | idx = sk_BY_DIR_HASH_find(ent->hashes, &htmp); |
346 | 0 | hent = sk_BY_DIR_HASH_value(ent->hashes, idx); |
347 | 0 | } |
348 | 0 | if (hent == NULL) { |
349 | 0 | hent = OPENSSL_malloc(sizeof(*hent)); |
350 | 0 | if (hent == NULL) { |
351 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
352 | 0 | X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE); |
353 | 0 | ok = 0; |
354 | 0 | goto finish; |
355 | 0 | } |
356 | 0 | hent->hash = h; |
357 | 0 | hent->suffix = k; |
358 | 0 | if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) { |
359 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
360 | 0 | OPENSSL_free(hent); |
361 | 0 | X509err(X509_F_GET_CERT_BY_SUBJECT, ERR_R_MALLOC_FAILURE); |
362 | 0 | ok = 0; |
363 | 0 | goto finish; |
364 | 0 | } |
365 | 0 | } else if (hent->suffix < k) { |
366 | 0 | hent->suffix = k; |
367 | 0 | } |
368 | 0 |
|
369 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
370 | 0 |
|
371 | 0 | } |
372 | 0 |
|
373 | 0 | if (tmp != NULL) { |
374 | 0 | ok = 1; |
375 | 0 | ret->type = tmp->type; |
376 | 0 | memcpy(&ret->data, &tmp->data, sizeof(ret->data)); |
377 | 0 |
|
378 | 0 | /* |
379 | 0 | * Clear any errors that might have been raised processing empty |
380 | 0 | * or malformed files. |
381 | 0 | */ |
382 | 0 | ERR_clear_error(); |
383 | 0 |
|
384 | 0 | goto finish; |
385 | 0 | } |
386 | 0 | } |
387 | 0 | finish: |
388 | 0 | BUF_MEM_free(b); |
389 | 0 | return ok; |
390 | 0 | } |