1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import base64
19 import struct
20
21 import dns.exception
22 import dns.dnssec
23 import dns.rdata
24
25
26 __all__ = ["SEP", "REVOKE", "ZONE",
27 "flags_to_text_set", "flags_from_text_set"]
28
29
30 SEP = 0x0001
31 REVOKE = 0x0080
32 ZONE = 0x0100
33
34 _flag_by_text = {
35 'SEP': SEP,
36 'REVOKE': REVOKE,
37 'ZONE': ZONE
38 }
39
40
41
42
43 _flag_by_value = {y: x for x, y in _flag_by_text.items()}
44
45
46 -def flags_to_text_set(flags):
47 """Convert a DNSKEY flags value to set texts
48 @rtype: set([string])"""
49
50 flags_set = set()
51 mask = 0x1
52 while mask <= 0x8000:
53 if flags & mask:
54 text = _flag_by_value.get(mask)
55 if not text:
56 text = hex(mask)
57 flags_set.add(text)
58 mask <<= 1
59 return flags_set
60
61
62 -def flags_from_text_set(texts_set):
63 """Convert set of DNSKEY flag mnemonic texts to DNSKEY flag value
64 @rtype: int"""
65
66 flags = 0
67 for text in texts_set:
68 try:
69 flags += _flag_by_text[text]
70 except KeyError:
71 raise NotImplementedError(
72 "DNSKEY flag '%s' is not supported" % text)
73 return flags
74
77
78 """Base class for rdata that is like a DNSKEY record
79
80 @ivar flags: the key flags
81 @type flags: int
82 @ivar protocol: the protocol for which this key may be used
83 @type protocol: int
84 @ivar algorithm: the algorithm used for the key
85 @type algorithm: int
86 @ivar key: the public key
87 @type key: string"""
88
89 __slots__ = ['flags', 'protocol', 'algorithm', 'key']
90
91 - def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
97
98 - def to_text(self, origin=None, relativize=True, **kw):
99 return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm,
100 dns.rdata._base64ify(self.key))
101
102 @classmethod
103 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
104 flags = tok.get_uint16()
105 protocol = tok.get_uint8()
106 algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
107 chunks = []
108 while 1:
109 t = tok.get().unescape()
110 if t.is_eol_or_eof():
111 break
112 if not t.is_identifier():
113 raise dns.exception.SyntaxError
114 chunks.append(t.value.encode())
115 b64 = b''.join(chunks)
116 key = base64.b64decode(b64)
117 return cls(rdclass, rdtype, flags, protocol, algorithm, key)
118
119 - def to_wire(self, file, compress=None, origin=None):
123
124 @classmethod
125 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
126 if rdlen < 4:
127 raise dns.exception.FormError
128 header = struct.unpack('!HBB', wire[current: current + 4])
129 current += 4
130 rdlen -= 4
131 key = wire[current: current + rdlen].unwrap()
132 return cls(rdclass, rdtype, header[0], header[1], header[2],
133 key)
134
136 """Convert a DNSKEY flags value to set texts
137 @rtype: set([string])"""
138 return flags_to_text_set(self.flags)
139