1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import struct
17
18 import dns.exception
19 import dns.dnssec
20 import dns.rdata
21
22 _flags_from_text = {
23 'NOCONF': (0x4000, 0xC000),
24 'NOAUTH': (0x8000, 0xC000),
25 'NOKEY': (0xC000, 0xC000),
26 'FLAG2': (0x2000, 0x2000),
27 'EXTEND': (0x1000, 0x1000),
28 'FLAG4': (0x0800, 0x0800),
29 'FLAG5': (0x0400, 0x0400),
30 'USER': (0x0000, 0x0300),
31 'ZONE': (0x0100, 0x0300),
32 'HOST': (0x0200, 0x0300),
33 'NTYP3': (0x0300, 0x0300),
34 'FLAG8': (0x0080, 0x0080),
35 'FLAG9': (0x0040, 0x0040),
36 'FLAG10': (0x0020, 0x0020),
37 'FLAG11': (0x0010, 0x0010),
38 'SIG0': (0x0000, 0x000f),
39 'SIG1': (0x0001, 0x000f),
40 'SIG2': (0x0002, 0x000f),
41 'SIG3': (0x0003, 0x000f),
42 'SIG4': (0x0004, 0x000f),
43 'SIG5': (0x0005, 0x000f),
44 'SIG6': (0x0006, 0x000f),
45 'SIG7': (0x0007, 0x000f),
46 'SIG8': (0x0008, 0x000f),
47 'SIG9': (0x0009, 0x000f),
48 'SIG10': (0x000a, 0x000f),
49 'SIG11': (0x000b, 0x000f),
50 'SIG12': (0x000c, 0x000f),
51 'SIG13': (0x000d, 0x000f),
52 'SIG14': (0x000e, 0x000f),
53 'SIG15': (0x000f, 0x000f),
54 }
55
56 _protocol_from_text = {
57 'NONE' : 0,
58 'TLS' : 1,
59 'EMAIL' : 2,
60 'DNSSEC' : 3,
61 'IPSEC' : 4,
62 'ALL' : 255,
63 }
64
66 """KEY-like record base
67
68 @ivar flags: the key flags
69 @type flags: int
70 @ivar protocol: the protocol for which this key may be used
71 @type protocol: int
72 @ivar algorithm: the algorithm used for the key
73 @type algorithm: int
74 @ivar key: the public key
75 @type key: string"""
76
77 __slots__ = ['flags', 'protocol', 'algorithm', 'key']
78
79 - def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
85
86 - def to_text(self, origin=None, relativize=True, **kw):
87 return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm,
88 dns.rdata._base64ify(self.key))
89
90 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
91 flags = tok.get_string()
92 if flags.isdigit():
93 flags = int(flags)
94 else:
95 flag_names = flags.split('|')
96 flags = 0
97 for flag in flag_names:
98 v = _flags_from_text.get(flag)
99 if v is None:
100 raise dns.exception.SyntaxError('unknown flag %s' % flag)
101 flags &= ~v[1]
102 flags |= v[0]
103 protocol = tok.get_string()
104 if protocol.isdigit():
105 protocol = int(protocol)
106 else:
107 protocol = _protocol_from_text.get(protocol)
108 if protocol is None:
109 raise dns.exception.SyntaxError('unknown protocol %s' % protocol)
110
111 algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
112 chunks = []
113 while 1:
114 t = tok.get().unescape()
115 if t.is_eol_or_eof():
116 break
117 if not t.is_identifier():
118 raise dns.exception.SyntaxError
119 chunks.append(t.value)
120 b64 = ''.join(chunks)
121 key = b64.decode('base64_codec')
122 return cls(rdclass, rdtype, flags, protocol, algorithm, key)
123
124 from_text = classmethod(from_text)
125
126 - def to_wire(self, file, compress = None, origin = None):
130
131 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
132 if rdlen < 4:
133 raise dns.exception.FormError
134 header = struct.unpack('!HBB', wire[current : current + 4])
135 current += 4
136 rdlen -= 4
137 key = wire[current : current + rdlen]
138 return cls(rdclass, rdtype, header[0], header[1], header[2],
139 key)
140
141 from_wire = classmethod(from_wire)
142
143 - def _cmp(self, other):
150