src/wolfSSL/src/internal.c
void SSL_ResourceFree(WOLFSSL* ssl)
{
    /* Note: any resources used during the handshake should be released in the
     * function FreeHandshakeResources(). Be careful with the special cases
     * like the RNG which may optionally be kept for the whole session. (For
     * example with the RNG, it isn't used beyond the handshake except when
     * using stream ciphers where it is retained. */

    FreeCiphers(ssl);
    FreeArrays(ssl, 0);
    wc_FreeRng(ssl->rng);
    XFREE(ssl->rng, ssl->heap, DYNAMIC_TYPE_RNG);
    XFREE(ssl->suites, ssl->heap, DYNAMIC_TYPE_SUITES);
    XFREE(ssl->hsHashes, ssl->heap, DYNAMIC_TYPE_HASHES);
    XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);

    /* clear keys struct after session */
    ForceZero(&(ssl->keys), sizeof(Keys));

#ifndef NO_DH
    if (ssl->buffers.serverDH_Priv.buffer) {
        ForceZero(ssl->buffers.serverDH_Priv.buffer,
                                             ssl->buffers.serverDH_Priv.length);
    }
    XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_DH);
    XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_DH);
    /* parameters (p,g) may be owned by ctx */
    if (ssl->buffers.weOwnDH || ssl->options.side == WOLFSSL_CLIENT_END) {
        XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_DH);
        XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_DH);
    }
#endif
#ifndef NO_CERTS
    wolfSSL_UnloadCertsKeys(ssl);
#endif
#ifndef NO_RSA
    if (ssl->peerRsaKey) {
        wc_FreeRsaKey(ssl->peerRsaKey);
        XFREE(ssl->peerRsaKey, ssl->heap, DYNAMIC_TYPE_RSA);
    }
#endif
    if (ssl->buffers.inputBuffer.dynamicFlag)
        ShrinkInputBuffer(ssl, FORCED_FREE);
    if (ssl->buffers.outputBuffer.dynamicFlag)
        ShrinkOutputBuffer(ssl);
#ifdef WOLFSSL_DTLS
    DtlsPoolDelete(ssl);
    if (ssl->dtls_msg_list != NULL) {
        DtlsMsgListDelete(ssl->dtls_msg_list, ssl->heap);
        ssl->dtls_msg_list = NULL;
    }
    XFREE(ssl->buffers.dtlsCtx.peer.sa, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
    ssl->buffers.dtlsCtx.peer.sa = NULL;
#ifndef NO_WOLFSSL_SERVER
    XFREE(ssl->buffers.dtlsCookieSecret.buffer, ssl->heap,
          DYNAMIC_TYPE_COOKIE_PWD);
#endif
#endif /* WOLFSSL_DTLS */
#if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
    if (ssl->biord != ssl->biowr)        /* only free write if different */
        wolfSSL_BIO_free(ssl->biowr);
    wolfSSL_BIO_free(ssl->biord);        /* always free read bio */
#endif
#ifdef HAVE_LIBZ
    FreeStreams(ssl);
#endif
#ifdef HAVE_ECC
    if (ssl->peerEccKey) {
        if (ssl->peerEccKeyPresent)
            wc_ecc_free(ssl->peerEccKey);
        XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
    }
    if (ssl->peerEccDsaKey) {
        if (ssl->peerEccDsaKeyPresent)
            wc_ecc_free(ssl->peerEccDsaKey);
        XFREE(ssl->peerEccDsaKey, ssl->heap, DYNAMIC_TYPE_ECC);
    }
    if (ssl->eccTempKey) {
        if (ssl->eccTempKeyPresent)
            wc_ecc_free(ssl->eccTempKey);
        XFREE(ssl->eccTempKey, ssl->heap, DYNAMIC_TYPE_ECC);
    }
#endif
#ifdef HAVE_PK_CALLBACKS
    #ifdef HAVE_ECC
        XFREE(ssl->buffers.peerEccDsaKey.buffer, ssl->heap, DYNAMIC_TYPE_ECC);
    #endif /* HAVE_ECC */
    #ifndef NO_RSA
        XFREE(ssl->buffers.peerRsaKey.buffer, ssl->heap, DYNAMIC_TYPE_RSA);
    #endif /* NO_RSA */
#endif /* HAVE_PK_CALLBACKS */
#ifdef HAVE_TLS_EXTENSIONS
    TLSX_FreeAll(ssl->extensions);

#ifdef HAVE_ALPN
    if (ssl->alpn_client_list != NULL) {
        XFREE(ssl->alpn_client_list, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        ssl->alpn_client_list = NULL;
    }
#endif
#endif /* HAVE_TLS_EXTENSIONS */
#ifdef HAVE_NETX
    if (ssl->nxCtx.nxPacket)
        nx_packet_release(ssl->nxCtx.nxPacket);
#endif
#if defined(KEEP_PEER_CERT) || defined(GOAHEAD_WS)
    FreeX509(&ssl->peerCert);
#endif
}
/* In case holding SSL object in array and don't want to free actual ssl */
In case holding SSL object in array and don't want to free actual ssl
In case holding SSL object in array and don ' t want to free actual ssl
void SSL_ResourceFree(WOLFSSL* ssl)
{
    FreeCiphers(ssl);
    FreeArrays(ssl, 0);
    wc_FreeRng(ssl->rng);
    XFREE(ssl->rng, ssl->heap, DYNAMIC_TYPE_RNG);
    XFREE(ssl->suites, ssl->heap, DYNAMIC_TYPE_SUITES);
    XFREE(ssl->hsHashes, ssl->heap, DYNAMIC_TYPE_HASHES);
    XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
    ForceZero(&(ssl->keys), sizeof(Keys));
#ifndef NO_DH
    if (ssl->buffers.serverDH_Priv.buffer) {
        ForceZero(ssl->buffers.serverDH_Priv.buffer,
                                             ssl->buffers.serverDH_Priv.length);
    }
    XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_DH);
    XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_DH);
    if (ssl->buffers.weOwnDH || ssl->options.side == WOLFSSL_CLIENT_END) {
        XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_DH);
        XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_DH);
    }
#endif
#ifndef NO_CERTS
    wolfSSL_UnloadCertsKeys(ssl);
#endif
#ifndef NO_RSA
    if (ssl->peerRsaKey) {
        wc_FreeRsaKey(ssl->peerRsaKey);
        XFREE(ssl->peerRsaKey, ssl->heap, DYNAMIC_TYPE_RSA);
    }
#endif
    if (ssl->buffers.inputBuffer.dynamicFlag)
        ShrinkInputBuffer(ssl, FORCED_FREE);
    if (ssl->buffers.outputBuffer.dynamicFlag)
        ShrinkOutputBuffer(ssl);
#ifdef WOLFSSL_DTLS
    DtlsPoolDelete(ssl);
    if (ssl->dtls_msg_list != NULL) {
        DtlsMsgListDelete(ssl->dtls_msg_list, ssl->heap);
        ssl->dtls_msg_list = NULL;
    }
    XFREE(ssl->buffers.dtlsCtx.peer.sa, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
    ssl->buffers.dtlsCtx.peer.sa = NULL;
#ifndef NO_WOLFSSL_SERVER
    XFREE(ssl->buffers.dtlsCookieSecret.buffer, ssl->heap,
          DYNAMIC_TYPE_COOKIE_PWD);
#endif
#endif 
#if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
    if (ssl->biord != ssl->biowr)        
        wolfSSL_BIO_free(ssl->biowr);
    wolfSSL_BIO_free(ssl->biord);        
#endif
#ifdef HAVE_LIBZ
    FreeStreams(ssl);
#endif
#ifdef HAVE_ECC
    if (ssl->peerEccKey) {
        if (ssl->peerEccKeyPresent)
            wc_ecc_free(ssl->peerEccKey);
        XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
    }
    if (ssl->peerEccDsaKey) {
        if (ssl->peerEccDsaKeyPresent)
            wc_ecc_free(ssl->peerEccDsaKey);
        XFREE(ssl->peerEccDsaKey, ssl->heap, DYNAMIC_TYPE_ECC);
    }
    if (ssl->eccTempKey) {
        if (ssl->eccTempKeyPresent)
            wc_ecc_free(ssl->eccTempKey);
        XFREE(ssl->eccTempKey, ssl->heap, DYNAMIC_TYPE_ECC);
    }
#endif
#ifdef HAVE_PK_CALLBACKS
    #ifdef HAVE_ECC
        XFREE(ssl->buffers.peerEccDsaKey.buffer, ssl->heap, DYNAMIC_TYPE_ECC);
    #endif 
    #ifndef NO_RSA
        XFREE(ssl->buffers.peerRsaKey.buffer, ssl->heap, DYNAMIC_TYPE_RSA);
    #endif 
#endif 
#ifdef HAVE_TLS_EXTENSIONS
    TLSX_FreeAll(ssl->extensions);
#ifdef HAVE_ALPN
    if (ssl->alpn_client_list != NULL) {
        XFREE(ssl->alpn_client_list, NULL, DYNAMIC_TYPE_TMP_BUFFER);
        ssl->alpn_client_list = NULL;
    }
#endif
#endif 
#ifdef HAVE_NETX
    if (ssl->nxCtx.nxPacket)
        nx_packet_release(ssl->nxCtx.nxPacket);
#endif
#if defined(KEEP_PEER_CERT) || defined(GOAHEAD_WS)
    FreeX509(&ssl->peerCert);
#endif
}
void SSL_ResourceFree ( WOLFSSL * ssl ) { FreeCiphers ( ssl ) ; FreeArrays ( ssl , 0 ) ; wc_FreeRng ( ssl -> rng ) ; XFREE ( ssl -> rng , ssl -> heap , DYNAMIC_TYPE_RNG ) ; XFREE ( ssl -> suites , ssl -> heap , DYNAMIC_TYPE_SUITES ) ; XFREE ( ssl -> hsHashes , ssl -> heap , DYNAMIC_TYPE_HASHES ) ; XFREE ( ssl -> buffers . domainName . buffer , ssl -> heap , DYNAMIC_TYPE_DOMAIN ) ; ForceZero ( & ( ssl -> keys ) , sizeof ( Keys ) ) ; #ifndef NO_DH if ( ssl -> buffers . serverDH_Priv . buffer ) { ForceZero ( ssl -> buffers . serverDH_Priv . buffer , ssl -> buffers . serverDH_Priv . length ) ; } XFREE ( ssl -> buffers . serverDH_Priv . buffer , ssl -> heap , DYNAMIC_TYPE_DH ) ; XFREE ( ssl -> buffers . serverDH_Pub . buffer , ssl -> heap , DYNAMIC_TYPE_DH ) ; if ( ssl -> buffers . weOwnDH || ssl -> options . side == WOLFSSL_CLIENT_END ) { XFREE ( ssl -> buffers . serverDH_G . buffer , ssl -> heap , DYNAMIC_TYPE_DH ) ; XFREE ( ssl -> buffers . serverDH_P . buffer , ssl -> heap , DYNAMIC_TYPE_DH ) ; } #endif #ifndef NO_CERTS wolfSSL_UnloadCertsKeys ( ssl ) ; #endif #ifndef NO_RSA if ( ssl -> peerRsaKey ) { wc_FreeRsaKey ( ssl -> peerRsaKey ) ; XFREE ( ssl -> peerRsaKey , ssl -> heap , DYNAMIC_TYPE_RSA ) ; } #endif if ( ssl -> buffers . inputBuffer . dynamicFlag ) ShrinkInputBuffer ( ssl , FORCED_FREE ) ; if ( ssl -> buffers . outputBuffer . dynamicFlag ) ShrinkOutputBuffer ( ssl ) ; #ifdef WOLFSSL_DTLS DtlsPoolDelete ( ssl ) ; if ( ssl -> dtls_msg_list != NULL ) { DtlsMsgListDelete ( ssl -> dtls_msg_list , ssl -> heap ) ; ssl -> dtls_msg_list = NULL ; } XFREE ( ssl -> buffers . dtlsCtx . peer . sa , ssl -> heap , DYNAMIC_TYPE_SOCKADDR ) ; ssl -> buffers . dtlsCtx . peer . sa = NULL ; #ifndef NO_WOLFSSL_SERVER XFREE ( ssl -> buffers . dtlsCookieSecret . buffer , ssl -> heap , DYNAMIC_TYPE_COOKIE_PWD ) ; #endif #endif #if defined ( OPENSSL_EXTRA ) || defined ( GOAHEAD_WS ) if ( ssl -> biord != ssl -> biowr ) wolfSSL_BIO_free ( ssl -> biowr ) ; wolfSSL_BIO_free ( ssl -> biord ) ; #endif #ifdef HAVE_LIBZ FreeStreams ( ssl ) ; #endif #ifdef HAVE_ECC if ( ssl -> peerEccKey ) { if ( ssl -> peerEccKeyPresent ) wc_ecc_free ( ssl -> peerEccKey ) ; XFREE ( ssl -> peerEccKey , ssl -> heap , DYNAMIC_TYPE_ECC ) ; } if ( ssl -> peerEccDsaKey ) { if ( ssl -> peerEccDsaKeyPresent ) wc_ecc_free ( ssl -> peerEccDsaKey ) ; XFREE ( ssl -> peerEccDsaKey , ssl -> heap , DYNAMIC_TYPE_ECC ) ; } if ( ssl -> eccTempKey ) { if ( ssl -> eccTempKeyPresent ) wc_ecc_free ( ssl -> eccTempKey ) ; XFREE ( ssl -> eccTempKey , ssl -> heap , DYNAMIC_TYPE_ECC ) ; } #endif #ifdef HAVE_PK_CALLBACKS #ifdef HAVE_ECC XFREE ( ssl -> buffers . peerEccDsaKey . buffer , ssl -> heap , DYNAMIC_TYPE_ECC ) ; #endif #ifndef NO_RSA XFREE ( ssl -> buffers . peerRsaKey . buffer , ssl -> heap , DYNAMIC_TYPE_RSA ) ; #endif #endif #ifdef HAVE_TLS_EXTENSIONS TLSX_FreeAll ( ssl -> extensions ) ; #ifdef HAVE_ALPN if ( ssl -> alpn_client_list != NULL ) { XFREE ( ssl -> alpn_client_list , NULL , DYNAMIC_TYPE_TMP_BUFFER ) ; ssl -> alpn_client_list = NULL ; } #endif #endif #ifdef HAVE_NETX if ( ssl -> nxCtx . nxPacket ) nx_packet_release ( ssl -> nxCtx . nxPacket ) ; #endif #if defined ( KEEP_PEER_CERT ) || defined ( GOAHEAD_WS ) FreeX509 ( & ssl -> peerCert ) ; #endif }
In case holding SSL object in array and don't want to free actual ssl
In case holding SSL object in array and don ' t want to free actual ssl
/* Note: any resources used during the handshake should be released in the
     * function FreeHandshakeResources(). Be careful with the special cases
     * like the RNG which may optionally be kept for the whole session. (For
     * example with the RNG, it isn't used beyond the handshake except when
     * using stream ciphers where it is retained. */ /* clear keys struct after session */ /* parameters (p,g) may be owned by ctx */ /* WOLFSSL_DTLS */ /* only free write if different */ /* always free read bio */ /* HAVE_ECC */ /* NO_RSA */ /* HAVE_PK_CALLBACKS */ /* HAVE_TLS_EXTENSIONS */