4 * Copyright (c) 2012 - 2013 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-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"
46 EXPORT_API int email_add_certificate(char *certificate_path, char *email_address)
48 EM_DEBUG_FUNC_BEGIN("Certificate path : [%s]", certificate_path);
49 int result_from_ipc = 0;
50 int err = EMAIL_ERROR_NONE;
52 if (!certificate_path) {
53 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
54 return EMAIL_ERROR_INVALID_PARAM;
57 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_ADD_CERTIFICATE);
59 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
60 err = EMAIL_ERROR_NULL_VALUE;
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;
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;
76 if (emipc_execute_proxy_api(hAPI) < 0) {
77 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
78 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
92 emipc_destroy_email_api(hAPI);
94 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
98 EXPORT_API int email_delete_certificate(char *email_address)
100 EM_DEBUG_FUNC_BEGIN("Eamil_address : [%s]", email_address);
101 int result_from_ipc = 0;
102 int err = EMAIL_ERROR_NONE;
104 if (!email_address) {
105 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
106 return EMAIL_ERROR_INVALID_PARAM;
109 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_DELETE_CERTIFICATE);
111 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
112 err = EMAIL_ERROR_NULL_VALUE;
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;
122 if (emipc_execute_proxy_api(hAPI) < 0) {
123 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
124 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
138 emipc_destroy_email_api(hAPI);
140 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
144 EXPORT_API int email_get_certificate(char *email_address, email_certificate_t **certificate)
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;
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);
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);
160 if (!em_convert_certificate_tbl_to_certificate(cert, certificate, &err)) {
161 EM_DEBUG_EXCEPTION("em_convert_certificate_tbl_to_certificate failed");
165 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
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)
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;
179 EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
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;
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");
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);
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");
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;
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");
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);
222 emstorage_free_account(&p_account_tbl, 1, NULL);
224 if (p_output_mail_data)
225 email_free_mail_data(&p_output_mail_data, 1);
227 if (p_output_attachment_data)
228 email_free_attachment_data(&p_output_attachment_data, p_output_attachment_count);
230 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
234 EXPORT_API int email_verify_signature(int mail_id, int *verify)
236 EM_DEBUG_FUNC_BEGIN("mail_id : [%d]", mail_id);
237 int result_from_ipc = 0;
238 int err = EMAIL_ERROR_NONE;
241 EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
243 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
245 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
246 err = EMAIL_ERROR_NULL_VALUE;
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;
256 if (emipc_execute_proxy_api(hAPI) < 0) {
257 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
258 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
272 emipc_destroy_email_api(hAPI);
277 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
281 EXPORT_API int email_verify_certificate(char *certificate_path, int *verify)
283 EM_DEBUG_FUNC_BEGIN("certificate : [%s]", certificate_path);
284 int err = EMAIL_ERROR_NONE;
285 int result_from_ipc = 0;
288 if (!certificate_path) {
289 EM_DEBUG_EXCEPTION("Invalid parameter");
290 return EMAIL_ERROR_INVALID_PARAM;
293 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_CERTIFICATE);
295 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
296 err = EMAIL_ERROR_NULL_VALUE;
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;
306 if (emipc_execute_proxy_api(hAPI) < 0) {
307 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
308 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
322 emipc_destroy_email_api(hAPI);
327 EM_DEBUG_FUNC_END("ERROR CODE [%d]", err);
332 EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url, unsigned *handle)
334 EM_DEBUG_FUNC_BEGIN("email_address : [%s], response_url : [%s], handle : [%p]", email_address, response_url, handle);
336 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
338 int err = EMAIL_ERROR_NONE;
339 HIPC_API hAPI = NULL;
341 hAPI = emipc_create_email_api(_EMAIL_API_CHECK_OCSP_STATUS);
343 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
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);
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);
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);
360 emipc_get_paramter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
361 if (err == EMAIL_ERROR_NONE) {
363 emipc_get_paramter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
367 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle)
369 EM_DEBUG_FUNC_BEGIN("account_id : [%d], email_address : [%s], handle : [%p]", account_id, email_address, handle);
371 EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
372 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
374 int err = EMAIL_ERROR_NONE;
376 email_account_server_t account_server_type;
377 ASNotiData as_noti_data;
379 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
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;
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;
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;
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);
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;
414 EM_DEBUG_FUNC_END("err [%d]", err);
418 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle)
420 EM_DEBUG_FUNC_BEGIN("account_id : [%d], email_address : [%s], handle : [%p]", account_id, email_address, handle);
422 EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
423 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
425 int err = EMAIL_ERROR_NONE;
427 email_account_server_t account_server_type;
428 ASNotiData as_noti_data;
430 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
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;
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;
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;
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);
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;
465 EM_DEBUG_FUNC_END("err [%d]", err);
469 EXPORT_API int email_free_certificate(email_certificate_t **certificate, int count)
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);