protected override void UpgradeConfirm() { baseDamage = baseDamage * USM.GetDmgRateData()[0]; damage = baseDamage; baseFireRate = baseFireRate * USM.GetAtkSpdRateData()[0]; fireRate = baseFireRate; }
public IActionResult USP(USPM US) { if (US.Name == null) { US.Name = "Unknown"; } if (US.Restaurant_Name == null) { return(View()); } if (US.Favorite_Dish == null) { US.Favorite_Dish = "........ you cant pick just one thing! It's all tasty!"; } if (US.Phone == null) { US.Phone = "Coming Soon"; } USM.AddUS(US); return(View("US", USM.User_Suggestions)); }
/// <summary> /// "Look-ahead" decode of SNMP packet header including USM information /// </summary> /// <remarks> /// Decode first component of the SNMP version 3 packet allowing the caller to retrieve USM SecureName needed to retrieve /// client security parameters that will allow authentication and privacy decryption to take place. /// /// This method is used to support Agent like behavior or to handle unsolicited packets like TRAP and INFORMs. In all of /// these cases, sender of packets will forward a packet without a request being sent by you. In turn, you will need /// to parse enough of the packet to retrieve SecureName which you can use to retrieve security parameters associated with /// that user and attempt to authorize and privacy decrypt the received packet. /// /// Only use this method when your application is acting as an Agent or if you need to process TRAP and INFORM packets. /// </remarks> /// <param name="berBuffer">Raw SNMP version 3 packet</param> /// <param name="length">SNMP version 3 packet length</param> /// <returns>UserSecurityModel class parsed from the parameter SNMP version 3 packet</returns> /// <exception cref="SnmpInvalidVersionException">Thrown when attempting to parse an SNMP packet that is not version 3</exception> /// <exception cref="OverflowException">Thrown when header specifies packet length that is longer then the amount of data received.</exception> /// <exception cref="SnmpDecodingException">Thrown when invalid sequence is enountered while decoding global message data sequence</exception> /// <exception cref="SnmpException">Thrown with SnmpException.UnsupportedNoAuthPriv when packet is using privacy without authentication (not allowed)</exception> /// <exception cref="SnmpException">Thrown with SnmpException.UnsupportedSecurityModel when packet is sent with security model other then USM (only USM is defined in SNMPv3 standard)</exception> public UserSecurityModel GetUSM(byte[] berBuffer, int length) { 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 byte asnType = AsnType.ParseHeader(buffer, ref offset, out int len); if (asnType != SnmpConstants.SMI_SEQUENCE) { throw new SnmpDecodingException("Invalid sequence type when decoding 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 != USM.Type) { throw new SnmpException(SnmpException.UnsupportedSecurityModel, "Class only support SNMP Version 3 User Security Model."); } // parse user security model offset = USM.Decode(buffer, offset); return(USM); }
public static void Main(string[] args) { #if REQUIRES_CRT_INIT CRT.Auto.Initialize(); #endif if (args.Length < 2) { Console.WriteLine("SNMP++.NET command line utility\n" + "Author: Marek Malowidzki 2003,2004 ([email protected])\n" + "Based on SNMP++ package from Peter E. Mellquist (HP) and Jochen Katz\n" + "SNMP++.NET " + Assembly.GetAssembly(typeof(Snmp)) + " built on " + Snmp.BuildTime + "; SNMP++ v. " + Snmp.Snmp_ppVersion + "\n\n" + "Usage: " + Environment.GetCommandLineArgs()[0] + " <op> <agent> [<options>]\n" + "where:\n" + "op - SNMP operation to perform (get,getnext,getbulk,set,walk,table)\n" + "agent - IP address or DNS name of an agent\n" + "options are the following:\n" + "-v<version> - SNMP version to use (v1(default),v2c,v3)\n" + "-p<port> - port number to use\n" + "-r<retries> - number of retries (default: 2)\n" + "-t<timeout> - timeout in milliseconds (default: 1000)\n" + "-d<bool> - print debug messages\n" + "-o<oid> - subsequent OID\n" + "-T<type> - subsequent SNMP type name:\n" + string.Join(",", SnmpSyntax.SupportedSyntaxNames) + " (o stands for oid and s for string)\n" + "-V<value> - subsequent value\n" + "Debug options:\n" + "-Df<file> - log file name\n" + "-Dl<level> - log level\n" + "SNMPv1/v2c options:\n" + "-c<comm> - read community name (default: public)\n" + "-C<comm> - write community name (default: public)\n" + "SNMPv3 options:\n" + "-b<boot> - boot counter value (default: 100)\n" + "-sn<secName> - security name\n" + "-sl<secLevel>- security level (" + ManagerUtilities.EnumInfo(typeof(SecurityLevel), SecurityLevel.AuthPriv) + ")\n" + "-sm<secModel>- security model (" + ManagerUtilities.EnumInfo(typeof(SecurityModel), SecurityModel.USM) + ")\n" + "-xn<ctxName> - context name\n" + "-xe<ctxEngId>- context engine ID (default: discover)\n" + "-A<authProto>- authentication protocol (" + ManagerUtilities.EnumInfo(typeof(AuthProtocol), AuthProtocol.None) + ")\n" + "-P<privProto>- privacy protocol (" + ManagerUtilities.EnumInfo(typeof(PrivProtocol), PrivProtocol.None) + ")\n" + "-Ua<authPass>- authentication password\n" + "-Up<privPass>- privacy password\n" + "Table operation options:\n" + "-Os<rowOid> - row index to start from\n" + "-Oe<rowOid> - row index to finish at\n" + "-On<count> - max number of rows to retrieve\n" + "-Or<count> - rows per query or 0 for heuristics\n" + "Testing options:\n" + "-Xa<bool> - use asynchronous interface\n" + "-Xm<filePfx> - collect memory usage in <filePfx>.<ext> files, where ext denotes memory type\n" + "-Xn<repeat> - repeat number of times (default: 1)\n" + "-Xp<priority>- set process priority (" + ManagerUtilities.EnumInfo(typeof(ProcessPriorityClass), ProcessPriorityClass.Normal) + ")\n" + "-Xs<bool> - use asychronous interface for synchronous calls\n" + "-Xt<threads> - run multiple threads (default: 1)"); Environment.Exit(1); } string option = "<ip>"; Thread statsCollector = null; MemoryStats stats = null; int retCode = 1; try { UdpAddress udp = new UdpAddress(args[1]); SnmpVersion ver = SnmpVersion.SNMPv1; SnmpTarget.DefaultRetries = 2; SnmpTarget.DefaultTimeout = 1000; uint nRepeats = 1, nThreads = 1; int debugLevel = int.MinValue; string statsFile = null, debugFile = null; TableReader.GetTableOptions tableOptions = new TableReader.GetTableOptions(); bool async = false, asyncSync = false, debug = false; string readCommunity = "public", writeCommunity = "public"; AuthProtocol authProto = AuthProtocol.None; PrivProtocol privProto = PrivProtocol.None; string authPass = "", privPass = "", secName = "", ctxName = "", ctxEngId = ""; SecurityLevel secLevel = SecurityLevel.AuthPriv; SecurityModel secModel = SecurityModel.USM; uint boot = 100; string[] oids = new string[128], types = new string[128], vals = new string[128]; int noids = 0, ntypes = 0, nvals = 0; string val; int index = 2; while (ManagerUtilities.GetOption(args, out option, out val, ref index)) { char sub; switch (option) { case "v": ver = (SnmpVersion)Enum.Parse( typeof(SnmpVersion), "SNMPv" + val, true); break; case "p": udp = new UdpAddress(udp.Ip, int.Parse(val)); break; case "r": SnmpTarget.DefaultRetries = int.Parse(val); break; case "t": SnmpTarget.DefaultTimeout = int.Parse(val); break; case "d": debug = bool.Parse(val); break; case "o": oids[noids++] = val; break; case "T": types[ntypes++] = val; break; case "V": vals[nvals++] = val; break; case "c": readCommunity = val; break; case "C": writeCommunity = val; break; case "b": boot = uint.Parse(val); break; case "s": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'n': secName = val; break; case 'l': secLevel = (SecurityLevel)Enum.Parse( typeof(SecurityLevel), val, true); break; case 'm': secModel = (SecurityModel)Enum.Parse( typeof(SecurityModel), val, true); break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; case "x": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'n': ctxName = val; break; case 'e': ctxEngId = val; break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; case "A": authProto = (AuthProtocol)Enum.Parse( typeof(AuthProtocol), val, true); break; case "P": privProto = (PrivProtocol)Enum.Parse( typeof(PrivProtocol), val, true); break; case "D": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'f': debugFile = val; break; case 'l': debugLevel = int.Parse(val); break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; case "U": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'a': authPass = val; break; case 'p': privPass = val; break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; case "O": { switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 's': tableOptions.startRowIndex = new Oid(val); break; case 'e': tableOptions.endRowIndex = new Oid(val); break; case 'n': tableOptions.maxRows = int.Parse(val); break; case 'r': tableOptions.rowsPerQuery = int.Parse(val); break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; } case "X": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'a': async = ParseBoolWithDefault(val); break; case 'm': statsFile = val; break; case 'n': nRepeats = uint.Parse(val); break; case 'p': System.Diagnostics.Process.GetCurrentProcess().PriorityClass = (ProcessPriorityClass)Enum.Parse( typeof(ProcessPriorityClass), val, true); break; case 's': asyncSync = ParseBoolWithDefault(val); break; case 't': if ((nThreads = uint.Parse(val)) <= 0) { throw new ArgumentException( val + ": invalid threads number"); } break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; default: throw new ArgumentException( "-" + option + ": invalid option"); } } if (noids == 0) { option = "<OIDs>"; throw new ArgumentException( "No OIDs specified, use -o option"); } bool asyncMode = async || asyncSync; // Debug options if (debugFile != null) { Snmp.DebugLogFile = debugFile; } if (debugLevel != int.MinValue) { Snmp.DebugLogLevel = debugLevel; } // Operation type processing option = "<Pdu creation>"; PduType pduType; OperType operType; GetOperType(args[0].ToLower(), out pduType, out operType); // Adjusting settings for a table operation if (operType == OperType.Table) { TableReader.UseAsyncInvoke = asyncMode; } // Pdu creation Pdu pdu; using (IMemoryManager mgr = MemoryManager.GetMemoryManager()) { Vb[] vbs = ManagerUtilities.CreateVbs(pduType, oids, noids, types, ntypes, vals, nvals); pdu = new Pdu(pduType, vbs); mgr.Remove(pdu); // remove Pdu from the memory manager } // SnmpTarget creation SnmpTarget target; if (ver == SnmpVersion.SNMPv3) { option = "<SNMPv3 initialization>"; V3MP.Init(new OctetStr("SNMP++.NET"), boot); USM usm = V3MP.Instance.Usm; usm.AddUsmUser(secName, authProto, privProto, authPass, privPass); target = new UTarget(udp, secName, secModel); pdu.SecurityLevel = secLevel; pdu.ContextName = new OctetStr(ctxName); pdu.ContextEngineId = new OctetStr(ctxEngId); } else { option = "<SNMPv1/v2c initialization>"; target = new CTarget(udp, ver, readCommunity, writeCommunity); } udp = null; // Memory usage statistics initialization option = "<Statistics collector initialization>"; if (statsFile != null) { stats = new MemoryStats(statsFile); statsCollector = new Thread(new ThreadStart(stats.Collect)); statsCollector.Priority = ThreadPriority.BelowNormal; statsCollector.Start(); } // Snmp session initialization & further processing option = "<SNMP session initialization>"; using (Snmp snmp = new Snmp(asyncMode)) { Thread.CurrentThread.Name = "0"; Barrier barrier; Delegate fun; if (async) { fun = new Delegate(AsyncProcess); barrier = new Barrier(nThreads + 1); } else { fun = new Delegate(Process); barrier = new Barrier(nThreads); } Manager mgr = new Manager(0, snmp, target, pdu, operType, ref tableOptions, barrier, nRepeats, asyncSync, debug); DateTime start = DateTime.Now; int ncalls = fun(mgr, ref option); double msec = DateTime.Now.Subtract(start).TotalMilliseconds; // clear references on stack pdu = null; target = null; mgr = null; Console.WriteLine("{0} {1} SNMP request(s) in {2} msec. ({3} req./sec.)", ncalls, asyncMode ? "asynchronous" : "synchronous", (int)msec, msec > 0 ? (1000 * (long)ncalls / msec) : 0); } retCode = 0; } catch (SnmpClassException e) { Console.Error.WriteLine("*** SNMP class error while processing {0}:\n" + "SnmpClass status code: {1}\n{2}", option, e.Status, e); } catch (SnmpException e) { Console.Error.WriteLine("*** SNMP protocol error while processing {0}:\n" + "SNMP error status: {1}\nSNMP error index: {2}\n{3}", option, e.ErrorStatus, e.ErrorIndex, e); } catch (Exception e) { Console.Error.WriteLine("*** Error while processing {0}:\n{1}", option, e); } Console.WriteLine("Remaining native SNMP++ objects before GC: " + MemoryManager.Count); if (statsCollector != null) { stats.WaitForNextStat(); } GC.Collect(); GC.Collect(); GC.WaitForPendingFinalizers(); if (statsCollector != null) { stats.WaitForNextStat(); statsCollector.Interrupt(); statsCollector.Join(); } // Despite our honest intentions, there may still be uncollected // SNMP++.NET objects, especially in the Release mode Console.WriteLine("Remaining native SNMP++ objects after GC: " + MemoryManager.Count); Environment.Exit(retCode); }
public static void Main(string[] args) { #if REQUIRES_CRT_INIT CRT.Auto.Initialize(); #endif string[] secNames = new string[100], authPass = new string[100], privPass = new string[100]; AuthProtocol[] authProtos = new AuthProtocol[100]; PrivProtocol[] privProtos = new PrivProtocol[100]; int port = 162, nSecNames = 0, naps = 0, npps = 0, napr = 0, nppr = 0; uint boot = 100; if (args.Length < 2) { Console.WriteLine("SNMP++.NET trap listener utility\n" + "Author: Marek Malowidzki 2003,2004 ([email protected])\n" + "Based on SNMP++ package from Peter E. Mellquist (HP) and Jochen Katz\n" + "SNMP++.NET " + Assembly.GetAssembly(typeof(Snmp)) + " built on " + Snmp.BuildTime + "; SNMP++ v. " + Snmp.Snmp_ppVersion + "\n\n" + "Usage: " + Environment.GetCommandLineArgs()[0] + " <options>\n" + "where options are the following (specify at least one):\n" + "-p<port> - port number to use (default: 162)\n" + "SNMPv3 options:\n" + "-b<boot> - boot counter value (default: 100)\n" + "Subsequent SNMPv3 users' data:\n" + "-sn<secName> - security name\n" + "-A<authProto>- authentication protocol (" + ManagerUtilities.EnumInfo(typeof(AuthProtocol), AuthProtocol.None) + ")\n" + "-P<privProto>- privacy protocol (" + ManagerUtilities.EnumInfo(typeof(PrivProtocol), PrivProtocol.None) + ")\n" + "-Ua<authPass>- authentication password\n" + "-Up<privPass>- privacy password\n"); Environment.Exit(1); } string option, val; int index = 0; while (ManagerUtilities.GetOption(args, out option, out val, ref index)) { char sub; switch (option) { case "p": port = int.Parse(val); break; case "b": boot = uint.Parse(val); break; case "s": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'n': secNames[nSecNames++] = val; break; default: throw new ArgumentException( sub + ": invalid sub-option"); } break; case "A": authProtos[napr++] = (AuthProtocol)Enum.Parse( typeof(AuthProtocol), val, true); break; case "P": privProtos[nppr++] = (PrivProtocol)Enum.Parse( typeof(PrivProtocol), val, true); break; case "U": switch (sub = ManagerUtilities.GetSubOption(ref val)) { case 'a': authPass[naps++] = val; break; case 'p': privPass[npps++] = val; break; default: throw new ApplicationException( sub + ": invalid sub-option"); } break; default: throw new ApplicationException( "-" + option + ": invalid option"); } } try { option = "v3MP initialization"; V3MP.Init(new OctetStr("SNMP++.NET"), boot); USM usm = V3MP.Instance.Usm; for (int i = 0; i < nSecNames; i++) { usm.AddUsmUser(secNames[i], authProtos[i], privProtos[i], authPass[i], privPass[i]); } option = "Snmp initialization"; using (Snmp snmp = new Snmp(true)) { option = "traps configuration"; Console.WriteLine("Registering trap listener for port " + port); snmp.NotifyListenPort = port; snmp.NotifyRegister(null, null, new NotifyCallback(GotTrap), CB_DATA_); option = "traps"; Console.WriteLine("Processing traps..."); Thread.Sleep(Timeout.Infinite); } } catch (SnmpClassException e) { Console.Error.WriteLine("*** SNMP class error while processing {0}:\n" + "SnmpClass status code: {1}\n{2}", option, e.Status, e); } catch (SnmpException e) { Console.Error.WriteLine("*** SNMP protocol error while processing {0}:\n" + "SNMP error status: {1}\nSNMP error index: {2}\n{3}", option, e.ErrorStatus, e.ErrorIndex, e); } catch (Exception e) { Console.Error.WriteLine("*** Error while processing {0}:\n{1}", option, e); } }
public static AttributeSet Parse(string vcsid, NameValueCollection request) { int index; Hashtable htAttr = new Hashtable(); foreach (string key in request.AllKeys) { if (!(key.StartsWith(ATTR + vcsid) || key.StartsWith(ATTR_D + vcsid) || key.StartsWith(ATTR_T + vcsid)) ) { continue; } AttributeTypes type = AttributeTypes.ATTR_ID; string val = request[key]; string skey = key.Substring(ATTR.Length); if (skey.StartsWith(USD)) { index = skey.LastIndexOf(US); string ekey = skey.Substring(index); skey = skey.Substring(0, index); skey = skey.Substring(USD.Length); if (USC.Equals(ekey)) { type = AttributeTypes.ATTR_TEXT; } else if (USD.Equals(ekey)) { type = AttributeTypes.ATTR_DATE_D; } else if (USM.Equals(ekey)) { type = AttributeTypes.ATTR_DATE_M; } else if (USY.Equals(ekey)) { type = AttributeTypes.ATTR_DATE_Y; } } else if (skey.StartsWith(UST)) { skey = skey.Substring(UST.Length); type = AttributeTypes.ATTR_TEXT; } else { if (val.IndexOf(SEP) > -1) { type = AttributeTypes.ATTR_IDS; } else { type = AttributeTypes.ATTR_ID; } } AttrInfo info = new AttrInfo(); info.typeId = type; // Peel of the vcsid index = skey.IndexOf(US); info.attrId = skey.Substring(index + 1); info.csid = skey.Substring(0, index); info.val = request[key]; skey = info.attrId; object obj = htAttr[info.attrId]; if (obj != null) { if (obj is ArrayList) { ((ArrayList)obj).Add(info); } else { ArrayList al = new ArrayList(); al.Add(obj); al.Add(info); htAttr.Remove(info.attrId); htAttr.Add(skey, al); } } else { htAttr.Add(info.attrId, info); } } return(Compile(htAttr, vcsid)); }
protected override void UpgradeConfirm() { baseDamage = baseDamage * USM.GetDmgRate()[3]; damage = baseDamage; }
/// <summary> /// Encode SNMP version 3 packet /// </summary> /// <param name="authKey">Authentication key (not password)</param> /// <param name="privKey">Privacy key (not password)</param> /// <remarks> /// Before encoding the packet into a byte array you need to ensure all required information is /// set. Examples of required information is request type, Vbs (Oid + values pairs), USM settings including /// SecretName, authentication method and secret (if needed), privacy method and secret (if needed), etc. /// </remarks> /// <returns>Byte array BER encoded SNMP packet.</returns> public byte[] Encode(byte[] authKey, byte[] privKey) { MutableByte buffer = new MutableByte(); // encode the global message data sequence header information MutableByte globalMessageData = new MutableByte(); // if message id is 0 then generate a new, random message id if (_messageId.Value == 0) { Random rand = new Random(); _messageId.Value = rand.Next(1, Int32.MaxValue); } // encode message id _messageId.Encode(globalMessageData); // encode max message size _maxMessageSize.Encode(globalMessageData); // message flags MsgFlags.Encode(globalMessageData); // security model code _securityModel.Value = USM.Type; _securityModel.Encode(globalMessageData); // add global message data to the main buffer // encode sequence header and add data AsnType.BuildHeader(buffer, SnmpConstants.SMI_SEQUENCE, globalMessageData.Length); buffer.Append(globalMessageData); MutableByte packetHeader = new MutableByte(buffer); // before going down this road, check if this is a discovery packet OctetString savedUserName = new OctetString(); bool privacy = MsgFlags.Privacy; bool authentication = MsgFlags.Authentication; bool reportable = MsgFlags.Reportable; if (USM.EngineId.Length <= 0) { // save USM settings prior to encoding a Discovery packet savedUserName.Set(USM.SecurityName); USM.SecurityName.Reset(); // delete security name for discovery packets MsgFlags.Authentication = false; MsgFlags.Privacy = false; MsgFlags.Reportable = true; } USM.Encode(buffer); if (USM.EngineId.Length <= 0) { // restore saved USM values USM.SecurityName.Set(savedUserName); MsgFlags.Authentication = authentication; MsgFlags.Privacy = privacy; MsgFlags.Reportable = reportable; } // Check if privacy encryption is required MutableByte encodedPdu = new MutableByte(); if (MsgFlags.Privacy && USM.EngineId.Length > 0) { IPrivacyProtocol privacyProtocol = PrivacyProtocol.GetInstance(USM.Privacy); if (privacyProtocol == null) { throw new SnmpException(SnmpException.UnsupportedPrivacyProtocol, "Specified privacy protocol is not supported."); } // Get BER encoded ScopedPdu MutableByte unencryptedPdu = new MutableByte(); ScopedPdu.Encode(unencryptedPdu); // we have to expand the key IAuthenticationDigest auth = Authentication.GetInstance(USM.Authentication); if (auth == null) { throw new SnmpException(SnmpException.UnsupportedNoAuthPriv, "Invalid authentication protocol. noAuthPriv mode not supported."); } byte[] encryptedBuffer = privacyProtocol.Encrypt(unencryptedPdu, 0, unencryptedPdu.Length, privKey, USM.EngineBoots, USM.EngineTime, out byte[] privacyParameters, auth); USM.PrivacyParameters.Set(privacyParameters); OctetString encryptedOctetString = new OctetString(encryptedBuffer); encryptedOctetString.Encode(encodedPdu); // now redo packet encoding buffer.Reset(); buffer.Set(packetHeader); USM.Encode(buffer); int preEncodedLength = encodedPdu.Length; buffer.Append(encodedPdu); if (_maxMessageSize.Value != 0) { // verify compliance with maximum message size if ((encodedPdu.Length - preEncodedLength) > _maxMessageSize) { throw new SnmpException(SnmpException.MaximumMessageSizeExceeded, "ScopedPdu exceeds maximum message size."); } } } else { ScopedPdu.Encode(encodedPdu); buffer.Append(encodedPdu); } base.Encode(buffer); if (MsgFlags.Authentication && USM.EngineId.Length > 0) { USM.Authenticate(authKey, ref buffer); // Now re-encode the packet with the authentication information USM.Encode(packetHeader); packetHeader.Append(encodedPdu); base.Encode(packetHeader); buffer = packetHeader; } return(buffer); }
/// <summary> /// Decode SNMP version 3 packet. This method will perform authentication check and decode privacy protected <see cref="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. /// </summary> /// <param name="berBuffer">BER encoded SNMP version 3 packet buffer</param> /// <param name="length">Buffer length</param> /// <param name="authKey">Authentication key (not password)</param> /// <param name="privKey">Privacy key (not password)</param> 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 byte asnType = AsnType.ParseHeader(buffer, ref offset, out int 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 != USM.Type) { throw new SnmpException(SnmpException.UnsupportedSecurityModel, "Class only support SNMP Version 3 User Security Model."); } // parse user security model offset = USM.Decode(buffer, offset); // Authenticate message if authentication flag is set and packet is not a discovery packet if (MsgFlags.Authentication && USM.EngineId.Length > 0) { // Authenticate packet if (USM.AuthenticationParameters.Length != 12) { throw new SnmpAuthenticationException("Invalid authentication parameter field length."); } if (!USM.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 && USM.EngineId.Length > 0) { IPrivacyProtocol privacyProtocol = PrivacyProtocol.GetInstance(USM.Privacy); if (privacyProtocol == null) { throw new SnmpException(SnmpException.UnsupportedPrivacyProtocol, "Privacy protocol requested is not supported."); } if (USM.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, USM.EngineBoots, USM.EngineTime, USM.PrivacyParameters); int tempOffset = 0; offset = ScopedPdu.Decode(decryptedScopedPdu, tempOffset); } else { offset = ScopedPdu.Decode(buffer, offset); } return(offset); }