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

OpenSslX509ChainProcessor: ignore NotSignatureValid on last element. #69668

Merged
merged 5 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -66,6 +66,86 @@ public static void BuildChain()
}
}

public enum BuildChainWithNotSignatureValidTest : int
{
TrustedRoot,
UntrustedRoot,
PartialChain,
EndCertNotSignatureValid
}

public static IEnumerable<object[]> BuildChainWithNotSignatureValidData()
{
yield return new object[] { true, X509ChainStatusFlags.NoError, BuildChainWithNotSignatureValidTest.TrustedRoot };
yield return new object[] { false, X509ChainStatusFlags.UntrustedRoot | X509ChainStatusFlags.NotSignatureValid, BuildChainWithNotSignatureValidTest.UntrustedRoot };
yield return new object[] { false, X509ChainStatusFlags.PartialChain, BuildChainWithNotSignatureValidTest.PartialChain };
yield return new object[] { false, X509ChainStatusFlags.NotSignatureValid, BuildChainWithNotSignatureValidTest.EndCertNotSignatureValid };
}

[Theory]
[MemberData(nameof(BuildChainWithNotSignatureValidData))]
[PlatformSpecific(TestPlatforms.Linux)] // NotSignatureValid gets ignored on the root certificate.
public static void BuildChainWithNotSignatureValid(
tmds marked this conversation as resolved.
Show resolved Hide resolved
bool chainBuildsSuccessfully,
X509ChainStatusFlags chainFlags,
BuildChainWithNotSignatureValidTest test)
{
TestDataGenerator.MakeTestChain3(
out X509Certificate2 endCert,
out X509Certificate2 intermediateCert,
out X509Certificate2 rootCert,
testName: test.ToString());

X509Certificate2 tampered;
switch (test)
{
case BuildChainWithNotSignatureValidTest.EndCertNotSignatureValid:
tampered = TestDataGenerator.TamperSignature(endCert);
endCert.Dispose();
endCert = tampered;
break;
default:
// Make root cert signature invalid.
tampered = TestDataGenerator.TamperSignature(rootCert);
rootCert.Dispose();
rootCert = tampered;
break;
}

using (endCert)
using (intermediateCert)
using (rootCert)
using (ChainHolder chainHolder = new ChainHolder())
{
X509Chain chainTest = chainHolder.Chain;
chainTest.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chainTest.ChainPolicy.VerificationTime = endCert.NotBefore.AddSeconds(1);
chainTest.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chainTest.ChainPolicy.ExtraStore.Add(endCert);
tmds marked this conversation as resolved.
Show resolved Hide resolved

switch (test)
{
case BuildChainWithNotSignatureValidTest.TrustedRoot:
case BuildChainWithNotSignatureValidTest.EndCertNotSignatureValid:
chainTest.ChainPolicy.ExtraStore.Add(intermediateCert);
chainTest.ChainPolicy.CustomTrustStore.Add(rootCert);
break;
case BuildChainWithNotSignatureValidTest.PartialChain:
chainTest.ChainPolicy.CustomTrustStore.Add(rootCert);
break;
tmds marked this conversation as resolved.
Show resolved Hide resolved
case BuildChainWithNotSignatureValidTest.UntrustedRoot:
chainTest.ChainPolicy.ExtraStore.Add(intermediateCert);
chainTest.ChainPolicy.ExtraStore.Add(rootCert);
break;
default:
throw new InvalidDataException();
}

Assert.Equal(chainBuildsSuccessfully, chainTest.Build(endCert));
Assert.Equal(chainFlags, chainTest.AllStatusFlags());
}
}

[PlatformSpecific(TestPlatforms.Windows)]
[Fact]
public static void VerifyChainFromHandle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ X509Certificate2 TamperIfNeeded(X509Certificate2 input, X509ChainStatusFlags fla
{
if ((flags & X509ChainStatusFlags.NotSignatureValid) != 0)
{
X509Certificate2 tampered = TamperSignature(input);
X509Certificate2 tampered = TestDataGenerator.TamperSignature(input);
input.Dispose();
return tampered;
}
Expand Down Expand Up @@ -948,13 +948,6 @@ private static void TestNameConstrainedChain(
}
}

private static X509Certificate2 TamperSignature(X509Certificate2 input)
{
byte[] cert = input.RawData;
cert[cert.Length - 1] ^= 0xFF;
return new X509Certificate2(cert);
}

private static X509Extension BuildPolicyConstraints(
int? requireExplicitPolicySkipCerts = null,
int? inhibitPolicyMappingSkipCerts = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,12 @@ private static byte[] CreateSerial()
RandomNumberGenerator.Fill(bytes);
return bytes;
}

internal static X509Certificate2 TamperSignature(X509Certificate2 input)
{
byte[] cert = input.RawData;
cert[cert.Length - 1] ^= 0xFF;
return new X509Certificate2(cert);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ private X509ChainElement[] BuildChainElements(
overallStatus = null;

List<X509ChainStatus>? statusBuilder = null;

bool overallHasNotSignatureValid = false;
using (SafeX509StackHandle chainStack = Interop.Crypto.X509StoreCtxGetChain(_storeCtx))
{
int chainSize = Interop.Crypto.GetX509StackFieldCount(chainStack);
Expand All @@ -921,7 +921,20 @@ private X509ChainElement[] BuildChainElements(
statusBuilder ??= new List<X509ChainStatus>();
overallStatus ??= new List<X509ChainStatus>();

AddElementStatus(elementErrors.Value, statusBuilder, overallStatus);
bool hadSignatureNotValid = overallHasNotSignatureValid;
AddElementStatus(elementErrors.Value, statusBuilder, overallStatus, ref overallHasNotSignatureValid);

// Clear NotSignatureValid for the last element when overall chain is not PartialChain or UntrustedRoot.
bool isLastElement = i == chainSize - 1;
if (isLastElement && !hadSignatureNotValid && overallHasNotSignatureValid)
{
if (!ContainsStatus(overallStatus, X509ChainStatusFlags.PartialChain) &&
!ContainsStatus(overallStatus, X509ChainStatusFlags.UntrustedRoot))
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
RemoveStatus(statusBuilder, X509ChainStatusFlags.NotSignatureValid);
RemoveStatus(overallStatus, X509ChainStatusFlags.NotSignatureValid);
}
}
status = statusBuilder.ToArray();
statusBuilder.Clear();
}
Expand Down Expand Up @@ -1013,11 +1026,12 @@ private static void ProcessPolicy(
private static void AddElementStatus(
ErrorCollection errorCodes,
List<X509ChainStatus> elementStatus,
List<X509ChainStatus> overallStatus)
List<X509ChainStatus> overallStatus,
ref bool overallHasNotSignatureValid)
{
foreach (var errorCode in errorCodes)
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
AddElementStatus(errorCode, elementStatus, overallStatus);
AddElementStatus(errorCode, elementStatus, overallStatus, ref overallHasNotSignatureValid);
}

foreach (X509ChainStatus element in elementStatus)
Expand All @@ -1042,7 +1056,8 @@ private static void AddElementStatus(
private static void AddElementStatus(
Interop.Crypto.X509VerifyStatusCode errorCode,
List<X509ChainStatus> elementStatus,
List<X509ChainStatus> overallStatus)
List<X509ChainStatus> overallStatus,
ref bool overallHasNotSignatureValid)
{
X509ChainStatusFlags statusFlag = MapVerifyErrorToChainStatus(errorCode);

Expand All @@ -1069,6 +1084,10 @@ private static void AddElementStatus(

elementStatus.Add(chainStatus);
AddUniqueStatus(overallStatus, ref chainStatus);
if (statusFlag == X509ChainStatusFlags.NotSignatureValid)
tmds marked this conversation as resolved.
Show resolved Hide resolved
{
overallHasNotSignatureValid = true;
}
}

private static void AddUniqueStatus(List<X509ChainStatus> list, ref X509ChainStatus status)
Expand All @@ -1086,6 +1105,31 @@ private static void AddUniqueStatus(List<X509ChainStatus> list, ref X509ChainSta
list.Add(status);
}

private static bool ContainsStatus(List<X509ChainStatus> list, X509ChainStatusFlags statusCode)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Status == statusCode)
{
return true;
}
}

return false;
}

private static void RemoveStatus(List<X509ChainStatus> list, X509ChainStatusFlags statusCode)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Status == statusCode)
{
list.RemoveAt(i);
return;
}
}
}

private static X509ChainStatusFlags MapVerifyErrorToChainStatus(Interop.Crypto.X509VerifyStatusCode code)
{
switch (code.UniversalCode)
Expand Down