1 # Copyright (C) 2004-2007, 2009-2011 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.
28 class DNSKEY(dns.rdata.Rdata):
31 @ivar flags: the key flags
33 @ivar protocol: the protocol for which this key may be used
35 @ivar algorithm: the algorithm used for the key
37 @ivar key: the public key
40 __slots__ = ['flags', 'protocol', 'algorithm', 'key']
42 def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
43 super(DNSKEY, self).__init__(rdclass, rdtype)
45 self.protocol = protocol
46 self.algorithm = algorithm
49 def to_text(self, origin=None, relativize=True, **kw):
50 return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm,
51 dns.rdata._base64ify(self.key))
53 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
54 flags = tok.get_uint16()
55 protocol = tok.get_uint8()
56 algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
59 t = tok.get().unescape()
62 if not t.is_identifier():
63 raise dns.exception.SyntaxError
64 chunks.append(t.value)
66 key = b64.decode('base64_codec')
67 return cls(rdclass, rdtype, flags, protocol, algorithm, key)
69 from_text = classmethod(from_text)
71 def to_wire(self, file, compress = None, origin = None):
72 header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
76 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
78 raise dns.exception.FormError
79 header = struct.unpack('!HBB', wire[current : current + 4])
82 key = wire[current : current + rdlen].unwrap()
83 return cls(rdclass, rdtype, header[0], header[1], header[2],
86 from_wire = classmethod(from_wire)
88 def _cmp(self, other):
89 hs = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
90 ho = struct.pack("!HBB", other.flags, other.protocol, other.algorithm)
93 v = cmp(self.key, other.key)