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 | |
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 | */ |
| 99 | class Session |
| 100 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 101 | public: |
| 102 | Session() = default; |
| 103 | ~Session() = default; |
| 104 | Session(const Session&) = delete; |
| 105 | Session& operator=(const Session&) = delete; |
| 106 | Session(Session&&) = default; |
| 107 | Session& operator=(Session&&) = default; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 108 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 109 | /** |
| 110 | * @brief Session Constructor |
| 111 | * |
| 112 | * This is issued by the Session Manager when a session is started for |
| 113 | * the Open SessionRequest command |
| 114 | * |
| 115 | * @param[in] inRemoteConsoleSessID - Remote Console Session ID |
| 116 | * @param[in] priv - Privilege Level requested in the Command |
| 117 | */ |
| 118 | Session(SessionID inRemoteConsoleSessID, Privilege priv) : |
Vernon Mauery | 8977d12 | 2018-10-24 14:02:16 -0700 | [diff] [blame] | 119 | curPrivLevel(priv), bmcSessionID(crypto::prng::rand()), |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 120 | remoteConsoleSessionID(inRemoteConsoleSessID) |
| 121 | { |
| 122 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 123 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 124 | auto getBMCSessionID() const |
| 125 | { |
| 126 | return bmcSessionID; |
| 127 | } |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 128 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 129 | auto getRCSessionID() const |
| 130 | { |
| 131 | return remoteConsoleSessionID; |
| 132 | } |
| 133 | |
| 134 | auto getAuthAlgo() const |
| 135 | { |
| 136 | if (authAlgoInterface) |
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 | return authAlgoInterface.get(); |
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 | else |
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 | throw std::runtime_error("Authentication Algorithm Empty"); |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 143 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 144 | } |
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 | void setAuthAlgo(std::unique_ptr<cipher::rakp_auth::Interface>&& inAuthAlgo) |
| 147 | { |
| 148 | authAlgoInterface = std::move(inAuthAlgo); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @brief Get Session's Integrity Algorithm |
| 153 | * |
| 154 | * @return pointer to the integrity algorithm |
| 155 | */ |
| 156 | auto getIntegrityAlgo() const |
| 157 | { |
| 158 | if (integrityAlgoInterface) |
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 | return integrityAlgoInterface.get(); |
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 | else |
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 | throw std::runtime_error("Integrity Algorithm Empty"); |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 165 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 166 | } |
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 | /** |
| 169 | * @brief Set Session's Integrity Algorithm |
| 170 | * |
| 171 | * @param[in] integrityAlgo - unique pointer to integrity algorithm |
| 172 | * instance |
| 173 | */ |
| 174 | void setIntegrityAlgo( |
| 175 | std::unique_ptr<cipher::integrity::Interface>&& integrityAlgo) |
| 176 | { |
| 177 | integrityAlgoInterface = std::move(integrityAlgo); |
| 178 | } |
| 179 | |
| 180 | /** @brief Check if integrity algorithm is enabled for this session. |
| 181 | * |
| 182 | * @return true if integrity algorithm is enabled else false. |
| 183 | */ |
| 184 | auto isIntegrityAlgoEnabled() |
| 185 | { |
| 186 | return integrityAlgoInterface ? true : false; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @brief Get Session's Confidentiality Algorithm |
| 191 | * |
| 192 | * @return pointer to the confidentiality algorithm |
| 193 | */ |
| 194 | auto getCryptAlgo() const |
| 195 | { |
| 196 | if (cryptAlgoInterface) |
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 | return cryptAlgoInterface.get(); |
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 | else |
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 | throw std::runtime_error("Confidentiality Algorithm Empty"); |
Tom Joseph | 638d066 | 2017-01-10 16:02:07 +0530 | [diff] [blame] | 203 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 204 | } |
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 | /** |
| 207 | * @brief Set Session's Confidentiality Algorithm |
| 208 | * |
| 209 | * @param[in] confAlgo - unique pointer to confidentiality algorithm |
| 210 | * instance |
| 211 | */ |
| 212 | void setCryptAlgo(std::unique_ptr<cipher::crypt::Interface>&& cryptAlgo) |
| 213 | { |
| 214 | cryptAlgoInterface = std::move(cryptAlgo); |
| 215 | } |
Tom Joseph | d8c7861 | 2017-03-31 10:17:30 +0530 | [diff] [blame] | 216 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 217 | /** @brief Check if confidentiality algorithm is enabled for this |
| 218 | * session. |
| 219 | * |
| 220 | * @return true if confidentiality algorithm is enabled else false. |
| 221 | */ |
| 222 | auto isCryptAlgoEnabled() |
| 223 | { |
| 224 | return cryptAlgoInterface ? true : false; |
| 225 | } |
Tom Joseph | 491dbd0 | 2017-01-24 18:20:41 +0530 | [diff] [blame] | 226 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 227 | void updateLastTransactionTime() |
| 228 | { |
| 229 | lastTime = std::chrono::steady_clock::now(); |
| 230 | } |
Tom Joseph | 491dbd0 | 2017-01-24 18:20:41 +0530 | [diff] [blame] | 231 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 232 | /** |
| 233 | * @brief Session Active Status |
| 234 | * |
| 235 | * Session Active status is decided upon the Session State and the last |
| 236 | * transaction time is compared against the session inactivity timeout. |
| 237 | * |
| 238 | */ |
Vernon Mauery | 07e5b28 | 2018-10-24 13:11:23 -0700 | [diff] [blame] | 239 | bool isSessionActive() |
| 240 | { |
| 241 | auto currentTime = std::chrono::steady_clock::now(); |
| 242 | auto elapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>( |
| 243 | currentTime - lastTime); |
| 244 | |
| 245 | switch (state) |
| 246 | { |
| 247 | case State::SETUP_IN_PROGRESS: |
| 248 | if (elapsedSeconds < SESSION_SETUP_TIMEOUT) |
| 249 | { |
| 250 | return true; |
| 251 | } |
| 252 | break; |
| 253 | case State::ACTIVE: |
| 254 | if (elapsedSeconds < SESSION_INACTIVITY_TIMEOUT) |
| 255 | { |
| 256 | return true; |
| 257 | } |
| 258 | break; |
| 259 | default: |
| 260 | return false; |
| 261 | } |
| 262 | return false; |
| 263 | } |
Tom Joseph | 895df94 | 2017-03-31 10:19:40 +0530 | [diff] [blame] | 264 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 265 | /** |
| 266 | * @brief Session's Current Privilege Level |
| 267 | */ |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 268 | Privilege curPrivLevel = Privilege::CALLBACK; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 269 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 270 | /** |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 271 | * @brief Session's Requested Maximum Privilege Level |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 272 | */ |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 273 | uint8_t reqMaxPrivLevel; |
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 | SequenceNumbers sequenceNums; // Session Sequence Numbers |
| 276 | State state = State::INACTIVE; // Session State |
| 277 | std::string userName{}; // User Name |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 278 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 279 | /** @brief Socket channel for communicating with the remote client.*/ |
| 280 | std::shared_ptr<udpsocket::Channel> channelPtr; |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 281 | uint8_t chNum; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 282 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 283 | private: |
| 284 | SessionID bmcSessionID = 0; // BMC Session ID |
| 285 | SessionID remoteConsoleSessionID = 0; // Remote Console Session ID |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 286 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 287 | // Authentication Algorithm Interface for the Session |
| 288 | std::unique_ptr<cipher::rakp_auth::Interface> authAlgoInterface; |
Tom Joseph | cc27e12 | 2017-03-31 10:21:04 +0530 | [diff] [blame] | 289 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 290 | // Integrity Algorithm Interface for the Session |
| 291 | std::unique_ptr<cipher::integrity::Interface> integrityAlgoInterface = |
| 292 | nullptr; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 293 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 294 | // Confidentiality Algorithm Interface for the Session |
| 295 | std::unique_ptr<cipher::crypt::Interface> cryptAlgoInterface = nullptr; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 296 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 297 | // Last Transaction Time |
| 298 | decltype(std::chrono::steady_clock::now()) lastTime; |
Tom Joseph | f0ca513 | 2016-08-09 08:16:12 -0500 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | } // namespace session |