10000 ManagedOpenSsl in .net core throws “System.BadImageFormatException" · Issue #50 · openssl-net/openssl-net · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

ManagedOpenSsl in .net core throws “System.BadImageFormatException" #50

Open
sivshan opened this issue Feb 18, 2018 · 1 comment
Open

Comments

@sivshan
Copy link
sivshan commented Feb 18, 2018

Guys, I have been facing multiple challenges with getting the ManagedOpenSsl to work with .Net Core. I downloaded ManagedOpenSsl.NetCore from nuget package.

My end goal is to create a .pfx file from certificate PEM and private key. In the below code, ocert.KeyPair.PrivateKey is the certificate private key and ocert.CertificatePem is the certificate PEM.

ManagedOpenSsl.NetCore.Core.BIO key_bio = new ManagedOpenSsl.NetCore.Core.BIO(ocert.KeyPair.PrivateKey);
            ManagedOpenSsl.NetCore.Core.BIO cert_bio = new ManagedOpenSsl.NetCore.Core.BIO(ocert.CertificatePem);

            ManagedOpenSsl.NetCore.Crypto.CryptoKey key = ManagedOpenSsl.NetCore.Crypto.CryptoKey.FromPrivateKey(ocert.KeyPair.PrivateKey, "xxxxx");
            ManagedOpenSsl.NetCore.X509.X509Certificate cert = new ManagedOpenSsl.NetCore.X509.X509Certificate(cert_bio);
            ManagedOpenSsl.NetCore.Core.Stack<ManagedOpenSsl.NetCore.X509.X509Certificate> hmm = new ManagedOpenSsl.NetCore.Core.Stack<ManagedOpenSsl.NetCore.X509.X509Certificate>();

            var pfx = new ManagedOpenSsl.NetCore.X509.PKCS12(password, key, cert, hmm); // <-- 
            ManagedOpenSsl.NetCore.X509.X509Certificate certpfx = pfx.Certificate;

I'm getting error {System.TypeInitializationException: The type initializer for 'ManagedOpenSsl.NetCore.Core.Native' threw an exception. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at ManagedOpenSsl.NetCore.Core.Native.SSLeay() at ManagedOpenSsl.NetCore.Core.Version.get_Library() at ManagedOpenSsl.NetCore.Core.Native..cctor() --- End of inner exception stack trace - in the line of code `ManagedOpenSsl.NetCore.Core.BIO key_bio = new ManagedOpenSsl.NetCore.Core.BIO(ocert.KeyPair.PrivateKey);

I have set the Platform Target to "Any CPU", still the issue occurs. Also any implementation on how to save the pfx.Certificate to a specific folder in .pfx format ? Any help will be appreciated. Been struggling with this issue for a while now.

@SelaO
Copy link
SelaO commented Nov 13, 2019

@sivshan did you manage to solve this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0