• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "hksclientipcserialization_fuzzer.h"
17 
18 #include <cstring>
19 #include <securec.h>
20 #include <vector>
21 
22 #include "file_ex.h"
23 #include "hks_api.h"
24 #include "hks_log.h"
25 #include "hks_mem.h"
26 #include "hks_param.h"
27 #include "hks_client_ipc_serialization.h"
28 #include "hks_type.h"
29 #include "hks_type_inner.h"
30 
31 #include "hks_fuzz_util.h"
32 
33 constexpr uint32_t TEST_BLOB_SIZE = 3221225431;
34 constexpr uint32_t NUM = 2;
35 
36 namespace OHOS {
37 namespace Security {
38 namespace Hks {
39 
40 /**
41  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest001
42  * @tc.desc: tdd CopyUint32ToBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
43  * @tc.type: FUNC
44  */
HksClientIpcSerializationTest001()45 static void HksClientIpcSerializationTest001()
46 {
47     HKS_LOG_I("enter HksClientIpcSerializationTest001");
48 
49     uint32_t index = 15;
50     const uint32_t destBlobSize = 10;
51     uint8_t destBlobData[destBlobSize] = { 0 };
52     struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };
53 
54     CopyUint32ToBuffer(0, &destBlob, &index);
55 }
56 
57 /**
58  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest002
59  * @tc.desc: tdd HksOnceParamPack CopyBlobToBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
60  * @tc.type: FUNC
61  */
HksClientIpcSerializationTest002()62 static void HksClientIpcSerializationTest002()
63 {
64     HKS_LOG_I("enter HksClientIpcSerializationTest002");
65 
66     uint32_t index = 15;
67     const uint32_t destBlobSize = 10;
68     uint8_t destBlobData[destBlobSize] = { 0 };
69     struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };
70 
71     HksOnceParamPack(&destBlob, nullptr, nullptr, &index);
72 }
73 
74 /**
75  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest003
76  * @tc.desc: tdd HksAgreeKeyPack CopyParamSetToBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
77  * @tc.type: FUNC
78  */
HksClientIpcSerializationTest003()79 static void HksClientIpcSerializationTest003()
80 {
81     HKS_LOG_I("enter HksClientIpcSerializationTest003");
82 
83     const uint32_t destBlobSize = 10;
84     uint8_t destBlobData[destBlobSize] = { 0 };
85     struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };
86     const struct HksParamSet paramSet = { .paramSetSize = 12 };
87 
88     HksAgreeKeyPack(&destBlob, &paramSet, nullptr, nullptr, nullptr);
89 }
90 
91 /**
92  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest004
93  * @tc.desc: tdd HksGetKeyInfoListUnpackFromService GetUint32FromBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
94  * @tc.type: FUNC
95  */
HksClientIpcSerializationTest004()96 static void HksClientIpcSerializationTest004()
97 {
98     HKS_LOG_I("enter HksClientIpcSerializationTest004");
99 
100     const uint32_t destBlobSize = 2;
101     uint8_t destBlobData[destBlobSize] = { 0 };
102     struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };
103 
104     HksGetKeyInfoListUnpackFromService(&destBlob, nullptr, nullptr);
105 }
106 
107 /**
108  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest005
109  * @tc.desc: tdd HksCertificateChainUnpackFromService GetBlobFromBuffer, expect HKS_ERROR_IPC_MSG_FAIL
110  * @tc.type: FUNC
111  */
HksClientIpcSerializationTest005()112 static void HksClientIpcSerializationTest005()
113 {
114     HKS_LOG_I("enter HksClientIpcSerializationTest005");
115 
116     const uint32_t srcBlobSize = 7;
117     uint8_t srcBlobData[srcBlobSize] = { 4 };
118     struct HksBlob srcBlob = { .size = srcBlobSize, .data = srcBlobData };
119     struct HksCertChain certChain = { .certsCount = 4 };
120 
121     HksCertificateChainUnpackFromService(&srcBlob, false, &certChain);
122 }
123 
124 /**
125  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest006
126  * @tc.desc: tdd EncodeCertChain CheckAndCalculateSize
127  * @tc.type: FUNC
128  */
HksClientIpcSerializationTest006()129 static void HksClientIpcSerializationTest006()
130 {
131     HKS_LOG_I("enter HksClientIpcSerializationTest006");
132 
133     struct HksBlob inBlob = { .size = UINT32_MAX };
134     int32_t ret = EncodeCertChain(&inBlob, nullptr);
135 
136     inBlob.size = UINT32_MAX - NUM;
137     ret = EncodeCertChain(&inBlob, nullptr);
138 
139     inBlob.size = TEST_BLOB_SIZE;
140     ret = EncodeCertChain(&inBlob, nullptr);
141 }
142 
143 /**
144  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest007
145  * @tc.desc: tdd EncodeCertChain CheckAndCalculateSize Base64Encode
146  * @tc.type: FUNC
147  */
148 uint8_t inBlobData[] = {
149     0x30, 0x82, 0x03, 0x13, 0x30, 0x82, 0x02, 0xb9, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x0e, 0x63,
150     0xcb, 0x7d, 0xcd, 0xb3, 0x86, 0x85, 0x27, 0xc6, 0xbc, 0xe0, 0x4d, 0x33, 0x99, 0x30, 0x0a, 0x06,
151     0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x5d, 0x31, 0x39, 0x30, 0x37, 0x06,
152     0x03, 0x55, 0x04, 0x03, 0x0c, 0x30, 0x48, 0x75, 0x61, 0x77, 0x65, 0x69, 0x20, 0x43, 0x42, 0x47,
153     0x20, 0x45, 0x43, 0x43, 0x20, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x20, 0x41, 0x6e, 0x6f, 0x6e,
154     0x79, 0x6d, 0x6f, 0x75, 0x73, 0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f,
155     0x6e, 0x20, 0x43, 0x41, 0x20, 0x31, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
156     0x0a, 0x48, 0x75, 0x61, 0x77, 0x65, 0x69, 0x20, 0x43, 0x42, 0x47, 0x31, 0x0b, 0x30, 0x09, 0x06,
157     0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4e, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x38,
158     0x30, 0x38, 0x30, 0x32, 0x30, 0x35, 0x35, 0x37, 0x5a, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x38, 0x31,
159     0x35, 0x30, 0x32, 0x30, 0x35, 0x35, 0x37, 0x5a, 0x30, 0x2c, 0x31, 0x2a, 0x30, 0x28, 0x06, 0x03,
160     0x55, 0x04, 0x03, 0x0c, 0x21, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x20, 0x43, 0x65, 0x72, 0x74,
161     0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x4d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65,
162     0x6e, 0x74, 0x20, 0x4b, 0x65, 0x79, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
163     0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00,
164     0x04, 0xea, 0xba, 0xbf, 0x64, 0x55, 0x32, 0x59, 0x2b, 0xd6, 0xe2, 0x95, 0xea, 0x06, 0x4f, 0x35,
165     0xe9, 0x58, 0x48, 0x68, 0x68, 0x9e, 0x55, 0x1e, 0xf4, 0xad, 0x3e, 0x62, 0x39, 0xad, 0xd4, 0xba,
166     0x7b, 0x99, 0xbc, 0x0c, 0x9e, 0x8f, 0xc3, 0x77, 0x8f, 0x57, 0x01, 0xd8, 0x8a, 0xa6, 0x5d, 0x5b,
167     0xe3, 0xfd, 0x0e, 0x23, 0xff, 0x1a, 0xe5, 0xbe, 0x6e, 0xd4, 0x73, 0x1d, 0xc4, 0x00, 0xe3, 0x9a,
168     0x08, 0xa3, 0x82, 0x01, 0x8c, 0x30, 0x82, 0x01, 0x88, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
169     0x04, 0x16, 0x04, 0x14, 0x55, 0xdc, 0x0d, 0xb0, 0x00, 0xa6, 0x36, 0x92, 0xa0, 0x9c, 0x93, 0xb5,
170     0x8f, 0xd8, 0x68, 0x17, 0x44, 0x71, 0x1b, 0x16, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
171     0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30,
172     0x16, 0x80, 0x14, 0xe3, 0x2c, 0xcb, 0xff, 0x76, 0x87, 0x3b, 0x12, 0xfb, 0x43, 0x22, 0x3f, 0x3f,
173     0xfb, 0x02, 0x06, 0x81, 0xdf, 0x27, 0xa7, 0x30, 0x82, 0x01, 0x36, 0x06, 0x0c, 0x2b, 0x06, 0x01,
174     0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x01, 0x03, 0x04, 0x82, 0x01, 0x24, 0x30, 0x82, 0x01,
175     0x20, 0x02, 0x01, 0x00, 0x30, 0x81, 0xcf, 0x02, 0x01, 0x02, 0x06, 0x0d, 0x2b, 0x06, 0x01, 0x04,
176     0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x01, 0x03, 0x30, 0x81, 0xba, 0x06, 0x0e, 0x2b, 0x06,
177     0x01, 0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x01, 0x03, 0x01, 0x04, 0x81, 0xa7, 0x7b,
178     0x22, 0x61, 0x70, 0x70, 0x49, 0x64, 0x22, 0x3a, 0x22, 0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x78, 0x61,
179     0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6d, 0x79, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69,
180     0x6f, 0x6e, 0x5f, 0x42, 0x4e, 0x73, 0x72, 0x6a, 0x4d, 0x73, 0x67, 0x4a, 0x54, 0x30, 0x4f, 0x61,
181     0x59, 0x50, 0x49, 0x36, 0x70, 0x75, 0x57, 0x70, 0x7a, 0x74, 0x2b, 0x67, 0x43, 0x36, 0x71, 0x44,
182     0x55, 0x73, 0x51, 0x78, 0x33, 0x78, 0x77, 0x49, 0x51, 0x67, 0x68, 0x4a, 0x4a, 0x6a, 0x7a, 0x6d,
183     0x66, 0x4d, 0x49, 0x58, 0x59, 0x38, 0x6f, 0x32, 0x2b, 0x49, 0x57, 0x56, 0x79, 0x48, 0x37, 0x43,
184     0x2f, 0x61, 0x63, 0x53, 0x2f, 0x44, 0x4a, 0x6f, 0x43, 0x57, 0x78, 0x41, 0x74, 0x44, 0x51, 0x4c,
185     0x2b, 0x51, 0x35, 0x78, 0x36, 0x2b, 0x34, 0x78, 0x2f, 0x41, 0x3d, 0x22, 0x2c, 0x22, 0x62, 0x75,
186     0x6e, 0x64, 0x6c, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x63, 0x6f, 0x6d, 0x2e, 0x65,
187     0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6d, 0x79, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61,
188     0x74, 0x69, 0x6f, 0x6e, 0x22, 0x7d, 0x30, 0x22, 0x02, 0x01, 0x00, 0x06, 0x0d, 0x2b, 0x06, 0x01,
189     0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x01, 0x04, 0x04, 0x0e, 0x63, 0x68, 0x61, 0x6c,
190     0x6c, 0x65, 0x6e, 0x67, 0x65, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x30, 0x25, 0x02, 0x01, 0x03, 0x06,
191     0x0e, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x02, 0x02, 0x06, 0x04,
192     0x10, 0x28, 0xc4, 0xfb, 0x49, 0x44, 0xaf, 0xec, 0x11, 0xb9, 0x09, 0x02, 0x42, 0xac, 0x12, 0x00,
193     0x02, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00,
194     0x30, 0x45, 0x02, 0x21, 0x00, 0x92, 0x41, 0xa8, 0xf7, 0xb9, 0x57, 0x79, 0x78, 0x0d, 0xd8, 0xf1,
195     0x76, 0xaf, 0x10, 0x4e, 0xef, 0xce, 0xc5, 0xff, 0xbe, 0x8b, 0x04, 0x86, 0xb4, 0xd4, 0xa5, 0x11,
196     0x13, 0x16, 0xc5, 0xf8, 0xa1, 0x02, 0x20, 0x3b, 0xb2, 0x22, 0x50, 0xf5, 0x10, 0x76, 0x05, 0x98,
197     0xf7, 0x5a, 0xeb, 0xe3, 0x92, 0xfe, 0x29, 0x31, 0x0f, 0xc8, 0x3a, 0x04, 0xf1, 0x97, 0xbf, 0x39,
198     0x3a, 0x5d, 0xf5, 0xe3, 0xb7, 0x35, 0xa1
199 };
HksClientIpcSerializationTest007()200 static void HksClientIpcSerializationTest007()
201 {
202     HKS_LOG_I("enter HksClientIpcSerializationTest007");
203 
204     uint32_t inBlobSize = sizeof(inBlobData) / sizeof(inBlobData[0]);
205     struct HksBlob inBlob = { .size = inBlobSize, .data = inBlobData };
206     uint32_t outBlobSize = 4096;
207     uint8_t outBlobData[outBlobSize];
208     for (uint32_t i = 0; i < inBlobSize; ++i) {
209     outBlobData[i] = inBlobData[i];
210     }
211     struct HksBlob outBlob = { .size = outBlobSize, .data = outBlobData };
212 
213     EncodeCertChain(&inBlob, &outBlob);
214 }
215 
216 /**
217  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest008
218  * @tc.desc: tdd HksCertificateChainUnpackFromService
219  * @tc.type: FUNC
220  */
HksClientIpcSerializationTest008()221 static void HksClientIpcSerializationTest008()
222 {
223     HKS_LOG_I("enter HksClientIpcSerializationTest008");
224 
225     const uint32_t srcBlobSize1 = 2;
226     uint8_t srcBlobData1[srcBlobSize1] = { 5 };
227     struct HksBlob srcBlob = { .size = srcBlobSize1, .data = srcBlobData1 };
228     struct HksCertChain certChain = { .certsCount = 4 };
229 
230     int32_t ret = HksCertificateChainUnpackFromService(&srcBlob, true, &certChain);
231 
232     const uint32_t srcBlobSize2 = 4;
233     uint8_t srcBlobData2[srcBlobSize2] = { 5 };
234     srcBlob.size = srcBlobSize2;
235     srcBlob.data = srcBlobData2;
236     ret = HksCertificateChainUnpackFromService(&srcBlob, true, &certChain);
237 }
238 
239 /**
240  * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest009
241  * @tc.desc: tdd HksListAliasesUnpackFromService
242  * @tc.type: FUNC
243  */
HksClientIpcSerializationTest009()244 static void HksClientIpcSerializationTest009()
245 {
246     HKS_LOG_I("enter HksClientIpcSerializationTest009");
247 
248     struct HksBlob srcBlob = { .size = 0, .data = nullptr };
249     HksListAliasesUnpackFromService(&srcBlob, nullptr);
250 }
251 }
252 }
253 }
254 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)255 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
256 {
257     (void)data;
258     (void)size;
259     OHOS::Security::Hks::HksClientIpcSerializationTest001();
260     OHOS::Security::Hks::HksClientIpcSerializationTest002();
261     OHOS::Security::Hks::HksClientIpcSerializationTest003();
262     OHOS::Security::Hks::HksClientIpcSerializationTest004();
263     OHOS::Security::Hks::HksClientIpcSerializationTest005();
264     OHOS::Security::Hks::HksClientIpcSerializationTest006();
265     OHOS::Security::Hks::HksClientIpcSerializationTest007();
266     OHOS::Security::Hks::HksClientIpcSerializationTest008();
267     OHOS::Security::Hks::HksClientIpcSerializationTest009();
268     return 0;
269 }
270