39865bcd4931c28edbf4c3ae479249ed3bb5027d
[platform/core/security/libwebappenc.git] / srcs / web_app_enc.c
1 /*
2  *  Copyright (c) 2000 - 2015 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
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include "web_app_enc.h"
27 #include "key_handler.h"
28 #include "crypto_service.h"
29 #include "wae_log.h"
30
31
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)
35 {
36     int ret = WAE_ERROR_NONE;
37     unsigned char *pDek = NULL;
38     size_t dekLen = -1;
39
40     if(pPkgId == NULL) {
41         WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
42         ret = WAE_ERROR_INVALID_PARAMETER;
43         goto error;
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     if(ppEncryptedData == NULL || pEncDataLen == NULL) {
51         WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
52         ret = WAE_ERROR_INVALID_PARAMETER;
53         goto error;
54     }
55
56     // get APP_DEK.
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);
61     }
62     if(ret != WAE_ERROR_NONE) {
63         goto error;
64     }
65
66     // encrypt
67     ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
68     if(ret != WAE_ERROR_NONE) {
69         goto error;
70     }
71
72 error:
73     if(pDek != NULL)
74         free(pDek);
75
76     return ret;
77 }
78
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)
82 {
83     int ret = WAE_ERROR_NONE;
84     unsigned char *pDek = NULL;
85     size_t dekLen = -1;
86
87     if(pPkgId == NULL) {
88         WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
89         ret = WAE_ERROR_INVALID_PARAMETER;
90         goto error;
91     }
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;
95         goto error;
96     }
97     if(ppDecryptedData == NULL || pDecDataLen == NULL) {
98         WAE_SLOGE("Invalid Parameter. ppDecryptedData or pDecDataLen is NULL");
99         ret = WAE_ERROR_INVALID_PARAMETER;
100         goto error;
101     }
102
103     ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
104     if(ret != WAE_ERROR_NONE) {
105         goto error;
106     }
107
108     // decrypt
109     ret = decrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppDecryptedData, pDecDataLen);
110     if(ret != WAE_ERROR_NONE) {
111         goto error;
112     }
113
114 error:
115     if(pDek != NULL)
116         free(pDek);
117
118     return ret;
119 }
120
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)
124 {
125
126     int ret = WAE_ERROR_NONE;
127     unsigned char *pDek = NULL;
128     size_t dekLen = -1;
129
130     if(pPkgId == NULL) {
131         WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
132         ret = WAE_ERROR_INVALID_PARAMETER;
133         goto error;
134     }
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;
138         goto error;
139     }
140     if(ppEncryptedData == NULL || pEncDataLen == NULL) {
141         WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
142         ret = WAE_ERROR_INVALID_PARAMETER;
143         goto error;
144     }
145
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);
149     }
150     if(ret != WAE_ERROR_NONE) {
151         goto error;
152     }
153
154     // encrypt
155     ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
156     if(ret != WAE_ERROR_NONE) {
157         goto error;
158     }
159 error:
160     if(pDek != NULL)
161         free(pDek);
162
163     return ret;
164 }
165
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)
169 {
170     // same with the decryption of downloaded web application
171     return _wae_decrypt_downloaded_web_application(pPkgId, appType,
172                                                    pData, dataLen, ppDecryptedData, pDecDataLen);
173 }
174
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)
178 {
179     int ret = WAE_ERROR_NONE;
180
181     if(appType == WAE_PRELOADED_APP)
182         ret = _wae_encrypt_preloaded_web_application(pPkgId,
183                                                      pData, dataLen, ppEncryptedData, pEncDataLen);
184     else
185         ret = _wae_encrypt_downloaded_web_application(pPkgId, appType,
186                                                      pData, dataLen, ppEncryptedData, pEncDataLen);
187
188     WAE_SLOGI("Encrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
189                pPkgId, appType, dataLen, ret);
190     return ret;
191 }
192
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)
196 {
197     int ret = WAE_ERROR_NONE;
198
199     if(appType == WAE_PRELOADED_APP)
200         ret = _wae_decrypt_preloaded_web_application(pPkgId, appType,
201                                                      pData, dataLen, ppDecryptedData, pDecDataLen);
202     else
203         ret = _wae_decrypt_downloaded_web_application(pPkgId, appType,
204                                                      pData, dataLen, ppDecryptedData, pDecDataLen);
205
206     WAE_SLOGI("Decrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
207                pPkgId, appType, dataLen, ret);
208     return ret;
209 }
210
211
212 int wae_remove_app_dek(const char* pPkgId, wae_app_type_e appType)
213 {
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);
217     return ret;
218 }