Skip to content

Commit 4e4deb6

Browse files
committed
Added tests for timestamping rule.
1 parent 35fdba2 commit 4e4deb6

File tree

9 files changed

+62
-7
lines changed

9 files changed

+62
-7
lines changed

AuthenticodeLint/Rules/TimestampedRule.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Security.Cryptography.Pkcs;
32

43
namespace AuthenticodeLint.Rules
54
{
@@ -9,7 +8,7 @@ public class TimestampedRule : IAuthenticodeSignatureRule
98

109
public string RuleName { get; } = "Timestamped Rule";
1110

12-
public string ShortDescription { get; } = "Signatures should have a time stamped counter signer.";
11+
public string ShortDescription { get; } = "Signatures should have a timestamp counter signer.";
1312

1413
public unsafe RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1514
{
@@ -48,7 +47,7 @@ public unsafe RuleResult Validate(Graph<Signature> graph, SignatureLogger verbos
4847
}
4948
if (!isSigned)
5049
{
51-
verboseWriter.LogSignatureMessage(signatureInfo, $"Signature is not timestamped.");
50+
verboseWriter.LogSignatureMessage(signatureInfo, "Signature is not timestamped.");
5251
pass = false;
5352
}
5453
else if (!strongSign)

AuthenticodeLint/SignatureExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private unsafe Graph<Signature> GetSignatures(CryptMsgSafeHandle messageHandle)
5858
}
5959

6060

61-
public static Graph<Signature> RecursiveSigner(IList<byte[]> cmsData)
61+
private static Graph<Signature> RecursiveSigner(IList<byte[]> cmsData)
6262
{
6363
var graphItems = new List<GraphItem<Signature>>();
6464
foreach (var data in cmsData)

AuthenticodeLintTests/AuthenticodeLintTests.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838
<ItemGroup>
3939
<Compile Include="CommandLineParsingTests.cs" />
4040
<Compile Include="Properties\AssemblyInfo.cs" />
41+
<Compile Include="Rules\TimestampedRuleTests.cs" />
4142
<Compile Include="Rules\WinCertificatePaddingRuleTests.cs" />
4243
</ItemGroup>
4344
<ItemGroup>
44-
<None Include="inputs\wintrustnonpadded.ex_" />
45-
<None Include="inputs\wintrustpadded.ex_" />
4645
<None Include="project.json" />
4746
</ItemGroup>
4847
<ItemGroup>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using AuthenticodeLint;
2+
using AuthenticodeLint.Rules;
3+
using System.Collections.Generic;
4+
using Xunit;
5+
6+
namespace AuthenticodeLintTests.Rules
7+
{
8+
public class TimestampedRuleTests
9+
{
10+
private static CheckConfiguration Configuration => new CheckConfiguration(new List<string>(), null, false, new HashSet<int>(), false, RevocationChecking.None);
11+
12+
private static Graph<Signature> GetGraphForFile(string file)
13+
{
14+
var extractor = new SignatureExtractor();
15+
return extractor.Extract(file);
16+
}
17+
18+
[
19+
Theory,
20+
InlineData("../../inputs/notimestamp.ex_"),
21+
InlineData("../../inputs/notimestamp.dl_")
22+
]
23+
public void ShouldFailIfNoTimestamp(string file)
24+
{
25+
var signatures = GetGraphForFile(file);
26+
var rule = new TimestampedRule();
27+
28+
var logger = new MemorySignatureLogger();
29+
var result = rule.Validate(signatures, logger, Configuration);
30+
Assert.Equal(RuleResult.Fail, result);
31+
Assert.Collection(logger.Messages, s => s.EndsWith("Signature is not timestamped."));
32+
}
33+
34+
[Fact]
35+
public void ShouldFailIfTimestampUsesWeakSignatureAlgorithm()
36+
{
37+
var signatures = GetGraphForFile("../../inputs/timestampedweaksig.ex_");
38+
var rule = new TimestampedRule();
39+
40+
var logger = new MemorySignatureLogger();
41+
var result = rule.Validate(signatures, logger, Configuration);
42+
Assert.Equal(RuleResult.Fail, result);
43+
Assert.Collection(logger.Messages, s => s.EndsWith("Signature is not timestamped with the expected hash algorithm SHA256."));
44+
}
45+
46+
[Fact]
47+
public void ShouldPassIfTimestampedAlgorithmIsValid()
48+
{
49+
var signatures = GetGraphForFile("../../inputs/timestampedvalid.ex_");
50+
var rule = new TimestampedRule();
51+
52+
var logger = new MemorySignatureLogger();
53+
var result = rule.Validate(signatures, logger, Configuration);
54+
Assert.Equal(RuleResult.Pass, result);
55+
Assert.Empty(logger.Messages);
56+
}
57+
}
58+
}

AuthenticodeLintTests/Rules/WinCertificatePaddingRuleTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public void NonBinaryShouldThrow()
6565
//Rules shouldn't handle non-signed, non-binary content since that validation happens further up.
6666
var file = "../../inputs/nonbinary.txt";
6767
var rule = new WinCertificatePaddingRule();
68-
var logger = new MemorySignatureLogger();
6968

7069
Assert.Throws<InvalidOperationException>(() => rule.Validate(file, SignatureLogger.Null, Configuration));
7170
}
35.7 KB
Binary file not shown.
36.7 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)