Skip to content

Commit e03bbf8

Browse files
authored
Fix for TSTInfo encoding issue. Copilot-generated test that updated implementation is still understood by all consumers (#7248)
1 parent fb4c283 commit e03bbf8

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

  • test
    • NuGet.Core.Tests/NuGet.Packaging.Test/SigningTests
    • TestUtilities/Microsoft.Internal.NuGet.Testing.SignedPackages/Asn1

test/NuGet.Core.Tests/NuGet.Packaging.Test/SigningTests/TstInfoTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,46 @@ public void Read_WithExtensions_ReturnsInstance()
9494
Verify(testTstInfo);
9595
}
9696

97+
[Fact]
98+
public void Encode_WithDefaultOrderingFalse_OmitsBooleanAndIsAcceptedByConsumers()
99+
{
100+
// DER X.690 §11.5: a value equal to the DEFAULT must not be encoded.
101+
// 'ordering BOOLEAN DEFAULT FALSE' must be omitted when false.
102+
TestTstInfo testTstInfo = CreateTestTstInfo();
103+
104+
AsnWriter writer = new(AsnEncodingRules.DER);
105+
testTstInfo.Encode(writer);
106+
byte[] bytes = writer.Encode();
107+
108+
// Verify the ordering boolean is not present in the encoded bytes
109+
AsnReader reader = new(bytes, AsnEncodingRules.DER);
110+
AsnReader sequence = reader.ReadSequence();
111+
112+
sequence.ReadInteger(); // version
113+
sequence.ReadObjectIdentifier(); // policy
114+
sequence.ReadSequence(); // messageImprint
115+
sequence.ReadInteger(); // serialNumber
116+
sequence.ReadGeneralizedTime(); // genTime
117+
// accuracy is not present in this test data
118+
119+
bool hasBooleanTag = sequence.HasData && sequence.PeekTag() == Asn1Tag.Boolean;
120+
Assert.False(hasBooleanTag,
121+
"Ordering=false must not be explicitly encoded per DER rules (X.690 §11.5).");
122+
123+
// Verify NuGet's own parser accepts the encoding and reads ordering as false
124+
TstInfo parsed = TstInfo.Read(bytes);
125+
Assert.False(parsed.Ordering);
126+
127+
#if IS_CORECLR
128+
// Verify the BCL parser also accepts the encoding and reads ordering as false
129+
Assert.True(
130+
System.Security.Cryptography.Pkcs.Rfc3161TimestampTokenInfo.TryDecode(
131+
new ReadOnlyMemory<byte>(bytes), out System.Security.Cryptography.Pkcs.Rfc3161TimestampTokenInfo? bclTstInfo, out _),
132+
"BCL Rfc3161TimestampTokenInfo.TryDecode rejected the encoded TSTInfo.");
133+
Assert.False(bclTstInfo!.IsOrdering);
134+
#endif
135+
}
136+
97137
private static void Verify(TestTstInfo expectedTstInfo)
98138
{
99139
AsnWriter writer = new(AsnEncodingRules.DER);

test/TestUtilities/Microsoft.Internal.NuGet.Testing.SignedPackages/Asn1/TstInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public byte[] Encode(AsnWriter writer, bool omitFractionalSeconds = false)
9797
Accuracy.Encode(writer);
9898
}
9999

100-
writer.WriteBoolean(Ordering);
100+
if (Ordering)
101+
{
102+
writer.WriteBoolean(Ordering);
103+
}
101104

102105
if (Nonce is not null)
103106
{

0 commit comments

Comments
 (0)