blob: 0aff4b84ef403be4bf4edca0f20a83f09272fd77 [file] [log] [blame]
Jayanth Othayoth70804dc2018-03-20 06:31:59 -05001#pragma once
Adriana Kobylak70ca2422018-09-06 14:23:38 -05002#include "utils.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05003
Jayanth Othayoth70804dc2018-03-20 06:31:59 -05004#include <openssl/evp.h>
5#include <openssl/pem.h>
Gunnar Millsf6ed5892018-09-07 17:08:02 -05006#include <openssl/rsa.h>
7#include <sys/mman.h>
8#include <unistd.h>
9
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050010#include <experimental/filesystem>
11#include <set>
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050012
13namespace openpower
14{
15namespace software
16{
17namespace image
18{
19
20namespace fs = std::experimental::filesystem;
21using Key_t = std::string;
22using Hash_t = std::string;
23using PublicKeyPath = fs::path;
24using HashFilePath = fs::path;
25using KeyHashPathPair = std::pair<HashFilePath, PublicKeyPath>;
26using AvailableKeyTypes = std::set<Key_t>;
27
28// RAII support for openSSL functions.
29using BIO_MEM_Ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;
30using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
31using EVP_MD_CTX_Ptr =
Adriana Kobylak70ca2422018-09-06 14:23:38 -050032 std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>;
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050033
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050034/** @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 */
Lei YU1db9adf2019-03-05 16:02:31 +080050 explicit CustomFd(int fd) : fd(fd)
Jayanth Othayoth70804dc2018-03-20 06:31:59 -050051 {
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
112/** @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
127 /**
128 * @brief Constructs Signature.
129 * @param[in] imageDirPath - image path
130 * @param[in] signedConfPath - Path of public key
131 * hash function files
132 */
Lei YU2b2d2292019-03-18 15:22:56 +0800133 explicit Signature(const fs::path& imageDirPath,
134 const std::string& pnorFileName,
135 const fs::path& signedConfPath);
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500136
137 /**
138 * @brief Image signature verification function.
139 * Verify the Manifest and public key file signature using the
140 * public keys available in the system first. After successful
141 * validation, continue the whole image files signature
142 * validation using the image specific public key and the
143 * hash function.
144 *
145 * @return true if signature verification was successful,
146 * false if not
147 */
148 bool verify();
149
150 private:
151 /**
152 * @brief Function used for system level file signature validation
153 * of image specific publickey file and manifest file
154 * using the available public keys and hash functions
155 * in the system.
156 * Refer code-update documentation for more details.
157 */
158 bool systemLevelVerify();
159
160 /**
161 * @brief Return all key types stored in the BMC based on the
162 * public key and hashfunc files stored in the BMC.
163 *
164 * @return list
165 */
166 AvailableKeyTypes getAvailableKeyTypesFromSystem() const;
167
168 /**
169 * @brief Return public key and hash function file names for the
170 * corresponding key type
171 *
172 * @param[in] key - key type
173 * @return Pair of hash and public key file names
174 */
175 inline KeyHashPathPair getKeyHashFileNames(const Key_t& key) const;
176
177 /**
178 * @brief Verify the file signature using public key and hash function
179 *
180 * @param[in] - Image file path
181 * @param[in] - Signature file path
182 * @param[in] - Public key
183 * @param[in] - Hash function name
184 * @return true if signature verification was successful, false if not
185 */
186 bool verifyFile(const fs::path& file, const fs::path& signature,
187 const fs::path& publicKey, const std::string& hashFunc);
188
189 /**
190 * @brief Create RSA object from the public key
191 * @param[in] - publickey
192 * @param[out] - RSA Object.
193 */
194 inline RSA* createPublicRSA(const fs::path& publicKey);
195
196 /**
197 * @brief Memory map the file
198 * @param[in] - file path
199 * @param[in] - file size
200 * @param[out] - Custom Mmap address
201 */
202 CustomMap mapFile(const fs::path& path, size_t size);
203
204 /** @brief Directory where software images are placed*/
205 fs::path imageDirPath;
206
Lei YU2b2d2292019-03-18 15:22:56 +0800207 /** @brief The PNOR file name in imageDirPath */
208 std::string pnorFileName;
209
Jayanth Othayoth70804dc2018-03-20 06:31:59 -0500210 /** @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;
218};
219
220} // namespace image
221} // namespace software
222} // namespace openpower