2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Dongsun Lee (ds73.lee@samsung.com)
20 * @brief provides fucntions for encryption and decryption of web application.
26 #include "web_app_enc.h"
27 #include "key_handler.h"
28 #include "crypto_service.h"
32 int _wae_encrypt_downloaded_web_application(const char* pPkgId, wae_app_type_e appType,
33 const unsigned char* pData, size_t dataLen,
34 unsigned char** ppEncryptedData, size_t* pEncDataLen)
36 int ret = WAE_ERROR_NONE;
37 unsigned char *pDek = NULL;
41 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
42 ret = WAE_ERROR_INVALID_PARAMETER;
45 if(pData == NULL || dataLen <= 0) {
46 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
47 ret = WAE_ERROR_INVALID_PARAMETER;
50 if(ppEncryptedData == NULL || pEncDataLen == NULL) {
51 WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
52 ret = WAE_ERROR_INVALID_PARAMETER;
57 // if not exists, create APP_DEK
58 ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
59 if(ret == WAE_ERROR_NO_KEY) {
60 ret = create_app_dek(pPkgId, appType, &pDek, &dekLen);
62 if(ret != WAE_ERROR_NONE) {
67 ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
68 if(ret != WAE_ERROR_NONE) {
79 int _wae_decrypt_downloaded_web_application(const char* pPkgId, wae_app_type_e appType,
80 const unsigned char* pData, size_t dataLen,
81 unsigned char** ppDecryptedData, size_t* pDecDataLen)
83 int ret = WAE_ERROR_NONE;
84 unsigned char *pDek = NULL;
88 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
89 ret = WAE_ERROR_INVALID_PARAMETER;
92 if(pData == NULL || dataLen <= 0) {
93 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
94 ret = WAE_ERROR_INVALID_PARAMETER;
97 if(ppDecryptedData == NULL || pDecDataLen == NULL) {
98 WAE_SLOGE("Invalid Parameter. ppDecryptedData or pDecDataLen is NULL");
99 ret = WAE_ERROR_INVALID_PARAMETER;
103 ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
104 if(ret != WAE_ERROR_NONE) {
109 ret = decrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppDecryptedData, pDecDataLen);
110 if(ret != WAE_ERROR_NONE) {
121 int _wae_encrypt_preloaded_web_application(const char* pPkgId,
122 const unsigned char* pData, size_t dataLen,
123 unsigned char** ppEncryptedData, size_t* pEncDataLen)
126 int ret = WAE_ERROR_NONE;
127 unsigned char *pDek = NULL;
131 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
132 ret = WAE_ERROR_INVALID_PARAMETER;
135 if(pData == NULL || dataLen <= 0) {
136 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
137 ret = WAE_ERROR_INVALID_PARAMETER;
140 if(ppEncryptedData == NULL || pEncDataLen == NULL) {
141 WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
142 ret = WAE_ERROR_INVALID_PARAMETER;
146 ret = get_preloaded_app_dek(pPkgId, &pDek, &dekLen);
147 if(ret == WAE_ERROR_NO_KEY) {
148 ret = create_preloaded_app_dek(pPkgId, &pDek, &dekLen);
150 if(ret != WAE_ERROR_NONE) {
155 ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
156 if(ret != WAE_ERROR_NONE) {
166 int _wae_decrypt_preloaded_web_application(const char* pPkgId, wae_app_type_e appType,
167 const unsigned char* pData, size_t dataLen,
168 unsigned char** ppDecryptedData, size_t* pDecDataLen)
170 // same with the decryption of downloaded web application
171 return _wae_decrypt_downloaded_web_application(pPkgId, appType,
172 pData, dataLen, ppDecryptedData, pDecDataLen);
175 int wae_encrypt_web_application(const char* pPkgId, wae_app_type_e appType,
176 const unsigned char* pData, size_t dataLen,
177 unsigned char** ppEncryptedData, size_t* pEncDataLen)
179 int ret = WAE_ERROR_NONE;
181 if(appType == WAE_PRELOADED_APP)
182 ret = _wae_encrypt_preloaded_web_application(pPkgId,
183 pData, dataLen, ppEncryptedData, pEncDataLen);
185 ret = _wae_encrypt_downloaded_web_application(pPkgId, appType,
186 pData, dataLen, ppEncryptedData, pEncDataLen);
188 WAE_SLOGI("Encrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
189 pPkgId, appType, dataLen, ret);
193 int wae_decrypt_web_application(const char* pPkgId, wae_app_type_e appType,
194 const unsigned char* pData, size_t dataLen,
195 unsigned char** ppDecryptedData, size_t* pDecDataLen)
197 int ret = WAE_ERROR_NONE;
199 if(appType == WAE_PRELOADED_APP)
200 ret = _wae_decrypt_preloaded_web_application(pPkgId, appType,
201 pData, dataLen, ppDecryptedData, pDecDataLen);
203 ret = _wae_decrypt_downloaded_web_application(pPkgId, appType,
204 pData, dataLen, ppDecryptedData, pDecDataLen);
206 WAE_SLOGI("Decrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
207 pPkgId, appType, dataLen, ret);
212 int wae_remove_app_dek(const char* pPkgId, wae_app_type_e appType)
214 int ret = WAE_ERROR_NONE;
215 ret = remove_app_dek(pPkgId, appType);
216 WAE_SLOGI("Remove APP DEK. pkgId=%s, appType=%d, ret=%d", pPkgId, appType, ret);