1 /*
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hksfiletransfer_fuzzer.h"
17
18 #include "hks_config_parser.h"
19 #include "hks_file_transfer.h"
20 #include "hks_log.h"
21 #include "hks_mem.h"
22 #include "hks_param.h"
23 #include "hks_type_inner.h"
24
25 #undef HUKS_SA_UPGRADE_CONFIG
26 #undef HUKS_HAP_UPGRADE_CONFIG
27 #undef HUKS_SA_SKIP_UPGRADE_CONFIG
28 #undef HUKS_HAP_SKIP_UPGRADE_CONFIG
29
30 #define HUKS_SA_UPGRADE_CONFIG { { 6, false, false }, { 7, false, true }, { 8, true, false }, { 9, true, true } }
31 #define HUKS_HAP_UPGRADE_CONFIG { { "com.example.demo1", true, true }, \
32 { "com.example.demo2", true, false }, \
33 { "com.example.demo3", false, true }, \
34 { "com.example.demo4", false, false } }
35 #define HUKS_SA_SKIP_UPGRADE_CONFIG { 0, 10, 99 }
36 #define HUKS_HAP_SKIP_UPGRADE_CONFIG { "com.example.skip1", "com.example.skip2" }
37
38 #include "hks_config_parser.c"
39
40 namespace OHOS {
41 namespace Security {
42 namespace Hks {
43
44 enum HksAtType g_accessTokenType = HKS_TOKEN_HAP;
45 char *g_hapName = nullptr;
46
HksServiceUpgradeConfigParserTest001()47 static void HksServiceUpgradeConfigParserTest001()
48 {
49 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest001");
50 g_hapName = const_cast<char *>("com.example.demo1");
51 g_accessTokenType = HKS_TOKEN_HAP;
52 struct HksParamSet *paramSet001 = nullptr;
53 (void)HksInitParamSet(¶mSet001);
54 uint32_t userId001 = 0;
55 struct HksParam params[] = {
56 {
57 .tag = HKS_TAG_PROCESS_NAME,
58 .blob = {
59 .data = reinterpret_cast<uint8_t *>(&userId001),
60 .size = sizeof(uint32_t)
61 }
62 }, {
63 .tag = HKS_TAG_USER_ID,
64 .uint32Param = userId001
65 }, {
66 .tag = HKS_TAG_ACCESS_TOKEN_ID,
67 .uint64Param = 0
68 }
69 };
70 (void)HksAddParams(paramSet001, params, HKS_ARRAY_SIZE(params));
71 (void)HksBuildParamSet(¶mSet001);
72 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet001),
73 .size = paramSet001->paramSetSize };
74 struct HksUpgradeFileTransferInfo info = { 0 };
75 (void)HksParseConfig("", &fileContent, &info);
76
77 HksFreeParamSet(¶mSet001);
78 }
79
HksServiceUpgradeConfigParserTest002()80 static void HksServiceUpgradeConfigParserTest002()
81 {
82 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest002");
83 g_hapName = const_cast<char *>("com.example.demo2");
84 g_accessTokenType = HKS_TOKEN_HAP;
85 struct HksParamSet *paramSet002 = nullptr;
86 (void)HksInitParamSet(¶mSet002);
87 uint32_t userId002 = 100;
88 struct HksParam params[] = {
89 {
90 .tag = HKS_TAG_PROCESS_NAME,
91 .blob = {
92 .data = reinterpret_cast<uint8_t *>(&userId002),
93 .size = sizeof(uint32_t)
94 }
95 }, {
96 .tag = HKS_TAG_USER_ID,
97 .uint32Param = userId002
98 }, {
99 .tag = HKS_TAG_ACCESS_TOKEN_ID,
100 .uint64Param = 0
101 }
102 };
103 (void)HksAddParams(paramSet002, params, HKS_ARRAY_SIZE(params));
104 (void)HksBuildParamSet(¶mSet002);
105 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet002),
106 .size = paramSet002->paramSetSize };
107 struct HksUpgradeFileTransferInfo info = { 0 };
108 (void)HksParseConfig("", &fileContent, &info);
109
110 HksFreeParamSet(¶mSet002);
111 }
112
HksServiceUpgradeConfigParserTest003()113 static void HksServiceUpgradeConfigParserTest003()
114 {
115 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest003");
116 g_hapName = const_cast<char *>("com.example.demo3");
117 g_accessTokenType = HKS_TOKEN_HAP;
118 struct HksParamSet *paramSet003 = nullptr;
119 (void)HksInitParamSet(¶mSet003);
120 uint32_t userId003 = 100;
121 struct HksParam params[] = {
122 {
123 .tag = HKS_TAG_PROCESS_NAME,
124 .blob = {
125 .data = reinterpret_cast<uint8_t *>(&userId003),
126 .size = sizeof(uint32_t)
127 }
128 }, {
129 .tag = HKS_TAG_USER_ID,
130 .uint32Param = userId003
131 }, {
132 .tag = HKS_TAG_ACCESS_TOKEN_ID,
133 .uint64Param = 0
134 }
135 };
136 (void)HksAddParams(paramSet003, params, HKS_ARRAY_SIZE(params));
137 (void)HksBuildParamSet(¶mSet003);
138 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet003),
139 .size = paramSet003->paramSetSize };
140 struct HksUpgradeFileTransferInfo info = { 0 };
141 (void)HksParseConfig("", &fileContent, &info);
142
143 HksFreeParamSet(¶mSet003);
144 }
145
HksServiceUpgradeConfigParserTest004()146 static void HksServiceUpgradeConfigParserTest004()
147 {
148 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest004");
149 g_hapName = const_cast<char *>("com.example.demo4");
150 g_accessTokenType = HKS_TOKEN_HAP;
151 struct HksParamSet *paramSet004 = nullptr;
152 (void)HksInitParamSet(¶mSet004);
153 uint32_t userId004 = 100;
154 struct HksParam params[] = {
155 {
156 .tag = HKS_TAG_PROCESS_NAME,
157 .blob = {
158 .data = reinterpret_cast<uint8_t *>(&userId004),
159 .size = sizeof(uint32_t)
160 }
161 }, {
162 .tag = HKS_TAG_USER_ID,
163 .uint32Param = userId004
164 }, {
165 .tag = HKS_TAG_ACCESS_TOKEN_ID,
166 .uint64Param = 0
167 }
168 };
169 (void)HksAddParams(paramSet004, params, HKS_ARRAY_SIZE(params));
170 (void)HksBuildParamSet(¶mSet004);
171 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet004),
172 .size = paramSet004->paramSetSize };
173 struct HksUpgradeFileTransferInfo info = { 0 };
174 (void)HksParseConfig("", &fileContent, &info);
175
176 HksFreeParamSet(¶mSet004);
177 }
178
HksServiceUpgradeConfigParserTest005()179 static void HksServiceUpgradeConfigParserTest005()
180 {
181 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest005");
182 g_hapName = const_cast<char *>("com.example.skip1");
183 g_accessTokenType = HKS_TOKEN_HAP;
184 struct HksParamSet *paramSet005 = nullptr;
185 (void)HksInitParamSet(¶mSet005);
186 uint32_t userId005 = 100;
187 struct HksParam params[] = {
188 {
189 .tag = HKS_TAG_PROCESS_NAME,
190 .blob = {
191 .data = reinterpret_cast<uint8_t *>(&userId005),
192 .size = sizeof(uint32_t)
193 }
194 }, {
195 .tag = HKS_TAG_USER_ID,
196 .uint32Param = userId005
197 }, {
198 .tag = HKS_TAG_ACCESS_TOKEN_ID,
199 .uint64Param = 0
200 }
201 };
202 (void)HksAddParams(paramSet005, params, HKS_ARRAY_SIZE(params));
203 (void)HksBuildParamSet(¶mSet005);
204 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet005),
205 .size = paramSet005->paramSetSize };
206 struct HksUpgradeFileTransferInfo info = { 0 };
207 (void)HksParseConfig("", &fileContent, &info);
208
209 HksFreeParamSet(¶mSet005);
210 }
211
HksServiceUpgradeConfigParserTest006()212 static void HksServiceUpgradeConfigParserTest006()
213 {
214 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest006");
215 g_accessTokenType = HKS_TOKEN_NATIVE;
216 struct HksParamSet *paramSet006 = nullptr;
217 (void)HksInitParamSet(¶mSet006);
218 uint32_t userId006 = 0;
219 uint32_t uid006 = 6;
220 struct HksParam params[] = {
221 {
222 .tag = HKS_TAG_PROCESS_NAME,
223 .blob = {
224 .data = reinterpret_cast<uint8_t *>(&uid006),
225 .size = sizeof(uint32_t)
226 }
227 }, {
228 .tag = HKS_TAG_USER_ID,
229 .uint32Param = userId006
230 }, {
231 .tag = HKS_TAG_ACCESS_TOKEN_ID,
232 .uint64Param = 0
233 }
234 };
235 (void)HksAddParams(paramSet006, params, HKS_ARRAY_SIZE(params));
236 (void)HksBuildParamSet(¶mSet006);
237 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet006),
238 .size = paramSet006->paramSetSize };
239 struct HksUpgradeFileTransferInfo info = { 0 };
240 (void)HksParseConfig("", &fileContent, &info);
241
242 HksFreeParamSet(¶mSet006);
243 }
244
HksServiceUpgradeConfigParserTest007()245 static void HksServiceUpgradeConfigParserTest007()
246 {
247 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest007");
248 g_accessTokenType = HKS_TOKEN_NATIVE;
249 struct HksParamSet *paramSet007 = nullptr;
250 (void)HksInitParamSet(¶mSet007);
251 uint32_t userId007 = 0;
252 uint32_t uid007 = 7;
253 struct HksParam params[] = {
254 {
255 .tag = HKS_TAG_PROCESS_NAME,
256 .blob = {
257 .data = reinterpret_cast<uint8_t *>(&uid007),
258 .size = sizeof(uint32_t)
259 }
260 }, {
261 .tag = HKS_TAG_USER_ID,
262 .uint32Param = userId007
263 }, {
264 .tag = HKS_TAG_ACCESS_TOKEN_ID,
265 .uint64Param = 0
266 }
267 };
268 (void)HksAddParams(paramSet007, params, HKS_ARRAY_SIZE(params));
269 (void)HksBuildParamSet(¶mSet007);
270 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet007),
271 .size = paramSet007->paramSetSize };
272 struct HksUpgradeFileTransferInfo info = { 0 };
273 (void)HksParseConfig("", &fileContent, &info);
274
275 HksFreeParamSet(¶mSet007);
276 }
277
HksServiceUpgradeConfigParserTest008()278 static void HksServiceUpgradeConfigParserTest008()
279 {
280 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest008");
281 g_accessTokenType = HKS_TOKEN_NATIVE;
282 struct HksParamSet *paramSet008 = nullptr;
283 (void)HksInitParamSet(¶mSet008);
284 uint32_t userId008 = 0;
285 uint32_t uid008 = 8;
286 struct HksParam params[] = {
287 {
288 .tag = HKS_TAG_PROCESS_NAME,
289 .blob = {
290 .data = reinterpret_cast<uint8_t *>(&uid008),
291 .size = sizeof(uint32_t)
292 }
293 }, {
294 .tag = HKS_TAG_USER_ID,
295 .uint32Param = userId008
296 }, {
297 .tag = HKS_TAG_ACCESS_TOKEN_ID,
298 .uint64Param = 0
299 }
300 };
301 (void)HksAddParams(paramSet008, params, HKS_ARRAY_SIZE(params));
302 (void)HksBuildParamSet(¶mSet008);
303 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet008),
304 .size = paramSet008->paramSetSize };
305 struct HksUpgradeFileTransferInfo info = { 0 };
306 (void)HksParseConfig("", &fileContent, &info);
307
308 HksFreeParamSet(¶mSet008);
309 }
310
HksServiceUpgradeConfigParserTest009()311 static void HksServiceUpgradeConfigParserTest009()
312 {
313 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest009");
314 g_accessTokenType = HKS_TOKEN_NATIVE;
315 struct HksParamSet *paramSet009 = nullptr;
316 (void)HksInitParamSet(¶mSet009);
317 uint32_t userId009 = 0;
318 uint32_t uid009 = 9;
319 struct HksParam params[] = {
320 {
321 .tag = HKS_TAG_PROCESS_NAME,
322 .blob = {
323 .data = reinterpret_cast<uint8_t *>(&uid009),
324 .size = sizeof(uint32_t)
325 }
326 }, {
327 .tag = HKS_TAG_USER_ID,
328 .uint32Param = userId009
329 }, {
330 .tag = HKS_TAG_ACCESS_TOKEN_ID,
331 .uint64Param = 0
332 }
333 };
334 (void)HksAddParams(paramSet009, params, HKS_ARRAY_SIZE(params));
335 (void)HksBuildParamSet(¶mSet009);
336 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet009),
337 .size = paramSet009->paramSetSize };
338 struct HksUpgradeFileTransferInfo info = { 0 };
339 (void)HksParseConfig("", &fileContent, &info);
340
341 HksFreeParamSet(¶mSet009);
342 }
343
HksServiceUpgradeConfigParserTest010()344 static void HksServiceUpgradeConfigParserTest010()
345 {
346 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest010");
347 g_accessTokenType = HKS_TOKEN_NATIVE;
348 struct HksParamSet *paramSet010 = nullptr;
349 (void)HksInitParamSet(¶mSet010);
350 uint32_t userId010 = 0;
351 uint32_t uid010 = 10;
352 struct HksParam params[] = {
353 {
354 .tag = HKS_TAG_PROCESS_NAME,
355 .blob = {
356 .data = reinterpret_cast<uint8_t *>(&uid010),
357 .size = sizeof(uint32_t)
358 }
359 }, {
360 .tag = HKS_TAG_USER_ID,
361 .uint32Param = userId010
362 }, {
363 .tag = HKS_TAG_ACCESS_TOKEN_ID,
364 .uint64Param = 0
365 }
366 };
367 (void)HksAddParams(paramSet010, params, HKS_ARRAY_SIZE(params));
368 (void)HksBuildParamSet(¶mSet010);
369 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet010),
370 .size = paramSet010->paramSetSize };
371 struct HksUpgradeFileTransferInfo info = { 0 };
372 (void)HksParseConfig("", &fileContent, &info);
373
374 HksFreeParamSet(¶mSet010);
375 }
376
HksServiceUpgradeConfigParserTest011()377 static void HksServiceUpgradeConfigParserTest011()
378 {
379 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest011");
380 g_accessTokenType = HKS_TOKEN_NATIVE;
381 struct HksParamSet *paramSet011 = nullptr;
382 (void)HksInitParamSet(¶mSet011);
383 uint32_t userId011 = 0;
384 uint32_t uid011 = 11;
385 struct HksParam params[] = {
386 {
387 .tag = HKS_TAG_PROCESS_NAME,
388 .blob = {
389 .data = reinterpret_cast<uint8_t *>(&uid011),
390 .size = sizeof(uint32_t)
391 }
392 }, {
393 .tag = HKS_TAG_USER_ID,
394 .uint32Param = userId011
395 }, {
396 .tag = HKS_TAG_ACCESS_TOKEN_ID,
397 .uint64Param = 0
398 }
399 };
400 (void)HksAddParams(paramSet011, params, HKS_ARRAY_SIZE(params));
401 (void)HksBuildParamSet(¶mSet011);
402 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet011),
403 .size = paramSet011->paramSetSize };
404 struct HksUpgradeFileTransferInfo info = { 0 };
405 (void)HksParseConfig("DistributedDataRdb_test", &fileContent, &info);
406
407 HksFreeParamSet(¶mSet011);
408 }
409
HksServiceUpgradeConfigParserTest012()410 static void HksServiceUpgradeConfigParserTest012()
411 {
412 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest012");
413 g_accessTokenType = HKS_TOKEN_NATIVE;
414 struct HksParamSet *paramSet012 = nullptr;
415 (void)HksInitParamSet(¶mSet012);
416 uint32_t userId012 = 0;
417 uint32_t uid012 = 11;
418 struct HksParam params[] = {
419 {
420 .tag = HKS_TAG_PROCESS_NAME,
421 .blob = {
422 .data = reinterpret_cast<uint8_t *>(&uid012),
423 .size = sizeof(uint32_t)
424 }
425 }, {
426 .tag = HKS_TAG_USER_ID,
427 .uint32Param = userId012
428 }, {
429 .tag = HKS_TAG_ACCESS_TOKEN_ID,
430 .uint64Param = 0
431 }
432 };
433 (void)HksAddParams(paramSet012, params, HKS_ARRAY_SIZE(params));
434 (void)HksBuildParamSet(¶mSet012);
435 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet012),
436 .size = paramSet012->paramSetSize };
437 struct HksUpgradeFileTransferInfo info = { 0 };
438 (void)HksParseConfig("distributeddb_client_root_key", &fileContent, &info);
439
440 HksFreeParamSet(¶mSet012);
441 }
442
HksServiceUpgradeConfigParserTest013()443 static void HksServiceUpgradeConfigParserTest013()
444 {
445 HKS_LOG_I("enter HksServiceUpgradeConfigParserTest013");
446 g_accessTokenType = HKS_TOKEN_NATIVE;
447 struct HksParamSet *paramSet013 = nullptr;
448 (void)HksInitParamSet(¶mSet013);
449 uint32_t userId013 = 0;
450 uint32_t uid013 = 11;
451 struct HksParam params[] = {
452 {
453 .tag = HKS_TAG_PROCESS_NAME,
454 .blob = {
455 .data = reinterpret_cast<uint8_t *>(&uid013),
456 .size = sizeof(uint32_t)
457 }
458 }, {
459 .tag = HKS_TAG_USER_ID,
460 .uint32Param = userId013
461 }, {
462 .tag = HKS_TAG_ACCESS_TOKEN_ID,
463 .uint64Param = 0
464 }
465 };
466 (void)HksAddParams(paramSet013, params, HKS_ARRAY_SIZE(params));
467 (void)HksBuildParamSet(¶mSet013);
468 struct HksBlob fileContent = { .data = reinterpret_cast<uint8_t *>(paramSet013),
469 .size = paramSet013->paramSetSize };
470 struct HksUpgradeFileTransferInfo info = { 0 };
471 (void)HksParseConfig("distributeddb_client_root_key_etc", &fileContent, &info);
472
473 HksFreeParamSet(¶mSet013);
474 }
475
HksFileTransferTest001()476 static void HksFileTransferTest001()
477 {
478 HKS_LOG_I("enter HksFileTransferTest001");
479 const uint32_t testUserId = 100;
480 HksUpgradeFileTransferOnUserUnlock(testUserId);
481 }
482 }
483 }
484 }
485
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)486 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
487 {
488 (void)data;
489 (void)size;
490 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest001();
491 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest002();
492 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest003();
493 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest004();
494 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest005();
495 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest006();
496 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest007();
497 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest008();
498 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest009();
499 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest010();
500 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest011();
501 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest012();
502 OHOS::Security::Hks::HksServiceUpgradeConfigParserTest013();
503 OHOS::Security::Hks::HksFileTransferTest001();
504 return 0;
505 }
506