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
+ 293
72
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -112,14 +112,13 @@ char * OpenSSL_encrypt(const char *text, const char *pem) {
memcpy(encrypted,tmp,size);
deinit:
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
BIO_free(rec_cert_bio);
X509_free(rec_cert);
sk_X509_pop_free(cert_stack, X509_free);
OpenSSL_deinitialize();
// OpenSSL ver 1.0.2.f has a bug (seemingly) that causes a crash when freeing cms content info pointers
//if (cms != NULL) CMS_ContentInfo_free(cms);
return (void*) encrypted;
}
@@ -183,13 +182,13 @@ char* OpenSSL_decrypt(const char *str, const char *pem) {
decrypted[size]=0;//To Nullterminate the string
memcpy(decrypted, tmp, size);
deinit:
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
BIO_free(rec_cert_bio);
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
// CMS_ContentInfo_free(cms);
return decrypted;
}
@@ -272,13 +271,101 @@ char * OpenSSL_sign(const char *text, const char *pem, const int detached)
deinit:
deinit:
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
BIO_free(sig_cert_bio);
sk_X509_pop_free(cert_stack, X509_free);
X509_free(sig_cert);
OpenSSL_deinitialize();
return mail;
}
struct SMIME_verification * OpenSSL_verify(const char *text, const char *pem_cert) {
struct SMIME_verification *ver = malloc(sizeof(struct SMIME_verification));
OpenSSL_initialize();
char *realtext = NULL, *tmp=NULL;
// https://github.com/openssl/openssl/blob/master/demos/cms/cms_dec.c
// in = string to encrypt, out = encrypted string, rec_cert_bio =
BIO *in = NULL, *out = NULL, *sig_cert_bio = NULL;
// recipient certificate
X509 *sig_cert = NULL;
X509_STORE *cert_store = NULL;
CMS_ContentInfo *cms = NULL;
BIO *detached = NULL;
cert_store = X509_STORE_new();
// this trick allows to hardcode a certificate as a string
sig_cert_bio = BIO_new_mem_buf(pem_cert, (int) strlen(pem_cert));
// rec_cert_bio = BIO_new_file("keys/mykey.pem", "r");
in = BIO_new_mem_buf(text,(int) strlen(text)); // simpletest
if (!sig_cert_bio) {
printf("Failed reading mykey.pem!\n");
goto deinit;
}
sig_cert = PEM_read_bio_X509(sig_cert_bio, NULL, 0, NULL);
if (!sig_cert ) {
printf("Failed reading pem cert\n");
goto deinit;
}
if (!X509_STORE_add_cert(cert_store, sig_cert)) {
printf("Failed at adding cert to store\n");
goto deinit;
}
cms = SMIME_read_CMS(in, &detached);
if (!cms)
{
printf("Failed at SMIME_READ");
unsigned long err = ERR_get_error();
printf("\nSMIME ERROR: %s\n", ERR_func_error_string(err));
goto deinit;
}
out = BIO_new(BIO_s_mem());
if (!CMS_verify(cms, NULL, cert_store, detached, out, 0))
{
printf("Verification failed");
int i =0;
// MAKE A LINKED LIST AND PACK ALL THE ERROS THERE
// MAKE A LINKED LIST FOR THE SIGNERS IN THE STRUCT
while (int err =ERR_get_error(); err !0 )
ERR_func_error_string()
goto deinit;
}
long size = BIO_get_mem_data(out, &tmp);
ver->text = malloc(20);
strncpy(ver->text,"test",4);
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;
/*decrypted= (char *) malloc(size+1);
decrypted[size]=0;//To Nullterminate the string
memcpy(decrypted, tmp, size);*/
deinit:
CMS_ContentInfo_free(cms);
BIO_free(in);
BIO_free(out); //also frees tmp
BIO_free(sig_cert_bio);
X509_free(sig_cert);
OpenSSL_deinitialize();
// OpenSSL ver 1.0.2.f has a bug (seemingly) that causes a crash when freeing cms content info pointers
return ver;
}
Loading