Skip to content

Commit 34afae2

Browse files
committed
Fixes from refactoring and more cleanup.
1 parent 2a27b50 commit 34afae2

17 files changed

+67
-159
lines changed
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<OutputType>Exe</OutputType>
3+
<OutputType>WinExe</OutputType>
44
<AssemblyName>authlint</AssemblyName>
5-
<TargetFrameworks>net461</TargetFrameworks>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
66
<VersionPrefix>0.11.0</VersionPrefix>
77
<Authors>Kevin Jones</Authors>
8-
<LangVersion>latest</LangVersion>
98
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
109
<Copyright>Kevin Jones 2016-2017</Copyright>
1110
</PropertyGroup>
1211
<ItemGroup>
1312
<PackageReference Include="AuthenticodeExaminer" Version="0.3.0" />
1413
</ItemGroup>
15-
<ItemGroup>
16-
<Reference Include="System.Security" />
17-
</ItemGroup>
1814
</Project>

AuthenticodeLint/BitStrengthCalculator.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
1-
using System.Security.Cryptography.X509Certificates;
1+
using System.Collections.Concurrent;
2+
using System.Security.Cryptography;
3+
using System.Security.Cryptography.X509Certificates;
24

35
namespace AuthenticodeLint
46
{
57
public static class BitStrengthCalculator
68
{
9+
private static ConcurrentDictionary<string, int> _cachedEccCurveSizes = new ConcurrentDictionary<string, int>();
10+
711
public static CertificateBitStrength CalculateStrength(X509Certificate2 certificate)
812
{
913
PublicKeyAlgorithm keyAlgorithm;
1014
int? bitSize;
1115
switch (certificate.PublicKey.Oid.Value)
1216
{
1317
case KnownOids.X509Algorithms.Ecc:
18+
keyAlgorithm = PublicKeyAlgorithm.ECDSA;
1419
var parameterOid = OidParser.ReadFromBytes(certificate.PublicKey.EncodedParameters.RawData);
15-
switch (parameterOid.Value)
20+
bitSize = _cachedEccCurveSizes.GetOrAdd(parameterOid.Value, oid =>
1621
{
17-
case KnownOids.EccCurves.EcdsaP256:
18-
keyAlgorithm = PublicKeyAlgorithm.ECDSA;
19-
bitSize = 256;
20-
break;
21-
case KnownOids.EccCurves.EcdsaP384:
22-
keyAlgorithm = PublicKeyAlgorithm.ECDSA;
23-
bitSize = 384;
24-
break;
25-
case KnownOids.EccCurves.EcdsaP521:
26-
keyAlgorithm = PublicKeyAlgorithm.ECDSA;
27-
bitSize = 521;
28-
break;
29-
default:
30-
keyAlgorithm = PublicKeyAlgorithm.Other;
31-
bitSize = null;
32-
break;
33-
}
22+
var curve = ECCurve.CreateFromValue(oid);
23+
using (var ecdsa = ECDsa.Create(curve))
24+
{
25+
return ecdsa.KeySize;
26+
}
27+
});
3428
break;
3529
case KnownOids.X509Algorithms.RSA:
3630
keyAlgorithm = PublicKeyAlgorithm.RSA;

AuthenticodeLint/CertificatePaddingExtractor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public static byte[] ExtractPadding(string filePath)
3232
using (var memoryStream = new MemoryStream())
3333
{
3434
int read;
35-
var buffer = new byte[0x1000];
36-
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
35+
Span<byte> buffer = stackalloc byte[0x400];
36+
while ((read = reader.Read(buffer)) > 0)
3737
{
38-
memoryStream.Write(buffer, 0, read);
38+
memoryStream.Write(buffer.Slice(0, read));
3939
}
4040
var winCertificate = memoryStream.ToArray();
4141
var signer = new SignedCms();

AuthenticodeLint/CommandLineParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace AuthenticodeLint
77

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

1212
public CommandLineParameter(string name, string value)
1313
{

AuthenticodeLint/Extraction.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using AuthenticodeExaminer;
2-
using AuthenticodeLint.Interop;
2+
using System;
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Security.Cryptography.X509Certificates;
@@ -41,19 +41,20 @@ public static void ExtractToDisk(string file, CheckConfiguration configuration,
4141

4242
private static string SerializeCertificate(X509Certificate2 certificate)
4343
{
44-
string base64Certificate = null;
45-
var binaryCertificate = certificate.Export(X509ContentType.Cert);
46-
47-
uint size = 0;
48-
if (Crypt32.CryptBinaryToString(binaryCertificate, (uint)binaryCertificate.Length, CryptBinaryToStringFlags.CRYPT_STRING_BASE64HEADER, null, ref size))
44+
var octets = certificate.Export(X509ContentType.Cert);
45+
var formatted = Convert.ToBase64String(octets);
46+
var builder = new StringBuilder();
47+
builder.AppendLine("-----BEGIN CERTIFICATE-----");
48+
var i = 0;
49+
while (i < formatted.Length)
4950
{
50-
var builder = new StringBuilder((int)size);
51-
if (Crypt32.CryptBinaryToString(binaryCertificate, (uint)binaryCertificate.Length, CryptBinaryToStringFlags.CRYPT_STRING_BASE64HEADER, builder, ref size))
52-
{
53-
base64Certificate = builder.ToString();
54-
}
51+
const int MAX_LINE_SIZE = 64;
52+
var size = Math.Min(MAX_LINE_SIZE, formatted.Length - i);
53+
builder.AppendLine(formatted.Substring(i, size));
54+
i += size;
5555
}
56-
return base64Certificate;
56+
builder.AppendLine("-----END CERTIFICATE-----");
57+
return builder.ToString();
5758
}
5859
}
5960
}

AuthenticodeLint/Interop/Crypt32.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

AuthenticodeLint/PE/PortableExecutable.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using AuthenticodeLint.Interop;
22
using System;
33
using System.Collections.Generic;
4-
using System.Diagnostics;
54
using System.IO;
65
using System.IO.MemoryMappedFiles;
7-
using System.Linq;
86
using System.Runtime.InteropServices;
97

108
namespace AuthenticodeLint.PE
@@ -15,15 +13,14 @@ public class PortableExecutable : IDisposable
1513

1614
public PortableExecutable(string filePath)
1715
{
18-
_file = MemoryMappedFile.CreateFromFile(filePath, System.IO.FileMode.Open, "PortableExecutableView", 0, MemoryMappedFileAccess.Read);
16+
_file = MemoryMappedFile.CreateFromFile(filePath, FileMode.Open, "PortableExecutableView", 0, MemoryMappedFileAccess.Read);
1917
}
2018

2119
public DosHeader GetDosHeader()
2220
{
2321
using (var view = _file.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read))
2422
{
25-
IMAGE_DOS_HEADER header;
26-
view.Read(0L, out header);
23+
view.Read(0L, out IMAGE_DOS_HEADER header);
2724
if (header.e_magic != DOS_MAGIC)
2825
{
2926
throw new InvalidOperationException("File does not have a valid DOS header.");
@@ -45,12 +42,10 @@ public PeHeader GetPEHeader(DosHeader dosHeader)
4542
{
4643
throw new InvalidOperationException("File does not have a valid PE header.");
4744
}
48-
IMAGE_FILE_HEADER fileHeader;
49-
view.Read(sizeof(uint), out fileHeader);
45+
view.Read(sizeof(uint), out IMAGE_FILE_HEADER fileHeader);
5046
if (fileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
5147
{
52-
IMAGE_OPTIONAL_HEADER64 header64;
53-
view.Read(sizeof(uint) + Marshal.SizeOf<IMAGE_FILE_HEADER>(), out header64);
48+
view.Read(sizeof(uint) + Marshal.SizeOf<IMAGE_FILE_HEADER>(), out IMAGE_OPTIONAL_HEADER64 header64);
5449
if (header64.Magic != PE32_64)
5550
{
5651
throw new InvalidOperationException("File is x86-64 but has a image type other than PE32+.");
@@ -61,8 +56,7 @@ public PeHeader GetPEHeader(DosHeader dosHeader)
6156
}
6257
else if (fileHeader.Machine == IMAGE_FILE_MACHINE_I386)
6358
{
64-
IMAGE_OPTIONAL_HEADER32 header32;
65-
view.Read(sizeof(uint) + Marshal.SizeOf<IMAGE_FILE_HEADER>(), out header32);
59+
view.Read(sizeof(uint) + Marshal.SizeOf<IMAGE_FILE_HEADER>(), out IMAGE_OPTIONAL_HEADER32 header32);
6660
if (header32.Magic != PE32_32)
6761
{
6862
throw new InvalidOperationException("File is x86 but has a image type other than PE32.");

AuthenticodeLint/Rules/10003-TimestampedRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class TimestampedRule : IAuthenticodeSignatureRule
1515

1616
public RuleSet RuleSet { get; } = RuleSet.All;
1717

18-
public unsafe RuleResult Validate(IReadOnlyList<ICmsSignature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
18+
public RuleResult Validate(IReadOnlyList<ICmsSignature> graph, SignatureLogger verboseWriter, CheckConfiguration configuration)
1919
{
2020
var signatures = graph.VisitAll(SignatureKind.AnySignature);
2121
var pass = true;

AuthenticodeLint/Rules/10004-PublisherInformationRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public RuleResult Validate(IReadOnlyList<ICmsSignature> graph, SignatureLogger v
2929
break;
3030
}
3131
}
32-
if (info == null)
32+
if (info == null || info.IsEmpty)
3333
{
3434
result = RuleResult.Fail;
3535
verboseWriter.LogSignatureMessage(signature, "Signature does not have any publisher information.");

AuthenticodeLint/Rules/10005-PublisherInformationUrlHttpsRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public RuleResult Validate(IReadOnlyList<ICmsSignature> graph, SignatureLogger v
2929
break;
3030
}
3131
}
32-
if (info == null)
32+
if (info == null || info.IsEmpty)
3333
{
3434
result = RuleResult.Fail;
3535
verboseWriter.LogSignatureMessage(signature, "Signature does not have any publisher information.");

0 commit comments

Comments
 (0)