Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 1 | #include "rakp34.hpp" |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <cstring> |
| 5 | #include <iostream> |
| 6 | |
| 7 | #include "comm_module.hpp" |
| 8 | #include "endian.hpp" |
| 9 | #include "guid.hpp" |
| 10 | #include "main.hpp" |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 11 | #include "rmcp.hpp" |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 12 | |
| 13 | namespace command |
| 14 | { |
| 15 | |
Tom Joseph | ef02fb3 | 2017-01-19 12:55:11 +0530 | [diff] [blame] | 16 | void applyIntegrityAlgo(const uint32_t bmcSessionID) |
| 17 | { |
| 18 | auto session = (std::get<session::Manager&>(singletonPool).getSession( |
| 19 | bmcSessionID)).lock(); |
| 20 | |
| 21 | auto authAlgo = session->getAuthAlgo(); |
| 22 | |
| 23 | switch (authAlgo->intAlgo) |
| 24 | { |
| 25 | case cipher::integrity::Algorithms::HMAC_SHA1_96: |
| 26 | { |
| 27 | session->setIntegrityAlgo( |
| 28 | std::make_unique<cipher::integrity::AlgoSHA1>( |
| 29 | authAlgo->sessionIntegrityKey)); |
| 30 | break; |
| 31 | } |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 32 | case cipher::integrity::Algorithms::HMAC_SHA256_128: |
| 33 | { |
| 34 | session->setIntegrityAlgo( |
| 35 | std::make_unique<cipher::integrity::AlgoSHA256>( |
| 36 | authAlgo->sessionIntegrityKey)); |
| 37 | break; |
| 38 | } |
Tom Joseph | ef02fb3 | 2017-01-19 12:55:11 +0530 | [diff] [blame] | 39 | default: |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
Tom Joseph | 4c766eb | 2017-01-24 18:24:57 +0530 | [diff] [blame] | 44 | void applyCryptAlgo(const uint32_t bmcSessionID) |
| 45 | { |
| 46 | auto session = (std::get<session::Manager&>(singletonPool).getSession( |
| 47 | bmcSessionID)).lock(); |
| 48 | |
| 49 | auto authAlgo = session->getAuthAlgo(); |
| 50 | |
| 51 | switch (authAlgo->cryptAlgo) |
| 52 | { |
| 53 | case cipher::crypt::Algorithms::AES_CBC_128: |
| 54 | { |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 55 | auto intAlgo = session->getIntegrityAlgo(); |
| 56 | auto k2 = intAlgo->generateKn( |
| 57 | authAlgo->sessionIntegrityKey, rmcp::const_2); |
| 58 | session->setCryptAlgo( |
| 59 | std::make_unique<cipher::crypt::AlgoAES128>(k2)); |
Tom Joseph | 4c766eb | 2017-01-24 18:24:57 +0530 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | default: |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 67 | std::vector<uint8_t> RAKP34(const std::vector<uint8_t>& inPayload, |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 68 | const message::Handler& handler) |
| 69 | { |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 70 | |
| 71 | std::vector<uint8_t> outPayload(sizeof(RAKP4response)); |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 72 | auto request = reinterpret_cast<const RAKP3request*>(inPayload.data()); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 73 | auto response = reinterpret_cast<RAKP4response*>(outPayload.data()); |
| 74 | |
| 75 | // Check if the RAKP3 Payload Length is as expected |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 76 | if (inPayload.size() < sizeof(RAKP3request)) |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 77 | { |
| 78 | std::cerr << "RAKP34: Invalid RAKP3 request\n"; |
| 79 | response->rmcpStatusCode = |
| 80 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_INTEGRITY_VALUE); |
| 81 | return outPayload; |
| 82 | } |
| 83 | |
| 84 | // Session ID zero is reserved for Session Setup |
| 85 | if(endian::from_ipmi(request->managedSystemSessionID) == |
| 86 | session::SESSION_ZERO) |
| 87 | { |
| 88 | std::cerr << "RAKP34: BMC invalid Session ID\n"; |
| 89 | response->rmcpStatusCode = |
| 90 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID); |
| 91 | return outPayload; |
| 92 | } |
| 93 | |
| 94 | std::shared_ptr<session::Session> session; |
| 95 | try |
| 96 | { |
| 97 | session = (std::get<session::Manager&>(singletonPool).getSession( |
| 98 | endian::from_ipmi(request->managedSystemSessionID))).lock(); |
| 99 | } |
| 100 | catch (std::exception& e) |
| 101 | { |
| 102 | std::cerr << e.what() << "\n"; |
| 103 | response->rmcpStatusCode = |
| 104 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID); |
| 105 | return outPayload; |
| 106 | } |
| 107 | |
| 108 | session->updateLastTransactionTime(); |
| 109 | |
| 110 | auto authAlgo = session->getAuthAlgo(); |
| 111 | /* |
| 112 | * Key Authentication Code - RAKP 3 |
| 113 | * |
| 114 | * 1) Managed System Random Number - 16 bytes |
| 115 | * 2) Remote Console Session ID - 4 bytes |
| 116 | * 3) Session Privilege Level - 1 byte |
| 117 | * 4) User Name Length Byte - 1 byte (0 for 'null' username) |
| 118 | * 5) User Name - variable (absent for 'null' username) |
| 119 | */ |
| 120 | |
| 121 | // Remote Console Session ID |
| 122 | auto rcSessionID = endian::to_ipmi(session->getRCSessionID()); |
| 123 | |
| 124 | // Session Privilege Level |
| 125 | auto sessPrivLevel = static_cast<uint8_t>(session->curPrivLevel); |
| 126 | |
| 127 | // User Name Length Byte |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 128 | auto userLength = static_cast<uint8_t>(session->userName.size()); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 129 | |
| 130 | std::vector<uint8_t> input; |
| 131 | input.resize(cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN + |
| 132 | sizeof(rcSessionID) + sizeof(sessPrivLevel) + |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 133 | sizeof(userLength) + userLength); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 134 | |
| 135 | auto iter = input.begin(); |
| 136 | |
| 137 | // Managed System Random Number |
| 138 | std::copy(authAlgo->bmcRandomNum.begin(), authAlgo->bmcRandomNum.end(), |
| 139 | iter); |
| 140 | std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN); |
| 141 | |
| 142 | // Remote Console Session ID |
| 143 | std::copy_n(reinterpret_cast<uint8_t*>(&rcSessionID), sizeof(rcSessionID), |
| 144 | iter); |
| 145 | std::advance(iter, sizeof(rcSessionID)); |
| 146 | |
| 147 | // Session Privilege Level |
| 148 | std::copy_n(reinterpret_cast<uint8_t*>(&sessPrivLevel), |
| 149 | sizeof(sessPrivLevel), iter); |
| 150 | std::advance(iter, sizeof(sessPrivLevel)); |
| 151 | |
| 152 | // User Name Length Byte |
| 153 | std::copy_n(&userLength, sizeof(userLength), iter); |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 154 | std::advance(iter, sizeof(userLength)); |
| 155 | |
| 156 | std::copy_n(session->userName.data(), userLength, iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 157 | |
| 158 | // Generate Key Exchange Authentication Code - RAKP2 |
| 159 | auto output = authAlgo->generateHMAC(input); |
| 160 | |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 161 | if (inPayload.size() != (sizeof(RAKP3request) + output.size()) || |
| 162 | std::memcmp(output.data(), request+1, output.size())) |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 163 | { |
| 164 | std::cerr << "Mismatch in HMAC sent by remote console\n"; |
| 165 | |
| 166 | response->messageTag = request->messageTag; |
| 167 | response->rmcpStatusCode = static_cast<uint8_t> |
| 168 | (RAKP_ReturnCode::INVALID_INTEGRITY_VALUE); |
| 169 | response->reserved = 0; |
| 170 | response->remoteConsoleSessionID = rcSessionID; |
| 171 | |
| 172 | //close the session |
| 173 | std::get<session::Manager&>(singletonPool).stopSession( |
| 174 | session->getBMCSessionID()); |
| 175 | |
| 176 | return outPayload; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Session Integrity Key |
| 181 | * |
| 182 | * 1) Remote Console Random Number - 16 bytes |
| 183 | * 2) Managed System Random Number - 16 bytes |
| 184 | * 3) Session Privilege Level - 1 byte |
| 185 | * 4) User Name Length Byte - 1 byte (0 for 'null' username) |
| 186 | * 5) User Name - variable (absent for 'null' username) |
| 187 | */ |
| 188 | |
| 189 | input.clear(); |
| 190 | |
| 191 | input.resize(cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN + |
| 192 | cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN + |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 193 | sizeof(sessPrivLevel) + sizeof(userLength) + userLength); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 194 | iter = input.begin(); |
| 195 | |
| 196 | // Remote Console Random Number |
| 197 | std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(), |
| 198 | iter); |
| 199 | std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN); |
| 200 | |
| 201 | // Managed Console Random Number |
| 202 | std::copy(authAlgo->bmcRandomNum.begin(), authAlgo->bmcRandomNum.end(), |
| 203 | iter); |
| 204 | std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN); |
| 205 | |
| 206 | // Session Privilege Level |
| 207 | std::copy_n(reinterpret_cast<uint8_t*>(&sessPrivLevel), |
| 208 | sizeof(sessPrivLevel), iter); |
| 209 | std::advance(iter, sizeof(sessPrivLevel)); |
| 210 | |
| 211 | // User Name Length Byte |
| 212 | std::copy_n(&userLength, sizeof(userLength), iter); |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 213 | std::advance(iter, sizeof(userLength)); |
| 214 | |
| 215 | std::copy_n(session->userName.data(), userLength, iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 216 | |
| 217 | // Generate Session Integrity Key |
| 218 | auto sikOutput = authAlgo->generateHMAC(input); |
| 219 | |
| 220 | // Update the SIK in the Authentication Algo Interface |
| 221 | authAlgo->sessionIntegrityKey.insert(authAlgo->sessionIntegrityKey.begin(), |
| 222 | sikOutput.begin(), sikOutput.end()); |
| 223 | |
| 224 | /* |
| 225 | * Integrity Check Value |
| 226 | * |
| 227 | * 1) Remote Console Random Number - 16 bytes |
| 228 | * 2) Managed System Session ID - 4 bytes |
| 229 | * 3) Managed System GUID - 16 bytes |
| 230 | */ |
| 231 | |
| 232 | // Get Managed System Session ID |
| 233 | auto bmcSessionID = endian::to_ipmi(session->getBMCSessionID()); |
| 234 | |
| 235 | input.clear(); |
| 236 | |
| 237 | input.resize(cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN + |
| 238 | sizeof(bmcSessionID) + BMC_GUID_LEN); |
| 239 | iter = input.begin(); |
| 240 | |
| 241 | // Remote Console Random Number |
| 242 | std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(), |
| 243 | iter); |
| 244 | std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN); |
| 245 | |
| 246 | // Managed System Session ID |
| 247 | std::copy_n(reinterpret_cast<uint8_t*>(&bmcSessionID), sizeof(bmcSessionID), |
| 248 | iter); |
| 249 | std::advance(iter, sizeof(bmcSessionID)); |
| 250 | |
| 251 | // Managed System GUID |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 252 | std::copy_n(cache::guid.data(), cache::guid.size(), iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 253 | |
| 254 | // Integrity Check Value |
| 255 | auto icv = authAlgo->generateICV(input); |
| 256 | |
| 257 | outPayload.resize(sizeof(RAKP4response)); |
| 258 | |
| 259 | response->messageTag = request->messageTag; |
| 260 | response->rmcpStatusCode = static_cast<uint8_t>(RAKP_ReturnCode::NO_ERROR); |
| 261 | response->reserved = 0; |
| 262 | response->remoteConsoleSessionID = rcSessionID; |
| 263 | |
| 264 | // Insert the HMAC output into the payload |
| 265 | outPayload.insert(outPayload.end(), icv.begin(), icv.end()); |
| 266 | |
Tom Joseph | ef02fb3 | 2017-01-19 12:55:11 +0530 | [diff] [blame] | 267 | // Set the Integrity Algorithm |
| 268 | applyIntegrityAlgo(session->getBMCSessionID()); |
Tom Joseph | 818d070 | 2017-01-10 16:39:38 +0530 | [diff] [blame] | 269 | |
Tom Joseph | 4c766eb | 2017-01-24 18:24:57 +0530 | [diff] [blame] | 270 | // Set the Confidentiality Algorithm |
| 271 | applyCryptAlgo(session->getBMCSessionID()); |
| 272 | |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 273 | session->state = session::State::ACTIVE; |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 274 | return outPayload; |
| 275 | } |
| 276 | |
| 277 | } // namespace command |