Kea  1.9.9-git
option6_auth.cc
Go to the documentation of this file.
1 // Copyright (C) 2018,2021 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 #include <dhcp/dhcp6.h>
9 #include <dhcp/libdhcp++.h>
10 #include <dhcp/option6_auth.h>
11 #include <dhcp/option_space.h>
12 #include <exceptions/exceptions.h>
13 #include <util/io_utilities.h>
14 #include <util/encode/hex.h>
15 
16 #include <sstream>
17 #include <stdint.h>
18 
19 using namespace std;
20 using namespace isc::util;
21 
22 namespace isc {
23 namespace dhcp {
24 
25  Option6Auth::Option6Auth(const uint8_t proto, const uint8_t algo,
26  const uint8_t method, const uint64_t rdm,
27  const std::vector<uint8_t>& info)
28  : Option(Option::V6, D6O_AUTH),
29  protocol_(proto), algorithm_(algo),
30  rdm_method_(method), rdm_value_(rdm),
31  auth_info_(info) {
32 }
33 
36  return (cloneInternal<Option6Auth>());
37 }
38 
39 void
42  isc_throw(OutOfRange, "Option " << type_ << "Buffer too small for"
43  "packing data");
44  }
45 
46  //header = option code + length
47  buf.writeUint16(type_);
48  // length = 11 bytes fixed field length+ length of auth information
49  buf.writeUint16(11 + uint16_t(auth_info_.size()));
50  // protocol 1 byte
51  buf.writeUint8(protocol_);
52  // algorithm 1 byte
54  // replay detection method
56  // replay detection value
57  buf.writeUint64( rdm_value_);
58  // authentication information for reconfig msg
59  // should have zero
60 
61  for (auto i : auth_info_) {
62  buf.writeUint8(i);
63  }
64 }
65 
66 void
69  isc_throw(OutOfRange, "Option " << type_ << "Buffer too small for"
70  "computing hash input");
71  }
72 
73  //header = option code + length
74  buf.writeUint16(type_);
75  // length = 11 bytes fixed field length+ length of auth information
77  // protocol 1 byte
78  buf.writeUint8(protocol_);
79  // algorithm 1 byte
81  // replay detection method
83  // replay detection value
85  // authentication information for reconfig msg
86  // should have zero
87  for (uint8_t i = 0; i < OPTION6_HASH_MSG_LEN; i++) {
88  buf.writeUint8(0);
89  }
90 }
91 
92 void
95  // throw if it contains length less than minimum size of the auth option
96  if (distance(begin, end) < Option6Auth::OPTION6_AUTH_MIN_LEN) {
97  isc_throw(OutOfRange, "Option " << type_ << " truncated");
98  }
99 
100  protocol_ = *begin;
101  begin += sizeof(uint8_t);
102 
103  algorithm_ = *begin;
104  begin += sizeof(uint8_t);
105 
106  rdm_method_ = *begin;
107  begin += sizeof(uint8_t);
108 
109  rdm_value_ = isc::util::readUint64(&(*begin), sizeof(uint64_t));
110  begin += sizeof(uint64_t);
111 
112  auth_info_.erase(auth_info_.begin(), auth_info_.end());
113  std::for_each(begin, end, [this](uint8_t msgdata)
114  { auth_info_.push_back(msgdata); });
115 }
116 
117 std::string
118 Option6Auth::toText(int indent) const {
119  stringstream output;
120  std::string in(indent, ' '); //base indent
121 
122  output << in << "protocol=" << static_cast<int>(protocol_)
123  << ", algorithm=" << static_cast<int>(algorithm_)
124  << ", rdm method=" << static_cast<int>(rdm_method_)
125  << ", rdm value=" << rdm_value_
126  << ", value=" << isc::util::encode::encodeHex(auth_info_);
127 
128  return output.str();
129 }
130 
131 } // end namespace dhcp
132 } // end namespace isc
uint64_t rdm_value_
keeps replay detection method value
Definition: option6_auth.h:137
uint8_t rdm_method_
keeps replay detection method type
Definition: option6_auth.h:134
size_t getCapacity() const
Return the current capacity of the buffer.
Definition: buffer.h:393
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
static const uint8_t OPTION6_AUTH_MIN_LEN
Definition: option6_auth.h:30
STL namespace.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option6_auth.cc:35
std::vector< uint8_t > auth_info_
keeps authentication information
Definition: option6_auth.h:140
void writeUint64(uint64_t data)
Write an unsigned 64-bit integer in host byte order into the buffer in network byte order...
Definition: buffer.h:532
void packHashInput(isc::util::OutputBuffer &buf) const
Writes option in wire-format to buf, for computing hash auth info filled with 0 for a length of 128 b...
Definition: option6_auth.cc:67
static const uint8_t OPTION6_HDR
Definition: option6_auth.h:32
Definition: dhcp6.h:31
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: edns.h:19
uint8_t protocol_
keeps protocol type
Definition: option6_auth.h:128
uint8_t algorithm_
keeps hash algorithm value
Definition: option6_auth.h:131
void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to buf, returns pointer to first unused byte after stored option...
Definition: option6_auth.cc:40
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
virtual std::string toText(int indent=0) const
Provides human readable text representation.
Defines the logger used by the top-level component of kea-dhcp-ddns.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Definition: base_n.cc:469
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
static const uint8_t OPTION6_HASH_MSG_LEN
Definition: option6_auth.h:31
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order...
Definition: buffer.h:490
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option6_auth.cc:93
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:569
uint64_t readUint64(const uint8_t *buffer, size_t length)
Read Unsigned 64-Bit Integer from Buffer.
Definition: io_utilities.h:105