128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
// /**
|
|
// * File: USMMessage.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using ln.snmp.asn1;
|
|
using System.Linq;
|
|
using ln.snmp.endpoint;
|
|
namespace ln.snmp.types
|
|
{
|
|
public class USMMessage : SnmpMessage
|
|
{
|
|
public SnmpV3AuthMethod AuthMethod { get; set; }
|
|
public SnmpV3PrivMethod PrivMethod { get; set; }
|
|
|
|
public Integer msgVersion { get; } = new Integer(3);
|
|
public MsgGlobalData msgGlobalData { get; set; }
|
|
public UsmSecurityParameters SecurityParameters { get; set; }
|
|
|
|
public ScopedPDU ScopedPDU { get; set; }
|
|
public OctetString EncryptedPDU { get; set; }
|
|
|
|
public Variable msgData { get; set; }
|
|
|
|
public override PDU snmpPDU
|
|
{
|
|
get => ScopedPDU?.PDU;
|
|
set => ScopedPDU.PDU = value;
|
|
}
|
|
|
|
public USMMessage()
|
|
: base(SnmpVersion.V3)
|
|
{
|
|
msgGlobalData = new MsgGlobalData();
|
|
SecurityParameters = new UsmSecurityParameters();
|
|
msgData = new NullValue();
|
|
}
|
|
|
|
public USMMessage(ASN1Value asn)
|
|
:this()
|
|
{
|
|
Items = asn.Items.Select((x) => (Variable)x).ToArray();
|
|
}
|
|
|
|
public override Variable[] Items
|
|
{
|
|
get => new Variable[] {
|
|
msgVersion,
|
|
msgGlobalData,
|
|
new OctetString(((ASN1Value)SecurityParameters).AsByteArray),
|
|
msgData
|
|
};
|
|
set
|
|
{
|
|
msgGlobalData = new MsgGlobalData(value[1]);
|
|
SecurityParameters = new UsmSecurityParameters(new ASN1Value(value[2].Bytes));
|
|
|
|
if (value[3].Identifier.Is(IdentifierClass.UNIVERSAL,false,0x04))
|
|
{
|
|
EncryptedPDU = value[3] as OctetString;
|
|
msgData = EncryptedPDU;
|
|
} else {
|
|
ScopedPDU = new ScopedPDU(value[3]);
|
|
msgData = ScopedPDU;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override int MessageID
|
|
{
|
|
get => (int)msgGlobalData.msgID.LongValue;
|
|
set => msgGlobalData.msgID.LongValue = value;
|
|
}
|
|
|
|
public override void Add(Variable item)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
public override void Remove(Variable item)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public class MsgGlobalData : AbstractSequence
|
|
{
|
|
public Integer msgID { get; set; }
|
|
public Integer msgMaxSize { get; set; }
|
|
public OctetString msgFlags { get; set; }
|
|
public Integer msgSecurityModel { get; set; }
|
|
|
|
public MsgGlobalData()
|
|
:base(new Identifier(IdentifierClass.UNIVERSAL,true,0x10))
|
|
{
|
|
msgID = new Integer((int)(DateTimeOffset.Now.ToUnixTimeSeconds()));
|
|
msgMaxSize = new Integer(65000);
|
|
msgFlags = new OctetString(new byte[] { 0x04 });
|
|
msgSecurityModel = new Integer(3);
|
|
}
|
|
public MsgGlobalData(ASN1Value asn)
|
|
:this()
|
|
{
|
|
Items = asn.Items.ToVariableArray();
|
|
}
|
|
|
|
public override void Add(Variable item) => throw new NotImplementedException();
|
|
public override void Remove(Variable item) => throw new NotImplementedException();
|
|
|
|
public override Variable[] Items
|
|
{
|
|
get => new Variable[] { msgID,msgMaxSize,msgFlags,msgSecurityModel };
|
|
set
|
|
{
|
|
msgID = value[0] as Integer;
|
|
msgMaxSize = value[1] as Integer;
|
|
msgFlags = value[2] as OctetString;
|
|
msgSecurityModel = value[3] as Integer;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|