Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 1 | #include "message_parsers.hpp" |
| 2 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 3 | #include "endian.hpp" |
| 4 | #include "main.hpp" |
| 5 | #include "message.hpp" |
| 6 | #include "sessions_manager.hpp" |
| 7 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 10 | namespace message |
| 11 | { |
| 12 | |
| 13 | namespace parser |
| 14 | { |
| 15 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 16 | std::tuple<std::shared_ptr<Message>, SessionHeader> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 17 | unflatten(std::vector<uint8_t>& inPacket) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 18 | { |
| 19 | // Check if the packet has atleast the size of the RMCP Header |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 20 | if (inPacket.size() < sizeof(RmcpHeader_t)) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 21 | { |
| 22 | throw std::runtime_error("RMCP Header missing"); |
| 23 | } |
| 24 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 25 | auto rmcpHeaderPtr = reinterpret_cast<RmcpHeader_t*>(inPacket.data()); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 26 | |
| 27 | // Verify if the fields in the RMCP header conforms to the specification |
| 28 | if ((rmcpHeaderPtr->version != RMCP_VERSION) || |
| 29 | (rmcpHeaderPtr->rmcpSeqNum != RMCP_SEQ) || |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 30 | (rmcpHeaderPtr->classOfMsg < static_cast<uint8_t>(ClassOfMsg::ASF) && |
| 31 | rmcpHeaderPtr->classOfMsg > static_cast<uint8_t>(ClassOfMsg::OEM))) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 32 | { |
| 33 | throw std::runtime_error("RMCP Header is invalid"); |
| 34 | } |
| 35 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 36 | if (rmcpHeaderPtr->classOfMsg == static_cast<uint8_t>(ClassOfMsg::ASF)) |
| 37 | { |
| 38 | #ifndef RMCP_PING |
| 39 | throw std::runtime_error("RMCP Ping is not supported"); |
| 40 | #else |
| 41 | return std::make_tuple(asfparser::unflatten(inPacket), |
| 42 | SessionHeader::IPMI15); |
| 43 | #endif // RMCP_PING |
| 44 | } |
| 45 | |
| 46 | auto sessionHeaderPtr = reinterpret_cast<BasicHeader_t*>(inPacket.data()); |
| 47 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 48 | // Read the Session Header and invoke the parser corresponding to the |
| 49 | // header type |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 50 | switch (static_cast<SessionHeader>(sessionHeaderPtr->format.formatType)) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 51 | { |
| 52 | case SessionHeader::IPMI15: |
| 53 | { |
| 54 | return std::make_tuple(ipmi15parser::unflatten(inPacket), |
| 55 | SessionHeader::IPMI15); |
| 56 | } |
| 57 | case SessionHeader::IPMI20: |
| 58 | { |
| 59 | return std::make_tuple(ipmi20parser::unflatten(inPacket), |
| 60 | SessionHeader::IPMI20); |
| 61 | } |
| 62 | default: |
| 63 | { |
| 64 | throw std::runtime_error("Invalid Session Header"); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Lei YU | a82e4d3 | 2022-07-13 10:03:20 +0800 | [diff] [blame] | 69 | std::vector<uint8_t> flatten(const std::shared_ptr<Message>& outMessage, |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 70 | SessionHeader authType, |
Lei YU | a82e4d3 | 2022-07-13 10:03:20 +0800 | [diff] [blame] | 71 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 72 | { |
| 73 | // Call the flatten routine based on the header type |
| 74 | switch (authType) |
| 75 | { |
| 76 | case SessionHeader::IPMI15: |
| 77 | { |
| 78 | return ipmi15parser::flatten(outMessage, session); |
| 79 | } |
| 80 | case SessionHeader::IPMI20: |
| 81 | { |
| 82 | return ipmi20parser::flatten(outMessage, session); |
| 83 | } |
| 84 | default: |
| 85 | { |
| 86 | return {}; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace parser |
| 92 | |
| 93 | namespace ipmi15parser |
| 94 | { |
| 95 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 96 | std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 97 | { |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 98 | if (inPacket.size() < sizeof(SessionHeader_t)) |
| 99 | { |
| 100 | throw std::runtime_error("IPMI1.5 Session Header Missing"); |
| 101 | } |
| 102 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 103 | auto header = reinterpret_cast<SessionHeader_t*>(inPacket.data()); |
| 104 | |
Vernon Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 105 | uint32_t sessionID = endian::from_ipmi(header->sessId); |
| 106 | if (sessionID != session::sessionZero) |
| 107 | { |
| 108 | throw std::runtime_error("IPMI1.5 session packets are unsupported"); |
| 109 | } |
| 110 | |
| 111 | auto message = std::make_shared<Message>(); |
| 112 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 113 | message->payloadType = PayloadType::IPMI; |
Vernon Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 114 | message->bmcSessionID = session::sessionZero; |
Tom Joseph | 6a56076 | 2017-01-10 15:30:28 +0530 | [diff] [blame] | 115 | message->sessionSeqNum = endian::from_ipmi(header->sessSeqNum); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 116 | message->isPacketEncrypted = false; |
| 117 | message->isPacketAuthenticated = false; |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 118 | message->rmcpMsgClass = |
| 119 | static_cast<ClassOfMsg>(header->base.rmcp.classOfMsg); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 120 | |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 121 | // Confirm the number of data bytes received correlates to |
| 122 | // the packet length in the header |
Vernon Mauery | a71b1ba | 2021-06-08 15:53:46 -0700 | [diff] [blame] | 123 | size_t payloadLen = header->payloadLength; |
| 124 | if ((payloadLen == 0) || (inPacket.size() < (sizeof(*header) + payloadLen))) |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 125 | { |
| 126 | throw std::runtime_error("Invalid data length"); |
| 127 | } |
| 128 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 129 | (message->payload) |
| 130 | .assign(inPacket.data() + sizeof(SessionHeader_t), |
| 131 | inPacket.data() + sizeof(SessionHeader_t) + payloadLen); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 132 | |
| 133 | return message; |
| 134 | } |
| 135 | |
George Liu | be1470c | 2022-07-04 14:33:24 +0800 | [diff] [blame] | 136 | std::vector<uint8_t> |
| 137 | flatten(const std::shared_ptr<Message>& outMessage, |
| 138 | const std::shared_ptr<session::Session>& /* session */) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 139 | { |
| 140 | std::vector<uint8_t> packet(sizeof(SessionHeader_t)); |
| 141 | |
| 142 | // Insert Session Header into the Packet |
| 143 | auto header = reinterpret_cast<SessionHeader_t*>(packet.data()); |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 144 | header->base.rmcp.version = parser::RMCP_VERSION; |
| 145 | header->base.rmcp.reserved = 0x00; |
| 146 | header->base.rmcp.rmcpSeqNum = parser::RMCP_SEQ; |
| 147 | header->base.rmcp.classOfMsg = static_cast<uint8_t>(ClassOfMsg::IPMI); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 148 | header->base.format.formatType = |
| 149 | static_cast<uint8_t>(parser::SessionHeader::IPMI15); |
| 150 | header->sessSeqNum = 0; |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 151 | header->sessId = endian::to_ipmi(outMessage->rcSessionID); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 152 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 153 | header->payloadLength = static_cast<uint8_t>(outMessage->payload.size()); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 154 | |
| 155 | // Insert the Payload into the Packet |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 156 | packet.insert(packet.end(), outMessage->payload.begin(), |
| 157 | outMessage->payload.end()); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 158 | |
| 159 | // Insert the Session Trailer |
| 160 | packet.resize(packet.size() + sizeof(SessionTrailer_t)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 161 | auto trailer = |
| 162 | reinterpret_cast<SessionTrailer_t*>(packet.data() + packet.size()); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 163 | trailer->legacyPad = 0x00; |
| 164 | |
| 165 | return packet; |
| 166 | } |
| 167 | |
| 168 | } // namespace ipmi15parser |
| 169 | |
| 170 | namespace ipmi20parser |
| 171 | { |
| 172 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 173 | std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 174 | { |
| 175 | // Check if the packet has atleast the Session Header |
| 176 | if (inPacket.size() < sizeof(SessionHeader_t)) |
| 177 | { |
| 178 | throw std::runtime_error("IPMI2.0 Session Header Missing"); |
| 179 | } |
| 180 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 181 | auto header = reinterpret_cast<SessionHeader_t*>(inPacket.data()); |
| 182 | |
Vernon Mauery | 1ab1c6b | 2021-06-08 15:54:41 -0700 | [diff] [blame] | 183 | uint32_t sessionID = endian::from_ipmi(header->sessId); |
| 184 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 185 | auto session = session::Manager::get().getSession(sessionID); |
Vernon Mauery | 1ab1c6b | 2021-06-08 15:54:41 -0700 | [diff] [blame] | 186 | if (!session) |
| 187 | { |
| 188 | throw std::runtime_error("RMCP+ message from unknown session"); |
| 189 | } |
| 190 | |
| 191 | auto message = std::make_shared<Message>(); |
| 192 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 193 | message->payloadType = static_cast<PayloadType>(header->payloadType & 0x3F); |
Vernon Mauery | 1ab1c6b | 2021-06-08 15:54:41 -0700 | [diff] [blame] | 194 | message->bmcSessionID = sessionID; |
Tom Joseph | 6a56076 | 2017-01-10 15:30:28 +0530 | [diff] [blame] | 195 | message->sessionSeqNum = endian::from_ipmi(header->sessSeqNum); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 196 | message->isPacketEncrypted = |
| 197 | ((header->payloadType & PAYLOAD_ENCRYPT_MASK) ? true : false); |
| 198 | message->isPacketAuthenticated = |
| 199 | ((header->payloadType & PAYLOAD_AUTH_MASK) ? true : false); |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 200 | message->rmcpMsgClass = |
| 201 | static_cast<ClassOfMsg>(header->base.rmcp.classOfMsg); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 202 | |
Vernon Mauery | a71b1ba | 2021-06-08 15:53:46 -0700 | [diff] [blame] | 203 | // Confirm the number of data bytes received correlates to |
| 204 | // the packet length in the header |
| 205 | size_t payloadLen = endian::from_ipmi(header->payloadLength); |
| 206 | if ((payloadLen == 0) || (inPacket.size() < (sizeof(*header) + payloadLen))) |
| 207 | { |
| 208 | throw std::runtime_error("Invalid data length"); |
| 209 | } |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 210 | |
Vernon Mauery | 1ab1c6b | 2021-06-08 15:54:41 -0700 | [diff] [blame] | 211 | bool integrityMismatch = |
| 212 | session->isIntegrityAlgoEnabled() && !message->isPacketAuthenticated; |
| 213 | bool encryptMismatch = |
| 214 | session->isCryptAlgoEnabled() && !message->isPacketEncrypted; |
| 215 | |
| 216 | if (sessionID != session::sessionZero && |
| 217 | (integrityMismatch || encryptMismatch)) |
| 218 | { |
| 219 | throw std::runtime_error("unencrypted or unauthenticated message"); |
| 220 | } |
| 221 | |
Tom Joseph | 5fa487c | 2017-01-20 12:42:39 +0530 | [diff] [blame] | 222 | if (message->isPacketAuthenticated) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 223 | { |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 224 | if (!(internal::verifyPacketIntegrity(inPacket, message, payloadLen, |
| 225 | session))) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 226 | { |
| 227 | throw std::runtime_error("Packet Integrity check failed"); |
| 228 | } |
| 229 | } |
| 230 | |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 231 | // Decrypt the payload if the payload is encrypted |
| 232 | if (message->isPacketEncrypted) |
| 233 | { |
| 234 | // Assign the decrypted payload to the IPMI Message |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 235 | message->payload = |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 236 | internal::decryptPayload(inPacket, message, payloadLen, session); |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 237 | } |
| 238 | else |
| 239 | { |
| 240 | message->payload.assign(inPacket.begin() + sizeof(SessionHeader_t), |
| 241 | inPacket.begin() + sizeof(SessionHeader_t) + |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 242 | payloadLen); |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 243 | } |
| 244 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 245 | return message; |
| 246 | } |
| 247 | |
Lei YU | a82e4d3 | 2022-07-13 10:03:20 +0800 | [diff] [blame] | 248 | std::vector<uint8_t> flatten(const std::shared_ptr<Message>& outMessage, |
| 249 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 250 | { |
| 251 | std::vector<uint8_t> packet(sizeof(SessionHeader_t)); |
| 252 | |
| 253 | SessionHeader_t* header = reinterpret_cast<SessionHeader_t*>(packet.data()); |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 254 | header->base.rmcp.version = parser::RMCP_VERSION; |
| 255 | header->base.rmcp.reserved = 0x00; |
| 256 | header->base.rmcp.rmcpSeqNum = parser::RMCP_SEQ; |
| 257 | header->base.rmcp.classOfMsg = static_cast<uint8_t>(ClassOfMsg::IPMI); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 258 | header->base.format.formatType = |
| 259 | static_cast<uint8_t>(parser::SessionHeader::IPMI20); |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 260 | header->payloadType = static_cast<uint8_t>(outMessage->payloadType); |
| 261 | header->sessId = endian::to_ipmi(outMessage->rcSessionID); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 262 | |
| 263 | // Add session sequence number |
| 264 | internal::addSequenceNumber(packet, session); |
| 265 | |
Tom Joseph | 1404bca | 2017-01-26 14:17:02 +0530 | [diff] [blame] | 266 | size_t payloadLen = 0; |
| 267 | |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 268 | // Encrypt the payload if needed |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 269 | if (outMessage->isPacketEncrypted) |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 270 | { |
| 271 | header->payloadType |= PAYLOAD_ENCRYPT_MASK; |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 272 | auto cipherPayload = internal::encryptPayload(outMessage, session); |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 273 | payloadLen = cipherPayload.size(); |
| 274 | header->payloadLength = endian::to_ipmi<uint16_t>(cipherPayload.size()); |
| 275 | |
| 276 | // Insert the encrypted payload into the outgoing IPMI packet |
| 277 | packet.insert(packet.end(), cipherPayload.begin(), cipherPayload.end()); |
| 278 | } |
| 279 | else |
| 280 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 281 | header->payloadLength = |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 282 | endian::to_ipmi<uint16_t>(outMessage->payload.size()); |
| 283 | payloadLen = outMessage->payload.size(); |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 284 | |
| 285 | // Insert the Payload into the Packet |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 286 | packet.insert(packet.end(), outMessage->payload.begin(), |
| 287 | outMessage->payload.end()); |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 288 | } |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 289 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 290 | if (outMessage->isPacketAuthenticated) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 291 | { |
Tom Joseph | b882dbb | 2019-02-09 23:13:48 +0530 | [diff] [blame] | 292 | header = reinterpret_cast<SessionHeader_t*>(packet.data()); |
| 293 | header->payloadType |= PAYLOAD_AUTH_MASK; |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 294 | internal::addIntegrityData(packet, outMessage, payloadLen, session); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 295 | } |
| 296 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 297 | return packet; |
| 298 | } |
| 299 | |
| 300 | namespace internal |
| 301 | { |
| 302 | |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 303 | void addSequenceNumber(std::vector<uint8_t>& packet, |
Lei YU | a82e4d3 | 2022-07-13 10:03:20 +0800 | [diff] [blame] | 304 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 305 | { |
| 306 | SessionHeader_t* header = reinterpret_cast<SessionHeader_t*>(packet.data()); |
| 307 | |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 308 | if (header->sessId == session::sessionZero) |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 309 | { |
| 310 | header->sessSeqNum = 0x00; |
| 311 | } |
| 312 | else |
| 313 | { |
Vernon Mauery | 224f36a | 2018-10-25 08:52:23 -0700 | [diff] [blame] | 314 | auto seqNum = session->sequenceNums.increment(); |
Tom Joseph | 6a56076 | 2017-01-10 15:30:28 +0530 | [diff] [blame] | 315 | header->sessSeqNum = endian::to_ipmi(seqNum); |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 319 | bool verifyPacketIntegrity(const std::vector<uint8_t>& packet, |
George Liu | be1470c | 2022-07-04 14:33:24 +0800 | [diff] [blame] | 320 | const std::shared_ptr<Message>& /* message */, |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 321 | size_t payloadLen, |
| 322 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 323 | { |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 324 | /* |
| 325 | * Padding bytes are added to cause the number of bytes in the data range |
| 326 | * covered by the AuthCode(Integrity Data) field to be a multiple of 4 bytes |
| 327 | * .If present each integrity Pad byte is set to FFh. The following logic |
| 328 | * calculates the number of padding bytes added in the IPMI packet. |
| 329 | */ |
Tom Joseph | 5a2d3ce | 2017-02-01 20:18:37 +0530 | [diff] [blame] | 330 | auto paddingLen = 4 - ((payloadLen + 2) & 3); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 331 | |
| 332 | auto sessTrailerPos = sizeof(SessionHeader_t) + payloadLen + paddingLen; |
| 333 | |
Zhikui Ren | 2b1edef | 2020-07-24 14:32:13 -0700 | [diff] [blame] | 334 | // verify packet size includes trailer struct starts at sessTrailerPos |
| 335 | if (packet.size() < (sessTrailerPos + sizeof(SessionTrailer_t))) |
| 336 | { |
| 337 | return false; |
| 338 | } |
| 339 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 340 | auto trailer = reinterpret_cast<const SessionTrailer_t*>(packet.data() + |
| 341 | sessTrailerPos); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 342 | |
| 343 | // Check trailer->padLength against paddingLen, both should match up, |
| 344 | // return false if the lengths don't match |
Tom Joseph | 5fa487c | 2017-01-20 12:42:39 +0530 | [diff] [blame] | 345 | if (trailer->padLength != paddingLen) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 346 | { |
| 347 | return false; |
| 348 | } |
| 349 | |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 350 | auto integrityAlgo = session->getIntegrityAlgo(); |
| 351 | |
| 352 | // Check if Integrity data length is as expected, check integrity data |
| 353 | // length is same as the length expected for the Integrity Algorithm that |
| 354 | // was negotiated during the session open process. |
Tom Joseph | 5fa487c | 2017-01-20 12:42:39 +0530 | [diff] [blame] | 355 | if ((packet.size() - sessTrailerPos - sizeof(SessionTrailer_t)) != |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 356 | integrityAlgo->authCodeLength) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 357 | { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | auto integrityIter = packet.cbegin(); |
| 362 | std::advance(integrityIter, sessTrailerPos + sizeof(SessionTrailer_t)); |
| 363 | |
| 364 | // The integrity data is calculated from the AuthType/Format field up to and |
| 365 | // including the field that immediately precedes the AuthCode field itself. |
| 366 | size_t length = packet.size() - integrityAlgo->authCodeLength - |
| 367 | message::parser::RMCP_SESSION_HEADER_SIZE; |
| 368 | |
George Liu | 198b0f8 | 2022-08-04 20:22:48 +0800 | [diff] [blame] | 369 | return integrityAlgo->verifyIntegrityData(packet, length, integrityIter, |
| 370 | packet.cend()); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 371 | } |
| 372 | |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 373 | void addIntegrityData(std::vector<uint8_t>& packet, |
George Liu | be1470c | 2022-07-04 14:33:24 +0800 | [diff] [blame] | 374 | const std::shared_ptr<Message>& /* message */, |
Lei YU | a82e4d3 | 2022-07-13 10:03:20 +0800 | [diff] [blame] | 375 | size_t payloadLen, |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 376 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 377 | { |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 378 | // The following logic calculates the number of padding bytes to be added to |
| 379 | // IPMI packet. If needed each integrity Pad byte is set to FFh. |
Tom Joseph | 5a2d3ce | 2017-02-01 20:18:37 +0530 | [diff] [blame] | 380 | auto paddingLen = 4 - ((payloadLen + 2) & 3); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 381 | packet.insert(packet.end(), paddingLen, 0xFF); |
| 382 | |
| 383 | packet.resize(packet.size() + sizeof(SessionTrailer_t)); |
| 384 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 385 | auto trailer = reinterpret_cast<SessionTrailer_t*>( |
| 386 | packet.data() + packet.size() - sizeof(SessionTrailer_t)); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 387 | |
| 388 | trailer->padLength = paddingLen; |
| 389 | trailer->nextHeader = parser::RMCP_MESSAGE_CLASS_IPMI; |
| 390 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 391 | auto integrityData = |
| 392 | session->getIntegrityAlgo()->generateIntegrityData(packet); |
Tom Joseph | 64703f4 | 2017-01-10 17:03:48 +0530 | [diff] [blame] | 393 | |
| 394 | packet.insert(packet.end(), integrityData.begin(), integrityData.end()); |
| 395 | } |
| 396 | |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 397 | std::vector<uint8_t> |
| 398 | decryptPayload(const std::vector<uint8_t>& packet, |
George Liu | be1470c | 2022-07-04 14:33:24 +0800 | [diff] [blame] | 399 | const std::shared_ptr<Message>& /* message */, |
| 400 | size_t payloadLen, |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 401 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 402 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 403 | return session->getCryptAlgo()->decryptPayload( |
| 404 | packet, sizeof(SessionHeader_t), payloadLen); |
Tom Joseph | 78478a8 | 2017-01-26 14:24:29 +0530 | [diff] [blame] | 405 | } |
| 406 | |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 407 | std::vector<uint8_t> |
Lei YU | a82e4d3 | 2022-07-13 10:03:20 +0800 | [diff] [blame] | 408 | encryptPayload(const std::shared_ptr<Message>& message, |
Lei YU | ce1f4fc | 2022-07-12 20:22:17 +0800 | [diff] [blame] | 409 | const std::shared_ptr<session::Session>& session) |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 410 | { |
Vernon Mauery | d999ffc | 2018-10-25 09:16:05 -0700 | [diff] [blame] | 411 | return session->getCryptAlgo()->encryptPayload(message->payload); |
Tom Joseph | 7536283 | 2017-01-26 15:17:04 +0530 | [diff] [blame] | 412 | } |
| 413 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 414 | } // namespace internal |
| 415 | |
| 416 | } // namespace ipmi20parser |
| 417 | |
Kirill Pakhomov | de7dd5c | 2021-02-27 18:45:22 +0300 | [diff] [blame] | 418 | #ifdef RMCP_PING |
| 419 | namespace asfparser |
| 420 | { |
| 421 | std::shared_ptr<Message> unflatten(std::vector<uint8_t>& inPacket) |
| 422 | { |
| 423 | auto message = std::make_shared<Message>(); |
| 424 | |
| 425 | auto header = reinterpret_cast<AsfMessagePing_t*>(inPacket.data()); |
| 426 | |
| 427 | message->payloadType = PayloadType::IPMI; |
| 428 | message->rmcpMsgClass = ClassOfMsg::ASF; |
| 429 | message->asfMsgTag = header->msgTag; |
| 430 | |
| 431 | return message; |
| 432 | } |
| 433 | |
| 434 | std::vector<uint8_t> flatten(uint8_t asfMsgTag) |
| 435 | { |
| 436 | std::vector<uint8_t> packet(sizeof(AsfMessagePong_t)); |
| 437 | |
| 438 | // Insert RMCP header into the Packet |
| 439 | auto header = reinterpret_cast<AsfMessagePong_t*>(packet.data()); |
| 440 | header->ping.rmcp.version = parser::RMCP_VERSION; |
| 441 | header->ping.rmcp.reserved = 0x00; |
| 442 | header->ping.rmcp.rmcpSeqNum = parser::RMCP_SEQ; |
| 443 | header->ping.rmcp.classOfMsg = static_cast<uint8_t>(ClassOfMsg::ASF); |
| 444 | |
| 445 | // No OEM-specific capabilities exist, therefore the second |
| 446 | // IANA Enterprise Number contains the same IANA(4542) |
| 447 | header->ping.iana = header->iana = endian::to_ipmi(parser::ASF_IANA); |
| 448 | header->ping.msgType = static_cast<uint8_t>(RmcpMsgType::PONG); |
| 449 | header->ping.msgTag = asfMsgTag; |
| 450 | header->ping.reserved = 0x00; |
| 451 | header->ping.dataLen = |
| 452 | parser::RMCP_ASF_PONG_DATA_LEN; // as per spec 13.2.4, |
| 453 | |
| 454 | header->iana = parser::ASF_IANA; |
| 455 | header->oemDefined = 0x00; |
| 456 | header->suppEntities = parser::ASF_SUPP_ENT; |
| 457 | header->suppInteract = parser::ASF_SUPP_INT; |
| 458 | header->reserved1 = 0x00; |
| 459 | header->reserved2 = 0x00; |
| 460 | |
| 461 | return packet; |
| 462 | } |
| 463 | |
| 464 | } // namespace asfparser |
| 465 | #endif // RMCP_PING |
| 466 | |
Tom Joseph | 4e57ada | 2016-08-10 06:51:12 -0500 | [diff] [blame] | 467 | } // namespace message |