Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dns/rdtypes/dsbase.py: 60%

43 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) 2010, 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 

18import binascii 

19import struct 

20 

21import dns.dnssectypes 

22import dns.immutable 

23import dns.rdata 

24import dns.rdatatype 

25 

26 

27@dns.immutable.immutable 

28class DSBase(dns.rdata.Rdata): 

29 

30 """Base class for rdata that is like a DS record""" 

31 

32 __slots__ = ["key_tag", "algorithm", "digest_type", "digest"] 

33 

34 # Digest types registry: 

35 # https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml 

36 _digest_length_by_type = { 

37 1: 20, # SHA-1, RFC 3658 Sec. 2.4 

38 2: 32, # SHA-256, RFC 4509 Sec. 2.2 

39 3: 32, # GOST R 34.11-94, RFC 5933 Sec. 4 in conjunction with RFC 4490 Sec. 2.1 

40 4: 48, # SHA-384, RFC 6605 Sec. 2 

41 } 

42 

43 def __init__(self, rdclass, rdtype, key_tag, algorithm, digest_type, digest): 

44 super().__init__(rdclass, rdtype) 

45 self.key_tag = self._as_uint16(key_tag) 

46 self.algorithm = dns.dnssectypes.Algorithm.make(algorithm) 

47 self.digest_type = dns.dnssectypes.DSDigest.make(self._as_uint8(digest_type)) 

48 self.digest = self._as_bytes(digest) 

49 try: 

50 if len(self.digest) != self._digest_length_by_type[self.digest_type]: 

51 raise ValueError("digest length inconsistent with digest type") 

52 except KeyError: 

53 if self.digest_type == 0: # reserved, RFC 3658 Sec. 2.4 

54 raise ValueError("digest type 0 is reserved") 

55 

56 def to_text(self, origin=None, relativize=True, **kw): 

57 kw = kw.copy() 

58 chunksize = kw.pop("chunksize", 128) 

59 return "%d %d %d %s" % ( 

60 self.key_tag, 

61 self.algorithm, 

62 self.digest_type, 

63 dns.rdata._hexify(self.digest, chunksize=chunksize, **kw), 

64 ) 

65 

66 @classmethod 

67 def from_text( 

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

69 ): 

70 key_tag = tok.get_uint16() 

71 algorithm = tok.get_string() 

72 digest_type = tok.get_uint8() 

73 digest = tok.concatenate_remaining_identifiers().encode() 

74 digest = binascii.unhexlify(digest) 

75 return cls(rdclass, rdtype, key_tag, algorithm, digest_type, digest) 

76 

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

78 header = struct.pack("!HBB", self.key_tag, self.algorithm, self.digest_type) 

79 file.write(header) 

80 file.write(self.digest) 

81 

82 @classmethod 

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

84 header = parser.get_struct("!HBB") 

85 digest = parser.get_remaining() 

86 return cls(rdclass, rdtype, header[0], header[1], header[2], digest)