1# Copyright (C) 2005-2007, 2009, 2010 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 16import struct 17 18import dns.rdata 19import dns.rdatatype 20 21class SSHFP(dns.rdata.Rdata): 22 """SSHFP record 23 24 @ivar algorithm: the algorithm 25 @type algorithm: int 26 @ivar fp_type: the digest type 27 @type fp_type: int 28 @ivar fingerprint: the fingerprint 29 @type fingerprint: string 30 @see: draft-ietf-secsh-dns-05.txt""" 31 32 __slots__ = ['algorithm', 'fp_type', 'fingerprint'] 33 34 def __init__(self, rdclass, rdtype, algorithm, fp_type, 35 fingerprint): 36 super(SSHFP, self).__init__(rdclass, rdtype) 37 self.algorithm = algorithm 38 self.fp_type = fp_type 39 self.fingerprint = fingerprint 40 41 def to_text(self, origin=None, relativize=True, **kw): 42 return '%d %d %s' % (self.algorithm, 43 self.fp_type, 44 dns.rdata._hexify(self.fingerprint, 45 chunksize=128)) 46 47 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True): 48 algorithm = tok.get_uint8() 49 fp_type = tok.get_uint8() 50 fingerprint = tok.get_string() 51 fingerprint = fingerprint.decode('hex_codec') 52 tok.get_eol() 53 return cls(rdclass, rdtype, algorithm, fp_type, fingerprint) 54 55 from_text = classmethod(from_text) 56 57 def to_wire(self, file, compress = None, origin = None): 58 header = struct.pack("!BB", self.algorithm, self.fp_type) 59 file.write(header) 60 file.write(self.fingerprint) 61 62 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None): 63 header = struct.unpack("!BB", wire[current : current + 2]) 64 current += 2 65 rdlen -= 2 66 fingerprint = wire[current : current + rdlen] 67 return cls(rdclass, rdtype, header[0], header[1], fingerprint) 68 69 from_wire = classmethod(from_wire) 70 71 def _cmp(self, other): 72 hs = struct.pack("!BB", self.algorithm, self.fp_type) 73 ho = struct.pack("!BB", other.algorithm, other.fp_type) 74 v = cmp(hs, ho) 75 if v == 0: 76 v = cmp(self.fingerprint, other.fingerprint) 77 return v 78