Skip to content

Commit 54f0cde

Browse files
build(window): add ksigntool to windows build tools
1 parent 60bcf1d commit 54f0cde

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed
9 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ksigntool", "ksigntool\ksigntool.csproj", "{2CDB78D0-F107-4F46-A5AD-99C909421C00}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2CDB78D0-F107-4F46-A5AD-99C909421C00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2CDB78D0-F107-4F46-A5AD-99C909421C00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2CDB78D0-F107-4F46-A5AD-99C909421C00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2CDB78D0-F107-4F46-A5AD-99C909421C00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3956B2AF-0343-4710-BFB9-07BA3C45C909}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
4+
using System.Windows.Automation;
5+
6+
namespace AutoSafeNetLogon
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
// Find and extract the password argument
13+
string passwordArg = args.FirstOrDefault(a => a.StartsWith("/password:", StringComparison.OrdinalIgnoreCase));
14+
string password = "";
15+
if (passwordArg != null)
16+
{
17+
password = passwordArg.Substring("/password:".Length);
18+
}
19+
20+
if (!string.IsNullOrWhiteSpace(password))
21+
{
22+
// Add an automation event handler to handle SafeNet token password requests
23+
SatisfyEverySafeNetTokenPasswordRequest(password);
24+
}
25+
else
26+
{
27+
Console.WriteLine("Missing /password:<pwd> argument, if the the certificate is on a SafeNet token, you will be prompted for the password.");
28+
}
29+
30+
// Remove password from args before calling signtool
31+
var remainingArgs = args.Where(a => !a.StartsWith("/password:", StringComparison.OrdinalIgnoreCase)).ToArray();
32+
33+
// Run signtool with the remaining args<
34+
var startInfo = new ProcessStartInfo
35+
{
36+
FileName = "signtool.exe",
37+
Arguments = string.Join(" ", remainingArgs),
38+
UseShellExecute = false,
39+
RedirectStandardOutput = true,
40+
RedirectStandardError = true,
41+
};
42+
43+
try
44+
{
45+
Console.WriteLine("Signtool starting with arguments: " + startInfo.Arguments);
46+
var process = Process.Start(startInfo);
47+
process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
48+
process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data);
49+
process.BeginOutputReadLine();
50+
process.BeginErrorReadLine();
51+
process.WaitForExit();
52+
if (process.ExitCode != 0)
53+
{
54+
Console.Error.WriteLine($"Signtool exited with code {process.ExitCode}");
55+
Environment.Exit(process.ExitCode);
56+
}
57+
else
58+
{
59+
Console.WriteLine("Signtool completed successfully.");
60+
}
61+
}
62+
catch (Exception ex)
63+
{
64+
Console.Error.WriteLine($"Failed to start signtool: {ex.Message}");
65+
}
66+
Console.WriteLine("ksigntool finished");
67+
// Clear automation handlers
68+
Automation.RemoveAllEventHandlers();
69+
}
70+
71+
static void SatisfyEverySafeNetTokenPasswordRequest(string password)
72+
{
73+
int count = 0;
74+
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, (sender, e) =>
75+
{
76+
var element = sender as AutomationElement;
77+
if (element.Current.Name == "Token Logon" || element.Current.Name == "Connexion au token")
78+
{
79+
WindowPattern pattern = (WindowPattern)element.GetCurrentPattern(WindowPattern.Pattern);
80+
pattern.WaitForInputIdle(10000);
81+
var edit = element.FindFirst(TreeScope.Descendants, new AndCondition(
82+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
83+
new PropertyCondition(AutomationElement.NameProperty, "Token Password:")));
84+
if (edit == null)
85+
{
86+
edit = element.FindFirst(TreeScope.Descendants, new AndCondition(
87+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
88+
new PropertyCondition(AutomationElement.NameProperty, "Mot de passe du token:")));
89+
}
90+
91+
var ok = element.FindFirst(TreeScope.Descendants, new AndCondition(
92+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
93+
new PropertyCondition(AutomationElement.NameProperty, "OK")));
94+
95+
if (edit != null && ok != null)
96+
{
97+
count++;
98+
ValuePattern vp = (ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern);
99+
vp.SetValue(password);
100+
Console.WriteLine("SafeNet window (count: " + count + " window(s)) detected. Setting password...");
101+
102+
InvokePattern ip = (InvokePattern)ok.GetCurrentPattern(InvokePattern.Pattern);
103+
ip.Invoke();
104+
}
105+
else
106+
{
107+
Console.WriteLine("SafeNet window detected but not with edit and button...");
108+
}
109+
}
110+
});
111+
}
112+
}
113+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ksigntool")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ksigntool")]
13+
[assembly: AssemblyCopyright("Copyright © 2025")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("2cdb78d0-f107-4f46-a5ad-99c909421c00")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{2CDB78D0-F107-4F46-A5AD-99C909421C00}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>ksigntool</RootNamespace>
10+
<AssemblyName>ksigntool</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
<Reference Include="UIAutomationClient" />
45+
<Reference Include="UIAutomationTypes" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="Program.cs" />
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="App.config" />
53+
</ItemGroup>
54+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
55+
<PropertyGroup>
56+
<PostBuildEvent>copy $(TargetPath) $(SolutionDir)\..</PostBuildEvent>
57+
</PropertyGroup>
58+
</Project>

0 commit comments

Comments
 (0)