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

# Source Code for Module dns.rdtypes.dnskeybase

      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
     23  # wildcard import
     24  __all__ = ["SEP", "REVOKE", "ZONE",
     25             "flags_to_text_set", "flags_from_text_set"]
     26
     27  # flag constants
     28  SEP = 0x0001
     29  REVOKE = 0x0080
     30  ZONE = 0x0100
     31
     32  _flag_by_text = {
     33      'SEP': SEP,
     34      'REVOKE': REVOKE,
     35      'ZONE': ZONE
     36  }
     37
     38  # We construct the inverse mapping programmatically to ensure that we
     39  # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
     40  # would cause the mapping not to be true inverse.
     41  _flag_by_value = dict((y, x) for x, y in _flag_by_text.items())
     42
     43
     44 -def flags_to_text_set(flags):
     45      """Convert a DNSKEY flags value to set texts
     46      @rtype: set([string])"""
     47
     48      flags_set = set()
     49      mask = 0x1
     50      while mask <= 0x8000:
     51          if flags & mask:
     52              text = _flag_by_value.get(mask)
     53              if not text:
     54                  text = hex(mask)
     55              flags_set.add(text)
     56          mask <<= 1
     57      return flags_set
     58
     59
     60 -def flags_from_text_set(texts_set):
     61      """Convert set of DNSKEY flag mnemonic texts to DNSKEY flag value
     62      @rtype: int"""
     63
     64      flags = 0
     65      for text in texts_set:
     66          try:
     67              flags += _flag_by_text[text]
     68          except KeyError:
     69              raise NotImplementedError(
     70                  "DNSKEY flag '%s' is not supported" % text)
     71      return flags
     72
     73
     74 -class DNSKEYBase(dns.rdata.Rdata):
     75
     76      """Base class for rdata that is like a DNSKEY record
     77
     78      @ivar flags: the key flags
     79      @type flags: int
     80      @ivar protocol: the protocol for which this key may be used
     81      @type protocol: int
     82      @ivar algorithm: the algorithm used for the key
     83      @type algorithm: int
     84      @ivar key: the public key
     85      @type key: string"""
     86
     87      __slots__ = ['flags', 'protocol', 'algorithm', 'key']
     88
     89 -    def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
     90          super(DNSKEYBase, self).__init__(rdclass, rdtype)
     91          self.flags = flags
     92          self.protocol = protocol
     93          self.algorithm = algorithm
     94          self.key = key
     95
     96 -    def to_text(self, origin=None, relativize=True, **kw):
     97          return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm,
     98                                  dns.rdata._base64ify(self.key))
     99
    100      @classmethod
    101 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
    102          flags = tok.get_uint16()
    103          protocol = tok.get_uint8()
    104          algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
    105          chunks = []
    106          while 1:
    107              t = tok.get().unescape()
    108              if t.is_eol_or_eof():
    109                  break
    110              if not t.is_identifier():
    111                  raise dns.exception.SyntaxError
    112              chunks.append(t.value.encode())
    113          b64 = b''.join(chunks)
    114          key = base64.b64decode(b64)
    115          return cls(rdclass, rdtype, flags, protocol, algorithm, key)
    116
    117 -    def to_wire(self, file, compress=None, origin=None):
    118          header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
    119          file.write(header)
    120          file.write(self.key)
    121
    122      @classmethod
    123 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    124          if rdlen < 4:
    125              raise dns.exception.FormError
    126          header = struct.unpack('!HBB', wire[current: current + 4])
    127          current += 4
    128          rdlen -= 4
    129          key = wire[current: current + rdlen].unwrap()
    130          return cls(rdclass, rdtype, header[0], header[1], header[2],
    131                     key)
    132
    133 -    def flags_to_text_set(self):
    134          """Convert a DNSKEY flags value to set texts
    135          @rtype: set([string])"""
    136          return flags_to_text_set(self.flags)
    137