Skip to content

Commit b284687

Browse files
authored
fix: optimize the error message for unmatchable certificate bundle (#113)
Fix: - optimize the error message for unmatchable certificate bundle (correct DNs but incorrect public key) - optimize error message for partial chain Test: Previous error message: ```json { "errorCode": "ERROR", "errorMessage": "Error occurred during a cryptographic operation." } ``` Current error message: ```json { "errorCode": "VALIDATION_ERROR", "errorMessage": "Failed to build the X509 chain. Error occurred during a cryptographic operation. The certificate bundle is unreadable. Please ensure the certificate bundle matches the specific certifcate." } ``` Resolves #114 Signed-off-by: Junjie Gao <junjiegao@microsoft.com> --------- Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
1 parent d49ee99 commit b284687

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

Notation.Plugin.AzureKeyVault.Tests/Certificate/CertificateChainTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,16 @@ public void Build_WithIncompleteCertificateBundle_ThrowsValidationException()
4444
// Act and Assert
4545
Assert.Throws<ValidationException>(() => CertificateChain.Build(invalidLeafCert, certificateBundle));
4646
}
47+
48+
[Fact]
49+
public void Build_WithValidLeafAndUnmatchableCertificateBundle_BuildsCertificateChain()
50+
{
51+
// Arrange
52+
X509Certificate2 leafCert = new X509Certificate2(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "leaf.crt"));
53+
X509Certificate2Collection certificateBundle = CertificateBundle.Create(Path.Combine(Directory.GetCurrentDirectory(), "TestData", "unmatchable_root.pem"));
54+
55+
// Act and Assert
56+
Assert.Throws<ValidationException>(() => CertificateChain.Build(leafCert, certificateBundle));
57+
}
4758
}
4859
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDBzCCAe+gAwIBAgIUIx7ad2iEjmGO+dMOMcE8bEJUH1AwDQYJKoZIhvcNAQEL
3+
BQAwEjEQMA4GA1UEAwwHVGVzdCBDQTAgFw0yMzA1MjQwODQxMTJaGA8yMTIzMDQz
4+
MDA4NDExMlowEjEQMA4GA1UEAwwHVGVzdCBDQTCCASIwDQYJKoZIhvcNAQEBBQAD
5+
ggEPADCCAQoCggEBAJY6tHwYDw4MhBD/5lGymyIkCnsaOHFf8OiZwgTTpstvFVH/
6+
go9kr//BR9B/q7uewXVM4z+2/30xmtxN37mv+T45jFXkJwK00P5B8wFt9+oeGroh
7+
0eM8692we76NqTXPC6P2Td9dysyJ2f4ELLC7alinpLc2R6VaX5HLbJxZegrrP/w/
8+
CF0hiUzbdP9TfLqlmPcV29Tp4S3sLHhM3vmezF7XML4ciUybu3HlQKHA99edGxRs
9+
wh6aqxY/NmD+YtRg/9j3T7kmNLfXx0lbsQ5gKvMRSUGf3F5GLw/eGwz4vbTWmHTD
10+
xxWD07NSilvEhMjXzT0IymLSnecCPfWses7CvX0CAwEAAaNTMFEwHQYDVR0OBBYE
11+
FNsu62tfPB+kluRc4WrhN2kBQfdEMB8GA1UdIwQYMBaAFNsu62tfPB+kluRc4Wrh
12+
N2kBQfdEMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAHtVhbvR
13+
Fo26F+KmGRUgJgbz/0MIj5uOvynY6fpT8SynBYpx4z21esWCGAtt7X563prUfGXb
14+
rxQFYZ7FVAByOBxnGXEMfvcr1ON5wpnMsWE9WA2KA0I/uQFNKgBJedbGsvaptbeu
15+
zXIzMHWF/jblN5lmnRSW0QKqbY9D9B3Nfr6RW25X1E6zgabBoVEZBtqAT/pLFVIh
16+
es6kXh3+/i1K+F/olAyna0t+xAQjCgZaBKatEpaj95eiPQB6ODfi57xWeKuA7IPn
17+
a2obfC6s9Y2RhzUNSlre8/e56qzF7j0H8/rilkv1s6C425PfcSlAmybB8CL91XA6
18+
uxfr1nYPtXzHr/s=
19+
-----END CERTIFICATE-----

Notation.Plugin.AzureKeyVault/Certificate/CertificateChain.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Security.Cryptography;
12
using System.Security.Cryptography.X509Certificates;
23
using Notation.Plugin.Protocol;
34

@@ -24,17 +25,24 @@ public static List<byte[]> Build(X509Certificate2 leafCert, X509Certificate2Coll
2425
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
2526
chain.ChainPolicy.CustomTrustStore.AddRange(certificateBundle);
2627

27-
bool isValid = chain.Build(leafCert);
28-
if (!isValid)
28+
try
2929
{
30-
throw new ValidationException("Certificate is invalid");
30+
bool isValid = chain.Build(leafCert);
31+
if (!isValid)
32+
{
33+
throw new ValidationException("Certificate is invalid");
34+
}
35+
}
36+
catch (CryptographicException e)
37+
{
38+
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.");
3139
}
3240

3341
foreach (X509ChainStatus status in chain.ChainStatus)
3442
{
3543
if (status.Status == X509ChainStatusFlags.PartialChain)
3644
{
37-
throw new ValidationException("Failed to build the X509 chain up to the root certificate. 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");
45+
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");
3846
}
3947

4048
if (status.Status != X509ChainStatusFlags.NoError && status.Status != X509ChainStatusFlags.UntrustedRoot)

0 commit comments

Comments
 (0)