Handle additional X509 chain statuses for macOS
authorKevin Jones <kevin@vcsjones.com>
Mon, 27 Apr 2020 21:25:48 +0000 (17:25 -0400)
committerGitHub <noreply@github.com>
Mon, 27 Apr 2020 21:25:48 +0000 (14:25 -0700)
MacOS returns a different status string for certificates that are in a special
database that are explicitly distrusted. Windows has similar behavior, which
reports the certificates as PAL_X509ChainExplicitDistrust. This makes macOS
do the same instead of throwing an exception.

Linux does not appear to have any special distrusting for these
certificates.

src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509chain.c
src/libraries/System.Security.Cryptography.X509Certificates/tests/ChainTests.cs

index 76dfb3e..9103292 100644 (file)
@@ -169,7 +169,8 @@ static void MergeStatusCodes(CFTypeRef key, CFTypeRef value, void* context)
     else if (CFEqual(keyString, CFSTR("BasicConstraints")) || CFEqual(keyString, CFSTR("BasicConstraintsCA")) ||
              CFEqual(keyString, CFSTR("BasicConstraintsPathLen")))
         *pStatus |= PAL_X509ChainInvalidBasicConstraints;
-    else if (CFEqual(keyString, CFSTR("UsageConstraints")))
+    else if (CFEqual(keyString, CFSTR("UsageConstraints")) || CFEqual(keyString, CFSTR("BlackListedLeaf")) ||
+             CFEqual(keyString, CFSTR("BlackListedKey")))
         *pStatus |= PAL_X509ChainExplicitDistrust;
     else if (CFEqual(keyString, CFSTR("RevocationResponseRequired")))
         *pStatus |= PAL_X509ChainRevocationStatusUnknown;
index b6a0367..751b35e 100644 (file)
@@ -1063,6 +1063,145 @@ tHP28fj0LUop/QFojSZPsaPAW6JvoQ0t4hd6WoyX6z7FsA==
             }
         }
 
+        [Fact]
+        [PlatformSpecific(~TestPlatforms.Linux)]
+        public static void BuildChainForFraudulentCertificate()
+        {
+            // This certificate is a misissued certificate for a "high-value"
+            // domain, mail.google.com. Windows and macOS give this certificate
+            // special distrust treatment beyond normal revocation, resulting
+            // in ExplicitDistrust. OpenSSL relies on normal revocation routines
+            // to distrust this certificate, so we skip this test on Linux.
+
+            byte[] certBytes = Convert.FromBase64String(@"
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ificate2(certBytes))
+            using (ChainHolder chainHolder = new ChainHolder())
+            {
+                X509Chain chain = chainHolder.Chain;
+                chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
+                chain.ChainPolicy.VerificationTime = cert.NotBefore.AddHours(2);
+                Assert.False(chain.Build(cert));
+
+                X509ChainElement certElement = chain.ChainElements
+                    .OfType<X509ChainElement>()
+                    .Single(e => e.Certificate.Subject == cert.Subject);
+
+                const X509ChainStatusFlags ExpectedFlag = X509ChainStatusFlags.ExplicitDistrust;
+                X509ChainStatusFlags actualFlags = certElement.AllStatusFlags();
+                Assert.True((actualFlags & ExpectedFlag) == ExpectedFlag, $"Has expected flag {ExpectedFlag} but was {actualFlags}");
+            }
+        }
+
+        [Fact]
+        [PlatformSpecific(~TestPlatforms.Linux)]
+        public static void BuildChainForCertificateSignedWithDisallowedKey()
+        {
+            // The intermediate certificate is from the now defunct CA DigiNotar.
+            // This intermediate is disallowed on the macOS on Windows platforms
+            // which result in an ExplicitDistrust result. OpenSSL does not treat
+            // this intermediate differently, and distributions have removed the
+            // root CA from the trust store anyway, resulting in a partial chain.
+            // Since OpenSSL isn't going out of its way to give the CA or its
+            // intermediates any special distrust, we skip this test on Linux.
+
+            byte[] intermediateBytes = Convert.FromBase64String(@"
+MIIDzTCCAzagAwIBAgIERpwssDANBgkqhkiG9w0BAQUFADCBwzELMAkGA1UEBhMC
+VVMxFDASBgNVBAoTC0VudHJ1c3QubmV0MTswOQYDVQQLEzJ3d3cuZW50cnVzdC5u
+ZXQvQ1BTIGluY29ycC4gYnkgcmVmLiAobGltaXRzIGxpYWIuKTElMCMGA1UECxMc
+KGMpIDE5OTkgRW50cnVzdC5uZXQgTGltaXRlZDE6MDgGA1UEAxMxRW50cnVzdC5u
+ZXQgU2VjdXJlIFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw0wNzA3
+MjYxNTU5MDBaFw0xMzA4MjYxNjI5MDBaMGgxCzAJBgNVBAYTAk5MMRIwE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
+sDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQAD
+gYEAGZH8mFA+TMlGMqXifNKs713LQ8bWv4j7bNNBsySUROa0+uhhKtGhh8089Cnn
+lWOt5PxA7mHNbkGVwPbvPwg32LedZ6nRgpjHE8BJe57z2YmoawxLhxtzLyhOzfe8
+yY1kePIfwE+GFWvagZ2ehANB/6LgBTT8jFhR95Tw2oE3N0I=");
+
+            using (X509Certificate2 intermediateCert = new X509Certificate2(intermediateBytes))
+            using (X509Certificate2 cert = new X509Certificate2(leafBytes))
+            using (ChainHolder chainHolder = new ChainHolder())
+            {
+                X509Chain chain = chainHolder.Chain;
+                chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
+                chain.ChainPolicy.VerificationTime = cert.NotBefore.AddHours(2);
+                chain.ChainPolicy.ExtraStore.Add(intermediateCert);
+                Assert.False(chain.Build(cert));
+
+                X509ChainElement certElement = chain.ChainElements
+                    .OfType<X509ChainElement>()
+                    .Single(e => e.Certificate.Subject == intermediateCert.Subject);
+
+                const X509ChainStatusFlags ExpectedFlag = X509ChainStatusFlags.ExplicitDistrust;
+                X509ChainStatusFlags actualFlags = certElement.AllStatusFlags();
+                Assert.True((actualFlags & ExpectedFlag) == ExpectedFlag, $"Has expected flag {ExpectedFlag} but was {actualFlags}");
+            }
+        }
+
         internal static X509ChainStatusFlags AllStatusFlags(this X509Chain chain)
         {
             return chain.ChainStatus.Aggregate(