Removed the certificate api
[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_get_decrypt_message(int mail_id, email_mail_data_t **output_mail_data, 
49                                                                                 email_attachment_data_t **output_attachment_data, 
50                                                                                 int *output_attachment_count, int *verify)
51 {
52         EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
53         int err = EMAIL_ERROR_NONE;
54         int p_output_attachment_count = 0;
55     int i = 0;
56         char *decrypt_filepath = NULL;
57     char *search = NULL;
58     char *multi_user_name = NULL;
59         email_mail_data_t *p_output_mail_data = NULL;
60         email_attachment_data_t *p_output_attachment_data = NULL;
61         emstorage_account_tbl_t *p_account_tbl = NULL;
62
63         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
64
65         if (!output_mail_data || !output_attachment_data || !output_attachment_count) {
66                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
67                 err = EMAIL_ERROR_INVALID_PARAM;
68                 goto FINISH_OFF;
69         }
70
71     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
72         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]");
73         goto FINISH_OFF;
74     }
75
76         if ((err = emcore_get_mail_data(multi_user_name, mail_id, &p_output_mail_data)) != EMAIL_ERROR_NONE) {
77                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
78                 goto FINISH_OFF;
79         }
80
81         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)) {
82                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
83                 goto FINISH_OFF;
84         }
85
86         if ((err = emcore_get_attachment_data_list(multi_user_name, mail_id, &p_output_attachment_data, &p_output_attachment_count)) != EMAIL_ERROR_NONE) {
87                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed");
88                 goto FINISH_OFF;
89         }
90
91         for (i = 0; i < p_output_attachment_count; i++) {
92                 EM_DEBUG_LOG("mime_type : [%s]", p_output_attachment_data[i].attachment_mime_type);
93                 if (p_output_attachment_data[i].attachment_mime_type && (search = strcasestr(p_output_attachment_data[i].attachment_mime_type, "PKCS7-MIME"))) {
94                         EM_DEBUG_LOG("Found the encrypt file");
95                         break;
96                 } else if (p_output_attachment_data[i].attachment_mime_type && (search = strcasestr(p_output_attachment_data[i].attachment_mime_type, "octet-stream"))) {
97                         EM_DEBUG_LOG("Found the encrypt file");
98                         break;
99                 }
100         }
101
102         if (!search) {
103                 EM_DEBUG_EXCEPTION("No have a decrypt file");
104                 err = EMAIL_ERROR_INVALID_PARAM;
105                 goto FINISH_OFF;
106         }
107
108         if (p_output_mail_data->smime_type == EMAIL_SMIME_ENCRYPTED || p_output_mail_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
109                 emcore_init_openssl_library();
110                 if (!emcore_smime_get_decrypt_message(p_output_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
111                         EM_DEBUG_EXCEPTION("emcore_smime_get_decrypt_message failed");
112                         emcore_clean_openssl_library();
113                         goto FINISH_OFF;
114                 }
115                 emcore_clean_openssl_library();
116         } else if (p_output_mail_data->smime_type == EMAIL_PGP_ENCRYPTED) {
117                 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) {
118                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
119                         goto FINISH_OFF;
120                 }
121         } else if (p_output_mail_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
122                 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) {
123                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
124                         goto FINISH_OFF;
125                 }
126         } else {
127                 EM_DEBUG_LOG("Invalid encrypted mail");
128                 err = EMAIL_ERROR_INVALID_PARAM;
129                 goto FINISH_OFF;
130         }
131
132         /* Change decrpyt_message to mail_data_t */
133         if (!emcore_parse_mime_file_to_mail(decrypt_filepath, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
134                 EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed : [%d]", err);
135                 goto FINISH_OFF;
136         }
137
138         (*output_mail_data)->subject                 = EM_SAFE_STRDUP(p_output_mail_data->subject);
139         (*output_mail_data)->date_time               = p_output_mail_data->date_time;
140         (*output_mail_data)->full_address_return     = EM_SAFE_STRDUP(p_output_mail_data->full_address_return);
141         (*output_mail_data)->email_address_recipient = EM_SAFE_STRDUP(p_output_mail_data->email_address_recipient);
142         (*output_mail_data)->email_address_sender    = EM_SAFE_STRDUP(p_output_mail_data->email_address_sender);
143         (*output_mail_data)->full_address_reply      = EM_SAFE_STRDUP(p_output_mail_data->full_address_reply);
144         (*output_mail_data)->full_address_from       = EM_SAFE_STRDUP(p_output_mail_data->full_address_from);
145         (*output_mail_data)->full_address_to         = EM_SAFE_STRDUP(p_output_mail_data->full_address_to);
146         (*output_mail_data)->full_address_cc         = EM_SAFE_STRDUP(p_output_mail_data->full_address_cc);
147         (*output_mail_data)->full_address_bcc        = EM_SAFE_STRDUP(p_output_mail_data->full_address_bcc);
148         (*output_mail_data)->flags_flagged_field     = p_output_mail_data->flags_flagged_field;
149
150 FINISH_OFF:
151
152     EM_SAFE_FREE(decrypt_filepath);
153
154         if (p_account_tbl)
155                 emstorage_free_account(&p_account_tbl, 1, NULL);
156
157         if (p_output_mail_data)
158                 email_free_mail_data(&p_output_mail_data, 1);
159
160         if (p_output_attachment_data)
161                 email_free_attachment_data(&p_output_attachment_data, p_output_attachment_count);
162
163     EM_SAFE_FREE(multi_user_name);
164
165         EM_DEBUG_API_END ("err[%d]", err);
166         return err;
167 }
168
169 EXPORT_API int email_get_decrypt_message_ex(email_mail_data_t *input_mail_data, 
170                                                                                         email_attachment_data_t *input_attachment_data, 
171                                                                                         int input_attachment_count,
172                                             email_mail_data_t **output_mail_data, 
173                                                                                         email_attachment_data_t **output_attachment_data, 
174                                                                                         int *output_attachment_count,
175                                                                                         int *verify)
176 {
177         EM_DEBUG_API_BEGIN ();
178         int err = EMAIL_ERROR_NONE;
179     int i = 0;
180         char *decrypt_filepath = NULL;
181     char *search = NULL;
182     char *multi_user_name = NULL;
183         emstorage_account_tbl_t *p_account_tbl = NULL;
184
185         EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
186
187         if (!output_mail_data || !output_attachment_data || !output_attachment_count) {
188                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
189                 err = EMAIL_ERROR_INVALID_PARAM;
190                 goto FINISH_OFF;
191         }
192
193     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
194         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
195         goto FINISH_OFF;
196     }
197
198         if (!emstorage_get_account_by_id(multi_user_name, input_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
199                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
200                 goto FINISH_OFF;
201         }
202
203         for (i = 0; i < input_attachment_count; i++) {
204                 EM_DEBUG_LOG("mime_type : [%s]", input_attachment_data[i].attachment_mime_type);
205                 if (input_attachment_data[i].attachment_mime_type && (search = strcasestr(input_attachment_data[i].attachment_mime_type, "PKCS7-MIME"))) {
206                         EM_DEBUG_LOG("Found the encrypt file");
207                         break;
208                 } else if (input_attachment_data[i].attachment_mime_type && (search = strcasestr(input_attachment_data[i].attachment_mime_type, "octet-stream"))) {
209                         EM_DEBUG_LOG("Found the encrypt file");
210                         break;
211                 }
212         }
213
214         if (!search) {
215                 EM_DEBUG_EXCEPTION("No have a decrypt file");
216                 err = EMAIL_ERROR_INVALID_PARAM;
217                 goto FINISH_OFF;
218         }
219
220         if (input_mail_data->smime_type == EMAIL_SMIME_ENCRYPTED || input_mail_data->smime_type == EMAIL_SMIME_SIGNED_AND_ENCRYPTED) {
221                 emcore_init_openssl_library();
222                 if (!emcore_smime_get_decrypt_message(input_attachment_data[i].attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
223                         EM_DEBUG_EXCEPTION("emcore_smime_get_decrypt_message failed");
224                         emcore_clean_openssl_library();
225                         goto FINISH_OFF;
226                 }
227                 emcore_clean_openssl_library();
228         } else if (input_mail_data->smime_type == EMAIL_PGP_ENCRYPTED) {
229                 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) {
230                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
231                         goto FINISH_OFF;
232                 }
233         } else if (input_mail_data->smime_type == EMAIL_PGP_SIGNED_AND_ENCRYPTED) {
234                 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) {
235                         EM_DEBUG_EXCEPTION("emcore_pgp_get_decrypted_message failed : [%d]", err);
236                         goto FINISH_OFF;
237                 }
238         } else {
239                 EM_DEBUG_LOG("Invalid encrypted mail");
240                 err = EMAIL_ERROR_INVALID_PARAM;
241                 goto FINISH_OFF;
242         }
243
244         /* Change decrpyt_message to mail_data_t */
245         if (!emcore_parse_mime_file_to_mail(decrypt_filepath, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
246                 EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed : [%d]", err);
247                 goto FINISH_OFF;
248         }
249
250         (*output_mail_data)->subject                 = EM_SAFE_STRDUP(input_mail_data->subject);
251         (*output_mail_data)->date_time               = input_mail_data->date_time;
252         (*output_mail_data)->full_address_return     = EM_SAFE_STRDUP(input_mail_data->full_address_return);
253         (*output_mail_data)->email_address_recipient = EM_SAFE_STRDUP(input_mail_data->email_address_recipient);
254         (*output_mail_data)->email_address_sender    = EM_SAFE_STRDUP(input_mail_data->email_address_sender);
255         (*output_mail_data)->full_address_reply      = EM_SAFE_STRDUP(input_mail_data->full_address_reply);
256         (*output_mail_data)->full_address_from       = EM_SAFE_STRDUP(input_mail_data->full_address_from);
257         (*output_mail_data)->full_address_to         = EM_SAFE_STRDUP(input_mail_data->full_address_to);
258         (*output_mail_data)->full_address_cc         = EM_SAFE_STRDUP(input_mail_data->full_address_cc);
259         (*output_mail_data)->full_address_bcc        = EM_SAFE_STRDUP(input_mail_data->full_address_bcc);
260         (*output_mail_data)->flags_flagged_field     = input_mail_data->flags_flagged_field;
261
262 FINISH_OFF:
263
264     EM_SAFE_FREE(decrypt_filepath);
265     EM_SAFE_FREE(multi_user_name);
266
267         if (p_account_tbl)
268                 emstorage_free_account(&p_account_tbl, 1, NULL);
269
270         EM_DEBUG_API_END ("err[%d]", err);
271         return err;
272 }
273
274 EXPORT_API int email_verify_signature(int mail_id, int *verify)
275 {
276         EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
277
278         if (mail_id <= 0) {
279                 EM_DEBUG_EXCEPTION("Invalid parameter");
280                 return EMAIL_ERROR_INVALID_PARAM;
281         }
282
283         int result_from_ipc = 0;
284         int p_verify = 0;
285         int err = EMAIL_ERROR_NONE;
286
287         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
288         if (hAPI == NULL) {
289                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
290                 err = EMAIL_ERROR_NULL_VALUE;
291                 goto FINISH_OFF;
292         }
293
294         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &mail_id, sizeof(int))) {
295                 EM_DEBUG_EXCEPTION("emipc_add_parameter pass_phrase[%d] failed", mail_id);
296                 err = EMAIL_ERROR_NULL_VALUE;
297                 goto FINISH_OFF;
298         }
299
300         if (emipc_execute_proxy_api(hAPI) < 0) {
301                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
302                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
303                 goto FINISH_OFF;
304         }
305
306         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &p_verify);
307         if (result_from_ipc != EMAIL_ERROR_NONE) {
308                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
309                 err = EMAIL_ERROR_IPC_CRASH;
310                 goto FINISH_OFF;
311         }
312
313 FINISH_OFF:
314
315         if (hAPI)
316                 emipc_destroy_email_api(hAPI);
317
318         if (verify != NULL)
319                 *verify = p_verify;
320
321         EM_DEBUG_API_END ("err[%d]", err);
322         return err;
323 }
324
325 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)
326 {
327         EM_DEBUG_API_BEGIN ();
328
329         if (!input_mail_data || !input_attachment_data || input_attachment_count <= 0) {
330                 EM_DEBUG_EXCEPTION("Invalid parameter");
331                 return EMAIL_ERROR_INVALID_PARAM;
332         }
333
334         int count = 0;
335         int err = EMAIL_ERROR_NONE;
336
337         for (count = 0; count < input_attachment_count ; count++) {
338                 if (input_attachment_data[count].attachment_mime_type && strcasestr(input_attachment_data[count].attachment_mime_type, "SIGNATURE"))
339                         break;
340         }
341
342         if (input_mail_data->smime_type == EMAIL_SMIME_SIGNED) {
343                 emcore_init_openssl_library();
344                 if (!emcore_verify_signature(input_attachment_data[count].attachment_path, input_mail_data->file_path_mime_entity, verify, &err)) 
345                         EM_DEBUG_EXCEPTION("emcore_verify_signature failed : [%d]", err);
346
347                 emcore_clean_openssl_library();
348         } else if(input_mail_data->smime_type == EMAIL_PGP_SIGNED) {
349                 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)
350                         EM_DEBUG_EXCEPTION("emcore_pgp_get_verify_siganture failed : [%d]", err);
351         } else {
352                 EM_DEBUG_LOG("Invalid signed mail : mime_type[%d]", input_mail_data->smime_type);
353                 err = EMAIL_ERROR_INVALID_PARAM;
354         }
355
356
357         EM_DEBUG_API_END ("err[%d]", err);
358         return err;
359 }
360
361 /*
362 EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url, unsigned *handle)
363 {
364         EM_DEBUG_FUNC_BEGIN_SEC("email_address : [%s], response_url : [%s], handle : [%p]", email_address, response_url, handle);
365
366         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
367
368         int err = EMAIL_ERROR_NONE;
369         HIPC_API hAPI = NULL;
370
371         hAPI = emipc_create_email_api(_EMAIL_API_CHECK_OCSP_STATUS);
372         
373         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
374
375         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
376                 EM_DEBUG_EXCEPTION("email_check_ocsp_status--ADD Param email_address failed");
377                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
378         }
379
380         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, response_url, EM_SAFE_STRLEN(response_url)+1)) {
381                 EM_DEBUG_EXCEPTION("email_check_ocsp_status--ADD Param response_url failed");
382                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
383         }
384
385         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
386                 EM_DEBUG_EXCEPTION("email_check_oscp_status--emipc_execute_proxy_api failed");
387                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
388         }
389
390         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
391         if (err == EMAIL_ERROR_NONE) {
392                 if (handle)
393                         emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
394         }
395 }
396 */
397 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle)
398 {
399         EM_DEBUG_API_BEGIN ("account_id[%d]", account_id);
400         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d] email_address[%s] handle[%p]", account_id, email_address, handle);
401         
402         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
403         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
404
405         int err = EMAIL_ERROR_NONE;
406         int as_handle = 0;
407     char *multi_user_name = NULL;
408         email_account_server_t account_server_type;
409         ASNotiData as_noti_data;
410
411     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
412         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
413         goto FINISH_OFF;
414     }
415
416         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
417
418         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false) {
419                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
420                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
421                 goto FINISH_OFF;
422         }
423
424         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
425                 EM_DEBUG_EXCEPTION("This api is not supported except of active sync");
426                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
427                 goto FINISH_OFF;
428         }
429
430         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
431                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync_failed[%d]", err);
432                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
433                 goto FINISH_OFF;                
434         }
435
436         as_noti_data.validate_certificate.handle = as_handle;
437         as_noti_data.validate_certificate.account_id = account_id;
438         as_noti_data.validate_certificate.email_address = email_address;
439     as_noti_data.validate_certificate.multi_user_name = multi_user_name;
440
441         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE, &as_noti_data) == false) {
442                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
443                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
444                 goto FINISH_OFF;
445         }
446
447         if (handle)
448                 *handle = as_handle;
449
450 FINISH_OFF:
451
452     EM_SAFE_FREE(multi_user_name);
453         EM_DEBUG_API_END ("err[%d]", err);
454         return err;
455 }
456
457 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle)
458 {
459         EM_DEBUG_API_BEGIN ("account_id[%d]", account_id);
460         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d] email_address[%s] handle[%p]", account_id, email_address, handle);
461         
462         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
463         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
464
465         int err = EMAIL_ERROR_NONE;
466         int as_handle = 0;
467     char *multi_user_name = NULL;
468         email_account_server_t account_server_type;
469         ASNotiData as_noti_data;
470
471     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
472         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
473         goto FINISH_OFF;
474     }
475
476         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
477
478         if (em_get_account_server_type_by_account_id(multi_user_name, account_id, &account_server_type, false, &err) == false) {
479                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
480                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
481                 goto FINISH_OFF;
482         }
483
484         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
485                 EM_DEBUG_EXCEPTION("This api is not supported except of active sync");
486                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
487                 goto FINISH_OFF;
488         }
489
490         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
491                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync_failed[%d]", err);
492                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
493                 goto FINISH_OFF;                
494         }
495
496         as_noti_data.get_resolve_recipients.handle          = as_handle;
497         as_noti_data.get_resolve_recipients.account_id      = account_id;
498         as_noti_data.get_resolve_recipients.email_address   = email_address;
499     as_noti_data.get_resolve_recipients.multi_user_name = multi_user_name;
500
501         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT, &as_noti_data) == false) {
502                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
503                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
504                 goto FINISH_OFF;
505         }
506
507         if (handle)
508                 *handle = as_handle;
509
510 FINISH_OFF:
511
512     EM_SAFE_FREE(multi_user_name);
513         EM_DEBUG_API_END ("err[%d]", err);
514         return err;
515 }