Oocx.ReadX509CertificateFromPem
1.0.0
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
<PackageReference Include="Oocx.ReadX509CertificateFromPem" Version="1.0.0" />
paket add Oocx.ReadX509CertificateFromPem --version 1.0.0
#r "nuget: Oocx.ReadX509CertificateFromPem, 1.0.0"
// 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
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 | Versions 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. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.WebUtilities (>= 2.2.0)
- System.Security.Cryptography.Pkcs (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release