Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "image_verify.hpp" |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 4 | |
Lei YU | 1be8d50 | 2018-06-20 11:48:36 +0800 | [diff] [blame] | 5 | #include "images.hpp" |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 6 | #include "utils.hpp" |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 7 | #include "version.hpp" |
| 8 | |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 9 | #include <fcntl.h> |
| 10 | #include <openssl/err.h> |
| 11 | #include <sys/stat.h> |
| 12 | |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 13 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 14 | #include <phosphor-logging/elog.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 15 | #include <phosphor-logging/lg2.hpp> |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Common/error.hpp> |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 17 | |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 18 | #include <cassert> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 19 | #include <fstream> |
| 20 | #include <set> |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 21 | #include <system_error> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 22 | |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace software |
| 26 | { |
| 27 | namespace image |
| 28 | { |
| 29 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 30 | PHOSPHOR_LOG2_USING; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | using namespace phosphor::software::manager; |
| 33 | using InternalFailure = |
Adriana Kobylak | ce82de5 | 2024-01-16 13:56:38 -0600 | [diff] [blame] | 34 | sdbusplus::error::xyz::openbmc_project::common::InternalFailure; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 35 | |
| 36 | constexpr auto keyTypeTag = "KeyType"; |
| 37 | constexpr auto hashFunctionTag = "HashType"; |
| 38 | |
| 39 | Signature::Signature(const fs::path& imageDirPath, |
| 40 | const fs::path& signedConfPath) : |
Patrick Williams | fc33ba8 | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 41 | imageDirPath(imageDirPath), signedConfPath(signedConfPath) |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 42 | { |
| 43 | fs::path file(imageDirPath / MANIFEST_FILE_NAME); |
| 44 | |
| 45 | keyType = Version::getValue(file, keyTypeTag); |
| 46 | hashType = Version::getValue(file, hashFunctionTag); |
Lei YU | 6173a07 | 2022-05-23 19:18:57 +0800 | [diff] [blame] | 47 | |
| 48 | // Get purpose |
| 49 | auto purposeString = Version::getValue(file, "purpose"); |
| 50 | auto convertedPurpose = |
| 51 | sdbusplus::message::convert_from_string<VersionPurpose>(purposeString); |
| 52 | purpose = convertedPurpose.value_or(Version::VersionPurpose::Unknown); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | AvailableKeyTypes Signature::getAvailableKeyTypesFromSystem() const |
| 56 | { |
| 57 | AvailableKeyTypes keyTypes{}; |
| 58 | |
| 59 | // Find the path of all the files |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 60 | std::error_code ec; |
| 61 | if (!fs::is_directory(signedConfPath, ec)) |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 62 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 63 | error("Signed configuration path not found in the system: {ERROR_MSG}", |
| 64 | "ERROR_MSG", ec.message()); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 65 | elog<InternalFailure>(); |
| 66 | } |
| 67 | |
| 68 | // Look for all the hash and public key file names get the key value |
| 69 | // For example: |
| 70 | // /etc/activationdata/OpenBMC/publickey |
| 71 | // /etc/activationdata/OpenBMC/hashfunc |
| 72 | // /etc/activationdata/GA/publickey |
| 73 | // /etc/activationdata/GA/hashfunc |
| 74 | // Set will have OpenBMC, GA |
| 75 | |
| 76 | for (const auto& p : fs::recursive_directory_iterator(signedConfPath)) |
| 77 | { |
| 78 | if ((p.path().filename() == HASH_FILE_NAME) || |
| 79 | (p.path().filename() == PUBLICKEY_FILE_NAME)) |
| 80 | { |
| 81 | // extract the key types |
| 82 | // /etc/activationdata/OpenBMC/ -> get OpenBMC from the path |
| 83 | auto key = p.path().parent_path(); |
| 84 | keyTypes.insert(key.filename()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return keyTypes; |
| 89 | } |
| 90 | |
| 91 | inline KeyHashPathPair Signature::getKeyHashFileNames(const Key_t& key) const |
| 92 | { |
| 93 | fs::path hashpath(signedConfPath / key / HASH_FILE_NAME); |
| 94 | fs::path keyPath(signedConfPath / key / PUBLICKEY_FILE_NAME); |
| 95 | |
| 96 | return std::make_pair(std::move(hashpath), std::move(keyPath)); |
| 97 | } |
| 98 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 99 | bool Signature::verifyFullImage() |
| 100 | { |
| 101 | bool ret = true; |
Konstantin Aladyshev | 294991a | 2023-04-19 15:24:20 +0300 | [diff] [blame] | 102 | #ifdef WANT_SIGNATURE_VERIFY |
Lei YU | 6173a07 | 2022-05-23 19:18:57 +0800 | [diff] [blame] | 103 | // Only verify full image for BMC |
| 104 | if (purpose != VersionPurpose::BMC) |
| 105 | { |
| 106 | return ret; |
| 107 | } |
| 108 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 109 | std::vector<std::string> fullImages = { |
| 110 | fs::path(imageDirPath) / "image-bmc.sig", |
| 111 | fs::path(imageDirPath) / "image-hostfw.sig", |
| 112 | fs::path(imageDirPath) / "image-kernel.sig", |
| 113 | fs::path(imageDirPath) / "image-rofs.sig", |
| 114 | fs::path(imageDirPath) / "image-rwfs.sig", |
| 115 | fs::path(imageDirPath) / "image-u-boot.sig", |
| 116 | fs::path(imageDirPath) / "MANIFEST.sig", |
| 117 | fs::path(imageDirPath) / "publickey.sig"}; |
| 118 | |
| 119 | // Merge files |
| 120 | std::string tmpFullFile = "/tmp/image-full"; |
| 121 | utils::mergeFiles(fullImages, tmpFullFile); |
| 122 | |
| 123 | // Validate the full image files |
| 124 | fs::path pkeyFullFile(tmpFullFile); |
| 125 | |
| 126 | std::string imageFullSig = "image-full.sig"; |
| 127 | fs::path pkeyFullFileSig(imageDirPath / imageFullSig); |
| 128 | pkeyFullFileSig.replace_extension(SIGNATURE_FILE_EXT); |
| 129 | |
| 130 | // image specific publickey file name. |
| 131 | fs::path publicKeyFile(imageDirPath / PUBLICKEY_FILE_NAME); |
| 132 | |
| 133 | ret = verifyFile(pkeyFullFile, pkeyFullFileSig, publicKeyFile, hashType); |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 134 | |
| 135 | std::error_code ec; |
| 136 | fs::remove(tmpFullFile, ec); |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 137 | #endif |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 142 | bool Signature::verify() |
| 143 | { |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 144 | try |
| 145 | { |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 146 | bool valid; |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 147 | // Verify the MANIFEST and publickey file using available |
| 148 | // public keys and hash on the system. |
Pavithra Barithaya | 2d5704e | 2024-06-26 02:35:08 -0500 | [diff] [blame] | 149 | if (!systemLevelVerify()) |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 150 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 151 | error("System level Signature Validation failed"); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 155 | bool bmcFilesFound = false; |
Gunnar Mills | e11a202 | 2018-03-23 12:04:48 -0500 | [diff] [blame] | 156 | // image specific publickey file name. |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 157 | fs::path publicKeyFile(imageDirPath / PUBLICKEY_FILE_NAME); |
| 158 | |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 159 | // Record the images which are being updated |
| 160 | // First check and Validate for the fullimage, then check and Validate |
| 161 | // for images with partitions |
| 162 | std::vector<std::string> imageUpdateList = {bmcFullImages}; |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 163 | valid = checkAndVerifyImage(imageDirPath, publicKeyFile, |
| 164 | imageUpdateList, bmcFilesFound); |
| 165 | if (bmcFilesFound && !valid) |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 170 | if (!valid) |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 171 | { |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 172 | // Validate bmcImages |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 173 | imageUpdateList.clear(); |
| 174 | imageUpdateList.assign(bmcImages.begin(), bmcImages.end()); |
| 175 | valid = checkAndVerifyImage(imageDirPath, publicKeyFile, |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 176 | imageUpdateList, bmcFilesFound); |
| 177 | if (bmcFilesFound && !valid) |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 178 | { |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | } |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 182 | |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 183 | // Validate the optional image files. |
| 184 | auto optionalImages = getOptionalImages(); |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 185 | bool optionalFilesFound = false; |
| 186 | bool optionalImagesValid = false; |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 187 | for (const auto& optionalImage : optionalImages) |
| 188 | { |
| 189 | // Build Image File name |
| 190 | fs::path file(imageDirPath); |
| 191 | file /= optionalImage; |
| 192 | |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 193 | std::error_code ec; |
| 194 | if (fs::exists(file, ec)) |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 195 | { |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 196 | optionalFilesFound = true; |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 197 | // Build Signature File name |
| 198 | fs::path sigFile(file); |
Lei YU | 92c7e9e | 2021-05-20 16:32:28 +0800 | [diff] [blame] | 199 | sigFile += SIGNATURE_FILE_EXT; |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 200 | |
| 201 | // Verify the signature. |
Patrick Williams | fc33ba8 | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 202 | optionalImagesValid = |
| 203 | verifyFile(file, sigFile, publicKeyFile, hashType); |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 204 | if (!optionalImagesValid) |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 205 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 206 | error("Image file Signature Validation failed on {IMAGE}", |
| 207 | "IMAGE", optionalImage); |
Adriana Kobylak | 73609bb | 2020-06-18 15:05:40 -0500 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | } |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 212 | |
Pavithra Barithaya | 2d5704e | 2024-06-26 02:35:08 -0500 | [diff] [blame] | 213 | if (!verifyFullImage()) |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 214 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 215 | error("Image full file Signature Validation failed"); |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 219 | if (!bmcFilesFound && !optionalFilesFound) |
| 220 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 221 | error("Unable to find files to verify"); |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 222 | return false; |
| 223 | } |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 224 | |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 225 | // Either BMC images or optional images shall be valid |
| 226 | assert(valid || optionalImagesValid); |
| 227 | |
Manojkiran Eda | 19dd56b | 2024-06-17 14:29:52 +0530 | [diff] [blame] | 228 | debug("Successfully completed Signature validation."); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | catch (const InternalFailure& e) |
| 232 | { |
| 233 | return false; |
| 234 | } |
| 235 | catch (const std::exception& e) |
| 236 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 237 | error("Error during processing: {ERROR}", "ERROR", e); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool Signature::systemLevelVerify() |
| 243 | { |
| 244 | // Get available key types from the system. |
| 245 | auto keyTypes = getAvailableKeyTypesFromSystem(); |
| 246 | if (keyTypes.empty()) |
| 247 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 248 | error("Missing Signature configuration data in system"); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 249 | elog<InternalFailure>(); |
| 250 | } |
| 251 | |
| 252 | // Build publickey and its signature file name. |
| 253 | fs::path pkeyFile(imageDirPath / PUBLICKEY_FILE_NAME); |
| 254 | fs::path pkeyFileSig(pkeyFile); |
| 255 | pkeyFileSig.replace_extension(SIGNATURE_FILE_EXT); |
| 256 | |
| 257 | // Build manifest and its signature file name. |
| 258 | fs::path manifestFile(imageDirPath / MANIFEST_FILE_NAME); |
| 259 | fs::path manifestFileSig(manifestFile); |
| 260 | manifestFileSig.replace_extension(SIGNATURE_FILE_EXT); |
| 261 | |
| 262 | auto valid = false; |
| 263 | |
| 264 | // Verify the file signature with available key types |
| 265 | // public keys and hash function. |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 266 | // For any internal failure during the key/hash pair specific |
| 267 | // validation, should continue the validation with next |
| 268 | // available Key/hash pair. |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 269 | for (const auto& keyType : keyTypes) |
| 270 | { |
| 271 | auto keyHashPair = getKeyHashFileNames(keyType); |
| 272 | |
| 273 | auto hashFunc = Version::getValue(keyHashPair.first, hashFunctionTag); |
| 274 | |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 275 | try |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 276 | { |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 277 | // Verify manifest file signature |
| 278 | valid = verifyFile(manifestFile, manifestFileSig, |
| 279 | keyHashPair.second, hashFunc); |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 280 | if (valid) |
| 281 | { |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 282 | // Verify publickey file signature. |
| 283 | valid = verifyFile(pkeyFile, pkeyFileSig, keyHashPair.second, |
| 284 | hashFunc); |
| 285 | if (valid) |
| 286 | { |
| 287 | break; |
| 288 | } |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 289 | } |
| 290 | } |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 291 | catch (const InternalFailure& e) |
| 292 | { |
| 293 | valid = false; |
| 294 | } |
Jayanth Othayoth | 2ab9b10 | 2018-02-21 05:27:47 -0600 | [diff] [blame] | 295 | } |
| 296 | return valid; |
| 297 | } |
| 298 | |
| 299 | bool Signature::verifyFile(const fs::path& file, const fs::path& sigFile, |
| 300 | const fs::path& publicKey, |
| 301 | const std::string& hashFunc) |
| 302 | { |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 303 | // Check existence of the files in the system. |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 304 | std::error_code ec; |
| 305 | if (!(fs::exists(file, ec) && fs::exists(sigFile, ec))) |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 306 | { |
George Liu | 2851e0d | 2023-12-05 14:44:11 +0800 | [diff] [blame] | 307 | error("Failed to find the Data or signature file {PATH}", "PATH", file); |
| 308 | if (ec) |
| 309 | { |
| 310 | error("Error message: {ERROR_MSG}", "ERROR_MSG", ec.message()); |
| 311 | } |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 312 | elog<InternalFailure>(); |
| 313 | } |
| 314 | |
| 315 | // Create RSA. |
| 316 | auto publicRSA = createPublicRSA(publicKey); |
Patrick Williams | d75c869 | 2021-12-07 16:25:10 -0600 | [diff] [blame] | 317 | if (!publicRSA) |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 318 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 319 | error("Failed to create RSA from {PATH}", "PATH", publicKey); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 320 | elog<InternalFailure>(); |
| 321 | } |
| 322 | |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 323 | // Initializes a digest context. |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 324 | EVP_MD_CTX_Ptr rsaVerifyCtx(EVP_MD_CTX_new(), ::EVP_MD_CTX_free); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 325 | |
| 326 | // Adds all digest algorithms to the internal table |
| 327 | OpenSSL_add_all_digests(); |
| 328 | |
Gunnar Mills | 2bcba02 | 2018-04-08 15:02:04 -0500 | [diff] [blame] | 329 | // Create Hash structure. |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 330 | auto hashStruct = EVP_get_digestbyname(hashFunc.c_str()); |
| 331 | if (!hashStruct) |
| 332 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 333 | error("EVP_get_digestbynam: Unknown message digest: {HASH}", "HASH", |
| 334 | hashFunc); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 335 | elog<InternalFailure>(); |
| 336 | } |
| 337 | |
| 338 | auto result = EVP_DigestVerifyInit(rsaVerifyCtx.get(), nullptr, hashStruct, |
Patrick Williams | d75c869 | 2021-12-07 16:25:10 -0600 | [diff] [blame] | 339 | nullptr, publicRSA.get()); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 340 | |
| 341 | if (result <= 0) |
| 342 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 343 | error("Error ({RC}) occurred during EVP_DigestVerifyInit", "RC", |
| 344 | ERR_get_error()); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 345 | elog<InternalFailure>(); |
| 346 | } |
| 347 | |
| 348 | // Hash the data file and update the verification context |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 349 | auto size = fs::file_size(file, ec); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 350 | auto dataPtr = mapFile(file, size); |
| 351 | |
| 352 | result = EVP_DigestVerifyUpdate(rsaVerifyCtx.get(), dataPtr(), size); |
| 353 | if (result <= 0) |
| 354 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 355 | error("Error ({RC}) occurred during EVP_DigestVerifyUpdate", "RC", |
| 356 | ERR_get_error()); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 357 | elog<InternalFailure>(); |
| 358 | } |
| 359 | |
| 360 | // Verify the data with signature. |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 361 | size = fs::file_size(sigFile, ec); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 362 | auto signature = mapFile(sigFile, size); |
| 363 | |
| 364 | result = EVP_DigestVerifyFinal( |
| 365 | rsaVerifyCtx.get(), reinterpret_cast<unsigned char*>(signature()), |
| 366 | size); |
| 367 | |
| 368 | // Check the verification result. |
| 369 | if (result < 0) |
| 370 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 371 | error("Error ({RC}) occurred during EVP_DigestVerifyFinal", "RC", |
| 372 | ERR_get_error()); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 373 | elog<InternalFailure>(); |
| 374 | } |
| 375 | |
| 376 | if (result == 0) |
| 377 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 378 | error("EVP_DigestVerifyFinal:Signature validation failed on {PATH}", |
| 379 | "PATH", sigFile); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 380 | return false; |
| 381 | } |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | |
Patrick Williams | d75c869 | 2021-12-07 16:25:10 -0600 | [diff] [blame] | 385 | inline EVP_PKEY_Ptr Signature::createPublicRSA(const fs::path& publicKey) |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 386 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 387 | std::error_code ec; |
| 388 | auto size = fs::file_size(publicKey, ec); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 389 | |
| 390 | // Read public key file |
| 391 | auto data = mapFile(publicKey, size); |
| 392 | |
| 393 | BIO_MEM_Ptr keyBio(BIO_new_mem_buf(data(), -1), &::BIO_free); |
| 394 | if (keyBio.get() == nullptr) |
| 395 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 396 | error("Failed to create new BIO Memory buffer"); |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 397 | elog<InternalFailure>(); |
| 398 | } |
| 399 | |
Patrick Williams | d75c869 | 2021-12-07 16:25:10 -0600 | [diff] [blame] | 400 | return {PEM_read_bio_PUBKEY(keyBio.get(), nullptr, nullptr, nullptr), |
| 401 | &::EVP_PKEY_free}; |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | CustomMap Signature::mapFile(const fs::path& path, size_t size) |
| 405 | { |
Jayanth Othayoth | fb6e1fc | 2018-02-21 05:43:20 -0600 | [diff] [blame] | 406 | CustomFd fd(open(path.c_str(), O_RDONLY)); |
| 407 | |
| 408 | return CustomMap(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd(), 0), |
| 409 | size); |
| 410 | } |
| 411 | |
Patrick Williams | fc33ba8 | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 412 | bool Signature::checkAndVerifyImage( |
| 413 | const std::string& filePath, const std::string& publicKeyPath, |
| 414 | const std::vector<std::string>& imageList, bool& fileFound) |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 415 | { |
| 416 | bool valid = true; |
| 417 | |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 418 | fileFound = false; |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 419 | for (auto& bmcImage : imageList) |
| 420 | { |
| 421 | fs::path file(filePath); |
| 422 | file /= bmcImage; |
| 423 | |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 424 | std::error_code ec; |
| 425 | if (!fs::exists(file, ec)) |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 426 | { |
| 427 | valid = false; |
| 428 | break; |
| 429 | } |
Lei YU | 7ab55e2 | 2021-05-19 13:26:53 +0800 | [diff] [blame] | 430 | fileFound = true; |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 431 | |
| 432 | fs::path sigFile(file); |
Lei YU | 92c7e9e | 2021-05-20 16:32:28 +0800 | [diff] [blame] | 433 | sigFile += SIGNATURE_FILE_EXT; |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 434 | |
| 435 | // Verify the signature. |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 436 | valid = verifyFile(file, sigFile, publicKeyPath, hashType); |
Pavithra Barithaya | 2d5704e | 2024-06-26 02:35:08 -0500 | [diff] [blame] | 437 | if (!valid) |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 438 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 439 | error("Image file Signature Validation failed on {PATH}", "PATH", |
| 440 | bmcImage); |
Henry Tian | 574f94b | 2021-01-06 10:33:59 +0800 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return valid; |
| 446 | } |
Jayanth Othayoth | 9d7cd83 | 2018-02-21 05:12:39 -0600 | [diff] [blame] | 447 | } // namespace image |
| 448 | } // namespace software |
| 449 | } // namespace phosphor |