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