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
|
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <sys/time.h>
#include <stdint.h>
#include <cassert> // for the tentative verifyTentative()
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <exceptions/exceptions.h>
#include <util/buffer.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/tsig.h>
#include <dns/tsigerror.h>
#include <dns/tsigkey.h>
#include <cryptolink/cryptolink.h>
#include <cryptolink/crypto_hmac.h>
using namespace std;
using namespace isc::util;
using namespace isc::cryptolink;
using namespace isc::dns::rdata;
namespace isc {
namespace dns {
// Borrowed from dnssectime.cc. This trick should be unified somewhere.
namespace tsig {
namespace detail {
int64_t (*gettimeFunction)() = NULL;
}
}
namespace {
int64_t
gettimeofdayWrapper() {
using namespace tsig::detail;
if (gettimeFunction != NULL) {
return (gettimeFunction());
}
struct timeval now;
gettimeofday(&now, NULL);
return (static_cast<int64_t>(now.tv_sec));
}
}
namespace {
typedef boost::shared_ptr<HMAC> HMACPtr;
struct TSIGContext::TSIGContextImpl {
TSIGContextImpl(const TSIGKey& key) :
state_(INIT), key_(key), error_(Rcode::NOERROR()),
previous_timesigned_(0)
{}
State state_;
TSIGKey key_;
vector<uint8_t> previous_digest_;
TSIGError error_;
uint64_t previous_timesigned_; // only meaningful for response with BADTIME
};
}
const RRClass&
TSIGRecord::getClass() {
return (RRClass::ANY());
}
TSIGContext::TSIGContext(const TSIGKey& key) : impl_(new TSIGContextImpl(key))
{
}
TSIGContext::~TSIGContext() {
delete impl_;
}
TSIGContext::State
TSIGContext::getState() const {
return (impl_->state_);
}
TSIGError
TSIGContext::getError() const {
return (impl_->error_);
}
ConstTSIGRecordPtr
TSIGContext::sign(const uint16_t qid, const void* const data,
const size_t data_len)
{
TSIGError error(TSIGError::NOERROR());
const uint64_t now = (gettimeofdayWrapper() & 0x0000ffffffffffffULL);
// For responses adjust the error code.
if (impl_->state_ == CHECKED) {
error = impl_->error_;
}
// For errors related to key or MAC, return an unsigned response as
// specified in Section 4.3 of RFC2845.
if (error == TSIGError::BAD_SIG() || error == TSIGError::BAD_KEY()) {
impl_->previous_digest_.clear();
impl_->state_ = SIGNED;
ConstTSIGRecordPtr tsig(new TSIGRecord(
any::TSIG(impl_->key_.getAlgorithmName(),
now, DEFAULT_FUDGE, NULL, 0,
qid, error.getCode(), 0, NULL)));
return (tsig);
}
OutputBuffer variables(0);
HMACPtr hmac(CryptoLink::getCryptoLink().createHMAC(
impl_->key_.getSecret(),
impl_->key_.getSecretLength(),
impl_->key_.getCryptoAlgorithm()),
deleteHMAC);
// If the context has previous MAC (either the Request MAC or its own
// previous MAC), digest it.
if (impl_->state_ != INIT) {
const uint16_t previous_digest_len(impl_->previous_digest_.size());
variables.writeUint16(previous_digest_len);
if (previous_digest_len != 0) {
variables.writeData(&impl_->previous_digest_[0],
previous_digest_len);
}
hmac->update(variables.getData(), variables.getLength());
}
// Digest the message (without TSIG)
hmac->update(data, data_len);
//
// Digest TSIG variables. If state_ is SIGNED we skip digesting them
// except for time related variables (RFC2845 4.4).
//
variables.clear();
if (impl_->state_ != SIGNED) {
impl_->key_.getKeyName().toWire(variables);
TSIGRecord::getClass().toWire(variables);
variables.writeUint32(TSIGRecord::TSIG_TTL);
impl_->key_.getAlgorithmName().toWire(variables);
}
const uint64_t time_signed = (error == TSIGError::BAD_TIME()) ?
impl_->previous_timesigned_ : now;
variables.writeUint16(time_signed >> 32);
variables.writeUint32(time_signed & 0xffffffff);
variables.writeUint16(DEFAULT_FUDGE);
hmac->update(variables.getData(), variables.getLength());
variables.clear();
if (impl_->state_ != SIGNED) {
variables.writeUint16(error.getCode());
// For BADTIME error, digest 6 bytes of other data.
// (6 bytes = size of time signed value)
variables.writeUint16((error == TSIGError::BAD_TIME()) ? 6 : 0);
hmac->update(variables.getData(), variables.getLength());
variables.clear();
if (error == TSIGError::BAD_TIME()) {
variables.writeUint16(now >> 32);
variables.writeUint32(now & 0xffffffff);
hmac->update(variables.getData(), variables.getLength());
}
}
const uint16_t otherlen = variables.getLength();
// Get the final digest, update internal state, then finish.
impl_->previous_digest_ = hmac->sign();
impl_->state_ = SIGNED;
ConstTSIGRecordPtr tsig(new TSIGRecord(
any::TSIG(impl_->key_.getAlgorithmName(),
time_signed, DEFAULT_FUDGE,
impl_->previous_digest_.size(),
&impl_->previous_digest_[0],
qid, error.getCode(), otherlen,
otherlen == 0 ?
NULL : variables.getData())));
return (tsig);
}
void
TSIGContext::verifyTentative(ConstTSIGRecordPtr tsig, TSIGError error) {
const any::TSIG tsig_rdata = tsig->getRdata();
impl_->error_ = error;
if (error == TSIGError::BAD_TIME()) {
impl_->previous_timesigned_ = tsig_rdata.getTimeSigned();
}
// For simplicity we assume non empty digests.
assert(tsig_rdata.getMACSize() != 0);
impl_->previous_digest_.assign(
static_cast<const uint8_t*>(tsig_rdata.getMAC()),
static_cast<const uint8_t*>(tsig_rdata.getMAC()) +
tsig_rdata.getMACSize());
impl_->state_ = CHECKED;
}
namespace {
const char* const tsigerror_text[] = {
"BADSIG",
"BADKEY",
"BADTIME"
};
}
TSIGError::TSIGError(Rcode rcode) : code_(rcode.getCode()) {
if (code_ > MAX_RCODE_FOR_TSIGERROR) {
isc_throw(OutOfRange, "Invalid RCODE for TSIG Error: " << rcode);
}
}
string
TSIGError::toText() const {
if (code_ <= MAX_RCODE_FOR_TSIGERROR) {
return (Rcode(code_).toText());
} else if (code_ <= BAD_TIME_CODE) {
return (tsigerror_text[code_ - (MAX_RCODE_FOR_TSIGERROR + 1)]);
} else {
return (boost::lexical_cast<string>(code_));
}
}
} // namespace dns
} // namespace isc
|