Delphi、C++ Builder 和 .NET 库和签名服务器

从 Delphi、.NET 和 CI/CD 使用 Azure Trusted Signing

从 Delphi 或 .NET 使用 Azure Trusted Signing 为 Windows 应用程序签名。

对 EXE 和 DLL 文件进行 Authenticode 签名
私钥保留在 Azure 中
无需本地 PFX,无需 USB 令牌
构建代理无需 Azure 密钥即可签名
TsgcAzureTrustedSigningProviderOAuth2 客户端凭据Microsoft 颁发的公共信任证书远程签名服务器

从 CI 管道签名 → · 查看价格 →

SignWithAzure.pas
// Authenticode-sign with Azure Trusted Signing
var
  vKeyProvider: TsgcAzureTrustedSigningProvider;
  vSigner: TsgcAuthenticodeSigner;
begin
  vKeyProvider := TsgcAzureTrustedSigningProvider.Create(nil);
  vSigner := TsgcAuthenticodeSigner.Create(nil);
  try
    vKeyProvider.TenantId := '00000000-0000-0000-0000-000000000000';
    vKeyProvider.ClientId := '11111111-1111-1111-1111-111111111111';
    vKeyProvider.ClientSecret := 'my-client-secret';
    vKeyProvider.AccountName := 'my-trusted-signing-account';
    vKeyProvider.CertificateProfileName := 'public-trust';
    vKeyProvider.Endpoint := 'https://eus.codesigning.azure.net';
    vKeyProvider.Connect;

    vSigner.KeyProvider := vKeyProvider;
    vSigner.Level := alBES;   // alT additionally requires a TSAClient
    vSigner.SignFile('myapp.exe', 'myapp-signed.exe');
  finally
    vSigner.Free;
    vKeyProvider.Free;
  end;
end;

sgcSign 如何使用 Azure Trusted Signing

从你自己的 Delphi 或 .NET 代码,或通过 sgcSign Server 从任意 CI 管道,使用 Azure Trusted Signing 为 Windows 应用程序签名,私钥始终保留在 Azure 中。

一个密钥提供程序,三处可以发起签名:你的 Delphi 应用程序、你的 .NET 代码,或通过签名服务器的构建管道。

Azure Trusted Signing(前称 Azure Code Signing)是 Microsoft 的代码签名服务。证书归 Microsoft 所有,并由其为你预配签名密钥,私钥完全保存在 Azure 中。sgcSign 通过 TsgcAzureTrustedSigningProvider 与其通信,后者使用 OAuth2 客户端凭据进行身份验证。

每个 sgcSign 密钥提供程序都实现 IsgcKeyProvider,因此签名代码不关心密钥存放在哪里。开发时使用本地 PFX,测试时使用 Windows 证书存储,发布时只需更换一个提供程序即可使用 Azure Trusted Signing。其他选项列在密钥提供程序页面。

从 Delphi 和 C++ Builder

TsgcAuthenticodeSigner 将 Azure 提供程序作为其 KeyProvider,在 Windows 上于进程内为 .exe、.dll、.sys、.ocx、.cpl 和 .scr 文件签名,无需外部签名工具。

从 .NET

esegece.sgcSign NuGet 包提供同样的 TsgcAzureTrustedSigningProvider 类,属性也相同,在 C# 中将其指定给签名器的 KeyProvider。

从 CI/CD

sgcSign Server 将 Azure 凭据保存为 AzureTS 提供程序。构建代理使用 API 密钥通过 HTTPS 签名,永远看不到租户、客户端或密钥。

需要配置的内容

在 Azure 门户中完成三个步骤,然后在 sgcSign 中配置提供程序的值。同样的值用于 Delphi 和 .NET 提供程序以及服务器配置。

从 Azure 账户到第一个签名

  1. 在 Azure 门户中创建 Azure Trusted Signing 账户。
  2. 创建证书配置文件并记下配置文件名称。
  3. 注册一个 Azure AD 应用程序,并授予它 Trusted Signing Certificate Profile Signer 角色。
  4. 使用租户 ID、客户端 ID、客户端密钥、账户名称和证书配置文件名称配置提供程序。Endpoint 设置你账户的签名端点 URL。

在签名之前对提供程序调用 Connect。在服务器中,客户端密钥可以来自环境变量,因此永远不会写入配置文件。

settings.txt
Delphi / .NET property     Server param (AzureTS)
TenantId                   tenant_id
ClientId                   client_id
ClientSecret               client_secret_env
AccountName                account
CertificateProfileName     certificate_profile
Endpoint                   endpoint

Delphi 中的 Authenticode 选项

上方主视觉中的代码就是全部工作。TsgcAuthenticodeSigner 的这些属性决定了它生成的签名。

带时间戳的签名

Level 默认为 alT,会通过指定给 TSAClient 的 TsgcTSAClient 添加 RFC 3161 时间戳。alBES 则不带时间戳签名。

哈希和发布者详细信息

Hash 默认为 ahSHA256。Description 和 URL 会在签名中嵌入程序名称和更多信息链接。

双重签名

将 AppendSignature 设为 True,可向已签名的文件添加嵌套签名,例如为旧版 Windows 使用 SHA-1,为当前 Windows 使用 SHA-256。

文件、流和内存

SignFile 读写文件,SignStream 处理流,SignBytes 为保存在内存中的二进制文件签名。

C# 中的同一个提供程序

.NET 库保留了 Delphi 的名称,因此同时使用两种语言签名的团队只需配置一次 Azure Trusted Signing。

安装、配置、指定

  • 使用 dotnet add package esegece.sgcSign 添加 Registered 包。
  • 在 TsgcAzureTrustedSigningProvider 上设置租户、客户端、密钥、账户和证书配置文件。
  • 将其指定给签名器的 KeyProvider,与 Delphi 中完全一样。
  • NuGet 包、ClickOnce 清单和 VSIX 扩展由 sgcSign Server 使用同一个 Azure 提供程序签名。
AzureProvider.cs
var azure = new TsgcAzureTrustedSigningProvider();
azure.TenantId = "your-tenant-id";
azure.ClientId = "your-client-id";
azure.ClientSecret = "your-client-secret";
azure.AccountName = "your-account-name";
azure.CertificateProfileName = "your-profile";

// Assign it to the KeyProvider of an sgcSign signer
signer.KeyProvider = azure;

使用 Azure Trusted Signing 从 CI 管道签名

将 Azure 凭据放在一台 sgcSign Server 上,而不是每个构建代理上。运行器持有 API 密钥,并通过 HTTPS 签名。

一个提供程序条目,适用于每个运行器

  • 在 sgcSignServer.conf.json 中添加一个 AzureTS 提供程序。任何以 _env 结尾的参数都从环境变量读取,因此客户端密钥不会出现在文件中。
  • sgcSignServer --selftest-providers 会加载配置,初始化每个提供程序然后退出,因此你可以在正式上线前检查 Azure 的值。
  • sgcsign 客户端在运行器上对可执行文件计算哈希,并且只发送哈希。服务器使用 Azure Trusted Signing 对其签名,客户端再嵌入签名。
  • 按项目划分的 API 密钥、可选的审批,以及哈希链式审计日志,会记录谁使用 Azure 配置文件签署了什么。
sgcSignServer.conf.json
"providers": [
  {
    "name": "release",
    "type": "AzureTS",
    "params": {
      "tenant_id": "00000000-0000-0000-0000-000000000000",
      "client_id": "00000000-0000-0000-0000-000000000000",
      "client_secret_env": "SGCSIGN_AZURE_TS_SECRET",
      "account": "your-account",
      "certificate_profile": "your-profile",
      "endpoint": "https://eus.codesigning.azure.net/"
    }
  }
]

rem On the build agent
set SGCSIGN_SERVER=https://sign.example.com:8443
set SGCSIGN_APIKEY=%SIGNING_API_KEY%
sgcsign sign -f authenticode -p release -o MyApp-signed.exe MyApp.exe

两分钟了解 sgcSign

从 Delphi 和 .NET 进行代码和文档签名,使用与 Azure 提供程序接入的同一个密钥提供程序模型。

从 Delphi 和 .NET 为代码和文档签名:PAdES、XAdES、CAdES 和 Authenticode,支持智能卡和 HSM。

常见问题解答

创建 TsgcAzureTrustedSigningProvider,设置 TenantId、ClientId、ClientSecret、AccountName、CertificateProfileName 和 Endpoint,然后调用 Connect。将其指定给 TsgcAuthenticodeSigner 的 KeyProvider,并使用输入和输出路径调用 SignFile。本页顶部的示例是完整的。

可以。在 sgcSign Server 上配置 AzureTS 提供程序,并为每个运行器提供一个 API 密钥。运行器调用 sgcsign sign 或 REST API,服务器使用 Azure Trusted Signing 签名,Azure 客户端密钥永远不会到达构建代理。

可以,通过 sgcSign Server。它会使用 Authenticode 为 .exe 和 .dll 文件签名,在 POST /api/v1/sign/nuget 为 .nupkg 包签名,也可为 ClickOnce 清单和 VSIX 扩展签名,全部使用你配置的密钥提供程序,包括 Azure Trusted Signing。在你自己的 .NET 代码中,esegece.sgcSign 包提供同样的 Azure 提供程序类。

不需要。使用 Azure Trusted Signing 时,证书归 Microsoft 所有,并由其预配签名密钥,私钥完全保存在 Azure 中。sgcSign 通过 OAuth2 客户端凭据使用它签名,因此构建机器上不会有证书或密钥文件。

一个 Azure Trusted Signing 账户、一个证书配置文件,以及一个具有 Trusted Signing Certificate Profile Signer 角色的 Azure AD 应用程序。由此你可以获得租户 ID、客户端 ID、客户端密钥、账户名称和配置文件名称。设置部分将每个值对应到 Delphi 属性和服务器参数。

可以。在 Delphi 中,将 Level 设为 alT,并将 TsgcTSAClient 指定给 TSAClient。sgcSign Server 保留了一份默认的 RFC 3161 时间戳服务器列表,并按顺序尝试直到有一个应答,当某个提供程序需要自己的服务器时,可使用每个提供程序独立的 tsa_urls 列表。

超值之选:All-AccesseSeGeCe 全部产品,含高级支持,每年 €1,059 起。
查看 All-Access 价格

今天就使用 Azure Trusted Signing 签名

从 Delphi 应用程序、.NET 程序或构建管道发起,私钥始终保留在 Azure 中。