blob: 3e65288e39a8c22393470fbf0eda9c116224a3dd [file] [log] [blame]
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -06001#pragma once
2#include <experimental/filesystem>
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -06003#include <set>
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -06004
5namespace phosphor
6{
7namespace software
8{
9namespace image
10{
11
12namespace fs = std::experimental::filesystem;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060013using Key_t = std::string;
14using Hash_t = std::string;
15using PublicKeyPath = fs::path;
16using HashFilePath = fs::path;
17using KeyHashPathPair = std::pair<HashFilePath, PublicKeyPath>;
18using AvailableKeyTypes = std::set<Key_t>;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060019
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060020// BMC flash image file name list.
21const std::vector<std::string> bmcImages = {"image-kernel", "image-rofs",
22 "image-rwfs", "image-u-boot"};
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060023/** @class Signature
24 * @brief Contains signature verification functions.
25 * @details The software image class that contains the signature
26 * verification functions for signed image.
27 */
28class Signature
29{
30 public:
31 Signature() = delete;
32 Signature(const Signature&) = delete;
33 Signature& operator=(const Signature&) = delete;
34 Signature(Signature&&) = default;
35 Signature& operator=(Signature&&) = default;
36 ~Signature() = default;
37
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060038 /**
39 * @brief Constructs Signature.
40 * @param[in] imageDirPath - image path
41 * @param[in] signedConfPath - Path of public key
42 * hash function files
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060043 */
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060044 Signature(const fs::path& imageDirPath, const fs::path& signedConfPath);
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060045
46 /**
47 * @brief Image signature verification function.
48 * Verify the Manifest and public key file signature using the
49 * public keys available in the system first. After successful
50 * validation, continue the whole image files signature
51 * validation using the image specific public key and the
52 * hash function.
53 *
54 * @return true if signature verification was successful,
55 * false if not
56 */
57 bool verify();
58
59 private:
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060060 /**
61 * @brief Function used for system level file signature validation
62 * of image specfic publickey file and manifest file
63 * using the available public keys and hash functions
64 * in the system.
65 * Refer code-update documenation for more details.
66 */
67 bool systemLevelVerify();
68
69 /**
70 * @brief Return all key types stored in the BMC based on the
71 * public key and hashfunc files stored in the BMC.
72 *
73 * @return list
74 */
75 AvailableKeyTypes getAvailableKeyTypesFromSystem() const;
76
77 /**
78 * @brief Return public key and hash function file names for the
79 * corresponding key type
80 *
81 * @param[in] key - key type
82 * @return Pair of hash and public key file names
83 */
84 inline KeyHashPathPair getKeyHashFileNames(const Key_t& key) const;
85
86 /**
87 * @brief Verify the file signature using public key and hash function
88 *
89 * @param[in] - Image file path
90 * @param[in] - Signature file path
91 * @param[in] - Public key
92 * @param[in] - Hash function name
93 * @return true if signature verification was successful, false if not
94 */
95 bool verifyFile(const fs::path& file, const fs::path& signature,
96 const fs::path& publicKey, const std::string& hashFunc);
97
98 /** @brief Directory where software images are placed */
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060099 fs::path imageDirPath;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600100
101 /** @brief Path of public key and hash function files */
102 fs::path signedConfPath;
103
104 /** @brief key type defined in mainfest file */
105 Key_t keyType;
106
107 /** @brief Hash type defined in mainfest file */
108 Hash_t hashType;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600109};
110
111} // namespace image
112} // namespace software
113} // namespace phosphor