2017-10-13 03:06:35 +08:00
|
|
|
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;
|
2017-09-22 20:59:41 -07:00
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
namespace k8s
|
|
|
|
|
{
|
2017-11-08 14:22:10 +08:00
|
|
|
public static class CertUtils
|
2017-09-22 20:59:41 -07:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-10-11 21:27:22 +08:00
|
|
|
/// 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));
|
|
|
|
|
}
|
2017-10-11 21:27:22 +08:00
|
|
|
|
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());
|
|
|
|
|
}
|
2017-10-11 21:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates pfx from client configuration
|
2017-09-22 20:59:41 -07:00
|
|
|
/// </summary>
|
2018-03-12 17:55:21 -04:00
|
|
|
/// <param name="config">Kubernetes Client Configuration</param>
|
2017-09-22 20:59:41 -07:00
|
|
|
/// <returns>Generated Pfx Path</returns>
|
2017-09-27 21:51:00 -07:00
|
|
|
public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
|
2017-09-22 20:59:41 -07:00
|
|
|
{
|
2017-10-13 03:06:35 +08:00
|
|
|
byte[] keyData = null;
|
|
|
|
|
byte[] certData = null;
|
2017-09-22 20:59:41 -07:00
|
|
|
|
2017-10-13 01:53:25 +08:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData))
|
2017-09-22 20:59:41 -07:00
|
|
|
{
|
2017-10-13 01:53:25 +08:00
|
|
|
keyData = Convert.FromBase64String(config.ClientCertificateKeyData);
|
2017-09-22 20:59:41 -07:00
|
|
|
}
|
2017-10-13 01:53:25 +08:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientKeyFilePath))
|
2017-09-22 20:59:41 -07:00
|
|
|
{
|
2017-10-13 01:53:25 +08:00
|
|
|
keyData = File.ReadAllBytes(config.ClientKeyFilePath);
|
2017-09-22 20:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
if (keyData == null)
|
|
|
|
|
{
|
2018-06-12 00:09:51 +04:00
|
|
|
throw new KubeConfigException("keyData is empty");
|
2017-10-13 03:06:35 +08:00
|
|
|
}
|
|
|
|
|
|
2017-09-22 20:59:41 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateData))
|
|
|
|
|
{
|
|
|
|
|
certData = Convert.FromBase64String(config.ClientCertificateData);
|
|
|
|
|
}
|
2017-10-13 01:53:25 +08:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateFilePath))
|
2017-09-22 20:59:41 -07:00
|
|
|
{
|
2017-10-13 01:53:25 +08:00
|
|
|
certData = File.ReadAllBytes(config.ClientCertificateFilePath);
|
2017-09-22 20:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
if (certData == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KubeConfigException("certData is empty");
|
|
|
|
|
}
|
2017-09-22 20:59:41 -07:00
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
var cert = new X509CertificateParser().ReadCertificate(new MemoryStream(certData));
|
|
|
|
|
|
|
|
|
|
object obj;
|
2017-09-22 20:59:41 -07:00
|
|
|
using (var reader = new StreamReader(new MemoryStream(keyData)))
|
|
|
|
|
{
|
2017-10-13 03:06:35 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2017-10-13 03:06:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
2019-02-07 07:58:08 +01:00
|
|
|
|
|
|
|
|
if (config.ClientCertificateKeyStoreFlags.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new X509Certificate2(pkcs.ToArray(), "", config.ClientCertificateKeyStoreFlags.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new X509Certificate2(pkcs.ToArray());
|
|
|
|
|
}
|
2017-09-22 20:59:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-11 21:27:22 +08:00
|
|
|
}
|