Tizen 2.0 Release
[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 "email-api.h"
34 #include "string.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-ipc.h"
45
46 EXPORT_API int email_add_certificate(char *certificate_path, char *email_address)
47 {
48         EM_DEBUG_FUNC_BEGIN("Certificate path : [%s]", certificate_path);
49         int result_from_ipc = 0;
50         int err = EMAIL_ERROR_NONE;
51         
52         if (!certificate_path) {
53                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
54                 return EMAIL_ERROR_INVALID_PARAM;
55         }
56
57         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_CERTIFICATE);
58         if (hAPI == NULL) {
59                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
60                 err = EMAIL_ERROR_NULL_VALUE;
61                 goto FINISH_OFF;
62         }
63
64         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, certificate_path, EM_SAFE_STRLEN(certificate_path)+1)) {
65                 EM_DEBUG_EXCEPTION("emipc_add_parameter certificate_path[%s] failed", certificate_path);
66                 err = EMAIL_ERROR_NULL_VALUE;
67                 goto FINISH_OFF;
68         }
69
70         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
71                 EM_DEBUG_EXCEPTION("emipc_add_parameter certificate_path[%s] failed", email_address);
72                 err = EMAIL_ERROR_NULL_VALUE;
73                 goto FINISH_OFF;
74         }
75
76         if (emipc_execute_proxy_api(hAPI) < 0) {
77                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
78                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
79                 goto FINISH_OFF;
80         }
81
82         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
83         if (result_from_ipc != EMAIL_ERROR_NONE) {
84                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
85                 err = EMAIL_ERROR_IPC_CRASH;
86                 goto FINISH_OFF;
87         }
88
89 FINISH_OFF:
90
91         if (hAPI)
92                 emipc_destroy_email_api(hAPI);
93
94         EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
95         return err;
96 }
97
98 EXPORT_API int email_delete_certificate(char *email_address)
99 {
100         EM_DEBUG_FUNC_BEGIN("Eamil_address : [%s]", email_address);
101         int result_from_ipc = 0;
102         int err = EMAIL_ERROR_NONE;
103         
104         if (!email_address) {
105                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
106                 return EMAIL_ERROR_INVALID_PARAM;
107         }
108
109         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_CERTIFICATE);
110         if (hAPI == NULL) {
111                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
112                 err = EMAIL_ERROR_NULL_VALUE;
113                 goto FINISH_OFF;
114         }
115
116         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
117                 EM_DEBUG_EXCEPTION("emipc_add_parameter email_address[%s] failed", email_address);
118                 err = EMAIL_ERROR_NULL_VALUE;
119                 goto FINISH_OFF;
120         }
121
122         if (emipc_execute_proxy_api(hAPI) < 0) {
123                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
124                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
125                 goto FINISH_OFF;
126         }
127
128         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
129         if (result_from_ipc != EMAIL_ERROR_NONE) {
130                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
131                 err = EMAIL_ERROR_IPC_CRASH;
132                 goto FINISH_OFF;
133         }
134
135 FINISH_OFF:
136
137         if (hAPI)
138                 emipc_destroy_email_api(hAPI);
139
140         EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
141         return err;
142 }
143
144 EXPORT_API int email_get_certificate(char *email_address, email_certificate_t **certificate)
145 {
146         EM_DEBUG_FUNC_BEGIN();
147         int err = EMAIL_ERROR_NONE;
148         char temp_email_address[130] = {0, };
149         emstorage_certificate_tbl_t *cert = NULL;
150         
151         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
152         EM_IF_NULL_RETURN_VALUE(certificate, EMAIL_ERROR_INVALID_PARAM);
153         SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
154         
155         if (!emstorage_get_certificate_by_email_address(temp_email_address, &cert, false, 0, &err)) {
156                 EM_DEBUG_EXCEPTION("emstorage_get_certificate_by_index failed - %d", err);
157                 return err;
158         }
159
160         if (!em_convert_certificate_tbl_to_certificate(cert, certificate, &err)) {
161                 EM_DEBUG_EXCEPTION("em_convert_certificate_tbl_to_certificate failed");
162                 return err;
163         }       
164         
165         EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
166         return err;
167 }
168
169 EXPORT_API int email_get_decrypt_message(int mail_id, email_mail_data_t **output_mail_data, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
170 {
171         EM_DEBUG_FUNC_BEGIN("mail_id : [%d]", mail_id);
172         int err = EMAIL_ERROR_NONE;
173         int p_output_attachment_count = 0;
174         char *decrypt_filepath = NULL;
175         email_mail_data_t *p_output_mail_data = NULL;
176         email_attachment_data_t *p_output_attachment_data = NULL;
177         emstorage_account_tbl_t *p_account_tbl = NULL;
178
179         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
180
181         if (!output_mail_data || !output_attachment_data || !output_attachment_count) {
182                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
183                 err = EMAIL_ERROR_INVALID_PARAM;
184                 goto FINISH_OFF;
185         }
186
187         if ((err = emcore_get_mail_data(mail_id, &p_output_mail_data)) != EMAIL_ERROR_NONE) {
188                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
189                 goto FINISH_OFF;
190         }
191
192         if (!emstorage_get_account_by_id(p_output_mail_data->account_id, EMAIL_ACC_GET_OPT_OPTIONS, &p_account_tbl, false, &err)) {
193                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
194                 goto FINISH_OFF;
195         }
196
197         if ((err = emcore_get_attachment_data_list(mail_id, &p_output_attachment_data, &p_output_attachment_count)) != EMAIL_ERROR_NONE) {
198                 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed");
199                 goto FINISH_OFF;
200         }
201
202         if (p_output_attachment_count != 1 || !p_output_attachment_data) {
203                 EM_DEBUG_EXCEPTION("This is not the encrypted mail");
204                 err = EMAIL_ERROR_INVALID_PARAM;
205                 goto FINISH_OFF;
206         }
207
208         if (!emcore_smime_set_decrypt_message(p_output_attachment_data->attachment_path, p_account_tbl->certificate_path, &decrypt_filepath, &err)) {
209                 EM_DEBUG_EXCEPTION("emcore_smime_set_decrypt_message failed");
210                 goto FINISH_OFF;
211         }
212
213         /* Change decrpyt_message to mail_data_t */
214         if (!emcore_parse_mime_file_to_mail(decrypt_filepath, output_mail_data, output_attachment_data, output_attachment_count, &err)) {
215                 EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed : [%d]", err);
216                 goto FINISH_OFF;
217         }
218
219 FINISH_OFF:
220
221         if (p_account_tbl)
222                 emstorage_free_account(&p_account_tbl, 1, NULL);
223
224         if (p_output_mail_data)
225                 email_free_mail_data(&p_output_mail_data, 1);
226
227         if (p_output_attachment_data)
228                 email_free_attachment_data(&p_output_attachment_data, p_output_attachment_count);
229
230         EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
231         return err;
232 }
233
234 EXPORT_API int email_verify_signature(int mail_id, int *verify)
235 {
236         EM_DEBUG_FUNC_BEGIN("mail_id : [%d]", mail_id);
237         int result_from_ipc = 0;
238         int err = EMAIL_ERROR_NONE;
239         int p_verify = 0;
240
241         EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);    
242
243         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
244         if (hAPI == NULL) {
245                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
246                 err = EMAIL_ERROR_NULL_VALUE;
247                 goto FINISH_OFF;
248         }
249
250         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, &mail_id, sizeof(int))) {
251                 EM_DEBUG_EXCEPTION("emipc_add_parameter pass_phrase[%d] failed", mail_id);
252                 err = EMAIL_ERROR_NULL_VALUE;
253                 goto FINISH_OFF;
254         }
255
256         if (emipc_execute_proxy_api(hAPI) < 0) {
257                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
258                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
259                 goto FINISH_OFF;
260         }
261
262         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &p_verify);
263         if (result_from_ipc != EMAIL_ERROR_NONE) {
264                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
265                 err = EMAIL_ERROR_IPC_CRASH;
266                 goto FINISH_OFF;
267         }
268
269 FINISH_OFF:
270
271         if (hAPI)
272                 emipc_destroy_email_api(hAPI);
273
274         if (verify != NULL)
275                 *verify = p_verify;
276
277         EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
278         return err;
279 }
280
281 EXPORT_API int email_verify_certificate(char *certificate_path, int *verify)
282 {
283         EM_DEBUG_FUNC_BEGIN("certificate : [%s]", certificate_path);
284         int err = EMAIL_ERROR_NONE;
285         int result_from_ipc = 0;
286         int p_verify = 0;
287         
288         if (!certificate_path) {
289                 EM_DEBUG_EXCEPTION("Invalid parameter");
290                 return EMAIL_ERROR_INVALID_PARAM;
291         }
292
293         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_CERTIFICATE);
294         if (hAPI == NULL) {
295                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
296                 err = EMAIL_ERROR_NULL_VALUE;
297                 goto FINISH_OFF;
298         }
299
300         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, certificate_path, EM_SAFE_STRLEN(certificate_path)+1)) {
301                 EM_DEBUG_EXCEPTION("emipc_add_paramter failed : [%s]", certificate_path);
302                 err = EMAIL_ERROR_NULL_VALUE;
303                 goto FINISH_OFF;
304         }
305
306         if (emipc_execute_proxy_api(hAPI) < 0) {
307                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
308                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
309                 goto FINISH_OFF;
310         }
311
312         result_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &p_verify);
313         if (result_from_ipc != EMAIL_ERROR_NONE) {
314                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
315                 err = EMAIL_ERROR_IPC_CRASH;
316                 goto FINISH_OFF;
317         }
318
319 FINISH_OFF:
320
321         if (hAPI)       
322                 emipc_destroy_email_api(hAPI);
323
324         if (verify != NULL)
325                 *verify = p_verify;
326
327         EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
328         return err;
329 }
330
331 /*
332 EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url, unsigned *handle)
333 {
334         EM_DEBUG_FUNC_BEGIN("email_address : [%s], response_url : [%s], handle : [%p]", email_address, response_url, handle);
335
336         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
337
338         int err = EMAIL_ERROR_NONE;
339         HIPC_API hAPI = NULL;
340
341         hAPI = emipc_create_email_api(_EMAIL_API_CHECK_OCSP_STATUS);
342         
343         EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
344
345         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, email_address, EM_SAFE_STRLEN(email_address)+1)) {
346                 EM_DEBUG_EXCEPTION("email_check_ocsp_status--ADD Param email_address failed");
347                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
348         }
349
350         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, response_url, EM_SAFE_STRLEN(response_url)+1)) {
351                 EM_DEBUG_EXCEPTION("email_check_ocsp_status--ADD Param response_url failed");
352                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
353         }
354
355         if (emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
356                 EM_DEBUG_EXCEPTION("email_check_oscp_status--emipc_execute_proxy_api failed");
357                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
358         }
359
360         emipc_get_paramter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
361         if (err == EMAIL_ERROR_NONE) {
362                 if (handle)
363                         emipc_get_paramter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
364         }
365 }
366 */
367 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle)
368 {
369         EM_DEBUG_FUNC_BEGIN("account_id : [%d], email_address : [%s], handle : [%p]", account_id, email_address, handle);
370         
371         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
372         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
373
374         int err = EMAIL_ERROR_NONE;
375         int as_handle = 0;
376         email_account_server_t account_server_type;
377         ASNotiData as_noti_data;
378
379         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
380
381         if (em_get_account_server_type_by_account_id(account_id, &account_server_type, false, &err)     == false) {
382                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
383                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
384                 goto FINISH_OFF;
385         }
386
387         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
388                 EM_DEBUG_EXCEPTION("This api is not supported except of active sync");
389                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
390                 goto FINISH_OFF;
391         }
392
393         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
394                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync_failed[%d]", err);
395                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
396                 goto FINISH_OFF;                
397         }
398
399         as_noti_data.validate_certificate.handle = as_handle;
400         as_noti_data.validate_certificate.account_id = account_id;
401         as_noti_data.validate_certificate.email_address = strdup(email_address);
402
403         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE, &as_noti_data) == false) {
404                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
405                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
406                 goto FINISH_OFF;
407         }
408
409         if (handle)
410                 *handle = as_handle;
411
412 FINISH_OFF:
413
414         EM_DEBUG_FUNC_END("err [%d]", err);
415         return err;
416 }
417
418 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle)
419 {
420         EM_DEBUG_FUNC_BEGIN("account_id : [%d], email_address : [%s], handle : [%p]", account_id, email_address, handle);
421         
422         EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
423         EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
424
425         int err = EMAIL_ERROR_NONE;
426         int as_handle = 0;
427         email_account_server_t account_server_type;
428         ASNotiData as_noti_data;
429
430         memset(&as_noti_data, 0x00, sizeof(ASNotiData));
431
432         if (em_get_account_server_type_by_account_id(account_id, &account_server_type, false, &err)     == false) {
433                 EM_DEBUG_EXCEPTION("em_get_account_server_type_by_account_id failed[%d]", err);
434                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
435                 goto FINISH_OFF;
436         }
437
438         if (account_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
439                 EM_DEBUG_EXCEPTION("This api is not supported except of active sync");
440                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
441                 goto FINISH_OFF;
442         }
443
444         if (em_get_handle_for_activesync(&as_handle, &err) == false) {
445                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync_failed[%d]", err);
446                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
447                 goto FINISH_OFF;                
448         }
449
450         as_noti_data.get_resolve_recipients.handle = as_handle;
451         as_noti_data.get_resolve_recipients.account_id = account_id;
452         as_noti_data.get_resolve_recipients.email_address = strdup(email_address);
453
454         if (em_send_notification_to_active_sync_engine(ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT, &as_noti_data) == false) {
455                 EM_DEBUG_EXCEPTION("em_send_notification_to_active_sync_engine failed");
456                 err = EMAIL_ERROR_ACTIVE_SYNC_NOTI_FAILURE;
457                 goto FINISH_OFF;
458         }
459
460         if (handle)
461                 *handle = as_handle;
462
463 FINISH_OFF:
464
465         EM_DEBUG_FUNC_END("err [%d]", err);
466         return err;
467 }
468
469 EXPORT_API int email_free_certificate(email_certificate_t **certificate, int count)
470 {
471         EM_DEBUG_FUNC_BEGIN("certificate[%p], count[%d]", certificate, count);
472         int err = EMAIL_ERROR_NONE;
473         emcore_free_certificate(certificate, count, &err);
474         EM_DEBUG_FUNC_END("err [%d]", err);
475         return err;
476 }