namespace ReleaseBuilder;
public static class ProcessRunner
{
///
/// The hash algorithms used for signing with Authenticode
///
private static readonly IReadOnlyList OSSLHashAlgs = new[] { "sha1", "sha256" };
///
/// The company name to encode in the Authenticode certificate
///
private const string OSSLOrganization = "Duplicati";
///
/// The url to encode in the Authenticode certificate
///
private const string OSSLUrl = "https://duplicati.com";
///
/// Performs code signing of the
///
/// The path to the signcode binary
/// The path to the PFX file
/// The password to decrypt the PFX file
/// The executable to sign, in-place
/// An awaitable task
public static async Task OsslCodeSign(string osslsigncode, string pfxfile, string pfxpassword, string executable)
{
var first = true;
foreach (var hashalg in OSSLHashAlgs)
{
var tmp = Path.GetTempFileName();
File.Delete(tmp);
var args = new[] {
osslsigncode, "sign",
"-pkcs12", pfxfile,
"-pass", pfxpassword,
"-n", OSSLOrganization,
"-i", OSSLUrl,
"-h", hashalg,
first ? "" : "-nest",
"-t", $"http://timestamp.digicert.com?alg={hashalg}",
"-in", executable,
"-out", tmp
};
await ProcessHelper.Execute(args.Where(x => !string.IsNullOrWhiteSpace(x)));
File.Move(tmp, executable, true);
first = false;
}
}
///
/// Runs MacOS codesign on a single file
///
/// The path to the codesign binary
/// The identity used for codesign
/// The entitlements to activate for the file
/// The file to sign
/// An awaitable task
public static Task MacOSCodeSign(string codesign, string codesignIdentity, string entitlementFile, string file)
=> ProcessHelper.Execute([
codesign,
"--force",
"--timestamp",
"--options=runtime",
"--entitlements", entitlementFile,
"--sign", codesignIdentity,
file
]);
///
/// Runs MacOS codesign on a single file
///
/// The path to the productsign binary
/// The identity used for codesign
/// The entitlements to activate for the file
/// The file to sign
/// An awaitable task
public static async Task MacOSProductSign(string productsign, string codesignIdentity, string file)
{
var outputfile = file + ".signed";
await ProcessHelper.Execute([
productsign,
"--sign", codesignIdentity,
file,
outputfile
]);
File.Move(outputfile, file, true);
}
}