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 29fbc6c9d7SPatrick Williams #include <phosphor-logging/log.hpp> 30fbc6c9d7SPatrick Williams 31b29b5ab3SAppaRao Puli #include <cerrno> 324654d99fSRichard Marian Thomaiyar #include <cstring> 334654d99fSRichard Marian Thomaiyar #include <fstream> 34b29b5ab3SAppaRao Puli #include <iomanip> 354654d99fSRichard Marian Thomaiyar 364654d99fSRichard Marian Thomaiyar namespace ipmi 374654d99fSRichard Marian Thomaiyar { 384654d99fSRichard Marian Thomaiyar 394654d99fSRichard Marian Thomaiyar static const char* passwdFileName = "/etc/ipmi_pass"; 404654d99fSRichard Marian Thomaiyar static const char* encryptKeyFileName = "/etc/key_file"; 414654d99fSRichard Marian Thomaiyar static const size_t maxKeySize = 8; 424654d99fSRichard Marian Thomaiyar 436ba8d315SRichard Marian Thomaiyar constexpr mode_t modeMask = 446ba8d315SRichard Marian Thomaiyar (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO); 456ba8d315SRichard Marian Thomaiyar 46b29b5ab3SAppaRao Puli #define META_PASSWD_SIG "=OPENBMC=" 47b29b5ab3SAppaRao Puli 484654d99fSRichard Marian Thomaiyar /* 494654d99fSRichard Marian Thomaiyar * Meta data struct for encrypted password file 504654d99fSRichard Marian Thomaiyar */ 5148e55585SRichard Marian Thomaiyar struct MetaPassStruct 524654d99fSRichard Marian Thomaiyar { 534654d99fSRichard Marian Thomaiyar char signature[10]; 544654d99fSRichard Marian Thomaiyar unsigned char reseved[2]; 5565a9168cSTim Lee size_t hashSize; 5665a9168cSTim Lee size_t ivSize; 5765a9168cSTim Lee size_t dataSize; 5865a9168cSTim Lee size_t padSize; 5965a9168cSTim Lee size_t macSize; 604654d99fSRichard Marian Thomaiyar }; 614654d99fSRichard Marian Thomaiyar 624654d99fSRichard Marian Thomaiyar using namespace phosphor::logging; 634654d99fSRichard Marian Thomaiyar 644654d99fSRichard Marian Thomaiyar PasswdMgr::PasswdMgr() 654654d99fSRichard Marian Thomaiyar { 666ba8d315SRichard Marian Thomaiyar restrictFilesPermission(); 674654d99fSRichard Marian Thomaiyar initPasswordMap(); 684654d99fSRichard Marian Thomaiyar } 694654d99fSRichard Marian Thomaiyar 706ba8d315SRichard Marian Thomaiyar void PasswdMgr::restrictFilesPermission(void) 716ba8d315SRichard Marian Thomaiyar { 726ba8d315SRichard Marian Thomaiyar struct stat st = {}; 736ba8d315SRichard Marian Thomaiyar // Restrict file permission to owner read & write 746ba8d315SRichard Marian Thomaiyar if (stat(passwdFileName, &st) == 0) 756ba8d315SRichard Marian Thomaiyar { 766ba8d315SRichard Marian Thomaiyar if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR)) 776ba8d315SRichard Marian Thomaiyar { 78*3771f5f2SPavanKumarIntel if (chmod(passwdFileName, S_IRUSR | S_IWUSR) == -1) 79*3771f5f2SPavanKumarIntel { 80*3771f5f2SPavanKumarIntel log<level::DEBUG>("Error setting chmod for ipmi_pass file"); 81*3771f5f2SPavanKumarIntel } 826ba8d315SRichard Marian Thomaiyar } 836ba8d315SRichard Marian Thomaiyar } 846ba8d315SRichard Marian Thomaiyar 856ba8d315SRichard Marian Thomaiyar if (stat(encryptKeyFileName, &st) == 0) 866ba8d315SRichard Marian Thomaiyar { 876ba8d315SRichard Marian Thomaiyar if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR)) 886ba8d315SRichard Marian Thomaiyar { 89*3771f5f2SPavanKumarIntel if (chmod(encryptKeyFileName, S_IRUSR | S_IWUSR) == -1) 90*3771f5f2SPavanKumarIntel { 91*3771f5f2SPavanKumarIntel log<level::DEBUG>("Error setting chmod for ipmi_pass file"); 92*3771f5f2SPavanKumarIntel } 936ba8d315SRichard Marian Thomaiyar } 946ba8d315SRichard Marian Thomaiyar } 956ba8d315SRichard Marian Thomaiyar } 966ba8d315SRichard Marian Thomaiyar 971e22a0f1SVernon Mauery SecureString PasswdMgr::getPasswdByUserName(const std::string& userName) 984654d99fSRichard Marian Thomaiyar { 994654d99fSRichard Marian Thomaiyar checkAndReload(); 1004654d99fSRichard Marian Thomaiyar auto iter = passwdMapList.find(userName); 1014654d99fSRichard Marian Thomaiyar if (iter == passwdMapList.end()) 1024654d99fSRichard Marian Thomaiyar { 1031e22a0f1SVernon Mauery return SecureString(); 1044654d99fSRichard Marian Thomaiyar } 1054654d99fSRichard Marian Thomaiyar return iter->second; 1064654d99fSRichard Marian Thomaiyar } 1074654d99fSRichard Marian Thomaiyar 10842bed64dSRichard Marian Thomaiyar int PasswdMgr::updateUserEntry(const std::string& userName, 10942bed64dSRichard Marian Thomaiyar const std::string& newUserName) 110b29b5ab3SAppaRao Puli { 111b29b5ab3SAppaRao Puli std::time_t updatedTime = getUpdatedFileTime(); 112b29b5ab3SAppaRao Puli // Check file time stamp to know passwdMapList is up-to-date. 113b29b5ab3SAppaRao Puli // If not up-to-date, then updatePasswdSpecialFile will read and 114b29b5ab3SAppaRao Puli // check the user entry existance. 115b29b5ab3SAppaRao Puli if (fileLastUpdatedTime == updatedTime && updatedTime != -EIO) 116b29b5ab3SAppaRao Puli { 117b29b5ab3SAppaRao Puli if (passwdMapList.find(userName) == passwdMapList.end()) 118b29b5ab3SAppaRao Puli { 119b29b5ab3SAppaRao Puli log<level::DEBUG>("User not found"); 120b29b5ab3SAppaRao Puli return 0; 121b29b5ab3SAppaRao Puli } 122b29b5ab3SAppaRao Puli } 123b29b5ab3SAppaRao Puli 124b29b5ab3SAppaRao Puli // Write passwdMap to Encryted file 12542bed64dSRichard Marian Thomaiyar if (updatePasswdSpecialFile(userName, newUserName) != 0) 126b29b5ab3SAppaRao Puli { 127b29b5ab3SAppaRao Puli log<level::DEBUG>("Passwd file update failed"); 128b29b5ab3SAppaRao Puli return -EIO; 129b29b5ab3SAppaRao Puli } 130b29b5ab3SAppaRao Puli 131b29b5ab3SAppaRao Puli log<level::DEBUG>("Passwd file updated successfully"); 132b29b5ab3SAppaRao Puli return 0; 133b29b5ab3SAppaRao Puli } 134b29b5ab3SAppaRao Puli 1354654d99fSRichard Marian Thomaiyar void PasswdMgr::checkAndReload(void) 1364654d99fSRichard Marian Thomaiyar { 137b29b5ab3SAppaRao Puli std::time_t updatedTime = getUpdatedFileTime(); 138b29b5ab3SAppaRao Puli if (fileLastUpdatedTime != updatedTime && updatedTime != -1) 1394654d99fSRichard Marian Thomaiyar { 1404654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Reloading password map list"); 1414654d99fSRichard Marian Thomaiyar passwdMapList.clear(); 1424654d99fSRichard Marian Thomaiyar initPasswordMap(); 1434654d99fSRichard Marian Thomaiyar } 1444654d99fSRichard Marian Thomaiyar } 1454654d99fSRichard Marian Thomaiyar 146b29b5ab3SAppaRao Puli int PasswdMgr::encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher, 147b29b5ab3SAppaRao Puli uint8_t* key, size_t keyLen, uint8_t* iv, 148b29b5ab3SAppaRao Puli size_t ivLen, uint8_t* inBytes, 149b29b5ab3SAppaRao Puli size_t inBytesLen, uint8_t* mac, 150b29b5ab3SAppaRao Puli size_t* macLen, unsigned char* outBytes, 151b29b5ab3SAppaRao Puli size_t* outBytesLen) 1524654d99fSRichard Marian Thomaiyar { 1534654d99fSRichard Marian Thomaiyar if (cipher == NULL || key == NULL || iv == NULL || inBytes == NULL || 1544654d99fSRichard Marian Thomaiyar outBytes == NULL || mac == NULL || inBytesLen == 0 || 1554654d99fSRichard Marian Thomaiyar (size_t)EVP_CIPHER_key_length(cipher) > keyLen || 1564654d99fSRichard Marian Thomaiyar (size_t)EVP_CIPHER_iv_length(cipher) > ivLen) 1574654d99fSRichard Marian Thomaiyar { 1584654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Error Invalid Inputs"); 159b29b5ab3SAppaRao Puli return -EINVAL; 1604654d99fSRichard Marian Thomaiyar } 1614654d99fSRichard Marian Thomaiyar 162b29b5ab3SAppaRao Puli if (!doEncrypt) 163b29b5ab3SAppaRao Puli { 164b29b5ab3SAppaRao Puli // verify MAC before decrypting the data. 1654654d99fSRichard Marian Thomaiyar std::array<uint8_t, EVP_MAX_MD_SIZE> calMac; 1664654d99fSRichard Marian Thomaiyar size_t calMacLen = calMac.size(); 1674654d99fSRichard Marian Thomaiyar // calculate MAC for the encrypted message. 1684654d99fSRichard Marian Thomaiyar if (NULL == HMAC(EVP_sha256(), key, keyLen, inBytes, inBytesLen, 1694654d99fSRichard Marian Thomaiyar calMac.data(), 1704654d99fSRichard Marian Thomaiyar reinterpret_cast<unsigned int*>(&calMacLen))) 1714654d99fSRichard Marian Thomaiyar { 1724654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Error: Failed to calculate MAC"); 173b29b5ab3SAppaRao Puli return -EIO; 1744654d99fSRichard Marian Thomaiyar } 175b29b5ab3SAppaRao Puli if (!((calMacLen == *macLen) && 1764654d99fSRichard Marian Thomaiyar (std::memcmp(calMac.data(), mac, calMacLen) == 0))) 1774654d99fSRichard Marian Thomaiyar { 1784654d99fSRichard Marian Thomaiyar log<level::DEBUG>("Authenticated message doesn't match"); 179b29b5ab3SAppaRao Puli return -EBADMSG; 180b29b5ab3SAppaRao Puli } 1814654d99fSRichard Marian Thomaiyar } 1824654d99fSRichard Marian Thomaiyar 1834654d99fSRichard Marian Thomaiyar std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ctx( 1844654d99fSRichard Marian Thomaiyar EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free); 1854654d99fSRichard Marian Thomaiyar 186bf30c8d3SP Dheeraj Srujan Kumar if (!ctx) 187bf30c8d3SP Dheeraj Srujan Kumar { 188bf30c8d3SP Dheeraj Srujan Kumar log<level::DEBUG>("Error: EVP_CIPHER_CTX is NULL"); 189bf30c8d3SP Dheeraj Srujan Kumar return -ENOMEM; 190bf30c8d3SP Dheeraj Srujan Kumar } 191bf30c8d3SP Dheeraj Srujan Kumar 192a67caed7SP Dheeraj Srujan Kumar EVP_CIPHER_CTX_set_padding(ctx.get(), 1); 193a67caed7SP Dheeraj Srujan Kumar 194b29b5ab3SAppaRao Puli // Set key & IV 195b29b5ab3SAppaRao Puli int retval = EVP_CipherInit_ex(ctx.get(), cipher, NULL, key, iv, 196b29b5ab3SAppaRao Puli static_cast<int>(doEncrypt)); 1974654d99fSRichard Marian Thomaiyar if (!retval) 1984654d99fSRichard Marian Thomaiyar { 1994654d99fSRichard Marian Thomaiyar log<level::DEBUG>("EVP_CipherInit_ex failed", 2004654d99fSRichard Marian Thomaiyar entry("RET_VAL=%d", retval)); 201b29b5ab3SAppaRao Puli return -EIO; 2024654d99fSRichard Marian Thomaiyar } 2034654d99fSRichard Marian Thomaiyar 2044654d99fSRichard Marian Thomaiyar int outLen = 0, outEVPLen = 0; 2054654d99fSRichard Marian Thomaiyar if ((retval = EVP_CipherUpdate(ctx.get(), outBytes + outLen, &outEVPLen, 2064654d99fSRichard Marian Thomaiyar inBytes, inBytesLen))) 2074654d99fSRichard Marian Thomaiyar { 2084654d99fSRichard Marian Thomaiyar outLen += outEVPLen; 209fbc6c9d7SPatrick Williams if ((retval = EVP_CipherFinal(ctx.get(), outBytes + outLen, 210fbc6c9d7SPatrick Williams &outEVPLen))) 2114654d99fSRichard Marian Thomaiyar { 2124654d99fSRichard Marian Thomaiyar outLen += outEVPLen; 2134654d99fSRichard Marian Thomaiyar *outBytesLen = outLen; 2144654d99fSRichard Marian Thomaiyar } 2154654d99fSRichard Marian Thomaiyar else 2164654d99fSRichard Marian Thomaiyar { 2174654d99fSRichard Marian Thomaiyar log<level::DEBUG>("EVP_CipherFinal fails", 2184654d99fSRichard Marian Thomaiyar entry("RET_VAL=%d", retval)); 219b29b5ab3SAppaRao Puli return -EIO; 2204654d99fSRichard Marian Thomaiyar } 2214654d99fSRichard Marian Thomaiyar } 2224654d99fSRichard Marian Thomaiyar else 2234654d99fSRichard Marian Thomaiyar { 2244654d99fSRichard Marian Thomaiyar log<level::DEBUG>("EVP_CipherUpdate fails", 2254654d99fSRichard Marian Thomaiyar entry("RET_VAL=%d", retval)); 226b29b5ab3SAppaRao Puli return -EIO; 227b29b5ab3SAppaRao Puli } 228b29b5ab3SAppaRao Puli 229b29b5ab3SAppaRao Puli if (doEncrypt) 230b29b5ab3SAppaRao Puli { 231b29b5ab3SAppaRao Puli // Create MAC for the encrypted message 232b29b5ab3SAppaRao Puli if (NULL == HMAC(EVP_sha256(), key, keyLen, outBytes, *outBytesLen, mac, 233b29b5ab3SAppaRao Puli reinterpret_cast<unsigned int*>(macLen))) 234b29b5ab3SAppaRao Puli { 235b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to create authentication"); 236b29b5ab3SAppaRao Puli return -EIO; 237b29b5ab3SAppaRao Puli } 2384654d99fSRichard Marian Thomaiyar } 2394654d99fSRichard Marian Thomaiyar return 0; 2404654d99fSRichard Marian Thomaiyar } 2414654d99fSRichard Marian Thomaiyar 2424654d99fSRichard Marian Thomaiyar void PasswdMgr::initPasswordMap(void) 2434654d99fSRichard Marian Thomaiyar { 2442f0ad74dSAndrew Geissler // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{}; 2451e22a0f1SVernon Mauery SecureString dataBuf; 2464654d99fSRichard Marian Thomaiyar 247b29b5ab3SAppaRao Puli if (readPasswdFileData(dataBuf) != 0) 2484654d99fSRichard Marian Thomaiyar { 249b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading the encrypted pass file"); 2504654d99fSRichard Marian Thomaiyar return; 2514654d99fSRichard Marian Thomaiyar } 2524654d99fSRichard Marian Thomaiyar 253b29b5ab3SAppaRao Puli if (dataBuf.size() != 0) 2544654d99fSRichard Marian Thomaiyar { 2554654d99fSRichard Marian Thomaiyar // populate the user list with password 2561e22a0f1SVernon Mauery char* outPtr = dataBuf.data(); 2574654d99fSRichard Marian Thomaiyar char* nToken = NULL; 2584654d99fSRichard Marian Thomaiyar char* linePtr = strtok_r(outPtr, "\n", &nToken); 25951d0c40aSPatrick Venture size_t lineSize = 0; 2604654d99fSRichard Marian Thomaiyar while (linePtr != NULL) 2614654d99fSRichard Marian Thomaiyar { 26251d0c40aSPatrick Venture size_t userEPos = 0; 2631e22a0f1SVernon Mauery SecureString lineStr(linePtr); 2644654d99fSRichard Marian Thomaiyar if ((userEPos = lineStr.find(":")) != std::string::npos) 2654654d99fSRichard Marian Thomaiyar { 2664654d99fSRichard Marian Thomaiyar lineSize = lineStr.size(); 2674654d99fSRichard Marian Thomaiyar passwdMapList.emplace( 2684654d99fSRichard Marian Thomaiyar lineStr.substr(0, userEPos), 2694654d99fSRichard Marian Thomaiyar lineStr.substr(userEPos + 1, lineSize - (userEPos + 1))); 2704654d99fSRichard Marian Thomaiyar } 2714654d99fSRichard Marian Thomaiyar linePtr = strtok_r(NULL, "\n", &nToken); 2724654d99fSRichard Marian Thomaiyar } 273b29b5ab3SAppaRao Puli } 274b29b5ab3SAppaRao Puli 2754654d99fSRichard Marian Thomaiyar // Update the timestamp 276b29b5ab3SAppaRao Puli fileLastUpdatedTime = getUpdatedFileTime(); 277b29b5ab3SAppaRao Puli return; 278b29b5ab3SAppaRao Puli } 279b29b5ab3SAppaRao Puli 2801e22a0f1SVernon Mauery int PasswdMgr::readPasswdFileData(SecureString& outBytes) 281b29b5ab3SAppaRao Puli { 282b29b5ab3SAppaRao Puli std::array<uint8_t, maxKeySize> keyBuff; 283b29b5ab3SAppaRao Puli std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary); 284b29b5ab3SAppaRao Puli if (!keyFile.is_open()) 285b29b5ab3SAppaRao Puli { 286b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in opening encryption key file"); 287b29b5ab3SAppaRao Puli return -EIO; 288b29b5ab3SAppaRao Puli } 289b29b5ab3SAppaRao Puli keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size()); 290b29b5ab3SAppaRao Puli if (keyFile.fail()) 291b29b5ab3SAppaRao Puli { 292b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading encryption key file"); 293b29b5ab3SAppaRao Puli return -EIO; 294b29b5ab3SAppaRao Puli } 295b29b5ab3SAppaRao Puli 296b29b5ab3SAppaRao Puli std::ifstream passwdFile(passwdFileName, std::ios::in | std::ios::binary); 297b29b5ab3SAppaRao Puli if (!passwdFile.is_open()) 298b29b5ab3SAppaRao Puli { 299b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in opening ipmi password file"); 300b29b5ab3SAppaRao Puli return -EIO; 301b29b5ab3SAppaRao Puli } 302b29b5ab3SAppaRao Puli 303b29b5ab3SAppaRao Puli // calculate file size and read the data 304b29b5ab3SAppaRao Puli passwdFile.seekg(0, std::ios::end); 305b29b5ab3SAppaRao Puli ssize_t fileSize = passwdFile.tellg(); 306b29b5ab3SAppaRao Puli passwdFile.seekg(0, std::ios::beg); 307b29b5ab3SAppaRao Puli std::vector<uint8_t> input(fileSize); 308b29b5ab3SAppaRao Puli passwdFile.read(reinterpret_cast<char*>(input.data()), fileSize); 309b29b5ab3SAppaRao Puli if (passwdFile.fail()) 310b29b5ab3SAppaRao Puli { 311b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading encryption key file"); 312b29b5ab3SAppaRao Puli return -EIO; 313b29b5ab3SAppaRao Puli } 314b29b5ab3SAppaRao Puli 315b29b5ab3SAppaRao Puli // verify the signature first 31648e55585SRichard Marian Thomaiyar MetaPassStruct* metaData = reinterpret_cast<MetaPassStruct*>(input.data()); 317b29b5ab3SAppaRao Puli if (std::strncmp(metaData->signature, META_PASSWD_SIG, 318b29b5ab3SAppaRao Puli sizeof(metaData->signature))) 319b29b5ab3SAppaRao Puli { 320b29b5ab3SAppaRao Puli log<level::DEBUG>("Error signature mismatch in password file"); 321b29b5ab3SAppaRao Puli return -EBADMSG; 322b29b5ab3SAppaRao Puli } 323b29b5ab3SAppaRao Puli 324b29b5ab3SAppaRao Puli size_t inBytesLen = metaData->dataSize + metaData->padSize; 325b29b5ab3SAppaRao Puli // If data is empty i.e no password map then return success 326b29b5ab3SAppaRao Puli if (inBytesLen == 0) 327b29b5ab3SAppaRao Puli { 328b29b5ab3SAppaRao Puli log<level::DEBUG>("Empty password file"); 329b29b5ab3SAppaRao Puli return 0; 330b29b5ab3SAppaRao Puli } 331b29b5ab3SAppaRao Puli 332b29b5ab3SAppaRao Puli // compute the key needed to decrypt 333b29b5ab3SAppaRao Puli std::array<uint8_t, EVP_MAX_KEY_LENGTH> key; 334b29b5ab3SAppaRao Puli auto keyLen = key.size(); 335b29b5ab3SAppaRao Puli if (NULL == HMAC(EVP_sha256(), keyBuff.data(), keyBuff.size(), 336b29b5ab3SAppaRao Puli input.data() + sizeof(*metaData), metaData->hashSize, 337b29b5ab3SAppaRao Puli key.data(), reinterpret_cast<unsigned int*>(&keyLen))) 338b29b5ab3SAppaRao Puli { 339b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to create MAC for authentication"); 340b29b5ab3SAppaRao Puli return -EIO; 341b29b5ab3SAppaRao Puli } 342b29b5ab3SAppaRao Puli 343b29b5ab3SAppaRao Puli // decrypt the data 344b29b5ab3SAppaRao Puli uint8_t* iv = input.data() + sizeof(*metaData) + metaData->hashSize; 345b29b5ab3SAppaRao Puli size_t ivLen = metaData->ivSize; 346b29b5ab3SAppaRao Puli uint8_t* inBytes = iv + ivLen; 347b29b5ab3SAppaRao Puli uint8_t* mac = inBytes + inBytesLen; 348b29b5ab3SAppaRao Puli size_t macLen = metaData->macSize; 349b29b5ab3SAppaRao Puli 350b29b5ab3SAppaRao Puli size_t outBytesLen = 0; 351b29b5ab3SAppaRao Puli // Resize to actual data size 3521e22a0f1SVernon Mauery outBytes.resize(inBytesLen + EVP_MAX_BLOCK_LENGTH, '\0'); 353b29b5ab3SAppaRao Puli if (encryptDecryptData(false, EVP_aes_128_cbc(), key.data(), keyLen, iv, 354b29b5ab3SAppaRao Puli ivLen, inBytes, inBytesLen, mac, &macLen, 3551e22a0f1SVernon Mauery reinterpret_cast<unsigned char*>(outBytes.data()), 3561e22a0f1SVernon Mauery &outBytesLen) != 0) 357b29b5ab3SAppaRao Puli { 358b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in decryption"); 359b29b5ab3SAppaRao Puli return -EIO; 360b29b5ab3SAppaRao Puli } 361b29b5ab3SAppaRao Puli // Resize the vector to outBytesLen 362b29b5ab3SAppaRao Puli outBytes.resize(outBytesLen); 363b29b5ab3SAppaRao Puli 364b29b5ab3SAppaRao Puli OPENSSL_cleanse(key.data(), keyLen); 365b29b5ab3SAppaRao Puli OPENSSL_cleanse(iv, ivLen); 366b29b5ab3SAppaRao Puli 367b29b5ab3SAppaRao Puli return 0; 368b29b5ab3SAppaRao Puli } 369b29b5ab3SAppaRao Puli 37042bed64dSRichard Marian Thomaiyar int PasswdMgr::updatePasswdSpecialFile(const std::string& userName, 37142bed64dSRichard Marian Thomaiyar const std::string& newUserName) 372b29b5ab3SAppaRao Puli { 3732f0ad74dSAndrew Geissler // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{}; 374b29b5ab3SAppaRao Puli 375b29b5ab3SAppaRao Puli size_t bytesWritten = 0; 376b29b5ab3SAppaRao Puli size_t inBytesLen = 0; 377b29b5ab3SAppaRao Puli size_t isUsrFound = false; 378b29b5ab3SAppaRao Puli const EVP_CIPHER* cipher = EVP_aes_128_cbc(); 3791e22a0f1SVernon Mauery SecureString dataBuf; 380b29b5ab3SAppaRao Puli 381b29b5ab3SAppaRao Puli // Read the encrypted file and get the file data 382b29b5ab3SAppaRao Puli // Check user existance and return if not exist. 383b29b5ab3SAppaRao Puli if (readPasswdFileData(dataBuf) != 0) 384b29b5ab3SAppaRao Puli { 385b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading the encrypted pass file"); 386b29b5ab3SAppaRao Puli return -EIO; 387b29b5ab3SAppaRao Puli } 388b29b5ab3SAppaRao Puli 389b29b5ab3SAppaRao Puli if (dataBuf.size() != 0) 390b29b5ab3SAppaRao Puli { 391fbc6c9d7SPatrick Williams inBytesLen = dataBuf.size() + newUserName.size() + 392fbc6c9d7SPatrick Williams EVP_CIPHER_block_size(cipher); 393b29b5ab3SAppaRao Puli } 394b29b5ab3SAppaRao Puli 3951e22a0f1SVernon Mauery SecureString inBytes(inBytesLen, '\0'); 396b29b5ab3SAppaRao Puli if (inBytesLen != 0) 397b29b5ab3SAppaRao Puli { 398b29b5ab3SAppaRao Puli char* outPtr = reinterpret_cast<char*>(dataBuf.data()); 399b29b5ab3SAppaRao Puli char* nToken = NULL; 400b29b5ab3SAppaRao Puli char* linePtr = strtok_r(outPtr, "\n", &nToken); 401b29b5ab3SAppaRao Puli while (linePtr != NULL) 402b29b5ab3SAppaRao Puli { 40351d0c40aSPatrick Venture size_t userEPos = 0; 40451d0c40aSPatrick Venture 4051e22a0f1SVernon Mauery SecureString lineStr(linePtr); 406b29b5ab3SAppaRao Puli if ((userEPos = lineStr.find(":")) != std::string::npos) 407b29b5ab3SAppaRao Puli { 408b29b5ab3SAppaRao Puli if (userName.compare(lineStr.substr(0, userEPos)) == 0) 409b29b5ab3SAppaRao Puli { 410b29b5ab3SAppaRao Puli isUsrFound = true; 41142bed64dSRichard Marian Thomaiyar if (!newUserName.empty()) 41242bed64dSRichard Marian Thomaiyar { 41342bed64dSRichard Marian Thomaiyar bytesWritten += std::snprintf( 4141e22a0f1SVernon Mauery &inBytes[0] + bytesWritten, 41542bed64dSRichard Marian Thomaiyar (inBytesLen - bytesWritten), "%s%s\n", 41642bed64dSRichard Marian Thomaiyar newUserName.c_str(), 41742bed64dSRichard Marian Thomaiyar lineStr.substr(userEPos, lineStr.size()).data()); 41842bed64dSRichard Marian Thomaiyar } 419b29b5ab3SAppaRao Puli } 420b29b5ab3SAppaRao Puli else 421b29b5ab3SAppaRao Puli { 4221e22a0f1SVernon Mauery bytesWritten += std::snprintf(&inBytes[0] + bytesWritten, 4231e22a0f1SVernon Mauery (inBytesLen - bytesWritten), 4241e22a0f1SVernon Mauery "%s\n", lineStr.data()); 425b29b5ab3SAppaRao Puli } 426b29b5ab3SAppaRao Puli } 427b29b5ab3SAppaRao Puli linePtr = strtok_r(NULL, "\n", &nToken); 428b29b5ab3SAppaRao Puli } 429161f20d5SRichard Marian Thomaiyar inBytesLen = bytesWritten; 430b29b5ab3SAppaRao Puli } 431b29b5ab3SAppaRao Puli if (!isUsrFound) 432b29b5ab3SAppaRao Puli { 433b29b5ab3SAppaRao Puli log<level::DEBUG>("User doesn't exist"); 434b29b5ab3SAppaRao Puli return 0; 435b29b5ab3SAppaRao Puli } 436b29b5ab3SAppaRao Puli 437b29b5ab3SAppaRao Puli // Read the key buff from key file 438b29b5ab3SAppaRao Puli std::array<uint8_t, maxKeySize> keyBuff; 439b29b5ab3SAppaRao Puli std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary); 440b29b5ab3SAppaRao Puli if (!keyFile.good()) 441b29b5ab3SAppaRao Puli { 442b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in opening encryption key file"); 443b29b5ab3SAppaRao Puli return -EIO; 444b29b5ab3SAppaRao Puli } 445b29b5ab3SAppaRao Puli keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size()); 446b29b5ab3SAppaRao Puli if (keyFile.fail()) 447b29b5ab3SAppaRao Puli { 448b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in reading encryption key file"); 449b29b5ab3SAppaRao Puli return -EIO; 450b29b5ab3SAppaRao Puli } 451b29b5ab3SAppaRao Puli keyFile.close(); 452b29b5ab3SAppaRao Puli 453b29b5ab3SAppaRao Puli // Read the original passwd file mode 454b29b5ab3SAppaRao Puli struct stat st = {}; 455b29b5ab3SAppaRao Puli if (stat(passwdFileName, &st) != 0) 456b29b5ab3SAppaRao Puli { 457b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in getting password file fstat()"); 458b29b5ab3SAppaRao Puli return -EIO; 459b29b5ab3SAppaRao Puli } 460b29b5ab3SAppaRao Puli 461b29b5ab3SAppaRao Puli // Create temporary file for write 462b29b5ab3SAppaRao Puli std::string pwdFile(passwdFileName); 463b29b5ab3SAppaRao Puli std::vector<char> tempFileName(pwdFile.begin(), pwdFile.end()); 464b29b5ab3SAppaRao Puli std::vector<char> fileTemplate = {'_', '_', 'X', 'X', 'X', 465b29b5ab3SAppaRao Puli 'X', 'X', 'X', '\0'}; 466b29b5ab3SAppaRao Puli tempFileName.insert(tempFileName.end(), fileTemplate.begin(), 467b29b5ab3SAppaRao Puli fileTemplate.end()); 468b29b5ab3SAppaRao Puli int fd = mkstemp((char*)tempFileName.data()); 469b29b5ab3SAppaRao Puli if (fd == -1) 470b29b5ab3SAppaRao Puli { 471b29b5ab3SAppaRao Puli log<level::DEBUG>("Error creating temp file"); 472b29b5ab3SAppaRao Puli return -EIO; 473b29b5ab3SAppaRao Puli } 474b29b5ab3SAppaRao Puli 475b29b5ab3SAppaRao Puli std::string strTempFileName(tempFileName.data()); 476b29b5ab3SAppaRao Puli // Open the temp file for writing from provided fd 477b29b5ab3SAppaRao Puli // By "true", remove it at exit if still there. 478b29b5ab3SAppaRao Puli // This is needed to cleanup the temp file at exception 479b29b5ab3SAppaRao Puli phosphor::user::File temp(fd, strTempFileName, "w", true); 480b29b5ab3SAppaRao Puli if ((temp)() == NULL) 481b29b5ab3SAppaRao Puli { 482b29b5ab3SAppaRao Puli close(fd); 483b29b5ab3SAppaRao Puli log<level::DEBUG>("Error creating temp file"); 484b29b5ab3SAppaRao Puli return -EIO; 485b29b5ab3SAppaRao Puli } 486b29b5ab3SAppaRao Puli 487b265455aSVernon Mauery // Set the file mode as read-write for owner only 488b265455aSVernon Mauery if (fchmod(fileno((temp)()), S_IRUSR | S_IWUSR) < 0) 489b29b5ab3SAppaRao Puli { 490b29b5ab3SAppaRao Puli log<level::DEBUG>("Error setting fchmod for temp file"); 491b29b5ab3SAppaRao Puli return -EIO; 492b29b5ab3SAppaRao Puli } 493b29b5ab3SAppaRao Puli 494b29b5ab3SAppaRao Puli const EVP_MD* digest = EVP_sha256(); 495b29b5ab3SAppaRao Puli size_t hashLen = EVP_MD_block_size(digest); 496b29b5ab3SAppaRao Puli std::vector<uint8_t> hash(hashLen); 497b29b5ab3SAppaRao Puli size_t ivLen = EVP_CIPHER_iv_length(cipher); 498b29b5ab3SAppaRao Puli std::vector<uint8_t> iv(ivLen); 499b29b5ab3SAppaRao Puli std::array<uint8_t, EVP_MAX_KEY_LENGTH> key; 500b29b5ab3SAppaRao Puli size_t keyLen = key.size(); 501b29b5ab3SAppaRao Puli std::array<uint8_t, EVP_MAX_MD_SIZE> mac; 502b29b5ab3SAppaRao Puli size_t macLen = mac.size(); 503b29b5ab3SAppaRao Puli 504b29b5ab3SAppaRao Puli // Create random hash and generate hash key which will be used for 505b29b5ab3SAppaRao Puli // encryption. 506b29b5ab3SAppaRao Puli if (RAND_bytes(hash.data(), hashLen) != 1) 507b29b5ab3SAppaRao Puli { 508b29b5ab3SAppaRao Puli log<level::DEBUG>("Hash genertion failed, bailing out"); 509b29b5ab3SAppaRao Puli return -EIO; 510b29b5ab3SAppaRao Puli } 511b29b5ab3SAppaRao Puli if (NULL == HMAC(digest, keyBuff.data(), keyBuff.size(), hash.data(), 512b29b5ab3SAppaRao Puli hashLen, key.data(), 513b29b5ab3SAppaRao Puli reinterpret_cast<unsigned int*>(&keyLen))) 514b29b5ab3SAppaRao Puli { 515b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to create MAC for authentication"); 516b29b5ab3SAppaRao Puli return -EIO; 517b29b5ab3SAppaRao Puli } 518b29b5ab3SAppaRao Puli 519b29b5ab3SAppaRao Puli // Generate IV values 520b29b5ab3SAppaRao Puli if (RAND_bytes(iv.data(), ivLen) != 1) 521b29b5ab3SAppaRao Puli { 522b29b5ab3SAppaRao Puli log<level::DEBUG>("UV genertion failed, bailing out"); 523b29b5ab3SAppaRao Puli return -EIO; 524b29b5ab3SAppaRao Puli } 525b29b5ab3SAppaRao Puli 526b29b5ab3SAppaRao Puli // Encrypt the input data 527b29b5ab3SAppaRao Puli std::vector<uint8_t> outBytes(inBytesLen + EVP_MAX_BLOCK_LENGTH); 528b29b5ab3SAppaRao Puli size_t outBytesLen = 0; 529b29b5ab3SAppaRao Puli if (inBytesLen != 0) 530b29b5ab3SAppaRao Puli { 5311e22a0f1SVernon Mauery if (encryptDecryptData( 5321e22a0f1SVernon Mauery true, EVP_aes_128_cbc(), key.data(), keyLen, iv.data(), ivLen, 5331e22a0f1SVernon Mauery reinterpret_cast<unsigned char*>(inBytes.data()), inBytesLen, 5341e22a0f1SVernon Mauery mac.data(), &macLen, outBytes.data(), &outBytesLen) != 0) 535b29b5ab3SAppaRao Puli { 536b29b5ab3SAppaRao Puli log<level::DEBUG>("Error while encrypting the data"); 537b29b5ab3SAppaRao Puli return -EIO; 538b29b5ab3SAppaRao Puli } 539b29b5ab3SAppaRao Puli outBytes[outBytesLen] = 0; 540b29b5ab3SAppaRao Puli } 541b29b5ab3SAppaRao Puli OPENSSL_cleanse(key.data(), keyLen); 542b29b5ab3SAppaRao Puli 543b29b5ab3SAppaRao Puli // Update the meta password structure. 54448e55585SRichard Marian Thomaiyar MetaPassStruct metaData = {META_PASSWD_SIG, {0, 0}, 0, 0, 0, 0, 0}; 545b29b5ab3SAppaRao Puli metaData.hashSize = hashLen; 546b29b5ab3SAppaRao Puli metaData.ivSize = ivLen; 547b29b5ab3SAppaRao Puli metaData.dataSize = bytesWritten; 548b29b5ab3SAppaRao Puli metaData.padSize = outBytesLen - bytesWritten; 549b29b5ab3SAppaRao Puli metaData.macSize = macLen; 550b29b5ab3SAppaRao Puli 551b29b5ab3SAppaRao Puli if (fwrite(&metaData, 1, sizeof(metaData), (temp)()) != sizeof(metaData)) 552b29b5ab3SAppaRao Puli { 553b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing meta data"); 554b29b5ab3SAppaRao Puli return -EIO; 555b29b5ab3SAppaRao Puli } 556b29b5ab3SAppaRao Puli 557b29b5ab3SAppaRao Puli if (fwrite(&hash[0], 1, hashLen, (temp)()) != hashLen) 558b29b5ab3SAppaRao Puli { 559b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing hash data"); 560b29b5ab3SAppaRao Puli return -EIO; 561b29b5ab3SAppaRao Puli } 562b29b5ab3SAppaRao Puli 563b29b5ab3SAppaRao Puli if (fwrite(&iv[0], 1, ivLen, (temp)()) != ivLen) 564b29b5ab3SAppaRao Puli { 565b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing IV data"); 566b29b5ab3SAppaRao Puli return -EIO; 567b29b5ab3SAppaRao Puli } 568b29b5ab3SAppaRao Puli 569b29b5ab3SAppaRao Puli if (fwrite(&outBytes[0], 1, outBytesLen, (temp)()) != outBytesLen) 570b29b5ab3SAppaRao Puli { 571b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing encrypted data"); 572b29b5ab3SAppaRao Puli return -EIO; 573b29b5ab3SAppaRao Puli } 574b29b5ab3SAppaRao Puli 575b29b5ab3SAppaRao Puli if (fwrite(&mac[0], 1, macLen, (temp)()) != macLen) 576b29b5ab3SAppaRao Puli { 577b29b5ab3SAppaRao Puli log<level::DEBUG>("Error in writing MAC data"); 578b29b5ab3SAppaRao Puli return -EIO; 579b29b5ab3SAppaRao Puli } 580b29b5ab3SAppaRao Puli 581b29b5ab3SAppaRao Puli if (fflush((temp)())) 582b29b5ab3SAppaRao Puli { 583b29b5ab3SAppaRao Puli log<level::DEBUG>( 584b29b5ab3SAppaRao Puli "File fflush error while writing entries to special file"); 585b29b5ab3SAppaRao Puli return -EIO; 586b29b5ab3SAppaRao Puli } 587b29b5ab3SAppaRao Puli 588b29b5ab3SAppaRao Puli OPENSSL_cleanse(iv.data(), ivLen); 589b29b5ab3SAppaRao Puli 590b29b5ab3SAppaRao Puli // Rename the tmp file to actual file 591b29b5ab3SAppaRao Puli if (std::rename(strTempFileName.data(), passwdFileName) != 0) 592b29b5ab3SAppaRao Puli { 593b29b5ab3SAppaRao Puli log<level::DEBUG>("Failed to rename tmp file to ipmi-pass"); 594b29b5ab3SAppaRao Puli return -EIO; 595b29b5ab3SAppaRao Puli } 596b29b5ab3SAppaRao Puli 597b29b5ab3SAppaRao Puli return 0; 598b29b5ab3SAppaRao Puli } 599b29b5ab3SAppaRao Puli 600b29b5ab3SAppaRao Puli std::time_t PasswdMgr::getUpdatedFileTime() 601b29b5ab3SAppaRao Puli { 6024654d99fSRichard Marian Thomaiyar struct stat fileStat = {}; 6034654d99fSRichard Marian Thomaiyar if (stat(passwdFileName, &fileStat) != 0) 6044654d99fSRichard Marian Thomaiyar { 605b29b5ab3SAppaRao Puli log<level::DEBUG>("Error - Getting passwd file time stamp"); 606b29b5ab3SAppaRao Puli return -EIO; 6074654d99fSRichard Marian Thomaiyar } 608b29b5ab3SAppaRao Puli return fileStat.st_mtime; 6094654d99fSRichard Marian Thomaiyar } 6104654d99fSRichard Marian Thomaiyar 6114654d99fSRichard Marian Thomaiyar } // namespace ipmi 612