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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include "securec.h"
20 #include "../profile_resources.h"
21
22 namespace OHOS {
23 namespace SignatureTools {
SignProfileTest001(const uint8_t * data,size_t size)24 bool SignProfileTest001(const uint8_t* data, size_t size)
25 {
26 std::string content(data, data + size);
27 Options options;
28 std::string mode = SIGN_PROFILE_MODE;
29 std::string keyAlias = SIGN_PROFILE_KEY_ALIAS;
30 std::string profileCertFile = SIGN_PROFILE_PROFILE_CERT_FILE;
31 std::string signAlg = SIGN_PROFILE_SIGN_ALG;
32 std::string keystoreFile = SIGN_PROFILE_KEY_STORE_FILE;
33 std::string outFile = SIGN_PROFILE_OUT_FILE;
34 std::string inFile = SIGN_PROFILE_IN_FILE;
35 char keyStorePwd[] = "123456";
36 char keypwd[] = "123456";
37 options[Options::KEY_ALIAS] = keyAlias;
38 options[Options::MODE] = mode;
39 options[Options::PROFILE_CERT_FILE] = profileCertFile;
40 options[Options::SIGN_ALG] = signAlg;
41 options[Options::KEY_STORE_FILE] = keystoreFile;
42 options[Options::OUT_FILE] = outFile;
43 options[Options::IN_FILE] = inFile;
44 options[Options::KEY_RIGHTS] = keypwd;
45 options[Options::KEY_STORE_RIGHTS] = keyStorePwd;
46
47 LocalizationAdapter adapter(&options);
48 SignerFactory factory;
49 std::shared_ptr<Signer> signer = factory.GetSigner(adapter);
50 PKCS7Data p7;
51 std::string p7b;
52 int result = p7.Sign(content, signer, signAlg, p7b);
53 return result == 0;
54 }
55
SignProfileTest002(const uint8_t * data,size_t size)56 bool SignProfileTest002(const uint8_t* data, size_t size)
57 {
58 Options options;
59 std::string mode = SIGN_PROFILE_MODE;
60 std::string keyAlias = SIGN_PROFILE_KEY_ALIAS;
61 std::string profileCertFile = SIGN_PROFILE_PROFILE_CERT_FILE;
62 std::string signAlg = SIGN_PROFILE_SIGN_ALG;
63 std::string keystoreFile = SIGN_PROFILE_KEY_STORE_FILE;
64 std::string outFile = SIGN_PROFILE_OUT_FILE;
65 std::string inFile = SIGN_PROFILE_IN_FILE;
66 char keyStorePwd[] = "123456";
67 char keypwd[] = "123456";
68 options[Options::KEY_ALIAS] = keyAlias;
69 options[Options::MODE] = mode;
70 options[Options::PROFILE_CERT_FILE] = profileCertFile;
71 options[Options::SIGN_ALG] = signAlg;
72 options[Options::KEY_STORE_FILE] = keystoreFile;
73 options[Options::OUT_FILE] = outFile;
74 options[Options::IN_FILE] = inFile;
75 options[Options::KEY_RIGHTS] = keypwd;
76 options[Options::KEY_STORE_RIGHTS] = keyStorePwd;
77
78 LocalizationAdapter adapter(&options);
79 SignerFactory factory;
80 std::shared_ptr<Signer> signer = factory.GetSigner(adapter);
81 STACK_OF(X509)* certs = signer->GetCertificates();
82 PKCS7Data::PrintCertChainSub(certs);
83 return true;
84 }
85
SignProfileTest003(const uint8_t * data,size_t size)86 bool SignProfileTest003(const uint8_t* data, size_t size)
87 {
88 std::string provision(reinterpret_cast<const char*>(data), size);
89 ProfileInfo info;
90 AppProvisionVerifyResult result = ParseAndVerify(provision, info);
91 return result == AppProvisionVerifyResult::PROVISION_OK;
92 }
93
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)94 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
95 {
96 SignProfileTest001(data, size);
97 SignProfileTest002(data, size);
98 SignProfileTest003(data, size);
99 return true;
100 }
101 }
102 }
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
105 {
106 /* Run your code on data */
107 OHOS::SignatureTools::DoSomethingInterestingWithMyAPI(data, size);
108 return 0;
109 }