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 "channel_layer.hpp"
18 
19 #include <ipmid/api-types.hpp>
20 #include <ipmid/message/types.hpp>
21 #include <map>
22 #include <nlohmann/json.hpp>
23 
24 namespace ipmi
25 {
26 static const std::string csPrivDefaultFileName =
27     "/usr/share/ipmi-providers/cs_privilege_levels.json";
28 
29 static const std::string csPrivFileName =
30     "/var/lib/ipmi/cs_privilege_levels.json";
31 
32 static const size_t maxCSRecords = 16;
33 
34 using ChannelNumCipherIDPair = std::pair<uint8_t, uint8_t>;
35 using privMap = std::map<ChannelNumCipherIDPair, uint4_t>;
36 
37 /** @class CipherConfig
38  *  @brief Class to provide cipher suite functionalities
39  */
40 class CipherConfig
41 {
42   public:
43     ~CipherConfig() = default;
44     explicit CipherConfig(const std::string& csFileName,
45                           const std::string& csDefaultFileName);
46     CipherConfig() = delete;
47 
48     /** @brief function to get cipher suite privileges from config file
49      *
50      *  @param[in] chNum - channel number for which we want to get cipher suite
51      * privilege levels
52      *
53      *  @param[in] csPrivilegeLevels - gets filled by cipher suite privilege
54      * levels
55      *
56      *  @return 0 for success, non zero value for failure
57      */
58     ipmi::Cc getCSPrivilegeLevels(
59         uint8_t chNum, std::array<uint4_t, maxCSRecords>& csPrivilegeLevels);
60 
61     /** @brief function to set/update cipher suite privileges in config file
62      *
63      *  @param[in] chNum - channel number for which we want to update cipher
64      * suite privilege levels
65      *
66      *  @param[in] csPrivilegeLevels - cipher suite privilege levels to update
67      * in config file
68      *
69      *  @return 0 for success, non zero value for failure
70      */
71     ipmi::Cc setCSPrivilegeLevels(
72         uint8_t chNum,
73         const std::array<uint4_t, maxCSRecords>& csPrivilegeLevels);
74 
75   private:
76     std::string cipherSuitePrivFileName, cipherSuiteDefaultPrivFileName;
77 
78     privMap csPrivilegeMap;
79 
80     /** @brief function to read json config file
81      *
82      *  @return nlohmann::json object
83      */
84     nlohmann::json readCSPrivilegeLevels(const std::string& csFileName);
85 
86     /** @brief function to write json config file
87      *
88      *  @param[in] jsonData - json object
89      *
90      *  @return 0 for success, -errno for failure.
91      */
92     int writeCSPrivilegeLevels(const nlohmann::json& jsonData);
93 
94     /** @brief convert to cipher suite privilege from string to value
95      *
96      *  @param[in] value - privilege value
97      *
98      *  @return cipher suite privilege index
99      */
100     uint4_t convertToPrivLimitIndex(const std::string& value);
101 
102     /** @brief function to convert privilege value to string
103      *
104      *  @param[in] value - privilege value
105      *
106      *  @return privilege in string
107      */
108     std::string convertToPrivLimitString(const uint4_t& value);
109 
110     /** @brief function to load CS Privilege Levels from json file/files to map
111      *
112      */
113     void loadCSPrivilegesToMap();
114 
115     /** @brief function to update CS privileges map from json object data,
116      * jsonData
117      *
118      */
119     void updateCSPrivilegesMap(const nlohmann::json& jsonData);
120 };
121 
122 /** @brief function to create static CipherConfig object
123  *
124  *  @param[in] csFileName - user setting cipher suite privilege file name
125  *  @param[in] csDefaultFileName - default cipher suite privilege file name
126  *
127  *  @return static CipherConfig object
128  */
129 CipherConfig& getCipherConfigObject(const std::string& csFileName,
130                                     const std::string& csDefaultFileName);
131 } // namespace ipmi
132