Last active
September 19, 2018 22:22
-
-
Save Dviejopomata/a8973f5f7c7b96afe4c3cb1bfc04559a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Org.BouncyCastle.Crypto; | |
using Org.BouncyCastle.Crypto.Parameters; | |
using Org.BouncyCastle.OpenSsl; | |
using Org.BouncyCastle.Pkcs; | |
using Org.BouncyCastle.Security; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
using X509Certificate = Org.BouncyCastle.X509.X509Certificate; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
private X509Certificate2 LoadCertificate() | |
{ | |
var sslKey = Environment.GetEnvironmentVariable("SSL_KEY"); | |
var sslCertficate = Environment.GetEnvironmentVariable("SSL_CERTIFICATE"); | |
var keyBytes = Convert.FromBase64String(sslKey); | |
var crtBytes = Convert.FromBase64String(sslCertficate); | |
var pkcs12Store = new Pkcs12Store(); | |
var pr1 = new PemReader(new StringReader(Encoding.UTF8.GetString(crtBytes))); | |
var cert = (X509Certificate) pr1.ReadObject(); | |
var certEntry = new X509CertificateEntry(cert); | |
pkcs12Store.SetCertificateEntry("", certEntry); | |
var pr = new PemReader(new StringReader(Encoding.UTF8.GetString(keyBytes))); | |
var kp = (AsymmetricCipherKeyPair) pr.ReadObject(); | |
pkcs12Store.SetKeyEntry("", new AsymmetricKeyEntry(kp.Private), new[] {certEntry}); | |
using (var pfxStream = new MemoryStream()) | |
{ | |
pkcs12Store.Save(pfxStream, "".ToCharArray(), new SecureRandom()); | |
pfxStream.Seek(0, SeekOrigin.Begin); | |
return new X509Certificate2(pfxStream.ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment