Coverity issue fixes for email service
[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 #include <ckmc/ckmc-type.h>
31 #include <SecCryptoSvc.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36
37 #include "email-core-utils.h"
38 #include "email-debug-log.h"
39 #include "email-utilities.h"
40
41
42 #define EMAIL_SERVICE_CS_DERIVE_PASS "email_service_cs_derive_pass"
43
44 /* Adding '/' method for system daemon */
45 /*
46 static char *add_shared_owner_prefix(const char *name)
47 {
48         EM_DEBUG_FUNC_BEGIN();
49         char *ckm_alias = NULL;
50
51         if (name == NULL) {
52                 EM_DEBUG_EXCEPTION("Invalid parameter");
53                 return NULL;
54         }
55
56         ckm_alias = g_strconcat(ckmc_owner_id_system, ckmc_owner_id_separator, name, NULL);
57
58         EM_DEBUG_FUNC_END();
59         return ckm_alias;
60 }
61 */
62 static char *__get_key_manager_alias(const char* name)
63 {
64         int ret = 0;
65         char *ckmc_alias = NULL;
66
67         ret = ckmc_alias_new("/User", name, &ckmc_alias);
68         if (ret != CKMC_ERROR_NONE) {
69                 EM_DEBUG_LOG("ckmc_alias_new failed");
70                 return NULL;
71
72         }
73
74         EM_DEBUG_LOG("alias : [%s]", ckmc_alias);
75         return ckmc_alias;
76
77 }
78 /* Add new data */
79 INTERNAL_FUNC int emcore_add_password_in_key_manager(char *data_name, char *stored_data)
80 {
81         EM_DEBUG_FUNC_BEGIN();
82         int err = EMAIL_ERROR_NONE;
83         int ckmc_ret = CKMC_ERROR_NONE;
84         char *alias = NULL;
85         ckmc_policy_s email_policy;
86         ckmc_raw_buffer_s email_data;
87         char errno_buf[ERRNO_BUF_SIZE] = {0};
88
89         if (data_name == NULL || stored_data == NULL) {
90                 EM_DEBUG_EXCEPTION("Invalid parameter");
91                 err = EMAIL_ERROR_INVALID_PARAM;
92                 return err;
93         }
94
95         alias = __get_key_manager_alias(data_name);
96         EM_DEBUG_LOG("alias : [%s]", alias);
97
98         email_policy.password = NULL;
99         email_policy.extractable = true;
100
101 //      EM_DEBUG_LOG("stored_data : [%s]", stored_data);
102
103         email_data.data = (unsigned char *)stored_data;
104         email_data.size = strlen(stored_data);
105
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;
110                 goto FINISH_OFF;
111         }
112
113 FINISH_OFF:
114
115         EM_SAFE_FREE(alias);
116
117         EM_DEBUG_FUNC_END();
118         return err;
119 }
120
121 /* Get the stored data */
122 INTERNAL_FUNC int emcore_get_password_in_key_manager(char *data_name, char **stored_data)
123 {
124         EM_DEBUG_FUNC_BEGIN();
125         int err = EMAIL_ERROR_NONE;
126         int ckmc_ret = CKMC_ERROR_NONE;
127         char *alias = NULL;
128         ckmc_raw_buffer_s *email_data = NULL;
129
130         if (data_name == NULL) {
131                 EM_DEBUG_EXCEPTION("Invalid parameter");
132                 err = EMAIL_ERROR_INVALID_PARAM;
133                 return err;
134         }
135
136         alias = __get_key_manager_alias(data_name);
137         EM_DEBUG_LOG("alias : [%s]", alias);
138
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;
143                 goto FINISH_OFF;
144         }
145
146         EM_DEBUG_LOG_DEV("stored_data : [%s]", email_data->data);
147         EM_DEBUG_LOG_DEV("stored_data length : [%d]", email_data->size);
148
149 FINISH_OFF:
150
151         if (stored_data) {
152                 if (email_data) *stored_data = g_strndup((const gchar *)email_data->data, email_data->size);
153         }
154
155         if (email_data)
156                 ckmc_buffer_free(email_data);
157
158         EM_SAFE_FREE(alias);
159
160         EM_DEBUG_FUNC_END();
161         return err;
162 }
163
164 /* Remove the stored data */
165 INTERNAL_FUNC int emcore_remove_password_in_key_manager(char *data_name)
166 {
167         EM_DEBUG_FUNC_BEGIN();
168         int err = EMAIL_ERROR_NONE;
169         int ckmc_ret = CKMC_ERROR_NONE;
170         char *alias = NULL;
171
172         if (data_name == NULL) {
173                 EM_DEBUG_EXCEPTION("Invalid parameter");
174                 err = EMAIL_ERROR_INVALID_PARAM;
175                 return err;
176         }
177
178         alias = __get_key_manager_alias(data_name);
179         EM_DEBUG_LOG("alias : [%s]", alias);
180
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);
184
185                 err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
186                 goto FINISH_OFF;
187         }
188
189 FINISH_OFF:
190
191         EM_SAFE_FREE(alias);
192         EM_DEBUG_FUNC_END();
193         return err;
194 }
195
196 INTERNAL_FUNC int emcore_get_certificate_in_key_manager(char *alias, char *password,
197                                                                                                                 const unsigned char **cert_data,
198                                                                                                                 int *cert_size)
199 {
200         EM_DEBUG_FUNC_BEGIN();
201         int err = EMAIL_ERROR_NONE;
202
203         if (alias == NULL) {
204                 EM_DEBUG_EXCEPTION("Invalid parameter");
205                 err = EMAIL_ERROR_INVALID_PARAM;
206                 return err;
207         }
208
209         int ckmc_ret = CKMC_ERROR_NONE;
210         unsigned char *p_cert_data = NULL;
211         ckmc_cert_s *output_cert = NULL;
212
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;
217                 goto FINISH_OFF;
218         }
219
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);
223
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;
228                 goto FINISH_OFF;
229         }
230
231         memcpy(p_cert_data, output_cert->raw_cert, output_cert->cert_size);
232
233         *cert_data = p_cert_data;
234         *cert_size = output_cert->cert_size;
235
236 FINISH_OFF:
237
238         if (output_cert)
239                 ckmc_cert_free(output_cert);
240
241         if (err != EMAIL_ERROR_NONE)
242                 EM_SAFE_FREE(p_cert_data);
243
244         EM_DEBUG_FUNC_END();
245         return err;
246 }
247
248 /* check key in key manager */
249 INTERNAL_FUNC int emcore_check_key_in_key_manager(const char *data_name)
250 {
251         EM_DEBUG_FUNC_BEGIN();
252         int err = EMAIL_ERROR_NONE;
253         int ckmc_ret = CKMC_ERROR_NONE;
254         char *alias = NULL;
255         ckmc_key_s *email_key = NULL;
256
257         if (data_name == NULL) {
258                 EM_DEBUG_EXCEPTION("Invalid parameter");
259                 err = EMAIL_ERROR_INVALID_PARAM;
260                 return err;
261         }
262
263         alias = __get_key_manager_alias(data_name);
264
265         if (alias == NULL) {
266                 EM_DEBUG_EXCEPTION("Invalid parameter");
267                 err = EMAIL_ERROR_INVALID_PARAM;
268                 return err;
269         }
270
271
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;
276                 goto FINISH_OFF;
277         }
278
279
280 FINISH_OFF:
281         if (email_key != NULL)
282                 ckmc_key_free(email_key);
283
284         EM_SAFE_FREE(alias);
285
286         EM_DEBUG_FUNC_END();
287         return err;
288 }
289
290
291
292 /* save key in key manager */
293 INTERNAL_FUNC int emcore_save_key_in_key_manager(const char *data_name)
294 {
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;
300
301         const char *pass = EMAIL_SERVICE_CS_DERIVE_PASS;
302         unsigned char *pass_key = NULL;
303         char *alias = NULL;
304
305         if (data_name == NULL) {
306                 EM_DEBUG_EXCEPTION("Invalid parameter");
307                 err = EMAIL_ERROR_INVALID_PARAM;
308                 return err;
309         }
310
311
312         int cs_ret = cs_derive_key_with_pass(pass, strlen(pass), 32, &pass_key);
313
314         if (cs_ret != CS_ERROR_NONE) {
315                 EM_DEBUG_LOG("creat key failed");
316                 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
317                 goto FINISH_OFF;
318         }
319
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;
324
325         email_policy.password = NULL;
326         email_policy.extractable = false;
327
328         alias = __get_key_manager_alias(data_name);
329
330         ckmc_ret = ckmc_save_key(alias, email_key, email_policy);
331
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;
335                 goto FINISH_OFF;
336         }
337
338
339 FINISH_OFF:
340
341
342         EM_SAFE_FREE(alias);
343         EM_DEBUG_FUNC_END();
344         return err;
345 }
346
347
348
349 /* encrypte data */
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)
351 {
352         EM_DEBUG_FUNC_BEGIN();
353         char *alias = NULL;
354         int err = EMAIL_ERROR_NONE;
355         int ckmc_ret = CKMC_ERROR_NONE;
356
357         ckmc_raw_buffer_s plaintext;
358         ckmc_raw_buffer_s *encrypted = NULL;
359         ckmc_param_list_h params = NULL;
360
361         unsigned char iv[16] = {0, };
362         ckmc_raw_buffer_s iv_buffer;
363         iv_buffer.data = iv;
364         iv_buffer.size = sizeof(iv);
365
366
367         plaintext.data = (unsigned char *)input_data;
368         plaintext.size = input_length;
369
370         if (data_name == NULL) {
371                 EM_DEBUG_EXCEPTION("Invalid parameter");
372                 err = EMAIL_ERROR_INVALID_PARAM;
373                 return err;
374         }
375
376
377         ckmc_ret = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, &params);
378         if (ckmc_ret != CKMC_ERROR_NONE) {
379                 EM_DEBUG_LOG("ckmc_generate_new_params failed");
380                 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
381                 return err;
382         }
383
384
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;
389                 goto FINISH_OFF;
390         }
391
392
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;
398                 goto FINISH_OFF;
399         }
400
401         *output_data = encrypted->data;
402         *output_length = encrypted->size;
403
404         if (encrypted->data != NULL)
405                 encrypted->data = NULL;
406
407 FINISH_OFF:
408
409         if (encrypted != NULL)
410                 ckmc_buffer_free(encrypted);
411
412         if (params != NULL)
413                 ckmc_param_list_free(params);
414
415         EM_SAFE_FREE(alias);
416         EM_DEBUG_FUNC_END();
417         return err;
418 }
419
420
421
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)
424 {
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;
432
433         char *alias = NULL;
434         char errno_buf[ERRNO_BUF_SIZE] = {0};
435         unsigned char iv[16] = {0, };
436         ckmc_raw_buffer_s iv_buffer;
437         iv_buffer.data = iv;
438         iv_buffer.size = sizeof(iv);
439
440         if (data_name == NULL && file_path == NULL) {
441                 EM_DEBUG_EXCEPTION("Invalid parameter");
442                 err = EMAIL_ERROR_INVALID_PARAM;
443                 return err;
444         }
445
446
447         int fd = 0;
448         struct stat st_buf;
449
450         if (stat(file_path, &st_buf) < 0) {
451                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", file_path);
452                 err = EMAIL_ERROR_SYSTEM_FAILURE;
453                 goto FINISH_OFF;
454         }
455
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);
459                 goto FINISH_OFF;
460         }
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));
464
465         if (!encrypted) {
466                 EM_DEBUG_EXCEPTION("malloc failed...");
467                 err = EMAIL_ERROR_OUT_OF_MEMORY;
468                 goto FINISH_OFF;
469         }
470
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;
474                 goto FINISH_OFF;
475         }
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;
479                 goto FINISH_OFF;
480         }
481         encrypted->size = buffer_length;
482
483
484         ckmc_ret = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, &params);
485         if (ckmc_ret != CKMC_ERROR_NONE) {
486
487                 EM_DEBUG_LOG("ckmc_generate_new_params failed");
488                 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
489                 goto FINISH_OFF;
490
491         }
492
493
494         ckmc_ret = ckmc_param_list_set_buffer(params, CKMC_PARAM_ED_IV, &iv_buffer);
495         if (ckmc_ret != CKMC_ERROR_NONE) {
496
497                 EM_DEBUG_LOG("ckmc_param_list_set_buffer failed");
498                 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
499                 goto FINISH_OFF;
500
501         }
502
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) {
506
507                 EM_DEBUG_LOG("ckmc_encrypt_data failed");
508                 err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
509                 goto FINISH_OFF;
510
511         }
512
513         *output_data = (char *)decrypted->data;
514         *output_length = decrypted->size;
515
516         if (decrypted->data != NULL)
517                 decrypted->data = NULL;
518
519
520
521
522
523 FINISH_OFF:
524
525         if (encrypted != NULL)
526                 ckmc_buffer_free(encrypted);
527
528
529         if (decrypted != NULL)
530                 ckmc_buffer_free(decrypted);
531
532         if (params != NULL)
533                 ckmc_param_list_free(params);
534
535
536         EM_SAFE_CLOSE(fd);
537         EM_SAFE_FREE(alias);
538         EM_DEBUG_FUNC_END();
539         return err;
540 }
541
542