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)  

CERT.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2003-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 struct
19 import base64
20 
21 import dns.exception
22 import dns.dnssec
23 import dns.rdata
24 import dns.tokenizer
25 
26 _ctype_by_value = {
27  1: 'PKIX',
28  2: 'SPKI',
29  3: 'PGP',
30  253: 'URI',
31  254: 'OID',
32 }
33 
34 _ctype_by_name = {
35  'PKIX': 1,
36  'SPKI': 2,
37  'PGP': 3,
38  'URI': 253,
39  'OID': 254,
40 }
41 
42 
43 def _ctype_from_text(what):
44  v = _ctype_by_name.get(what)
45  if v is not None:
46  return v
47  return int(what)
48 
49 
50 def _ctype_to_text(what):
51  v = _ctype_by_value.get(what)
52  if v is not None:
53  return v
54  return str(what)
55 
56 
58 
59  """CERT record
60 
61  @ivar certificate_type: certificate type
62  @type certificate_type: int
63  @ivar key_tag: key tag
64  @type key_tag: int
65  @ivar algorithm: algorithm
66  @type algorithm: int
67  @ivar certificate: the certificate or CRL
68  @type certificate: string
69  @see: RFC 2538"""
70 
71  __slots__ = ['certificate_type', 'key_tag', 'algorithm', 'certificate']
72 
73  def __init__(self, rdclass, rdtype, certificate_type, key_tag, algorithm,
74  certificate):
75  super(CERT, self).__init__(rdclass, rdtype)
76  self.certificate_type = certificate_type
77  self.key_tag = key_tag
78  self.algorithm = algorithm
79  self.certificate = certificate
80 
81  def to_text(self, origin=None, relativize=True, **kw):
82  certificate_type = _ctype_to_text(self.certificate_type)
83  return "%s %d %s %s" % (certificate_type, self.key_tag,
86 
87  @classmethod
88  def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
89  certificate_type = _ctype_from_text(tok.get_string())
90  key_tag = tok.get_uint16()
91  algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
92  if algorithm < 0 or algorithm > 255:
93  raise dns.exception.SyntaxError("bad algorithm type")
94  chunks = []
95  while 1:
96  t = tok.get().unescape()
97  if t.is_eol_or_eof():
98  break
99  if not t.is_identifier():
101  chunks.append(t.value.encode())
102  b64 = b''.join(chunks)
103  certificate = base64.b64decode(b64)
104  return cls(rdclass, rdtype, certificate_type, key_tag,
105  algorithm, certificate)
106 
107  def to_wire(self, file, compress=None, origin=None):
108  prefix = struct.pack("!HHB", self.certificate_type, self.key_tag,
109  self.algorithm)
110  file.write(prefix)
111  file.write(self.certificate)
112 
113  @classmethod
114  def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
115  prefix = wire[current: current + 5].unwrap()
116  current += 5
117  rdlen -= 5
118  if rdlen < 0:
120  (certificate_type, key_tag, algorithm) = struct.unpack("!HHB", prefix)
121  certificate = wire[current: current + rdlen].unwrap()
122  return cls(rdclass, rdtype, certificate_type, key_tag, algorithm,
123  certificate)
dns.rdtypes.ANY.CERT.CERT.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: CERT.py:81
dns.exception.FormError
Definition: exception.py:109
dns.exception.SyntaxError
Definition: exception.py:113
dns.rdtypes.ANY.CERT.CERT.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: CERT.py:114
dns.rdtypes.ANY.CERT.CERT
Definition: CERT.py:57
dns.rdtypes.ANY.CERT.CERT.algorithm
algorithm
Definition: CERT.py:77
dns.rdtypes.ANY.CERT.CERT.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: CERT.py:88
dns.dnssec.algorithm_to_text
def algorithm_to_text(value)
Definition: dnssec.py:107
dns.rdtypes.ANY.CERT.CERT.certificate
certificate
Definition: CERT.py:78
dns.rdtypes.ANY.CERT.CERT.key_tag
key_tag
Definition: CERT.py:76
dns.dnssec
Definition: dnssec.py:1
dns.rdtypes.ANY.CERT._ctype_from_text
def _ctype_from_text(what)
Definition: CERT.py:43
dns.dnssec.algorithm_from_text
def algorithm_from_text(text)
Definition: dnssec.py:95
dns.rdtypes.ANY.CERT.CERT.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: CERT.py:107
dns.rdtypes.ANY.CERT.CERT.__init__
def __init__(self, rdclass, rdtype, certificate_type, key_tag, algorithm, certificate)
Definition: CERT.py:73
dns.rdata
Definition: rdata.py:1
dns.rdata._base64ify
def _base64ify(data, chunksize=_base64_chunksize)
Definition: rdata.py:53
dns.rdtypes.ANY.CERT.CERT.certificate_type
certificate_type
Definition: CERT.py:75
dns.rdtypes.ANY.CERT._ctype_to_text
def _ctype_to_text(what)
Definition: CERT.py:50
dns.tokenizer
Definition: tokenizer.py:1
dns.exception
Definition: exception.py:1
dns.rdata.Rdata
Definition: rdata.py:95