From cec7b8b88fec9fde3885cd898e371b85cd699427 Mon Sep 17 00:00:00 2001 From: antiduh Date: Sun, 29 Jun 2014 16:39:42 +0000 Subject: [PATCH] Switch the rest of the API to the new TimeStamp structure. Fix the TimeStamp structure to handle dates that are past the upper limit of DateTime.MaxValue. --- NSspi/Contexts/ClientContext.cs | 4 ++-- NSspi/Contexts/ContextNativeMethods.cs | 8 +++---- NSspi/Contexts/ServerContext.cs | 4 ++-- NSspi/Credentials/Credential.cs | 4 ++-- NSspi/Credentials/CredentialNativeMethods.cs | 2 +- NSspi/TimeStamp.cs | 22 +++++++++++++++++--- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/NSspi/Contexts/ClientContext.cs b/NSspi/Contexts/ClientContext.cs index c68799d..7ebbcb5 100644 --- a/NSspi/Contexts/ClientContext.cs +++ b/NSspi/Contexts/ClientContext.cs @@ -24,7 +24,7 @@ namespace NSspi.Contexts public SecurityStatus Init( byte[] serverToken, out byte[] outToken ) { - long rawExpiry = 0; + TimeStamp rawExpiry = new TimeStamp(); SecurityStatus status; @@ -112,7 +112,7 @@ namespace NSspi.Contexts this.Initialized = true; outToken = null; - this.Expiry = TimeStamp.Calc( rawExpiry ); + this.Expiry = rawExpiry.ToDateTime(); } else if ( status == SecurityStatus.ContinueNeeded ) { diff --git a/NSspi/Contexts/ContextNativeMethods.cs b/NSspi/Contexts/ContextNativeMethods.cs index 462a07f..5f3d330 100644 --- a/NSspi/Contexts/ContextNativeMethods.cs +++ b/NSspi/Contexts/ContextNativeMethods.cs @@ -52,7 +52,7 @@ namespace NSspi.Contexts ref RawSspiHandle newContextHandle, IntPtr outputBuffer, ref ContextAttrib outputAttribs, - ref long expiry + ref TimeStamp expiry ); @@ -66,7 +66,7 @@ namespace NSspi.Contexts ref RawSspiHandle newContextHandle, IntPtr outputBuffer, ref ContextAttrib outputAttribs, - ref long expiry + ref TimeStamp expiry ); @@ -83,7 +83,7 @@ namespace NSspi.Contexts ref RawSspiHandle newContextHandle, IntPtr outputBuffer, ref ContextAttrib contextAttribs, - ref long expiry + ref TimeStamp expiry ); @@ -100,7 +100,7 @@ namespace NSspi.Contexts ref RawSspiHandle newContextHandle, IntPtr outputBuffer, ref ContextAttrib contextAttribs, - ref long expiry + ref TimeStamp expiry ); [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success )] diff --git a/NSspi/Contexts/ServerContext.cs b/NSspi/Contexts/ServerContext.cs index 9f80768..9de26ea 100644 --- a/NSspi/Contexts/ServerContext.cs +++ b/NSspi/Contexts/ServerContext.cs @@ -30,7 +30,7 @@ namespace NSspi.Contexts SecureBuffer outBuffer = new SecureBuffer( new byte[12288], BufferType.Token ); SecurityStatus status; - long rawExpiry = 0; + TimeStamp rawExpiry = new TimeStamp(); SecureBufferAdapter clientAdapter; SecureBufferAdapter outAdapter; @@ -87,7 +87,7 @@ namespace NSspi.Contexts nextToken = null; } - this.Expiry = TimeStamp.Calc( rawExpiry ); + this.Expiry = rawExpiry.ToDateTime(); InitProviderCapabilities(); } diff --git a/NSspi/Credentials/Credential.cs b/NSspi/Credentials/Credential.cs index 55a8df0..ff3db8f 100644 --- a/NSspi/Credentials/Credential.cs +++ b/NSspi/Credentials/Credential.cs @@ -36,7 +36,7 @@ namespace NSspi.Credentials { string packageName; CredentialUse use; - long rawExpiry = 0; + TimeStamp rawExpiry = new TimeStamp(); // -- Package -- if ( package == SecurityPackage.Kerberos ) @@ -101,7 +101,7 @@ namespace NSspi.Credentials throw new SSPIException( "Failed to call AcquireCredentialHandle", status ); } - this.expiry = TimeStamp.Calc( rawExpiry ); + this.expiry = rawExpiry.ToDateTime(); } ~Credential() diff --git a/NSspi/Credentials/CredentialNativeMethods.cs b/NSspi/Credentials/CredentialNativeMethods.cs index 7305e46..36ad8d2 100644 --- a/NSspi/Credentials/CredentialNativeMethods.cs +++ b/NSspi/Credentials/CredentialNativeMethods.cs @@ -47,7 +47,7 @@ namespace NSspi.Credentials IntPtr getKeyFunc, IntPtr getKeyData, ref RawSspiHandle credentialHandle, - ref long expiry + ref TimeStamp expiry ); [ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )] diff --git a/NSspi/TimeStamp.cs b/NSspi/TimeStamp.cs index 32ece40..1521545 100644 --- a/NSspi/TimeStamp.cs +++ b/NSspi/TimeStamp.cs @@ -1,18 +1,34 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace NSspi { - public static class TimeStamp + [StructLayout( LayoutKind.Sequential )] + public struct TimeStamp { public static readonly DateTime Epoch = new DateTime( 1601, 1, 1, 0, 0, 0, DateTimeKind.Utc ); - public static DateTime Calc( long rawExpiry ) + private long time; + + public DateTime ToDateTime() { - return TimeStamp.Epoch.AddTicks( rawExpiry ); + ulong test = (ulong)this.time + (ulong)(Epoch.Ticks); + + // Sometimes the value returned is massive, eg, 0x7fffff154e84ffff, which is a value + // somewhere in the year 30848. This would overflow DateTime, since it peaks at 31-Dec-9999. + // http://stackoverflow.com/questions/24478056/ + if ( test > (ulong)DateTime.MaxValue.Ticks ) + { + return DateTime.MaxValue; + } + else + { + return DateTime.FromFileTimeUtc( this.time ); + } } } }