Files
csharp/src/KubernetesClient/CertUtils.cs

113 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using k8s.Exceptions;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
2019-02-15 11:57:24 -08:00
using X509Certificate = Org.BouncyCastle.X509.X509Certificate;
namespace k8s
{
public static class CertUtils
{
/// <summary>
/// Load pem encoded cert file
/// </summary>
/// <param name="file">Path to pem encoded cert file</param>
/// <returns>x509 instance.</returns>
public static X509Certificate2 LoadPemFileCert(string file)
{
2019-02-15 11:57:24 -08:00
var certs = new X509CertificateParser().ReadCertificates(File.OpenRead(file));
var store = new Pkcs12StoreBuilder().Build();
foreach (X509Certificate cert in certs)
{
store.SetCertificateEntry(Guid.NewGuid().ToString(), new X509CertificateEntry(cert));
}
2019-02-15 11:57:24 -08:00
using (var pkcs = new MemoryStream())
{
store.Save(pkcs, new char[0], new SecureRandom());
// TODO not a chain
return new X509Certificate2(pkcs.ToArray());
}
}
/// <summary>
/// Generates pfx from client configuration
/// </summary>
2018-03-12 17:55:21 -04:00
/// <param name="config">Kubernetes Client Configuration</param>
/// <returns>Generated Pfx Path</returns>
2017-09-27 21:51:00 -07:00
public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
{
byte[] keyData = null;
byte[] certData = null;
if (!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData))
{
keyData = Convert.FromBase64String(config.ClientCertificateKeyData);
}
if (!string.IsNullOrWhiteSpace(config.ClientKeyFilePath))
{
keyData = File.ReadAllBytes(config.ClientKeyFilePath);
}
if (keyData == null)
{
throw new KubeConfigException("keyData is empty");
}
if (!string.IsNullOrWhiteSpace(config.ClientCertificateData))
{
certData = Convert.FromBase64String(config.ClientCertificateData);
}
if (!string.IsNullOrWhiteSpace(config.ClientCertificateFilePath))
{
certData = File.ReadAllBytes(config.ClientCertificateFilePath);
}
if (certData == null)
{
throw new KubeConfigException("certData is empty");
}
var cert = new X509CertificateParser().ReadCertificate(new MemoryStream(certData));
object obj;
using (var reader = new StreamReader(new MemoryStream(keyData)))
{
obj = new PemReader(reader).ReadObject();
var key = obj as AsymmetricCipherKeyPair;
if (key != null)
{
var cipherKey = key;
2017-09-27 21:51:00 -07:00
obj = cipherKey.Private;
}
}
var rsaKeyParams = (RsaPrivateCrtKeyParameters) obj;
var store = new Pkcs12StoreBuilder().Build();
store.SetKeyEntry("K8SKEY", new AsymmetricKeyEntry(rsaKeyParams), new[] {new X509CertificateEntry(cert)});
using (var pkcs = new MemoryStream())
{
2017-10-13 03:34:24 +08:00
store.Save(pkcs, new char[0], new SecureRandom());
if (config.ClientCertificateKeyStoreFlags.HasValue)
{
return new X509Certificate2(pkcs.ToArray(), "", config.ClientCertificateKeyStoreFlags.Value);
}
else
{
return new X509Certificate2(pkcs.ToArray());
}
}
}
}
}