src/wolfSSL/src/internal.c
int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
{
    byte havePSK = 0;
    byte haveAnon = 0;
    byte newSSL;
    byte haveRSA = 0;
    (void) haveAnon; /* Squash unused var warnings */

    if(!ssl || !ctx || ssl->suites == NULL)
        return BAD_FUNC_ARG;

    newSSL = ssl->ctx == NULL; /* Assign after null check */

#ifndef NO_PSK
    if (ctx->server_hint[0] && ssl->arrays == NULL) {
        return BAD_FUNC_ARG;  /* needed for copy below */
    }
#endif


#ifndef NO_RSA
    haveRSA = 1;
#endif
#ifndef NO_PSK
    havePSK = ctx->havePSK;
#endif /* NO_PSK */
#ifdef HAVE_ANON
    haveAnon = ctx->haveAnon;
#endif /* HAVE_ANON*/

    /* decrement previous CTX reference count if exists.
     * This should only happen if switching ctxs!*/
    if (!newSSL) {
        WOLFSSL_MSG("freeing old ctx to decrement reference count. Switching ctx.");
        wolfSSL_CTX_free(ssl->ctx);
    }

    /* increment CTX reference count */
    if (LockMutex(&ctx->countMutex) != 0) {
        WOLFSSL_MSG("Couldn't lock CTX count mutex");
        return BAD_MUTEX_E;
    }
    ctx->refCount++;
    UnLockMutex(&ctx->countMutex);
    ssl->ctx     = ctx; /* only for passing to calls, options could change */
    ssl->version = ctx->method->version;

#ifdef HAVE_ECC
    ssl->eccTempKeySz = ctx->eccTempKeySz;
    ssl->pkCurveOID = ctx->pkCurveOID;
#endif

    ssl->timeout = ctx->timeout;
    ssl->verifyCallback    = ctx->verifyCallback;
    ssl->options.side      = ctx->method->side;
    ssl->options.downgrade    = ctx->method->downgrade;
    ssl->options.minDowngrade = ctx->minDowngrade;

    if (ssl->options.side == WOLFSSL_SERVER_END)
        ssl->options.haveDH = ctx->haveDH;

    ssl->options.haveNTRU      = ctx->haveNTRU;
    ssl->options.haveECDSAsig  = ctx->haveECDSAsig;
    ssl->options.haveECC       = ctx->haveECC;
    ssl->options.haveStaticECC = ctx->haveStaticECC;

#ifndef NO_PSK
    ssl->options.havePSK   = ctx->havePSK;
    ssl->options.client_psk_cb = ctx->client_psk_cb;
    ssl->options.server_psk_cb = ctx->server_psk_cb;
#endif /* NO_PSK */

#ifdef HAVE_ANON
    ssl->options.haveAnon = ctx->haveAnon;
#endif
#ifndef NO_DH
    ssl->options.minDhKeySz = ctx->minDhKeySz;
#endif

    ssl->options.sessionCacheOff      = ctx->sessionCacheOff;
    ssl->options.sessionCacheFlushOff = ctx->sessionCacheFlushOff;

    ssl->options.verifyPeer     = ctx->verifyPeer;
    ssl->options.verifyNone     = ctx->verifyNone;
    ssl->options.failNoCert     = ctx->failNoCert;
    ssl->options.failNoCertxPSK = ctx->failNoCertxPSK;
    ssl->options.sendVerify     = ctx->sendVerify;

    ssl->heap = ctx->heap;    /* defaults to self */
    ssl->options.partialWrite  = ctx->partialWrite;
    ssl->options.quietShutdown = ctx->quietShutdown;
    ssl->options.groupMessages = ctx->groupMessages;

#ifndef NO_DH
    if (ssl->options.side == WOLFSSL_SERVER_END) {
        ssl->buffers.serverDH_P = ctx->serverDH_P;
        ssl->buffers.serverDH_G = ctx->serverDH_G;
    }
#endif

#ifndef NO_CERTS
    /* ctx still owns certificate, certChain, key, dh, and cm */
    ssl->buffers.certificate = ctx->certificate;
    ssl->buffers.certChain = ctx->certChain;
    ssl->buffers.key = ctx->privateKey;
#endif

#ifdef HAVE_CAVIUM
    ssl->devId = ctx->devId;
#endif

#ifndef NO_PSK
    if (ctx->server_hint[0]) {   /* set in CTX */
        XSTRNCPY(ssl->arrays->server_hint, ctx->server_hint, MAX_PSK_ID_LEN);
        ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
    }
#endif /* NO_PSK */

    if (ctx->suites)
        *ssl->suites = *ctx->suites;
    else
        XMEMSET(ssl->suites, 0, sizeof(Suites));

    /* make sure server has DH parms, and add PSK if there, add NTRU too */
    if (ssl->options.side == WOLFSSL_SERVER_END)
        InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
                   ssl->options.haveDH, ssl->options.haveNTRU,
                   ssl->options.haveECDSAsig, ssl->options.haveECC,
                   ssl->options.haveStaticECC, ssl->options.side);
    else
        InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, TRUE,
                   ssl->options.haveNTRU, ssl->options.haveECDSAsig,
                   ssl->options.haveECC, ssl->options.haveStaticECC,
                   ssl->options.side);

#ifndef NO_CERTS
    /* make sure server has cert and key unless using PSK or Anon
     * This should be true even if just switching ssl ctx */
    if (ssl->options.side == WOLFSSL_SERVER_END && !havePSK && !haveAnon)
        if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer ||
            !ssl->buffers.key || !ssl->buffers.key->buffer) {
            WOLFSSL_MSG("Server missing certificate and/or private key");
            return NO_PRIVATE_KEY;
        }
#endif

    return SSL_SUCCESS;
}
/* This function inherits a WOLFSSL_CTX's fields into an SSL object.
   It is used during initialization and to switch an ssl's CTX with
   wolfSSL_Set_SSL_CTX.  Requires ssl->suites alloc and ssl-arrays with PSK
   SSL_SUCCESS return value on success */
This function inherits a WOLFSSL_CTX's fields into an SSL object.
It is used during initialization and to switch an ssl's CTX with
wolfSSL_Set_SSL_CTX.  Requires ssl->suites alloc and ssl-arrays with PSK
SSL_SUCCESS return value on success
This function inherits a WOLFSSL_CTX ' s fields into an SSL object . It is used during initialization and to switch an ssl ' s CTX with wolfSSL_Set_SSL_CTX . Requires ssl - > suites alloc and ssl - arrays with PSK SSL_SUCCESS return value on success
int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
{
    byte havePSK = 0;
    byte haveAnon = 0;
    byte newSSL;
    byte haveRSA = 0;
    (void) haveAnon; 
    if(!ssl || !ctx || ssl->suites == NULL)
        return BAD_FUNC_ARG;
    newSSL = ssl->ctx == NULL; 
#ifndef NO_PSK
    if (ctx->server_hint[0] && ssl->arrays == NULL) {
        return BAD_FUNC_ARG;  
    }
#endif
#ifndef NO_RSA
    haveRSA = 1;
#endif
#ifndef NO_PSK
    havePSK = ctx->havePSK;
#endif 
#ifdef HAVE_ANON
    haveAnon = ctx->haveAnon;
#endif 
    if (!newSSL) {
        WOLFSSL_MSG("freeing old ctx to decrement reference count. Switching ctx.");
        wolfSSL_CTX_free(ssl->ctx);
    }
    if (LockMutex(&ctx->countMutex) != 0) {
        WOLFSSL_MSG("Couldn't lock CTX count mutex");
        return BAD_MUTEX_E;
    }
    ctx->refCount++;
    UnLockMutex(&ctx->countMutex);
    ssl->ctx     = ctx; 
    ssl->version = ctx->method->version;
#ifdef HAVE_ECC
    ssl->eccTempKeySz = ctx->eccTempKeySz;
    ssl->pkCurveOID = ctx->pkCurveOID;
#endif
    ssl->timeout = ctx->timeout;
    ssl->verifyCallback    = ctx->verifyCallback;
    ssl->options.side      = ctx->method->side;
    ssl->options.downgrade    = ctx->method->downgrade;
    ssl->options.minDowngrade = ctx->minDowngrade;
    if (ssl->options.side == WOLFSSL_SERVER_END)
        ssl->options.haveDH = ctx->haveDH;
    ssl->options.haveNTRU      = ctx->haveNTRU;
    ssl->options.haveECDSAsig  = ctx->haveECDSAsig;
    ssl->options.haveECC       = ctx->haveECC;
    ssl->options.haveStaticECC = ctx->haveStaticECC;
#ifndef NO_PSK
    ssl->options.havePSK   = ctx->havePSK;
    ssl->options.client_psk_cb = ctx->client_psk_cb;
    ssl->options.server_psk_cb = ctx->server_psk_cb;
#endif 
#ifdef HAVE_ANON
    ssl->options.haveAnon = ctx->haveAnon;
#endif
#ifndef NO_DH
    ssl->options.minDhKeySz = ctx->minDhKeySz;
#endif
    ssl->options.sessionCacheOff      = ctx->sessionCacheOff;
    ssl->options.sessionCacheFlushOff = ctx->sessionCacheFlushOff;
    ssl->options.verifyPeer     = ctx->verifyPeer;
    ssl->options.verifyNone     = ctx->verifyNone;
    ssl->options.failNoCert     = ctx->failNoCert;
    ssl->options.failNoCertxPSK = ctx->failNoCertxPSK;
    ssl->options.sendVerify     = ctx->sendVerify;
    ssl->heap = ctx->heap;    
    ssl->options.partialWrite  = ctx->partialWrite;
    ssl->options.quietShutdown = ctx->quietShutdown;
    ssl->options.groupMessages = ctx->groupMessages;
#ifndef NO_DH
    if (ssl->options.side == WOLFSSL_SERVER_END) {
        ssl->buffers.serverDH_P = ctx->serverDH_P;
        ssl->buffers.serverDH_G = ctx->serverDH_G;
    }
#endif
#ifndef NO_CERTS
    ssl->buffers.certificate = ctx->certificate;
    ssl->buffers.certChain = ctx->certChain;
    ssl->buffers.key = ctx->privateKey;
#endif
#ifdef HAVE_CAVIUM
    ssl->devId = ctx->devId;
#endif
#ifndef NO_PSK
    if (ctx->server_hint[0]) {   
        XSTRNCPY(ssl->arrays->server_hint, ctx->server_hint, MAX_PSK_ID_LEN);
        ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
    }
#endif 
    if (ctx->suites)
        *ssl->suites = *ctx->suites;
    else
        XMEMSET(ssl->suites, 0, sizeof(Suites));
    if (ssl->options.side == WOLFSSL_SERVER_END)
        InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
                   ssl->options.haveDH, ssl->options.haveNTRU,
                   ssl->options.haveECDSAsig, ssl->options.haveECC,
                   ssl->options.haveStaticECC, ssl->options.side);
    else
        InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, TRUE,
                   ssl->options.haveNTRU, ssl->options.haveECDSAsig,
                   ssl->options.haveECC, ssl->options.haveStaticECC,
                   ssl->options.side);
#ifndef NO_CERTS
    if (ssl->options.side == WOLFSSL_SERVER_END && !havePSK && !haveAnon)
        if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer ||
            !ssl->buffers.key || !ssl->buffers.key->buffer) {
            WOLFSSL_MSG("Server missing certificate and/or private key");
            return NO_PRIVATE_KEY;
        }
#endif
    return SSL_SUCCESS;
}
int SetSSL_CTX ( WOLFSSL * ssl , WOLFSSL_CTX * ctx ) { byte havePSK = 0 ; byte haveAnon = 0 ; byte newSSL ; byte haveRSA = 0 ; ( void ) haveAnon ; if ( ! ssl || ! ctx || ssl -> suites == NULL ) return BAD_FUNC_ARG ; newSSL = ssl -> ctx == NULL ; #ifndef NO_PSK if ( ctx -> server_hint [ 0 ] && ssl -> arrays == NULL ) { return BAD_FUNC_ARG ; } #endif #ifndef NO_RSA haveRSA = 1 ; #endif #ifndef NO_PSK havePSK = ctx -> havePSK ; #endif #ifdef HAVE_ANON haveAnon = ctx -> haveAnon ; #endif if ( ! newSSL ) { WOLFSSL_MSG ( " " ) ; wolfSSL_CTX_free ( ssl -> ctx ) ; } if ( LockMutex ( & ctx -> countMutex ) != 0 ) { WOLFSSL_MSG ( " " ) ; return BAD_MUTEX_E ; } ctx -> refCount ++ ; UnLockMutex ( & ctx -> countMutex ) ; ssl -> ctx = ctx ; ssl -> version = ctx -> method -> version ; #ifdef HAVE_ECC ssl -> eccTempKeySz = ctx -> eccTempKeySz ; ssl -> pkCurveOID = ctx -> pkCurveOID ; #endif ssl -> timeout = ctx -> timeout ; ssl -> verifyCallback = ctx -> verifyCallback ; ssl -> options . side = ctx -> method -> side ; ssl -> options . downgrade = ctx -> method -> downgrade ; ssl -> options . minDowngrade = ctx -> minDowngrade ; if ( ssl -> options . side == WOLFSSL_SERVER_END ) ssl -> options . haveDH = ctx -> haveDH ; ssl -> options . haveNTRU = ctx -> haveNTRU ; ssl -> options . haveECDSAsig = ctx -> haveECDSAsig ; ssl -> options . haveECC = ctx -> haveECC ; ssl -> options . haveStaticECC = ctx -> haveStaticECC ; #ifndef NO_PSK ssl -> options . havePSK = ctx -> havePSK ; ssl -> options . client_psk_cb = ctx -> client_psk_cb ; ssl -> options . server_psk_cb = ctx -> server_psk_cb ; #endif #ifdef HAVE_ANON ssl -> options . haveAnon = ctx -> haveAnon ; #endif #ifndef NO_DH ssl -> options . minDhKeySz = ctx -> minDhKeySz ; #endif ssl -> options . sessionCacheOff = ctx -> sessionCacheOff ; ssl -> options . sessionCacheFlushOff = ctx -> sessionCacheFlushOff ; ssl -> options . verifyPeer = ctx -> verifyPeer ; ssl -> options . verifyNone = ctx -> verifyNone ; ssl -> options . failNoCert = ctx -> failNoCert ; ssl -> options . failNoCertxPSK = ctx -> failNoCertxPSK ; ssl -> options . sendVerify = ctx -> sendVerify ; ssl -> heap = ctx -> heap ; ssl -> options . partialWrite = ctx -> partialWrite ; ssl -> options . quietShutdown = ctx -> quietShutdown ; ssl -> options . groupMessages = ctx -> groupMessages ; #ifndef NO_DH if ( ssl -> options . side == WOLFSSL_SERVER_END ) { ssl -> buffers . serverDH_P = ctx -> serverDH_P ; ssl -> buffers . serverDH_G = ctx -> serverDH_G ; } #endif #ifndef NO_CERTS ssl -> buffers . certificate = ctx -> certificate ; ssl -> buffers . certChain = ctx -> certChain ; ssl -> buffers . key = ctx -> privateKey ; #endif #ifdef HAVE_CAVIUM ssl -> devId = ctx -> devId ; #endif #ifndef NO_PSK if ( ctx -> server_hint [ 0 ] ) { XSTRNCPY ( ssl -> arrays -> server_hint , ctx -> server_hint , MAX_PSK_ID_LEN ) ; ssl -> arrays -> server_hint [ MAX_PSK_ID_LEN - 1 ] = ' \0 ' ; } #endif if ( ctx -> suites ) * ssl -> suites = * ctx -> suites ; else XMEMSET ( ssl -> suites , 0 , sizeof ( Suites ) ) ; if ( ssl -> options . side == WOLFSSL_SERVER_END ) InitSuites ( ssl -> suites , ssl -> version , haveRSA , havePSK , ssl -> options . haveDH , ssl -> options . haveNTRU , ssl -> options . haveECDSAsig , ssl -> options . haveECC , ssl -> options . haveStaticECC , ssl -> options . side ) ; else InitSuites ( ssl -> suites , ssl -> version , haveRSA , havePSK , TRUE , ssl -> options . haveNTRU , ssl -> options . haveECDSAsig , ssl -> options . haveECC , ssl -> options . haveStaticECC , ssl -> options . side ) ; #ifndef NO_CERTS if ( ssl -> options . side == WOLFSSL_SERVER_END && ! havePSK && ! haveAnon ) if ( ! ssl -> buffers . certificate || ! ssl -> buffers . certificate -> buffer || ! ssl -> buffers . key || ! ssl -> buffers . key -> buffer ) { WOLFSSL_MSG ( " " ) ; return NO_PRIVATE_KEY ; } #endif return SSL_SUCCESS ; }
This function inherits a WOLFSSL_CTX's fields into an SSL object.
This function inherits a WOLFSSL_CTX ' s fields into an SSL object .
/* Squash unused var warnings */ /* Assign after null check */ /* needed for copy below */ /* NO_PSK */ /* HAVE_ANON*/ /* decrement previous CTX reference count if exists.
     * This should only happen if switching ctxs!*/ /* increment CTX reference count */ /* only for passing to calls, options could change */ /* NO_PSK */ /* defaults to self */ /* ctx still owns certificate, certChain, key, dh, and cm */ /* set in CTX */ /* NO_PSK */ /* make sure server has DH parms, and add PSK if there, add NTRU too */ /* make sure server has cert and key unless using PSK or Anon
     * This should be true even if just switching ssl ctx */