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 "sign_tool_service_impl.h"
17 #include <ctime>
18
19 #include "pkcs7_data.h"
20 #include "profile_sign_tool.h"
21 #include "nlohmann/json.hpp"
22 #include "profile_info.h"
23 #include "profile_verify.h"
24 #include "signature_tools_errno.h"
25 #include "local_sign_provider.h"
26 #include "signature_tools_log.h"
27 #include "param_constants.h"
28 #include "verify_hap.h"
29 #include "constant.h"
30 #include "remote_sign_provider.h"
31 #include "verify_elf.h"
32 #include "verify_bin.h"
33
34 namespace OHOS {
35 namespace SignatureTools {
36
GenerateCA(Options * options)37 bool SignToolServiceImpl::GenerateCA(Options* options)
38 {
39 bool flag = true;
40 std::unique_ptr<LocalizationAdapter> adapter = std::make_unique<LocalizationAdapter>(options);
41 bool isEmpty = FileUtils::IsEmpty(options->GetString(Options::ISSUER_KEY_ALIAS));
42 EVP_PKEY* subKey = adapter->GetAliasKey(true);
43 if (!subKey) {
44 SIGNATURE_TOOLS_LOGE("failed to get subKey!");
45 return false;
46 }
47 if (isEmpty) {
48 if (HandleIssuerKeyAliasEmpty(options) == RET_FAILED) {
49 EVP_PKEY_free(subKey);
50 return false;
51 }
52 flag = GenerateRootCertToFile(options, subKey);
53 EVP_PKEY_free(subKey);
54 } else {
55 EVP_PKEY* rootKey = nullptr;
56 if (HandleIsserKeyAliasNotEmpty(options) == RET_FAILED) {
57 EVP_PKEY_free(subKey);
58 return false;
59 }
60 adapter->SetIssuerKeyStoreFile(true);
61 rootKey = adapter->GetAliasKey(false);
62 flag = GenerateSubCertToFile(options, rootKey);
63 EVP_PKEY_free(rootKey);
64 EVP_PKEY_free(subKey);
65 }
66 adapter->ResetPwd();
67 return flag;
68 }
69
GenerateRootCertToFile(Options * options,EVP_PKEY * rootKey)70 bool SignToolServiceImpl::GenerateRootCertToFile(Options* options, EVP_PKEY* rootKey)
71 {
72 if (rootKey == nullptr) {
73 SIGNATURE_TOOLS_LOGE("generate root cert failed because rootKey is nullptr!");
74 return false;
75 }
76 std::string signAlg = options->GetString(Options::SIGN_ALG);
77 std::string subject = options->GetString(Options::SUBJECT);
78 X509* certPtr = nullptr;
79 X509_REQ* csr = nullptr;
80 bool result = false;
81 do {
82 csr = CertTools::GenerateCsr(rootKey, signAlg, subject);
83 if (csr == nullptr) {
84 SIGNATURE_TOOLS_LOGE("generate root cert failed because csr is nullptr!");
85 break;
86 }
87 certPtr = CertTools::GenerateRootCertificate(rootKey, csr, options);
88 if (certPtr == nullptr) {
89 SIGNATURE_TOOLS_LOGE("generate root cert failed because certPtr is nullptr!");
90 break;
91 }
92 if (!X509CertVerify(certPtr, rootKey)) {
93 SIGNATURE_TOOLS_LOGE("generate root cert failed because verify failed!");
94 break;
95 }
96 if (!OutputModeOfCert(certPtr, options)) {
97 SIGNATURE_TOOLS_LOGE("generate root cert failed because output failed!");
98 break;
99 }
100 result = true;
101 } while (0);
102
103 X509_free(certPtr);
104 X509_REQ_free(csr);
105 return result;
106 }
107
GenerateSubCertToFile(Options * options,EVP_PKEY * rootKey)108 bool SignToolServiceImpl::GenerateSubCertToFile(Options* options, EVP_PKEY* rootKey)
109 {
110 if (rootKey == nullptr) {
111 SIGNATURE_TOOLS_LOGE("generate sub cert failed because rootKey is nullptr!");
112 return false;
113 }
114 std::string signAlg = options->GetString(Options::SIGN_ALG);
115 std::string issuer = options->GetString(Options::ISSUER);
116 X509* cert = nullptr;
117 X509_REQ* csr = nullptr;
118 bool result = false;
119 do {
120 csr = CertTools::GenerateCsr(rootKey, signAlg, issuer);
121 if (csr == nullptr) {
122 SIGNATURE_TOOLS_LOGE("generate sub cert failed because csr is nullptr!");
123 break;
124 }
125 cert = CertTools::GenerateSubCert(rootKey, csr, options);
126 if (cert == nullptr) {
127 SIGNATURE_TOOLS_LOGE("generate sub cert failed because cert is nullptr!");
128 break;
129 }
130 if (!X509CertVerify(cert, rootKey)) {
131 SIGNATURE_TOOLS_LOGE("generate sub cert failed because verify failed!");
132 break;
133 }
134 if (!OutputModeOfCert(cert, options)) {
135 SIGNATURE_TOOLS_LOGE("generate sub cert failed because output failed!");
136 break;
137 }
138 result = true;
139 } while (0);
140
141 X509_free(cert);
142 X509_REQ_free(csr);
143 return result;
144 }
145
HandleIssuerKeyAliasEmpty(Options * options)146 int SignToolServiceImpl::HandleIssuerKeyAliasEmpty(Options* options)
147 {
148 std::string iksFile = options->GetString(Options::ISSUER_KEY_STORE_FILE);
149 if (!FileUtils::IsEmpty(iksFile) && !options->Equals(Options::KEY_STORE_FILE, Options::ISSUER_KEY_STORE_FILE)) {
150 PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR,
151 "Parameter '" + iksFile + "' and parameter '" +
152 options->GetString(Options::KEY_STORE_FILE) + "' are inconsistent");
153 return RET_FAILED;
154 }
155 char* keyStoreRights = options->GetChars(Options::KEY_STORE_RIGHTS);
156 char* issuerKeyStoreRights = options->GetChars(Options::ISSUER_KEY_STORE_RIGHTS);
157 if (!FileUtils::IsEmpty(iksFile)) {
158 if ((keyStoreRights == nullptr && issuerKeyStoreRights != nullptr) ||
159 (keyStoreRights != nullptr && issuerKeyStoreRights == nullptr)) {
160 goto err;
161 } else if (keyStoreRights == nullptr && issuerKeyStoreRights == nullptr) {
162 return RET_OK;
163 } else {
164 if (std::strcmp(keyStoreRights, issuerKeyStoreRights) != 0) {
165 goto err;
166 }
167 }
168 }
169 return RET_OK;
170 err:
171 PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR,
172 "Parameter 'keystorePwd' and parameter 'issuerKeystorePwd' are inconsistent");
173 return RET_FAILED;
174 }
175
HandleIsserKeyAliasNotEmpty(Options * options)176 int SignToolServiceImpl::HandleIsserKeyAliasNotEmpty(Options* options)
177 {
178 std::string iksFile = options->GetString(Options::ISSUER_KEY_STORE_FILE);
179 if (!FileUtils::IsEmpty(iksFile)) {
180 if (!FileUtils::ValidFileType(iksFile, {"p12", "jks"})) {
181 SIGNATURE_TOOLS_LOGE("issuer keystore file type is inconsistent!");
182 return RET_FAILED;
183 }
184 }
185 return RET_OK;
186 }
187
OutputModeOfCert(X509 * cert,Options * options)188 bool SignToolServiceImpl::OutputModeOfCert(X509* cert, Options* options)
189 {
190 std::string outFile = options->GetString(Options::OUT_FILE);
191 if (!outFile.empty()) {
192 if (!CertTools::SaveCertTofile(outFile, cert)) {
193 PrintErrorNumberMsg("WRITE_FILE_ERROR", IO_ERROR, "failed to save cert to file");
194 return false;
195 }
196 } else {
197 if (!PrintX509CertFromMemory(cert)) {
198 return false;
199 }
200 }
201 return true;
202 }
203
GenerateCert(Options * options)204 bool SignToolServiceImpl::GenerateCert(Options* options)
205 {
206 std::unique_ptr<LocalizationAdapter> adapter = std::make_unique<LocalizationAdapter>(options);
207 std::string signAlg = options->GetString(Options::SIGN_ALG);
208 std::string subject = options->GetString(Options::SUBJECT);
209 EVP_PKEY* subjectkeyPair = nullptr;
210 EVP_PKEY* rootKeyPair = nullptr;
211 X509_REQ* csr = nullptr;
212 X509* cert = nullptr;
213 bool result = false;
214 subjectkeyPair = adapter->GetAliasKey(false);
215 if (!subjectkeyPair) {
216 goto err;
217 }
218 adapter->SetIssuerKeyStoreFile(true);
219 rootKeyPair = adapter->GetIssuerKeyByAlias();
220 if (!rootKeyPair) {
221 goto err;
222 }
223 adapter->ResetPwd();
224 csr = CertTools::GenerateCsr(subjectkeyPair, signAlg, subject);
225 if (!csr) {
226 goto err;
227 }
228 cert = CertTools::GenerateCert(rootKeyPair, csr, options);
229 if (!cert) {
230 goto err;
231 }
232 if (!X509CertVerify(cert, rootKeyPair)) {
233 goto err;
234 }
235 if (!OutputModeOfCert(cert, options)) {
236 goto err;
237 }
238 result = true;
239 err:
240 if (result == false)
241 SIGNATURE_TOOLS_LOGE("generate cert failed!");
242 adapter->ResetPwd();
243 X509_free(cert);
244 X509_REQ_free(csr);
245 EVP_PKEY_free(rootKeyPair);
246 EVP_PKEY_free(subjectkeyPair);
247 return result;
248 }
249
GenerateKeyStore(Options * options)250 bool SignToolServiceImpl::GenerateKeyStore(Options* options)
251 {
252 std::unique_ptr<LocalizationAdapter> adaptePtr = std::make_unique<LocalizationAdapter>(options);
253 std::string keyAlias = adaptePtr->options->GetString(Options::KEY_ALIAS);
254
255 int status = adaptePtr->IsAliasExist(keyAlias);
256 if (status == RET_OK) {
257 adaptePtr->ResetPwd();
258 PrintErrorNumberMsg("KEY_ALIAS_ERROR", KEY_ALIAS_ERROR, "'" + keyAlias
259 + "' key alias already exists and cannot be generated repeatedly");
260 return false;
261 }
262
263 if (!adaptePtr->keyStoreHelper->GetPassWordStatus()) {
264 adaptePtr->ResetPwd();
265 return false;
266 }
267
268 EVP_PKEY* keyPair = nullptr;
269 keyPair = adaptePtr->GetAliasKey(true);
270 adaptePtr->ResetPwd();
271 if (keyPair == nullptr) {
272 SIGNATURE_TOOLS_LOGE("failed to get keypair!");
273 return false;
274 }
275 EVP_PKEY_free(keyPair);
276 return true;
277 }
278
GenerateCsr(Options * options)279 bool SignToolServiceImpl::GenerateCsr(Options* options)
280 {
281 std::unique_ptr<LocalizationAdapter> adapter = std::make_unique<LocalizationAdapter>(options);
282 EVP_PKEY* keyPair = adapter->GetAliasKey(false);
283 if (!keyPair) {
284 SIGNATURE_TOOLS_LOGE("failed to get keypair!");
285 adapter->ResetPwd();
286 return false;
287 }
288 adapter->ResetPwd();
289 X509_REQ* csr = nullptr;
290 std::string signAlg = options->GetString(Options::SIGN_ALG);
291 std::string subject = options->GetString(Options::SUBJECT);
292 if (signAlg.empty()) {
293 PrintErrorNumberMsg("INVALIDPARAM_ERROR",
294 INVALIDPARAM_ERROR,
295 "Please check if signalg has been specified which is required.");
296 EVP_PKEY_free(keyPair);
297 return false;
298 }
299 if (subject.empty()) {
300 PrintErrorNumberMsg("INVALIDPARAM_ERROR",
301 INVALIDPARAM_ERROR,
302 "Please check if subject has been specified which is required.");
303 EVP_PKEY_free(keyPair);
304 return false;
305 }
306 csr = CertTools::GenerateCsr(keyPair, signAlg, subject);
307 if (!csr) {
308 SIGNATURE_TOOLS_LOGE("failed to generate csr request!");
309 EVP_PKEY_free(keyPair);
310 return false;
311 }
312 std::string csrStr = CertTools::CsrToString(csr);
313 EVP_PKEY_free(keyPair);
314 X509_REQ_free(csr);
315 if (csrStr.size() == 0) {
316 PrintErrorNumberMsg("PARSE_ERROR", PARSE_ERROR,
317 "failed to transform csr to string!");
318 return false;
319 }
320 std::string outFile = options->GetString(Options::OUT_FILE);
321 return OutputString(csrStr, outFile);
322 }
323
OutputString(std::string content,std::string file)324 bool SignToolServiceImpl::OutputString(std::string content, std::string file)
325 {
326 if (file.size() == 0) {
327 puts(content.c_str());
328 return true;
329 }
330 std::ofstream outFile(file.c_str());
331 if (!outFile.is_open()) {
332 PrintErrorNumberMsg("IO_ERROR", IO_ERROR,
333 "failed to open the outFile " + file + " !");
334 return false;
335 }
336 outFile << content;
337 outFile.close();
338 return true;
339 }
340
X509CertVerify(X509 * cert,EVP_PKEY * privateKey)341 bool SignToolServiceImpl::X509CertVerify(X509* cert, EVP_PKEY* privateKey)
342 {
343 if (!X509_verify(cert, privateKey)) {
344 PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR, "Verify certificate failed!");
345 return false;
346 }
347 return true;
348 }
349
GetCsr(EVP_PKEY * keyPair,std::string signAlg,std::string subject)350 X509_REQ* SignToolServiceImpl::GetCsr(EVP_PKEY* keyPair, std::string signAlg, std::string subject)
351 {
352 if (signAlg.empty() || subject.empty()) {
353 SIGNATURE_TOOLS_LOGE("failed to get signalg or subject!");
354 return nullptr;
355 }
356 X509_REQ* csr = CertTools::GenerateCsr(keyPair, signAlg, subject);
357 if (!csr) {
358 SIGNATURE_TOOLS_LOGE("failed to generate csr!");
359 return nullptr;
360 }
361 return csr;
362 }
363
GenerateAppCert(Options * options)364 bool SignToolServiceImpl::GenerateAppCert(Options* options)
365 {
366 std::unique_ptr<LocalizationAdapter> adapter = std::make_unique<LocalizationAdapter>(options);
367 EVP_PKEY* keyPairPtr = nullptr;
368 EVP_PKEY* issuerKeyPairPtr = nullptr;
369 X509_REQ* csrPtr = nullptr;
370 X509* x509CertificatePtr = nullptr;
371 std::string signAlg = adapter->options->GetString(Options::SIGN_ALG);
372 std::string subject = adapter->options->GetString(Options::SUBJECT);
373
374 if (!(keyPairPtr = adapter->GetAliasKey(false))) { // get keypair
375 goto err;
376 }
377 adapter->SetIssuerKeyStoreFile(true);
378 if (!(issuerKeyPairPtr = adapter->GetIssuerKeyByAlias())) { // get issuer keypair
379 goto err;
380 }
381 adapter->ResetPwd(); // clean pwd for safety
382 csrPtr = GetCsr(keyPairPtr, signAlg, subject);
383 if (!csrPtr) { // get CSR request
384 goto err;
385 }
386 x509CertificatePtr = CertTools::GenerateEndCert(csrPtr, issuerKeyPairPtr, *adapter,
387 APP_SIGNING_CAPABILITY,
388 sizeof(APP_SIGNING_CAPABILITY)); // get app x509 cert
389 if (!x509CertificatePtr) {
390 PrintErrorNumberMsg("CERTIFICATE_ERROR", CERTIFICATE_ERROR, "generate app cert failed");
391 goto err;
392 }
393 if (!X509CertVerify(x509CertificatePtr, issuerKeyPairPtr)) {
394 goto err;
395 }
396 if (!GetAndOutPutCert(*adapter, x509CertificatePtr)) {
397 goto err;
398 }
399 // realse heap memory
400 adapter->AppAndProfileAssetsRealse({issuerKeyPairPtr, keyPairPtr}, {csrPtr}, {x509CertificatePtr});
401 return true;
402
403 err:
404 adapter->AppAndProfileAssetsRealse({issuerKeyPairPtr, keyPairPtr}, {csrPtr}, {x509CertificatePtr});
405 adapter->ResetPwd();
406 return false;
407 }
408
GenerateProfileCert(Options * options)409 bool SignToolServiceImpl::GenerateProfileCert(Options* options)
410 {
411 std::unique_ptr<LocalizationAdapter> adapter = std::make_unique<LocalizationAdapter>(options);
412 EVP_PKEY* keyPair = nullptr;
413 EVP_PKEY* issuerKeyPair = nullptr;
414 X509_REQ* csr = nullptr;
415 X509* x509Certificate = nullptr;
416 std::string signAlg = adapter->options->GetString(Options::SIGN_ALG);
417 std::string subject = adapter->options->GetString(Options::SUBJECT);
418
419 if (!(keyPair = adapter->GetAliasKey(false))) { // get keypair
420 goto err;
421 }
422 adapter->SetIssuerKeyStoreFile(true);
423 if (!(issuerKeyPair = adapter->GetIssuerKeyByAlias())) { // get issuer keypair
424 goto err;
425 }
426 adapter->ResetPwd(); // clean pwd for safety
427 csr = GetCsr(keyPair, signAlg, subject);
428 if (!csr) { // get CSR request
429 goto err;
430 }
431 x509Certificate = CertTools::GenerateEndCert(csr, issuerKeyPair, *adapter,
432 PROFILE_SIGNING_CAPABILITY,
433 sizeof(PROFILE_SIGNING_CAPABILITY)); // get profile x509 cert
434 if (!x509Certificate) {
435 PrintErrorNumberMsg("CERTIFICATE_ERROR", CERTIFICATE_ERROR, "generate profile cert failed");
436 goto err;
437 }
438 if (!X509CertVerify(x509Certificate, issuerKeyPair)) {
439 goto err;
440 }
441 if (!GetAndOutPutCert(*adapter, x509Certificate)) { // output cert to file
442 goto err;
443 }
444
445 adapter->AppAndProfileAssetsRealse({issuerKeyPair, keyPair}, {csr}, {x509Certificate}); // realse heap memory
446 return true;
447
448 err:
449 adapter->AppAndProfileAssetsRealse({issuerKeyPair, keyPair}, {csr}, {x509Certificate});
450 adapter->ResetPwd();
451 return false;
452 }
453
GetAndOutPutCert(LocalizationAdapter & adapter,X509 * cert)454 bool SignToolServiceImpl::GetAndOutPutCert(LocalizationAdapter& adapter, X509* cert)
455 {
456 std::string outFile = adapter.options->GetString(Options::OUT_FILE);
457 bool successflag = false;
458 X509* subCaCert = nullptr;
459 X509* rootCaCert = nullptr;
460 if (adapter.IsOutFormChain()) {
461 std::vector<X509*> certificates;
462 certificates.emplace_back(cert); // add entity cert
463 successflag = (!(subCaCert = adapter.GetSubCaCertFile()) ||
464 !(rootCaCert = adapter.GetCaCertFile()));
465 if (successflag) {
466 return false;
467 }
468 certificates.emplace_back(subCaCert); // add sub ca cert
469 certificates.emplace_back(rootCaCert); // add root ca cert
470
471 if (outFile.empty()) {
472 successflag = PrintX509CertChainFromMemory(certificates); // print certchain to cmd
473 adapter.AppAndProfileAssetsRealse({}, {}, {certificates[1], certificates[2]});
474 return successflag;
475 }
476 successflag = OutPutCertChain(certificates, adapter.GetOutFile()); // out put certchain to file
477 adapter.AppAndProfileAssetsRealse({}, {}, {certificates[1], certificates[2]});
478 return successflag;
479 }
480
481 if (outFile.empty()) {
482 successflag = PrintX509CertFromMemory(cert); // print cert to cmd
483 return successflag;
484 }
485 successflag = OutPutCert(cert, adapter.GetOutFile()); // out put cert to file
486 return successflag;
487 }
488
SignProfile(Options * options)489 bool SignToolServiceImpl::SignProfile(Options* options)
490 {
491 LocalizationAdapter adapter(options);
492 const std::string inFile = adapter.GetInFile();
493 const std::string outFile = adapter.GetOutFile();
494 std::string provisionContent;
495 std::string p7b;
496 if (SignToolServiceImpl::GetProvisionContent(inFile, provisionContent) < 0) {
497 SIGNATURE_TOOLS_LOGE("getProvisionContent failed");
498 return false;
499 }
500 if (ProfileSignTool::GenerateP7b(adapter, provisionContent, p7b) < 0) {
501 SIGNATURE_TOOLS_LOGE("generate P7b data failed");
502 return false;
503 }
504 if (FileUtils::Write(p7b, outFile) < 0) {
505 SIGNATURE_TOOLS_LOGE("write p7b data failed");
506 return false;
507 }
508 return true;
509 }
510
SignHap(Options * options)511 bool SignToolServiceImpl::SignHap(Options* options)
512 {
513 std::string mode = options->GetString(Options::MODE);
514 std::shared_ptr<SignProvider> signProvider;
515 if (LOCAL_SIGN == mode) {
516 signProvider = std::make_shared<LocalSignProvider>();
517 } else if (REMOTE_SIGN == mode) {
518 signProvider = std::make_shared<RemoteSignProvider>();
519 } else {
520 SIGNATURE_TOOLS_LOGE("Resign mode. But not implemented yet");
521 return false;
522 }
523 std::string inForm = options->GetString(Options::INFORM);
524 if (ZIP == inForm) {
525 return signProvider->Sign(options);
526 } else if (ELF == inForm) {
527 return signProvider->SignElf(options);
528 } else {
529 return signProvider->SignBin(options);
530 }
531 return true;
532 }
533
VerifyProfile(Options * options)534 bool SignToolServiceImpl::VerifyProfile(Options* options)
535 {
536 LocalizationAdapter adapter(options);
537 std::string p7b;
538 if (FileUtils::ReadFile(adapter.GetInFile(), p7b) < 0) {
539 SIGNATURE_TOOLS_LOGE("read p7b data error");
540 return false;
541 }
542 PKCS7Data p7Data;
543 if (p7Data.Parse(p7b) < 0) {
544 SIGNATURE_TOOLS_LOGE("verify profile failed");
545 return false;
546 }
547 if (p7Data.Verify() < 0) {
548 SIGNATURE_TOOLS_LOGE("verify profile failed");
549 return false;
550 }
551 const std::string outFile = adapter.GetOutFile();
552 std::string originalData;
553 if (p7Data.GetContent(originalData) < 0) {
554 SIGNATURE_TOOLS_LOGE("get content failed");
555 return false;
556 }
557 if (outFile.empty()) {
558 PrintMsg(originalData);
559 } else {
560 std::ofstream out(outFile, std::ios::binary);
561 out.write(originalData.data(), originalData.size());
562 }
563 return true;
564 }
565
OutPutCertChain(std::vector<X509 * > & certs,const std::string & outPutPath)566 bool SignToolServiceImpl::OutPutCertChain(std::vector<X509*>& certs, const std::string& outPutPath)
567 {
568 SIGNATURE_TOOLS_LOGD("outPutPath = %s", outPutPath.c_str());
569 BIO* bio = nullptr;
570 if (!(bio = BIO_new_file(outPutPath.c_str(), "wb"))) {
571 SIGNATURE_TOOLS_LOGE("failed to open file %s", outPutPath.c_str());
572 goto err;
573 }
574 for (auto cert : certs) {
575 if (PEM_write_bio_X509(bio, cert) < 0) {
576 SIGNATURE_TOOLS_LOGE("failed to write certChain to file!");
577 goto err;
578 }
579 }
580 BIO_free(bio);
581 return true;
582 err:
583 VerifyHapOpensslUtils::GetOpensslErrorMessage();
584 BIO_free(bio);
585 return false;
586 }
587
OutPutCert(X509 * cert,const std::string & outPutPath)588 bool SignToolServiceImpl::OutPutCert(X509* cert, const std::string& outPutPath)
589 {
590 BIO* bio = BIO_new_file(outPutPath.c_str(), "wb");
591 if (!bio) {
592 SIGNATURE_TOOLS_LOGE("failed to open file: %s", outPutPath.c_str());
593 goto err;
594 }
595 if (!PEM_write_bio_X509(bio, cert)) {
596 SIGNATURE_TOOLS_LOGE("failed to write cert to file!");
597 goto err;
598 }
599 BIO_free(bio);
600 return true;
601 err:
602 VerifyHapOpensslUtils::GetOpensslErrorMessage();
603 BIO_free(bio);
604 return false;
605 }
606
GetProvisionContent(const std::string & input,std::string & ret)607 int SignToolServiceImpl::GetProvisionContent(const std::string& input, std::string& ret)
608 {
609 std::string bytes;
610 if (FileUtils::ReadFile(input, bytes) < 0) {
611 SIGNATURE_TOOLS_LOGE("provision read faild!");
612 return IO_ERROR;
613 }
614 nlohmann::json obj = nlohmann::json::parse(bytes);
615 if (obj.is_discarded() || (!obj.is_structured())) {
616 PrintErrorNumberMsg("PARSE ERROR", PARSE_ERROR, "Parsing appProvision failed!");
617 return PARSE_ERROR;
618 }
619 ret = obj.dump();
620 ProfileInfo provision;
621 AppProvisionVerifyResult result = ParseProvision(ret, provision);
622 if (result != PROVISION_OK) {
623 SIGNATURE_TOOLS_LOGE("invalid provision");
624 return INVALIDPARAM_ERROR;
625 }
626 return 0;
627 }
628
PrintX509CertFromMemory(X509 * cert)629 bool SignToolServiceImpl::PrintX509CertFromMemory(X509* cert)
630 {
631 BIO* bio = BIO_new(BIO_s_mem());
632 if (!bio) {
633 VerifyHapOpensslUtils::GetOpensslErrorMessage();
634 return false;
635 }
636 if (PEM_write_bio_X509(bio, cert) == 1) {
637 BUF_MEM* bptr;
638 BIO_get_mem_ptr(bio, &bptr);
639 PrintMsg(std::string(bptr->data, bptr->length));
640 } else {
641 VerifyHapOpensslUtils::GetOpensslErrorMessage();
642 PrintErrorNumberMsg("IO_ERROR", IO_ERROR, "print x509 cert falied");
643 BIO_free(bio);
644 return false;
645 }
646 BIO_free(bio);
647 return true;
648 }
649
PrintX509CertChainFromMemory(std::vector<X509 * > certs)650 bool SignToolServiceImpl::PrintX509CertChainFromMemory(std::vector<X509*> certs)
651 {
652 for (auto cert : certs) {
653 BIO* bio = BIO_new(BIO_s_mem());
654 BUF_MEM* bptr = nullptr;
655 if (!bio) {
656 VerifyHapOpensslUtils::GetOpensslErrorMessage();
657 return false;
658 }
659 if (PEM_write_bio_X509(bio, cert) == 1) {
660 BIO_get_mem_ptr(bio, &bptr);
661 PrintMsg(std::string(bptr->data, bptr->length));
662 } else {
663 VerifyHapOpensslUtils::GetOpensslErrorMessage();
664 PrintErrorNumberMsg("IO_ERROR", IO_ERROR, "print x509 cert chain falied");
665 BIO_free(bio);
666 return false;
667 }
668 BIO_free(bio);
669 }
670 return true;
671 }
672
VerifyHapSigner(Options * option)673 bool SignToolServiceImpl::VerifyHapSigner(Options* option)
674 {
675 std::string inForm = option->GetString(ParamConstants::PARAM_IN_FORM);
676 if (inForm == ZIP) {
677 VerifyHap hapVerify;
678 int32_t ret = hapVerify.Verify(option->GetString(Options::IN_FILE), option);
679 if (ret == RET_OK) {
680 PrintMsg("hap verify successed!");
681 return true;
682 }
683 PrintMsg("hap verify failed !");
684 return false;
685 } else if (inForm == ELF) {
686 VerifyElf verifyElf;
687 if (!verifyElf.Verify(option)) {
688 PrintMsg("elf verify failed!");
689 return false;
690 }
691 PrintMsg("elf verify successed!");
692 return true;
693 } else if (inForm == BIN) {
694 VerifyBin verifyBin;
695 if (!verifyBin.Verify(option)) {
696 PrintMsg("bin verify failed!");
697 return false;
698 }
699 PrintMsg("bin verify successed!");
700 return true;
701 } else {
702 PrintErrorNumberMsg("NOT_SUPPORT_ERROR", NOT_SUPPORT_ERROR, "Unsupported inForm!");
703 return false;
704 }
705 }
706
707 } // namespace SignatureTools
708 } // namespace OHOS
709