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
     17  import struct
     18
     19  import dns.exception
     20  import dns.dnssec
     21  import dns.rdata
     22
     23
     24  # flag constants
     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  # We construct the inverse mapping programmatically to ensure that we
     36  # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
     37  # would cause the mapping not to be true inverse.
     38  _flag_by_value = dict([(y, x) for x, y in _flag_by_text.iteritems()])
     39
     40
     41 -def flags_to_text_set(flags):
     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
     57 -def flags_from_text_set(texts_set):
     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
     71 -class DNSKEY(dns.rdata.Rdata):
     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):
     86          super(DNSKEY, self).__init__(rdclass, rdtype)
     87          self.flags = flags
     88          self.protocol = protocol
     89          self.algorithm = algorithm
     90          self.key = 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):
    115          header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
    116          file.write(header)
    117          file.write(self.key)
    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):
    132          hs = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
    133          ho = struct.pack("!HBB", other.flags, other.protocol, other.algorithm)
    134          v = cmp(hs, ho)
    135          if v == 0:
    136              v = cmp(self.key, other.key)
    137          return v
    138
    139 -    def flags_to_text_set(self):
    140          """Convert a DNSKEY flags value to set texts
    141          @rtype: set([string])"""
    142          return flags_to_text_set(self.flags)
    143