| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include <openssl/err.h> |
| 7 |
#include <openssl/mem.h> |
| 8 |
#include <openssl/thread.h> |
| 9 |
#include <openssl/x509.h> |
| 10 |
|
| 11 |
#include "../internal.h" |
| 12 |
#include "internal.h" |
| 13 |
|
| 14 |
|
| 15 |
static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
| 16 |
X509_NAME *name); |
| 17 |
static X509_OBJECT *X509_OBJECT_retrieve_by_subject( |
| 18 |
STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name); |
| 19 |
static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
| 20 |
X509_OBJECT *x); |
| 21 |
static int X509_OBJECT_up_ref_count(X509_OBJECT *a); |
| 22 |
|
| 23 |
static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, |
| 24 |
X509_STORE *store); |
| 25 |
static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name, |
| 26 |
X509_OBJECT *ret); |
| 27 |
|
| 28 |
static X509_LOOKUP *X509_LOOKUP_new(const X509_LOOKUP_METHOD *method, |
| 29 |
X509_STORE *store) { |
| 30 |
X509_LOOKUP *ret = OPENSSL_zalloc(sizeof(X509_LOOKUP)); |
| 31 |
if (ret == NULL) { |
| 32 |
return NULL; |
| 33 |
} |
| 34 |
|
| 35 |
ret->method = method; |
| 36 |
ret->store_ctx = store; |
| 37 |
if ((method->new_item != NULL) && !method->new_item(ret)) { |
| 38 |
OPENSSL_free(ret); |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
return ret; |
| 42 |
} |
| 43 |
|
| 44 |
void X509_LOOKUP_free(X509_LOOKUP *ctx) { |
| 45 |
if (ctx == NULL) { |
| 46 |
return; |
| 47 |
} |
| 48 |
if (ctx->method != NULL && ctx->method->free != NULL) { |
| 49 |
(*ctx->method->free)(ctx); |
| 50 |
} |
| 51 |
OPENSSL_free(ctx); |
| 52 |
} |
| 53 |
|
| 54 |
int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, |
| 55 |
char **ret) { |
| 56 |
if (ctx->method == NULL) { |
| 57 |
return -1; |
| 58 |
} |
| 59 |
if (ctx->method->ctrl != NULL) { |
| 60 |
return ctx->method->ctrl(ctx, cmd, argc, argl, ret); |
| 61 |
} else { |
| 62 |
return 1; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name, |
| 67 |
X509_OBJECT *ret) { |
| 68 |
if (ctx->method == NULL || ctx->method->get_by_subject == NULL) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
// Note |get_by_subject| leaves |ret| in an inconsistent state. It has |
| 72 |
// pointers to an |X509| or |X509_CRL|, but has not bumped the refcount yet. |
| 73 |
// For now, the caller is expected to fix this, but ideally we'd fix the |
| 74 |
// |X509_LOOKUP| convention itself. |
| 75 |
return ctx->method->get_by_subject(ctx, type, name, ret) > 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int x509_object_cmp(const X509_OBJECT *a, const X509_OBJECT *b) { |
| 79 |
int ret = a->type - b->type; |
| 80 |
if (ret) { |
| 81 |
return ret; |
| 82 |
} |
| 83 |
switch (a->type) { |
| 84 |
case X509_LU_X509: |
| 85 |
return X509_subject_name_cmp(a->data.x509, b->data.x509); |
| 86 |
case X509_LU_CRL: |
| 87 |
return X509_CRL_cmp(a->data.crl, b->data.crl); |
| 88 |
default: |
| 89 |
// abort(); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static int x509_object_cmp_sk(const X509_OBJECT *const *a, |
| 95 |
const X509_OBJECT *const *b) { |
| 96 |
return x509_object_cmp(*a, *b); |
| 97 |
} |
| 98 |
|
| 99 |
static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
| 100 |
CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 101 |
|
| 102 |
X509_STORE *X509_STORE_new(void) { |
| 103 |
X509_STORE *ret = OPENSSL_zalloc(sizeof(X509_STORE)); |
| 104 |
if (ret == NULL) { |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
ret->references = 1; |
| 109 |
CRYPTO_MUTEX_init(&ret->objs_lock); |
| 110 |
CRYPTO_new_ex_data(&ret->ex_data); |
| 111 |
ret->objs = sk_X509_OBJECT_new(x509_object_cmp_sk); |
| 112 |
ret->get_cert_methods = sk_X509_LOOKUP_new_null(); |
| 113 |
ret->param = X509_VERIFY_PARAM_new(); |
| 114 |
if (ret->objs == NULL || |
| 115 |
ret->get_cert_methods == NULL || |
| 116 |
ret->param == NULL) { |
| 117 |
X509_STORE_free(ret); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
return ret; |
| 122 |
} |
| 123 |
|
| 124 |
int X509_STORE_lock(X509_STORE *v) { |
| 125 |
if (v == NULL) { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
CRYPTO_MUTEX_lock_write(&v->objs_lock); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
|
| 132 |
int X509_STORE_unlock(X509_STORE *v) { |
| 133 |
if (v == NULL) { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
CRYPTO_MUTEX_unlock_write(&v->objs_lock); |
| 137 |
return 1; |
| 138 |
} |
| 139 |
|
| 140 |
int X509_STORE_up_ref(X509_STORE *store) { |
| 141 |
CRYPTO_refcount_inc(&store->references); |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
void X509_STORE_free(X509_STORE *vfy) { |
| 146 |
if (vfy == NULL) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
if (!CRYPTO_refcount_dec_and_test_zero(&vfy->references)) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
CRYPTO_MUTEX_cleanup(&vfy->objs_lock); |
| 155 |
CRYPTO_free_ex_data(&g_ex_data_class, vfy, &vfy->ex_data); |
| 156 |
sk_X509_LOOKUP_pop_free(vfy->get_cert_methods, X509_LOOKUP_free); |
| 157 |
sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free); |
| 158 |
X509_VERIFY_PARAM_free(vfy->param); |
| 159 |
OPENSSL_free(vfy); |
| 160 |
} |
| 161 |
|
| 162 |
X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, const X509_LOOKUP_METHOD *m) { |
| 163 |
STACK_OF(X509_LOOKUP) *sk = v->get_cert_methods; |
| 164 |
for (size_t i = 0; i < sk_X509_LOOKUP_num(sk); i++) { |
| 165 |
X509_LOOKUP *lu = sk_X509_LOOKUP_value(sk, i); |
| 166 |
if (m == lu->method) { |
| 167 |
return lu; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
X509_LOOKUP *lu = X509_LOOKUP_new(m, v); |
| 172 |
if (lu == NULL || !sk_X509_LOOKUP_push(v->get_cert_methods, lu)) { |
| 173 |
X509_LOOKUP_free(lu); |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
|
| 177 |
return lu; |
| 178 |
} |
| 179 |
|
| 180 |
int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name, |
| 181 |
X509_OBJECT *ret) { |
| 182 |
X509_STORE *ctx = vs->ctx; |
| 183 |
X509_OBJECT stmp; |
| 184 |
CRYPTO_MUTEX_lock_write(&ctx->objs_lock); |
| 185 |
X509_OBJECT *tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name); |
| 186 |
CRYPTO_MUTEX_unlock_write(&ctx->objs_lock); |
| 187 |
|
| 188 |
if (tmp == NULL || type == X509_LU_CRL) { |
| 189 |
for (size_t i = 0; i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) { |
| 190 |
X509_LOOKUP *lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i); |
| 191 |
if (X509_LOOKUP_by_subject(lu, type, name, &stmp)) { |
| 192 |
tmp = &stmp; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
if (tmp == NULL) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// if (ret->data.ptr != NULL) X509_OBJECT_free_contents(ret); |
| 202 |
|
| 203 |
ret->type = tmp->type; |
| 204 |
ret->data.ptr = tmp->data.ptr; |
| 205 |
|
| 206 |
X509_OBJECT_up_ref_count(ret); |
| 207 |
|
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
static int x509_store_add(X509_STORE *ctx, void *x, int is_crl) { |
| 212 |
if (x == NULL) { |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
X509_OBJECT *const obj = X509_OBJECT_new(); |
| 217 |
if (obj == NULL) { |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
if (is_crl) { |
| 222 |
obj->type = X509_LU_CRL; |
| 223 |
obj->data.crl = (X509_CRL *)x; |
| 224 |
} else { |
| 225 |
obj->type = X509_LU_X509; |
| 226 |
obj->data.x509 = (X509 *)x; |
| 227 |
} |
| 228 |
X509_OBJECT_up_ref_count(obj); |
| 229 |
|
| 230 |
CRYPTO_MUTEX_lock_write(&ctx->objs_lock); |
| 231 |
|
| 232 |
int ret = 1; |
| 233 |
int added = 0; |
| 234 |
// Duplicates are silently ignored |
| 235 |
if (!X509_OBJECT_retrieve_match(ctx->objs, obj)) { |
| 236 |
ret = added = (sk_X509_OBJECT_push(ctx->objs, obj) != 0); |
| 237 |
} |
| 238 |
|
| 239 |
CRYPTO_MUTEX_unlock_write(&ctx->objs_lock); |
| 240 |
|
| 241 |
if (!added) { |
| 242 |
X509_OBJECT_free(obj); |
| 243 |
} |
| 244 |
|
| 245 |
return ret; |
| 246 |
} |
| 247 |
|
| 248 |
int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) { |
| 249 |
return x509_store_add(ctx, x, /*is_crl=*/0); |
| 250 |
} |
| 251 |
|
| 252 |
int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) { |
| 253 |
return x509_store_add(ctx, x, /*is_crl=*/1); |
| 254 |
} |
| 255 |
|
| 256 |
X509_OBJECT *X509_OBJECT_new(void) { |
| 257 |
return OPENSSL_zalloc(sizeof(X509_OBJECT)); |
| 258 |
} |
| 259 |
|
| 260 |
void X509_OBJECT_free(X509_OBJECT *obj) { |
| 261 |
if (obj == NULL) { |
| 262 |
return; |
| 263 |
} |
| 264 |
X509_OBJECT_free_contents(obj); |
| 265 |
OPENSSL_free(obj); |
| 266 |
} |
| 267 |
|
| 268 |
static int X509_OBJECT_up_ref_count(X509_OBJECT *a) { |
| 269 |
switch (a->type) { |
| 270 |
case X509_LU_X509: |
| 271 |
X509_up_ref(a->data.x509); |
| 272 |
break; |
| 273 |
case X509_LU_CRL: |
| 274 |
X509_CRL_up_ref(a->data.crl); |
| 275 |
break; |
| 276 |
} |
| 277 |
return 1; |
| 278 |
} |
| 279 |
|
| 280 |
void X509_OBJECT_free_contents(X509_OBJECT *a) { |
| 281 |
switch (a->type) { |
| 282 |
case X509_LU_X509: |
| 283 |
X509_free(a->data.x509); |
| 284 |
break; |
| 285 |
case X509_LU_CRL: |
| 286 |
X509_CRL_free(a->data.crl); |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
OPENSSL_memset(a, 0, sizeof(X509_OBJECT)); |
| 291 |
} |
| 292 |
|
| 293 |
int X509_OBJECT_get_type(const X509_OBJECT *a) { return a->type; } |
| 294 |
|
| 295 |
X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a) { |
| 296 |
if (a == NULL || a->type != X509_LU_X509) { |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
return a->data.x509; |
| 300 |
} |
| 301 |
|
| 302 |
X509_CRL *X509_OBJECT_get0_X509_CRL(const X509_OBJECT *a) { |
| 303 |
if (a == NULL || a->type != X509_LU_CRL) { |
| 304 |
return NULL; |
| 305 |
} |
| 306 |
return a->data.crl; |
| 307 |
} |
| 308 |
|
| 309 |
int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj) { |
| 310 |
if (a == NULL || !X509_up_ref(obj)) { |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
X509_OBJECT_free_contents(a); |
| 315 |
a->type = X509_LU_X509; |
| 316 |
a->data.x509 = obj; |
| 317 |
return 1; |
| 318 |
} |
| 319 |
|
| 320 |
int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj) { |
| 321 |
if (a == NULL || !X509_CRL_up_ref(obj)) { |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
X509_OBJECT_free_contents(a); |
| 326 |
a->type = X509_LU_CRL; |
| 327 |
a->data.crl = obj; |
| 328 |
return 1; |
| 329 |
} |
| 330 |
|
| 331 |
static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type, |
| 332 |
X509_NAME *name, int *pnmatch) { |
| 333 |
X509_OBJECT stmp; |
| 334 |
X509 x509_s; |
| 335 |
X509_CINF cinf_s; |
| 336 |
X509_CRL crl_s; |
| 337 |
X509_CRL_INFO crl_info_s; |
| 338 |
|
| 339 |
stmp.type = type; |
| 340 |
switch (type) { |
| 341 |
case X509_LU_X509: |
| 342 |
stmp.data.x509 = &x509_s; |
| 343 |
x509_s.cert_info = &cinf_s; |
| 344 |
cinf_s.subject = name; |
| 345 |
break; |
| 346 |
case X509_LU_CRL: |
| 347 |
stmp.data.crl = &crl_s; |
| 348 |
crl_s.crl = &crl_info_s; |
| 349 |
crl_info_s.issuer = name; |
| 350 |
break; |
| 351 |
default: |
| 352 |
// abort(); |
| 353 |
return -1; |
| 354 |
} |
| 355 |
|
| 356 |
size_t idx; |
| 357 |
sk_X509_OBJECT_sort(h); |
| 358 |
if (!sk_X509_OBJECT_find_awslc(h, &idx, &stmp)) { |
| 359 |
return -1; |
| 360 |
} |
| 361 |
|
| 362 |
if (pnmatch != NULL) { |
| 363 |
*pnmatch = 1; |
| 364 |
for (size_t tidx = idx + 1; tidx < sk_X509_OBJECT_num(h); tidx++) { |
| 365 |
const X509_OBJECT *tobj = sk_X509_OBJECT_value(h, tidx); |
| 366 |
if (x509_object_cmp(tobj, &stmp)) { |
| 367 |
break; |
| 368 |
} |
| 369 |
(*pnmatch)++; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return (int)idx; |
| 374 |
} |
| 375 |
|
| 376 |
static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type, |
| 377 |
X509_NAME *name) { |
| 378 |
return x509_object_idx_cnt(h, type, name, NULL); |
| 379 |
} |
| 380 |
|
| 381 |
X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, |
| 382 |
int type, X509_NAME *name) { |
| 383 |
int idx; |
| 384 |
idx = X509_OBJECT_idx_by_subject(h, type, name); |
| 385 |
if (idx == -1) { |
| 386 |
return NULL; |
| 387 |
} |
| 388 |
return sk_X509_OBJECT_value(h, idx); |
| 389 |
} |
| 390 |
|
| 391 |
STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *st) { |
| 392 |
return st->objs; |
| 393 |
} |
| 394 |
|
| 395 |
STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm) { |
| 396 |
int cnt; |
| 397 |
STACK_OF(X509) *sk = sk_X509_new_null(); |
| 398 |
if (sk == NULL) { |
| 399 |
return NULL; |
| 400 |
} |
| 401 |
CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
| 402 |
int idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); |
| 403 |
if (idx < 0) { |
| 404 |
// Nothing found in cache: do lookup to possibly add new objects to |
| 405 |
// cache |
| 406 |
X509_OBJECT xobj; |
| 407 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 408 |
if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) { |
| 409 |
sk_X509_free(sk); |
| 410 |
return NULL; |
| 411 |
} |
| 412 |
X509_OBJECT_free_contents(&xobj); |
| 413 |
CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
| 414 |
idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); |
| 415 |
if (idx < 0) { |
| 416 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 417 |
sk_X509_free(sk); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
} |
| 421 |
for (int i = 0; i < cnt; i++, idx++) { |
| 422 |
X509_OBJECT *obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); |
| 423 |
X509 *x = obj->data.x509; |
| 424 |
if (!sk_X509_push(sk, x)) { |
| 425 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 426 |
sk_X509_pop_free(sk, X509_free); |
| 427 |
return NULL; |
| 428 |
} |
| 429 |
X509_up_ref(x); |
| 430 |
} |
| 431 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 432 |
return sk; |
| 433 |
} |
| 434 |
|
| 435 |
STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *ctx, |
| 436 |
X509_NAME *nm) { |
| 437 |
int cnt; |
| 438 |
X509_OBJECT xobj; |
| 439 |
STACK_OF(X509_CRL) *sk = sk_X509_CRL_new_null(); |
| 440 |
if (sk == NULL) { |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
|
| 444 |
// Always do lookup to possibly add new CRLs to cache. |
| 445 |
if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_CRL, nm, &xobj)) { |
| 446 |
sk_X509_CRL_free(sk); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
X509_OBJECT_free_contents(&xobj); |
| 450 |
CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
| 451 |
int idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_CRL, nm, &cnt); |
| 452 |
if (idx < 0) { |
| 453 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 454 |
sk_X509_CRL_free(sk); |
| 455 |
return NULL; |
| 456 |
} |
| 457 |
|
| 458 |
for (int i = 0; i < cnt; i++, idx++) { |
| 459 |
X509_OBJECT *obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); |
| 460 |
X509_CRL *x = obj->data.crl; |
| 461 |
X509_CRL_up_ref(x); |
| 462 |
if (!sk_X509_CRL_push(sk, x)) { |
| 463 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 464 |
X509_CRL_free(x); |
| 465 |
sk_X509_CRL_pop_free(sk, X509_CRL_free); |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
} |
| 469 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 470 |
return sk; |
| 471 |
} |
| 472 |
|
| 473 |
static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, |
| 474 |
X509_OBJECT *x) { |
| 475 |
sk_X509_OBJECT_sort(h); |
| 476 |
size_t idx; |
| 477 |
if (!sk_X509_OBJECT_find_awslc(h, &idx, x)) { |
| 478 |
return NULL; |
| 479 |
} |
| 480 |
if ((x->type != X509_LU_X509) && (x->type != X509_LU_CRL)) { |
| 481 |
return sk_X509_OBJECT_value(h, idx); |
| 482 |
} |
| 483 |
for (size_t i = idx; i < sk_X509_OBJECT_num(h); i++) { |
| 484 |
X509_OBJECT *obj = sk_X509_OBJECT_value(h, i); |
| 485 |
if (x509_object_cmp(obj, x)) { |
| 486 |
return NULL; |
| 487 |
} |
| 488 |
if (x->type == X509_LU_X509) { |
| 489 |
if (!X509_cmp(obj->data.x509, x->data.x509)) { |
| 490 |
return obj; |
| 491 |
} |
| 492 |
} else if (x->type == X509_LU_CRL) { |
| 493 |
if (!X509_CRL_match(obj->data.crl, x->data.crl)) { |
| 494 |
return obj; |
| 495 |
} |
| 496 |
} else { |
| 497 |
return obj; |
| 498 |
} |
| 499 |
} |
| 500 |
return NULL; |
| 501 |
} |
| 502 |
|
| 503 |
// Try to get issuer certificate from store. Due to limitations of the API |
| 504 |
// this can only retrieve a single certificate matching a given subject name. |
| 505 |
// However it will fill the cache with all matching certificates, so we can |
| 506 |
// examine the cache for all matches. Return values are: 1 lookup |
| 507 |
// successful. 0 certificate not found. -1 some other error. |
| 508 |
int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) { |
| 509 |
X509_NAME *xn; |
| 510 |
X509_OBJECT obj, *pobj; |
| 511 |
int idx, ret; |
| 512 |
size_t i; |
| 513 |
*issuer = NULL; |
| 514 |
xn = X509_get_issuer_name(x); |
| 515 |
if (!X509_STORE_CTX_get_by_subject(ctx, X509_LU_X509, xn, &obj)) { |
| 516 |
return 0; |
| 517 |
} |
| 518 |
// If certificate matches all OK |
| 519 |
if (x509_check_issued_with_callback(ctx, x, obj.data.x509)) { |
| 520 |
if (x509_check_cert_time(ctx, obj.data.x509, /*suppress_error*/1)) { |
| 521 |
*issuer = obj.data.x509; |
| 522 |
return 1; |
| 523 |
} |
| 524 |
} |
| 525 |
X509_OBJECT_free_contents(&obj); |
| 526 |
|
| 527 |
// Else find index of first cert accepted by |
| 528 |
// |x509_check_issued_with_callback|. |
| 529 |
ret = 0; |
| 530 |
CRYPTO_MUTEX_lock_write(&ctx->ctx->objs_lock); |
| 531 |
idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn); |
| 532 |
if (idx != -1) { // should be true as we've had at least one |
| 533 |
// match |
| 534 |
// Look through all matching certs for suitable issuer |
| 535 |
for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) { |
| 536 |
pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); |
| 537 |
// See if we've run past the matches |
| 538 |
if (pobj->type != X509_LU_X509) { |
| 539 |
break; |
| 540 |
} |
| 541 |
if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) { |
| 542 |
break; |
| 543 |
} |
| 544 |
if (x509_check_issued_with_callback(ctx, x, pobj->data.x509)) { |
| 545 |
*issuer = pobj->data.x509; |
| 546 |
ret = 1; |
| 547 |
// Break the loop with a match if the time check is valid, otherwise |
| 548 |
// we continue searching. We leave the last tested issuer certificate in |
| 549 |
// |issuer| on purpose. This returns the closest match if none of the |
| 550 |
// candidate issuer certificates' timestamps were valid. |
| 551 |
if (x509_check_cert_time(ctx, *issuer, /*suppress_error*/1)) { |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
CRYPTO_MUTEX_unlock_write(&ctx->ctx->objs_lock); |
| 558 |
if(*issuer) { |
| 559 |
X509_up_ref(*issuer); |
| 560 |
} |
| 561 |
return ret; |
| 562 |
} |
| 563 |
|
| 564 |
int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags) { |
| 565 |
return X509_VERIFY_PARAM_set_flags(ctx->param, flags); |
| 566 |
} |
| 567 |
|
| 568 |
int X509_STORE_set_depth(X509_STORE *ctx, int depth) { |
| 569 |
X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 570 |
return 1; |
| 571 |
} |
| 572 |
|
| 573 |
int X509_STORE_set_purpose(X509_STORE *ctx, int purpose) { |
| 574 |
return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 575 |
} |
| 576 |
|
| 577 |
int X509_STORE_set_trust(X509_STORE *ctx, int trust) { |
| 578 |
return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 579 |
} |
| 580 |
|
| 581 |
int X509_STORE_set1_param(X509_STORE *ctx, const X509_VERIFY_PARAM *param) { |
| 582 |
return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 583 |
} |
| 584 |
|
| 585 |
X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx) { return ctx->param; } |
| 586 |
|
| 587 |
void X509_STORE_set_verify_cb(X509_STORE *ctx, |
| 588 |
X509_STORE_CTX_verify_cb verify_cb) { |
| 589 |
ctx->verify_cb = verify_cb; |
| 590 |
} |
| 591 |
|
| 592 |
X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx) { |
| 593 |
return ctx->verify_cb; |
| 594 |
} |
| 595 |
|
| 596 |
X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx) { |
| 597 |
return ctx->lookup_crls; |
| 598 |
} |
| 599 |
|
| 600 |
void X509_STORE_set_lookup_crls(X509_STORE *ctx, |
| 601 |
X509_STORE_CTX_lookup_crls_fn lookup_crls) { |
| 602 |
ctx->lookup_crls = lookup_crls; |
| 603 |
} |
| 604 |
|
| 605 |
void X509_STORE_set_get_crl(X509_STORE *ctx, |
| 606 |
X509_STORE_CTX_get_crl_fn get_crl) { |
| 607 |
ctx->get_crl = get_crl; |
| 608 |
} |
| 609 |
|
| 610 |
void X509_STORE_set_check_crl(X509_STORE *ctx, |
| 611 |
X509_STORE_CTX_check_crl_fn check_crl) { |
| 612 |
ctx->check_crl = check_crl; |
| 613 |
} |
| 614 |
|
| 615 |
X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx) { return ctx->ctx; } |
| 616 |
|
| 617 |
int X509_STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 618 |
CRYPTO_EX_dup *dup_unused, |
| 619 |
CRYPTO_EX_free *free_func) { |
| 620 |
int index; |
| 621 |
if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
| 622 |
free_func)) { |
| 623 |
return -1; |
| 624 |
} |
| 625 |
return index; |
| 626 |
} |
| 627 |
|
| 628 |
int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data) { |
| 629 |
return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
| 630 |
} |
| 631 |
|
| 632 |
void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx) { |
| 633 |
return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
| 634 |
} |