| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2001-2007, 2009-2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
"""DNS TSIG support.""" |
| 19 |
|
| 20 |
import base64 |
| 21 |
import hashlib |
| 22 |
import hmac |
| 23 |
import struct |
| 24 |
|
| 25 |
import dns.exception |
| 26 |
import dns.name |
| 27 |
import dns.rcode |
| 28 |
import dns.rdataclass |
| 29 |
import dns.rdatatype |
| 30 |
|
| 31 |
|
| 32 |
class BadTime(dns.exception.DNSException): |
| 33 |
"""The current time is not within the TSIG's validity time.""" |
| 34 |
|
| 35 |
|
| 36 |
class BadSignature(dns.exception.DNSException): |
| 37 |
"""The TSIG signature fails to verify.""" |
| 38 |
|
| 39 |
|
| 40 |
class BadKey(dns.exception.DNSException): |
| 41 |
"""The TSIG record owner name does not match the key.""" |
| 42 |
|
| 43 |
|
| 44 |
class BadAlgorithm(dns.exception.DNSException): |
| 45 |
"""The TSIG algorithm does not match the key.""" |
| 46 |
|
| 47 |
|
| 48 |
class PeerError(dns.exception.DNSException): |
| 49 |
"""Base class for all TSIG errors generated by the remote peer""" |
| 50 |
|
| 51 |
|
| 52 |
class PeerBadKey(PeerError): |
| 53 |
"""The peer didn't know the key we used""" |
| 54 |
|
| 55 |
|
| 56 |
class PeerBadSignature(PeerError): |
| 57 |
"""The peer didn't like the signature we sent""" |
| 58 |
|
| 59 |
|
| 60 |
class PeerBadTime(PeerError): |
| 61 |
"""The peer didn't like the time we sent""" |
| 62 |
|
| 63 |
|
| 64 |
class PeerBadTruncation(PeerError): |
| 65 |
"""The peer didn't like amount of truncation in the TSIG we sent""" |
| 66 |
|
| 67 |
|
| 68 |
# TSIG Algorithms |
| 69 |
|
| 70 |
HMAC_MD5 = dns.name.from_text("HMAC-MD5.SIG-ALG.REG.INT") |
| 71 |
HMAC_SHA1 = dns.name.from_text("hmac-sha1") |
| 72 |
HMAC_SHA224 = dns.name.from_text("hmac-sha224") |
| 73 |
HMAC_SHA256 = dns.name.from_text("hmac-sha256") |
| 74 |
HMAC_SHA256_128 = dns.name.from_text("hmac-sha256-128") |
| 75 |
HMAC_SHA384 = dns.name.from_text("hmac-sha384") |
| 76 |
HMAC_SHA384_192 = dns.name.from_text("hmac-sha384-192") |
| 77 |
HMAC_SHA512 = dns.name.from_text("hmac-sha512") |
| 78 |
HMAC_SHA512_256 = dns.name.from_text("hmac-sha512-256") |
| 79 |
GSS_TSIG = dns.name.from_text("gss-tsig") |
| 80 |
|
| 81 |
default_algorithm = HMAC_SHA256 |
| 82 |
|
| 83 |
mac_sizes = { |
| 84 |
HMAC_SHA1: 20, |
| 85 |
HMAC_SHA224: 28, |
| 86 |
HMAC_SHA256: 32, |
| 87 |
HMAC_SHA256_128: 16, |
| 88 |
HMAC_SHA384: 48, |
| 89 |
HMAC_SHA384_192: 24, |
| 90 |
HMAC_SHA512: 64, |
| 91 |
HMAC_SHA512_256: 32, |
| 92 |
HMAC_MD5: 16, |
| 93 |
GSS_TSIG: 128, # This is what we assume to be the worst case! |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
class GSSTSig: |
| 98 |
""" |
| 99 |
GSS-TSIG TSIG implementation. This uses the GSS-API context established |
| 100 |
in the TKEY message handshake to sign messages using GSS-API message |
| 101 |
integrity codes, per the RFC. |
| 102 |
|
| 103 |
In order to avoid a direct GSSAPI dependency, the keyring holds a ref |
| 104 |
to the GSSAPI object required, rather than the key itself. |
| 105 |
""" |
| 106 |
|
| 107 |
def __init__(self, gssapi_context): |
| 108 |
self.gssapi_context = gssapi_context |
| 109 |
self.data = b"" |
| 110 |
self.name = "gss-tsig" |
| 111 |
|
| 112 |
def update(self, data): |
| 113 |
self.data += data |
| 114 |
|
| 115 |
def sign(self): |
| 116 |
# defer to the GSSAPI function to sign |
| 117 |
return self.gssapi_context.get_signature(self.data) |
| 118 |
|
| 119 |
def verify(self, expected): |
| 120 |
try: |
| 121 |
# defer to the GSSAPI function to verify |
| 122 |
return self.gssapi_context.verify_signature(self.data, expected) |
| 123 |
except Exception: |
| 124 |
# note the usage of a bare exception |
| 125 |
raise BadSignature |
| 126 |
|
| 127 |
|
| 128 |
class GSSTSigAdapter: |
| 129 |
def __init__(self, keyring): |
| 130 |
self.keyring = keyring |
| 131 |
|
| 132 |
def __call__(self, message, keyname): |
| 133 |
if keyname in self.keyring: |
| 134 |
key = self.keyring[keyname] |
| 135 |
if isinstance(key, Key) and key.algorithm == GSS_TSIG: |
| 136 |
if message: |
| 137 |
GSSTSigAdapter.parse_tkey_and_step(key, message, keyname) |
| 138 |
return key |
| 139 |
else: |
| 140 |
return None |
| 141 |
|
| 142 |
@classmethod |
| 143 |
def parse_tkey_and_step(cls, key, message, keyname): |
| 144 |
# if the message is a TKEY type, absorb the key material |
| 145 |
# into the context using step(); this is used to allow the |
| 146 |
# client to complete the GSSAPI negotiation before attempting |
| 147 |
# to verify the signed response to a TKEY message exchange |
| 148 |
try: |
| 149 |
rrset = message.find_rrset( |
| 150 |
message.answer, keyname, dns.rdataclass.ANY, dns.rdatatype.TKEY |
| 151 |
) |
| 152 |
if rrset: |
| 153 |
token = rrset[0].key |
| 154 |
gssapi_context = key.secret |
| 155 |
return gssapi_context.step(token) |
| 156 |
except KeyError: |
| 157 |
pass |
| 158 |
|
| 159 |
|
| 160 |
class HMACTSig: |
| 161 |
""" |
| 162 |
HMAC TSIG implementation. This uses the HMAC python module to handle the |
| 163 |
sign/verify operations. |
| 164 |
""" |
| 165 |
|
| 166 |
_hashes = { |
| 167 |
HMAC_SHA1: hashlib.sha1, |
| 168 |
HMAC_SHA224: hashlib.sha224, |
| 169 |
HMAC_SHA256: hashlib.sha256, |
| 170 |
HMAC_SHA256_128: (hashlib.sha256, 128), |
| 171 |
HMAC_SHA384: hashlib.sha384, |
| 172 |
HMAC_SHA384_192: (hashlib.sha384, 192), |
| 173 |
HMAC_SHA512: hashlib.sha512, |
| 174 |
HMAC_SHA512_256: (hashlib.sha512, 256), |
| 175 |
HMAC_MD5: hashlib.md5, |
| 176 |
} |
| 177 |
|
| 178 |
def __init__(self, key, algorithm): |
| 179 |
try: |
| 180 |
hashinfo = self._hashes[algorithm] |
| 181 |
except KeyError: |
| 182 |
raise NotImplementedError(f"TSIG algorithm {algorithm} is not supported") |
| 183 |
|
| 184 |
# create the HMAC context |
| 185 |
if isinstance(hashinfo, tuple): |
| 186 |
self.hmac_context = hmac.new(key, digestmod=hashinfo[0]) |
| 187 |
self.size = hashinfo[1] |
| 188 |
else: |
| 189 |
self.hmac_context = hmac.new(key, digestmod=hashinfo) |
| 190 |
self.size = None |
| 191 |
self.name = self.hmac_context.name |
| 192 |
if self.size: |
| 193 |
self.name += f"-{self.size}" |
| 194 |
|
| 195 |
def update(self, data): |
| 196 |
return self.hmac_context.update(data) |
| 197 |
|
| 198 |
def sign(self): |
| 199 |
# defer to the HMAC digest() function for that digestmod |
| 200 |
digest = self.hmac_context.digest() |
| 201 |
if self.size: |
| 202 |
digest = digest[: (self.size // 8)] |
| 203 |
return digest |
| 204 |
|
| 205 |
def verify(self, expected): |
| 206 |
# re-digest and compare the results |
| 207 |
mac = self.sign() |
| 208 |
if not hmac.compare_digest(mac, expected): |
| 209 |
raise BadSignature |
| 210 |
|
| 211 |
|
| 212 |
def _digest(wire, key, rdata, time=None, request_mac=None, ctx=None, multi=None): |
| 213 |
"""Return a context containing the TSIG rdata for the input parameters |
| 214 |
@rtype: dns.tsig.HMACTSig or dns.tsig.GSSTSig object |
| 215 |
@raises ValueError: I{other_data} is too long |
| 216 |
@raises NotImplementedError: I{algorithm} is not supported |
| 217 |
""" |
| 218 |
|
| 219 |
first = not (ctx and multi) |
| 220 |
if first: |
| 221 |
ctx = get_context(key) |
| 222 |
if request_mac: |
| 223 |
ctx.update(struct.pack("!H", len(request_mac))) |
| 224 |
ctx.update(request_mac) |
| 225 |
assert ctx is not None # for type checkers |
| 226 |
ctx.update(struct.pack("!H", rdata.original_id)) |
| 227 |
ctx.update(wire[2:]) |
| 228 |
if first: |
| 229 |
ctx.update(key.name.to_digestable()) |
| 230 |
ctx.update(struct.pack("!H", dns.rdataclass.ANY)) |
| 231 |
ctx.update(struct.pack("!I", 0)) |
| 232 |
if time is None: |
| 233 |
time = rdata.time_signed |
| 234 |
upper_time = (time >> 32) & 0xFFFF |
| 235 |
lower_time = time & 0xFFFFFFFF |
| 236 |
time_encoded = struct.pack("!HIH", upper_time, lower_time, rdata.fudge) |
| 237 |
other_len = len(rdata.other) |
| 238 |
if other_len > 65535: |
| 239 |
raise ValueError("TSIG Other Data is > 65535 bytes") |
| 240 |
if first: |
| 241 |
ctx.update(key.algorithm.to_digestable() + time_encoded) |
| 242 |
ctx.update(struct.pack("!HH", rdata.error, other_len) + rdata.other) |
| 243 |
else: |
| 244 |
ctx.update(time_encoded) |
| 245 |
return ctx |
| 246 |
|
| 247 |
|
| 248 |
def _maybe_start_digest(key, mac, multi): |
| 249 |
"""If this is the first message in a multi-message sequence, |
| 250 |
start a new context. |
| 251 |
@rtype: dns.tsig.HMACTSig or dns.tsig.GSSTSig object |
| 252 |
""" |
| 253 |
if multi: |
| 254 |
ctx = get_context(key) |
| 255 |
ctx.update(struct.pack("!H", len(mac))) |
| 256 |
ctx.update(mac) |
| 257 |
return ctx |
| 258 |
else: |
| 259 |
return None |
| 260 |
|
| 261 |
|
| 262 |
def sign(wire, key, rdata, time=None, request_mac=None, ctx=None, multi=False): |
| 263 |
"""Return a (tsig_rdata, mac, ctx) tuple containing the HMAC TSIG rdata |
| 264 |
for the input parameters, the HMAC MAC calculated by applying the |
| 265 |
TSIG signature algorithm, and the TSIG digest context. |
| 266 |
@rtype: (string, dns.tsig.HMACTSig or dns.tsig.GSSTSig object) |
| 267 |
@raises ValueError: I{other_data} is too long |
| 268 |
@raises NotImplementedError: I{algorithm} is not supported |
| 269 |
""" |
| 270 |
|
| 271 |
ctx = _digest(wire, key, rdata, time, request_mac, ctx, multi) |
| 272 |
mac = ctx.sign() |
| 273 |
tsig = rdata.replace(time_signed=time, mac=mac) |
| 274 |
|
| 275 |
return (tsig, _maybe_start_digest(key, mac, multi)) |
| 276 |
|
| 277 |
|
| 278 |
def validate( |
| 279 |
wire, key, owner, rdata, now, request_mac, tsig_start, ctx=None, multi=False |
| 280 |
): |
| 281 |
"""Validate the specified TSIG rdata against the other input parameters. |
| 282 |
|
| 283 |
@raises FormError: The TSIG is badly formed. |
| 284 |
@raises BadTime: There is too much time skew between the client and the |
| 285 |
server. |
| 286 |
@raises BadSignature: The TSIG signature did not validate |
| 287 |
@rtype: dns.tsig.HMACTSig or dns.tsig.GSSTSig object""" |
| 288 |
|
| 289 |
(adcount,) = struct.unpack("!H", wire[10:12]) |
| 290 |
if adcount == 0: |
| 291 |
raise dns.exception.FormError |
| 292 |
adcount -= 1 |
| 293 |
new_wire = wire[0:10] + struct.pack("!H", adcount) + wire[12:tsig_start] |
| 294 |
if rdata.error != 0: |
| 295 |
if rdata.error == dns.rcode.BADSIG: |
| 296 |
raise PeerBadSignature |
| 297 |
elif rdata.error == dns.rcode.BADKEY: |
| 298 |
raise PeerBadKey |
| 299 |
elif rdata.error == dns.rcode.BADTIME: |
| 300 |
raise PeerBadTime |
| 301 |
elif rdata.error == dns.rcode.BADTRUNC: |
| 302 |
raise PeerBadTruncation |
| 303 |
else: |
| 304 |
raise PeerError(f"unknown TSIG error code {rdata.error}") |
| 305 |
if abs(rdata.time_signed - now) > rdata.fudge: |
| 306 |
raise BadTime |
| 307 |
if key.name != owner: |
| 308 |
raise BadKey |
| 309 |
if key.algorithm != rdata.algorithm: |
| 310 |
raise BadAlgorithm |
| 311 |
ctx = _digest(new_wire, key, rdata, None, request_mac, ctx, multi) |
| 312 |
ctx.verify(rdata.mac) |
| 313 |
return _maybe_start_digest(key, rdata.mac, multi) |
| 314 |
|
| 315 |
|
| 316 |
def get_context(key): |
| 317 |
"""Returns an HMAC context for the specified key. |
| 318 |
|
| 319 |
@rtype: HMAC context |
| 320 |
@raises NotImplementedError: I{algorithm} is not supported |
| 321 |
""" |
| 322 |
|
| 323 |
if key.algorithm == GSS_TSIG: |
| 324 |
return GSSTSig(key.secret) |
| 325 |
else: |
| 326 |
return HMACTSig(key.secret, key.algorithm) |
| 327 |
|
| 328 |
|
| 329 |
class Key: |
| 330 |
def __init__( |
| 331 |
self, |
| 332 |
name: dns.name.Name | str, |
| 333 |
secret: bytes | str, |
| 334 |
algorithm: dns.name.Name | str = default_algorithm, |
| 335 |
): |
| 336 |
if isinstance(name, str): |
| 337 |
name = dns.name.from_text(name) |
| 338 |
self.name = name |
| 339 |
if isinstance(secret, str): |
| 340 |
secret = base64.decodebytes(secret.encode()) |
| 341 |
self.secret = secret |
| 342 |
if isinstance(algorithm, str): |
| 343 |
algorithm = dns.name.from_text(algorithm) |
| 344 |
self.algorithm = algorithm |
| 345 |
|
| 346 |
def __eq__(self, other): |
| 347 |
return ( |
| 348 |
isinstance(other, Key) |
| 349 |
and self.name == other.name |
| 350 |
and self.secret == other.secret |
| 351 |
and self.algorithm == other.algorithm |
| 352 |
) |
| 353 |
|
| 354 |
def __repr__(self): |
| 355 |
r = f"<DNS key name='{self.name}', " + f"algorithm='{self.algorithm}'" |
| 356 |
if self.algorithm != GSS_TSIG: |
| 357 |
r += f", secret='{base64.b64encode(self.secret).decode()}'" |
| 358 |
r += ">" |
| 359 |
return r |