1 /*
2  * Copyright (c) 2023 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 "iso_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "device_auth_defines.h"
22 #include "hc_types.h"
23 #include "hks_api.h"
24 #include "hks_param.h"
25 #include "hks_type.h"
26 #include "iso_protocol.h"
27 #include "json_utils.h"
28 #include "uint8buff_utils.h"
29 #include "device_auth.h"
30 
31 namespace OHOS {
32 #define PSK_SIZE 32
33 static const uint8_t PSK_VAL[PSK_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
34     20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
35 static const char *AUTH_ID_C_VAL = "5420459D93FE773F9945FD64277FBA2CAB8FB996DDC1D0B97676FBB1242B3930";
36 static const char *AUTH_ID_S_VAL = "52E2706717D5C39D736E134CC1E3BE1BAA2AA52DB7C76A37C749558BD2E6492C";
37 static const char *MSG_C_VAL = "client send msg";
38 static const char *MSG_S_VAL = "server send msg";
39 
40 static Uint8Buff g_psk = { (uint8_t *)PSK_VAL, PSK_SIZE };
41 static Uint8Buff g_authIdC = { (uint8_t *)AUTH_ID_C_VAL, 64 };
42 static Uint8Buff g_authIdS = { (uint8_t *)AUTH_ID_S_VAL, 64 };
43 static Uint8Buff g_msgC = { (uint8_t *)MSG_C_VAL, 16 };
44 static Uint8Buff g_msgS = { (uint8_t *)MSG_S_VAL, 16 };
45 static IsoInitParams g_paramsC = { g_authIdC, DEFAULT_OS_ACCOUNT };
46 static IsoInitParams g_paramsS = { g_authIdS, DEFAULT_OS_ACCOUNT };
47 
IsoTest01(void)48 static void IsoTest01(void)
49 {
50     HksInitialize();
51     BaseProtocol *self;
52     (void)CreateIsoProtocol(&g_paramsC, true, &self);
53     self->destroy(self);
54 }
55 
IsoTest02(void)56 static void IsoTest02(void)
57 {
58     HksInitialize();
59     BaseProtocol *client;
60     int32_t res = CreateIsoProtocol(&g_paramsC, true, &client);
61 
62     BaseProtocol *server;
63     res = CreateIsoProtocol(&g_paramsS, false, &server);
64 
65     res = client->setPsk(client, &g_psk);
66     res = server->setPsk(server, &g_psk);
67 
68     CJson *clientOut = nullptr;
69     CJson *serverOut = nullptr;
70 
71     res = client->setSelfProtectedMsg(client, &g_msgC);
72     res = client->setPeerProtectedMsg(client, &g_msgS);
73     res = server->setSelfProtectedMsg(server, &g_msgS);
74     res = server->setPeerProtectedMsg(server, &g_msgC);
75 
76     res = client->start(client, &clientOut);
77 
78     while (clientOut != nullptr || serverOut != nullptr) {
79         if (clientOut != nullptr) {
80             res = server->process(server, clientOut, &serverOut);
81             FreeJson(clientOut);
82             clientOut = nullptr;
83         } else {
84             res = client->process(client, serverOut, &clientOut);
85             FreeJson(serverOut);
86             serverOut = nullptr;
87         }
88     }
89     Uint8Buff clientKey = { nullptr, 0 };
90     res = client->getSessionKey(client, &clientKey);
91     FreeUint8Buff(&clientKey);
92     Uint8Buff serverKey = { nullptr, 0 };
93     res = server->getSessionKey(server, &serverKey);
94     FreeUint8Buff(&serverKey);
95 
96     client->destroy(client);
97     server->destroy(server);
98 }
99 
IsoTest03(void)100 static void IsoTest03(void)
101 {
102     HksInitialize();
103     BaseProtocol *client;
104     int32_t res = CreateIsoProtocol(&g_paramsC, true, &client);
105 
106     BaseProtocol *server;
107     res = CreateIsoProtocol(&g_paramsS, false, &server);
108 
109     res = client->setPsk(client, &g_psk);
110     res = server->setPsk(server, &g_psk);
111 
112     CJson *clientOut = nullptr;
113     CJson *serverOut = nullptr;
114 
115     res = client->start(client, &clientOut);
116 
117     while (clientOut != nullptr || serverOut != nullptr) {
118         if (clientOut != nullptr) {
119             res = server->process(server, clientOut, &serverOut);
120             FreeJson(clientOut);
121             clientOut = nullptr;
122         } else {
123             res = client->process(client, serverOut, &clientOut);
124             FreeJson(serverOut);
125             serverOut = nullptr;
126         }
127     }
128     Uint8Buff clientKey;
129     res = client->getSessionKey(client, &clientKey);
130     FreeUint8Buff(&clientKey);
131     Uint8Buff serverKey;
132     res = server->getSessionKey(server, &serverKey);
133     FreeUint8Buff(&serverKey);
134 
135     client->destroy(client);
136     server->destroy(server);
137 }
138 
IsoTest04(void)139 static void IsoTest04(void)
140 {
141     HksInitialize();
142     BaseProtocol *self;
143     (void)CreateIsoProtocol(nullptr, true, &self);
144 }
145 
IsoTest05(void)146 static void IsoTest05(void)
147 {
148     HksInitialize();
149     (void)CreateIsoProtocol(&g_paramsC, true, nullptr);
150 }
151 
IsoTest06(void)152 static void IsoTest06(void)
153 {
154     HksInitialize();
155     IsoInitParams errParams = { { nullptr, 32 }, DEFAULT_OS_ACCOUNT };
156     BaseProtocol *self;
157     (void)CreateIsoProtocol(&errParams, true, &self);
158 }
159 
IsoTest07(void)160 static void IsoTest07(void)
161 {
162     HksInitialize();
163     IsoInitParams errParams = { { (uint8_t *)AUTH_ID_C_VAL, 0 }, DEFAULT_OS_ACCOUNT };
164     BaseProtocol *self;
165     (void)CreateIsoProtocol(&errParams, true, &self);
166 }
167 
IsoTest08(void)168 static void IsoTest08(void)
169 {
170     HksInitialize();
171     BaseProtocol *self;
172     (void)CreateIsoProtocol(&g_paramsC, true, &self);
173     (void)self->setPsk(nullptr, &g_psk);
174     self->destroy(self);
175 }
176 
IsoTest09(void)177 static void IsoTest09(void)
178 {
179     HksInitialize();
180     BaseProtocol *self;
181     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
182     res = self->setPsk(self, nullptr);
183     self->destroy(self);
184 }
185 
IsoTest10(void)186 static void IsoTest10(void)
187 {
188     HksInitialize();
189     BaseProtocol *self;
190     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
191     Uint8Buff errParams = { nullptr, PSK_SIZE };
192     res = self->setPsk(self, &errParams);
193     self->destroy(self);
194 }
195 
IsoTest11(void)196 static void IsoTest11(void)
197 {
198     HksInitialize();
199     BaseProtocol *self;
200     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
201     Uint8Buff errParams = { (uint8_t *)PSK_VAL, 0 };
202     res = self->setPsk(self, &errParams);
203     self->destroy(self);
204 }
205 
IsoTest12(void)206 static void IsoTest12(void)
207 {
208     HksInitialize();
209     BaseProtocol *self;
210     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
211     CJson *out = nullptr;
212     res = self->start(nullptr, &out);
213     self->destroy(self);
214 }
215 
IsoTest13(void)216 static void IsoTest13(void)
217 {
218     HksInitialize();
219     BaseProtocol *self;
220     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
221     res = self->start(self, nullptr);
222     self->destroy(self);
223 }
224 
IsoTest14(void)225 static void IsoTest14(void)
226 {
227     HksInitialize();
228     BaseProtocol *self;
229     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
230     self->curState = self->finishState;
231     CJson *out = nullptr;
232     res = self->start(self, &out);
233     self->destroy(self);
234 }
235 
IsoTest15(void)236 static void IsoTest15(void)
237 {
238     HksInitialize();
239     BaseProtocol *self;
240     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
241     self->curState = self->failState;
242     CJson *out = nullptr;
243     res = self->start(self, &out);
244     self->destroy(self);
245 }
246 
IsoTest16(void)247 static void IsoTest16(void)
248 {
249     HksInitialize();
250     BaseProtocol *self;
251     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
252     CJson recvMsg;
253     CJson *sendMsg = nullptr;
254     res = self->process(nullptr, &recvMsg, &sendMsg);
255     self->destroy(self);
256 }
257 
IsoTest17(void)258 static void IsoTest17(void)
259 {
260     HksInitialize();
261     BaseProtocol *self;
262     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
263     CJson *sendMsg = nullptr;
264     res = self->process(self, nullptr, &sendMsg);
265     self->destroy(self);
266 }
267 
IsoTest18(void)268 static void IsoTest18(void)
269 {
270     HksInitialize();
271     BaseProtocol *self;
272     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
273     CJson recvMsg;
274     res = self->process(self, &recvMsg, nullptr);
275     self->destroy(self);
276 }
277 
IsoTest19(void)278 static void IsoTest19(void)
279 {
280     HksInitialize();
281     BaseProtocol *self;
282     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
283     self->curState = self->finishState;
284     CJson recvMsg;
285     CJson *sendMsg = nullptr;
286     res = self->process(self, &recvMsg, &sendMsg);
287     self->destroy(self);
288 }
289 
IsoTest20(void)290 static void IsoTest20(void)
291 {
292     HksInitialize();
293     BaseProtocol *self;
294     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
295     self->curState = self->failState;
296     CJson recvMsg;
297     CJson *sendMsg = nullptr;
298     res = self->process(self, &recvMsg, &sendMsg);
299     self->destroy(self);
300 }
301 
IsoTest21(void)302 static void IsoTest21(void)
303 {
304     HksInitialize();
305     BaseProtocol *self;
306     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
307     res = self->setSelfProtectedMsg(nullptr, &g_msgC);
308     self->destroy(self);
309 }
310 
IsoTest22(void)311 static void IsoTest22(void)
312 {
313     HksInitialize();
314     BaseProtocol *self;
315     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
316     res = self->setSelfProtectedMsg(self, nullptr);
317     self->destroy(self);
318 }
319 
IsoTest23(void)320 static void IsoTest23(void)
321 {
322     HksInitialize();
323     BaseProtocol *self;
324     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
325     res = self->setPeerProtectedMsg(nullptr, &g_msgS);
326     self->destroy(self);
327 }
328 
IsoTest24(void)329 static void IsoTest24(void)
330 {
331     HksInitialize();
332     BaseProtocol *self;
333     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
334     res = self->setPeerProtectedMsg(self, nullptr);
335     self->destroy(self);
336 }
337 
IsoTest25(void)338 static void IsoTest25(void)
339 {
340     HksInitialize();
341     BaseProtocol *self;
342     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
343     Uint8Buff key = { nullptr, 0 };
344     res = self->getSessionKey(nullptr, &key);
345     self->destroy(self);
346 }
347 
IsoTest26(void)348 static void IsoTest26(void)
349 {
350     HksInitialize();
351     BaseProtocol *self;
352     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
353     res = self->getSessionKey(self, nullptr);
354     self->destroy(self);
355 }
356 
IsoTest27(void)357 static void IsoTest27(void)
358 {
359     HksInitialize();
360     BaseProtocol *self;
361     int32_t res = CreateIsoProtocol(&g_paramsC, true, &self);
362     Uint8Buff key = { nullptr, 0 };
363     res = self->getSessionKey(self, &key);
364     self->destroy(self);
365 }
366 
IsoTest28(void)367 static void IsoTest28(void)
368 {
369     HksInitialize();
370     BaseProtocol *self;
371     (void)CreateIsoProtocol(&g_paramsC, true, &self);
372     self->destroy(nullptr);
373     self->destroy(self);
374 }
375 
FuzzDoCallback(const uint8_t * data,size_t size)376 bool FuzzDoCallback(const uint8_t* data, size_t size)
377 {
378     (void)data;
379     (void)size;
380     (void)IsoTest01();
381     (void)IsoTest02();
382     (void)IsoTest03();
383     (void)IsoTest04();
384     (void)IsoTest05();
385     (void)IsoTest06();
386     (void)IsoTest07();
387     (void)IsoTest08();
388     (void)IsoTest09();
389     (void)IsoTest10();
390     (void)IsoTest11();
391     (void)IsoTest12();
392     (void)IsoTest13();
393     (void)IsoTest14();
394     (void)IsoTest15();
395     (void)IsoTest16();
396     (void)IsoTest17();
397     (void)IsoTest18();
398     (void)IsoTest19();
399     (void)IsoTest20();
400     (void)IsoTest21();
401     (void)IsoTest22();
402     (void)IsoTest23();
403     (void)IsoTest24();
404     (void)IsoTest25();
405     (void)IsoTest26();
406     (void)IsoTest27();
407     (void)IsoTest28();
408     return true;
409 }
410 }
411 
412 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)413 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
414 {
415     /* Run your code on data */
416     OHOS::FuzzDoCallback(data, size);
417     return 0;
418 }
419 
420