summaryrefslogtreecommitdiffstats
path: root/crypto/x509/x509_lu.c
diff options
context:
space:
mode:
authorRich Salz <rsalz@openssl.org>2017-04-20 15:33:42 -0400
committerRich Salz <rsalz@openssl.org>2017-04-20 15:33:42 -0400
commitc0452248ea1a59a41023a4765ef7d9825e80a62b (patch)
treeacf05d2312af49b5cc0b60f9ba38a720458fac3c /crypto/x509/x509_lu.c
parent0444c52a5ff3c2c09f8d7f0f5b464e10231de032 (diff)
Ignore dups in X509_STORE_add_*
X509_STORE_add_cert and X509_STORE_add_crl are changed to return success if the object to be added was already found in the store, rather than returning an error. Raise errors if empty or malformed files are read when loading certificates and CRLs. Remove NULL checks and allow a segv to occur. Add error handing for all calls to X509_STORE_add_c{ert|tl} Refactor these two routines into one. Bring the unit test for duplicate certificates up to date using the test framework. Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Rich Salz <rsalz@openssl.org> (Merged from https://github.com/openssl/openssl/pull/2830)
Diffstat (limited to 'crypto/x509/x509_lu.c')
-rw-r--r--crypto/x509/x509_lu.c63
1 files changed, 24 insertions, 39 deletions
diff --git a/crypto/x509/x509_lu.c b/crypto/x509/x509_lu.c
index 889fbe8282..e64fda24f4 100644
--- a/crypto/x509/x509_lu.c
+++ b/crypto/x509/x509_lu.c
@@ -1,5 +1,5 @@
/*
- * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -290,26 +290,29 @@ int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,
return 1;
}
-int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
-{
+static int x509_store_add(X509_STORE *ctx, void *x, int crl) {
X509_OBJECT *obj;
- int ret = 1, added = 1;
+ int ret = 0, added = 0;
if (x == NULL)
return 0;
obj = X509_OBJECT_new();
if (obj == NULL)
return 0;
- obj->type = X509_LU_X509;
- obj->data.x509 = x;
+
+ if (crl) {
+ obj->type = X509_LU_CRL;
+ obj->data.crl = (X509_CRL *)x;
+ } else {
+ obj->type = X509_LU_X509;
+ obj->data.x509 = (X509 *)x;
+ }
X509_OBJECT_up_ref_count(obj);
CRYPTO_THREAD_write_lock(ctx->lock);
if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
- X509err(X509_F_X509_STORE_ADD_CERT,
- X509_R_CERT_ALREADY_IN_HASH_TABLE);
- ret = 0;
+ ret = 1;
} else {
added = sk_X509_OBJECT_push(ctx->objs, obj);
ret = added != 0;
@@ -317,46 +320,28 @@ int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
CRYPTO_THREAD_unlock(ctx->lock);
- if (!ret) /* obj not pushed */
+ if (added == 0) /* obj not pushed */
X509_OBJECT_free(obj);
- if (!added) /* on push failure */
- X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
return ret;
}
-int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
+int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)
{
- X509_OBJECT *obj;
- int ret = 1, added = 1;
-
- if (x == NULL)
- return 0;
- obj = X509_OBJECT_new();
- if (obj == NULL)
+ if (!x509_store_add(ctx, x, 0)) {
+ X509err(X509_F_X509_STORE_ADD_CERT, ERR_R_MALLOC_FAILURE);
return 0;
- obj->type = X509_LU_CRL;
- obj->data.crl = x;
- X509_OBJECT_up_ref_count(obj);
-
- CRYPTO_THREAD_write_lock(ctx->lock);
-
- if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
- X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);
- ret = 0;
- } else {
- added = sk_X509_OBJECT_push(ctx->objs, obj);
- ret = added != 0;
}
+ return 1;
+}
- CRYPTO_THREAD_unlock(ctx->lock);
-
- if (!ret) /* obj not pushed */
- X509_OBJECT_free(obj);
- if (!added) /* on push failure */
+int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
+{
+ if (!x509_store_add(ctx, x, 1)) {
X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
-
- return ret;
+ return 0;
+ }
+ return 1;
}
int X509_OBJECT_up_ref_count(X509_OBJECT *a)