Tizen 2.0 Release
[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("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("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("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         OpenSSL_add_all_algorithms();
186         ERR_load_crypto_strings();
187         if (!(fp = fopen(certificate, "rb"))) {
188                 EM_DEBUG_EXCEPTION("fopen failed : [%s]", certificate);
189                 err = EMAIL_ERROR_SYSTEM_FAILURE;
190                 goto FINISH_OFF;
191         }
192         
193         p12 = d2i_PKCS12_fp(fp, NULL);
194         if (!p12) {
195                 EM_DEBUG_EXCEPTION("d2i_PKCS12_fp failed");
196                 err = EMAIL_ERROR_SYSTEM_FAILURE;
197                 goto FINISH_OFF;
198         }
199
200         if (!PKCS12_parse(p12, password, pri_key, cert, ca)) {
201                 EM_DEBUG_EXCEPTION("PKCS12_parse failed");
202                 err = EMAIL_ERROR_SYSTEM_FAILURE;
203                 goto FINISH_OFF;
204         }
205
206         ret = true;
207
208 FINISH_OFF:
209
210         if (fp)
211                 fclose(fp);
212
213         if (p12)
214                 PKCS12_free(p12);
215
216         if (err_code)
217                 *err_code = err;
218
219         return ret;     
220 }
221 #endif
222
223 INTERNAL_FUNC int emcore_load_PFX_file(char *certificate, EVP_PKEY **pri_key, X509 **cert, STACK_OF(X509) **ca, int *err_code)
224 {
225         EM_DEBUG_FUNC_BEGIN("certificate : [%s]", certificate);
226         int err = EMAIL_ERROR_NONE;
227         int ret = false;
228         size_t key_size = 0;
229         char *private_key = NULL;
230
231         /* Variable for certificate */
232         X509 *t_cert = NULL;
233         BIO *bio_mem = NULL;
234 //      STACK_OF(X509) *t_ca = NULL;
235         
236         /* Variable for private key */
237         EVP_PKEY *t_pri_key = NULL;
238
239         CertSvcString csstring;
240         CertSvcInstance cert_instance;
241         CertSvcCertificate csc_cert;
242         CertSvcCertificateList certificate_list;
243
244         if (certificate == NULL) {
245                 EM_DEBUG_EXCEPTION("Invalid parameter");
246                 err = EMAIL_ERROR_INVALID_PARAM;
247                 goto FINISH_OFF;
248         }
249
250         /* Create instance */
251         err = certsvc_instance_new(&cert_instance);
252         if (err != CERTSVC_SUCCESS) {
253                 EM_DEBUG_EXCEPTION("certsvc_instance_new failed : [%d]", err);
254                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
255                 goto FINISH_OFF;
256         }
257
258         /* Make the pfxID string */
259         err = certsvc_string_new(cert_instance, certificate, EM_SAFE_STRLEN(certificate), &csstring);
260         if (err != CERTSVC_SUCCESS) {
261                 EM_DEBUG_EXCEPTION("certsvc_string_new failed : [%d]", err);
262                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
263                 goto FINISH_OFF;
264         }
265
266         /* Load the certificate list of pkcs12 type */
267         err = certsvc_pkcs12_load_certificate_list(cert_instance, csstring, &certificate_list);
268         if (err != CERTSVC_SUCCESS) {
269                 EM_DEBUG_EXCEPTION("certsvc_pkcs12_load_certificate_list failed : [%d]", err);
270                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
271                 goto FINISH_OFF;
272         }
273
274         /* Get a certificate */
275         err = certsvc_certificate_list_get_one(certificate_list, 0, &csc_cert);
276         if (err != CERTSVC_SUCCESS) {
277                 EM_DEBUG_EXCEPTION("certsvc_certificate_list_get_one failed : [%d]", err);
278                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
279                 goto FINISH_OFF;
280         }
281
282         /* Convert from char to X509 */
283         err = certsvc_certificate_dup_x509(csc_cert, &t_cert);
284         if (t_cert == NULL || err != CERTSVC_SUCCESS) {
285                 EM_DEBUG_EXCEPTION("certsvc_certificate_dup_x509 failed");
286                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
287                 goto FINISH_OFF;
288         }
289
290         /* Get the private key */
291         err = certsvc_pkcs12_private_key_dup(cert_instance, csstring, &private_key, &key_size);
292         if (err != CERTSVC_SUCCESS) {
293                 EM_DEBUG_EXCEPTION("certsvc_pkcs12_private_key_dup failed : [%d]", err);
294                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
295                 goto FINISH_OFF;
296         }
297
298         EM_DEBUG_LOG("key_size : [%d], private_key : [%s]", key_size, private_key);
299
300         /* Convert char to pkey */
301         bio_mem = BIO_new(BIO_s_mem());
302         if (bio_mem == NULL) {
303                 EM_DEBUG_EXCEPTION("malloc failed");
304                 err = EMAIL_ERROR_OUT_OF_MEMORY;
305                 goto FINISH_OFF;
306         }
307
308         if (BIO_write(bio_mem, private_key, key_size) <= 0) {
309                 EM_DEBUG_EXCEPTION("BIO_write failed");
310                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
311                 goto FINISH_OFF;
312         }
313
314         t_pri_key = PEM_read_bio_PrivateKey(bio_mem, NULL, 0, NULL);
315         if (t_pri_key == NULL) {
316                 EM_DEBUG_EXCEPTION("PEM_read_bio_PrivateKey failed");
317                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
318                 goto FINISH_OFF;
319         }
320
321         ret = true;
322         
323 FINISH_OFF:
324
325         if (bio_mem)
326                 BIO_free(bio_mem);
327
328         if (true) {
329                 if (cert)
330                         *cert = t_cert;
331
332                 if (pri_key)
333                         *pri_key = t_pri_key;
334         } else {
335                 X509_free(t_cert);
336                 EVP_PKEY_free(t_pri_key);
337         }
338
339         if (private_key)
340                 EM_SAFE_FREE(private_key);
341
342         if (err_code)
343                 *err_code = err;
344
345         return ret;
346 }
347
348 INTERNAL_FUNC int emcore_add_public_certificate(char *public_cert_path, char *save_name, int *err_code)
349 {
350         EM_DEBUG_FUNC_BEGIN("Path [%s], filename [%s]", public_cert_path, save_name);
351         int err = EMAIL_ERROR_NONE;     
352         int ret = false;
353         int validity = 0;
354         int cert_type = 0;
355         char temp_file[512] = {0, };
356         char temp_save_name[512] = {0, };
357         char filepath[512] = {0, };
358         char *extension = NULL;
359         emstorage_certificate_tbl_t *cert = NULL;
360         CERT_CONTEXT *context = NULL;
361
362         if (public_cert_path == NULL || save_name == NULL) {
363                 EM_DEBUG_EXCEPTION("Invalid parameter");
364                 err = EMAIL_ERROR_INVALID_PARAM;
365                 goto FINISH_OFF;                
366         }
367         
368         /* Initilize the structure of certificate */
369         context = cert_svc_cert_context_init();
370
371         /* Parse the file type */
372         extension = em_get_extension_from_file_path(public_cert_path, NULL);
373         if (extension == NULL) {
374                 EM_DEBUG_EXCEPTION("Invalid parameter");
375                 err = EMAIL_ERROR_INVALID_PARAM;
376                 goto FINISH_OFF;
377         }
378
379         /* Get the file type information */
380         cert_type = emcore_get_certificate_type(extension, &err);
381         if (!cert_type || err == EMAIL_ERROR_INVALID_CERTIFICATE) {
382                 EM_DEBUG_EXCEPTION("Invalid certificate");
383                 goto FINISH_OFF;
384         }
385
386         /* Create temp file and rename */
387         if (cert_type == CERT_TYPE_P7S) {
388                 extension = "der";
389         }
390         
391         SNPRINTF(temp_file, sizeof(temp_file), "%s%s%s.%s", MAILTEMP, DIR_SEPERATOR, save_name, extension);
392         EM_DEBUG_LOG("temp cert path : [%s]", temp_file);
393
394         if (!emstorage_copy_file(public_cert_path, temp_file, false, &err)) {
395                 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
396                 goto FINISH_OFF;
397         }
398         
399         /* Load the public certificate */
400         err = cert_svc_load_file_to_context(context, temp_file);
401         if (err != CERT_SVC_ERR_NO_ERROR) {
402                 EM_DEBUG_EXCEPTION("Load cert failed : [%d]", err);
403                 err = EMAIL_ERROR_INVALID_CERTIFICATE;
404                 goto FINISH_OFF;
405         }
406
407         /* Verify the certificate */
408         if (cert_svc_verify_certificate(context, &validity) != CERT_SVC_ERR_NO_ERROR) {
409                 EM_DEBUG_EXCEPTION("cert_svc_verify_certificate failed");
410 //              err = EMAIL_ERROR_INVALID_CERTIFICATE;
411 //              goto FINISH_OFF;
412         } 
413
414         if (validity <= 0) {
415                 EM_DEBUG_LOG("Invalid certificate");
416         }
417
418         /* Load the certificate information */
419         if (cert_svc_extract_certificate_data(context) != CERT_SVC_ERR_NO_ERROR) {
420                 EM_DEBUG_EXCEPTION("Extract the certificate failed");
421                 err = EMAIL_ERROR_UNKNOWN;
422                 goto FINISH_OFF;
423         }
424
425         /* Store the certificate file to trusted folder */
426         if (cert_svc_add_certificate_to_store(temp_file, TRUSTED_USER) != CERT_SVC_ERR_NO_ERROR) {
427                 EM_DEBUG_EXCEPTION("Add certificate to trusted folder");
428                 err = EMAIL_ERROR_UNKNOWN;
429                 goto FINISH_OFF;
430         }
431
432         /* Store the certificate to DB */
433         SNPRINTF(filepath, sizeof(filepath), "%s%s%s.%s", CERT_SVC_STORE_PATH, TRUSTED_USER, save_name, extension);
434         SNPRINTF(temp_save_name, sizeof(temp_save_name), "<%s>", save_name);
435
436         cert = (emstorage_certificate_tbl_t *)em_malloc(sizeof(emstorage_certificate_tbl_t));
437         if (cert == NULL) {
438                 EM_DEBUG_EXCEPTION("em_malloc failed");
439                 err = EMAIL_ERROR_OUT_OF_MEMORY;
440                 goto FINISH_OFF;
441         }
442
443         cert->issue_year = context->certDesc->info.validPeriod.firstYear;
444         cert->issue_year = context->certDesc->info.validPeriod.firstYear;
445         cert->issue_month = context->certDesc->info.validPeriod.firstMonth;
446         cert->issue_day = context->certDesc->info.validPeriod.firstDay;         
447         cert->expiration_year= context->certDesc->info.validPeriod.secondYear;          
448         cert->expiration_month = context->certDesc->info.validPeriod.secondMonth;               
449         cert->expiration_day = context->certDesc->info.validPeriod.secondDay;                           
450         cert->issue_organization_name = EM_SAFE_STRDUP(context->certDesc->info.issuer.organizationName);
451         cert->email_address = EM_SAFE_STRDUP(temp_save_name);
452         cert->subject_str = EM_SAFE_STRDUP(context->certDesc->info.issuerStr);          
453         cert->filepath = EM_SAFE_STRDUP(filepath);                              
454
455         if (emstorage_add_certificate(cert, true, &err)) {
456                 EM_DEBUG_EXCEPTION("emstorage_add_certificate failed");
457                 goto FINISH_OFF;
458         }
459
460         if (!emstorage_delete_file(public_cert_path, &err)) {
461                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
462                 goto FINISH_OFF;
463         }
464
465         ret = true;
466
467 FINISH_OFF:
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 *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(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("remove failed : [%s]", certificate->filepath);
503                 goto FINISH_OFF;
504         }
505
506         if (!emstorage_delete_certificate(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("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         /* Initialize */
540         OpenSSL_add_all_algorithms();
541
542         /* Open p7s file */
543         bio_p7s = BIO_new_file(p7s_file_path, INMODE);
544         if (!bio_p7s) {
545                 EM_DEBUG_EXCEPTION("File open failed");
546                 err = EMAIL_ERROR_SYSTEM_FAILURE;
547                 goto FINISH_OFF;
548         }
549
550         /* Open input data file */
551         bio_indata = BIO_new_file(mime_entity, INMODE);
552         if (!bio_p7s) {
553                 EM_DEBUG_EXCEPTION("File open failed");
554                 err = EMAIL_ERROR_SYSTEM_FAILURE;
555                 goto FINISH_OFF;
556         }
557
558         /* Load the p7s file */
559         pkcs7_p7s = d2i_PKCS7_bio(bio_p7s, NULL);
560         if (!pkcs7_p7s) {
561                 EM_DEBUG_EXCEPTION("d2i_PKCS7_bio failed");
562                 err = EMAIL_ERROR_LOAD_CERTIFICATE_FAILURE;
563                 goto FINISH_OFF;
564         }
565
566         if (!PKCS7_verify(pkcs7_p7s, NULL, NULL, bio_indata, NULL, flags)) {
567                 EM_DEBUG_EXCEPTION("PKCS7_verify failed");
568                 err = EMAIL_ERROR_SYSTEM_FAILURE;
569                 goto FINISH_OFF;
570         }
571
572         t_validity = 1;
573
574         ret = true;
575
576 FINISH_OFF:
577
578         EVP_cleanup();
579
580         if (pkcs7_p7s)
581                 PKCS7_free(pkcs7_p7s);
582
583         if (bio_p7s)
584                 BIO_free(bio_p7s);
585
586         if (bio_indata)
587                 BIO_free(bio_indata);
588
589         if (validity)
590                 *validity = t_validity;
591
592         if (err_code)
593                 *err_code = err;
594
595         EM_DEBUG_FUNC_END();
596         return ret;
597 }
598
599 INTERNAL_FUNC int emcore_verify_certificate(char *certificate, int *validity, int *err_code)
600 {
601         EM_DEBUG_FUNC_BEGIN();
602         int ret = false;
603         int err = EMAIL_ERROR_NONE;
604         int p_validity = 0;
605
606         CERT_CONTEXT *context = NULL;
607         
608         context = cert_svc_cert_context_init();
609
610         err = cert_svc_load_file_to_context(context, certificate);
611         if (err != CERT_SVC_ERR_NO_ERROR) {
612                 EM_DEBUG_EXCEPTION("Certificate load failed");
613                 goto FINISH_OFF;
614         }
615
616         err = cert_svc_verify_certificate(context, &p_validity);
617         if (err != CERT_SVC_ERR_NO_ERROR) {
618                 EM_DEBUG_EXCEPTION("Certificate verify failed");
619                 goto FINISH_OFF;
620         }
621
622         ret = true;
623
624 FINISH_OFF:
625
626         if (validity != NULL) 
627                 *validity = p_validity;
628
629         if (err_code != NULL) {
630                 *err_code = err;
631         }
632
633         cert_svc_cert_context_final(context);
634
635         EM_DEBUG_FUNC_END();
636         return ret;
637 }
638
639 INTERNAL_FUNC int emcore_free_certificate(email_certificate_t **certificate, int count, int *err_code)
640 {
641         EM_DEBUG_FUNC_BEGIN("certificate [%p], count [%d]", certificate, count);
642         
643         if (count <= 0 || !certificate || !*certificate) {
644                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
645                 if (err_code)   
646                         *err_code = EMAIL_ERROR_INVALID_PARAM;
647                 return false;
648         }
649
650         email_certificate_t *p_certificate = *certificate;
651         int i;
652         
653         for (i=0;i<count;i++) {
654                 EM_SAFE_FREE(p_certificate[i].issue_organization_name);
655                 EM_SAFE_FREE(p_certificate[i].email_address);
656                 EM_SAFE_FREE(p_certificate[i].subject_str);
657                 EM_SAFE_FREE(p_certificate[i].filepath);
658         }
659
660         EM_SAFE_FREE(p_certificate);
661         *certificate = NULL;
662
663         if (err_code)
664                 *err_code = EMAIL_ERROR_NONE;
665
666         EM_DEBUG_FUNC_END();
667         return true;
668 }