1 /*
2  * Copyright (c) 2023-2023 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 "param_reader.h"
17 
18 #include <memory>
19 #include <iostream>
20 #include <fstream>
21 
22 #include "hiview_logger.h"
23 #include "log_sign_tools.h"
24 #include "param_const_common.h"
25 #include "string_util.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 DEFINE_LOG_TAG("Hiview-ParamUpdate");
30 
31 namespace {
32     const int MIN_SIZE = 2;
33 }
34 
VerifyCertFile()35 bool ParamReader::VerifyCertFile()
36 {
37     std::string certFile = CFG_PATH + "CERT.ENC";
38     std::string verifyFile = CFG_PATH + "CERT.SF";
39     if (!LogSignTools::VerifyFileSign(PUBKEY_PATH, certFile, verifyFile)) {
40         HIVIEW_LOGE("verify failed %{public}s,%{public}s, %{public}s", PUBKEY_PATH.c_str(),
41             certFile.c_str(), verifyFile.c_str());
42         return false;
43     }
44 
45     std::string manifestFile = CFG_PATH + "MANIFEST.MF";
46     std::ifstream file(verifyFile);
47     if (!file.good()) {
48         HIVIEW_LOGE("Verify is not good");
49         return false;
50     }
51     std::string line;
52     std::string sha256Digest;
53     std::getline(file, line);
54     file.close();
55     std::vector<std::string> strs;
56     StringUtil::SplitStr(line, ":", strs);
57     if (strs.size() < MIN_SIZE) {
58         HIVIEW_LOGE("get sha256Digest failed.");
59         return false;
60     }
61     sha256Digest = strs[1];
62     StringUtil::TrimStr(sha256Digest);
63 
64     std::string manifestDigest = LogSignTools::CalcFileSha256Digest(manifestFile);
65     if (sha256Digest == manifestDigest) {
66         HIVIEW_LOGI("Verify manifestFile success");
67         return true;
68     }
69     HIVIEW_LOGE("verify cert file failed");
70     return false;
71 };
72 
VerifyParamFile(const std::string & filePathStr)73 bool ParamReader::VerifyParamFile(const std::string &filePathStr)
74 {
75     std::string manifestFile = CFG_PATH + "MANIFEST.MF";
76     std::ifstream file(manifestFile);
77     if (!file.good()) {
78         HIVIEW_LOGE("manifestFile is not good");
79         return false;
80     }
81     std::string absFilePath = CFG_PATH + filePathStr;
82     std::ifstream paramFile(absFilePath);
83     if (!paramFile.good()) {
84         HIVIEW_LOGE("paramFile is not good");
85         return false;
86     }
87 
88     std::string sha256Digest;
89     std::string line;
90     while (std::getline(file, line)) {
91         std::string nextline;
92         if (line.find("Name: " + filePathStr) != std::string::npos) {
93             std::getline(file, nextline);
94             std::vector<std::string> strs;
95             StringUtil::SplitStr(nextline, ":", strs);
96             if (strs.size() < MIN_SIZE) {
97                 HIVIEW_LOGE("get sha256Digest failed.");
98                 return false;
99             }
100             sha256Digest = strs[1];
101             StringUtil::TrimStr(sha256Digest);
102             break;
103         }
104     }
105     if (sha256Digest.empty()) {
106         HIVIEW_LOGE("VerifyParamFile failed, sha256Digest is empty");
107         return false;
108     }
109 
110     std::string sha256Str = LogSignTools::CalcFileSha256Digest(absFilePath);
111     if (sha256Digest == sha256Str) {
112         HIVIEW_LOGI("VerifyParamFile success");
113         return true;
114     }
115     HIVIEW_LOGE("VerifyParamFile failed");
116     return false;
117 };
118 } // namespace HiviewDFX
119 } // namespace OHOS
120