diff --git a/NSspi/Contexts/ClientContext.cs b/NSspi/Contexts/ClientContext.cs index a244634..06be5ba 100644 --- a/NSspi/Contexts/ClientContext.cs +++ b/NSspi/Contexts/ClientContext.cs @@ -114,15 +114,11 @@ namespace NSspi.Contexts if ( status == SecurityStatus.OK ) { - this.Initialized = true; + base.Initialize( rawExpiry.ToDateTime() ); outToken = null; - - this.Expiry = rawExpiry.ToDateTime(); } else if ( status == SecurityStatus.ContinueNeeded ) { - this.Initialized = false; - outToken = new byte[outTokenBuffer.Length]; Array.Copy( outTokenBuffer.Buffer, outToken, outToken.Length ); } diff --git a/NSspi/Contexts/Context.cs b/NSspi/Contexts/Context.cs index 4026f65..e763e26 100644 --- a/NSspi/Contexts/Context.cs +++ b/NSspi/Contexts/Context.cs @@ -26,11 +26,11 @@ namespace NSspi.Contexts /// /// Whether or not the context is fully formed. /// - public bool Initialized { get; protected set; } + public bool Initialized { get; private set; } protected Credential Credential { get; private set; } - public SafeContextHandle ContextHandle { get; protected set; } + public SafeContextHandle ContextHandle { get; private set; } public string AuthorityName { @@ -48,10 +48,20 @@ namespace NSspi.Contexts } } - public DateTime Expiry { get; protected set; } + public DateTime Expiry { get; private set; } public bool Disposed { get; private set; } + /// + /// Marks the context as having completed the initialization process, ie, exchanging of authentication tokens. + /// + /// The date and time that the context will expire. + protected void Initialize( DateTime expiry ) + { + this.Expiry = expiry; + this.Initialized = true; + } + public void Dispose() { Dispose( true ); diff --git a/NSspi/Contexts/ServerContext.cs b/NSspi/Contexts/ServerContext.cs index b3a2263..1fdbe54 100644 --- a/NSspi/Contexts/ServerContext.cs +++ b/NSspi/Contexts/ServerContext.cs @@ -36,8 +36,8 @@ namespace NSspi.Contexts public SecurityStatus AcceptToken( byte[] clientToken, out byte[] nextToken ) { - SecureBuffer clientBuffer = new SecureBuffer( clientToken, BufferType.Token ); - SecureBuffer outBuffer = new SecureBuffer( new byte[12288], BufferType.Token ); + SecureBuffer clientBuffer; + SecureBuffer outBuffer; SecurityStatus status; TimeStamp rawExpiry = new TimeStamp(); @@ -49,6 +49,15 @@ namespace NSspi.Contexts { throw new ObjectDisposedException( "ServerContext" ); } + else if( this.Initialized ) + { + throw new InvalidOperationException( + "Attempted to continue initialization of a ServerContext after initialization had completed." + ); + } + + clientBuffer = new SecureBuffer( clientToken, BufferType.Token ); + outBuffer = new SecureBuffer( new byte[12288], BufferType.Token ); using ( clientAdapter = new SecureBufferAdapter( clientBuffer ) ) { @@ -90,7 +99,8 @@ namespace NSspi.Contexts if ( status == SecurityStatus.OK ) { nextToken = null; - this.Initialized = true; + + base.Initialize( rawExpiry.ToDateTime() ); if ( outBuffer.Length != 0 ) { @@ -101,13 +111,9 @@ namespace NSspi.Contexts { nextToken = null; } - - this.Expiry = rawExpiry.ToDateTime(); } else if ( status == SecurityStatus.ContinueNeeded ) { - this.Initialized = false; - nextToken = new byte[outBuffer.Length]; Array.Copy( outBuffer.Buffer, nextToken, nextToken.Length ); }