Example #1
0
File: v2c.py Project: tsondt/Canvas
    def _decode(self, input):
        """
           _decode(input) -> (value, rest)

           Decode SNMP PDU (string), return PDU type (integer), request
           serial ID (integer), error status (integer), error index (integer)
           and variables bindings (string).

           See RFC 1157 for details.
        """
        # Decode PDU
        tag = self.decode_tag(ord(input[0]))
        (pdu, rest) = eval(tag + '()').decode(input)
        self['tag'] = tag[:-4]

        # Get request ID from PDU
        (self['request_id'], pdu) = asn1.INTEGER().decode(pdu)

        # Get non repeaters and max repetitions from PDU
        for key in ('non_repeaters', 'max_repetitions'):
            (self[key], pdu) = asn1.INTEGER().decode(pdu)

        # Get variables bindings
        self['bindings'] = pdu

        return rest
Example #2
0
 def packASN1(self):
     # yuck what a horrid library!
     dssParms = ''.join([asn1.INTEGER(x).encode()
                          for x in [self.dsa.p,
                                    self.dsa.q,
                                    self.dsa.g]])
     keyInfo = ''.join([asn1.SEQUENCE(''.join([idDSA.encode(),
                                                asn1.SEQUENCE(dssParms).encode()])).encode(),
                         asn1.INTEGER(self.dsa.y).encode()])
     return asn1.SEQUENCE(keyInfo).encode()
Example #3
0
File: v2c.py Project: tsondt/Canvas
    def _encode(self):
        """
            _encode() -> octet stream

           Encode non-repeaters and max-repetitions counters (integers)
           along with variables bindings (string) into SNMP V2 bulk PDU.
        """
        return eval(self['tag']+'_PDU')(\
               asn1.INTEGER(self['request_id']).encode() + \
               asn1.INTEGER(self['non_repeaters']).encode() + \
               asn1.INTEGER(self['max_repetitions']).encode() + \
               self['bindings']).encode()
Example #4
0
File: v2c.py Project: tsondt/Canvas
    def _encode(self):
        """
            _encode() -> octet stream

           Encode PDU type (string), request ID (integer), error status and
           index (integers) alone with variables bindings (string) into
           SNMP PDU.
        """
        return eval(self['tag']+'_PDU')(\
               asn1.INTEGER(self['request_id']).encode() + \
               asn1.INTEGER(self['error_status']).encode() + \
               asn1.INTEGER(self['error_index']).encode() + \
               self['bindings']).encode()
Example #5
0
    def signASN1(self, str):
        t = chr((len(long_to_bytes(self.dsa.p))/64)-1)
        r, s = self.dsa.sign(bytes_to_long(sha.new(str).digest()),
                             HIPutils.RandomPool.get_bytes(
            len(long_to_bytes(self.dsa.q))-1))
##        print 'HI.sign'
##        print binascii.hexlify(long_to_bytes(r)), binascii.hexlify(long_to_bytes(s))
##        print binascii.hexlify(sha.new(str).digest())
##        print repr(self.dsa.__dict__)
##        print self.dsa.size()
        #print len(r), binascii.hexlify(r), len(s), binascii.hexlify(s)
        sigInfo = ''.join([asn1.INTEGER().encode(r),
                           asn1.INTEGER().encode(s)])
        sig = asn1.SEQUENCE(sigInfo).encode()
##        print repr(sig)
        return sig
Example #6
0
    def _decode(self, input):
        """
           _decode(input) -> (value, rest)

           Decode SNMP trap PDU (string) to enterpise Object-ID (list of
           integer sub-IDs), agent IP address (string), generic trap type
           (integer), specific trap type (integer), timeticks (integer) and
           variable bindings (string).

           See RFC-1157 for details.
        """
        # Decode PDU
        tag = self.decode_tag(ord(input[0]))
        (pdu, rest) = eval(tag + '()').decode(input)
        self['tag'] = tag[:-4]

        # Get enterprise Object ID
        (self['enterprise'], pdu) = asn1.OBJECTID().decode(pdu)

        # Get agent address
        (self['agent_addr'], pdu) = asn1.IPADDRESS().decode(pdu)

        # Get generic and specific traps
        for key in ('generic_trap', 'specific_trap'):
            (self[key], pdu) = asn1.INTEGER().decode(pdu)

        # Get time stamp
        (self['time_stamp'], pdu) = asn1.TIMETICKS().decode(pdu)

        # Get variables bindings
        self['bindings'] = pdu

        return rest
Example #7
0
    def _encode(self):
        """
           _encode() -> octet stream

           Encode enterpise Object-ID (given as a list of integer subIDs),
           agent IP address (string), generic trap type (integer), specific
           trap type (integer), timeticks (integer) and variable bindings
           (string) into SNMP Trap-PDU (see RFC-1157 for details)
        """
        return eval(self['tag']+'_PDU')(\
               asn1.OBJECTID(self.value['enterprise']).encode() + \
               asn1.IPADDRESS(self['agent_addr']).encode() + \
               asn1.INTEGER(self['generic_trap']).encode() + \
               asn1.INTEGER(self['specific_trap']).encode() + \
               asn1.TIMETICKS(self['time_stamp']).encode() + \
               self['bindings']).encode()
Example #8
0
    def _encode(self):
        """
           _encode() -> octet stream

           Encode SNMP version, community name and PDU into SNMP message.
        """
        return asn1.SEQUENCE( \
               asn1.INTEGER(self['version']).encode() + \
               asn1.OCTETSTRING(self['community']).encode() + \
               self['pdu']).encode()
Example #9
0
    def _decode(self, input):
        """
           _decode(input) -> (value, rest)

           Parse SNMP message (string), return SNMP protocol version used
           (integer), community name (string) and SNMP PDU (string).
        """
        # Unpack message
        (message, rest) = asn1.SEQUENCE().decode(input)

        # Get SNMP version
        (self['version'], message) = asn1.INTEGER().decode(message)

        # Get SNMP community name
        (self['community'], self['pdu']) = \
                            asn1.OCTETSTRING().decode(message)
        return rest
Example #10
0
    def decode_header(self, input):
        """
           _decode(input) -> (value, rest)

           Parse SNMP message (string), return SNMP protocol version used
           (integer) and community name (string).
        """
        # Unpack message
        (message, rest) = asn1.SEQUENCE().decode(input)

        # Get SNMP version
        (self['version'], message) = asn1.INTEGER().decode(message)

        # Get SNMP community name
        (self['community'], pdu) = \
                            asn1.OCTETSTRING().decode(message)
        if pdu:
            return (pdu, rest)
        else:
            return (None, rest)