<commit_before>#include <assert.h>
#include "evt_tls.h"


//openssl 1.0.2 and later has SSL_is_server API to check 
//if the ssl connection is server or not
// Some older versions does not have this function.
// Hence this function is introduced.

// 0 - client
// 1 - server
int SSL_get_role(const SSL *s)
{
    return s->server;
}


static void tls_begin(void)
{
    SSL_library_init();
    SSL_load_error_strings();
    ERR_load_BIO_strings();
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();
}


evt_tls_t *getSSL(evt_ctx_t *d_eng)
{
     evt_tls_t *con = malloc(sizeof(evt_tls_t));
     if ( !con ) {
	 return NULL;
     }
     memset( con, 0, sizeof *con);

     SSL *ssl  = SSL_new(d_eng->ctx);

     if ( !ssl ) {
	 return NULL;
     }
     con->ssl = ssl;

     //use default buf size for now.
     BIO_new_bio_pair(&(con->ssl_bio_), 0, &(con->app_bio_), 0);

     SSL_set_bio(con->ssl, con->ssl_bio_, con->ssl_bio_);

     QUEUE_INIT(&(con->q));
     QUEUE_INSERT_TAIL(&(d_eng->live_con), &(con->q));

     con->writer = d_eng->writer;

     return con;
}

void evt_ctx_set_writer(evt_ctx_t *ctx, net_wrtr my_writer)
{
    assert(ctx != NULL);
    assert( ctx->writer == NULL);
    ctx->writer = my_writer;
    assert( ctx->writer != NULL);

}

void evt_ctx_set_rdr(evt_ctx_t *ctx, net_rdr my_rdr)
{
    assert(ctx != NULL);
    assert( ctx->rdr == NULL);
    ctx->rdr = my_rdr;
    assert( ctx->writer != NULL);

}

void evt_ctx_set_nio(evt_ctx_t *ctx, net_wrtr my_writer, net_rdr my_rdr)
{
    assert(ctx != NULL);
    assert( ctx->writer == NULL);
    ctx->writer = my_writer;
    assert( ctx->writer != NULL);

    assert(ctx != NULL);
    assert( ctx->rdr == NULL);
    ctx->rdr = my_rdr;
    assert( ctx->writer != NULL);
}


int evt_ctx_set_crt_key(evt_ctx_t *tls, char *crtf, char *key)
{
    //SSL_CTX_set_verify(tls->ctx, SSL_VERIFY_NONE, uv__tls_verify_peer);
    SSL_CTX_set_verify(tls->ctx, SSL_VERIFY_NONE, NULL);

    int r = SSL_CTX_use_certificate_file(tls->ctx, crtf, SSL_FILETYPE_PEM);
    if(r != 1) {
        return -1;
    }
    tls->cert_set = 1;

    r = SSL_CTX_use_PrivateKey_file(tls->ctx, key, SSL_FILETYPE_PEM);
    if(r != 1) {
        return -1;
    }

    r = SSL_CTX_check_private_key(tls->ctx);
    if(r != 1) {
        return -1;
    }
    tls->key_set = 1;
    return 0;
}


int evt_ctx_init(evt_ctx_t *tls)<commit_msg>remove two unused functions for setting writer and reader<commit_after>#include <assert.h>
#include "evt_tls.h"


//openssl 1.0.2 and later has SSL_is_server API to check 
//if the ssl connection is server or not
// Some older versions does not have this function.
// Hence this function is introduced.

// 0 - client
// 1 - server
int SSL_get_role(const SSL *s)
{
    return s->server;
}


static void tls_begin(void)
{
    SSL_library_init();
    SSL_load_error_strings();
    ERR_load_BIO_strings();
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();
}


evt_tls_t *getSSL(evt_ctx_t *d_eng)
{
     evt_tls_t *con = malloc(sizeof(evt_tls_t));
     if ( !con ) {
	 return NULL;
     }
     memset( con, 0, sizeof *con);

     SSL *ssl  = SSL_new(d_eng->ctx);

     if ( !ssl ) {
	 return NULL;
     }
     con->ssl = ssl;

     //use default buf size for now.
     BIO_new_bio_pair(&(con->ssl_bio_), 0, &(con->app_bio_), 0);

     SSL_set_bio(con->ssl, con->ssl_bio_, con->ssl_bio_);

     QUEUE_INIT(&(con->q));
     QUEUE_INSERT_TAIL(&(d_eng->live_con), &(con->q));

     con->writer = d_eng->writer;

     return con;
}

void evt_ctx_set_writer(evt_ctx_t *ctx, net_wrtr my_writer)
{
    assert(ctx != NULL);
    assert( ctx->writer == NULL);
    ctx->writer = my_writer;
    assert( ctx->writer != NULL);

}

int evt_ctx_set_crt_key(evt_ctx_t *tls, char *crtf, char *key)
{
    //SSL_CTX_set_verify(tls->ctx, SSL_VERIFY_NONE, uv__tls_verify_peer);
    SSL_CTX_set_verify(tls->ctx, SSL_VERIFY_NONE, NULL);

    int r = SSL_CTX_use_certificate_file(tls->ctx, crtf, SSL_FILETYPE_PEM);
    if(r != 1) {
        return -1;
    }
    tls->cert_set = 1;

    r = SSL_CTX_use_PrivateKey_file(tls->ctx, key, SSL_FILETYPE_PEM);
    if(r != 1) {
        return -1;
    }

    r = SSL_CTX_check_private_key(tls->ctx);
    if(r != 1) {
        return -1;
    }
    tls->key_set = 1;
    return 0;
}


int evt_ctx_init(evt_ctx_t *tls)
{'fraction_non_alphanumeric': 0.14355174808983562, 'fraction_numerical': 0.007872192637184533, 'mean_word_length': 3.003707136237257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5117973', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1713, 'n_words': 361}