-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SignFile.cs
99 lines (90 loc) · 3.37 KB
/
SignFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
using Microsoft.Build.Utilities;
#nullable disable
namespace Microsoft.Build.Tasks
{
/// <summary>
/// This task signs the passed in file using the Authenticode cert
/// provided and optionally uses a timestamp if a URL is provided.
/// It can sign ClickOnce manifests as well as exe's.
/// </summary>
[SupportedOSPlatform("windows")]
public sealed class SignFile : Task
{
public SignFile()
: base(AssemblyResources.PrimaryResources, "MSBuild.")
{
}
[Required]
public string CertificateThumbprint { get; set; }
[Required]
public ITaskItem SigningTarget { get; set; }
public string TargetFrameworkIdentifier { get; set; } = Constants.DotNetFrameworkIdentifier;
public String TargetFrameworkVersion { get; set; }
public string TimestampUrl { get; set; }
public bool DisallowMansignTimestampFallback { get; set; } = false;
public override bool Execute()
{
if (!NativeMethodsShared.IsWindows)
{
Log.LogErrorWithCodeFromResources("General.TaskRequiresWindows", nameof(SignFile));
return false;
}
try
{
SecurityUtilities.SignFile(
CertificateThumbprint,
TimestampUrl == null ? null : new Uri(TimestampUrl),
SigningTarget.ItemSpec,
TargetFrameworkVersion,
TargetFrameworkIdentifier,
DisallowMansignTimestampFallback);
return true;
}
catch (ArgumentException ex) when (ex.ParamName.Equals("certThumbprint"))
{
Log.LogErrorWithCodeFromResources("SignFile.CertNotInStore");
return false;
}
catch (FileNotFoundException ex)
{
Log.LogErrorWithCodeFromResources("SignFile.TargetFileNotFound", ex.FileName);
return false;
}
catch (ApplicationException ex)
{
Log.LogErrorWithCodeFromResources("SignFile.SignToolError", ex.Message.Trim());
return false;
}
catch (WarningException ex)
{
Log.LogWarningWithCodeFromResources("SignFile.SignToolWarning", ex.Message.Trim());
return true;
}
catch (CryptographicException ex)
{
Log.LogErrorWithCodeFromResources("SignFile.SignToolError", ex.Message.Trim());
return false;
}
catch (Win32Exception ex)
{
Log.LogErrorWithCodeFromResources("SignFile.SignToolError", ex.Message.Trim());
return false;
}
catch (UriFormatException ex)
{
Log.LogErrorWithCodeFromResources("SignFile.SignToolError", ex.Message.Trim());
return false;
}
}
}
}