SnmpSharpNet.SnmpV3Packet.decode C# (CSharp) Method

decode() public method

Decode SNMP version 3 packet. This method will perform authentication check and decode privacy protected ScopedPdu. This method will not check for the timeliness of the packet, correct engine boot value or engine id because it does not have a reference to the engine time prior to this call.
public decode ( byte berBuffer, int length, byte authKey, byte privKey ) : int
berBuffer byte BER encoded SNMP version 3 packet buffer
length int Buffer length
authKey byte Authentication key (not password)
privKey byte Privacy key (not password)
return int
        public int decode(byte[] berBuffer, int length, byte[] authKey, byte[] privKey)
        {
            MutableByte buffer = new MutableByte(berBuffer, length);

            int offset = 0;

            // let base class parse first sequence and SNMP version number
            offset = base.decode(buffer, length);

            // check for correct SNMP protocol version
            if (_protocolVersion != (int)SnmpVersion.Ver3)
                throw new SnmpInvalidVersionException("Expecting SNMP version 3.");

            // now grab the global message data sequence header information
            int len = 0;
            byte asnType = AsnType.ParseHeader(buffer, ref offset, out len);
            if (asnType != SnmpConstants.SMI_SEQUENCE)
                throw new SnmpDecodingException("Invalid sequence type in global message data sequence.");

            // check that packet size can accommodate the length specified in the header
            if (len > (buffer.Length - offset))
                throw new OverflowException("Packet is too small to contain the data described in the header.");

            // retrieve message id
            offset = _messageId.decode(buffer, offset);

            // max message size
            offset = _maxMessageSize.decode(buffer, offset);

            // message flags
            offset = _msgFlags.decode(buffer, offset);

            // verify that a valid authentication/privacy configuration is present in the packet
            if (_msgFlags.Authentication == false && _msgFlags.Privacy == true)
                throw new SnmpException(SnmpException.UnsupportedNoAuthPriv, "SNMP version 3 noAuthPriv security combination is not supported.");

            // security model code
            offset = _securityModel.decode(buffer, offset);

            // we only support USM. code = 0x03
            if (_securityModel.Value != _userSecurityModel.Type)
                throw new SnmpException(SnmpException.UnsupportedSecurityModel, "Class only support SNMP Version 3 User Security Model.");

            // parse user security model
            offset = _userSecurityModel.decode(buffer, offset);

            // Authenticate message if authentication flag is set and packet is not a discovery packet
            if (_msgFlags.Authentication && _userSecurityModel.EngineId.Length > 0)
            {
                // Authenticate packet
                if (_userSecurityModel.AuthenticationParameters.Length != 12)
                    throw new SnmpAuthenticationException("Invalid authentication parameter field length.");
                if (!_userSecurityModel.IsAuthentic(authKey, buffer))
                    throw new SnmpAuthenticationException("Authentication of the incoming packet failed.");
            }

            // Decode ScopedPdu if it is privacy protected and packet is not a discovery packet
            if (_msgFlags.Privacy && _userSecurityModel.EngineId.Length > 0)
            {
                IPrivacyProtocol privacyProtocol = PrivacyProtocol.GetInstance(_userSecurityModel.Privacy);
                if (privacyProtocol == null)
                {
                    throw new SnmpException(SnmpException.UnsupportedPrivacyProtocol, "Privacy protocol requested is not supported.");
                }
                if (_userSecurityModel.PrivacyParameters.Length != privacyProtocol.PrivacyParametersLength)
                    throw new SnmpException(SnmpException.InvalidPrivacyParameterLength, "Invalid privacy parameters field length.");

                // Initialize a temporary OctetString class to hold encrypted ScopedPdu
                OctetString encryptedScopedPdu = new OctetString();
                offset = encryptedScopedPdu.decode(buffer, offset);

                // decode encrypted packet
                byte[] decryptedScopedPdu = privacyProtocol.Decrypt(encryptedScopedPdu, 0, encryptedScopedPdu.Length, privKey, _userSecurityModel.EngineBoots, _userSecurityModel.EngineTime, _userSecurityModel.PrivacyParameters);
                int tempOffset = 0;
                offset = _scopedPdu.decode(decryptedScopedPdu, tempOffset);
            }
            else
                offset = _scopedPdu.decode(buffer, offset);
            return offset;
        }

Same methods

SnmpV3Packet::decode ( byte berBuffer, int length ) : int

Usage Example

Example #1
0
        internal void AsyncResponse(AsyncRequestResult result, IPEndPoint peer, byte[] buffer, int buflen)
        {
            if (result != AsyncRequestResult.NoError)
            {
                _response(result, null);
            }
            else
            {
                if (buffer == null || buffer.Length <= 0 || buflen <= 0)
                {
                    _response(AsyncRequestResult.NoDataReceived, null);
                    return;
                }
                // verify packet
                if (_agentParameters.Version == (int)SnmpVersion.Ver1)
                {
                    SnmpV1Packet packet = new SnmpV1Packet();
                    try
                    {
                        packet.decode(buffer, buflen);
                    }
                    catch (Exception ex)
                    {
                        ex.GetType();
                        // Console.WriteLine("Exception while decoding SNMP packet: " + ex.ToString());
                        _response(AsyncRequestResult.DecodeError, packet);
                        return;
                    }
                    _response(AsyncRequestResult.NoError, packet);
                    return;
                }
                else if (_agentParameters.Version == SnmpVersion.Ver2)
                {
                    SnmpV2Packet packet = new SnmpV2Packet();
                    try
                    {
                        packet.decode(buffer, buflen);
                    }
                    catch (Exception ex)
                    {
                        ex.GetType();
                        // Console.WriteLine("Exception while decoding SNMP packet: " + ex.ToString());
                        // MutableByte b = new MutableByte(buffer, buflen);
                        // Console.WriteLine("Buffer length {0}", buflen);
                        // SnmpConstants.DumpHex(b);
                        _response(AsyncRequestResult.DecodeError, packet);
                        return;
                    }

                    _response(AsyncRequestResult.NoError, packet);
                }
                else if (_agentParameters.Version == SnmpVersion.Ver3)
                {
                    SnmpV3Packet          packet    = new SnmpV3Packet();
                    SecureAgentParameters secparams = (SecureAgentParameters)_agentParameters;
                    secparams.InitializePacket(packet);
                    try {
                        if (secparams.HasCachedKeys)
                        {
                            packet.decode(buffer, buflen, secparams.AuthenticationKey, secparams.PrivacyKey);
                        }
                        else
                        {
                            packet.decode(buffer, buflen);
                        }
                    }
                    catch
                    {
                        _response(AsyncRequestResult.DecodeError, packet);
                        return;
                    }
                    if (!secparams.ValidateIncomingPacket(packet))
                    {
                        _response(AsyncRequestResult.AuthenticationError, packet);
                    }
                    else
                    {
                        secparams.UpdateDiscoveryValues(packet);                         // update time, etc. values
                        _response(AsyncRequestResult.NoError, packet);
                    }
                }
            }
        }
All Usage Examples Of SnmpSharpNet.SnmpV3Packet::decode