// IPrivacyProvider extension methods.
// Copyright (C) 2010 Lex Li
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*
* Created by SharpDevelop.
* User: lextm
* Date: 10/10/2010
* Time: 6:59 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace GSF.Net.Snmp.Security
{
///
/// Extension class for .
///
public static class PrivacyProviderExtension
{
///
/// Converts to .
///
/// Levels.
public static Levels ToSecurityLevel(this IPrivacyProvider privacy)
{
if (privacy == null)
{
throw new ArgumentNullException(nameof(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;
}
///
/// Gets the scope data.
///
/// The privacy provider.
/// The header.
/// The parameters.
/// The raw scope data.
/// ISnmpData.
///
/// privacy
/// or
/// header
///
public static ISnmpData GetScopeData(this IPrivacyProvider privacy, Header header, SecurityParameters parameters, ISnmpData rawScopeData)
{
if (privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
}
if (header == null)
{
throw new ArgumentNullException(nameof(header));
}
return Levels.Privacy == (header.SecurityLevel & Levels.Privacy)
? privacy.Encrypt(rawScopeData, parameters)
: rawScopeData;
}
///
/// Computes the hash.
///
/// The version.
/// The header.
/// The parameters.
/// The scope.
/// The privacy provider.
public static void ComputeHash(this IPrivacyProvider privacy, VersionCode version, Header header, SecurityParameters parameters, ISegment scope)
{
if (header == null)
{
throw new ArgumentNullException(nameof(header));
}
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
if (privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
}
IAuthenticationProvider provider = privacy.AuthenticationProvider;
if (provider is DefaultAuthenticationProvider)
{
return;
}
if (0 == (header.SecurityLevel & Levels.Authentication))
{
return;
}
ISnmpData scopeData = privacy.GetScopeData(header, parameters, scope.GetData(version));
parameters.AuthenticationParameters = provider.ComputeHash(version, header, parameters, scopeData, privacy, null); // replace the hash.
}
///
/// Verifies the hash.
///
/// The version.
/// The header.
/// The parameters.
/// The scope bytes.
/// The privacy provider.
/// The length bytes.
///
/// Returns true if hash matches. Otherwise, returns false.
///
public static bool VerifyHash(this IPrivacyProvider privacy, VersionCode version, Header header, SecurityParameters parameters, ISnmpData scopeBytes, byte[] length)
{
if (header == null)
{
throw new ArgumentNullException(nameof(header));
}
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
if (scopeBytes == null)
{
throw new ArgumentNullException(nameof(scopeBytes));
}
if (privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
}
IAuthenticationProvider provider = privacy.AuthenticationProvider;
if (provider is DefaultAuthenticationProvider)
{
return true;
}
if (0 == (header.SecurityLevel & Levels.Authentication))
{
return true;
}
OctetString expected = parameters.AuthenticationParameters;
parameters.AuthenticationParameters = provider.CleanDigest; // clean the hash first.
OctetString newHash = provider.ComputeHash(version, header, parameters, scopeBytes, privacy, length);
parameters.AuthenticationParameters = expected; // restore the hash.
return newHash == expected;
}
}
}