如何在发布单个文件 dotnet core 应用程序期间签署二进制文件?

ole*_*sii 5 msbuild publishing code-signing authenticode .net-core

我想对单文件发布的 .net core 应用程序中的二进制文件进行签名。这是因为我希望这些库在解压到 时进行数字签名。这是我的项目文件的缩短版本。%temp%\.net\%app_name%\%random_dir%

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
    <PublishTrimmed>false</PublishTrimmed>
    <PublishReadyToRun>false</PublishReadyToRun>
    <Configuration>Release</Configuration>
    <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>    
  </PropertyGroup>

  <Target Name="SignPrePublishedFiles" AfterTargets="ComputeAndCopyFilesToPublishDirectory">
    <ItemGroup>            
      <FileToSign Include="$(OutDir)Foo.*.dll" />      
    </ItemGroup>    
    <Exec Command="jsign ~~~params removed for brevity~~~ %(FileToSign.Identity)" />
  </Target>
  
</Project>
Run Code Online (Sandbox Code Playgroud)

目标在目录SignPrePublishedFiles中签署所需的文件$(OutDir),但发布的应用程序包含未签名的二进制文件。我认为这是因为以下时间不正确:AfterTargets="ComputeAndCopyFilesToPublishDirectory"或文件夹不正确,我假设$(OutDir)用于发布。在这里,我使用jsign因为构建在 Linux 上运行,但签名是针对 Windows 二进制文件完成的。

如何签署已发布的文件,特别是单文件应用程序内的文件?

ban*_*ana 1

您可以在项目位于 obj/ 中时对输出 dll 和 apphost 包装器进行签名 - 这就是最终发布的内容。尝试这样的事情:

  <Target Name="SignIntermediates" BeforeTargets="GetCopyToOutputDirectoryItems">
    <ItemGroup>
      <FileToSign Include="$(IntermediateOutputPath)$(TargetFileName)" />
      <FileToSign Condition="'$(TargetExt)' == '.dll' And '$(OutputType)' == 'WinExe'" Include="$(IntermediateOutputPath)apphost.exe" />
    </ItemGroup>

    <Exec Command="jsign ~~~params removed for brevity~~~ %(FileToSign.Identity)" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

apphost.exeGetCopyToOutputDirectoryItems 是复制并重命名为之前的最后一个公共目标$(TargetName).exe