* @param _expr The call to the OpenSSL function and storage of the
* result.
*/
-#define SSL_BIND_MEMORY(_expr) \
+#define SSL_BIND_OBJ_MEMORY(_expr) \
do { \
void *_nmem; \
MEM(ssl_talloc_ctx = talloc_init(STRINGIFY(_expr))); \
ssl_talloc_ctx = NULL; \
} while (0)
+/** Bind all memory allocated from this point until the next instance of SSL_BIND_MEMORY_END to _obj
+ *
+ * @param[in] _obj to bind memory to.
+ */
+#define SSL_BIND_MEMORY_BEGIN(_obj) \
+do { \
+ if (!fr_cond_assert(!ssl_talloc_ctx && (_obj))) { \
+ MEM(ssl_talloc_ctx = talloc_init(STRINGIFY(_obj))); \
+ talloc_steal(_obj, ssl_talloc_ctx); \
+ } \
+} while(0)
+
+#define SSL_BIND_MEMORY_END ssl_talloc_ctx = NULL
+
/*
* tls/cache.c
*/
int ctx_options = 0;
void *app_data_index;
- SSL_BIND_MEMORY(ctx = SSL_CTX_new(SSLv23_method())); /* which is really "all known SSL / TLS methods". Idiots. */
+ SSL_BIND_OBJ_MEMORY(ctx = SSL_CTX_new(SSLv23_method())); /* which is really "all known SSL / TLS methods". Idiots. */
if (!ctx) {
tls_log_error(NULL, "Failed creating TLS context");
return NULL;
}
+ /*
+ * Bind any other memory to the ctx to fix
+ * leaks on exit.
+ */
+ SSL_BIND_MEMORY_BEGIN(ctx);
+
/*
* Save the config on the context so that callbacks which
* only get SSL_CTX* e.g. session persistence, can get it
if (conf->psk_query && !*conf->psk_query) {
ERROR("Invalid PSK Configuration: psk_query cannot be empty");
error:
+ SSL_BIND_MEMORY_END;
SSL_CTX_free(ctx);
return NULL;
}
SSL_free(ssl);
}
- /*
- * Setup session caching
- */
- tls_cache_init(ctx, conf->session_cache_server ? true : false, conf->session_cache_lifetime);
-
/*
* Load dh params
*/
if (ctx_dh_params_load(ctx, dh_file) < 0) goto error;
}
+ /*
+ * We're done configuring the ctx.
+ */
+ SSL_BIND_MEMORY_END;
+
+ /*
+ * Setup session caching
+ */
+ tls_cache_init(ctx, conf->session_cache_server ? true : false, conf->session_cache_lifetime);
+
return ctx;
}
#endif