1 # Copyright (C) 2004-2007, 2009, 2010 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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),
56 _protocol_from_text = {
65 class KEYBase(dns.rdata.Rdata):
66 """KEY-like record base
68 @ivar flags: the key flags
70 @ivar protocol: the protocol for which this key may be used
72 @ivar algorithm: the algorithm used for the key
74 @ivar key: the public key
77 __slots__ = ['flags', 'protocol', 'algorithm', 'key']
79 def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
80 super(KEYBase, self).__init__(rdclass, rdtype)
82 self.protocol = protocol
83 self.algorithm = algorithm
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))
90 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
91 flags = tok.get_string()
95 flag_names = flags.split('|')
97 for flag in flag_names:
98 v = _flags_from_text.get(flag)
100 raise dns.exception.SyntaxError('unknown flag %s' % flag)
103 protocol = tok.get_string()
104 if protocol.isdigit():
105 protocol = int(protocol)
107 protocol = _protocol_from_text.get(protocol)
109 raise dns.exception.SyntaxError('unknown protocol %s' % protocol)
111 algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
114 t = tok.get().unescape()
115 if t.is_eol_or_eof():
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)
124 from_text = classmethod(from_text)
126 def to_wire(self, file, compress = None, origin = None):
127 header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
131 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
133 raise dns.exception.FormError
134 header = struct.unpack('!HBB', wire[current : current + 4])
137 key = wire[current : current + rdlen]
138 return cls(rdclass, rdtype, header[0], header[1], header[2],
141 from_wire = classmethod(from_wire)
143 def _cmp(self, other):
144 hs = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
145 ho = struct.pack("!HBB", other.flags, other.protocol, other.algorithm)
148 v = cmp(self.key, other.key)