committing initial code. It was a fun day!

Gemini is very nice and simple <3
This commit is contained in:
Egidijus Lileika
2021-02-06 17:59:11 +02:00
parent b9fc631211
commit 69435b34a5
9 changed files with 392 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
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;
}
}
}