1 /*
2  * Copyright (c) 2024-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 <memory>
17 #include <fstream>
18 #include <gtest/gtest.h>
19 
20 #include "verify_bin.h"
21 #include "sign_bin.h"
22 #include "hash_utils.h"
23 
24 namespace OHOS {
25 namespace SignatureTools {
26 
27 class VerifyBinTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase();
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void VerifyBinTest::SetUpTestCase(void)
36 {
37     (void)rename("./elfVerify/bin_verify_digest_err_package.txt", "./elfVerify/bin_verify_digest_err_package.bin");
38     (void)rename("./elfVerify/bin_get_profile_err_package.txt", "./elfVerify/bin_get_profile_err_package.bin");
39     (void)rename("./elfVerify/bin_get_digest_err_package.txt", "./elfVerify/bin_get_digest_err_package.bin");
40     (void)rename("./elfVerify/bin_signed_package.txt", "./elfVerify/bin_signed_package.bin");
41     (void)rename("./elfVerify/bin_unsigned_package.txt", "./elfVerify/bin_unsigned_package.bin");
42 }
43 
TearDownTestCase(void)44 void VerifyBinTest::TearDownTestCase(void)
45 {
46 }
47 
SetUp()48 void VerifyBinTest::SetUp()
49 {
50 }
51 
TearDown()52 void VerifyBinTest::TearDown()
53 {
54 }
55 
56 static const std::map<std::string, std::string> PARAMS = { {"keyPwd", "123456"},
57                                                            {"mode", "localSign"},
58                                                            {"keyAlias", "oh-app1-key-v1"},
59                                                            {"signAlg", "SHA256withECDSA"},
60                                                            {"appCertFile", "./hapSign/app-release1.pem"},
61                                                            {"signCode", "1"},
62                                                            {"compatibleVersion", "9"},
63                                                            {"keystorePwd", "123456"},
64                                                            {"outFile", "./elfVerify/bin_signed_package.bin"},
65                                                            {"profileSigned", "1"},
66                                                            {"profileFile", "./hapSign/signed-profile.p7b"},
67                                                            {"keystoreFile", "./hapSign/ohtest.jks"},
68                                                            {"inFile", "./elfVerify/bin_unsigned_package.bin"} };
69 
70 /**
71  * @tc.name: Verify001
72  * @tc.desc: Test function of VerifyBinTest::Verify() interface for SUCCESS.
73  * @tc.size: MEDIUM
74  * @tc.type: FUNC
75  * @tc.level Level 1
76  * @tc.require: SR000H63TL
77  */
HWTEST_F(VerifyBinTest, Verify001, testing::ext::TestSize.Level1)78 HWTEST_F(VerifyBinTest, Verify001, testing::ext::TestSize.Level1)
79 {
80     Options options;
81     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_signed_package.bin"));
82     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
83     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
84 
85     VerifyBin verifyBin;
86     bool flag = verifyBin.Verify(&options);
87 
88     EXPECT_EQ(flag, true);
89 }
90 
91 /**
92  * @tc.name: Verify002
93  * @tc.desc: param not exist
94  * @tc.size: MEDIUM
95  * @tc.type: FUNC
96  * @tc.level Level 1
97  * @tc.require: SR000H63TL
98  */
HWTEST_F(VerifyBinTest, Verify002, testing::ext::TestSize.Level1)99 HWTEST_F(VerifyBinTest, Verify002, testing::ext::TestSize.Level1)
100 {
101     VerifyBin verifyBin;
102     bool flag = verifyBin.Verify(nullptr);
103 
104     EXPECT_EQ(flag, false);
105 }
106 
107 /**
108  * @tc.name: Verify003
109  * @tc.desc: cer file not exist
110  * @tc.size: MEDIUM
111  * @tc.type: FUNC
112  * @tc.level Level 1
113  * @tc.require: SR000H63TL
114  */
HWTEST_F(VerifyBinTest, Verify003, testing::ext::TestSize.Level1)115 HWTEST_F(VerifyBinTest, Verify003, testing::ext::TestSize.Level1)
116 {
117     Options options;
118     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_signed_package.bin"));
119     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
120 
121     VerifyBin verifyBin;
122     bool flag = verifyBin.Verify(&options);
123 
124     EXPECT_EQ(flag, false);
125 }
126 
127 /**
128  * @tc.name: Verify004
129  * @tc.desc: p7b file not exist
130  * @tc.size: MEDIUM
131  * @tc.type: FUNC
132  * @tc.level Level 1
133  * @tc.require: SR000H63TL
134  */
HWTEST_F(VerifyBinTest, Verify004, testing::ext::TestSize.Level1)135 HWTEST_F(VerifyBinTest, Verify004, testing::ext::TestSize.Level1)
136 {
137     Options options;
138     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_signed_package.bin"));
139     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
140 
141     VerifyBin verifyBin;
142     bool flag = verifyBin.Verify(&options);
143 
144     EXPECT_EQ(flag, false);
145 }
146 
147 /**
148  * @tc.name: Verify005
149  * @tc.desc: input file not exist
150  * @tc.size: MEDIUM
151  * @tc.type: FUNC
152  * @tc.level Level 1
153  * @tc.require: SR000H63TL
154  */
HWTEST_F(VerifyBinTest, Verify005, testing::ext::TestSize.Level1)155 HWTEST_F(VerifyBinTest, Verify005, testing::ext::TestSize.Level1)
156 {
157     Options options;
158     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
159     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
160 
161     VerifyBin verifyBin;
162     bool flag = verifyBin.Verify(&options);
163 
164     EXPECT_EQ(flag, false);
165 }
166 
167 /**
168  * @tc.name: Verify006
169  * @tc.desc: invalid input file
170  * @tc.size: MEDIUM
171  * @tc.type: FUNC
172  * @tc.level Level 1
173  * @tc.require: SR000H63TL
174  */
HWTEST_F(VerifyBinTest, Verify006, testing::ext::TestSize.Level1)175 HWTEST_F(VerifyBinTest, Verify006, testing::ext::TestSize.Level1)
176 {
177     Options options;
178     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_signed_package111.bin"));
179     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
180     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
181 
182     VerifyBin verifyBin;
183     bool flag = verifyBin.Verify(&options);
184 
185     EXPECT_EQ(flag, false);
186 }
187 
188 /**
189  * @tc.name: Verify007
190  * @tc.desc: unsigned wrong input file
191  * @tc.size: MEDIUM
192  * @tc.type: FUNC
193  * @tc.level Level 1
194  * @tc.require: SR000H63TL
195  */
HWTEST_F(VerifyBinTest, Verify007, testing::ext::TestSize.Level1)196 HWTEST_F(VerifyBinTest, Verify007, testing::ext::TestSize.Level1)
197 {
198     Options options;
199     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_unsigned_package.bin"));
200     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
201     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
202 
203     VerifyBin verifyBin;
204     bool flag = verifyBin.Verify(&options);
205 
206     EXPECT_EQ(flag, false);
207 }
208 
209 /**
210  * @tc.name: Verify008
211  * @tc.desc: Test function of VerifyBinTest::Verify() interface for SUCCESS.
212  * @tc.size: MEDIUM
213  * @tc.type: FUNC
214  * @tc.level Level 1
215  * @tc.require: SR000H63TL
216  */
HWTEST_F(VerifyBinTest, Verify008, testing::ext::TestSize.Level1)217 HWTEST_F(VerifyBinTest, Verify008, testing::ext::TestSize.Level1)
218 {
219     Options options;
220     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_signed_package.bin"));
221     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/readonly.cer"));
222     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
223 
224     VerifyBin verifyBin;
225     bool flag = verifyBin.Verify(&options);
226 
227     EXPECT_EQ(flag, true);
228 }
229 
230 /**
231  * @tc.name: Verify009
232  * @tc.desc: verify bin digest error
233  * @tc.size: MEDIUM
234  * @tc.type: FUNC
235  * @tc.level Level 1
236  * @tc.require: SR000H63TL
237  */
HWTEST_F(VerifyBinTest, Verify009, testing::ext::TestSize.Level1)238 HWTEST_F(VerifyBinTest, Verify009, testing::ext::TestSize.Level1)
239 {
240     Options options;
241     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_verify_digest_err_package.bin"));
242     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
243     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
244 
245     VerifyBin verifyBin;
246     bool flag = verifyBin.Verify(&options);
247 
248     EXPECT_EQ(flag, false);
249 }
250 
251 /**
252  * @tc.name: Verify010
253  * @tc.desc: get profile error
254  * @tc.size: MEDIUM
255  * @tc.type: FUNC
256  * @tc.level Level 1
257  * @tc.require: SR000H63TL
258  */
HWTEST_F(VerifyBinTest, Verify010, testing::ext::TestSize.Level1)259 HWTEST_F(VerifyBinTest, Verify010, testing::ext::TestSize.Level1)
260 {
261     Options options;
262     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_get_profile_err_package.bin"));
263     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
264     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
265 
266     VerifyBin verifyBin;
267     bool flag = verifyBin.Verify(&options);
268 
269     EXPECT_EQ(flag, false);
270 }
271 
272 /**
273  * @tc.name: Verify011
274  * @tc.desc: get bin digest error
275  * @tc.size: MEDIUM
276  * @tc.type: FUNC
277  * @tc.level Level 1
278  * @tc.require: SR000H63TL
279  */
HWTEST_F(VerifyBinTest, Verify011, testing::ext::TestSize.Level1)280 HWTEST_F(VerifyBinTest, Verify011, testing::ext::TestSize.Level1)
281 {
282     Options options;
283     options.emplace(Options::IN_FILE, std::string("./elfVerify/bin_get_digest_err_package.bin"));
284     options.emplace(Options::OUT_CERT_CHAIN, std::string("./elfVerify/xx.cer"));
285     options.emplace(Options::OUT_PROFILE, std::string("./elfVerify/xx.p7b"));
286 
287     VerifyBin verifyBin;
288     bool flag = verifyBin.Verify(&options);
289 
290     EXPECT_EQ(flag, false);
291 }
292 
293 /**
294  * @tc.name: SignBin001
295  * @tc.desc: sign bin with wrong compatible version
296  * @tc.size: MEDIUM
297  * @tc.type: FUNC
298  * @tc.level Level 1
299  * @tc.require: SR000H63TL
300  */
HWTEST_F(VerifyBinTest, SignBin001, testing::ext::TestSize.Level1)301 HWTEST_F(VerifyBinTest, SignBin001, testing::ext::TestSize.Level1)
302 {
303     std::shared_ptr<SignBin> api = std::make_shared<SignBin>();
304     std::map<std::string, std::string> signParams;
305     signParams["a"] = "4";
306     signParams["appCertFile"] = "./hapSign/app-release1.pem";
307     signParams["compatibleVersion"] = "9";
308     signParams["inFile"] = "./elfVerify/bin_unsigned_package.bin";
309     signParams["inForm"] = "bin";
310     signParams["keyAlias"] = "oh-app1-key-v1";
311     signParams["keyPwd"] = "123456";
312     signParams["keystoreFile"] = "./hapSign/ohtest.p12";
313     signParams["keystorePwd"] = "123456";
314     signParams["outFile"] = "./elfVerify/bin_signed_package.bin";
315     signParams["profileFile"] = "./hapSign/signed-profile.p7b";
316     signParams["profileSigned"] = "1";
317     signParams["signAlg"] = "SHA256withECDSA";
318     signParams["signCode"] = "1";
319 
320     SignerConfig signerConfig;
321     signerConfig.SetCompatibleVersion(9);
322     signerConfig.FillParameters(PARAMS);
323 
324     ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
325     std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
326     SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::ECDSA_WITH_SHA256, "ECDSA_WITH_SHA256",
327                                                 contentDigestAlgorithm, signatureAlgAndParams);
328     std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
329     signatureAlgorithms.push_back(signatureAlgorithm);
330     signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
331 
332     Options options;
333     options.emplace("mode", std::string("localSign"));
334     options.emplace("keyPwd", std::string("123456"));
335     options.emplace("outFile", std::string("./elfVerify/bin_signed_package.bin"));
336     options.emplace("keyAlias", std::string("oh-app1-key-v1"));
337     options.emplace("profileFile", std::string("./hapSign/signed-profile.p7b"));
338     options.emplace("signAlg", std::string("SHA256withECDSA"));
339     options.emplace("keystorePwd", std::string("123456"));
340     options.emplace("keystoreFile", std::string("./hapSign/ohtest.p12"));
341     options.emplace("appCertFile", std::string("./hapSign/app-release1.pem"));
342     options.emplace("inFile", std::string("./elfVerify/bin_unsigned_package.bin"));
343     signerConfig.SetOptions(&options);
344     bool flag = api->Sign(signerConfig, signParams);
345 
346     EXPECT_EQ(flag, false);
347 }
348 
349 /**
350  * @tc.name: GetHashAlgsId001
351  * @tc.desc: get invalid alg id
352  * @tc.size: MEDIUM
353  * @tc.type: FUNC
354  * @tc.level Level 1
355  * @tc.require: SR000H63TL
356  */
HWTEST_F(VerifyBinTest, GetHashAlgsId001, testing::ext::TestSize.Level1)357 HWTEST_F(VerifyBinTest, GetHashAlgsId001, testing::ext::TestSize.Level1)
358 {
359     int algId = HashUtils::GetHashAlgsId("SHA-224");
360 
361     EXPECT_EQ(algId, 0);
362 }
363 
364 /**
365  * @tc.name: GetHashAlgsId002
366  * @tc.desc: get alg id of SHA-256
367  * @tc.size: MEDIUM
368  * @tc.type: FUNC
369  * @tc.level Level 1
370  * @tc.require: SR000H63TL
371  */
HWTEST_F(VerifyBinTest, GetHashAlgsId002, testing::ext::TestSize.Level1)372 HWTEST_F(VerifyBinTest, GetHashAlgsId002, testing::ext::TestSize.Level1)
373 {
374     int algId = HashUtils::GetHashAlgsId("SHA-256");
375 
376     EXPECT_EQ(algId, 6);
377 }
378 
379 /**
380  * @tc.name: GetHashAlgsId003
381  * @tc.desc: get alg id of SHA-384
382  * @tc.size: MEDIUM
383  * @tc.type: FUNC
384  * @tc.level Level 1
385  * @tc.require: SR000H63TL
386  */
HWTEST_F(VerifyBinTest, GetHashAlgsId003, testing::ext::TestSize.Level1)387 HWTEST_F(VerifyBinTest, GetHashAlgsId003, testing::ext::TestSize.Level1)
388 {
389     int algId = HashUtils::GetHashAlgsId("SHA-384");
390 
391     EXPECT_EQ(algId, 7);
392 }
393 
394 /**
395  * @tc.name: GetHashAlgsId004
396  * @tc.desc: get alg id of SHA-512
397  * @tc.size: MEDIUM
398  * @tc.type: FUNC
399  * @tc.level Level 1
400  * @tc.require: SR000H63TL
401  */
HWTEST_F(VerifyBinTest, GetHashAlgsId004, testing::ext::TestSize.Level1)402 HWTEST_F(VerifyBinTest, GetHashAlgsId004, testing::ext::TestSize.Level1)
403 {
404     int algId = HashUtils::GetHashAlgsId("SHA-512");
405 
406     EXPECT_EQ(algId, 8);
407 }
408 
409 /**
410  * @tc.name: GetHashAlgName001
411  * @tc.desc: get alg name of id 5
412  * @tc.size: MEDIUM
413  * @tc.type: FUNC
414  * @tc.level Level 1
415  * @tc.require: SR000H63TL
416  */
HWTEST_F(VerifyBinTest, GetHashAlgName001, testing::ext::TestSize.Level1)417 HWTEST_F(VerifyBinTest, GetHashAlgName001, testing::ext::TestSize.Level1)
418 {
419     std::string alg = HashUtils::GetHashAlgName(5);
420     int size = alg.size();
421     EXPECT_EQ(size, 0);
422 }
423 
424 /**
425  * @tc.name: GetHashAlgName002
426  * @tc.desc: get alg name of id 6
427  * @tc.size: MEDIUM
428  * @tc.type: FUNC
429  * @tc.level Level 1
430  * @tc.require: SR000H63TL
431  */
HWTEST_F(VerifyBinTest, GetHashAlgName002, testing::ext::TestSize.Level1)432 HWTEST_F(VerifyBinTest, GetHashAlgName002, testing::ext::TestSize.Level1)
433 {
434     std::string alg = HashUtils::GetHashAlgName(6);
435     int size = alg.size();
436     EXPECT_NE(size, 0);
437 }
438 
439 /**
440  * @tc.name: GetHashAlgName003
441  * @tc.desc: get alg name of id 7
442  * @tc.size: MEDIUM
443  * @tc.type: FUNC
444  * @tc.level Level 1
445  * @tc.require: SR000H63TL
446  */
HWTEST_F(VerifyBinTest, GetHashAlgName003, testing::ext::TestSize.Level1)447 HWTEST_F(VerifyBinTest, GetHashAlgName003, testing::ext::TestSize.Level1)
448 {
449     std::string alg = HashUtils::GetHashAlgName(7);
450     int size = alg.size();
451     EXPECT_NE(size, 0);
452 }
453 
454 /**
455  * @tc.name: GetHashAlgName004
456  * @tc.desc: get alg name of id 8
457  * @tc.size: MEDIUM
458  * @tc.type: FUNC
459  * @tc.level Level 1
460  * @tc.require: SR000H63TL
461  */
HWTEST_F(VerifyBinTest, GetHashAlgName004, testing::ext::TestSize.Level1)462 HWTEST_F(VerifyBinTest, GetHashAlgName004, testing::ext::TestSize.Level1)
463 {
464     std::string alg = HashUtils::GetHashAlgName(8);
465     int size = alg.size();
466     EXPECT_NE(size, 0);
467 }
468 
469 /**
470  * @tc.name: GetDigestFromBytes001
471  * @tc.desc: get digest from bytes with 0 size file bytes
472  * @tc.size: MEDIUM
473  * @tc.type: FUNC
474  * @tc.level Level 1
475  * @tc.require: SR000H63TL
476  */
HWTEST_F(VerifyBinTest, GetDigestFromBytes001, testing::ext::TestSize.Level1)477 HWTEST_F(VerifyBinTest, GetDigestFromBytes001, testing::ext::TestSize.Level1)
478 {
479     std::vector<int8_t> fileBytes;
480     int64_t length = 0;
481     std::string algName = "SHA-256";
482     std::vector<int8_t> dig = HashUtils::GetDigestFromBytes(fileBytes, length, algName);
483     int size = dig.size();
484 
485     EXPECT_EQ(size, 0);
486 }
487 
488 /**
489  * @tc.name: GetDigestFromBytes002
490  * @tc.desc: get digest from bytes with some size file bytes
491  * @tc.size: MEDIUM
492  * @tc.type: FUNC
493  * @tc.level Level 1
494  * @tc.require: SR000H63TL
495  */
HWTEST_F(VerifyBinTest, GetDigestFromBytes002, testing::ext::TestSize.Level1)496 HWTEST_F(VerifyBinTest, GetDigestFromBytes002, testing::ext::TestSize.Level1)
497 {
498     std::vector<int8_t> fileBytes = { 1, 1 };
499     int64_t length = 0;
500     std::string algName = "SHA-256";
501     std::vector<int8_t> dig = HashUtils::GetDigestFromBytes(fileBytes, length, algName);
502     int size = dig.size();
503 
504     EXPECT_EQ(size, 0);
505 }
506 } // namespace SignatureTools
507 } // namespace OHOS