Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Yubikey support #35

Merged
merged 19 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Support.GenerateEWTManifest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Diagnostics.Tracing;
using TameMyCerts;

// Generate the manifest
string? manifest = EventSource.GenerateManifest(typeof(EWTLogger), "TameMyCerts.Events.dll");
// Save the manifest to a file
if (manifest is not null)
{
File.WriteAllText("TameMyCerts.Events.man", manifest);
Console.WriteLine("Manifest generated and saved to TameMyCerts.Events.man");
}
else
{
Console.WriteLine("Failed to generate manifest. The manifest content is null.");
}
44 changes: 44 additions & 0 deletions Support.GenerateEWTManifest/Support.GenerateEWTManifest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TameMyCerts\TameMyCerts.csproj" />
</ItemGroup>

<Target Name="GenerateManifest" AfterTargets="Build">
<Exec Command="$(TargetDir)\$(TargetName).exe" WorkingDirectory="$(TargetDir)" />
</Target>

<Target Name="MessageCompiler" AfterTargets="Build" DependsOnTargets="GenerateManifest">
<Exec Command="&quot;$(NuGetPackageRoot)microsoft.windows.sdk.buildtools\10.0.26100.1742\bin\10.0.26100.0\x64\mc.exe&quot; &quot;TameMyCerts.Events.man&quot; -r ." WorkingDirectory="$(TargetDir)" />
</Target>

<Target Name="ResourceCompiler" AfterTargets="Build" DependsOnTargets="MessageCompiler">
<Exec Command="&quot;$(NuGetPackageRoot)microsoft.windows.sdk.buildtools\10.0.26100.1742\bin\10.0.26100.0\x64\rc.exe&quot; &quot;TameMyCerts.Events.rc&quot;" WorkingDirectory="$(TargetDir)" />
</Target>

<Target Name="BuildDll" AfterTargets="Build" DependsOnTargets="MessageCompiler">
<Exec Command="&quot;$(MSBuildSDKsPath)\..\Current\Bin\Roslyn\csc.exe&quot; /out:TameMyCerts.Events.dll /target:library /win32res:TameMyCerts.Events.res" WorkingDirectory="$(TargetDir)" />
</Target>

<Target Name="CopyOutputFiles" AfterTargets="Build">
<ItemGroup>
<!-- Include DLLs, XMLs, and JSON files from the output path -->
<FilesToCopy Include="$(OutputPath)**\TameMyCerts.Events.dll" />
<FilesToCopy Include="$(OutputPath)**\TameMyCerts.Events.man" />
</ItemGroup>

<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="..\TameMyCerts\bin\$(Configuration)\$(TargetFramework)\" SkipUnchangedFiles="true" />

</Target>
</Project>
22 changes: 22 additions & 0 deletions TameMyCerts.Tests/EWTLoggerListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using TameMyCerts;

namespace TameMyCerts.Tests
{
public class EWTLoggerListener : EventListener
{
private readonly List<EventWrittenEventArgs> events = new List<EventWrittenEventArgs>();
protected override void OnEventWritten(EventWrittenEventArgs eventData) { events.Add(eventData); }
public List<EventWrittenEventArgs> Events => events;
public void ClearEvents() { events.Clear(); }

protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == "TameMyCerts")
{
EnableEvents(eventSource, EventLevel.LogAlways, (EventKeywords)(-1));
}
}
}
}
2 changes: 1 addition & 1 deletion TameMyCerts.Tests/TameMyCerts.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<NoWarn>1701;1702;CA1416</NoWarn>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TameMyCerts\TameMyCerts.csproj" />
<ProjectReference Include="..\TameMyCerts\TameMyCerts.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
Expand Down
99 changes: 99 additions & 0 deletions TameMyCerts.Tests/XMLPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using TameMyCerts.Enums;
using TameMyCerts.Models;
using TameMyCerts.Validators;
using Xunit;
using Xunit.Abstractions;
using static System.Net.Mime.MediaTypeNames;

namespace TameMyCerts.Tests;

public class XMLPolicyTests
{
private EWTLoggerListener _listener;
private readonly ITestOutputHelper output;

public XMLPolicyTests(ITestOutputHelper output)
{
this.output = output;
this._listener = new EWTLoggerListener();
}

internal void PrintResult(CertificateRequestValidationResult result)
{
output.WriteLine("0x{0:X} ({0}) {1}.", result.StatusCode,
new Win32Exception(result.StatusCode).Message);
output.WriteLine(string.Join("\n", result.Description));
}

[Fact]
public void Test_reading_compliant_XML()
{
var filename = Path.GetTempFileName();

string sampleXML = @"<CertificateRequestPolicy xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<AuditOnly>false</AuditOnly>
</CertificateRequestPolicy>";
File.WriteAllText(filename, sampleXML);

CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename);

Assert.False(cacheEntry.CertificateRequestPolicy.AuditOnly);
Assert.Empty(cacheEntry.ErrorMessage);

File.Delete(filename);
}

[Fact]
public void Test_Unknown_XML_Element()
{
var filename = Path.GetTempFileName();

string sampleXML = @"<CertificateRequestPolicy xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<ThisDoewNotExist>false</ThisDoewNotExist>
</CertificateRequestPolicy>
";
File.WriteAllText(filename, sampleXML);
_listener.ClearEvents();

CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename);

Assert.Empty(cacheEntry.ErrorMessage);
Assert.Equal(2, _listener.Events.Count);
Assert.Equal(92, _listener.Events[0].EventId);

File.Delete(filename);
}

[Fact]
public void Test_Unknown_XML_Element2()
{
var filename = Path.GetTempFileName();

string sampleXML = @"<CertificateRequestPolicy xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<DirectoryServicesMapping><AllowedOrganizationalUnits><Test>This should fault</Test></AllowedOrganizationalUnits></DirectoryServicesMapping>
</CertificateRequestPolicy>
";
File.WriteAllText(filename, sampleXML);
_listener.ClearEvents();

CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename);

Assert.Empty(cacheEntry.ErrorMessage);
Assert.Equal(2, _listener.Events.Count);
Assert.Equal(92, _listener.Events[0].EventId);

File.Delete(filename);
}

}
Loading
Loading