Skip to content

Commit a338356

Browse files
davidbenjustsmth
authored andcommitted
Add X509_OBJECT_new and X509_OBJECT_free
This is a bit of a mess. The immediate motivation here is that there is no legitimate reason to ever call X509_OBJECT_free_contents outside of the library. Unsurprisingly, this means rust-openssl uses it. rust-openssl uses it because they want to be able to free X509_OBJECTs. Add OpenSSL 1.1.x's X509_OBJECT_free, which is what they should be using it instead. As it turns out, they don't *actually* need to free X509_OBJECTs. This is just some design mistake that cause them to need free functions for types they never free. On top of that, the only reason rust-openssl references X509_OBJECT is for X509_STORE_get0_objects, but their use of that API is a Rust safety violation anyway. It's all a mess. As for whether freeing it ever makes sense, the question is whether X509_STORE_get_by_subject needs to be a public API. In so far as it is public, callers would need to create empty X509_OBJECTs as an output, now that X509_OBJECT is opaque. There are also other users of X509_STORE_get0_objects that might benefit from an X509_STORE_get1_objects, in which case X509_OBJECT_free will be useful. For now just to unblock fixing the more immediate rust-openssl mistake (rather than the underlying mistake), add the APIs that X509_STORE_get_by_subject callers would need if they existed. There's quite a bit to clean up around X509_OBJECT, but start by adding these APIs. As part of this, since rust-openssl prevents us from removing X509_OBJECT_free_contents, deprecate it and fix it to leave the X509_OBJECT in a self-consistent state. (This is moot because rust-openssl will never call it, but still.) Change-Id: I78708f2d2464eb9a18844fef0d62cb0a727b9f47 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/64129 Reviewed-by: Bob Beck <[email protected]> Auto-Submit: David Benjamin <[email protected]> Commit-Queue: David Benjamin <[email protected]> (cherry picked from commit c2b7df5850398dc7c73146c07f6eed95dd363a48)
1 parent 21618dd commit a338356

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

crypto/x509/x509_lu.c

+12-27
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,6 @@ int X509_STORE_up_ref(X509_STORE *store) {
217217
return 1;
218218
}
219219

220-
static void cleanup(X509_OBJECT *a) {
221-
if (a == NULL) {
222-
return;
223-
}
224-
if (a->type == X509_LU_X509) {
225-
X509_free(a->data.x509);
226-
} else if (a->type == X509_LU_CRL) {
227-
X509_CRL_free(a->data.crl);
228-
} else {
229-
// abort();
230-
}
231-
232-
OPENSSL_free(a);
233-
}
234-
235220
void X509_STORE_free(X509_STORE *vfy) {
236221
size_t j;
237222
STACK_OF(X509_LOOKUP) *sk;
@@ -254,7 +239,7 @@ void X509_STORE_free(X509_STORE *vfy) {
254239
X509_LOOKUP_free(lu);
255240
}
256241
sk_X509_LOOKUP_free(sk);
257-
sk_X509_OBJECT_pop_free(vfy->objs, cleanup);
242+
sk_X509_OBJECT_pop_free(vfy->objs, X509_OBJECT_free);
258243

259244
if (vfy->param) {
260245
X509_VERIFY_PARAM_free(vfy->param);
@@ -328,7 +313,7 @@ static int x509_store_add(X509_STORE *ctx, void *x, int is_crl) {
328313
return 0;
329314
}
330315

331-
X509_OBJECT *const obj = (X509_OBJECT *)OPENSSL_malloc(sizeof(X509_OBJECT));
316+
X509_OBJECT *const obj = X509_OBJECT_new();
332317
if (obj == NULL) {
333318
return 0;
334319
}
@@ -354,8 +339,7 @@ static int x509_store_add(X509_STORE *ctx, void *x, int is_crl) {
354339
CRYPTO_MUTEX_unlock_write(&ctx->objs_lock);
355340

356341
if (!added) {
357-
X509_OBJECT_free_contents(obj);
358-
OPENSSL_free(obj);
342+
X509_OBJECT_free(obj);
359343
}
360344

361345
return ret;
@@ -370,11 +354,15 @@ int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) {
370354
}
371355

372356
X509_OBJECT *X509_OBJECT_new(void) {
373-
X509_OBJECT *ret = OPENSSL_zalloc(sizeof(X509_OBJECT));
374-
if (ret == NULL) {
375-
return NULL;
357+
return OPENSSL_zalloc(sizeof(X509_OBJECT));
358+
}
359+
360+
void X509_OBJECT_free(X509_OBJECT *obj) {
361+
if (obj == NULL) {
362+
return;
376363
}
377-
return ret;
364+
X509_OBJECT_free_contents(obj);
365+
OPENSSL_free(obj);
378366
}
379367

380368
int X509_OBJECT_up_ref_count(X509_OBJECT *a) {
@@ -398,11 +386,8 @@ void X509_OBJECT_free_contents(X509_OBJECT *a) {
398386
X509_CRL_free(a->data.crl);
399387
break;
400388
}
401-
}
402389

403-
void X509_OBJECT_free(X509_OBJECT *a) {
404-
X509_OBJECT_free_contents(a);
405-
OPENSSL_free(a);
390+
OPENSSL_memset(a, 0, sizeof(X509_OBJECT));
406391
}
407392

408393
int X509_OBJECT_get_type(const X509_OBJECT *a) { return a->type; }

include/openssl/x509.h

+17
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,7 @@ The X509_STORE then calls a function to actually verify the
28202820
certificate chain.
28212821
*/
28222822

2823+
#define X509_LU_NONE 0
28232824
#define X509_LU_X509 1
28242825
#define X509_LU_CRL 2
28252826
#define X509_LU_PKEY 3
@@ -3016,6 +3017,22 @@ OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_retrieve_by_subject(
30163017
STACK_OF(X509_OBJECT) *h, int type, X509_NAME *name);
30173018
OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h,
30183019
X509_OBJECT *x);
3020+
3021+
// X509_OBJECT_new returns a newly-allocated, empty |X509_OBJECT| or NULL on
3022+
// error.
3023+
OPENSSL_EXPORT X509_OBJECT *X509_OBJECT_new(void);
3024+
3025+
// X509_OBJECT_free releases memory associated with |obj|.
3026+
OPENSSL_EXPORT void X509_OBJECT_free(X509_OBJECT *obj);
3027+
3028+
// X509_OBJECT_get_type returns the type of |obj|, which will be one of the
3029+
// |X509_LU_*| constants.
3030+
OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *obj);
3031+
3032+
// X509_OBJECT_get0_X509 returns |obj| as a certificate, or NULL if |obj| is not
3033+
// a certificate.
3034+
OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *obj);
3035+
30193036
OPENSSL_EXPORT int X509_OBJECT_up_ref_count(X509_OBJECT *a);
30203037
OPENSSL_EXPORT int X509_OBJECT_get_type(const X509_OBJECT *a);
30213038
OPENSSL_EXPORT X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a);

0 commit comments

Comments
 (0)