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