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 | |
| 19 | #include <ctime> |
| 20 | #include <unordered_map> |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame^] | 21 | #include <vector> |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 22 | |
| 23 | namespace ipmi |
| 24 | { |
| 25 | |
| 26 | class PasswdMgr |
| 27 | { |
| 28 | public: |
| 29 | ~PasswdMgr() = default; |
| 30 | PasswdMgr(const PasswdMgr&) = delete; |
| 31 | PasswdMgr& operator=(const PasswdMgr&) = delete; |
| 32 | PasswdMgr(PasswdMgr&&) = delete; |
| 33 | PasswdMgr& operator=(PasswdMgr&&) = delete; |
| 34 | |
| 35 | /** @brief Constructs user password list |
| 36 | * |
| 37 | */ |
| 38 | PasswdMgr(); |
| 39 | |
| 40 | /** @brief Get password for the user |
| 41 | * |
| 42 | * @param[in] userName - user name |
| 43 | * |
| 44 | * @return password string. will return empty string, if unable to locate |
| 45 | * the user |
| 46 | */ |
| 47 | std::string getPasswdByUserName(const std::string& userName); |
| 48 | |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame^] | 49 | /** @brief Clear username and password entry for the specified user |
| 50 | * |
| 51 | * @param[in] userName - username |
| 52 | * |
| 53 | * @return error response |
| 54 | */ |
| 55 | int clearUserEntry(const std::string& userName); |
| 56 | |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 57 | private: |
| 58 | using UserName = std::string; |
| 59 | using Password = std::string; |
| 60 | std::unordered_map<UserName, Password> passwdMapList; |
| 61 | std::time_t fileLastUpdatedTime; |
| 62 | /** @brief check timestamp and reload password map if required |
| 63 | * |
| 64 | */ |
| 65 | void checkAndReload(void); |
| 66 | /** @brief initializes passwdMapList by reading the encrypted file |
| 67 | * |
| 68 | * Initializes the passwordMapList members after decrypting the |
| 69 | * password file. passwordMapList will be used further in IPMI |
| 70 | * authentication. |
| 71 | */ |
| 72 | void initPasswordMap(void); |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame^] | 73 | |
| 74 | /** @brief Function to read the encrypted password file data |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 75 | * |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame^] | 76 | * @param[out] outBytes - vector to hold decrypted password file data |
| 77 | * |
| 78 | * @return error response |
| 79 | */ |
| 80 | int readPasswdFileData(std::vector<uint8_t>& outBytes); |
| 81 | /** @brief Updates special password file by clearing the password entry |
| 82 | * for the user specified. |
| 83 | * |
| 84 | * @param[in] userName - user name entry that has to be removed. |
| 85 | * |
| 86 | * @return error response |
| 87 | */ |
| 88 | int updatePasswdSpecialFile(const std::string& userName); |
| 89 | /** @brief encrypts or decrypt the data provided |
| 90 | * |
| 91 | * @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] | 92 | * @param[in] cipher - cipher to be used |
| 93 | * @param[in] key - pointer to the key |
| 94 | * @param[in] keyLen - Length of the key to be used |
| 95 | * @param[in] iv - pointer to initialization vector |
| 96 | * @param[in] ivLen - Length of the iv |
| 97 | * @param[in] inBytes - input data to be encrypted / decrypted |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame^] | 98 | * @param[in] inBytesLen - input size to be encrypted / decrypted |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 99 | * @param[in] mac - message authentication code - to figure out corruption |
| 100 | * @param[in] macLen - size of MAC |
| 101 | * @param[in] outBytes - ptr to store output bytes |
| 102 | * @param[in] outBytesLen - outbut data length. |
| 103 | * |
| 104 | * @return error response |
| 105 | */ |
AppaRao Puli | b29b5ab | 2018-05-17 10:28:48 +0530 | [diff] [blame^] | 106 | int encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher, |
| 107 | uint8_t* key, size_t keyLen, uint8_t* iv, |
| 108 | size_t ivLen, uint8_t* inBytes, size_t inBytesLen, |
| 109 | uint8_t* mac, size_t* macLen, uint8_t* outBytes, |
| 110 | size_t* outBytesLen); |
| 111 | |
| 112 | /** @brief returns updated file time of passwd file entry. |
| 113 | * |
| 114 | * @return timestamp or -1 for error. |
| 115 | */ |
| 116 | std::time_t getUpdatedFileTime(); |
Richard Marian Thomaiyar | 4654d99 | 2018-04-19 05:38:37 +0530 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace ipmi |