xref: /openbmc/phosphor-host-ipmid/user_channel/passwd_mgr.cpp (revision fbc6c9d743d22682f3753841071b8460ad50b5d9)
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 
29*fbc6c9d7SPatrick Williams #include <phosphor-logging/log.hpp>
30*fbc6c9d7SPatrick 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         {
786ba8d315SRichard Marian Thomaiyar             chmod(passwdFileName, S_IRUSR | S_IWUSR);
796ba8d315SRichard Marian Thomaiyar         }
806ba8d315SRichard Marian Thomaiyar     }
816ba8d315SRichard Marian Thomaiyar 
826ba8d315SRichard Marian Thomaiyar     if (stat(encryptKeyFileName, &st) == 0)
836ba8d315SRichard Marian Thomaiyar     {
846ba8d315SRichard Marian Thomaiyar         if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR))
856ba8d315SRichard Marian Thomaiyar         {
866ba8d315SRichard Marian Thomaiyar             chmod(encryptKeyFileName, S_IRUSR | S_IWUSR);
876ba8d315SRichard Marian Thomaiyar         }
886ba8d315SRichard Marian Thomaiyar     }
896ba8d315SRichard Marian Thomaiyar }
906ba8d315SRichard Marian Thomaiyar 
911e22a0f1SVernon Mauery SecureString PasswdMgr::getPasswdByUserName(const std::string& userName)
924654d99fSRichard Marian Thomaiyar {
934654d99fSRichard Marian Thomaiyar     checkAndReload();
944654d99fSRichard Marian Thomaiyar     auto iter = passwdMapList.find(userName);
954654d99fSRichard Marian Thomaiyar     if (iter == passwdMapList.end())
964654d99fSRichard Marian Thomaiyar     {
971e22a0f1SVernon Mauery         return SecureString();
984654d99fSRichard Marian Thomaiyar     }
994654d99fSRichard Marian Thomaiyar     return iter->second;
1004654d99fSRichard Marian Thomaiyar }
1014654d99fSRichard Marian Thomaiyar 
10242bed64dSRichard Marian Thomaiyar int PasswdMgr::updateUserEntry(const std::string& userName,
10342bed64dSRichard Marian Thomaiyar                                const std::string& newUserName)
104b29b5ab3SAppaRao Puli {
105b29b5ab3SAppaRao Puli     std::time_t updatedTime = getUpdatedFileTime();
106b29b5ab3SAppaRao Puli     // Check file time stamp to know passwdMapList is up-to-date.
107b29b5ab3SAppaRao Puli     // If not up-to-date, then updatePasswdSpecialFile will read and
108b29b5ab3SAppaRao Puli     // check the user entry existance.
109b29b5ab3SAppaRao Puli     if (fileLastUpdatedTime == updatedTime && updatedTime != -EIO)
110b29b5ab3SAppaRao Puli     {
111b29b5ab3SAppaRao Puli         if (passwdMapList.find(userName) == passwdMapList.end())
112b29b5ab3SAppaRao Puli         {
113b29b5ab3SAppaRao Puli             log<level::DEBUG>("User not found");
114b29b5ab3SAppaRao Puli             return 0;
115b29b5ab3SAppaRao Puli         }
116b29b5ab3SAppaRao Puli     }
117b29b5ab3SAppaRao Puli 
118b29b5ab3SAppaRao Puli     // Write passwdMap to Encryted file
11942bed64dSRichard Marian Thomaiyar     if (updatePasswdSpecialFile(userName, newUserName) != 0)
120b29b5ab3SAppaRao Puli     {
121b29b5ab3SAppaRao Puli         log<level::DEBUG>("Passwd file update failed");
122b29b5ab3SAppaRao Puli         return -EIO;
123b29b5ab3SAppaRao Puli     }
124b29b5ab3SAppaRao Puli 
125b29b5ab3SAppaRao Puli     log<level::DEBUG>("Passwd file updated successfully");
126b29b5ab3SAppaRao Puli     return 0;
127b29b5ab3SAppaRao Puli }
128b29b5ab3SAppaRao Puli 
1294654d99fSRichard Marian Thomaiyar void PasswdMgr::checkAndReload(void)
1304654d99fSRichard Marian Thomaiyar {
131b29b5ab3SAppaRao Puli     std::time_t updatedTime = getUpdatedFileTime();
132b29b5ab3SAppaRao Puli     if (fileLastUpdatedTime != updatedTime && updatedTime != -1)
1334654d99fSRichard Marian Thomaiyar     {
1344654d99fSRichard Marian Thomaiyar         log<level::DEBUG>("Reloading password map list");
1354654d99fSRichard Marian Thomaiyar         passwdMapList.clear();
1364654d99fSRichard Marian Thomaiyar         initPasswordMap();
1374654d99fSRichard Marian Thomaiyar     }
1384654d99fSRichard Marian Thomaiyar }
1394654d99fSRichard Marian Thomaiyar 
140b29b5ab3SAppaRao Puli int PasswdMgr::encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher,
141b29b5ab3SAppaRao Puli                                   uint8_t* key, size_t keyLen, uint8_t* iv,
142b29b5ab3SAppaRao Puli                                   size_t ivLen, uint8_t* inBytes,
143b29b5ab3SAppaRao Puli                                   size_t inBytesLen, uint8_t* mac,
144b29b5ab3SAppaRao Puli                                   size_t* macLen, unsigned char* outBytes,
145b29b5ab3SAppaRao Puli                                   size_t* outBytesLen)
1464654d99fSRichard Marian Thomaiyar {
1474654d99fSRichard Marian Thomaiyar     if (cipher == NULL || key == NULL || iv == NULL || inBytes == NULL ||
1484654d99fSRichard Marian Thomaiyar         outBytes == NULL || mac == NULL || inBytesLen == 0 ||
1494654d99fSRichard Marian Thomaiyar         (size_t)EVP_CIPHER_key_length(cipher) > keyLen ||
1504654d99fSRichard Marian Thomaiyar         (size_t)EVP_CIPHER_iv_length(cipher) > ivLen)
1514654d99fSRichard Marian Thomaiyar     {
1524654d99fSRichard Marian Thomaiyar         log<level::DEBUG>("Error Invalid Inputs");
153b29b5ab3SAppaRao Puli         return -EINVAL;
1544654d99fSRichard Marian Thomaiyar     }
1554654d99fSRichard Marian Thomaiyar 
156b29b5ab3SAppaRao Puli     if (!doEncrypt)
157b29b5ab3SAppaRao Puli     {
158b29b5ab3SAppaRao Puli         // verify MAC before decrypting the data.
1594654d99fSRichard Marian Thomaiyar         std::array<uint8_t, EVP_MAX_MD_SIZE> calMac;
1604654d99fSRichard Marian Thomaiyar         size_t calMacLen = calMac.size();
1614654d99fSRichard Marian Thomaiyar         // calculate MAC for the encrypted message.
1624654d99fSRichard Marian Thomaiyar         if (NULL == HMAC(EVP_sha256(), key, keyLen, inBytes, inBytesLen,
1634654d99fSRichard Marian Thomaiyar                          calMac.data(),
1644654d99fSRichard Marian Thomaiyar                          reinterpret_cast<unsigned int*>(&calMacLen)))
1654654d99fSRichard Marian Thomaiyar         {
1664654d99fSRichard Marian Thomaiyar             log<level::DEBUG>("Error: Failed to calculate MAC");
167b29b5ab3SAppaRao Puli             return -EIO;
1684654d99fSRichard Marian Thomaiyar         }
169b29b5ab3SAppaRao Puli         if (!((calMacLen == *macLen) &&
1704654d99fSRichard Marian Thomaiyar               (std::memcmp(calMac.data(), mac, calMacLen) == 0)))
1714654d99fSRichard Marian Thomaiyar         {
1724654d99fSRichard Marian Thomaiyar             log<level::DEBUG>("Authenticated message doesn't match");
173b29b5ab3SAppaRao Puli             return -EBADMSG;
174b29b5ab3SAppaRao Puli         }
1754654d99fSRichard Marian Thomaiyar     }
1764654d99fSRichard Marian Thomaiyar 
1774654d99fSRichard Marian Thomaiyar     std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ctx(
1784654d99fSRichard Marian Thomaiyar         EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
1794654d99fSRichard Marian Thomaiyar 
180bf30c8d3SP Dheeraj Srujan Kumar     if (!ctx)
181bf30c8d3SP Dheeraj Srujan Kumar     {
182bf30c8d3SP Dheeraj Srujan Kumar         log<level::DEBUG>("Error: EVP_CIPHER_CTX is NULL");
183bf30c8d3SP Dheeraj Srujan Kumar         return -ENOMEM;
184bf30c8d3SP Dheeraj Srujan Kumar     }
185bf30c8d3SP Dheeraj Srujan Kumar 
186a67caed7SP Dheeraj Srujan Kumar     EVP_CIPHER_CTX_set_padding(ctx.get(), 1);
187a67caed7SP Dheeraj Srujan Kumar 
188b29b5ab3SAppaRao Puli     // Set key & IV
189b29b5ab3SAppaRao Puli     int retval = EVP_CipherInit_ex(ctx.get(), cipher, NULL, key, iv,
190b29b5ab3SAppaRao Puli                                    static_cast<int>(doEncrypt));
1914654d99fSRichard Marian Thomaiyar     if (!retval)
1924654d99fSRichard Marian Thomaiyar     {
1934654d99fSRichard Marian Thomaiyar         log<level::DEBUG>("EVP_CipherInit_ex failed",
1944654d99fSRichard Marian Thomaiyar                           entry("RET_VAL=%d", retval));
195b29b5ab3SAppaRao Puli         return -EIO;
1964654d99fSRichard Marian Thomaiyar     }
1974654d99fSRichard Marian Thomaiyar 
1984654d99fSRichard Marian Thomaiyar     int outLen = 0, outEVPLen = 0;
1994654d99fSRichard Marian Thomaiyar     if ((retval = EVP_CipherUpdate(ctx.get(), outBytes + outLen, &outEVPLen,
2004654d99fSRichard Marian Thomaiyar                                    inBytes, inBytesLen)))
2014654d99fSRichard Marian Thomaiyar     {
2024654d99fSRichard Marian Thomaiyar         outLen += outEVPLen;
203*fbc6c9d7SPatrick Williams         if ((retval = EVP_CipherFinal(ctx.get(), outBytes + outLen,
204*fbc6c9d7SPatrick Williams                                       &outEVPLen)))
2054654d99fSRichard Marian Thomaiyar         {
2064654d99fSRichard Marian Thomaiyar             outLen += outEVPLen;
2074654d99fSRichard Marian Thomaiyar             *outBytesLen = outLen;
2084654d99fSRichard Marian Thomaiyar         }
2094654d99fSRichard Marian Thomaiyar         else
2104654d99fSRichard Marian Thomaiyar         {
2114654d99fSRichard Marian Thomaiyar             log<level::DEBUG>("EVP_CipherFinal fails",
2124654d99fSRichard Marian Thomaiyar                               entry("RET_VAL=%d", retval));
213b29b5ab3SAppaRao Puli             return -EIO;
2144654d99fSRichard Marian Thomaiyar         }
2154654d99fSRichard Marian Thomaiyar     }
2164654d99fSRichard Marian Thomaiyar     else
2174654d99fSRichard Marian Thomaiyar     {
2184654d99fSRichard Marian Thomaiyar         log<level::DEBUG>("EVP_CipherUpdate fails",
2194654d99fSRichard Marian Thomaiyar                           entry("RET_VAL=%d", retval));
220b29b5ab3SAppaRao Puli         return -EIO;
221b29b5ab3SAppaRao Puli     }
222b29b5ab3SAppaRao Puli 
223b29b5ab3SAppaRao Puli     if (doEncrypt)
224b29b5ab3SAppaRao Puli     {
225b29b5ab3SAppaRao Puli         // Create MAC for the encrypted message
226b29b5ab3SAppaRao Puli         if (NULL == HMAC(EVP_sha256(), key, keyLen, outBytes, *outBytesLen, mac,
227b29b5ab3SAppaRao Puli                          reinterpret_cast<unsigned int*>(macLen)))
228b29b5ab3SAppaRao Puli         {
229b29b5ab3SAppaRao Puli             log<level::DEBUG>("Failed to create authentication");
230b29b5ab3SAppaRao Puli             return -EIO;
231b29b5ab3SAppaRao Puli         }
2324654d99fSRichard Marian Thomaiyar     }
2334654d99fSRichard Marian Thomaiyar     return 0;
2344654d99fSRichard Marian Thomaiyar }
2354654d99fSRichard Marian Thomaiyar 
2364654d99fSRichard Marian Thomaiyar void PasswdMgr::initPasswordMap(void)
2374654d99fSRichard Marian Thomaiyar {
2382f0ad74dSAndrew Geissler     // TODO  phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{};
2391e22a0f1SVernon Mauery     SecureString dataBuf;
2404654d99fSRichard Marian Thomaiyar 
241b29b5ab3SAppaRao Puli     if (readPasswdFileData(dataBuf) != 0)
2424654d99fSRichard Marian Thomaiyar     {
243b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in reading the encrypted pass file");
2444654d99fSRichard Marian Thomaiyar         return;
2454654d99fSRichard Marian Thomaiyar     }
2464654d99fSRichard Marian Thomaiyar 
247b29b5ab3SAppaRao Puli     if (dataBuf.size() != 0)
2484654d99fSRichard Marian Thomaiyar     {
2494654d99fSRichard Marian Thomaiyar         // populate the user list with password
2501e22a0f1SVernon Mauery         char* outPtr = dataBuf.data();
2514654d99fSRichard Marian Thomaiyar         char* nToken = NULL;
2524654d99fSRichard Marian Thomaiyar         char* linePtr = strtok_r(outPtr, "\n", &nToken);
25351d0c40aSPatrick Venture         size_t lineSize = 0;
2544654d99fSRichard Marian Thomaiyar         while (linePtr != NULL)
2554654d99fSRichard Marian Thomaiyar         {
25651d0c40aSPatrick Venture             size_t userEPos = 0;
2571e22a0f1SVernon Mauery             SecureString lineStr(linePtr);
2584654d99fSRichard Marian Thomaiyar             if ((userEPos = lineStr.find(":")) != std::string::npos)
2594654d99fSRichard Marian Thomaiyar             {
2604654d99fSRichard Marian Thomaiyar                 lineSize = lineStr.size();
2614654d99fSRichard Marian Thomaiyar                 passwdMapList.emplace(
2624654d99fSRichard Marian Thomaiyar                     lineStr.substr(0, userEPos),
2634654d99fSRichard Marian Thomaiyar                     lineStr.substr(userEPos + 1, lineSize - (userEPos + 1)));
2644654d99fSRichard Marian Thomaiyar             }
2654654d99fSRichard Marian Thomaiyar             linePtr = strtok_r(NULL, "\n", &nToken);
2664654d99fSRichard Marian Thomaiyar         }
267b29b5ab3SAppaRao Puli     }
268b29b5ab3SAppaRao Puli 
2694654d99fSRichard Marian Thomaiyar     // Update the timestamp
270b29b5ab3SAppaRao Puli     fileLastUpdatedTime = getUpdatedFileTime();
271b29b5ab3SAppaRao Puli     return;
272b29b5ab3SAppaRao Puli }
273b29b5ab3SAppaRao Puli 
2741e22a0f1SVernon Mauery int PasswdMgr::readPasswdFileData(SecureString& 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
3461e22a0f1SVernon Mauery     outBytes.resize(inBytesLen + EVP_MAX_BLOCK_LENGTH, '\0');
347b29b5ab3SAppaRao Puli     if (encryptDecryptData(false, EVP_aes_128_cbc(), key.data(), keyLen, iv,
348b29b5ab3SAppaRao Puli                            ivLen, inBytes, inBytesLen, mac, &macLen,
3491e22a0f1SVernon Mauery                            reinterpret_cast<unsigned char*>(outBytes.data()),
3501e22a0f1SVernon Mauery                            &outBytesLen) != 0)
351b29b5ab3SAppaRao Puli     {
352b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in decryption");
353b29b5ab3SAppaRao Puli         return -EIO;
354b29b5ab3SAppaRao Puli     }
355b29b5ab3SAppaRao Puli     // Resize the vector to outBytesLen
356b29b5ab3SAppaRao Puli     outBytes.resize(outBytesLen);
357b29b5ab3SAppaRao Puli 
358b29b5ab3SAppaRao Puli     OPENSSL_cleanse(key.data(), keyLen);
359b29b5ab3SAppaRao Puli     OPENSSL_cleanse(iv, ivLen);
360b29b5ab3SAppaRao Puli 
361b29b5ab3SAppaRao Puli     return 0;
362b29b5ab3SAppaRao Puli }
363b29b5ab3SAppaRao Puli 
36442bed64dSRichard Marian Thomaiyar int PasswdMgr::updatePasswdSpecialFile(const std::string& userName,
36542bed64dSRichard Marian Thomaiyar                                        const std::string& newUserName)
366b29b5ab3SAppaRao Puli {
3672f0ad74dSAndrew Geissler     // TODO  phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{};
368b29b5ab3SAppaRao Puli 
369b29b5ab3SAppaRao Puli     size_t bytesWritten = 0;
370b29b5ab3SAppaRao Puli     size_t inBytesLen = 0;
371b29b5ab3SAppaRao Puli     size_t isUsrFound = false;
372b29b5ab3SAppaRao Puli     const EVP_CIPHER* cipher = EVP_aes_128_cbc();
3731e22a0f1SVernon Mauery     SecureString dataBuf;
374b29b5ab3SAppaRao Puli 
375b29b5ab3SAppaRao Puli     // Read the encrypted file and get the file data
376b29b5ab3SAppaRao Puli     // Check user existance and return if not exist.
377b29b5ab3SAppaRao Puli     if (readPasswdFileData(dataBuf) != 0)
378b29b5ab3SAppaRao Puli     {
379b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in reading the encrypted pass file");
380b29b5ab3SAppaRao Puli         return -EIO;
381b29b5ab3SAppaRao Puli     }
382b29b5ab3SAppaRao Puli 
383b29b5ab3SAppaRao Puli     if (dataBuf.size() != 0)
384b29b5ab3SAppaRao Puli     {
385*fbc6c9d7SPatrick Williams         inBytesLen = dataBuf.size() + newUserName.size() +
386*fbc6c9d7SPatrick Williams                      EVP_CIPHER_block_size(cipher);
387b29b5ab3SAppaRao Puli     }
388b29b5ab3SAppaRao Puli 
3891e22a0f1SVernon Mauery     SecureString inBytes(inBytesLen, '\0');
390b29b5ab3SAppaRao Puli     if (inBytesLen != 0)
391b29b5ab3SAppaRao Puli     {
392b29b5ab3SAppaRao Puli         char* outPtr = reinterpret_cast<char*>(dataBuf.data());
393b29b5ab3SAppaRao Puli         char* nToken = NULL;
394b29b5ab3SAppaRao Puli         char* linePtr = strtok_r(outPtr, "\n", &nToken);
395b29b5ab3SAppaRao Puli         while (linePtr != NULL)
396b29b5ab3SAppaRao Puli         {
39751d0c40aSPatrick Venture             size_t userEPos = 0;
39851d0c40aSPatrick Venture 
3991e22a0f1SVernon Mauery             SecureString lineStr(linePtr);
400b29b5ab3SAppaRao Puli             if ((userEPos = lineStr.find(":")) != std::string::npos)
401b29b5ab3SAppaRao Puli             {
402b29b5ab3SAppaRao Puli                 if (userName.compare(lineStr.substr(0, userEPos)) == 0)
403b29b5ab3SAppaRao Puli                 {
404b29b5ab3SAppaRao Puli                     isUsrFound = true;
40542bed64dSRichard Marian Thomaiyar                     if (!newUserName.empty())
40642bed64dSRichard Marian Thomaiyar                     {
40742bed64dSRichard Marian Thomaiyar                         bytesWritten += std::snprintf(
4081e22a0f1SVernon Mauery                             &inBytes[0] + bytesWritten,
40942bed64dSRichard Marian Thomaiyar                             (inBytesLen - bytesWritten), "%s%s\n",
41042bed64dSRichard Marian Thomaiyar                             newUserName.c_str(),
41142bed64dSRichard Marian Thomaiyar                             lineStr.substr(userEPos, lineStr.size()).data());
41242bed64dSRichard Marian Thomaiyar                     }
413b29b5ab3SAppaRao Puli                 }
414b29b5ab3SAppaRao Puli                 else
415b29b5ab3SAppaRao Puli                 {
4161e22a0f1SVernon Mauery                     bytesWritten += std::snprintf(&inBytes[0] + bytesWritten,
4171e22a0f1SVernon Mauery                                                   (inBytesLen - bytesWritten),
4181e22a0f1SVernon Mauery                                                   "%s\n", lineStr.data());
419b29b5ab3SAppaRao Puli                 }
420b29b5ab3SAppaRao Puli             }
421b29b5ab3SAppaRao Puli             linePtr = strtok_r(NULL, "\n", &nToken);
422b29b5ab3SAppaRao Puli         }
423161f20d5SRichard Marian Thomaiyar         inBytesLen = bytesWritten;
424b29b5ab3SAppaRao Puli     }
425b29b5ab3SAppaRao Puli     if (!isUsrFound)
426b29b5ab3SAppaRao Puli     {
427b29b5ab3SAppaRao Puli         log<level::DEBUG>("User doesn't exist");
428b29b5ab3SAppaRao Puli         return 0;
429b29b5ab3SAppaRao Puli     }
430b29b5ab3SAppaRao Puli 
431b29b5ab3SAppaRao Puli     // Read the key buff from key file
432b29b5ab3SAppaRao Puli     std::array<uint8_t, maxKeySize> keyBuff;
433b29b5ab3SAppaRao Puli     std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary);
434b29b5ab3SAppaRao Puli     if (!keyFile.good())
435b29b5ab3SAppaRao Puli     {
436b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in opening encryption key file");
437b29b5ab3SAppaRao Puli         return -EIO;
438b29b5ab3SAppaRao Puli     }
439b29b5ab3SAppaRao Puli     keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size());
440b29b5ab3SAppaRao Puli     if (keyFile.fail())
441b29b5ab3SAppaRao Puli     {
442b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in reading encryption key file");
443b29b5ab3SAppaRao Puli         return -EIO;
444b29b5ab3SAppaRao Puli     }
445b29b5ab3SAppaRao Puli     keyFile.close();
446b29b5ab3SAppaRao Puli 
447b29b5ab3SAppaRao Puli     // Read the original passwd file mode
448b29b5ab3SAppaRao Puli     struct stat st = {};
449b29b5ab3SAppaRao Puli     if (stat(passwdFileName, &st) != 0)
450b29b5ab3SAppaRao Puli     {
451b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in getting password file fstat()");
452b29b5ab3SAppaRao Puli         return -EIO;
453b29b5ab3SAppaRao Puli     }
454b29b5ab3SAppaRao Puli 
455b29b5ab3SAppaRao Puli     // Create temporary file for write
456b29b5ab3SAppaRao Puli     std::string pwdFile(passwdFileName);
457b29b5ab3SAppaRao Puli     std::vector<char> tempFileName(pwdFile.begin(), pwdFile.end());
458b29b5ab3SAppaRao Puli     std::vector<char> fileTemplate = {'_', '_', 'X', 'X', 'X',
459b29b5ab3SAppaRao Puli                                       'X', 'X', 'X', '\0'};
460b29b5ab3SAppaRao Puli     tempFileName.insert(tempFileName.end(), fileTemplate.begin(),
461b29b5ab3SAppaRao Puli                         fileTemplate.end());
462b29b5ab3SAppaRao Puli     int fd = mkstemp((char*)tempFileName.data());
463b29b5ab3SAppaRao Puli     if (fd == -1)
464b29b5ab3SAppaRao Puli     {
465b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error creating temp file");
466b29b5ab3SAppaRao Puli         return -EIO;
467b29b5ab3SAppaRao Puli     }
468b29b5ab3SAppaRao Puli 
469b29b5ab3SAppaRao Puli     std::string strTempFileName(tempFileName.data());
470b29b5ab3SAppaRao Puli     // Open the temp file for writing from provided fd
471b29b5ab3SAppaRao Puli     // By "true", remove it at exit if still there.
472b29b5ab3SAppaRao Puli     // This is needed to cleanup the temp file at exception
473b29b5ab3SAppaRao Puli     phosphor::user::File temp(fd, strTempFileName, "w", true);
474b29b5ab3SAppaRao Puli     if ((temp)() == NULL)
475b29b5ab3SAppaRao Puli     {
476b29b5ab3SAppaRao Puli         close(fd);
477b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error creating temp file");
478b29b5ab3SAppaRao Puli         return -EIO;
479b29b5ab3SAppaRao Puli     }
480b29b5ab3SAppaRao Puli 
481b265455aSVernon Mauery     // Set the file mode as read-write for owner only
482b265455aSVernon Mauery     if (fchmod(fileno((temp)()), S_IRUSR | S_IWUSR) < 0)
483b29b5ab3SAppaRao Puli     {
484b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error setting fchmod for temp file");
485b29b5ab3SAppaRao Puli         return -EIO;
486b29b5ab3SAppaRao Puli     }
487b29b5ab3SAppaRao Puli 
488b29b5ab3SAppaRao Puli     const EVP_MD* digest = EVP_sha256();
489b29b5ab3SAppaRao Puli     size_t hashLen = EVP_MD_block_size(digest);
490b29b5ab3SAppaRao Puli     std::vector<uint8_t> hash(hashLen);
491b29b5ab3SAppaRao Puli     size_t ivLen = EVP_CIPHER_iv_length(cipher);
492b29b5ab3SAppaRao Puli     std::vector<uint8_t> iv(ivLen);
493b29b5ab3SAppaRao Puli     std::array<uint8_t, EVP_MAX_KEY_LENGTH> key;
494b29b5ab3SAppaRao Puli     size_t keyLen = key.size();
495b29b5ab3SAppaRao Puli     std::array<uint8_t, EVP_MAX_MD_SIZE> mac;
496b29b5ab3SAppaRao Puli     size_t macLen = mac.size();
497b29b5ab3SAppaRao Puli 
498b29b5ab3SAppaRao Puli     // Create random hash and generate hash key which will be used for
499b29b5ab3SAppaRao Puli     // encryption.
500b29b5ab3SAppaRao Puli     if (RAND_bytes(hash.data(), hashLen) != 1)
501b29b5ab3SAppaRao Puli     {
502b29b5ab3SAppaRao Puli         log<level::DEBUG>("Hash genertion failed, bailing out");
503b29b5ab3SAppaRao Puli         return -EIO;
504b29b5ab3SAppaRao Puli     }
505b29b5ab3SAppaRao Puli     if (NULL == HMAC(digest, keyBuff.data(), keyBuff.size(), hash.data(),
506b29b5ab3SAppaRao Puli                      hashLen, key.data(),
507b29b5ab3SAppaRao Puli                      reinterpret_cast<unsigned int*>(&keyLen)))
508b29b5ab3SAppaRao Puli     {
509b29b5ab3SAppaRao Puli         log<level::DEBUG>("Failed to create MAC for authentication");
510b29b5ab3SAppaRao Puli         return -EIO;
511b29b5ab3SAppaRao Puli     }
512b29b5ab3SAppaRao Puli 
513b29b5ab3SAppaRao Puli     // Generate IV values
514b29b5ab3SAppaRao Puli     if (RAND_bytes(iv.data(), ivLen) != 1)
515b29b5ab3SAppaRao Puli     {
516b29b5ab3SAppaRao Puli         log<level::DEBUG>("UV genertion failed, bailing out");
517b29b5ab3SAppaRao Puli         return -EIO;
518b29b5ab3SAppaRao Puli     }
519b29b5ab3SAppaRao Puli 
520b29b5ab3SAppaRao Puli     // Encrypt the input data
521b29b5ab3SAppaRao Puli     std::vector<uint8_t> outBytes(inBytesLen + EVP_MAX_BLOCK_LENGTH);
522b29b5ab3SAppaRao Puli     size_t outBytesLen = 0;
523b29b5ab3SAppaRao Puli     if (inBytesLen != 0)
524b29b5ab3SAppaRao Puli     {
5251e22a0f1SVernon Mauery         if (encryptDecryptData(
5261e22a0f1SVernon Mauery                 true, EVP_aes_128_cbc(), key.data(), keyLen, iv.data(), ivLen,
5271e22a0f1SVernon Mauery                 reinterpret_cast<unsigned char*>(inBytes.data()), inBytesLen,
5281e22a0f1SVernon Mauery                 mac.data(), &macLen, outBytes.data(), &outBytesLen) != 0)
529b29b5ab3SAppaRao Puli         {
530b29b5ab3SAppaRao Puli             log<level::DEBUG>("Error while encrypting the data");
531b29b5ab3SAppaRao Puli             return -EIO;
532b29b5ab3SAppaRao Puli         }
533b29b5ab3SAppaRao Puli         outBytes[outBytesLen] = 0;
534b29b5ab3SAppaRao Puli     }
535b29b5ab3SAppaRao Puli     OPENSSL_cleanse(key.data(), keyLen);
536b29b5ab3SAppaRao Puli 
537b29b5ab3SAppaRao Puli     // Update the meta password structure.
53848e55585SRichard Marian Thomaiyar     MetaPassStruct metaData = {META_PASSWD_SIG, {0, 0}, 0, 0, 0, 0, 0};
539b29b5ab3SAppaRao Puli     metaData.hashSize = hashLen;
540b29b5ab3SAppaRao Puli     metaData.ivSize = ivLen;
541b29b5ab3SAppaRao Puli     metaData.dataSize = bytesWritten;
542b29b5ab3SAppaRao Puli     metaData.padSize = outBytesLen - bytesWritten;
543b29b5ab3SAppaRao Puli     metaData.macSize = macLen;
544b29b5ab3SAppaRao Puli 
545b29b5ab3SAppaRao Puli     if (fwrite(&metaData, 1, sizeof(metaData), (temp)()) != sizeof(metaData))
546b29b5ab3SAppaRao Puli     {
547b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in writing meta data");
548b29b5ab3SAppaRao Puli         return -EIO;
549b29b5ab3SAppaRao Puli     }
550b29b5ab3SAppaRao Puli 
551b29b5ab3SAppaRao Puli     if (fwrite(&hash[0], 1, hashLen, (temp)()) != hashLen)
552b29b5ab3SAppaRao Puli     {
553b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in writing hash data");
554b29b5ab3SAppaRao Puli         return -EIO;
555b29b5ab3SAppaRao Puli     }
556b29b5ab3SAppaRao Puli 
557b29b5ab3SAppaRao Puli     if (fwrite(&iv[0], 1, ivLen, (temp)()) != ivLen)
558b29b5ab3SAppaRao Puli     {
559b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in writing IV data");
560b29b5ab3SAppaRao Puli         return -EIO;
561b29b5ab3SAppaRao Puli     }
562b29b5ab3SAppaRao Puli 
563b29b5ab3SAppaRao Puli     if (fwrite(&outBytes[0], 1, outBytesLen, (temp)()) != outBytesLen)
564b29b5ab3SAppaRao Puli     {
565b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in writing encrypted data");
566b29b5ab3SAppaRao Puli         return -EIO;
567b29b5ab3SAppaRao Puli     }
568b29b5ab3SAppaRao Puli 
569b29b5ab3SAppaRao Puli     if (fwrite(&mac[0], 1, macLen, (temp)()) != macLen)
570b29b5ab3SAppaRao Puli     {
571b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error in writing MAC data");
572b29b5ab3SAppaRao Puli         return -EIO;
573b29b5ab3SAppaRao Puli     }
574b29b5ab3SAppaRao Puli 
575b29b5ab3SAppaRao Puli     if (fflush((temp)()))
576b29b5ab3SAppaRao Puli     {
577b29b5ab3SAppaRao Puli         log<level::DEBUG>(
578b29b5ab3SAppaRao Puli             "File fflush error while writing entries to special file");
579b29b5ab3SAppaRao Puli         return -EIO;
580b29b5ab3SAppaRao Puli     }
581b29b5ab3SAppaRao Puli 
582b29b5ab3SAppaRao Puli     OPENSSL_cleanse(iv.data(), ivLen);
583b29b5ab3SAppaRao Puli 
584b29b5ab3SAppaRao Puli     // Rename the tmp  file to actual file
585b29b5ab3SAppaRao Puli     if (std::rename(strTempFileName.data(), passwdFileName) != 0)
586b29b5ab3SAppaRao Puli     {
587b29b5ab3SAppaRao Puli         log<level::DEBUG>("Failed to rename tmp file to ipmi-pass");
588b29b5ab3SAppaRao Puli         return -EIO;
589b29b5ab3SAppaRao Puli     }
590b29b5ab3SAppaRao Puli 
591b29b5ab3SAppaRao Puli     return 0;
592b29b5ab3SAppaRao Puli }
593b29b5ab3SAppaRao Puli 
594b29b5ab3SAppaRao Puli std::time_t PasswdMgr::getUpdatedFileTime()
595b29b5ab3SAppaRao Puli {
5964654d99fSRichard Marian Thomaiyar     struct stat fileStat = {};
5974654d99fSRichard Marian Thomaiyar     if (stat(passwdFileName, &fileStat) != 0)
5984654d99fSRichard Marian Thomaiyar     {
599b29b5ab3SAppaRao Puli         log<level::DEBUG>("Error - Getting passwd file time stamp");
600b29b5ab3SAppaRao Puli         return -EIO;
6014654d99fSRichard Marian Thomaiyar     }
602b29b5ab3SAppaRao Puli     return fileStat.st_mtime;
6034654d99fSRichard Marian Thomaiyar }
6044654d99fSRichard Marian Thomaiyar 
6054654d99fSRichard Marian Thomaiyar } // namespace ipmi
606