| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <inttypes.h> |
| 5 |
#include <string.h> |
| 6 |
#include <sys/stat.h> |
| 7 |
#include <sys/types.h> |
| 8 |
|
| 9 |
#include <openssl/buf.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/thread.h> |
| 13 |
#include <openssl/x509.h> |
| 14 |
|
| 15 |
#include "../internal.h" |
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
typedef struct lookup_dir_hashes_st { |
| 19 |
uint32_t hash; |
| 20 |
int suffix; |
| 21 |
} BY_DIR_HASH; |
| 22 |
|
| 23 |
typedef struct lookup_dir_entry_st { |
| 24 |
CRYPTO_MUTEX lock; |
| 25 |
char *dir; |
| 26 |
int dir_type; |
| 27 |
STACK_OF(BY_DIR_HASH) *hashes; |
| 28 |
} BY_DIR_ENTRY; |
| 29 |
|
| 30 |
typedef struct lookup_dir_st { |
| 31 |
STACK_OF(BY_DIR_ENTRY) *dirs; |
| 32 |
} BY_DIR; |
| 33 |
|
| 34 |
DEFINE_STACK_OF(BY_DIR_HASH) |
| 35 |
DEFINE_STACK_OF(BY_DIR_ENTRY) |
| 36 |
|
| 37 |
static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, |
| 38 |
char **ret); |
| 39 |
static int new_dir(X509_LOOKUP *lu); |
| 40 |
static void free_dir(X509_LOOKUP *lu); |
| 41 |
static int add_cert_dir(BY_DIR *ctx, const char *dir, int type); |
| 42 |
static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, |
| 43 |
X509_OBJECT *ret); |
| 44 |
static const X509_LOOKUP_METHOD x509_dir_lookup = { |
| 45 |
new_dir, // new |
| 46 |
free_dir, // free |
| 47 |
dir_ctrl, // ctrl |
| 48 |
get_cert_by_subject, // get_by_subject |
| 49 |
}; |
| 50 |
|
| 51 |
const X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void) { |
| 52 |
return &x509_dir_lookup; |
| 53 |
} |
| 54 |
|
| 55 |
static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, |
| 56 |
char **retp) { |
| 57 |
int ret = 0; |
| 58 |
char *dir = NULL; |
| 59 |
|
| 60 |
BY_DIR *ld = ctx->method_data; |
| 61 |
|
| 62 |
switch (cmd) { |
| 63 |
case X509_L_ADD_DIR: |
| 64 |
if (argl == X509_FILETYPE_DEFAULT) { |
| 65 |
dir = (char *)getenv(X509_get_default_cert_dir_env()); |
| 66 |
if (dir) { |
| 67 |
ret = add_cert_dir(ld, dir, X509_FILETYPE_PEM); |
| 68 |
} else { |
| 69 |
ret = |
| 70 |
add_cert_dir(ld, X509_get_default_cert_dir(), X509_FILETYPE_PEM); |
| 71 |
} |
| 72 |
if (!ret) { |
| 73 |
OPENSSL_PUT_ERROR(X509, X509_R_LOADING_CERT_DIR); |
| 74 |
} |
| 75 |
} else { |
| 76 |
ret = add_cert_dir(ld, argp, (int)argl); |
| 77 |
} |
| 78 |
break; |
| 79 |
} |
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
static int new_dir(X509_LOOKUP *lu) { |
| 84 |
BY_DIR *a; |
| 85 |
|
| 86 |
if ((a = (BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL) { |
| 87 |
return 0; |
| 88 |
} |
| 89 |
a->dirs = NULL; |
| 90 |
lu->method_data = a; |
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
static void by_dir_hash_free(BY_DIR_HASH *hash) { OPENSSL_free(hash); } |
| 95 |
|
| 96 |
static int by_dir_hash_cmp(const BY_DIR_HASH *const *a, |
| 97 |
const BY_DIR_HASH *const *b) { |
| 98 |
if ((*a)->hash > (*b)->hash) { |
| 99 |
return 1; |
| 100 |
} |
| 101 |
if ((*a)->hash < (*b)->hash) { |
| 102 |
return -1; |
| 103 |
} |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
static void by_dir_entry_free(BY_DIR_ENTRY *ent) { |
| 108 |
if (ent != NULL) { |
| 109 |
CRYPTO_MUTEX_cleanup(&ent->lock); |
| 110 |
OPENSSL_free(ent->dir); |
| 111 |
sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free); |
| 112 |
OPENSSL_free(ent); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static void free_dir(X509_LOOKUP *lu) { |
| 117 |
BY_DIR *a = lu->method_data; |
| 118 |
if (a != NULL) { |
| 119 |
sk_BY_DIR_ENTRY_pop_free(a->dirs, by_dir_entry_free); |
| 120 |
OPENSSL_free(a); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) { |
| 125 |
size_t j, len; |
| 126 |
const char *s, *ss, *p; |
| 127 |
|
| 128 |
if (dir == NULL || !*dir) { |
| 129 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_DIRECTORY); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
s = dir; |
| 134 |
p = s; |
| 135 |
do { |
| 136 |
if ((*p == ':') || (*p == '\0')) { |
| 137 |
BY_DIR_ENTRY *ent; |
| 138 |
ss = s; |
| 139 |
s = p + 1; |
| 140 |
len = p - ss; |
| 141 |
if (len == 0) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
for (j = 0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++) { |
| 145 |
ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j); |
| 146 |
if (strlen(ent->dir) == len && strncmp(ent->dir, ss, len) == 0) { |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
if (j < sk_BY_DIR_ENTRY_num(ctx->dirs)) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
if (ctx->dirs == NULL) { |
| 154 |
ctx->dirs = sk_BY_DIR_ENTRY_new_null(); |
| 155 |
if (!ctx->dirs) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
} |
| 159 |
ent = OPENSSL_malloc(sizeof(BY_DIR_ENTRY)); |
| 160 |
if (!ent) { |
| 161 |
return 0; |
| 162 |
} |
| 163 |
CRYPTO_MUTEX_init(&ent->lock); |
| 164 |
ent->dir_type = type; |
| 165 |
ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); |
| 166 |
ent->dir = OPENSSL_strndup(ss, len); |
| 167 |
if (ent->dir == NULL || ent->hashes == NULL || |
| 168 |
!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { |
| 169 |
by_dir_entry_free(ent); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
} |
| 173 |
} while (*p++ != '\0'); |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, |
| 178 |
X509_OBJECT *ret) { |
| 179 |
union { |
| 180 |
struct { |
| 181 |
X509 st_x509; |
| 182 |
X509_CINF st_x509_cinf; |
| 183 |
} x509; |
| 184 |
struct { |
| 185 |
X509_CRL st_crl; |
| 186 |
X509_CRL_INFO st_crl_info; |
| 187 |
} crl; |
| 188 |
} data; |
| 189 |
int ok = 0; |
| 190 |
size_t i; |
| 191 |
int j, k; |
| 192 |
uint32_t h; |
| 193 |
uint32_t hash_array[2]; |
| 194 |
int hash_index; |
| 195 |
BUF_MEM *b = NULL; |
| 196 |
X509_OBJECT stmp, *tmp; |
| 197 |
const char *postfix; |
| 198 |
|
| 199 |
if (name == NULL) { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
stmp.type = type; |
| 204 |
if (type == X509_LU_X509) { |
| 205 |
data.x509.st_x509.cert_info = &data.x509.st_x509_cinf; |
| 206 |
data.x509.st_x509_cinf.subject = name; |
| 207 |
stmp.data.x509 = &data.x509.st_x509; |
| 208 |
postfix = ""; |
| 209 |
} else if (type == X509_LU_CRL) { |
| 210 |
data.crl.st_crl.crl = &data.crl.st_crl_info; |
| 211 |
data.crl.st_crl_info.issuer = name; |
| 212 |
stmp.data.crl = &data.crl.st_crl; |
| 213 |
postfix = "r"; |
| 214 |
} else { |
| 215 |
OPENSSL_PUT_ERROR(X509, X509_R_WRONG_LOOKUP_TYPE); |
| 216 |
goto finish; |
| 217 |
} |
| 218 |
|
| 219 |
if ((b = BUF_MEM_new()) == NULL) { |
| 220 |
OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB); |
| 221 |
goto finish; |
| 222 |
} |
| 223 |
|
| 224 |
BY_DIR *ctx = xl->method_data; |
| 225 |
|
| 226 |
hash_array[0] = X509_NAME_hash(name); |
| 227 |
hash_array[1] = X509_NAME_hash_old(name); |
| 228 |
for (hash_index = 0; hash_index < 2; ++hash_index) { |
| 229 |
h = hash_array[hash_index]; |
| 230 |
for (i = 0; i < sk_BY_DIR_ENTRY_num(ctx->dirs); i++) { |
| 231 |
BY_DIR_ENTRY *ent; |
| 232 |
size_t idx; |
| 233 |
BY_DIR_HASH htmp, *hent; |
| 234 |
ent = sk_BY_DIR_ENTRY_value(ctx->dirs, i); |
| 235 |
j = strlen(ent->dir) + 1 + 8 + 6 + 1 + 1; |
| 236 |
if (!BUF_MEM_grow(b, j)) { |
| 237 |
goto finish; |
| 238 |
} |
| 239 |
if (type == X509_LU_CRL && ent->hashes) { |
| 240 |
htmp.hash = h; |
| 241 |
CRYPTO_MUTEX_lock_read(&ent->lock); |
| 242 |
if (sk_BY_DIR_HASH_find_awslc(ent->hashes, &idx, &htmp)) { |
| 243 |
hent = sk_BY_DIR_HASH_value(ent->hashes, idx); |
| 244 |
k = hent->suffix; |
| 245 |
} else { |
| 246 |
hent = NULL; |
| 247 |
k = 0; |
| 248 |
} |
| 249 |
CRYPTO_MUTEX_unlock_read(&ent->lock); |
| 250 |
} else { |
| 251 |
k = 0; |
| 252 |
hent = NULL; |
| 253 |
} |
| 254 |
for (;;) { |
| 255 |
snprintf(b->data, b->max, "%s/%08" PRIx32 ".%s%d", ent->dir, h, postfix, |
| 256 |
k); |
| 257 |
if (type == X509_LU_X509) { |
| 258 |
if ((X509_load_cert_file(xl, b->data, ent->dir_type)) == 0) { |
| 259 |
// Don't expose the lower level error, All of these boil |
| 260 |
// down to "we could not find a CA". |
| 261 |
ERR_clear_error(); |
| 262 |
break; |
| 263 |
} |
| 264 |
} else if (type == X509_LU_CRL) { |
| 265 |
if ((X509_load_crl_file(xl, b->data, ent->dir_type)) == 0) { |
| 266 |
// Don't expose the lower level error, All of these boil |
| 267 |
// down to "we could not find a CRL". |
| 268 |
ERR_clear_error(); |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
// The lack of a CA or CRL will be caught higher up |
| 273 |
k++; |
| 274 |
} |
| 275 |
|
| 276 |
// we have added it to the cache so now pull it out again |
| 277 |
CRYPTO_MUTEX_lock_write(&xl->store_ctx->objs_lock); |
| 278 |
tmp = NULL; |
| 279 |
sk_X509_OBJECT_sort(xl->store_ctx->objs); |
| 280 |
if (sk_X509_OBJECT_find_awslc(xl->store_ctx->objs, &idx, &stmp)) { |
| 281 |
tmp = sk_X509_OBJECT_value(xl->store_ctx->objs, idx); |
| 282 |
} |
| 283 |
CRYPTO_MUTEX_unlock_write(&xl->store_ctx->objs_lock); |
| 284 |
|
| 285 |
// If a CRL, update the last file suffix added for this |
| 286 |
|
| 287 |
if (type == X509_LU_CRL) { |
| 288 |
CRYPTO_MUTEX_lock_write(&ent->lock); |
| 289 |
// Look for entry again in case another thread added an entry |
| 290 |
// first. |
| 291 |
if (!hent) { |
| 292 |
htmp.hash = h; |
| 293 |
sk_BY_DIR_HASH_sort(ent->hashes); |
| 294 |
if (sk_BY_DIR_HASH_find_awslc(ent->hashes, &idx, &htmp)) { |
| 295 |
hent = sk_BY_DIR_HASH_value(ent->hashes, idx); |
| 296 |
} |
| 297 |
} |
| 298 |
if (!hent) { |
| 299 |
hent = OPENSSL_malloc(sizeof(BY_DIR_HASH)); |
| 300 |
if (hent == NULL) { |
| 301 |
CRYPTO_MUTEX_unlock_write(&ent->lock); |
| 302 |
ok = 0; |
| 303 |
goto finish; |
| 304 |
} |
| 305 |
hent->hash = h; |
| 306 |
hent->suffix = k; |
| 307 |
if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) { |
| 308 |
CRYPTO_MUTEX_unlock_write(&ent->lock); |
| 309 |
OPENSSL_free(hent); |
| 310 |
ok = 0; |
| 311 |
goto finish; |
| 312 |
} |
| 313 |
sk_BY_DIR_HASH_sort(ent->hashes); |
| 314 |
} else if (hent->suffix < k) { |
| 315 |
hent->suffix = k; |
| 316 |
} |
| 317 |
|
| 318 |
CRYPTO_MUTEX_unlock_write(&ent->lock); |
| 319 |
} |
| 320 |
|
| 321 |
if (tmp != NULL) { |
| 322 |
ok = 1; |
| 323 |
ret->type = tmp->type; |
| 324 |
OPENSSL_memcpy(&ret->data, &tmp->data, sizeof(ret->data)); |
| 325 |
|
| 326 |
// Clear any errors that might have been raised processing empty |
| 327 |
// or malformed files. |
| 328 |
ERR_clear_error(); |
| 329 |
|
| 330 |
// If we were going to up the reference count, we would need |
| 331 |
// to do it on a perl 'type' basis |
| 332 |
goto finish; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
finish: |
| 337 |
BUF_MEM_free(b); |
| 338 |
return ok; |
| 339 |
} |
| 340 |
|
| 341 |
int X509_LOOKUP_add_dir(X509_LOOKUP *lookup, const char *name, int type) { |
| 342 |
return X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, name, type, NULL); |
| 343 |
} |