15#include <boost/scoped_ptr.hpp>
34 return (
"Access-Request");
36 return (
"Access-Accept");
38 return (
"Access-Reject");
40 return (
"Accounting-Request");
42 return (
"Accounting-Response");
44 return (
"Accounting-Status");
46 return (
"Password-Request");
48 return (
"Password-Ack");
50 return (
"Password-Reject");
52 return (
"Accounting-Message");
54 return (
"Access-Challenge");
56 return (
"Status-Server");
58 return (
"Status-Client");
60 result <<
"Message-Code-" <<
static_cast<unsigned>(code);
61 return (result.str());
66 const vector<uint8_t>& auth,
const string& secret,
83 for (auto const& attr : *other.attributes_) {
84 attributes_->add(attr);
90 const vector<uint8_t>& auth,
114 if (auth.size() != AUTH_VECTOR_LEN) {
123 auth_.resize(AUTH_VECTOR_LEN, 0);
129 if (
auth_.size() != AUTH_VECTOR_LEN) {
136 if (secret.empty()) {
157 size_t msg_auth_ptr = 0;
168 if (msg_auth_ptr != 0) {
172 (attr->getValueLen() != AUTH_VECTOR_LEN)) {
177 vector<uint8_t> binary = attr->toBytes();
178 if (binary.empty()) {
181 if (
buffer_.size() + binary.size() > PW_MAX_MSG_SIZE) {
194 if (msg_auth_ptr != 0) {
203 md->final(&
auth_[0], AUTH_VECTOR_LEN);
208 .arg(
static_cast<unsigned>(
code_))
222 if (
buffer_.size() < AUTH_HDR_LEN) {
224 <<
" < " << AUTH_HDR_LEN);
231 auth_.resize(AUTH_VECTOR_LEN);
233 }
else if (
auth_.size() != AUTH_VECTOR_LEN) {
242 <<
" length " <<
length_ <<
" < " << AUTH_HDR_LEN);
244 if (
length_ > PW_MAX_MSG_SIZE) {
246 <<
" length " <<
length_ <<
" > " << PW_MAX_MSG_SIZE);
254 vector<uint8_t> work =
buffer_;
257 md->update(&work[0], work.size());
260 digest.resize(AUTH_VECTOR_LEN);
261 md->final(&
digest[0], AUTH_VECTOR_LEN);
268 auth_.resize(AUTH_VECTOR_LEN);
274 size_t ptr = AUTH_HDR_LEN;
275 size_t msg_auth_ptr = 0;
283 const uint8_t type =
buffer_[ptr];
284 const uint8_t len =
buffer_[ptr + 1];
288 <<
static_cast<unsigned>(type) <<
"): length "
289 <<
static_cast<unsigned>(len) <<
", space "
294 <<
static_cast<unsigned>(len) <<
" < 3");
296 vector<uint8_t> binary;
298 memmove(&binary[0], &
buffer_[ptr], binary.size());
305 if (msg_auth_ptr != 0) {
313 if (msg_auth_ptr != 0) {
322 .arg(
static_cast<unsigned>(
code_))
331 (attr->getValueLen() == 0) ||
332 (
auth_.size() != AUTH_VECTOR_LEN)) {
337 vector<uint8_t> password = attr->toBinary();
338 size_t len = password.size();
339 len = (len + AUTH_VECTOR_LEN - 1) & ~(AUTH_VECTOR_LEN - 1);
340 if (len > AUTH_PASS_LEN) {
343 password.resize(len);
346 for (
size_t i = 0; i < len; i += AUTH_VECTOR_LEN) {
354 to_hash = &password[i - AUTH_VECTOR_LEN];
356 md->update(to_hash, AUTH_VECTOR_LEN);
359 digest.resize(AUTH_VECTOR_LEN);
360 md->final(&
digest[0], AUTH_VECTOR_LEN);
361 for (
size_t j = 0; j < AUTH_VECTOR_LEN; j++) {
362 password[i + j] ^=
digest[j];
364 memset(&
digest[0], 0, AUTH_VECTOR_LEN);
374 (attr->getValueLen() == 0) ||
375 ((attr->getValueLen() % AUTH_VECTOR_LEN) != 0) ||
376 (
auth_.size() != AUTH_VECTOR_LEN)) {
381 vector<uint8_t> password = attr->toBinary();
382 size_t len = password.size();
383 if (len > AUTH_PASS_LEN) {
385 password.resize(len);
391 if (i < AUTH_VECTOR_LEN) {
394 i -= AUTH_VECTOR_LEN;
402 to_hash = &password[i - AUTH_VECTOR_LEN];
404 md->update(to_hash, AUTH_VECTOR_LEN);
407 digest.resize(AUTH_VECTOR_LEN);
408 md->final(&
digest[0], AUTH_VECTOR_LEN);
409 for (
size_t j = 0; j < AUTH_VECTOR_LEN; j++) {
410 password[i + j] ^=
digest[j];
412 memset(&
digest[0], 0, AUTH_VECTOR_LEN);
416 while (password.back() == 0) {
418 if (password.size() == 1) {
430 if ((ptr < AUTH_HDR_LEN) || (ptr >
buffer_.size() - 2 - AUTH_VECTOR_LEN) ||
431 (
buffer_[ptr + 1] != 2 + AUTH_VECTOR_LEN) ||
432 (
auth_.size() != AUTH_VECTOR_LEN)) {
436 boost::scoped_ptr<HMAC> hmac(
440 std::vector<uint8_t> to_sign =
buffer_;
441 memmove(&to_sign[4], &
auth_[0],
auth_.size());
442 memset(&to_sign[ptr + 2], 0, AUTH_VECTOR_LEN);
443 hmac->update(&to_sign[0], to_sign.size());
444 vector<uint8_t> sign = hmac->sign(AUTH_VECTOR_LEN);
445 memmove(&
buffer_[ptr + 2], &sign[0], sign.size());
450 if ((ptr < AUTH_HDR_LEN) || (ptr >
buffer_.size() - 2 - AUTH_VECTOR_LEN) ||
451 (
buffer_[ptr + 1] != 2 + AUTH_VECTOR_LEN) ||
452 (
auth_.size() != AUTH_VECTOR_LEN)) {
456 vector<uint8_t> sign;
457 sign.resize(AUTH_VECTOR_LEN);
458 memmove(&sign[0], &
buffer_[ptr + 2], sign.size());
460 boost::scoped_ptr<HMAC> hmac(
464 std::vector<uint8_t> to_verify =
buffer_;
465 memmove(&to_verify[4], &
auth_[0],
auth_.size());
466 memset(&to_verify[ptr + 2], 0, AUTH_VECTOR_LEN);
468 hmac->update(&to_verify[0], to_verify.size());
469 if (!hmac->verify(&sign[0], sign.size())) {
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 CryptoLink & getCryptoLink()
Returns a reference to the singleton instance.
static AttrDefs & instance()
Returns a single instance.
static AttributePtr fromBytes(const std::vector< uint8_t > &bytes)
Generic factories.
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.
void signMessageAuthenticator(size_t ptr)
Encode Message-Authenticator in an Status-Server.
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 verifyMessageAuthenticator(size_t ptr)
Decode Message-Authenticator in an Status-Server.
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.
std::vector< uint8_t > random(size_t len)
Generate random value.
void digest(const void *data, const size_t data_len, const HashAlgorithm hash_algorithm, isc::util::OutputBuffer &result, size_t len)
Create an Hash digest for the given data.
@ PW_USER_PASSWORD
string.
@ PW_MESSAGE_AUTHENTICATOR
string.
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.
const isc::log::MessageID RADIUS_ENCODE_MESSAGE
isc::log::Logger radius_logger("radius-hooks")
Radius Logger.
Defines the logger used by the top-level component of kea-lfc.