Skip to content

Commit d02560b

Browse files
committed
Refactor rule interface.
The interface is now broken up so that file-based checks and signature-based checks don't share a common interface.
1 parent 8b1a19f commit d02560b

13 files changed

+49
-38
lines changed

AuthenticodeLint/CheckEngine.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
22
using AuthenticodeLint.Rules;
3+
using System;
4+
using System.Linq;
35

46
namespace AuthenticodeLint
57
{
@@ -14,20 +16,12 @@ static CheckEngine()
1416

1517
public IReadOnlyList<IAuthenticodeRule> GetRules()
1618
{
17-
return new List<IAuthenticodeRule>
18-
{
19-
new Sha1PrimarySignatureRule(),
20-
new Sha2SignatureExistsRule(),
21-
new NoWeakFileDigestAlgorithmsRule(),
22-
new TimestampedRule(),
23-
new PublisherInformationPresentRule(),
24-
new PublisherInformationUrlHttpsRule(),
25-
new SigningCertificateDigestAlgorithmRule(),
26-
new TrustedSignatureRule(),
27-
new WinCertificatePaddingRule(),
28-
new NoUnknownUnsignedAttibuteRule(),
29-
new NoUnknownCertificatesRule()
30-
};
19+
return (from type in typeof(IAuthenticodeRule).Assembly.GetExportedTypes()
20+
where typeof(IAuthenticodeRule).IsAssignableFrom(type) && type.GetConstructor(Type.EmptyTypes) != null
21+
let instance = (IAuthenticodeRule)Activator.CreateInstance(type)
22+
orderby instance.RuleId
23+
select instance
24+
).ToList();
3125
}
3226

3327
public RuleEngineResult RunAllRules(string file, Graph<Signature> signatures, List<IRuleResultCollector> collectors, CheckConfiguration configuration)
@@ -52,9 +46,17 @@ public RuleEngineResult RunAllRules(string file, Graph<Signature> signatures, Li
5246
{
5347
result = RuleResult.Skip;
5448
}
49+
else if (rule is IAuthenticodeFileRule)
50+
{
51+
result = ((IAuthenticodeFileRule)rule).Validate(file, verboseWriter, configuration);
52+
}
53+
else if (rule is IAuthenticodeSignatureRule)
54+
{
55+
result = ((IAuthenticodeSignatureRule)rule).Validate(signatures, verboseWriter, configuration);
56+
}
5557
else
5658
{
57-
result = rule.Validate(signatures, verboseWriter, configuration, file);
59+
throw new NotSupportedException("Rule type is not supported.");
5860
}
5961
}
6062
if (result != RuleResult.Pass)

AuthenticodeLint/Rules/CertificateChainRuleBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace AuthenticodeLint.Rules
44
{
5-
public abstract class CertificateChainRuleBase : IAuthenticodeRule
5+
public abstract class CertificateChainRuleBase : IAuthenticodeSignatureRule
66
{
77
public abstract int RuleId { get; }
88
public abstract string RuleName { get; }
99
public abstract string ShortDescription { get; }
1010

1111
protected abstract bool ValidateChain(Signature signer, X509Chain chain, SignatureLogger verboseWriter);
1212

13-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
13+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1414
{
1515
var signatures = graph.VisitAll();
1616
var result = RuleResult.Pass;

AuthenticodeLint/Rules/IAuthenticodeRule.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ public interface IAuthenticodeRule
55
int RuleId { get; }
66
string ShortDescription { get; }
77
string RuleName { get; }
8-
RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file);
8+
}
9+
10+
public interface IAuthenticodeSignatureRule : IAuthenticodeRule
11+
{
12+
RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration);
13+
}
14+
15+
public interface IAuthenticodeFileRule : IAuthenticodeRule
16+
{
17+
RuleResult Validate(string file, SignatureLogger verboseWriter, CheckConfiguration configuration);
918
}
1019
}

AuthenticodeLint/Rules/NoUnknownCertificatesRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
namespace AuthenticodeLint.Rules
1010
{
11-
public class NoUnknownCertificatesRule : IAuthenticodeRule
11+
public class NoUnknownCertificatesRule : IAuthenticodeSignatureRule
1212
{
1313
public int RuleId { get; } = 10010;
1414

1515
public string RuleName { get; } = "No Unknown Certificates";
1616

1717
public string ShortDescription { get; } = "Checks for unknown embedded certificates.";
1818

19-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
19+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
2020
{
2121
var result = RuleResult.Pass;
2222
var signatures = graph.VisitAll();

AuthenticodeLint/Rules/NoUnknownUnsignedAttibuteRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace AuthenticodeLint.Rules
55
{
6-
public class NoUnknownUnsignedAttibuteRule : IAuthenticodeRule
6+
public class NoUnknownUnsignedAttibuteRule : IAuthenticodeSignatureRule
77
{
88
public int RuleId { get; } = 10009;
99

@@ -18,7 +18,7 @@ public class NoUnknownUnsignedAttibuteRule : IAuthenticodeRule
1818
KnownOids.NestedSignatureOid
1919
};
2020

21-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
21+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
2222
{
2323
var signatures = graph.VisitAll();
2424
var result = RuleResult.Pass;

AuthenticodeLint/Rules/NoWeakFileDigestAlgorithmsRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
namespace AuthenticodeLint.Rules
22
{
3-
public class NoWeakFileDigestAlgorithmsRule : IAuthenticodeRule
3+
public class NoWeakFileDigestAlgorithmsRule : IAuthenticodeSignatureRule
44
{
55
public int RuleId { get; } = 10002;
66

77
public string RuleName { get; } = "No Weak File Digests";
88

99
public string ShortDescription { get; } = "Checks for weak file digest algorithms.";
1010

11-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
11+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1212
{
1313
var signatures = graph.VisitAll();
1414
var result = RuleResult.Pass;

AuthenticodeLint/Rules/PublisherInformationRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
namespace AuthenticodeLint.Rules
55
{
6-
public class PublisherInformationPresentRule : IAuthenticodeRule
6+
public class PublisherInformationPresentRule : IAuthenticodeSignatureRule
77
{
88
public int RuleId { get; } = 10004;
99

1010
public string RuleName { get; } = "Publisher Information Present";
1111

1212
public string ShortDescription { get; } = "Checks that the signature provided publisher information.";
1313

14-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
14+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1515
{
1616
var signatures = graph.VisitAll();
1717
var result = RuleResult.Pass;

AuthenticodeLint/Rules/PublisherInformationUrlHttpsRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
namespace AuthenticodeLint.Rules
55
{
6-
public class PublisherInformationUrlHttpsRule : IAuthenticodeRule
6+
public class PublisherInformationUrlHttpsRule : IAuthenticodeSignatureRule
77
{
88
public int RuleId { get; } = 10005;
99

1010
public string RuleName { get; } = "Publisher Information URL HTTPS Rule";
1111

1212
public string ShortDescription { get; } = "Checks that the signature uses HTTPS for the publisher's URL.";
1313

14-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
14+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1515
{
1616
var signatures = graph.VisitAll();
1717
var result = RuleResult.Pass;

AuthenticodeLint/Rules/Sha1PrimarySignatureRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace AuthenticodeLint.Rules
44
{
5-
public class Sha1PrimarySignatureRule : IAuthenticodeRule
5+
public class Sha1PrimarySignatureRule : IAuthenticodeSignatureRule
66
{
77
public int RuleId { get; } = 10000;
88

99
public string RuleName { get; } = "Primary SHA1";
1010

1111
public string ShortDescription { get; } = "Primary signature should be SHA1.";
1212

13-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
13+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1414
{
1515
var primary = graph.Items.SingleOrDefault()?.Node;
1616
//There are zero signatures.

AuthenticodeLint/Rules/Sha2SignatureExistsRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
namespace AuthenticodeLint.Rules
77
{
8-
public class Sha2SignatureExistsRule : IAuthenticodeRule
8+
public class Sha2SignatureExistsRule : IAuthenticodeSignatureRule
99
{
1010
public int RuleId { get; } = 10001;
1111

1212
public string RuleName { get; } = "SHA2 Signed";
1313

1414
public string ShortDescription { get; } = "A SHA2 signature should exist.";
1515

16-
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration, string file)
16+
public RuleResult Validate(Graph<Signature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1717
{
1818
var signatures = graph.VisitAll();
1919
if (signatures.Any(s =>

0 commit comments

Comments
 (0)