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

Source Code for Module dns.rdtypes.ANY.CERT

  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 57 -class CERT(dns.rdata.Rdata):
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):
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, 84 dns.dnssec.algorithm_to_text(self.algorithm), 85 dns.rdata._base64ify(self.certificate))
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(): 100 raise dns.exception.SyntaxError 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: 119 raise dns.exception.FormError 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)
124