| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2004-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 enum |
| 20 |
import struct |
| 21 |
|
| 22 |
import dns.dnssectypes |
| 23 |
import dns.exception |
| 24 |
import dns.immutable |
| 25 |
import dns.rdata |
| 26 |
|
| 27 |
# wildcard import |
| 28 |
__all__ = ["SEP", "REVOKE", "ZONE"] # noqa: F822 |
| 29 |
|
| 30 |
|
| 31 |
class Flag(enum.IntFlag): |
| 32 |
SEP = 0x0001 |
| 33 |
REVOKE = 0x0080 |
| 34 |
ZONE = 0x0100 |
| 35 |
|
| 36 |
|
| 37 |
@dns.immutable.immutable |
| 38 |
class DNSKEYBase(dns.rdata.Rdata): |
| 39 |
"""Base class for rdata that is like a DNSKEY record""" |
| 40 |
|
| 41 |
__slots__ = ["flags", "protocol", "algorithm", "key"] |
| 42 |
|
| 43 |
def __init__(self, rdclass, rdtype, flags, protocol, algorithm, key): |
| 44 |
super().__init__(rdclass, rdtype) |
| 45 |
self.flags = Flag(self._as_uint16(flags)) |
| 46 |
self.protocol = self._as_uint8(protocol) |
| 47 |
self.algorithm = dns.dnssectypes.Algorithm.make(algorithm) |
| 48 |
self.key = self._as_bytes(key) |
| 49 |
|
| 50 |
def to_text(self, origin=None, relativize=True, **kw): |
| 51 |
key = dns.rdata._base64ify(self.key, **kw) # pyright: ignore |
| 52 |
return f"{self.flags} {self.protocol} {self.algorithm} {key}" |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def from_text( |
| 56 |
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None |
| 57 |
): |
| 58 |
flags = tok.get_uint16() |
| 59 |
protocol = tok.get_uint8() |
| 60 |
algorithm = tok.get_string() |
| 61 |
b64 = tok.concatenate_remaining_identifiers().encode() |
| 62 |
key = base64.b64decode(b64) |
| 63 |
return cls(rdclass, rdtype, flags, protocol, algorithm, key) |
| 64 |
|
| 65 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 66 |
header = struct.pack("!HBB", self.flags, self.protocol, self.algorithm) |
| 67 |
file.write(header) |
| 68 |
file.write(self.key) |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 72 |
header = parser.get_struct("!HBB") |
| 73 |
key = parser.get_remaining() |
| 74 |
return cls(rdclass, rdtype, header[0], header[1], header[2], key) |
| 75 |
|
| 76 |
|
| 77 |
### BEGIN generated Flag constants |
| 78 |
|
| 79 |
SEP = Flag.SEP |
| 80 |
REVOKE = Flag.REVOKE |
| 81 |
ZONE = Flag.ZONE |
| 82 |
|
| 83 |
### END generated Flag constants |