blob: 1c32b1a524b5113ac8d700fb014dfba1a2528aa0 [file] [log] [blame]
Gunnar Millsb0ce9962018-09-07 13:39:10 -05001#include "image_verify.hpp"
George Liu0a06e972020-12-17 09:17:04 +08002#include "utils.hpp"
Gunnar Mills01d55c32017-04-20 10:52:15 -05003#include "version.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05004
Patrick Williams0890ab92021-12-08 10:30:23 -06005#include <openssl/evp.h>
Gunnar Mills01d55c32017-04-20 10:52:15 -05006#include <stdlib.h>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007
Adriana Kobylakc98d9122020-05-05 10:36:01 -05008#include <filesystem>
Gunnar Mills01d55c32017-04-20 10:52:15 -05009#include <fstream>
10#include <iostream>
11#include <sstream>
12#include <string>
George Liu0a06e972020-12-17 09:17:04 +080013#include <vector>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050014
15#include <gtest/gtest.h>
Gunnar Mills01d55c32017-04-20 10:52:15 -050016
17using namespace phosphor::software::manager;
Jayanth Othayoth6be275b2018-02-27 08:36:38 -060018using namespace phosphor::software::image;
Gunnar Mills01d55c32017-04-20 10:52:15 -050019
Gunnar Mills01d55c32017-04-20 10:52:15 -050020class VersionTest : public testing::Test
21{
Adriana Kobylak2285fe02018-02-27 15:36:59 -060022 protected:
23 virtual void SetUp()
24 {
25 char versionDir[] = "./versionXXXXXX";
26 _directory = mkdtemp(versionDir);
Gunnar Mills01d55c32017-04-20 10:52:15 -050027
Adriana Kobylak2285fe02018-02-27 15:36:59 -060028 if (_directory.empty())
Gunnar Mills01d55c32017-04-20 10:52:15 -050029 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060030 throw std::bad_alloc();
Gunnar Mills01d55c32017-04-20 10:52:15 -050031 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060032 }
Gunnar Mills01d55c32017-04-20 10:52:15 -050033
Adriana Kobylak2285fe02018-02-27 15:36:59 -060034 virtual void TearDown()
35 {
36 fs::remove_all(_directory);
37 }
Gunnar Mills01d55c32017-04-20 10:52:15 -050038
Adriana Kobylak2285fe02018-02-27 15:36:59 -060039 std::string _directory;
Gunnar Mills01d55c32017-04-20 10:52:15 -050040};
41
42/** @brief Make sure we correctly get the version and purpose from getValue()*/
43TEST_F(VersionTest, TestGetValue)
44{
45 auto manifestFilePath = _directory + "/" + "MANIFEST";
46 auto version = "test-version";
47 auto purpose = "BMC";
48
49 std::ofstream file;
50 file.open(manifestFilePath, std::ofstream::out);
51 ASSERT_TRUE(file.is_open());
52
Lei YU5a7363b2019-10-18 16:50:59 +080053 file << "version=" << version << "\n";
54 file << "purpose=" << purpose << "\n";
55 file.close();
56
57 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
58 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
59}
60
61TEST_F(VersionTest, TestGetValueWithCRLF)
62{
63 auto manifestFilePath = _directory + "/" + "MANIFEST";
64 auto version = "test-version";
65 auto purpose = "BMC";
66
67 std::ofstream file;
68 file.open(manifestFilePath, std::ofstream::out);
69 ASSERT_TRUE(file.is_open());
70
71 file << "version=" << version << "\r\n";
72 file << "purpose=" << purpose << "\r\n";
Gunnar Mills01d55c32017-04-20 10:52:15 -050073 file.close();
74
75 EXPECT_EQ(Version::getValue(manifestFilePath, "version"), version);
76 EXPECT_EQ(Version::getValue(manifestFilePath, "purpose"), purpose);
77}
78
Adriana Kobylak2a3d9f52020-05-06 10:46:32 -050079TEST_F(VersionTest, TestGetVersionWithQuotes)
80{
81 auto releasePath = _directory + "/" + "os-release";
82 auto version = "1.2.3-test-version";
83
84 std::ofstream file;
85 file.open(releasePath, std::ofstream::out);
86 ASSERT_TRUE(file.is_open());
87
88 file << "VERSION_ID=\"" << version << "\"\n";
89 file.close();
90
91 EXPECT_EQ(Version::getBMCVersion(releasePath), version);
92}
93
94TEST_F(VersionTest, TestGetVersionWithoutQuotes)
95{
96 auto releasePath = _directory + "/" + "os-release";
97 auto version = "9.88.1-test-version";
98
99 std::ofstream file;
100 file.open(releasePath, std::ofstream::out);
101 ASSERT_TRUE(file.is_open());
102
103 file << "VERSION_ID=" << version << "\n";
104 file.close();
105
106 EXPECT_EQ(Version::getBMCVersion(releasePath), version);
107}
108
Gunnar Mills01d55c32017-04-20 10:52:15 -0500109/** @brief Make sure we correctly get the Id from getId()*/
110TEST_F(VersionTest, TestGetId)
111{
Gunnar Mills01d55c32017-04-20 10:52:15 -0500112 auto version = "test-id";
Patrick Williams0890ab92021-12-08 10:30:23 -0600113 unsigned char digest[EVP_MAX_MD_SIZE];
114 unsigned int digest_count = 0;
115
116 EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free);
117
118 EVP_DigestInit(ctx.get(), EVP_sha512());
119 EVP_DigestUpdate(ctx.get(), version, strlen(version));
120 EVP_DigestFinal(ctx.get(), digest, &digest_count);
121
122 char mdString[EVP_MAX_MD_SIZE * 2 + 1];
123 for (decltype(digest_count) i = 0; i < digest_count; i++)
Saqib Khan26a960d2017-09-19 14:23:28 -0500124 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600125 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
Saqib Khan26a960d2017-09-19 14:23:28 -0500126 }
127 std::string hexId = std::string(mdString);
128 hexId = hexId.substr(0, 8);
129 EXPECT_EQ(Version::getId(version), hexId);
Gunnar Mills01d55c32017-04-20 10:52:15 -0500130}
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600131
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700132TEST_F(VersionTest, TestGetExtendedVersion)
133{
134 auto releasePath = _directory + "/" + "os-release";
135 auto ExtendedVersion = "9.88.1-test-ExtendedVersion";
136
137 std::ofstream file;
138 file.open(releasePath, std::ofstream::out);
139 ASSERT_TRUE(file.is_open());
140
141 file << "EXTENDED_VERSION=" << ExtendedVersion << "\n";
142 file.close();
143
144 EXPECT_EQ(Version::getBMCExtendedVersion(releasePath), ExtendedVersion);
145}
146
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600147class SignatureTest : public testing::Test
148{
149 static constexpr auto opensslCmd = "openssl dgst -sha256 -sign ";
150 static constexpr auto testPath = "/tmp/_testSig";
151
152 protected:
153 void command(const std::string& cmd)
154 {
155 auto val = std::system(cmd.c_str());
156 if (val)
157 {
158 std::cout << "COMMAND Error: " << val << std::endl;
159 }
160 }
161 virtual void SetUp()
162 {
163 // Create test base directory.
164 fs::create_directories(testPath);
165
166 // Create unique temporary path for images
167 std::string tmpDir(testPath);
168 tmpDir += "/extractXXXXXX";
169 std::string imageDir = mkdtemp(const_cast<char*>(tmpDir.c_str()));
170
171 // Create unique temporary configuration path
172 std::string tmpConfDir(testPath);
173 tmpConfDir += "/confXXXXXX";
174 std::string confDir = mkdtemp(const_cast<char*>(tmpConfDir.c_str()));
175
176 extractPath = imageDir;
177 extractPath /= "images";
178
179 signedConfPath = confDir;
180 signedConfPath /= "conf";
181
182 signedConfOpenBMCPath = confDir;
183 signedConfOpenBMCPath /= "conf";
184 signedConfOpenBMCPath /= "OpenBMC";
185
186 std::cout << "SETUP " << std::endl;
187
188 command("mkdir " + extractPath.string());
189 command("mkdir " + signedConfPath.string());
190 command("mkdir " + signedConfOpenBMCPath.string());
191
192 std::string hashFile = signedConfOpenBMCPath.string() + "/hashfunc";
193 command("echo \"HashType=RSA-SHA256\" > " + hashFile);
194
195 std::string manifestFile = extractPath.string() + "/" + "MANIFEST";
196 command("echo \"HashType=RSA-SHA256\" > " + manifestFile);
197 command("echo \"KeyType=OpenBMC\" >> " + manifestFile);
198
199 std::string kernelFile = extractPath.string() + "/" + "image-kernel";
200 command("echo \"image-kernel file \" > " + kernelFile);
201
202 std::string rofsFile = extractPath.string() + "/" + "image-rofs";
203 command("echo \"image-rofs file \" > " + rofsFile);
204
205 std::string rwfsFile = extractPath.string() + "/" + "image-rwfs";
206 command("echo \"image-rwfs file \" > " + rwfsFile);
207
208 std::string ubootFile = extractPath.string() + "/" + "image-u-boot";
209 command("echo \"image-u-boot file \" > " + ubootFile);
210
211 std::string pkeyFile = extractPath.string() + "/" + "private.pem";
212 command("openssl genrsa -out " + pkeyFile + " 2048");
213
214 std::string pubkeyFile = extractPath.string() + "/" + "publickey";
215 command("openssl rsa -in " + pkeyFile + " -outform PEM " +
216 "-pubout -out " + pubkeyFile);
217
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600218 command("cp " + pubkeyFile + " " + signedConfOpenBMCPath.string());
219 command(opensslCmd + pkeyFile + " -out " + kernelFile + ".sig " +
220 kernelFile);
221
222 command(opensslCmd + pkeyFile + " -out " + manifestFile + ".sig " +
223 manifestFile);
224 command(opensslCmd + pkeyFile + " -out " + rofsFile + ".sig " +
225 rofsFile);
226 command(opensslCmd + pkeyFile + " -out " + rwfsFile + ".sig " +
227 rwfsFile);
228 command(opensslCmd + pkeyFile + " -out " + ubootFile + ".sig " +
229 ubootFile);
230 command(opensslCmd + pkeyFile + " -out " + pubkeyFile + ".sig " +
231 pubkeyFile);
232
George Liu1c8781f2021-08-26 16:09:18 +0800233#ifdef WANT_SIGNATURE_FULL_VERIFY
234 std::string fullFile = extractPath.string() + "/" + "image-full";
235 command("cat " + kernelFile + ".sig " + rofsFile + ".sig " + rwfsFile +
236 ".sig " + ubootFile + ".sig " + manifestFile + ".sig " +
237 pubkeyFile + ".sig > " + fullFile);
238 command(opensslCmd + pkeyFile + " -out " + fullFile + ".sig " +
239 fullFile);
240#endif
241
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600242 signature = std::make_unique<Signature>(extractPath, signedConfPath);
243 }
244 virtual void TearDown()
245 {
246 command("rm -rf " + std::string(testPath));
247 }
248
249 std::unique_ptr<Signature> signature;
250 fs::path extractPath;
251 fs::path signedConfPath;
252 fs::path signedConfOpenBMCPath;
253};
254
Gunnar Mills2bcba022018-04-08 15:02:04 -0500255/** @brief Test for success scenario*/
Jayanth Othayoth6be275b2018-02-27 08:36:38 -0600256TEST_F(SignatureTest, TestSignatureVerify)
257{
258 EXPECT_TRUE(signature->verify());
259}
260
261/** @brief Test failure scenario with corrupted signature file*/
262TEST_F(SignatureTest, TestCorruptSignatureFile)
263{
264 // corrupt the image-kernel.sig file and ensure that verification fails
265 std::string kernelFile = extractPath.string() + "/" + "image-kernel";
266 command("echo \"dummy data\" > " + kernelFile + ".sig ");
267 EXPECT_FALSE(signature->verify());
268}
269
270/** @brief Test failure scenario with no public key in the image*/
271TEST_F(SignatureTest, TestNoPublicKeyInImage)
272{
273 // Remove publickey file from the image and ensure that verify fails
274 std::string pubkeyFile = extractPath.string() + "/" + "publickey";
275 command("rm " + pubkeyFile);
276 EXPECT_FALSE(signature->verify());
277}
278
279/** @brief Test failure scenario with invalid hash function value*/
280TEST_F(SignatureTest, TestInvalidHashValue)
281{
282 // Change the hashfunc value and ensure that verification fails
283 std::string hashFile = signedConfOpenBMCPath.string() + "/hashfunc";
284 command("echo \"HashType=md5\" > " + hashFile);
285 EXPECT_FALSE(signature->verify());
286}
287
288/** @brief Test for failure scenario with no config file in system*/
289TEST_F(SignatureTest, TestNoConfigFileInSystem)
290{
291 // Remove the conf folder in the system and ensure that verify fails
292 command("rm -rf " + signedConfOpenBMCPath.string());
293 EXPECT_FALSE(signature->verify());
294}
George Liu0a06e972020-12-17 09:17:04 +0800295
296class FileTest : public testing::Test
297{
298 protected:
299 std::string readFile(fs::path path)
300 {
301 std::ifstream f(path, std::ios::in);
302 const auto sz = fs::file_size(path);
303 std::string result(sz, '\0');
304 f.read(result.data(), sz);
305
306 return result;
307 }
308
309 void command(const std::string& cmd)
310 {
311 auto val = std::system(cmd.c_str());
312 if (val)
313 {
314 std::cout << "COMMAND Error: " << val << std::endl;
315 }
316 }
317
318 virtual void SetUp()
319 {
320 // Create test base directory.
321 tmpDir = fs::temp_directory_path() / "testFileXXXXXX";
322 if (!mkdtemp(tmpDir.data()))
323 {
324 throw "Failed to create tmp dir";
325 }
326
327 std::string file1 = tmpDir + "/file1";
328 std::string file2 = tmpDir + "/file2";
329 command("echo \"File Test1\n\n\" > " + file1);
330 command("echo \"FileTe st2\n\nte st2\" > " + file2);
331
332 srcFiles.push_back(file1);
333 srcFiles.push_back(file2);
334 }
335
336 virtual void TearDown()
337 {
338 fs::remove_all(tmpDir);
339 }
340
341 std::vector<std::string> srcFiles;
342 std::string tmpDir;
343};
344
345TEST_F(FileTest, TestMergeFiles)
346{
347 std::string retFile = tmpDir + "/retFile";
348 for (auto file : srcFiles)
349 {
350 command("cat " + file + " >> " + retFile);
351 }
352
353 std::string dstFile = tmpDir + "/dstFile";
354 utils::mergeFiles(srcFiles, dstFile);
355
356 ASSERT_NE(fs::file_size(retFile), static_cast<uintmax_t>(-1));
357 ASSERT_NE(fs::file_size(dstFile), static_cast<uintmax_t>(-1));
358 ASSERT_EQ(fs::file_size(retFile), fs::file_size(dstFile));
359
360 std::string ssRetFile = readFile(fs::path(retFile));
361 std::string ssDstFile = readFile(fs::path(dstFile));
362 ASSERT_EQ(ssRetFile, ssDstFile);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -0600363}
364
365TEST(ExecTest, TestConstructArgv)
366{
367 auto name = "/bin/ls";
368 auto arg1 = "-a";
369 auto arg2 = "-l";
370 auto arg3 = "-t";
371 auto arg4 = "-rS";
372 auto argV = utils::internal::constructArgv(name, arg1, arg2, arg3, arg4);
373 char** charArray = argV.data();
374 EXPECT_EQ(argV.size(), 6);
375 EXPECT_EQ(charArray[0], name);
376 EXPECT_EQ(charArray[1], arg1);
377 EXPECT_EQ(charArray[2], arg2);
378 EXPECT_EQ(charArray[3], arg3);
379 EXPECT_EQ(charArray[4], arg4);
380 EXPECT_EQ(charArray[5], nullptr);
381
382 name = "/usr/bin/du";
383 argV = utils::internal::constructArgv(name);
384 charArray = argV.data();
385 EXPECT_EQ(argV.size(), 2);
386 EXPECT_EQ(charArray[0], name);
387 EXPECT_EQ(charArray[1], nullptr);
388
389 name = "/usr/bin/hexdump";
390 arg1 = "-C";
391 arg2 = "/path/to/filename";
392 argV = utils::internal::constructArgv(name, arg1, arg2);
393 charArray = argV.data();
394 EXPECT_EQ(argV.size(), 4);
395 EXPECT_EQ(charArray[0], name);
396 EXPECT_EQ(charArray[1], arg1);
397 EXPECT_EQ(charArray[2], arg2);
398 EXPECT_EQ(charArray[3], nullptr);
399}