1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import base64
17 import calendar
18 import struct
19 import time
20
21 import dns.dnssec
22 import dns.exception
23 import dns.rdata
24 import dns.rdatatype
25 import dns.util
28 """Raised when a SIG or RRSIG RR's time cannot be parsed."""
29 pass
30
32 if len(what) != 14:
33 raise BadSigTime
34 year = int(what[0:4])
35 month = int(what[4:6])
36 day = int(what[6:8])
37 hour = int(what[8:10])
38 minute = int(what[10:12])
39 second = int(what[12:14])
40 return calendar.timegm((year, month, day, hour, minute, second,
41 0, 0, 0))
42
44 return time.strftime('%Y%m%d%H%M%S', time.gmtime(what))
45
46 -class RRSIG(dns.rdata.Rdata):
47 """RRSIG record
48
49 @ivar type_covered: the rdata type this signature covers
50 @type type_covered: int
51 @ivar algorithm: the algorithm used for the sig
52 @type algorithm: int
53 @ivar labels: number of labels
54 @type labels: int
55 @ivar original_ttl: the original TTL
56 @type original_ttl: long
57 @ivar expiration: signature expiration time
58 @type expiration: long
59 @ivar inception: signature inception time
60 @type inception: long
61 @ivar key_tag: the key tag
62 @type key_tag: int
63 @ivar signer: the signer
64 @type signer: dns.name.Name object
65 @ivar signature: the signature
66 @type signature: string"""
67
68 __slots__ = ['type_covered', 'algorithm', 'labels', 'original_ttl',
69 'expiration', 'inception', 'key_tag', 'signer',
70 'signature']
71
72 - def __init__(self, rdclass, rdtype, type_covered, algorithm, labels,
73 original_ttl, expiration, inception, key_tag, signer,
74 signature):
85
87 return self.type_covered
88
89 - def to_text(self, origin=None, relativize=True, **kw):
90 return '%s %d %d %d %s %s %d %s %s' % (
91 dns.rdatatype.to_text(self.type_covered),
92 self.algorithm,
93 self.labels,
94 self.original_ttl,
95 posixtime_to_sigtime(self.expiration),
96 posixtime_to_sigtime(self.inception),
97 self.key_tag,
98 self.signer,
99 dns.rdata._base64ify(self.signature)
100 )
101
102 @classmethod
103 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
104 type_covered = dns.rdatatype.from_text(tok.get_string())
105 algorithm = dns.dnssec.algorithm_from_text(tok.get_string())
106 labels = tok.get_int()
107 original_ttl = tok.get_ttl()
108 expiration = sigtime_to_posixtime(tok.get_string())
109 inception = sigtime_to_posixtime(tok.get_string())
110 key_tag = tok.get_int()
111 signer = tok.get_name()
112 signer = signer.choose_relativity(origin, relativize)
113 chunks = []
114 while 1:
115 t = tok.get().unescape()
116 if t.is_eol_or_eof():
117 break
118 if not t.is_identifier():
119 raise dns.exception.SyntaxError
120 chunks.append(t.value)
121 b64 = ''.join(chunks)
122 signature = base64.b64decode(b64.encode('ascii'))
123 return cls(rdclass, rdtype, type_covered, algorithm, labels,
124 original_ttl, expiration, inception, key_tag, signer,
125 signature)
126
127 - def to_wire(self, file, compress = None, origin = None):
128 header = struct.pack('!HBBIIIH', self.type_covered,
129 self.algorithm, self.labels,
130 self.original_ttl, self.expiration,
131 self.inception, self.key_tag)
132 file.write(header)
133 self.signer.to_wire(file, None, origin)
134 file.write(self.signature)
135
136 @classmethod
137 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
138 header = struct.unpack('!HBBIIIH', wire[current : current + 18])
139 current += 18
140 rdlen -= 18
141 (signer, cused) = dns.name.from_wire(wire[: current + rdlen], current)
142 current += cused
143 rdlen -= cused
144 if not origin is None:
145 signer = signer.relativize(origin)
146 signature = wire[current : current + rdlen].unwrap()
147 return cls(rdclass, rdtype, header[0], header[1], header[2],
148 header[3], header[4], header[5], header[6], signer,
149 signature)
150
153
154 - def _cmp(self, other):
155 hs = struct.pack('!HBBIIIH', self.type_covered,
156 self.algorithm, self.labels,
157 self.original_ttl, self.expiration,
158 self.inception, self.key_tag)
159 ho = struct.pack('!HBBIIIH', other.type_covered,
160 other.algorithm, other.labels,
161 other.original_ttl, other.expiration,
162 other.inception, other.key_tag)
163 v = dns.util.cmp(hs, ho)
164 if v == 0:
165 v = dns.util.cmp(self.signer, other.signer)
166 if v == 0:
167 v = dns.util.cmp(self.signature, other.signature)
168 return v
169