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