Example #1
0
        private void txtBytes_TextChanged(object sender, EventArgs e)
        {
            tvMessage.Nodes.Clear();
            var users = new UserRegistry();
            IAuthenticationProvider authen;
            if (tscbAuthentication.SelectedIndex == 0)
            {
                authen = DefaultAuthenticationProvider.Instance;
            }
            else if (tscbAuthentication.SelectedIndex == 1)
            {
                authen = new MD5AuthenticationProvider(new OctetString(tstxtAuthentication.Text));
            }
            else
            {
                authen = new SHA1AuthenticationProvider(new OctetString(tstxtAuthentication.Text));
            }

            IPrivacyProvider privacy;
            if (tscbPrivacy.SelectedIndex == 0)
            {
                privacy = new DefaultPrivacyProvider(authen);
            }
            else if (tscbPrivacy.SelectedIndex == 1)
            {
                privacy = new DESPrivacyProvider(new OctetString(tstxtPrivacy.Text), authen);
            }
            else
            {
                privacy = new AESPrivacyProvider(new OctetString(tstxtPrivacy.Text), authen);
            }

            users.Add(new User(new OctetString(tstxtUser.Text), privacy));

            try
            {
                var messages = MessageFactory.ParseMessages(ByteTool.Convert(txtBytes.Text.Replace("\"", null).Replace("+", null)), users);
                messages.Fill(tvMessage);
            }
            catch (Exception ex)
            {
                tvMessage.Nodes.Add(ex.Message);
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="authentication">The authentication.</param>
        /// <param name="authenticationPhrase">The authen phrase.</param>
        /// <param name="privacy">The privacy.</param>
        /// <param name="privacyPhrase">The privacy phrase.</param>
        public User(OctetString name, string authentication, OctetString authenticationPhrase, string privacy, OctetString privacyPhrase)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (authentication == null)
            {
                throw new ArgumentNullException("authentication");
            }

            if (authenticationPhrase == null)
            {
                throw new ArgumentNullException("authenticationPhrase");
            }

            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            if (privacyPhrase == null)
            {
                throw new ArgumentNullException("privacyPhrase");
            }

            IAuthenticationProvider authenticationProvider;
            if (string.IsNullOrEmpty(authentication))
            {
                authenticationProvider = DefaultAuthenticationProvider.Instance;
            }
            else if (authentication.ToUpperInvariant() == "MD5")
            {
                authenticationProvider = new MD5AuthenticationProvider(authenticationPhrase);
            }
            else if (authentication.ToUpperInvariant() == "SHA")
            {
                authenticationProvider = new SHA1AuthenticationProvider(authenticationPhrase);
            }
            else
            {
                throw new ArgumentException("Unknown authentication method: " + authentication, "authentication");
            }

            IPrivacyProvider privacyProvider;
            if (string.IsNullOrEmpty(privacy))
            {
                privacyProvider = new DefaultPrivacyProvider(authenticationProvider);
            }
            else if (privacy.ToUpperInvariant() == "DES")
            {
                privacyProvider = new DESPrivacyProvider(privacyPhrase, authenticationProvider);
            }
            else
            {
                throw new ArgumentException("Unknown privacy method: " + privacy, "privacy");
            }

            Name = name;
            Privacy = privacyProvider;
        }
        public void TestTrapV3AuthBytes()
        {
            byte[] bytes = Properties.Resources.v3authNoPriv_BER_Issue;
            Stream stream = new MemoryStream(bytes);
            UserRegistry registry = new UserRegistry();
            SHA1AuthenticationProvider authen = new SHA1AuthenticationProvider(new OctetString("testpass"));
            registry.Add(new OctetString("test"), new DefaultPrivacyProvider(authen));
            IList<ISnmpMessage> messages = MessageFactory.ParseMessages(bytes, registry);
            Assert.AreEqual(1, messages.Count);
            ISnmpMessage message = messages[0];
            Assert.AreEqual("80001299030005B706CF69", message.Parameters.EngineId.ToHexString());
            Assert.AreEqual(41, message.Parameters.EngineBoots.ToInt32());
            Assert.AreEqual(877, message.Parameters.EngineTime.ToInt32());
            Assert.AreEqual("test", message.Parameters.UserName.ToString());
            Assert.AreEqual("C107F9DAA3FC552960E38936", message.Parameters.AuthenticationParameters.ToHexString());
            Assert.AreEqual("", message.Parameters.PrivacyParameters.ToHexString());
            Assert.AreEqual("80001299030005B706CF69", message.Scope.ContextEngineId.ToHexString()); // SNMP#NET returns string.Empty here.
            Assert.AreEqual("", message.Scope.ContextName.ToHexString());
            Assert.AreEqual(681323585, message.MessageId());
            Assert.AreEqual(681323584, message.RequestId());

            Console.WriteLine(new OctetString(bytes).ToHexString());
            Console.WriteLine(new OctetString(message.ToBytes()).ToHexString());
            Assert.AreEqual(bytes, message.ToBytes());
        }