Convert project to .NET 4.8 and add Wrap and Unwrap functionality to Encrypt and Decrypt messages

This commit is contained in:
2023-10-06 09:28:43 -04:00
parent 3a7629fada
commit 1bfa2f8ce4
18 changed files with 53 additions and 36 deletions

View File

@@ -83,6 +83,11 @@ namespace NSspi.Contexts
/// </summary> /// </summary>
public bool Disposed { get; private set; } public bool Disposed { get; private set; }
/// <summary>
/// Constant for wrapping only...no encryption
/// </summary>
protected uint KERB_WRAP_NO_ENCRYPT = 0x80000001;
/// <summary> /// <summary>
/// Marks the context as having completed the initialization process, ie, exchanging of authentication tokens. /// Marks the context as having completed the initialization process, ie, exchanging of authentication tokens.
/// </summary> /// </summary>
@@ -230,8 +235,9 @@ namespace NSspi.Contexts
/// - The padding buffer. /// - The padding buffer.
/// </remarks> /// </remarks>
/// <param name="input">The raw message to encrypt.</param> /// <param name="input">The raw message to encrypt.</param>
/// <param name="wrapOnly">Only wrap the message, no encryption</param>
/// <returns>The packed and encrypted message.</returns> /// <returns>The packed and encrypted message.</returns>
public byte[] Encrypt( byte[] input ) public byte[] Encrypt( byte[] input, bool wrapOnly = false )
{ {
// The message is encrypted in place in the buffer we provide to Win32 EncryptMessage // The message is encrypted in place in the buffer we provide to Win32 EncryptMessage
SecPkgContext_Sizes sizes; SecPkgContext_Sizes sizes;
@@ -256,9 +262,14 @@ namespace NSspi.Contexts
using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) ) using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) )
{ {
uint qualityOfProtection = 0u;
if (wrapOnly)
{
qualityOfProtection = KERB_WRAP_NO_ENCRYPT;
}
status = ContextNativeMethods.SafeEncryptMessage( status = ContextNativeMethods.SafeEncryptMessage(
this.ContextHandle, this.ContextHandle,
0, qualityOfProtection,
adapter, adapter,
0 0
); );
@@ -312,8 +323,9 @@ namespace NSspi.Contexts
/// - The padding buffer. /// - The padding buffer.
/// </remarks> /// </remarks>
/// <param name="input">The packed and encrypted data.</param> /// <param name="input">The packed and encrypted data.</param>
/// <param name="unwrapOnly">Only wrap the message, no encryption</param>
/// <returns>The original plaintext message.</returns> /// <returns>The original plaintext message.</returns>
public byte[] Decrypt( byte[] input ) public byte[] Decrypt( byte[] input, bool unwrapOnly = false)
{ {
SecPkgContext_Sizes sizes; SecPkgContext_Sizes sizes;
@@ -393,9 +405,14 @@ namespace NSspi.Contexts
using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) ) using( adapter = new SecureBufferAdapter( new[] { trailerBuffer, dataBuffer, paddingBuffer } ) )
{ {
uint qualityOfProtection = 0u;
if (unwrapOnly)
{
qualityOfProtection = KERB_WRAP_NO_ENCRYPT;
}
status = ContextNativeMethods.SafeDecryptMessage( status = ContextNativeMethods.SafeDecryptMessage(
this.ContextHandle, this.ContextHandle,
0, qualityOfProtection,
adapter, adapter,
0 0
); );

View File

@@ -106,9 +106,9 @@ namespace NSspi.Contexts
[DllImport( "Secur32.dll", EntryPoint = "EncryptMessage", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "EncryptMessage", CharSet = CharSet.Unicode )]
internal static extern SecurityStatus EncryptMessage( internal static extern SecurityStatus EncryptMessage(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
int qualityOfProtection, uint qualityOfProtection,
IntPtr bufferDescriptor, IntPtr bufferDescriptor,
int sequenceNumber uint sequenceNumber
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )] [ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )]
@@ -116,17 +116,17 @@ namespace NSspi.Contexts
internal static extern SecurityStatus DecryptMessage( internal static extern SecurityStatus DecryptMessage(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
IntPtr bufferDescriptor, IntPtr bufferDescriptor,
int sequenceNumber, uint sequenceNumber,
int qualityOfProtection uint qualityOfProtection
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )] [ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )]
[DllImport( "Secur32.dll", EntryPoint = "MakeSignature", CharSet = CharSet.Unicode )] [DllImport( "Secur32.dll", EntryPoint = "MakeSignature", CharSet = CharSet.Unicode )]
internal static extern SecurityStatus MakeSignature( internal static extern SecurityStatus MakeSignature(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
int qualityOfProtection, uint qualityOfProtection,
IntPtr bufferDescriptor, IntPtr bufferDescriptor,
int sequenceNumber uint sequenceNumber
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )] [ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )]
@@ -134,8 +134,8 @@ namespace NSspi.Contexts
internal static extern SecurityStatus VerifySignature( internal static extern SecurityStatus VerifySignature(
ref RawSspiHandle contextHandle, ref RawSspiHandle contextHandle,
IntPtr bufferDescriptor, IntPtr bufferDescriptor,
int sequenceNumber, uint sequenceNumber,
int qualityOfProtection uint qualityOfProtection
); );
[ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )] [ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )]
@@ -255,9 +255,9 @@ namespace NSspi.Contexts
/// <returns></returns> /// <returns></returns>
internal static SecurityStatus SafeEncryptMessage( internal static SecurityStatus SafeEncryptMessage(
SafeContextHandle handle, SafeContextHandle handle,
int qualityOfProtection, uint qualityOfProtection,
SecureBufferAdapter bufferAdapter, SecureBufferAdapter bufferAdapter,
int sequenceNumber ) uint sequenceNumber )
{ {
SecurityStatus status = SecurityStatus.InternalError; SecurityStatus status = SecurityStatus.InternalError;
bool gotRef = false; bool gotRef = false;
@@ -306,9 +306,9 @@ namespace NSspi.Contexts
/// <returns></returns> /// <returns></returns>
internal static SecurityStatus SafeDecryptMessage( internal static SecurityStatus SafeDecryptMessage(
SafeContextHandle handle, SafeContextHandle handle,
int qualityOfProtection, uint qualityOfProtection,
SecureBufferAdapter bufferAdapter, SecureBufferAdapter bufferAdapter,
int sequenceNumber ) uint sequenceNumber )
{ {
SecurityStatus status = SecurityStatus.InvalidHandle; SecurityStatus status = SecurityStatus.InvalidHandle;
bool gotRef = false; bool gotRef = false;
@@ -357,9 +357,9 @@ namespace NSspi.Contexts
/// <returns></returns> /// <returns></returns>
internal static SecurityStatus SafeMakeSignature( internal static SecurityStatus SafeMakeSignature(
SafeContextHandle handle, SafeContextHandle handle,
int qualityOfProtection, uint qualityOfProtection,
SecureBufferAdapter adapter, SecureBufferAdapter adapter,
int sequenceNumber ) uint sequenceNumber )
{ {
bool gotRef = false; bool gotRef = false;
SecurityStatus status = SecurityStatus.InternalError; SecurityStatus status = SecurityStatus.InternalError;
@@ -408,9 +408,9 @@ namespace NSspi.Contexts
/// <returns></returns> /// <returns></returns>
internal static SecurityStatus SafeVerifySignature( internal static SecurityStatus SafeVerifySignature(
SafeContextHandle handle, SafeContextHandle handle,
int qualityOfProtection, uint qualityOfProtection,
SecureBufferAdapter adapter, SecureBufferAdapter adapter,
int sequenceNumber ) uint sequenceNumber )
{ {
bool gotRef = false; bool gotRef = false;
SecurityStatus status = SecurityStatus.InternalError; SecurityStatus status = SecurityStatus.InternalError;

View File

@@ -1,6 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup> </startup>
</configuration> </configuration>

View File

@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NsspiDemo</RootNamespace> <RootNamespace>NsspiDemo</RootNamespace>
<AssemblyName>NsspiDemo</AssemblyName> <AssemblyName>NsspiDemo</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>

View File

@@ -19,7 +19,7 @@ namespace NsspiDemo.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources { internal class Resources {

View File

@@ -12,7 +12,7 @@ namespace NsspiDemo.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@@ -1,6 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup> </startup>
</configuration> </configuration>

View File

@@ -19,7 +19,7 @@ namespace TestClient.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources { internal class Resources {

View File

@@ -12,7 +12,7 @@ namespace TestClient.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestClient</RootNamespace> <RootNamespace>TestClient</RootNamespace>
<AssemblyName>TestClient</AssemblyName> <AssemblyName>TestClient</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup> </startup>
</configuration> </configuration>

View File

@@ -19,7 +19,7 @@ namespace TestProtocol.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources { internal class Resources {

View File

@@ -12,7 +12,7 @@ namespace TestProtocol.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestProtocol</RootNamespace> <RootNamespace>TestProtocol</RootNamespace>
<AssemblyName>TestProtocol</AssemblyName> <AssemblyName>TestProtocol</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<configuration> <configuration>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup> </startup>
</configuration> </configuration>

View File

@@ -19,7 +19,7 @@ namespace TestServer.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources { internal class Resources {

View File

@@ -12,7 +12,7 @@ namespace TestServer.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestServer</RootNamespace> <RootNamespace>TestServer</RootNamespace>
<AssemblyName>TestServer</AssemblyName> <AssemblyName>TestServer</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>