Skip to content

Commit b3ccc02

Browse files
committed
Unit test for SHA1 primary.
1 parent bb86371 commit b3ccc02

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

AuthenticodeLint/KnownOids.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace AuthenticodeLint
44
{
5-
internal static class KnownOids
5+
public static class KnownOids
66
{
77
public static class X509Algorithms
88
{

AuthenticodeLintTests/AuthenticodeLintTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
</ItemGroup>
3838
<ItemGroup>
3939
<Compile Include="CommandLineParsingTests.cs" />
40+
<Compile Include="FakeSignature.cs" />
4041
<Compile Include="Properties\AssemblyInfo.cs" />
4142
<Compile Include="Rules\PublisherInformationPresentRuleTests.cs" />
43+
<Compile Include="Rules\Sha1PrimarySignatureRuleTests.cs" />
4244
<Compile Include="Rules\TimestampedRuleTests.cs" />
4345
<Compile Include="Rules\WinCertificatePaddingRuleTests.cs" />
4446
</ItemGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using AuthenticodeLint;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Security.Cryptography;
8+
using System.Security.Cryptography.X509Certificates;
9+
10+
namespace AuthenticodeLintTests
11+
{
12+
public class FakeSignature : ISignature
13+
{
14+
private List<ISignature> _nestedSignatures = new List<ISignature>();
15+
16+
public X509Certificate2Collection AdditionalCertificates { get; set; }
17+
public X509Certificate2 Certificate { get; set; }
18+
public Oid DigestAlgorithm { get; set; }
19+
public Oid HashEncryptionAlgorithm { get; set; }
20+
public SignatureKind Kind { get; set; }
21+
public CryptographicAttributeObjectCollection SignedAttributes { get; set; }
22+
public CryptographicAttributeObjectCollection UnsignedAttributes { get; set; }
23+
public IReadOnlyList<ISignature> GetNestedSignatures() => _nestedSignatures;
24+
25+
public void Add(ISignature signature) => _nestedSignatures.Add(signature);
26+
27+
public FakeSignature()
28+
{
29+
SignedAttributes = new CryptographicAttributeObjectCollection();
30+
UnsignedAttributes = new CryptographicAttributeObjectCollection();
31+
SignedAttributes.Add(new AsnEncodedData(new Oid(KnownOids.MessageDigest), new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }));
32+
}
33+
}
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using AuthenticodeLint;
2+
using AuthenticodeLint.Rules;
3+
using System.Collections.Generic;
4+
using System.Security.Cryptography;
5+
using Xunit;
6+
7+
namespace AuthenticodeLintTests.Rules
8+
{
9+
public class Sha1PrimarySignatureRuleTests
10+
{
11+
private static CheckConfiguration Configuration => new CheckConfiguration(new List<string>(), null, false, new HashSet<int>(), false, RevocationChecking.None, null);
12+
13+
[
14+
Theory,
15+
InlineData(KnownOids.MD2),
16+
InlineData(KnownOids.MD5),
17+
InlineData(KnownOids.SHA256),
18+
InlineData(KnownOids.SHA384),
19+
InlineData(KnownOids.SHA512),
20+
]
21+
public void ShouldFailOnNonSha1Algorithms(string oid)
22+
{
23+
var algorithm = new Oid(oid);
24+
var signature = new FakeSignature
25+
{
26+
DigestAlgorithm = algorithm
27+
};
28+
var check = new Sha1PrimarySignatureRule();
29+
var logger = new MemorySignatureLogger();
30+
var result = check.Validate(new List<ISignature> { signature }, logger, Configuration);
31+
Assert.Equal(RuleResult.Fail, result);
32+
Assert.Contains($"Signature 000102030405060708090a: Expected {nameof(KnownOids.SHA1)} digest algorithm but is {algorithm.FriendlyName}.", logger.Messages);
33+
}
34+
35+
[Fact]
36+
public void ShouldPassOnSha1Algorithm()
37+
{
38+
var algorithm = new Oid(KnownOids.SHA1);
39+
var signature = new FakeSignature
40+
{
41+
DigestAlgorithm = algorithm
42+
};
43+
var check = new Sha1PrimarySignatureRule();
44+
var logger = new MemorySignatureLogger();
45+
var result = check.Validate(new List<ISignature> { signature }, logger, Configuration);
46+
Assert.Equal(RuleResult.Pass, result);
47+
Assert.Empty(logger.Messages);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)