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