Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNMPv3 over DTLS #133

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
210a3be
Merge pull request #1 from lextudio/master
kdurkin77 Nov 15, 2019
33d89e3
Merge pull request #2 from lextudio/master
kdurkin77 Dec 5, 2019
375dca0
Updated to use DTLS2.Net to allow for SNMPv3 over DTLS
kdurkin77 Dec 5, 2019
98a183d
Merge branch 'master' of https://github.com/kmiller77/sharpsnmplib
kdurkin77 Dec 5, 2019
ccb8536
Removed the project reference to DTLS2.Net and added the nuget package
kdurkin77 Dec 5, 2019
328f898
Forgot to remove my test project
kdurkin77 Dec 5, 2019
900037d
Removed the default keyword to allow for the Mac version to build pro…
kdurkin77 Dec 5, 2019
7007b74
Created a static Empty to use instead of the empty ctor for SecurityP…
kdurkin77 Dec 6, 2019
8319b80
Removed the Listeners and ListenerBindings (this needs to be moved in…
kdurkin77 Dec 6, 2019
2b42aa7
Added some unit testing and removed incorrect parameter comment in Ge…
kdurkin77 Dec 6, 2019
a11571a
Updating DTLS2.Net Version
kdurkin77 Dec 11, 2019
221da76
Updated GetBulkRequestMessage, GetNextRequestMessage, and SetRequestM…
kdurkin77 Dec 11, 2019
817c5b2
Forgot to update the test - will add more tests for the other message…
kdurkin77 Dec 11, 2019
2c47b81
Updated DTLS2.Net package - that update includes a timeout when conne…
kdurkin77 Dec 19, 2019
1927f5a
Added a timeout to connecting to the server in GetSecureResponse
kdurkin77 Jan 3, 2020
0d3b7a8
Created separate timeout for connection and response
kdurkin77 Jan 7, 2020
a7e33ce
Updated to the new DTLS2.Net library
kdurkin77 Jan 9, 2020
a4488c8
Upgraded DTLS2.Net library - using async
kdurkin77 Jan 14, 2020
c1e0a27
Merge branch 'master' of https://github.com/lextudio/sharpsnmplib
kdurkin77 Jan 14, 2020
cb8914b
Fixed from merge
kdurkin77 Jan 14, 2020
f10b0e9
Merge pull request #4 from lextudio/master
kdurkin77 Jan 16, 2020
5a9ef97
Updated DTLS2.Net
kdurkin77 Jan 17, 2020
2c50ccc
Merge pull request #5 from lextudio/master
kdurkin77 Jan 17, 2020
14c6417
Updated DTLS2.Net project
kdurkin77 Jan 17, 2020
44efc4c
Updating DTLS2.Net and no longer calling StopAsync since Client is no…
kdurkin77 Jan 20, 2020
8dee6aa
Updated the async functions to have the Async suffix
kdurkin77 Jan 28, 2020
bff3913
Updating DTLS library
kdurkin77 Jan 28, 2020
89f378c
Updated the DTLS2.Net library in an effort to help with high CPU with…
kdurkin77 Feb 3, 2020
39457b2
Merge pull request #6 from lextudio/master
kdurkin77 Feb 3, 2020
5adcd46
Added support for standard 2.1 and upgraded DTLS2.Net
kdurkin77 Feb 4, 2020
d27a0ec
Merge pull request #7 from lextudio/master
kdurkin77 Jun 11, 2020
fb4d8cf
Updated DTLS2.NET
kdurkin77 Jun 11, 2020
d849c60
Upgraded DLTS2.Net and added in missing .ConfigureAwait(false)
kdurkin77 Aug 12, 2020
18c2c72
Added ability to use TimeSpan for timeouts as well as int for millise…
kdurkin77 Aug 24, 2020
59ce5d9
Updated packages
kdurkin77 Aug 28, 2020
3d33735
Updated DTLS2.Net
kdurkin77 Sep 15, 2020
b677193
Updated DTLS.Net library
kdurkin77 Jul 1, 2021
4933bc7
Merge branch 'master' of https://github.com/lextudio/sharpsnmplib int…
kdurkin77 Feb 16, 2022
af04e83
Some cleanup based on warnings
kdurkin77 Feb 16, 2022
c9cc316
merged from upstream master
kdurkin77 May 10, 2024
dfed035
corrected the target frameworks and added net8.0
kdurkin77 May 10, 2024
a4c181d
updated packages
kdurkin77 May 10, 2024
2a55df2
Updated TargetFrameworks and Microsoft.SourceLink.GitHub and SonarAna…
kdurkin77 May 13, 2024
ed31d91
updated DTLS2.Net
kdurkin77 May 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions SharpSnmpLib/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,27 @@ public Header(ISnmpData data)
/// <param name="messageId">The message id.</param>
/// <param name="maxMessageSize">Size of the max message.</param>
/// <param name="securityLevel">The security level.</param>
/// <param name="securityModel">The security model.</param>
/// <remarks>If you want an empty header, please use <see cref="Empty"/>.</remarks>
public Header(Integer32? messageId, Integer32 maxMessageSize, Levels securityLevel)
public Header(Integer32? messageId, Integer32 maxMessageSize, Levels securityLevel, Integer32 securityModel)
{
_messageId = messageId;
_messageId = messageId;
_maxSize = maxMessageSize ?? throw new ArgumentNullException(nameof(maxMessageSize));
SecurityLevel = securityLevel;
_flags = new OctetString(SecurityLevel);
_securityModel = DefaultSecurityModel;
_securityModel = securityModel;
}

/// <summary>
/// Initializes a new instance of the <see cref="Header"/> class.
/// </summary>
/// <param name="messageId">The message id.</param>
/// <param name="maxMessageSize">Size of the max message.</param>
/// <param name="securityLevel">The security level.</param>
/// <remarks>If you want an empty header, please use <see cref="Empty"/>.</remarks>
public Header(Integer32? messageId, Integer32 maxMessageSize, Levels securityLevel)
: this(messageId, maxMessageSize, securityLevel, DefaultSecurityModel)
{
}

/// <summary>
Expand All @@ -102,6 +115,15 @@ public Header(Integer32? messageId, Integer32 maxMessageSize, Levels securityLev
/// <value>The message ID.</value>
public int MessageId => _messageId == null ? throw new InvalidOperationException() : _messageId.ToInt32();

/// <summary>
/// Gets the security model.
/// </summary>
/// <value>The security model.</value>
public SecurityModel SecurityModel
{
get { return (SecurityModel)_securityModel.ToInt32(); }
}

#region ISegment Members

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions SharpSnmpLib/Messaging/Discoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress
using var udp = new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);
udp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
var buffer = new ArraySegment<byte>(bytes);
await udp.SendToAsync(buffer, SocketFlags.None, broadcastAddress);
await udp.SendToAsync(buffer, SocketFlags.None, broadcastAddress).ConfigureAwait(false);

var activeBefore = Interlocked.CompareExchange(ref _active, Active, Inactive);
if (activeBefore == Active)
Expand All @@ -341,7 +341,8 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress
#else
await Task.WhenAny(
ReceiveAsync(udp),
Task.Delay(interval));
Task.Delay(interval))
.ConfigureAwait(false);
#endif
Interlocked.CompareExchange(ref _active, Inactive, Active);
try
Expand Down Expand Up @@ -370,7 +371,7 @@ private async Task ReceiveAsync(Socket socket)
EndPoint remote = new IPEndPoint(IPAddress.Any, 0);

var buffer = new byte[_bufferSize];
var result = await socket.ReceiveMessageFromAsync(new ArraySegment<byte>(buffer), SocketFlags.None, remote);
var result = await socket.ReceiveMessageFromAsync(new ArraySegment<byte>(buffer), SocketFlags.None, remote).ConfigureAwait(false);
await Task.Factory.StartNew(() => HandleMessage(buffer, result.ReceivedBytes, (IPEndPoint) result.RemoteEndPoint))
.ConfigureAwait(false);
}
Expand Down
109 changes: 100 additions & 9 deletions SharpSnmpLib/Messaging/GetBulkRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,69 @@ public GetBulkRequestMessage(VersionCode version, int messageId, int requestId,
{
}

/// <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();
}

/// <summary>
/// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
/// </summary>
Expand All @@ -121,7 +184,8 @@ public GetBulkRequestMessage(VersionCode version, int messageId, int requestId,
/// <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)
/// <param name="securityModel">The security model.</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, SecurityModel securityModel)
{
if (variables == null)
{
Expand Down Expand Up @@ -160,16 +224,25 @@ public GetBulkRequestMessage(VersionCode version, int messageId, int requestId,

Version = version;
Privacy = privacy ?? throw new ArgumentNullException(nameof(privacy));
Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable, new Integer32((int)securityModel));
var parameters = report.Parameters;
var authenticationProvider = Privacy.AuthenticationProvider;
Parameters = new SecurityParameters(
parameters.EngineId,
parameters.EngineBoots,
parameters.EngineTime,
userName,
authenticationProvider.CleanDigest,
Privacy.Salt);

if (securityModel == SecurityModel.Tsm)
{
Parameters = SecurityParameters.Empty;
}
else
{
Parameters = new SecurityParameters(
parameters.EngineId,
parameters.EngineBoots,
parameters.EngineTime,
userName,
authenticationProvider.CleanDigest,
Privacy.Salt);
}

var pdu = new GetBulkRequestPdu(
requestId,
nonRepeaters,
Expand All @@ -188,6 +261,24 @@ public GetBulkRequestMessage(VersionCode version, int messageId, int requestId,
_bytes = this.PackMessage(null).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="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>
/// <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)
: this(version, messageId, requestId, userName, contextName, nonRepeaters, maxRepetitions, variables, privacy, maxMessageSize, report, SecurityModel.Usm)
{
}

/// <summary>
/// Creates a <see cref="GetBulkRequestMessage"/> with a specific <see cref="Sequence"/>.
Expand Down
91 changes: 82 additions & 9 deletions SharpSnmpLib/Messaging/GetNextRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,53 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId,
{
}

/// <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="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>
public GetNextRequestMessage(VersionCode version, int messageId, int requestId, OctetString contextName, 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));
}

Version = version;
Privacy = privacy;
Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable, new Integer32((int)SecurityModel.Tsm));
Parameters = SecurityParameters.Empty;

var pdu = new GetNextRequestPdu(
requestId,
variables);
var contextEngineId = OctetString.Empty;
Scope = new Scope(contextEngineId, contextName, pdu);

Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(null).ToBytes();
}

/// <summary>
/// Initializes a new instance of the <see cref="GetNextRequestMessage"/> class.
/// </summary>
Expand All @@ -104,7 +151,8 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId,
/// <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)
/// <param name="securityModel">The security model</param>
public GetNextRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, OctetString contextName, IList<Variable> variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report, SecurityModel securityModel)
{
if (variables == null)
{
Expand Down Expand Up @@ -134,16 +182,25 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId,
Version = version;
Privacy = privacy ?? throw new ArgumentNullException(nameof(privacy));

Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable, new Integer32((int)securityModel));
var parameters = report.Parameters;
var authenticationProvider = Privacy.AuthenticationProvider;
Parameters = new SecurityParameters(
parameters.EngineId,
parameters.EngineBoots,
parameters.EngineTime,
userName,
authenticationProvider.CleanDigest,
Privacy.Salt);

if (securityModel == SecurityModel.Tsm)
{
Parameters = SecurityParameters.Empty;
}
else
{
Parameters = new SecurityParameters(
parameters.EngineId,
parameters.EngineBoots,
parameters.EngineTime,
userName,
authenticationProvider.CleanDigest,
Privacy.Salt);
}

var pdu = new GetNextRequestPdu(
requestId,
variables);
Expand All @@ -160,6 +217,22 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId,
_bytes = this.PackMessage(null).ToBytes();
}

/// <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)
: this(version, messageId, requestId, userName, contextName, variables, privacy, maxMessageSize, report, SecurityModel.Usm)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="GetNextRequestMessage"/> class.
Expand Down
Loading