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