<commit_before>//
// Created by <NAME> on 2/15/16.
//

#include <celix_errno.h>
#include <bundle_context.h>
#include <unistd.h>
#include "trust_manager_worker.h"
#include "trust_manager_impl.h"
#include "stdlib.h"
#include <mbedtls/pk.h>
#include <mbedtls/x509_crt.h>
#include "trust_manager_certificaterequester.h"
#include "trust_manager_keygen.h"
#include "trust_manager_storage.h"

#define DEFAULT_WORKER_SLEEP 1

struct trust_worker {
    celix_thread_mutex_t workerLock;
    celix_thread_t workerThread;

    volatile bool running;
};

/**
 *
 */
static int trustWorker_rekey(void) {
    int res;
    mbedtls_pk_context *key = (mbedtls_pk_context *) malloc(sizeof(mbedtls_pk_context));
    generate_keypair(key);

    char *cert_path = malloc(1024);
    char *ca_cert_path = malloc(1024);
    char *pubkey_path = malloc(1024);
    char *privkey_path = malloc(1024);
    char *pubkey = malloc(4096);
    char *privkey = malloc(4096);
    char* cert = malloc(4096);

    get_next_public_key_file_path(pubkey_path);
    res = get_public_key(key, pubkey);
    if (!res) {
        write_pem_to_file(pubkey, pubkey_path);
    } else {
        goto fail;
    }

    get_next_private_key_file_path(privkey_path);
    res = get_private_key(key, privkey);
    if (!res) {
        write_pem_to_file(privkey, privkey_path);
    } else {
        goto fail;
    }

    // request cert from ca
    get_next_certificate_file_path(cert_path);
    res = csr_get_certificate(key, cert);
    if (!res) {
        write_pem_to_file(cert, cert_path);
    } else {
        goto fail;
    }

    printf("\nrekey SUCCESS!");

    fail:
    free(cert);
    free(pubkey_path);
    free(privkey_path);
    free(ca_cert_path);
    free(cert_path);
    free(pubkey);
    free(privkey);
    return res;
}

/**
 * Recusrive trust reload. r specifies tries.
 */
static int refresh_ca_trust_r(mbedtls_x509_crt *ca_cert, int r)
{
    int ret;
    char *ca_cert_filename = malloc(1024);
    ret = get_recent_ca_certificate(ca_cert_filename);
    ret += load_certificate(ca_cert, ca_cert_filename);
    // verify ca cert
    if (ret != 0) {

        char *ca_cert_path = malloc(1024);
        char* ca_cert_buf = malloc(4096);
        get_next_ca_certificate_file_path(ca_cert_path);
        if (!request_ca_certificate(ca_cert_buf)) {
            write_pem_to_file(ca_cert_buf, ca_cert_path);
        }
        free(ca_cert_buf);
        free(ca_cert_path);

        if (r >= 0) {
            refresh_ca_trust_r(ca_cert, --r);
        }
    }
    return ret;
}

/**
 * Reload the trust.
 */
static int refresh_ca_trust(mbedtls_x509_crt *ca_cert)
{
    refresh_ca_trust_r(ca_cert, 2);
}


/*
 * Perform all validation and refresh of certificates / csr's.
 */
static void* trustWorker_run(void* data) {
    trust_worker_pt worker = (trust_worker_pt) data;

    int i = 0;
    while ((celixThreadMutex_lock(&worker->workerLock) == CELIX_SUCCESS) && worker->running) {
        int ret = 0;
        mbedtls_x509_crt *ca_cert = (mbedtls_x509_crt*) malloc(sizeof(mbedtls_x509_crt));
        mbedtls_x509_crt *cert = (mbedtls_x509_crt*) malloc(sizeof(mbedtls_x509_crt));


        refresh_ca_trust(ca_cert);

        char *ca_cert_filename = malloc(1024);
        ret = get_recent_ca_certificate(ca_cert_filename);
        ret += load_certificate(ca_cert, ca_cert_filename);
        // verify ca cert
        if (ret != 0) {
            trustWorker_ca_reload();
            goto fail;
        }

        char *cert_filename = malloc(1024);
        ret = get_recent_certificate(cert_filename);
        ret += load_certificate(cert, cert_filename);

        // verify cert
        if (ret == 0) {
            if (verify_certificate(cert, ca_cert, 0) != 0) {
                trustWorker_rekey();
            } else {
                goto fail;
            }
        } else {
            trustWorker_rekey();
        }

        fail:
        free(ca_cert_filename);
        free(cert_filename);
        free(ca_cert);<commit_msg>continuing exection after successful initial trust reload<commit_after>//
// Created by <NAME> on 2/15/16.
//

#include <celix_errno.h>
#include <bundle_context.h>
#include <unistd.h>
#include "trust_manager_worker.h"
#include "trust_manager_impl.h"
#include "stdlib.h"
#include <mbedtls/pk.h>
#include <mbedtls/x509_crt.h>
#include "trust_manager_certificaterequester.h"
#include "trust_manager_keygen.h"
#include "trust_manager_storage.h"

#define DEFAULT_WORKER_SLEEP 1

struct trust_worker {
    celix_thread_mutex_t workerLock;
    celix_thread_t workerThread;

    volatile bool running;
};

/**
 *
 */
static int trustWorker_rekey(void) {
    int res;
    mbedtls_pk_context *key = (mbedtls_pk_context *) malloc(sizeof(mbedtls_pk_context));
    generate_keypair(key);

    char *cert_path = malloc(1024);
    char *ca_cert_path = malloc(1024);
    char *pubkey_path = malloc(1024);
    char *privkey_path = malloc(1024);
    char *pubkey = malloc(4096);
    char *privkey = malloc(4096);
    char* cert = malloc(4096);

    get_next_public_key_file_path(pubkey_path);
    res = get_public_key(key, pubkey);
    if (!res) {
        write_pem_to_file(pubkey, pubkey_path);
    } else {
        goto fail;
    }

    get_next_private_key_file_path(privkey_path);
    res = get_private_key(key, privkey);
    if (!res) {
        write_pem_to_file(privkey, privkey_path);
    } else {
        goto fail;
    }

    // request cert from ca
    get_next_certificate_file_path(cert_path);
    res = csr_get_certificate(key, cert);
    if (!res) {
        write_pem_to_file(cert, cert_path);
    } else {
        goto fail;
    }

    printf("\nrekey SUCCESS!");

    fail:
    free(cert);
    free(pubkey_path);
    free(privkey_path);
    free(ca_cert_path);
    free(cert_path);
    free(pubkey);
    free(privkey);
    return res;
}

/**
 * Recusrive trust reload. r specifies tries.
 */
static int refresh_ca_trust_r(mbedtls_x509_crt *ca_cert, int r)
{
    int ret;
    char *ca_cert_filename = malloc(1024);
    ret = get_recent_ca_certificate(ca_cert_filename);
    ret += load_certificate(ca_cert, ca_cert_filename);
    // verify ca cert
    if (ret != 0) {

        char *ca_cert_path = malloc(1024);
        char* ca_cert_buf = malloc(4096);
        get_next_ca_certificate_file_path(ca_cert_path);
        if (!request_ca_certificate(ca_cert_buf)) {
            write_pem_to_file(ca_cert_buf, ca_cert_path);
        }
        free(ca_cert_buf);
        free(ca_cert_path);

        if (r >= 0) {
            refresh_ca_trust_r(ca_cert, --r);
        }
    }
    return ret;
}

/**
 * Reload the trust.
 */
static int refresh_ca_trust(mbedtls_x509_crt *ca_cert)
{
    return refresh_ca_trust_r(ca_cert, 2);
}


/*
 * Perform all validation and refresh of certificates / csr's.
 */
static void* trustWorker_run(void* data) {
    trust_worker_pt worker = (trust_worker_pt) data;

    int i = 0;
    while ((celixThreadMutex_lock(&worker->workerLock) == CELIX_SUCCESS) && worker->running) {
        int ret = 0;
        mbedtls_x509_crt *ca_cert = (mbedtls_x509_crt*) malloc(sizeof(mbedtls_x509_crt));
        mbedtls_x509_crt *cert = (mbedtls_x509_crt*) malloc(sizeof(mbedtls_x509_crt));


        refresh_ca_trust(ca_cert);

        char *ca_cert_filename = malloc(1024);
        ret = get_recent_ca_certificate(ca_cert_filename);
        ret += load_certificate(ca_cert, ca_cert_filename);
        // verify ca cert
        if (ret != 0) {
            if (!trustWorker_ca_reload()) {
                printf("hard ca fail");
                goto fail;
            }
        }

        char *cert_filename = malloc(1024);
        ret = get_recent_certificate(cert_filename);
        ret += load_certificate(cert, cert_filename);

        // verify cert
        if (ret == 0) {
            if (verify_certificate(cert, ca_cert, 0) != 0) {
                trustWorker_rekey();
            } else {
                goto fail;
            }
        } else {
            trustWorker_rekey();
        }

        fail:
        free(ca_cert_filename);
        free(cert_filename);
        free(ca_cert);
{'fraction_non_alphanumeric': 0.11387240356083086, 'fraction_numerical': 0.02225519287833828, 'mean_word_length': 2.776377217553688, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, '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': '833464', 'n_tokens_mistral': 3074, 'n_tokens_neox': 2833, 'n_words': 524}