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