blob: 4fb2d9c35f70bfd524663b372a2aa8145ba6f8da [file] [log] [blame]
Gunnar Millsf6ed5892018-09-07 17:08:02 -05001#include "config.h"
Jayanth Othayoth70804dc2018-03-20 06:31:59 -05002
3#include "image_verify.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05004
Jayanth Othayoth70804dc2018-03-20 06:31:59 -05005#include "version.hpp"
6
Gunnar Millsf6ed5892018-09-07 17:08:02 -05007#include <fcntl.h>
8#include <openssl/err.h>
9#include <sys/stat.h>
10
11#include <fstream>
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050012#include <phosphor-logging/elog-errors.hpp>
Gunnar Millsf6ed5892018-09-07 17:08:02 -050013#include <phosphor-logging/elog.hpp>
14#include <phosphor-logging/log.hpp>
15#include <set>
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050016#include <xyz/openbmc_project/Common/error.hpp>
17
18namespace openpower
19{
20namespace software
21{
22namespace image
23{
24
25using namespace phosphor::logging;
26using namespace openpower::software::updater;
27using InternalFailure =
28 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
29
30constexpr auto keyTypeTag = "KeyType";
31constexpr auto hashFunctionTag = "HashType";
32
33Signature::Signature(const fs::path& imageDirPath,
Lei YU2b2d2292019-03-18 15:22:56 +080034 const std::string& pnorFileName,
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050035 const fs::path& signedConfPath) :
36 imageDirPath(imageDirPath),
Lei YU2b2d2292019-03-18 15:22:56 +080037 pnorFileName(pnorFileName), signedConfPath(signedConfPath)
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050038{
39 fs::path file(imageDirPath / MANIFEST_FILE);
40
41 auto keyValues =
42 Version::getValue(file, {{keyTypeTag, " "}, {hashFunctionTag, " "}});
43 keyType = keyValues.at(keyTypeTag);
44 hashType = keyValues.at(hashFunctionTag);
45}
46
47AvailableKeyTypes Signature::getAvailableKeyTypesFromSystem() const
48{
49 AvailableKeyTypes keyTypes{};
50
51 // Find the path of all the files
52 if (!fs::is_directory(signedConfPath))
53 {
54 log<level::ERR>("Signed configuration path not found in the system");
55 elog<InternalFailure>();
56 }
57
58 // Look for all the hash and public key file names get the key value
59 // For example:
60 // /etc/activationdata/OpenPOWER/publickey
61 // /etc/activationdata/OpenPOWER/hashfunc
62 // /etc/activationdata/GA/publickey
63 // /etc/activationdata/GA/hashfunc
64 // Set will have OpenPOWER, GA
65
66 for (const auto& p : fs::recursive_directory_iterator(signedConfPath))
67 {
68 if ((p.path().filename() == HASH_FILE_NAME) ||
69 (p.path().filename() == PUBLICKEY_FILE_NAME))
70 {
71 // extract the key types
72 // /etc/activationdata/GA/ -> get GA from the path
73 auto key = p.path().parent_path();
74 keyTypes.insert(key.filename());
75 }
76 }
77
78 return keyTypes;
79}
80
81inline KeyHashPathPair Signature::getKeyHashFileNames(const Key_t& key) const
82{
83 fs::path hashpath(signedConfPath / key / HASH_FILE_NAME);
84 fs::path keyPath(signedConfPath / key / PUBLICKEY_FILE_NAME);
85
86 return std::make_pair(std::move(hashpath), std::move(keyPath));
87}
88
89bool Signature::verify()
90{
91 try
92 {
93 // Verify the MANIFEST and publickey file using available
94 // public keys and hash on the system.
95 if (false == systemLevelVerify())
96 {
97 log<level::ERR>("System level Signature Validation failed");
98 return false;
99 }
100
Gunnar Mills4c7d2062018-03-23 12:12:20 -0500101 // image specific publickey file name.
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500102 fs::path publicKeyFile(imageDirPath / PUBLICKEY_FILE_NAME);
103
104 // Validate the PNOR image file.
105 // Build Image File name
106 fs::path file(imageDirPath);
Lei YU2b2d2292019-03-18 15:22:56 +0800107 file /= pnorFileName;
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500108
109 // Build Signature File name
110 std::string fileName = file.filename();
111 fs::path sigFile(imageDirPath);
112 sigFile /= fileName + SIGNATURE_FILE_EXT;
113
114 // Verify the signature.
115 auto valid = verifyFile(file, sigFile, publicKeyFile, hashType);
116 if (valid == false)
117 {
118 log<level::ERR>("Image file Signature Validation failed",
Lei YU2b2d2292019-03-18 15:22:56 +0800119 entry("IMAGE=%s", pnorFileName.c_str()));
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500120 return false;
121 }
122
Gunnar Mills4c7d2062018-03-23 12:12:20 -0500123 log<level::DEBUG>("Successfully completed Signature vaildation.");
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500124
125 return true;
126 }
127 catch (const InternalFailure& e)
128 {
129 return false;
130 }
131 catch (const std::exception& e)
132 {
133 log<level::ERR>(e.what());
134 return false;
135 }
136}
137
138bool Signature::systemLevelVerify()
139{
140 // Get available key types from the system.
141 auto keyTypes = getAvailableKeyTypesFromSystem();
142 if (keyTypes.empty())
143 {
144 log<level::ERR>("Missing Signature configuration data in system");
145 elog<InternalFailure>();
146 }
147
148 // Build publickey and its signature file name.
149 fs::path pkeyFile(imageDirPath / PUBLICKEY_FILE_NAME);
150 fs::path pkeyFileSig(pkeyFile);
151 pkeyFileSig.replace_extension(SIGNATURE_FILE_EXT);
152
153 // Build manifest and its signature file name.
154 fs::path manifestFile(imageDirPath / MANIFEST_FILE);
155 fs::path manifestFileSig(manifestFile);
156 manifestFileSig.replace_extension(SIGNATURE_FILE_EXT);
157
158 auto valid = false;
159
160 // Verify the file signature with available key types
161 // public keys and hash function.
162 // For any internal failure during the key/hash pair specific
163 // validation, should continue the validation with next
164 // available Key/hash pair.
165 for (const auto& keyType : keyTypes)
166 {
167 auto keyHashPair = getKeyHashFileNames(keyType);
168 auto keyValues =
169 Version::getValue(keyHashPair.first, {{hashFunctionTag, " "}});
170 auto hashFunc = keyValues.at(hashFunctionTag);
171
172 try
173 {
174 // Verify manifest file signature
175 valid = verifyFile(manifestFile, manifestFileSig,
176 keyHashPair.second, hashFunc);
177 if (valid)
178 {
179 // Verify publickey file signature.
180 valid = verifyFile(pkeyFile, pkeyFileSig, keyHashPair.second,
181 hashFunc);
182 if (valid)
183 {
184 break;
185 }
186 }
187 }
188 catch (const InternalFailure& e)
189 {
190 valid = false;
191 }
192 }
193 return valid;
194}
195
196bool Signature::verifyFile(const fs::path& file, const fs::path& sigFile,
197 const fs::path& publicKey,
198 const std::string& hashFunc)
199{
200
201 // Check existence of the files in the system.
202 if (!(fs::exists(file) && fs::exists(sigFile)))
203 {
204 log<level::ERR>("Failed to find the Data or signature file.",
205 entry("FILE=%s", file.c_str()));
206 elog<InternalFailure>();
207 }
208
209 // Create RSA.
210 auto publicRSA = createPublicRSA(publicKey);
211 if (publicRSA == nullptr)
212 {
213 log<level::ERR>("Failed to create RSA",
214 entry("FILE=%s", publicKey.c_str()));
215 elog<InternalFailure>();
216 }
217
218 // Assign key to RSA.
219 EVP_PKEY_Ptr pKeyPtr(EVP_PKEY_new(), ::EVP_PKEY_free);
220 EVP_PKEY_assign_RSA(pKeyPtr.get(), publicRSA);
221
222 // Initializes a digest context.
Adriana Kobylak70ca2422018-09-06 14:23:38 -0500223 EVP_MD_CTX_Ptr rsaVerifyCtx(EVP_MD_CTX_new(), ::EVP_MD_CTX_free);
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500224
225 // Adds all digest algorithms to the internal table
226 OpenSSL_add_all_digests();
227
Gunnar Millsa9cfced2018-04-08 15:01:57 -0500228 // Create Hash structure.
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500229 auto hashStruct = EVP_get_digestbyname(hashFunc.c_str());
230 if (!hashStruct)
231 {
232 log<level::ERR>("EVP_get_digestbynam: Unknown message digest",
233 entry("HASH=%s", hashFunc.c_str()));
234 elog<InternalFailure>();
235 }
236
237 auto result = EVP_DigestVerifyInit(rsaVerifyCtx.get(), nullptr, hashStruct,
238 nullptr, pKeyPtr.get());
239
240 if (result <= 0)
241 {
Gunnar Mills4c7d2062018-03-23 12:12:20 -0500242 log<level::ERR>("Error occurred during EVP_DigestVerifyInit",
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500243 entry("ERRCODE=%lu", ERR_get_error()));
244 elog<InternalFailure>();
245 }
246
247 // Hash the data file and update the verification context
248 auto size = fs::file_size(file);
249 auto dataPtr = mapFile(file, size);
250
251 result = EVP_DigestVerifyUpdate(rsaVerifyCtx.get(), dataPtr(), size);
252 if (result <= 0)
253 {
Gunnar Mills4c7d2062018-03-23 12:12:20 -0500254 log<level::ERR>("Error occurred during EVP_DigestVerifyUpdate",
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500255 entry("ERRCODE=%lu", ERR_get_error()));
256 elog<InternalFailure>();
257 }
258
259 // Verify the data with signature.
260 size = fs::file_size(sigFile);
261 auto signature = mapFile(sigFile, size);
262
263 result = EVP_DigestVerifyFinal(
264 rsaVerifyCtx.get(), reinterpret_cast<unsigned char*>(signature()),
265 size);
266
267 // Check the verification result.
268 if (result < 0)
269 {
Gunnar Mills4c7d2062018-03-23 12:12:20 -0500270 log<level::ERR>("Error occurred during EVP_DigestVerifyFinal",
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500271 entry("ERRCODE=%lu", ERR_get_error()));
272 elog<InternalFailure>();
273 }
274
275 if (result == 0)
276 {
277 log<level::ERR>("EVP_DigestVerifyFinal:Signature validation failed",
278 entry("PATH=%s", sigFile.c_str()));
279 return false;
280 }
281 return true;
282}
283
284inline RSA* Signature::createPublicRSA(const fs::path& publicKey)
285{
286 RSA* rsa = nullptr;
287 auto size = fs::file_size(publicKey);
288
289 // Read public key file
290 auto data = mapFile(publicKey, size);
291
292 BIO_MEM_Ptr keyBio(BIO_new_mem_buf(data(), -1), &::BIO_free);
293 if (keyBio.get() == nullptr)
294 {
295 log<level::ERR>("Failed to create new BIO Memory buffer");
296 elog<InternalFailure>();
297 }
298
299 rsa = PEM_read_bio_RSA_PUBKEY(keyBio.get(), &rsa, nullptr, nullptr);
300
301 return rsa;
302}
303
304CustomMap Signature::mapFile(const fs::path& path, size_t size)
305{
306
307 CustomFd fd(open(path.c_str(), O_RDONLY));
308
309 return CustomMap(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd(), 0),
310 size);
311}
312
313} // namespace image
314} // namespace software
315} // namespace openpower