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