fixed build warning
[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 #include "email-utilities.h"
34
35 /* Adding '/' method for system daemon */
36 /*
37 static char *add_shared_owner_prefix(const char *name)
38 {
39         EM_DEBUG_FUNC_BEGIN();
40         char *ckm_alias = NULL;
41
42         if (name == NULL) {
43                 EM_DEBUG_EXCEPTION("Invalid parameter");
44                 return NULL;
45         }
46
47         ckm_alias = g_strconcat(ckmc_owner_id_system, ckmc_owner_id_separator, name, NULL);
48
49         EM_DEBUG_FUNC_END();
50         return ckm_alias;
51 }
52 */
53
54 /* Add new data */
55 INTERNAL_FUNC int emcore_add_password_in_key_manager(char *data_name, char *stored_data)
56 {
57         EM_DEBUG_FUNC_BEGIN();
58         int err = EMAIL_ERROR_NONE;
59         int ckmc_ret = CKMC_ERROR_NONE;
60         char *alias = NULL;
61         ckmc_policy_s email_policy;
62         ckmc_raw_buffer_s email_data;
63         char errno_buf[ERRNO_BUF_SIZE] = {0};
64
65         if (data_name == NULL || stored_data == NULL) {
66                 EM_DEBUG_EXCEPTION("Invalid parameter");
67                 err = EMAIL_ERROR_INVALID_PARAM;
68                 return err;
69         }
70
71 //      alias = add_shared_owner_prefix(data_name);
72         alias = g_strdup(data_name);
73         EM_DEBUG_LOG("alias : [%s]", alias);
74
75         email_policy.password = NULL;
76         email_policy.extractable = true;
77
78 //      EM_DEBUG_LOG("stored_data : [%s]", stored_data);
79
80         email_data.data = (unsigned char *)stored_data;
81         email_data.size = strlen(stored_data);
82
83         ckmc_ret = ckmc_save_data(alias, email_data, email_policy);
84         if (ckmc_ret != CKMC_ERROR_NONE) {
85                 EM_DEBUG_EXCEPTION("ckmc_save_data failed [%d][%s]", errno, EM_STRERROR(errno_buf));
86                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
87                 goto FINISH_OFF;
88         }
89
90 FINISH_OFF:
91
92         EM_SAFE_FREE(alias);
93
94         EM_DEBUG_FUNC_END();
95         return err;
96 }
97
98 /* Get the stored data */
99 INTERNAL_FUNC int emcore_get_password_in_key_manager(char *data_name, char **stored_data)
100 {
101         EM_DEBUG_FUNC_BEGIN();
102         int err = EMAIL_ERROR_NONE;
103         int ckmc_ret = CKMC_ERROR_NONE;
104         char *alias = NULL;
105         ckmc_raw_buffer_s *email_data = NULL;
106
107         if (data_name == NULL) {
108                 EM_DEBUG_EXCEPTION("Invalid parameter");
109                 err = EMAIL_ERROR_INVALID_PARAM;
110                 return err;
111         }
112
113 //      alias = add_shared_owner_prefix(data_name);
114         alias = g_strdup(data_name);
115         EM_DEBUG_LOG("alias : [%s]", alias);
116
117         ckmc_ret = ckmc_get_data(alias, NULL, &email_data);
118         if (ckmc_ret != CKMC_ERROR_NONE) {
119                 EM_DEBUG_EXCEPTION("ckmc_get_data failed : [%d]", ckmc_ret);
120                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
121                 goto FINISH_OFF;
122         }
123
124         EM_DEBUG_LOG_DEV("stored_data : [%s]", email_data->data);
125         EM_DEBUG_LOG_DEV("stored_data length : [%d]", email_data->size);
126
127 FINISH_OFF:
128
129         if (stored_data) {
130                 if (email_data) *stored_data = g_strndup((const gchar *)email_data->data, email_data->size);
131         }
132
133         if (email_data)
134                 ckmc_buffer_free(email_data);
135
136         EM_SAFE_FREE(alias);
137
138         EM_DEBUG_FUNC_END();
139         return err;
140 }
141
142 /* Remove the stored data */
143 INTERNAL_FUNC int emcore_remove_password_in_key_manager(char *data_name)
144 {
145         EM_DEBUG_FUNC_BEGIN();
146         int err = EMAIL_ERROR_NONE;
147         int ckmc_ret = CKMC_ERROR_NONE;
148         char *alias = NULL;
149
150         if (data_name == NULL) {
151                 EM_DEBUG_EXCEPTION("Invalid parameter");
152                 err = EMAIL_ERROR_INVALID_PARAM;
153                 return err;
154         }
155
156 //      alias = add_shared_owner_prefix(data_name);
157         alias = g_strdup(data_name);
158         EM_DEBUG_LOG("alias : [%s]", alias);
159
160         ckmc_ret = ckmc_remove_alias(alias);
161         if (ckmc_ret != CKMC_ERROR_NONE) {
162                 EM_DEBUG_EXCEPTION("ckmc_remove_alias failed : [%d]", ckmc_ret);
163                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
164                 goto FINISH_OFF;
165         }
166
167 FINISH_OFF:
168
169         EM_SAFE_FREE(alias);
170         EM_DEBUG_FUNC_END();
171         return err;
172 }
173
174 INTERNAL_FUNC int emcore_get_certificate_in_key_manager(char *alias, char *password,
175                                                                                                                 const unsigned char **cert_data,
176                                                                                                                 int *cert_size)
177 {
178         EM_DEBUG_FUNC_BEGIN();
179         int err = EMAIL_ERROR_NONE;
180
181         if (alias == NULL) {
182                 EM_DEBUG_EXCEPTION("Invalid parameter");
183                 err = EMAIL_ERROR_INVALID_PARAM;
184                 return err;
185         }
186
187         int ckmc_ret = CKMC_ERROR_NONE;
188         unsigned char *p_cert_data = NULL;
189         ckmc_cert_s *output_cert = NULL;
190
191         ckmc_ret = ckmc_get_cert(alias, password, &output_cert);
192         if (ckmc_ret != CKMC_ERROR_NONE) {
193                 EM_DEBUG_EXCEPTION("ckmc_get_cert failed : [%d]", ckmc_ret);
194                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
195                 goto FINISH_OFF;
196         }
197
198         EM_DEBUG_LOG("Cert size : [%d]", output_cert->cert_size);
199         EM_DEBUG_LOG("Cert format : [%d]", output_cert->data_format);
200         EM_DEBUG_LOG_DEV("Cert string : [%s]", output_cert->raw_cert);
201
202         p_cert_data = em_malloc(output_cert->cert_size + 1);
203         if (p_cert_data == NULL) {
204                 EM_DEBUG_EXCEPTION("em_mallocfailed");
205                 err = EMAIL_ERROR_OUT_OF_MEMORY;
206                 goto FINISH_OFF;
207         }
208
209         memcpy(p_cert_data, output_cert->raw_cert, output_cert->cert_size);
210
211         *cert_data = p_cert_data;
212         *cert_size = output_cert->cert_size;
213
214 FINISH_OFF:
215
216         if (output_cert)
217                 ckmc_cert_free(output_cert);
218
219         if (err != EMAIL_ERROR_NONE) {
220                 EM_SAFE_FREE(p_cert_data);
221         }
222
223         EM_DEBUG_FUNC_END();
224         return err;
225 }