This repository has been archived on 2023-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
cuipod/Cuipod/CertificateUtils.cs
Egidijus Lileika 69435b34a5 committing initial code. It was a fun day!
Gemini is very nice and simple <3
2021-02-06 17:59:11 +02:00

35 lines
1.1 KiB
C#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace Cuipod
{
public static class CertificateUtils
{
static public X509Certificate2 LoadCertificate(string certFilePath, string privateRSAKeyFilePath)
{
X509Certificate2 cert = new X509Certificate2(certFilePath);
string[] lines = File.ReadAllLines(privateRSAKeyFilePath, Encoding.UTF8);
string privateKeyData = "";
for (int i = 1; i < lines.Length - 1; ++i)
{
privateKeyData += lines[i];
}
byte[] bytes = Convert.FromBase64String(privateKeyData);
RSA rsa = RSA.Create();
// For now we always expect to have cert with BEGIN PRIVATE KEY label
// TODO: add handling for other types, maybe??
rsa.ImportPkcs8PrivateKey(bytes, out _);
X509Certificate2 pubPrivEphemeral = cert.CopyWithPrivateKey(rsa);
cert = new X509Certificate2(pubPrivEphemeral.Export(X509ContentType.Pfx));
return cert;
}
}
}