• 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 <cstddef>
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstring>
20 
21 #include "sign_tool_service_impl.h"
22 #include <openssl/ssl.h>
23 #include "options.h"
24 #include "cert_tools.h"
25 #include "params_run_tool.h"
26 #include "localization_adapter.h"
27 
28 
29 namespace OHOS {
30 namespace SignatureTools {
31 
TestGenerateSubCert(const uint8_t * data,size_t size)32 bool TestGenerateSubCert(const uint8_t* data, size_t size)
33 {
34     std::shared_ptr<Options> params = std::make_shared<Options>();
35     std::string keyAlias = "oh-app1-key-v1";
36     std::string issuerkeyAlias = "oh-app-sign-srv-ca-key-v1";
37     char keyPwd[] = "123456";
38     std::string keyAlg = "ECC";
39     int keySize = 256;
40     std::string keystoreFile = "./generateCA/OpenHarmony.p12";
41     char keystorePwd[] = "123456";
42     std::string signAlg = "SHA256withECDSA";
43     std::string subject = "C=CN,O=OpenHarmony,OU=OpenHarmony Community,CN= Openharmony Application CA";
44     std::string issuer = "C=CN,O=OpenHarmony_test,OU=OpenHarmony Community,CN= Openharmony Application SUB  CA";
45     char isksPwd[] = "123456";
46     (*params)["keystorePwd"] = keystorePwd;
47     (*params)["issuerKeystorePwd"] = isksPwd;
48     (*params)["keyAlias"] = keyAlias;
49     (*params)["keyPwd"] = keyPwd;
50     (*params)["keyAlg"] = keyAlg;
51     (*params)["keySize"] = keySize;
52     (*params)["keystoreFile"] = keystoreFile;
53     (*params)["signAlg"] = signAlg;
54     (*params)["subject"] = subject;
55     (*params)["issuer"] = issuer;
56     (*params)["issuerKeyAlias"] = issuerkeyAlias;
57     std::unique_ptr<LocalizationAdapter> adaptePtr = std::make_unique<LocalizationAdapter>(params.get());
58     EVP_PKEY* keyPair = nullptr;
59     keyPair = adaptePtr->GetAliasKey(true);
60     X509_REQ* csr = CertTools::GenerateCsr(keyPair, signAlg, subject);
61     CertTools::GenerateSubCert(keyPair, csr, params.get());
62     return true;
63 }
64 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)65 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
66 {
67     bool ret = false;
68     std::shared_ptr<SignToolServiceImpl> api = std::make_shared<SignToolServiceImpl>();
69     std::shared_ptr<Options> params = std::make_shared<Options>();
70     std::string keyAlias = "oh-app1-key-v1";
71     std::string issuerkeyAlias = "oh-root-ca-key-v1";
72     std::string keystoreFile = "./generateCA/OpenHarmony.p12";
73     std::string signAlg = "SHA256withECDSA";
74     std::string subject = "C=CN,O=OpenHarmony,OU=OpenHarmony Community,CN= Openharmony Application CA";
75     std::string issuer = "C=CN,O=OpenHarmony_test,OU=OpenHarmony Community,CN= Openharmony Application SUB  CA";
76     bool keyUsage = true;
77     std::string basicConstraints = "true";
78     std::string basicConstraintsCritical = "true";
79     std::string basicConstraintsCa = "true";
80     bool keyUsageCritical = true;
81     char secret[] = "123456";
82     char isksPwd[] = "123456";
83     char keystorePwd[] = "123456";
84     char issuerkeypwd[] = "123456";
85     int validity = 365;
86     std::string outfile = "./generateCA/general.cer";
87     (*params)["keyPwd"] = secret;
88     (*params)["issuerKeystorePwd"] = isksPwd;
89     (*params)["issuerKeyPwd"] = issuerkeypwd;
90     (*params)["keyAlias"] = keyAlias;
91     (*params)["keystoreFile"] = keystoreFile;
92     (*params)["keystorePwd"] = keystorePwd;
93     (*params)["signAlg"] = signAlg;
94     (*params)["subject"] = subject;
95     (*params)["issuer"] = issuer;
96     (*params)["issuerKeyAlias"] = issuerkeyAlias;
97     (*params)["keyUsage"] = keyUsage;
98     (*params)["basicConstraints"] = basicConstraints;
99     (*params)["basicConstraintsCritical"] = basicConstraintsCritical;
100     (*params)["basicConstraintsCa"] = basicConstraintsCa;
101     (*params)["keyUsageCritical"] = keyUsageCritical;
102     (*params)["validity"] = validity;
103     (*params)["outFile"] = outfile;
104     api->GenerateCert(params.get());
105     ret = api->OutputString("hello world", "./generateCA/tmp.data");
106     return ret;
107 }
108 }
109 }
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114     /* Run your code on data */
115     OHOS::SignatureTools::TestGenerateSubCert(data, size);
116     OHOS::SignatureTools::DoSomethingInterestingWithMyAPI(data, size);
117     return 0;
118 }