1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3# Copyright (C) 2016 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
18import base64
19
20import dns.exception
21import dns.rdata
22import dns.tokenizer
23
24class OPENPGPKEY(dns.rdata.Rdata):
25
26    """OPENPGPKEY record"""
27
28    # see: RFC 7929
29
30    def __init__(self, rdclass, rdtype, key):
31        super().__init__(rdclass, rdtype)
32        object.__setattr__(self, 'key', key)
33
34    def to_text(self, origin=None, relativize=True, **kw):
35        return dns.rdata._base64ify(self.key)
36
37    @classmethod
38    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True,
39                  relativize_to=None):
40        b64 = tok.concatenate_remaining_identifiers().encode()
41        key = base64.b64decode(b64)
42        return cls(rdclass, rdtype, key)
43
44    def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
45        file.write(self.key)
46
47    @classmethod
48    def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
49        key = parser.get_remaining()
50        return cls(rdclass, rdtype, key)
51