1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Connect to EPP Server with SSLstream & PEM files

Discussion in 'Visual Basic .NET' started by mixing, May 7, 2016.

  1. mixing

    mixing Regular Member

    Joined:
    Jan 18, 2014
    Messages:
    279
    Likes Received:
    33
    Gender:
    Male
    Location:
    USA
    Home Page:
    I hope someone can help me out with this, it involves using TCPClient/SSLStream for connecting to an EPP gateway. A certificate is also required in order to establish an authenticated and encrypted communications channel between my program and the registry.

    According to the registry I must pass 3 files:
    cert.pem - public key (my x.509 certificate)
    key.pem - my private key.
    cacert.pem - The Root Certificate for the Certificate Authority that signed your certificate.

    I've gone out and purchased my SSL. I then created my key file, downloaded my certificate along with the root certificate, and converted all of these to PEM using openssl. The issue is, now that I have all of these, I'm not really sure how I would go about passing them with my SSLstream connection in vb.net..

    Here is the current code I have
    Code:
        Dim certificatePem As String = "C:\cert.pem"
        Dim keyPem As String = "C:\key.pem"
        Dim cacertPem As String = "C:\cacert.pem"
    
        Private Sub Thread1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Thread1.DoWork
            Dim client As New TcpClient(DRShost, 700)
            Dim sslStream As New Security.SslStream(client.GetStream(), True)
    
            Dim cert As New X509Certificate(certificatePem)
            Dim key As New X509Certificate2(keyPem, "ChallengePasswordSetupWhenCreatingMyKey")
            Dim cacert As New X509Certificate(cacertPem)
            Dim clientCertificatecollection As New X509CertificateCollection()
            clientCertificatecollection.Add(cert)
            clientCertificatecollection.Add(key)
            clientCertificatecollection.Add(cacert)
            sslStream.AuthenticateAsClient(DRShost, clientCertificatecollection, SslProtocols.[Default], False)
    
            Dim greeting As String = GetResponse(sslStream, System.Text.Encoding.UTF8)
    
            MsgBox(greeting)
        End Sub
    
    As you can see from the code above, I'm guessing that I need to create a collection for all of these certificates and since my key is not actually a certificate (crt) I'm guessing this needs to be passed as X509Certificate2?

    When running the code above this is what I get
    CryptographicException was unhandled by user
    Cannot find the requested object.

    This happens on this line:
    Dim key As New X509Certificate2(keyPem, "ChallengePasswordSetupWhenCreatingMyKey")

    Any ideas what I'm doing wrong or should be doing?
     
  2. mixing

    mixing Regular Member

    Joined:
    Jan 18, 2014
    Messages:
    279
    Likes Received:
    33
    Gender:
    Male
    Location:
    USA
    Home Page:
    Just a quick update.. Apparently I cannot put an X509Certificate2 into a X509CertificateCollection. I tried creating a X509Certificate2Collection, but am unsure exactly how I would include this in the SSLstream. For example:
    Code:
            Dim client As New TcpClient(DRShost, 700)
            Dim sslStream As New Security.SslStream(client.GetStream(), True)
    
            Dim certificate As New X509Certificate(certificatePem)
            Dim cacertificate As New X509Certificate(cacertPem)
            Dim key As New X509Certificate2(keyPem)
            Dim x509CertificateCollection1 As New X509CertificateCollection
            Dim x509CertificateCollection2 As New X509Certificate2Collection
            x509CertificateCollection1.Add(certificate)
            x509CertificateCollection1.Add(cacertificate)
            x509CertificateCollection2.Add(key)
    
            sslStream.AuthenticateAsClient(DRShost, x509CertificateCollection1, SslProtocols.Default, False)
    
    The problem here is that my x509CertificateCollection2 is still not being included. Any suggestions?
     
  3. Netro

    Netro Junior Member

    Joined:
    Jun 12, 2015
    Messages:
    107
    Likes Received:
    27
    Occupation:
    Baka Dev.
    Dim certificatePem As String = "C:\cert.pem"
    Dim keyPem As String = "C:\key.pem"
    Dim cacertPem As String = "C:\cacert.pem"

    This may be your problem. You basically load in these strings the files paths, not their contents.

    Use File.ReadAllText Function

    Example: Dim certificatePem As String = File.ReadAllText("C:\cert.pem")