|
| 1 | +using k8s.Exceptions; |
| 2 | +using Org.BouncyCastle.Asn1.Nist; |
| 3 | +using Org.BouncyCastle.Asn1.Pkcs; |
| 4 | +using Org.BouncyCastle.Crypto; |
| 5 | +using Org.BouncyCastle.OpenSsl; |
| 6 | +using Org.BouncyCastle.Pkcs; |
| 7 | +using Org.BouncyCastle.Security; |
| 8 | +using Org.BouncyCastle.X509; |
| 9 | +using System.IO; |
| 10 | +using System.Security.Cryptography.X509Certificates; |
| 11 | + |
| 12 | +namespace k8s |
| 13 | +{ |
| 14 | + internal static class CertUtils |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Load pem encoded cert file |
| 18 | + /// </summary> |
| 19 | + /// <param name="file">Path to pem encoded cert file</param> |
| 20 | + /// <returns>List of x509 instances.</returns> |
| 21 | + public static X509Certificate2Collection LoadPemFileCert(string file) |
| 22 | + { |
| 23 | + var certCollection = new X509Certificate2Collection(); |
| 24 | + using (var stream = FileSystem.Current.OpenRead(file)) |
| 25 | + { |
| 26 | + var certs = new X509CertificateParser().ReadCertificates(stream); |
| 27 | + |
| 28 | + // Convert BouncyCastle X509Certificates to the .NET cryptography implementation and add |
| 29 | + // it to the certificate collection |
| 30 | + // |
| 31 | + foreach (Org.BouncyCastle.X509.X509Certificate cert in certs) |
| 32 | + { |
| 33 | + // This null password is to change the constructor to fix this KB: |
| 34 | + // https://support.microsoft.com/en-us/topic/kb5025823-change-in-how-net-applications-import-x-509-certificates-bf81c936-af2b-446e-9f7a-016f4713b46b |
| 35 | + string nullPassword = null; |
| 36 | + certCollection.Add(new X509Certificate2(cert.GetEncoded(), nullPassword)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return certCollection; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Generates pfx from client configuration |
| 45 | + /// </summary> |
| 46 | + /// <param name="config">Kubernetes Client Configuration</param> |
| 47 | + /// <returns>Generated Pfx Path</returns> |
| 48 | + public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config) |
| 49 | + { |
| 50 | + if (config == null) |
| 51 | + { |
| 52 | + throw new ArgumentNullException(nameof(config)); |
| 53 | + } |
| 54 | + |
| 55 | + byte[] keyData = null; |
| 56 | + byte[] certData = null; |
| 57 | + |
| 58 | + if (!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData)) |
| 59 | + { |
| 60 | + keyData = Convert.FromBase64String(config.ClientCertificateKeyData); |
| 61 | + } |
| 62 | + |
| 63 | + if (!string.IsNullOrWhiteSpace(config.ClientKeyFilePath)) |
| 64 | + { |
| 65 | + keyData = File.ReadAllBytes(config.ClientKeyFilePath); |
| 66 | + } |
| 67 | + |
| 68 | + if (keyData == null) |
| 69 | + { |
| 70 | + throw new KubeConfigException("keyData is empty"); |
| 71 | + } |
| 72 | + |
| 73 | + if (!string.IsNullOrWhiteSpace(config.ClientCertificateData)) |
| 74 | + { |
| 75 | + certData = Convert.FromBase64String(config.ClientCertificateData); |
| 76 | + } |
| 77 | + |
| 78 | + if (!string.IsNullOrWhiteSpace(config.ClientCertificateFilePath)) |
| 79 | + { |
| 80 | + certData = File.ReadAllBytes(config.ClientCertificateFilePath); |
| 81 | + } |
| 82 | + |
| 83 | + if (certData == null) |
| 84 | + { |
| 85 | + throw new KubeConfigException("certData is empty"); |
| 86 | + } |
| 87 | + |
| 88 | + var cert = new X509CertificateParser().ReadCertificate(new MemoryStream(certData)); |
| 89 | + // key usage is a bit string, zero-th bit is 'digitalSignature' |
| 90 | + // See https://www.alvestrand.no/objectid/2.5.29.15.html for more details. |
| 91 | + if (cert != null && cert.GetKeyUsage() != null && !cert.GetKeyUsage()[0]) |
| 92 | + { |
| 93 | + throw new Exception( |
| 94 | + "Client certificates must be marked for digital signing. " + |
| 95 | + "See https://github.com/kubernetes-client/csharp/issues/319"); |
| 96 | + } |
| 97 | + |
| 98 | + object obj; |
| 99 | + using (var reader = new StreamReader(new MemoryStream(keyData))) |
| 100 | + { |
| 101 | + obj = new PemReader(reader).ReadObject(); |
| 102 | + if (obj is AsymmetricCipherKeyPair key) |
| 103 | + { |
| 104 | + var cipherKey = key; |
| 105 | + obj = cipherKey.Private; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + var keyParams = (AsymmetricKeyParameter)obj; |
| 110 | + |
| 111 | + var store = new Pkcs12StoreBuilder() |
| 112 | + .SetKeyAlgorithm(NistObjectIdentifiers.IdAes128Cbc, PkcsObjectIdentifiers.IdHmacWithSha1) |
| 113 | + .Build(); |
| 114 | + store.SetKeyEntry("K8SKEY", new AsymmetricKeyEntry(keyParams), new[] { new X509CertificateEntry(cert) }); |
| 115 | + |
| 116 | + using var pkcs = new MemoryStream(); |
| 117 | + |
| 118 | + store.Save(pkcs, new char[0], new SecureRandom()); |
| 119 | + |
| 120 | + // This null password is to change the constructor to fix this KB: |
| 121 | + // https://support.microsoft.com/en-us/topic/kb5025823-change-in-how-net-applications-import-x-509-certificates-bf81c936-af2b-446e-9f7a-016f4713b46b |
| 122 | + string nullPassword = null; |
| 123 | + |
| 124 | + if (config.ClientCertificateKeyStoreFlags.HasValue) |
| 125 | + { |
| 126 | + return new X509Certificate2(pkcs.ToArray(), nullPassword, config.ClientCertificateKeyStoreFlags.Value); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + return new X509Certificate2(pkcs.ToArray(), nullPassword); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Retrieves Client Certificate PFX from configuration |
| 136 | + /// </summary> |
| 137 | + /// <param name="config">Kubernetes Client Configuration</param> |
| 138 | + /// <returns>Client certificate PFX</returns> |
| 139 | + public static X509Certificate2 GetClientCert(KubernetesClientConfiguration config) |
| 140 | + { |
| 141 | + if (config == null) |
| 142 | + { |
| 143 | + throw new ArgumentNullException(nameof(config)); |
| 144 | + } |
| 145 | + |
| 146 | + if ((!string.IsNullOrWhiteSpace(config.ClientCertificateData) || |
| 147 | + !string.IsNullOrWhiteSpace(config.ClientCertificateFilePath)) && |
| 148 | + (!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData) || |
| 149 | + !string.IsNullOrWhiteSpace(config.ClientKeyFilePath))) |
| 150 | + { |
| 151 | + return GeneratePfx(config); |
| 152 | + } |
| 153 | + |
| 154 | + return null; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments