Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReportMessage"/> class.
        /// </summary>
        /// <param name="version">The version code.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The security parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        public ReportMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }
            
            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

            Version = version;
            Header = header;
            Parameters = parameters;
            Scope = scope;
            Privacy = privacy;
            Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);            
            _bytes = this.PackMessage(length).ToBytes();
        }
Пример #2
0
        public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList <Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

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

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

            Version    = version;
            Privacy    = privacy;
            Enterprise = enterprise;
            TimeStamp  = time;

            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToOctetString(true));
            var parameters             = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;

            Parameters = new SecurityParameters(
                parameters.EngineId,
                parameters.EngineBoots,
                parameters.EngineTime,
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new InformRequestPdu(
                requestId,
                enterprise,
                time,
                variables);
            var scope = report.Scope;

            Scope = new Scope(scope.ContextEngineId, scope.ContextName, pdu);

            authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = this.PackMessage().ToBytes();
        }
Пример #3
0
        internal InformRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

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

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

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

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;
            InformRequestPdu pdu = (InformRequestPdu)scope.Pdu;

            Enterprise = pdu.Enterprise;
            TimeStamp  = pdu.TimeStamp;

            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
        internal TrapV2Message(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

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

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

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

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;
            var pdu = (TrapV2Pdu)Scope.Pdu;

            Enterprise = pdu.Enterprise;
            TimeStamp  = pdu.TimeStamp;
            _bytes     = this.PackMessage(length).ToBytes();
        }
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <returns></returns>
        public OctetString ComputeHash(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy)
        {
            // TODO: make it extension method.
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

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

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

            return OctetString.Empty;
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReportMessage"/> class.
        /// </summary>
        /// <param name="version">The version code.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The security parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        public ReportMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

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

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

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;
            Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = this.PackMessage().ToBytes();
        }
Пример #7
0
        internal GetRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

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

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

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

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;

            _bytes = this.PackMessage(length).ToBytes();
        }
 internal static Sequence PackMessage(VersionCode version, ISegment header, SecurityParameters parameters, ISegment scope, IPrivacyProvider privacy)
 {
     if (scope == null)
     {
         throw new ArgumentNullException("scope");
     }
     
     if (parameters == null)
     {
         throw new ArgumentNullException("parameters");
     }
     
     if (header == null)
     {
         throw new ArgumentNullException("header");
     }
     
     if (privacy == null)
     {
         throw new ArgumentNullException("privacy");
     }
     
     ISnmpData[] collection = new ISnmpData[4]
                                      {
                                          new Integer32((int)version),
                                          header.GetData(version),
                                          parameters.GetData(version),
                                          privacy.Encrypt(scope.GetData(version), parameters)
                                      };
     return new Sequence(collection);
 }
Пример #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResponseMessage"/> class.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="needAuthentication">if set to <c>true</c>, authentication is needed.</param>
        public ResponseMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, bool needAuthentication)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

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

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

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

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;

            if (needAuthentication)
            {
                Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            }

            _bytes = this.PackMessage().ToBytes();
        }
        public SecureAgentProfile(Guid id, VersionCode version, IPEndPoint agent, string agentName, string authenticationPassphrase, string privacyPassphrase, int authenticationMethod, int privacyMethod, string userName, int timeout)
            : base(id, version, agent, agentName, userName, timeout)
        {
            AuthenticationPassphrase = authenticationPassphrase;
            PrivacyPassphrase = privacyPassphrase;
            AuthenticationMethod = authenticationMethod;
            PrivacyMethod = privacyMethod;

            switch (AuthenticationMethod)
            {
                case 0:
                    _auth = DefaultAuthenticationProvider.Instance;
                    break;
                case 1:
                    _auth = new MD5AuthenticationProvider(new OctetString(AuthenticationPassphrase));
                    break;
                case 2:
                    _auth = new SHA1AuthenticationProvider(new OctetString(AuthenticationPassphrase));
                    break;
            }

            switch (PrivacyMethod)
            {
                case 0:
                    _privacy = new DefaultPrivacyProvider(_auth);
                    break;
                case 1:
                    _privacy = new DESPrivacyProvider(new OctetString(PrivacyPassphrase), _auth);
                    break;
                case 2:
                    _privacy = new AESPrivacyProvider(new OctetString(PrivacyPassphrase), _auth);
                    break;
            }
        }
Пример #11
0
        public override void CopyRequest(ErrorCode status, int index)
        {
            OctetString      userName = Request.Parameters.UserName;
            IPrivacyProvider privacy  = Users.Find(userName);

            int[] time = Group.EngineTimeData;
            Response = new ResponseMessage(
                Request.Version,
                new Header(
                    new Integer32(Request.MessageId()),
                    new Integer32(Messenger.MaxMessageSize),
                    privacy.ToSecurityLevel()),
                new SecurityParameters(
                    Group.EngineId,
                    new Integer32(time[0]),
                    new Integer32(time[1]),
                    userName,
                    privacy.AuthenticationProvider.CleanDigest,
                    privacy.Salt),
                new Scope(
                    Group.EngineId,
                    OctetString.Empty,
                    new ResponsePdu(
                        Request.RequestId(),
                        status,
                        index,
                        Request.Pdu().Variables)),
                privacy,
                true,
                null);
            if (TooBig)
            {
                GenerateTooBig();
            }
        }
Пример #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReportMessage"/> class.
        /// </summary>
        /// <param name="version">The version code.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The security parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        public ReportMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("Only v3 is supported.", nameof(version));
            }

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;
            Privacy.ComputeHash(Version, Header, Parameters, Scope);
            _bytes = this.PackMessage(length).ToBytes();
        }
Пример #13
0
        private IPrivacyProvider CreateKeys()
        {
            IAuthenticationProvider auth = null;
            IPrivacyProvider        priv = null;

            switch (ConnectionOptions.Authentication)
            {
            case Authentication.MD5:
                auth = new MD5AuthenticationProvider(new OctetString(ConnectionOptions.SNMPPass));
                break;

            case Authentication.SHA:
                auth = new SHA1AuthenticationProvider(new OctetString(ConnectionOptions.SNMPPass));
                break;
            }
            switch (ConnectionOptions.Encryption)
            {
            case Encryption.AES:
                priv = new AESPrivacyProvider(new OctetString(ConnectionOptions.SNMPEncryptionKey), auth);
                break;

            case Encryption.DES:
                priv = new DESPrivacyProvider(new OctetString(ConnectionOptions.SNMPEncryptionKey), auth);
                break;
            }
            return(priv);
        }
Пример #14
0
        internal override void CopyRequest(ErrorCode status, int index)
        {
            var userName             = Request.Parameters.UserName;
            IPrivacyProvider privacy = Users.Find(userName);

            Response = new ResponseMessage(
                Request.Version,
                new Header(
                    new Integer32(Request.MessageId),
                    new Integer32(Messenger.MaxMessageSize),
                    new OctetString(new[] { (byte)Levels.Reportable }),
                    new Integer32(3)),
                new SecurityParameters(
                    Group.EngineId,
                    new Integer32(Group.EngineBoots),
                    new Integer32(Group.EngineTime),
                    userName,
                    privacy.AuthenticationProvider.CleanDigest,
                    privacy.Salt),
                new Scope(
                    Group.EngineId,
                    OctetString.Empty,
                    new ResponsePdu(
                        Request.RequestId,
                        status,
                        index,
                        Request.Pdu.Variables)),
                privacy,
                true);
            if (TooBig)
            {
                GenerateTooBig();
            }
        }
Пример #15
0
        internal InformRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

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

            Version    = version;
            Header     = header;
            Parameters = parameters;
            Scope      = scope;
            Privacy    = privacy;
            var pdu = (InformRequestPdu)scope.Pdu;

            Enterprise = pdu.Enterprise;
            TimeStamp  = pdu.TimeStamp;

            _bytes = this.PackMessage(length).ToBytes();
        }
Пример #16
0
        public override bool HandleMembership()
        {
            ISnmpMessage       request    = Request;
            SecurityParameters parameters = request.Parameters;
            SnmpType           typeCode   = Request.TypeCode();

            if (typeCode == SnmpType.Unknown)
            {
                HandleFailure(Group.DecryptionError);
                return(false);
            }

            if (typeCode != SnmpType.TrapV2Pdu && parameters.EngineId != Group.EngineId)
            {
                HandleDiscovery();
                return(true);
            }

            IPrivacyProvider user = Users.Find(parameters.UserName);

            if (user == null)
            {
                HandleFailure(Group.UnknownSecurityName);
                return(false);
            }

            if (typeCode == SnmpType.TrapV2Pdu && (user.EngineIds == null || !user.EngineIds.Contains(parameters.EngineId)))
            {
                HandleFailure(Group.UnknownEngineId);
                return(false);
            }

            if (parameters.IsInvalid)
            {
                HandleFailure(Group.AuthenticationFailure);
                return(false);
            }

            if (typeCode == SnmpType.TrapV2Pdu)
            {
                return(true);
            }

            if ((user.ToSecurityLevel() | Levels.Reportable) != request.Header.SecurityLevel)
            {
                HandleFailure(Group.UnsupportedSecurityLevel);
                return(false);
            }

            bool inTime = EngineGroup.IsInTime(Group.EngineTimeData, parameters.EngineBoots.ToInt32(), parameters.EngineTime.ToInt32());

            if (!inTime)
            {
                HandleFailure(Group.NotInTimeWindow);
                return(false);
            }

            return(true);
        }
Пример #17
0
 public IPrivacyProvider FindByProvider(OctetString userName, IPrivacyProvider provider)
 {
     if (userName == null)
     {
         throw new ArgumentNullException(nameof(userName));
     }
     return(_identifiedUsers.FirstOrDefault(u => u.Value.Name == userName && u.Value.Privacy == provider).Value.Privacy);
 }
Пример #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GetNextRequestMessage"/> class.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        /// <param name="report">The report.</param>
        public GetNextRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, IList <Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

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

            Version = version;
            Privacy = privacy;
            Levels recordToSecurityLevel = PrivacyProviderExtension.ToSecurityLevel(privacy);

            recordToSecurityLevel |= Levels.Reportable;
            byte b = (byte)recordToSecurityLevel;

            // TODO: define more constants.
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), new OctetString(new[] { b }), new Integer32(3));
            var parameters             = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;

            Parameters = new SecurityParameters(
                parameters.EngineId,
                parameters.EngineBoots,
                parameters.EngineTime,
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            GetNextRequestPdu pdu = new GetNextRequestPdu(
                requestId,
                ErrorCode.NoError,
                0,
                variables);
            var scope = report.Scope;

            Scope = new Scope(scope.ContextEngineId, scope.ContextName, pdu);

            Parameters.AuthenticationParameters = authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
        /// <summary>
        /// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="nonRepeaters">The non repeaters.</param>
        /// <param name="maxRepetitions">The max repetitions.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        /// <param name="report">The report.</param>
        public GetBulkRequestMessage(
            VersionCode version, int messageId, int requestId, OctetString userName, int nonRepeaters, int maxRepetitions, IList <Variable> variables,
            IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

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

            if (nonRepeaters > variables.Count)
            {
                throw new ArgumentException("nonRepeaters should not be greater than variable count", "nonRepeaters");
            }

            if (maxRepetitions < 1)
            {
                throw new ArgumentException("maxRepetitions should be greater than 0", "maxRepetitions");
            }

            Version = version;
            Privacy = privacy;

            // TODO: define more constants.
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
            var parameters             = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;

            Parameters = new SecurityParameters(parameters.EngineId, parameters.EngineBoots, parameters.EngineTime, userName, authenticationProvider.CleanDigest,
                                                Privacy.Salt);
            var pdu             = new GetBulkRequestPdu(requestId, nonRepeaters, maxRepetitions, variables);
            var scope           = report.Scope;
            var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;

            Scope = new Scope(contextEngineId, scope.ContextName, pdu);

            authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = this.PackMessage(null).ToBytes();
        }
Пример #20
0
        public TrapV2Message(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, List <Variable> variables, IPrivacyProvider privacy, int maxMessageSize, OctetString engineId, int engineBoots, int engineTime)
        {
            if (userName == null)
            {
                throw new ArgumentNullException(nameof(userName));
            }

            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("Only v3 is supported.", nameof(version));
            }

            if (enterprise == null)
            {
                throw new ArgumentNullException(nameof(enterprise));
            }

            if (engineId == null)
            {
                throw new ArgumentNullException(nameof(engineId));
            }

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

            Version    = version;
            Privacy    = privacy;
            Enterprise = enterprise;
            TimeStamp  = time;

            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel());
            var authenticationProvider = Privacy.AuthenticationProvider;

            Parameters = new SecurityParameters(
                engineId,
                new Integer32(engineBoots),
                new Integer32(engineTime),
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new TrapV2Pdu(
                requestId,
                enterprise,
                time,
                variables);

            // TODO: may expose engine ID in the future.
            Scope = new Scope(OctetString.Empty, OctetString.Empty, pdu);
            Privacy.ComputeHash(Version, Header, Parameters, Scope);
            _bytes = this.PackMessage(null).ToBytes();
        }
Пример #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GetNextRequestMessage"/> class.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="contextName">Context name.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        /// <param name="report">The report.</param>
        public GetNextRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, OctetString contextName, IList <Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            if (userName == null)
            {
                throw new ArgumentNullException(nameof(userName));
            }

            if (contextName == null)
            {
                throw new ArgumentNullException(nameof(contextName));
            }

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("Only v3 is supported.", nameof(version));
            }

            if (report == null)
            {
                throw new ArgumentNullException(nameof(report));
            }

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

            Version = version;
            Privacy = privacy;

            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
            var parameters             = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;

            Parameters = new SecurityParameters(
                parameters.EngineId,
                parameters.EngineBoots,
                parameters.EngineTime,
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new GetNextRequestPdu(
                requestId,
                variables);
            var scope           = report.Scope;
            var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;

            Scope = new Scope(contextEngineId, contextName, pdu);

            Privacy.ComputeHash(Version, Header, Parameters, Scope);
            _bytes = this.PackMessage(null).ToBytes();
        }
Пример #22
0
        /// <summary>
        /// Converts to <see cref="OctetString"/>.
        /// </summary>
        /// <param name="privacy">Privacy provider.</param>
        /// <param name="reportable">Reportable flag.</param>
        /// <returns></returns>
        public static OctetString ToOctetString(this IPrivacyProvider privacy, bool reportable)
        {
            Levels recordToSecurityLevel = privacy.ToSecurityLevel();

            if (reportable)
            {
                recordToSecurityLevel |= Levels.Reportable;
            }

            return(new OctetString(new[] { (byte)recordToSecurityLevel }));
        }
Пример #23
0
 internal InformRequestMessage(
     VersionCode version,
     IPEndPoint sourceEndPoint,
     Header header,
     SecurityParameters parameters,
     Scope scope,
     IPrivacyProvider privacy)
     : base(version, sourceEndPoint.Address, header, parameters, scope, privacy)
 {
     SourceEndPoint = sourceEndPoint;
 }
        /// <summary>
        /// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="contextName">Context name.</param>
        /// <param name="nonRepeaters">The non repeaters.</param>
        /// <param name="maxRepetitions">The max repetitions.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, OctetString contextName, int nonRepeaters, int maxRepetitions, IList <Variable> variables, IPrivacyProvider privacy, int maxMessageSize)
        {
            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            if (contextName == null)
            {
                throw new ArgumentNullException(nameof(contextName));
            }

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("Only v3 is supported.", nameof(version));
            }

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

            if (nonRepeaters > variables.Count)
            {
                throw new ArgumentException("nonRepeaters should not be greater than variable count.", nameof(nonRepeaters));
            }

            if (maxRepetitions < 1)
            {
                throw new ArgumentException("maxRepetitions should be greater than 0.", nameof(maxRepetitions));
            }

            Version = version;
            Privacy = privacy;

            // TODO: define more constants.
            Header     = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable, new Integer32((int)SecurityModel.Tsm));
            Parameters = SecurityParameters.Empty;

            var pdu = new GetBulkRequestPdu(
                requestId,
                nonRepeaters,
                maxRepetitions,
                variables);
            var contextEngineId = OctetString.Empty;

            Scope = new Scope(contextEngineId, contextName, pdu);

            Privacy.ComputeHash(Version, Header, Parameters, Scope);
            _bytes = this.PackMessage(null).ToBytes();
        }
Пример #25
0
 public DiscordUserEventHandler(IUserStore userStore,
                                IPrivacyProvider privacyProvider,
                                INonMemberRoleProvider nonMemberRoleProvider,
                                IGameRoleProvider gameRoleProvider,
                                IDatabaseAccess databaseAccess,
                                IDynamicConfiguration dynamicConfiguration)
 {
     _userStore             = userStore;
     _privacyProvider       = privacyProvider;
     _nonMemberRoleProvider = nonMemberRoleProvider;
     _gameRoleProvider      = gameRoleProvider;
     _databaseAccess        = databaseAccess;
     _dynamicConfiguration  = dynamicConfiguration;
 }
Пример #26
0
        /// <summary>
        /// Gets the scope data.
        /// </summary>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="rawScopeData">The raw scope data.</param>
        /// <returns>ISnmpData.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// privacy
        /// or
        /// header
        /// </exception>
        public static ISnmpData GetScopeData(this IPrivacyProvider privacy, Header header, SecurityParameters parameters, ISnmpData rawScopeData)
        {
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

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

            return(Levels.Privacy == (header.SecurityLevel & Levels.Privacy) ? privacy.Encrypt(rawScopeData, parameters) : rawScopeData);
        }
        /// <summary>
        /// Walks.
        /// </summary>
        /// <param name="version">Protocol version.</param>
        /// <param name="endpoint">Endpoint.</param>
        /// <param name="community">Community name.</param>
        /// <param name="table">OID.</param>
        /// <param name="list">A list to hold the results.</param>
        /// <param name="maxRepetitions">The max repetitions.</param>
        /// <param name="mode">Walk mode.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="report">The report.</param>
        /// <returns></returns>
        public static async Task <int> BulkWalkAsync(
            VersionCode version, IPEndPoint endpoint, OctetString community, ObjectIdentifier table, IList <Variable> list, int maxRepetitions, WalkMode mode,
            IPrivacyProvider privacy, ISnmpMessage report)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            var tableV = new Variable(table);
            var seed   = tableV;
            IList <Variable> next;
            var result  = 0;
            var message = report;
            var data    = await BulkHasNextAsync(version, endpoint, community, seed, maxRepetitions, privacy, message).ConfigureAwait(false);

            next    = data.Item2;
            message = data.Item3;
            while (data.Item1)
            {
                var subTreeMask = string.Format(CultureInfo.InvariantCulture, "{0}.", table);
                var rowMask     = string.Format(CultureInfo.InvariantCulture, "{0}.1.1.", table);
                foreach (var v in next)
                {
                    var id = v.Id.ToString();
                    if (v.Data.TypeCode == SnmpType.EndOfMibView)
                    {
                        goto end;
                    }

                    if (mode == WalkMode.WithinSubtree && !id.StartsWith(subTreeMask, StringComparison.Ordinal))
                    {
                        // not in sub tree
                        goto end;
                    }

                    list.Add(v);
                    if (id.StartsWith(rowMask, StringComparison.Ordinal))
                    {
                        result++;
                    }
                }

                seed = next[next.Count - 1];
                data = await BulkHasNextAsync(version, endpoint, community, seed, maxRepetitions, privacy, message).ConfigureAwait(false);
            }

end:
            return(result);
        }
        private static bool BulkHasNext(
            VersionCode version, IPEndPoint receiver, OctetString community, Variable seed, int timeout, int maxRepetitions, out IList <Variable> next,
            IPrivacyProvider privacy, ref ISnmpMessage report)
        {
            if (version == VersionCode.V1)
            {
                throw new ArgumentException("v1 is not supported", "version");
            }

            var variables = new List <Variable> {
                new Variable(seed.Id)
            };
            var request = version == VersionCode.V3
                                                                        ? new GetBulkRequestMessage(version, MessageCounter.NextId, RequestCounter.NextId, community, 0, maxRepetitions, variables, privacy, MaxMessageSize,
                                                                                                    report) : new GetBulkRequestMessage(RequestCounter.NextId, version, community, 0, maxRepetitions, variables);
            var reply = request.GetResponse(timeout, receiver);

            if (reply is ReportMessage)
            {
                if (reply.Pdu().Variables.Count == 0)
                {
                    // TODO: whether it is good to return?
                    next = new List <Variable> (0);
                    return(false);
                }

                var id = reply.Pdu().Variables[0].Id;
                if (id != IdNotInTimeWindow)
                {
                    // var error = id.GetErrorMessage();
                    // TODO: whether it is good to return?
                    next = new List <Variable> (0);
                    return(false);
                }

                // according to RFC 3414, send a second request to sync time.
                request = new GetBulkRequestMessage(version, MessageCounter.NextId, RequestCounter.NextId, community, 0, maxRepetitions, variables, privacy, MaxMessageSize,
                                                    reply);
                reply = request.GetResponse(timeout, receiver);
            }
            else if (reply.Pdu().ErrorStatus.ToInt32() != 0)
            {
                throw ErrorException.Create("error in response", receiver.Address, reply);
            }

            next   = reply.Pdu().Variables;
            report = request;
            return(next.Count != 0);
        }
Пример #29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="privacy">The privacy provider.</param>
        public User(OctetString name, IPrivacyProvider privacy)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

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

            Name = name;
            Privacy = privacy;
        }
Пример #30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="privacy">The privacy provider.</param>
        public User(OctetString name, IPrivacyProvider privacy)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

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

            Name    = name;
            Privacy = privacy;
        }
Пример #31
0
 public BotEngine(ILogger <BotEngine> logger,
                  IDynamicConfiguration dynamicConfiguration,
                  IDiscordAccess discordAccess,
                  IUnitsSignalRClient unitsSignalRClient,
                  IBotInformationProvider botInformationProvider,
                  IPrivacyProvider privacyProvider)
 {
     _logger = logger;
     _dynamicConfiguration             = dynamicConfiguration;
     _dynamicConfiguration.DataLoaded += DynamicConfiguration_DataLoaded;
     _discordAccess          = discordAccess;
     _unitsSignalRClient     = unitsSignalRClient;
     _botInformationProvider = botInformationProvider;
     _privacyProvider        = privacyProvider;
     _isFirstConnect         = true;
 }
Пример #32
0
        internal TrapV2Message(
            VersionCode version,
            IPAddress senderIp,
            int messageId,
            int requestId,
            OctetString userName,
            ObjectIdentifier enterprise,
            uint time,
            IList <Variable> variables,
            IPrivacyProvider privacy,
            int maxMessageSize)
        {
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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


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

            Version    = version;
            Privacy    = privacy;
            Enterprise = enterprise;
            TimeStamp  = time;
            SenderIP   = senderIp;

            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
        }
Пример #33
0
        private ProviderPair SecurityCheck(Arguments args)
        {
            if (string.IsNullOrEmpty(args.UserName))
            {
                throw new InvalidOperationException("User name needs to be specified for v3.");
            }

            IAuthenticationProvider auth = (args.Level & Levels.Authentication) == Levels.Authentication
                                               ? GetAuthenticationProviderByName(args.Authentication, args.AuthPhrase)
                                               : DefaultAuthenticationProvider.Instance;

            IPrivacyProvider priv = (args.Level & Levels.Privacy) == Levels.Privacy
                                        ? new DESPrivacyProvider(new OctetString(args.PrivPhrase), auth)
                                        : DefaultPrivacyProvider.Instance;

            return(new ProviderPair(auth, priv));
        }
        public static void SendInform(
            int requestId, VersionCode version, IPEndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList <Variable> variables,
            int timeout, IPrivacyProvider privacy, ISnmpMessage report)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException("receiver");
            }

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

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

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

            if (version == VersionCode.V3 && privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            if (version == VersionCode.V3 && report == null)
            {
                throw new ArgumentNullException("report");
            }

            var message = version == VersionCode.V3
                                                                        ? new InformRequestMessage(version, MessageCounter.NextId, requestId, community, enterprise, timestamp, variables, privacy, MaxMessageSize, report)
                                                                        : new InformRequestMessage(requestId, version, community, enterprise, timestamp, variables);

            var response = message.GetResponse(timeout, receiver);

            if (response.Pdu().ErrorStatus.ToInt32() != 0)
            {
                throw ErrorException.Create("error in response", receiver.Address, response);
            }
        }
Пример #35
0
        /// <summary>
        /// Verifies the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="scopeBytes">The scope bytes.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        /// <returns>
        /// Returns <c>true</c> if hash matches. Otherwise, returns <c>false</c>.
        /// </returns>
        public static bool VerifyHash(this IPrivacyProvider privacy, VersionCode version, Header header, SecurityParameters parameters, ISnmpData scopeBytes, byte[] length)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

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

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

            var provider = privacy.AuthenticationProvider;

            if (provider is DefaultAuthenticationProvider)
            {
                return(true);
            }

            if (0 == (header.SecurityLevel & Levels.Authentication))
            {
                return(true);
            }

            var expected = parameters.AuthenticationParameters;

            parameters.AuthenticationParameters = provider.CleanDigest; // clean the hash first.
            var newHash = provider.ComputeHash(version, header, parameters, scopeBytes, privacy, length);

            parameters.AuthenticationParameters = expected; // restore the hash.
            return(newHash == expected);
        }
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="provider">The authentication provider.</param>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        public static void ComputeHash(this IAuthenticationProvider provider, VersionCode version, Header header, SecurityParameters parameters, ISegment scope, IPrivacyProvider privacy)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

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

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

            if (provider is DefaultAuthenticationProvider)
            {
                return;
            }

            if (0 == (header.SecurityLevel & Levels.Authentication))
            {
                return;
            }

            var scopeData = privacy.GetScopeData(header, parameters, scope.GetData(version));
            parameters.AuthenticationParameters = provider.ComputeHash(version, header, parameters, scopeData, privacy, null); // replace the hash.
        }
        /// <summary>
        /// Toes the security level.
        /// </summary>
        /// <returns></returns>
        public static Levels ToSecurityLevel(IPrivacyProvider privacy)
        {
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }
                
            Levels flags;
            if (privacy.AuthenticationProvider == DefaultAuthenticationProvider.Instance)
            {
                flags = 0;
            }
            else if (privacy is DefaultPrivacyProvider)
            {
                flags = Levels.Authentication;
            }
            else
            {
                flags = Levels.Authentication | Levels.Privacy;
            }

            return flags;
        }
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="data">The scope data.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        /// <returns></returns>
        public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy, byte[] length)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

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

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

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

            return OctetString.Empty;
        }
Пример #39
0
 public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, int nonRepeaters, int maxRepetitions, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
     : this(version, messageId, requestId, userName, nonRepeaters, maxRepetitions, variables, privacy, 0xFFE3, report)
 {
 }
Пример #40
0
 /// <summary>
 /// Adds the specified user name.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="privacy">The privacy provider.</param>
 public UserRegistry Add(OctetString userName, IPrivacyProvider privacy)
 {
     return Add(new User(userName, privacy));
 }
 public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
     : this(version, messageId, requestId, userName, enterprise, time, variables, privacy, 0xFFE3, report)       
 {
 }
Пример #42
0
        internal TrapV2Message(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            Version = version;
            Header = header;
            Parameters = parameters;
            Scope = scope;
            Privacy = privacy;
            TrapV2Pdu pdu = (TrapV2Pdu)Scope.Pdu;
            Enterprise = pdu.Enterprise;
            TimeStamp = pdu.TimeStamp;

            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
        public GetResponseMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, bool needAuthentication)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            Version = version;
            Header = header;
            Parameters = parameters;
            Scope = scope;
            Privacy = privacy;

            if (needAuthentication)
            {
                Parameters.AuthenticationParameters = Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            }

            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
		/// <summary>
		/// Computes the hash.
		/// </summary>
		/// <param name="version">The version.</param>
		/// <param name="header">The header.</param>
		/// <param name="parameters">The parameters.</param>
		/// <param name="scopeBytes">The scope bytes.</param>
		/// <param name="privacy">The privacy provider.</param>
		/// <returns></returns>
		private OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData scopeBytes, IPrivacyProvider privacy)
		{
			if (scopeBytes == null)
			{
				throw new ArgumentNullException("scopeBytes");
			}

			byte[] key = PasswordToKey(_password, parameters.EngineId.GetRaw());
#if ! SILVERLIGHT  //mc++
			using (HMACMD5 md5 = new HMACMD5(key))
			{
				byte[] hash = md5.ComputeHash(SnmpMessageExtension.PackMessage(version, header, parameters, scopeBytes, privacy).ToBytes());
				md5.Clear();
				byte[] result = new byte[DigestLength];
				Array.Copy(hash, result, result.Length);
				return new OctetString(result);
			}
#endif
			return null;
		}
Пример #45
0
        public static void SendInform(int requestId, VersionCode version, IPEndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList<Variable> variables, int timeout, IPrivacyProvider privacy, ISnmpMessage report)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException("receiver");
            }

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

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

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

            if (version == VersionCode.V3 && privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            if (version == VersionCode.V3 && report == null)
            {
                throw new ArgumentNullException("report");
            }

            var message = version == VersionCode.V3
                                    ? new InformRequestMessage(
                                          version,
                                          MessageCounter.NextId,
                                          requestId,
                                          community,
                                          enterprise,
                                          timestamp,
                                          variables,
                                          privacy,
                                          MaxMessageSize,
                                          report)
                                    : new InformRequestMessage(
                                          requestId,
                                          version,
                                          community,
                                          enterprise,
                                          timestamp,
                                          variables);

            var response = message.GetResponse(timeout, receiver);
            if (response.Pdu().ErrorStatus.ToInt32() != 0)
            {
                throw ErrorException.Create(
                    "error in response",
                    receiver.Address,
                    response);
            }
        }
Пример #46
0
        public TrapV2Message(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, OctetString engineId, int engineBoots, int engineTime)
        {
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }
            
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }
            
            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

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

            Version = version;
            Privacy = privacy;
            Enterprise = enterprise;
            TimeStamp = time;
            Levels recordToSecurityLevel = PrivacyProviderExtension.ToSecurityLevel(privacy);
            byte b = (byte)recordToSecurityLevel;
            
            // TODO: define more constants.
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), new OctetString(new[] { b }), new Integer32(3));
            var authenticationProvider = Privacy.AuthenticationProvider;
            Parameters = new SecurityParameters(
                engineId,
                new Integer32(engineBoots), 
                new Integer32(engineTime), 
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new TrapV2Pdu(
                requestId,
                enterprise,
                time,
                variables);
            Scope = new Scope(OctetString.Empty, OctetString.Empty, pdu);

            Parameters.AuthenticationParameters = authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
		/// <summary>
		/// Computes the hash.
		/// </summary>
		/// <param name="version">The version.</param>
		/// <param name="header">The header.</param>
		/// <param name="parameters">The parameters.</param>
		/// <param name="scope">The scope.</param>
		/// <param name="privacy">The privacy provider.</param>
		/// <returns></returns>
		public OctetString ComputeHash(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy)
		{
			if (header == null)
			{
				throw new ArgumentNullException("header");
			}

			if (parameters == null)
			{
				throw new ArgumentNullException("parameters");
			}
			
			if (scope == null)
			{
				throw new ArgumentNullException("scope");
			}
			
			if (privacy == null)
			{
				throw new ArgumentNullException("privacy");
			}
			
			return ComputeHash(version, header, parameters, privacy.Encrypt(scope.GetData(version), parameters), privacy);
		}
Пример #48
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SetRequestMessage"/> class.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        /// <param name="report">The report.</param>
        public SetRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }
            
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }
            
            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

            Version = version;
            Privacy = privacy;

            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
            var parameters = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;
            Parameters = new SecurityParameters(
                parameters.EngineId,
                parameters.EngineBoots,
                parameters.EngineTime,
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new SetRequestPdu(
                requestId,
                variables);
            var scope = report.Scope;
            var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
            Scope = new Scope(contextEngineId, scope.ContextName, pdu);

            authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = this.PackMessage(null).ToBytes();
        }
Пример #49
0
 public SetRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
     : this(version, messageId, requestId, userName, variables, privacy, 0xFFE3, report)
 {
 }
		/// <summary>
		/// Verifies the hash.
		/// </summary>
		/// <param name="version">The version.</param>
		/// <param name="header">The header.</param>
		/// <param name="parameters">The parameters.</param>
		/// <param name="scopeBytes">The scope bytes.</param>
		/// <param name="privacy">The privacy provider.</param>
		/// <returns>
		/// Returns <code>true</code> if hash matches. Otherwise, returns <code>false</code>.
		/// </returns>
		public bool VerifyHash(VersionCode version, Header header, SecurityParameters parameters, ISnmpData scopeBytes, IPrivacyProvider privacy)
		{
			if (header == null)
			{
				throw new ArgumentNullException("header");
			}

			if (parameters == null)
			{
				throw new ArgumentNullException("parameters");
			}
			
			if (scopeBytes == null)
			{
				throw new ArgumentNullException("scopeBytes");
			}
			
			if (privacy == null)
			{
				throw new ArgumentNullException("privacy");
			} 
			
			var expected = parameters.AuthenticationParameters;
			parameters.AuthenticationParameters = CleanDigest;
			bool result = ComputeHash(version, header, parameters, scopeBytes, privacy) == expected;
			parameters.AuthenticationParameters = expected;
			return result;
		}
        internal SetRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            Version = version;
            Header = header;
            Parameters = parameters;
            Scope = scope;
            Privacy = privacy;

            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
Пример #52
0
        /// <summary>
        /// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="contextName">Name of context.</param>
        /// <param name="nonRepeaters">The non repeaters.</param>
        /// <param name="maxRepetitions">The max repetitions.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        /// <param name="report">The report.</param>
        public GetBulkRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, OctetString contextName, int nonRepeaters, int maxRepetitions, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

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

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

            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

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

            if (nonRepeaters > variables.Count)
            {
                throw new ArgumentException("nonRepeaters should not be greater than variable count", "nonRepeaters");
            }

            if (maxRepetitions < 1)
            {
                throw new ArgumentException("maxRepetitions should be greater than 0", "maxRepetitions");
            }

            Version = version;
            Privacy = privacy;

            // TODO: define more constants.
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
            var parameters = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;
            Parameters = new SecurityParameters(
                parameters.EngineId,
                parameters.EngineBoots,
                parameters.EngineTime,
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new GetBulkRequestPdu(
                requestId,
                nonRepeaters,
                maxRepetitions,
                variables);
            var scope = report.Scope;
            var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
            Scope = new Scope(contextEngineId, contextName, pdu);

            authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = this.PackMessage(null).ToBytes();
        }
Пример #53
0
        internal GetBulkRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            Version = version;
            Header = header;
            Parameters = parameters;
            Scope = scope;
            Privacy = privacy;

            _bytes = this.PackMessage(length).ToBytes();
        }
Пример #54
0
 /// <summary>
 /// Adds the specified user name.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="privacy">The privacy provider.</param>
 public void Add(OctetString userName, IPrivacyProvider privacy)
 {
     Add(new User(userName, privacy));
 }
Пример #55
0
        private static bool BulkHasNext(VersionCode version, IPEndPoint endpoint, OctetString community, Variable seed, int timeout, int maxRepetitions, out IList<Variable> next, IPrivacyProvider privacy, ref ISnmpMessage report)
        {
            if (version == VersionCode.V1)
            {
                throw new ArgumentException("v1 is not supported", "version");
            }

            var variables = new List<Variable> { new Variable(seed.Id) };
            var requestId = RequestCounter.NextId;
            var message = version == VersionCode.V3
                                                ? new GetBulkRequestMessage(
                                                      version,
                                                      MessageCounter.NextId,
                                                      requestId,
                                                      community,
                                                      0,
                                                      maxRepetitions,
                                                      variables, 
                                                      privacy, 
                                                      MaxMessageSize,
                                                      report)
                                                : new GetBulkRequestMessage(
                                                      requestId,
                                                      version,
                                                      community,
                                                      0,
                                                      maxRepetitions,
                                                      variables);

            var response = message.GetResponse(timeout, endpoint);
            var pdu = response.Pdu();
            if (pdu.ErrorStatus.ToInt32() != 0)
            {
                throw ErrorException.Create(
                    "error in response",
                    endpoint.Address,
                    response);
            }

            next = pdu.Variables;
            report = message;
            return next.Count != 0;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SetRequestMessage"/> class.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="messageId">The message id.</param>
        /// <param name="requestId">The request id.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="variables">The variables.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="maxMessageSize">Size of the max message.</param>
        /// <param name="report">The report.</param>
        public SetRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
        {
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }
            
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }
            
            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

            Version = version;
            Privacy = privacy;
            Levels recordToSecurityLevel = PrivacyProviderExtension.ToSecurityLevel(privacy);
            recordToSecurityLevel |= Levels.Reportable;
            byte b = (byte)recordToSecurityLevel;
            
            // TODO: define more constants.
            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), new OctetString(new[] { b }), new Integer32(3));
            var parameters = report.Parameters;
            var authenticationProvider = Privacy.AuthenticationProvider;
            Parameters = new SecurityParameters(
                parameters.EngineId,
                parameters.EngineBoots,
                parameters.EngineTime,
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            SetRequestPdu pdu = new SetRequestPdu(
                requestId,
                ErrorCode.NoError,
                0,
                variables);
            var scope = report.Scope;
            Scope = new Scope(scope.ContextEngineId, scope.ContextName, pdu);

            Parameters.AuthenticationParameters = authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = SnmpMessageExtension.PackMessage(Version, Header, Parameters, Scope, Privacy).ToBytes();
        }
Пример #57
0
        /// <summary>
        /// Walks.
        /// </summary>
        /// <param name="version">Protocol version.</param>
        /// <param name="endpoint">Endpoint.</param>
        /// <param name="community">Community name.</param>
        /// <param name="table">OID.</param>
        /// <param name="list">A list to hold the results.</param>
        /// <param name="timeout">The time-out value, in milliseconds. The default value is 0, which indicates an infinite time-out period. Specifying -1 also indicates an infinite time-out period.</param>
        /// <param name="maxRepetitions">The max repetitions.</param>
        /// <param name="mode">Walk mode.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="report">The report.</param>
        /// <returns></returns>
        public static int BulkWalk(VersionCode version, IPEndPoint endpoint, OctetString community, ObjectIdentifier table, IList<Variable> list, int timeout, int maxRepetitions, WalkMode mode, IPrivacyProvider privacy, ISnmpMessage report)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            var tableV = new Variable(table);
            var seed = tableV;
            IList<Variable> next;
            var result = 0;
            var message = report;
            while (BulkHasNext(version, endpoint, community, seed, timeout, maxRepetitions, out next, privacy, ref message))
            {
                var subTreeMask = string.Format(CultureInfo.InvariantCulture, "{0}.", table);
                var rowMask = string.Format(CultureInfo.InvariantCulture, "{0}.1.1.", table);
                foreach (var v in next)
                {
                    var id = v.Id.ToString();
                    if (v.Data.TypeCode == SnmpType.EndOfMibView)
                    {
                        goto end;
                    }

                    if (mode == WalkMode.WithinSubtree && !id.StartsWith(subTreeMask, StringComparison.Ordinal))
                    {
                        // not in sub tree
                        goto end;
                    }

                    list.Add(v);
                    if (id.StartsWith(rowMask, StringComparison.Ordinal))
                    {
                        result++;
                    }
                }

                seed = next[next.Count - 1];
            }

        end:
            return result;
        }
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="data">The scope data.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        /// <returns></returns>
        public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy, byte[] length)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            var key = PasswordToKey(_password, parameters.EngineId.GetRaw());
            using (var md5 = new HMACMD5(key))
            {
                var hash = md5.ComputeHash(ByteTool.PackMessage(length, version, header, parameters, data).ToBytes());
                md5.Clear();
                var result = new byte[DigestLength];
                Buffer.BlockCopy(hash, 0, result, 0, result.Length);
                return new OctetString(result);
            }
        }
Пример #59
0
        private static bool BulkHasNext(VersionCode version, IPEndPoint receiver, OctetString community, Variable seed, int timeout, int maxRepetitions, out IList<Variable> next, IPrivacyProvider privacy, ref ISnmpMessage report)
        {
            if (version == VersionCode.V1)
            {
                throw new ArgumentException("v1 is not supported", "version");
            }

            var variables = new List<Variable> { new Variable(seed.Id) };
            var request = version == VersionCode.V3
                                                ? new GetBulkRequestMessage(
                                                      version,
                                                      MessageCounter.NextId,
                                                      RequestCounter.NextId,
                                                      community,
                                                      0,
                                                      maxRepetitions,
                                                      variables,
                                                      privacy,
                                                      MaxMessageSize,
                                                      report)
                                                : new GetBulkRequestMessage(
                                                      RequestCounter.NextId,
                                                      version,
                                                      community,
                                                      0,
                                                      maxRepetitions,
                                                      variables);
            var reply = request.GetResponse(timeout, receiver);
            if (reply is ReportMessage)
            {
                if (reply.Pdu().Variables.Count == 0)
                {
                    // TODO: whether it is good to return?
                    next = new List<Variable>(0);
                    return false;
                }

                var id = reply.Pdu().Variables[0].Id;
                if (id != IdNotInTimeWindow)
                {
                    // var error = id.GetErrorMessage();
                    // TODO: whether it is good to return?
                    next = new List<Variable>(0);
                    return false;
                }

                // according to RFC 3414, send a second request to sync time.
                request = new GetBulkRequestMessage(
                    version,
                    MessageCounter.NextId,
                    RequestCounter.NextId,
                    community,
                    0,
                    maxRepetitions,
                    variables,
                    privacy,
                    MaxMessageSize,
                    reply);
                reply = request.GetResponse(timeout, receiver);
            }
            else if (reply.Pdu().ErrorStatus.ToInt32() != 0)
            {
                throw ErrorException.Create(
                    "error in response",
                    receiver.Address,
                    reply);
            }

            next = reply.Pdu().Variables;
            report = request;
            return next.Count != 0;
        }
Пример #60
0
        public TrapV2Message(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, OctetString engineId, int engineBoots, int engineTime)
        {
            if (userName == null)
            {
                throw new ArgumentNullException("userName");
            }
            
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }
            
            if (version != VersionCode.V3)
            {
                throw new ArgumentException("only v3 is supported", "version");
            }

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

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

            Version = version;
            Privacy = privacy;
            Enterprise = enterprise;
            TimeStamp = time;

            Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel());
            var authenticationProvider = Privacy.AuthenticationProvider;
            Parameters = new SecurityParameters(
                engineId,
                new Integer32(engineBoots), 
                new Integer32(engineTime), 
                userName,
                authenticationProvider.CleanDigest,
                Privacy.Salt);
            var pdu = new TrapV2Pdu(
                requestId,
                enterprise,
                time,
                variables);
            
            // TODO: may expose engine ID in the future.
            Scope = new Scope(OctetString.Empty, OctetString.Empty, pdu);
            authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
            _bytes = this.PackMessage(null).ToBytes();
        }