Debian Codegraph @

dns/rdtypes/tlsabase.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2005-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 import binascii
19 import struct
20
21 import dns.immutable
22 import dns.rdata
23 import dns.rdatatype
24
25
26 @dns.immutable.immutable
27 class TLSABase(dns.rdata.Rdata):
28 """Base class for TLSA and SMIMEA records"""
29
30 # see: RFC 6698
31
32 __slots__ = ["usage", "selector", "mtype", "cert"]
33
34 def __init__(self, rdclass, rdtype, usage, selector, mtype, cert):
35 super().__init__(rdclass, rdtype)
36 self.usage = self._as_uint8(usage)
37 self.selector = self._as_uint8(selector)
38 self.mtype = self._as_uint8(mtype)
39 self.cert = self._as_bytes(cert)
40
41 def to_text(self, origin=None, relativize=True, **kw):
42 kw = kw.copy()
43 chunksize = kw.pop("chunksize", 128)
44 cert = dns.rdata._hexify(
45 self.cert, chunksize=chunksize, **kw # pyright: ignore
46 )
47 return f"{self.usage} {self.selector} {self.mtype} {cert}"
48
49 @classmethod
50 def from_text(
51 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
52 ):
53 usage = tok.get_uint8()
54 selector = tok.get_uint8()
55 mtype = tok.get_uint8()
56 cert = tok.concatenate_remaining_identifiers().encode()
57 cert = binascii.unhexlify(cert)
58 return cls(rdclass, rdtype, usage, selector, mtype, cert)
59
60 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
61 header = struct.pack("!BBB", self.usage, self.selector, self.mtype)
62 file.write(header)
63 file.write(self.cert)
64
65 @classmethod
66 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
67 header = parser.get_struct("BBB")
68 cert = parser.get_remaining()
69 return cls(rdclass, rdtype, header[0], header[1], header[2], cert)