Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | |
| 17 | #include "passwd_mgr.hpp" |
| 18 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 19 | #include "file.hpp" |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 20 | #include "shadowlock.hpp" |
| 21 | |
| 22 | #include <openssl/hmac.h> |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 23 | #include <openssl/rand.h> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 24 | #include <openssl/sha.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 27 | #include <unistd.h> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 28 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 29 | #include <phosphor-logging/log.hpp> |
| 30 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 31 | #include <cerrno> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 32 | #include <cstring> |
| 33 | #include <fstream> |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 34 | #include <iomanip> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 35 | |
| 36 | namespace ipmi |
| 37 | { |
| 38 | |
| 39 | static const char* passwdFileName = "/etc/ipmi_pass"; |
| 40 | static const char* encryptKeyFileName = "/etc/key_file"; |
| 41 | static const size_t maxKeySize = 8; |
| 42 | |
Richard Marian Thomaiyar | 6ba8d31 | 2020-04-10 23:52:50 +0530 | [diff] [blame] | 43 | constexpr mode_t modeMask = |
| 44 | (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); |
| 45 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 46 | #define META_PASSWD_SIG "=OPENBMC=" |
| 47 | |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 48 | /* |
| 49 | * Meta data struct for encrypted password file |
| 50 | */ |
Richard Marian Thomaiyar | 48e5558 | 2018-12-20 15:58:04 +0530 | [diff] [blame] | 51 | struct MetaPassStruct |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 52 | { |
| 53 | char signature[10]; |
| 54 | unsigned char reseved[2]; |
Tim Lee | 65a9168 | 2022-11-30 17:14:13 +0800 | [diff] [blame] | 55 | size_t hashSize; |
| 56 | size_t ivSize; |
| 57 | size_t dataSize; |
| 58 | size_t padSize; |
| 59 | size_t macSize; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | using namespace phosphor::logging; |
| 63 | |
| 64 | PasswdMgr::PasswdMgr() |
| 65 | { |
Richard Marian Thomaiyar | 6ba8d31 | 2020-04-10 23:52:50 +0530 | [diff] [blame] | 66 | restrictFilesPermission(); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 67 | initPasswordMap(); |
| 68 | } |
| 69 | |
Richard Marian Thomaiyar | 6ba8d31 | 2020-04-10 23:52:50 +0530 | [diff] [blame] | 70 | void PasswdMgr::restrictFilesPermission(void) |
| 71 | { |
| 72 | struct stat st = {}; |
| 73 | // Restrict file permission to owner read & write |
| 74 | if (stat(passwdFileName, &st) == 0) |
| 75 | { |
| 76 | if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR)) |
| 77 | { |
| 78 | chmod(passwdFileName, S_IRUSR | S_IWUSR); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (stat(encryptKeyFileName, &st) == 0) |
| 83 | { |
| 84 | if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR)) |
| 85 | { |
| 86 | chmod(encryptKeyFileName, S_IRUSR | S_IWUSR); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 91 | SecureString PasswdMgr::getPasswdByUserName(const std::string& userName) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 92 | { |
| 93 | checkAndReload(); |
| 94 | auto iter = passwdMapList.find(userName); |
| 95 | if (iter == passwdMapList.end()) |
| 96 | { |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 97 | return SecureString(); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 98 | } |
| 99 | return iter->second; |
| 100 | } |
| 101 | |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 102 | int PasswdMgr::updateUserEntry(const std::string& userName, |
| 103 | const std::string& newUserName) |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 104 | { |
| 105 | std::time_t updatedTime = getUpdatedFileTime(); |
| 106 | // Check file time stamp to know passwdMapList is up-to-date. |
| 107 | // If not up-to-date, then updatePasswdSpecialFile will read and |
| 108 | // check the user entry existance. |
| 109 | if (fileLastUpdatedTime == updatedTime && updatedTime != -EIO) |
| 110 | { |
| 111 | if (passwdMapList.find(userName) == passwdMapList.end()) |
| 112 | { |
| 113 | log<level::DEBUG>("User not found"); |
| 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Write passwdMap to Encryted file |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 119 | if (updatePasswdSpecialFile(userName, newUserName) != 0) |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 120 | { |
| 121 | log<level::DEBUG>("Passwd file update failed"); |
| 122 | return -EIO; |
| 123 | } |
| 124 | |
| 125 | log<level::DEBUG>("Passwd file updated successfully"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 129 | void PasswdMgr::checkAndReload(void) |
| 130 | { |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 131 | std::time_t updatedTime = getUpdatedFileTime(); |
| 132 | if (fileLastUpdatedTime != updatedTime && updatedTime != -1) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 133 | { |
| 134 | log<level::DEBUG>("Reloading password map list"); |
| 135 | passwdMapList.clear(); |
| 136 | initPasswordMap(); |
| 137 | } |
| 138 | } |
| 139 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 140 | int PasswdMgr::encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher, |
| 141 | uint8_t* key, size_t keyLen, uint8_t* iv, |
| 142 | size_t ivLen, uint8_t* inBytes, |
| 143 | size_t inBytesLen, uint8_t* mac, |
| 144 | size_t* macLen, unsigned char* outBytes, |
| 145 | size_t* outBytesLen) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 146 | { |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 147 | if (cipher == NULL || key == NULL || iv == NULL || inBytes == NULL || |
| 148 | outBytes == NULL || mac == NULL || inBytesLen == 0 || |
| 149 | (size_t)EVP_CIPHER_key_length(cipher) > keyLen || |
| 150 | (size_t)EVP_CIPHER_iv_length(cipher) > ivLen) |
| 151 | { |
| 152 | log<level::DEBUG>("Error Invalid Inputs"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 153 | return -EINVAL; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 154 | } |
| 155 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 156 | if (!doEncrypt) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 157 | { |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 158 | // verify MAC before decrypting the data. |
| 159 | std::array<uint8_t, EVP_MAX_MD_SIZE> calMac; |
| 160 | size_t calMacLen = calMac.size(); |
| 161 | // calculate MAC for the encrypted message. |
| 162 | if (NULL == HMAC(EVP_sha256(), key, keyLen, inBytes, inBytesLen, |
| 163 | calMac.data(), |
| 164 | reinterpret_cast<unsigned int*>(&calMacLen))) |
| 165 | { |
| 166 | log<level::DEBUG>("Error: Failed to calculate MAC"); |
| 167 | return -EIO; |
| 168 | } |
| 169 | if (!((calMacLen == *macLen) && |
| 170 | (std::memcmp(calMac.data(), mac, calMacLen) == 0))) |
| 171 | { |
| 172 | log<level::DEBUG>("Authenticated message doesn't match"); |
| 173 | return -EBADMSG; |
| 174 | } |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ctx( |
| 178 | EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 179 | |
P Dheeraj Srujan Kumar | bf30c8d | 2021-07-20 04:06:37 +0530 | [diff] [blame] | 180 | if (!ctx) |
| 181 | { |
| 182 | log<level::DEBUG>("Error: EVP_CIPHER_CTX is NULL"); |
| 183 | return -ENOMEM; |
| 184 | } |
| 185 | |
P Dheeraj Srujan Kumar | a67caed | 2021-08-25 21:55:09 +0530 | [diff] [blame] | 186 | EVP_CIPHER_CTX_set_padding(ctx.get(), 1); |
| 187 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 188 | // Set key & IV |
| 189 | int retval = EVP_CipherInit_ex(ctx.get(), cipher, NULL, key, iv, |
| 190 | static_cast<int>(doEncrypt)); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 191 | if (!retval) |
| 192 | { |
| 193 | log<level::DEBUG>("EVP_CipherInit_ex failed", |
| 194 | entry("RET_VAL=%d", retval)); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 195 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | int outLen = 0, outEVPLen = 0; |
| 199 | if ((retval = EVP_CipherUpdate(ctx.get(), outBytes + outLen, &outEVPLen, |
| 200 | inBytes, inBytesLen))) |
| 201 | { |
| 202 | outLen += outEVPLen; |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 203 | if ((retval = EVP_CipherFinal(ctx.get(), outBytes + outLen, |
| 204 | &outEVPLen))) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 205 | { |
| 206 | outLen += outEVPLen; |
| 207 | *outBytesLen = outLen; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | log<level::DEBUG>("EVP_CipherFinal fails", |
| 212 | entry("RET_VAL=%d", retval)); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 213 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | log<level::DEBUG>("EVP_CipherUpdate fails", |
| 219 | entry("RET_VAL=%d", retval)); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 220 | return -EIO; |
| 221 | } |
| 222 | |
| 223 | if (doEncrypt) |
| 224 | { |
| 225 | // Create MAC for the encrypted message |
| 226 | if (NULL == HMAC(EVP_sha256(), key, keyLen, outBytes, *outBytesLen, mac, |
| 227 | reinterpret_cast<unsigned int*>(macLen))) |
| 228 | { |
| 229 | log<level::DEBUG>("Failed to create authentication"); |
| 230 | return -EIO; |
| 231 | } |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | void PasswdMgr::initPasswordMap(void) |
| 237 | { |
Andrew Geissler | 2f0ad74 | 2021-05-14 13:39:15 -0500 | [diff] [blame] | 238 | // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{}; |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 239 | SecureString dataBuf; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 240 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 241 | if (readPasswdFileData(dataBuf) != 0) |
| 242 | { |
| 243 | log<level::DEBUG>("Error in reading the encrypted pass file"); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (dataBuf.size() != 0) |
| 248 | { |
| 249 | // populate the user list with password |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 250 | char* outPtr = dataBuf.data(); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 251 | char* nToken = NULL; |
| 252 | char* linePtr = strtok_r(outPtr, "\n", &nToken); |
Patrick Venture | 51d0c40 | 2019-08-19 11:19:19 -0700 | [diff] [blame] | 253 | size_t lineSize = 0; |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 254 | while (linePtr != NULL) |
| 255 | { |
Patrick Venture | 51d0c40 | 2019-08-19 11:19:19 -0700 | [diff] [blame] | 256 | size_t userEPos = 0; |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 257 | SecureString lineStr(linePtr); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 258 | if ((userEPos = lineStr.find(":")) != std::string::npos) |
| 259 | { |
| 260 | lineSize = lineStr.size(); |
| 261 | passwdMapList.emplace( |
| 262 | lineStr.substr(0, userEPos), |
| 263 | lineStr.substr(userEPos + 1, lineSize - (userEPos + 1))); |
| 264 | } |
| 265 | linePtr = strtok_r(NULL, "\n", &nToken); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Update the timestamp |
| 270 | fileLastUpdatedTime = getUpdatedFileTime(); |
| 271 | return; |
| 272 | } |
| 273 | |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 274 | int PasswdMgr::readPasswdFileData(SecureString& outBytes) |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 275 | { |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 276 | std::array<uint8_t, maxKeySize> keyBuff; |
| 277 | std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary); |
| 278 | if (!keyFile.is_open()) |
| 279 | { |
| 280 | log<level::DEBUG>("Error in opening encryption key file"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 281 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 282 | } |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 283 | keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size()); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 284 | if (keyFile.fail()) |
| 285 | { |
| 286 | log<level::DEBUG>("Error in reading encryption key file"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 287 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | std::ifstream passwdFile(passwdFileName, std::ios::in | std::ios::binary); |
| 291 | if (!passwdFile.is_open()) |
| 292 | { |
| 293 | log<level::DEBUG>("Error in opening ipmi password file"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 294 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // calculate file size and read the data |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 298 | passwdFile.seekg(0, std::ios::end); |
| 299 | ssize_t fileSize = passwdFile.tellg(); |
| 300 | passwdFile.seekg(0, std::ios::beg); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 301 | std::vector<uint8_t> input(fileSize); |
| 302 | passwdFile.read(reinterpret_cast<char*>(input.data()), fileSize); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 303 | if (passwdFile.fail()) |
| 304 | { |
| 305 | log<level::DEBUG>("Error in reading encryption key file"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 306 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // verify the signature first |
Richard Marian Thomaiyar | 48e5558 | 2018-12-20 15:58:04 +0530 | [diff] [blame] | 310 | MetaPassStruct* metaData = reinterpret_cast<MetaPassStruct*>(input.data()); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 311 | if (std::strncmp(metaData->signature, META_PASSWD_SIG, |
| 312 | sizeof(metaData->signature))) |
| 313 | { |
| 314 | log<level::DEBUG>("Error signature mismatch in password file"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 315 | return -EBADMSG; |
| 316 | } |
| 317 | |
| 318 | size_t inBytesLen = metaData->dataSize + metaData->padSize; |
| 319 | // If data is empty i.e no password map then return success |
| 320 | if (inBytesLen == 0) |
| 321 | { |
| 322 | log<level::DEBUG>("Empty password file"); |
| 323 | return 0; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // compute the key needed to decrypt |
| 327 | std::array<uint8_t, EVP_MAX_KEY_LENGTH> key; |
| 328 | auto keyLen = key.size(); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 329 | if (NULL == HMAC(EVP_sha256(), keyBuff.data(), keyBuff.size(), |
| 330 | input.data() + sizeof(*metaData), metaData->hashSize, |
| 331 | key.data(), reinterpret_cast<unsigned int*>(&keyLen))) |
| 332 | { |
| 333 | log<level::DEBUG>("Failed to create MAC for authentication"); |
| 334 | return -EIO; |
| 335 | } |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 336 | |
| 337 | // decrypt the data |
| 338 | uint8_t* iv = input.data() + sizeof(*metaData) + metaData->hashSize; |
| 339 | size_t ivLen = metaData->ivSize; |
| 340 | uint8_t* inBytes = iv + ivLen; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 341 | uint8_t* mac = inBytes + inBytesLen; |
| 342 | size_t macLen = metaData->macSize; |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 343 | |
| 344 | size_t outBytesLen = 0; |
| 345 | // Resize to actual data size |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 346 | outBytes.resize(inBytesLen + EVP_MAX_BLOCK_LENGTH, '\0'); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 347 | if (encryptDecryptData(false, EVP_aes_128_cbc(), key.data(), keyLen, iv, |
| 348 | ivLen, inBytes, inBytesLen, mac, &macLen, |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 349 | reinterpret_cast<unsigned char*>(outBytes.data()), |
| 350 | &outBytesLen) != 0) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 351 | { |
| 352 | log<level::DEBUG>("Error in decryption"); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 353 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 354 | } |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 355 | // Resize the vector to outBytesLen |
| 356 | outBytes.resize(outBytesLen); |
| 357 | |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 358 | OPENSSL_cleanse(key.data(), keyLen); |
| 359 | OPENSSL_cleanse(iv, ivLen); |
| 360 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 364 | int PasswdMgr::updatePasswdSpecialFile(const std::string& userName, |
| 365 | const std::string& newUserName) |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 366 | { |
Andrew Geissler | 2f0ad74 | 2021-05-14 13:39:15 -0500 | [diff] [blame] | 367 | // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{}; |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 368 | |
| 369 | size_t bytesWritten = 0; |
| 370 | size_t inBytesLen = 0; |
| 371 | size_t isUsrFound = false; |
| 372 | const EVP_CIPHER* cipher = EVP_aes_128_cbc(); |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 373 | SecureString dataBuf; |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 374 | |
| 375 | // Read the encrypted file and get the file data |
| 376 | // Check user existance and return if not exist. |
| 377 | if (readPasswdFileData(dataBuf) != 0) |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 378 | { |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 379 | log<level::DEBUG>("Error in reading the encrypted pass file"); |
| 380 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 381 | } |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 382 | |
| 383 | if (dataBuf.size() != 0) |
| 384 | { |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 385 | inBytesLen = dataBuf.size() + newUserName.size() + |
| 386 | EVP_CIPHER_block_size(cipher); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 387 | } |
| 388 | |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 389 | SecureString inBytes(inBytesLen, '\0'); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 390 | if (inBytesLen != 0) |
| 391 | { |
| 392 | char* outPtr = reinterpret_cast<char*>(dataBuf.data()); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 393 | char* nToken = NULL; |
| 394 | char* linePtr = strtok_r(outPtr, "\n", &nToken); |
| 395 | while (linePtr != NULL) |
| 396 | { |
Patrick Venture | 51d0c40 | 2019-08-19 11:19:19 -0700 | [diff] [blame] | 397 | size_t userEPos = 0; |
| 398 | |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 399 | SecureString lineStr(linePtr); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 400 | if ((userEPos = lineStr.find(":")) != std::string::npos) |
| 401 | { |
| 402 | if (userName.compare(lineStr.substr(0, userEPos)) == 0) |
| 403 | { |
| 404 | isUsrFound = true; |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 405 | if (!newUserName.empty()) |
| 406 | { |
| 407 | bytesWritten += std::snprintf( |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 408 | &inBytes[0] + bytesWritten, |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 409 | (inBytesLen - bytesWritten), "%s%s\n", |
| 410 | newUserName.c_str(), |
| 411 | lineStr.substr(userEPos, lineStr.size()).data()); |
| 412 | } |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 413 | } |
| 414 | else |
| 415 | { |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 416 | bytesWritten += std::snprintf(&inBytes[0] + bytesWritten, |
| 417 | (inBytesLen - bytesWritten), |
| 418 | "%s\n", lineStr.data()); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | linePtr = strtok_r(NULL, "\n", &nToken); |
| 422 | } |
Richard Marian Thomaiyar | 161f20d | 2019-01-28 20:33:16 +0530 | [diff] [blame] | 423 | inBytesLen = bytesWritten; |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 424 | } |
| 425 | if (!isUsrFound) |
| 426 | { |
| 427 | log<level::DEBUG>("User doesn't exist"); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | // Read the key buff from key file |
| 432 | std::array<uint8_t, maxKeySize> keyBuff; |
| 433 | std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary); |
| 434 | if (!keyFile.good()) |
| 435 | { |
| 436 | log<level::DEBUG>("Error in opening encryption key file"); |
| 437 | return -EIO; |
| 438 | } |
| 439 | keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size()); |
| 440 | if (keyFile.fail()) |
| 441 | { |
| 442 | log<level::DEBUG>("Error in reading encryption key file"); |
| 443 | return -EIO; |
| 444 | } |
| 445 | keyFile.close(); |
| 446 | |
| 447 | // Read the original passwd file mode |
| 448 | struct stat st = {}; |
| 449 | if (stat(passwdFileName, &st) != 0) |
| 450 | { |
| 451 | log<level::DEBUG>("Error in getting password file fstat()"); |
| 452 | return -EIO; |
| 453 | } |
| 454 | |
| 455 | // Create temporary file for write |
| 456 | std::string pwdFile(passwdFileName); |
| 457 | std::vector<char> tempFileName(pwdFile.begin(), pwdFile.end()); |
| 458 | std::vector<char> fileTemplate = {'_', '_', 'X', 'X', 'X', |
| 459 | 'X', 'X', 'X', '\0'}; |
| 460 | tempFileName.insert(tempFileName.end(), fileTemplate.begin(), |
| 461 | fileTemplate.end()); |
| 462 | int fd = mkstemp((char*)tempFileName.data()); |
| 463 | if (fd == -1) |
| 464 | { |
| 465 | log<level::DEBUG>("Error creating temp file"); |
| 466 | return -EIO; |
| 467 | } |
| 468 | |
| 469 | std::string strTempFileName(tempFileName.data()); |
| 470 | // Open the temp file for writing from provided fd |
| 471 | // By "true", remove it at exit if still there. |
| 472 | // This is needed to cleanup the temp file at exception |
| 473 | phosphor::user::File temp(fd, strTempFileName, "w", true); |
| 474 | if ((temp)() == NULL) |
| 475 | { |
| 476 | close(fd); |
| 477 | log<level::DEBUG>("Error creating temp file"); |
| 478 | return -EIO; |
| 479 | } |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 480 | |
Vernon Mauery | b265455 | 2020-04-03 14:28:53 -0700 | [diff] [blame] | 481 | // Set the file mode as read-write for owner only |
| 482 | if (fchmod(fileno((temp)()), S_IRUSR | S_IWUSR) < 0) |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 483 | { |
| 484 | log<level::DEBUG>("Error setting fchmod for temp file"); |
| 485 | return -EIO; |
| 486 | } |
| 487 | |
| 488 | const EVP_MD* digest = EVP_sha256(); |
| 489 | size_t hashLen = EVP_MD_block_size(digest); |
| 490 | std::vector<uint8_t> hash(hashLen); |
| 491 | size_t ivLen = EVP_CIPHER_iv_length(cipher); |
| 492 | std::vector<uint8_t> iv(ivLen); |
| 493 | std::array<uint8_t, EVP_MAX_KEY_LENGTH> key; |
| 494 | size_t keyLen = key.size(); |
| 495 | std::array<uint8_t, EVP_MAX_MD_SIZE> mac; |
| 496 | size_t macLen = mac.size(); |
| 497 | |
| 498 | // Create random hash and generate hash key which will be used for |
| 499 | // encryption. |
| 500 | if (RAND_bytes(hash.data(), hashLen) != 1) |
| 501 | { |
| 502 | log<level::DEBUG>("Hash genertion failed, bailing out"); |
| 503 | return -EIO; |
| 504 | } |
| 505 | if (NULL == HMAC(digest, keyBuff.data(), keyBuff.size(), hash.data(), |
| 506 | hashLen, key.data(), |
| 507 | reinterpret_cast<unsigned int*>(&keyLen))) |
| 508 | { |
| 509 | log<level::DEBUG>("Failed to create MAC for authentication"); |
| 510 | return -EIO; |
| 511 | } |
| 512 | |
| 513 | // Generate IV values |
| 514 | if (RAND_bytes(iv.data(), ivLen) != 1) |
| 515 | { |
| 516 | log<level::DEBUG>("UV genertion failed, bailing out"); |
| 517 | return -EIO; |
| 518 | } |
| 519 | |
| 520 | // Encrypt the input data |
| 521 | std::vector<uint8_t> outBytes(inBytesLen + EVP_MAX_BLOCK_LENGTH); |
| 522 | size_t outBytesLen = 0; |
| 523 | if (inBytesLen != 0) |
| 524 | { |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 525 | if (encryptDecryptData( |
| 526 | true, EVP_aes_128_cbc(), key.data(), keyLen, iv.data(), ivLen, |
| 527 | reinterpret_cast<unsigned char*>(inBytes.data()), inBytesLen, |
| 528 | mac.data(), &macLen, outBytes.data(), &outBytesLen) != 0) |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 529 | { |
| 530 | log<level::DEBUG>("Error while encrypting the data"); |
| 531 | return -EIO; |
| 532 | } |
| 533 | outBytes[outBytesLen] = 0; |
| 534 | } |
| 535 | OPENSSL_cleanse(key.data(), keyLen); |
| 536 | |
| 537 | // Update the meta password structure. |
Richard Marian Thomaiyar | 48e5558 | 2018-12-20 15:58:04 +0530 | [diff] [blame] | 538 | MetaPassStruct metaData = {META_PASSWD_SIG, {0, 0}, 0, 0, 0, 0, 0}; |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 539 | metaData.hashSize = hashLen; |
| 540 | metaData.ivSize = ivLen; |
| 541 | metaData.dataSize = bytesWritten; |
| 542 | metaData.padSize = outBytesLen - bytesWritten; |
| 543 | metaData.macSize = macLen; |
| 544 | |
| 545 | if (fwrite(&metaData, 1, sizeof(metaData), (temp)()) != sizeof(metaData)) |
| 546 | { |
| 547 | log<level::DEBUG>("Error in writing meta data"); |
| 548 | return -EIO; |
| 549 | } |
| 550 | |
| 551 | if (fwrite(&hash[0], 1, hashLen, (temp)()) != hashLen) |
| 552 | { |
| 553 | log<level::DEBUG>("Error in writing hash data"); |
| 554 | return -EIO; |
| 555 | } |
| 556 | |
| 557 | if (fwrite(&iv[0], 1, ivLen, (temp)()) != ivLen) |
| 558 | { |
| 559 | log<level::DEBUG>("Error in writing IV data"); |
| 560 | return -EIO; |
| 561 | } |
| 562 | |
| 563 | if (fwrite(&outBytes[0], 1, outBytesLen, (temp)()) != outBytesLen) |
| 564 | { |
| 565 | log<level::DEBUG>("Error in writing encrypted data"); |
| 566 | return -EIO; |
| 567 | } |
| 568 | |
| 569 | if (fwrite(&mac[0], 1, macLen, (temp)()) != macLen) |
| 570 | { |
| 571 | log<level::DEBUG>("Error in writing MAC data"); |
| 572 | return -EIO; |
| 573 | } |
| 574 | |
| 575 | if (fflush((temp)())) |
| 576 | { |
| 577 | log<level::DEBUG>( |
| 578 | "File fflush error while writing entries to special file"); |
| 579 | return -EIO; |
| 580 | } |
| 581 | |
| 582 | OPENSSL_cleanse(iv.data(), ivLen); |
| 583 | |
| 584 | // Rename the tmp file to actual file |
| 585 | if (std::rename(strTempFileName.data(), passwdFileName) != 0) |
| 586 | { |
| 587 | log<level::DEBUG>("Failed to rename tmp file to ipmi-pass"); |
| 588 | return -EIO; |
| 589 | } |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | std::time_t PasswdMgr::getUpdatedFileTime() |
| 595 | { |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 596 | struct stat fileStat = {}; |
| 597 | if (stat(passwdFileName, &fileStat) != 0) |
| 598 | { |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 599 | log<level::DEBUG>("Error - Getting passwd file time stamp"); |
| 600 | return -EIO; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 601 | } |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 602 | return fileStat.st_mtime; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | } // namespace ipmi |