summaryrefslogtreecommitdiff
path: root/srcs/key_handler.c
diff options
context:
space:
mode:
Diffstat (limited to 'srcs/key_handler.c')
-rw-r--r--srcs/key_handler.c504
1 files changed, 240 insertions, 264 deletions
diff --git a/srcs/key_handler.c b/srcs/key_handler.c
index 15dc96f..0cb2776 100644
--- a/srcs/key_handler.c
+++ b/srcs/key_handler.c
@@ -47,7 +47,7 @@
#define MAX_CACHE_SIZE 100
typedef struct _dek_cache_element {
- char pkgId[MAX_PKGID_LEN];
+ char pkg_id[MAX_PKGID_LEN];
unsigned char dek[DEK_LEN];
} dek_cache_element;
@@ -57,55 +57,51 @@ int NEXT_CACHE_IDX = -1;
void _initialize_cache()
{
NEXT_CACHE_IDX = 0;
- memset(APP_DEK_CACHE, 0, sizeof(dek_cache_element)*MAX_CACHE_SIZE);
+ memset(APP_DEK_CACHE, 0, sizeof(dek_cache_element) * MAX_CACHE_SIZE);
}
-unsigned char *_get_app_dek_from_cache(const char *pkgId)
+const unsigned char *_get_app_dek_from_cache(const char *pkg_id)
{
if (NEXT_CACHE_IDX < 0)
_initialize_cache();
- for (int i = 0; i < MAX_CACHE_SIZE; i++) {
- //WAE_SLOGI("CACHED APP_DEK[%d]=%s", i, APP_DEK_CACHE[i].pkgId);
- if (strlen(APP_DEK_CACHE[i].pkgId) == strlen(pkgId) &&
- strncmp(pkgId, APP_DEK_CACHE[i].pkgId, strlen(pkgId)) == 0) {
+ 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 *pkgId, unsigned char *dek)
+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 (int i = 0; i < MAX_CACHE_SIZE; i++) {
- if (strlen(APP_DEK_CACHE[i].pkgId) == strlen(pkgId) &&
- strncmp(pkgId, APP_DEK_CACHE[i].pkgId, strlen(pkgId)) == 0) {
+ 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].pkgId, pkgId, strlen(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++;
+ ++NEXT_CACHE_IDX;
if (NEXT_CACHE_IDX >= MAX_CACHE_SIZE)
NEXT_CACHE_IDX = 0;
}
-void _remove_app_dek_from_cache(const char *pkgId)
+void _remove_app_dek_from_cache(const char *pkg_id)
{
- for (int i = 0; i < MAX_CACHE_SIZE; i++) {
- if (strlen(APP_DEK_CACHE[i].pkgId) == strlen(pkgId) &&
- strncmp(pkgId, APP_DEK_CACHE[i].pkgId, strlen(pkgId)) == 0) {
- memset(APP_DEK_CACHE[i].pkgId, 0, sizeof(APP_DEK_CACHE[i].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) {
+ memset(APP_DEK_CACHE[i].pkg_id, 0, MAX_PKGID_LEN);
return;
}
}
@@ -137,48 +133,43 @@ int _to_wae_error(int key_manager_error)
int _get_random(size_t length, unsigned char *random)
{
- FILE *f = NULL;
- size_t i = 0;
- int ch = 0;
+ FILE *f = fopen(RANDOM_FILE, "r");
- //read random file
- if ((f = fopen(RANDOM_FILE, "r")) != NULL) {
- while (i < length) {
- if ((ch = fgetc(f)) == EOF) {
- break;
- }
-
- random[i] = (unsigned char) ch;
- i++;
- }
+ if (f == NULL) {
+ WAE_SLOGE("Failed to open random file source: %s", RANDOM_FILE);
+ return WAE_ERROR_FILE;
}
- if (f != NULL)
- fclose(f);
+ 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 *pPkgId, wae_app_type_e appType, bool forSave, char *alias, size_t buff_len)
+void _get_alias(const char *pkg_id, wae_app_type_e app_type, bool forSave, char *alias, size_t buff_len)
{
- if (appType == WAE_DOWNLOADED_NORMAL_APP) {
+ if (app_type == WAE_DOWNLOADED_NORMAL_APP) {
if (forSave) {
snprintf(alias, buff_len, "%s%s",
APP_DEK_ALIAS_PFX,
- pPkgId);
+ pkg_id);
} else {
snprintf(alias, buff_len, "%c%s%s%s%s",
'/', INSTALLER_LABEL,
ckmc_owner_id_separator,
APP_DEK_ALIAS_PFX,
- pPkgId);
+ 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,
- pPkgId);
+ pkg_id);
}
}
@@ -213,49 +204,46 @@ const char *_get_dek_store_path()
return tzplatform_mkpath3(TZ_SYS_SHARE, "wae", "app_dek");
}
-int _add_dek_to_key_manager(const char *pPkgId, wae_app_type_e appType, const unsigned char *pDek, size_t len)
+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,};
+ char alias[MAX_ALIAS_LEN] = {0, };
ckmc_raw_buffer_s buff;
ckmc_policy_s policy;
- buff.data = (unsigned char *)pDek;
- buff.size = len;
+ buff.data = (unsigned char *)dek;
+ buff.size = dek_len;
policy.password = NULL;
policy.extractable = true;
- // save app_dek in key_manager
- _get_alias(pPkgId, appType, true, alias, sizeof(alias));
+ _get_alias(pkg_id, app_type, true, alias, sizeof(alias));
// even if it fails to remove, ignore it.
- ret = _to_wae_error(ckmc_remove_alias(alias));
+ 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. pkgId=%s, alias=%s, ret=%d", pPkgId, alias, ret);
+ 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, pPkgId, CKMC_PERMISSION_READ));
-
+ 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. pkgId=%s, ret=%d", pPkgId, ret);
+ 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. pkgId=%s, alias=%s", pPkgId, alias);
+ 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 *pPkgId, size_t size, char *path)
+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, pPkgId);
+ _get_dek_store_path(), APP_DEK_FILE_PFX, pkg_id);
if (ret < 0)
return WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
@@ -263,47 +251,46 @@ int _get_preloaded_app_dek_file_path(const char *pPkgId, size_t size, char *path
return WAE_ERROR_NONE;
}
-int _extract_pkg_id_from_file_name(const char *fileName, char *pkgId)
+int _extract_pkg_id_from_file_name(const char *file_name, char *pkg_id)
{
- char *start = strstr(fileName, APP_DEK_FILE_PFX);
+ char *start = strstr(file_name, APP_DEK_FILE_PFX);
if (start == NULL) {
- WAE_SLOGE("WAE: Fail to extract pkgid from APP_DEK file. fileName=%s", fileName);
+ 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(fileName, ".adek");
+ char *end = strstr(file_name, ".adek");
if (start == NULL) {
- WAE_SLOGE("WAE: Fail to extract pkgid from APP_DEK file. fileName=%s", fileName);
+ WAE_SLOGE("WAE: Fail to extract pkgid from APP_DEK file. file_name=%s", file_name);
return WAE_ERROR_FILE;
}
- strncpy(pkgId, start, end - start);
- pkgId[end - start] = 0; //terminate string
+ 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 *pPkgId, unsigned char **encrypted_app_dek, size_t *len)
+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(pPkgId, sizeof(path), path);
- return _read_from_file(path, encrypted_app_dek, len);
+ _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 *pPkgId, const unsigned char *encrypted_app_dek, size_t 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(pPkgId, sizeof(path), path);
- return _write_to_file(path, encrypted_app_dek, len);
+ _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 **data, size_t *len)
+int _read_from_file(const char *path, unsigned char **pdata, size_t *pdata_len)
{
int ret = WAE_ERROR_NONE;
- int file_len = -1;
unsigned char *file_contents = NULL;
int ch = 0;
int i = 0;
@@ -316,7 +303,7 @@ int _read_from_file(const char *path, unsigned char **data, size_t *len)
}
fseek(f, 0, SEEK_END); // move to the end of a file
- file_len = ftell(f);
+ int file_len = ftell(f);
if (file_len <= 0) {
WAE_SLOGE("WAE: Failed to get file size by ftell. ret: %d", file_len);
@@ -326,7 +313,7 @@ int _read_from_file(const char *path, unsigned char **data, size_t *len)
fseek(f, 0, SEEK_SET); // move to the start of a file
- file_contents = (unsigned char *) malloc(file_len);
+ file_contents = (unsigned char *)malloc(file_len);
if (file_contents == NULL) {
WAE_SLOGE("WAE: Fail to allocate memory for encrypted_app_dek");
@@ -336,29 +323,23 @@ int _read_from_file(const char *path, unsigned char **data, size_t *len)
memset(file_contents, 0x00, file_len);
- while ((ch = fgetc(f)) != EOF) {
+ while ((ch = fgetc(f)) != EOF)
file_contents[i++] = (char)ch;
- }
- *data = file_contents;
- *len = file_len;
+ *pdata = file_contents;
+ *pdata_len = file_len;
error:
- if (f != NULL)
- fclose(f);
+ fclose(f);
- if (ret != WAE_ERROR_NONE && file_contents != NULL)
+ if (ret != WAE_ERROR_NONE)
free(file_contents);
return ret;
}
-int _write_to_file(const char *path, const unsigned char *data, size_t len)
+int _write_to_file(const char *path, const unsigned char *data, size_t data_len)
{
- int ret = WAE_ERROR_NONE;
-
- int write_len = -1;
-
FILE *f = fopen(path, "w");
if (f == NULL) {
@@ -366,72 +347,80 @@ int _write_to_file(const char *path, const unsigned char *data, size_t len)
return WAE_ERROR_FILE;
}
- write_len = fwrite(data, 1, len, f);
+ int write_len = fwrite(data, 1, data_len, f);
+
+ fclose(f);
- if (write_len != (int) len) {
+ if (write_len != (int)data_len) {
WAE_SLOGE("WAE: Fail to write a file. file=%s", path);
- ret = WAE_ERROR_FILE;
- goto error;
+ return WAE_ERROR_FILE;
}
-error:
- if (f != NULL)
- fclose(f);
-
- return ret;
+ return WAE_ERROR_NONE;
}
-int get_app_dek(const char *pPkgId, wae_app_type_e appType, unsigned char **ppDek, size_t *dekLen)
+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;
- char *password = NULL;
- ckmc_raw_buffer_s *pDekBuffer = NULL;
- char alias[MAX_ALIAS_LEN] = {0,};
- unsigned char *pDek = NULL;
+ ckmc_raw_buffer_s *dek_buffer = NULL;
+ char alias[MAX_ALIAS_LEN] = {0, };
- unsigned char *cached_dek = _get_app_dek_from_cache(pPkgId);
+ const unsigned char *cached_dek = _get_app_dek_from_cache(pkg_id);
if (cached_dek == NULL) {
// get APP_DEK from system database
- _get_alias(pPkgId, appType, false, alias, sizeof(alias));
+ _get_alias(pkg_id, app_type, false, alias, sizeof(alias));
- ret = _to_wae_error(ckmc_get_data(alias, password, &pDekBuffer));
+ ret = _to_wae_error(ckmc_get_data(alias, NULL, &dek_buffer));
if (ret != WAE_ERROR_NONE) {
- WAE_SLOGE("WAE: Fail to get APP_DEK from key-manager. pkgId=%s, alias=%s, ret=%d",
- pPkgId, alias, ret);
+ 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;
}
- pDek = (unsigned char *)malloc(DEK_LEN);
+ unsigned char *dek = (unsigned char *)malloc(DEK_LEN);
- if (pDek == NULL) {
+ if (dek == NULL) {
WAE_SLOGE("Fail to allocate a memory");
ret = WAE_ERROR_MEMORY;
goto error;
}
- memcpy(pDek, (cached_dek != NULL) ? cached_dek : pDekBuffer->data, DEK_LEN);
+ memcpy(dek, cached_dek, DEK_LEN);
+
+ *pdek = dek;
+ *pdek_len = DEK_LEN;
- *ppDek = pDek;
- *dekLen = DEK_LEN;
- WAE_SLOGI("WAE: Success to get APP_DEK from key-manager. pkgId=%s, alias=%s", pPkgId, alias);
+ WAE_SLOGI("WAE: Success to get APP_DEK from key-manager. pkg_id=%s, alias=%s",
+ pkg_id, alias);
error:
- if (pDekBuffer != NULL)
- ckmc_buffer_free(pDekBuffer);
+ ckmc_buffer_free(dek_buffer);
- if (ret != WAE_ERROR_NONE && pDek != NULL)
- free(pDek);
+ if (ret != WAE_ERROR_NONE)
+ free(dek);
return ret;
}
-int create_app_dek(const char *pPkgId, wae_app_type_e appType, unsigned char **ppDek, size_t *dekLen)
+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);
+ unsigned char *dek = (unsigned char *)malloc(DEK_LEN);
if (dek == NULL)
return WAE_ERROR_MEMORY;
@@ -439,69 +428,63 @@ int create_app_dek(const char *pPkgId, wae_app_type_e appType, unsigned char **p
int ret = _get_random(DEK_LEN, dek);
if (ret != WAE_ERROR_NONE) {
- WAE_SLOGE("WAE: Fail to get random for APP_DEK. pkgId=%s, ret=%d", pPkgId, ret);
+ 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(pPkgId, appType, dek, DEK_LEN);
+ 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(pPkgId, dek);
+ _add_app_dek_to_cache(pkg_id, dek);
- *ppDek = dek;
- *dekLen = DEK_LEN;
+ *pdek = dek;
+ *pdek_len = DEK_LEN;
- WAE_SLOGI("WAE: Success to create APP_DEK and store it in key-manager. pkgId=%s", pPkgId);
+ WAE_SLOGI("WAE: Success to create APP_DEK and store it in key-manager. pkg_id=%s", pkg_id);
+
+ return WAE_ERROR_NONE;
error:
- if (ret != WAE_ERROR_NONE && dek != NULL)
- free(dek);
+ free(dek);
return ret;
}
-int get_preloaded_app_dek(const char *pPkgId, unsigned char **ppDek, size_t *dekLen)
+int get_preloaded_app_dek(const char *pkg_id, unsigned char **pdek, size_t *pdek_len)
{
- int ret = WAE_ERROR_NONE;
-
- unsigned char *cached_dek = _get_app_dek_from_cache(pPkgId);
+ 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);
+ unsigned char *dek = (unsigned char *)malloc(DEK_LEN);
if (dek == NULL) {
WAE_SLOGE("WAE: Fail to allocate memory for preloaded app dek");
- ret = WAE_ERROR_MEMORY;
- goto error;
+ return WAE_ERROR_MEMORY;
}
memcpy(dek, cached_dek, DEK_LEN);
- *ppDek = dek;
- *dekLen = DEK_LEN;
-
-error:
- if (ret != WAE_ERROR_NONE && dek != NULL)
- free(dek);
+ *pdek = dek;
+ *pdek_len = DEK_LEN;
- return ret;
+ return WAE_ERROR_NONE;
}
-int create_preloaded_app_dek(const char *pPkgId, unsigned char **ppDek, size_t *dekLen)
+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 pubKeyLen = 0;
+ unsigned char *pubkey = NULL;
+ size_t pubkey_len = 0;
// create APP_DEK
unsigned char *dek = (unsigned char *)malloc(DEK_LEN);
@@ -515,14 +498,14 @@ int create_preloaded_app_dek(const char *pPkgId, unsigned char **ppDek, size_t *
goto error;
// encrypt APP_DEK with APP_DEK_KEK
- ret = _read_from_file(_get_dek_kek_pub_key_path(), &pubKey, &pubKeyLen);
+ 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, pubKeyLen, dek, DEK_LEN, &encrypted_app_dek, &encrypted_app_dek_len);
+ 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");
@@ -530,73 +513,70 @@ int create_preloaded_app_dek(const char *pPkgId, unsigned char **ppDek, size_t *
}
// write APP_DEK in a file
- ret = _write_encrypted_app_dek_to_file(pPkgId, encrypted_app_dek, encrypted_app_dek_len);
+ 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. pkgId=%s", pPkgId);
+ 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(pPkgId, dek);
+ _add_app_dek_to_cache(pkg_id, dek);
- *ppDek = dek;
- *dekLen = DEK_LEN;
- WAE_SLOGI("WAE: Success to create preleaded APP_DEK and write it in initail value file. pkgId=%s", pPkgId);
+ *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:
- if (pubKey != NULL)
- free(pubKey);
+ free(pubkey);
+ free(encrypted_app_dek);
- if (encrypted_app_dek != NULL)
- free(encrypted_app_dek);
-
- if (ret != WAE_ERROR_NONE && dek != NULL)
+ if (ret != WAE_ERROR_NONE)
free(dek);
return ret;
}
-int _get_app_dek_kek(unsigned char **ppDekKek, size_t *kekLen)
+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(), ppDekKek, kekLen);
+ 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;
}
- /*
- char* password = NULL;
- ckmc_raw_buffer_s *pKekBuffer = NULL;
- unsigned char* pKek = 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, password, &pKekBuffer));
- 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;
- }
-
- pKek = (unsigned char*) malloc(pKekBuffer->size);
- if(pKek == NULL) {
- WAE_SLOGE("Fail to allocate a memory");
- ret = WAE_ERROR_MEMORY;
- goto error;
- }
- memcpy(pKek, pKekBuffer->data, pKekBuffer->size);
-
- *ppDekKek = pKek;
- *kekLen = pKekBuffer->size;
- WAE_SLOGI("Success to get APP_DEK_KEK from key-manager.");
- error:
- if(pKekBuffer != NULL)
- ckmc_buffer_free(pKekBuffer);
- if(ret != WAE_ERROR_NONE && pKek != NULL)
- free(pKek);
- */
+#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;
}
@@ -605,8 +585,8 @@ 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 *pBuffer = NULL;
- int ret = _to_wae_error(ckmc_get_data(loading_done_alias, NULL, &pBuffer));
+ 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");
@@ -615,8 +595,7 @@ int _get_app_deks_loaded()
else
WAE_SLOGE("WAE: Fail to get information from key-manager about APP_DEK_LOADING_DONE_ALIAS. ret=%d", ret);
- if (pBuffer != NULL)
- ckmc_buffer_free(pBuffer);
+ ckmc_buffer_free(buffer);
return ret;
}
@@ -625,10 +604,10 @@ int _set_app_deks_loaded()
{
ckmc_raw_buffer_s buff;
ckmc_policy_s policy;
- unsigned char dummyData[1] = {0};
+ unsigned char dummy_data[1] = {0};
- buff.data = dummyData;
- buff.size = sizeof(dummyData);
+ buff.data = dummy_data;
+ buff.size = sizeof(dummy_data);
policy.password = NULL;
policy.extractable = true;
@@ -673,19 +652,15 @@ int load_preloaded_app_deks(bool reload)
{
int ret = WAE_ERROR_NONE;
- char pkgId[MAX_PKGID_LEN] = {0, };
+ char pkg_id[MAX_PKGID_LEN] = {0, };
- DIR *dir = NULL;
- struct dirent entry;
- struct dirent *result;
- int error;
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 priKeyLen = 0;
+ unsigned char *prikey = NULL;
+ size_t prikey_len = 0;
int error_during_loading = 0;
@@ -697,23 +672,25 @@ int load_preloaded_app_deks(bool reload)
return ret;
}
- ret = _get_app_dek_kek(&priKey, &priKeyLen);
+ 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 = opendir(_get_dek_store_path());
+ DIR *dir = opendir(_get_dek_store_path());
if (dir == NULL) {
WAE_SLOGE("Fail to open dir. dir=%s", _get_dek_store_path());
- ret = WAE_ERROR_FILE;
- goto error;
+ return WAE_ERROR_FILE;
}
+ struct dirent entry;
+ struct dirent *result = NULL;
+
while (true) {
- error = readdir_r(dir, &entry, &result);
+ int error = readdir_r(dir, &entry, &result);
if (error != 0) {
ret = WAE_ERROR_FILE;
@@ -726,58 +703,57 @@ int load_preloaded_app_deks(bool reload)
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) {
- 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, pkgId);
-
- 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, priKeyLen, 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(pkgId, 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);
- continue;
- } else if (ret != WAE_ERROR_NONE) {
- error_during_loading++;
- WAE_SLOGW("Fail to add APP DEK to key-manager. file=%s", file_path_buff);
- continue;
- }
+ 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);
}
}
@@ -791,27 +767,27 @@ int load_preloaded_app_deks(bool reload)
}
error:
- if (priKey != NULL)
- free(priKey);
+ free(prikey);
+ closedir(dir);
return ret;
}
-int remove_app_dek(const char *pPkgId, wae_app_type_e appType)
+int remove_app_dek(const char *pkg_id, wae_app_type_e app_type)
{
char alias[MAX_ALIAS_LEN] = {0,};
- _get_alias(pPkgId, appType, true, alias, sizeof(alias));
+ _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. pkgId=%s, alias=%s, ret=%d", pPkgId, alias, ret);
+ 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(pPkgId);
- WAE_SLOGI("Success to remove APP_DEK from key-manager. pkgId=%s", pPkgId);
+ _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;
}