Debian Codegraph @

dns/rdtypes/IN/IPSECKEY.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2006, 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 base64
19 import struct
20
21 import dns.exception
22 import dns.immutable
23 import dns.rdata
24 import dns.rdtypes.util
25
26
27 class Gateway(dns.rdtypes.util.Gateway):
28 name = "IPSECKEY gateway"
29
30
31 @dns.immutable.immutable
32 class IPSECKEY(dns.rdata.Rdata):
33 """IPSECKEY record"""
34
35 # see: RFC 4025
36
37 __slots__ = ["precedence", "gateway_type", "algorithm", "gateway", "key"]
38
39 def __init__(
40 self, rdclass, rdtype, precedence, gateway_type, algorithm, gateway, key
41 ):
42 super().__init__(rdclass, rdtype)
43 gateway = Gateway(gateway_type, gateway)
44 self.precedence = self._as_uint8(precedence)
45 self.gateway_type = gateway.type
46 self.algorithm = self._as_uint8(algorithm)
47 self.gateway = gateway.gateway
48 self.key = self._as_bytes(key)
49
50 def to_text(self, origin=None, relativize=True, **kw):
51 gateway = Gateway(self.gateway_type, self.gateway).to_text(origin, relativize)
52 key = dns.rdata._base64ify(self.key, **kw) # pyright: ignore
53 return f"{self.precedence} {self.gateway_type} {self.algorithm} {gateway} {key}"
54
55 @classmethod
56 def from_text(
57 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
58 ):
59 precedence = tok.get_uint8()
60 gateway_type = tok.get_uint8()
61 algorithm = tok.get_uint8()
62 gateway = Gateway.from_text(
63 gateway_type, tok, origin, relativize, relativize_to
64 )
65 b64 = tok.concatenate_remaining_identifiers().encode()
66 key = base64.b64decode(b64)
67 return cls(
68 rdclass, rdtype, precedence, gateway_type, algorithm, gateway.gateway, key
69 )
70
71 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
72 header = struct.pack("!BBB", self.precedence, self.gateway_type, self.algorithm)
73 file.write(header)
74 Gateway(self.gateway_type, self.gateway).to_wire(
75 file, compress, origin, canonicalize
76 )
77 file.write(self.key)
78
79 @classmethod
80 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
81 header = parser.get_struct("!BBB")
82 gateway_type = header[1]
83 gateway = Gateway.from_wire_parser(gateway_type, parser, origin)
84 key = parser.get_remaining()
85 return cls(
86 rdclass, rdtype, header[0], gateway_type, header[2], gateway.gateway, key
87 )