• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <hidl-hash/Hash.h>
18 
19 #include <algorithm>
20 #include <fstream>
21 #include <iomanip>
22 #include <map>
23 #include <regex>
24 #include <sstream>
25 
26 #include <android-base/hex.h>
27 #include <android-base/logging.h>
28 #include <openssl/sha.h>
29 
30 namespace android {
31 
32 const std::vector<uint8_t> Hash::kEmptyHash = std::vector<uint8_t>(SHA256_DIGEST_LENGTH, 0);
33 
getMutableHash(const std::string & path)34 Hash& Hash::getMutableHash(const std::string& path) {
35     static std::map<std::string, Hash> hashes;
36 
37     auto it = hashes.find(path);
38 
39     if (hashes.find(path) == hashes.end()) {
40         it = hashes.insert(it, {path, Hash(path)});
41     }
42 
43     return it->second;
44 }
45 
getHash(const std::string & path)46 const Hash& Hash::getHash(const std::string& path) {
47     return getMutableHash(path);
48 }
49 
clearHash(const std::string & path)50 void Hash::clearHash(const std::string& path) {
51     getMutableHash(path).mHash = kEmptyHash;
52 }
53 
sha256File(const std::string & path)54 static std::vector<uint8_t> sha256File(const std::string& path) {
55     std::ifstream stream(path);
56     std::stringstream fileStream;
57     fileStream << stream.rdbuf();
58     std::string fileContent = fileStream.str();
59 
60     std::vector<uint8_t> ret = std::vector<uint8_t>(SHA256_DIGEST_LENGTH);
61 
62     SHA256(reinterpret_cast<const uint8_t*>(fileContent.c_str()), fileContent.size(), ret.data());
63 
64     return ret;
65 }
66 
Hash(const std::string & path)67 Hash::Hash(const std::string& path) : mPath(path), mHash(sha256File(path)) {}
68 
hexString() const69 std::string Hash::hexString() const {
70     return android::base::HexString(mHash.data(), mHash.size());
71 }
72 
raw() const73 const std::vector<uint8_t>& Hash::raw() const {
74     return mHash;
75 }
76 
getPath() const77 const std::string& Hash::getPath() const {
78     return mPath;
79 }
80 
81 #define HASH "([0-9a-f]+)"
82 #define FQNAME "([^\\s]+)"
83 #define SPACES " +"
84 #define MAYBE_SPACES " *"
85 #define OPTIONAL_COMMENT "(?:#.*)?"
86 static const std::regex kHashLine("(?:" MAYBE_SPACES HASH SPACES FQNAME MAYBE_SPACES
87                                   ")?" OPTIONAL_COMMENT);
88 
89 struct HashFile {
parseandroid::HashFile90     static const HashFile* parse(const std::string& path, std::string* err) {
91         static std::map<std::string, HashFile*> hashfiles;
92         auto it = hashfiles.find(path);
93 
94         if (it == hashfiles.end()) {
95             it = hashfiles.insert(it, {path, readHashFile(path, err)});
96         }
97 
98         return it->second;
99     }
100 
lookupandroid::HashFile101     std::vector<std::string> lookup(const std::string& fqName) const {
102         auto it = hashes.find(fqName);
103 
104         if (it == hashes.end()) {
105             return {};
106         }
107 
108         return it->second;
109     }
110 
111    private:
readHashFileandroid::HashFile112     static HashFile* readHashFile(const std::string& path, std::string* err) {
113         std::ifstream stream(path);
114         if (!stream) {
115             return nullptr;
116         }
117 
118         HashFile* file = new HashFile();
119         file->path = path;
120 
121         std::string line;
122         while (std::getline(stream, line)) {
123             std::smatch match;
124             bool valid = std::regex_match(line, match, kHashLine);
125 
126             if (!valid) {
127                 *err = "Error reading line from " + path + ": " + line;
128                 delete file;
129                 return nullptr;
130             }
131 
132             CHECK_EQ(match.size(), 3u);
133 
134             std::string hash = match.str(1);
135             std::string fqName = match.str(2);
136 
137             if (hash.size() == 0 && fqName.size() == 0) {
138                 continue;
139             }
140 
141             if (hash.size() == 0 || fqName.size() == 0) {
142                 *err = "Hash or fqName empty on " + path + ": " + line;
143                 delete file;
144                 return nullptr;
145             }
146 
147             file->hashes[fqName].push_back(hash);
148         }
149         return file;
150     }
151 
152     std::string path;
153     std::map<std::string, std::vector<std::string>> hashes;
154 };
155 
lookupHash(const std::string & path,const std::string & interfaceName,std::string * err,bool * fileExists)156 std::vector<std::string> Hash::lookupHash(const std::string& path, const std::string& interfaceName,
157                                           std::string* err, bool* fileExists) {
158     *err = "";
159     const HashFile* file = HashFile::parse(path, err);
160 
161     if (file == nullptr || err->size() > 0) {
162         if (fileExists != nullptr) *fileExists = false;
163         return {};
164     }
165 
166     if (fileExists != nullptr) *fileExists = true;
167 
168     return file->lookup(interfaceName);
169 }
170 
171 }  // namespace android
172