Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 1 | #pragma once |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 2 | #include "openssl_alloc.hpp" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 3 | |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 4 | #include <openssl/evp.h> |
| 5 | #include <openssl/pem.h> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 6 | #include <openssl/rsa.h> |
| 7 | #include <sys/mman.h> |
| 8 | #include <unistd.h> |
| 9 | |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 10 | #include <filesystem> |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 11 | #include <set> |
Andrew Geissler | 9155b71 | 2020-05-16 13:04:44 -0500 | [diff] [blame] | 12 | #include <string> |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 13 | #include <vector> |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace software |
| 18 | { |
| 19 | namespace image |
| 20 | { |
| 21 | |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 22 | namespace fs = std::filesystem; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 23 | using Key_t = std::string; |
| 24 | using Hash_t = std::string; |
| 25 | using PublicKeyPath = fs::path; |
| 26 | using HashFilePath = fs::path; |
| 27 | using KeyHashPathPair = std::pair<HashFilePath, PublicKeyPath>; |
| 28 | using AvailableKeyTypes = std::set<Key_t>; |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 29 | |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 30 | // RAII support for openSSL functions. |
| 31 | using BIO_MEM_Ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>; |
| 32 | using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; |
| 33 | using EVP_MD_CTX_Ptr = |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 34 | std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 35 | |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 36 | /** @struct CustomFd |
| 37 | * |
| 38 | * RAII wrapper for file descriptor. |
| 39 | */ |
| 40 | struct CustomFd |
| 41 | { |
| 42 | public: |
| 43 | CustomFd() = delete; |
| 44 | CustomFd(const CustomFd&) = delete; |
| 45 | CustomFd& operator=(const CustomFd&) = delete; |
| 46 | CustomFd(CustomFd&&) = default; |
| 47 | CustomFd& operator=(CustomFd&&) = default; |
| 48 | /** @brief Saves File descriptor and uses it to do file operation |
| 49 | * |
| 50 | * @param[in] fd - File descriptor |
| 51 | */ |
| 52 | CustomFd(int fd) : fd(fd) |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 53 | {} |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 54 | |
| 55 | ~CustomFd() |
| 56 | { |
| 57 | if (fd >= 0) |
| 58 | { |
| 59 | close(fd); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | int operator()() const |
| 64 | { |
| 65 | return fd; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | /** @brief File descriptor */ |
| 70 | int fd = -1; |
| 71 | }; |
| 72 | |
| 73 | /** @struct CustomMap |
| 74 | * |
| 75 | * RAII wrapper for mmap. |
| 76 | */ |
| 77 | struct CustomMap |
| 78 | { |
| 79 | private: |
| 80 | /** @brief starting address of the map */ |
| 81 | void* addr; |
| 82 | |
| 83 | /** @brief length of the mapping */ |
| 84 | size_t length; |
| 85 | |
| 86 | public: |
| 87 | CustomMap() = delete; |
| 88 | CustomMap(const CustomMap&) = delete; |
| 89 | CustomMap& operator=(const CustomMap&) = delete; |
| 90 | CustomMap(CustomMap&&) = default; |
| 91 | CustomMap& operator=(CustomMap&&) = default; |
| 92 | |
| 93 | /** @brief Saves starting address of the map and |
| 94 | * and length of the file. |
| 95 | * @param[in] addr - Starting address of the map |
| 96 | * @param[in] length - length of the map |
| 97 | */ |
| 98 | CustomMap(void* addr, size_t length) : addr(addr), length(length) |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 99 | {} |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 100 | |
| 101 | ~CustomMap() |
| 102 | { |
| 103 | munmap(addr, length); |
| 104 | } |
| 105 | |
| 106 | void* operator()() const |
| 107 | { |
| 108 | return addr; |
| 109 | } |
| 110 | }; |
| 111 | |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 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 | */ |
| 117 | class 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 Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 127 | /** |
| 128 | * @brief Constructs Signature. |
| 129 | * @param[in] imageDirPath - image path |
| 130 | * @param[in] signedConfPath - Path of public key |
| 131 | * hash function files |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 132 | */ |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 133 | Signature(const fs::path& imageDirPath, const fs::path& signedConfPath); |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 134 | |
| 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 Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 149 | /** |
| 150 | * @brief Function used for system level file signature validation |
Gunnar Mills | e11a202 | 2018-03-23 12:04:48 -0500 | [diff] [blame] | 151 | * of image specific publickey file and manifest file |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 152 | * using the available public keys and hash functions |
| 153 | * in the system. |
Gunnar Mills | 2bcba02 | 2018-04-08 15:02:04 -0500 | [diff] [blame] | 154 | * Refer code-update documentation for more details. |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 155 | */ |
| 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 Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 187 | /** |
| 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 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 202 | /** |
| 203 | * @brief Verify the full file signature using public key and hash function |
| 204 | * |
| 205 | * @return true if signature verification was successful, false if not |
| 206 | */ |
| 207 | bool verifyFullImage(); |
| 208 | |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 209 | /** @brief Directory where software images are placed*/ |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 210 | fs::path imageDirPath; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 211 | |
| 212 | /** @brief Path of public key and hash function files */ |
| 213 | fs::path signedConfPath; |
| 214 | |
| 215 | /** @brief key type defined in mainfest file */ |
| 216 | Key_t keyType; |
| 217 | |
| 218 | /** @brief Hash type defined in mainfest file */ |
| 219 | Hash_t hashType; |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 220 | |
| 221 | /** @brief Check and Verify the required image files |
| 222 | * |
| 223 | * @param[in] filePath - BMC tarball file path |
| 224 | * @param[in] publicKeyPath - publicKey file Path |
| 225 | * @param[in] imageList - Image filenames included in the BMC tarball |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 226 | * @param[out] fileFound - Indicate if the file to verify is found or not |
| 227 | * |
| 228 | * @return true if all image files are found in BMC tarball and |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 229 | * Verify Sucess false if one of image files is missing |
| 230 | */ |
| 231 | bool checkAndVerifyImage(const std::string& filePath, |
| 232 | const std::string& publicKeyPath, |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 233 | const std::vector<std::string>& imageList, |
| 234 | bool& fileFound); |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | } // namespace image |
| 238 | } // namespace software |
| 239 | } // namespace phosphor |