///
/// Copyright © 2003-2008 JetBrains s.r.o.
/// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder).
///
using System;
using System.Security;
using JetBrains.Build.Common.Infra;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
namespace JetBrains.Build.Common.Tasks
{
///
/// Signs the files with Microsoft Authenticode.
/// Note: this file calls the MSBuild Security Utilities to sign the file, instead of calling SignTool.exe manually.
///
public class Sign_SecurityUtilities : TaskBase
{
#region Attributes
///
/// The input files that will be signed by the task.
///
[Required]
public ITaskItem[] InputFiles
{
get
{
return BagGetTry(AttributeName.InputFiles);
}
set
{
BagSet(AttributeName.InputFiles, value);
}
}
///
/// The file that contains the private keys to use for signing.
///
[Required]
public ITaskItem KeyFile
{
get
{
return BagGetTry(AttributeName.KeyFile);
}
set
{
BagSet(AttributeName.KeyFile, value);
}
}
///
/// An optional password to the key file.
///
public string Password
{
get
{
return BagGet(AttributeName.Password);
}
set
{
BagSet(AttributeName.Password, value);
}
}
///
/// Specifies an optional server to timestamp the files being signed.
///
public string TimestampingServer
{
get
{
return BagGetTry(AttributeName.TimestampingServer);
}
set
{
BagSet(AttributeName.TimestampingServer, value);
}
}
#endregion
#region Overrides
///
/// The method to be overriden in inheriting tasks.
/// Throw an exception in case of an errror.
///
protected override void ExecuteTask()
{
// Keyfile password
var password = new SecureString();
foreach(char c in BagGet(AttributeName.Password, ""))
password.AppendChar(c);
// Timestamping server
string sTimestampServer = BagGet(AttributeName.TimestampingServer, "");
Uri uriTimestampServer = string.IsNullOrEmpty(sTimestampServer) ? null : new Uri(sTimestampServer);
if(uriTimestampServer == null)
Log.LogWarning("It would be better to specify the Timestamping Server Uri.");
// Sign each file
foreach(ITaskItem item in BagGet(AttributeName.InputFiles))
SecurityUtilities.SignFile(GetStringValue(AttributeName.KeyFile), password, uriTimestampServer, item.GetMetadata("FullPath"));
}
#endregion
}
}