abaf4aa6c45b74ac80231cecffcefa763823c45e
[platform/core/security/libwebappenc.git] / tests / wae_tests.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        wae_tests.c
18  * @author      Dongsun Lee (ds73.lee@samsung.com)
19  * @version     1.0
20  * @brief       internal test cases for libwebappenc.
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "web_app_enc.h"
29 #include "key_handler.h"
30 #include "crypto_service.h"
31
32 #include <tzplatform_config.h>
33
34 static int tc_seq = 0;
35 static int tc_succ = 0;
36 static int tc_fail = 0;
37
38 #define FPRINTF(format, args...) fprintf(stdout, format, ##args)
39
40 static int RUNTC(int (*tc_method)(), const char* tc_name)
41 {
42     int ret = WAE_ERROR_NONE;
43     FPRINTF("[%02d:%s]started...\n", tc_seq, tc_name);
44     ret = tc_method();
45     if(ret == WAE_ERROR_NONE) {
46         FPRINTF("[%02d:%s]ended. SUCCESS\n\n", tc_seq, tc_name);
47         tc_succ++;
48     } else {
49         FPRINTF("[%02d:%s]ended. FAIL. error=%d\n\n", tc_seq, tc_name, ret);
50         tc_fail++;
51     }
52     tc_seq++;
53     return ret;
54 }
55
56 static void PRINT_TC_SUMMARY()
57 {
58     FPRINTF("\n");
59     FPRINTF("===============================================\n");
60     FPRINTF(" TOTAL = %d, SUCCESS = %d, FAIL = %d\n", tc_seq, tc_succ, tc_fail);
61     FPRINTF("===============================================\n");
62 }
63
64 void _print_binary_to_hex(const char* msg, unsigned char* bin, size_t len)
65 {
66     size_t i = 0;
67     FPRINTF("%s", msg);
68     for(i=0; i<len; i++) {
69         FPRINTF("%02x", bin[i]);
70     }
71     FPRINTF("\n");
72 }
73
74 int _compare_binary(const unsigned char* b1, size_t b1Len, const unsigned char* b2, size_t b2Len)
75 {
76     size_t i = 0;
77     if(b1Len != b2Len)
78         return b1Len - b2Len;
79     for(i=0; i<b1Len; i++) {
80         if(b1[i] != b2[i])
81             return b1[i] - b2[i];
82     }
83     return 0;
84 }
85
86
87 //=================================================================================
88 // tests for crypto_service.h
89 //=================================================================================
90 int wae_tc_encrypt_decrypt_app_dek()
91 {
92     int ret = WAE_ERROR_NONE;
93     unsigned char dek[32];
94     unsigned char* encryptedDek = NULL;
95     size_t encryptedDekLen = 0;
96     unsigned char* decryptedDek = NULL;
97     size_t decryptedDekLen = 0;
98
99     const char* priKey =
100             "-----BEGIN RSA PRIVATE KEY-----\n"
101             "MIIEpgIBAAKCAQEA0kWtjpRO7Zh2KX2naVE/BDJdrfwK9xexfNA0MkY2VJ4J2AKM\n"
102             "YTj1D1jntceryupCEHOvP3rum+WsFvPXduz9+VKnSsSqj4jcTUubtpDUGA5G79Iq\n"
103             "LEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq8JkqBPoCCwtUs73ruE9VbtsBO/kT\n"
104             "lASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCwS2OTvQDNvsXfFnA0ZJEEYw/rZLir\n"
105             "j7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12YxqHdy7gnJXodLhvE/cR4SN9VW7+qmC\n"
106             "MBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4+wIDAQABAoIBAQCwxqV/vc2RUGDe\n"
107             "xuXM0+IvrAw37jJlw4SS0xNexMp+XxMViCbuwYy851h96azS/himbiuCKd6aL/96\n"
108             "mGunbtyiFEvSvv5Jh5z2Wr9BQAcfZjla+4w7BIsg9UNifE/OfgLsQBu34xhsHtfK\n"
109             "7nFehCOl/I5n+qtnD5KZPe0DWacQdwY4vEAj6YyXdb2bBg+MiwE9KVxGEIUDbklh\n"
110             "Is70JXczjLZCS+lIpOKh0/lbZmBZePoUbVTtS+GvtPTpQC/aTHRkwGoEtuPEWpbL\n"
111             "0Q1d6zO+vDJVLJlb5FF2haghs8IlqAxkkPjeUTNye+WktRrDQxmPu/blbxQrygfq\n"
112             "Au5tBnsxAoGBAOiVtcpg32puo3Yq2Y78oboe9PuHaQP0d3DhwP3/7J0BeNslpjW7\n"
113             "E1LWsVsCanxTE8XPUdFfAWgMk7lQqESN0wawGmSmWk+eQPZdjHanBaC8vh7aKjo6\n"
114             "q9FdT1DKjrRi23QyDco3f3E7hvM93IAAhw1ikNu8DT19JAxtdeMh5WAZAoGBAOdw\n"
115             "6neEvIFXh3RWEv2/GKVhVR8mxDqxmuFdXpOF+YWsK0Tg4uC8jm9kUGnwXgT2Mjke\n"
116             "oAwYAFcRbHQQGsxy/vkV16kv4aurTE2hMpjeXCAakwV0Pi2w1f9WnDokjgORkOmc\n"
117             "+QK9I8egdFPMVDfQjhLslhSUY0Eb4qcJ6q9WxfQzAoGBANSsAFybk+7oWAO3TtQW\n"
118             "YXOk1vIgcYAyS/0mEKixGZS/QdlxZbf/5b17nxTO8rvX416fIftG2ixgQ7vR6us0\n"
119             "m9+jq56ZFj9zP4eHJudf9h9yNo5TgwVXnMCGh/4iGbcMJgrrsfxUHu5VNiK5UCSj\n"
120             "VtqAZGDoZVryUMIkXQVhezIRAoGBAN7QUIqcGbcUA24257Wu4hVlrUN+WPCAyDEr\n"
121             "aL/x/ZV5eXaoYwQlw6LuGpTDOmDgfN2M5FyARuOL/LOIRaSLGXnIU4WoeUSCd8VM\n"
122             "6Z9Og7bMnrpjfPEUDBH02hcH1kkNPUwLOZgva2Dm0tdSIcpSWFVTu/E4Io4uQHi8\n"
123             "DVqc2ZsNAoGBAJT76ezXNSSv8hnrKqTpwgTicpqhRZ3eFQjyl4HRL26AJMKv++x8\n"
124             "4/IsVIwxaHzpbN3nnCjmAHV4gX9YpxVnvYcZflC9WZeDkwNMLmPYb3Zg27EzSMfQ\n"
125             "8yrfWJZo3qobipcHf1yohAt4fHk9kUKtPHEwp0xKe//rfhswLb3VCzvQ\n"
126             "-----END RSA PRIVATE KEY-----";
127     const char* pubKey =
128             "-----BEGIN PUBLIC KEY-----\n"
129             "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0kWtjpRO7Zh2KX2naVE/\n"
130             "BDJdrfwK9xexfNA0MkY2VJ4J2AKMYTj1D1jntceryupCEHOvP3rum+WsFvPXduz9\n"
131             "+VKnSsSqj4jcTUubtpDUGA5G79IqLEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq\n"
132             "8JkqBPoCCwtUs73ruE9VbtsBO/kTlASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCw\n"
133             "S2OTvQDNvsXfFnA0ZJEEYw/rZLirj7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12Yxq\n"
134             "Hdy7gnJXodLhvE/cR4SN9VW7+qmCMBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4\n"
135             "+wIDAQAB\n"
136             "-----END PUBLIC KEY-----";
137
138     ret = encrypt_app_dek((const unsigned char*) pubKey, strlen(pubKey),
139                           dek, sizeof(dek), &encryptedDek, &encryptedDekLen);
140     if(ret != WAE_ERROR_NONE){
141         FPRINTF("...FAIL: encrypt_app_dek. ret=%d\n", ret);
142         ret = WAE_ERROR_CRYPTO;
143         goto error;
144     }
145
146     ret = decrypt_app_dek((const unsigned char*) priKey, strlen(priKey), NULL,
147                           encryptedDek, encryptedDekLen, &decryptedDek, &decryptedDekLen);
148     if(ret != WAE_ERROR_NONE){
149         FPRINTF("...FAIL: decrypt_app_dek. ret=%d\n", ret);
150         ret = WAE_ERROR_CRYPTO;
151         goto error;
152     }
153
154     _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
155     _print_binary_to_hex("...ENC  DEK= ", encryptedDek, encryptedDekLen);
156     _print_binary_to_hex("...DEC  DEK= ", decryptedDek, decryptedDekLen);
157
158     if(_compare_binary(dek, sizeof(dek), decryptedDek, decryptedDekLen) != 0) {
159         FPRINTF("...FAIL: ORIG DEK != decrypted DEK\n");
160         ret = WAE_ERROR_CRYPTO;
161         goto error;
162     }
163
164     ret = WAE_ERROR_NONE;
165
166 error:
167     if(encryptedDek != NULL)
168         free(encryptedDek);
169     if(decryptedDek != NULL)
170         free(decryptedDek);
171
172     return ret;
173 }
174
175 int wae_tc_encrypt_decrypt_aes_cbc()
176 {
177     int ret = WAE_ERROR_NONE;
178
179     unsigned char dek[32] = {0, };
180     size_t keyLen = 32;
181     const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
182     size_t plaintextLen = strlen(plaintext);
183     unsigned char* encrypted = NULL;
184     size_t encLen = 0;
185     unsigned char* decrypted = NULL;
186     size_t decLen = 0;
187     char decrypted_str[1024] = {0, };
188
189     ret = _get_random(keyLen, dek);
190
191     ret = encrypt_aes_cbc(dek, keyLen, (const unsigned char*)plaintext, plaintextLen, &encrypted, &encLen);
192     if(ret != WAE_ERROR_NONE){
193         FPRINTF("...FAIL: encrypt_aes_cbc. ret=%d\n", ret);
194         ret = WAE_ERROR_CRYPTO;
195         goto error;
196     }
197
198     ret = decrypt_aes_cbc(dek, keyLen, encrypted, encLen, &decrypted, &decLen);
199     if(ret != WAE_ERROR_NONE){
200         FPRINTF("...FAIL: decrypt_aes_cbc. ret=%d\n", ret);
201         ret = WAE_ERROR_CRYPTO;
202         goto error;
203     }
204
205     if(plaintextLen != decLen) {
206         FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", (int) plaintextLen, (int) decLen);
207         ret = WAE_ERROR_CRYPTO;
208         goto error;
209     }
210
211     memcpy(decrypted_str, decrypted, decLen);
212     FPRINTF("...plaintext = %s\n", plaintext);
213     FPRINTF("...decrypted = %s\n", decrypted_str);
214     if(strcmp(plaintext, decrypted_str) != 0) {
215         FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
216         ret = WAE_ERROR_CRYPTO;
217         goto error;
218     }
219
220 error:
221     if(encrypted != NULL)
222         free(encrypted);
223     if(decrypted != NULL)
224         free(decrypted);
225
226     return ret;
227 }
228
229 //=================================================================================
230 // tests for key_handler.h
231 //=================================================================================
232 int wae_tc_cache()
233 {
234     int ret = WAE_ERROR_NONE;
235
236     const char* pkg1 = "pkg1";
237     const char* pkg2 = "pkg2";
238     const char* pkg3 = "pkg3";
239     const char* pkgDummy = "dummy";
240
241     unsigned char dek1[32] = {1, };
242     unsigned char dek2[32] = {2, };
243     unsigned char dek3[32] = {3, };
244     unsigned char* retDek = NULL;
245
246     _initialize_cache();
247
248     _add_app_dek_to_cache(pkg1, dek1);
249     _add_app_dek_to_cache(pkg2, dek2);
250     _add_app_dek_to_cache(pkg3, dek3);
251
252     retDek = NULL;
253     retDek = _get_app_dek_from_cache(pkg1);
254     if(retDek == NULL || _compare_binary(dek1, 32, retDek, 32) != 0) {
255         FPRINTF("failed in cache. Diffent DEK1\n");
256         ret = WAE_ERROR_UNKNOWN;
257         goto error;
258     }
259     _print_binary_to_hex("...DEK1         : ", dek1, 32);
260     _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
261
262     retDek = NULL;
263     retDek = _get_app_dek_from_cache(pkg2);
264     if(retDek == NULL || _compare_binary(dek2, 32, retDek, 32) != 0) {
265         FPRINTF("failed in cache. Diffent DEK2\n");
266         ret = WAE_ERROR_UNKNOWN;
267         goto error;
268     }
269     _print_binary_to_hex("...DEK2         : ", dek2, 32);
270     _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
271
272     retDek = NULL;
273     retDek = _get_app_dek_from_cache(pkg3);
274     if(retDek == NULL || _compare_binary(dek3, 32, retDek, 32) != 0) {
275         FPRINTF("failed in cache. Diffent DEK3\n");
276         ret = WAE_ERROR_UNKNOWN;
277         goto error;
278     }
279     _print_binary_to_hex("...DEK3         : ", dek3, 32);
280     _print_binary_to_hex("...Returen DEK3 : ", retDek, 32);
281
282     retDek = NULL;
283     retDek = _get_app_dek_from_cache(pkgDummy);
284     if(retDek != NULL) {
285         FPRINTF("failed in cache. Wrong DEK_DUMMY1 returned\n");
286         _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
287         ret = WAE_ERROR_UNKNOWN;
288         goto error;
289     }
290
291     _remove_app_dek_from_cache(pkg3);
292     retDek = NULL;
293     retDek = _get_app_dek_from_cache(pkg3);
294     if(retDek != NULL) {
295         FPRINTF("fail to remove app dek from cache\n");
296         ret = WAE_ERROR_UNKNOWN;
297         goto error;
298     }
299
300     _initialize_cache();
301
302     _add_app_dek_to_cache(pkg1, dek1);
303
304     retDek = NULL;
305     retDek = _get_app_dek_from_cache(pkg2);
306     if(retDek != NULL) {
307         FPRINTF("failed in cache. Wrong DEK_DUMMY2 returned\n");
308         _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
309         ret = WAE_ERROR_UNKNOWN;
310         goto error;
311     }
312
313     ret = WAE_ERROR_NONE;
314 error:
315     return ret;
316 }
317
318 int wae_tc_get_random()
319 {
320     int ret = WAE_ERROR_NONE;
321
322     size_t rand_len = 32;
323     unsigned char random[32] = {0, };
324
325     ret = _get_random(rand_len, random);
326
327     _print_binary_to_hex("...RANDOM = ", random, sizeof(random));
328
329     return ret;
330 }
331
332 int wae_tc_get_alias()
333 {
334     int ret = WAE_ERROR_NONE;
335
336     const char* pkgId = "TEST_PKG_ID";
337     char alias[256] = {0, };
338
339     _get_alias(pkgId, WAE_DOWNLOADED_NORMAL_APP, true, alias, sizeof(alias));
340     FPRINTF("...pkgid=%s, alias for normal for save. app=%s\n", pkgId, alias);
341
342     _get_alias(pkgId, WAE_DOWNLOADED_NORMAL_APP, false, alias, sizeof(alias));
343     FPRINTF("...pkgid=%s, alias for normal for get. app=%s\n", pkgId, alias);
344
345     _get_alias(pkgId, WAE_DOWNLOADED_GLOBAL_APP, true, alias, sizeof(alias));
346     FPRINTF("...pkgid=%s, alias for global app=%s\n", pkgId, alias);
347
348     _get_alias(pkgId, WAE_PRELOADED_APP, true, alias, sizeof(alias));
349     FPRINTF("...pkgid=%s, alias for preloaded app=%s\n", pkgId, alias);
350
351     return ret;
352 }
353
354 int _wae_tc_add_get_remove_dek(wae_app_type_e appType)
355 {
356     int ret = WAE_ERROR_NONE;
357
358     const char* pkgId = "TEST_PKG_ID";
359
360     size_t dekLen= 32;
361     unsigned char dek[32] = {0, };
362     size_t storedDekLen = 0;
363     unsigned char* storedDek = NULL;
364
365     ret = _get_random(dekLen, dek);
366
367     remove_app_dek(pkgId, appType);
368
369     ret = _add_dek_to_key_manager(pkgId, appType, dek, dekLen);
370     if(ret != WAE_ERROR_NONE) {
371         FPRINTF("...FAIL: _add_dek_to_key_manager. ret=%d\n", ret);
372         goto error;
373     }
374
375     ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
376     if(ret != WAE_ERROR_NONE) {
377         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
378         goto error;
379     }
380
381     if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
382         ret = WAE_ERROR_KEY_MANAGER;
383         FPRINTF("...FAIL: DEK != STORED_DEK.\n");
384         goto error;
385     }
386
387     ret = remove_app_dek(pkgId, appType);
388     if(ret != WAE_ERROR_NONE) {
389         FPRINTF("...FAIL: remove_app_dek. ret=%d\n", ret);
390         goto error;
391     }
392
393     ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
394     if(ret == WAE_ERROR_NONE) {
395         ret = WAE_ERROR_UNKNOWN;
396         FPRINTF("...FAIL: APP DEK still exists in key_manager.\n");
397         goto error;
398     }
399
400     ret = WAE_ERROR_NONE;
401 error:
402     if(storedDek != NULL)
403         free(storedDek);
404
405     return ret;
406 }
407
408 int wae_tc_add_get_remove_dek_for_normal_app()
409 {
410     return _wae_tc_add_get_remove_dek(WAE_DOWNLOADED_NORMAL_APP);
411 }
412
413 int wae_tc_add_get_remove_dek_for_global_app()
414 {
415     return _wae_tc_add_get_remove_dek(WAE_DOWNLOADED_GLOBAL_APP);
416 }
417
418 int wae_tc_add_get_remove_dek_for_preloaded_app()
419 {
420     return _wae_tc_add_get_remove_dek(WAE_PRELOADED_APP);
421 }
422
423 int wae_tc_get_preloaded_app_dek_file_path()
424 {
425     int ret = WAE_ERROR_NONE;
426
427     const char *pkgId = "test_pkg";
428     const char *expectedPath = tzplatform_mkpath4(TZ_SYS_SHARE,
429                                     "wae", "app_dek", "WAE_APP_DEK_test_pkg.adek");
430     char path[256];
431
432     FPRINTF("...expected path : %s\n", expectedPath);
433     ret = _get_preloaded_app_dek_file_path(pkgId, sizeof(path), path);
434     FPRINTF("...returned path : %s\n", path);
435
436     if(ret != WAE_ERROR_NONE || strncmp(expectedPath, path, strlen(expectedPath)) != 0) {
437         ret = WAE_ERROR_UNKNOWN;
438         goto error;
439     }
440 error:
441     return ret;
442 }
443
444 int wae_tc_extract_pkg_id_from_file_name()
445 {
446     int ret = WAE_ERROR_NONE;
447     const char* fileName = "WAE_APP_DEK_test_pkg.adek";
448     const char* expectedPkgId = "test_pkg";
449     char pkgId[100];
450
451     ret = _extract_pkg_id_from_file_name(fileName, pkgId);
452     FPRINTF("...expected pkgId: %s\n", expectedPkgId);
453     FPRINTF("...returned pkgId: %s\n", pkgId);
454
455     if(ret != WAE_ERROR_NONE || strncmp(expectedPkgId, pkgId, strlen(expectedPkgId)) != 0) {
456         ret = WAE_ERROR_UNKNOWN;
457         goto error;
458     }
459 error:
460     return ret;
461
462 }
463
464 int wae_tc_read_write_encrypted_app_dek()
465 {
466     int ret = WAE_ERROR_NONE;
467     const char* pkgId = "write_test_pkg";
468     unsigned char dek[256];
469     unsigned char* readDek = NULL;
470     size_t readDekLen = 0;
471
472     ret = _write_encrypted_app_dek_to_file(pkgId, dek, sizeof(dek));
473     if(ret != WAE_ERROR_NONE) {
474         FPRINTF("Fail to _write_encrypted_app_dek_to_file. pkgId=%s\n", pkgId);
475         goto error;
476     }
477
478     ret = _read_encrypted_app_dek_from_file(pkgId, &readDek, &readDekLen);
479     if(ret != WAE_ERROR_NONE) {
480         FPRINTF("Fail to _read_encrypted_app_dek_from_file. pkgId=%s\n", pkgId);
481         goto error;
482     }
483
484     _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
485     _print_binary_to_hex("...READ DEK= ", readDek, readDekLen);
486     if(_compare_binary(dek, sizeof(dek), readDek, readDekLen) != 0 ) {
487         ret = WAE_ERROR_UNKNOWN;
488         FPRINTF("...FAIL: DEK != read_DEK.\n");
489         goto error;
490     }
491
492 error:
493     if(readDek != NULL)
494         free(readDek);
495     return ret;
496 }
497
498
499 int _wae_tc_create_app_dek(wae_app_type_e appType)
500 {
501     int ret = WAE_ERROR_NONE;
502
503     const char* pkgId = "TEST_PKG_ID";
504     unsigned char* dek = NULL;
505     size_t dekLen = 0;
506
507     size_t storedDekLen = 0;
508     unsigned char* storedDek = NULL;
509
510     remove_app_dek(pkgId, appType);
511
512     ret = create_app_dek(pkgId, appType, &dek, &dekLen);
513     if(ret != WAE_ERROR_NONE) {
514         FPRINTF("...FAIL: create_app_dek. ret=%d\n", ret);
515         goto error;
516     }
517
518     ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
519     if(ret != WAE_ERROR_NONE) {
520         ret = WAE_ERROR_KEY_MANAGER;
521         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
522         goto error;
523     }
524
525     _print_binary_to_hex("...CREATED  DEK = ", dek, dekLen);
526     _print_binary_to_hex("...STORED   DEK = ", storedDek, storedDekLen);
527     if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
528         ret = WAE_ERROR_FILE;
529         FPRINTF("...FAIL: DEK != STORED_DEK.\n");
530         goto error;
531     }
532
533     remove_app_dek(pkgId, appType);
534
535     ret = WAE_ERROR_NONE;
536 error:
537     if(dek != NULL)
538         free(dek);
539     if(storedDek != NULL)
540         free(storedDek);
541     return ret;
542 }
543
544 int wae_tc_create_app_dek_for_normal_app()
545 {
546     return _wae_tc_create_app_dek(WAE_DOWNLOADED_NORMAL_APP);
547 }
548
549 int wae_tc_create_app_dek_for_global_app()
550 {
551     return _wae_tc_create_app_dek(WAE_DOWNLOADED_GLOBAL_APP);
552 }
553
554 int wae_tc_create_app_dek_for_preloaded_app()
555 {
556     return _wae_tc_create_app_dek(WAE_PRELOADED_APP);
557 }
558
559 int wae_tc_get_create_preloaded_app_dek()
560 {
561     int ret = WAE_ERROR_NONE;
562
563     const char* pkgId = "TEST_PKG_ID_FOR_CREATE";
564     unsigned char *dek = NULL;
565     unsigned char *readDek = NULL;
566     size_t readDekLen = 0;
567     size_t dekLen = 0;
568
569     ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
570     if(ret != WAE_ERROR_NO_KEY) {
571         FPRINTF("...FAIL: There should be no APP DEK.  get_preloaded_app_dek. ret=%d\n", ret);
572         ret = WAE_ERROR_FILE;
573         goto error;
574     }
575
576     ret = create_preloaded_app_dek(pkgId, &dek, &dekLen);
577     if(ret != WAE_ERROR_NONE) {
578         FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
579         goto error;
580     }
581
582     ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
583     if(ret != WAE_ERROR_NONE) {
584         FPRINTF("...FAIL: get_preloaded_app_dek. ret=%d\n", ret);
585         goto error;
586     }
587
588     _print_binary_to_hex("...CREATED DEK = ", dek, dekLen);
589     _print_binary_to_hex("...READ    DEK = ", readDek, readDekLen);
590
591     if(_compare_binary(dek, dekLen, readDek, readDekLen) != 0 ) {
592         ret = WAE_ERROR_FILE;
593         FPRINTF("...FAIL: DEK != READ_DEK.\n");
594         goto error;
595     }
596
597     ret = WAE_ERROR_NONE;
598 error:
599     if(dek != NULL)
600         free(dek);
601     if(readDek != NULL)
602         free(readDek);
603     return ret;
604 }
605
606 int wae_tc_load_preloaded_app_deks()
607 {
608     int ret = WAE_ERROR_NONE;
609
610     const char* pkgId1 = "TEST_PKGID_1";
611     unsigned char* dek1 = NULL;
612     size_t dekLen1 = 0;
613     unsigned char* readDek1 = NULL;
614     size_t readDekLen1 = 0;
615     char path1[MAX_PATH_LEN] = {0, };
616
617     const char* pkgId2 = "TEST_PKGID_2";
618     unsigned char* dek2 = NULL;
619     size_t dekLen2 = 0;
620     unsigned char* readDek2 = NULL;
621     size_t readDekLen2 = 0;
622     char path2[MAX_PATH_LEN] = {0, };
623
624     _get_preloaded_app_dek_file_path(pkgId1, sizeof(path1), path1);
625     _get_preloaded_app_dek_file_path(pkgId2, sizeof(path2), path2);
626
627     // remove old test data
628     remove_app_dek(pkgId1, WAE_PRELOADED_APP);
629     remove_app_dek(pkgId2, WAE_PRELOADED_APP);
630     unlink(path1);
631     unlink(path2);
632
633     // create 2 dek for preloaded app
634     ret = create_preloaded_app_dek(pkgId1, &dek1, &dekLen1);
635     if(ret != WAE_ERROR_NONE) {
636         FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
637         goto error;
638     }
639
640     ret = create_preloaded_app_dek(pkgId2, &dek2, &dekLen2);
641     if(ret != WAE_ERROR_NONE) {
642         FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
643         goto error;
644     }
645
646     // load_preloaded_app_deks
647     ret = load_preloaded_app_deks(true);
648     if(ret != WAE_ERROR_NONE) {
649         FPRINTF("...FAIL: load_preloaded_app_deks. ret=%d\n", ret);
650         goto error;
651     }
652
653     // get_app_dek
654     ret = get_app_dek(pkgId1, WAE_PRELOADED_APP, &readDek1, &readDekLen1);
655     if(ret != WAE_ERROR_NONE) {
656         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
657         goto error;
658     }
659
660     ret = get_app_dek(pkgId2, WAE_PRELOADED_APP, &readDek2, &readDekLen2);
661     if(ret != WAE_ERROR_NONE) {
662         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
663         goto error;
664     }
665
666     _print_binary_to_hex("...CREATED DEK1 = ", dek1, dekLen1);
667     _print_binary_to_hex("...READ    DEK1 = ", readDek1, readDekLen1);
668     if(_compare_binary(dek1, dekLen1, readDek1, readDekLen1) != 0 ) {
669         ret = WAE_ERROR_FILE;
670         FPRINTF("...FAIL: DEK1 != READ_DEK1.\n");
671         goto error;
672     }
673
674     _print_binary_to_hex("...CREATED DEK2 = ", dek2, dekLen2);
675     _print_binary_to_hex("...READ    DEK2 = ", readDek2, readDekLen2);
676     if(_compare_binary(dek2, dekLen2, readDek2, readDekLen2) != 0 ) {
677         ret = WAE_ERROR_FILE;
678         FPRINTF("...FAIL: DEK2 != READ_DEK2.\n");
679         goto error;
680     }
681
682     // remove_app_dek
683     remove_app_dek(pkgId1, WAE_PRELOADED_APP);
684     remove_app_dek(pkgId2, WAE_PRELOADED_APP);
685
686     ret = WAE_ERROR_NONE;
687 error:
688     if(dek1 != NULL)
689         free(dek1);
690     if(readDek1 != NULL)
691         free(readDek1);
692     if(dek2 != NULL)
693         free(dek2);
694     if(readDek2 != NULL)
695         free(readDek2);
696
697     return ret;
698 }
699
700 int _wae_tc_encrypt_decrypt_web_app(wae_app_type_e appType)
701 {
702     int ret = WAE_ERROR_NONE;
703
704     const char* pkgId1 = "testpkg_for_normal";
705     const char* pkgId2 = "testpkg_for_global";
706     const char* pkgId3 = "testpkg_for_preloaded";
707     const char* pkgId = NULL;
708     const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
709     size_t plaintextLen = strlen(plaintext);
710     unsigned char* encrypted = NULL;
711     size_t encLen = 0;
712     unsigned char* decrypted = NULL;
713     size_t decLen = 0;
714     char decrypted_str[1024] = {0, };
715
716     switch(appType) {
717         case WAE_DOWNLOADED_NORMAL_APP:
718             pkgId = pkgId1;
719             break;
720         case WAE_DOWNLOADED_GLOBAL_APP:
721             pkgId = pkgId2;
722             break;
723         case WAE_PRELOADED_APP:
724             pkgId = pkgId3;
725             break;
726     }
727
728     // remove old test data
729     ret = wae_remove_app_dek(pkgId, appType);
730     if(appType == WAE_PRELOADED_APP) {
731         _clear_app_deks_loaded();
732     }
733
734     // test for downloaded web application
735     ret = wae_encrypt_web_application(pkgId, appType,
736                                       (const unsigned char*)plaintext, plaintextLen,
737                                       &encrypted, &encLen);
738     if(ret != WAE_ERROR_NONE){
739         FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
740         goto error;
741     }
742
743     // encrypt test twice
744     ret = wae_encrypt_web_application(pkgId, appType,
745                                       (const unsigned char*)plaintext, plaintextLen,
746                                       &encrypted, &encLen);
747     if(ret != WAE_ERROR_NONE){
748         FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
749         goto error;
750     }
751
752     _remove_app_dek_from_cache(pkgId);
753
754     if(appType == WAE_PRELOADED_APP) {
755         load_preloaded_app_deks(true);
756     }
757
758     ret = wae_decrypt_web_application(pkgId, appType, encrypted, encLen, &decrypted, &decLen);
759     if(ret != WAE_ERROR_NONE){
760         FPRINTF("...FAIL: wae_decrypt_web_application. ret=%d\n", ret);
761         goto error;
762     }
763
764     if(plaintextLen != decLen) {
765         FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", (int) plaintextLen, (int) decLen);
766         ret = WAE_ERROR_CRYPTO;
767         goto error;
768     }
769
770     memcpy(decrypted_str, decrypted, decLen);
771     FPRINTF("...plaintext(downloaded) = %s\n", plaintext);
772     FPRINTF("...decrypted(downloaded) = %s\n", decrypted_str);
773     if(strcmp(plaintext, decrypted_str) != 0) {
774         FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
775         ret = WAE_ERROR_CRYPTO;
776         goto error;
777     }
778
779     ret = wae_remove_app_dek(pkgId, appType);
780     if(ret != WAE_ERROR_NONE){
781         FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
782         goto error;
783     }
784
785 error:
786     if(encrypted != NULL)
787         free(encrypted);
788     if(decrypted != NULL)
789         free(decrypted);
790
791     return ret;
792 }
793
794 int wae_tc_encrypt_decrypt_normal_app()
795 {
796     return _wae_tc_encrypt_decrypt_web_app(WAE_DOWNLOADED_NORMAL_APP);
797 }
798
799 int wae_tc_encrypt_decrypt_global_app()
800 {
801     return _wae_tc_encrypt_decrypt_web_app(WAE_DOWNLOADED_GLOBAL_APP);
802 }
803
804 int wae_tc_encrypt_decrypt_preloaded_app()
805 {
806     return _wae_tc_encrypt_decrypt_web_app(WAE_PRELOADED_APP);
807 }
808
809
810 int run_test_cases(char* test_mode)
811 {
812     if(strcmp(test_mode, "system") == 0) {
813         RUNTC(wae_tc_encrypt_decrypt_app_dek, "wae_tc_encrypt_decrypt_app_dek");
814         RUNTC(wae_tc_encrypt_decrypt_aes_cbc, "wae_tc_encrypt_decrypt_aes_cbc");
815         RUNTC(wae_tc_cache, "wae_tc_cache");
816
817         RUNTC(wae_tc_get_random, "wae_tc_get_random");
818         RUNTC(wae_tc_get_alias, "wae_tc_get_alias");
819
820         RUNTC(wae_tc_add_get_remove_dek_for_global_app, "wae_tc_add_get_remove_dek_for_global_app");
821         RUNTC(wae_tc_add_get_remove_dek_for_preloaded_app, "wae_tc_add_get_remove_dek_for_preloaded_app");
822
823         RUNTC(wae_tc_get_preloaded_app_dek_file_path, "wae_tc_get_preloaded_app_dek_file_path");
824         RUNTC(wae_tc_extract_pkg_id_from_file_name, "wae_tc_extract_pkg_id_from_file_name");
825         RUNTC(wae_tc_read_write_encrypted_app_dek, "wae_tc_read_write_encrypted_app_dek");
826
827         RUNTC(wae_tc_create_app_dek_for_global_app, "wae_tc_create_app_dek_for_global_app");
828         RUNTC(wae_tc_create_app_dek_for_preloaded_app, "wae_tc_create_app_dek_for_preloaded_app");
829
830         RUNTC(wae_tc_get_create_preloaded_app_dek, "wae_tc_get_create_preloaded_app_dek");
831         RUNTC(wae_tc_load_preloaded_app_deks, "wae_tc_load_preloaded_app_deks");
832
833         RUNTC(wae_tc_encrypt_decrypt_global_app, "wae_tc_encrypt_decrypt_global_app");
834         RUNTC(wae_tc_encrypt_decrypt_preloaded_app, "wae_tc_encrypt_decrypt_preloaded_app");
835     }else {
836         RUNTC(wae_tc_add_get_remove_dek_for_normal_app, "wae_tc_add_get_remove_dek_for_normal_app");
837         RUNTC(wae_tc_create_app_dek_for_normal_app, "wae_tc_create_app_dek_for_normal_app");
838         RUNTC(wae_tc_encrypt_decrypt_normal_app, "wae_tc_encrypt_decrypt_normal_app");
839     }
840
841     PRINT_TC_SUMMARY();
842     return 0;
843 }
844
845 int main(int argc, char* argv[])
846 {
847     int ret = 0;
848
849     if(argc != 2 || (strcmp(argv[1],"system") != 0 && strcmp(argv[1],"user")) ) {
850         FPRINTF("invalid command formant.  command format : %s system|user\n", argv[0]);
851         exit(1);
852     }
853
854     ret = run_test_cases(argv[1]);
855
856     return ret;
857 }