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