Skip to content
Snippets Groups Projects

Resolve "SMIME Support"

Merged lazarog98 requested to merge 232-smime-support into dev
Compare and Show latest version
4 files
+ 274
80
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -52,7 +52,59 @@ int print_test(int a) {
return 0;
}
char * OpenSSL_encrypt(const char *text, const char *pem) {
char_array_with_length *create_list_of_errors() {
unsigned long err = 0;
linked_list *head = NULL;
linked_list *cur = NULL;
int first = 1;
char_array_with_length *res = malloc(sizeof(char_array_with_length));
int length = 0;
char ** arr = NULL;
while ((err = ERR_get_error()) != 0) {
printf("%s", "Added ERROR\n");
const char *error = ERR_func_error_string(err);
//printf("\nError: %s", error);
char * error_permanent = (char *) malloc(strlen(error)+1);
error_permanent[strlen(error)]=0;//To Nullterminate the string
memcpy(error_permanent,error,strlen(error));
linked_list * newerr = malloc(sizeof(linked_list));
newerr->str = error_permanent;
newerr->next = NULL;
if (first)
{
first=0;
head = newerr;
}
else
{
cur->next=newerr;
}
cur=newerr;
length++;
}
if (length) {
arr = malloc(sizeof(char*)*length);
linked_list *cur = head;
// TODO: make sure this doesn't cause errors
int i = 0;
while (cur != NULL) {
arr[i] = cur->str;
linked_list *old = cur;
cur = cur->next;
free(old);
i++;
}
}
res->arr = arr;
res->size = length;
return res;
}
result * OpenSSL_encrypt(const char *text, const char *pem) {
// https://github.com/openssl/openssl/blob/master/demos/cms/cms_enc.c
OpenSSL_initialize();
char *encrypted = NULL, *tmp=NULL;;
@@ -62,7 +114,8 @@ char * OpenSSL_encrypt(const char *text, const char *pem) {
X509 *rec_cert = NULL;
STACK_OF(X509) *cert_stack = NULL;
CMS_ContentInfo *cms = NULL;
unsigned long err = 0;
char_array_with_length *temp = NULL;
result *res = malloc(sizeof(result));
// this trick allows to hardcode a certificate as a string
rec_cert_bio = BIO_new_mem_buf(pem, (int) strlen(pem));
@@ -112,9 +165,14 @@ char * OpenSSL_encrypt(const char *text, const char *pem) {
encrypted[size]=0;//To Nullterminate the string
memcpy(encrypted,tmp,size);
res->res = encrypted;
deinit:
// TODO: Collect all errors in a list and return them with the result
while ((err = ERR_get_error()) != 0) printf("\nError: %s", ERR_func_error_string(err));
temp = create_list_of_errors();
res->errors = temp->arr;
res->num_errors = temp->size;
free(temp);
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
@@ -122,10 +180,10 @@ deinit:
X509_free(rec_cert);
OpenSSL_deinitialize();
// OpenSSL ver 1.0.2.f has a bug (seemingly) that causes a crash when freeing cms content info pointers
return (void*) encrypted;
return res;
}
char* OpenSSL_decrypt(const char *str, const char *pem) {
result * OpenSSL_decrypt(const char *str, const char *pem) {
OpenSSL_initialize();
char *decrypted = NULL, *tmp=NULL;
// https://github.com/openssl/openssl/blob/master/demos/cms/cms_dec.c
@@ -136,7 +194,8 @@ char* OpenSSL_decrypt(const char *str, const char *pem) {
X509 *rec_cert = NULL;
CMS_ContentInfo *cms = NULL;
EVP_PKEY *rkey = NULL;
unsigned long err = 0;
result *res = malloc(sizeof(result));
char_array_with_length *temp = NULL;
// this trick allows to hardcode a certificate as a string
rec_cert_bio = BIO_new_mem_buf(pem, (int) strlen(pem));
@@ -148,12 +207,13 @@ char* OpenSSL_decrypt(const char *str, const char *pem) {
printf("Failed reading mykey.pem!\n");
goto deinit;
}
rec_cert = PEM_read_bio_X509(rec_cert_bio, NULL, 0, NULL);
if (!rec_cert ) {
printf("Failed reading pem cert\n");
goto deinit;
}
BIO_reset(rec_cert_bio);
rkey = PEM_read_bio_PrivateKey(rec_cert_bio, NULL, 0, NULL);
@@ -167,7 +227,6 @@ char* OpenSSL_decrypt(const char *str, const char *pem) {
if (!cms)
{
printf("DEC Failed at SMIME_READ");
while ((err = ERR_get_error()) != 0) printf("\nSMIME ERROR: %s", ERR_func_error_string(err));
goto deinit;
}
@@ -178,13 +237,18 @@ char* OpenSSL_decrypt(const char *str, const char *pem) {
printf("Failed at Decrypt");
goto deinit;
}
long size = BIO_get_mem_data(out, &tmp);
decrypted= (char *) malloc(size+1);
decrypted[size]=0;//To Nullterminate the string
memcpy(decrypted, tmp, size);
res->res = decrypted;
deinit:
temp = create_list_of_errors();
res->errors = temp->arr;
res->num_errors = temp->size;
free(temp);
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
@@ -192,10 +256,10 @@ deinit:
X509_free(rec_cert);
OpenSSL_deinitialize();
// OpenSSL ver 1.0.2.f has a bug (seemingly) that causes a crash when freeing cms content info pointers
return decrypted;
return res;
}
char * OpenSSL_sign(const char *text, const char *pem, const int detached)
result * OpenSSL_sign(const char *text, const char *pem, const int detached)
{
OpenSSL_initialize();
char *mail = NULL, *tmp=NULL;
@@ -206,7 +270,8 @@ char * OpenSSL_sign(const char *text, const char *pem, const int detached)
CMS_ContentInfo *cms = NULL;
EVP_PKEY *skey = NULL;
int flags = CMS_STREAM | CMS_PARTIAL;
unsigned long err = 0;
result *res = malloc(sizeof(result));
char_array_with_length *temp = NULL;
if (detached) flags |= CMS_DETACHED;
@@ -244,13 +309,11 @@ char * OpenSSL_sign(const char *text, const char *pem, const int detached)
cms = CMS_sign(NULL, NULL, NULL, in, flags);
if (!cms)
{
while ((err = ERR_get_error()) != 0) printf("\nFailed at signstart: %s", ERR_func_error_string(err));
goto deinit;
}
if (!CMS_add1_signer(cms, sig_cert, skey, EVP_sha256(), flags))
{
while ((err = ERR_get_error()) != 0) printf("\nFailed at signeradd: %s", ERR_func_error_string(err));
goto deinit;
}
@@ -270,9 +333,12 @@ char * OpenSSL_sign(const char *text, const char *pem, const int detached)
memcpy(mail,tmp,size);
res->res = mail;
deinit:
temp = create_list_of_errors();
res->errors = temp->arr;
res->num_errors = temp->size;
free(temp);
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
@@ -280,11 +346,11 @@ deinit:
X509_free(sig_cert);
OpenSSL_deinitialize();
return mail;
return res;
}
struct SMIME_verification * OpenSSL_verify(const char *text, const char *pem_cert) {
struct SMIME_verification *ver = malloc(sizeof(struct SMIME_verification));
SMIME_verification * OpenSSL_verify(const char *text, const char *pem_cert) {
SMIME_verification *ver = malloc(sizeof(SMIME_verification));
OpenSSL_initialize();
char *realtext = NULL, *tmp=NULL;
@@ -347,13 +413,13 @@ struct SMIME_verification * OpenSSL_verify(const char *text, const char *pem_cer
ver->text = malloc(20);
strncpy(ver->text,"test",4);
ver->cert = malloc(2*sizeof(char*));
/*ver->cert = malloc(2*sizeof(char*));
ver->cert[0] = malloc(20);
strncpy(ver->cert[0], "testcert", 8);
ver->cert[0][8] =0;
ver->cert[1] = malloc(20);
strncpy(ver->cert[1], "trectset", 8);
ver->cert[1][8] =0;
ver->cert[1][8] =0;*/
/*decrypted= (char *) malloc(size+1);
decrypted[size]=0;//To Nullterminate the string
memcpy(decrypted, tmp, size);*/
Loading