Modified the key path
[platform/core/messaging/email-service.git] / email-core / email-core-key-manager.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: 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  * email-core-task-manager.c
24  *
25  *  Created on: 2015. 7. 24.
26  *      Author: sh0701.kwon@samsung.com
27  */
28
29 #include <ckmc/ckmc-manager.h>
30
31 #include "email-core-utils.h"
32 #include "email-debug-log.h"
33
34 /* Adding '/' method for system daemon */
35 static char *add_shared_owner_prefix(const char *name)
36 {
37         EM_DEBUG_FUNC_BEGIN();
38         char *ckm_alias = NULL;
39
40         if (name == NULL) {
41                 EM_DEBUG_EXCEPTION("Invalid parameter");
42                 return NULL;
43         }
44
45         ckm_alias = g_strconcat(ckmc_label_shared_owner, ckmc_label_name_separator, name, NULL);
46
47         EM_DEBUG_FUNC_END();
48         return ckm_alias;
49 }
50
51 /* Add new data */
52 INTERNAL_FUNC int emcore_add_password_in_key_manager(char *data_name, char *stored_data)
53 {
54         EM_DEBUG_FUNC_BEGIN();
55         int err = EMAIL_ERROR_NONE;
56         int ckmc_ret = CKMC_ERROR_NONE;
57         char *alias = NULL;
58         ckmc_policy_s email_policy;
59         ckmc_raw_buffer_s email_data;
60         char errno_buf[ERRNO_BUF_SIZE] = {0};
61
62         if (data_name == NULL) {
63                 EM_DEBUG_EXCEPTION("Invalid parameter");
64                 err = EMAIL_ERROR_INVALID_PARAM;
65                 return err;
66         }
67         
68 //      alias = add_shared_owner_prefix(data_name);
69         alias = g_strdup(data_name);
70         EM_DEBUG_LOG("alias : [%s]", alias);
71
72         email_policy.password = NULL;
73         email_policy.extractable = true;
74
75         EM_DEBUG_LOG("stored_data : [%s]", stored_data);
76
77         email_data.data = (unsigned char *)stored_data;
78         email_data.size = strlen(stored_data);
79
80         ckmc_ret = ckmc_save_data(alias, email_data, email_policy);
81         if (ckmc_ret != CKMC_ERROR_NONE) {
82                 EM_DEBUG_EXCEPTION("ckmc_save_data failed [%d][%s]", errno, EM_STRERROR(errno_buf));
83                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
84                 goto FINISH_OFF;
85         }
86
87 FINISH_OFF:
88
89         EM_SAFE_FREE(alias);
90
91         EM_DEBUG_FUNC_END();
92         return err;
93 }
94
95 /* Get the stored data */
96 INTERNAL_FUNC int emcore_get_password_in_key_manager(char *data_name, char **stored_data)
97 {
98         EM_DEBUG_FUNC_BEGIN();
99         int err = EMAIL_ERROR_NONE;
100         int ckmc_ret = CKMC_ERROR_NONE;
101         char *alias = NULL;
102         ckmc_raw_buffer_s *email_data = NULL;
103
104         if (data_name == NULL) {
105                 EM_DEBUG_EXCEPTION("Invalid parameter");
106                 err = EMAIL_ERROR_INVALID_PARAM;
107                 return err;
108         }
109
110 //      alias = add_shared_owner_prefix(data_name); 
111         alias = g_strdup(data_name);
112         EM_DEBUG_LOG("alias : [%s]", alias);
113
114         ckmc_ret = ckmc_get_data(alias, NULL, &email_data);
115         if (ckmc_ret != CKMC_ERROR_NONE) {
116                 EM_DEBUG_EXCEPTION("ckmc_get_data failed : [%d]", ckmc_ret);
117                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
118                 goto FINISH_OFF;
119         }
120
121         EM_DEBUG_LOG("stored_data : [%s]", email_data->data);
122
123 FINISH_OFF:
124
125         if (stored_data)
126                 *stored_data = EM_SAFE_STRDUP(email_data->data);
127
128         if (email_data)
129                 ckmc_buffer_free(email_data);
130
131         EM_SAFE_FREE(alias);
132
133         EM_DEBUG_FUNC_END();
134         return err;
135 }
136
137 /* Remove the stored data */
138 INTERNAL_FUNC int emcore_remove_password_in_key_manager(char *data_name)
139 {
140         EM_DEBUG_FUNC_BEGIN();
141         int err = EMAIL_ERROR_NONE;
142         int ckmc_ret = CKMC_ERROR_NONE;
143         char *alias = NULL;
144
145         if (data_name == NULL) {
146                 EM_DEBUG_EXCEPTION("Invalid parameter");
147                 err = EMAIL_ERROR_INVALID_PARAM;
148                 return err;
149         }
150
151 //      alias = add_shared_owner_prefix(data_name);  
152         alias = g_strdup(data_name);
153         EM_DEBUG_LOG("alias : [%s]", alias);
154
155         ckmc_ret = ckmc_remove_alias(alias);
156         if (ckmc_ret != CKMC_ERROR_NONE) {
157                 EM_DEBUG_EXCEPTION("ckmc_remove_alias failed : [%d]", ckmc_ret);
158                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
159                 goto FINISH_OFF;
160         }
161
162 FINISH_OFF:
163
164         EM_SAFE_FREE(alias);
165         EM_DEBUG_FUNC_END();
166         return err;
167 }