blob: 1130ee95a6d67442e3886af92febbcfdc45cf20a [file] [log] [blame]
Tom Joseph8bb10b72016-12-06 17:47:56 +05301#include "rakp12.hpp"
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include "comm_module.hpp"
4#include "endian.hpp"
5#include "guid.hpp"
Vernon Mauery2085ae02021-06-10 11:51:00 -07006#include "sessions_manager.hpp"
Vernon Mauery9e801a22018-10-12 13:20:49 -07007
Tom Joseph8bb10b72016-12-06 17:47:56 +05308#include <openssl/rand.h>
9
10#include <algorithm>
Tom Joseph56527b92018-03-21 19:31:58 +053011#include <cstring>
Tom Joseph8bb10b72016-12-06 17:47:56 +053012#include <iomanip>
Jayaprakash Mutyala05c14472020-10-19 10:26:24 +000013#include <ipmid/types.hpp>
Vernon Maueryfc37e592018-12-19 14:55:15 -080014#include <phosphor-logging/log.hpp>
Tom Joseph8bb10b72016-12-06 17:47:56 +053015
Vernon Maueryfc37e592018-12-19 14:55:15 -080016using namespace phosphor::logging;
17
Tom Joseph8bb10b72016-12-06 17:47:56 +053018namespace command
19{
20
AppaRao Puliecb32fb2020-07-01 22:55:38 +053021bool isChannelAccessModeEnabled(const uint8_t accessMode)
22{
23 return accessMode !=
24 static_cast<uint8_t>(ipmi::EChannelAccessMode::disabled);
25}
26
sunitakx0e0546f2021-06-15 08:57:10 +000027void logInvalidLoginRedfishEvent(const std::string& journalMsg,
28 const std::optional<std::string>& messageArgs)
29{
30 static constexpr std::string_view openBMCMessageRegistryVersion = "0.1.";
31 std::string messageID = "OpenBMC." +
32 std::string(openBMCMessageRegistryVersion) +
33 "InvalidLoginAttempted";
34 phosphor::logging::log<phosphor::logging::level::ERR>(
35 journalMsg.c_str(),
36 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", messageID.c_str()),
37 phosphor::logging::entry("REDFISH_MESSAGE_ARGS=%s",
38 messageArgs.value().c_str()));
39}
Tom Joseph18a45e92017-04-11 11:30:44 +053040std::vector<uint8_t> RAKP12(const std::vector<uint8_t>& inPayload,
Vernon Mauery41ff9b52021-06-11 11:37:40 -070041 std::shared_ptr<message::Handler>& handler)
Tom Joseph8bb10b72016-12-06 17:47:56 +053042{
Tom Joseph18a45e92017-04-11 11:30:44 +053043 auto request = reinterpret_cast<const RAKP1request*>(inPayload.data());
Zhikui Ren2b1edef2020-07-24 14:32:13 -070044 // verify inPayload minimum size
45 if (inPayload.size() < (sizeof(*request) - userNameMaxLen))
46 {
47 std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID};
48 return errorPayload;
49 }
50
51 std::vector<uint8_t> outPayload(sizeof(RAKP2response));
Tom Joseph8bb10b72016-12-06 17:47:56 +053052 auto response = reinterpret_cast<RAKP2response*>(outPayload.data());
53
54 // Session ID zero is reserved for Session Setup
Vernon Mauery9e801a22018-10-12 13:20:49 -070055 if (endian::from_ipmi(request->managedSystemSessionID) ==
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053056 session::sessionZero)
Tom Joseph8bb10b72016-12-06 17:47:56 +053057 {
Vernon Maueryfc37e592018-12-19 14:55:15 -080058 log<level::INFO>("RAKP12: BMC invalid Session ID");
Tom Joseph8bb10b72016-12-06 17:47:56 +053059 response->rmcpStatusCode =
60 static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
61 return outPayload;
62 }
63
64 std::shared_ptr<session::Session> session;
65 try
66 {
Vernon Mauery2085ae02021-06-10 11:51:00 -070067 session = session::Manager::get().getSession(
68 endian::from_ipmi(request->managedSystemSessionID));
Tom Joseph8bb10b72016-12-06 17:47:56 +053069 }
70 catch (std::exception& e)
71 {
Vernon Maueryfc37e592018-12-19 14:55:15 -080072 log<level::ERR>("RAKP12 : session not found",
73 entry("EXCEPTION=%s", e.what()));
Tom Joseph8bb10b72016-12-06 17:47:56 +053074 response->rmcpStatusCode =
75 static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
76 return outPayload;
77 }
78
Vernon Mauery9e801a22018-10-12 13:20:49 -070079 auto rakp1Size =
80 sizeof(RAKP1request) - (userNameMaxLen - request->user_name_len);
Tom Joseph56527b92018-03-21 19:31:58 +053081
sunitakx0e0546f2021-06-15 08:57:10 +000082 std::string message = "Invalid login attempted via RCMPP interface ";
Tom Joseph56527b92018-03-21 19:31:58 +053083 // Validate user name length in the message
84 if (request->user_name_len > userNameMaxLen ||
Vernon Mauery9e801a22018-10-12 13:20:49 -070085 inPayload.size() != rakp1Size)
Tom Joseph56527b92018-03-21 19:31:58 +053086 {
87 response->rmcpStatusCode =
88 static_cast<uint8_t>(RAKP_ReturnCode::INVALID_NAME_LENGTH);
sunitakx0e0546f2021-06-15 08:57:10 +000089 logInvalidLoginRedfishEvent(message);
Tom Joseph56527b92018-03-21 19:31:58 +053090 return outPayload;
91 }
92
93 session->userName.assign(request->user_name, request->user_name_len);
94
Tom Joseph8bb10b72016-12-06 17:47:56 +053095 // Update transaction time
96 session->updateLastTransactionTime();
97
98 auto rcSessionID = endian::to_ipmi(session->getRCSessionID());
99 auto bmcSessionID = endian::to_ipmi(session->getBMCSessionID());
100 auto authAlgo = session->getAuthAlgo();
101
102 /*
103 * Generate Key Authentication Code - RAKP 2
104 *
105 * 1) Remote Console Session ID - 4 bytes
106 * 2) Managed System Session ID - 4 bytes
107 * 3) Remote Console Random Number - 16 bytes
108 * 4) Managed System Random Number - 16 bytes
109 * 5) Managed System GUID - 16 bytes
110 * 6) Requested Privilege Level - 1 byte
111 * 7) User Name Length Byte - 1 byte (0 for 'null' username)
112 * 8) User Name - variable (absent for 'null' username)
113 */
114
115 std::vector<uint8_t> input;
116 input.resize(sizeof(rcSessionID) + sizeof(bmcSessionID) +
117 cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN +
Vernon Mauery9e801a22018-10-12 13:20:49 -0700118 cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN + BMC_GUID_LEN +
119 sizeof(request->req_max_privilege_level) +
120 sizeof(request->user_name_len) + session->userName.size());
Tom Joseph8bb10b72016-12-06 17:47:56 +0530121
122 auto iter = input.begin();
123
124 // Remote Console Session ID
Vernon Mauery9e801a22018-10-12 13:20:49 -0700125 std::copy_n(reinterpret_cast<uint8_t*>(&rcSessionID), sizeof(rcSessionID),
126 iter);
Tom Joseph8bb10b72016-12-06 17:47:56 +0530127 std::advance(iter, sizeof(rcSessionID));
128
129 // Managed System Session ID
130 std::copy_n(reinterpret_cast<uint8_t*>(&bmcSessionID), sizeof(bmcSessionID),
131 iter);
132 std::advance(iter, sizeof(bmcSessionID));
133
134 // Copy the Remote Console Random Number from the RAKP1 request to the
135 // Authentication Algorithm
Vernon Mauery9e801a22018-10-12 13:20:49 -0700136 std::copy_n(
137 reinterpret_cast<const uint8_t*>(request->remote_console_random_number),
138 cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN,
139 authAlgo->rcRandomNum.begin());
Tom Joseph8bb10b72016-12-06 17:47:56 +0530140
Vernon Mauery9e801a22018-10-12 13:20:49 -0700141 std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(), iter);
Tom Joseph8bb10b72016-12-06 17:47:56 +0530142 std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN);
143
144 // Generate the Managed System Random Number
145 if (!RAND_bytes(input.data() + sizeof(rcSessionID) + sizeof(bmcSessionID) +
Vernon Mauery9e801a22018-10-12 13:20:49 -0700146 cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN,
Tom Joseph8bb10b72016-12-06 17:47:56 +0530147 cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN))
148 {
149 response->rmcpStatusCode =
150 static_cast<uint8_t>(RAKP_ReturnCode::INSUFFICIENT_RESOURCE);
151 return outPayload;
152 }
Richard Marian Thomaiyard5a4f452019-01-16 12:15:44 +0530153 // As stated in Set Session Privilege Level command in IPMI Spec, when
jayaprakash Mutyala2555e2e2019-12-24 22:51:46 +0000154 // creating a session through Activate command / RAKP 1 message, it must
155 // be established with USER privilege as well as all other sessions are
156 // initially set to USER privilege, regardless of the requested maximum
157 // privilege.
158 if (!(static_cast<session::Privilege>(request->req_max_privilege_level &
159 session::reqMaxPrivMask) >
160 session::Privilege::CALLBACK))
Richard Marian Thomaiyard5a4f452019-01-16 12:15:44 +0530161 {
jayaprakash Mutyala2555e2e2019-12-24 22:51:46 +0000162 response->rmcpStatusCode =
163 static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_ROLE_PRIV);
164 return outPayload;
Richard Marian Thomaiyard5a4f452019-01-16 12:15:44 +0530165 }
jayaprakash Mutyala2555e2e2019-12-24 22:51:46 +0000166 session->currentPrivilege(static_cast<uint8_t>(session::Privilege::USER));
167
Tom Joseph4021b1f2019-02-12 10:10:12 +0530168 session->reqMaxPrivLevel =
169 static_cast<session::Privilege>(request->req_max_privilege_level);
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530170 if (request->user_name_len == 0)
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +0530171 {
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530172 // Bail out, if user name is not specified.
173 // Yes, NULL user name is not supported for security reasons.
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530174 response->rmcpStatusCode =
175 static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
sunitakx0e0546f2021-06-15 08:57:10 +0000176 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530177 return outPayload;
Richard Marian Thomaiyard2563c52018-11-29 11:49:10 +0530178 }
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530179
180 // Perform user name based lookup
181 std::string userName(request->user_name, request->user_name_len);
Jayaprakash Mutyala05c14472020-10-19 10:26:24 +0000182 ipmi::SecureString passwd;
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530183 uint8_t userId = ipmi::ipmiUserGetUserId(userName);
184 if (userId == ipmi::invalidUserId)
185 {
186 response->rmcpStatusCode =
187 static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
sunitakx0e0546f2021-06-15 08:57:10 +0000188 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530189 return outPayload;
190 }
191 // check user is enabled before proceeding.
192 bool userEnabled = false;
193 ipmi::ipmiUserCheckEnabled(userId, userEnabled);
194 if (!userEnabled)
195 {
196 response->rmcpStatusCode =
197 static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE);
sunitakx0e0546f2021-06-15 08:57:10 +0000198 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530199 return outPayload;
200 }
Richard Marian Thomaiyar4c4694e2019-06-20 16:36:49 +0530201 // Get the user password for RAKP message authenticate
202 passwd = ipmi::ipmiUserGetPassword(userName);
203 if (passwd.empty())
204 {
205 response->rmcpStatusCode =
206 static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
sunitakx0e0546f2021-06-15 08:57:10 +0000207 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyar4c4694e2019-06-20 16:36:49 +0530208 return outPayload;
209 }
Ayushi Smritib31e9692019-05-15 12:06:51 +0000210 // Check whether user is already locked for failed attempts
211 if (!ipmi::ipmiUserPamAuthenticate(userName, passwd))
212 {
213 log<level::ERR>("Authentication failed - user already locked out",
214 entry("USER-ID=%d", static_cast<uint8_t>(userId)));
215
216 response->rmcpStatusCode =
217 static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
sunitakx0e0546f2021-06-15 08:57:10 +0000218 logInvalidLoginRedfishEvent(message);
Ayushi Smritib31e9692019-05-15 12:06:51 +0000219 return outPayload;
220 }
Saravanan Palanisamyd9c86bb2019-08-07 17:57:37 +0000221
222 uint8_t chNum = static_cast<uint8_t>(getInterfaceIndex());
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530223 // Get channel based access information
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530224 if ((ipmi::ipmiUserGetPrivilegeAccess(
225 userId, chNum, session->sessionUserPrivAccess) != IPMI_CC_OK) ||
226 (ipmi::getChannelAccessData(chNum, session->sessionChannelAccess) !=
227 IPMI_CC_OK))
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530228 {
229 response->rmcpStatusCode =
230 static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE);
sunitakx0e0546f2021-06-15 08:57:10 +0000231 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530232 return outPayload;
233 }
AppaRao Puliecb32fb2020-07-01 22:55:38 +0530234 if (!isChannelAccessModeEnabled(session->sessionChannelAccess.accessMode))
235 {
236 phosphor::logging::log<phosphor::logging::level::ERR>(
237 "Channel access mode disabled.");
238 response->rmcpStatusCode =
239 static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE);
sunitakx0e0546f2021-06-15 08:57:10 +0000240 logInvalidLoginRedfishEvent(message);
AppaRao Puliecb32fb2020-07-01 22:55:38 +0530241 return outPayload;
242 }
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530243 if (session->sessionUserPrivAccess.privilege >
244 static_cast<uint8_t>(session::Privilege::OEM))
Richard Marian Thomaiyar7e5d38d2019-03-02 22:11:41 +0530245 {
246 response->rmcpStatusCode =
247 static_cast<uint8_t>(RAKP_ReturnCode::INACTIVE_ROLE);
sunitakx0e0546f2021-06-15 08:57:10 +0000248 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyar7e5d38d2019-03-02 22:11:41 +0530249 return outPayload;
250 }
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530251 session->channelNum(chNum);
252 session->userID(userId);
jayaprakash Mutyala2555e2e2019-12-24 22:51:46 +0000253 // minimum privilege of Channel / User / session::privilege::USER
Richard Marian Thomaiyard5a4f452019-01-16 12:15:44 +0530254 // has to be used as session current privilege level
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530255 uint8_t minPriv = 0;
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530256 if (session->sessionChannelAccess.privLimit <
257 session->sessionUserPrivAccess.privilege)
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530258 {
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530259 minPriv = session->sessionChannelAccess.privLimit;
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530260 }
261 else
262 {
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530263 minPriv = session->sessionUserPrivAccess.privilege;
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530264 }
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530265 if (session->currentPrivilege() > minPriv)
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530266 {
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530267 session->currentPrivilege(static_cast<uint8_t>(minPriv));
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530268 }
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530269 // For username / privilege lookup, fail with UNAUTH_NAME, if requested
Richard Marian Thomaiyard8e92fe2019-01-16 11:56:23 +0530270 // max privilege does not match user privilege
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530271 if (((request->req_max_privilege_level & userNameOnlyLookupMask) ==
272 userNamePrivLookup) &&
Richard Marian Thomaiyard8e92fe2019-01-16 11:56:23 +0530273 ((request->req_max_privilege_level & session::reqMaxPrivMask) !=
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +0530274 session->sessionUserPrivAccess.privilege))
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530275 {
Vernon Maueryfc37e592018-12-19 14:55:15 -0800276 log<level::INFO>(
277 "Username/Privilege lookup failed for requested privilege");
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530278 response->rmcpStatusCode =
279 static_cast<uint8_t>(RAKP_ReturnCode::UNAUTH_NAME);
sunitakx0e0546f2021-06-15 08:57:10 +0000280
281 logInvalidLoginRedfishEvent(message);
Richard Marian Thomaiyard91fd9d2018-12-06 12:03:50 +0530282 return outPayload;
283 }
Richard Marian Thomaiyar99b87842018-12-06 21:35:43 +0530284
285 std::fill(authAlgo->userKey.data(),
286 authAlgo->userKey.data() + authAlgo->userKey.size(), 0);
287 std::copy_n(passwd.c_str(), passwd.size(), authAlgo->userKey.data());
288
Tom Joseph8bb10b72016-12-06 17:47:56 +0530289 // Copy the Managed System Random Number to the Authentication Algorithm
290 std::copy_n(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN,
291 authAlgo->bmcRandomNum.begin());
292 std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN);
293
294 // Managed System GUID
Tom Joseph83029cb2017-09-01 16:37:31 +0530295 std::copy_n(cache::guid.data(), cache::guid.size(), iter);
Tom Joseph8bb10b72016-12-06 17:47:56 +0530296 std::advance(iter, BMC_GUID_LEN);
297
298 // Requested Privilege Level
Tom Joseph8bb10b72016-12-06 17:47:56 +0530299 std::copy_n(&(request->req_max_privilege_level),
300 sizeof(request->req_max_privilege_level), iter);
301 std::advance(iter, sizeof(request->req_max_privilege_level));
302
Tom Joseph8bb10b72016-12-06 17:47:56 +0530303 // User Name Length Byte
304 std::copy_n(&(request->user_name_len), sizeof(request->user_name_len),
305 iter);
Tom Joseph56527b92018-03-21 19:31:58 +0530306 std::advance(iter, sizeof(request->user_name_len));
307
308 std::copy_n(session->userName.data(), session->userName.size(), iter);
Tom Joseph8bb10b72016-12-06 17:47:56 +0530309
310 // Generate Key Exchange Authentication Code - RAKP2
311 auto output = authAlgo->generateHMAC(input);
312
313 response->messageTag = request->messageTag;
314 response->rmcpStatusCode = static_cast<uint8_t>(RAKP_ReturnCode::NO_ERROR);
315 response->reserved = 0;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700316 response->remoteConsoleSessionID = rcSessionID;
Tom Joseph8bb10b72016-12-06 17:47:56 +0530317
318 // Copy Managed System Random Number to the Response
319 std::copy(authAlgo->bmcRandomNum.begin(), authAlgo->bmcRandomNum.end(),
320 response->managed_system_random_number);
321
322 // Copy System GUID to the Response
Vernon Mauery9e801a22018-10-12 13:20:49 -0700323 std::copy_n(cache::guid.data(), cache::guid.size(),
Tom Joseph83029cb2017-09-01 16:37:31 +0530324 response->managed_system_guid);
Tom Joseph8bb10b72016-12-06 17:47:56 +0530325
326 // Insert the HMAC output into the payload
327 outPayload.insert(outPayload.end(), output.begin(), output.end());
Tom Joseph8bb10b72016-12-06 17:47:56 +0530328 return outPayload;
329}
330
331} // namespace command