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

Source Code for Module dns.rdtypes.ANY.RRSIG

  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 calendar 
 17  import struct 
 18  import time 
 19   
 20  import dns.dnssec 
 21  import dns.exception 
 22  import dns.rdata 
 23  import dns.rdatatype 
 24   
25 -class BadSigTime(dns.exception.DNSException):
26 """Time in DNS SIG or RRSIG resource record cannot be parsed."""
27
28 -def sigtime_to_posixtime(what):
29 if len(what) != 14: 30 raise BadSigTime 31 year = int(what[0:4]) 32 month = int(what[4:6]) 33 day = int(what[6:8]) 34 hour = int(what[8:10]) 35 minute = int(what[10:12]) 36 second = int(what[12:14]) 37 return calendar.timegm((year, month, day, hour, minute, second, 38 0, 0, 0))
39
40 -def posixtime_to_sigtime(what):
41 return time.strftime('%Y%m%d%H%M%S', time.gmtime(what))
42
43 -class RRSIG(dns.rdata.Rdata):
44 """RRSIG record 45 46 @ivar type_covered: the rdata type this signature covers 47 @type type_covered: int 48 @ivar algorithm: the algorithm used for the sig 49 @type algorithm: int 50 @ivar labels: number of labels 51 @type labels: int 52 @ivar original_ttl: the original TTL 53 @type original_ttl: long 54 @ivar expiration: signature expiration time 55 @type expiration: long 56 @ivar inception: signature inception time 57 @type inception: long 58 @ivar key_tag: the key tag 59 @type key_tag: int 60 @ivar signer: the signer 61 @type signer: dns.name.Name object 62 @ivar signature: the signature 63 @type signature: string""" 64 65 __slots__ = ['type_covered', 'algorithm', 'labels', 'original_ttl', 66 'expiration', 'inception', 'key_tag', 'signer', 67 'signature'] 68
69 - def __init__(self, rdclass, rdtype, type_covered, algorithm, labels, 70 original_ttl, expiration, inception, key_tag, signer, 71 signature):
72 super(RRSIG, self).__init__(rdclass, rdtype) 73 self.type_covered = type_covered 74 self.algorithm = algorithm 75 self.labels = labels 76 self.original_ttl = original_ttl 77 self.expiration = expiration 78 self.inception = inception 79 self.key_tag = key_tag 80 self.signer = signer 81 self.signature = signature
82
83 - def covers(self):
84 return self.type_covered
85
86 - def to_text(self, origin=None, relativize=True, **kw):
87 return '%s %d %d %d %s %s %d %s %s' % ( 88 dns.rdatatype.to_text(self.type_covered), 89 self.algorithm, 90 self.labels, 91 self.original_ttl, 92 posixtime_to_sigtime(self.expiration), 93 posixtime_to_sigtime(self.inception), 94 self.key_tag, 95 self.signer.choose_relativity(origin, relativize), 96 dns.rdata._base64ify(self.signature) 97 )
98
99 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
100 type_covered = dns.rdatatype.from_text(tok.get_string()) 101 algorithm = dns.dnssec.algorithm_from_text(tok.get_string()) 102 labels = tok.get_int() 103 original_ttl = tok.get_ttl() 104 expiration = sigtime_to_posixtime(tok.get_string()) 105 inception = sigtime_to_posixtime(tok.get_string()) 106 key_tag = tok.get_int() 107 signer = tok.get_name() 108 signer = signer.choose_relativity(origin, relativize) 109 chunks = [] 110 while 1: 111 t = tok.get().unescape() 112 if t.is_eol_or_eof(): 113 break 114 if not t.is_identifier(): 115 raise dns.exception.SyntaxError 116 chunks.append(t.value) 117 b64 = ''.join(chunks) 118 signature = b64.decode('base64_codec') 119 return cls(rdclass, rdtype, type_covered, algorithm, labels, 120 original_ttl, expiration, inception, key_tag, signer, 121 signature)
122 123 from_text = classmethod(from_text) 124
125 - def to_wire(self, file, compress = None, origin = None):
126 header = struct.pack('!HBBIIIH', self.type_covered, 127 self.algorithm, self.labels, 128 self.original_ttl, self.expiration, 129 self.inception, self.key_tag) 130 file.write(header) 131 self.signer.to_wire(file, None, origin) 132 file.write(self.signature)
133
134 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
135 header = struct.unpack('!HBBIIIH', wire[current : current + 18]) 136 current += 18 137 rdlen -= 18 138 (signer, cused) = dns.name.from_wire(wire[: current + rdlen], current) 139 current += cused 140 rdlen -= cused 141 if not origin is None: 142 signer = signer.relativize(origin) 143 signature = wire[current : current + rdlen].unwrap() 144 return cls(rdclass, rdtype, header[0], header[1], header[2], 145 header[3], header[4], header[5], header[6], signer, 146 signature)
147 148 from_wire = classmethod(from_wire) 149
150 - def choose_relativity(self, origin = None, relativize = True):
152