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