Skip to content

Commit

Permalink
Style fixed using clang-format.
Browse files Browse the repository at this point in the history
Closes #427.
  • Loading branch information
stefhak authored and superdump committed Oct 22, 2015
1 parent 4a2e390 commit 1a17818
Showing 1 changed file with 132 additions and 138 deletions.
270 changes: 132 additions & 138 deletions transport/owr_crypto_utils.c
Expand Up @@ -41,7 +41,6 @@
#include "owr_private.h"
#include "owr_utils.h"


#ifdef OWR_STATIC
#include <stdlib.h>
#endif
Expand Down Expand Up @@ -73,203 +72,198 @@ GST_DEBUG_CATEGORY_EXTERN(_owrcrypto_debug);
* OwrCaptureSourcesCallback:
* @privatekey: (transfer none)
* @certificate: (transfer none)
* @fingerprint: (transfer none)
* @user_data: (allow-none):
*
* Prototype for the callback passed to owr_get_capture_sources()
*/

typedef struct {
OwrCryptoDataCallback callback;
gboolean errorDetected;
gchar *pem_key;
gchar *pem_cert;
gchar *char_fprint;
OwrCryptoDataCallback callback;
gboolean errorDetected;
gchar* pem_key;
gchar* pem_cert;
gchar* char_fprint;
} CryptoData;


void owr_crypto_create_crypto_data(OwrCryptoDataCallback callback)
{
GThread *crypto_worker;
GThread* crypto_worker;

crypto_worker = g_thread_new("crypto_worker", _create_crypto_worker_run, (gpointer)callback);
crypto_worker = g_thread_new("crypto_worker", _create_crypto_worker_run, (gpointer)callback);

g_thread_unref(crypto_worker);
g_thread_unref(crypto_worker);
}

gpointer _create_crypto_worker_run(gpointer data) {

OwrCryptoDataCallback callback = (OwrCryptoDataCallback) data;

g_return_val_if_fail(callback, NULL);

X509 *cert;
gpointer _create_crypto_worker_run(gpointer data)
{

X509_NAME *name = NULL;
OwrCryptoDataCallback callback = (OwrCryptoDataCallback)data;

g_return_val_if_fail(callback, NULL);

EVP_PKEY *key_pair;
X509* cert;

RSA *rsa;
X509_NAME* name = NULL;

#define GST_DTLS_BIO_BUFFER_SIZE 4096
BIO *bio_cert;
gchar buffer_cert[GST_DTLS_BIO_BUFFER_SIZE] = { 0 };
gint len_cert;
gchar *pem_cert = NULL;
BIO *bio_key;
gchar buffer_key[GST_DTLS_BIO_BUFFER_SIZE] = { 0 };
gint len_key;
gchar *pem_key = NULL;
EVP_PKEY* key_pair;

gboolean errorDetected = FALSE;
RSA* rsa;

bio_cert = BIO_new (BIO_s_mem ());
bio_key = BIO_new (BIO_s_mem ());
#define GST_DTLS_BIO_BUFFER_SIZE 4096
BIO* bio_cert;
gchar buffer_cert[GST_DTLS_BIO_BUFFER_SIZE] = { 0 };
gint len_cert;
gchar* pem_cert = NULL;
BIO* bio_key;
gchar buffer_key[GST_DTLS_BIO_BUFFER_SIZE] = { 0 };
gint len_key;
gchar* pem_key = NULL;

GString *string_fprint = NULL;
gchar *char_fprint = NULL;
guint j;
const EVP_MD *fprint_type = NULL;
fprint_type = EVP_sha256();
guchar fprint[EVP_MAX_MD_SIZE];
gboolean errorDetected = FALSE;

guint fprint_size;
bio_cert = BIO_new(BIO_s_mem());
bio_key = BIO_new(BIO_s_mem());

cert = X509_new ();
GString* string_fprint = NULL;
gchar* char_fprint = NULL;
guint j;
const EVP_MD* fprint_type = NULL;
fprint_type = EVP_sha256();
guchar fprint[EVP_MAX_MD_SIZE];

key_pair = EVP_PKEY_new ();
guint fprint_size;

rsa = RSA_generate_key (2048, RSA_F4, NULL, NULL);
cert = X509_new();

EVP_PKEY_assign_RSA (key_pair, rsa);
key_pair = EVP_PKEY_new();

X509_set_version (cert, 2);
ASN1_INTEGER_set (X509_get_serialNumber (cert), 0);
X509_gmtime_adj (X509_get_notBefore (cert), 0);
X509_gmtime_adj (X509_get_notAfter (cert), 31536000L); /* A year */
X509_set_pubkey (cert, key_pair);
rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL);

name = X509_get_subject_name (cert);
X509_NAME_add_entry_by_txt (name, "C", MBSTRING_ASC, (unsigned char *) "SE",
-1, -1, 0);
X509_NAME_add_entry_by_txt (name, "CN", MBSTRING_ASC,
(unsigned char *) "OpenWebRTC", -1, -1, 0);
X509_set_issuer_name (cert, name);
name = NULL;
EVP_PKEY_assign_RSA(key_pair, rsa);

X509_sign (cert, key_pair, EVP_sha256 ());
X509_set_version(cert, 2);
ASN1_INTEGER_set(X509_get_serialNumber(cert), 0);
X509_gmtime_adj(X509_get_notBefore(cert), 0);
X509_gmtime_adj(X509_get_notAfter(cert), 31536000L); /* A year */
X509_set_pubkey(cert, key_pair);

if (!X509_digest(cert, fprint_type, fprint, &fprint_size)) {
GST_ERROR("Error, could not create certificate fingerprint");
errorDetected = TRUE;
}
name = X509_get_subject_name(cert);
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char*)"SE",
-1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
(unsigned char*)"OpenWebRTC", -1, -1, 0);
X509_set_issuer_name(cert, name);
name = NULL;

string_fprint = g_string_new (NULL);
X509_sign(cert, key_pair, EVP_sha256());

for (j=0; j < fprint_size; j++) {
g_string_append_printf(string_fprint, "%02X", fprint[j]);
if (j+1 != fprint_size) {
g_string_append_printf(string_fprint, "%c", ':');
}
}
if (!X509_digest(cert, fprint_type, fprint, &fprint_size)) {
GST_ERROR("Error, could not create certificate fingerprint");
errorDetected = TRUE;
}

char_fprint = g_string_free(string_fprint, FALSE);
string_fprint = g_string_new(NULL);

if (!PEM_write_bio_X509 (bio_cert, (X509 *) cert)) {
GST_ERROR("Error, could not write certificate bio");
errorDetected = TRUE;
}
for (j = 0; j < fprint_size; j++) {
g_string_append_printf(string_fprint, "%02X", fprint[j]);
if (j + 1 != fprint_size) {
g_string_append_printf(string_fprint, "%c", ':');
}
}

char_fprint = g_string_free(string_fprint, FALSE);

if (!PEM_write_bio_PrivateKey (bio_key, (EVP_PKEY *) key_pair, NULL, NULL, 0, 0, NULL)) {
GST_ERROR("Error, could not write PrivateKey bio");
errorDetected = TRUE;
}
if (!PEM_write_bio_X509(bio_cert, (X509*)cert)) {
GST_ERROR("Error, could not write certificate bio");
errorDetected = TRUE;
}

len_cert = BIO_read (bio_cert, buffer_cert, GST_DTLS_BIO_BUFFER_SIZE);
if (!len_cert) {
GST_ERROR("Error, no certificate length");
errorDetected = TRUE;
}
if (!PEM_write_bio_PrivateKey(bio_key, (EVP_PKEY*)key_pair, NULL, NULL, 0, 0, NULL)) {
GST_ERROR("Error, could not write PrivateKey bio");
errorDetected = TRUE;
}

len_key = BIO_read (bio_key, buffer_key, GST_DTLS_BIO_BUFFER_SIZE);
if (!len_key) {
GST_ERROR("Error, no key length");
errorDetected = TRUE;
}
len_cert = BIO_read(bio_cert, buffer_cert, GST_DTLS_BIO_BUFFER_SIZE);
if (!len_cert) {
GST_ERROR("Error, no certificate length");
errorDetected = TRUE;
}

pem_cert = g_strndup (buffer_cert, len_cert);
pem_key = g_strndup (buffer_key, len_key);
len_key = BIO_read(bio_key, buffer_key, GST_DTLS_BIO_BUFFER_SIZE);
if (!len_key) {
GST_ERROR("Error, no key length");
errorDetected = TRUE;
}

CryptoData *report_data = g_new0(CryptoData, 1);
pem_cert = g_strndup(buffer_cert, len_cert);
pem_key = g_strndup(buffer_key, len_key);

report_data->callback = callback;
report_data->errorDetected = errorDetected;
report_data->pem_key = pem_key;
report_data->pem_cert = pem_cert;
report_data->char_fprint = char_fprint;
CryptoData* report_data = g_new0(CryptoData, 1);

g_idle_add(_create_crypto_worker_report, (gpointer)report_data);
report_data->callback = callback;
report_data->errorDetected = errorDetected;
report_data->pem_key = pem_key;
report_data->pem_cert = pem_cert;
report_data->char_fprint = char_fprint;

// some cleanup
g_idle_add(_create_crypto_worker_report, (gpointer)report_data);

//RSA_free(rsa); -- gives segmentation fault about every second time
// some cleanup

X509_free(cert);
BIO_free (bio_cert);
BIO_free (bio_key);
EVP_PKEY_free(key_pair);
//RSA_free(rsa); -- gives segmentation fault about every second time

return NULL;
X509_free(cert);
BIO_free(bio_cert);
BIO_free(bio_key);
EVP_PKEY_free(key_pair);

return NULL;
}

gboolean _create_crypto_worker_report(gpointer data) {

CryptoData *report_data = (CryptoData *) data;
gboolean _create_crypto_worker_report(gpointer data)
{

GClosure *closure;
CryptoData* report_data = (CryptoData*)data;

closure = g_cclosure_new(G_CALLBACK(report_data->callback), NULL, NULL);
g_closure_set_marshal(closure, g_cclosure_marshal_generic);
GClosure* closure;

closure = g_cclosure_new(G_CALLBACK(report_data->callback), NULL, NULL);
g_closure_set_marshal(closure, g_cclosure_marshal_generic);

GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };

g_value_init(&params[0], G_TYPE_STRING);
g_value_init(&params[1], G_TYPE_STRING);
g_value_init(&params[2], G_TYPE_STRING);
g_value_init(&params[0], G_TYPE_STRING);
g_value_init(&params[1], G_TYPE_STRING);
g_value_init(&params[2], G_TYPE_STRING);

if (report_data->errorDetected) {
GST_ERROR("Returning with error");
g_value_set_string(&params[0], "Failure");
g_value_set_string(&params[1], "Failure");
g_value_set_string(&params[2], "Failure");
if (report_data->errorDetected) {
GST_ERROR("Returning with error");
g_value_set_string(&params[0], "Failure");
g_value_set_string(&params[1], "Failure");
g_value_set_string(&params[2], "Failure");

g_closure_invoke(closure, NULL, 3, (const GValue *)&params, NULL);
}
else {
g_value_set_string(&params[0], report_data->pem_key);
g_value_set_string(&params[1], report_data->pem_cert);
g_value_set_string(&params[2], report_data->char_fprint);
g_closure_invoke(closure, NULL, 3, (const GValue*)&params, NULL);
}
else {
g_value_set_string(&params[0], report_data->pem_key);
g_value_set_string(&params[1], report_data->pem_cert);
g_value_set_string(&params[2], report_data->char_fprint);

g_closure_invoke(closure, NULL, 3, (const GValue *)&params, NULL);
}
g_closure_invoke(closure, NULL, 3, (const GValue*)&params, NULL);
}

// some cleanup
// some cleanup

g_value_unset(&params[0]);
g_value_unset(&params[1]);
g_value_unset(&params[2]);
g_closure_unref(closure);
g_value_unset(&params[0]);
g_value_unset(&params[1]);
g_value_unset(&params[2]);
g_closure_unref(closure);

g_free(report_data->pem_key);
g_free(report_data->pem_cert);
g_free(report_data->char_fprint);
g_free(report_data);
g_free(report_data->pem_key);
g_free(report_data->pem_cert);
g_free(report_data->char_fprint);
g_free(report_data);

return FALSE;
return FALSE;
}



0 comments on commit 1a17818

Please sign in to comment.