summaryrefslogtreecommitdiff
path: root/email-core/email-core-key-manager.c
blob: 17fcb21d3ff0579df49b3b3a7cf40dbad7442796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
*  email-service
*
* Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Sunghyun Kwon <sh0701.kwon@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/*
 * email-core-task-manager.c
 *
 *  Created on: 2015. 7. 24.
 *      Author: sh0701.kwon@samsung.com
 */

#include <ckmc/ckmc-manager.h>
#include <ckmc/ckmc-type.h>
#include <SecCryptoSvc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include "email-core-utils.h"
#include "email-debug-log.h"
#include "email-utilities.h"


#define EMAIL_SERVICE_CS_DERIVE_PASS "email_service_cs_derive_pass"

/* Adding '/' method for system daemon */
/*
static char *add_shared_owner_prefix(const char *name)
{
	EM_DEBUG_FUNC_BEGIN();
	char *ckm_alias = NULL;

	if (name == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		return NULL;
	}

	ckm_alias = g_strconcat(ckmc_owner_id_system, ckmc_owner_id_separator, name, NULL);

	EM_DEBUG_FUNC_END();
	return ckm_alias;
}
*/
static char *__get_key_manager_alias(const char* name)
{
	int ret = 0;
	char *ckmc_alias = NULL;

	ret = ckmc_alias_new("/User", name, &ckmc_alias);
	if (ret != CKMC_ERROR_NONE) {
		EM_DEBUG_LOG("ckmc_alias_new failed");
		return NULL;

	}

	EM_DEBUG_LOG("alias : [%s]", ckmc_alias);
	return ckmc_alias;

}
/* Add new data */
INTERNAL_FUNC int emcore_add_password_in_key_manager(char *data_name, char *stored_data)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;
	char *alias = NULL;
	ckmc_policy_s email_policy;
	ckmc_raw_buffer_s email_data;
	char errno_buf[ERRNO_BUF_SIZE] = {0};

	if (data_name == NULL || stored_data == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}

	alias = __get_key_manager_alias(data_name);
	EM_DEBUG_LOG("alias : [%s]", alias);

	email_policy.password = NULL;
	email_policy.extractable = true;

//	EM_DEBUG_LOG("stored_data : [%s]", stored_data);

	email_data.data = (unsigned char *)stored_data;
	email_data.size = strlen(stored_data);

	ckmc_ret = ckmc_save_data(alias, email_data, email_policy);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("ckmc_save_data failed [%d][%s]", errno, EM_STRERROR(errno_buf));
		err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
		goto FINISH_OFF;
	}

FINISH_OFF:

	EM_SAFE_FREE(alias);

	EM_DEBUG_FUNC_END();
	return err;
}

/* Get the stored data */
INTERNAL_FUNC int emcore_get_password_in_key_manager(char *data_name, char **stored_data)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;
	char *alias = NULL;
	ckmc_raw_buffer_s *email_data = NULL;

	if (data_name == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}

	alias = __get_key_manager_alias(data_name);
	EM_DEBUG_LOG("alias : [%s]", alias);

	ckmc_ret = ckmc_get_data(alias, NULL, &email_data);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("ckmc_get_data failed : [%d]", ckmc_ret);
		err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
		goto FINISH_OFF;
	}

	EM_DEBUG_LOG_DEV("stored_data : [%s]", email_data->data);
	EM_DEBUG_LOG_DEV("stored_data length : [%d]", email_data->size);

FINISH_OFF:

	if (stored_data) {
		if (email_data) *stored_data = g_strndup((const gchar *)email_data->data, email_data->size);
	}

	if (email_data)
		ckmc_buffer_free(email_data);

	EM_SAFE_FREE(alias);

	EM_DEBUG_FUNC_END();
	return err;
}

/* Remove the stored data */
INTERNAL_FUNC int emcore_remove_password_in_key_manager(char *data_name)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;
	char *alias = NULL;

	if (data_name == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}

	alias = __get_key_manager_alias(data_name);
	EM_DEBUG_LOG("alias : [%s]", alias);

	ckmc_ret = ckmc_remove_alias(alias);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("ckmc_remove_alias failed : [%d]", ckmc_ret);

		err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
		goto FINISH_OFF;
	}

FINISH_OFF:

	EM_SAFE_FREE(alias);
	EM_DEBUG_FUNC_END();
	return err;
}

INTERNAL_FUNC int emcore_get_certificate_in_key_manager(char *alias, char *password,
														const unsigned char **cert_data,
														int *cert_size)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;

	if (alias == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}

	int ckmc_ret = CKMC_ERROR_NONE;
	unsigned char *p_cert_data = NULL;
	ckmc_cert_s *output_cert = NULL;

	ckmc_ret = ckmc_get_cert(alias, password, &output_cert);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("ckmc_get_cert failed : [%d]", ckmc_ret);
		err = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
		goto FINISH_OFF;
	}

	EM_DEBUG_LOG("Cert size : [%d]", output_cert->cert_size);
	EM_DEBUG_LOG("Cert format : [%d]", output_cert->data_format);
	EM_DEBUG_LOG_DEV("Cert string : [%s]", output_cert->raw_cert);

	p_cert_data = em_malloc(output_cert->cert_size + 1);
	if (p_cert_data == NULL) {
		EM_DEBUG_EXCEPTION("em_mallocfailed");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}

	memcpy(p_cert_data, output_cert->raw_cert, output_cert->cert_size);

	*cert_data = p_cert_data;
	*cert_size = output_cert->cert_size;

FINISH_OFF:

	if (output_cert)
		ckmc_cert_free(output_cert);

	if (err != EMAIL_ERROR_NONE)
		EM_SAFE_FREE(p_cert_data);

	EM_DEBUG_FUNC_END();
	return err;
}

/* check key in key manager */
INTERNAL_FUNC int emcore_check_key_in_key_manager(const char *data_name)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;
	char *alias = NULL;
	ckmc_key_s *email_key = NULL;

	if (data_name == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}

	alias = __get_key_manager_alias(data_name);

	if (alias == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}


	ckmc_ret = ckmc_get_key(alias, NULL, &email_key);
	if (ckmc_ret == CKMC_ERROR_DB_ALIAS_UNKNOWN) {
		EM_DEBUG_EXCEPTION("email error key not found");
		err = EMAIL_ERROR_KEY_NOT_FOUND;
		goto FINISH_OFF;
	}


FINISH_OFF:
	if (email_key != NULL)
		ckmc_key_free(email_key);

	EM_SAFE_FREE(alias);

	EM_DEBUG_FUNC_END();
	return err;
}



/* save key in key manager */
INTERNAL_FUNC int emcore_save_key_in_key_manager(const char *data_name)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;
	ckmc_key_s email_key;
	ckmc_policy_s email_policy;

	const char *pass = EMAIL_SERVICE_CS_DERIVE_PASS;
	unsigned char *pass_key = NULL;
	char *alias = NULL;

	if (data_name == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}


	int cs_ret = cs_derive_key_with_pass(pass, strlen(pass), 32, &pass_key);

	if (cs_ret != CS_ERROR_NONE) {
		EM_DEBUG_LOG("creat key failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;
	}

	email_key.raw_key = (unsigned char *)pass_key;
	email_key.key_size = 32;
	email_key.key_type = CKMC_KEY_AES;
	email_key.password = NULL;

	email_policy.password = NULL;
	email_policy.extractable = false;

	alias = __get_key_manager_alias(data_name);

	ckmc_ret = ckmc_save_key(alias, email_key, email_policy);

	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("email error key not save : %s", get_error_message(ckmc_ret));
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;
	}


FINISH_OFF:


	EM_SAFE_FREE(alias);
	EM_DEBUG_FUNC_END();
	return err;
}



/* encrypte data */
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)
{
	EM_DEBUG_FUNC_BEGIN();
	char *alias = NULL;
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;

	ckmc_raw_buffer_s plaintext;
	ckmc_raw_buffer_s *encrypted = NULL;
	ckmc_param_list_h params = NULL;

	unsigned char iv[16] = {0, };
	ckmc_raw_buffer_s iv_buffer;
	iv_buffer.data = iv;
	iv_buffer.size = sizeof(iv);


	plaintext.data = (unsigned char *)input_data;
	plaintext.size = input_length;

	if (data_name == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}


	ckmc_ret = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, &params);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_LOG("ckmc_generate_new_params failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		return err;
	}


	ckmc_ret = ckmc_param_list_set_buffer(params, CKMC_PARAM_ED_IV, &iv_buffer);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_LOG("ckmc_param_list_set_buffer failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;
	}


	alias = __get_key_manager_alias(data_name);
	ckmc_ret = ckmc_encrypt_data(params, alias, NULL, plaintext, &encrypted);
	if (ckmc_ret != CKMC_ERROR_NONE) {
		EM_DEBUG_LOG("ckmc_encrypt_data failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;
	}

	*output_data = encrypted->data;
	*output_length = encrypted->size;

	if (encrypted->data != NULL)
		encrypted->data = NULL;

FINISH_OFF:

	if (encrypted != NULL)
		ckmc_buffer_free(encrypted);

	if (params != NULL)
		ckmc_param_list_free(params);

	EM_SAFE_FREE(alias);
	EM_DEBUG_FUNC_END();
	return err;
}



/* decrypte data from file */
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)
{
	EM_DEBUG_FUNC_BEGIN();
	int err = EMAIL_ERROR_NONE;
	int ckmc_ret = CKMC_ERROR_NONE;
	int buffer_length = 0;
	ckmc_raw_buffer_s *encrypted = NULL;
	ckmc_raw_buffer_s *decrypted = NULL;
	ckmc_param_list_h params = NULL;

	char *alias = NULL;
	char errno_buf[ERRNO_BUF_SIZE] = {0};
	unsigned char iv[16] = {0, };
	ckmc_raw_buffer_s iv_buffer;
	iv_buffer.data = iv;
	iv_buffer.size = sizeof(iv);

	if (data_name == NULL && file_path == NULL) {
		EM_DEBUG_EXCEPTION("Invalid parameter");
		err = EMAIL_ERROR_INVALID_PARAM;
		return err;
	}


	int fd = 0;
	struct stat st_buf;

	if (stat(file_path, &st_buf) < 0) {
		EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", file_path);
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}

	err = em_open(file_path, O_RDONLY, 0, &fd);
	if (err != EMAIL_ERROR_NONE) {
		EM_DEBUG_EXCEPTION("em_open failed : [%d] [%s]", err, file_path);
		goto FINISH_OFF;
	}
	buffer_length = st_buf.st_size;
	EM_DEBUG_LOG("account buffer_length[%d]", buffer_length);
	encrypted = (ckmc_raw_buffer_s*)malloc(sizeof(ckmc_raw_buffer_s));

	if (!encrypted) {
		EM_DEBUG_EXCEPTION("malloc failed...");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}

	if ((encrypted->data = (unsigned char *)em_malloc(buffer_length + 1)) == NULL) {
		EM_DEBUG_EXCEPTION("em_malloc failed...");
		err = EMAIL_ERROR_OUT_OF_MEMORY;
		goto FINISH_OFF;
	}
	if (read(fd, encrypted->data, buffer_length) < 0) {
		EM_DEBUG_EXCEPTION("read failed : [%s]", EM_STRERROR(errno_buf));
		err = EMAIL_ERROR_SYSTEM_FAILURE;
		goto FINISH_OFF;
	}
	encrypted->size = buffer_length;


	ckmc_ret = ckmc_generate_new_params(CKMC_ALGO_AES_CBC, &params);
	if (ckmc_ret != CKMC_ERROR_NONE) {

		EM_DEBUG_LOG("ckmc_generate_new_params failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;

	}


	ckmc_ret = ckmc_param_list_set_buffer(params, CKMC_PARAM_ED_IV, &iv_buffer);
	if (ckmc_ret != CKMC_ERROR_NONE) {

		EM_DEBUG_LOG("ckmc_param_list_set_buffer failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;

	}

	alias = __get_key_manager_alias(data_name);
	ckmc_ret = ckmc_decrypt_data(params, alias, NULL, *encrypted, &decrypted);
	if (ckmc_ret != CKMC_ERROR_NONE) {

		EM_DEBUG_LOG("ckmc_encrypt_data failed");
		err = EMAIL_ERROR_KEY_MANAGER_FAILURE;
		goto FINISH_OFF;

	}

	*output_data = (char *)decrypted->data;
	*output_length = decrypted->size;

	if (decrypted->data != NULL)
		decrypted->data = NULL;





FINISH_OFF:

	if (encrypted != NULL)
		ckmc_buffer_free(encrypted);


	if (decrypted != NULL)
		ckmc_buffer_free(decrypted);

	if (params != NULL)
		ckmc_param_list_free(params);


	EM_SAFE_CLOSE(fd);
	EM_SAFE_FREE(alias);
	EM_DEBUG_FUNC_END();
	return err;
}