1 # Copyright (C) 2004-2007, 2009-2011 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 b32_hex_to_normal = string.maketrans('0123456789ABCDEFGHIJKLMNOPQRSTUV',
26 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')
27 b32_normal_to_hex = string.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
28 '0123456789ABCDEFGHIJKLMNOPQRSTUV')
30 # hash algorithm constants
36 class NSEC3(dns.rdata.Rdata):
39 @ivar algorithm: the hash algorithm number
41 @ivar flags: the flags
43 @ivar iterations: the number of iterations
47 @ivar next: the next name hash
49 @ivar windows: the windowed bitmap list
50 @type windows: list of (window number, string) tuples"""
52 __slots__ = ['algorithm', 'flags', 'iterations', 'salt', 'next', 'windows']
54 def __init__(self, rdclass, rdtype, algorithm, flags, iterations, salt,
56 super(NSEC3, self).__init__(rdclass, rdtype)
57 self.algorithm = algorithm
59 self.iterations = iterations
62 self.windows = windows
64 def to_text(self, origin=None, relativize=True, **kw):
65 next = base64.b32encode(self.next).translate(b32_normal_to_hex).lower()
69 salt = self.salt.encode('hex-codec')
71 for (window, bitmap) in self.windows:
73 for i in xrange(0, len(bitmap)):
75 for j in xrange(0, 8):
76 if byte & (0x80 >> j):
77 bits.append(dns.rdatatype.to_text(window * 256 + \
79 text += (' ' + ' '.join(bits))
80 return '%u %u %u %s %s%s' % (self.algorithm, self.flags, self.iterations,
83 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
84 algorithm = tok.get_uint8()
85 flags = tok.get_uint8()
86 iterations = tok.get_uint16()
87 salt = tok.get_string()
91 salt = salt.decode('hex-codec')
92 next = tok.get_string().upper().translate(b32_hex_to_normal)
93 next = base64.b32decode(next)
96 token = tok.get().unescape()
97 if token.is_eol_or_eof():
99 nrdtype = dns.rdatatype.from_text(token.value)
101 raise dns.exception.SyntaxError("NSEC3 with bit 0")
103 raise dns.exception.SyntaxError("NSEC3 with bit > 65535")
104 rdtypes.append(nrdtype)
111 for nrdtype in rdtypes:
112 if nrdtype == prior_rdtype:
114 prior_rdtype = nrdtype
115 new_window = nrdtype // 256
116 if new_window != window:
117 windows.append((window, ''.join(bitmap[0:octets])))
120 offset = nrdtype % 256
124 bitmap[byte] = chr(ord(bitmap[byte]) | (0x80 >> bit))
125 windows.append((window, ''.join(bitmap[0:octets])))
126 return cls(rdclass, rdtype, algorithm, flags, iterations, salt, next, windows)
128 from_text = classmethod(from_text)
130 def to_wire(self, file, compress = None, origin = None):
132 file.write(struct.pack("!BBHB", self.algorithm, self.flags,
134 file.write(self.salt)
136 file.write(struct.pack("!B", l))
137 file.write(self.next)
138 for (window, bitmap) in self.windows:
139 file.write(chr(window))
140 file.write(chr(len(bitmap)))
143 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
144 (algorithm, flags, iterations, slen) = struct.unpack('!BBHB',
145 wire[current : current + 5])
148 salt = wire[current : current + slen].unwrap()
151 (nlen, ) = struct.unpack('!B', wire[current])
154 next = wire[current : current + nlen].unwrap()
160 raise dns.exception.FormError("NSEC3 too short")
161 window = ord(wire[current])
162 octets = ord(wire[current + 1])
163 if octets == 0 or octets > 32:
164 raise dns.exception.FormError("bad NSEC3 octets")
168 raise dns.exception.FormError("bad NSEC3 bitmap length")
169 bitmap = wire[current : current + octets].unwrap()
172 windows.append((window, bitmap))
173 return cls(rdclass, rdtype, algorithm, flags, iterations, salt, next, windows)
175 from_wire = classmethod(from_wire)
177 def _cmp(self, other):
178 b1 = cStringIO.StringIO()
180 b2 = cStringIO.StringIO()
182 return cmp(b1.getvalue(), b2.getvalue())