Skip to content

Commit 18e6bf5

Browse files
davidbenjustsmth
authored andcommitted
Unexport various unused X509_OBJECT and X509_LOOKUP functions.
Some things of note: - Anyone calling X509_OBJECT_up_ref_count is breaking X509_OBJECT's internal invariants, or relying on someone else handing back an X509_OBJECT with broken invariants. - X509_LOOKUP_by_subject hands back an X509_OBJECT with broken internal invariants. Fortunately, it is never called, so unexport it as a the first step to cleaning this up. Change-Id: Ia67693f802671cf857bf51aec6e20f27d1525212 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/64130 Auto-Submit: David Benjamin <[email protected]> Reviewed-by: Bob Beck <[email protected]> Commit-Queue: David Benjamin <[email protected]> (cherry picked from commit d9b81bb43a24b3adb6e8a616a4828e1bad89c486)
1 parent 526543f commit 18e6bf5

File tree

3 files changed

+38
-49
lines changed

3 files changed

+38
-49
lines changed

crypto/x509/x509_lu.c

+29-24
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,22 @@
6565
#include "../internal.h"
6666
#include "internal.h"
6767

68-
X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method) {
69-
X509_LOOKUP *ret;
7068

71-
ret = (X509_LOOKUP *)OPENSSL_zalloc(sizeof(X509_LOOKUP));
69+
static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
70+
X509_NAME *name);
71+
static X509_OBJECT *X509_OBJECT_retrieve_by_subject(
72+
STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name);
73+
static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
74+
X509_OBJECT *x);
75+
static int X509_OBJECT_up_ref_count(X509_OBJECT *a);
76+
77+
static X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
78+
static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
79+
X509_OBJECT *ret);
80+
static int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
81+
82+
static X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method) {
83+
X509_LOOKUP *ret = OPENSSL_zalloc(sizeof(X509_LOOKUP));
7284
if (ret == NULL) {
7385
return NULL;
7486
}
@@ -91,18 +103,7 @@ void X509_LOOKUP_free(X509_LOOKUP *ctx) {
91103
OPENSSL_free(ctx);
92104
}
93105

94-
int X509_LOOKUP_init(X509_LOOKUP *ctx) {
95-
if (ctx->method == NULL) {
96-
return 0;
97-
}
98-
if (ctx->method->init != NULL) {
99-
return ctx->method->init(ctx);
100-
} else {
101-
return 1;
102-
}
103-
}
104-
105-
int X509_LOOKUP_shutdown(X509_LOOKUP *ctx) {
106+
static int X509_LOOKUP_shutdown(X509_LOOKUP *ctx) {
106107
if (ctx->method == NULL) {
107108
return 0;
108109
}
@@ -125,14 +126,18 @@ int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
125126
}
126127
}
127128

128-
int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
129-
X509_OBJECT *ret) {
129+
static int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,
130+
X509_OBJECT *ret) {
130131
if ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL)) {
131132
return 0;
132133
}
133134
if (ctx->skip) {
134135
return 0;
135136
}
137+
// Note |get_by_subject| leaves |ret| in an inconsistent state. It has
138+
// pointers to an |X509| or |X509_CRL|, but has not bumped the refcount yet.
139+
// For now, the caller is expected to fix this, but ideally we'd fix the
140+
// |X509_LOOKUP| convention itself.
136141
return ctx->method->get_by_subject(ctx, type, name, ret) > 0;
137142
}
138143

@@ -365,7 +370,7 @@ void X509_OBJECT_free(X509_OBJECT *obj) {
365370
OPENSSL_free(obj);
366371
}
367372

368-
int X509_OBJECT_up_ref_count(X509_OBJECT *a) {
373+
static int X509_OBJECT_up_ref_count(X509_OBJECT *a) {
369374
switch (a->type) {
370375
case X509_LU_X509:
371376
X509_up_ref(a->data.x509);
@@ -473,13 +478,13 @@ static int x509_object_idx_cnt(STACK_OF(X509_OBJECT) *h, int type,
473478
return (int)idx;
474479
}
475480

476-
int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
477-
X509_NAME *name) {
481+
static int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,
482+
X509_NAME *name) {
478483
return x509_object_idx_cnt(h, type, name, NULL);
479484
}
480485

481-
X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, int type,
482-
X509_NAME *name) {
486+
X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,
487+
int type, X509_NAME *name) {
483488
int idx;
484489
idx = X509_OBJECT_idx_by_subject(h, type, name);
485490
if (idx == -1) {
@@ -574,8 +579,8 @@ STACK_OF(X509_CRL) *X509_STORE_get1_crls(X509_STORE_CTX *ctx, X509_NAME *nm) {
574579
return sk;
575580
}
576581

577-
X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
578-
X509_OBJECT *x) {
582+
static X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
583+
X509_OBJECT *x) {
579584
sk_X509_OBJECT_sort(h);
580585
size_t idx;
581586
if (!sk_X509_OBJECT_find_awslc(h, &idx, x)) {

crypto/x509/x509_test.cc

+2-9
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ TEST(X509Test, MatchFoundSetsPeername) {
18511851
char *peername = nullptr;
18521852
EXPECT_NE(1, X509_check_host(leaf.get(), kWrongHostname, strlen(kWrongHostname), 0, &peername));
18531853
ASSERT_EQ(nullptr, peername);
1854-
1854+
18551855
EXPECT_EQ(1, X509_check_host(leaf.get(), kHostname, strlen(kHostname), 0, &peername));
18561856
EXPECT_STREQ(peername, kHostname);
18571857
OPENSSL_free(peername);
@@ -5086,15 +5086,8 @@ TEST(X509Test, AddDuplicates) {
50865086
ASSERT_TRUE(X509_STORE_lock(store.get()));
50875087
// Sleep after taking the lock to cause contention. Sleep longer than the
50885088
// adder half of threads to ensure we hold the lock while they contend
5089-
// for it. |X509_OBJECT_retrieve_by_subject| is called because it doesn't
5090-
// take a lock on the store, thus avoiding deadlock.
5089+
// for it.
50915090
std::this_thread::sleep_for(std::chrono::microseconds(11 + (sleep_buf[0] % 5)));
5092-
EXPECT_TRUE(X509_OBJECT_retrieve_by_subject(
5093-
store->objs, X509_LU_X509, X509_get_subject_name(a.get())
5094-
));
5095-
EXPECT_TRUE(X509_OBJECT_retrieve_by_subject(
5096-
store->objs, X509_LU_X509, X509_get_subject_name(b.get())
5097-
));
50985091
ASSERT_TRUE(X509_STORE_unlock(store.get()));
50995092
});
51005093
}

include/openssl/x509.h

+7-16
Original file line numberDiff line numberDiff line change
@@ -2658,6 +2658,13 @@ OPENSSL_EXPORT int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
26582658
OPENSSL_EXPORT X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(
26592659
X509_STORE_CTX *ctx);
26602660

2661+
// X509_LOOKUP_free releases memory associated with |ctx|. This function should
2662+
// never be used outside the library. No function in the public API hands
2663+
// ownership of an |X509_LOOKUP| to the caller.
2664+
//
2665+
// TODO(davidben): Unexport this function after rust-openssl is fixed to no
2666+
// longer call it.
2667+
OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
26612668

26622669
// Private structures.
26632670

@@ -3016,13 +3023,6 @@ OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void);
30163023
// X509_OBJECT_free frees an |X509_OBJECT| from the heap.
30173024
OPENSSL_EXPORT void X509_OBJECT_free(X509_OBJECT *a);
30183025

3019-
OPENSSL_EXPORT int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h,
3020-
int type, X509_NAME *name);
3021-
OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_retrieve_by_subject(
3022-
STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name);
3023-
OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
3024-
X509_OBJECT *x);
3025-
30263026
// X509_OBJECT_new returns a newly-allocated, empty |X509_OBJECT| or NULL on
30273027
// error.
30283028
OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void);
@@ -3038,7 +3038,6 @@ OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *obj);
30383038
// a certificate.
30393039
OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *obj);
30403040

3041-
OPENSSL_EXPORT int X509_OBJECT_up_ref_count(X509_OBJECT *a);
30423041
OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *a);
30433042
OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a);
30443043
// X509_OBJECT_get0_X509_CRL returns the |X509_CRL| associated with |a|
@@ -3182,13 +3181,6 @@ OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *ctx, const char *file,
31823181
OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file,
31833182
int type);
31843183

3185-
OPENSSL_EXPORT X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
3186-
OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
3187-
OPENSSL_EXPORT int X509_LOOKUP_init(X509_LOOKUP *ctx);
3188-
OPENSSL_EXPORT int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type,
3189-
X509_NAME *name, X509_OBJECT *ret);
3190-
OPENSSL_EXPORT int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
3191-
31923184
OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
31933185
const char *dir);
31943186
OPENSSL_EXPORT int X509_STORE_set_default_paths(X509_STORE *ctx);
@@ -3379,7 +3371,6 @@ BORINGSSL_MAKE_DELETER(X509_ATTRIBUTE, X509_ATTRIBUTE_free)
33793371
BORINGSSL_MAKE_DELETER(X509_CRL, X509_CRL_free)
33803372
BORINGSSL_MAKE_UP_REF(X509_CRL, X509_CRL_up_ref)
33813373
BORINGSSL_MAKE_DELETER(X509_EXTENSION, X509_EXTENSION_free)
3382-
BORINGSSL_MAKE_DELETER(X509_LOOKUP, X509_LOOKUP_free)
33833374
BORINGSSL_MAKE_DELETER(X509_OBJECT, X509_OBJECT_free)
33843375
BORINGSSL_MAKE_DELETER(X509_NAME, X509_NAME_free)
33853376
BORINGSSL_MAKE_DELETER(X509_NAME_ENTRY, X509_NAME_ENTRY_free)

0 commit comments

Comments
 (0)