blob: c40713c7d68734ca90d556da73e57c3060d6644d [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.
32const std::vector<std::string> bmcImages = {"image-kernel", "image-rofs",
33 "image-rwfs", "image-u-boot"};
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060034/** @struct CustomFd
35 *
36 * RAII wrapper for file descriptor.
37 */
38struct CustomFd
39{
40 public:
41 CustomFd() = delete;
42 CustomFd(const CustomFd&) = delete;
43 CustomFd& operator=(const CustomFd&) = delete;
44 CustomFd(CustomFd&&) = default;
45 CustomFd& operator=(CustomFd&&) = default;
46 /** @brief Saves File descriptor and uses it to do file operation
47 *
48 * @param[in] fd - File descriptor
49 */
50 CustomFd(int fd) : fd(fd)
51 {
52 }
53
54 ~CustomFd()
55 {
56 if (fd >= 0)
57 {
58 close(fd);
59 }
60 }
61
62 int operator()() const
63 {
64 return fd;
65 }
66
67 private:
68 /** @brief File descriptor */
69 int fd = -1;
70};
71
72/** @struct CustomMap
73 *
74 * RAII wrapper for mmap.
75 */
76struct CustomMap
77{
78 private:
79 /** @brief starting address of the map */
80 void* addr;
81
82 /** @brief length of the mapping */
83 size_t length;
84
85 public:
86 CustomMap() = delete;
87 CustomMap(const CustomMap&) = delete;
88 CustomMap& operator=(const CustomMap&) = delete;
89 CustomMap(CustomMap&&) = default;
90 CustomMap& operator=(CustomMap&&) = default;
91
92 /** @brief Saves starting address of the map and
93 * and length of the file.
94 * @param[in] addr - Starting address of the map
95 * @param[in] length - length of the map
96 */
97 CustomMap(void* addr, size_t length) : addr(addr), length(length)
98 {
99 }
100
101 ~CustomMap()
102 {
103 munmap(addr, length);
104 }
105
106 void* operator()() const
107 {
108 return addr;
109 }
110};
111
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600112/** @class Signature
113 * @brief Contains signature verification functions.
114 * @details The software image class that contains the signature
115 * verification functions for signed image.
116 */
117class Signature
118{
119 public:
120 Signature() = delete;
121 Signature(const Signature&) = delete;
122 Signature& operator=(const Signature&) = delete;
123 Signature(Signature&&) = default;
124 Signature& operator=(Signature&&) = default;
125 ~Signature() = default;
126
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600127 /**
128 * @brief Constructs Signature.
129 * @param[in] imageDirPath - image path
130 * @param[in] signedConfPath - Path of public key
131 * hash function files
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600132 */
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600133 Signature(const fs::path& imageDirPath, const fs::path& signedConfPath);
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600134
135 /**
136 * @brief Image signature verification function.
137 * Verify the Manifest and public key file signature using the
138 * public keys available in the system first. After successful
139 * validation, continue the whole image files signature
140 * validation using the image specific public key and the
141 * hash function.
142 *
143 * @return true if signature verification was successful,
144 * false if not
145 */
146 bool verify();
147
148 private:
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600149 /**
150 * @brief Function used for system level file signature validation
Gunnar Millse11a2022018-03-23 12:04:48 -0500151 * of image specific publickey file and manifest file
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600152 * using the available public keys and hash functions
153 * in the system.
Gunnar Mills2bcba022018-04-08 15:02:04 -0500154 * Refer code-update documentation for more details.
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600155 */
156 bool systemLevelVerify();
157
158 /**
159 * @brief Return all key types stored in the BMC based on the
160 * public key and hashfunc files stored in the BMC.
161 *
162 * @return list
163 */
164 AvailableKeyTypes getAvailableKeyTypesFromSystem() const;
165
166 /**
167 * @brief Return public key and hash function file names for the
168 * corresponding key type
169 *
170 * @param[in] key - key type
171 * @return Pair of hash and public key file names
172 */
173 inline KeyHashPathPair getKeyHashFileNames(const Key_t& key) const;
174
175 /**
176 * @brief Verify the file signature using public key and hash function
177 *
178 * @param[in] - Image file path
179 * @param[in] - Signature file path
180 * @param[in] - Public key
181 * @param[in] - Hash function name
182 * @return true if signature verification was successful, false if not
183 */
184 bool verifyFile(const fs::path& file, const fs::path& signature,
185 const fs::path& publicKey, const std::string& hashFunc);
186
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -0600187 /**
188 * @brief Create RSA object from the public key
189 * @param[in] - publickey
190 * @param[out] - RSA Object.
191 */
192 inline RSA* createPublicRSA(const fs::path& publicKey);
193
194 /**
195 * @brief Memory map the file
196 * @param[in] - file path
197 * @param[in] - file size
198 * @param[out] - Custom Mmap address
199 */
200 CustomMap mapFile(const fs::path& path, size_t size);
201
202 /** @brief Directory where software images are placed*/
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600203 fs::path imageDirPath;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600204
205 /** @brief Path of public key and hash function files */
206 fs::path signedConfPath;
207
208 /** @brief key type defined in mainfest file */
209 Key_t keyType;
210
211 /** @brief Hash type defined in mainfest file */
212 Hash_t hashType;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600213};
214
215} // namespace image
216} // namespace software
217} // namespace phosphor