dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

dnskeybase.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2004-2007, 2009-2011 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18 import base64
19 import struct
20 
21 import dns.exception
22 import dns.dnssec
23 import dns.rdata
24 
25 # wildcard import
26 __all__ = ["SEP", "REVOKE", "ZONE",
27  "flags_to_text_set", "flags_from_text_set"]
28 
29 # flag constants
30 SEP = 0x0001
31 REVOKE = 0x0080
32 ZONE = 0x0100
33 
34 _flag_by_text = {
35  'SEP': SEP,
36  'REVOKE': REVOKE,
37  'ZONE': ZONE
38 }
39 
40 # We construct the inverse mapping programmatically to ensure that we
41 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
42 # would cause the mapping not to be true inverse.
43 _flag_by_value = {y: x for x, y in _flag_by_text.items()}
44 
45 
46 def flags_to_text_set(flags):
47  """Convert a DNSKEY flags value to set texts
48  @rtype: set([string])"""
49 
50  flags_set = set()
51  mask = 0x1
52  while mask <= 0x8000:
53  if flags & mask:
54  text = _flag_by_value.get(mask)
55  if not text:
56  text = hex(mask)
57  flags_set.add(text)
58  mask <<= 1
59  return flags_set
60 
61 
62 def flags_from_text_set(texts_set):
63  """Convert set of DNSKEY flag mnemonic texts to DNSKEY flag value
64  @rtype: int"""
65 
66  flags = 0
67  for text in texts_set:
68  try:
69  flags += _flag_by_text[text]
70  except KeyError:
71  raise NotImplementedError(
72  "DNSKEY flag '%s' is not supported" % text)
73  return flags
74 
75 
77 
78  """Base class for rdata that is like a DNSKEY record
79 
80  @ivar flags: the key flags
81  @type flags: int
82  @ivar protocol: the protocol for which this key may be used
83  @type protocol: int
84  @ivar algorithm: the algorithm used for the key
85  @type algorithm: int
86  @ivar key: the public key
87  @type key: string"""
88 
89  __slots__ = ['flags', 'protocol', 'algorithm', 'key']
90 
91  def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key):
92  super(DNSKEYBase, self).__init__(rdclass, rdtype)
93  self.flags = flags
94  self.protocol = protocol
95  self.algorithm = algorithm
96  self.key = key
97 
98  def to_text(self, origin=None, relativize=True, **kw):
99  return '%d %d %d %s' % (self.flags, self.protocol, self.algorithm,
101 
102  @classmethod
103  def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
104  flags = tok.get_uint16()
105  protocol = tok.get_uint8()
106  algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
107  chunks = []
108  while 1:
109  t = tok.get().unescape()
110  if t.is_eol_or_eof():
111  break
112  if not t.is_identifier():
114  chunks.append(t.value.encode())
115  b64 = b''.join(chunks)
116  key = base64.b64decode(b64)
117  return cls(rdclass, rdtype, flags, protocol, algorithm, key)
118 
119  def to_wire(self, file, compress=None, origin=None):
120  header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm)
121  file.write(header)
122  file.write(self.key)
123 
124  @classmethod
125  def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
126  if rdlen < 4:
128  header = struct.unpack('!HBB', wire[current: current + 4])
129  current += 4
130  rdlen -= 4
131  key = wire[current: current + rdlen].unwrap()
132  return cls(rdclass, rdtype, header[0], header[1], header[2],
133  key)
134 
135  def flags_to_text_set(self):
136  """Convert a DNSKEY flags value to set texts
137  @rtype: set([string])"""
138  return flags_to_text_set(self.flags)
dns.rdtypes.dnskeybase.DNSKEYBase.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: dnskeybase.py:103
dns.exception.FormError
Definition: exception.py:109
dns.rdtypes.dnskeybase.DNSKEYBase.__init__
def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key)
Definition: dnskeybase.py:91
dns.exception.SyntaxError
Definition: exception.py:113
dns.rdtypes.dnskeybase.flags_to_text_set
def flags_to_text_set(flags)
Definition: dnskeybase.py:46
dns.rdtypes.dnskeybase.DNSKEYBase.key
key
Definition: dnskeybase.py:96
dns.rdtypes.dnskeybase.DNSKEYBase.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: dnskeybase.py:125
dns.rdtypes.dnskeybase.DNSKEYBase.flags_to_text_set
def flags_to_text_set(self)
Definition: dnskeybase.py:135
dns.rdtypes.dnskeybase.DNSKEYBase.algorithm
algorithm
Definition: dnskeybase.py:95
dns.dnssec
Definition: dnssec.py:1
dns.rdtypes.dnskeybase.DNSKEYBase.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: dnskeybase.py:98
dns.rdtypes.dnskeybase.DNSKEYBase.flags
flags
Definition: dnskeybase.py:93
dns.rdtypes.dnskeybase.DNSKEYBase
Definition: dnskeybase.py:76
dns.dnssec.algorithm_from_text
def algorithm_from_text(text)
Definition: dnssec.py:95
dns.rdata
Definition: rdata.py:1
dns.rdtypes.dnskeybase.DNSKEYBase.protocol
protocol
Definition: dnskeybase.py:94
dns.rdata._base64ify
def _base64ify(data, chunksize=_base64_chunksize)
Definition: rdata.py:53
dns.rdtypes.dnskeybase.DNSKEYBase.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: dnskeybase.py:119
dns.rdtypes.dnskeybase.flags_from_text_set
def flags_from_text_set(texts_set)
Definition: dnskeybase.py:62
dns.exception
Definition: exception.py:1
dns.rdata.Rdata
Definition: rdata.py:95