Oocx.ReadX509CertificateFromPem 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Oocx.ReadX509CertificateFromPem --version 1.0.0
NuGet\Install-Package Oocx.ReadX509CertificateFromPem -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Oocx.ReadX509CertificateFromPem" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Oocx.ReadX509CertificateFromPem --version 1.0.0
#r "nuget: Oocx.ReadX509CertificateFromPem, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Oocx.ReadX509CertificateFromPem as a Cake Addin
#addin nuget:?package=Oocx.ReadX509CertificateFromPem&version=1.0.0

// Install Oocx.ReadX509CertificateFromPem as a Cake Tool
#tool nuget:?package=Oocx.ReadX509CertificateFromPem&version=1.0.0

Build Status Quality Gate Status Coverage Maintainability Rating Vulnerabilities Security Rating

Read certificates and private keys from PEM files

X509 certificates and private keys can be stored in different formats. Not all of them are supported out of the box by the .NET class library.

Certificates with private keys can currently only be loaded from .pfx files (PKCS#12).

For RSA, DSA, and ECDsa, there is X509Certificate2.CopyWithPrivateKey() which could be used to add a private key to a certificate. However, there is no overload that supports an ECDH private key. .NET Core 3 adds a Pkcs12Builder class. This class can be used to create a pfx file at runtime. This allows us to read certificate and private key from a PEM file to create a new X509Certificate2 instance.

This library does exactly that. You can either copy the source to your own project or reference it as a NuGet package (TODO - this is work in progress, I did not publish a package yet).

So assuming you have two files, certificate.pem and key.pem, you can now can then easily create a new X509Certificate2 from those files:

var reader = new CertificateFromPemReader();
X509Certificate2 myCertificate = reader.LoadCertificateWithPrivateKey("certificate.pem", "key.pem");

As .NET core uses different, platform specific implementations for cryptography, not all types of keys will work on all platforms.

I'm using this code in a .net core container that runs on kubernetes and gets its certificate from cert-manager, which proviedes certificaets as Kubernetes secrets with key and certificate in PEM format.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.3 82,691 5/23/2020
1.1.0 667 1/22/2020
1.0.0 493 1/5/2020

Initial release