This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-c-core.git

commit da3832cd21560ad54f8c0f50f613b9de39b2f0da
Author: Robert Lazarski <[email protected]>
AuthorDate: Sat Jan 10 08:55:07 2026 -1000

    Fix SSL memory leaks (AXIS2C-1388)
    
    ssl_stream.c:
    - Free ctx and stream when axis2_ssl_utils_initialize_ssl fails
    
    ssl_utils.c - axis2_ssl_utils_initialize_ssl:
    - Free ssl when BIO_new_socket fails
    - Free ssl when SSL_connect fails
    - Free ssl when hostname verification fails (2 locations)
    - Free ssl when certificate verification fails
    
    ssl_utils.c - axis2_ssl_utils_cleanup_ssl:
    - Fix broken logic that only freed SSL when shutdown returned 0
    - Now always free SSL regardless of shutdown result
    
    Co-Authored-By: Claude Opus 4.5 <[email protected]>
---
 src/core/transport/http/sender/ssl/ssl_stream.c |  1 +
 src/core/transport/http/sender/ssl/ssl_utils.c  | 13 +++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/core/transport/http/sender/ssl/ssl_stream.c 
b/src/core/transport/http/sender/ssl/ssl_stream.c
index e3d9e1621..0f626a54d 100644
--- a/src/core/transport/http/sender/ssl/ssl_stream.c
+++ b/src/core/transport/http/sender/ssl/ssl_stream.c
@@ -100,6 +100,7 @@ axutil_stream_create_ssl(
         stream_impl->socket, host);
     if (!stream_impl->ssl)
     {
+        axis2_ssl_stream_free((axutil_stream_t *) stream_impl, env);
         AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_SSL_ENGINE, AXIS2_FAILURE);
         return NULL;
     }
diff --git a/src/core/transport/http/sender/ssl/ssl_utils.c 
b/src/core/transport/http/sender/ssl/ssl_utils.c
index 06d412654..a0a2aaa45 100644
--- a/src/core/transport/http/sender/ssl/ssl_utils.c
+++ b/src/core/transport/http/sender/ssl/ssl_utils.c
@@ -193,6 +193,7 @@ axis2_ssl_utils_initialize_ssl(
         AXIS2_LOG_ERROR (env->log, AXIS2_LOG_SI,
             "[ssl]unable to create BIO new socket for socket %d",
             (int)socket);
+        SSL_free(ssl);
         return NULL;
     }
 
@@ -200,6 +201,7 @@ axis2_ssl_utils_initialize_ssl(
     if (SSL_connect(ssl) <= 0)
     {
         AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_SSL_ENGINE, AXIS2_FAILURE);
+        SSL_free(ssl);
         return NULL;
     }
 
@@ -253,6 +255,7 @@ axis2_ssl_utils_initialize_ssl(
                                 AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI,
                                         "[ssl client] peer name does not match 
certificate CN/SAN");
                                 X509_free(peer_cert);
+                                SSL_free(ssl);
                                 return NULL;
                             }
                         }
@@ -272,6 +275,7 @@ axis2_ssl_utils_initialize_ssl(
         AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
             "[ssl client] SSL certificate verification failed (%s)",
             sslerror);
+        SSL_free(ssl);
         return NULL;
     }
     else {
@@ -291,6 +295,7 @@ axis2_ssl_utils_initialize_ssl(
                     AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI,
                             "[ssl client] peer name does not match certificate 
CN/SAN");
                     X509_free(peer_cert);
+                    SSL_free(ssl);
                     return NULL;
                 }
             }
@@ -312,10 +317,10 @@ axis2_ssl_utils_cleanup_ssl(
 
     if (ssl)
     {
-        if(SSL_shutdown(ssl)==0)
-               {
-                       SSL_free(ssl);
-               }
+        /* SSL_shutdown returns 0 if not yet complete, 1 if complete, <0 on 
error.
+         * We should always free the SSL object regardless of shutdown result. 
*/
+        SSL_shutdown(ssl);
+        SSL_free(ssl);
     }
     if (ctx)
     {

Reply via email to