Debian Codegraph @

dns/rdtypes/ANY/NSEC3.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2004-2017 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 import base64
19 import binascii
20 import struct
21
22 import dns.exception
23 import dns.immutable
24 import dns.name
25 import dns.rdata
26 import dns.rdatatype
27 import dns.rdtypes.util
28
29 b32_hex_to_normal = bytes.maketrans(
30 b"0123456789ABCDEFGHIJKLMNOPQRSTUV", b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
31 )
32 b32_normal_to_hex = bytes.maketrans(
33 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", b"0123456789ABCDEFGHIJKLMNOPQRSTUV"
34 )
35
36 # hash algorithm constants
37 SHA1 = 1
38
39 # flag constants
40 OPTOUT = 1
41
42
43 @dns.immutable.immutable
44 class Bitmap(dns.rdtypes.util.Bitmap):
45 type_name = "NSEC3"
46
47
48 @dns.immutable.immutable
49 class NSEC3(dns.rdata.Rdata):
50 """NSEC3 record"""
51
52 __slots__ = ["algorithm", "flags", "iterations", "salt", "next", "windows"]
53
54 def __init__(
55 self, rdclass, rdtype, algorithm, flags, iterations, salt, next, windows
56 ):
57 super().__init__(rdclass, rdtype)
58 self.algorithm = self._as_uint8(algorithm)
59 self.flags = self._as_uint8(flags)
60 self.iterations = self._as_uint16(iterations)
61 self.salt = self._as_bytes(salt, True, 255)
62 self.next = self._as_bytes(next, True, 255)
63 if not isinstance(windows, Bitmap):
64 windows = Bitmap(windows)
65 self.windows = tuple(windows.windows)
66
67 def _next_text(self):
68 next = base64.b32encode(self.next).translate(b32_normal_to_hex).lower().decode()
69 next = next.rstrip("=")
70 return next
71
72 def to_text(self, origin=None, relativize=True, **kw):
73 next = self._next_text()
74 if self.salt == b"":
75 salt = "-"
76 else:
77 salt = binascii.hexlify(self.salt).decode()
78 text = Bitmap(self.windows).to_text()
79 return f"{self.algorithm} {self.flags} {self.iterations} {salt} {next}{text}"
80
81 @classmethod
82 def from_text(
83 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
84 ):
85 algorithm = tok.get_uint8()
86 flags = tok.get_uint8()
87 iterations = tok.get_uint16()
88 salt = tok.get_string()
89 if salt == "-":
90 salt = b""
91 else:
92 salt = binascii.unhexlify(salt.encode("ascii"))
93 next = tok.get_string().encode("ascii").upper().translate(b32_hex_to_normal)
94 if next.endswith(b"="):
95 raise binascii.Error("Incorrect padding")
96 if len(next) % 8 != 0:
97 next += b"=" * (8 - len(next) % 8)
98 next = base64.b32decode(next)
99 bitmap = Bitmap.from_text(tok)
100 return cls(rdclass, rdtype, algorithm, flags, iterations, salt, next, bitmap)
101
102 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
103 l = len(self.salt)
104 file.write(struct.pack("!BBHB", self.algorithm, self.flags, self.iterations, l))
105 file.write(self.salt)
106 l = len(self.next)
107 file.write(struct.pack("!B", l))
108 file.write(self.next)
109 Bitmap(self.windows).to_wire(file)
110
111 @classmethod
112 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
113 (algorithm, flags, iterations) = parser.get_struct("!BBH")
114 salt = parser.get_counted_bytes()
115 next = parser.get_counted_bytes()
116 bitmap = Bitmap.from_wire_parser(parser)
117 return cls(rdclass, rdtype, algorithm, flags, iterations, salt, next, bitmap)
118
119 def next_name(self, origin=None):
120 return dns.name.from_text(self._next_text(), origin)