CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2018
    Posts
    3

    Problems with SSLstream.Read

    I'm connecting to an EPP server via TCPclient and it's protected by SSL certificate authentication. The company that provides the EPP server said I needed to get a certificate (not self-signed), upload the CRT file into my user account, and then combine the CRT and KEY files into one (I choose PFX) that will I need to pass with each message in order to invoke an authorization / certificate combination. I've upload the CRT and created the PFX files fine.

    I've tested this several ways (SSLstream, NetworkStream, StreamWriter/StreamReader) and none of them have been able to produce me with a readable response. At the same time, neither method produces any exceptions. From my testing it appears that I connect to the server okay, convert my string and write okay, but then either get a blank response or a jibberish response that looks like it might be encrypted? Below are two ways I've tried to do this (SSLstream & NetworkStream). You can see my code, plus a screenshot of the response.

    SSLstream
    Code:
            'Create a collection and add the certificate
            Dim collection = New X509Certificate2Collection()
            collection.Import("C:\\Users\\Drew\\Desktop\\testcert\\FIcert.pfx", "password", X509KeyStorageFlags.DefaultKeySet)
            Dim store = New X509Store(StoreName.My)
            store.Open(OpenFlags.ReadWrite)
    
            Try
                For Each certificate As X509Certificate2 In collection
                    store.Add(certificate)
                Next
            Finally
                store.Close()
            End Try
    
    
            'Create the Hello EPP request and put it into a String
            Dim hello As XElement = <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
                                        <hello/>
                                    </epp>
    
    
            'Create client, convert XML element to string and pass it with certificate collection
            Try
                Dim client As New TcpClient(DRShost, 700)
                Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(hello.ToString)
                Dim callback As New RemoteCertificateValidationCallback(AddressOf CertificateValidationCallback)
                Dim sslStream As New Security.SslStream(client.GetStream(), False, callback)
                sslStream.AuthenticateAsClient(DRShost, collection, SslProtocols.[Default], False)
    
                If sslStream.IsAuthenticated Then
                    'All of these return TRUE except the last one
                    MsgBox("IsAuthenticated: " & sslStream.IsAuthenticated)
                    MsgBox("IsMutuallyAuthenticated: " & sslStream.IsMutuallyAuthenticated)
                    MsgBox("IsEncrypted: " & sslStream.IsEncrypted)
                    MsgBox("IsSigned: " & sslStream.IsSigned)
                    MsgBox("IsServer: " & sslStream.IsServer)
                End If
                sslStream.Write(data, 0, data.Length)
                MsgBox("Sent: " & hello.ToString)
                data = New [Byte](256) {}
                Dim bytes As Integer = sslStream.Read(data, 0, data.Length)
                Dim responseData As String = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
                MsgBox("Received: " & responseData)
            Catch ex As AuthenticationException
                MsgBox(ex.Message)
            Catch ex As SocketException
                MsgBox(ex.Message)
            Catch ex As IOException
                MsgBox(ex.Message)
            Finally
                If sslStream IsNot Nothing Then
                    sslStream.Close()
                End If
            End Try
    Response:



    NetworkStream
    Code:
            'Create a collection and add the certificate
            Dim collection = New X509Certificate2Collection()
            collection.Import("C:\\Users\\Drew\\Desktop\\testcert\\FIcert.pfx", "password", X509KeyStorageFlags.DefaultKeySet)
            Dim store = New X509Store(StoreName.My)
            store.Open(OpenFlags.ReadWrite)
    
            Try
                For Each certificate As X509Certificate2 In collection
                    store.Add(certificate)
                Next
            Finally
                store.Close()
            End Try
    
    
            'Create the Hello EPP request and put it into a String
            Dim hello As XElement = <epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
                                        <hello/>
                                    </epp>
    
    
            'Create client and networkstream, convert XML and write it
            Dim client As TcpClient = New TcpClient(DRShost, 700)
            Dim tcpNetStream As NetworkStream = client.GetStream()
            Dim sslStream As New Security.SslStream(tcpNetStream, False, New RemoteCertificateValidationCallback(AddressOf CertificateValidationCallback), Nothing)
            sslStream.AuthenticateAsClient(DRShost, collection, SslProtocols.[Default], False)
            Dim encodedBytes As Byte() = ASCIIEncoding.ASCII.GetBytes(hello.ToString)
    
            tcpNetStream.Write(encodedBytes, 0, encodedBytes.Length)
    
            Dim bytesToReceive As Byte() = New Byte(client.ReceiveBufferSize - 1) {}
            Dim bytesRead As Integer = tcpNetStream.Read(bytesToReceive, 0, client.ReceiveBufferSize)
            MsgBox("Received : " & Encoding.ASCII.GetString(bytesToReceive, 0, bytesRead))
            client.Close()
    Response:



    I thought maybe the response from the second set of code was coming back encrypted, so I changed this line:
    Code:
    Dim bytesRead As Integer = tcpNetStream.Read(bytesToReceive, 0, client.ReceiveBufferSize)
    To read from the sslStream instead. However, this just leads to once again receiving a blank response:
    Code:
    Dim bytesRead As Integer = sslStream.Read(bytesToReceive, 0, client.ReceiveBufferSize)
    Any help on this would be greatly appreciated. I've scoured the web and tried everything I could think of, but to no avail. I do notice that when I view the string I'm sending it does not contain "<?xml version="1.0" encoding="UTF-8" standalone="no"?>". However, I believe that is added automatically by XElement?
    Last edited by 2kaud; September 29th, 2018 at 04:22 AM. Reason: Fixed code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problems with SSLstream.Read

    Have you contacted the company for assistance?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured