|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Security; |
| 5 | +using System.Security.Cryptography.X509Certificates; |
| 6 | + |
| 7 | +namespace Elasticsearch.Net |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// A collection of handy baked in server certificate validation callbacks |
| 11 | + /// </summary> |
| 12 | + public static class CertificateValidations |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// DANGEROUS, never use this in production validates ALL certificates to true. |
| 16 | + /// </summary> |
| 17 | + /// <returns>Always true, allowing ALL certificates</returns> |
| 18 | + public static bool AllowAll(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Always false, in effect blocking ALL certificates |
| 22 | + /// </summary> |
| 23 | + /// <returns>Always false, always blocking ALL certificates</returns> |
| 24 | + public static bool DenyAll(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => false; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Helper to create a certificate validation callback based on the certificate authority certificate that we used to |
| 28 | + /// generate the nodes certificates with. This callback expects the CA to be part of the chain as intermediate CA. |
| 29 | + /// </summary> |
| 30 | + /// <param name="caCertificate">The ca certificate used to generate the nodes certificate </param> |
| 31 | + /// <param name="trustRoot">Custom CA are never trusted by default unless they are in the machines trusted store, set this to true |
| 32 | + /// if you've added the CA to the machines trusted store. In which case UntrustedRoot should not be accepted. |
| 33 | + /// </param> |
| 34 | + /// <param name="revocationMode">By default we do not check revocation, it is however recommended to check this (either offline or online).</param> |
| 35 | + public static Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> AuthorityPartOfChain( |
| 36 | + X509Certificate caCertificate, bool trustRoot = true, X509RevocationMode revocationMode = X509RevocationMode.NoCheck) => |
| 37 | + (sender, cert, chain, errors) => |
| 38 | + errors == SslPolicyErrors.None |
| 39 | + || ValidIntermediateCa(caCertificate, cert, chain, trustRoot, revocationMode); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Helper to create a certificate validation callback based on the certificate authority certificate that we used to |
| 43 | + /// generate the nodes certificates with. This callback does NOT expect the CA to be part of the chain presented by the server. |
| 44 | + /// Including the root certificate in the chain increases the SSL handshake size and Elasticsearch's certgen by default does not include |
| 45 | + /// the CA in the certificate chain. |
| 46 | + /// </summary> |
| 47 | + /// <param name="caCertificate">The ca certificate used to generate the nodes certificate </param> |
| 48 | + /// <param name="trustRoot">Custom CA are never trusted by default unless they are in the machines trusted store, set this to true |
| 49 | + /// if you've added the CA to the machines trusted store. In which case UntrustedRoot should not be accepted. |
| 50 | + /// </param> |
| 51 | + /// <param name="revocationMode">By default we do not check revocation, it is however recommended to check this (either offline or online).</param> |
| 52 | + public static Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> AuthorityIsRoot( |
| 53 | + X509Certificate caCertificate, bool trustRoot = true, X509RevocationMode revocationMode = X509RevocationMode.NoCheck) => |
| 54 | + (sender, cert, chain, errors) => |
| 55 | + errors == SslPolicyErrors.None |
| 56 | + || ValidRootCa(caCertificate, cert, chain, trustRoot, revocationMode); |
| 57 | + |
| 58 | + private static X509Certificate2 to2(X509Certificate certificate) |
| 59 | + { |
| 60 | + #if DOTNETCORE |
| 61 | + return new X509Certificate2(certificate.Export(X509ContentType.Cert)); |
| 62 | + #else |
| 63 | + return new X509Certificate2(certificate); |
| 64 | + #endif |
| 65 | + } |
| 66 | + |
| 67 | + private static bool ValidRootCa(X509Certificate caCertificate, X509Certificate certificate, X509Chain chain, bool trustRoot, |
| 68 | + X509RevocationMode revocationMode) |
| 69 | + { |
| 70 | + var ca = to2(caCertificate); |
| 71 | + var privateChain = new X509Chain {ChainPolicy = {RevocationMode = revocationMode}}; |
| 72 | + privateChain.ChainPolicy.ExtraStore.Add(ca); |
| 73 | + privateChain.Build(to2(certificate)); |
| 74 | + |
| 75 | + //lets validate the our chain status |
| 76 | + foreach (var chainStatus in privateChain.ChainStatus) |
| 77 | + { |
| 78 | + //custom CA's that are not in the machine trusted store will always have this status |
| 79 | + //by setting trustRoot = true (default) we skip this error |
| 80 | + if (chainStatus.Status == X509ChainStatusFlags.UntrustedRoot && trustRoot) continue; |
| 81 | + //trustRoot is false so we expected our CA to be in the machines trusted store |
| 82 | + if (chainStatus.Status == X509ChainStatusFlags.UntrustedRoot) return false; |
| 83 | + //otherwise if the chain has any error of any sort return false |
| 84 | + if (chainStatus.Status != X509ChainStatusFlags.NoError) return false; |
| 85 | + } |
| 86 | + return true; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + private static bool ValidIntermediateCa(X509Certificate caCertificate, X509Certificate certificate, X509Chain chain, bool trustRoot, |
| 91 | + X509RevocationMode revocationMode) |
| 92 | + { |
| 93 | + var ca = to2(caCertificate); |
| 94 | + var privateChain = new X509Chain {ChainPolicy = {RevocationMode = revocationMode}}; |
| 95 | + privateChain.ChainPolicy.ExtraStore.Add(ca); |
| 96 | + privateChain.Build(to2(certificate)); |
| 97 | + |
| 98 | + //Assert our chain has the same number of elements as the certifcate presented by the server |
| 99 | + if (chain.ChainElements.Count != privateChain.ChainElements.Count) return false; |
| 100 | + |
| 101 | + //lets validate the our chain status |
| 102 | + foreach (var chainStatus in privateChain.ChainStatus) |
| 103 | + { |
| 104 | + //custom CA's that are not in the machine trusted store will always have this status |
| 105 | + //by setting trustRoot = true (default) we skip this error |
| 106 | + if (chainStatus.Status == X509ChainStatusFlags.UntrustedRoot && trustRoot) continue; |
| 107 | + //trustRoot is false so we expected our CA to be in the machines trusted store |
| 108 | + if (chainStatus.Status == X509ChainStatusFlags.UntrustedRoot) return false; |
| 109 | + //otherwise if the chain has any error of any sort return false |
| 110 | + if (chainStatus.Status != X509ChainStatusFlags.NoError) return false; |
| 111 | + } |
| 112 | + |
| 113 | + var found = false; |
| 114 | + //We are going to walk both chains and make sure the thumbprints align |
| 115 | + //while making sure one of the chains certificates presented by the server has our expected CA thumbprint |
| 116 | + for (var i = 0; i < chain.ChainElements.Count; i++) |
| 117 | + { |
| 118 | + var c = chain.ChainElements[i].Certificate.Thumbprint; |
| 119 | + var cPrivate = privateChain.ChainElements[i].Certificate.Thumbprint; |
| 120 | + if (c == ca.Thumbprint) found = true; |
| 121 | + |
| 122 | + //mis aligned certificate chain, return false so we do not accept this certificate |
| 123 | + if (c != cPrivate) return false; |
| 124 | + i++; |
| 125 | + } |
| 126 | + return found; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments