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