blob: 24deac0921bd8cb63a44f8cf4b1c4ea048631a46 [file] [log] [blame]
Tom Joseph50fb50a2016-12-06 17:51:06 +05301#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 Mauery9b307be2017-11-22 09:28:16 -080011#include "rmcp.hpp"
Tom Joseph50fb50a2016-12-06 17:51:06 +053012
13namespace command
14{
15
Tom Josephef02fb32017-01-19 12:55:11 +053016void 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 Mauery7e9e2ef2017-11-29 08:36:29 -080032 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 Josephef02fb32017-01-19 12:55:11 +053039 default:
40 break;
41 }
42}
43
Tom Joseph4c766eb2017-01-24 18:24:57 +053044void 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 Mauery9b307be2017-11-22 09:28:16 -080055 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 Joseph4c766eb2017-01-24 18:24:57 +053060 break;
61 }
62 default:
63 break;
64 }
65}
66
Tom Joseph18a45e92017-04-11 11:30:44 +053067std::vector<uint8_t> RAKP34(const std::vector<uint8_t>& inPayload,
Tom Joseph50fb50a2016-12-06 17:51:06 +053068 const message::Handler& handler)
69{
70 std::cout << ">> RAKP34\n";
71
72 std::vector<uint8_t> outPayload(sizeof(RAKP4response));
Tom Joseph18a45e92017-04-11 11:30:44 +053073 auto request = reinterpret_cast<const RAKP3request*>(inPayload.data());
Tom Joseph50fb50a2016-12-06 17:51:06 +053074 auto response = reinterpret_cast<RAKP4response*>(outPayload.data());
75
76 // Check if the RAKP3 Payload Length is as expected
Vernon Mauery9b307be2017-11-22 09:28:16 -080077 if (inPayload.size() < sizeof(RAKP3request))
Tom Joseph50fb50a2016-12-06 17:51:06 +053078 {
79 std::cerr << "RAKP34: Invalid RAKP3 request\n";
80 response->rmcpStatusCode =
81 static_cast<uint8_t>(RAKP_ReturnCode::INVALID_INTEGRITY_VALUE);
82 return outPayload;
83 }
84
85 // Session ID zero is reserved for Session Setup
86 if(endian::from_ipmi(request->managedSystemSessionID) ==
87 session::SESSION_ZERO)
88 {
89 std::cerr << "RAKP34: BMC invalid Session ID\n";
90 response->rmcpStatusCode =
91 static_cast<uint8_t>(RAKP_ReturnCode::INVALID_SESSION_ID);
92 return outPayload;
93 }
94
95 std::shared_ptr<session::Session> session;
96 try
97 {
98 session = (std::get<session::Manager&>(singletonPool).getSession(
99 endian::from_ipmi(request->managedSystemSessionID))).lock();
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
129 uint8_t userLength = 0;
130
131 std::vector<uint8_t> input;
132 input.resize(cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN +
133 sizeof(rcSessionID) + sizeof(sessPrivLevel) +
134 sizeof(userLength));
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);
155
156 // Generate Key Exchange Authentication Code - RAKP2
157 auto output = authAlgo->generateHMAC(input);
158
Vernon Mauery9b307be2017-11-22 09:28:16 -0800159 if (inPayload.size() != (sizeof(RAKP3request) + output.size()) ||
160 std::memcmp(output.data(), request+1, output.size()))
Tom Joseph50fb50a2016-12-06 17:51:06 +0530161 {
162 std::cerr << "Mismatch in HMAC sent by remote console\n";
163
164 response->messageTag = request->messageTag;
165 response->rmcpStatusCode = static_cast<uint8_t>
166 (RAKP_ReturnCode::INVALID_INTEGRITY_VALUE);
167 response->reserved = 0;
168 response->remoteConsoleSessionID = rcSessionID;
169
170 //close the session
171 std::get<session::Manager&>(singletonPool).stopSession(
172 session->getBMCSessionID());
173
174 return outPayload;
175 }
176
177 /*
178 * Session Integrity Key
179 *
180 * 1) Remote Console Random Number - 16 bytes
181 * 2) Managed System Random Number - 16 bytes
182 * 3) Session Privilege Level - 1 byte
183 * 4) User Name Length Byte - 1 byte (0 for 'null' username)
184 * 5) User Name - variable (absent for 'null' username)
185 */
186
187 input.clear();
188
189 input.resize(cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN +
190 cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN +
191 sizeof(sessPrivLevel) + sizeof(userLength));
192 iter = input.begin();
193
194 // Remote Console Random Number
195 std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(),
196 iter);
197 std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN);
198
199 // Managed Console Random Number
200 std::copy(authAlgo->bmcRandomNum.begin(), authAlgo->bmcRandomNum.end(),
201 iter);
202 std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN);
203
204 // Session Privilege Level
205 std::copy_n(reinterpret_cast<uint8_t*>(&sessPrivLevel),
206 sizeof(sessPrivLevel), iter);
207 std::advance(iter, sizeof(sessPrivLevel));
208
209 // User Name Length Byte
210 std::copy_n(&userLength, sizeof(userLength), iter);
211
212 // Generate Session Integrity Key
213 auto sikOutput = authAlgo->generateHMAC(input);
214
215 // Update the SIK in the Authentication Algo Interface
216 authAlgo->sessionIntegrityKey.insert(authAlgo->sessionIntegrityKey.begin(),
217 sikOutput.begin(), sikOutput.end());
218
219 /*
220 * Integrity Check Value
221 *
222 * 1) Remote Console Random Number - 16 bytes
223 * 2) Managed System Session ID - 4 bytes
224 * 3) Managed System GUID - 16 bytes
225 */
226
227 // Get Managed System Session ID
228 auto bmcSessionID = endian::to_ipmi(session->getBMCSessionID());
229
230 input.clear();
231
232 input.resize(cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN +
233 sizeof(bmcSessionID) + BMC_GUID_LEN);
234 iter = input.begin();
235
236 // Remote Console Random Number
237 std::copy(authAlgo->rcRandomNum.begin(), authAlgo->rcRandomNum.end(),
238 iter);
239 std::advance(iter, cipher::rakp_auth::REMOTE_CONSOLE_RANDOM_NUMBER_LEN);
240
241 // Managed System Session ID
242 std::copy_n(reinterpret_cast<uint8_t*>(&bmcSessionID), sizeof(bmcSessionID),
243 iter);
244 std::advance(iter, sizeof(bmcSessionID));
245
246 // Managed System GUID
Tom Joseph83029cb2017-09-01 16:37:31 +0530247 std::copy_n(cache::guid.data(), cache::guid.size(), iter);
Tom Joseph50fb50a2016-12-06 17:51:06 +0530248
249 // Integrity Check Value
250 auto icv = authAlgo->generateICV(input);
251
252 outPayload.resize(sizeof(RAKP4response));
253
254 response->messageTag = request->messageTag;
255 response->rmcpStatusCode = static_cast<uint8_t>(RAKP_ReturnCode::NO_ERROR);
256 response->reserved = 0;
257 response->remoteConsoleSessionID = rcSessionID;
258
259 // Insert the HMAC output into the payload
260 outPayload.insert(outPayload.end(), icv.begin(), icv.end());
261
Tom Josephef02fb32017-01-19 12:55:11 +0530262 // Set the Integrity Algorithm
263 applyIntegrityAlgo(session->getBMCSessionID());
Tom Joseph818d0702017-01-10 16:39:38 +0530264
Tom Joseph4c766eb2017-01-24 18:24:57 +0530265 // Set the Confidentiality Algorithm
266 applyCryptAlgo(session->getBMCSessionID());
267
Tom Joseph50fb50a2016-12-06 17:51:06 +0530268 session->state = session::State::ACTIVE;
269
270 std::cout << "<< RAKP34\n";
271 return outPayload;
272}
273
274} // namespace command