[Bug]: `dotnet-mage` includes invalid timestamp in signed manifest
- Dominant language
- C#
- Stars
- 5.5k
- Forks
- 1.5k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 141
Description
### Issue Description
When both RFC 3161 timestamping and legacy Authenticode timestamping fail, `dotnet-mage` may put an invalid timestamp in a signed manifest.
CC @NikolaMilosavljevic, @clairernovotny
### Steps to Reproduce
1. [Install](https://github.com/dotnet/deployment-tools/blob/main/Documentation/dotnet-mage/README.md#prerequisites-for-using-this-tool) `dotnet-mage` 7.0 as a global tool.
2. Extract the contents of [WindowsFormsApp1.zip](https://github.com/dotnet/deployment-tools/files/11466502/WindowsFormsApp1.zip) to your local drive (e.g.: `C:\repro\WindowsFormsApp1`). (I created this C# Windows Forms App (.NET Framework) project in Visual Studio 2022, published it, deleted some files unnecessary to the repro, and ZIP-ed it up.)
3. Save [this PowerShell script](https://gist.github.com/dtivel/1f847698b4d649d6c1282a1bd5118c49) in a directory (e.g.: `C:\repro\repro.ps1`).
4. Where `` is the root directory for the extracted content, open a PowerShell prompt, path to the PowerShell script created in the previous step, and execute:
```PowerShell
.\repro.ps1 '\publish'
```
Example:
```PowerShell
.\repro.ps1 'C:\repro\WindowsFormsApp1\publish'
```
### Expected Behavior
The timestamp operation will fail, and timestamp failure details will be reported.
### Actual Behavior
Timestamping fails, but an invalid timestamp is put in the signed manifest, and success is reported.
### Analysis
The repro steps will attempt to sign the application manifest using a publisher certificate with the RSA SHA-384 signature algorithm and then RFC 3161 timestamp using the Azure Code Signing (ACS) timestamping service (http://timestamp.acs.microsoft.com).
[First](https://github.com/dotnet/msbuild/blob/9deb5b58fb2b02990cba4b7add8c8dcbb9f658ed/src/Tasks/ManifestUtil/mansign2.cs#L886), `dotnet-mage` requests an RFC 3161 timestamp. However, because the request has a SHA-1 message imprint, the timestamp server will reject the timestamping request. RFC 3161 provides a [mechanism](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2) for a timestamping server to return a rejection response with details. The ACS timestamping service does not support SHA-1, so the timestamping response is a rejection with a clear reason given. (I captured this response in Fiddler.)

Because the RFC 3161 timestamping request failed, `dotnet-mage` [falls back](https://github.com/dotnet/msbuild/blob/9deb5b58fb2b02990cba4b7add8c8dcbb9f658ed/src/Tasks/ManifestUtil/mansign2.cs#L910) to a legacy Authenticode timestamp. However, the ACS timestamping service does not support legacy Authenticode timestamping. Again, a rejection with a clear reason is given:

At this point, instead of verifying the received "timestamp", `dotnet-mage` puts the rejection response in the application manifest as the timestamp. Attempting to verify the manifest signature will result in:
```
ASN1 corrupted data.
Manifest does not have a valid signature.
```
This bug is unrelated to https://github.com/dotnet/deployment-tools/issues/275 except that this bug relies on that bug for an easy repro. In theory, this bug could repro even if https://github.com/dotnet/deployment-tools/issues/275 were fixed, but the repro would be different.
### Versions & Configurations
dotnet --info
```
.NET SDK:
Version: 7.0.400-preview.23225.8
Commit: c63317749e
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.400-preview.23225.8\
Host:
Version: 7.0.5
Architecture: x64
Commit: 8042d61b17
.NET SDKs installed:
3.1.426 [C:\Program Files\dotnet\sdk]
5.0.214 [C:\Program Files\dotnet\sdk]
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.311 [C:\Program Files\dotnet\sdk]
7.0.105 [C:\Program Files\dotnet\sdk]
7.0.203 [C:\Program Files\dotnet\sdk]
7.0.400-preview.23225.8 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0-alpha1.19530.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0-alpha1.19528.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0-alpha1.19530.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
```
dotnet tool list --global
```
Package Id Version Commands
--------------------------------------------------------------------
microsoft.dotnet.mage 7.0.0 dotnet-mage
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.