Apply coding style of tizen by astyle
[platform/core/security/libwebappenc.git] / srcs / web_app_enc.c
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  *
16  *
17  * @file        web_app_enc.c
18  * @author      Dongsun Lee (ds73.lee@samsung.com)
19  * @version     1.0
20  * @brief       provides fucntions for encryption and decryption of web application.
21  */
22 #include "web_app_enc.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include "key_handler.h"
28 #include "crypto_service.h"
29 #include "wae_log.h"
30
31 int _wae_encrypt_downloaded_web_application(const char *pPkgId, wae_app_type_e appType,
32                 const unsigned char *pData, size_t dataLen,
33                 unsigned char **ppEncryptedData, size_t *pEncDataLen)
34 {
35         int ret = WAE_ERROR_NONE;
36         unsigned char *pDek = NULL;
37         size_t dekLen = -1;
38
39         if (pPkgId == NULL) {
40                 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
41                 ret = WAE_ERROR_INVALID_PARAMETER;
42                 goto error;
43         }
44
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;
48                 goto error;
49         }
50
51         if (ppEncryptedData == NULL || pEncDataLen == NULL) {
52                 WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
53                 ret = WAE_ERROR_INVALID_PARAMETER;
54                 goto error;
55         }
56
57         // get APP_DEK.
58         //   if not exists, create APP_DEK
59         ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
60
61         if (ret == WAE_ERROR_NO_KEY) {
62                 ret = create_app_dek(pPkgId, appType, &pDek, &dekLen);
63         }
64
65         if (ret != WAE_ERROR_NONE) {
66                 goto error;
67         }
68
69         // encrypt
70         ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
71
72         if (ret != WAE_ERROR_NONE) {
73                 goto error;
74         }
75
76 error:
77         if (pDek != NULL)
78                 free(pDek);
79
80         return ret;
81 }
82
83 int _wae_decrypt_downloaded_web_application(const char *pPkgId, wae_app_type_e appType,
84                 const unsigned char *pData, size_t dataLen,
85                 unsigned char **ppDecryptedData, size_t *pDecDataLen)
86 {
87         int ret = WAE_ERROR_NONE;
88         unsigned char *pDek = NULL;
89         size_t dekLen = -1;
90
91         if (pPkgId == NULL) {
92                 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
93                 ret = WAE_ERROR_INVALID_PARAMETER;
94                 goto error;
95         }
96
97         if (pData == NULL || dataLen <= 0) {
98                 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
99                 ret = WAE_ERROR_INVALID_PARAMETER;
100                 goto error;
101         }
102
103         if (ppDecryptedData == NULL || pDecDataLen == NULL) {
104                 WAE_SLOGE("Invalid Parameter. ppDecryptedData or pDecDataLen is NULL");
105                 ret = WAE_ERROR_INVALID_PARAMETER;
106                 goto error;
107         }
108
109         ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
110
111         if (ret != WAE_ERROR_NONE) {
112                 goto error;
113         }
114
115         // decrypt
116         ret = decrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppDecryptedData, pDecDataLen);
117
118         if (ret != WAE_ERROR_NONE) {
119                 goto error;
120         }
121
122 error:
123         if (pDek != NULL)
124                 free(pDek);
125
126         return ret;
127 }
128
129 int _wae_encrypt_preloaded_web_application(const char *pPkgId,
130                 const unsigned char *pData, size_t dataLen,
131                 unsigned char **ppEncryptedData, size_t *pEncDataLen)
132 {
133
134         int ret = WAE_ERROR_NONE;
135         unsigned char *pDek = NULL;
136         size_t dekLen = -1;
137
138         if (pPkgId == NULL) {
139                 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
140                 ret = WAE_ERROR_INVALID_PARAMETER;
141                 goto error;
142         }
143
144         if (pData == NULL || dataLen <= 0) {
145                 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
146                 ret = WAE_ERROR_INVALID_PARAMETER;
147                 goto error;
148         }
149
150         if (ppEncryptedData == NULL || pEncDataLen == NULL) {
151                 WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
152                 ret = WAE_ERROR_INVALID_PARAMETER;
153                 goto error;
154         }
155
156         ret = get_preloaded_app_dek(pPkgId, &pDek, &dekLen);
157
158         if (ret == WAE_ERROR_NO_KEY) {
159                 ret = create_preloaded_app_dek(pPkgId, &pDek, &dekLen);
160         }
161
162         if (ret != WAE_ERROR_NONE) {
163                 goto error;
164         }
165
166         // encrypt
167         ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
168
169         if (ret != WAE_ERROR_NONE) {
170                 goto error;
171         }
172
173 error:
174         if (pDek != NULL)
175                 free(pDek);
176
177         return ret;
178 }
179
180 int _wae_decrypt_preloaded_web_application(const char *pPkgId, wae_app_type_e appType,
181                 const unsigned char *pData, size_t dataLen,
182                 unsigned char **ppDecryptedData, size_t *pDecDataLen)
183 {
184         // same with the decryption of downloaded web application
185         return _wae_decrypt_downloaded_web_application(pPkgId, appType,
186                         pData, dataLen, ppDecryptedData, pDecDataLen);
187 }
188
189 int wae_encrypt_web_application(const char *pPkgId, wae_app_type_e appType,
190                                                                 const unsigned char *pData, size_t dataLen,
191                                                                 unsigned char **ppEncryptedData, size_t *pEncDataLen)
192 {
193         int ret = WAE_ERROR_NONE;
194
195         if (appType == WAE_PRELOADED_APP)
196                 ret = _wae_encrypt_preloaded_web_application(pPkgId,
197                                 pData, dataLen, ppEncryptedData, pEncDataLen);
198         else
199                 ret = _wae_encrypt_downloaded_web_application(pPkgId, appType,
200                                 pData, dataLen, ppEncryptedData, pEncDataLen);
201
202         WAE_SLOGI("Encrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
203                           pPkgId, appType, dataLen, ret);
204         return ret;
205 }
206
207 int wae_decrypt_web_application(const char *pPkgId, wae_app_type_e appType,
208                                                                 const unsigned char *pData, size_t dataLen,
209                                                                 unsigned char **ppDecryptedData, size_t *pDecDataLen)
210 {
211         int ret = WAE_ERROR_NONE;
212
213         if (appType == WAE_PRELOADED_APP)
214                 ret = _wae_decrypt_preloaded_web_application(pPkgId, appType,
215                                 pData, dataLen, ppDecryptedData, pDecDataLen);
216         else
217                 ret = _wae_decrypt_downloaded_web_application(pPkgId, appType,
218                                 pData, dataLen, ppDecryptedData, pDecDataLen);
219
220         WAE_SLOGI("Decrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
221                           pPkgId, appType, dataLen, ret);
222         return ret;
223 }
224
225
226 int wae_remove_app_dek(const char *pPkgId, wae_app_type_e appType)
227 {
228         int ret = remove_app_dek(pPkgId, appType);
229         WAE_SLOGI("Remove APP DEK. pkgId=%s, appType=%d, ret=%d", pPkgId, appType, ret);
230         return ret;
231 }