]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-ssl-iostream: Add refcounting to ssl_iostream_context
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 31 Oct 2017 16:41:47 +0000 (18:41 +0200)
committerTimo Sirainen <tss@dovecot.fi>
Mon, 6 Nov 2017 23:09:00 +0000 (01:09 +0200)
src/lib-ssl-iostream/iostream-openssl-context.c
src/lib-ssl-iostream/iostream-openssl.c
src/lib-ssl-iostream/iostream-openssl.h
src/lib-ssl-iostream/iostream-ssl-private.h
src/lib-ssl-iostream/iostream-ssl.c
src/lib-ssl-iostream/iostream-ssl.h

index 1a2df3a3b6146fbc25d12650a4998a6f80d88b22..940aebb44e5d95be50566b49defdebcb6fb30a50 100644 (file)
@@ -586,6 +586,7 @@ int openssl_iostream_context_init_client(const struct ssl_iostream_settings *set
        SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
 
        ctx = i_new(struct ssl_iostream_context, 1);
+       ctx->refcount = 1;
        ctx->ssl_ctx = ssl_ctx;
        ctx->client_ctx = TRUE;
        if (ssl_iostream_context_init_common(ctx, &set_copy, error_r) < 0) {
@@ -612,6 +613,7 @@ int openssl_iostream_context_init_server(const struct ssl_iostream_settings *set
        }
 
        ctx = i_new(struct ssl_iostream_context, 1);
+       ctx->refcount = 1;
        ctx->ssl_ctx = ssl_ctx;
        if (ssl_iostream_context_init_common(ctx, set, error_r) < 0) {
                ssl_iostream_context_unref(&ctx);
@@ -621,8 +623,18 @@ int openssl_iostream_context_init_server(const struct ssl_iostream_settings *set
        return 0;
 }
 
-void openssl_iostream_context_deinit(struct ssl_iostream_context *ctx)
+void openssl_iostream_context_ref(struct ssl_iostream_context *ctx)
 {
+       i_assert(ctx->refcount > 0);
+       ctx->refcount++;
+}
+
+void openssl_iostream_context_unref(struct ssl_iostream_context *ctx)
+{
+       i_assert(ctx->refcount > 0);
+       if (--ctx->refcount > 0)
+               return;
+
        SSL_CTX_free(ctx->ssl_ctx);
        pool_unref(&ctx->pool);
        i_free(ctx);
index 73f22d16ee9868056bafa6e65a488dbf88cac97d..37af544129c56b09c3d964aa8a03c069097a9034 100644 (file)
@@ -776,7 +776,8 @@ openssl_iostream_get_last_error(struct ssl_iostream *ssl_io)
 static const struct iostream_ssl_vfuncs ssl_vfuncs = {
        .context_init_client = openssl_iostream_context_init_client,
        .context_init_server = openssl_iostream_context_init_server,
-       .context_deinit = openssl_iostream_context_deinit,
+       .context_ref = openssl_iostream_context_ref,
+       .context_unref = openssl_iostream_context_unref,
 
        .create = openssl_iostream_create,
        .unref = openssl_iostream_unref,
index 5849b48c50540f19f3edb80c21b36d224973f68a..65a70d9bc4d657644ccd98515daaf7074d5ff975 100644 (file)
@@ -16,6 +16,7 @@ enum openssl_iostream_sync_type {
 };
 
 struct ssl_iostream_context {
+       int refcount;
        SSL_CTX *ssl_ctx;
 
        pool_t pool;
@@ -75,7 +76,8 @@ int openssl_iostream_context_init_client(const struct ssl_iostream_settings *set
 int openssl_iostream_context_init_server(const struct ssl_iostream_settings *set,
                                         struct ssl_iostream_context **ctx_r,
                                         const char **error_r);
-void openssl_iostream_context_deinit(struct ssl_iostream_context *ctx);
+void openssl_iostream_context_ref(struct ssl_iostream_context *ctx);
+void openssl_iostream_context_unref(struct ssl_iostream_context *ctx);
 void openssl_iostream_global_deinit(void);
 
 int openssl_iostream_load_key(const struct ssl_iostream_cert *set,
index 2995381ac433e22fc45aaa4d9bbaa95ed26ee4c7..2972b38ac8f366a296c10a8300ac826d6f7d6c21 100644 (file)
@@ -10,7 +10,8 @@ struct iostream_ssl_vfuncs {
        int (*context_init_server)(const struct ssl_iostream_settings *set,
                                   struct ssl_iostream_context **ctx_r,
                                   const char **error_r);
-       void (*context_deinit)(struct ssl_iostream_context *ctx);
+       void (*context_ref)(struct ssl_iostream_context *ctx);
+       void (*context_unref)(struct ssl_iostream_context *ctx);
 
        int (*create)(struct ssl_iostream_context *ctx, const char *host,
                      const struct ssl_iostream_settings *set,
index e4485ec4e39e2cfad4a3300eb225f7701d7a2f1d..343f1daa9f061e97ca9cd0ec7ec1a4be74b2cba7 100644 (file)
@@ -98,12 +98,17 @@ int ssl_iostream_context_init_server(const struct ssl_iostream_settings *set,
        return ssl_vfuncs->context_init_server(set, ctx_r, error_r);
 }
 
+void ssl_iostream_context_ref(struct ssl_iostream_context *ctx)
+{
+       ssl_vfuncs->context_ref(ctx);
+}
+
 void ssl_iostream_context_unref(struct ssl_iostream_context **_ctx)
 {
        struct ssl_iostream_context *ctx = *_ctx;
 
        *_ctx = NULL;
-       ssl_vfuncs->context_deinit(ctx);
+       ssl_vfuncs->context_unref(ctx);
 }
 
 int io_stream_create_ssl_client(struct ssl_iostream_context *ctx, const char *host,
index 531bf052628a029408793fee3a61138aacc4248c..255495f1dc1432bdd80445a06cd0cb210367f9a3 100644 (file)
@@ -92,6 +92,7 @@ int ssl_iostream_context_init_client(const struct ssl_iostream_settings *set,
 int ssl_iostream_context_init_server(const struct ssl_iostream_settings *set,
                                     struct ssl_iostream_context **ctx_r,
                                     const char **error_r);
+void ssl_iostream_context_ref(struct ssl_iostream_context *ctx);
 void ssl_iostream_context_unref(struct ssl_iostream_context **ctx);
 /* FIXME: temporarily for backwards compatibility, remove later */
 #define ssl_iostream_context_deinit(ctx) ssl_iostream_context_unref(ctx)