Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 1 | #include "rakp34.hpp" |
| 2 | |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 3 | #include "comm_module.hpp" |
| 4 | #include "endian.hpp" |
| 5 | #include "guid.hpp" |
| 6 | #include "main.hpp" |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 7 | #include "rmcp.hpp" |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 8 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <cstring> |
| 11 | #include <iostream> |
| 12 | |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 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 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 18 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 19 | std::get<session::Manager&>(singletonPool).getSession(bmcSessionID); |
Tom Joseph | ef02fb3 | 2017-01-19 12:55:11 +0530 | [diff] [blame] | 20 | |
| 21 | auto authAlgo = session->getAuthAlgo(); |
| 22 | |
| 23 | switch (authAlgo->intAlgo) |
| 24 | { |
| 25 | case cipher::integrity::Algorithms::HMAC_SHA1_96: |
| 26 | { |
| 27 | session->setIntegrityAlgo( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 28 | std::make_unique<cipher::integrity::AlgoSHA1>( |
| 29 | authAlgo->sessionIntegrityKey)); |
Tom Joseph | ef02fb3 | 2017-01-19 12:55:11 +0530 | [diff] [blame] | 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>( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 36 | authAlgo->sessionIntegrityKey)); |
Vernon Mauery | 7e9e2ef | 2017-11-29 08:36:29 -0800 | [diff] [blame] | 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 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 46 | auto session = |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 47 | std::get<session::Manager&>(singletonPool).getSession(bmcSessionID); |
Tom Joseph | 4c766eb | 2017-01-24 18:24:57 +0530 | [diff] [blame] | 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(); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 56 | auto k2 = intAlgo->generateKn(authAlgo->sessionIntegrityKey, |
| 57 | rmcp::const_2); |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 58 | session->setCryptAlgo( |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 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 |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 85 | if (endian::from_ipmi(request->managedSystemSessionID) == |
| 86 | session::SESSION_ZERO) |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 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 | { |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 97 | session = |
| 98 | std::get<session::Manager&>(singletonPool) |
| 99 | .getSession(endian::from_ipmi(request->managedSystemSessionID)); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 100 | } |
| 101 | catch (std::exception& e) |
| 102 | { |
| 103 | std::cerr << e.what() << "\n"; |
| 104 | response->rmcpStatusCode = |
| 105 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID); |
| 106 | return outPayload; |
| 107 | } |
| 108 | |
| 109 | session->updateLastTransactionTime(); |
| 110 | |
| 111 | auto authAlgo = session->getAuthAlgo(); |
| 112 | /* |
| 113 | * Key Authentication Code - RAKP 3 |
| 114 | * |
| 115 | * 1) Managed System Random Number - 16 bytes |
| 116 | * 2) Remote Console Session ID - 4 bytes |
| 117 | * 3) Session Privilege Level - 1 byte |
| 118 | * 4) User Name Length Byte - 1 byte (0 for 'null' username) |
| 119 | * 5) User Name - variable (absent for 'null' username) |
| 120 | */ |
| 121 | |
| 122 | // Remote Console Session ID |
| 123 | auto rcSessionID = endian::to_ipmi(session->getRCSessionID()); |
| 124 | |
| 125 | // Session Privilege Level |
| 126 | auto sessPrivLevel = static_cast<uint8_t>(session->curPrivLevel); |
| 127 | |
| 128 | // User Name Length Byte |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 129 | auto userLength = static_cast<uint8_t>(session->userName.size()); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 130 | |
| 131 | std::vector<uint8_t> input; |
| 132 | input.resize(cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN + |
| 133 | sizeof(rcSessionID) + sizeof(sessPrivLevel) + |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 134 | sizeof(userLength) + userLength); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 135 | |
| 136 | auto iter = input.begin(); |
| 137 | |
| 138 | // Managed System Random Number |
| 139 | std::copy(authAlgo->bmcRandomNum.begin(), authAlgo->bmcRandomNum.end(), |
| 140 | iter); |
| 141 | std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN); |
| 142 | |
| 143 | // Remote Console Session ID |
| 144 | std::copy_n(reinterpret_cast<uint8_t*>(&rcSessionID), sizeof(rcSessionID), |
| 145 | iter); |
| 146 | std::advance(iter, sizeof(rcSessionID)); |
| 147 | |
| 148 | // Session Privilege Level |
| 149 | std::copy_n(reinterpret_cast<uint8_t*>(&sessPrivLevel), |
| 150 | sizeof(sessPrivLevel), iter); |
| 151 | std::advance(iter, sizeof(sessPrivLevel)); |
| 152 | |
| 153 | // User Name Length Byte |
| 154 | std::copy_n(&userLength, sizeof(userLength), iter); |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 155 | std::advance(iter, sizeof(userLength)); |
| 156 | |
| 157 | std::copy_n(session->userName.data(), userLength, iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 158 | |
| 159 | // Generate Key Exchange Authentication Code - RAKP2 |
| 160 | auto output = authAlgo->generateHMAC(input); |
| 161 | |
Vernon Mauery | 9b307be | 2017-11-22 09:28:16 -0800 | [diff] [blame] | 162 | if (inPayload.size() != (sizeof(RAKP3request) + output.size()) || |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 163 | std::memcmp(output.data(), request + 1, output.size())) |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 164 | { |
| 165 | std::cerr << "Mismatch in HMAC sent by remote console\n"; |
| 166 | |
| 167 | response->messageTag = request->messageTag; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 168 | response->rmcpStatusCode = |
| 169 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_INTEGRITY_VALUE); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 170 | response->reserved = 0; |
| 171 | response->remoteConsoleSessionID = rcSessionID; |
| 172 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 173 | // close the session |
| 174 | std::get<session::Manager&>(singletonPool) |
| 175 | .stopSession(session->getBMCSessionID()); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 176 | |
| 177 | return outPayload; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Session Integrity Key |
| 182 | * |
| 183 | * 1) Remote Console Random Number - 16 bytes |
| 184 | * 2) Managed System Random Number - 16 bytes |
| 185 | * 3) Session Privilege Level - 1 byte |
| 186 | * 4) User Name Length Byte - 1 byte (0 for 'null' username) |
| 187 | * 5) User Name - variable (absent for 'null' username) |
| 188 | */ |
| 189 | |
| 190 | input.clear(); |
| 191 | |
| 192 | input.resize(cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN + |
| 193 | cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN + |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 194 | sizeof(sessPrivLevel) + sizeof(userLength) + userLength); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 195 | iter = input.begin(); |
| 196 | |
| 197 | // Remote Console Random Number |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 198 | std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(), iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 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 |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 242 | std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(), iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 243 | std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN); |
| 244 | |
| 245 | // Managed System Session ID |
| 246 | std::copy_n(reinterpret_cast<uint8_t*>(&bmcSessionID), sizeof(bmcSessionID), |
| 247 | iter); |
| 248 | std::advance(iter, sizeof(bmcSessionID)); |
| 249 | |
| 250 | // Managed System GUID |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 251 | std::copy_n(cache::guid.data(), cache::guid.size(), iter); |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 252 | |
| 253 | // Integrity Check Value |
| 254 | auto icv = authAlgo->generateICV(input); |
| 255 | |
| 256 | outPayload.resize(sizeof(RAKP4response)); |
| 257 | |
| 258 | response->messageTag = request->messageTag; |
| 259 | response->rmcpStatusCode = static_cast<uint8_t>(RAKP_ReturnCode::NO_ERROR); |
| 260 | response->reserved = 0; |
| 261 | response->remoteConsoleSessionID = rcSessionID; |
| 262 | |
| 263 | // Insert the HMAC output into the payload |
| 264 | outPayload.insert(outPayload.end(), icv.begin(), icv.end()); |
| 265 | |
Tom Joseph | ef02fb3 | 2017-01-19 12:55:11 +0530 | [diff] [blame] | 266 | // Set the Integrity Algorithm |
| 267 | applyIntegrityAlgo(session->getBMCSessionID()); |
Tom Joseph | 818d070 | 2017-01-10 16:39:38 +0530 | [diff] [blame] | 268 | |
Tom Joseph | 4c766eb | 2017-01-24 18:24:57 +0530 | [diff] [blame] | 269 | // Set the Confidentiality Algorithm |
| 270 | applyCryptAlgo(session->getBMCSessionID()); |
| 271 | |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 272 | session->state = session::State::ACTIVE; |
Tom Joseph | 50fb50a | 2016-12-06 17:51:06 +0530 | [diff] [blame] | 273 | return outPayload; |
| 274 | } |
| 275 | |
| 276 | } // namespace command |