Kea 3.1.1
client_message.cc
Go to the documentation of this file.
1// Copyright (C) 2023-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <client_attribute.h>
10#include <client_message.h>
11#include <radius_log.h>
14#include <boost/scoped_ptr.hpp>
15#include <sstream>
16
17using namespace isc;
18using namespace isc::asiolink;
19using namespace isc::cryptolink;
20using namespace isc::data;
21using namespace isc::util;
22using namespace std;
23
24namespace isc {
25namespace radius {
26
27string
28msgCodeToText(const uint8_t code) {
29 ostringstream result;
30 switch (code) {
32 return ("Access-Request");
34 return ("Access-Accept");
36 return ("Access-Reject");
38 return ("Accounting-Request");
40 return ("Accounting-Response");
42 return ("Accounting-Status");
44 return ("Password-Request");
45 case PW_PASSWORD_ACK:
46 return ("Password-Ack");
48 return ("Password-Reject");
50 return ("Accounting-Message");
52 return ("Access-Challenge");
54 return ("Status-Server");
56 return ("Status-Client");
57 default:
58 result << "Message-Code-" << static_cast<unsigned>(code);
59 return (result.str());
60 }
61}
62
63Message::Message(const uint8_t code, uint16_t length,
64 const vector<uint8_t>& auth, const string& secret,
65 const AttributesPtr& attributes)
66 : code_(code), length_(length), auth_(auth), secret_(secret),
67 attributes_(attributes), buffer_() {
68}
69
71 : code_(other.code_),
72 length_(other.length_),
73 auth_(other.auth_),
74 secret_(other.secret_),
76 buffer_(other.buffer_) {
77 if (!other.attributes_) {
78 attributes_.reset();
79 } else {
80 for (auto const& attr : *other.attributes_) {
81 attributes_->add(attr);
82 }
83 }
84}
85
86Message::Message(const vector<uint8_t>& buffer,
87 const vector<uint8_t>& auth,
88 const string& secret)
89 : code_(0), length_(0), auth_(auth), secret_(secret), attributes_(),
90 buffer_(buffer) {
91}
92
94 if (secret_.size() > 0) {
95 memset(&secret_[0], 0, secret_.size());
96 }
97 secret_.clear();
98}
99
100void
102 vector<uint8_t> r = cryptolink::random(1);
103 if (r.size() == 0) {
104 isc_throw(Unexpected, "random failed");
105 }
106 identifier_ = r[0];
107}
108
109void
110Message::setAuth(const vector<uint8_t>& auth) {
111 if (auth.size() != AUTH_VECTOR_LEN) {
112 isc_throw(BadValue, "authenticator must be 16 bytes long");
113 }
114 auth_ = auth;
115}
116
117void
119 auth_.clear();
120 auth_.resize(AUTH_VECTOR_LEN, 0);
121}
122
123void
125 auth_ = cryptolink::random(AUTH_VECTOR_LEN);
126 if (auth_.size() != AUTH_VECTOR_LEN) {
127 isc_throw(Unexpected, "random failed");
128 }
129}
130
131void
132Message::setSecret(const string& secret) {
133 if (secret.empty()) {
134 isc_throw(BadValue, "empty secret");
135 }
136 secret_ = secret;
137}
138
139vector<uint8_t>
141 if (secret_.empty()) {
142 isc_throw(InvalidOperation, "empty secret");
143 }
144
145 // Header.
146 buffer_.resize(AUTH_HDR_LEN);
147 buffer_[0] = code_;
148 buffer_[1] = identifier_;
149 buffer_[2] = static_cast<uint8_t>((length_ & 0xff00) >> 8);
150 buffer_[3] = static_cast<uint8_t>(length_ & 0xff);
151 memmove(&buffer_[4], &auth_[0], auth_.size());
152
153 // Fill attributes.
154 if (attributes_) {
155 for (auto attr : *attributes_) {
156 if (!attr) {
157 continue;
158 }
159 if ((code_ == PW_ACCESS_REQUEST) &&
160 (attr->getType() == PW_USER_PASSWORD)) {
161 attr = encodeUserPassword(attr);
162 }
163 vector<uint8_t> binary = attr->toBytes();
164 if (binary.empty()) {
165 continue;
166 }
167 if (buffer_.size() + binary.size() > PW_MAX_MSG_SIZE) {
168 isc_throw(BadValue, "message becomes too large");
169 }
170 buffer_.insert(buffer_.end(), binary.cbegin(), binary.cend());
171 }
172 }
173
174 // Finish.
175 length_ = static_cast<uint16_t>(buffer_.size());
176 buffer_[2] = static_cast<uint8_t>((length_ & 0xff00) >> 8);
177 buffer_[3] = static_cast<uint8_t>(length_ & 0xff);
178
179 if (code_ != PW_ACCESS_REQUEST) {
180 boost::scoped_ptr<Hash> md(CryptoLink::getCryptoLink().createHash(MD5));
181 md->update(&buffer_[0], buffer_.size());
182 md->update(&secret_[0], secret_.size());
183 md->final(&auth_[0], AUTH_VECTOR_LEN);
184 memmove(&buffer_[4], &auth_[0], auth_.size());
185 }
186
188 .arg(msgCodeToText(code_))
189 .arg(static_cast<unsigned>(code_))
190 .arg(static_cast<unsigned>(identifier_))
191 .arg(length_)
192 .arg(attributes_ ? attributes_->size() : 0);
193 return (buffer_);
194}
195
196void
198 if (secret_.empty()) {
199 isc_throw(InvalidOperation, "empty secret");
200 }
201
202 // Length.
203 if (buffer_.size() < AUTH_HDR_LEN) {
204 isc_throw(BadValue, "message is too short " << buffer_.size()
205 << " < " << AUTH_HDR_LEN);
206 }
207 code_ = buffer_[0];
208 identifier_ = buffer_[1];
209 length_ = static_cast<uint16_t>(buffer_[2]) << 8;
210 length_ |= static_cast<uint16_t>(buffer_[3]);
211 if (code_ == PW_ACCESS_REQUEST) {
212 auth_.resize(AUTH_VECTOR_LEN);
213 memmove(&auth_[0], &buffer_[4], AUTH_VECTOR_LEN);
214 } else if (auth_.size() != AUTH_VECTOR_LEN) {
215 isc_throw(InvalidOperation, "bad authenticator");
216 }
217 if (length_ > buffer_.size()) {
218 isc_throw(BadValue, "truncated " << msgCodeToText(code_)
219 << " length " << length_ << ", got " << buffer_.size());
220 }
221 if (length_ < AUTH_HDR_LEN) {
222 isc_throw(BadValue, "too short " << msgCodeToText(code_)
223 << " length " << length_ << " < " << AUTH_HDR_LEN);
224 }
225 if (length_ > PW_MAX_MSG_SIZE) {
226 isc_throw(BadValue, "too large " << msgCodeToText(code_)
227 << " length " << length_ << " > " << PW_MAX_MSG_SIZE);
228 }
229 if (length_ < buffer_.size()) {
230 buffer_.resize(length_);
231 }
232
233 // Verify authentication.
234 if (code_ != PW_ACCESS_REQUEST) {
235 vector<uint8_t> work = buffer_;
236 memmove(&work[4], &auth_[0], auth_.size());
237 boost::scoped_ptr<Hash> md(CryptoLink::getCryptoLink().createHash(MD5));
238 md->update(&work[0], work.size());
239 md->update(&secret_[0], secret_.size());
240 vector<uint8_t> digest;
241 digest.resize(AUTH_VECTOR_LEN);
242 md->final(&digest[0], AUTH_VECTOR_LEN);
243 if (memcmp(&digest[0], &buffer_[4], AUTH_VECTOR_LEN) != 0) {
244 isc_throw(BadValue, "authentication for " << msgCodeToText(code_)
245 << " failed");
246 }
247 }
248 auth_.resize(AUTH_VECTOR_LEN);
249 memmove(&auth_[0], &buffer_[4], auth_.size());
250
251 // Get attributes.
252 attributes_.reset(new Attributes());
253 size_t ptr = AUTH_HDR_LEN;
254 for (;;) {
255 if (ptr == length_) {
256 break;
257 }
258 if (ptr + 2 > length_) {
259 isc_throw(BadValue, "trailing octet");
260 }
261 const uint8_t type = buffer_[ptr];
262 const uint8_t len = buffer_[ptr + 1];
263 if (ptr + len > length_) {
264 isc_throw(BadValue, "trailing truncated "
265 << AttrDefs::instance().getName(type) << " ("
266 << static_cast<unsigned>(type) << "): length "
267 << static_cast<unsigned>(len) << ", space "
268 << (length_ - ptr));
269 }
270 if (len < 3) {
271 isc_throw(BadValue, "too small attribute length "
272 << static_cast<unsigned>(len) << " < 3");
273 }
274 vector<uint8_t> binary;
275 binary.resize(len);
276 memmove(&binary[0], &buffer_[ptr], binary.size());
278 if ((code_ == PW_ACCESS_REQUEST) && attr &&
279 (attr->getType() == PW_USER_PASSWORD)) {
280 attr = decodeUserPassword(attr);
281 }
282 attributes_->add(attr);
283 ptr += len;
284 }
285 if (attributes_->empty()) {
286 attributes_.reset();
287 }
288
290 .arg(msgCodeToText(code_))
291 .arg(static_cast<unsigned>(code_))
292 .arg(static_cast<unsigned>(identifier_))
293 .arg(length_)
294 .arg(attributes_ ? attributes_->size() : 0);
295}
296
299 if (!attr || (attr->getValueType() != PW_TYPE_STRING) ||
300 (attr->getValueLen() == 0) ||
301 (auth_.size() != AUTH_VECTOR_LEN)) {
302 isc_throw(Unexpected, "Can't encode User-Password");
303 }
304
305 // Get padded password.
306 vector<uint8_t> password = attr->toBinary();
307 size_t len = password.size();
308 len = (len + AUTH_VECTOR_LEN - 1) & ~(AUTH_VECTOR_LEN - 1);
309 if (len > AUTH_PASS_LEN) {
310 len = AUTH_PASS_LEN;
311 }
312 password.resize(len);
313
314 // Hide password.
315 for (size_t i = 0; i < len; i += AUTH_VECTOR_LEN) {
316 boost::scoped_ptr<Hash> md(CryptoLink::getCryptoLink().createHash(MD5));
317 md->update(&secret_[0], secret_.size());
318
319 uint8_t* to_hash;
320 if (i == 0) {
321 to_hash = &auth_[0];
322 } else {
323 to_hash = &password[i - AUTH_VECTOR_LEN];
324 }
325 md->update(to_hash, AUTH_VECTOR_LEN);
326
327 vector<uint8_t> digest;
328 digest.resize(AUTH_VECTOR_LEN);
329 md->final(&digest[0], AUTH_VECTOR_LEN);
330 for (size_t j = 0; j < AUTH_VECTOR_LEN; j++) {
331 password[i + j] ^= digest[j];
332 }
333 memset(&digest[0], 0, AUTH_VECTOR_LEN);
334 }
335
336 // Return hidden attribute.
337 return (Attribute::fromBinary(PW_USER_PASSWORD, password));
338}
339
342 if (!attr || (attr->getValueType() != PW_TYPE_STRING) ||
343 (attr->getValueLen() == 0) ||
344 ((attr->getValueLen() % AUTH_VECTOR_LEN) != 0) ||
345 (auth_.size() != AUTH_VECTOR_LEN)) {
346 isc_throw(Unexpected, "can't decode User-Password");
347 }
348
349 // Get hidden password.
350 vector<uint8_t> password = attr->toBinary();
351 size_t len = password.size();
352 if (len > AUTH_PASS_LEN) {
353 len = AUTH_PASS_LEN;
354 password.resize(len);
355 }
356
357 // Get plain text password.
358 size_t i = len;
359 for (;;) {
360 if (i < AUTH_VECTOR_LEN) {
361 break;
362 }
363 i -= AUTH_VECTOR_LEN;
364 boost::scoped_ptr<Hash> md(CryptoLink::getCryptoLink().createHash(MD5));
365 md->update(&secret_[0], secret_.size());
366
367 uint8_t* to_hash;
368 if (i == 0) {
369 to_hash = &auth_[0];
370 } else {
371 to_hash = &password[i - AUTH_VECTOR_LEN];
372 }
373 md->update(to_hash, AUTH_VECTOR_LEN);
374
375 vector<uint8_t> digest;
376 digest.resize(AUTH_VECTOR_LEN);
377 md->final(&digest[0], AUTH_VECTOR_LEN);
378 for (size_t j = 0; j < AUTH_VECTOR_LEN; j++) {
379 password[i + j] ^= digest[j];
380 }
381 memset(&digest[0], 0, AUTH_VECTOR_LEN);
382 }
383
384 // Unpad password (requires no trailing nuls).
385 while (password.back() == 0) {
386 // Never leave an empty password.
387 if (password.size() == 1) {
388 break;
389 }
390 password.pop_back();
391 }
392
393 // Return plain text attribute.
394 return (Attribute::fromBinary(PW_USER_PASSWORD, password));
395}
396
397} // end of namespace isc::radius
398} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
A generic exception that is thrown when an unexpected error condition occurs.
static AttrDefs & instance()
Returns a single instance.
static AttributePtr fromBytes(const std::vector< uint8_t > &bytes)
From bytes (wire format).
static AttributePtr fromBinary(const uint8_t type, const std::vector< uint8_t > &value)
From binary with type.
Collection of attributes.
std::vector< uint8_t > auth_
Authenticator: header[4] (16 octets).
void setAuth(const std::vector< uint8_t > &auth)
Set authenticator.
ConstAttributePtr encodeUserPassword(const ConstAttributePtr &attr)
Encode User-Password in an Access-Request.
ConstAttributePtr decodeUserPassword(const ConstAttributePtr &attr)
Decode User-Password in an Access-Request.
Message(const uint8_t code, uint16_t length, const std::vector< uint8_t > &auth, const std::string &secret, const AttributesPtr &attributes)
Constructor.
uint8_t identifier_
Identifier (random): header[1].
void randomAuth()
Randomize authenticator.
std::vector< uint8_t > encode()
Encode a message.
std::vector< uint8_t > buffer_
Buffer (message content).
uint8_t code_
Code (useful values in MsgCode): header[0].
void decode()
Decode a message.
AttributesPtr attributes_
Attributes: header[20]...
uint16_t length_
Length: header[2] (16 bits, network order).
std::string secret_
Secret (not empty).
void zeroAuth()
Fill authenticator with 0.
virtual ~Message()
Destructor.
void setSecret(const std::string &secret)
Set secret.
void randomIdentifier()
Randomize identifier.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
boost::shared_ptr< Attributes > AttributesPtr
Shared pointers to attribute collection.
boost::shared_ptr< const Attribute > ConstAttributePtr
const isc::log::MessageID RADIUS_DECODE_MESSAGE
string msgCodeToText(const uint8_t code)
MsgCode value -> name function.
const int RADIUS_DBG_TRACE
Radius logging levels.
Definition radius_log.h:26
const isc::log::MessageID RADIUS_ENCODE_MESSAGE
isc::log::Logger radius_logger("radius-hooks")
Radius Logger.
Definition radius_log.h:35
Defines the logger used by the top-level component of kea-lfc.