blob: 3c9c2a4e5791a4df01439bb559cd443e447b4542 [file] [log] [blame]
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -06001#pragma once
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -06002#include <openssl/rsa.h>
3#include <openssl/evp.h>
4#include <openssl/pem.h>
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -06005#include <experimental/filesystem>
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -06006#include <set>
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -06007#include <unistd.h>
8#include <sys/mman.h>
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -06009
10namespace phosphor
11{
12namespace software
13{
14namespace image
15{
16
17namespace fs = std::experimental::filesystem;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060018using Key_t = std::string;
19using Hash_t = std::string;
20using PublicKeyPath = fs::path;
21using HashFilePath = fs::path;
22using KeyHashPathPair = std::pair<HashFilePath, PublicKeyPath>;
23using AvailableKeyTypes = std::set<Key_t>;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060024
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060025// RAII support for openSSL functions.
26using BIO_MEM_Ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;
27using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
28using EVP_MD_CTX_Ptr =
29 std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_destroy)>;
30
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060031// BMC flash image file name list.
Lei YUa7853ee2018-05-23 11:13:12 +080032#ifdef UBIFS_LAYOUT
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060033const std::vector<std::string> bmcImages = {"image-kernel", "image-rofs",
34 "image-rwfs", "image-u-boot"};
Lei YUa7853ee2018-05-23 11:13:12 +080035#else
36const std::vector<std::string> bmcImages = {"image-bmc"};
37#endif
38
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060039/** @struct CustomFd
40 *
41 * RAII wrapper for file descriptor.
42 */
43struct CustomFd
44{
45 public:
46 CustomFd() = delete;
47 CustomFd(const CustomFd&) = delete;
48 CustomFd& operator=(const CustomFd&) = delete;
49 CustomFd(CustomFd&&) = default;
50 CustomFd& operator=(CustomFd&&) = default;
51 /** @brief Saves File descriptor and uses it to do file operation
52 *
53 * @param[in] fd - File descriptor
54 */
55 CustomFd(int fd) : fd(fd)
56 {
57 }
58
59 ~CustomFd()
60 {
61 if (fd >= 0)
62 {
63 close(fd);
64 }
65 }
66
67 int operator()() const
68 {
69 return fd;
70 }
71
72 private:
73 /** @brief File descriptor */
74 int fd = -1;
75};
76
77/** @struct CustomMap
78 *
79 * RAII wrapper for mmap.
80 */
81struct CustomMap
82{
83 private:
84 /** @brief starting address of the map */
85 void* addr;
86
87 /** @brief length of the mapping */
88 size_t length;
89
90 public:
91 CustomMap() = delete;
92 CustomMap(const CustomMap&) = delete;
93 CustomMap& operator=(const CustomMap&) = delete;
94 CustomMap(CustomMap&&) = default;
95 CustomMap& operator=(CustomMap&&) = default;
96
97 /** @brief Saves starting address of the map and
98 * and length of the file.
99 * @param[in] addr - Starting address of the map
100 * @param[in] length - length of the map
101 */
102 CustomMap(void* addr, size_t length) : addr(addr), length(length)
103 {
104 }
105
106 ~CustomMap()
107 {
108 munmap(addr, length);
109 }
110
111 void* operator()() const
112 {
113 return addr;
114 }
115};
116
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600117/** @class Signature
118 * @brief Contains signature verification functions.
119 * @details The software image class that contains the signature
120 * verification functions for signed image.
121 */
122class Signature
123{
124 public:
125 Signature() = delete;
126 Signature(const Signature&) = delete;
127 Signature& operator=(const Signature&) = delete;
128 Signature(Signature&&) = default;
129 Signature& operator=(Signature&&) = default;
130 ~Signature() = default;
131
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600132 /**
133 * @brief Constructs Signature.
134 * @param[in] imageDirPath - image path
135 * @param[in] signedConfPath - Path of public key
136 * hash function files
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600137 */
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600138 Signature(const fs::path& imageDirPath, const fs::path& signedConfPath);
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600139
140 /**
141 * @brief Image signature verification function.
142 * Verify the Manifest and public key file signature using the
143 * public keys available in the system first. After successful
144 * validation, continue the whole image files signature
145 * validation using the image specific public key and the
146 * hash function.
147 *
148 * @return true if signature verification was successful,
149 * false if not
150 */
151 bool verify();
152
153 private:
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600154 /**
155 * @brief Function used for system level file signature validation
Gunnar Millse11a2022018-03-23 12:04:48 -0500156 * of image specific publickey file and manifest file
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600157 * using the available public keys and hash functions
158 * in the system.
Gunnar Mills2bcba022018-04-08 15:02:04 -0500159 * Refer code-update documentation for more details.
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600160 */
161 bool systemLevelVerify();
162
163 /**
164 * @brief Return all key types stored in the BMC based on the
165 * public key and hashfunc files stored in the BMC.
166 *
167 * @return list
168 */
169 AvailableKeyTypes getAvailableKeyTypesFromSystem() const;
170
171 /**
172 * @brief Return public key and hash function file names for the
173 * corresponding key type
174 *
175 * @param[in] key - key type
176 * @return Pair of hash and public key file names
177 */
178 inline KeyHashPathPair getKeyHashFileNames(const Key_t& key) const;
179
180 /**
181 * @brief Verify the file signature using public key and hash function
182 *
183 * @param[in] - Image file path
184 * @param[in] - Signature file path
185 * @param[in] - Public key
186 * @param[in] - Hash function name
187 * @return true if signature verification was successful, false if not
188 */
189 bool verifyFile(const fs::path& file, const fs::path& signature,
190 const fs::path& publicKey, const std::string& hashFunc);
191
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -0600192 /**
193 * @brief Create RSA object from the public key
194 * @param[in] - publickey
195 * @param[out] - RSA Object.
196 */
197 inline RSA* createPublicRSA(const fs::path& publicKey);
198
199 /**
200 * @brief Memory map the file
201 * @param[in] - file path
202 * @param[in] - file size
203 * @param[out] - Custom Mmap address
204 */
205 CustomMap mapFile(const fs::path& path, size_t size);
206
207 /** @brief Directory where software images are placed*/
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600208 fs::path imageDirPath;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600209
210 /** @brief Path of public key and hash function files */
211 fs::path signedConfPath;
212
213 /** @brief key type defined in mainfest file */
214 Key_t keyType;
215
216 /** @brief Hash type defined in mainfest file */
217 Hash_t hashType;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600218};
219
220} // namespace image
221} // namespace software
222} // namespace phosphor