~ubuntu-branches/ubuntu/saucy/dnspython/saucy-proposed

« back to all changes in this revision

Viewing changes to dns/rdtypes/ANY/TLSA.py

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2013-07-17 02:49:46 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130717024946-g4j9400pq90uhgia
Tags: 1.11.0-1
* New upstream release
* Agreed maintainer switch to DPMT
  - Add Vcs-* fields to debian/control
* Bump minimum debhelper version to 8.1 and compat to 8 for build-arch/indep
  support
* Bump standards version to 3.9.4 without further change

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2007, 2009-2011 Nominum, Inc.
 
2
#
 
3
# Permission to use, copy, modify, and distribute this software and its
 
4
# documentation for any purpose with or without fee is hereby granted,
 
5
# provided that the above copyright notice and this permission notice
 
6
# appear in all copies.
 
7
#
 
8
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
 
9
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
10
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
 
11
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
12
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
13
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 
14
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
15
 
 
16
import struct
 
17
 
 
18
import dns.rdata
 
19
import dns.rdatatype
 
20
 
 
21
class TLSA(dns.rdata.Rdata):
 
22
    """TLSA record
 
23
 
 
24
    @ivar usage: The certificate usage
 
25
    @type usage: int
 
26
    @ivar selector: The selector field
 
27
    @type selector: int
 
28
    @ivar mtype: The 'matching type' field
 
29
    @type mtype: int
 
30
    @ivar cert: The 'Certificate Association Data' field
 
31
    @type cert: string
 
32
    @see: RFC 6698"""
 
33
 
 
34
    __slots__ = ['usage', 'selector', 'mtype', 'cert']
 
35
 
 
36
    def __init__(self, rdclass, rdtype, usage, selector,
 
37
                 mtype, cert):
 
38
        super(TLSA, self).__init__(rdclass, rdtype)
 
39
        self.usage = usage
 
40
        self.selector = selector
 
41
        self.mtype = mtype
 
42
        self.cert = cert
 
43
 
 
44
    def to_text(self, origin=None, relativize=True, **kw):
 
45
        return '%d %d %d %s' % (self.usage,
 
46
                                self.selector,
 
47
                                self.mtype,
 
48
                                dns.rdata._hexify(self.cert,
 
49
                                               chunksize=128))
 
50
 
 
51
    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
 
52
        usage = tok.get_uint8()
 
53
        selector = tok.get_uint8()
 
54
        mtype = tok.get_uint8()
 
55
        cert_chunks = []
 
56
        while 1:
 
57
            t = tok.get().unescape()
 
58
            if t.is_eol_or_eof():
 
59
                break
 
60
            if not t.is_identifier():
 
61
                raise dns.exception.SyntaxError
 
62
            cert_chunks.append(t.value)
 
63
        cert = ''.join(cert_chunks)
 
64
        cert = cert.decode('hex_codec')
 
65
        return cls(rdclass, rdtype, usage, selector, mtype, cert)
 
66
 
 
67
    from_text = classmethod(from_text)
 
68
 
 
69
    def to_wire(self, file, compress = None, origin = None):
 
70
        header = struct.pack("!BBB", self.usage, self.selector, self.mtype)
 
71
        file.write(header)
 
72
        file.write(self.cert)
 
73
 
 
74
    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
 
75
        header = struct.unpack("!BBB", wire[current : current + 3])
 
76
        current += 3
 
77
        rdlen -= 3
 
78
        cert = wire[current : current + rdlen].unwrap()
 
79
        return cls(rdclass, rdtype, header[0], header[1], header[2], cert)
 
80
 
 
81
    from_wire = classmethod(from_wire)
 
82
 
 
83
    def _cmp(self, other):
 
84
        hs = struct.pack("!BBB", self.usage, self.selector, self.mtype)
 
85
        ho = struct.pack("!BBB", other.usage, other.selector, other.mtype)
 
86
        v = cmp(hs, ho)
 
87
        if v == 0:
 
88
            v = cmp(self.cert, other.cert)
 
89
        return v