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 #include "code_signing.h"
20 #include "zip_signer.h"
21 #include "sign_provider.h"
22 #include "local_sign_provider.h"
23 #include "unzip_handle_param.h"
24
25 namespace OHOS {
26 namespace SignatureTools {
27
28 class CodeSigningTest : public testing::Test {
29 public:
SetUpTestCase(void)30 static void SetUpTestCase(void)
31 {
32 (void)rename("./codeSigning/entry-default-unsigned-so.txt", "./codeSigning/entry-default-unsigned-so.hap");
33 (void)rename("./codeSigning/entry-default-unsigned-so.txt", "./codeSigning/entry-default-unsigned-so.elf");
34 (void)rename("./codeSigning/unsigned-file.txt", "./codeSigning/unsigned-file.hap");
35 }
TearDownTestCase()36 static void TearDownTestCase()
37 {
38 }
SetUp()39 void SetUp()
40 {
41 };
TearDown()42 void TearDown()
43 {
44 };
45 };
46
47 /**
48 * @tc.name: generateSignature001
49 * @tc.desc: generate signature with signed data
50 * @tc.size: MEDIUM
51 * @tc.type: FUNC
52 * @tc.level Level 1
53 * @tc.require: SR000H63TL
54 */
55 HWTEST_F(CodeSigningTest, generateSignature001, testing::ext::TestSize.Level1)
56 {
57 SignerConfig signerConfig;
58 signerConfig.SetCompatibleVersion(9);
59
60 std::map<std::string, std::string> params;
61 params["keyPwd"] = "123456";
62 params["mode"] = "localSign";
63 params["keyAlias"] = "oh-app1-key-v1";
64 params["signAlg"] = "SHA256withECDSA";
65 params["appCertFile"] = "./codeSigning/app-release1.pem";
66 params["signCode"] = "1";
67 params["compatibleVersion"] = "9";
68 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
69 params["profileFile"] = "./codeSigning/signed-profile.p7b";
70 params["keystorePwd"] = "123456";
71 params["keystoreFile"] = "./codeSigning/ohtest.jks";
72 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
73 params["profileSigned"] = "1";
74 signerConfig.FillParameters(params);
75
76 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
77 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
78 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
79 contentDigestAlgorithm, signatureAlgAndParams);
80 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
81 signatureAlgorithms.push_back(signatureAlgorithm);
82 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
83
84 Options options;
85 options.emplace("mode", std::string("localSign"));
86 options.emplace("keyPwd", std::string("123456"));
87 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
88 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
89 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
90 options.emplace("signAlg", std::string("SHA256withECDSA"));
91 options.emplace("keystorePwd", std::string("123456"));
92 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
93 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
94 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
95 signerConfig.SetOptions(&options);
96
97 CodeSigning codeSigning(&signerConfig);
98
99 std::vector<int8_t> signedData = {70, 83, 86, 101, 114, 105, 116, 121, 1,
100 0, 32, 0, -82, 98, 15, 102, 95, -26, -90, 88, 83, 8, -42, -65, -121,
101 117, -43, -95, -102, -56, 109, 93, 25, -9, -88, 44, -25, 119, -39, -68,
102 -15, 11, 123, -80};
103 std::string ownerID;
104 std::vector<int8_t> ret;
105 bool flag = codeSigning.GenerateSignature(signedData, ownerID, ret);
106 EXPECT_EQ(flag, false);
107 }
108
109 /**
110 * @tc.name: generateSignature002
111 * @tc.desc: generate signature without signed data
112 * @tc.size: MEDIUM
113 * @tc.type: FUNC
114 * @tc.level Level 1
115 * @tc.require: SR000H63TL
116 */
117 HWTEST_F(CodeSigningTest, generateSignature002, testing::ext::TestSize.Level1)
118 {
119 SignerConfig signerConfig;
120 signerConfig.SetCompatibleVersion(9);
121
122 std::map<std::string, std::string> params;
123 params["keyPwd"] = "123456";
124 params["mode"] = "localSign";
125 params["keyAlias"] = "oh-app1-key-v1";
126 params["signAlg"] = "SHA256withECDSA";
127 params["appCertFile"] = "./codeSigning/app-release1.pem";
128 params["signCode"] = "1";
129 params["compatibleVersion"] = "9";
130 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
131 params["profileFile"] = "./codeSigning/signed-profile.p7b";
132 params["keystorePwd"] = "123456";
133 params["keystoreFile"] = "./codeSigning/ohtest.jks";
134 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
135 params["profileSigned"] = "1";
136 signerConfig.FillParameters(params);
137
138 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
139 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
140 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
141 contentDigestAlgorithm, signatureAlgAndParams);
142 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
143 signatureAlgorithms.push_back(signatureAlgorithm);
144 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
145
146 Options options;
147 options.emplace("mode", std::string("localSign"));
148 options.emplace("keyPwd", std::string("123456"));
149 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
150 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
151 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
152 options.emplace("signAlg", std::string("SHA256withECDSA"));
153 options.emplace("keystorePwd", std::string("123456"));
154 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
155 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
156 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
157 signerConfig.SetOptions(&options);
158
159 CodeSigning codeSigning(&signerConfig);
160
161 std::vector<int8_t> signedData;
162 std::string ownerID;
163 std::vector<int8_t> ret;
164 bool flag = codeSigning.GenerateSignature(signedData, ownerID, ret);
165 EXPECT_EQ(flag, false);
166 }
167
168 /**
169 * @tc.name: GetNativeEntriesFromHap
170 * @tc.desc: Test function of CodeSigningTest::GetNativeEntriesFromHap() interface for FAILED.
171 * @tc.size: MEDIUM
172 * @tc.type: FUNC
173 * @tc.level Level 1
174 * @tc.require: SR000H63TL
175 */
176 HWTEST_F(CodeSigningTest, GetNativeEntriesFromHap, testing::ext::TestSize.Level1)
177 {
178 SignerConfig signerConfig;
179 signerConfig.SetCompatibleVersion(9);
180
181 std::map<std::string, std::string> params;
182 params["keyPwd"] = "123456";
183 params["mode"] = "localSign";
184 params["keyAlias"] = "oh-app1-key-v1";
185 params["signAlg"] = "SHA256withECDSA";
186 params["appCertFile"] = "./codeSigning/app-release1.pem";
187 params["signCode"] = "1";
188 params["compatibleVersion"] = "9";
189 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
190 params["profileFile"] = "./codeSigning/signed-profile.p7b";
191 params["keystorePwd"] = "123456";
192 params["keystoreFile"] = "./codeSigning/ohtest.jks";
193 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
194 params["profileSigned"] = "1";
195 signerConfig.FillParameters(params);
196
197 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
198 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
199 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
200 contentDigestAlgorithm, signatureAlgAndParams);
201 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
202 signatureAlgorithms.push_back(signatureAlgorithm);
203 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
204
205 Options options;
206 options.emplace("mode", std::string("localSign"));
207 options.emplace("keyPwd", std::string("123456"));
208 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
209 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
210 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
211 options.emplace("signAlg", std::string("SHA256withECDSA"));
212 options.emplace("keystorePwd", std::string("123456"));
213 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
214 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
215 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
216 signerConfig.SetOptions(&options);
217
218 CodeSigning codeSigning(&signerConfig);
219
220 std::string packageName = "./codeSigning/entry-default-unsigned-so.hap";
221 std::vector<std::pair<std::string, SignInfo>> ret;
222 std::string ownerID;
223 UnzipHandleParam param(ret, ownerID, true);
224 bool flag = codeSigning.GetNativeEntriesFromHap(packageName, param);
225 EXPECT_EQ(flag, false);
226 }
227
228 /**
229 * @tc.name: getTimestamp
230 * @tc.desc: Test function of CodeSigningTest::getTimestamp() interface for SUCCESS.
231 * @tc.size: MEDIUM
232 * @tc.type: FUNC
233 * @tc.level Level 1
234 * @tc.require: SR000H63TL
235 */
236 HWTEST_F(CodeSigningTest, getTimestamp, testing::ext::TestSize.Level1)
237 {
238 SignerConfig signerConfig;
239 signerConfig.SetCompatibleVersion(9);
240
241 std::map<std::string, std::string> params;
242 params["keyPwd"] = "123456";
243 params["mode"] = "localSign";
244 params["keyAlias"] = "oh-app1-key-v1";
245 params["signAlg"] = "SHA256withECDSA";
246 params["appCertFile"] = "./codeSigning/app-release1.pem";
247 params["signCode"] = "1";
248 params["compatibleVersion"] = "9";
249 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
250 params["profileFile"] = "./codeSigning/signed-profile.p7b";
251 params["keystorePwd"] = "123456";
252 params["keystoreFile"] = "./codeSigning/ohtest.jks";
253 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
254 params["profileSigned"] = "1";
255 signerConfig.FillParameters(params);
256
257 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
258 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
259 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
260 contentDigestAlgorithm, signatureAlgAndParams);
261 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
262 signatureAlgorithms.push_back(signatureAlgorithm);
263 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
264
265 Options options;
266 options.emplace("mode", std::string("localSign"));
267 options.emplace("keyPwd", std::string("123456"));
268 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
269 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
270 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
271 options.emplace("signAlg", std::string("SHA256withECDSA"));
272 options.emplace("keystorePwd", std::string("123456"));
273 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
274 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
275 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
276 signerConfig.SetOptions(&options);
277
278 CodeSigning codeSigning(&signerConfig);
279 codeSigning.UpdateCodeSignBlock();
280 int64_t timeStamp = codeSigning.GetTimestamp();
281 EXPECT_NE(timeStamp, 0);
282 }
283
284 /**
285 * @tc.name: isNativeFile
286 * @tc.desc: Test function of CodeSigningTest::isNativeFile() interface for SUCCESS.
287 * @tc.size: MEDIUM
288 * @tc.type: FUNC
289 * @tc.level Level 1
290 * @tc.require: SR000H63TL
291 */
292 HWTEST_F(CodeSigningTest, isNativeFile, testing::ext::TestSize.Level1)
293 {
294 SignerConfig signerConfig;
295 signerConfig.SetCompatibleVersion(9);
296
297 std::map<std::string, std::string> params;
298 params["keyPwd"] = "123456";
299 params["mode"] = "localSign";
300 params["keyAlias"] = "oh-app1-key-v1";
301 params["signAlg"] = "SHA256withECDSA";
302 params["appCertFile"] = "./codeSigning/app-release1.pem";
303 params["signCode"] = "1";
304 params["compatibleVersion"] = "9";
305 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
306 params["profileFile"] = "./codeSigning/signed-profile.p7b";
307 params["keystorePwd"] = "123456";
308 params["keystoreFile"] = "./codeSigning/ohtest.jks";
309 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
310 params["profileSigned"] = "1";
311 signerConfig.FillParameters(params);
312
313 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
314 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
315 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
316 contentDigestAlgorithm, signatureAlgAndParams);
317 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
318 signatureAlgorithms.push_back(signatureAlgorithm);
319 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
320
321 Options options;
322 options.emplace("mode", std::string("localSign"));
323 options.emplace("keyPwd", std::string("123456"));
324 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
325 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
326 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
327 options.emplace("signAlg", std::string("SHA256withECDSA"));
328 options.emplace("keystorePwd", std::string("123456"));
329 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
330 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
331 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
332 signerConfig.SetOptions(&options);
333
334 CodeSigning codeSigning(&signerConfig);
335 std::string input = "libs/arm64-v8a/libc++_shared.so";
336 bool flag = codeSigning.IsNativeFile(input);
337 EXPECT_EQ(flag, true);
338 }
339
340 /**
341 * @tc.name: signFile
342 * @tc.desc: Test function of CodeSigningTest::signFile() interface for FAILED.
343 * @tc.size: MEDIUM
344 * @tc.type: FUNC
345 * @tc.level Level 1
346 * @tc.require: SR000H63TL
347 */
348 HWTEST_F(CodeSigningTest, signFile, testing::ext::TestSize.Level1)
349 {
350 SignerConfig signerConfig;
351 signerConfig.SetCompatibleVersion(9);
352
353 std::map<std::string, std::string> params;
354 params["keyPwd"] = "123456";
355 params["mode"] = "localSign";
356 params["keyAlias"] = "oh-app1-key-v1";
357 params["signAlg"] = "SHA256withECDSA";
358 params["appCertFile"] = "./codeSigning/app-release1.pem";
359 params["signCode"] = "1";
360 params["compatibleVersion"] = "9";
361 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
362 params["profileFile"] = "./codeSigning/signed-profile.p7b";
363 params["keystorePwd"] = "123456";
364 params["keystoreFile"] = "./codeSigning/ohtest.jks";
365 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
366 params["profileSigned"] = "1";
367 signerConfig.FillParameters(params);
368
369 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
370 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
371 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
372 contentDigestAlgorithm, signatureAlgAndParams);
373 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
374 signatureAlgorithms.push_back(signatureAlgorithm);
375 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
376
377 Options options;
378 options.emplace("mode", std::string("localSign"));
379 options.emplace("keyPwd", std::string("123456"));
380 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
381 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
382 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
383 options.emplace("signAlg", std::string("SHA256withECDSA"));
384 options.emplace("keystorePwd", std::string("123456"));
385 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
386 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
387 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
388 signerConfig.SetOptions(&options);
389
390 CodeSigning codeSigning(&signerConfig);
391
392 std::ifstream inputStream;
393 inputStream.open("./codeSigning/entry-default-unsigned-so.hap", std::ios::binary);
394 int64_t fileSize = 3479976;
395 bool storeTree = true;
396 int64_t fsvTreeOffset = 1024;
397 std::string ownerID;
398 std::pair<SignInfo, std::vector<int8_t>> ret;
399 bool flag = codeSigning.SignFile(inputStream, fileSize, storeTree, fsvTreeOffset, ownerID, ret);
400 EXPECT_EQ(flag, false);
401 }
402
403 /**
404 * @tc.name: signNativeLibs
405 * @tc.desc: Test function of CodeSigningTest::signNativeLibs() interface for FAILED.
406 * @tc.size: MEDIUM
407 * @tc.type: FUNC
408 * @tc.level Level 1
409 * @tc.require: SR000H63TL
410 */
411 HWTEST_F(CodeSigningTest, signNativeLibs, testing::ext::TestSize.Level1)
412 {
413 SignerConfig signerConfig;
414 signerConfig.SetCompatibleVersion(9);
415
416 std::map<std::string, std::string> params;
417 params["keyPwd"] = "123456";
418 params["mode"] = "localSign";
419 params["keyAlias"] = "oh-app1-key-v1";
420 params["signAlg"] = "SHA256withECDSA";
421 params["appCertFile"] = "./codeSigning/app-release1.pem";
422 params["signCode"] = "1";
423 params["compatibleVersion"] = "9";
424 params["outFile"] = "./codeSigning/entry-default-signed-so.hap";
425 params["profileFile"] = "./codeSigning/signed-profile.p7b";
426 params["keystorePwd"] = "123456";
427 params["keystoreFile"] = "./codeSigning/ohtest.jks";
428 params["inFile"] = "./codeSigning/entry-default-unsigned-so.hap";
429 params["profileSigned"] = "1";
430 signerConfig.FillParameters(params);
431
432 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
433 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
434 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
435 contentDigestAlgorithm, signatureAlgAndParams);
436 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
437 signatureAlgorithms.push_back(signatureAlgorithm);
438 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
439
440 Options options;
441 options.emplace("mode", std::string("localSign"));
442 options.emplace("keyPwd", std::string("123456"));
443 options.emplace("outFile", std::string("./codeSigning/entry-default-signed-so.hap"));
444 options.emplace("keyAlias", std::string("oh-app1-key-v1"));
445 options.emplace("profileFile", std::string("./codeSigning/signed-profile.p7b"));
446 options.emplace("signAlg", std::string("SHA256withECDSA"));
447 options.emplace("keystorePwd", std::string("123456"));
448 options.emplace("keystoreFile", std::string("./codeSigning/ohtest.jks"));
449 options.emplace("appCertFile", std::string("./codeSigning/app-release1.pem"));
450 options.emplace("inFile", std::string("./codeSigning/entry-default-unsigned-so.hap"));
451 signerConfig.SetOptions(&options);
452
453 CodeSigning codeSigning(&signerConfig);
454 std::string input = "./codeSigning/entry-default-unsigned-so.hap";
455 std::string ownerID;
456 bool flag = codeSigning.SignNativeLibs(input, ownerID);
457 EXPECT_EQ(flag, false);
458 }
459
460 /**
461 * @tc.name: AppendCodeSignBlock
462 * @tc.desc: Test function of CodeSigningTest::AppendCodeSignBlock() interface for FAILED.
463 * @tc.size: MEDIUM
464 * @tc.type: FUNC
465 * @tc.level Level 1
466 * @tc.require: SR000H63TL
467 */
468 HWTEST_F(CodeSigningTest, AppendCodeSignBlock, testing::ext::TestSize.Level1)
469 {
470 std::shared_ptr<LocalSignProvider> signProvider = std::make_unique<LocalSignProvider>();
471 std::shared_ptr<Options> params = std::make_shared<Options>();
472
473 std::string mode = "localSign";
474 std::string keyAlias = "oh-app1-key-v1";
475 std::string signAlg = "SHA256withECDSA";
476 std::string signCode = "1";
477 std::string appCertFile = "./codeSigning/app-release1.pem";
478 std::string profileFile = "./codeSigning/signed-profile.p7b";
479 std::string inFile = "./codeSigning/entry-default-unsigned-so.elf";
480 std::string keystoreFile = "./codeSigning/ohtest.p12";
481 std::string outFile = "./codeSigning/entry-default-signed-so.hap";
482 char keyPwd[] = "123456";
483 char keystorePwd[] = "123456";
484 std::string inForm = "elf";
485
486 (*params)["mode"] = mode;
487 (*params)["keyAlias"] = keyAlias;
488 (*params)["signAlg"] = signAlg;
489 (*params)["signCode"] = signCode;
490 (*params)["appCertFile"] = appCertFile;
491 (*params)["profileFile"] = profileFile;
492 (*params)["inFile"] = inFile;
493 (*params)["keystoreFile"] = keystoreFile;
494 (*params)["outFile"] = outFile;
495 (*params)["keyPwd"] = keyPwd;
496 (*params)["keystorePwd"] = keystorePwd;
497 (*params)["inForm"] = inForm;
498
499 bool ret = signProvider->Sign(params.get());
500 EXPECT_EQ(ret, false);
501 }
502
SetParamsMap(std::map<std::string,std::string> & params)503 void SetParamsMap(std::map<std::string, std::string>& params)
504 {
505 params["keyPwd"] = "123456";
506 params["mode"] = "localSign";
507 params["keyAlias"] = "oh-app1-key-v1";
508 params["signAlg"] = "SHA256withECDSA";
509 params["appCertFile"] = "./hapSign/app-release1.pem";
510 params["signCode"] = "1";
511 params["compatibleVersion"] = "9";
512 params["outFile"] = "./hapSign/entry-default-signed.elf";
513 params["profileFile"] = "./hapSign/signed-profile.p7b";
514 params["keystorePwd"] = "123456";
515 params["keystoreFile"] = "./hapSign/ohtest.jks";
516 params["inFile"] = "./codeSigning/unsigned-file.hap";
517 params["profileSigned"] = "1";
518 params["inForm"] = "elf";
519 std::string provision = "{\"app-distribution-type\": \"app_gallery\",\"bundle-info\":{\"app-"
520 "feature\":\"hos_system_app\",\"bundle-name\":\"com.OpenHarmony.app.test\",\"developer-id\":\"O"
521 "penHarmony\",\"distribution-certificate\":\"-----BEGIN CERTIFICATE-----\\n"
522 "MIICMzCCAbegAwIBAgIEaOC/zDAMBggqhkjOPQQDAwUAMGMxCzAJBgNVBAYTAkNO\\n"
523 "MRQwEgYDVQQKEwtPcGVuSGFybW9ueTEZMBcGA1UECxMQT3Blbkhhcm1vbnkgVGVh\\n"
524 "bTEjMCEGA1UEAxMaT3Blbkhhcm1vbnkgQXBwbGljYXRpb24gQ0EwHhcNMjEwMjAy\\n"
525 "MTIxOTMxWhcNNDkxMjMxMTIxOTMxWjBoMQswCQYDVQQGEwJDTjEUMBIGA1UEChML\\n"
526 "T3Blbkhhcm1vbnkxGTAXBgNVBAsTEE9wZW5IYXJtb255IFRlYW0xKDAmBgNVBAMT\\n"
527 "H09wZW5IYXJtb255IEFwcGxpY2F0aW9uIFJlbGVhc2UwWTATBgcqhkjOPQIBBggq\\n"
528 "hkjOPQMBBwNCAATbYOCQQpW5fdkYHN45v0X3AHax12jPBdEDosFRIZ1eXmxOYzSG\\n"
529 "JwMfsHhUU90E8lI0TXYZnNmgM1sovubeQqATo1IwUDAfBgNVHSMEGDAWgBTbhrci\\n"
530 "FtULoUu33SV7ufEFfaItRzAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0OBBYEFPtxruhl\\n"
531 "cRBQsJdwcZqLu9oNUVgaMAwGCCqGSM49BAMDBQADaAAwZQIxAJta0PQ2p4DIu/ps\\n"
532 "LMdLCDgQ5UH1l0B4PGhBlMgdi2zf8nk9spazEQI/0XNwpft8QAIwHSuA2WelVi/o\\n"
533 "zAlF08DnbJrOOtOnQq5wHOPlDYB4OtUzOYJk9scotrEnJxJzGsh/\\n"
534 "-----END CERTIFICATE-----\\n"
535 "\"},\"debug-info\":{\"device-id-type\":\"udid\",\"device-ids\":[\"69C7505BE341BDA5948C3C0CB"
536 "44ABCD530296054159EFE0BD16A16CD0129CC42\",\"7EED06506FCE6325EB2E2FAA019458B856AB10493A6718C76"
537 "79A73F958732865\"]},\"issuer\":\"pki_internal\",\"permissions\":{\"restricted-permissions\":"
538 "[\"\"]},\"type\":\"release\",\"uuid\":\"fe686e1b-3770-4824-a938-961b140a7c98\",\"validity\":"
539 "{\"not-after\":1705127532,\"not-before\":1610519532},\"version-code\":1,\"version-name\":\"1.0.0\"}";
540 params["profileContent"] = provision;
541 }
542
SetOptions(Options * options)543 void SetOptions(Options* options)
544 {
545 std::string mode = "localSign";
546 std::string keyAlias = "oh-app1-key-v1";
547 std::string signAlg = "SHA256withECDSA";
548 std::string signCode = "1";
549 std::string appCertFile = "./hapSign/app-release1.pem";
550 std::string profileFile = "./hapSign/signed-profile.p7b";
551 std::string inFile = "./codeSigning/unsigned-file.hap";
552 std::string keystoreFile = "./hapSign/ohtest.p12";
553 std::string outFile = "./hapSign/entry-default-signed.elf";
554 std::string inForm = "elf";
555 static char keyPwd[] = "123456";
556 static char keystorePwd[] = "123456";
557
558 (*options)["mode"] = "localSign";
559 (*options)["keyAlias"] = keyAlias;
560 (*options)["signAlg"] = signAlg;
561 (*options)["signCode"] = signCode;
562 (*options)["appCertFile"] = appCertFile;
563 (*options)["profileFile"] = profileFile;
564 (*options)["inFile"] = inFile;
565 (*options)["keystoreFile"] = keystoreFile;
566 (*options)["outFile"] = outFile;
567 (*options)["inForm"] = inForm;
568 (*options)["keyPwd"] = keyPwd;
569 (*options)["keystorePwd"] = keystorePwd;
570 }
571
572 /**
573 * @tc.name: GetElfCodeSignBlock
574 * @tc.desc: get elf code sign block without inForm
575 * @tc.size: MEDIUM
576 * @tc.type: FUNC
577 * @tc.level Level 1
578 * @tc.require: SR000H63TL
579 */
580 HWTEST_F(CodeSigningTest, GetElfCodeSignBlock001, testing::ext::TestSize.Level1)
581 {
582 // success
583 SignerConfig signerConfig;
584 signerConfig.SetCompatibleVersion(9);
585
586 std::map<std::string, std::string> params;
587 SetParamsMap(params);
588 signerConfig.FillParameters(params);
589
590 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
591 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
592 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::ECDSA_WITH_SHA256, "ECDSA_WITH_SHA256",
593 contentDigestAlgorithm, signatureAlgAndParams);
594 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
595 signatureAlgorithms.push_back(signatureAlgorithm);
596 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
597
598 std::shared_ptr<Options> options = std::make_shared<Options>();
599 SetOptions(options.get());
600 signerConfig.SetOptions(options.get());
601 signerConfig.GetSigner();
602
603 CodeSigning codeSigning(&signerConfig);
604 std::string inputFile = "./hapSign/tmpFile1718344274";
605 int64_t offset = 29184024;
606 std::vector<int8_t> codesignData;
607 std::string inForm = "elf";
608 bool ret = codeSigning.GetElfCodeSignBlock(inputFile, offset, inForm, params.at("profileContent"), codesignData);
609 EXPECT_EQ(ret, false);
610 }
611
612 /**
613 * @tc.name: GetElfCodeSignBlock002
614 * @tc.desc: get elf code sign block with bin inForm
615 * @tc.size: MEDIUM
616 * @tc.type: FUNC
617 * @tc.level Level 1
618 * @tc.require: SR000H63TL
619 */
620 HWTEST_F(CodeSigningTest, GetElfCodeSignBlock002, testing::ext::TestSize.Level1)
621 {
622 // inForm error
623 SignerConfig signerConfig;
624 signerConfig.SetCompatibleVersion(9);
625
626 std::map<std::string, std::string> params;
627 SetParamsMap(params);
628 params["inForm"] = "bin";
629 signerConfig.FillParameters(params);
630
631 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
632 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
633 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::ECDSA_WITH_SHA256, "ECDSA_WITH_SHA256",
634 contentDigestAlgorithm, signatureAlgAndParams);
635 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
636 signatureAlgorithms.push_back(signatureAlgorithm);
637 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
638
639 std::shared_ptr<Options> options = std::make_shared<Options>();
640 SetOptions(options.get());
641 (*options)["inForm"] = "bin";
642 signerConfig.SetOptions(options.get());
643 signerConfig.GetSigner();
644
645 CodeSigning codeSigning(&signerConfig);
646 std::string inputFile = "./hapSign/tmpFile1718344274";
647 int64_t offset = 29184024;
648 std::vector<int8_t> codesignData;
649 std::string inForm = "bin";
650 bool ret = codeSigning.GetElfCodeSignBlock(inputFile, offset, inForm, params.at("profileContent"), codesignData);
651 EXPECT_EQ(ret, false);
652 }
653
654 /**
655 * @tc.name: GetElfCodeSignBlock003
656 * @tc.desc: get elf code sign block with elf inForm
657 * @tc.size: MEDIUM
658 * @tc.type: FUNC
659 * @tc.level Level 1
660 * @tc.require: SR000H63TL
661 */
662 HWTEST_F(CodeSigningTest, GetElfCodeSignBlock003, testing::ext::TestSize.Level1)
663 {
664 // inFile error
665 SignerConfig signerConfig;
666 signerConfig.SetCompatibleVersion(9);
667
668 std::map<std::string, std::string> params;
669 SetParamsMap(params);
670 params["inFile"] = "./hapSign_test/tmpFile1718344274";
671 signerConfig.FillParameters(params);
672
673 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
674 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
675 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::ECDSA_WITH_SHA256, "ECDSA_WITH_SHA256",
676 contentDigestAlgorithm, signatureAlgAndParams);
677 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
678 signatureAlgorithms.push_back(signatureAlgorithm);
679 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
680
681 std::shared_ptr<Options> options = std::make_shared<Options>();
682 SetOptions(options.get());
683 (*options)["inFile"] = "./hapSign_test/tmpFile1718344274";
684 signerConfig.SetOptions(options.get());
685 signerConfig.GetSigner();
686
687 CodeSigning codeSigning(&signerConfig);
688 std::string inputFile = params.at("inFile");
689 int64_t offset = 29184024;
690 std::vector<int8_t> codesignData;
691 std::string inForm = "elf";
692 bool ret = codeSigning.GetElfCodeSignBlock(inputFile, offset, inForm, params.at("profileContent"), codesignData);
693 EXPECT_EQ(ret, false);
694 }
695
696 /**
697 * @tc.name: GetElfCodeSignBlock004
698 * @tc.desc: get elf code sign block without input file
699 * @tc.size: MEDIUM
700 * @tc.type: FUNC
701 * @tc.level Level 1
702 * @tc.require: SR000H63TL
703 */
704 HWTEST_F(CodeSigningTest, GetElfCodeSignBlock004, testing::ext::TestSize.Level1)
705 {
706 // signatureAlgorithm set DSA_WITH_SHA256 error
707 SignerConfig signerConfig;
708 signerConfig.SetCompatibleVersion(9);
709
710 std::map<std::string, std::string> params;
711 SetParamsMap(params);
712 signerConfig.FillParameters(params);
713
714 ContentDigestAlgorithm contentDigestAlgorithm("SHA-256", 32);
715 std::pair<std::string, void*> signatureAlgAndParams("SHA256withECDSA", nullptr);
716 SignatureAlgorithmHelper signatureAlgorithm(SignatureAlgorithmId::DSA_WITH_SHA256, "ECDSA_WITH_SHA256",
717 contentDigestAlgorithm, signatureAlgAndParams);
718 std::vector<SignatureAlgorithmHelper> signatureAlgorithms;
719 signatureAlgorithms.push_back(signatureAlgorithm);
720 signerConfig.SetSignatureAlgorithms(signatureAlgorithms);
721
722 std::shared_ptr<Options> options = std::make_shared<Options>();
723 SetOptions(options.get());
724 signerConfig.SetOptions(options.get());
725 signerConfig.GetSigner();
726
727 CodeSigning codeSigning(&signerConfig);
728 std::string inputFile = params.at("inFile");
729 int64_t offset = 29184024;
730 std::vector<int8_t> codesignData;
731 std::string inForm = "elf";
732 bool ret = codeSigning.GetElfCodeSignBlock(inputFile, offset, inForm, params.at("profileContent"), codesignData);
733 EXPECT_EQ(ret, false);
734 }
735 } // namespace SignatureTools
736 } // namespace OHOS