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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 | // Copyright (C) 2010-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <map><--- Include file:
#include <utility><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <exceptions/exceptions.h>
#include <cryptolink/cryptolink.h>
#include <dns/name.h>
#include <util/encode/encode.h>
#include <dns/tsigkey.h>
#include <boost/lexical_cast.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace std;
using namespace isc::cryptolink;
namespace isc {
namespace dns {
struct
TSIGKey::TSIGKeyImpl {
TSIGKeyImpl(const Name& key_name, const Name& algorithm_name,
isc::cryptolink::HashAlgorithm algorithm,
size_t digestbits) :
key_name_(key_name), algorithm_name_(algorithm_name),
algorithm_(algorithm), digestbits_(digestbits),
secret_() {
// Convert the key and algorithm names to the canonical form.
key_name_.downcase();
if (algorithm == isc::cryptolink::MD5) {
algorithm_name_ = TSIGKey::HMACMD5_NAME();
}
algorithm_name_.downcase();
}
TSIGKeyImpl(const Name& key_name, const Name& algorithm_name,
isc::cryptolink::HashAlgorithm algorithm,
size_t digestbits,
const void* secret, size_t secret_len) :
key_name_(key_name), algorithm_name_(algorithm_name),
algorithm_(algorithm), digestbits_(digestbits),
secret_(static_cast<const uint8_t*>(secret),
static_cast<const uint8_t*>(secret) + secret_len) {
// Convert the key and algorithm names to the canonical form.
key_name_.downcase();
if (algorithm == isc::cryptolink::MD5) {
algorithm_name_ = TSIGKey::HMACMD5_NAME();
}
algorithm_name_.downcase();
}
Name key_name_;
Name algorithm_name_;
const isc::cryptolink::HashAlgorithm algorithm_;
size_t digestbits_;
const vector<uint8_t> secret_;
};
namespace {
HashAlgorithm
convertAlgorithmName(const isc::dns::Name& name) {
if (name == TSIGKey::HMACMD5_NAME()) {
return (isc::cryptolink::MD5);
}
if (name == TSIGKey::HMACMD5_SHORT_NAME()) {
return (isc::cryptolink::MD5);
}
if (name == TSIGKey::HMACSHA1_NAME()) {
return (isc::cryptolink::SHA1);
}
if (name == TSIGKey::HMACSHA224_NAME()) {
return (isc::cryptolink::SHA224);
}
if (name == TSIGKey::HMACSHA256_NAME()) {
return (isc::cryptolink::SHA256);
}
if (name == TSIGKey::HMACSHA384_NAME()) {
return (isc::cryptolink::SHA384);
}
if (name == TSIGKey::HMACSHA512_NAME()) {
return (isc::cryptolink::SHA512);
}
return (isc::cryptolink::UNKNOWN_HASH);
}
}
TSIGKey::TSIGKey(const Name& key_name, const Name& algorithm_name,
const void* secret, size_t secret_len,
size_t digestbits /*= 0*/) : impl_(0) {
const HashAlgorithm algorithm = convertAlgorithmName(algorithm_name);
if ((secret && secret_len == 0) ||
(!secret && secret_len != 0)) {
isc_throw(InvalidParameter,
"TSIGKey secret and its length are inconsistent: " <<
key_name << ":" << algorithm_name);
}
if (algorithm == isc::cryptolink::UNKNOWN_HASH && secret_len != 0) {
isc_throw(InvalidParameter,
"TSIGKey with unknown algorithm has non empty secret: " <<
key_name << ":" << algorithm_name);
}
if (!secret) {
impl_.reset(new TSIGKey::TSIGKeyImpl(key_name, algorithm_name, algorithm,
digestbits));
} else {
impl_.reset(new TSIGKey::TSIGKeyImpl(key_name, algorithm_name, algorithm,
digestbits, secret, secret_len));
}
}
TSIGKey::TSIGKey(const std::string& str) : impl_(0) {
try {
istringstream iss(str);
string keyname_str;
getline(iss, keyname_str, ':');
if (iss.fail() || iss.bad() || iss.eof()) {
isc_throw(InvalidParameter, "Invalid TSIG key string: " << str);
}
string secret_str;
getline(iss, secret_str, ':');
if (iss.fail() || iss.bad()) {
isc_throw(InvalidParameter, "Invalid TSIG key string: " << str);
}
string algo_str;
if (!iss.eof()) {
getline(iss, algo_str, ':');
}
if (iss.fail() || iss.bad()) {
isc_throw(InvalidParameter, "Invalid TSIG key string: " << str);
}
string dgstbt_str;
if (!iss.eof()) {
getline(iss, dgstbt_str);
}
if (iss.fail() || iss.bad()) {
isc_throw(InvalidParameter, "Invalid TSIG key string: " << str);
}
const Name algo_name(algo_str.empty() ? "hmac-md5.sig-alg.reg.int" :
algo_str);
const HashAlgorithm algorithm = convertAlgorithmName(algo_name);
size_t digestbits = 0;
try {
if (!dgstbt_str.empty()) {
digestbits = boost::lexical_cast<size_t>(dgstbt_str);
}
} catch (const boost::bad_lexical_cast&) {
isc_throw(InvalidParameter,
"TSIG key with non-numeric digestbits: " << dgstbt_str);
}
vector<uint8_t> secret;
isc::util::encode::decodeBase64(secret_str, secret);
if (algorithm == isc::cryptolink::UNKNOWN_HASH && !secret.empty()) {
isc_throw(InvalidParameter,
"TSIG key with unknown algorithm has non empty secret: "
<< str);
}
if (secret.empty()) {
impl_.reset(new TSIGKey::TSIGKeyImpl(Name(keyname_str), algo_name, algorithm,
digestbits));
} else {
impl_.reset(new TSIGKey::TSIGKeyImpl(Name(keyname_str), algo_name, algorithm,
digestbits, &secret[0], secret.size()));
}
} catch (const isc::Exception& e) {
// 'reduce' the several types of exceptions name parsing and
// Base64 decoding can throw to just the InvalidParameter
isc_throw(InvalidParameter, e.what());
}
}
TSIGKey::TSIGKey(const TSIGKey& source) : impl_(new TSIGKeyImpl(*source.impl_)) {
}
TSIGKey&
TSIGKey::operator=(const TSIGKey& source) {
if (this == &source) {
return (*this);
}
impl_.reset(new TSIGKey::TSIGKeyImpl(*source.impl_));
return (*this);
}
TSIGKey::~TSIGKey() {
}
const Name&
TSIGKey::getKeyName() const {
return (impl_->key_name_);
}
const Name&
TSIGKey::getAlgorithmName() const {
return (impl_->algorithm_name_);
}
isc::cryptolink::HashAlgorithm
TSIGKey::getAlgorithm() const {
return (impl_->algorithm_);
}
size_t
TSIGKey::getDigestbits() const {
return (impl_->digestbits_);
}
const void*
TSIGKey::getSecret() const {
return ((impl_->secret_.size() > 0) ? &impl_->secret_[0] : 0);
}
size_t
TSIGKey::getSecretLength() const {
return (impl_->secret_.size());
}
std::string
TSIGKey::toText() const {
size_t digestbits = getDigestbits();
const vector<uint8_t> secret_v(static_cast<const uint8_t*>(getSecret()),
static_cast<const uint8_t*>(getSecret()) +
getSecretLength());
std::string secret_str = isc::util::encode::encodeBase64(secret_v);
if (digestbits) {
std::string dgstbt_str = boost::lexical_cast<std::string>(static_cast<int>(digestbits));
return (getKeyName().toText() + ":" + secret_str + ":" +
getAlgorithmName().toText() + ":" + dgstbt_str);
} else {
return (getKeyName().toText() + ":" + secret_str + ":" +
getAlgorithmName().toText());
}
}
struct TSIGKeyRing::TSIGKeyRingImpl {
typedef map<Name, TSIGKey> TSIGKeyMap;
typedef pair<Name, TSIGKey> NameAndKey;
TSIGKeyMap keys;
};
TSIGKeyRing::TSIGKeyRing() : impl_(new TSIGKeyRingImpl()) {
}
TSIGKeyRing::~TSIGKeyRing() {
}
unsigned int
TSIGKeyRing::size() const {
return (impl_->keys.size());
}
TSIGKeyRing::Result
TSIGKeyRing::add(const TSIGKey& key) {
if (impl_->keys.insert(TSIGKeyRingImpl::NameAndKey(key.getKeyName(), key)).second) {
return (SUCCESS);
} else {
return (EXIST);
}
}
TSIGKeyRing::Result
TSIGKeyRing::remove(const Name& key_name) {
return (impl_->keys.erase(key_name) == 1 ? SUCCESS : NOTFOUND);
}
TSIGKeyRing::FindResult
TSIGKeyRing::find(const Name& key_name) const {
TSIGKeyRingImpl::TSIGKeyMap::const_iterator found =
impl_->keys.find(key_name);
if (found == impl_->keys.end()) {
return (FindResult(NOTFOUND, 0));
}
return (FindResult(SUCCESS, &((*found).second)));
}
TSIGKeyRing::FindResult
TSIGKeyRing::find(const Name& key_name, const Name& algorithm_name) const {
TSIGKeyRingImpl::TSIGKeyMap::const_iterator found =
impl_->keys.find(key_name);
if (found == impl_->keys.end() ||
(*found).second.getAlgorithmName() != algorithm_name) {
return (FindResult(NOTFOUND, 0));
}
return (FindResult(SUCCESS, &((*found).second)));
}
const
Name& TSIGKey::HMACMD5_NAME() {
static Name alg_name("hmac-md5.sig-alg.reg.int");
return (alg_name);
}
const
Name& TSIGKey::HMACMD5_SHORT_NAME() {
static Name alg_name("hmac-md5");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA1_NAME() {
static Name alg_name("hmac-sha1");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA224_NAME() {
static Name alg_name("hmac-sha224");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA256_NAME() {
static Name alg_name("hmac-sha256");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA384_NAME() {
static Name alg_name("hmac-sha384");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA512_NAME() {
static Name alg_name("hmac-sha512");
return (alg_name);
}
const
Name& TSIGKey::GSSTSIG_NAME() {
static Name alg_name("gss-tsig");
return (alg_name);
}
} // namespace dns
} // namespace isc
|