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
|
// Copyright (C) 2010 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 <map>
#include <utility>
#include <vector>
#include <exceptions/exceptions.h>
#include <crypto/crypto.h>
#include <dns/name.h>
#include <dns/util/base64.h>
#include <dns/tsigkey.h>
using namespace std;
namespace isc {
namespace dns {
struct
TSIGKey::TSIGKeyImpl {
TSIGKeyImpl(const Name& key_name, const Name& algorithm_name,
const void* secret, size_t secret_len) :
key_name_(key_name), algorithm_name_(algorithm_name),
secret_(static_cast<const uint8_t*>(secret),
static_cast<const uint8_t*>(secret) + secret_len)
{
// Convert the name to the canonical form.
algorithm_name_.downcase();
}
const Name key_name_;
Name algorithm_name_;
const vector<uint8_t> secret_;
};
TSIGKey::TSIGKey(const Name& key_name, const Name& algorithm_name,
const void* secret, size_t secret_len) : impl_(NULL)
{
if (algorithm_name != HMACMD5_NAME() &&
algorithm_name != HMACSHA1_NAME() &&
algorithm_name != HMACSHA256_NAME()) {
isc_throw(InvalidParameter, "Unknown TSIG algorithm is specified: " <<
algorithm_name);
}
if ((secret != NULL && secret_len == 0) ||
(secret == NULL && secret_len != 0)) {
isc_throw(InvalidParameter,
"TSIGKey secret and its length are inconsistent");
}
impl_ = new TSIGKeyImpl(key_name, algorithm_name, secret, secret_len);
}
TSIGKey::TSIGKey(const std::string& str) : impl_(NULL) {
const size_t pos = str.find(':');
if (pos == 0 || pos == str.npos || pos == str.size() - 1) {
// error
isc_throw(InvalidParameter, "Invalid TSIG key string");
}
try {
const Name key_name(str.substr(0, pos));
Name algo_name("hmac-md5.sig-alg.reg.int");
// optional algorithm part
size_t pos2 = str.find(':', pos + 1);
if (pos2 != str.npos) {
if (pos2 == pos + 1) {
isc_throw(InvalidParameter, "Invalid TSIG key string");
}
algo_name = Name(str.substr(pos2 + 1));
} else {
pos2 = str.size() - pos;
}
const std::string secret_str = str.substr(pos + 1, pos2 - pos - 1);
if (algo_name != HMACMD5_NAME() &&
algo_name != HMACSHA1_NAME() &&
algo_name != HMACSHA256_NAME()) {
isc_throw(InvalidParameter, "Unknown TSIG algorithm is specified: " <<
algo_name);
}
vector<uint8_t> secret;
decodeBase64(secret_str, secret);
impl_ = new TSIGKeyImpl(key_name, algo_name, &secret[0],
secret.size());
} catch (const 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 (impl_ == source.impl_) {
return (*this);
}
TSIGKeyImpl* newimpl = new TSIGKeyImpl(*source.impl_);
delete impl_;
impl_ = newimpl;
return (*this);
}
TSIGKey::~TSIGKey() {
delete impl_;
}
const Name&
TSIGKey::getKeyName() const {
return (impl_->key_name_);
}
const Name&
TSIGKey::getAlgorithmName() const {
return (impl_->algorithm_name_);
}
const void*
TSIGKey::getSecret() const {
return ((impl_->secret_.size() > 0) ? &impl_->secret_[0] : NULL);
}
size_t
TSIGKey::getSecretLength() const {
return (impl_->secret_.size());
}
std::string
TSIGKey::toText() const {
const vector<uint8_t> secret_v(static_cast<const uint8_t*>(getSecret()),
static_cast<const uint8_t*>(getSecret()) +
getSecretLength());
std::string secret_str = encodeBase64(secret_v);
return (getKeyName().toText() + ":" + secret_str + ":" +
getAlgorithmName().toText());
}
const
Name& TSIGKey::HMACMD5_NAME() {
static Name alg_name("hmac-md5.sig-alg.reg.int");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA1_NAME() {
static Name alg_name("hmac-sha1");
return (alg_name);
}
const
Name& TSIGKey::HMACSHA256_NAME() {
static Name alg_name("hmac-sha256");
return (alg_name);
}
struct TSIGKeyRing::TSIGKeyRingImpl {
typedef map<Name, TSIGKey> TSIGKeyMap;
typedef pair<Name, TSIGKey> NameAndKey;
TSIGKeyMap keys;
};
TSIGKeyRing::TSIGKeyRing() : impl_(new TSIGKeyRingImpl) {
}
TSIGKeyRing::~TSIGKeyRing() {
delete impl_;
}
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
== true) {
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) {
TSIGKeyRingImpl::TSIGKeyMap::const_iterator found =
impl_->keys.find(key_name);
if (found == impl_->keys.end()) {
return (FindResult(NOTFOUND, NULL));
}
return (FindResult(SUCCESS, &((*found).second)));
}
} // namespace dns
} // namespace isc
|