Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 1 | /* |
| 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 | #pragma once |
| 17 | #include <openssl/evp.h> |
| 18 | |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 19 | #include <ipmid/types.hpp> |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 20 | |
| 21 | #include <ctime> |
Andrew Geissler | ecc0342 | 2020-05-16 15:08:22 -0500 | [diff] [blame] | 22 | #include <string> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 23 | #include <unordered_map> |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 24 | #include <vector> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 25 | |
| 26 | namespace ipmi |
| 27 | { |
| 28 | |
| 29 | class PasswdMgr |
| 30 | { |
| 31 | public: |
| 32 | ~PasswdMgr() = default; |
| 33 | PasswdMgr(const PasswdMgr&) = delete; |
| 34 | PasswdMgr& operator=(const PasswdMgr&) = delete; |
| 35 | PasswdMgr(PasswdMgr&&) = delete; |
| 36 | PasswdMgr& operator=(PasswdMgr&&) = delete; |
| 37 | |
| 38 | /** @brief Constructs user password list |
| 39 | * |
| 40 | */ |
| 41 | PasswdMgr(); |
| 42 | |
| 43 | /** @brief Get password for the user |
| 44 | * |
| 45 | * @param[in] userName - user name |
| 46 | * |
| 47 | * @return password string. will return empty string, if unable to locate |
| 48 | * the user |
| 49 | */ |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 50 | SecureString getPasswdByUserName(const std::string& userName); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 51 | |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 52 | /** @brief Update / clear username and password entry for the specified |
| 53 | * user |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 54 | * |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 55 | * @param[in] userName - user name that has to be renamed / deleted |
| 56 | * @param[in] newUserName - new user name. If empty, userName will be |
| 57 | * deleted. |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 58 | * |
| 59 | * @return error response |
| 60 | */ |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 61 | int updateUserEntry(const std::string& userName, |
| 62 | const std::string& newUserName); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 63 | |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 64 | private: |
| 65 | using UserName = std::string; |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 66 | using Password = SecureString; |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 67 | std::unordered_map<UserName, Password> passwdMapList; |
| 68 | std::time_t fileLastUpdatedTime; |
Richard Marian Thomaiyar | 6ba8d31 | 2020-04-10 23:52:50 +0530 | [diff] [blame] | 69 | |
| 70 | /** @brief restrict file permission |
| 71 | * |
| 72 | */ |
| 73 | void restrictFilesPermission(void); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 74 | /** @brief check timestamp and reload password map if required |
| 75 | * |
| 76 | */ |
| 77 | void checkAndReload(void); |
| 78 | /** @brief initializes passwdMapList by reading the encrypted file |
| 79 | * |
| 80 | * Initializes the passwordMapList members after decrypting the |
| 81 | * password file. passwordMapList will be used further in IPMI |
| 82 | * authentication. |
| 83 | */ |
| 84 | void initPasswordMap(void); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 85 | |
| 86 | /** @brief Function to read the encrypted password file data |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 87 | * |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 88 | * @param[out] outBytes - vector to hold decrypted password file data |
| 89 | * |
| 90 | * @return error response |
| 91 | */ |
Vernon Mauery | 1e22a0f | 2021-07-30 13:36:54 -0700 | [diff] [blame] | 92 | int readPasswdFileData(SecureString& outBytes); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 93 | /** @brief Updates special password file by clearing the password entry |
| 94 | * for the user specified. |
| 95 | * |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 96 | * @param[in] userName - user name that has to be renamed / deleted |
| 97 | * @param[in] newUserName - new user name. If empty, userName will be |
| 98 | * deleted. |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 99 | * |
| 100 | * @return error response |
| 101 | */ |
Richard Marian Thomaiyar | 42bed64 | 2018-09-21 12:28:57 +0530 | [diff] [blame] | 102 | int updatePasswdSpecialFile(const std::string& userName, |
| 103 | const std::string& newUserName); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 104 | /** @brief encrypts or decrypt the data provided |
| 105 | * |
| 106 | * @param[in] doEncrypt - do encrypt if set to true, else do decrypt. |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 107 | * @param[in] cipher - cipher to be used |
| 108 | * @param[in] key - pointer to the key |
| 109 | * @param[in] keyLen - Length of the key to be used |
| 110 | * @param[in] iv - pointer to initialization vector |
| 111 | * @param[in] ivLen - Length of the iv |
| 112 | * @param[in] inBytes - input data to be encrypted / decrypted |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 113 | * @param[in] inBytesLen - input size to be encrypted / decrypted |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 114 | * @param[in] mac - message authentication code - to figure out corruption |
| 115 | * @param[in] macLen - size of MAC |
| 116 | * @param[in] outBytes - ptr to store output bytes |
| 117 | * @param[in] outBytesLen - outbut data length. |
| 118 | * |
| 119 | * @return error response |
| 120 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 121 | int encryptDecryptData( |
| 122 | bool doEncrypt, const EVP_CIPHER* cipher, uint8_t* key, size_t keyLen, |
| 123 | uint8_t* iv, size_t ivLen, uint8_t* inBytes, size_t inBytesLen, |
| 124 | uint8_t* mac, size_t* macLen, uint8_t* outBytes, size_t* outBytesLen); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame] | 125 | |
| 126 | /** @brief returns updated file time of passwd file entry. |
| 127 | * |
| 128 | * @return timestamp or -1 for error. |
| 129 | */ |
| 130 | std::time_t getUpdatedFileTime(); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace ipmi |