1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import struct
19 import binascii
20
21 import dns.rdata
22 import dns.rdatatype
23
24
25 -class TLSA(dns.rdata.Rdata):
26
27 """TLSA record
28
29 @ivar usage: The certificate usage
30 @type usage: int
31 @ivar selector: The selector field
32 @type selector: int
33 @ivar mtype: The 'matching type' field
34 @type mtype: int
35 @ivar cert: The 'Certificate Association Data' field
36 @type cert: string
37 @see: RFC 6698"""
38
39 __slots__ = ['usage', 'selector', 'mtype', 'cert']
40
41 - def __init__(self, rdclass, rdtype, usage, selector,
42 mtype, cert):
48
49 - def to_text(self, origin=None, relativize=True, **kw):
50 return '%d %d %d %s' % (self.usage,
51 self.selector,
52 self.mtype,
53 dns.rdata._hexify(self.cert,
54 chunksize=128))
55
56 @classmethod
57 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
58 usage = tok.get_uint8()
59 selector = tok.get_uint8()
60 mtype = tok.get_uint8()
61 cert_chunks = []
62 while 1:
63 t = tok.get().unescape()
64 if t.is_eol_or_eof():
65 break
66 if not t.is_identifier():
67 raise dns.exception.SyntaxError
68 cert_chunks.append(t.value.encode())
69 cert = b''.join(cert_chunks)
70 cert = binascii.unhexlify(cert)
71 return cls(rdclass, rdtype, usage, selector, mtype, cert)
72
73 - def to_wire(self, file, compress=None, origin=None):
74 header = struct.pack("!BBB", self.usage, self.selector, self.mtype)
75 file.write(header)
76 file.write(self.cert)
77
78 @classmethod
79 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
80 header = struct.unpack("!BBB", wire[current: current + 3])
81 current += 3
82 rdlen -= 3
83 cert = wire[current: current + rdlen].unwrap()
84 return cls(rdclass, rdtype, header[0], header[1], header[2], cert)
85