blob: f5acbb9ff8e5acdd6ed077429833b5c1f8c53f86 [file] [log] [blame]
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -06001#pragma once
Jayashankar Padatha0135602019-04-22 16:22:58 +05302#include "openssl_alloc.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05003
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -06004#include <openssl/evp.h>
5#include <openssl/pem.h>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05006#include <openssl/rsa.h>
7#include <sys/mman.h>
8#include <unistd.h>
9
Adriana Kobylakc98d9122020-05-05 10:36:01 -050010#include <filesystem>
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060011#include <set>
Andrew Geissler9155b712020-05-16 13:04:44 -050012#include <string>
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060013
14namespace phosphor
15{
16namespace software
17{
18namespace image
19{
20
Adriana Kobylakc98d9122020-05-05 10:36:01 -050021namespace fs = std::filesystem;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060022using Key_t = std::string;
23using Hash_t = std::string;
24using PublicKeyPath = fs::path;
25using HashFilePath = fs::path;
26using KeyHashPathPair = std::pair<HashFilePath, PublicKeyPath>;
27using AvailableKeyTypes = std::set<Key_t>;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -060028
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060029// RAII support for openSSL functions.
30using BIO_MEM_Ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>;
31using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
32using EVP_MD_CTX_Ptr =
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050033 std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>;
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060034
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060035/** @struct CustomFd
36 *
37 * RAII wrapper for file descriptor.
38 */
39struct CustomFd
40{
41 public:
42 CustomFd() = delete;
43 CustomFd(const CustomFd&) = delete;
44 CustomFd& operator=(const CustomFd&) = delete;
45 CustomFd(CustomFd&&) = default;
46 CustomFd& operator=(CustomFd&&) = default;
47 /** @brief Saves File descriptor and uses it to do file operation
48 *
49 * @param[in] fd - File descriptor
50 */
51 CustomFd(int fd) : fd(fd)
Adriana Kobylak58aa7502020-06-08 11:12:11 -050052 {}
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060053
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)
Adriana Kobylak58aa7502020-06-08 11:12:11 -050098 {}
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -060099
100 ~CustomMap()
101 {
102 munmap(addr, length);
103 }
104
105 void* operator()() const
106 {
107 return addr;
108 }
109};
110
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600111/** @class Signature
112 * @brief Contains signature verification functions.
113 * @details The software image class that contains the signature
114 * verification functions for signed image.
115 */
116class Signature
117{
118 public:
119 Signature() = delete;
120 Signature(const Signature&) = delete;
121 Signature& operator=(const Signature&) = delete;
122 Signature(Signature&&) = default;
123 Signature& operator=(Signature&&) = default;
124 ~Signature() = default;
125
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600126 /**
127 * @brief Constructs Signature.
128 * @param[in] imageDirPath - image path
129 * @param[in] signedConfPath - Path of public key
130 * hash function files
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600131 */
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600132 Signature(const fs::path& imageDirPath, const fs::path& signedConfPath);
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600133
134 /**
135 * @brief Image signature verification function.
136 * Verify the Manifest and public key file signature using the
137 * public keys available in the system first. After successful
138 * validation, continue the whole image files signature
139 * validation using the image specific public key and the
140 * hash function.
141 *
142 * @return true if signature verification was successful,
143 * false if not
144 */
145 bool verify();
146
147 private:
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600148 /**
149 * @brief Function used for system level file signature validation
Gunnar Millse11a2022018-03-23 12:04:48 -0500150 * of image specific publickey file and manifest file
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600151 * using the available public keys and hash functions
152 * in the system.
Gunnar Mills2bcba022018-04-08 15:02:04 -0500153 * Refer code-update documentation for more details.
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600154 */
155 bool systemLevelVerify();
156
157 /**
158 * @brief Return all key types stored in the BMC based on the
159 * public key and hashfunc files stored in the BMC.
160 *
161 * @return list
162 */
163 AvailableKeyTypes getAvailableKeyTypesFromSystem() const;
164
165 /**
166 * @brief Return public key and hash function file names for the
167 * corresponding key type
168 *
169 * @param[in] key - key type
170 * @return Pair of hash and public key file names
171 */
172 inline KeyHashPathPair getKeyHashFileNames(const Key_t& key) const;
173
174 /**
175 * @brief Verify the file signature using public key and hash function
176 *
177 * @param[in] - Image file path
178 * @param[in] - Signature file path
179 * @param[in] - Public key
180 * @param[in] - Hash function name
181 * @return true if signature verification was successful, false if not
182 */
183 bool verifyFile(const fs::path& file, const fs::path& signature,
184 const fs::path& publicKey, const std::string& hashFunc);
185
Jayanth Othayothfb6e1fc2018-02-21 05:43:20 -0600186 /**
187 * @brief Create RSA object from the public key
188 * @param[in] - publickey
189 * @param[out] - RSA Object.
190 */
191 inline RSA* createPublicRSA(const fs::path& publicKey);
192
193 /**
194 * @brief Memory map the file
195 * @param[in] - file path
196 * @param[in] - file size
197 * @param[out] - Custom Mmap address
198 */
199 CustomMap mapFile(const fs::path& path, size_t size);
200
201 /** @brief Directory where software images are placed*/
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600202 fs::path imageDirPath;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -0600203
204 /** @brief Path of public key and hash function files */
205 fs::path signedConfPath;
206
207 /** @brief key type defined in mainfest file */
208 Key_t keyType;
209
210 /** @brief Hash type defined in mainfest file */
211 Hash_t hashType;
Jayanth Othayoth9d7cd832018-02-21 05:12:39 -0600212};
213
214} // namespace image
215} // namespace software
216} // namespace phosphor