summaryrefslogtreecommitdiff
path: root/srcs/key_handler.c
blob: 0cb27766b5cf623442090b6695df9fa70ce01a61 (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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
 *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  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
 *
 *
 * @file        key_handler.c
 * @author      Dongsun Lee (ds73.lee@samsung.com)
 * @version     1.0
 * @brief       Key manupulatation.
 */
#include "key_handler.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>

#include <ckmc/ckmc-manager.h>
#include <tzplatform_config.h>

#include "wae_log.h"
#include "web_app_enc.h"
#include "crypto_service.h"

#define RANDOM_FILE                 "/dev/urandom"
#define APP_DEK_KEK_PRIKEY_PASSWORD "wae_appdek_kek_1q2w3e4r"
#define APP_DEK_ALIAS_PFX           "APP_DEK_"
#define APP_DEK_LOADING_DONE_ALIAS  "APP_DEKS_LOADING_FINISHED"
#define APP_DEK_FILE_PFX            "WAE_APP_DEK"
#define APP_DEK_KEK_ALIAS           "WAE_APP_DEK_KEK"

#define DEK_LEN        32
#define MAX_ALIAS_LEN  256
#define MAX_PKGID_LEN  256
#define MAX_CACHE_SIZE 100

typedef struct _dek_cache_element {
	char pkg_id[MAX_PKGID_LEN];
	unsigned char dek[DEK_LEN];
} dek_cache_element;

dek_cache_element APP_DEK_CACHE[MAX_CACHE_SIZE];
int NEXT_CACHE_IDX = -1;

void _initialize_cache()
{
	NEXT_CACHE_IDX = 0;
	memset(APP_DEK_CACHE, 0, sizeof(dek_cache_element) * MAX_CACHE_SIZE);
}

const unsigned char *_get_app_dek_from_cache(const char *pkg_id)
{
	if (NEXT_CACHE_IDX < 0)
		_initialize_cache();

	for (size_t i = 0; i < MAX_CACHE_SIZE; i++) {
		//WAE_SLOGI("CACHED APP_DEK[%d]=%s", i, APP_DEK_CACHE[i].pkg_id);
		if (strncmp(pkg_id, APP_DEK_CACHE[i].pkg_id, MAX_PKGID_LEN) == 0)
			return APP_DEK_CACHE[i].dek;
	}

	return NULL;
}

void _add_app_dek_to_cache(const char *pkg_id, const unsigned char *dek)
{
	if (NEXT_CACHE_IDX < 0)
		_initialize_cache();

	// if existing one has the same pkgid
	for (size_t i = 0; i < MAX_CACHE_SIZE; i++) {
		if (strncmp(pkg_id, APP_DEK_CACHE[i].pkg_id, MAX_PKGID_LEN) == 0) {
			memcpy(APP_DEK_CACHE[i].dek, dek, DEK_LEN);
			return;
		}
	}

	// for new pkgid
	strncpy(APP_DEK_CACHE[NEXT_CACHE_IDX].pkg_id, pkg_id, MAX_PKGID_LEN - 1);
	memcpy(APP_DEK_CACHE[NEXT_CACHE_IDX].dek, dek, DEK_LEN);

	++NEXT_CACHE_IDX;

	if (NEXT_CACHE_IDX >= MAX_CACHE_SIZE)
		NEXT_CACHE_IDX = 0;
}

void _remove_app_dek_from_cache(const char *pkg_id)
{
	for (size_t i = 0; i < MAX_CACHE_SIZE; i++) {
		if (strncmp(pkg_id, APP_DEK_CACHE[i].pkg_id, MAX_PKGID_LEN) == 0) {
			memset(APP_DEK_CACHE[i].pkg_id, 0, MAX_PKGID_LEN);
			return;
		}
	}

}

int _to_wae_error(int key_manager_error)
{
	switch (key_manager_error) {
	case CKMC_ERROR_NONE:
		return WAE_ERROR_NONE;

	case CKMC_ERROR_INVALID_PARAMETER:
		return WAE_ERROR_INVALID_PARAMETER;

	case CKMC_ERROR_PERMISSION_DENIED:
		return WAE_ERROR_PERMISSION_DENIED;

	case CKMC_ERROR_DB_ALIAS_UNKNOWN:
		return WAE_ERROR_NO_KEY;

	case CKMC_ERROR_DB_ALIAS_EXISTS:
		return WAE_ERROR_KEY_EXISTS;

	default:
		return WAE_ERROR_KEY_MANAGER;
	}
}

int _get_random(size_t length, unsigned char *random)
{
	FILE *f = fopen(RANDOM_FILE, "r");

	if (f == NULL) {
		WAE_SLOGE("Failed to open random file source: %s", RANDOM_FILE);
		return WAE_ERROR_FILE;
	}

	size_t i = 0;
	int ch = 0;
	while (i < length && (ch = fgetc(f) != EOF))
		random[i++] = (unsigned char)ch;

	fclose(f);

	return WAE_ERROR_NONE;
}

void _get_alias(const char *pkg_id, wae_app_type_e app_type, bool forSave, char *alias, size_t buff_len)
{
	if (app_type == WAE_DOWNLOADED_NORMAL_APP) {
		if (forSave) {
			snprintf(alias, buff_len, "%s%s",
					 APP_DEK_ALIAS_PFX,
					 pkg_id);
		} else {
			snprintf(alias, buff_len, "%c%s%s%s%s",
					'/', INSTALLER_LABEL,
					 ckmc_owner_id_separator,
					 APP_DEK_ALIAS_PFX,
					 pkg_id);
		}
	} else { // system alias
		snprintf(alias, buff_len, "%s%s%s%s",
				 ckmc_owner_id_system,
				 ckmc_owner_id_separator,
				 APP_DEK_ALIAS_PFX,
				 pkg_id);
	}
}

void _get_dek_kek_alias(char *alias, size_t buff_len)
{
	snprintf(alias, buff_len, "%s%s%s",
			 ckmc_owner_id_system,
			 ckmc_owner_id_separator,
			 APP_DEK_KEK_ALIAS);
}

void _get_dek_loading_done_alias(char *alias, size_t buff_len)
{
	snprintf(alias, buff_len, "%s%s%s",
			 ckmc_owner_id_system,
			 ckmc_owner_id_separator,
			 APP_DEK_LOADING_DONE_ALIAS);
}

const char *_get_dek_kek_pub_key_path()
{
	return tzplatform_mkpath4(TZ_SYS_SHARE, "wae", "app_dek", "WAE_APPDEK_KEK_PublicKey.pem");
}

const char *_get_dek_kek_pri_key_path()
{
	return tzplatform_mkpath4(TZ_SYS_SHARE, "wae", "app_dek", "WAE_APPDEK_KEK_PrivateKey.pem");
}

const char *_get_dek_store_path()
{
	return tzplatform_mkpath3(TZ_SYS_SHARE, "wae", "app_dek");
}

int _add_dek_to_key_manager(const char *pkg_id, wae_app_type_e app_type, const unsigned char *dek, size_t dek_len)
{
	int ret = WAE_ERROR_NONE;
	char alias[MAX_ALIAS_LEN] = {0, };
	ckmc_raw_buffer_s buff;
	ckmc_policy_s policy;

	buff.data = (unsigned char *)dek;
	buff.size = dek_len;

	policy.password = NULL;
	policy.extractable = true;

	_get_alias(pkg_id, app_type, true, alias, sizeof(alias));

	// even if it fails to remove, ignore it.
	ckmc_remove_alias(alias);

	ret = _to_wae_error(ckmc_save_data(alias, buff, policy));
	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to add APP_DEK to key-manager. pkg_id=%s, alias=%s, ret=%d", pkg_id, alias, ret);
		return ret;
	}

	// share app_dek for web app laucher to use app_dek
	ret = _to_wae_error(ckmc_set_permission(alias, pkg_id, CKMC_PERMISSION_READ));
	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to set_permission to APP_DEK. pkg_id=%s, ret=%d", pkg_id, ret);
		return ret;
	}

	WAE_SLOGI("WAE: Success to add APP_DEK to key-manager. pkg_id=%s, alias=%s", pkg_id, alias);

	return ret;
}

int _get_preloaded_app_dek_file_path(const char *pkg_id, size_t size, char *path)
{
	int ret = snprintf(path, size, "%s/%s_%s.adek",
				   _get_dek_store_path(), APP_DEK_FILE_PFX, pkg_id);

	if (ret < 0)
		return WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */

	return WAE_ERROR_NONE;
}

int _extract_pkg_id_from_file_name(const char *file_name, char *pkg_id)
{
	char *start = strstr(file_name, APP_DEK_FILE_PFX);

	if (start == NULL) {
		WAE_SLOGE("WAE: Fail to extract pkgid from APP_DEK file. file_name=%s", file_name);
		return WAE_ERROR_FILE;
	}

	start = start + strlen(APP_DEK_FILE_PFX) + 1;
	char *end = strstr(file_name, ".adek");

	if (start == NULL) {
		WAE_SLOGE("WAE: Fail to extract pkgid from APP_DEK file. file_name=%s", file_name);
		return WAE_ERROR_FILE;
	}

	strncpy(pkg_id, start, end - start);
	pkg_id[end - start] = 0; //terminate string

	return WAE_ERROR_NONE;
}

int _read_encrypted_app_dek_from_file(const char *pkg_id, unsigned char **pencrypted_app_dek, size_t *pencrypted_app_dek_len)
{
	char path[MAX_PATH_LEN] = {0,};
	_get_preloaded_app_dek_file_path(pkg_id, sizeof(path), path);
	return _read_from_file(path, pencrypted_app_dek, pencrypted_app_dek_len);
}

int _write_encrypted_app_dek_to_file(const char *pkg_id, const unsigned char *encrypted_app_dek, size_t encrypted_app_dek_len)
{
	char path[MAX_PATH_LEN] = {0,};
	_get_preloaded_app_dek_file_path(pkg_id, sizeof(path), path);
	return _write_to_file(path, encrypted_app_dek, encrypted_app_dek_len);
}

int _read_from_file(const char *path, unsigned char **pdata, size_t *pdata_len)
{
	int ret = WAE_ERROR_NONE;
	unsigned char *file_contents = NULL;
	int ch = 0;
	int i = 0;

	FILE *f = fopen(path, "r");

	if (f == NULL) {
		WAE_SLOGE("WAE: Fail to open a file. file=%s", path);
		return WAE_ERROR_FILE;
	}

	fseek(f, 0, SEEK_END); // move to the end of a file
	int file_len = ftell(f);

	if (file_len <= 0) {
		WAE_SLOGE("WAE: Failed to get file size by ftell. ret: %d", file_len);
		ret = WAE_ERROR_FILE;
		goto error;
	}

	fseek(f, 0, SEEK_SET); // move to the start of a file

	file_contents = (unsigned char *)malloc(file_len);

	if (file_contents == NULL) {
		WAE_SLOGE("WAE: Fail to allocate memory for encrypted_app_dek");
		ret = WAE_ERROR_MEMORY;
		goto error;
	}

	memset(file_contents, 0x00, file_len);

	while ((ch = fgetc(f)) != EOF)
		file_contents[i++] = (char)ch;

	*pdata = file_contents;
	*pdata_len = file_len;

error:
	fclose(f);

	if (ret != WAE_ERROR_NONE)
		free(file_contents);

	return ret;
}

int _write_to_file(const char *path, const unsigned char *data, size_t data_len)
{
	FILE *f = fopen(path, "w");

	if (f == NULL) {
		WAE_SLOGE("WAE: Fail to open a file. file=%s", path);
		return WAE_ERROR_FILE;
	}

	int write_len = fwrite(data, 1, data_len, f);

	fclose(f);

	if (write_len != (int)data_len) {
		WAE_SLOGE("WAE: Fail to write a file. file=%s", path);
		return WAE_ERROR_FILE;
	}

	return WAE_ERROR_NONE;
}

int get_app_dek(const char *pkg_id, wae_app_type_e app_type, unsigned char **pdek, size_t *pdek_len)
{
	int ret = WAE_ERROR_NONE;

	ckmc_raw_buffer_s *dek_buffer = NULL;
	char alias[MAX_ALIAS_LEN] = {0, };

	const unsigned char *cached_dek = _get_app_dek_from_cache(pkg_id);

	if (cached_dek == NULL) {
		// get APP_DEK from system database
		_get_alias(pkg_id, app_type, false, alias, sizeof(alias));

		ret = _to_wae_error(ckmc_get_data(alias, NULL, &dek_buffer));

		if (ret != WAE_ERROR_NONE) {
			WAE_SLOGE("Failed to get APP_DEK from key-manager. pkg_id=%s, alias=%s, ret=%d",
					  pkg_id, alias, ret);
			goto error;
		} else if (dek_buffer == NULL || dek_buffer->data == NULL) {
			WAE_SLOGE("key-manager success but buffer is null for getting dek of pkg_id=%s",
					  pkg_id);
			ret = WAE_ERROR_KEY_MANAGER;
			goto error;
		} else if (dek_buffer->size != DEK_LEN) {
			WAE_SLOGE("DEK's length which has been saved in key-manager is not valid!");
			ret = WAE_ERROR_KEY_MANAGER;
			goto error;
		}

		WAE_SLOGD("Successfully get dek from key-manager for pkgid=%s", pkg_id);
		cached_dek = dek_buffer->data;
	}

	unsigned char *dek = (unsigned char *)malloc(DEK_LEN);

	if (dek == NULL) {
		WAE_SLOGE("Fail to allocate a memory");
		ret = WAE_ERROR_MEMORY;
		goto error;
	}

	memcpy(dek, cached_dek, DEK_LEN);

	*pdek = dek;
	*pdek_len = DEK_LEN;

	WAE_SLOGI("WAE: Success to get APP_DEK from key-manager. pkg_id=%s, alias=%s",
			  pkg_id, alias);

error:
	ckmc_buffer_free(dek_buffer);

	if (ret != WAE_ERROR_NONE)
		free(dek);

	return ret;
}

int create_app_dek(const char *pkg_id, wae_app_type_e app_type, unsigned char **pdek, size_t *pdek_len)
{
	unsigned char *dek = (unsigned char *)malloc(DEK_LEN);

	if (dek == NULL)
		return WAE_ERROR_MEMORY;

	int ret = _get_random(DEK_LEN, dek);

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to get random for APP_DEK. pkg_id=%s, ret=%d", pkg_id, ret);
		goto error;
	}

	// save app_dek in key_manager
	ret = _add_dek_to_key_manager(pkg_id, app_type, dek, DEK_LEN);

	if (ret != WAE_ERROR_NONE) {
		goto error;
	}

	// store APP_DEK in cache
	_add_app_dek_to_cache(pkg_id, dek);

	*pdek = dek;
	*pdek_len = DEK_LEN;

	WAE_SLOGI("WAE: Success to create APP_DEK and store it in key-manager. pkg_id=%s", pkg_id);

	return WAE_ERROR_NONE;

error:
	free(dek);

	return ret;
}

int get_preloaded_app_dek(const char *pkg_id, unsigned char **pdek, size_t *pdek_len)
{
	const unsigned char *cached_dek = _get_app_dek_from_cache(pkg_id);

	if (cached_dek == NULL) {
		WAE_SLOGE("WAE: Fail to get APP_DEK from cache for preloaded app");
		return WAE_ERROR_NO_KEY;
	}

	unsigned char *dek = (unsigned char *)malloc(DEK_LEN);

	if (dek == NULL) {
		WAE_SLOGE("WAE: Fail to allocate memory for preloaded app dek");
		return WAE_ERROR_MEMORY;
	}

	memcpy(dek, cached_dek, DEK_LEN);

	*pdek = dek;
	*pdek_len = DEK_LEN;

	return WAE_ERROR_NONE;
}

int create_preloaded_app_dek(const char *pkg_id, unsigned char **pdek, size_t *pdek_len)
{
	unsigned char *encrypted_app_dek = NULL;
	size_t encrypted_app_dek_len = 0;
	unsigned char *pubkey = NULL;
	size_t pubkey_len = 0;

	// create APP_DEK
	unsigned char *dek = (unsigned char *)malloc(DEK_LEN);

	if (dek == NULL)
		return WAE_ERROR_MEMORY;

	int ret = _get_random(DEK_LEN, dek);

	if (ret != WAE_ERROR_NONE)
		goto error;

	// encrypt APP_DEK with APP_DEK_KEK
	ret = _read_from_file(_get_dek_kek_pub_key_path(), &pubkey, &pubkey_len);

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to read APP_DEK_KEK Public Key");
		goto error;
	}

	ret = encrypt_app_dek(pubkey, pubkey_len, dek, DEK_LEN, &encrypted_app_dek, &encrypted_app_dek_len);

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to encrypt APP_DEK with APP_DEK_KEK");
		goto error;
	}

	// write APP_DEK in a file
	ret = _write_encrypted_app_dek_to_file(pkg_id, encrypted_app_dek, encrypted_app_dek_len);

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to write encrypted APP_DEK. pkg_id=%s", pkg_id);
		goto error;
	}

	// store APP_DEK in cache
	_add_app_dek_to_cache(pkg_id, dek);

	*pdek = dek;
	*pdek_len = DEK_LEN;
	WAE_SLOGI("WAE: Success to create preleaded APP_DEK and write it in initail value file. pkg_id=%s", pkg_id);

error:
	free(pubkey);
	free(encrypted_app_dek);

	if (ret != WAE_ERROR_NONE)
		free(dek);

	return ret;
}

int _get_app_dek_kek(unsigned char **pdek_kek, size_t *pdek_kek_len)
{
	int ret = _read_from_file(_get_dek_kek_pri_key_path(), pdek_kek, pdek_kek_len);

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("WAE: Fail to read APP_DEK_KEK Private Key");
		return ret;
	}

#if 0
	ckmc_raw_buffer_s *kek_buffer = NULL;
	unsigned char* kek = NULL;

	char dek_kek_alias[MAX_ALIAS_LEN] = {0, };
	_get_dek_kek_alias(dek_kek_alias, sizeof(dek_kek_alias));

	ret = _to_wae_error(ckmc_get_data(dek_kek_alias, NULL, &kek_buffer));
	if (ret != WAE_ERROR_NONE) {
	    WAE_SLOGE("Fail to get APP_DEK_KEK from key-manager. alias=%s, ret=%d",
				  APP_DEK_KEK_ALIAS, ret);
	    goto error;
	}

	kek = (unsigned char *)malloc(kek_buffer->size);
	if(kek == NULL) {
	    WAE_SLOGE("Fail to allocate a memory");
	    ret = WAE_ERROR_MEMORY;
	    goto error;
	}
	memcpy(kek, kek_buffer->data, kek_buffer->size);

	*pdek_kek = kek;
	*pdek_kek_len = kek_buffer->size;
	WAE_SLOGI("Success to get APP_DEK_KEK from key-manager.");

error:
	ckmc_buffer_free(kek_buffer);
	free(kek);
#endif

	return ret;
}

int _get_app_deks_loaded()
{
	char loading_done_alias[MAX_ALIAS_LEN] = {0, };
	_get_dek_loading_done_alias(loading_done_alias, sizeof(loading_done_alias));

	ckmc_raw_buffer_s *buffer = NULL;
	int ret = _to_wae_error(ckmc_get_data(loading_done_alias, NULL, &buffer));

	if (ret == WAE_ERROR_NO_KEY)
		WAE_SLOGI("WAE: APP_DEK_LOADING was not done");
	else if (ret == WAE_ERROR_NONE)
		WAE_SLOGI("WAE: APP_DEK_LOADING was already done");
	else
		WAE_SLOGE("WAE: Fail to get information from key-manager about APP_DEK_LOADING_DONE_ALIAS. ret=%d", ret);

	ckmc_buffer_free(buffer);

	return ret;
}

int _set_app_deks_loaded()
{
	ckmc_raw_buffer_s buff;
	ckmc_policy_s policy;
	unsigned char dummy_data[1] = {0};

	buff.data = dummy_data;
	buff.size = sizeof(dummy_data);

	policy.password = NULL;
	policy.extractable = true;

	char loading_done_alias[MAX_ALIAS_LEN] = {0, };
	_get_dek_loading_done_alias(loading_done_alias, sizeof(loading_done_alias));

	int ret = _to_wae_error(ckmc_save_data(loading_done_alias, buff, policy));

	if (ret == WAE_ERROR_KEY_EXISTS) {
		WAE_SLOGI("WAE: APP_DEK_LOADING was already done");
		ret = WAE_ERROR_NONE;
	} else if (ret == WAE_ERROR_NONE) {
		WAE_SLOGI("Success to set APP_DEK_LOADING_DONE_ALIAS to key-manager.");
	} else {
		WAE_SLOGE("WAE: Fail to set APP_DEK_LOADING_DONE_ALIAS to key-manager. ret=%d", ret);
	}

	return ret;
}

int _clear_app_deks_loaded()
{
	char loading_done_alias[MAX_ALIAS_LEN] = {0, };
	_get_dek_loading_done_alias(loading_done_alias, sizeof(loading_done_alias));

	int ret = _to_wae_error(ckmc_remove_alias(loading_done_alias));

	if (ret == WAE_ERROR_NO_KEY) {
		WAE_SLOGI("APP_DEK_LOADING_DONE_ALIAS was not set to key-manager before.");
		ret = WAE_ERROR_NONE;
	} else if (ret == WAE_ERROR_NONE) {
		WAE_SLOGI("Success to clear app deks loaded");
	} else {
		WAE_SLOGE("Fail to clear APP_DEK_LOADING_DONE_ALIAS to key-manager. ret=%d", ret);
	}

	return ret;
}

int load_preloaded_app_deks(bool reload)
{
	int ret = WAE_ERROR_NONE;

	char pkg_id[MAX_PKGID_LEN] = {0, };

	char file_path_buff[MAX_PATH_LEN];
	unsigned char *encrypted_app_dek = NULL;
	size_t encrypted_app_dek_len = 0;
	unsigned char *app_dek = NULL;
	size_t app_dek_len = 0;
	unsigned char *prikey = NULL;
	size_t prikey_len = 0;

	int error_during_loading = 0;

	if (!reload) {
		// check if all deks were already loaded into key-manager.
		ret = _get_app_deks_loaded();

		if (ret == WAE_ERROR_NONE)
			return ret;
	}

	ret = _get_app_dek_kek(&prikey, &prikey_len);

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("Fail to get APP_DEK_KEK Private Key");
		return ret;
	}

	DIR *dir = opendir(_get_dek_store_path());

	if (dir == NULL) {
		WAE_SLOGE("Fail to open dir. dir=%s", _get_dek_store_path());
		return WAE_ERROR_FILE;
	}

	struct dirent entry;
	struct dirent *result = NULL;

	while (true) {
		int error = readdir_r(dir, &entry, &result);

		if (error != 0) {
			ret = WAE_ERROR_FILE;
			goto error;
		}

		// readdir_r returns NULL in *result if the end
		// of the directory stream is reached
		if (result == NULL)
			break;

		// regular file && start with KEY_MANAGER_INITIAL_VALUE_FILE_PFX
		if (entry.d_type != DT_REG || strstr(entry.d_name, APP_DEK_FILE_PFX) == NULL)
			continue;

		memset(file_path_buff, 0, sizeof(file_path_buff));
		ret = snprintf(file_path_buff, sizeof(file_path_buff), "%s/%s",
					   _get_dek_store_path(), entry.d_name);

		if (ret < 0) {
			WAE_SLOGE("Failed to make file path by snprintf.");
			ret = WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
			goto error;
		}

		ret = _extract_pkg_id_from_file_name(entry.d_name, pkg_id);

		if (ret != WAE_ERROR_NONE) {
			WAE_SLOGW("Fail to extract pkgid from file. It will be ignored. file=%s", file_path_buff);
			continue;
		}

		ret = _read_from_file(file_path_buff, &encrypted_app_dek, &encrypted_app_dek_len);

		if (ret != WAE_ERROR_NONE || encrypted_app_dek == NULL) {
			error_during_loading++;
			WAE_SLOGW("Fail to read file. It will be ignored. file=%s", file_path_buff);
			continue;
		}

		ret = decrypt_app_dek(prikey, prikey_len, APP_DEK_KEK_PRIKEY_PASSWORD,
							  encrypted_app_dek, encrypted_app_dek_len,
							  &app_dek, &app_dek_len);

		if (ret != WAE_ERROR_NONE || app_dek == NULL) {
			error_during_loading++;
			WAE_SLOGW("Fail to decrypt APP DEK. It will be ignored. file=%s", file_path_buff);
			continue;
		}

		// save app_dek in key_manager
		ret = _add_dek_to_key_manager(pkg_id, WAE_PRELOADED_APP, app_dek, app_dek_len);
		// free temp objects
		free(app_dek);
		free(encrypted_app_dek);
		app_dek = NULL;
		encrypted_app_dek = NULL;

		if (ret == WAE_ERROR_KEY_EXISTS) {
			WAE_SLOGI("Key Manager already has APP_DEK. It will be ignored. file=%s", file_path_buff);
		} else if (ret != WAE_ERROR_NONE) {
			error_during_loading++;
			WAE_SLOGW("Fail to add APP DEK to key-manager. file=%s", file_path_buff);
		}
	}

	ret = _set_app_deks_loaded();

	if (ret == WAE_ERROR_NONE) {
		WAE_SLOGI("Success to load_preloaded_app_deks");
		ret = WAE_ERROR_NONE;
	} else {
		WAE_SLOGW("Fail to _set_app_deks_loaded to key-manager. ret=%d", ret);
	}

error:
	free(prikey);
	closedir(dir);

	return ret;
}

int remove_app_dek(const char *pkg_id, wae_app_type_e app_type)
{
	char alias[MAX_ALIAS_LEN] = {0,};

	_get_alias(pkg_id, app_type, true, alias, sizeof(alias));

	int ret = _to_wae_error(ckmc_remove_alias(alias));

	if (ret != WAE_ERROR_NONE) {
		WAE_SLOGE("Fail to remove APP_DEK from  key-manager. pkg_id=%s, alias=%s, ret=%d", pkg_id, alias, ret);
		return ret;
	}

	_remove_app_dek_from_cache(pkg_id);
	WAE_SLOGI("Success to remove APP_DEK from  key-manager. pkg_id=%s", pkg_id);

	return WAE_ERROR_NONE;
}