Exemplo n.º 1
0
        //
        // Client side starts here, but server also loops through this method.
        //
        private void SendBlob(byte[] incoming, int count)
        {
            ProtocolToken message = _context.NextMessage(incoming, 0, count);

            _securityStatus = message.Status;

            if (message.Size != 0)
            {
                if (_context.IsServer && _CachedSession == CachedSessionStatus.Unknown)
                {
                    //
                    //[Schannel] If the first call to ASC returns a token less than 200 bytes,
                    //           then it's a reconnect (a handshake based on a cache entry).
                    //
                    _CachedSession = message.Size < 200 ? CachedSessionStatus.IsCached : CachedSessionStatus.IsNotCached;
                }

                if (_framing == Framing.Unified)
                {
                    _framing = DetectFraming(message.Payload, message.Payload.Length);
                }

                InnerStream.Write(message.Payload, 0, message.Size);
            }

            CheckCompletionBeforeNextReceive(message);
        }
Exemplo n.º 2
0
 internal void ProcessAuthentication(LazyAsyncResult lazyResult)
 {
     if (Interlocked.Exchange(ref this._NestedAuth, 1) == 1)
     {
         throw new InvalidOperationException(SR.GetString("net_io_invalidnestedcall", new object[] { (lazyResult == null) ? "BeginAuthenticate" : "Authenticate", "authenticate" }));
     }
     try
     {
         this.CheckThrow(false);
         AsyncProtocolRequest asyncRequest = null;
         if (lazyResult != null)
         {
             asyncRequest = new AsyncProtocolRequest(lazyResult)
             {
                 Buffer = null
             };
         }
         this._CachedSession = CachedSessionStatus.Unknown;
         this.ForceAuthentication(this.Context.IsServer, null, asyncRequest);
         if ((lazyResult == null) && Logging.On)
         {
             Logging.PrintInfo(Logging.Web, SR.GetString("net_log_sspi_selected_cipher_suite", new object[] { "ProcessAuthentication", this.SslProtocol, this.CipherAlgorithm, this.CipherStrength, this.HashAlgorithm, this.HashStrength, this.KeyExchangeAlgorithm, this.KeyExchangeStrength }));
         }
     }
     finally
     {
         if ((lazyResult == null) || (this._Exception != null))
         {
             this._NestedAuth = 0;
         }
     }
 }
Exemplo n.º 3
0
        //
        // This method assumes that a SSPI context is already in a good shape.
        // For example it is either a fresh context or already authenticated context that needs renegotiation.
        //
        private Task ProcessAuthentication(bool isAsync = false, bool isApm = false, CancellationToken cancellationToken = default)
        {
            Task result = null;

            if (Interlocked.Exchange(ref _nestedAuth, 1) == 1)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidnestedcall, isApm ? "BeginAuthenticate" : "Authenticate", "authenticate"));
            }

            try
            {
                ThrowIfExceptional();

                //  A trick to discover and avoid cached sessions.
                _CachedSession = CachedSessionStatus.Unknown;

                if (isAsync)
                {
                    result = ForceAuthenticationAsync(_context.IsServer, null, cancellationToken);
                }
                else
                {
                    ForceAuthentication(_context.IsServer, null);

                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Log.SspiSelectedCipherSuite(nameof(ProcessAuthentication),
                                                                   SslProtocol,
                                                                   CipherAlgorithm,
                                                                   CipherStrength,
                                                                   HashAlgorithm,
                                                                   HashStrength,
                                                                   KeyExchangeAlgorithm,
                                                                   KeyExchangeStrength);
                    }
                }
            }
            finally
            {
                // Operation has completed.
                _nestedAuth = 0;
            }

            return(result);
        }
Exemplo n.º 4
0
        private void StartSendBlob(byte[] incoming, int count, AsyncProtocolRequest asyncRequest)
        {
            ProtocolToken message = this.Context.NextMessage(incoming, 0, count);

            this._SecurityStatus = message.Status;
            if (message.Size != 0)
            {
                if (this.Context.IsServer && (this._CachedSession == CachedSessionStatus.Unknown))
                {
                    this._CachedSession = (message.Size < 200) ? CachedSessionStatus.IsCached : CachedSessionStatus.IsNotCached;
                }
                if (this._Framing == Framing.Unified)
                {
                    this._Framing = this.DetectFraming(message.Payload, message.Payload.Length);
                }
                if (((message.Done && this._ForceBufferingLastHandshakePayload) && ((this.InnerStream.GetType() == typeof(NetworkStream)) && !this._PendingReHandshake)) && !this.CheckWin9xCachedSession())
                {
                    this._LastPayload = message.Payload;
                }
                else if (asyncRequest == null)
                {
                    this.InnerStream.Write(message.Payload, 0, message.Size);
                }
                else
                {
                    asyncRequest.AsyncState = message;
                    IAsyncResult asyncResult = this.InnerStream.BeginWrite(message.Payload, 0, message.Size, _WriteCallback, asyncRequest);
                    if (!asyncResult.CompletedSynchronously)
                    {
                        return;
                    }
                    this.InnerStream.EndWrite(asyncResult);
                }
            }
            this.CheckCompletionBeforeNextReceive(message, asyncRequest);
        }
Exemplo n.º 5
0
        //
        // Client side starts here, but server also loops through this method.
        //
        private void StartSendBlob(byte[] incoming, int count, AsyncProtocolRequest asyncRequest)
        {
            ProtocolToken message = Context.NextMessage(incoming, 0, count);
            _securityStatus = message.Status;

            if (message.Size != 0)
            {
                if (Context.IsServer && _CachedSession == CachedSessionStatus.Unknown)
                {
                    //
                    //[Schannel] If the first call to ASC returns a token less than 200 bytes,
                    //           then it's a reconnect (a handshake based on a cache entry).
                    //
                    _CachedSession = message.Size < 200 ? CachedSessionStatus.IsCached : CachedSessionStatus.IsNotCached;
                }

                if (_Framing == Framing.Unified)
                {
                    _Framing = DetectFraming(message.Payload, message.Payload.Length);
                }

                if (asyncRequest == null)
                {
                    InnerStream.Write(message.Payload, 0, message.Size);
                }
                else
                {
                    asyncRequest.AsyncState = message;
                    IAsyncResult ar = InnerStreamAPM.BeginWrite(message.Payload, 0, message.Size, s_writeCallback, asyncRequest);
                    if (!ar.CompletedSynchronously)
                    {
#if DEBUG
                        asyncRequest._DebugAsyncChain = ar;
#endif
                        return;
                    }

                    InnerStreamAPM.EndWrite(ar);
                }
            }

            CheckCompletionBeforeNextReceive(message, asyncRequest);
        }
Exemplo n.º 6
0
        //
        // This method assumes that a SSPI context is already in a good shape.
        // For example it is either a fresh context or already authenticated context that needs renegotiation.
        //
        internal void ProcessAuthentication(LazyAsyncResult lazyResult)
        {
            if (Interlocked.Exchange(ref _nestedAuth, 1) == 1)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidnestedcall, lazyResult == null ? "BeginAuthenticate" : "Authenticate", "authenticate"));
            }

            try
            {
                CheckThrow(false);
                AsyncProtocolRequest asyncRequest = null;
                if (lazyResult != null)
                {
                    asyncRequest = new AsyncProtocolRequest(lazyResult);
                    asyncRequest.Buffer = null;
#if DEBUG
                    lazyResult._debugAsyncChain = asyncRequest;
#endif
                }

                //  A trick to discover and avoid cached sessions.
                _CachedSession = CachedSessionStatus.Unknown;

                ForceAuthentication(Context.IsServer, null, asyncRequest);

                // Not aync so the connection is completed at this point.
                if (lazyResult == null && SecurityEventSource.Log.IsEnabled())
                {
                    SecurityEventSource.Log.SspiSelectedCipherSuite("ProcessAuthentication",
                        SslProtocol,
                        CipherAlgorithm,
                        CipherStrength,
                        HashAlgorithm,
                        HashStrength,
                        KeyExchangeAlgorithm,
                        KeyExchangeStrength);
                }
            }
            catch (Exception)
            {
                // If an exception emerges synchronously, the asynchronous operation was not
                // initiated, so no operation is in progress.
                _nestedAuth = 0;
                throw;
            }
            finally
            {
                // For synchronous operations, the operation has completed.
                if (lazyResult == null)
                {
                    _nestedAuth = 0;
                }
            }
        }
Exemplo n.º 7
0
 private void Win9xSessionRestarted()
 {
     this._CachedSession = CachedSessionStatus.Renegotiated;
 }
Exemplo n.º 8
0
        //
        // This method assumes that a SSPI context is already in a good shape.
        // For example it is either a fresh context or already authenticated context that needs renegotiation.
        //
        internal void ProcessAuthentication(LazyAsyncResult lazyResult)
        {
            if (Interlocked.Exchange(ref _nestedAuth, 1) == 1)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidnestedcall, lazyResult == null ? "BeginAuthenticate" : "Authenticate", "authenticate"));
            }

            try
            {
                CheckThrow(false);
                AsyncProtocolRequest asyncRequest = null;
                if (lazyResult != null)
                {
                    asyncRequest = new AsyncProtocolRequest(lazyResult);
                    asyncRequest.Buffer = null;
#if DEBUG
                    lazyResult._DebugAsyncChain = asyncRequest;
#endif
                }

                //  A trick to discover and avoid cached sessions.
                _CachedSession = CachedSessionStatus.Unknown;

                ForceAuthentication(Context.IsServer, null, asyncRequest);

                // Not aync so the connection is completed at this point.
                if (lazyResult == null && Logging.On)
                {
                    Logging.PrintInfo(Logging.Web, SR.Format(SR.net_log_sspi_selected_cipher_suite,
                        "ProcessAuthentication",
                        SslProtocol,
                        CipherAlgorithm,
                        CipherStrength,
                        HashAlgorithm,
                        HashStrength,
                        KeyExchangeAlgorithm,
                        KeyExchangeStrength));
                }
            }
            finally
            {
                if (lazyResult == null || _exception != null)
                {
                    _nestedAuth = 0;
                }
            }
        }
Exemplo n.º 9
0
        //
        // Client side starts here, but server also loops through this method
        //
        private void StartSendBlob(byte[] incoming, int count, AsyncProtocolRequest asyncRequest)
        {
            ProtocolToken message = Context.NextMessage(incoming, 0, count);
            _SecurityStatus = message.Status;

            if (message.Size != 0)
            {
                if (Context.IsServer && _CachedSession == CachedSessionStatus.Unknown)
                {
                    //
                    //[Schannel] If the first call to ASC returns a token less than 200 bytes,
                    //           then it's a reconnect (a handshake based on a cache entry)
                    //
                    _CachedSession = message.Size < 200? CachedSessionStatus.IsCached: CachedSessionStatus.IsNotCached;
                }

                if (_Framing == Framing.Unified)
                {
                    _Framing = DetectFraming(message.Payload, message.Payload.Length);
                }

                // Even if we are comleted, there could be a blob for sending.
                // ONLY for TlsStream we want to delay it if the underlined stream is a NetworkStream that is subject to Nagle algorithm
                // 
                if ( message.Done && _ForceBufferingLastHandshakePayload && InnerStream.GetType() == typeof(NetworkStream) && !_PendingReHandshake)
                {
                    _LastPayload = message.Payload;
                }
                else
                {
                    if (asyncRequest == null)
                    {
                        InnerStream.Write(message.Payload, 0, message.Size);
                    }
                    else
                    {
                        asyncRequest.AsyncState = message;
                        IAsyncResult ar = InnerStream.BeginWrite(message.Payload, 0, message.Size, _WriteCallback, asyncRequest);
                        if (!ar.CompletedSynchronously)
                        {
#if DEBUG
                            asyncRequest._DebugAsyncChain = ar;
#endif
                            return;
                        }
                        InnerStream.EndWrite(ar);
                    }
                }
            }
            CheckCompletionBeforeNextReceive(message, asyncRequest);
        }
 private void StartSendBlob(byte[] incoming, int count, AsyncProtocolRequest asyncRequest)
 {
     ProtocolToken message = this.Context.NextMessage(incoming, 0, count);
     this._SecurityStatus = message.Status;
     if (message.Size != 0)
     {
         if (this.Context.IsServer && (this._CachedSession == CachedSessionStatus.Unknown))
         {
             this._CachedSession = (message.Size < 200) ? CachedSessionStatus.IsCached : CachedSessionStatus.IsNotCached;
         }
         if (this._Framing == Framing.Unified)
         {
             this._Framing = this.DetectFraming(message.Payload, message.Payload.Length);
         }
         if (((message.Done && this._ForceBufferingLastHandshakePayload) && ((this.InnerStream.GetType() == typeof(NetworkStream)) && !this._PendingReHandshake)) && !this.CheckWin9xCachedSession())
         {
             this._LastPayload = message.Payload;
         }
         else if (asyncRequest == null)
         {
             this.InnerStream.Write(message.Payload, 0, message.Size);
         }
         else
         {
             asyncRequest.AsyncState = message;
             IAsyncResult asyncResult = this.InnerStream.BeginWrite(message.Payload, 0, message.Size, _WriteCallback, asyncRequest);
             if (!asyncResult.CompletedSynchronously)
             {
                 return;
             }
             this.InnerStream.EndWrite(asyncResult);
         }
     }
     this.CheckCompletionBeforeNextReceive(message, asyncRequest);
 }
 internal void ProcessAuthentication(LazyAsyncResult lazyResult)
 {
     if (Interlocked.Exchange(ref this._NestedAuth, 1) == 1)
     {
         throw new InvalidOperationException(SR.GetString("net_io_invalidnestedcall", new object[] { (lazyResult == null) ? "BeginAuthenticate" : "Authenticate", "authenticate" }));
     }
     try
     {
         this.CheckThrow(false);
         AsyncProtocolRequest asyncRequest = null;
         if (lazyResult != null)
         {
             asyncRequest = new AsyncProtocolRequest(lazyResult) {
                 Buffer = null
             };
         }
         this._CachedSession = CachedSessionStatus.Unknown;
         this.ForceAuthentication(this.Context.IsServer, null, asyncRequest);
         if ((lazyResult == null) && Logging.On)
         {
             Logging.PrintInfo(Logging.Web, SR.GetString("net_log_sspi_selected_cipher_suite", new object[] { "ProcessAuthentication", this.SslProtocol, this.CipherAlgorithm, this.CipherStrength, this.HashAlgorithm, this.HashStrength, this.KeyExchangeAlgorithm, this.KeyExchangeStrength }));
         }
     }
     finally
     {
         if ((lazyResult == null) || (this._Exception != null))
         {
             this._NestedAuth = 0;
         }
     }
 }
 private void Win9xSessionRestarted()
 {
     this._CachedSession = CachedSessionStatus.Renegotiated;
 }