blob: 7b5b7ec8357defe2cbcacb79d96f54addfcfc649 [file] [log] [blame]
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +05301/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#include "passwd_mgr.hpp"
18
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053019#include "file.hpp"
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053020#include "shadowlock.hpp"
21
22#include <openssl/hmac.h>
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053023#include <openssl/rand.h>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053024#include <openssl/sha.h>
25#include <string.h>
26#include <sys/stat.h>
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053027#include <unistd.h>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053028
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050029#include <phosphor-logging/log.hpp>
30
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053031#include <cerrno>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053032#include <cstring>
33#include <fstream>
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053034#include <iomanip>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053035
36namespace ipmi
37{
38
39static const char* passwdFileName = "/etc/ipmi_pass";
40static const char* encryptKeyFileName = "/etc/key_file";
41static const size_t maxKeySize = 8;
42
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053043constexpr mode_t modeMask =
44 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
45
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053046#define META_PASSWD_SIG "=OPENBMC="
47
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053048/*
49 * Meta data struct for encrypted password file
50 */
Richard Marian Thomaiyar48e55582018-12-20 15:58:04 +053051struct MetaPassStruct
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053052{
53 char signature[10];
54 unsigned char reseved[2];
Tim Lee65a91682022-11-30 17:14:13 +080055 size_t hashSize;
56 size_t ivSize;
57 size_t dataSize;
58 size_t padSize;
59 size_t macSize;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053060};
61
62using namespace phosphor::logging;
63
64PasswdMgr::PasswdMgr()
65{
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053066 restrictFilesPermission();
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053067 initPasswordMap();
68}
69
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053070void PasswdMgr::restrictFilesPermission(void)
71{
72 struct stat st = {};
73 // Restrict file permission to owner read & write
74 if (stat(passwdFileName, &st) == 0)
75 {
76 if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR))
77 {
78 chmod(passwdFileName, S_IRUSR | S_IWUSR);
79 }
80 }
81
82 if (stat(encryptKeyFileName, &st) == 0)
83 {
84 if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR))
85 {
86 chmod(encryptKeyFileName, S_IRUSR | S_IWUSR);
87 }
88 }
89}
90
Vernon Mauery1e22a0f2021-07-30 13:36:54 -070091SecureString PasswdMgr::getPasswdByUserName(const std::string& userName)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053092{
93 checkAndReload();
94 auto iter = passwdMapList.find(userName);
95 if (iter == passwdMapList.end())
96 {
Vernon Mauery1e22a0f2021-07-30 13:36:54 -070097 return SecureString();
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053098 }
99 return iter->second;
100}
101
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530102int PasswdMgr::updateUserEntry(const std::string& userName,
103 const std::string& newUserName)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530104{
105 std::time_t updatedTime = getUpdatedFileTime();
106 // Check file time stamp to know passwdMapList is up-to-date.
107 // If not up-to-date, then updatePasswdSpecialFile will read and
108 // check the user entry existance.
109 if (fileLastUpdatedTime == updatedTime && updatedTime != -EIO)
110 {
111 if (passwdMapList.find(userName) == passwdMapList.end())
112 {
113 log<level::DEBUG>("User not found");
114 return 0;
115 }
116 }
117
118 // Write passwdMap to Encryted file
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530119 if (updatePasswdSpecialFile(userName, newUserName) != 0)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530120 {
121 log<level::DEBUG>("Passwd file update failed");
122 return -EIO;
123 }
124
125 log<level::DEBUG>("Passwd file updated successfully");
126 return 0;
127}
128
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530129void PasswdMgr::checkAndReload(void)
130{
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530131 std::time_t updatedTime = getUpdatedFileTime();
132 if (fileLastUpdatedTime != updatedTime && updatedTime != -1)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530133 {
134 log<level::DEBUG>("Reloading password map list");
135 passwdMapList.clear();
136 initPasswordMap();
137 }
138}
139
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530140int PasswdMgr::encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher,
141 uint8_t* key, size_t keyLen, uint8_t* iv,
142 size_t ivLen, uint8_t* inBytes,
143 size_t inBytesLen, uint8_t* mac,
144 size_t* macLen, unsigned char* outBytes,
145 size_t* outBytesLen)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530146{
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530147 if (cipher == NULL || key == NULL || iv == NULL || inBytes == NULL ||
148 outBytes == NULL || mac == NULL || inBytesLen == 0 ||
149 (size_t)EVP_CIPHER_key_length(cipher) > keyLen ||
150 (size_t)EVP_CIPHER_iv_length(cipher) > ivLen)
151 {
152 log<level::DEBUG>("Error Invalid Inputs");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530153 return -EINVAL;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530154 }
155
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530156 if (!doEncrypt)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530157 {
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530158 // verify MAC before decrypting the data.
159 std::array<uint8_t, EVP_MAX_MD_SIZE> calMac;
160 size_t calMacLen = calMac.size();
161 // calculate MAC for the encrypted message.
162 if (NULL == HMAC(EVP_sha256(), key, keyLen, inBytes, inBytesLen,
163 calMac.data(),
164 reinterpret_cast<unsigned int*>(&calMacLen)))
165 {
166 log<level::DEBUG>("Error: Failed to calculate MAC");
167 return -EIO;
168 }
169 if (!((calMacLen == *macLen) &&
170 (std::memcmp(calMac.data(), mac, calMacLen) == 0)))
171 {
172 log<level::DEBUG>("Authenticated message doesn't match");
173 return -EBADMSG;
174 }
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530175 }
176
177 std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ctx(
178 EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530179
P Dheeraj Srujan Kumarbf30c8d2021-07-20 04:06:37 +0530180 if (!ctx)
181 {
182 log<level::DEBUG>("Error: EVP_CIPHER_CTX is NULL");
183 return -ENOMEM;
184 }
185
P Dheeraj Srujan Kumara67caed2021-08-25 21:55:09 +0530186 EVP_CIPHER_CTX_set_padding(ctx.get(), 1);
187
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530188 // Set key & IV
189 int retval = EVP_CipherInit_ex(ctx.get(), cipher, NULL, key, iv,
190 static_cast<int>(doEncrypt));
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530191 if (!retval)
192 {
193 log<level::DEBUG>("EVP_CipherInit_ex failed",
194 entry("RET_VAL=%d", retval));
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530195 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530196 }
197
198 int outLen = 0, outEVPLen = 0;
199 if ((retval = EVP_CipherUpdate(ctx.get(), outBytes + outLen, &outEVPLen,
200 inBytes, inBytesLen)))
201 {
202 outLen += outEVPLen;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500203 if ((retval = EVP_CipherFinal(ctx.get(), outBytes + outLen,
204 &outEVPLen)))
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530205 {
206 outLen += outEVPLen;
207 *outBytesLen = outLen;
208 }
209 else
210 {
211 log<level::DEBUG>("EVP_CipherFinal fails",
212 entry("RET_VAL=%d", retval));
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530213 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530214 }
215 }
216 else
217 {
218 log<level::DEBUG>("EVP_CipherUpdate fails",
219 entry("RET_VAL=%d", retval));
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530220 return -EIO;
221 }
222
223 if (doEncrypt)
224 {
225 // Create MAC for the encrypted message
226 if (NULL == HMAC(EVP_sha256(), key, keyLen, outBytes, *outBytesLen, mac,
227 reinterpret_cast<unsigned int*>(macLen)))
228 {
229 log<level::DEBUG>("Failed to create authentication");
230 return -EIO;
231 }
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530232 }
233 return 0;
234}
235
236void PasswdMgr::initPasswordMap(void)
237{
Andrew Geissler2f0ad742021-05-14 13:39:15 -0500238 // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{};
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700239 SecureString dataBuf;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530240
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530241 if (readPasswdFileData(dataBuf) != 0)
242 {
243 log<level::DEBUG>("Error in reading the encrypted pass file");
244 return;
245 }
246
247 if (dataBuf.size() != 0)
248 {
249 // populate the user list with password
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700250 char* outPtr = dataBuf.data();
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530251 char* nToken = NULL;
252 char* linePtr = strtok_r(outPtr, "\n", &nToken);
Patrick Venture51d0c402019-08-19 11:19:19 -0700253 size_t lineSize = 0;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530254 while (linePtr != NULL)
255 {
Patrick Venture51d0c402019-08-19 11:19:19 -0700256 size_t userEPos = 0;
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700257 SecureString lineStr(linePtr);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530258 if ((userEPos = lineStr.find(":")) != std::string::npos)
259 {
260 lineSize = lineStr.size();
261 passwdMapList.emplace(
262 lineStr.substr(0, userEPos),
263 lineStr.substr(userEPos + 1, lineSize - (userEPos + 1)));
264 }
265 linePtr = strtok_r(NULL, "\n", &nToken);
266 }
267 }
268
269 // Update the timestamp
270 fileLastUpdatedTime = getUpdatedFileTime();
271 return;
272}
273
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700274int PasswdMgr::readPasswdFileData(SecureString& outBytes)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530275{
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530276 std::array<uint8_t, maxKeySize> keyBuff;
277 std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary);
278 if (!keyFile.is_open())
279 {
280 log<level::DEBUG>("Error in opening encryption key file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530281 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530282 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530283 keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size());
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530284 if (keyFile.fail())
285 {
286 log<level::DEBUG>("Error in reading encryption key file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530287 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530288 }
289
290 std::ifstream passwdFile(passwdFileName, std::ios::in | std::ios::binary);
291 if (!passwdFile.is_open())
292 {
293 log<level::DEBUG>("Error in opening ipmi password file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530294 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530295 }
296
297 // calculate file size and read the data
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530298 passwdFile.seekg(0, std::ios::end);
299 ssize_t fileSize = passwdFile.tellg();
300 passwdFile.seekg(0, std::ios::beg);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530301 std::vector<uint8_t> input(fileSize);
302 passwdFile.read(reinterpret_cast<char*>(input.data()), fileSize);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530303 if (passwdFile.fail())
304 {
305 log<level::DEBUG>("Error in reading encryption key file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530306 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530307 }
308
309 // verify the signature first
Richard Marian Thomaiyar48e55582018-12-20 15:58:04 +0530310 MetaPassStruct* metaData = reinterpret_cast<MetaPassStruct*>(input.data());
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530311 if (std::strncmp(metaData->signature, META_PASSWD_SIG,
312 sizeof(metaData->signature)))
313 {
314 log<level::DEBUG>("Error signature mismatch in password file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530315 return -EBADMSG;
316 }
317
318 size_t inBytesLen = metaData->dataSize + metaData->padSize;
319 // If data is empty i.e no password map then return success
320 if (inBytesLen == 0)
321 {
322 log<level::DEBUG>("Empty password file");
323 return 0;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530324 }
325
326 // compute the key needed to decrypt
327 std::array<uint8_t, EVP_MAX_KEY_LENGTH> key;
328 auto keyLen = key.size();
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530329 if (NULL == HMAC(EVP_sha256(), keyBuff.data(), keyBuff.size(),
330 input.data() + sizeof(*metaData), metaData->hashSize,
331 key.data(), reinterpret_cast<unsigned int*>(&keyLen)))
332 {
333 log<level::DEBUG>("Failed to create MAC for authentication");
334 return -EIO;
335 }
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530336
337 // decrypt the data
338 uint8_t* iv = input.data() + sizeof(*metaData) + metaData->hashSize;
339 size_t ivLen = metaData->ivSize;
340 uint8_t* inBytes = iv + ivLen;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530341 uint8_t* mac = inBytes + inBytesLen;
342 size_t macLen = metaData->macSize;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530343
344 size_t outBytesLen = 0;
345 // Resize to actual data size
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700346 outBytes.resize(inBytesLen + EVP_MAX_BLOCK_LENGTH, '\0');
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530347 if (encryptDecryptData(false, EVP_aes_128_cbc(), key.data(), keyLen, iv,
348 ivLen, inBytes, inBytesLen, mac, &macLen,
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700349 reinterpret_cast<unsigned char*>(outBytes.data()),
350 &outBytesLen) != 0)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530351 {
352 log<level::DEBUG>("Error in decryption");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530353 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530354 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530355 // Resize the vector to outBytesLen
356 outBytes.resize(outBytesLen);
357
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530358 OPENSSL_cleanse(key.data(), keyLen);
359 OPENSSL_cleanse(iv, ivLen);
360
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530361 return 0;
362}
363
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530364int PasswdMgr::updatePasswdSpecialFile(const std::string& userName,
365 const std::string& newUserName)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530366{
Andrew Geissler2f0ad742021-05-14 13:39:15 -0500367 // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{};
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530368
369 size_t bytesWritten = 0;
370 size_t inBytesLen = 0;
371 size_t isUsrFound = false;
372 const EVP_CIPHER* cipher = EVP_aes_128_cbc();
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700373 SecureString dataBuf;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530374
375 // Read the encrypted file and get the file data
376 // Check user existance and return if not exist.
377 if (readPasswdFileData(dataBuf) != 0)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530378 {
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530379 log<level::DEBUG>("Error in reading the encrypted pass file");
380 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530381 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530382
383 if (dataBuf.size() != 0)
384 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500385 inBytesLen = dataBuf.size() + newUserName.size() +
386 EVP_CIPHER_block_size(cipher);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530387 }
388
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700389 SecureString inBytes(inBytesLen, '\0');
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530390 if (inBytesLen != 0)
391 {
392 char* outPtr = reinterpret_cast<char*>(dataBuf.data());
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530393 char* nToken = NULL;
394 char* linePtr = strtok_r(outPtr, "\n", &nToken);
395 while (linePtr != NULL)
396 {
Patrick Venture51d0c402019-08-19 11:19:19 -0700397 size_t userEPos = 0;
398
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700399 SecureString lineStr(linePtr);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530400 if ((userEPos = lineStr.find(":")) != std::string::npos)
401 {
402 if (userName.compare(lineStr.substr(0, userEPos)) == 0)
403 {
404 isUsrFound = true;
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530405 if (!newUserName.empty())
406 {
407 bytesWritten += std::snprintf(
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700408 &inBytes[0] + bytesWritten,
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530409 (inBytesLen - bytesWritten), "%s%s\n",
410 newUserName.c_str(),
411 lineStr.substr(userEPos, lineStr.size()).data());
412 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530413 }
414 else
415 {
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700416 bytesWritten += std::snprintf(&inBytes[0] + bytesWritten,
417 (inBytesLen - bytesWritten),
418 "%s\n", lineStr.data());
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530419 }
420 }
421 linePtr = strtok_r(NULL, "\n", &nToken);
422 }
Richard Marian Thomaiyar161f20d2019-01-28 20:33:16 +0530423 inBytesLen = bytesWritten;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530424 }
425 if (!isUsrFound)
426 {
427 log<level::DEBUG>("User doesn't exist");
428 return 0;
429 }
430
431 // Read the key buff from key file
432 std::array<uint8_t, maxKeySize> keyBuff;
433 std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary);
434 if (!keyFile.good())
435 {
436 log<level::DEBUG>("Error in opening encryption key file");
437 return -EIO;
438 }
439 keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size());
440 if (keyFile.fail())
441 {
442 log<level::DEBUG>("Error in reading encryption key file");
443 return -EIO;
444 }
445 keyFile.close();
446
447 // Read the original passwd file mode
448 struct stat st = {};
449 if (stat(passwdFileName, &st) != 0)
450 {
451 log<level::DEBUG>("Error in getting password file fstat()");
452 return -EIO;
453 }
454
455 // Create temporary file for write
456 std::string pwdFile(passwdFileName);
457 std::vector<char> tempFileName(pwdFile.begin(), pwdFile.end());
458 std::vector<char> fileTemplate = {'_', '_', 'X', 'X', 'X',
459 'X', 'X', 'X', '\0'};
460 tempFileName.insert(tempFileName.end(), fileTemplate.begin(),
461 fileTemplate.end());
462 int fd = mkstemp((char*)tempFileName.data());
463 if (fd == -1)
464 {
465 log<level::DEBUG>("Error creating temp file");
466 return -EIO;
467 }
468
469 std::string strTempFileName(tempFileName.data());
470 // Open the temp file for writing from provided fd
471 // By "true", remove it at exit if still there.
472 // This is needed to cleanup the temp file at exception
473 phosphor::user::File temp(fd, strTempFileName, "w", true);
474 if ((temp)() == NULL)
475 {
476 close(fd);
477 log<level::DEBUG>("Error creating temp file");
478 return -EIO;
479 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530480
Vernon Maueryb2654552020-04-03 14:28:53 -0700481 // Set the file mode as read-write for owner only
482 if (fchmod(fileno((temp)()), S_IRUSR | S_IWUSR) < 0)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530483 {
484 log<level::DEBUG>("Error setting fchmod for temp file");
485 return -EIO;
486 }
487
488 const EVP_MD* digest = EVP_sha256();
489 size_t hashLen = EVP_MD_block_size(digest);
490 std::vector<uint8_t> hash(hashLen);
491 size_t ivLen = EVP_CIPHER_iv_length(cipher);
492 std::vector<uint8_t> iv(ivLen);
493 std::array<uint8_t, EVP_MAX_KEY_LENGTH> key;
494 size_t keyLen = key.size();
495 std::array<uint8_t, EVP_MAX_MD_SIZE> mac;
496 size_t macLen = mac.size();
497
498 // Create random hash and generate hash key which will be used for
499 // encryption.
500 if (RAND_bytes(hash.data(), hashLen) != 1)
501 {
502 log<level::DEBUG>("Hash genertion failed, bailing out");
503 return -EIO;
504 }
505 if (NULL == HMAC(digest, keyBuff.data(), keyBuff.size(), hash.data(),
506 hashLen, key.data(),
507 reinterpret_cast<unsigned int*>(&keyLen)))
508 {
509 log<level::DEBUG>("Failed to create MAC for authentication");
510 return -EIO;
511 }
512
513 // Generate IV values
514 if (RAND_bytes(iv.data(), ivLen) != 1)
515 {
516 log<level::DEBUG>("UV genertion failed, bailing out");
517 return -EIO;
518 }
519
520 // Encrypt the input data
521 std::vector<uint8_t> outBytes(inBytesLen + EVP_MAX_BLOCK_LENGTH);
522 size_t outBytesLen = 0;
523 if (inBytesLen != 0)
524 {
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700525 if (encryptDecryptData(
526 true, EVP_aes_128_cbc(), key.data(), keyLen, iv.data(), ivLen,
527 reinterpret_cast<unsigned char*>(inBytes.data()), inBytesLen,
528 mac.data(), &macLen, outBytes.data(), &outBytesLen) != 0)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530529 {
530 log<level::DEBUG>("Error while encrypting the data");
531 return -EIO;
532 }
533 outBytes[outBytesLen] = 0;
534 }
535 OPENSSL_cleanse(key.data(), keyLen);
536
537 // Update the meta password structure.
Richard Marian Thomaiyar48e55582018-12-20 15:58:04 +0530538 MetaPassStruct metaData = {META_PASSWD_SIG, {0, 0}, 0, 0, 0, 0, 0};
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530539 metaData.hashSize = hashLen;
540 metaData.ivSize = ivLen;
541 metaData.dataSize = bytesWritten;
542 metaData.padSize = outBytesLen - bytesWritten;
543 metaData.macSize = macLen;
544
545 if (fwrite(&metaData, 1, sizeof(metaData), (temp)()) != sizeof(metaData))
546 {
547 log<level::DEBUG>("Error in writing meta data");
548 return -EIO;
549 }
550
551 if (fwrite(&hash[0], 1, hashLen, (temp)()) != hashLen)
552 {
553 log<level::DEBUG>("Error in writing hash data");
554 return -EIO;
555 }
556
557 if (fwrite(&iv[0], 1, ivLen, (temp)()) != ivLen)
558 {
559 log<level::DEBUG>("Error in writing IV data");
560 return -EIO;
561 }
562
563 if (fwrite(&outBytes[0], 1, outBytesLen, (temp)()) != outBytesLen)
564 {
565 log<level::DEBUG>("Error in writing encrypted data");
566 return -EIO;
567 }
568
569 if (fwrite(&mac[0], 1, macLen, (temp)()) != macLen)
570 {
571 log<level::DEBUG>("Error in writing MAC data");
572 return -EIO;
573 }
574
575 if (fflush((temp)()))
576 {
577 log<level::DEBUG>(
578 "File fflush error while writing entries to special file");
579 return -EIO;
580 }
581
582 OPENSSL_cleanse(iv.data(), ivLen);
583
584 // Rename the tmp file to actual file
585 if (std::rename(strTempFileName.data(), passwdFileName) != 0)
586 {
587 log<level::DEBUG>("Failed to rename tmp file to ipmi-pass");
588 return -EIO;
589 }
590
591 return 0;
592}
593
594std::time_t PasswdMgr::getUpdatedFileTime()
595{
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530596 struct stat fileStat = {};
597 if (stat(passwdFileName, &fileStat) != 0)
598 {
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530599 log<level::DEBUG>("Error - Getting passwd file time stamp");
600 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530601 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530602 return fileStat.st_mtime;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530603}
604
605} // namespace ipmi