-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ricaun-io/develop
Version 1.0.1
- Loading branch information
Showing
6 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace ricaun.Security.WinTrust.Tests | ||
{ | ||
public class Tests_Certificate | ||
{ | ||
string Directory => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
|
||
[TestCase("ConsoleAppSigned.exe", "Sectigo Public Code Signing CA R36")] | ||
[TestCase("ConsoleAppSignedNotTrusted.exe", "signfile")] | ||
public void GetSignedFileIssuerCode_ShouldBe(string fileName, string communName) | ||
{ | ||
var filePath = Path.Combine(Directory, fileName); | ||
var result = Certificate.GetSignedFileIssuer(filePath, "cn"); | ||
Assert.AreEqual(communName, result); | ||
} | ||
|
||
[TestCase("ConsoleAppSigned.exe", "ricaun")] | ||
[TestCase("ConsoleAppSignedNotTrusted.exe", "signfile")] | ||
public void GetSignedFileSubjectCode_ShouldBe(string fileName, string communName) | ||
{ | ||
var filePath = Path.Combine(Directory, fileName); | ||
var result = Certificate.GetSignedFileSubject(filePath, "cn"); | ||
Assert.AreEqual(communName, result); | ||
} | ||
|
||
[TestCase("ConsoleApp.exe", false)] | ||
[TestCase("ConsoleAppSigned.exe", true)] | ||
[TestCase("ConsoleAppSignedNotTrusted.exe", true)] | ||
public void GetSignedFileIssuer_ShouldBe(string fileName, bool isNotEmpty) | ||
{ | ||
var filePath = Path.Combine(Directory, fileName); | ||
var result = Certificate.GetSignedFileIssuer(filePath); | ||
Assert.AreEqual(isNotEmpty, !string.IsNullOrEmpty(result)); | ||
} | ||
|
||
[TestCase("ConsoleApp.exe", false)] | ||
[TestCase("ConsoleAppSigned.exe", true)] | ||
[TestCase("ConsoleAppSignedNotTrusted.exe", true)] | ||
public void GetSignedFileSubject_ShouldBe(string fileName, bool isNotEmpty) | ||
{ | ||
var filePath = Path.Combine(Directory, fileName); | ||
var result = Certificate.GetSignedFileSubject(filePath); | ||
Assert.AreEqual(isNotEmpty, !string.IsNullOrEmpty(result)); | ||
} | ||
|
||
[TestCase("C:\\Windows\\explorer.exe", "cn", "Microsoft Windows")] | ||
[TestCase("C:\\Windows\\explorer.exe", "o", "Microsoft Corporation")] | ||
public void Windows_GetSignedFileSubjectCode_ShouldBe(string filePath, string communName, string expected) | ||
{ | ||
if (File.Exists(filePath) == false) | ||
Assert.Ignore("File not found"); | ||
|
||
var result = Certificate.GetSignedFileSubject(filePath, communName); | ||
Assert.AreEqual(expected, result); | ||
} | ||
|
||
[Test] | ||
public void Internal_SplitAndGetStartWith() | ||
{ | ||
var baseString = "CN=signfile, OU=signfile1, O=signfile2, L=signfile3, S=signfile4, C=signfile5"; | ||
var expectedValues = new Dictionary<string, string>() { | ||
{"cn","signfile"}, | ||
{"ou","signfile1"}, | ||
{"o","signfile2"}, | ||
{"l","signfile3"}, | ||
{"s","signfile4"}, | ||
{"c","signfile5"}, | ||
}; | ||
|
||
foreach (var expectedValue in expectedValues) | ||
{ | ||
var code = expectedValue.Key; | ||
var expected = expectedValue.Value; | ||
|
||
Assert.AreEqual(expected, Certificate.SplitAndGetStartWith(baseString, code)); | ||
Assert.AreEqual(expected, Certificate.SplitAndGetStartWith(baseString, code + '='), "= should be Ignore"); | ||
Assert.AreEqual(expected, Certificate.SplitAndGetStartWith(baseString, code.ToUpper()), "UpperCase be Ignore"); | ||
} | ||
|
||
Assert.IsEmpty(Certificate.SplitAndGetStartWith(baseString, "a")); | ||
Assert.IsEmpty(Certificate.SplitAndGetStartWith(baseString, "b")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,106 @@ | ||
namespace ricaun.Security.WinTrust | ||
{ | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
/// <summary> | ||
/// Certificate | ||
/// </summary> | ||
public sealed class Certificate | ||
{ | ||
/// <summary> | ||
/// Try to <see cref="System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile"/> to check if the file is signed. | ||
/// Try to <see cref="X509Certificate.CreateFromSignedFile"/> to check if the file is signed. | ||
/// </summary> | ||
/// <param name="fileName"></param> | ||
/// <returns></returns> | ||
public static bool IsSignedFile(string fileName) | ||
{ | ||
try | ||
{ | ||
System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile(fileName); | ||
X509Certificate.CreateFromSignedFile(fileName); | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get SignedFileIssuer | ||
/// </summary> | ||
/// <param name="fileName"></param> | ||
/// <returns></returns> | ||
/// <remarks>Return <see cref="string.Empty"/> if the <paramref name="fileName"/> is not signed.</remarks> | ||
public static string GetSignedFileIssuer(string fileName) | ||
{ | ||
if (IsSignedFile(fileName)) | ||
{ | ||
return X509Certificate.CreateFromSignedFile(fileName).Issuer; | ||
} | ||
return string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Get SignedFileSubject | ||
/// </summary> | ||
/// <param name="fileName"></param> | ||
/// <returns></returns> | ||
/// <remarks>Return <see cref="string.Empty"/> if the <paramref name="fileName"/> is not signed.</remarks> | ||
public static string GetSignedFileSubject(string fileName) | ||
{ | ||
if (IsSignedFile(fileName)) | ||
{ | ||
return X509Certificate.CreateFromSignedFile(fileName).Subject; | ||
} | ||
return string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Get SignedFileIssuer with the <paramref name="code"/> start with. | ||
/// </summary> | ||
/// <param name="fileName"></param> | ||
/// <param name="code"></param> | ||
/// <remarks>Return <see cref="string.Empty"/> if the <paramref name="fileName"/> is not signed.</remarks> | ||
public static string GetSignedFileIssuer(string fileName, string code) | ||
{ | ||
var subject = GetSignedFileIssuer(fileName); | ||
|
||
if (string.IsNullOrEmpty(subject)) return string.Empty; | ||
|
||
return SplitAndGetStartWith(subject, code); | ||
} | ||
|
||
/// <summary> | ||
/// Get SignedFileSubject with the <paramref name="code"/> start with. | ||
/// </summary> | ||
/// <param name="fileName"></param> | ||
/// <param name="code"></param> | ||
/// <remarks>Return <see cref="string.Empty"/> if the <paramref name="fileName"/> is not signed.</remarks> | ||
public static string GetSignedFileSubject(string fileName, string code) | ||
{ | ||
var subject = GetSignedFileSubject(fileName); | ||
|
||
if (string.IsNullOrEmpty(subject)) return string.Empty; | ||
|
||
return SplitAndGetStartWith(subject, code); | ||
} | ||
|
||
internal static string SplitAndGetStartWith(string subject, string codeStartWith) | ||
{ | ||
const char ConstEqual = '='; | ||
const char ConstSeparator = ','; | ||
|
||
var texts = subject.Split(ConstSeparator) | ||
.Select(s => s.Trim()); | ||
|
||
codeStartWith = codeStartWith.Trim().TrimEnd(ConstEqual) + ConstEqual; | ||
|
||
if (texts.FirstOrDefault(t => t.StartsWith(codeStartWith, System.StringComparison.InvariantCultureIgnoreCase)) is string text) | ||
{ | ||
return text.Remove(0, codeStartWith.Length); | ||
} | ||
return string.Empty; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters