[pgsqlclient-checkins] pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls SslClie
Status: Inactive
Brought to you by:
carlosga_fb
From: <car...@us...> - 2004-02-17 14:27:07
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30037 Modified Files: SslClientStream.cs Log Message: 2004-02-17 Carlos Guzmán Álvarez <car...@te...> * Mono.Security.Protocol.Tls/SslClientStream.cs: - Added changes for implement Async methods ( Tanks to Sebastien Pouliot ) Index: SslClientStream.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SslClientStream.cs 17 Feb 2004 13:11:28 -0000 1.3 --- SslClientStream.cs 17 Feb 2004 14:18:12 -0000 1.4 *************** *** 229,273 **** public SslClientStream( ! Stream stream, ! string targetHost, ! bool ownsStream) ! : this(stream, targetHost, ! ownsStream, SecurityProtocolType.Default, null) { } public SslClientStream( ! Stream stream, string targetHost, X509Certificate clientCertificate) : ! this( ! stream, targetHost, ! false, SecurityProtocolType.Default, ! new X509CertificateCollection(new X509Certificate[]{clientCertificate})) { } public SslClientStream( ! Stream stream, ! string targetHost, X509CertificateCollection clientCertificates) : ! this(stream, targetHost, false, ! SecurityProtocolType.Default, clientCertificates) { } public SslClientStream( ! Stream stream, ! string targetHost, ! bool ownsStream, ! SecurityProtocolType securityProtocolType) : ! this(stream, targetHost, ownsStream, securityProtocolType, ! new X509CertificateCollection()) { } public SslClientStream( ! Stream stream, ! string targetHost, ! bool ownsStream, ! SecurityProtocolType securityProtocolType, ! X509CertificateCollection clientCertificates) { if (stream == null) --- 229,278 ---- public SslClientStream( ! Stream stream, ! string targetHost, ! bool ownsStream) ! : this( ! stream, targetHost, ownsStream, ! SecurityProtocolType.Default, null) { } public SslClientStream( ! Stream stream, ! string targetHost, ! X509Certificate clientCertificate) ! : this( ! stream, targetHost, false, SecurityProtocolType.Default, ! new X509CertificateCollection(new X509Certificate[]{clientCertificate})) { } public SslClientStream( ! Stream stream, ! string targetHost, ! X509CertificateCollection clientCertificates) : ! this( ! stream, targetHost, false, SecurityProtocolType.Default, ! clientCertificates) { } public SslClientStream( ! Stream stream, ! string targetHost, ! bool ownsStream, ! SecurityProtocolType securityProtocolType) ! : this( ! stream, targetHost, ownsStream, securityProtocolType, ! new X509CertificateCollection()) { } public SslClientStream( ! Stream stream, ! string targetHost, ! bool ownsStream, ! SecurityProtocolType securityProtocolType, ! X509CertificateCollection clientCertificates) { if (stream == null) *************** *** 336,341 **** } } ! this.ownsStream = false; ! this.innerStream = null; if (this.ClientCertSelection != null) { --- 341,346 ---- } } ! this.ownsStream = false; ! this.innerStream = null; if (this.ClientCertSelection != null) { *************** *** 359,372 **** public override IAsyncResult BeginRead( ! byte[] buffer, ! int offset, ! int count, ! AsyncCallback callback, ! object state) { ! if (this.disposed) ! { ! throw new ObjectDisposedException("The SslClientStream is closed."); ! } if (buffer == null) --- 364,374 ---- public override IAsyncResult BeginRead( ! byte[] buffer, ! int offset, ! int count, ! AsyncCallback callback, ! object state) { ! this.checkDisposed(); if (buffer == null) *************** *** 391,453 **** } - throw new NotSupportedException(); - } - - public override IAsyncResult BeginWrite( - byte[] buffer, - int offset, - int count, - AsyncCallback callback, - object state) - { - throw new NotSupportedException(); - } - - public override int EndRead(IAsyncResult asyncResult) - { - if (this.disposed) - { - throw new ObjectDisposedException("The SslClientStream is closed."); - } - if (asyncResult == null) - { - throw new ArgumentNullException("asyncResult is null or was not obtained by calling BeginRead."); - } - - throw new NotSupportedException(); - } - - public override void EndWrite(IAsyncResult asyncResult) - { - throw new NotSupportedException(); - } - - public override void Close() - { - ((IDisposable)this).Dispose(); - } - - public override void Flush() - { - if (this.disposed) - { - throw new ObjectDisposedException("The SslClientStream is closed."); - } - - this.innerStream.Flush(); - } - - public int Read(byte[] buffer) - { - return this.Read(buffer, 0, buffer.Length); - } - - public override int Read(byte[] buffer, int offset, int count) - { - if (this.disposed) - { - throw new ObjectDisposedException("The SslClientStream is closed."); - } - if (!this.context.HandshakeFinished) { --- 393,396 ---- *************** *** 455,483 **** } - 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."); - } - if (!Monitor.TryEnter(this.read)) { throw new InvalidOperationException("A read operation is already in progress."); } try { --- 398,408 ---- } if (!Monitor.TryEnter(this.read)) { throw new InvalidOperationException("A read operation is already in progress."); } + + IAsyncResult asyncResult; + try { *************** *** 527,531 **** } ! return this.inputBuffer.Read(buffer, offset, count); } catch (TlsException ex) --- 452,457 ---- } ! asyncResult = this.inputBuffer.BeginRead( ! buffer, offset, count, callback, state); } catch (TlsException ex) *************** *** 541,567 **** System.Threading.Monitor.Exit(this.read); } - } ! public override long Seek(long offset, SeekOrigin origin) ! { ! throw new NotSupportedException(); ! } ! ! public override void SetLength(long value) ! { ! throw new NotSupportedException(); ! } ! ! public void Write(byte[] buffer) ! { ! this.Write(buffer, 0, buffer.Length); } ! public override void Write(byte[] buffer, int offset, int count) { ! if (this.disposed) ! { ! throw new ObjectDisposedException("The SslClientStream is closed."); ! } if (!this.context.HandshakeFinished) --- 467,482 ---- System.Threading.Monitor.Exit(this.read); } ! return asyncResult; } ! public override IAsyncResult BeginWrite( ! byte[] buffer, ! int offset, ! int count, ! AsyncCallback callback, ! object state) { ! this.checkDisposed(); if (!this.context.HandshakeFinished) *************** *** 596,599 **** --- 511,517 ---- throw new InvalidOperationException("A write operation is already in progress."); } + + IAsyncResult asyncResult; + try { *************** *** 601,608 **** // Send the buffer as a TLS record ! byte[] recordData = new byte[count]; ! System.Array.Copy(buffer, offset, recordData, 0, count); ! ! this.sendRecord(TlsContentType.ApplicationData, recordData); } catch (TlsException ex) --- 519,527 ---- // Send the buffer as a TLS record ! byte[] record = this.encodeRecord( ! TlsContentType.ApplicationData, buffer, offset, count); ! ! asyncResult = this.innerStream.BeginWrite( ! record, 0, record.Length, callback, state); } catch (TlsException ex) *************** *** 618,621 **** --- 537,612 ---- Monitor.Exit(this.write); } + + return asyncResult; + } + + public override int EndRead(IAsyncResult asyncResult) + { + this.checkDisposed(); + + if (asyncResult == null) + { + throw new ArgumentNullException("asyncResult is null or was not obtained by calling BeginRead."); + } + + return this.inputBuffer.EndRead(asyncResult); + } + + public override void EndWrite(IAsyncResult asyncResult) + { + this.checkDisposed(); + + if (asyncResult == null) + { + throw new ArgumentNullException("asyncResult is null or was not obtained by calling BeginRead."); + } + + this.innerStream.EndWrite (asyncResult); + } + + public override void Close() + { + ((IDisposable)this).Dispose(); + } + + public override void Flush() + { + this.checkDisposed(); + + this.innerStream.Flush(); + } + + public int Read(byte[] buffer) + { + return this.Read(buffer, 0, buffer.Length); + } + + public override int Read(byte[] buffer, int offset, int count) + { + IAsyncResult res = this.BeginRead(buffer, offset, count, null, null); + + return this.EndRead(res); + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotSupportedException(); + } + + public override void SetLength(long value) + { + throw new NotSupportedException(); + } + + public void Write(byte[] buffer) + { + this.Write(buffer, 0, buffer.Length); + } + + public override void Write(byte[] buffer, int offset, int count) + { + IAsyncResult res = this.BeginWrite (buffer, offset, count, null, null); + + this.EndWrite(res); } *************** *** 727,733 **** } private void sendRecord(TlsHandshakeType type) { ! TlsHandshakeMessage msg = createClientHandshakeMessage(type); // Write record --- 718,739 ---- } + private void sendChangeCipherSpec() + { + // Send Change Cipher Spec message + this.sendRecord(TlsContentType.ChangeCipherSpec, new byte[] {1}); + + // Reset sequence numbers + this.context.WriteSequenceNumber = 0; + + // Make the pending state to be the current state + this.context.IsActual = true; + + // Send Finished message + this.sendRecord(TlsHandshakeType.Finished); + } + private void sendRecord(TlsHandshakeType type) { ! TlsHandshakeMessage msg = this.createClientHandshakeMessage(type); // Write record *************** *** 741,760 **** } ! private void sendChangeCipherSpec() { ! // Send Change Cipher Spec message ! this.sendRecord(TlsContentType.ChangeCipherSpec, new byte[] {1}); ! // Reset sequence numbers ! this.context.WriteSequenceNumber = 0; ! // Make the pending state to be the current state ! this.context.IsActual = true; ! // Send Finished message ! this.sendRecord(TlsHandshakeType.Finished); } ! ! private void sendRecord(TlsContentType contentType, byte[] recordData) { if (this.context.ConnectionEnd) --- 747,776 ---- } ! private void sendRecord(TlsContentType contentType, byte[] recordData) { ! if (this.context.ConnectionEnd) ! { ! throw this.context.CreateException("The session is finished and it's no longer valid."); ! } ! byte[] record = this.encodeRecord(contentType, recordData); ! this.innerStream.Write(record, 0, record.Length); ! } ! private byte[] encodeRecord(TlsContentType contentType, byte[] recordData) ! { ! return this.encodeRecord( ! contentType, ! recordData, ! 0, ! recordData.Length); } ! ! private byte[] encodeRecord( ! TlsContentType contentType, ! byte[] recordData, ! int offset, ! int count) { if (this.context.ConnectionEnd) *************** *** 763,767 **** } ! byte[][] fragments = this.fragmentData(recordData); for (int i = 0; i < fragments.Length; i++) { --- 779,785 ---- } ! TlsStream record = new TlsStream(); ! ! byte[][] fragments = this.fragmentData(recordData, offset, count); for (int i = 0; i < fragments.Length; i++) { *************** *** 775,803 **** // Write tls message - TlsStream record = new TlsStream(); record.Write((byte)contentType); record.Write((short)this.context.Protocol); record.Write((short)fragment.Length); record.Write(fragment); - - // Write record - this.innerStream.Write(record.ToArray(), 0, (int)record.Length); - - // Reset record data - record.Reset(); } - } ! private byte[][] fragmentData(byte[] messageData) { ArrayList d = new ArrayList(); ! int position = 0; ! while (position < messageData.Length) { short fragmentLength = 0; byte[] fragmentData; ! if ((messageData.Length - position) > TlsContext.MAX_FRAGMENT_SIZE) { fragmentLength = TlsContext.MAX_FRAGMENT_SIZE; --- 793,816 ---- // Write tls message record.Write((byte)contentType); record.Write((short)this.context.Protocol); record.Write((short)fragment.Length); record.Write(fragment); } ! return record.ToArray(); ! } ! ! private byte[][] fragmentData(byte[] messageData, int offset, int count) { ArrayList d = new ArrayList(); ! int position = offset; ! while (position < ( offset + count )) { short fragmentLength = 0; byte[] fragmentData; ! if ((count - position) > TlsContext.MAX_FRAGMENT_SIZE) { fragmentLength = TlsContext.MAX_FRAGMENT_SIZE; *************** *** 805,809 **** else { ! fragmentLength = (short)(messageData.Length - position); } fragmentData = new byte[fragmentLength]; --- 818,822 ---- else { ! fragmentLength = (short)(count - position); } fragmentData = new byte[fragmentLength]; *************** *** 829,833 **** #region Cryptography Methods ! private byte[] encryptRecordFragment(TlsContentType contentType, byte[] fragment) { // Calculate message MAC --- 842,848 ---- #region Cryptography Methods ! private byte[] encryptRecordFragment( ! TlsContentType contentType, ! byte[] fragment) { // Calculate message MAC *************** *** 851,857 **** } ! private TlsStream decryptRecordFragment(TlsContentType contentType, ! SecurityProtocolType protocol, ! byte[] fragment) { byte[] dcrFragment = null; --- 866,873 ---- } ! private TlsStream decryptRecordFragment( ! TlsContentType contentType, ! SecurityProtocolType protocol, ! byte[] fragment) { byte[] dcrFragment = null; *************** *** 917,921 **** } ! private void processAlert(TlsAlertLevel alertLevel, TlsAlertDescription alertDesc) { switch (alertLevel) --- 933,939 ---- } ! private void processAlert( ! TlsAlertLevel alertLevel, ! TlsAlertDescription alertDesc) { switch (alertLevel) *************** *** 960,963 **** --- 978,989 ---- } + private void checkDisposed() + { + if (this.disposed) + { + throw new ObjectDisposedException("The SslClientStream is closed."); + } + } + #endregion |