Skip to content

Commit ccfaec5

Browse files
authored
Merge pull request #31 from vcsjones/net6
Upgrade to .NET 6 and nullable
2 parents 28c08b0 + c7d42e8 commit ccfaec5

17 files changed

+90
-129
lines changed

AuthenticodeLint/AuthenticodeLint.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<OutputType>WinExe</OutputType>
44
<AssemblyName>authlint</AssemblyName>
55
<PackageId>AuthenticodeLint</PackageId>
6-
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<TargetFramework>net6.0</TargetFramework>
77
<VersionPrefix>0.12.0</VersionPrefix>
88
<Authors>Kevin Jones</Authors>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10-
<Copyright>Kevin Jones 2016-2018</Copyright>
10+
<Copyright>Kevin Jones 2016-2021</Copyright>
1111
<PackAsTool>true</PackAsTool>
1212
<ToolCommandName>authlint</ToolCommandName>
1313
<Description>Authenticode Lint is a Windows command-line tool for linting an examining an Authenticode signed file.</Description>
@@ -17,6 +17,7 @@
1717
<RepositoryUrl>https://github.com/vcsjones/AuthenticodeLint</RepositoryUrl>
1818
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1919
<EmbedUntrackedSources>true</EmbedUntrackedSources>
20+
<Nullable>enable</Nullable>
2021
</PropertyGroup>
2122
<ItemGroup>
2223
<PackageReference Include="AuthenticodeExaminer" Version="0.3.0" />

AuthenticodeLint/BitStrengthCalculator.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace AuthenticodeLint
66
{
77
public static class BitStrengthCalculator
88
{
9-
private static ConcurrentDictionary<string, int> _cachedEccCurveSizes = new ConcurrentDictionary<string, int>();
9+
private static readonly ConcurrentDictionary<string, int> _cachedEccCurveSizes = new ConcurrentDictionary<string, int>();
1010

1111
public static CertificateBitStrength CalculateStrength(X509Certificate2 certificate)
1212
{
@@ -16,19 +16,30 @@ public static CertificateBitStrength CalculateStrength(X509Certificate2 certific
1616
{
1717
case KnownOids.X509Algorithms.Ecc:
1818
keyAlgorithm = PublicKeyAlgorithm.ECDSA;
19-
var parameterOid = OidParser.ReadFromBytes(certificate.PublicKey.EncodedParameters.RawData);
20-
bitSize = _cachedEccCurveSizes.GetOrAdd(parameterOid.Value, oid =>
19+
string? parameterOid = OidParser.ReadFromBytes(certificate.PublicKey.EncodedParameters.RawData);
20+
21+
if (parameterOid is null)
22+
{
23+
bitSize = null;
24+
}
25+
else
2126
{
22-
var curve = ECCurve.CreateFromValue(oid);
23-
using (var ecdsa = ECDsa.Create(curve))
27+
bitSize = _cachedEccCurveSizes.GetOrAdd(parameterOid, static oid =>
2428
{
25-
return ecdsa.KeySize;
26-
}
27-
});
29+
var curve = ECCurve.CreateFromValue(oid);
30+
using (var ecdsa = ECDsa.Create(curve))
31+
{
32+
return ecdsa.KeySize;
33+
}
34+
});
35+
}
2836
break;
2937
case KnownOids.X509Algorithms.RSA:
3038
keyAlgorithm = PublicKeyAlgorithm.RSA;
31-
bitSize = certificate.PublicKey.Key.KeySize;
39+
using (RSA? rsa = certificate.GetRSAPublicKey())
40+
{
41+
bitSize = rsa?.KeySize;
42+
}
3243
break;
3344
default:
3445
keyAlgorithm = PublicKeyAlgorithm.Other;

AuthenticodeLint/CertificatePaddingExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace AuthenticodeLint
77
{
88
public static class CertificatePaddingExtractor
99
{
10-
public static byte[] ExtractPadding(string filePath)
10+
public static byte[]? ExtractPadding(string filePath)
1111
{
1212
using (var file = new PortableExecutable(filePath))
1313
{

AuthenticodeLint/CheckEngine.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public IReadOnlyList<IAuthenticodeRule> GetRules()
1919
{
2020
return (from type in typeof(IAuthenticodeRule).Assembly.GetExportedTypes()
2121
where typeof(IAuthenticodeRule).IsAssignableFrom(type) && type.GetConstructor(Type.EmptyTypes) != null
22-
let instance = (IAuthenticodeRule)Activator.CreateInstance(type)
22+
let instance = (IAuthenticodeRule)Activator.CreateInstance(type)! // We know this should not be null.
2323
orderby instance.RuleId
2424
select instance
2525
).ToList();
@@ -53,17 +53,12 @@ public RuleEngineResult RunAllRules(string file, IReadOnlyList<ICmsSignature> si
5353
}
5454
else
5555
{
56-
switch (rule)
56+
result = rule switch
5757
{
58-
case IAuthenticodeFileRule fileRule:
59-
result = fileRule.Validate(file, verboseWriter, configuration);
60-
break;
61-
case IAuthenticodeSignatureRule sigRule:
62-
result = sigRule.Validate(signatures, verboseWriter, configuration);
63-
break;
64-
default:
65-
throw new NotSupportedException("Rule type is not supported.");
66-
}
58+
IAuthenticodeFileRule fileRule => fileRule.Validate(file, verboseWriter, configuration),
59+
IAuthenticodeSignatureRule sigRule => sigRule.Validate(signatures, verboseWriter, configuration),
60+
_ => throw new NotSupportedException("Rule type is not supported."),
61+
};
6762
}
6863
}
6964
if (result == RuleResult.Fail)

AuthenticodeLint/CommandLineParser.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ namespace AuthenticodeLint
77

88
public readonly struct CommandLineParameter
99
{
10-
private readonly string _name, _value;
10+
private readonly string _name;
11+
private readonly string? _value;
1112

12-
public CommandLineParameter(string name, string value)
13+
public CommandLineParameter(string name, string? value)
1314
{
1415
_name = name;
1516
_value = value;
1617
}
1718

1819
public string Name => _name;
19-
public string Value => _value;
20+
public string? Value => _value;
2021
}
2122

2223

2324
public class CommandLineParser
2425
{
2526
public static IEnumerable<CommandLineParameter> CreateCommandLineParametersWithValues(IEnumerable<string> input)
2627
{
27-
string parameterName = null;
28+
string? parameterName = null;
2829
foreach (var token in input)
2930
{
3031
if (string.IsNullOrWhiteSpace(token) || token.Length == 0)

AuthenticodeLint/ConfigurationValidator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace AuthenticodeLint
88
public class CheckConfiguration
99
{
1010
public IReadOnlyList<string> InputPaths { get; }
11-
public string ReportPath { get; }
11+
public string? ReportPath { get; }
1212
public bool Quiet { get; }
1313
public HashSet<int> SuppressErrorIDs { get; }
1414
public bool Verbose { get; }
1515
public RevocationChecking RevocationMode {get;}
16-
public string ExtractPath { get; }
16+
public string? ExtractPath { get; }
1717
public RuleSet RuleSet { get; }
1818

19-
public CheckConfiguration(IReadOnlyList<string> inputPaths, string reportPath, bool quiet, HashSet<int> suppressErrorIDs, bool verbose, RevocationChecking revocationMode, string extract, RuleSet ruleSet)
19+
public CheckConfiguration(IReadOnlyList<string> inputPaths, string? reportPath, bool quiet, HashSet<int> suppressErrorIDs, bool verbose, RevocationChecking revocationMode, string? extract, RuleSet ruleSet)
2020
{
2121
InputPaths = inputPaths;
2222
ReportPath = reportPath;

AuthenticodeLint/OidParser.cs

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,20 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Security.Cryptography;
1+
using System.Formats.Asn1;
52

63
namespace AuthenticodeLint
74
{
85
public static class OidParser
96
{
10-
private const byte MAGIC_OID_VALUE = 6;
11-
private const int MAGIC_OID_OFFSET = 0;
12-
private const int DATA_LENGTH_OFFSET = 1;
13-
private const int FIRST_OCTET_OFFSET = 2;
14-
private const int VLQ_DATA_OFFSET = 3;
15-
16-
public static Oid ReadFromBytes(byte[] data)
7+
public static string? ReadFromBytes(byte[] data)
178
{
18-
if (data == null || data.Length < FIRST_OCTET_OFFSET)
19-
{
20-
return null;
21-
}
22-
var magicValue = data[MAGIC_OID_OFFSET];
23-
if (magicValue != MAGIC_OID_VALUE)
24-
{
25-
return null;
26-
}
27-
var dataLength = data[DATA_LENGTH_OFFSET];
28-
if (data.Length - FIRST_OCTET_OFFSET != dataLength)
29-
{
30-
return null;
31-
}
32-
var firstValue = data[FIRST_OCTET_OFFSET] / 40L;
33-
var secondValue = data[FIRST_OCTET_OFFSET] % 40L;
34-
var remainder = data.Skip(VLQ_DATA_OFFSET);
359
try
3610
{
37-
return new Oid(string.Join(".", new[] { firstValue, secondValue }.Concat(ReadVlqData(remainder))));
11+
AsnReader reader = new AsnReader(data, AsnEncodingRules.DER);
12+
return reader.ReadObjectIdentifier();
3813
}
39-
catch (InvalidOperationException)
14+
catch (AsnContentException)
4015
{
4116
return null;
4217
}
4318
}
44-
45-
private static IEnumerable<long> ReadVlqData(IEnumerable<byte> data)
46-
{
47-
var value = 0L;
48-
foreach (var item in data)
49-
{
50-
value <<= 7;
51-
if ((item & 0x80) == 0x80)
52-
{
53-
value |= (byte)(item & ~0x80);
54-
}
55-
else
56-
{
57-
yield return value | item;
58-
value = 0;
59-
}
60-
}
61-
if (value != 0)
62-
{
63-
throw new InvalidOperationException();
64-
}
65-
}
6619
}
6720
}

AuthenticodeLint/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static int Main(string[] args)
1717
return ExitCodes.PlatformNotSupported;
1818
}
1919
var cli = Environment.CommandLine;
20-
List<CommandLineParameter> parsedCommandLine;
20+
List<CommandLineParameter>? parsedCommandLine;
2121
try
2222
{
2323
var commandLine = CommandLineParser.LexCommandLine(cli).Skip(1);
@@ -39,8 +39,8 @@ static int Main(string[] args)
3939
var suppress = new HashSet<int>();
4040
bool quiet = false;
4141
bool verbose = false;
42-
string report = null;
43-
string extract = null;
42+
string? report = null;
43+
string? extract = null;
4444
var revocation = RevocationChecking.None;
4545
var ruleSet = RuleSet.Modern;
4646
foreach(var parameter in parsedCommandLine)
@@ -54,7 +54,7 @@ static int Main(string[] args)
5454
}
5555
var filePattern = Path.GetFileName(parameter.Value);
5656
//The value contains a pattern.
57-
if (filePattern.Contains("*") || filePattern.Contains("?"))
57+
if (filePattern.Contains('*') || filePattern.Contains('?'))
5858
{
5959
var directory = Path.GetDirectoryName(parameter.Value);
6060
if (Directory.Exists(directory))

AuthenticodeLint/Rules/10004-PublisherInformationRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public class PublisherInformationPresentRule : IAuthenticodeSignatureRule
1313
public string ShortDescription => "Checks that the signature provided publisher information.";
1414

1515
public RuleSet RuleSet => RuleSet.All;
16-
16+
1717
public RuleResult Validate(IReadOnlyList<ICmsSignature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1818
{
1919
var signatures = graph.VisitAll(SignatureKind.AnySignature, deep: true);
2020
var result = RuleResult.Pass;
2121
foreach (var signature in signatures)
2222
{
23-
PublisherInformation info = null;
23+
PublisherInformation? info = null;
2424
foreach (var attribute in signature.SignedAttributes)
2525
{
2626
if (attribute.Oid.Value == KnownOids.OpusInfo)

AuthenticodeLint/Rules/10005-PublisherInformationUrlHttpsRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public RuleResult Validate(IReadOnlyList<ICmsSignature> graph, SignatureLogger v
2020
var result = RuleResult.Pass;
2121
foreach(var signature in signatures)
2222
{
23-
PublisherInformation info = null;
23+
PublisherInformation? info = null;
2424
foreach(var attribute in signature.SignedAttributes)
2525
{
2626
if (attribute.Oid.Value == KnownOids.OpusInfo)

0 commit comments

Comments
 (0)