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
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"
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)
52 EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
53 int err = EMAIL_ERROR_NONE;
54 int p_output_attachment_count = 0;
56 char *decrypt_filepath = 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;
63 EM_IF_NULL_RETURN_VALUE(mail_id, EMAIL_ERROR_INVALID_PARAM);
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;
71 if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
72 EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]");
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");
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);
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");
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");
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");
103 EM_DEBUG_EXCEPTION("No have a decrypt file");
104 err = EMAIL_ERROR_INVALID_PARAM;
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();
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);
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);
127 EM_DEBUG_LOG("Invalid encrypted mail");
128 err = EMAIL_ERROR_INVALID_PARAM;
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);
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;
152 EM_SAFE_FREE(decrypt_filepath);
155 emstorage_free_account(&p_account_tbl, 1, NULL);
157 if (p_output_mail_data)
158 email_free_mail_data(&p_output_mail_data, 1);
160 if (p_output_attachment_data)
161 email_free_attachment_data(&p_output_attachment_data, p_output_attachment_count);
163 EM_SAFE_FREE(multi_user_name);
165 EM_DEBUG_API_END ("err[%d]", err);
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,
177 EM_DEBUG_API_BEGIN ();
178 int err = EMAIL_ERROR_NONE;
180 char *decrypt_filepath = NULL;
182 char *multi_user_name = NULL;
183 emstorage_account_tbl_t *p_account_tbl = NULL;
185 EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
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;
193 if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
194 EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
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);
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");
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");
215 EM_DEBUG_EXCEPTION("No have a decrypt file");
216 err = EMAIL_ERROR_INVALID_PARAM;
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();
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);
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);
239 EM_DEBUG_LOG("Invalid encrypted mail");
240 err = EMAIL_ERROR_INVALID_PARAM;
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);
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;
264 EM_SAFE_FREE(decrypt_filepath);
265 EM_SAFE_FREE(multi_user_name);
268 emstorage_free_account(&p_account_tbl, 1, NULL);
270 EM_DEBUG_API_END ("err[%d]", err);
274 EXPORT_API int email_verify_signature(int mail_id, int *verify)
276 EM_DEBUG_API_BEGIN ("mail_id[%d]", mail_id);
279 EM_DEBUG_EXCEPTION("Invalid parameter");
280 return EMAIL_ERROR_INVALID_PARAM;
283 int result_from_ipc = 0;
285 int err = EMAIL_ERROR_NONE;
287 HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_VERIFY_SIGNATURE);
289 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
290 err = EMAIL_ERROR_NULL_VALUE;
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;
300 if (emipc_execute_proxy_api(hAPI) < 0) {
301 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
302 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
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;
316 emipc_destroy_email_api(hAPI);
321 EM_DEBUG_API_END ("err[%d]", err);
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)
327 EM_DEBUG_API_BEGIN ();
329 if (!input_mail_data || !input_attachment_data || input_attachment_count <= 0) {
330 EM_DEBUG_EXCEPTION("Invalid parameter");
331 return EMAIL_ERROR_INVALID_PARAM;
335 int err = EMAIL_ERROR_NONE;
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"))
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);
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);
352 EM_DEBUG_LOG("Invalid signed mail : mime_type[%d]", input_mail_data->smime_type);
353 err = EMAIL_ERROR_INVALID_PARAM;
357 EM_DEBUG_API_END ("err[%d]", err);
362 EXPORT_API int email_check_ocsp_status(char *email_address, char *response_url, unsigned *handle)
364 EM_DEBUG_FUNC_BEGIN_SEC("email_address : [%s], response_url : [%s], handle : [%p]", email_address, response_url, handle);
366 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
368 int err = EMAIL_ERROR_NONE;
369 HIPC_API hAPI = NULL;
371 hAPI = emipc_create_email_api(_EMAIL_API_CHECK_OCSP_STATUS);
373 EM_IF_NULL_RETURN_VALUE(hAPI, EMAIL_ERROR_NULL_VALUE);
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);
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);
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);
390 emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
391 if (err == EMAIL_ERROR_NONE) {
393 emipc_get_parameter(hAPI, ePARAMETER_OUT, 1, sizeof(int), handle);
397 EXPORT_API int email_validate_certificate(int account_id, char *email_address, unsigned *handle)
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);
402 EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
403 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
405 int err = EMAIL_ERROR_NONE;
407 char *multi_user_name = NULL;
408 email_account_server_t account_server_type;
409 ASNotiData as_noti_data;
411 if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
412 EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
416 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
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;
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;
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;
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;
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;
452 EM_SAFE_FREE(multi_user_name);
453 EM_DEBUG_API_END ("err[%d]", err);
457 EXPORT_API int email_get_resolve_recipients(int account_id, char *email_address, unsigned *handle)
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);
462 EM_IF_NULL_RETURN_VALUE(account_id, EMAIL_ERROR_INVALID_PARAM);
463 EM_IF_NULL_RETURN_VALUE(email_address, EMAIL_ERROR_INVALID_PARAM);
465 int err = EMAIL_ERROR_NONE;
467 char *multi_user_name = NULL;
468 email_account_server_t account_server_type;
469 ASNotiData as_noti_data;
471 if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
472 EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
476 memset(&as_noti_data, 0x00, sizeof(ASNotiData));
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;
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;
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;
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;
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;
512 EM_SAFE_FREE(multi_user_name);
513 EM_DEBUG_API_END ("err[%d]", err);