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