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()) {
147 if (
auth_.size() != AUTH_VECTOR_LEN) {
160 size_t msg_auth_ptr = 0;
171 if (msg_auth_ptr != 0) {
175 (attr->getValueLen() != AUTH_VECTOR_LEN)) {
180 vector<uint8_t> binary = attr->toBytes();
181 if (binary.empty()) {
184 if (
buffer_.size() + binary.size() > PW_MAX_MSG_SIZE) {
197 if (msg_auth_ptr != 0) {
206 md->final(&
auth_[0], AUTH_VECTOR_LEN);
211 .arg(
static_cast<unsigned>(
code_))
225 if (
buffer_.size() < AUTH_HDR_LEN) {
227 <<
" < " << AUTH_HDR_LEN);
234 auth_.resize(AUTH_VECTOR_LEN);
236 }
else if (
auth_.size() != AUTH_VECTOR_LEN) {
246 <<
" length " <<
length_ <<
" < " << AUTH_HDR_LEN);
248 if (
length_ > PW_MAX_MSG_SIZE) {
250 <<
" length " <<
length_ <<
" > " << PW_MAX_MSG_SIZE);
258 vector<uint8_t> work =
buffer_;
261 md->update(&work[0], work.size());
264 digest.resize(AUTH_VECTOR_LEN);
265 md->final(&
digest[0], AUTH_VECTOR_LEN);
272 auth_.resize(AUTH_VECTOR_LEN);
278 size_t ptr = AUTH_HDR_LEN;
279 size_t msg_auth_ptr = 0;
287 const uint8_t type =
buffer_[ptr];
288 const uint8_t len =
buffer_[ptr + 1];
292 <<
static_cast<unsigned>(type) <<
"): length "
293 <<
static_cast<unsigned>(len) <<
", space "
298 <<
static_cast<unsigned>(len) <<
" < 3");
300 vector<uint8_t> binary;
302 memmove(&binary[0], &
buffer_[ptr], binary.size());
309 if (msg_auth_ptr != 0) {
317 if (msg_auth_ptr != 0) {
326 .arg(
static_cast<unsigned>(
code_))
335 (attr->getValueLen() == 0) ||
336 (
auth_.size() != AUTH_VECTOR_LEN) ||
342 vector<uint8_t> password = attr->toBinary();
343 size_t len = password.size();
344 len = (len + AUTH_VECTOR_LEN - 1) & ~(AUTH_VECTOR_LEN - 1);
345 if (len > AUTH_PASS_LEN) {
348 password.resize(len);
351 for (
size_t i = 0; i < len; i += AUTH_VECTOR_LEN) {
359 to_hash = &password[i - AUTH_VECTOR_LEN];
361 md->update(to_hash, AUTH_VECTOR_LEN);
364 digest.resize(AUTH_VECTOR_LEN);
365 md->final(&
digest[0], AUTH_VECTOR_LEN);
366 for (
size_t j = 0; j < AUTH_VECTOR_LEN; j++) {
367 password[i + j] ^=
digest[j];
369 memset(&
digest[0], 0, AUTH_VECTOR_LEN);
379 (attr->getValueLen() == 0) ||
380 ((attr->getValueLen() % AUTH_VECTOR_LEN) != 0) ||
381 (
auth_.size() != AUTH_VECTOR_LEN) ||
387 vector<uint8_t> password = attr->toBinary();
388 size_t len = password.size();
389 if (len > AUTH_PASS_LEN) {
391 password.resize(len);
397 if (i < AUTH_VECTOR_LEN) {
400 i -= AUTH_VECTOR_LEN;
408 to_hash = &password[i - AUTH_VECTOR_LEN];
410 md->update(to_hash, AUTH_VECTOR_LEN);
413 digest.resize(AUTH_VECTOR_LEN);
414 md->final(&
digest[0], AUTH_VECTOR_LEN);
415 for (
size_t j = 0; j < AUTH_VECTOR_LEN; j++) {
416 password[i + j] ^=
digest[j];
418 memset(&
digest[0], 0, AUTH_VECTOR_LEN);
422 while (password.back() == 0) {
424 if (password.size() == 1) {
436 if ((ptr < AUTH_HDR_LEN) || (ptr >
buffer_.size() - 2 - AUTH_VECTOR_LEN) ||
437 (
buffer_[ptr + 1] != 2 + AUTH_VECTOR_LEN) ||
438 (
auth_.size() != AUTH_VECTOR_LEN) ||
443 boost::scoped_ptr<HMAC> hmac(
447 std::vector<uint8_t> to_sign =
buffer_;
448 memmove(&to_sign[4], &
auth_[0],
auth_.size());
449 memset(&to_sign[ptr + 2], 0, AUTH_VECTOR_LEN);
450 hmac->update(&to_sign[0], to_sign.size());
451 vector<uint8_t> sign = hmac->sign(AUTH_VECTOR_LEN);
452 memmove(&
buffer_[ptr + 2], &sign[0], sign.size());
457 if ((ptr < AUTH_HDR_LEN) || (ptr >
buffer_.size() - 2 - AUTH_VECTOR_LEN) ||
458 (
buffer_[ptr + 1] != 2 + AUTH_VECTOR_LEN) ||
459 (
auth_.size() != AUTH_VECTOR_LEN) ||
464 vector<uint8_t> sign;
465 sign.resize(AUTH_VECTOR_LEN);
466 memmove(&sign[0], &
buffer_[ptr + 2], sign.size());
468 boost::scoped_ptr<HMAC> hmac(
472 std::vector<uint8_t> to_verify =
buffer_;
473 memmove(&to_verify[4], &
auth_[0],
auth_.size());
474 memset(&to_verify[ptr + 2], 0, AUTH_VECTOR_LEN);
476 hmac->update(&to_verify[0], to_verify.size());
477 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.