Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 1 | #include "rakp12.hpp" |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "comm_module.hpp" |
| 4 | #include "endian.hpp" |
| 5 | #include "guid.hpp" |
| 6 | #include "main.hpp" |
| 7 | |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 8 | #include <openssl/rand.h> |
| 9 | |
| 10 | #include <algorithm> |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 11 | #include <cstring> |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 12 | #include <iomanip> |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 14 | #include <user_channel/channel_layer.hpp> |
| 15 | #include <user_channel/user_layer.hpp> |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 16 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 19 | namespace command |
| 20 | { |
| 21 | |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 22 | std::vector<uint8_t> RAKP12(const std::vector<uint8_t>& inPayload, |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 23 | const message::Handler& handler) |
| 24 | { |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 25 | std::vector<uint8_t> outPayload(sizeof(RAKP2response)); |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 26 | auto request = reinterpret_cast<const RAKP1request*>(inPayload.data()); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 27 | auto response = reinterpret_cast<RAKP2response*>(outPayload.data()); |
| 28 | |
| 29 | // Session ID zero is reserved for Session Setup |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 30 | if (endian::from_ipmi(request->managedSystemSessionID) == |
| 31 | session::SESSION_ZERO) |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 32 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 33 | log<level::INFO>("RAKP12: BMC invalid Session ID"); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 34 | response->rmcpStatusCode = |
| 35 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID); |
| 36 | return outPayload; |
| 37 | } |
| 38 | |
| 39 | std::shared_ptr<session::Session> session; |
| 40 | try |
| 41 | { |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 42 | session = |
| 43 | std::get<session::Manager&>(singletonPool) |
| 44 | .getSession(endian::from_ipmi(request->managedSystemSessionID)); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 45 | } |
| 46 | catch (std::exception& e) |
| 47 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 48 | log<level::ERR>("RAKP12 : session not found", |
| 49 | entry("EXCEPTION=%s", e.what())); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 50 | response->rmcpStatusCode = |
| 51 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID); |
| 52 | return outPayload; |
| 53 | } |
| 54 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 55 | auto rakp1Size = |
| 56 | sizeof(RAKP1request) - (userNameMaxLen - request->user_name_len); |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 57 | |
| 58 | // Validate user name length in the message |
| 59 | if (request->user_name_len > userNameMaxLen || |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 60 | inPayload.size() != rakp1Size) |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 61 | { |
| 62 | response->rmcpStatusCode = |
| 63 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_NAME_LENGTH); |
| 64 | return outPayload; |
| 65 | } |
| 66 | |
| 67 | session->userName.assign(request->user_name, request->user_name_len); |
| 68 | |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 69 | // Update transaction time |
| 70 | session->updateLastTransactionTime(); |
| 71 | |
| 72 | auto rcSessionID = endian::to_ipmi(session->getRCSessionID()); |
| 73 | auto bmcSessionID = endian::to_ipmi(session->getBMCSessionID()); |
| 74 | auto authAlgo = session->getAuthAlgo(); |
| 75 | |
| 76 | /* |
| 77 | * Generate Key Authentication Code - RAKP 2 |
| 78 | * |
| 79 | * 1) Remote Console Session ID - 4 bytes |
| 80 | * 2) Managed System Session ID - 4 bytes |
| 81 | * 3) Remote Console Random Number - 16 bytes |
| 82 | * 4) Managed System Random Number - 16 bytes |
| 83 | * 5) Managed System GUID - 16 bytes |
| 84 | * 6) Requested Privilege Level - 1 byte |
| 85 | * 7) User Name Length Byte - 1 byte (0 for 'null' username) |
| 86 | * 8) User Name - variable (absent for 'null' username) |
| 87 | */ |
| 88 | |
| 89 | std::vector<uint8_t> input; |
| 90 | input.resize(sizeof(rcSessionID) + sizeof(bmcSessionID) + |
| 91 | cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN + |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 92 | cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN + BMC_GUID_LEN + |
| 93 | sizeof(request->req_max_privilege_level) + |
| 94 | sizeof(request->user_name_len) + session->userName.size()); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 95 | |
| 96 | auto iter = input.begin(); |
| 97 | |
| 98 | // Remote Console Session ID |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 99 | std::copy_n(reinterpret_cast<uint8_t*>(&rcSessionID), sizeof(rcSessionID), |
| 100 | iter); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 101 | std::advance(iter, sizeof(rcSessionID)); |
| 102 | |
| 103 | // Managed System Session ID |
| 104 | std::copy_n(reinterpret_cast<uint8_t*>(&bmcSessionID), sizeof(bmcSessionID), |
| 105 | iter); |
| 106 | std::advance(iter, sizeof(bmcSessionID)); |
| 107 | |
| 108 | // Copy the Remote Console Random Number from the RAKP1 request to the |
| 109 | // Authentication Algorithm |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 110 | std::copy_n( |
| 111 | reinterpret_cast<const uint8_t*>(request->remote_console_random_number), |
| 112 | cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN, |
| 113 | authAlgo->rcRandomNum.begin()); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 114 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 115 | std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(), iter); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 116 | std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN); |
| 117 | |
| 118 | // Generate the Managed System Random Number |
| 119 | if (!RAND_bytes(input.data() + sizeof(rcSessionID) + sizeof(bmcSessionID) + |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 120 | cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN, |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 121 | cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN)) |
| 122 | { |
| 123 | response->rmcpStatusCode = |
| 124 | static_cast<uint8_t>(RAKP_ReturnCode::INSUFFICIENT_RESOURCE); |
| 125 | return outPayload; |
| 126 | } |
Richard Marian Thomaiyar | d5a4f45 | 2019-01-16 12:15:44 +0530 | [diff] [blame] | 127 | // As stated in Set Session Privilege Level command in IPMI Spec, when |
| 128 | // creating a session through Activate command / RAKP 1 message, it must be |
| 129 | // established with CALLBACK privilege if requested for callback. All other |
| 130 | // sessions are initialy set to USER privilege, regardless of the requested |
| 131 | // maximum privilege. |
| 132 | session->curPrivLevel = session::Privilege::CALLBACK; |
| 133 | if (static_cast<session::Privilege>(request->req_max_privilege_level & |
| 134 | session::reqMaxPrivMask) > |
| 135 | session::Privilege::CALLBACK) |
| 136 | { |
| 137 | session->curPrivLevel = session::Privilege::USER; |
| 138 | } |
Tom Joseph | 4021b1f | 2019-02-12 10:10:12 +0530 | [diff] [blame] | 139 | session->reqMaxPrivLevel = |
| 140 | static_cast<session::Privilege>(request->req_max_privilege_level); |
Richard Marian Thomaiyar | d91fd9d | 2018-12-06 12:03:50 +0530 | [diff] [blame] | 141 | if (request->user_name_len == 0) |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 142 | { |
Richard Marian Thomaiyar | d91fd9d | 2018-12-06 12:03:50 +0530 | [diff] [blame] | 143 | // Bail out, if user name is not specified. |
| 144 | // Yes, NULL user name is not supported for security reasons. |
Richard Marian Thomaiyar | 99b8784 | 2018-12-06 21:35:43 +0530 | [diff] [blame] | 145 | response->rmcpStatusCode = |
| 146 | static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME); |
| 147 | return outPayload; |
Richard Marian Thomaiyar | d2563c5 | 2018-11-29 11:49:10 +0530 | [diff] [blame] | 148 | } |
Richard Marian Thomaiyar | 99b8784 | 2018-12-06 21:35:43 +0530 | [diff] [blame] | 149 | |
| 150 | // Perform user name based lookup |
| 151 | std::string userName(request->user_name, request->user_name_len); |
| 152 | std::string passwd; |
| 153 | uint8_t userId = ipmi::ipmiUserGetUserId(userName); |
| 154 | if (userId == ipmi::invalidUserId) |
| 155 | { |
| 156 | response->rmcpStatusCode = |
| 157 | static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME); |
| 158 | return outPayload; |
| 159 | } |
| 160 | // check user is enabled before proceeding. |
| 161 | bool userEnabled = false; |
| 162 | ipmi::ipmiUserCheckEnabled(userId, userEnabled); |
| 163 | if (!userEnabled) |
| 164 | { |
| 165 | response->rmcpStatusCode = |
| 166 | static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE); |
| 167 | return outPayload; |
| 168 | } |
| 169 | // Get the user password for RAKP message authenticate |
| 170 | passwd = ipmi::ipmiUserGetPassword(userName); |
| 171 | if (passwd.empty()) |
| 172 | { |
| 173 | response->rmcpStatusCode = |
| 174 | static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME); |
| 175 | return outPayload; |
| 176 | } |
| 177 | ipmi::PrivAccess userAccess{}; |
| 178 | ipmi::ChannelAccess chAccess{}; |
| 179 | // TODO Replace with proper calls. |
| 180 | uint8_t chNum = static_cast<uint8_t>(ipmi::EChannelID::chanLan1); |
| 181 | // Get channel based access information |
| 182 | if ((ipmi::ipmiUserGetPrivilegeAccess(userId, chNum, userAccess) != |
| 183 | IPMI_CC_OK) || |
| 184 | (ipmi::getChannelAccessData(chNum, chAccess) != IPMI_CC_OK)) |
| 185 | { |
| 186 | response->rmcpStatusCode = |
| 187 | static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE); |
| 188 | return outPayload; |
| 189 | } |
Richard Marian Thomaiyar | 7e5d38d | 2019-03-02 22:11:41 +0530 | [diff] [blame^] | 190 | if (userAccess.privilege > static_cast<uint8_t>(session::Privilege::OEM)) |
| 191 | { |
| 192 | response->rmcpStatusCode = |
| 193 | static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE); |
| 194 | return outPayload; |
| 195 | } |
Richard Marian Thomaiyar | 99b8784 | 2018-12-06 21:35:43 +0530 | [diff] [blame] | 196 | session->chNum = chNum; |
Richard Marian Thomaiyar | d5a4f45 | 2019-01-16 12:15:44 +0530 | [diff] [blame] | 197 | // minimum privilege of Channel / User / session::privilege::USER/CALLBACK / |
| 198 | // has to be used as session current privilege level |
Richard Marian Thomaiyar | 99b8784 | 2018-12-06 21:35:43 +0530 | [diff] [blame] | 199 | uint8_t minPriv = 0; |
| 200 | if (chAccess.privLimit < userAccess.privilege) |
| 201 | { |
| 202 | minPriv = chAccess.privLimit; |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | minPriv = userAccess.privilege; |
| 207 | } |
| 208 | if (session->curPrivLevel > static_cast<session::Privilege>(minPriv)) |
| 209 | { |
| 210 | session->curPrivLevel = static_cast<session::Privilege>(minPriv); |
| 211 | } |
Richard Marian Thomaiyar | d91fd9d | 2018-12-06 12:03:50 +0530 | [diff] [blame] | 212 | // For username / privilege lookup, fail with UNAUTH_NAME, if requested |
Richard Marian Thomaiyar | d8e92fe | 2019-01-16 11:56:23 +0530 | [diff] [blame] | 213 | // max privilege does not match user privilege |
Richard Marian Thomaiyar | d91fd9d | 2018-12-06 12:03:50 +0530 | [diff] [blame] | 214 | if (((request->req_max_privilege_level & userNameOnlyLookupMask) == |
| 215 | userNamePrivLookup) && |
Richard Marian Thomaiyar | d8e92fe | 2019-01-16 11:56:23 +0530 | [diff] [blame] | 216 | ((request->req_max_privilege_level & session::reqMaxPrivMask) != |
Richard Marian Thomaiyar | d91fd9d | 2018-12-06 12:03:50 +0530 | [diff] [blame] | 217 | userAccess.privilege)) |
| 218 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 219 | log<level::INFO>( |
| 220 | "Username/Privilege lookup failed for requested privilege"); |
Richard Marian Thomaiyar | d91fd9d | 2018-12-06 12:03:50 +0530 | [diff] [blame] | 221 | response->rmcpStatusCode = |
| 222 | static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME); |
| 223 | return outPayload; |
| 224 | } |
Richard Marian Thomaiyar | 99b8784 | 2018-12-06 21:35:43 +0530 | [diff] [blame] | 225 | |
| 226 | std::fill(authAlgo->userKey.data(), |
| 227 | authAlgo->userKey.data() + authAlgo->userKey.size(), 0); |
| 228 | std::copy_n(passwd.c_str(), passwd.size(), authAlgo->userKey.data()); |
| 229 | |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 230 | // Copy the Managed System Random Number to the Authentication Algorithm |
| 231 | std::copy_n(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN, |
| 232 | authAlgo->bmcRandomNum.begin()); |
| 233 | std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN); |
| 234 | |
| 235 | // Managed System GUID |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 236 | std::copy_n(cache::guid.data(), cache::guid.size(), iter); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 237 | std::advance(iter, BMC_GUID_LEN); |
| 238 | |
| 239 | // Requested Privilege Level |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 240 | std::copy_n(&(request->req_max_privilege_level), |
| 241 | sizeof(request->req_max_privilege_level), iter); |
| 242 | std::advance(iter, sizeof(request->req_max_privilege_level)); |
| 243 | |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 244 | // User Name Length Byte |
| 245 | std::copy_n(&(request->user_name_len), sizeof(request->user_name_len), |
| 246 | iter); |
Tom Joseph | 56527b9 | 2018-03-21 19:31:58 +0530 | [diff] [blame] | 247 | std::advance(iter, sizeof(request->user_name_len)); |
| 248 | |
| 249 | std::copy_n(session->userName.data(), session->userName.size(), iter); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 250 | |
| 251 | // Generate Key Exchange Authentication Code - RAKP2 |
| 252 | auto output = authAlgo->generateHMAC(input); |
| 253 | |
| 254 | response->messageTag = request->messageTag; |
| 255 | response->rmcpStatusCode = static_cast<uint8_t>(RAKP_ReturnCode::NO_ERROR); |
| 256 | response->reserved = 0; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 257 | response->remoteConsoleSessionID = rcSessionID; |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 258 | |
| 259 | // Copy Managed System Random Number to the Response |
| 260 | std::copy(authAlgo->bmcRandomNum.begin(), authAlgo->bmcRandomNum.end(), |
| 261 | response->managed_system_random_number); |
| 262 | |
| 263 | // Copy System GUID to the Response |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 264 | std::copy_n(cache::guid.data(), cache::guid.size(), |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 265 | response->managed_system_guid); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 266 | |
| 267 | // Insert the HMAC output into the payload |
| 268 | outPayload.insert(outPayload.end(), output.begin(), output.end()); |
Tom Joseph | 8bb10b7 | 2016-12-06 17:47:56 +0530 | [diff] [blame] | 269 | return outPayload; |
| 270 | } |
| 271 | |
| 272 | } // namespace command |