4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
33 #include "email-api.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"
44 EXPORT_API int email_add_certificate(char *certificate_path, char *email_address)
46 EM_DEBUG_FUNC_BEGIN("Certificate path : [%s]", certificate_path);
47 int result_from_ipc = 0;
48 int err = EMAIL_ERROR_NONE;
50 if (!certificate_path) {
51 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
52 return EMAIL_ERROR_INVALID_PARAM;
55 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_CERTIFICATE);
57 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
58 err = EMAIL_ERROR_NULL_VALUE;
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;
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;
74 if (emipc_execute_proxy_api(hAPI) < 0) {
75 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
76 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
90 emipc_destroy_email_api(hAPI);
92 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
96 EXPORT_API int email_delete_certificate(char *email_address)
98 EM_DEBUG_FUNC_BEGIN("Eamil_address : [%s]", email_address);
99 int result_from_ipc = 0;
100 int err = EMAIL_ERROR_NONE;
102 if (!email_address) {
103 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
104 return EMAIL_ERROR_INVALID_PARAM;
107 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_CERTIFICATE);
109 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
110 err = EMAIL_ERROR_NULL_VALUE;
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;
120 if (emipc_execute_proxy_api(hAPI) < 0) {
121 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
122 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
136 emipc_destroy_email_api(hAPI);
138 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
142 EXPORT_API int email_get_certificate(char *email_address, email_certificate_t **certificate)
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;
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);
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);
158 if (!em_convert_certificate_tbl_to_certificate(cert, certificate, &err)) {
159 EM_DEBUG_EXCEPTION("em_convert_certificate_tbl_to_certificate failed");
163 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
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)
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;
174 EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
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;
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");
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");
191 emcore_smime_set_decrypt_message(p_output_mail_data->file_path_mime_entity, p_account->certificate_path, decrypt_message, &err);
193 /* Change decrpyt_message to mail_data_t */
198 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
202 EXPORT_API int email_verify_signature(char *certificate_path, int mail_id, int *verify)
204 EM_DEBUG_FUNC_BEGIN("Certificate path : [%s]", certificate_path);
205 int result_from_ipc = 0;
206 int err = EMAIL_ERROR_NONE;
209 if (!certificate_path) {
210 EM_DEBUG_EXCEPTION("Invalid parameter");
211 return EMAIL_ERROR_INVALID_PARAM;
214 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
216 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
217 err = EMAIL_ERROR_NULL_VALUE;
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;
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;
233 if (emipc_execute_proxy_api(hAPI) < 0) {
234 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
235 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
249 emipc_destroy_email_api(hAPI);
254 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
258 EXPORT_API int email_verify_certificate(char *certificate_path, int *verify)
260 EM_DEBUG_FUNC_BEGIN("certificate : [%s]", certificate_path);
261 int err = EMAIL_ERROR_NONE;
262 int result_from_ipc = 0;
265 if (!certificate_path) {
266 EM_DEBUG_EXCEPTION("Invalid parameter");
267 return EMAIL_ERROR_INVALID_PARAM;
270 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_CERTIFICATE);
272 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
273 err = EMAIL_ERROR_NULL_VALUE;
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;
283 if (emipc_execute_proxy_api(hAPI) < 0) {
284 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
285 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
299 emipc_destroy_email_api(hAPI);
304 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
309 EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url, unsigned *handle)
311 EM_DEBUG_FUNC_BEGIN("email_address : [%s], response_url : [%s], handle : [%p]", email_address, response_url, handle);
313 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
315 int err = EMAIL_ERROR_NONE;
316 HIPC_API hAPI = NULL;
318 hAPI = emipc_create_email_api(_EMAIL_API_CHECK_OCSP_STATUS);
320 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
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);
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);
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);
337 emipc_get_paramter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
338 if (err == EMAIL_ERROR_NONE) {
340 emipc_get_paramter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
344 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle)
346 EM_DEBUG_FUNC_BEGIN("account_id : [%d], email_address : [%s], handle : [%p]", account_id, email_address, handle);
348 EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
349 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
351 int err = EMAIL_ERROR_NONE;
353 email_account_server_t account_server_type;
354 ASNotiData as_noti_data;
356 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
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;
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;
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;
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);
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;
391 EM_DEBUG_FUNC_END("err [%d]", err);
395 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle)
397 EM_DEBUG_FUNC_BEGIN("account_id : [%d], email_address : [%s], handle : [%p]", account_id, email_address, handle);
399 EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
400 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
402 int err = EMAIL_ERROR_NONE;
404 email_account_server_t account_server_type;
405 ASNotiData as_noti_data;
407 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
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;
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;
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;
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);
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;
442 EM_DEBUG_FUNC_END("err [%d]", err);
446 EXPORT_API int email_free_certificate(email_certificate_t **certificate, int count)
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);