Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dns/rdtypes/ANY/NSEC3.py: 41%

68 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 07:09 +0000

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 

18import base64 

19import binascii 

20import struct 

21 

22import dns.exception 

23import dns.immutable 

24import dns.rdata 

25import dns.rdatatype 

26import dns.rdtypes.util 

27 

28b32_hex_to_normal = bytes.maketrans( 

29 b"0123456789ABCDEFGHIJKLMNOPQRSTUV", b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" 

30) 

31b32_normal_to_hex = bytes.maketrans( 

32 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", b"0123456789ABCDEFGHIJKLMNOPQRSTUV" 

33) 

34 

35# hash algorithm constants 

36SHA1 = 1 

37 

38# flag constants 

39OPTOUT = 1 

40 

41 

42@dns.immutable.immutable 

43class Bitmap(dns.rdtypes.util.Bitmap): 

44 type_name = "NSEC3" 

45 

46 

47@dns.immutable.immutable 

48class NSEC3(dns.rdata.Rdata): 

49 

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 to_text(self, origin=None, relativize=True, **kw): 

68 next = base64.b32encode(self.next).translate(b32_normal_to_hex).lower().decode() 

69 next = next.rstrip("=") 

70 if self.salt == b"": 

71 salt = "-" 

72 else: 

73 salt = binascii.hexlify(self.salt).decode() 

74 text = Bitmap(self.windows).to_text() 

75 return "%u %u %u %s %s%s" % ( 

76 self.algorithm, 

77 self.flags, 

78 self.iterations, 

79 salt, 

80 next, 

81 text, 

82 ) 

83 

84 @classmethod 

85 def from_text( 

86 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None 

87 ): 

88 algorithm = tok.get_uint8() 

89 flags = tok.get_uint8() 

90 iterations = tok.get_uint16() 

91 salt = tok.get_string() 

92 if salt == "-": 

93 salt = b"" 

94 else: 

95 salt = binascii.unhexlify(salt.encode("ascii")) 

96 next = tok.get_string().encode("ascii").upper().translate(b32_hex_to_normal) 

97 if next.endswith(b"="): 

98 raise binascii.Error("Incorrect padding") 

99 if len(next) % 8 != 0: 

100 next += b"=" * (8 - len(next) % 8) 

101 next = base64.b32decode(next) 

102 bitmap = Bitmap.from_text(tok) 

103 return cls(rdclass, rdtype, algorithm, flags, iterations, salt, next, bitmap) 

104 

105 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 

106 l = len(self.salt) 

107 file.write(struct.pack("!BBHB", self.algorithm, self.flags, self.iterations, l)) 

108 file.write(self.salt) 

109 l = len(self.next) 

110 file.write(struct.pack("!B", l)) 

111 file.write(self.next) 

112 Bitmap(self.windows).to_wire(file) 

113 

114 @classmethod 

115 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): 

116 (algorithm, flags, iterations) = parser.get_struct("!BBH") 

117 salt = parser.get_counted_bytes() 

118 next = parser.get_counted_bytes() 

119 bitmap = Bitmap.from_wire_parser(parser) 

120 return cls(rdclass, rdtype, algorithm, flags, iterations, salt, next, bitmap)