[pgsqlclient-checkins] pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient PgDbClient.cs,1.3
Status: Inactive
Brought to you by:
carlosga_fb
Update of /cvsroot/pgsqlclient/pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient In directory sc8-pr-cvs1:/tmp/cvs-serv21067 Modified Files: PgDbClient.cs PgInetReader.cs PgInetWriter.cs PgResponsePacket.cs PgStatement.cs Log Message: Added initial release of a simple TLS protocol implementation, it uses tree files from the Mono project that are under the MIT X11 license. Index: PgDbClient.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient/PgDbClient.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** PgDbClient.cs 15 Aug 2003 17:50:09 -0000 1.3 --- PgDbClient.cs 20 Aug 2003 11:53:19 -0000 1.4 *************** *** 25,29 **** using System.Text; ! //using PgSqlClient.Security.TLS; namespace PostgreSql.Data.NPgClient --- 25,30 ---- using System.Text; ! using PgSqlClient.Security.TLS; ! using PgSqlClient.Security.TLS.Alerts; namespace PostgreSql.Data.NPgClient *************** *** 52,59 **** private Socket socket; private NetworkStream networkStream; ! private PgInetWriter send; ! private PgInetReader receive; private PgConnectionParams settings; private char transactionStatus; #endregion --- 53,61 ---- private Socket socket; private NetworkStream networkStream; ! private TlsWriter send; ! private TlsReader receive; private PgConnectionParams settings; private char transactionStatus; + private TlsSession session; #endregion *************** *** 100,109 **** } ! public PgInetReader Receive { get { return receive; } } ! public PgInetWriter Send { get { return send; } --- 102,111 ---- } ! public TlsReader Receive { get { return receive; } } ! public TlsWriter Send { get { return send; } *************** *** 140,148 **** PgDbClient.InitializeTypes(); PgDbClient.InitializeCharSets(); - - InitializeSocket(); lock (this) { if (settings.SSL) { --- 142,152 ---- PgDbClient.InitializeTypes(); PgDbClient.InitializeCharSets(); lock (this) { + session = new TlsSession(new TlsSessionSettings(TlsProtocol.Tls1)); + + initializeSocket(); + if (settings.SSL) { *************** *** 152,165 **** if (settings.SSL) { - /* - TlsSession session = new TlsSession(new TlsSessionSettings(TlsProtocol.Tls1)); - - TlsWriter tlsSend = session.GetWriter(new BufferedStream(this.networkStream)); - TlsReader tlsRead = session.GetReader(new BufferedStream(this.networkStream)); - try { // Start TLS Session session.StartSession(); } catch (TlsException ex) --- 156,170 ---- if (settings.SSL) { try { // Start TLS Session session.StartSession(); + + send.WriteRecord(Encoding.Default.GetBytes("GET / HTTP/1.0")); + + byte[] r = receive.ReadRecord(); + + // End TLS Session + session.EndSession(); } catch (TlsException ex) *************** *** 172,176 **** throw new PgClientException(ex.Message); } - */ } } --- 177,180 ---- *************** *** 189,194 **** packet.Write((byte)0); // Terminator ! send.Write(packet.GetSimplePacketBytes()); ! send.Flush(); PgResponsePacket response = new PgResponsePacket(); --- 193,197 ---- packet.Write((byte)0); // Terminator ! SendData(packet.GetSimplePacketBytes()); PgResponsePacket response = new PgResponsePacket(); *************** *** 216,221 **** PgOutputPacket packet = new PgOutputPacket(); ! send.Write(packet.GetPacketBytes(PgFrontEndCodes.TERMINATE)); ! send.Flush(); // Close socket and streams --- 219,223 ---- PgOutputPacket packet = new PgOutputPacket(); ! SendData(packet.GetPacketBytes(PgFrontEndCodes.TERMINATE)); // Close socket and streams *************** *** 236,239 **** --- 238,254 ---- } + internal void SendData(byte[] data) + { + if (settings.SSL) + { + send.WriteRecord(data); + } + else + { + send.Write(data); + } + + send.Flush(); + } #endregion *************** *** 245,299 **** char type; int length; ! PgResponsePacket responsePacket; lock (this) { length = 0; ! type = (char)receive.ReadByte(); ! if ((sslRequest.Length > 0 && !sslRequest[0]) || type == PgBackendCodes.ERROR_RESPONSE) ! { ! length = receive.ReadInt() - 4; ! } ! ! if (length != 0) { ! responsePacket = new PgResponsePacket(type, receive.ReadBytes(length)); } else { ! responsePacket = new PgResponsePacket(type, new byte[0]); ! } ! responsePacket.Encoding = Settings.Encoding; ! } ! switch (type) ! { ! case PgBackendCodes.ERROR_RESPONSE: ! { ! // Read the error message and trow the exception ! PgClientException ex = processErrorPacket(responsePacket); ! throw ex; ! } ! case PgBackendCodes.NOTICE_RESPONSE: ! { ! // Read the notice message and raise an InfoMessage event ! PgClientException ex = processErrorPacket(responsePacket); ! this.InfoMessage(this, new PgClientMessageEventArgs(ex)); ! } ! break; ! case PgBackendCodes.NOTIFICATION_RESPONSE: ! { ! processNotificationResponse(responsePacket); } - break; ! default: ! break; ! } return responsePacket; --- 260,338 ---- char type; int length; ! PgResponsePacket responsePacket = null; lock (this) { length = 0; ! if ((sslRequest.Length > 0 && sslRequest[0])) { ! type = receive.ReadChar(); ! return new PgResponsePacket(type, new byte[0]); } else { ! if (settings.SSL) ! { ! // TLS/SSL Application data contents ! PgResponsePacket tmpPacket = new PgResponsePacket(receive.ReadRecord()); ! type = tmpPacket.ReadChar(); ! length = tmpPacket.ReadInt() - 4; ! if (length != 0) ! { ! responsePacket = new PgResponsePacket(type, tmpPacket.ReadBytes(length)); ! } ! tmpPacket.Reset(); ! } ! else ! { ! type = (char)receive.ReadByte(); ! length = IPAddress.HostToNetworkOrder(receive.ReadInt32()) - 4; ! if (length != 0) ! { ! responsePacket = new PgResponsePacket(type, receive.ReadBytes(length)); ! } ! } ! if (length == 0) ! { ! responsePacket = new PgResponsePacket(type, new byte[0]); ! } ! switch (type) ! { ! case PgBackendCodes.ERROR_RESPONSE: ! { ! // Read the error message and trow the exception ! PgClientException ex = processErrorPacket(responsePacket); ! ! throw ex; ! } ! ! case PgBackendCodes.NOTICE_RESPONSE: ! { ! // Read the notice message and raise an InfoMessage event ! PgClientException ex = processErrorPacket(responsePacket); ! ! this.InfoMessage(this, new PgClientMessageEventArgs(ex)); ! } ! break; ! ! case PgBackendCodes.NOTIFICATION_RESPONSE: ! { ! processNotificationResponse(responsePacket); ! } ! break; ! ! default: ! break; ! } } ! responsePacket.Encoding = Settings.Encoding; ! } return responsePacket; *************** *** 393,398 **** // Send the packet to the server ! send.Write(outPacket.GetPacketBytes(PgFrontEndCodes.PASSWORD_MESSAGE)); ! send.Flush(); } --- 432,436 ---- // Send the packet to the server ! SendData(outPacket.GetPacketBytes(PgFrontEndCodes.PASSWORD_MESSAGE)); } *************** *** 541,546 **** // Send packet to the server ! send.Write(packet.GetPacketBytes(PgFrontEndCodes.FLUSH)); ! send.Flush(); } catch (Exception ex) --- 579,583 ---- // Send packet to the server ! SendData(packet.GetPacketBytes(PgFrontEndCodes.FLUSH)); } catch (Exception ex) *************** *** 560,565 **** // Send packet to the server ! send.Write(packet.GetPacketBytes(PgFrontEndCodes.SYNC)); ! send.Flush(); } catch (Exception ex) --- 597,601 ---- // Send packet to the server ! SendData(packet.GetPacketBytes(PgFrontEndCodes.SYNC)); } catch (Exception ex) *************** *** 584,589 **** // Send packet to the server ! send.Write(packet.GetSimplePacketBytes()); ! send.Flush(); } catch (Exception ex) --- 620,624 ---- // Send packet to the server ! SendData(packet.GetSimplePacketBytes()); } catch (Exception ex) *************** *** 598,601 **** --- 633,638 ---- lock (this) { + settings.SSL = false; + try { *************** *** 605,610 **** // Send packet to the server ! send.Write(packet.GetSimplePacketBytes()); ! send.Flush(); // Receive server response --- 642,646 ---- // Send packet to the server ! SendData(packet.GetSimplePacketBytes()); // Receive server response *************** *** 735,739 **** } ! public void InitializeSocket() { IPAddress hostadd = Dns.Resolve(settings.ServerName).AddressList[0]; --- 771,779 ---- } ! #endregion ! ! #region PRIVATE_METHODS ! ! private void initializeSocket() { IPAddress hostadd = Dns.Resolve(settings.ServerName).AddressList[0]; *************** *** 748,753 **** networkStream = new NetworkStream(socket, true); ! send = new PgInetWriter(new BufferedStream(networkStream)); ! receive = new PgInetReader(new BufferedStream(networkStream)); } --- 788,794 ---- networkStream = new NetworkStream(socket, true); ! // Create streams for read/write operations ! send = session.GetWriter(networkStream); ! receive = session.GetReader(networkStream); } Index: PgInetReader.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient/PgInetReader.cs,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** PgInetReader.cs 2 Aug 2003 19:43:02 -0000 1.1.1.1 --- PgInetReader.cs 20 Aug 2003 11:53:19 -0000 1.2 *************** *** 22,37 **** using System.Net; namespace PostgreSql.Data.NPgClient { ! internal class PgInetReader : BinaryReader { - #region CONTRUCTORS - - public PgInetReader(Stream input) : base(input) - { - } - - #endregion - #region METHODS --- 22,31 ---- using System.Net; + using PgSqlClient.Security.TLS; + namespace PostgreSql.Data.NPgClient { ! internal class PgInetReader : TlsReader { #region METHODS Index: PgInetWriter.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient/PgInetWriter.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PgInetWriter.cs 15 Aug 2003 17:50:09 -0000 1.2 --- PgInetWriter.cs 20 Aug 2003 11:53:19 -0000 1.3 *************** *** 22,65 **** using System.Net; namespace PostgreSql.Data.NPgClient { ! internal class PgInetWriter : BinaryWriter { - #region CONSTRUCTORS - - public PgInetWriter(Stream output) : base(output) - { - } - - #endregion - - #region METHODS - - /* - public void WriteSimplePacket(PgOutputPacket packet) - { - // Write packet contents - packet.Position = 0; - packet.WriteInt(packet.GetByteCount() + 1); - Write(packet.GetBytes()); - - Flush(); - } - - public void WritePacket(char format, PgOutputPacket packet) - { - // Update packet Length - packet.Position = 0; - - packet.Write((byte)format); - packet.WriteInt(packet.GetByteCount()); - - // Write packet contents - Write(packet.GetBytes()); - - Flush(); - } - */ - #endregion } } --- 22,31 ---- using System.Net; + using PgSqlClient.Security.TLS; + namespace PostgreSql.Data.NPgClient { ! internal class PgInetWriter : TlsWriter { } } Index: PgResponsePacket.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient/PgResponsePacket.cs,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** PgResponsePacket.cs 2 Aug 2003 19:43:02 -0000 1.1.1.1 --- PgResponsePacket.cs 20 Aug 2003 11:53:19 -0000 1.2 *************** *** 57,63 **** --- 57,77 ---- } + public PgResponsePacket(byte[] contents) : base(new MemoryStream(contents)) + { + } + public PgResponsePacket(char message, byte[] contents) : base(new MemoryStream(contents)) { this.message = message; + } + + #endregion + + #region STREAM_METHODS + + public void Reset() + { + ((MemoryStream)BaseStream).SetLength(0); + ((MemoryStream)BaseStream).Position = 0; } Index: PgStatement.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PostgreSql.Data.PgSqlClient/source/NPgClient/PgStatement.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PgStatement.cs 15 Aug 2003 17:50:09 -0000 1.2 --- PgStatement.cs 20 Aug 2003 11:53:19 -0000 1.3 *************** *** 190,196 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.PARSE)); ! db.Send.Flush(); ! // Sync server and client db.Flush(); --- 190,195 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.PARSE)); ! // Sync server and client db.Flush(); *************** *** 240,246 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.DESCRIBE)); ! db.Send.Flush(); ! // Sync server and client db.Flush(); --- 239,244 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.DESCRIBE)); ! // Sync server and client db.Flush(); *************** *** 309,315 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.BIND)); ! db.Send.Flush(); ! // Sync server and client db.Flush(); --- 307,312 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.BIND)); ! // Sync server and client db.Flush(); *************** *** 344,349 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.EXECUTE)); ! db.Send.Flush(); // Sync server and client --- 341,345 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.EXECUTE)); // Sync server and client *************** *** 400,406 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.FUNCTION_CALL)); ! db.Send.Flush(); ! // Receive response PgResponsePacket response = db.ReceiveResponsePacket(); --- 396,401 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.FUNCTION_CALL)); ! // Receive response PgResponsePacket response = db.ReceiveResponsePacket(); *************** *** 431,436 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.QUERY)); ! db.Send.Flush(); // Receive response --- 426,430 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.QUERY)); // Receive response *************** *** 503,508 **** // Send packet to the server ! db.Send.Write(packet.GetPacketBytes(PgFrontEndCodes.CLOSE)); ! db.Send.Flush(); // Sync server and client --- 497,501 ---- // Send packet to the server ! db.SendData(packet.GetPacketBytes(PgFrontEndCodes.CLOSE)); // Sync server and client |