1 
2 /*
3  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #include "cert_mgr_adapter_fuzz.h"
18 
19 #include <array>
20 #include <cstdint>
21 #include <cstring>
22 #include <string>
23 #include <vector>
24 
25 #include "cert_mgr_adapter_impl.h"
26 
27 using namespace OHOS::NWeb;
28 
CertManagerAdapterFuzzTest(const uint8_t * data,size_t size)29 void CertManagerAdapterFuzzTest(const uint8_t* data, size_t size)
30 {
31     CertManagerAdapterImpl adapter;
32 
33     uint8_t certData[MAX_LEN_CERTIFICATE];
34     adapter.GetCertDataBySubject("test_subject", certData, CM_SYSTEM_TRUSTED_STORE);
35 
36     uint8_t systemRootCertData[MAX_LEN_CERTIFICATE];
37     adapter.GetSytemRootCertData(1, systemRootCertData);
38 
39     uint8_t userRootCertData[MAX_LEN_CERTIFICATE];
40     adapter.GetUserRootCertData(1, userRootCertData);
41 
42     uint8_t certBuffer[MAX_LEN_CERTIFICATE_CHAIN];
43     uint32_t certLen = MAX_LEN_CERTIFICATE_CHAIN;
44     adapter.GetAppCert(certData, certBuffer, &certLen);
45 
46     uint8_t signData[MAX_LEN_CERTIFICATE];
47     uint32_t signDataLen = sizeof(signData);
48     std::string uri = "test_uri";
49     adapter.Sign(reinterpret_cast<const uint8_t*>(uri.c_str()), certData, sizeof(certData), signData, signDataLen);
50 
51     std::vector<std::string> certs;
52     adapter.GetTrustAnchorsForHostName("example.com", certs);
53 
54     std::vector<std::string> pins;
55     adapter.GetPinSetForHostName("example.com", pins);
56 
57     adapter.GetCertMaxSize();
58     adapter.GetAppCertMaxSize();
59     adapter.GetSytemRootCertSum();
60     adapter.GetUserRootCertSum();
61 }
62 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64 {
65     CertManagerAdapterFuzzTest(data, size);
66     return 0;
67 }