Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2020 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #include "password.hpp" |
| 17 | |
| 18 | #include "xyz/openbmc_project/BIOSConfig/Common/error.hpp" |
| 19 | #include "xyz/openbmc_project/Common/error.hpp" |
| 20 | |
| 21 | #include <boost/algorithm/hex.hpp> |
| 22 | #include <boost/asio.hpp> |
| 23 | #include <phosphor-logging/elog-errors.hpp> |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 24 | #include <phosphor-logging/lg2.hpp> |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 25 | #include <sdbusplus/asio/connection.hpp> |
| 26 | #include <sdbusplus/asio/object_server.hpp> |
| 27 | |
| 28 | #include <fstream> |
| 29 | #include <iostream> |
| 30 | |
| 31 | namespace bios_config_pwd |
| 32 | { |
| 33 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 34 | using namespace sdbusplus::xyz::openbmc_project::BIOSConfig::Common::Error; |
| 35 | |
yes | d0f034a | 2022-12-29 18:35:37 +0530 | [diff] [blame] | 36 | bool Password::compareDigest(const EVP_MD* digestFunc, size_t digestLen, |
| 37 | const std::array<uint8_t, maxHashSize>& expected, |
| 38 | const std::array<uint8_t, maxSeedSize>& seed, |
| 39 | const std::string& rawData) |
| 40 | { |
| 41 | std::vector<uint8_t> output(digestLen); |
| 42 | unsigned int hashLen = digestLen; |
| 43 | |
| 44 | if (!PKCS5_PBKDF2_HMAC(reinterpret_cast<const char*>(rawData.c_str()), |
| 45 | rawData.length() + 1, |
| 46 | reinterpret_cast<const unsigned char*>(seed.data()), |
| 47 | seed.size(), iterValue, digestFunc, hashLen, |
| 48 | output.data())) |
| 49 | { |
| 50 | lg2::error("Generate PKCS5_PBKDF2_HMAC Integrity Check Value failed"); |
| 51 | throw InternalFailure(); |
| 52 | } |
| 53 | |
| 54 | if (std::memcmp(output.data(), expected.data(), |
| 55 | output.size() * sizeof(uint8_t)) == 0) |
| 56 | { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 63 | bool Password::isMatch(const std::array<uint8_t, maxHashSize>& expected, |
| 64 | const std::array<uint8_t, maxSeedSize>& seed, |
George Liu | 616f922 | 2021-12-29 14:25:39 +0800 | [diff] [blame] | 65 | const std::string& rawData, const std::string& algo) |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 66 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 67 | lg2::error("isMatch"); |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 68 | |
| 69 | if (algo == "SHA256") |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 70 | { |
yes | d0f034a | 2022-12-29 18:35:37 +0530 | [diff] [blame] | 71 | return compareDigest(EVP_sha256(), SHA256_DIGEST_LENGTH, expected, seed, |
| 72 | rawData); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 73 | } |
yes | d0f034a | 2022-12-29 18:35:37 +0530 | [diff] [blame] | 74 | |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 75 | if (algo == "SHA384") |
| 76 | { |
yes | d0f034a | 2022-12-29 18:35:37 +0530 | [diff] [blame] | 77 | return compareDigest(EVP_sha384(), SHA384_DIGEST_LENGTH, expected, seed, |
| 78 | rawData); |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 79 | } |
| 80 | |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
yes | 8c22d07 | 2023-03-22 15:11:26 +0530 | [diff] [blame] | 84 | bool Password::verifyIntegrityCheck(std::string& newPassword, |
| 85 | std::array<uint8_t, maxSeedSize>& seed, |
| 86 | unsigned int mdLen, |
| 87 | const EVP_MD* digestFunc) |
| 88 | { |
| 89 | mNewPwdHash.fill(0); |
| 90 | |
| 91 | if (!PKCS5_PBKDF2_HMAC(reinterpret_cast<const char*>(newPassword.c_str()), |
| 92 | newPassword.length() + 1, |
| 93 | reinterpret_cast<const unsigned char*>(seed.data()), |
| 94 | seed.size(), iterValue, digestFunc, mdLen, |
| 95 | mNewPwdHash.data())) |
| 96 | { |
| 97 | lg2::error("Verify PKCS5_PBKDF2_HMAC Integrity Check failed"); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 104 | void Password::verifyPassword(std::string userName, std::string currentPassword, |
| 105 | std::string newPassword) |
| 106 | { |
| 107 | if (fs::exists(seedFile.c_str())) |
| 108 | { |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 109 | std::array<uint8_t, maxHashSize> orgUsrPwdHash; |
| 110 | std::array<uint8_t, maxHashSize> orgAdminPwdHash; |
| 111 | std::array<uint8_t, maxSeedSize> seed; |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 112 | std::string hashAlgo = ""; |
| 113 | try |
| 114 | { |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 115 | nlohmann::json json = nullptr; |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 116 | std::ifstream ifs(seedFile.c_str()); |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 117 | if (ifs.is_open()) |
| 118 | { |
| 119 | try |
| 120 | { |
| 121 | json = nlohmann::json::parse(ifs, nullptr, false); |
| 122 | } |
| 123 | catch (const nlohmann::json::parse_error& e) |
| 124 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 125 | lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", |
| 126 | e); |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 127 | throw InternalFailure(); |
| 128 | } |
| 129 | |
| 130 | if (json.is_discarded()) |
| 131 | { |
| 132 | return; |
| 133 | } |
| 134 | orgUsrPwdHash = json["UserPwdHash"]; |
| 135 | orgAdminPwdHash = json["AdminPwdHash"]; |
| 136 | seed = json["Seed"]; |
| 137 | hashAlgo = json["HashAlgo"]; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | return; |
| 142 | } |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 143 | } |
| 144 | catch (nlohmann::detail::exception& e) |
| 145 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 146 | lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", e); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 147 | throw InternalFailure(); |
| 148 | } |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 149 | if (userName == "AdminPassword") |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 150 | { |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 151 | if (!isMatch(orgAdminPwdHash, seed, currentPassword, hashAlgo)) |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 152 | { |
| 153 | throw InvalidCurrentPassword(); |
| 154 | } |
| 155 | } |
| 156 | else |
| 157 | { |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 158 | if (!isMatch(orgUsrPwdHash, seed, currentPassword, hashAlgo)) |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 159 | { |
| 160 | throw InvalidCurrentPassword(); |
| 161 | } |
| 162 | } |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 163 | if (hashAlgo == "SHA256") |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 164 | { |
yes | 8c22d07 | 2023-03-22 15:11:26 +0530 | [diff] [blame] | 165 | if (!verifyIntegrityCheck(newPassword, seed, 32, EVP_sha256())) |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 166 | { |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 167 | throw InternalFailure(); |
| 168 | } |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 169 | } |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 170 | if (hashAlgo == "SHA384") |
| 171 | { |
yes | 8c22d07 | 2023-03-22 15:11:26 +0530 | [diff] [blame] | 172 | if (!verifyIntegrityCheck(newPassword, seed, 48, EVP_sha384())) |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 173 | { |
| 174 | throw InternalFailure(); |
| 175 | } |
| 176 | } |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | throw InternalFailure(); |
| 180 | } |
| 181 | void Password::changePassword(std::string userName, std::string currentPassword, |
| 182 | std::string newPassword) |
| 183 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 184 | lg2::debug("BIOS config changePassword"); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 185 | verifyPassword(userName, currentPassword, newPassword); |
| 186 | |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 187 | std::ifstream fs(seedFile.c_str()); |
| 188 | nlohmann::json json = nullptr; |
| 189 | |
| 190 | if (fs.is_open()) |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 191 | { |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 192 | try |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 193 | { |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 194 | json = nlohmann::json::parse(fs, nullptr, false); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 195 | } |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 196 | catch (const nlohmann::json::parse_error& e) |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 197 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 198 | lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", e); |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 199 | throw InternalFailure(); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 200 | } |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 201 | |
| 202 | if (json.is_discarded()) |
| 203 | { |
| 204 | throw InternalFailure(); |
| 205 | } |
| 206 | json["AdminPwdHash"] = mNewPwdHash; |
| 207 | json["IsAdminPwdChanged"] = true; |
| 208 | |
| 209 | std::ofstream ofs(seedFile.c_str(), std::ios::out); |
| 210 | const auto& writeData = json.dump(); |
| 211 | ofs << writeData; |
| 212 | ofs.close(); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 213 | } |
Ayushi Smriti | 96e72ec | 2021-05-20 13:44:12 +0530 | [diff] [blame] | 214 | else |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 215 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 216 | lg2::debug("Cannot open file stream"); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 217 | throw InternalFailure(); |
| 218 | } |
| 219 | } |
| 220 | Password::Password(sdbusplus::asio::object_server& objectServer, |
| 221 | std::shared_ptr<sdbusplus::asio::connection>& systemBus) : |
| 222 | sdbusplus::xyz::openbmc_project::BIOSConfig::server::Password( |
| 223 | *systemBus, objectPathPwd), |
| 224 | objServer(objectServer), systemBus(systemBus) |
| 225 | { |
yes | 8de46ff | 2023-03-22 14:51:28 +0530 | [diff] [blame] | 226 | lg2::debug("BIOS config password is running"); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 227 | try |
| 228 | { |
| 229 | fs::path biosDir(BIOS_PERSIST_PATH); |
| 230 | fs::create_directories(biosDir); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 231 | seedFile = biosDir / biosSeedFile; |
| 232 | } |
| 233 | catch (const fs::filesystem_error& e) |
| 234 | { |
George Liu | 567a3cf | 2021-12-08 10:36:03 +0800 | [diff] [blame] | 235 | lg2::error("Failed to parse JSON file: {ERROR}", "ERROR", e); |
Kuiying Wang | 8f70621 | 2020-12-16 18:59:24 +0800 | [diff] [blame] | 236 | throw InternalFailure(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | } // namespace bios_config_pwd |