src/wolfSSL/src/internal.c
int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
{
    int  ret;

    XMEMSET(ssl, 0, sizeof(WOLFSSL));

    ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
    ssl->buffers.inputBuffer.bufferSize  = STATIC_BUFFER_LEN;

    ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
    ssl->buffers.outputBuffer.bufferSize  = STATIC_BUFFER_LEN;

#if defined(KEEP_PEER_CERT) || defined(GOAHEAD_WS)
    InitX509(&ssl->peerCert, 0);
#endif

    ssl->rfd = -1;   /* set to invalid descriptor */
    ssl->wfd = -1;

    ssl->IOCB_ReadCtx  = &ssl->rfd;  /* prevent invalid pointer access if not */
    ssl->IOCB_WriteCtx = &ssl->wfd;  /* correctly set */

#ifdef HAVE_NETX
    ssl->IOCB_ReadCtx  = &ssl->nxCtx;  /* default NetX IO ctx, same for read */
    ssl->IOCB_WriteCtx = &ssl->nxCtx;  /* and write */
#endif
#ifdef WOLFSSL_DTLS
    ssl->dtls_expected_rx = MAX_MTU;
#endif

    ssl->options.serverState = NULL_STATE;
    ssl->options.clientState = NULL_STATE;
    ssl->options.connectState = CONNECT_BEGIN;
    ssl->options.acceptState  = ACCEPT_BEGIN;
    ssl->options.handShakeState  = NULL_STATE;
    ssl->options.processReply = doProcessInit;

#ifdef WOLFSSL_DTLS
    ssl->dtls_timeout_init              = DTLS_TIMEOUT_INIT;
    ssl->dtls_timeout_max               = DTLS_TIMEOUT_MAX;
    ssl->dtls_timeout                   = ssl->dtls_timeout_init;
#endif

    #ifndef NO_OLD_TLS
        ssl->hmac = SSL_hmac; /* default to SSLv3 */
    #else
        ssl->hmac = TLS_hmac;
    #endif


#ifdef WOLFSSL_DTLS
    ssl->buffers.dtlsCtx.fd = -1;
#endif

    ssl->cipher.ssl = ssl;

#ifdef HAVE_TLS_EXTENSIONS
#ifdef HAVE_MAX_FRAGMENT
    ssl->max_fragment = MAX_RECORD_SIZE;
#endif
#ifdef HAVE_ALPN
    ssl->alpn_client_list = NULL;
#endif
#endif

    /* default alert state (none) */
    ssl->alert_history.last_rx.code  = -1;
    ssl->alert_history.last_rx.level = -1;
    ssl->alert_history.last_tx.code  = -1;
    ssl->alert_history.last_tx.level = -1;

    InitCiphers(ssl);
    InitCipherSpecs(&ssl->specs);

    /* all done with init, now can return errors, call other stuff */

    /* arrays */
    ssl->arrays = (Arrays*)XMALLOC(sizeof(Arrays), ssl->heap,
                                                           DYNAMIC_TYPE_ARRAYS);
    if (ssl->arrays == NULL) {
        WOLFSSL_MSG("Arrays Memory error");
        return MEMORY_E;
    }
    XMEMSET(ssl->arrays, 0, sizeof(Arrays));

    /* suites */
    ssl->suites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap,
                                   DYNAMIC_TYPE_SUITES);
    if (ssl->suites == NULL) {
        WOLFSSL_MSG("Suites Memory error");
        return MEMORY_E;
    }

    /* Initialize SSL with the appropriate fields from it's ctx */
    /* requires valid arrays and suites */
    if((ret =  SetSSL_CTX(ssl, ctx)) != SSL_SUCCESS)
        return ret;

    ssl->options.dtls = ssl->version.major == DTLS_MAJOR;

    /* hsHashes */
    ssl->hsHashes = (HS_Hashes*)XMALLOC(sizeof(HS_Hashes), ssl->heap,
                                                           DYNAMIC_TYPE_HASHES);
    if (ssl->hsHashes == NULL) {
        WOLFSSL_MSG("HS_Hashes Memory error");
        return MEMORY_E;
    }

#ifndef NO_OLD_TLS
#ifndef NO_MD5
    wc_InitMd5(&ssl->hsHashes->hashMd5);
#endif
#ifndef NO_SHA
    ret = wc_InitSha(&ssl->hsHashes->hashSha);
    if (ret != 0) {
        return ret;
    }
#endif
#endif
#ifndef NO_SHA256
    ret = wc_InitSha256(&ssl->hsHashes->hashSha256);
    if (ret != 0) {
        return ret;
    }
#endif
#ifdef WOLFSSL_SHA384
    ret = wc_InitSha384(&ssl->hsHashes->hashSha384);
    if (ret != 0) {
        return ret;
    }
#endif
#ifdef WOLFSSL_SHA512
    ret = wc_InitSha512(&ssl->hsHashes->hashSha512);
    if (ret != 0) {
        return ret;
    }
#endif

    /* RNG */
    ssl->rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), ssl->heap, DYNAMIC_TYPE_RNG);
    if (ssl->rng == NULL) {
        WOLFSSL_MSG("RNG Memory error");
        return MEMORY_E;
    }

    if ( (ret = wc_InitRng(ssl->rng)) != 0) {
        WOLFSSL_MSG("RNG Init error");
        return ret;
    }

#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
    if (ssl->options.dtls && ssl->options.side == WOLFSSL_SERVER_END) {
        ret = wolfSSL_DTLS_SetCookieSecret(ssl, NULL, 0);
        if (ret != 0) {
            WOLFSSL_MSG("DTLS Cookie Secret error");
            return ret;
        }
    }
#endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */

#ifdef HAVE_SECRET_CALLBACK
    ssl->sessionSecretCb  = NULL;
    ssl->sessionSecretCtx = NULL;
#endif
    return 0;
}
/* init everything to 0, NULL, default values before calling anything that may
   fail so that destructor has a "good" state to cleanup
   0 on success */
init everything to 0, NULL, default values before calling anything that may
fail so that destructor has a "good" state to cleanup
0 on success
init everything to 0 NULL default values before calling anything that may fail so that destructor has a " good " state to cleanup 0 on success
int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
{
    int  ret;
    XMEMSET(ssl, 0, sizeof(WOLFSSL));
    ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
    ssl->buffers.inputBuffer.bufferSize  = STATIC_BUFFER_LEN;
    ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
    ssl->buffers.outputBuffer.bufferSize  = STATIC_BUFFER_LEN;
#if defined(KEEP_PEER_CERT) || defined(GOAHEAD_WS)
    InitX509(&ssl->peerCert, 0);
#endif
    ssl->rfd = -1;   
    ssl->wfd = -1;
    ssl->IOCB_ReadCtx  = &ssl->rfd;  
    ssl->IOCB_WriteCtx = &ssl->wfd;  
#ifdef HAVE_NETX
    ssl->IOCB_ReadCtx  = &ssl->nxCtx;  
    ssl->IOCB_WriteCtx = &ssl->nxCtx;  
#endif
#ifdef WOLFSSL_DTLS
    ssl->dtls_expected_rx = MAX_MTU;
#endif
    ssl->options.serverState = NULL_STATE;
    ssl->options.clientState = NULL_STATE;
    ssl->options.connectState = CONNECT_BEGIN;
    ssl->options.acceptState  = ACCEPT_BEGIN;
    ssl->options.handShakeState  = NULL_STATE;
    ssl->options.processReply = doProcessInit;
#ifdef WOLFSSL_DTLS
    ssl->dtls_timeout_init              = DTLS_TIMEOUT_INIT;
    ssl->dtls_timeout_max               = DTLS_TIMEOUT_MAX;
    ssl->dtls_timeout                   = ssl->dtls_timeout_init;
#endif
    #ifndef NO_OLD_TLS
        ssl->hmac = SSL_hmac; 
    #else
        ssl->hmac = TLS_hmac;
    #endif
#ifdef WOLFSSL_DTLS
    ssl->buffers.dtlsCtx.fd = -1;
#endif
    ssl->cipher.ssl = ssl;
#ifdef HAVE_TLS_EXTENSIONS
#ifdef HAVE_MAX_FRAGMENT
    ssl->max_fragment = MAX_RECORD_SIZE;
#endif
#ifdef HAVE_ALPN
    ssl->alpn_client_list = NULL;
#endif
#endif
    ssl->alert_history.last_rx.code  = -1;
    ssl->alert_history.last_rx.level = -1;
    ssl->alert_history.last_tx.code  = -1;
    ssl->alert_history.last_tx.level = -1;
    InitCiphers(ssl);
    InitCipherSpecs(&ssl->specs);
    ssl->arrays = (Arrays*)XMALLOC(sizeof(Arrays), ssl->heap,
                                                           DYNAMIC_TYPE_ARRAYS);
    if (ssl->arrays == NULL) {
        WOLFSSL_MSG("Arrays Memory error");
        return MEMORY_E;
    }
    XMEMSET(ssl->arrays, 0, sizeof(Arrays));
    ssl->suites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap,
                                   DYNAMIC_TYPE_SUITES);
    if (ssl->suites == NULL) {
        WOLFSSL_MSG("Suites Memory error");
        return MEMORY_E;
    }
    if((ret =  SetSSL_CTX(ssl, ctx)) != SSL_SUCCESS)
        return ret;
    ssl->options.dtls = ssl->version.major == DTLS_MAJOR;
    ssl->hsHashes = (HS_Hashes*)XMALLOC(sizeof(HS_Hashes), ssl->heap,
                                                           DYNAMIC_TYPE_HASHES);
    if (ssl->hsHashes == NULL) {
        WOLFSSL_MSG("HS_Hashes Memory error");
        return MEMORY_E;
    }
#ifndef NO_OLD_TLS
#ifndef NO_MD5
    wc_InitMd5(&ssl->hsHashes->hashMd5);
#endif
#ifndef NO_SHA
    ret = wc_InitSha(&ssl->hsHashes->hashSha);
    if (ret != 0) {
        return ret;
    }
#endif
#endif
#ifndef NO_SHA256
    ret = wc_InitSha256(&ssl->hsHashes->hashSha256);
    if (ret != 0) {
        return ret;
    }
#endif
#ifdef WOLFSSL_SHA384
    ret = wc_InitSha384(&ssl->hsHashes->hashSha384);
    if (ret != 0) {
        return ret;
    }
#endif
#ifdef WOLFSSL_SHA512
    ret = wc_InitSha512(&ssl->hsHashes->hashSha512);
    if (ret != 0) {
        return ret;
    }
#endif
    ssl->rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), ssl->heap, DYNAMIC_TYPE_RNG);
    if (ssl->rng == NULL) {
        WOLFSSL_MSG("RNG Memory error");
        return MEMORY_E;
    }
    if ( (ret = wc_InitRng(ssl->rng)) != 0) {
        WOLFSSL_MSG("RNG Init error");
        return ret;
    }
#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
    if (ssl->options.dtls && ssl->options.side == WOLFSSL_SERVER_END) {
        ret = wolfSSL_DTLS_SetCookieSecret(ssl, NULL, 0);
        if (ret != 0) {
            WOLFSSL_MSG("DTLS Cookie Secret error");
            return ret;
        }
    }
#endif 
#ifdef HAVE_SECRET_CALLBACK
    ssl->sessionSecretCb  = NULL;
    ssl->sessionSecretCtx = NULL;
#endif
    return 0;
}
int InitSSL ( WOLFSSL * ssl , WOLFSSL_CTX * ctx ) { int ret ; XMEMSET ( ssl , 0 , sizeof ( WOLFSSL ) ) ; ssl -> buffers . inputBuffer . buffer = ssl -> buffers . inputBuffer . staticBuffer ; ssl -> buffers . inputBuffer . bufferSize = STATIC_BUFFER_LEN ; ssl -> buffers . outputBuffer . buffer = ssl -> buffers . outputBuffer . staticBuffer ; ssl -> buffers . outputBuffer . bufferSize = STATIC_BUFFER_LEN ; #if defined ( KEEP_PEER_CERT ) || defined ( GOAHEAD_WS ) InitX509 ( & ssl -> peerCert , 0 ) ; #endif ssl -> rfd = -1 ; ssl -> wfd = -1 ; ssl -> IOCB_ReadCtx = & ssl -> rfd ; ssl -> IOCB_WriteCtx = & ssl -> wfd ; #ifdef HAVE_NETX ssl -> IOCB_ReadCtx = & ssl -> nxCtx ; ssl -> IOCB_WriteCtx = & ssl -> nxCtx ; #endif #ifdef WOLFSSL_DTLS ssl -> dtls_expected_rx = MAX_MTU ; #endif ssl -> options . serverState = NULL_STATE ; ssl -> options . clientState = NULL_STATE ; ssl -> options . connectState = CONNECT_BEGIN ; ssl -> options . acceptState = ACCEPT_BEGIN ; ssl -> options . handShakeState = NULL_STATE ; ssl -> options . processReply = doProcessInit ; #ifdef WOLFSSL_DTLS ssl -> dtls_timeout_init = DTLS_TIMEOUT_INIT ; ssl -> dtls_timeout_max = DTLS_TIMEOUT_MAX ; ssl -> dtls_timeout = ssl -> dtls_timeout_init ; #endif #ifndef NO_OLD_TLS ssl -> hmac = SSL_hmac ; #else ssl -> hmac = TLS_hmac ; #endif #ifdef WOLFSSL_DTLS ssl -> buffers . dtlsCtx . fd = -1 ; #endif ssl -> cipher . ssl = ssl ; #ifdef HAVE_TLS_EXTENSIONS #ifdef HAVE_MAX_FRAGMENT ssl -> max_fragment = MAX_RECORD_SIZE ; #endif #ifdef HAVE_ALPN ssl -> alpn_client_list = NULL ; #endif #endif ssl -> alert_history . last_rx . code = -1 ; ssl -> alert_history . last_rx . level = -1 ; ssl -> alert_history . last_tx . code = -1 ; ssl -> alert_history . last_tx . level = -1 ; InitCiphers ( ssl ) ; InitCipherSpecs ( & ssl -> specs ) ; ssl -> arrays = ( Arrays * ) XMALLOC ( sizeof ( Arrays ) , ssl -> heap , DYNAMIC_TYPE_ARRAYS ) ; if ( ssl -> arrays == NULL ) { WOLFSSL_MSG ( " " ) ; return MEMORY_E ; } XMEMSET ( ssl -> arrays , 0 , sizeof ( Arrays ) ) ; ssl -> suites = ( Suites * ) XMALLOC ( sizeof ( Suites ) , ssl -> heap , DYNAMIC_TYPE_SUITES ) ; if ( ssl -> suites == NULL ) { WOLFSSL_MSG ( " " ) ; return MEMORY_E ; } if ( ( ret = SetSSL_CTX ( ssl , ctx ) ) != SSL_SUCCESS ) return ret ; ssl -> options . dtls = ssl -> version . major == DTLS_MAJOR ; ssl -> hsHashes = ( HS_Hashes * ) XMALLOC ( sizeof ( HS_Hashes ) , ssl -> heap , DYNAMIC_TYPE_HASHES ) ; if ( ssl -> hsHashes == NULL ) { WOLFSSL_MSG ( " " ) ; return MEMORY_E ; } #ifndef NO_OLD_TLS #ifndef NO_MD5 wc_InitMd5 ( & ssl -> hsHashes -> hashMd5 ) ; #endif #ifndef NO_SHA ret = wc_InitSha ( & ssl -> hsHashes -> hashSha ) ; if ( ret != 0 ) { return ret ; } #endif #endif #ifndef NO_SHA256 ret = wc_InitSha256 ( & ssl -> hsHashes -> hashSha256 ) ; if ( ret != 0 ) { return ret ; } #endif #ifdef WOLFSSL_SHA384 ret = wc_InitSha384 ( & ssl -> hsHashes -> hashSha384 ) ; if ( ret != 0 ) { return ret ; } #endif #ifdef WOLFSSL_SHA512 ret = wc_InitSha512 ( & ssl -> hsHashes -> hashSha512 ) ; if ( ret != 0 ) { return ret ; } #endif ssl -> rng = ( WC_RNG * ) XMALLOC ( sizeof ( WC_RNG ) , ssl -> heap , DYNAMIC_TYPE_RNG ) ; if ( ssl -> rng == NULL ) { WOLFSSL_MSG ( " " ) ; return MEMORY_E ; } if ( ( ret = wc_InitRng ( ssl -> rng ) ) != 0 ) { WOLFSSL_MSG ( " " ) ; return ret ; } #if defined ( WOLFSSL_DTLS ) && ! defined ( NO_WOLFSSL_SERVER ) if ( ssl -> options . dtls && ssl -> options . side == WOLFSSL_SERVER_END ) { ret = wolfSSL_DTLS_SetCookieSecret ( ssl , NULL , 0 ) ; if ( ret != 0 ) { WOLFSSL_MSG ( " " ) ; return ret ; } } #endif #ifdef HAVE_SECRET_CALLBACK ssl -> sessionSecretCb = NULL ; ssl -> sessionSecretCtx = NULL ; #endif return 0 ; }
init everything to 0, NULL, default values before calling anything that may
fail so that destructor has a "good" state to cleanup
0 on success
init everything to 0 NULL default values before calling anything that may fail so that destructor has a " good " state to cleanup 0 on success
/* set to invalid descriptor */ /* prevent invalid pointer access if not */ /* correctly set */ /* default NetX IO ctx, same for read */ /* and write */ /* default to SSLv3 */ /* default alert state (none) */ /* all done with init, now can return errors, call other stuff */ /* arrays */ /* suites */ /* Initialize SSL with the appropriate fields from it's ctx */ /* requires valid arrays and suites */ /* hsHashes */ /* RNG */ /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */