Package dns :: Package rdtypes :: Package ANY :: Module DNSKEY
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.ANY.DNSKEY

 1  # Copyright (C) 2004-2007, 2009-2011 Nominum, Inc. 
 2  # 
 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. 
 7  # 
 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. 
15   
16  import base64 
17  import struct 
18   
19  import dns.exception 
20  import dns.dnssec 
21  import dns.rdata 
22  import dns.util 
23   
24  # flag constants 
25  SEP = 0x0001 
26  REVOKE = 0x0080 
27  ZONE = 0x0100 
28 29 -class DNSKEY(dns.rdata.Rdata):
30 """DNSKEY record 31 32 @ivar flags: the key flags 33 @type flags: int 34 @ivar protocol: the protocol for which this key may be used 35 @type protocol: int 36 @ivar algorithm: the algorithm used for the key 37 @type algorithm: int 38 @ivar key: the public key 39 @type key: string""" 40 41 __slots__ = ['flags', 'protocol', 'algorithm', 'key'] 42
43 - def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
44 super(DNSKEY, self).__init__(rdclass, rdtype) 45 self.flags = flags 46 self.protocol = protocol 47 self.algorithm = algorithm 48 self.key = key
49
50 - def to_text(self, origin=None, relativize=True, **kw):
51 return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm, 52 dns.rdata._base64ify(self.key))
53 54 @classmethod
55 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
56 flags = tok.get_uint16() 57 protocol = tok.get_uint8() 58 algorithm = dns.dnssec.algorithm_from_text(tok.get_string()) 59 chunks = [] 60 while 1: 61 t = tok.get().unescape() 62 if t.is_eol_or_eof(): 63 break 64 if not t.is_identifier(): 65 raise dns.exception.SyntaxError 66 chunks.append(t.value) 67 b64 = ''.join(chunks) 68 key = base64.b64decode(b64.encode('ascii')) 69 return cls(rdclass, rdtype, flags, protocol, algorithm, key)
70
71 - def to_wire(self, file, compress = None, origin = None):
72 header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm) 73 file.write(header) 74 file.write(self.key)
75 76 @classmethod
77 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
78 if rdlen < 4: 79 raise dns.exception.FormError 80 header = struct.unpack('!HBB', wire[current : current + 4]) 81 current += 4 82 rdlen -= 4 83 key = wire[current : current + rdlen].unwrap() 84 return cls(rdclass, rdtype, header[0], header[1], header[2], 85 key)
86
87 - def _cmp(self, other):
88 hs = struct.pack("!HBB", self.flags, self.protocol, self.algorithm) 89 ho = struct.pack("!HBB", other.flags, other.protocol, other.algorithm) 90 v = dns.util.cmp(hs, ho) 91 if v == 0: 92 v = dns.util.cmp(self.key, other.key) 93 return v
94