| Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 1 | #include "channel.hpp" | 
| Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 2 |  | 
| Johnathan Mantey | 74a2102 | 2018-12-13 13:17:56 -0800 | [diff] [blame] | 3 | #include "user_channel/channel_layer.hpp" | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 4 |  | 
| Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 5 | #include <arpa/inet.h> | 
 | 6 |  | 
| Tom Joseph | 1322768 | 2018-08-10 01:05:21 +0530 | [diff] [blame] | 7 | #include <boost/process/child.hpp> | 
| Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 8 | #include <ipmid/types.hpp> | 
| Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 9 | #include <ipmid/utils.hpp> | 
| Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> | 
 | 13 |  | 
 | 14 | #include <fstream> | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 15 | #include <set> | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 16 | #include <string> | 
| Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 17 |  | 
 | 18 | using namespace phosphor::logging; | 
| Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 19 | using namespace sdbusplus::error::xyz::openbmc_project::common; | 
| Patrick Venture | 5794fcf | 2017-10-26 11:11:14 -0700 | [diff] [blame] | 20 |  | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 21 | namespace cipher | 
 | 22 | { | 
 | 23 |  | 
 | 24 | /** @brief Get the supported Cipher records | 
 | 25 |  * | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 26 |  * The cipher records are read from the JSON file and converted into | 
 | 27 |  * 1. cipher suite record format mentioned in the IPMI specification. The | 
 | 28 |  * records can be either OEM or standard cipher. Each json entry is parsed and | 
 | 29 |  * converted into the cipher record format and pushed into the vector. | 
 | 30 |  * 2. Algorithms listed in vector format | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 31 |  * | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 32 |  * @return pair of vector containing 1. all the cipher suite records. 2. | 
 | 33 |  * Algorithms supported | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 34 |  * | 
 | 35 |  */ | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 36 | std::pair<std::vector<uint8_t>, std::vector<uint8_t>> getCipherRecords() | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 37 | { | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 38 |     std::vector<uint8_t> cipherRecords; | 
 | 39 |     std::vector<uint8_t> supportedAlgorithmRecords; | 
 | 40 |     // create set to get the unique supported algorithms | 
 | 41 |     std::set<uint8_t> supportedAlgorithmSet; | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 42 |  | 
 | 43 |     std::ifstream jsonFile(configFile); | 
 | 44 |     if (!jsonFile.is_open()) | 
 | 45 |     { | 
 | 46 |         log<level::ERR>("Channel Cipher suites file not found"); | 
 | 47 |         elog<InternalFailure>(); | 
 | 48 |     } | 
 | 49 |  | 
 | 50 |     auto data = Json::parse(jsonFile, nullptr, false); | 
 | 51 |     if (data.is_discarded()) | 
 | 52 |     { | 
 | 53 |         log<level::ERR>("Parsing channel cipher suites JSON failed"); | 
 | 54 |         elog<InternalFailure>(); | 
 | 55 |     } | 
 | 56 |  | 
 | 57 |     for (const auto& record : data) | 
 | 58 |     { | 
 | 59 |         if (record.find(oem) != record.end()) | 
 | 60 |         { | 
 | 61 |             // OEM cipher suite - 0xC1 | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 62 |             cipherRecords.push_back(oemCipherSuite); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 63 |             // Cipher Suite ID | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 64 |             cipherRecords.push_back(record.value(cipher, 0)); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 65 |             // OEM IANA - 3 bytes | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 66 |             cipherRecords.push_back(record.value(oem, 0)); | 
 | 67 |             cipherRecords.push_back(record.value(oem, 0) >> 8); | 
 | 68 |             cipherRecords.push_back(record.value(oem, 0) >> 16); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 69 |         } | 
 | 70 |         else | 
 | 71 |         { | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 72 |             // Standard cipher suite - 0xC0 | 
 | 73 |             cipherRecords.push_back(stdCipherSuite); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 74 |             // Cipher Suite ID | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 75 |             cipherRecords.push_back(record.value(cipher, 0)); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 76 |         } | 
 | 77 |  | 
 | 78 |         // Authentication algorithm number | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 79 |         cipherRecords.push_back(record.value(auth, 0)); | 
 | 80 |         supportedAlgorithmSet.insert(record.value(auth, 0)); | 
 | 81 |  | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 82 |         // Integrity algorithm number | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 83 |         cipherRecords.push_back(record.value(integrity, 0) | integrityTag); | 
 | 84 |         supportedAlgorithmSet.insert(record.value(integrity, 0) | integrityTag); | 
 | 85 |  | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 86 |         // Confidentiality algorithm number | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 87 |         cipherRecords.push_back(record.value(conf, 0) | confTag); | 
 | 88 |         supportedAlgorithmSet.insert(record.value(conf, 0) | confTag); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 89 |     } | 
 | 90 |  | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 91 |     // copy the set to supportedAlgorithmRecord which is vector based. | 
 | 92 |     std::copy(supportedAlgorithmSet.begin(), supportedAlgorithmSet.end(), | 
 | 93 |               std::back_inserter(supportedAlgorithmRecords)); | 
 | 94 |  | 
 | 95 |     return std::make_pair(cipherRecords, supportedAlgorithmRecords); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 96 | } | 
 | 97 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 98 | } // namespace cipher | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 99 |  | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 100 | /** @brief this command is used to look up what authentication, integrity, | 
 | 101 |  *  confidentiality algorithms are supported. | 
 | 102 |  * | 
 | 103 |  *  @ param ctx - context pointer | 
 | 104 |  *  @ param channelNumber - channel number | 
 | 105 |  *  @ param payloadType - payload type | 
 | 106 |  *  @ param listIndex - list index | 
 | 107 |  *  @ param algoSelectBit - list algorithms | 
 | 108 |  * | 
 | 109 |  *  @returns ipmi completion code plus response data | 
 | 110 |  *  - rspChannel - channel number for authentication algorithm. | 
 | 111 |  *  - rspRecords - cipher suite records. | 
 | 112 |  **/ | 
 | 113 | ipmi::RspType<uint8_t,             // Channel Number | 
 | 114 |               std::vector<uint8_t> // Cipher Records | 
 | 115 |               > | 
 | 116 |     getChannelCipherSuites(ipmi::Context::ptr ctx, uint4_t channelNumber, | 
 | 117 |                            uint4_t reserved1, uint8_t payloadType, | 
 | 118 |                            uint6_t listIndex, uint1_t reserved2, | 
 | 119 |                            uint1_t algoSelectBit) | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 120 | { | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 121 |     static std::vector<uint8_t> cipherRecords; | 
 | 122 |     static std::vector<uint8_t> supportedAlgorithms; | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 123 |     static auto recordInit = false; | 
 | 124 |  | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 125 |     uint8_t rspChannel = ipmi::convertCurrentChannelNum( | 
 | 126 |         static_cast<uint8_t>(channelNumber), ctx->channel); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 127 |  | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 128 |     if (!ipmi::isValidChannel(rspChannel) || reserved1 != 0 || reserved2 != 0) | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 129 |     { | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 130 |         return ipmi::responseInvalidFieldRequest(); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 131 |     } | 
| jayaprakash Mutyala | 69daefa | 2019-10-03 19:36:49 +0000 | [diff] [blame] | 132 |     if (!ipmi::isValidPayloadType(static_cast<ipmi::PayloadType>(payloadType))) | 
 | 133 |     { | 
 | 134 |         log<level::DEBUG>("Get channel cipher suites - Invalid payload type"); | 
 | 135 |         constexpr uint8_t ccPayloadTypeNotSupported = 0x80; | 
 | 136 |         return ipmi::response(ccPayloadTypeNotSupported); | 
 | 137 |     } | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 138 |  | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 139 |     if (!recordInit) | 
 | 140 |     { | 
 | 141 |         try | 
 | 142 |         { | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 143 |             std::tie(cipherRecords, | 
 | 144 |                      supportedAlgorithms) = cipher::getCipherRecords(); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 145 |             recordInit = true; | 
 | 146 |         } | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 147 |         catch (const std::exception& e) | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 148 |         { | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 149 |             return ipmi::responseUnspecifiedError(); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 150 |         } | 
 | 151 |     } | 
 | 152 |  | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 153 |     const std::vector<uint8_t>& records = algoSelectBit ? cipherRecords | 
 | 154 |                                                         : supportedAlgorithms; | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 155 |     static constexpr auto respSize = 16; | 
| Richard Marian Thomaiyar | f301f04 | 2019-01-16 15:56:16 +0530 | [diff] [blame] | 156 |  | 
| Ayushi Smriti | d7dadc2 | 2019-09-03 11:43:45 +0530 | [diff] [blame] | 157 |     // Session support is available in active LAN channels. | 
 | 158 |     if ((ipmi::getChannelSessionSupport(rspChannel) == | 
 | 159 |          ipmi::EChannelSessSupported::none) || | 
 | 160 |         !(ipmi::doesDeviceExist(rspChannel))) | 
 | 161 |     { | 
 | 162 |         log<level::DEBUG>("Get channel cipher suites - Device does not exist"); | 
 | 163 |         return ipmi::responseInvalidFieldRequest(); | 
 | 164 |     } | 
 | 165 |  | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 166 |     // List index(00h-3Fh), 0h selects the first set of 16, 1h selects the next | 
 | 167 |     // set of 16 and so on. | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 168 |  | 
 | 169 |     // Calculate the number of record data bytes to be returned. | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 170 |     auto start = std::min(static_cast<size_t>(listIndex) * respSize, | 
 | 171 |                           records.size()); | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 172 |     auto end = std::min((static_cast<size_t>(listIndex) * respSize) + respSize, | 
 | 173 |                         records.size()); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 174 |     auto size = end - start; | 
 | 175 |  | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 176 |     std::vector<uint8_t> rspRecords; | 
 | 177 |     std::copy_n(records.data() + start, size, std::back_inserter(rspRecords)); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 178 |  | 
| Ayushi Smriti | 5c3b72c | 2019-08-30 13:47:31 +0000 | [diff] [blame] | 179 |     return ipmi::responseSuccess(rspChannel, rspRecords); | 
| Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 180 | } |