blob: c5323fea1a66989c3b75edabb93d2ac6e4e7efe9 [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
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053029#include <cerrno>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053030#include <cstring>
31#include <fstream>
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053032#include <iomanip>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053033#include <phosphor-logging/log.hpp>
34
35namespace ipmi
36{
37
38static const char* passwdFileName = "/etc/ipmi_pass";
39static const char* encryptKeyFileName = "/etc/key_file";
40static const size_t maxKeySize = 8;
41
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053042constexpr mode_t modeMask =
43 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
44
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053045#define META_PASSWD_SIG "=OPENBMC="
46
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053047/*
48 * Meta data struct for encrypted password file
49 */
Richard Marian Thomaiyar48e55582018-12-20 15:58:04 +053050struct MetaPassStruct
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053051{
52 char signature[10];
53 unsigned char reseved[2];
54 size_t hashSize;
55 size_t ivSize;
56 size_t dataSize;
57 size_t padSize;
58 size_t macSize;
59};
60
61using namespace phosphor::logging;
62
63PasswdMgr::PasswdMgr()
64{
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053065 restrictFilesPermission();
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053066 initPasswordMap();
67}
68
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053069void PasswdMgr::restrictFilesPermission(void)
70{
71 struct stat st = {};
72 // Restrict file permission to owner read & write
73 if (stat(passwdFileName, &st) == 0)
74 {
75 if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR))
76 {
77 chmod(passwdFileName, S_IRUSR | S_IWUSR);
78 }
79 }
80
81 if (stat(encryptKeyFileName, &st) == 0)
82 {
83 if ((st.st_mode & modeMask) != (S_IRUSR | S_IWUSR))
84 {
85 chmod(encryptKeyFileName, S_IRUSR | S_IWUSR);
86 }
87 }
88}
89
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053090std::string PasswdMgr::getPasswdByUserName(const std::string& userName)
91{
92 checkAndReload();
93 auto iter = passwdMapList.find(userName);
94 if (iter == passwdMapList.end())
95 {
96 return std::string();
97 }
98 return iter->second;
99}
100
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530101int PasswdMgr::updateUserEntry(const std::string& userName,
102 const std::string& newUserName)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530103{
104 std::time_t updatedTime = getUpdatedFileTime();
105 // Check file time stamp to know passwdMapList is up-to-date.
106 // If not up-to-date, then updatePasswdSpecialFile will read and
107 // check the user entry existance.
108 if (fileLastUpdatedTime == updatedTime && updatedTime != -EIO)
109 {
110 if (passwdMapList.find(userName) == passwdMapList.end())
111 {
112 log<level::DEBUG>("User not found");
113 return 0;
114 }
115 }
116
117 // Write passwdMap to Encryted file
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530118 if (updatePasswdSpecialFile(userName, newUserName) != 0)
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530119 {
120 log<level::DEBUG>("Passwd file update failed");
121 return -EIO;
122 }
123
124 log<level::DEBUG>("Passwd file updated successfully");
125 return 0;
126}
127
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530128void PasswdMgr::checkAndReload(void)
129{
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530130 std::time_t updatedTime = getUpdatedFileTime();
131 if (fileLastUpdatedTime != updatedTime && updatedTime != -1)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530132 {
133 log<level::DEBUG>("Reloading password map list");
134 passwdMapList.clear();
135 initPasswordMap();
136 }
137}
138
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530139int PasswdMgr::encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher,
140 uint8_t* key, size_t keyLen, uint8_t* iv,
141 size_t ivLen, uint8_t* inBytes,
142 size_t inBytesLen, uint8_t* mac,
143 size_t* macLen, unsigned char* outBytes,
144 size_t* outBytesLen)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530145{
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530146 if (cipher == NULL || key == NULL || iv == NULL || inBytes == NULL ||
147 outBytes == NULL || mac == NULL || inBytesLen == 0 ||
148 (size_t)EVP_CIPHER_key_length(cipher) > keyLen ||
149 (size_t)EVP_CIPHER_iv_length(cipher) > ivLen)
150 {
151 log<level::DEBUG>("Error Invalid Inputs");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530152 return -EINVAL;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530153 }
154
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530155 if (!doEncrypt)
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530156 {
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530157 // verify MAC before decrypting the data.
158 std::array<uint8_t, EVP_MAX_MD_SIZE> calMac;
159 size_t calMacLen = calMac.size();
160 // calculate MAC for the encrypted message.
161 if (NULL == HMAC(EVP_sha256(), key, keyLen, inBytes, inBytesLen,
162 calMac.data(),
163 reinterpret_cast<unsigned int*>(&calMacLen)))
164 {
165 log<level::DEBUG>("Error: Failed to calculate MAC");
166 return -EIO;
167 }
168 if (!((calMacLen == *macLen) &&
169 (std::memcmp(calMac.data(), mac, calMacLen) == 0)))
170 {
171 log<level::DEBUG>("Authenticated message doesn't match");
172 return -EBADMSG;
173 }
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530174 }
175
176 std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)> ctx(
177 EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530178
P Dheeraj Srujan Kumarbf30c8d2021-07-20 04:06:37 +0530179 if (!ctx)
180 {
181 log<level::DEBUG>("Error: EVP_CIPHER_CTX is NULL");
182 return -ENOMEM;
183 }
184
P Dheeraj Srujan Kumara67caed2021-08-25 21:55:09 +0530185 EVP_CIPHER_CTX_set_padding(ctx.get(), 1);
186
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530187 // Set key & IV
188 int retval = EVP_CipherInit_ex(ctx.get(), cipher, NULL, key, iv,
189 static_cast<int>(doEncrypt));
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530190 if (!retval)
191 {
192 log<level::DEBUG>("EVP_CipherInit_ex failed",
193 entry("RET_VAL=%d", retval));
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530194 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530195 }
196
197 int outLen = 0, outEVPLen = 0;
198 if ((retval = EVP_CipherUpdate(ctx.get(), outBytes + outLen, &outEVPLen,
199 inBytes, inBytesLen)))
200 {
201 outLen += outEVPLen;
202 if ((retval =
203 EVP_CipherFinal(ctx.get(), outBytes + outLen, &outEVPLen)))
204 {
205 outLen += outEVPLen;
206 *outBytesLen = outLen;
207 }
208 else
209 {
210 log<level::DEBUG>("EVP_CipherFinal fails",
211 entry("RET_VAL=%d", retval));
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530212 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530213 }
214 }
215 else
216 {
217 log<level::DEBUG>("EVP_CipherUpdate fails",
218 entry("RET_VAL=%d", retval));
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530219 return -EIO;
220 }
221
222 if (doEncrypt)
223 {
224 // Create MAC for the encrypted message
225 if (NULL == HMAC(EVP_sha256(), key, keyLen, outBytes, *outBytesLen, mac,
226 reinterpret_cast<unsigned int*>(macLen)))
227 {
228 log<level::DEBUG>("Failed to create authentication");
229 return -EIO;
230 }
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530231 }
232 return 0;
233}
234
235void PasswdMgr::initPasswordMap(void)
236{
Andrew Geissler2f0ad742021-05-14 13:39:15 -0500237 // TODO phosphor-host-ipmid#170 phosphor::user::shadow::Lock lock{};
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530238 std::vector<uint8_t> dataBuf;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530239
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530240 if (readPasswdFileData(dataBuf) != 0)
241 {
242 log<level::DEBUG>("Error in reading the encrypted pass file");
243 return;
244 }
245
246 if (dataBuf.size() != 0)
247 {
248 // populate the user list with password
249 char* outPtr = reinterpret_cast<char*>(dataBuf.data());
250 char* nToken = NULL;
251 char* linePtr = strtok_r(outPtr, "\n", &nToken);
Patrick Venture51d0c402019-08-19 11:19:19 -0700252 size_t lineSize = 0;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530253 while (linePtr != NULL)
254 {
Patrick Venture51d0c402019-08-19 11:19:19 -0700255 size_t userEPos = 0;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530256 std::string lineStr(linePtr);
257 if ((userEPos = lineStr.find(":")) != std::string::npos)
258 {
259 lineSize = lineStr.size();
260 passwdMapList.emplace(
261 lineStr.substr(0, userEPos),
262 lineStr.substr(userEPos + 1, lineSize - (userEPos + 1)));
263 }
264 linePtr = strtok_r(NULL, "\n", &nToken);
265 }
266 }
267
268 // Update the timestamp
269 fileLastUpdatedTime = getUpdatedFileTime();
Jayaprakash Mutyala70bd0632020-10-23 06:24:55 +0000270 // Clear sensitive data
271 OPENSSL_cleanse(dataBuf.data(), dataBuf.size());
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530272 return;
273}
274
275int PasswdMgr::readPasswdFileData(std::vector<uint8_t>& outBytes)
276{
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530277 std::array<uint8_t, maxKeySize> keyBuff;
278 std::ifstream keyFile(encryptKeyFileName, std::ios::in | std::ios::binary);
279 if (!keyFile.is_open())
280 {
281 log<level::DEBUG>("Error in opening encryption key file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530282 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530283 }
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530284 keyFile.read(reinterpret_cast<char*>(keyBuff.data()), keyBuff.size());
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530285 if (keyFile.fail())
286 {
287 log<level::DEBUG>("Error in reading encryption key file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530288 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530289 }
290
291 std::ifstream passwdFile(passwdFileName, std::ios::in | std::ios::binary);
292 if (!passwdFile.is_open())
293 {
294 log<level::DEBUG>("Error in opening ipmi password file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530295 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530296 }
297
298 // calculate file size and read the data
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530299 passwdFile.seekg(0, std::ios::end);
300 ssize_t fileSize = passwdFile.tellg();
301 passwdFile.seekg(0, std::ios::beg);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530302 std::vector<uint8_t> input(fileSize);
303 passwdFile.read(reinterpret_cast<char*>(input.data()), fileSize);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530304 if (passwdFile.fail())
305 {
306 log<level::DEBUG>("Error in reading encryption key file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530307 return -EIO;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530308 }
309
310 // verify the signature first
Richard Marian Thomaiyar48e55582018-12-20 15:58:04 +0530311 MetaPassStruct* metaData = reinterpret_cast<MetaPassStruct*>(input.data());
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530312 if (std::strncmp(metaData->signature, META_PASSWD_SIG,
313 sizeof(metaData->signature)))
314 {
315 log<level::DEBUG>("Error signature mismatch in password file");
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530316 return -EBADMSG;
317 }
318
319 size_t inBytesLen = metaData->dataSize + metaData->padSize;
320 // If data is empty i.e no password map then return success
321 if (inBytesLen == 0)
322 {
323 log<level::DEBUG>("Empty password file");
324 return 0;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530325 }
326
327 // compute the key needed to decrypt
328 std::array<uint8_t, EVP_MAX_KEY_LENGTH> key;
329 auto keyLen = key.size();
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530330 if (NULL == HMAC(EVP_sha256(), keyBuff.data(), keyBuff.size(),
331 input.data() + sizeof(*metaData), metaData->hashSize,
332 key.data(), reinterpret_cast<unsigned int*>(&keyLen)))
333 {
334 log<level::DEBUG>("Failed to create MAC for authentication");
335 return -EIO;
336 }
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530337
338 // decrypt the data
339 uint8_t* iv = input.data() + sizeof(*metaData) + metaData->hashSize;
340 size_t ivLen = metaData->ivSize;
341 uint8_t* inBytes = iv + ivLen;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530342 uint8_t* mac = inBytes + inBytesLen;
343 size_t macLen = metaData->macSize;
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530344
345 size_t outBytesLen = 0;
346 // Resize to actual data size
347 outBytes.resize(inBytesLen + EVP_MAX_BLOCK_LENGTH);
348 if (encryptDecryptData(false, EVP_aes_128_cbc(), key.data(), keyLen, iv,
349 ivLen, inBytes, inBytesLen, mac, &macLen,
350 outBytes.data(), &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();
373 std::vector<uint8_t> dataBuf;
374
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 {
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530385 inBytesLen =
386 dataBuf.size() + newUserName.size() + EVP_CIPHER_block_size(cipher);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530387 }
388
389 std::vector<uint8_t> inBytes(inBytesLen);
390 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
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530399 std::string lineStr(linePtr);
400 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(
408 reinterpret_cast<char*>(&inBytes[0]) + bytesWritten,
409 (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 {
416 bytesWritten += std::snprintf(
417 reinterpret_cast<char*>(&inBytes[0]) + bytesWritten,
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530418 (inBytesLen - bytesWritten), "%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 {
525 if (encryptDecryptData(true, EVP_aes_128_cbc(), key.data(), keyLen,
526 iv.data(), ivLen, inBytes.data(), inBytesLen,
527 mac.data(), &macLen, outBytes.data(),
528 &outBytesLen) != 0)
529 {
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