66c9c020e42186408b3e9285d278f9121a984489
[platform/core/messaging/email-service.git] / email-api / email-api-smime.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 /**
25  *
26  * This file contains the data structures and interfaces needed for application,
27  * to interact with Email Engine.
28  * @file                email-api-smime.c
29  * @brief               This file contains the data structures and interfaces of SMIME related Functionality provided by
30  *                      Email Engine . 
31  */
32
33 #include "string.h"
34 #include "email-api-mail.h"
35 #include "email-convert.h"
36 #include "email-api-account.h"
37 #include "email-storage.h"
38 #include "email-utilities.h"
39 #include "email-core-mail.h"
40 #include "email-core-mime.h"
41 #include "email-core-account.h"
42 #include "email-core-cert.h"
43 #include "email-core-smime.h"
44 #include "email-core-pgp.h"
45 #include "email-core-signal.h"
46 #include "email-ipc.h"
47
48 EXPORT_API int email_add_certificate(char *certificate_path, char *email_address)
49 {
50         EM_DEBUG_API_BEGIN ();
51         EM_DEBUG_FUNC_BEGIN_SEC("certificate_path[%s]", certificate_path);
52         int result_from_ipc = 0;
53         int err = EMAIL_ERROR_NONE;
54         
55         if (!certificate_path) {
56                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
57                 return EMAIL_ERROR_INVALID_PARAM;
58         }
59
60         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_CERTIFICATE);
61         if (hAPI == NULL) {
62                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
63                 err = EMAIL_ERROR_NULL_VALUE;
64                 goto FINISH_OFF;
65         }
66
67         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, certificate_path, EM_SAFE_STRLEN(certificate_path)+1)) {
68                 EM_DEBUG_EXCEPTION_SEC("emipc_add_parameter certificate_path[%s] failed", certificate_path);
69                 err = EMAIL_ERROR_NULL_VALUE;
70                 goto FINISH_OFF;
71         }
72
73         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
74                 EM_DEBUG_EXCEPTION_SEC("emipc_add_parameter certificate_path[%s] failed", email_address);
75                 err = EMAIL_ERROR_NULL_VALUE;
76                 goto FINISH_OFF;
77         }
78
79         if (emipc_execute_proxy_api(hAPI) < 0) {
80                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
81                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
82                 goto FINISH_OFF;
83         }
84
85         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
86         if (result_from_ipc != EMAIL_ERROR_NONE) {
87                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
88                 err = EMAIL_ERROR_IPC_CRASH;
89                 goto FINISH_OFF;
90         }
91
92 FINISH_OFF:
93
94         if (hAPI)
95                 emipc_destroy_email_api(hAPI);
96
97         EM_DEBUG_API_END ("err[%d]", err);
98         return err;
99 }
100
101 EXPORT_API int email_delete_certificate(char *email_address)
102 {
103         EM_DEBUG_API_BEGIN ();
104         EM_DEBUG_FUNC_BEGIN_SEC("email_address[%s]", email_address);
105         int result_from_ipc = 0;
106         int err = EMAIL_ERROR_NONE;
107         
108         if (!email_address) {
109                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
110                 return EMAIL_ERROR_INVALID_PARAM;
111         }
112
113         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_CERTIFICATE);
114         if (hAPI == NULL) {
115                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
116                 err = EMAIL_ERROR_NULL_VALUE;
117                 goto FINISH_OFF;
118         }
119
120         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
121                 EM_DEBUG_EXCEPTION_SEC("emipc_add_parameter email_address[%s] failed", email_address);
122                 err = EMAIL_ERROR_NULL_VALUE;
123                 goto FINISH_OFF;
124         }
125
126         if (emipc_execute_proxy_api(hAPI) < 0) {
127                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
128                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
129                 goto FINISH_OFF;
130         }
131
132         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
133         if (result_from_ipc != EMAIL_ERROR_NONE) {
134                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
135                 err = EMAIL_ERROR_IPC_CRASH;
136                 goto FINISH_OFF;
137         }
138
139 FINISH_OFF:
140
141         if (hAPI)
142                 emipc_destroy_email_api(hAPI);
143
144         EM_DEBUG_API_END ("err[%d]", err);
145         return err;
146 }
147
148 EXPORT_API int email_get_certificate(char *email_address, email_certificate_t **certificate)
149 {
150         EM_DEBUG_API_BEGIN ();
151         int err = EMAIL_ERROR_NONE;
152         char temp_email_address[130] = {0, };
153     char *multi_user_name = NULL;
154         emstorage_certificate_tbl_t *cert = NULL;
155         
156         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
157         EM_IF_NULL_RETURN_VALUE(certificate, EMAIL_ERROR_INVALID_PARAM);
158
159     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
160         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
161         goto FINISH_OFF;
162     }
163
164         SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
165
166         if (!emstorage_get_certificate_by_email_address(multi_user_name, temp_email_address, &cert, false, 0, &err)) {
167                 EM_DEBUG_EXCEPTION("emstorage_get_certificate_by_index failed - %d", err);
168         goto FINISH_OFF;
169         }
170
171         if (!em_convert_certificate_tbl_to_certificate(cert, certificate, &err)) {
172                 EM_DEBUG_EXCEPTION("em_convert_certificate_tbl_to_certificate failed");
173         goto FINISH_OFF;
174         }       
175
176 FINISH_OFF:
177     
178     EM_SAFE_FREE(multi_user_name);
179
180         EM_DEBUG_API_END ("err[%d]", err);
181         return err;
182 }
183
184 EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output_mail_data, 
185                                                                                 email_attachment_data_t **output_attachment_data, 
186                                                                                 int *output_attachment_count, int *verify)
187 {
188         EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
189         int err = EMAIL_ERROR_NONE;
190         int p_output_attachment_count = 0;
191     int i = 0;
192         char *decrypt_filepath = NULL;
193     char *search = NULL;
194     char *multi_user_name = NULL;
195         email_mail_data_t *p_output_mail_data = NULL;
196         email_attachment_data_t *p_output_attachment_data = NULL;
197         emstorage_account_tbl_t *p_account_tbl = NULL;
198
199         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
200
201         if (!output_mail_data || !output_attachment_data || !output_attachment_count) {
202                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
203                 err = EMAIL_ERROR_INVALID_PARAM;
204                 goto FINISH_OFF;
205         }
206
207     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
208         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]");
209         goto FINISH_OFF;
210     }
211
212         if ((err = emcore_get_mail_data(multi_user_name, mail_id, &p_output_mail_data)) != EMAIL_ERROR_NONE) {
213                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
214                 goto FINISH_OFF;
215         }
216
217         if (!emstorage_get_account_by_id(multi_user_name, p_output_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
218                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
219                 goto FINISH_OFF;
220         }
221
222         if ((err = emcore_get_attachment_data_list(multi_user_name, mail_id, &p_output_attachment_data, &p_output_attachment_count)) != EMAIL_ERROR_NONE) {
223                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed");
224                 goto FINISH_OFF;
225         }
226
227         for (i = 0; i < p_output_attachment_count; i++) {
228                 EM_DEBUG_LOG("mime_type : [%s]", p_output_attachment_data[i].attachment_mime_type);
229                 if (p_output_attachment_data[i].attachment_mime_type && (search = strcasestr(p_output_attachment_data[i].attachment_mime_type, "PKCS7-MIME"))) {
230                         EM_DEBUG_LOG("Found the encrypt file");
231                         break;
232                 } else if (p_output_attachment_data[i].attachment_mime_type && (search = strcasestr(p_output_attachment_data[i].attachment_mime_type, "octet-stream"))) {
233                         EM_DEBUG_LOG("Found the encrypt file");
234                         break;
235                 }
236         }
237
238         if (!search) {
239                 EM_DEBUG_EXCEPTION("No have a decrypt file");
240                 err = EMAIL_ERROR_INVALID_PARAM;
241                 goto FINISH_OFF;
242         }
243
244         if (p_output_mail_data->smime_type == EMAIL_SMIME_ENCRYPTED || p_output_mail_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
245                 emcore_init_openssl_library();
246                 if (!emcore_smime_get_decrypt_message(p_output_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
247                         EM_DEBUG_EXCEPTION("emcore_smime_get_decrypt_message failed");
248                         emcore_clean_openssl_library();
249                         goto FINISH_OFF;
250                 }
251                 emcore_clean_openssl_library();
252         } else if (p_output_mail_data->smime_type == EMAIL_PGP_ENCRYPTED) {
253                 if ((err = emcore_pgp_get_decrypted_message(p_output_attachment_data[i].attachment_path, p_output_mail_data->pgp_password, false, &decrypt_filepath, verify)) != EMAIL_ERROR_NONE) {
254                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
255                         goto FINISH_OFF;
256                 }
257         } else if (p_output_mail_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
258                 if ((err = emcore_pgp_get_decrypted_message(p_output_attachment_data[i].attachment_path, p_output_mail_data->pgp_password, true, &decrypt_filepath, verify)) != EMAIL_ERROR_NONE) {
259                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
260                         goto FINISH_OFF;
261                 }
262         } else {
263                 EM_DEBUG_LOG("Invalid encrypted mail");
264                 err = EMAIL_ERROR_INVALID_PARAM;
265                 goto FINISH_OFF;
266         }
267
268         /* Change decrpyt_message to mail_data_t */
269         if (!emcore_parse_mime_file_to_mail(decrypt_filepath, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
270                 EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed : [%d]", err);
271                 goto FINISH_OFF;
272         }
273
274         (*output_mail_data)->subject                 = EM_SAFE_STRDUP(p_output_mail_data->subject);
275         (*output_mail_data)->date_time               = p_output_mail_data->date_time;
276         (*output_mail_data)->full_address_return     = EM_SAFE_STRDUP(p_output_mail_data->full_address_return);
277         (*output_mail_data)->email_address_recipient = EM_SAFE_STRDUP(p_output_mail_data->email_address_recipient);
278         (*output_mail_data)->email_address_sender    = EM_SAFE_STRDUP(p_output_mail_data->email_address_sender);
279         (*output_mail_data)->full_address_reply      = EM_SAFE_STRDUP(p_output_mail_data->full_address_reply);
280         (*output_mail_data)->full_address_from       = EM_SAFE_STRDUP(p_output_mail_data->full_address_from);
281         (*output_mail_data)->full_address_to         = EM_SAFE_STRDUP(p_output_mail_data->full_address_to);
282         (*output_mail_data)->full_address_cc         = EM_SAFE_STRDUP(p_output_mail_data->full_address_cc);
283         (*output_mail_data)->full_address_bcc        = EM_SAFE_STRDUP(p_output_mail_data->full_address_bcc);
284         (*output_mail_data)->flags_flagged_field     = p_output_mail_data->flags_flagged_field;
285
286 FINISH_OFF:
287
288     EM_SAFE_FREE(decrypt_filepath);
289
290         if (p_account_tbl)
291                 emstorage_free_account(&p_account_tbl, 1, NULL);
292
293         if (p_output_mail_data)
294                 email_free_mail_data(&p_output_mail_data, 1);
295
296         if (p_output_attachment_data)
297                 email_free_attachment_data(&p_output_attachment_data, p_output_attachment_count);
298
299     EM_SAFE_FREE(multi_user_name);
300
301         EM_DEBUG_API_END ("err[%d]", err);
302         return err;
303 }
304
305 EXPORT_API int email_get_decrypt_message_ex(email_mail_data_t *input_mail_data, 
306                                                                                         email_attachment_data_t *input_attachment_data, 
307                                                                                         int input_attachment_count,
308                                             email_mail_data_t **output_mail_data, 
309                                                                                         email_attachment_data_t **output_attachment_data, 
310                                                                                         int *output_attachment_count,
311                                                                                         int *verify)
312 {
313         EM_DEBUG_API_BEGIN ();
314         int err = EMAIL_ERROR_NONE;
315     int i = 0;
316         char *decrypt_filepath = NULL;
317     char *search = NULL;
318     char *multi_user_name = NULL;
319         emstorage_account_tbl_t *p_account_tbl = NULL;
320
321         EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
322
323         if (!output_mail_data || !output_attachment_data || !output_attachment_count) {
324                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
325                 err = EMAIL_ERROR_INVALID_PARAM;
326                 goto FINISH_OFF;
327         }
328
329     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
330         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
331         goto FINISH_OFF;
332     }
333
334         if (!emstorage_get_account_by_id(multi_user_name, input_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
335                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
336                 goto FINISH_OFF;
337         }
338
339         for (i = 0; i < input_attachment_count; i++) {
340                 EM_DEBUG_LOG("mime_type : [%s]", input_attachment_data[i].attachment_mime_type);
341                 if (input_attachment_data[i].attachment_mime_type && (search = strcasestr(input_attachment_data[i].attachment_mime_type, "PKCS7-MIME"))) {
342                         EM_DEBUG_LOG("Found the encrypt file");
343                         break;
344                 } else if (input_attachment_data[i].attachment_mime_type && (search = strcasestr(input_attachment_data[i].attachment_mime_type, "octet-stream"))) {
345                         EM_DEBUG_LOG("Found the encrypt file");
346                         break;
347                 }
348         }
349
350         if (!search) {
351                 EM_DEBUG_EXCEPTION("No have a decrypt file");
352                 err = EMAIL_ERROR_INVALID_PARAM;
353                 goto FINISH_OFF;
354         }
355
356         if (input_mail_data->smime_type == EMAIL_SMIME_ENCRYPTED || input_mail_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
357                 emcore_init_openssl_library();
358                 if (!emcore_smime_get_decrypt_message(input_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
359                         EM_DEBUG_EXCEPTION("emcore_smime_get_decrypt_message failed");
360                         emcore_clean_openssl_library();
361                         goto FINISH_OFF;
362                 }
363                 emcore_clean_openssl_library();
364         } else if (input_mail_data->smime_type == EMAIL_PGP_ENCRYPTED) {
365                 if ((err = emcore_pgp_get_decrypted_message(input_attachment_data[i].attachment_path, input_mail_data->pgp_password, false, &decrypt_filepath, verify)) != EMAIL_ERROR_NONE) {
366                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
367                         goto FINISH_OFF;
368                 }
369         } else if (input_mail_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
370                 if ((err = emcore_pgp_get_decrypted_message(input_attachment_data[i].attachment_path, input_mail_data->pgp_password, true, &decrypt_filepath, verify)) != EMAIL_ERROR_NONE) {
371                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
372                         goto FINISH_OFF;
373                 }
374         } else {
375                 EM_DEBUG_LOG("Invalid encrypted mail");
376                 err = EMAIL_ERROR_INVALID_PARAM;
377                 goto FINISH_OFF;
378         }
379
380         /* Change decrpyt_message to mail_data_t */
381         if (!emcore_parse_mime_file_to_mail(decrypt_filepath, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
382                 EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed : [%d]", err);
383                 goto FINISH_OFF;
384         }
385
386         (*output_mail_data)->subject                 = EM_SAFE_STRDUP(input_mail_data->subject);
387         (*output_mail_data)->date_time               = input_mail_data->date_time;
388         (*output_mail_data)->full_address_return     = EM_SAFE_STRDUP(input_mail_data->full_address_return);
389         (*output_mail_data)->email_address_recipient = EM_SAFE_STRDUP(input_mail_data->email_address_recipient);
390         (*output_mail_data)->email_address_sender    = EM_SAFE_STRDUP(input_mail_data->email_address_sender);
391         (*output_mail_data)->full_address_reply      = EM_SAFE_STRDUP(input_mail_data->full_address_reply);
392         (*output_mail_data)->full_address_from       = EM_SAFE_STRDUP(input_mail_data->full_address_from);
393         (*output_mail_data)->full_address_to         = EM_SAFE_STRDUP(input_mail_data->full_address_to);
394         (*output_mail_data)->full_address_cc         = EM_SAFE_STRDUP(input_mail_data->full_address_cc);
395         (*output_mail_data)->full_address_bcc        = EM_SAFE_STRDUP(input_mail_data->full_address_bcc);
396         (*output_mail_data)->flags_flagged_field     = input_mail_data->flags_flagged_field;
397
398 FINISH_OFF:
399
400     EM_SAFE_FREE(decrypt_filepath);
401     EM_SAFE_FREE(multi_user_name);
402
403         if (p_account_tbl)
404                 emstorage_free_account(&p_account_tbl, 1, NULL);
405
406         EM_DEBUG_API_END ("err[%d]", err);
407         return err;
408 }
409
410 EXPORT_API int email_verify_signature(int mail_id, int *verify)
411 {
412         EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
413
414         if (mail_id <= 0) {
415                 EM_DEBUG_EXCEPTION("Invalid parameter");
416                 return EMAIL_ERROR_INVALID_PARAM;
417         }
418
419         int result_from_ipc = 0;
420         int p_verify = 0;
421         int err = EMAIL_ERROR_NONE;
422
423         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
424         if (hAPI == NULL) {
425                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
426                 err = EMAIL_ERROR_NULL_VALUE;
427                 goto FINISH_OFF;
428         }
429
430         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &mail_id, sizeof(int))) {
431                 EM_DEBUG_EXCEPTION("emipc_add_parameter pass_phrase[%d] failed", mail_id);
432                 err = EMAIL_ERROR_NULL_VALUE;
433                 goto FINISH_OFF;
434         }
435
436         if (emipc_execute_proxy_api(hAPI) < 0) {
437                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
438                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
439                 goto FINISH_OFF;
440         }
441
442         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &p_verify);
443         if (result_from_ipc != EMAIL_ERROR_NONE) {
444                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
445                 err = EMAIL_ERROR_IPC_CRASH;
446                 goto FINISH_OFF;
447         }
448
449 FINISH_OFF:
450
451         if (hAPI)
452                 emipc_destroy_email_api(hAPI);
453
454         if (verify != NULL)
455                 *verify = p_verify;
456
457         EM_DEBUG_API_END ("err[%d]", err);
458         return err;
459 }
460
461 EXPORT_API int email_verify_signature_ex(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data, int input_attachment_count, int *verify)
462 {
463         EM_DEBUG_API_BEGIN ();
464
465         if (!input_mail_data || !input_attachment_data || input_attachment_count <= 0) {
466                 EM_DEBUG_EXCEPTION("Invalid parameter");
467                 return EMAIL_ERROR_INVALID_PARAM;
468         }
469
470         int count = 0;
471         int err = EMAIL_ERROR_NONE;
472
473         for (count = 0; count < input_attachment_count ; count++) {
474                 if (input_attachment_data[count].attachment_mime_type && strcasestr(input_attachment_data[count].attachment_mime_type, "SIGNATURE"))
475                         break;
476         }
477
478         if (input_mail_data->smime_type == EMAIL_SMIME_SIGNED) {
479                 emcore_init_openssl_library();
480                 if (!emcore_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, verify, &err)) 
481                         EM_DEBUG_EXCEPTION("emcore_verify_signature failed : [%d]", err);
482
483                 emcore_clean_openssl_library();
484         } else if(input_mail_data->smime_type == EMAIL_PGP_SIGNED) {
485                 if ((err = emcore_pgp_get_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, input_mail_data->digest_type, verify)) != EMAIL_ERROR_NONE)
486                         EM_DEBUG_EXCEPTION("emcore_pgp_get_verify_siganture failed : [%d]", err);
487         } else {
488                 EM_DEBUG_LOG("Invalid signed mail : mime_type[%d]", input_mail_data->smime_type);
489                 err = EMAIL_ERROR_INVALID_PARAM;
490         }
491
492
493         EM_DEBUG_API_END ("err[%d]", err);
494         return err;
495 }
496
497 EXPORT_API int email_verify_certificate(char *certificate_path, int *verify)
498 {
499         EM_DEBUG_API_BEGIN ();
500         EM_DEBUG_FUNC_BEGIN_SEC("certificate_path[%s]", certificate_path);
501         int err = EMAIL_ERROR_NONE;
502         int result_from_ipc = 0;
503         int p_verify = 0;
504         
505         if (!certificate_path) {
506                 EM_DEBUG_EXCEPTION("Invalid parameter");
507                 return EMAIL_ERROR_INVALID_PARAM;
508         }
509
510         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_CERTIFICATE);
511         if (hAPI == NULL) {
512                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
513                 err = EMAIL_ERROR_NULL_VALUE;
514                 goto FINISH_OFF;
515         }
516
517         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, certificate_path, EM_SAFE_STRLEN(certificate_path)+1)) {
518                 EM_DEBUG_EXCEPTION_SEC("emipc_add_paramter failed : [%s]", certificate_path);
519                 err = EMAIL_ERROR_NULL_VALUE;
520                 goto FINISH_OFF;
521         }
522
523         if (emipc_execute_proxy_api(hAPI) < 0) {
524                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
525                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
526                 goto FINISH_OFF;
527         }
528
529         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &p_verify);
530         if (result_from_ipc != EMAIL_ERROR_NONE) {
531                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
532                 err = EMAIL_ERROR_IPC_CRASH;
533                 goto FINISH_OFF;
534         }
535
536 FINISH_OFF:
537
538         if (hAPI)       
539                 emipc_destroy_email_api(hAPI);
540
541         if (verify != NULL)
542                 *verify = p_verify;
543
544         EM_DEBUG_API_END ("err[%d]", err);
545         return err;
546 }
547
548 /*
549 EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url, unsigned *handle)
550 {
551         EM_DEBUG_FUNC_BEGIN_SEC("email_address : [%s], response_url : [%s], handle : [%p]", email_address, response_url, handle);
552
553         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
554
555         int err = EMAIL_ERROR_NONE;
556         HIPC_API hAPI = NULL;
557
558         hAPI = emipc_create_email_api(_EMAIL_API_CHECK_OCSP_STATUS);
559         
560         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
561
562         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
563                 EM_DEBUG_EXCEPTION("email_check_ocsp_status--ADD Param email_address failed");
564                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
565         }
566
567         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, response_url, EM_SAFE_STRLEN(response_url)+1)) {
568                 EM_DEBUG_EXCEPTION("email_check_ocsp_status--ADD Param response_url failed");
569                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
570         }
571
572         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
573                 EM_DEBUG_EXCEPTION("email_check_oscp_status--emipc_execute_proxy_api failed");
574                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
575         }
576
577         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
578         if (err == EMAIL_ERROR_NONE) {
579                 if (handle)
580                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
581         }
582 }
583 */
584 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle)
585 {
586         EM_DEBUG_API_BEGIN ("account_id[%d]", account_id);
587         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d] email_address[%s] handle[%p]", account_id, email_address, handle);
588         
589         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
590         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
591
592         int err = EMAIL_ERROR_NONE;
593         int as_handle = 0;
594     char *multi_user_name = NULL;
595         email_account_server_t account_server_type;
596         ASNotiData as_noti_data;
597
598     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
599         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
600         goto FINISH_OFF;
601     }
602
603         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
604
605         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false) {
606                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
607                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
608                 goto FINISH_OFF;
609         }
610
611         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
612                 EM_DEBUG_EXCEPTION("This api is not supported except of active sync");
613                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
614                 goto FINISH_OFF;
615         }
616
617         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
618                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync_failed[%d]", err);
619                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
620                 goto FINISH_OFF;                
621         }
622
623         as_noti_data.validate_certificate.handle = as_handle;
624         as_noti_data.validate_certificate.account_id = account_id;
625         as_noti_data.validate_certificate.email_address = email_address;
626     as_noti_data.validate_certificate.multi_user_name = multi_user_name;
627
628         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE, &as_noti_data) == false) {
629                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
630                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
631                 goto FINISH_OFF;
632         }
633
634         if (handle)
635                 *handle = as_handle;
636
637 FINISH_OFF:
638
639     EM_SAFE_FREE(multi_user_name);
640         EM_DEBUG_API_END ("err[%d]", err);
641         return err;
642 }
643
644 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle)
645 {
646         EM_DEBUG_API_BEGIN ("account_id[%d]", account_id);
647         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d] email_address[%s] handle[%p]", account_id, email_address, handle);
648         
649         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
650         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
651
652         int err = EMAIL_ERROR_NONE;
653         int as_handle = 0;
654     char *multi_user_name = NULL;
655         email_account_server_t account_server_type;
656         ASNotiData as_noti_data;
657
658     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
659         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
660         goto FINISH_OFF;
661     }
662
663         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
664
665         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false) {
666                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
667                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
668                 goto FINISH_OFF;
669         }
670
671         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
672                 EM_DEBUG_EXCEPTION("This api is not supported except of active sync");
673                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
674                 goto FINISH_OFF;
675         }
676
677         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
678                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync_failed[%d]", err);
679                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
680                 goto FINISH_OFF;                
681         }
682
683         as_noti_data.get_resolve_recipients.handle          = as_handle;
684         as_noti_data.get_resolve_recipients.account_id      = account_id;
685         as_noti_data.get_resolve_recipients.email_address   = email_address;
686     as_noti_data.get_resolve_recipients.multi_user_name = multi_user_name;
687
688         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT, &as_noti_data) == false) {
689                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
690                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
691                 goto FINISH_OFF;
692         }
693
694         if (handle)
695                 *handle = as_handle;
696
697 FINISH_OFF:
698
699     EM_SAFE_FREE(multi_user_name);
700         EM_DEBUG_API_END ("err[%d]", err);
701         return err;
702 }
703
704 EXPORT_API int email_free_certificate(email_certificate_t **certificate, int count)
705 {
706         EM_DEBUG_API_BEGIN ("certificate[%p] count[%d]", certificate, count);
707         int err = EMAIL_ERROR_NONE;
708         emcore_free_certificate(certificate, count, &err);
709         EM_DEBUG_API_END ("err[%d]", err);
710         return err;
711 }