Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove X509Chain.Build to avoid compatibility issue #117

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,5 @@ public void Build_WithValidLeafAndCertificateBundle_BuildsCertificateChain()
Assert.NotNull(certificateChain);
Assert.True(certificateChain.Count > 0);
}

[Fact]
public void Build_WithInvalidLeafCertificate_ThrowsValidationException()
{
// Arrange
X509Certificate2 expiredLeafCert = new X509Certificate2(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "expired_leaf.crt"));
X509Certificate2Collection certificateBundle = new X509Certificate2Collection();

// Act and Assert
Assert.Throws<ValidationException>(() => CertificateChain.Build(expiredLeafCert, certificateBundle));
}

[Fact]
public void Build_WithIncompleteCertificateBundle_ThrowsValidationException()
{
// Arrange
X509Certificate2 invalidLeafCert = new X509Certificate2(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "leaf.crt"));
X509Certificate2Collection certificateBundle = new X509Certificate2Collection();

// Act and Assert
Assert.Throws<ValidationException>(() => CertificateChain.Build(invalidLeafCert, certificateBundle));
}

[Fact]
public void Build_WithValidLeafAndUnmatchableCertificateBundle_BuildsCertificateChain()
{
// Arrange
X509Certificate2 leafCert = new X509Certificate2(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "leaf.crt"));
X509Certificate2Collection certificateBundle = CertificateBundle.Create(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "unmatchable_root.pem"));

// Act and Assert
Assert.Throws<ValidationException>(() => CertificateChain.Build(leafCert, certificateBundle));
}
}
}
37 changes: 4 additions & 33 deletions Notation.Plugin.AzureKeyVault/Certificate/CertificateChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,16 @@ static class CertificateChain
/// Build a certificate chain from a leaf certificate and a
/// certificate bundle.
///
/// Note: the method doen't check the validity of the chain.
/// <param name="certificateBundle">The certificate bundle.</param>
/// <param name="leafCert">The leaf certificate.</param>
/// <returns>A list of raw certificates in a chain.</returns>
/// </summary>
public static List<byte[]> Build(X509Certificate2 leafCert, X509Certificate2Collection certificateBundle)
{
X509Chain chain = new X509Chain();
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.CustomTrustStore.AddRange(certificateBundle);

try
{
bool isValid = chain.Build(leafCert);
if (!isValid)
{
throw new ValidationException("Certificate is invalid");
}
}
catch (CryptographicException e)
{
throw new ValidationException($"Failed to build the X509 chain. {e.Message} The certificate bundle is unreadable. Please ensure the certificate bundle matches the specific certifcate.");
}

foreach (X509ChainStatus status in chain.ChainStatus)
{
if (status.Status == X509ChainStatusFlags.PartialChain)
{
throw new ValidationException("Failed to build the X509 chain up to the root certificate. The provided certificate bundle either does not match or does not contain enough certificates to build a complete chain. To resolve this issue, provide the intermediate and root certificates by passing the certificate bundle file's path to the `ca_certs` key in the pluginConfig");
}

if (status.Status != X509ChainStatusFlags.NoError && status.Status != X509ChainStatusFlags.UntrustedRoot)
{
throw new ValidationException($"Failed to build the X509 chain due to {status.StatusInformation}");
}
}

return chain.ChainElements.Select(x => x.Certificate.RawData).ToList();
X509Certificate2Collection chain = new X509Certificate2Collection(leafCert);
chain.AddRange(certificateBundle);
return chain.Select(x => x.RawData).ToList();
}
}
}