<commit_before>#if defined(USE_MBEDTLS)  // USE_MBEDTLS used with NO_SSL

#include <string.h>
#include "mbedtls/certs.h"
#include "mbedtls/ssl.h"
#include "mbedtls/net.h"
#include "mbedtls/pk.h"
#include "mbedtls/x509.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/error.h"
#include "mbedtls/debug.h"
#include "mbedtls/platform.h"

typedef mbedtls_ssl_context SSL;

typedef struct {
    mbedtls_ssl_config conf; /* SSL configuration */
    mbedtls_x509_crt cert; /* Certificate */
    mbedtls_ctr_drbg_context ctr; /* Counter random generator state */
    mbedtls_entropy_context entropy; /* Entropy context */
    mbedtls_pk_context pkey; /* Private key */
} SSL_CTX;

// public api
int mbed_sslctx_init(SSL_CTX *ctx, const char *crt);
void mbed_sslctx_uninit(SSL_CTX *ctx);
void mbed_ssl_close(mbedtls_ssl_context *ssl);
int mbed_ssl_accept(mbedtls_ssl_context **ssl, SSL_CTX *ssl_ctx, int *sock);
int mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len);
int mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len);

static void mbed_debug(void *context, int level, const char *file, int line, const char *str);
static int mbed_ssl_handshake(mbedtls_ssl_context *ssl);

int
mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
{
    mbedtls_ssl_config *conf;
    int rc;

    fprintf(stdout, "Initializing MbedTLS SSL\n");
    if (ctx == NULL || crt == NULL) {
        return -1;
    }

    mbedtls_entropy_init(&ctx->entropy);

    conf = &ctx->conf;
    mbedtls_ssl_config_init(conf);

    // set debug level
    mbedtls_debug_set_threshold(2);
    mbedtls_ssl_conf_dbg(conf, mbed_debug, stdout);
    mbedtls_pk_init(&ctx->pkey);
    mbedtls_ctr_drbg_init(&ctx->ctr);
    mbedtls_x509_crt_init(&ctx->cert);
    if ((rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
                                    mbedtls_entropy_func,
                                    &ctx->entropy,
                                    (unsigned char *)"CivetWeb",
                                    strlen("CivetWeb")))
        != 0) {
        fprintf(stderr, "Cannot seed rng\n");
        return -1;
    }

    if (mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL) != 0) {
        fprintf(stderr, "parse key file failed\n");
        return -1;
    }

    if (mbedtls_x509_crt_parse_file(&ctx->cert, crt) != 0) {
        fprintf(stderr, "parse crt file faied\n");<commit_msg>mbedtls: debug functions need not to be defined, check it<commit_after>#if defined(USE_MBEDTLS)  // USE_MBEDTLS used with NO_SSL

#include <string.h>
#include "mbedtls/certs.h"
#include "mbedtls/ssl.h"
#include "mbedtls/net.h"
#include "mbedtls/pk.h"
#include "mbedtls/x509.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/error.h"
#include "mbedtls/debug.h"
#include "mbedtls/platform.h"

typedef mbedtls_ssl_context SSL;

typedef struct {
    mbedtls_ssl_config conf; /* SSL configuration */
    mbedtls_x509_crt cert; /* Certificate */
    mbedtls_ctr_drbg_context ctr; /* Counter random generator state */
    mbedtls_entropy_context entropy; /* Entropy context */
    mbedtls_pk_context pkey; /* Private key */
} SSL_CTX;

// public api
int mbed_sslctx_init(SSL_CTX *ctx, const char *crt);
void mbed_sslctx_uninit(SSL_CTX *ctx);
void mbed_ssl_close(mbedtls_ssl_context *ssl);
int mbed_ssl_accept(mbedtls_ssl_context **ssl, SSL_CTX *ssl_ctx, int *sock);
int mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len);
int mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len);

static void mbed_debug(void *context, int level, const char *file, int line, const char *str);
static int mbed_ssl_handshake(mbedtls_ssl_context *ssl);

int
mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
{
    mbedtls_ssl_config *conf;
    int rc;

    fprintf(stdout, "Initializing MbedTLS SSL\n");
    if (ctx == NULL || crt == NULL) {
        return -1;
    }

    mbedtls_entropy_init(&ctx->entropy);

    conf = &ctx->conf;
    mbedtls_ssl_config_init(conf);

    // set debug level
#if defined(CONFIG_MBEDTLS_DEBUG)
    mbedtls_debug_set_threshold(2);
    mbedtls_ssl_conf_dbg(conf, mbed_debug, stdout);
#endif
    mbedtls_pk_init(&ctx->pkey);
    mbedtls_ctr_drbg_init(&ctx->ctr);
    mbedtls_x509_crt_init(&ctx->cert);
    if ((rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
                                    mbedtls_entropy_func,
                                    &ctx->entropy,
                                    (unsigned char *)"CivetWeb",
                                    strlen("CivetWeb")))
        != 0) {
        fprintf(stderr, "Cannot seed rng\n");
        return -1;
    }

    if (mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL) != 0) {
        fprintf(stderr, "parse key file failed\n");
        return -1;
    }

    if (mbedtls_x509_crt_parse_file(&ctx->cert, crt) != 0) {
        fprintf(stderr, "parse crt file faied\n");
{'fraction_non_alphanumeric': 0.12139544262956241, 'fraction_numerical': 0.008872756604154063, 'mean_word_length': 3.2356959863364647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5406439', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1833, 'n_words': 383}