예제 #1
0
        private bool BeginNegotiateHandshake(SslStreamBase.InternalAsyncResult asyncResult)
        {
            bool result;

            try
            {
                object obj = this.negotiate;
                lock (obj)
                {
                    if (this.context.HandshakeState == HandshakeState.None)
                    {
                        this.OnBeginNegotiateHandshake(new AsyncCallback(this.AsyncHandshakeCallback), asyncResult);
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            catch (TlsException ex)
            {
                this.negotiationComplete.Set();
                this.protocol.SendAlert(ex.Alert);
                throw new IOException("The authentication or decryption has failed.", ex);
            }
            catch (Exception innerException)
            {
                this.negotiationComplete.Set();
                this.protocol.SendAlert(AlertDescription.InternalError);
                throw new IOException("The authentication or decryption has failed.", innerException);
            }
            return(result);
        }
예제 #2
0
 private bool BeginNegotiateHandshake(SslStreamBase.InternalAsyncResult asyncResult)
 {
     try
     {
         lock (this.negotiate)
         {
             if (this.context.HandshakeState != HandshakeState.None)
             {
                 return(false);
             }
             this.OnBeginNegotiateHandshake(new AsyncCallback(this.AsyncHandshakeCallback), (object)asyncResult);
             return(true);
         }
     }
     catch (TlsException ex)
     {
         this.negotiationComplete.Set();
         this.protocol.SendAlert(ex.Alert);
         throw new IOException("The authentication or decryption has failed.", (Exception)ex);
     }
     catch (Exception ex)
     {
         this.negotiationComplete.Set();
         this.protocol.SendAlert(AlertDescription.InternalError);
         throw new IOException("The authentication or decryption has failed.", ex);
     }
 }
예제 #3
0
 private void EndNegotiateHandshake(SslStreamBase.InternalAsyncResult asyncResult)
 {
     if (!asyncResult.IsCompleted)
     {
         asyncResult.AsyncWaitHandle.WaitOne();
     }
     if (asyncResult.CompletedWithError)
     {
         throw asyncResult.AsyncException;
     }
 }
예제 #4
0
 internal void NegotiateHandshake()
 {
     if (this.MightNeedHandshake)
     {
         SslStreamBase.InternalAsyncResult asyncResult = new SslStreamBase.InternalAsyncResult(null, null, null, 0, 0, false, false);
         if (!this.BeginNegotiateHandshake(asyncResult))
         {
             this.negotiationComplete.WaitOne();
         }
         else
         {
             this.EndNegotiateHandshake(asyncResult);
         }
     }
 }
예제 #5
0
 internal void NegotiateHandshake()
 {
     if (!this.MightNeedHandshake)
     {
         return;
     }
     SslStreamBase.InternalAsyncResult asyncResult = new SslStreamBase.InternalAsyncResult((AsyncCallback)null, (object)null, (byte[])null, 0, 0, false, false);
     if (!this.BeginNegotiateHandshake(asyncResult))
     {
         this.negotiationComplete.WaitOne();
     }
     else
     {
         this.EndNegotiateHandshake(asyncResult);
     }
 }
예제 #6
0
 private void InternalWriteCallback(IAsyncResult ar)
 {
     if (this.disposed)
     {
         return;
     }
     SslStreamBase.InternalAsyncResult asyncState = (SslStreamBase.InternalAsyncResult)ar.AsyncState;
     try
     {
         this.innerStream.EndWrite(ar);
         asyncState.SetComplete();
     }
     catch (Exception ex)
     {
         asyncState.SetComplete(ex);
     }
 }
예제 #7
0
 public override void EndWrite(IAsyncResult asyncResult)
 {
     this.checkDisposed();
     SslStreamBase.InternalAsyncResult internalAsyncResult = asyncResult as SslStreamBase.InternalAsyncResult;
     if (internalAsyncResult == null)
     {
         throw new ArgumentNullException("asyncResult is null or was not obtained by calling BeginWrite.");
     }
     if (!asyncResult.IsCompleted && !internalAsyncResult.AsyncWaitHandle.WaitOne(300000, false))
     {
         throw new TlsException(AlertDescription.InternalError, "Couldn't complete EndWrite");
     }
     if (internalAsyncResult.CompletedWithError)
     {
         throw internalAsyncResult.AsyncException;
     }
 }
예제 #8
0
 private void InternalBeginRead(SslStreamBase.InternalAsyncResult asyncResult)
 {
     try
     {
         int    num = 0;
         object obj = this.read;
         lock (obj)
         {
             bool flag  = this.inputBuffer.Position == this.inputBuffer.Length && this.inputBuffer.Length > 0L;
             bool flag2 = this.inputBuffer.Length > 0L && asyncResult.Count > 0;
             if (flag)
             {
                 this.resetBuffer();
             }
             else if (flag2)
             {
                 num = this.inputBuffer.Read(asyncResult.Buffer, asyncResult.Offset, asyncResult.Count);
             }
         }
         if (0 < num)
         {
             asyncResult.SetComplete(num);
         }
         else if (!this.context.ReceivedConnectionEnd)
         {
             this.innerStream.BeginRead(this.recbuf, 0, this.recbuf.Length, new AsyncCallback(this.InternalReadCallback), new object[]
             {
                 this.recbuf,
                 asyncResult
             });
         }
         else
         {
             asyncResult.SetComplete(0);
         }
     }
     catch (TlsException ex)
     {
         this.protocol.SendAlert(ex.Alert);
         throw new IOException("The authentication or decryption has failed.", ex);
     }
     catch (Exception innerException)
     {
         throw new IOException("IO exception during read.", innerException);
     }
 }
예제 #9
0
 public override IAsyncResult BeginWrite(
     byte[] buffer,
     int offset,
     int count,
     AsyncCallback callback,
     object state)
 {
     this.checkDisposed();
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer is a null reference.");
     }
     if (offset < 0)
     {
         throw new ArgumentOutOfRangeException("offset is less than 0.");
     }
     if (offset > buffer.Length)
     {
         throw new ArgumentOutOfRangeException("offset is greater than the length of buffer.");
     }
     if (count < 0)
     {
         throw new ArgumentOutOfRangeException("count is less than 0.");
     }
     if (count > buffer.Length - offset)
     {
         throw new ArgumentOutOfRangeException("count is less than the length of buffer minus the value of the offset parameter.");
     }
     SslStreamBase.InternalAsyncResult asyncResult = new SslStreamBase.InternalAsyncResult(callback, state, buffer, offset, count, true, true);
     if (this.MightNeedHandshake)
     {
         if (!this.BeginNegotiateHandshake(asyncResult))
         {
             this.negotiationComplete.WaitOne();
             this.InternalBeginWrite(asyncResult);
         }
     }
     else
     {
         this.InternalBeginWrite(asyncResult);
     }
     return((IAsyncResult)asyncResult);
 }
예제 #10
0
 private void AsyncHandshakeCallback(IAsyncResult asyncResult)
 {
     SslStreamBase.InternalAsyncResult asyncState = asyncResult.AsyncState as SslStreamBase.InternalAsyncResult;
     try
     {
         try
         {
             this.OnNegotiateHandshakeCallback(asyncResult);
         }
         catch (TlsException ex)
         {
             this.protocol.SendAlert(ex.Alert);
             throw new IOException("The authentication or decryption has failed.", (Exception)ex);
         }
         catch (Exception ex)
         {
             this.protocol.SendAlert(AlertDescription.InternalError);
             throw new IOException("The authentication or decryption has failed.", ex);
         }
         if (asyncState.ProceedAfterHandshake)
         {
             if (asyncState.FromWrite)
             {
                 this.InternalBeginWrite(asyncState);
             }
             else
             {
                 this.InternalBeginRead(asyncState);
             }
             this.negotiationComplete.Set();
         }
         else
         {
             this.negotiationComplete.Set();
             asyncState.SetComplete();
         }
     }
     catch (Exception ex)
     {
         this.negotiationComplete.Set();
         asyncState.SetComplete(ex);
     }
 }
예제 #11
0
 private void InternalBeginWrite(SslStreamBase.InternalAsyncResult asyncResult)
 {
     try
     {
         lock (this.write)
         {
             byte[] buffer = this.protocol.EncodeRecord(ContentType.ApplicationData, asyncResult.Buffer, asyncResult.Offset, asyncResult.Count);
             this.innerStream.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(this.InternalWriteCallback), (object)asyncResult);
         }
     }
     catch (TlsException ex)
     {
         this.protocol.SendAlert(ex.Alert);
         this.Close();
         throw new IOException("The authentication or decryption has failed.", (Exception)ex);
     }
     catch (Exception ex)
     {
         throw new IOException("IO exception during Write.", ex);
     }
 }
예제 #12
0
 private void InternalReadCallback(IAsyncResult result)
 {
     if (this.disposed)
     {
         return;
     }
     object[] asyncState = (object[])result.AsyncState;
     byte[]   buffer1    = (byte[])asyncState[0];
     SslStreamBase.InternalAsyncResult internalAsyncResult = (SslStreamBase.InternalAsyncResult)asyncState[1];
     try
     {
         int count = this.innerStream.EndRead(result);
         if (count > 0)
         {
             this.recordStream.Write(buffer1, 0, count);
             bool flag = false;
             long num  = this.recordStream.Position;
             this.recordStream.Position = 0L;
             byte[] buffer2 = (byte[])null;
             if (this.recordStream.Length >= 5L)
             {
                 buffer2 = this.protocol.ReceiveRecord((Stream)this.recordStream);
             }
             while (buffer2 != null)
             {
                 long   length  = this.recordStream.Length - this.recordStream.Position;
                 byte[] buffer3 = (byte[])null;
                 if (length > 0L)
                 {
                     buffer3 = new byte[length];
                     this.recordStream.Read(buffer3, 0, buffer3.Length);
                 }
                 lock (this.read)
                 {
                     long position = this.inputBuffer.Position;
                     if (buffer2.Length > 0)
                     {
                         this.inputBuffer.Seek(0L, SeekOrigin.End);
                         this.inputBuffer.Write(buffer2, 0, buffer2.Length);
                         this.inputBuffer.Seek(position, SeekOrigin.Begin);
                         flag = true;
                     }
                 }
                 this.recordStream.SetLength(0L);
                 buffer2 = (byte[])null;
                 if (length > 0L)
                 {
                     this.recordStream.Write(buffer3, 0, buffer3.Length);
                     if (this.recordStream.Length >= 5L)
                     {
                         this.recordStream.Position = 0L;
                         buffer2 = this.protocol.ReceiveRecord((Stream)this.recordStream);
                         if (buffer2 == null)
                         {
                             num = this.recordStream.Length;
                         }
                     }
                     else
                     {
                         num = length;
                     }
                 }
                 else
                 {
                     num = 0L;
                 }
             }
             if (!flag && count > 0)
             {
                 if (this.context.ReceivedConnectionEnd)
                 {
                     internalAsyncResult.SetComplete(0);
                 }
                 else
                 {
                     this.recordStream.Position = this.recordStream.Length;
                     this.innerStream.BeginRead(buffer1, 0, buffer1.Length, new AsyncCallback(this.InternalReadCallback), (object)asyncState);
                 }
             }
             else
             {
                 this.recordStream.Position = num;
                 int bytesRead = 0;
                 lock (this.read)
                     bytesRead = this.inputBuffer.Read(internalAsyncResult.Buffer, internalAsyncResult.Offset, internalAsyncResult.Count);
                 internalAsyncResult.SetComplete(bytesRead);
             }
         }
         else
         {
             internalAsyncResult.SetComplete(0);
         }
     }
     catch (Exception ex)
     {
         internalAsyncResult.SetComplete(ex);
     }
 }
예제 #13
0
 private void InternalReadCallback(IAsyncResult result)
 {
     if (this.disposed)
     {
         return;
     }
     object[] array  = (object[])result.AsyncState;
     byte[]   array2 = (byte[])array[0];
     SslStreamBase.InternalAsyncResult internalAsyncResult = (SslStreamBase.InternalAsyncResult)array[1];
     try
     {
         int num = this.innerStream.EndRead(result);
         if (num > 0)
         {
             this.recordStream.Write(array2, 0, num);
             bool flag     = false;
             long position = this.recordStream.Position;
             this.recordStream.Position = 0L;
             byte[] array3 = null;
             if (this.recordStream.Length >= 5L)
             {
                 array3 = this.protocol.ReceiveRecord(this.recordStream);
             }
             while (array3 != null)
             {
                 long   num2   = this.recordStream.Length - this.recordStream.Position;
                 byte[] array4 = null;
                 if (num2 > 0L)
                 {
                     array4 = new byte[num2];
                     this.recordStream.Read(array4, 0, array4.Length);
                 }
                 object obj = this.read;
                 lock (obj)
                 {
                     long position2 = this.inputBuffer.Position;
                     if (array3.Length > 0)
                     {
                         this.inputBuffer.Seek(0L, SeekOrigin.End);
                         this.inputBuffer.Write(array3, 0, array3.Length);
                         this.inputBuffer.Seek(position2, SeekOrigin.Begin);
                         flag = true;
                     }
                 }
                 this.recordStream.SetLength(0L);
                 array3 = null;
                 if (num2 > 0L)
                 {
                     this.recordStream.Write(array4, 0, array4.Length);
                     if (this.recordStream.Length >= 5L)
                     {
                         this.recordStream.Position = 0L;
                         array3 = this.protocol.ReceiveRecord(this.recordStream);
                         if (array3 == null)
                         {
                             position = this.recordStream.Length;
                         }
                     }
                     else
                     {
                         position = num2;
                     }
                 }
                 else
                 {
                     position = 0L;
                 }
             }
             if (!flag && num > 0)
             {
                 if (this.context.ReceivedConnectionEnd)
                 {
                     internalAsyncResult.SetComplete(0);
                 }
                 else
                 {
                     this.recordStream.Position = this.recordStream.Length;
                     this.innerStream.BeginRead(array2, 0, array2.Length, new AsyncCallback(this.InternalReadCallback), array);
                 }
             }
             else
             {
                 this.recordStream.Position = position;
                 int    complete = 0;
                 object obj2     = this.read;
                 lock (obj2)
                 {
                     complete = this.inputBuffer.Read(internalAsyncResult.Buffer, internalAsyncResult.Offset, internalAsyncResult.Count);
                 }
                 internalAsyncResult.SetComplete(complete);
             }
         }
         else
         {
             internalAsyncResult.SetComplete(0);
         }
     }
     catch (Exception complete2)
     {
         internalAsyncResult.SetComplete(complete2);
     }
 }