News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
Secure Socket Question  
by Chris [Chris at QTMedical dot Com]
posted on 2003/12/04

I am trying to encrypt some data that needs to be sent via a socket connection. I though about trying to write my own wrapper but I think your security library is a little flexible.

I have been trying to use the SecureSocket with the SSL3 Security protocol to send arbiturary text to a server component I wrote (also using the security library). I have as of yet been unable to get the client and server app to communicate wiht each other. The client / server app do not use http to make / send request I am just trying to send text.

When I connect to the server wiht the client app it accepts the connection but when I tell the securesocket object to recieve data from the socket it never returns. I am not using threading in this simple example.

Here is the code for the Client

Imports Org.Mentalis.Security.Ssl
Imports System.Net
Imports System.Net.Sockets

Public Class frmClient
Inherits System.Windows.Forms.Form
Dim Socket As SecureSocket

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtConsole As System.Windows.Forms.TextBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents lnkSend As System.Windows.Forms.LinkLabel
Friend WithEvents lnkConnect As System.Windows.Forms.LinkLabel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtConsole = New System.Windows.Forms.TextBox()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.lnkSend = New System.Windows.Forms.LinkLabel()
Me.lnkConnect = New System.Windows.Forms.LinkLabel()
Me.SuspendLayout()
'
'txtConsole
'
Me.txtConsole.Multiline = True
Me.txtConsole.Name = "txtConsole"
Me.txtConsole.Size = New System.Drawing.Size(288, 232)
Me.txtConsole.TabIndex = 0
Me.txtConsole.Text = ""
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(0, 240)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(248, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'lnkSend
'
Me.lnkSend.Location = New System.Drawing.Point(256, 240)
Me.lnkSend.Name = "lnkSend"
Me.lnkSend.Size = New System.Drawing.Size(32, 16)
Me.lnkSend.TabIndex = 2
Me.lnkSend.TabStop = True
Me.lnkSend.Text = "Send"
'
'lnkConnect
'
Me.lnkConnect.Location = New System.Drawing.Point(0, 264)
Me.lnkConnect.Name = "lnkConnect"
Me.lnkConnect.Size = New System.Drawing.Size(72, 23)
Me.lnkConnect.TabIndex = 3
Me.lnkConnect.TabStop = True
Me.lnkConnect.Text = "Connect"
'
'frmClient
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(288, 285)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lnkConnect, Me.lnkSend, Me.TextBox1, Me.txtConsole})
Me.Name = "frmClient"
Me.Text = "Client"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub lnkConnect_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkConnect.LinkClicked
Dim SecurityOptions As New SecurityOptions(SecureProtocol.Ssl3)
SecurityOptions.Certificate = Nothing
SecurityOptions.Entity = ConnectionEnd.Client
SecurityOptions.CommonName = "Nav-Crp"
SecurityOptions.VerificationType = CredentialVerification.None
SecurityOptions.Flags = SecurityFlags.Default

Socket = New SecureSocket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp, SecurityOptions)
Try
Socket.Connect(New System.Net.IPEndPoint(IPAddress.Parse("192.168.1.67"), 5000))
If Socket.Connected Then
txtConsole.Text += "Connected To Server" & vbCrLf
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

Private Sub SendText(ByVal value As String)
Dim reqBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(value)
Dim sent As Integer = Socket.Send(reqBytes, 0, reqBytes.Length, SocketFlags.None)
Try
While sent <> reqBytes.Length
sent += Socket.Send(reqBytes, sent, reqBytes.Length - sent, SocketFlags.None)
End While
Catch ex As Exception
txtConsole.Text += "Exception occurred while sending: " + ex.ToString() & vbCrLf
Return
End Try

End Sub
End Class


****************************************
Server Code
****************************************

Imports System.Text
Imports System.IO
Imports System.Net

Imports Org.Mentalis.Security.Ssl
Imports Org.Mentalis.Security.Certificates

Public Class frmServer
Inherits System.Windows.Forms.Form

Private ServerSocket As SecureSocket
Private isConnected As Boolean = False
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtConsole As System.Windows.Forms.TextBox
Friend WithEvents txtInput As System.Windows.Forms.TextBox
Friend WithEvents lnkSend As System.Windows.Forms.LinkLabel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtConsole = New System.Windows.Forms.TextBox()
Me.txtInput = New System.Windows.Forms.TextBox()
Me.lnkSend = New System.Windows.Forms.LinkLabel()
Me.SuspendLayout()
'
'txtConsole
'
Me.txtConsole.Multiline = True
Me.txtConsole.Name = "txtConsole"
Me.txtConsole.Size = New System.Drawing.Size(288, 232)
Me.txtConsole.TabIndex = 0
Me.txtConsole.Text = ""
'
'txtInput
'
Me.txtInput.Location = New System.Drawing.Point(0, 240)
Me.txtInput.Name = "txtInput"
Me.txtInput.Size = New System.Drawing.Size(248, 20)
Me.txtInput.TabIndex = 1
Me.txtInput.Text = ""
'
'lnkSend
'
Me.lnkSend.Location = New System.Drawing.Point(256, 240)
Me.lnkSend.Name = "lnkSend"
Me.lnkSend.Size = New System.Drawing.Size(32, 23)
Me.lnkSend.TabIndex = 2
Me.lnkSend.TabStop = True
Me.lnkSend.Text = "Send"
'
'frmServer
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(288, 261)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lnkSend, Me.txtInput, Me.txtConsole})
Me.Name = "frmServer"
Me.Text = "Server"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub StartServer(ByVal Ep As IPEndPoint, ByVal Sp As SecureProtocol, ByVal cert As Certificate)
Dim Options As New SecurityOptions(Sp, cert, ConnectionEnd.Server, CredentialVerification.None, Nothing, "Nav-Crp", SecurityFlags.Default, SslAlgorithms.SECURE_CIPHERS, Nothing)
ServerSocket = New SecureSocket(Net.Sockets.AddressFamily.Unspecified, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
ServerSocket.Bind(Ep)
ServerSocket.Listen(10)

publishText("Server: Listening For Connections on " & ServerSocket.LocalEndPoint.ToString())
Application.DoEvents()

Dim SS As VirtualSocket
Dim query As String = ""
Dim buffer(1023) As Byte
Dim ret As Integer = 0
Dim lastReply As String = ""
Dim Ascii As New ASCIIEncoding()

SS = CType(ServerSocket.Accept, SecureSocket)
publishText("Server: Accepting Incoming Connection")

Do
Do
Try
ret = SS.Receive(buffer)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
lastReply += Ascii.GetString(buffer, 0, ret)
Application.DoEvents()
Loop Until IsValidReply(lastReply)
'Process the reply
ProcessReply(lastReply)
Loop Until lastReply.ToUpper = "QUIT"

SS.Shutdown(Net.Sockets.SocketShutdown.Both)
SS.Close()
End Sub

Private Function IsValidReply(ByVal Value As String)
If Value.IndexOf(vbCrLf) > -1 Then
Return True
End If

Return False
End Function

Private Sub ProcessReply(ByVal value As String)
publishText(value)
End Sub

Private Sub publishText(ByVal Value As String)
txtConsole.Text += Value & vbCrLf
End Sub

Private Sub lnkSend_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkSend.LinkClicked
Dim cert As New Certificate(Certificate.CreateFromPfxFile("server.pfx", "test"))
Dim ep As New IPEndPoint(IPAddress.Parse("192.168.1.67"), 5000)

StartServer(ep, SecureProtocol.Ssl3, cert)
End Sub
End Class








I have a feeling its just something simple I am missing do you have any ideas?

by Chris
posted on 2003/12/04

You can ingore this thread.

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.