4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: 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.
23 * email-core-task-manager.c
25 * Created on: 2015. 7. 24.
26 * Author: sh0701.kwon@samsung.com
29 #include <ckmc/ckmc-manager.h>
30 #include <ckmc/ckmc-type.h>
31 #include <SecCryptoSvc.h>
32 #include <sys/types.h>
37 #include "email-core-utils.h"
38 #include "email-debug-log.h"
39 #include "email-utilities.h"
42 #define EMAIL_SERVICE_CS_DERIVE_PASS "email_service_cs_derive_pass"
44 /* Adding '/' method for system daemon */
46 static char *add_shared_owner_prefix(const char *name)
48 EM_DEBUG_FUNC_BEGIN();
49 char *ckm_alias = NULL;
52 EM_DEBUG_EXCEPTION("Invalid parameter");
56 ckm_alias = g_strconcat(ckmc_owner_id_system, ckmc_owner_id_separator, name, NULL);
62 static char *__get_key_manager_alias(const char* name)
65 char *ckmc_alias = NULL;
67 ret = ckmc_alias_new("/User", name, &ckmc_alias);
68 if (ret != CKMC_ERROR_NONE) {
69 EM_DEBUG_LOG("ckmc_alias_new failed");
74 EM_DEBUG_LOG("alias : [%s]", ckmc_alias);
79 INTERNAL_FUNC int emcore_add_password_in_key_manager(char *data_name, char *stored_data)
81 EM_DEBUG_FUNC_BEGIN();
82 int err = EMAIL_ERROR_NONE;
83 int ckmc_ret = CKMC_ERROR_NONE;
85 ckmc_policy_s email_policy;
86 ckmc_raw_buffer_s email_data;
87 char errno_buf[ERRNO_BUF_SIZE] = {0};
89 if (data_name == NULL || stored_data == NULL) {
90 EM_DEBUG_EXCEPTION("Invalid parameter");
91 err = EMAIL_ERROR_INVALID_PARAM;
95 alias = __get_key_manager_alias(data_name);
96 EM_DEBUG_LOG("alias : [%s]", alias);
98 email_policy.password = NULL;
99 email_policy.extractable = true;
101 // EM_DEBUG_LOG("stored_data : [%s]", stored_data);
103 email_data.data = (unsigned char *)stored_data;
104 email_data.size = strlen(stored_data);
106 ckmc_ret = ckmc_save_data(alias, email_data, email_policy);
107 if (ckmc_ret != CKMC_ERROR_NONE) {
108 EM_DEBUG_EXCEPTION("ckmc_save_data failed [%d][%s]", errno, EM_STRERROR(errno_buf));
109 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
121 /* Get the stored data */
122 INTERNAL_FUNC int emcore_get_password_in_key_manager(char *data_name, char **stored_data)
124 EM_DEBUG_FUNC_BEGIN();
125 int err = EMAIL_ERROR_NONE;
126 int ckmc_ret = CKMC_ERROR_NONE;
128 ckmc_raw_buffer_s *email_data = NULL;
130 if (data_name == NULL) {
131 EM_DEBUG_EXCEPTION("Invalid parameter");
132 err = EMAIL_ERROR_INVALID_PARAM;
136 alias = __get_key_manager_alias(data_name);
137 EM_DEBUG_LOG("alias : [%s]", alias);
139 ckmc_ret = ckmc_get_data(alias, NULL, &email_data);
140 if (ckmc_ret != CKMC_ERROR_NONE) {
141 EM_DEBUG_EXCEPTION("ckmc_get_data failed : [%d]", ckmc_ret);
142 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
146 EM_DEBUG_LOG_DEV("stored_data : [%s]", email_data->data);
147 EM_DEBUG_LOG_DEV("stored_data length : [%d]", email_data->size);
152 if (email_data) *stored_data = g_strndup((const gchar *)email_data->data, email_data->size);
156 ckmc_buffer_free(email_data);
164 /* Remove the stored data */
165 INTERNAL_FUNC int emcore_remove_password_in_key_manager(char *data_name)
167 EM_DEBUG_FUNC_BEGIN();
168 int err = EMAIL_ERROR_NONE;
169 int ckmc_ret = CKMC_ERROR_NONE;
172 if (data_name == NULL) {
173 EM_DEBUG_EXCEPTION("Invalid parameter");
174 err = EMAIL_ERROR_INVALID_PARAM;
178 alias = __get_key_manager_alias(data_name);
179 EM_DEBUG_LOG("alias : [%s]", alias);
181 ckmc_ret = ckmc_remove_alias(alias);
182 if (ckmc_ret != CKMC_ERROR_NONE) {
183 EM_DEBUG_EXCEPTION("ckmc_remove_alias failed : [%d]", ckmc_ret);
185 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
196 INTERNAL_FUNC int emcore_get_certificate_in_key_manager(char *alias, char *password,
197 const unsigned char **cert_data,
200 EM_DEBUG_FUNC_BEGIN();
201 int err = EMAIL_ERROR_NONE;
204 EM_DEBUG_EXCEPTION("Invalid parameter");
205 err = EMAIL_ERROR_INVALID_PARAM;
209 int ckmc_ret = CKMC_ERROR_NONE;
210 unsigned char *p_cert_data = NULL;
211 ckmc_cert_s *output_cert = NULL;
213 ckmc_ret = ckmc_get_cert(alias, password, &output_cert);
214 if (ckmc_ret != CKMC_ERROR_NONE) {
215 EM_DEBUG_EXCEPTION("ckmc_get_cert failed : [%d]", ckmc_ret);
216 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
220 EM_DEBUG_LOG("Cert size : [%zu]", output_cert->cert_size);
221 EM_DEBUG_LOG("Cert format : [%d]", output_cert->data_format);
222 EM_DEBUG_LOG_DEV("Cert string : [%s]", output_cert->raw_cert);
224 p_cert_data = em_malloc(output_cert->cert_size + 1);
225 if (p_cert_data == NULL) {
226 EM_DEBUG_EXCEPTION("em_mallocfailed");
227 err = EMAIL_ERROR_OUT_OF_MEMORY;
231 memcpy(p_cert_data, output_cert->raw_cert, output_cert->cert_size);
233 *cert_data = p_cert_data;
234 *cert_size = output_cert->cert_size;
239 ckmc_cert_free(output_cert);
241 if (err != EMAIL_ERROR_NONE)
242 EM_SAFE_FREE(p_cert_data);
248 /* check key in key manager */
249 INTERNAL_FUNC int emcore_check_key_in_key_manager(const char *data_name)
251 EM_DEBUG_FUNC_BEGIN();
252 int err = EMAIL_ERROR_NONE;
253 int ckmc_ret = CKMC_ERROR_NONE;
255 ckmc_key_s *email_key = NULL;
257 if (data_name == NULL) {
258 EM_DEBUG_EXCEPTION("Invalid parameter");
259 err = EMAIL_ERROR_INVALID_PARAM;
263 alias = __get_key_manager_alias(data_name);
266 EM_DEBUG_EXCEPTION("Invalid parameter");
267 err = EMAIL_ERROR_INVALID_PARAM;
272 ckmc_ret = ckmc_get_key(alias, NULL, &email_key);
273 if (ckmc_ret == CKMC_ERROR_DB_ALIAS_UNKNOWN) {
274 EM_DEBUG_EXCEPTION("email error key not found");
275 err = EMAIL_ERROR_KEY_NOT_FOUND;
281 if (email_key != NULL)
282 ckmc_key_free(email_key);
292 /* save key in key manager */
293 INTERNAL_FUNC int emcore_save_key_in_key_manager(const char *data_name)
295 EM_DEBUG_FUNC_BEGIN();
296 int err = EMAIL_ERROR_NONE;
297 int ckmc_ret = CKMC_ERROR_NONE;
298 ckmc_key_s email_key;
299 ckmc_policy_s email_policy;
301 const char *pass = EMAIL_SERVICE_CS_DERIVE_PASS;
302 unsigned char *pass_key = NULL;
305 if (data_name == NULL) {
306 EM_DEBUG_EXCEPTION("Invalid parameter");
307 err = EMAIL_ERROR_INVALID_PARAM;
312 int cs_ret = cs_derive_key_with_pass(pass, strlen(pass), 32, &pass_key);
314 if (cs_ret != CS_ERROR_NONE) {
315 EM_DEBUG_LOG("creat key failed");
316 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
320 email_key.raw_key = (unsigned char *)pass_key;
321 email_key.key_size = 32;
322 email_key.key_type = CKMC_KEY_AES;
323 email_key.password = NULL;
325 email_policy.password = NULL;
326 email_policy.extractable = false;
328 alias = __get_key_manager_alias(data_name);
330 ckmc_ret = ckmc_save_key(alias, email_key, email_policy);
332 if (ckmc_ret != CKMC_ERROR_NONE) {
333 EM_DEBUG_EXCEPTION("email error key not save : %s", get_error_message(ckmc_ret));
334 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
350 INTERNAL_FUNC int emcore_encryption_data_in_key_manager(const char *data_name, char *input_data, int input_length, unsigned char **output_data, int *output_length)
352 EM_DEBUG_FUNC_BEGIN();
354 int err = EMAIL_ERROR_NONE;
355 int ckmc_ret = CKMC_ERROR_NONE;
357 ckmc_raw_buffer_s plaintext;
358 ckmc_raw_buffer_s *encrypted = NULL;
359 ckmc_param_list_h params = NULL;
361 unsigned char iv[16] = {0, };
362 ckmc_raw_buffer_s iv_buffer;
364 iv_buffer.size = sizeof(iv);
367 plaintext.data = (unsigned char *)input_data;
368 plaintext.size = input_length;
370 if (data_name == NULL) {
371 EM_DEBUG_EXCEPTION("Invalid parameter");
372 err = EMAIL_ERROR_INVALID_PARAM;
377 ckmc_ret = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, ¶ms);
378 if (ckmc_ret != CKMC_ERROR_NONE) {
379 EM_DEBUG_LOG("ckmc_generate_new_params failed");
380 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
385 ckmc_ret = ckmc_param_list_set_buffer(params, CKMC_PARAM_ED_IV, &iv_buffer);
386 if (ckmc_ret != CKMC_ERROR_NONE) {
387 EM_DEBUG_LOG("ckmc_param_list_set_buffer failed");
388 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
393 alias = __get_key_manager_alias(data_name);
394 ckmc_ret = ckmc_encrypt_data(params, alias, NULL, plaintext, &encrypted);
395 if (ckmc_ret != CKMC_ERROR_NONE) {
396 EM_DEBUG_LOG("ckmc_encrypt_data failed");
397 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
401 *output_data = encrypted->data;
402 *output_length = encrypted->size;
404 if (encrypted->data != NULL)
405 encrypted->data = NULL;
409 if (encrypted != NULL)
410 ckmc_buffer_free(encrypted);
413 ckmc_param_list_free(params);
422 /* decrypte data from file */
423 INTERNAL_FUNC int emcore_decryption_data_from_file_in_key_manager(const char *data_name, const char *file_path, char **output_data, int *output_length)
425 EM_DEBUG_FUNC_BEGIN();
426 int err = EMAIL_ERROR_NONE;
427 int ckmc_ret = CKMC_ERROR_NONE;
428 int buffer_length = 0;
429 ckmc_raw_buffer_s *encrypted = NULL;
430 ckmc_raw_buffer_s *decrypted = NULL;
431 ckmc_param_list_h params = NULL;
434 char errno_buf[ERRNO_BUF_SIZE] = {0};
435 unsigned char iv[16] = {0, };
436 ckmc_raw_buffer_s iv_buffer;
438 iv_buffer.size = sizeof(iv);
440 if (data_name == NULL && file_path == NULL) {
441 EM_DEBUG_EXCEPTION("Invalid parameter");
442 err = EMAIL_ERROR_INVALID_PARAM;
450 if (stat(file_path, &st_buf) < 0) {
451 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", file_path);
452 err = EMAIL_ERROR_SYSTEM_FAILURE;
456 err = em_open(file_path, O_RDONLY, 0, &fd);
457 if (err != EMAIL_ERROR_NONE) {
458 EM_DEBUG_EXCEPTION("em_open failed : [%d] [%s]", err, file_path);
461 buffer_length = st_buf.st_size;
462 EM_DEBUG_LOG("account buffer_length[%d]", buffer_length);
463 encrypted = (ckmc_raw_buffer_s*)malloc(sizeof(ckmc_raw_buffer_s));
466 EM_DEBUG_EXCEPTION("malloc failed...");
467 err = EMAIL_ERROR_OUT_OF_MEMORY;
471 if ((encrypted->data = (unsigned char *)em_malloc(buffer_length + 1)) == NULL) {
472 EM_DEBUG_EXCEPTION("em_malloc failed...");
473 err = EMAIL_ERROR_OUT_OF_MEMORY;
476 if (read(fd, encrypted->data, buffer_length) < 0) {
477 EM_DEBUG_EXCEPTION("read failed : [%s]", EM_STRERROR(errno_buf));
478 err = EMAIL_ERROR_SYSTEM_FAILURE;
481 encrypted->size = buffer_length;
484 ckmc_ret = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, ¶ms);
485 if (ckmc_ret != CKMC_ERROR_NONE) {
487 EM_DEBUG_LOG("ckmc_generate_new_params failed");
488 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
494 ckmc_ret = ckmc_param_list_set_buffer(params, CKMC_PARAM_ED_IV, &iv_buffer);
495 if (ckmc_ret != CKMC_ERROR_NONE) {
497 EM_DEBUG_LOG("ckmc_param_list_set_buffer failed");
498 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
503 alias = __get_key_manager_alias(data_name);
504 ckmc_ret = ckmc_decrypt_data(params, alias, NULL, *encrypted, &decrypted);
505 if (ckmc_ret != CKMC_ERROR_NONE) {
507 EM_DEBUG_LOG("ckmc_encrypt_data failed");
508 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
513 *output_data = (char *)decrypted->data;
514 *output_length = decrypted->size;
516 if (decrypted->data != NULL)
517 decrypted->data = NULL;
525 if (encrypted != NULL)
526 ckmc_buffer_free(encrypted);
529 if (decrypted != NULL)
530 ckmc_buffer_free(decrypted);
533 ckmc_param_list_free(params);