private static AssemblyInfo SignSingleAssembly(string assemblyPath, string keyPath, string outputDirectory, string password, params string[] probingPaths) { try { PrintMessage(null, LogLevel.Verbose); PrintMessage(string.Format("Strong-name signing '{0}'...", assemblyPath), LogLevel.Verbose); var oldInfo = SigningHelper.GetAssemblyInfo(assemblyPath); var newInfo = SigningHelper.SignAssembly(assemblyPath, keyPath, outputDirectory, password, probingPaths); if (!oldInfo.IsSigned && newInfo.IsSigned) { PrintMessageColor(string.Format("'{0}' was strong-name signed successfully.", newInfo.FilePath), LogLevel.Changes, ConsoleColor.Green); return(newInfo); } else { PrintMessage("Already strong-name signed...", LogLevel.Verbose); } } catch (BadImageFormatException bife) { PrintMessageColor(string.Format("Warning: {0}", bife.Message), LogLevel.Silent, ConsoleColor.Yellow); } catch (Exception ex) { PrintMessageColor(string.Format("Error: {0}", ex.Message), LogLevel.Silent, ConsoleColor.Red); } return(null); }
private static bool TrySignPackagePart(PackagePart packagePart, string keyPath, string keyPassword, bool signedPackage) { if (packagePart.Uri.ToString().EndsWith(".exe") || packagePart.Uri.ToString().EndsWith(".dll")) { string tempPath = Path.GetTempFileName(); try { using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write)) { packagePart.GetStream().CopyTo(stream); } if (!SigningHelper.GetAssemblyInfo(tempPath).IsSigned) { signedPackage = true; SigningHelper.SignAssembly(tempPath, keyPath ?? string.Empty, Path.GetDirectoryName(tempPath), keyPassword ?? string.Empty); using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Read)) { stream.CopyTo(packagePart.GetStream(FileMode.Create, FileAccess.Write)); } } } finally { File.Delete(tempPath); } } return(signedPackage); }
public void SignAssembly_NewLocationWithPdb_Should_Succeed() { var tempDir = Path.Combine(TestAssemblyDirectory, Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempDir); var outDir = Path.Combine(tempDir, "out"); Directory.CreateDirectory(outDir); try { string sourceAssemblyPath = Path.Combine(tempDir, "Brutal.Dev.StrongNameSigner.TestAssembly.A.dll"); File.Copy(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.A.dll"), sourceAssemblyPath); File.Copy(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.A.pdb"), Path.Combine(tempDir, "Brutal.Dev.StrongNameSigner.TestAssembly.A.pdb")); SigningHelper.SignAssembly(sourceAssemblyPath, null, outDir); string outAssembly = Path.Combine(outDir, Path.GetFileName(sourceAssemblyPath)); Assert.IsTrue(File.Exists(outAssembly)); Assert.IsTrue(File.Exists(Path.ChangeExtension(outAssembly, ".pdb"))); var info = SigningHelper.GetAssemblyInfo(outAssembly); Assert.IsTrue(info.IsSigned); } finally { Directory.Delete(tempDir, true); } }
public void FixAssemblyReference_Should_Fix_InternalsVisbileTo() { // Sign assembly A. SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.A.dll"), null, Path.Combine(TestAssemblyDirectory, "IVT")); // Copy unsigned assembly B and just fix the references (has none but will fix InternalsVisibleTo). File.Copy(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.B.dll"), Path.Combine(TestAssemblyDirectory, "IVT", "Brutal.Dev.StrongNameSigner.TestAssembly.B.dll"), true); SigningHelper.FixAssemblyReference(Path.Combine(TestAssemblyDirectory, "IVT", "Brutal.Dev.StrongNameSigner.TestAssembly.A.dll"), Path.Combine(TestAssemblyDirectory, "IVT", "Brutal.Dev.StrongNameSigner.TestAssembly.B.dll")).ShouldBe(true); }
public void SignAssembly_Should_Reassemble_NET_20_x86_Assembly_Correctly() { var info = SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20-x86.exe"), string.Empty, Path.Combine(TestAssemblyDirectory, "Signed")); info.DotNetVersion.ShouldBe("2.0.50727"); info.IsAnyCpu.ShouldBe(false); info.Is32BitOnly.ShouldBe(true); info.Is64BitOnly.ShouldBe(false); info.IsSigned.ShouldBe(true); }
public void SignAssembly_Public_API_Test() { var info = SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20.exe"), string.Empty, Path.Combine(TestAssemblyDirectory, "Signed")); info.ShouldNotBe(null); info.DotNetVersion.ShouldBe("2.0.50727"); info.Is32BitOnly.ShouldBe(false); info.Is32BitPreferred.ShouldBe(false); info.Is64BitOnly.ShouldBe(false); info.IsAnyCpu.ShouldBe(true); info.IsSigned.ShouldBe(true); }
public void SignAssembly_InPlaceWithoutPdb_Should_Succeed() { var tempDir = Path.Combine(TestAssemblyDirectory, Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempDir); try { string targetAssemblyPath = Path.Combine(tempDir, "Brutal.Dev.StrongNameSigner.TestAssembly.A.dll"); File.Copy(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.A.dll"), targetAssemblyPath); SigningHelper.SignAssembly(targetAssemblyPath); var info = SigningHelper.GetAssemblyInfo(targetAssemblyPath); Assert.IsTrue(info.IsSigned); } finally { Directory.Delete(tempDir, true); } }
private static bool SignSingleAssembly(string assemblyPath, string keyPath, string outputDirectory) { try { C.WriteLine(); C.WriteLine("Strong-name signing {0}...", assemblyPath); var info = SigningHelper.GetAssemblyInfo(assemblyPath); if (!info.IsSigned) { info = SigningHelper.SignAssembly(assemblyPath, keyPath, outputDirectory); C.ForegroundColor = ConsoleColor.Green; C.WriteLine("{0} was strong-name signed successfully!", info.FilePath); C.ResetColor(); return(true); } else { C.WriteLine("Already strong-name signed..."); } } catch (BadImageFormatException bife) { C.ForegroundColor = ConsoleColor.Yellow; C.WriteLine("Warning: {0}", bife.Message); C.ResetColor(); } catch (Exception ex) { C.ForegroundColor = ConsoleColor.Red; C.WriteLine("Error: {0}", ex.Message); C.ResetColor(); } return(false); }
public void SignAssembly_Public_API_Password_Protected_Key_Path_Should_Throw_Exception_When_Wrong_Password_Provided() { SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20.exe"), Path.Combine(TestAssemblyDirectory, "PasswordTest.pfx"), Path.Combine(TestAssemblyDirectory, "Signed"), "oops"); }
public void SignAssembly_Public_API_Invalid_Path_Should_Throw_Exception() { SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20.doesnotexist")); }
public void SignAssembly_Public_API_Invalid_Key_Path_Should_Throw_Exception_For_Missing_Directory() { Assert.Throws <DirectoryNotFoundException>(() => SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET40.exe"), "C:\\DoesNotExist\\KeyFile.snk")); }
private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) { e.Result = string.Empty; int progress = 0; int signedFiles = 0; int referenceFixes = 0; var processedAssemblyPaths = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var signedAssemblyPaths = new HashSet <string>(StringComparer.OrdinalIgnoreCase); if (e.Argument is IEnumerable <string> assemblyPaths) { var probingPaths = assemblyPaths.Select(f => Path.GetDirectoryName(f)).Distinct().ToArray(); // We go through assemblies three times and every assembly -1 for reference fixes. double progressMax = (assemblyPaths.Count() + (assemblyPaths.Count() * (assemblyPaths.Count() - 1))) * 3; foreach (var filePath in assemblyPaths) { var assemblyPair = new AssemblyPair(); try { log.AppendFormat("Strong-name signing {0}...", filePath).AppendLine(); assemblyPair.OldInfo = SigningHelper.GetAssemblyInfo(filePath); assemblyPair.NewInfo = SigningHelper.SignAssembly(filePath, keyFile, outputPath, password, probingPaths); if (assemblyPair.NewInfo.SigningType == StrongNameType.DelaySigned) { log.Append("Delay-signed assembly signing is not supported yet...").AppendLine(); } else if (!assemblyPair.OldInfo.IsSigned && assemblyPair.NewInfo.IsSigned) { log.Append("Strong-name signed successfully.").AppendLine(); signedAssemblyPaths.Add(assemblyPair.NewInfo.FilePath); signedFiles++; } else { log.Append("Already strong-name signed...").AppendLine(); } processedAssemblyPaths.Add(assemblyPair.NewInfo.FilePath); } catch (Exception ex) { log.AppendFormat("Error strong-name signing {0}: {1}", filePath, ex.Message).AppendLine(); } backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100), assemblyPair); if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } } var referencesToFix = new HashSet <string>(processedAssemblyPaths, StringComparer.OrdinalIgnoreCase); foreach (var filePath in processedAssemblyPaths) { // Go through all the references excluding the file we are working on. foreach (var reference in referencesToFix.Where(r => !r.Equals(filePath))) { backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100)); if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } log.AppendFormat("Fixing references to {1} in {0}...", filePath, reference).AppendLine(); if (SigningHelper.FixAssemblyReference(filePath, reference, keyFile, password, probingPaths)) { log.Append("Reference was found and fixed.").AppendLine(); referenceFixes++; } else { log.Append("Nothing to fix.").AppendLine(); } } } // Go through all processed assemblies and remove invalid friend references. foreach (var filePath in signedAssemblyPaths) { backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100)); if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } log.AppendFormat("Removing invalid friend references from '{0}'...", filePath).AppendLine(); if (SigningHelper.RemoveInvalidFriendAssemblies(filePath, keyFile, password, probingPaths)) { log.Append("Invalid friend assemblies removed.").AppendLine(); referenceFixes++; } else { log.Append("Nothing to fix.").AppendLine(); } } e.Result = string.Format(CultureInfo.CurrentCulture, "{0} out of {1} assemblies were strong-name signed and {2} references were fixed.", signedFiles, assemblyPaths.Count(), referenceFixes); } }
private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) { e.Result = string.Empty; int progress = 0; int signedFiles = 0; int referenceFixes = 0; var assemblies = e.Argument as IEnumerable <string>; if (assemblies != null) { // We go through assemblies twice and every assembly -1 for reference fixes. double progressMax = assemblies.Count() + (assemblies.Count() * (assemblies.Count() - 1)); foreach (var assembly in assemblies) { AssemblyInfo info = null; try { log.AppendFormat("Strong-name signing {0}...", assembly).AppendLine(); info = SigningHelper.GetAssemblyInfo(assembly); if (!info.IsSigned) { info = SigningHelper.SignAssembly(assembly, keyFile, outputPath); log.Append("Strong-name signed successfully.").AppendLine(); signedFiles++; } else { log.Append("Already strong-name signed...").AppendLine(); } } catch (Exception ex) { log.AppendFormat("Error strong-name signing {0}: {1}", assembly, ex.Message).AppendLine(); } backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100), info); if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } } var referencesToFix = new List <string>(assemblies); foreach (var assembly in assemblies) { // Go through all the references excluding the file we are working on. foreach (var reference in referencesToFix.Where(r => !r.Equals(assembly))) { backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100)); if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } log.AppendFormat("Fixing references to {1} in {0}...", assembly, reference).AppendLine(); if (SigningHelper.FixAssemblyReference(assembly, reference)) { log.Append("Reference was found and fixed.").AppendLine(); referenceFixes++; } else { log.Append("Nothing to fix.").AppendLine(); } } } e.Result = string.Format(CultureInfo.CurrentCulture, "{0} out of {1} assemblies were strong-name signed and {2} references were fixed.", signedFiles, assemblies.Count(), referenceFixes); } }
static void Main(string[] args) { var newInfo = SigningHelper.SignAssembly(@"C:\MyAssembly.dll"); }
public void SignAssembly_Public_API_Password_Protected_Key_Path_Should_Work_With_Correct_Password() { SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20.exe"), Path.Combine(TestAssemblyDirectory, "PasswordTest.pfx"), Path.Combine(TestAssemblyDirectory, "Signed"), "password123"); }
public void SignAssembly_Public_API_Invalid_Key_Path_Should_Throw_Exception_For_Missing_File() { SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20.exe"), "C:\\KeyFileThatDoesNotExist.snk"); }
public void SignAssembly_Public_API_Password_Protected_Key_Path_Should_Throw_Exception_When_Password_Not_Provided() { Assert.Throws <CryptographicException>(() => SigningHelper.SignAssembly(Path.Combine(TestAssemblyDirectory, "Brutal.Dev.StrongNameSigner.TestAssembly.NET20.exe"), Path.Combine(TestAssemblyDirectory, "PasswordTest.pfx"))); }