diff --git a/src/SecureSign.Core/Models/PathConfig.cs b/src/SecureSign.Core/Models/PathConfig.cs index d661cf6..942446e 100644 --- a/src/SecureSign.Core/Models/PathConfig.cs +++ b/src/SecureSign.Core/Models/PathConfig.cs @@ -49,5 +49,15 @@ public class PathConfig /// public string SignTool { get; set; } = @"C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86\signtool.exe"; + /// + /// Gets or sets the path to nuget.exe + /// + public string Nuget { get; set; } = @"nuget"; + + /// + /// Gets or sets the timestamp service url for Authenticode signing + /// + public string Timestamper { get; set; } = @"http://timestamp.digicert.com"; + } } diff --git a/src/SecureSign.Core/Signers/AuthenticodeSigner.cs b/src/SecureSign.Core/Signers/AuthenticodeSigner.cs index aaf73a2..6641504 100644 --- a/src/SecureSign.Core/Signers/AuthenticodeSigner.cs +++ b/src/SecureSign.Core/Signers/AuthenticodeSigner.cs @@ -66,6 +66,11 @@ public async Task SignAsync(Stream input, X509Certificate2 cert, string File.WriteAllBytes(certFile, exportedCert); await input.CopyToFileAsync(inputFile); + if (fileExtention == "nupkg") + { + return await SignUsingNugetAsync(inputFile, certFile, password); + } + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (fileExtention.Contains("ps")) @@ -110,7 +115,7 @@ await RunProcessAsync( $"/p \"{CommandLineEncoder.Utils.EncodeArgText(certPassword)}\"", $"/d \"{CommandLineEncoder.Utils.EncodeArgText(description)}\"", $"/du \"{CommandLineEncoder.Utils.EncodeArgText(url)}\"", - "/tr http://timestamp.digicert.com", + $"/tr {_pathConfig.Timestamper}", "/td sha256", "/fd sha256", $"\"{CommandLineEncoder.Utils.EncodeArgText(inputFile)}\"", @@ -137,7 +142,7 @@ await RunProcessAsync( "-command", "\"$Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2;", $"$Cert.Import('{CommandLineEncoder.Utils.EncodeArgText(certFile)}','{CommandLineEncoder.Utils.EncodeArgText(certPassword)}',[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet);", - $"Set-AuthenticodeSignature '{CommandLineEncoder.Utils.EncodeArgText(inputFile)}' $Cert -Timestamp http://timestamp.digicert.com\"", + $"Set-AuthenticodeSignature '{CommandLineEncoder.Utils.EncodeArgText(inputFile)}' $Cert -Timestamp {_pathConfig.Timestamper}\"", } ); @@ -145,6 +150,50 @@ await RunProcessAsync( return File.OpenRead(inputFile); } + /// + /// Signs the specified file using nuget.exe. Needs to be a nupkg + /// + /// File to sign + /// Path to the certificate to use for signing + /// Password for the certificate + /// A signed copy of the file + private async Task SignUsingNugetAsync(string inputFile, string certFile, string certPassword) + { + // if we aren't windows, we need to call nuget from mono + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + await RunProcessAsync( + "mono", + new[] + { + _pathConfig.Nuget, + "sign", + $"-CertificatePath \"{CommandLineEncoder.Utils.EncodeArgText(certFile)}\"", + $"-CertificatePassword \"{CommandLineEncoder.Utils.EncodeArgText(certPassword)}\"", + $"-Timestamper {_pathConfig.Timestamper}", + $"\"{CommandLineEncoder.Utils.EncodeArgText(inputFile)}\"", + } + ); + } + else + { + await RunProcessAsync( + _pathConfig.Nuget, + new[] + { + "sign", + $"-CertificatePath \"{CommandLineEncoder.Utils.EncodeArgText(certFile)}\"", + $"-CertificatePassword \"{CommandLineEncoder.Utils.EncodeArgText(certPassword)}\"", + $"-Timestamper {_pathConfig.Timestamper}", + $"\"{CommandLineEncoder.Utils.EncodeArgText(inputFile)}\"", + } + ); + } + + // nuget signs in-place, so just return the file we were given. + return File.OpenRead(inputFile); + } + /// /// Signs the specified file using osslsigncode /// @@ -204,7 +253,7 @@ private async Task RunOsslSignCodeAsync(string certFile, string certPasswordFile var args = new List { "sign", - "-ts http://timestamp.digicert.com", + $"-ts {_pathConfig.Timestamper}", $"-n \"{CommandLineEncoder.Utils.EncodeArgText(description)}\"", $"-i \"{CommandLineEncoder.Utils.EncodeArgText(url)}\"", $"-pkcs12 \"{CommandLineEncoder.Utils.EncodeArgText(certFile)}\"",