Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "auth_algo.hpp" |
| 4 | #include "crypt_algo.hpp" |
| 5 | #include "endian.hpp" |
| 6 | #include "integrity_algo.hpp" |
Vernon Mauery | 8977d12 | 2018-10-24 14:02:16 -0700 | [diff] [blame] | 7 | #include "prng.hpp" |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include "socket_channel.hpp" |
| 9 | |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 10 | #include <chrono> |
| 11 | #include <exception> |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 12 | #include <ipmid/api.hpp> |
| 13 | #include <ipmid/sessiondef.hpp> |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 14 | #include <list> |
| 15 | #include <memory> |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 16 | #include <sdbusplus/bus.hpp> |
| 17 | #include <sdbusplus/server/object.hpp> |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 18 | #include <string> |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 19 | #include <unordered_map> |
Richard Marian Thomaiyar | 992e53c | 2019-03-03 13:30:46 +0530 | [diff] [blame] | 20 | #include <user_channel/channel_layer.hpp> |
| 21 | #include <user_channel/user_layer.hpp> |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 22 | #include <vector> |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 23 | #include <xyz/openbmc_project/Ipmi/SessionInfo/server.hpp> |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 24 | |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 25 | namespace session |
| 26 | { |
| 27 | |
| 28 | using namespace std::chrono_literals; |
| 29 | using SessionID = uint32_t; |
| 30 | |
| 31 | enum class Privilege : uint8_t |
| 32 | { |
| 33 | HIGHEST_MATCHING, |
| 34 | CALLBACK, |
| 35 | USER, |
| 36 | OPERATOR, |
| 37 | ADMIN, |
| 38 | OEM, |
| 39 | }; |
| 40 | |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 41 | // Seconds of inactivity allowed during session setup stage |
| 42 | constexpr auto SESSION_SETUP_TIMEOUT = 5s; |
| 43 | // Seconds of inactivity allowed when session is active |
| 44 | constexpr auto SESSION_INACTIVITY_TIMEOUT = 60s; |
| 45 | |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 46 | // Mask to get only the privilege from requested maximum privlege (RAKP message |
| 47 | // 1) |
| 48 | constexpr uint8_t reqMaxPrivMask = 0xF; |
| 49 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 50 | /** |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 51 | * @struct SequenceNumbers Session Sequence Numbers |
| 52 | * |
| 53 | * IPMI v2.0 RMCP+ Session Sequence Numbers are used for rejecting packets that |
| 54 | * may have been duplicated by the network or intentionally replayed. There are |
| 55 | * two sets of Session SequenceNumbers for a given session.One set of inbound |
| 56 | * and outbound sequence numbers is used for authenticated (signed) packets, |
| 57 | * and the other set is used for unauthenticated packets. |
| 58 | * |
| 59 | * The individual Session Sequence Numbers is are initialized to zero whenever |
| 60 | * a session is created and incremented by one at the start of outbound |
| 61 | * processing for a given packet (i.e. the first transmitted packet has a ‘1’ |
| 62 | * as the sequence number, not 0). Session Sequence numbers are incremented for |
| 63 | * every packet that is transmitted by a given sender, regardless of whether |
| 64 | * the payload for the packet is a ‘retry’ or not. |
| 65 | */ |
| 66 | struct SequenceNumbers |
| 67 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 68 | auto get(bool inbound = true) const |
| 69 | { |
| 70 | return inbound ? in : out; |
| 71 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 72 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 73 | void set(uint32_t seqNumber, bool inbound = true) |
| 74 | { |
| 75 | inbound ? (in = seqNumber) : (out = seqNumber); |
| 76 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 77 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 78 | auto increment() |
| 79 | { |
| 80 | return ++out; |
| 81 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 82 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 83 | private: |
| 84 | uint32_t in = 0; |
| 85 | uint32_t out = 0; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 86 | }; |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 87 | /** |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 88 | * @class Session |
| 89 | * |
| 90 | * Encapsulates the data related to an IPMI Session |
| 91 | * |
| 92 | * Authenticated IPMI communication to the BMC is accomplished by establishing |
| 93 | * a session. Once established, a session is identified by a Session ID. The |
| 94 | * Session ID may be thought of as a handle that identifies a connection between |
| 95 | * a given remote user and the BMC. The specification supports having multiple |
| 96 | * active sessions established with the BMC. It is recommended that a BMC |
| 97 | * implementation support at least four simultaneous sessions |
| 98 | */ |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 99 | |
| 100 | using SessionIface = sdbusplus::server::object::object< |
| 101 | sdbusplus::xyz::openbmc_project::Ipmi::server::SessionInfo>; |
| 102 | |
| 103 | class Session : public SessionIface |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 104 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 105 | public: |
| 106 | Session() = default; |
| 107 | ~Session() = default; |
| 108 | Session(const Session&) = delete; |
| 109 | Session& operator=(const Session&) = delete; |
| 110 | Session(Session&&) = default; |
| 111 | Session& operator=(Session&&) = default; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 112 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 113 | /** |
| 114 | * @brief Session Constructor |
| 115 | * |
| 116 | * This is issued by the Session Manager when a session is started for |
| 117 | * the Open SessionRequest command |
| 118 | * |
| 119 | * @param[in] inRemoteConsoleSessID - Remote Console Session ID |
| 120 | * @param[in] priv - Privilege Level requested in the Command |
| 121 | */ |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 122 | Session(sdbusplus::bus::bus& bus, const char* path, |
| 123 | SessionID inRemoteConsoleSessID, SessionID BMCSessionID, |
| 124 | char priv) : |
| 125 | SessionIface(bus, path) |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 126 | { |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 127 | reqMaxPrivLevel = static_cast<session::Privilege>(priv); |
| 128 | bmcSessionID = BMCSessionID; |
| 129 | remoteConsoleSessionID = inRemoteConsoleSessID; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 130 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 131 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 132 | auto getBMCSessionID() const |
| 133 | { |
| 134 | return bmcSessionID; |
| 135 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 136 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 137 | auto getRCSessionID() const |
| 138 | { |
| 139 | return remoteConsoleSessionID; |
| 140 | } |
| 141 | |
| 142 | auto getAuthAlgo() const |
| 143 | { |
| 144 | if (authAlgoInterface) |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 145 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 146 | return authAlgoInterface.get(); |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 147 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 148 | else |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 149 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 150 | throw std::runtime_error("Authentication Algorithm Empty"); |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 151 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 152 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 153 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 154 | void setAuthAlgo(std::unique_ptr<cipher::rakp_auth::Interface>&& inAuthAlgo) |
| 155 | { |
| 156 | authAlgoInterface = std::move(inAuthAlgo); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @brief Get Session's Integrity Algorithm |
| 161 | * |
| 162 | * @return pointer to the integrity algorithm |
| 163 | */ |
| 164 | auto getIntegrityAlgo() const |
| 165 | { |
| 166 | if (integrityAlgoInterface) |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 167 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 168 | return integrityAlgoInterface.get(); |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 169 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 170 | else |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 171 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 172 | throw std::runtime_error("Integrity Algorithm Empty"); |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 173 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 174 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 175 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 176 | /** |
| 177 | * @brief Set Session's Integrity Algorithm |
| 178 | * |
| 179 | * @param[in] integrityAlgo - unique pointer to integrity algorithm |
| 180 | * instance |
| 181 | */ |
| 182 | void setIntegrityAlgo( |
| 183 | std::unique_ptr<cipher::integrity::Interface>&& integrityAlgo) |
| 184 | { |
| 185 | integrityAlgoInterface = std::move(integrityAlgo); |
| 186 | } |
| 187 | |
| 188 | /** @brief Check if integrity algorithm is enabled for this session. |
| 189 | * |
| 190 | * @return true if integrity algorithm is enabled else false. |
| 191 | */ |
| 192 | auto isIntegrityAlgoEnabled() |
| 193 | { |
| 194 | return integrityAlgoInterface ? true : false; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @brief Get Session's Confidentiality Algorithm |
| 199 | * |
| 200 | * @return pointer to the confidentiality algorithm |
| 201 | */ |
| 202 | auto getCryptAlgo() const |
| 203 | { |
| 204 | if (cryptAlgoInterface) |
Tom Joseph | 638d066 | 2017-01-10 16:02:07 +0530 | [diff] [blame] | 205 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 206 | return cryptAlgoInterface.get(); |
Tom Joseph | 638d066 | 2017-01-10 16:02:07 +0530 | [diff] [blame] | 207 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 208 | else |
Tom Joseph | 638d066 | 2017-01-10 16:02:07 +0530 | [diff] [blame] | 209 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 210 | throw std::runtime_error("Confidentiality Algorithm Empty"); |
Tom Joseph | 638d066 | 2017-01-10 16:02:07 +0530 | [diff] [blame] | 211 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 212 | } |
Tom Joseph | 638d066 | 2017-01-10 16:02:07 +0530 | [diff] [blame] | 213 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 214 | /** |
| 215 | * @brief Set Session's Confidentiality Algorithm |
| 216 | * |
| 217 | * @param[in] confAlgo - unique pointer to confidentiality algorithm |
| 218 | * instance |
| 219 | */ |
| 220 | void setCryptAlgo(std::unique_ptr<cipher::crypt::Interface>&& cryptAlgo) |
| 221 | { |
| 222 | cryptAlgoInterface = std::move(cryptAlgo); |
| 223 | } |
Tom Joseph | d8c7861 | 2017-03-31 10:17:30 +0530 | [diff] [blame] | 224 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 225 | /** @brief Check if confidentiality algorithm is enabled for this |
| 226 | * session. |
| 227 | * |
| 228 | * @return true if confidentiality algorithm is enabled else false. |
| 229 | */ |
| 230 | auto isCryptAlgoEnabled() |
| 231 | { |
| 232 | return cryptAlgoInterface ? true : false; |
| 233 | } |
Tom Joseph | 491dbd0 | 2017-01-24 18:20:41 +0530 | [diff] [blame] | 234 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 235 | void updateLastTransactionTime() |
| 236 | { |
| 237 | lastTime = std::chrono::steady_clock::now(); |
| 238 | } |
Tom Joseph | 491dbd0 | 2017-01-24 18:20:41 +0530 | [diff] [blame] | 239 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 240 | /** |
| 241 | * @brief Session Active Status |
| 242 | * |
| 243 | * Session Active status is decided upon the Session State and the last |
| 244 | * transaction time is compared against the session inactivity timeout. |
| 245 | * |
| 246 | */ |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 247 | bool isSessionActive(uint8_t sessionState) |
Vernon Mauery | 07e5b28 | 2018-10-24 13:11:23 -0700 | [diff] [blame] | 248 | { |
| 249 | auto currentTime = std::chrono::steady_clock::now(); |
| 250 | auto elapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>( |
| 251 | currentTime - lastTime); |
| 252 | |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 253 | State state = static_cast<session::State>(sessionState); |
| 254 | |
Vernon Mauery | 07e5b28 | 2018-10-24 13:11:23 -0700 | [diff] [blame] | 255 | switch (state) |
| 256 | { |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 257 | case State::setupInProgress: |
Vernon Mauery | 07e5b28 | 2018-10-24 13:11:23 -0700 | [diff] [blame] | 258 | if (elapsedSeconds < SESSION_SETUP_TIMEOUT) |
| 259 | { |
| 260 | return true; |
| 261 | } |
| 262 | break; |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 263 | case State::active: |
Vernon Mauery | 07e5b28 | 2018-10-24 13:11:23 -0700 | [diff] [blame] | 264 | if (elapsedSeconds < SESSION_INACTIVITY_TIMEOUT) |
| 265 | { |
| 266 | return true; |
| 267 | } |
| 268 | break; |
| 269 | default: |
| 270 | return false; |
| 271 | } |
| 272 | return false; |
| 273 | } |
Tom Joseph | 895df94 | 2017-03-31 10:19:40 +0530 | [diff] [blame] | 274 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 275 | /** |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 276 | * @brief Session's Requested Maximum Privilege Level |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 277 | */ |
Tom Joseph | 4021b1f | 2019-02-12 10:10:12 +0530 | [diff] [blame] | 278 | Privilege reqMaxPrivLevel; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 279 | |
Richard Marian Thomaiyar | 992e53c | 2019-03-03 13:30:46 +0530 | [diff] [blame] | 280 | /** |
| 281 | * @brief session's user & channel access details |
| 282 | */ |
| 283 | ipmi::PrivAccess sessionUserPrivAccess{}; |
| 284 | ipmi::ChannelAccess sessionChannelAccess{}; |
| 285 | |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 286 | SequenceNumbers sequenceNums; // Session Sequence Numbers |
| 287 | std::string userName{}; // User Name |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 288 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 289 | /** @brief Socket channel for communicating with the remote client.*/ |
| 290 | std::shared_ptr<udpsocket::Channel> channelPtr; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 291 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 292 | private: |
| 293 | SessionID bmcSessionID = 0; // BMC Session ID |
| 294 | SessionID remoteConsoleSessionID = 0; // Remote Console Session ID |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 295 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 296 | // Authentication Algorithm Interface for the Session |
| 297 | std::unique_ptr<cipher::rakp_auth::Interface> authAlgoInterface; |
Tom Joseph | cc27e12 | 2017-03-31 10:21:04 +0530 | [diff] [blame] | 298 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 299 | // Integrity Algorithm Interface for the Session |
| 300 | std::unique_ptr<cipher::integrity::Interface> integrityAlgoInterface = |
| 301 | nullptr; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 302 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 303 | // Confidentiality Algorithm Interface for the Session |
| 304 | std::unique_ptr<cipher::crypt::Interface> cryptAlgoInterface = nullptr; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 305 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 306 | // Last Transaction Time |
| 307 | decltype(std::chrono::steady_clock::now()) lastTime; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | } // namespace session |