07ee60bdcfb1f07f95f1d5b7b1b989aa8746e113
[platform/core/messaging/email-service.git] / email-core / email-core-cert.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /******************************************************************************
24  * File :  email-core-cert.h
25  * Desc :  Certificate API
26  *
27  * Auth :
28  *
29  * History :
30  *    2006.08.16  :  created
31  *****************************************************************************/
32 #include <openssl/pkcs7.h>
33 #include <openssl/pkcs12.h>
34 #include <openssl/pem.h>
35 #include <openssl/err.h>
36 #include <cert-service.h>
37 #include <glib.h>
38 #include <cert-svc/ccert.h>
39 #include <cert-svc/cstring.h>
40 #include <cert-svc/cpkcs12.h>
41 #include <cert-svc/cinstance.h>
42 #include <cert-svc/cprimitives.h>
43
44 #include "email-core-cert.h"
45 #include "email-core-mail.h"
46 #include "email-core-utils.h"
47 #include "email-utilities.h"
48 #include "email-storage.h"
49 #include "email-debug-log.h"
50
51 #define READ_MODE "r"
52 #define WRITE_MODE "w"
53
54 #define TRUSTED_USER "trusteduser/email/"
55
56 typedef enum {
57         CERT_TYPE_ETC          = 0,
58         CERT_TYPE_PKCS12,
59         CERT_TYPE_PKCS7,
60         CERT_TYPE_P7S
61 } cert_type;
62
63 static int emcore_get_certificate_type(char *extension, int *err_code)
64 {
65         EM_DEBUG_FUNC_BEGIN("extensiong is [%s]", extension);
66         int index = 0;
67         int type = 0;
68         int err = EMAIL_ERROR_NONE;
69         char *supported_file_type[] = {"pfx", "p12", "p7s", "pem", "der", "crt", "cer", NULL};
70
71         if (!extension) {
72                 EM_DEBUG_EXCEPTION("Invalid parameter");
73                 err = EMAIL_ERROR_INVALID_PARAM;
74                 goto FINISH_OFF;
75         }
76
77         while(supported_file_type[index]) {
78                 EM_DEBUG_LOG_SEC("certificate extension[%d]:[%s]", index, supported_file_type[index]);
79                 if (strcasecmp(extension, supported_file_type[index]) == 0) {
80                         switch (index) {
81                         case 0:
82                         case 1:
83                                 type = CERT_TYPE_PKCS12;
84                                 err = EMAIL_ERROR_INVALID_CERTIFICATE;
85                                 break;
86                         case 2:
87                                 type = CERT_TYPE_P7S;
88                                 break;
89                         case 3:
90                         case 4:
91                         case 5:
92                         case 6:
93                                 type = CERT_TYPE_PKCS7;
94                                 break;
95                         default:
96                                 type = CERT_TYPE_ETC;
97                                 err = EMAIL_ERROR_INVALID_CERTIFICATE;
98                                 break;
99                         }
100                 }
101                 index++;
102         }
103
104 FINISH_OFF:
105
106         if (err_code) {
107                 *err_code = err;
108         }
109
110         EM_DEBUG_FUNC_END("File type is [%d]", type);
111         return type;
112 }
113 /*
114 static GList *emcore_make_glist_from_string(char *email_address_list)
115 {
116         EM_DEBUG_FUNC_BEGIN_SEC("email_address list : [%s]", email_address_list);
117         int index = 0;
118         const gchar seperator = 0x01;
119         GList *email_list = NULL;
120         gchar *p_email_address_list = NULL;
121         gchar **token_list = NULL;
122
123         p_email_address_list = g_strdup(email_address_list);
124
125         token_list = g_strsplit(p_email_address_list, &seperator, -1);
126         while (token_list[index] != NULL) {
127                 email_list = g_list_append(email_list, token_list[index]);
128                 index++;
129         }
130
131         if (p_email_address_list)
132                 g_free(p_email_address_list);
133
134         return email_list;
135 }
136
137 static char *emcore_store_public_certificate(STACK_OF(X509) *certificates, char *email_address, int *err_code)
138 {
139         EM_DEBUG_FUNC_BEGIN();
140         int index = 0;
141         int err = EMAIL_ERROR_NONE;
142         char *file_path = NULL;
143         BIO *outfile = NULL;
144
145         file_path = (char *)em_malloc(256);
146         if (file_path == NULL) {
147                 EM_DEBUG_EXCEPTION("em_malloc failed");
148                 err = EMAIL_ERROR_OUT_OF_MEMORY;
149                 goto FINISH_OFF;
150         }
151
152         SNPRINTF(file_path, 256, "%s%s%s", CERT_SVC_STORE_PATH, TRUSTED_USER, email_address);
153         outfile = BIO_new_file(file_path, WRITE_MODE);
154         if (outfile == NULL) {
155                 EM_DEBUG_EXCEPTION("File open failed[write mode]");
156                 err = EMAIL_ERROR_SYSTEM_FAILURE;
157                 goto FINISH_OFF;
158         }
159
160         for (index = 0; index < sk_X509_num(certificates); index++) {
161                 EM_DEBUG_LOG("Write the certificate in pem file : [%d]", index);
162                 PEM_write_bio_X509(outfile, sk_X509_value(certificates, index));
163         }
164
165 FINISH_OFF:
166
167         if (outfile)
168                 BIO_free(outfile);
169
170         EM_DEBUG_FUNC_END();
171
172         return file_path;
173 }
174 */
175 #if 0
176 INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, char *password, EVP_PKEY **pri_key, X509 **cert, STACK_OF(X509) **ca, int *err_code)
177 {
178         EM_DEBUG_FUNC_BEGIN_SEC("Certificate path : [%s], password : [%s]", certificate, password);
179
180         int err = EMAIL_ERROR_NONE;
181         int ret = false;
182         FILE *fp = NULL;
183         PKCS12 *p12 = NULL;
184
185         err = em_fopen(certificate, "rb", &fp);
186         if (err != EMAIL_ERROR_NONE) {
187                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed : [%s] [%d]", certificate, err);
188                 goto FINISH_OFF;
189         }
190
191         p12 = d2i_PKCS12_fp(fp, NULL);
192         if (!p12) {
193                 EM_DEBUG_EXCEPTION("d2i_PKCS12_fp failed");
194                 err = EMAIL_ERROR_SYSTEM_FAILURE;
195                 goto FINISH_OFF;
196         }
197
198         if (!PKCS12_parse(p12, password, pri_key, cert, ca)) {
199                 EM_DEBUG_EXCEPTION("PKCS12_parse failed");
200                 err = EMAIL_ERROR_SYSTEM_FAILURE;
201                 goto FINISH_OFF;
202         }
203
204         ret = true;
205
206 FINISH_OFF:
207
208         if (fp)
209                 fclose(fp);
210
211         if (p12)
212                 PKCS12_free(p12);
213
214         if (err_code)
215                 *err_code = err;
216
217         return ret;
218 }
219 #endif
220
221 INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, EVP_PKEY **pri_key, X509 **cert, STACK_OF(X509) **ca, int *err_code)
222 {
223         EM_DEBUG_FUNC_BEGIN_SEC("certificate : [%s]", certificate);
224         int err = EMAIL_ERROR_NONE;
225         int ret = false;
226         size_t key_size = 0;
227         char *private_key = NULL;
228
229         /* Variable for certificate */
230         X509 *t_cert = NULL;
231         BIO *bio_mem = NULL;
232 //      STACK_OF(X509) *t_ca = NULL;
233
234         /* Variable for private key */
235         EVP_PKEY *t_pri_key = NULL;
236
237         CertSvcString csstring;
238         CertSvcInstance cert_instance;
239         CertSvcCertificate csc_cert;
240         CertSvcCertificateList certificate_list;
241
242         if (certificate == NULL) {
243                 EM_DEBUG_EXCEPTION("Invalid parameter");
244                 err = EMAIL_ERROR_INVALID_PARAM;
245                 goto FINISH_OFF;
246         }
247
248         /* Create instance */
249         err = certsvc_instance_new(&cert_instance);
250         if (err != CERTSVC_SUCCESS) {
251                 EM_DEBUG_EXCEPTION("certsvc_instance_new failed : [%d]", err);
252                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
253                 goto FINISH_OFF;
254         }
255
256         /* Make the pfxID string */
257         err = certsvc_string_new(cert_instance, certificate, EM_SAFE_STRLEN(certificate), &csstring);
258         if (err != CERTSVC_SUCCESS) {
259                 EM_DEBUG_EXCEPTION("certsvc_string_new failed : [%d]", err);
260                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
261                 goto FINISH_OFF;
262         }
263
264         /* Load the certificate list of pkcs12 type */
265         err = certsvc_pkcs12_load_certificate_list(cert_instance, csstring, &certificate_list);
266         if (err != CERTSVC_SUCCESS) {
267                 EM_DEBUG_EXCEPTION("certsvc_pkcs12_load_certificate_list failed : [%d]", err);
268                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
269                 goto FINISH_OFF;
270         }
271
272         /* Get a certificate */
273         err = certsvc_certificate_list_get_one(certificate_list, 0, &csc_cert);
274         if (err != CERTSVC_SUCCESS) {
275                 EM_DEBUG_EXCEPTION("certsvc_certificate_list_get_one failed : [%d]", err);
276                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
277                 goto FINISH_OFF;
278         }
279
280         /* Convert from char to X509 */
281         err = certsvc_certificate_dup_x509(csc_cert, &t_cert);
282         if (t_cert == NULL || err != CERTSVC_SUCCESS) {
283                 EM_DEBUG_EXCEPTION("certsvc_certificate_dup_x509 failed");
284                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
285                 goto FINISH_OFF;
286         }
287
288         /* Get the private key */
289         err = certsvc_pkcs12_private_key_dup(cert_instance, csstring, &private_key, &key_size);
290         if (err != CERTSVC_SUCCESS) {
291                 EM_DEBUG_EXCEPTION("certsvc_pkcs12_private_key_dup failed : [%d]", err);
292                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
293                 goto FINISH_OFF;
294         }
295
296         EM_DEBUG_LOG_DEV("key_size : [%d], private_key : [%s]", key_size, private_key);
297
298         /* Convert char to pkey */
299         bio_mem = BIO_new(BIO_s_mem());
300         if (bio_mem == NULL) {
301                 EM_DEBUG_EXCEPTION("malloc failed");
302                 err = EMAIL_ERROR_OUT_OF_MEMORY;
303                 goto FINISH_OFF;
304         }
305
306         if (BIO_write(bio_mem, private_key, key_size) <= 0) {
307                 EM_DEBUG_EXCEPTION("BIO_write failed");
308                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
309                 goto FINISH_OFF;
310         }
311
312         t_pri_key = PEM_read_bio_PrivateKey(bio_mem, NULL, 0, NULL);
313         if (t_pri_key == NULL) {
314                 EM_DEBUG_EXCEPTION("PEM_read_bio_PrivateKey failed");
315                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
316                 goto FINISH_OFF;
317         }
318
319         ret = true;
320
321 FINISH_OFF:
322
323         if (bio_mem)
324                 BIO_free(bio_mem);
325
326         if (true) {
327                 if (cert)
328                         *cert = t_cert;
329
330                 if (pri_key)
331                         *pri_key = t_pri_key;
332         } else {
333                 X509_free(t_cert);
334                 EVP_PKEY_free(t_pri_key);
335         }
336
337         if (private_key)
338                 EM_SAFE_FREE(private_key);
339
340         if (err_code)
341                 *err_code = err;
342
343         return ret;
344 }
345
346 INTERNAL_FUNC int emcore_add_public_certificate(char *multi_user_name, char *public_cert_path, char *save_name, int *err_code)
347 {
348         EM_DEBUG_FUNC_BEGIN_SEC("Path [%s], filename [%s]", public_cert_path, save_name);
349         int err = EMAIL_ERROR_NONE;
350         int ret = false;
351         int validity = 0;
352         int cert_type = 0;
353         char temp_file[512] = {0, };
354         char temp_save_name[512] = {0, };
355         char filepath[512] = {0, };
356         char *extension = NULL;
357         emstorage_certificate_tbl_t *cert = NULL;
358         CERT_CONTEXT *context = NULL;
359
360         if (public_cert_path == NULL || save_name == NULL) {
361                 EM_DEBUG_EXCEPTION("Invalid parameter");
362                 err = EMAIL_ERROR_INVALID_PARAM;
363                 goto FINISH_OFF;
364         }
365
366         /* Initilize the structure of certificate */
367         context = cert_svc_cert_context_init();
368
369         /* Parse the file type */
370         extension = em_get_extension_from_file_path(public_cert_path, NULL);
371         if (extension == NULL) {
372                 EM_DEBUG_EXCEPTION("Invalid parameter");
373                 err = EMAIL_ERROR_INVALID_PARAM;
374                 goto FINISH_OFF;
375         }
376
377         /* Get the file type information */
378         cert_type = emcore_get_certificate_type(extension, &err);
379         if (!cert_type || err == EMAIL_ERROR_INVALID_CERTIFICATE) {
380                 EM_DEBUG_EXCEPTION("Invalid certificate");
381                 goto FINISH_OFF;
382         }
383
384         /* Create temp file and rename */
385         if (cert_type == CERT_TYPE_P7S) {
386                 extension = "der";
387         }
388
389         SNPRINTF(temp_file, sizeof(temp_file), "%s%s%s.%s", MAILTEMP, DIR_SEPERATOR, save_name, extension);
390         EM_DEBUG_LOG_SEC("temp cert path : [%s]", temp_file);
391
392         if (!emstorage_copy_file(public_cert_path, temp_file, false, &err)) {
393                 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
394                 goto FINISH_OFF;
395         }
396
397         /* Load the public certificate */
398         err = cert_svc_load_file_to_context(context, temp_file);
399         if (err != CERT_SVC_ERR_NO_ERROR) {
400                 EM_DEBUG_EXCEPTION("Load cert failed : [%d]", err);
401                 err = EMAIL_ERROR_INVALID_CERTIFICATE;
402                 goto FINISH_OFF;
403         }
404
405         /* Verify the certificate */
406         if (cert_svc_verify_certificate(context, &validity) != CERT_SVC_ERR_NO_ERROR) {
407                 EM_DEBUG_EXCEPTION("cert_svc_verify_certificate failed");
408 //              err = EMAIL_ERROR_INVALID_CERTIFICATE;
409 //              goto FINISH_OFF;
410         }
411
412         if (validity <= 0) {
413                 EM_DEBUG_LOG("Invalid certificate");
414         }
415
416         /* Load the certificate information */
417         if (cert_svc_extract_certificate_data(context) != CERT_SVC_ERR_NO_ERROR) {
418                 EM_DEBUG_EXCEPTION("Extract the certificate failed");
419                 err = EMAIL_ERROR_UNKNOWN;
420                 goto FINISH_OFF;
421         }
422
423         /* Store the certificate file to trusted folder */
424         if (cert_svc_add_certificate_to_store(temp_file, TRUSTED_USER) != CERT_SVC_ERR_NO_ERROR) {
425                 EM_DEBUG_EXCEPTION("Add certificate to trusted folder");
426                 err = EMAIL_ERROR_UNKNOWN;
427                 goto FINISH_OFF;
428         }
429
430         /* Store the certificate to DB */
431         SNPRINTF(filepath, sizeof(filepath), "%s%s%s.%s", CERT_SVC_STORE_PATH, TRUSTED_USER, save_name, extension);
432         SNPRINTF(temp_save_name, sizeof(temp_save_name), "<%s>", save_name);
433
434         cert = (emstorage_certificate_tbl_t *)em_malloc(sizeof(emstorage_certificate_tbl_t));
435         if (cert == NULL) {
436                 EM_DEBUG_EXCEPTION("em_malloc failed");
437                 err = EMAIL_ERROR_OUT_OF_MEMORY;
438                 goto FINISH_OFF;
439         }
440
441         cert->issue_year = context->certDesc->info.validPeriod.firstYear;
442         cert->issue_year = context->certDesc->info.validPeriod.firstYear;
443         cert->issue_month = context->certDesc->info.validPeriod.firstMonth;
444         cert->issue_day = context->certDesc->info.validPeriod.firstDay;
445         cert->expiration_year= context->certDesc->info.validPeriod.secondYear;
446         cert->expiration_month = context->certDesc->info.validPeriod.secondMonth;
447         cert->expiration_day = context->certDesc->info.validPeriod.secondDay;
448         cert->issue_organization_name = EM_SAFE_STRDUP(context->certDesc->info.issuer.organizationName);
449         cert->email_address = EM_SAFE_STRDUP(temp_save_name);
450         cert->subject_str = EM_SAFE_STRDUP(context->certDesc->info.issuerStr);
451         cert->filepath = EM_SAFE_STRDUP(filepath);
452
453         if (emstorage_add_certificate(multi_user_name, cert, true, &err)) {
454                 EM_DEBUG_EXCEPTION("emstorage_add_certificate failed");
455                 goto FINISH_OFF;
456         }
457
458         if (!emstorage_delete_file(public_cert_path, &err)) {
459                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
460                 goto FINISH_OFF;
461         }
462
463         ret = true;
464
465 FINISH_OFF:
466
467         emstorage_delete_file(temp_file, NULL);
468
469         emstorage_free_certificate(&cert, 1, NULL);
470
471         cert_svc_cert_context_final(context);
472
473         if (err_code != NULL) {
474                 *err_code = err;
475         }
476
477         EM_DEBUG_FUNC_END();
478
479         return ret;
480
481 }
482
483 INTERNAL_FUNC int emcore_delete_public_certificate(char *multi_user_name, char *email_address, int *err_code)
484 {
485         EM_DEBUG_FUNC_BEGIN();
486         int ret = false;
487         int err = EMAIL_ERROR_NONE;
488         emstorage_certificate_tbl_t *certificate = NULL;
489
490         if (email_address == NULL) {
491                 EM_DEBUG_EXCEPTION("Invalid parameter");
492                 err = EMAIL_ERROR_INVALID_PARAM;
493                 goto FINISH_OFF;
494         }
495
496         if (!emstorage_get_certificate_by_email_address(multi_user_name, email_address, &certificate, false, 0, &err)) {
497                 EM_DEBUG_EXCEPTION("emstorage_get_certificate failed");
498                 goto FINISH_OFF;
499         }
500
501         if (remove(certificate->filepath) < 0) {
502                 EM_DEBUG_EXCEPTION_SEC("remove failed : [%s]", certificate->filepath);
503                 goto FINISH_OFF;
504         }
505
506         if (!emstorage_delete_certificate(multi_user_name, certificate->certificate_id, true, &err)) {
507                 EM_DEBUG_EXCEPTION("emstorage_delete_certificate failed");
508                 goto FINISH_OFF;
509         }
510
511         ret = true;
512 FINISH_OFF:
513
514         if (certificate != NULL)
515                 emstorage_free_certificate(&certificate, 1, NULL);
516
517         if (err_code != NULL)
518                 *err_code = err;
519
520         EM_DEBUG_FUNC_END();
521
522         return ret;
523 }
524
525 INTERNAL_FUNC int emcore_verify_signature(char *p7s_file_path, char *mime_entity, int *validity, int *err_code)
526 {
527         EM_DEBUG_FUNC_BEGIN_SEC("path : [%s], mime_entity : [%s]", p7s_file_path, mime_entity);
528         int ret = false;
529         int err = EMAIL_ERROR_NONE;
530         int t_validity = 0;
531         int flags = PKCS7_NOVERIFY;
532
533         BIO *bio_p7s = NULL;
534
535         BIO *bio_indata = NULL;
536
537         PKCS7 *pkcs7_p7s = NULL;
538
539         /* Open p7s file */
540         bio_p7s = BIO_new_file(p7s_file_path, INMODE);
541         if (!bio_p7s) {
542                 EM_DEBUG_EXCEPTION("File open failed");
543                 err = EMAIL_ERROR_SYSTEM_FAILURE;
544                 goto FINISH_OFF;
545         }
546
547         /* Open input data file */
548         bio_indata = BIO_new_file(mime_entity, INMODE);
549         if (!bio_p7s) {
550                 EM_DEBUG_EXCEPTION("File open failed");
551                 err = EMAIL_ERROR_SYSTEM_FAILURE;
552                 goto FINISH_OFF;
553         }
554
555         /* Load the p7s file */
556         pkcs7_p7s = d2i_PKCS7_bio(bio_p7s, NULL);
557         if (!pkcs7_p7s) {
558                 EM_DEBUG_EXCEPTION("d2i_PKCS7_bio failed");
559                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
560                 goto FINISH_OFF;
561         }
562
563         if (!PKCS7_verify(pkcs7_p7s, NULL, NULL, bio_indata, NULL, flags)) {
564                 EM_DEBUG_EXCEPTION("PKCS7_verify failed");
565                 goto FINISH_OFF;
566         }
567
568         t_validity = 1;
569
570         ret = true;
571
572 FINISH_OFF:
573
574         if (pkcs7_p7s)
575                 PKCS7_free(pkcs7_p7s);
576
577         if (bio_p7s)
578                 BIO_free(bio_p7s);
579
580         if (bio_indata)
581                 BIO_free(bio_indata);
582
583         if (validity)
584                 *validity = t_validity;
585
586         if (err_code)
587                 *err_code = err;
588
589         EM_DEBUG_FUNC_END();
590         return ret;
591 }
592
593 INTERNAL_FUNC int emcore_verify_certificate(char *certificate, int *validity, int *err_code)
594 {
595         EM_DEBUG_FUNC_BEGIN();
596         int ret = false;
597         int err = EMAIL_ERROR_NONE;
598         int p_validity = 0;
599
600         CERT_CONTEXT *context = NULL;
601
602         context = cert_svc_cert_context_init();
603
604         err = cert_svc_load_file_to_context(context, certificate);
605         if (err != CERT_SVC_ERR_NO_ERROR) {
606                 EM_DEBUG_EXCEPTION("Certificate load failed");
607                 goto FINISH_OFF;
608         }
609
610         err = cert_svc_verify_certificate(context, &p_validity);
611         if (err != CERT_SVC_ERR_NO_ERROR) {
612                 EM_DEBUG_EXCEPTION("Certificate verify failed");
613                 goto FINISH_OFF;
614         }
615
616         ret = true;
617
618 FINISH_OFF:
619
620         if (validity != NULL)
621                 *validity = p_validity;
622
623         if (err_code != NULL) {
624                 *err_code = err;
625         }
626
627         cert_svc_cert_context_final(context);
628
629         EM_DEBUG_FUNC_END();
630         return ret;
631 }
632
633 INTERNAL_FUNC int emcore_free_certificate(email_certificate_t **certificate, int count, int *err_code)
634 {
635         EM_DEBUG_FUNC_BEGIN("certificate [%p], count [%d]", certificate, count);
636
637         if (count <= 0 || !certificate || !*certificate) {
638                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
639                 if (err_code)
640                         *err_code = EMAIL_ERROR_INVALID_PARAM;
641                 return false;
642         }
643
644         email_certificate_t *p_certificate = *certificate;
645         int i;
646
647         for (i=0;i<count;i++) {
648                 EM_SAFE_FREE(p_certificate[i].issue_organization_name);
649                 EM_SAFE_FREE(p_certificate[i].email_address);
650                 EM_SAFE_FREE(p_certificate[i].subject_str);
651                 EM_SAFE_FREE(p_certificate[i].filepath);
652         }
653
654         EM_SAFE_FREE(p_certificate);
655         *certificate = NULL;
656
657         if (err_code)
658                 *err_code = EMAIL_ERROR_NONE;
659
660         EM_DEBUG_FUNC_END();
661         return true;
662 }