4

What is the difference between

    Dim pCa As IntPtr = 
        GCHandle.Alloc(New IntPtr, GCHandleType.Pinned).AddrOfPinnedObject

and

    Dim pCa as IntPtr = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(pCa, Marshal.AllocHGlobal(4))   ' **stach_st_X509

?

I am trying to port pkread.c to a .NET function, PFXtoPEM().

The source for PFXtoPEM() is available at libCurlVB.NET-native.

This is what I have so far:

'''Return Type: int, 0 = success or 1 = failure
'''PFXinfile: String
'''password: String
'''PEMoutfile: String
Public Shared Function PFXtoPEM(ByVal PFXinfile As String, ByVal password As String, ByVal PEMoutfile As String) As Integer
    Dim fp As IntPtr = fopen(PFXinfile, "rb")
    Dim p12 As IntPtr
    Dim cert As X509
    Dim ca As stack_st_X509

    Dim pPkey As IntPtr = GCHandle.Alloc(New IntPtr, GCHandleType.Pinned).AddrOfPinnedObject
    
    Dim pCert As IntPtr = GCHandle.Alloc(New IntPtr, GCHandleType.Pinned).AddrOfPinnedObject

    Dim pCa As IntPtr = GCHandle.Alloc(New IntPtr, GCHandleType.Pinned).AddrOfPinnedObject


    If fp.Equals(IntPtr.Zero) Then
        MessageBox.Show(String.Concat("Error opening file: ", PFXinfile), "Error Converting PFX to PEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Return 1
    End If

    Dim StdErr As IntPtr = freopen("NUL", "a", IntPtr.op_Explicit(__iob_func.ToInt64() + (Marshal.SizeOf(New FILEp) * 2)))
    Const errbufSize = 8192
    Dim errBuf As IntPtr = Marshal.AllocHGlobal(errbufSize)

    ERR_load_crypto_strings()
    memset(errBuf, Asc(vbNullChar), errbufSize)
    setvbuf(StdErr, errBuf, _IOFBF, errbufSize)

    OPENSSL_add_all_algorithms_noconf()

    Try
        p12 = d2i_PKCS12_fp(fp, IntPtr.Zero)
        If p12.Equals(IntPtr.Zero) Then Throw New SEHException("d2i_PKCS12_fp didn't throw but also didn't succeed")
    Catch ex As SEHException
        ERR_print_errors_fp(StdErr)
        MessageBox.Show(String.Concat("Error reading PKCS#12 file. " & vbNewLine, Marshal.PtrToStringAnsi(errBuf)), "Error Converting PFX to PEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Return 1
    End Try

    fclose(fp)

    Try

        PKCS12_parse(p12, password, pPkey, pCert, pCa)

    Catch ex As Exception
        ERR_print_errors_fp(StdErr)
        MessageBox.Show(String.Concat("Error parsing PKCS#12 file, check pfx password. " & vbNewLine, Marshal.PtrToStringAnsi(errBuf)), "Error Converting PFX to PEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Return 1
    End Try

    PKCS12_free(p12)

    fp = fopen(PEMoutfile, "w")
    If fp.Equals(IntPtr.Zero) Then
        MessageBox.Show(String.Concat("Error opening file: ", PEMoutfile), "Error Converting PFX to PEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Return 1
    End If


    If Not IntPtr.op_Explicit(Marshal.ReadInt32(pPkey)).Equals(IntPtr.Zero) Then
        fprintf(fp, "****Private Key****" & vbNewLine)
        PEM_write_PrivateKey(fp, IntPtr.op_Explicit(Marshal.ReadInt32(pPkey)), Nothing, Nothing, Nothing, Nothing, Nothing)
    End If


    If Not IntPtr.op_Explicit(Marshal.ReadInt32(pCert)).Equals(IntPtr.Zero) Then
        fprintf(fp, "***User Certificate***" & vbNewLine)
        fprintf(fp, "subject=")
        fprintf(fp, X509_NAME_oneline(IntPtr.op_Explicit(Marshal.ReadInt32(IntPtr.op_Explicit(Marshal.ReadInt32(Marshal.ReadInt32(pCert)) + 20))), 0, 0))
        fprintf(fp, vbNewLine & "issuer=")
        fprintf(fp, X509_NAME_oneline(IntPtr.op_Explicit(Marshal.ReadInt32(IntPtr.op_Explicit(Marshal.ReadInt32(Marshal.ReadInt32(pCert)) + 12))), 0, 0))
        fprintf(fp, vbNewLine)
        PEM_write_X509_AUX(fp, IntPtr.op_Explicit((Marshal.ReadInt32(pCert))))
    End If


    If Not IntPtr.op_Explicit(Marshal.ReadInt32(pCert)).Equals(IntPtr.Zero) Then
        ca = Marshal.PtrToStructure(IntPtr.op_Explicit(Marshal.ReadInt32(pCa)), GetType(stack_st_X509))
        fprintf(fp, "****CA Certificates****" & vbNewLine)

        For i = 0 To ca.stack.num - 1

            cert = Marshal.PtrToStructure(IntPtr.op_Explicit(Marshal.ReadInt32(IntPtr.op_Explicit(ca.stack.data.ToInt32 + 4 * i))), GetType(X509))
            Dim certGChandle As GCHandle = GCHandle.Alloc(cert.cert_info, GCHandleType.Pinned)
            Dim pCertInfo As IntPtr = certGChandle.AddrOfPinnedObject

            fprintf(fp, "subject=")
            fprintf(fp, X509_NAME_oneline(IntPtr.op_Explicit(Marshal.ReadInt32(IntPtr.op_Explicit(Marshal.ReadInt32(pCertInfo) + 20))), 0, 0))
            fprintf(fp, vbNewLine & "issuer=")
            fprintf(fp, X509_NAME_oneline(IntPtr.op_Explicit(Marshal.ReadInt32(IntPtr.op_Explicit(Marshal.ReadInt32(pCertInfo) + 12))), 0, 0))
            fprintf(fp, vbNewLine)
            PEM_write_X509_AUX(fp, IntPtr.op_Explicit(Marshal.ReadInt32(IntPtr.op_Explicit(ca.stack.data.ToInt32 + 4 * i))))
            certGChandle.Free()

        Next

    End If

    fclose(fp)


    Marshal.FreeHGlobal(errBuf)

    Return 0

End Function

Now, this function works, it converts PFX to PEM succesfully, supplied the right password. But, I didn't want to init pPkey,pCert, and pCa with GCHandle. So I changed it to this:

'''Return Type: int, 0 = success or 1 = failure
'''PFXinfile: String
'''password: String
'''PEMoutfile: String
Public Shared Function PFXtoPEM(ByVal PFXinfile As String, ByVal password As String, ByVal PEMoutfile As String) As Integer
    Dim fp As IntPtr = fopen(PFXinfile, "rb")
    Dim p12 As IntPtr
    Dim cert As X509
    Dim ca As stack_st_X509

    Dim pPkey As IntPtr = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(pPkey, Marshal.AllocHGlobal(4)) ' **EVP_PKEY
    Dim pCert As IntPtr = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(pCert, Marshal.AllocHGlobal(4)) ' **X509
    Dim pCa As IntPtr = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(pCa, Marshal.AllocHGlobal(4))   ' **stach_st_X509

    '...

    fclose(fp)

    Marshal.FreeHGlobal(IntPtr.op_Explicit(Marshal.ReadInt32(pPkey)))
    Marshal.FreeHGlobal(IntPtr.op_Explicit(Marshal.ReadInt32(pCert)))
    Marshal.FreeHGlobal(IntPtr.op_Explicit(Marshal.ReadInt32(pCa)))
    Marshal.FreeHGlobal(pPkey)
    Marshal.FreeHGlobal(pCert)
    Marshal.FreeHGlobal(pCa)

    Marshal.FreeHGlobal(errBuf)

    Return 0

End Function

Now this doesn't work. PKCS12_parse ,

<DllImport("libeay32.dll", _
SetLastError:=True, _
CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function PKCS12_parse( _
ByVal p12 As IntPtr, _
<MarshalAs(UnmanagedType.LPStr)> ByVal pass As String, _
ByVal pkey As IntPtr, _
ByVal cert As IntPtr, _
ByVal ca As IntPtr) As Integer
End Function 

throws System.AccessViolationException

However this works:

'''Return Type: int, 0 = success or 1 = failure
'''PFXinfile: String
'''password: String
'''PEMoutfile: String
Public Shared Function PFXtoPEM(ByVal PFXinfile As String, ByVal password As String, ByVal PEMoutfile As String) As Integer
    Dim fp As IntPtr = fopen(PFXinfile, "rb")
    Dim p12 As IntPtr
    Dim cert As X509
    Dim ca As stack_st_X509

    Dim pPkey As IntPtr = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(pPkey, Marshal.AllocHGlobal(4)) ' **EVP_PKEY
    Dim pCert As IntPtr = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(pCert, Marshal.AllocHGlobal(4)) ' **X509
    Dim pCa As IntPtr = GCHandle.Alloc(New IntPtr, GCHandleType.Pinned).AddrOfPinnedObject
    
    '...

    fclose(fp)

    Debug.Print(IntPtr.op_Explicit(Marshal.ReadInt32(pPkey)))
    Marshal.FreeHGlobal(IntPtr.op_Explicit(Marshal.ReadInt32(pPkey)))
    Marshal.FreeHGlobal(IntPtr.op_Explicit(Marshal.ReadInt32(pCert)))
    Marshal.FreeHGlobal(pPkey)
    Marshal.FreeHGlobal(pCert)

    Marshal.FreeHGlobal(errBuf)

    Return 0

End Function
4
  • 1
    You are leaking memory all over the place. GCHandle is an object (structure really) and when you allocate one you need to hold onto it! Otherwise you are unable to free it later. Likewise, AllocHGlobal is grabbing memory that you need to release at some point.
    – tcarvin
    Sep 25, 2013 at 19:10
  • @tcarvin, I'm aware of that.
    – Motomotes
    Sep 25, 2013 at 19:25
  • You are making assumptions on the size of a Pointer in your code. Is the process being run as 64 bit?
    – tcarvin
    Sep 30, 2013 at 13:12
  • No, my machine is 32 bit, libeay32.dll is a 32 bit library.
    – Motomotes
    Nov 7, 2013 at 12:59

1 Answer 1

2

The problem was that special memory freeing functions within the OpenSSL library are needed to be called to ensure the memory is properly released:

  EVP_PKEY_free(IntPtr.op_Explicit(Marshal.ReadInt32(pPkey)))
  X509_free(IntPtr.op_Explicit(Marshal.ReadInt32(pCert)))
  sk_free(IntPtr.op_Explicit(Marshal.ReadInt32(pCa)))
 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.