summaryrefslogtreecommitdiff
path: root/email-core/email-core-smime.c
blob: 19aa539376e863d68cd1e66bd0b9d0ca123b54f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
/*
*  email-service
*
* Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/



/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***
 *File :  email-core-smime.c
 *Desc :  MIME Operation
 *
 *Auth :
 *
 *History :
 *   2011.04.14  :  created
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***/
#undef close

#include <openssl/pkcs7.h>
#include <openssl/pkcs12.h>
#include <openssl/buffer.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/ssl.h>

#include "email-utilities.h"
#include "email-core-global.h"
#include "email-core-utils.h"
#include "email-core-mail.h"
#include "email-core-smtp.h"
#include "email-storage.h"
#include "email-core-smime.h"
#include "email-core-pgp.h"
#include "email-core-cert.h"
#include "email-debug-log.h"

/* /opt/share/cert-svc/certs is a base path */

#define SMIME_SIGNED_FILE "smime.p7s"
#define SMIME_ENCRYPT_FILE "smime.p7m"
#define DECRYPT_TEMP_FILE "decrypt_temp_file.eml"

/* If not present then the default digest algorithm for signing key will be used SHA1 */
static const EVP_MD *emcore_get_digest_algorithm(email_digest_type digest_type)
{
	const EVP_MD *digest_algo = NULL;
	
	switch (digest_type) {
	case DIGEST_TYPE_MD5:
		digest_algo = EVP_md5();
		break;
	case DIGEST_TYPE_SHA1:
	default:
		digest_algo = EVP_sha1();
		break;
	}

	return digest_algo;
}

/* If not present then the default cipher algorithm for signing key will be used RC2(40) */
static const EVP_CIPHER *emcore_get_cipher_algorithm(email_cipher_type cipher_type)
{
	const EVP_CIPHER *cipher = NULL;

	switch (cipher_type) {
	case CIPHER_TYPE_RC2_128 :
		cipher = EVP_rc2_cbc();
		break;
	case CIPHER_TYPE_RC2_64 :
		cipher = EVP_rc2_64_cbc();
		break;
	case CIPHER_TYPE_DES3 :
		cipher = EVP_des_ede3_cbc();
		break;
	case CIPHER_TYPE_DES :
		cipher = EVP_des_cbc();
		break;
#ifdef __FEATURE_USE_MORE_CIPHER_TYPE__
	case CIPHER_TYPE_SEED :
		cipher = EVP_seed_cbc();
		break;
	case CIPHER_TYPE_AES128 :
		cipher = EVP_aes_128_cbc();
		break;
	case CIPHER_TYPE_AES192 :
		cipher = EVP_aes_192_cbc();
		break;
	case CIPHER_TYPE_AES256 :
		cipher = EVP_aes_256_cbc();
		break;
#ifndef OPENSSL_NO_CAMELLIA		
	case CIPHER_TYPE_CAMELLIA128 :
		cipher = EVP_camellia_128_cbc();
		break;
	case CIPHER_TYPE_CAMELLIA192 :
		cipher = EVP_camellia_192_cbc();
		break;
	case CIPHER_TYPE_CAMELLIA256 :
		cipher = EVP_camellia_256_cbc();
		break;
#endif
#endif
	case CIPHER_TYPE_RC2_40 :
	default :
		cipher = EVP_rc2_40_cbc();
		break;
	}

	return cipher;
}

static int get_x509_stack_of_recipient_certs(char *recipients, STACK_OF(X509) **output_recipient_certs, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("recipients : [%s], STACK_OF(X509) : [%p]", recipients, output_recipient_certs);

	int err = EMAIL_ERROR_NONE;
	int ret = false;	
	int i = 0, j = 0;
	int cert_size = 0;
	char *temp_recipients = NULL;
	char *email_address = NULL;
	char file_name[512] = {0, };
	const unsigned char *in_cert = NULL;

	ADDRESS *token_address = NULL;

	X509 *x509_cert = NULL;
	STACK_OF(X509) *temp_recipient_certs = NULL;

	CERT_CONTEXT *context = NULL;
	emstorage_certificate_tbl_t *cert = NULL;

	if (!recipients || !output_recipient_certs) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	/* Initialize the variable */
	temp_recipient_certs = sk_X509_new_null();

	temp_recipients = EM_SAFE_STRDUP(recipients);

	for (i = 0, j = EM_SAFE_STRLEN(temp_recipients); i < j; i++)
		if (temp_recipients[i] == ';') temp_recipients[i] = ',';

	rfc822_parse_adrlist(&token_address, temp_recipients, NULL);

	while (token_address) {
		context = cert_svc_cert_context_init();
		if (!context) { /*prevent 20162*/
			EM_DEBUG_EXCEPTION("cert_svc_cert_context_init failed");
			goto FINISH_OFF;			
		}

		EM_DEBUG_LOG_SEC("email_address_mailbox : [%s], email_address_host : [%s]", token_address->mailbox, token_address->host);

		email_address = g_strdup_printf("<%s@%s>", token_address->mailbox, token_address->host);
		if (!emstorage_get_certificate_by_email_address(email_address, &cert, false, 0, &err)) {
			EM_DEBUG_EXCEPTION("emstorage_get_certificate_by_email_address failed : [%d]", err);
			goto FINISH_OFF;
		}

		if (!cert) { /*prevent 20161*/
			EM_DEBUG_EXCEPTION("cert is NULL");
			goto FINISH_OFF;	
		}

		
		SNPRINTF(file_name, sizeof(file_name), "%s", cert->filepath);
		EM_DEBUG_LOG_SEC("file_name : [%s]", file_name);
		if (cert_svc_load_file_to_context(context, file_name) != CERT_SVC_ERR_NO_ERROR) {
			EM_DEBUG_EXCEPTION("cert_svc_load_file_to_context failed");
			err = EMAIL_ERROR_SYSTEM_FAILURE;
			goto FINISH_OFF;
		}

		in_cert = context->certBuf->data;
		cert_size = context->certBuf->size;

		if (d2i_X509(&x509_cert, &in_cert, cert_size) == NULL) {
			EM_DEBUG_EXCEPTION("d2i_X509 failed");
			err = EMAIL_ERROR_SYSTEM_FAILURE;
			goto FINISH_OFF;
		}

		if (!sk_X509_push(temp_recipient_certs, x509_cert)) {
			EM_DEBUG_EXCEPTION("sk_X509_push failed");
			err = EMAIL_ERROR_SYSTEM_FAILURE;
			goto FINISH_OFF;
		}

		cert_svc_cert_context_final(context);
		context = NULL;
		
		emstorage_free_certificate(&cert, 1, NULL);
		cert = NULL;

		x509_cert = NULL;

		token_address = token_address->next;
	}

	*output_recipient_certs = temp_recipient_certs;

	ret = true;

FINISH_OFF:

	if (!ret) {
		if (temp_recipient_certs)
			sk_X509_pop_free(temp_recipient_certs, X509_free);

		if (x509_cert)
			X509_free(x509_cert);
	}

	if (cert)
		emstorage_free_certificate(&cert, 1, NULL);

	if (context)
		cert_svc_cert_context_final(context);

	EM_SAFE_FREE(temp_recipients);
	EM_SAFE_FREE(email_address);
	if (token_address)
		mail_free_address(&token_address);

	if (err_code)
		*err_code = err;

	EM_DEBUG_FUNC_END();
	return ret;
}

/* Opaque signed and encrypted method */
/*
static PKCS7 *opaque_signed_and_encrypt(STACK_OF(X509) *recipients_cert, X509 *signer, EVP_PKEY *private_key, BIO *mime_entity, const EVP_CIPHER *cipher, const EVP_MD *md, int flags)
{
	EM_DEBUG_FUNC_BEGIN();
	int i = 0;
	PKCS7 *pkcs7 = NULL;
	BIO *p7bio = NULL;
	X509 *x509;

	if (!(pkcs7 = PKCS7_new())) {
		EM_DEBUG_EXCEPTION("PKCS7 malloc failed");
		return NULL;
	}
	
	if (!PKCS7_set_type(pkcs7, NID_pkcs7_signedAndEnveloped)) {
		EM_DEBUG_EXCEPTION("Set type failed");
		goto FINISH_OFF;
	}

	if (!PKCS7_add_signature(pkcs7, signer, private_key, md)) {
		EM_DEBUG_EXCEPTION("PKCS7_add_signature failed");
		goto FINISH_OFF;
	}

	if (!PKCS7_add_certificate(pkcs7, signer)) {
		EM_DEBUG_EXCEPTION("PKCS7_add_certificate failed");
		goto FINISH_OFF;
	}

	for (i = 0; i < sk_X509_num(recipients_cert); i++) {
		x509 = sk_X509_value(recipients_cert, i);
		if (!PKCS7_add_recipient(pkcs7, x509)) {
			EM_DEBUG_EXCEPTION("PKCS7_add_recipient failed");
			goto FINISH_OFF;
		}
	}

	if (!PKCS7_set_cipher(pkcs7, cipher)) {
		EM_DEBUG_EXCEPTION("Cipher failed");
		goto FINISH_OFF;
	}

	if (flags & PKCS7_STREAM)
		return pkcs7;

	if (PKCS7_final(pkcs7, mime_entity, flags))
		return pkcs7;
	
FINISH_OFF:
	BIO_free_all(p7bio);
	PKCS7_free(pkcs7);
	return NULL;
}
*/

INTERNAL_FUNC int emcore_smime_set_signed_message(char *certificate, char *mime_entity, email_digest_type digest_type, char **file_path, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN_SEC("certificate path : [%s], mime_entity : [%s]", certificate, mime_entity);
	int err, ret = false;
	char temp_smime_filepath[512];
	X509 *cert = NULL;
	STACK_OF(X509) *other_certs = NULL;
	EVP_PKEY *private_key = NULL;
	const EVP_MD *digest = NULL;
	BIO *bio_mime_entity = NULL, *bio_cert = NULL, *bio_prikey = NULL;
	BIO *smime_attachment = NULL;
	PKCS7 *signed_message = NULL;
	int flags = PKCS7_DETACHED | PKCS7_PARTIAL;

	SNPRINTF(temp_smime_filepath, sizeof(temp_smime_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, SMIME_SIGNED_FILE);
	EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_smime_filepath);

	smime_attachment = BIO_new_file(temp_smime_filepath, OUTMODE);
	if (!smime_attachment) {
		EM_DEBUG_EXCEPTION_SEC("Cannot open output file %s", temp_smime_filepath);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	/* Load certificate for getting the certificate and private key */
	if (!emcore_load_PFX_file(certificate, &private_key, &cert, &other_certs, &err)) {
		EM_DEBUG_EXCEPTION("Load the private certificate failed : [%d]", err);
		goto FINISH_OFF;
	}

	bio_mime_entity = BIO_new_file(mime_entity, READMODE);
	if (!bio_mime_entity) {
		EM_DEBUG_EXCEPTION("Cannot open file[%s]", mime_entity);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	signed_message = PKCS7_sign(NULL, NULL, other_certs, bio_mime_entity, flags);
	if (!signed_message) {
		EM_DEBUG_EXCEPTION("Error creating PKCS#7 structure");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	
	/* Get the digest algorithm */
	digest = emcore_get_digest_algorithm(digest_type);

	if (!PKCS7_sign_add_signer(signed_message, cert, private_key, digest, flags)) {
		EM_DEBUG_EXCEPTION("PKCS7_sign_add_signer failed");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	if (!PKCS7_final(signed_message, bio_mime_entity, flags)) {
		EM_DEBUG_EXCEPTION("PKCS7_final failed");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	if (!i2d_PKCS7_bio_stream(smime_attachment, signed_message, bio_mime_entity, flags)) {
		EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	BIO_flush(smime_attachment);
	ret = true;

FINISH_OFF:
	if (file_path && ret)
		*file_path = EM_SAFE_STRDUP(temp_smime_filepath);

	X509_free(cert);
	sk_X509_pop_free(other_certs, X509_free);
	EVP_PKEY_free(private_key);
	PKCS7_free(signed_message);

	BIO_free(bio_mime_entity);
	BIO_free(bio_cert);
	BIO_free(bio_prikey);
	BIO_free_all(smime_attachment);

	if (err_code != NULL)
		*err_code = err;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return ret;
}

INTERNAL_FUNC int emcore_smime_set_encrypt_message(char *recipient_list, char *mime_entity, email_cipher_type cipher_type, char **file_path, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN("certificate path : [%p], mime_entity : [%p]", recipient_list, mime_entity);
	char temp_smime_filepath[512];
	int err = EMAIL_ERROR_NONE, ret = false;
//	int flags = PKCS7_DETACHED | PKCS7_STREAM;
	int flags = PKCS7_BINARY;

	CERT_CONTEXT *loaded_cert = NULL;
	STACK_OF(X509) *recipient_certs = NULL;
	X509 *cert = NULL;
	BIO *bio_mime_entity = NULL, *bio_cert = NULL;
	BIO *smime_attachment = NULL;
	PKCS7 *encrypt_message = NULL;
	const EVP_CIPHER *cipher = NULL;

	loaded_cert = cert_svc_cert_context_init();

	SNPRINTF(temp_smime_filepath, sizeof(temp_smime_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, SMIME_ENCRYPT_FILE);
	EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_smime_filepath);

	smime_attachment = BIO_new_file(temp_smime_filepath, OUTMODE);
	if (!smime_attachment) {
		EM_DEBUG_EXCEPTION_SEC("Cannot open output file %s", temp_smime_filepath);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	if (!get_x509_stack_of_recipient_certs(recipient_list, &recipient_certs, &err)) {
		EM_DEBUG_EXCEPTION("get_x509_stack_of_recipient_certs failed [%d]", err);
		goto FINISH_OFF;
	}

	bio_mime_entity = BIO_new_file(mime_entity, READMODE);
	if (!bio_mime_entity) {
		EM_DEBUG_EXCEPTION("Cannot open file[%s]", mime_entity);
		goto FINISH_OFF;
	}
	
	/* Get cipher algorithm */
	cipher = emcore_get_cipher_algorithm(cipher_type);
	
	encrypt_message = PKCS7_encrypt(recipient_certs, bio_mime_entity, cipher, flags);
	if (encrypt_message == NULL) {
		EM_DEBUG_EXCEPTION("PKCS7_encrypt failed [%ld]", ERR_get_error());
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	if (!i2d_PKCS7_bio_stream(smime_attachment, encrypt_message, bio_mime_entity, flags)) {
		EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	BIO_flush(smime_attachment);

	ret = true;

FINISH_OFF:
	if (file_path && ret)
		*file_path = EM_SAFE_STRDUP(temp_smime_filepath);

	PKCS7_free(encrypt_message);

	X509_free(cert);
	sk_X509_pop_free(recipient_certs, X509_free);

	BIO_free(bio_cert);
	BIO_free(bio_mime_entity);
	BIO_free_all(smime_attachment);

	cert_svc_cert_context_final(loaded_cert);
	if (err_code != NULL)
		*err_code = err;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return ret;
}

INTERNAL_FUNC int emcore_smime_set_signed_and_encrypt_message(char *recipient_list, char *certificate, char *mime_entity, email_cipher_type cipher_type, email_digest_type digest_type, char **file_path, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN_SEC("certificate path : [%s], mime_entity : [%s]", recipient_list, mime_entity);
	char temp_smime_filepath[512];
	char temp_mime_entity_path[512];
	int err = EMAIL_ERROR_NONE, ret = false;
	int flags = PKCS7_DETACHED | PKCS7_PARTIAL | PKCS7_STREAM;

	STACK_OF(X509) *recipient_certs = NULL;
	STACK_OF(X509) *other_certs = NULL;
	BIO *bio_mime_entity = NULL, *bio_cert = NULL;
	BIO *bio_signed_message = NULL;
	BIO *smime_attachment = NULL;
	PKCS7 *signed_message = NULL;
	PKCS7 *encrypt_message = NULL;
	const EVP_CIPHER *cipher = NULL;
	const EVP_MD *digest = NULL;

	/* Variable for private certificate */
	EVP_PKEY *private_key = NULL;
	X509 *cert = NULL;

	SNPRINTF(temp_smime_filepath, sizeof(temp_smime_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, SMIME_ENCRYPT_FILE);
	EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_smime_filepath);

	smime_attachment = BIO_new_file(temp_smime_filepath, OUTMODE);
	if (!smime_attachment) {
		EM_DEBUG_EXCEPTION_SEC("Cannot open output file %s", temp_smime_filepath);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	/* Signing the mail */
	/* 1. Load the private certificate */
	if (!emcore_load_PFX_file(certificate, &private_key, &cert, &other_certs, &err)) {
		EM_DEBUG_EXCEPTION("Load the private certificate failed : [%d]", err);
		goto FINISH_OFF;
	}

	/* 2. Read mime entity */
	bio_mime_entity = BIO_new_file(mime_entity, READMODE);
	if (!bio_mime_entity) {
		EM_DEBUG_EXCEPTION("Cannot open file[%s]", mime_entity);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	/* 3. signing */
	signed_message = PKCS7_sign(NULL, NULL, other_certs, bio_mime_entity, flags);
	if (!signed_message) {
		EM_DEBUG_EXCEPTION("Error creating PKCS#7 structure");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	
	/* 4. Get the digest algorithm */
	digest = emcore_get_digest_algorithm(digest_type);

	/* 5. Apply a digest algorithm */
	if (!PKCS7_sign_add_signer(signed_message, cert, private_key, digest, flags)) {
		EM_DEBUG_EXCEPTION("PKCS7_sign_add_signer failed");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	/* 6. Create signing message */
	SNPRINTF(temp_mime_entity_path, sizeof(temp_mime_entity_path), "%s%smime_entity", MAILTEMP, DIR_SEPERATOR);
	EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_mime_entity_path);

	bio_signed_message = BIO_new_file(temp_mime_entity_path, WRITEMODE);
	if (!bio_signed_message) {
		EM_DEBUG_EXCEPTION_SEC("Cannot open output file %s", temp_mime_entity_path);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	if (!SMIME_write_PKCS7(bio_signed_message, signed_message, bio_mime_entity, flags | SMIME_OLDMIME | SMIME_CRLFEOL)) {
		EM_DEBUG_EXCEPTION("SMIME_write_PKCS7 error");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	BIO_free(bio_signed_message);

	/* Encrypting the mail */
	/* 1. Get the recipient certs */
	if (!get_x509_stack_of_recipient_certs(recipient_list, &recipient_certs, &err)) {
		EM_DEBUG_EXCEPTION("get_x509_stack_of_recipient_certs failed [%d]", err);
		goto FINISH_OFF;
	}

	/* 2. Get cipher algorithm */
	cipher = emcore_get_cipher_algorithm(cipher_type);
	
	flags = PKCS7_BINARY;

	/* 3. Encrypt the signing message */	
	bio_signed_message = BIO_new_file(temp_mime_entity_path, READMODE);
	if (!bio_signed_message) {
		EM_DEBUG_EXCEPTION_SEC("Cannot open output file %s", temp_mime_entity_path);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	encrypt_message = PKCS7_encrypt(recipient_certs, bio_signed_message, cipher, flags);
	if (encrypt_message == NULL) {
		EM_DEBUG_EXCEPTION("PKCS7_encrypt failed [%ld]", ERR_get_error());
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	
	/* 4. Write the encrypt message in file */
	if (!i2d_PKCS7_bio_stream(smime_attachment, encrypt_message, bio_mime_entity, flags)) {
		EM_DEBUG_EXCEPTION("i2d_PKCS7_bio_stream failed");
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	BIO_flush(smime_attachment);

	ret = true;

FINISH_OFF:
	if (file_path && ret)
		*file_path = EM_SAFE_STRDUP(temp_smime_filepath);

	emstorage_delete_file(temp_mime_entity_path, NULL);

	PKCS7_free(signed_message);
	PKCS7_free(encrypt_message);
	EVP_PKEY_free(private_key);

	X509_free(cert);
	sk_X509_pop_free(other_certs, X509_free);
	sk_X509_pop_free(recipient_certs, X509_free);

	BIO_free(bio_cert);
	BIO_free(bio_mime_entity);
	BIO_free(bio_signed_message);
	BIO_free_all(smime_attachment);

	if (err_code != NULL)
		*err_code = err;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return ret;
}

INTERNAL_FUNC int emcore_smime_get_decrypt_message(char *encrypt_message, char *certificate, char **decrypt_message, int *err_code)
{
	EM_DEBUG_FUNC_BEGIN_SEC("encrypt_file : [%s], certificate : [%s]", encrypt_message, certificate);
	int ret = false;
	int err = EMAIL_ERROR_NONE;
	char temp_decrypt_filepath[512] = {0, };

	X509 *cert = NULL;
	EVP_PKEY *private_key = NULL;
	BIO *infile = NULL, *out_buf = NULL;
	PKCS7 *p7_encrypt_message = NULL;
	STACK_OF(X509) *recipient_certs = NULL;

	/* Load the encrypted message */
	infile = BIO_new_file(encrypt_message, INMODE);
	if (infile == NULL) {
		EM_DEBUG_EXCEPTION("Cannot open output file %s", encrypt_message);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	p7_encrypt_message = d2i_PKCS7_bio(infile, NULL);
	if (!p7_encrypt_message) {
		EM_DEBUG_EXCEPTION("Error reading S/MIME message");
		err = EMAIL_ERROR_INVALID_PARAM;
		goto FINISH_OFF;
	}

	/* Initialize the output file for decrypted message */
	SNPRINTF(temp_decrypt_filepath, sizeof(temp_decrypt_filepath), "%s%s%s", MAILTEMP, DIR_SEPERATOR, DECRYPT_TEMP_FILE);
	EM_DEBUG_LOG_SEC("attachment file path of smime : [%s]", temp_decrypt_filepath);

	out_buf = BIO_new_file(temp_decrypt_filepath, OUTMODE);
	if (!out_buf) {
		EM_DEBUG_EXCEPTION_SEC("Cannot open output file %s", temp_decrypt_filepath);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	/* Search private cert */
	if (!emcore_load_PFX_file(certificate, &private_key, &cert, NULL, &err)) {
		EM_DEBUG_EXCEPTION("Load the private certificate failed : [%d]", err);
		goto FINISH_OFF;
	}

	if (!PKCS7_decrypt(p7_encrypt_message, private_key, cert, out_buf, 0)) {
		EM_DEBUG_EXCEPTION("Decrpyt failed");
		err = EMAIL_ERROR_DECRYPT_FAILED;
		goto FINISH_OFF;
	}

	ret = true;

FINISH_OFF:

	if (decrypt_message && ret)
		*decrypt_message = EM_SAFE_STRDUP(temp_decrypt_filepath);

	X509_free(cert);
	EVP_PKEY_free(private_key);
	BIO_free(out_buf);
	BIO_free_all(infile);
	sk_X509_pop_free(recipient_certs, X509_free);

	if (err_code != NULL)
		*err_code = err;

	EM_DEBUG_FUNC_END("err [%d]", err);
	return ret;
}

/*
INTERNAL_FUNC int emcore_smime_verify_signed_message(char *signed_message, char *ca_file, char *ca_path, int *verify)
{
	int ret = false;
	int temp_verify = 0;
	BIO *indata = NULL;
	BIO *content = NULL;
	X509_STORE *store = NULL;
	X509_LOOKUP *lookup = NULL;
	PKCS7 *p7 = NULL;

	if (BIO_write(indata, signed_message, sizeof(signed_message)) <= 0) {
		EM_DEBUG_EXCEPTION("Char to Bio failed");
		goto FINISH_OFF;
	}

	p7 = SMIME_read_PKCS7(indata, &content);
	if (!p7) {
		EM_DEBUG_EXCEPTION("SMIME_read_PKCS7 failed");
		goto FINISH_OFF;
	}

	if (!(store = X509_STORE_new())) {
		EM_DEBUG_EXCEPTION("Initialize x509_store failed");
		goto FINISH_OFF;
	}
	
	lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
	if (lookup == NULL) {
		EM_DEBUG_EXCEPTION("Initialize lookup store failed");
		goto FINISH_OFF;
	}

	if (ca_file) {
		if (!X509_LOOKUP_load_file(lookup, ca_file, X509_FILETYPE_PEM)) {
			EM_DEBUG_EXCEPTION("X509_LOOKUP_load_file failed");
			goto FINISH_OFF;
		}
	} else {
		X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
	}

	lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
	if (lookup == NULL) {
		EM_DEBUG_EXCEPTION("X509_STORE_add_lookup failed");
		goto FINISH_OFF;
	}
	
	if (ca_path) {
		if (!X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM)) {
			EM_DEBUG_EXCEPTION("CA path load failed");
			goto FINISH_OFF;
		}
	} else {
		X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
	}
	
	temp_verify = PKCS7_verify(p7, NULL, store, content, NULL, 0);
	if (temp_verify) 
		EM_DEBUG_LOG("Verification Successful\n");

	ret = true;

FINISH_OFF:
	if (store)
		X509_STORE_free(store);
	if (p7)
		PKCS7_free(p7);
	
	if (indata)
		BIO_free(indata);

	if (verify != NULL)
		*verify = temp_verify;

	ERR_clear_error();
	return ret;
}
*/

INTERNAL_FUNC int emcore_convert_mail_data_to_smime_data(emstorage_account_tbl_t *account_tbl_item, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data_list, int *output_attachment_count)
{
	EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list [%p], input_attachment_count [%d], output_mail_data [%p], output_attachment_data_list [%p]", input_mail_data, input_attachment_data_list, input_attachment_count, output_mail_data, output_attachment_data_list);

	int i = 0;
	int err = EMAIL_ERROR_NONE;
	int smime_type = EMAIL_SMIME_NONE;
	int address_length = 0;
	int attachment_count = input_attachment_count;
	int file_size = 0;
	char *name = NULL;
	char *smime_file_path = NULL;
	char *other_certificate_list = NULL;
	email_attachment_data_t new_attachment_data = {0};
	email_attachment_data_t *new_attachment_list = NULL;

	/* Validating parameters */
	if (!input_mail_data || !(input_mail_data->account_id) || !(input_mail_data->mailbox_id) 
		|| !output_attachment_count || !output_mail_data || !output_attachment_data_list ) { /*prevent#53051*/
		EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}

	smime_type = input_mail_data->smime_type;
	if (!smime_type) 
		smime_type = account_tbl_item->smime_type;

	/* Signed and Encrypt the message */
	switch (smime_type) {
	case EMAIL_SMIME_SIGNED:			/* Clear signed message */
		if (!emcore_smime_set_signed_message(account_tbl_item->certificate_path, input_mail_data->file_path_mime_entity, account_tbl_item->digest_type, &smime_file_path, &err)) {
			EM_DEBUG_EXCEPTION("em_core_smime_set_clear_signed_message is failed : [%d]", err);
			goto FINISH_OFF;
		}
	
		EM_DEBUG_LOG_SEC("smime_file_path : %s", smime_file_path);
		name = strrchr(smime_file_path, '/');

		new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
		new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
		new_attachment_data.attachment_mime_type = strdup("pkcs7-signature");

		attachment_count += 1;

		break;
	case EMAIL_SMIME_ENCRYPTED:			/* Encryption message */
		address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);

		other_certificate_list = em_malloc(address_length + 4);
		
		SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);

		if (!emcore_smime_set_encrypt_message(other_certificate_list, input_mail_data->file_path_mime_entity, account_tbl_item->cipher_type, &smime_file_path, &err)) {
			EM_DEBUG_EXCEPTION("emcore_smime_set_encrypt_message is failed : [%d]", err);
			goto FINISH_OFF;
		}

		EM_DEBUG_LOG_SEC("smime_file_path : %s", smime_file_path);
		name = strrchr(smime_file_path, '/');

		new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
		new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
		new_attachment_data.attachment_mime_type = strdup("pkcs7-mime");

		attachment_count = 1;

		break;
	case EMAIL_SMIME_SIGNED_AND_ENCRYPTED:			/* Signed and Encryption message */
		address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);

		other_certificate_list = em_malloc(address_length + 4);
		
		SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);

		if (!emcore_smime_set_signed_and_encrypt_message(other_certificate_list, account_tbl_item->certificate_path, input_mail_data->file_path_mime_entity, account_tbl_item->cipher_type, account_tbl_item->digest_type, &smime_file_path, &err)) {
			EM_DEBUG_EXCEPTION("em_core_smime_set_signed_and_encrypt_message is failed : [%d]", err);
			goto FINISH_OFF;
		}

		EM_DEBUG_LOG_SEC("smime_file_path : %s", smime_file_path);
		name = strrchr(smime_file_path, '/');

		new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
		new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
		new_attachment_data.attachment_mime_type = strdup("pkcs7-mime");

		attachment_count = 1;

		break;
	case EMAIL_PGP_SIGNED:
		if ((err = emcore_pgp_set_signed_message(NULL, input_mail_data->pgp_password, input_mail_data->file_path_mime_entity, account_tbl_item->user_email_address, account_tbl_item->digest_type, &smime_file_path)) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("em_core_smime_set_clear_signed_message is failed : [%d]", err);
			goto FINISH_OFF;
		}
	
		EM_DEBUG_LOG_SEC("smime_file_path : %s", smime_file_path);
		name = strrchr(smime_file_path, '/');

		new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
		new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
		new_attachment_data.attachment_mime_type = strdup("pgp-signature");

		attachment_count += 1;

		break;
	case EMAIL_PGP_ENCRYPTED:
		address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);

		other_certificate_list = em_malloc(address_length + 4);
		
		SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);

		if ((err = emcore_pgp_set_encrypted_message(other_certificate_list, NULL, input_mail_data->pgp_password, input_mail_data->file_path_mime_entity, account_tbl_item->user_email_address, input_mail_data->digest_type, &smime_file_path)) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emcore_pgp_set_encrypted_message failed : [%d]", err);
			goto FINISH_OFF;
		}

		name = strrchr(smime_file_path, '/');

		new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
		new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
		new_attachment_data.attachment_mime_type = strdup("octet-stream");

		attachment_count = 1;

		break;
	case EMAIL_PGP_SIGNED_AND_ENCRYPTED:
		address_length = EM_SAFE_STRLEN(input_mail_data->full_address_from) + EM_SAFE_STRLEN(input_mail_data->full_address_to) + EM_SAFE_STRLEN(input_mail_data->full_address_cc) + EM_SAFE_STRLEN(input_mail_data->full_address_bcc);

		other_certificate_list = em_malloc(address_length + 4);
		if (other_certificate_list == NULL) {
			EM_DEBUG_EXCEPTION("em_malloc failed");
			err = EMAIL_ERROR_OUT_OF_MEMORY;
			goto FINISH_OFF;
		}
		
		SNPRINTF(other_certificate_list, address_length + 2, "%s;%s;%s;%s", input_mail_data->full_address_from, input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc);

		if ((err = emcore_pgp_set_signed_and_encrypted_message(other_certificate_list, NULL, input_mail_data->pgp_password, input_mail_data->file_path_mime_entity, account_tbl_item->user_email_address, input_mail_data->digest_type, &smime_file_path)) != EMAIL_ERROR_NONE) {
			EM_DEBUG_EXCEPTION("emcore_pgp_set_encrypted_message failed : [%d]", err);
			goto FINISH_OFF;
		}

		name = strrchr(smime_file_path, '/');

		new_attachment_data.attachment_name = EM_SAFE_STRDUP(name + 1);
		new_attachment_data.attachment_path = EM_SAFE_STRDUP(smime_file_path);
		new_attachment_data.attachment_mime_type = strdup("octet-stream");

		attachment_count = 1;

		break;
	default:
		EM_DEBUG_LOG("MIME none");
		break;
	}

	if (!emcore_get_file_size(smime_file_path, &file_size, NULL)) {
		EM_DEBUG_EXCEPTION("emcore_get_file_size failed");
		goto FINISH_OFF;
	}

	new_attachment_data.attachment_size = file_size;
	new_attachment_data.save_status = 1;

	new_attachment_list = (email_attachment_data_t *)em_malloc(sizeof(email_attachment_data_t) * attachment_count);
	if (new_attachment_list == NULL) {
		EM_DEBUG_EXCEPTION("em_malloc failed");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}

	if (smime_type ==  EMAIL_SMIME_SIGNED) {
		for (i = 0; i < input_attachment_count; i++) {
			new_attachment_list[i].attachment_id         = input_attachment_data_list[i].attachment_id;
			new_attachment_list[i].attachment_name       = EM_SAFE_STRDUP(input_attachment_data_list[i].attachment_name);
			new_attachment_list[i].attachment_path       = EM_SAFE_STRDUP(input_attachment_data_list[i].attachment_path);
			new_attachment_list[i].content_id            = EM_SAFE_STRDUP(input_attachment_data_list[i].content_id);
			new_attachment_list[i].attachment_size       = input_attachment_data_list[i].attachment_size;
			new_attachment_list[i].mail_id               = input_attachment_data_list[i].mail_id;
			new_attachment_list[i].account_id            = input_attachment_data_list[i].account_id;
			new_attachment_list[i].mailbox_id            = input_attachment_data_list[i].mailbox_id;
			new_attachment_list[i].save_status           = input_attachment_data_list[i].save_status;
			new_attachment_list[i].drm_status            = input_attachment_data_list[i].drm_status;
			new_attachment_list[i].inline_content_status = input_attachment_data_list[i].inline_content_status;
			new_attachment_list[i].attachment_mime_type  = EM_SAFE_STRDUP(input_attachment_data_list[i].attachment_mime_type);
		}
	}

	new_attachment_list[attachment_count - 1].attachment_id         = new_attachment_data.attachment_id;
	new_attachment_list[attachment_count - 1].attachment_name       = EM_SAFE_STRDUP(new_attachment_data.attachment_name);
	new_attachment_list[attachment_count - 1].attachment_path       = EM_SAFE_STRDUP(new_attachment_data.attachment_path);
	new_attachment_list[attachment_count - 1].content_id            = EM_SAFE_STRDUP(new_attachment_data.content_id);
	new_attachment_list[attachment_count - 1].attachment_size       = new_attachment_data.attachment_size;
	new_attachment_list[attachment_count - 1].mail_id               = new_attachment_data.mail_id;
	new_attachment_list[attachment_count - 1].account_id            = new_attachment_data.account_id;
	new_attachment_list[attachment_count - 1].mailbox_id            = new_attachment_data.mailbox_id;
	new_attachment_list[attachment_count - 1].save_status           = new_attachment_data.save_status;
	new_attachment_list[attachment_count - 1].drm_status            = new_attachment_data.drm_status;
	new_attachment_list[attachment_count - 1].inline_content_status = new_attachment_data.inline_content_status;
	new_attachment_list[attachment_count - 1].attachment_mime_type  = EM_SAFE_STRDUP(new_attachment_data.attachment_mime_type);

	input_mail_data->smime_type = smime_type;
	input_mail_data->digest_type = account_tbl_item->digest_type;

FINISH_OFF:	

	EM_SAFE_FREE(other_certificate_list);
	EM_SAFE_FREE(smime_file_path);

	*output_attachment_count = attachment_count;
	
	*output_attachment_data_list = new_attachment_list;

	*output_mail_data = input_mail_data;

    EM_SAFE_FREE(new_attachment_data.attachment_name);
    EM_SAFE_FREE(new_attachment_data.attachment_path);
    EM_SAFE_FREE(new_attachment_data.attachment_mime_type);
    EM_SAFE_FREE(new_attachment_data.content_id);

    EM_DEBUG_LOG("err : [%d]", err);
	return err;				
}

INTERNAL_FUNC void emcore_init_openssl_library()
{
        EM_DEBUG_FUNC_BEGIN();
        SSL_library_init();
        ERR_load_crypto_strings();
        EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void emcore_clean_openssl_library()
{
        EM_DEBUG_FUNC_BEGIN();
        ERR_free_strings();
        EVP_cleanup();
        EM_DEBUG_FUNC_END();
}