当前位置: 动力学知识库 > 问答 > 编程问答 >

ECDSA PrivateKey as PEM from PKCS12 using OPENSSL C#

问题描述:

I need to retrieve the PrivateKey either as string or PEM from a PKCS12 file which uses ECDSA-SHA256 signing algorithm. Public key retrieval is fine but for signing purpose I need to retrieve the PrivateKey.

I am using ManagedOpenSSL.dll as the wrapper for my purpose but it doesn't contain a method like:

PEM_write_PrivateKey(IntPtr bp, IntPtr x,IntPtr enc, byte[] kstr,int klen, pem_password_cb cb, IntPtr user_data)

When I tried to call the same method from underlying libeay32.dll in ManagedOPENSSL source it throws the following error while executing the above statement:

OPENSSL_Uplink(1113B000,08):no OPENSSL_APPLINK

The libeay32.dll and ssleay32.dll versions are 1.0.21.

Code Snippet:

BIO bio;

bio = BIO.MemoryBuffer();

byte[] rootcert = System.IO.File.ReadAllBytes(path);

bio.Write(rootcert);

bio.SetClose(BIO.CloseOption.Close);

PKCS12 pk = new PKCS12(bio, passwd);

OpenSSL.X509.X509Certificate cert1 = pkcs.Certificate;

OpenSSL.Crypto.CryptoKey evp = cert1.PrivateKey;

ManagedOPENSLL source in Native.cs I added

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]

public extern static int PEM_write_PrivateKey(IntPtr bp, IntPtr x,IntPtr enc, byte[] kstr,int klen, pem_password_cb cb, IntPtr user_data);

when OpenSSL.Crypto.CryptoKey evp is expanded in Visual Studio intellisense

Can anyone please tell me where I am going wrong or is there a better way to retrieve the Private Key?

分享给朋友:
您可能感兴趣的文章:
随机阅读: