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