14654d99fSRichard Marian Thomaiyar /* 24654d99fSRichard Marian Thomaiyar // Copyright (c) 2018 Intel Corporation 34654d99fSRichard Marian Thomaiyar // 44654d99fSRichard Marian Thomaiyar // Licensed under the Apache License, Version 2.0 (the "License"); 54654d99fSRichard Marian Thomaiyar // you may not use this file except in compliance with the License. 64654d99fSRichard Marian Thomaiyar // You may obtain a copy of the License at 74654d99fSRichard Marian Thomaiyar // 84654d99fSRichard Marian Thomaiyar // http://www.apache.org/licenses/LICENSE-2.0 94654d99fSRichard Marian Thomaiyar // 104654d99fSRichard Marian Thomaiyar // Unless required by applicable law or agreed to in writing, software 114654d99fSRichard Marian Thomaiyar // distributed under the License is distributed on an "AS IS" BASIS, 124654d99fSRichard Marian Thomaiyar // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134654d99fSRichard Marian Thomaiyar // See the License for the specific language governing permissions and 144654d99fSRichard Marian Thomaiyar // limitations under the License. 154654d99fSRichard Marian Thomaiyar */ 164654d99fSRichard Marian Thomaiyar 174654d99fSRichard Marian Thomaiyar #include "passwd_mgr.hpp" 184654d99fSRichard Marian Thomaiyar 19b29b5ab3SAppaRao Puli #include "file.hpp" 204654d99fSRichard Marian Thomaiyar #include "shadowlock.hpp" 214654d99fSRichard Marian Thomaiyar 224654d99fSRichard Marian Thomaiyar #include <openssl/hmac.h> 23b29b5ab3SAppaRao Puli #include <openssl/rand.h> 244654d99fSRichard Marian Thomaiyar #include <openssl/sha.h> 254654d99fSRichard Marian Thomaiyar #include <string.h> 264654d99fSRichard Marian Thomaiyar #include <sys/stat.h> 27b29b5ab3SAppaRao Puli #include <unistd.h> 284654d99fSRichard Marian Thomaiyar 29b29b5ab3SAppaRao Puli #include <cerrno> 304654d99fSRichard Marian Thomaiyar #include <cstring> 314654d99fSRichard Marian Thomaiyar #include <fstream> 32b29b5ab3SAppaRao Puli #include <iomanip> 334654d99fSRichard Marian Thomaiyar #include <phosphor-logging/log.hpp> 344654d99fSRichard Marian Thomaiyar 354654d99fSRichard Marian Thomaiyar namespace ipmi 364654d99fSRichard Marian Thomaiyar { 374654d99fSRichard Marian Thomaiyar 384654d99fSRichard Marian Thomaiyar static const char* passwdFileName = "/etc/ipmi_pass"; 394654d99fSRichard Marian Thomaiyar static const char* encryptKeyFileName = "/etc/key_file"; 404654d99fSRichard Marian Thomaiyar static const size_t maxKeySize = 8; 414654d99fSRichard Marian Thomaiyar 426ba8d315SRichard Marian Thomaiyar constexpr mode_t modeMask = 436ba8d315SRichard Marian Thomaiyar (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); 446ba8d315SRichard Marian Thomaiyar 45b29b5ab3SAppaRao Puli #define META_PASSWD_SIG "=OPENBMC=" 46b29b5ab3SAppaRao Puli 474654d99fSRichard Marian Thomaiyar /* 484654d99fSRichard Marian Thomaiyar * Meta data struct for encrypted password file 494654d99fSRichard Marian Thomaiyar */ 5048e55585SRichard Marian Thomaiyar struct MetaPassStruct 514654d99fSRichard Marian Thomaiyar { 524654d99fSRichard Marian Thomaiyar char signature[10]; 534654d99fSRichard Marian Thomaiyar unsigned char reseved[2]; 544654d99fSRichard Marian Thomaiyar size_t hashSize; 554654d99fSRichard Marian Thomaiyar size_t ivSize; 564654d99fSRichard Marian Thomaiyar size_t dataSize; 574654d99fSRichard Marian Thomaiyar size_t padSize; 584654d99fSRichard Marian Thomaiyar size_t macSize; 594654d99fSRichard Marian Thomaiyar }; 604654d99fSRichard Marian Thomaiyar 614654d99fSRichard Marian Thomaiyar using namespace phosphor::logging; 624654d99fSRichard Marian Thomaiyar 634654d99fSRichard Marian Thomaiyar PasswdMgr::PasswdMgr() 644654d99fSRichard Marian Thomaiyar { 656ba8d315SRichard Marian Thomaiyar restrictFilesPermission(); 664654d99fSRichard Marian Thomaiyar initPasswordMap(); 674654d99fSRichard Marian Thomaiyar } 684654d99fSRichard Marian Thomaiyar 696ba8d315SRichard Marian Thomaiyar void PasswdMgr::restrictFilesPermission(void) 706ba8d315SRichard Marian Thomaiyar { 716ba8d315SRichard Marian Thomaiyar struct stat st = {}; 726ba8d315SRichard Marian Thomaiyar // Restrict file permission to owner read & write 736ba8d315SRichard Marian Thomaiyar if (stat(passwdFileName, &st) == 0) 746ba8d315SRichard Marian Thomaiyar { 756ba8d315SRichard Marian Thomaiyar if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR)) 766ba8d315SRichard Marian Thomaiyar { 776ba8d315SRichard Marian Thomaiyar chmod(passwdFileName, S_IRUSR | S_IWUSR); 786ba8d315SRichard Marian Thomaiyar } 796ba8d315SRichard Marian Thomaiyar } 806ba8d315SRichard Marian Thomaiyar 816ba8d315SRichard Marian Thomaiyar if (stat(encryptKeyFileName, &st) == 0) 826ba8d315SRichard Marian Thomaiyar { 836ba8d315SRichard Marian Thomaiyar if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR)) 846ba8d315SRichard Marian Thomaiyar { 856ba8d315SRichard Marian Thomaiyar chmod(encryptKeyFileName, S_IRUSR | S_IWUSR); 866ba8d315SRichard Marian Thomaiyar } 876ba8d315SRichard Marian Thomaiyar } 886ba8d315SRichard Marian Thomaiyar } 896ba8d315SRichard Marian Thomaiyar 904654d99fSRichard Marian Thomaiyar std::string PasswdMgr::getPasswdByUserName(const std::string& userName) 914654d99fSRichard Marian Thomaiyar { 924654d99fSRichard Marian Thomaiyar checkAndReload(); 934654d99fSRichard Marian Thomaiyar auto iter = passwdMapList.find(userName); 944654d99fSRichard Marian Thomaiyar if (iter == passwdMapList.end()) 954654d99fSRichard Marian Thomaiyar { 964654d99fSRichard Marian Thomaiyar return std::string(); 974654d99fSRichard Marian Thomaiyar } 984654d99fSRichard Marian Thomaiyar return iter->second; 994654d99fSRichard Marian Thomaiyar } 1004654d99fSRichard Marian Thomaiyar 10142bed64dSRichard Marian Thomaiyar int PasswdMgr::updateUserEntry(const std::string& userName, 10242bed64dSRichard Marian Thomaiyar const std::string& newUserName) 103b29b5ab3SAppaRao Puli { 104b29b5ab3SAppaRao Puli std::time_t updatedTime = getUpdatedFileTime(); 105b29b5ab3SAppaRao Puli // Check file time stamp to know passwdMapList is up-to-date. 106b29b5ab3SAppaRao Puli // If not up-to-date, then updatePasswdSpecialFile will read and 107b29b5ab3SAppaRao Puli // check the user entry existance. 108b29b5ab3SAppaRao Puli if (fileLastUpdatedTime == updatedTime && updatedTime != -EIO) 109b29b5ab3SAppaRao Puli { 110b29b5ab3SAppaRao Puli if (passwdMapList.find(userName) == passwdMapList.end()) 111b29b5ab3SAppaRao Puli { 112b29b5ab3SAppaRao Puli log<level::DEBUG>("User not found"); 113b29b5ab3SAppaRao Puli return 0; 114b29b5ab3SAppaRao Puli } 115b29b5ab3SAppaRao Puli } 116b29b5ab3SAppaRao Puli 117b29b5ab3SAppaRao Puli // Write passwdMap to Encryted file 11842bed64dSRichard Marian Thomaiyar if (updatePasswdSpecialFile(userName, newUserName) != 0) 119b29b5ab3SAppaRao Puli { 120b29b5ab3SAppaRao Puli log<level::DEBUG>("Passwd file update failed"); 121b29b5ab3SAppaRao Puli return -EIO; 122b29b5ab3SAppaRao Puli } 123b29b5ab3SAppaRao Puli 124b29b5ab3SAppaRao Puli log<level::DEBUG>("Passwd file updated successfully"); 125b29b5ab3SAppaRao Puli return 0; 126b29b5ab3SAppaRao Puli } 127b29b5ab3SAppaRao Puli 1284654d99fSRichard Marian Thomaiyar void PasswdMgr::checkAndReload(void) 1294654d99fSRichard Marian Thomaiyar { 130b29b5ab3SAppaRao Puli std::time_t updatedTime = getUpdatedFileTime(); 131b29b5ab3SAppaRao Puli if (fileLastUpdatedTime != updatedTime && updatedTime != -1) 1324654d99fSRichard Marian Thomaiyar { 1334654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Reloading password map list"); 1344654d99fSRichard Marian Thomaiyar passwdMapList.clear(); 1354654d99fSRichard Marian Thomaiyar initPasswordMap(); 1364654d99fSRichard Marian Thomaiyar } 1374654d99fSRichard Marian Thomaiyar } 1384654d99fSRichard Marian Thomaiyar 139b29b5ab3SAppaRao Puli int PasswdMgr::encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher, 140b29b5ab3SAppaRao Puli uint8_t* key, size_t keyLen, uint8_t* iv, 141b29b5ab3SAppaRao Puli size_t ivLen, uint8_t* inBytes, 142b29b5ab3SAppaRao Puli size_t inBytesLen, uint8_t* mac, 143b29b5ab3SAppaRao Puli size_t* macLen, unsigned char* outBytes, 144b29b5ab3SAppaRao Puli size_t* outBytesLen) 1454654d99fSRichard Marian Thomaiyar { 1464654d99fSRichard Marian Thomaiyar if (cipher == NULL || key == NULL || iv == NULL || inBytes == NULL || 1474654d99fSRichard Marian Thomaiyar outBytes == NULL || mac == NULL || inBytesLen == 0 || 1484654d99fSRichard Marian Thomaiyar (size_t)EVP_CIPHER_key_length(cipher) > keyLen || 1494654d99fSRichard Marian Thomaiyar (size_t)EVP_CIPHER_iv_length(cipher) > ivLen) 1504654d99fSRichard Marian Thomaiyar { 1514654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Error Invalid Inputs"); 152b29b5ab3SAppaRao Puli return -EINVAL; 1534654d99fSRichard Marian Thomaiyar } 1544654d99fSRichard Marian Thomaiyar 155b29b5ab3SAppaRao Puli if (!doEncrypt) 156b29b5ab3SAppaRao Puli { 157b29b5ab3SAppaRao Puli // verify MAC before decrypting the data. 1584654d99fSRichard Marian Thomaiyar std::array<uint8_t, EVP_MAX_MD_SIZE> calMac; 1594654d99fSRichard Marian Thomaiyar size_t calMacLen = calMac.size(); 1604654d99fSRichard Marian Thomaiyar // calculate MAC for the encrypted message. 1614654d99fSRichard Marian Thomaiyar if (NULL == HMAC(EVP_sha256(), key, keyLen, inBytes, inBytesLen, 1624654d99fSRichard Marian Thomaiyar calMac.data(), 1634654d99fSRichard Marian Thomaiyar reinterpret_cast<unsigned int*>(&calMacLen))) 1644654d99fSRichard Marian Thomaiyar { 1654654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Error: Failed to calculate MAC"); 166b29b5ab3SAppaRao Puli return -EIO; 1674654d99fSRichard Marian Thomaiyar } 168b29b5ab3SAppaRao Puli if (!((calMacLen == *macLen) && 1694654d99fSRichard Marian Thomaiyar (std::memcmp(calMac.data(), mac, calMacLen) == 0))) 1704654d99fSRichard Marian Thomaiyar { 1714654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Authenticated message doesn't match"); 172b29b5ab3SAppaRao Puli return -EBADMSG; 173b29b5ab3SAppaRao Puli } 1744654d99fSRichard Marian Thomaiyar } 1754654d99fSRichard Marian Thomaiyar 1764654d99fSRichard Marian Thomaiyar std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ctx( 1774654d99fSRichard Marian Thomaiyar EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free); 1784654d99fSRichard Marian Thomaiyar EVP_CIPHER_CTX_set_padding(ctx.get(), 1); 1794654d99fSRichard Marian Thomaiyar 180*bf30c8d3SP Dheeraj Srujan Kumar if (!ctx) 181*bf30c8d3SP Dheeraj Srujan Kumar { 182*bf30c8d3SP Dheeraj Srujan Kumar log<level::DEBUG>("Error: EVP_CIPHER_CTX is NULL"); 183*bf30c8d3SP Dheeraj Srujan Kumar return -ENOMEM; 184*bf30c8d3SP Dheeraj Srujan Kumar } 185*bf30c8d3SP Dheeraj Srujan Kumar 186b29b5ab3SAppaRao Puli // Set key & IV 187b29b5ab3SAppaRao Puli int retval = EVP_CipherInit_ex(ctx.get(), cipher, NULL, key, iv, 188b29b5ab3SAppaRao Puli static_cast<int>(doEncrypt)); 1894654d99fSRichard Marian Thomaiyar if (!retval) 1904654d99fSRichard Marian Thomaiyar { 1914654d99fSRichard Marian Thomaiyar log<level::DEBUG>("EVP_CipherInit_ex failed", 1924654d99fSRichard Marian Thomaiyar entry("RET_VAL=%d", retval)); 193b29b5ab3SAppaRao Puli return -EIO; 1944654d99fSRichard Marian Thomaiyar } 1954654d99fSRichard Marian Thomaiyar 1964654d99fSRichard Marian Thomaiyar int outLen = 0, outEVPLen = 0; 1974654d99fSRichard Marian Thomaiyar if ((retval = EVP_CipherUpdate(ctx.get(), outBytes + outLen, &outEVPLen, 1984654d99fSRichard Marian Thomaiyar inBytes, inBytesLen))) 1994654d99fSRichard Marian Thomaiyar { 2004654d99fSRichard Marian Thomaiyar outLen += outEVPLen; 2014654d99fSRichard Marian Thomaiyar if ((retval = 2024654d99fSRichard Marian Thomaiyar EVP_CipherFinal(ctx.get(), outBytes + outLen, &outEVPLen))) 2034654d99fSRichard Marian Thomaiyar { 2044654d99fSRichard Marian Thomaiyar outLen += outEVPLen; 2054654d99fSRichard Marian Thomaiyar *outBytesLen = outLen; 2064654d99fSRichard Marian Thomaiyar } 2074654d99fSRichard Marian Thomaiyar else 2084654d99fSRichard Marian Thomaiyar { 2094654d99fSRichard Marian Thomaiyar log<level::DEBUG>("EVP_CipherFinal fails", 2104654d99fSRichard Marian Thomaiyar entry("RET_VAL=%d", retval)); 211b29b5ab3SAppaRao Puli return -EIO; 2124654d99fSRichard Marian Thomaiyar } 2134654d99fSRichard Marian Thomaiyar } 2144654d99fSRichard Marian Thomaiyar else 2154654d99fSRichard Marian Thomaiyar { 2164654d99fSRichard Marian Thomaiyar log<level::DEBUG>("EVP_CipherUpdate fails", 2174654d99fSRichard Marian Thomaiyar entry("RET_VAL=%d", retval)); 218b29b5ab3SAppaRao Puli return -EIO; 219b29b5ab3SAppaRao Puli } 220b29b5ab3SAppaRao Puli 221b29b5ab3SAppaRao Puli if (doEncrypt) 222b29b5ab3SAppaRao Puli { 223b29b5ab3SAppaRao Puli // Create MAC for the encrypted message 224b29b5ab3SAppaRao Puli if (NULL == HMAC(EVP_sha256(), key, keyLen, outBytes, *outBytesLen, mac, 225b29b5ab3SAppaRao Puli reinterpret_cast<unsigned int*>(macLen))) 226b29b5ab3SAppaRao Puli { 227b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to create authentication"); 228b29b5ab3SAppaRao Puli return -EIO; 229b29b5ab3SAppaRao Puli } 2304654d99fSRichard Marian Thomaiyar } 2314654d99fSRichard Marian Thomaiyar return 0; 2324654d99fSRichard Marian Thomaiyar } 2334654d99fSRichard Marian Thomaiyar 2344654d99fSRichard Marian Thomaiyar void PasswdMgr::initPasswordMap(void) 2354654d99fSRichard Marian Thomaiyar { 2362f0ad74dSAndrew Geissler // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{}; 237b29b5ab3SAppaRao Puli std::vector<uint8_t> dataBuf; 2384654d99fSRichard Marian Thomaiyar 239b29b5ab3SAppaRao Puli if (readPasswdFileData(dataBuf) != 0) 2404654d99fSRichard Marian Thomaiyar { 241b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading the encrypted pass file"); 2424654d99fSRichard Marian Thomaiyar return; 2434654d99fSRichard Marian Thomaiyar } 2444654d99fSRichard Marian Thomaiyar 245b29b5ab3SAppaRao Puli if (dataBuf.size() != 0) 2464654d99fSRichard Marian Thomaiyar { 2474654d99fSRichard Marian Thomaiyar // populate the user list with password 248b29b5ab3SAppaRao Puli char* outPtr = reinterpret_cast<char*>(dataBuf.data()); 2494654d99fSRichard Marian Thomaiyar char* nToken = NULL; 2504654d99fSRichard Marian Thomaiyar char* linePtr = strtok_r(outPtr, "\n", &nToken); 25151d0c40aSPatrick Venture size_t lineSize = 0; 2524654d99fSRichard Marian Thomaiyar while (linePtr != NULL) 2534654d99fSRichard Marian Thomaiyar { 25451d0c40aSPatrick Venture size_t userEPos = 0; 2554654d99fSRichard Marian Thomaiyar std::string lineStr(linePtr); 2564654d99fSRichard Marian Thomaiyar if ((userEPos = lineStr.find(":")) != std::string::npos) 2574654d99fSRichard Marian Thomaiyar { 2584654d99fSRichard Marian Thomaiyar lineSize = lineStr.size(); 2594654d99fSRichard Marian Thomaiyar passwdMapList.emplace( 2604654d99fSRichard Marian Thomaiyar lineStr.substr(0, userEPos), 2614654d99fSRichard Marian Thomaiyar lineStr.substr(userEPos + 1, lineSize - (userEPos + 1))); 2624654d99fSRichard Marian Thomaiyar } 2634654d99fSRichard Marian Thomaiyar linePtr = strtok_r(NULL, "\n", &nToken); 2644654d99fSRichard Marian Thomaiyar } 265b29b5ab3SAppaRao Puli } 266b29b5ab3SAppaRao Puli 2674654d99fSRichard Marian Thomaiyar // Update the timestamp 268b29b5ab3SAppaRao Puli fileLastUpdatedTime = getUpdatedFileTime(); 26970bd0635SJayaprakash Mutyala // Clear sensitive data 27070bd0635SJayaprakash Mutyala OPENSSL_cleanse(dataBuf.data(), dataBuf.size()); 271b29b5ab3SAppaRao Puli return; 272b29b5ab3SAppaRao Puli } 273b29b5ab3SAppaRao Puli 274b29b5ab3SAppaRao Puli int PasswdMgr::readPasswdFileData(std::vector<uint8_t>& outBytes) 275b29b5ab3SAppaRao Puli { 276b29b5ab3SAppaRao Puli std::array<uint8_t, maxKeySize> keyBuff; 277b29b5ab3SAppaRao Puli std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary); 278b29b5ab3SAppaRao Puli if (!keyFile.is_open()) 279b29b5ab3SAppaRao Puli { 280b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in opening encryption key file"); 281b29b5ab3SAppaRao Puli return -EIO; 282b29b5ab3SAppaRao Puli } 283b29b5ab3SAppaRao Puli keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size()); 284b29b5ab3SAppaRao Puli if (keyFile.fail()) 285b29b5ab3SAppaRao Puli { 286b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading encryption key file"); 287b29b5ab3SAppaRao Puli return -EIO; 288b29b5ab3SAppaRao Puli } 289b29b5ab3SAppaRao Puli 290b29b5ab3SAppaRao Puli std::ifstream passwdFile(passwdFileName, std::ios::in | std::ios::binary); 291b29b5ab3SAppaRao Puli if (!passwdFile.is_open()) 292b29b5ab3SAppaRao Puli { 293b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in opening ipmi password file"); 294b29b5ab3SAppaRao Puli return -EIO; 295b29b5ab3SAppaRao Puli } 296b29b5ab3SAppaRao Puli 297b29b5ab3SAppaRao Puli // calculate file size and read the data 298b29b5ab3SAppaRao Puli passwdFile.seekg(0, std::ios::end); 299b29b5ab3SAppaRao Puli ssize_t fileSize = passwdFile.tellg(); 300b29b5ab3SAppaRao Puli passwdFile.seekg(0, std::ios::beg); 301b29b5ab3SAppaRao Puli std::vector<uint8_t> input(fileSize); 302b29b5ab3SAppaRao Puli passwdFile.read(reinterpret_cast<char*>(input.data()), fileSize); 303b29b5ab3SAppaRao Puli if (passwdFile.fail()) 304b29b5ab3SAppaRao Puli { 305b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading encryption key file"); 306b29b5ab3SAppaRao Puli return -EIO; 307b29b5ab3SAppaRao Puli } 308b29b5ab3SAppaRao Puli 309b29b5ab3SAppaRao Puli // verify the signature first 31048e55585SRichard Marian Thomaiyar MetaPassStruct* metaData = reinterpret_cast<MetaPassStruct*>(input.data()); 311b29b5ab3SAppaRao Puli if (std::strncmp(metaData->signature, META_PASSWD_SIG, 312b29b5ab3SAppaRao Puli sizeof(metaData->signature))) 313b29b5ab3SAppaRao Puli { 314b29b5ab3SAppaRao Puli log<level::DEBUG>("Error signature mismatch in password file"); 315b29b5ab3SAppaRao Puli return -EBADMSG; 316b29b5ab3SAppaRao Puli } 317b29b5ab3SAppaRao Puli 318b29b5ab3SAppaRao Puli size_t inBytesLen = metaData->dataSize + metaData->padSize; 319b29b5ab3SAppaRao Puli // If data is empty i.e no password map then return success 320b29b5ab3SAppaRao Puli if (inBytesLen == 0) 321b29b5ab3SAppaRao Puli { 322b29b5ab3SAppaRao Puli log<level::DEBUG>("Empty password file"); 323b29b5ab3SAppaRao Puli return 0; 324b29b5ab3SAppaRao Puli } 325b29b5ab3SAppaRao Puli 326b29b5ab3SAppaRao Puli // compute the key needed to decrypt 327b29b5ab3SAppaRao Puli std::array<uint8_t, EVP_MAX_KEY_LENGTH> key; 328b29b5ab3SAppaRao Puli auto keyLen = key.size(); 329b29b5ab3SAppaRao Puli if (NULL == HMAC(EVP_sha256(), keyBuff.data(), keyBuff.size(), 330b29b5ab3SAppaRao Puli input.data() + sizeof(*metaData), metaData->hashSize, 331b29b5ab3SAppaRao Puli key.data(), reinterpret_cast<unsigned int*>(&keyLen))) 332b29b5ab3SAppaRao Puli { 333b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to create MAC for authentication"); 334b29b5ab3SAppaRao Puli return -EIO; 335b29b5ab3SAppaRao Puli } 336b29b5ab3SAppaRao Puli 337b29b5ab3SAppaRao Puli // decrypt the data 338b29b5ab3SAppaRao Puli uint8_t* iv = input.data() + sizeof(*metaData) + metaData->hashSize; 339b29b5ab3SAppaRao Puli size_t ivLen = metaData->ivSize; 340b29b5ab3SAppaRao Puli uint8_t* inBytes = iv + ivLen; 341b29b5ab3SAppaRao Puli uint8_t* mac = inBytes + inBytesLen; 342b29b5ab3SAppaRao Puli size_t macLen = metaData->macSize; 343b29b5ab3SAppaRao Puli 344b29b5ab3SAppaRao Puli size_t outBytesLen = 0; 345b29b5ab3SAppaRao Puli // Resize to actual data size 346b29b5ab3SAppaRao Puli outBytes.resize(inBytesLen + EVP_MAX_BLOCK_LENGTH); 347b29b5ab3SAppaRao Puli if (encryptDecryptData(false, EVP_aes_128_cbc(), key.data(), keyLen, iv, 348b29b5ab3SAppaRao Puli ivLen, inBytes, inBytesLen, mac, &macLen, 349b29b5ab3SAppaRao Puli outBytes.data(), &outBytesLen) != 0) 350b29b5ab3SAppaRao Puli { 351b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in decryption"); 352b29b5ab3SAppaRao Puli return -EIO; 353b29b5ab3SAppaRao Puli } 354b29b5ab3SAppaRao Puli // Resize the vector to outBytesLen 355b29b5ab3SAppaRao Puli outBytes.resize(outBytesLen); 356b29b5ab3SAppaRao Puli 357b29b5ab3SAppaRao Puli OPENSSL_cleanse(key.data(), keyLen); 358b29b5ab3SAppaRao Puli OPENSSL_cleanse(iv, ivLen); 359b29b5ab3SAppaRao Puli 360b29b5ab3SAppaRao Puli return 0; 361b29b5ab3SAppaRao Puli } 362b29b5ab3SAppaRao Puli 36342bed64dSRichard Marian Thomaiyar int PasswdMgr::updatePasswdSpecialFile(const std::string& userName, 36442bed64dSRichard Marian Thomaiyar const std::string& newUserName) 365b29b5ab3SAppaRao Puli { 3662f0ad74dSAndrew Geissler // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{}; 367b29b5ab3SAppaRao Puli 368b29b5ab3SAppaRao Puli size_t bytesWritten = 0; 369b29b5ab3SAppaRao Puli size_t inBytesLen = 0; 370b29b5ab3SAppaRao Puli size_t isUsrFound = false; 371b29b5ab3SAppaRao Puli const EVP_CIPHER* cipher = EVP_aes_128_cbc(); 372b29b5ab3SAppaRao Puli std::vector<uint8_t> dataBuf; 373b29b5ab3SAppaRao Puli 374b29b5ab3SAppaRao Puli // Read the encrypted file and get the file data 375b29b5ab3SAppaRao Puli // Check user existance and return if not exist. 376b29b5ab3SAppaRao Puli if (readPasswdFileData(dataBuf) != 0) 377b29b5ab3SAppaRao Puli { 378b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading the encrypted pass file"); 379b29b5ab3SAppaRao Puli return -EIO; 380b29b5ab3SAppaRao Puli } 381b29b5ab3SAppaRao Puli 382b29b5ab3SAppaRao Puli if (dataBuf.size() != 0) 383b29b5ab3SAppaRao Puli { 38442bed64dSRichard Marian Thomaiyar inBytesLen = 38542bed64dSRichard Marian Thomaiyar dataBuf.size() + newUserName.size() + EVP_CIPHER_block_size(cipher); 386b29b5ab3SAppaRao Puli } 387b29b5ab3SAppaRao Puli 388b29b5ab3SAppaRao Puli std::vector<uint8_t> inBytes(inBytesLen); 389b29b5ab3SAppaRao Puli if (inBytesLen != 0) 390b29b5ab3SAppaRao Puli { 391b29b5ab3SAppaRao Puli char* outPtr = reinterpret_cast<char*>(dataBuf.data()); 392b29b5ab3SAppaRao Puli char* nToken = NULL; 393b29b5ab3SAppaRao Puli char* linePtr = strtok_r(outPtr, "\n", &nToken); 394b29b5ab3SAppaRao Puli while (linePtr != NULL) 395b29b5ab3SAppaRao Puli { 39651d0c40aSPatrick Venture size_t userEPos = 0; 39751d0c40aSPatrick Venture 398b29b5ab3SAppaRao Puli std::string lineStr(linePtr); 399b29b5ab3SAppaRao Puli if ((userEPos = lineStr.find(":")) != std::string::npos) 400b29b5ab3SAppaRao Puli { 401b29b5ab3SAppaRao Puli if (userName.compare(lineStr.substr(0, userEPos)) == 0) 402b29b5ab3SAppaRao Puli { 403b29b5ab3SAppaRao Puli isUsrFound = true; 40442bed64dSRichard Marian Thomaiyar if (!newUserName.empty()) 40542bed64dSRichard Marian Thomaiyar { 40642bed64dSRichard Marian Thomaiyar bytesWritten += std::snprintf( 40742bed64dSRichard Marian Thomaiyar reinterpret_cast<char*>(&inBytes[0]) + bytesWritten, 40842bed64dSRichard Marian Thomaiyar (inBytesLen - bytesWritten), "%s%s\n", 40942bed64dSRichard Marian Thomaiyar newUserName.c_str(), 41042bed64dSRichard Marian Thomaiyar lineStr.substr(userEPos, lineStr.size()).data()); 41142bed64dSRichard Marian Thomaiyar } 412b29b5ab3SAppaRao Puli } 413b29b5ab3SAppaRao Puli else 414b29b5ab3SAppaRao Puli { 415b29b5ab3SAppaRao Puli bytesWritten += std::snprintf( 416b29b5ab3SAppaRao Puli reinterpret_cast<char*>(&inBytes[0]) + bytesWritten, 41742bed64dSRichard Marian Thomaiyar (inBytesLen - bytesWritten), "%s\n", lineStr.data()); 418b29b5ab3SAppaRao Puli } 419b29b5ab3SAppaRao Puli } 420b29b5ab3SAppaRao Puli linePtr = strtok_r(NULL, "\n", &nToken); 421b29b5ab3SAppaRao Puli } 422161f20d5SRichard Marian Thomaiyar inBytesLen = bytesWritten; 423b29b5ab3SAppaRao Puli } 424b29b5ab3SAppaRao Puli if (!isUsrFound) 425b29b5ab3SAppaRao Puli { 426b29b5ab3SAppaRao Puli log<level::DEBUG>("User doesn't exist"); 427b29b5ab3SAppaRao Puli return 0; 428b29b5ab3SAppaRao Puli } 429b29b5ab3SAppaRao Puli 430b29b5ab3SAppaRao Puli // Read the key buff from key file 431b29b5ab3SAppaRao Puli std::array<uint8_t, maxKeySize> keyBuff; 432b29b5ab3SAppaRao Puli std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary); 433b29b5ab3SAppaRao Puli if (!keyFile.good()) 434b29b5ab3SAppaRao Puli { 435b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in opening encryption key file"); 436b29b5ab3SAppaRao Puli return -EIO; 437b29b5ab3SAppaRao Puli } 438b29b5ab3SAppaRao Puli keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size()); 439b29b5ab3SAppaRao Puli if (keyFile.fail()) 440b29b5ab3SAppaRao Puli { 441b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading encryption key file"); 442b29b5ab3SAppaRao Puli return -EIO; 443b29b5ab3SAppaRao Puli } 444b29b5ab3SAppaRao Puli keyFile.close(); 445b29b5ab3SAppaRao Puli 446b29b5ab3SAppaRao Puli // Read the original passwd file mode 447b29b5ab3SAppaRao Puli struct stat st = {}; 448b29b5ab3SAppaRao Puli if (stat(passwdFileName, &st) != 0) 449b29b5ab3SAppaRao Puli { 450b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in getting password file fstat()"); 451b29b5ab3SAppaRao Puli return -EIO; 452b29b5ab3SAppaRao Puli } 453b29b5ab3SAppaRao Puli 454b29b5ab3SAppaRao Puli // Create temporary file for write 455b29b5ab3SAppaRao Puli std::string pwdFile(passwdFileName); 456b29b5ab3SAppaRao Puli std::vector<char> tempFileName(pwdFile.begin(), pwdFile.end()); 457b29b5ab3SAppaRao Puli std::vector<char> fileTemplate = {'_', '_', 'X', 'X', 'X', 458b29b5ab3SAppaRao Puli 'X', 'X', 'X', '\0'}; 459b29b5ab3SAppaRao Puli tempFileName.insert(tempFileName.end(), fileTemplate.begin(), 460b29b5ab3SAppaRao Puli fileTemplate.end()); 461b29b5ab3SAppaRao Puli int fd = mkstemp((char*)tempFileName.data()); 462b29b5ab3SAppaRao Puli if (fd == -1) 463b29b5ab3SAppaRao Puli { 464b29b5ab3SAppaRao Puli log<level::DEBUG>("Error creating temp file"); 465b29b5ab3SAppaRao Puli return -EIO; 466b29b5ab3SAppaRao Puli } 467b29b5ab3SAppaRao Puli 468b29b5ab3SAppaRao Puli std::string strTempFileName(tempFileName.data()); 469b29b5ab3SAppaRao Puli // Open the temp file for writing from provided fd 470b29b5ab3SAppaRao Puli // By "true", remove it at exit if still there. 471b29b5ab3SAppaRao Puli // This is needed to cleanup the temp file at exception 472b29b5ab3SAppaRao Puli phosphor::user::File temp(fd, strTempFileName, "w", true); 473b29b5ab3SAppaRao Puli if ((temp)() == NULL) 474b29b5ab3SAppaRao Puli { 475b29b5ab3SAppaRao Puli close(fd); 476b29b5ab3SAppaRao Puli log<level::DEBUG>("Error creating temp file"); 477b29b5ab3SAppaRao Puli return -EIO; 478b29b5ab3SAppaRao Puli } 479b29b5ab3SAppaRao Puli 480b265455aSVernon Mauery // Set the file mode as read-write for owner only 481b265455aSVernon Mauery if (fchmod(fileno((temp)()), S_IRUSR | S_IWUSR) < 0) 482b29b5ab3SAppaRao Puli { 483b29b5ab3SAppaRao Puli log<level::DEBUG>("Error setting fchmod for temp file"); 484b29b5ab3SAppaRao Puli return -EIO; 485b29b5ab3SAppaRao Puli } 486b29b5ab3SAppaRao Puli 487b29b5ab3SAppaRao Puli const EVP_MD* digest = EVP_sha256(); 488b29b5ab3SAppaRao Puli size_t hashLen = EVP_MD_block_size(digest); 489b29b5ab3SAppaRao Puli std::vector<uint8_t> hash(hashLen); 490b29b5ab3SAppaRao Puli size_t ivLen = EVP_CIPHER_iv_length(cipher); 491b29b5ab3SAppaRao Puli std::vector<uint8_t> iv(ivLen); 492b29b5ab3SAppaRao Puli std::array<uint8_t, EVP_MAX_KEY_LENGTH> key; 493b29b5ab3SAppaRao Puli size_t keyLen = key.size(); 494b29b5ab3SAppaRao Puli std::array<uint8_t, EVP_MAX_MD_SIZE> mac; 495b29b5ab3SAppaRao Puli size_t macLen = mac.size(); 496b29b5ab3SAppaRao Puli 497b29b5ab3SAppaRao Puli // Create random hash and generate hash key which will be used for 498b29b5ab3SAppaRao Puli // encryption. 499b29b5ab3SAppaRao Puli if (RAND_bytes(hash.data(), hashLen) != 1) 500b29b5ab3SAppaRao Puli { 501b29b5ab3SAppaRao Puli log<level::DEBUG>("Hash genertion failed, bailing out"); 502b29b5ab3SAppaRao Puli return -EIO; 503b29b5ab3SAppaRao Puli } 504b29b5ab3SAppaRao Puli if (NULL == HMAC(digest, keyBuff.data(), keyBuff.size(), hash.data(), 505b29b5ab3SAppaRao Puli hashLen, key.data(), 506b29b5ab3SAppaRao Puli reinterpret_cast<unsigned int*>(&keyLen))) 507b29b5ab3SAppaRao Puli { 508b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to create MAC for authentication"); 509b29b5ab3SAppaRao Puli return -EIO; 510b29b5ab3SAppaRao Puli } 511b29b5ab3SAppaRao Puli 512b29b5ab3SAppaRao Puli // Generate IV values 513b29b5ab3SAppaRao Puli if (RAND_bytes(iv.data(), ivLen) != 1) 514b29b5ab3SAppaRao Puli { 515b29b5ab3SAppaRao Puli log<level::DEBUG>("UV genertion failed, bailing out"); 516b29b5ab3SAppaRao Puli return -EIO; 517b29b5ab3SAppaRao Puli } 518b29b5ab3SAppaRao Puli 519b29b5ab3SAppaRao Puli // Encrypt the input data 520b29b5ab3SAppaRao Puli std::vector<uint8_t> outBytes(inBytesLen + EVP_MAX_BLOCK_LENGTH); 521b29b5ab3SAppaRao Puli size_t outBytesLen = 0; 522b29b5ab3SAppaRao Puli if (inBytesLen != 0) 523b29b5ab3SAppaRao Puli { 524b29b5ab3SAppaRao Puli if (encryptDecryptData(true, EVP_aes_128_cbc(), key.data(), keyLen, 525b29b5ab3SAppaRao Puli iv.data(), ivLen, inBytes.data(), inBytesLen, 526b29b5ab3SAppaRao Puli mac.data(), &macLen, outBytes.data(), 527b29b5ab3SAppaRao Puli &outBytesLen) != 0) 528b29b5ab3SAppaRao Puli { 529b29b5ab3SAppaRao Puli log<level::DEBUG>("Error while encrypting the data"); 530b29b5ab3SAppaRao Puli return -EIO; 531b29b5ab3SAppaRao Puli } 532b29b5ab3SAppaRao Puli outBytes[outBytesLen] = 0; 533b29b5ab3SAppaRao Puli } 534b29b5ab3SAppaRao Puli OPENSSL_cleanse(key.data(), keyLen); 535b29b5ab3SAppaRao Puli 536b29b5ab3SAppaRao Puli // Update the meta password structure. 53748e55585SRichard Marian Thomaiyar MetaPassStruct metaData = {META_PASSWD_SIG, {0, 0}, 0, 0, 0, 0, 0}; 538b29b5ab3SAppaRao Puli metaData.hashSize = hashLen; 539b29b5ab3SAppaRao Puli metaData.ivSize = ivLen; 540b29b5ab3SAppaRao Puli metaData.dataSize = bytesWritten; 541b29b5ab3SAppaRao Puli metaData.padSize = outBytesLen - bytesWritten; 542b29b5ab3SAppaRao Puli metaData.macSize = macLen; 543b29b5ab3SAppaRao Puli 544b29b5ab3SAppaRao Puli if (fwrite(&metaData, 1, sizeof(metaData), (temp)()) != sizeof(metaData)) 545b29b5ab3SAppaRao Puli { 546b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing meta data"); 547b29b5ab3SAppaRao Puli return -EIO; 548b29b5ab3SAppaRao Puli } 549b29b5ab3SAppaRao Puli 550b29b5ab3SAppaRao Puli if (fwrite(&hash[0], 1, hashLen, (temp)()) != hashLen) 551b29b5ab3SAppaRao Puli { 552b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing hash data"); 553b29b5ab3SAppaRao Puli return -EIO; 554b29b5ab3SAppaRao Puli } 555b29b5ab3SAppaRao Puli 556b29b5ab3SAppaRao Puli if (fwrite(&iv[0], 1, ivLen, (temp)()) != ivLen) 557b29b5ab3SAppaRao Puli { 558b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing IV data"); 559b29b5ab3SAppaRao Puli return -EIO; 560b29b5ab3SAppaRao Puli } 561b29b5ab3SAppaRao Puli 562b29b5ab3SAppaRao Puli if (fwrite(&outBytes[0], 1, outBytesLen, (temp)()) != outBytesLen) 563b29b5ab3SAppaRao Puli { 564b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing encrypted data"); 565b29b5ab3SAppaRao Puli return -EIO; 566b29b5ab3SAppaRao Puli } 567b29b5ab3SAppaRao Puli 568b29b5ab3SAppaRao Puli if (fwrite(&mac[0], 1, macLen, (temp)()) != macLen) 569b29b5ab3SAppaRao Puli { 570b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing MAC data"); 571b29b5ab3SAppaRao Puli return -EIO; 572b29b5ab3SAppaRao Puli } 573b29b5ab3SAppaRao Puli 574b29b5ab3SAppaRao Puli if (fflush((temp)())) 575b29b5ab3SAppaRao Puli { 576b29b5ab3SAppaRao Puli log<level::DEBUG>( 577b29b5ab3SAppaRao Puli "File fflush error while writing entries to special file"); 578b29b5ab3SAppaRao Puli return -EIO; 579b29b5ab3SAppaRao Puli } 580b29b5ab3SAppaRao Puli 581b29b5ab3SAppaRao Puli OPENSSL_cleanse(iv.data(), ivLen); 582b29b5ab3SAppaRao Puli 583b29b5ab3SAppaRao Puli // Rename the tmp file to actual file 584b29b5ab3SAppaRao Puli if (std::rename(strTempFileName.data(), passwdFileName) != 0) 585b29b5ab3SAppaRao Puli { 586b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to rename tmp file to ipmi-pass"); 587b29b5ab3SAppaRao Puli return -EIO; 588b29b5ab3SAppaRao Puli } 589b29b5ab3SAppaRao Puli 590b29b5ab3SAppaRao Puli return 0; 591b29b5ab3SAppaRao Puli } 592b29b5ab3SAppaRao Puli 593b29b5ab3SAppaRao Puli std::time_t PasswdMgr::getUpdatedFileTime() 594b29b5ab3SAppaRao Puli { 5954654d99fSRichard Marian Thomaiyar struct stat fileStat = {}; 5964654d99fSRichard Marian Thomaiyar if (stat(passwdFileName, &fileStat) != 0) 5974654d99fSRichard Marian Thomaiyar { 598b29b5ab3SAppaRao Puli log<level::DEBUG>("Error - Getting passwd file time stamp"); 599b29b5ab3SAppaRao Puli return -EIO; 6004654d99fSRichard Marian Thomaiyar } 601b29b5ab3SAppaRao Puli return fileStat.st_mtime; 6024654d99fSRichard Marian Thomaiyar } 6034654d99fSRichard Marian Thomaiyar 6044654d99fSRichard Marian Thomaiyar } // namespace ipmi 605