Exemplo n.º 1
0
        /// <summary>
        /// Decode USM portion of the SNMP version 3 packet.
        /// </summary>
        /// <param name="buffer">Received SNMP packet BER encoded</param>
        /// <param name="offset">Offset within the buffer to start decoding USM information</param>
        /// <returns>Buffer position after the decoded value</returns>
        /// <exception cref="SnmpDecodingException">Thrown when decoding enountered invalid data type in USM information</exception>
        /// <exception cref="OverflowException">Thrown when packet is too small to contain information length specified in header</exception>
        public override int Decode(byte[] buffer, int offset)
        {
            // Grab the octet string header
            byte type = ParseHeader(buffer, ref offset, out int len);

            if (type != OCTETSTRING)
            {
                throw new SnmpDecodingException("Invalid value type found while looking for USM header.");
            }

            if (len > (buffer.Length - offset))
            {
                throw new OverflowException("Packet too small");
            }
            // Now grab the sequence header
            type = ParseHeader(buffer, ref offset, out len);
            if (type != SnmpConstants.SMI_SEQUENCE)
            {
                throw new SnmpDecodingException("Sequence missing from USM header.");
            }

            if (len > (buffer.Length - offset))
            {
                throw new OverflowException("Packet too small");
            }

            // now grab values one at the time
            offset = _engineId.Decode(buffer, offset);

            offset = _engineBoots.Decode(buffer, offset);

            offset = _engineTime.Decode(buffer, offset);

            offset = _securityName.Decode(buffer, offset);

            int saveOffset = offset;

            offset = AuthenticationParameters.Decode(buffer, offset);

            if (AuthenticationParameters.Length > 0)
            {
                // walk through and set the authentication parameters to 0x00 in the packet

                saveOffset += 2; // Skip BER encoded variable type and length
                for (int i = 0; i < AuthenticationParameters.Length; i++)
                {
                    buffer[saveOffset + i] = 0x00;
                }
            }
            offset = _privacyParameters.Decode(buffer, offset);
            return(offset);
        }